fix(server): refresh Claude context meter after compact - #7249
Conversation
…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>
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
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. Comment |
ApprovabilityVerdict: 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. |
Fixes #4650.
What changed
compact_boundaryonly emittedthread.token-usage.updatedwhencompact_metadata.post_tokenswas a finite integer > 0. SDKpost_tokensis optional, so a successful/compactoften sent no usage event and the composer meter kept the pre-compactionusedTokens.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_notificationwere writing cumulativetotal_tokensintousedTokensthroughMath.max. That cannot decrease, so a correct post-compact reading got ratcheted back up — worse with subagent progress that also carriestool_uses/duration_ms. Those events now keep the last activeusedTokensand only advancetotalProcessedTokenswhen a prior reading exists.Why it should exist
After
/compactthe meter stayed red/full, then could snap down and back up later with no user action. The UI already follows the latestusedTokens; the stale value was coming from the adapter. Teaching the web meter about Claude payloads would still leavelastKnownTokenUsagewrong.Test
vp test run src/provider/Layers/ClaudeAdapter.test.tsis 76/76.post_tokensdoes not callgetContextUsageundefined/{}/0/"18000"/-1) calls it oncetask_progresskeepsusedTokens: 18000task_progressno longer inventsusedTokensNo 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.updatedis derived around compaction and subagent progress.compact_boundaryhandling now resolves usage throughresolveCompactBoundaryTokenUsage: it still prefers validcompact_metadata.post_tokens, but when metadata is missing or unusable it falls back once togetContextUsageviaqueryCurrentContextUsage. 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 driveusedTokensviaMath.maxontotal_tokens. Those events keep the last activeusedTokensand only bumptotalProcessedTokenswhen a prior reading exists, so post-compact drops are not undone by subagent or cumulative progress.Tests add harness stubs for
getContextUsageand cover metadata vs fallback paths, failure cases, ordering beforecompactedstate, 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_progressmessages no longer update active context usage fromtotal_tokens; they only updatetotalProcessedTokenswhen it strictly increases beyond prior readings.compact_boundary, a newresolveCompactBoundaryTokenUsagehelper first tries to derive usage frompost_tokensmetadata, then falls back to querying the current context usage viaqueryCurrentContextUsage.task_progressevents no longer emitthread.token-usage.updated, andtoolUses/durationMsare no longer included in usage snapshots from task progress.Macroscope summarized b203979.