fix(host-service): workspace delete re-checks the disk after git unregisters the worktree

Merged
SA
2mo ago
+317-17

Fixes #6730

What was wrong

git worktree remove --force --force is not atomic: git can unregister the worktree from the repo's admin area and then fail partway through the recursive directory delete (locked file, permission issue, a live process still writing). Git exits non-zero, but git worktree list --porcelain no longer shows the worktree.

The delete saga leaned on exactly the wrong side of that split, three ways:

  1. gitWorktreeRemoveTask discarded the git error (.catch(() => {})) and derived its verdict solely from the registry read, whose comment declared it "authoritative". Unregistration is authoritative for "registered or not", but it says nothing about the files.
  2. The destroy saga took stillRegistered: false as "removed" and never looked at the disk, even though the two sibling branches in the same function (session delete, missing project repo) already do the existsSync plus guarded rm dance. The folder was orphaned in a state nothing can see: git does not list it, the row is archived, and clicking Delete again reports success without touching it.
  3. When removal did fail visibly (stillRegistered: true), the thrown message was just Failed to remove worktree at <path>, because the real git error was already gone. That bare toast was the entire diagnostic record for the incident in the issue.

As the issue triage points out, the startup reconciler (archived-workspace-reconcile) walks the same faulty path, so it would "resume" the same interrupted delete on every host-service start, report success, and never clear the orphan.

The fix

  • gitWorktreeRemoveTask keeps the git error and returns it as removeError alongside stillRegistered. The registry read still decides registration; the error is no longer thrown away.
  • After an unregistered verdict, the saga re-checks existsSync(worktreePath). A surviving folder inside the managed worktrees root is removed with the same isInsideProjectWorktreesRoot-guarded rm the other two branches use, so a corrupt path can never aim a recursive delete at user data. A folder outside the root is left on disk with an explicit warning instead of being silently forgotten.
  • Both failure throws (still registered, and the new unregistered but could not remove) now carry git's actual error, so the toast names the real cause (for the incident in the issue, the Operation not permitted on the locked file).

Retrying a delete that previously orphaned a folder now takes the fallback and actually clears it, which also un-wedges the reconciler loop.

Tests

Three new cases in workspace-cleanup.test.ts, next to the existing removal-ordering tests and against real temp directories:

  • git unregisters but the folder survives inside the managed root: destroy succeeds and the folder is gone (fails on main: destroy succeeds and the folder is orphaned),
  • the same shape outside the managed root: destroy succeeds, folder is left, and a warning says so,
  • stillRegistered: true with a captured git error: the thrown message contains that error.

The harness's mocked removeWorktree gained the optional removeError field, and the db select mock now answers .get() for getHostWorktreeBaseDir (reached when a spec sets no per-project base dir).

Full host-service suite: 1215 pass, 0 fail. Typecheck and Biome clean.

Deliberately out of scope

The issue also suggests gating worktree removal on terminal disposal (retry when PTY kills fail) and logging the warnings array server-side. Both are reasonable follow-ups; this PR is the disk-recheck and error-capture core that makes the delete correct and diagnosable.


Summary by cubic

Re-checks the worktree directory on disk after git worktree remove --force --force and preserves Git's error, so interrupted removals no longer orphan folders or report success without touching them.

  • gitWorktreeRemoveTask now returns { stillRegistered, removeError } with trimmed Git error text; callers propagate it.
  • After an unregistered verdict, the destroy path re-checks the path with a no-follow lstatSync check: a dangling symlink is treated as a leftover to remove, not an absence.
  • Leftovers inside the managed worktrees root are removed with a guarded rm; outside the root they're left on disk with a warning.
  • The root guard canonicalizes the parent of an unresolvable leaf, so a base behind a symlink (macOS /var → /private/var) still matches.
  • If the fallback rm fails, the error includes both the local remove error and the original Git error.
  • worktreeRemoved reflects the final disk state; outside-root leftovers set it to false.
  • Scope: touches packages/host-service delete path only; no DB/schema/config changes or migrations.

Summary by CodeRabbit

  • Bug Fixes

    • Improved workspace cleanup when Git worktrees are only partially removed.
    • Automatically removes leftover worktree directories within managed locations.
    • Prevents unsafe deletion outside approved worktree locations and reports clear cleanup errors.
    • Surfaces Git removal failures while continuing to verify worktree registration status.
    • Reports whether a worktree was fully removed based on its final directory state.
  • Tests

    • Added coverage for incomplete removals, safe cleanup, warnings, and error reporting.