fix(grok): surface context window usage in the composer - #5405
fix(grok): surface context window usage in the composer#5405ahmed-besic wants to merge 4 commits into
Conversation
Grok reports context fill via session/update _meta.totalTokens and model windows via availableModels _meta.totalContextTokens, but T3 never mapped these into thread.token-usage.updated. Parse ACP usage_update and Grok meta, join with the model window, and emit for the existing ContextWindowMeter. Cursor keeps a no-op case for the shared event until verified on a real Cursor install.
|
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 introduces new user-facing functionality (context window usage meter in the composer) with ~650 lines of new code, new event types, and new state management. Despite the 'fix' label, this is a feature addition that warrants human review. You can customize Macroscope's approvability policy. Learn more. |
Used tokens were surfacing without maxTokens, so the meter only showed "16k" with no limit. Keep a lastKnownMaxTokens from session model meta and always enrich usage snapshots with that window for used/max fill.
Avoid catalog-coupled and personal custom model ids in unit tests.
Preserve zero-valued token breakdowns from prompt meta, and only fall back to a sole known model window when resolving the initial max.
What Changed
Grok already reports session context fill and model window size over ACP, but T3 never turned that into the composer context meter that Codex already shows.
This maps:
sessionUpdate: "usage_update"(used/size) when presentsession/update_meta.totalTokenswhile streaming, plusavailableModels[]._meta.totalContextTokensfor the window size_meta.totalTokens/_meta.usageas a reliable end-of-turn snapshotThose become
thread.token-usage.updated→context-window.updated, which the existingContextWindowMeteralready renders. No new UI components.Cursor gets a no-op case for the shared
TokenUsageUpdatedparse event so the ACP union stays exhaustive; wiringmakeAcpTokenUsageUpdatedEventthere is the same one-liner once verified against a real Cursor ACP install.Why
Codex threads show a context ring (used / max / %). Grok threads showed nothing, even though
grok agent stdioalready streams fill (_meta.totalTokens) and advertises per-model windows (totalContextTokens, e.g. 500k / 200k / 128k). Without the adapter mapping, the meter never got a snapshot.Live probe against
grok0.2.118: Grok does not emit standard ACPusage_updateyet, so this PR supports that path for when it lands and uses the_metasources that work today.UI Changes
Uses the existing context window meter in the composer.
Validation
vp test run src/provider/acp/AcpRuntimeModel.test.ts src/provider/acp/AcpCoreRuntimeEvents.test.ts src/provider/acp/GrokAcpSupport.test.ts— 23 passedvp test run src/provider/Layers/GrokAdapter.test.ts— 20 passedgrok agent stdio): confirmed_meta.totalTokensonsession/update,totalContextTokenson models, and prompt_meta.usage; nousage_updatefrom this CLI version yetChecklist
Made by Grok Build via the Grok Build harness while working on T3 Code.
Note
Low Risk
Provider-adapter and event-mapping changes with broad test coverage; no auth or persistence changes, and Cursor behavior is unchanged (no-op).
Overview
Wires Grok ACP session data into the existing composer context meter by emitting
thread.token-usage.updatedruntime events (same path Codex already uses).ACP parsing now materializes
TokenUsageUpdatedfrom standardusage_update(used/size) and, when that is absent, fromsession/update_meta.totalTokens(Grok’s current streaming signal). A sharedmakeAcpTokenUsageUpdatedEventbuilds the canonical runtime payload.Grok adapter caches per-model windows from
availableModels[]._meta.totalContextTokens, enriches snapshots withmaxTokensandcompactsAutomatically, and forwards usage on stream updates (including between turns whenactiveTurnIdis cleared) plus an end-of-turn snapshot from prompt_meta. Cursor adds an exhaustive no-opTokenUsageUpdatedbranch until the same path is verified live.Web labels the
grokprovider in context-window display helpers.Reviewed by Cursor Bugbot for commit e7f3b2e. Bugbot is set up for automated code reviews on this repo. Configure here.
Note
Surface context window usage in the Grok provider composer
thread.token-usage.updatedevent emission to the Grok adapter, fired both during streaming (via ACPsession/updatenotifications) and at the end of each prompt turn from prompt response metadata.maxTokensandcompactsAutomatically.usage_updateACP notifications and_metafields intoTokenUsageUpdatedevents.makeAcpTokenUsageUpdatedEventfactory in AcpCoreRuntimeEvents.ts for consistent construction of token-usage runtime events across ACP providers.grokprovider string to'Grok'in contextWindow.ts for correct UI display.Macroscope summarized e7f3b2e.