Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-11084): address remaining review nits on code-block recording artifact (#6625)
Aaron Perez0986298ac9e0
diff --git a/skyvern/forge/sdk/artifact/manager.py b/skyvern/forge/sdk/artifact/manager.pyindex 5c6f2b4b9..6addb5e61 100644--- a/skyvern/forge/sdk/artifact/manager.py+++ b/skyvern/forge/sdk/artifact/manager.py@@ -1078,7 +1078,7 @@ class ArtifactManager:# key the upload tracking on the first available scope id instead of failing on a null task_id.aio_task_key = artifact[primary_key] or artifact["workflow_run_block_id"] or artifact["run_id"]if not aio_task_key:- raise ValueError(f"{primary_key} is required to update artifact data.")+ raise ValueError("artifact must have a task_id, workflow_run_block_id, or run_id to track its upload.")self.upload_aiotasks_map[aio_task_key].append(aio_task)return aio_task_keydiff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.pyindex bf3ab2127..db21cbf7a 100644--- a/skyvern/forge/sdk/workflow/models/block.py+++ b/skyvern/forge/sdk/workflow/models/block.py@@ -3738,13 +3738,10 @@ async def wrapper({default_args}):workflow_run_block_id: str,organization_id: str | None,) -> None:- """Register the run-scoped RECORDING row that a code block otherwise lacks.-- The agent path creates it in ``initialize_execution_state``; a code block runs no agent- step, so ``_fetch_recording_urls`` finds nothing and the Recording tab is empty. The- workflow cleanup's ``persist_video_data`` backfills the finalized video. Best-effort and- idempotent across blocks sharing one browser.- """+ """Register the run-scoped RECORDING row that a code block otherwise lacks so the recording is reachable."""+ # get_video_artifacts returns the same VideoArtifact objects held on browser_state, so the id+ # written below is observed by the early-return guard on later blocks sharing the browser.+ # Idempotency assumes those blocks run sequentially (no concurrent first-time registration).browser_artifacts = browser_state.browser_artifactsif not browser_artifacts or all(va.video_artifact_id for va in browser_artifacts.video_artifacts):return@@ -4015,8 +4012,10 @@ async def wrapper({default_args}):nonlocal workflow_run_blockif task is None:return- if action.action_type not in (ActionType.GOTO_URL, ActionType.CLICK, ActionType.INPUT_TEXT):- return+ # Every action that reaches this sink is one the recorder chose to surface on the timeline+ # (goto, click, input, page.evaluate, select, hover, ...), so each one earns a screenshot.+ # Re-listing eligible types here only drifts from the recorder's maps — which is exactly how+ # page.evaluate (EXECUTE_JS) ended up with no screenshot.# page.screenshot() shares the CDP channel with the user's page calls, so it must run synchronously# in the user-await chain (a backgrounded capture races the next action and clips a mid-nav frame);# only the page-free S3 upload is deferred off the critical path.diff --git a/tests/unit/test_code_block_recording_artifact.py b/tests/unit/test_code_block_recording_artifact.pyindex 5e2888e92..2afe93567 100644--- a/tests/unit/test_code_block_recording_artifact.py+++ b/tests/unit/test_code_block_recording_artifact.py@@ -152,6 +152,64 @@ async def test_artifact_failure_is_swallowed() -> None:assert video_artifacts[0].video_artifact_id is None+async def test_second_call_skips_re_registration() -> None:+ # get_video_artifacts returns the same VideoArtifact objects held on browser_state, so the id+ # written on the first call is visible to the early-return guard and the second call is a no-op.+ video_artifacts = [VideoArtifact(video_path="/tmp/recording.webm", video_data=b"partial-video")]+ browser_state = _browser_state(video_artifacts)+ get_video = AsyncMock(return_value=video_artifacts)+ get_block = AsyncMock(return_value=SimpleNamespace(workflow_run_block_id="wrb_1"))+ create_artifact = AsyncMock(return_value="a_recording")++ with (+ patch(f"{_BLOCK_PATH}.BROWSER_MANAGER.get_video_artifacts", get_video),+ patch(f"{_BLOCK_PATH}.DATABASE.observer.get_workflow_run_block", get_block),+ patch(f"{_BLOCK_PATH}.ARTIFACT_MANAGER.create_workflow_run_block_artifact", create_artifact),+ ):+ block = _code_block()+ for _ in range(2):+ await block._ensure_run_recording_artifact(+ browser_state=browser_state,+ workflow_run_id="wr_1",+ workflow_run_block_id="wrb_1",+ organization_id="o_1",+ )++ get_video.assert_awaited_once()+ get_block.assert_awaited_once()+ create_artifact.assert_awaited_once()+ assert video_artifacts[0].video_artifact_id == "a_recording"+++async def test_registers_when_video_data_not_yet_captured() -> None:+ # Registration happens before persist_video_data backfills the bytes, so video_data is still the+ # empty default; the row must be created anyway so cleanup has an id to update.+ video_artifacts = [VideoArtifact(video_path="/tmp/recording.webm")]+ assert video_artifacts[0].video_data == b""+ browser_state = _browser_state(video_artifacts)+ get_video = AsyncMock(return_value=video_artifacts)+ get_block = AsyncMock(return_value=SimpleNamespace(workflow_run_block_id="wrb_1"))+ create_artifact = AsyncMock(return_value="a_recording")++ with (+ patch(f"{_BLOCK_PATH}.BROWSER_MANAGER.get_video_artifacts", get_video),+ patch(f"{_BLOCK_PATH}.DATABASE.observer.get_workflow_run_block", get_block),+ patch(f"{_BLOCK_PATH}.ARTIFACT_MANAGER.create_workflow_run_block_artifact", create_artifact),+ ):+ await _code_block()._ensure_run_recording_artifact(+ browser_state=browser_state,+ workflow_run_id="wr_1",+ workflow_run_block_id="wrb_1",+ organization_id="o_1",+ )++ create_artifact.assert_awaited_once()+ assert create_artifact.call_args.kwargs["data"] == b""+ assert video_artifacts[0].video_artifact_id == "a_recording"++def _recording_artifact(*,task_id: str | None = None,diff --git a/tests/unit/workflow/test_code_block_recorder.py b/tests/unit/workflow/test_code_block_recorder.pyindex 99f91e3fc..61ddf2809 100644--- a/tests/unit/workflow/test_code_block_recorder.py+++ b/tests/unit/workflow/test_code_block_recorder.py@@ -542,6 +542,24 @@ async def test_backgrounded_screenshots_are_drained_and_linked_before_persist(assert all(a.screenshot_artifact_id == "artifact_1" for a in actions)+async def test_page_evaluate_action_captures_and_links_screenshot(monkeypatch: pytest.MonkeyPatch) -> None:+ """page.evaluate (EXECUTE_JS) is a recorded, timeline-visible action and must get a screenshot+ like clicks and navigations do, so the run detail panel can render it instead of "No screenshot"."""+ page = FakePage()+ context = FakeWorkflowRunContext()+ mocks = _patch_execute_environment(monkeypatch, page, context)++ block = _make_code_block("await page.evaluate('() => document.title')", goal="go")+ result = await block.execute(workflow_run_id="wr_test", workflow_run_block_id="wrb_test", organization_id="o_test")++ assert result.success is True+ actions = _created_actions(mocks)+ assert [a.action_type for a in actions] == [ActionType.EXECUTE_JS]+ assert mocks["create_artifact"].await_count == 1+ assert actions[0].screenshot_artifact_id == "artifact_1"++@pytest.mark.asyncioasync def test_persist_failure_does_not_fail_the_block(monkeypatch: pytest.MonkeyPatch) -> None:page = FakePage()