Skip to content

Automate browser based workflows with AI

fix(SKY-15371): clean up confirmed download popups at the action seam (#8393)

LawyZheng2008985cd6e4
diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.py
index b74eaf4f3..e2d0de170 100644
--- a/skyvern/webeye/actions/handler.py
+++ b/skyvern/webeye/actions/handler.py
@@ -3999,14 +3999,14 @@ class ActionHandler:
false_click_bypass_eligible = (
file_download_false_click_eligible and isinstance(action, ClickAction) and action.download is False
)
- # The popup-grace persistence wrapper stays separately gated: it captures/persists a
- # download the click mints on a popup, which is only worth its cost when grace > 0.
- observe_false_click = (
- false_click_bypass_eligible
- and browser_state is not None
- and settings.FILE_DOWNLOAD_FALSE_CLICK_POPUP_GRACE_SECONDS > 0
- )
- if not observe_false_click:
+ # Popup cleanup runs whenever an eligible click could mint a download on a popup, so the
+ # marker popup never lingers as the working page. Only persistence -- and its grace wait --
+ # stays gated on grace > 0, since capturing/persisting the download is what carries the cost.
+ capture_false_click_popup = false_click_bypass_eligible and browser_state is not None
+ persist_false_click_download = (
+ capture_false_click_popup and settings.FILE_DOWNLOAD_FALSE_CLICK_POPUP_GRACE_SECONDS > 0
+ )
+ if not capture_false_click_popup:
false_click_eligible_token = (
_false_click_download_eligible.set(True) if false_click_bypass_eligible else None
)
@@ -4048,7 +4048,9 @@ class ActionHandler:
captured: tuple[Download, Page] | None = None
if false_click_download_event.done():
captured = false_click_download_event.result()
- elif download_callbacks:
+ elif download_callbacks and settings.FILE_DOWNLOAD_FALSE_CLICK_POPUP_GRACE_SECONDS > 0:
+ # grace>0 only: wait briefly for a late download event. At grace=0 we never
+ # introduce a new wait -- cleanup acts only on an already-resolved capture.
try:
captured = await asyncio.wait_for(
asyncio.shield(false_click_download_event),
@@ -4060,32 +4062,37 @@ class ActionHandler:
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:
+ # Persistence and every side effect it needs -- run id, download dir, storage
+ # listing, directory creation, persist, finalize -- stay grace-gated. At
+ # grace=0 we skip all of it and only the popup cleanup/restore below runs.
+ if not persist_false_click_download:
+ return
+ 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)
persisted = await _persist_captured_download(
false_click_download,
target=_download_target_path(download_dir, false_click_download.suggested_filename)
diff --git a/tests/unit/test_download_file_action_handler.py b/tests/unit/test_download_file_action_handler.py
index a3d1ea93d..60a6798bc 100644
--- a/tests/unit/test_download_file_action_handler.py
+++ b/tests/unit/test_download_file_action_handler.py
@@ -155,6 +155,8 @@ async def _run_false_click_observation(
needs_cdp_frame_publisher: bool = False,
rig: tuple | None = None,
action_outcome: list[ActionSuccess | ActionFailure] | BaseException | None = None,
+ grace: float = 0.05,
+ get_download_dir_mock: MagicMock | 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
@@ -175,11 +177,12 @@ async def _run_false_click_observation(
return action_outcome
return [ActionSuccess()]
+ gdd_mock = get_download_dir_mock if get_download_dir_mock is not None else MagicMock(return_value=str(tmp_path))
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),
+ patch("skyvern.webeye.actions.handler.get_download_dir", gdd_mock),
+ patch("skyvern.webeye.actions.handler.settings.FILE_DOWNLOAD_FALSE_CLICK_POPUP_GRACE_SECONDS", grace),
):
results = await ActionHandler.handle_action(
scraped_page,
@@ -424,6 +427,130 @@ async def test_false_click_finalizes_artifacts_and_closes_only_emitting_popup(tm
assert results[-1].downloaded_files == action.downloaded_files == ["captured_1.pdf"]
+async def test_false_click_grace_zero_closes_download_popup_without_persisting(tmp_path: Path) -> None:
+ # At grace=0 a FileDownloadBlock click that mints a download on a popup must still have that popup
+ # closed and the original page restored before handle_action returns; persistence stays gated on
+ # grace>0, so the download is not credited here.
+ downloaded_path = tmp_path / "captured_grace0.pdf"
+ downloaded_path.write_bytes(b"content")
+ rig = _make_false_click_observation_context()
+ _, _, context, page, scraped_page, action = rig
+ popup = _EventEmitter(context, ":")
+ 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"
+
+ results, action, _, _, _ = await _run_false_click_observation(tmp_path, click_effect=click, rig=rig, grace=0)
+
+ popup.close.assert_awaited_once()
+ scraped_page._browser_state.navigate_to_url.assert_awaited_once_with(page=page, url="https://example.test/files")
+ assert not action.download_triggered and not action.downloaded_files
+ assert not results[-1].download_triggered
+
+
+async def test_false_click_grace_zero_leaves_non_download_popup_open(tmp_path: Path) -> None:
+ # A popup event without a confirmed same-action download must never be closed as a download popup.
+ rig = _make_false_click_observation_context()
+ _, _, context, page, scraped_page, action = rig
+ popup = _EventEmitter(context, ":")
+ 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 opens, but no download is confirmed
+
+ results, action, _, _, _ = await _run_false_click_observation(tmp_path, click_effect=click, rig=rig, grace=0)
+
+ popup.close.assert_not_awaited()
+ scraped_page._browser_state.navigate_to_url.assert_not_awaited()
+ assert not action.download_triggered and not action.downloaded_files
+
+
+async def test_false_click_grace_zero_adds_no_capture_dwell(tmp_path: Path) -> None:
+ # At grace=0 the capture path adds no new timed wait -- it acts only on an already-resolved
+ # download event. The admission wait_for lives in _handle_action (patched out here), so a zero
+ # count here is scoped to the outer capture, not a global wait_for ban.
+ rig = _make_false_click_observation_context()
+ _, _, context, page, scraped_page, _ = rig
+ popup = _EventEmitter(context, ":")
+ 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, no download -> event never resolves
+
+ wait_for_spy = AsyncMock(wraps=asyncio.wait_for)
+ with patch("skyvern.webeye.actions.handler.asyncio.wait_for", wait_for_spy):
+ await _run_false_click_observation(tmp_path, click_effect=click, rig=rig, grace=0)
+
+ wait_for_spy.assert_not_awaited()
+ popup.close.assert_not_awaited()
+
+
+async def test_false_click_grace_zero_closes_only_download_popup(tmp_path: Path) -> None:
+ # Only the popup that minted the download is closed; a pre-existing tab and the original action
+ # page are never closed.
+ downloaded_path = tmp_path / "captured_iso.pdf"
+ downloaded_path.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, ":")
+ unrelated.close = AsyncMock() # type: ignore[attr-defined]
+ popup.close = AsyncMock() # type: ignore[attr-defined]
+ page.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"
+
+ await _run_false_click_observation(tmp_path, click_effect=click, rig=rig, grace=0)
+
+ popup.close.assert_awaited_once()
+ unrelated.close.assert_not_awaited()
+ page.close.assert_not_awaited()
+
+
+async def test_false_click_grace_zero_runs_no_persistence_setup(tmp_path: Path) -> None:
+ # At grace=0 no persistence-only setup may run -- get_download_dir must not be called and no
+ # storage listing happens -- while the captured popup is still closed and the original page
+ # restored.
+ downloaded_path = tmp_path / "captured_no_setup.pdf"
+ downloaded_path.write_bytes(b"content")
+ rig = _make_false_click_observation_context()
+ _, _, context, page, scraped_page, _ = rig
+ popup = _EventEmitter(context, ":")
+ popup.close = AsyncMock() # type: ignore[attr-defined]
+ scraped_page._browser_state.navigate_to_url = AsyncMock()
+ get_download_dir_mock = MagicMock(return_value=str(tmp_path))
+
+ 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"
+
+ _, action, _, _, storage = await _run_false_click_observation(
+ tmp_path, click_effect=click, rig=rig, grace=0, get_download_dir_mock=get_download_dir_mock
+ )
+
+ get_download_dir_mock.assert_not_called()
+ storage.list_downloaded_files_in_browser_session.assert_not_called()
+ popup.close.assert_awaited_once()
+ scraped_page._browser_state.navigate_to_url.assert_awaited_once_with(page=page, url="https://example.test/files")
+ assert not action.download_triggered and not action.downloaded_files
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize("eager", [None, "remote"])
async def test_false_click_post_settle_empty_artifact_is_uncredited(tmp_path: Path, eager: str | None) -> None: