Task 8: Adopt existing profiles and worktrees
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
-
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 startensure_herdr_listenerfrom that Local path. - Connect only to Task 4’s dedicated
2codesocket. 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 → existingworktree_path.cwdis the project git dir (projectfolder);--pathis the canonical checkout. Socket params must be absolute. Pass--trust-repositoryand--no-focus. - Use JSON
worktree.openonly. Neverworktree.create,git worktree add,git worktree remove,workspace.createas 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.
- Add an explicit adopt/resume entry point (tests and later Herdr-selected / Task 19 startup). Do not call it from Local default startup in
-
Reuse mappings and checkpoint after each success
- If a profile mapping exists and
workspace_identity_stateis Bound against the current projection, skip that profile. - If no mapping exists,
worktree.open, thenbind_profile_workspacewith the returnedworkspace_idin namespace2code. 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_idis Missing,worktree.openagain andreplace_profile_workspacewith the returned id (explicit Replaced). Do not rematch by label, path, orworktree_pathonto some other projected workspace. - Retry after interruption must not create a second workspace for the same checkout. Depend on verified
already_open: true+ the sameworkspace_id. Uniqueness still refuses binding oneworkspace_idto two profiles. worktree.*is outcome-uncertain (outcome_uncertain). Do not assume an in-flight open committed. Recover by a subsequentworktree.open(idempotentalready_open) or a fresh snapshot, then bind the returnedworkspace_id. Do not auto-replay other mutations.- Keep Herdr wire envelopes (
method,params,result) inside the Herdr adapter / adoption module, notmodelor handler signatures. A typedworktree.openhelper onHerdrClientis fine.
- If a profile mapping exists and
-
Identity integrity on replace (#416)
- When
replace_profile_workspacechanges a profile’sworkspace_id, do not leave that profile’ssession_runtime_mappings.workspace_idpointing 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 rewritepane_idfromterminal_idor labels. - Tests: mixed profile/session workspace ids after replace are a failure.
- When
-
Wire without transferring live ownership
- Keep
HerdrStubAdapterfail-closed for create/restore/close/write/resize. Adoption must not implementTerminalRuntimecreate/close. - Do not bind
RuntimeSelectorsession 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 byworktree.openis Herdr workspace state for Task 9 to reuse later; it is not a 2code tab in this task. - Do not use
herdr-client.sockorherdr terminal session. Do not callserver.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/ProjectIPC shape. Do not updateworktree_pathas a side effect of adoption.
- Keep
-
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.openpreserves uncommitted files; does not recreate the checkout; does not callworktree.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 mixedworkspace_ids on the same profile.- Optional live check against Task 4
ensure_server+ Task 5 client on the2codenamespace 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.
- In-memory / fake opener: interrupt after the first profile bind, retry, second profile adopted, first mapping unchanged, no duplicate
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_workspacemust not leave stale sessionworkspace_ids (#416) - Update:
src-tauri/crates/infra/src/herdr/transport.rsorsrc-tauri/crates/service/src/runtime/herdr.rs— typedworktree.open; keepTerminalRuntimefail-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 5HerdrClient/outcome_uncertain, Task 1 lifecycle table indocs/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.openon 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_idin namespace2code, never display names.terminal_idis not persisted.worktree_pathremains a cache. - [ ] Missing stored workspaces are reopened and explicitly replaced. Tests prove no silent rebind by label or path. Session mapping
workspace_iddoes 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
TerminalRuntimestays 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 --workspacefromsrc-tauri(orcargo test --workspace --exclude codewhere GTK is unavailable). Frontend lint, typecheck, and tests apply only if frontend scope is introduced. IPC changes (none expected) would usebun run typegen; do not editsrc/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.movedin 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.