Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/opencode/src/session/overflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ export function isOverflow(input: {
if (input.cfg.compaction?.auto === false) return false
if (input.model.limit.context === 0) return false

const count =
input.tokens.total || input.tokens.input + input.tokens.output + input.tokens.cache.read + input.tokens.cache.write
const tokens = input.tokens
const prompt = tokens.input + tokens.cache.read + tokens.cache.write
// A real prompt can never exceed the model's context window. Some providers report
// impossible usage after image-heavy histories (#50474); trusting it makes
// auto-compaction loop on every step, so ignore such reports here. Genuine
// overflow still recovers through the ContextOverflowError path.
if (prompt > input.model.limit.context) return false

const count = tokens.total || prompt + tokens.output
return count >= usable(input)
}
40 changes: 40 additions & 0 deletions packages/opencode/test/session/compaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,46 @@ describe("session.compaction.isOverflow", () => {
},
),
)

// ─── Regression: #50474 ───────────────────────────────────────────────
// A provider reported usage.input 24x the model's context window. isOverflow
// trusted it, so auto-compaction re-triggered every step until interrupted.

it.live(
"returns false when reported input exceeds the model's context window",
provideTmpdirInstance(() =>
Effect.gen(function* () {
const compact = yield* SessionCompaction.Service
const model = createModel({ context: 1_048_576, input: 1_048_576, output: 384_000 })
const tokens = { input: 25_391_744, output: 2_041_207, reasoning: 0, cache: { read: 0, write: 0 } }
expect(yield* compact.isOverflow({ tokens, model })).toBe(false)
}),
),
)

it.live(
"returns false when input plus cache exceeds the model's context window",
provideTmpdirInstance(() =>
Effect.gen(function* () {
const compact = yield* SessionCompaction.Service
const model = createModel({ context: 1_048_576, output: 384_000 })
const tokens = { input: 600_000, output: 10_000, reasoning: 0, cache: { read: 600_000, write: 0 } }
expect(yield* compact.isOverflow({ tokens, model })).toBe(false)
}),
),
)

it.live(
"still returns true when the prompt exactly fills the context window",
provideTmpdirInstance(() =>
Effect.gen(function* () {
const compact = yield* SessionCompaction.Service
const model = createModel({ context: 100_000, output: 32_000 })
const tokens = { input: 100_000, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }
expect(yield* compact.isOverflow({ tokens, model })).toBe(true)
}),
),
)
})

describe("session.compaction.create", () => {
Expand Down
Loading