Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-9113): skip ffmpeg remux on per-step video sync (#5559)
Andrew Neilsone63689c9810a
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.pyindex 3e1617de9..52b684040 100644--- a/skyvern/forge/agent.py+++ b/skyvern/forge/agent.py@@ -2482,7 +2482,7 @@ class ForgeAgent:returntry:video_artifacts = await app.BROWSER_MANAGER.get_video_artifacts(- task_id=task.task_id, browser_state=browser_state+ task_id=task.task_id, browser_state=browser_state, finalize=False)for video_artifact in video_artifacts:await app.ARTIFACT_MANAGER.update_artifact_data(@@ -2641,10 +2641,12 @@ class ForgeAgent:task=task,browser_session_id=browser_session_id,)- # Initialize video artifact for the task here, afterwards it'll only get updated+ # Initialize video artifact for the task here, afterwards it'll only get updated.+ # The recording file is still open here, so skip the ffmpeg remux — matches the+ # per-step sync path; the finalized upload happens in cleanup_and_persist_task.if browser_state and browser_state.browser_artifacts:video_artifacts = await app.BROWSER_MANAGER.get_video_artifacts(- task_id=task.task_id, browser_state=browser_state+ task_id=task.task_id, browser_state=browser_state, finalize=False)for idx, video_artifact in enumerate(video_artifacts):if video_artifact.video_artifact_id:@@ -3980,9 +3982,11 @@ class ForgeAgent:task.organization_id,)if browser_state:- # Update recording artifact after closing the browser, so we can get an accurate recording+ # Only remux via ffmpeg when the browser was actually closed — otherwise+ # the recording file is still open (persistent/remote sessions) and the+ # remux would fail on the partial container anyway.video_artifacts = await app.BROWSER_MANAGER.get_video_artifacts(- task_id=task.task_id, browser_state=browser_state+ task_id=task.task_id, browser_state=browser_state, finalize=close_browser_on_completion)LOG.debug("Uploading video artifacts", number_of_video_artifacts=len(video_artifacts))for video_artifact in video_artifacts:diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.pyindex 7d05809ba..29bab3583 100644--- a/skyvern/forge/sdk/workflow/service.py+++ b/skyvern/forge/sdk/workflow/service.py@@ -4600,7 +4600,9 @@ class WorkflowService:child_workflow_run_ids=child_workflow_run_ids,)if browser_state:- await self.persist_video_data(browser_state, workflow, workflow_run)+ await self.persist_video_data(+ browser_state, workflow, workflow_run, close_browser_on_completion=close_browser_on_completion+ )if tasks:await self.persist_debug_artifacts(browser_state, tasks[-1], workflow, workflow_run)# Skip workflow-scoped session save when using browser_profile_id to avoid conflicts@@ -4786,13 +4788,20 @@ class WorkflowService:) from easync def persist_video_data(- self, browser_state: BrowserState, workflow: Workflow, workflow_run: WorkflowRun+ self,+ browser_state: BrowserState,+ workflow: Workflow,+ workflow_run: WorkflowRun,+ close_browser_on_completion: bool = True,) -> None:- # Create recording artifact after closing the browser, so we can get an accurate recording+ # Only remux via ffmpeg when the browser was actually closed — otherwise+ # the recording file is still open (persistent sessions, shared browser,+ # remote address) and the remux would fail on the partial container.video_artifacts = await app.BROWSER_MANAGER.get_video_artifacts(workflow_id=workflow.workflow_id,workflow_run_id=workflow_run.workflow_run_id,browser_state=browser_state,+ finalize=close_browser_on_completion,)LOG.debug("Persisting video data", number_of_video_artifacts=len(video_artifacts))for video_artifact in video_artifacts:diff --git a/skyvern/services/script_service.py b/skyvern/services/script_service.pyindex 4da86f0fe..aa06af5a3 100644--- a/skyvern/services/script_service.py+++ b/skyvern/services/script_service.py@@ -567,8 +567,10 @@ async def _create_video_artifact(if not browser_state:return Noneif browser_state.browser_artifacts:+ # Recording file is still open during block execution — skip the ffmpeg remux;+ # the finalized upload happens after browser_context.close() in cleanup.video_artifacts = await app.BROWSER_MANAGER.get_video_artifacts(- task_id=task.task_id, browser_state=browser_state+ task_id=task.task_id, browser_state=browser_state, finalize=False)for idx, video_artifact in enumerate(video_artifacts):if video_artifact.video_artifact_id:diff --git a/skyvern/webeye/browser_manager.py b/skyvern/webeye/browser_manager.pyindex 6b36e0271..b6cd40805 100644--- a/skyvern/webeye/browser_manager.py+++ b/skyvern/webeye/browser_manager.py@@ -69,6 +69,7 @@ class BrowserManager(Protocol):task_id: str = "",workflow_id: str = "",workflow_run_id: str = "",+ finalize: bool = True,) -> list[VideoArtifact]: ...async def get_har_data(diff --git a/skyvern/webeye/real_browser_manager.py b/skyvern/webeye/real_browser_manager.pyindex 60b102d8e..eae11412b 100644--- a/skyvern/webeye/real_browser_manager.py+++ b/skyvern/webeye/real_browser_manager.py@@ -280,6 +280,7 @@ class RealBrowserManager(BrowserManager):task_id: str = "",workflow_id: str = "",workflow_run_id: str = "",+ finalize: bool = True,) -> list[VideoArtifact]:if len(browser_state.browser_artifacts.video_artifacts) == 0:LOG.warning(@@ -293,9 +294,15 @@ class RealBrowserManager(BrowserManager):for i, video_artifact in enumerate(browser_state.browser_artifacts.video_artifacts):path = video_artifact.video_pathif path and os.path.exists(path=path):- # Remux via ffmpeg so the WebM container has a valid Duration + Cues,- # even when browser_context.close() was killed mid-finalization.- browser_state.browser_artifacts.video_artifacts[i].video_data = await finalize_webm(path)+ if finalize:+ # Remux via ffmpeg so the WebM container has a valid Duration + Cues,+ # even when browser_context.close() was killed mid-finalization.+ browser_state.browser_artifacts.video_artifacts[i].video_data = await finalize_webm(path)+ else:+ # Per-step snapshot while recording is still open — skip ffmpeg: the file is+ # partial, so remux would either fail or be thrown away by the final pass.+ with open(path, "rb") as f:+ browser_state.browser_artifacts.video_artifacts[i].video_data = f.read()else:LOG.debug("Video path not found",diff --git a/tests/unit/test_real_browser_manager.py b/tests/unit/test_real_browser_manager.pyindex 3d9ceb069..00c091294 100644--- a/tests/unit/test_real_browser_manager.py+++ b/tests/unit/test_real_browser_manager.py@@ -131,3 +131,44 @@ async def test_non_pbs_workflow_run_inherits_parent_browser() -> None:# Both entries should be syncedassert manager.pages["wfr_child"] is parent_stateassert manager.pages["wfr_parent"] is parent_state+++def _make_browser_state_with_video(video_path: str) -> MagicMock:+ video_artifact = MagicMock()+ video_artifact.video_path = video_path+ video_artifact.video_data = None+ browser_state = MagicMock()+ browser_state.browser_artifacts.video_artifacts = [video_artifact]+ return browser_state+++async def test_get_video_artifacts_finalize_true_invokes_ffmpeg(tmp_path) -> None:+ """The default (finalize=True) path remuxes via ffmpeg so the final upload has Duration + Cues."""+ src = tmp_path / "recording.webm"+ src.write_bytes(b"raw-webm-bytes")+ browser_state = _make_browser_state_with_video(str(src))++ with patch("skyvern.webeye.real_browser_manager.finalize_webm", new=AsyncMock(return_value=b"remuxed")) as m:+ artifacts = await RealBrowserManager().get_video_artifacts(browser_state=browser_state)++ m.assert_awaited_once_with(str(src))+ assert artifacts[0].video_data == b"remuxed"+++async def test_get_video_artifacts_finalize_false_skips_ffmpeg(tmp_path) -> None:+ """finalize=False is the per-step-snapshot path: read raw bytes, never spawn ffmpeg.++ This is what prevents long browser tasks from firing one ffmpeg subprocess per step+ (the step-sync runs while the recording file is still open — remux is pointless there).+ """+ src = tmp_path / "recording.webm"+ src.write_bytes(b"partial-webm-bytes")+ browser_state = _make_browser_state_with_video(str(src))++ with patch("skyvern.webeye.real_browser_manager.finalize_webm", new=AsyncMock()) as m:+ artifacts = await RealBrowserManager().get_video_artifacts(browser_state=browser_state, finalize=False)++ m.assert_not_awaited()+ assert artifacts[0].video_data == b"partial-webm-bytes"diff --git a/tests/unit/test_sync_video_artifact_after_step.py b/tests/unit/test_sync_video_artifact_after_step.pyindex 372c59cae..dfa647a01 100644--- a/tests/unit/test_sync_video_artifact_after_step.py+++ b/tests/unit/test_sync_video_artifact_after_step.py@@ -48,8 +48,10 @@ async def test_sync_video_uploads_each_artifact() -> None:await agent._sync_video_artifact_after_step(task, browser_state=browser_state)+ # Per-step sync runs while the recording file is still open; finalize=False skips the+ # ffmpeg remux path so long tasks do not spawn one ffmpeg subprocess per step.mock_app.BROWSER_MANAGER.get_video_artifacts.assert_awaited_once_with(- task_id=task.task_id, browser_state=browser_state+ task_id=task.task_id, browser_state=browser_state, finalize=False)assert mock_app.ARTIFACT_MANAGER.update_artifact_data.await_count == 2mock_app.ARTIFACT_MANAGER.update_artifact_data.assert_any_await(