Skip to content

Commit d6e5f2e

Browse files
fix(server): Claude effort changes apply to the running turn (#34563)
1 parent a02bc75 commit d6e5f2e

9 files changed

Lines changed: 270 additions & 18 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
@@ -11148,6 +11148,29 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1114811148
return false;
1114911149
});
1115011150

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+
1115111174
const openQuery = Effect.fnUntraced(function* (
1115211175
turnInput: ProviderAdapter.ProviderAdapterV2TurnInput,
1115311176
nativeThreadId: string,
@@ -11188,24 +11211,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1118811211
(existing.selection.apiModelId === compiledSelection.apiModelId ||
1118911212
existing.openedPermissionMode === queryPolicy.permissionMode)
1119011213
) {
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);
1120911215
}
1121011216
if (
1121111217
existing !== null &&
@@ -12403,6 +12409,30 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1240312409
);
1240412410
}),
1240512411
},
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+
}),
1240612436
stopTask: Effect.fn("ClaudeAdapterV2.stopTask")(function* ({ providerThread, taskId }) {
1240712437
const liveQuery = yield* Ref.get(queryContext);
1240812438
const task = (yield* Ref.get(sessionSubagentsByTaskId)).get(taskId);

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

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,3 +494,119 @@ it.effect.each(deliveries)(
494494
),
495495
),
496496
);
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‎

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
@@ -4624,6 +4624,46 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
46244624
// Archiving ends the guest share, so its waiting files go too.
46254625
yield* releaseGuestAttachments(command, thread.guestShare, updatedThread.guestShare, effects);
46264626

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+
46274667
if (command.type === "thread.metadata.update" && command.regenerateTitle === true) {
46284668
yield* Ref.update(effects, (existing) => [
46294669
...existing,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,7 @@ it.effect(
771771
assert.equal(summary.stoppedSessions, 1);
772772
assert.equal(summary.retiredEffects, 2);
773773
assert.deepEqual(committedInput?.cancelUnsettledEffects?.effectTypes, [
774+
"provider-session.apply-model-selection",
774775
"provider-turn.start",
775776
"provider-turn.interrupt",
776777
"provider-turn.steer",

‎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)