Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Revert the one-look Task V3 terminated-verdict hold (SKY-18113) (#8858)
Aaron Perez7ba6bcb4aacb
diff --git a/skyvern/forge/taskv3/loop.py b/skyvern/forge/taskv3/loop.pyindex 667a59c93..5a762baf7 100644--- a/skyvern/forge/taskv3/loop.py+++ b/skyvern/forge/taskv3/loop.py@@ -1335,11 +1335,11 @@ class _ProgressLedger:self.peak_actions_since_progress = max(self.peak_actions_since_progress, self.actions_since_progress)-# Failure-evidence gate: finish(failed) and finish(terminated) shortly after a submit-class action or a-# solve_captcha attempt are held for ONE evidence turn per fresh trigger, because submissions and-# captcha protocols complete asynchronously — the sampled false-negative verdicts fired 2-7s after-# the model's last look while the page went on to show the submission confirmation. Trigger tools-# are the ones whose page effects can land after their tool result; the window is in loop turns so intervening+# Failure-evidence gate: a finish(failed) issued shortly after a submit-class action or a+# solve_captcha attempt is held for ONE evidence turn, because submissions and captcha protocols+# complete asynchronously — the sampled false-negative verdicts fired 2-7s after the model's last+# look while the page went on to show the submission confirmation. Trigger tools are the ones whose+# page effects can land after their tool result; the window is in loop turns so intervening# perception does NOT disarm it (the state can flip after the last observe while a protocol is in# flight). The true verdict-to-flip latency is unmeasured in the sampled replays: the quiescence# wait exits on the first stable fingerprint pair (so honest gated failures pay ~one sample), the@@ -1562,8 +1562,6 @@ class ActivityRecency:tokens_remaining: int | None = Nonelast_turn_tokens: int = 0last_trigger_turn: int | None = None- # Trigger actions can occur more than once in a model turn.- failure_evidence_trigger_generation: int = 0# True while one more read of some probe, returning what it last returned, would trip the stall# terminator: a deferral-forced observe must never be the snapshot that trips it. KNOWN LIMIT: a# run that reaches the edge and then stops reading that tool altogether leaves this true for the@@ -2459,9 +2457,9 @@ def make_finish_tool(is capped at `deadline_at` (time.monotonic clock) and abandoned once `should_cancel` reportsTrue, so probing cannot outlive the loop's own bounds.- The symmetric non-completed side: when `activity` reports recent submit-class/captcha activity,- finish(failed) and finish(terminated) are held for ONE evidence turn (`max_failure_deferrals` per- fresh trigger, not per run or verdict attempt) — a quiescence wait+ The symmetric failure side: when `activity` reports recent submit-class/captcha activity, a+ finish(failed) is held for ONE evidence turn (`max_failure_deferrals`, per run like the+ completed-side cap, not per verdict attempt) — a quiescence waitbounded by `failure_settle_max_seconds`, then a deferral asking the model to re-observe —because async submissions and captcha protocols otherwise produce false-negative verdicts.@@ -2470,8 +2468,8 @@ def make_finish_tool(run is still awaiting a code it has unspent polling budget for -- a give-up at one 120s slice ofa 15-minute budget throws away minutes of waiting the run already owns. The hold is bounded bythe callee (the budget shrinks under every productive hold) and refused here without the deadline- headroom to fund the blocking poll slice it asks for. Terminated verdicts otherwise follow the- same evidence gate as failed verdicts.+ headroom to fund the blocking poll slice it asks for. Apart from that gate, terminated is+ ungated on both sides.`pending_marker` reports the text the page still shows the control in `submit_watch` as inflight with, or None. A settled page is not a submitted one -- a submit frozen mid-flight is@@ -2489,7 +2487,6 @@ def make_finish_tool(the settle window, and a conversion whose identity changed or could not be read is refused, settled or not."""deferrals = 0failure_deferrals = 0- failure_deferral_trigger_generation: int | None = Nonereask_asked = Falseasync def _bounded_fingerprint() -> str | None:@@ -2652,15 +2649,12 @@ def make_finish_tool()async def handler(args: dict[str, Any]) -> ToolResult:- nonlocal deferrals, failure_deferrals, failure_deferral_trigger_generation, reask_asked+ nonlocal deferrals, failure_deferrals, reask_askedstatus = args.get("status")if status not in ("completed", "failed", "terminated"):return ToolResult.error(f"invalid finish status: {status!r}; call finish again with status=completed|failed|terminated")- if activity is not None and activity.failure_evidence_trigger_generation != failure_deferral_trigger_generation:- failure_deferrals = 0- failure_deferral_trigger_generation = activity.failure_evidence_trigger_generationif (status == "completed"and pending_marker is not None@@ -2758,7 +2752,7 @@ def make_finish_tool("report failure.")if (- status in ("failed", "terminated")+ status == "failed"and activity is not Noneand page_fingerprint is not Noneand failure_deferrals < max_failure_deferrals@@ -2794,9 +2788,9 @@ def make_finish_tool(pass # unknown page state still defers: the model's re-observe is the evidence stepif should_defer:failure_deferrals += 1- LOG.info("taskv3 finish failure deferred for evidence", status=status, turn=activity.turn)+ LOG.info("taskv3 finish failure deferred for evidence", turn=activity.turn)return ToolResult.error(- "non-completed verdict held for one evidence check: it follows recent page actions or "+ "failure verdict held for one evidence check: it follows recent page actions or ""a captcha attempt whose effects can land after your last look — submissions and ""captcha protocols often complete asynchronously, so the page may no longer show ""the state this verdict was based on. Re-observe the page once (waiting briefly "@@ -4978,7 +4972,6 @@ async def run_agent_tool_loop(st.block_action_transitioned = Trueif activity is not None and _arms_failure_evidence(tool_name, args, result.status == "ok"):activity.last_trigger_turn = st.turns- activity.failure_evidence_trigger_generation += 1if submit_watch is not None:submit_selector = _names_submit_control(tool_name, args, result.status == "ok")if submit_selector is not None:diff --git a/tests/unit/test_taskv3_loop.py b/tests/unit/test_taskv3_loop.pyindex 3dbd74052..b6273f1e0 100644--- a/tests/unit/test_taskv3_loop.py+++ b/tests/unit/test_taskv3_loop.py@@ -6346,10 +6346,9 @@ async def test_dead_posting_real_trace_shape_gains_at_most_one_observe() -> None@pytest.mark.asyncio[email protected]("status", ["failed", "terminated"])-async def test_finish_without_recent_trigger_is_not_gated(status: str) -> None:- # A non-completed verdict with no recent submit-class or captcha activity (missing input data,- # dead page never interacted with) needs no page evidence: accepted immediately, page never sampled.+async def test_finish_failed_without_recent_trigger_is_not_gated() -> None:+ # A failure with no recent submit-class or captcha activity (missing input data, dead page+ # never interacted with) needs no page evidence: accepted immediately, page never sampled.activity = ActivityRecency()fingerprint, fp_calls = _fingerprint_seq(["fp"])tools = [@@ -6358,10 +6357,10 @@ async def test_finish_without_recent_trigger_is_not_gated(status: str) -> None:]script = [[("observe", {})],- [("finish", {"status": status, "reason": "the posting does not exist"})],+ [("finish", {"status": "failed", "reason": "the posting does not exist"})],]outcome, _ = await _run(script, tools, activity=activity)- assert outcome.status == status+ assert outcome.status == "failed"assert outcome.turns == 2assert fp_calls["n"] == 0@@ -6506,9 +6505,9 @@ async def test_failure_settle_wait_is_bounded_on_a_never_quiet_page() -> None:@pytest.mark.asyncio-async def test_finish_terminated_after_submit_click_defers_for_evidence_then_stands() -> None:- # A terminated verdict after a submit-shaped click uses the same one-turn evidence check as a- # failed verdict: a re-observe is required before the page-blocked reason can stand.+async def test_finish_terminated_after_submit_click_stands_without_a_relook() -> None:+ # terminate_criterion verdicts stay cheap: terminated never consults the page, even with+ # submit activity in the window.activity = ActivityRecency()fingerprint, fp_calls = _fingerprint_seq(["fp"])clicks: list[tuple[str, dict[str, Any]]] = []@@ -6525,75 +6524,9 @@ async def test_finish_terminated_after_submit_click_defers_for_evidence_then_sta]outcome, _ = await _run(script, tools, activity=activity)assert outcome.status == "terminated"- assert outcome.reason == "submission still rejected after re-observe"- deferrals = [- m- for m in outcome.messages- if m.get("role") == "tool" and "held for one evidence check" in str(m.get("content"))- ]- assert len(deferrals) == 1- assert fp_calls["n"] == 1---async def test_held_terminated_verdict_can_complete_after_confirmation_is_observed() -> None:- activity = ActivityRecency()- fingerprint, fp_calls = _fingerprint_seq(["before-confirmation", "confirmation", "confirmation"])- clicks: list[tuple[str, dict[str, Any]]] = []- tools = [- _billable_tool("click", clicks),- _perception_tool("observe", "url=x text: 'Submission confirmation received.'"),- make_finish_tool(page_fingerprint=fingerprint, activity=activity, settle_wait_seconds=0.0),- ]- script = [- [("click", {"selector": "#btn-submit"})],- [("finish", {"status": "terminated", "reason": "submission still processing"})],- [("observe", {})],- [("finish", {"status": "completed", "reason": "confirmation visible"})],- ]-- with capture_logs() as logs:- outcome, _ = await _run(script, tools, activity=activity)-- assert outcome.status == "completed"- assert outcome.reason == "confirmation visible"- assert fp_calls["n"] == 3- held = next(log for log in logs if log["event"] == "taskv3 finish failure deferred for evidence")- assert held["status"] == "terminated"---async def test_failure_evidence_budget_rearms_after_new_submit_activity() -> None:- # A previous held verdict must not spend the evidence check for a later submit attempt.- activity = ActivityRecency()- fingerprint, fp_calls = _fingerprint_seq(["fp"])- clicks: list[tuple[str, dict[str, Any]]] = []- tools = [- _billable_tool("click", clicks),- _perception_tool("observe", _REJECTION_OBSERVE),- make_finish_tool(page_fingerprint=fingerprint, activity=activity, settle_wait_seconds=0.0),- ]- script = [- [("click", {"selector": "#first-submit"})],- [("finish", {"status": "failed", "reason": "first submission rejected"})],- [("observe", {})],- [("click", {"selector": "#retry-submit"})],- [("finish", {"status": "terminated", "reason": "retry still processing"})],- [("observe", {})],- [("finish", {"status": "terminated", "reason": "retry rejected after re-observe"})],- ]-- outcome, _ = await _run(script, tools, activity=activity)-- assert outcome.status == "terminated"- assert outcome.reason == "retry rejected after re-observe"- deferrals = [- m- for m in outcome.messages- if m.get("role") == "tool" and "held for one evidence check" in str(m.get("content"))- ]- assert len(deferrals) == 2- assert fp_calls["n"] == 2+ assert outcome.reason == "submission rejected"+ assert outcome.turns == 2+ assert fp_calls["n"] == 0@pytest.mark.asyncio