AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend/copilot): show a lost send's finished answer instead of "Failed to fetch" (#15380)
Reinier van der Leerf358a5fc94a6
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/fakeBackend.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/fakeBackend.tsindex faa7933f76..ccd12b167a 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/fakeBackend.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/fakeBackend.ts@@ -34,10 +34,12 @@ export interface FakeConnection {silence(): void;}-type Responder = (request: {- method: string;- url: URL;-}) => "stream" | "network-error" | { status: number; body: unknown };+type Responder = (request: { method: string; url: URL }) =>+ | "stream"+ | "network-error"+ // An open response that never yields a byte: a page frozen before it read any.+ | "silent"+ | { status: number; body: unknown };/*** The backend as the runtime sees it, for one recorded turn: the turn's@@ -77,6 +79,11 @@ export function fakeBackend(turn: RecordedTurn) {requests.push({ method, url });const answer = responder({ method, url });if (answer === "network-error") throw new TypeError("Failed to fetch");+ if (answer === "silent")+ return new Response(new ReadableStream<Uint8Array>(), {+ status: 200,+ headers: { "content-type": "text/event-stream" },+ });if (answer !== "stream") {return new Response(JSON.stringify(answer.body), {status: answer.status,diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/turnRuntime.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/turnRuntime.test.tsindex 73dfd78e4d..74e40f9913 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/turnRuntime.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/turnRuntime.test.ts@@ -300,6 +300,68 @@ describe("recovery", () => {expect(textsOf(render())).toEqual([PROMPT, FIRST_TEXT]);});+ it("a send cut while hidden whose turn finished meanwhile shows the answer, not an error", async () => {+ backend.respond(({ method }) =>+ method === "POST" ? "network-error" : "stream",+ );+ setVisibility("hidden");+ void runtime.send({ text: PROMPT }, undefined);+ await advance(60_000);++ // The backend ran the turn to its end while the tab slept.+ backend.beginRunning();+ backend.publish();+ setVisibility("visible");+ await advance(10_000);+ expect(snapshot().error).toBeNull();+ expect(textsOf(render()).join("")).toBe(+ `${PROMPT}${FIRST_TEXT}${SECOND_TEXT}`,+ );+ });++ it("a send silent while the tab slept is given up on return, not at the next tick", async () => {+ backend.respond(({ method }) => (method === "POST" ? "silent" : "stream"));+ setVisibility("hidden");+ void runtime.send({ text: PROMPT }, undefined);+ await advance(60_000);+ backend.beginRunning();+ backend.publish();++ const views = vi.spyOn(backend, "view");+ setVisibility("visible");+ await advance(0);+ expect(views).toHaveBeenCalledTimes(1);+ expect(textsOf(render()).join("")).toBe(+ `${PROMPT}${FIRST_TEXT}${SECOND_TEXT}`,+ );+ });++ it("a send that never reached the server still reports the failure", async () => {+ backend.respond(({ method }) =>+ method === "POST" ? "network-error" : "stream",+ );+ setVisibility("hidden");+ void runtime.send({ text: PROMPT }, undefined);+ await advance(60_000);+ setVisibility("visible");+ await advance(10_000);+ expect(snapshot().error?.message).toBe("Failed to fetch");+ });++ it("a send lost while a later send lands still reports its own failure", async () => {+ backend.respond(({ method }) =>+ method === "POST" ? "network-error" : "stream",+ );+ void runtime.send({ text: "Did this one arrive?" }, undefined);+ await advance(0);++ // The user sends again while the lost one is being looked for.+ backend.respond(() => "stream");+ await sendAndPublish(toolTurn.sse.length);+ await advance(10_000);+ expect(snapshot().error?.message).toBe("Failed to fetch");+ });+it("a lost resend of the same prompt takes no earlier prompt's row", async () => {await sendAndPublish(toolTurn.sse.length);await advance(2_000);diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnRuntime.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnRuntime.tsindex fc645158f2..d57d8f5c61 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnRuntime.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnRuntime.ts@@ -137,10 +137,15 @@ interface Connection {turnId: string | null;closedByClient: boolean;syntheticError: string | null;+ /** A send's own prompt segment, by key. */+ prompt: string | null;}/** What the classification needs to know about a connection that is gone. */-type Lost = Pick<Connection, "turnId" | "openedAt" | "syntheticError">;+type Lost = Pick<+ Connection,+ "turnId" | "openedAt" | "syntheticError" | "prompt"+>;type ConnectionEnd =| { kind: "closed" }@@ -268,6 +273,7 @@ export class TurnRuntime {method: "POST",body: JSON.stringify(body),extraHeaders: { "Content-Type": "application/json" },+ prompt: message.id,});}@@ -417,7 +423,12 @@ export class TurnRuntime {kind: Connection["kind"],turnId: string | null,url: string,- init: { method?: string; body?: string; extraHeaders?: object } = {},+ init: {+ method?: string;+ body?: string;+ extraHeaders?: object;+ prompt?: string;+ } = {},asRotation = false,) {const now = Date.now();@@ -431,6 +442,7 @@ export class TurnRuntime {turnId,closedByClient: false,syntheticError: null,+ prompt: init.prompt ?? null,};if (asRotation) this.rotating = conn;else {@@ -646,6 +658,8 @@ export class TurnRuntime {if (this.disposed || this.stopped) return;await sleep(FINISH_PROBE_MS);}+ // The view holds the prompt, so the send arrived; its rows say the rest.+ if (this.lostSendLanded(lost.prompt)) return;if (end.kind === "failed" || lost.syntheticError) {this.error =end.kind === "failed" && end.error instanceof Error@@ -655,6 +669,11 @@ export class TurnRuntime {}}+ private lostSendLanded(promptKey: string | null) {+ const prompt = promptKey ? this.segmentByKey(promptKey) : null;+ return prompt?.kind === "user" && prompt.sequence !== null;+ }+// ── The turn's lifecycle ───────────────────────────────────────────────private attach(@@ -788,6 +807,7 @@ export class TurnRuntime {turnId: seg.log.turnId,openedAt: Date.now(),syntheticError: null,+ prompt: null,});return;}@@ -822,6 +842,7 @@ export class TurnRuntime {turnId: seg.log.turnId,openedAt: Date.now(),syntheticError: null,+ prompt: null,});return;}@@ -1035,6 +1056,8 @@ export class TurnRuntime {this.unresolvedPost = null;if (unresolved && !this.runningTurn())void this.recoverUnknownTurn(unresolved.lost, unresolved.end);+ // A send still waiting for its turn is otherwise given up only at the next tick.+ else if (this.isPostPending()) this.dropIfSilent();else this.ensureConnected();}@@ -1051,17 +1074,24 @@ export class TurnRuntime {this.tickTimer = null;return;}- if (!slot || document.visibilityState === "hidden") return;- const now = Date.now();- if (now - slot.lastFrameAt >= LIVENESS_MS) {- slot.closedByClient = true;- slot.controller.abort();- this.slot = null;- if (slot.turnId === null) this.findLostTurn(slot, { kind: "closed" });- else this.retry(slot);- return;- }- if (!this.rotating && now - slot.openedAt >= ROTATE_AFTER_MS) this.rotate();+ if (!slot || this.dropIfSilent()) return;+ if (document.visibilityState === "hidden") return;+ if (!this.rotating && Date.now() - slot.openedAt >= ROTATE_AFTER_MS)+ this.rotate();+ }++ // The slot is cleared before the recovery starts, so the tick and a wake+ // never both recover one connection.+ private dropIfSilent() {+ const slot = this.slot;+ if (!slot || document.visibilityState === "hidden") return false;+ if (Date.now() - slot.lastFrameAt < LIVENESS_MS) return false;+ slot.closedByClient = true;+ slot.controller.abort();+ this.slot = null;+ if (slot.turnId === null) this.findLostTurn(slot, { kind: "closed" });+ else this.retry(slot);+ return true;}// Rebuilding from the DB view: no other trigger reconnects meanwhile, and