From ff31b55cc90fce14934f26a5f3f084e2840f9bd6 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 28 Jul 2026 12:53:33 +0200 Subject: [PATCH] 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 --- .../src/provider/acp/XAiAcpExtension.test.ts | 63 +++++++++++++++++++ .../src/provider/acp/XAiAcpExtension.ts | 19 ++++-- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/apps/server/src/provider/acp/XAiAcpExtension.test.ts b/apps/server/src/provider/acp/XAiAcpExtension.test.ts index 31117a995c..0086c08278 100644 --- a/apps/server/src/provider/acp/XAiAcpExtension.test.ts +++ b/apps/server/src/provider/acp/XAiAcpExtension.test.ts @@ -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(); + const hungPrompt = yield* Deferred.make(); + 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"); + }), + ); }); diff --git a/apps/server/src/provider/acp/XAiAcpExtension.ts b/apps/server/src/provider/acp/XAiAcpExtension.ts index 40e9ed83b4..ab8c7acefa 100644 --- a/apps/server/src/provider/acp/XAiAcpExtension.ts +++ b/apps/server/src/provider/acp/XAiAcpExtension.ts @@ -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>, - sessionId: string, + sessionId?: string, ) => Ref.modify(pendingRef, (pending) => { const [toAbort, remaining] = pending.reduce< [ReadonlyArray, ReadonlyArray] >( ([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"];