Skip to content

fix(server): recreate a thread's worktree before starting a turn - #7839

Open
mackinleysmith wants to merge 2 commits into
pingdotgg:mainfrom
mackinleysmith:t3code/recreate-worktree-before-turn
Open

fix(server): recreate a thread's worktree before starting a turn#7839
mackinleysmith wants to merge 2 commits into
pingdotgg:mainfrom
mackinleysmith:t3code/recreate-worktree-before-turn

Conversation

@mackinleysmith

@mackinleysmith mackinleysmith commented Aug 21, 2026

Copy link
Copy Markdown

Problem

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/server typecheck clean.
  • ProviderCommandReactor.test.ts, 46/46 passing, including a new test that a missing worktree path triggers prune and createWorktree with the thread's branch before startSession.

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.

Reviewed by Cursor Bugbot for commit 130bcc4. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Recreate missing thread worktree before starting a turn

  • On thread.turn.start, the reactor now checks if the thread's worktreePath exists; if missing, it resolves the project cwd, prunes stale worktree admin entries, and recreates the worktree from the thread branch before proceeding.
  • Adds pruneWorktrees to GitWorkflowService and GitVcsDriver interfaces; the core driver runs git worktree prune with 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: ensureThreadWorktree in ProviderCommandReactor.ts silently swallows non-interrupt errors — a persistently broken worktree will produce warnings but not abort the turn.

Macroscope summarized 130bcc4.

@coderabbitai

coderabbitai Bot commented Aug 21, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 50d0f849-3594-4cb1-bc7f-386d12fa0d61

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added vouch:trusted PR author is trusted by repo permissions or the VOUCHED list. size:M 30-99 changed lines (additions + deletions). labels Aug 21, 2026
@macroscopeapp

macroscopeapp Bot commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at 130bcc4

Macroscope'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.

You can add or adjust custom eligibility rules. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M 30-99 changed lines (additions + deletions). vouch:trusted PR author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant