Skip to content

Commit 57946e8

Browse files
fix(opencode): the token replay counts reasoning toward the compaction threshold
The product's overThreshold and floorAfterCompaction count tokens.total, the provider's totalTokens, which includes reasoning. The replay passed tokens without a total, so overThreshold fell back to input + output + cache and left reasoning out: a reasoning-heavy session compacted later in the replay than it would in the product. The replayed tokens now carry total = prompt + output + reasoning, and the guard's floor uses the same count. Ref anomalyco#47 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01GHX6CpBR14wnFxJLDahwTr
1 parent cd14172 commit 57946e8

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎packages/opencode/script/token-usage.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ export interface Projection {
227227
* that step); for a full cache miss it is the whole simulated prompt, scaled by
228228
* the actual miss ratio; cache writes keep their actual size where they fit.
229229
* Compaction follows the product: before each request, overThreshold (the
230-
* product's function) looks at the last finished request's prompt plus output,
231-
* with the re-compaction floor reset by every summary, real or simulated.
230+
* product's function) looks at the last finished request's tokens.total (prompt,
231+
* output and reasoning), with the re-compaction floor reset by every summary, real or simulated.
232232
* A simulated compaction costs one summary request with the history sent
233233
* uncached (as observed: summary requests read no cache; the whole simulated
234234
* prompt is charged, which overstates it, since observed summaries send about
@@ -253,7 +253,7 @@ export function project(steps: ReadonlyArray<Step>, lever: Lever, options: Proje
253253
let carried = 0
254254
let missNext = false
255255
// What the product looks at before each request: the last finished request's
256-
// tokens (prompt + output), and the floor its re-compaction guard uses, the
256+
// tokens (prompt + output + reasoning), and the floor its re-compaction guard uses, the
257257
// first request after the latest summary (real or simulated).
258258
let last: { tokens: SessionV1.Assistant["tokens"]; summary: boolean } | undefined
259259
let floor: number | undefined
@@ -276,14 +276,18 @@ export function project(steps: ReadonlyArray<Step>, lever: Lever, options: Proje
276276
total.cacheWrite += request.write
277277
total.output += request.output
278278
total.reasoning += request.reasoning
279+
// the product counts tokens.total (the provider's totalTokens: prompt, output
280+
// and reasoning), both for the threshold and for the guard's floor
281+
const count = request.size + request.output + request.reasoning
279282
const tokens = {
283+
total: count,
280284
input: request.uncached,
281285
output: request.output,
282286
reasoning: request.reasoning,
283287
cache: { read, write: request.write },
284288
}
285289
if (floorPending && !request.summary) {
286-
floor = request.size + request.output
290+
floor = count
287291
floorPending = false
288292
}
289293
last = { tokens, summary: request.summary }

‎packages/opencode/test/script/token-usage.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ describe("TokenUsage.project", () => {
150150
expect(TokenUsage.project(under, { compactAt: 100_000 }, options).compactions).toBe(0)
151151
})
152152

153+
test("reasoning counts toward the threshold, as tokens.total does in the product", () => {
154+
clock = 0
155+
// 95_000 prompt + 500 output + 4_500 reasoning = 100_000: the product's total reaches the threshold
156+
const steps = [step({ prompt: 95_000, reasoning: 4_500 }), step({ prompt: 95_100 })]
157+
expect(TokenUsage.project(steps, { compactAt: 100_000 }, options).compactions).toBe(1)
158+
clock = 0
159+
const under = [step({ prompt: 95_000, reasoning: 4_499 }), step({ prompt: 95_100 })]
160+
expect(TokenUsage.project(under, { compactAt: 100_000 }, options).compactions).toBe(0)
161+
})
162+
163+
test("the guard's floor counts reasoning too, as floorAfterCompaction does", () => {
164+
clock = 0
165+
// every request counts 100_000 + 500 + 50_000 = 150_500. After the first compaction the floor is
166+
// that same 150_500, so the next needs 200_500 and never comes; a floor without reasoning
167+
// (100_500) would let every later request compact again.
168+
const steps = Array.from({ length: 5 }, () => step({ prompt: 100_000, reasoning: 50_000 }))
169+
expect(TokenUsage.project(steps, { compactAt: 100_000 }, options).compactions).toBe(1)
170+
})
171+
153172
test("the product's guard: after a compaction the next one needs half a threshold over the first request after it", () => {
154173
clock = 0
155174
// 40 steps growing 10k each from 100k; the kept prompt (120k) is itself over the threshold

0 commit comments

Comments
 (0)