From 01e3dd3508a7b10076d5f646e73bcbd310839796 Mon Sep 17 00:00:00 2001 From: Tak Hoffman <781889+Takhoffman@users.noreply.github.com> Date: Fri, 27 Mar 2026 21:47:59 -0500 Subject: [PATCH] fix(regression): normalize provider aliases in context window guard --- src/agents/context-window-guard.test.ts | 37 +++++++++++++++++++++++++ src/agents/context-window-guard.ts | 3 +- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/agents/context-window-guard.test.ts b/src/agents/context-window-guard.test.ts index 8758103a4c4..757dc6d1020 100644 --- a/src/agents/context-window-guard.test.ts +++ b/src/agents/context-window-guard.test.ts @@ -85,6 +85,43 @@ describe("context-window-guard", () => { expect(guard.shouldBlock).toBe(true); }); + it("normalizes provider aliases when reading models config context windows", () => { + const cfg = { + models: { + providers: { + "z.ai": { + baseUrl: "http://localhost", + apiKey: "x", + models: [ + { + id: "glm-5", + name: "glm-5", + reasoning: false, + input: ["text"], + cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, + contextWindow: 12_000, + maxTokens: 256, + }, + ], + }, + }, + }, + } satisfies OpenClawConfig; + + const info = resolveContextWindowInfo({ + cfg, + provider: "z-ai", + modelId: "glm-5", + modelContextWindow: 64_000, + defaultTokens: 200_000, + }); + + expect(info).toEqual({ + source: "modelsConfig", + tokens: 12_000, + }); + }); + it("caps with agents.defaults.contextTokens", () => { const cfg = { agents: { defaults: { contextTokens: 20_000 } }, diff --git a/src/agents/context-window-guard.ts b/src/agents/context-window-guard.ts index 1dc38870bf6..054a125bdd7 100644 --- a/src/agents/context-window-guard.ts +++ b/src/agents/context-window-guard.ts @@ -1,4 +1,5 @@ import type { OpenClawConfig } from "../config/config.js"; +import { findNormalizedProviderValue } from "./provider-id.js"; export const CONTEXT_WINDOW_HARD_MIN_TOKENS = 16_000; export const CONTEXT_WINDOW_WARN_BELOW_TOKENS = 32_000; @@ -29,7 +30,7 @@ export function resolveContextWindowInfo(params: { const providers = params.cfg?.models?.providers as | Record }> | undefined; - const providerEntry = providers?.[params.provider]; + const providerEntry = findNormalizedProviderValue(providers, params.provider); const models = Array.isArray(providerEntry?.models) ? providerEntry.models : []; const match = models.find((m) => m?.id === params.modelId); return normalizePositiveInt(match?.contextWindow);