惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
D
Docker
GbyAI
GbyAI
宝玉的分享
宝玉的分享
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Vercel News
Vercel News
博客园_首页
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
S
SegmentFault 最新的问题
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
V
V2EX
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
T
Tailwind CSS Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix: clamp compaction max_tokens to model output limit (#...
adzendo · 2026-05-07 · via Recent Commits to openclaw:main

@@ -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+

});