Skip to content

Commit 42210fc

Browse files
fix(server): preserve accepted launch model and validate retry workspace
1 parent 1f12be2 commit 42210fc

4 files changed

Lines changed: 122 additions & 32 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ it.effect("attributes a launched thread's first message to the calling thread",
6666
}),
6767
Layer.mock(ThreadLaunch.ThreadLaunchService)({
6868
checkWorkspace: () => Effect.void,
69+
resolveModelSelection: () => Effect.succeed(modelSelection),
6970
launch: (input) => {
7071
launchedSender = input.initialMessage?.senderThreadId;
7172
return Effect.succeed({
@@ -167,6 +168,7 @@ it.effect("launches a scratch thread into the Scratch project", () =>
167168
}),
168169
Layer.mock(ThreadLaunch.ThreadLaunchService)({
169170
checkWorkspace: () => Effect.void,
171+
resolveModelSelection: () => Effect.succeed(modelSelection),
170172
launch: Effect.fnUntraced(function* (input) {
171173
launched.push(input);
172174
const folder = yield* folders
@@ -375,6 +377,7 @@ const clientLaunchHarness = (input: {
375377
Layer.mock(ThreadManagement.ThreadManagementService)({}),
376378
Layer.mock(ThreadLaunch.ThreadLaunchService)({
377379
checkWorkspace: () => Effect.void,
380+
resolveModelSelection: () => Effect.succeed(modelSelection),
378381
launch: (launch) => {
379382
input.launched.push(launch);
380383
return Effect.succeed({

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,6 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
8585
),
8686
)).projectId
8787
: yield* resolveProjectId(context, input.projectId);
88-
const readProject = Project.ProjectService.pipe(
89-
Effect.flatMap((projects) => projects.getById(projectId)),
90-
Effect.mapError(unavailable),
91-
Effect.map(Option.getOrUndefined),
92-
);
9388
yield* ThreadLaunch.ThreadLaunchService.pipe(
9489
Effect.flatMap((launches) =>
9590
launches.checkWorkspace({
@@ -115,11 +110,16 @@ export const layer = McpToolAccess.toLayer(ProjectToolkit, {
115110
});
116111
}),
117112
);
118-
const modelSelection =
119-
input.modelSelection ??
120-
caller?.modelSelection ??
121-
(yield* readProject)?.defaultModelSelection ??
122-
undefined;
113+
const modelSelection = yield* ThreadLaunch.ThreadLaunchService.pipe(
114+
Effect.flatMap((launches) =>
115+
launches.resolveModelSelection({
116+
commandId,
117+
projectId,
118+
modelSelection: input.modelSelection ?? caller?.modelSelection,
119+
}),
120+
),
121+
Effect.mapError(unavailable),
122+
);
123123
if (modelSelection === undefined)
124124
return yield* new OrchestratorMcpFailure({
125125
code: "invalid_request",

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

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
ChatAttachmentId,
1515
ComposerContextId,
1616
type ChatAttachment,
17+
type ModelSelection,
1718
CommandId,
1819
EnvironmentId,
1920
DEFAULT_SERVER_SETTINGS,
@@ -122,6 +123,7 @@ interface HarnessOptions {
122123
readonly git?: Partial<GitWorkflow.GitWorkflowService["Service"]>;
123124
readonly gitDriver?: Layer.Layer<GitVcsDriver.GitVcsDriver>;
124125
readonly workspaceRoot?: string;
126+
readonly getProject?: ProjectService.ProjectService["Service"]["getById"];
125127
}
126128

127129
function makeHarness(options: HarnessOptions = {}) {
@@ -174,17 +176,19 @@ function makeHarness(options: HarnessOptions = {}) {
174176
bootstrap: () => Effect.die("unused"),
175177
update: () => Effect.die("unused"),
176178
delete: () => Effect.die("unused"),
177-
getById: (id) =>
178-
Effect.succeed(
179-
id === projectId
180-
? Option.some({
181-
...project,
182-
workspaceRoot: options.workspaceRoot ?? project.workspaceRoot,
183-
})
184-
: id === otherProjectId
185-
? Option.some(otherProject)
186-
: Option.none(),
187-
),
179+
getById:
180+
options.getProject ??
181+
((id) =>
182+
Effect.succeed(
183+
id === projectId
184+
? Option.some({
185+
...project,
186+
workspaceRoot: options.workspaceRoot ?? project.workspaceRoot,
187+
})
188+
: id === otherProjectId
189+
? Option.some(otherProject)
190+
: Option.none(),
191+
)),
188192
getByWorkspaceRoot: () => Effect.succeed(Option.some(project)),
189193
snapshot: Effect.die("unused"),
190194
getShell: () => Effect.die("unused"),
@@ -2483,6 +2487,7 @@ it.effect("shared intake preserves durable attachment bytes after a lost launch
24832487
),
24842488
),
24852489
checkWorkspace: launches.checkWorkspace,
2490+
resolveModelSelection: launches.resolveModelSelection,
24862491
retryPreparation: launches.retryPreparation,
24872492
prepareRun: launches.prepareRun,
24882493
}),
@@ -3223,7 +3228,7 @@ it.effect.each(
32233228
);
32243229

32253230
it.effect.each(["worktree", "existing_worktree"] as const)(
3226-
"replays an accepted MCP launch after its %s input disappears, but validates fresh launches",
3231+
"replays an accepted MCP launch after its model default and %s input disappear, but validates fresh launches",
32273232
(type) =>
32283233
Effect.gen(function* () {
32293234
const fileSystem = yield* FileSystem.FileSystem;
@@ -3252,8 +3257,15 @@ it.effect.each(["worktree", "existing_worktree"] as const)(
32523257
"init",
32533258
]);
32543259
yield* git(["worktree", "add", "-b", "feature", worktree]);
3260+
let defaultModelSelection: ModelSelection | null = modelSelection;
32553261
const harness = makeHarness({
32563262
workspaceRoot: repo,
3263+
getProject: (id) =>
3264+
Effect.succeed(
3265+
id === projectId
3266+
? Option.some({ ...project, workspaceRoot: repo, defaultModelSelection })
3267+
: Option.none(),
3268+
),
32573269
gitDriver: gitDriverLayer,
32583270
hasCommit: ({ refName }) =>
32593271
git(["rev-parse", "--verify", `${refName}^{commit}`]).pipe(
@@ -3291,13 +3303,18 @@ it.effect.each(["worktree", "existing_worktree"] as const)(
32913303
type === "worktree"
32923304
? { type, baseRef: "feature", startFromOrigin: false }
32933305
: { type, worktreePath: worktree };
3294-
const handle = (clientRequestId: string, strategy = workspaceStrategy) =>
3306+
const handle = (
3307+
clientRequestId: string,
3308+
strategy: ThreadLaunch.ThreadLaunchWorkspaceStrategy = workspaceStrategy,
3309+
message?: string,
3310+
) =>
32953311
toolkit
32963312
.handle("t3_thread_launch", {
32973313
title: "Replay",
32983314
projectId,
32993315
clientRequestId,
33003316
workspaceStrategy: strategy,
3317+
...(message === undefined ? {} : { message }),
33013318
})
33023319
.pipe(
33033320
Stream.unwrap,
@@ -3312,9 +3329,32 @@ it.effect.each(["worktree", "existing_worktree"] as const)(
33123329
}
33133330
const first = yield* handle("accepted");
33143331
assert.property(first, "threadId");
3332+
assert.deepInclude(first, { modelSelection });
3333+
assert.include(
3334+
yield* handle(
3335+
"accepted",
3336+
{ type: "existing_worktree", worktreePath: directory },
3337+
"Run setup",
3338+
),
3339+
{ code: "invalid_request" },
3340+
);
3341+
const created = (yield* threads.listProjectThreads({
3342+
projectId,
3343+
includeSubagents: true,
3344+
}))[0]!;
3345+
const unchanged = yield* threads.getThreadProjection(created.id);
3346+
assert.isEmpty(unchanged.messages);
3347+
assert.isEmpty(unchanged.runs);
33153348
yield* git(["worktree", "remove", worktree]);
33163349
yield* git(["branch", "-D", "feature"]);
3350+
defaultModelSelection = null;
33173351
assert.deepEqual(yield* handle("accepted"), first);
3352+
const missingModel = yield* handle("fresh-no-model", { type: "root" });
3353+
assert.include(missingModel, { code: "invalid_request" });
3354+
assert.include(
3355+
(missingModel as { message: string }).message,
3356+
"project has no default model",
3357+
);
33183358
const fresh = yield* handle("fresh");
33193359
assert.include(fresh, { code: "invalid_request" });
33203360
if (type === "worktree")

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

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ export class ThreadLaunchService extends Context.Service<
178178
ThreadLaunchResult,
179179
ThreadLaunchError | ThreadManagement.ThreadManagementProviderInstanceDisabledError
180180
>;
181+
readonly resolveModelSelection: (
182+
input: Pick<ThreadLaunchInput, "commandId" | "projectId"> & {
183+
readonly modelSelection?: ModelSelection | undefined;
184+
},
185+
) => Effect.Effect<ModelSelection | undefined, ThreadLaunchError>;
181186
/**
182187
* Refuse invalid workspaces before creating a thread, while allowing an
183188
* accepted launch to replay after its original workspace changes.
@@ -234,7 +239,11 @@ const make = Effect.gen(function* () {
234239
yield* Effect.addFinalizer(() => Scope.close(preparationScope, Exit.void));
235240

236241
const mapError =
237-
(input: PreparationInput, operation: ThreadLaunchError["operation"], threadId?: ThreadId) =>
242+
(
243+
input: Pick<ThreadLaunchInput, "commandId" | "projectId">,
244+
operation: ThreadLaunchError["operation"],
245+
threadId?: ThreadId,
246+
) =>
238247
(cause: unknown) =>
239248
new ThreadLaunchError({
240249
operation,
@@ -273,19 +282,51 @@ const make = Effect.gen(function* () {
273282
}
274283
});
275284

285+
const readAcceptedLaunchThread = Effect.fn("ThreadLaunchService.readAcceptedLaunchThread")(
286+
function* (input: Pick<ThreadLaunchInput, "commandId" | "projectId">) {
287+
const receipt = yield* receipts
288+
.getByCommandId(input.commandId)
289+
.pipe(Effect.mapError(mapError(input, "read-receipt")));
290+
if (
291+
Option.isNone(receipt) ||
292+
receipt.value.status !== "accepted" ||
293+
receipt.value.commandType !== "thread.create"
294+
)
295+
return;
296+
const threadId = receipt.value.threadId;
297+
const shell = yield* threads
298+
.getThreadShell(threadId)
299+
.pipe(Effect.mapError(mapError(input, "create-thread", threadId)));
300+
if (shell === null)
301+
return yield* mapError(input, "create-thread", threadId)("Thread not found.");
302+
if (shell.projectId !== input.projectId)
303+
return yield* mapError(input, "resolve-project", threadId)("Project identity changed.");
304+
return shell;
305+
},
306+
);
307+
308+
const resolveModelSelection: ThreadLaunchService["Service"]["resolveModelSelection"] = Effect.fn(
309+
"ThreadLaunchService.resolveModelSelection",
310+
)(function* (input) {
311+
const accepted = yield* readAcceptedLaunchThread(input);
312+
if (accepted !== undefined) return accepted.modelSelection;
313+
if (input.modelSelection !== undefined) return input.modelSelection;
314+
const project = yield* projects
315+
.getById(input.projectId)
316+
.pipe(Effect.mapError(mapError(input, "resolve-project")));
317+
return Option.getOrUndefined(project)?.defaultModelSelection ?? undefined;
318+
});
319+
276320
const checkWorkspace: ThreadLaunchService["Service"]["checkWorkspace"] = Effect.fn(
277321
"ThreadLaunchService.checkWorkspace",
278322
)(function* (input) {
279-
const receipt = yield* receipts
280-
.getByCommandId(input.commandId)
281-
.pipe(Effect.mapError(mapError(input, "read-receipt")));
323+
const accepted = yield* readAcceptedLaunchThread(input);
324+
const strategy = input.workspaceStrategy;
282325
if (
283-
Option.isSome(receipt) &&
284-
receipt.value.status === "accepted" &&
285-
receipt.value.commandType === "thread.create"
326+
accepted !== undefined &&
327+
(strategy.type !== "existing_worktree" || accepted.worktreePath === strategy.worktreePath)
286328
)
287329
return;
288-
const strategy = input.workspaceStrategy;
289330
if (strategy.type === "existing_worktree") {
290331
const project = Option.getOrUndefined(
291332
yield* projects
@@ -1168,7 +1209,13 @@ const make = Effect.gen(function* () {
11681209
);
11691210
};
11701211

1171-
return ThreadLaunchService.of({ launch, checkWorkspace, retryPreparation, prepareRun });
1212+
return ThreadLaunchService.of({
1213+
launch,
1214+
resolveModelSelection,
1215+
checkWorkspace,
1216+
retryPreparation,
1217+
prepareRun,
1218+
});
11721219
});
11731220

11741221
export const layer = Layer.effect(ThreadLaunchService, make);

0 commit comments

Comments
 (0)