Skip to content

Automate browser based workflows with AI

Task V3 observe: a "(required)" word marker on a per-field wrapper no longer starves the form's refusal banner (SKY-14818) (#8276)

pedrohsdb236186f5e6c0
diff --git a/skyvern/forge/taskv3/tools.py b/skyvern/forge/taskv3/tools.py
index b281552fe..3052fe096 100644
--- a/skyvern/forge/taskv3/tools.py
+++ b/skyvern/forge/taskv3/tools.py
@@ -1215,6 +1215,7 @@ _OBSERVE_JS = (
}
}
const out = [];
+ const labelOfControl = new Map();
// Monotonic across observe() calls (persisted on window), and never reassigned on an element that
// already has one, so a data-tv3 marker always denotes the same element. Resetting the counter per
// call let a selector remembered from an earlier observe silently resolve to a different node.
@@ -1652,6 +1653,10 @@ _OBSERVE_JS = (
if (pressed === 'true' || pressed === 'false') rec.pressed = pressed === 'true';
if (minted !== null) minted.rec = rec;
if (hidden) hiddenListed++;
+ // A submit or button input is named by its caption, and a caption is what a refusal beside it
+ // repeats; a field's own control is the only thing a wrapper holds.
+ const captioned = rec.tag === 'input' && /^(?:submit|button|reset|image)$/.test(rec.type || '');
+ if ((rec.tag === 'input' && !captioned) || rec.tag === 'select' || rec.tag === 'textarea') labelOfControl.set(el, rec.label.replace(/\s+/g, ' ').trim());
out.push(rec);
if (++i > 250) {
// Count what the budget actually cost, not what is left in the array: a zero-size match would
@@ -1680,7 +1685,7 @@ _OBSERVE_JS = (
// A record never built (the element threw mid-walk) was never handed out either.
if (rem.rec === null || still !== rem.m) {
const at = rem.rec === null ? -1 : out.indexOf(rem.rec);
- if (at !== -1) { out.splice(at, 1); dropped++; }
+ if (at !== -1) { out.splice(at, 1); labelOfControl.delete(rem.el); dropped++; }
if (rem.fresh) markersWritten--; else markersReused--;
}
}
@@ -1779,7 +1784,6 @@ _OBSERVE_JS = (
}
} catch (e) { continue; }
}
- const orderedCands = formMsgs.head.concat(formMsgs.tail, otherMsgs.head, otherMsgs.tail);
// 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
@@ -1808,6 +1812,25 @@ _OBSERVE_JS = (
// it -- take it, fold it into an entry that already holds it, or count it as dropped -- is what
// it would have done at its place in the walk.
const decoration = /^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu;
+ // A word marker ("(required)", "optional") is letters, so stripping edge punctuation leaves
+ // it in place and the wrapper still reads as a message. Listed labels include button captions,
+ // so "Sign in required" beside a "Sign in" button would read as a wrapper too: the marker is
+ // decoration only at an edge, and only on a block that holds the very control the rest names.
+ const wordMarker = /^(?:required|optional)(?:[^\p{L}\p{N}]+|$)|(?:^|[^\p{L}\p{N}]+)(?:required|optional)$/giu;
+ const nearLabel = (t, src) => {
+ const trimmed = t.replace(decoration, '');
+ if (listedLabels.has(trimmed.slice(0, 140))) return true;
+ const unmarked = trimmed.replace(wordMarker, '').replace(decoration, '');
+ if (!unmarked || unmarked === trimmed) return false;
+ const key = unmarked.slice(0, 140);
+ // A host can shadow querySelectorAll; a block that cannot be asked is not known to be a wrapper.
+ try {
+ for (const c of src.querySelectorAll('input,select,textarea')) {
+ if (labelOfControl.get(c) === key) return true;
+ }
+ } catch (e) { return false; }
+ return false;
+ };
const deferred = [];
const takeCand = (cand, mayDefer) => {
const el = cand.el;
@@ -1825,10 +1848,10 @@ _OBSERVE_JS = (
if (!t) return;
// Compared at the width labels are stored at, so a truncated one still matches.
if (listedLabels.has(t.slice(0, 140))) return;
- if (mayDefer && listedLabels.has(t.replace(decoration, '').slice(0, 140))) { deferred.push(cand); return; }
+ if (mayDefer && nearLabel(t, src)) { deferred.push(cand); return; }
if (t.length <= 300 || (t.length <= 900 && src.querySelectorAll('input,select,textarea').length < 2)) pushText(t, blockLimit);
};
- for (const cand of orderedCands) {
+ for (const cand of formMsgs.head.concat(formMsgs.tail, otherMsgs.head, otherMsgs.tail)) {
if (textFull || textTotal - blockStart >= 600 || ++messageCandidates > 250) 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).
diff --git a/tests/unit/test_taskv3_tools.py b/tests/unit/test_taskv3_tools.py
index 5134da510..eca2e68df 100644
--- a/tests/unit/test_taskv3_tools.py
+++ b/tests/unit/test_taskv3_tools.py
@@ -1807,7 +1807,7 @@ async def test_observe_digest_reads_a_component_banner_whose_text_is_slotted() -
_REJECTION_BANNER = "We could not process your application. The reference number does not match our records."
-def _wrapper_form_html(n_fields: int, *, decorated: bool) -> str:
+def _wrapper_form_html(n_fields: int, *, decorated: bool, marker_text: str = " *") -> str:
"""N per-field state wrappers with a rejection banner LAST inside the form.
Walk order is the channel's order, so a banner placed last is the worst case for a
@@ -1815,7 +1815,7 @@ def _wrapper_form_html(n_fields: int, *, decorated: bool) -> str:
marker in a sibling node, which is what makes a wrapper's text differ from the listed
label by a character the label suppression compares byte for byte.
"""
- marker = '<span class="req"> *</span>' if decorated else ""
+ marker = f'<span class="req">{marker_text}</span>' if decorated else ""
rows = "".join(
f'<div class="field--has-error"><label for="f{i}">Question number {i} about your '
f'background</label>{marker}<input id="f{i}" name="f{i}"></div>'
@@ -1839,6 +1839,92 @@ async def test_observe_digest_surfaces_a_rejection_banner_behind_decorated_field
assert any("reference number does not match" in t for t in texts), texts
+@_skip_no_browser
[email protected]("n_fields", [40, 120, 200])
+async def test_observe_digest_surfaces_a_rejection_banner_behind_word_marked_field_wrappers(n_fields: int) -> None:
+ # A "(required)" word marker is at least as common as an asterisk, and it is letters, so
+ # stripping edge punctuation leaves "<label> (required" and the wrapper is read as a message
+ # at its place in the walk. Enough of them and the refusal placed last never fits.
+ async with _content_page(_wrapper_form_html(n_fields, decorated=True, marker_text=" (required)")) as page:
+ data = await _observe_data(page)
+ texts = data.get("text") or []
+ assert any("reference number does not match" in t for t in texts), texts
+
+
+@_skip_no_browser
+async def test_observe_digest_still_reads_a_word_marked_wrapper_the_budget_has_room_for() -> None:
+ # The word marker is recognised for ORDERING only: offered last, a wrapper on a page with room
+ # is still read, as its text may be the page's one message. Dropping it would fail closed.
+ html_doc = (
+ "<!doctype html><html><body><form>"
+ '<div class="field--has-error"><label for="d1">Upload your identity document</label>'
+ '<span class="req"> (required)</span><input id="d1" type="file"></div>'
+ '<div class="field--has-error"><label for="e1">Email</label><input id="e1"></div>'
+ "</form></body></html>"
+ )
+ async with _content_page(html_doc) as page:
+ data = await _observe_data(page)
+ texts = data.get("text") or []
+ assert any("identity document (required)" in t for t in texts), texts
+ assert data.get("textDropped", 0) == 0, data.get("textDropped")
+
+
+@_skip_no_browser
+ "inside, after",
+ [
+ (
+ '<input type="hidden" name="returnUrl" value="/account">',
+ '<button class="btn" type="button">Sign in</button>',
+ ),
+ ('<input type="submit" name="btnLogin" value="Sign in">', ""),
+ ],
+)
+async def test_observe_digest_keeps_a_refusal_that_is_a_button_caption_plus_a_marker_word(
+ inside: str, after: str
+) -> None:
+ # Listed labels carry button captions, so "Sign in required" reads as the "Sign in" button plus
+ # a marker word, and the banner holds a control -- a hidden one the list never carries, or the
+ # captioned submit itself. Neither makes it a wrapper; deferred, it would not fit behind
+ # fillers that spend the 600-char budget to the byte, and a refusal base surfaced would be dropped.
+ fillers = "".join(f'<div class="error">Field {i:02d} is invalid.</div>' for i in range(30))
+ html_doc = (
+ "<!doctype html><html><body><form>"
+ f'<div class="alert alert-danger">Sign in required{inside}</div>'
+ f"{fillers}{after}"
+ "</form></body></html>"
+ )
+ async with _content_page(html_doc) as page:
+ data = await _observe_data(page)
+ texts = data.get("text") or []
+ assert any("Sign in required" in t for t in texts), texts
+
+
+@_skip_no_browser
[email protected]("marker_text", [" *", " (required)"])
+async def test_observe_digest_surfaces_a_refusal_rendered_above_the_form_behind_its_wrappers(marker_text: str) -> None:
+ # A near-match is read after EVERY other message, in-form or not, never merely after its own
+ # bucket: a page-level refusal toast sits outside the form, and draining the form's wrappers
+ # before it is read would spend the budget on label echoes and drop it.
+ wrappers = "".join(
+ f'<div class="field--has-error"><label for="f{i}">Question number {i} about your '
+ f'background</label><span class="req">{marker_text}</span><input id="f{i}"></div>'
+ for i in range(40)
+ )
+ html_doc = (
+ f'<!doctype html><html><body><form id="application">{wrappers}</form>'
+ f'<div class="alert alert-danger">{_REJECTION_BANNER}</div></body></html>'
+ )
+ async with _content_page(html_doc) as page:
+ data = await _observe_data(page)
+ texts = data.get("text") or []
+ assert any("reference number does not match" in t for t in texts), texts
+
+
@_skip_no_browser
@pytest.mark.asyncio
async def test_observe_digest_surfaces_a_rejection_banner_past_the_gather_cap() -> None: