Skip to content

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): keep AutoPilot middle messages when tail window slides (#13379)

Abhimanyu Yadav60940b0dd229
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.ts
new file mode 100644
index 0000000000..988911ee07
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useHydrateOnStreamEnd.test.ts
@@ -0,0 +1,168 @@
+import { cleanup, renderHook } from "@testing-library/react";
+import type { UIDataTypes, UIMessage, UITools } from "ai";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import {
+ _resetInterruptedToastLedgerForTests,
+ useHydrateOnStreamEnd,
+} from "../useHydrateOnStreamEnd";
+
+vi.mock("@/components/molecules/Toast/use-toast", () => ({
+ toast: vi.fn(),
+}));
+
+type Messages = UIMessage<unknown, UIDataTypes, UITools>[];
+
+const SESSION_ID = "session-1";
+
+function seqMessage(seq: number): UIMessage<unknown, UIDataTypes, UITools> {
+ return {
+ id: `${SESSION_ID}-seq-${seq}`,
+ role: seq % 2 === 0 ? "assistant" : "user",
+ parts: [{ type: "text", text: `message ${seq}`, state: "done" }],
+ };
+}
+
+function range(start: number, end: number): Messages {
+ const out: Messages = [];
+ for (let seq = start; seq <= end; seq++) out.push(seqMessage(seq));
+ return out;
+}
+
+function seqOf(message: UIMessage): number {
+ const match = /-seq-(\d+)$/.exec(message.id);
+ return match ? Number(match[1]) : NaN;
+}
+
+/**
+ * Drive the hook through a stream-end force-hydrate and capture the result
+ * `setMessages` produces. `prev` is the in-memory AI-SDK state at the moment
+ * the force-hydrate applies; `staleWindow`/`freshWindow` are the two distinct
+ * `hydratedMessages` references React Query swaps in (stale = pre-turn, fresh
+ * = the post-turn refetch).
+ */
+function runForceHydrate({
+ prev,
+ staleWindow,
+ freshWindow,
+}: {
+ prev: Messages;
+ staleWindow: Messages;
+ freshWindow: Messages;
+}): Messages | null {
+ let captured: Messages | null = null;
+ const setMessages = vi.fn(
+ (updater: Messages | ((p: Messages) => Messages)) => {
+ captured = typeof updater === "function" ? updater(prev) : updater;
+ },
+ );
+
+ type Props = Parameters<typeof useHydrateOnStreamEnd>[0];
+ const baseProps = {
+ sessionId: SESSION_ID,
+ isReconnectScheduled: false,
+ hasActiveStream: false,
+ setMessages,
+ };
+
+ const { rerender } = renderHook<void, Props>(
+ (props) => useHydrateOnStreamEnd(props),
+ {
+ initialProps: {
+ ...baseProps,
+ status: "streaming",
+ hydratedMessages: staleWindow,
+ },
+ },
+ );
+
+ // Stream ends → arms force-hydrate, snapshots the stale (pre-turn) window.
+ rerender({
+ ...baseProps,
+ status: "ready",
+ hydratedMessages: staleWindow,
+ } satisfies Props);
+
+ // React Query swaps in the fresh post-turn window → force-hydrate applies.
+ rerender({
+ ...baseProps,
+ status: "ready",
+ hydratedMessages: freshWindow,
+ } satisfies Props);
+
+ return captured;
+}
+
+describe("useHydrateOnStreamEnd — sliding-window history retention (SECRT-2424)", () => {
+ afterEach(() => {
+ _resetInterruptedToastLedgerForTests();
+ cleanup();
+ });
+
+ it("keeps middle messages when the tail window slides past loaded history", () => {
+ // Before the turn, memory holds seq 51-102 (the prior window plus the
+ // turn that just streamed). The refetched window only covers seq 53-102,
+ // so a blind replace would drop seq 51 and 52 into a hole between the
+ // recent window and any older `pagedMessages`.
+ const prev = range(51, 102);
+ const freshWindow = range(53, 102);
+
+ const result = runForceHydrate({
+ prev,
+ staleWindow: range(51, 100),
+ freshWindow,
+ });
+
+ expect(result).not.toBeNull();
+ const seqs = result!.map(seqOf);
+ // No hole: 51 and 52 survive, contiguous through 102.
+ expect(seqs).toEqual(range(51, 102).map(seqOf));
+ expect(seqs).toContain(51);
+ expect(seqs).toContain(52);
+ });
+
+ it("retains all older messages when the window omits the start of a grown session", () => {
+ // Session grew from <50 to 60 messages with no scroll-back: the window
+ // is seq 11-60, but memory still holds seq 1-60. seq 1-10 must survive.
+ const result = runForceHydrate({
+ prev: range(1, 60),
+ staleWindow: range(1, 50),
+ freshWindow: range(11, 60),
+ });
+
+ expect(result).not.toBeNull();
+ expect(result!.map(seqOf)).toEqual(range(1, 60).map(seqOf));
+ });
+
+ it("replaces with the window unchanged when nothing older is retained", () => {
+ // Memory and the fresh window cover the same range — no older tail to
+ // keep, so the canonical window wins outright (no duplicates).
+ const freshWindow = range(53, 102);
+ const result = runForceHydrate({
+ prev: range(53, 102),
+ staleWindow: range(53, 100),
+ freshWindow,
+ });
+
+ expect(result).not.toBeNull();
+ expect(result!.map(seqOf)).toEqual(freshWindow.map(seqOf));
+ });
+
+ it("retains all of prev when the window shares no sequence with memory", () => {
+ // Pathological no-overlap: every in-memory message is older than the whole
+ // refetched window (the leading run is the entire array). All of prev must
+ // survive, prepended to the disjoint window.
+ const prev = range(1, 40);
+ const freshWindow = range(60, 102);
+ const result = runForceHydrate({
+ prev,
+ staleWindow: range(60, 100),
+ freshWindow,
+ });
+
+ expect(result).not.toBeNull();
+ expect(result!.map(seqOf)).toEqual([
+ ...range(1, 40).map(seqOf),
+ ...freshWindow.map(seqOf),
+ ]);
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts
index dc7031afe5..43a15b4efc 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts
@@ -142,7 +142,7 @@ function toToolInput(rawArguments: unknown): unknown {
// caller refuses the merge in those cases (the safer default).
const HYDRATED_ID_SEQ_RE = /-seq-(\d+)$/;
-function extractDbSequence(uiMessage: UIMessage): number | null {
+export function extractDbSequence(uiMessage: UIMessage): number | null {
if (typeof uiMessage.id !== "string") return null;
const match = HYDRATED_ID_SEQ_RE.exec(uiMessage.id);
return match ? Number(match[1]) : null;
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.ts
index f034db68df..76fbed9608 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useHydrateOnStreamEnd.ts
@@ -7,9 +7,49 @@ import {
hasInProgressAssistantParts,
resolveInterruptedMessage,
} from "./helpers";
+import { extractDbSequence } from "./helpers/convertChatSessionToUiMessages";
const PROMOTED_BUBBLE_ID_PREFIX = "promoted-";
+/**
+ * The hydrated payload is only the backend's most-recent tail window (the
+ * `GET /sessions` endpoint returns the latest ~50 messages). That window
+ * slides forward as the conversation grows, so a blind force-replace would
+ * drop every in-memory message older than the new window's oldest sequence —
+ * tearing a hole between the live state and any older history the user
+ * already scrolled back to (`pagedMessages`), which scroll-back can't refill
+ * (it only fetches messages older than what's loaded). See SECRT-2424.
+ *
+ * Keep the `prev` messages whose DB sequence predates the window so the
+ * force-hydrated result stays contiguous with the older history. Both arrays
+ * are in ascending DB-sequence order, so the window's oldest sequence is the
+ * first `-seq-N` row in `hydrated`, and the older `prev` messages are the
+ * leading run before that boundary — a single slice. Streaming / idx-fallback
+ * rows (no `-seq-N` id) carry no sequence and mark the end of that run; they
+ * are excluded because they already live inside the refetched window.
+ */
+function retainOlderHistory(
+ prev: UIMessage[],
+ hydrated: UIMessage[],
+): UIMessage[] {
+ let oldestHydratedSeq: number | null = null;
+ for (const message of hydrated) {
+ oldestHydratedSeq = extractDbSequence(message);
+ if (oldestHydratedSeq !== null) break;
+ }
+ if (oldestHydratedSeq === null) return hydrated;
+ const windowStart = oldestHydratedSeq;
+
+ const windowStartIndex = prev.findIndex((message) => {
+ const seq = extractDbSequence(message);
+ return seq === null || seq >= windowStart;
+ });
+ const olderCount = windowStartIndex === -1 ? prev.length : windowStartIndex;
+ if (olderCount === 0) return hydrated;
+
+ return [...prev.slice(0, olderCount), ...hydrated];
+}
+
function getMessageText(message: UIMessage): string {
return message.parts
.map((part) => (part.type === "text" && "text" in part ? part.text : ""))
@@ -182,7 +222,9 @@ export function useHydrateOnStreamEnd({
// Still the pre-turn snapshot — wait for the refetch.
return;
}
- setMessages((prev) => preservePromotedUserBubbles(prev, finalized));
+ setMessages((prev) =>
+ preservePromotedUserBubbles(prev, retainOlderHistory(prev, finalized)),
+ );
needsForceHydrateRef.current = false;
staleRefAtStreamEnd.current = null;
return;