Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Import the curator worker through its cloud package in the advisory boundary test (#9001)
LawyZheng98d0ef7a738e
diff --git a/skyvern/forge/sdk/utils/capped_subprocess.py b/skyvern/forge/sdk/utils/capped_subprocess.pyindex c938eacc2..4367b6258 100644--- a/skyvern/forge/sdk/utils/capped_subprocess.py+++ b/skyvern/forge/sdk/utils/capped_subprocess.py@@ -340,7 +340,7 @@ def _run_child(timer.start()watchdog.start()# A child that died before reading its request closes the pipe; reading its exit below reports that. Stdin- # stays open until the child's memory has been read, because the child waits for it to close before it exits.+ # stays open for the ready acknowledgement and until the child's memory has been read.with contextlib.suppress(BrokenPipeError):stdin.write(struct.pack("<Q", len(request)) + request)stdin.flush()@@ -349,6 +349,8 @@ def _run_child(loaded = _PlainDataUnpickler(reader).load()if isinstance(loaded, dict) and loaded.get("status") == "ready":watchdog.learn(_checked_ready(loaded))+ stdin.write(b"\x00")+ stdin.flush()# A new unpickler per message, because one unpickler numbers its memo across the messages it loads.loaded = _PlainDataUnpickler(reader).load()outcome = _checked_outcome(loaded)@@ -377,6 +379,7 @@ def _run_child(duration_seconds = round(time.monotonic() - started, 2)applied_allowance = outcome.get("allowance_bytes", allowance) if outcome is not None else allowance+ memory_limit_exceeded = watchdog.growth_bytes is not None and watchdog.growth_bytes > watchdog.allowance_bytesmemory_fields = {"rss_baseline_bytes": watchdog.baseline_bytes,"rss_peak_sampled_bytes": watchdog.peak_sampled_bytes,@@ -384,7 +387,7 @@ def _run_child("recheck_headroom_bytes": outcome.get("headroom_bytes") if outcome is not None else None,"recheck_headroom_source": outcome.get("headroom_source") if outcome is not None else None,}- if outcome is not None and outcome["status"] == "ok":+ if outcome is not None and outcome["status"] == "ok" and not memory_limit_exceeded:LOG.info("Capped subprocess finished",target=target,@@ -400,6 +403,11 @@ def _run_child(if result_too_large or status == "result_too_large":kind = "result_too_large"message = f"returned more than the {_describe_bytes(max_result_bytes)} a result may hold"+ elif watchdog.killed.is_set() or memory_limit_exceeded:+ kind = "out_of_memory"+ message = (+ f"ran out of memory: it needed more than the {watchdog.allowance_bytes // (1024 * 1024)} MiB it may use"+ )elif outcome is not None and status == "error":kind = "error"message = outcome["message"]@@ -410,11 +418,6 @@ def _run_child(elif status == "no_headroom":kind = "no_headroom"message = _NO_HEADROOM_MESSAGE- elif watchdog.killed.is_set():- kind = "out_of_memory"- message = (- f"ran out of memory: it needed more than the {watchdog.allowance_bytes // (1024 * 1024)} MiB it may use"- )elif result_problem is not None:kind = "crashed"message = f"returned a result that {result_problem}"diff --git a/skyvern/forge/sdk/utils/capped_subprocess_child.py b/skyvern/forge/sdk/utils/capped_subprocess_child.pyindex 21acd28b5..cbdd01dc0 100644--- a/skyvern/forge/sdk/utils/capped_subprocess_child.py+++ b/skyvern/forge/sdk/utils/capped_subprocess_child.py@@ -56,9 +56,11 @@ def main() -> None:if allowance is None:raise _NoHeadroom_apply_limits(allowance, request["cpu_seconds"])- # The parent reads this process's resident memory when it gets this message, so growth counts from here.+ # The parent samples this ready state before acknowledging work, so growth counts from here.pickle.dump({"status": "ready", "allowance_bytes": allowance}, result_stream, protocol=pickle.HIGHEST_PROTOCOL)result_stream.flush()+ if sys.stdin.buffer.read(1) != b"\x00":+ returnoutcome = {"status": "ok", "value": func(*args)}except _NoHeadroom:outcome = {"status": "no_headroom"}diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex f9eabb125..c2734e69c 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -5264,6 +5264,11 @@ class ActionHandler:attempt_started_at=false_click_attempt_started_at,)action.finished_at = naive_utc_now()+ # _handle_action stamped the status from the results it returned, but the download+ # lifecycle above can still downgrade the terminal result (a settled false-click close+ # here, a replaced result on the download path). Re-derive before the row is written so+ # the persisted status can never disagree with the result the run reports.+ action.status = _terminal_action_status(results)persisted_action = await app.DATABASE.workflow_params.create_action(action=skyvern_context.action_for_multi_field_totp_persistence(action, app.WORKFLOW_CONTEXT_MANAGER.get_one_time_codes_for_run(action.workflow_run_id)@@ -6082,6 +6087,8 @@ class ActionHandler:download_triggered=download_triggered,)results[-1].download_failure_status = preserved_download_failure_status+ if "results" in locals() and results:+ action.status = _terminal_action_status(results)persisted_action = await app.DATABASE.workflow_params.create_action(action=skyvern_context.action_for_multi_field_totp_persistence(action, app.WORKFLOW_CONTEXT_MANAGER.get_one_time_codes_for_run(action.workflow_run_id)diff --git a/tests/unit/test_capped_subprocess.py b/tests/unit/test_capped_subprocess.pyindex 6961fd4e7..a7deb68fd 100644--- a/tests/unit/test_capped_subprocess.py+++ b/tests/unit/test_capped_subprocess.py@@ -174,7 +174,33 @@ def test_a_child_that_outgrows_its_allowance_is_killed_and_reported_out_of_memorassert "ran out of memory" in str(excinfo.value)-def test_the_growth_of_a_child_is_measured_by_the_parent_before_the_child_exits() -> None:+def test_a_child_that_returns_past_its_allowance_is_still_reported_out_of_memory(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ monkeypatch.setattr(capped_subprocess, "MEMORY_CAP_ENFORCED", False)+ monkeypatch.setattr(capped_subprocess, "_WATCHDOG_POLL_SECONDS", 10)++ with pytest.raises(CappedSubprocessError) as excinfo:+ run_in_capped_subprocess(+ _keep_memory_and_return, 50, memory_limit_bytes=40 * MIB, max_result_bytes=MIB, timeout_seconds=60+ )++ assert excinfo.value.kind == "out_of_memory"+++def test_the_growth_of_a_child_is_measured_by_the_parent_before_the_child_exits(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ sample = capped_subprocess._MemoryWatchdog.sample++ def delayed_baseline_sample(watchdog: capped_subprocess._MemoryWatchdog) -> int | None:+ if watchdog.baseline_bytes is None:+ # Reproduce a parent preempted between receiving ready and sampling its baseline.+ time.sleep(0.5)+ return sample(watchdog)++ monkeypatch.setattr(capped_subprocess._MemoryWatchdog, "sample", delayed_baseline_sample)+with structlog.testing.capture_logs() as logs:run_in_capped_subprocess(_keep_memory_and_return, 50, memory_limit_bytes=256 * MIB, max_result_bytes=MIB, timeout_seconds=60diff --git a/tests/unit/test_download_file_action_handler.py b/tests/unit/test_download_file_action_handler.pyindex 9c43c7b2f..f0c7c2425 100644--- a/tests/unit/test_download_file_action_handler.py+++ b/tests/unit/test_download_file_action_handler.py@@ -18,6 +18,7 @@ from structlog.testing import capture_logsfrom skyvern.config import settingsfrom skyvern.constants import BROWSER_DOWNLOAD_TIMEOUT, BROWSER_DOWNLOADING_SUFFIX+from skyvern.errors.errors import UserDefinedErrorfrom skyvern.exceptions import BlockedHostfrom skyvern.forge.agent import ForgeAgentfrom skyvern.forge.sdk.core.skyvern_context import SkyvernContext@@ -1226,6 +1227,59 @@ async def test_handle_action_recovers_working_page_closed_by_download_click() ->assert results[-1].download_triggered is False+async def test_persisted_status_follows_a_result_the_download_wrapper_downgraded() -> None:+ """The inner handler succeeds, then the download lifecycle finds no file and replaces the+ terminal result with a failure. The status written to the actions row is stamped before that+ replacement, so without a re-derive the run reports status=completed alongside success=False."""+ now = datetime.now(UTC)+ organization = make_organization(now)+ task, step, page, browser_state, scraped_page, action = _make_download_click_context(+ now=now,+ organization=organization,+ page_url="https://example.com/downloads",+ )+ task = task.model_copy(update={"download_timeout": 0.01})+ action.errors = [+ UserDefinedError(error_code="no_file_registered", reasoning="no file was registered", confidence_float=1.0)+ ]+ page.evaluate = AsyncMock(return_value=[])+ page.context._skyvern_cdp_download_active = False+ persisted_statuses: list[ActionStatus] = []++ async def succeed_and_stamp(*args: object, **kwargs: object) -> list[ActionSuccess]:+ # What the real _handle_action does in its finally block for a successful result.+ action.status = ActionStatus.completed+ return [ActionSuccess()]++ with tempfile.TemporaryDirectory() as temp_dir:+ mock_app = MagicMock()+ mock_app.BROWSER_MANAGER.get_for_task.return_value = browser_state+ mock_app.DATABASE.workflow_params.create_action = AsyncMock(+ side_effect=lambda action: persisted_statuses.append(action.status) or action+ )+ mock_app.STORAGE = MagicMock()++ with (+ patch.object(ActionHandler, "_handle_action", side_effect=succeed_and_stamp),+ patch("skyvern.webeye.actions.handler.get_download_dir", return_value=temp_dir),+ patch("skyvern.webeye.actions.handler.list_files_in_directory", return_value=[]),+ patch("skyvern.webeye.actions.handler.skyvern_context.current", return_value=None),+ patch("skyvern.webeye.actions.handler.app", mock_app),+ ):+ results = await ActionHandler.handle_action(+ scraped_page=scraped_page,+ task=task,+ step=step,+ page=page,+ action=action,+ )++ assert results[-1].success is False+ assert results[-1].download_triggered is False+ assert persisted_statuses == [ActionStatus.failed]++@pytest.mark.asyncioasync def test_handle_action_download_recovery_new_context_does_not_claim_preexisting_page() -> None:"""Working-page recovery can reconnect into a NEW BrowserContext, replacing the working page. The