Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
feat(SKY-9441): offer WIP workflow on cancel (#5787)
Andrew Neilson182171d2ce22
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsxindex 6fad9d7d7..181b326f4 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx@@ -223,6 +223,10 @@ export function WorkflowCopilotChat({const streamingAbortController = useRef<AbortController | null>(null);const pendingMessageId = useRef<string | null>(null);const pendingCancelToken = useRef<string | null>(null);+ const cancelSafetyTimer = useRef<ReturnType<typeof setTimeout> | null>(null);+ // Backend cancel watcher polls Redis: a turn can complete normally between+ // the cancel POST and the watcher firing, so the frontend must remember it.+ const cancelInFlightController = useRef<AbortController | null>(null);const [workflowCopilotChatId, setWorkflowCopilotChatId] = useState<string | null>(null);@@ -232,6 +236,15 @@ export function WorkflowCopilotChat({useEffect(() => {workflowCopilotChatIdRef.current = workflowCopilotChatId;}, [workflowCopilotChatId]);+ useEffect(() => {+ return () => {+ streamingAbortController.current?.abort();+ if (cancelSafetyTimer.current !== null) {+ clearTimeout(cancelSafetyTimer.current);+ cancelSafetyTimer.current = null;+ }+ };+ }, []);const [size, setSize] = useState({width: DEFAULT_WINDOW_WIDTH,height: DEFAULT_WINDOW_HEIGHT,@@ -572,40 +585,56 @@ export function WorkflowCopilotChat({}, [credentialGetter, workflowPermanentId]);const cancelSend = useCallback(async () => {- if (!streamingAbortController.current) return;+ // Capture upfront so the 15s timer below can't latch onto a next turn's controller.+ const controllerAtCancel = streamingAbortController.current;+ if (!controllerAtCancel) return;const cancelToken = pendingCancelToken.current;pendingCancelToken.current = null;if (!cancelToken) return;- // Backend persists the user message + a matching AI bubble on cancel,- // so chat history reload stays consistent. We don't remove the user's- // pending message either — reload would otherwise show only the AI- // cancellation reply with no prompt above it.- setMessages((prev) => [- ...prev,- {- id: `${Date.now()}-cancel`,- sender: "ai",- content: "Cancelled by user.",- timestamp: new Date().toISOString(),- },- ]);+ setProcessingStatus("Cancelling...");+ setLatestNarration("");+ setToolActivity([]);+ cancelInFlightController.current = controllerAtCancel;- streamingAbortController.current.abort();+ const appendCancelledBubble = () => {+ setMessages((prev) => [+ ...prev,+ {+ id: `${Date.now()}-cancel`,+ sender: "ai",+ content: "Cancelled by user.",+ timestamp: new Date().toISOString(),+ },+ ]);+ };try {const client = await getClient(credentialGetter, "sans-api-v1");await client.post<void>("/workflow/copilot/cancel", {cancel_token: cancelToken,} as WorkflowCopilotCancelRequest);+ // Safety net: if the SSE channel never resolves, surface a fallback+ // bubble and abort so handleSend's finally clears "Cancelling...".+ if (cancelSafetyTimer.current !== null) {+ clearTimeout(cancelSafetyTimer.current);+ }+ cancelSafetyTimer.current = setTimeout(() => {+ cancelSafetyTimer.current = null;+ if (streamingAbortController.current !== controllerAtCancel) return;+ appendCancelledBubble();+ controllerAtCancel.abort();+ }, 15_000);} catch (error) {// 503 (Redis disabled) or network failure: client-side abort still// gives the user immediate feedback; the backend will run to// completion in that environment. Log so we can spot it in dev.console.warn("Workflow copilot cancel POST failed", error);+ controllerAtCancel.abort();+ appendCancelledBubble();}- }, [credentialGetter]);+ }, [credentialGetter, setToolActivity]);const cancelQueuedPrompt = useCallback(() => {if (!queuedPrompt) {@@ -831,10 +860,14 @@ export function WorkflowCopilotChat({};setMessages((prev) => [...prev, aiMessage]);+ const userCancelledThisTurn =+ cancelInFlightController.current === abortController;if (response.updated_workflow &&autoAccept &&- !response.unvalidated+ !response.unvalidated &&+ !response.cancelled &&+ !userCancelledThisTurn) {applyWorkflowUpdate(response.updated_workflow);} else {@@ -920,6 +953,13 @@ export function WorkflowCopilotChat({if (streamingAbortController.current === abortController) {streamingAbortController.current = null;}+ if (cancelInFlightController.current === abortController) {+ cancelInFlightController.current = null;+ }+ if (cancelSafetyTimer.current !== null) {+ clearTimeout(cancelSafetyTimer.current);+ cancelSafetyTimer.current = null;+ }pendingMessageId.current = null;pendingCancelToken.current = null;setIsLoading(false);diff --git a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.tsindex 095ef3931..622e88027 100644--- a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts@@ -85,6 +85,8 @@ export interface WorkflowCopilotStreamResponseUpdate {response_time: string;// Clients must NOT auto-apply when true; render Accept/Reject explicitly.unvalidated?: boolean;+ // Same auto-apply guard as unvalidated; cancel forces explicit review.+ cancelled?: boolean;}export interface WorkflowCopilotStreamErrorUpdate {diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.pyindex 0b60a0142..3a94aa802 100644--- a/skyvern/forge/sdk/copilot/agent.py+++ b/skyvern/forge/sdk/copilot/agent.py@@ -273,6 +273,12 @@ _UNEXPECTED_ERROR_REPLY_UNVALIDATED = (_UNEXPECTED_ERROR_REPLY_TESTED = ("I hit an unexpected issue, but I have a tested draft for you. Accept it to save, or discard.")+_CANCEL_REPLY_DEFAULT = "Cancelled by user."+_CANCEL_REPLY_UNVALIDATED = (+ "Cancelled. I have a draft workflow you can keep — accept it to save "+ "(note: it hasn't been verified end-to-end), or discard."+)+_CANCEL_REPLY_TESTED = "Cancelled. I have a tested draft for you. Accept it to save, or discard."def _build_wip_exit_result(@@ -282,6 +288,7 @@ def _build_wip_exit_result(default_reply: str,unvalidated_reply: str,tested_reply: str,+ cancelled: bool = False,) -> AgentResult:"""Selected non-success exits surface the most recent successfully parsed workflow."""# ``last_test_ok=None`` covers both "test never ran" and "test ran with@@ -303,8 +310,9 @@ def _build_wip_exit_result(workflow_was_persisted=ctx.workflow_persisted,total_tokens=ctx.total_tokens_used,unvalidated=unvalidated,+ cancelled=cancelled,)- return _build_exit_result(ctx, default_reply, global_llm_context)+ return _build_exit_result(ctx, default_reply, global_llm_context, cancelled=cancelled)def _build_timeout_exit_result(ctx: CopilotContext, global_llm_context: str | None) -> AgentResult:@@ -337,6 +345,17 @@ def _build_unexpected_error_exit_result(ctx: CopilotContext, global_llm_context:)+def _build_cancel_exit_result(ctx: CopilotContext, global_llm_context: str | None) -> AgentResult:+ return _build_wip_exit_result(+ ctx,+ global_llm_context,+ default_reply=_CANCEL_REPLY_DEFAULT,+ unvalidated_reply=_CANCEL_REPLY_UNVALIDATED,+ tested_reply=_CANCEL_REPLY_TESTED,+ cancelled=True,+ )++def _translate_to_agent_result(result: RunResultStreaming,ctx: CopilotContext,@@ -668,7 +687,7 @@ async def run_copilot_agent(# Re-raising would leave the route with ``agent_result is None``# and skip its ``workflow_was_persisted`` rollback decision.LOG.info("Copilot run cancelled by user")- return _build_exit_result(ctx, "Cancelled by user.", global_llm_context, cancelled=True)+ return _build_cancel_exit_result(ctx, global_llm_context)except MaxTurnsExceeded:return _build_max_turns_exit_result(ctx, global_llm_context)except CopilotTotalTimeoutError:diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.pyindex a0b864803..e08cd412a 100644--- a/skyvern/forge/sdk/routes/workflow_copilot.py+++ b/skyvern/forge/sdk/routes/workflow_copilot.py@@ -193,8 +193,8 @@ async def _ensure_terminal_frame(stream: EventSourceStream, already_emitted: boodef _effective_auto_accept(auto_accept: bool | None, agent_result: object | None) -> bool:- """``unvalidated`` overrides ``auto_accept=True`` to force explicit Accept/Reject."""- if bool(getattr(agent_result, "unvalidated", False)):+ """``unvalidated`` and ``cancelled`` override ``auto_accept=True`` to force explicit Accept/Reject."""+ if bool(getattr(agent_result, "unvalidated", False)) or bool(getattr(agent_result, "cancelled", False)):return Falsereturn auto_accept is True@@ -216,6 +216,40 @@ async def _clear_proposed_workflow(chat: Any) -> None:)+def _build_proposed_workflow_data(updated_workflow: Workflow, agent_result: AgentResult) -> dict[str, Any]:+ proposed_data = dict(updated_workflow.model_dump(mode="json"))+ if agent_result.workflow_yaml:+ proposed_data["_copilot_yaml"] = agent_result.workflow_yaml+ if agent_result.unvalidated:+ proposed_data["_copilot_unvalidated"] = True+ return proposed_data+++async def _persist_proposed_workflow_state(chat: Any, agent_result: AgentResult, restored: bool) -> None:+ updated_workflow = agent_result.updated_workflow+ auto_accept_effective = _effective_auto_accept(chat.auto_accept, agent_result)+ if not auto_accept_effective and updated_workflow:+ await app.DATABASE.workflow_params.update_workflow_copilot_chat(+ organization_id=chat.organization_id,+ workflow_copilot_chat_id=chat.workflow_copilot_chat_id,+ proposed_workflow=_build_proposed_workflow_data(updated_workflow, agent_result),+ )+ elif chat.proposed_workflow is not None and (restored or agent_result.clear_proposed_workflow):+ # Null any persisted proposed_workflow the assistant just invalidated+ # so a reload does not resurrect a stale Accept/Reject card. Runs+ # under both auto_accept values — a stale proposal can survive an+ # auto-accept toggle.+ await _clear_proposed_workflow(chat)+ elif (+ auto_accept_effective+ and chat.proposed_workflow is not None+ and chat.proposed_workflow.get("_copilot_unvalidated") is True+ ):+ # The leftover unvalidated proposal is no longer attached to the chat+ # tail; clear it so reload doesn't resurrect a stale Accept/Reject card.+ await _clear_proposed_workflow(chat)++async def _persist_cancel_turn(stream: EventSourceStream,chat: Any,@@ -224,14 +258,36 @@ async def _persist_cancel_turn(user_message: str,agent_result: AgentResult | None,) -> None:- """Persist a cancelled turn and emit a terminal SSE error frame.+ """Persist a cancelled turn and emit a terminal SSE response frame.Pass the agent's ``AgentResult`` for cancels during the agent run sorollback uses the same ``workflow_was_persisted`` source of truth asthe success path; pass ``None`` for pre-agent cancels."""- if agent_result is not None and _should_restore_persisted_workflow(chat.auto_accept, agent_result):- await asyncio.shield(_restore_workflow_definition(original_workflow, organization_id))+ if agent_result is None:+ user_response = "Cancelled by user."+ updated_workflow = None+ updated_global_llm_context = None+ total_tokens = None+ response_type = "REPLY"+ unvalidated = False+ if chat.proposed_workflow is not None:+ await asyncio.shield(_clear_proposed_workflow(chat))+ else:+ restored = _should_restore_persisted_workflow(chat.auto_accept, agent_result)+ if restored:+ await asyncio.shield(_restore_workflow_definition(original_workflow, organization_id))+ if agent_result.updated_workflow is None and chat.proposed_workflow is not None:+ await asyncio.shield(_clear_proposed_workflow(chat))+ else:+ await asyncio.shield(_persist_proposed_workflow_state(chat, agent_result, restored))+ user_response = agent_result.user_response+ updated_workflow = agent_result.updated_workflow+ updated_global_llm_context = agent_result.global_llm_context+ total_tokens = getattr(agent_result, "total_tokens", None)+ response_type = getattr(agent_result, "response_type", "REPLY")+ unvalidated = bool(getattr(agent_result, "unvalidated", False))+await asyncio.shield(app.DATABASE.workflow_params.create_workflow_copilot_chat_message(organization_id=chat.organization_id,@@ -240,23 +296,37 @@ async def _persist_cancel_turn(content=user_message,))- await asyncio.shield(+ assistant_message = await asyncio.shield(app.DATABASE.workflow_params.create_workflow_copilot_chat_message(organization_id=chat.organization_id,workflow_copilot_chat_id=chat.workflow_copilot_chat_id,sender=WorkflowCopilotChatSender.AI,- content="Cancelled by user.",+ content=user_response,+ global_llm_context=updated_global_llm_context,))- # Best-effort: client may have already aborted, in which case stream.send- # silently drops. The optimistic FE bubble already shows the cancellation.- with contextlib.suppress(Exception):- await stream.send(- WorkflowCopilotStreamErrorUpdate(- type=WorkflowCopilotStreamMessageType.ERROR,- error="Cancelled by user.",+ try:+ await asyncio.shield(+ stream.send(+ WorkflowCopilotStreamResponseUpdate(+ type=WorkflowCopilotStreamMessageType.RESPONSE,+ workflow_copilot_chat_id=chat.workflow_copilot_chat_id,+ message=user_response,+ updated_workflow=updated_workflow.model_dump(mode="json") if updated_workflow else None,+ response_time=assistant_message.created_at,+ total_tokens=total_tokens,+ response_type=response_type,+ unvalidated=unvalidated,+ cancelled=True,+ )))+ except BaseException:+ LOG.warning(+ "Failed to send cancel RESPONSE frame; persistence already committed",+ workflow_copilot_chat_id=chat.workflow_copilot_chat_id,+ exc_info=True,+ )async def _finalise_normal_turn(@@ -293,32 +363,7 @@ async def _finalise_normal_turn(if restored:await _restore_workflow_definition(original_workflow, organization_id)- auto_accept_effective = _effective_auto_accept(chat.auto_accept, agent_result)- if not auto_accept_effective and updated_workflow:- proposed_data = updated_workflow.model_dump(mode="json")- if agent_result.workflow_yaml:- proposed_data["_copilot_yaml"] = agent_result.workflow_yaml- if agent_result.unvalidated:- proposed_data["_copilot_unvalidated"] = True- await app.DATABASE.workflow_params.update_workflow_copilot_chat(- organization_id=chat.organization_id,- workflow_copilot_chat_id=chat.workflow_copilot_chat_id,- proposed_workflow=proposed_data,- )- elif chat.proposed_workflow is not None and (restored or agent_result.clear_proposed_workflow):- # Null any persisted proposed_workflow the assistant just invalidated- # so a reload does not resurrect a stale Accept/Reject card. Runs- # under both auto_accept values — a stale proposal can survive an- # auto-accept toggle.- await _clear_proposed_workflow(chat)- elif (- auto_accept_effective- and chat.proposed_workflow is not None- and chat.proposed_workflow.get("_copilot_unvalidated") is True- ):- # The leftover unvalidated proposal is no longer attached to the chat- # tail; clear it so reload doesn't resurrect a stale Accept/Reject card.- await _clear_proposed_workflow(chat)+ await _persist_proposed_workflow_state(chat, agent_result, restored)await app.DATABASE.workflow_params.create_workflow_copilot_chat_message(organization_id=chat.organization_id,@@ -1237,6 +1282,7 @@ async def _new_copilot_chat_post("Workflow copilot v2 cancelled by user during pre-agent setup",workflow_copilot_chat_id=chat_request.workflow_copilot_chat_id,)+ returnelse:# Operational cancel (worker shutdown, deploy drain) or a# cancel that arrived after _finalise_normal_turn started@@ -1248,7 +1294,7 @@ async def _new_copilot_chat_post(workflow_copilot_chat_id=chat_request.workflow_copilot_chat_id,user_cancel_observed=user_cancel_observed[0],)- raise+ raiseexcept Exception as exc:if chat is not None and _should_restore_persisted_workflow(chat.auto_accept, agent_result):await _restore_workflow_definition(original_workflow, organization.organization_id)diff --git a/skyvern/forge/sdk/schemas/workflow_copilot.py b/skyvern/forge/sdk/schemas/workflow_copilot.pyindex 9f91020a8..4f4771dc9 100644--- a/skyvern/forge/sdk/schemas/workflow_copilot.py+++ b/skyvern/forge/sdk/schemas/workflow_copilot.py@@ -121,6 +121,10 @@ class WorkflowCopilotStreamResponseUpdate(BaseModel):False,description="When true, clients must not auto-apply; render Accept/Reject explicitly.",)+ cancelled: bool = Field(+ False,+ description="When true, this RESPONSE was emitted by a user cancel; clients must not auto-apply.",+ )class WorkflowCopilotStreamErrorUpdate(BaseModel):diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.pyindex 1c8e080ea..28fd53a27 100644--- a/tests/unit/test_copilot_agent_helpers.py+++ b/tests/unit/test_copilot_agent_helpers.py@@ -183,6 +183,7 @@ class TestShouldRestorePersistedWorkflow:r.workflow_was_persisted = persistedr.updated_workflow = updated_workflowr.unvalidated = False+ r.cancelled = Falsereturn rdef test_restores_when_no_proposal_even_under_auto_accept(self) -> None:diff --git a/tests/unit/test_copilot_timeout_wip.py b/tests/unit/test_copilot_timeout_wip.pyindex 95d64cf0d..030bad801 100644--- a/tests/unit/test_copilot_timeout_wip.py+++ b/tests/unit/test_copilot_timeout_wip.py@@ -5,6 +5,9 @@ from __future__ import annotationsfrom unittest.mock import MagicMockfrom skyvern.forge.sdk.copilot.agent import (+ _CANCEL_REPLY_DEFAULT,+ _CANCEL_REPLY_TESTED,+ _CANCEL_REPLY_UNVALIDATED,_MAX_TURNS_REPLY_DEFAULT,_MAX_TURNS_REPLY_TESTED,_MAX_TURNS_REPLY_UNVALIDATED,@@ -14,6 +17,7 @@ from skyvern.forge.sdk.copilot.agent import (_UNEXPECTED_ERROR_REPLY_DEFAULT,_UNEXPECTED_ERROR_REPLY_TESTED,_UNEXPECTED_ERROR_REPLY_UNVALIDATED,+ _build_cancel_exit_result,_build_max_turns_exit_result,_build_timeout_exit_result,_build_unexpected_error_exit_result,@@ -235,3 +239,52 @@ class TestBuildUnexpectedErrorExitResult:assert result.workflow_yaml is Noneassert result.unvalidated is Falseassert result.user_response == _UNEXPECTED_ERROR_REPLY_DEFAULT+++class TestBuildCancelExitResult:+ def test_no_workflow_falls_back_to_cancel_reply(self) -> None:+ ctx = _ctx(last_workflow=None, last_workflow_yaml=None, last_test_ok=None)++ result = _build_cancel_exit_result(ctx, global_llm_context=None)++ assert result.cancelled is True+ assert result.updated_workflow is None+ assert result.workflow_yaml is None+ assert result.unvalidated is False+ assert result.user_response == _CANCEL_REPLY_DEFAULT++ def test_untested_workflow_surfaces_as_unvalidated_cancel_wip(self) -> None:+ wf = MagicMock(name="wf")+ ctx = _ctx(last_workflow=wf, last_workflow_yaml="version: '1.0'", last_test_ok=None)++ result = _build_cancel_exit_result(ctx, global_llm_context=None)++ assert result.cancelled is True+ assert result.updated_workflow is wf+ assert result.workflow_yaml == "version: '1.0'"+ assert result.unvalidated is True+ assert result.user_response == _CANCEL_REPLY_UNVALIDATED++ def test_passing_test_surfaces_as_tested_cancel_proposal(self) -> None:+ wf = MagicMock(name="wf")+ ctx = _ctx(last_workflow=wf, last_workflow_yaml="version: '1.0'", last_test_ok=True)++ result = _build_cancel_exit_result(ctx, global_llm_context=None)++ assert result.cancelled is True+ assert result.updated_workflow is wf+ assert result.workflow_yaml == "version: '1.0'"+ assert result.unvalidated is False+ assert result.user_response == _CANCEL_REPLY_TESTED++ def test_failed_test_drops_cancel_proposal(self) -> None:+ wf = MagicMock(name="wf")+ ctx = _ctx(last_workflow=wf, last_workflow_yaml="version: '1.0'", last_test_ok=False)++ result = _build_cancel_exit_result(ctx, global_llm_context=None)++ assert result.cancelled is True+ assert result.updated_workflow is None+ assert result.workflow_yaml is None+ assert result.unvalidated is False+ assert result.user_response == _CANCEL_REPLY_DEFAULTdiff --git a/tests/unit/test_workflow_copilot_cancel.py b/tests/unit/test_workflow_copilot_cancel.pyindex afb761d12..4e0185e9b 100644--- a/tests/unit/test_workflow_copilot_cancel.py+++ b/tests/unit/test_workflow_copilot_cancel.py@@ -11,7 +11,7 @@ Covers:the same source of truth on cancel as it does on success).- The route's success-path branch on ``agent_result.cancelled`` runs thecancel-specific persistence (rollback + user msg + ``Cancelled by user.``- AI msg + ERROR frame) and skips the normal RESPONSE finalisation.+ AI msg + RESPONSE frame) and skips proposal persistence when there is no WIP.- ``/workflow/copilot/cancel`` returns 503 when the Redis cache is absent and204 + the expected key/TTL when it is present.- An operational cancel (``task.cancel()`` without ``user_cancel_observed[0]``@@ -36,6 +36,7 @@ from skyvern.forge.sdk.copilot.context import AgentResult, CopilotContextfrom skyvern.forge.sdk.routes.workflow_copilot import (COPILOT_CANCEL_TTL,_copilot_cancel_key,+ _persist_cancel_turn,_watch_for_cancel,workflow_copilot_cancel,workflow_copilot_chat_post,@@ -228,38 +229,34 @@ def _setup_route_mocks(return restore_mock, workflow_params-async def test_route_cancel_branch_persists_user_and_cancelled_messages(- monkeypatch: pytest.MonkeyPatch,-) -> None:- """``agent_result.cancelled=True`` -> rollback + USER msg + ``Cancelled by user.`` AI msg + ERROR frame."""- monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)- captured = _install_fake_create(monkeypatch)-- chat = SimpleNamespace(+def _make_chat(*, proposed_workflow: Any = None, auto_accept: bool) -> SimpleNamespace:+ return SimpleNamespace(workflow_copilot_chat_id="chat-1",workflow_permanent_id="wpid-1",organization_id="org-1",- proposed_workflow=None,- auto_accept=False,+ proposed_workflow=proposed_workflow,+ auto_accept=auto_accept,)- original_workflow = SimpleNamespace(+++def _make_original_workflow() -> SimpleNamespace:+ return SimpleNamespace(workflow_id="wf-canonical",title="Original",description="Original description",workflow_definition=None,)- agent_result = SimpleNamespace(- user_response="Cancelled by user.",- updated_workflow=None,- global_llm_context=None,- workflow_yaml=None,- workflow_was_persisted=True,- clear_proposed_workflow=False,- cancelled=True,- total_tokens=None,- response_type="REPLY",- )+++async def _drive_cancel_route(+ monkeypatch: pytest.MonkeyPatch,+ chat: SimpleNamespace,+ original_workflow: SimpleNamespace,+ agent_result: SimpleNamespace,+) -> tuple[AsyncMock, SimpleNamespace, list[Any]]:+ """Run a single chat-post + handler turn and return (restore_mock, workflow_params, sent_payloads)."""+ monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)+ captured = _install_fake_create(monkeypatch)restore_mock, workflow_params = _setup_route_mocks(monkeypatch, chat, original_workflow, agent_result)request = MagicMock()@@ -282,8 +279,34 @@ async def test_route_cancel_branch_persists_user_and_cancelled_messages(handler = captured["handler"]assert callable(handler)await handler(stream)+ # Cancel turn always emits exactly two chat rows: the user prompt and the+ # AI cancellation reply. Guard against a future regression that double-inserts.+ assert workflow_params.create_workflow_copilot_chat_message.await_count == 2+ return restore_mock, workflow_params, sent_payloads+++async def test_route_cancel_branch_persists_user_and_cancelled_messages(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """``agent_result.cancelled=True`` with no WIP -> rollback + cancellation RESPONSE."""+ chat = _make_chat(auto_accept=False)+ original_workflow = _make_original_workflow()+ agent_result = SimpleNamespace(+ user_response="Cancelled by user.",+ updated_workflow=None,+ global_llm_context=None,+ workflow_yaml=None,+ workflow_was_persisted=True,+ clear_proposed_workflow=False,+ cancelled=True,+ total_tokens=None,+ response_type="REPLY",+ )+ restore_mock, workflow_params, sent_payloads = await _drive_cancel_route(+ monkeypatch, chat, original_workflow, agent_result+ )- # Rollback fired (workflow_was_persisted=True, auto_accept=False, no proposal).restore_mock.assert_awaited_once()# Two chat-message inserts: user msg + Cancelled by user. AI msg.@@ -295,14 +318,210 @@ async def test_route_cancel_branch_persists_user_and_cancelled_messages(assert "please update" in contentsassert "Cancelled by user." in contents- # Normal RESPONSE finalisation did NOT run: no proposed_workflow update,- # no RESPONSE frame.+ # No proposed_workflow update when the cancelled result has no WIP.workflow_params.update_workflow_copilot_chat.assert_not_awaited()- response_types = [getattr(p, "type", None) for p in sent_payloads]- assert WorkflowCopilotStreamMessageType.RESPONSE not in response_types- # An ERROR frame carrying "Cancelled by user." was emitted.+ response_frames = [+ p for p in sent_payloads if getattr(p, "type", None) == WorkflowCopilotStreamMessageType.RESPONSE+ ]+ assert len(response_frames) == 1+ assert response_frames[0].message == "Cancelled by user."+ assert response_frames[0].updated_workflow is None+ assert response_frames[0].cancelled is True++ error_frames = [p for p in sent_payloads if getattr(p, "type", None) == WorkflowCopilotStreamMessageType.ERROR]+ assert error_frames == []+++async def test_route_cancel_branch_persists_wip_proposal_and_response_frame(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Cancelled WIP -> proposed_workflow + normal RESPONSE so Review/Accept/Reject can render."""+ chat = _make_chat(auto_accept=True)+ original_workflow = _make_original_workflow()+ updated_workflow = MagicMock()+ updated_workflow.model_dump.return_value = {"workflow_id": "wf-canonical", "title": "Draft"}+ agent_result = SimpleNamespace(+ user_response="Cancelled. I have a draft workflow you can keep.",+ updated_workflow=updated_workflow,+ global_llm_context=None,+ workflow_yaml="title: Draft",+ workflow_was_persisted=True,+ clear_proposed_workflow=False,+ cancelled=True,+ total_tokens=123,+ response_type="REPLY",+ unvalidated=True,+ )+ restore_mock, workflow_params, sent_payloads = await _drive_cancel_route(+ monkeypatch, chat, original_workflow, agent_result+ )++ restore_mock.assert_awaited_once()++ workflow_params.update_workflow_copilot_chat.assert_awaited_once()+ proposed_workflow = workflow_params.update_workflow_copilot_chat.await_args.kwargs["proposed_workflow"]+ assert proposed_workflow == {+ "workflow_id": "wf-canonical",+ "title": "Draft",+ "_copilot_yaml": "title: Draft",+ "_copilot_unvalidated": True,+ }++ insert_calls = workflow_params.create_workflow_copilot_chat_message.await_args_list+ contents = [c.kwargs.get("content") for c in insert_calls]+ assert "please update" in contents+ assert "Cancelled. I have a draft workflow you can keep." in contents++ response_frames = [+ p for p in sent_payloads if getattr(p, "type", None) == WorkflowCopilotStreamMessageType.RESPONSE+ ]+ assert len(response_frames) == 1+ frame = response_frames[0]+ assert frame.message == "Cancelled. I have a draft workflow you can keep."+ assert frame.updated_workflow == {"workflow_id": "wf-canonical", "title": "Draft"}+ assert frame.unvalidated is True+ assert frame.cancelled is True+error_frames = [p for p in sent_payloads if getattr(p, "type", None) == WorkflowCopilotStreamMessageType.ERROR]- assert any(getattr(f, "error", "") == "Cancelled by user." for f in error_frames)+ assert error_frames == []+++async def test_route_cancel_tested_wip_with_auto_accept_still_persists_proposal(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Cancel + tested WIP + auto_accept=True must not auto-apply: cancel always forces review."""+ chat = _make_chat(auto_accept=True)+ original_workflow = _make_original_workflow()+ updated_workflow = MagicMock()+ updated_workflow.model_dump.return_value = {"workflow_id": "wf-canonical", "title": "Tested Draft"}+ agent_result = SimpleNamespace(+ user_response="Cancelled. I have a tested draft for you. Accept it to save, or discard.",+ updated_workflow=updated_workflow,+ global_llm_context=None,+ workflow_yaml="title: Tested Draft",+ workflow_was_persisted=True,+ clear_proposed_workflow=False,+ cancelled=True,+ total_tokens=123,+ response_type="REPLY",+ unvalidated=False,+ )+ restore_mock, workflow_params, sent_payloads = await _drive_cancel_route(+ monkeypatch, chat, original_workflow, agent_result+ )++ restore_mock.assert_awaited_once()++ workflow_params.update_workflow_copilot_chat.assert_awaited_once()+ proposed_workflow = workflow_params.update_workflow_copilot_chat.await_args.kwargs["proposed_workflow"]+ assert proposed_workflow == {+ "workflow_id": "wf-canonical",+ "title": "Tested Draft",+ "_copilot_yaml": "title: Tested Draft",+ }++ response_frames = [+ p for p in sent_payloads if getattr(p, "type", None) == WorkflowCopilotStreamMessageType.RESPONSE+ ]+ assert len(response_frames) == 1+ assert response_frames[0].unvalidated is False+ assert response_frames[0].cancelled is True+++async def test_route_cancel_clears_stale_proposed_workflow_when_no_wip(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Cancel with no WIP + chat already has stale proposed_workflow -> clear-stale branch nulls it."""+ chat = _make_chat(+ proposed_workflow={"workflow_id": "wf-canonical", "title": "Stale"},+ auto_accept=False,+ )+ original_workflow = _make_original_workflow()+ agent_result = SimpleNamespace(+ user_response="Cancelled by user.",+ updated_workflow=None,+ global_llm_context=None,+ workflow_yaml=None,+ workflow_was_persisted=True,+ clear_proposed_workflow=False,+ cancelled=True,+ total_tokens=None,+ response_type="REPLY",+ unvalidated=False,+ )+ restore_mock, workflow_params, _sent = await _drive_cancel_route(monkeypatch, chat, original_workflow, agent_result)++ restore_mock.assert_awaited_once()+ workflow_params.update_workflow_copilot_chat.assert_awaited_once()+ assert workflow_params.update_workflow_copilot_chat.await_args.kwargs["proposed_workflow"] is None+++async def test_route_cancel_clears_stale_proposed_workflow_when_no_wip_and_no_persistence(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Cancel + no WIP + workflow_was_persisted=False still clears any stale proposal."""+ chat = _make_chat(+ proposed_workflow={"workflow_id": "wf-canonical", "title": "Stale"},+ auto_accept=False,+ )+ original_workflow = _make_original_workflow()+ agent_result = SimpleNamespace(+ user_response="Cancelled by user.",+ updated_workflow=None,+ global_llm_context=None,+ workflow_yaml=None,+ workflow_was_persisted=False,+ clear_proposed_workflow=False,+ cancelled=True,+ total_tokens=None,+ response_type="REPLY",+ unvalidated=False,+ )+ restore_mock, workflow_params, _sent = await _drive_cancel_route(monkeypatch, chat, original_workflow, agent_result)++ restore_mock.assert_not_awaited()+ workflow_params.update_workflow_copilot_chat.assert_awaited_once()+ assert workflow_params.update_workflow_copilot_chat.await_args.kwargs["proposed_workflow"] is None+++async def test_pre_agent_cancel_clears_stale_proposed_workflow() -> None:+ """Pre-agent cancel (agent_result=None) must clear any stale proposal.++ Without this, reload reattaches the old card to the new "Cancelled by user." message.+ """+ chat = SimpleNamespace(+ organization_id="org-1",+ workflow_copilot_chat_id="chat-1",+ proposed_workflow={"workflow_id": "wf-canonical", "title": "Stale"},+ auto_accept=False,+ )+ workflow_params = SimpleNamespace(+ update_workflow_copilot_chat=AsyncMock(),+ create_workflow_copilot_chat_message=AsyncMock(+ return_value=SimpleNamespace(created_at=datetime(2026, 4, 27, tzinfo=timezone.utc))+ ),+ )+ app.DATABASE.workflow_params = workflow_params++ stream = MagicMock()+ stream.send = AsyncMock(return_value=True)++ await _persist_cancel_turn(+ stream=stream,+ chat=chat,+ organization_id="org-1",+ original_workflow=None,+ user_message="please update",+ agent_result=None,+ )++ workflow_params.update_workflow_copilot_chat.assert_awaited_once()+ assert workflow_params.update_workflow_copilot_chat.await_args.kwargs["proposed_workflow"] is None# ---------------------------------------------------------------------------diff --git a/tests/unit/test_workflow_copilot_route_helpers.py b/tests/unit/test_workflow_copilot_route_helpers.pyindex e9efd1f5f..08bb6d801 100644--- a/tests/unit/test_workflow_copilot_route_helpers.py+++ b/tests/unit/test_workflow_copilot_route_helpers.py@@ -10,6 +10,7 @@ hand-edit lands.from __future__ import annotations+from typing import Anyfrom unittest.mock import MagicMockfrom skyvern.forge.sdk.routes.workflow_copilot import (@@ -19,30 +20,48 @@ from skyvern.forge.sdk.routes.workflow_copilot import ()+def _agent_result(+ *,+ persisted: bool,+ unvalidated: bool = False,+ cancelled: bool = False,+ updated_workflow: Any = None,+ **kwargs: Any,+) -> MagicMock:+ """MagicMock with override flags explicitly set so a forgotten attr can't pass via MagicMock truthiness."""+ r = MagicMock()+ r.workflow_was_persisted = persisted+ r.unvalidated = unvalidated+ r.cancelled = cancelled+ r.updated_workflow = updated_workflow+ for k, v in kwargs.items():+ setattr(r, k, v)+ return r++class TestShouldRestorePersistedWorkflow:def test_restores_for_non_auto_accept_and_persisted_workflow(self) -> None:- agent_result = MagicMock()- agent_result.workflow_was_persisted = True+ agent_result = _agent_result(persisted=True)assert _should_restore_persisted_workflow(False, agent_result) is Trueassert _should_restore_persisted_workflow(None, agent_result) is Truedef test_does_not_restore_for_auto_accept_or_unpersisted_result(self) -> None:- persisted = MagicMock()- persisted.workflow_was_persisted = True- persisted.unvalidated = False- not_persisted = MagicMock()- not_persisted.workflow_was_persisted = False+ persisted = _agent_result(persisted=True, updated_workflow=MagicMock())+ not_persisted = _agent_result(persisted=False)assert _should_restore_persisted_workflow(True, persisted) is Falseassert _should_restore_persisted_workflow(False, not_persisted) is Falseassert _should_restore_persisted_workflow(False, None) is Falsedef test_unvalidated_timeout_wip_forces_rollback_under_auto_accept(self) -> None:- agent_result = MagicMock()- agent_result.workflow_was_persisted = True- agent_result.unvalidated = True- agent_result.updated_workflow = MagicMock()+ agent_result = _agent_result(persisted=True, unvalidated=True, updated_workflow=MagicMock())++ assert _should_restore_persisted_workflow(True, agent_result) is True+ assert _should_restore_persisted_workflow(False, agent_result) is True++ def test_cancelled_wip_forces_rollback_under_auto_accept(self) -> None:+ agent_result = _agent_result(persisted=True, cancelled=True, updated_workflow=MagicMock())assert _should_restore_persisted_workflow(True, agent_result) is Trueassert _should_restore_persisted_workflow(False, agent_result) is True@@ -52,13 +71,23 @@ class TestEffectiveAutoAccept:def test_unvalidated_overrides_auto_accept(self) -> None:unvalidated = MagicMock()unvalidated.unvalidated = True+ unvalidated.cancelled = Falseassert _effective_auto_accept(True, unvalidated) is Falseassert _effective_auto_accept(False, unvalidated) is False+ def test_cancelled_overrides_auto_accept(self) -> None:+ cancelled = MagicMock()+ cancelled.unvalidated = False+ cancelled.cancelled = True++ assert _effective_auto_accept(True, cancelled) is False+ assert _effective_auto_accept(False, cancelled) is False+def test_validated_proposal_respects_auto_accept_setting(self) -> None:validated = MagicMock()validated.unvalidated = False+ validated.cancelled = Falseassert _effective_auto_accept(True, validated) is Trueassert _effective_auto_accept(False, validated) is False