Repository navigation
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1325
Root cause and protocol evidence
Grok's ACP
PromptResponse._meta.usage.inputTokensis turn-wide billing input, already including cache reads. RPCE treated that sum plus cached reads as context occupancy. Grok separately supplies current conversation state as_meta.totalTokensand the last model call's full prompt count as_meta.inputTokens. The latter survives compaction, so the current-state count is preferred; last-call input is only a compatibility fallback whentotalTokensis absent. Its per-model_meta.totalContextTokenswas also discarded, leaving the context ring on its 500,000-token fallback.Verified against the latest public Grok source, commit
2bdd1d6a6369de0e8c68132ea4539e9abd9e14a8:Issue comments, related origin/main history, and open/merged PR searches were checked before editing; no existing fix or duplicate PR was found.
Changes
_meta.totalTokens, which is reseeded after compaction; only responses without that field may fall back to last-call_meta.inputTokens. Zero/invalid current-state claims never resurrect stale last-call input. Do not add cache reads or substitute billing totals._meta.modelId, then falls back to its session-local confirmed catalog selection: Grok can report an API routing slug rather than a catalog key, including effort-specific routing. The resolved capacity travels onmessage_stop.modelContextWindow, including when the composer selection isdefault. The ring renders that stored per-turn window; there is no unobserved global-registry lookup. Nil still means no new window claim. If Grok cannot resolve capacity, it now explicitly withdraws the old bound using the documented zero-window signal; the estimator clears that bound before validating the current count and never stores/displays zero as a real capacity.ingestNonCodexTurnFinalization, the default-selection ring, oversight windows, response-model changes, compaction with stale last-call input, missing/legacy/zero counts, unchanged non-Grok behavior, and persisted model/variant capacities. Fixtures includesessionId,promptId,modelId, andtotalTokens; end-to-end tests obtain capacities from the protocol metadata instead of injecting the 500k fallback.The usage baseline is explicitly synthetic, reconstructed from the issue's observed token counts and verified source protocol; it is not represented as a captured wire recording. No live Grok runs were used.
Validation
Passed:
make dev-test FILTER=GrokBuildContextUsageTests: confirmed the original 253,360 vs 25,678 context-count failure and 500,000 vs reported 1,000,000/128,000 window failures before the production fix. Earlier test-harness import/launch-fixture setup errors were corrected before this behavioral red run.make dev-test FILTER='GrokBuildContextUsageTests|GrokBuildACPAgentProviderTests|GrokBuildReasoningEffortAdvertisementTests|AgentRuntimeSidebarViewModelTests|AgentSessionLinkContextLoadTests': 95 tests, 0 failures, including all 5 Grok context tests after round-2 review.make dev-swift-build PRODUCT=RepoPromptmake dev-lint(repository-pinned SwiftFormat 0.61.1 and strict SwiftLint); only changed files were formatted, not the repository.preflight.sh commitandpreflight.sh push(secret scanning, guardrails, clean outgoing boundary).Not run: full root suite, provider-package suite (unchanged package), full
pr-readylane, release/package validation, live CE MCP smoke, or UI/provider runtime validation. No visible app launch/stop/relaunch was performed; the changed build was not activated.Maintainer-guidance check
Confirmed root cause; upstream protocol remains the authority. The new persisted field is optional for older catalogs. No destructive migration or new background work; ring lookup is memory-only. Scope is limited to Grok context accounting and reported model capacity.
Draft for the requested opposing-model review; please keep in draft until that review is addressed.
Adversarial review follow-up
Addressed all five forwarded items: response/session-local window resolution for
default; compaction-aware current totals; stored snapshot windows rather than global-registry reads; realistic synthetic controller-to-view-model fixtures; provider-neutral shared-estimator documentation. Kept the PR in draft.Additional verified source: conversation replacement reseeds total tokens, turn finalization reads current total.
Round-2 routing-ID review
session/set_model. The regression switches 128k → 1M using routing sluggrok-large-reasoning-high, verifies the 210k current count is retained, and asserts the same session/ring/oversight on every turn, including the unresolved-capacity turn. An additional unchanged-count regression verifies that window-only withdrawal publishes.pr-readylane were run for this follow-up.