Skip to content

Commit cd4fd3d

Browse files
committed
fix(core): treat an oversized compaction buffer as unset
1 parent 75865d4 commit cd4fd3d

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

‎packages/core/src/session/compaction.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -923,9 +923,10 @@ const calculateCeiling = (limit: SessionContext.Loaded["model"]["limit"], buffer
923923
// all, only a provider rejection can limit the request.
924924
const window = limit.input || limit.context
925925
if (window <= 0) return Number.POSITIVE_INFINITY
926-
// Keep at most 90% of the window free. A larger buffer would make the ceiling negative and every estimate
927-
// cross it, so automatic compaction would run on every step, including the first request of a session.
928-
if (buffer !== undefined) return window - Math.min(buffer, Math.floor(window * 0.9))
926+
// A buffer at or above the window cannot leave room for the request; treat it as unset so the default
927+
// reserve applies. Clamping to a fraction instead can put the ceiling below the fixed prompt size
928+
// (system, tools, instructions), which makes automatic compaction due on every step.
929+
if (buffer !== undefined && buffer < window) return window - buffer
929930
return window - Math.max(Math.floor(window * 0.1), window >= 2 * RESERVE_MIN ? RESERVE_MIN : 0)
930931
}
931932

‎packages/core/test/session-compaction.test.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,13 @@ it.effect("clamps an oversized compaction buffer and waits for an exchange to su
334334
const status = (messages: readonly SessionMessage.Info[]) =>
335335
compaction.compact({ reason: "auto", context: context(messages) }).pipe(Effect.map((outcome) => outcome.status))
336336

337-
// 600k free tokens on a 100k window keeps at most 90% free, so the ceiling is 10% of the window.
338-
expect(yield* status([assistant(9_999)])).toBe("skipped")
339-
expect(yield* status([assistant(10_000)])).toBe("completed")
337+
// 600k free tokens on a 100k window is treated as unset, so the ceiling is the default 16k reserve
338+
// below the window.
339+
expect(yield* status([assistant(83_999)])).toBe("skipped")
340+
expect(yield* status([assistant(84_000)])).toBe("completed")
340341
// The opening prompt alone has no older exchange; a huge prompt must not summarize itself or
341342
// surface `compaction.unavailable` as a failed compaction.
342-
expect(yield* status([user("x".repeat(60_000))])).toBe("skipped")
343+
expect(yield* status([user("x".repeat(400_000))])).toBe("skipped")
343344
}),
344345
)
345346

0 commit comments

Comments
 (0)