Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Task V3 observe: scan open component roots for the cross-origin iframes it reports (#8262)
pedrohsdbcbc11aefcb46
diff --git a/skyvern/cli/core/browser_ops.py b/skyvern/cli/core/browser_ops.pyindex 1cc552606..0817ed3a4 100644--- a/skyvern/cli/core/browser_ops.py+++ b/skyvern/cli/core/browser_ops.py@@ -16,7 +16,7 @@ from contextvars import ContextVar, Tokenfrom dataclasses import dataclass, fieldfrom datetime import datetime, timezonefrom pathlib import Path-from typing import Any, Awaitable, Callable, Literal+from typing import Any, Awaitable, Callable, Iterator, Literalimport structlogfrom playwright.async_api import TimeoutError as PlaywrightTimeoutError@@ -1738,6 +1738,14 @@ class CustomSelectMatchError(RuntimeError):self.observed_options = observed_options+def _ticks(deadline: float) -> Iterator[float]:+ # Always one tick: the open click or fill may have eaten the budget on a stalled event loop,+ # and a timeout bounds waiting, not whether the first scan/probe happens at all.+ yield time.monotonic()+ while (now := time.monotonic()) < deadline:+ yield now++def _normalized_option(value: Any) -> str:return " ".join(str(value or "").split()).casefold()@@ -1935,7 +1943,7 @@ async def do_select_option(# Each tick re-scans fresh: the pre-open/pre-fill scan can only see the collapsed# control's display text (not the real options), and matching that display against# the requested value clicks a no-op display node instead of a real option.- while (now := time.monotonic()) < option_deadline:+ for now in _ticks(option_deadline):options = await _scoped_custom_options(page,control,@@ -2033,7 +2041,7 @@ async def do_select_option(before_channels = _custom_select_commit_channels(before)before_channel_values = {_normalized_option(v) for v in (before.get("channelValues") or [])} - {""}before_text = _normalized_option(before.get("text"))- while time.monotonic() < deadline:+ for _ in _ticks(deadline):committed = await control.evaluate(_CUSTOM_SELECT_COMMIT_JS, commit_target, timeout=probe_timeout)if isinstance(committed, dict):committed_stable_values = _stable_values(committed)diff --git a/skyvern/forge/taskv3/tools.py b/skyvern/forge/taskv3/tools.pyindex 32ec8bcc8..020b43741 100644--- a/skyvern/forge/taskv3/tools.py+++ b/skyvern/forge/taskv3/tools.py@@ -732,6 +732,10 @@ _OBSERVE_JS = (// Set wherever we learn that some region of the page cannot be read. Declared here because the// walk below is one of those places and it runs before the marker gather.let sawUnreadableRoot = false;+ // Narrower than sawUnreadableRoot, which a page-wide flag several unrelated failures also set:+ // this counts only roots the walk never discovered, so a channel that iterates allRoots can say+ // whether allRoots was the whole story.+ let undiscoveredRoots = 0;// A web component renders its real input/button inside an open shadow root, which// document.querySelectorAll does not cross. Playwright's selector engine does, so these elements// were always actionable and only perception was blind — a page of them reads as a handful of@@ -770,6 +774,7 @@ _OBSERVE_JS = (// This costs the element's entire root, not the element -- its own matches were pushed by// the query above. The root never reaches allRoots, so the loss is disclosed here instead.sawUnreadableRoot = true;+ undiscoveredRoots++;}}};@@ -796,7 +801,8 @@ _OBSERVE_JS = (// One root that cannot be enumerated costs its own subtree, not the walk. A root that throws// for every query is disclosed by the marker gather below; one that throws only for this// query is not, and is not defended here.- try { enumerate(frame.root, frame.host, kids); } catch (e) { /* this root only */ }+ // Its own matches are lost, and so is every root nested under it -- those never reach allRoots.+ try { enumerate(frame.root, frame.host, kids); } catch (e) { undiscoveredRoots++; }descend(kids);}}@@ -1391,26 +1397,52 @@ _OBSERVE_JS = (// exists. Attributes only, never the frame's document (page.frames-based traversal was considered// and rejected: presence is the contract here, not cross-frame reach). Same visibility rule as// elements, so hidden tracking pixels stay out. Isolated like the digest above.- const iframeInfo = { total: 0, entries: [] };+ // `failed` and `unread` are this channel's own bookkeeping, not a question put to the page: on the+ // section that reports gates, "found none" and "could not look" must not render as one sentence.+ const iframeInfo = { total: 0, entries: [], failed: false, unread: 0 };try {const sig = /captcha|turnstile|challenges\.cloudflare|arkoselabs|funcaptcha|datadome|perimeterx|verify you are human|security challenge/i;- for (const f of document.querySelectorAll('iframe')) {- const r = f.getBoundingClientRect();- if (r.width === 0 || r.height === 0) continue;- // A frame with srcdoc renders the inline (same-origin) document; its src is a dead fallback.- if (f.hasAttribute('srcdoc')) continue;- const src = f.getAttribute('src') || '';- let u;- try { u = new URL(src, location.href); } catch (e) { continue; }- if ((u.protocol !== 'http:' && u.protocol !== 'https:') || u.origin === location.origin) continue;- iframeInfo.total++;- if (iframeInfo.entries.length >= 8) continue;- const ttl = (f.getAttribute('title') || '').replace(/\s+/g, ' ').trim().slice(0, 80);- iframeInfo.entries.push({ host: u.host.slice(0, 80), title: ttl, captcha: sig.test(src + ' ' + ttl) });+ // A design system packages the widget inside its own shadow root, where a document query cannot+ // reach it. Walked like the ARIA, message and heading channels above, so this asks the page no+ // new question, only the same one of more roots.+ for (const root of allRoots) {+ // One root that refuses this query costs its own root, not the channel: an uncaught throw+ // reaches the outer catch and empties every entry, including main-document ones a+ // document-only scan reported fine. The iteration is inside the try because a root can hand+ // back a non-iterable instead of throwing, which is the same attack one line later.+ try {+ for (const f of root.querySelectorAll('iframe')) {+ // Walking more roots means reading more frames, so one poisoned frame inside a component+ // must not cost the roots already scanned.+ try {+ const r = f.getBoundingClientRect();+ if (r.width === 0 || r.height === 0) continue;+ // A frame with srcdoc renders the inline (same-origin) document; its src is a dead fallback.+ if (f.hasAttribute('srcdoc')) continue;+ const src = f.getAttribute('src') || '';+ let u;+ try { u = new URL(src, location.href); } catch (e) { continue; }+ if ((u.protocol !== 'http:' && u.protocol !== 'https:') || u.origin === location.origin) continue;+ const ttl = (f.getAttribute('title') || '').replace(/\s+/g, ' ').trim().slice(0, 80);+ const isCaptcha = sig.test(src + ' ' + ttl);+ // Counted once every throwable read has succeeded: incrementing earlier put a frame in+ // `total` and in `unread` at once, so the two summed past the page's real count.+ iframeInfo.total++;+ if (iframeInfo.entries.length < 8) {+ iframeInfo.entries.push({ host: u.host.slice(0, 80), title: ttl, captcha: isCaptcha });+ } else if (isCaptcha) {+ // Spending all 8 slots on ad embeds and dropping the one frame this channel exists to+ // report defeats the channel, so a gate displaces an embed; the cap and total hold.+ const at = iframeInfo.entries.findIndex((e) => !e.captcha);+ if (at !== -1) iframeInfo.entries[at] = { host: u.host.slice(0, 80), title: ttl, captcha: isCaptcha };+ }+ } catch (e) { iframeInfo.unread++; continue; }+ }+ } catch (e) { iframeInfo.unread++; continue; }}- } catch (e) { iframeInfo.total = 0; iframeInfo.entries.length = 0; }+ } catch (e) { iframeInfo.total = 0; iframeInfo.entries.length = 0; iframeInfo.unread = 0; iframeInfo.failed = true; }- return JSON.stringify({ url: location.href, title: document.title, text: texts, textTruncated: textFull, textDropped: textDropped, iframes: iframeInfo, dropped: dropped, truncated: truncated, truncatedInComponents: truncatedInComponents, unnamedAnonymous: unnamedAnonymous, unnamedDuplicated: unnamedDuplicated, unnamedUnverifiable: unnamedUnverifiable, unnamedUnsafe: unnamedUnsafe, unreadableRoot: sawUnreadableRoot, rootCount: allRoots.length - 1, hiddenListed: hiddenListed, elements: out });+ return JSON.stringify({ url: location.href, title: document.title, text: texts, textTruncated: textFull, textDropped: textDropped, iframes: iframeInfo, dropped: dropped, truncated: truncated, truncatedInComponents: truncatedInComponents, unnamedAnonymous: unnamedAnonymous, unnamedDuplicated: unnamedDuplicated, unnamedUnverifiable: unnamedUnverifiable, unnamedUnsafe: unnamedUnsafe, unreadableRoot: sawUnreadableRoot, undiscoveredRoots: undiscoveredRoots, rootCount: allRoots.length - 1, hiddenListed: hiddenListed, elements: out });}""")@@ -1533,10 +1565,26 @@ def build_browser_tools(lines.append(f"note: {text_dropped} more page message(s) did not fit the text digest")iframe_info = data.get("iframes") or {}iframe_entries = iframe_info.get("entries") or []- # Always emitted, including the zero case. This scan is document-only, so a captcha packaged- # inside a component produces no entry — and printing nothing made that indistinguishable- # from a page with no gate on it, on the one section whose whole purpose is to report gates.- if iframe_entries:+ iframe_unread = iframe_info.get("unread") or 0+ # Every branch states the scope it actually covered: a confident absence is read as "no gate+ # here" on the page most likely to have one.+ #+ # Not a frame count: one unreadable region is a single frame or a whole root, and a root holds+ # any number of frames.+ if iframe_unread:+ iframe_hedge = f"{iframe_unread} unreadable region(s) may hold more"+ elif data.get("undiscoveredRoots"):+ # A root the walk never found holds frames that are missing from `total` without the scan+ # knowing they exist. Keyed off that count and not `unreadableRoot`, which is page-wide and+ # several failures unrelated to the root walk also set.+ iframe_hedge = "part of this page could not be read, so there may be more"+ else:+ iframe_hedge = ""+ if iframe_info.get("failed"):+ # Never "none" and never a count: the scan did not run, so the page's frames are unknown+ # rather than absent.+ lines.append("iframes: the frame scan failed on this page; frame presence is unknown")+ elif iframe_entries:total = iframe_info.get("total", len(iframe_entries))parts = []for f in iframe_entries:@@ -1544,14 +1592,22 @@ def build_browser_tools(title = f" {f['title']!r}" if f.get("title") else ""parts.append(f"{flag}{_digest_token(f.get('host') or '?', 80)}{title}")overflow = f" (+{total - len(iframe_entries)} more)" if total > len(iframe_entries) else ""+ # `total` counts what was readable, so without this the sentence is an absolute claim+ # about a page some of which was never read.lines.append(- f"iframes: {total} cross-origin (contents NOT listed here and NOT reachable by selector; "- "component roots not scanned): " + "; ".join(parts) + overflow+ f"iframes: {total} cross-origin in the page and its open component roots "+ "(contents NOT listed here and NOT reachable by selector): "+ + "; ".join(parts)+ + overflow+ + (f"; {iframe_hedge}" if iframe_hedge else ""))+ elif iframe_hedge:+ lines.append(f"iframes: none found; {iframe_hedge}")elif data.get("rootCount"):- # Only where the unscanned region actually exists. On a page with no components there is- # nothing this line could be hiding, and it would cost a line on every observe of every run.- lines.append("iframes: none in the main document (component roots not scanned)")+ # Only where a component root actually exists. On a page with no components the line+ # says nothing the element list doesn't, and it would cost a line on every observe of+ # every run.+ lines.append("iframes: none in the page or its open component roots")dropped = data.get("dropped") or 0if dropped:# Without this an element list emptied by unreadable elements is indistinguishable fromdiff --git a/tests/unit/test_mcp_browser_tools.py b/tests/unit/test_mcp_browser_tools.pyindex f20950906..4a8d199b5 100644--- a/tests/unit/test_mcp_browser_tools.py+++ b/tests/unit/test_mcp_browser_tools.py@@ -1694,6 +1694,42 @@ async def test_do_select_option_accepts_idempotent_exact_committed_value(assert control.evaluate.await_count == 4+async def test_do_select_option_still_scans_and_probes_once_when_opening_ate_the_budget(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # A stalled event loop between the open click and the first scan (a loaded CI shard) must not+ # turn the budget into "no option looked at": the deadline bounds waiting, not the first tick.+ target = {"tag": "button", "role": "combobox", "haspopup": False, "editable": False}+ page, control = make_select_like_page(target)+ monkeypatch.setattr(+ browser_ops,+ "_get_dom_observe_elements",+ AsyncMock(return_value=[{"selector": "#oregon", "role": "option", "name": "OR"}]),+ )+ committed = {+ "text": "Oregon",+ "value": "OR",+ "dataValues": [],+ "expanded": "false",+ "optionVisible": False,+ "optionSelected": False,+ }+ control.evaluate.side_effect = [+ target,+ [{"selector": "#oregon", "label": "OR", "value": "OR"}],+ committed,+ committed,+ ]++ async def stalled_click(**_: object) -> None:+ await asyncio.sleep(0.02)++ control.click = AsyncMock(side_effect=stalled_click)++ assert await do_select_option(page, "#state", "or", timeout=1) == "OR"++@pytest.mark.asyncioasync def test_do_select_option_rejects_toggle_deselect_of_requested_value(monkeypatch: pytest.MonkeyPatch,diff --git a/tests/unit/test_taskv3_tools.py b/tests/unit/test_taskv3_tools.pyindex fdd279498..a5f209228 100644--- a/tests/unit/test_taskv3_tools.py+++ b/tests/unit/test_taskv3_tools.py@@ -1704,6 +1704,357 @@ async def test_observe_omits_iframe_line_when_no_cross_origin_iframes() -> None:assert "iframes:" not in r.content+@_skip_no_browser+async def test_observe_reports_a_captcha_iframe_packaged_inside_a_component() -> None:+ # A design system ships the challenge widget as a component, so the frame is a child of its+ # shadow root and a document query never matches it. The channel then reported "none" on the+ # one page that most needed a gate reported — and the solve arm can already reach the widget+ # by selector, so the only broken link was telling the model the gate is there. Drives the real+ # observe handler, not the JS alone: the rendered line is what the model actually reads.+ html = """<!doctype html><html><body>+ <form>+ <input id="email" type="email" placeholder="Email" style="display:block;width:200px;height:24px">+ <ds-challenge id="gate" style="display:block;width:300px;height:65px"></ds-challenge>+ <button id="submit" style="display:block;width:80px;height:20px">Submit application</button>+ </form>+ <script>+ document.getElementById('gate').attachShadow({mode: 'open'}).innerHTML =+ '<iframe src="https://challenges.antibot-vendor.test/turnstile/anchor?k=secret123"' ++ ' title="Sign-in widget" width="300" height="65"></iframe>';+ </script>+ </body></html>"""+ async with _content_page(html) as page:+ data = await _observe_data(page)+ entries = (data.get("iframes") or {}).get("entries") or []+ assert len(entries) == 1, data.get("iframes")+ assert entries[0]["host"] == "challenges.antibot-vendor.test"+ assert entries[0]["captcha"] is True # from the src alone: the title carries no signature word+ assert "secret123" not in json.dumps(entries) # hosts only — the contract is unchanged++ r = await _tool(build_browser_tools(_fixed_page_provider(page)), "observe").handler({})+ assert r.status == "ok"+ line = next((ln for ln in r.content.splitlines() if ln.startswith("iframes:")), None)+ # Pinned whole: asserting a substring lets the scope clause silently revert to the old+ # "component roots not scanned", which is the false claim this change exists to retire.+ assert line == (+ "iframes: 1 cross-origin in the page and its open component roots (contents NOT listed "+ "here and NOT reachable by selector): [captcha] challenges.antibot-vendor.test 'Sign-in widget'"+ ), line+++@_skip_no_browser+async def test_observe_counts_a_frame_once_however_deeply_its_component_nests() -> None:+ # Four frames reachable four different ways. The slotted one is the trap: it renders inside the+ # component but belongs to the host's light DOM, so document and the root must not both claim it.+ html = """<!doctype html><html><body>+ <iframe src="https://light.antibot-vendor.test/a" width="60" height="20"></iframe>+ <ds-outer id="outer" style="display:block;width:300px;height:80px"></ds-outer>+ <ds-panel id="panel" style="display:block;width:300px;height:40px">+ <iframe src="https://slotted.antibot-vendor.test/d" width="60" height="20"></iframe>+ </ds-panel>+ <script>+ const outer = document.getElementById('outer').attachShadow({mode: 'open'});+ outer.innerHTML =+ '<iframe src="https://depth1.antibot-vendor.test/b" width="60" height="20"></iframe>' ++ '<ds-inner id="inner" style="display:block;width:300px;height:40px"></ds-inner>';+ outer.getElementById('inner').attachShadow({mode: 'open'}).innerHTML =+ '<iframe src="https://depth2.antibot-vendor.test/c" width="60" height="20"></iframe>';+ document.getElementById('panel').attachShadow({mode: 'open'}).innerHTML = '<slot></slot>';+ </script>+ </body></html>"""+ async with _content_page(html) as page:+ data = await _observe_data(page)+ info = data.get("iframes") or {}+ hosts = sorted(e["host"] for e in info.get("entries") or [])+ assert hosts == [+ "depth1.antibot-vendor.test",+ "depth2.antibot-vendor.test",+ "light.antibot-vendor.test",+ "slotted.antibot-vendor.test",+ ], hosts+ assert info.get("total") == 4, info # not 5: the slotted frame is counted by document only+++@_skip_no_browser+async def test_observe_iframe_cap_holds_when_the_frames_are_spread_across_components() -> None:+ # The cap bounds the payload, so it has to bound what the roots add too — otherwise a page of+ # frame-carrying components regrows exactly the context the cap exists to hold down.+ hosts = "\n".join(+ f'<ds-slot{i} id="s{i}" style="display:block;width:60px;height:20px"></ds-slot{i}>' for i in range(12)+ )+ html = f"""<!doctype html><html><body>+ {hosts}+ <script>+ for (let i = 0; i < 12; i++) {{+ document.getElementById('s' + i).attachShadow({{mode: 'open'}}).innerHTML =+ '<iframe src="https://embed' + i + '.media.test/player" width="60" height="20"></iframe>';+ }}+ </script>+ </body></html>"""+ async with _content_page(html) as page:+ data = await _observe_data(page)+ info = data.get("iframes") or {}+ assert info.get("total") == 12, info # the count stays honest past the cap+ assert len(info.get("entries") or []) == 8, info++ r = await _tool(build_browser_tools(_fixed_page_provider(page)), "observe").handler({})+ assert "+4 more" in r.content, r.content+++@_skip_no_browser+async def test_one_component_refusing_the_iframe_query_costs_only_its_own_root() -> None:+ # Walking more roots adds throw sources to a channel whose outer catch empties EVERY entry, so+ # without per-root containment one hostile component would erase the main-document captcha that+ # the old document-only scan reported fine. That regression is what this pins.+ html = """<!doctype html><html><body>+ <input id="email" type="email" placeholder="Email" style="display:block;width:200px;height:24px">+ <iframe src="https://challenges.antibot-vendor.test/turnstile/anchor"+ title="Security challenge" width="300" height="65"></iframe>+ <ds-hostile id="bad" style="display:block;width:60px;height:20px"></ds-hostile>+ <script>+ const sr = document.getElementById('bad').attachShadow({mode: 'open'});+ sr.innerHTML = '<iframe src="https://hidden.antibot-vendor.test/x" width="60" height="20"></iframe>';+ // Refuses this one selector only: a root that throws for every query is already disclosed by+ // the marker gather, and this is the case that is not.+ const real = sr.querySelectorAll.bind(sr);+ Object.defineProperty(sr, 'querySelectorAll', {+ value: (s) => { if (String(s) === 'iframe') throw new Error('boom'); return real(s); },+ });+ </script>+ </body></html>"""+ async with _content_page(html) as page:+ data = await _observe_data(page)+ info = data.get("iframes") or {}+ hosts = [e["host"] for e in info.get("entries") or []]+ assert hosts == ["challenges.antibot-vendor.test"], info # the healthy frame survives+ assert info.get("total") == 1, info+ assert "#email" in [e["selector"] for e in data["elements"]] # element perception intact+ # The refusing root is NOT laundered into the unreadable-root disclosure: that flag drives a+ # separate caveat and widening it here would misreport every page carrying one such component.+ assert data.get("unreadableRoot") is False, data.get("unreadableRoot")+ # The root it could not read is still disclosed, so the count is not passed off as absolute:+ # this page holds 2 cross-origin frames and the line may not claim 1 is all of them.+ assert info.get("unread") == 1, info+ r = await _tool(build_browser_tools(_fixed_page_provider(page)), "observe").handler({})+ assert "1 unreadable region(s) may hold more" in r.content, r.content+++@_skip_no_browser+async def test_a_root_that_answers_with_a_non_iterable_costs_only_its_own_root() -> None:+ # Guarding the query call alone is not enough: a root can answer with something that is not a+ # list rather than throwing, and the iteration that follows is then what throws. Same attack,+ # one line apart, and it used to take the whole channel down with the main-document captcha.+ html = """<!doctype html><html><body>+ <iframe src="https://challenges.antibot-vendor.test/turnstile/anchor"+ title="Security challenge" width="300" height="65"></iframe>+ <ds-hostile id="bad" style="display:block;width:60px;height:20px"></ds-hostile>+ <script>+ const sr = document.getElementById('bad').attachShadow({mode: 'open'});+ const real = sr.querySelectorAll.bind(sr);+ Object.defineProperty(sr, 'querySelectorAll',+ { value: (s) => String(s) === 'iframe' ? {} : real(s) });+ </script>+ </body></html>"""+ async with _content_page(html) as page:+ data = await _observe_data(page)+ info = data.get("iframes") or {}+ assert info.get("failed") is False, info # the channel survives+ assert [e["host"] for e in info.get("entries") or []] == ["challenges.antibot-vendor.test"], info+ assert info.get("unread") == 1, info+++@_skip_no_browser+async def test_a_poisoned_frame_in_a_component_cannot_erase_a_main_document_captcha() -> None:+ # Walking more roots means reading more frames, so a frame whose own accessors throw is now+ # reachable where it was not before. Without per-frame containment that throw reaches the outer+ # catch and empties the channel — losing a main-document captcha that even the unwalked scan+ # reported fine. Making the gate harder to see is the one outcome this change may not produce.+ html = """<!doctype html><html><body>+ <iframe src="https://challenges.antibot-vendor.test/turnstile/anchor"+ title="Security challenge" width="300" height="65"></iframe>+ <ds-widget id="w" style="display:block;width:60px;height:20px"></ds-widget>+ <script>+ const sr = document.getElementById('w').attachShadow({mode: 'open'});+ sr.innerHTML = '<iframe src="https://x.media.test/a" width="60" height="20"></iframe>';+ sr.querySelector('iframe').getBoundingClientRect = () => { throw new Error('boom'); };+ </script>+ </body></html>"""+ async with _content_page(html) as page:+ data = await _observe_data(page)+ info = data.get("iframes") or {}+ assert [e["host"] for e in info.get("entries") or []] == ["challenges.antibot-vendor.test"], info+ assert info.get("failed") is False, info+ assert info.get("unread") == 1, info+++@_skip_no_browser+async def test_a_frame_that_throws_midway_is_counted_once_not_in_both_tallies() -> None:+ # A frame whose later reads throw must not land in `total` AND in `unread`: a reader adds the+ # two, and the sum would exceed the frames the page actually has. The honest-total contract is+ # the whole reason this section is allowed to state a number at all.+ html = """<!doctype html><html><body>+ <iframe src="https://a.media.test/x" width="60" height="20"></iframe>+ <iframe id="bad" src="https://b.media.test/y" width="60" height="20"></iframe>+ <script>+ const bad = document.getElementById('bad'), real = bad.getAttribute.bind(bad);+ Object.defineProperty(bad, 'getAttribute',+ { value: (n) => { if (n === 'title') throw new Error('x'); return real(n); } });+ </script>+ </body></html>"""+ async with _content_page(html) as page:+ info = (await _observe_data(page)).get("iframes") or {}+ assert info.get("total") == 1, info # only the frame that was read all the way through+ assert info.get("unread") == 1, info+ assert len(info.get("entries") or []) == 1, info+ r = await _tool(build_browser_tools(_fixed_page_provider(page)), "observe").handler({})+ # No "(+N more)": one frame counted, one disclosed as unreadable, nothing double-claimed.+ assert "more)" not in r.content, r.content+++@_skip_no_browser+async def test_an_unreadable_frame_never_renders_as_an_absence() -> None:+ # "Found none" and "could not look" are different facts. Rendering them as one sentence is the+ # defect this section exists to retire, and it is worst here: a page that defeats the scan is+ # exactly the kind that carries a gate. Named for the branch it reaches — the frame is unreadable,+ # which is not the same as the whole scan failing (that branch is covered separately below).+ html = """<!doctype html><html><body>+ <input id="email" type="email" placeholder="Email" style="display:block;width:200px;height:24px">+ <iframe id="bad" src="https://challenges.antibot-vendor.test/anchor" title="Security challenge"+ width="300" height="65"></iframe>+ <ds-any id="c" style="display:block;width:10px;height:10px"></ds-any>+ <script>+ document.getElementById('c').attachShadow({mode: 'open'});+ Object.defineProperty(document.getElementById('bad'), 'getBoundingClientRect',+ { get() { throw new Error('boom'); } });+ </script>+ </body></html>"""+ async with _content_page(html) as page:+ r = await _tool(build_browser_tools(_fixed_page_provider(page)), "observe").handler({})+ line = next((ln for ln in r.content.splitlines() if ln.startswith("iframes:")), None)+ assert line is not None, r.content+ # A component root exists, so the old code took the "none ... component roots" branch and+ # denied a gate that is sitting in the main document.+ assert line == "iframes: none found; 1 unreadable region(s) may hold more", line+++@_skip_no_browser+async def test_a_root_the_walk_never_reached_hedges_the_iframe_count() -> None:+ # A host whose shadowRoot getter throws never enters the walk, so the frames inside it are absent+ # from the count without the scan ever learning they existed. Without the hedge the line states a+ # complete-sounding total for a page it did not finish reading — the same unearned confidence in+ # a new shape, and the count is the one number a model would use to decide there is no gate.+ html = """<!doctype html><html><body>+ <iframe src="https://ads.partner-a.test/slot" title="Sponsored" width="60" height="20"></iframe>+ <ds-sealed id="s" style="display:block;width:60px;height:20px"></ds-sealed>+ <script>+ const host = document.getElementById('s');+ host.attachShadow({mode: 'open'}).innerHTML =+ '<iframe src="https://challenges.antibot-vendor.test/turnstile/anchor" width="60" height="20"></iframe>';+ Object.defineProperty(host, 'shadowRoot', { get() { throw new Error('sealed'); } });+ </script>+ </body></html>"""+ async with _content_page(html) as page:+ data = await _observe_data(page)+ assert data.get("unreadableRoot") is True, data.get("unreadableRoot")+ assert (data.get("iframes") or {}).get("total") == 1, data.get("iframes") # the gate is missing+ r = await _tool(build_browser_tools(_fixed_page_provider(page)), "observe").handler({})+ line = next((ln for ln in r.content.splitlines() if ln.startswith("iframes:")), None)+ assert line is not None, r.content+ assert "part of this page could not be read, so there may be more" in line, line+++@_skip_no_browser+async def test_an_unrelated_root_failure_does_not_hedge_the_iframe_count() -> None:+ # `unreadableRoot` is page-wide and several failures with nothing to do with the frame walk also+ # set it — a marker-gather refusal, a closest('form') throw, a uniqueness probe. Hedging on it+ # would attach "there may be more" to a count that was in fact complete, which is the same+ # mismatch between claim and scan as the defect being fixed, only in the cautious direction.+ html = """<!doctype html><html><body>+ <iframe src="https://a.media.test/x" width="60" height="20"></iframe>+ <ds-quirk id="q" style="display:block;width:60px;height:20px"></ds-quirk>+ <script>+ const sr = document.getElementById('q').attachShadow({mode: 'open'});+ sr.innerHTML = '<button style="display:block;width:40px;height:20px">Go</button>';+ const real = sr.querySelectorAll.bind(sr);+ Object.defineProperty(sr, 'querySelectorAll',+ { value: (s) => { if (String(s).indexOf('data-tv3') !== -1) throw new Error('nope'); return real(s); } });+ </script>+ </body></html>"""+ async with _content_page(html) as page:+ data = await _observe_data(page)+ # The root was reached and scanned for frames; only an unrelated query on it failed.+ assert data.get("unreadableRoot") is True, data+ assert not data.get("undiscoveredRoots"), data.get("undiscoveredRoots")+ r = await _tool(build_browser_tools(_fixed_page_provider(page)), "observe").handler({})+ line = next((ln for ln in r.content.splitlines() if ln.startswith("iframes:")), None)+ assert line is not None, r.content+ assert "may hold more" not in line and "could not be read" not in line, line+++async def test_a_failed_iframe_scan_renders_as_unknown_not_as_none() -> None:+ # The outer catch is a backstop: it fires only if something throws outside both inner guards, so+ # no page fixture reaches it now that the root walk and the per-frame reads are both contained.+ # Pinned at the renderer instead of pretending otherwise — the contract that matters is that a+ # scan which did not run is never rendered as a page without gates.+ class _FailedScanPage(_FakePage):+ async def evaluate(self, _js: str) -> str:+ return json.dumps(+ {+ "url": self.url,+ "title": "Apply",+ "text": [],+ "iframes": {"total": 0, "entries": [], "failed": True, "unread": 0},+ "rootCount": 3,+ "elements": [],+ }+ )++ tools = build_browser_tools(_fixed_page_provider(_FailedScanPage()))+ r = await _tool(tools, "observe").handler({})+ line = next((ln for ln in r.content.splitlines() if ln.startswith("iframes:")), None)+ assert line == "iframes: the frame scan failed on this page; frame presence is unknown", line+++@_skip_no_browser+async def test_a_captcha_is_not_displaced_from_the_entry_cap_by_ad_embeds() -> None:+ # The cap bounds the payload, but a page whose 8 slots are all ad embeds must not report the one+ # frame the channel exists to report as an anonymous "+1 more". Walking component roots enlarges+ # the population competing for those slots, so the cap has to prefer the gate.+ embeds = "\n".join(+ f'<iframe src="https://embed{i}.media.test/player" width="60" height="20"></iframe>' for i in range(8)+ )+ html = f"""<!doctype html><html><body>+ {embeds}+ <ds-challenge id="gate" style="display:block;width:300px;height:65px"></ds-challenge>+ <script>+ document.getElementById('gate').attachShadow({{mode: 'open'}}).innerHTML =+ '<iframe src="https://challenges.antibot-vendor.test/turnstile/anchor"' ++ ' title="Security challenge" width="300" height="65"></iframe>';+ </script>+ </body></html>"""+ async with _content_page(html) as page:+ data = await _observe_data(page)+ info = data.get("iframes") or {}+ assert info.get("total") == 9, info # the count stays honest+ assert len(info.get("entries") or []) == 8, info # the cap still holds+ assert [e for e in info["entries"] if e["captcha"]], info # and the gate is inside it+ r = await _tool(build_browser_tools(_fixed_page_provider(page)), "observe").handler({})+ assert "[captcha] challenges.antibot-vendor.test" in r.content, r.content+ assert "+1 more" in r.content, r.content++@_skip_no_browser@pytest.mark.asyncioasync def test_observe_digest_superset_replaces_terse_contained_entry() -> None:@@ -2966,10 +3317,12 @@ async def test_click_on_skinned_checkbox_routes_via_label() -> None:@_skip_no_browser@pytest.mark.asyncioasync def test_the_iframe_line_is_emitted_even_when_the_scan_found_nothing() -> None:- # The scan is document-only, so a captcha packaged inside a component yields no entry. Printing- # nothing made that indistinguishable from a page carrying no gate at all, on the one section- # whose purpose is to report gates. Emitted only where a root exists to hide one: on a page with- # no components the line would be vacuous and would cost every observe of every run.+ # Printing nothing made "no gate" indistinguishable from "could not look", on the one section+ # whose purpose is to report gates. Now that the open roots ARE scanned, the line states the+ # scope it actually covered. "open" is load-bearing: a closed root is not scanned here — not+ # because nothing could reach it (the frame list can, and the v1 scraper uses that) but because+ # this channel's contract is a DOM query, and an absence claim wider than the scan is the thing+ # that reads to a model as "no gate here".async with _live_page("""<button id="go" style="display:block;width:80px;height:20px">Go</button><div id="wrap" style="display:block;width:80px;height:20px"></div>@@ -2981,7 +3334,7 @@ async def test_the_iframe_line_is_emitted_even_when_the_scan_found_nothing() ->r = await _tool(tools, "observe").handler({})line = next((ln for ln in r.content.splitlines() if ln.startswith("iframes:")), None)assert line is not None, r.content- assert "component roots not scanned" in line, line+ assert line == "iframes: none in the page or its open component roots", lineasync with _live_page('<button id="go" style="display:block;width:80px;height:20px">Go</button>') as page:tools = build_browser_tools(_fixed_page_provider(page))