Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,7 @@ function queueTailModelSelection(
return (
queuedRunsInDeliveryOrder(projection).findLast(
(run) =>
run.threadPickSuperseded !== true &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High orchestration-v2/Orchestrator.ts:1033

A superseded pinned scheduled run is omitted from the queue-tail selection, so a later model-less message is predicted to use B even though startNextQueuedRun lets that scheduled run write A back to the thread. The later message therefore runs on B instead of inheriting the actual queue-tail selection; preserve the pinned-scheduled-run exception here.

🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
In file @apps/server/src/orchestration-v2/Orchestrator.ts around line 1033:

A superseded pinned scheduled run is omitted from the queue-tail selection, so a later model-less message is predicted to use `B` even though `startNextQueuedRun` lets that scheduled run write `A` back to the thread. The later message therefore runs on `B` instead of inheriting the actual queue-tail selection; preserve the pinned-scheduled-run exception here.

!followsCurrentConversation(
projection.messages.find((message) => message.id === run.userMessageId),
),
Expand Down Expand Up @@ -2658,9 +2659,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
projection.thread.modelSelection,
queuedRun.modelSelection,
);
// The picker may already have moved ahead of the native conversation.
// The picker, or a superseded queued start, may have left the thread ahead of the native conversation.
const switchFromActiveProvider =
followsCurrent &&
activeProviderThread !== undefined &&
activeProviderThread.providerInstanceId !== queuedRun.providerInstanceId;
const switchPlan =
Expand Down Expand Up @@ -3111,7 +3111,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
: "Provider or model selection changed.";
yield* writeSystemEvents(
[
...(selectionChanged
// A scheduled task's pinned model still moves the thread back when it starts.
...(selectionChanged &&
(queuedRun.threadPickSuperseded !== true ||
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
(queuedMessage.scheduledTaskId !== undefined &&
queuedMessage.followsThreadSelection !== true))
? [
{
type:
Expand Down Expand Up @@ -5269,6 +5273,25 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
occurredAt: now,
payload: updatedThread,
});
if (command.type === "thread.model-selection.set" || command.type === "provider.switch") {
const { runs } = yield* loadProjectionForCommand(command, ["runs"], {
runStatuses: ["queued"],
});
for (const run of runs) {
if (run.threadPickSuperseded === true) continue;
yield* emit(
events,
command,
)({
type: "run.updated",
threadId: command.threadId,
runId: run.id,
providerInstanceId: run.providerInstanceId,
occurredAt: now,
payload: { ...run, threadPickSuperseded: true },
});
}
}
// Archiving ends the guest share, so its waiting files go too.
yield* releaseGuestAttachments(command, thread.guestShare, updatedThread.guestShare, effects);

Expand Down
77 changes: 77 additions & 0 deletions apps/server/src/orchestration-v2/runtimeLayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8998,6 +8998,83 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
}),
);

it.effect("keeps a model picked after a message was queued when that message starts", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
const eventSink = yield* EventSink.EventSinkV2;
const threadId = ThreadId.make("runtime-layer-pick-after-queued-message");
const pickedSelection = { ...modelSelection, model: "gpt-5.5" };
yield* orchestrator.dispatch({
type: "thread.create",
commandId: CommandId.make(`${threadId}:create`),
threadId,
projectId: ProjectId.make(`${threadId}:project`),
createdBy: "user",
creationSource: "web",
title: "Pick after a queued message",
modelSelection,
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: process.cwd(),
});
for (const key of ["active", "queued"] as const) {
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make(`${threadId}:${key}`),
threadId,
messageId: MessageId.make(`${threadId}:${key}`),
text: key,
attachments: [],
modelSelection,
dispatchMode: { type: key === "active" ? "start_immediately" : "queue_after_active" },
createdBy: "user",
creationSource: "web",
});
}
// The web picker persists a same-instance pick at once (useApplyThreadModelPick).
yield* orchestrator.dispatch({
type: "thread.model-selection.set",
commandId: CommandId.make(`${threadId}:pick`),
threadId,
modelSelection: pickedSelection,
});
// A message queued after the pick without its own model takes the pick, not the older queue tail.
yield* orchestrator.dispatch({
type: "message.dispatch",
commandId: CommandId.make(`${threadId}:implicit`),
threadId,
messageId: MessageId.make(`${threadId}:implicit`),
text: "implicit",
attachments: [],
dispatchMode: { type: "queue_after_active" },
createdBy: "user",
creationSource: "web",
});
const [active, queued, implicit] = (yield* orchestrator.getThreadProjection(threadId)).runs;
assert.isDefined(active);
assert.isDefined(queued);
assert.deepEqual(implicit?.modelSelection, pickedSelection);
const now = yield* DateTime.now;
yield* eventSink.write({
events: [
{
id: EventId.make(`${active.id}:completed`),
type: "run.updated",
threadId,
runId: active.id,
occurredAt: now,
payload: { ...active, status: "completed", completedAt: now },
},
],
});
yield* orchestrator.resumeQueuedRuns;
const after = yield* orchestrator.getThreadProjection(threadId);
assert.equal(after.runs.find((run) => run.id === queued.id)?.status, "starting");
assert.deepEqual(after.thread.modelSelection, pickedSelection);
}),
);

it.effect("keeps a buffered provider wake on its own provider behind a queued pick", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
Expand Down
Loading
Loading