Skip to content

Automate browser based workflows with AI

Turn on the orphan-schedule sweep's backend deletes for the production cron worker (SKY-18564) (#9067)

Shuchang Zheng9a5f750c04da
diff --git a/tests/unit/test_code_block_captcha.py b/tests/unit/test_code_block_captcha.py
index c73c0a157..9d1cc0c2d 100644
--- a/tests/unit/test_code_block_captcha.py
+++ b/tests/unit/test_code_block_captcha.py
@@ -1,7 +1,6 @@
from __future__ import annotations
import asyncio
-import time
from collections.abc import Mapping
from datetime import datetime, timezone
from types import SimpleNamespace
@@ -26,6 +25,7 @@ from skyvern.schemas.self_heal import HealClassification, HealSkipReason
from skyvern.webeye.actions.action_types import ActionType
from skyvern.webeye.actions.actions import ActionStatus
from skyvern.webeye.utils import captcha_solver as captcha_solver_module
+from skyvern.webeye.utils import challenge_signature
from skyvern.webeye.utils.captcha_solver import (
MAX_IMAGE_CAPTCHA_READS,
CaptchaChallengeUnsolvedError,
@@ -42,6 +42,7 @@ from tests.unit.copilot_test_helpers import (
challenge_browser_page,
skip_no_browser,
)
+from tests.unit.scoped_asyncio import ScopedAsyncio
CHALLENGE_SUBDOMAIN_URL = "https://edge.challenges.cloudflare.com/cdn-cgi/challenge-platform/fake"
SOLVED = ChallengeOutcome(ChallengeStatus.SOLVED, None, "extension")
@@ -1302,25 +1303,91 @@ async def test_nested_onscreen_match_after_an_offscreen_one_is_detected(monkeypa
assert await solve_challenge_ladder(page, probe_child_frames=True) is True, "an on-screen match must count"
+class _InFlight:
+ def __init__(self) -> None:
+ self.count = 0
+ self.peaks: list[int] = []
+ self._peak = 0
+
+ def enter(self) -> None:
+ self.count += 1
+ self._peak = max(self._peak, self.count)
+
+ def leave(self) -> None:
+ self.count -= 1
+ if self.count == 0:
+ self.peaks.append(self._peak)
+ self._peak = 0
+
+
class _WedgedFrame:
url = "https://app.example/wedged"
parent_frame = object()
+ def __init__(self, in_flight: _InFlight | None = None) -> None:
+ self.in_flight = in_flight if in_flight is not None else _InFlight()
+ self.release = asyncio.Event()
+
async def frame_element(self) -> None:
- await asyncio.Event().wait()
+ self.in_flight.enter()
+ try:
+ await self.release.wait()
+ finally:
+ self.in_flight.leave()
+
+
+class _WedgedChallengeFrame(_WedgedFrame):
+ url = CHALLENGE_URL
+
+
+def _unbounded_timeout(_delay: float | None) -> asyncio.Timeout:
+ return asyncio.timeout(None)
+
+
+async def _verdict_over_wedged_frames(
+ frames: list[_WedgedFrame], in_flight: _InFlight, *, probe_child_frames: bool = False
+) -> bool:
+ page = FakePage(frames=frames) # type: ignore[arg-type]
+ try:
+ verdict = await asyncio.wait_for(solve_challenge_ladder(page, probe_child_frames=probe_child_frames), 30)
+ leaked = in_flight.count
+ except TimeoutError:
+ pytest.fail("no budget ended the probes of frames that never answer")
+ finally:
+ for frame in frames:
+ frame.release.set()
+ assert leaked == 0, f"{leaked} wedged frame probes outlived the verdict"
+ return verdict
@pytest.mark.asyncio
async def test_wedged_child_frames_cannot_outlast_the_scan_budget(monkeypatch: pytest.MonkeyPatch) -> None:
- # Each frame is bounded on its own, so without a whole-scan budget five wedged frames would cost five
- # full per-frame bounds before the ladder could even decide nothing is there.
+ # Every asyncio.timeout bound in the solver is lifted, so only the whole-scan budget can end a scan of frames
+ # that never answer.
monkeypatch.setattr(app, "AGENT_FUNCTION", AgentFunction())
- monkeypatch.setattr(captcha_solver_module, "_CHILD_FRAME_SCAN_BUDGET_SECONDS", 0.3)
- page = FakePage(frames=[_WedgedFrame() for _ in range(5)])
+ monkeypatch.setattr(captcha_solver_module, "_CHILD_FRAME_SCAN_BUDGET_SECONDS", 0.01)
+ monkeypatch.setattr(captcha_solver_module, "asyncio", ScopedAsyncio(timeout=_unbounded_timeout))
+ in_flight = _InFlight()
+ frames = [_WedgedFrame(in_flight) for _ in range(5)]
- started = time.monotonic()
- assert await solve_challenge_ladder(page, probe_child_frames=True) is False
- assert time.monotonic() - started < 1.0
+ assert await _verdict_over_wedged_frames(frames, in_flight, probe_child_frames=True) is False
+ assert in_flight.peaks == [len(frames)], "the scan must hold every wedged frame at once, so five cost one budget"
+
+
+async def test_wedged_challenge_frames_read_absent_on_the_default_path(monkeypatch: pytest.MonkeyPatch) -> None:
+ # An absent verdict costs one frame scan and one rendered-frame probe, and each holds every challenge frame at
+ # once, so five wedged frames cost each step's bound once.
+ monkeypatch.setattr(app, "AGENT_FUNCTION", AgentFunction())
+ monkeypatch.setattr(captcha_solver_module, "_CHILD_FRAME_SCAN_BUDGET_SECONDS", 0.01)
+ monkeypatch.setattr(challenge_signature, "_CHALLENGE_FRAME_PROBE_TIMEOUT_SECONDS", 0.01)
+ in_flight = _InFlight()
+ frames: list[_WedgedFrame] = [_WedgedChallengeFrame(in_flight) for _ in range(5)]
+
+ assert await _verdict_over_wedged_frames(frames, in_flight) is False, "frames that never answer must read absent"
+ assert in_flight.peaks == [len(frames)] * 2, (
+ "the frame scan and then the rendered-frame probe must each hold every challenge frame at once"
+ )
class _DetachedFrame: