fix(core): cap compaction's prompt size at the context window (#60660)

This commit is contained in:
Aiden Cline
2026-10-30 11:57:58 -05:00
committed by GitHub
Unverified
parent a8091a2ea0
commit cb1e58fe44
2 changed files with 17 additions and 4 deletions
+11 -4
View File
@@ -1133,9 +1133,8 @@ export const estimatePrompt = (context: SessionContext.Loaded): Estimate => {
* to give up `RESERVE_MIN` keeps `RESERVE_SHARE`.
*/
const calculateCeiling = (limit: SessionContext.Loaded["model"]["limit"], buffer: number | undefined) => {
// Unknown limits are reported as 0. An unknown input limit falls back to the context window; with no window at
// all, only a provider rejection can limit the request.
const window = limit.input || limit.context
// With no window at all, only a provider rejection can limit the request.
const window = inputWindow(limit)
if (window <= 0) return Number.POSITIVE_INFINITY
// A buffer that leaves no room, such as one set for a larger model, is ignored.
if (buffer !== undefined && buffer < window) return window - buffer
@@ -1147,7 +1146,15 @@ const calculateCeiling = (limit: SessionContext.Loaded["model"]["limit"], buffer
* cannot generate that much. Zero for an unknown window.
*/
const summaryRoom = (limit: SessionContext.Loaded["model"]["limit"], output = RESERVE_MIN) =>
Math.max(0, (limit.input || limit.context) - Math.min(RESERVE_MIN, output))
Math.max(0, inputWindow(limit) - Math.min(RESERVE_MIN, output))
/**
* The largest prompt the model takes, or 0 when unknown. Unknown limits are reported as 0, and an unknown input limit
* falls back to the context window. The context window also holds the reply, so an input limit above it, as some
* catalogs and config overrides report, cannot be used in full.
*/
const inputWindow = (limit: SessionContext.Loaded["model"]["limit"]) =>
limit.input && limit.context ? Math.min(limit.input, limit.context) : limit.input || limit.context
/**
* Another provider's count may describe a native window this model cannot replay. History then holds the original
@@ -430,6 +430,12 @@ it.effect("auto compaction estimates current content against the buffered prompt
expect(yield* due(native(231_200))).toBe(true)
expect(yield* due(native(1_000_000, { context: 0, input: undefined, output: 0 }))).toBe(false)
// An input limit above the context window, as some catalogs and config overrides report, cannot be used in full.
const overstated = { context: 128_000, input: 272_000, output: 128_000 }
expect(yield* due(input(108_799, overstated))).toBe(false)
expect(yield* due(input(108_800, overstated))).toBe(true)
expect(yield* due(input(200_000, { context: 0, input: 272_000, output: 0 }))).toBe(false)
// The summary's 16k output limit is more than 15% of a 100k window, so it sets the ceiling.
const contextLimited = { context: 100_000, output: 10_000 }
expect(yield* due(input(83_999, contextLimited))).toBe(false)