Skip to content

Fix the context meter's blind spots and stale compaction arming - #339

Merged
TheGreatAxios merged 4 commits into
release/tui-bugfixesfrom
cl-5564-context-window-fills-far-faster-than-the-meter-reports
Aug 7, 2026
Merged

Fix the context meter's blind spots and stale compaction arming#339
TheGreatAxios merged 4 commits into
release/tui-bugfixesfrom
cl-5564-context-window-fills-far-faster-than-the-meter-reports

Conversation

@TheGreatAxios

@TheGreatAxios TheGreatAxios commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Fold the system prompt and active tool schemas into the local context estimate, which previously only summed turn content and undercounted by whatever the harness's own framing cost
  • Fall back to that estimate when a provider omits or zeroes usage on a turn, instead of pinning the arming decision (and the status-bar meter) at a stale or 0% reading
  • Re-check the estimate on each tool result rather than only at the last inference.done snapshot, without arming below the turn floor that createPruningCompactor independently enforces
  • Derive that floor from the compactor's own keepRecentTurns config (via a shared constant and a compactorNoOpFloor helper) instead of an independent literal, which had also been off by one and could arm a compaction the compactor was guaranteed to no-op on
  • Route context-size accounting (compaction, the status-bar meter, faremeter) through one shared contextTokensFromUsage function, and mark the meter with a tilde when it is showing an estimate rather than provider-reported usage

Verification

  • bun run typecheck, bun run build, and bun test pass on this branch after rebasing onto origin/release/tui-bugfixes; observed 4362/4362 across two consecutive full-suite runs
  • Added/updated unit tests in src/agent/compaction.test.ts, src/agent/context-estimate.test.ts, src/context-compactor.test.ts, src/cost/cost-summary.test.ts, src/director.test.ts, src/tui-opentui/prompt-border.test.ts, and tests/unit/context-window.test.ts, including a test that exercises the exact turn count at the compactor's no-op floor and one past it
  • Known gap, covered by a test rather than left silent: when the provider reports real usage under threshold, a huge tool result arriving before the next inference.done is not re-checked mid-cycle (compaction.test.ts, "does not catch a huge tool result mid-cycle when the provider reported real usage") — only the usage-omitted path gets the live re-check
  • Deferred, not part of this PR: bounding sub-agent report size as it enters the parent context, and a general policy for preempting compaction mid-turn during a tool-heavy streak beyond the specific tool.done re-check made here

Closes CL-5564

@linear-code

linear-code Bot commented Aug 7, 2026

Copy link
Copy Markdown

CL-5564

@TheGreatAxios
TheGreatAxios changed the base branch from main to release/tui-bugfixes August 7, 2026 04:20
@TheGreatAxios
TheGreatAxios force-pushed the cl-5564-context-window-fills-far-faster-than-the-meter-reports branch from f38586d to b12b4a4 Compare August 7, 2026 04:48
@TheGreatAxios TheGreatAxios changed the title Fix the context meter's blind spots and let compaction fire before the turn floor Fix the context meter's blind spots and stale compaction arming Aug 7, 2026
The system prompt and active tool schemas ride on every request the
same way turns do, but the local context estimate only ever summed
turn content, so it undercounted by whatever the harness's own
framing cost.
Compaction arming read only reported input tokens, so a provider
that omits or zeroes usage pinned the estimate at 0 and never armed,
even as real occupancy grew. Cache reads and writes ride on the
context window the same way input does, so both now route through
one shared token-counting function instead of being hand-picked
per call site.

A tool result produced by a turn's own tool batch also arrives after
that turn's arming decision was made; when the decision came from
the local estimate (usage was missing), the governor now re-derives
it against the live estimate on the next tool.done instead of
waiting for the following inference.done. The arming rule itself is
unchanged: it still requires more turns than createPruningCompactor's
own no-op floor, since arming below that floor cannot compact
anything.
The status bar previously read only the last turn's reported input
and cache tokens, with no fallback, so a provider that omits usage
pinned the meter at a stale or 0% reading. It now trusts the same
estimate the compaction governor already computed, including the
governor's own decision on whether that number is estimated, rather
than re-deriving that decision from a second usage read. The tilde
prefix that marks an estimated percentage is written once and reused
by both the status bar and the prompt border. Cost accounting's own
input-plus-cache sum is replaced with the same shared function so
all three consumers agree on what "context size" means.
MIN_TURNS_TO_COMPACT was an independent literal that happened to
match createPruningCompactor's keepRecentTurns, itself duplicated as
a third literal in the session and sub-agent compactor registrations.
The independent copy was also off by one: the compactor's own no-op
condition is keepRecentTurns + 1, not keepRecentTurns, so the
governor could arm a compaction at the exact turn count the
compactor was guaranteed to no-op on. All three call sites now share
one exported constant, and the governor computes its floor with the
same function the compactor uses internally.
@TheGreatAxios
TheGreatAxios force-pushed the cl-5564-context-window-fills-far-faster-than-the-meter-reports branch from b12b4a4 to fc57acc Compare August 7, 2026 05:19
@TheGreatAxios
TheGreatAxios merged commit b2b6848 into release/tui-bugfixes Aug 7, 2026
2 checks passed
@TheGreatAxios
TheGreatAxios deleted the cl-5564-context-window-fills-far-faster-than-the-meter-reports branch August 7, 2026 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant