Skip to content

Automate browser based workflows with AI

fix(SKY-9565): relax late block-running guard (#5884)

Andrew Neilson028b90594439
diff --git a/skyvern/forge/sdk/copilot/tools.py b/skyvern/forge/sdk/copilot/tools.py
index 262cee1db..085a8d73e 100644
--- a/skyvern/forge/sdk/copilot/tools.py
+++ b/skyvern/forge/sdk/copilot/tools.py
@@ -114,7 +114,8 @@ PER_TOOL_CALL_BUDGET_SECONDS = 240
# false-positives on healthy runs.
RUN_BLOCKS_STAGNATION_WINDOW_SECONDS = 90
-MIN_BLOCK_RUNNING_REMAINING_SECONDS = PER_TOOL_CALL_BUDGET_SECONDS + RUN_BLOCKS_STAGNATION_WINDOW_SECONDS
+# Reserve final-reply room; active block runs shrink their own budget near the deadline.
+COPILOT_FINAL_REPLY_RESERVE_SECONDS = 90
# 5 s balances responsiveness (18 samples inside the stagnation window) against
# DB load (240 polls worst case at the safety ceiling).
@@ -398,9 +399,17 @@ def _copilot_seconds_remaining(ctx: AgentContext) -> float | None:
return TOTAL_TIMEOUT_SECONDS - (time.monotonic() - float(started_at))
+def _active_block_run_budget_seconds(ctx: AgentContext) -> int:
+ remaining = _copilot_seconds_remaining(ctx)
+ if remaining is None:
+ return PER_TOOL_CALL_BUDGET_SECONDS
+ remaining_after_reply_reserve = remaining - COPILOT_FINAL_REPLY_RESERVE_SECONDS
+ return max(1, min(PER_TOOL_CALL_BUDGET_SECONDS, int(remaining_after_reply_reserve)))
+
+
def _late_block_running_call_error(ctx: AgentContext) -> str | None:
remaining = _copilot_seconds_remaining(ctx)
- if remaining is None or remaining >= MIN_BLOCK_RUNNING_REMAINING_SECONDS:
+ if remaining is None or remaining > COPILOT_FINAL_REPLY_RESERVE_SECONDS:
return None
last_failed_workflow_yaml = getattr(ctx, "last_failed_workflow_yaml", None)
@@ -420,13 +429,13 @@ def _late_block_running_call_error(ctx: AgentContext) -> str | None:
if isinstance(last_failed_workflow_yaml, str) and last_failed_workflow_yaml:
return (
- f"Less than {MIN_BLOCK_RUNNING_REMAINING_SECONDS} seconds remain in this Copilot turn "
+ f"Less than {COPILOT_FINAL_REPLY_RESERVE_SECONDS} seconds remain in this Copilot turn "
"after the previous workflow run failed. Do NOT retry block-running tools; reply to the user "
"with the failure evidence gathered so far and make clear that any draft workflow is unverified."
)
return (
- f"Less than {MIN_BLOCK_RUNNING_REMAINING_SECONDS} seconds remain in this Copilot turn. "
+ f"Less than {COPILOT_FINAL_REPLY_RESERVE_SECONDS} seconds remain in this Copilot turn. "
"Do NOT start another block-running tool call; reply to the user with the workflow draft and "
"progress gathered so far, and make clear which parts have not been verified end-to-end."
)
@@ -1595,7 +1604,7 @@ async def _run_blocks_and_collect_debug(
# indefinitely.
budget_exit_reason: WatchdogExitReason
if stagnation_enabled:
- budget_seconds = max(1, PER_TOOL_CALL_BUDGET_SECONDS)
+ budget_seconds = _active_block_run_budget_seconds(ctx)
budget_exit_reason = "per_tool_budget"
else:
budget_seconds = max(1, RUN_BLOCKS_SAFETY_CEILING_SECONDS - 10)
diff --git a/tests/unit/test_copilot_mid_correction_guard.py b/tests/unit/test_copilot_mid_correction_guard.py
index 7325592bb..f102b71c8 100644
--- a/tests/unit/test_copilot_mid_correction_guard.py
+++ b/tests/unit/test_copilot_mid_correction_guard.py
@@ -14,8 +14,7 @@ from skyvern.forge.sdk.copilot.agent import (
from skyvern.forge.sdk.copilot.context import CopilotContext
from skyvern.forge.sdk.copilot.enforcement import TOTAL_TIMEOUT_SECONDS
from skyvern.forge.sdk.copilot.tools import (
- MIN_BLOCK_RUNNING_REMAINING_SECONDS,
- PER_TOOL_CALL_BUDGET_SECONDS,
+ COPILOT_FINAL_REPLY_RESERVE_SECONDS,
_record_run_blocks_result,
_tool_loop_error,
)
@@ -45,7 +44,7 @@ def _ctx_after_failure_with_verified_prefix(*, elapsed_seconds: float) -> Copilo
def test_tool_loop_error_blocks_run_when_budget_low_after_failure() -> None:
- elapsed = TOTAL_TIMEOUT_SECONDS - (PER_TOOL_CALL_BUDGET_SECONDS - 30)
+ elapsed = TOTAL_TIMEOUT_SECONDS - (COPILOT_FINAL_REPLY_RESERVE_SECONDS - 10)
ctx = _ctx_after_failure_with_verified_prefix(elapsed_seconds=elapsed)
for tool in ("update_and_run_blocks", "run_blocks_and_collect_debug"):
msg = _tool_loop_error(ctx, tool)
@@ -54,7 +53,7 @@ def test_tool_loop_error_blocks_run_when_budget_low_after_failure() -> None:
def test_tool_loop_error_no_guard_when_budget_sufficient() -> None:
ctx = _ctx_after_failure_with_verified_prefix(
- elapsed_seconds=TOTAL_TIMEOUT_SECONDS - (MIN_BLOCK_RUNNING_REMAINING_SECONDS + 30)
+ elapsed_seconds=TOTAL_TIMEOUT_SECONDS - (COPILOT_FINAL_REPLY_RESERVE_SECONDS + 30)
)
assert _tool_loop_error(ctx, "update_and_run_blocks") is None
@@ -78,7 +77,7 @@ def test_tool_loop_error_blocks_failed_retry_when_no_good_workflow_exists() -> N
def test_tool_loop_error_no_guard_for_non_block_running_tools() -> None:
- elapsed = TOTAL_TIMEOUT_SECONDS - (PER_TOOL_CALL_BUDGET_SECONDS - 30)
+ elapsed = TOTAL_TIMEOUT_SECONDS - (COPILOT_FINAL_REPLY_RESERVE_SECONDS - 10)
ctx = _ctx_after_failure_with_verified_prefix(elapsed_seconds=elapsed)
for tool in ("update_workflow", "list_credentials", "get_run_results"):
assert _tool_loop_error(ctx, tool) is None, tool
@@ -87,7 +86,7 @@ def test_tool_loop_error_no_guard_for_non_block_running_tools() -> None:
def test_tool_loop_error_guard_persists_through_update_workflow() -> None:
# ``last_test_ok`` flips to None on every ``update_workflow``, but
# ``last_failed_workflow_yaml`` stays — so the guard must still fire.
- elapsed = TOTAL_TIMEOUT_SECONDS - (PER_TOOL_CALL_BUDGET_SECONDS - 30)
+ elapsed = TOTAL_TIMEOUT_SECONDS - (COPILOT_FINAL_REPLY_RESERVE_SECONDS - 10)
ctx = _ctx_after_failure_with_verified_prefix(elapsed_seconds=elapsed)
ctx.last_test_ok = None
ctx.last_workflow = SimpleNamespace(workflow_id="wf-edited")
diff --git a/tests/unit/test_copilot_tool_loop_error_block_running.py b/tests/unit/test_copilot_tool_loop_error_block_running.py
index 8fed6e8a3..ec70ecae7 100644
--- a/tests/unit/test_copilot_tool_loop_error_block_running.py
+++ b/tests/unit/test_copilot_tool_loop_error_block_running.py
@@ -6,7 +6,14 @@ import time
from types import SimpleNamespace
from typing import Any
-from skyvern.forge.sdk.copilot.tools import BLOCK_RUNNING_TOOLS, _tool_loop_error
+from skyvern.forge.sdk.copilot.enforcement import TOTAL_TIMEOUT_SECONDS
+from skyvern.forge.sdk.copilot.tools import (
+ BLOCK_RUNNING_TOOLS,
+ COPILOT_FINAL_REPLY_RESERVE_SECONDS,
+ PER_TOOL_CALL_BUDGET_SECONDS,
+ _active_block_run_budget_seconds,
+ _tool_loop_error,
+)
def _ctx(**overrides: Any) -> Any:
@@ -63,36 +70,32 @@ def test_block_running_tool_blocks_late_retry_after_failed_workflow() -> None:
msg = _tool_loop_error(ctx, "update_and_run_blocks")
assert msg is not None
- assert "less than 330 seconds" in msg.lower()
+ assert "less than 90 seconds" in msg.lower()
assert "Do NOT retry" in msg
assert "reply to the user" in msg
assert "any draft workflow is unverified" in msg
-def test_late_retry_guard_requires_reply_room_after_tool_budget() -> None:
+def test_late_retry_guard_allows_latter_half_calls_when_reply_room_remains() -> None:
ctx = _ctx(
last_failed_workflow_yaml="version: '1.0'",
- copilot_run_start_monotonic=time.monotonic() - 271,
+ copilot_run_start_monotonic=time.monotonic() - 300,
)
- msg = _tool_loop_error(ctx, "run_blocks_and_collect_debug")
-
- assert msg is not None
- assert "less than 330 seconds" in msg.lower()
- assert "Do NOT retry" in msg
+ assert _tool_loop_error(ctx, "run_blocks_and_collect_debug") is None
def test_block_running_tool_blocks_late_continuation_after_successful_prefix() -> None:
ctx = _ctx(
last_workflow_yaml="version: '1.0'",
last_test_ok=True,
- copilot_run_start_monotonic=time.monotonic() - 420,
+ copilot_run_start_monotonic=time.monotonic() - 540,
)
msg = _tool_loop_error(ctx, "update_and_run_blocks")
assert msg is not None
- assert "less than 330 seconds" in msg.lower()
+ assert "less than 90 seconds" in msg.lower()
assert "Do NOT start another block-running tool call" in msg
assert "workflow draft and progress gathered so far" in msg
assert "not been verified end-to-end" in msg
@@ -107,6 +110,21 @@ def test_late_retry_guard_waits_until_budget_is_low() -> None:
assert _tool_loop_error(ctx, "update_and_run_blocks") is None
+def test_active_block_run_budget_shrinks_near_deadline() -> None:
+ remaining = COPILOT_FINAL_REPLY_RESERVE_SECONDS + 30
+ ctx = _ctx(copilot_run_start_monotonic=time.monotonic() - (TOTAL_TIMEOUT_SECONDS - remaining))
+
+ budget = _active_block_run_budget_seconds(ctx)
+ assert 25 <= budget <= 30
+
+
+def test_active_block_run_budget_uses_full_budget_when_deadline_is_distant() -> None:
+ remaining = COPILOT_FINAL_REPLY_RESERVE_SECONDS + PER_TOOL_CALL_BUDGET_SECONDS + 30
+ ctx = _ctx(copilot_run_start_monotonic=time.monotonic() - (TOTAL_TIMEOUT_SECONDS - remaining))
+
+ assert _active_block_run_budget_seconds(ctx) == PER_TOOL_CALL_BUDGET_SECONDS
+
+
def test_late_retry_guard_is_scoped_to_block_running_tools() -> None:
ctx = _ctx(
last_failed_workflow_yaml="version: '1.0'",