Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Render a Copilot crash exit as a crash, not as a browser-operation failure (SKY-15512) (#8419)
Andrew Neilson994c73a6fe2b
diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.pyindex 2d6176ea1..ce7cff82a 100644--- a/skyvern/forge/sdk/copilot/agent.py+++ b/skyvern/forge/sdk/copilot/agent.py@@ -182,6 +182,7 @@ from skyvern.forge.sdk.copilot.streaming_adapter import (maybe_emit_design_end,)from skyvern.forge.sdk.copilot.terminal_envelope import (+ InterruptedTurnFacts,TerminalCause,TerminalOutcomeEnvelope,assemble_terminal_envelope,@@ -210,6 +211,7 @@ from skyvern.forge.sdk.copilot.turn_halt import (from skyvern.forge.sdk.copilot.turn_origin import TurnOriginfrom skyvern.forge.sdk.copilot.turn_outcome import (CANCEL_TERMINAL_REASON,+ UNEXPECTED_ERROR_TERMINAL_REASON,apply_repeated_reply_guard,connected_account_choice_context,selected_connected_account_id,@@ -1622,6 +1624,29 @@ def _terminal_connect_failure(ctx: CopilotContext) -> BuildTestConnectFailure |return outcome.connect_failure if isinstance(outcome, RecordedBuildTestOutcome) else None+def _crash_exit_interruption(+ ctx: CopilotContext,+ turn_outcome: TurnOutcome | None,+ failed_operation: BuildTestFailedOperation | None,+) -> InterruptedTurnFacts | None:+ """A turn that died in the error handler stopped; it never reported a test result.++ Scoped to a crash that inherited a `failed_operation` from an earlier build test in the same+ turn, because that latch is the only thing that would otherwise author the reply. A crash with+ no latch already renders the recoverable-failure text, which names an error reference this+ would drop. The latch itself is kept: it still drives the unverified/unapplied terminal state.+ """+ if failed_operation is None:+ return None+ if turn_outcome is None or turn_outcome.terminal_reason != UNEXPECTED_ERROR_TERMINAL_REASON:+ return None+ # Only the identity is known here. ``workflow_persisted`` is always False under staging, which+ # is the population this path serves, and ``last_workflow`` is stamped version=1 by the YAML+ # parse rather than carrying the workflow's real version, so both would state something about+ # the turn that this site cannot observe.+ return InterruptedTurnFacts(workflow_permanent_id=ctx.workflow_permanent_id)++def _assemble_terminal_envelope_safe(*,response_type: str,@@ -1640,6 +1665,7 @@ def _assemble_terminal_envelope_safe(failed_operation: BuildTestFailedOperation | None = None,connect_failure: BuildTestConnectFailure | None = None,proposal_present: bool = False,+ interruption: InterruptedTurnFacts | None = None,) -> dict[str, Any] | None:try:envelope = assemble_terminal_envelope(@@ -1658,6 +1684,7 @@ def _assemble_terminal_envelope_safe(failed_operation=failed_operation,connect_failure=connect_failure,proposal_present=proposal_present,+ interruption=interruption,)except Exception:LOG.warning("copilot terminal envelope assembly failed", exc_info=True)@@ -1887,9 +1914,12 @@ def _make_agent_result(failed_operation=failed_operation,connect_failure=connect_failure,proposal_present=result_carries_workflow,+ interruption=_crash_exit_interruption(ctx, turn_outcome, failed_operation),)if terminal_envelope is not None and (- terminal_envelope.get("failed_operation") is not None or terminal_envelope.get("connect_failure") is not None+ terminal_envelope.get("failed_operation") is not None+ or terminal_envelope.get("connect_failure") is not None+ or terminal_envelope.get("interruption") is not None):envelope = TerminalOutcomeEnvelope.model_validate(terminal_envelope)terminal_message, replaced = render_terminal_message(@@ -3212,7 +3242,7 @@ def _build_unexpected_error_exit_result(default_reply=default_reply,unvalidated_reply=_UNEXPECTED_ERROR_REPLY_UNVALIDATED,tested_reply=_UNEXPECTED_ERROR_REPLY_TESTED,- terminal_reason="unexpected_error",+ terminal_reason=UNEXPECTED_ERROR_TERMINAL_REASON,)LOG.warning("Copilot unexpected error translated to recoverable reply",diff --git a/skyvern/forge/sdk/copilot/terminal_envelope.py b/skyvern/forge/sdk/copilot/terminal_envelope.pyindex 84d7e5cf7..3dc390f6f 100644--- a/skyvern/forge/sdk/copilot/terminal_envelope.py+++ b/skyvern/forge/sdk/copilot/terminal_envelope.py@@ -96,6 +96,7 @@ def assemble_terminal_envelope(failed_operation: BuildTestFailedOperation | None = None,connect_failure: BuildTestConnectFailure | None = None,proposal_present: bool = False,+ interruption: InterruptedTurnFacts | None = None,) -> TerminalOutcomeEnvelope | None:run_outcome = select_run_outcome_anchor(run_outcomes)run_verdict = run_outcome.verdict if run_outcome is not None else None@@ -155,6 +156,7 @@ def assemble_terminal_envelope(failed_operation=failed_operation,connect_failure=connect_failure,proposal_present=proposal_present,+ interruption=interruption,)@@ -208,7 +210,7 @@ def interrupted_terminal_envelope(facts: InterruptedTurnFacts | None = None) ->)-def render_interrupted_message(facts: InterruptedTurnFacts | None = None) -> str:+def render_interrupted_message(facts: InterruptedTurnFacts | None = None, *, proposal_present: bool = False) -> str:"""User-facing copy for an interrupted turn: what is known, and never why it stopped."""message = INTERRUPTED_TERMINAL_HEADLINEif facts is not None:@@ -228,6 +230,10 @@ def render_interrupted_message(facts: InterruptedTurnFacts | None = None) -> strmessage = _append_sentence(message, f"Last recorded build-test phase: {facts.last_recorded_build_test_phase}.")+ if proposal_present:+ # The Accept/Discard card stays on screen for an untested draft, so a message that did not+ # mention it would contradict what the user is looking at.+ message = _append_sentence(message, "The untested draft is available for review.")return _append_sentence(message, INTERRUPTED_TERMINAL_RETRY)@@ -258,6 +264,11 @@ def render_terminal_message(envelope: TerminalOutcomeEnvelope, agent_message: streturn agent_message, Falsemessage = _append_sentence(message, f"{pending_question_intro}: {agent_message}")return message, True+ # A crash and a failed test are different endings, and the latch a crash inherits from an+ # earlier build test in the same turn would otherwise render both with the same sentence.+ # A connect failure still outranks this: it names identities the operator needs.+ if envelope.interruption is not None and not cancelled:+ return render_interrupted_message(envelope.interruption, proposal_present=envelope.proposal_present), Trueif envelope.failed_operation is not None and not cancelled:message = "I stopped after a browser operation failed while testing the workflow."if envelope.proposal_present:diff --git a/skyvern/forge/sdk/copilot/turn_outcome.py b/skyvern/forge/sdk/copilot/turn_outcome.pyindex 0862aaec2..c2d46d910 100644--- a/skyvern/forge/sdk/copilot/turn_outcome.py+++ b/skyvern/forge/sdk/copilot/turn_outcome.py@@ -22,6 +22,7 @@ IDENTICAL_REPLY_BLOCKED_TERMINAL_REASON = "identical_reply_blocked"# and stamps RECOVER itself, so that value never reaches the labelers here. See# ROUTE_OWNED_TERMINAL_REASONS in dev_scripts/replay_turn_outcome_kind.py for the full split.CANCEL_TERMINAL_REASON = "cancel"+UNEXPECTED_ERROR_TERMINAL_REASON = "unexpected_error"CopilotComposerMode = Literal["ask", "build", "code"]diff --git a/tests/unit/test_copilot_terminal_envelope.py b/tests/unit/test_copilot_terminal_envelope.pyindex 8da2aa191..31482123c 100644--- a/tests/unit/test_copilot_terminal_envelope.py+++ b/tests/unit/test_copilot_terminal_envelope.py@@ -15,12 +15,18 @@ from skyvern.forge.sdk.copilot.blocker_signal import (CopilotToolBlockerSignal,)from skyvern.forge.sdk.copilot.build_test_connect_failure import build_test_connect_failure_sentence-from skyvern.forge.sdk.copilot.build_test_outcome import BuildTestConnectFailure, BuildTestFailedOperation+from skyvern.forge.sdk.copilot.build_test_outcome import (+ BuildTestConnectFailure,+ BuildTestFailedOperation,+ RecordedBuildTestOutcome,+)from skyvern.forge.sdk.copilot.config import CopilotConfigfrom skyvern.forge.sdk.copilot.run_outcome import RecordedRunOutcomefrom skyvern.forge.sdk.copilot.secret_scrub import clear_session_scrub_values, register_secret_scrub_valuefrom skyvern.forge.sdk.copilot.terminal_envelope import (+ INTERRUPTED_TERMINAL_HEADLINE,MINIMAL_HONEST_STOP,+ InterruptedTurnFacts,TerminalOutcomeEnvelope,assemble_terminal_envelope,finalize_applied_state,@@ -251,6 +257,79 @@ def test_connect_failure_owns_terminal_over_earlier_failed_operation() -> None:assert "fresh browser session" in message+def test_crash_exit_renders_interrupted_not_the_failed_operation_sentence() -> None:+ envelope = _assemble(+ proposal_disposition="review_untested",+ failed_operation=BuildTestFailedOperation(kind="browser_operation_failed"),+ proposal_present=True,+ interruption=InterruptedTurnFacts(workflow_permanent_id="wpid_1", workflow_version=4),+ )+ message, replaced = render_terminal_message(envelope, "The code run failed.", cancelled=False)++ assert replaced is True+ assert message.startswith(INTERRUPTED_TERMINAL_HEADLINE)+ assert "browser operation failed" not in message.lower()+ assert "wpid_1" in message+ # The Accept/Discard card is still on screen, so the copy must not read as if it were gone.+ assert "The untested draft is available for review." in message+ # The latch is not dropped: it still drives the unverified/unapplied terminal state.+ assert envelope.failed_operation is not None+ assert envelope.verified is False+ assert envelope.workflow_applied is False+++def test_failed_operation_without_a_crash_keeps_its_own_sentence() -> None:+ envelope = _assemble(+ proposal_disposition="review_untested",+ failed_operation=BuildTestFailedOperation(kind="browser_operation_failed"),+ proposal_present=True,+ )+ message, replaced = render_terminal_message(envelope, "The code run failed.", cancelled=False)++ assert replaced is True+ assert "browser operation failed" in message.lower()+ assert INTERRUPTED_TERMINAL_HEADLINE not in message+++def test_unexpected_error_exit_renders_interrupted_and_keeps_its_terminal_reason() -> None:+ ctx = make_copilot_ctx(+ latest_recorded_build_test_outcome=RecordedBuildTestOutcome(+ phase="persisted_block_run",+ attempted_tool="update_and_run_blocks",+ verdict="repairable_failure",+ reason_code="runtime_block_failure",+ failed_operation=BuildTestFailedOperation(kind="browser_operation_failed"),+ ),+ # The YAML parse stamps this constant whatever the workflow's real version is, so reading+ # it here would report version 1 on every crash.+ last_workflow=SimpleNamespace(version=1),+ )++ result = agent_module._build_unexpected_error_exit_result(ctx, None, error=RuntimeError("boom"))++ assert result.turn_outcome is not None+ assert result.turn_outcome.terminal_reason == "unexpected_error"+ assert result.user_response.startswith(INTERRUPTED_TERMINAL_HEADLINE)+ assert "browser operation failed" not in result.user_response.lower()+ # Neither fact is observable at a crash site, so the copy must not state either one.+ assert "version 1" not in result.user_response+ assert "were not saved" not in result.user_response+++def test_connect_failure_still_outranks_a_crash_exit() -> None:+ envelope = _assemble(+ proposal_disposition="review_untested",+ connect_failure=BuildTestConnectFailure(state="cdp_connect_failed", browser_session_id="pbs_1"),+ proposal_present=True,+ interruption=InterruptedTurnFacts(workflow_permanent_id="wpid_1"),+ )+ message, replaced = render_terminal_message(envelope, "The code run failed.", cancelled=False)++ assert replaced is True+ assert "cdp_connect_failed" in message+ assert INTERRUPTED_TERMINAL_HEADLINE not in message++@pytest.mark.parametrize("capacity_cause", ["deadline_expired", "max_turns_exceeded"])def test_connect_failure_does_not_overwrite_capacity_terminal_cause(capacity_cause: str) -> None:failure = BuildTestConnectFailure(state="cdp_connect_failed", browser_session_id="pbs_1")