Skip to content

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

fix(frontend/builder): stabilize ReactFlow prop references on the builder canvas (#13750)

Jialong(Bruce) Li541b4ad8271b
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/Flow.tsx b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/Flow.tsx
index 4a40df8780..0c76d823c3 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/Flow.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/Flow.tsx
@@ -6,7 +6,7 @@ import { BuilderChatPanel } from "../../BuilderChatPanel/BuilderChatPanel";
import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";
import { Background, ReactFlow } from "@xyflow/react";
import { parseAsString, useQueryStates } from "nuqs";
-import { useCallback, useMemo } from "react";
+import { MouseEvent, useCallback, useMemo } from "react";
import { useShallow } from "zustand/react/shallow";
import { useGraphStore } from "../../../stores/graphStore";
import { useNodeStore } from "../../../stores/nodeStore";
@@ -31,6 +31,8 @@ import { useFlowRealtime } from "./useFlowRealtime";
import "@xyflow/react/dist/style.css";
import "./flow.css";
+const DELETE_KEY_CODES = ["Backspace", "Delete"];
+
export const Flow = () => {
const [{ flowID, flowExecutionID }] = useQueryStates({
flowID: parseAsString,
@@ -63,6 +65,10 @@ export const Flow = () => {
const nodeTypes = useMemo(() => ({ custom: CustomNode }), []);
const edgeTypes = useMemo(() => ({ custom: CustomEdge }), []);
+ const onNodeContextMenu = useCallback((event: MouseEvent) => {
+ event.preventDefault();
+ }, []);
+
const onNodeDragStop = useCallback(() => {
const currentNodes = useNodeStore.getState().nodes;
setNodes(
@@ -111,9 +117,7 @@ export const Flow = () => {
onConnect={onConnect}
onEdgesChange={onEdgesChange}
onNodeDragStop={onNodeDragStop}
- onNodeContextMenu={(event) => {
- event.preventDefault();
- }}
+ onNodeContextMenu={onNodeContextMenu}
maxZoom={2}
minZoom={0.05}
onDragOver={onDragOver}
@@ -121,7 +125,7 @@ export const Flow = () => {
nodesDraggable={!isLocked}
nodesConnectable={!isLocked}
elementsSelectable={!isLocked}
- deleteKeyCode={["Backspace", "Delete"]}
+ deleteKeyCode={DELETE_KEY_CODES}
>
<Background />
<CustomControls
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/__tests__/Flow.test.tsx b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/__tests__/Flow.test.tsx
index 3dc19d1df5..31eccb6aad 100644
--- a/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/__tests__/Flow.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/FlowEditor/Flow/__tests__/Flow.test.tsx
@@ -33,10 +33,20 @@ vi.mock("../helpers/resolve-collision", () => ({
resolveCollisions: (nodes: unknown) => nodes,
}));
+const mockCapturedReactFlowProps: Record<string, unknown>[] = [];
+
+interface MockReactFlowProps {
+ children: ReactNode;
+ [key: string]: unknown;
+}
+
+function MockReactFlow({ children, ...props }: MockReactFlowProps) {
+ mockCapturedReactFlowProps.push(props);
+ return <div data-testid="react-flow">{children}</div>;
+}
+
vi.mock("@xyflow/react", () => ({
- ReactFlow: ({ children }: { children: ReactNode }) => (
- <div data-testid="react-flow">{children}</div>
- ),
+ ReactFlow: MockReactFlow,
Background: () => <div data-testid="background" />,
useReactFlow: () => ({ zoomIn: vi.fn(), zoomOut: vi.fn(), fitView: vi.fn() }),
}));
@@ -175,3 +185,39 @@ describe("Flow read-only gating", () => {
expect(screen.queryByTestId("trigger-agent-banner")).toBeNull();
});
});
+
+describe("Flow canvas re-render stability", () => {
+ beforeEach(() => {
+ mockIsReadOnly = false;
+ mockHasWebhookNodes = false;
+ mockCapturedReactFlowProps.length = 0;
+ });
+
+ test("deleteKeyCode and onNodeContextMenu keep a stable reference across re-renders", () => {
+ const { rerender } = render(<Flow />);
+ rerender(<Flow />);
+
+ expect(mockCapturedReactFlowProps.length).toBeGreaterThanOrEqual(2);
+ const [firstProps, secondProps] = mockCapturedReactFlowProps;
+
+ // A new array/function identity on every render forces React Flow's
+ // internal effects (e.g. the delete-key listener) to tear down and
+ // re-subscribe on every render, which is a real cause of jank while
+ // panning/zooming/dragging blocks since Flow re-renders on every
+ // node-position update.
+ expect(secondProps.deleteKeyCode).toBe(firstProps.deleteKeyCode);
+ expect(secondProps.onNodeContextMenu).toBe(firstProps.onNodeContextMenu);
+
+ // Reference stability alone doesn't guard the feature: also assert the
+ // value/behaviour so a stable-but-broken handler or key list is caught.
+ expect(firstProps.deleteKeyCode).toEqual(["Backspace", "Delete"]);
+
+ const preventDefault = vi.fn();
+ (
+ firstProps.onNodeContextMenu as (event: {
+ preventDefault: () => void;
+ }) => void
+ )({ preventDefault });
+ expect(preventDefault).toHaveBeenCalled();
+ });
+});