Skip to content

Commit 773aeb1

Browse files
saphidclaude
andcommitted
refactor(server): t3_worktree_list picks its checkout in WorktreeMcpService
Move the removed-worktree fallback out of the MCP handler into WorktreeMcpService.listRefs, and let filesystem errors other than a missing path fail instead of falling back to the project root. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 309c693 commit 773aeb1

5 files changed

Lines changed: 78 additions & 116 deletions

File tree

‎apps/server/src/mcp/WorktreeMcpService.test.ts‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as Fiber from "effect/Fiber";
1818
import * as FileSystem from "effect/FileSystem";
1919
import * as Layer from "effect/Layer";
2020
import * as Option from "effect/Option";
21+
import * as PlatformError from "effect/PlatformError";
2122
import * as Schema from "effect/Schema";
2223

2324
import * as GitWorkflowService from "../git/GitWorkflowService.ts";
@@ -113,6 +114,7 @@ interface HarnessOptions {
113114
readonly removeWorktreeFails?: boolean;
114115
readonly deleteLocalBranchFails?: boolean;
115116
readonly createWorktreeGate?: Effect.Effect<void>;
117+
readonly existsFails?: boolean;
116118
}
117119

118120
const makeHarness = (options: HarnessOptions = {}) => {
@@ -232,7 +234,7 @@ const makeHarness = (options: HarnessOptions = {}) => {
232234
),
233235
),
234236
);
235-
const listRefs = vi.fn((input: { readonly query?: string | undefined }) =>
237+
const listRefs = vi.fn((input: { readonly cwd: string; readonly query?: string | undefined }) =>
236238
Effect.succeed({
237239
refs:
238240
options.existingBranchWorktreePath === undefined
@@ -319,13 +321,27 @@ const makeHarness = (options: HarnessOptions = {}) => {
319321
refreshStatus,
320322
} satisfies Partial<VcsStatusBroadcaster.VcsStatusBroadcaster["Service"]>),
321323
NodeServices.layer,
324+
options.existsFails
325+
? FileSystem.layerNoop({
326+
exists: (path) =>
327+
Effect.fail(
328+
PlatformError.systemError({
329+
_tag: "PermissionDenied",
330+
module: "FileSystem",
331+
method: "exists",
332+
pathOrDescriptor: path,
333+
}),
334+
),
335+
})
336+
: Layer.empty,
322337
),
323338
),
324339
);
325340

326341
return {
327342
layer,
328343
scope,
344+
listRefs,
329345
dispatch,
330346
sendToThread,
331347
fetchWorktreeBaseCommit,
@@ -689,3 +705,34 @@ describe("t3_worktree_status", () => {
689705
});
690706
});
691707
});
708+
709+
describe("t3_worktree_list", () => {
710+
const listRefs = (harness: ReturnType<typeof makeHarness>, worktreePath: string | null) =>
711+
Effect.gen(function* () {
712+
const service = yield* WorktreeMcpService.WorktreeMcpService;
713+
return yield* service.listRefs({ worktreePath, workspaceRoot });
714+
}).pipe(Effect.provide(harness.layer));
715+
716+
it.effect("lists refs from the thread's worktree, or the project root once it was removed", () =>
717+
Effect.gen(function* () {
718+
const fs = yield* FileSystem.FileSystem;
719+
const worktreePath = yield* fs.makeTempDirectoryScoped({ prefix: "worktree-list-" });
720+
const harness = makeHarness();
721+
yield* listRefs(harness, worktreePath);
722+
yield* listRefs(harness, `${worktreePath}/removed`);
723+
expect(harness.listRefs.mock.calls.map(([input]) => input.cwd)).toEqual([
724+
worktreePath,
725+
workspaceRoot,
726+
]);
727+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
728+
);
729+
730+
it.effect("fails instead of falling back when the worktree cannot be checked", () => {
731+
const harness = makeHarness({ existsFails: true });
732+
return Effect.gen(function* () {
733+
const exit = yield* Effect.exit(listRefs(harness, "/worktrees/project/unreadable"));
734+
expectTypedFailure(exit, { _tag: "PlatformError" });
735+
expect(harness.listRefs).not.toHaveBeenCalled();
736+
});
737+
});
738+
});

‎apps/server/src/mcp/WorktreeMcpService.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
CommandId,
3+
type GitCommandError,
34
MessageId,
45
type ProjectId,
56
WorktreeMcpFailure,
@@ -8,13 +9,17 @@ import {
89
type WorktreeMcpHandoffResult,
910
type WorktreeMcpSetupScriptStatus,
1011
type WorktreeMcpStatusResult,
12+
type VcsListRefsInput,
13+
type VcsListRefsResult,
1114
} from "@t3tools/contracts";
1215
import * as Cause from "effect/Cause";
1316
import * as Context from "effect/Context";
1417
import * as Crypto from "effect/Crypto";
1518
import * as Effect from "effect/Effect";
19+
import * as FileSystem from "effect/FileSystem";
1620
import * as Layer from "effect/Layer";
1721
import * as Option from "effect/Option";
22+
import type * as PlatformError from "effect/PlatformError";
1823

1924
import * as GitWorkflowService from "../git/GitWorkflowService.ts";
2025
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
@@ -34,6 +39,12 @@ export class WorktreeMcpService extends Context.Service<
3439
readonly status: (
3540
scope: McpInvocationScope,
3641
) => Effect.Effect<WorktreeMcpStatusResult, WorktreeMcpFailure>;
42+
readonly listRefs: (
43+
input: Omit<VcsListRefsInput, "cwd"> & {
44+
readonly worktreePath: string | null;
45+
readonly workspaceRoot: string;
46+
},
47+
) => Effect.Effect<VcsListRefsResult, GitCommandError | PlatformError.PlatformError>;
3748
}
3849
>()("t3/mcp/WorktreeMcpService") {}
3950

@@ -56,6 +67,7 @@ const asOperationFailed = (prefix: string) =>
5667

5768
const make = Effect.gen(function* () {
5869
const crypto = yield* Crypto.Crypto;
70+
const fileSystem = yield* FileSystem.FileSystem;
5971
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
6072
const projects = yield* ProjectService.ProjectService;
6173
const serverSettings = yield* ServerSettings.ServerSettingsService;
@@ -428,13 +440,25 @@ const make = Effect.gen(function* () {
428440
},
429441
);
430442

431-
return WorktreeMcpService.of({ handoff, status });
443+
const listRefs: WorktreeMcpService["Service"]["listRefs"] = Effect.fn(
444+
"WorktreeMcpService.listRefs",
445+
)(function* ({ worktreePath, workspaceRoot, ...refs }) {
446+
// Storage cleanup removes a worktree but keeps its path on the thread.
447+
const cwd =
448+
worktreePath !== null && (yield* fileSystem.exists(worktreePath))
449+
? worktreePath
450+
: workspaceRoot;
451+
return yield* gitWorkflow.listRefs({ ...refs, cwd });
452+
});
453+
454+
return WorktreeMcpService.of({ handoff, status, listRefs });
432455
});
433456

434457
export const layer: Layer.Layer<
435458
WorktreeMcpService,
436459
never,
437460
| Crypto.Crypto
461+
| FileSystem.FileSystem
438462
| ThreadManagementService.ThreadManagementService
439463
| ProjectService.ProjectService
440464
| ServerSettings.ServerSettingsService

‎apps/server/src/mcp/toolkits/worktree/handlers.test.ts‎

Lines changed: 0 additions & 97 deletions
This file was deleted.

‎apps/server/src/mcp/toolkits/worktree/handlers.ts‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { OrchestratorMcpFailure } from "@t3tools/contracts";
22
import * as Option from "effect/Option";
3-
import * as GitWorkflow from "../../../git/GitWorkflowService.ts";
43
import * as Project from "../../../project/ProjectService.ts";
54
import { readThread, unavailable } from "../../threadAccess.ts";
65
import * as Effect from "effect/Effect";
7-
import * as FileSystem from "effect/FileSystem";
86

97
import * as McpInvocationContext from "../../McpInvocationContext.ts";
108
import * as McpToolAccess from "../../McpToolAccess.ts";
@@ -31,19 +29,12 @@ const handlers = {
3129
code: "invalid_request",
3230
message: "The project was not found.",
3331
});
34-
const fileSystem = yield* FileSystem.FileSystem;
35-
// Storage cleanup removes a worktree but keeps its path on the thread.
36-
const worktreeExists =
37-
thread.worktreePath !== null &&
38-
(yield* fileSystem.exists(thread.worktreePath).pipe(Effect.orElseSucceed(() => false)));
39-
const git = yield* GitWorkflow.GitWorkflowService;
40-
return yield* git
32+
const service = yield* WorktreeMcpService.WorktreeMcpService;
33+
return yield* service
4134
.listRefs({
4235
...refs,
43-
cwd:
44-
worktreeExists && thread.worktreePath !== null
45-
? thread.worktreePath
46-
: project.value.workspaceRoot,
36+
worktreePath: thread.worktreePath,
37+
workspaceRoot: project.value.workspaceRoot,
4738
})
4839
.pipe(Effect.mapError(unavailable));
4940
}),

‎apps/server/src/mcp/toolkits/worktree/tools.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import {
99
WorktreeMcpStatusResult,
1010
ThreadId,
1111
} from "@t3tools/contracts";
12-
import * as FileSystem from "effect/FileSystem";
1312
import * as Schema from "effect/Schema";
14-
import * as GitWorkflowService from "../../../git/GitWorkflowService.ts";
1513
import * as ProjectService from "../../../project/ProjectService.ts";
1614
import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
1715
import { Tool, Toolkit } from "effect/ai";
@@ -79,8 +77,7 @@ const WorktreeListTool = Tool.make("t3_worktree_list", {
7977
McpInvocationContext.McpInvocationContext,
8078
ThreadManagementService.ThreadManagementService,
8179
ProjectService.ProjectService,
82-
GitWorkflowService.GitWorkflowService,
83-
FileSystem.FileSystem,
80+
WorktreeMcpService.WorktreeMcpService,
8481
],
8582
})
8683
.annotate(Tool.Readonly, true)

0 commit comments

Comments
 (0)