Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-12743): add opt-in popup grace for file-download clicks (#7631)
LawyZhengce21feaf0ad5
diff --git a/skyvern/config.py b/skyvern/config.pyindex d697fda59..f32a59ec8 100644--- a/skyvern/config.py+++ b/skyvern/config.py@@ -152,6 +152,7 @@ class Settings(BaseSettings):# Global kill-switch for select/autocomplete shadow-match observability (LLM-vs-deterministic# agreement logging). Not per-org; set false to silence the logs everywhere.SKYVERN_SELECT_SHADOW_MATCH: bool = True+ FILE_DOWNLOAD_FALSE_CLICK_POPUP_GRACE_SECONDS: float = Field(default=0, ge=0, le=60)DEBUG_MODE: bool = FalseDATABASE_STRING: str = Field(default_factory=_default_database_string)DATABASE_REPLICA_STRING: str | None = Nonediff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.pyindex 8520f2683..297cf8e83 100644--- a/skyvern/forge/agent.py+++ b/skyvern/forge/agent.py@@ -117,6 +117,7 @@ from skyvern.forge.sdk.workflow.models.block import (ActionBlock,BaseTaskBlock,CodeBlock,+ FileDownloadBlock,ValidationBlock,)from skyvern.forge.sdk.workflow.models.workflow import Workflow, WorkflowRun, WorkflowRunStatus@@ -1650,6 +1651,7 @@ class ForgeAgent:detailed_agent_step_output=detailed_agent_step_output,pdf_auto_download_src=pdf_auto_download_src,pdf_auto_download_used_bytes=pdf_auto_download_used_bytes,+ file_download_false_click_eligible=isinstance(task_block, FileDownloadBlock),_step_span=_step_span,artifact_tracker=artifact_tracker,)@@ -1725,6 +1727,7 @@ class ForgeAgent:detailed_agent_step_output: DetailedAgentStepOutput,pdf_auto_download_src: str | None,pdf_auto_download_used_bytes: bool,+ file_download_false_click_eligible: bool,_step_span: otel_trace.Span,artifact_tracker: _BackgroundArtifactTaskTracker,) -> tuple[list[Action], tuple[Step, DetailedAgentStepOutput] | None]:@@ -1885,6 +1888,7 @@ class ForgeAgent:step=step,page=current_page,action=action,+ file_download_false_click_eligible=file_download_false_click_eligible,)await app.AGENT_FUNCTION.post_action_execution(action)detailed_agent_step_output.actions_and_results[action_idx] = (diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex 2f90646c4..13cc7eab4 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -138,6 +138,7 @@ from skyvern.webeye.actions.actions import ()from skyvern.webeye.actions.responses import ActionAbort, ActionFailure, ActionResult, ActionSuccessfrom skyvern.webeye.browser_factory import initialize_download_dir+from skyvern.webeye.browser_state import BrowserStatefrom skyvern.webeye.cdp_download_interceptor import (DOWNLOAD_MIME_TYPES,MAX_FILE_SIZE_BYTES,@@ -699,18 +700,18 @@ async def _save_adopted_session_download(deferred save_as runs."""download_target = _download_target_path(download_dir, download.suggested_filename)- try:- await download.save_as(download_target)- if download_target.exists() and download_target.stat().st_size > 0:- return download_target- download_target.unlink(missing_ok=True)+ persisted = await _persist_captured_download(+ download, target=download_target, timeout=BROWSER_DOWNLOAD_MAX_WAIT_TIME+ )+ if persisted.path is not None:+ return persisted.path+ if persisted.outcome == "empty":LOG.warning("Adopted-session eager save_as produced an empty file; re-fetching download url",download_dir=str(download_dir),workflow_run_id=workflow_run_id,)- except Exception:- download_target.unlink(missing_ok=True)+ else:LOG.warning("Adopted-session eager save_as failed; re-fetching download url",download_dir=str(download_dir),@@ -773,6 +774,101 @@ def _remove_download_listener(page: Page, callback: Callable[[Download], None])LOG.warning("Page does not support removing download listeners")+def _remove_popup_listener(page: Page, callback: Callable[[Page], None]) -> None:+ off = getattr(page, "off", None)+ if callable(off):+ off("popup", callback)+ return+ page.remove_listener("popup", callback)+++class _CapturedDownloadPersistence(NamedTuple):+ path: Path | None+ outcome: str+++async def _persist_captured_download(+ download: Download, *, target: Path | None, timeout: float, owned_dir: Path | None = None+) -> _CapturedDownloadPersistence:+ try:+ async with asyncio.timeout(timeout):+ try:+ failure = await download.failure()+ except TypeError:+ failure = None+ if failure is not None:+ return _CapturedDownloadPersistence(None, "download_failed")+ if target is None:+ try:+ local_path_value = await download.path()+ if local_path_value and (local_path := Path(local_path_value)).is_file():+ if local_path.stat().st_size:+ return _CapturedDownloadPersistence(local_path, "local_path")+ if owned_dir is not None and local_path.parent.resolve() == owned_dir.resolve():+ local_path.unlink(missing_ok=True)+ return _CapturedDownloadPersistence(None, "empty")+ except Exception:+ return _CapturedDownloadPersistence(None, "path_unavailable")+ return _CapturedDownloadPersistence(None, "path_unavailable")+ await download.save_as(target)+ if target.is_file() and target.stat().st_size:+ return _CapturedDownloadPersistence(target, "saved")+ target.unlink(missing_ok=True)+ return _CapturedDownloadPersistence(None, "empty")+ except (asyncio.TimeoutError, asyncio.CancelledError) as error:+ if target is not None:+ target.unlink(missing_ok=True)+ if isinstance(error, asyncio.CancelledError):+ raise+ return _CapturedDownloadPersistence(None, "timeout")+ except Exception:+ if target is not None:+ target.unlink(missing_ok=True)+ return _CapturedDownloadPersistence(None, "save_failed")+++async def _finalize_download_artifacts(+ *,+ download_dir: Path,+ task: Task,+ list_files_before: list[str],+ list_observed_download_files: Callable[[], Awaitable[list[str]]],+) -> tuple[list[str], set[str]]:+ await check_downloading_files_and_wait_for_download_to_complete(+ download_dir=download_dir,+ organization_id=task.organization_id,+ browser_session_id=task.browser_session_id,+ timeout=task.download_timeout or BROWSER_DOWNLOAD_TIMEOUT,+ )+ list_files_after = await list_observed_download_files()+ new_file_paths = set(list_files_after) - set(list_files_before)+ paths = _deduplicate_new_downloaded_file_paths(+ new_file_paths,+ workflow_run_id=task.workflow_run_id,+ observed_file_paths=set(list_files_after),+ )+ return [os.path.basename(path) for path in paths], new_file_paths+++async def _cleanup_captured_download_popup(+ popup: Page, browser_state: BrowserState, page: Page, page_url_before_download: str+) -> None:+ cleanup = [("popup_close", popup.close())]+ if page.url in {"about:blank", ":"} and page_url_before_download not in {"about:blank", ":"}:+ cleanup.append(+ ("working_page_recovery", browser_state.navigate_to_url(page=page, url=page_url_before_download))+ )+ results = await asyncio.gather(*(operation for _, operation in cleanup), return_exceptions=True)+ for (operation, _), result in zip(cleanup, results, strict=True):+ if isinstance(result, BaseException):+ LOG.warning(+ "Captured download popup cleanup operation failed",+ operation=operation,+ exception_type=type(result).__name__,+ exc_info=(type(result), result, result.__traceback__),+ )++def _canonical_download_duplicate_stem(stem: str) -> str:"""Return a stem with common browser duplicate suffixes removed."""return DOWNLOAD_DUPLICATE_STEM_SUFFIX_RE.sub("", stem)@@ -2117,6 +2213,8 @@ class ActionHandler:step: Step,page: Page,action: Action,+ *,+ file_download_false_click_eligible: bool = False,) -> list[ActionResult]:# task_id, step_id auto-attached by @traced from SkyvernContext_action_span = otel_trace.get_current_span()@@ -2135,15 +2233,154 @@ class ActionHandler:_action_span.set_attribute("triggers_download", trigger_download_action)_tracer = otel_trace.get_tracer("skyvern")if not trigger_download_action:- with _tracer.start_as_current_span("skyvern.agent.action.handle_inner") as _hi_span:- apply_context_attrs(_hi_span)- results = await ActionHandler._handle_action(- scraped_page=scraped_page,- task=task,- step=step,- page=page,- action=action,- )+ observe_false_click = (+ file_download_false_click_eligible+ and isinstance(action, ClickAction)+ and action.download is False+ and browser_state is not None+ and settings.FILE_DOWNLOAD_FALSE_CLICK_POPUP_GRACE_SECONDS > 0+ )+ if not observe_false_click:+ with _tracer.start_as_current_span("skyvern.agent.action.handle_inner") as _hi_span:+ apply_context_attrs(_hi_span)+ results = await ActionHandler._handle_action(+ scraped_page=scraped_page,+ task=task,+ step=step,+ page=page,+ action=action,+ )+ else:+ assert browser_state is not None+ page_url_before_download = page.url+ with _tracer.start_as_current_span("skyvern.agent.action.false_click_download"):+ false_click_download_event: asyncio.Future[tuple[Download, Page]] = (+ asyncio.get_running_loop().create_future()+ )+ download_callbacks: list[tuple[Page, Callable[[Download], None]]] = []++ def on_popup(download_page: Page) -> None:+ def capture_download(download: Download) -> None:+ if not false_click_download_event.done():+ false_click_download_event.set_result((download, download_page))++ download_page.on("download", capture_download)+ download_callbacks.append((download_page, capture_download))++ page.on("popup", on_popup)++ async def process_captured_download(results: list[ActionResult] | None) -> None:+ await asyncio.sleep(0)+ captured: tuple[Download, Page] | None = None+ if false_click_download_event.done():+ captured = false_click_download_event.result()+ elif download_callbacks:+ try:+ captured = await asyncio.wait_for(+ asyncio.shield(false_click_download_event),+ timeout=settings.FILE_DOWNLOAD_FALSE_CLICK_POPUP_GRACE_SECONDS,+ )+ except asyncio.TimeoutError:+ pass+ if captured is None:+ return++ false_click_download, download_popup = captured+ context = skyvern_context.current()+ run_id = resolve_run_download_id(context, task.workflow_run_id or task.task_id)+ download_dir = Path(get_download_dir(run_id=run_id))++ async def list_false_click_files(extra: Path | None = None) -> list[str]:+ files = list_files_in_directory(download_dir)+ if task.browser_session_id:+ files += await app.STORAGE.list_downloaded_files_in_browser_session(+ organization_id=task.organization_id,+ browser_session_id=task.browser_session_id,+ )+ if extra and extra.is_file():+ files.append(str(extra))+ return files++ browser_artifacts = getattr(browser_state, "browser_artifacts", None)+ remote_session_id = getattr(browser_artifacts, "remote_browser_session_id", None)+ remote_session = isinstance(remote_session_id, str) and bool(remote_session_id)+ eager_save = (+ getattr(browser_state, "release_driver_on_close", False) is True+ or remote_session+ or getattr(browser_artifacts, "needs_cdp_frame_publisher", False) is True+ )+ if eager_save:+ download_dir.mkdir(parents=True, exist_ok=True)+ try:+ persisted = await _persist_captured_download(+ false_click_download,+ target=_download_target_path(download_dir, false_click_download.suggested_filename)+ if eager_save+ else None,+ timeout=task.download_timeout or BROWSER_DOWNLOAD_MAX_WAIT_TIME,+ owned_dir=download_dir,+ )+ if persisted.path is not None:+ observed_after_persist = await list_false_click_files()+ baseline = [path for path in observed_after_persist if path != str(persisted.path)]+ names, _ = await _finalize_download_artifacts(+ download_dir=download_dir,+ task=task,+ list_files_before=baseline,+ list_observed_download_files=lambda: list_false_click_files(persisted.path),+ )+ try:+ persisted_artifact_qualified = (+ persisted.path.is_file() and persisted.path.stat().st_size > 0+ )+ except OSError:+ persisted_artifact_qualified = False+ result = results[-1] if results else None+ if names and persisted_artifact_qualified and isinstance(result, ActionResult):+ result.downloaded_files = action.downloaded_files = names+ result.download_triggered = action.download_triggered = True+ finally:+ await _cleanup_captured_download_popup(+ download_popup, browser_state, page, page_url_before_download+ )++ try:+ with _tracer.start_as_current_span("skyvern.agent.action.handle_inner") as _hi_span:+ apply_context_attrs(_hi_span)+ try:+ results = await ActionHandler._handle_action(+ scraped_page=scraped_page,+ task=task,+ step=step,+ page=page,+ action=action,+ )+ except asyncio.CancelledError:+ raise+ except BaseException:+ try:+ await process_captured_download(None)+ except asyncio.CancelledError:+ raise+ except BaseException:+ LOG.warning(+ "Captured download processing failed after action exception",+ exc_info=True,+ )+ raise+ await process_captured_download(results)+ finally:+ try:+ _remove_popup_listener(page, on_popup)+ except Exception:+ LOG.warning("Failed to remove captured download popup listener", exc_info=True)+ for observed_page, callback in download_callbacks:+ try:+ _remove_download_listener(observed_page, callback)+ except Exception:+ LOG.warning("Failed to remove captured download listener", exc_info=True)+ if not false_click_download_event.done():+ false_click_download_event.cancel()persisted_action = await app.DATABASE.workflow_params.create_action(action=action)action.action_id = persisted_action.action_idreturn results@@ -2348,42 +2585,39 @@ class ActionHandler:and time.monotonic() - download_event_captured_at >= _download_event_grace_seconds):download_event_fallback_attempted = True- download_target = _download_target_path(- download_dir, captured_download.suggested_filename+ persisted = await _persist_captured_download(+ captured_download,+ target=_download_target_path(download_dir, captured_download.suggested_filename),+ timeout=task.download_timeout or BROWSER_DOWNLOAD_MAX_WAIT_TIME,)- try:- await captured_download.save_as(download_target)- if download_target.exists() and download_target.stat().st_size == 0:- download_target.unlink(missing_ok=True)- LOG.warning(- "Captured download event fallback produced an empty file; marking download triggered without artifact",- download_dir=download_dir,- download_target=str(download_target),- workflow_run_id=task.workflow_run_id,- )- list_files_after = await _list_observed_download_files()- download_triggered = True- break-- list_files_after = await _list_observed_download_files()- LOG.info(- "Copied captured download event to active run directory",+ if persisted.outcome == "empty":+ LOG.warning(+ "Captured download event fallback produced an empty file; marking download triggered without artifact",download_dir=download_dir,- download_target=str(download_target),workflow_run_id=task.workflow_run_id,)+ list_files_after = await _list_observed_download_files()download_triggered = True- download_event_fallback_used = Truebreak- except Exception:- LOG.warning(- "Failed to copy captured download event to active run directory",+ if persisted.path is not None:+ list_files_after = await _list_observed_download_files()+ LOG.info(+ "Copied captured download event to active run directory",download_dir=download_dir,+ download_target=str(persisted.path),workflow_run_id=task.workflow_run_id,- exc_info=True,)- download_event_fallback_failed = True+ download_triggered = True+ download_event_fallback_used = Truebreak+ LOG.warning(+ "Failed to copy captured download event to active run directory",+ download_dir=download_dir,+ workflow_run_id=task.workflow_run_id,+ outcome=persisted.outcome,+ )+ download_event_fallback_failed = True+ breakelapsed_since_action = time.monotonic() - download_wait_started_atif not download_signal_observed:download_wait_matched_errors = match_user_defined_errors_from_transient_text(@@ -2465,18 +2699,14 @@ class ActionHandler:results[-1].download_triggered = Trueaction.download_triggered = True- await check_downloading_files_and_wait_for_download_to_complete(+ downloaded_file_names, new_file_paths = await _finalize_download_artifacts(download_dir=download_dir,- organization_id=task.organization_id,- browser_session_id=task.browser_session_id,- timeout=task.download_timeout or BROWSER_DOWNLOAD_TIMEOUT,+ task=task,+ list_files_before=list_files_before,+ list_observed_download_files=_list_observed_download_files,)-- # Re-scan after waiting for .crdownload files to settle. The first- # snapshot stops at the earliest download signal, while late browser- # artifacts can still appear before task cleanup persists files.- list_files_after = await _list_observed_download_files()- new_file_paths = set(list_files_after) - set(list_files_before)+ if downloaded_file_names:+ results[-1].downloaded_files = action.downloaded_files = downloaded_file_namesif xhr_fallback_moved_paths:post_settle_extra_paths = new_file_paths - xhr_fallback_moved_pathsif post_settle_extra_paths:@@ -2489,21 +2719,6 @@ class ActionHandler:post_settle_extra_file_count=len(post_settle_extra_paths),post_settle_extra_files=sorted(os.path.basename(fp) for fp in post_settle_extra_paths),)- deduplicated_paths = _deduplicate_new_downloaded_file_paths(- new_file_paths,- workflow_run_id=task.workflow_run_id,- observed_file_paths=set(list_files_after),- )- downloaded_file_names = [os.path.basename(fp) for fp in deduplicated_paths]- if downloaded_file_names:- results[-1].downloaded_files = downloaded_file_names- action.downloaded_files = downloaded_file_names- LOG.info(- "Downloaded files captured",- downloaded_files=downloaded_file_names,- workflow_run_id=task.workflow_run_id,- )-return resultsfinally:await transient_text_observer.stop()@@ -2530,10 +2745,6 @@ class ActionHandler:# close the extra pageawait pages_after_download[-1].close()- # After a print/download action the working page sometimes navigates to- # about:blank (e.g. when the browser follows a download URL that yields no- # renderable content). Detect this and navigate back to the original URL so- # subsequent steps are not stuck on a blank page.blank_page_urls = {"about:blank", ":"}if page.url in blank_page_urls and page_url_before_download not in blank_page_urls:LOG.warning(diff --git a/tests/unit/test_agent_step_characterization.py b/tests/unit/test_agent_step_characterization.pyindex cad0ad6d7..e135245ad 100644--- a/tests/unit/test_agent_step_characterization.py+++ b/tests/unit/test_agent_step_characterization.py@@ -16,6 +16,7 @@ from skyvern.forge.sdk.core.skyvern_context import SkyvernContextfrom skyvern.forge.sdk.models import Step, StepStatusfrom skyvern.forge.sdk.schemas.organizations import Organizationfrom skyvern.forge.sdk.schemas.tasks import Task+from skyvern.forge.sdk.workflow.models.block import BaseTaskBlock, FileDownloadBlockfrom skyvern.webeye.actions.action_types import ActionTypefrom skyvern.webeye.actions.actions import (Action,@@ -56,7 +57,7 @@ class AgentStepRig:action_handler: AsyncMockupdate_statuses: list[StepStatus | None] = field(default_factory=list)- async def run(self) -> tuple[Step, DetailedAgentStepOutput]:+ async def run(self, task_block: BaseTaskBlock | None = None) -> tuple[Step, DetailedAgentStepOutput]:skyvern_context.set(self.context)try:return await self.agent.agent_step(@@ -64,6 +65,7 @@ class AgentStepRig:step=self.step,browser_state=self.browser_state,organization=self.organization,+ task_block=task_block,)finally:skyvern_context.reset()@@ -209,6 +211,15 @@ async def test_injected_actions_from_prepare_step_execution_skip_llm(monkeypatchassert output.actions == [injected]+async def test_agent_step_wires_file_download_false_click_eligibility(monkeypatch: pytest.MonkeyPatch) -> None:+ rig = make_agent_step_rig(monkeypatch, parsed_actions=[_click()])++ await rig.run(task_block=FileDownloadBlock.model_construct(label="test", complete_on_download=False))++ assert rig.action_handler.await_args.kwargs["file_download_false_click_eligible"] is True++@pytest.mark.asyncioasync def test_no_generated_actions_marks_step_failed(monkeypatch: pytest.MonkeyPatch) -> None:rig = make_agent_step_rig(monkeypatch, parsed_actions=[])diff --git a/tests/unit/test_download_file_action_handler.py b/tests/unit/test_download_file_action_handler.pyindex f77a3ac57..23f64e2be 100644--- a/tests/unit/test_download_file_action_handler.py+++ b/tests/unit/test_download_file_action_handler.py@@ -1,3 +1,4 @@+import asyncioimport osimport tempfileimport time@@ -12,12 +13,633 @@ from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanEfrom skyvern.errors.errors import UserDefinedErrorfrom skyvern.forge.sdk.models import StepStatusfrom skyvern.webeye.actions.actions import ClickAction, DownloadFileAction-from skyvern.webeye.actions.handler import ActionHandler, _remove_download_listener, handle_download_file_action+from skyvern.webeye.actions.handler import (+ ActionHandler,+ _cleanup_captured_download_popup,+ _persist_captured_download,+ _remove_download_listener,+ handle_download_file_action,+)from skyvern.webeye.actions.responses import ActionFailure, ActionSuccessfrom skyvern.webeye.scraper.scraped_page import ScrapedPagefrom tests.unit.helpers import make_organization, make_step, make_task+class _EventEmitter:+ def __init__(self, context: object = None, url: str = "https://example.test/files") -> None:+ self.listeners: dict[str, list[Callable]] = {}+ self.context, self.url = context, url++ def on(self, event: str, callback: Callable) -> None:+ self.listeners.setdefault(event, []).append(callback)++ def remove_listener(self, event: str, callback: Callable) -> None:+ if callback in (callbacks := self.listeners.get(event, [])):+ callbacks.remove(callback)++ off = remove_listener++ def emit(self, event: str, value: object) -> object:+ for callback in list(self.listeners.get(event, [])):+ callback(value)+ return value+++def _download(*, path: Path | None = None, failure: str | None = None, save_as: object = None) -> MagicMock:+ download = MagicMock(suggested_filename=path.name if path else "download.pdf")+ download.failure = AsyncMock(return_value=failure)+ download.path = AsyncMock(return_value=path)+ download.save_as = AsyncMock(side_effect=save_as)+ return download+++async def test_persist_captured_download_cancellation_cleans_owned_target(tmp_path: Path) -> None:+ async def save_as(target: Path) -> None:+ Path(target).touch()+ raise asyncio.CancelledError++ with pytest.raises(asyncio.CancelledError):+ await _persist_captured_download(+ _download(save_as=save_as), target=(target := tmp_path / "partial.pdf"), timeout=1+ )+ assert not target.exists()+++def _make_false_click_observation_context() -> tuple:+ now = datetime.now(UTC)+ task = make_task(+ now, make_organization(now), workflow_run_id="wr-popup", browser_session_id="bs-popup", download_timeout=0.05+ )+ step = make_step(now, task, step_id="step-popup", status=StepStatus.created, order=0, output=None)+ page = _EventEmitter(context := _EventEmitter())+ scraped_page = MagicMock(_browser_state=MagicMock())+ scraped_page._browser_state.list_valid_pages = AsyncMock(return_value=[page])+ return task, step, context, page, scraped_page, ClickAction(element_id="download-link", download=False)+++async def _run_false_click_observation(+ tmp_path: Path,+ *,+ click_effect: Callable[[_EventEmitter, _EventEmitter], object] | None = None,+ remote: bool = False,+ needs_cdp_frame_publisher: bool = False,+ rig: tuple | None = None,+ action_outcome: list[ActionSuccess | ActionFailure] | BaseException | None = None,+) -> tuple:+ task, step, context, page, scraped_page, action = rig or _make_false_click_observation_context()+ scraped_page._browser_state.release_driver_on_close = remote+ scraped_page._browser_state.browser_artifacts.needs_cdp_frame_publisher = needs_cdp_frame_publisher+ app_mock = MagicMock()+ storage = app_mock.STORAGE+ storage.list_downloaded_files_in_browser_session = AsyncMock(return_value=[])+ app_mock.BROWSER_MANAGER.get_for_task.return_value = scraped_page._browser_state+ app_mock.DATABASE.workflow_params.create_action = AsyncMock(return_value=action)++ async def inner(*args: object, **kwargs: object) -> list[ActionSuccess | ActionFailure]:+ if click_effect:+ click_effect(context, page)+ if isinstance(action_outcome, BaseException):+ raise action_outcome+ if action_outcome is not None:+ return action_outcome+ return [ActionSuccess()]++ with (+ patch.object(ActionHandler, "_handle_action", side_effect=inner),+ patch("skyvern.webeye.actions.handler.app", app_mock),+ patch("skyvern.webeye.actions.handler.get_download_dir", return_value=str(tmp_path)),+ patch("skyvern.webeye.actions.handler.settings.FILE_DOWNLOAD_FALSE_CLICK_POPUP_GRACE_SECONDS", 0.05),+ ):+ results = await ActionHandler.handle_action(+ scraped_page,+ task,+ step,+ page,+ action,+ file_download_false_click_eligible=True,+ )+ return results, action, context, page, storage+++async def test_false_click_captured_download_is_finalized_after_action_failure(tmp_path: Path) -> None:+ downloaded_path = tmp_path / "captured-after-failure.pdf"+ downloaded_path.write_bytes(b"content")+ rig = _make_false_click_observation_context()+ _, _, context, page, scraped_page, action = rig+ unrelated = _EventEmitter(context, "https://example.test/unrelated")+ popup = _EventEmitter(context, "about:blank")+ unrelated.close = AsyncMock() # type: ignore[attr-defined]+ popup.close = AsyncMock() # type: ignore[attr-defined]+ scraped_page._browser_state.navigate_to_url = AsyncMock()+ failure = ActionFailure(RuntimeError("click failed"))++ def click(_context: _EventEmitter, clicked_page: _EventEmitter) -> None:+ clicked_page.emit("popup", popup)+ popup.emit("download", _download(path=downloaded_path))+ clicked_page.url = "about:blank"++ with patch(+ "skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",+ new=AsyncMock(),+ ) as settle:+ results, _, _, _, _ = await _run_false_click_observation(+ tmp_path, click_effect=click, rig=rig, action_outcome=[failure]+ )++ assert results == [failure]+ assert results[0] is failure and not results[0].success+ assert results[0].downloaded_files == action.downloaded_files == ["captured-after-failure.pdf"]+ assert results[0].download_triggered is action.download_triggered is True+ settle.assert_awaited_once()+ popup.close.assert_awaited_once()+ unrelated.close.assert_not_awaited()+ scraped_page._browser_state.navigate_to_url.assert_awaited_once_with(page=page, url="https://example.test/files")++[email protected]("post_settle", ["empty", "vanished"])+async def test_false_click_action_failure_does_not_credit_disqualified_artifact(+ tmp_path: Path, post_settle: str+) -> None:+ downloaded_path = tmp_path / "disqualified-after-failure.pdf"+ downloaded_path.write_bytes(b"content")+ rig = _make_false_click_observation_context()+ _, _, context, page, scraped_page, action = rig+ unrelated = _EventEmitter(context, "https://example.test/unrelated")+ popup = _EventEmitter(context, "about:blank")+ unrelated.close = AsyncMock() # type: ignore[attr-defined]+ popup.close = AsyncMock() # type: ignore[attr-defined]+ scraped_page._browser_state.navigate_to_url = AsyncMock()+ failure = ActionFailure(RuntimeError("click failed"))++ def click(_context: _EventEmitter, clicked_page: _EventEmitter) -> None:+ clicked_page.emit("popup", popup)+ popup.emit("download", _download(path=downloaded_path))+ clicked_page.url = "about:blank"++ async def disqualify(**_: object) -> None:+ if post_settle == "empty":+ downloaded_path.write_bytes(b"")+ else:+ downloaded_path.unlink()++ with patch(+ "skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",+ new=AsyncMock(side_effect=disqualify),+ ):+ results, _, _, _, _ = await _run_false_click_observation(+ tmp_path, click_effect=click, rig=rig, action_outcome=[failure]+ )++ assert results == [failure]+ assert results[0] is failure and not results[0].success+ assert not results[0].download_triggered and not results[0].downloaded_files+ assert not action.download_triggered and not action.downloaded_files+ popup.close.assert_awaited_once()+ unrelated.close.assert_not_awaited()+ scraped_page._browser_state.navigate_to_url.assert_awaited_once_with(page=page, url="https://example.test/files")+++async def test_false_click_captured_download_is_finalized_before_original_exception(tmp_path: Path) -> None:+ downloaded_path = tmp_path / "captured-before-exception.pdf"+ downloaded_path.write_bytes(b"content")+ rig = _make_false_click_observation_context()+ _, _, context, page, scraped_page, action = rig+ unrelated = _EventEmitter(context, "https://example.test/unrelated")+ popup = _EventEmitter(context, "about:blank")+ unrelated.close = AsyncMock() # type: ignore[attr-defined]+ popup.close = AsyncMock(side_effect=RuntimeError("cleanup failed")) # type: ignore[attr-defined]+ scraped_page._browser_state.navigate_to_url = AsyncMock()+ original = RuntimeError("original click exception")++ def click(_context: _EventEmitter, clicked_page: _EventEmitter) -> None:+ clicked_page.emit("popup", popup)+ popup.emit("download", _download(path=downloaded_path))+ clicked_page.url = "about:blank"++ with (+ patch(+ "skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",+ new=AsyncMock(),+ ) as settle,+ pytest.raises(RuntimeError, match="original click exception") as raised,+ ):+ await _run_false_click_observation(tmp_path, click_effect=click, rig=rig, action_outcome=original)++ assert raised.value is original+ assert not action.download_triggered and not action.downloaded_files+ settle.assert_awaited_once()+ popup.close.assert_awaited_once()+ unrelated.close.assert_not_awaited()+ scraped_page._browser_state.navigate_to_url.assert_awaited_once_with(page=page, url="https://example.test/files")+++async def test_false_click_listener_cleanup_does_not_mask_original_exception(tmp_path: Path) -> None:+ downloaded_path = tmp_path / "captured-before-listener-cleanup.pdf"+ downloaded_path.write_bytes(b"content")+ rig = _make_false_click_observation_context()+ _, _, context, page, scraped_page, action = rig+ popup = _EventEmitter(context, "about:blank")+ popup.close = AsyncMock() # type: ignore[attr-defined]+ scraped_page._browser_state.navigate_to_url = AsyncMock()+ original = RuntimeError("original click exception")++ def click(_context: _EventEmitter, clicked_page: _EventEmitter) -> None:+ clicked_page.emit("popup", popup)+ popup.emit("download", _download(path=downloaded_path))+ clicked_page.off = MagicMock(side_effect=RuntimeError("popup listener removal failed")) # type: ignore[method-assign]++ with (+ patch(+ "skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",+ new=AsyncMock(),+ ),+ pytest.raises(RuntimeError, match="original click exception") as raised,+ ):+ await _run_false_click_observation(tmp_path, click_effect=click, rig=rig, action_outcome=original)++ assert raised.value is original+ assert popup.listeners["download"] == []+ popup.close.assert_awaited_once()+ assert not action.download_triggered and not action.downloaded_files+++async def test_false_click_finalizes_artifacts_and_closes_only_emitting_popup(tmp_path: Path) -> None:+ _, _, context, page, storage = await _run_false_click_observation(tmp_path)+ assert context.listeners == {} and not page.listeners["popup"] and storage.mock_calls == []+ (downloaded_path := tmp_path / "captured_1.pdf").write_bytes(b"content")+ rig = _make_false_click_observation_context()+ _, _, context, page, scraped_page, _ = rig+ unrelated = _EventEmitter(context, "https://example.test/unrelated")+ popup = _EventEmitter(context, "about:blank")+ unrelated.close = AsyncMock() # type: ignore[attr-defined]+ popup.close = AsyncMock() # type: ignore[attr-defined]+ scraped_page._browser_state.navigate_to_url = AsyncMock()++ def click(_context: _EventEmitter, clicked_page: _EventEmitter) -> None:+ clicked_page.emit("popup", popup)+ popup.emit("download", _download(path=downloaded_path))+ clicked_page.url = "about:blank"++ with patch(+ "skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",+ new=AsyncMock(),+ ):+ results, action, _, _, _ = await _run_false_click_observation(tmp_path, click_effect=click, rig=rig)++ popup.close.assert_awaited_once()+ unrelated.close.assert_not_awaited()+ scraped_page._browser_state.navigate_to_url.assert_awaited_once_with(page=page, url="https://example.test/files")+ assert results[-1].downloaded_files == action.downloaded_files == ["captured_1.pdf"]++[email protected]("eager", [None, "remote"])+async def test_false_click_post_settle_empty_artifact_is_uncredited(tmp_path: Path, eager: str | None) -> None:+ local_path = tmp_path / "local.pdf" if eager is None else None+ if local_path is not None:+ local_path.write_bytes(b"content")++ async def save_as(target: Path) -> None:+ Path(target).write_bytes(b"content")++ rig = _make_false_click_observation_context()+ _, _, context, page, scraped_page, action = rig+ unrelated = _EventEmitter(context, "https://example.test/unrelated")+ popup = _EventEmitter(context, "about:blank")+ unrelated.close = AsyncMock() # type: ignore[attr-defined]+ popup.close = AsyncMock() # type: ignore[attr-defined]+ scraped_page._browser_state.navigate_to_url = AsyncMock()++ def click(_context: _EventEmitter, clicked_page: _EventEmitter) -> None:+ clicked_page.emit("popup", popup)+ popup.emit("download", _download(path=local_path, save_as=save_as))+ clicked_page.url = "about:blank"++ async def truncate_persisted_artifact(**_: object) -> None:+ persisted_path = local_path or next(tmp_path.iterdir())+ persisted_path.write_bytes(b"")++ with patch(+ "skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",+ new=AsyncMock(side_effect=truncate_persisted_artifact),+ ):+ results, _, context, page, _ = await _run_false_click_observation(+ tmp_path,+ click_effect=click,+ remote=eager == "remote",+ rig=rig,+ )++ assert not results[-1].download_triggered and not results[-1].downloaded_files+ assert not action.download_triggered and not action.downloaded_files+ assert page.listeners["popup"] == popup.listeners["download"] == []+ popup.close.assert_awaited_once()+ unrelated.close.assert_not_awaited()+ scraped_page._browser_state.navigate_to_url.assert_awaited_once_with(page=page, url="https://example.test/files")+++ ("outcome", "eager"),+ [+ ("saved", "remote"),+ ("local_path", None),+ ("empty", "cdp"),+ ("download_failed", "remote"),+ ("path_unavailable", None),+ ("timeout", "remote"),+ ("save_failed", "remote"),+ ],+)+async def test_false_click_unsuccessful_download_is_uncredited(tmp_path: Path, outcome: str, eager: str | None) -> None:+ async def save_as(target: Path) -> None:+ if outcome == "timeout":+ await asyncio.Event().wait()+ if outcome == "save_failed":+ raise RuntimeError("save failed")+ Path(target).write_bytes(b"content" if outcome == "saved" else b"")++ local_path = tmp_path / "local.pdf" if outcome == "local_path" else None+ if local_path is not None:+ local_path.write_bytes(b"content")+ download = _download(+ path=local_path,+ failure="failed" if outcome == "download_failed" else None,+ save_as=save_as,+ )+ rig = _make_false_click_observation_context()+ _, _, context, page, scraped_page, _ = rig+ unrelated = _EventEmitter(context, "https://example.test/unrelated")+ popup = _EventEmitter(context, "about:blank")+ unrelated.close = AsyncMock() # type: ignore[attr-defined]+ popup.close = AsyncMock() # type: ignore[attr-defined]+ scraped_page._browser_state.navigate_to_url = AsyncMock()++ def click(_context: _EventEmitter, clicked_page: _EventEmitter) -> None:+ clicked_page.emit("popup", popup)+ popup.emit("download", download)+ clicked_page.url = "about:blank"++ with patch(+ "skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",+ new=AsyncMock(side_effect=lambda **_: next(tmp_path.iterdir()).unlink()),+ ) as settle:+ results, _, context, page, storage = await _run_false_click_observation(+ tmp_path,+ click_effect=click,+ remote=eager == "remote",+ needs_cdp_frame_publisher=eager == "cdp",+ rig=rig,+ )+ assert not results[-1].download_triggered and not results[-1].downloaded_files and not list(tmp_path.iterdir())+ assert page.listeners["popup"] == popup.listeners["download"] == []+ assert settle.await_count == (1 if outcome in {"saved", "local_path"} else 0)+ popup.close.assert_awaited_once()+ unrelated.close.assert_not_awaited()+ scraped_page._browser_state.navigate_to_url.assert_awaited_once_with(page=page, url="https://example.test/files")+ assert storage.list_downloaded_files_in_browser_session.await_count == (+ 2 if outcome in {"saved", "local_path"} else 0+ )+++async def test_false_click_persistence_cancellation_cleans_popup_and_propagates(tmp_path: Path) -> None:+ rig = _make_false_click_observation_context()+ _, _, context, page, scraped_page, _ = rig+ unrelated = _EventEmitter(context, "https://example.test/unrelated")+ popup = _EventEmitter(context, "about:blank")+ unrelated.close = AsyncMock() # type: ignore[attr-defined]+ popup.close = AsyncMock(side_effect=RuntimeError("cleanup close failed")) # type: ignore[attr-defined]+ scraped_page._browser_state.navigate_to_url = AsyncMock()++ def click(_context: _EventEmitter, clicked_page: _EventEmitter) -> None:+ clicked_page.emit("popup", popup)+ popup.emit("download", _download(save_as=asyncio.CancelledError()))+ clicked_page.url = "about:blank"++ with pytest.raises(asyncio.CancelledError):+ await _run_false_click_observation(tmp_path, click_effect=click, remote=True, rig=rig)++ assert not rig[-1].download_triggered and not rig[-1].downloaded_files+ popup.close.assert_awaited_once()+ unrelated.close.assert_not_awaited()+ scraped_page._browser_state.navigate_to_url.assert_awaited_once_with(page=page, url="https://example.test/files")+++async def test_false_click_persistence_can_exceed_grace_within_task_timeout(tmp_path: Path) -> None:+ rig = _make_false_click_observation_context()+ task, _, context, page, scraped_page, action = rig+ task.download_timeout = 10+ popup = _EventEmitter(context, "about:blank")+ popup.close = AsyncMock() # type: ignore[attr-defined]+ scraped_page._browser_state.navigate_to_url = AsyncMock()+ persistence_timeouts: list[float] = []++ async def persist_with_task_timeout(+ _download: object, *, target: Path, timeout: float, owned_dir: Path+ ) -> MagicMock:+ persistence_timeouts.append(timeout)+ Path(target).write_bytes(b"content")+ return MagicMock(path=target, outcome="saved")++ def click(_context: _EventEmitter, clicked_page: _EventEmitter) -> None:+ clicked_page.emit("popup", popup)+ popup.emit("download", _download())++ with (+ patch(+ "skyvern.webeye.actions.handler._persist_captured_download",+ new=AsyncMock(side_effect=persist_with_task_timeout),+ ),+ patch(+ "skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",+ new=AsyncMock(),+ ),+ ):+ results, _, _, _, _ = await _run_false_click_observation(tmp_path, click_effect=click, remote=True, rig=rig)++ assert persistence_timeouts == [10]+ assert results[-1].downloaded_files == action.downloaded_files+ assert len(action.downloaded_files) == 1 and action.downloaded_files[0].endswith("-download.pdf")+ assert results[-1].download_triggered is action.download_triggered is True+ assert page.listeners["popup"] == popup.listeners["download"] == []+ popup.close.assert_awaited_once()+++async def test_false_click_persistence_remains_bounded_by_task_timeout(tmp_path: Path) -> None:+ rig = _make_false_click_observation_context()+ task, _, context, page, scraped_page, action = rig+ task.download_timeout = 0.01+ popup = _EventEmitter(context, "about:blank")+ popup.close = AsyncMock() # type: ignore[attr-defined]+ scraped_page._browser_state.navigate_to_url = AsyncMock()++ async def save_as(_target: Path) -> None:+ await asyncio.Event().wait()++ def click(_context: _EventEmitter, clicked_page: _EventEmitter) -> None:+ clicked_page.emit("popup", popup)+ popup.emit("download", _download(save_as=save_as))++ results, _, _, _, _ = await _run_false_click_observation(tmp_path, click_effect=click, remote=True, rig=rig)++ assert not results[-1].download_triggered and not action.download_triggered+ assert not list(tmp_path.iterdir())+ assert page.listeners["popup"] == popup.listeners["download"] == []+ popup.close.assert_awaited_once()+++async def test_false_click_processing_cancellation_after_click_exception_propagates(tmp_path: Path) -> None:+ rig = _make_false_click_observation_context()+ _, _, context, page, scraped_page, _ = rig+ popup = _EventEmitter(context, "about:blank")+ popup.close = AsyncMock() # type: ignore[attr-defined]+ scraped_page._browser_state.navigate_to_url = AsyncMock()+ original = RuntimeError("original click exception")++ def click(_context: _EventEmitter, clicked_page: _EventEmitter) -> None:+ clicked_page.emit("popup", popup)+ popup.emit("download", _download(save_as=asyncio.CancelledError()))++ with pytest.raises(asyncio.CancelledError):+ await _run_false_click_observation(tmp_path, click_effect=click, remote=True, rig=rig, action_outcome=original)++ assert page.listeners["popup"] == popup.listeners["download"] == []+ popup.close.assert_awaited_once()+++async def test_false_click_cancellation_from_click_skips_captured_download_processing(tmp_path: Path) -> None:+ rig = _make_false_click_observation_context()+ _, _, context, page, scraped_page, _ = rig+ popup = _EventEmitter(context, "about:blank")+ popup.close = AsyncMock() # type: ignore[attr-defined]+ scraped_page._browser_state.navigate_to_url = AsyncMock()+ original = asyncio.CancelledError()++ def click(_context: _EventEmitter, clicked_page: _EventEmitter) -> None:+ clicked_page.emit("popup", popup)+ popup.emit("download", _download())++ with (+ patch("skyvern.webeye.actions.handler._persist_captured_download", new=AsyncMock()) as persist,+ patch(+ "skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",+ new=AsyncMock(),+ ) as settle,+ pytest.raises(asyncio.CancelledError) as raised,+ ):+ await _run_false_click_observation(tmp_path, click_effect=click, rig=rig, action_outcome=original)++ assert raised.value is original+ persist.assert_not_awaited()+ settle.assert_not_awaited()+ assert page.listeners["popup"] == popup.listeners["download"] == []+ popup.close.assert_not_awaited()+ scraped_page._browser_state.navigate_to_url.assert_not_awaited()+++async def test_false_click_processing_failure_after_click_exception_preserves_original(tmp_path: Path) -> None:+ rig = _make_false_click_observation_context()+ _, _, context, page, scraped_page, _ = rig+ popup = _EventEmitter(context, "about:blank")+ popup.close = AsyncMock() # type: ignore[attr-defined]+ scraped_page._browser_state.navigate_to_url = AsyncMock()+ original = RuntimeError("original click exception")++ def click(_context: _EventEmitter, clicked_page: _EventEmitter) -> None:+ clicked_page.emit("popup", popup)+ popup.emit("download", _download())++ with (+ patch(+ "skyvern.webeye.actions.handler._persist_captured_download",+ new=AsyncMock(side_effect=ValueError("processing failed")),+ ),+ pytest.raises(RuntimeError, match="original click exception") as raised,+ ):+ await _run_false_click_observation(tmp_path, click_effect=click, remote=True, rig=rig, action_outcome=original)++ assert raised.value is original+ assert page.listeners["popup"] == popup.listeners["download"] == []+ popup.close.assert_awaited_once()+++ ("failed_operations", "expected_log_count"),+ [+ ({"popup_close"}, 1),+ ({"working_page_recovery"}, 1),+ ({"popup_close", "working_page_recovery"}, 2),+ ],+)+async def test_cleanup_captured_download_popup_logs_failures_and_remains_best_effort(+ failed_operations: set[str], expected_log_count: int+) -> None:+ page = _EventEmitter(url="about:blank")+ popup = _EventEmitter(url="about:blank")+ popup.close = AsyncMock(side_effect=RuntimeError("close failed") if "popup_close" in failed_operations else None) # type: ignore[attr-defined]+ browser_state = MagicMock()+ browser_state.navigate_to_url = AsyncMock(+ side_effect=RuntimeError("navigate failed") if "working_page_recovery" in failed_operations else None+ )++ with patch("skyvern.webeye.actions.handler.LOG.warning") as warning:+ await _cleanup_captured_download_popup(popup, browser_state, page, "https://example.test/files")++ popup.close.assert_awaited_once()+ browser_state.navigate_to_url.assert_awaited_once_with(page=page, url="https://example.test/files")+ assert warning.call_count == expected_log_count+ assert {call.kwargs["operation"] for call in warning.call_args_list} == failed_operations+ assert {call.kwargs["exception_type"] for call in warning.call_args_list} == {"RuntimeError"}++[email protected]("failure", [RuntimeError("settle failed"), asyncio.CancelledError()])+async def test_false_click_finalization_failure_cleans_popup_and_propagates(+ tmp_path: Path, failure: BaseException+) -> None:+ rig = _make_false_click_observation_context()+ _, _, context, page, scraped_page, _ = rig+ unrelated = _EventEmitter(context, "https://example.test/unrelated")+ popup = _EventEmitter(context, "about:blank")+ unrelated.close = AsyncMock() # type: ignore[attr-defined]+ popup.close = AsyncMock(side_effect=RuntimeError("cleanup close failed")) # type: ignore[attr-defined]+ scraped_page._browser_state.navigate_to_url = AsyncMock()++ async def save_as(target: Path) -> None:+ Path(target).write_bytes(b"content")++ def click(_context: _EventEmitter, clicked_page: _EventEmitter) -> None:+ clicked_page.emit("popup", popup)+ popup.emit("download", _download(save_as=save_as))+ clicked_page.url = "about:blank"++ with (+ patch(+ "skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",+ new=AsyncMock(side_effect=failure),+ ),+ pytest.raises(type(failure), match="settle failed" if isinstance(failure, RuntimeError) else None),+ ):+ await _run_false_click_observation(tmp_path, click_effect=click, remote=True, rig=rig)++ popup.close.assert_awaited_once()+ unrelated.close.assert_not_awaited()+ scraped_page._browser_state.navigate_to_url.assert_awaited_once_with(page=page, url="https://example.test/files")++def _download_wait_span_attrs(span_exporter: InMemorySpanExporter) -> dict:span = next((span for span in span_exporter.get_finished_spans() if span.name == "skyvern.agent.action.download_wait"),@@ -972,6 +1594,9 @@ async def test_handle_action_copies_download_event_when_no_observed_file_appears),patch("skyvern.webeye.actions.handler.app", mock_app),patch("skyvern.webeye.actions.handler.DOWNLOAD_EVENT_ACTIVE_DIR_GRACE_SECONDS", 0),+ patch(+ "skyvern.webeye.actions.handler._persist_captured_download", wraps=_persist_captured_download+ ) as persist,):results = await ActionHandler.handle_action(scraped_page=scraped_page,@@ -988,6 +1613,7 @@ async def test_handle_action_copies_download_event_when_no_observed_file_appearsassert action.downloaded_files == results[-1].downloaded_filesassert wait_for_downloads.await_count == 1download.save_as.assert_awaited_once()+ persist.assert_awaited_once()saved_path = download.save_as.await_args.args[0]assert os.path.dirname(saved_path) == primary_dirpage.off.assert_called_once_with("download", download_callbacks["download"])