Skip to content

Commit edd7b25

Browse files
authored
fix(agents): native runs ignore the selected 200K context window (#128838)
* fix(agents): native runs ignore the selected 200K context window The session-selected context-window option was passed to resolveContextWindowInfo as its lowest-priority input, so a discovered contextTokens value or a models.providers entry silently outranked it. The CLI runner and both gateway projections already clamp the resolved window with the selected option; the embedded runner did not, leaving the session row reporting 200K while the native run budgeted and auto-compacted against the wider window. Mirror the CLI clamp, including its guard that only an effective option caps the window, so models without selectable windows are untouched. * test(agents): use the shared configured-model fixture for the provider cap case
1 parent 482fbdc commit edd7b25

2 files changed

Lines changed: 74 additions & 4 deletions

File tree

‎src/agents/embedded-agent-runner/run/setup.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,66 @@ describe("resolveEmbeddedRuntimeModelPolicy", () => {
338338
expect(unselected.effectiveModel.contextWindow).toBe(1_000_000);
339339
});
340340

341+
it("keeps the session-selected context window ahead of discovered and configured caps", () => {
342+
// The selection is passed as the lowest-priority input, so a discovered
343+
// contextTokens value or a models.providers entry would otherwise leave a
344+
// 200k session budgeting against the wider window while the session row
345+
// and model projections already report the selected 200k.
346+
const runtimeModel: ProviderRuntimeModel = {
347+
provider: "anthropic",
348+
id: "claude-sonnet-5",
349+
name: "Claude Sonnet 5",
350+
baseUrl: "https://api.anthropic.com",
351+
api: "anthropic-messages",
352+
reasoning: true,
353+
input: ["text"],
354+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
355+
contextWindow: 1_000_000,
356+
contextTokens: 272_000,
357+
maxTokens: 128_000,
358+
contextWindows: [
359+
{ id: "200k", label: "200K", contextWindow: 200_000 },
360+
{ id: "1m", label: "1M", contextWindow: 1_000_000 },
361+
],
362+
contextWindowDefault: "1m",
363+
};
364+
const resolve = (params: { cfg?: OpenClawConfig; contextWindow?: string }) =>
365+
resolveEmbeddedRuntimeModelPolicy({
366+
cfg: params.cfg,
367+
provider: "anthropic",
368+
modelId: "claude-sonnet-5",
369+
runtimeModel,
370+
nativeModelOwned: false,
371+
...(params.contextWindow ? { contextWindow: params.contextWindow } : {}),
372+
});
373+
374+
const discovered = resolve({ contextWindow: "200k" });
375+
expect(discovered.contextTokenBudget).toBe(200_000);
376+
expect(discovered.effectiveModel.contextWindow).toBe(200_000);
377+
378+
const configured = resolve({
379+
contextWindow: "200k",
380+
cfg: {
381+
models: {
382+
providers: {
383+
anthropic: {
384+
baseUrl: "https://api.anthropic.com",
385+
models: [createConfiguredModel({ id: "claude-sonnet-5", contextTokens: 1_000_000 })],
386+
},
387+
},
388+
},
389+
} satisfies OpenClawConfig,
390+
});
391+
expect(configured.contextTokenBudget).toBe(200_000);
392+
expect(configured.effectiveModel.contextWindow).toBe(200_000);
393+
394+
// Without a selection the declared default resolves to the wider option, so
395+
// the tighter discovered cap still owns the budget.
396+
const unselected = resolve({});
397+
expect(unselected.contextTokenBudget).toBe(272_000);
398+
expect(unselected.effectiveModel.contextWindow).toBe(272_000);
399+
});
400+
341401
it("preserves the effective budget and adds an authored cap for plugin transports (#124702)", () => {
342402
const resolve = (models: ModelDefinitionConfig[]) =>
343403
resolveEmbeddedRunEffectiveModel({

‎src/agents/embedded-agent-runner/run/setup.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,28 @@ function resolveEffectiveRuntimeModel(params: {
219219
// The session-selected context-window option caps native runs too; the CLI
220220
// backend maps the option id to argv/env separately, but budget and payload
221221
// sizing must honor the selection on every runtime path.
222-
const selectedWindowTokens = resolveModelContextWindowProfile({
222+
const contextWindowProfile = resolveModelContextWindowProfile({
223223
catalogEntry: params.runtimeModel,
224224
selected: params.contextWindow,
225-
}).contextTokens;
226-
const ctxInfo = resolveContextWindowInfo({
225+
});
226+
const resolvedCtxInfo = resolveContextWindowInfo({
227227
cfg: params.cfg,
228228
provider: params.contextConfigProvider ?? params.provider,
229229
modelId: params.modelId,
230230
modelContextTokens: readAgentModelContextTokens(params.runtimeModel),
231-
modelContextWindow: selectedWindowTokens,
231+
modelContextWindow: contextWindowProfile.contextTokens,
232232
defaultTokens: DEFAULT_CONTEXT_TOKENS,
233233
});
234+
// resolveContextWindowInfo ranks the passed selection below both the
235+
// discovered model cap and models.providers.*.models[].contextTokens, so a
236+
// 200k session would keep budgeting against the wider window. Only an
237+
// effective option caps here; the bare catalog scalar stays subordinate.
238+
const ctxInfo =
239+
contextWindowProfile.contextWindow &&
240+
contextWindowProfile.contextTokens !== undefined &&
241+
resolvedCtxInfo.tokens > contextWindowProfile.contextTokens
242+
? { ...resolvedCtxInfo, tokens: contextWindowProfile.contextTokens, source: "model" as const }
243+
: resolvedCtxInfo;
234244

235245
// Apply contextTokens cap to model so session runtime's auto-compaction
236246
// threshold uses the effective limit, not the native context window.

0 commit comments

Comments
 (0)