Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

perf(frontend/library): stop re-subscribing to execution events per render (#15681)

Abhimanyu Yadav781f276e7f22
diff --git a/autogpt_platform/frontend/scripts/perf/scenarios/library-running-events.ts b/autogpt_platform/frontend/scripts/perf/scenarios/library-running-events.ts
new file mode 100644
index 0000000000..a1ecb4da05
--- /dev/null
+++ b/autogpt_platform/frontend/scripts/perf/scenarios/library-running-events.ts
@@ -0,0 +1,127 @@
+import type { Page, WebSocketRoute } from "@playwright/test";
+import { createLibraryFixture } from "../lib/library-fixture";
+import { installRoutes, type RouteHandler } from "../lib/routes";
+import { settle, type Scenario } from "../lib/scenario";
+
+const READY_TIMEOUT_MS = 60_000;
+const EVENT_INTERVAL_MS = 250;
+const EVENT_COUNT = 80;
+// The fleet summary refetches at most once per 2 s, so a trailing refetch
+// can land up to 2 s after the last event; it counts towards the run.
+const TRAILING_WINDOW_MS = 2_000;
+const SUBSCRIBE_TIMEOUT_MS = 15_000;
+
+type Fixture = ReturnType<typeof createLibraryFixture>;
+
+/**
+ * Stands in for the websocket server: answers heartbeats and subscriptions,
+ * counts `subscribe_graph_executions` frames, and lets the scenario push
+ * `graph_execution_event`s at the page.
+ */
+async function mockExecutionSocket(page: Page) {
+ const sockets = new Set<WebSocketRoute>();
+ const stats = { subscribeFrames: 0, subscribedGraphIDs: new Set<string>() };
+ await page.routeWebSocket(/\/ws(\?|$)/, (ws) => {
+ sockets.add(ws);
+ ws.onClose(() => sockets.delete(ws));
+ ws.onMessage((raw) => {
+ const message = JSON.parse(String(raw));
+ if (message.method === "heartbeat") {
+ ws.send(JSON.stringify({ method: "heartbeat", data: "pong" }));
+ return;
+ }
+ if (message.method === "subscribe_graph_executions") {
+ stats.subscribeFrames += 1;
+ stats.subscribedGraphIDs.add(String(message.data?.graph_id));
+ }
+ ws.send(
+ JSON.stringify({
+ method: message.method,
+ success: true,
+ channel: message.data?.graph_id ?? "",
+ }),
+ );
+ });
+ });
+ function push(execution: Fixture["executions"][number]) {
+ const frame = JSON.stringify({
+ method: "graph_execution_event",
+ success: true,
+ data: { ...execution, outputs: {}, event_type: "graph_exec_update" },
+ });
+ for (const ws of sockets) ws.send(frame);
+ }
+ async function waitForSubscriptions(graphIDs: string[]) {
+ const deadline = Date.now() + SUBSCRIBE_TIMEOUT_MS;
+ while (!graphIDs.every((id) => stats.subscribedGraphIDs.has(id))) {
+ if (Date.now() > deadline) {
+ const missing = graphIDs.filter(
+ (id) => !stats.subscribedGraphIDs.has(id),
+ ).length;
+ throw new Error(
+ `The page never subscribed to ${missing} of ${graphIDs.length} workflows; events would reach no one.`,
+ );
+ }
+ await page.waitForTimeout(100);
+ }
+ }
+ return { stats, push, waitForSubscriptions };
+}
+
+function countRequests(handler: RouteHandler | undefined) {
+ const stats = { count: 0 };
+ const counted: RouteHandler = async (route, match) => {
+ if (route.request().method() === "GET") stats.count += 1;
+ await handler?.(route, match);
+ };
+ return { stats, counted };
+}
+
+export const libraryRunningEvents: Scenario = {
+ name: "library-running-events",
+ description:
+ "The library with 20 RUNNING workflows while the websocket pushes an execution event every 250 ms.",
+ readyWhen:
+ "the 20th workflow is listed, network idle + 1 s, and the socket has a subscription for all 20 workflows",
+ windowMs: 0,
+ async setup(ctx) {
+ const { page } = ctx;
+ const fixture = createLibraryFixture(ctx.userID);
+ const executions = countRequests(fixture.handlers.executions);
+ const socket = await mockExecutionSocket(page);
+ await installRoutes(page, {
+ ...fixture.handlers,
+ executions: executions.counted,
+ });
+ // With the sort already in the URL the page does not replace it on load;
+ // that navigation holds back the websocket token server action, so on a
+ // bare /library the socket never opens within the run.
+ await page.goto("/library?sort=updatedAt");
+ await page
+ .getByText(fixture.agents[fixture.agents.length - 1].name)
+ .first()
+ .waitFor({ state: "visible", timeout: READY_TIMEOUT_MS });
+ await settle(page);
+ await socket.waitForSubscriptions(
+ fixture.agents.map((agent) => agent.graph_id),
+ );
+ return {
+ async run() {
+ const subscribesBefore = socket.stats.subscribeFrames;
+ const fetchesBefore = executions.stats.count;
+ for (let index = 0; index < EVENT_COUNT; index += 1) {
+ socket.push(fixture.executions[index % fixture.executions.length]);
+ await page.waitForTimeout(EVENT_INTERVAL_MS);
+ }
+ await page.waitForTimeout(TRAILING_WINDOW_MS);
+ const seconds = (EVENT_COUNT * EVENT_INTERVAL_MS) / 1000;
+ console.log(
+ `[library-running-events] ${EVENT_COUNT} events in ${seconds}s (+ ${TRAILING_WINDOW_MS / 1000}s after): ` +
+ `${socket.stats.subscribeFrames - subscribesBefore} subscribe_graph_executions frames, ` +
+ `${executions.stats.count - fetchesBefore} GET /executions ` +
+ `(setup: ${subscribesBefore} subscribes, ${fetchesBefore} fetches)`,
+ );
+ },
+ };
+ },
+};
diff --git a/autogpt_platform/frontend/scripts/perf/scenarios/registry.ts b/autogpt_platform/frontend/scripts/perf/scenarios/registry.ts
index 7c3ed6e9be..cb6e8ad2e6 100644
--- a/autogpt_platform/frontend/scripts/perf/scenarios/registry.ts
+++ b/autogpt_platform/frontend/scripts/perf/scenarios/registry.ts
@@ -7,6 +7,7 @@ import { copilotStreamReply } from "./copilot-stream-reply";
import { copilotSwitchThreads } from "./copilot-switch-threads";
import { copilotType } from "./copilot-type";
import { libraryRunning } from "./library-running";
+import { libraryRunningEvents } from "./library-running-events";
export const SCENARIOS: Scenario[] = [
copilotIdle,
@@ -17,6 +18,7 @@ export const SCENARIOS: Scenario[] = [
builderDragRunning,
libraryRunning,
builderConnectEdge,
+ libraryRunningEvents,
];
export function findScenario(name: string) {
diff --git a/autogpt_platform/frontend/src/app/(platform)/library/__tests__/execution-events.test.tsx b/autogpt_platform/frontend/src/app/(platform)/library/__tests__/execution-events.test.tsx
new file mode 100644
index 0000000000..4d8dd829a7
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/library/__tests__/execution-events.test.tsx
@@ -0,0 +1,150 @@
+import {
+ afterEach,
+ beforeEach,
+ describe,
+ expect,
+ test,
+ vi,
+ type MockInstance,
+} from "vitest";
+import { act, render, screen, waitFor } from "@/tests/integrations/test-utils";
+import { server } from "@/mocks/mock-server";
+import {
+ getGetV2ListLibraryAgentsMockHandler,
+ getGetV2ListLibraryAgentsResponseMock,
+} from "@/app/api/__generated__/endpoints/library/library.msw";
+import { getGetV1ListAllExecutionsMockHandler } from "@/app/api/__generated__/endpoints/graphs/graphs.msw";
+import type { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";
+import BackendAPI from "@/lib/autogpt-server-api/client";
+import type { GraphExecution } from "@/lib/autogpt-server-api/types";
+import LibraryPage from "../page";
+
+type ExecutionHandler = (execution: GraphExecution) => void;
+
+const executionHandlers = new Set<ExecutionHandler>();
+
+function makeAgent(index: number): LibraryAgent {
+ const base = getGetV2ListLibraryAgentsResponseMock().agents[0];
+ return {
+ ...base,
+ id: `agent-${index}`,
+ graph_id: `graph-${index}`,
+ name: `Workflow ${index}`,
+ };
+}
+
+const AGENTS = [makeAgent(1), makeAgent(2), makeAgent(3)];
+
+function setupHandlers() {
+ server.use(
+ getGetV2ListLibraryAgentsMockHandler({
+ ...getGetV2ListLibraryAgentsResponseMock(),
+ agents: AGENTS,
+ pagination: {
+ total_items: AGENTS.length,
+ total_pages: 1,
+ current_page: 1,
+ page_size: 20,
+ },
+ }),
+ getGetV1ListAllExecutionsMockHandler([]),
+ );
+}
+
+function emitExecutionEvent(graphID: string, runID: string) {
+ const execution = {
+ id: runID,
+ graph_id: graphID,
+ status: "RUNNING",
+ } as unknown as GraphExecution;
+ act(() => {
+ executionHandlers.forEach((handler) => handler(execution));
+ });
+}
+
+function countExecutionListRequests() {
+ const counter = { count: 0 };
+ function record({ request }: { request: Request }) {
+ if (
+ request.method === "GET" &&
+ new URL(request.url).pathname.endsWith("/api/executions")
+ ) {
+ counter.count += 1;
+ }
+ }
+ server.events.on("request:start", record);
+ return {
+ counter,
+ stop: () => server.events.removeListener("request:start", record),
+ };
+}
+
+let subscribe: MockInstance<BackendAPI["subscribeToGraphExecutions"]>;
+
+beforeEach(() => {
+ executionHandlers.clear();
+ subscribe = vi
+ .spyOn(BackendAPI.prototype, "subscribeToGraphExecutions")
+ .mockResolvedValue(undefined);
+ vi.spyOn(BackendAPI.prototype, "connectWebSocket").mockResolvedValue(
+ undefined,
+ );
+ vi.spyOn(BackendAPI.prototype, "onWebSocketConnect").mockImplementation(
+ (handler: () => void) => {
+ handler();
+ return () => undefined;
+ },
+ );
+ vi.spyOn(BackendAPI.prototype, "onWebSocketMessage").mockImplementation(((
+ method: string,
+ handler: ExecutionHandler,
+ ) => {
+ if (method !== "graph_execution_event") return () => undefined;
+ executionHandlers.add(handler);
+ return () => executionHandlers.delete(handler);
+ }) as unknown as BackendAPI["onWebSocketMessage"]);
+});
+
+afterEach(() => {
+ vi.restoreAllMocks();
+});
+
+describe("LibraryPage execution events", () => {
+ test("re-rendering the page with the same data does not re-subscribe", async () => {
+ setupHandlers();
+ const { rerender } = render(<LibraryPage />);
+ await screen.findByText("Workflow 3");
+ await waitFor(() => expect(subscribe).toHaveBeenCalledTimes(3));
+
+ rerender(<LibraryPage />);
+ rerender(<LibraryPage />);
+ rerender(<LibraryPage />);
+ await act(async () => undefined);
+
+ expect(subscribe.mock.calls.map(([graphID]) => graphID).sort()).toEqual([
+ "graph-1",
+ "graph-2",
+ "graph-3",
+ ]);
+ });
+
+ test("a burst of execution events refetches the run list at most twice", async () => {
+ setupHandlers();
+ render(<LibraryPage />);
+ await screen.findByText("Workflow 3");
+ await waitFor(() => expect(executionHandlers.size).toBeGreaterThan(0));
+
+ const requests = countExecutionListRequests();
+ try {
+ for (let index = 0; index < 10; index += 1) {
+ emitExecutionEvent(`graph-${(index % 3) + 1}`, `run-${index}`);
+ }
+ await waitFor(() => expect(requests.counter.count).toBe(1));
+ await waitFor(() => expect(requests.counter.count).toBe(2), {
+ timeout: 4000,
+ });
+ } finally {
+ requests.stop();
+ }
+ });
+});
diff --git a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/sidebar/SidebarRunsList/useSidebarRunsList.ts b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/sidebar/SidebarRunsList/useSidebarRunsList.ts
index ebb3f0f5f6..92d3e22434 100644
--- a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/sidebar/SidebarRunsList/useSidebarRunsList.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/sidebar/SidebarRunsList/useSidebarRunsList.ts
@@ -18,7 +18,10 @@ import {
retryUnlessClientError,
} from "../../../helpers";
import { useAgentPresetsQuery } from "../../../hooks/useAgentPresetsQuery";
-import { useExecutionEvents } from "@/hooks/useExecutionEvents";
+import {
+ EXECUTION_REFETCH_THROTTLE_MS,
+ useExecutionEvents,
+} from "@/hooks/useExecutionEvents";
import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";
import { useQueryClient } from "@tanstack/react-query";
import { parseAsString, useQueryStates } from "nuqs";
@@ -150,6 +153,9 @@ export function useSidebarRunsList({
useExecutionEvents({
graphId: graphId || undefined,
enabled: !!graphId && tabValue === "runs",
+ // Refetching the infinite query reloads every loaded page in turn, so a
+ // burst of events is folded into one refetch now and one at the end.
+ throttleMs: EXECUTION_REFETCH_THROTTLE_MS,
onExecutionUpdate: (_execution) => {
// Invalidate and refetch the query to ensure we have the latest data
// This is simpler and more reliable than manually updating the cache
diff --git a/autogpt_platform/frontend/src/app/(platform)/library/hooks/useLibraryFleetSummary.ts b/autogpt_platform/frontend/src/app/(platform)/library/hooks/useLibraryFleetSummary.ts
index f36533de5e..a5be01b82b 100644
--- a/autogpt_platform/frontend/src/app/(platform)/library/hooks/useLibraryFleetSummary.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/library/hooks/useLibraryFleetSummary.ts
@@ -9,7 +9,10 @@ import { AgentExecutionStatus } from "@/app/api/__generated__/models/agentExecut
import type { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";
import type { UserExecutionCostSummary } from "@/app/api/__generated__/models/userExecutionCostSummary";
import { okData } from "@/app/api/helpers";
-import { useExecutionEvents } from "@/hooks/useExecutionEvents";
+import {
+ EXECUTION_REFETCH_THROTTLE_MS,
+ useExecutionEvents,
+} from "@/hooks/useExecutionEvents";
import { useIsBillingEnabled } from "@/hooks/useIsBillingEnabled";
import { useQueryClient } from "@tanstack/react-query";
import { useCallback, useMemo } from "react";
@@ -75,6 +78,7 @@ export function useLibraryFleetSummary(
useExecutionEvents({
graphIds: graphIDs.length > 0 ? graphIDs : undefined,
enabled: graphIDs.length > 0,
+ throttleMs: EXECUTION_REFETCH_THROTTLE_MS,
onExecutionUpdate: handleExecutionUpdate,
});
diff --git a/autogpt_platform/frontend/src/hooks/__tests__/useExecutionEvents.test.tsx b/autogpt_platform/frontend/src/hooks/__tests__/useExecutionEvents.test.tsx
new file mode 100644
index 0000000000..e6e33c678b
--- /dev/null
+++ b/autogpt_platform/frontend/src/hooks/__tests__/useExecutionEvents.test.tsx
@@ -0,0 +1,227 @@
+import { act, renderHook } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
+import type { GraphExecution } from "@/lib/autogpt-server-api/types";
+
+type MessageHandler = (execution: GraphExecution) => void;
+
+const messageHandlers = new Set<MessageHandler>();
+const connectHandlers = new Set<() => void>();
+const disconnectHandlers = new Set<() => void>();
+const subscribeToGraphExecutions = vi.fn(() => Promise.resolve());
+const onWebSocketConnect = vi.fn((handler: () => void) => {
+ connectHandlers.add(handler);
+ handler();
+ return () => connectHandlers.delete(handler);
+});
+const fakeAPI = {
+ onWebSocketConnect,
+ onWebSocketDisconnect: (handler: () => void) => {
+ disconnectHandlers.add(handler);
+ return () => disconnectHandlers.delete(handler);
+ },
+ subscribeToGraphExecutions,
+ connectWebSocket: vi.fn(() => Promise.resolve()),
+ onWebSocketMessage: (_method: string, handler: MessageHandler) => {
+ messageHandlers.add(handler);
+ return () => messageHandlers.delete(handler);
+ },
+};
+
+vi.mock("@/lib/autogpt-server-api/context", () => ({
+ useBackendAPI: () => fakeAPI,
+}));
+
+import { useExecutionEvents } from "../useExecutionEvents";
+
+function makeExecution(graphID: string, id: string) {
+ return { id, graph_id: graphID } as unknown as GraphExecution;
+}
+
+function emit(execution: GraphExecution) {
+ act(() => {
+ messageHandlers.forEach((handler) => handler(execution));
+ });
+}
+
+beforeEach(() => {
+ messageHandlers.clear();
+ connectHandlers.clear();
+ disconnectHandlers.clear();
+ subscribeToGraphExecutions.mockClear();
+ onWebSocketConnect.mockClear();
+});
+
+describe("useExecutionEvents subscriptions", () => {
+ test("a re-render with an equal but new graphIds array does not re-subscribe", () => {
+ const { rerender } = renderHook(
+ ({ graphIds }) => useExecutionEvents({ graphIds }),
+ { initialProps: { graphIds: ["graph-a", "graph-b"] } },
+ );
+ expect(subscribeToGraphExecutions).toHaveBeenCalledTimes(2);
+
+ rerender({ graphIds: ["graph-a", "graph-b"] });
+ rerender({ graphIds: ["graph-a", "graph-b"] });
+
+ expect(onWebSocketConnect).toHaveBeenCalledTimes(1);
+ expect(subscribeToGraphExecutions).toHaveBeenCalledTimes(2);
+ });
+
+ test("a changed graph ID list subscribes again", () => {
+ const { rerender } = renderHook(
+ ({ graphIds }) => useExecutionEvents({ graphIds }),
+ { initialProps: { graphIds: ["graph-a"] } },
+ );
+ rerender({ graphIds: ["graph-a", "graph-b"] });
+
+ expect(subscribeToGraphExecutions.mock.calls).toEqual([
+ ["graph-a"],
+ ["graph-a"],
+ ["graph-b"],
+ ]);
+ });
+
+ test("subscribes again on a new connection after the socket drops", () => {
+ const { rerender } = renderHook(
+ ({ graphIds }) => useExecutionEvents({ graphIds }),
+ { initialProps: { graphIds: ["graph-a", "graph-b"] } },
+ );
+
+ act(() => {
+ disconnectHandlers.forEach((handler) => handler());
+ connectHandlers.forEach((handler) => handler());
+ });
+ rerender({ graphIds: ["graph-a", "graph-b"] });
+
+ expect(subscribeToGraphExecutions.mock.calls).toEqual([
+ ["graph-a"],
+ ["graph-b"],
+ ["graph-a"],
+ ["graph-b"],
+ ]);
+ });
+
+ test("a second connect on the same connection does not subscribe twice", () => {
+ renderHook(() => useExecutionEvents({ graphIds: ["graph-a"] }));
+
+ act(() => {
+ connectHandlers.forEach((handler) => handler());
+ });
+
+ expect(subscribeToGraphExecutions).toHaveBeenCalledTimes(1);
+ });
+
+ test("only events for the subscribed graphs reach the handler", () => {
+ const onExecutionUpdate = vi.fn();
+ renderHook(() =>
+ useExecutionEvents({ graphIds: ["graph-a"], onExecutionUpdate }),
+ );
+
+ emit(makeExecution("graph-b", "run-1"));
+ emit(makeExecution("graph-a", "run-2"));
+
+ expect(onExecutionUpdate.mock.calls).toEqual([
+ [makeExecution("graph-a", "run-2")],
+ ]);
+ });
+});
+
+describe("useExecutionEvents throttleMs", () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ });
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
+ test("without throttleMs every event is delivered", () => {
+ const onExecutionUpdate = vi.fn();
+ renderHook(() =>
+ useExecutionEvents({ graphId: "graph-a", onExecutionUpdate }),
+ );
+
+ for (let index = 0; index < 5; index += 1) {
+ emit(makeExecution("graph-a", `run-${index}`));
+ }
+
+ expect(onExecutionUpdate).toHaveBeenCalledTimes(5);
+ });
+
+ test("delivers the first event at once and the latest one when the window ends", () => {
+ const onExecutionUpdate = vi.fn();
+ renderHook(() =>
+ useExecutionEvents({
+ graphId: "graph-a",
+ throttleMs: 2000,
+ onExecutionUpdate,
+ }),
+ );
+
+ for (let index = 0; index < 10; index += 1) {
+ emit(makeExecution("graph-a", `run-${index}`));
+ }
+ expect(onExecutionUpdate.mock.calls).toEqual([
+ [makeExecution("graph-a", "run-0")],
+ ]);
+
+ act(() => {
+ vi.advanceTimersByTime(1999);
+ });
+ expect(onExecutionUpdate).toHaveBeenCalledTimes(1);
+
+ act(() => {
+ vi.advanceTimersByTime(1);
+ });
+ expect(onExecutionUpdate.mock.calls).toEqual([
+ [makeExecution("graph-a", "run-0")],
+ [makeExecution("graph-a", "run-9")],
+ ]);
+
+ act(() => {
+ vi.advanceTimersByTime(10_000);
+ });
+ expect(onExecutionUpdate).toHaveBeenCalledTimes(2);
+ });
+
+ test("an event after a quiet window is delivered at once", () => {
+ const onExecutionUpdate = vi.fn();
+ renderHook(() =>
+ useExecutionEvents({
+ graphId: "graph-a",
+ throttleMs: 2000,
+ onExecutionUpdate,
+ }),
+ );
+
+ emit(makeExecution("graph-a", "run-1"));
+ act(() => {
+ vi.advanceTimersByTime(2000);
+ });
+ emit(makeExecution("graph-a", "run-2"));
+
+ expect(onExecutionUpdate).toHaveBeenCalledTimes(2);
+ });
+
+ test("a pending event is delivered when the subscription is torn down", () => {
+ const onExecutionUpdate = vi.fn();
+ const { unmount } = renderHook(() =>
+ useExecutionEvents({
+ graphId: "graph-a",
+ throttleMs: 2000,
+ onExecutionUpdate,
+ }),
+ );
+
+ emit(makeExecution("graph-a", "run-1"));
+ emit(makeExecution("graph-a", "run-2"));
+ unmount();
+
+ expect(onExecutionUpdate.mock.calls).toEqual([
+ [makeExecution("graph-a", "run-1")],
+ [makeExecution("graph-a", "run-2")],
+ ]);
+ act(() => {
+ vi.advanceTimersByTime(2000);
+ });
+ expect(onExecutionUpdate).toHaveBeenCalledTimes(2);
+ });
+});
diff --git a/autogpt_platform/frontend/src/hooks/useExecutionEvents.ts b/autogpt_platform/frontend/src/hooks/useExecutionEvents.ts
index ac24396a20..1a88d417ea 100644
--- a/autogpt_platform/frontend/src/hooks/useExecutionEvents.ts
+++ b/autogpt_platform/frontend/src/hooks/useExecutionEvents.ts
@@ -7,10 +7,19 @@ import { useEffect, useRef } from "react";
type ExecutionEventHandler = (execution: GraphExecution) => void;
+/** For handlers that refetch a list: at most one refetch per 2 s. */
+export const EXECUTION_REFETCH_THROTTLE_MS = 2000;
+
interface UseExecutionEventsOptions {
graphId?: GraphID | string | null;
graphIds?: (GraphID | string)[];
enabled?: boolean;
+ /**
+ * Call `onExecutionUpdate` at most once per this many milliseconds: the
+ * first event right away, then the latest one when the window closes.
+ * For handlers that refetch, where only the newest event matters.
+ */
+ throttleMs?: number;
onExecutionUpdate?: ExecutionEventHandler;
}
@@ -22,12 +31,14 @@ interface UseExecutionEventsOptions {
* @param options.graphId - The graph ID to subscribe to (single graph)
* @param options.graphIds - Array of graph IDs to subscribe to (multiple graphs)
* @param options.enabled - Whether the subscription is enabled (default: true)
+ * @param options.throttleMs - Deliver at most one update per this many ms
* @param options.onExecutionUpdate - Callback invoked when an execution is updated
*/
export function useExecutionEvents({
graphId,
graphIds,
enabled = true,
+ throttleMs,
onExecutionUpdate,
}: UseExecutionEventsOptions) {
const api = useBackendAPI();
@@ -37,24 +48,25 @@ export function useExecutionEvents({
onExecutionUpdateRef.current = onExecutionUpdate;
}, [onExecutionUpdate]);
+ // Keyed on the IDs' content, so an equal array built on every render of
+ // the caller does not tear the subscription down and send it again.
+ const idsKey = (graphIds || (graphId ? [graphId] : []))
+ .map((id) => String(id))
+ .join(",");
+
useEffect(() => {
if (!enabled) return;
+ if (idsKey === "") return;
- const idsToSubscribe = graphIds || (graphId ? [graphId] : []);
- if (idsToSubscribe.length === 0) return;
-
- // Normalize IDs to strings for consistent comparison
- const normalizedIds = idsToSubscribe.map((id) => String(id));
+ const normalizedIds = new Set(idsKey.split(","));
const subscribedIds = new Set<string>();
+ const throttle = createThrottle((execution: GraphExecution) => {
+ onExecutionUpdateRef.current?.(execution);
+ }, throttleMs);
const handleExecutionEvent = (execution: GraphExecution) => {
- // Filter by graphIds if provided, using normalized string comparison
- if (normalizedIds.length > 0) {
- const executionGraphId = String(execution.graph_id);
- if (!normalizedIds.includes(executionGraphId)) return;
- }
-
- onExecutionUpdateRef.current?.(execution);
+ if (!normalizedIds.has(String(execution.graph_id))) return;
+ throttle.call(execution);
};
const connectHandler = api.onWebSocketConnect(() => {
@@ -76,6 +88,12 @@ export function useExecutionEvents({
});
});
+ // A new connection starts with no subscriptions on the server, so the
+ // next connect must send them all again.
+ const disconnectHandler = api.onWebSocketDisconnect(() => {
+ subscribedIds.clear();
+ });
+
const messageHandler = api.onWebSocketMessage(
"graph_execution_event",
handleExecutionEvent,
@@ -85,10 +103,47 @@ export function useExecutionEvents({
return () => {
connectHandler();
+ disconnectHandler();
messageHandler();
+ throttle.flush();
// Note: Backend automatically cleans up subscriptions on websocket disconnect
// If IDs change while connected, old subscriptions remain but are filtered client-side
subscribedIds.clear();
};
- }, [api, graphId, graphIds, enabled]);
+ }, [api, idsKey, enabled, throttleMs]);
+}
+
+function createThrottle<T>(fn: (value: T) => void, waitMs?: number) {
+ let timer: ReturnType<typeof setTimeout> | null = null;
+ let pending: { value: T } | null = null;
+
+ function closeWindow() {
+ timer = null;
+ if (!pending) return;
+ const { value } = pending;
+ pending = null;
+ fn(value);
+ timer = setTimeout(closeWindow, waitMs);
+ }
+
+ return {
+ call(value: T) {
+ if (!waitMs) return fn(value);
+ if (timer) {
+ pending = { value };
+ return;
+ }
+ fn(value);
+ timer = setTimeout(closeWindow, waitMs);
+ },
+ /** Delivers a held-back value now, so tearing down never drops one. */
+ flush() {
+ if (timer) clearTimeout(timer);
+ timer = null;
+ if (!pending) return;
+ const { value } = pending;
+ pending = null;
+ fn(value);
+ },
+ };
}
diff --git a/autogpt_platform/frontend/src/hooks/useLibraryAgents/useLibraryAgents.ts b/autogpt_platform/frontend/src/hooks/useLibraryAgents/useLibraryAgents.ts
index f506ccc72b..2ad98c2aa8 100644
--- a/autogpt_platform/frontend/src/hooks/useLibraryAgents/useLibraryAgents.ts
+++ b/autogpt_platform/frontend/src/hooks/useLibraryAgents/useLibraryAgents.ts
@@ -22,7 +22,12 @@ export function useLibraryAgents() {
},
);
- const agents = agentsQueryData ? unpaginate(agentsQueryData, "agents") : [];
+ // Stable between renders, so consumers keyed on it (status maps, the
+ // execution event subscription) only recompute when the data changes.
+ const agents = useMemo(
+ () => (agentsQueryData ? unpaginate(agentsQueryData, "agents") : []),
+ [agentsQueryData],
+ );
// Use agents.length as dependency to avoid recreating map unnecessarily
const agentInfoMap = useMemo(
diff --git a/docs/engineering/frontend-perf-baseline.md b/docs/engineering/frontend-perf-baseline.md
index 295ecbbee1..793d169668 100644
--- a/docs/engineering/frontend-perf-baseline.md
+++ b/docs/engineering/frontend-perf-baseline.md
@@ -389,3 +389,51 @@ Added later than the other tables: app code is `dev` at `a1578ad9a` (2026-10-19)
| `Primitive.div.Slot props:children` | 231 |
| `Primitive.div.SlotClone props:children` | 231 |
| `Text props:children` | 224 |
+
+## library-running-events · baseline — median of 3 runs
+
+Added after the table above, so it was measured on `dev` at `a1578ad9a` (2026-10-19), on the same machine. The websocket is mocked; each run prints what it saw, and all three printed the same: 80 events in 20 s, counted until 2 s after the last one, caused 1,600 `subscribe_graph_executions` frames (all 20 workflows on every event) and 80 `GET /executions` refetches. Each refetch re-renders the whole card grid.
+
+| metric | median | range | spread |
+| --- | --- | --- | --- |
+| commits | 242 | 242 – 242 | 0.0% |
+| total renders | 143,052 | 143,052 – 143,052 | 0.0% |
+| renders per commit | 591.1 | 591.1 – 591.1 | 0.0% |
+| max renders in one commit | 1,768 | 1,768 – 1,768 | 0.0% |
+| script time (ms) | 8,442 | 8,191 – 8,797 | 7.2% |
+| task time (ms) | 10,585 | 10,338 – 10,975 | 6.0% |
+| layout time (ms) | 55 | 55 – 58 | 6.5% |
+| style recalc time (ms) | 160 | 139 – 175 | 22.7% |
+| long tasks (≥ 50 ms) | 80 | 80 – 80 | 0.0% |
+| long task total (ms) | 8,288 | 8,053 – 8,635 | 7.0% |
+| heap used after GC (MB) | 43.5 | 43.5 – 43.5 | 0.1% |
+
+**Top components (median renders)**
+
+| name | renders |
+| --- | --- |
+| `HugeiconsIcon` | 9,760 |
+| `Icon` | 9,760 |
+| `Presence` | 8,642 |
+| `Dialog` | 6,881 |
+| `Text` | 5,440 |
+| `motion.div` | 4,880 |
+| `DialogPortal` | 3,441 |
+| `DialogPortalProvider` | 3,441 |
+| `DialogProvider` | 3,441 |
+| `BaseContent` | 3,440 |
+
+**Top render causes (median renders)**
+
+| name | renders |
+| --- | --- |
+| `HugeiconsIcon parent-same-props<-Icon` | 9,760 |
+| `Presence props:children` | 8,642 |
+| `Icon parent-same-props<-motion.div` | 4,800 |
+| `Dialog props:onOpenChange,children` | 3,441 |
+| `DialogPortal props:children` | 3,441 |
+| `DialogPortalProvider props:children` | 3,441 |
+| `DialogProvider props:children` | 3,441 |
+| `BaseContent props:children` | 3,440 |
+| `DialogWrap props:styling,handleOpen,handleClose,children` | 3,440 |
+| `LinkComponent props:children` | 3,200 |
diff --git a/docs/engineering/frontend-perf-measurement.md b/docs/engineering/frontend-perf-measurement.md
index 5378702f7d..e324a3172f 100644
--- a/docs/engineering/frontend-perf-measurement.md
+++ b/docs/engineering/frontend-perf-measurement.md
@@ -109,6 +109,7 @@ Names come from the component function. Third-party code bundled already minifie
| `builder-drag-running` | Open a 50-node workflow whose run is RUNNING (the page polls it every second) | Drag one node for 2 s with 60 pointer moves | 2 s |
| `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 |
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.