From 6709f5646de29460a7caf8e4f553369a130413b7 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Wed, 21 Oct 2026 16:45:00 -0500 Subject: [PATCH] fix(core): give compaction summaries the same output limit as normal turns (#57564) --- packages/core/src/session/compaction.ts | 13 ++++++++++--- packages/core/src/session/model-request.ts | 13 +++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/core/src/session/compaction.ts b/packages/core/src/session/compaction.ts index b2f4599fd8e..7b305d8d661 100644 --- a/packages/core/src/session/compaction.ts +++ b/packages/core/src/session/compaction.ts @@ -101,6 +101,8 @@ const RESERVE_MIN = 16_000 const RESERVE_SHARE = 0.15 /** A common window size, assumed for the compaction request when the model's window is unknown. */ const UNKNOWN_WINDOW = 200_000 +/** A summary gets at least this much output, or the room left if less, even when the estimate margin leaves less. */ +const SUMMARY_OUTPUT_FLOOR = 32_000 const IMAGE_TOKEN_ESTIMATE = 1_500 const PDF_TOKEN_ESTIMATE = 2_000 @@ -875,15 +877,20 @@ const hasMeasuredPrompt = (message: SessionMessage.Info, model: SessionContext.L * cannot shrink the conversation further. Lower the limit to the room the request being sent leaves in the window, * so the input and the requested output never exceed the window by our own estimate. * - * Like `budget` in `prepare`, the estimate counts as measured, without the margin primary requests add for estimates: - * `deliver` aims at about 90% of the window, so that margin would cut every summary near its target to the minimum. + * The limit leaves the same margin for a low estimate that primary requests do. `deliver` aims at about 90% of the + * window, so that margin alone would cut a summary near its target to the minimum; there it still gets up to + * `SUMMARY_OUTPUT_FLOOR` of the room left without the margin. */ const fitOutput = (request: LLMRequest, limit: SessionContext.Loaded["model"]["limit"]) => { const current = request.generation?.maxTokens if (current === undefined) return request + const estimated = estimateRequest(request) const maxTokens = Math.min( current, - SessionModelRequest.outputLimit(limit, "compaction", { measured: estimateRequest(request), estimated: 0 }), + Math.max( + SessionModelRequest.outputLimit(limit, { measured: 0, estimated }), + Math.min(SUMMARY_OUTPUT_FLOOR, SessionModelRequest.outputLimit(limit, { measured: estimated, estimated: 0 })), + ), ) if (maxTokens === current) return request return LLMRequest.update(request, { generation: { ...request.generation, maxTokens } }) diff --git a/packages/core/src/session/model-request.ts b/packages/core/src/session/model-request.ts index 238f67ae8bb..e9cf14642f4 100644 --- a/packages/core/src/session/model-request.ts +++ b/packages/core/src/session/model-request.ts @@ -48,8 +48,6 @@ const GENERATION_KEYS = new Set(Object.keys(GenerationOptions.fields)) const OUTPUT_TOKEN_FALLBACK = 32_000 // No reply needs more, however much the model allows. const OUTPUT_TOKEN_MAX = 256_000 -// A summary never needs more, and a request asking for more cannot be shrunk to fit a window the catalog overstates. -const SUMMARY_OUTPUT_MAX = 32_000 // Prompt text is estimated at about 4 characters per token, which can run low on dense text such as code. const ESTIMATE_ERROR = 0.15 // Never ask for less; only reachable with automatic compaction off, since it keeps the window from filling this far. @@ -84,13 +82,8 @@ export interface Input { } /** The default output limit: the catalog limit, fitted to the room the prompt leaves in the context window. */ -export const outputLimit = ( - limit: Model.Info["limit"], - kind: "primary" | "compaction", - inputTokens?: Input["inputTokens"], -) => { - const model = Math.min(limit.output > 0 ? limit.output : OUTPUT_TOKEN_FALLBACK, OUTPUT_TOKEN_MAX) - const requested = kind === "compaction" ? Math.min(model, SUMMARY_OUTPUT_MAX) : model +export const outputLimit = (limit: Model.Info["limit"], inputTokens?: Input["inputTokens"]) => { + const requested = Math.min(limit.output > 0 ? limit.output : OUTPUT_TOKEN_FALLBACK, OUTPUT_TOKEN_MAX) if (inputTokens === undefined || limit.context <= 0) return requested const room = limit.context - inputTokens.measured - Math.ceil(inputTokens.estimated * (1 + ESTIMATE_ERROR)) return Math.min(requested, Math.max(OUTPUT_TOKEN_MIN, room)) @@ -258,7 +251,7 @@ export const layer = Layer.effect( messages: input.messages, options: kind === "primary" || kind === "compaction" - ? { maxTokens: outputLimit(model.limit, kind, input.inputTokens) } + ? { maxTokens: outputLimit(model.limit, input.inputTokens) } : {}, }, Object.fromEntries(Array.from(given, ([d, t]) => [t.name, d])),