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.

test(frontend/copilot): park compaction streams instead of timing them (#15429)

Nicholas Tindlebfa3fa354627
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/compaction-progress.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/compaction-progress.test.tsx
index 0c8fd6877f..94f166161d 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/compaction-progress.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/compaction-progress.test.tsx
@@ -1,6 +1,9 @@
import type { SessionDetailResponseMessagesItem } from "@/app/api/__generated__/models/sessionDetailResponseMessagesItem";
import { server } from "@/mocks/mock-server";
-import { copilotStreamHandler } from "@/tests/integrations/copilot-sse";
+import {
+ copilotStreamHandler,
+ createStreamHold,
+} from "@/tests/integrations/copilot-sse";
import { screen, waitFor } from "@testing-library/react";
import type { UIMessageChunk } from "ai";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -207,8 +210,8 @@ const TWO_CYCLE_TURN: UIMessageChunk[] = [
{ type: "start-step" },
...openCompactionRow("compaction-2"),
summarizingPhase(64000),
- // The hold sits before this chunk so the second cycle stays live long
- // enough to assert against (see SECOND_CYCLE_HOLD_INDEX).
+ // The stream is held before this chunk so the second cycle stays live
+ // while the test looks (see SECOND_CYCLE_HOLD_INDEX).
closeCompactionRow(
{ tokensBefore: 64000, tokensAfter: 20000 },
"compaction-2",
@@ -237,8 +240,8 @@ const DEAD_STREAM_TURN: UIMessageChunk[] = [
closeCompactionRow({ tokensBefore: 128000, tokensAfter: 31000 }),
{ type: "finish-step" },
REBUILDING_PHASE,
- // The hold sits before this chunk so the live "Reloading context…" bar
- // is observable before the stream closes (see DEAD_STREAM_HOLD_INDEX).
+ // The stream is held before this chunk so the live "Reloading context…"
+ // bar is observable before the stream closes (see DEAD_STREAM_HOLD_INDEX).
{ type: "finish" },
];
@@ -307,17 +310,17 @@ const DROPPED_COMPACTION_TURN: UIMessageChunk[] = [
{ type: "finish" },
];
-// Holds long enough to outlast `waitFor`'s default 1000ms window, so a
-// loaded CI runner can't watch the held state expire mid-poll.
-const HOLD_MS = 1_500;
-// Post-hold assertions wait for the stream to play out past the hold.
-const POST_HOLD_TIMEOUT = { timeout: 5_000 };
+// Every wait below is for the state it names, never for a timer. A state
+// the turn only passes through is parked with a stream hold, released once
+// the test has looked, so a loaded runner can't let it slip by. The timeouts
+// only stop a stream that hangs; they are not part of the timing.
+const HANG_TIMEOUT = { timeout: 15_000 };
// The rebuild phase is emitted AFTER the tool row closes — it covers the
// transcript upload, CLI restart and uncached prefill, which is the silence
-// this feature exists to narrate. Holding on the chunk that follows it keeps
-// that window observable: settling the row on `output-available` would erase
-// the phase entirely.
+// this feature exists to narrate. Holding the stream before the chunk that
+// follows it keeps that window observable: settling the row on
+// `output-available` would erase the phase entirely.
const REBUILDING_HOLD_INDEX =
COMPACTION_TURN.findIndex(
(c) =>
@@ -325,46 +328,54 @@ const REBUILDING_HOLD_INDEX =
(c.data as { phase?: string } | undefined)?.phase === "rebuilding",
) + 1;
-// `COMPACTION_TURN`'s chunk index of `tool-output-available` — the delay
-// applied *before* this chunk is what keeps the turn parked in the
-// "summarizing" phase long enough for `waitFor` to observe it (see below).
+// `COMPACTION_TURN`'s chunk index of `tool-output-available`: holding the
+// stream before this chunk parks the turn in the "summarizing" phase.
const SUMMARIZING_HOLD_INDEX = COMPACTION_TURN.findIndex(
(c) => c.type === "tool-output-available",
);
-describe.each(STREAM_PATHS)("on the %s path", (path) => {
+describe.each(STREAM_PATHS)("on the %s path", { timeout: 30_000 }, (path) => {
beforeEach(() => {
streamPath.runtime = path === "stream runtime";
});
describe("context compaction progress", () => {
- beforeEach(() => {
- resetChatRuntimes();
+ let releaseHeldStream = () => {};
+
+ // A uniform 15ms gap keeps the stream from resolving inside a single
+ // microtask tick. With `holdBeforeChunk`, the stream also stops in front
+ // of that chunk until the returned function releases it.
+ function streamTurn(chunks: UIMessageChunk[], holdBeforeChunk?: number) {
+ const stream =
+ holdBeforeChunk === undefined
+ ? undefined
+ : createStreamHold(holdBeforeChunk);
+ releaseHeldStream = stream?.release ?? (() => {});
server.use(
sessionHandler(),
- // A uniform 15ms gap keeps the stream from resolving inside a single
- // microtask tick, but the "summarizing" phase itself only spans the
- // single gap between the `data-compaction` chunk and
- // `tool-output-available` — at 15ms that's narrower than
- // `waitFor`'s ~50ms poll interval, so the assertion below could miss
- // it entirely depending on scheduling. Hold specifically at that
- // gap for long enough to make the phase reliably observable.
copilotStreamHandler({
baseUrl: TEST_BACKEND_BASE_URL,
sessionId: TEST_SESSION_ID,
- chunks: COMPACTION_TURN,
- perChunkDelaysMs: COMPACTION_TURN.map((_, i) =>
- i === SUMMARIZING_HOLD_INDEX ? HOLD_MS : 15,
- ),
+ chunks,
+ delayMsBetweenChunks: 15,
+ hold: stream?.hold,
}),
);
+ return releaseHeldStream;
+ }
+
+ beforeEach(() => {
+ resetChatRuntimes();
+ streamTurn(COMPACTION_TURN);
});
afterEach(() => {
+ releaseHeldStream();
resetChatRuntimes();
});
it("shows a live bar while compacting, not a completed message", async () => {
+ streamTurn(COMPACTION_TURN, SUMMARIZING_HOLD_INDEX);
renderHost();
await typeAndSend("summarise this");
@@ -377,7 +388,7 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
// true throughout the compaction turn, so this holds from the very
// first chunk onward — not just once the bar is visible.
expect(screen.queryByText("Thinking…")).toBeNull();
- });
+ }, HANG_TIMEOUT);
});
it("lands on the payoff copy with real numbers", async () => {
@@ -388,7 +399,7 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
expect(
screen.getByText(/Condensed 412 messages · 128K → 31K tokens/),
).toBeDefined();
- }, POST_HOLD_TIMEOUT);
+ }, HANG_TIMEOUT);
});
it("never shows the old apologetic copy", async () => {
@@ -397,20 +408,12 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
await waitFor(() => {
expect(screen.getByText("All caught up.")).toBeDefined();
- }, POST_HOLD_TIMEOUT);
+ }, HANG_TIMEOUT);
expect(screen.queryByText(/Earlier messages were summarized/)).toBeNull();
});
it("keeps a settled compaction row out of a collapsed tool group when another tool call follows it", async () => {
- server.use(
- sessionHandler(),
- copilotStreamHandler({
- baseUrl: TEST_BACKEND_BASE_URL,
- sessionId: TEST_SESSION_ID,
- chunks: COMPACTION_THEN_TOOL_TURN,
- delayMsBetweenChunks: 15,
- }),
- );
+ streamTurn(COMPACTION_THEN_TOOL_TURN);
renderHost();
await typeAndSend("search and summarise");
@@ -419,7 +422,7 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
expect(
screen.getByText(/Condensed 412 messages · 128K → 31K tokens/),
).toBeDefined();
- });
+ }, HANG_TIMEOUT);
// If the compaction row had folded into a CollapsedToolGroup with the
// adjacent web_search call, the payoff copy above would be hidden
// behind a "N tool calls completed" toggle instead of standing alone.
@@ -431,22 +434,14 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
// it, and CI refuses a skipped test, so the case exists on one path only.
const sdkOnly = path === "AI SDK" ? it : () => {};
sdkOnly("renders nothing for a compaction that failed", async () => {
- server.use(
- sessionHandler(),
- copilotStreamHandler({
- baseUrl: TEST_BACKEND_BASE_URL,
- sessionId: TEST_SESSION_ID,
- chunks: FAILED_COMPACTION_TURN,
- delayMsBetweenChunks: 15,
- }),
- );
+ streamTurn(FAILED_COMPACTION_TURN);
renderHost();
await typeAndSend("summarise this");
await waitFor(() => {
expect(screen.getByText("Carrying on regardless.")).toBeDefined();
- });
+ }, HANG_TIMEOUT);
// A failed compaction condensed nothing — neither the live copy nor
// the settled "Condensed…" claim may render for it.
expect(screen.queryByText(/Condensing our conversation/)).toBeNull();
@@ -455,15 +450,7 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
});
it("tells the user the context was reset when history was dropped", async () => {
- server.use(
- sessionHandler(),
- copilotStreamHandler({
- baseUrl: TEST_BACKEND_BASE_URL,
- sessionId: TEST_SESSION_ID,
- chunks: DROPPED_COMPACTION_TURN,
- delayMsBetweenChunks: 15,
- }),
- );
+ streamTurn(DROPPED_COMPACTION_TURN);
renderHost();
await typeAndSend("summarise this");
@@ -475,29 +462,21 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
"Started a fresh context — earlier messages were dropped",
),
).toBeDefined();
- });
+ }, HANG_TIMEOUT);
// A drop condensed nothing: no payoff claim, no lingering bar.
expect(screen.queryByText(/Condensed/)).toBeNull();
expect(screen.queryByRole("progressbar")).toBeNull();
});
it("renders nothing for a row retired by an aborted prediction", async () => {
- server.use(
- sessionHandler(),
- copilotStreamHandler({
- baseUrl: TEST_BACKEND_BASE_URL,
- sessionId: TEST_SESSION_ID,
- chunks: ABORTED_COMPACTION_TURN,
- delayMsBetweenChunks: 15,
- }),
- );
+ streamTurn(ABORTED_COMPACTION_TURN);
renderHost();
await typeAndSend("quick question");
await waitFor(() => {
expect(screen.getByText("No condensing needed.")).toBeDefined();
- });
+ }, HANG_TIMEOUT);
// The abort sentinel (output "") must not read as a real compaction —
// neither the live copy nor the settled payoff copy may survive.
expect(screen.queryByText(/Condensing our conversation/)).toBeNull();
@@ -506,17 +485,7 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
});
it("keeps the first cycle settled while a second cycle streams", async () => {
- server.use(
- sessionHandler(),
- copilotStreamHandler({
- baseUrl: TEST_BACKEND_BASE_URL,
- sessionId: TEST_SESSION_ID,
- chunks: TWO_CYCLE_TURN,
- perChunkDelaysMs: TWO_CYCLE_TURN.map((_, i) =>
- i === SECOND_CYCLE_HOLD_INDEX ? HOLD_MS : 15,
- ),
- }),
- );
+ streamTurn(TWO_CYCLE_TURN, SECOND_CYCLE_HOLD_INDEX);
renderHost();
await typeAndSend("summarise twice");
@@ -532,21 +501,11 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
// per-row phase gate, the second cycle's phase re-animates the
// first (closed) row and two bars render.
expect(screen.getAllByRole("progressbar")).toHaveLength(1);
- });
+ }, HANG_TIMEOUT);
});
it("retires the live bar when the stream dies after a compaction phase", async () => {
- server.use(
- sessionHandler(),
- copilotStreamHandler({
- baseUrl: TEST_BACKEND_BASE_URL,
- sessionId: TEST_SESSION_ID,
- chunks: DEAD_STREAM_TURN,
- perChunkDelaysMs: DEAD_STREAM_TURN.map((_, i) =>
- i === DEAD_STREAM_HOLD_INDEX ? HOLD_MS : 15,
- ),
- }),
- );
+ const release = streamTurn(DEAD_STREAM_TURN, DEAD_STREAM_HOLD_INDEX);
renderHost();
await typeAndSend("summarise this");
@@ -555,7 +514,8 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
// bar up while the connection is open.
await waitFor(() => {
expect(screen.getByRole("progressbar")).toBeDefined();
- });
+ }, HANG_TIMEOUT);
+ release();
// Once the stream closes with no trailing text, the streaming gate
// must null the phase — the row settles instead of spinning forever.
await waitFor(() => {
@@ -563,21 +523,11 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
expect(
screen.getByText(/Condensed the conversation · 128K → 31K tokens/),
).toBeDefined();
- }, POST_HOLD_TIMEOUT);
+ }, HANG_TIMEOUT);
});
it("keeps the bar live through the rebuild that follows the closed row", async () => {
- server.use(
- sessionHandler(),
- copilotStreamHandler({
- baseUrl: TEST_BACKEND_BASE_URL,
- sessionId: TEST_SESSION_ID,
- chunks: COMPACTION_TURN,
- perChunkDelaysMs: COMPACTION_TURN.map((_, i) =>
- i === REBUILDING_HOLD_INDEX ? HOLD_MS : 15,
- ),
- }),
- );
+ streamTurn(COMPACTION_TURN, REBUILDING_HOLD_INDEX);
renderHost();
await typeAndSend("summarise this");
@@ -590,21 +540,14 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
// compaction gate the ThinkingIndicator would stack its own spinner
// and timer directly under the CompactionCard's.
expect(screen.queryByText("Thinking…")).toBeNull();
- });
+ }, HANG_TIMEOUT);
expect(screen.getByRole("progressbar")).toBeDefined();
});
it("renders nothing for a row left open when the stream is interrupted", async () => {
- server.use(
- sessionHandler(),
- copilotStreamHandler({
- baseUrl: TEST_BACKEND_BASE_URL,
- sessionId: TEST_SESSION_ID,
- chunks: INTERRUPTED_OPEN_ROW_TURN,
- perChunkDelaysMs: INTERRUPTED_OPEN_ROW_TURN.map((_, i) =>
- i === INTERRUPTED_HOLD_INDEX ? HOLD_MS : 15,
- ),
- }),
+ const release = streamTurn(
+ INTERRUPTED_OPEN_ROW_TURN,
+ INTERRUPTED_HOLD_INDEX,
);
renderHost();
@@ -613,12 +556,13 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
// Live while the connection is open.
await waitFor(() => {
expect(screen.getByRole("progressbar")).toBeDefined();
- });
+ }, HANG_TIMEOUT);
+ release();
// The row never closed, so once the stream is gone it is neither a live
// bar nor a "Condensed…" claim — it disappears.
await waitFor(() => {
expect(screen.queryByRole("progressbar")).toBeNull();
- }, POST_HOLD_TIMEOUT);
+ }, HANG_TIMEOUT);
expect(screen.queryByText(/Condensed/)).toBeNull();
expect(screen.queryByText(/Condensing/)).toBeNull();
});
@@ -672,7 +616,7 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
expect(
screen.getByText("Condensed the conversation · 128K → 31K tokens"),
).toBeDefined();
- });
+ }, HANG_TIMEOUT);
expect(screen.queryByRole("progressbar")).toBeNull();
});
@@ -705,7 +649,7 @@ describe.each(STREAM_PATHS)("on the %s path", (path) => {
expect(
screen.getByText("Condensed the conversation to keep going"),
).toBeDefined();
- });
+ }, HANG_TIMEOUT);
});
});
});
diff --git a/autogpt_platform/frontend/src/tests/integrations/copilot-sse.ts b/autogpt_platform/frontend/src/tests/integrations/copilot-sse.ts
index ae202acdb8..cfc99916a4 100644
--- a/autogpt_platform/frontend/src/tests/integrations/copilot-sse.ts
+++ b/autogpt_platform/frontend/src/tests/integrations/copilot-sse.ts
@@ -17,6 +17,12 @@ interface StreamChunksOptions {
* `chunks`. When set, overrides `delayMsBetweenChunks`.
*/
perChunkDelaysMs?: number[];
+ /**
+ * Parks the stream before a chunk until the test releases it, so a state
+ * the turn only passes through stays on screen for as long as the test
+ * needs to look at it, however slow the machine. See `createStreamHold`.
+ */
+ hold?: StreamHold;
/**
* Aborts in-progress chunk production. Wire this from the MSW handler's
* `request.signal` so a consumer-side fetch abort can stop the stream and
@@ -31,6 +37,21 @@ interface StreamChunksOptions {
after?: string | null;
}
+export interface StreamHold {
+ /** Index of the chunk the stream stops in front of. */
+ beforeChunk: number;
+ released: Promise<void>;
+}
+
+/** A hold for `beforeChunk`, plus the `release` that lets the stream go on. */
+export function createStreamHold(beforeChunk: number) {
+ let release = () => {};
+ const released = new Promise<void>((resolve) => {
+ release = () => resolve();
+ });
+ return { hold: { beforeChunk, released } satisfies StreamHold, release };
+}
+
let turnCounter = 0;
export function streamSseResponse(
@@ -38,6 +59,7 @@ export function streamSseResponse(
options: StreamChunksOptions = {},
) {
const { delayMsBetweenChunks = 0, perChunkDelaysMs, abortSignal } = options;
+ let hold = options.hold;
const turnId = options.turnId ?? `sse-turn-${++turnCounter}`;
const encoder = new TextEncoder();
let nextIndex = options.after ? Number(options.after.split("-")[0]) : 0;
@@ -77,6 +99,23 @@ export function streamSseResponse(
}
return;
}
+ if (hold && nextIndex === hold.beforeChunk) {
+ const { released } = hold;
+ hold = undefined;
+ await new Promise<void>((resolve) => {
+ pendingResolve = resolve;
+ void released.then(resolve);
+ });
+ pendingResolve = undefined;
+ if (cancelled) {
+ try {
+ controller.close();
+ } catch {
+ // already closed
+ }
+ return;
+ }
+ }
const delayMs = delayForIndex(nextIndex);
if (delayMs > 0) {
await new Promise<void>((resolve) => {