Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Settle the page before Task V3 captures its post-action screenshot (SKY-18163) (#8870)
Aaron Perezf5f3f28a9ce0
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.pyindex 56764539e..432b71c0d 100644--- a/skyvern/forge/agent.py+++ b/skyvern/forge/agent.py@@ -1333,6 +1333,22 @@ def _task_v3_run_secret_values(task: Task) -> set[str]:)+async def _settle_before_post_action_screenshot(browser_state: BrowserState) -> None:+ # Never raises: `safe_wait_for_animation_end` swallows its own timeout, and a page or frame this+ # cannot reach is one the capture that follows has to handle anyway.+ try:+ page = await browser_state.get_working_page()+ if page is None:+ return+ skyvern_frame = await SkyvernFrame.create_instance(+ frame=page,+ engine_selection=browser_state.engine_selection,+ )+ await skyvern_frame.safe_wait_for_animation_end(caller="taskv3_post_action_artifact")+ except Exception:+ LOG.debug("task_v3 could not settle the page before the post-action screenshot", exc_info=True)++def _task_v3_goal_check_redactor(task: Task, context: SkyvernContext | None) -> Callable[[str], str]:"""One secret set per goal check, built when the check starts."""# Raw runtime values too, not only the artifact-redaction set: a verification code the run read@@ -2693,6 +2709,10 @@ class ForgeAgent:nonlocal v3_round_billedscreenshot_artifact_id: str | None = Nonetry:+ # A tool call returns as soon as the page accepts it, so a navigating click leaves this+ # capture racing the next paint and it lands on a blank page (SKY-18163). Settle first,+ # bounded, the way the step engine's own post-action capture does.+ await _settle_before_post_action_screenshot(browser_state)screenshot = await browser_state.take_post_action_screenshot(scrolling_number=0)screenshot_artifact_id = await app.ARTIFACT_MANAGER.create_artifact(step=step, artifact_type=ArtifactType.SCREENSHOT_ACTION, data=screenshotdiff --git a/tests/unit/test_agent_task_v3.py b/tests/unit/test_agent_task_v3.pyindex d1a068b1b..04f2f4942 100644--- a/tests/unit/test_agent_task_v3.py+++ b/tests/unit/test_agent_task_v3.py@@ -117,6 +117,9 @@ async def _run_execute_task_v3(action_rounds: list[list[RoundAction]] | None = None,action_round_texts: list[str | None] | None = None,screenshot_raises: bool = False,+ # Records what the post-action capture path does, in order: "settle" per page settle,+ # "screenshot" per capture. Also stubs SkyvernFrame so the settle needs no real frame.+ capture_order: list[str] | None = None,task_block: BaseTaskBlock | None = None,recovery_credential_parameter_keys: list[str] | None = None,validation_without_page_information: bool = False,@@ -184,6 +187,20 @@ async def _run_execute_task_v3(return_value=b"png-bytes",side_effect=RuntimeError("screenshot boom") if screenshot_raises else None,)+ if capture_order is not None:++ async def _record_capture(**_kwargs: Any) -> bytes:+ capture_order.append("screenshot")+ if screenshot_raises:+ raise RuntimeError("screenshot boom")+ return b"png-bytes"++ browser_state.take_post_action_screenshot = AsyncMock(side_effect=_record_capture)+ settling_frame = MagicMock()+ settling_frame.safe_wait_for_animation_end = AsyncMock(+ side_effect=lambda **_kwargs: capture_order.append("settle")+ )+ monkeypatch.setattr("skyvern.forge.agent.SkyvernFrame.create_instance", AsyncMock(return_value=settling_frame))async def _loop(**kwargs: Any) -> LoopOutcome:# Exposed so tests can probe context state as seen from inside the loop (and, since this@@ -1204,6 +1221,32 @@ async def test_execute_task_v3_persists_per_action_screenshots_and_rows(monkeypaassert all(a.response is None for a in persisted)+async def test_execute_task_v3_settles_the_page_before_each_post_action_screenshot(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # SKY-18163: a tool call returns as soon as the page accepts it, so capturing straight after a+ # navigating click photographs the blank page mid-paint and the run details show empty+ # screenshots. Every round's capture must be preceded by the bounded page settle.+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=["click", "click"])+ rounds = [+ [RoundAction("click", {"selector": "#next"}, True, billable=True)],+ [RoundAction("click", {"selector": "#submit"}, True, billable=True)],+ ]+ capture_order: list[str] = []+ await _run_execute_task_v3(+ monkeypatch,+ outcome,+ action_rounds=rounds,+ capture_order=capture_order,+ data_extraction_goal=None,+ extracted_information_schema=None,+ )+ # The two action rounds, each settled before its own capture. The terminal decision capture that+ # follows them runs after the loop has already finished, so it is not part of this contract.+ assert capture_order[:4] == ["settle", "screenshot", "settle", "screenshot"]++_GOTO_OUTCOME = {"requested_url": "https://forms.example.test/contact-us","url": "https://forms.example.test/contact",