Task 9: Implement Herdr terminal lifecycle commands
Summary
Create, enumerate, and explicitly close terminals through Herdr v0.9.0, as required by parent plan #394. Adapt terminal creation and listing to runtime identities. Reuse any root pane returned during workspace/tab creation. Separate releasing a GUI attachment from terminating a terminal. Reconcile ambiguous creation/close outcomes before retrying. Do not switch the default runtime, attach live frames, restore by respawning shells, or replace Local PTY ownership.
Pin remains Herdr v0.9.0, JSON protocol 22, endpoint generation 1. Mapping: docs/herdr-integration.md External split mapping. A 2code project remains a repository entry. A profile already maps to one Herdr workspace_id via Task 8. A 2code terminal tab maps to a Herdr pane_id. terminal_id is live PTY identity only and must not be persisted. Namespace: Task 4 SESSION_NAME (2code) in src-tauri/crates/infra/src/herdr/process.rs. Persist session→pane with Task 7 bind_session_pane. Classify stored ids with Task 7 pane_identity_state against a Task 6 RuntimeProjection. tab.create / pane.close are outcome-uncertain (outcome_uncertain). Closing a 2code tab maps to JSON pane.close. Closing the last pane removes the Herdr workspace record; it must not git worktree remove or worktree.remove.
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), #417 (Task 8, accepted on task-8-profile-adoption). Checkout base: #417 (task-8-profile-adoption). Open contract-gap issues #396–#401, docs gap #407, Task 6 event-apply gap #413, and line-count trackers #400/#405/#408/#410/#412/#415/#418 do not block Linux Task 9. #403 does not block starting this task, but this task must route profile/project session teardown through RuntimeRouter so a Herdr-owned identity is not killed as Local. Live Windows named-pipe remains #399. Live macOS Unix sockets remain #401. Windows live attach remains #396.
Work to complete
-
Herdr create / list / close on runtime identities
- Implement JSON
tab.createandpane.close(pluspane.get/pane.list/session.snapshotfor reconcile) on the Task 42codesocket. Never the user default session or$XDG_CONFIG_HOME/herdr/herdr.sock. - Replace fail-closed
create_session/list_project_sessions/close_session/delete_sessionon the Herdr adapter when a client is configured. Keepwrite/resize/history/restore_session/flush/clearfail-closed (Task 10/12). RuntimeRouter::newstays Local-default. A Herdr adapter with no client/endpoint stays fail-closed and must still not spawn a Local PTY. Do not callensure_herdr_listener/ adoption / sync from Local default startup insrc-tauri/src/lib.rs.- Look up the profile’s bound
workspace_idfromPtySessionMeta.profile_id. If there is no mapping, orworkspace_identity_stateis Missing, fail closed. Do notworkspace.create,worktree.create, orworktree.opento invent a workspace. - First terminal for a profile with no
session_runtime_mappings: reuse one unbound projected pane in that workspace (the adoption root from Task 8worktree.open, typically the workspace root pane). Do nottab.createa second root. Do not bind an extra external split pane as “create”. - Later terminals:
tab.createwith thatworkspace_id, then bind only the returnedroot_pane.pane_id. Do notpane.split. Flattened listing still treats every pane as a 2code tab later; this task does not reconstruct BSP and does not auto-create 2code rows for external splits (Task 12). - Insert a 2code
pty_sessionsmetadata row andbind_session_panewithworkspace_id+pane_idin namespace2code. Persist immediately. Do not persistterminal_idortab_idas the association key. Do not spawn a Local PTY, writepty_log, or mutate git worktrees. list_project_sessionson the Herdr adapter returns 2code session records that have session→pane mappings for profiles in that project. Local default listing stays Local-only. Do not merge Local and Herdr sessions into one list.
- Implement JSON
-
Terminate vs release, and uncertain outcomes
close_pty_session/TerminalRuntime::close_sessionon Herdr ispane.close(terminate the pane process). After confirmed close, unbindsession_runtime_mappings, unbindRuntimeSelectorownership, and mark the 2code session closed. Do not treat this as GUI detach.- Do not call
herdr terminal session/terminal.release/herdr-client.sock. Attachment release belongs to Task 10. A passing close test that only drops a client helper is a failure. tab.createandpane.closeare outcome-uncertain. OnHerdrUncertainOutcome, do not auto-replay. Reconcile withsession.snapshotorpane.get/pane.list: bind the observedpane_idif create committed; treatpane_not_foundas successful close. Then persist. Do not retry a secondtab.create/pane.closeuntil that reconcile.- If bind fails after
tab.create,pane.closethe new pane. If bind fails after reusing an adopted root pane, do notpane.closethat pane. - Last pane
pane.closeremoves the Herdr workspace (pane_not_foundafterward). Unbind the profile’s workspace mapping so it is not still Bound against a vanished id. Do notworktree.opento recreate,workspace.closeas a separate path,worktree.remove, orgit worktree remove. - Do not assume live
pane.exited/layout.updated/tab.movedfrom the Task 6 projection (#413). Mutation reconcile is snapshot/pane.get/pane.listplus already-appliedpane_closed/tab_created.
-
Ownership: one backend per terminal
- Bind the 2code session id to
RuntimeBackend::Herdronly after a successful create/reuse. Refuse to bind an id already owned by Local. - Herdr create must not insert into the Local session map. Local create must not write
session_runtime_mappingsor calltab.create/pane.close. - Keep Herdr wire envelopes (
method,params,result) inside the Herdr adapter / transport helpers, notmodelor handler signatures. Typedtab_create/pane_close/pane_gethelpers onHerdrClientare fine. - Do not call
server.stop. GUI exit still only drops client helpers.
- Bind the 2code session id to
-
Profile/project session teardown (#403)
- Route
delete_profile/delete_projectsession teardown throughRuntimeRouter::close_session(then unbind), notpty::close_session_fullalone. - Fail closed if teardown would kill a Local PTY for an identity the selector treats as Herdr-owned, or would
pane.closea Local-owned id. - Do not change worktree/git teardown, dirty checks, or
worktree.remove(Task 15). Session close only.
- Route
-
Prove with tests
- Fake client: reuse unbound adopted root pane on first create; second create calls
tab.createonce;already-style duplicate create is not a second pane. - Uncertain
tab.create: no auto-replay; reconcile binds onepane_id. Uncertainpane.close:pane_not_foundis success; a still-present pane is not blindly closed again. - Last-pane close: workspace gone; profile workspace mapping not Bound; git checkout still on disk.
- Bind-failure after
tab.createcloses the new pane; bind-failure after root reuse does not. #403: deleting a profile/project with a router-created Local session unbinds that identity; a Herdr-owned id is not closed via Localclose_session_full.- Existing Local PTY, Task 2 routing, Task 6 projection, Task 7 mapping, and Task 8 adoption tests keep passing.
RuntimeRouter::newstill selects Local. Herdr-selected tests with no client still do not spawn Local PTYs. - Optional live check against Task 4
ensure_server+ Task 5 client on the2codenamespace with a disposable adopted checkout: create, list, close, last-pane workspace removal. Skip without sidecar unless an explicit required flag is set. Never target the user default session. Live fixtures must not delete the user’s real checkouts.
- Fake client: reuse unbound adopted root pane on first create; second create calls
Likely files and directories
- Update:
src-tauri/crates/service/src/runtime/herdr.rs— create/list/close/delete when a client is configured; keep write/resize/restore fail-closed - Update:
src-tauri/crates/service/src/runtime.rs— unbind after confirmed close; keep Local default; colocated routing tests - Update:
src-tauri/crates/infra/src/herdr/transport.rs— typedtab.create/pane.close/pane.get/pane.list; keepserver.stoprefused - Update:
src-tauri/crates/service/src/profile.rs,src-tauri/crates/service/src/project.rs,src-tauri/src/handler/profile.rs,src-tauri/src/handler/project.rs,src-tauri/src/bridge.rs— session teardown viaRuntimeRouter(#403) - Update if needed:
src-tauri/src/handler/pty.rs— still thin; no IPC shape change expected;src-tauri/crates/model/src/error.rs— structured missing-workspace / uncertain-lifecycle errors only - Reuse, do not duplicate: Task 8 adoption, Task 7 mapping uniqueness, Task 6 projection ids, Task 5
HerdrClient/outcome_uncertain, Task 4SESSION_NAME, Task 1 lifecycle table indocs/herdr-integration.md - Do not rewrite: Local PTY spawn/read/log, Task 6 subscribe/apply, Task 8
worktree.openadoption, frontend terminal code,docs/herdr-integration.md
Estimated changes: 250–450 lines total, additions plus deletions, including tests; excluding generated files and lockfile churn. Material scope growth should become a separate issue.
Acceptance criteria
- [ ] Herdr-backed create/list/close use
pane_idin namespace2code. First terminal reuses an unbound adopted root pane; additional terminals usetab.createand itsroot_pane.terminal_idis not persisted. - [ ]
close_sessionterminates viapane.close. It is not GUI attachment release. Last-pane close unbinds the vanished workspace mapping and does not delete the git checkout. - [ ] Uncertain create/close outcomes are reconciled before retry; tests prove no auto-replay and no duplicate panes.
- [ ] Default runtime remains Local. Herdr write/resize/history/restore stay fail-closed. Local PTY/agent path still works. No 2code session or worktree is owned by both backends. Lifecycle is not started from Local default startup.
- [ ] Profile/project session teardown goes through
RuntimeRouterand cannot kill Local for a Herdr-owned id (#403). - [ ] Production lifecycle does not call
workspace.create,worktree.create/open/remove,pane.split,server.stop, or CLI terminal attach. - [ ] Windows named-pipe code may compile; live Windows is not claimed (#399). macOS live Unix is not claimed (#401). Live attach is not this task (#396).
- [ ]
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
- Live frame bridge / CLI
terminal sessionattach (Task 10), xterm transport (Task 11), reattachment UI and history replay removal (Task 12), agent notifications (Task 13). - Creating new profiles through Herdr
worktree.create(Task 14) or routing worktree/project cleanup 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, docs gap #407, Task 6 event-apply gap #413, or line-count trackers #400/#405/#408/#410/#412/#415/#418 except the #403 session-teardown integrity this task needs.
- 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.
- Startup commands on create (#397).
Standing constraints
- No default-runtime switch.
- Existing Local PTY/agent path stays working until the Herdr path passes acceptance.
- Never let both runtimes own the same terminal or worktree.