diff --git a/packages/core/src/session/compaction.ts b/packages/core/src/session/compaction.ts index 49005f93db1..e056b0b2bb7 100644 --- a/packages/core/src/session/compaction.ts +++ b/packages/core/src/session/compaction.ts @@ -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 diff --git a/packages/core/test/session-compaction.test.ts b/packages/core/test/session-compaction.test.ts index efb047b0072..43d11f47254 100644 --- a/packages/core/test/session-compaction.test.ts +++ b/packages/core/test/session-compaction.test.ts @@ -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)