Skip to content

Commit 163bdea

Browse files
t3dotggclaudeSouthCarpet
authored
fix(server): delegates finish when their leftover work settles, and Grok commands stop holding runs open (#22709)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Fuksss <[email protected]>
1 parent dd8452a commit 163bdea

7 files changed

Lines changed: 764 additions & 15 deletions

File tree

‎apps/server/scripts/acp-mock-agent.ts‎

Lines changed: 122 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ const emitXAiAskUserQuestionThenHang =
4646
process.env.T3_ACP_EMIT_XAI_ASK_USER_QUESTION_THEN_HANG === "1";
4747
const emitContentThenHang = process.env.T3_ACP_EMIT_CONTENT_THEN_HANG === "1";
4848
const emitPlanThenHang = process.env.T3_ACP_EMIT_PLAN_THEN_HANG === "1";
49+
const grokKillTaskShape = process.env.T3_ACP_GROK_KILL_TASK_SHAPE;
4950
const emitActiveToolThenHang = process.env.T3_ACP_EMIT_ACTIVE_TOOL_THEN_HANG === "1";
5051
const emitGrokMonitorPostTurnPoll = process.env.T3_ACP_EMIT_GROK_MONITOR_POST_TURN_POLL === "1";
5152
const emitGrokBackgroundTaskStarted = process.env.T3_ACP_EMIT_GROK_BACKGROUND_TASK_STARTED === "1";
53+
const emitGrokBackgroundTaskPoll = process.env.T3_ACP_GROK_BACKGROUND_TASK_COMPLETION === "poll";
5254
const emitForeignSessionUpdates = process.env.T3_ACP_EMIT_FOREIGN_SESSION_UPDATES === "1";
5355
const waitForResumeRelease = process.env.T3_ACP_WAIT_FOR_RESUME_RELEASE === "1";
5456
const completeFirstPromptOnCancel = process.env.T3_ACP_COMPLETE_FIRST_PROMPT_ON_CANCEL === "1";
@@ -461,6 +463,71 @@ function modelState(): AcpCompat.SessionModelState {
461463
};
462464
}
463465

466+
function emitGrokKillTaskScenario(
467+
sessionId: string,
468+
promptId: string | undefined,
469+
shape: string,
470+
): void {
471+
const taskId = "call-fb9d0000-0000-0000-0000-000000000026-29";
472+
const outcome =
473+
shape === "already_exited" || shape === "known_failure" ? "already_exited" : "killed";
474+
writeJsonRpcNotification("session/update", {
475+
sessionId,
476+
update: {
477+
sessionUpdate: "tool_call_update",
478+
toolCallId: "kill-target-command",
479+
title: "run_terminal_command",
480+
kind: "execute",
481+
status: "in_progress",
482+
rawInput: { command: "sleep 40" },
483+
},
484+
});
485+
writeJsonRpcNotification("session/update", {
486+
sessionId,
487+
update: {
488+
sessionUpdate: "tool_call_update",
489+
toolCallId: "kill-target-command",
490+
status: "completed",
491+
rawOutput: {
492+
type: "BackgroundTaskStarted",
493+
task_id: taskId,
494+
task_type: "bash",
495+
command: "sleep 40",
496+
},
497+
},
498+
});
499+
if (shape === "known_failure") {
500+
writeJsonRpcNotification("_x.ai/task_completed", {
501+
sessionId,
502+
update: {
503+
sessionUpdate: "task_completed",
504+
task_snapshot: { task_id: taskId, kind: "bash", exit_code: 7, output: "known failure\n" },
505+
},
506+
});
507+
}
508+
writeJsonRpcNotification("session/update", {
509+
sessionId,
510+
update: {
511+
sessionUpdate: "tool_call_update",
512+
toolCallId: "kill-command",
513+
title: `kill ${taskId} (${outcome})`,
514+
kind: "other",
515+
status: "completed",
516+
rawInput: {
517+
task_id: taskId,
518+
...(shape === "output_only" ? {} : { variant: "KillTask" }),
519+
},
520+
...(shape === "variant_only" ? {} : { rawOutput: { type: "KillTask", outcome } }),
521+
},
522+
});
523+
writeJsonRpcNotification("_x.ai/session/prompt_complete", {
524+
sessionId,
525+
promptId,
526+
stopReason: "end_turn",
527+
agentResult: null,
528+
});
529+
}
530+
464531
const program = Effect.gen(function* () {
465532
const agent = yield* EffectAcpAgent.AcpAgent;
466533
const resumeRelease = yield* Deferred.make<void>();
@@ -1498,6 +1565,16 @@ const program = Effect.gen(function* () {
14981565
return yield* Effect.never;
14991566
}
15001567

1568+
if (grokKillTaskShape !== undefined) {
1569+
emitGrokKillTaskScenario(
1570+
requestedSessionId,
1571+
promptIdFromRequestMeta(request),
1572+
grokKillTaskShape,
1573+
);
1574+
// Keep the prompt RPC and peer alive after the kill result.
1575+
return yield* Effect.never;
1576+
}
1577+
15011578
if (emitPlanThenHang) {
15021579
yield* agent.client.sessionUpdate({
15031580
sessionId: requestedSessionId,
@@ -1685,13 +1762,15 @@ const program = Effect.gen(function* () {
16851762
return yield* Effect.never;
16861763
}
16871764

1688-
if (emitGrokBackgroundTaskStarted) {
1689-
const toolCallId = "call-fb9d0000-0000-0000-0000-000000000026";
1765+
if (emitGrokBackgroundTaskStarted || emitGrokBackgroundTaskPoll) {
1766+
const toolCallId = !emitGrokBackgroundTaskPoll
1767+
? "call-fb9d0000-0000-0000-0000-000000000026"
1768+
: "call-fb9d0000-0000-0000-0000-000000000026-29";
16901769
const command = "sleep 40; echo done-a";
16911770
writeJsonRpcNotification("session/update", {
16921771
sessionId: requestedSessionId,
16931772
update: {
1694-
sessionUpdate: "tool_call",
1773+
sessionUpdate: !emitGrokBackgroundTaskPoll ? "tool_call" : "tool_call_update",
16951774
toolCallId,
16961775
title: "run_terminal_command",
16971776
kind: "execute",
@@ -1714,6 +1793,46 @@ const program = Effect.gen(function* () {
17141793
},
17151794
},
17161795
});
1796+
if (emitGrokBackgroundTaskPoll) {
1797+
writeJsonRpcNotification("session/update", {
1798+
sessionId: requestedSessionId,
1799+
update: {
1800+
sessionUpdate: "tool_call_update",
1801+
toolCallId: "call-background-poll",
1802+
title: "get_command_or_subagent_output",
1803+
kind: "other",
1804+
status: "completed",
1805+
rawOutput: {
1806+
type: "TaskOutput",
1807+
Result: {
1808+
task_id: toolCallId,
1809+
command,
1810+
status: "completed",
1811+
exit_code: 0,
1812+
output: "done-a",
1813+
},
1814+
},
1815+
},
1816+
});
1817+
1818+
yield* agent.client.sessionUpdate({
1819+
sessionId: requestedSessionId,
1820+
update: {
1821+
sessionUpdate: "agent_message_chunk",
1822+
content: { type: "text", text: "The background command finished." },
1823+
},
1824+
});
1825+
writeJsonRpcNotification("_x.ai/session/update", {
1826+
sessionId: requestedSessionId,
1827+
update: {
1828+
sessionUpdate: "turn_completed",
1829+
prompt_id: promptIdFromRequestMeta(request),
1830+
stop_reason: "end_turn",
1831+
},
1832+
});
1833+
// Neither the prompt RPC nor the process ends, matching the Grok incident.
1834+
return yield* Effect.never;
1835+
}
17171836
writeJsonRpcNotification("_x.ai/session/prompt_complete", {
17181837
sessionId: requestedSessionId,
17191838
promptId: promptIdFromRequestMeta(request) ?? "mock-xai-prompt-1",

‎apps/server/src/orchestration-v2/Adapters/AcpAdapterV2.test.ts‎

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ import {
6363
} from "@t3tools/provider-acp/server/sessionConfig";
6464
import {
6565
extractXAiAcpSubagentUpdate,
66+
makeGrokAdapterV2,
67+
makeXAiPromptCompletionRuntime,
6668
normalizeXAiAcpToolCallState,
6769
registerXAiBackgroundTaskTracking,
6870
} from "@t3tools/provider-grok/testing";
@@ -3079,6 +3081,193 @@ describe("AcpAdapterV2", () => {
30793081
}).pipe(Effect.provide(layerTest), Effect.scoped),
30803082
);
30813083

3084+
// Oracle: Grok incident frames. Command completion must release the turn even
3085+
// when the CLI keeps the prompt RPC and process alive. Command polls stay visible.
3086+
it.live(
3087+
"completes a structured Grok background command from poll with an unanswered prompt RPC",
3088+
() =>
3089+
Effect.gen(function* () {
3090+
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
3091+
const path = yield* Path.Path;
3092+
const mockAgentPath = yield* path.fromFileUrl(
3093+
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
3094+
);
3095+
const instanceId = ProviderInstanceId.make("grok-background-completion");
3096+
const adapter = yield* makeGrokAdapterV2({
3097+
instanceId,
3098+
settings: DEFAULT_GROK_SETTINGS,
3099+
environment: {},
3100+
hostPlatform: yield* HostProcess.Platform,
3101+
selfInvocation: yield* resolveSelfInvocation(),
3102+
makeRuntime: (input) =>
3103+
makeMockRuntime({
3104+
childProcessSpawner,
3105+
mockAgentPath,
3106+
environment: { T3_ACP_GROK_BACKGROUND_TASK_COMPLETION: "poll" },
3107+
})(input).pipe(Effect.flatMap(makeXAiPromptCompletionRuntime)),
3108+
});
3109+
const threadId = ThreadId.make("grok-background-completion");
3110+
const modelSelection = { instanceId, model: "default" } as const;
3111+
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3112+
runtimeMode: "full-access",
3113+
interactionMode: "default",
3114+
cwd: process.cwd(),
3115+
});
3116+
const runtime = yield* adapter.openSession({
3117+
threadId,
3118+
providerSessionId: ProviderSessionId.make("grok-background-completion"),
3119+
modelSelection,
3120+
runtimePolicy,
3121+
});
3122+
const providerThread = yield* runtime.ensureThread({
3123+
threadId,
3124+
modelSelection,
3125+
runtimePolicy,
3126+
});
3127+
const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
3128+
yield* runtime.events.pipe(
3129+
Stream.runForEach((event) => Queue.offer(events, event)),
3130+
Effect.forkScoped,
3131+
);
3132+
yield* runtime.startTurn(
3133+
makeTurnInput({
3134+
threadId,
3135+
providerThread,
3136+
instanceId,
3137+
runtimePolicy,
3138+
now: yield* DateTime.now,
3139+
}),
3140+
);
3141+
const observed = yield* Stream.fromQueue(events).pipe(
3142+
Stream.takeUntil((event) => event.type === "turn.terminal"),
3143+
Stream.runCollect,
3144+
);
3145+
const allEvents = [...observed];
3146+
let next = yield* Queue.poll(events);
3147+
while (Option.isSome(next)) {
3148+
allEvents.push(next.value);
3149+
next = yield* Queue.poll(events);
3150+
}
3151+
const terminals = allEvents.filter((event) => event.type === "turn.terminal");
3152+
assert.deepEqual(
3153+
terminals.map((event) => event.status),
3154+
["completed"],
3155+
);
3156+
const items = allEvents.flatMap((event) =>
3157+
event.type === "turn_item.updated" ? [event.turnItem] : [],
3158+
);
3159+
assert.isFalse(items.some((item) => item.type === "subagent"));
3160+
const command = items.findLast(
3161+
(item) => item.nativeItemRef?.nativeId === "call-fb9d0000-0000-0000-0000-000000000026-29",
3162+
);
3163+
assert.equal(command?.type, "command_execution");
3164+
assert.equal(command?.status, "completed");
3165+
assert.isTrue(
3166+
items.some((item) => item.nativeItemRef?.nativeId === "call-background-poll"),
3167+
"command TaskOutput must retain its normal tool frame",
3168+
);
3169+
}).pipe(Effect.provide(layerTest), Effect.scoped),
3170+
);
3171+
3172+
// Oracle: #15888 KillTask reports. Command completion must release the turn even
3173+
// when the CLI keeps the prompt RPC and process alive. Kill calls stay visible.
3174+
it.live.each([
3175+
{ shape: "output_only", expectedCommand: { type: "command_execution", status: "completed" } },
3176+
{ shape: "variant_only", expectedCommand: { type: "command_execution", status: "completed" } },
3177+
{ shape: "killed", expectedCommand: { type: "command_execution", status: "completed" } },
3178+
{
3179+
shape: "already_exited",
3180+
expectedCommand: { type: "command_execution", status: "completed" },
3181+
},
3182+
{
3183+
shape: "known_failure",
3184+
expectedCommand: { type: "command_execution", status: "failed", output: "known failure\n" },
3185+
},
3186+
] as const)(
3187+
"completes a Grok KillTask command ($shape) and preserves any known result",
3188+
({ shape, expectedCommand }) =>
3189+
Effect.gen(function* () {
3190+
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;
3191+
const path = yield* Path.Path;
3192+
const mockAgentPath = yield* path.fromFileUrl(
3193+
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
3194+
);
3195+
const instanceId = ProviderInstanceId.make("grok-kill-completion");
3196+
const adapter = yield* makeGrokAdapterV2({
3197+
instanceId,
3198+
settings: DEFAULT_GROK_SETTINGS,
3199+
environment: {},
3200+
hostPlatform: yield* HostProcess.Platform,
3201+
selfInvocation: yield* resolveSelfInvocation(),
3202+
makeRuntime: (input) =>
3203+
makeMockRuntime({
3204+
childProcessSpawner,
3205+
mockAgentPath,
3206+
environment: { T3_ACP_GROK_KILL_TASK_SHAPE: shape },
3207+
})(input).pipe(Effect.flatMap(makeXAiPromptCompletionRuntime)),
3208+
});
3209+
const threadId = ThreadId.make("grok-kill-completion");
3210+
const modelSelection = { instanceId, model: "default" } as const;
3211+
const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
3212+
runtimeMode: "full-access",
3213+
interactionMode: "default",
3214+
cwd: process.cwd(),
3215+
});
3216+
const runtime = yield* adapter.openSession({
3217+
threadId,
3218+
providerSessionId: ProviderSessionId.make("grok-kill-completion"),
3219+
modelSelection,
3220+
runtimePolicy,
3221+
});
3222+
const providerThread = yield* runtime.ensureThread({
3223+
threadId,
3224+
modelSelection,
3225+
runtimePolicy,
3226+
});
3227+
const events = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>();
3228+
yield* runtime.events.pipe(
3229+
Stream.runForEach((event) => Queue.offer(events, event)),
3230+
Effect.forkScoped,
3231+
);
3232+
yield* runtime.startTurn(
3233+
makeTurnInput({
3234+
threadId,
3235+
providerThread,
3236+
instanceId,
3237+
runtimePolicy,
3238+
now: yield* DateTime.now,
3239+
}),
3240+
);
3241+
const observed = yield* Stream.fromQueue(events).pipe(
3242+
Stream.takeUntil((event) => event.type === "turn.terminal"),
3243+
Stream.runCollect,
3244+
);
3245+
const allEvents = [...observed];
3246+
let next = yield* Queue.poll(events);
3247+
while (Option.isSome(next)) {
3248+
allEvents.push(next.value);
3249+
next = yield* Queue.poll(events);
3250+
}
3251+
const terminals = allEvents.filter((event) => event.type === "turn.terminal");
3252+
assert.deepEqual(
3253+
terminals.map((event) => event.status),
3254+
["completed"],
3255+
);
3256+
const items = allEvents.flatMap((event) =>
3257+
event.type === "turn_item.updated" ? [event.turnItem] : [],
3258+
);
3259+
assert.isFalse(items.some((item) => item.type === "subagent"));
3260+
const command = items.findLast(
3261+
(item) => item.nativeItemRef?.nativeId === "kill-target-command",
3262+
);
3263+
assert.deepInclude(command, expectedCommand);
3264+
assert.isTrue(
3265+
items.some((item) => item.nativeItemRef?.nativeId === "kill-command"),
3266+
"KillTask must retain its normal tool frame",
3267+
);
3268+
}).pipe(Effect.provide(layerTest), Effect.scoped),
3269+
);
3270+
30823271
it.effect("keeps an agent's empty-valued default choice selectable", () =>
30833272
Effect.gen(function* () {
30843273
const childProcessSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;

0 commit comments

Comments
 (0)