Task 15: Delegate profile and project runtime cleanup
Summary
Delegate profile and project runtime cleanup to Herdr v0.9.0, as required by parent plan #394. Route workspace/worktree removal through JSON worktree.remove. Preserve dirty-file and unpushed-commit checks. Distinguish forgetting a project from deleting its worktrees. Remove local metadata only after confirmed cleanup or an explicit retained-worktree outcome. Do not switch the default runtime, pane.close Herdr terminals on project forget, git worktree remove a Herdr-owned checkout, or worktree.remove a default/primary checkout.
Pin remains Herdr v0.9.0, JSON protocol 22, endpoint generation 1. Verified remove: dirty checkout → dirty_worktree_requires_force (checkout and pane kept); worktree.remove --force deletes the checkout, drops workspace/pane records, terminates the pane process, and does not delete the git branch (docs/herdr-integration.md Workspace and worktree lifecycle). JSON worktree.* is outcome-uncertain (outcome_uncertain). Namespace stays Task 4 2code. Route by profile mapping (profile_runtime_mappings Bound in namespace 2code), not RuntimeRouter::selected_backend(). Session teardown stays Task 9 teardown_session / #403. The Herdr profile-delete path must release_herdr_session (no pane.close) so worktree.remove can drop panes itself.
Today delete_with_runtime always teardown_session then Local cleanup_profile (teardown_script → git worktree remove --force → git branch -D). delete_project drops catalog rows after the same session teardown. Neither path calls 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), #419 (Task 9, accepted on task-9-terminal-lifecycle), #421 (Task 10, accepted on task-10-frame-bridge), #423 (Task 11, accepted on task-11-xterm-transport), #425 (Task 12, accepted on task-12-reattach-restore), #427 (Task 13, accepted on task-13-agent-indicators), #428 (Task 14, accepted on task-14-profile-create). Checkout base: #428 (task-14-profile-create). Open contract-gap issues #396–#401, docs gap #407, Task 6 event-apply gap #413, implemented Task 8/9 gaps #403/#416, and line-count trackers #400/#405/#408/#410/#412/#415/#418/#420/#422/#424/#426 do not block Linux Task 15. #403 session teardown is already on Task 9; this task owns worktree/git teardown.
Work to complete
-
Herdr-mapped linked-worktree profile delete via
worktree.remove- Route by mapping, not selected backend. A Bound
2codeprofile mapping uses JSONworktree.removeeven whenRuntimeRouter::newis still Local. An unmapped profile keeps today’s Localcleanup_profile. A mapped profile with no Herdr client stays fail-closed and must not fall back togit worktree remove. - Never the user default session or
$XDG_CONFIG_HOME/herdr/herdr.sock. Do not callensure_herdr_listener/ adoption / sync from Local default startup insrc-tauri/src/lib.rs. - Refuse default/primary checkouts:
is_default, checkout path equal to the project folder, orworktree.listis_linked_worktree == false. Do notworktree.remove/git worktree removethose paths. - Order for a mapped linked worktree:
worktree.listbyworkspace_id(uncertain list fails closed) →release_herdr_sessionfor bound sessions (nopane.close, no Local PTY kill) →teardown_scriptonce while the checkout exists → JSONworktree.removewithworkspace_id,trust_repository: true, andforcefrom git dirty stats (or an explicit test override) →git branch -D→ delete the 2code profile row (mappings CASCADE). - Dirty without force:
dirty_worktree_requires_forcefails closed and keeps the profile row. Confirmed dirty delete passesforce: true. Herdr does not delete the git branch; 2code stillbranch -Dafter confirmed remove. workspace_not_found/worktree_not_foundis success (idempotent retry). Ifworktree.removereturns success but the checkout is still on disk, fail closed — do notgit worktree removea Herdr-owned path.- Do not
workspace.close,pane.close,tab.create,pane.split,worktree.create, orgit worktree addas part of delete.
- Route by mapping, not selected backend. A Bound
-
Project delete is forget/retain; Local unmapped delete stays git
- Project delete drops 2code catalog rows and mappings. Call
forget_project_session: Local-owned ids stillteardown_session; Herdr-owned ids only release/unbind. Do notworktree.remove,git worktree remove,workspace.close, orpane.closeHerdr terminals. Checkouts and Herdr workspaces stay. - Local-unmapped profile delete still
teardown_session+teardown_script+git worktree remove --force+git branch -D. Existingintegration_project.rsdelete tests keep passing. teardown_scriptstays 2code-owned and runs once. Do not run Herdr plugin teardown hooks orpane.send_inputas teardown.- Production
delete_profiledoes not grow an IPCforceflag; infer force from git dirty stats. Keepget_profile_delete_check/DeleteProfileDialogdirty/unpushed warnings. Do not rewriteDeleteProjectDialogcopy in this task. - Do not call
server.stop. GUI exit still only drops client helpers.
- Project delete drops 2code catalog rows and mappings. Call
-
Ownership: one backend per worktree
- Herdr-mapped delete must not call
infra::git::worktree_remove. - Local-unmapped delete must not call
worktree.remove. - Mapped-on-Local-selected-backend still uses Herdr
worktree.remove(mapping wins). - Do not attach observe and control together. Do not
--takeover.
- Herdr-mapped delete must not call
-
Uncertain / partial outcomes
worktree.removeis outcome-uncertain. OnHerdrUncertainOutcome, do not auto-replay. Reconcile withworktree.list/session.snapshot. If the workspace is gone and the checkout is gone, treat success and continue tobranch -D+ row delete. If it is still present, fail closed.- Do not assume live
worktree_removedfrom the Task 6 projection is sufficient (#413). - Keep Herdr wire envelopes (
method,params,result) inside the Herdr adapter / transport helpers. A typedworktree.removehelper onHerdrClientis required (workspace_id, not a display name; noterminal_id).
-
Prove with tests
- Fake client: Herdr-mapped non-default linked-worktree delete calls
worktree.removeonce withworkspace_id,trust_repository: true, and inferred/explicitforce; nogit worktree remove;teardown_scriptran once; profile row gone;git branch -Dran. - Dirty without force keeps the profile row and does not replay. Forced dirty remove then
branch -D. Uncertain remove: no auto-replay; absent workspace is success; still-present workspace is not removed again. - Default/primary /
is_linked_worktree == falserefused. Mapped-without-runtime fail-closed (no Local git fallback). Mapped delete on Local-selected backend still usesworktree.remove. - Project delete: no
worktree.remove/workspace.close/pane.close; mappings unbound; Local PTYs torn down; Herdr checkouts retained. - Local-unmapped delete still uses git worktree remove + branch -D.
RuntimeRouter::newstill selects Local. - Optional live check against Task 4
ensure_server+ Task 5 client on the2codenamespace with a disposable linked worktree: dirty refuse, forced remove, checkout gone, branch kept until 2codebranch -D. 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. bun run lint:check,bun run typecheck,bun run testif frontend/IPC scope is introduced. Rust:cargo test --workspacefromsrc-tauri(or--exclude codewhere GTK is unavailable). IPC changes usebun run typegen; do not editsrc/generated/by hand.
- Fake client: Herdr-mapped non-default linked-worktree delete calls
Likely files and directories
- Update:
src-tauri/crates/infra/src/herdr/transport.rs— typedworktree.remove; keepserver.stoprefused; do not auto-replay - Update:
src-tauri/crates/service/src/runtime/herdr.rs—HerdrWorktreeClient::worktree_remove - Update:
src-tauri/crates/service/src/runtime.rs—release_herdr_session,forget_project_session; Local default unchanged - Update:
src-tauri/crates/service/src/profile.rs— mapping-based Herdr delete; keep Localcleanup_profile; colocated tests - Update:
src-tauri/crates/service/src/project.rs— forget/retain catalog delete - Reuse, do not duplicate: Task 9
teardown_session(#403), Task 7find_profile_mapping,DeleteProfileDialog.tsxdirty/unpushed check - Do not rewrite: Local PTY, Task 10 CLI attach, Task 11/12/13 terminal/agent paths,
DeleteProjectDialog.tsxcopy,docs/herdr-integration.md,docs/data-flow.md
Estimated changes: 300–500 lines total, additions plus deletions, including tests; excluding generated files, lockfile churn and binary artifacts. Material scope growth should become a separate issue.
Acceptance criteria
- [ ] Herdr-mapped non-default linked-worktree profile delete uses JSON
worktree.removeon namespace2code, keyed byworkspace_id. Local-unmapped delete still usesgit worktree remove+git branch -D. - [ ] Dirty/unpushed checks stay. Dirty Herdr remove without force fails closed and keeps the profile row. Confirmed dirty delete passes force. Herdr does not delete the git branch, so 2code still
branch -Dafter confirmed remove. - [ ] Project delete is forget/retain: drop 2code catalog and mappings; do not
worktree.remove,git worktree remove,workspace.close, orpane.closeHerdr terminals. - [ ] Default/primary checkouts are never
worktree.remove.teardown_scriptstays 2code-owned once. Uncertain remove is reconciled without auto-replay. No 2code session or worktree is owned by both backends. - [ ] Default runtime remains Local. Local delete path still works. Adapter choice for cleanup uses the profile mapping, not
selected_backend(). Cleanup is not started from Local default startup. - [ ] Production delete does not call
git worktree removeon the Herdr-mapped path,workspace.closeas forget,pane.closeon project forget,pane.send_input,server.stop,--takeover, or a reimplementedherdr-client.sockprotocol. - [ ] Windows named-pipe code may compile; live Windows is not claimed (#399). macOS live Unix is not claimed (#401). Session teardown remains #403 on Task 9.
- [ ]
cargo test --workspacefromsrc-tauri(orcargo test --workspace --exclude codewhere GTK is unavailable). Frontend lint, typecheck, and tests apply if frontend/IPC scope is introduced.bun run typegenfor IPC; do not editsrc/generated/by hand.
Out of scope
- Git/editor path reconciliation (Task 16).
- Startup-command / shell / env mapping (Task 17 / #397).
- Runtime health UI (Task 18), legacy-session migration (Task 19), acceptance cutover (Task 20), or making Herdr the default (Task 21).
- Deleting
detector/(Task 22) or the local PTY runtime (Task 23). - Rewriting
DeleteProjectDialog.tsx/ i18n copy to say forget/retain, or updatingdocs/data-flow.md(later UX/docs). - Closing or implementing contract-gap issues #396–#401, docs gap #407, Task 6 event-apply gap #413, implemented gaps #403/#416, or line-count trackers #400/#405/#408/#410/#412/#415/#418/#420/#422/#424/#426.
- Applying
pane.exited/layout.updated/tab.movedin the projection (#413). - Updating
docs/herdr-integration.md. - Adding an IPC
forceflag or changingDeleteProfileDialogdirty/unpushed UX beyond keeping it working. - Remote Herdr operation.
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.
- Never
worktree.removea default/primary checkout. - Do not call
server.stop,--takeover,pane.send_input, or a reimplementedherdr-client.sockprotocol.