Skip to content

fix(grok): Complete turns from xAI prompt completion#3156

Open
mwolson wants to merge 1 commit into
pingdotgg:mainfrom
mwolson:fix/grok-prompt-complete-fallback
Open

fix(grok): Complete turns from xAI prompt completion#3156
mwolson wants to merge 1 commit into
pingdotgg:mainfrom
mwolson:fix/grok-prompt-complete-fallback

Conversation

@mwolson

@mwolson mwolson commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add an xAI prompt-complete fallback for Grok ACP prompts that finish on the wire but leave session/prompt stranded.
  • Keep Grok adapter session snapshots in running only while prompts are actually in flight, then clear activeTurnId on completion.
  • Add direct runtime and adapter regression coverage for the Grok completion hang, plus protocol coverage for Grok extension notifications batched before a standard prompt response.

Problem and Fix

This is a narrow bridge fix for the current Grok Composer 2.5 Fast hang while the sturdier ACP/orchestrator work in #2829 continues.

Problem and Why it Happened Fix
Grok can emit _x.ai/session/prompt_complete and return the session to idle while the standard session/prompt RPC remains stranded. T3 Code waits for session/prompt before emitting turn.completed, so the composer can stay stuck on Stop even though the turn is done. Register a per-session fallback before each ACP prompt and race the standard prompt RPC against _x.ai/session/prompt_complete, synthesizing the normal ACP PromptResponse shape when the xAI notification wins.
Grok adapter session snapshots could retain activeTurnId after the prompt settled. Consumers reading adapter session state could still see the session as active. Mark Grok sessions running while prompt work is active and restore ready while clearing activeTurnId when the final prompt in the turn settles.
The existing tests did not reproduce a Grok prompt that completes through xAI's private notification while the prompt RPC never returns. Extend the ACP mock agent with a prompt-complete-then-hang mode and cover it at both AcpSessionRuntime and GrokAdapter levels.

Defensive Fixes

Problem and Why it Happened Fix
Grok extension notifications can be batched before the normal session/prompt response. Add effect-acp client coverage that keeps the standard prompt response routable after Grok extension notifications in the same input batch.
Multiple pending prompt fallbacks on one session are unlikely in the current composer flow but possible through steering paths. Resolve fallback completions FIFO by session, which avoids letting an older completion unblock the newest prompt.

Validation

  • vp test apps/server/src/provider/Layers/GrokAdapter.test.ts apps/server/src/provider/acp/AcpJsonRpcConnection.test.ts packages/effect-acp/src/client.test.ts
  • vp run typecheck
  • vp check

Note

Fix Grok turn completion by resolving prompts from xAI _x.ai/session/prompt_complete notifications

  • AcpSessionRuntime now races the standard session/prompt RPC against a fallback Deferred resolved by _x.ai/session/prompt_complete notifications, handling the case where Grok emits its own completion signal but never settles the RPC.
  • Stale prompt_complete notifications (for already-completed prompts) are tracked and ignored using a bounded list of completed prompt IDs.
  • GrokAdapter marks sessions as running at prompt start and transitions back to ready only when the last in-flight prompt for the active turn settles, including on error or RPC hang.
  • A mock agent script gains environment flags to simulate xAI prompt completion then hang, stale completions, and prompt failures for integration testing.

Macroscope summarized 1b2c893.


Note

Medium Risk
Changes Grok turn completion and session lifecycle in the provider stack; behavior is narrow and heavily regression-tested but affects when turn.completed fires and adapter session snapshots update.

Overview
Fixes Grok turns that finish on the wire but never unblock the UI because session/prompt never returns.

AcpSessionRuntime now races each prompt against Grok’s _x.ai/session/prompt_complete notification, building a normal PromptResponse when the xAI signal wins first. Already-finished promptId values are remembered so late duplicate completions are ignored.

GrokAdapter sets sessions to running while prompts are in flight and returns them to ready (clearing activeTurnId and emitting turn.completed) only when the last prompt for the active turn settles, including on RPC failure via settlePromptInFlight.

The ACP mock agent gains env-driven modes (prompt-complete-then-hang, stale completion, forced prompt failure), with coverage in AcpJsonRpcConnection, GrokAdapter, and effect-acp client batching tests.

Reviewed by Cursor Bugbot for commit 1b2c893. Bugbot is set up for automated code reviews on this repo. Configure here.

@coderabbitai

coderabbitai Bot commented Jun 19, 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

Run ID: 3b672871-bb73-48b7-a6aa-6039a45ac063

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
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

@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 Jun 19, 2026
@mwolson mwolson marked this pull request as ready for review June 19, 2026 03:20
Comment thread apps/server/src/provider/Layers/GrokAdapter.ts
Comment thread apps/server/src/provider/acp/AcpSessionRuntime.ts Outdated
@macroscopeapp

macroscopeapp Bot commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Needs human review

1 blocking correctness issue found. This PR introduces a new fallback mechanism for completing Grok turns with complex async state management. Unresolved review comments identify potential race conditions where turn.completed may not be emitted in concurrent prompt scenarios, warranting human review to verify correctness.

You can customize Macroscope's approvability policy. Learn more.

@mwolson mwolson force-pushed the fix/grok-prompt-complete-fallback branch 4 times, most recently from 59e347f to 7ac3370 Compare June 19, 2026 03:44

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 59e347f. Configure here.

Comment thread apps/server/src/provider/Layers/GrokAdapter.ts Outdated
Comment thread apps/server/src/provider/Layers/GrokAdapter.ts Outdated
@mwolson mwolson force-pushed the fix/grok-prompt-complete-fallback branch from 7ac3370 to 4ffdde1 Compare June 19, 2026 03:49
@mwolson mwolson force-pushed the fix/grok-prompt-complete-fallback branch from 4ffdde1 to 1b2c893 Compare June 20, 2026 02:04
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