Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Scale the task_v3 token backstop with the action-step budget, bounded at a hard ceiling (SKY-15262) (#8336)
pedrohsdb6f61b0d19150
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.pyindex beeb8c029..aa2ffa1af 100644--- a/skyvern/forge/agent.py+++ b/skyvern/forge/agent.py@@ -1358,6 +1358,7 @@ class ForgeAgent:from skyvern.forge.taskv3.captcha_tools import build_captcha_toolsfrom skyvern.forge.taskv3.engine import (DEFAULT_DEADLINE_SECONDS,+ MAX_TOKENS_CEILING,MIN_ACTION_STEPS,coerce_v3_parameters,run_task_v3_agent_loop,@@ -1693,7 +1694,19 @@ class ForgeAgent:)return step, taskstep_cap = min(step_cap, remaining_workflow_steps)- max_turns, max_tool_calls = taskv3_runaway_backstops(step_cap)+ max_turns, max_tool_calls, max_tokens = taskv3_runaway_backstops(step_cap)+ if max_tokens == MAX_TOKENS_CEILING:+ # A step cap large enough to hit the token ceiling silently re-caps the run's tokens; make+ # that observable so a recurrence of the flat-ceiling failure mode is found here, not by+ # canary failure analysis.+ LOG.info(+ "task_v3 token backstop clamped at its ceiling",+ log_code="taskv3_token_backstop_clamped",+ task_id=task.task_id,+ workflow_run_id=task.workflow_run_id,+ step_cap=step_cap,+ max_tokens=max_tokens,+ )# Per-action persistence (parity with the step engine): the loop hands us each successful# action round, and we persist one DB row per action + one screenshot per round, so the Task# API's action_screenshot_urls and GET /tasks/{id}/actions are populated for v3. Additive and@@ -1892,6 +1905,7 @@ class ForgeAgent:max_action_steps=step_cap,max_turns=max_turns,max_tool_calls=max_tool_calls,+ max_tokens=max_tokens,step=step,should_cancel=_should_cancel,on_action_round=_on_action_round,diff --git a/skyvern/forge/taskv3/engine.py b/skyvern/forge/taskv3/engine.pyindex 452cde258..4c05b17c2 100644--- a/skyvern/forge/taskv3/engine.py+++ b/skyvern/forge/taskv3/engine.py@@ -71,6 +71,16 @@ MAX_TOOL_CALLS_PER_ACTION_STEP = 25# 24 is the lowest cap with a measured success rate, and it clears the p95 of rounds that successful# runs actually consume (15-18) with margin for the runs a lower cap silently suppressed.MIN_ACTION_STEPS = 24+# Token need grows with the action-step budget (every extra round re-sends the transcript), so the+# token backstop scales like the turn/tool-call guards. Anchored to the action-step floor: a budget+# at or below MIN_ACTION_STEPS keeps exactly DEFAULT_MAX_TOKENS; only larger budgets rise, so a long+# block's raised step cap isn't silently nullified by the flat token ceiling.+MAX_TOKENS_PER_ACTION_STEP = DEFAULT_MAX_TOKENS // MIN_ACTION_STEPS+# The scaling clamps here: the token guard is a runaway backstop, not a budget, and a caller's step+# cap is not bounded at the route layer, so an extreme value must not carry the ceiling away with+# it. 4x covers every observed legitimate long-block need (~2x) with margin. Deliberately asymmetric:+# turns/tool-calls scale unbounded (they cost loop iterations), tokens are the direct-spend guard.+MAX_TOKENS_CEILING = 4 * DEFAULT_MAX_TOKENSPAGE_FREE_SYSTEM_PROMPT = """You are completing a data-only assessment. You have NO browser tools: do not attempt to observe or interact with any page. Judge strictly from the goal, criteria, and data provided, then call `finish(status, reason, extracted_output)` — status=completed when the completion criterion holds, status=terminated when the termination criterion holds, status=failed only if the provided information is insufficient to decide."""@@ -109,16 +119,17 @@ AUTO_OBSERVE_GUIDANCE = """When an action result already ends with an auto-observe block, act from that snapshot instead of calling observe again. If an action changes only styling or focus (hover menus, toggles), the result may say no markup change was detected — observe if you expect something new to be visible. If the next action on the same page does not depend on seeing this one's result, put it in the same turn (the button that advances the form included); type a whole value or key sequence in one `type`/`press_key`, never one character per turn."""-def taskv3_runaway_backstops(max_action_steps: int | None) -> tuple[int, int]:- """Return (max_turns, max_tool_calls) anti-runaway guards for an action-step budget.+def taskv3_runaway_backstops(max_action_steps: int | None) -> tuple[int, int, int]:+ """Return (max_turns, max_tool_calls, max_tokens) anti-runaway guards for an action-step budget.Generous enough that a productive run is bounded by max_action_steps, not by these guards; withno action-step budget, fall back to the engine's fixed defaults."""if not max_action_steps:- return DEFAULT_MAX_TURNS, DEFAULT_MAX_TOOL_CALLS+ return DEFAULT_MAX_TURNS, DEFAULT_MAX_TOOL_CALLS, DEFAULT_MAX_TOKENSreturn (max(DEFAULT_MAX_TURNS, max_action_steps * MAX_TURNS_PER_ACTION_STEP),max(DEFAULT_MAX_TOOL_CALLS, max_action_steps * MAX_TOOL_CALLS_PER_ACTION_STEP),+ min(MAX_TOKENS_CEILING, max(DEFAULT_MAX_TOKENS, max_action_steps * MAX_TOKENS_PER_ACTION_STEP)),)diff --git a/tests/unit/test_agent_task_v3.py b/tests/unit/test_agent_task_v3.pyindex 52eb92430..54a5e8aa1 100644--- a/tests/unit/test_agent_task_v3.py+++ b/tests/unit/test_agent_task_v3.py@@ -353,6 +353,39 @@ async def test_execute_task_v3_honors_explicit_step_cap_above_floor(monkeypatch:assert loop_mock.await_args.kwargs["max_action_steps"] == 30+async def test_execute_task_v3_scales_token_backstop_with_step_cap(monkeypatch: pytest.MonkeyPatch) -> None:+ # A long block's token need grows with its action-step budget: a high explicit cap must raise+ # the loop's token backstop proportionally, or the run dies at the flat ceiling mid-progress+ # while well inside its step budget.+ from skyvern.forge.taskv3.engine import DEFAULT_MAX_TOKENS++ outcome = LoopOutcome(status="completed", reason="done", billable_actions=["click"])+ _step, _task, loop_mock, _post = await _run_execute_task_v3(+ monkeypatch,+ outcome,+ max_steps_per_run=2 * MIN_ACTION_STEPS,+ data_extraction_goal=None,+ extracted_information_schema=None,+ )+ assert loop_mock.await_args.kwargs["max_action_steps"] == 2 * MIN_ACTION_STEPS+ assert loop_mock.await_args.kwargs["max_tokens"] == 2 * DEFAULT_MAX_TOKENS+++async def test_execute_task_v3_token_backstop_unchanged_at_or_below_floor(monkeypatch: pytest.MonkeyPatch) -> None:+ # At or below the action-step floor the token backstop stays at its historical default — the+ # scaling only ever raises the ceiling for budgets above the floor, never changes small blocks.+ from skyvern.forge.taskv3.engine import DEFAULT_MAX_TOKENS++ outcome = LoopOutcome(status="completed", reason="done", billable_actions=["click"])+ _step, _task, loop_mock, _post = await _run_execute_task_v3(+ monkeypatch, outcome, max_steps_per_run=7, data_extraction_goal=None, extracted_information_schema=None+ )+ assert loop_mock.await_args.kwargs["max_action_steps"] == MIN_ACTION_STEPS+ assert loop_mock.await_args.kwargs["max_tokens"] == DEFAULT_MAX_TOKENS++@pytest.mark.asyncioasync def test_execute_task_v3_surfaces_criteria_in_goal(monkeypatch: pytest.MonkeyPatch) -> None:outcome = LoopOutcome(status="completed", reason="done", billable_actions=["click"])diff --git a/tests/unit/test_taskv3_engine.py b/tests/unit/test_taskv3_engine.pyindex 1100c8881..e87b8cf43 100644--- a/tests/unit/test_taskv3_engine.py+++ b/tests/unit/test_taskv3_engine.py@@ -198,20 +198,39 @@ async def test_engine_wires_budget_and_retry_defaults(monkeypatch: pytest.Monkeydef test_runaway_backstops_scale_with_action_step_budget() -> None:# No action-step budget -> the guards are the engine's fixed defaults.- assert taskv3_runaway_backstops(None) == (DEFAULT_MAX_TURNS, DEFAULT_MAX_TOOL_CALLS)- assert taskv3_runaway_backstops(0) == (DEFAULT_MAX_TURNS, DEFAULT_MAX_TOOL_CALLS)+ defaults = (DEFAULT_MAX_TURNS, DEFAULT_MAX_TOOL_CALLS, engine_mod.DEFAULT_MAX_TOKENS)+ assert taskv3_runaway_backstops(None) == defaults+ assert taskv3_runaway_backstops(0) == defaults# Small cap: the fixed floors dominate, so a productive run keeps its historical headroom.- assert taskv3_runaway_backstops(10) == (DEFAULT_MAX_TURNS, DEFAULT_MAX_TOOL_CALLS)- # Large cap: both guards scale up so the action-step budget -- not the guards -- bounds the run.- big = 100+ assert taskv3_runaway_backstops(10) == defaults+ # Large cap: all guards scale up so the action-step budget -- not the guards -- bounds the run.+ big = 80assert taskv3_runaway_backstops(big) == (big * MAX_TURNS_PER_ACTION_STEP,big * MAX_TOOL_CALLS_PER_ACTION_STEP,+ big * engine_mod.MAX_TOKENS_PER_ACTION_STEP,)# Monotonic: a larger cap never yields smaller guards.- t_small, c_small = taskv3_runaway_backstops(20)- t_big, c_big = taskv3_runaway_backstops(80)- assert t_big >= t_small and c_big >= c_small+ t_small, c_small, k_small = taskv3_runaway_backstops(20)+ t_big, c_big, k_big = taskv3_runaway_backstops(80)+ assert t_big >= t_small and c_big >= c_small and k_big >= k_small+++def test_runaway_backstops_token_ceiling_anchored_to_the_floor() -> None:+ # The per-step token allowance is anchored so a budget at the action-step floor keeps exactly+ # the historical 1.5M ceiling; only budgets above the floor get a proportionally higher one.+ assert taskv3_runaway_backstops(engine_mod.MIN_ACTION_STEPS)[2] == engine_mod.DEFAULT_MAX_TOKENS+ assert taskv3_runaway_backstops(2 * engine_mod.MIN_ACTION_STEPS)[2] == 2 * engine_mod.DEFAULT_MAX_TOKENS+++def test_runaway_backstops_token_ceiling_is_bounded() -> None:+ # The token guard is a runaway BACKSTOP, not a budget: a caller-supplied step cap is not+ # bounded at the route layer, so an extreme value must not carry the token ceiling away+ # with it — the scaling clamps at a hard maximum.+ assert engine_mod.MAX_TOKENS_CEILING == 4 * engine_mod.DEFAULT_MAX_TOKENS+ assert taskv3_runaway_backstops(5000)[2] == engine_mod.MAX_TOKENS_CEILING+ # Turn/tool-call guards keep their pre-existing proportional scaling.+ assert taskv3_runaway_backstops(5000)[0] == 5000 * MAX_TURNS_PER_ACTION_STEP@pytest.mark.asyncio