Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
SKY-10711: trim redundant full-DOM re-scrape in composition inspect; drop dead page-evidence cache (#6426)
Andrew Neilsondc92711e5fc2
diff --git a/skyvern/forge/sdk/copilot/tools.py b/skyvern/forge/sdk/copilot/tools.pyindex de514965d..0c2155313 100644--- a/skyvern/forge/sdk/copilot/tools.py+++ b/skyvern/forge/sdk/copilot/tools.py@@ -7846,7 +7846,7 @@ async def _composition_evidence_after_navigation_failure(current_url = current_url or inspected_url# Same size-cap survival as the success path: a heavy page that rendered before the nav# error still parses via the stripped-body evaluate instead of yielding hollow evidence.- html, html_error, html_truncated = await _composition_get_html(ctx)+ html, html_error, html_truncated, _ = await _composition_get_html(ctx)if html_error is None:evidence = parse_composition_html(html, inspected_url=inspected_url, current_url=current_url)evidence = _composition_add_inspection_warning(@@ -8085,7 +8085,7 @@ async def _augment_composition_evidence_with_computed_obstruction_candidates(return _merge_visual_obstruction_candidates(evidence, candidates)-async def _composition_get_html(copilot_ctx: Any) -> tuple[str, str | None, bool]:+async def _composition_get_html(copilot_ctx: Any, *, skip_raw: bool = False) -> tuple[str, str | None, bool, bool]:"""Return body HTML for composition parsing, surviving the MCP response size cap.`skyvern_get_html("body")` is the fast path, but the shared size cap DROPS the@@ -8094,20 +8094,25 @@ async def _composition_get_html(copilot_ctx: Any) -> tuple[str, str | None, boolparse an empty string and report hollow evidence. On an empty/capped read, fallback to an `evaluate` that returns the body with script/style/svg/etc. strippedand length-bounded; that fits under the cap while preserving the form/link- structure. Returns (html, error, truncated): error is set only on a hard read- failure; truncated is True when the stripped fallback was sliced at the cap (the- raw path is never silently truncated — it is dropped over the cap and falls back).+ structure. Returns (html, error, truncated, used_stripped): error is set only on+ a hard read failure; truncated is True when the stripped fallback was sliced at+ the cap; used_stripped is True when the bounded read was the source (raw skipped+ or cap-dropped). `skip_raw` goes straight to the stripped read so a caller that+ has already seen the raw serialization get cap-dropped for this page need not+ re-issue it."""- html_result = await _discovery_get_html(copilot_ctx)- if html_result.get("ok"):- html = _discovery_extract_html_payload(html_result)- if html.strip():- return html, None, False+ html_result: dict[str, Any] = {}+ if not skip_raw:+ html_result = await _discovery_get_html(copilot_ctx)+ if html_result.get("ok"):+ html = _discovery_extract_html_payload(html_result)+ if html.strip():+ return html, None, False, Falsestripped, truncated = await _composition_get_stripped_html(copilot_ctx)if stripped and stripped.strip():- return stripped, None, truncated+ return stripped, None, truncated, Trueerror = html_result.get("error")- return "", str(error) if error else None, False+ return "", str(error) if error else None, False, Trueasync def _capture_composition_evidence(@@ -8129,10 +8134,16 @@ async def _capture_composition_evidence("""evidence: dict[str, Any] | None = Nonehtml_truncated = False+ skip_raw = Falsefor attempt in range(_COMPOSITION_HOLLOW_RECAPTURE_RETRIES + 1):- html, html_error, html_truncated = await _composition_get_html(copilot_ctx)+ html, html_error, html_truncated, used_stripped = await _composition_get_html(copilot_ctx, skip_raw=skip_raw)if html_error is not None:return None, html_error+ # On a heavy page the raw get_html serialization is dropped over the MCP size cap and+ # falls back to the stripped read; once that happens, settle-and-recapture via the+ # stripped path only so a slow page is still retried without re-serializing the full DOM.+ if used_stripped:+ skip_raw = Trueevidence = parse_composition_html(html, inspected_url=inspected_url, current_url=current_url)if has_bounded_page_schema(evidence):break@@ -8151,6 +8162,39 @@ async def _capture_composition_evidence(return evidence, None+def _normalized_inspect_url(url: str | None) -> str | None:+ """Normalized full URL for strict same-page comparison, or None when not comparable.++ Preserves scheme, the path's trailing slash, query, and fragment so distinct rendered+ states (http vs https, /p vs /p/, ?q=a vs ?q=b, hash-routed SPA states) never collide;+ only netloc case and an empty root path are normalized.+ """+ if not isinstance(url, str) or not url.strip():+ return None+ try:+ parsed = urlparse(url.strip())+ except Exception:+ return None+ if parsed.scheme.lower() not in {"http", "https"} or not parsed.netloc:+ return None+ path = parsed.path or "/"+ query = f"?{parsed.query}" if parsed.query else ""+ fragment = f"#{parsed.fragment}" if parsed.fragment else ""+ return f"{parsed.scheme.lower()}://{parsed.netloc.lower()}{path}{query}{fragment}"+++def _same_inspect_target(live_url: str | None, target_url: str | None) -> bool:+ """True when the live page is the exact page a URL-target inspect would navigate to.++ Strict full-URL equality, so a different scheme, trailing slash, query, or fragment+ still navigates. Used to skip the re-navigation when the agent is already standing on+ the requested page.+ """+ live_key = _normalized_inspect_url(live_url)+ target_key = _normalized_inspect_url(target_url)+ return live_key is not None and live_key == target_key++async def _inspect_page_for_composition_impl(copilot_ctx: Any,target_url: str,@@ -8176,28 +8220,6 @@ async def _inspect_page_for_composition_impl(use_current_page=use_current_page,)- if (- not bypass_budget_for_post_run_current_page- and copilot_ctx.page_inspection_calls_this_turn >= _COMPOSITION_INSPECTION_PER_TURN_BUDGET- ):- result = {- "ok": False,- "data": None,- "error": _page_inspection_budget_error(copilot_ctx, scope="turn"),- }- record_tool_step_result_for_ctx(copilot_ctx, "inspect_page_for_composition", arguments, result)- return result-- cumulative = copilot_ctx.prior_page_inspection_calls_made + copilot_ctx.page_inspection_calls_this_turn- if not bypass_budget_for_post_run_current_page and cumulative >= _COMPOSITION_INSPECTION_PER_CHAT_BUDGET:- result = {- "ok": False,- "data": None,- "error": _page_inspection_budget_error(copilot_ctx, scope="chat"),- }- record_tool_step_result_for_ctx(copilot_ctx, "inspect_page_for_composition", arguments, result)- return result-entry_url: strkind: strif use_current_page:@@ -8220,23 +8242,51 @@ async def _inspect_page_for_composition_impl(record_tool_step_result_for_ctx(copilot_ctx, "inspect_page_for_composition", arguments, regression_error)return regression_error+ # Skip re-navigation when the inspect target is the page the browser is already on. A+ # passive client-side redirect can move the browser without a tool, so for a URL target+ # confirm against the live URL; for current_page the live URL is the target by definition.+ if use_current_page:+ inspect_target_url = current_url+ on_target_page = True+ else:+ live_url, _ = await _fallback_page_info(copilot_ctx)+ on_target_page = _same_inspect_target(live_url, entry_url)+ inspect_target_url = live_url if on_target_page else entry_url++ if (+ not bypass_budget_for_post_run_current_page+ and copilot_ctx.page_inspection_calls_this_turn >= _COMPOSITION_INSPECTION_PER_TURN_BUDGET+ ):+ result = {+ "ok": False,+ "data": None,+ "error": _page_inspection_budget_error(copilot_ctx, scope="turn"),+ }+ record_tool_step_result_for_ctx(copilot_ctx, "inspect_page_for_composition", arguments, result)+ return result++ cumulative = copilot_ctx.prior_page_inspection_calls_made + copilot_ctx.page_inspection_calls_this_turn+ if not bypass_budget_for_post_run_current_page and cumulative >= _COMPOSITION_INSPECTION_PER_CHAT_BUDGET:+ result = {+ "ok": False,+ "data": None,+ "error": _page_inspection_budget_error(copilot_ctx, scope="chat"),+ }+ record_tool_step_result_for_ctx(copilot_ctx, "inspect_page_for_composition", arguments, result)+ return result++ evidence = None+ html_error: str | None = Nonewith copilot_span("inspect_page_for_composition",data={"target_url_kind": kind},):- if use_current_page:- current_url = entry_url+ if on_target_page:+ # current_page, or a URL target the agent is already on — capture without navigating.+ current_url = inspect_target_url or entry_urlevidence, html_error = await _capture_composition_evidence(copilot_ctx, inspected_url=entry_url, current_url=current_url)- if html_error is not None:- result = {- "ok": False,- "data": None,- "error": f"inspect_page_for_composition could not read page HTML: {html_error}",- }- record_tool_step_result_for_ctx(copilot_ctx, "inspect_page_for_composition", arguments, result)- return resultelse:nav_result = await _discovery_navigate(copilot_ctx,@@ -8266,14 +8316,15 @@ async def _inspect_page_for_composition_impl(evidence, html_error = await _capture_composition_evidence(copilot_ctx, inspected_url=entry_url, current_url=current_url)- if html_error is not None:- result = {- "ok": False,- "data": None,- "error": f"inspect_page_for_composition could not read page HTML: {html_error}",- }- record_tool_step_result_for_ctx(copilot_ctx, "inspect_page_for_composition", arguments, result)- return result++ if html_error is not None:+ result = {+ "ok": False,+ "data": None,+ "error": f"inspect_page_for_composition could not read page HTML: {html_error}",+ }+ record_tool_step_result_for_ctx(copilot_ctx, "inspect_page_for_composition", arguments, result)+ return resultif evidence is None:result = {@@ -8405,7 +8456,7 @@ async def _discovery_walk(# fall back to a stripped-body evaluate that keeps the links/forms the resolver needs# to identify a usable entrypoint. (Discovery only resolves the entrypoint, so a sliced# tail does not matter here.)- html, _, _ = await _composition_get_html(ctx)+ html, _, _, _ = await _composition_get_html(ctx)page_title, anchors, form_fields = _discovery_parse_html(html)evidence_trail.append(diff --git a/tests/unit/test_copilot_composition_scrape.py b/tests/unit/test_copilot_composition_scrape.pynew file mode 100644index 000000000..2966c7384--- /dev/null+++ b/tests/unit/test_copilot_composition_scrape.py@@ -0,0 +1,71 @@+"""Reduced SKY-10711 — skip-renavigation URL matching + the recapture loop's+doomed-raw-scrape trim. (The build-time page-evidence cache was removed: it never+served in a real scout because the agent acts between inspects.)+"""++from __future__ import annotations++from types import SimpleNamespace++import pytest++from skyvern.forge.sdk.copilot import tools+from skyvern.forge.sdk.copilot.tools import _normalized_inspect_url, _same_inspect_target+++def test_normalized_inspect_url_preserves_distinguishing_parts() -> None:+ assert _normalized_inspect_url("https://Example.com/Search?q=a#frag") == "https://example.com/Search?q=a#frag"+ # query distinguishes search states; scheme and trailing slash are significant+ assert _normalized_inspect_url("https://h/s?q=a") != _normalized_inspect_url("https://h/s?q=b")+ assert _normalized_inspect_url("http://h/p") != _normalized_inspect_url("https://h/p")+ assert _normalized_inspect_url("https://h/p") != _normalized_inspect_url("https://h/p/")+ # empty root path collapses to "/"+ assert _normalized_inspect_url("https://h") == _normalized_inspect_url("https://h/")+++def test_normalized_inspect_url_rejects_non_http() -> None:+ for value in ("", None, "current_page", "about:blank", "file:///tmp/x.html"):+ assert _normalized_inspect_url(value) is None+++def test_same_inspect_target_is_strict() -> None:+ assert _same_inspect_target("https://h/p?q=1", "https://h/p?q=1") is True+ assert _same_inspect_target("https://h/p?q=1", "https://h/p?q=2") is False+ assert _same_inspect_target("https://h/p", "https://h/p/") is False+ assert _same_inspect_target("current_page", "https://h/p") is False+++_HOLLOW_HTML = "<div>loading</div>"+_BOUNDED_HTML = "<form><input name='q'><button type='submit'>Go</button></form>"+++async def test_recapture_skips_raw_get_html_after_cap_drop(monkeypatch: pytest.MonkeyPatch) -> None:+ """On a heavy page the raw get_html is dropped over the MCP size cap; the settle retry+ must re-read via the stripped path only, not re-serialize the full DOM."""+ raw_calls = {"n": 0}+ stripped_payloads = iter([_HOLLOW_HTML, _BOUNDED_HTML])++ async def fake_raw(ctx: object) -> dict:+ raw_calls["n"] += 1+ return {"ok": True, "data": {}} # cap-dropped: no html payload -> forces stripped fallback++ async def fake_stripped(ctx: object) -> tuple[str, bool]:+ return next(stripped_payloads), False++ async def identity(ctx: object, evidence: dict) -> dict:+ return evidence++ monkeypatch.setattr(tools, "_discovery_get_html", fake_raw)+ monkeypatch.setattr(tools, "_composition_get_stripped_html", fake_stripped)+ monkeypatch.setattr(tools, "_augment_composition_evidence_with_computed_obstruction_candidates", identity)++ evidence, html_error = await tools._capture_composition_evidence(+ SimpleNamespace(), inspected_url="https://example.com/s", current_url="https://example.com/s"+ )++ assert html_error is None+ assert evidence is not None+ assert tools.has_bounded_page_schema(evidence)+ # First iteration's raw read is cap-dropped; the settle retry skips it entirely.+ assert raw_calls["n"] == 1diff --git a/tests/unit/test_copilot_discovery.py b/tests/unit/test_copilot_discovery.pyindex 63af6d2f3..74c0bd255 100644--- a/tests/unit/test_copilot_discovery.py+++ b/tests/unit/test_copilot_discovery.py@@ -23,6 +23,7 @@ class _Ctx:self.composition_page_evidence = Noneself.pending_browser_interaction_observation = Noneself.workflow_verification_evidence = WorkflowVerificationEvidence()+ self.browser_session_id = Noneclass _FailingNavigateServer:@@ -608,12 +609,12 @@ async def test_composition_get_html_flags_truncation_when_stripped_body_hits_capfrom skyvern.forge.sdk.copilot.tools import _COMPOSITION_STRIPPED_HTML_MAX_CHARS, _composition_get_htmlat_cap = "<body>" + "x" * _COMPOSITION_STRIPPED_HTML_MAX_CHARS- _, error, truncated = await _composition_get_html(_Ctx(_StrippedHtmlServer(at_cap)))+ _, error, truncated, _ = await _composition_get_html(_Ctx(_StrippedHtmlServer(at_cap)))assert error is Noneassert truncated is Trueunder_cap = "<body><form><input name='x'></form></body>"- _, error, truncated = await _composition_get_html(_Ctx(_StrippedHtmlServer(under_cap)))+ _, error, truncated, _ = await _composition_get_html(_Ctx(_StrippedHtmlServer(under_cap)))assert error is Noneassert truncated is False