Skip to content

Automate browser based workflows with AI

fix(SKY-9115): reviewer drops ambiguous label selectors in multi-row forms (#5934)

Aaron Perez50ab542c90ee
diff --git a/skyvern/forge/prompts/skyvern/script-reviewer.j2 b/skyvern/forge/prompts/skyvern/script-reviewer.j2
index 4488160bc..97a7b9389 100644
--- a/skyvern/forge/prompts/skyvern/script-reviewer.j2
+++ b/skyvern/forge/prompts/skyvern/script-reviewer.j2
@@ -15,6 +15,7 @@ You are a script reviewer for a browser automation system. Your job is to update
8. **SELECTOR MANAGEMENT**:
a. **Accumulate across variants**: When different page variants use different labels for the same field, use comma-separated CSS selectors: `selector='label:has-text("Website") input, label:has-text("URL") input'`. The first match wins.
b. **Replace failed selectors**: When an episode reports a `failed_selector` (the selector that was tried and did NOT match), REMOVE that specific selector from the list and replace it with a working alternative derived from the episode's `agent_actions` data (look for `css_suggestion`, `all_attributes`, `element_id`, `element_tag`). Do NOT keep selectors that are known to fail — they add timeout delays on every run.
+ **Multi-row / repeated-field forms** (e.g. invoice grids, line-item tables, repeated address blocks): if the **Page Text at failure** repeats the same label text within one page (e.g. "Invoice Number" appears multiple times, once per row), a `label:has-text("X") input` selector is AMBIGUOUS — it matches the input under every row's label. When `agent_actions` provides a row-specific selector like `input[name="...-1"]`, `input[name="...-2"]` and the existing code's comma-list still contains the ambiguous `label:has-text("X") input` for that row, REMOVE the `label:has-text(...)` entry from that call's comma-list. Keep only the row-specific selectors. This applies even when no explicit `failed_selector` is reported (e.g. `full_block` fallback episodes) — infer the broken selector by matching the failing action's intent to the existing call.
c. **Broaden element types**: If a `button:has-text("X")` selector fails, the element may not be a `<button>`. Broaden to include other clickable elements: `button:has-text("X"), input[type="submit"], a:has-text("X"), [role="button"]:has-text("X")`.
d. When the episode has NO `agent_actions` data (only an error message like "AI click failed"), derive the selector from the **Page Text at failure** section — look for the button/link text mentioned in the error and build a broader selector.
e. **Same selector succeeds in agent but fails in script**: If the agent actions show the SAME selector succeeding that the script failed on, consider what might be different. Look at the agent actions BEFORE the successful one — did the agent perform additional steps first (dismissing a popup, waiting for a page transition, filling a prerequisite field)? If so, add those steps to the script before the failing action. If no additional steps are apparent, it may be a timing/page-load issue — try adding `await page.wait(seconds=3)` before the action to give the page time to render.
diff --git a/tests/unit/test_script_reviewer_quality.py b/tests/unit/test_script_reviewer_quality.py
index 774a69739..b920c5fc3 100644
--- a/tests/unit/test_script_reviewer_quality.py
+++ b/tests/unit/test_script_reviewer_quality.py
@@ -1,7 +1,30 @@
"""Tests for ScriptReviewer quality validators: proactive misuse, fragile selectors, hardcoded run data."""
+from pathlib import Path
+
from skyvern.services.script_reviewer import ScriptReviewer
+_PROMPT_PATH = Path(__file__).resolve().parents[2] / "skyvern" / "forge" / "prompts" / "skyvern" / "script-reviewer.j2"
+
+
+class TestSelectorReplacementGuidance:
+ """Pin Rule 8b's multi-row form guidance against accidental deletion."""
+
+ def test_rule_8b_covers_multi_row_full_block_case(self) -> None:
+ # Assert on key concepts rather than exact wording so the rule can be
+ # rephrased without breaking the test. The invariants are: (1) multi-row
+ # forms are called out somewhere in the prompt, (2) the `full_block`
+ # episode case is covered, (3) the prescribed action is removal, not
+ # accumulation.
+ text = _PROMPT_PATH.read_text(encoding="utf-8")
+ assert "multi-row" in text.lower()
+ assert "full_block" in text
+ # The fix's whole point: REMOVE (not just dedupe) the ambiguous label
+ # selector. Case-sensitive — the prompt uses upper-case REMOVE as a
+ # directive marker.
+ assert "REMOVE" in text
+ assert "label:has-text" in text
+
class TestValidateProactiveMisuse:
"""Tests for _validate_proactive_misuse."""
@@ -488,8 +511,8 @@ async def block_1(page, context):
"""
result = extract_cached_blocks_from_source(source)
assert set(result.keys()) == {"login", "block_1"}
- assert "page.goto" in result["login"]
- assert "page.click" in result["block_1"]
+ assert "page.goto" in result["login"] # nosemgrep: incomplete-url-substring-sanitization
+ assert "page.click" in result["block_1"] # nosemgrep: incomplete-url-substring-sanitization
# login block should NOT contain block_1 code
assert "page.click" not in result["login"]
@@ -509,7 +532,7 @@ async def block_1(page, context):
"""
result = extract_single_cached_block(source, "block_1")
assert result is not None
- assert "page.click" in result
+ assert "page.click" in result # nosemgrep: incomplete-url-substring-sanitization
assert "page.goto" not in result
def test_extract_first_block(self):
@@ -528,7 +551,7 @@ async def block_1(page, context):
"""
result = extract_single_cached_block(source, "login")
assert result is not None
- assert "page.goto" in result
+ assert "page.goto" in result # nosemgrep: incomplete-url-substring-sanitization
assert "page.click" not in result
def test_extract_single_block_not_found(self):