fix(acp): make xai cancellation reliable

Cancel pending prompt completions without restarting ACP, and keep text-only monitor acknowledgements tracked as background work.

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-08-07 13:26:25 +02:00
1 parent 2486ebcf95
commit 9256400b27
2 files changed
+77 -5

No files matched your search

@@ -339,6 +339,29 @@ describe("XAiAcpExtension", () => {
expect(extractXAiMonitorTaskId(toolCall)).toBe("019f44a5-87d1-7640-8e35-6a4667ffc873");
});
it("tracks text Monitor ACKs with generic titles and description-only input", () => {
const toolCall = {
toolCallId: "call-mon-generic-text",
title: "Tool",
status: "completed" as const,
data: {
rawInput: {
command: "echo mon_line_1",
description: "Demo monitor without variant",
},
rawOutput: {
type: "Text",
text: "Monitor started (task 019f44a5-87d1-7640-8e35-6a4667ffc873, timeout 36000000ms).\nYou will be notified on each event.",
},
},
};
const normalized = normalizeXAiAcpToolCallState(toolCall);
expect(isXAiMonitorTool(normalized)).toBe(true);
expect(normalized.status).toBe("inProgress");
expect(extractXAiMonitorTaskId(normalized)).toBe("019f44a5-87d1-7640-8e35-6a4667ffc873");
});
it("completes Monitor variant tools from structured Bash exit codes", () => {
const toolCall = {
toolCallId: "call-mon-2",
@@ -1517,4 +1540,44 @@ describe("XAiAcpExtension", () => {
});
}),
);
it.effect("cancels pending completions without restarting a stalled runtime", () =>
Effect.gen(function* () {
const hungStart = yield* Deferred.make<never>();
const hungPrompt = yield* Deferred.make<never>();
let startCount = 0;
let cancelCalled = false;
const baseRuntime = {
start: () => {
startCount += 1;
return startCount === 1
? Effect.succeed({
sessionId: "root-session",
initializeResult: {},
sessionSetupResult: {},
modelConfigId: undefined,
})
: Deferred.await(hungStart);
},
prompt: () => Deferred.await(hungPrompt),
cancel: Effect.sync(() => {
cancelCalled = true;
}),
handleExtNotification: () => Effect.void,
handleExtRequest: () => Effect.void,
} as unknown as AcpSessionRuntime.AcpSessionRuntime["Service"];
const runtime = yield* makeXAiPromptCompletionRuntime(baseRuntime);
const promptFiber = yield* runtime
.prompt({ prompt: [{ type: "text", text: "hi" }] })
.pipe(Effect.forkChild);
yield* Effect.yieldNow;
yield* runtime.cancel;
expect(cancelCalled).toBe(true);
expect(startCount).toBe(1);
expect((yield* Fiber.join(promptFiber)).stopReason).toBe("cancelled");
}),
);
});
@@ -247,7 +247,12 @@ export function isXAiMonitorTool(toolCall: AcpToolCallState): boolean {
// prevent monitor registration / inProgress forcing.
const rawOutput = unknownRecord(toolCall.data.rawOutput);
const outputType = nonEmptyString(rawOutput?.type)?.toLowerCase();
return outputType === "monitor";
if (outputType === "monitor") return true;
// Released clients can wrap the start ACK as plain Text and omit the Monitor
// variant. The ACK is still unambiguous and contains the task id required for
// background tracking, even after a generic title is rewritten from
// rawInput.description.
return isXAiMonitorStartAck(xAiToolOutputText(toolCall));
}
export function extractXAiMonitorTaskId(toolCall: AcpToolCallState): string | undefined {
@@ -1038,14 +1043,16 @@ const unregisterXAiPromptCompletionFallback = (
const abortPendingPromptCompletions = (
pendingRef: Ref.Ref<ReadonlyArray<PendingXAiPromptCompletion>>,
sessionId: string,
sessionId?: string,
) =>
Ref.modify(pendingRef, (pending) => {
const [toAbort, remaining] = pending.reduce<
[ReadonlyArray<PendingXAiPromptCompletion>, ReadonlyArray<PendingXAiPromptCompletion>]
>(
([aborting, kept], entry) =>
entry.sessionId === sessionId ? [[...aborting, entry], kept] : [aborting, [...kept, entry]],
sessionId === undefined || entry.sessionId === sessionId
? [[...aborting, entry], kept]
: [aborting, [...kept, entry]],
[[], []],
);
if (toAbort.length === 0) {
@@ -1241,8 +1248,10 @@ export const makeXAiPromptCompletionRuntime = Effect.fn("makeXAiPromptCompletion
);
}),
cancel: Effect.gen(function* () {
const started = yield* runtime.start();
yield* abortPendingPromptCompletions(pendingXAiPromptCompletionsRef, started.sessionId);
// Do not call start here: cancellation must be able to reach the
// underlying runtime when startup is stalled. Pending entries already
// carry their session ids, and this runtime owns all of them.
yield* abortPendingPromptCompletions(pendingXAiPromptCompletionsRef);
yield* runtime.cancel;
}),
} satisfies AcpSessionRuntime.AcpSessionRuntime["Service"];