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

Task 15: Delegate profile and project runtime cleanup

Open AkaraChen opened this issue 2 months ago • 0 comments

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

  1. Herdr-mapped linked-worktree profile delete via worktree.remove

    • Route by mapping, not selected backend. A Bound 2code profile mapping uses JSON worktree.remove even when RuntimeRouter::new is still Local. An unmapped profile keeps today’s Local cleanup_profile. A mapped profile with no Herdr client stays fail-closed and must not fall back to git worktree remove.
    • Never the user default session or $XDG_CONFIG_HOME/herdr/herdr.sock. Do not call ensure_herdr_listener / adoption / sync from Local default startup in src-tauri/src/lib.rs.
    • Refuse default/primary checkouts: is_default, checkout path equal to the project folder, or worktree.list is_linked_worktree == false. Do not worktree.remove / git worktree remove those paths.
    • Order for a mapped linked worktree: worktree.list by workspace_id (uncertain list fails closed) → release_herdr_session for bound sessions (no pane.close, no Local PTY kill) → teardown_script once while the checkout exists → JSON worktree.remove with workspace_id, trust_repository: true, and force from git dirty stats (or an explicit test override) → git branch -D → delete the 2code profile row (mappings CASCADE).
    • Dirty without force: dirty_worktree_requires_force fails closed and keeps the profile row. Confirmed dirty delete passes force: true. Herdr does not delete the git branch; 2code still branch -D after confirmed remove.
    • workspace_not_found / worktree_not_found is success (idempotent retry). If worktree.remove returns success but the checkout is still on disk, fail closed — do not git worktree remove a Herdr-owned path.
    • Do not workspace.close, pane.close, tab.create, pane.split, worktree.create, or git worktree add as part of delete.
  2. 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 still teardown_session; Herdr-owned ids only release/unbind. Do not worktree.remove, git worktree remove, workspace.close, or pane.close Herdr terminals. Checkouts and Herdr workspaces stay.
    • Local-unmapped profile delete still teardown_session + teardown_script + git worktree remove --force + git branch -D. Existing integration_project.rs delete tests keep passing.
    • teardown_script stays 2code-owned and runs once. Do not run Herdr plugin teardown hooks or pane.send_input as teardown.
    • Production delete_profile does not grow an IPC force flag; infer force from git dirty stats. Keep get_profile_delete_check / DeleteProfileDialog dirty/unpushed warnings. Do not rewrite DeleteProjectDialog copy in this task.
    • Do not call server.stop. GUI exit still only drops client helpers.
  3. 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.
  4. Uncertain / partial outcomes

    • worktree.remove is outcome-uncertain. On HerdrUncertainOutcome, do not auto-replay. Reconcile with worktree.list / session.snapshot. If the workspace is gone and the checkout is gone, treat success and continue to branch -D + row delete. If it is still present, fail closed.
    • Do not assume live worktree_removed from the Task 6 projection is sufficient (#413).
    • Keep Herdr wire envelopes (method, params, result) inside the Herdr adapter / transport helpers. A typed worktree.remove helper on HerdrClient is required (workspace_id, not a display name; no terminal_id).
  5. Prove with tests

    • Fake client: Herdr-mapped non-default linked-worktree delete calls worktree.remove once with workspace_id, trust_repository: true, and inferred/explicit force; no git worktree remove; teardown_script ran once; profile row gone; git branch -D ran.
    • 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 == false refused. Mapped-without-runtime fail-closed (no Local git fallback). Mapped delete on Local-selected backend still uses worktree.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::new still selects Local.
    • Optional live check against Task 4 ensure_server + Task 5 client on the 2code namespace with a disposable linked worktree: dirty refuse, forced remove, checkout gone, branch kept until 2code branch -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 test if frontend/IPC scope is introduced. Rust: cargo test --workspace from src-tauri (or --exclude code where GTK is unavailable). IPC changes use bun run typegen; do not edit src/generated/ by hand.

Likely files and directories

  • Update: src-tauri/crates/infra/src/herdr/transport.rs — typed worktree.remove; keep server.stop refused; 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 Local cleanup_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 7 find_profile_mapping, DeleteProfileDialog.tsx dirty/unpushed check
  • Do not rewrite: Local PTY, Task 10 CLI attach, Task 11/12/13 terminal/agent paths, DeleteProjectDialog.tsx copy, 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.remove on namespace 2code, keyed by workspace_id. Local-unmapped delete still uses git 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 -D after confirmed remove.
  • [ ] Project delete is forget/retain: drop 2code catalog and mappings; do not worktree.remove, git worktree remove, workspace.close, or pane.close Herdr terminals.
  • [ ] Default/primary checkouts are never worktree.remove. teardown_script stays 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 remove on the Herdr-mapped path, workspace.close as forget, pane.close on project forget, pane.send_input, server.stop, --takeover, or a reimplemented herdr-client.sock protocol.
  • [ ] 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 --workspace from src-tauri (or cargo test --workspace --exclude code where GTK is unavailable). Frontend lint, typecheck, and tests apply if frontend/IPC scope is introduced. bun run typegen for IPC; do not edit src/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 updating docs/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.moved in the projection (#413).
  • Updating docs/herdr-integration.md.
  • Adding an IPC force flag or changing DeleteProfileDialog dirty/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.remove a default/primary checkout.
  • Do not call server.stop, --takeover, pane.send_input, or a reimplemented herdr-client.sock protocol.

AkaraChen avatar Sep 15 '26 19:09 AkaraChen