Skip to content

Automate browser based workflows with AI

Task V3 observe: surface a rejection banner rendered inside a component (#8259)

pedrohsdb58c53451b8dd
diff --git a/skyvern/forge/taskv3/tools.py b/skyvern/forge/taskv3/tools.py
index eb3ac4a4f..7bb430ce0 100644
--- a/skyvern/forge/taskv3/tools.py
+++ b/skyvern/forge/taskv3/tools.py
@@ -590,6 +590,9 @@ _OBSERVE_JS = (
// counted and disclosed. Every root is kept for the uniqueness probe below.
const allRoots = [];
const els = [];
+ // Roots whose host chain reaches a <form>. `closest` stops at the root it starts in, so a block
+ // inside a component cannot see the form its host sits in; this is that answer, carried down.
+ const inFormRoots = new Set();
{
const seenRoots = new Set();
// Pushes `root`'s own matches onto `els`, and collects the roots nested directly under it into
@@ -610,7 +613,7 @@ _OBSERVE_JS = (
if (sr && sr.nodeType === 11) {
if (seenRoots.has(sr)) continue;
seenRoots.add(sr);
- kids.push({ root: sr, host: el });
+ kids.push({ root: sr, host: el, parent: root });
}
} catch (e) {
// This costs the element's entire root, not the element -- its own matches were pushed by
@@ -634,6 +637,10 @@ _OBSERVE_JS = (
while (stack.length) {
const frame = stack.pop();
allRoots.push(frame.root);
+ // Pre-order, so the parent root's answer is already settled when we get here.
+ try {
+ if (inFormRoots.has(frame.parent) || frame.host.closest('form')) inFormRoots.add(frame.root);
+ } catch (e) { /* one host that cannot answer only costs its own root's ranking */ }
const kids = [];
// 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
@@ -1073,9 +1080,43 @@ _OBSERVE_JS = (
// Only "error" is matched against id: id-named chrome (#alert-count, #cookie-warning) is the
// false-positive family this channel is most exposed to, and "error" is the one id that isn't.
const msgSel = '[class*="error" i],[class*="invalid" i],[class*="alert" i],[class*="warning" i],[id*="error" i]';
- const msgCands = Array.from(document.querySelectorAll(msgSel));
- const inForm = msgCands.filter((el) => el.closest('form'));
- const orderedCands = inForm.concat(msgCands.filter((el) => !el.closest('form')));
+ // A component renders its validation summary in its own shadow root, where a document query
+ // cannot reach it -- the blindness already lifted for controls, on the channel that carries
+ // refusal messages. Walked like the ARIA and heading channels above, so this asks the page no
+ // new question, only the same one of more roots.
+ const formMsgs = [];
+ const otherMsgs = [];
+ for (const root of allRoots) {
+ // nodeType 11 first: Document has no `host`, so `root.host` would hit the HTML named-property
+ // getter and <form name="host"> would supply one.
+ const host = root.nodeType === 11 ? root.host || null : null;
+ // One component that refuses this query costs its own root, not the digest: an uncaught throw
+ // reaches the outer catch and empties every channel. A root that throws for every query is
+ // already disclosed by the marker gather; one that throws only for this selector is not, and
+ // is not defended here.
+ let cands;
+ try { cands = root.querySelectorAll(msgSel); } catch (e) { continue; }
+ for (const el of cands) {
+ // Being inside a form is what separates a validation message from page chrome, and it is
+ // a structural fact rather than a guess about which roots tend to hold content. Ranking
+ // component blocks above light-DOM ones instead would bury a page's own banner under the
+ // cookie-consent and chat widgets that also ship as components.
+ const bucket = (el.closest('form') || inFormRoots.has(root)) ? formMsgs : otherMsgs;
+ // Bounded per bucket at the same 200 the loop below stops at, so a page of components cannot
+ // make the gather itself the expensive part.
+ if (bucket.length < 200) bucket.push({ el: el, host: host });
+ }
+ }
+ const orderedCands = formMsgs.concat(otherMsgs);
+ // A per-field state wrapper (`field--has-error`, `field--no-error`) matches this selector and its
+ // text is just the control's own name, so it spends the channel's budget on what the element list
+ // already carries -- enough of them and the page's real message never fits. Read off the records
+ // already built, so recognising them asks the page nothing.
+ const listedLabels = new Set();
+ for (const r of out) {
+ const lb = (r.label || '').replace(/\s+/g, ' ').trim();
+ if (lb) listedLabels.add(lb);
+ }
// Stricter than visible(): this channel's selector is broad and site chrome is routinely present
// but hidden, whereas an ARIA live region styled invisible is not a pattern worth the extra reads.
const visibleText = (el) => {
@@ -1085,15 +1126,27 @@ _OBSERVE_JS = (
const cs = getComputedStyle(el);
return cs.visibility !== 'hidden' && cs.opacity !== '0';
};
- for (const el of orderedCands) {
+ for (const cand of orderedCands) {
if (textFull || textTotal - blockStart >= 600 || ++messageCandidates > 200) break;
// This selector set is broad, so one poisoned element degrades to "skip it", not to an
// emptied digest (the outer catch is for the narrow ARIA channel).
try {
+ const el = cand.el;
if (!visibleText(el)) continue;
- const t = (el.innerText || '').replace(/\s+/g, ' ').trim();
+ // A component's message block is `<div class="alert"><slot></slot></div>`: the words are
+ // slotted from the host's light DOM, so the block's own innerText is empty and the host
+ // carries them. Same fallback the heading channel below uses, and the field count comes
+ // from whichever node supplied the text.
+ let t = (el.innerText || '').replace(/\s+/g, ' ').trim();
+ let src = el;
+ if (!t && cand.host) {
+ t = (cand.host.innerText || '').replace(/\s+/g, ' ').trim();
+ src = cand.host;
+ }
if (!t) continue;
- if (t.length <= 300 || (t.length <= 900 && el.querySelectorAll('input,select,textarea').length < 2)) pushText(t, blockLimit);
+ // Compared at the width labels are stored at, so a truncated one still matches.
+ if (listedLabels.has(t.slice(0, 140))) continue;
+ if (t.length <= 300 || (t.length <= 900 && src.querySelectorAll('input,select,textarea').length < 2)) pushText(t, blockLimit);
} catch (e) { continue; }
}
// role=heading alongside h1-h3: a component's heading is a custom element, so its tag name
diff --git a/tests/unit/test_taskv3_tools.py b/tests/unit/test_taskv3_tools.py
index 7e6fc9ab9..734fa6ff0 100644
--- a/tests/unit/test_taskv3_tools.py
+++ b/tests/unit/test_taskv3_tools.py
@@ -1422,6 +1422,148 @@ async def test_observe_digest_containment_dedupe_keeps_one_banner_copy() -> None
assert sum("Access revoked" in t for t in texts) == 1
+_COMPONENT_REJECTION_HTML = """<!doctype html><html><head><title>Application</title></head><body>
+ {chrome}
+ <ds-page></ds-page>
+ <script>
+ customElements.define('ds-alert', class extends HTMLElement {{
+ connectedCallback() {{
+ const r = this.attachShadow({{mode: 'open'}});
+ r.innerHTML = '<div class="ds-alert ds-alert--error">We could not process your application. The email address is already registered.</div>';
+ }}
+ }});
+ customElements.define('ds-field', class extends HTMLElement {{
+ connectedCallback() {{
+ const r = this.attachShadow({{mode: 'open'}});
+ const id = this.getAttribute('fid');
+ r.innerHTML = '<div class="ds-field ds-field--no-error">' +
+ '<label for="' + id + '">' + this.getAttribute('lbl') + '</label>' +
+ '<input id="' + id + '" type="text"></div>';
+ }}
+ }});
+ customElements.define('ds-page', class extends HTMLElement {{
+ connectedCallback() {{
+ const r = this.attachShadow({{mode: 'open'}});
+ let f = '';
+ for (let i = 0; i < 40; i++)
+ f += '<ds-field fid="f' + i + '" lbl="Question number ' + i + ' about your background"></ds-field>';
+ r.innerHTML = '<form><h2>Your details</h2>' + f +
+ '<ds-alert></ds-alert><button id="submit">Submit</button></form>';
+ }}
+ }});
+ </script>
+</body></html>"""
+
+
+@_skip_no_browser
+async def test_observe_digest_surfaces_a_rejection_banner_rendered_inside_a_component() -> None:
+ # A design-system app renders its form AND its validation summary in components. The fields get
+ # enumerated but the message channel queried only the document, so a refused submission read as a
+ # quiet page. Both things that compete for this channel's budget are present: light-DOM chrome
+ # that more than fills it, and per-field state wrappers whose class matches the selector while
+ # their text is only the label the element list already carries. The banner has to outrank both.
+ chrome = "".join(
+ f'<div class="site-alert-{i}">Notice {i}: ' + ("filler text about unrelated site chrome " * 6) + "</div>"
+ for i in range(6)
+ )
+ async with _content_page(_COMPONENT_REJECTION_HTML.format(chrome=chrome)) as page:
+ data = await _observe_data(page)
+ texts = data.get("text") or []
+ assert "already registered" in texts[0]
+ assert not any("Question number" in t for t in texts) # labels are the element list's job
+ assert "#submit" in [e["selector"] for e in data["elements"]]
+
+
+@_skip_no_browser
+async def test_component_chrome_does_not_outrank_a_light_dom_rejection_banner() -> None:
+ # Cookie-consent, chat and ad widgets ship as components with alert/warning class names too, so
+ # "lives in a component" cannot mean "is probably the page's message" — ranking on that buries a
+ # light-DOM banner the document-only channel used to surface. Being inside a form is the signal.
+ pad = "we use cookies and similar technologies to personalise content and analyse traffic " * 3
+ html = """<!doctype html><html><body>
+ <cookie-consent></cookie-consent><chat-launcher></chat-launcher><ad-slot></ad-slot>
+ <div class="page-error">We could not process your application. The email is already registered.</div>
+ <form><input id="email" type="email"><button id="submit">Submit</button></form>
+ <script>
+ const mk = (tag, cls, txt) => customElements.define(tag, class extends HTMLElement {
+ connectedCallback() {
+ this.attachShadow({mode: 'open'}).innerHTML = '<div class="' + cls + '">' + txt + '</div>';
+ }
+ });
+ mk('cookie-consent', 'cc-alert', 'Cookie notice. PAD');
+ mk('chat-launcher', 'cl-alert-badge', 'Chat with us. PAD');
+ mk('ad-slot', 'ad-warning', 'Sponsored. PAD');
+ </script>
+ </body></html>""".replace("PAD", pad)
+ async with _content_page(html) as page:
+ data = await _observe_data(page)
+ texts = data.get("text") or []
+ assert "already registered" in texts[0]
+
+
+@_skip_no_browser
+async def test_observe_digest_reads_a_component_banner_whose_text_is_slotted() -> None:
+ # The common design-system spelling is `<ds-alert>message</ds-alert>` over a root of
+ # `<div class="alert"><slot></slot></div>`. The block matches the selector but renders slotted
+ # light-DOM text, so its own innerText is empty and only the host carries the words.
+ html = """<!doctype html><html><body>
+ <ds-alert><span>Your card was declined by the issuer</span></ds-alert>
+ <input id="email" type="email">
+ <script>
+ customElements.define('ds-alert', class extends HTMLElement {
+ connectedCallback() {
+ this.attachShadow({mode: 'open'}).innerHTML = '<div class="alert"><slot></slot></div>';
+ }
+ });
+ </script>
+ </body></html>"""
+ async with _content_page(html) as page:
+ data = await _observe_data(page)
+ assert any("card was declined" in t for t in (data.get("text") or []))
+
+
+@_skip_no_browser
+async def test_one_component_refusing_the_message_query_costs_its_root_not_the_digest() -> None:
+ # Querying every open root for message blocks puts the whole digest behind any root that throws.
+ # A root that refuses every query already empties the digest via the ARIA channel, so the shape
+ # that isolates this loop is the selective one: it must cost its own root and nothing else.
+ html = """<!doctype html><html><body>
+ <div class="page-error">Light DOM notice</div>
+ <ds-hostile></ds-hostile>
+ <ds-good></ds-good>
+ <input id="email" type="email">
+ <script>
+ customElements.define('ds-hostile', class extends HTMLElement {
+ connectedCallback() {
+ const r = this.attachShadow({mode: 'open'});
+ r.innerHTML = '<div class="alert">unreachable</div>';
+ const real = r.querySelectorAll.bind(r);
+ r.querySelectorAll = (s) => {
+ if (String(s).includes('error')) throw new Error('boom');
+ return real(s);
+ };
+ }
+ });
+ customElements.define('ds-good', class extends HTMLElement {
+ connectedCallback() {
+ const r = this.attachShadow({mode: 'open'});
+ r.innerHTML = '<div class="alert">Your card was declined</div>';
+ }
+ });
+ </script>
+ </body></html>"""
+ async with _content_page(html) as page:
+ data = await _observe_data(page)
+ texts = data.get("text") or []
+ assert any("card was declined" in t for t in texts)
+ assert any("Light DOM notice" in t for t in texts)
+ assert not any("unreachable" in t for t in texts)
+
+
@_skip_no_browser
@pytest.mark.asyncio
async def test_observe_survives_hostile_page_with_throwing_text_accessor() -> None: