fix(suggest-compact): don't quote a percentage against an assumed window

The context signal always rendered "N% of <window> window", including when
the window size was the assumed 200k default rather than a detected value.
On a 1M session whose transcript carries no [1m] marker, that produced
lines like:

  [StrategicCompact] Context ~194k tokens (97% of 200k window)

while actual usage was ~19%. The user compacts on a false alarm, loses
context, and the resulting quality drop reads as a model regression.

The gap is structural: the context threshold defaults to 80% of the
window (160k on 200k), so the signal fires precisely in the 160k-200k
band where the size cannot be determined — above 200k the observed-tokens
fallback correctly infers 1M, and below 160k nothing fires.

Model id alone cannot close this. A tier may ship both a 200k and a 1M
variant under one id, so neither the known-family table nor a new entry
can distinguish them, and the transcript records no window field.

So stop asserting what isn't known: resolveContextWindow() now reports
whether the size was detected (env override, [1m] marker, known family,
or observed tokens > 200k) or assumed, and the hook omits the percentage
and window label when it was assumed. The token count, threshold, and
firing behaviour are unchanged.

resolveContextWindowTokens() keeps its existing signature and semantics.

Note: 3 pre-existing failures in tests/hooks/suggest-compact.test.js
reproduce identically on unmodified main and are untouched here.
This commit is contained in:
Tanel
2026-08-29 14:55:13 -04:00
committed by haelyra
parent e82e477034
commit ecdd517765
3 changed files with 82 additions and 15 deletions
+9 -3
View File
@@ -38,7 +38,8 @@ const {
resolveContextThreshold,
resolveContextInterval,
computeContextBucket,
formatWindowLabel
formatWindowLabel,
isContextWindowInferred
} = require('../lib/transcript-context');
const COUNTER_FILE_PREFIX = 'claude-tool-count-';
@@ -185,8 +186,13 @@ function buildContextSuggestion(transcriptPath, bucketFile, env) {
writeFile(bucketFile, String(bucket));
const approxTokens = `${Math.round(usage.tokens / 1000)}k`;
const percent = Math.round((usage.tokens / windowTokens) * 100);
return `[StrategicCompact] Context ~${approxTokens} tokens (${percent}% of ${formatWindowLabel(windowTokens)} window) - consider /compact at the next logical boundary`;
// Only quote a percentage when the window size was actually detected.
// Against an assumed 200k default the denominator is a guess, and a
// "97% of 200k window" line on a 1M session triggers needless compaction.
const scale = isContextWindowInferred(usage.tokens, usage.model)
? ''
: ` (${Math.round((usage.tokens / windowTokens) * 100)}% of ${formatWindowLabel(windowTokens)} window)`;
return `[StrategicCompact] Context ~${approxTokens} tokens${scale} - consider /compact at the next logical boundary`;
} catch (err) {
log(`[StrategicCompact] Context signal skipped: ${err.message}`);
return null;