Skip to content

Commit 5731c02

Browse files
authored
fix(server): queued delegated completions keep a newer model choice (pingdotgg#22579)
1 parent 655165d commit 5731c02

2 files changed

Lines changed: 112 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,8 @@ function followsCurrentConversation(
767767
message: OrchestrationV2ConversationMessage | undefined,
768768
): boolean {
769769
return (
770-
message?.notification?.source.kind === "monitor" && !isBufferedProviderNotification(message)
770+
message?.delegatedCompletion !== undefined ||
771+
(message?.notification?.source.kind === "monitor" && !isBufferedProviderNotification(message))
771772
);
772773
}
773774

@@ -776,7 +777,6 @@ function queuedRunModelSelection(
776777
run: OrchestrationV2Run,
777778
): ModelSelection {
778779
const message = projection.messages.find((candidate) => candidate.id === run.userMessageId);
779-
// App-owned monitor updates follow the agent currently handling the thread.
780780
return followsCurrentConversation(message)
781781
? projection.thread.modelSelection
782782
: run.modelSelection;

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

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5714,6 +5714,116 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
57145714
}),
57155715
);
57165716

5717+
it.effect.each([
5718+
{ ...modelSelection, model: "gpt-5.5", options: [{ id: "reasoningEffort", value: "high" }] },
5719+
{ instanceId: alternateInstanceId, model: "gpt-5.5" },
5720+
])(
5721+
"starts a queued delegated completion on a selection set while it waited: %j",
5722+
(newerSelection) =>
5723+
Effect.gen(function* () {
5724+
const orchestrator = yield* Orchestrator.OrchestratorV2;
5725+
const eventSink = yield* EventSink.EventSinkV2;
5726+
const threadId = ThreadId.make(
5727+
`runtime-layer-queued-delegated-completion-selection-${newerSelection.instanceId}`,
5728+
);
5729+
const taskId = NodeId.make(`${threadId}:task`);
5730+
const completionMessageId = MessageId.make(`${threadId}:completion`);
5731+
yield* orchestrator.dispatch({
5732+
type: "thread.create",
5733+
commandId: CommandId.make(`${threadId}:create`),
5734+
threadId,
5735+
projectId: ProjectId.make(`${threadId}:project`),
5736+
createdBy: "user",
5737+
creationSource: "web",
5738+
title: "Delegated completion selection",
5739+
modelSelection,
5740+
runtimeMode: "full-access",
5741+
interactionMode: "default",
5742+
branch: null,
5743+
worktreePath: process.cwd(),
5744+
});
5745+
yield* orchestrator.dispatch({
5746+
type: "message.dispatch",
5747+
commandId: CommandId.make(`${threadId}:active`),
5748+
threadId,
5749+
messageId: MessageId.make(`${threadId}:active`),
5750+
text: "Delegate a task",
5751+
attachments: [],
5752+
modelSelection,
5753+
dispatchMode: { type: "start_immediately" },
5754+
createdBy: "user",
5755+
creationSource: "web",
5756+
});
5757+
const active = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;
5758+
const now = yield* DateTime.now;
5759+
yield* eventSink.write({
5760+
events: [
5761+
{
5762+
id: EventId.make(`${threadId}:cohort`),
5763+
type: "run.updated",
5764+
threadId,
5765+
runId: active.id,
5766+
occurredAt: now,
5767+
payload: {
5768+
...active,
5769+
delegatedCompletion: {
5770+
disposition: "open",
5771+
nextGeneration: 2,
5772+
delivery: { messageId: completionMessageId, generation: 1, taskIds: [taskId] },
5773+
},
5774+
},
5775+
},
5776+
],
5777+
});
5778+
yield* orchestrator.dispatch({
5779+
type: "message.dispatch",
5780+
commandId: CommandId.make(`${threadId}:completion`),
5781+
threadId,
5782+
messageId: completionMessageId,
5783+
text: "Delegated task finished",
5784+
attachments: [],
5785+
dispatchMode: { type: "queue_after_active" },
5786+
createdBy: "agent",
5787+
creationSource: "server",
5788+
delegatedCompletion: { parentRunId: active.id, generation: 1, taskIds: [taskId] },
5789+
});
5790+
yield* orchestrator.dispatch({
5791+
type: "thread.model-selection.set",
5792+
commandId: CommandId.make(`${threadId}:select-newer`),
5793+
threadId,
5794+
modelSelection: newerSelection,
5795+
});
5796+
const waiting = yield* orchestrator.getThreadProjection(threadId);
5797+
yield* eventSink.write({
5798+
events: [
5799+
{
5800+
id: EventId.make(`${threadId}:completed`),
5801+
type: "run.updated",
5802+
threadId,
5803+
runId: active.id,
5804+
occurredAt: now,
5805+
payload: {
5806+
...waiting.runs.find((run) => run.id === active.id)!,
5807+
status: "completed",
5808+
completedAt: now,
5809+
},
5810+
},
5811+
],
5812+
});
5813+
yield* orchestrator.resumeQueuedRuns;
5814+
const after = yield* orchestrator.getThreadProjection(threadId);
5815+
const delivered = after.runs.find((run) => run.userMessageId === completionMessageId)!;
5816+
assert.equal(delivered.status, "starting");
5817+
assert.deepEqual(delivered.modelSelection, newerSelection);
5818+
assert.equal(
5819+
after.attempts.find((attempt) => attempt.id === delivered.activeAttemptId)
5820+
?.providerInstanceId,
5821+
newerSelection.instanceId,
5822+
);
5823+
assert.deepEqual(after.thread.modelSelection, newerSelection);
5824+
}),
5825+
);
5826+
57175827
it.effect("keeps the queue after a user interrupts the active run", () =>
57185828
Effect.gen(function* () {
57195829
const orchestrator = yield* Orchestrator.OrchestratorV2;

0 commit comments

Comments
 (0)