Skip to content

Fix worktree terminal startup race#879

Open
maarao wants to merge 1 commit intopingdotgg:mainfrom
maarao:fix/draft-worktree-terminal-startup-race
Open

Fix worktree terminal startup race#879
maarao wants to merge 1 commit intopingdotgg:mainfrom
maarao:fix/draft-worktree-terminal-startup-race

Conversation

@maarao
Copy link

@maarao maarao commented Mar 11, 2026

What Changed

This fixes a race in the first-message worktree setup flow that could prevent actions configured to run automatically on worktree creation from running.

When a new worktree is created from a local draft thread, ChatView now updates the draft thread's branch/worktree context before running the setup script. This keeps the terminal drawer's mount-time terminal.open call aligned with the new worktree instead of briefly reopening the PTY against the project root.

I also added a browser regression test covering this path.

Why

On the first message in worktree mode, the setup script terminal was opened with the new worktree path, but the terminal drawer could immediately remount and call terminal.open again using stale local draft-thread state.

Because terminal.open restarts the PTY when cwd or env changes, that second open could wipe out the injected setup command. The result was that the terminal opened, but the automatic worktree setup action did not run until it was triggered manually.

This change fixes that by keeping the local draft-thread state in sync as soon as the worktree is created.

UI Changes

No visual UI changes.

This is a terminal startup timing/interaction fix.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes
  • I included a video for animation/interaction changes

Note

Fix draft worktree terminal startup race by updating draft thread context on worktree creation

  • When a worktree is created from a local draft thread, ChatView now immediately calls setDraftThreadContext with the new branch, worktree path, and envMode: 'worktree', so the terminal drawer targets the new worktree instead of the project root.
  • Adds a new browser integration test that verifies at least two terminalOpen requests are made with the correct worktree cwd and env after sending a message in a draft thread.
  • Extends the WebSocket mock in ChatView.browser.tsx to support a per-fixture rpcResolver hook and to return {history: ""} for terminalOpen/terminalRestart calls; the resolver now receives the full request body instead of just the method tag.

Macroscope summarized 3c388e9.

@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2026

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

Run ID: b6a5db3c-25f0-4fe9-b61a-7b1f61bf1446

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
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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 and usage tips.

@github-actions github-actions bot added the vouch:unvouched PR author is not yet trusted in the VOUCHED list. label Mar 11, 2026
@maarao maarao changed the title Fix draft worktree terminal startup race Fix worktree terminal startup race Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant