Skip to content

Automate browser based workflows with AI

feat(SKY-11580): diagnose turns produce a useful diagnosis instead of dead-ending (#6937)

pedrohsdbbb3471a1c201
diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.py
index 763735732..81c84a0c9 100644
--- a/skyvern/forge/sdk/copilot/agent.py
+++ b/skyvern/forge/sdk/copilot/agent.py
@@ -2672,6 +2672,22 @@ async def _translate_to_agent_result(
note = detail if not contains_internal_machinery_leak(detail) else _INLINE_REJECT_NOTE_FALLBACK
return f"{response}\n\n(Note: {note})"
+ turn_intent = ctx.turn_intent if isinstance(ctx.turn_intent, TurnIntent) else None
+ may_update_workflow = turn_intent is None or turn_intent.authority.may_update_workflow
+ if resp_type == "REPLACE_WORKFLOW" and turn_intent is not None and not turn_intent.authority.may_update_workflow:
+ # A no-mutation turn (e.g. DIAGNOSE) must not stage a draft even via an inline REPLACE_WORKFLOW, which
+ # bypasses the may_update_workflow=False authority enforced on the update_workflow tool. Downgrade to a
+ # REPLY so the diagnosis still lands but no edit is staged; the user applies it on a follow-up edit turn.
+ LOG.info(
+ "copilot suppressed inline REPLACE_WORKFLOW on no-mutation turn",
+ turn_intent_mode=turn_intent.mode.value,
+ )
+ user_response = _with_inline_reject_note(
+ user_response,
+ "Diagnosing a failed run doesn't edit the workflow on its own — confirm and I'll apply the change.",
+ )
+ resp_type = "REPLY"
+
if resp_type == "REPLACE_WORKFLOW":
LOG.warning("Agent used inline REPLACE_WORKFLOW instead of update_workflow tool")
workflow_yaml = action_data.get("workflow_yaml", "")
@@ -2774,9 +2790,15 @@ async def _translate_to_agent_result(
# never run, so a leftover ``last_test_ok is True`` from an earlier tested
# (but different) yaml must not promote this untested one.
# ``blocker_active`` should already have rewritten resp_type away from
- # REPLACE_WORKFLOW above; explicit guard here defends against future
- # refactors that re-emit REPLACE_WORKFLOW post-rendering.
- if resp_type == "REPLACE_WORKFLOW" and last_workflow is not ctx.last_workflow and not blocker_active:
+ # REPLACE_WORKFLOW above, and a no-mutation turn was downgraded to REPLY;
+ # the explicit guards here defend against future refactors that re-emit
+ # REPLACE_WORKFLOW post-rendering or bypass the downgrade.
+ if (
+ resp_type == "REPLACE_WORKFLOW"
+ and last_workflow is not ctx.last_workflow
+ and not blocker_active
+ and may_update_workflow
+ ):
ctx.last_workflow = last_workflow
ctx.last_workflow_yaml = last_workflow_yaml
ctx.last_test_ok = None
diff --git a/skyvern/forge/sdk/copilot/tools/guardrails.py b/skyvern/forge/sdk/copilot/tools/guardrails.py
index cb3a15977..8e1de1433 100644
--- a/skyvern/forge/sdk/copilot/tools/guardrails.py
+++ b/skyvern/forge/sdk/copilot/tools/guardrails.py
@@ -6,7 +6,7 @@ from typing import Any
import structlog
from agents import ToolGuardrailFunctionOutput, ToolInputGuardrail, ToolInputGuardrailData
-from skyvern.forge.sdk.copilot.blocker_signal import CopilotToolBlockerSignal, RecoveryHint
+from skyvern.forge.sdk.copilot.blocker_signal import BlockerKind, CopilotToolBlockerSignal, RecoveryHint
from skyvern.forge.sdk.copilot.build_phase import _phase_blocker_signal
from skyvern.forge.sdk.copilot.composition_evidence import (
composition_page_evidence_error,
@@ -573,6 +573,24 @@ def _turn_intent_tool_error(ctx: AgentContext, tool_name: str) -> CopilotToolBlo
user_facing_reason="I'll answer with the information I already have.",
recovery_hint="report_blocker_to_user",
)
+ if may_read_run_context:
+ # Mutually exclusive with the blocks_context_read branch above, which requires may_read_run_context False.
+ # The allow-path returned None above, so any reaching tool is denied; deny it without terminating, keeping
+ # the turn alive to diagnose from the run evidence.
+ return _build_turn_intent_signal(
+ tool_name=tool_name,
+ classifier_mode=intent.mode.value,
+ reason_code=reason_code,
+ agent_steering_text=(
+ "You are diagnosing a past run and cannot run blocks, edit the workflow, inspect a live page, or "
+ "fetch additional context in this turn. Diagnose the failure from the run's failure evidence and "
+ "the workflow code already in context, then propose one concrete fix for the user to confirm."
+ ),
+ user_facing_reason="Let me work through what the run shows.",
+ recovery_hint="retry_with_different_tool",
+ blocker_kind="tool_error",
+ renders_final_reply=False,
+ )
return _build_turn_intent_signal(
tool_name=tool_name,
classifier_mode=intent.mode.value,
@@ -596,13 +614,16 @@ def _build_turn_intent_signal(
user_facing_reason: str,
recovery_hint: RecoveryHint,
cleared_by_tools: frozenset[str] = frozenset(),
+ blocker_kind: BlockerKind = "authority_denied",
+ renders_final_reply: bool = True,
) -> CopilotToolBlockerSignal:
return CopilotToolBlockerSignal(
- blocker_kind="authority_denied",
+ blocker_kind=blocker_kind,
agent_steering_text=agent_steering_text,
user_facing_reason=user_facing_reason,
recovery_hint=recovery_hint,
cleared_by_tools=cleared_by_tools,
+ renders_final_reply=renders_final_reply,
internal_reason_code=reason_code,
blocked_tool=tool_name,
classifier_mode=classifier_mode,
diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.py
index ca33b45f2..67916620f 100644
--- a/tests/unit/test_copilot_agent_helpers.py
+++ b/tests/unit/test_copilot_agent_helpers.py
@@ -1864,6 +1864,40 @@ class TestTranslateToAgentResultGating:
assert agent_result.workflow_yaml is None
assert agent_result.response_type == "REPLACE_WORKFLOW"
+ def test_inline_replace_workflow_suppressed_on_no_mutation_turn(self, monkeypatch) -> None:
+ # A DIAGNOSE turn is may_update_workflow=False, but an inline REPLACE_WORKFLOW bypasses the
+ # may_update_workflow authority enforced on the update_workflow tool. The translate helper must downgrade
+ # it to a REPLY so the diagnosis lands without processing or staging the REPLACE candidate.
+ def _must_not_process(**kwargs):
+ raise AssertionError("inline REPLACE_WORKFLOW was processed on a no-mutation turn")
+
+ monkeypatch.setattr("skyvern.forge.sdk.copilot.tools._process_workflow_yaml", _must_not_process)
+ ctx = _ctx(
+ turn_intent=TurnIntent(
+ mode=TurnIntentMode.DIAGNOSE,
+ authority=TurnIntentAuthority(
+ may_update_workflow=False, may_run_blocks=False, may_read_run_context=True
+ ),
+ ),
+ )
+ result = _fake_run_result(
+ {
+ "type": "REPLACE_WORKFLOW",
+ "user_response": "Here's the fixed workflow.",
+ "workflow_yaml": "new: yaml",
+ }
+ )
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
+ )
+ # Downgraded to REPLY; the REPLACE candidate is neither processed (the patch would have raised) nor staged.
+ assert agent_result.response_type == "REPLY"
+ assert ctx.last_workflow is None
+ assert agent_result.updated_workflow is None
+ assert "confirm and i'll apply" in agent_result.user_response.lower()
+
def test_inline_replace_workflow_rejects_stale_block_metadata(self, monkeypatch) -> None:
# Inline REPLACE_WORKFLOW bypasses _update_workflow, so it must also
# reject a corrected workflow whose labels/titles still describe the
diff --git a/tests/unit/test_copilot_turn_intent_tool_gate.py b/tests/unit/test_copilot_turn_intent_tool_gate.py
index c05262f76..42b3000a7 100644
--- a/tests/unit/test_copilot_turn_intent_tool_gate.py
+++ b/tests/unit/test_copilot_turn_intent_tool_gate.py
@@ -82,10 +82,8 @@ def _assert_signal(
("mode", "tool_name", "expected_reason"),
[
(TurnIntentMode.DOCS_ANSWER, "update_workflow", "turn_intent_no_mutation_update_blocked"),
- (TurnIntentMode.DIAGNOSE, "run_blocks_and_collect_debug", "turn_intent_no_mutation_run_blocked"),
(TurnIntentMode.DOCS_ANSWER, "inspect_page_for_composition", "turn_intent_page_inspection_blocked"),
(TurnIntentMode.DOCS_ANSWER, "list_credentials", "turn_intent_credential_metadata_blocked"),
- (TurnIntentMode.DIAGNOSE, "list_credentials", "turn_intent_credential_metadata_blocked"),
(TurnIntentMode.CLARIFY, "inspect_page_for_composition", "turn_intent_page_inspection_blocked"),
(TurnIntentMode.CLARIFY, "update_and_run_blocks", "turn_intent_no_mutation_run_blocked"),
(TurnIntentMode.REFUSE, "update_and_run_blocks", "turn_intent_no_mutation_run_blocked"),
@@ -109,6 +107,55 @@ def test_no_mutation_turn_intent_blocks_mutating_tools(
_assert_signal(signal, internal_reason_code=expected_reason, classifier_mode=mode.value, blocked_tool=tool_name)
+def _diagnose_intent() -> TurnIntent:
+ return TurnIntent(
+ mode=TurnIntentMode.DIAGNOSE,
+ authority=TurnIntentAuthority(
+ may_update_workflow=False,
+ may_run_blocks=False,
+ may_read_run_context=True,
+ ),
+ )
+
+
+ ("tool_name", "expected_reason"),
+ [
+ ("run_blocks_and_collect_debug", "turn_intent_no_mutation_run_blocked"),
+ ("update_and_run_blocks", "turn_intent_no_mutation_run_blocked"),
+ ("update_workflow", "turn_intent_no_mutation_update_blocked"),
+ ("inspect_page_for_composition", "turn_intent_page_inspection_blocked"),
+ ("list_credentials", "turn_intent_credential_metadata_blocked"),
+ ],
+)
+def test_diagnose_denied_tool_soft_blocks_and_continues_to_diagnosis(tool_name: str, expected_reason: str) -> None:
+ # A denied tool in a diagnose turn stays denied but must not terminate the turn: the blocker is a
+ # non-terminating soft steer toward an evidence-grounded fix, covering every denied tool class.
+ signal = _turn_intent_tool_error(_ctx(_diagnose_intent()), tool_name)
+ assert signal is not None
+ assert signal.internal_reason_code == expected_reason
+ assert signal.blocked_tool == tool_name
+ assert signal.classifier_mode == TurnIntentMode.DIAGNOSE.value
+ # the load-bearing assertions: soft, non-terminating, steered at the run evidence + a confirmable fix
+ assert signal.blocker_kind == "tool_error"
+ assert signal.renders_final_reply is False
+ assert signal.recovery_hint == "retry_with_different_tool"
+ steer = signal.agent_steering_text.lower()
+ assert "diagnose" in steer and "fix" in steer
+ assert "i'll respond with the information i already have" not in signal.user_facing_reason.lower()
+ for token in _LEAK_TOKENS:
+ assert token not in signal.user_facing_reason
+
+
[email protected]("tool_name", ["update_workflow", "update_and_run_blocks", "run_blocks_and_collect_debug"])
+def test_diagnose_soft_block_still_denies_side_effect(tool_name: str) -> None:
+ # A non-terminating soft block still returns a blocker, so the side-effecting tool is denied and never runs;
+ # renders_final_reply governs only whether the turn ends, not whether the side effect runs.
+ signal = _turn_intent_tool_error(_ctx(_diagnose_intent()), tool_name)
+ assert signal is not None
+ assert tool_name not in signal.cleared_by_tools
+
+
def test_turn_intent_gate_allows_draft_update_without_run_authority() -> None:
intent = TurnIntent(
mode=TurnIntentMode.DRAFT_ONLY,