Skip to content

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

Open
ahmed-besic wants to merge 4 commits into
pingdotgg:mainfrom
ahmed-besic:fix/grok-context-window
Open

fix(grok): surface context window usage in the composer#5405
ahmed-besic wants to merge 4 commits into
pingdotgg:mainfrom
ahmed-besic:fix/grok-context-window

Conversation

@ahmed-besic

@ahmed-besic ahmed-besic commented Aug 5, 2026

Copy link
Copy Markdown

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.updatedcontext-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

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.updated runtime events (same path Codex already uses).

ACP parsing now materializes TokenUsageUpdated from standard usage_update (used / size) and, when that is absent, from session/update _meta.totalTokens (Grok’s current streaming signal). A shared makeAcpTokenUsageUpdatedEvent builds the canonical runtime payload.

Grok adapter caches per-model windows from availableModels[]._meta.totalContextTokens, enriches snapshots with maxTokens and compactsAutomatically, and forwards usage on stream updates (including between turns when activeTurnId is cleared) plus an end-of-turn snapshot from prompt _meta. Cursor adds an exhaustive no-op TokenUsageUpdated branch until the same path is verified live.

Web labels the grok provider 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

  • 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 e7f3b2e.

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.
@coderabbitai

coderabbitai Bot commented Aug 5, 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: 97c10701-d4fe-4baa-834d-1844161fce55

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:L 100-499 changed lines (additions + deletions). labels Aug 5, 2026
Comment thread apps/server/src/provider/acp/GrokAcpSupport.ts Outdated
@macroscopeapp

macroscopeapp Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

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.

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.
Comment thread apps/server/src/provider/acp/GrokAcpSupport.ts Outdated
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L 100-499 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.

1 participant