Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Redesign the low-credit banner as a single meter row (SKY-17994) (#8841)
Shuchang Zheng53686765b0ad
diff --git a/tests/unit/test_code_block_steps.py b/tests/unit/test_code_block_steps.pyindex e1c28bcae..89ecfafbe 100644--- a/tests/unit/test_code_block_steps.py+++ b/tests/unit/test_code_block_steps.py@@ -1,3 +1,4 @@+import mathimport textwrapimport timeimport timeit@@ -513,9 +514,17 @@ def test_goto_address_that_is_not_a_plain_http_address_names_the_variable(addresdef _derive_seconds(code: str) -> float:- # Thread CPU time, not wall time: a busy neighbour thread stalls a long run at every GIL switch while a- # run shorter than the switch interval escapes it, which doubles the ratio. timeit pauses the garbage collector.- return min(timeit.repeat(lambda: derive_code_block_steps(code), number=1, repeat=3, timer=time.thread_time))+ # Thread CPU time, so a busy neighbour thread cannot stall one run; timeit pauses the garbage collector.+ return timeit.timeit(lambda: derive_code_block_steps(code), number=1, timer=time.thread_time)+++def _derive_growth(small: str, large: str) -> float:+ # A busy core still slows CPU time, so the runs alternate and the best of each is compared.+ small_best = large_best = math.inf+ for _ in range(5):+ small_best = min(small_best, _derive_seconds(small))+ large_best = min(large_best, _derive_seconds(large))+ return large_best / small_bestdef test_many_gotos_on_variables_derive_in_linear_time():@@ -523,8 +532,8 @@ def test_many_gotos_on_variables_derive_in_linear_time():return "".join(f"url_{i} = 'https://example.com/{i}'\nawait page.goto(url_{i})\n" for i in range(count))assert derive_code_block_steps(gotos(2000))[-1]["description"] == "Open https://example.com/1999"- # Quadratic growth makes 4x the gotos take ~16x as long; linear stays near 4x.- assert _derive_seconds(gotos(2000)) < 8 * _derive_seconds(gotos(500))+ # Quadratic growth makes 8x the gotos take ~64x as long; linear stays near 8x.+ assert _derive_growth(gotos(250), gotos(2000)) < 24def test_unclosed_template_openers_derive_in_linear_time():@@ -532,8 +541,8 @@ def test_unclosed_template_openers_derive_in_linear_time():return "await page.goto('https://example.com/a')\n" + "x = 1 # {# note {% here\n" * countassert derive_code_block_steps(commented(2000))[0]["description"] == "Open https://example.com/a"- # Quadratic growth makes 4x the lines take ~16x as long; linear stays near 4x.- assert _derive_seconds(commented(2000)) < 8 * _derive_seconds(commented(500))+ # Quadratic growth makes 8x the lines take ~64x as long; linear stays near 8x.+ assert _derive_growth(commented(250), commented(2000)) < 24def test_reads_chained_in_one_expression_derive_in_linear_time():@@ -542,8 +551,8 @@ def test_reads_chained_in_one_expression_derive_in_linear_time():# Python 3.11's parser rejects much longer chains, which would return [] and pass vacuously.assert derive_code_block_steps(chained(2000))[0]["description"] == "Extract total"- # Quadratic growth makes 4x the reads take ~16x as long; linear stays near 4x.- assert _derive_seconds(chained(2000)) < 8 * _derive_seconds(chained(500))+ # Quadratic growth makes 8x the reads take ~64x as long; linear stays near 8x.+ assert _derive_growth(chained(250), chained(2000)) < 24def test_code_too_deep_for_the_parser_derives_no_steps():diff --git a/tests/unit/test_error_detection_integration.py b/tests/unit/test_error_detection_integration.pyindex 8823859e5..6871d0a2d 100644--- a/tests/unit/test_error_detection_integration.py+++ b/tests/unit/test_error_detection_integration.py@@ -600,8 +600,8 @@ def test_response_error_filter_keeps_case_variant_under_the_mapping_key(agent):@pytest.mark.asyncio-async def test_error_detection_performance_doesnt_block_failure(agent, mock_browser_state):- """Test that slow error detection doesn't significantly delay task failure."""+async def test_slow_error_detection_runs_once_after_the_failure_is_saved(agent, mock_browser_state):+ """The failure is saved before slow error detection starts, and detection is awaited exactly once."""now = datetime.now()organization = make_organization(now)task = make_task(@@ -620,8 +620,14 @@ async def test_error_detection_performance_doesnt_block_failure(agent, mock_brow# Simulate slow error detectionimport asyncio+ detection_finished = asyncio.Event()+ saves_before_detection: list[int] = []+async def slow_detection(*args, **kwargs):+ # fail_task swallows detection errors, so the save count is checked after it returns.+ saves_before_detection.append(mock_update_task.await_count)await asyncio.sleep(0.1) # Simulate some delay+ detection_finished.set()return [UserDefinedError(error_code="timeout", reasoning="Timeout detected", confidence_float=0.80)]with patch(@@ -633,15 +639,16 @@ async def test_error_detection_performance_doesnt_block_failure(agent, mock_browwith patch("skyvern.forge.agent.app") as mock_app:mock_app.DATABASE.tasks.update_task = AsyncMock()- import time-- start_time = time.time()- result = await agent.fail_task(task, step, "Task timeout", mock_browser_state)- elapsed = time.time() - start_time+ # The wait_for only turns a hang into a failure; counts, not wall time, show there is no retry loop.+ result = await asyncio.wait_for(+ agent.fail_task(task, step, "Task timeout", mock_browser_state), timeout=30+ )- # Should complete (error detection runs but doesn't block indefinitely)assert result is True- # Should take at least 0.1s (the sleep time)- assert elapsed >= 0.1- # But not much more (no retry loops or hangs)- assert elapsed < 1.0+ assert detection_finished.is_set()+ assert saves_before_detection == [1]+ assert mock_detect.await_count == 1+ mock_app.DATABASE.tasks.update_task.assert_awaited_once()+ assert [+ e["error_code"] for e in mock_app.DATABASE.tasks.update_task.call_args.kwargs["errors"]+ ] == ["timeout"]diff --git a/tests/unit/test_taskv3_opaque_refs.py b/tests/unit/test_taskv3_opaque_refs.pyindex 52d886a23..9eec9c140 100644--- a/tests/unit/test_taskv3_opaque_refs.py+++ b/tests/unit/test_taskv3_opaque_refs.py@@ -2,7 +2,9 @@from __future__ import annotations+import mathimport time+import timeitfrom typing import Any, Callableimport pytest@@ -724,13 +726,20 @@ def test_mask_canonical_match_handles_thousands_of_quote_glued_copies_iterativeltoken = next(iter(refs.refs))echoed = "https://example.test/f?token=abcdefghijklmnop0123"- def timed(copies: int) -> float:- started = time.perf_counter()+ def mask(copies: int) -> None:assert refs.mask("'".join([echoed] * copies)) == "'".join([token] * copies)- return time.perf_counter() - started- # Linear, not quadratic: four times the copies costs well under sixteen times the work.- assert timed(4000) < 6 * max(timed(1000), 0.005)+ def timed(copies: int) -> float:+ return timeit.timeit(lambda: mask(copies), number=1, timer=time.thread_time)++ # Thread CPU time with the garbage collector paused, alternating sizes and keeping the best of each, so a busy+ # runner slows both alike.+ small_best = large_best = math.inf+ for _ in range(5):+ small_best = min(small_best, timed(500))+ large_best = min(large_best, timed(4000))+ # Linear, not quadratic: eight times the copies costs ~8x the work, where quadratic would cost ~64x.+ assert large_best < 16 * small_bestdef test_mask_canonical_match_is_still_by_membership() -> None: