Skip to content

Commit 7fb1224

Browse files
fix(server): Claude effort changes apply to the running turn
thread.model-selection.set only saved the selection, so an effort change made mid-turn (for example through t3_thread_configure) reached Claude only when the next turn opened its query. When the instance and session stay the same and a turn is running on a session whose runtime supports it, the command now queues a provider-session.apply-model-selection effect. Claude implements the new optional applyModelSelection by sending the flag settings to the live query, sharing the code the next turn already uses. Model or thinking changes still wait for the next turn. The repro test now changes effort to medium: the bundled catalog caps Sonnet 4.6 at high, so xhigh compiled to the running selection. Fixes #34349 AI-assisted; verified with the tests and tools listed above.
1 parent d914c85 commit 7fb1224

8 files changed

Lines changed: 158 additions & 21 deletions

File tree

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

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10900,6 +10900,29 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1090010900
return false;
1090110901
});
1090210902

10903+
const applySelectionLive = Effect.fnUntraced(function* (
10904+
live: ClaudeLiveQueryContext,
10905+
selection: CompiledClaudeModelSelection,
10906+
) {
10907+
// Preserve launcher-selected models unless the model changed or a failed update left it unknown.
10908+
yield* (
10909+
live.selection.queryIdentity === "" ||
10910+
live.selection.apiModelId !== selection.apiModelId
10911+
? live.query.setModel(selection.apiModelId)
10912+
: Effect.void
10913+
).pipe(
10914+
Effect.andThen(
10915+
live.query.applyFlagSettings(claudeLiveSelectionFlagSettings(selection)),
10916+
),
10917+
Effect.tapError(() =>
10918+
Effect.sync(() => {
10919+
live.selection = { ...live.selection, queryIdentity: "" };
10920+
}),
10921+
),
10922+
);
10923+
live.selection = selection;
10924+
});
10925+
1090310926
const openQuery = Effect.fnUntraced(function* (
1090410927
turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
1090510928
nativeThreadId: string,
@@ -10940,24 +10963,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1094010963
(existing.selection.apiModelId === compiledSelection.apiModelId ||
1094110964
existing.openedPermissionMode === queryPolicy.permissionMode)
1094210965
) {
10943-
const live = existing;
10944-
// Preserve launcher-selected models unless the model changed or a failed update left it unknown.
10945-
yield* (
10946-
live.selection.queryIdentity === "" ||
10947-
live.selection.apiModelId !== compiledSelection.apiModelId
10948-
? live.query.setModel(compiledSelection.apiModelId)
10949-
: Effect.void
10950-
).pipe(
10951-
Effect.andThen(
10952-
live.query.applyFlagSettings(claudeLiveSelectionFlagSettings(compiledSelection)),
10953-
),
10954-
Effect.tapError(() =>
10955-
Effect.sync(() => {
10956-
live.selection = { ...live.selection, queryIdentity: "" };
10957-
}),
10958-
),
10959-
);
10960-
live.selection = compiledSelection;
10966+
yield* applySelectionLive(existing, compiledSelection);
1096110967
}
1096210968
if (
1096310969
existing !== null &&
@@ -12118,6 +12124,30 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1211812124
);
1211912125
}),
1212012126
},
12127+
applyModelSelection: Effect.fn("ClaudeAdapterV2.applyModelSelection")(function* ({
12128+
providerThread,
12129+
modelSelection,
12130+
}) {
12131+
const live = yield* Ref.get(queryContext);
12132+
const selection = compileClaudeModelSelection(modelSelection, modelCatalog);
12133+
// A model change still waits for the next turn, which checks it against the turn's permission mode.
12134+
if (
12135+
live === null ||
12136+
live.stopping ||
12137+
live.nativeThreadId !== providerThread.nativeThreadRef?.nativeId ||
12138+
live.selection.queryIdentity === selection.queryIdentity ||
12139+
live.selection.apiModelId !== selection.apiModelId ||
12140+
!canApplyClaudeSelectionLive(live.selection, selection)
12141+
) {
12142+
return;
12143+
}
12144+
// A failed update clears the live identity, so the next turn applies the selection again.
12145+
yield* applySelectionLive(live, selection).pipe(
12146+
Effect.catch((cause) =>
12147+
Effect.logWarning("orchestration-v2.claude-live-selection-failed", { cause }),
12148+
),
12149+
);
12150+
}),
1212112151
stopTask: Effect.fn("ClaudeAdapterV2.stopTask")(function* ({ providerThread, taskId }) {
1212212152
const liveQuery = yield* Ref.get(queryContext);
1212312153
const task = (yield* Ref.get(sessionSubagentsByTaskId)).get(taskId);

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,13 +580,15 @@ it.effect("applies an effort change to the running Claude query without waiting
580580

581581
yield* orchestrator.dispatch({
582582
type: "thread.model-selection.set",
583-
commandId: CommandId.make("xhigh"),
583+
commandId: CommandId.make("medium"),
584584
threadId,
585-
modelSelection: { ...modelSelection, options: [{ id: "effort", value: "xhigh" }] },
585+
modelSelection: { ...modelSelection, options: [{ id: "effort", value: "medium" }] },
586586
});
587587
yield* worker.drain();
588588

589-
assert.deepEqual(flagSettings, [{ effortLevel: "xhigh", fastMode: null, ultracode: null }]);
589+
assert.deepEqual(flagSettings, [
590+
{ effortLevel: "medium", fastMode: null, ultracode: null },
591+
]);
590592
}).pipe(
591593
Effect.provide(
592594
ProviderReplayHarness.layerWithRegistry(

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CheckpointScopeId,
44
CommandId,
55
MessageId,
6+
ModelSelection,
67
NodeId,
78
ProviderSessionId,
89
RunAttemptId,
@@ -40,6 +41,12 @@ export const OrchestrationEffectRequestV2 = Schema.Union([
4041
detail: Schema.optional(Schema.String),
4142
revokeMcpCredential: Schema.optional(Schema.Boolean),
4243
}),
44+
Schema.Struct({
45+
type: Schema.Literal("provider-session.apply-model-selection"),
46+
providerSessionId: ProviderSessionId,
47+
providerThreadId: ProviderThreadId,
48+
modelSelection: ModelSelection,
49+
}),
4350
Schema.Struct({
4451
type: Schema.Literal("provider-turn.start"),
4552
runId: RunId,
@@ -136,6 +143,7 @@ export const REPLAY_SAFE_EFFECT_TYPES_AFTER_PROCESS_LOSS = [
136143
] as const satisfies ReadonlyArray<OrchestrationEffectRequestV2["type"]>;
137144

138145
export const PROCESS_BOUND_EFFECT_TYPES = [
146+
"provider-session.apply-model-selection",
139147
"provider-turn.start",
140148
"provider-turn.interrupt",
141149
"provider-turn.steer",

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ function layerExecutorFor(input: {
9393
ProviderTurnControlService.ProviderTurnControlServiceV2.of({
9494
interrupt: input.interrupt ?? (() => Effect.void),
9595
steer: () => Effect.void,
96+
applyModelSelection: () => Effect.void,
9697
interruptAndAwaitTerminal: (request) =>
9798
record(
9899
request.replacementProviderSessionId === undefined

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,24 @@ export const layerExecutor: Layer.Layer<
138138
}),
139139
),
140140
);
141+
case "provider-session.apply-model-selection":
142+
return providerTurnControl
143+
.applyModelSelection({
144+
threadId: effect.threadId,
145+
providerSessionId: effect.request.providerSessionId,
146+
providerThreadId: effect.request.providerThreadId,
147+
modelSelection: effect.request.modelSelection,
148+
})
149+
.pipe(
150+
Effect.mapError(
151+
(cause) =>
152+
new OrchestrationEffectExecutionError({
153+
effectId: effect.id,
154+
effectType: effect.request.type,
155+
cause,
156+
}),
157+
),
158+
);
141159
case "provider-turn.start":
142160
return providerTurnStart
143161
.start({ threadId: effect.threadId, runId: effect.request.runId, willRetry })

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4540,6 +4540,46 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
45404540
// Archiving ends the guest share, so its waiting files go too.
45414541
yield* releaseGuestAttachments(command, thread.guestShare, updatedThread.guestShare, effects);
45424542

4543+
if (
4544+
command.type === "thread.model-selection.set" &&
4545+
providerSwitchPlan !== null &&
4546+
!providerSwitchPlan.instanceChanged &&
4547+
providerSwitchPlan.releaseProviderSessionIds.length === 0
4548+
) {
4549+
const { providerThread, providerTurn } = yield* projectionStore
4550+
.getRunningTurnContext(command.threadId)
4551+
.pipe(mapDispatchError(command));
4552+
const providerSessionId = providerThread?.providerSessionId;
4553+
if (providerThread !== undefined && providerTurn !== undefined && providerSessionId) {
4554+
const session = yield* providerSessions.get(providerSessionId).pipe(
4555+
Effect.mapError(
4556+
(cause) =>
4557+
new OrchestratorDispatchError({
4558+
commandId: command.commandId,
4559+
commandType: command.type,
4560+
cause,
4561+
}),
4562+
),
4563+
);
4564+
if (Option.isSome(session) && session.value.applyModelSelection !== undefined) {
4565+
yield* Ref.update(effects, (existing) => [
4566+
...existing,
4567+
{
4568+
id: `effect:${command.commandId}:provider-session.apply-model-selection`,
4569+
commandId: command.commandId,
4570+
threadId: command.threadId,
4571+
request: {
4572+
type: "provider-session.apply-model-selection",
4573+
providerSessionId,
4574+
providerThreadId: providerThread.id,
4575+
modelSelection: command.modelSelection,
4576+
},
4577+
} satisfies PendingOrchestrationEffectV2,
4578+
]);
4579+
}
4580+
}
4581+
}
4582+
45434583
if (command.type === "thread.metadata.update" && command.regenerateTitle === true) {
45444584
yield* Ref.update(effects, (existing) => [
45454585
...existing,

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { projectComposerContextForProvider } from "@t3tools/shared/composerContextReferences";
22
import {
33
MessageId,
4+
type ModelSelection,
45
type NodeId,
56
ProviderSessionId,
67
ProviderThreadId,
@@ -68,6 +69,18 @@ export interface ProviderTurnControlServiceV2Shape {
6869
readonly providerTurnId: ProviderTurnId;
6970
readonly messageId: MessageId;
7071
}) => Effect.Effect<void, ProviderTurnControlError>;
72+
/** Best effort: a turn that ended or moved sessions picks the selection up when it next starts. */
73+
readonly applyModelSelection: (input: {
74+
readonly threadId: ThreadId;
75+
readonly providerSessionId: ProviderSessionId;
76+
readonly providerThreadId: ProviderThreadId;
77+
readonly modelSelection: ModelSelection;
78+
}) => Effect.Effect<
79+
void,
80+
| ProjectionStore.ProjectionStoreV2Error
81+
| ProviderSessionManager.ProviderSessionManagerV2Error
82+
| ProviderAdapter.ProviderAdapterV2Error
83+
>;
7184
readonly interruptAndAwaitTerminal: (input: {
7285
readonly threadId: ThreadId;
7386
readonly providerSessionId: ProviderSessionId;
@@ -185,6 +198,26 @@ export const layer: Layer.Layer<
185198
});
186199

187200
return ProviderTurnControlServiceV2.of({
201+
applyModelSelection: Effect.fn("ProviderTurnControlService.applyModelSelection")(
202+
function* (input) {
203+
const { providerThread, providerTurn } = yield* projections.getRunningTurnContext(
204+
input.threadId,
205+
);
206+
if (
207+
providerThread?.id !== input.providerThreadId ||
208+
providerThread.providerSessionId !== input.providerSessionId ||
209+
providerTurn === undefined
210+
) {
211+
return;
212+
}
213+
const session = yield* sessions.get(input.providerSessionId);
214+
if (Option.isNone(session) || session.value.applyModelSelection === undefined) return;
215+
yield* session.value.applyModelSelection({
216+
providerThread,
217+
modelSelection: input.modelSelection,
218+
});
219+
},
220+
),
188221
interrupt: Effect.fn("ProviderTurnControlService.interrupt")(
189222
function* (input) {
190223
const loaded = yield* load({ ...input, operation: "interrupt" });

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,11 @@ export interface ProviderAdapterV2SessionRuntime {
676676
readonly steerTurn: (
677677
input: ProviderAdapterV2SteerInput,
678678
) => Effect.Effect<void, ProviderAdapterV2Error>;
679+
/** Applies a thread's new selection to its running process where the provider can; otherwise it waits for the next turn. */
680+
readonly applyModelSelection?: (input: {
681+
readonly providerThread: OrchestrationV2ProviderThread;
682+
readonly modelSelection: ModelSelection;
683+
}) => Effect.Effect<void, ProviderAdapterV2Error>;
679684
readonly stopTask?: (input: {
680685
readonly providerThread: OrchestrationV2ProviderThread;
681686
readonly taskId: string;

0 commit comments

Comments
 (0)