Skip to content

fix(server): refresh Claude context meter after compact - #7249

Open
imMxts wants to merge 3 commits into
pingdotgg:mainfrom
imMxts:cursor/claude-compact-meter-fallback-4df3
Open

fix(server): refresh Claude context meter after compact#7249
imMxts wants to merge 3 commits into
pingdotgg:mainfrom
imMxts:cursor/claude-compact-meter-fallback-4df3

Conversation

@imMxts

@imMxts imMxts commented Aug 16, 2026

Copy link
Copy Markdown

Fixes #4650.

What changed

compact_boundary only emitted thread.token-usage.updated when compact_metadata.post_tokens was a finite integer > 0. SDK post_tokens is optional, so a successful /compact often sent no usage event and the composer meter kept the pre-compaction usedTokens.

This keeps that cheap path, then falls back once to the existing queryCurrentContextUsage (getContextUsage) when metadata is missing, zero, or unusable. Compacted thread state is still emitted if neither source yields a snapshot; no usage value is invented.

task_progress / task_notification were writing cumulative total_tokens into usedTokens through Math.max. That cannot decrease, so a correct post-compact reading got ratcheted back up — worse with subagent progress that also carries tool_uses / duration_ms. Those events now keep the last active usedTokens and only advance totalProcessedTokens when a prior reading exists.

apps/server/src/provider/Layers/ClaudeAdapter.ts      |  73 +--
apps/server/src/provider/Layers/ClaudeAdapter.test.ts | 510 +++++++++++++++++----
2 files changed, 460 insertions(+), 123 deletions(-)

Why it should exist

After /compact the meter stayed red/full, then could snap down and back up later with no user action. The UI already follows the latest usedTokens; the stale value was coming from the adapter. Teaching the web meter about Claude payloads would still leave lastKnownTokenUsage wrong.

Test

vp test run src/provider/Layers/ClaudeAdapter.test.ts is 76/76.

  • valid post_tokens does not call getContextUsage
  • unusable metadata (undefined / {} / 0 / "18000" / -1) calls it once
  • missing or throwing query emits compacted state and no fabricated usage
  • compact then cumulative task_progress keeps usedTokens: 18000
  • first task_progress no longer invents usedTokens

No UI change, so no before/after images.


Note

Medium Risk
Changes token-usage semantics in the Claude provider layer that feeds the UI meter; behavior is well-tested but affects runtime event ordering and fallback SDK calls after compact.

Overview
Fixes stale or bouncing context meter values in the Claude adapter by correcting how thread.token-usage.updated is derived around compaction and subagent progress.

compact_boundary handling now resolves usage through resolveCompactBoundaryTokenUsage: it still prefers valid compact_metadata.post_tokens, but when metadata is missing or unusable it falls back once to getContextUsage via queryCurrentContextUsage. Compacted thread state is still emitted even when no snapshot is available; the adapter does not invent token counts.

task_progress / cumulative totals no longer drive usedTokens via Math.max on total_tokens. Those events keep the last active usedTokens and only bump totalProcessedTokens when a prior reading exists, so post-compact drops are not undone by subagent or cumulative progress.

Tests add harness stubs for getContextUsage and cover metadata vs fallback paths, failure cases, ordering before compacted state, and interaction with turn-completion usage.

Reviewed by Cursor Bugbot for commit 2e08d87. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Refresh Claude context meter after compact by querying current usage as fallback

  • task_progress messages no longer update active context usage from total_tokens; they only update totalProcessedTokens when it strictly increases beyond prior readings.
  • On compact_boundary, a new resolveCompactBoundaryTokenUsage helper first tries to derive usage from post_tokens metadata, then falls back to querying the current context usage via queryCurrentContextUsage.
  • If neither source is available (or the query throws), no fabricated usage is emitted, preserving the last known values.
  • Behavioral Change: task_progress events no longer emit thread.token-usage.updated, and toolUses/durationMs are no longer included in usage snapshots from task progress.

Macroscope summarized b203979.

cursoragent and others added 2 commits August 16, 2026 18:17
…ens is missing

Claude compact_boundary events can omit or zero post_tokens, and the adapter
dropped usage updates instead of querying getContextUsage. The composer meter
then kept the pre-compaction usedTokens.

Keep the cheap post_tokens path. Fall back to the existing context-usage query
only when metadata is unusable, and emit no fabricated usage when that query
is absent or fails.

Co-authored-by: Mats Varnskühler <imMxts@users.noreply.github.com>
Task progress reports cumulative spend, often from subagents. Writing that
total into usedTokens through Math.max made the context meter unable to
drop after compact and snap back to full. Keep the last active reading and
only advance totalProcessedTokens.

Co-authored-by: Mats Varnskühler <imMxts@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Aug 16, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 74f471b5-0ad0-45a1-b868-9a19ed5bb562

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added vouch:unvouched PR author is not yet trusted in the VOUCHED list. size:M 30-99 changed lines (additions + deletions). labels Aug 16, 2026
@imMxts
imMxts marked this pull request as ready for review August 16, 2026 20:01
@macroscopeapp

macroscopeapp Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Needs human review

This PR modifies runtime token usage tracking logic in the Claude adapter, including how context usage is computed after compaction events and a new fallback to query current context. These behavioral changes warrant human review despite comprehensive test coverage.

You can customize Macroscope's approvability policy. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M 30-99 changed lines (additions + deletions). vouch:unvouched PR author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: context meter ratchets up and never reflects /compact — task_progress writes cumulative tokens into usedTokens via Math.max

2 participants