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

Task 9: Implement Herdr terminal lifecycle commands

Open AkaraChen opened this issue 2 months ago • 0 comments

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

  1. Herdr create / list / close on runtime identities

    • Implement JSON tab.create and pane.close (plus pane.get / pane.list / session.snapshot for reconcile) on the Task 4 2code socket. Never the user default session or $XDG_CONFIG_HOME/herdr/herdr.sock.
    • Replace fail-closed create_session / list_project_sessions / close_session / delete_session on the Herdr adapter when a client is configured. Keep write / resize / history / restore_session / flush / clear fail-closed (Task 10/12).
    • RuntimeRouter::new stays Local-default. A Herdr adapter with no client/endpoint stays fail-closed and must still not spawn a Local PTY. Do not call ensure_herdr_listener / adoption / sync from Local default startup in src-tauri/src/lib.rs.
    • Look up the profile’s bound workspace_id from PtySessionMeta.profile_id. If there is no mapping, or workspace_identity_state is Missing, fail closed. Do not workspace.create, worktree.create, or worktree.open to 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 8 worktree.open, typically the workspace root pane). Do not tab.create a second root. Do not bind an extra external split pane as “create”.
    • Later terminals: tab.create with that workspace_id, then bind only the returned root_pane.pane_id. Do not pane.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_sessions metadata row and bind_session_pane with workspace_id + pane_id in namespace 2code. Persist immediately. Do not persist terminal_id or tab_id as the association key. Do not spawn a Local PTY, write pty_log, or mutate git worktrees.
    • list_project_sessions on 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.
  2. Terminate vs release, and uncertain outcomes

    • close_pty_session / TerminalRuntime::close_session on Herdr is pane.close (terminate the pane process). After confirmed close, unbind session_runtime_mappings, unbind RuntimeSelector ownership, 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.create and pane.close are outcome-uncertain. On HerdrUncertainOutcome, do not auto-replay. Reconcile with session.snapshot or pane.get / pane.list: bind the observed pane_id if create committed; treat pane_not_found as successful close. Then persist. Do not retry a second tab.create/pane.close until that reconcile.
    • If bind fails after tab.create, pane.close the new pane. If bind fails after reusing an adopted root pane, do not pane.close that pane.
    • Last pane pane.close removes the Herdr workspace (pane_not_found afterward). Unbind the profile’s workspace mapping so it is not still Bound against a vanished id. Do not worktree.open to recreate, workspace.close as a separate path, worktree.remove, or git worktree remove.
    • Do not assume live pane.exited / layout.updated / tab.moved from the Task 6 projection (#413). Mutation reconcile is snapshot/pane.get/pane.list plus already-applied pane_closed / tab_created.
  3. Ownership: one backend per terminal

    • Bind the 2code session id to RuntimeBackend::Herdr only 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_mappings or call tab.create/pane.close.
    • Keep Herdr wire envelopes (method, params, result) inside the Herdr adapter / transport helpers, not model or handler signatures. Typed tab_create / pane_close / pane_get helpers on HerdrClient are fine.
    • Do not call server.stop. GUI exit still only drops client helpers.
  4. Profile/project session teardown (#403)

    • Route delete_profile / delete_project session teardown through RuntimeRouter::close_session (then unbind), not pty::close_session_full alone.
    • Fail closed if teardown would kill a Local PTY for an identity the selector treats as Herdr-owned, or would pane.close a Local-owned id.
    • Do not change worktree/git teardown, dirty checks, or worktree.remove (Task 15). Session close only.
  5. Prove with tests

    • Fake client: reuse unbound adopted root pane on first create; second create calls tab.create once; already-style duplicate create is not a second pane.
    • Uncertain tab.create: no auto-replay; reconcile binds one pane_id. Uncertain pane.close: pane_not_found is 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.create closes 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 Local close_session_full.
    • Existing Local PTY, Task 2 routing, Task 6 projection, Task 7 mapping, and Task 8 adoption tests keep passing. RuntimeRouter::new still 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 the 2code namespace 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.

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 — typed tab.create / pane.close / pane.get / pane.list; keep server.stop refused
  • 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 via RuntimeRouter (#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 4 SESSION_NAME, Task 1 lifecycle table in docs/herdr-integration.md
  • Do not rewrite: Local PTY spawn/read/log, Task 6 subscribe/apply, Task 8 worktree.open adoption, 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_id in namespace 2code. First terminal reuses an unbound adopted root pane; additional terminals use tab.create and its root_pane. terminal_id is not persisted.
  • [ ] close_session terminates via pane.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 RuntimeRouter and 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 --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

  • Live frame bridge / CLI terminal session attach (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.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.
  • 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.

AkaraChen avatar Sep 15 '26 10:09 AkaraChen