fix(provider-grok): report model windows for handoff budgets #20180
main fromrefs/pull/20180/head·merged by juliusmarminge View on GitHubProblem#
Switching a thread back to a Grok thread it used before can fail with "Insufficient context allowance for the provider handoff" before Grok receives anything. The Grok adapter supplies no context window. When usage is unavailable, the handoff budget assumes a 128,000-token window and counts each stored byte on the Grok thread as a token. The reported thread held 160,684 bytes, which leaves a budget of 0.
Change#
The ACP adapter gets an optional flavor hook that reads a model's context window from the active session's metadata. The Grok flavor implements it. It resolves the selected model with resolveGrokAcpBaseModelId and returns that model's _meta.totalContextTokens from the session setup, or from initialize when the setup omits model state. Grok also lists 500,000 in contextWindows, but totalContextTokens is the default, so the lookup never assumes the larger window. Another instance, an unlisted or custom model, and grok-build get no window and keep the existing fallback. Other ACP flavors are unchanged.
Scope and approval#
Closes #20166, limited to the Grok part a maintainer accepted as a bug. #17462 covers the Cursor window. Large Grok histories can still fail until #19414 defers handoffs that do not fit. Without usage, a 256,000-token window leaves room only while the thread's stored bytes stay below about 192,000 minus the new message and its attachments.
Verification#
Linux, Grok CLI 1.0.60, signed in. ACP initialize and session/new listed grok-4.7, grok-4.7-build-fast, grok-4.6 and grok-4.5, each with totalContextTokens: 256000 and contextWindows: [256000, 500000]. The session offered no config option that selects a window.
# in apps/server
vp test run src/orchestration-v2/Adapters/GrokAdapterV2.test.ts src/orchestration-v2/Adapters/AcpAdapterV2.test.ts src/orchestration-v2/ContextHandoffBudget.test.ts src/orchestration-v2/testkit/OrchestratorReplayFixtures.integration.test.ts
# in packages/provider-grok
vp test run src/server/acpSupport.test.ts
- 319 tests pass in
apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts,apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.ts,apps/server/src/orchestration-v2/ContextHandoffBudget.test.ts,apps/server/src/orchestration-v2/testkit/OrchestratorReplayFixtures.integration.test.ts,packages/provider-grok/src/server/acpSupport.test.ts. - With
packages/provider-grok/src/server/adapter.ts,packages/provider-acp/src/server/adapter.tsreverted to09c5e7ac01, 1 test fails with assertion errors: "Grok model context windows > budgets handoffs with the selected Grok model's window". vp fmt --checkandvp lintreport no errors and no new warnings on the 4 changed TS files.- Typecheck passes for
@t3tools/provider-grok,@t3tools/provider-acp,t3. - On a source server with signed-in Grok, a Grok → Claude → same Grok switch-back with 102,667 stored bytes on the Grok thread fails on
09c5e7ac01with "Insufficient context allowance" and no provider turn. With this change the same native Grok thread receives the handoff and replies.
The new test opens a Grok session against the repo's ACP mock agent, which reports grok-4.7 with a 256,000 default and a listed 500,000 capacity. With 160,684 bytes of history, no usage and a 16,000 cap, the handoff budget is 16,000 with the reported window and 0 without it. Another instance, a custom slug and grok-build get no window.
Model: GPT-6.1-Sol, Claude Opus 5.5 (review), GPT-6-Astra (review). Harness: Codex, Claude Code.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
Approvability#
Verdict: Approved at
557605cMacroscope's review found this PR approvable — This is a narrowly scoped Grok handoff bug fix that reads the active session’s reported model window without changing other providers or product defaults. The shared adapter change is optional and supported by focused tests covering matching, fallback, and isolation behavior.
You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).@coderabbitai review
✅ Action performed
Review finished.
Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.
juliusmarminge merged thisab89a59
juliusmarminge closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
4645d358-37eb-4e77-86b2-d945423e1ddf📥 Commits
Reviewing files that changed from the base of the PR and between 2577baf8b0dc541283fa93d6919b0e354effadf4 and 557605c0e3fe4f23bf262178d38b8f14a82d04b0.
📒 Files selected for processing: 4
apps/server/scripts/acp-mock-agent.tsapps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.tspackages/provider-acp/src/server/adapter.tspackages/provider-grok/src/server/adapter.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.
📝 Walkthrough
The ACP session runtime now supports an optional context-window lookup. The Grok adapter provides the lookup using session model metadata. The mock agent and integration test cover configured context limits, model matching, and handoff budgeting.
Changes: Grok context-window lookup
packages/provider-acp/src/server/adapter.tspackages/provider-grok/src/server/adapter.ts,apps/server/scripts/acp-mock-agent.ts,apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.tsPriority: ➖ Normal
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
juliusmarmingeMerge Risk: ⚪ Minimal · up to
55760No confirmed issue currently prevents merging. The behavior when setup metadata omits the selected model remains unverified.
Security architecture risk: 🔵 Low · up to
55760The change retains destination checks and transfer-budget limits. No security boundary bypass was established, but capacity-estimate freshness during overlapping changes and cleanup remains unproven.
Retained concerns No architecture-level concerns identified.
Security review details
Security Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
Pre-merge checks:
3 · 
1
#20166covers both Cursor and Grok handoff failures. This pull request implements the Grok portion:AcpAdapterV2Flavor.getModelContextWindowforwards session metadata, and the Grok flavor reso…#20166, or provide an updated issue scope that explicitly removes the Cursor portion from this direct target.Full details: Description check
Explanation
The description includes complete Problem, Change, Scope and approval, and Verification sections. It explains the affected behavior, implementation, linked issue, test results, limitations, and validation environment.
Full details: Out of Scope Changes check
Explanation
The changes stay within the reported Grok handoff objective. The ACP hook is supporting infrastructure. The mock-agent settings and Grok integration test support verification. The Grok adapter reads the selected model's session metadata and preserves fallback behavior for unsupported models and instances. No unrelated behavior is shown.
Full details: Linked Issues check
Explanation
Issue
#20166covers both Cursor and Grok handoff failures. This pull request implements the Grok portion:AcpAdapterV2Flavor.getModelContextWindowforwards session metadata, and the Grok flavor resolves the selected model and reads_meta.totalContextTokens. Tests cover the Grok budget and fallback cases. The supplied change summary shows no Cursor adapter implementation or Cursor test. The current PR scope statement does not remove the Cursor requirement from the directly linked open issue.Resolution
Implement and test the Cursor context-window path required by
#20166, or provide an updated issue scope that explicitly removes the Cursor portion from this direct target.✨ Finishing Touches
🧪 Generate unit tests (beta)
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment `@coderabbitai help` to get the list of available commands.