fix(server): Grok's reply to a finished background command opens its own run #14488
t3code/codex-turn-mapping fromrefs/pull/14488/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
juliusmarminge added labelsmacroscope-review
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/orchestration-v2/testkit/fixtures/grok_background_bash_fast_wake/grok_transcript.ndjson line 181Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +161,21 @@161+{"type":"emit_inbound","label":"notification:_x.ai/sessions/changed","frame":{"kind":"notification","method":"_x.ai/sessions/changed","params":{"upserted":[{"sessionId":"00000000-0000-4000-8000-000000000001","title":null,"cwd":"<workspace>","isWorktree":false,"modelId":"grok-4.7-build-fast","reasoningEffort":"high","yolo":true,"activity":"idle","resident":true,"lastChangeUnixMs":1790806320996,"origin":{"kind":"local"}}],"removed":[]}}}162+{"type":"emit_inbound","label":"response:session/prompt","frame":{"kind":"response","method":"session/prompt","result":{"stopReason":"end_turn","_meta":{"sessionId":"00000000-0000-4000-8000-000000000001","requestId":"t3-xai-prompt-1","promptId":"t3-xai-prompt-1","totalTokens":24207,"modelId":"grok-4.7-build-fast","inputTokens":24153,"outputTokens":45,"cachedReadTokens":23424,"reasoningTokens":42,"usage":{"inputTokens":47609,"outputTokens":452,"totalTokens":48061,"cachedReadTokens":25088,"cacheCreationTokens":0,"reasoningTokens":381,"modelCalls":2,"apiDurationMs":5678,"costUsdTicks":410026400,"modelUsage":{"grok-4.7-build-fast":{"inputTokens":47609,"outputTokens":452,"totalTokens":48061,"cachedReadTokens":25088,"cacheCreationTokens":0,"reasoningTokens":381,"modelCalls":2,"apiDurationMs":5678,"costUsdTicks":410026400}},"numTurns":2}}}}}163+{"type":"emit_inbound","label":"notification:_x.ai/session_notification:last_turn_summary","frame":{"kind":"notification","method":"_x.ai/session_notification","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"last_turn_summary","summary":"ROOT_DONE — background tock loop started","prompt_id":"t3-xai-prompt-1"},"_meta":{"agentTimestampMs":1790806323713}}}}164+{"type":"emit_inbound","label":"notification:session/update:tool_call_update","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"tool_call_update","toolCallId":"call-d7a33c86-7bd4-4e3a-9a05-472873bf3c87-0","status":"in_progress","content":[{"type":"content","content":{"type":"text","text":"tock 1\n"}}],"rawOutput":{"type":"Bash","output":[116,111,99,107,32,49,10],"output_for_prompt":"tock 1\n","exit_code":0,"command":"for i in 1 2 3; do sleep 8; echo tock $i; done","truncated":false,"signal":null,"timed_out":false,"description":null,"current_dir":"<workspace>","output_file":"","total_bytes":7}}}}}165+{"type":"emit_inbound","label":"notification:session/update:tool_call_update","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"tool_call_update","toolCallId":"call-d7a33c86-7bd4-4e3a-9a05-472873bf3c87-0","status":"in_progress","content":[{"type":"content","content":{"type":"text","text":"tock 1\ntock 2\n"}}],"rawOutput":{"type":"Bash","output":[116,111,99,107,32,49,10,116,111,99,107,32,50,10],"output_for_prompt":"tock 1\ntock 2\n","exit_code":0,"command":"for i in 1 2 3; do sleep 8; echo tock $i; done","truncated":false,"signal":null,"timed_out":false,"description":null,"current_dir":"<workspace>","output_file":"","total_bytes":14}}}}}166+{"type":"emit_inbound","label":"notification:session/update:tool_call_update","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"tool_call_update","toolCallId":"call-d7a33c86-7bd4-4e3a-9a05-472873bf3c87-0","status":"in_progress","content":[{"type":"content","content":{"type":"text","text":"tock 1\ntock 2\ntock 3\n"}}],"rawOutput":{"type":"Bash","output":[116,111,99,107,32,49,10,116,111,99,107,32,50,10,116,111,99,107,32,51,10],"output_for_prompt":"tock 1\ntock 2\ntock 3\n","exit_code":0,"command":"for i in 1 2 3; do sleep 8; echo tock $i; done","truncated":false,"signal":null,"timed_out":false,"description":null,"current_dir":"<workspace>","output_file":"","total_bytes":21}}}}}167+{"type":"emit_inbound","label":"notification:_x.ai/queue/changed","frame":{"kind":"notification","method":"_x.ai/queue/changed","params":{"sessionId":"00000000-0000-4000-8000-000000000001","entries":[]}}}168+{"type":"emit_inbound","label":"notification:_x.ai/queue/changed","frame":{"kind":"notification","method":"_x.ai/queue/changed","params":{"sessionId":"00000000-0000-4000-8000-000000000001","entries":[],"runningPromptId":"task-completed-00000000-0000-4000-8000-000000000002","runningText":"<system-reminder>\nBackground task \"00000000-0000-4000-8000-000000000002\" completed (exit code: 0).\nDescription: Run tock loop in background | Duration: 24.1s\nUse get_command_or_subagent_output(\"00000000-0000-4000-8000-000000000002\") to see the full output.\n</system-reminder>","runningKind":"prompt"}}}169+{"type":"emit_inbound","label":"notification:_x.ai/task_completed:task_completed","frame":{"kind":"notification","method":"_x.ai/task_completed","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"task_completed","task_snapshot":{"task_id":"00000000-0000-4000-8000-000000000002","command":"for i in 1 2 3; do sleep 8; echo tock $i; done","cwd":"<workspace>","start_time":{"secs_since_epoch":1790806318,"nanos_since_epoch":653022000},"end_time":{"secs_since_epoch":1790806342,"nanos_since_epoch":726097000},"output":"tock 1\ntock 2\ntock 3\n","output_file":"/home/grok-replay/.grok/sessions/%3Cworkspace%3E/00000000-0000-4000-8000-000000000001/terminal/call-d7a33c86-7bd4-4e3a-9a05-472873bf3c87-0.log","truncated":false,"output_total_bytes":21,"exit_code":0,"signal":null,"completed":true,"kind":"bash","block_waited":false,"explicitly_killed":false,"kill_result_delivered":false,"owner_session_id":"00000000-0000-4000-8000-000000000001","description":"Run tock loo170+{"type":"emit_inbound","label":"notification:_x.ai/session_notification:background_tasks","frame":{"kind":"notification","method":"_x.ai/session_notification","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"background_tasks","tasks":[{"task_id":"00000000-0000-4000-8000-000000000002","command":"for i in 1 2 3; do sleep 8; echo tock $i; done","description":"Run tock loop in background","cwd":"<workspace>","kind":"bash","status":"completed","started_at":"2026-09-30T22:11:58.653022+00:00","ended_at":"2026-09-30T22:12:22.726097+00:00","output_file":"/home/grok-replay/.grok/sessions/%3Cworkspace%3E/00000000-0000-4000-8000-000000000001/terminal/call-d7a33c86-7bd4-4e3a-9a05-472873bf3c87-0.log","exit_code":0}]},"_meta":{"eventId":"00000000-0000-4000-8000-000000000001-122","agentTimestampMs":1790806342782}}}}171+{"type":"emit_inbound","label":"notification:_x.ai/session_notification:hook_run_started","frame":{"kind":"notification","method":"_x.ai/session_notification","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"hook_run_started","event_name":"user_prompt_submit","prompt_id":"task-completed-00000000-0000-4000-8000-000000000002","count":1},"_meta":{"agentTimestampMs":1790806342783}}}}172+{"type":"emit_inbound","label":"notification:_x.ai/session_notification:hook_execution","frame":{"kind":"notification","method":"_x.ai/session_notification","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"hook_execution","event_name":"user_prompt_submit","prompt_id":"task-completed-00000000-0000-4000-8000-000000000002","runs":[{"name":"global/settings:user_prompt_submit[0].hooks[0]","status":{"status":"success","elapsed_ms":642}}]},"_meta":{"eventId":"00000000-0000-4000-8000-000000000001-123","agentTimestampMs":1790806343425}}}}173+{"type":"emit_inbound","label":"notification:session/update:agent_thought_chunk:task-completed-00000000-0000-4000-8000-000000000002","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"The"}},"_meta":{"totalTokens":24275,"eventId":"00000000-0000-4000-8000-000000000001-126","agentTimestampMs":1790806344682,"promptId":"task-completed-00000000-0000-4000-8000-000000000002","streamStartMs":1790806344157,"turnStartMs":1790806343501,"updateType":"AgentThoughtChunk","chunkId":1}}}}174+{"type":"emit_inbound","label":"notification:session/update:agent_thought_chunk:task-completed-00000000-0000-4000-8000-000000000002","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" background"}},"_meta":{"totalTokens":24275,"eventId":"00000000-0000-4000-8000-000000000001-127","agentTimestampMs":1790806344682,"promptId":"task-completed-00000000-0000-4000-8000-000000000002","streamStartMs":1790806344157,"turnStartMs":1790806343501,"updateType":"AgentThoughtChunk","chunkId":2}}}}175+{"type":"emit_inbound","label":"notification:session/update:agent_thought_chunk:task-completed-00000000-0000-4000-8000-000000000002","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" task"}},"_meta":{"totalTokens":24275,"eventId":"00000000-0000-4000-8000-000000000001-128","agentTimestampMs":1790806344682,"promptId":"task-completed-00000000-0000-4000-8000-000000000002","streamStartMs":1790806344157,"turnStartMs":1790806343501,"updateType":"AgentThoughtChunk","chunkId":3}}}}176+{"type":"emit_inbound","label":"notification:session/update:agent_thought_chunk:task-completed-00000000-0000-4000-8000-000000000002","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" completed"}},"_meta":{"totalTokens":24275,"eventId":"00000000-0000-4000-8000-000000000001-129","agentTimestampMs":1790806344682,"promptId":"task-completed-00000000-0000-4000-8000-000000000002","streamStartMs":1790806344157,"turnStartMs":1790806343501,"updateType":"AgentThoughtChunk","chunkId":4}}}}177+{"type":"emit_inbound","label":"notification:session/update:agent_thought_chunk:task-completed-00000000-0000-4000-8000-000000000002","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":"."}},"_meta":{"totalTokens":24275,"eventId":"00000000-0000-4000-8000-000000000001-130","agentTimestampMs":1790806344682,"promptId":"task-completed-00000000-0000-4000-8000-000000000002","streamStartMs":1790806344157,"turnStartMs":1790806343501,"updateType":"AgentThoughtChunk","chunkId":5}}}}178+{"type":"emit_inbound","label":"notification:session/update:agent_thought_chunk:task-completed-00000000-0000-4000-8000-000000000002","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" The"}},"_meta":{"totalTokens":24275,"eventId":"00000000-0000-4000-8000-000000000001-131","agentTimestampMs":1790806344682,"promptId":"task-completed-00000000-0000-4000-8000-000000000002","streamStartMs":1790806344157,"turnStartMs":1790806343501,"updateType":"AgentThoughtChunk","chunkId":6}}}}179+{"type":"emit_inbound","label":"notification:session/update:agent_thought_chunk:task-completed-00000000-0000-4000-8000-000000000002","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" user"}},"_meta":{"totalTokens":24275,"eventId":"00000000-0000-4000-8000-000000000001-132","agentTimestampMs":1790806344682,"promptId":"task-completed-00000000-0000-4000-8000-000000000002","streamStartMs":1790806344157,"turnStartMs":1790806343501,"updateType":"AgentThoughtChunk","chunkId":7}}}}180+{"type":"emit_inbound","label":"notification:session/update:agent_thought_chunk:task-completed-00000000-0000-4000-8000-000000000002","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":" said"}},"_meta":{"totalTokens":24275,"eventId":"00000000-0000-4000-8000-000000000001-133","agentTimestampMs":1790806344682,"promptId":"task-completed-00000000-0000-4000-8000-000000000002","streamStartMs":1790806344157,"turnStartMs":1790806343501,"updateType":"AgentThoughtChunk","chunkId":8}}}}181+{"type":"emit_inbound","label":"notification:session/update:agent_thought_chunk:task-completed-00000000-0000-4000-8000-000000000002","frame":{"kind":"notification","method":"session/update","params":{"sessionId":"00000000-0000-4000-8000-000000000001","update":{"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":":"}},"_meta":{"totalTokens":24275,"eventId":"00000000-0000-4000-8000-000000000001-134","agentTimestampMs":1790806344682,"promptId":"task-completed-00000000-0000-4000-8000-000000000002","streamStartMs":1790806344157,"turnStartMs":1790806343501,"updateType":"AgentThoughtChunk","chunkId":9}}}}
- apps/server/src/provider/acp/XAiAcpExtension.ts line 1421OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1388,2 +1401,21 @@13881401 }138914021403+/**1404+ * Grok settles a failed prompt with `stopReason: "error"` and the provider's1405+ * message in `agentResult` before its `session/prompt` RPC error arrives. The1406+ * completion wins the race, so it must carry the failure itself.1407+ */1408+function xAiPromptFailure(1409+ notification: XAiPromptCompleteNotification,1410+): EffectAcpErrors.AcpRequestError | null {1411+ if (notification.stopReason === "rate_limit") {1412+ return new EffectAcpErrors.AcpRequestError({1413+ code: xAiRateLimitedErrorCode,1414+ errorMessage: "Grok usage limit reached. Try again later.",1415+ });1416+ }1417+ if (notification.stopReason === "error") {1418+ return new EffectAcpErrors.AcpRequestError({1419+ code: -32603,1420+ errorMessage:1421+ nonEmptyString(notification.agentResult) ?? "Grok ended the turn with an error.",
- apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.ts line 4353Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -4319,3 +4336,18 @@43194336 // Re-check after the activeSessionId yield: idle/prompt settle can43204337 // finalize the same context object while we waited.43214338 if (context.finalized) return;4339+ // The agent started its own turn about finished background work while4340+ // this settled root turn was still held for it. That turn is the4341+ // agent's reply, not more of the root: settle the root first so the4342+ // frame reaches the wake path and opens a continuation run.4343+ if (4344+ context.promptSettled &&4345+ !acpIsProviderContinuationMessage(context.input.message) &&4346+ flavor.isProviderWakeNotification?.(notification) === true4347+ ) {4348+ if (hasDeferredBackgroundWork(context)) {4349+ yield* Ref.update(wakeBuffer, (current) => [...current, notification]);4350+ return;4351+ }4352+ yield* finalizeTurn(context, context.promptSettledStatus ?? "completed");4353+ const wake = yield* bufferPostSettleWake(notification);
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
juliusmarminge closed this
juliusmarminge reopened this
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/provider/acp/XAiAcpExtension.ts line 1432OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1404,12 +1415,18 @@14041415 });14051416 }14061417 if (notification.stopReason === "error") {1418+ // Grok's raw result is unbounded provider text: keep it only as the cause.1419+ const agentResult = nonEmptyString(notification.agentResult);14071420 return new EffectAcpErrors.AcpRequestError({14081421 code: -32603,1409- errorMessage:1410- nonEmptyString(notification.agentResult) ?? "Grok ended the turn with an error.",1422+ errorMessage: "Grok ended the turn with an error.",1423+ operation: "receive-response",1424+ ...(agentResult === undefined ? {} : { cause: new XAiPromptError({ agentResult }) }),14111425 });14121426 }14131427 return null;14141428 }141514291430+/** Grok's own text for a failed prompt, carried only as a cause. */1431+export class XAiPromptError extends Schema.TaggedError<XAiPromptError>()("XAiPromptError", {1432+ agentResult: Schema.String,
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
juliusmarminge merged this9449473
juliusmarminge closed this
Sign in to join this conversation.
Macroscope has since reviewed this pull request. An earlier review was skipped by a cost limit; a review has now completed, so that notice no longer applies.