Skip to content

Commit 3a19fd0

Browse files
committed
fix(server): preserve 1M context in Claude subagent selections
1 parent 924fde8 commit 3a19fd0

3 files changed

Lines changed: 118 additions & 8 deletions

File tree

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

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12499,13 +12499,60 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1249912499
),
1250012500
);
1250112501

12502-
it.effect("resolves a subagent's model and effort against the instance's custom models", () =>
12502+
it.effect.each([
12503+
{
12504+
route: "bare",
12505+
reportedModel: "gateway-route",
12506+
tagged: false,
12507+
selection: { model: "gateway-route", options: [{ id: "effort", value: "high" }] },
12508+
contextWindow: undefined,
12509+
},
12510+
{
12511+
route: "expanded",
12512+
reportedModel: "gateway-route[1m]",
12513+
tagged: false,
12514+
selection: {
12515+
model: "gateway-route",
12516+
options: [
12517+
{ id: "contextWindow", value: "1m" },
12518+
{ id: "effort", value: "high" },
12519+
],
12520+
},
12521+
contextWindow: 1_000_000,
12522+
},
12523+
{
12524+
route: "explicitly tagged",
12525+
reportedModel: "gateway-route[1m]",
12526+
tagged: true,
12527+
selection: { model: "gateway-route[1m]", options: [{ id: "effort", value: "high" }] },
12528+
contextWindow: 1_000_000,
12529+
},
12530+
{
12531+
route: "unconfigured",
12532+
reportedModel: "unconfigured-route[1m]",
12533+
tagged: false,
12534+
selection: { model: "unconfigured-route[1m]" },
12535+
contextWindow: undefined,
12536+
},
12537+
])("resolves custom subagent models, effort, and $route context", (testCase) =>
1250312538
Effect.scoped(
1250412539
Effect.gen(function* () {
1250512540
const TUNED_TOOL_USE_ID = "toolu-effort-custom-tuned";
1250612541
const SHADOW_TOOL_USE_ID = "toolu-effort-custom-shadow";
12542+
const CONTEXT_TOOL_USE_ID = "toolu-context-custom";
1250712543
const harness = yield* makeWakeHarnessWithOptions({
12508-
settings: CUSTOM_MODEL_CLAUDE_SETTINGS,
12544+
settings: {
12545+
...CUSTOM_MODEL_CLAUDE_SETTINGS,
12546+
customModels: [
12547+
...CUSTOM_MODEL_CLAUDE_SETTINGS.customModels,
12548+
...CUSTOM_CONTEXT_CLAUDE_SETTINGS.customModels,
12549+
...(testCase.tagged
12550+
? CUSTOM_CONTEXT_CLAUDE_SETTINGS.customModels.flatMap((entry) =>
12551+
typeof entry === "string" ? [] : [{ ...entry, slug: "gateway-route[1m]" }],
12552+
)
12553+
: []),
12554+
],
12555+
},
1250912556
});
1251012557
const now = yield* DateTime.now;
1251112558
yield* harness.runtime.startTurn(
@@ -12551,12 +12598,24 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1255112598
prompt: "Check another thing.",
1255212599
},
1255312600
},
12601+
{
12602+
type: "tool_use",
12603+
id: CONTEXT_TOOL_USE_ID,
12604+
name: "Agent",
12605+
input: {
12606+
description: "Context",
12607+
subagent_type: "general-purpose",
12608+
model: "gateway-route",
12609+
effort: "high",
12610+
prompt: "Check the context window.",
12611+
},
12612+
},
1255412613
],
1255512614
},
1255612615
uuid: "00000000-0000-4000-8000-000000000231",
1255712616
session_id: WAKE_NATIVE_SESSION,
1255812617
}),
12559-
...[TUNED_TOOL_USE_ID, SHADOW_TOOL_USE_ID].map((id, index) =>
12618+
...[TUNED_TOOL_USE_ID, SHADOW_TOOL_USE_ID, CONTEXT_TOOL_USE_ID].map((id, index) =>
1256012619
claudeSdkFrame({
1256112620
type: "system",
1256212621
subtype: "task_started",
@@ -12568,8 +12627,21 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1256812627
session_id: WAKE_NATIVE_SESSION,
1256912628
}),
1257012629
),
12630+
claudeSdkFrame({
12631+
type: "assistant",
12632+
parent_tool_use_id: CONTEXT_TOOL_USE_ID,
12633+
message: {
12634+
model: testCase.reportedModel,
12635+
id: "msg_custom_context_observed",
12636+
type: "message",
12637+
role: "assistant",
12638+
content: [{ type: "text", text: "Checking." }],
12639+
},
12640+
uuid: "00000000-0000-4000-8000-000000000235",
12641+
session_id: WAKE_NATIVE_SESSION,
12642+
}),
1257112643
makeResultFrame({
12572-
uuid: "00000000-0000-4000-8000-000000000234",
12644+
uuid: "00000000-0000-4000-8000-000000000236",
1257312645
result: "Spawned the subagents.",
1257412646
}),
1257512647
]) {
@@ -12592,7 +12664,12 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1259212664
options: [{ id: "effort", value: "brutal" }],
1259312665
},
1259412666
[`task-${SHADOW_TOOL_USE_ID}`]: { instanceId, model: "opus" },
12667+
[`task-${CONTEXT_TOOL_USE_ID}`]: { instanceId, ...testCase.selection },
1259512668
});
12669+
assert.strictEqual(
12670+
harness.runtime.getModelContextWindow?.(selections[`task-${CONTEXT_TOOL_USE_ID}`]),
12671+
testCase.contextWindow,
12672+
);
1259612673
}).pipe(
1259712674
Effect.provide(
1259812675
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ import {
160160
import {
161161
BUNDLED_CLAUDE_MODEL_CATALOG,
162162
resolveClaudeCatalogContextWindowTokens,
163+
resolveClaudeCatalogModelSelection,
163164
type ClaudeModelCatalog,
164165
getClaudeCatalogModelCapabilities,
165166
resolveClaudeModelSlug,
@@ -4147,14 +4148,24 @@ function claudeSubagentModelSelection(
41474148
model: string,
41484149
effort: string | undefined,
41494150
): ModelSelection {
4150-
const descriptor = getClaudeCatalogModelCapabilities(catalog, model).optionDescriptors?.find(
4151-
(candidate) => candidate.id === "effort",
4152-
);
4151+
const selection = resolveClaudeCatalogModelSelection(catalog, instanceId, model);
4152+
const descriptor = getClaudeCatalogModelCapabilities(
4153+
catalog,
4154+
selection.model,
4155+
).optionDescriptors?.find((candidate) => candidate.id === "effort");
41534156
const offered =
41544157
effort !== undefined &&
41554158
descriptor?.type === "select" &&
41564159
descriptor.options.some((option) => option.id === effort);
4157-
return { instanceId, model, ...(offered ? { options: [{ id: "effort", value: effort }] } : {}) };
4160+
return offered
4161+
? {
4162+
...selection,
4163+
options: [
4164+
...(selection.options?.filter((option) => option.id !== "effort") ?? []),
4165+
{ id: "effort", value: effort },
4166+
],
4167+
}
4168+
: selection;
41584169
}
41594170

41604171
const PENDING_CLAUDE_SUBAGENT_CAP = 64;

‎apps/server/src/provider/ClaudeModelCatalog.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,28 @@ export function resolveClaudeModelSlug(catalog: ClaudeModelCatalog, slugOrAlias:
144144
return resolveClaudeCatalogModel(catalog, slugOrAlias)?.model.slug ?? slugOrAlias;
145145
}
146146

147+
export function resolveClaudeCatalogModelSelection(
148+
catalog: ClaudeModelCatalog,
149+
instanceId: ModelSelection["instanceId"],
150+
model: string,
151+
): ModelSelection {
152+
const exact = resolveClaudeCatalogModel(catalog, model);
153+
if (exact) return { instanceId, model: exact.model.slug };
154+
155+
for (const entry of catalog.models) {
156+
for (const [id, suffixes] of Object.entries(entry.runtime.modelSuffixes ?? {})) {
157+
for (const [value, suffix] of Object.entries(suffixes)) {
158+
if (!suffix || !model.toLowerCase().endsWith(suffix.toLowerCase())) continue;
159+
const base = resolveClaudeCatalogModel(catalog, model.slice(0, -suffix.length));
160+
if (base !== entry) continue;
161+
return { instanceId, model: entry.model.slug, options: [{ id, value }] };
162+
}
163+
}
164+
}
165+
166+
return { instanceId, model };
167+
}
168+
147169
export function getClaudeCatalogModelCapabilities(
148170
catalog: ClaudeModelCatalog,
149171
slugOrAlias: string | null | undefined,

0 commit comments

Comments
 (0)