Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
feat(SKY-10076): drop non-navigable hrefs from the lean element tree (#6461)
pedrohsdbd1adca189f1f
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.pyindex d331823ab..a42b42364 100644--- a/skyvern/forge/agent.py+++ b/skyvern/forge/agent.py@@ -2846,6 +2846,7 @@ class ForgeAgent:lean_compress_long_href=lean_enabled,lean_compress_image_src=lean_enabled,lean_strip_url_query_strings=lean_enabled,+ lean_compress_nonnavigable_href=lean_enabled,)# This prompt is critical for our agent, we probably should use the primary LLM handler@@ -3761,6 +3762,9 @@ class ForgeAgent:compress_long_href=False,compress_image_src=True,strip_url_query_strings=True,+ # non-navigable hrefs (javascript:/empty/'#') carry no+ # destination signal, so they're safe to drop for the planner.+ compress_nonnavigable_href=True,)else:elements_for_prompt = scraped_page.build_element_tree(element_tree_format)@@ -3900,6 +3904,7 @@ class ForgeAgent:lean_compress_long_href=False,lean_compress_image_src=context.enable_lean_element_tree,lean_strip_url_query_strings=context.enable_lean_element_tree,+ lean_compress_nonnavigable_href=context.enable_lean_element_tree,)# Map template to prompt_name for logging/caching guardsdiff --git a/skyvern/utils/lean_html.py b/skyvern/utils/lean_html.pyindex f1dfe357b..d66970ed7 100644--- a/skyvern/utils/lean_html.py+++ b/skyvern/utils/lean_html.py@@ -10,6 +10,10 @@ per template:1. compress_long_href 7.2% — long hrefs → `"#templated"`2. compress_image_src 4.3% — drops `<img src="…">`3. strip_url_query_strings 0.9% — strips `?…` from href/src+ 4. compress_nonnavigable_href — — drops non-navigable hrefs;+ strips `javascript:` wrappers+ on textless controls so the+ semantic payload survivesCombined potential ~12% planner savings. The largest single source of bloatin offline analysis was Skyvern internal IDs (~24% of the verifier prompt),@@ -36,6 +40,70 @@ from typing import Any_HASHED_HREF_PLACEHOLDER = "#templated"_HASHED_HREF_LEN_THRESHOLD = 150+# Bounds the _has_text_signal descent on adversarially deep DOMs (well above any+# real label nesting; hitting it returns no-signal, which keeps the href).+_TEXT_SIGNAL_MAX_DEPTH = 50+++def _is_pure_idiom_href(href: Any) -> bool:+ """A content-free href — empty, bare '#', or a javascript no-op. Carries no+ signal at all, so it is always safe to drop.++ Excludes hash routes (`#/checkout`) and same-page anchors (`#section`),+ which are real destinations. Broader `javascript:` payloads (e.g.+ `javascript:void(downloadPdf('123'))`) are NOT pure idioms — their inner+ expression names the control's action and is preserved via+ `_strip_javascript_wrapper` when no other label survives.+ """+ if not isinstance(href, str):+ return False+ stripped = href.strip()+ if stripped in ("", "#"):+ return True+ return stripped.lower() in ("javascript:", "javascript:;", "javascript:void(0)", "javascript:void(0);")+++_JS_NOOP_PAYLOADS = ("", "0", "null", "undefined")+++def _strip_javascript_wrapper(href: str) -> str:+ """Return the semantic payload of a `javascript:` href, stripping the scheme+ and an optional `void(...)` wrapper. Returns `""` when nothing meaningful+ remains (pure no-ops like `javascript:`, `javascript:void(undefined)`).++ Non-`javascript:` hrefs are returned unchanged (after a trim). Keeping the+ inner expression preserves the only naming signal on textless icon controls+ (`javascript:__doPostBack('lnkDownloadPDF','')` → `__doPostBack('lnkDownloadPDF','')`),+ while dropping the noisy wrapper for tokens.+ """+ s = href.strip()+ if not s.lower().startswith("javascript:"):+ return s+ payload = s[len("javascript:") :].strip().rstrip(";").strip()+ if payload.lower().startswith("void(") and payload.endswith(")"):+ payload = payload[len("void(") : -1].strip()+ return "" if payload.lower() in _JS_NOOP_PAYLOADS else payload+++def _has_text_signal(node: dict, _depth: int = 0) -> bool:+ """Whether the element keeps a human-readable label after slimming — its own+ visible text, an alt/aria-label/title, or a labeled descendant.++ The icon marker and machine-only ids do not count, so a truly textless+ control reads as no-signal and we preserve its (otherwise sole) href hint.+ """+ text = (node.get("text") or "").strip()+ if text and text != "[icon]":+ return True+ attributes = node.get("attributes") or {}+ for key in ("aria-label", "title", "alt"):+ value = attributes.get(key)+ if isinstance(value, str) and value.strip():+ return True+ if _depth >= _TEXT_SIGNAL_MAX_DEPTH:+ return False+ return any(_has_text_signal(child, _depth + 1) for child in node.get("children") or [])+def _transform_node(node: dict,@@ -43,6 +111,7 @@ def _transform_node(compress_long_href: bool,compress_image_src: bool,strip_url_query_strings: bool,+ compress_nonnavigable_href: bool,) -> dict | None:"""Apply the selected lean transforms to a single node and recurse into children.@@ -79,6 +148,26 @@ def _transform_node(if isinstance(href, str) and len(href) > _HASHED_HREF_LEN_THRESHOLD:attributes["href"] = _HASHED_HREF_PLACEHOLDER+ # 4. Drop non-navigable hrefs (the agent acts on the element id, not the+ # href). A javascript:/postback href can still carry a control-name hint, so+ # when a human-readable label survives, the wrapped expression is redundant+ # and we drop it; on a textless control we keep the inner expression as the+ # only naming signal and strip the noisy `javascript:`/`void(...)` wrapper.+ if compress_nonnavigable_href:+ href = attributes.get("href")+ if isinstance(href, str):+ if _is_pure_idiom_href(href):+ attributes.pop("href", None)+ elif href.strip().lower().startswith("javascript:"):+ if _has_text_signal(node):+ attributes.pop("href", None)+ else:+ payload = _strip_javascript_wrapper(href)+ if payload:+ attributes["href"] = payload+ else:+ attributes.pop("href", None)+node["attributes"] = attributeschildren = node.get("children")@@ -90,6 +179,7 @@ def _transform_node(compress_long_href=compress_long_href,compress_image_src=compress_image_src,strip_url_query_strings=strip_url_query_strings,+ compress_nonnavigable_href=compress_nonnavigable_href,)if transformed is not None:new_children.append(transformed)@@ -104,6 +194,7 @@ def apply_lean_to_tree(compress_long_href: bool = False,compress_image_src: bool = False,strip_url_query_strings: bool = False,+ compress_nonnavigable_href: bool = False,) -> list[dict]:"""Apply the deterministic lean-tree recipe to a list of element dicts.@@ -119,6 +210,7 @@ def apply_lean_to_tree(compress_long_href=compress_long_href,compress_image_src=compress_image_src,strip_url_query_strings=strip_url_query_strings,+ compress_nonnavigable_href=compress_nonnavigable_href,)if transformed is not None:out.append(transformed)diff --git a/skyvern/utils/prompt_engine.py b/skyvern/utils/prompt_engine.pyindex 3743645ba..9cf2b8f56 100644--- a/skyvern/utils/prompt_engine.py+++ b/skyvern/utils/prompt_engine.py@@ -83,6 +83,7 @@ def load_prompt_with_elements_tracked(lean_compress_long_href: bool = False,lean_compress_image_src: bool = False,lean_strip_url_query_strings: bool = False,+ lean_compress_nonnavigable_href: bool = False,**kwargs: Any,) -> tuple[str, dict[str, Any]]:"""Same as load_prompt_with_elements but also returns post-ceiling kwargs.@@ -92,7 +93,12 @@ def load_prompt_with_elements_tracked(inputs for caching should use these values instead of the pre-drop kwargsso two requests that render to the same final prompt share a cache key."""- lean_any = lean_compress_long_href or lean_compress_image_src or lean_strip_url_query_strings+ lean_any = (+ lean_compress_long_href+ or lean_compress_image_src+ or lean_strip_url_query_strings+ or lean_compress_nonnavigable_href+ )if lean_any and element_tree_builder.support_lean_elements_tree():elements = _sanitize_elements_for_prompt(element_tree_builder,@@ -101,6 +107,7 @@ def load_prompt_with_elements_tracked(compress_long_href=lean_compress_long_href,compress_image_src=lean_compress_image_src,strip_url_query_strings=lean_strip_url_query_strings,+ compress_nonnavigable_href=lean_compress_nonnavigable_href,),)else:@@ -189,6 +196,7 @@ def load_prompt_with_elements(lean_compress_long_href: bool = False,lean_compress_image_src: bool = False,lean_strip_url_query_strings: bool = False,+ lean_compress_nonnavigable_href: bool = False,**kwargs: Any,) -> str:prompt, _ = load_prompt_with_elements_tracked(@@ -199,6 +207,7 @@ def load_prompt_with_elements(lean_compress_long_href=lean_compress_long_href,lean_compress_image_src=lean_compress_image_src,lean_strip_url_query_strings=lean_strip_url_query_strings,+ lean_compress_nonnavigable_href=lean_compress_nonnavigable_href,**kwargs,)return promptdiff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex 457aa9897..cba5bdc8d 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -1467,6 +1467,7 @@ async def handle_sequential_click_for_dropdown(lean_compress_long_href=lean_enabled,lean_compress_image_src=lean_enabled,lean_strip_url_query_strings=lean_enabled,+ lean_compress_nonnavigable_href=lean_enabled,)distinct_id_for_override = task.workflow_run_id if task.workflow_run_id else task.task_idcheck_user_goal_handler = await resolve_check_user_goal_handler(@@ -4422,13 +4423,15 @@ async def select_from_emerging_elements(# Extract minimal subtrees rooted at new elements — avoids sending the full page DOM# which gets truncated on large pages, losing portal-rendered dropdown items.new_element_subtrees = _extract_new_subtrees(scraped_page_after_open.element_tree_trimmed, new_element_ids)+ _ctx = skyvern_context.current()+ lean_enabled = bool(_ctx and _ctx.enable_lean_element_tree)if new_element_subtrees:- _ctx = skyvern_context.current()- if _ctx and _ctx.enable_lean_element_tree:+ if lean_enabled:new_element_subtrees = apply_lean_to_tree(new_element_subtrees,compress_image_src=True,strip_url_query_strings=True,+ compress_nonnavigable_href=True,)incremental_html = "".join(json_to_html(element, need_skyvern_attrs=True) for element in new_element_subtrees)else:@@ -4437,7 +4440,17 @@ async def select_from_emerging_elements(current_element_id=current_element_id,new_element_id_count=len(new_element_ids),)- incremental_html = scraped_page_after_open.build_element_tree(html_need_skyvern_attrs=True)+ # Keep the recipe consistent under the one flag (SKY-10076): apply lean to+ # the full trimmed tree on the fallback path too, mirroring the branch above.+ fallback_tree = scraped_page_after_open.element_tree_trimmed+ if lean_enabled:+ fallback_tree = apply_lean_to_tree(+ fallback_tree,+ compress_image_src=True,+ strip_url_query_strings=True,+ compress_nonnavigable_href=True,+ )+ incremental_html = "".join(json_to_html(element, need_skyvern_attrs=True) for element in fallback_tree)LOG.debug("Built HTML for emerging-element custom-select",current_element_id=current_element_id,diff --git a/skyvern/webeye/scraper/scraped_page.py b/skyvern/webeye/scraper/scraped_page.pyindex 61ebfbb12..4735669b4 100644--- a/skyvern/webeye/scraper/scraped_page.py+++ b/skyvern/webeye/scraper/scraped_page.py@@ -157,6 +157,7 @@ class ElementTreeBuilder(ABC):compress_long_href: bool = False,compress_image_src: bool = False,strip_url_query_strings: bool = False,+ compress_nonnavigable_href: bool = False,) -> str:pass@@ -187,10 +188,10 @@ class ScrapedPage(BaseModel, ElementTreeBuilder):element_tree: list[dict]element_tree_trimmed: list[dict]economy_element_tree: list[dict] | None = None- # SKY-9718 Layer 1: lazy cache for lean trees, keyed by the 3-flag combo+ # SKY-9718 Layer 1: lazy cache for lean trees, keyed by the 4-flag combo# the caller asked for. Two call sites in one prompt build asking for# different combos each pay the walk cost once.- lean_element_tree_cache: dict[tuple[bool, bool, bool], list[dict]] = {}+ lean_element_tree_cache: dict[tuple[bool, bool, bool, bool], list[dict]] = {}last_used_element_tree: list[dict] | None = Nonelast_used_element_tree_html: str | None = Nonescreenshots: list[bytes] = []@@ -411,6 +412,7 @@ class ScrapedPage(BaseModel, ElementTreeBuilder):compress_long_href: bool = False,compress_image_src: bool = False,strip_url_query_strings: bool = False,+ compress_nonnavigable_href: bool = False,) -> str:"""SKY-9718 Layer 1 — deterministic lean element tree.@@ -431,6 +433,7 @@ class ScrapedPage(BaseModel, ElementTreeBuilder):compress_long_href,compress_image_src,strip_url_query_strings,+ compress_nonnavigable_href,)cached = self.lean_element_tree_cache.get(cache_key)if cached is None:@@ -439,6 +442,7 @@ class ScrapedPage(BaseModel, ElementTreeBuilder):compress_long_href=compress_long_href,compress_image_src=compress_image_src,strip_url_query_strings=strip_url_query_strings,+ compress_nonnavigable_href=compress_nonnavigable_href,)self.lean_element_tree_cache[cache_key] = cacheddiff --git a/skyvern/webeye/scraper/scraper.py b/skyvern/webeye/scraper/scraper.pyindex c73a9ce50..b465a87c9 100644--- a/skyvern/webeye/scraper/scraper.py+++ b/skyvern/webeye/scraper/scraper.py@@ -784,6 +784,7 @@ class IncrementalScrapePage(ElementTreeBuilder):compress_long_href: bool = False,compress_image_src: bool = False,strip_url_query_strings: bool = False,+ compress_nonnavigable_href: bool = False,) -> str:raise NotImplementedError("Not implemented")diff --git a/tests/unit/test_build_lean_elements_tree.py b/tests/unit/test_build_lean_elements_tree.pyindex ef476582f..f5713bd1c 100644--- a/tests/unit/test_build_lean_elements_tree.py+++ b/tests/unit/test_build_lean_elements_tree.py@@ -51,7 +51,8 @@ def test_cache_hit_same_flag_combo_returns_same_tree_object() -> None:# First call populates the cache.page.build_lean_elements_tree(strip_url_query_strings=True)- cache_key = (False, False, True)+ # key order: (compress_long_href, compress_image_src, strip_url_query_strings, compress_nonnavigable_href)+ cache_key = (False, False, True, False)assert cache_key in page.lean_element_tree_cachecached_tree = page.lean_element_tree_cache[cache_key]@@ -77,7 +78,7 @@ def test_cache_isolation_across_flag_combos() -> None:page.build_lean_elements_tree(compress_image_src=True, strip_url_query_strings=True)keys = set(page.lean_element_tree_cache.keys())- assert keys == {(False, True, False), (False, False, True), (False, True, True)}+ assert keys == {(False, True, False, False), (False, False, True, False), (False, True, True, False)}# Different combos produce different rendered output.only_img = page.build_lean_elements_tree(compress_image_src=True)@@ -92,7 +93,24 @@ def test_cache_isolation_default_flags_off_is_its_own_slot() -> None:"""No-op call (all flags False) is a valid combo with its own cache key."""page = _make_scraped_page([_node("a", attributes={"href": "/x?utm=1"})])page.build_lean_elements_tree()- assert (False, False, False) in page.lean_element_tree_cache+ assert (False, False, False, False) in page.lean_element_tree_cache+++def test_nonnavigable_href_flag_has_its_own_cache_slot_and_drops_href() -> None:+ """The 4th transform (compress_nonnavigable_href) must participate in the cache key+ (so it can't return a stale 3-flag tree) and must drop a labeled javascript: href."""+ labeled_postback = _node("a", id="AA", attributes={"href": "javascript:__doPostBack('x','')"})+ labeled_postback["text"] = "Detail"+ page = _make_scraped_page([labeled_postback])++ plain = page.build_lean_elements_tree()+ slim = page.build_lean_elements_tree(compress_nonnavigable_href=True)++ assert (False, False, False, False) in page.lean_element_tree_cache+ assert (False, False, False, True) in page.lean_element_tree_cache+ assert "__doPostBack" in plain # untouched without the flag+ assert "__doPostBack" not in slim # dropped with the flag+ assert "href" not in slim# --- JSON vs HTML dispatch -----------------------------------------------diff --git a/tests/unit/test_lean_html.py b/tests/unit/test_lean_html.pyindex a4be6b836..93a707969 100644--- a/tests/unit/test_lean_html.py+++ b/tests/unit/test_lean_html.py@@ -26,6 +26,7 @@ ALL_LEAN_FLAGS = dict(compress_long_href=True,compress_image_src=True,strip_url_query_strings=True,+ compress_nonnavigable_href=True,)@@ -137,6 +138,171 @@ def test_strip_url_query_strings_off_keeps_query() -> None:assert out[0]["attributes"]["href"] == "/x?utm=1"+# --- flag #4: compress_nonnavigable_href ---------------------------------+++def test_nonnavigable_drops_javascript_href() -> None:+ tree = [_node("a", id="AABm", attributes={"href": "javascript:void(0)"}, text="More", children=[])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=True)+ assert "href" not in out[0]["attributes"]+ # tag identity, id, and text survive — element stays clickable-by-id.+ assert out[0]["id"] == "AABm"+ assert out[0]["text"] == "More"+++def test_nonnavigable_drops_labeled_webforms_postback_href() -> None:+ """A labeled __doPostBack anchor (visible text present) drops the opaque token."""+ href = "javascript:__doPostBack('DataListResultats$ctl02$lnkDetail','')"+ tree = [_node("a", id="AABm", attributes={"href": href}, text="+ Details", children=[])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=True)+ assert "href" not in out[0]["attributes"]+++def test_nonnavigable_keeps_textless_postback_href_stripped() -> None:+ """A textless icon link's only naming signal is the postback control name, so+ it survives — but with the noisy `javascript:` wrapper stripped (semantic+ payload preserved, token cost reduced)."""+ href = "javascript:__doPostBack('ctl00$grid$lnkDownloadPDF','')"+ tree = [_node("a", id="AA", attributes={"href": href}, text="", children=[])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=True)+ assert out[0]["attributes"]["href"] == "__doPostBack('ctl00$grid$lnkDownloadPDF','')"+++def test_nonnavigable_keeps_icon_marker_only_postback_href_stripped() -> None:+ """`[icon]` is not a human label, so an icon-only postback link keeps its+ semantic payload (with the `javascript:` wrapper stripped)."""+ href = "javascript:__doPostBack('ctl00$grid$lnkExport','')"+ tree = [_node("a", id="AA", attributes={"href": href}, text="[icon]", children=[])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=True)+ assert out[0]["attributes"]["href"] == "__doPostBack('ctl00$grid$lnkExport','')"+++def test_nonnavigable_keeps_textless_void_wrapped_call_stripped() -> None:+ """A `javascript:void(downloadFn(...))` href on a textless control keeps the+ inner function-name signal — Codex/Lawy regression."""+ tree = [_node("a", id="AA", attributes={"href": "javascript:void(downloadPdf('123'))"}, text="", children=[])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=True)+ assert out[0]["attributes"]["href"] == "downloadPdf('123')"+++def test_nonnavigable_keeps_textless_bare_javascript_expression_stripped() -> None:+ """A bare `javascript:fn(...)` href (no void wrapper) on a textless control+ keeps the function call after the scheme is stripped."""+ tree = [_node("a", id="AA", attributes={"href": "javascript:openModal('confirm')"}, text="", children=[])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=True)+ assert out[0]["attributes"]["href"] == "openModal('confirm')"+++def test_nonnavigable_drops_textless_javascript_no_op_payloads() -> None:+ """`javascript:void(0)`, `javascript:`, `javascript:void(undefined)` etc.+ carry no signal — drop the href entirely even on textless controls."""+ for noop in (+ "javascript:",+ "javascript:void(0)",+ "javascript:void(0);",+ "javascript:void(undefined)",+ "javascript:void(null)",+ "javascript:;",+ ):+ tree = [_node("a", id="AA", attributes={"href": noop}, text="", children=[])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=True)+ assert "href" not in out[0]["attributes"], f"{noop!r} should drop"+++def test_nonnavigable_drops_postback_href_labeled_by_aria() -> None:+ href = "javascript:__doPostBack('ctl00$grid$lnkExport','')"+ tree = [_node("a", id="AA", attributes={"href": href, "aria-label": "Export"}, text="", children=[])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=True)+ assert "href" not in out[0]["attributes"]+++def test_nonnavigable_drops_postback_href_labeled_by_child() -> None:+ """A wrapper anchor with the label in a child span still drops the postback href."""+ href = "javascript:__doPostBack('ctl00$grid$lnkDetail','')"+ tree = [_node("a", id="AA", attributes={"href": href}, children=[_node("span", id="AB", text="Details")])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=True)+ assert "href" not in out[0]["attributes"]+++def test_nonnavigable_keeps_href_when_label_is_below_recursion_depth_cap() -> None:+ """Depth guard: a label nested deeper than _TEXT_SIGNAL_MAX_DEPTH isn't detected,+ so the anchor is treated as textless and the semantic payload is conservatively+ KEPT (no spurious drop, no RecursionError on deep DOMs). The `javascript:`+ wrapper is still stripped on the textless path."""+ from skyvern.utils.lean_html import _TEXT_SIGNAL_MAX_DEPTH++ href = "javascript:__doPostBack('ctl00$grid$lnkDeep','')"+ node: dict = _node("span", id="ZZ", text="Details")+ for _ in range(_TEXT_SIGNAL_MAX_DEPTH + 5):+ node = _node("div", id="WR", children=[node])+ anchor = _node("a", id="AA", attributes={"href": href}, text="", children=[node])+ out = apply_lean_to_tree([anchor], compress_nonnavigable_href=True)+ assert out[0]["attributes"]["href"] == "__doPostBack('ctl00$grid$lnkDeep','')"+++def test_nonnavigable_drops_pure_idiom_even_when_textless() -> None:+ """Pure idioms (#/empty/javascript:;/void(0)) are content-free, so they drop+ regardless of whether the element has a label."""+ for idiom in ("#", "", "javascript:;", "javascript:void(0)", "javascript:void(0);"):+ tree = [_node("a", id="AA", attributes={"href": idiom}, text="", children=[])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=True)+ assert "href" not in out[0]["attributes"], f"{idiom!r} should drop even on a textless element"+++def test_nonnavigable_drops_empty_and_bare_hash() -> None:+ for noop in ("", "#", " "):+ tree = [_node("a", id="AA", attributes={"href": noop}, children=[])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=True)+ assert "href" not in out[0]["attributes"], f"{noop!r} should be dropped"+++def test_nonnavigable_preserves_hash_routes_and_anchors() -> None:+ """SPA hash routes (#/...) and same-page anchors (#name) are real destinations — keep them."""+ for navigable in ("#/checkout", "#/orders/123", "#section", "#" + "x"):+ tree = [_node("a", id="AA", attributes={"href": navigable}, children=[])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=True)+ assert out[0]["attributes"]["href"] == navigable, f"{navigable!r} must be preserved"+++def test_nonnavigable_preserves_real_urls_even_when_long() -> None:+ long_url = "https://www.example.com/very/long/destination/" + "x" * 200+ tree = [_node("a", id="AA", attributes={"href": long_url}, children=[])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=True)+ assert out[0]["attributes"]["href"] == long_url+++def test_nonnavigable_substring_in_navigable_url_is_not_dropped() -> None:+ """RISK-4 regression: the rule is scheme-anchored, not a bare substring match.+ A real http(s) URL whose path merely contains '__doPostBack' must survive."""+ url = "https://docs.example.com/help/__doPostBack-explained"+ tree = [_node("a", id="AA", attributes={"href": url}, children=[])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=True)+ assert out[0]["attributes"]["href"] == url+++def test_nonnavigable_off_keeps_href() -> None:+ tree = [_node("a", id="AA", attributes={"href": "javascript:void(0)"}, children=[])]+ out = apply_lean_to_tree(tree, compress_nonnavigable_href=False)+ assert out[0]["attributes"]["href"] == "javascript:void(0)"+++def test_nonnavigable_is_token_monotonic_never_adds_bytes() -> None:+ """The transform only ever removes the attribute — it must never replace a short+ href with a longer marker (that would inflate tokens, the inverse of the goal)."""+ from skyvern.forge.sdk.core import skyvern_context+ from skyvern.forge.sdk.core.skyvern_context import SkyvernContext+ from skyvern.webeye.scraper.scraped_page import json_to_html++ with skyvern_context.scoped(SkyvernContext(organization_id="o_test", workflow_run_id="wr_test")):+ for noop in ("#", "", "javascript:__doPostBack('x','')"):+ node = _node("a", id="AA", attributes={"href": noop}, text="t", children=[])+ before = json_to_html(copy.deepcopy(node), need_skyvern_attrs=False)+ after = json_to_html(+ apply_lean_to_tree([node], compress_nonnavigable_href=True)[0], need_skyvern_attrs=False+ )+ assert len(after) <= len(before), f"href={noop!r}: rendered HTML grew ({before!r} -> {after!r})"++# --- defaults / no-ops ---------------------------------------------------diff --git a/tests/unit/test_template_input_sanitization.py b/tests/unit/test_template_input_sanitization.pyindex 576e95a35..e883d5c5a 100644--- a/tests/unit/test_template_input_sanitization.py+++ b/tests/unit/test_template_input_sanitization.py@@ -256,6 +256,7 @@ class _FakeBuilderWithLastHtml:compress_long_href: bool = False,compress_image_src: bool = False,strip_url_query_strings: bool = False,+ compress_nonnavigable_href: bool = False,) -> str:self.last_used_element_tree_html = self._htmlreturn self._html@@ -285,6 +286,7 @@ class _FakeBuilderNoneLastHtml:compress_long_href: bool = False,compress_image_src: bool = False,strip_url_query_strings: bool = False,+ compress_nonnavigable_href: bool = False,) -> str:return self._html