Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(grok): surface context window usage in the composer #5405

Closedahmed-besic(GitHub profile, opens in a new tab)wants to merge 5 commits intomain fromrefs/pull/5405/headopened View on GitHub

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:

  • ACP sessionUpdate: "usage_update" (used / size) when present
  • Grok's current path: session/update _meta.totalTokens while streaming, plus availableModels[]._meta.totalContextTokens for the window size
  • Prompt response _meta.totalTokens / _meta.usage as a reliable end-of-turn snapshot

Those become thread.token-usage.updated → context-window.updated, which the existing ContextWindowMeter already renders. No new UI components.

Cursor gets a no-op case for the shared TokenUsageUpdated parse event so the ACP union stays exhaustive; wiring makeAcpTokenUsageUpdatedEvent there 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 stdio already 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 grok 0.2.118: Grok does not emit standard ACP usage_update yet, so this PR supports that path for when it lands and uses the _meta sources that work today.

UI Changes

Uses the existing context window meter in the composer.

  • Before: Grok threads have no context size / fill indicator.
  • After: After the first usage-bearing update (stream meta or prompt result), Grok shows the same ring + used/max tooltip as Codex.

Validation

  • vp test run src/provider/acp/AcpRuntimeModel.test.ts src/provider/acp/AcpCoreRuntimeEvents.test.ts src/provider/acp/GrokAcpSupport.test.ts — 23 passed
  • vp test run src/provider/Layers/GrokAdapter.test.ts — 20 passed
  • Live ACP probe (grok agent stdio): confirmed _meta.totalTokens on session/update, totalContextTokens on models, and prompt _meta.usage; no usage_update from this CLI version yet
  • Meter math from live numbers: e.g. 19,267 / 500,000 ≈ 3.9% used

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes (happy to add after a UI smoke against a Grok thread)
  • A video is not applicable (no animation/timing change)

Made by Grok Build via the Grok Build harness while working on T3 Code.


Note

Medium Risk Touches Grok ACP session event handling and token-usage snapshots that feed the composer meter. Parsing is additive and conservative (positive used tokens, no guessing among multiple windows), but incorrect mapping would show a wrong fill % to users.

Overview Grok threads now feed the existing composer context meter. The adapter emits thread.token-usage.updated from ACP usage_update (when present), streaming session/update _meta.totalTokens, and prompt-response _meta as an end-of-turn snapshot, enriched with per-model totalContextTokens and compactsAutomatically.

Session context keeps a modelId→window map and last-known max so fill % still works between turns and after model lookup misses. Usage events are allowed after activeTurnId is cleared so the meter can update between turns; interrupted turns are skipped.

Cursor only adds a no-op TokenUsageUpdated case so the shared ACP event union stays exhaustive. UI change is a grok → Grok display-name mapping.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 8a491a6d9f88a54f6c89ad6d3be970612c52cda5. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Surface context window usage in the Grok provider composer

  • Adds thread.token-usage.updated event emission to the Grok adapter, fired both during streaming (via ACP session/update notifications) and at the end of each prompt turn from prompt response metadata.
  • Introduces helpers in GrokAcpSupport.ts to extract context window sizes from model metadata, build a modelId→window map, and enrich usage snapshots with maxTokens and compactsAutomatically.
  • Extends AcpRuntimeModel.ts to parse usage_update ACP notifications and _meta fields into TokenUsageUpdated events.
  • Adds makeAcpTokenUsageUpdatedEvent factory in AcpCoreRuntimeEvents.ts for consistent construction of token-usage runtime events across ACP providers.
  • Maps the grok provider string to 'Grok' in contextWindow.ts for correct UI display.
Macroscope summarized 8a491a6.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      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: 943c338f-0f2a-40ce-9163-8bef728f6237

      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.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: 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](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. Merged current main. Context-window tests, Grok adapter tests, and server/web typechecks are passing.

    5. Live check against current Grok ACP: tokenUsageFromGrokPromptMeta should not feed the composer meter.

      On a greeting turn:

      • session/update _meta.totalTokens = 1615 (context occupancy)
      • Prompt response usage.totalTokens = 20554 (billed tokens)

      If the prompt-response path runs after the notification, the ring jumps to the billed number. Grok still does not send usage_update; occupancy lives on session/update _meta.totalTokens. Window size came from session signals.json (contextWindowTokens: 500000), not from the prompt result.

      The rest of the mapping (accept usage_update if it ever lands, plus _meta.totalTokens while streaming) matches what I saw. The prompt-meta snapshot is the part that would show the wrong fill %.

    6. Note

      🤖 GPT-6 Astra (preview) responding on behalf of Theo

      This was closed as part of an automated cleanup pass. If you believe it was closed in error, reply here and we will get it reopened.

      Closing in favor of #9206, where review of the Grok context meter continues. That version uses context occupancy and excludes billed prompt totals, which can show the wrong fill percentage here. The model-window selection cases are recorded there for review. This does not mean the meter fix has shipped.

Sign in to join this conversation.