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.
feat(frontend/copilot): fold the chat stream into the rows the server persists, and report where they drift (#15088)
Reinier van der Leer313c259c018d
diff --git a/autogpt_platform/frontend/package.json b/autogpt_platform/frontend/package.jsonindex 6871a2c342..b8f55b7b6a 100644--- a/autogpt_platform/frontend/package.json+++ b/autogpt_platform/frontend/package.json@@ -85,6 +85,7 @@"dexie": "4.2.1","dotenv": "17.2.3","embla-carousel-react": "8.6.0",+ "eventsource-parser": "3.0.6","flatbush": "4.5.0","framer-motion": "13.3.0","geist": "1.5.1",diff --git a/autogpt_platform/frontend/pnpm-lock.yaml b/autogpt_platform/frontend/pnpm-lock.yamlindex 5da3444beb..1911370e6a 100644--- a/autogpt_platform/frontend/pnpm-lock.yaml+++ b/autogpt_platform/frontend/pnpm-lock.yaml@@ -169,6 +169,9 @@ importers:embla-carousel-react:specifier: 8.6.0version: 8.6.0([email protected])+ eventsource-parser:+ specifier: 3.0.6+ version: 3.0.6flatbush:specifier: 4.5.0version: 4.5.0diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/drift/stream-shadow.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/drift/stream-shadow.test.tsxnew file mode 100644index 0000000000..a74eb697f9--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/drift/stream-shadow.test.tsx@@ -0,0 +1,120 @@+import * as Sentry from "@sentry/nextjs";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { getShadowLog, resetShadows } from "../../stream/turnShadow";+import { TEST_BACKEND_BASE_URL, TEST_SESSION_ID } from "../sse-helpers";+import {+ createBackendSim,+ loadRecordedTurn,+ renderAgainst,+ sendPrompt,+} from "./backend-sim";+import { resetChatState, waitForStableTranscript } from "./drift-report";++vi.mock("@sentry/nextjs", () => ({+ captureMessage: vi.fn(),+ captureException: vi.fn(),+ addBreadcrumb: vi.fn(),+ getTraceData: () => ({}),+}));+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 () => ({}) };+});+vi.mock("@/lib/auth/hooks/useAuth", () => ({+ useAuth: () => ({ isUserLoading: false, isLoggedIn: true }),+}));+const { shadowFlag } = vi.hoisted(() => ({ shadowFlag: { rate: 1 } }));+vi.mock("@/services/feature-flags/use-get-flag", async (importActual) => {+ const actual =+ await importActual<+ typeof import("@/services/feature-flags/use-get-flag")+ >();+ return {+ ...actual,+ useGetFlag: (flag: string) =>+ flag === actual.Flag.COPILOT_STREAM_SHADOW ? shadowFlag.rate : false,+ };+});++beforeEach(() => {+ resetChatState();+ resetShadows();+ shadowFlag.rate = 1;+ vi.mocked(Sentry.captureMessage).mockClear();+});++afterEach(() => resetChatState());++function driftKinds() {+ return vi+ .mocked(Sentry.captureMessage)+ .mock.calls.map(+ ([, hint]) =>+ (hint as { tags: { stream_drift: string } }).tags.stream_drift,+ );+}++// The shadow folds the stream the AI SDK renders, off the same response.+describe("the stream shadow inside the chat", () => {+ it.each(["dummy-text-turn", "baseline-tool-turn", "sdk-late-tool-result"])(+ "folds a sent turn to the persisted rows and reports nothing (%s)",+ { timeout: 60_000 },+ async (name) => {+ const turn = loadRecordedTurn(name);+ const sim = createBackendSim([{ turn }]);+ renderAgainst(sim);+ await sendPrompt(sim, turn);+ sim.publish();+ await waitForStableTranscript(1500);++ const log = getShadowLog(TEST_SESSION_ID);+ expect(log?.status).toBe("finished");+ expect(log?.rows.map((r) => r.content)).toEqual(+ turn.rows.slice(1).map((r) => r.content ?? ""),+ );+ expect(driftKinds()).toEqual([]);+ },+ );++ it(+ "leaves the stream alone while copilot-stream-shadow is 0",+ { timeout: 60_000 },+ async () => {+ shadowFlag.rate = 0;+ const turn = loadRecordedTurn("dummy-text-turn");+ const sim = createBackendSim([{ turn }]);+ renderAgainst(sim);+ await sendPrompt(sim, turn);+ sim.publish();+ await waitForStableTranscript(1500);++ expect(getShadowLog(TEST_SESSION_ID)).toBeNull();+ },+ );++ it(+ "W8: reports a reply whose final persist failed as drift at finish",+ { timeout: 60_000 },+ async () => {+ const turn = loadRecordedTurn("dummy-text-turn");+ const sim = createBackendSim([{ turn, persistedRows: [turn.rows[0]] }]);+ renderAgainst(sim);+ await sendPrompt(sim, turn);+ sim.publish();++ await vi.waitFor(() => expect(driftKinds()).toEqual(["finish"]), {+ timeout: 15_000,+ });+ },+ );+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamTransport.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamTransport.tsindex a59efa1407..22caad5bf4 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamTransport.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/copilotStreamTransport.ts@@ -8,6 +8,7 @@ import { getAutopilotModeChoice } from "./autopilotModeStore";import { createSmoothingTransform } from "./copilotStreamSmoothing";import { getKickoffExpertIdFromMetadata } from "./expertKickoff";import { getCopilotAuthHeaders } from "./helpers";+import { createShadowFetch } from "./stream/turnShadow";import { isVoiceTurn } from "./voice/pendingVoiceStart";import { isTokenDevtoolEnabled } from "./tokenDevtool/gate";import { createUsageCapturingFetch } from "./tokenDevtool/usageTap";@@ -63,11 +64,14 @@ export function createCopilotTransport({return new SmoothedCopilotChatTransport({api: baseUrl,- // Dev-only: tee the raw SSE stream so `: usage {...}` comments (dropped- // by the AI SDK parser) feed the token devtool badge.- ...(isTokenDevtoolEnabled()- ? { fetch: createUsageCapturingFetch(sessionId) }- : {}),+ // Tee the raw SSE into the stream converter's shadow, and in dev into the+ // token devtool, which reads the `: usage {...}` comments the SDK drops.+ fetch: createShadowFetch(+ sessionId,+ isTokenDevtoolEnabled()+ ? createUsageCapturingFetch(sessionId)+ : undefined,+ ),prepareSendMessagesRequest: async ({ messages }) => {const last = messages[messages.length - 1];const kickoffExpertId = getKickoffExpertIdFromMetadata(last.metadata);diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/renderTurnLog.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/renderTurnLog.test.tsnew file mode 100644index 0000000000..129416db7a--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/__tests__/renderTurnLog.test.ts@@ -0,0 +1,195 @@+import { describe, expect, it } from "vitest";++import { foldEntries, type WireChunk } from "../../stream/turnConverter";+import { emptyTurnLog, type TurnLog } from "../../stream/turnLog";+import {+ entriesOf,+ loadRecordedTurn,+ recordedTurnNames,+} from "../../stream/__tests__/recordedTurns";+import {+ convertChatSessionMessagesToUiMessages,+ createTurnLogRenderer,+} from "../convertChatSessionToUiMessages";++function fold(chunks: WireChunk[], log: TurnLog = emptyTurnLog()) {+ const offset = log.cursor ? Number(log.cursor.split("-")[0]) : 0;+ return foldEntries(+ log,+ chunks.map((chunk, i) => ({+ turn: "t",+ entryId: `${offset + i + 1}-0`,+ chunk,+ })),+ );+}++const render = (log: TurnLog) => createTurnLogRenderer((id) => id)(log);++// Follow-ups are drained at tool boundaries, so a drain follows a result.+const toolRound: WireChunk[] = [+ {+ type: "tool-input-available",+ toolCallId: "c1",+ toolName: "bash_exec",+ input: {},+ },+ { type: "tool-output-available", toolCallId: "c1", output: "ok" },+];++describe("rendering a turn log", () => {+ it("marks the block still writing as streaming, and done once it ends", () => {+ const open = fold([+ { type: "reasoning-start", id: "r" },+ { type: "reasoning-delta", id: "r", delta: "hmm" },+ { type: "text-start", id: "a" },+ { type: "text-delta", id: "a", delta: "Hel" },+ ]);+ expect(render(open)[0].parts).toMatchObject([+ { type: "reasoning", text: "hmm", state: "streaming" },+ { type: "text", text: "Hel", state: "streaming" },+ ]);+ const closed = fold(+ [+ { type: "reasoning-end", id: "r" },+ { type: "text-end", id: "a" },+ ],+ open,+ );+ expect(render(closed)[0].parts).toMatchObject([+ { state: "done" },+ { state: "done" },+ ]);+ });++ it("shows a pending call as running, then as interrupted once the turn is over", () => {+ const pending = fold([+ {+ type: "tool-input-available",+ toolCallId: "c1",+ toolName: "bash_exec",+ input: { command: "ls" },+ },+ ]);+ expect(render(pending)[0].parts).toEqual([+ expect.objectContaining({+ type: "tool-bash_exec",+ state: "input-available",+ input: { command: "ls" },+ }),+ ]);+ const finished = fold([{ type: "finish" }], pending);+ expect(render(finished)[0].parts).toEqual([+ expect.objectContaining({+ state: "output-error",+ errorText: "Interrupted",+ }),+ ]);+ const answered = fold(+ [+ {+ type: "tool-output-available",+ toolCallId: "c1",+ output: { files: 2 },+ },+ ],+ pending,+ );+ expect(render(answered)[0].parts).toEqual([+ expect.objectContaining({+ state: "output-available",+ output: { files: 2 },+ }),+ ]);+ });++ it("puts a status that arrives before any content in the turn's own bubble, under one key throughout", () => {+ const status = fold([+ { type: "data-status", data: { message: "Message received…" } },+ { type: "start-step" },+ ]);+ const [early] = render(status);+ expect(early.id).toBe("turn:t");+ expect(early.parts).toEqual([+ {+ type: "data-status",+ id: "1-0",+ data: { message: "Message received…" },+ },+ { type: "step-start" },+ ]);+ const withText = fold(+ [+ { type: "text-start", id: "a" },+ { type: "text-delta", id: "a", delta: "Hi" },+ ],+ status,+ );+ const [later] = render(withText);+ expect(later.id).toBe("turn:t");+ expect(later.parts.map((p) => p.type)).toEqual(["data-status", "text"]);+ });++ it("gives a drained follow-up its own bubble and keys the answer after it by that follow-up", () => {+ const log = fold([+ { type: "text-start", id: "a" },+ { type: "text-delta", id: "a", delta: "One." },+ { type: "text-end", id: "a" },+ ...toolRound,+ {+ type: "data-pending-drained",+ data: { messages: [{ id: "p1", content: "and two?" }] },+ },+ { type: "text-start", id: "b" },+ { type: "text-delta", id: "b", delta: "Two." },+ ]);+ expect(render(log).map((m) => [m.id, m.role])).toEqual([+ ["turn:t", "assistant"],+ ["user:p1", "user"],+ ["turn:t:after:user:p1", "assistant"],+ ]);+ });++ it("returns an unchanged bubble as the same object, so React skips it", () => {+ const renderer = createTurnLogRenderer((id) => id);+ const before = fold([+ { type: "text-start", id: "a" },+ { type: "text-delta", id: "a", delta: "One." },+ { type: "text-end", id: "a" },+ ...toolRound,+ {+ type: "data-pending-drained",+ data: { messages: [{ id: "p1", content: "more" }] },+ },+ { type: "text-start", id: "b" },+ { type: "text-delta", id: "b", delta: "Tw" },+ ]);+ const first = renderer(before);+ const second = renderer(+ fold([{ type: "text-delta", id: "b", delta: "o." }], before),+ );+ expect(second[0]).toBe(first[0]);+ expect(second[1]).toBe(first[1]);+ expect(second[2]).not.toBe(first[2]);+ expect(second[2].parts).toMatchObject([{ text: "Two." }]);+ });+});++// The renderer shares its row rules with hydration, so the finished turn a+// user watched stream is the turn a reload of its rows shows.+describe.each(recordedTurnNames())("live equals reload: %s", (name) => {+ it("renders the folded turn as hydration renders its persisted rows", async () => {+ const turn = loadRecordedTurn(name);+ const log = foldEntries(emptyTurnLog(), await entriesOf(turn));+ const first = log.checkpoints[0].sequence;+ const reloaded = convertChatSessionMessagesToUiMessages(+ "s",+ turn.rows.filter((row) => (row.sequence ?? -1) >= first),+ { isComplete: true },+ ).messages;+ const live = render(log);+ expect(+ live.map((m) => m.parts.filter((p) => !p.type.startsWith("data-"))),+ ).toEqual(reloaded.map((m) => m.parts));+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.tsindex 31cefc2ae8..afd1ac70b3 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers/convertChatSessionToUiMessages.ts@@ -1,5 +1,6 @@import { getGetWorkspaceDownloadFileByIdUrl } from "@/app/api/__generated__/endpoints/workspace/workspace";import type { FileUIPart, UIMessage, UIDataTypes, UITools } from "ai";+import type { LogRow, OverlayPart, TurnLog } from "../stream/turnLog";import { toolDisplayName } from "./toolDisplay";import {WORKSPACE_FOLDER_PART_TYPE,@@ -437,94 +438,11 @@ export function convertChatSessionMessagesToUiMessages(const uiRole: "user" | "assistant" =msg.role === "reasoning" ? "assistant" : msg.role;- const parts: UIMessage<unknown, UIDataTypes, UITools>["parts"] = [];-- if (typeof msg.content === "string" && msg.content.trim()) {- if (msg.role === "reasoning") {- parts.push({- type: "reasoning",- text: msg.content,- state: "done",- } as UIMessage<unknown, UIDataTypes, UITools>["parts"][number]);- } else if (msg.role === "user") {- const { cleanText, fileParts, folderParts } = extractFileParts(- msg.content,- fileUrlBuilder,- );- if (cleanText) {- parts.push({ type: "text", text: cleanText, state: "done" });- }- for (const fp of fileParts) {- parts.push(fp);- }- for (const folderPart of folderParts) {- parts.push(- folderPart as UIMessage<- unknown,- UIDataTypes,- UITools- >["parts"][number],- );- }- } else {- parts.push({ type: "text", text: msg.content, state: "done" });- }- }-- if (uiRole === "assistant" && Array.isArray(msg.tool_calls)) {- for (const rawToolCall of msg.tool_calls) {- if (!rawToolCall || typeof rawToolCall !== "object") continue;- const toolCall = rawToolCall as {- id?: unknown;- display_name?: unknown;- function?: { name?: unknown; arguments?: unknown };- };-- const toolCallId = String(toolCall.id ?? "").trim();- const toolName = String(toolCall.function?.name ?? "").trim();- if (!toolCallId || !toolName) continue;-- const input = toToolInput(toolCall.function?.arguments);- const output = toolOutputsByCallId.get(toolCallId);- const title = toolDisplayName(toolCall.display_name) ?? undefined;-- if (output !== undefined) {- parts.push({- type: `tool-${toolName}`,- toolCallId,- title,- state: "output-available",- input,- output: typeof output === "string" ? safeJsonParse(output) : output,- });- } else if (options?.isComplete) {- // Session is complete (no active stream) but this tool call has- // no output in the DB — mark as completed to stop stale spinners.- parts.push({- type: `tool-${toolName}`,- toolCallId,- title,- state: "output-available",- input,- output: "",- });- } else {- parts.push({- type: `tool-${toolName}`,- toolCallId,- title,- state: "input-available",- input,- });- }- }- }-- // User messages must always be rendered, even with empty content, so the- // initial prompt is visible when reloading a session.- if (parts.length === 0 && msg.role === "user") {- parts.push({ type: "text", text: "", state: "done" });- }+ const parts = buildRowParts(msg, {+ toolOutputsByCallId,+ fileUrlBuilder,+ unanswered: options?.isComplete ? "completed" : "input-available",+ });if (parts.length === 0) return;// Merge consecutive assistant messages (including reasoning rows) into a@@ -624,3 +542,277 @@ export function convertChatSessionMessagesToUiMessages(: (uiMessages[activeTurnStartIndex]?.id ?? null),};}++type UiMessage = UIMessage<unknown, UIDataTypes, UITools>;+type UiPart = UiMessage["parts"][number];++interface RowPartsContext {+ toolOutputsByCallId: Map<string, unknown>;+ fileUrlBuilder: (fileId: string) => string;+ /** How a tool call with no output renders: still running, or the turn is over. */+ unanswered: "input-available" | "completed" | "interrupted";+ /** A block is still writing into this row. */+ streaming?: boolean;+}++function buildRowParts(msg: SessionChatMessage, ctx: RowPartsContext) {+ const textState = ctx.streaming ? "streaming" : "done";+ const parts: UiPart[] = [];++ if (typeof msg.content === "string" && msg.content.trim()) {+ if (msg.role === "reasoning") {+ parts.push({ type: "reasoning", text: msg.content, state: textState });+ } else if (msg.role === "user") {+ const { cleanText, fileParts, folderParts } = extractFileParts(+ msg.content,+ ctx.fileUrlBuilder,+ );+ if (cleanText) {+ parts.push({ type: "text", text: cleanText, state: "done" });+ }+ parts.push(...fileParts);+ parts.push(...(folderParts as UiPart[]));+ } else {+ parts.push({ type: "text", text: msg.content, state: textState });+ }+ }++ if (msg.role !== "user" && Array.isArray(msg.tool_calls)) {+ for (const rawToolCall of msg.tool_calls) {+ const part = toolCallPart(rawToolCall, ctx);+ if (part) parts.push(part);+ }+ }++ // User messages must always be rendered, even with empty content, so the+ // initial prompt is visible when reloading a session.+ if (parts.length === 0 && msg.role === "user") {+ parts.push({ type: "text", text: "", state: "done" });+ }+ return parts;+}++function toolCallPart(rawToolCall: unknown, ctx: RowPartsContext) {+ if (!rawToolCall || typeof rawToolCall !== "object") return null;+ const toolCall = rawToolCall as {+ id?: unknown;+ display_name?: unknown;+ function?: { name?: unknown; arguments?: unknown };+ };+ const toolCallId = String(toolCall.id ?? "").trim();+ const toolName = String(toolCall.function?.name ?? "").trim();+ if (!toolCallId || !toolName) return null;++ const base = {+ type: `tool-${toolName}` as const,+ toolCallId,+ title: toolDisplayName(toolCall.display_name) ?? undefined,+ input: toToolInput(toolCall.function?.arguments),+ };+ const output = ctx.toolOutputsByCallId.get(toolCallId);+ if (output !== undefined) {+ return {+ ...base,+ state: "output-available",+ output: typeof output === "string" ? safeJsonParse(output) : output,+ } as UiPart;+ }+ // Session is complete (no active stream) but this tool call has no output+ // in the DB — mark as completed to stop stale spinners.+ if (ctx.unanswered === "completed") {+ return { ...base, state: "output-available", output: "" } as UiPart;+ }+ if (ctx.unanswered === "interrupted") {+ return {+ ...base,+ state: "output-error",+ errorText: "Interrupted",+ } as UiPart;+ }+ return { ...base, state: "input-available" } as UiPart;+}++/**+ * Render a live turn from its log with the same row rules as hydration, so a+ * reload shows what the stream showed. A message whose rows, open flags,+ * tool outputs and overlay did not change is returned as the same object, so+ * React skips it.+ */+export function createTurnLogRenderer(+ fileUrlBuilder: (fileId: string) => string = defaultWorkspaceFileUrl,+) {+ let memo = new Map<string, { deps: unknown[]; message: UiMessage }>();++ return function renderTurnLog(log: TurnLog): UiMessage[] {+ const outputs = new Map<string, unknown>();+ for (const row of log.rows) {+ if (row.role === "tool" && row.toolCallId) {+ outputs.set(row.toolCallId, row.content);+ }+ }+ const turnOver = log.status === "finished" || log.status === "failed";+ const streamingRows = new Set(+ Object.values(log.blocks)+ .filter((block) => block.open && block.row !== null)+ .map((block) => block.row),+ );+ const stepIsEmpty =+ log.openStep !== null &&+ log.openStep.rows === log.rows &&+ log.openStep.overlay === log.overlay;+ const ctx: RowPartsContext = {+ toolOutputsByCallId: outputs,+ fileUrlBuilder,+ unanswered: turnOver ? "interrupted" : "input-available",+ };++ const groups = groupTurnRows(log);+ const next = new Map<string, { deps: unknown[]; message: UiMessage }>();+ const messages = groups.map((group, index) => {+ const trailingStep = stepIsEmpty && index === groups.length - 1;+ const rows = group.rows.map((i) => log.rows[i]);+ const deps: unknown[] = [+ ctx.unanswered,+ trailingStep,+ ...group.rows.flatMap((i) => [log.rows[i], streamingRows.has(i)]),+ ...rows.flatMap((row) => row.toolCalls.map((c) => outputs.get(c.id))),+ ...group.overlay,+ ];+ const cached = memo.get(group.key);+ const message =+ cached && sameDeps(cached.deps, deps)+ ? cached.message+ : buildGroupMessage(+ group,+ log.rows,+ streamingRows,+ trailingStep,+ ctx,+ );+ next.set(group.key, { deps, message });+ return message;+ });+ memo = next;+ return messages;+ };+}++interface TurnGroup {+ key: string;+ role: "user" | "assistant";+ /** Indices of the rows that draw parts; tool rows only feed outputs. */+ rows: number[];+ overlay: OverlayPart[];+}++/**+ * Consecutive assistant and reasoning rows share a bubble, as on reload. The+ * keys never depend on sequences: the first assistant bubble is the turn's,+ * and a later one is keyed by the drained follow-up it answers.+ */+function groupTurnRows(log: TurnLog): TurnGroup[] {+ const turn = log.turnId ?? "";+ const groups: TurnGroup[] = [];+ const groupOfRow: number[] = [];+ let afterUser: string | null = null;+ const assistantKey = () =>+ afterUser ? `turn:${turn}:after:${afterUser}` : `turn:${turn}`;++ log.rows.forEach((row, i) => {+ const last = groups[groups.length - 1];+ if (row.role === "user") {+ groups.push({ key: row.key, role: "user", rows: [i], overlay: [] });+ afterUser = row.key;+ } else if (row.role !== "tool") {+ if (last?.role === "assistant") last.rows.push(i);+ else {+ groups.push({+ key: assistantKey(),+ role: "assistant",+ rows: [i],+ overlay: [],+ });+ }+ }+ groupOfRow[i] = groups.length - 1;+ });++ for (const part of log.overlay) {+ const from = part.anchor === 0 ? -1 : groupOfRow[part.anchor - 1];+ let target =+ from >= 0 && groups[from].role === "assistant"+ ? from+ : groups.findIndex((g, gi) => gi > from && g.role === "assistant");+ if (target === -1) {+ groups.push({+ key: assistantKey(),+ role: "assistant",+ rows: [],+ overlay: [],+ });+ target = groups.length - 1;+ }+ groups[target].overlay.push(part);+ }+ return groups;+}++function buildGroupMessage(+ group: TurnGroup,+ rows: readonly LogRow[],+ streamingRows: Set<number | null>,+ trailingStep: boolean,+ ctx: RowPartsContext,+): UiMessage {+ const parts: UiPart[] = [];+ let overlayIndex = 0;+ const flushOverlayBefore = (rowIndex: number) => {+ while (+ overlayIndex < group.overlay.length &&+ group.overlay[overlayIndex].anchor <= rowIndex+ ) {+ const { type, entryId, data } = group.overlay[overlayIndex++];+ parts.push({ type, id: entryId, data } as UiPart);+ }+ };+ for (const index of group.rows) {+ flushOverlayBefore(index);+ parts.push(+ ...buildRowParts(sessionRowOf(rows[index]), {+ ...ctx,+ streaming: streamingRows.has(index),+ }),+ );+ }+ flushOverlayBefore(Infinity);+ if (trailingStep) parts.push({ type: "step-start" });+ const metadata = group.rows.length ? rows[group.rows[0]].metadata : null;+ return {+ id: group.key,+ role: group.role,+ parts,+ ...(metadata ? { metadata } : {}),+ };+}++function sessionRowOf(row: LogRow): SessionChatMessage {+ return {+ id: null,+ role: row.role,+ content: row.content,+ tool_call_id: row.toolCallId,+ tool_calls: row.toolCalls.map((call) => ({+ id: call.id,+ display_name: call.displayName,+ function: { name: call.name, arguments: call.input },+ })),+ sequence: row.sequence,+ duration_ms: null,+ created_at: null,+ metadata: row.metadata,+ };+}++function sameDeps(a: unknown[], b: unknown[]) {+ return a.length === b.length && a.every((value, i) => Object.is(value, b[i]));+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/pacedTransport.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/pacedTransport.test.tsnew file mode 100644index 0000000000..da62689f17--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/pacedTransport.test.ts@@ -0,0 +1,102 @@+import { server } from "@/mocks/mock-server";+import { Chat } from "@ai-sdk/react";+import type { UIMessage } from "ai";+import { http, HttpResponse } from "msw";+import { afterEach, describe, expect, it, vi } from "vitest";++import { createCopilotTransport } from "../../copilotStreamTransport";+import { getShadowLog, resetShadows, setStreamShadowRate } from "../turnShadow";++const BASE = "http://localhost:18006";+vi.mock("@/services/environment", async (importActual) => {+ const actual = await importActual<typeof import("@/services/environment")>();+ return {+ ...actual,+ environment: { ...actual.environment, getAGPTServerBaseUrl: () => BASE },+ };+});+vi.mock("../../helpers", async (importActual) => {+ const actual = await importActual<typeof import("../../helpers")>();+ return { ...actual, getCopilotAuthHeaders: async () => ({}) };+});++afterEach(() => resetShadows());++// The SDK reads the teed branch through the word pacing, long after the tap+// has read the whole body; the turn must still end as a clean finish.+describe("a paced POST turn read through the shadow tee", () => {+ it(+ "finishes without a disconnect or an error",+ { timeout: 60_000 },+ async () => {+ setStreamShadowRate(1);+ server.use(+ http.post(+ `${BASE}/api/chat/sessions/s1/stream`,+ () =>+ new HttpResponse(arrivingOverTime(essayFrames(300), 5), {+ headers: { "content-type": "text/event-stream" },+ }),+ ),+ );+ const finishes: unknown[] = [];+ const errors: Error[] = [];+ const chat = new Chat<UIMessage>({+ id: "s1",+ transport: createCopilotTransport({+ sessionId: "s1",+ copilotModelRef: { current: undefined },+ }),+ onFinish: (args) => finishes.push(args),+ onError: (error) => errors.push(error),+ });++ await chat.sendMessage({ text: "Write an essay" });++ expect(errors).toEqual([]);+ expect(finishes).toEqual([+ expect.objectContaining({ isDisconnect: false, isError: false }),+ ]);+ expect(chat.status).toBe("ready");+ expect(getShadowLog("s1")?.status).toBe("finished");+ },+ );+});++function essayFrames(words: number) {+ let n = 0;+ const frame = (data: unknown) =>+ `id: t:${++n}-0\ndata: ${JSON.stringify(data)}\n\n`;+ const frames = [+ frame({ type: "start", messageId: "m1" }),+ frame({ type: "start-step" }),+ frame({ type: "text-start", id: "b" }),+ ];+ for (let i = 0; i < words; i += 6) {+ frames.push(+ frame({ type: "text-delta", id: "b", delta: "lighthouse ".repeat(6) }),+ );+ }+ frames.push(+ frame({ type: "text-end", id: "b" }),+ frame({ type: "finish-step" }),+ frame({ type: "finish" }),+ "data: [DONE]\n\n",+ );+ return frames;+}++function arrivingOverTime(frames: string[], everyMs: number) {+ const encoder = new TextEncoder();+ let i = 0;+ return new ReadableStream<Uint8Array>({+ async pull(controller) {+ if (i >= frames.length) {+ controller.close();+ return;+ }+ await new Promise((resolve) => setTimeout(resolve, everyMs));+ controller.enqueue(encoder.encode(frames[i++]));+ },+ });+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/recordedTurns.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/recordedTurns.tsnew file mode 100644index 0000000000..475b8de024--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/recordedTurns.ts@@ -0,0 +1,51 @@+import fs from "node:fs";+import path from "node:path";++import { readSseFrames } from "../sseClient";+import type { StreamEntry } from "../turnConverter";+import type { PersistedRow } from "../turnLog";++const FIXTURE_ROOT = path.resolve(+ process.cwd(),+ "../backend/test/fixtures/copilot_stream",+);++export interface RecordedTurn {+ name: string;+ /** The frames the route writes for each stored entry, as the browser reads them. */+ sse: string[];+ /** Every row the session GET returns once the turn has ended. */+ rows: PersistedRow[];+}++/** Every turn `stream_drift/recording.py` recorded, both engines. */+export function recordedTurnNames(): string[] {+ return fs+ .readdirSync(FIXTURE_ROOT, { withFileTypes: true })+ .filter((entry) => entry.isDirectory())+ .map((entry) => entry.name)+ .sort();+}++export function loadRecordedTurn(name: string): RecordedTurn {+ const dir = path.join(FIXTURE_ROOT, name);+ const sse = fs+ .readFileSync(path.join(dir, "frames.jsonl"), "utf8")+ .split("\n")+ .filter(Boolean)+ .map((line) => (JSON.parse(line) as { sse: string }).sse);+ const rows = JSON.parse(+ fs.readFileSync(path.join(dir, "rows.json"), "utf8"),+ ) as PersistedRow[];+ return { name, sse, rows };+}++/** The turn's entries, through the same SSE parser the client uses. */+export async function entriesOf(turn: RecordedTurn): Promise<StreamEntry[]> {+ const entries: StreamEntry[] = [];+ const body = new Response(turn.sse.join("") + "data: [DONE]\n\n").body!;+ await readSseFrames(body, (frame) => {+ if (frame.kind === "entry") entries.push(frame.entry);+ });+ return entries;+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/replayEquivalence.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/replayEquivalence.test.tsnew file mode 100644index 0000000000..fcb2e2d660--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/replayEquivalence.test.ts@@ -0,0 +1,140 @@+import { createHash } from "node:crypto";+import { beforeAll, describe, expect, it } from "vitest";++import { foldEntries, type StreamEntry } from "../turnConverter";+import {+ canonicalRows,+ emptyTurnLog,+ logRowFromPersisted,+ seedTurnLog,+ type LogRow,+} from "../turnLog";+import { diffRows } from "../turnShadow";+import {+ entriesOf,+ loadRecordedTurn,+ recordedTurnNames,+ type RecordedTurn,+} from "./recordedTurns";++const names = recordedTurnNames();++describe.each(names)("replay equivalence: %s", (name) => {+ let turn: RecordedTurn;+ let entries: StreamEntry[];+ let whole: ReturnType<typeof emptyTurnLog>;++ beforeAll(async () => {+ turn = loadRecordedTurn(name);+ entries = await entriesOf(turn);+ whole = foldEntries(emptyTurnLog(), entries);+ });++ it("the fold from 0-0 equals the rows the backend persisted", () => {+ const final = whole.checkpoints[whole.checkpoints.length - 1];+ expect(final, "the recorded turn ends on a checkpoint").toBeDefined();+ const persisted = turnRows(turn, final.sequence);++ expect(whole.protocolErrors).toEqual([]);+ expect(whole.status).toBe("finished");+ expect(final.rows).toBe(persisted.length);+ expect(diffRows(whole.rows, persisted)).toEqual([]);+ expect(whole.rows.map((r) => r.sequence)).toEqual(+ persisted.map((r) => r.sequence),+ );+ });++ it("every checkpoint's digest is the digest of the folded rows", () => {+ expect(whole.checkpoints.length).toBeGreaterThan(0);+ for (const checkpoint of whole.checkpoints) {+ expect(sha256(checkpoint.canonical)).toBe(checkpoint.digest);+ }+ });++ it("every cut point, resumed at or before its cursor, equals the whole fold", () => {+ for (let cut = 0; cut <= entries.length; cut++) {+ const head = foldEntries(emptyTurnLog(), entries.slice(0, cut));+ for (let from = 0; from <= cut; from++) {+ expect(foldEntries(head, entries.slice(from))).toEqual(whole);+ }+ }+ });++ it("the persisted rows at each checkpoint, tailed from it, equal the whole fold", () => {+ whole.checkpoints.forEach((checkpoint) => {+ const at = entries.findIndex((e) => e.entryId === checkpoint.entryId);+ const seeded = seedTurnLog({+ turnId: whole.turnId!,+ rows: persistedAt(turn, checkpoint.sequence, checkpoint.rows),+ checkpoint: {+ entry_id: checkpoint.entryId,+ rows: checkpoint.rows,+ sequence: checkpoint.sequence,+ },+ });+ const resumed = foldEntries(seeded, entries.slice(at + 1));+ expect(resumed.protocolErrors).toEqual([]);+ expect(diffRows(resumed.rows, whole.rows)).toEqual([]);+ expect(canonicalRows(resumed.rows)).toBe(canonicalRows(whole.rows));+ });+ });++ // A second connection opens at the cursor it finds, or before it; from then+ // on both deliver in their own order and race arbitrarily.+ it("two overlapping deliveries, randomly interleaved, converge", () => {+ const random = seededRandom(name.length * 7919);+ for (let trial = 0; trial < 200; trial++) {+ const applied = Math.floor(random() * (entries.length + 1));+ const opensAt = Math.floor(random() * (applied + 1));+ const merged = [+ ...entries.slice(0, applied),+ ...interleave(entries.slice(applied), entries.slice(opensAt), random),+ ];+ expect(foldEntries(emptyTurnLog(), merged)).toEqual(whole);+ }+ });+});++it("the recorded turns cover both engines", () => {+ expect(names).toEqual(+ expect.arrayContaining(["baseline-tool-turn", "sdk-late-tool-result"]),+ );+});++function turnRows(turn: RecordedTurn, fromSequence: number): LogRow[] {+ return turn.rows+ .filter((row) => (row.sequence ?? -1) >= fromSequence)+ .map(logRowFromPersisted);+}++function persistedAt(turn: RecordedTurn, sequence: number, count: number) {+ return turn.rows.filter(+ (row) =>+ (row.sequence ?? -1) >= sequence &&+ (row.sequence ?? -1) < sequence + count,+ );+}++/** Each delivery keeps its own order; the two race arbitrarily. */+function interleave<T>(a: T[], b: T[], random: () => number): T[] {+ const out: T[] = [];+ let i = 0;+ let j = 0;+ while (i < a.length || j < b.length) {+ const takeA = j >= b.length || (i < a.length && random() < 0.5);+ out.push(takeA ? a[i++] : b[j++]);+ }+ return out;+}++function seededRandom(seed: number) {+ let state = seed % 2147483647 || 1;+ return () => {+ state = (state * 16807) % 2147483647;+ return (state - 1) / 2147483646;+ };+}++function sha256(text: string) {+ return createHash("sha256").update(text, "utf8").digest("hex");+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/sseClient.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/sseClient.test.tsnew file mode 100644index 0000000000..24499b1b42--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/sseClient.test.ts@@ -0,0 +1,101 @@+import { server } from "@/mocks/mock-server";+import { http, HttpResponse } from "msw";+import { describe, expect, it } from "vitest";++import { fetchSse, readSseFrames, type SseFrame } from "../sseClient";++function bodyOf(parts: (string | Uint8Array)[]) {+ const encoder = new TextEncoder();+ return new ReadableStream<Uint8Array>({+ start(controller) {+ for (const part of parts) {+ controller.enqueue(+ typeof part === "string" ? encoder.encode(part) : part,+ );+ }+ controller.close();+ },+ });+}++async function framesOf(parts: (string | Uint8Array)[]) {+ const frames: SseFrame[] = [];+ await readSseFrames(bodyOf(parts), (frame) => frames.push(frame));+ return frames;+}++describe("readSseFrames", () => {+ it("tells entries, the route's own frames, comments and the terminator apart", async () => {+ const frames = await framesOf([+ 'id: turn-1:5-0\ndata: {"type":"text-delta","id":"a","delta":"hi"}\n\n',+ ": heartbeat\n\n",+ 'data: {"type":"error","errorText":"route failed"}\n\n',+ "data: [DONE]\n\n",+ ]);+ expect(frames).toEqual([+ {+ kind: "entry",+ entry: {+ turn: "turn-1",+ entryId: "5-0",+ chunk: { type: "text-delta", id: "a", delta: "hi" },+ },+ },+ { kind: "comment", text: "heartbeat" },+ {+ kind: "synthetic",+ chunk: { type: "error", errorText: "route failed" },+ },+ { kind: "done" },+ ]);+ });++ it("does not carry an entry's id over to the id-less frame after it", async () => {+ const frames = await framesOf([+ 'id: t:1-0\ndata: {"type":"start","messageId":"m"}\n\n',+ 'data: {"type":"finish"}\n\n',+ ]);+ expect(frames.map((f) => f.kind)).toEqual(["entry", "synthetic"]);+ });++ it("reassembles a frame, and a character, split across network chunks", async () => {+ const bytes = new TextEncoder().encode(+ 'id: t:1-0\ndata: {"type":"text-delta","id":"a","delta":"…"}\n\n',+ );+ const ellipsis = bytes.indexOf(0xe2);+ const frames = await framesOf([+ bytes.slice(0, ellipsis + 1),+ bytes.slice(ellipsis + 1),+ ]);+ expect(frames).toEqual([+ expect.objectContaining({+ entry: expect.objectContaining({+ chunk: expect.objectContaining({ delta: "…" }),+ }),+ }),+ ]);+ });+});++describe("fetchSse", () => {+ it("returns a refused resume's status and body instead of frames", async () => {+ server.use(+ http.get("http://sse.test/stream", () =>+ HttpResponse.json(+ { reason: "trimmed", checkpoint: { entry_id: "9-0" } },+ { status: 409 },+ ),+ ),+ );+ const frames: SseFrame[] = [];+ const result = await fetchSse("http://sse.test/stream", {}, (f) =>+ frames.push(f),+ );+ expect(result).toEqual({+ ok: false,+ status: 409,+ body: { reason: "trimmed", checkpoint: { entry_id: "9-0" } },+ });+ expect(frames).toEqual([]);+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/turnConverter.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/turnConverter.test.tsnew file mode 100644index 0000000000..de0a488b44--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/turnConverter.test.ts@@ -0,0 +1,455 @@+import { describe, expect, it } from "vitest";++import { applyEntry, foldEntries, type WireChunk } from "../turnConverter";+import {+ COPILOT_ERROR_PREFIX,+ COPILOT_RETRYABLE_ERROR_PREFIX,+ emptyTurnLog,+ seedTurnLog,+ type TurnLog,+} from "../turnLog";++const TURN = "turn-1";++function fold(chunks: WireChunk[], log: TurnLog = emptyTurnLog()) {+ const offset = log.cursor ? Number(log.cursor.split("-")[0]) : 0;+ return foldEntries(+ log,+ chunks.map((chunk, i) => ({+ turn: TURN,+ entryId: `${offset + i + 1}-0`,+ chunk,+ })),+ );+}++function rowsOf(log: TurnLog) {+ return log.rows.map((r) => ({+ role: r.role,+ content: r.content,+ calls: r.toolCalls.map((c) => c.id),+ ...(r.toolCallId ? { toolCallId: r.toolCallId } : {}),+ }));+}++function reasons(log: TurnLog) {+ return log.protocolErrors.map((e) => `${e.chunkType}: ${e.reason}`);+}++const text = (id: string, delta: string): WireChunk[] => [+ { type: "text-start", id },+ { type: "text-delta", id, delta },+ { type: "text-end", id },+];+const call = (id: string, input: unknown = {}): WireChunk[] => [+ { type: "tool-input-start", toolCallId: id, toolName: "bash_exec" },+ {+ type: "tool-input-available",+ toolCallId: id,+ toolName: "bash_exec",+ input,+ },+];+const output = (id: string, value: unknown = "ok"): WireChunk => ({+ type: "tool-output-available",+ toolCallId: id,+ output: value,+});++describe("the converter table", () => {+ it("start opens the turn; a second start changes nothing", () => {+ const log = fold([{ type: "start", messageId: "m1" }]);+ expect(log.status).toBe("running");+ expect(log.messageId).toBe("m1");+ expect(fold([{ type: "start", messageId: "m1" }], log)).toMatchObject({+ messageId: "m1",+ rows: [],+ });+ });++ it("a start for a second message in the turn opens a new assistant row", () => {+ const log = fold([+ { type: "start", messageId: "m1" },+ ...text("a", "First reply."),+ { type: "start", messageId: "m2" },+ ...text("b", "Second reply."),+ ]);+ expect(rowsOf(log)).toEqual([+ { role: "assistant", content: "First reply.", calls: [] },+ { role: "assistant", content: "Second reply.", calls: [] },+ ]);+ });++ it("start-step opens a step and finish-step closes it", () => {+ const opened = fold([{ type: "start-step" }]);+ expect(opened.openStep).not.toBeNull();+ expect(fold([{ type: "finish-step" }], opened).openStep).toBeNull();+ });++ it("finish-step with a block still open closes it and counts a protocol error", () => {+ const log = fold([+ { type: "start-step" },+ { type: "text-start", id: "a" },+ { type: "text-delta", id: "a", delta: "hi" },+ { type: "finish-step" },+ ]);+ expect(log.blocks.a.open).toBe(false);+ expect(reasons(log)).toEqual(["finish-step: block open"]);+ });++ it("a known text-start continues the block instead of opening a second one", () => {+ const log = fold([+ { type: "text-start", id: "a" },+ { type: "text-delta", id: "a", delta: "one " },+ { type: "text-start", id: "a" },+ { type: "text-delta", id: "a", delta: "two" },+ ]);+ expect(rowsOf(log)).toEqual([+ { role: "assistant", content: "one two", calls: [] },+ ]);+ expect(log.protocolErrors).toEqual([]);+ });++ it("text opens its row on the first delta, so an empty block persists nothing", () => {+ expect(+ fold([+ { type: "text-start", id: "a" },+ { type: "text-end", id: "a" },+ ]).rows,+ ).toEqual([]);+ });++ it("reasoning-start appends its own row at once", () => {+ const log = fold([+ { type: "reasoning-start", id: "r" },+ { type: "reasoning-delta", id: "r", delta: "think" },+ { type: "reasoning-end", id: "r" },+ ]);+ expect(rowsOf(log)).toEqual([+ { role: "reasoning", content: "think", calls: [] },+ ]);+ });++ it.each([+ [+ "text-delta",+ "closed block",+ [...text("a", "x"), { type: "text-delta", id: "a", delta: "y" }],+ ],+ [+ "text-delta",+ "unknown block",+ [{ type: "text-delta", id: "nope", delta: "y" }],+ ],+ [+ "reasoning-delta",+ "closed block",+ [+ { type: "reasoning-start", id: "r" },+ { type: "reasoning-end", id: "r" },+ { type: "reasoning-delta", id: "r", delta: "y" },+ ],+ ],+ [+ "reasoning-delta",+ "unknown block",+ [{ type: "reasoning-delta", id: "nope", delta: "y" }],+ ],+ [+ "text-delta",+ "unknown block",+ [+ { type: "reasoning-start", id: "r" },+ { type: "text-delta", id: "r", delta: "y" },+ ],+ ],+ ["text-end", "unknown block", [{ type: "text-end", id: "nope" }]],+ ["reasoning-end", "unknown block", [{ type: "reasoning-end", id: "nope" }]],+ ] as [string, string, WireChunk[]][])(+ "%s on a %s is a protocol error that changes no row",+ (type, reason, chunks) => {+ const before = fold(chunks.slice(0, -1));+ const after = fold(chunks.slice(-1), before);+ expect(reasons(after)).toEqual([`${type}: ${reason}`]);+ expect(after.rows).toBe(before.rows);+ },+ );++ it("an end on a closed block is a no-op", () => {+ const log = fold([...text("a", "x"), { type: "text-end", id: "a" }]);+ expect(log.protocolErrors).toEqual([]);+ });++ it("tool-input-available opens the call on the current assistant row", () => {+ const log = fold([...text("a", "Let me look."), ...call("c1", { q: 1 })]);+ expect(rowsOf(log)).toEqual([+ { role: "assistant", content: "Let me look.", calls: ["c1"] },+ ]);+ expect(log.tools.c1.phase).toBe("input-available");+ });++ it("a repeated input-available with the same input is a no-op", () => {+ const once = fold(call("c1", { q: 1 }));+ const twice = fold(call("c1", { q: 1 }), once);+ expect(twice.rows).toBe(once.rows);+ expect(twice.protocolErrors).toEqual([]);+ });++ it("a second input-available with a different input is a protocol error", () => {+ const log = fold([+ ...call("c1", { q: 1 }),+ ...call("c1", { q: 2 }).slice(1),+ ]);+ expect(reasons(log)).toEqual(["tool-input-available: input changed"]);+ expect(log.rows[0].toolCalls[0].input).toEqual({ q: 1 });+ });++ it("tool-output-available sets the result row once; a repeat is a no-op", () => {+ const once = fold([...call("c1"), output("c1", "first")]);+ const twice = fold([output("c1", "second")], once);+ expect(rowsOf(twice)).toEqual([+ { role: "assistant", content: "", calls: ["c1"] },+ { role: "tool", content: "first", calls: [], toolCallId: "c1" },+ ]);+ expect(twice.rows).toBe(once.rows);+ });++ it("an output for an unknown call is a protocol error, and still the row the backend persists", () => {+ const log = fold([output("ghost", { todos: [] })]);+ expect(reasons(log)).toEqual(["tool-output-available: unknown tool call"]);+ expect(rowsOf(log)).toEqual([+ { role: "tool", content: '{"todos":[]}', calls: [], toolCallId: "ghost" },+ ]);+ });++ it.each([+ [+ "[code:transient_api_error] Try again",+ COPILOT_RETRYABLE_ERROR_PREFIX,+ "Try again",+ ],+ ["[code:baseline_error] Boom", COPILOT_RETRYABLE_ERROR_PREFIX, "Boom"],+ ["[code:sdk_stream_error] Boom", COPILOT_ERROR_PREFIX, "Boom"],+ ["No code at all", COPILOT_ERROR_PREFIX, "No code at all"],+ ])(+ "error %j appends a marker row decided by its code",+ (errorText, prefix, shown) => {+ const log = fold([...text("a", "partial"), { type: "error", errorText }]);+ expect(log.rows[1].content).toBe(`${prefix} ${shown}`);+ expect(log.status).toBe("failed");+ },+ );++ it("a provider failure ahead of the error decides the marker and rides on it", () => {+ const failure = {+ kind: "usage_limit",+ message: "Limit reached.",+ retryable: false,+ };+ const log = fold([+ { type: "data-provider-failure", data: failure },+ { type: "error", errorText: "[code:usage_limit] raw" },+ ]);+ expect(log.rows[0]).toMatchObject({+ content: `${COPILOT_ERROR_PREFIX} Limit reached.`,+ metadata: { provider_failure: failure },+ });+ expect(log.overlay.map((p) => p.type)).toEqual(["data-provider-failure"]);+ });++ it("a second error does not stack a second marker", () => {+ const log = fold([+ { type: "error", errorText: "one" },+ { type: "error", errorText: "two" },+ ]);+ expect(log.rows).toHaveLength(1);+ });++ it("finish closes the turn and nothing else", () => {+ const log = fold([...call("c1"), { type: "finish" }]);+ expect(log.status).toBe("finished");+ expect(log.tools.c1.phase).toBe("input-available");+ expect(+ fold([{ type: "error", errorText: "x" }, { type: "finish" }]).status,+ ).toBe("failed");+ });++ it("data-tool-display names the call, before or after it arrives", () => {+ const display = (name: string): WireChunk => ({+ type: "data-tool-display",+ id: "c1",+ data: { toolCallId: "c1", displayName: name },+ });+ expect(+ fold([display("Early"), ...call("c1")]).rows[0].toolCalls[0].displayName,+ ).toBe("Early");+ expect(+ fold([...call("c1"), display("Late")]).rows[0].toolCalls[0].displayName,+ ).toBe("Late");+ });++ it("data-pending-drained appends user rows keyed by pending id, once each", () => {+ const drained: WireChunk = {+ type: "data-pending-drained",+ data: { drainedCount: 1, messages: [{ id: "p1", content: "and also" }] },+ };+ const log = fold([...text("a", "hi"), drained, drained]);+ expect(rowsOf(log).map((r) => r.role)).toEqual(["assistant", "user"]);+ expect(log.rows[1].key).toBe("user:p1");+ });++ it("other data parts go to the overlay, keyed by entry id", () => {+ const log = fold([+ { type: "data-status", data: { message: "Working…" } },+ { type: "data-compaction", data: { phase: "summarizing" } },+ ]);+ expect(log.overlay).toEqual([+ {+ entryId: "1-0",+ type: "data-status",+ data: { message: "Working…" },+ anchor: 0,+ },+ {+ entryId: "2-0",+ type: "data-compaction",+ data: { phase: "summarizing" },+ anchor: 0,+ },+ ]);+ expect(log.rows).toEqual([]);+ });++ it("data-checkpoint assigns sequences to the rows it covers and keeps the digest input", () => {+ const log = fold([+ ...text("a", "hi"),+ ...call("c1"),+ output("c1"),+ {+ type: "data-checkpoint",+ data: { rows: 2, sequence: 7, digest: "d" },+ transient: true,+ },+ ]);+ expect(log.rows.map((r) => r.sequence)).toEqual([7, 8]);+ expect(log.checkpoints).toEqual([+ {+ entryId: "7-0",+ rows: 2,+ sequence: 7,+ digest: "d",+ canonical: '[["assistant","hi",[["c1","bash_exec"]]],["tool","c1",[]]]',+ },+ ]);+ });++ it("an unknown chunk type is a protocol error", () => {+ expect(reasons(fold([{ type: "tool-input-delta" }]))).toEqual([+ "tool-input-delta: unknown chunk type",+ ]);+ });+});++describe("the cursor guard", () => {+ it("drops an entry at or before the cursor, so an overlapping replay applies nothing twice", () => {+ const entries = [...text("a", "hello"), ...call("c1"), output("c1")].map(+ (chunk, i) => ({ turn: TURN, entryId: `${i + 1}-0`, chunk }),+ );+ const once = foldEntries(emptyTurnLog(), entries);+ const replayed = foldEntries(once, entries);+ expect(replayed).toBe(once);+ expect(rowsOf(replayed)).toEqual([+ { role: "assistant", content: "hello", calls: ["c1"] },+ { role: "tool", content: "ok", calls: [], toolCallId: "c1" },+ ]);+ });++ it("orders ids numerically on both halves", () => {+ const log = foldEntries(emptyTurnLog(), [+ { turn: TURN, entryId: "10-2", chunk: { type: "text-start", id: "a" } },+ {+ turn: TURN,+ entryId: "10-10",+ chunk: { type: "text-delta", id: "a", delta: "x" },+ },+ {+ turn: TURN,+ entryId: "9-99",+ chunk: { type: "text-delta", id: "a", delta: "dup" },+ },+ ]);+ expect(log.rows[0].content).toBe("x");+ });++ it("an entry from another turn is a protocol error and moves nothing", () => {+ const log = fold(text("a", "x"));+ const other = applyEntry(log, {+ turn: "turn-2",+ entryId: "99-0",+ chunk: { type: "text-delta", id: "a", delta: "y" },+ });+ expect(reasons(other)).toEqual(["text-delta: another turn"]);+ expect(other.cursor).toBe(log.cursor);+ });+});++describe("the row rules", () => {+ it("after tool results, the next text or call opens a new assistant row", () => {+ const log = fold([+ ...text("a", "First."),+ ...call("c1"),+ output("c1"),+ ...call("c2"),+ output("c2"),+ ...text("b", "Then."),+ ]);+ expect(rowsOf(log)).toEqual([+ { role: "assistant", content: "First.", calls: ["c1"] },+ { role: "tool", content: "ok", calls: [], toolCallId: "c1" },+ { role: "assistant", content: "", calls: ["c2"] },+ { role: "tool", content: "ok", calls: [], toolCallId: "c2" },+ { role: "assistant", content: "Then.", calls: [] },+ ]);+ });++ it("text after a reasoning block with no tool result between extends the earlier assistant row", () => {+ const log = fold([+ ...text("a", "One. "),+ { type: "reasoning-start", id: "r" },+ { type: "reasoning-delta", id: "r", delta: "hmm" },+ { type: "reasoning-end", id: "r" },+ ...text("b", "Two."),+ ]);+ expect(rowsOf(log)).toEqual([+ { role: "assistant", content: "One. Two.", calls: [] },+ { role: "reasoning", content: "hmm", calls: [] },+ ]);+ });++ it("seeding from persisted rows at a checkpoint continues where the stream does", () => {+ const seeded = seedTurnLog({+ turnId: TURN,+ rows: [+ { role: "user", content: "prompt", sequence: 0 },+ {+ role: "assistant",+ content: "Looking.",+ sequence: 1,+ tool_calls: [+ { id: "c1", function: { name: "bash_exec", arguments: "{}" } },+ ],+ },+ { role: "tool", content: "ok", tool_call_id: "c1", sequence: 2 },+ ],+ checkpoint: { entry_id: "5-0", rows: 2, sequence: 1 },+ });+ expect(seeded.hasToolResults).toBe(true);+ const log = fold(text("b", "Done."), seeded);+ expect(rowsOf(log)).toEqual([+ { role: "assistant", content: "Looking.", calls: ["c1"] },+ { role: "tool", content: "ok", calls: [], toolCallId: "c1" },+ { role: "assistant", content: "Done.", calls: [] },+ ]);+ expect(log.rows.map((r) => r.sequence)).toEqual([1, 2, null]);+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/turnShadow.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/turnShadow.test.tsnew file mode 100644index 0000000000..7a4ef447cc--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/__tests__/turnShadow.test.ts@@ -0,0 +1,219 @@+import * as Sentry from "@sentry/nextjs";+import { beforeEach, describe, expect, it, vi } from "vitest";++import {+ compareShadowWithSession,+ createShadowFetch,+ getShadowLog,+ resetShadows,+ setStreamShadowRate,+} from "../turnShadow";+import { loadRecordedTurn } from "./recordedTurns";++vi.mock("@sentry/nextjs", () => ({ captureMessage: vi.fn() }));++const SESSION = "s1";+const turn = loadRecordedTurn("baseline-tool-turn");+const body = turn.sse.join("") + "data: [DONE]\n\n";+// The turn up to the first text block's second delta, as a server cut leaves it.+const cutBody = turn.sse.slice(0, 7).join("");++beforeEach(() => {+ resetShadows();+ setStreamShadowRate(1);+ vi.mocked(Sentry.captureMessage).mockClear();+});++async function stream(sse: string) {+ const shadowFetch = createShadowFetch(SESSION, async () => new Response(sse));+ const response = await shadowFetch("http://x/stream", { method: "POST" });+ const text = await response.text();+ await vi.waitFor(() =>+ expect(getShadowLog(SESSION)?.status).toBe("finished"),+ );+ return text;+}++function driftKinds() {+ return vi+ .mocked(Sentry.captureMessage)+ .mock.calls.map(+ ([, hint]) =>+ (hint as { tags: { stream_drift: string } }).tags.stream_drift,+ );+}++describe("the stream shadow", () => {+ it("hands the chat the stream untouched and reports nothing for a turn that matches", async () => {+ expect(await stream(body)).toBe(body);+ compareShadowWithSession(SESSION, {+ messages: turn.rows,+ active_stream: null,+ });+ // The digest check is async; give it the time the tampered case needs.+ await new Promise((resolve) => setTimeout(resolve, 50));+ expect(getShadowLog(SESSION)?.rows).toHaveLength(3);+ expect(driftKinds()).toEqual([]);+ });++ it("reports a checkpoint whose digest the folded rows do not reproduce", async () => {+ const tampered = body.replace(/"digest": "[0-9a-f]+"/, '"digest": "0000"');+ expect(tampered).not.toBe(body);+ await stream(tampered);+ await vi.waitFor(() => expect(driftKinds()).toEqual(["checkpoint"]));+ });++ it("reports a finished turn whose persisted rows lack what streamed", async () => {+ await stream(body);+ const withoutReply = turn.rows.filter((row) => row.sequence !== 3);+ compareShadowWithSession(SESSION, {+ messages: withoutReply,+ active_stream: null,+ });+ expect(driftKinds()).toEqual(["finish"]);+ });++ it("does not spend the finish check on a view whose window misses the turn", async () => {+ await stream(body);+ const afterTurnStart = turn.rows.filter((row) => (row.sequence ?? 0) > 1);+ compareShadowWithSession(SESSION, {+ messages: afterTurnStart,+ active_stream: null,+ has_more_messages: true,+ });+ expect(driftKinds()).toEqual([]);+ const withoutReply = turn.rows.filter((row) => row.sequence !== 3);+ compareShadowWithSession(SESSION, {+ messages: withoutReply,+ active_stream: null,+ });+ expect(driftKinds()).toEqual(["finish"]);+ });++ it("keeps a finished turn's check when the next turn starts before the view arrives", async () => {+ await stream(body);+ const next = body.replaceAll("drift-turn:", "next-turn:");+ const nextTurn = createShadowFetch(SESSION, async () => new Response(next));+ await (await nextTurn("http://x/stream", {})).text();+ await vi.waitFor(() =>+ expect(getShadowLog(SESSION)?.turnId).toBe("next-turn"),+ );+ const withoutReply = turn.rows.filter((row) => row.sequence !== 3);+ compareShadowWithSession(SESSION, {+ messages: withoutReply,+ active_stream: { turn_id: "next-turn" },+ });+ expect(driftKinds()).toEqual(["finish"]);+ });++ it("keeps a shadow for the most recent sessions only", async () => {+ for (const id of ["s-1", "s-2", "s-3", "s-4", "s-5", "s-6"]) {+ const shadowFetch = createShadowFetch(id, async () => new Response(body));+ await (await shadowFetch("http://x/stream", {})).text();+ await vi.waitFor(() => expect(getShadowLog(id)?.status).toBe("finished"));+ }+ expect(getShadowLog("s-1")).toBeNull();+ expect(getShadowLog("s-6")?.status).toBe("finished");+ });++ it("leaves a session the flag does not sample untouched", async () => {+ setStreamShadowRate(0);+ const original = new Response(body);+ const shadowFetch = createShadowFetch(SESSION, async () => original);+ expect(await shadowFetch("http://x/stream", {})).toBe(original);+ expect(getShadowLog(SESSION)).toBeNull();+ });++ it("reports a turn the server finished while its stream ended before finish", async () => {+ const cut = createShadowFetch(SESSION, async () => new Response(cutBody));+ await (await cut("http://x/stream", {})).text();+ await vi.waitFor(() => expect(getShadowLog(SESSION)?.rows).toHaveLength(1));+ compareShadowWithSession(SESSION, {+ messages: turn.rows,+ active_stream: { turn_id: "drift-turn" },+ });+ expect(driftKinds()).toEqual([]);+ compareShadowWithSession(SESSION, {+ messages: turn.rows,+ active_stream: null,+ });+ expect(driftKinds()).toEqual(["abandoned"]);+ });++ it("reports a turn the client stopped as stopped", async () => {+ const encoder = new TextEncoder();+ const source = new ReadableStream<Uint8Array>({+ start(controller) {+ controller.enqueue(encoder.encode(cutBody));+ },+ });+ const abort = new AbortController();+ const shadowFetch = createShadowFetch(+ SESSION,+ async () => new Response(source),+ );+ await shadowFetch("http://x/stream", { signal: abort.signal });+ await vi.waitFor(() => expect(getShadowLog(SESSION)?.rows).toHaveLength(1));+ abort.abort();+ await vi.waitFor(() => {+ compareShadowWithSession(SESSION, { messages: [], active_stream: null });+ expect(driftKinds()).toEqual(["stopped"]);+ });+ });++ it("checks a cut turn as finished once a resume delivers its end", async () => {+ const cut = createShadowFetch(SESSION, async () => new Response(cutBody));+ await (await cut("http://x/stream", {})).text();+ await stream(body);+ compareShadowWithSession(SESSION, {+ messages: turn.rows,+ active_stream: null,+ });+ await new Promise((resolve) => setTimeout(resolve, 50));+ expect(driftKinds()).toEqual([]);+ });++ it("waits while the session view still runs the turn", async () => {+ await stream(body);+ compareShadowWithSession(SESSION, {+ messages: [],+ active_stream: { turn_id: "drift-turn" },+ });+ expect(driftKinds()).toEqual([]);+ });++ it("reports nothing for a replay that opens past the turn's start", async () => {+ const tail = turn.sse.slice(+ turn.sse.findIndex((f) => f.includes("tool-output-available")),+ );+ const shadowFetch = createShadowFetch(+ SESSION,+ async () => new Response(tail.join("")),+ );+ await (await shadowFetch("http://x/stream", {})).text();+ await vi.waitFor(() =>+ expect(getShadowLog(SESSION)?.status).toBe("finished"),+ );+ compareShadowWithSession(SESSION, {+ messages: turn.rows,+ active_stream: null,+ });+ expect(getShadowLog(SESSION)?.protocolErrors.length).toBeGreaterThan(0);+ expect(driftKinds()).toEqual([]);+ });++ it("stops reading when the chat cancels its branch, so the connection closes", async () => {+ const cancelled = vi.fn();+ const source = new ReadableStream<Uint8Array>({+ pull: () => new Promise(() => {}),+ cancel: cancelled,+ });+ const shadowFetch = createShadowFetch(+ SESSION,+ async () => new Response(source),+ );+ const response = await shadowFetch("http://x/stream", {});+ await response.body!.cancel();+ await vi.waitFor(() => expect(cancelled).toHaveBeenCalled());+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/sseClient.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/sseClient.tsnew file mode 100644index 0000000000..dc45bdbd05--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/sseClient.ts@@ -0,0 +1,87 @@+import { createParser } from "eventsource-parser";++import type { StreamEntry, WireChunk } from "./turnConverter";++/**+ * One SSE frame. An `entry` carries a stored entry and its id; a `synthetic`+ * data frame has no id because the route wrote it itself, so it ends this+ * connection and never the turn. Comments are surfaced because they are the+ * liveness signal: the route and the listener heartbeat through them.+ */+export type SseFrame =+ | { kind: "entry"; entry: StreamEntry }+ | { kind: "synthetic"; chunk: WireChunk }+ | { kind: "comment"; text: string }+ | { kind: "done" };++export type SseResult =+ | { ok: true }+ | { ok: false; status: number; body: unknown };++/** Open an SSE request and deliver its frames until the body ends. */+export async function fetchSse(+ url: string,+ init: RequestInit,+ onFrame: (frame: SseFrame) => void,+): Promise<SseResult> {+ const response = await fetch(url, init);+ if (!response.ok || !response.body) {+ const body = await response.json().catch(() => null);+ return { ok: false, status: response.status, body };+ }+ await readSseFrames(response.body, onFrame, init.signal ?? undefined);+ return { ok: true };+}++/** Parse an SSE byte stream into frames; resolves when the stream ends. */+export async function readSseFrames(+ body: ReadableStream<Uint8Array>,+ onFrame: (frame: SseFrame) => void,+ signal?: AbortSignal,+): Promise<void> {+ const parser = createParser({+ onEvent(event) {+ const frame = toFrame(event.id, event.data);+ if (frame) onFrame(frame);+ },+ onComment(comment) {+ onFrame({ kind: "comment", text: comment.trim() });+ },+ });+ const reader = body.getReader();+ const cancel = () => void reader.cancel().catch(() => {});+ signal?.addEventListener("abort", cancel);+ if (signal?.aborted) cancel();+ const decoder = new TextDecoder();+ try {+ while (true) {+ const { done, value } = await reader.read();+ if (done) break;+ parser.feed(decoder.decode(value, { stream: true }));+ }+ } finally {+ signal?.removeEventListener("abort", cancel);+ reader.releaseLock();+ }+}++/** `<turn>:<entry>`; the entry id never holds a colon, a turn id might. */+export function parseFrameId(id: string) {+ const split = id.lastIndexOf(":");+ if (split <= 0) return null;+ return { turn: id.slice(0, split), entryId: id.slice(split + 1) };+}++function toFrame(id: string | undefined, data: string): SseFrame | null {+ if (data === "[DONE]") return { kind: "done" };+ let chunk: WireChunk;+ try {+ chunk = JSON.parse(data) as WireChunk;+ } catch {+ return null;+ }+ if (!chunk || typeof chunk.type !== "string") return null;+ const parsed = id ? parseFrameId(id) : null;+ if (!parsed) return { kind: "synthetic", chunk };+ return { kind: "entry", entry: { ...parsed, chunk } };+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnConverter.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnConverter.tsnew file mode 100644index 0000000000..fe7c5d4976--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnConverter.ts@@ -0,0 +1,319 @@+import {+ addToolCall,+ addToolResult,+ appendDrainedUserRows,+ appendErrorMarker,+ appendReasoning,+ appendText,+ canonicalRows,+ openReasoning,+ setToolDisplayName,+ type TurnLog,+} from "./turnLog";++/** One JSON chunk as it arrives on the wire. */+export interface WireChunk {+ type: string;+ [field: string]: unknown;+}++/** An entry-bearing frame: its SSE id is `<turn>:<entry>`. */+export interface StreamEntry {+ turn: string;+ entryId: string;+ chunk: WireChunk;+}++/**+ * Apply one stored entry. Pure, keyed by part identity, and idempotent over+ * any suffix: an entry at or before the cursor is dropped before any rule+ * runs, so overlapping deliveries converge. A protocol error is recorded and+ * never stops the fold.+ */+export function applyEntry(log: TurnLog, entry: StreamEntry): TurnLog {+ if (log.turnId !== null && entry.turn !== log.turnId) {+ return protocolError(log, entry.entryId, entry.chunk.type, "another turn");+ }+ if (log.cursor !== null && compareEntryIds(entry.entryId, log.cursor) <= 0) {+ return log;+ }+ const advanced: TurnLog = {+ ...log,+ turnId: entry.turn,+ cursor: entry.entryId,+ status: log.status === "idle" ? "running" : log.status,+ };+ return applyChunk(advanced, entry.entryId, entry.chunk);+}++export function foldEntries(log: TurnLog, entries: Iterable<StreamEntry>) {+ let next = log;+ for (const entry of entries) next = applyEntry(next, entry);+ return next;+}++/** Redis stream ids are `<ms>-<seq>`, ordered numerically on both halves. */+export function compareEntryIds(a: string, b: string): number {+ const [aMs, aSeq] = a.split("-").map(Number);+ const [bMs, bSeq] = b.split("-").map(Number);+ return aMs !== bMs ? aMs - bMs : (aSeq ?? 0) - (bSeq ?? 0);+}++function applyChunk(log: TurnLog, entryId: string, chunk: WireChunk): TurnLog {+ switch (chunk.type) {+ case "start":+ return startMessage(log, str(chunk.messageId));+ case "finish":+ return {+ ...log,+ openStep: null,+ status: log.status === "failed" ? "failed" : "finished",+ };+ case "start-step":+ return { ...log, openStep: { rows: log.rows, overlay: log.overlay } };+ case "finish-step":+ return finishStep(log, entryId);+ case "text-start":+ case "reasoning-start":+ return startBlock(log, entryId, chunk);+ case "text-delta":+ case "reasoning-delta":+ return extendBlock(log, entryId, chunk);+ case "text-end":+ case "reasoning-end":+ return endBlock(log, entryId, chunk);+ case "tool-input-start":+ return startTool(log, chunk);+ case "tool-input-available":+ return receiveToolInput(log, entryId, chunk);+ case "tool-output-available":+ return receiveToolOutput(log, entryId, chunk);+ case "error":+ return appendErrorMarker(log, entryId, str(chunk.errorText));+ case "data-tool-display": {+ const data = record(chunk.data);+ return setToolDisplayName(+ log,+ str(data.toolCallId),+ str(data.displayName),+ );+ }+ case "data-pending-drained":+ return appendDrainedUserRows(log, drainedMessages(chunk.data));+ case "data-checkpoint":+ return recordCheckpoint(log, entryId, record(chunk.data));+ case "data-cursor":+ return log;+ case "data-provider-failure":+ return {+ ...addOverlay(log, entryId, chunk),+ providerFailure: record(chunk.data),+ };+ default:+ if (chunk.type.startsWith("data-"))+ return addOverlay(log, entryId, chunk);+ return protocolError(log, entryId, chunk.type, "unknown chunk type");+ }+}++// An auto-continue streams a second message into the same turn; the engine+// gives each message a fresh assistant row, as it does each call.+function startMessage(log: TurnLog, messageId: string): TurnLog {+ if (!messageId || messageId === log.messageId) return log;+ return { ...log, messageId, assistantRow: null, hasToolResults: false };+}++// The backend ends every open block before a step closes (#14762).+function finishStep(log: TurnLog, entryId: string): TurnLog {+ const open = Object.entries(log.blocks).filter(([, b]) => b.open);+ const closed = { ...log, openStep: null };+ if (open.length === 0) return closed;+ const blocks = { ...log.blocks };+ for (const [id, block] of open) blocks[id] = { ...block, open: false };+ return protocolError(+ { ...closed, blocks },+ entryId,+ "finish-step",+ "block open",+ );+}++function startBlock(log: TurnLog, entryId: string, chunk: WireChunk): TurnLog {+ const id = str(chunk.id);+ if (log.blocks[id]) return log;+ if (chunk.type === "text-start") {+ return {+ ...log,+ blocks: { ...log.blocks, [id]: { kind: "text", open: true, row: null } },+ };+ }+ const opened = openReasoning(log, id);+ return {+ ...opened,+ blocks: {+ ...log.blocks,+ [id]: { kind: "reasoning", open: true, row: log.rows.length },+ },+ };+}++function extendBlock(log: TurnLog, entryId: string, chunk: WireChunk): TurnLog {+ const id = str(chunk.id);+ const block = log.blocks[id];+ const kind = chunk.type === "text-delta" ? "text" : "reasoning";+ if (!block || block.kind !== kind) {+ return protocolError(log, entryId, chunk.type, "unknown block");+ }+ if (!block.open)+ return protocolError(log, entryId, chunk.type, "closed block");+ const delta = str(chunk.delta);+ if (kind === "reasoning" && block.row !== null) {+ return appendReasoning(log, block.row, delta);+ }+ const written = appendText(log, id, delta);+ return {+ ...written,+ blocks: { ...log.blocks, [id]: { ...block, row: written.assistantRow } },+ };+}++function endBlock(log: TurnLog, entryId: string, chunk: WireChunk): TurnLog {+ const id = str(chunk.id);+ const block = log.blocks[id];+ if (!block) return protocolError(log, entryId, chunk.type, "unknown block");+ if (!block.open) return log;+ return { ...log, blocks: { ...log.blocks, [id]: { ...block, open: false } } };+}++function startTool(log: TurnLog, chunk: WireChunk): TurnLog {+ const id = str(chunk.toolCallId);+ if (log.tools[id]) return log;+ return {+ ...log,+ tools: {+ ...log.tools,+ [id]: { name: str(chunk.toolName), phase: "input-streaming" },+ },+ };+}++function receiveToolInput(+ log: TurnLog,+ entryId: string,+ chunk: WireChunk,+): TurnLog {+ const id = str(chunk.toolCallId);+ const input = chunk.input ?? {};+ const known = log.tools[id];+ if (known && known.phase !== "input-streaming") {+ const call = log.rows.flatMap((r) => r.toolCalls).find((c) => c.id === id);+ return call && !jsonEqual(call.input, input)+ ? protocolError(log, entryId, chunk.type, "input changed")+ : log;+ }+ const name = str(chunk.toolName) || known?.name || "";+ return {+ ...addToolCall(log, { id, name, input }),+ tools: { ...log.tools, [id]: { name, phase: "input-available" } },+ };+}++// An output for a call the log never saw is still a row the backend persists,+// so it is kept as well as reported.+function receiveToolOutput(+ log: TurnLog,+ entryId: string,+ chunk: WireChunk,+): TurnLog {+ const id = str(chunk.toolCallId);+ const known = log.tools[id];+ if (known?.phase === "output-available") return log;+ const withRow = {+ ...addToolResult(log, id, chunk.output),+ tools: {+ ...log.tools,+ [id]: { name: known?.name ?? "", phase: "output-available" as const },+ },+ };+ return known?.phase === "input-available"+ ? withRow+ : protocolError(withRow, entryId, chunk.type, "unknown tool call");+}++function recordCheckpoint(+ log: TurnLog,+ entryId: string,+ data: Record<string, unknown>,+): TurnLog {+ const count = num(data.rows);+ const sequence = num(data.sequence);+ const rows = log.rows.map((row, i) =>+ i < count && row.sequence !== sequence + i+ ? { ...row, sequence: sequence + i }+ : row,+ );+ const checkpoint = {+ entryId,+ rows: count,+ sequence,+ digest: str(data.digest),+ canonical: canonicalRows(rows.slice(0, count)),+ };+ return { ...log, rows, checkpoints: [...log.checkpoints, checkpoint] };+}++function addOverlay(log: TurnLog, entryId: string, chunk: WireChunk): TurnLog {+ const part = {+ entryId,+ type: chunk.type,+ data: chunk.data,+ anchor: log.rows.length,+ };+ return { ...log, overlay: [...log.overlay, part] };+}++function protocolError(+ log: TurnLog,+ entryId: string,+ chunkType: string,+ reason: string,+): TurnLog {+ return {+ ...log,+ protocolErrors: [...log.protocolErrors, { entryId, chunkType, reason }],+ };+}++function drainedMessages(data: unknown) {+ const messages = record(data).messages;+ return (Array.isArray(messages) ? messages : []).map((m) => ({+ id: str(record(m).id),+ content: str(record(m).content),+ }));+}++export function jsonEqual(a: unknown, b: unknown): boolean {+ if (a === b) return true;+ if (typeof a !== "object" || typeof b !== "object" || !a || !b) return false;+ if (Array.isArray(a) !== Array.isArray(b)) return false;+ const aKeys = Object.keys(a);+ const bRecord = b as Record<string, unknown>;+ return (+ aKeys.length === Object.keys(b).length &&+ aKeys.every((k) => jsonEqual((a as Record<string, unknown>)[k], bRecord[k]))+ );+}++function record(value: unknown): Record<string, unknown> {+ return value && typeof value === "object"+ ? (value as Record<string, unknown>)+ : {};+}++function str(value: unknown): string {+ return typeof value === "string" ? value : "";+}++function num(value: unknown): number {+ return typeof value === "number" && Number.isFinite(value) ? value : 0;+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnLog.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnLog.tsnew file mode 100644index 0000000000..0a38f09ff3--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnLog.ts@@ -0,0 +1,405 @@+/**+ * The client's model of one running turn: the rows the backend persists for+ * it, plus what rows cannot hold (open blocks, pending tools, transient parts).+ *+ * The row rules mirror `_dispatch_response` in `backend/copilot/sdk/service.py`+ * and `persist_pending_as_user_rows`; a change there that is not mirrored here+ * turns the replay-equivalence test red.+ */++export type RowRole = "assistant" | "reasoning" | "tool" | "user";++export interface LogToolCall {+ id: string;+ name: string;+ input: unknown;+ displayName: string | null;+}++export interface LogRow {+ /** Stable for the life of the row: a block, tool call or pending message id. */+ key: string;+ role: RowRole;+ content: string;+ toolCalls: readonly LogToolCall[];+ toolCallId: string | null;+ sequence: number | null;+ metadata: Record<string, unknown> | null;+}++export interface BlockState {+ kind: "text" | "reasoning";+ open: boolean;+ /** The row this block writes into; a text block has none until its first delta. */+ row: number | null;+}++export interface ToolState {+ name: string;+ phase: "input-streaming" | "input-available" | "output-available";+}++export interface OverlayPart {+ entryId: string;+ type: string;+ data: unknown;+ /** How many rows existed when it arrived; it renders after the last of them. */+ anchor: number;+}++export interface Checkpoint {+ entryId: string;+ rows: number;+ sequence: number;+ digest: string;+ /** The digest input over this log's rows at the moment the checkpoint applied. */+ canonical: string;+}++export interface ProtocolError {+ entryId: string;+ chunkType: string;+ reason: string;+}++export interface TurnLog {+ turnId: string | null;+ /** The last applied entry id; null until one is applied (a cursor of `0-0`). */+ cursor: string | null;+ status: "idle" | "running" | "finished" | "failed";+ messageId: string | null;+ rows: readonly LogRow[];+ blocks: Readonly<Record<string, BlockState>>;+ tools: Readonly<Record<string, ToolState>>;+ displayNames: Readonly<Record<string, string>>;+ /** The engine's current assistant row, once appended. */+ assistantRow: number | null;+ /** A tool result landed since the current assistant row opened. */+ hasToolResults: boolean;+ overlay: readonly OverlayPart[];+ /** The rows and overlay as the open step found them; null when no step is open. */+ openStep: { rows: readonly LogRow[]; overlay: readonly OverlayPart[] } | null;+ providerFailure: Record<string, unknown> | null;+ checkpoints: readonly Checkpoint[];+ protocolErrors: readonly ProtocolError[];+}++// Kept in step with `backend/copilot/constants.py`.+export const COPILOT_ERROR_PREFIX = "[__COPILOT_ERROR_f7a1__]";+export const COPILOT_RETRYABLE_ERROR_PREFIX =+ "[__COPILOT_RETRYABLE_ERROR_a9c2__]";+const COPILOT_SYSTEM_PREFIX = "[__COPILOT_SYSTEM_e3b0__]";+// SDK `_RETRYABLE_STREAM_ERROR_CODES`, plus the baseline's default-retryable code.+const RETRYABLE_ERROR_CODES = new Set([+ "transient_api_error",+ "empty_completion",+ "baseline_error",+]);+const CODE_PREFIX_RE = /^\s*\[code:([^\]]*)\] ?/;++export function emptyTurnLog(): TurnLog {+ return {+ turnId: null,+ cursor: null,+ status: "idle",+ messageId: null,+ rows: [],+ blocks: {},+ tools: {},+ displayNames: {},+ assistantRow: null,+ hasToolResults: false,+ overlay: [],+ openStep: null,+ providerFailure: null,+ checkpoints: [],+ protocolErrors: [],+ };+}++/** Row rule 1: a text delta extends the assistant row, or opens one after tool results. */+export function appendText(log: TurnLog, blockId: string, delta: string) {+ if (log.hasToolResults && log.assistantRow !== null) {+ return {+ ...pushRow(log, assistantRow(`assistant:${blockId}`, delta)),+ assistantRow: log.rows.length,+ hasToolResults: false,+ };+ }+ if (log.assistantRow === null) {+ return {+ ...pushRow(log, assistantRow(`assistant:${blockId}`, delta)),+ assistantRow: log.rows.length,+ };+ }+ const row = log.rows[log.assistantRow];+ return patchRow(log, log.assistantRow, { content: row.content + delta });+}++/** Row rule 2: every reasoning block is its own row, appended at its start. */+export function openReasoning(log: TurnLog, blockId: string) {+ return pushRow(log, {+ ...assistantRow(`reasoning:${blockId}`, ""),+ role: "reasoning",+ });+}++/** Row rule 3: a reasoning delta extends its block's row. */+export function appendReasoning(log: TurnLog, row: number, delta: string) {+ return patchRow(log, row, { content: log.rows[row].content + delta });+}++/** Row rule 4: a tool call joins the current assistant row, or opens one after tool results. */+export function addToolCall(+ log: TurnLog,+ call: Omit<LogToolCall, "displayName">,+) {+ const toolCall = { ...call, displayName: log.displayNames[call.id] ?? null };+ if (log.assistantRow === null || log.hasToolResults) {+ return {+ ...pushRow(log, {+ ...assistantRow(`assistant:${call.id}`, ""),+ toolCalls: [toolCall],+ }),+ assistantRow: log.rows.length,+ hasToolResults: false,+ };+ }+ const row = log.rows[log.assistantRow];+ return patchRow(log, log.assistantRow, {+ toolCalls: [...row.toolCalls, toolCall],+ });+}++/** Row rule 5: a tool result is its own row, once per call. */+export function addToolResult(+ log: TurnLog,+ toolCallId: string,+ output: unknown,+) {+ if (log.rows.some((r) => r.role === "tool" && r.toolCallId === toolCallId)) {+ return log;+ }+ return {+ ...pushRow(log, {+ ...assistantRow(`tool:${toolCallId}`, toolContent(output)),+ role: "tool",+ toolCallId,+ }),+ hasToolResults: true,+ };+}++/** Row rule 6: an error becomes a marker row, unless one already ends the turn. */+export function appendErrorMarker(+ log: TurnLog,+ entryId: string,+ errorText: string,+) {+ const failure = log.providerFailure;+ const cleared = { ...log, providerFailure: null, status: "failed" as const };+ const last = log.rows[log.rows.length - 1];+ if (last && isMarker(last)) return cleared;+ const code = CODE_PREFIX_RE.exec(errorText)?.[1] ?? null;+ const display =+ typeof failure?.message === "string"+ ? failure.message+ : errorText.replace(CODE_PREFIX_RE, "");+ const retryable = failure+ ? failure.retryable === true+ : code !== null && RETRYABLE_ERROR_CODES.has(code);+ const prefix = retryable+ ? COPILOT_RETRYABLE_ERROR_PREFIX+ : COPILOT_ERROR_PREFIX;+ return pushRow(cleared, {+ ...assistantRow(`error:${entryId}`, `${prefix} ${display}`),+ metadata: failure ? { provider_failure: failure } : null,+ });+}++/** Row rule 7: a display name lands on its tool call, now or when the call arrives. */+export function setToolDisplayName(+ log: TurnLog,+ toolCallId: string,+ name: string,+) {+ const displayNames = { ...log.displayNames, [toolCallId]: name };+ const index = log.rows.findIndex((r) =>+ r.toolCalls.some((c) => c.id === toolCallId),+ );+ if (index === -1) return { ...log, displayNames };+ const toolCalls = log.rows[index].toolCalls.map((c) =>+ c.id === toolCallId ? { ...c, displayName: name } : c,+ );+ return { ...patchRow(log, index, { toolCalls }), displayNames };+}++/**+ * Row rule 8: drained follow-ups are user rows, keyed by pending message id,+ * at the hint's place in the stream.+ */+export function appendDrainedUserRows(+ log: TurnLog,+ messages: readonly { id: string; content: string }[],+) {+ const known = new Set(log.rows.map((r) => r.key));+ const rows = messages+ .map((m) => ({+ ...assistantRow(`user:${m.id}`, m.content),+ role: "user" as const,+ }))+ .filter((row) => !known.has(row.key));+ return rows.reduce(pushRow, log);+}++/**+ * The inverse of the row rules at a checkpoint: the persisted rows of a turn+ * seed a log that the stream tail after the checkpoint continues. At a+ * checkpoint no block is open and no tool call is pending.+ */+export function seedTurnLog({+ turnId,+ rows,+ checkpoint,+}: {+ turnId: string;+ rows: readonly PersistedRow[];+ checkpoint: { entry_id: string; rows: number; sequence: number } | null;+}): TurnLog {+ const base = { ...emptyTurnLog(), turnId, status: "running" as const };+ if (!checkpoint) return base;+ const { sequence, rows: count } = checkpoint;+ const seeded = rows+ .filter(+ (r) =>+ typeof r.sequence === "number" &&+ r.sequence >= sequence &&+ r.sequence < sequence + count,+ )+ .sort((a, b) => (a.sequence ?? 0) - (b.sequence ?? 0))+ .map(logRowFromPersisted);+ const lastAssistant = seeded.findLastIndex(+ (r) => r.role === "assistant" && !isMarker(r),+ );+ const tools: Record<string, ToolState> = {};+ const answered = new Set(seeded.map((r) => r.toolCallId));+ for (const call of seeded.flatMap((r) => r.toolCalls)) {+ tools[call.id] = {+ name: call.name,+ phase: answered.has(call.id) ? "output-available" : "input-available",+ };+ }+ return {+ ...base,+ cursor: checkpoint.entry_id,+ rows: seeded,+ tools,+ assistantRow: lastAssistant === -1 ? null : lastAssistant,+ hasToolResults:+ lastAssistant !== -1 &&+ seeded.slice(lastAssistant + 1).some((r) => r.role === "tool"),+ };+}++/** The checkpoint digest's input, as `stream_checkpoint.rows_digest` builds it. */+export function canonicalRows(rows: readonly LogRow[]): string {+ return JSON.stringify(+ rows.map((row) => [+ row.role,+ (row.role === "tool" ? row.toolCallId : row.content) || "",+ row.toolCalls.map((call) => [call.id, call.name]),+ ]),+ );+}++export function isMarker(row: LogRow): boolean {+ return (+ row.role === "assistant" &&+ [+ COPILOT_ERROR_PREFIX,+ COPILOT_RETRYABLE_ERROR_PREFIX,+ COPILOT_SYSTEM_PREFIX,+ ].some((prefix) => row.content.startsWith(prefix))+ );+}++/** A row as `GET /sessions/{id}` returns it. */+export interface PersistedRow {+ role: string;+ content?: string | null;+ tool_call_id?: string | null;+ tool_calls?: unknown[] | null;+ sequence?: number | null;+ metadata?: Record<string, unknown> | null;+}++export function logRowFromPersisted(row: PersistedRow): LogRow {+ const role = (["assistant", "reasoning", "tool", "user"] as const).find(+ (r) => r === row.role,+ );+ return {+ key: `seq:${row.sequence}`,+ role: role ?? "assistant",+ content: row.content ?? "",+ toolCalls: (row.tool_calls ?? []).map(persistedToolCall),+ toolCallId: row.tool_call_id ?? null,+ sequence: row.sequence ?? null,+ metadata: row.metadata ?? null,+ };+}++export function parseJsonLoose(value: string): unknown {+ try {+ return JSON.parse(value) as unknown;+ } catch {+ return value;+ }+}++function persistedToolCall(raw: unknown): LogToolCall {+ const call = (raw ?? {}) as {+ id?: unknown;+ display_name?: unknown;+ function?: { name?: unknown; arguments?: unknown };+ };+ const args = call.function?.arguments;+ return {+ id: String(call.id ?? ""),+ name: String(call.function?.name ?? ""),+ input: typeof args === "string" ? parseJsonLoose(args) : (args ?? {}),+ displayName:+ typeof call.display_name === "string" ? call.display_name : null,+ };+}++function assistantRow(key: string, content: string): LogRow {+ return {+ key,+ role: "assistant",+ content,+ toolCalls: [],+ toolCallId: null,+ sequence: null,+ metadata: null,+ };+}++// The backend stores a dict output as `json.dumps`, whose spacing differs;+// only the digest-free finish comparison reads it, and it parses both sides.+function toolContent(output: unknown): string {+ return typeof output === "string" ? output : JSON.stringify(output);+}++function pushRow(log: TurnLog, row: LogRow): TurnLog {+ return { ...log, rows: [...log.rows, row] };+}++function patchRow(+ log: TurnLog,+ index: number,+ patch: Partial<LogRow>,+): TurnLog {+ const rows = log.rows.slice();+ rows[index] = { ...rows[index], ...patch };+ return { ...log, rows };+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnShadow.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnShadow.tsnew file mode 100644index 0000000000..166836caa3--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/stream/turnShadow.ts@@ -0,0 +1,324 @@+import * as Sentry from "@sentry/nextjs";++import { readSseFrames } from "./sseClient";+import { applyEntry, jsonEqual, type StreamEntry } from "./turnConverter";+import {+ emptyTurnLog,+ logRowFromPersisted,+ parseJsonLoose,+ type LogRow,+ type PersistedRow,+ type TurnLog,+} from "./turnLog";++/**+ * Shadow mode: the stream the AI SDK renders is also folded by our converter,+ * which is checked against the server at every checkpoint and against the+ * persisted rows once the turn is over. It renders nothing and reports drift+ * to Sentry; any failure in it degrades to silence.+ */+interface ShadowTurn {+ log: TurnLog;+ verified: number;+ reportedErrors: number;+ compared: boolean;+ /** Its stream ended before `finish`: closed from outside, or cancelled by the client. */+ ended: "abandoned" | "stopped" | null;+}++/** A finished turn keeps its slot until compared, so the next turn's entries+ * cannot take it before the session view that checks it arrives. */+interface SessionShadow {+ current: ShadowTurn;+ previous: ShadowTurn | null;+}++// Each slot holds a whole turn's rows; only the most recent sessions keep one.+const MAX_SESSIONS = 5;+const shadows = new Map<string, SessionShadow>();+let shadowRate = 0;+const sampledSessions = new Map<string, boolean>();++/** The share of chat sessions the shadow tees, from `copilot-stream-shadow`; 0 is off. */+export function setStreamShadowRate(rate: unknown) {+ shadowRate =+ typeof rate === "number" && Number.isFinite(rate)+ ? Math.min(1, Math.max(0, rate))+ : 0;+}++/** Wrap the transport's fetch so a sampled session's stream responses are teed into the shadow. */+export function createShadowFetch(+ sessionId: string,+ inner: typeof fetch = (input, init) => fetch(input, init),+): typeof fetch {+ return async (input, init) => {+ const response = await inner(input, init);+ if (!response.ok || !response.body || !isSampled(sessionId)) {+ return response;+ }+ const [main, tap] = response.body.tee();+ const stopTap = new AbortController();+ const stop = () => stopTap.abort();+ init?.signal?.addEventListener("abort", stop);+ let turnId: string | null = null;+ void readSseFrames(+ tap,+ (frame) => {+ if (frame.kind !== "entry") return;+ turnId = frame.entry.turn;+ applyShadowEntry(sessionId, frame.entry);+ },+ stopTap.signal,+ )+ .catch(() => {})+ .finally(() => {+ init?.signal?.removeEventListener("abort", stop);+ markEnded(sessionId, turnId, stopTap.signal.aborted);+ });+ return new Response(+ onCancel(main, () => stopTap.abort()),+ {+ status: response.status,+ statusText: response.statusText,+ headers: response.headers,+ },+ );+ };+}++export function applyShadowEntry(sessionId: string, entry: StreamEntry) {+ try {+ const shadow = shadowFor(sessionId, entry.turn);+ shadow.log = applyEntry(shadow.log, entry);+ shadow.ended = null;+ if (!isWholeTurn(shadow.log)) return;+ reportProtocolErrors(shadow);+ void verifyCheckpoints(shadow);+ } catch {+ // The shadow must never break the chat it watches.+ }+}++/** Once the DB view no longer runs a shadowed finished turn, diff its rows. */+export function compareShadowWithSession(+ sessionId: string,+ session: SessionView,+) {+ const slot = shadows.get(sessionId);+ if (!slot) return;+ if (slot.previous) compareTurn(slot.previous, session);+ if (slot.previous?.compared) slot.previous = null;+ compareTurn(slot.current, session);+}++export function getShadowLog(sessionId: string): TurnLog | null {+ return shadows.get(sessionId)?.current.log ?? null;+}++export function resetShadows() {+ shadows.clear();+ sampledSessions.clear();+ shadowRate = 0;+}++/** One entry per differing row, with the fields that differ. */+export function diffRows(+ live: readonly LogRow[],+ persisted: readonly LogRow[],+) {+ const diffs: { index: number; fields: string[] }[] = [];+ for (let i = 0; i < Math.max(live.length, persisted.length); i++) {+ const fields = rowDifferences(live[i], persisted[i]);+ if (fields.length > 0) diffs.push({ index: i, fields });+ }+ return diffs;+}++interface SessionView {+ messages?: readonly unknown[] | null;+ active_stream?: { turn_id: string } | null;+ has_more_messages?: boolean;+}++// Sampled per session, not per request, so a turn's resumes are teed when its POST was.+function isSampled(sessionId: string) {+ if (shadowRate <= 0) return false;+ let sampled = sampledSessions.get(sessionId);+ if (sampled === undefined) {+ sampled = Math.random() < shadowRate;+ sampledSessions.set(sessionId, sampled);+ }+ return sampled;+}++function markEnded(+ sessionId: string,+ turnId: string | null,+ byClient: boolean,+) {+ const slot = shadows.get(sessionId);+ const shadow = [slot?.current, slot?.previous].find(+ (s) => s && turnId !== null && s.log.turnId === turnId,+ );+ const over =+ shadow?.log.status === "finished" || shadow?.log.status === "failed";+ if (!shadow || over) return;+ shadow.ended = byClient ? "stopped" : "abandoned";+}++function shadowFor(sessionId: string, turnId: string): ShadowTurn {+ const slot = shadows.get(sessionId);+ if (slot?.current.log.turnId === turnId) return slot.current;+ if (slot?.previous?.log.turnId === turnId) return slot.previous;+ const current = {+ log: emptyTurnLog(),+ verified: 0,+ reportedErrors: 0,+ compared: false,+ ended: null,+ };+ const pending = slot && !slot.current.compared ? slot.current : null;+ shadows.delete(sessionId);+ shadows.set(sessionId, {+ current,+ previous: pending ?? slot?.previous ?? null,+ });+ const oldest = shadows.keys().next().value;+ if (shadows.size > MAX_SESSIONS && oldest !== undefined) {+ shadows.delete(oldest);+ }+ return current;+}++function compareTurn(shadow: ShadowTurn, session: SessionView) {+ if (shadow.compared || !isWholeTurn(shadow.log)) return;+ const { log } = shadow;+ if (session.active_stream?.turn_id === log.turnId) return;+ if (log.status !== "finished" && log.status !== "failed") {+ // The server is done with a turn whose stream the client lost: what the+ // screen shows stopped where the stream did.+ if (!shadow.ended) return;+ shadow.compared = true;+ reportDrift(shadow.ended, log, {+ rows: log.rows.length,+ checkpoints: log.checkpoints.length,+ });+ return;+ }+ const last = log.checkpoints[log.checkpoints.length - 1];+ if (!last) {+ shadow.compared = true;+ reportDrift("finish_without_checkpoint", log, { rows: log.rows.length });+ return;+ }+ const numbered = (session.messages ?? [])+ .map((row) => row as PersistedRow)+ .filter((row) => typeof row.sequence === "number")+ .sort((a, b) => (a.sequence ?? 0) - (b.sequence ?? 0));+ // The session GET returns a window; a turn that starts before it is not checked.+ const windowStart = numbered[0]?.sequence ?? Infinity;+ if (session.has_more_messages !== false && windowStart > last.sequence)+ return;+ shadow.compared = true;+ const turnRows = numbered+ .filter((row) => (row.sequence ?? -1) >= last.sequence)+ .slice(0, last.rows)+ .map(logRowFromPersisted);+ const diffs = diffRows(log.rows, turnRows);+ if (diffs.length > 0) reportDrift("finish", log, { diffs });+}++// A replay that opens past the turn's `start` (a trimmed stream) is a tail,+// not a turn; comparing it with the whole turn would report drift that is not.+function isWholeTurn(log: TurnLog) {+ return log.messageId !== null;+}++function reportProtocolErrors(shadow: ShadowTurn) {+ const errors = shadow.log.protocolErrors.slice(shadow.reportedErrors);+ shadow.reportedErrors = shadow.log.protocolErrors.length;+ for (const error of errors) {+ reportDrift("protocol_error", shadow.log, { ...error });+ }+}++async function verifyCheckpoints(shadow: ShadowTurn) {+ while (shadow.verified < shadow.log.checkpoints.length) {+ const checkpoint = shadow.log.checkpoints[shadow.verified++];+ const digest = await sha256Hex(checkpoint.canonical);+ if (digest !== null && digest !== checkpoint.digest) {+ reportDrift("checkpoint", shadow.log, {+ entryId: checkpoint.entryId,+ rows: checkpoint.rows,+ serverDigest: checkpoint.digest,+ clientDigest: digest,+ });+ }+ }+}++function rowDifferences(a: LogRow | undefined, b: LogRow | undefined) {+ if (!a || !b) return ["missing"];+ const fields: string[] = [];+ if (a.role !== b.role) fields.push("role");+ if (a.role === "tool") {+ if (a.toolCallId !== b.toolCallId) fields.push("tool_call_id");+ if (!jsonEqual(parseJsonLoose(a.content), parseJsonLoose(b.content))) {+ fields.push("output");+ }+ } else if (a.content !== b.content) {+ fields.push("content");+ }+ const sameCalls =+ a.toolCalls.length === b.toolCalls.length &&+ a.toolCalls.every((call, i) => {+ const other = b.toolCalls[i];+ return (+ call.id === other.id &&+ call.name === other.name &&+ jsonEqual(call.input, other.input)+ );+ });+ if (!sameCalls) fields.push("tool_calls");+ return fields;+}++function reportDrift(+ kind: string,+ log: TurnLog,+ extra: Record<string, unknown>,+) {+ Sentry.captureMessage(`copilot stream drift: ${kind}`, {+ level: "warning",+ tags: { stream_drift: kind },+ extra: { turnId: log.turnId, cursor: log.cursor, ...extra },+ });+}++// `crypto.subtle` exists only in secure contexts; a plain-HTTP LAN origin skips the check.+async function sha256Hex(text: string): Promise<string | null> {+ const subtle = globalThis.crypto?.subtle;+ if (!subtle) return null;+ const hash = await subtle.digest("SHA-256", new TextEncoder().encode(text));+ return Array.from(new Uint8Array(hash), (b) =>+ b.toString(16).padStart(2, "0"),+ ).join("");+}++// A tee branch outlives its sibling: when the SDK cancels the body, the tap+// must stop too or it holds the connection open.+function onCancel(stream: ReadableStream<Uint8Array>, cancelled: () => void) {+ const reader = stream.getReader();+ return new ReadableStream<Uint8Array>({+ async pull(controller) {+ const { done, value } = await reader.read();+ if (done) controller.close();+ else controller.enqueue(value);+ },+ cancel(reason) {+ cancelled();+ return reader.cancel(reason);+ },+ });+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.tsindex bfbd83e993..13ac13ff0c 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.ts@@ -11,6 +11,7 @@ import { useCopilotUIStore } from "./store";import { toast } from "@/components/molecules/Toast/use-toast";import { ApiError } from "@/lib/autogpt-server-api/helpers";import { trackFunnel } from "@/services/experts/experts-analytics";+import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";import * as Sentry from "@sentry/nextjs";import { useQueryClient } from "@tanstack/react-query";import { parseAsString, useQueryState } from "nuqs";@@ -28,6 +29,10 @@ import {import { useAutopilotModeStore } from "./autopilotModeStore";import { useCopilotStreamStore } from "./copilotStreamStore";import { latestExpertSessionParams } from "./expertSessionQuery";+import {+ compareShadowWithSession,+ setStreamShadowRate,+} from "./stream/turnShadow";interface UseChatSessionOptions {dryRun?: boolean;@@ -171,6 +176,17 @@ export function useChatSession({? sessionQuery.data.data: null;+ const streamShadowRate = useGetFlag(Flag.COPILOT_STREAM_SHADOW);+ useEffect(() => {+ setStreamShadowRate(streamShadowRate);+ }, [streamShadowRate]);++ useEffect(() => {+ if (sessionId && freshSessionData) {+ compareShadowWithSession(sessionId, freshSessionData);+ }+ }, [sessionId, freshSessionData]);+// Expose active_stream info so the caller can trigger manual resume// after hydration completes (rather than relying on AI SDK's built-in// resume which fires before hydration).diff --git a/autogpt_platform/frontend/src/services/feature-flags/use-get-flag.ts b/autogpt_platform/frontend/src/services/feature-flags/use-get-flag.tsindex 0d9ce4a2b2..489676ebe8 100644--- a/autogpt_platform/frontend/src/services/feature-flags/use-get-flag.ts+++ b/autogpt_platform/frontend/src/services/feature-flags/use-get-flag.ts@@ -61,6 +61,9 @@ export enum Flag {// The chat's approval mode selector. Mirror of the backend ``Flag`` enum,// which ignores a sent mode when off. Fail-closed.COPILOT_AUTO_MODE = "copilot-auto-mode",+ // The share of chat sessions whose stream the converter shadows and checks+ // against the server, 0 to 1 (a PostHog payload). 0 is off.+ COPILOT_STREAM_SHADOW = "copilot-stream-shadow",}const isPwMockEnabled = process.env.NEXT_PUBLIC_PW_TEST === "true";@@ -95,6 +98,7 @@ const defaultFlags = {[Flag.COPILOT_BOT_PLATFORMS]: {} as Record<string, boolean>,[Flag.COPILOT_VOICE_MODE]: false,[Flag.COPILOT_AUTO_MODE]: false,+ [Flag.COPILOT_STREAM_SHADOW]: 0,};type FlagValues = typeof defaultFlags;@@ -163,6 +167,7 @@ function readEnvOverride(flag: Flag): string | undefined {case Flag.COPILOT_AUTO_MODE:return process.env.NEXT_PUBLIC_FORCE_FLAG_COPILOT_AUTO_MODE;case Flag.COPILOT_BOT_PLATFORMS:+ case Flag.COPILOT_STREAM_SHADOW:return undefined;}}@@ -175,6 +180,7 @@ function readEnvOverride(flag: Flag): string | undefined {const ARRAY_TYPED_FLAGS: ReadonlySet<Flag> = new Set([Flag.MARKETPLACE_SEARCH_TERMS,Flag.COPILOT_BOT_PLATFORMS,+ Flag.COPILOT_STREAM_SHADOW,]);// Master local-dev switch: ``NEXT_PUBLIC_FORCE_ALL_FLAGS=true`` turns every