Skip to content

Automate browser based workflows with AI

fix(SKY-9565): block late Copilot block-running attempts (#5880)

Andrew Neilsone1c259331610
diff --git a/skyvern/forge/sdk/copilot/tools.py b/skyvern/forge/sdk/copilot/tools.py
index 7c5f1668c..cee10ce0d 100644
--- a/skyvern/forge/sdk/copilot/tools.py
+++ b/skyvern/forge/sdk/copilot/tools.py
@@ -113,6 +113,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
+
# 5 s balances responsiveness (18 samples inside the stagnation window) against
# DB load (240 polls worst case at the safety ceiling).
RUN_BLOCKS_POLL_INTERVAL_SECONDS = 5.0
@@ -388,6 +390,47 @@ async def _attach_failed_block_screenshots(
BLOCK_RUNNING_TOOLS = frozenset({"run_blocks_and_collect_debug", "update_and_run_blocks"})
+def _copilot_seconds_remaining(ctx: AgentContext) -> float | None:
+ started_at = getattr(ctx, "copilot_run_start_monotonic", None)
+ if not isinstance(started_at, int | float):
+ return None
+ return TOTAL_TIMEOUT_SECONDS - (time.monotonic() - float(started_at))
+
+
+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:
+ return None
+
+ last_failed_workflow_yaml = getattr(ctx, "last_failed_workflow_yaml", None)
+ last_good_workflow_yaml = getattr(ctx, "last_good_workflow_yaml", None)
+ if (
+ isinstance(last_failed_workflow_yaml, str)
+ and last_failed_workflow_yaml
+ and isinstance(last_good_workflow_yaml, str)
+ and last_good_workflow_yaml
+ ):
+ return (
+ f"Wall-clock budget too low to retry: about {int(max(0.0, remaining))}s remain of the "
+ f"{TOTAL_TIMEOUT_SECONDS}s session budget. A verified workflow exists from before the failure. "
+ "Do NOT call update_and_run_blocks or run_blocks_and_collect_debug again. REPLY now: summarize "
+ "what worked, name the block that failed, and tell the user they can keep the verified prefix or discard."
+ )
+
+ 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 "
+ "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. "
+ "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."
+ )
+
+
def _tool_loop_error(ctx: AgentContext, tool_name: str, arguments: dict[str, Any] | None = None) -> str | None:
detected = detect_failed_tool_step_loop_for_ctx(ctx, tool_name, arguments or {})
if detected is not None:
@@ -457,26 +500,9 @@ def _tool_loop_error(ctx: AgentContext, tool_name: str, arguments: dict[str, Any
"explaining the failure and asking them to verify the URL."
)
- # Gate on ``last_failed_workflow_yaml`` (sticky across
- # ``update_workflow``) rather than ``last_test_ok``: otherwise the
- # agent can sandwich an ``update_workflow`` call between the failure
- # and the retry to clear ``last_test_ok=None`` and slip past.
- start_ts = getattr(ctx, "copilot_run_start_monotonic", None)
- last_failed_yaml = getattr(ctx, "last_failed_workflow_yaml", None)
- last_good_yaml = getattr(ctx, "last_good_workflow_yaml", None)
- if start_ts is not None and last_failed_yaml and last_good_yaml:
- remaining = TOTAL_TIMEOUT_SECONDS - (time.monotonic() - start_ts)
- if remaining < PER_TOOL_CALL_BUDGET_SECONDS:
- return (
- f"Wall-clock budget too low to retry: about "
- f"{int(max(0.0, remaining))}s remain of the "
- f"{TOTAL_TIMEOUT_SECONDS}s session budget. A verified "
- "workflow exists from before the failure. Do NOT call "
- "update_and_run_blocks or run_blocks_and_collect_debug "
- "again. REPLY now: summarize what worked, name the "
- "block that failed, and tell the user they can keep "
- "the verified prefix or discard."
- )
+ late_block_running_error = _late_block_running_call_error(ctx)
+ if late_block_running_error is not None:
+ return late_block_running_error
return None
diff --git a/tests/unit/test_copilot_mid_correction_guard.py b/tests/unit/test_copilot_mid_correction_guard.py
index 88a3ae2e0..7325592bb 100644
--- a/tests/unit/test_copilot_mid_correction_guard.py
+++ b/tests/unit/test_copilot_mid_correction_guard.py
@@ -14,6 +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,
_record_run_blocks_result,
_tool_loop_error,
@@ -53,23 +54,27 @@ 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 - (PER_TOOL_CALL_BUDGET_SECONDS + 60)
+ elapsed_seconds=TOTAL_TIMEOUT_SECONDS - (MIN_BLOCK_RUNNING_REMAINING_SECONDS + 30)
)
assert _tool_loop_error(ctx, "update_and_run_blocks") is None
-def test_tool_loop_error_no_guard_on_first_call() -> None:
+def test_tool_loop_error_blocks_first_call_when_turn_budget_is_too_low() -> None:
ctx = _fresh_context()
ctx.copilot_run_start_monotonic = time.monotonic() - (TOTAL_TIMEOUT_SECONDS - 10)
- assert _tool_loop_error(ctx, "update_and_run_blocks") is None
+ msg = _tool_loop_error(ctx, "update_and_run_blocks")
+ assert msg is not None
+ assert "Do NOT start another block-running tool call" in msg
-def test_tool_loop_error_no_guard_when_no_good_workflow_exists() -> None:
+def test_tool_loop_error_blocks_failed_retry_when_no_good_workflow_exists() -> None:
ctx = _fresh_context()
ctx.copilot_run_start_monotonic = time.monotonic() - (TOTAL_TIMEOUT_SECONDS - 10)
ctx.last_test_ok = False
ctx.last_failed_workflow_yaml = "yaml-failed"
- assert _tool_loop_error(ctx, "update_and_run_blocks") is None
+ msg = _tool_loop_error(ctx, "update_and_run_blocks")
+ assert msg is not None
+ assert "Do NOT retry" in msg
def test_tool_loop_error_no_guard_for_non_block_running_tools() -> None:
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 0d8819569..8fed6e8a3 100644
--- a/tests/unit/test_copilot_tool_loop_error_block_running.py
+++ b/tests/unit/test_copilot_tool_loop_error_block_running.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+import time
from types import SimpleNamespace
from typing import Any
@@ -15,6 +16,10 @@ def _ctx(**overrides: Any) -> Any:
"repeated_action_fingerprint_streak_count": 0,
"last_test_non_retriable_nav_error": None,
"non_retriable_nav_error_last_emitted_signature": None,
+ "last_failed_workflow_yaml": None,
+ "last_workflow_yaml": None,
+ "last_test_ok": None,
+ "copilot_run_start_monotonic": None,
}
base.update(overrides)
return SimpleNamespace(**base)
@@ -49,6 +54,68 @@ def test_block_running_tool_is_blocked_by_repeated_action_streak() -> None:
assert "Repeated-action abort" in msg
+def test_block_running_tool_blocks_late_retry_after_failed_workflow() -> None:
+ ctx = _ctx(
+ last_failed_workflow_yaml="version: '1.0'",
+ 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 "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:
+ ctx = _ctx(
+ last_failed_workflow_yaml="version: '1.0'",
+ copilot_run_start_monotonic=time.monotonic() - 271,
+ )
+
+ 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
+
+
+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,
+ )
+
+ msg = _tool_loop_error(ctx, "update_and_run_blocks")
+
+ assert msg is not None
+ assert "less than 330 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
+
+
+def test_late_retry_guard_waits_until_budget_is_low() -> None:
+ ctx = _ctx(
+ last_failed_workflow_yaml="version: '1.0'",
+ copilot_run_start_monotonic=time.monotonic() - 250,
+ )
+
+ assert _tool_loop_error(ctx, "update_and_run_blocks") is None
+
+
+def test_late_retry_guard_is_scoped_to_block_running_tools() -> None:
+ ctx = _ctx(
+ last_failed_workflow_yaml="version: '1.0'",
+ copilot_run_start_monotonic=time.monotonic() - 540,
+ )
+
+ assert _tool_loop_error(ctx, "update_workflow") is None
+
+
def test_bypass_applies_to_both_block_running_tool_names() -> None:
assert "update_and_run_blocks" in BLOCK_RUNNING_TOOLS
assert "run_blocks_and_collect_debug" in BLOCK_RUNNING_TOOLS