Skip to content

Commit 5984bbb

Browse files
fix(server): a model change sent with a Claude steer now applies to the running turn (#34564)
1 parent b20bbc5 commit 5984bbb

9 files changed

Lines changed: 160 additions & 12 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6361,6 +6361,120 @@ describe("ClaudeAdapterV2 background wake turns", () => {
63616361
),
63626362
);
63636363

6364+
// regression: #34549 a model change sent with a steer was shown but never applied to the turn.
6365+
it.effect("applies a model change sent with a steer to the running turn", () =>
6366+
Effect.scoped(
6367+
Effect.gen(function* () {
6368+
const harness = yield* makeWakeHarness;
6369+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
6370+
const opus = {
6371+
instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
6372+
model: "claude-opus-5-5",
6373+
} satisfies ModelSelection;
6374+
const input = makeClaudeTestTurnInput({
6375+
threadId: harness.threadId,
6376+
providerThread: harness.providerThread,
6377+
now: yield* DateTime.now,
6378+
attemptId: RunAttemptId.make("attempt-steer-model-change"),
6379+
text: "Run the slow build.",
6380+
attachments: [],
6381+
modelSelection: opus,
6382+
});
6383+
yield* harness.runtime.startTurn(input);
6384+
yield* harness.runtime.steerTurn({
6385+
threadId: harness.threadId,
6386+
runId: input.runId,
6387+
providerThread: harness.providerThread,
6388+
providerTurnId: idAllocator.derive.providerTurn({
6389+
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
6390+
nativeTurnId: `turn:${input.attemptId}`,
6391+
}),
6392+
modelSelection: { ...opus, model: "claude-sonnet-4-6" },
6393+
message: {
6394+
createdBy: "user",
6395+
creationSource: "web",
6396+
messageId: MessageId.make("steer-model-change"),
6397+
text: "Which model are you?",
6398+
attachments: [],
6399+
},
6400+
});
6401+
assert.deepEqual(harness.liveSelectionChanges, [
6402+
{ model: "claude-sonnet-4-6" },
6403+
{ effortLevel: "high", fastMode: null, ultracode: null },
6404+
]);
6405+
assert.lengthOf(harness.offeredMessages, 2);
6406+
}).pipe(
6407+
Effect.provide(
6408+
Layer.mergeAll(
6409+
IdAllocator.layer,
6410+
McpProviderSessions.layer,
6411+
NodeServices.layer,
6412+
NodeCrypto.layer,
6413+
),
6414+
),
6415+
),
6416+
),
6417+
);
6418+
6419+
it.effect("fails a model-changing steer when the turn ends during the selection change", () =>
6420+
Effect.scoped(
6421+
Effect.gen(function* () {
6422+
let duringFlags: Effect.Effect<void> = Effect.void;
6423+
const harness = yield* makeWakeHarnessWithOptions({
6424+
applyFlagSettings: () => Effect.suspend(() => duringFlags),
6425+
});
6426+
const idAllocator = yield* IdAllocator.IdAllocatorV2;
6427+
const opus = {
6428+
instanceId: ClaudeAdapterV2.CLAUDE_DEFAULT_INSTANCE_ID,
6429+
model: "claude-opus-5-5",
6430+
} satisfies ModelSelection;
6431+
const input = makeClaudeTestTurnInput({
6432+
threadId: harness.threadId,
6433+
providerThread: harness.providerThread,
6434+
now: yield* DateTime.now,
6435+
attemptId: RunAttemptId.make("attempt-steer-model-change-race"),
6436+
text: "Run the slow build.",
6437+
attachments: [],
6438+
modelSelection: opus,
6439+
});
6440+
yield* harness.runtime.startTurn(input);
6441+
duringFlags = harness
6442+
.offerAndWait(turnOneResult)
6443+
.pipe(Effect.andThen(Queue.take(harness.terminalReceipts)), Effect.asVoid);
6444+
const steer = yield* harness.runtime
6445+
.steerTurn({
6446+
threadId: harness.threadId,
6447+
runId: input.runId,
6448+
providerThread: harness.providerThread,
6449+
providerTurnId: idAllocator.derive.providerTurn({
6450+
driver: ClaudeAdapterV2.CLAUDE_PROVIDER,
6451+
nativeTurnId: `turn:${input.attemptId}`,
6452+
}),
6453+
modelSelection: { ...opus, model: "claude-sonnet-4-6" },
6454+
message: {
6455+
createdBy: "user",
6456+
creationSource: "web",
6457+
messageId: MessageId.make("steer-model-change-race"),
6458+
text: "Which model are you?",
6459+
attachments: [],
6460+
},
6461+
})
6462+
.pipe(Effect.result);
6463+
assert.equal(steer._tag, "Failure");
6464+
assert.lengthOf(harness.offeredMessages, 1);
6465+
}).pipe(
6466+
Effect.provide(
6467+
Layer.mergeAll(
6468+
IdAllocator.layer,
6469+
McpProviderSessions.layer,
6470+
NodeServices.layer,
6471+
NodeCrypto.layer,
6472+
),
6473+
),
6474+
),
6475+
),
6476+
);
6477+
63646478
it.effect("re-sends the model on the turn after a live selection change fails", () =>
63656479
Effect.scoped(
63666480
Effect.gen(function* () {

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12018,11 +12018,26 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1201812018
}),
1201912019
persistAttachmentBudget(existing.nativeThreadId),
1202012020
);
12021+
// A selection sent with the steer applies to the running turn when the live process can switch to it.
12022+
const steerSelection =
12023+
turnInput.modelSelection === undefined
12024+
? undefined
12025+
: compileClaudeModelSelection(turnInput.modelSelection, modelCatalog);
12026+
const liveSteerSelection =
12027+
steerSelection !== undefined &&
12028+
existing.selection.queryIdentity !== steerSelection.queryIdentity &&
12029+
canApplyClaudeSelectionLive(existing.selection, steerSelection) &&
12030+
(existing.selection.apiModelId === steerSelection.apiModelId ||
12031+
existing.openedPermissionMode === currentTurn.queryPolicy.permissionMode)
12032+
? steerSelection
12033+
: undefined;
1202112034
const userMessage = yield* makeClaudeUserMessageWithAttachments({
1202212035
text: applyClaudePromptEffortPrefix(
1202312036
turnInput.message.text,
12024-
compileClaudeModelSelection(currentTurn.input.modelSelection, modelCatalog)
12025-
.promptEffort,
12037+
(
12038+
liveSteerSelection ??
12039+
compileClaudeModelSelection(currentTurn.input.modelSelection, modelCatalog)
12040+
).promptEffort,
1202612041
),
1202712042
attachments: turnInput.message.attachments,
1202812043
priority: "now",
@@ -12036,9 +12051,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1203612051
Effect.provideService(Path.Path, path),
1203712052
);
1203812053
yield* backgroundForegroundSubagents(existing, backgroundDeadline);
12039-
// The turn can end while the moves are answered. A steer offered
12040-
// then would start a new turn the orchestrator never asked for, so
12041-
// the steer fails and the orchestrator starts the follow-up turn.
12054+
if (liveSteerSelection !== undefined) {
12055+
yield* applySelectionLive(existing, liveSteerSelection);
12056+
}
12057+
// The turn can end while the moves or the selection change are answered.
12058+
// A steer offered then would start a new turn the orchestrator never
12059+
// asked for, so the steer fails and the orchestrator starts the follow-up turn.
1204212060
if (
1204312061
(yield* Ref.get(activeTurn))?.providerTurnId !== turnInput.providerTurnId ||
1204412062
(yield* Ref.get(queryContext)) !== existing ||

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export const OrchestrationEffectRequestV2 = Schema.Union([
6464
providerThreadId: ProviderThreadId,
6565
providerTurnId: ProviderTurnId,
6666
messageId: MessageId,
67+
modelSelection: Schema.optional(ModelSelection),
6768
}),
6869
Schema.Struct({
6970
type: Schema.Literal("provider-turn.restart"),

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ export const layerExecutor: Layer.Layer<
257257
providerThreadId: effect.request.providerThreadId,
258258
providerTurnId: effect.request.providerTurnId,
259259
messageId: effect.request.messageId,
260+
...(effect.request.modelSelection === undefined
261+
? {}
262+
: { modelSelection: effect.request.modelSelection }),
260263
})
261264
.pipe(
262265
Effect.tap(() =>

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5611,9 +5611,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
56115611
if (steeringPolicy === "active_steering") {
56125612
const instanceChanged =
56135613
input.projection.thread.providerInstanceId !== input.modelSelection.instanceId;
5614+
// Only a user's steer carries the picker's selection; the adapter applies it to the live turn.
5615+
const steerCarriesSelection =
5616+
input.delegatedCompletion === undefined && input.notification === undefined;
56145617
if (
5615-
input.delegatedCompletion === undefined &&
5616-
input.notification === undefined &&
5618+
steerCarriesSelection &&
56175619
(instanceChanged ||
56185620
!modelSelectionsEqual(input.projection.thread.modelSelection, input.modelSelection))
56195621
) {
@@ -5649,6 +5651,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
56495651
providerThreadId: providerThread.id,
56505652
providerTurnId: providerTurn.id,
56515653
messageId: input.messageId,
5654+
...(steerCarriesSelection ? { modelSelection: input.modelSelection } : {}),
56525655
},
56535656
} satisfies PendingOrchestrationEffectV2,
56545657
]);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ export interface ProviderTurnControlServiceV2Shape {
6868
readonly providerThreadId: ProviderThreadId;
6969
readonly providerTurnId: ProviderTurnId;
7070
readonly messageId: MessageId;
71+
readonly modelSelection?: ModelSelection;
7172
}) => Effect.Effect<void, ProviderTurnControlError>;
7273
/** Best effort: a turn that ended or moved sessions picks the selection up when it next starts. */
7374
readonly applyModelSelection: (input: {
@@ -405,6 +406,9 @@ export const layer: Layer.Layer<
405406
runId: run.id,
406407
providerThread: loaded.providerThread,
407408
providerTurnId: loaded.providerTurn.id,
409+
...(input.modelSelection === undefined
410+
? {}
411+
: { modelSelection: input.modelSelection }),
408412
message: {
409413
messageId: message.id,
410414
text: projectComposerContextForProvider({

‎apps/server/src/orchestration-v2/SteeringCompletion.integration.test.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ const nextTurnSelectionHarness = Effect.fn("nextTurnSelectionHarness")(function*
499499
const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
500500
const started: ProviderAdapter.ProviderAdapterV2TurnInput[] = [];
501501
const steered: string[] = [];
502+
const steeredSelections: Array<ModelSelection | undefined> = [];
502503
const interrupted: ProviderTurnId[] = [];
503504
const capabilities = {
504505
...CodexProviderCapabilitiesV2,
@@ -587,6 +588,7 @@ const nextTurnSelectionHarness = Effect.fn("nextTurnSelectionHarness")(function*
587588
steerTurn: (turn) =>
588589
Effect.sync(() => {
589590
steered.push(turn.message.text);
591+
steeredSelections.push(turn.modelSelection);
590592
}),
591593
interruptTurn: ({ providerTurnId }) =>
592594
Effect.sync(() => {
@@ -658,7 +660,7 @@ const nextTurnSelectionHarness = Effect.fn("nextTurnSelectionHarness")(function*
658660
yield* Fiber.join(running);
659661
return threadId;
660662
});
661-
return { cwd, events, started, steered, interrupted, layer, startFirstTurn };
663+
return { cwd, events, started, steered, steeredSelections, interrupted, layer, startFirstTurn };
662664
});
663665

664666
it.effect.each([
@@ -1189,10 +1191,8 @@ it.effect(
11891191
() =>
11901192
Effect.scoped(
11911193
Effect.gen(function* () {
1192-
const { started, steered, layer, startFirstTurn } = yield* nextTurnSelectionHarness(
1193-
"steering-selection-change",
1194-
true,
1195-
);
1194+
const { started, steered, steeredSelections, layer, startFirstTurn } =
1195+
yield* nextTurnSelectionHarness("steering-selection-change", true);
11961196
yield* Effect.gen(function* () {
11971197
const orchestrator = yield* Orchestrator.OrchestratorV2;
11981198
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
@@ -1252,6 +1252,8 @@ it.effect(
12521252
yield* steer("steer-back", runSelection);
12531253
const back = yield* orchestrator.getThreadProjection(threadId);
12541254
assert.deepEqual(steered, ["steer-changed", "steer-reverted", "steer-back"]);
1255+
// The adapter gets each steer's selection so it can switch the live turn, and back.
1256+
assert.deepEqual(steeredSelections, [composerSelection, runSelection, runSelection]);
12551257
assert.equal(back.thread.providerInstanceId, instanceId);
12561258
assert.deepEqual(back.thread.modelSelection, runSelection);
12571259
}).pipe(Effect.provide(layer));

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive", (it) => {
15201520
providerThreadId: providerThread.id,
15211521
providerTurnId: providerTurn.id,
15221522
messageId: steerMessageId,
1523+
modelSelection: run.modelSelection,
15231524
},
15241525
],
15251526
);

‎packages/provider-core/src/server/ProviderAdapter.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,8 @@ export interface ProviderAdapterV2SteerInput {
521521
readonly providerThread: OrchestrationV2ProviderThread;
522522
readonly providerTurnId: ProviderTurnId;
523523
readonly message: ProviderAdapterV2TurnMessage;
524+
/** The selection sent with the message when it differs from the running turn's. */
525+
readonly modelSelection?: ModelSelection;
524526
}
525527

526528
export interface ProviderAdapterV2InterruptInput {

0 commit comments

Comments
 (0)