From 5f48beabbbbb6b562a36dbfed9fa8f553b222487 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 12 Nov 2026 06:53:47 +0000 Subject: [PATCH 1/7] test(server): a queued message's start keeps a model picked after it was queued Reproduces #30547: the web picker saves a same-instance pick to the thread at once, and starting an earlier-queued message writes that message's model back over the newer pick. AI-assisted; verified with the tests and tools listed above. --- .../src/orchestration-v2/runtimeLayer.test.ts | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts index 78c9b5996dcde..1846cf071ad89 100644 --- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts +++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts @@ -7544,6 +7544,70 @@ 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, + }); + const [active, queued] = (yield* orchestrator.getThreadProjection(threadId)).runs; + assert.isDefined(active); + assert.isDefined(queued); + 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; From c34be8a964d3d1cff81e4a2874a369b8df258f71 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 05:55:22 +0000 Subject: [PATCH 2/7] fix(server): a queued message's start no longer undoes a newer model pick A message queued before a model pick still runs on its queue-time model, but when it started the server wrote that model back onto the thread, overwriting the pick the user made while it waited (the web picker saves same-instance picks to the thread at once). A thread pick now marks the runs already queued, and starting a marked run leaves the thread's model selection alone. Fixes pingdotgg/t3code#30547 Co-Authored-By: Claude Opus 5.5 --- .../src/orchestration-v2/Orchestrator.ts | 19 ++++++++++++++++++- packages/contracts/src/orchestrationV2.ts | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index d3475403196b8..05091f571c8ed 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -2747,7 +2747,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio : "Provider or model selection changed."; yield* writeSystemEvents( [ - ...(selectionChanged + ...(selectionChanged && queuedRun.threadPickSuperseded !== true ? [ { type: @@ -4712,6 +4712,23 @@ 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"], { turnItemTypes: [] }); + for (const run of runs) { + if (run.status !== "queued" || 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); diff --git a/packages/contracts/src/orchestrationV2.ts b/packages/contracts/src/orchestrationV2.ts index b3a2a0f1ca29d..ec9284a06aa03 100644 --- a/packages/contracts/src/orchestrationV2.ts +++ b/packages/contracts/src/orchestrationV2.ts @@ -708,6 +708,8 @@ export const OrchestrationV2Run = Schema.Struct({ status: OrchestrationV2RunStatus, queuePosition: Schema.optional(Schema.NullOr(PositiveInt)), queueHeld: Schema.optional(Schema.Boolean), + /** A thread model pick landed while this run was queued, so its start keeps that pick. */ + threadPickSuperseded: Schema.optional(Schema.Boolean), steerWhenReadyRunId: Schema.optional(RunId), requestedAt: Schema.DateTimeUtc, startedAt: Schema.NullOr(Schema.DateTimeUtc), From afc60d3d0357401498f929e6a1a1a10d88e39cc3 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 06:25:22 +0000 Subject: [PATCH 3/7] fix(server): a pinned scheduled delivery still moves the thread to its model on start Co-Authored-By: Claude Opus 5.5 --- apps/server/src/orchestration-v2/Orchestrator.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 05091f571c8ed..8f281f315093c 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -2747,7 +2747,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio : "Provider or model selection changed."; yield* writeSystemEvents( [ - ...(selectionChanged && queuedRun.threadPickSuperseded !== true + // A scheduled task's pinned model still moves the thread back when it starts. + ...(selectionChanged && + (queuedRun.threadPickSuperseded !== true || queuedMessage.scheduledTaskId !== undefined) ? [ { type: From cde22a4b7a2f1ffad7a7f1a778f0a3669c4b1b85 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 06:54:01 +0000 Subject: [PATCH 4/7] fix(server): a message queued after a pick takes the pick, not an older queued model Co-Authored-By: Claude Opus 5.5 --- apps/server/src/orchestration-v2/Orchestrator.ts | 1 + .../src/orchestration-v2/runtimeLayer.test.ts | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 8f281f315093c..0604c48ac8dcc 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -906,6 +906,7 @@ function queueTailModelSelection( return ( queuedRunsInDeliveryOrder(projection).findLast( (run) => + run.threadPickSuperseded !== true && !followsCurrentConversation( projection.messages.find((message) => message.id === run.userMessageId), ), diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts index 1846cf071ad89..125c3117304e8 100644 --- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts +++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts @@ -7585,9 +7585,22 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { threadId, modelSelection: pickedSelection, }); - const [active, queued] = (yield* orchestrator.getThreadProjection(threadId)).runs; + // 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: [ From 02dd6373c95c9d70c3be1dc44e318905b0fc63cc Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 07:09:59 +0000 Subject: [PATCH 5/7] fix(server): only a pinned scheduled delivery moves the thread back to its model Co-Authored-By: Claude Opus 5.5 --- apps/server/src/orchestration-v2/Orchestrator.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 05259a0535a49..d961169fb904b 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -3114,7 +3114,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio [ // A scheduled task's pinned model still moves the thread back when it starts. ...(selectionChanged && - (queuedRun.threadPickSuperseded !== true || queuedMessage.scheduledTaskId !== undefined) + (queuedRun.threadPickSuperseded !== true || + (queuedMessage.scheduledTaskId !== undefined && + queuedMessage.followsThreadSelection !== true)) ? [ { type: From 5b29f7509a92084b8a03a288047f6399d06f3560 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 15:53:27 +0000 Subject: [PATCH 6/7] perf(server): a model pick reads only the thread's queued runs Co-Authored-By: Claude Opus 5.5 --- apps/server/src/orchestration-v2/Orchestrator.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index d961169fb904b..aaebfa43fe247 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -5275,9 +5275,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio payload: updatedThread, }); if (command.type === "thread.model-selection.set" || command.type === "provider.switch") { - const { runs } = yield* loadProjectionForCommand(command, ["runs"], { turnItemTypes: [] }); + const { runs } = yield* loadProjectionForCommand(command, ["runs"], { + runStatuses: ["queued"], + }); for (const run of runs) { - if (run.status !== "queued" || run.threadPickSuperseded === true) continue; + if (run.threadPickSuperseded === true) continue; yield* emit( events, command, From 5c83435840c4c1a50fe7f307973685dc7aa2cfb1 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Mon, 16 Nov 2026 16:07:45 +0000 Subject: [PATCH 7/7] fix(server): a queued start after a superseded one resumes the active account's native thread A queued run whose start keeps a newer pick leaves the thread selection ahead of the native conversation, so the next queued start now compares against the active provider thread for every message, as an immediate start already does. Co-Authored-By: Claude Opus 5.5 --- .../src/orchestration-v2/Orchestrator.ts | 3 +- .../ProviderSwitch.integration.test.ts | 396 ++++++++++-------- 2 files changed, 224 insertions(+), 175 deletions(-) diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index aaebfa43fe247..37e71cea00d4e 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -2659,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 = diff --git a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts index edc12dcd98840..9f55c03d0c367 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts @@ -2373,185 +2373,235 @@ describe("orchestration v2 provider switching", () => { }), ); - it.live("resumes a queued account switch without requiring a portable handoff", () => - Effect.gen(function* () { - const cwd = yield* checkpointWorkspace("queued-account-switch"); - const capturedTurns = yield* Ref.make>([]); - const started = yield* Deferred.make(); - const alternateSelection: ModelSelection = { - ...CODEX_MODEL_SELECTION, - instanceId: ProviderInstanceId.make("codex-alternate"), - }; - const alternateCapabilities = { - ...CodexProviderCapabilitiesV2, - canConsumeHandoffSummaries: false, - }; - const adapters = [ - makeTestAdapter({ - instanceId: CODEX_MODEL_SELECTION.instanceId, - driver: CODEX_DRIVER, - capabilities: CodexProviderCapabilitiesV2, - modelSelection: CODEX_MODEL_SELECTION, - responseByRunOrdinal: {}, - capturedTurns, - holdFirstTurn: started, - }), - makeTestAdapter({ - instanceId: alternateSelection.instanceId, - driver: CODEX_DRIVER, - capabilities: alternateCapabilities, - modelSelection: alternateSelection, - responseByRunOrdinal: { 2: "Alternate account complete" }, - capturedTurns, - }), - ]; - const layerRegistry = Layer.succeed( - ProviderAdapterRegistry.ProviderAdapterRegistryV2, - ProviderAdapterRegistry.ProviderAdapterRegistryV2.of({ - get: (instanceId) => { - const adapter = adapters.find((candidate) => candidate.instanceId === instanceId); - return adapter === undefined - ? Effect.fail( - new ProviderAdapterRegistry.ProviderAdapterRegistryLookupError({ instanceId }), - ) - : Effect.succeed(adapter); - }, - list: () => Effect.succeed(adapters.map((adapter) => adapter.instanceId)), - getMetadata: (instanceId) => { - const adapter = adapters.find((candidate) => candidate.instanceId === instanceId); - return adapter === undefined - ? Effect.fail( - new ProviderAdapterRegistry.ProviderAdapterRegistryLookupError({ instanceId }), - ) - : Effect.succeed({ - driver: CODEX_DRIVER, - continuationKey: "codex:shared-native-account-history", - enabled: true, - capabilities: - instanceId === alternateSelection.instanceId - ? alternateCapabilities - : CodexProviderCapabilitiesV2, - }); - }, - }), - ); - const queuedThreadId = ThreadId.make("thread:queued-account-switch"); - const projection = yield* Effect.gen(function* () { - const orchestrator = yield* Orchestrator.OrchestratorV2; - const eventSink = yield* EventSink.EventSinkV2; - const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; - yield* orchestrator.dispatch({ - type: "thread.create", - createdBy: "user", - creationSource: "web", - commandId: CommandId.make("command:queued-account-switch:create"), - threadId: queuedThreadId, - projectId: ProjectId.make("project:queued-account-switch"), - title: "Queued account switch", - modelSelection: CODEX_MODEL_SELECTION, - runtimeMode: "full-access", - interactionMode: "default", - branch: null, - worktreePath: cwd, - }); - yield* orchestrator.dispatch({ - type: "message.dispatch", - createdBy: "user", - creationSource: "web", - commandId: CommandId.make("command:queued-account-switch:first"), - threadId: queuedThreadId, - messageId: MessageId.make("message:queued-account-switch:first"), - text: "First account turn", - attachments: [], - modelSelection: CODEX_MODEL_SELECTION, - dispatchMode: { type: "start_immediately" }, - }); - yield* Deferred.await(started); - yield* orchestrator.dispatch({ - type: "message.dispatch", - createdBy: "user", - creationSource: "web", - commandId: CommandId.make("command:queued-account-switch:second"), - threadId: queuedThreadId, - messageId: MessageId.make("message:queued-account-switch:second"), - text: "Alternate account turn", - attachments: [], - modelSelection: alternateSelection, - dispatchMode: { type: "queue_after_active" }, - }); - const queued = yield* orchestrator.getThreadProjection(queuedThreadId); - assert.deepEqual( - queued.runs.map((run) => run.status), - ["running", "queued"], + it.live.each(["behind a running turn", "after a turn queued before a newer pick"] as const)( + "resumes a queued account switch without requiring a portable handoff %s", + (scenario) => + Effect.gen(function* () { + const supersededPick = scenario !== "behind a running turn"; + const cwd = yield* checkpointWorkspace("queued-account-switch"); + const capturedTurns = yield* Ref.make>([]); + const started = yield* Deferred.make(); + const alternateSelection: ModelSelection = { + ...CODEX_MODEL_SELECTION, + instanceId: ProviderInstanceId.make("codex-alternate"), + }; + const alternateCapabilities = { + ...CodexProviderCapabilitiesV2, + canConsumeHandoffSummaries: false, + }; + const adapters = [ + makeTestAdapter({ + instanceId: CODEX_MODEL_SELECTION.instanceId, + driver: CODEX_DRIVER, + capabilities: CodexProviderCapabilitiesV2, + modelSelection: CODEX_MODEL_SELECTION, + responseByRunOrdinal: {}, + capturedTurns, + holdFirstTurn: started, + }), + makeTestAdapter({ + instanceId: alternateSelection.instanceId, + driver: CODEX_DRIVER, + capabilities: alternateCapabilities, + modelSelection: alternateSelection, + responseByRunOrdinal: { 2: "Alternate account complete" }, + capturedTurns, + }), + ]; + const layerRegistry = Layer.succeed( + ProviderAdapterRegistry.ProviderAdapterRegistryV2, + ProviderAdapterRegistry.ProviderAdapterRegistryV2.of({ + get: (instanceId) => { + const adapter = adapters.find((candidate) => candidate.instanceId === instanceId); + return adapter === undefined + ? Effect.fail( + new ProviderAdapterRegistry.ProviderAdapterRegistryLookupError({ instanceId }), + ) + : Effect.succeed(adapter); + }, + list: () => Effect.succeed(adapters.map((adapter) => adapter.instanceId)), + getMetadata: (instanceId) => { + const adapter = adapters.find((candidate) => candidate.instanceId === instanceId); + return adapter === undefined + ? Effect.fail( + new ProviderAdapterRegistry.ProviderAdapterRegistryLookupError({ instanceId }), + ) + : Effect.succeed({ + driver: CODEX_DRIVER, + continuationKey: "codex:shared-native-account-history", + enabled: true, + capabilities: + instanceId === alternateSelection.instanceId + ? alternateCapabilities + : CodexProviderCapabilitiesV2, + }); + }, + }), ); - assert.equal(queued.thread.providerInstanceId, CODEX_MODEL_SELECTION.instanceId); - const sourceNativeRef = queued.providerThreads.find( - (providerThread) => providerThread.id === queued.runs[0]?.providerThreadId, - )?.nativeThreadRef; - assert.isNotNull(sourceNativeRef); - const now = yield* DateTime.now; - yield* eventSink.write({ - events: [ - { - id: EventId.make("event:queued-account-switch:first-complete"), - type: "run.updated", + const queuedThreadId = ThreadId.make("thread:queued-account-switch"); + const projection = yield* Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const eventSink = yield* EventSink.EventSinkV2; + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + yield* orchestrator.dispatch({ + type: "thread.create", + createdBy: "user", + creationSource: "web", + commandId: CommandId.make("command:queued-account-switch:create"), + threadId: queuedThreadId, + projectId: ProjectId.make("project:queued-account-switch"), + title: "Queued account switch", + modelSelection: CODEX_MODEL_SELECTION, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: cwd, + }); + yield* orchestrator.dispatch({ + type: "message.dispatch", + createdBy: "user", + creationSource: "web", + commandId: CommandId.make("command:queued-account-switch:first"), + threadId: queuedThreadId, + messageId: MessageId.make("message:queued-account-switch:first"), + text: "First account turn", + attachments: [], + modelSelection: CODEX_MODEL_SELECTION, + dispatchMode: { type: "start_immediately" }, + }); + yield* Deferred.await(started); + if (supersededPick) { + yield* orchestrator.dispatch({ + type: "message.dispatch", + createdBy: "user", + creationSource: "web", + commandId: CommandId.make("command:queued-account-switch:earlier"), + threadId: queuedThreadId, + messageId: MessageId.make("message:queued-account-switch:earlier"), + text: "Earlier account turn", + attachments: [], + modelSelection: CODEX_MODEL_SELECTION, + dispatchMode: { type: "queue_after_active" }, + }); + } + yield* orchestrator.dispatch({ + type: "message.dispatch", + createdBy: "user", + creationSource: "web", + commandId: CommandId.make("command:queued-account-switch:second"), + threadId: queuedThreadId, + messageId: MessageId.make("message:queued-account-switch:second"), + text: "Alternate account turn", + attachments: [], + modelSelection: alternateSelection, + dispatchMode: { type: "queue_after_active" }, + }); + const queued = yield* orchestrator.getThreadProjection(queuedThreadId); + assert.deepEqual( + queued.runs.map((run) => run.status), + supersededPick ? ["running", "queued", "queued"] : ["running", "queued"], + ); + assert.equal(queued.thread.providerInstanceId, CODEX_MODEL_SELECTION.instanceId); + const sourceNativeRef = queued.providerThreads.find( + (providerThread) => providerThread.id === queued.runs[0]?.providerThreadId, + )?.nativeThreadRef; + assert.isNotNull(sourceNativeRef); + if (supersededPick) { + // A held queue lets the pick land before the earlier Codex turn starts. That start keeps + // the pick, so the thread is on the alternate account while Codex owns the native thread. + yield* orchestrator.dispatch({ + type: "run.interrupt", + commandId: CommandId.make("command:queued-account-switch:interrupt"), threadId: queuedThreadId, runId: queued.runs[0]!.id, - providerInstanceId: CODEX_MODEL_SELECTION.instanceId, - occurredAt: now, - payload: { ...queued.runs[0]!, status: "completed", completedAt: now }, - }, - ], - }); - yield* orchestrator.resumeQueuedRuns; - yield* orchestrator.streamStoredEvents.pipe( - Stream.filter( - (event) => - event.event.type === "run.updated" && - event.event.runId === queued.runs[1]?.id && - event.event.payload.status === "completed", - ), - Stream.runHead, - ); - yield* worker.drain(); - const delivered = yield* orchestrator.getThreadProjection(queuedThreadId); - const targetNativeRef = delivered.providerThreads.find( - (providerThread) => providerThread.id === delivered.runs[1]?.providerThreadId, - )?.nativeThreadRef; - assert.deepEqual(targetNativeRef, sourceNativeRef); - return delivered; - }).pipe( - Effect.provide( - ProviderReplayHarness.layerWithRegistry( - { - name: "queued-account-switch", - runtimePolicyOverride: { - cwd, - approvalPolicy: "never", - sandboxPolicy: { - type: "readOnly", - access: { type: "fullAccess" }, - networkAccess: false, + holdQueue: true, + }); + yield* orchestrator.dispatch({ + type: "thread.model-selection.set", + commandId: CommandId.make("command:queued-account-switch:pick"), + threadId: queuedThreadId, + modelSelection: alternateSelection, + }); + yield* orchestrator.dispatch({ + type: "queue.resume", + commandId: CommandId.make("command:queued-account-switch:resume"), + threadId: queuedThreadId, + }); + } else { + const now = yield* DateTime.now; + yield* eventSink.write({ + events: [ + { + id: EventId.make("event:queued-account-switch:first-complete"), + type: "run.updated", + threadId: queuedThreadId, + runId: queued.runs[0]!.id, + providerInstanceId: CODEX_MODEL_SELECTION.instanceId, + occurredAt: now, + payload: { ...queued.runs[0]!, status: "completed", completedAt: now }, + }, + ], + }); + } + const alternateRun = queued.runs.at(-1); + yield* orchestrator.resumeQueuedRuns; + yield* orchestrator.streamStoredEvents.pipe( + Stream.filter( + (event) => + event.event.type === "run.updated" && + event.event.runId === alternateRun?.id && + event.event.payload.status === "completed", + ), + Stream.runHead, + ); + yield* worker.drain(); + const delivered = yield* orchestrator.getThreadProjection(queuedThreadId); + const targetNativeRef = delivered.providerThreads.find( + (providerThread) => providerThread.id === delivered.runs.at(-1)?.providerThreadId, + )?.nativeThreadRef; + assert.deepEqual(targetNativeRef, sourceNativeRef); + return delivered; + }).pipe( + Effect.provide( + ProviderReplayHarness.layerWithRegistry( + { + name: "queued-account-switch", + runtimePolicyOverride: { + cwd, + approvalPolicy: "never", + sandboxPolicy: { + type: "readOnly", + access: { type: "fullAccess" }, + networkAccess: false, + }, }, }, - }, - layerRegistry, + layerRegistry, + ), ), - ), - ); - assert.deepEqual( - projection.runs.map((run) => [run.providerInstanceId, run.status]), - [ - [CODEX_MODEL_SELECTION.instanceId, "completed"], - [alternateSelection.instanceId, "completed"], - ], - ); - assert.lengthOf(projection.contextHandoffs, 0); - assert.deepEqual( - (yield* Ref.get(capturedTurns)).map((turn) => turn.text), - ["First account turn", "Alternate account turn"], - ); - }), + ); + assert.deepEqual( + projection.runs.map((run) => [run.providerInstanceId, run.status]), + [ + [CODEX_MODEL_SELECTION.instanceId, supersededPick ? "interrupted" : "completed"], + ...(supersededPick ? [[CODEX_MODEL_SELECTION.instanceId, "completed"]] : []), + [alternateSelection.instanceId, "completed"], + ], + ); + // The interrupted turn reaches the earlier Codex turn as a same-thread delta, never the switch. + assert.deepEqual( + projection.contextHandoffs.map((handoff) => handoff.targetRunId), + supersededPick ? [projection.runs[1]!.id] : [], + ); + const turnTexts = (yield* Ref.get(capturedTurns)).map((turn) => turn.text); + assert.equal(turnTexts.at(-1), "Alternate account turn"); + assert.deepEqual( + turnTexts.map((text) => text.split("\n").at(-1)), + supersededPick + ? ["First account turn", "Earlier account turn", "Alternate account turn"] + : ["First account turn", "Alternate account turn"], + ); + }), ); it.live("finishes earlier queued Codex turns before handing context to queued Claude", () =>