Skip to content

Commit dedadc5

Browse files
fix(provider-acp): keep a finished Grok shell from reopening
A later tool update with no terminal projection was normalized back to inProgress when its raw output was still BackgroundTaskStarted. Deferred finalize then held the turn open again. Keep a stored completed or failed status in that case. An explicit projection still wins, and a later update that normalizes to its own terminal status is left alone.
1 parent 7794550 commit dedadc5

3 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎packages/provider-acp/src/server/adapter.backgroundStatus.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,22 @@ describe("applyTerminalProjectedToolStatus", () => {
2828
expect(applyTerminalProjectedToolStatus(backgroundShell, "interrupted")).toBe(backgroundShell);
2929
expect(applyTerminalProjectedToolStatus(backgroundShell, "running")).toBe(backgroundShell);
3030
});
31+
32+
it("keeps a finished shell finished when a later update is only running", () => {
33+
const finished: AcpToolCallState = { ...backgroundShell, status: "failed" };
34+
expect(applyTerminalProjectedToolStatus(backgroundShell, undefined, finished).status).toBe(
35+
"failed",
36+
);
37+
const completed: AcpToolCallState = { ...backgroundShell, status: "completed" };
38+
expect(applyTerminalProjectedToolStatus(backgroundShell, undefined, completed).status).toBe(
39+
"completed",
40+
);
41+
// An explicit projection still wins over the stored status.
42+
expect(applyTerminalProjectedToolStatus(backgroundShell, "completed", finished).status).toBe(
43+
"completed",
44+
);
45+
// A later update that is itself terminal is not rewritten back.
46+
const laterFailure: AcpToolCallState = { ...backgroundShell, status: "failed" };
47+
expect(applyTerminalProjectedToolStatus(laterFailure, undefined, completed)).toBe(laterFailure);
48+
});
3149
});

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,16 +1069,32 @@ type ProjectedToolStatus = ReturnType<typeof toolStatus> | "interrupted";
10691069
* A terminal projection is the caller's decision that the tool ended.
10701070
* Grok's normalize forces a `BackgroundTaskStarted` shell back to inProgress;
10711071
* storing that leaves deferred finalize holding the turn after `task_completed`.
1072+
* A later unprojected update can do the same after the shell was already
1073+
* finished, so a stored completed/failed status is kept when normalize only
1074+
* demotes it to pending or inProgress. An explicit projection still wins, and
1075+
* a later update that normalizes to its own terminal status is left alone.
10721076
* `interrupted` stays off the stored status: turn teardown already projects it,
10731077
* and the ACP tool state has no interrupted variant.
10741078
*/
10751079
export function applyTerminalProjectedToolStatus(
10761080
toolCall: AcpToolCallState,
10771081
projectedStatus: ProjectedToolStatus | undefined,
1082+
previous?: AcpToolCallState,
10781083
): AcpToolCallState {
1079-
if (projectedStatus !== "completed" && projectedStatus !== "failed") return toolCall;
1080-
if (toolCall.status === projectedStatus) return toolCall;
1081-
return { ...toolCall, status: projectedStatus };
1084+
const demoted =
1085+
toolCall.status === undefined ||
1086+
toolCall.status === "pending" ||
1087+
toolCall.status === "inProgress";
1088+
const preserved =
1089+
projectedStatus === undefined &&
1090+
demoted &&
1091+
(previous?.status === "completed" || previous?.status === "failed")
1092+
? previous.status
1093+
: undefined;
1094+
const status = projectedStatus ?? preserved;
1095+
if (status !== "completed" && status !== "failed") return toolCall;
1096+
if (toolCall.status === status) return toolCall;
1097+
return { ...toolCall, status };
10821098
}
10831099

10841100
function nodeStatus(status: ProjectedToolStatus): OrchestrationV2ExecutionNode["status"] {
@@ -3119,6 +3135,7 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
31193135
const toolCall = applyTerminalProjectedToolStatus(
31203136
flavor.normalizeToolCall?.(merged) ?? merged,
31213137
projectedStatus,
3138+
previous,
31223139
);
31233140
context.tools.set(toolCall.toolCallId, toolCall);
31243141
const backgroundTaskId = flavor.extractBackgroundTaskId?.(toolCall);

‎packages/provider-grok/src/server/xaiAcpExtension.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
xAiSubagentFinishedNotice,
3535
XAiAskUserQuestionRequest,
3636
} from "./xaiAcpExtension.ts";
37+
import { applyTerminalProjectedToolStatus } from "@t3tools/provider-acp/server/adapter";
3738
import * as AcpSessionRuntime from "@t3tools/provider-acp/server/AcpSessionRuntime";
3839
import {
3940
type AcpToolCallState,
@@ -751,6 +752,14 @@ describe("XAiAcpExtension", () => {
751752
]);
752753
// A shell poll must not open a phantom subagent for the tool-call id.
753754
expect(extractXAiAcpSubagentUpdate(polled)).toBeUndefined();
755+
// The start ACK is what Grok re-sends. After the shell is failed, normalize
756+
// would reopen it; the stored terminal status has to survive that replay.
757+
const failedShell = { ...started, status: "failed" as const };
758+
const replayed = normalizeXAiAcpToolCallState(mergeToolCallState(failedShell, started));
759+
expect(replayed.status).toBe("inProgress");
760+
expect(applyTerminalProjectedToolStatus(replayed, undefined, failedShell).status).toBe(
761+
"failed",
762+
);
754763
expect(
755764
extractXAiBackgroundTaskCompletion({
756765
...polled,

0 commit comments

Comments
 (0)