fix(host-service): workspace delete no longer times out on node_modules heavy worktrees

SA
1mo ago
+256-12

Fixes #6887

What was broken

Deleting a workspace whose worktree carries a big dependency tree failed with:

Failed to verify worktree removal at ...: [host-worker] Task "git/removeWorktree" timed out after 120000ms

Not a hang. As the issue measured, git worktree remove does its recursive delete with a single threaded walk, and a 161k entry worktree (87 percent of it node_modules) simply does not fit in 120 seconds on a loaded machine with a cold metadata cache. The workspace un-archives correctly after the failure, but retrying hits the same wall, so the workspace becomes undeletable.

The fix

Invert who deletes, as the issue suggests. gitWorktreeRemoveTask now:

  1. Deletes the directory itself with removeDirectoryTree, the same helper the destroy saga's other branches already use. fs.rm fans unlinks across the threadpool instead of git's serial walk, and the helper keeps its EACCES recovery pass for read-only directories.
  2. Then runs git worktree remove --force --force purely to unregister. The task's own comment already establishes that this handles an absent directory, and with the tree gone it returns near instantly.
  3. Verifies with git worktree list --porcelain exactly as before.

Two things the issue's triage insisted on, both done:

  • The root guard is plumbed into the task. The payload now carries projectId and worktreeBaseDir, and the in-task delete only runs when the path sits inside <base>/<projectId>/, the same isInsideProjectWorktreesRoot check the saga's own rm branches apply. A corrupt stored path can never aim a recursive delete at user data. Outside the managed root, the delete stays git's, exactly today's behavior.
  • The budget is not left at a value the old design set. Raised to 600s: even a fast delete can outlast a small fixed cap on a swap-thrashing, nearly full disk, and a timeout mid delete recreates the partial states the #6730 fix exists to catch. Since the archive-first design already removed the workspace from every list at the start, a longer-running background delete costs the user nothing.

The result also reports removedByApp so tests and telemetry can tell which side did the delete.

What I did not do

The issue triage floats replacing the fixed budget with a progress heartbeat that resets the timer. That touches WorkerTaskRunner for every task and deserves its own change; the inversion plus the larger budget removes the failure the issue reports without it. Also worth knowing: the heavy fs.rm shares the process-wide libuv threadpool (4 threads), so a huge delete keeps that pool busy for its duration. That is the bound on its concurrency, and the work happens off the event loop either way.

Tests

New git-worktree-remove.test.ts runs the real handler against real repos and worktrees: inside the managed root the app deletes the tree (removedByApp: true, directory gone, unregistered); outside the root the app stays out and git removes it as before; a worktree whose directory is already gone still unregisters. The two guard tests fail on main since the field and the behavior do not exist there. The pool timeout test's fixture now passes the new guard fields, placed outside the managed root on purpose so the asserted timeout phase stays on the git step.

Full host-service suite: 1998 pass. The 2 failures (git-status-partial adversarial timing tests) fail identically on a clean checkout of main on this machine, before this change; they hit their 5s per-test cap under load and are unrelated to this path.


Summary by cubic

Fixes #6887 by handing forced worktree removal from Git's recursive delete to an app-side removeDirectoryTree followed by Git unregistration. This avoids the 120-second timeout for node_modules-heavy worktrees while keeping Git deletion outside the managed root and raising the task budget to 600 seconds.

  • App-side deletion runs only for forced removals inside <worktreeBaseDir>/<projectId>/, gated on the resolved path being a registered worktree of the repo; anything else stays Git's, so its refusal behavior is intact.
  • A stored path that is itself a symlink is refused outright: the task reports stillRegistered: true with a reason so the caller blocks the delete instead of letting Git remove whatever the link resolves to.
  • Removal results now include removedByApp; added real-repository tests for managed, unmanaged, already-removed, symlinked, and unregistered worktrees.

Summary by CodeRabbit

  • Bug Fixes
    • Forced cleanup now removes the contents of worktrees inside the project-managed location before asking Git to unregister them. Worktrees outside that location continue to be removed by Git.
    • Cleanup does not follow symlinked worktree paths and leaves folders not registered with Git untouched.
    • Git can unregister worktrees whose directories are already missing.
    • Increased the cleanup timeout, and cleanup continues to ask Git to unregister a worktree if app-side removal fails.