From 80238595ed4e346a340dbe57ba1bf8d1c19383ac Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 17 Jun 2026 10:56:55 +0800 Subject: [PATCH] refactor(agents): hide context window thresholds --- src/agents/context-window-guard.test.ts | 26 ++++++++----------- src/agents/context-window-guard.ts | 4 +-- .../run.overflow-compaction.harness.ts | 1 - 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/agents/context-window-guard.test.ts b/src/agents/context-window-guard.test.ts index b173d2f1c61..e8db088b47b 100644 --- a/src/agents/context-window-guard.test.ts +++ b/src/agents/context-window-guard.test.ts @@ -3,11 +3,9 @@ import { describe, expect, it } from "vitest"; import type { OpenClawConfig } from "../config/config.js"; import { CONTEXT_WINDOW_HARD_MIN_TOKENS, - CONTEXT_WINDOW_WARN_BELOW_TOKENS, evaluateContextWindowGuard, formatContextWindowBlockMessage, formatContextWindowWarningMessage, - resolveContextWindowGuardThresholds, resolveContextWindowInfo, } from "./context-window-guard.js"; @@ -297,24 +295,22 @@ describe("context-window-guard", () => { expect(guard.shouldBlock).toBe(false); }); - it("exports threshold floors as expected", () => { + it("exports the public hard-min floor as expected", () => { expect(CONTEXT_WINDOW_HARD_MIN_TOKENS).toBe(4_000); - expect(CONTEXT_WINDOW_WARN_BELOW_TOKENS).toBe(8_000); }); - it("derives percentage-based thresholds above the safe floors", () => { - expect(resolveContextWindowGuardThresholds(1_000_000)).toEqual({ - hardMinTokens: 100_000, - warnBelowTokens: 200_000, + it("derives percentage-based guard thresholds above the safe floors", () => { + const largeGuard = evaluateContextWindowGuard({ + info: { tokens: 1_000_000, source: "model" }, }); - expect(resolveContextWindowGuardThresholds(64_000)).toEqual({ - hardMinTokens: 6_400, - warnBelowTokens: 12_800, - }); - expect(resolveContextWindowGuardThresholds(Number.NaN)).toEqual({ - hardMinTokens: 4_000, - warnBelowTokens: 8_000, + expect(largeGuard.hardMinTokens).toBe(100_000); + expect(largeGuard.warnBelowTokens).toBe(200_000); + + const mediumGuard = evaluateContextWindowGuard({ + info: { tokens: 64_000, source: "model" }, }); + expect(mediumGuard.hardMinTokens).toBe(6_400); + expect(mediumGuard.warnBelowTokens).toBe(12_800); }); it("derives guard thresholds from the reference window when capped", () => { diff --git a/src/agents/context-window-guard.ts b/src/agents/context-window-guard.ts index 8364efb22a4..d095cc3203b 100644 --- a/src/agents/context-window-guard.ts +++ b/src/agents/context-window-guard.ts @@ -9,7 +9,7 @@ import type { OpenClawConfig } from "../config/types.openclaw.js"; import { resolveProviderEndpoint } from "./provider-attribution.js"; export const CONTEXT_WINDOW_HARD_MIN_TOKENS = 4_000; -export const CONTEXT_WINDOW_WARN_BELOW_TOKENS = 8_000; +const CONTEXT_WINDOW_WARN_BELOW_TOKENS = 8_000; const CONTEXT_WINDOW_HARD_MIN_RATIO = 0.1; const CONTEXT_WINDOW_WARN_BELOW_RATIO = 0.2; @@ -125,7 +125,7 @@ function resolveContextWindowGuardHint(params: { } /** Derive warning/block floors from the resolved model context window. */ -export function resolveContextWindowGuardThresholds( +function resolveContextWindowGuardThresholds( contextWindowTokens: number, ): ContextWindowGuardThresholds { const tokens = normalizePositiveInt(contextWindowTokens) ?? 0; diff --git a/src/agents/embedded-agent-runner/run.overflow-compaction.harness.ts b/src/agents/embedded-agent-runner/run.overflow-compaction.harness.ts index 396d3535e67..9622e5d0ac3 100644 --- a/src/agents/embedded-agent-runner/run.overflow-compaction.harness.ts +++ b/src/agents/embedded-agent-runner/run.overflow-compaction.harness.ts @@ -701,7 +701,6 @@ export async function loadRunOverflowCompactionHarness(): Promise<{ vi.doMock("../context-window-guard.js", () => ({ CONTEXT_WINDOW_HARD_MIN_TOKENS: 1000, - CONTEXT_WINDOW_WARN_BELOW_TOKENS: 5000, evaluateContextWindowGuard: mockedEvaluateContextWindowGuard, formatContextWindowBlockMessage: mockedFormatContextWindowBlockMessage, formatContextWindowWarningMessage: mockedFormatContextWindowWarningMessage,