Skip to content

Commit ac43135

Browse files
adzendojalehman
andauthored
fix: clamp compaction max_tokens to model output limit (#54392)
Merged via squash. Prepared head SHA: 8a88821 Co-authored-by: adzendo <[email protected]> Co-authored-by: jalehman <[email protected]> Reviewed-by: @jalehman
1 parent 6b97f57 commit ac43135

4 files changed

Lines changed: 166 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ Docs: https://docs.openclaw.ai
466466
- web_search/Brave: fix provider selection when Brave is installed as an external plugin and `tools.web.search.provider: "brave"` is explicitly configured — a redundant provider re-resolution at startup could race and return an empty list, causing a spurious `WEB_SEARCH_PROVIDER_INVALID_AUTODETECT` warning and treating the explicitly configured provider as absent. Fixes #77676. Thanks @openperf.
467467
- Doctor/plugins: discover doctor contracts from load-path channel plugins during `openclaw doctor --fix`, so plugin-owned legacy config repair runs before validation. (#77477) Thanks @jalehman.
468468
- Dependencies: bump transitive `basic-ftp` to 5.3.1 so the runtime lockfile no longer includes the vulnerable 5.3.0 build flagged by the production dependency audit. (#78637) Thanks @sallyom.
469+
- Agents/compaction: clamp compaction summary reserve tokens to each model's output limit so high-context compaction no longer requests invalid `max_tokens` values. (#54392) Thanks @adzendo.
469470

470471
## 2026.5.3-1
471472

‎src/agents/compaction.identifier-preservation.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ vi.mock("@mariozechner/pi-coding-agent", async () => {
77
const actual = await vi.importActual<typeof piCodingAgent>("@mariozechner/pi-coding-agent");
88
return {
99
...actual,
10+
estimateTokens: vi.fn((message: unknown) => Math.ceil(JSON.stringify(message).length / 4)),
1011
generateSummary: vi.fn(),
1112
};
1213
});
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import type { AgentMessage } from "@mariozechner/pi-agent-core";
2+
import type { ExtensionContext } from "@mariozechner/pi-coding-agent";
3+
import { beforeEach, describe, expect, it, vi } from "vitest";
4+
5+
const piCodingAgentMocks = vi.hoisted(() => ({
6+
estimateTokens: vi.fn((message: unknown) => Math.ceil(JSON.stringify(message).length / 4)),
7+
generateSummary: vi.fn(),
8+
}));
9+
10+
vi.mock("@mariozechner/pi-coding-agent", async () => {
11+
const actual = await vi.importActual<typeof import("@mariozechner/pi-coding-agent")>(
12+
"@mariozechner/pi-coding-agent",
13+
);
14+
return {
15+
...actual,
16+
estimateTokens: piCodingAgentMocks.estimateTokens,
17+
generateSummary: piCodingAgentMocks.generateSummary,
18+
};
19+
});
20+
21+
const mockGenerateSummary = piCodingAgentMocks.generateSummary;
22+
23+
let summarizeInStages: typeof import("./compaction.js").summarizeInStages;
24+
25+
async function loadFreshCompactionModuleForTest() {
26+
vi.resetModules();
27+
({ summarizeInStages } = await import("./compaction.js"));
28+
}
29+
30+
function makeMessage(index: number, size = 1200): AgentMessage {
31+
return {
32+
role: "user",
33+
content: `m${index}-${"x".repeat(size)}`,
34+
timestamp: index,
35+
};
36+
}
37+
38+
describe("compaction reserveTokens clamping", () => {
39+
beforeEach(async () => {
40+
await loadFreshCompactionModuleForTest();
41+
mockGenerateSummary.mockReset();
42+
mockGenerateSummary.mockResolvedValue("summary");
43+
piCodingAgentMocks.estimateTokens.mockReset();
44+
piCodingAgentMocks.estimateTokens.mockImplementation((message: unknown) =>
45+
Math.ceil(JSON.stringify(message).length / 4),
46+
);
47+
});
48+
49+
it("clamps reserveTokens when model maxTokens is smaller than requested", async () => {
50+
// Simulate the exact bug scenario: large context window (1M) with
51+
// reserveTokensFloor of 300K, but model output limit is only 128K.
52+
// Without clamping, generateSummary would receive 300K and compute
53+
// max_tokens = floor(0.8 * 300K) = 240K, exceeding the 128K model limit.
54+
const model = {
55+
provider: "anthropic",
56+
model: "claude-sonnet-4-6",
57+
contextWindow: 1_000_000,
58+
maxTokens: 128_000,
59+
} as unknown as NonNullable<ExtensionContext["model"]>;
60+
61+
await summarizeInStages({
62+
model,
63+
apiKey: "test-key", // pragma: allowlist secret
64+
reserveTokens: 300_000,
65+
maxChunkTokens: 8000,
66+
contextWindow: 1_000_000,
67+
signal: new AbortController().signal,
68+
messages: [makeMessage(1), makeMessage(2)],
69+
});
70+
71+
expect(mockGenerateSummary).toHaveBeenCalled();
72+
// Third argument to generateSummary is reserveTokens.
73+
// With maxTokens 128K, the clamp should be floor(128_000 / 0.8) = 160_000.
74+
const passedReserveTokens = mockGenerateSummary.mock.calls[0][2];
75+
expect(passedReserveTokens).toBeLessThanOrEqual(Math.floor(128_000 / 0.8));
76+
expect(passedReserveTokens).toBe(160_000);
77+
});
78+
79+
it("does not clamp when model maxTokens is large enough", async () => {
80+
const model = {
81+
provider: "anthropic",
82+
model: "claude-opus-4-6",
83+
contextWindow: 200_000,
84+
maxTokens: 32_000,
85+
} as unknown as NonNullable<ExtensionContext["model"]>;
86+
87+
// reserveTokens 4000 is well under floor(32_000 / 0.8) = 40_000
88+
await summarizeInStages({
89+
model,
90+
apiKey: "test-key", // pragma: allowlist secret
91+
reserveTokens: 4000,
92+
maxChunkTokens: 8000,
93+
contextWindow: 200_000,
94+
signal: new AbortController().signal,
95+
messages: [makeMessage(1), makeMessage(2)],
96+
});
97+
98+
expect(mockGenerateSummary).toHaveBeenCalled();
99+
const passedReserveTokens = mockGenerateSummary.mock.calls[0][2];
100+
expect(passedReserveTokens).toBe(4000);
101+
});
102+
103+
it("falls back to 128K default when model has no maxTokens field", async () => {
104+
// Model without maxTokens defined — should default to 128_000 as the cap.
105+
const model = {
106+
provider: "anthropic",
107+
model: "claude-3-opus",
108+
contextWindow: 1_000_000,
109+
} as unknown as NonNullable<ExtensionContext["model"]>;
110+
111+
await summarizeInStages({
112+
model,
113+
apiKey: "test-key", // pragma: allowlist secret
114+
reserveTokens: 300_000,
115+
maxChunkTokens: 8000,
116+
contextWindow: 1_000_000,
117+
signal: new AbortController().signal,
118+
messages: [makeMessage(1), makeMessage(2)],
119+
});
120+
121+
expect(mockGenerateSummary).toHaveBeenCalled();
122+
// Fallback maxTokens is 128_000, so clamp = floor(128_000 / 0.8) = 160_000
123+
const passedReserveTokens = mockGenerateSummary.mock.calls[0][2];
124+
expect(passedReserveTokens).toBe(160_000);
125+
});
126+
127+
it("clamps consistently across all chunks in staged summarization", async () => {
128+
const model = {
129+
provider: "anthropic",
130+
model: "claude-sonnet-4-6",
131+
contextWindow: 1_000_000,
132+
maxTokens: 128_000,
133+
} as unknown as NonNullable<ExtensionContext["model"]>;
134+
135+
// Use enough messages and small chunk size to force multiple chunks
136+
await summarizeInStages({
137+
model,
138+
apiKey: "test-key", // pragma: allowlist secret
139+
reserveTokens: 300_000,
140+
maxChunkTokens: 1000,
141+
contextWindow: 1_000_000,
142+
signal: new AbortController().signal,
143+
messages: Array.from({ length: 4 }, (_, i) => makeMessage(i + 1)),
144+
parts: 2,
145+
minMessagesForSplit: 4,
146+
});
147+
148+
expect(mockGenerateSummary.mock.calls.length).toBeGreaterThan(1);
149+
const expectedClamp = Math.floor(128_000 / 0.8);
150+
for (const call of mockGenerateSummary.mock.calls) {
151+
expect(call[2]).toBeLessThanOrEqual(expectedClamp);
152+
}
153+
});
154+
});

‎src/agents/compaction.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,22 @@ async function summarizeChunks(params: {
314314
params.customInstructions,
315315
params.summarizationInstructions,
316316
);
317+
318+
// Clamp reserveTokens to the model's maxTokens output cap.
319+
// generateSummary() uses Math.floor(0.8 * reserveTokens) as max_tokens for the API call.
320+
// With large context windows (1M tokens), reserveTokensFloor can be 300K+, producing
321+
// max_tokens of 240K+ which exceeds model output limits (e.g. 128K for Anthropic).
322+
// By clamping reserveTokens here, we ensure the downstream max_tokens stays within bounds.
323+
const modelMaxTokens = params.model.maxTokens ?? 128_000;
324+
const clampedReserveTokens = Math.min(params.reserveTokens, Math.floor(modelMaxTokens / 0.8));
325+
317326
for (const chunk of chunks) {
318327
summary = await retryAsync(
319328
() =>
320329
generateSummary(
321330
chunk,
322331
params.model,
323-
params.reserveTokens,
332+
clampedReserveTokens,
324333
params.apiKey,
325334
params.headers,
326335
params.signal,

0 commit comments

Comments
 (0)