Skip to content

Commit f9e5fc5

Browse files
kevin-dpclaude
andcommitted
test(agents): cover the over-window clamp in computeContextBreakdown [review #4596]
The breakdown clamps `used` to the window so the segments still sum to the window and `free` can't go negative when a step reports usage above the window. That path was exercised but untested; lock it in. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 71c6db8 commit f9e5fc5

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

packages/agents-runtime/test/token-accountant.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ describe(`computeContextBreakdown`, () => {
5151
expect(segs.every((s) => s.tokens >= 0)).toBe(true)
5252
})
5353

54+
it(`clamps usedTokens to the window when the step is over the window`, () => {
55+
const segs = computeContextBreakdown(
56+
// A step can report usage above the window; usedTokens stays un-clamped on
57+
// ContextUsage but the breakdown must clamp it so the segments still sum to
58+
// the window and `free` doesn't go negative.
59+
{ usedTokens: 120_000, contextWindow: 100_000, ratio: 1 },
60+
{ systemTokens: 5_000, toolsTokens: 15_000 }
61+
)
62+
const tokens = Object.fromEntries(segs.map((s) => [s.key, s.tokens]))
63+
expect(tokens).toEqual({
64+
system: 5_000,
65+
tools: 15_000,
66+
messages: 80_000, // window (100k) − system − tools
67+
free: 0,
68+
})
69+
expect(segs.reduce((n, s) => n + s.tokens, 0)).toBe(100_000)
70+
})
71+
5472
it(`parseContextBreakdown reads persisted JSON and tolerates junk`, () => {
5573
expect(parseContextBreakdown(`{"system":12,"tools":34}`)).toEqual({
5674
systemTokens: 12,

0 commit comments

Comments
 (0)