Skip to content

Commit 1fdb652

Browse files
fix(server): validate worktree membership on launch retries
1 parent c60c49f commit 1fdb652

2 files changed

Lines changed: 45 additions & 6 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
102102
),
103103
)).projectId
104104
: yield* resolveProjectId(context, input.projectId);
105-
// Accepted creates keep their workspace and model even if refs or
106-
// project defaults change. Message intake still handles their replay.
105+
// Accepted creates skip mutable base/default-model preflights.
106+
// Message intake still handles their replay and preparation.
107107
const acceptedLaunch =
108108
input.clientRequestId === undefined
109109
? undefined
@@ -120,7 +120,7 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
120120
Effect.mapError(unavailable),
121121
Effect.map(Option.getOrUndefined),
122122
);
123-
if (acceptedLaunch === undefined && input.workspaceStrategy?.type === "existing_worktree") {
123+
if (input.workspaceStrategy?.type === "existing_worktree") {
124124
const project = yield* readProject;
125125
if (project === undefined)
126126
return yield* new OrchestratorMcpFailure({

‎apps/server/src/orchestration-v2/ThreadLaunchService.test.ts‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,7 +2040,7 @@ it.effect("replays a server-allocated launch", () =>
20402040
}),
20412041
);
20422042

2043-
it.effect("MCP replays an accepted launch after its base and project default disappear", () =>
2043+
it.effect("MCP replays accepted launches and checks worktree membership", () =>
20442044
Effect.gen(function* () {
20452045
const setupEntered = yield* Deferred.make<void>();
20462046
const allowSetup = yield* Deferred.make<void>();
@@ -2075,7 +2075,9 @@ it.effect("MCP replays an accepted launch after its base and project default dis
20752075
NodeCrypto.layer,
20762076
NodeServices.layer,
20772077
Layer.succeed(McpInvocationContext.McpInvocationContext, context),
2078-
Layer.mock(GitVcsDriver.GitVcsDriver)({}),
2078+
Layer.mock(GitVcsDriver.GitVcsDriver)({
2079+
listWorktreePaths: () => Effect.succeed([project.workspaceRoot, "/repo-worktrees/feature"]),
2080+
}),
20792081
ServerConfig.layerTest(process.cwd(), { prefix: "t3-launch-retry-" }).pipe(
20802082
Layer.provide(NodeServices.layer),
20812083
),
@@ -2097,12 +2099,47 @@ it.effect("MCP replays an accepted launch after its base and project default dis
20972099
Stream.runCollect,
20982100
Effect.map((results) => results.at(-1)?.result),
20992101
);
2102+
const fs = yield* FileSystem.FileSystem;
2103+
const outsidePath = yield* fs.makeTempDirectoryScoped({ prefix: "launch-unregistered-" });
2104+
const outsideStrategy = { type: "existing_worktree" as const, worktreePath: outsidePath };
2105+
const refusal = {
2106+
code: "invalid_request",
2107+
message:
2108+
"worktreePath must be one of the project's git worktrees. t3_worktree_list shows them.",
2109+
};
2110+
const idleInput = {
2111+
title: "Idle accepted launch",
2112+
projectId,
2113+
clientRequestId: "idle-retry",
2114+
workspaceStrategy: { type: "root" as const },
2115+
};
2116+
assert.include(yield* launch(idleInput), { runId: null, status: null });
2117+
assert.include(
2118+
yield* launch({
2119+
...idleInput,
2120+
message: "Run outside the project",
2121+
workspaceStrategy: outsideStrategy,
2122+
}),
2123+
refusal,
2124+
);
2125+
const threads = yield* ThreadManagement.ThreadManagementService;
2126+
const idle = yield* threads.getThreadProjection(
2127+
ThreadId.make("mcp:launch:client%3Alaunch-retry:idle-retry"),
2128+
);
2129+
assert.equal(idle.messages.length, 0);
2130+
assert.equal(idle.runs.length, 0);
2131+
assert.equal(idle.thread.worktreePath, null);
2132+
assert.equal(harness.createWorktree.mock.calls.length, 0);
2133+
assert.equal(harness.runSetup.mock.calls.length, 0);
2134+
21002135
const first = yield* launch(input);
21012136
assert.isDefined(first);
21022137
assert.notProperty(first, "code");
21032138
yield* Deferred.await(setupEntered);
21042139
changed = true;
21052140

2141+
assert.include(yield* launch({ ...input, workspaceStrategy: outsideStrategy }), refusal);
2142+
21062143
assert.deepEqual(yield* launch(input), first);
21072144
assert.deepEqual(yield* launch({ ...input, modelSelection: claudeModelSelection }), first);
21082145
assert.include(yield* launch({ ...input, projectId: otherProjectId }), {
@@ -2129,10 +2166,12 @@ it.effect("MCP replays an accepted launch after its base and project default dis
21292166
{ code: "invalid_request" },
21302167
);
21312168
const threadId = ThreadId.make("mcp:launch:client%3Alaunch-retry:lost-response");
2132-
const threads = yield* ThreadManagement.ThreadManagementService;
21332169
const projection = yield* threads.getThreadProjection(threadId);
21342170
assert.equal(projection.messages.length, 1);
21352171
assert.equal(projection.runs.length, 1);
2172+
assert.equal(projection.runs[0]?.status, "preparing");
2173+
assert.deepEqual(projection.runs[0]?.workspacePreparation, input.workspaceStrategy);
2174+
assert.equal(projection.thread.worktreePath, "/repo-worktrees/feature");
21362175
assert.equal(harness.createWorktree.mock.calls.length, 1);
21372176
assert.equal(harness.runSetup.mock.calls.length, 1);
21382177
yield* Deferred.succeed(allowSetup, undefined);

0 commit comments

Comments
 (0)