@devries-cameron20 / t3code-1 / commits / 0a41bbce4f
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): t3_worktree_list lists refs after storage cleanup removes a thread's worktree (#29645)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>

Alex Southwell committed Nov 3, 2026 at 23:29 UTC 0a41bbce4f63b0ae9275c1e3de56f7396050ffcd
4 files changed +81 -8
apps/server/src/mcp/WorktreeMcpService.test.ts
+48 -1
@@ -19,6 +19,7 @@ import * as Fiber from "effect/Fiber";
19 import * as FileSystem from "effect/FileSystem";
20 import * as Layer from "effect/Layer";
21 import * as Option from "effect/Option";
22 +import * as PlatformError from "effect/PlatformError";
23 import * as Schema from "effect/Schema";
24
25 import * as GitWorkflowService from "../git/GitWorkflowService.ts";
@@ -115,6 +116,7 @@ interface HarnessOptions {
116 readonly removeWorktreeFails?: boolean;
117 readonly deleteLocalBranchFails?: boolean;
118 readonly createWorktreeGate?: Effect.Effect<void>;
119 + readonly existsFails?: boolean;
120 }
121
122 const makeHarness = (options: HarnessOptions = {}) => {
@@ -234,7 +236,7 @@ const makeHarness = (options: HarnessOptions = {}) => {
236 ),
237 ),
238 );
237 - const listRefs = vi.fn((input: { readonly query?: string | undefined }) =>
239 + const listRefs = vi.fn((input: { readonly cwd: string; readonly query?: string | undefined }) =>
240 Effect.succeed({
241 refs:
242 options.existingBranchWorktreePath === undefined
@@ -331,6 +333,19 @@ const makeHarness = (options: HarnessOptions = {}) => {
333 } satisfies Partial<VcsStatusBroadcaster.VcsStatusBroadcaster["Service"]>),
334 NodeServices.layer,
335 NodeCrypto.layer,
336 + options.existsFails
337 + ? FileSystem.layerNoop({
338 + exists: (path) =>
339 + Effect.fail(
340 + PlatformError.systemError({
341 + _tag: "PermissionDenied",
342 + module: "FileSystem",
343 + method: "exists",
344 + pathOrDescriptor: path,
345 + }),
346 + ),
347 + })
348 + : Layer.empty,
349 ),
350 ),
351 );
@@ -338,6 +353,7 @@ const makeHarness = (options: HarnessOptions = {}) => {
353 return {
354 layer,
355 scope,
356 + listRefs,
357 dispatch,
358 sendToThread,
359 fetchWorktreeBaseCommit,
@@ -711,3 +727,34 @@ describe("t3_worktree_status", () => {
727 });
728 });
729 });
730 +
731 +describe("t3_worktree_list", () => {
732 + const listRefs = (harness: ReturnType<typeof makeHarness>, worktreePath: string | null) =>
733 + Effect.gen(function* () {
734 + const service = yield* WorktreeMcpService.WorktreeMcpService;
735 + return yield* service.listRefs({ worktreePath, workspaceRoot });
736 + }).pipe(Effect.provide(harness.layer));
737 +
738 + it.effect("lists refs from the thread's worktree, or the project root once it was removed", () =>
739 + Effect.gen(function* () {
740 + const fs = yield* FileSystem.FileSystem;
741 + const worktreePath = yield* fs.makeTempDirectoryScoped({ prefix: "worktree-list-" });
742 + const harness = makeHarness();
743 + yield* listRefs(harness, worktreePath);
744 + yield* listRefs(harness, `${worktreePath}/removed`);
745 + expect(harness.listRefs.mock.calls.map(([input]) => input.cwd)).toEqual([
746 + worktreePath,
747 + workspaceRoot,
748 + ]);
749 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
750 + );
751 +
752 + it.effect("fails instead of falling back when the worktree cannot be checked", () => {
753 + const harness = makeHarness({ existsFails: true });
754 + return Effect.gen(function* () {
755 + const exit = yield* Effect.exit(listRefs(harness, "/worktrees/project/unreadable"));
756 + expectTypedFailure(exit, { _tag: "PlatformError" });
757 + expect(harness.listRefs).not.toHaveBeenCalled();
758 + });
759 + });
760 +});
apps/server/src/mcp/WorktreeMcpService.ts
+25 -1
@@ -1,5 +1,6 @@
1 import {
2 CommandId,
3 + type GitCommandError,
4 MessageId,
5 type ProjectId,
6 WorktreeMcpFailure,
@@ -8,14 +9,18 @@ import {
9 type WorktreeMcpHandoffResult,
10 type WorktreeMcpSetupScriptStatus,
11 type WorktreeMcpStatusResult,
12 + type VcsListRefsInput,
13 + type VcsListRefsResult,
14 } from "@t3tools/contracts";
15 import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
16 import * as Cause from "effect/Cause";
17 import * as Context from "effect/Context";
18 import * as SecureRandom from "effect/crypto/SecureRandom";
19 import * as Effect from "effect/Effect";
20 +import * as FileSystem from "effect/FileSystem";
21 import * as Layer from "effect/Layer";
22 import * as Option from "effect/Option";
23 +import type * as PlatformError from "effect/PlatformError";
24
25 import * as GitWorkflowService from "../git/GitWorkflowService.ts";
26 import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
@@ -35,6 +40,12 @@ export class WorktreeMcpService extends Context.Service<
40 readonly status: (
41 scope: McpInvocationScope,
42 ) => Effect.Effect<WorktreeMcpStatusResult, WorktreeMcpFailure>;
43 + readonly listRefs: (
44 + input: Omit<VcsListRefsInput, "cwd"> & {
45 + readonly worktreePath: string | null;
46 + readonly workspaceRoot: string;
47 + },
48 + ) => Effect.Effect<VcsListRefsResult, GitCommandError | PlatformError.PlatformError>;
49 }
50 >()("t3/mcp/WorktreeMcpService") {}
51
@@ -57,6 +68,7 @@ const asOperationFailed = (prefix: string) =>
68
69 const make = Effect.gen(function* () {
70 const random = yield* SecureRandom.SecureRandom;
71 + const fileSystem = yield* FileSystem.FileSystem;
72 const threadManagement = yield* ThreadManagementService.ThreadManagementService;
73 const projects = yield* ProjectService.ProjectService;
74 const serverSettings = yield* ServerSettings.ServerSettingsService;
@@ -434,13 +446,25 @@ const make = Effect.gen(function* () {
446 },
447 );
448
437 - return WorktreeMcpService.of({ handoff, status });
449 + const listRefs: WorktreeMcpService["Service"]["listRefs"] = Effect.fn(
450 + "WorktreeMcpService.listRefs",
451 + )(function* ({ worktreePath, workspaceRoot, ...refs }) {
452 + // Storage cleanup removes a worktree but keeps its path on the thread.
453 + const cwd =
454 + worktreePath !== null && (yield* fileSystem.exists(worktreePath))
455 + ? worktreePath
456 + : workspaceRoot;
457 + return yield* gitWorkflow.listRefs({ ...refs, cwd });
458 + });
459 +
460 + return WorktreeMcpService.of({ handoff, status, listRefs });
461 });
462
463 export const layer: Layer.Layer<
464 WorktreeMcpService,
465 never,
466 | SecureRandom.SecureRandom
467 + | FileSystem.FileSystem
468 | ThreadManagementService.ThreadManagementService
469 | ProjectService.ProjectService
470 | ServerSettings.ServerSettingsService
apps/server/src/mcp/toolkits/worktree/handlers.ts
+7 -4
@@ -1,6 +1,5 @@
1 import { OrchestratorMcpFailure } from "@t3tools/contracts";
2 import * as Option from "effect/Option";
3 -import * as GitWorkflow from "../../../git/GitWorkflowService.ts";
3 import * as Project from "../../../project/ProjectService.ts";
4 import { readThread, unavailable } from "../../threadAccess.ts";
5 import * as Effect from "effect/Effect";
@@ -30,9 +29,13 @@ const handlers = {
29 code: "invalid_request",
30 message: "The project was not found.",
31 });
33 - const git = yield* GitWorkflow.GitWorkflowService;
34 - return yield* git
35 - .listRefs({ ...refs, cwd: thread.worktreePath ?? project.value.workspaceRoot })
32 + const service = yield* WorktreeMcpService.WorktreeMcpService;
33 + return yield* service
34 + .listRefs({
35 + ...refs,
36 + worktreePath: thread.worktreePath,
37 + workspaceRoot: project.value.workspaceRoot,
38 + })
39 .pipe(Effect.mapError(unavailable));
40 }),
41 ),
apps/server/src/mcp/toolkits/worktree/tools.ts
+1 -2
@@ -10,7 +10,6 @@ import {
10 ThreadId,
11 } from "@t3tools/contracts";
12 import * as Schema from "effect/Schema";
13 -import * as GitWorkflowService from "../../../git/GitWorkflowService.ts";
13 import * as ProjectService from "../../../project/ProjectService.ts";
14 import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
15 import { Tool, Toolkit } from "effect/ai";
@@ -78,7 +77,7 @@ const WorktreeListTool = Tool.make("t3_worktree_list", {
77 McpInvocationContext.McpInvocationContext,
78 ThreadManagementService.ThreadManagementService,
79 ProjectService.ProjectService,
81 - GitWorkflowService.GitWorkflowService,
80 + WorktreeMcpService.WorktreeMcpService,
81 ],
82 })
83 .annotate(Tool.Readonly, true)