diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index ccb72f5fe620b..56c03cbb1551f 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -82,7 +82,7 @@ import { import * as ProviderFailover from "./ProviderFailover.ts"; import * as ProviderCooldown from "../provider/ProviderCooldown.ts"; import * as KeyedLock from "@t3tools/shared/KeyedLock"; -import { modelSelectionsEqual } from "@t3tools/shared/model"; +import { draftModelPickMatchesThread, modelSelectionsEqual } from "@t3tools/shared/model"; import { modelSwitchSince } from "./modelSwitch.ts"; import { combinePullRequestWatchWakes, type PullRequestWatchWake } from "./pullRequestWatch.ts"; import { recordThreadOpen } from "@t3tools/shared/paletteSearch"; @@ -1050,7 +1050,11 @@ function threadAtRunStart( ): OrchestrationV2ThreadProjection["thread"] { const scheduled = projection.thread.scheduledModelSelection; if (scheduled == null || starting.model === "server") return projection.thread; - const cleared = { ...projection.thread, scheduledModelSelection: null }; + const cleared = { + ...projection.thread, + scheduledModelSelection: null, + bypassProviderCooldown: false, + }; if (starting.model === "named") { return starting.ordinal !== null && starting.ordinal <= scheduled.queuedThroughOrdinal ? projection.thread @@ -2529,6 +2533,19 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio (candidate) => candidate.id === storedQueuedRun.userMessageId, ); const requestedSelection = queuedRunModelSelection(projection, storedQueuedRun); + const recoveryRun = + storedQueuedRun.failoverOfRunId !== undefined || + storedQueuedRun.restartContinuationOfRunId !== undefined; + const consumesProviderCooldownBypass = + !recoveryRun && + (storedQueuedRun.explicitModelSelection !== true || + projection.thread.scheduledModelSelection == null || + storedQueuedRun.ordinal > + projection.thread.scheduledModelSelection.queuedThroughOrdinal) && + queuedMessage !== undefined && + ProviderFailover.resendable(queuedMessage) && + projection.thread.bypassProviderCooldown === true && + draftModelPickMatchesThread(requestedSelection, nextTurnModelSelection(projection.thread)); const modelSelection = queuedMessage === undefined ? requestedSelection @@ -2540,13 +2557,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio run: { ...storedQueuedRun, bypassProviderCooldown: - storedQueuedRun.bypassProviderCooldown === true || - projection.thread.bypassProviderCooldown === true || - (projection.thread.scheduledModelSelection != null && - modelSelectionsEqual( - requestedSelection, - projection.thread.scheduledModelSelection.modelSelection, - )), + storedQueuedRun.bypassProviderCooldown === true || consumesProviderCooldownBypass, }, message: queuedMessage, }); @@ -2739,8 +2750,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio const threadAtStart = threadAtRunStart(projection, { runId: queuedRun.id, ordinal: queuedRun.ordinal, - model: - queuedRun.explicitModelSelection !== true + model: recoveryRun + ? "server" + : queuedRun.explicitModelSelection !== true ? "default" : queuedMessage?.creationSource === "server" ? "server" @@ -3216,7 +3228,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio [ ...(selectionChanged || threadAtStart !== projection.thread || - projection.thread.bypassProviderCooldown === true + consumesProviderCooldownBypass ? [ { type: !selectionChanged @@ -3231,7 +3243,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio ...threadAtStart, providerInstanceId: queuedRun.providerInstanceId, modelSelection: queuedRun.modelSelection, - bypassProviderCooldown: false, + ...(consumesProviderCooldownBypass ? { bypassProviderCooldown: false } : {}), updatedAt: now, }, }, @@ -5377,6 +5389,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio return { ...thread, scheduledModelSelection: providerSwitchPlan.scheduled, + bypassProviderCooldown: true, updatedAt: now, }; } @@ -7929,30 +7942,33 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio ) { modelSelection = queueTailModelSelection(projection); } + const recoveryRun = + command.failoverOfRunId !== undefined || command.restartContinuationOfRunId !== undefined; + const resendablePrompt = ProviderFailover.resendable({ ...incomingMessage, role: "user" }); + const consumesProviderCooldownBypass = + !recoveryRun && + resendablePrompt && + projection.thread.bypassProviderCooldown === true && + draftModelPickMatchesThread(modelSelection, nextTurnModelSelection(projection.thread)); const bypassProviderCooldown = - (command.modelSelection !== undefined && - !modelSelectionsEqual(command.modelSelection, projection.thread.modelSelection)) || - projection.thread.bypassProviderCooldown === true || - (projection.thread.scheduledModelSelection != null && - modelSelectionsEqual( - modelSelection, - projection.thread.scheduledModelSelection.modelSelection, - )); - if ( - command.failoverOfRunId === undefined && - command.restartContinuationOfRunId === undefined && - startsOrQueuesRun - ) { + (resendablePrompt && + command.modelSelection !== undefined && + !draftModelPickMatchesThread( + command.modelSelection, + nextTurnModelSelection(projection.thread), + )) || + consumesProviderCooldownBypass; + if (!recoveryRun && startsOrQueuesRun) { modelSelection = yield* ProviderFailover.resolveProviderCooldownTarget({ cooldown: providerCooldown, settings: serverSettings, projectId: projection.thread.projectId, modelSelection, run: { bypassProviderCooldown }, - message: { ...command, role: "user" }, + message: { ...incomingMessage, role: "user" }, }); } - if (projection.thread.bypassProviderCooldown === true && startsOrQueuesRun) { + if (consumesProviderCooldownBypass && startsOrQueuesRun) { const now = yield* DateTime.now; const event = yield* emit( events, @@ -8383,8 +8399,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio const threadAtStart = threadAtRunStart(projection, { runId: null, ordinal: null, - model: - command.modelSelection === undefined + model: recoveryRun + ? "server" + : command.modelSelection === undefined ? "default" : command.creationSource === "server" ? "server" diff --git a/apps/server/src/orchestration-v2/ProviderFailover.ts b/apps/server/src/orchestration-v2/ProviderFailover.ts index 985f800d183c3..d92c57cf6d95c 100644 --- a/apps/server/src/orchestration-v2/ProviderFailover.ts +++ b/apps/server/src/orchestration-v2/ProviderFailover.ts @@ -37,7 +37,7 @@ export interface ProviderFailoverTargets { * Only a prompt someone sent is re-sent. Wakes, delegated results, compaction and slash * commands are provider-specific or have their own recovery. */ -function resendable( +export function resendable( message: Pick< OrchestrationV2ConversationMessage, "role" | "notification" | "delegatedCompletion" | "idleCompaction" | "workflowScript" | "text" diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts index 05e753d51e04b..2e3bbd7a1066d 100644 --- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts +++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts @@ -8449,7 +8449,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { worktreePath: process.cwd(), }); // A restart continuation names the run's own model and comes from the server. - const send = (key: string, kind: "agent" | "server", queue: boolean) => + const send = (key: string, kind: "agent" | "server" | "notification", queue: boolean) => orchestrator.dispatch({ type: "message.dispatch", commandId: CommandId.make(`${threadId}:${key}`), @@ -8458,9 +8458,19 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { text: key, attachments: [], ...(kind === "server" ? { modelSelection } : {}), + ...(kind === "notification" + ? { + modelSelection: scheduledSelection, + notification: { + source: { kind: "monitor" as const }, + outcome: "updated" as const, + summary: key, + }, + } + : {}), dispatchMode: { type: queue ? "queue_after_active" : "start_immediately" }, createdBy: "agent", - creationSource: kind === "server" ? "server" : "mcp", + creationSource: kind === "agent" ? "mcp" : "server", }); const run = (key: string) => orchestrator @@ -8494,6 +8504,12 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { .getThreadProjection(threadId) .pipe(Effect.map(({ thread }) => thread.scheduledModelSelection ?? null)); yield* send("active", "agent", false); + yield* orchestrator.dispatch({ + type: "thread.model-selection.set", + commandId: CommandId.make(`${threadId}:pick`), + threadId, + modelSelection, + }); yield* orchestrator.dispatch({ type: "thread.model-selection.set", commandId: CommandId.make(`${threadId}:schedule`), @@ -8501,12 +8517,16 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { modelSelection: scheduledSelection, applyAt: "next_turn", }); - return { threadId, send, run, complete, scheduledNow }; + const bypassNow = orchestrator + .getThreadProjection(threadId) + .pipe(Effect.map(({ thread }) => thread.bypassProviderCooldown === true)); + return { threadId, send, run, complete, scheduledNow, bypassNow }; }); // Queued behind the busy run. const queued = yield* scenario("queued"); yield* queued.send("continuation", "server", true); + assert.isTrue(yield* queued.bypassNow); yield* queued.send("after", "agent", true); assert.deepEqual((yield* queued.run("after")).modelSelection, scheduledSelection); yield* queued.complete("active"); @@ -8523,13 +8543,33 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { // Started at once on the idle thread, after the busy run ended. const immediate = yield* scenario("immediate"); yield* immediate.complete("active"); - yield* immediate.send("continuation", "server", false); - assert.deepEqual((yield* immediate.run("continuation")).modelSelection, modelSelection); - assert.isNotNull(yield* immediate.scheduledNow()); - yield* immediate.complete("continuation"); + for (const key of ["continuation", "another-continuation"]) { + yield* immediate.send(key, "server", false); + assert.deepEqual((yield* immediate.run(key)).modelSelection, modelSelection); + assert.isNotNull(yield* immediate.scheduledNow()); + assert.isTrue(yield* immediate.bypassNow); + yield* immediate.complete(key); + assert.isTrue(yield* immediate.bypassNow); + } yield* immediate.send("next", "agent", false); assert.deepEqual((yield* immediate.run("next")).modelSelection, scheduledSelection); assert.isNull(yield* immediate.scheduledNow()); + assert.isFalse(yield* immediate.bypassNow); + + const notifications = yield* scenario("notifications"); + yield* notifications.complete("active"); + for (const key of ["wake", "another-wake"]) { + yield* notifications.send(key, "notification", true); + assert.deepEqual((yield* notifications.run(key)).modelSelection, scheduledSelection); + assert.isNotNull(yield* notifications.scheduledNow()); + assert.isTrue(yield* notifications.bypassNow); + yield* notifications.complete(key); + assert.isTrue(yield* notifications.bypassNow); + } + yield* notifications.send("next", "agent", false); + assert.deepEqual((yield* notifications.run("next")).modelSelection, scheduledSelection); + assert.isNull(yield* notifications.scheduledNow()); + assert.isFalse(yield* notifications.bypassNow); }), ); @@ -9163,6 +9203,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { modelSelection: newerSelection, }); const waiting = yield* orchestrator.getThreadProjection(threadId); + assert.isTrue(waiting.thread.bypassProviderCooldown); yield* eventSink.write({ events: [ { @@ -9190,6 +9231,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { newerSelection.instanceId, ); assert.deepEqual(after.thread.modelSelection, newerSelection); + assert.isTrue(after.thread.bypassProviderCooldown); }), ); @@ -10160,6 +10202,17 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { dispatchMode: { type: "start_immediately" }, }); const cut = (yield* runFor("cut"))!; + const scheduledSelection = { ...modelSelection, model: "gpt-5.5" }; + yield* orchestrator.dispatch({ + type: "thread.model-selection.set", + commandId: CommandId.make(`${threadId}:schedule`), + threadId, + modelSelection: scheduledSelection, + applyAt: "next_turn", + }); + const beforeRecovery = yield* orchestrator.getThreadProjection(threadId); + assert.isNotNull(beforeRecovery.thread.scheduledModelSelection); + assert.isTrue(beforeRecovery.thread.bypassProviderCooldown); yield* write("cut:cancelled", { ...cut, status: "cancelled" }); yield* orchestrator.dispatch({ type: "message.dispatch", @@ -10170,10 +10223,13 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { messageId: messageId("continuation"), text: "Continue where you left off.", attachments: [], - modelSelection, + modelSelection: scheduledSelection, dispatchMode: { type: "start_immediately" }, restartContinuationOfRunId: cut.id, }); + const afterRecovery = yield* orchestrator.getThreadProjection(threadId); + assert.isNotNull(afterRecovery.thread.scheduledModelSelection); + assert.isTrue(afterRecovery.thread.bypassProviderCooldown); yield* orchestrator.dispatch({ type: "message.dispatch", createdBy: "user", @@ -12931,6 +12987,7 @@ it.layer( key: string, options?: { readonly freshContext?: boolean; + readonly scheduledFallback?: boolean; readonly fallbackInstanceId?: ProviderInstanceId; /** The failed message opens the thread; there is no earlier role. */ readonly sourceFirst?: boolean; @@ -13020,6 +13077,15 @@ it.layer( const source = yield* latest; yield* send("queued", "Queue: a later note.", "queue_after_active"); const queued = yield* latest; + if (options?.scheduledFallback === true) { + yield* orchestrator.dispatch({ + type: "thread.model-selection.set", + commandId: CommandId.make(`failover:schedule:${key}`), + threadId, + modelSelection: fallback, + applyAt: "next_turn", + }); + } yield* finish(source, { status: "failed", startedAt: now, @@ -13052,7 +13118,11 @@ it.layer( const orchestrator = yield* Orchestrator.OrchestratorV2; const { threadId, source, queued } = yield* failedRunWithClaim(`dispatch:${freshContext}`, { freshContext, + scheduledFallback: true, }); + const beforeRecovery = yield* orchestrator.getThreadProjection(threadId); + assert.isNotNull(beforeRecovery.thread.scheduledModelSelection); + assert.isTrue(beforeRecovery.thread.bypassProviderCooldown); // Startup queue recovery and the parent wait both treat the claim as unfinished work. yield* orchestrator.resumeQueuedRuns; const pending = yield* orchestrator.getThreadProjection(threadId); @@ -13068,6 +13138,8 @@ it.layer( yield* runFailover(threadId, source.id, 2); const after = yield* orchestrator.getThreadProjection(threadId); + assert.isNotNull(after.thread.scheduledModelSelection); + assert.isTrue(after.thread.bypassProviderCooldown); const failedOver = after.runs.filter((run) => run.failoverOfRunId === source.id); assert.lengthOf(failedOver, 1); const run = failedOver[0]!; 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 742952c47a5dc..38630271cca63 100644 --- a/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts +++ b/apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts @@ -4037,9 +4037,15 @@ describe("orchestration v2 provider switching", () => { "explicit", "picker", "scheduled", + "scheduled-dispatch", + "scheduled-explicit-current", + "scheduled-superseded", + "scheduled-older-explicit", "immediate-default", "immediate-deferred", "immediate-same", + "immediate-fast-off", + "immediate-fast-omitted", "immediate-different", ] as const)("keeps queued %s routing safe while its requested account is cooling", (kind) => Effect.scoped( @@ -4048,7 +4054,7 @@ describe("orchestration v2 provider switching", () => { const capturedTurns = yield* Ref.make>([]); const started = yield* Deferred.make(); const immediate = kind.startsWith("immediate"); - const startsOnClaude = kind === "scheduled" || kind === "immediate-different"; + const startsOnClaude = kind.startsWith("scheduled") || kind === "immediate-different"; const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([ makeTestAdapter({ instanceId: CODEX_MODEL_SELECTION.instanceId, @@ -4066,7 +4072,7 @@ describe("orchestration v2 provider switching", () => { modelSelection: CLAUDE_MODEL_SELECTION, responseByRunOrdinal: {}, capturedTurns, - ...(kind === "scheduled" ? { holdFirstTurn: started } : {}), + ...(kind.startsWith("scheduled") ? { holdFirstTurn: started } : {}), }), ]); yield* Effect.gen(function* () { @@ -4080,7 +4086,11 @@ describe("orchestration v2 provider switching", () => { threadId, projectId, title: "Cooldown queue", - modelSelection: startsOnClaude ? CLAUDE_MODEL_SELECTION : CODEX_MODEL_SELECTION, + modelSelection: startsOnClaude + ? CLAUDE_MODEL_SELECTION + : kind === "immediate-fast-omitted" + ? { ...CODEX_MODEL_SELECTION, options: [{ id: "fastMode", value: false }] } + : CODEX_MODEL_SELECTION, runtimeMode: "full-access", interactionMode: "default", branch: null, @@ -4088,14 +4098,19 @@ describe("orchestration v2 provider switching", () => { createdBy: "user", creationSource: "web", }); - const armCooldown = cooldown.observe(CODEX_MODEL_SELECTION.instanceId, { - status: "failed", - failure: { - ...makeProviderFailure({ class: "usage_limit", message: "Account limit" }), - limitScope: "account", - resetAt: "2099-01-01T00:00:00.000Z", + const armCooldown = cooldown.observe( + kind === "scheduled-explicit-current" + ? CLAUDE_MODEL_SELECTION.instanceId + : CODEX_MODEL_SELECTION.instanceId, + { + status: "failed", + failure: { + ...makeProviderFailure({ class: "usage_limit", message: "Account limit" }), + limitScope: "account", + resetAt: "2099-01-01T00:00:00.000Z", + }, }, - }); + ); if (immediate) { yield* armCooldown; yield* orchestrator.dispatch({ @@ -4107,7 +4122,12 @@ describe("orchestration v2 provider switching", () => { attachments: [], ...(kind === "immediate-default" || kind === "immediate-deferred" ? {} - : { modelSelection: CODEX_MODEL_SELECTION }), + : { + modelSelection: + kind === "immediate-fast-off" + ? { ...CODEX_MODEL_SELECTION, options: [{ id: "fastMode", value: false }] } + : CODEX_MODEL_SELECTION, + }), dispatchMode: { type: kind === "immediate-deferred" ? "defer_start" : "start_immediately", }, @@ -4148,24 +4168,41 @@ describe("orchestration v2 provider switching", () => { messageId: MessageId.make(`message:queued-cooldown:${position}`), text: position, attachments: [], - ...(position === "queued" && kind === "explicit" + ...(position === "queued" && + (kind === "explicit" || kind === "scheduled-older-explicit") ? { modelSelection: CODEX_MODEL_SELECTION } - : {}), + : position === "queued" && kind === "scheduled-superseded" + ? { modelSelection: CLAUDE_MODEL_SELECTION } + : {}), dispatchMode: { type: position === "first" ? "start_immediately" : "queue_after_active", }, createdBy: "user", creationSource: "web", }); - if (position === "first") yield* Deferred.await(started); + if (position === "first") { + yield* Deferred.await(started); + if (kind === "scheduled-superseded") { + yield* orchestrator.dispatch({ + type: "thread.model-selection.set", + commandId: CommandId.make("command:queued-cooldown:early-pick"), + threadId, + modelSelection: CODEX_MODEL_SELECTION, + applyAt: "next_turn", + }); + } + } } - if (kind === "picker" || kind === "scheduled") { + if ( + kind === "picker" || + (kind.startsWith("scheduled") && kind !== "scheduled-superseded") + ) { yield* orchestrator.dispatch({ type: "thread.model-selection.set", commandId: CommandId.make("command:queued-cooldown:pick"), threadId, modelSelection: CODEX_MODEL_SELECTION, - ...(kind === "scheduled" ? { applyAt: "next_turn" as const } : {}), + ...(kind.startsWith("scheduled") ? { applyAt: "next_turn" as const } : {}), }); } const queued = yield* orchestrator.getThreadProjection(threadId); @@ -4184,6 +4221,9 @@ describe("orchestration v2 provider switching", () => { occurredAt: now, payload: { ...pending!, + ...(kind === "scheduled-dispatch" || kind === "scheduled-explicit-current" + ? { queueHeld: true } + : {}), ...(kind === "failover" ? { failoverOfRunId: active!.id } : {}), ...(kind === "restart" ? { restartContinuationOfRunId: active!.id } : {}), ...(kind === "claimed" @@ -4208,6 +4248,60 @@ describe("orchestration v2 provider switching", () => { }, ], }); + if (kind === "scheduled-explicit-current") { + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make("command:scheduled-cooldown:explicit-current"), + threadId, + messageId: MessageId.make("message:scheduled-cooldown:explicit-current"), + text: "Use the persisted model instead of the scheduled pick.", + attachments: [], + modelSelection: CLAUDE_MODEL_SELECTION, + dispatchMode: { type: "start_immediately" }, + createdBy: "user", + creationSource: "web", + }); + yield* worker.drain(); + const after = yield* orchestrator.getThreadProjection(threadId); + assert.deepEqual(after.runs.at(-1)?.modelSelection, CLAUDE_MODEL_SELECTION); + assert.isNull(after.thread.scheduledModelSelection); + assert.isFalse(after.thread.bypassProviderCooldown === true); + return; + } + if (kind === "scheduled-dispatch") { + for (const position of ["picked", "later"] as const) { + yield* armCooldown; + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make(`command:scheduled-cooldown:${position}`), + threadId, + messageId: MessageId.make(`message:scheduled-cooldown:${position}`), + text: position, + attachments: [], + dispatchMode: { type: "start_immediately" }, + createdBy: "agent", + creationSource: "mcp", + }); + yield* worker.drain(); + yield* orchestrator.streamStoredEvents.pipe( + Stream.filter( + ({ event }) => + event.type === "run.updated" && + event.payload.userMessageId === + MessageId.make(`message:scheduled-cooldown:${position}`) && + event.payload.status === "completed", + ), + Stream.runHead, + ); + const after = yield* orchestrator.getThreadProjection(threadId); + const expected = + position === "picked" ? CODEX_MODEL_SELECTION : CLAUDE_MODEL_SELECTION; + assert.deepEqual(after.runs.at(-1)?.modelSelection, expected); + assert.deepEqual((yield* Ref.get(capturedTurns)).at(-1)?.modelSelection, expected); + assert.isNotNull(after.thread.scheduledModelSelection); + } + return; + } yield* orchestrator.resumeQueuedRuns; yield* worker.drain(); yield* orchestrator.streamStoredEvents.pipe( @@ -4221,7 +4315,7 @@ describe("orchestration v2 provider switching", () => { ); const after = yield* orchestrator.getThreadProjection(threadId); const expected = - kind === "default" || kind === "explicit" + kind === "default" || kind === "explicit" || kind === "scheduled-superseded" ? CLAUDE_MODEL_SELECTION : CODEX_MODEL_SELECTION; assert.deepEqual( @@ -4229,13 +4323,44 @@ describe("orchestration v2 provider switching", () => { expected, ); assert.deepEqual((yield* Ref.get(capturedTurns)).at(-1)?.modelSelection, expected); - assert.isFalse(after.thread.bypassProviderCooldown === true); + if (kind === "scheduled-older-explicit") { + assert.isNotNull(after.thread.scheduledModelSelection); + assert.isTrue(after.thread.bypassProviderCooldown); + yield* armCooldown; + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make("command:scheduled-cooldown:consume"), + threadId, + messageId: MessageId.make("message:scheduled-cooldown:consume"), + text: "Now use my pending pick.", + attachments: [], + dispatchMode: { type: "start_immediately" }, + createdBy: "user", + creationSource: "web", + }); + yield* worker.drain(); + const consumed = yield* waitForIdle(threadId); + assert.deepEqual(consumed.runs.at(-1)?.modelSelection, CODEX_MODEL_SELECTION); + assert.isNull(consumed.thread.scheduledModelSelection); + assert.isFalse(consumed.thread.bypassProviderCooldown === true); + } else { + if (kind === "scheduled-superseded") + assert.isNull(after.thread.scheduledModelSelection); + assert.isFalse(after.thread.bypassProviderCooldown === true); + } }).pipe( Effect.provide( ProviderReplayHarness.layerWithRegistry( { name: `queued-cooldown-${kind}`, runtimePolicyOverride: { cwd } }, layerRegistry, - { settings: { fallbackModelSelection: CLAUDE_MODEL_SELECTION } }, + { + settings: { + fallbackModelSelection: + kind === "scheduled-explicit-current" + ? CODEX_MODEL_SELECTION + : CLAUDE_MODEL_SELECTION, + }, + }, ).pipe(Layer.provideMerge(ProviderCooldown.layer)), ), );