Skip to content

Automate browser based workflows with AI

SKY-11193 Render Copilot status from typed run outcome (#6706)

Andrew Neilsonb6a25971d46a
diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.py
index e071c4c51..9ea145a3b 100644
--- a/skyvern/forge/sdk/copilot/agent.py
+++ b/skyvern/forge/sdk/copilot/agent.py
@@ -117,6 +117,7 @@ from skyvern.forge.sdk.copilot.request_policy import (
build_request_policy,
redact_raw_secrets_for_prompt,
)
+from skyvern.forge.sdk.copilot.run_outcome import RecordedRunOutcome, run_outcome_display_reason
from skyvern.forge.sdk.copilot.runtime import _browser_context_is_attachable
from skyvern.forge.sdk.copilot.streaming_adapter import (
emit_turn_start,
@@ -1402,6 +1403,62 @@ _UNVALIDATED_PROPOSAL_AFFORDANCE = (
"It has not been tested or verified end-to-end."
)
+
+@dataclass(frozen=True)
+class _TypedRunOutcomeReply:
+ user_response: str
+ demonstrated: bool
+
+
+def _safe_run_outcome_display_reason(recorded: RecordedRunOutcome) -> str | None:
+ reason = run_outcome_display_reason(recorded.display_reason)
+ if reason is None or contains_internal_machinery_leak(reason):
+ return None
+ try:
+ assert_clean_user_facing_text(reason)
+ except ValueError:
+ return None
+ return reason.rstrip(".")
+
+
+def _render_typed_run_outcome_reply(
+ ctx: CopilotContext,
+ *,
+ response_type: ResponseType,
+ has_verified_workflow: bool,
+ blocker_active: bool,
+) -> _TypedRunOutcomeReply | None:
+ if blocker_active or response_type != "REPLY":
+ return None
+ recorded = ctx.last_run_outcome
+ if not isinstance(recorded, RecordedRunOutcome):
+ return None
+ if recorded.verdict == "demonstrated":
+ if not has_verified_workflow or not verified_goal_claim_authorized(ctx):
+ return None
+ return _TypedRunOutcomeReply(
+ user_response=(
+ "I created and tested the workflow successfully. The latest run demonstrated the requested outcome."
+ ),
+ demonstrated=True,
+ )
+
+ reason = _safe_run_outcome_display_reason(recorded)
+ if recorded.verdict == "not_demonstrated":
+ user_response = (
+ "I built and ran the workflow, but the latest run did not demonstrate the requested outcome. "
+ "Review the draft before using it."
+ )
+ else:
+ user_response = (
+ "I built and ran the workflow, but the latest run could not verify the requested outcome. "
+ "Review the draft before using it."
+ )
+ if reason is not None:
+ user_response = f"{user_response} Reason: {reason}."
+ return _TypedRunOutcomeReply(user_response=user_response, demonstrated=False)
+
+
# Pre-validated safe string the finalization shim falls back to when the
# rendered blocker reply somehow trips OutputPolicy. Asserted clean at module
# load time so a future OutputPolicy regression doesn't silently land here.
@@ -2224,6 +2281,15 @@ async def _translate_to_agent_result(
verified_workflow, verified_yaml = _verified_workflow_or_none(ctx)
# Default-true preserves backwards-compat with stale prompts and missing fields.
agent_admits_incomplete = _is_explicit_false(action_data.get("goal_reached"))
+ typed_outcome_reply = _render_typed_run_outcome_reply(
+ ctx,
+ response_type=resp_type,
+ has_verified_workflow=verified_workflow is not None,
+ blocker_active=blocker_active,
+ )
+ if typed_outcome_reply is not None:
+ user_response = typed_outcome_reply.user_response
+ agent_admits_incomplete = not typed_outcome_reply.demonstrated
last_workflow = None
last_workflow_yaml = None
diff --git a/tests/unit/test_copilot_agent_blocker_renderer.py b/tests/unit/test_copilot_agent_blocker_renderer.py
index 75780d9f8..4a774576f 100644
--- a/tests/unit/test_copilot_agent_blocker_renderer.py
+++ b/tests/unit/test_copilot_agent_blocker_renderer.py
@@ -200,6 +200,27 @@ def test_shim_overrides_proposal_even_when_pre_override_result_carries_workflow(
assert overridden.workflow_yaml is None
+def test_blocker_signal_wins_over_demonstrated_recorded_outcome() -> None:
+ ctx = _ctx()
+ ctx.blocker_signal = _signal(user_facing="I need one more detail before I can continue.")
+ ctx.last_run_outcome = RecordedRunOutcome(verdict="demonstrated", workflow_run_id="wr_hidden")
+ fake_workflow = SimpleNamespace(name="verified")
+ result = AgentResult(
+ user_response="I created and tested the workflow successfully.",
+ updated_workflow=fake_workflow,
+ global_llm_context=None,
+ workflow_yaml="title: verified\n",
+ )
+
+ overridden = _finalize_result_with_blocker_override(ctx, result)
+
+ assert overridden.user_response == "I need one more detail before I can continue."
+ assert "created and tested" not in overridden.user_response.lower()
+ assert "wr_hidden" not in overridden.user_response
+ assert overridden.updated_workflow is None
+ assert overridden.proposal_disposition == "no_proposal"
+
+
def test_shim_recomputes_turn_outcome_from_rendered_reply() -> None:
ctx = _ctx()
ctx.blocker_signal = _signal()
diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.py
index 5ec85ea0c..6cbce8b4d 100644
--- a/tests/unit/test_copilot_agent_helpers.py
+++ b/tests/unit/test_copilot_agent_helpers.py
@@ -38,6 +38,7 @@ from skyvern.forge.sdk.copilot.request_policy import (
build_transcript_context,
redact_raw_secrets_for_prompt,
)
+from skyvern.forge.sdk.copilot.run_outcome import RecordedRunOutcome
from skyvern.forge.sdk.copilot.turn_context import TranscriptContext, TurnContextOmission, TurnContextPacket
from skyvern.forge.sdk.copilot.turn_intent import TurnIntent, TurnIntentAuthority, TurnIntentMode
from skyvern.forge.sdk.copilot.verification_evidence import WorkflowVerificationEvidence
@@ -1901,6 +1902,131 @@ workflow_definition:
assert agent_result.workflow_yaml == "title: drafted"
assert agent_result.proposal_disposition == "review_untested"
+ def test_demonstrated_recorded_outcome_overrides_goal_reached_false_underclaim(self) -> None:
+ from skyvern.forge.sdk.copilot.completion_verification import CompletionVerificationResult, CriterionVerdict
+
+ wf = SimpleNamespace(name="verified")
+ ctx = _ctx(
+ last_workflow=wf,
+ last_workflow_yaml="title: verified",
+ last_test_ok=True,
+ last_full_workflow_test_ok=True,
+ last_update_block_count=4,
+ last_run_outcome=RecordedRunOutcome(verdict="demonstrated", workflow_run_id="wr_secret"),
+ completion_verification_result=CompletionVerificationResult(
+ status="evaluated",
+ criterion_ids=["c0"],
+ verdicts=[CriterionVerdict(criterion_id="c0", state="satisfied", reason_code="evidence_confirms")],
+ ),
+ )
+ result = _fake_run_result(
+ {
+ "type": "REPLY",
+ "user_response": "I drafted this but did not test it end-to-end.",
+ "goal_reached": False,
+ }
+ )
+
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
+ )
+
+ assert agent_result.updated_workflow is wf
+ assert agent_result.workflow_yaml == "title: verified"
+ assert agent_result.proposal_disposition == "auto_applicable"
+ assert "created and tested" in agent_result.user_response.lower()
+ assert "demonstrated the requested outcome" in agent_result.user_response.lower()
+ assert "did not test" not in agent_result.user_response.lower()
+ assert "wr_secret" not in agent_result.user_response
+
+ def test_demonstrated_recorded_outcome_overrides_misleading_free_text(self) -> None:
+ from skyvern.forge.sdk.copilot.completion_verification import CompletionVerificationResult, CriterionVerdict
+
+ wf = SimpleNamespace(name="verified")
+ ctx = _ctx(
+ last_workflow=wf,
+ last_workflow_yaml="title: verified",
+ last_test_ok=True,
+ last_full_workflow_test_ok=True,
+ last_run_outcome=RecordedRunOutcome(verdict="demonstrated"),
+ completion_verification_result=CompletionVerificationResult(
+ status="evaluated",
+ criterion_ids=["c0"],
+ verdicts=[CriterionVerdict(criterion_id="c0", state="satisfied", reason_code="evidence_confirms")],
+ ),
+ )
+ result = _fake_run_result(
+ {
+ "type": "REPLY",
+ "user_response": "The test could not verify the requested result.",
+ "goal_reached": True,
+ }
+ )
+
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
+ )
+
+ assert agent_result.updated_workflow is wf
+ assert agent_result.proposal_disposition == "auto_applicable"
+ assert "demonstrated the requested outcome" in agent_result.user_response.lower()
+ assert "could not verify" not in agent_result.user_response.lower()
+
+ def test_not_demonstrated_recorded_outcome_stays_conservative_and_sanitized(self) -> None:
+ wf = SimpleNamespace(name="drafted")
+ ctx = _ctx(
+ last_workflow=wf,
+ last_workflow_yaml="title: drafted",
+ last_test_ok=True,
+ last_full_workflow_test_ok=True,
+ last_run_outcome=RecordedRunOutcome(
+ verdict="not_demonstrated",
+ display_reason="Statement month was still April.",
+ workflow_run_id="wr_hidden",
+ ),
+ )
+ result = _fake_run_result({"type": "REPLY", "user_response": "All set.", "goal_reached": True})
+
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
+ )
+
+ assert agent_result.updated_workflow is wf
+ assert agent_result.proposal_disposition == "review_untested"
+ assert "did not demonstrate the requested outcome" in agent_result.user_response.lower()
+ assert "statement month was still april" in agent_result.user_response.lower()
+ assert "all set" not in agent_result.user_response.lower()
+ assert "wr_hidden" not in agent_result.user_response
+
+ def test_not_evaluated_recorded_outcome_reports_run_unverified_not_untested(self) -> None:
+ wf = SimpleNamespace(name="drafted")
+ ctx = _ctx(
+ last_workflow=wf,
+ last_workflow_yaml="title: drafted",
+ last_test_ok=True,
+ last_full_workflow_test_ok=True,
+ last_run_outcome=RecordedRunOutcome(verdict="not_evaluated", display_reason="Output judge unavailable."),
+ )
+ result = _fake_run_result({"type": "REPLY", "user_response": "All set.", "goal_reached": True})
+
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
+ )
+
+ assert agent_result.updated_workflow is wf
+ assert agent_result.proposal_disposition == "review_untested"
+ assert "could not verify the requested outcome" in agent_result.user_response.lower()
+ assert "output judge unavailable" in agent_result.user_response.lower()
+ assert "untested" not in agent_result.user_response.lower()
+
def test_goal_reached_default_true_keeps_verified_path(self) -> None:
# Backwards-compat: stale prompts that omit goal_reached must continue
# to surface a tested workflow as validated.