Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-11679): keep CDP-mode Record Browser alive across the transport swap (#7048)
Celal Zamanoğlu7feeefe552b8
diff --git a/skyvern-frontend/src/components/BrowserStream.test.tsx b/skyvern-frontend/src/components/BrowserStream.test.tsxindex 85ed8943e..da0d75fa6 100644--- a/skyvern-frontend/src/components/BrowserStream.test.tsx+++ b/skyvern-frontend/src/components/BrowserStream.test.tsx@@ -1,3 +1,4 @@+import { StrictMode } from "react";import { QueryClient, QueryClientProvider } from "@tanstack/react-query";import {cleanup,@@ -162,6 +163,26 @@ function renderBrowserStream(props: { onActivity?: () => void } = {}) {);}+function renderWithRecordingReset(+ resetRecordingOnUnmount: boolean | undefined,+ { strict = false }: { strict?: boolean } = {},+) {+ const queryClient = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ const tree = (+ <QueryClientProvider client={queryClient}>+ <BrowserStream+ browserSessionId="pbs_test"+ interactive={false}+ showControlButtons={true}+ resetRecordingOnUnmount={resetRecordingOnUnmount}+ />+ </QueryClientProvider>+ );+ return render(strict ? <StrictMode>{tree}</StrictMode> : tree);+}+describe("BrowserStream", () => {beforeEach(() => {Object.defineProperty(globalThis, "WebSocket", {@@ -225,4 +246,44 @@ describe("BrowserStream", () => {expect(onActivity).toHaveBeenCalledTimes(1);});++ describe("recording reset lifecycle", () => {+ it("resets the recording store on unmount by default", async () => {+ const { unmount } = renderWithRecordingReset(undefined);+ await waitFor(() => expect(mocks.rfbInstances).toHaveLength(1));++ expect(mocks.recordingStore.reset).not.toHaveBeenCalled();+ unmount();+ expect(mocks.recordingStore.reset).toHaveBeenCalledTimes(1);+ });++ it("does not reset the recording store on unmount when opted out", async () => {+ const { unmount } = renderWithRecordingReset(false);+ await waitFor(() => expect(mocks.rfbInstances).toHaveLength(1));++ unmount();+ expect(mocks.recordingStore.reset).not.toHaveBeenCalled();+ });++ // Pins that StrictMode's transient unmount really fires the cleanup in this+ // environment, so the opt-out test below cannot pass vacuously.+ it("runs the unmount cleanup on a StrictMode double-mount by default", async () => {+ renderWithRecordingReset(undefined, { strict: true });+ await waitFor(() =>+ expect(mocks.recordingStore.reset).toHaveBeenCalledTimes(1),+ );+ });++ // The local repro: StrictMode remounts the fresh VNC stream (mount -> unmount+ // -> mount) the instant recording starts. The transient unmount must not+ // clear the recording that just began.+ it("survives a StrictMode double-mount when opted out", async () => {+ renderWithRecordingReset(false, { strict: true });+ await waitFor(() =>+ expect(mocks.rfbInstances.length).toBeGreaterThanOrEqual(1),+ );++ expect(mocks.recordingStore.reset).not.toHaveBeenCalled();+ });+ });});diff --git a/skyvern-frontend/src/components/BrowserStream.tsx b/skyvern-frontend/src/components/BrowserStream.tsxindex 4398f0e92..01bd39f4a 100644--- a/skyvern-frontend/src/components/BrowserStream.tsx+++ b/skyvern-frontend/src/components/BrowserStream.tsx@@ -146,6 +146,9 @@ type Props = {exfiltrate?: boolean;interactive?: boolean;showControlButtons?: boolean;+ // Whether unmounting clears the recording store. The studio passes false: it+ // remounts this component across CDP<->VNC swaps without the session ending.+ resetRecordingOnUnmount?: boolean;task?: {run: TaskApiResponse;};@@ -250,6 +253,7 @@ function BrowserStream({exfiltrate = false,interactive = true,showControlButtons = undefined,+ resetRecordingOnUnmount = true,task = undefined,workflow = undefined,resizeTrigger,@@ -933,15 +937,20 @@ function BrowserStream({};}, [canvasContainer]);- // Reset the recording store when the stream unmounts — including mid-recording.- // The stream never unmounts during a live recording flow (the studio keeps it- // in a detached host across pane changes; the editor keeps it mounted while- // recording), so an unmount means the user abandoned the session (navigated- // away / switched workflows) and stale isRecording state would otherwise leak- // into the next mounted workflow as a stuck recording panel.+ // Read the flag through a ref so the unmount cleanup stays mount-scoped: a+ // StrictMode double-mount or transport swap must not cancel a live recording.+ const resetRecordingOnUnmountRef = useRef(resetRecordingOnUnmount);+ resetRecordingOnUnmountRef.current = resetRecordingOnUnmount;+ // By default an unmount means the user abandoned the session, and the reset+ // keeps stale isRecording from leaking into the next mounted workflow as a+ // stuck recording panel. Surfaces that remount the stream while the session+ // lives on (transport swaps, per-run streams) opt out and reset at the+ // session level instead.useEffect(() => {return () => {- useRecordingStore.getState().reset();+ if (resetRecordingOnUnmountRef.current) {+ useRecordingStore.getState().reset();+ }};}, []);diff --git a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsxindex f0d7c2b8e..ba5fdafb9 100644--- a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx+++ b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx@@ -725,6 +725,17 @@ function Workspace({(!activeDebugSession || activeDebugSession.vnc_streaming_supported);const showCdpBrowserPanel =isCdpStreamingMode && shouldFetchDebugSession && !isRateLimited;+ // Recording is session-scoped: the stream opts out of reset-on-unmount (it+ // remounts across the CDP<->VNC recording swap mid-session), so the debug+ // session owns the reset here — embedded, StudioBrowserStream owns it. The id+ // guard keeps the null -> first-id transition from clearing a recording that+ // started before the session resolved.+ useEffect(() => {+ if (embedded || !liveBrowserSessionId) {+ return;+ }+ return () => useRecordingStore.getState().reset();+ }, [embedded, liveBrowserSessionId]);// Embedded: the shell owns the stream, so bind the copilot once the backend// session exists — else it gets a null id and the backend spins a separate browser.const copilotRequiresLiveBrowser =@@ -2419,6 +2430,9 @@ function Workspace({// The recording panel overlays the canvas whenever a// recording is live here, so the REC pill is redundant.hideRecordingIndicator={true}+ // Remounts across the CDP<->VNC recording swap; the+ // debug session owns the recording reset.+ resetRecordingOnUnmount={false}resizeTrigger={windowResizeTrigger}isExecuting={!!workflowRun && !isFinalized}onReadyChange={handleLiveBrowserReadyChange}diff --git a/skyvern-frontend/src/routes/workflows/studio/StreamPresenter.test.tsx b/skyvern-frontend/src/routes/workflows/studio/StreamPresenter.test.tsxnew file mode 100644index 000000000..5dfac9cca--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/studio/StreamPresenter.test.tsx@@ -0,0 +1,59 @@+// @vitest-environment jsdom++import { cleanup, render, screen } from "@testing-library/react";+import { afterEach, describe, expect, it, vi } from "vitest";++import { StreamPresenter } from "./StreamPresenter";++const runtimeConfigMock = vi.hoisted(() => ({+ browserStreamingMode: "cdp" as "cdp" | "vnc",+}));++const browserStreamProps = vi.hoisted(+ () =>+ ({ last: null }) as { last: { resetRecordingOnUnmount?: boolean } | null },+);++vi.mock("@/hooks/useRuntimeConfig", () => ({+ useBrowserStreamingMode: () => ({+ browserStreamingMode: runtimeConfigMock.browserStreamingMode,+ }),+}));++vi.mock("@/components/BrowserStream", () => ({+ BrowserStream: (props: { resetRecordingOnUnmount?: boolean }) => {+ browserStreamProps.last = props;+ return <div data-testid="vnc-stream" />;+ },+}));++vi.mock("@/routes/browserSessions/BrowserSessionStream", () => ({+ BrowserSessionStream: () => <div data-testid="cdp-stream" />,+}));++describe("StreamPresenter transport-swap recording", () => {+ afterEach(() => {+ cleanup();+ browserStreamProps.last = null;+ runtimeConfigMock.browserStreamingMode = "cdp";+ });++ it("shows the CDP stream when not recording in cdp mode", () => {+ render(<StreamPresenter browserSessionId="pbs_test" />);+ expect(screen.queryByTestId("cdp-stream")).not.toBeNull();+ expect(screen.queryByTestId("vnc-stream")).toBeNull();+ });++ it("swaps to the VNC stream and opts it out of the unmount reset when recording starts", () => {+ const { rerender } = render(+ <StreamPresenter browserSessionId="pbs_test" isRecording={false} />,+ );+ expect(screen.queryByTestId("cdp-stream")).not.toBeNull();++ // Recording forces VNC: the CDP stream unmounts, the fresh VNC stream mounts.+ rerender(<StreamPresenter browserSessionId="pbs_test" isRecording />);+ expect(screen.queryByTestId("vnc-stream")).not.toBeNull();+ expect(screen.queryByTestId("cdp-stream")).toBeNull();+ expect(browserStreamProps.last?.resetRecordingOnUnmount).toBe(false);+ });+});diff --git a/skyvern-frontend/src/routes/workflows/studio/StreamPresenter.tsx b/skyvern-frontend/src/routes/workflows/studio/StreamPresenter.tsxindex fe69a9d5f..318453927 100644--- a/skyvern-frontend/src/routes/workflows/studio/StreamPresenter.tsx+++ b/skyvern-frontend/src/routes/workflows/studio/StreamPresenter.tsx@@ -51,6 +51,10 @@ export function StreamPresenter({showControlButtons={showControlButtons}exfiltrate={isRecording}hideRecordingIndicator={hideRecordingIndicator}+ // The studio swaps CDP<->VNC when recording toggles and StrictMode remounts+ // this component; the recording must survive those. StudioBrowserStream owns+ // the session-level reset instead.+ resetRecordingOnUnmount={false}onActivity={onActivity}/>);diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioBrowserStream.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioBrowserStream.tsxindex cfdb8f463..e87d8a6f5 100644--- a/skyvern-frontend/src/routes/workflows/studio/StudioBrowserStream.tsx+++ b/skyvern-frontend/src/routes/workflows/studio/StudioBrowserStream.tsx@@ -16,6 +16,7 @@ export function StudioBrowserStream() {const { panes } = useStudioPanes();const browserPaneOpen = panes.includes("browser");const isRecording = useRecordingStore((s) => s.isRecording);+ const resetRecording = useRecordingStore((s) => s.reset);const reloadNonce = useStudioBrowserStore((s) => s.reloadNonce);const setStreamUrl = useStudioBrowserStore((s) => s.setStreamUrl);const markActivity = useStudioBrowserStore((s) => s.markActivity);@@ -38,6 +39,13 @@ export function StudioBrowserStream() {return () => reset();}, [browserSessionId, reset]);+ // Recording is session-scoped: clear it when the studio's browser session ends+ // or changes. The transport stream no longer resets on unmount (it remounts+ // across CDP<->VNC swaps without the session ending), so this owns that.+ useEffect(() => {+ return () => resetRecording();+ }, [browserSessionId, resetRecording]);+useEffect(() => {if (browserPaneOpen) {clearActivity();diff --git a/skyvern-frontend/src/routes/workflows/studio/runview/RunLiveStream.tsx b/skyvern-frontend/src/routes/workflows/studio/runview/RunLiveStream.tsxindex a0687e350..8842f95dc 100644--- a/skyvern-frontend/src/routes/workflows/studio/runview/RunLiveStream.tsx+++ b/skyvern-frontend/src/routes/workflows/studio/runview/RunLiveStream.tsx@@ -40,6 +40,9 @@ export function RunLiveStream({browserSessionId={browserSessionId}interactive={interactive}showControlButtons={interactive}+ // A recording can be live while this per-run stream mounts and unmounts;+ // StudioBrowserStream owns the session-level reset.+ resetRecordingOnUnmount={false}onClose={() => setVncFailed(true)}/>);