Skip to content

Automate browser based workflows with AI

Copilot: stop unbounded retry loops on code-block turns — end with an honest failure message or a corrective hint (SKY-12711) (#7557)

Andrew Neilsondf443c1bacdd
diff --git a/skyvern/forge/sdk/copilot/tools/workflow_update.py b/skyvern/forge/sdk/copilot/tools/workflow_update.py
index 40e41eae7..da87ba319 100644
--- a/skyvern/forge/sdk/copilot/tools/workflow_update.py
+++ b/skyvern/forge/sdk/copilot/tools/workflow_update.py
@@ -3887,6 +3887,32 @@ def _adjudicate_metadata_reject_ladder(
)
+def _synthesized_metadata_reject_directive(
+ ctx: AgentContext,
+ *,
+ workflow_yaml: str,
+ raw_metadata: object,
+ label_candidates: list[str],
+ required_paths: set[str],
+) -> dict[str, Any] | None:
+ if len(label_candidates) != 1:
+ return None
+ label = label_candidates[0]
+ full_required_fields = {"declared_goal", *_CODE_ARTIFACT_REQUIRED_LIST_FIELDS, "evidence_refs_or_observation_refs"}
+ missing_fields_by_label = _metadata_missing_required_fields_by_label(
+ raw_metadata, labels=[label], missing_labels=[]
+ )
+ if set(missing_fields_by_label.get(label) or []) != full_required_fields:
+ return None
+ return _adjudicate_metadata_reject_ladder(
+ ctx,
+ workflow_yaml=workflow_yaml,
+ raw_metadata=raw_metadata,
+ missing_labels=[label],
+ required_paths=required_paths,
+ )
+
+
def _record_output_contract_reject(
ctx: AgentContext,
evaluation: _OutputContractEvaluation,
@@ -12300,13 +12326,23 @@ async def _update_workflow(
block_labels=output_empty_labels,
)
_record_code_authoring_guardrail_reject(ctx)
+ reject_data = _code_repair_progress_data()
+ metadata_convergence_directive = _synthesized_metadata_reject_directive(
+ ctx,
+ workflow_yaml=workflow_yaml,
+ raw_metadata=ctx.code_artifact_metadata,
+ label_candidates=output_empty_labels,
+ required_paths=unresolved_recorded_output_paths,
+ )
+ if metadata_convergence_directive is not None:
+ reject_data["metadata_convergence_directive"] = metadata_convergence_directive
return reject(
error=(
"Submitted workflow does not return any keyed output after the last recorded test outcome. "
"Add structured output for the unsatisfied completion criteria before testing again."
),
user_facing_summary=_compiled_authoring_user_summary(),
- data=_code_repair_progress_data(),
+ data=reject_data,
)
missing_output_paths = (
@@ -12356,6 +12392,16 @@ async def _update_workflow(
)
_record_code_authoring_guardrail_reject(ctx)
missing_path_text = ", ".join(missing_output_paths[:8])
+ reject_data = _code_repair_progress_data()
+ metadata_convergence_directive = _synthesized_metadata_reject_directive(
+ ctx,
+ workflow_yaml=workflow_yaml,
+ raw_metadata=ctx.code_artifact_metadata,
+ label_candidates=block_labels,
+ required_paths=set(missing_output_paths),
+ )
+ if metadata_convergence_directive is not None:
+ reject_data["metadata_convergence_directive"] = metadata_convergence_directive
return reject(
error=(
"Submitted workflow does not cover the missing requested output paths from the last recorded test "
@@ -12363,7 +12409,7 @@ async def _update_workflow(
"produce matching structured output before testing again; output_root is diagnostic only."
),
user_facing_summary=_compiled_authoring_user_summary(),
- data=_code_repair_progress_data(),
+ data=reject_data,
)
select_option_mismatch_context = _select_option_text_click_repair_context(workflow_yaml, ctx)
diff --git a/skyvern/forge/sdk/copilot/turn_ownership.py b/skyvern/forge/sdk/copilot/turn_ownership.py
index fa93cc8f3..9f96c060d 100644
--- a/skyvern/forge/sdk/copilot/turn_ownership.py
+++ b/skyvern/forge/sdk/copilot/turn_ownership.py
@@ -79,10 +79,12 @@ _PRECEDENCE_ORDER: tuple[TurnClaimant, ...] = (
# Grounding-over-persistence nested exception: a live grounding requirement is emitted instead
# of the persistence force when both hold.
TurnClaimant.RECORDED_OUTCOME_GROUNDING,
- # Cascade position after the rescout steer and below the grounding exception.
- TurnClaimant.SYNTHESIZED_BLOCK_PERSISTENCE_FORCE,
- # The churn convergence floor fires before the no-progress floor in the enforcement backstop.
+ # A budget-exhausted churn halt is the loop exit and outranks the persistence force, whose
+ # accepted-save channel has already failed by then; the pair only co-occurs on synthesized turns.
TurnClaimant.CODE_AUTHORING_CHURN,
+ # Cascade position after the rescout steer and below the grounding exception; defers to the
+ # exhausted churn halt above.
+ TurnClaimant.SYNTHESIZED_BLOCK_PERSISTENCE_FORCE,
# Loop detectors defer to every steering gate above them.
TurnClaimant.LOOP_DETECTED,
)
diff --git a/tests/unit/test_copilot_output_contract_actuation.py b/tests/unit/test_copilot_output_contract_actuation.py
index 51dbc5393..884cb9a60 100644
--- a/tests/unit/test_copilot_output_contract_actuation.py
+++ b/tests/unit/test_copilot_output_contract_actuation.py
@@ -1364,6 +1364,49 @@ def test_has_metadata_reject_attaches_no_metadata_convergence_directive(monkeypa
assert "metadata_convergence_directive" not in payload
+def test_synthesized_metadata_less_reject_arms_convergence_directive() -> None:
+ ctx = _ladder_ctx()
+ ctx.turn_id = "turn-synth-metadata-less"
+
+ with capture_logs() as logs:
+ directive = wu._synthesized_metadata_reject_directive(
+ ctx,
+ workflow_yaml=_PAGE_READ_YAML,
+ raw_metadata=[],
+ label_candidates=["collect"],
+ required_paths={"output.confirmation_number"},
+ )
+
+ assert directive is not None
+ assert any(entry["event"] == "copilot_output_contract_spine_structure_directive_emitted" for entry in logs)
+
+
+def test_synthesized_metadata_complete_reject_arms_no_directive() -> None:
+ ctx = _ladder_ctx()
+ ctx.turn_id = "turn-synth-metadata-complete"
+ complete_metadata = [
+ {
+ "block_label": "collect",
+ "declared_goal": "Collect the confirmation number",
+ "claimed_outcomes": ["confirmation captured"],
+ "page_dependencies": ["confirmation page"],
+ "completion_criteria": ["confirmation non-empty"],
+ "terminal_verifier_expectations": ["confirmation rendered"],
+ "evidence_refs": ["scout:confirmation"],
+ }
+ ]
+
+ directive = wu._synthesized_metadata_reject_directive(
+ ctx,
+ workflow_yaml=_PAGE_READ_YAML,
+ raw_metadata=complete_metadata,
+ label_candidates=["collect"],
+ required_paths={"output.confirmation_number"},
+ )
+
+ assert directive is None
+
+
def test_reject_seam_metadata_required_reaches_advisory_before_no_source_terminal() -> None:
ctx = _ladder_ctx()
signature = "sig_metadata_required"
diff --git a/tests/unit/test_copilot_turn_ownership.py b/tests/unit/test_copilot_turn_ownership.py
index 5f800c65a..d171d266a 100644
--- a/tests/unit/test_copilot_turn_ownership.py
+++ b/tests/unit/test_copilot_turn_ownership.py
@@ -1,5 +1,7 @@
from __future__ import annotations
+import pytest
+
from skyvern.forge.sdk.copilot.blocker_signal import (
GENUINELY_TERMINAL_BLOCKER_REASON_CODES,
SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE,
@@ -7,8 +9,14 @@ from skyvern.forge.sdk.copilot.blocker_signal import (
stash_blocker_signal,
)
from skyvern.forge.sdk.copilot.context import CopilotContext
+from skyvern.forge.sdk.copilot.enforcement import (
+ MAX_CODE_AUTHORING_GUARDRAIL_REJECTS,
+ _record_code_authoring_guardrail_reject,
+)
from skyvern.forge.sdk.copilot.output_contracts import OutputContractAdvisoryState
from skyvern.forge.sdk.copilot.turn_halt import (
+ CopilotTurnHalt,
+ raise_if_turn_halt,
retire_outranked_turn_halt,
stash_turn_halt_from_blocker_signal,
)
@@ -50,6 +58,23 @@ def _churn_signal() -> CopilotToolBlockerSignal:
return _signal("code_authoring_guardrail_churn")
+_REPLAY_STRUCTURAL_KEY = "b3d8aac91f1ae3a92d60ebd15ebee04216af45b03f18fe39d6deeefa77140e33"
+
+
+def _synthesized_force_signal() -> CopilotToolBlockerSignal:
+ return _signal(SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE, blocker_kind="tool_error", renders_final_reply=False)
+
+
+def _synthesized_replay_context() -> CopilotContext:
+ ctx = make_copilot_context()
+ ctx.impose_synthesized_code_block = True
+ ctx.recorded_build_test_outcome_history = [
+ {"structural_key": _REPLAY_STRUCTURAL_KEY, "phase": "build_test"},
+ {"structural_key": _REPLAY_STRUCTURAL_KEY, "phase": "build_test"},
+ ]
+ return ctx
+
+
def _grant_ladder(ctx: CopilotContext) -> None:
ctx.output_contract_actuation_by_signature["sig_a"] = OutputContractAdvisoryState.GRANTED
@@ -416,3 +441,44 @@ def test_fresh_context_has_no_owner() -> None:
ctx = make_copilot_context()
assert current_turn_owner(ctx) is None
assert ctx.gate_precedence_conflict_events == []
+
+
+def test_exhausted_churn_owns_over_held_synthesized_force_and_halts() -> None:
+ ctx = _synthesized_replay_context()
+ assert (
+ claim_and_stash_blocker_signal(
+ ctx, TurnClaimant.SYNTHESIZED_BLOCK_PERSISTENCE_FORCE, _synthesized_force_signal()
+ )
+ is not None
+ )
+
+ for _ in range(MAX_CODE_AUTHORING_GUARDRAIL_REJECTS):
+ _record_code_authoring_guardrail_reject(ctx)
+
+ owner = current_turn_owner(ctx)
+ assert owner is not None
+ assert owner.claimant is TurnClaimant.CODE_AUTHORING_CHURN
+ assert ctx.blocker_signal is not None
+ assert ctx.blocker_signal.internal_reason_code == "code_authoring_guardrail_churn"
+ assert blocker_signal_render_allowed(ctx, ctx.blocker_signal) is True
+ assert "code_authoring_guardrail_churn>synthesized_block_persistence_force" in _conflict_fingerprints(ctx)
+ assert ctx.code_authoring_guardrail_reject_count == MAX_CODE_AUTHORING_GUARDRAIL_REJECTS
+
+ stash_turn_halt_from_blocker_signal(ctx, ctx.blocker_signal, source="enforcement_backstop")
+ with pytest.raises(CopilotTurnHalt):
+ raise_if_turn_halt(ctx)
+
+
+def test_standard_path_churn_precedence_unchanged() -> None:
+ ctx = make_copilot_context()
+ assert (
+ claim_and_stash_blocker_signal(
+ ctx, TurnClaimant.CREDENTIAL_PRIORITY_CHURN, _signal("credential_priority_authoring_churn")
+ )
+ is not None
+ )
+ assert claim_turn(ctx, TurnClaimant.CODE_AUTHORING_CHURN) is ClaimOutcome.YIELDED
+
+ ctx2 = make_copilot_context()
+ assert claim_and_stash_blocker_signal(ctx2, TurnClaimant.CODE_AUTHORING_CHURN, _churn_signal()) is not None
+ assert claim_turn(ctx2, TurnClaimant.LOOP_DETECTED) is ClaimOutcome.YIELDED