Skip to content
TypeScript

fix(server): queued delegated completions keep a newer model choice (#22579)

F4llen5731c0222a97
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts
index db398ebf8aa..5f9c6ea3993 100644
--- a/apps/server/src/orchestration-v2/Orchestrator.ts
+++ b/apps/server/src/orchestration-v2/Orchestrator.ts
@@ -767,7 +767,8 @@ function followsCurrentConversation(
message: OrchestrationV2ConversationMessage | undefined,
): boolean {
return (
- message?.notification?.source.kind === "monitor" && !isBufferedProviderNotification(message)
+ message?.delegatedCompletion !== undefined ||
+ (message?.notification?.source.kind === "monitor" && !isBufferedProviderNotification(message))
);
}
@@ -776,7 +777,6 @@ function queuedRunModelSelection(
run: OrchestrationV2Run,
): ModelSelection {
const message = projection.messages.find((candidate) => candidate.id === run.userMessageId);
- // App-owned monitor updates follow the agent currently handling the thread.
return followsCurrentConversation(message)
? projection.thread.modelSelection
: run.modelSelection;
diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts
index 7b4a24219b2..8e1ca75fcac 100644
--- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts
+++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts
@@ -5714,6 +5714,116 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
}),
);
+ it.effect.each([
+ { ...modelSelection, model: "gpt-5.5", options: [{ id: "reasoningEffort", value: "high" }] },
+ { instanceId: alternateInstanceId, model: "gpt-5.5" },
+ ])(
+ "starts a queued delegated completion on a selection set while it waited: %j",
+ (newerSelection) =>
+ Effect.gen(function* () {
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
+ const eventSink = yield* EventSink.EventSinkV2;
+ const threadId = ThreadId.make(
+ `runtime-layer-queued-delegated-completion-selection-${newerSelection.instanceId}`,
+ );
+ const taskId = NodeId.make(`${threadId}:task`);
+ const completionMessageId = MessageId.make(`${threadId}:completion`);
+ yield* orchestrator.dispatch({
+ type: "thread.create",
+ commandId: CommandId.make(`${threadId}:create`),
+ threadId,
+ projectId: ProjectId.make(`${threadId}:project`),
+ createdBy: "user",
+ creationSource: "web",
+ title: "Delegated completion selection",
+ modelSelection,
+ runtimeMode: "full-access",
+ interactionMode: "default",
+ branch: null,
+ worktreePath: process.cwd(),
+ });
+ yield* orchestrator.dispatch({
+ type: "message.dispatch",
+ commandId: CommandId.make(`${threadId}:active`),
+ threadId,
+ messageId: MessageId.make(`${threadId}:active`),
+ text: "Delegate a task",
+ attachments: [],
+ modelSelection,
+ dispatchMode: { type: "start_immediately" },
+ createdBy: "user",
+ creationSource: "web",
+ });
+ const active = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;
+ const now = yield* DateTime.now;
+ yield* eventSink.write({
+ events: [
+ {
+ id: EventId.make(`${threadId}:cohort`),
+ type: "run.updated",
+ threadId,
+ runId: active.id,
+ occurredAt: now,
+ payload: {
+ ...active,
+ delegatedCompletion: {
+ disposition: "open",
+ nextGeneration: 2,
+ delivery: { messageId: completionMessageId, generation: 1, taskIds: [taskId] },
+ },
+ },
+ },
+ ],
+ });
+ yield* orchestrator.dispatch({
+ type: "message.dispatch",
+ commandId: CommandId.make(`${threadId}:completion`),
+ threadId,
+ messageId: completionMessageId,
+ text: "Delegated task finished",
+ attachments: [],
+ dispatchMode: { type: "queue_after_active" },
+ createdBy: "agent",
+ creationSource: "server",
+ delegatedCompletion: { parentRunId: active.id, generation: 1, taskIds: [taskId] },
+ });
+ yield* orchestrator.dispatch({
+ type: "thread.model-selection.set",
+ commandId: CommandId.make(`${threadId}:select-newer`),
+ threadId,
+ modelSelection: newerSelection,
+ });
+ const waiting = yield* orchestrator.getThreadProjection(threadId);
+ yield* eventSink.write({
+ events: [
+ {
+ id: EventId.make(`${threadId}:completed`),
+ type: "run.updated",
+ threadId,
+ runId: active.id,
+ occurredAt: now,
+ payload: {
+ ...waiting.runs.find((run) => run.id === active.id)!,
+ status: "completed",
+ completedAt: now,
+ },
+ },
+ ],
+ });
+ yield* orchestrator.resumeQueuedRuns;
+ const after = yield* orchestrator.getThreadProjection(threadId);
+ const delivered = after.runs.find((run) => run.userMessageId === completionMessageId)!;
+ assert.equal(delivered.status, "starting");
+ assert.deepEqual(delivered.modelSelection, newerSelection);
+ assert.equal(
+ after.attempts.find((attempt) => attempt.id === delivered.activeAttemptId)
+ ?.providerInstanceId,
+ newerSelection.instanceId,
+ );
+ assert.deepEqual(after.thread.modelSelection, newerSelection);
+ }),
+ );
+
it.effect("keeps the queue after a user interrupts the active run", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;