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.
perf(frontend/builder): batch and dedupe builder execution updates (#15689)
Abhimanyu Yadava889331e8923
diff --git a/autogpt_platform/frontend/scripts/perf/scenarios/builder-run-events.ts b/autogpt_platform/frontend/scripts/perf/scenarios/builder-run-events.tsnew file mode 100644index 0000000000..f28fb3d5ba--- /dev/null+++ b/autogpt_platform/frontend/scripts/perf/scenarios/builder-run-events.ts@@ -0,0 +1,140 @@+import type { Page, WebSocketRoute } from "@playwright/test";+import { BuildPage } from "../../../src/playwright/pages/build.page";+import { createBuilderFixture } from "../lib/builder-fixture";+import { installRoutes } from "../lib/routes";+import { settle, type Scenario } from "../lib/scenario";++const FIRST_PENDING_NODE = 30;+const EVENT_INTERVAL_MS = 50;+const READY_TIMEOUT_MS = 60_000;+const FINISHED_AT = "2026-01-05T09:00:05.000Z";++type Execution = ReturnType<typeof createBuilderFixture>["execution"];+type NodeExecution = Execution["node_executions"][number];++/**+ * The 10 nodes still RUNNING or QUEUED go RUNNING, then COMPLETED. Every event+ * arrives twice, as it does when the poll and the websocket both deliver it.+ */+function nodeEvents(execution: Execution) {+ return execution.node_executions+ .slice(FIRST_PENDING_NODE)+ .flatMap((nodeExecution: NodeExecution) => {+ const running = {+ ...nodeExecution,+ status: "RUNNING",+ start_time: nodeExecution.queue_time,+ };+ const completed = {+ ...running,+ status: "COMPLETED",+ output_data: { output: [nodeExecution.node_exec_id] },+ end_time: FINISHED_AT,+ };+ return [running, running, completed, completed];+ });+}++/**+ * Passes the builder's websocket through to the real server, except the+ * subscribe for the fixture run, which the harness acknowledges itself (the+ * backend has no such run and would reject it).+ */+async function routeExecutionSocket(page: Page, executionID: string) {+ let socket: WebSocketRoute | null = null;+ let markSubscribed: () => void = () => undefined;+ const subscribed = new Promise<void>((resolve) => {+ markSubscribed = resolve;+ });+ await page.routeWebSocket(/\/ws(\?|$)/, (ws) => {+ const server = ws.connectToServer();+ socket = ws;+ ws.onMessage((message) => {+ const parsed = JSON.parse(String(message));+ if (+ parsed.method === "subscribe_graph_execution" &&+ parsed.data?.graph_exec_id === executionID+ ) {+ ws.send(+ JSON.stringify({+ method: "subscribe_graph_execution",+ success: true,+ channel: `perf|graph_exec#${executionID}`,+ }),+ );+ markSubscribed();+ return;+ }+ server.send(message);+ });+ });+ return {+ subscribed,+ send(method: string, data: unknown) {+ socket?.send(JSON.stringify({ method, data }));+ },+ };+}++async function withTimeout<T>(promise: Promise<T>, ms: number, what: string) {+ let timer: ReturnType<typeof setTimeout> | undefined;+ try {+ return await Promise.race([+ promise,+ new Promise<never>((_, reject) => {+ timer = setTimeout(+ () => reject(new Error(`Timed out waiting for ${what}`)),+ ms,+ );+ }),+ ]);+ } finally {+ clearTimeout(timer);+ }+}++export const builderRunEvents: Scenario = {+ name: "builder-run-events",+ description:+ "A 50-node workflow's RUNNING run receives 40 node execution events over the websocket (10 nodes go RUNNING then COMPLETED, every event twice), one every 50 ms.",+ readyWhen:+ "all 50 nodes are on the canvas with the running execution loaded and the websocket subscribe acknowledged, then network idle + 1 s",+ windowMs: 2_000,+ async setup(ctx) {+ const { page } = ctx;+ const fixture = createBuilderFixture(ctx.userID);+ await installRoutes(page, fixture.handlers);+ const socket = await routeExecutionSocket(page, fixture.execution.id);+ const builder = new BuildPage(page);+ await page.goto(+ `/build?flowID=${fixture.graph.id}&flowExecutionID=${fixture.execution.id}`,+ );+ await builder.closeTutorial();+ await page+ .locator('[data-id^="custom-node-"]')+ .nth(fixture.graph.nodes.length - 1)+ .waitFor({ state: "attached", timeout: READY_TIMEOUT_MS });+ await withTimeout(+ socket.subscribed,+ READY_TIMEOUT_MS,+ "websocket subscribe",+ );+ await settle(page);+ const { node_executions } = fixture.execution;+ const events = nodeEvents(fixture.execution);+ return {+ async run() {+ for (const event of events) {+ // The poll sees what the websocket has sent, as with the real backend.+ const index = node_executions.findIndex(+ (nodeExecution) =>+ nodeExecution.node_exec_id === event.node_exec_id,+ );+ node_executions[index] = event;+ socket.send("node_execution_event", event);+ await page.waitForTimeout(EVENT_INTERVAL_MS);+ }+ },+ };+ },+};diff --git a/autogpt_platform/frontend/scripts/perf/scenarios/registry.ts b/autogpt_platform/frontend/scripts/perf/scenarios/registry.tsindex cb6e8ad2e6..a8d66ad8e8 100644--- a/autogpt_platform/frontend/scripts/perf/scenarios/registry.ts+++ b/autogpt_platform/frontend/scripts/perf/scenarios/registry.ts@@ -1,6 +1,7 @@import type { Scenario } from "../lib/scenario";import { builderConnectEdge } from "./builder-connect-edge";import { builderDragRunning } from "./builder-drag-running";+import { builderRunEvents } from "./builder-run-events";import { copilotBackgroundStream } from "./copilot-background-stream";import { copilotIdle } from "./copilot-idle";import { copilotStreamReply } from "./copilot-stream-reply";@@ -19,6 +20,7 @@ export const SCENARIOS: Scenario[] = [libraryRunning,builderConnectEdge,libraryRunningEvents,+ builderRunEvents,];export function findScenario(name: string) {diff --git a/autogpt_platform/frontend/src/app/(platform)/build/__tests__/applyNodeExecutionEvent.test.ts b/autogpt_platform/frontend/src/app/(platform)/build/__tests__/applyNodeExecutionEvent.test.tsnew file mode 100644index 0000000000..718c565269--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/build/__tests__/applyNodeExecutionEvent.test.ts@@ -0,0 +1,218 @@+import { beforeEach, describe, expect, it } from "vitest";+import { useNodeStore } from "../stores/nodeStore";+import { useEdgeStore } from "../stores/edgeStore";+import { BlockUIType } from "../components/types";+import type { CustomNode } from "../components/FlowEditor/nodes/CustomNode/CustomNode";+import type { CustomEdge } from "../components/FlowEditor/edges/CustomEdge";+import type { NodeExecutionResult } from "@/app/api/__generated__/models/nodeExecutionResult";++function createNode(id: string): CustomNode {+ return {+ id,+ type: "custom",+ position: { x: 0, y: 0 },+ data: {+ hardcodedValues: {},+ title: "Store Value",+ description: "",+ inputSchema: {},+ outputSchema: {},+ uiType: BlockUIType.STANDARD,+ block_id: "block-1",+ costs: [],+ categories: [],+ },+ };+}++function createEdge(source: string, target: string): CustomEdge {+ return {+ id: `${source}->${target}`,+ source,+ target,+ sourceHandle: "output",+ targetHandle: "input",+ type: "custom",+ };+}++// A fresh object every call, with fresh Dates, like a poll response or a+// websocket message.+function createEvent(+ overrides: Partial<NodeExecutionResult> = {},+): NodeExecutionResult {+ return {+ user_id: "user-1",+ graph_id: "graph-1",+ graph_version: 1,+ graph_exec_id: "graph-exec-1",+ node_exec_id: "node-exec-1",+ node_id: "1",+ block_id: "block-1",+ status: "RUNNING",+ input_data: { input: "hello" },+ output_data: {},+ add_time: new Date("2026-01-05T09:00:00.000Z"),+ queue_time: new Date("2026-01-05T09:00:00.000Z"),+ start_time: new Date("2026-01-05T09:00:01.000Z"),+ end_time: null,+ ...overrides,+ };+}++function countWrites() {+ const writes = { nodes: 0, edges: 0 };+ const stopNodes = useNodeStore.subscribe(() => writes.nodes++);+ const stopEdges = useEdgeStore.subscribe(() => writes.edges++);+ return {+ writes,+ stop() {+ stopNodes();+ stopEdges();+ },+ };+}++describe("applyNodeExecutionEvent", () => {+ beforeEach(() => {+ useNodeStore.setState({+ nodes: [createNode("0"), createNode("1")],+ latestNodeInputData: {},+ latestNodeOutputData: {},+ accumulatedNodeInputData: {},+ accumulatedNodeOutputData: {},+ });+ useEdgeStore.setState({ edges: [createEdge("0", "1")] });+ });++ it("updates status, results and edge beads with one write per store", () => {+ const counter = countWrites();++ useNodeStore.getState().applyNodeExecutionEvent("1", createEvent());+ counter.stop();++ expect(counter.writes).toEqual({ nodes: 1, edges: 1 });+ const state = useNodeStore.getState();+ expect(state.getNodeStatus("1")).toBe("RUNNING");+ expect(state.getNodeExecutionResults("1")).toHaveLength(1);+ expect(state.getLatestNodeInputData("1")).toEqual({ input: "hello" });+ expect(state.getAccumulatedNodeInputData("1")).toEqual({+ input: ["hello"],+ });+ expect(useEdgeStore.getState().edges[0].data).toMatchObject({+ beadUp: 1,+ beadDown: 1,+ });+ });++ it("returns the same state objects for an identical event", () => {+ useNodeStore.getState().applyNodeExecutionEvent("1", createEvent());+ const nodeState = useNodeStore.getState();+ const edgeState = useEdgeStore.getState();+ const counter = countWrites();++ useNodeStore.getState().applyNodeExecutionEvent("1", createEvent());+ counter.stop();++ expect(useNodeStore.getState()).toBe(nodeState);+ expect(useEdgeStore.getState()).toBe(edgeState);+ expect(counter.writes).toEqual({ nodes: 0, edges: 0 });+ });++ it("updates only the status when the payload is unchanged", () => {+ useNodeStore.getState().applyNodeExecutionEvent("1", createEvent());+ const [firstResult] = useNodeStore.getState().getNodeExecutionResults("1");+ const otherNode = useNodeStore.getState().nodes[0];++ useNodeStore+ .getState()+ .applyNodeExecutionEvent("1", createEvent({ status: "COMPLETED" }));++ const state = useNodeStore.getState();+ expect(state.getNodeStatus("1")).toBe("COMPLETED");+ expect(state.getNodeExecutionResults("1")).toEqual([firstResult]);+ expect(state.getNodeExecutionResults("1")[0]).toBe(firstResult);+ expect(state.nodes[0]).toBe(otherNode);+ });++ it("replaces a result whose output changed and rebuilds its accumulated data", () => {+ useNodeStore.getState().applyNodeExecutionEvent("1", createEvent());+ useNodeStore.getState().applyNodeExecutionEvent(+ "1",+ createEvent({+ status: "COMPLETED",+ output_data: { output: ["hello"] },+ end_time: new Date("2026-01-05T09:00:02.000Z"),+ }),+ );++ const state = useNodeStore.getState();+ expect(state.getNodeStatus("1")).toBe("COMPLETED");+ expect(state.getNodeExecutionResults("1")).toHaveLength(1);+ expect(state.getLatestNodeOutputData("1")).toEqual({ output: ["hello"] });+ expect(state.getAccumulatedNodeOutputData("1")).toEqual({+ output: ["hello"],+ });+ expect(state.getAccumulatedNodeInputData("1")).toEqual({+ input: ["hello"],+ });+ });++ it("appends a new execution of the same node", () => {+ useNodeStore.getState().applyNodeExecutionEvent("1", createEvent());+ useNodeStore.getState().applyNodeExecutionEvent(+ "1",+ createEvent({+ node_exec_id: "node-exec-2",+ input_data: { input: "again" },+ }),+ );++ const state = useNodeStore.getState();+ expect(state.getNodeExecutionResults("1")).toHaveLength(2);+ expect(state.getLatestNodeInputData("1")).toEqual({ input: "again" });+ expect(state.getAccumulatedNodeInputData("1")).toEqual({+ input: ["hello", "again"],+ });+ });++ it("leaves the store untouched for a node that is not on the canvas", () => {+ const nodeState = useNodeStore.getState();++ useNodeStore+ .getState()+ .applyNodeExecutionEvent("missing", createEvent({ node_id: "missing" }));++ expect(useNodeStore.getState()).toBe(nodeState);+ });+});++describe("updateNodeStatus", () => {+ beforeEach(() => {+ useNodeStore.setState({ nodes: [createNode("1")] });+ });++ it("leaves the store untouched when the status is unchanged", () => {+ useNodeStore.getState().updateNodeStatus("1", "RUNNING");+ const nodeState = useNodeStore.getState();++ useNodeStore.getState().updateNodeStatus("1", "RUNNING");++ expect(useNodeStore.getState()).toBe(nodeState);+ });+});++describe("updateNodeExecutionResult", () => {+ beforeEach(() => {+ useNodeStore.setState({ nodes: [createNode("1")] });+ });++ it("leaves the store untouched for a repeated result", () => {+ useNodeStore.getState().updateNodeExecutionResult("1", createEvent());+ const nodeState = useNodeStore.getState();++ useNodeStore.getState().updateNodeExecutionResult("1", createEvent());++ expect(useNodeStore.getState()).toBe(nodeState);+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/__tests__/executionUpdateOrder.test.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/__tests__/executionUpdateOrder.test.tsnew file mode 100644index 0000000000..95739a22df--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/__tests__/executionUpdateOrder.test.ts@@ -0,0 +1,136 @@+import { beforeEach, describe, expect, it } from "vitest";+import type { NodeExecutionResult } from "@/app/api/__generated__/models/nodeExecutionResult";+import {+ forgetDeliveries,+ isSnapshotStatusCurrent,+ orderSnapshotExecutions,+ recordFetchStart,+ recordFetchSuccess,+ recordGraphStatusDelivery,+ recordNodeExecutionDelivery,+} from "../executionUpdateOrder";++function execution(+ nodeExecID: string,+ status: NodeExecutionResult["status"],+): NodeExecutionResult {+ return {+ user_id: "user-1",+ graph_id: "graph-1",+ graph_version: 1,+ graph_exec_id: "run-1",+ node_exec_id: nodeExecID,+ node_id: `node-of-${nodeExecID}`,+ block_id: "block-1",+ status,+ input_data: {},+ output_data: {},+ add_time: new Date("2026-01-05T09:00:00.000Z"),+ queue_time: null,+ start_time: null,+ end_time: null,+ };+}++let nextUpdatedAt = 1_000;++// A fetch whose start and landing are recorded around `during`.+function fetchSnapshot(during: () => void = () => undefined) {+ recordFetchStart();+ during();+ nextUpdatedAt += 1;+ recordFetchSuccess(nextUpdatedAt);+ return nextUpdatedAt;+}++describe("executionUpdateOrder", () => {+ beforeEach(() => {+ forgetDeliveries();+ });++ it("applies a snapshot as is when nothing arrived over the websocket", () => {+ const snapshot = [execution("a", "RUNNING"), execution("b", "QUEUED")];+ const updatedAt = fetchSnapshot();++ expect(orderSnapshotExecutions(snapshot, updatedAt)).toBe(snapshot);+ expect(isSnapshotStatusCurrent(updatedAt)).toBe(true);+ });++ it("puts deliveries that arrived while the fetch was in flight last", () => {+ const delivered = execution("a", "COMPLETED");+ const updatedAt = fetchSnapshot(() => {+ recordNodeExecutionDelivery(delivered);+ recordGraphStatusDelivery();+ });+ const snapshot = [execution("a", "RUNNING"), execution("b", "QUEUED")];++ expect(orderSnapshotExecutions(snapshot, updatedAt)).toEqual([+ snapshot[1],+ delivered,+ ]);+ expect(isSnapshotStatusCurrent(updatedAt)).toBe(false);+ });++ it("trusts a snapshot fetched after the deliveries arrived", () => {+ recordNodeExecutionDelivery(execution("a", "RUNNING"));+ recordGraphStatusDelivery();+ const snapshot = [execution("a", "COMPLETED")];+ const updatedAt = fetchSnapshot();++ expect(orderSnapshotExecutions(snapshot, updatedAt)).toBe(snapshot);+ expect(isSnapshotStatusCurrent(updatedAt)).toBe(true);+ });++ it("keeps deliveries in arrival order, with the latest copy of each", () => {+ const updatedAt = fetchSnapshot(() => {+ recordNodeExecutionDelivery(execution("a", "RUNNING"));+ recordNodeExecutionDelivery(execution("b", "RUNNING"));+ recordNodeExecutionDelivery(execution("a", "COMPLETED"));+ });++ expect(+ orderSnapshotExecutions([], updatedAt).map(+ (result) => `${result.node_exec_id}:${result.status}`,+ ),+ ).toEqual(["b:RUNNING", "a:COMPLETED"]);+ });++ it("treats a snapshot whose fetch start was not seen as older than every delivery", () => {+ const delivered = execution("a", "COMPLETED");+ recordNodeExecutionDelivery(delivered);+ recordGraphStatusDelivery();++ expect(orderSnapshotExecutions([execution("a", "RUNNING")], 42)).toEqual([+ delivered,+ ]);+ expect(isSnapshotStatusCurrent(42)).toBe(false);+ });++ it("trusts snapshots again once the deliveries are forgotten", () => {+ const updatedAt = fetchSnapshot(() => {+ recordNodeExecutionDelivery(execution("a", "COMPLETED"));+ recordGraphStatusDelivery();+ });+ forgetDeliveries();+ const snapshot = [execution("a", "RUNNING")];++ expect(orderSnapshotExecutions(snapshot, updatedAt)).toBe(snapshot);+ expect(isSnapshotStatusCurrent(updatedAt)).toBe(true);+ });++ it("keeps tracking recent snapshots after older ones are dropped", () => {+ const first = fetchSnapshot(() =>+ recordNodeExecutionDelivery(execution("a", "COMPLETED")),+ );+ for (let index = 0; index < 25; index++) fetchSnapshot();+ const latest = fetchSnapshot();++ expect(+ orderSnapshotExecutions([execution("a", "RUNNING")], latest),+ ).toEqual([execution("a", "RUNNING")]);+ // The first snapshot is no longer tracked, so it counts as the oldest.+ expect(+ orderSnapshotExecutions([execution("a", "RUNNING")], first)[0].status,+ ).toBe("COMPLETED");+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/__tests__/executionUpdates.test.tsx b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/__tests__/executionUpdates.test.tsxnew file mode 100644index 0000000000..ac1c2e14e8--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/__tests__/executionUpdates.test.tsx@@ -0,0 +1,479 @@+import { server } from "@/mocks/mock-server";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import { act, renderHook, waitFor } from "@testing-library/react";+import { http, HttpResponse } from "msw";+import { NuqsTestingAdapter } from "nuqs/adapters/testing";+import type { ReactNode } from "react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import { useEdgeStore } from "../../../../stores/edgeStore";+import { useGraphStore } from "../../../../stores/graphStore";+import { useNodeStore } from "../../../../stores/nodeStore";+import { useFlow } from "../useFlow";+import { useFlowRealtime } from "../useFlowRealtime";++// Stands in for the BackendAPI websocket: the test decides when the socket+// connects, when the server acknowledges the subscription, and what it sends.+const socket = vi.hoisted(() => {+ type Handler = (data: unknown) => void;+ const messageHandlers = new Map<string, Set<Handler>>();+ const connectHandlers = new Set<() => void>();+ const disconnectHandlers = new Set<() => void>();+ const state = { open: false, subscribes: [] as string[] };+ const api = {+ onWebSocketMessage(method: string, handler: Handler) {+ if (!messageHandlers.has(method)) messageHandlers.set(method, new Set());+ messageHandlers.get(method)!.add(handler);+ return () => messageHandlers.get(method)!.delete(handler);+ },+ onWebSocketConnect(handler: () => void) {+ connectHandlers.add(handler);+ if (state.open) handler();+ return () => connectHandlers.delete(handler);+ },+ onWebSocketDisconnect(handler: () => void) {+ disconnectHandlers.add(handler);+ return () => disconnectHandlers.delete(handler);+ },+ subscribeToGraphExecution(executionID: string) {+ state.subscribes.push(executionID);+ return Promise.resolve();+ },+ };+ return {+ api,+ state,+ connect() {+ state.open = true;+ connectHandlers.forEach((handler) => handler());+ },+ disconnect() {+ state.open = false;+ disconnectHandlers.forEach((handler) => handler());+ },+ send(method: string, data?: unknown) {+ messageHandlers.get(method)?.forEach((handler) => handler(data));+ },+ reset() {+ state.open = false;+ state.subscribes = [];+ messageHandlers.clear();+ connectHandlers.clear();+ disconnectHandlers.clear();+ },+ };+});++vi.mock("@/lib/autogpt-server-api/context", () => ({+ useBackendAPI: () => socket.api,+}));++vi.mock("@xyflow/react", async () => {+ const actual = await vi.importActual("@xyflow/react");+ return {+ ...actual,+ useReactFlow: () => ({+ screenToFlowPosition: (position: unknown) => position,+ fitView: vi.fn(),+ }),+ };+});++vi.mock("../../../../hooks/useIsReadOnlyGraph", () => ({+ useIsReadOnlyGraph: () => ({ isReadOnly: false }),+}));++const GRAPH_ID = "graph-1";+const EXECUTION_ID = "execution-1";+const BLOCK_ID = "block-1";+const NODE_IDS = ["node-1", "node-2", "node-3"];+const POLL_INTERVAL_MS = 1_000;+const RECONCILE_INTERVAL_MS = 10_000;++type NodeStatus = "QUEUED" | "RUNNING" | "COMPLETED";++function nodeExecution(nodeID: string, status: NodeStatus) {+ return {+ user_id: "user-1",+ graph_id: GRAPH_ID,+ graph_version: 1,+ graph_exec_id: EXECUTION_ID,+ node_exec_id: `exec-${nodeID}`,+ node_id: nodeID,+ block_id: BLOCK_ID,+ status,+ input_data: { input: nodeID },+ output_data: status === "COMPLETED" ? { output: [nodeID] } : {},+ add_time: "2026-01-05T09:00:00.000Z",+ queue_time: "2026-01-05T09:00:00.000Z",+ start_time: status === "QUEUED" ? null : "2026-01-05T09:00:01.000Z",+ end_time: status === "COMPLETED" ? "2026-01-05T09:00:02.000Z" : null,+ };+}++// What the backend returns for the run; tests change it as the run moves on.+let run: {+ status: "RUNNING" | "COMPLETED";+ nodes: Partial<Record<string, NodeStatus>>;+ earlierExecutions?: ReturnType<typeof nodeExecution>[];+};+let executionRequests: number;+// While set, execution responses are held back after the backend has read+// the run, so they arrive stale.+let heldResponses: Promise<void> | null;++function graphResponse() {+ return {+ id: GRAPH_ID,+ version: 1,+ name: "Test workflow",+ description: "",+ is_active: true,+ nodes: NODE_IDS.map((id, index) => ({+ id,+ block_id: BLOCK_ID,+ input_default: {},+ metadata: { position: { x: index * 400, y: 0 } },+ input_links: [],+ output_links: [],+ })),+ links: [+ {+ id: "link-1",+ source_id: "node-1",+ sink_id: "node-2",+ source_name: "output",+ sink_name: "input",+ is_static: false,+ },+ ],+ input_schema: {},+ output_schema: {},+ credentials_input_schema: {},+ };+}++function executionResponse() {+ return {+ id: EXECUTION_ID,+ graph_id: GRAPH_ID,+ graph_version: 1,+ user_id: "user-1",+ status: run.status,+ started_at: "2026-01-05T09:00:00.000Z",+ ended_at: run.status === "COMPLETED" ? "2026-01-05T09:00:03.000Z" : null,+ inputs: {},+ outputs: {},+ node_executions: [+ ...(run.earlierExecutions ?? []),+ ...Object.entries(run.nodes).map(([nodeID, status]) =>+ nodeExecution(nodeID, status!),+ ),+ ],+ };+}++function renderBuilder() {+ const queryClient = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ function Wrapper({ children }: { children: ReactNode }) {+ return (+ <QueryClientProvider client={queryClient}>+ <NuqsTestingAdapter+ searchParams={`?flowID=${GRAPH_ID}&flowExecutionID=${EXECUTION_ID}`}+ >+ {children}+ </NuqsTestingAdapter>+ </QueryClientProvider>+ );+ }+ return renderHook(+ () => {+ useFlow();+ useFlowRealtime();+ },+ { wrapper: Wrapper },+ );+}++function nodeStatuses() {+ return Object.fromEntries(+ useNodeStore.getState().nodes.map((node) => [node.id, node.data.status]),+ );+}++async function acknowledgeSubscribe() {+ await waitFor(() => expect(socket.state.subscribes).toEqual([EXECUTION_ID]));+ const requestsBefore = executionRequests;+ act(() => socket.send("subscribe_graph_execution"));+ // The acknowledgement refetches once.+ await waitFor(() =>+ expect(executionRequests).toBeGreaterThan(requestsBefore),+ );+}++async function requestsDuring(ms: number) {+ const before = executionRequests;+ await act(() => new Promise((resolve) => setTimeout(resolve, ms)));+ return executionRequests - before;+}++describe("builder execution updates", () => {+ beforeEach(() => {+ socket.reset();+ run = { status: "RUNNING", nodes: { "node-1": "RUNNING" } };+ executionRequests = 0;+ heldResponses = null;+ server.use(+ http.get(`/api/proxy/api/graphs/${GRAPH_ID}`, () =>+ HttpResponse.json(graphResponse()),+ ),+ http.get(+ `/api/proxy/api/graphs/${GRAPH_ID}/executions/${EXECUTION_ID}`,+ async () => {+ executionRequests++;+ const snapshot = executionResponse();+ if (heldResponses) await heldResponses;+ return HttpResponse.json(snapshot);+ },+ ),+ http.get("/api/proxy/api/graphs", () => HttpResponse.json([])),+ http.get("/api/proxy/api/builder/blocks/batch", () =>+ HttpResponse.json([+ {+ id: BLOCK_ID,+ name: "Store Value",+ description: "",+ inputSchema: { type: "object", properties: {} },+ outputSchema: { type: "object", properties: {} },+ costs: [],+ categories: [],+ contributors: [],+ staticOutput: false,+ uiType: "Standard",+ },+ ]),+ ),+ );+ });++ afterEach(() => {+ useNodeStore.getState().setNodes([]);+ useEdgeStore.getState().setEdges([]);+ useGraphStore.getState().reset();+ });++ it("ends with every node's final status when the run finishes before the subscribe is acknowledged", async () => {+ renderBuilder();+ await waitFor(() => expect(nodeStatuses()["node-1"]).toBe("RUNNING"));++ act(() => socket.connect());+ await waitFor(() =>+ expect(socket.state.subscribes).toEqual([EXECUTION_ID]),+ );+ // The dry-run finishes while the subscription is still being set up, so+ // none of its node events ever reach the websocket.+ run = {+ status: "COMPLETED",+ nodes: {+ "node-1": "COMPLETED",+ "node-2": "COMPLETED",+ "node-3": "COMPLETED",+ },+ };+ act(() => socket.send("subscribe_graph_execution"));++ await waitFor(() =>+ expect(nodeStatuses()).toEqual({+ "node-1": "COMPLETED",+ "node-2": "COMPLETED",+ "node-3": "COMPLETED",+ }),+ );+ expect(useGraphStore.getState().isGraphRunning).toBe(false);+ expect(useNodeStore.getState().getLatestNodeOutputData("node-3")).toEqual({+ output: ["node-3"],+ });+ }, 15_000);++ it("polls while running until the subscribe is acknowledged, then only listens", async () => {+ renderBuilder();+ await waitFor(() => expect(nodeStatuses()["node-1"]).toBe("RUNNING"));+ act(() => socket.connect());++ // Subscribe sent but not acknowledged yet: keep polling.+ expect(await requestsDuring(POLL_INTERVAL_MS * 2.5)).toBeGreaterThan(0);++ await acknowledgeSubscribe();+ expect(await requestsDuring(POLL_INTERVAL_MS * 2.5)).toBe(0);++ act(() =>+ socket.send("node_execution_event", {+ ...nodeExecution("node-2", "RUNNING"),+ add_time: new Date("2026-01-05T09:00:00.000Z"),+ }),+ );+ expect(nodeStatuses()["node-2"]).toBe("RUNNING");+ expect(useEdgeStore.getState().edges[0].data?.beadUp).toBe(1);+ }, 20_000);++ it("polls again after the websocket drops until it resubscribes", async () => {+ renderBuilder();+ await waitFor(() => expect(nodeStatuses()["node-1"]).toBe("RUNNING"));+ act(() => socket.connect());+ await acknowledgeSubscribe();++ act(() => socket.disconnect());+ expect(await requestsDuring(POLL_INTERVAL_MS * 2.5)).toBeGreaterThan(0);++ socket.state.subscribes = [];+ act(() => socket.connect());+ await acknowledgeSubscribe();+ expect(await requestsDuring(POLL_INTERVAL_MS * 2.5)).toBe(0);+ }, 20_000);++ it("keeps newer websocket updates when a refetch that was in flight lands after them", async () => {+ renderBuilder();+ await waitFor(() => expect(nodeStatuses()["node-1"]).toBe("RUNNING"));+ act(() => socket.connect());+ await waitFor(() =>+ expect(socket.state.subscribes).toEqual([EXECUTION_ID]),+ );++ let release: () => void = () => undefined;+ heldResponses = new Promise((resolve) => {+ release = resolve;+ });+ const requestsBefore = executionRequests;+ act(() => socket.send("subscribe_graph_execution"));+ await waitFor(() =>+ expect(executionRequests).toBeGreaterThan(requestsBefore),+ );++ // The run finishes while the refetch, read as RUNNING, is on its way.+ run = { status: "COMPLETED", nodes: { "node-1": "COMPLETED" } };+ act(() => {+ socket.send("node_execution_event", nodeExecution("node-1", "COMPLETED"));+ socket.send("graph_execution_event", {+ id: EXECUTION_ID,+ status: "COMPLETED",+ });+ });+ expect(nodeStatuses()["node-1"]).toBe("COMPLETED");++ const requestsAtRelease = executionRequests;+ act(() => release());+ await act(() => new Promise((resolve) => setTimeout(resolve, 200)));++ expect(nodeStatuses()["node-1"]).toBe("COMPLETED");+ expect(useGraphStore.getState().isGraphRunning).toBe(false);+ expect(useNodeStore.getState().getLatestNodeOutputData("node-1")).toEqual({+ output: ["node-1"],+ });+ expect(executionRequests).toBe(requestsAtRelease);+ }, 15_000);++ it(+ "still catches up, slowly, on a run whose last events never reached the websocket",+ async () => {+ renderBuilder();+ await waitFor(() => expect(nodeStatuses()["node-1"]).toBe("RUNNING"));+ act(() => socket.connect());+ await acknowledgeSubscribe();+ act(() =>+ socket.send("node_execution_event", nodeExecution("node-1", "RUNNING")),+ );++ // The run finishes, but its events are lost before the websocket.+ run = { status: "COMPLETED", nodes: { "node-1": "COMPLETED" } };+ expect(await requestsDuring(POLL_INTERVAL_MS * 2.5)).toBe(0);++ await waitFor(() => expect(nodeStatuses()["node-1"]).toBe("COMPLETED"), {+ timeout: RECONCILE_INTERVAL_MS + 2_000,+ });+ expect(useGraphStore.getState().isGraphRunning).toBe(false);+ },+ RECONCILE_INTERVAL_MS + 15_000,+ );++ it("keeps the latest execution's status for a node that ran twice", async () => {+ run = { status: "RUNNING", nodes: { "node-1": "COMPLETED" } };+ renderBuilder();+ await waitFor(() => expect(nodeStatuses()["node-1"]).toBe("COMPLETED"));+ act(() => socket.connect());+ await acknowledgeSubscribe();++ const secondRun = {+ ...nodeExecution("node-1", "RUNNING"),+ node_exec_id: "exec-node-1-again",+ };+ act(() => socket.send("node_execution_event", secondRun));+ expect(nodeStatuses()["node-1"]).toBe("RUNNING");++ // A later snapshot lists both executions, the finished one first.+ run = {+ status: "RUNNING",+ nodes: {},+ earlierExecutions: [nodeExecution("node-1", "COMPLETED"), secondRun],+ };+ await acknowledgeSubscribe();+ await act(() => new Promise((resolve) => setTimeout(resolve, 200)));++ expect(nodeStatuses()["node-1"]).toBe("RUNNING");+ expect(+ useNodeStore.getState().getNodeExecutionResults("node-1"),+ ).toHaveLength(2);+ });++ it("refetches on every acknowledgement, so a late one for an earlier run cannot hide this run's", async () => {+ renderBuilder();+ await waitFor(() => expect(nodeStatuses()["node-1"]).toBe("RUNNING"));+ act(() => socket.connect());+ await waitFor(() =>+ expect(socket.state.subscribes).toEqual([EXECUTION_ID]),+ );++ // An ack left over from a run opened earlier arrives first...+ await acknowledgeSubscribe();+ // ...and the run finishes before this run's own subscription is in place.+ run = { status: "COMPLETED", nodes: { "node-1": "COMPLETED" } };+ await acknowledgeSubscribe();++ await waitFor(() => expect(nodeStatuses()["node-1"]).toBe("COMPLETED"));+ expect(useGraphStore.getState().isGraphRunning).toBe(false);+ });++ it("ignores a repeated websocket event", async () => {+ renderBuilder();+ await waitFor(() => expect(nodeStatuses()["node-1"]).toBe("RUNNING"));+ act(() => socket.connect());+ await acknowledgeSubscribe();++ const event = nodeExecution("node-2", "COMPLETED");+ act(() => socket.send("node_execution_event", event));+ const nodes = useNodeStore.getState().nodes;+ const edges = useEdgeStore.getState().edges;++ act(() => socket.send("node_execution_event", { ...event }));++ expect(useNodeStore.getState().nodes).toBe(nodes);+ expect(useEdgeStore.getState().edges).toBe(edges);+ expect(nodeStatuses()["node-2"]).toBe("COMPLETED");+ });++ it("ignores events for another run", async () => {+ renderBuilder();+ await waitFor(() => expect(nodeStatuses()["node-1"]).toBe("RUNNING"));+ act(() => socket.connect());+ await acknowledgeSubscribe();++ act(() =>+ socket.send("node_execution_event", {+ ...nodeExecution("node-3", "COMPLETED"),+ graph_exec_id: "another-execution",+ }),+ );++ expect(nodeStatuses()["node-3"]).toBeUndefined();+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/executionUpdateOrder.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/executionUpdateOrder.tsnew file mode 100644index 0000000000..1a0cc67336--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/executionUpdateOrder.ts@@ -0,0 +1,84 @@+import type { NodeExecutionResult } from "@/app/api/__generated__/models/nodeExecutionResult";++// Orders websocket deliveries against execution-details fetches for the+// subscribed run. The backend writes a change before it publishes it, so a+// fetch that started after a delivery arrived has read that change or a newer+// one, while a delivery that arrived after a fetch started may be newer than+// what the fetch read. Each delivery and each fetch start takes the next+// number in one sequence, which tells the two cases apart.++const MAX_TRACKED_SNAPSHOTS = 20;++let sequence = 0;+let fetchStartSequence = 0;+let graphStatusSequence = 0;+const deliveredNodeExecutions = new Map<+ string,+ { result: NodeExecutionResult; sequence: number }+>();+// dataUpdatedAt of a snapshot -> sequence when the fetch behind it started+const snapshotSequences = new Map<number, number>();++export function recordNodeExecutionDelivery(result: NodeExecutionResult) {+ sequence += 1;+ deliveredNodeExecutions.delete(result.node_exec_id);+ deliveredNodeExecutions.set(result.node_exec_id, { result, sequence });+}++export function recordGraphStatusDelivery() {+ sequence += 1;+ graphStatusSequence = sequence;+}++export function recordFetchStart() {+ sequence += 1;+ fetchStartSequence = sequence;+}++export function recordFetchSuccess(dataUpdatedAt: number) {+ snapshotSequences.set(dataUpdatedAt, fetchStartSequence);+ if (snapshotSequences.size > MAX_TRACKED_SNAPSHOTS) {+ const oldest = snapshotSequences.keys().next().value;+ if (oldest !== undefined) snapshotSequences.delete(oldest);+ }+}++/** Forget the deliveries, e.g. when the websocket drops and may miss events. */+export function forgetDeliveries() {+ deliveredNodeExecutions.clear();+ graphStatusSequence = 0;+}++// A snapshot whose fetch start was not seen counts as older than every+// delivery, so the websocket's copies win.+function snapshotSequence(dataUpdatedAt: number) {+ return snapshotSequences.get(dataUpdatedAt) ?? 0;+}++/**+ * The node executions to apply for a snapshot, in order: the snapshot's own,+ * except those the websocket delivered after its fetch started, then those+ * deliveries in arrival order.+ */+export function orderSnapshotExecutions(+ nodeExecutions: NodeExecutionResult[],+ dataUpdatedAt: number,+) {+ const since = snapshotSequence(dataUpdatedAt);+ const newer = Array.from(deliveredNodeExecutions.values())+ .filter((delivery) => delivery.sequence > since)+ .map((delivery) => delivery.result);+ if (newer.length === 0) return nodeExecutions;+ const newerIDs = new Set(newer.map((result) => result.node_exec_id));+ return [+ ...nodeExecutions.filter(+ (nodeExecution) => !newerIDs.has(nodeExecution.node_exec_id),+ ),+ ...newer,+ ];+}++/** False when the websocket sent a run status after this snapshot's fetch started. */+export function isSnapshotStatusCurrent(dataUpdatedAt: number) {+ return graphStatusSequence <= snapshotSequence(dataUpdatedAt);+}diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/useFlow.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/useFlow.tsindex b1df3947c9..8d22092269 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/useFlow.ts+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/useFlow.ts@@ -26,6 +26,16 @@ import {getGraphLoadErrorToast,retryUnlessClientError,} from "../../../helpers/graphLoadError";+import { useIsExecutionSubscribed } from "./useFlowRealtime";+import {+ isSnapshotStatusCurrent,+ orderSnapshotExecutions,+} from "./executionUpdateOrder";++const POLL_INTERVAL_MS = 1_000;+// While subscribed, a slow poll still catches an event the backend failed to+// deliver (e.g. a dropped pub/sub message), as the 1 s poll used to.+const RECONCILE_INTERVAL_MS = 10_000;export const useFlow = () => {const { isReadOnly } = useIsReadOnlyGraph();@@ -36,11 +46,8 @@ export const useFlow = () => {const [isInitialLoadComplete, setIsInitialLoadComplete] = useState(false);const addNodes = useNodeStore(useShallow((state) => state.addNodes));const addLinks = useEdgeStore(useShallow((state) => state.addLinks));- const updateNodeStatus = useNodeStore(- useShallow((state) => state.updateNodeStatus),- );- const updateNodeExecutionResult = useNodeStore(- useShallow((state) => state.updateNodeExecutionResult),+ const applyNodeExecutionEvent = useNodeStore(+ useShallow((state) => state.applyNodeExecutionEvent),);const setGraphSchemas = useGraphStore(useShallow((state) => state.setGraphSchemas),@@ -51,9 +58,6 @@ export const useFlow = () => {const setAvailableSubGraphs = useGraphStore(useShallow((state) => state.setAvailableSubGraphs),);- const updateEdgeBeads = useEdgeStore(- useShallow((state) => state.updateEdgeBeads),- );const { screenToFlowPosition, fitView } = useReactFlow();const addBlock = useNodeStore(useShallow((state) => state.addBlock));const setBlockMenuOpen = useControlPanelStore(@@ -69,22 +73,26 @@ export const useFlow = () => {const isGraphRunning = useGraphStore(useShallow((state) => state.isGraphRunning),);+ const isExecutionSubscribed = useIsExecutionSubscribed(flowExecutionID);- const { data: executionDetails } = useGetV1GetExecutionDetails(- flowID || "",- flowExecutionID || "",- {+ const { data: executionDetails, dataUpdatedAt: executionDetailsUpdatedAt } =+ useGetV1GetExecutionDetails(flowID || "", flowExecutionID || "", {query: {select: (res) => res.data as GetV1GetExecutionDetails200,enabled: !!flowID && !!flowExecutionID,- // Poll while the graph is running to catch updates that arrive before- // the WebSocket subscription is established (race condition on fast- // executions like dry-runs). Stops once the execution reaches a- // terminal state and isGraphRunning becomes false.- refetchInterval: isGraphRunning ? 1000 : false,+ // Poll every second while the graph is running until the server+ // acknowledges the WebSocket subscription (race condition on fast+ // executions like dry-runs); useFlowRealtime refetches once on that+ // acknowledgement. Then only reconcile slowly, every second again if+ // the WebSocket drops, and stop once the execution reaches a terminal+ // state and isGraphRunning becomes false.+ refetchInterval: isGraphRunning+ ? isExecutionSubscribed+ ? RECONCILE_INTERVAL_MS+ : POLL_INTERVAL_MS+ : false,},- },- );+ });// Fetch all available graphs for sub-agent update detectionconst { data: availableGraphs } = useGetV1ListUserGraphs({@@ -193,41 +201,33 @@ export const useFlow = () => {}}, [customNodes, graph?.links]);- // update node execution status in nodes+ // update node statuses, execution results and edge beads. Executions the+ // store already has are no-ops, so a poll that changed nothing writes+ // nothing. Re-runs when the nodes are rebuilt (customNodes), which drops them.+ // Websocket deliveries that arrived after this snapshot's fetch started may+ // be newer than it, so they replace the snapshot's copies and go last.useEffect(() => {if (executionDetails &&"node_executions" in executionDetails &&executionDetails.node_executions) {- executionDetails.node_executions.forEach((nodeExecution) => {- updateNodeStatus(nodeExecution.node_id, nodeExecution.status);+ orderSnapshotExecutions(+ executionDetails.node_executions,+ executionDetailsUpdatedAt,+ ).forEach((nodeExecution) => {+ applyNodeExecutionEvent(nodeExecution.node_id, nodeExecution);});}- }, [executionDetails, updateNodeStatus, customNodes]);+ }, [executionDetails, applyNodeExecutionEvent, customNodes]);- // update node execution results in nodes, also update edge beads+ // update graph execution status, unless the websocket sent one after this+ // snapshot's fetch starteduseEffect(() => {if (executionDetails &&- "node_executions" in executionDetails &&- executionDetails.node_executions+ isSnapshotStatusCurrent(executionDetailsUpdatedAt)) {- executionDetails.node_executions.forEach((nodeExecution) => {- updateNodeExecutionResult(nodeExecution.node_id, nodeExecution);- updateEdgeBeads(nodeExecution.node_id, nodeExecution);- });- }- }, [- executionDetails,- updateNodeExecutionResult,- updateEdgeBeads,- customNodes,- ]);-- // update graph execution status- useEffect(() => {- if (executionDetails) {setGraphExecutionStatus(executionDetails.status as AgentExecutionStatus);}}, [executionDetails]);diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/useFlowRealtime.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/useFlowRealtime.tsindex 1c73460bd4..73c6554427 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/useFlowRealtime.ts+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/useFlowRealtime.ts@@ -12,22 +12,42 @@ import { useGraphStore } from "../../../stores/graphStore";import { useEdgeStore } from "../../../stores/edgeStore";import { useQueryClient } from "@tanstack/react-query";import { getGetV1GetExecutionDetailsQueryKey } from "@/app/api/__generated__/endpoints/graphs/graphs";+import { create } from "zustand";+import {+ forgetDeliveries,+ recordFetchStart,+ recordFetchSuccess,+ recordGraphStatusDelivery,+ recordNodeExecutionDelivery,+} from "./executionUpdateOrder";++const useSubscribedExecutionStore = create<{ executionID: string | null }>(+ () => ({ executionID: null }),+);++function setSubscribedExecutionID(executionID: string | null) {+ useSubscribedExecutionStore.setState({ executionID });+}++/**+ * True once the server has acknowledged the websocket subscription to this+ * execution, so every later node event arrives over the websocket.+ */+export function useIsExecutionSubscribed(executionID: string | null) {+ return useSubscribedExecutionStore(+ (state) => executionID !== null && state.executionID === executionID,+ );+}export const useFlowRealtime = () => {const api = useBackendAPI();const queryClient = useQueryClient();- const updateNodeExecutionResult = useNodeStore(- useShallow((state) => state.updateNodeExecutionResult),- );- const updateStatus = useNodeStore(- useShallow((state) => state.updateNodeStatus),+ const applyNodeExecutionEvent = useNodeStore(+ useShallow((state) => state.applyNodeExecutionEvent),);const setGraphExecutionStatus = useGraphStore(useShallow((state) => state.setGraphExecutionStatus),);- const updateEdgeBeads = useEdgeStore(- useShallow((state) => state.updateEdgeBeads),- );const resetEdgeBeads = useEdgeStore(useShallow((state) => state.resetEdgeBeads),);@@ -44,12 +64,9 @@ export const useFlowRealtime = () => {if (data.graph_exec_id != flowExecutionID) {return;}- updateNodeExecutionResult(- data.node_id,- data as unknown as NodeExecutionResult,- );- updateStatus(data.node_id, data.status);- updateEdgeBeads(data.node_id, data as unknown as NodeExecutionResult);+ const result = data as unknown as NodeExecutionResult;+ recordNodeExecutionDelivery(result);+ applyNodeExecutionEvent(data.node_id, result);},);@@ -60,13 +77,64 @@ export const useFlowRealtime = () => {return;}+ recordGraphStatusDelivery();setGraphExecutionStatus(graphExecution.status as AgentExecutionStatus);},);+ const [executionDetailsURL] = getGetV1GetExecutionDetailsQueryKey(+ flowID ?? "",+ flowExecutionID ?? "",+ );++ function refetchExecutionDetails() {+ void queryClient.invalidateQueries({ queryKey: [executionDetailsURL] });+ }++ // Note when each execution-details fetch starts and lands, so useFlow can+ // tell which websocket deliveries a snapshot already includes.+ const unsubscribeQueryCache = queryClient+ .getQueryCache()+ .subscribe((event) => {+ if (event.type !== "updated") return;+ if (event.query.queryKey[0] !== executionDetailsURL) return;+ if (event.action.type === "fetch") recordFetchStart();+ if (event.action.type === "success") {+ recordFetchSuccess(event.query.state.dataUpdatedAt);+ }+ });++ let hasSentSubscribe = false;++ // The server acknowledges a subscription once it is registered. Refetch+ // then too: anything published before that point is in the response, and+ // everything after it arrives over the websocket, so useFlow stops polling.+ // The ack carries no payload, so a late one for a previously opened run+ // can land here first; that is harmless because the server answers+ // subscribes in the order they were sent on the socket, so this run's own+ // ack still follows and its refetch runs after this run is registered.+ const deregisterSubscribeAck = api.onWebSocketMessage(+ "subscribe_graph_execution",+ () => {+ if (!hasSentSubscribe) return;+ setSubscribedExecutionID(flowExecutionID);+ refetchExecutionDetails();+ },+ );++ // Events published while the socket is down are lost, so snapshots are+ // the source of truth again until the resubscribe.+ const deregisterDisconnect = api.onWebSocketDisconnect(() => {+ hasSentSubscribe = false;+ setSubscribedExecutionID(null);+ forgetDeliveries();+ });+const deregisterGraphExecutionSubscription =flowID && flowExecutionID? api.onWebSocketConnect(() => {+ hasSentSubscribe = true;+ forgetDeliveries();// Subscribe to execution updatesapi.subscribeToGraphExecution(flowExecutionID as GraphExecutionID) // TODO: We are currently using a manual type, we need to fix it in future@@ -78,12 +146,7 @@ export const useFlowRealtime = () => {// published before the WebSocket subscription was established.// This closes the race-condition window for fast-completing// executions like dry-runs / simulations.- void queryClient.invalidateQueries({- queryKey: getGetV1GetExecutionDetailsQueryKey(- flowID!,- flowExecutionID,- ),- });+ refetchExecutionDetails();}).catch((error) =>console.error(@@ -98,9 +161,22 @@ export const useFlowRealtime = () => {deregisterNodeExecutionEvent();deregisterGraphExecutionSubscription();deregisterGraphExecutionStatusEvent();+ deregisterSubscribeAck();+ deregisterDisconnect();+ unsubscribeQueryCache();+ setSubscribedExecutionID(null);+ forgetDeliveries();resetEdgeBeads();};- }, [api, flowExecutionID, resetEdgeBeads, queryClient, flowID]);+ }, [+ api,+ flowExecutionID,+ resetEdgeBeads,+ queryClient,+ flowID,+ applyNodeExecutionEvent,+ setGraphExecutionStatus,+ ]);return {};};diff --git a/autogpt_platform/frontend/src/app/(platform)/build/stores/nodeStore.ts b/autogpt_platform/frontend/src/app/(platform)/build/stores/nodeStore.tsindex 08924f7939..086fa7f70e 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/stores/nodeStore.ts+++ b/autogpt_platform/frontend/src/app/(platform)/build/stores/nodeStore.ts@@ -22,6 +22,7 @@ import {parseHandleIdToPath,} from "@/components/renderers/InputRenderer/helpers";import { accumulateExecutionData } from "./helpers";+import { reuseUnchanged } from "@/lib/reuseUnchanged";import { NodeResolutionData } from "./types";const MINIMUM_MOVE_BEFORE_LOG = 50;@@ -69,6 +70,10 @@ type NodeStore = {nodeId: string,result: NodeExecutionResult,) => void;+ applyNodeExecutionEvent: (+ nodeId: string,+ result: NodeExecutionResult,+ ) => void;getNodeExecutionResults: (nodeId: string) => NodeExecutionResult[];getLatestNodeInputData: (nodeId: string,@@ -310,11 +315,7 @@ export const useNodeStore = create<NodeStore>((set, get) => ({);},updateNodeStatus: (nodeId: string, status: AgentExecutionStatus) => {- set((state) => ({- nodes: state.nodes.map((n) =>- n.id === nodeId ? { ...n, data: { ...n.data, status } } : n,- ),- }));+ set((state) => applyExecutionUpdate(state, nodeId, { status }));},getNodeStatus: (nodeId: string) => {return get().nodes.find((n) => n.id === nodeId)?.data?.status;@@ -330,114 +331,13 @@ export const useNodeStore = create<NodeStore>((set, get) => ({},updateNodeExecutionResult: (nodeId: string, result: NodeExecutionResult) => {- set((state) => {- let latestNodeInputData = state.latestNodeInputData;- let latestNodeOutputData = state.latestNodeOutputData;- let accumulatedNodeInputData = state.accumulatedNodeInputData;- let accumulatedNodeOutputData = state.accumulatedNodeOutputData;-- const nodes = state.nodes.map((n) => {- if (n.id !== nodeId) return n;-- const existingResults = n.data.nodeExecutionResults || [];- const duplicateIndex = existingResults.findIndex(- (r) => r.node_exec_id === result.node_exec_id,- );-- if (duplicateIndex !== -1) {- const oldResult = existingResults[duplicateIndex];- const inputDataChanged =- JSON.stringify(oldResult.input_data) !==- JSON.stringify(result.input_data);- const outputDataChanged =- JSON.stringify(oldResult.output_data) !==- JSON.stringify(result.output_data);-- if (!inputDataChanged && !outputDataChanged) {- return n;- }-- const updatedResults = [...existingResults];- updatedResults[duplicateIndex] = result;-- const recomputedAccumulatedInput = updatedResults.reduce(- (acc, r) => accumulateExecutionData(acc, r.input_data),- {} as Record<string, unknown[]>,- );- const recomputedAccumulatedOutput = updatedResults.reduce(- (acc, r) => accumulateExecutionData(acc, r.output_data),- {} as Record<string, unknown[]>,- );-- const mostRecentResult = updatedResults[updatedResults.length - 1];- latestNodeInputData = {- ...latestNodeInputData,- [nodeId]: mostRecentResult.input_data,- };- latestNodeOutputData = {- ...latestNodeOutputData,- [nodeId]: mostRecentResult.output_data,- };-- accumulatedNodeInputData = {- ...accumulatedNodeInputData,- [nodeId]: recomputedAccumulatedInput,- };- accumulatedNodeOutputData = {- ...accumulatedNodeOutputData,- [nodeId]: recomputedAccumulatedOutput,- };-- return {- ...n,- data: {- ...n.data,- nodeExecutionResults: updatedResults,- },- };- }-- accumulatedNodeInputData = {- ...accumulatedNodeInputData,- [nodeId]: accumulateExecutionData(- accumulatedNodeInputData[nodeId] || {},- result.input_data,- ),- };- accumulatedNodeOutputData = {- ...accumulatedNodeOutputData,- [nodeId]: accumulateExecutionData(- accumulatedNodeOutputData[nodeId] || {},- result.output_data,- ),- };-- latestNodeInputData = {- ...latestNodeInputData,- [nodeId]: result.input_data,- };- latestNodeOutputData = {- ...latestNodeOutputData,- [nodeId]: result.output_data,- };-- return {- ...n,- data: {- ...n.data,- nodeExecutionResults: [...existingResults, result],- },- };- });-- return {- nodes,- latestNodeInputData,- latestNodeOutputData,- accumulatedNodeInputData,- accumulatedNodeOutputData,- };- });+ set((state) => applyExecutionUpdate(state, nodeId, { result }));+ },+ applyNodeExecutionEvent: (nodeId: string, result: NodeExecutionResult) => {+ set((state) =>+ applyExecutionUpdate(state, nodeId, { result, status: result.status }),+ );+ useEdgeStore.getState().updateEdgeBeads(nodeId, result);},getNodeExecutionResults: (nodeId: string) => {return (@@ -684,3 +584,119 @@ export const useNodeStore = create<NodeStore>((set, get) => ({return mismatch?.newType;},}));++/**+ * Applies a status and/or an execution result to one node in a single write.+ * Returns `state` itself when nothing changes, so subscribers are not notified.+ */+function applyExecutionUpdate(+ state: NodeStore,+ nodeId: string,+ update: { result?: NodeExecutionResult; status?: AgentExecutionStatus },+) {+ const index = state.nodes.findIndex((n) => n.id === nodeId);+ if (index === -1) return state;++ const node = state.nodes[index];+ const resultUpdate = update.result+ ? withExecutionResult(state, node, update.result)+ : null;+ const statusChanged =+ update.status !== undefined && update.status !== node.data.status;+ if (!resultUpdate && !statusChanged) return state;++ const { nodeExecutionResults, ...executionData } = resultUpdate ?? {};+ const nodes = [...state.nodes];+ nodes[index] = {+ ...node,+ data: {+ ...node.data,+ ...(nodeExecutionResults && { nodeExecutionResults }),+ ...(statusChanged && { status: update.status }),+ },+ };+ return { nodes, ...executionData };+}++/**+ * The node's results and the store's latest and accumulated data once+ * `result` is added, or null when the node already has an equal result.+ */+function withExecutionResult(+ state: NodeStore,+ node: CustomNode,+ result: NodeExecutionResult,+) {+ const nodeId = node.id;+ const existingResults = node.data.nodeExecutionResults || [];+ const duplicateIndex = existingResults.findIndex(+ (r) => r.node_exec_id === result.node_exec_id,+ );++ if (duplicateIndex === -1) {+ return {+ nodeExecutionResults: [...existingResults, result],+ latestNodeInputData: {+ ...state.latestNodeInputData,+ [nodeId]: result.input_data,+ },+ latestNodeOutputData: {+ ...state.latestNodeOutputData,+ [nodeId]: result.output_data,+ },+ accumulatedNodeInputData: {+ ...state.accumulatedNodeInputData,+ [nodeId]: accumulateExecutionData(+ state.accumulatedNodeInputData[nodeId] || {},+ result.input_data,+ ),+ },+ accumulatedNodeOutputData: {+ ...state.accumulatedNodeOutputData,+ [nodeId]: accumulateExecutionData(+ state.accumulatedNodeOutputData[nodeId] || {},+ result.output_data,+ ),+ },+ };+ }++ const oldResult = existingResults[duplicateIndex];+ const inputDataUnchanged =+ reuseUnchanged(oldResult.input_data, result.input_data) ===+ oldResult.input_data;+ const outputDataUnchanged =+ reuseUnchanged(oldResult.output_data, result.output_data) ===+ oldResult.output_data;+ if (inputDataUnchanged && outputDataUnchanged) return null;++ const updatedResults = [...existingResults];+ updatedResults[duplicateIndex] = result;+ const mostRecentResult = updatedResults[updatedResults.length - 1];++ return {+ nodeExecutionResults: updatedResults,+ latestNodeInputData: {+ ...state.latestNodeInputData,+ [nodeId]: mostRecentResult.input_data,+ },+ latestNodeOutputData: {+ ...state.latestNodeOutputData,+ [nodeId]: mostRecentResult.output_data,+ },+ accumulatedNodeInputData: {+ ...state.accumulatedNodeInputData,+ [nodeId]: updatedResults.reduce(+ (acc, r) => accumulateExecutionData(acc, r.input_data),+ {} as Record<string, unknown[]>,+ ),+ },+ accumulatedNodeOutputData: {+ ...state.accumulatedNodeOutputData,+ [nodeId]: updatedResults.reduce(+ (acc, r) => accumulateExecutionData(acc, r.output_data),+ {} as Record<string, unknown[]>,+ ),+ },+ };+}diff --git a/autogpt_platform/frontend/src/lib/reuseUnchanged.test.ts b/autogpt_platform/frontend/src/lib/reuseUnchanged.test.tsnew file mode 100644index 0000000000..8e6a608fa5--- /dev/null+++ b/autogpt_platform/frontend/src/lib/reuseUnchanged.test.ts@@ -0,0 +1,99 @@+import { describe, expect, it } from "vitest";+import { reuseUnchanged } from "./reuseUnchanged";++describe("reuseUnchanged", () => {+ it("returns the previous value when the next one is deep-equal", () => {+ const previous = {+ status: "COMPLETED",+ output: { result: [1, { nested: "a" }] },+ at: new Date("2026-01-05T09:00:00.000Z"),+ };+ const next = {+ status: "COMPLETED",+ output: { result: [1, { nested: "a" }] },+ at: new Date("2026-01-05T09:00:00.000Z"),+ };++ expect(reuseUnchanged(previous, next)).toBe(previous);+ });++ it("keeps equal subtrees and replaces only the changed ones", () => {+ const previous = {+ input: { prompt: "hello", options: { temperature: 0 } },+ output: { result: ["first"] },+ };+ const next = {+ input: { prompt: "hello", options: { temperature: 0 } },+ output: { result: ["first", "second"] },+ };++ const merged = reuseUnchanged(previous, next);++ expect(merged).not.toBe(previous);+ expect(merged).toEqual(next);+ expect(merged.input).toBe(previous.input);+ expect(merged.output).not.toBe(previous.output);+ expect(merged.output.result).toEqual(["first", "second"]);+ });++ it("keeps unchanged array items when an item changes", () => {+ const first = { id: "a", value: 1 };+ const previous = [first, { id: "b", value: 2 }];+ const next = [+ { id: "a", value: 1 },+ { id: "b", value: 3 },+ ];++ const merged = reuseUnchanged(previous, next);++ expect(merged).not.toBe(previous);+ expect(merged[0]).toBe(first);+ expect(merged[1]).not.toBe(previous[1]);+ expect(merged[1]).toEqual({ id: "b", value: 3 });+ });++ it("treats added, removed and undefined keys as changes", () => {+ const previous: Record<string, unknown> = { a: 1 };++ expect(reuseUnchanged(previous, { a: 1, b: 2 })).not.toBe(previous);+ const wider = { a: 1, b: 2 };+ expect(reuseUnchanged(wider, previous)).not.toBe(wider);+ expect(reuseUnchanged(wider, previous)).toEqual({ a: 1 });+ expect(reuseUnchanged(previous, { a: 1, b: undefined })).not.toBe(previous);+ expect(reuseUnchanged({ b: undefined }, { c: undefined })).toEqual({+ c: undefined,+ });+ });++ it("compares dates by time", () => {+ const previous = new Date("2026-01-05T09:00:00.000Z");++ expect(reuseUnchanged(previous, new Date(previous.getTime()))).toBe(+ previous,+ );+ const later = new Date("2026-01-05T09:00:01.000Z");+ expect(reuseUnchanged(previous, later)).toBe(later);+ });++ it("returns the next value when the types differ", () => {+ expect(reuseUnchanged({ a: 1 }, [1])).toEqual([1]);+ expect(reuseUnchanged([1], { 0: 1 })).toEqual({ 0: 1 });+ expect(reuseUnchanged("1", 1)).toBe(1);+ expect(reuseUnchanged(null, {})).toEqual({});+ expect(reuseUnchanged({}, null)).toBeNull();+ });++ it("only reuses other objects when they are the same reference", () => {+ const map = new Map([["a", 1]]);+ const sameContent = new Map([["a", 1]]);++ expect(reuseUnchanged(map, sameContent)).toBe(sameContent);+ const holder = { map };+ expect(reuseUnchanged(holder, { map })).toBe(holder);+ });++ it("treats NaN as equal to NaN", () => {+ const previous = { value: Number.NaN };+ expect(reuseUnchanged(previous, { value: Number.NaN })).toBe(previous);+ });+});diff --git a/autogpt_platform/frontend/src/lib/reuseUnchanged.ts b/autogpt_platform/frontend/src/lib/reuseUnchanged.tsnew file mode 100644index 0000000000..eac2275a96--- /dev/null+++ b/autogpt_platform/frontend/src/lib/reuseUnchanged.ts@@ -0,0 +1,52 @@+function isPlainObject(value: unknown): value is Record<string, unknown> {+ if (typeof value !== "object" || value === null) return false;+ const prototype = Object.getPrototypeOf(value);+ return prototype === Object.prototype || prototype === null;+}++/**+ * Returns `next`, but keeps every part of `previous` that is deep-equal to the+ * matching part of `next`, so unchanged subtrees keep their references. When+ * the two are deep-equal the result is `previous` itself, which makes+ * `reuseUnchanged(a, b) === a` a cheap equality check.+ *+ * Plain objects and arrays are compared by content and dates by time. Other+ * objects (Map, Set, class instances) are only equal when they are the same+ * reference. Unlike React Query's structural sharing, dates count as equal,+ * since API responses turn every timestamp into a fresh `Date`.+ */+export function reuseUnchanged<T>(previous: unknown, next: T): T {+ if (Object.is(previous, next)) return previous as T;++ if (previous instanceof Date && next instanceof Date) {+ return Object.is(previous.getTime(), next.getTime())+ ? (previous as T)+ : next;+ }++ if (Array.isArray(previous) && Array.isArray(next)) {+ let changed = previous.length !== next.length;+ const merged = next.map((item, index) => {+ const kept = reuseUnchanged(previous[index], item);+ if (!Object.is(kept, previous[index])) changed = true;+ return kept;+ });+ return (changed ? merged : previous) as T;+ }++ if (isPlainObject(previous) && isPlainObject(next)) {+ const keys = Object.keys(next);+ let changed = keys.length !== Object.keys(previous).length;+ const merged: Record<string, unknown> = {};+ for (const key of keys) {+ const kept = reuseUnchanged(previous[key], next[key]);+ if (!Object.is(kept, previous[key]) || !Object.hasOwn(previous, key)) {+ changed = true;+ }+ merged[key] = kept;+ }+ return (changed ? merged : previous) as T;+ }++ return next;+}diff --git a/docs/engineering/frontend-perf-baseline.md b/docs/engineering/frontend-perf-baseline.mdindex 793d169668..ccbfdfbdb0 100644--- a/docs/engineering/frontend-perf-baseline.md+++ b/docs/engineering/frontend-perf-baseline.md@@ -437,3 +437,51 @@ Added after the table above, so it was measured on `dev` at `a1578ad9a` (2026-10| `BaseContent props:children` | 3,440 || `DialogWrap props:styling,handleOpen,handleClose,children` | 3,440 || `LinkComponent props:children` | 3,200 |++## builder-run-events · baseline — median of 3 runs++Added with SECRT-3003: measured on `dev` app code at `a1578ad9a` on a similar 16 GB cloud sandbox.++| metric | median | range | spread |+| --- | --- | --- | --- |+| commits | 158 | 157 – 159 | 1.3% |+| total renders | 58,440 | 58,438 – 58,440 | 0.0% |+| renders per commit | 369.9 | 367.5 – 372.2 | 1.3% |+| max renders in one commit | 10,628 | 10,628 – 10,628 | 0.0% |+| script time (ms) | 2,528 | 2,367 – 2,774 | 16.1% |+| task time (ms) | 3,038 | 2,766 – 3,207 | 14.5% |+| layout time (ms) | 17 | 15 – 17 | 7.6% |+| style recalc time (ms) | 27 | 22 – 28 | 19.4% |+| long tasks (≥ 50 ms) | 6 | 6 – 10 | 66.7% |+| long task total (ms) | 1,868 | 1,588 – 2,065 | 25.5% |+| heap used after GC (MB) | 107.9 | 107.4 – 110.7 | 3.1% |++**Top components (median renders)**++| name | renders |+| --- | --- |+| `Presence` | 3,032 |+| `Text` | 2,667 |+| `HugeiconsIcon` | 2,438 |+| `Icon` | 2,438 |+| `Tooltip` | 2,266 |+| `TooltipContent` | 2,266 |+| `TooltipProvider` | 2,214 |+| `Primitive.div` | 1,962 |+| `Primitive.button` | 1,636 |+| `Popper` | 1,605 |++**Top render causes (median renders)**++| name | renders |+| --- | --- |+| `Presence props:children` | 2,932 |+| `HugeiconsIcon parent-same-props<-Icon` | 2,398 |+| `Tooltip props:children` | 2,226 |+| `TooltipProvider props:children` | 2,174 |+| `Primitive.div props:children` | 1,554 |+| `Primitive.div.Slot props:children` | 1,375 |+| `Primitive.div.SlotClone props:children` | 1,375 |+| `TooltipContent props:children` | 1,344 |+| `Primitive.button.Slot props:children,onPointerMove,onPointerLeave,onPointerDown,+4` | 1,217 |+| `Primitive.button.SlotClone props:onPointerMove,onPointerLeave,onPointerDown,onFocus,+4` | 1,217 |diff --git a/docs/engineering/frontend-perf-measurement.md b/docs/engineering/frontend-perf-measurement.mdindex e324a3172f..488ebecf9d 100644--- a/docs/engineering/frontend-perf-measurement.md+++ b/docs/engineering/frontend-perf-measurement.md@@ -110,6 +110,7 @@ Names come from the component function. Third-party code bundled already minifie| `library-running` | Open the library with 20 workflows, all with a RUNNING run | Nothing | 30 s || `builder-connect-edge` | Open the same 50-node workflow with no run (nothing polls) | Drag one new edge from StoreValue node 4's `output` to Calculator node 5's `a` input, 20 pointer moves over 0.6 s | 2 s after the drop || `library-running-events` | Open the same library at `/library?sort=updatedAt`, with the websocket mocked | Push a `graph_execution_event` for one of the 20 runs every 250 ms, 80 in all; print the `subscribe_graph_executions` frames and `GET /executions` requests seen | 20 s of events + 2 s |+| `builder-run-events` | Open the same 50-node workflow with its RUNNING run; the harness answers the websocket subscribe for that run itself | 40 node execution events over the websocket, one every 50 ms: the 10 unfinished nodes go RUNNING then COMPLETED, each event sent twice. The execution GET returns what has been sent so far, as the backend would | 2 s after the last event |Each scenario's `readyWhen` field says exactly what setup waits for before the window opens. Most wait for the page's own content, then for network idle (at most 15 s, because some pages poll), then one more second.