Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix: prevent false-positive "save changes" dialog in workflow editor (#SKY-8160) (#5006)
Celal Zamanoğlud31e86085c91
diff --git a/skyvern-frontend/src/components/GlobalNotificationListener.tsx b/skyvern-frontend/src/components/GlobalNotificationListener.tsxindex aafaa340b..ad0bd7b5d 100644--- a/skyvern-frontend/src/components/GlobalNotificationListener.tsx+++ b/skyvern-frontend/src/components/GlobalNotificationListener.tsx@@ -3,6 +3,7 @@ import {useNotificationStream,type VerificationRequest,} from "@/hooks/useNotificationStream";+import { enable2faNotifications } from "@/util/env";function notificationLabel(req: VerificationRequest): string {if (req.task_id) return `Task "${req.task_id}"`;@@ -34,6 +35,11 @@ function VerificationCodeHandler({ req }: { req: VerificationRequest }) {}function GlobalNotificationListener() {+ if (!enable2faNotifications) return null;+ return <GlobalNotificationListenerInner />;+}++function GlobalNotificationListenerInner() {const { verificationRequests } = useNotificationStream();if (verificationRequests.length === 0) return null;diff --git a/skyvern-frontend/src/hooks/useVerificationCodeAlert.ts b/skyvern-frontend/src/hooks/useVerificationCodeAlert.tsindex 3fff5389f..8fa6df206 100644--- a/skyvern-frontend/src/hooks/useVerificationCodeAlert.ts+++ b/skyvern-frontend/src/hooks/useVerificationCodeAlert.ts@@ -1,6 +1,7 @@import { useState, useEffect, useRef, createElement } from "react";import { toast } from "@/components/ui/use-toast";import { VerificationToastContent } from "@/components/VerificationToast";+import { enable2faNotifications } from "@/util/env";/** How long (minutes) before we consider the 2FA request timed out */const VERIFICATION_TIMEOUT_MINS = 15;@@ -81,6 +82,7 @@ function useVerificationCodeAlert({// Browser notification + in-app toast (fire once per waiting transition)useEffect(() => {+ if (!enable2faNotifications) return;if (!isWaitingForCode || notifiedTags.has(notificationTag)) return;notifiedTags.add(notificationTag);diff --git a/skyvern-frontend/src/routes/root/RootLayout.tsx b/skyvern-frontend/src/routes/root/RootLayout.tsxindex 2f1b726e2..d97a14518 100644--- a/skyvern-frontend/src/routes/root/RootLayout.tsx+++ b/skyvern-frontend/src/routes/root/RootLayout.tsx@@ -7,7 +7,6 @@ import { Sidebar } from "./Sidebar";import { useDebugStore } from "@/store/useDebugStore";import { GlobalNotificationListener } from "@/components/GlobalNotificationListener";import { SelfHealApiKeyBanner } from "@/components/SelfHealApiKeyBanner";-import { enable2faNotifications } from "@/util/env.ts";function RootLayout() {const collapsed = useSidebarStore((state) => state.collapsed);@@ -26,7 +25,7 @@ function RootLayout() {<div className="h-full w-full"><div className={horizontalPadding}><SelfHealApiKeyBanner />- {enable2faNotifications && <GlobalNotificationListener />}+ <GlobalNotificationListener /></div><Header /><maindiff --git a/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx b/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsxindex 81ac66272..b0f680734 100644--- a/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx+++ b/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx@@ -100,6 +100,13 @@ import { toast } from "@/components/ui/use-toast";import { useAutoPan } from "./useAutoPan";import { useAutoGenerateWorkflowTitle } from "../hooks/useAutoGenerateWorkflowTitle";+// Grace period after nodesInitialized before we start tracking changes.+// Allows mount-time effects (ResizeObserver, visibility toggling) to settle.+// Async API calls (e.g. WorkflowTriggerNode title hydration) are separately+// protected by beginInternalUpdate/endInternalUpdate guards, so this timeout+// only needs to cover synchronous mount-time effects.+const INITIAL_LOAD_SETTLE_MS = 500;+function convertToParametersYAML(parameters: ParametersState,): Array<@@ -342,8 +349,14 @@ function FlowRenderer({useEffect(() => {if (nodesInitialized) {setShouldConstrainPan(true);- // Mark initial load as complete after nodes are initialized- isInitialLoadRef.current = false;+ // Delay marking initial load as complete to allow mount-time effects+ // (ResizeObserver, async data fetches, visibility toggling) to settle+ // before we start tracking changes. Must be long enough for async+ // effects but short enough that users won't notice.+ const timer = setTimeout(() => {+ isInitialLoadRef.current = false;+ }, INITIAL_LOAD_SETTLE_MS);+ return () => clearTimeout(timer);}}, [nodesInitialized]);@@ -972,11 +985,11 @@ function FlowRenderer({// Only track changes after initial load is complete and not during internal updates// (e.g., switching conditional branches which is UI state, not workflow data)// Use getState() to get real-time value (not stale closure from render time)- const isInternalUpdate =- useWorkflowHasChangesStore.getState().isInternalUpdate;+ const internalUpdateCount =+ useWorkflowHasChangesStore.getState().internalUpdateCount;if (!isInitialLoadRef.current &&- !isInternalUpdate &&+ internalUpdateCount === 0 &&changes.some((change) => {return (change.type === "add" ||diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/ConditionalNode/ConditionalNode.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/ConditionalNode/ConditionalNode.tsxindex f01305997..891de42dc 100644--- a/skyvern-frontend/src/routes/workflows/editor/nodes/ConditionalNode/ConditionalNode.tsx+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/ConditionalNode/ConditionalNode.tsx@@ -1,4 +1,4 @@-import { useEffect, useMemo } from "react";+import { useCallback, useEffect, useMemo, useRef } from "react";import {Handle,NodeProps,@@ -45,7 +45,22 @@ function ConditionalNodeComponent({ id, data }: NodeProps<ConditionalNode>) {const nodes = useNodes<AppNode>();const { setNodes, setEdges } = useReactFlow();const node = nodes.find((n) => n.id === id);- const { setIsInternalUpdate } = useWorkflowHasChangesStore();+ const { beginInternalUpdate, endInternalUpdate } =+ useWorkflowHasChangesStore();+ // Track pending endInternalUpdate timer from handleSelectBranch so we can+ // clean it up on unmount and prevent a stuck counter.+ const branchSelectTimerRef = useRef<ReturnType<typeof setTimeout> | null>(+ null,+ );+ useEffect(() => {+ return () => {+ if (branchSelectTimerRef.current !== null) {+ clearTimeout(branchSelectTimerRef.current);+ endInternalUpdate();+ branchSelectTimerRef.current = null;+ }+ };+ }, [endInternalUpdate]);const update = useUpdate<ConditionalNodeData>({id,@@ -112,16 +127,30 @@ function ConditionalNodeComponent({ id, data }: NodeProps<ConditionalNode>) {useEffect(() => {if (!data.activeBranchId && orderedBranches.length > 0) {// Mark as internal update to prevent triggering "unsaved changes" dialog- setIsInternalUpdate(true);+ beginInternalUpdate();update({activeBranchId: orderedBranches[0]?.id ?? null,});// Clear the flag after layout completes- setTimeout(() => {- setIsInternalUpdate(false);+ let ended = false;+ const timer = setTimeout(() => {+ ended = true;+ endInternalUpdate();}, 50);+ return () => {+ clearTimeout(timer);+ if (!ended) {+ endInternalUpdate();+ }+ };}- }, [data.activeBranchId, orderedBranches, update, setIsInternalUpdate]);+ }, [+ data.activeBranchId,+ orderedBranches,+ update,+ beginInternalUpdate,+ endInternalUpdate,+ ]);// Toggle visibility of branch nodes/edges when activeBranchId changesuseEffect(() => {@@ -129,6 +158,9 @@ function ConditionalNodeComponent({ id, data }: NodeProps<ConditionalNode>) {return;}+ // Mark as internal update to prevent triggering "unsaved changes" dialog+ beginInternalUpdate();+const activeBranchId = data.activeBranchId;let updatedNodesSnapshot: Array<AppNode> = [];@@ -229,7 +261,27 @@ function ConditionalNodeComponent({ id, data }: NodeProps<ConditionalNode>) {setTimeout(() => {window.dispatchEvent(new CustomEvent("conditional-branch-changed"));}, 0);- }, [data.activeBranchId, id, setNodes, setEdges]);++ // Clear the internal update flag after changes propagate+ let ended = false;+ const timer = setTimeout(() => {+ ended = true;+ endInternalUpdate();+ }, 50);+ return () => {+ clearTimeout(timer);+ if (!ended) {+ endInternalUpdate();+ }+ };+ }, [+ data.activeBranchId,+ id,+ setNodes,+ setEdges,+ beginInternalUpdate,+ endInternalUpdate,+ ]);const handleAddCondition = () => {if (!data.editable) {@@ -275,19 +327,29 @@ function ConditionalNodeComponent({ id, data }: NodeProps<ConditionalNode>) {});};- const handleSelectBranch = (branchId: string) => {- if (!data.editable) {- return;- }- // Mark as internal update to prevent triggering "unsaved changes" dialog- // Switching branches is UI state, not actual workflow data changes- setIsInternalUpdate(true);- update({ activeBranchId: branchId });- // Clear the flag after layout completes (layout uses setTimeout(10))- setTimeout(() => {- setIsInternalUpdate(false);- }, 50);- };+ const handleSelectBranch = useCallback(+ (branchId: string) => {+ if (!data.editable) {+ return;+ }+ // Mark as internal update to prevent triggering "unsaved changes" dialog+ // Switching branches is UI state, not actual workflow data changes+ // Cancel any pending timer from a previous rapid call to keep the counter balanced+ if (branchSelectTimerRef.current !== null) {+ clearTimeout(branchSelectTimerRef.current);+ endInternalUpdate();+ }+ beginInternalUpdate();+ update({ activeBranchId: branchId });+ // Clear the flag after layout completes (layout uses setTimeout(10))+ // Store timer in ref so it can be cleaned up on unmount+ branchSelectTimerRef.current = setTimeout(() => {+ branchSelectTimerRef.current = null;+ endInternalUpdate();+ }, 50);+ },+ [data.editable, beginInternalUpdate, update, endInternalUpdate],+ );const handleRemoveBranch = (branchId: string) => {if (!data.editable) {diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/LoopNode/LoopNode.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/LoopNode/LoopNode.tsxindex 0f48e207d..1d1b4a97f 100644--- a/skyvern-frontend/src/routes/workflows/editor/nodes/LoopNode/LoopNode.tsx+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/LoopNode/LoopNode.tsx@@ -47,7 +47,7 @@ function LoopNode({ id, data }: NodeProps<LoopNode>) {const recordingStore = useRecordingStore();const headerRef = useRef<HTMLDivElement>(null);const { updateNodeData } = useReactFlow();- const lastHeaderHeight = useRef<number | undefined>(undefined);+ const lastHeaderHeight = useRef<number | undefined>(data._headerHeight);useEffect(() => {const el = headerRef.current;diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/WorkflowTriggerNode/WorkflowTriggerNode.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/WorkflowTriggerNode/WorkflowTriggerNode.tsxindex 1f9da9280..19125bc82 100644--- a/skyvern-frontend/src/routes/workflows/editor/nodes/WorkflowTriggerNode/WorkflowTriggerNode.tsx+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/WorkflowTriggerNode/WorkflowTriggerNode.tsx@@ -29,6 +29,7 @@ import { useParams } from "react-router-dom";import { statusIsRunningOrQueued } from "@/routes/tasks/types";import { useWorkflowRunQuery } from "@/routes/workflows/hooks/useWorkflowRunQuery";import { useRecordingStore } from "@/store/useRecordingStore";+import { useWorkflowHasChangesStore } from "@/store/WorkflowHasChangesStore";import { cn } from "@/util/utils";import { BlockExecutionOptions } from "../components/BlockExecutionOptions";@@ -64,6 +65,8 @@ function WorkflowTriggerNode({ id, data }: NodeProps<WorkflowTriggerNodeType>) {const update = useUpdate<WorkflowTriggerNodeType["data"]>({ id, editable });const recordingStore = useRecordingStore();+ const { beginInternalUpdate, endInternalUpdate } =+ useWorkflowHasChangesStore();const {workflowParameters,@@ -72,11 +75,30 @@ function WorkflowTriggerNode({ id, data }: NodeProps<WorkflowTriggerNodeType>) {} = useTargetWorkflowParametersQuery(data.workflowPermanentId);// Hydrate title from fetched workflow data (single API call, no duplicate)+ // Mark as internal update to prevent triggering "unsaved changes" dialoguseEffect(() => {if (fetchedTitle && fetchedTitle !== data.workflowTitle) {+ beginInternalUpdate();update({ workflowTitle: fetchedTitle });+ let ended = false;+ const timer = setTimeout(() => {+ ended = true;+ endInternalUpdate();+ }, 50);+ return () => {+ clearTimeout(timer);+ if (!ended) {+ endInternalUpdate();+ }+ };}- }, [fetchedTitle, data.workflowTitle, update]);+ }, [+ fetchedTitle,+ data.workflowTitle,+ update,+ beginInternalUpdate,+ endInternalUpdate,+ ]);const hasWorkflowSelected = isConcreteWpid(data.workflowPermanentId);diff --git a/skyvern-frontend/src/store/WorkflowHasChangesStore.ts b/skyvern-frontend/src/store/WorkflowHasChangesStore.tsindex c1251f7ff..1fdf74dd2 100644--- a/skyvern-frontend/src/store/WorkflowHasChangesStore.ts+++ b/skyvern-frontend/src/store/WorkflowHasChangesStore.ts@@ -31,13 +31,16 @@ type WorkflowHasChangesStore = {saveIsPending: boolean;saidOkToCodeCacheDeletion: boolean;showConfirmCodeCacheDeletion: boolean;- isInternalUpdate: boolean;+ // Reference-counted flag: multiple concurrent internal updates won't+ // accidentally clear each other. Gate on > 0 in consumers.+ internalUpdateCount: number;setGetSaveData: (getSaveData: () => SaveData) => void;setHasChanges: (hasChanges: boolean) => void;setSaveIsPending: (isPending: boolean) => void;setSaidOkToCodeCacheDeletion: (saidOkToCodeCacheDeletion: boolean) => void;setShowConfirmCodeCacheDeletion: (show: boolean) => void;- setIsInternalUpdate: (isInternalUpdate: boolean) => void;+ beginInternalUpdate: () => void;+ endInternalUpdate: () => void;};interface WorkflowSaveOpts {@@ -50,7 +53,7 @@ const useWorkflowHasChangesStore = create<WorkflowHasChangesStore>((set) => {saveIsPending: false,saidOkToCodeCacheDeletion: false,showConfirmCodeCacheDeletion: false,- isInternalUpdate: false,+ internalUpdateCount: 0,getSaveData: () => null,setGetSaveData: (getSaveData: () => SaveData) => {set({ getSaveData });@@ -67,8 +70,13 @@ const useWorkflowHasChangesStore = create<WorkflowHasChangesStore>((set) => {setShowConfirmCodeCacheDeletion: (show: boolean) => {set({ showConfirmCodeCacheDeletion: show });},- setIsInternalUpdate: (isInternalUpdate: boolean) => {- set({ isInternalUpdate });+ beginInternalUpdate: () => {+ set((state) => ({ internalUpdateCount: state.internalUpdateCount + 1 }));+ },+ endInternalUpdate: () => {+ set((state) => ({+ internalUpdateCount: Math.max(0, state.internalUpdateCount - 1),+ }));},};});