Skip to content

Commit a8c57f8

Browse files
opencodeclaude
andcommitted
fix(compaction): compute maxOutputTokens once, respect cfg.compaction.reserved in the no-limit.input branch
Copilot review on #34: usable() called ProviderTransform.maxOutputTokens() twice, and the no-limit.input branch ignored cfg.compaction?.reserved -- an operator override of the compaction buffer only applied to the other branch. Computed maxOutput once and threaded the same reserved-override precedence into both branches. Added a test confirming an explicit reserved value overrides the floor, verified via delete-the-fix. Co-Authored-By: Claude <[email protected]>
1 parent d8943d1 commit a8c57f8

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

‎packages/opencode/src/session/overflow.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ export function usable(input: { cfg: ConfigV1.Info; model: Provider.Model; outpu
1111
const context = input.model.limit.context
1212
if (context === 0) return 0
1313

14-
const reserved =
15-
input.cfg.compaction?.reserved ??
16-
Math.min(COMPACTION_BUFFER, ProviderTransform.maxOutputTokens(input.model, input.outputTokenMax))
17-
if (input.model.limit.input) return Math.max(0, input.model.limit.input - reserved)
14+
const maxOutput = ProviderTransform.maxOutputTokens(input.model, input.outputTokenMax)
15+
if (input.model.limit.input) {
16+
const reserved = input.cfg.compaction?.reserved ?? Math.min(COMPACTION_BUFFER, maxOutput)
17+
return Math.max(0, input.model.limit.input - reserved)
18+
}
1819
// TKT-377: isOverflow compares this against the PREVIOUS completed turn's real usage
1920
// (lastFinished.tokens, prompt.ts) -- by the time the NEXT request actually goes out, a new
2021
// user message and this turn's own tool results have grown the prompt past what was measured.
@@ -25,7 +26,9 @@ export function usable(input: { cfg: ConfigV1.Info; model: Provider.Model; outpu
2526
// Same max-with-floor shape as the V2 estimator's reserve (session/compaction.ts's
2627
// Math.max(output, config.buffer)): guarantee at least COMPACTION_BUFFER of headroom for
2728
// between-turn growth, not just whatever the model's own (possibly much smaller) output cap is.
28-
return Math.max(0, context - Math.max(ProviderTransform.maxOutputTokens(input.model, input.outputTokenMax), COMPACTION_BUFFER))
29+
// An explicit cfg.compaction.reserved still overrides the floor, same as the other branch.
30+
const reserved = input.cfg.compaction?.reserved ?? Math.max(maxOutput, COMPACTION_BUFFER)
31+
return Math.max(0, context - reserved)
2932
}
3033

3134
export function isOverflow(input: {

‎packages/opencode/test/session/compaction.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,23 @@ describe("session.compaction.isOverflow", () => {
584584
}),
585585
),
586586
)
587+
588+
it.live(
589+
"TKT-377/Copilot: an explicit cfg.compaction.reserved still overrides the floor in the no-limit.input branch",
590+
provideTmpdirInstance(
591+
() =>
592+
Effect.gen(function* () {
593+
const compact = yield* SessionCompaction.Service
594+
const model = createModel({ context: 131_072, output: 8_192 })
595+
// reserved=1000 explicitly configured, well below both maxOutputTokens (8192) and
596+
// COMPACTION_BUFFER (20000) -- usable = 131072-1000 = 130072. 129_500 is below that
597+
// (so isOverflow is false), even though it would exceed the 20000-floor default.
598+
const tokens = { input: 129_500, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }
599+
expect(yield* compact.isOverflow({ tokens, model })).toBe(false)
600+
}),
601+
{ config: { compaction: { reserved: 1_000 } } },
602+
),
603+
)
587604
})
588605

589606
describe("session.compaction.create", () => {

0 commit comments

Comments
 (0)