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): batch undo history for cascading operations (#12344)
Dreamd0a1d72e8a91
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/stores/historyStore.ts b/autogpt_platform/frontend/src/app/(platform)/build/stores/historyStore.tsindex 3a67bb8dcd..5f1b2ee774 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/stores/historyStore.ts+++ b/autogpt_platform/frontend/src/app/(platform)/build/stores/historyStore.ts@@ -25,34 +25,60 @@ type HistoryStore = {const MAX_HISTORY = 50;+// Microtask batching state — kept outside the store to avoid triggering+// re-renders. When multiple pushState calls happen in the same synchronous+// execution (e.g. node deletion cascading to edge cleanup), only the first+// (pre-change) state is kept and committed as a single history entry.+let pendingState: HistoryState | null = null;+let batchScheduled = false;+export const useHistoryStore = create<HistoryStore>((set, get) => ({past: [{ nodes: [], edges: [] }],future: [],pushState: (state: HistoryState) => {- const { past } = get();- const lastState = past[past.length - 1];-- if (lastState && isEqual(lastState, state)) {- return;+ // Keep only the first state within a microtask batch — it represents+ // the true pre-change snapshot before any cascading mutations.+ if (!pendingState) {+ pendingState = state;}- const actualCurrentState = {- nodes: useNodeStore.getState().nodes,- edges: useEdgeStore.getState().edges,- };+ if (!batchScheduled) {+ batchScheduled = true;+ queueMicrotask(() => {+ const stateToCommit = pendingState;+ pendingState = null;+ batchScheduled = false;- if (isEqual(state, actualCurrentState)) {- return;- }+ if (!stateToCommit) return;- set((prev) => ({- past: [...prev.past.slice(-MAX_HISTORY + 1), state],- future: [],- }));+ const { past } = get();+ const lastState = past[past.length - 1];++ if (lastState && isEqual(lastState, stateToCommit)) {+ return;+ }++ const actualCurrentState = {+ nodes: useNodeStore.getState().nodes,+ edges: useEdgeStore.getState().edges,+ };++ if (isEqual(stateToCommit, actualCurrentState)) {+ return;+ }++ set((prev) => ({+ past: [...prev.past.slice(-MAX_HISTORY + 1), stateToCommit],+ future: [],+ }));+ });+ }},initializeHistory: () => {+ pendingState = null;+const currentNodes = useNodeStore.getState().nodes;const currentEdges = useEdgeStore.getState().edges;@@ -122,5 +148,8 @@ export const useHistoryStore = create<HistoryStore>((set, get) => ({},canRedo: () => get().future.length > 0,- clear: () => set({ past: [{ nodes: [], edges: [] }], future: [] }),+ clear: () => {+ pendingState = null;+ set({ past: [{ nodes: [], edges: [] }], future: [] });+ },}));