Skip to content

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

Closed
matsvarn wants to merge 2 commits into
mainfrom
cursor/claude-compact-meter-fallback-4df3
Closed

matsvarn wants to merge 2 commits into
mainfrom
cursor/claude-compact-meter-fallback-4df3

Conversation

@matsvarn

Copy link
Copy Markdown
Owner

What Changed

Claude compact-boundary handling now:

  1. Keeps the cheap compact_metadata.post_tokens path.
  2. Falls back once to getContextUsage when post_tokens is missing, zero, or unusable.
  3. Leaves usedTokens untouched on task_progress / task_notification. Those events only update totalProcessedTokens when a prior active reading exists. They no longer write cumulative subagent spend into the context meter.

If neither compact metadata nor getContextUsage can produce a snapshot, compacted thread state is still emitted and no precise usage value is invented.

Why

After /compact, the composer meter stayed red/full, then could snap down and back up. Two provider bugs caused that:

  • compact_boundary dropped usage updates unless post_tokens was a finite integer > 0, so the UI kept the last pre-compact usedTokens. SDK post_tokens is optional.
  • task_progress fed cumulative total_tokens into usedTokens through Math.max, which cannot decrease and overwrites a correct compact reading. Subagent progress carrying tool_uses / duration_ms made this worse.

The Claude adapter is the right seam. The web meter already renders the latest usedTokens; teaching it about Claude payloads would spread logic and still leave lastKnownTokenUsage stale.

Fixes pingdotgg#4650.

UI Changes

None in client code. The composer meter already follows the latest context-window.updated event.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes
  • I included a video for animation/interaction changes
Open in Web Open in Cursor 

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>
@github-actions github-actions Bot added vouch:trusted PR author is trusted by repo permissions or the VOUCHED list. size:M labels Aug 16, 2026
@matsvarn matsvarn closed this Aug 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M vouch:trusted PR author is trusted by repo permissions or 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