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): hold stream-end force-hydrate while continuation turn is pending (#13631)
Ubbe4a5c58a02d68
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotStream.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotStream.test.tsxnew file mode 100644index 0000000000..72559b975f--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotStream.test.tsx@@ -0,0 +1,182 @@+import { getGetV2GetSessionMockHandler200 } from "@/app/api/__generated__/endpoints/chat/chat.msw";+import type { SessionDetailResponse } from "@/app/api/__generated__/models/sessionDetailResponse";+import { server } from "@/mocks/mock-server";+import {+ assistantTextChunks,+ copilotStreamHandler,+ streamSseResponse,+} from "@/tests/integrations/copilot-sse";+import { screen, waitFor } from "@testing-library/react";+import { http } from "msw";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { resetCopilotChatRegistry } from "../copilotChatRegistry";+import {+ renderHost,+ TEST_BACKEND_BASE_URL,+ TEST_SESSION_ID,+ typeAndSend,+} from "./sse-helpers";++vi.mock("@/services/environment", async (importActual) => {+ const actual = await importActual<typeof import("@/services/environment")>();+ return {+ ...actual,+ environment: {+ ...actual.environment,+ getAGPTServerBaseUrl: () => TEST_BACKEND_BASE_URL,+ },+ };+});++vi.mock("../helpers", async (importActual) => {+ const actual = await importActual<typeof import("../helpers")>();+ return {+ ...actual,+ getCopilotAuthHeaders: async () => ({ "x-test-auth": "yes" }),+ };+});++vi.mock("@/lib/supabase/hooks/useSupabase", () => ({+ useSupabase: () => ({ isUserLoading: false, isLoggedIn: true }),+}));++vi.mock("@/services/feature-flags/use-get-flag", () => ({+ Flag: {+ ARTIFACTS: "ARTIFACTS",+ CHAT_MODE_OPTION: "CHAT_MODE_OPTION",+ ENABLE_PLATFORM_PAYMENT: "ENABLE_PLATFORM_PAYMENT",+ },+ useGetFlag: () => false,+}));++// Records the `isFinishProbing` prop on every render so the producer-side+// lifecycle (set before the probe loop, reset in `finally`) is observable+// without reaching into `useCopilotStream` internals.+const { isFinishProbingHistory } = vi.hoisted(() => ({+ isFinishProbingHistory: [] as boolean[],+}));++vi.mock("../useHydrateOnStreamEnd", async (importActual) => {+ const actual =+ await importActual<typeof import("../useHydrateOnStreamEnd")>();+ return {+ ...actual,+ useHydrateOnStreamEnd: (+ args: Parameters<typeof actual.useHydrateOnStreamEnd>[0],+ ) => {+ isFinishProbingHistory.push(args.isFinishProbing);+ return actual.useHydrateOnStreamEnd(args);+ },+ };+});++function streamUrl() {+ return `${TEST_BACKEND_BASE_URL}/api/chat/sessions/${TEST_SESSION_ID}/stream`;+}++function sessionJson(+ activeStream: { turn_id: string; last_message_id: string } | null,+): SessionDetailResponse {+ return {+ id: TEST_SESSION_ID,+ created_at: "2026-05-13T00:00:00Z",+ updated_at: "2026-05-13T00:00:00Z",+ user_id: "test-user",+ chat_status: "idle",+ messages: [],+ has_more_messages: false,+ oldest_sequence: null,+ active_stream: activeStream,+ metadata: { dry_run: false, builder_graph_id: null },+ };+}++beforeEach(() => {+ resetCopilotChatRegistry();+ isFinishProbingHistory.length = 0;+});++afterEach(() => {+ resetCopilotChatRegistry();+});++describe("useCopilotStream — isFinishProbing lifecycle", () => {+ it("sets the flag during the post-finish probe and resets it when no continuation is pending", async () => {+ server.use(+ copilotStreamHandler({+ baseUrl: TEST_BACKEND_BASE_URL,+ sessionId: TEST_SESSION_ID,+ chunks: assistantTextChunks("Hi."),+ }),+ );++ renderHost();+ await typeAndSend("hi");+ await screen.findByText("Hi.", undefined, { timeout: 5000 });++ // The probe loop runs for ~500ms after the SSE finish.+ await waitFor(() => expect(isFinishProbingHistory).toContain(true), {+ timeout: 5000,+ });+ // No active backend stream → the loop exits normally and the finally+ // block resets the flag.+ await waitFor(() => expect(isFinishProbingHistory.at(-1)).toBe(false), {+ timeout: 5000,+ });+ });++ it(+ "resets the flag via finally when the probe finds a continuation stream",+ { timeout: 15000 },+ async () => {+ // Flipped by the stream POST: the session GET only reports a live+ // continuation stream once the first turn has actually run.+ let continuationPending = false;+ let resumeRequested = false;++ renderHost();+ // Registered AFTER renderHost so these take precedence over the+ // default pinned handlers (MSW matches most-recently-added first).+ server.use(+ getGetV2GetSessionMockHandler200(() =>+ sessionJson(+ continuationPending+ ? { turn_id: "turn-2", last_message_id: "msg-2" }+ : null,+ ),+ ),+ http.post(streamUrl(), ({ request }) => {+ continuationPending = true;+ return streamSseResponse(assistantTextChunks("Hi."), {+ abortSignal: request.signal,+ });+ }),+ http.get(streamUrl(), ({ request }) => {+ resumeRequested = true;+ continuationPending = false;+ return streamSseResponse(+ assistantTextChunks(" continued", { messageId: "test-message-2" }),+ { abortSignal: request.signal },+ );+ }),+ );++ await typeAndSend("hi");+ await screen.findByText("Hi.", undefined, { timeout: 5000 });++ // Probe window: the flag is up while the active-stream probe runs.+ await waitFor(() => expect(isFinishProbingHistory).toContain(true), {+ timeout: 5000,+ });+ // The probe sees the continuation stream → early-return through the+ // reconnect path → the finally block must still reset the flag.+ await waitFor(() => expect(isFinishProbingHistory.at(-1)).toBe(false), {+ timeout: 5000,+ });+ // ...and the reconnect actually picked the continuation turn up.+ await waitFor(() => expect(resumeRequested).toBe(true), {+ timeout: 8000,+ });+ },+ );+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useHydrateOnStreamEnd.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useHydrateOnStreamEnd.test.tsindex 988911ee07..68e69536f4 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useHydrateOnStreamEnd.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useHydrateOnStreamEnd.test.ts@@ -61,6 +61,7 @@ function runForceHydrate({sessionId: SESSION_ID,isReconnectScheduled: false,hasActiveStream: false,+ isFinishProbing: false,setMessages,};@@ -166,3 +167,178 @@ describe("useHydrateOnStreamEnd — sliding-window history retention (SECRT-2424]);});});++describe("useHydrateOnStreamEnd — continuation-turn flash guard", () => {+ type Props = Parameters<typeof useHydrateOnStreamEnd>[0];++ afterEach(() => {+ _resetInterruptedToastLedgerForTests();+ cleanup();+ });++ function makeCapturingSetMessages(prev: Messages) {+ const captured: { current: Messages | null } = { current: null };+ const setMessages = vi.fn(+ (updater: Messages | ((p: Messages) => Messages)) => {+ captured.current =+ typeof updater === "function" ? updater(prev) : updater;+ },+ );+ return { setMessages, captured };+ }++ function setup({ prev = [] as Messages } = {}) {+ const { setMessages, captured } = makeCapturingSetMessages(prev);+ const baseProps = {+ sessionId: SESSION_ID,+ isReconnectScheduled: false,+ hasActiveStream: false,+ isFinishProbing: false,+ setMessages,+ };+ const hook = renderHook<void, Props>(+ (props) => useHydrateOnStreamEnd(props),+ {+ initialProps: {+ ...baseProps,+ status: "streaming",+ hydratedMessages: range(51, 100),+ },+ },+ );+ // Stream ends → arms the force-hydrate against the stale pre-turn window.+ hook.rerender({+ ...baseProps,+ status: "ready",+ hydratedMessages: range(51, 100),+ } satisfies Props);+ return { ...hook, baseProps, setMessages, captured };+ }++ it("defers the force-hydrate while the post-finish probe is in flight", () => {+ const { rerender, baseProps, setMessages, captured } = setup();++ // Fresh post-turn window lands while `handleFinish` is still probing for+ // a continuation turn — the replace must NOT apply (it would swap every+ // message id, remount the list and flash the panel).+ rerender({+ ...baseProps,+ status: "ready",+ hydratedMessages: range(53, 102),+ isFinishProbing: true,+ } satisfies Props);+ expect(setMessages).not.toHaveBeenCalled();++ // Probe settles with no continuation → the replace lands.+ rerender({+ ...baseProps,+ status: "ready",+ hydratedMessages: range(53, 102),+ isFinishProbing: false,+ } satisfies Props);+ expect(setMessages).toHaveBeenCalledTimes(1);+ expect(captured.current!.map(seqOf)).toEqual(range(53, 102).map(seqOf));+ });++ it("holds the force-hydrate while the backend still has an active stream", () => {+ const { rerender, baseProps, setMessages, captured } = setup();++ // Refetched session data shows a continuation turn already live — the+ // resume flow takes over, so the replace must wait.+ rerender({+ ...baseProps,+ status: "ready",+ hydratedMessages: range(53, 102),+ hasActiveStream: true,+ } satisfies Props);+ expect(setMessages).not.toHaveBeenCalled();++ // Backend goes idle → the replace lands.+ rerender({+ ...baseProps,+ status: "ready",+ hydratedMessages: range(53, 102),+ hasActiveStream: false,+ } satisfies Props);+ expect(setMessages).toHaveBeenCalledTimes(1);+ expect(captured.current!.map(seqOf)).toEqual(range(53, 102).map(seqOf));+ });++ it("stays held through the probe → reconnect handoff", () => {+ const { rerender, baseProps, setMessages } = setup();++ // The real continuation flow: the probe is in flight...+ rerender({+ ...baseProps,+ status: "ready",+ hydratedMessages: range(53, 102),+ isFinishProbing: true,+ } satisfies Props);+ // ...then the probe finds an active stream and schedules the reconnect —+ // `isFinishProbing` flips false while `hasActiveStream` is now true. The+ // replace must stay held by the second gate.+ rerender({+ ...baseProps,+ status: "ready",+ hydratedMessages: range(53, 102),+ isFinishProbing: false,+ hasActiveStream: true,+ } satisfies Props);+ expect(setMessages).not.toHaveBeenCalled();++ // Continuation turn ends, backend idle → the replace finally lands.+ rerender({+ ...baseProps,+ status: "ready",+ hydratedMessages: range(53, 102),+ hasActiveStream: false,+ } satisfies Props);+ expect(setMessages).toHaveBeenCalledTimes(1);+ });++ it("retains older in-memory history when the deferred replace lands", () => {+ // Memory holds seq 1-60; the fresh window covers seq 53-102. The delayed+ // replace must still prepend the older in-memory run (seq 1-52) instead+ // of tearing a hole into scrolled-back history.+ const { rerender, baseProps, setMessages, captured } = setup({+ prev: range(1, 60),+ });++ rerender({+ ...baseProps,+ status: "ready",+ hydratedMessages: range(53, 102),+ isFinishProbing: true,+ } satisfies Props);+ expect(setMessages).not.toHaveBeenCalled();++ rerender({+ ...baseProps,+ status: "ready",+ hydratedMessages: range(53, 102),+ isFinishProbing: false,+ } satisfies Props);+ expect(setMessages).toHaveBeenCalledTimes(1);+ expect(captured.current!.map(seqOf)).toEqual(range(1, 102).map(seqOf));+ });++ it("still applies length-gated top-ups while a stream is active (session restore)", () => {+ // Reopening a session with an active backend stream relies on the top-up+ // branch to render history before the resume replay starts — only the+ // force-hydrate path is gated on `hasActiveStream`.+ const { setMessages, captured } = makeCapturingSetMessages([]);+ renderHook(() =>+ useHydrateOnStreamEnd({+ sessionId: SESSION_ID,+ status: "ready",+ hydratedMessages: range(1, 10),+ isReconnectScheduled: false,+ hasActiveStream: true,+ isFinishProbing: false,+ setMessages,+ }),+ );+ expect(setMessages).toHaveBeenCalledTimes(1);+ expect(captured.current!.map(seqOf)).toEqual(range(1, 10).map(seqOf));+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.tsindex 022e98cd1d..b0be7988a5 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts@@ -114,6 +114,11 @@ export function useCopilotStream({// flipped ``status`` back to ``ready`` yet (which can lag by many seconds// when aborting a GET-based resume fetch).const [isUserStopping, setIsUserStopping] = useState(false);+ // True while `handleFinish`'s post-turn active-stream probe is still+ // deciding whether a continuation turn is starting. Gates the force-hydrate+ // in `useHydrateOnStreamEnd` so it can't swap message ids (and flash the+ // whole list) in the window before the reconnect is scheduled.+ const [isFinishProbing, setIsFinishProbing] = useState(false);// Flipped to `false` during mount cleanup so async callbacks that were// already in flight (e.g. the post-stream settle in `onFinish`) bail out// instead of arming new timers / HTTP requests against a torn-down mount.@@ -168,15 +173,20 @@ export function useCopilotStream({? FINISH_REFETCH_ATTEMPTS_PENDING_SWITCH: FINISH_REFETCH_ATTEMPTS_DEFAULT;pendingEngineSwitchRef.current = false;- for (let attempt = 0; attempt < attempts; attempt++) {- await new Promise((r) => setTimeout(r, FINISH_REFETCH_SETTLE_MS));- if (!isMountedRef.current) return;- const result = await refetchSession();- if (!isMountedRef.current) return;- if (hasActiveBackendStream(result)) {- handleReconnectRef.current();- return;+ setIsFinishProbing(true);+ try {+ for (let attempt = 0; attempt < attempts; attempt++) {+ await new Promise((r) => setTimeout(r, FINISH_REFETCH_SETTLE_MS));+ if (!isMountedRef.current) return;+ const result = await refetchSession();+ if (!isMountedRef.current) return;+ if (hasActiveBackendStream(result)) {+ handleReconnectRef.current();+ return;+ }}+ } finally {+ if (isMountedRef.current) setIsFinishProbing(false);}}@@ -446,6 +456,7 @@ export function useCopilotStream({hydratedMessages,isReconnectScheduled,hasActiveStream,+ isFinishProbing,setMessages,});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.tsindex 76fbed9608..d340c23d85 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.ts@@ -120,6 +120,15 @@ interface Args {* resend.*/hasActiveStream: boolean;+ /**+ * True while the post-finish probe (`handleFinish` in `useCopilotStream`)+ * is still deciding whether the backend is starting a continuation turn.+ * Applying the force-hydrate in that window swaps every message id (AI-SDK+ * uuid → DB `-seq-N`), remounting the whole list and replaying the entrance+ * animation — the visible mid-conversation flash — only for the resume to+ * immediately take over. Hold the replace until the probe settles.+ */+ isFinishProbing: boolean;setMessages: (updater: UIMessage[] | ((prev: UIMessage[]) => UIMessage[]),) => void;@@ -160,6 +169,7 @@ export function useHydrateOnStreamEnd({hydratedMessages,isReconnectScheduled,hasActiveStream,+ isFinishProbing,setMessages,}: Args) {const prevStatusRef = useRef(status);@@ -217,11 +227,27 @@ export function useHydrateOnStreamEnd({});}+ // Hold ALL message replacement while the post-finish probe decides+ // whether a continuation turn is starting — replacing now would swap+ // every message id and remount the list mid-conversation. This gate is+ // deliberately wider than the `hasActiveStream` one below (which only+ // holds the force-hydrate): it also holds the length-gated top-up. The+ // interrupted toast above is NOT held — an interrupted turn must surface+ // immediately, not after the probe loop finishes.+ if (isFinishProbing) return;+if (needsForceHydrateRef.current) {if (isStaleForceHydrateSnapshot) {// Still the pre-turn snapshot — wait for the refetch.return;}+ // The fresh session data shows the backend still has a live stream+ // (continuation turn dispatching, or a resume about to start). The+ // resume effect in `useCopilotStream` reconnects while the backend+ // reports an active stream, and a merely-stale flag clears on the next+ // session refetch — either way this effect re-fires and the replace+ // lands once the backend goes idle, so no timeout fallback is needed.+ if (hasActiveStream) return;setMessages((prev) =>preservePromotedUserBubbles(prev, retainOlderHistory(prev, finalized)),);@@ -238,5 +264,6 @@ export function useHydrateOnStreamEnd({status,isReconnectScheduled,hasActiveStream,+ isFinishProbing,]);}