Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Refuse every fill tool in a Task V3 extraction block (SKY-16398) (#8575)
pedrohsdb026c3c001787
diff --git a/skyvern/forge/taskv3/engine.py b/skyvern/forge/taskv3/engine.pyindex 27813a51a..ab101cdec 100644--- a/skyvern/forge/taskv3/engine.py+++ b/skyvern/forge/taskv3/engine.py@@ -58,6 +58,7 @@ from skyvern.forge.taskv3.tools import (apply_blank_page_guard,build_browser_tools,)+from skyvern.schemas.workflows import BlockTypeLOG = structlog.get_logger()@@ -405,6 +406,7 @@ async def run_task_v3_agent_loop(final_turn_token_reserve=MAX_TOKENS_PER_ACTION_STEP,backstops_for_cap=taskv3_runaway_backstops,semantic_commit_stats=semantic_commit_stats,+ refuse_input_entry=block_type == BlockType.EXTRACTION,)finally:# The context outlives this run; a signal raised as the loop was cancelled must not firediff --git a/skyvern/forge/taskv3/loop.py b/skyvern/forge/taskv3/loop.pyindex 72f321b01..fd6fc4c1f 100644--- a/skyvern/forge/taskv3/loop.py+++ b/skyvern/forge/taskv3/loop.py@@ -515,6 +515,10 @@ ACTION_BUDGET_EXTENSION_MAX_FACTOR = 3# precision is measurable on the canary; change only with the dashboards that read them.ACTION_BUDGET_EXTENDED_EVENT = "taskv3 loop action budget extended"ACTION_BUDGET_EXTENSION_REFUSED_EVENT = "taskv3 loop action budget extension refused"+EXTRACTION_ENTRY_REFUSED_EVENT = "taskv3 loop extraction entry refused"+# Every tool that authors input on the page. Defined here rather than in tools.py because tools.py imports+# this module; the extraction-block refusal and tools.py's frame-work ledger both read this one set.+FILL_TOOLS = frozenset({"type", "select_option", "select_combobox", "file_upload"})# A wrap-up turn granted by a guard that a later budget extension raised past its trip; facetable# so a released latch is distinguishable from one that never fired.FINAL_TURN_RELEASED_EVENT = "taskv3 loop final turn grant released by budget extension"@@ -2190,6 +2194,9 @@ async def run_agent_tool_loop(# step-cap death into a token-cap death. None keeps the guards fixed for the whole run.backstops_for_cap: Callable[[int], tuple[int, int, int]] | None = None,semantic_commit_stats: SemanticCommitStats | None = None,+ # Set for an extraction block: it reads, and may click to reveal what it reads, but it does not+ # author input, so every FILL_TOOLS call is refused at dispatch.+ refuse_input_entry: bool = False,) -> LoopOutcome:tool_by_name = {tool.name: tool for tool in tools}st = LoopState(@@ -2805,6 +2812,24 @@ async def run_agent_tool_loop(})continue+ if refuse_input_entry and tool_name in FILL_TOOLS:+ LOG.info(EXTRACTION_ENTRY_REFUSED_EVENT, tool=tool_name, turn=st.turns)+ # A refused call did not do what the rest of the batch was planned around, so it marks the+ # batch failed: a later click, Enter-shaped submit, or finish in the same batch is skipped.+ batch_had_failure = True+ st.messages.append(+ {+ "role": "tool",+ "tool_call_id": tool_call_id,+ "name": tool_name,+ "content": (+ "refused: an extraction block does not type, select, or upload input. Extract what the "+ "page shows now (clicking to reveal content is allowed), or finish with a status "+ "that reflects it"+ ),+ }+ )+ continueif batch_had_failure and _may_submit(tool_name, args):st.messages.append({diff --git a/skyvern/forge/taskv3/tools.py b/skyvern/forge/taskv3/tools.pyindex 10b5a7e27..0e018ab56 100644--- a/skyvern/forge/taskv3/tools.py+++ b/skyvern/forge/taskv3/tools.py@@ -46,6 +46,7 @@ from skyvern.forge.sdk.core import skyvern_contextfrom skyvern.forge.sdk.core.skyvern_context import URL_IN_TEXT, canonical_url, opaque_url_echo_windowfrom skyvern.forge.taskv3.frame_perception import frame_perception_enabledfrom skyvern.forge.taskv3.loop import (+ FILL_TOOLS,NAVIGATION_DEAD_END_STATUSES,PAGE_UNAVAILABLE_ERROR,REF_SELECTOR_RE,@@ -12436,7 +12437,6 @@ def build_browser_tools(# Which tools leave work a reload would discard, and which leave something that may still be in# flight. The loop decides what counts as a submit; this only records WHERE the click landed.- _LEDGER_FILL_TOOLS = frozenset({"type", "select_option", "select_combobox", "file_upload"})_LEDGER_SUBMIT_TOOLS = frozenset({"click", "press_key"})async def _note_frame_work(tool_name: str, realm: Any, selector: Any, result: ToolResult) -> None:@@ -12447,7 +12447,7 @@ def build_browser_tools("""if realm is None or not isinstance(selector, str) or not selector:return- kind = "filled" if tool_name in _LEDGER_FILL_TOOLS else "submitted" if tool_name in _LEDGER_SUBMIT_TOOLS else ""+ kind = "filled" if tool_name in FILL_TOOLS else "submitted" if tool_name in _LEDGER_SUBMIT_TOOLS else ""if not kind:return# A FILL is recorded whether or not the VERDICT was ok, because mutating the control anddiff --git a/tests/unit/test_taskv3_engine.py b/tests/unit/test_taskv3_engine.pyindex 3c4bf68d1..5555230aa 100644--- a/tests/unit/test_taskv3_engine.py+++ b/tests/unit/test_taskv3_engine.py@@ -1374,6 +1374,26 @@ async def test_terminal_log_carries_duration_and_block_type() -> None:assert terminal[0]["block_type"] is None # bare task: no block context+ ("block_type", "refused_count"), [("extraction", 1), ("task", 0), ("navigation", 0), (None, 0)]+)+async def test_entry_is_refused_only_in_an_extraction_block(block_type: str | None, refused_count: int) -> None:+ script = [+ [("type", {"selector": "#q", "text": "Jane Doe"})],+ [("finish", {"status": "completed", "reason": "ok"})],+ ]+ with capture_logs() as logs:+ await run_task_v3_agent_loop(+ page_provider=_fixed_page_provider(_FakePage()),+ llm_caller=_ScriptedCaller(script),+ goal="x",+ block_type=block_type,+ )+ refused = [e for e in logs if e.get("event") == "taskv3 loop extraction entry refused"]+ assert len(refused) == refused_count++@pytest.mark.asyncioasync def test_terminal_log_carries_loop_telemetry_and_the_two_terminal_records_stay_dead() -> None:# The only test that can catch the loop telemetry silently dropping off this line, or eitherdiff --git a/tests/unit/test_taskv3_loop.py b/tests/unit/test_taskv3_loop.pyindex 40f6e832f..65e782fb7 100644--- a/tests/unit/test_taskv3_loop.py+++ b/tests/unit/test_taskv3_loop.py@@ -10073,3 +10073,61 @@ async def test_page_authored_notice_shaped_text_never_folds_into_a_frozen_verdicassert not (outcome.reason or "").startswith(PERCEPTION_STALL_REASON_PREFIX), outcome.reasonassert outcome.status == "completed", (outcome.status, outcome.reason)+++_FILL_CALLS = {+ "type": {"selector": "#q", "text": "Jane Doe"},+ "select_option": {"selector": "#s", "value": "a"},+ "select_combobox": {"selector": "#c", "value": "a"},+ "file_upload": {"selector": "#f", "file_url": "https://example.com/a.pdf"},+}++[email protected]("refuse", [True, False])[email protected]("fill_tool", sorted(_FILL_CALLS))+async def test_extraction_entry_guard_refuses_every_fill_tool_but_not_click(fill_tool: str, refuse: bool) -> None:+ assert set(_FILL_CALLS) == loop_module.FILL_TOOLS+ calls: list[tuple[str, dict[str, Any]]] = []+ tools = [_billable_tool(fill_tool, calls), _billable_tool("click", calls), make_finish_tool()]+ script = [+ [(fill_tool, _FILL_CALLS[fill_tool])],+ [("click", {"selector": "#tab"})],+ [("finish", {"status": "completed", "reason": "read"})],+ ]+ with capture_logs() as logs:+ outcome, caller = await _run(script, tools, refuse_input_entry=refuse)++ assert outcome.status == "completed"+ dispatched = [name for name, _ in calls]+ refusals = [e for e in logs if e["event"] == loop_module.EXTRACTION_ENTRY_REFUSED_EVENT]+ if refuse:+ assert dispatched == ["click"]+ assert [e["tool"] for e in refusals] == [fill_tool]+ fill_results = [m for m in caller.message_history if m.get("role") == "tool" and m["name"] == fill_tool]+ assert [m["content"].split(":")[0] for m in fill_results] == ["refused"]+ else:+ assert dispatched == [fill_tool, "click"]+ assert refusals == []++[email protected]("fill_tool", sorted(_FILL_CALLS))+async def test_refused_fill_call_stops_the_rest_of_its_batch(fill_tool: str) -> None:+ # A completed verdict batched behind a refused fill was written as if the fill had landed.+ calls: list[tuple[str, dict[str, Any]]] = []+ tools = [_billable_tool(fill_tool, calls), _billable_tool("click", calls), make_finish_tool()]+ script = [+ [+ (fill_tool, _FILL_CALLS[fill_tool]),+ ("click", {"selector": "#search"}),+ ("finish", {"status": "completed", "reason": "searched and read"}),+ ],+ [("finish", {"status": "failed", "reason": "the value is not on the page"})],+ ]+ outcome, caller = await _run(script, tools, refuse_input_entry=True)++ assert calls == []+ assert outcome.status == "failed", outcome.reason+ batch_results = [m["content"].split(":")[0] for m in caller.message_history if m.get("role") == "tool"][:3]+ assert batch_results == ["refused", "skipped", "skipped"]