fix(server): recreate a thread's worktree before starting a turn #7839
main fromrefs/pull/7839/head·merged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubProblem#
When a thread's worktree directory disappears, every later turn on that thread fails. The provider session is tied to the persisted cwd, and provider CLIs key their transcript store off that path, so the resume fails and the user sees a generic "session not found". The conversation isn't actually lost. Our projection still has every message and the transcript is still on disk. The directory the session is addressed by is gone, and nothing tries to bring it back.
This is the narrow version of the fix from #6197, which also changed session resume guards and Claude error classification. This PR does one thing.
Fix#
Before ProviderCommandReactor resolves a session for a turn, it checks whether the thread's worktreePath still exists. If it doesn't and the thread has a branch, it runs git worktree prune (a directory deleted by hand leaves an admin entry that makes git worktree add refuse the path) and then recreates the worktree from the thread's branch in the project's workspaceRoot.
Failures are logged and the turn continues, so the existing failure path reports the real error. Interrupts are re-raised. A failed stat counts as "exists" so a transient filesystem error can't trigger a recreate over a healthy worktree.
Out of scope here: a thread activity telling the user that uncommitted work in the old worktree is gone, and recovery when the branch itself was deleted. Those can follow if wanted.
Verification#
apps/servertypecheck clean.ProviderCommandReactor.test.ts, 46/46 passing, including a new test that a missing worktree path triggers prune andcreateWorktreewith the thread's branch beforestartSession.
Written with Claude Fable 5 in T3 Code.
Note
Medium Risk Touches turn-start orchestration and git worktree lifecycle. Recreate failures are logged and the turn still proceeds, so a missing path can still fail later rather than blocking.
Overview
On thread.turn.start, ProviderCommandReactor now checks that the thread's worktreePath still exists. If it is gone and the thread has a branch, it prunes stale git worktree admin entries and recreates the worktree at the recorded path from that branch in the project root.
Adds pruneWorktrees (git worktree prune) on the git driver and GitWorkflowService. A failed existence check is treated as present so a transient FS error cannot overwrite a healthy worktree. Non-interrupt recreate failures are logged and the turn continues so the real provider error still surfaces.
Note
Recreate missing thread worktree before starting a turn#
- On
thread.turn.start, the reactor now checks if the thread'sworktreePathexists; if missing, it resolves the projectcwd, prunes stale worktree admin entries, and recreates the worktree from the thread branch before proceeding. - Adds
pruneWorktreestoGitWorkflowServiceandGitVcsDriverinterfaces; the core driver runsgit worktree prunewith a 15s timeout and invalidates list-refs caches. - Failures in worktree recreation are logged and suppressed so the turn continues, unless the error is an interrupt.
- Risk:
ensureThreadWorktreein ProviderCommandReactor.ts silently swallows non-interrupt errors — a persistently broken worktree will produce warnings but not abort the turn.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
Approvability#
Verdict: Approved at
130bcc4Macroscope's review found this PR approvable — This is a self-contained bug fix that defensively recreates missing worktree directories before starting a turn. The implementation includes proper error handling (failures are logged but don't block), follows existing patterns, and has comprehensive test coverage.
Notes:
- No code objects were reviewed. Approvability was decided on eligibility alone.
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
50d0f849-3594-4cb1-bc7f-386d12fa0d61You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share