CompactionSettings.forWindow constructor
CompactionSettings.forWindow(
- int window
Creates settings scaled to a model's context window (in tokens).
gh-1742 (AC3): PROPORTIONAL headroom above the legacy-parity band. The legacy flat reserve (16,384) was tuned for 200k windows — 1.6% of a 1M window, which let real input ride 976k–982k for hours on the kimi-k3 session before the first auto-compaction. The scaling rule:
reserveTokens = min(65536, max(window ~/ 16, legacyReserve))— 6.25% of the window, clamped to 64Ki, floored by the EXACT legacy curve (min(16384, max(1024, window ~/ 4)), degenerate clamps included). The legacy term dominates below 262,144 (wherewindow/16would undercut it — the hosted 128k/200k class rides the legacy line exactly, so an estimate-only provider (E1) still compacts no later than before); the proportional term takes over above it, so a 1M window keeps ≥60k headroom at the trigger (enough to absorb one huge tool result plus estimator drift);keepRecentTokens = min(131072, max(window ~/ 8, legacyKeep))— the post-compaction floor scales with the window (12.5%, clamped 128Ki) above the mirrored legacy curve (min(20000, max(2048, window ~/ 2))), so the sawtooth restarts proportional on big windows instead of always ~20k.
Monotone in the window: a bigger window reserves (and keeps) as much or more (max of two monotone curves under monotone clamps). Degenerate small windows relax the floors proportionally so the reserve never eats half the window and the kept region never eats all of it (below that, compaction could not free anything).
Implementation
factory CompactionSettings.forWindow(int window) {
// Legacy-parity floors (gh-1742 AC3 + review round 1, comment 1):
// the pre-gh-1742 curves, byte-identical. The reserve floor is what
// keeps the (16k, 262k) band — most hosted sessions — firing no
// later than before; `window/16` alone compacts up to 12,288 tokens
// later there.
final legacyReserve = min(16384, max(1024, window ~/ 4));
final legacyKeep = min(20000, max(2048, window ~/ 2));
var reserveTokens = min(65536, max(window ~/ 16, legacyReserve));
var keepRecentTokens = min(131072, max(window ~/ 8, legacyKeep));
if (reserveTokens >= window ~/ 2) {
reserveTokens = max(128, window ~/ 4);
}
if (keepRecentTokens >= window) {
keepRecentTokens = max(256, window ~/ 2);
}
return CompactionSettings(
enabled: true,
reserveTokens: reserveTokens,
keepRecentTokens: keepRecentTokens,
);
}