Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
SKY-10678 Let a diagnosis turn repair the run it just read (#8053)
Andrew Neilson80aa339348b7
diff --git a/skyvern/forge/sdk/copilot/tools/guardrails.py b/skyvern/forge/sdk/copilot/tools/guardrails.pyindex 2f5690a77..25d0b03c6 100644--- a/skyvern/forge/sdk/copilot/tools/guardrails.py+++ b/skyvern/forge/sdk/copilot/tools/guardrails.py@@ -423,9 +423,10 @@ def _turn_intent_tool_error(ctx: AgentContext, tool_name: str) -> CopilotToolBloblocks_update = tool_name in WORKFLOW_MUTATION_TOOLS and not authority.may_update_workflowblocks_run = tool_name in BLOCK_RUNNING_TOOLS and not authority.may_run_blocks- blocks_page_inspection = tool_name in PAGE_SCHEMA_CONTEXT_TOOLS and (- intent.mode in NO_MUTATION_TURN_INTENT_MODES or not authority.may_update_workflow- )+ # Authority, not mode membership: every other NO_MUTATION mode already resolves to+ # may_update_workflow False, so keying on the mode too only ever blocked a diagnose turn that+ # had earned write authority — the one case where inspecting the page is the point.+ blocks_page_inspection = tool_name in PAGE_SCHEMA_CONTEXT_TOOLS and not authority.may_update_workflowblocks_credential_metadata = tool_name in CREDENTIAL_METADATA_TOOLS and not (authority.may_update_workflow or authority.may_run_blocks)diff --git a/skyvern/forge/sdk/copilot/turn_intent.py b/skyvern/forge/sdk/copilot/turn_intent.pyindex a6b796ea2..3e780771e 100644--- a/skyvern/forge/sdk/copilot/turn_intent.py+++ b/skyvern/forge/sdk/copilot/turn_intent.py@@ -963,20 +963,29 @@ def build_turn_intent(authority.may_run_blocks = Falseauthority.requires_user_input = Trueelif mode == TurnIntentMode.DIAGNOSE:- authority.may_update_workflow = False- retest_mandated = (- request_policy.testing_intent == "require_test"- and request_policy.allow_run_blocks- and classification is not None- and classification.mode == TurnIntentMode.DIAGNOSE+ classifier_chose_diagnose = classification is not None and classification.mode == TurnIntentMode.DIAGNOSE+ # An affirmative, confident diagnose classification says which context the turn needs; it does not+ # withdraw the authority RequestPolicy granted. Authority is still withheld on the routes that never+ # asserted the user wants a repair: a declared diagnose-first turn (fix_origin), a DIAGNOSE reached+ # by recovery from UNKNOWN or a classifier failure, and a low-confidence guess — which must not+ # out-rank an equally unsure edit, downgraded to CLARIFY above.+ diagnose_earns_authority = (+ classifier_chose_diagnose and not fix_origin and confidence >= _LOW_CONFIDENCE_MUTATION_THRESHOLD)- if retest_mandated:- # RequestPolicy owns testing policy; an explicit require_test re-run must not be inverted- # by the diagnose classification.- authority.may_run_blocks = True- reason_codes.append(TurnIntentReasonCode.TESTING_INTENT_RUN_OVERRIDES_DIAGNOSE)- else:- authority.may_run_blocks = False+ if not diagnose_earns_authority:+ authority.may_update_workflow = False+ retest_mandated = (+ request_policy.testing_intent == "require_test"+ and request_policy.allow_run_blocks+ and classifier_chose_diagnose+ )+ if retest_mandated:+ # RequestPolicy owns testing policy; an explicit require_test re-run must not be inverted+ # by the diagnose classification.+ authority.may_run_blocks = True+ reason_codes.append(TurnIntentReasonCode.TESTING_INTENT_RUN_OVERRIDES_DIAGNOSE)+ else:+ authority.may_run_blocks = Falseif RequiredContextKey.LATEST_RUN_RESULT not in required_context:required_context.append(RequiredContextKey.LATEST_RUN_RESULT)diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.pyindex 7af21a6e7..bf2e4027a 100644--- a/tests/unit/test_copilot_agent_helpers.py+++ b/tests/unit/test_copilot_agent_helpers.py@@ -1714,7 +1714,7 @@ class TestRequestPolicyInputGuardrail:assert ctx.completion_criteria_turn_state is None@pytest.mark.asyncio- async def test_read_only_diagnose_skips_authoring_enrichment(self, monkeypatch) -> None:+ async def test_declared_read_only_diagnose_skips_authoring_enrichment(self, monkeypatch) -> None:policy = RequestPolicy(testing_intent="unspecified", _authoring_pending=True)monkeypatch.setattr(agent_module,@@ -1743,6 +1743,7 @@ class TestRequestPolicyInputGuardrail:request_policy_handler=object(),turn_intent_handler=object(),workflow_run_id="wr_1",+ fix_origin=True,)guardrail = agent_module._build_copilot_input_guardrails(@@ -1819,7 +1820,7 @@ class TestRequestPolicyInputGuardrail:assert materialize_mock.await_args.kwargs["active_criteria"] == list(stored.criteria)assert ctx.turn_intent is not Noneassert ctx.turn_intent.mode is TurnIntentMode.DIAGNOSE- assert ctx.turn_intent.authority.may_update_workflow is False+ assert ctx.turn_intent.authority.may_update_workflow is Trueassert ctx.turn_intent.authority.may_run_blocks is Trueassert policy.completion_criteria == list(stored.criteria)assert ctx.completion_criteria_turn_state is not Nonediff --git a/tests/unit/test_copilot_turn_intent.py b/tests/unit/test_copilot_turn_intent.pyindex 4fc85a52b..4ed18b282 100644--- a/tests/unit/test_copilot_turn_intent.py+++ b/tests/unit/test_copilot_turn_intent.py@@ -256,12 +256,118 @@ def test_build_turn_intent_applies_diagnose_classification_and_read_authority()assert intent.mode == TurnIntentMode.DIAGNOSEassert intent.expected_output == TurnIntentExpectedOutput.RUN_RESULTassert intent.authority.may_read_run_context is True- assert intent.authority.may_update_workflow is False- assert intent.authority.may_run_blocks is Falseassert RequiredContextKey.LATEST_RUN_RESULT in intent.required_contextassert intent.target_entities["run"] == ["wr_123"]+def test_build_turn_intent_affirmative_diagnose_keeps_request_policy_write_authority() -> None:+ intent = build_turn_intent(+ user_message="We extracted the wrong value; we got the % change, not the number.",+ workflow_yaml="title: Existing\nworkflow_definition:\n blocks: []\n",+ chat_history=[],+ global_llm_context="",+ request_policy=RequestPolicy(allow_update_workflow=True, allow_run_blocks=True),+ workflow_run_id="wr_123",+ classifier_result=_classification(TurnIntentMode.DIAGNOSE),+ )++ assert intent.mode == TurnIntentMode.DIAGNOSE+ assert intent.authority.may_update_workflow is True+ assert intent.authority.may_run_blocks is True+ # The two capabilities a run repair needs are available on the same turn.+ assert intent.authority.may_read_run_context is True+++def test_build_turn_intent_affirmative_diagnose_still_honors_restrictive_request_policy() -> None:+ intent = build_turn_intent(+ user_message="We extracted the wrong value; we got the % change, not the number.",+ workflow_yaml="title: Existing\nworkflow_definition:\n blocks: []\n",+ chat_history=[],+ global_llm_context="",+ request_policy=RequestPolicy(allow_update_workflow=False, allow_run_blocks=False),+ workflow_run_id="wr_123",+ classifier_result=_classification(TurnIntentMode.DIAGNOSE),+ )++ assert intent.authority.may_update_workflow is False+ assert intent.authority.may_run_blocks is False++[email protected]("confidence", [0.0, 0.49])+def test_build_turn_intent_low_confidence_diagnose_does_not_earn_write_authority(confidence: float) -> None:+ # A missing/unparseable confidence coerces to 0.0, so an unsure diagnose must stay read-only rather+ # than inherit RequestPolicy's permissive defaults.+ classification = TurnIntentClassification(+ mode=TurnIntentMode.DIAGNOSE,+ expected_output=TurnIntentExpectedOutput.RUN_RESULT,+ required_context=[],+ confidence=confidence,+ target_entities={},+ reason_codes=[],+ missing_context_question=None,+ )+ intent = build_turn_intent(+ user_message="something looks off with the number",+ workflow_yaml="title: Existing\nworkflow_definition:\n blocks: []\n",+ chat_history=[],+ global_llm_context="",+ request_policy=RequestPolicy(allow_update_workflow=True, allow_run_blocks=True),+ workflow_run_id="wr_123",+ classifier_result=TurnIntentClassifierResult.success(classification),+ )++ assert intent.mode == TurnIntentMode.DIAGNOSE+ assert intent.authority.may_update_workflow is False+ assert intent.authority.may_run_blocks is False+ assert intent.authority.may_read_run_context is True+++def test_build_turn_intent_spurious_fix_origin_reason_code_does_not_withhold_authority() -> None:+ # The classifier may emit FIX_ORIGIN_DIAGNOSE itself; only the caller-supplied fix_origin flag+ # means the user actually asked for diagnose-first.+ classification = TurnIntentClassification(+ mode=TurnIntentMode.DIAGNOSE,+ expected_output=TurnIntentExpectedOutput.RUN_RESULT,+ required_context=[],+ confidence=0.91,+ target_entities={},+ reason_codes=[TurnIntentReasonCode.FIX_ORIGIN_DIAGNOSE],+ missing_context_question=None,+ )+ intent = build_turn_intent(+ user_message="We extracted the wrong value; we got the % change, not the number.",+ workflow_yaml="title: Existing\nworkflow_definition:\n blocks: []\n",+ chat_history=[],+ global_llm_context="",+ request_policy=RequestPolicy(allow_update_workflow=True, allow_run_blocks=True),+ workflow_run_id="wr_123",+ classifier_result=TurnIntentClassifierResult.success(classification),+ fix_origin=False,+ )++ assert intent.mode == TurnIntentMode.DIAGNOSE+ assert intent.authority.may_update_workflow is True+ assert intent.authority.may_run_blocks is True+++def test_build_turn_intent_declared_fix_origin_diagnose_withholds_write_authority() -> None:+ intent = build_turn_intent(+ user_message="We extracted the wrong value; we got the % change, not the number.",+ workflow_yaml="title: Existing\nworkflow_definition:\n blocks: []\n",+ chat_history=[],+ global_llm_context="",+ request_policy=RequestPolicy(allow_update_workflow=True, allow_run_blocks=True),+ workflow_run_id="wr_123",+ classifier_result=_classification(TurnIntentMode.DIAGNOSE),+ fix_origin=True,+ )++ assert intent.mode == TurnIntentMode.DIAGNOSE+ assert intent.authority.may_update_workflow is False+ assert intent.authority.may_run_blocks is False+ assert TurnIntentReasonCode.FIX_ORIGIN_DIAGNOSE in intent.reason_codes++def test_build_turn_intent_applies_draft_only_classification_without_run_authority() -> None:intent = build_turn_intent(user_message="Draft only; no validation run.",@@ -304,7 +410,7 @@ def test_build_turn_intent_llm_diagnose_outranks_skip_test_policy() -> None:assert intent.mode == TurnIntentMode.DIAGNOSEassert intent.authority.may_read_run_context is True- assert intent.authority.may_update_workflow is False+ assert intent.authority.may_run_blocks is Falseassert TurnIntentReasonCode.TESTING_INTENT_SKIP_TEST not in intent.reason_codes@@ -321,9 +427,25 @@ def test_build_turn_intent_diagnose_with_require_test_keeps_run_authority() -> Nassert intent.mode == TurnIntentMode.DIAGNOSEassert intent.authority.may_run_blocks is True- assert intent.authority.may_update_workflow is Falseassert intent.authority.may_read_run_context is Trueassert RequiredContextKey.LATEST_RUN_RESULT in intent.required_context+++def test_build_turn_intent_declared_diagnose_with_require_test_keeps_run_authority() -> None:+ intent = build_turn_intent(+ user_message="Test it again and confirm what it extracted.",+ workflow_yaml="title: Existing\nworkflow_definition:\n blocks: []\n",+ chat_history=[],+ global_llm_context="",+ request_policy=RequestPolicy(testing_intent="require_test", allow_update_workflow=True, allow_run_blocks=True),+ workflow_run_id="wr_123",+ classifier_result=_classification(TurnIntentMode.DIAGNOSE),+ fix_origin=True,+ )++ assert intent.mode == TurnIntentMode.DIAGNOSE+ assert intent.authority.may_run_blocks is True+ assert intent.authority.may_update_workflow is Falseassert TurnIntentReasonCode.TESTING_INTENT_RUN_OVERRIDES_DIAGNOSE in intent.reason_codes@@ -997,7 +1119,7 @@ def test_store_request_policy_attaches_classified_turn_intent_to_context() -> Noassert ctx.turn_intent is not Noneassert ctx.turn_intent.mode == TurnIntentMode.DIAGNOSE- assert ctx.turn_intent.authority.may_update_workflow is False+ assert ctx.turn_intent.authority.may_update_workflow is Trueassert ctx.turn_intent.authority.may_run_blocks is Falsediff --git a/tests/unit/test_copilot_turn_intent_tool_gate.py b/tests/unit/test_copilot_turn_intent_tool_gate.pyindex 31e61cc5b..4b7f58b73 100644--- a/tests/unit/test_copilot_turn_intent_tool_gate.py+++ b/tests/unit/test_copilot_turn_intent_tool_gate.py@@ -209,6 +209,27 @@ def test_diagnose_soft_block_still_denies_side_effect(tool_name: str) -> None:"get_run_results",id="diagnose_allows_get_run_results_tool",),+ pytest.param(+ TurnIntentMode.DIAGNOSE,+ {"may_update_workflow": True, "may_run_blocks": True, "may_read_run_context": True},+ {},+ "inspect_page_for_composition",+ id="diagnose_with_write_authority_allows_page_inspection",+ ),+ pytest.param(+ TurnIntentMode.DIAGNOSE,+ {"may_update_workflow": True, "may_run_blocks": True, "may_read_run_context": True},+ {},+ "update_workflow",+ id="diagnose_with_write_authority_allows_workflow_update",+ ),+ pytest.param(+ TurnIntentMode.DIAGNOSE,+ {"may_update_workflow": True, "may_run_blocks": True, "may_read_run_context": True},+ {},+ "update_and_run_blocks",+ id="diagnose_with_write_authority_allows_update_and_run",+ ),],)def test_turn_intent_gate_allows_authorized_tool(