From 7eb4f5df25262dea0dec5882366956367dded023 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 15 Feb 2026 02:04:22 +0000 Subject: [PATCH] Cover cleanup set+delete dual failure path during stream subscribe Co-authored-by: Eric Allam --- packages/ai/src/chatTransport.test.ts | 78 +++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/packages/ai/src/chatTransport.test.ts b/packages/ai/src/chatTransport.test.ts index be677d551..508faaceb 100644 --- a/packages/ai/src/chatTransport.test.ts +++ b/packages/ai/src/chatTransport.test.ts @@ -1413,6 +1413,59 @@ describe("TriggerChatTransport", function () { expect(errors[0]?.error.message).toBe("stream subscribe root cause"); }); + it("attempts both cleanup steps when set and delete both throw", async function () { + const errors: TriggerChatTransportError[] = []; + const runStore = new FailingCleanupSetAndDeleteRunStore(); + + const server = await startServer(function (req, res) { + if (req.method === "POST" && req.url === "/api/v1/tasks/chat-task/trigger") { + res.writeHead(200, { + "content-type": "application/json", + "x-trigger-jwt": "pk_stream_subscribe_cleanup_both_failure", + }); + res.end(JSON.stringify({ id: "run_stream_subscribe_cleanup_both_failure" })); + return; + } + + res.writeHead(404); + res.end(); + }); + + const transport = new TriggerChatTransport({ + task: "chat-task", + stream: "chat-stream", + accessToken: "pk_trigger", + baseURL: server.url, + runStore, + onError: function onError(error) { + errors.push(error); + }, + }); + + (transport as any).fetchRunStream = async function fetchRunStream() { + throw new Error("stream subscribe root cause"); + }; + + await expect( + transport.sendMessages({ + trigger: "submit-message", + chatId: "chat-stream-subscribe-cleanup-both-failure", + messageId: undefined, + messages: [], + abortSignal: undefined, + }) + ).rejects.toThrowError("stream subscribe root cause"); + + expect(errors).toHaveLength(1); + expect(errors[0]).toMatchObject({ + phase: "streamSubscribe", + chatId: "chat-stream-subscribe-cleanup-both-failure", + runId: "run_stream_subscribe_cleanup_both_failure", + }); + expect(runStore.setCalls).toContain("chat-stream-subscribe-cleanup-both-failure"); + expect(runStore.deleteCalls).toContain("chat-stream-subscribe-cleanup-both-failure"); + }); + it( "preserves stream subscribe root failures when cleanup and onError callbacks both fail", async function () { @@ -2988,6 +3041,31 @@ class FailingCleanupDeleteRunStore extends InMemoryTriggerChatRunStore { } } +class FailingCleanupSetAndDeleteRunStore extends InMemoryTriggerChatRunStore { + private setCallCount = 0; + public readonly setCalls: string[] = []; + public readonly deleteCalls: string[] = []; + + constructor(private readonly failOnSetCall: number = 2) { + super(); + } + + public set(state: TriggerChatRunState): void { + this.setCallCount += 1; + this.setCalls.push(state.chatId); + if (this.setCallCount === this.failOnSetCall) { + throw new Error("cleanup set failed"); + } + + super.set(state); + } + + public delete(chatId: string): void { + this.deleteCalls.push(chatId); + throw new Error("cleanup delete failed"); + } +} + class AsyncTrackedRunStore implements TriggerChatRunStore { private readonly runs = new Map(); public readonly getCalls: string[] = [];