@devries-cameron20 / t3code-1 / commits / 4168c14c7d
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): a queued message that names no model keeps the model picked ahead of it (#33591)

Julius Marminge committed Nov 9, 2026 at 23:59 UTC 4168c14c7db15702bc3745a36b9cd3b9c366630a
4 files changed +314 -6
apps/server/src/orchestration-v2/NotificationMailbox.ts
+4 -1
@@ -6,7 +6,10 @@ import {
6 } from "@t3tools/contracts";
7
8 export function isBufferedProviderNotification(
9 - message: OrchestrationV2ConversationMessage,
9 + message: Pick<
10 + OrchestrationV2ConversationMessage,
11 + "createdBy" | "creationSource" | "notification" | "delegatedCompletion"
12 + >,
13 ): boolean {
14 return (
15 message.createdBy === "agent" &&
apps/server/src/orchestration-v2/Orchestrator.ts
+36 -3
@@ -833,7 +833,12 @@ function nextQueuedRun(
833 }
834
835 function followsCurrentConversation(
836 - message: OrchestrationV2ConversationMessage | undefined,
836 + message:
837 + | Pick<
838 + OrchestrationV2ConversationMessage,
839 + "createdBy" | "creationSource" | "notification" | "delegatedCompletion"
840 + >
841 + | undefined,
842 ): boolean {
843 return (
844 message?.delegatedCompletion !== undefined ||
@@ -851,6 +856,20 @@ function queuedRunModelSelection(
856 : run.modelSelection;
857 }
858
859 +/** The selection the thread will be on once every queued run has started. */
860 +function queueTailModelSelection(
861 + projection: Pick<OrchestrationV2ThreadProjection, "thread" | "runs" | "messages">,
862 +): ModelSelection {
863 + return (
864 + queuedRunsInDeliveryOrder(projection).findLast(
865 + (run) =>
866 + !followsCurrentConversation(
867 + projection.messages.find((message) => message.id === run.userMessageId),
868 + ),
869 + )?.modelSelection ?? projection.thread.modelSelection
870 + );
871 +}
872 +
873 function usageLimitHoldsQueue(
874 projection: Pick<OrchestrationV2ThreadProjection, "runs" | "turnItems" | "providerSessions">,
875 providerInstanceId: ModelSelection["instanceId"],
@@ -5932,7 +5951,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
5951 ]);
5952 projection = applyToProjection(projection, event);
5953 }
5935 - const modelSelection = command.modelSelection ?? projection.thread.modelSelection;
5954 + let modelSelection = command.modelSelection ?? projection.thread.modelSelection;
5955 let dispatchMode = resolveMessageDispatchIntent(
5956 projection,
5957 command.dispatchMode,
@@ -5984,7 +6003,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
6003 : parsedGoalCommand;
6004 let goalStopRunId: RunId | undefined;
6005 if (goalCommand !== null) {
5987 - yield* ensureGoalCommandSupported(command, command, modelSelection.instanceId);
6006 // A goal command never joins a running turn. One that starts or ends goal
6007 // work stops the run and goes first in the queue; the rest wait. A send
6008 // that asked to be queued (no delivery intent) only queues, and a run
@@ -6168,6 +6186,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
6186 });
6187
6188 if (dispatchMode.type === "steer_active" || dispatchMode.type === "restart_active") {
6189 + yield* ensureGoalCommandSupported(command, command, modelSelection.instanceId);
6190 yield* dispatchSteerIntoRun({
6191 command,
6192 events,
@@ -6220,6 +6239,20 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
6239 dispatchMode.type === "start_immediately" ||
6240 dispatchMode.type === "queue_after_active";
6241 const shouldQueue = queueBehindRun !== undefined && startsOrQueuesRun;
6242 + // An explicit pick queued ahead reaches the thread only when its run starts. A goal
6243 + // command that stops the active run goes first, so nothing queued applies before it.
6244 + // Buffered provider wakes drain output their own provider already produced.
6245 + const incomingMessage = { ...command, delegatedCompletion };
6246 + if (
6247 + shouldQueue &&
6248 + command.modelSelection === undefined &&
6249 + goalStopRunId === undefined &&
6250 + !followsCurrentConversation(incomingMessage) &&
6251 + !isBufferedProviderNotification(incomingMessage)
6252 + ) {
6253 + modelSelection = queueTailModelSelection(projection);
6254 + }
6255 + yield* ensureGoalCommandSupported(command, command, modelSelection.instanceId);
6256 // Client messages resume held queues; server continuations and scheduled tasks do not.
6257 // Provider failure or usage limits re-hold the queue.
6258 const worktreeSetupUnresolved = worktreeSetupPending(projection);
apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts
+40
@@ -1606,6 +1606,46 @@ it.effect("stops the running turn and starts a goal sent into it next", () =>
1606 ),
1607 );
1608
1609 +it.effect("starts a goal that names no model on the thread's model ahead of a queued pick", () =>
1610 + Effect.scoped(
1611 + Effect.gen(function* () {
1612 + const { started, layer, startFirstTurn, read } = yield* goalHarness("goal-ahead-of-pick");
1613 + yield* Effect.gen(function* () {
1614 + const orchestrator = yield* Orchestrator.OrchestratorV2;
1615 + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1616 + const threadId = yield* startFirstTurn;
1617 + for (const [id, text, modelSelection] of [
1618 + ["picked", "picked", composerSelection],
1619 + ["goal", "/goal all tests pass", undefined],
1620 + ] as const) {
1621 + yield* orchestrator.dispatch({
1622 + type: "message.dispatch",
1623 + commandId: CommandId.make(id),
1624 + threadId,
1625 + messageId: MessageId.make(`message:${id}`),
1626 + text,
1627 + attachments: [],
1628 + ...(modelSelection === undefined ? {} : { modelSelection }),
1629 + dispatchMode:
1630 + id === "picked" ? { type: "queue_after_active" } : { type: "start_immediately" },
1631 + ...(id === "picked" ? {} : { deliveryIntent: "auto" as const }),
1632 + createdBy: "user",
1633 + creationSource: "web",
1634 + });
1635 + }
1636 + yield* worker.drain();
1637 +
1638 + assert.deepEqual(yield* read(threadId), {
1639 + queue: ["picked"],
1640 + started: ["first", "/goal all tests pass"],
1641 + stopRequested: true,
1642 + });
1643 + assert.deepEqual(started[1]?.modelSelection, runSelection);
1644 + }).pipe(Effect.provide(layer));
1645 + }),
1646 + ),
1647 +);
1648 +
1649 it.effect("queues a goal clear without stopping work when no goal drives it", () =>
1650 Effect.scoped(
1651 Effect.gen(function* () {
apps/server/src/orchestration-v2/runtimeLayer.test.ts
+234 -2
@@ -162,17 +162,35 @@ const alternateProviderInstance = {
162 instanceId: alternateInstanceId,
163 },
164 } satisfies ProviderInstance;
165 +const claudeInstanceId = ProviderInstanceId.make("claude_test");
166 +const claudeDriver = ProviderDriverKind.make("claudeAgent");
167 +const claudeProviderInstance = {
168 + ...providerInstance,
169 + instanceId: claudeInstanceId,
170 + driverKind: claudeDriver,
171 + continuationIdentity: { driverKind: claudeDriver, continuationKey: "claude:test" },
172 + displayName: "Claude test",
173 + orchestrationAdapter: {
174 + ...orchestrationAdapter,
175 + instanceId: claudeInstanceId,
176 + driver: claudeDriver,
177 + },
178 +} satisfies ProviderInstance;
179
180 const layerTestProviderInstanceRegistry = Layer.succeed(
181 ProviderInstanceRegistry.ProviderInstanceRegistry,
182 {
183 getInstance: (instanceId) =>
184 Effect.succeed(
171 - [providerInstance, alternateProviderInstance].find(
185 + [providerInstance, alternateProviderInstance, claudeProviderInstance].find(
186 (instance) => instanceId === instance.instanceId,
187 ),
188 ),
175 - listInstances: Effect.succeed([providerInstance, alternateProviderInstance]),
189 + listInstances: Effect.succeed([
190 + providerInstance,
191 + alternateProviderInstance,
192 + claudeProviderInstance,
193 + ]),
194 listUnavailable: Effect.succeed([]),
195 streamChanges: Stream.empty,
196 subscribeChanges: Effect.never,
@@ -6157,6 +6175,220 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
6175 }),
6176 );
6177
6178 + it.effect("keeps a model picked ahead of a queued agent message that names none", () =>
6179 + Effect.gen(function* () {
6180 + const orchestrator = yield* Orchestrator.OrchestratorV2;
6181 + const eventSink = yield* EventSink.EventSinkV2;
6182 + const threadId = ThreadId.make("runtime-layer-queued-agent-message-model-pick");
6183 + const pickedSelection = { ...modelSelection, model: "gpt-5.5" };
6184 + yield* orchestrator.dispatch({
6185 + type: "thread.create",
6186 + commandId: CommandId.make(`${threadId}:create`),
6187 + threadId,
6188 + projectId: ProjectId.make(`${threadId}:project`),
6189 + createdBy: "user",
6190 + creationSource: "web",
6191 + title: "Queued model pick",
6192 + modelSelection,
6193 + runtimeMode: "full-access",
6194 + interactionMode: "default",
6195 + branch: null,
6196 + worktreePath: process.cwd(),
6197 + });
6198 + for (const [key, selection] of [
6199 + ["active", modelSelection],
6200 + ["picked", pickedSelection],
6201 + ] as const) {
6202 + yield* orchestrator.dispatch({
6203 + type: "message.dispatch",
6204 + commandId: CommandId.make(`${threadId}:${key}`),
6205 + threadId,
6206 + messageId: MessageId.make(`${threadId}:${key}`),
6207 + text: key,
6208 + attachments: [],
6209 + modelSelection: selection,
6210 + dispatchMode: { type: key === "active" ? "start_immediately" : "queue_after_active" },
6211 + createdBy: "user",
6212 + creationSource: "web",
6213 + });
6214 + }
6215 + // What t3_thread_send dispatches: no model of its own.
6216 + yield* orchestrator.dispatch({
6217 + type: "message.dispatch",
6218 + commandId: CommandId.make(`${threadId}:agent`),
6219 + threadId,
6220 + messageId: MessageId.make(`${threadId}:agent`),
6221 + text: "Message from another thread",
6222 + attachments: [],
6223 + dispatchMode: { type: "queue_after_active" },
6224 + createdBy: "agent",
6225 + creationSource: "mcp",
6226 + });
6227 + const [active, picked, agent] = (yield* orchestrator.getThreadProjection(threadId)).runs;
6228 + assert.isDefined(active);
6229 + assert.isDefined(picked);
6230 + assert.isDefined(agent);
6231 + const now = yield* DateTime.now;
6232 + for (const runId of [active.id, picked.id]) {
6233 + const current = yield* orchestrator.getThreadProjection(threadId);
6234 + yield* eventSink.write({
6235 + events: [
6236 + {
6237 + id: EventId.make(`${runId}:completed`),
6238 + type: "run.updated",
6239 + threadId,
6240 + runId,
6241 + occurredAt: now,
6242 + payload: {
6243 + ...current.runs.find((run) => run.id === runId)!,
6244 + status: "completed",
6245 + completedAt: now,
6246 + },
6247 + },
6248 + ],
6249 + });
6250 + yield* orchestrator.resumeQueuedRuns;
6251 + }
6252 + const after = yield* orchestrator.getThreadProjection(threadId);
6253 + const delivered = after.runs.find((run) => run.id === agent.id)!;
6254 + assert.equal(delivered.status, "starting");
6255 + assert.deepEqual(delivered.modelSelection, pickedSelection);
6256 + assert.deepEqual(after.thread.modelSelection, pickedSelection);
6257 + }),
6258 + );
6259 +
6260 + it.effect("keeps a buffered provider wake on its own provider behind a queued pick", () =>
6261 + Effect.gen(function* () {
6262 + const orchestrator = yield* Orchestrator.OrchestratorV2;
6263 + const threadId = ThreadId.make("runtime-layer-buffered-wake-behind-queued-pick");
6264 + yield* orchestrator.dispatch({
6265 + type: "thread.create",
6266 + commandId: CommandId.make(`${threadId}:create`),
6267 + threadId,
6268 + projectId: ProjectId.make(`${threadId}:project`),
6269 + createdBy: "user",
6270 + creationSource: "web",
6271 + title: "Buffered wake behind a pick",
6272 + modelSelection,
6273 + runtimeMode: "full-access",
6274 + interactionMode: "default",
6275 + branch: null,
6276 + worktreePath: process.cwd(),
6277 + });
6278 + for (const [key, selection] of [
6279 + ["active", modelSelection],
6280 + ["picked", { ...modelSelection, instanceId: alternateInstanceId }],
6281 + ] as const) {
6282 + yield* orchestrator.dispatch({
6283 + type: "message.dispatch",
6284 + commandId: CommandId.make(`${threadId}:${key}`),
6285 + threadId,
6286 + messageId: MessageId.make(`${threadId}:${key}`),
6287 + text: key,
6288 + attachments: [],
6289 + modelSelection: selection,
6290 + dispatchMode: { type: key === "active" ? "start_immediately" : "queue_after_active" },
6291 + createdBy: "user",
6292 + creationSource: "web",
6293 + });
6294 + }
6295 + yield* orchestrator.dispatch({
6296 + type: "message.dispatch",
6297 + commandId: CommandId.make(`${threadId}:wake`),
6298 + threadId,
6299 + messageId: MessageId.make(`${threadId}:wake`),
6300 + text: "Background activity updated",
6301 + attachments: [],
6302 + dispatchMode: { type: "queue_after_active" },
6303 + createdBy: "agent",
6304 + creationSource: "provider",
6305 + notification: {
6306 + source: { kind: "background_task" },
6307 + outcome: "updated",
6308 + summary: "Background activity updated",
6309 + },
6310 + });
6311 + const projection = yield* orchestrator.getThreadProjection(threadId);
6312 + const active = projection.runs.find(
6313 + (run) => run.userMessageId === MessageId.make(`${threadId}:active`),
6314 + );
6315 + const wake = projection.runs.find(
6316 + (run) => run.userMessageId === MessageId.make(`${threadId}:wake`),
6317 + );
6318 + assert.deepEqual(wake?.modelSelection, modelSelection);
6319 + assert.equal(wake?.providerThreadId, active?.providerThreadId);
6320 + }),
6321 + );
6322 +
6323 + it.effect("checks a queued goal pause against the model picked ahead of it", () =>
6324 + Effect.gen(function* () {
6325 + const orchestrator = yield* Orchestrator.OrchestratorV2;
6326 + const threadId = ThreadId.make("runtime-layer-goal-pause-behind-queued-pick");
6327 + const claudeSelection = { ...modelSelection, instanceId: claudeInstanceId };
6328 + yield* orchestrator.dispatch({
6329 + type: "thread.create",
6330 + commandId: CommandId.make(`${threadId}:create`),
6331 + threadId,
6332 + projectId: ProjectId.make(`${threadId}:project`),
6333 + createdBy: "user",
6334 + creationSource: "web",
6335 + title: "Goal pause behind a pick",
6336 + modelSelection: claudeSelection,
6337 + runtimeMode: "full-access",
6338 + interactionMode: "default",
6339 + branch: null,
6340 + worktreePath: process.cwd(),
6341 + });
6342 + for (const [key, selection] of [
6343 + ["active", claudeSelection],
6344 + ["picked", modelSelection],
6345 + ] as const) {
6346 + yield* orchestrator.dispatch({
6347 + type: "message.dispatch",
6348 + commandId: CommandId.make(`${threadId}:${key}`),
6349 + threadId,
6350 + messageId: MessageId.make(`${threadId}:${key}`),
6351 + text: key,
6352 + attachments: [],
6353 + modelSelection: selection,
6354 + dispatchMode: { type: key === "active" ? "start_immediately" : "queue_after_active" },
6355 + createdBy: "user",
6356 + creationSource: "web",
6357 + });
6358 + }
6359 + const sendGoal = (key: string, text: string, deliveryIntent?: "auto") =>
6360 + orchestrator.dispatch({
6361 + type: "message.dispatch",
6362 + commandId: CommandId.make(`${threadId}:${key}`),
6363 + threadId,
6364 + messageId: MessageId.make(`${threadId}:${key}`),
6365 + text,
6366 + attachments: [],
6367 + dispatchMode: { type: "queue_after_active" },
6368 + ...(deliveryIntent === undefined ? {} : { deliveryIntent }),
6369 + createdBy: "agent",
6370 + creationSource: "mcp",
6371 + });
6372 + // It runs after the pick, on a provider that can pause goals.
6373 + yield* sendGoal("pause", "/goal pause");
6374 + // A resume stops the active run and starts before the pick, so the thread's
6375 + // Claude instance still refuses it.
6376 + const sequence = yield* orchestrator.getThreadEventSequence(threadId);
6377 + const refused = yield* Effect.flip(sendGoal("resume", "/goal resume", "auto"));
6378 + assert.instanceOf(refused, Orchestrator.OrchestratorDispatchError);
6379 + assert.equal(
6380 + refused.cause,
6381 + "Claude goals cannot be paused or resumed. Send /goal clear to remove the goal.",
6382 + );
6383 + assert.equal(yield* orchestrator.getThreadEventSequence(threadId), sequence);
6384 +
6385 + const pause = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
6386 + (run) => run.userMessageId === MessageId.make(`${threadId}:pause`),
6387 + );
6388 + assert.deepEqual(pause?.modelSelection, modelSelection);
6389 + }),
6390 + );
6391 +
6392 it.effect("keeps the queue after a user interrupts the active run", () =>
6393 Effect.gen(function* () {
6394 const orchestrator = yield* Orchestrator.OrchestratorV2;