The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 22:15 UTC
fix(server): Claude effort changes apply to the running turn (#34563)
Julius Marminge committed
Nov 11, 2026 at 14:03 UTC
d6e5f2e06faf45eb46ee5999ab1363daaea4e1c2
9 files changed
+270
-18
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+48
-18
@@ -11148,6 +11148,29 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
11148
return false;
11149
});
11150
11151
+ const applySelectionLive = Effect.fnUntraced(function* (
11152
+ live: ClaudeLiveQueryContext,
11153
+ selection: CompiledClaudeModelSelection,
11154
+ ) {
11155
+ // Preserve launcher-selected models unless the model changed or a failed update left it unknown.
11156
+ yield* (
11157
+ live.selection.queryIdentity === "" ||
11158
+ live.selection.apiModelId !== selection.apiModelId
11159
+ ? live.query.setModel(selection.apiModelId)
11160
+ : Effect.void
11161
+ ).pipe(
11162
+ Effect.andThen(
11163
+ live.query.applyFlagSettings(claudeLiveSelectionFlagSettings(selection)),
11164
+ ),
11165
+ Effect.tapError(() =>
11166
+ Effect.sync(() => {
11167
+ live.selection = { ...live.selection, queryIdentity: "" };
11168
+ }),
11169
+ ),
11170
+ );
11171
+ live.selection = selection;
11172
+ });
11173
+
11174
const openQuery = Effect.fnUntraced(function* (
11175
turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
11176
nativeThreadId: string,
@@ -11188,24 +11211,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
11211
(existing.selection.apiModelId === compiledSelection.apiModelId ||
11212
existing.openedPermissionMode === queryPolicy.permissionMode)
11213
) {
11191
- const live = existing;
11192
- // Preserve launcher-selected models unless the model changed or a failed update left it unknown.
11193
- yield* (
11194
- live.selection.queryIdentity === "" ||
11195
- live.selection.apiModelId !== compiledSelection.apiModelId
11196
- ? live.query.setModel(compiledSelection.apiModelId)
11197
- : Effect.void
11198
- ).pipe(
11199
- Effect.andThen(
11200
- live.query.applyFlagSettings(claudeLiveSelectionFlagSettings(compiledSelection)),
11201
- ),
11202
- Effect.tapError(() =>
11203
- Effect.sync(() => {
11204
- live.selection = { ...live.selection, queryIdentity: "" };
11205
- }),
11206
- ),
11207
- );
11208
- live.selection = compiledSelection;
11214
+ yield* applySelectionLive(existing, compiledSelection);
11215
}
11216
if (
11217
existing !== null &&
@@ -12403,6 +12409,30 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12409
);
12410
}),
12411
},
12412
+ applyModelSelection: Effect.fn("ClaudeAdapterV2.applyModelSelection")(function* ({
12413
+ providerThread,
12414
+ modelSelection,
12415
+ }) {
12416
+ const live = yield* Ref.get(queryContext);
12417
+ const selection = compileClaudeModelSelection(modelSelection, modelCatalog);
12418
+ // A model change still waits for the next turn, which checks it against the turn's permission mode.
12419
+ if (
12420
+ live === null ||
12421
+ live.stopping ||
12422
+ live.nativeThreadId !== providerThread.nativeThreadRef?.nativeId ||
12423
+ live.selection.queryIdentity === selection.queryIdentity ||
12424
+ live.selection.apiModelId !== selection.apiModelId ||
12425
+ !canApplyClaudeSelectionLive(live.selection, selection)
12426
+ ) {
12427
+ return;
12428
+ }
12429
+ // A failed update clears the live identity, so the next turn applies the selection again.
12430
+ yield* applySelectionLive(live, selection).pipe(
12431
+ Effect.catch((cause) =>
12432
+ Effect.logWarning("orchestration-v2.claude-live-selection-failed", { cause }),
12433
+ ),
12434
+ );
12435
+ }),
12436
stopTask: Effect.fn("ClaudeAdapterV2.stopTask")(function* ({ providerThread, taskId }) {
12437
const liveQuery = yield* Ref.get(queryContext);
12438
const task = (yield* Ref.get(sessionSubagentsByTaskId)).get(taskId);
apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts
+116
@@ -494,3 +494,119 @@ it.effect.each(deliveries)(
494
),
495
),
496
);
497
+
498
+it.effect("applies an effort change to the running Claude query without waiting for a turn", () =>
499
+ Effect.scoped(
500
+ Effect.gen(function* () {
501
+ const cwd = yield* checkpointWorkspace("claude-live-effort");
502
+ const sdkMessages = yield* Queue.unbounded<SDKMessage>();
503
+ const flagSettings: unknown[] = [];
504
+ const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
505
+ instanceId: modelSelection.instanceId,
506
+ settings,
507
+ environment: {},
508
+ attachmentsDir: cwd,
509
+ fileSystem: yield* FileSystem.FileSystem,
510
+ path: yield* Path.Path,
511
+ random: yield* SecureRandom.SecureRandom,
512
+ idAllocator: yield* IdAllocator.IdAllocatorV2,
513
+ queryRunner: {
514
+ allocateSessionId: Effect.succeed(sessionId),
515
+ open: () =>
516
+ Effect.succeed({
517
+ messages: Stream.fromQueue(sdkMessages),
518
+ offer: () => Effect.void,
519
+ setModel: () => Effect.void,
520
+ applyFlagSettings: (flags) =>
521
+ Effect.sync(() => {
522
+ flagSettings.push(flags);
523
+ }),
524
+ setPermissionMode: () => Effect.void,
525
+ backgroundTasks: () => Effect.succeed(false),
526
+ mcpServerStatus: Effect.succeed([]),
527
+ readMcpResource: () => Effect.die("unexpected MCP resource read"),
528
+ callMcpTool: () => Effect.die("unexpected MCP tool call"),
529
+ listPermissionRules: Effect.die("unexpected permission rules read"),
530
+ stopTask: () => Effect.void,
531
+ interrupt: Effect.die("an effort change must not interrupt the turn"),
532
+ close: Effect.void,
533
+ }),
534
+ forkSession: () => Effect.die("unused"),
535
+ subagentLaunchToolUseId: () => Effect.succeed(null),
536
+ assertComplete: Effect.void,
537
+ },
538
+ });
539
+ yield* Effect.gen(function* () {
540
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
541
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
542
+ const threadId = ThreadId.make("thread:live-effort");
543
+ const high = { ...modelSelection, options: [{ id: "effort", value: "high" }] };
544
+ const running = yield* orchestrator.streamDomainEvents.pipe(
545
+ Stream.filter(
546
+ (event) => event.type === "provider-turn.updated" && event.payload.status === "running",
547
+ ),
548
+ Stream.take(1),
549
+ Stream.runDrain,
550
+ Effect.forkScoped,
551
+ );
552
+ yield* orchestrator.dispatch({
553
+ type: "thread.create",
554
+ commandId: CommandId.make("create"),
555
+ threadId,
556
+ projectId: ProjectId.make("project:live-effort"),
557
+ title: "Live effort",
558
+ modelSelection: high,
559
+ runtimeMode: "full-access",
560
+ interactionMode: "default",
561
+ branch: null,
562
+ worktreePath: cwd,
563
+ createdBy: "user",
564
+ creationSource: "web",
565
+ });
566
+ yield* orchestrator.dispatch({
567
+ type: "message.dispatch",
568
+ commandId: CommandId.make("first"),
569
+ threadId,
570
+ messageId: MessageId.make("first"),
571
+ text: "Keep working.",
572
+ attachments: [],
573
+ dispatchMode: { type: "start_immediately" },
574
+ createdBy: "user",
575
+ creationSource: "web",
576
+ });
577
+ yield* worker.drain();
578
+ yield* Fiber.join(running);
579
+ assert.deepEqual(flagSettings, []);
580
+
581
+ yield* orchestrator.dispatch({
582
+ type: "thread.model-selection.set",
583
+ commandId: CommandId.make("medium"),
584
+ threadId,
585
+ modelSelection: { ...modelSelection, options: [{ id: "effort", value: "medium" }] },
586
+ });
587
+ yield* worker.drain();
588
+
589
+ assert.deepEqual(flagSettings, [
590
+ { effortLevel: "medium", fastMode: null, ultracode: null },
591
+ ]);
592
+ }).pipe(
593
+ Effect.provide(
594
+ ProviderReplayHarness.layerWithRegistry(
595
+ { name: "claude-live-effort" },
596
+ ProviderAdapterRegistry.layerSingle(adapter),
597
+ { runEffectWorker: false },
598
+ ),
599
+ ),
600
+ );
601
+ }).pipe(
602
+ Effect.provide(
603
+ Layer.mergeAll(
604
+ IdAllocator.layer,
605
+ McpProviderSessions.layer,
606
+ NodeServices.layer,
607
+ NodeCrypto.layer,
608
+ ),
609
+ ),
610
+ ),
611
+ ),
612
+);
apps/server/src/orchestration-v2/EffectOutbox.ts
+8
@@ -3,6 +3,7 @@ import {
3
CheckpointScopeId,
4
CommandId,
5
MessageId,
6
+ ModelSelection,
7
NodeId,
8
ProviderSessionId,
9
RunAttemptId,
@@ -40,6 +41,12 @@ export const OrchestrationEffectRequestV2 = Schema.Union([
41
detail: Schema.optional(Schema.String),
42
revokeMcpCredential: Schema.optional(Schema.Boolean),
43
}),
44
+ Schema.Struct({
45
+ type: Schema.Literal("provider-session.apply-model-selection"),
46
+ providerSessionId: ProviderSessionId,
47
+ providerThreadId: ProviderThreadId,
48
+ modelSelection: ModelSelection,
49
+ }),
50
Schema.Struct({
51
type: Schema.Literal("provider-turn.start"),
52
runId: RunId,
@@ -136,6 +143,7 @@ export const REPLAY_SAFE_EFFECT_TYPES_AFTER_PROCESS_LOSS = [
143
] as const satisfies ReadonlyArray<OrchestrationEffectRequestV2["type"]>;
144
145
export const PROCESS_BOUND_EFFECT_TYPES = [
146
+ "provider-session.apply-model-selection",
147
"provider-turn.start",
148
"provider-turn.interrupt",
149
"provider-turn.steer",
apps/server/src/orchestration-v2/EffectWorker.test.ts
+1
@@ -93,6 +93,7 @@ function layerExecutorFor(input: {
93
ProviderTurnControlService.ProviderTurnControlServiceV2.of({
94
interrupt: input.interrupt ?? (() => Effect.void),
95
steer: () => Effect.void,
96
+ applyModelSelection: () => Effect.void,
97
interruptAndAwaitTerminal: (request) =>
98
record(
99
request.replacementProviderSessionId === undefined
apps/server/src/orchestration-v2/EffectWorker.ts
+18
@@ -138,6 +138,24 @@ export const layerExecutor: Layer.Layer<
138
}),
139
),
140
);
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
+ );
159
case "provider-turn.start":
160
return providerTurnStart
161
.start({ threadId: effect.threadId, runId: effect.request.runId, willRetry })
apps/server/src/orchestration-v2/Orchestrator.ts
+40
@@ -4624,6 +4624,46 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
4624
// Archiving ends the guest share, so its waiting files go too.
4625
yield* releaseGuestAttachments(command, thread.guestShare, updatedThread.guestShare, effects);
4626
4627
+ if (
4628
+ command.type === "thread.model-selection.set" &&
4629
+ providerSwitchPlan !== null &&
4630
+ !providerSwitchPlan.instanceChanged &&
4631
+ providerSwitchPlan.releaseProviderSessionIds.length === 0
4632
+ ) {
4633
+ const { providerThread, providerTurn } = yield* projectionStore
4634
+ .getRunningTurnContext(command.threadId)
4635
+ .pipe(mapDispatchError(command));
4636
+ const providerSessionId = providerThread?.providerSessionId;
4637
+ if (providerThread !== undefined && providerTurn !== undefined && providerSessionId) {
4638
+ const session = yield* providerSessions.get(providerSessionId).pipe(
4639
+ Effect.mapError(
4640
+ (cause) =>
4641
+ new OrchestratorDispatchError({
4642
+ commandId: command.commandId,
4643
+ commandType: command.type,
4644
+ cause,
4645
+ }),
4646
+ ),
4647
+ );
4648
+ if (Option.isSome(session) && session.value.applyModelSelection !== undefined) {
4649
+ yield* Ref.update(effects, (existing) => [
4650
+ ...existing,
4651
+ {
4652
+ id: `effect:${command.commandId}:provider-session.apply-model-selection`,
4653
+ commandId: command.commandId,
4654
+ threadId: command.threadId,
4655
+ request: {
4656
+ type: "provider-session.apply-model-selection",
4657
+ providerSessionId,
4658
+ providerThreadId: providerThread.id,
4659
+ modelSelection: command.modelSelection,
4660
+ },
4661
+ } satisfies PendingOrchestrationEffectV2,
4662
+ ]);
4663
+ }
4664
+ }
4665
+ }
4666
+
4667
if (command.type === "thread.metadata.update" && command.regenerateTitle === true) {
4668
yield* Ref.update(effects, (existing) => [
4669
...existing,
apps/server/src/orchestration-v2/ProviderRuntimeRecoveryService.test.ts
+1
@@ -771,6 +771,7 @@ it.effect(
771
assert.equal(summary.stoppedSessions, 1);
772
assert.equal(summary.retiredEffects, 2);
773
assert.deepEqual(committedInput?.cancelUnsettledEffects?.effectTypes, [
774
+ "provider-session.apply-model-selection",
775
"provider-turn.start",
776
"provider-turn.interrupt",
777
"provider-turn.steer",
apps/server/src/orchestration-v2/ProviderTurnControlService.ts
+33
@@ -1,6 +1,7 @@
1
import { projectComposerContextForProvider } from "@t3tools/shared/composerContextReferences";
2
import {
3
MessageId,
4
+ type ModelSelection,
5
type NodeId,
6
ProviderSessionId,
7
ProviderThreadId,
@@ -68,6 +69,18 @@ export interface ProviderTurnControlServiceV2Shape {
69
readonly providerTurnId: ProviderTurnId;
70
readonly messageId: MessageId;
71
}) => 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
+ >;
84
readonly interruptAndAwaitTerminal: (input: {
85
readonly threadId: ThreadId;
86
readonly providerSessionId: ProviderSessionId;
@@ -185,6 +198,26 @@ export const layer: Layer.Layer<
198
});
199
200
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
+ ),
221
interrupt: Effect.fn("ProviderTurnControlService.interrupt")(
222
function* (input) {
223
const loaded = yield* load({ ...input, operation: "interrupt" });
packages/provider-core/src/server/ProviderAdapter.ts
+5
@@ -676,6 +676,11 @@ export interface ProviderAdapterV2SessionRuntime {
676
readonly steerTurn: (
677
input: ProviderAdapterV2SteerInput,
678
) => 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>;
684
readonly stopTask?: (input: {
685
readonly providerThread: OrchestrationV2ProviderThread;
686
readonly taskId: string;