Add worktree handoff and status tools to the t3-code MCP server#3754
Add worktree handoff and status tools to the t3-code MCP server#3754nsxdavid wants to merge 5 commits into
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
One convention issue: WorktreeOperationError copies the underlying cause.message into a detail field and builds its message from it, instead of preserving the real failure as cause and deriving the message from structural attributes. The sibling previewAutomation.ts errors already follow the expected pattern (cause: Schema.Defect() plus bounded diagnostics). See inline comment.
Posted via Macroscope — Effect Service Conventions
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a7bf0f58f3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
ApprovabilityVerdict: Needs human review 1 blocking correctness issue found. This PR introduces new MCP tools for worktree management with ~1500 lines of new logic. Multiple unresolved review comments identify medium-to-high severity bugs including race conditions and missing cleanup paths that warrant human review before merging. You can customize Macroscope's approvability policy. Learn more. |
There was a problem hiding this comment.
One finding on the new worktree handlers: WorktreeOperationError requires a cause, but the project-not-found path has no underlying failure and fabricates an Error only to satisfy that field.
Posted via Macroscope — Effect Service Conventions
juliusmarminge
left a comment
There was a problem hiding this comment.
please target against #2829 which introduces and reworks the MCP services a bit
769a4e7 to
5c63efe
Compare
5c63efe to
633ba7a
Compare
There was a problem hiding this comment.
One Effect service-convention violation: the new WorktreeMcpService retains a standalone WorktreeMcpServiceShape interface instead of defining the service interface inline in the Context.Service declaration. See details inline.
Posted via Macroscope — Effect Service Conventions
633ba7a to
a3cf90a
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
There are 3 total unresolved issues (including 2 from previous reviews).
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit a3cf90a. Configure here.
a3cf90a to
ee25215
Compare
ee25215 to
33558ea
Compare
33558ea to
ba8a145
Compare
ba8a145 to
fa1aec3
Compare
fa1aec3 to
cd1905b
Compare
| if (recheck.thread.worktreePath !== null) { | ||
| return yield* alreadyInWorktree(recheck.thread.worktreePath); | ||
| } | ||
| yield* threadManagement |
There was a problem hiding this comment.
🟡 Medium mcp/WorktreeMcpService.ts:241
The re-check in recheckAndBind reads the projection and only then calls thread.metadata.update, but the dispatch unconditionally overwrites worktreePath. A concurrent UI binding that commits between the recheck read and the dispatch gets silently overwritten, leaving the UI-created worktree untracked on disk. The handoffThreadsInFlight set only serializes MCP calls, so it does not prevent this race. The dispatched thread.metadata.update command needs an atomic conditional update (e.g., compare-and-set on the current worktreePath) so the binding only applies when the thread is still unbound at commit time.
🤖 Copy this AI Prompt to have your agent fix this:
In file @apps/server/src/mcp/WorktreeMcpService.ts around line 241:
The re-check in `recheckAndBind` reads the projection and only then calls `thread.metadata.update`, but the dispatch unconditionally overwrites `worktreePath`. A concurrent UI binding that commits between the recheck read and the dispatch gets silently overwritten, leaving the UI-created worktree untracked on disk. The `handoffThreadsInFlight` set only serializes MCP calls, so it does not prevent this race. The dispatched `thread.metadata.update` command needs an atomic conditional update (e.g., compare-and-set on the current `worktreePath`) so the binding only applies when the thread is still unbound at commit time.

What Changed
Closes #3753.
Adds two tools to the
t3-codeMCP server:worktree_handoffcreates a worktree and re-points the calling thread at it.branchis the only required argument; the rest are optional:baseRefdefaults to the branch checked out in the project workspacestartFromOrigindefaults to the existing "new worktrees start from origin" server settingpathdefaults to the managed worktrees directoryrunSetupScriptdefaults to trueworktree_statusreports the thread's current binding (attached or not, worktree path, branch, workspace root) so an agent can check before trying.No new machinery. The handler runs the same sequence as the thread-bootstrap path in
ws.ts:GitWorkflowService.createWorktree, athread.meta.updatedispatch, a status refresh, and the project setup script runner. The session restart with resume on cwd change picks the rest up on the next turn, and worktree cleanup on thread delete works unchanged since it keys offworktreePath. Aworktreecapability is added alongsidepreviewin the MCP credential.Deliberately not included: a detach/undo tool. The UI has no equivalent (worktree removal only happens on thread delete), and detaching raises lifecycle questions that seem worth their own discussion.
Why
See #3753: an agent can create a worktree from its shell, but the thread binding lives in T3 Code's orchestration state and nothing agent-reachable can update it, so agent-driven worktree setup dead-ends. The MCP server is the only channel T3 Code exposes to the agent, so this is the only place the capability can live.
Tested with unit tests over the handlers, an HTTP-level test that drives the production MCP layer end to end (auth, initialize, tools/list), and manually in a packaged Windows build: status, handoff from origin, session resume inside the worktree with conversation intact, and cleanup on thread delete.
Checklist
Note
Medium Risk
Handoff mutates thread metadata, runs git operations, and can queue agent messages; rollback and guards reduce orphan worktrees, but incorrect use still affects orchestration and disk state.
Overview
Adds
t3_worktree_handoffandt3_worktree_statusto the T3 Code MCP server so agents can bind orchestration state to a git worktree, not only create one from the shell.t3_worktree_handoffcreates a worktree (optional origin fetch, custom absolute path, setup script), dispatchesthread.metadata.updateto point the calling thread at it, optionally queuescontinuationPromptbefore slower follow-up work, and rolls back the worktree if metadata update fails.t3_worktree_statusreports attachment, paths, and server defaults. NewworktreeMCP capability is included on issued credentials; handoffs are serialized per thread.Contracts in
worktreeMcp.tsdefine inputs, results, andWorktreeMcpFailurecodes. The worktree toolkit registers on the HTTP MCP layer next to preview and orchestrator tools, with broad unit coverage and an HTTPtools/listtest.Reviewed by Cursor Bugbot for commit a3cf90a. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Add worktree handoff and status tools to the t3-code MCP server
t3_worktree_handoffcreates a git worktree and re-points the thread to it (optionally queuing a continuation prompt and running a setup script), andt3_worktree_statusreports the current thread's worktree attachment and defaults.WorktreeMcpFailuretagged error with codes likecapability_denied,already_in_worktree,handoff_in_progress, andoperation_failed.WorktreeMcpServiceto serialize concurrent handoffs and avoid races on thread attachment.worktreecapability is added toALL_MCP_CAPABILITIESand included in all newly issued MCP credentials.📊 Macroscope summarized cd1905b. 9 files reviewed, 0 issues evaluated, 0 issues filtered, 0 comments posted
🗂️ Filtered Issues
No issues evaluated.