Skip to content

Commit 7ba9e2d

Browse files
committed
fix(server): Claude usage and subagent effort follow the running process
A new turn that reuses the process with the same query identity kept the selection the process opened with, so usage reported a stale context window after a catalog refresh while getModelContextWindow reported the new one. The reused process now takes the turn's compiled selection. A continuation keeps its process whatever the turn's selection, but a subagent it started inherited the turn's effort. The turn context now takes its selection from the process it runs on.
1 parent 115950f commit 7ba9e2d

2 files changed

Lines changed: 153 additions & 16 deletions

File tree

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

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,6 +2791,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
27912791
runtime: {
27922792
...entry.runtime,
27932793
modelSuffixes: { contextWindow: { "1m": "[1m]", large: "[1m]" } },
2794+
contextWindowTokens: { ...entry.runtime.contextWindowTokens, large: 1_000_000 },
27942795
},
27952796
}));
27962797
const liveCatalog = yield* Ref.make(BUNDLED_CLAUDE_MODEL_CATALOG);
@@ -2914,6 +2915,101 @@ describe("ClaudeAdapterV2 background wake turns", () => {
29142915
),
29152916
);
29162917

2918+
it.effect("gives a continuation's subagent the effort its process runs at", () =>
2919+
Effect.gen(function* () {
2920+
const AGENT_TOOL_USE_ID = "toolu-continuation-effort";
2921+
const liveCatalog = yield* Ref.make(withSonnetDefaults({ effort: "low" }));
2922+
const harness = yield* makeWakeHarnessWithOptions({ modelCatalog: Ref.get(liveCatalog) });
2923+
const now = yield* DateTime.now;
2924+
yield* harness.runtime.startTurn(
2925+
makeClaudeTestTurnInput({
2926+
threadId: harness.threadId,
2927+
providerThread: harness.providerThread,
2928+
now,
2929+
attemptId: RunAttemptId.make("continuation-effort-1"),
2930+
text: "Run the build in the background.",
2931+
attachments: [],
2932+
modelSelection: sonnetSelection,
2933+
}),
2934+
);
2935+
yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
2936+
yield* Queue.offer(harness.sdkMessages, turnOneResult);
2937+
yield* Queue.take(harness.terminalReceipts);
2938+
yield* harness.offerAndWait(wakeNotification);
2939+
yield* harness.offerAndWait(wakeTurnInit);
2940+
2941+
yield* Ref.set(liveCatalog, withSonnetDefaults({ effort: "medium" }));
2942+
yield* harness.runtime.startTurn(
2943+
makeClaudeTestTurnInput({
2944+
threadId: harness.threadId,
2945+
providerThread: harness.providerThread,
2946+
now,
2947+
attemptId: RunAttemptId.make("continuation-effort-2"),
2948+
text: "Background task completed.",
2949+
attachments: [],
2950+
modelSelection: sonnetSelection,
2951+
providerTurnOrdinal: 2,
2952+
messageCreatedBy: "agent",
2953+
messageCreationSource: "provider",
2954+
}),
2955+
);
2956+
for (const frame of [
2957+
claudeSdkFrame({
2958+
type: "assistant",
2959+
parent_tool_use_id: null,
2960+
message: {
2961+
model: sonnetSelection.model,
2962+
id: "msg_continuation_effort_launch",
2963+
type: "message",
2964+
role: "assistant",
2965+
content: [
2966+
{
2967+
type: "tool_use",
2968+
id: AGENT_TOOL_USE_ID,
2969+
name: "Agent",
2970+
input: {
2971+
description: "Review",
2972+
subagent_type: "general-purpose",
2973+
prompt: "Review the build output.",
2974+
},
2975+
},
2976+
],
2977+
},
2978+
uuid: "00000000-0000-4000-8000-000000000231",
2979+
session_id: WAKE_NATIVE_SESSION,
2980+
}),
2981+
claudeSdkFrame({
2982+
type: "system",
2983+
subtype: "task_started",
2984+
task_id: "task-continuation-effort",
2985+
tool_use_id: AGENT_TOOL_USE_ID,
2986+
description: "Review",
2987+
task_type: "local_agent",
2988+
uuid: "00000000-0000-4000-8000-000000000232",
2989+
session_id: WAKE_NATIVE_SESSION,
2990+
}),
2991+
wakeResult,
2992+
]) {
2993+
yield* Queue.offer(harness.sdkMessages, frame);
2994+
}
2995+
yield* Queue.take(harness.terminalReceipts);
2996+
2997+
assert.equal(harness.getOpenedOptions()?.effort, "low");
2998+
const subagent = harness.events.findLast((event) => event.type === "subagent.updated");
2999+
assert.equal(
3000+
subagent?.type === "subagent.updated"
3001+
? getModelSelectionStringOptionValue(subagent.subagent.modelSelection, "effort")
3002+
: undefined,
3003+
"low",
3004+
);
3005+
}).pipe(
3006+
Effect.scoped,
3007+
Effect.provide(
3008+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3009+
),
3010+
),
3011+
);
3012+
29173013
it.effect("reports the window a live model change switched the process to", () =>
29183014
Effect.gen(function* () {
29193015
const harness = yield* makeWakeHarness;
@@ -2971,6 +3067,44 @@ describe("ClaudeAdapterV2 background wake turns", () => {
29713067
),
29723068
);
29733069

3070+
it.effect("reports a refreshed window on a new turn that reuses the process", () =>
3071+
Effect.gen(function* () {
3072+
const liveCatalog = yield* Ref.make(BUNDLED_CLAUDE_MODEL_CATALOG);
3073+
const harness = yield* makeWakeHarnessWithOptions({ modelCatalog: Ref.get(liveCatalog) });
3074+
yield* sonnetTurn(harness, "refreshed-window-a", 1, []);
3075+
const firstOptions = harness.getOpenedOptions();
3076+
yield* Queue.offer(
3077+
harness.sdkMessages,
3078+
makeResultFrame({ uuid: "refreshed-window-a-result", result: "First" }),
3079+
);
3080+
yield* Queue.take(harness.terminalReceipts);
3081+
3082+
yield* Ref.set(
3083+
liveCatalog,
3084+
mapSonnetEntry((entry) => ({
3085+
...entry,
3086+
runtime: { ...entry.runtime, fixedContextWindowTokens: 400_000 },
3087+
})),
3088+
);
3089+
yield* sonnetTurn(harness, "refreshed-window-b", 2, []);
3090+
assert.strictEqual(harness.getOpenedOptions(), firstOptions);
3091+
assert.equal(harness.runtime.getModelContextWindow?.(sonnetSelection), 400_000);
3092+
assert.equal(
3093+
yield* compactUsageMaxTokens(
3094+
harness,
3095+
"refreshed-window-compact",
3096+
makeResultFrame({ uuid: "refreshed-window-b-result", result: "Second" }),
3097+
),
3098+
400_000,
3099+
);
3100+
}).pipe(
3101+
Effect.scoped,
3102+
Effect.provide(
3103+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3104+
),
3105+
),
3106+
);
3107+
29743108
it.effect.each([
29753109
{ isError: false, title: "Check weather" },
29763110
{ isError: true, title: "Check weather" },

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,6 +2958,7 @@ interface ActiveClaudeTurnContext {
29582958
readonly nativeThreadId: string;
29592959
// The catalog this turn compiled against. Its steers and subagents reuse it.
29602960
readonly modelCatalog: ClaudeModelCatalog;
2961+
// What the turn's process runs. A continuation keeps an older process's selection.
29612962
readonly selection: CompiledClaudeModelSelection;
29622963
readonly nativeTurnId: string;
29632964
nativeMessageCursor: string | null;
@@ -8088,6 +8089,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
80888089
existing.queryPolicyKey === queryPolicyKey &&
80898090
existing.selection.queryIdentity === compiledSelection.queryIdentity
80908091
) {
8092+
// Same identity, but a refreshed catalog can still change the window.
8093+
existing.selection = compiledSelection;
80918094
// Claude can switch its own mode mid-session (EnterPlanMode), and
80928095
// a denied ExitPlanMode leaves it there. Put the live process back
80938096
// in the thread's mode before the next prompt.
@@ -8302,11 +8305,26 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
83028305
if (!goalsByNativeThread.has(nativeThreadId)) {
83038306
goalsByNativeThread.set(nativeThreadId, turnInput.providerThread.goal ?? null);
83048307
}
8308+
const userMessage =
8309+
promptUuid === null
8310+
? null
8311+
: yield* makeClaudeUserMessageWithAttachments({
8312+
text: applyClaudePromptEffortPrefix(
8313+
turnInput.message.text,
8314+
selection.promptEffort,
8315+
),
8316+
attachments: turnInput.message.attachments,
8317+
attachmentsDir,
8318+
fileSystem,
8319+
skillNames: yield* userInvocableSkillNames(turnInput.runtimePolicy.cwd),
8320+
uuid: promptUuid,
8321+
});
8322+
const querySession = yield* openQuery(turnInput, nativeThreadId, modelCatalog);
83058323
const context: ActiveClaudeTurnContext = {
83068324
input: turnInput,
83078325
nativeThreadId,
83088326
modelCatalog,
8309-
selection,
8327+
selection: querySession.selection,
83108328
nativeTurnId,
83118329
nativeMessageCursor: null,
83128330
providerTurnId,
@@ -8344,21 +8362,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
83448362
gatedFramesBeforeEcho: 0,
83458363
heldRootFrames: [],
83468364
};
8347-
const userMessage =
8348-
promptUuid === null
8349-
? null
8350-
: yield* makeClaudeUserMessageWithAttachments({
8351-
text: applyClaudePromptEffortPrefix(
8352-
turnInput.message.text,
8353-
selection.promptEffort,
8354-
),
8355-
attachments: turnInput.message.attachments,
8356-
attachmentsDir,
8357-
fileSystem,
8358-
skillNames: yield* userInvocableSkillNames(turnInput.runtimePolicy.cwd),
8359-
uuid: promptUuid,
8360-
});
8361-
const querySession = yield* openQuery(turnInput, nativeThreadId, modelCatalog);
83628365
const attachTurn = Effect.gen(function* () {
83638366
querySession.lastTurn = context;
83648367
yield* Ref.set(activeTurn, context);

0 commit comments

Comments
 (0)