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

  • When xAI's RPC hangs without returning a response, AcpSessionRuntime now races the standard session/prompt RPC against a fallback _x.ai/session/prompt_complete notification, resolving whichever arrives first.
  • Stale or duplicate xAI notifications (for already-completed prompts) are ignored using a bounded cache of recently completed prompt IDs.
  • GrokAdapter gains a streamingTurnId field so late session-update chunks can still be attributed to a turn after activeTurnId is cleared, and a new settlePromptInFlight helper centralizes session-state transitions and failed turn.completed emission.
  • The mock agent in acp-mock-agent.ts gains new env switches to simulate hanging prompts with xAI notifications, stale completions, and outright prompt failures.
  • Behavioral Change: session status now transitions to running at turn start and back to ready when the last in-flight prompt settles, rather than only on RPC completion.

Macroscope summarized 1ce246d.


Note

Medium Risk
Changes core Grok turn completion and session state in the provider stack; behavior is narrower and well-tested but affects when turn.completed fires and UI readiness.

Overview
Fixes Grok composer sessions that stay stuck on Stop when Grok finishes on the wire but the standard session/prompt RPC never returns.

ACP runtime registers a per-session fallback before each prompt and races the normal session/prompt call against _x.ai/session/prompt_complete, synthesizing a normal PromptResponse when the xAI notification wins. Already-completed promptIds are ignored so stale completions cannot unblock a later prompt.

Grok adapter keeps sessions running only while prompts are in flight, uses streamingTurnId so trailing session/update chunks still attach after the turn settles, and settlePromptInFlight restores ready, clears activeTurnId, and emits turn.completed (including failed turns when the prompt RPC errors).

Mock agent modes and tests cover hang-after-complete, stale notifications, session lifecycle, and Grok extension notifications batched before the standard prompt response.

Reviewed by Cursor Bugbot for commit 2a9ced0. 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: 84d0ec89-af55-40b6-ab21-00c27a4f1498

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

This PR adds significant new logic for handling Grok/xAI prompt completion as a fallback when the standard RPC hangs, including race conditions between RPC and notification-based completion. The changes affect core session lifecycle and turn settlement behavior, warranting human review despite good test coverage.

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
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 3 times, most recently from 1b2c893 to 3c38b27 Compare June 21, 2026 20:26
Comment thread apps/server/src/provider/Layers/GrokAdapter.ts
@mwolson mwolson force-pushed the fix/grok-prompt-complete-fallback branch from 2037ac6 to 3859afb Compare June 21, 2026 21:01
Comment thread apps/server/src/provider/Layers/GrokAdapter.ts
@mwolson mwolson force-pushed the fix/grok-prompt-complete-fallback branch 2 times, most recently from b6f559c to 17d881a Compare June 21, 2026 21:36
@github-actions github-actions Bot added size:XXL 1,000+ changed lines (additions + deletions). and removed size:L 100-499 changed lines (additions + deletions). labels Jun 21, 2026
@mwolson mwolson force-pushed the fix/grok-prompt-complete-fallback branch from 17d881a to ba54adf Compare June 21, 2026 21:37
Comment thread apps/server/src/provider/Layers/GrokAdapter.ts
@mwolson mwolson force-pushed the fix/grok-prompt-complete-fallback branch from ba54adf to a2aaf61 Compare June 21, 2026 21:50
@github-actions github-actions Bot added size:L 100-499 changed lines (additions + deletions). and removed size:XXL 1,000+ changed lines (additions + deletions). labels Jun 21, 2026
@mwolson mwolson force-pushed the fix/grok-prompt-complete-fallback branch from a2aaf61 to 2a9ced0 Compare June 21, 2026 21:54

@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 high 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 2a9ced0. Configure here.

Comment thread apps/server/src/provider/Layers/GrokAdapter.ts Outdated
Address Bugbot review: prefer streaming turn for late notification chunks
@mwolson mwolson force-pushed the fix/grok-prompt-complete-fallback branch from 2a9ced0 to 1ce246d Compare June 21, 2026 23: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