Skip to content

Commit ebba131

Browse files
fix(server): preserve accepted launch preparation and limits
1 parent 1fdb652 commit ebba131

3 files changed

Lines changed: 92 additions & 11 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11277,6 +11277,33 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1127711277
}),
1127811278
),
1127911279
);
11280+
if (
11281+
command.type === "message.dispatch" &&
11282+
command.dispatchMode?.type === "defer_start" &&
11283+
(yield* DispatchModeLimit) !== undefined
11284+
) {
11285+
const runCreated = storedEvents.find(
11286+
(stored) => stored.event.type === "run.created",
11287+
)?.event;
11288+
if (runCreated?.type === "run.created") {
11289+
const current = yield* projectionStore
11290+
.getThreadRecords(command.threadId, ["runs"])
11291+
.pipe(
11292+
Effect.mapError(
11293+
(cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }),
11294+
),
11295+
);
11296+
// Replaying this receipt can resume workspace preparation, so it must
11297+
// respect the caller's current ceiling just like a fresh dispatch.
11298+
if (
11299+
current.runs.some(
11300+
(run) => run.id === runCreated.payload.id && run.status === "preparing",
11301+
)
11302+
) {
11303+
yield* refuseAboveDispatchModeLimit(command, command.threadId, current.thread);
11304+
}
11305+
}
11306+
}
1128011307
if (command.type === "queue.resume") {
1128111308
yield* mapDispatchError(command)(startNextQueuedRun(command.threadId));
1128211309
}

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

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,7 @@ it.effect("MCP replays accepted launches and checks worktree membership", () =>
20902090
title: "Only once",
20912091
projectId,
20922092
clientRequestId: "lost-response",
2093+
runtimeMode: "approval-required" as const,
20932094
message: "Do the work",
20942095
workspaceStrategy: { type: "worktree" as const, baseRef: "temporary-base" },
20952096
};
@@ -2166,7 +2167,24 @@ it.effect("MCP replays accepted launches and checks worktree membership", () =>
21662167
{ code: "invalid_request" },
21672168
);
21682169
const threadId = ThreadId.make("mcp:launch:client%3Alaunch-retry:lost-response");
2170+
assert.equal((yield* threads.getThreadShell(threadId))?.runtimeMode, "approval-required");
2171+
yield* threads.dispatch({
2172+
type: "thread.runtime-mode.set",
2173+
commandId: CommandId.make("owner:raise-launch-modes"),
2174+
threadId,
2175+
runtimeMode: "full-access",
2176+
});
2177+
assert.include(
2178+
yield* launch(input).pipe(
2179+
Effect.provideService(McpInvocationContext.McpInvocationContext, {
2180+
...context,
2181+
client: { ...context.client!, access: "approval-required" },
2182+
}),
2183+
),
2184+
{ code: "orchestration_error" },
2185+
);
21692186
const projection = yield* threads.getThreadProjection(threadId);
2187+
assert.equal(projection.thread.runtimeMode, "full-access");
21702188
assert.equal(projection.messages.length, 1);
21712189
assert.equal(projection.runs.length, 1);
21722190
assert.equal(projection.runs[0]?.status, "preparing");
@@ -2183,6 +2201,15 @@ it.effect("MCP replays accepted launches and checks worktree membership", () =>
21832201
),
21842202
Stream.runHead,
21852203
);
2204+
assert.include(
2205+
yield* launch(input).pipe(
2206+
Effect.provideService(McpInvocationContext.McpInvocationContext, {
2207+
...context,
2208+
client: { ...context.client!, access: "approval-required" },
2209+
}),
2210+
),
2211+
{ threadId, status: "starting" },
2212+
);
21862213
}).pipe(Effect.provide(dependencies));
21872214
}),
21882215
);
@@ -2588,19 +2615,38 @@ it.effect("schedules an accepted preparing message exactly once across concurren
25882615
text: "Resume preparation",
25892616
attachments: [],
25902617
modelSelection: input.modelSelection,
2591-
dispatchMode: { type: "defer_start" },
2618+
dispatchMode: { type: "defer_start", workspaceStrategy: input.workspaceStrategy },
25922619
createdBy: input.createdBy,
25932620
creationSource: input.creationSource,
25942621
});
25952622
const preparing = yield* threads.getThreadProjection(input.threadId);
25962623
assert.equal(preparing.runs[0]?.status, "preparing");
25972624

2598-
const [first, second] = yield* Effect.all([launches.launch(input), launches.launch(input)], {
2625+
const retry = { ...input, workspaceStrategy: { type: "root" as const } };
2626+
const [first, second] = yield* Effect.all([launches.launch(retry), launches.launch(retry)], {
25992627
concurrency: "unbounded",
26002628
});
2601-
yield* Deferred.await(setupEntered);
2629+
const prepared = yield* Effect.raceFirst(
2630+
Deferred.await(setupEntered).pipe(Effect.as(true)),
2631+
threads.streamStoredEventsFrom({ threadId: input.threadId }).pipe(
2632+
Stream.filter(
2633+
(stored) =>
2634+
stored.commandId === CommandId.make(input.commandId + ":release") &&
2635+
stored.event.type === "run.updated",
2636+
),
2637+
Stream.runHead,
2638+
Effect.as(false),
2639+
),
2640+
);
2641+
assert.isTrue(prepared, "the accepted worktree setup must run before releasing the run");
26022642
assert.isTrue(first.resumed);
26032643
assert.isTrue(second.resumed);
2644+
assert.equal(harness.createWorktree.mock.calls.length, 1);
2645+
assert.equal(harness.runSetup.mock.calls[0]?.[0].worktreePath, "/repo-worktrees/feature");
2646+
assert.deepEqual(
2647+
(yield* threads.getThreadProjection(input.threadId)).runs[0]?.workspacePreparation,
2648+
input.workspaceStrategy,
2649+
);
26042650
assert.equal(harness.runSetup.mock.calls.length, 1);
26052651
yield* Deferred.succeed(allowSetup, undefined);
26062652
}).pipe(Effect.provide(harness.layer));

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,9 +1041,8 @@ const make = Effect.gen(function* () {
10411041
const projection = yield* threads
10421042
.getThreadProjection(threadId)
10431043
.pipe(Effect.mapError(mapError(input, "create-thread", threadId)));
1044-
const runIsPreparing =
1045-
runId !== null &&
1046-
projection.runs.some((run) => run.id === runId && run.status === "preparing");
1044+
const acceptedRun = projection.runs.find((run) => run.id === runId);
1045+
const runIsPreparing = acceptedRun?.status === "preparing";
10471046
const shouldSchedule = runId === null ? Option.isNone(launchReceipt) : runIsPreparing;
10481047
const boundWorktreePath = projection.thread.worktreePath;
10491048
const preparationStrategy: ThreadLaunchWorkspaceStrategy =
@@ -1070,11 +1069,20 @@ const make = Effect.gen(function* () {
10701069
Effect.mapError(mapError(input, "update-thread", threadId)),
10711070
);
10721071
if (preparationStillRequired) {
1073-
yield* schedulePreparation(
1074-
{ ...input, workspaceStrategy: preparationStrategy },
1075-
threadId,
1076-
runId,
1077-
);
1072+
if (acceptedRun?.workspacePreparation !== undefined) {
1073+
yield* scheduleRunPreparation(
1074+
{ commandId: input.commandId, threadId, runId: acceptedRun.id },
1075+
projection,
1076+
acceptedRun,
1077+
acceptedRun.workspacePreparation,
1078+
);
1079+
} else {
1080+
yield* schedulePreparation(
1081+
{ ...input, workspaceStrategy: preparationStrategy },
1082+
threadId,
1083+
runId,
1084+
);
1085+
}
10781086
} else {
10791087
yield* releasePreparation(input.commandId);
10801088
}

0 commit comments

Comments
 (0)