Skip to content

Automate browser based workflows with AI

Task V3: refuse a vetoed finish and retry, failing on the 5th veto as v1 does (SKY-15649) (#8860)

pedrohsdb7e423f7676ae
diff --git a/skyvern/config.py b/skyvern/config.py
index fde9367e0..6b230ed06 100644
--- a/skyvern/config.py
+++ b/skyvern/config.py
@@ -553,6 +553,9 @@ class Settings(BaseSettings):
# Kill switch for the tier-1 semantic commit read (SKY-15322): decisive-accept-only ARIA/value
# probe consulted before the shape heuristics, which remain the fallback either way.
TASK_V3_SEMANTIC_COMMIT_VERIFY: bool = True
+ # Kill switch for committing a list row that names the value without being it exactly (its main label, a
+ # singular/plural, a unique word prefix on a click-opened list) (SKY-18017). Off: typeaheads commit exact rows only.
+ TASK_V3_CHOICE_NONEXACT_COMMIT: bool = True
# Press a sub-pixel date segment through the layer painted over it, and route month/year and
# year-only segment groups to the segment path (SKY-17013). Force-on term only: runs are randomized per
# run by the flag of the same name, read through run_arm_enabled(DATE_SEGMENT_AIM_FLAG, ...).
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.py
index 0cf8f184a..41d79f804 100644
--- a/skyvern/forge/agent.py
+++ b/skyvern/forge/agent.py
@@ -2995,6 +2995,16 @@ class ForgeAgent:
peek_page=_fingerprint_page,
prompt_name=BLOCK_COMPLETION_CHECK_PROMPT_NAME,
)
+
+ async def _completion_gate() -> bool:
+ return await app.AGENT_FUNCTION.gate_step_completion(
+ task=task,
+ step=step,
+ task_block=task_block,
+ page=await browser_state.get_working_page(),
+ browser_state=browser_state,
+ )
+
outcome = await run_task_v3_agent_loop(
page_provider=_page_provider,
resolve_typed_text=resolve_typed_text,
@@ -3057,6 +3067,7 @@ class ForgeAgent:
),
completion_probe=completion_probe,
completion_blocker=completion_blocker,
+ completion_gate=_completion_gate,
staged_downloads=staged_downloads,
deadline_seconds=loop_deadline_seconds,
verification_blocker=verification_state.block_finish,
@@ -3096,13 +3107,12 @@ class ForgeAgent:
)
completion_rejection: str | None = None
if outcome.status == "completed":
- # Same deployment gate the step engine applies before accepting a completion verdict
- # (e.g. a submit block that must show a deterministic confirmation). The loop has already
- # returned, so a veto fails safe instead of falsely completing.
+ # The deployment gate for a completion the finish tool could not put to it (no headroom left to
+ # act on a refusal, a re-ask conversion, or a gate error). The loop has returned, so a veto fails.
gate_page = None
try:
gate_page = await browser_state.get_working_page()
- if not await app.AGENT_FUNCTION.gate_step_completion(
+ if not outcome.gate_passed and not await app.AGENT_FUNCTION.gate_step_completion(
task=task,
step=step,
task_block=task_block,
@@ -3112,12 +3122,20 @@ class ForgeAgent:
completion_rejection = "the deployment completion gate rejected it"
except CompletionGateTerminationError as termination:
outcome = replace(outcome, status="terminated", reason=termination.reason)
+ except StepTerminationError as exhausted:
+ if outcome.converted_from is not None:
+ # A vetoed re-ask conversion restores the model's own verdict below, whatever the veto count.
+ completion_rejection = exhausted.message or "the deployment completion gate rejected it"
+ else:
+ outcome = replace(outcome, status="failed", reason=exhausted.message or "")
except Exception:
LOG.warning(
"task_v3 completion gate errored; accepting completion", task_id=task.task_id, exc_info=True
)
if outcome.status == "terminated":
LOG.info("task_v3 completion terminated by completion gate", task_id=task.task_id)
+ elif outcome.status == "failed":
+ LOG.info("task_v3 completion failed by completion gate", task_id=task.task_id)
elif completion_rejection is not None:
LOG.info("task_v3 completion vetoed by completion gate", task_id=task.task_id)
# A page-bound goal cannot have been met on a tab with no document, and reporting it
diff --git a/skyvern/forge/taskv3/engine.py b/skyvern/forge/taskv3/engine.py
index 6058f68fa..529bf9ba1 100644
--- a/skyvern/forge/taskv3/engine.py
+++ b/skyvern/forge/taskv3/engine.py
@@ -60,6 +60,7 @@ from skyvern.forge.taskv3.loop import (
PERCEPTION_RETAIN_CHARS_HIGH,
ActivityRecency,
CompletionBlocker,
+ CompletionGate,
CompletionProbe,
LoopOutcome,
RoundAction,
@@ -277,6 +278,7 @@ async def run_task_v3_agent_loop(
pending_marker: Callable[[str], Awaitable[str | None]] | None = None,
completion_probe: CompletionProbe | None = None,
completion_blocker: CompletionBlocker | None = None,
+ completion_gate: CompletionGate | None = None,
staged_downloads: set[str] | None = None,
verification_blocker: VerificationBlocker | None = None,
initial_navigation_status: int | None = None,
@@ -553,6 +555,7 @@ async def run_task_v3_agent_loop(
verification_blocker=verification_blocker,
unlisted_reask=_unlisted_reask if reask_on else None,
document_identity=None if page_free else document_identity,
+ completion_gate=None if page_free else completion_gate,
)
tools = browser_tools + (extra_tools or []) + [finish_tool]
# The COMPLETE dispatch list, not just the browser tools: auth / captcha / code tools and finish
diff --git a/skyvern/forge/taskv3/field_commit.py b/skyvern/forge/taskv3/field_commit.py
index 18668c938..77860eac8 100644
--- a/skyvern/forge/taskv3/field_commit.py
+++ b/skyvern/forge/taskv3/field_commit.py
@@ -3,12 +3,16 @@ field back itself and hands the reading here, so no page objects."""
from __future__ import annotations
+import dataclasses
import decimal
+import difflib
import re
import unicodedata
+from collections.abc import Mapping, Sequence
from enum import Enum
-from typing import Any
+from typing import Any, Literal, TypeVar
+from skyvern.core.script_generations.fuzzy_matcher import match_option_exact_or_stem_with_tier, normalize_option_label
from skyvern.forge.taskv3.loop import ToolResult
# The observable-state vocabulary a readback compares — the same fields observe reports per element.
@@ -199,3 +203,306 @@ def entry_outcome(selector: str, match: EntryMatch | None, *, shown: str | None,
ok_class="held_differs",
entry_unconfirmed=True,
)
+
+
+_Row = TypeVar("_Row", bound=Mapping[str, Any])
+
+
+def _exact_tier_key(text: str) -> str:
+ """The exact tier's own equality key — `_canon_label` plus the shared matcher's case/apostrophe fold —
+ for pre-filters asking "which rows did that tier see as this value". A plain `_canon_label` filter
+ folds less than the tier does and would disagree with it, so pre-filters must use this key instead.
+ """
+ return normalize_option_label(_canon_label(text))
+
+
+def _prefix_tokens(s: str) -> list[str]:
+ # Fold commas and apostrophes so a short value token-prefix-matches a punctuated label ("Yes" → "Yes, I
+ # consent"). A slash is left intact so a combined "Yes/No" option is not prefix-matched by "Yes".
+ return re.sub(r"[,'’]", " ", s).lower().split()
+
+
+def _is_forward_prefix(want: list[str], label: list[str]) -> bool:
+ return bool(want) and len(want) < len(label) and label[: len(want)] == want
+
+
+def _row_identity(o: Mapping[str, Any]) -> str:
+ """The row's identity as `_MENU_OPTION_TEXTS_JS`'s `rowIdentity` derived it. A row with no `identity` key
+ is a note entry, whose `text` is already that identity; an empty one (a row nothing could read) is absent."""
+ identity = o.get("identity")
+ return identity if isinstance(identity, str) and identity else str(o.get("text") or "")
+
+
+def _lone_duplicate_candidate(rows: Sequence[Mapping[str, Any]]) -> int | None:
+ """Whether ≥2 matched rows are the SAME candidate rendered more than once: canonical row IDENTITY
+ agreement across every row is the load-bearing check, and a present aria-label, `val`, or other declared
+ value is a VETO on top of it — one disagreeing across otherwise-identical rows marks them distinct, an
+ absent one never does. Returns the FIRST row's `n` when the whole set collapses to one candidate, else
+ None so the caller keeps refusing.
+ """
+ if len(rows) < 2:
+ return None
+ if len({_canon_label(_row_identity(o)) for o in rows}) != 1:
+ return None
+ # The tagged leaf and its option ancestor are independent name surfaces: a shared leaf label
+ # ("Choose") must not mask ancestors that disagree, so each position vetoes on its own.
+ for surface in range(2):
+ names = {
+ _canon_label(str((o.get("labels") or [None, None])[surface]))
+ for o in rows
+ if (o.get("labels") or [None, None])[surface]
+ }
+ if len(names) >= 2:
+ return None
+ present_labels = {_canon_label(str(o.get("label"))) for o in rows if o.get("label")}
+ if len(present_labels) >= 2:
+ return None
+ # Vals are machine identifiers, not display text: compared byte-exact, never case/Unicode-folded,
+ # so "ID-A" and "id-a" stay two candidates. `vals` is the collapse's OWN read of the other value
+ # surfaces (data-code, data-key, name, title, ...) — unfiltered by the commit verifier's numeric/
+ # length drops, so "101" vs "202" is a real disagreement here. Non-empty sets must agree byte-exact;
+ # an empty set says nothing and cannot contradict.
+ present_vals = {str(o.get("val")) for o in rows if o.get("val") is not None}
+ if len(present_vals) >= 2:
+ return None
+ # Two-rule agreement over the surface+attribute-keyed entries. (1) A key present on BOTH rows
+ # must carry one value — crossed values across surfaces refuse, while a value carried on one
+ # row's surface only says nothing against the other row. (2) The depth-blind floor: one row's
+ # flattened attr=value pairs must nest inside the other's. Rows declaring identity on disjoint
+ # attributes cannot be confirmed the same, and an agreeing generic attribute (a shared title)
+ # beside disjoint identifiers is ordinary markup, not identity evidence — the pairs don't nest,
+ # so it refuses. A row declaring NOTHING still constrains nothing (its empty set nests, so an
+ # attribute-less a11y copy collapses).
+ val_maps: list[dict[str, str]] = []
+ for o in rows:
+ entries: dict[str, str] = {}
+ for raw in o.get("vals") or []:
+ key, _, val_part = str(raw).partition("=")
+ entries[key] = val_part
+ val_maps.append(entries)
+ for i, first in enumerate(val_maps):
+ for second in val_maps[i + 1 :]:
+ for shared in first.keys() & second.keys():
+ if first[shared] != second[shared]:
+ return None
+ flat_first = {f"{k.partition(':')[2]}={v}" for k, v in first.items()}
+ flat_second = {f"{k.partition(':')[2]}={v}" for k, v in second.items()}
+ if not (flat_first <= flat_second or flat_second <= flat_first):
+ return None
+ n = rows[0].get("n")
+ return n if isinstance(n, int) else None
+
+
+def _without_nested_copies(value: str, rows: Sequence[_Row]) -> list[_Row]:
+ """Drop an exact-match row that holds another exact-match row: a wrapper and the element carrying its
+ label are one candidate, and the innermost stands for it. `inside` is a row's nearest tagged ancestor;
+ siblings never share a chain, so identical siblings stay separate and are refused."""
+ want = _exact_tier_key(value)
+ exact = {o.get("n") for o in rows if _exact_tier_key(str(o.get("text") or "")) == want}
+ by_n: dict[Any, Mapping[str, Any]] = {o.get("n"): o for o in rows}
+ wrappers: set[Any] = set()
+ for n in exact:
+ seen: set[Any] = set()
+ up = (by_n.get(n) or {}).get("inside")
+ while up is not None and up not in seen:
+ if up in exact:
+ wrappers.add(up)
+ seen.add(up)
+ up = (by_n.get(up) or {}).get("inside")
+ return [o for o in rows if o.get("n") not in wrappers]
+
+
+class Tier(str, Enum):
+ EXACT = "exact"
+ PRIMARY = "primary"
+ STEM = "stem"
+ PREFIX = "prefix"
+
+
+# PRIMARY and STEM are one rank: each is the label up to a decoration or an inflection, so hits across them compete.
+_TIER_RANK = {Tier.EXACT: 0, Tier.PRIMARY: 1, Tier.STEM: 1, Tier.PREFIX: 2}
+ALL_TIERS = frozenset(Tier)
+OFFER_WHY = {
+ Tier.EXACT: "is it",
+ Tier.PRIMARY: "its main label is it",
+ Tier.STEM: "differs from it only by singular/plural",
+ Tier.PREFIX: "starts with it",
+}
+_TIER_NOTES = {
+ Tier.PRIMARY: "its main label is {value}",
+ Tier.STEM: "it is the only option matching {value} by singular/plural",
+ Tier.PREFIX: "it is the only option starting with {value}",
+}
+
+RefusalReason = Literal["none", "identical", "ambiguous", "incomplete", "reduced_query", "unsettled", "held"]
+
+
[email protected](frozen=True)
+class Pick:
+ n: int
+ text: str
+ tier: Tier
+
+
[email protected](frozen=True)
+class Refusal:
+ reason: RefusalReason
+ contenders: tuple[Mapping[str, Any], ...] = ()
+
+
[email protected](frozen=True)
+class Offer:
+ row: Mapping[str, Any]
+ tier: Tier | None
+
+
+_TRAILING_GROUP_RE = re.compile(r"^(.*?\S)\s*\([^()]*\)$")
+_SPACED_DASH_RE = re.compile(r"\s+[-\u2013\u2014]\s+")
+_DIAL_CODE_RE = re.compile(r"\s+\+\d[\d\s-]{0,6}$")
+
+
+def _without_leading_pictographs(text: str) -> tuple[str, bool]:
+ # Only pictographs and flags (Unicode So, with their joiners and modifiers) are decoration. A sign such as "<", ">"
+ # or "~" changes what the row means ("< 1 year" is not "1 year").
+ i, seen = 0, False
+ while i < len(text) and (unicodedata.category(text[i]) in ("So", "Sk") or text[i] in "\ufe0f\u200d"):
+ seen = seen or unicodedata.category(text[i]) == "So"
+ i += 1
+ if not (seen and text[i : i + 1].isspace()):
+ return text, False
+ # Only a flag (a regional-indicator pair) marks the row as a country, which is what makes a trailing "+N" a code.
+ return text[i:].lstrip(), any(0x1F1E6 <= ord(c) <= 0x1F1FF for c in text[:i])
+
+
+def primary_parts(text: str, dom_primary: str | None = None) -> tuple[str, ...]:
+ # Each part is ONE typographic cut: the one trailing group `_label_names_value` allows, the spaced dash `_lead_clause`
+ # splits on, or a dialing code. Unlike `_lead_clause`, never a comma: "City, Region" heads a hierarchy. A trailing
+ # "+N" is a dialing code only beside a leading flag; elsewhere it is part of the label ("👥 Employee +1").
+ body, flagged = _without_leading_pictographs(" ".join(text.split()))
+ group = _TRAILING_GROUP_RE.match(body)
+ dialed = _DIAL_CODE_RE.sub("", body) if flagged else ""
+ parts = [body, group.group(1) if group else "", _SPACED_DASH_RE.split(body, 1)[0], dialed]
+ if dom_primary and " ".join(dom_primary.split()) != " ".join(text.split()):
+ parts.append(dom_primary)
+ return tuple(dict.fromkeys(p for p in parts if p.strip()))
+
+
+def _dom_primary(row: Mapping[str, Any]) -> str | None:
+ return row["label"] if isinstance(row.get("label"), str) else None
+
+
+def tier_of(value: str, row: Mapping[str, Any], allow: frozenset[Tier] = ALL_TIERS) -> Tier | None:
+ """How `row` names `value`, read on the row side only: a shorter row never names a longer value. A main label
+ `allow` leaves out is read at the tiers below it."""
+ text, want = str(row.get("text") or ""), _exact_tier_key(value)
+ if not want:
+ return None
+ if _exact_tier_key(text) == want:
+ return Tier.EXACT
+ if Tier.PRIMARY in allow and any(_exact_tier_key(p) == want for p in primary_parts(text, _dom_primary(row))):
+ return Tier.PRIMARY
+ if match_option_exact_or_stem_with_tier(_canon_label(value), [_canon_label(text)])[1] == "stem":
+ return Tier.STEM
+ return Tier.PREFIX if _is_forward_prefix(_prefix_tokens(value), _prefix_tokens(text)) else None
+
+
+def _collapse_exact(value: str, rows: Sequence[Mapping[str, Any]], bare: frozenset[int]) -> int | None:
+ # A declared row outranks a bare node wearing its label. Bare rows carry no identity to veto on, so among them only
+ # nesting tells one candidate's copies from siblings.
+ declared = [o for o in rows if o.get("n") not in bare]
+ if len(declared) == 1:
+ return declared[0]["n"]
+ if declared:
+ return _lone_duplicate_candidate(declared)
+ survivors = _without_nested_copies(value, rows)
+ return survivors[0]["n"] if len(survivors) == 1 else None
+
+
+def match_choice(
+ value: str,
+ rows: Sequence[Mapping[str, Any]],
+ *,
+ complete: bool,
+ query_full: bool = True,
+ allow: frozenset[Tier] = ALL_TIERS,
+ collapse: bool = False,
+ bare: frozenset[int] = frozenset(),
+) -> Pick | Refusal:
+ """Which listed row is `value`. Exact rows that are not one candidate refuse before any lower tier. A non-exact hit
+ commits only when it is unique at the best rank (leaves sharing a `unit` are one row), its tier is allowed, the
+ query was the value itself and the list was read whole."""
+ graded = [(o, t) for o in rows if isinstance(o.get("n"), int) and (t := tier_of(value, o, allow)) is not None]
+ exact = [o for o, t in graded if t is Tier.EXACT]
+ if exact:
+ n = exact[0]["n"] if len(exact) == 1 else _collapse_exact(value, exact, bare) if collapse else None
+ if n is None:
+ return Refusal("identical", tuple(exact))
+ return Pick(n, next(str(o.get("text") or "") for o in exact if o["n"] == n), Tier.EXACT)
+ if not graded:
+ return Refusal("none")
+ rank = min(_TIER_RANK[t] for _, t in graded)
+ units: dict[object, tuple[Mapping[str, Any], Tier]] = {}
+ for o, t in graded:
+ if _TIER_RANK[t] == rank:
+ # Leaves of one declared row are one option only while they name the same thing: two controls inside one row,
+ # each with its own label, stay two contenders.
+ unit = (
+ (o["unit"], _dom_primary(o) or str(o.get("text") or "")) if isinstance(o.get("unit"), int) else o["n"]
+ )
+ units.setdefault(unit, (o, t))
+ contenders = tuple(o for o, _ in units.values())
+ hit: tuple[Mapping[str, Any], Tier] | None = next(iter(units.values())) if len(units) == 1 else None
+ if hit is None and collapse and (n := _lone_duplicate_candidate(contenders)) is not None:
+ hit = next(u for u in units.values() if u[0]["n"] == n)
+ if hit is None:
+ return Refusal("ambiguous", contenders)
+ if hit[1] not in allow:
+ return Refusal("none", contenders)
+ if not query_full:
+ return Refusal("reduced_query", contenders)
+ if not complete:
+ return Refusal("incomplete", contenders)
+ return Pick(hit[0]["n"], str(hit[0].get("text") or ""), hit[1])
+
+
+def contender_texts(value: str, texts: Sequence[str]) -> list[str]:
+ """The texts any tier takes for `value`, one entry per row."""
+ return [t for t in texts if tier_of(value, {"text": t}) is not None]
+
+
+def nearest(value: str, rows: Sequence[Mapping[str, Any]], k: int = 3) -> tuple[Offer, ...]:
+ """The rows most like `value`: tier hits by rank, then shared words, then string similarity, then list order. A row
+ sharing no word and under half similar is left out."""
+ want_words, want, seen = set(_prefix_tokens(value)), _canon_label(value), set()
+ ranked: list[tuple[tuple[float, ...], Offer]] = []
+ for i, o in enumerate(rows):
+ text = str(o.get("text") or "")
+ key = _exact_tier_key(text)
+ if not key or key in seen or o.get("nav"):
+ continue
+ seen.add(key)
+ parts = primary_parts(text, _dom_primary(o))
+ shared = len(want_words & set(_prefix_tokens(" ".join(parts))))
+ matchers = [difflib.SequenceMatcher(None, want, _canon_label(p)) for p in parts]
+ tier = tier_of(value, o)
+ # quick_ratio() bounds ratio() from above, so a row it already rules out skips the full comparison on long lists.
+ if tier is None and not shared and max(m.quick_ratio() for m in matchers) < 0.5:
+ continue
+ ratio = max(m.ratio() for m in matchers)
+ if tier is None and not shared and ratio < 0.5:
+ continue
+ ranked.append(((_TIER_RANK[tier] if tier else 3, -shared, -ratio, i), Offer(o, tier)))
+ ranked.sort(key=lambda r: r[0])
+ return tuple(offer for _, offer in ranked[:k])
+
+
+def with_tier_note(result: ToolResult, tier: Tier | None, shown_value: str) -> ToolResult:
+ """Names the inference behind an ok whose row is not the value exactly; `shown_value` is as the model may see it."""
+ if result.status != "ok" or tier is None or tier is Tier.EXACT:
+ return result
+ note = _TIER_NOTES[tier].format(value=repr(shown_value))
+ return dataclasses.replace(
+ result,
+ content=f"{result.content}; not an exact match: {note}",
+ data={**(result.data or {}), "choice": {"tier": tier.value}},
+ )
diff --git a/skyvern/forge/taskv3/loop.py b/skyvern/forge/taskv3/loop.py
index 5a762baf7..6dee9704d 100644
--- a/skyvern/forge/taskv3/loop.py
+++ b/skyvern/forge/taskv3/loop.py
@@ -29,7 +29,7 @@ from urllib.parse import urlsplit
import structlog
-from skyvern.exceptions import SkyvernContextWindowExceededError
+from skyvern.exceptions import CompletionGateTerminationError, SkyvernContextWindowExceededError, StepTerminationError
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
from skyvern.forge.sdk.workflow.context_manager import RANDOM_SECRET_ID_PREFIX
@@ -575,6 +575,14 @@ CompletionBlocker = Callable[[frozenset[str]], Awaitable[str | None]]
# non-complete verdict given up with polling budget still unspent are the same concern seen from two
# sides; splitting them into two hooks would scatter it.
VerificationBlocker = Callable[[str], Awaitable[str | None]]
+# The deployment's completion gate, consulted last on finish(completed): False refuses the verdict, while
+# CompletionGateTerminationError ends the run terminated and StepTerminationError ends it failed.
+CompletionGate = Callable[[], Awaitable[bool]]
+COMPLETION_GATE_REFUSAL = (
+ "The completion check did not confirm the result on the current page. If you have not submitted yet, "
+ "finish the step and call finish again. If you already submitted, do not submit again: wait for or look "
+ "for the confirmation, then call finish."
+)
# The status a re-ask conversion asks the verification gate about: a completion the model never claimed.
CONVERSION_VERIFICATION_STATUS = "converted"
@@ -658,6 +666,8 @@ class LoopOutcome:
converted_from: NonCompletedStatus | None = None
converted_from_reason: str = ""
unlisted_reask: dict[str, Any] | None = None
+ # The finish tool's completion gate approved this verdict, so the caller must not consult it again.
+ gate_passed: bool = False
def _get(obj: Any, key: str, default: Any = None) -> Any:
@@ -1559,6 +1569,7 @@ class ActivityRecency:
turn: int = 0
turns_remaining: int | None = None
tool_calls_remaining: int | None = None
+ action_steps_remaining: int | None = None
tokens_remaining: int | None = None
last_turn_tokens: int = 0
last_trigger_turn: int | None = None
@@ -2448,6 +2459,7 @@ def make_finish_tool(
verification_blocker: VerificationBlocker | None = None,
unlisted_reask: UnlistedReaskCheck | None = None,
document_identity: Callable[[], Awaitable[str | None]] | None = None,
+ completion_gate: CompletionGate | None = None,
) -> ToolSpec:
"""`page_fingerprint` samples an opaque fingerprint of the page's rendered content (None when no
page is available). A finish(completed) is deferred (bounded by `max_settle_deferrals`, then
@@ -2484,7 +2496,11 @@ def make_finish_tool(
missed a screen the site skipped. A grounded yes becomes completed unless a completed-side gate vetoes it;
it never gives a turn back, and a completed verdict cannot reach it. Its settle gate samples as many times as
the completed path would defer. When `document_identity` is wired, it is read before the judge and again after
- the settle window, and a conversion whose identity changed or could not be read is refused, settled or not."""
+ the settle window, and a conversion whose identity changed or could not be read is refused, settled or not.
+
+ `completion_gate` runs after every other completed-side gate, and only while the run has the turns and
+ action steps to act on a refusal; without them, or when the gate errors, the caller's post-loop check
+ decides. A refusal never ends the run, so only a later finish the gate approves can complete it."""
deferrals = 0
failure_deferrals = 0
reask_asked = False
@@ -2751,6 +2767,40 @@ def make_finish_tool(
"that keeps changing on its own (a clock, countdown or ticker) is not by itself a reason to "
"report failure."
)
+ gate_passed = False
+ if (
+ status == "completed"
+ and completion_gate is not None
+ and _has_hold_headroom(activity, deadline_at)
+ and (activity is None or activity.action_steps_remaining is None or activity.action_steps_remaining > 0)
+ ):
+ try:
+ async with asyncio.timeout(None if deadline_at is None else deadline_at - time.monotonic()):
+ gate_passed = await completion_gate()
+ except CompletionGateTerminationError as termination:
+ return ToolResult.ok(
+ content="Task attempt ended. No further actions are permitted.",
+ data={
+ "status": "terminated",
+ "reason": termination.reason,
+ "extracted_output": args.get("extracted_output"),
+ },
+ )
+ except StepTerminationError as exhausted:
+ return ToolResult.ok(
+ content="Task attempt ended. No further actions are permitted.",
+ data={
+ "status": "failed",
+ "reason": exhausted.message or "",
+ "extracted_output": args.get("extracted_output"),
+ },
+ )
+ except Exception:
+ LOG.warning("taskv3 completion gate errored; leaving the verdict to the caller", exc_info=True)
+ else:
+ if not gate_passed:
+ LOG.info("taskv3 completed verdict refused by the completion gate")
+ return ToolResult.error(COMPLETION_GATE_REFUSAL)
if (
status == "failed"
and activity is not None
@@ -2826,6 +2876,7 @@ def make_finish_tool(
"status": status,
"reason": args.get("reason") or "",
"extracted_output": args.get("extracted_output"),
+ "gate_passed": gate_passed,
},
)
@@ -3882,6 +3933,8 @@ async def run_agent_tool_loop(
activity.turn = st.turns
activity.turns_remaining = st.max_turns - st.turns
activity.tool_calls_remaining = st.max_tool_calls - st.total_tool_calls
+ if st.max_action_steps is not None:
+ activity.action_steps_remaining = st.max_action_steps - st.action_steps
# Elide superseded perception results before re-sending the transcript, so a perception-heavy
# run can't balloon the context to the token backstop (the pre-compaction runaway mode).
@@ -4544,6 +4597,8 @@ async def run_agent_tool_loop(
elif call_charged:
st.uncharged_refusals = 0
turn_charged = turn_charged or call_charged
+ if activity is not None and st.max_action_steps is not None:
+ activity.action_steps_remaining = st.max_action_steps - st.action_steps - int(turn_charged)
# Entry is recorded only when the tool succeeded (a stale-ref failure put nothing in the
# field), but the submit is recorded on DISPATCH whatever the verdict: the loop ran the
# click itself, so nothing here asks the page whether a submission completed. Entry first,
@@ -5024,6 +5079,7 @@ async def run_agent_tool_loop(
extracted_output=data.get("extracted_output"),
converted_from=_non_completed(data.get("converted_from")),
converted_from_reason=data.get("converted_from_reason", ""),
+ gate_passed=bool(data.get("gate_passed")),
# The model's own verdict wins whether or not it landed on the granted final turn;
# cap_trip just records the fact that a cap forced this to be the last turn.
cap_trip=st.cap_trip_pending if st.final_turn_granted else None,
diff --git a/skyvern/forge/taskv3/tools.py b/skyvern/forge/taskv3/tools.py
index 87ca1648c..1bfc23249 100644
--- a/skyvern/forge/taskv3/tools.py
+++ b/skyvern/forge/taskv3/tools.py
@@ -28,7 +28,7 @@ import sys
import time
import weakref
from collections import Counter, defaultdict, deque
-from collections.abc import Sequence
+from collections.abc import Mapping, Sequence
from contextvars import ContextVar
from datetime import datetime
from types import MappingProxyType
@@ -40,11 +40,6 @@ from PIL import Image, ImageDraw
from skyvern.config import settings
from skyvern.constants import BROWSER_DOWNLOADING_SUFFIX
-from skyvern.core.script_generations.fuzzy_matcher import (
- match_option_exact_or_stem,
- match_option_exact_or_stem_with_tier,
- normalize_option_label,
-)
from skyvern.forge.sdk.api.files import resolve_run_download_id
from skyvern.forge.sdk.artifact.storage.base import get_download_retry_started_at, is_file_from_retry_attempt
from skyvern.forge.sdk.core import skyvern_context
@@ -61,15 +56,28 @@ from skyvern.forge.sdk.workflow.models.credential_release import (
from skyvern.forge.taskv3 import input_dispatch
from skyvern.forge.taskv3.field_commit import (
_ZERO_WIDTH_RE,
+ OFFER_WHY,
CommitStatus,
EntryMatch,
+ Pick,
+ Refusal,
+ Tier,
_canon_label,
_classify_commit,
+ _exact_tier_key,
+ _lone_duplicate_candidate,
+ _row_identity,
_same_cell_value,
_typed_text_landed,
+ _without_nested_copies,
compare_text,
+ contender_texts,
entry_outcome,
+ match_choice,
+ nearest,
+ tier_of,
unconfirmed_ok,
+ with_tier_note,
)
from skyvern.forge.taskv3.loop import (
ACTION_OUTCOME_DATA_KEY,
@@ -629,6 +637,9 @@ class _TypeaheadPick(NamedTuple):
shared_surface: str | None = None
# The call's final result when pressing Enter to search settled it before any row was picked.
verdict: ToolResult | None = None
+ # How the picked row names the value, and why no row was picked.
+ tier: Tier | None = None
+ refusal: Refusal | None = None
# The smallest query many closed-vocabulary pickers need before they render candidates. Read by the
@@ -654,37 +665,6 @@ def _label_names_value(label: str, value: str) -> bool:
return bool(inner) and "(" not in inner and ")" not in inner
-def _match_option_exact(value: str, options: list[dict[str, Any]]) -> int | None:
- """Pick the row whose WHOLE label IS `value` after canonical cleanup (case/whitespace/apostrophes/
- Unicode forms/zero-width). No stem, prefix, or other inferred tier is ever accepted here — anything
- short of that exact match returns None so the caller hands the rows back instead of guessing.
- """
- rows = [(o.get("n"), str(o.get("text") or "")) for o in options if isinstance(o.get("n"), int)]
- if not value or not rows:
- return None
- idx, tier = match_option_exact_or_stem_with_tier(_canon_label(value), [_canon_label(label) for _, label in rows])
- return rows[idx][0] if idx is not None and tier == "exact" else None
-
-
-def _without_nested_copies(value: str, rows: list[dict[str, Any]]) -> list[dict[str, Any]]:
- """Drop an exact-match row that holds another exact-match row: a wrapper and the element carrying its
- label are one candidate, and the innermost stands for it. `inside` is a row's nearest tagged ancestor;
- siblings never share a chain, so identical siblings stay separate and are refused."""
- want = _exact_tier_key(value)
- exact = {o.get("n") for o in rows if _exact_tier_key(str(o.get("text") or "")) == want}
- by_n = {o.get("n"): o for o in rows}
- wrappers: set[Any] = set()
- for n in exact:
- seen: set[Any] = set()
- up = (by_n.get(n) or {}).get("inside")
- while up is not None and up not in seen:
- if up in exact:
- wrappers.add(up)
- seen.add(up)
- up = (by_n.get(up) or {}).get("inside")
- return [o for o in rows if o.get("n") not in wrappers]
-
-
def _rows_nested_in(n: int, rows: list[dict[str, Any]]) -> list[str]:
"""The labels of rows whose `inside` chain reaches row `n`."""
by_n = {o.get("n"): o for o in rows}
@@ -701,17 +681,10 @@ def _rows_nested_in(n: int, rows: list[dict[str, Any]]) -> list[str]:
return nested
-def _exact_tier_key(text: str) -> str:
- """The exact tier's own equality key — `_canon_label` plus the shared matcher's case/apostrophe fold —
- for pre-filters asking "which rows did that tier see as this value". A plain `_canon_label` filter
- folds less than the tier does and would disagree with it, so pre-filters must use this key instead.
- """
- return normalize_option_label(_canon_label(text))
-
-
-def _log_committed_pick(row: str, value: str, verdict: CommitStatus, path: str) -> None:
+def _log_committed_pick(row: str, value: str, verdict: CommitStatus, path: str, tier: Tier | None = None) -> None:
# Whether the clicked row IS the requested value, or the value plus one parenthesized decoration, so an
- # inexact pick can be counted apart from an exact one. No form values and no selector.
+ # inexact pick can be counted apart from an exact one; `tier` is the matcher's decision, None where no tier
+ # decided the row. No form values and no selector.
LOG.info(
"taskv3 combobox pick verdict",
tool_call_seq=current_tool_call_seq(),
@@ -719,76 +692,10 @@ def _log_committed_pick(row: str, value: str, verdict: CommitStatus, path: str)
verdict=verdict.value,
committed_matches_row=_exact_tier_key(row) == _exact_tier_key(value),
row_names_value=_label_names_value(row, value),
+ tier=tier.value if tier is not None else None,
)
-def _row_identity(o: dict[str, Any]) -> str:
- """The row's identity as `_MENU_OPTION_TEXTS_JS`'s `rowIdentity` derived it. A row with no `identity` key
- is a note entry, whose `text` is already that identity; an empty one (a row nothing could read) is absent."""
- identity = o.get("identity")
- return identity if isinstance(identity, str) and identity else str(o.get("text") or "")
-
-
-def _lone_duplicate_candidate(rows: list[dict[str, Any]]) -> int | None:
- """Whether ≥2 matched rows are the SAME candidate rendered more than once: canonical row IDENTITY
- agreement across every row is the load-bearing check, and a present aria-label, `val`, or other declared
- value is a VETO on top of it — one disagreeing across otherwise-identical rows marks them distinct, an
- absent one never does. Returns the FIRST row's `n` when the whole set collapses to one candidate, else
- None so the caller keeps refusing.
- """
- if len(rows) < 2:
- return None
- if len({_canon_label(_row_identity(o)) for o in rows}) != 1:
- return None
- # The tagged leaf and its option ancestor are independent name surfaces: a shared leaf label
- # ("Choose") must not mask ancestors that disagree, so each position vetoes on its own.
- for surface in range(2):
- names = {
- _canon_label(str((o.get("labels") or [None, None])[surface]))
- for o in rows
- if (o.get("labels") or [None, None])[surface]
- }
- if len(names) >= 2:
- return None
- present_labels = {_canon_label(str(o.get("label"))) for o in rows if o.get("label")}
- if len(present_labels) >= 2:
- return None
- # Vals are machine identifiers, not display text: compared byte-exact, never case/Unicode-folded,
- # so "ID-A" and "id-a" stay two candidates. `vals` is the collapse's OWN read of the other value
- # surfaces (data-code, data-key, name, title, ...) — unfiltered by the commit verifier's numeric/
- # length drops, so "101" vs "202" is a real disagreement here. Non-empty sets must agree byte-exact;
- # an empty set says nothing and cannot contradict.
- present_vals = {str(o.get("val")) for o in rows if o.get("val") is not None}
- if len(present_vals) >= 2:
- return None
- # Two-rule agreement over the surface+attribute-keyed entries. (1) A key present on BOTH rows
- # must carry one value — crossed values across surfaces refuse, while a value carried on one
- # row's surface only says nothing against the other row. (2) The depth-blind floor: one row's
- # flattened attr=value pairs must nest inside the other's. Rows declaring identity on disjoint
- # attributes cannot be confirmed the same, and an agreeing generic attribute (a shared title)
- # beside disjoint identifiers is ordinary markup, not identity evidence — the pairs don't nest,
- # so it refuses. A row declaring NOTHING still constrains nothing (its empty set nests, so an
- # attribute-less a11y copy collapses).
- val_maps: list[dict[str, str]] = []
- for o in rows:
- entries: dict[str, str] = {}
- for raw in o.get("vals") or []:
- key, _, val_part = str(raw).partition("=")
- entries[key] = val_part
- val_maps.append(entries)
- for i, first in enumerate(val_maps):
- for second in val_maps[i + 1 :]:
- for shared in first.keys() & second.keys():
- if first[shared] != second[shared]:
- return None
- flat_first = {f"{k.partition(':')[2]}={v}" for k, v in first.items()}
- flat_second = {f"{k.partition(':')[2]}={v}" for k, v in second.items()}
- if not (flat_first <= flat_second or flat_second <= flat_first):
- return None
- n = rows[0].get("n")
- return n if isinstance(n, int) else None
-
-
_ORDINAL_CAP = 2**31 - 1
@@ -1072,136 +979,6 @@ def _lead_clause(text: str) -> str:
return re.split(r"\s*[,;|(]\s*|\s+[-\u2013\u2014]\s+", norm)[0].strip()
-def _prefix_tokens(s: str) -> list[str]:
- # Fold commas and apostrophes so a short value token-prefix-matches a punctuated label ("Yes" → "Yes, I
- # consent"). A slash is left intact so a combined "Yes/No" option is not prefix-matched by "Yes".
- return re.sub(r"[,'’]", " ", s).lower().split()
-
-
-def _is_forward_prefix(want: list[str], label: list[str]) -> bool:
- return bool(want) and len(want) < len(label) and label[: len(want)] == want
-
-
-def _tier_for(want_canon: str, want_tokens: list[str], text: str) -> int | None:
- _, tier = match_option_exact_or_stem_with_tier(want_canon, [_canon_label(text)])
- if tier is not None:
- return 0 if tier == "exact" else 1
- return 2 if _is_forward_prefix(want_tokens, _prefix_tokens(text)) else None
-
-
-def _option_tier(value: str, text: str) -> int | None:
- """The best `_match_menu_option` tier that accepts `text` for `value`: 0 exact, 1 singular/plural stem,
- 2 forward word-prefix; None when no tier would."""
- return _tier_for(_canon_label(value), _prefix_tokens(value), text)
-
-
-def _match_menu_option(value: str, options: list[dict[str, Any]], *, collapse_duplicates: bool = False) -> int | None:
- """Pick the enumerated menu row (its data-tv3-menu index) whose label matches the wanted value.
-
- Deterministic and site-agnostic, precision-first. Exact/singular-plural-stem matching (apostrophe
- folding, unique-or-None) is delegated to the shared `match_option_exact_or_stem` so this is not a
- third copy of that logic. Failing that, a UNIQUE FORWARD token-prefix — the observed value is a whole-
- token prefix of a fuller option label ("Decline" → "Decline to self-identify") — is accepted. The
- REVERSE direction is deliberately NOT matched: committing a shorter, more-general option for a longer
- value ("New York" → "New") is a silent wrong success, and on a virtualised window the fuller row may
- simply be unrendered. A value that is only an incidental SUBSTRING of an option is never matched ("No"
- inside "Prefer not to answer"). Ambiguity or no match returns None so the caller hands the options
- back to the model. Uniqueness is only meaningful over the COMPLETE list — the caller must not pass a
- truncated slice.
-
- `collapse_duplicates` is for the one call site that reads the whole, non-overflowed menu: when the
- exact/stem tier or the prefix tier finds more than one hit, it hands those rows to
- `_lone_duplicate_candidate` before giving up — a value that matches several DOM rows wearing the
- same candidate still resolves, while several genuinely distinct rows still refuse. Off by default so
- a caller working from a partial or reconstructed row set (a virtualised scroll-search window) never
- collapses on incomplete evidence.
- """
- rows = [(o.get("n"), str(o.get("text") or "")) for o in options if isinstance(o.get("n"), int)]
- if not value or not rows:
- return None
-
- # Canonicalize before the exact/stem tier — the shared normalizer folds case and apostrophes but
- # not internal spacing, Unicode forms or zero-width characters.
- want_canon = _canon_label(value)
- hit = match_option_exact_or_stem(want_canon, [_canon_label(label) for _, label in rows])
- if hit is not None:
- return rows[hit][0]
- if collapse_duplicates:
- want_key = _exact_tier_key(value)
- exact_matched = [
- o for o in options if isinstance(o.get("n"), int) and _exact_tier_key(str(o.get("text") or "")) == want_key
- ]
- if len(exact_matched) >= 2:
- collapsed = _lone_duplicate_candidate(exact_matched)
- if collapsed is not None:
- return collapsed
- elif not exact_matched:
- # Mirror the shared matcher's stem tier (trailing-s stem, 3-char floor) so a duplicated row
- # that would stem-commit as a lone row still collapses. Only when the exact tier saw
- # NOTHING — the matcher never consults stems once exact matches exist.
- want_stem = want_key.rstrip("s")
- if len(want_stem) >= 3:
- stem_matched = [
- o
- for o in options
- if isinstance(o.get("n"), int)
- and len(k := _exact_tier_key(str(o.get("text") or "")).rstrip("s")) >= 3
- and k == want_stem
- ]
- if len(stem_matched) >= 2:
- collapsed = _lone_duplicate_candidate(stem_matched)
- if collapsed is not None:
- return collapsed
-
- want = _prefix_tokens(value)
- if not want:
- return None
- prefixed = [n for n, label in rows if _is_forward_prefix(want, _prefix_tokens(label))]
- if len(prefixed) == 1:
- return prefixed[0]
- if collapse_duplicates and len(prefixed) > 1:
- prefixed_ns = set(prefixed)
- prefixed_rows = [o for o in options if isinstance(o.get("n"), int) and o.get("n") in prefixed_ns]
- return _lone_duplicate_candidate(prefixed_rows)
- return None
-
-
-def _same_row(a: dict[str, Any], b: dict[str, Any]) -> bool:
- """Two reads of ONE row: the same raw text and no value or name that tells them apart. Rows that only share an
- identity key are distinct rows and stay two contenders."""
- return str(a.get("text") or "") == str(b.get("text") or "") and _lone_duplicate_candidate([a, b]) is not None
-
-
-def _distinct_rows(rows: list[dict[str, Any]]) -> list[dict[str, Any]]:
- out: list[dict[str, Any]] = []
- for row in rows:
- if not any(_same_row(row, o) for o in out):
- out.append(row)
- return out
-
-
-def _value_contenders(value: str, texts: list[str]) -> list[str]:
- """The texts any tier of `_match_menu_option` would accept for `value`, one entry per row."""
- want_canon, want_tokens = _canon_label(value), _prefix_tokens(value)
- return [t for t in texts if _tier_for(want_canon, want_tokens, t) is not None]
-
-
-def _commit_gate(
- hit_text: str, value: str, *, complete: bool, twins_seen: bool, contenders: list[str]
-) -> Literal["commit", "ambiguous", "incomplete"]:
- """The one rule every open-observe-pick path applies before clicking a matched row: the rows judged are the
- whole list, and nothing else the value names competes with the hit (the post-click read-back verifies it).
- `contenders` holds one entry per distinct row, so a label listed twice is two rows wearing it."""
- if twins_seen:
- return "ambiguous"
- tier = _option_tier(value, hit_text)
- rank = 3 if tier is None else tier
- # The hit is one of the contender rows; a second row at its tier or better competes with it.
- if sum(1 for t in contenders if (r := _option_tier(value, t)) is not None and r <= rank) >= 2:
- return "ambiguous"
- return "commit" if complete else "incomplete"
-
-
async def _read_list_window(page: Any, raw: Any, *, scroller: bool) -> _ListWindow:
"""One read of the rows `raw` against the scroller the finder tagged. A tagged scroller whose state cannot be
read leaves an unbounded extent, so the read never proves the list whole."""
@@ -1227,6 +1004,67 @@ def _unproven_row_error(value: str, selector: str, seen: str, row: dict[str, Any
)
+def _choice_tiers(*, typeahead: bool) -> frozenset[Tier]:
+ # The one read of the kill switch: off, a typeahead commits exact rows only and no path commits a main-label hit.
+ if not settings.TASK_V3_CHOICE_NONEXACT_COMMIT:
+ return frozenset({Tier.EXACT}) if typeahead else frozenset(Tier) - {Tier.PRIMARY}
+ # A typed search's row that only starts with the value is often one record of a hierarchy (an address lookup's
+ # top-k), so it is offered, never committed.
+ return frozenset(Tier) - {Tier.PREFIX} if typeahead else frozenset(Tier)
+
+
+def _same_row(a: Mapping[str, Any], b: Mapping[str, Any]) -> bool:
+ """Two reads of ONE row: the same raw text and no value or name that tells them apart."""
+ return str(a.get("text") or "") == str(b.get("text") or "") and _lone_duplicate_candidate([a, b]) is not None
+
+
+def _log_choice(path: str, choice: Pick | Refusal, rows: int, value: str, **facts: Any) -> None:
+ # No values and no selector. `tied` names the match kinds an ambiguous refusal weighed, so a main-label vs plural
+ # tie can be read apart from rows that merely share a label.
+ tied = (
+ sorted({t.value for o in choice.contenders if (t := tier_of(value, o)) is not None})
+ if isinstance(choice, Refusal) and choice.reason == "ambiguous"
+ else None
+ )
+ LOG.info(
+ "taskv3 choice match",
+ tool_call_seq=current_tool_call_seq(),
+ path=path,
+ tier=choice.tier.value if isinstance(choice, Pick) else None,
+ reason=choice.reason if isinstance(choice, Refusal) else None,
+ tied=tied,
+ rows=rows,
+ **facts,
+ )
+
+
+_NOT_PICKED = {
+ "incomplete": "the list may hold more rows",
+ "reduced_query": "the typed query was shortened",
+ "unsettled": "the list was still changing",
+ "held": "the field already shows the value",
+ "none": "a typed search commits only a row that is the value or its main label",
+}
+
+
+def _nearest_sentence(
+ value: str, rows: Sequence[Mapping[str, Any]], refusal: Refusal | None, sel: Callable[[Mapping[str, Any]], str]
+) -> str:
+ """The rows most like `value`, each with what it shares with it and why it was not picked; '' when none is near."""
+ offers = nearest(value, rows)
+ why_not = _NOT_PICKED.get(refusal.reason, "") if refusal is not None else ""
+ if refusal is not None and refusal.reason == "ambiguous":
+ why_not = f"{len(refusal.contenders)} rows match it equally"
+ parts = []
+ for offer in offers:
+ why = [OFFER_WHY[offer.tier]] if offer.tier is not None else []
+ if why and offer.tier is not Tier.EXACT and why_not:
+ why.append(f"not picked: {why_not}")
+ text = _model_text(offer.row.get("text") or "", 120)
+ parts.append(f"{sel(offer.row)}{text!r}" + (f" ({' — '.join(why)})" if why else ""))
+ return f"nearest: {'; '.join(parts)}" if parts else ""
+
+
def _ambiguous_rows_error(
selector: str,
value: str,
@@ -1237,6 +1075,7 @@ def _ambiguous_rows_error(
rows_unread: bool = False,
live_token: str | None = None,
live_search: str | None = None,
+ choice: Refusal | None = None,
) -> ToolResult:
"""The refusal owed a caller when rows reacted and none of them IS the requested value.
@@ -1255,8 +1094,17 @@ def _ambiguous_rows_error(
}
rows = [row for row in rows if id(row) not in copies]
shown = rows[:15]
+ if len(rows) > 15:
+ # Past 15 rows the ones shown are the contenders and the nearest, still printed in list order.
+ ranked = [*(choice.contenders if choice else ()), *(o.row for o in nearest(value, rows, k=15)), *rows]
+ keep = list(dict.fromkeys(o.get("n") for o in ranked))[:15]
+ shown = [o for o in rows if o.get("n") in keep]
+
+ def _sel(o: Mapping[str, Any]) -> str:
+ return f'[data-tv3-sugg="{o.get("n")}"][data-tv3-pick="{live_token}"] ' if live_token is not None else ""
+
listing = "; ".join(
- (f'[data-tv3-sugg="{o.get("n")}"][data-tv3-pick="{live_token}"] ' if live_token is not None else "")
+ _sel(o)
+ repr(_model_text(o.get("text") or "", 60))
+ (_row_value_suffix(o, groups[_canon_label(str(o.get("text") or ""))]) if live_token is not None else "")
for o in shown
@@ -1267,7 +1115,7 @@ def _ambiguous_rows_error(
if several
else f"{value!r} is not the one row showing in {selector}: "
)
- data = {"release_own_list": True}
+ data: dict[str, Any] = {"release_own_list": True}
if live_token is not None:
lead = (
f"{value!r} is not exactly any of the {len(rows)} rows showing in {selector}: "
@@ -1291,6 +1139,8 @@ def _ambiguous_rows_error(
note = note.removesuffix(" — type the option's full label")
tail = f" ({note})" if note else ""
message = f"{lead}{listing}{f'; +{more} more' if more > 0 else ''}{tail} — {next_step}; the field is NOT filled"
+ if choice is not None and (near := _nearest_sentence(value, rows, choice, _sel)):
+ message += f"; {near}"
# Spelled as literal writes rather than one computed error_class: the source census can only read a
# literal, and a single-file mypy run does not bind the annotation across the import either.
if several:
@@ -1947,7 +1797,7 @@ _PRESNAPSHOT_JS = (
# the field declares (rowSelFor: role=option, or a gridcell of a grid popup the field itself declares),
# EVERY such row is promoted, tagged data-tv3-sugg="1..N" top-to-bottom and returned as
# {count, options, declared: true} — geometry must never break a tie between real options, so the
-# caller's own precision matcher (_match_menu_option) picks, over the full text _MENU_OPTION_TEXTS_JS
+# caller's own precision matcher (match_choice) picks, over the full text _MENU_OPTION_TEXTS_JS
# reads back. Where nothing declares a row, reconstructing one from geometry does not converge, so the
# finder keeps the single-winner contract instead: highest score, innermost, refusing an ambiguous
# leading-clause tie and a multi-row container, as {count: 1, options: [the row], declared: false}.
@@ -1956,7 +1806,8 @@ _PRESNAPSHOT_JS = (
# snippet: two hand-copied predicates drifted once (menuitem listed as both option and nav, so
# `<a role=menuitem href>` read as an option and was clicked off the form). Semantics resolve from the
# closest declaring ANCESTOR, not the reduced leaf (`<a href><span>` reduces to the span).
-_ROW_SEMANTICS_JS = r"""
+_ROW_SEMANTICS_JS = (
+ r"""
const OPT_SEL = '[role="option"],[role="menuitemradio"],[role="menuitemcheckbox"],[role="treeitem"],[role="radio"]';
const NAV_SEL = 'a[href],button,[role="button"],[role="link"],[role="menuitem"],[role="tab"]';
const LIST_SEL = '[role="listbox"],[role="menu"],[role="tree"],[role="grid"],[role="radiogroup"],datalist';
@@ -2090,6 +1941,25 @@ _ROW_SEMANTICS_JS = r"""
return !declaredTargets(field).some((t) => t === region || pContains(t, region) || pContains(region, t));
} catch (e) { return false; }
};
+ // The one row admission both finders apply. 80 characters is the "row-sized text, not a paragraph" ceiling; a row
+ // the page declares an option may run to ROW_TEXT_MAX, and a searched row to what was searched plus 80.
+ const UNDECLARED_ROW_TEXT_MAX = 80;
+ const ROW_TEXT_MAX = """
+ + str(_MENU_ROW_TEXT_MAX)
+ + r""";
+ const declaresOption = (el) => {
+ try {
+ return el.matches(DECLARED_ROW_SEL) || !!el.querySelector(DECLARED_ROW_SEL) || !!composedClosest(el, DECLARED_ROW_SEL)
+ || !!composedClosest(el, '[role="listbox"],[role="menu"]');
+ } catch (e) { return false; }
+ };
+ const admitRow = (el, txt, o) => {
+ const cap = Math.max(UNDECLARED_ROW_TEXT_MAX, o.searchedCap || 0);
+ if (txt.length > cap && (txt.length > Math.max(ROW_TEXT_MAX, cap) || !o.declares(el))) return 'caption';
+ if (o.vetoNav && isNavRow(el)) return 'nav';
+ if (isAnnouncement(el, o.field)) return 'announcement';
+ return null;
+ };
// A control the row only WRAPS reaches the click just as the row's own box does -- but only while
// it is rendered. A display:none or visibility:hidden action never receives that click, so it says
// nothing about where the row leads.
@@ -2256,6 +2126,7 @@ _ROW_SEMANTICS_JS = r"""
return lists[0];
};
"""
+)
# The values a row declares for itself: the attributes on the row or on its option ancestor that NAME
# a value. A widget that commits a code ("CA" for "California") commits one of these, and nothing else
@@ -2301,12 +2172,9 @@ _FIND_SUGGESTION_BODY_JS = (
// appear, so no word of it gates a row, and which reacting node is the value is the caller's exact
// matcher's decision, never this script's.
const searched = typeof args.match === 'string';
- // 80 is the shared "row-sized text, not a paragraph" ceiling (_FIND_MENU_JS uses the same one). It
- // is a flat number everywhere else because no other finder knows what it is looking for -- this one
- // does: on a searched path the caller named the value in args.match, and only a row whose whole text
- // IS that value can ever be committed. So the ceiling is measured from it, keeping 80 as the
- // allowance for the extra parts a declared row can render beside its label.
- const txtCap = searched ? String(args.match).replace(/\s+/g, ' ').trim().length + 80 : 80;
+ // On a searched path the caller named the value in args.match, and only a row that is that value can ever be
+ // committed, so the caption ceiling is measured from it.
+ const searchedCap = searched ? String(args.match).replace(/\s+/g, ' ').trim().length + 80 : 0;
pQSA('[data-tv3-sugg], [data-tv3-pick]').forEach((e) => {
e.removeAttribute('data-tv3-sugg');
e.removeAttribute('data-tv3-pick');
@@ -2372,11 +2240,9 @@ _FIND_SUGGESTION_BODY_JS = (
if (!(ownPopup && pContains(ownPopup, el)) && (r.top < fr.top - 400 || r.top > fr.bottom + 500)) continue;
}
const txt = (el.innerText || '').trim();
- if (!txt || txt.length > txtCap) continue;
- // never click something navigational (would leave the form) unless it's explicitly an option;
- // last of the gates because it is the only one that walks a subtree
- if (isNavRow(el)) continue;
+ if (!txt) continue;
let score = 0;
+ let stemOnly = false;
const norm = txt.replace(/\s+/g, ' ').trim().toLowerCase();
// Without the dropdown region to lean on, what is left to separate a suggestion from a segmented
// control's own echo -- which is a node the widget REPLACES on input, so it reads as new -- is
@@ -2397,10 +2263,22 @@ _FIND_SUGGESTION_BODY_JS = (
const have = toks(txt);
for (const w of want) if (have.has(w)) score++;
if (isExact && score > 0) score += 100;
+ // A singular/plural of a word ranks beside the main label in the caller's matcher, so the gate has to let that
+ // row through as a rival (the shared stem rule: trailing s stripped, 3+ characters left); it never wins alone.
+ if (score <= 0) {
+ const stem = (w) => w.replace(/s+$/, '');
+ const haveStems = new Set([...have].map(stem).filter((w) => w.length >= 3));
+ stemOnly = [...want].some((w) => haveStems.has(stem(w)));
+ }
}
- if (score <= 0) continue;
- // Last, so a skipped node is one that scored (a wrapper around a region included).
- if (isAnnouncement(el, field)) { announceSkipped++; skippedEls.push(el); continue; }
+ if (score <= 0 && !stemOnly) continue;
+ // Last, so a skipped node is one that scored (a wrapper around a region included). The nav veto keeps anything
+ // navigational (it would leave the form) from being clicked unless it is explicitly an option.
+ const skip = admitRow(el, txt, {
+ field, vetoNav: true, searchedCap, declares: (e) => declaresOption(e) || !!composedClosest(e, rowSel),
+ });
+ if (skip === 'announcement') { announceSkipped++; skippedEls.push(el); }
+ if (skip) continue;
cands.push({ el, score, h: r.height, exactRow: isExact, r });
}
// A skipped node still takes part in containment: a wrapper whose scoring content is an announcement is no row.
@@ -2450,21 +2328,28 @@ _FIND_SUGGESTION_BODY_JS = (
const units = rows.concat(bare.map((c) => ({ el: c.el, r: c.r, bare: true })));
if (!units.length) return null;
// Top-to-bottom order, same as _FIND_MENU_JS. This finder only says which rows reacted; the caller
- // (via _match_menu_option, over the full untruncated text _MENU_OPTION_TEXTS_JS reads back) picks
+ // (via match_choice, over the full untruncated text _MENU_OPTION_TEXTS_JS reads back) picks
// which one is the wanted value -- geometry never breaks a tie between them.
units.sort((a, b) => a.r.top - b.r.top || a.r.left - b.r.left);
const options = [];
// Which tagged units the widget declared NO row for. Same text is not the same option: the caller
// needs the split to keep a bare node from standing in for a row the widget actually declared.
const bareNs = [];
+ // Units sharing no whole word with the value got in as singular/plural rivals only: never a pick on their own, and
+ // a reaction made of nothing else is no reaction.
+ const stemNs = [];
+ const sharesWord = (c) => searched || exact !== null || [...toks(c.el.innerText || '')].some((w) => want.has(w));
+ if (!units.some(sharesWord)) return null;
let n = 0;
for (const c of units) {
n++;
c.el.setAttribute('data-tv3-sugg', String(n));
if (c.bare) bareNs.push(n);
+ if (!sharesWord(c)) stemNs.push(n);
if (options.length < 15) options.push({ n, text: (c.el.innerText || '').trim() });
}
- return { count: n, options, declared: true, bare: bareNs };
+ // `gate`: whether every row sharing a word with the value was a candidate, which a non-exact pick relies on.
+ return { count: n, options, declared: true, bare: bareNs, stemOnly: stemNs, gate: searched ? 'searched' : exact !== null ? 'exact' : 'overlap' };
}
// Nothing here declares a row, so there is no unit to name: take the highest score, breaking ties
// toward the smallest (innermost) row.
@@ -2488,7 +2373,7 @@ _FIND_SUGGESTION_BODY_JS = (
// An empty-state row is never the winner.
const typedNorm = String(args.value || '').replace(/\s+/g, ' ').trim().toLowerCase();
const emptyState = (n) => emptyStateRow(n, typedNorm);
- const live = pool.filter((c) => !emptyState(c.el));
+ const live = pool.filter((c) => !emptyState(c.el) && c.score > 0);
// The exact row the widget will not take still names the answer, so no lesser row may stand in for it.
const barred = pool.find((c) => c.exactRow && emptyState(c.el));
if (barred && !live.some((c) => c.exactRow)) {
@@ -2522,7 +2407,7 @@ _FIND_SUGGESTION_JS = (
)
# Read back the row _FIND_SUGGESTION_JS tagged data-tv3-sugg="N" once the caller has picked `n` (via
-# _match_menu_option): its declared value/data-* attributes -- a widget that commits a code ("CA" for a
+# match_choice): its declared value/data-* attributes -- a widget that commits a code ("CA" for a
# row displaying "California") commits one of these -- and whether it came from the FOCUS-opened list
# rather than reacting to a keystroke (that pick is verified under the open->observe->pick contract, not
# the typeahead's change-based one; see _VERIFY_COMMIT_JS's `noSuggestionList`).
@@ -2725,22 +2610,26 @@ _DECLARED_LIST_ANSWER_JS = (
const ROW = DECLARED_ROW_SEL + ',[role="gridcell"]';
// A natively disabled row is not an answer; a label drawn in a shadow root still is.
const dead = (row) => emptyStateRow(row, typedNorm) || row.hasAttribute('disabled');
- const label = (row) => ((row.innerText || '').trim() || composedRowText(row)).replace(/\s+/g, ' ').trim().toLowerCase();
+ // The owned list's live rows, hidden ones included: Python judges which one IS the typed text.
+ const labels = [];
const owned = fieldOwnPopup(field, false);
if (owned) {
for (const row of pScopeAll()) {
if (!pContains(owned, row) || !row.matches(ROW) || dead(row)) continue;
- if (typedNorm && label(row) === typedNorm) return 'option';
+ labels.push((row.innerText || '').trim() || composedRowText(row));
}
}
- const popup = fieldOwnPopup(field, true);
- if (!popup) return (field.getAttribute('aria-expanded') || '').toLowerCase() === 'false' ? 'none' : 'rows';
- for (const row of pScopeAll()) {
- if (!pContains(popup, row) || !row.matches(ROW) || dead(row)) continue;
- const r = row.getBoundingClientRect();
- if (r.width > 0 && r.height > 0 && getComputedStyle(row).visibility !== 'hidden') return 'rows';
- }
- return 'none';
+ const state = (() => {
+ const popup = fieldOwnPopup(field, true);
+ if (!popup) return (field.getAttribute('aria-expanded') || '').toLowerCase() === 'false' ? 'none' : 'rows';
+ for (const row of pScopeAll()) {
+ if (!pContains(popup, row) || !row.matches(ROW) || dead(row)) continue;
+ const r = row.getBoundingClientRect();
+ if (r.width > 0 && r.height > 0 && getComputedStyle(row).visibility !== 'hidden') return 'rows';
+ }
+ return 'none';
+ })();
+ return { state, labels };
}"""
)
@@ -7325,6 +7214,21 @@ _LIST_BOX_JS = r"""
};
"""
+# Tags the scroller of the rows the suggestion finder tagged, so `_list_coverage` judges a typeahead's rows as it
+# judges a menu's.
+_TAG_SUGG_SCROLLER_JS = (
+ r"""(arg) => {"""
+ + _PIERCED_QUERY_JS
+ + _ROW_SEMANTICS_JS
+ + _TAG_LIST_SCROLLER_JS
+ + r"""
+ pQSA('[data-tv3-menu-scroller]').forEach((e) => e.removeAttribute('data-tv3-menu-scroller'));
+ const g = pQSA('[data-tv3-sugg]').map((el) => ({ el, r: el.getBoundingClientRect() }));
+ g.sort((a, b) => a.r.top - b.r.top);
+ return g.length ? tagListScroller(g, g[0].el) : false;
+}"""
+)
+
# Behavioral, site-agnostic menu finder: after a click (with the pre-snapshot taken first),
# look for the option list the page rendered IN REACTION — a NEW container (not data-tv3-pre: a
# pre-existing visible container whose rows merely changed, e.g. pagination refreshing a results list,
@@ -7345,12 +7249,6 @@ _FIND_MENU_JS = (
+ _MENU_ROW_ROLES_JS
+ r""";
const vis = (r) => r.width > 0 && r.height > 0;
- // 80 is the shared "row-sized text, not a paragraph" ceiling. A row the page declares an option or
- // menu item may run to ROW_TEXT_MAX, so a long option caption is listed whole.
- const UNDECLARED_ROW_TEXT_MAX = 80;
- const ROW_TEXT_MAX = """
- + str(_MENU_ROW_TEXT_MAX)
- + r""";
// `cascade`: the caller just clicked a row that DETACHED (a category replacing the list with its
// children). The trigger is gone, so trigger-anchored geometry/ARIA is waived — new rows in a
// FLOATING container carry the claim instead (enforced below).
@@ -7426,17 +7324,12 @@ _FIND_MENU_JS = (
const { r, txt } = box;
// A long row is an option only where the page declares one on it, in it, around it, or by its list (a
// listbox/menu, or the list a combobox controls): a pointer cursor alone makes every paragraph a row.
- if (txt.length > UNDECLARED_ROW_TEXT_MAX) {
- let declaresOption = false;
- try {
- declaresOption = el.matches(DECLARED_ROW_SEL) || !!el.querySelector(DECLARED_ROW_SEL) || !!composedClosest(el, DECLARED_ROW_SEL)
- || !!composedClosest(el, '[role="listbox"],[role="menu"]')
- || (controlledList && controlled !== el && pContains(controlled, el));
- } catch (e) { declaresOption = false; }
- if (!declaresOption) {
- if (arg.saltUnlisted && ownClickable(el)) unreadLong.push(el);
- continue;
- }
+ const skip = admitRow(el, txt, {
+ field: trigger, declares: (e) => declaresOption(e) || (controlledList && controlled !== e && pContains(controlled, e)),
+ });
+ if (skip) {
+ if (skip === 'caption' && arg.saltUnlisted && ownClickable(el)) unreadLong.push(el);
+ continue;
}
// Options are individually actionable rows. Requiring it per-row keeps a dialog's title/body
// text from being listed as "options" (and a horizontal Confirm/Cancel button pair then fails
@@ -7846,6 +7739,8 @@ _MENU_OPTION_TEXTS_JS = (
setsize_unknown: setsize === -1,
group: grp ? accessibleName(grp) || grp.id || 'group' : '',
lead: !rowOf(el) || leadOf.get(rowOf(el)) === el,
+ // The tag of the declared row's first leaf: leaves of one row are one option to the matcher.
+ unit: rowOf(el) ? num(leadOf.get(rowOf(el))) : null,
box: box,
after: after,
line: Number.isFinite(line) && line > 0 ? line : null,
@@ -15617,6 +15512,7 @@ def build_browser_tools(
# row before the results); the last reaction is returned when time runs out.
found: dict[str, Any] | None = None
refused_a_reaction = False
+ was_busy = False
soft_deadline = time.monotonic() + 0.4 * rounds
hard_deadline = time.monotonic() + 8.0
while True:
@@ -15645,8 +15541,11 @@ def build_browser_tools(
busy = bool(await page.evaluate(_MENU_BUSY_JS, await _probe_arg(page, selector)))
except Exception:
busy = False
- if not busy:
+ # Rows that replace the indicator land between the read above and this probe, so a list that just
+ # stopped being busy gets one more read before the wait ends.
+ if not busy and not was_busy:
break
+ was_busy = busy
return found
async def _commit_typeahead(
@@ -15661,9 +15560,9 @@ def build_browser_tools(
live_offer: tuple[str, list[dict[str, Any]]] | None = None,
) -> _TypeaheadPick:
# Poll for the suggestion rows rendered IN REACTION to whatever is already typed into `selector`,
- # pick among them, click, and verify the field committed. When the widget DECLARES its rows the
- # commit is exact-only: a row is picked when its whole label IS `value`, never on a stem or
- # prefix hit, so an ambiguous reaction is always handed back rather than guessed at. Where
+ # pick among them, click, and verify the field committed. When the widget DECLARES its rows,
+ # `match_choice` picks: a row whose whole label IS `value`, or a lone main-label or singular/plural
+ # hit on a whole, settled list the full value was typed into; anything else is handed back. Where
# nothing declares a list, the finder returns the single winner it always did and the pick is
# that row. `candidates` is the full-text reacting rows when a declared pick refused — the
# caller reports these instead of guessing which one was meant; None when nothing reacted, when
@@ -15694,17 +15593,14 @@ def build_browser_tools(
# floor under the pick, not the filter that does the work.
return [o for o in raw if isinstance(o, dict) and isinstance(o.get("n"), int) and not o.get("nav")]
- def _pick(rows: list[dict[str, Any]]) -> int | None:
- # A declared row commits solely on an exact label match, never on inferred meaning — an
- # unmatched row, lone or not, is refused like any other so the model chooses explicitly.
- return _match_option_exact(value, rows)
-
# Which tagged units the widget declared no row for, kept so verification can ask about the row
# actually CHOSEN rather than about the mixed pool it came from.
bare_tagged: set[int] = set()
+ choice: Pick | Refusal | None = None
+ choice_facts: dict[str, Any] = {}
async def _resolve(found: dict[str, Any]) -> tuple[bool, list[dict[str, Any]], int | None, int]:
- # A widget that declares its rows hands the pick to the exact matcher over every row it
+ # A widget that declares its rows hands the pick to `match_choice` over every row it
# tagged. Where nothing declares one, the finder already reduced the reaction to a single
# winner and there is nothing left to choose between — except under a reduced query, whose
# rows answer a broader question than the caller asked. The 4th value is the declared
@@ -15714,49 +15610,47 @@ def build_browser_tools(
if not tagged:
return bool(found.get("declared")), [], None, 0
if found.get("declared"):
+ # The scroller is tagged before the rows are read: a row's box is read against the tagged scroller.
+ try:
+ scroller = await page.evaluate(_TAG_SUGG_SCROLLER_JS, {}) is True
+ except Exception:
+ scroller = False
rows = await _full_rows()
- nonlocal bare_tagged
+ nonlocal bare_tagged, choice, choice_facts
bare_ns = {x for x in (found.get("bare") or []) if isinstance(x, int)}
bare_tagged = bare_ns
# Only rows the widget DECLARED count against its own aria-setsize; a bare node padding
# the list would hide a genuinely truncated one.
declared_rendered = [o for o in rows if o.get("n") not in bare_ns]
overflow = _declared_shortfall([_list_row(o) for o in declared_rendered])
- idx = _pick(rows)
- if idx is None and overflow == 0:
- # No exact-label winner over the complete rendered list — but "several rows" may be
- # one candidate wearing more than one face (an a11y duplicate, a portal+inline
- # render). Collapse only over the FULL list (never a partial/overflowed one), and
- # only to a genuinely lone survivor; several distinct candidates still refuse below.
- want = _exact_tier_key(value)
- matched = [o for o in rows if _exact_tier_key(str(o.get("text") or "")) == want]
- if len(matched) >= 2:
- # A row the widget DECLARED outranks a bare node wearing the same label. Text
- # agreement is what the collapse reads, and these two agree on it while being
- # different options — the bare one can commit another record under an identical
- # label, which reads as success. Only the declared unit may stand for them.
- declared_matched = [o for o in matched if o.get("n") not in bare_ns]
- if declared_matched:
- idx = (
- _lone_duplicate_candidate(declared_matched)
- if len(declared_matched) >= 2
- else declared_matched[0].get("n")
- )
- else:
- # Every exact match is bare, so the text-agreement collapse cannot run:
- # bare rows carry no declared identity for it to veto on, and two distinct
- # records wearing one label would read as a single candidate. Only nesting
- # is decidable here — a wrapper and the node carrying its label are ONE
- # candidate, related by the `inside` chain, and the innermost stands for
- # them. Siblings never share that chain, so they survive as two and refuse.
- survivors = _without_nested_copies(value, matched)
- if len(survivors) == 1:
- idx = survivors[0].get("n")
- return True, rows, idx, overflow
+ window = await _read_list_window(page, declared_rendered, scroller=scroller)
+ # A non-exact row is judged only where every row sharing a word with the value was a candidate.
+ choice_facts = {
+ "gate": found.get("gate"),
+ "complete": overflow == 0 and _list_coverage([window]) == "complete",
+ "query_full": not exact_only,
+ }
+ choice = match_choice(
+ value,
+ rows,
+ complete=bool(choice_facts["complete"]) and choice_facts["gate"] == "overlap",
+ query_full=not exact_only,
+ allow=_choice_tiers(typeahead=True),
+ collapse=overflow == 0,
+ bare=frozenset(bare_ns),
+ )
+ stem_only = {x for x in (found.get("stemOnly") or []) if isinstance(x, int)}
+ if isinstance(choice, Pick) and choice.n in stem_only:
+ # A row that shares only a singular/plural with the value stands as a rival, never as the answer.
+ choice = Refusal("none", tuple(o for o in rows if o.get("n") == choice.n))
+ return True, rows, choice.n if isinstance(choice, Pick) else None, overflow
if exact_only:
# The tagger returns at most 15 rows; an exact match has to see every one.
rows = await _full_rows()
- return False, rows, _match_option_exact(value, _without_nested_copies(value, rows)), 0
+ choice = match_choice(
+ value, _without_nested_copies(value, rows), complete=False, allow=frozenset({Tier.EXACT})
+ )
+ return False, rows, choice.n if isinstance(choice, Pick) else None, 0
return False, tagged, 1, 0
async def _row_info(n: int) -> dict[str, Any]:
@@ -15766,6 +15660,43 @@ def build_browser_tools(
info = None
return info if isinstance(info, dict) else {}
+ async def _rows_hold(judged_rows: list[dict[str, Any]]) -> tuple[int, bool, list[dict[str, Any]]]:
+ # A top-k list still answering can swap a twin in under a non-exact hit: the rows judged must read back the
+ # same, with the list not busy, 0.6 s later, within 2.4 s. The rows last read come back too: re-running the finder
+ # renumbers the tags, so a refusal must list those, never the judged ones.
+ start = time.monotonic()
+
+ def sig(rows: list[dict[str, Any]]) -> list[tuple[Any, ...]]:
+ # Text alone misses a same-text row carrying another value and a list that grew its declared size.
+ return [
+ (
+ o.get("n"),
+ o.get("text"),
+ o.get("label"),
+ o.get("val"),
+ repr(o.get("vals")),
+ o.get("setsize"),
+ o.get("setsize_unknown"),
+ )
+ for o in rows
+ ]
+
+ want = sig(judged_rows)
+ last = judged_rows
+ while time.monotonic() - start < 2.4:
+ await asyncio.sleep(0.6)
+ try:
+ busy = bool(await page.evaluate(_MENU_BUSY_JS, await _probe_arg(page, selector)))
+ except Exception:
+ busy = False
+ if busy:
+ continue
+ # Re-run the finder so a row that arrived after the first read is tagged and counted.
+ last = await _full_rows() if await _find() is not None else []
+ if sig(last) == want:
+ return round((time.monotonic() - start) * 1000), True, last
+ return round((time.monotonic() - start) * 1000), False, last
+
judged: tuple[bool, list[dict[str, Any]], int | None, int] | None = None
async def _names_the_value(found: dict[str, Any]) -> bool:
@@ -15778,8 +15709,9 @@ def build_browser_tools(
if live_offer is not None:
token, rows = live_offer
bare_tagged = {row["n"] for row in rows if row.get("bare")}
- matched = [row for row in rows if _exact_tier_key(str(row.get("text") or "")) == _exact_tier_key(value)]
- idx = matched[0]["n"] if len(matched) == 1 else _lone_duplicate_candidate(matched)
+ # A follow-up names a row by its exact label, so it commits only an exact row.
+ live = match_choice(value, rows, complete=False, allow=frozenset({Tier.EXACT}), collapse=True)
+ idx = live.n if isinstance(live, Pick) else None
if idx is None:
return _TypeaheadPick(None, None, False, None, clicked=False, declared=False)
stamp = f'[data-tv3-sugg="{idx}"][data-tv3-pick="{token}"]'
@@ -15808,6 +15740,17 @@ def build_browser_tools(
return _TypeaheadPick(None, None, False, None, clicked=False, declared=False, verdict=verdict)
declared_rows, rows, idx, overflow = judged if judged is not None else await _resolve(found)
stamp = f'[data-tv3-sugg="{idx}"]'
+ if isinstance(choice, Pick) and choice.tier is not Tier.EXACT:
+ held = bool(pre_own) and _exact_tier_key(pre_own or "") == _exact_tier_key(value)
+ settle_ms, settled, last = (0, False, rows) if held else await _rows_hold(rows)
+ choice_facts["settle_ms"] = settle_ms
+ if not settled:
+ # A list that emptied leaves no tag behind, so the judged rows name nothing a click could reach.
+ rows = last or rows
+ hit = tuple(o for o in rows if tier_of(value, o) is not None)
+ choice, idx = Refusal("held" if held else "unsettled", hit), None
+ if choice is not None:
+ _log_choice("typeahead", choice, len(rows), value, **choice_facts)
if idx is None:
declares = "its total unknown" if overflow < 0 else f"{overflow} rows"
note = (
@@ -15824,6 +15767,7 @@ def build_browser_tools(
declared=declared_rows,
note=note,
overflow=overflow,
+ refusal=choice if isinstance(choice, Refusal) else None,
)
best_txt = next((str(o.get("text") or "") for o in rows if o.get("n") == idx), value)
info = await _row_info(idx)
@@ -15869,11 +15813,15 @@ def build_browser_tools(
if live_offer is not None:
LOG.debug("taskv3 live row click failed; retrying through typing", selector=selector, exc_info=True)
return _TypeaheadPick(None, None, False, None, clicked=False, declared=False)
+ # The re-resolve below sets `choice` only on the declared path, so an earlier decision must not stand in for it.
+ choice = None
try:
refound = await _find()
if refound is not None:
declared_rows, rows, idx2, _overflow = await _resolve(refound)
- if idx2 is not None:
+ # A list that re-rendered under the click is the case the settle exists for, so only an exact
+ # row is re-picked here.
+ if idx2 is not None and not (isinstance(choice, Pick) and choice.tier is not Tier.EXACT):
idx = idx2
best_txt = next((str(o.get("text") or "") for o in rows if o.get("n") == idx), value)
info = await _row_info(idx)
@@ -15956,6 +15904,7 @@ def build_browser_tools(
declared=declared_rows,
pre_surface_hit=pre_surface_hit,
shared_surface=shared_surface,
+ tier=choice.tier if isinstance(choice, Pick) else None,
)
async def _is_native_date_input(page: Any, selector: str) -> bool:
@@ -16110,9 +16059,15 @@ def build_browser_tools(
# Fail toward an answer: a list that could not be read is no evidence the text matched nothing.
try:
arg = {**(await _probe_arg(page, selector)), "typed": text}
- return await page.evaluate(_DECLARED_LIST_ANSWER_JS, arg) != "none"
+ read = await page.evaluate(_DECLARED_LIST_ANSWER_JS, arg)
except Exception:
return True
+ if not isinstance(read, dict):
+ return True
+ want = _exact_tier_key(text)
+ return read.get("state") != "none" or (
+ bool(want) and any(isinstance(t, str) and _exact_tier_key(t) == want for t in read.get("labels") or [])
+ )
async def _answers_as_search(page: Any, selector: str) -> bool:
# Rows that reacted to the keystrokes, or aria-autocomplete list/both/inline, which also covers a combobox
@@ -18532,12 +18487,13 @@ def build_browser_tools(
note=pick.note,
rows_unread=bool(pick.overflow),
live_token=live_token,
+ choice=pick.refusal,
)
if pick.suggestion:
suggestion = _model_text(pick.suggestion, 60)
verdict, matches = await _typeahead_commit_verdict(page, selector, pick.committed, pick.readable)
if pick.clicked:
- _log_committed_pick(pick.suggestion, text, verdict, "type")
+ _log_committed_pick(pick.suggestion, text, verdict, "type", pick.tier)
if verdict is CommitStatus.OK:
if pick.declared:
closed = await _close_lingering_typeahead_list(
@@ -18545,9 +18501,13 @@ def build_browser_tools(
)
if closed is not None:
return closed
- return ToolResult.ok(
- f"typed into {selector}; it is a typeahead — selected {suggestion!r} "
- f"(committed value: {pick.committed!r})"
+ return with_tier_note(
+ ToolResult.ok(
+ f"typed into {selector}; it is a typeahead — selected {suggestion!r} "
+ f"(committed value: {pick.committed!r})"
+ ),
+ pick.tier,
+ _model_text(text, 60),
)
if not pick.committed and pick.shared_surface:
# The widget renders only a short form of the label ("+1"), and either another row visible
@@ -18815,7 +18775,7 @@ def build_browser_tools(
# after each step and matching over everything accumulated so far, keyed by TEXT: the
# virtualiser recycles nodes and `data-tv3-menu` numbers are reassigned 1..N on every scan,
# so a number from an earlier window is not an identity. Any hit, exact or forward-prefix ("United
- # States" -> "United States Minor Outlying Islands"), commits only through `_commit_gate` once the walk
+ # States" -> "United States Minor Outlying Islands"), commits only through `match_choice` once the walk
# has reached the end: a twin, or the exact row a prefix stands in for, may still be below. A label
# read on two distinct rows (two boxes, or twice in one window) is two rows wearing one text and is
# refused as ambiguous.
@@ -18893,13 +18853,12 @@ def build_browser_tools(
def _match_seen() -> str | None:
twins = _twin_keys()
- texts = list(
- dict.fromkeys(
- r.text for w in log.windows for r in w.rows if r.text and not r.nav and r.key not in twins
- )
- )
- hit = _match_menu_option(value, [{"n": k, "text": t} for k, t in enumerate(texts)])
- return texts[hit] if hit is not None else None
+ labels: dict[str, Any] = {}
+ for r in (r for w in log.windows for r in w.rows if r.text and not r.nav and r.key not in twins):
+ labels.setdefault(r.text, r.read.get("label"))
+ rows = [{"n": k, "text": t, "label": label} for k, (t, label) in enumerate(labels.items())]
+ hit = match_choice(value, rows, complete=True, allow=_choice_tiers(typeahead=False))
+ return hit.text if isinstance(hit, Pick) else None
def _row_n(current: list[dict[str, Any]], text: str) -> int | None:
fresh = next((o for o in current if str(o.get("text") or "") == text and not o.get("nav")), None)
@@ -18990,13 +18949,20 @@ def build_browser_tools(
def _verdict(text: str) -> Literal["commit", "ambiguous", "incomplete"]:
twins = _twin_keys()
- return _commit_gate(
- text,
- value,
- complete=complete,
- twins_seen=want in twins or _exact_tier_key(text) in twins,
- contenders=_value_contenders(value, [r.text for r in _log_rows(log.windows) if not r.nav]),
- )
+ if want in twins or _exact_tier_key(text) in twins:
+ return "ambiguous"
+ rows = [
+ {"n": k, "text": r.text, "label": r.read.get("label")}
+ for k, r in enumerate(_log_rows(log.windows))
+ if not r.nav
+ ]
+ choice = match_choice(value, rows, complete=complete, allow=_choice_tiers(typeahead=False))
+ if isinstance(choice, Pick):
+ # A walk that did not read the whole list may have missed an exact twin, so even EXACT waits.
+ if choice.text != text:
+ return "ambiguous"
+ return "commit" if complete else "incomplete"
+ return "incomplete" if choice.reason == "incomplete" else "ambiguous"
def _result(
current: list[dict[str, Any]],
@@ -19065,11 +19031,14 @@ def build_browser_tools(
},
)
- # collapse_duplicates only here: `options` is the COMPLETE, non-overflowed list this call just
- # read in full, so "several rows matched" can safely be checked for one candidate wearing more
- # than one face. The scroll-search's own `_match_seen` call below builds rows from an
- # accumulated, possibly-partial scan and must never collapse on that incomplete evidence.
- idx = None if overflowed else _match_menu_option(value, options, collapse_duplicates=True)
+ # Collapse only here: `options` is the COMPLETE, non-overflowed list this call just read in full, so
+ # "several rows matched" can safely be checked for one candidate wearing more than one face. The
+ # scroll-search's own `_match_seen` builds rows from an accumulated, possibly-partial scan and never collapses.
+ first = None
+ if not overflowed:
+ first = match_choice(value, options, complete=True, allow=_choice_tiers(typeahead=False), collapse=True)
+ _log_choice("open_pick", first, len(options), value, complete=True, query_full=True)
+ idx = first.n if isinstance(first, Pick) else None
matched_from_scroll: str | None = None
scanned_all = False
if idx is None and overflowed:
@@ -19100,7 +19069,7 @@ def build_browser_tools(
held = [t for t in row_texts if _exact_tier_key(t) == key]
if held and n > len(held):
row_texts += [held[0]] * (n - len(held))
- contenders = _value_contenders(value, row_texts)
+ contenders = contender_texts(value, row_texts)
def _ambiguous_error() -> ToolResult:
named = "; ".join(repr(_model_text(t, 60)) for t in contenders[:15])
@@ -19110,20 +19079,24 @@ def build_browser_tools(
error_class="list_ambiguous",
)
- if idx is not None and matched_from_scroll is None:
- full_hit = next((str(o.get("text") or "") for o in options if o.get("n") == idx), value)
- if (
- _commit_gate(
- full_hit,
- value,
- # `idx` is only matched over a read that did not overflow.
- complete=True,
- twins_seen=False,
- contenders=_value_contenders(value, [str(o.get("text") or "") for o in _distinct_rows(options)]),
- )
- != "commit"
- ):
- return _ambiguous_error()
+ if isinstance(first, Refusal) and first.reason == "ambiguous":
+ # The list was read whole and stays open, so each contender is named by its row tag, with what tells
+ # apart rows that share a label.
+ rivals = [dict(o) for o in first.contenders]
+ keys = Counter(_canon_label(str(o.get("text") or "")) for o in rivals)
+
+ def _rival(o: dict[str, Any]) -> str:
+ key = _canon_label(str(o.get("text") or ""))
+ same = [p for p in rivals if _canon_label(str(p.get("text") or "")) == key]
+ suffix = _row_value_suffix(o, same) if keys[key] >= 2 else ""
+ return f'[data-tv3-menu="{o.get("n")}"] {_model_text(o.get("text") or "", 60)!r}{suffix}'
+
+ return ToolResult.error(
+ f"{value!r} is ambiguous in {selector}'s list — it names more than one option "
+ f"({'; '.join(_rival(o) for o in rivals[:15])}); click the intended row by its listed selector, "
+ "or pass the one option's full text",
+ error_class="list_ambiguous",
+ )
async def _popup_filter_pick() -> tuple[int, list[dict[str, Any]]] | ToolResult | None:
# A list that renders only a fixed window of rows never shows one past it, however it is
@@ -19201,7 +19174,7 @@ def build_browser_tools(
except Exception:
raw = []
read_now = [o for o in raw or [] if isinstance(o, dict) and isinstance(o.get("n"), int)]
- # The same rule as the unfiltered read. `_commit_gate` commits no row from an incomplete read.
+ # The same rule as the unfiltered read: `match_choice` commits no non-exact row from an incomplete read.
window = await _read_list_window(
page, read_now, scroller=isinstance(tagged, dict) and bool(tagged.get("scroller"))
)
@@ -19275,6 +19248,7 @@ def build_browser_tools(
still_busy = False
verdict: str | None = None
unproven: str | None = None
+ rival_texts: list[str] = []
# Same budget as the open poll: a read that misses may be a placeholder row or a filter still in
# flight, so the wait ends only past the deadline on a settled, not-busy read.
soft_deadline = time.monotonic() + 2.4
@@ -19299,27 +19273,36 @@ def build_browser_tools(
if [str(o.get("text") or "") for o in filtered] != texts:
last = [str(o.get("text") or "") for o in filtered]
else:
- hit = _match_menu_option(value, filtered, collapse_duplicates=complete) if reacted else None
- hit_text = next((str(o.get("text") or "") for o in filtered if o.get("n") == hit), None)
- if hit is not None and hit_text is not None:
- verdict = _commit_gate(
- hit_text,
- value,
- complete=complete,
- twins_seen=exact_twins,
- contenders=contenders
- + _value_contenders(
- value,
- [
- str(o.get("text") or "")
- for o in _distinct_rows(filtered)
- if not any(_same_row(o, p) for p in evidence_rows)
- ],
- ),
- )
- if verdict == "commit":
- return hit, filtered
- unproven = hit_text if verdict == "incomplete" else None
+ # Rows read before the filter still compete, each with its own values so a twin is never
+ # collapsed into a filtered row it is not; numbered below zero, none of them is clicked.
+ rivals = [
+ o
+ for o in evidence_rows
+ if tier_of(value, o) is not None and not any(_same_row(o, f) for f in filtered)
+ ]
+ pool = [
+ *filtered,
+ *({**o, "n": -1 - k, "unit": None, "inside": None} for k, o in enumerate(rivals)),
+ ]
+ # The filter only narrows the list this call opened, so the open list's tier rules hold.
+ choice = match_choice(
+ value,
+ pool,
+ complete=complete,
+ allow=_choice_tiers(typeahead=False),
+ collapse=complete,
+ )
+ _log_choice("popup_filter", choice, len(pool), value, complete=complete)
+ if reacted and isinstance(choice, Pick) and choice.n >= 0:
+ if complete:
+ return choice.n, filtered
+ # A filtered list read only in part may hide an exact twin, as on the walk.
+ verdict, unproven = "incomplete", choice.text
+ break
+ if reacted and isinstance(choice, Refusal) and choice.reason != "none":
+ verdict = "ambiguous" if choice.reason in ("ambiguous", "identical") else "incomplete"
+ unproven = choice.contenders[0].get("text") if verdict == "incomplete" else None
+ rival_texts = [str(o.get("text") or "") for o in choice.contenders]
break
if time.monotonic() >= hard_deadline:
break
@@ -19341,9 +19324,10 @@ def build_browser_tools(
"the field is NOT filled — pass one option's exact text, or look() and click the option",
error_class="filter_not_found",
)
- several = _value_contenders(value, [str(o.get("text") or "") for o in filtered])
+ several = contender_texts(value, [str(o.get("text") or "") for o in filtered])
if verdict == "ambiguous":
- several = list(dict.fromkeys(contenders + several))
+ # One entry per contending row: two rows wearing one label are two contenders.
+ several = rival_texts
if len(several) < 2 or verdict == "incomplete":
several = []
listed = "; ".join(
@@ -19419,9 +19403,11 @@ def build_browser_tools(
)
vocab = "; ".join(repr(_model_text(o.get("text") or "", 60)) for o in shown)
more = f"; +{len(options) - len(shown)} more" if len(options) > len(shown) else ""
+ # Row numbers from earlier windows are not identities, so the nearest rows are named by text only.
+ near = _nearest_sentence(value, options, None, lambda o: "")
return ToolResult.error(
f"{value!r} matched no option in {selector}'s list — scrolled through all "
- f"{len(options)} options ({vocab}{more}); pass one option's exact text",
+ f"{len(options)} options ({vocab}{more}); pass one option's exact text{f'; {near}' if near else ''}",
error_class="list_no_match",
)
listing = "; ".join(
@@ -19456,17 +19442,19 @@ def build_browser_tools(
return _identical_text_rows_error(selector, value, same_as_value, menu_open=True)
listing = "; ".join(_listed_row(o, t) for o, t in zip(shown, shown_canon_texts))
not_shown = len(options) - len(shown)
+ near = _nearest_sentence(value, options, None, lambda o: f'[data-tv3-menu="{o.get("n")}"] ')
+ near = f"; {near}" if near else ""
if not_shown > 0:
# The match ran over every row the list rendered; only the listing is cut to 15.
return ToolResult.error(
f"opened {selector} but none of the {len(options)} options the list rendered matched "
f"{value!r} (the first {len(shown)}: {listing}) — pick the right one by its "
- '[data-tv3-menu="N"] selector, or pass one option\'s exact text',
+ f'[data-tv3-menu="N"] selector, or pass one option\'s exact text{near}',
error_class="list_no_match",
)
return ToolResult.error(
f"opened {selector} but no option matched {value!r}; the list shows {listing} — "
- 'pick the right one by its [data-tv3-menu="N"] selector, or look() to see the full menu',
+ f'pick the right one by its [data-tv3-menu="N"] selector, or look() to see the full menu{near}',
error_class="list_no_match",
)
matched = next((str(o.get("text") or "") for o in options if o.get("n") == idx), value)
@@ -19576,9 +19564,23 @@ def build_browser_tools(
pre_surface_hit,
)
verdict, matches = await _typeahead_commit_verdict(page, selector, committed or None, readable)
- _log_committed_pick(matched, value, verdict, "open_pick")
+ # The matcher's own decision where it made one; a walk or filter pick is graded under the same switch.
+ tier = (
+ first.tier
+ if isinstance(first, Pick) and first.n == idx
+ else tier_of(
+ value,
+ next((o for o in options if o.get("n") == idx), {"text": matched}),
+ _choice_tiers(typeahead=False),
+ )
+ )
+ _log_committed_pick(matched, value, verdict, "open_pick", tier)
if verdict is CommitStatus.OK:
- return ToolResult.ok(f"selected {matched!r} for {selector} (committed value: {committed!r})")
+ return with_tier_note(
+ ToolResult.ok(f"selected {matched!r} for {selector} (committed value: {committed!r})"),
+ tier,
+ _model_text(value, 60),
+ )
if verdict is CommitStatus.UNVERIFIED and matches != 1:
return unconfirmed_ok(
f"selected {matched!r} for {selector}, but it re-resolved to {matches} elements so the "
@@ -19681,10 +19683,11 @@ def build_browser_tools(
surface_vouched_pre_click: bool = False,
shared_surface: str | None = None,
clicked: bool,
+ tier: Tier | None = None,
) -> ToolResult:
verdict, matches = await _typeahead_commit_verdict(page, selector, committed, readable)
if clicked:
- _log_committed_pick(opt_txt, value, verdict, "typeahead")
+ _log_committed_pick(opt_txt, value, verdict, "typeahead", tier)
opt_txt = _model_text(opt_txt, 60)
if verdict is CommitStatus.OK:
if declared:
@@ -19740,6 +19743,7 @@ def build_browser_tools(
surface_vouched_pre_click=live_pick.pre_surface_hit,
shared_surface=live_pick.shared_surface,
clicked=True,
+ tier=live_pick.tier,
)
def _reduced_queries() -> list[str]:
@@ -19810,6 +19814,7 @@ def build_browser_tools(
surface_vouched_pre_click=rung_pick.pre_surface_hit,
shared_surface=rung_pick.shared_surface,
clicked=rung_pick.clicked,
+ tier=rung_pick.tier,
)
if rung_pick.candidates:
ladder_rung = rung
@@ -19825,6 +19830,7 @@ def build_browser_tools(
rows_unread=bool(rung_pick.overflow),
live_token=live_token,
live_search=rung,
+ choice=rung_pick.refusal,
)
return rung_pick.candidates
return []
@@ -19926,6 +19932,7 @@ def build_browser_tools(
note=pick.note,
rows_unread=bool(pick.overflow),
live_token=live_token,
+ choice=pick.refusal,
)
# The field's own text may be uncommitted (an earlier type()), so it is never reported
# as a fill — only named, so the caller can judge it.
@@ -20026,9 +20033,12 @@ def build_browser_tools(
)
else:
offered_note = ""
+ near = _nearest_sentence(
+ value, [{"n": k, "text": t} for k, t in enumerate(offered)], None, lambda o: ""
+ )
message = (
f"no autocomplete suggestion matched {value!r} for {selector}; the field is NOT filled "
- f"— do not assume success or move on as if it were{offered_note}"
+ f"— do not assume success or move on as if it were{offered_note}{f'; {near}' if near else ''}"
)
if offered:
return ToolResult.error(
@@ -20054,14 +20064,19 @@ def build_browser_tools(
):
return laddered
await _restore_pre_type_value(page, selector, pre_value, typed_queries)
- verdict = await _typeahead_verdict_result(
- pick.suggestion,
- pick.committed,
- pick.readable,
- declared=pick.declared,
- surface_vouched_pre_click=pick.pre_surface_hit,
- shared_surface=pick.shared_surface,
- clicked=pick.clicked,
+ verdict = with_tier_note(
+ await _typeahead_verdict_result(
+ pick.suggestion,
+ pick.committed,
+ pick.readable,
+ declared=pick.declared,
+ surface_vouched_pre_click=pick.pre_surface_hit,
+ shared_surface=pick.shared_surface,
+ clicked=pick.clicked,
+ tier=pick.tier,
+ ),
+ pick.tier,
+ _model_text(value, 60),
)
# A click the widget ignored leaves the caller's search in the field, which is text the
# widget never accepted as a value.
diff --git a/tests/unit/test_agent_task_v3.py b/tests/unit/test_agent_task_v3.py
index 4c0940f24..5d02aed16 100644
--- a/tests/unit/test_agent_task_v3.py
+++ b/tests/unit/test_agent_task_v3.py
@@ -23,7 +23,7 @@ from structlog.testing import capture_logs
from skyvern.config import settings
from skyvern.errors.errors import UserDefinedError
-from skyvern.exceptions import CompletionGateTerminationError, MissingBrowserStatePage
+from skyvern.exceptions import CompletionGateTerminationError, MissingBrowserStatePage, StepTerminationError
from skyvern.forge import agent as agent_module
from skyvern.forge import app
from skyvern.forge.agent import ForgeAgent
@@ -74,6 +74,7 @@ from skyvern.forge.taskv3.handoff_redaction import pin_caller_authored_block_url
from skyvern.forge.taskv3.loop import (
ACTION_BLOCK_TARGET_ACTION_RESERVE,
ACTION_LOOP_GUARD,
+ COMPLETION_GATE_REFUSAL,
NAV_DEAD_END_GUARD,
TOKEN_BUDGET_EXTENDED_EVENT,
LoopOutcome,
@@ -3943,9 +3944,14 @@ async def test_execute_task_v3_completion_gate_veto_fails_the_task(monkeypatch:
TaskStatus.terminated,
"the site holds an earlier entry",
),
+ (
+ StepTerminationError("vetoed too often", step_id="step-v3"),
+ TaskStatus.failed,
+ "Termination error. Reason: Step step-v3 cannot be executed and task is failed. Reason: vetoed too often",
+ ),
(RuntimeError("gate bug"), TaskStatus.completed, None),
],
- ids=["termination", "generic_error_accepts"],
+ ids=["termination", "veto_budget_spent_fails", "generic_error_accepts"],
)
async def test_execute_task_v3_completion_gate_raise(
monkeypatch: pytest.MonkeyPatch, gate_error: Exception, status: TaskStatus, failure_reason: str | None
@@ -3964,6 +3970,65 @@ async def test_execute_task_v3_completion_gate_raise(
assert task.failure_reason == failure_reason
+async def test_a_fifth_veto_on_a_reask_conversion_restores_the_models_own_verdict(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ _step, task, _loop_mock, _post = await _run_execute_task_v3(
+ monkeypatch,
+ LoopOutcome(
+ status="completed",
+ reason="the judge found the goal met",
+ billable_actions=["click"],
+ converted_from="terminated",
+ converted_from_reason="the site offers no way to continue",
+ ),
+ task_block=_make_block(NavigationBlock, navigation_goal="Submit the application"),
+ completion_gate_raises=StepTerminationError("vetoed too often", step_id="step-v3"),
+ data_extraction_goal=None,
+ extracted_information_schema=None,
+ )
+
+ assert task.status == TaskStatus.terminated
+ assert task.failure_reason == "the site offers no way to continue"
+
+
+async def test_execute_task_v3_gate_veto_is_refused_in_loop_and_the_approved_retry_skips_the_post_loop_gate(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ block = _make_block(NavigationBlock, navigation_goal="Submit the application")
+ page = _AdvancingFormPage()
+ caller = _ScriptedCaller(
+ [
+ [("finish", {"status": "completed", "reason": "looks done"})],
+ [("observe", {})],
+ [("finish", {"status": "completed", "reason": "confirmed"})],
+ ]
+ )
+
+ async def _real_loop(kwargs: dict[str, Any]) -> LoopOutcome:
+ return await run_task_v3_agent_loop(
+ **{**kwargs, "page_provider": _fixed_page_provider(page), "llm_caller": caller, "step": None}
+ )
+
+ _step, task, loop_mock, _post = await _run_execute_task_v3(
+ monkeypatch,
+ LoopOutcome(status="failed", reason="the canned outcome must not be used"),
+ task_block=block,
+ loop_body=_real_loop,
+ working_page=page,
+ # A post-loop consult would draw the trailing veto and fail the task.
+ completion_gate_raises=[False, True, False],
+ data_extraction_goal=None,
+ extracted_information_schema=None,
+ )
+
+ assert task.status == TaskStatus.completed
+ assert loop_mock.completion_gate.await_args.kwargs["task_block"] is block
+ assert sum(COMPLETION_GATE_REFUSAL in str(m.get("content")) for m in caller.message_history) == 1
+
+
@pytest.mark.asyncio
async def test_execute_task_v3_a_vetoed_reask_conversion_restores_the_models_verdict(
monkeypatch: pytest.MonkeyPatch,
diff --git a/tests/unit/test_taskv3_engine.py b/tests/unit/test_taskv3_engine.py
index f140f9c12..6e73d58b7 100644
--- a/tests/unit/test_taskv3_engine.py
+++ b/tests/unit/test_taskv3_engine.py
@@ -474,12 +474,19 @@ async def test_engine_defers_completion_while_delayed_render_settles(monkeypatch
@pytest.mark.asyncio
-async def test_page_free_mode_finishes_without_settle_probe() -> None:
- # Page-free runs have no page to settle: finish(completed) is immediate and the provider is
- # never consulted.
+async def test_page_free_mode_finishes_without_settle_probe_or_completion_gate() -> None:
+ # Page-free runs have no page to settle or to gate: finish(completed) is immediate and the
+ # provider is never consulted.
async def no_page() -> Any:
raise AssertionError("provider must not be consulted in page-free mode")
+ gate_calls = 0
+
+ async def refusing_gate() -> bool:
+ nonlocal gate_calls
+ gate_calls += 1
+ return False
+
script = [[("finish", {"status": "completed", "reason": "criteria hold"})]]
caller = _ScriptedCaller(script)
outcome = await run_task_v3_agent_loop(
@@ -487,10 +494,12 @@ async def test_page_free_mode_finishes_without_settle_probe() -> None:
llm_caller=caller,
goal="assess",
page_free=True,
+ completion_gate=refusing_gate,
max_action_steps=2,
- max_turns=4,
+ max_turns=8,
)
- assert outcome.status == "completed"
+ assert (outcome.status, outcome.reason, outcome.gate_passed) == ("completed", "criteria hold", False)
+ assert gate_calls == 0
@pytest.mark.asyncio
diff --git a/tests/unit/test_taskv3_field_commit.py b/tests/unit/test_taskv3_field_commit.py
index 852a6de79..e94487f46 100644
--- a/tests/unit/test_taskv3_field_commit.py
+++ b/tests/unit/test_taskv3_field_commit.py
@@ -5,7 +5,19 @@ import string
import pytest
-from skyvern.forge.taskv3.field_commit import EntryMatch, compare_text, entry_outcome
+from skyvern.forge.taskv3.field_commit import (
+ ALL_TIERS,
+ EntryMatch,
+ Pick,
+ Refusal,
+ Tier,
+ compare_text,
+ entry_outcome,
+ match_choice,
+ nearest,
+ with_tier_note,
+)
+from skyvern.forge.taskv3.loop import ToolResult
@pytest.mark.parametrize(
@@ -113,3 +125,167 @@ def test_entry_outcome_never_echoes_a_value_it_was_not_given() -> None:
for match in (EntryMatch.REFORMATTED, EntryMatch.DIFFERENT):
assert "'" not in entry_outcome("#f", match, shown=None).content
assert "'4417'" in entry_outcome("#f", EntryMatch.DIFFERENT, shown="4417").content
+
+
+def _rows(*texts: str) -> list[dict[str, object]]:
+ return [{"n": i, "text": t} for i, t in enumerate(texts, 1)]
+
+
+_TYPEAHEAD = ALL_TIERS - {Tier.PREFIX}
+
+
+def test_match_choice_menu_matrix() -> None:
+ def pick(value: str, rows: list[dict[str, object]]) -> int | None:
+ choice = match_choice(value, rows, complete=True)
+ return choice.n if isinstance(choice, Pick) else None
+
+ opts = _rows("Analytics", "Engineering", "People Operations")
+ assert pick("Analytics", opts) == 1
+ assert pick(" analytics ", opts) == 1
+ assert pick("PEOPLE OPERATIONS", opts) == 3
+ # A unique forward word-prefix commits on a whole list; the reverse direction and a bare substring never do.
+ assert pick("People", opts) == 3
+ eeo = _rows("Yes", "No", "Decline to self-identify")
+ assert pick("Decline", eeo) == 3
+ assert pick("People Operations Team", opts) is None
+ assert pick("New York", _rows("New", "Newark")) is None
+ assert pick("Prefer not to answer", eeo) is None
+ assert pick("Eng", opts) is None
+ assert pick("Masters Degree", _rows("Master's Degree", "PhD")) == 1
+ assert pick("Legal", opts) is None
+ assert pick("", opts) is None
+ assert pick("Yes", _rows("Yes", "Yes, I consent", "No")) == 1
+ assert pick("United States", _rows("United States Minor", "United States Major")) is None
+ assert pick("Analytics", [{"n": None, "text": "Analytics"}, {"n": 5, "text": "Analytics"}]) == 5
+ assert pick("Yes", _rows("Yes, I consent", "No")) == 1
+ assert pick("Yes", _rows("Yes/No", "Maybe")) is None
+
+
+ ("value", "texts", "n", "tier"),
+ [
+ ("United States", ["\U0001f1fa\U0001f1f8 United States +1"], 1, Tier.PRIMARY),
+ # The decorated row is the value's main label; the longer country only starts with it, a worse rank.
+ (
+ "United States",
+ ["\U0001f1fa\U0001f1f8 United States +1", "\U0001f1fa\U0001f1f2 United States Minor Outlying Islands +1"],
+ 1,
+ Tier.PRIMARY,
+ ),
+ ("United Kingdom", ["\U0001f1ec\U0001f1e7 United Kingdom +44"], 1, Tier.PRIMARY),
+ ("Canada", ["Canada (+1)"], 1, Tier.PRIMARY),
+ ("10023", ["10023(Example Co)"], 1, Tier.PRIMARY),
+ ("Full-time", ["Full-time - Permanent"], 1, Tier.PRIMARY),
+ ("Accounts", ["Account"], 1, Tier.STEM),
+ ],
+)
+def test_match_choice_commits_a_lone_decorated_row_on_a_whole_typed_list(
+ value: str, texts: list[str], n: int, tier: Tier
+) -> None:
+ assert match_choice(value, _rows(*texts), complete=True, allow=_TYPEAHEAD) == Pick(n, texts[n - 1], tier)
+ # The same hit never commits from part of a list, or from rows a shortened query rendered.
+ assert match_choice(value, _rows(*texts), complete=False, allow=_TYPEAHEAD).reason == "incomplete"
+ assert match_choice(value, _rows(*texts), complete=True, query_full=False).reason == "reduced_query"
+
+
+ ("value", "texts"),
+ [
+ ("United States", ["+1"]),
+ # A different ID is a different record, an unspaced dash is part of the label, and a comma heads a hierarchy.
+ ("1002", ["10023 - Example Co"]),
+ ("Full", ["Full-time - Permanent"]),
+ ("Springfield, USA", ["Springfield, Illinois, USA"]),
+ # The row side only: a row shorter than the value never stands for it.
+ ("Canada (+1)", ["Canada"]),
+ ("I accept", ["I agree", "I do not agree"]),
+ ],
+)
+def test_match_choice_refuses_and_offers_rows_that_are_not_the_value(value: str, texts: list[str]) -> None:
+ choice = match_choice(value, _rows(*texts), complete=True)
+ assert isinstance(choice, Refusal) and choice.reason == "none", choice
+ if value != "United States":
+ assert nearest(value, _rows(*texts))[0].row["text"] == texts[0]
+
+
+ ("value", "texts"),
+ [
+ # A leading sign is part of the label, never decoration.
+ ("1 year", ["< 1 year", "1-2 years", "2-5 years"]),
+ ("18", ["< 18", "18-24", "25-34"]),
+ ("10 years", ["> 10 years", "5-10 years", "1-5 years"]),
+ ("Other", ["~ Other", "Something else"]),
+ # A trailing "+N" with no flag beside it is a count, not a dialing code.
+ ("Employee", ["Employee +1", "Employee Only"]),
+ ("Employee", ["Employee +1"]),
+ # Only a flag makes "+N" a dialing code; any other pictograph is decoration and nothing more.
+ ("Employee", ["\U0001f465 Employee +1", "Employee Only"]),
+ ("Gold", ["\u2b50 Gold +2"]),
+ ],
+)
+def test_match_choice_never_commits_a_row_whose_sign_or_count_changes_the_value(value: str, texts: list[str]) -> None:
+ for allow in (_TYPEAHEAD, ALL_TIERS):
+ choice = match_choice(value, _rows(*texts), complete=True, allow=allow)
+ assert not (isinstance(choice, Pick) and choice.tier is Tier.PRIMARY), (allow, choice)
+ assert not isinstance(match_choice(value, _rows(*texts), complete=True, allow=_TYPEAHEAD), Pick)
+
+
+def test_match_choice_names_every_contender_of_one_rank() -> None:
+ both = match_choice("Springfield", _rows("Springfield, IL", "Springfield, MO"), complete=True)
+ assert both == Refusal("ambiguous", tuple(_rows("Springfield, IL", "Springfield, MO")))
+ # A main label and a plural are the same distance from the value, so neither outranks the other.
+ rival = match_choice("Account", _rows("Account (closed)", "Accounts"), complete=True)
+ assert isinstance(rival, Refusal) and rival.reason == "ambiguous" and len(rival.contenders) == 2
+
+
+def test_match_choice_refuses_exact_twins_before_any_lower_tier() -> None:
+ rows = [{"n": 1, "text": "Main", "val": "a"}, {"n": 2, "text": "Main", "val": "b"}, {"n": 3, "text": "Main Office"}]
+ assert match_choice("Main", rows, complete=True, collapse=True).reason == "identical"
+
+
+def test_a_click_opened_list_with_main_labels_switched_off_still_commits_a_lone_prefix_row() -> None:
+ # The kill switch must leave a click-opened list where main left it: a lone row starting with the value commits.
+ rows = [{"n": 1, "text": "Other (please specify)"}, {"n": 2, "text": "Yes"}]
+ assert match_choice("Other", rows, complete=True, allow=ALL_TIERS - {Tier.PRIMARY}) == Pick(
+ 1, "Other (please specify)", Tier.PREFIX
+ )
+
+
+def test_match_choice_counts_the_leaves_of_one_row_once() -> None:
+ # A row whose accessible name is the value, rendered as a flag leaf and a dial-code leaf.
+ leaves = [{"n": 4, "text": "\U0001f1fa\U0001f1f8", "label": "United States", "unit": 4}]
+ leaves.append({"n": 5, "text": "+1", "label": "United States", "unit": 4})
+ assert match_choice("United States", leaves, complete=True) == Pick(4, leaves[0]["text"], Tier.PRIMARY)
+ # Two controls inside one declared row, each with its own label, are two contenders.
+ controls = [
+ {"n": 7, "text": "Full-time - Permanent", "unit": 7},
+ {"n": 8, "text": "Full-time - Contract", "unit": 7},
+ ]
+ assert match_choice("Full-time", controls, complete=True).reason == "ambiguous"
+
+
+def test_a_typed_search_offers_a_row_that_only_starts_with_the_value_first() -> None:
+ rows = _rows("Springdale, AR", "Springfield, Sangamon, IL")
+ choice = match_choice("Springfield", rows, complete=True, allow=_TYPEAHEAD)
+ assert isinstance(choice, Refusal) and choice.reason == "none"
+ offers = nearest("Springfield", rows)
+ assert offers[0].row["text"] == "Springfield, Sangamon, IL" and offers[0].tier is Tier.PREFIX
+
+
+def test_a_refused_paraphrase_commits_once_the_offered_label_is_sent() -> None:
+ rows = _rows("I do not agree", "I agree")
+ assert match_choice("I accept", rows, complete=True).reason == "none"
+ offered = [o.row["text"] for o in nearest("I accept", rows)]
+ assert sorted(offered) == ["I agree", "I do not agree"]
+ assert match_choice(offered[offered.index("I agree")], rows, complete=True) == Pick(2, "I agree", Tier.EXACT)
+
+
+def test_a_non_exact_commit_names_its_tier() -> None:
+ ok = ToolResult.ok("selected 'United States +1' for #cc")
+ noted = with_tier_note(ok, Tier.PRIMARY, "United States")
+ assert noted.content.endswith("not an exact match: its main label is 'United States'"), noted.content
+ assert noted.data == {"choice": {"tier": "primary"}}
+ assert with_tier_note(ok, Tier.EXACT, "United States") is ok
+ refused = ToolResult.error("no", error_class="no_matching_row")
+ assert with_tier_note(refused, Tier.PRIMARY, "x") is refused
diff --git a/tests/unit/test_taskv3_loop.py b/tests/unit/test_taskv3_loop.py
index b6273f1e0..025414efd 100644
--- a/tests/unit/test_taskv3_loop.py
+++ b/tests/unit/test_taskv3_loop.py
@@ -11,6 +11,7 @@ from __future__ import annotations
import asyncio
import copy
import dataclasses
+import functools
import hashlib
import json
import random
@@ -25,7 +26,7 @@ from typing import Any, Awaitable, Callable, Collection
import pytest
from structlog.testing import capture_logs
-from skyvern.exceptions import SkyvernContextWindowExceededError
+from skyvern.exceptions import CompletionGateTerminationError, SkyvernContextWindowExceededError, StepTerminationError
from skyvern.forge.sdk.api.llm.exceptions import LLMProviderErrorRetryableTask
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
@@ -46,6 +47,7 @@ from skyvern.forge.taskv3.loop import (
ACTION_OUTCOME_DATA_KEY,
CLICK_DISPATCHED_DATA_KEY,
CODE_TOOL_NAME,
+ COMPLETION_GATE_REFUSAL,
FAILURE_EVIDENCE_MIN_TOOL_CALLS,
FAILURE_EVIDENCE_MIN_TURNS,
FINAL_TURN_GRANTED_EVENT,
@@ -8345,6 +8347,176 @@ async def test_completion_blocker_gates_completed_status_only() -> None:
assert outcome2.reason == "blocked reason"
+def _gate_refusals(outcome: LoopOutcome) -> int:
+ return sum(
+ 1 for m in outcome.messages if m.get("role") == "tool" and COMPLETION_GATE_REFUSAL in m.get("content", "")
+ )
+
+
+async def _async_value(value: Any) -> Any:
+ return value
+
+
+class _CountingGate:
+ """A completion gate that answers `verdict` and counts the vetoes it handed out."""
+
+ def __init__(self, verdict: bool = False) -> None:
+ self.verdict = verdict
+ self.calls = 0
+
+ async def __call__(self) -> bool:
+ self.calls += 1
+ return self.verdict
+
+
+async def test_completion_gate_veto_refuses_the_finish_and_a_later_approved_finish_completes() -> None:
+ verdicts = iter([False, True])
+
+ async def gate() -> bool:
+ return next(verdicts)
+
+ activity = ActivityRecency()
+ click_calls: list[tuple[str, dict[str, Any]]] = []
+ tools = [_billable_tool("click", click_calls), make_finish_tool(completion_gate=gate, activity=activity)]
+ script = [
+ [("finish", {"status": "completed", "reason": "looks done"})],
+ [("click", {"selector": "#submit"})],
+ [("finish", {"status": "completed", "reason": "submitted"})],
+ ]
+ outcome, _ = await _run(script, tools, activity=activity, max_action_steps=5)
+
+ assert (outcome.status, outcome.reason, outcome.gate_passed) == ("completed", "submitted", True)
+ assert click_calls == [("click", {"selector": "#submit"})]
+ assert _gate_refusals(outcome) == 1
+
+
+ "raised, status, reason",
+ [
+ (StepTerminationError("vetoed too often", step_id="stp_1"), "failed", "vetoed too often"),
+ (CompletionGateTerminationError("an earlier entry exists"), "terminated", "an earlier entry exists"),
+ ],
+ ids=["veto_budget_spent", "already_applied"],
+)
+async def test_completion_gate_raise_ends_the_run_with_the_gates_reason(
+ raised: Exception, status: str, reason: str
+) -> None:
+ async def gate() -> bool:
+ raise raised
+
+ outcome, caller = await _run(
+ [[("finish", {"status": "completed", "reason": "done"})]], [make_finish_tool(completion_gate=gate)]
+ )
+
+ assert outcome.status == status
+ assert reason in outcome.reason
+ assert not outcome.gate_passed
+ assert caller.calls == 1
+
+
[email protected]("budget", [{"max_turns": 2}, {"max_action_steps": 1}], ids=["no_turns_left", "no_steps_left"])
+async def test_completion_gate_without_headroom_leaves_the_verdict_to_the_caller(budget: dict[str, int]) -> None:
+ activity = ActivityRecency()
+ click_calls: list[tuple[str, dict[str, Any]]] = []
+ tools = [_billable_tool("click", click_calls), make_finish_tool(completion_gate=_CountingGate(), activity=activity)]
+ script = [
+ [("click", {"selector": "#submit"})],
+ [("finish", {"status": "completed", "reason": "submitted"})],
+ ]
+ outcome, _ = await _run(script, tools, activity=activity, **budget)
+
+ assert (outcome.status, outcome.gate_passed) == ("completed", False)
+ assert _gate_refusals(outcome) == 0
+
+
+async def test_completion_gate_is_skipped_when_a_click_earlier_in_the_turn_spent_the_last_action_step() -> None:
+ gate = _CountingGate()
+ activity = ActivityRecency()
+ click_calls: list[tuple[str, dict[str, Any]]] = []
+ tools = [_billable_tool("click", click_calls), make_finish_tool(completion_gate=gate, activity=activity)]
+ script = [[("click", {"selector": "#submit"}), ("finish", {"status": "completed", "reason": "submitted"})]]
+ outcome, _ = await _run(script, tools, activity=activity, max_action_steps=1)
+
+ assert (outcome.status, outcome.reason, outcome.gate_passed) == ("completed", "submitted", False)
+ assert gate.calls == 0
+
+
+async def _never_answers() -> bool:
+ await asyncio.Event().wait()
+ return True
+
+
+async def _errors() -> bool:
+ raise RuntimeError("gate bug")
+
+
[email protected]("gate", [_errors, _never_answers], ids=["gate_errors", "gate_outlives_the_deadline"])
+async def test_a_gate_that_cannot_answer_lets_the_finish_through_for_the_caller_to_decide(
+ monkeypatch: pytest.MonkeyPatch, gate: Callable[[], Awaitable[bool]]
+) -> None:
+ # The headroom floor is a minute; drop it so a sub-second deadline still lets the gate run.
+ monkeypatch.setattr(
+ loop_module,
+ "_has_hold_headroom",
+ functools.partial(loop_module._has_hold_headroom, min_deadline_headroom_seconds=0.0),
+ )
+ finish = make_finish_tool(completion_gate=gate, deadline_at=time.monotonic() + 0.3)
+ started = time.monotonic()
+ async with asyncio.timeout(5):
+ outcome, caller = await _run([[("finish", {"status": "completed", "reason": "done"})]], [finish])
+
+ assert (outcome.status, outcome.reason, outcome.gate_passed) == ("completed", "done", False)
+ assert caller.calls == 1
+ assert time.monotonic() - started < 2
+
+
[email protected]("status", ["failed", "terminated"])
+async def test_completion_gate_never_holds_a_non_completed_finish(status: str) -> None:
+ gate = _CountingGate()
+ outcome, _ = await _run(
+ [[("finish", {"status": status, "reason": "the site offers no way on"})]],
+ [make_finish_tool(completion_gate=gate)],
+ )
+
+ assert (outcome.status, outcome.reason) == (status, "the site offers no way on")
+ assert gate.calls == 0
+
+
+ "finish_kwargs, held_with",
+ [
+ (
+ {"completion_blocker": lambda _downloads: _async_value("the download has not landed")},
+ "the download has not landed",
+ ),
+ (
+ {"page_fingerprint": lambda: _async_value(str(time.monotonic_ns())), "settle_wait_seconds": 0},
+ "still rendering",
+ ),
+ ],
+ ids=["completion_blocker", "unsettled_page"],
+)
+async def test_an_earlier_completed_side_hold_answers_before_the_completion_gate_is_spent(
+ finish_kwargs: dict[str, Any], held_with: str
+) -> None:
+ gate = _CountingGate()
+ outcome, _ = await _run(
+ [[("finish", {"status": "completed", "reason": "done"})]],
+ [make_finish_tool(completion_gate=gate, **finish_kwargs)],
+ )
+
+ first_finish_result = next(m["content"] for m in outcome.messages if m.get("role") == "tool")
+ assert held_with in first_finish_result
+ assert gate.calls == 0
+
+
@pytest.mark.asyncio
async def test_completion_blocker_exception_fails_closed() -> None:
# A transient storage error checking for a landed download is evidence of nothing -- it must
diff --git a/tests/unit/test_taskv3_tools.py b/tests/unit/test_taskv3_tools.py
index eb60b05eb..76d6b4a2f 100644
--- a/tests/unit/test_taskv3_tools.py
+++ b/tests/unit/test_taskv3_tools.py
@@ -63,6 +63,7 @@ from skyvern.forge.taskv3.code_surface import (
apply_surface,
configured_surface,
)
+from skyvern.forge.taskv3.field_commit import Tier
from skyvern.forge.taskv3.loop import (
ACTION_OUTCOME_DATA_KEY,
CODE_TOOL_NAME,
@@ -137,54 +138,6 @@ def test_classify_commit_matrix() -> None:
assert _classify_commit(None, 2, S, committed_value=False) == CommitStatus.DID_NOT_COMMIT
-def test_match_menu_option_matrix() -> None:
- from skyvern.forge.taskv3.tools import _match_menu_option
-
- opts = [
- {"n": 1, "text": "Analytics"},
- {"n": 2, "text": "Engineering"},
- {"n": 3, "text": "People Operations"},
- ]
- # Exact normalized match (case/whitespace-insensitive) wins.
- assert _match_menu_option("Analytics", opts) == 1
- assert _match_menu_option(" analytics ", opts) == 1
- assert _match_menu_option("PEOPLE OPERATIONS", opts) == 3
- # FORWARD token-prefix only: a short observed value matches the fuller option label.
- assert _match_menu_option("People", opts) == 3
- eeo = [{"n": 1, "text": "Yes"}, {"n": 2, "text": "No"}, {"n": 3, "text": "Decline to self-identify"}]
- assert _match_menu_option("Decline", eeo) == 3
- # REVERSE is refused: a longer value must NOT commit a shorter, more-general option — the fuller row
- # may simply be unrendered (virtualised list), so committing "People Operations" for a "…Team" value
- # or "New" for "New York" would be a silent wrong success.
- assert _match_menu_option("People Operations Team", opts) is None
- assert _match_menu_option("New York", [{"n": 1, "text": "New"}, {"n": 2, "text": "Newark"}]) is None
- # CRITICAL: a value that is only an incidental SUBSTRING of an option is NOT matched — "No" is inside
- # "prefer not to answer" but must never commit the "No" row on a sensitive question.
- assert _match_menu_option("Prefer not to answer", eeo) is None
- # ...and an abbreviation that is not a whole-token prefix is not guessed at either.
- assert _match_menu_option("Eng", opts) is None
- # Apostrophe/quote folding comes from the shared exact/stem matcher.
- assert _match_menu_option("Masters Degree", [{"n": 1, "text": "Master's Degree"}, {"n": 2, "text": "PhD"}]) == 1
- # No match -> None (hand the options back to the model, don't guess).
- assert _match_menu_option("Legal", opts) is None
- assert _match_menu_option("", opts) is None
- # Exact wins even when a token-prefix is otherwise ambiguous: "Yes" is an exact row despite
- # "Yes, I consent" sharing its first token.
- yn = [{"n": 1, "text": "Yes"}, {"n": 2, "text": "Yes, I consent"}, {"n": 3, "text": "No"}]
- assert _match_menu_option("Yes", yn) == 1
- # Ambiguous forward-prefix with no exact match -> None, never an arbitrary pick.
- ambiguous = [{"n": 1, "text": "United States Minor"}, {"n": 2, "text": "United States Major"}]
- assert _match_menu_option("United States", ambiguous) is None
- # A row missing a usable index is ignored rather than crashing.
- assert _match_menu_option("Analytics", [{"n": None, "text": "Analytics"}, {"n": 5, "text": "Analytics"}]) == 5
- # A leading comma token is folded away, so a short value forward-prefix-matches a punctuated label
- # ("Yes" -> "Yes, I consent") instead of silently missing on the attached comma.
- punct = [{"n": 1, "text": "Yes, I consent"}, {"n": 2, "text": "No"}]
- assert _match_menu_option("Yes", punct) == 1
- # A slash is left intact so a combined single option is NOT prefix-matched by one of its halves.
- assert _match_menu_option("Yes", [{"n": 1, "text": "Yes/No"}, {"n": 2, "text": "Maybe"}]) is None
-
-
def test_annotate_screenshot_downscales_and_draws_marks() -> None:
import io as _io
@@ -32610,7 +32563,7 @@ _WRAPPED_INPUT_COMBOBOX_HTML = """
async def test_select_combobox_refuses_single_leading_clause_match_when_unique() -> None:
# The middle step of the cascade above on its own: with #state committed to "IL", "Springfield" is
# a unique city match -- but only a word-prefix of the row's full label, so it is refused rather
- # than auto-committed; the caller must supply "Springfield, Sangamon, IL".
+ # than auto-committed, and the row is offered first; the caller must supply "Springfield, Sangamon, IL".
async with _address_lookup_page() as page:
tools = build_browser_tools(_fixed_page_provider(page))
state_r = await _tool(tools, "select_combobox").handler({"selector": "#state", "value": "IL"})
@@ -32618,7 +32571,8 @@ async def test_select_combobox_refuses_single_leading_clause_match_when_unique()
city_r = await _tool(tools, "select_combobox").handler({"selector": "#city", "value": "Springfield"})
assert city_r.status == "error", city_r.content
- assert "Springfield, Sangamon, IL" in city_r.content, city_r.content
+ first_offer = city_r.content.split("nearest: ", 1)[1].split(";")[0]
+ assert "'Springfield, Sangamon, IL' (starts with it" in first_offer, city_r.content
@_skip_no_browser
@@ -33693,7 +33647,7 @@ async def test_type_ambiguity_puts_back_the_value_the_field_arrived_with() -> No
# from text this call typed and the widget never accepted.
async with _live_page(_PREFILLED_COUNTRY_FIXTURE_HTML) as page:
tools = build_browser_tools(_fixed_page_provider(page))
- r = await _tool(tools, "type").handler({"selector": "#country", "text": "United States"})
+ r = await _tool(tools, "type").handler({"selector": "#country", "text": "United"})
assert r.status == "error", r.content
assert "United States (+1)" in r.content, r.content
assert "United States Minor Outlying Islands (+1)" in r.content, r.content
@@ -34372,12 +34326,12 @@ async def test_click_on_a_container_wrapping_a_checkbox_and_a_button_gets_no_rea
@_skip_no_browser
@pytest.mark.asyncio
async def test_select_combobox_ambiguity_puts_back_the_value_the_field_arrived_with() -> None:
- # "United States" matches two rows, so the pick refuses. The field arrived holding "+1" -- a value
+ # "United" starts several rows, so the pick refuses. The field arrived holding "+1" -- a value
# the page put there -- and the refusal must hand it back: leaving the query behind replaces a real
# answer with text the widget never accepted, and a later read of the form cannot tell the two apart.
async with _live_page(_PREFILLED_COUNTRY_FIXTURE_HTML) as page:
tools = build_browser_tools(_fixed_page_provider(page))
- r = await _tool(tools, "select_combobox").handler({"selector": "#country", "value": "United States"})
+ r = await _tool(tools, "select_combobox").handler({"selector": "#country", "value": "United"})
assert r.status == "error", r.content
assert "United States (+1)" in r.content, r.content
assert "United States Minor Outlying Islands (+1)" in r.content, r.content
@@ -42656,3 +42610,334 @@ async def test_select_option_reads_a_visible_single_select_back(
assert r.status == status, r.content
if status == "error":
assert r.error_class == "did_not_commit" and "'Alpha'" in r.content, r.content
+
+
+# A phone-country picker: each declared row renders a flag, the country name and its dial code. With `virtual`, the
+# list ignores the query and renders only the window of its 300 rows the scroller shows.
+_PHONE_COUNTRY_HTML = """
+<!doctype html><html><body style="margin:0">
+ <input id="cc" type="text" role="combobox" aria-autocomplete="list" aria-controls="cc-list" autocomplete="off"
+ style="position:absolute;top:20px;left:20px;width:320px;height:24px">
+ <div id="cc-list" role="listbox"
+ style="position:absolute;top:50px;left:20px;width:320px;max-height:240px;overflow:auto;background:#fff;display:none">
+ <div id="cc-space" style="position:relative"></div>
+ </div>
+ <script>
+ var VIRTUAL = %s;
+ var ROWS = [['\\u{1F1FA}\\u{1F1F8}', 'United States', '+1'], ['\\u{1F1FA}\\u{1F1F2}', 'United States Minor Outlying Islands', '+1'],
+ ['\\u{1F1EC}\\u{1F1E7}', 'United Kingdom', '+44'], ['\\u{1F1E8}\\u{1F1E6}', 'Canada', '+1']];
+ if (VIRTUAL) { for (var i = 0; i < 296; i++) { ROWS.push(['', 'Region ' + (100 + i), '+' + (200 + i)]); } }
+ var input = document.getElementById('cc'), list = document.getElementById('cc-list');
+ var space = document.getElementById('cc-space');
+ var shown = [];
+ function row(r, top) {
+ var el = document.createElement('div');
+ el.setAttribute('role', 'option');
+ el.style.cssText = 'height:24px;cursor:pointer;' + (VIRTUAL ? 'position:absolute;left:0;right:0;top:' + top + 'px' : '');
+ el.innerHTML = '<span>' + r[0] + '</span> <span>' + r[1] + '</span> <span>' + r[2] + '</span>';
+ el.addEventListener('mousedown', function (e) { e.preventDefault(); });
+ el.addEventListener('click', function () {
+ input.value = el.innerText;
+ input.setAttribute('data-committed', r[1]);
+ list.style.display = 'none';
+ });
+ return el;
+ }
+ function render() {
+ space.innerHTML = '';
+ if (!VIRTUAL) { shown.forEach(function (r) { space.appendChild(row(r, 0)); }); return; }
+ space.style.height = (shown.length * 24) + 'px';
+ var first = Math.floor(list.scrollTop / 24);
+ shown.slice(first, first + 12).forEach(function (r, k) { space.appendChild(row(r, (first + k) * 24)); });
+ }
+ list.addEventListener('scroll', render);
+ input.addEventListener('input', function () {
+ var q = input.value.trim().toLowerCase();
+ shown = VIRTUAL ? ROWS : ROWS.filter(function (r) { return q && r[1].toLowerCase().indexOf(q) === 0; });
+ render();
+ list.style.display = q && shown.length ? 'block' : 'none';
+ });
+ </script>
+</body></html>
+"""
+
+
+@_skip_no_browser
+async def test_select_combobox_commits_a_country_row_decorated_with_its_dial_code() -> None:
+ async with _content_page(_PHONE_COUNTRY_HTML % "false") as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ with capture_logs() as logs:
+ r = await _tool(tools, "select_combobox").handler({"selector": "#cc", "value": "United States"})
+ assert r.status == "ok", r.content
+ assert await page.eval_on_selector("#cc", "el => el.getAttribute('data-committed')") == "United States"
+ match = [e for e in logs if e["event"] == "taskv3 choice match" and e["path"] == "typeahead"]
+ assert match and match[-1]["tier"] == "primary" and match[-1]["settle_ms"] > 0, match
+ (picked,) = (e for e in logs if e["event"] == "taskv3 combobox pick verdict")
+ assert picked["verdict"] == "ok" and picked["tier"] == "primary", picked
+ assert "United States" not in repr(match), match
+
+
+@_skip_no_browser
+async def test_select_combobox_never_commits_a_decorated_row_a_late_row_joins() -> None:
+ # A second "Canada" row lands between the first read and the settle's; the settle must see it, not only re-read the
+ # rows it tagged.
+ async with _content_page(_PHONE_COUNTRY_HTML % "false") as page:
+ await page.evaluate(
+ """() => document.getElementById('cc').addEventListener('input', (e) => {
+ if (!e.isTrusted || window.JOINED) return;
+ clearTimeout(window.LATE);
+ window.LATE = setTimeout(() => {
+ window.JOINED = true;
+ ROWS.push(['', 'Canada', '+2']);
+ document.getElementById('cc').dispatchEvent(new Event('input'));
+ }, 700);
+ })"""
+ )
+ tools = build_browser_tools(_fixed_page_provider(page))
+ with capture_logs() as logs:
+ r = await _tool(tools, "select_combobox").handler({"selector": "#cc", "value": "Canada"})
+ assert r.status == "error", r.content
+ assert await page.eval_on_selector("#cc", "el => el.getAttribute('data-committed')") is None
+ # The settle refused, not a later re-resolve, and the refusal lists the rows as it last tagged them.
+ reasons = [e["reason"] for e in logs if e["event"] == "taskv3 choice match" and e["path"] == "typeahead"]
+ assert reasons[0] == "unsettled", reasons
+ assert "+2" in r.content, r.content
+
+
+@_skip_no_browser
+async def test_select_combobox_with_the_nonexact_switch_off_commits_no_main_label_row(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr(settings, "TASK_V3_CHOICE_NONEXACT_COMMIT", False)
+ assert taskv3_tools._choice_tiers(typeahead=True) == frozenset({Tier.EXACT})
+ assert taskv3_tools._choice_tiers(typeahead=False) == frozenset(Tier) - {Tier.PRIMARY}
+ async with _content_page(_PHONE_COUNTRY_HTML % "false") as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "select_combobox").handler({"selector": "#cc", "value": "United States"})
+ assert r.status == "error", r.content
+ assert await page.eval_on_selector("#cc", "el => el.getAttribute('data-committed')") is None
+
+
+@_skip_no_browser
+async def test_select_combobox_offers_a_decorated_row_from_a_list_it_could_not_read_whole() -> None:
+ async with _content_page(_PHONE_COUNTRY_HTML % "true") as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "select_combobox").handler({"selector": "#cc", "value": "United States"})
+ assert r.status == "error", r.content
+ assert await page.eval_on_selector("#cc", "el => el.getAttribute('data-committed')") is None
+ near = r.content.split("nearest: ", 1)[1]
+ assert near.split(";")[0].endswith(
+ "United States +1' (its main label is it — not picked: the list may hold more rows)"
+ ), r.content
+
+
+@_skip_no_browser
+async def test_select_combobox_offers_a_decorated_row_for_a_value_with_no_word_to_overlap() -> None:
+ # "UK" has no 3-character word, so the finder admits rows by exact text alone and never sees every row that
+ # shares a word with it: the one row it found is not a complete view, whatever the list's coverage says.
+ async with _content_page(_PHONE_COUNTRY_HTML % "false") as page:
+ await page.evaluate("ROWS.push(['\\u{1F1EC}\\u{1F1E7}', 'UK', '+44'])")
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "select_combobox").handler({"selector": "#cc", "value": "UK"})
+ assert r.status == "error", r.content
+ assert await page.eval_on_selector("#cc", "el => el.getAttribute('data-committed')") is None
+ assert "UK +44' (its main label is it — not picked" in r.content, r.content
+
+
+# A click-opened list built when its trigger is clicked. `ANNOUNCE` lands beside the rows inside the popup.
+_CLICK_OPEN_LIST_HTML = """
+<!doctype html><html><body style="margin:0">
+ <button id="pick" aria-haspopup="true" style="position:absolute;top:20px;left:20px;width:260px;height:28px">Choose</button>
+ <script>
+ var ROWS = ROWS_JSON;
+ var b = document.getElementById('pick');
+ window.undone = false;
+ b.addEventListener('click', function () {
+ var old = document.getElementById('pop');
+ if (old) old.remove();
+ var pop = document.createElement('div');
+ pop.id = 'pop';
+ pop.style.cssText = 'position:absolute;top:52px;left:20px;width:260px;background:#fff';
+ pop.innerHTML = 'WRAP_OPEN<div id="opts" LIST_ATTRS></div>WRAP_CLOSE ANNOUNCE';
+ document.body.appendChild(pop);
+ var opts = document.getElementById('opts');
+ ROWS.forEach(function (t) {
+ var d = document.createElement('div');
+ if (ROW_ROLE) d.setAttribute('role', ROW_ROLE);
+ d.textContent = t;
+ d.style.cssText = 'height:24px;cursor:pointer';
+ d.addEventListener('click', function () {
+ b.textContent = t;
+ b.setAttribute('data-committed', t);
+ pop.remove();
+ });
+ opts.appendChild(d);
+ });
+ var undo = document.getElementById('undo');
+ if (undo) undo.addEventListener('click', function () { window.undone = true; });
+ });
+ </script>
+</body></html>
+"""
+
+
+def _click_open_list_html(
+ rows: list[str], *, list_attrs: str = "", wrap: str = "", announce: str = "", row_role: str = "option"
+) -> str:
+ return (
+ _CLICK_OPEN_LIST_HTML.replace("ROWS_JSON", json.dumps(rows))
+ .replace("ROW_ROLE", json.dumps(row_role))
+ .replace("LIST_ATTRS", list_attrs)
+ .replace("WRAP_OPEN", f"<div {wrap}>" if wrap else "")
+ .replace("WRAP_CLOSE", "</div>" if wrap else "")
+ .replace("ANNOUNCE", announce)
+ )
+
+
+@_skip_no_browser
+ ("rows", "value", "row"),
+ [
+ (["10023(Example Co)", "10024(Sample Group)", "20045(Other Org)"], "10023", "10023(Example Co)"),
+ (["Full-time - Permanent", "Part-time - Temporary"], "Full-time", "Full-time - Permanent"),
+ ],
+ ids=["id-name", "value-qualifier"],
+)
+async def test_select_combobox_commits_a_click_opened_row_named_by_its_main_label(
+ rows: list[str], value: str, row: str
+) -> None:
+ async with _content_page(_click_open_list_html(rows)) as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "select_combobox").handler({"selector": "#pick", "value": value})
+ assert r.status == "ok", r.content
+ assert "not an exact match" in r.content, r.content
+ assert await page.eval_on_selector("#pick", "el => el.getAttribute('data-committed')") == row
+
+
+@_skip_no_browser
+async def test_a_menu_never_lists_an_alerts_actions_as_its_rows() -> None:
+ # Undeclared rows and row-shaped actions: nothing but the announcement rule tells the toast from the menu.
+ toast = (
+ '<div role="alert"><div id="undo" style="height:24px;cursor:pointer">Undo</div>'
+ '<div style="height:24px;cursor:pointer">Dismiss</div></div>'
+ )
+ html = _click_open_list_html(["Alpha", "Beta", "Gamma"], announce=toast, row_role="")
+ async with _content_page(html) as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "select_combobox").handler({"selector": "#pick", "value": "Undo"})
+ assert r.status == "error", r.content
+ assert "Alpha" in r.content and "Dismiss" not in r.content, r.content
+ assert await page.evaluate("window.undone") is False
+
+
+@_skip_no_browser
+async def test_a_listbox_inside_a_status_region_is_still_a_menu() -> None:
+ html = _click_open_list_html(["Alpha", "Beta", "Gamma"], list_attrs='role="listbox"', wrap='role="status"')
+ async with _content_page(html) as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "select_combobox").handler({"selector": "#pick", "value": "Beta"})
+ assert r.status == "ok", r.content
+ assert await page.eval_on_selector("#pick", "el => el.getAttribute('data-committed')") == "Beta"
+
+
+_SENTENCE_ROWS_HTML = """
+<!doctype html><html><body style="margin:0">
+ <input id="auth" type="text" role="combobox" aria-autocomplete="list" aria-controls="auth-list" autocomplete="off"
+ style="position:absolute;top:20px;left:20px;width:560px;height:24px">
+ <div id="auth-list" role="listbox" style="position:absolute;top:50px;left:20px;width:560px;background:#fff"></div>
+ <script>
+ var ROWS = ROWS_JSON;
+ var input = document.getElementById('auth'), list = document.getElementById('auth-list');
+ input.addEventListener('input', function () {
+ var q = input.value.trim().toLowerCase();
+ list.innerHTML = '';
+ ROWS.filter(function (t) { return q && t.toLowerCase().indexOf(q) === 0; }).forEach(function (t) {
+ var d = document.createElement('div');
+ d.setAttribute('role', 'option');
+ d.textContent = t;
+ d.style.cssText = 'height:40px;cursor:pointer;font-size:12px';
+ d.addEventListener('mousedown', function (e) { e.preventDefault(); });
+ d.addEventListener('click', function () { input.value = t; input.setAttribute('data-committed', t); list.innerHTML = ''; });
+ list.appendChild(d);
+ });
+ });
+ </script>
+</body></html>
+"""
+
+
+@_skip_no_browser
+ ("value", "rows"),
+ [("Lawyers", ["Lawyers (Corporate)", "Lawyer"]), ("Lawyers", ["Lawyer"]), ("New", ["News", "New York"])],
+ ids=["main-label-beside-a-singular", "lone-singular", "plural-beside-a-longer-row"],
+)
+async def test_select_combobox_never_picks_a_row_that_shares_only_a_singular_or_plural(
+ value: str, rows: list[str]
+) -> None:
+ # A singular/plural row shares no whole word with the value: it reaches the matcher as a rival, never as the pick.
+ html = _SENTENCE_ROWS_HTML.replace("ROWS_JSON", json.dumps(rows)).replace(
+ "t.toLowerCase().indexOf(q) === 0", "q.length > 0"
+ )
+ async with _content_page(html) as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ with capture_logs() as logs:
+ r = await _tool(tools, "select_combobox").handler({"selector": "#auth", "value": value})
+ assert r.status == "error", r.content
+ assert await page.eval_on_selector("#auth", "el => el.getAttribute('data-committed')") is None
+ if rows == ["Lawyers (Corporate)", "Lawyer"]:
+ assert all(repr(t) in r.content for t in rows), r.content
+ tied = [e["tied"] for e in logs if e["event"] == "taskv3 choice match" and e["reason"] == "ambiguous"]
+ assert tied and tied[0] == ["primary", "stem"], tied
+
+
+@_skip_no_browser
+async def test_select_combobox_keeps_waiting_past_a_row_that_shares_only_a_singular() -> None:
+ # The singular renders first and the exact row a second later: a reaction made only of a singular/plural rival is
+ # no reaction, so the wait goes on until the value's own row arrives.
+ html = _SENTENCE_ROWS_HTML.replace("ROWS_JSON", json.dumps(["Lawyer"])).replace(
+ "t.toLowerCase().indexOf(q) === 0", "q.length > 0"
+ )
+ async with _content_page(html) as page:
+ await page.evaluate(
+ """() => document.getElementById('auth').addEventListener('input', (e) => {
+ if (!e.isTrusted || window.LATE) return;
+ window.LATE = setTimeout(() => {
+ ROWS.push('Lawyers');
+ document.getElementById('auth').dispatchEvent(new Event('input'));
+ }, 1000);
+ })"""
+ )
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "select_combobox").handler({"selector": "#auth", "value": "Lawyers"})
+ assert r.status == "ok", r.content
+ assert await page.eval_on_selector("#auth", "el => el.getAttribute('data-committed')") == "Lawyers"
+
+
+@_skip_no_browser
+async def test_select_combobox_reads_a_declared_sentence_row_longer_than_a_caption() -> None:
+ rows = [
+ "Yes, I am authorized to work in this country and will not require sponsorship now or in the future",
+ "No, I will require sponsorship to work in this country now or at some point in the future",
+ ]
+ assert all(len(t) > 80 for t in rows)
+ async with _content_page(_SENTENCE_ROWS_HTML.replace("ROWS_JSON", json.dumps(rows))) as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ with capture_logs() as logs:
+ r = await _tool(tools, "select_combobox").handler({"selector": "#auth", "value": rows[0]})
+ assert r.status == "ok", r.content
+ assert await page.eval_on_selector("#auth", "el => el.getAttribute('data-committed')") == rows[0]
+ # The full value's own read admits the row; a shorter search reaching it would be a "searched" gate.
+ gates = [e["gate"] for e in logs if e["event"] == "taskv3 choice match" and e["path"] == "typeahead"]
+ assert gates == ["overlap"], gates