Skip to content

Commit 4d8285d

Browse files
t3dotggclaude
andcommitted
fix(server): changing Claude effort no longer stops background agents
Model, effort, ultracode and fast mode changes now apply to the live Claude process through setModel and applyFlagSettings. Only changes the CLI fixes at launch (thinking, tool or permission policy) still need a new process, and those are still refused while background work runs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9a5dc75 commit 4d8285d

4 files changed

Lines changed: 164 additions & 47 deletions

File tree

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

Lines changed: 72 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ const CLAUDE_TEST_MODEL_SELECTION = {
132132
model: "claude-sonnet-4-6",
133133
options: [{ id: "effort", value: "ultrathink" }],
134134
} satisfies ModelSelection;
135+
// Thinking is a launch flag, so switching to this selection needs a new process.
136+
const CLAUDE_TEST_RESTART_MODEL_SELECTION = {
137+
...CLAUDE_TEST_MODEL_SELECTION,
138+
model: "claude-haiku-4-5-20251001",
139+
options: [{ id: "thinking", value: false }],
140+
} satisfies ModelSelection;
135141
const CLAUDE_TEST_RUNTIME_POLICY = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
136142
runtimeMode: "full-access",
137143
interactionMode: "default",
@@ -1027,6 +1033,7 @@ describe("ClaudeAdapterV2 Auto-accept edits", () => {
10271033
messages: Stream.never,
10281034
offer: () => Effect.void,
10291035
setModel: () => Effect.void,
1036+
applyFlagSettings: () => Effect.void,
10301037
setPermissionMode: () => Effect.void,
10311038
stopTask: () => Effect.void,
10321039
interrupt: Effect.void,
@@ -1187,6 +1194,7 @@ const captureSdkExecutablePaths = Effect.fn("captureSdkExecutablePaths")(functio
11871194
messages: Stream.never,
11881195
offer: () => Effect.void,
11891196
setModel: () => Effect.void,
1197+
applyFlagSettings: () => Effect.void,
11901198
setPermissionMode: () => Effect.void,
11911199
stopTask: () => Effect.void,
11921200
interrupt: Effect.void,
@@ -1307,6 +1315,7 @@ describe("ClaudeAdapterV2 resume compaction", () => {
13071315
messages: Stream.never,
13081316
offer: () => Effect.void,
13091317
setModel: () => Effect.void,
1318+
applyFlagSettings: () => Effect.void,
13101319
setPermissionMode: () => Effect.void,
13111320
stopTask: () => Effect.void,
13121321
interrupt: Effect.void,
@@ -1533,6 +1542,7 @@ describe("ClaudeAdapterV2 attachments", () => {
15331542
offeredMessages.push(message);
15341543
}),
15351544
setModel: () => Effect.void,
1545+
applyFlagSettings: () => Effect.void,
15361546
setPermissionMode: () => Effect.void,
15371547
stopTask: () => Effect.void,
15381548
interrupt: Effect.void,
@@ -1678,6 +1688,7 @@ describe("ClaudeAdapterV2 attachments", () => {
16781688
messages: Stream.never,
16791689
offer: () => Effect.void,
16801690
setModel: () => Effect.void,
1691+
applyFlagSettings: () => Effect.void,
16811692
setPermissionMode: () => Effect.void,
16821693
stopTask: () => Effect.void,
16831694
interrupt: Effect.void,
@@ -1773,6 +1784,7 @@ describe("ClaudeAdapterV2 native fork", () => {
17731784
messages: Stream.empty,
17741785
offer: () => Effect.void,
17751786
setModel: () => Effect.void,
1787+
applyFlagSettings: () => Effect.void,
17761788
setPermissionMode: () => Effect.void,
17771789
stopTask: () => Effect.void,
17781790
interrupt: Effect.void,
@@ -2065,6 +2077,7 @@ describe("ClaudeAdapterV2 native session identity", () => {
20652077
messages: Stream.empty,
20662078
offer: () => Effect.void,
20672079
setModel: () => Effect.void,
2080+
applyFlagSettings: () => Effect.void,
20682081
setPermissionMode: () => Effect.void,
20692082
stopTask: () => Effect.void,
20702083
interrupt: Effect.void,
@@ -2356,6 +2369,8 @@ describe("ClaudeAdapterV2 background wake turns", () => {
23562369
});
23572370
const offeredMessages: Array<SDKUserMessage> = [];
23582371
const permissionModeChanges: Array<string> = [];
2372+
// setModel and applyFlagSettings calls on the first process, in order.
2373+
const liveSelectionChanges: Array<unknown> = [];
23592374
const continuationRequests: Array<ProviderContinuationRequests.ProviderContinuationRequest> =
23602375
[];
23612376
const continuationReceipts =
@@ -2403,6 +2418,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
24032418
offeredMessages.push(message);
24042419
}),
24052420
setModel: () => Effect.void,
2421+
applyFlagSettings: () => Effect.void,
24062422
setPermissionMode: () => Effect.void,
24072423
stopTask: options?.stopTask ?? (() => Effect.void),
24082424
interrupt: Effect.void,
@@ -2432,7 +2448,14 @@ describe("ClaudeAdapterV2 background wake turns", () => {
24322448
Effect.sync(() => {
24332449
offeredMessages.push(message);
24342450
}),
2435-
setModel: () => Effect.void,
2451+
setModel: (model) =>
2452+
Effect.sync(() => {
2453+
liveSelectionChanges.push({ model });
2454+
}),
2455+
applyFlagSettings: (settings) =>
2456+
Effect.sync(() => {
2457+
liveSelectionChanges.push(settings);
2458+
}),
24362459
setPermissionMode: (mode) =>
24372460
Effect.sync(() => {
24382461
permissionModeChanges.push(mode);
@@ -2503,6 +2526,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
25032526
return uuid;
25042527
},
25052528
permissionModeChanges,
2529+
liveSelectionChanges,
25062530
continuationRequests,
25072531
continuationReceipts,
25082532
events,
@@ -2627,7 +2651,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
26272651
);
26282652

26292653
it.effect(
2630-
"reuses a background shell's query for omitted and explicit Normal, but blocks Fast",
2654+
"applies model, effort and Fast changes to a process running a background shell, but not a thinking change",
26312655
() =>
26322656
Effect.scoped(
26332657
Effect.gen(function* () {
@@ -2648,31 +2672,46 @@ describe("ClaudeAdapterV2 background wake turns", () => {
26482672
providerTurnOrdinal: ordinal,
26492673
modelSelection,
26502674
});
2675+
const settle = Effect.gen(function* () {
2676+
yield* harness.offerAndWait(turnOneResult);
2677+
yield* Queue.take(harness.terminalReceipts);
2678+
});
26512679
yield* harness.runtime.startTurn(turn(1, normal));
26522680
const originalOptions = harness.getOpenedOptions();
26532681
yield* harness.offerAndWait(wakeTaskStarted);
2654-
yield* harness.offerAndWait(turnOneResult);
2655-
yield* Queue.take(harness.terminalReceipts);
2682+
yield* settle;
26562683
assert.isTrue(yield* harness.hasPendingBackgroundWork);
26572684

2685+
// Omitted and explicit Normal are the same selection.
2686+
yield* harness.runtime.startTurn(
2687+
turn(2, { ...normal, options: [{ id: "fastMode", value: false }] }),
2688+
);
2689+
yield* settle;
2690+
assert.deepEqual(harness.liveSelectionChanges, []);
2691+
26582692
yield* harness.runtime.startTurn(
2659-
turn(2, {
2693+
turn(3, {
26602694
...normal,
2661-
options: [{ id: "fastMode", value: false }],
2695+
options: [
2696+
{ id: "effort", value: "ultracode" },
2697+
{ id: "fastMode", value: true },
2698+
],
26622699
}),
26632700
);
2701+
yield* settle;
2702+
yield* harness.runtime.startTurn(turn(4, { ...normal, model: "claude-sonnet-4-6" }));
2703+
yield* settle;
2704+
assert.deepEqual(harness.liveSelectionChanges, [
2705+
{ effortLevel: "xhigh", fastMode: true, ultracode: true },
2706+
{ model: "claude-sonnet-4-6" },
2707+
// Sonnet's default effort; it has no Fast mode.
2708+
{ effortLevel: "high", fastMode: null, ultracode: null },
2709+
]);
26642710
assert.strictEqual(harness.getOpenedOptions(), originalOptions);
2665-
assert.lengthOf(harness.offeredMessages, 2);
2666-
yield* harness.offerAndWait(turnOneResult);
2667-
yield* Queue.take(harness.terminalReceipts);
2711+
assert.lengthOf(harness.offeredMessages, 4);
26682712

26692713
const refused = yield* harness.runtime
2670-
.startTurn(
2671-
turn(3, {
2672-
...normal,
2673-
options: [{ id: "fastMode", value: true }],
2674-
}),
2675-
)
2714+
.startTurn(turn(5, CLAUDE_TEST_RESTART_MODEL_SELECTION))
26762715
.pipe(Effect.result);
26772716
assert.equal(refused._tag, "Failure");
26782717
if (refused._tag === "Failure") {
@@ -2682,7 +2721,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
26822721
);
26832722
}
26842723
assert.strictEqual(harness.getOpenedOptions(), originalOptions);
2685-
assert.lengthOf(harness.offeredMessages, 2);
2724+
assert.lengthOf(harness.offeredMessages, 4);
26862725
assert.isTrue(yield* harness.hasPendingBackgroundWork);
26872726
}).pipe(
26882727
Effect.provide(
@@ -4948,6 +4987,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
49484987
messages: Stream.fromQueue(sdkMessages),
49494988
offer: () => Effect.void,
49504989
setModel: () => Effect.void,
4990+
applyFlagSettings: () => Effect.void,
49514991
setPermissionMode: () => Effect.void,
49524992
stopTask: () => Effect.void,
49534993
interrupt: Effect.void,
@@ -5208,6 +5248,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
52085248
messages: Stream.fromQueue(queue),
52095249
offer: () => Effect.void,
52105250
setModel: () => Effect.void,
5251+
applyFlagSettings: () => Effect.void,
52115252
setPermissionMode: () => Effect.void,
52125253
stopTask: () => Effect.void,
52135254
interrupt: Effect.void,
@@ -10299,6 +10340,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1029910340
messages: Stream.fromQueue(sdkMessages),
1030010341
offer: () => Effect.void,
1030110342
setModel: () => Effect.void,
10343+
applyFlagSettings: () => Effect.void,
1030210344
setPermissionMode: () => Effect.void,
1030310345
stopTask: () => Effect.void,
1030410346
interrupt: Effect.void,
@@ -10370,10 +10412,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1037010412
);
1037110413
assert.isFalse(yield* hasPendingBackgroundWork);
1037210414

10373-
const alternateModel = {
10374-
...CLAUDE_TEST_MODEL_SELECTION,
10375-
model: "claude-haiku-4-5-20251001",
10376-
} satisfies ModelSelection;
10415+
const alternateModel = CLAUDE_TEST_RESTART_MODEL_SELECTION;
1037710416
// ProviderTurnStartService marks the thread active before startTurn;
1037810417
// the process-reset clear must preserve that status.
1037910418
const activeProviderThread = {
@@ -10489,6 +10528,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1048910528
messages: Stream.fromQueue(sdkMessages),
1049010529
offer: () => Effect.void,
1049110530
setModel: () => Effect.void,
10531+
applyFlagSettings: () => Effect.void,
1049210532
setPermissionMode: () => Effect.void,
1049310533
stopTask: () => Effect.void,
1049410534
interrupt: Effect.void,
@@ -10556,10 +10596,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1055610596

1055710597
// User turn changes model, replacing the query while the wake buffer
1055810598
// stays queued for the later provider continuation.
10559-
const alternateModel = {
10560-
...CLAUDE_TEST_MODEL_SELECTION,
10561-
model: "claude-haiku-4-5-20251001",
10562-
} satisfies ModelSelection;
10599+
const alternateModel = CLAUDE_TEST_RESTART_MODEL_SELECTION;
1056310600
yield* runtime.startTurn(
1056410601
makeClaudeTestTurnInput({
1056510602
threadId,
@@ -10731,6 +10768,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1073110768
messages: Stream.fromQueue(sdkMessages),
1073210769
offer: () => Effect.void,
1073310770
setModel: () => Effect.void,
10771+
applyFlagSettings: () => Effect.void,
1073410772
setPermissionMode: () => Effect.void,
1073510773
stopTask: () => Effect.void,
1073610774
interrupt: Effect.void,
@@ -10818,10 +10856,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1081810856
// Model-changing user turn replaces the query while continuation stays
1081910857
// queued. Process reset must not invent opaque classification for the
1082010858
// buffered subagent notification.
10821-
const alternateModel = {
10822-
...CLAUDE_TEST_MODEL_SELECTION,
10823-
model: "claude-haiku-4-5-20251001",
10824-
} satisfies ModelSelection;
10859+
const alternateModel = CLAUDE_TEST_RESTART_MODEL_SELECTION;
1082510860
yield* runtime.startTurn(
1082610861
makeClaudeTestTurnInput({
1082710862
threadId,
@@ -10900,7 +10935,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1090010935
),
1090110936
);
1090210937

10903-
it.effect("refuses a model change that would kill a running background subagent", () =>
10938+
it.effect("refuses a thinking change that would kill a running background subagent", () =>
1090410939
Effect.scoped(
1090510940
Effect.gen(function* () {
1090610941
const SUBAGENT_TASK_ID = "task-model-change-running-subagent";
@@ -10932,6 +10967,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1093210967
messages: Stream.fromQueue(sdkMessages),
1093310968
offer: () => Effect.void,
1093410969
setModel: () => Effect.void,
10970+
applyFlagSettings: () => Effect.void,
1093510971
setPermissionMode: () => Effect.void,
1093610972
stopTask: () => Effect.void,
1093710973
interrupt: Effect.void,
@@ -11004,12 +11040,9 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1100411040
yield* awaitUntil(() => terminals().length === 1, "first turn terminal");
1100511041
const settledTurn = terminals()[0]!;
1100611042

11007-
// The subagent runs inside the first CLI process. Another model needs
11008-
// another process, so the turn must not start and close this one.
11009-
const alternateModel = {
11010-
...CLAUDE_TEST_MODEL_SELECTION,
11011-
model: "claude-haiku-4-5-20251001",
11012-
} satisfies ModelSelection;
11043+
// The subagent runs inside the first CLI process. A thinking change
11044+
// needs another process, so the turn must not start and close this one.
11045+
const alternateModel = CLAUDE_TEST_RESTART_MODEL_SELECTION;
1101311046
const switchTurn = (attempt: string) =>
1101411047
runtime.startTurn(
1101511048
makeClaudeTestTurnInput({
@@ -11115,6 +11148,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1111511148
messages: Stream.fromQueue(sdkMessages),
1111611149
offer: () => Effect.void,
1111711150
setModel: () => Effect.void,
11151+
applyFlagSettings: () => Effect.void,
1111811152
setPermissionMode: () => Effect.void,
1111911153
stopTask: () => Effect.void,
1112011154
interrupt: Effect.void,
@@ -11174,10 +11208,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1117411208
);
1117511209
assert.isTrue(yield* hasPendingBackgroundWork);
1117611210

11177-
const alternateModel = {
11178-
...CLAUDE_TEST_MODEL_SELECTION,
11179-
model: "claude-haiku-4-5-20251001",
11180-
} satisfies ModelSelection;
11211+
const alternateModel = CLAUDE_TEST_RESTART_MODEL_SELECTION;
1118111212
const failedStart = yield* runtime
1118211213
.startTurn(
1118311214
makeClaudeTestTurnInput({
@@ -11254,6 +11285,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1125411285
messages: Stream.fromQueue(sdkMessages),
1125511286
offer: () => Effect.void,
1125611287
setModel: () => Effect.void,
11288+
applyFlagSettings: () => Effect.void,
1125711289
setPermissionMode: () => Effect.void,
1125811290
stopTask: () => Effect.void,
1125911291
interrupt: Effect.void,
@@ -11317,10 +11349,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1131711349
yield* awaitUntil(() => continuationRequests.length === 1, "first continuation request");
1131811350
assert.isTrue(yield* hasPendingBackgroundWork);
1131911351

11320-
const alternateModel = {
11321-
...CLAUDE_TEST_MODEL_SELECTION,
11322-
model: "claude-haiku-4-5-20251001",
11323-
} satisfies ModelSelection;
11352+
const alternateModel = CLAUDE_TEST_RESTART_MODEL_SELECTION;
1132411353
const failedStart = yield* runtime
1132511354
.startTurn(
1132611355
makeClaudeTestTurnInput({

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ type ClaudeOutboundFrame =
246246
| ClaudeQueryOpenFrame
247247
| ClaudePromptOfferFrame
248248
| ClaudeQuerySetModelFrame
249+
| {
250+
readonly type: "query.apply_flag_settings";
251+
readonly settings: Parameters<
252+
ClaudeAdapterV2.ClaudeAgentSdkQuerySession["applyFlagSettings"]
253+
>[0];
254+
}
249255
| ClaudeQuerySetPermissionModeFrame
250256
| ClaudeQueryInterruptFrame
251257
| ClaudePermissionResponseFrame
@@ -819,6 +825,13 @@ function makeReplayQueryRunner(
819825
model,
820826
});
821827
}),
828+
applyFlagSettings: (settings) =>
829+
replayEffect(() => {
830+
assertNextOutboundFrame({
831+
type: "query.apply_flag_settings",
832+
settings,
833+
});
834+
}),
822835
setPermissionMode: (mode) =>
823836
replayEffect(() => {
824837
assertNextOutboundFrame({

0 commit comments

Comments
 (0)