Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(provider-grok): report model windows for handoff budgets #20180

MergedScottN-PV(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/20180/headmerged by juliusmarminge View on GitHub

Problem

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.ts reverted to 09c5e7ac01, 1 test fails with assertion errors: "Grok model context windows > budgets handoffs with the selected Grok model's window".
  • vp fmt --check and vp lint report 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 09c5e7ac01 with "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. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration
      • Configuration used: Path: .coderabbit.config.ts
      • Review profile: CHILL
      • Plan: Advanced
      • Run ID: 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.ts
      • apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.ts
      • packages/provider-acp/src/server/adapter.ts
      • packages/provider-grok/src/server/adapter.ts

      Included 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

      Layer / File(s)Summary
      ACP runtime lookup contract
      packages/provider-acp/src/server/adapter.ts
      The adapter flavor can optionally provide a context-window lookup. The session runtime delegates to it with the active session setup.
      Grok model lookup and integration coverage
      packages/provider-grok/src/server/adapter.ts, apps/server/scripts/acp-mock-agent.ts, apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.ts
      The Grok adapter returns a valid context limit for a matching model. The mock agent accepts model and context-token settings, and the integration test checks lookup results and handoff budgets.

      Priority: ➖ Normal

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Bug fix · Severity of issue fixed: Medium

      Suggested reviewers: juliusmarminge

      Merge Risk: ⚪ Minimal · up to 55760

      No confirmed issue currently prevents merging. The behavior when setup metadata omits the selected model remains unverified.

      Security architecture risk: 🔵 Low · up to 55760

      The 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

      • inferred — Manipulated capacity metadata could increase the amount of history admitted to an already selected destination. Its influence remains bounded by the configured handoff cap and the unchanged 64,000 budget ceiling; the default configured cap is 16,000. This is not an unbounded history-transfer capability.

      Trust Boundaries and Controls

      • observed — External session metadata now influences a capacity decision, but only a validated numeric result leaves the provider-specific lookup. Instance matching, selected-model lookup, existing usage limits, and a reserved context margin constrain that influence. The added callback contains no tool execution, credential handling, or permission-policy mutation.

      Resilience and Maintainability Implications

      • inferred — The hook adds no state mutation and returns undefined when no active setup exists. Existing model-application and session-replacement paths maintain that setup. However, its read is outside the transition permit, and complete preflight serialization and post-finalization access were not established. This leaves a metadata-freshness uncertainty, not a demonstrated isolation or handoff-idempotency defect.
      Pre-merge checks: Passed 3 · Failed 1
      Failed Failed checks: 1 warning
      Check nameStatusExplanationResolution
      Linked Issues checkWarningIssue #20166 covers both Cursor and Grok handoff failures. This pull request implements the Grok portion: AcpAdapterV2Flavor.getModelContextWindow forwards session metadata, and the Grok flavor reso…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.
      Passed Passed checks: 3 passed
      Check nameStatusExplanation
      Title checkPassedThe title clearly and concisely describes the main change: reporting Grok model context windows for handoff budgets.
      Description checkPassedThe description includes complete Problem, Change, Scope and approval, and Verification sections. It explains the affected behavior, implementation, linked issue, test results, limitations, and valida…
      Out of Scope Changes checkPassedThe 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 t…
      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 #20166 covers both Cursor and Grok handoff failures. This pull request implements the Grok portion: AcpAdapterV2Flavor.getModelContextWindow forwards 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.

      • Fix all pre-merge checks with AI
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
      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. Approvability

      Verdict: Approved at 557605c

      Macroscope'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).
    4. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      ✅ 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.

    5. juliusmarminge merged thisab89a59

    6. juliusmarminge closed this

Sign in to join this conversation.