2code icon indicating copy to clipboard operation
2code copied to clipboard

Task 8: Adopt existing profiles and worktrees

Open AkaraChen opened this issue 2 months ago • 0 comments

Summary

Connect existing 2code checkouts to Herdr v0.9.0 without recreating them, as required by parent plan #394. Implement resumable adoption for default profiles and existing worktree profiles. Do not switch the default runtime, create new git worktrees, create/close Herdr terminals, or replace the fail-closed Herdr terminal adapter.

Pin remains Herdr v0.9.0, JSON protocol 22, endpoint generation 1. Adoption primitive: JSON worktree.open with absolute --path / --cwd, --trust-repository, and --no-focus, as verified in docs/herdr-integration.md Workspace and worktree lifecycle. Repeat open returns already_open: true with the same workspace_id / pane_id. Namespace: Task 4 SESSION_NAME (2code) in src-tauri/crates/infra/src/herdr/process.rs. Persist via Task 7 bind_profile_workspace / replace_profile_workspace. Classify stored ids with Task 7 workspace_identity_state against a Task 6 RuntimeProjection. worktree_path / project folder stay reconciled caches, not workspace authority. Default profiles still point at the original checkout.

A 2code project remains a repository entry. Each profile maps to one Herdr workspace (primary checkout or linked worktree). Herdr’s root pane from worktree.open is not a 2code terminal tab in this task. Do not persist terminal_id or bind session_runtime_mappings.

Dependencies: Parent plan #394. Prerequisites: #395 (Task 1, accepted on issue-395-herdr-integration-contract), #402 (Task 2, accepted on task-2-runtime-boundary), #404 (Task 3, accepted on task-3-herdr-sidecar), #406 (Task 4, accepted on task-4-server-discovery), #409 (Task 5, accepted on task-5-socket-client), #411 (Task 6, accepted on task-6-runtime-sync), #414 (Task 7, accepted on task-7-runtime-mapping). Checkout base: #414 (task-7-runtime-mapping). Open contract-gap issues #396–#401, Task 2 gap #403, docs gap #407, Task 6 event-apply gap #413, and line-count trackers #400/#405/#408/#410/#412/#415 do not block Linux Task 8. #416 does not block starting this task, but this task must not assume replace_profile_workspace already cascades onto session rows: adoption replace must keep session_runtime_mappings.workspace_id consistent (unbind stale session mappings for that profile, or refuse replace until they are rebound). Live Windows named-pipe remains #399. Live macOS Unix sockets remain #401.

Work to complete

  1. Resumable adoption service

    • Add an explicit adopt/resume entry point (tests and later Herdr-selected / Task 19 startup). Do not call it from Local default startup in src-tauri/src/lib.rs. Do not start ensure_herdr_listener from that Local path.
    • Connect only to Task 4’s dedicated 2code socket. Never the user default session or $XDG_CONFIG_HOME/herdr/herdr.sock.
    • Walk existing profiles in a stable order (projects, then default profile before worktree profiles). For each profile, resolve a canonical checkout path: default profile → original project folder / worktree_path; non-default → existing worktree_path. cwd is the project git dir (project folder); --path is the canonical checkout. Socket params must be absolute. Pass --trust-repository and --no-focus.
    • Use JSON worktree.open only. Never worktree.create, git worktree add, git worktree remove, workspace.create as a checkout-recreation path, workspace.close, worktree.remove, pane.close, pane.split, or setup/teardown scripts.
    • If canonicalize fails (missing checkout), record a per-profile error and continue; do not create directories or git worktrees. Prior successful binds stay persisted.
  2. Reuse mappings and checkpoint after each success

    • If a profile mapping exists and workspace_identity_state is Bound against the current projection, skip that profile.
    • If no mapping exists, worktree.open, then bind_profile_workspace with the returned workspace_id in namespace 2code. Persist immediately before the next profile (the mapping row is the resume checkpoint). Do not wrap the whole catalog in one transaction.
    • If the stored workspace_id is Missing, worktree.open again and replace_profile_workspace with the returned id (explicit Replaced). Do not rematch by label, path, or worktree_path onto some other projected workspace.
    • Retry after interruption must not create a second workspace for the same checkout. Depend on verified already_open: true + the same workspace_id. Uniqueness still refuses binding one workspace_id to two profiles.
    • worktree.* is outcome-uncertain (outcome_uncertain). Do not assume an in-flight open committed. Recover by a subsequent worktree.open (idempotent already_open) or a fresh snapshot, then bind the returned workspace_id. Do not auto-replay other mutations.
    • Keep Herdr wire envelopes (method, params, result) inside the Herdr adapter / adoption module, not model or handler signatures. A typed worktree.open helper on HerdrClient is fine.
  3. Identity integrity on replace (#416)

    • When replace_profile_workspace changes a profile’s workspace_id, do not leave that profile’s session_runtime_mappings.workspace_id pointing at the previous workspace.
    • Adoption does not bind session→pane. If session rows already exist, unbind them on workspace replace (old pane_ids belong to the missing workspace) or refuse replace until they are unbound. Do not invent pane rematch. Do not rewrite pane_id from terminal_id or labels.
    • Tests: mixed profile/session workspace ids after replace are a failure.
  4. Wire without transferring live ownership

    • Keep HerdrStubAdapter fail-closed for create/restore/close/write/resize. Adoption must not implement TerminalRuntime create/close.
    • Do not bind RuntimeSelector session ids to Herdr. Local PTYs stay Local. Do not close or restore Local sessions as part of adoption.
    • Do not write session_runtime_mappings. The root pane returned by worktree.open is Herdr workspace state for Task 9 to reuse later; it is not a 2code tab in this task.
    • Do not use herdr-client.sock or herdr terminal session. Do not call server.stop. GUI exit still only drops client helpers.
    • Do not change create_profile / delete_profile / project delete. New worktree creation stays Local until Task 14; cleanup stays Task 15.
    • Do not change Local Profile / Project IPC shape. Do not update worktree_path as a side effect of adoption.
  5. Prove with tests

    • In-memory / fake opener: interrupt after the first profile bind, retry, second profile adopted, first mapping unchanged, no duplicate workspace_ids.
    • Default profile dirty primary checkout: files preserved; no git worktree add; path canonicalization (trailing slash / relative) still one workspace.
    • Existing dirty linked worktree profile: worktree.open preserves uncommitted files; does not recreate the checkout; does not call worktree.create.
    • Bound mapping + projection hit → skip open (or already_open with same id). Missing mapping + new open → bind. Missing stored id + reopen → replace, not silent path rematch onto a different id.
    • #416: replace then inspect session rows; no mixed workspace_ids on the same profile.
    • Optional live check against Task 4 ensure_server + Task 5 client on the 2code namespace with a disposable repo. Skip without sidecar unless an explicit required flag is set. Never target the user default session. Live fixtures must clean Herdr workspace state without deleting the user’s real checkouts.
    • Existing PTY, Task 2 routing, Task 6 projection, and Task 7 mapping tests keep passing. Local default still does not ensure a Herdr listener, start sync, or start adoption.

Likely files and directories

  • New: src-tauri/crates/service/src/runtime_adoption.rs — adopt/resume loop, canonical paths, per-profile checkpoint; colocated tests
  • Update: src-tauri/crates/repo/src/runtime_mapping.rs — replace_profile_workspace must not leave stale session workspace_ids (#416)
  • Update: src-tauri/crates/infra/src/herdr/transport.rs or src-tauri/crates/service/src/runtime/herdr.rs — typed worktree.open; keep TerminalRuntime fail-closed
  • Update: src-tauri/crates/service/src/lib.rs — export adopt/resume that is not on the Local default path
  • Update if needed: src-tauri/crates/model/src/error.rs — structured adoption/missing-path errors only; no wire envelopes
  • Reuse, do not duplicate: Task 7 mapping tables and uniqueness, Task 6 projection id rules, Task 4 SESSION_NAME, Task 5 HerdrClient / outcome_uncertain, Task 1 lifecycle table in docs/herdr-integration.md
  • Do not rewrite: Local PTY, Task 2 routing, Task 6 subscribe/apply, create_profile / delete_profile, frontend terminal code, docs/herdr-integration.md

Estimated changes: 300–500 lines total, additions plus deletions, including tests; excluding generated files and lockfile churn. Material scope growth should become a separate issue.

Acceptance criteria

  • [ ] Resumable adoption connects default profiles and existing worktree profiles to Herdr via worktree.open on canonical paths without recreating checkouts.
  • [ ] Progress is recorded after each successful profile bind; interruption + retry does not create duplicate workspaces or extra git worktrees.
  • [ ] Identities are workspace_id in namespace 2code, never display names. terminal_id is not persisted. worktree_path remains a cache.
  • [ ] Missing stored workspaces are reopened and explicitly replaced. Tests prove no silent rebind by label or path. Session mapping workspace_id does not go stale on profile replace (#416).
  • [ ] Production adoption does not call worktree.create, git worktree add/remove, pane.close, server.stop, or CLI terminal attach. Session→pane rows are not written.
  • [ ] Default runtime remains Local. Herdr TerminalRuntime stays fail-closed. Local PTY/agent path still works. No 2code session or worktree is owned by both backends. Adoption/sync are not started from Local default startup.
  • [ ] Windows named-pipe code may compile; live Windows is not claimed (#399). macOS live Unix is not claimed (#401).
  • [ ] cargo test --workspace from src-tauri (or cargo test --workspace --exclude code where GTK is unavailable). Frontend lint, typecheck, and tests apply only if frontend scope is introduced. IPC changes (none expected) would use bun run typegen; do not edit src/generated/ by hand.

Out of scope

  • Herdr terminal create/close and root-pane tab mapping (Task 9), live frame bridge (Task 10), xterm transport (Task 11), reattachment UI (Task 12), agent notifications (Task 13).
  • Creating new profiles through Herdr worktree.create (Task 14) or routing delete/teardown through Herdr (Task 15).
  • User-facing runtime health UI, legacy-session migration UI, or making Herdr the default (Tasks 18/19/21).
  • Closing or implementing contract-gap issues #396–#401, Task 2 gap #403, docs gap #407, Task 6 event-apply gap #413, or line-count trackers #400/#405/#408/#410/#412/#415 except the #416 replace integrity this task needs for adoption.
  • Applying pane.exited / layout.updated / tab.moved in the projection (#413).
  • Updating docs/herdr-integration.md.
  • Removing the local PTY runtime or agent detector.
  • Changing frontend terminal rendering, restoration, or detection.
  • Remote Herdr operation.

AkaraChen avatar Sep 15 '26 10:09 AkaraChen