Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
feat(SKY-12136): opt-in keep_pending_proposal survives no-op turns (#7357)
Celal Zamanoğlu2de89ba3d11f
diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.pyindex 3fec87954..4d37e170a 100644--- a/skyvern/forge/sdk/routes/workflow_copilot.py+++ b/skyvern/forge/sdk/routes/workflow_copilot.py@@ -499,6 +499,9 @@ async def _clear_proposed_workflow(chat: Any) -> None:workflow_copilot_chat_id=chat.workflow_copilot_chat_id,proposed_workflow=None,)+ # Keep the in-memory chat in sync — a same-turn recovery retry reads+ # chat.proposed_workflow again and must not see the pre-write value.+ chat.proposed_workflow = Noneasync def _load_completion_criteria_snapshot(chat: Any) -> StoredCriteriaSnapshot | None:@@ -598,18 +601,25 @@ def _output_policy_blocked_final_response(agent_result: AgentResult) -> bool:return isinstance(diagnostics, dict) and diagnostics.get("final_output_policy_allowed") is False-async def _persist_proposed_workflow_state(chat: Any, agent_result: AgentResult, restored: bool) -> None:+async def _persist_proposed_workflow_state(+ chat: Any, agent_result: AgentResult, restored: bool, keep_pending_proposal: bool = False+) -> None:updated_workflow = agent_result.updated_workflowauto_accept_effective = _effective_auto_accept(chat.auto_accept, agent_result)if not auto_accept_effective and updated_workflow:+ proposed_workflow_data = _build_proposed_workflow_data(updated_workflow, agent_result)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),+ proposed_workflow=proposed_workflow_data,)+ # Keep the in-memory chat in sync — a same-turn recovery retry reads+ # chat.proposed_workflow again and must not see the pre-write value.+ chat.proposed_workflow = proposed_workflow_dataelif chat.proposed_workflow is not None and (- restored+ (restored and not keep_pending_proposal)or agent_result.clear_proposed_workflow+ or _should_commit_staged_workflow(chat.auto_accept, agent_result)or (getattr(agent_result, "apply_without_review", False) is Trueand auto_accept_effective@@ -619,14 +629,21 @@ async def _persist_proposed_workflow_state(chat: Any, agent_result: AgentResult,# 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.+ # auto-accept toggle. The staged-commit clause always wins over+ # keep_pending_proposal: this turn's own auto-commit already+ # overwrote canonical, so an earlier bypassed proposal is now stale+ # regardless of the client's preservation request.await _clear_proposed_workflow(chat)elif (# This intentionally checks the raw setting, not# ``auto_accept_effective``: no-proposal OutputPolicy blocks are not# auto-applicable, but ordinary auto-accept turns still need to clear a- # stale unvalidated card because the UI has no review panel for it.+ # stale unvalidated card once no UI renders a review panel for it.+ # keep_pending_proposal still wins: auto_accept doesn't cover+ # review_untested/review_tested, so a gate-worthy card can coexist+ # with auto_accept=True and must survive the same as any other bypass.chat.auto_accept is True+ and not keep_pending_proposaland chat.proposed_workflow is not Noneand chat.proposed_workflow.get("_copilot_unvalidated") is Trueand not _output_policy_blocked_final_response(agent_result)@@ -645,6 +662,7 @@ async def _persist_cancel_turn(agent_result: AgentResult | None,audio_artifact_id: str | None = None,turn_id: str | None = None,+ keep_pending_proposal: bool = False,) -> None:"""Persist a cancelled turn and emit a terminal SSE response frame.@@ -664,10 +682,11 @@ async def _persist_cancel_turn(response_turn_id = turn_idnarrative_summary = Nonenarrative_payload = None- if chat.proposed_workflow is not None:+ if chat.proposed_workflow is not None and not keep_pending_proposal:await asyncio.shield(_clear_proposed_workflow(chat))else:restored = _should_restore_persisted_workflow(chat.auto_accept, agent_result)+ restore_failed = Falseif restored:try:await asyncio.shield(_restore_workflow_definition(original_workflow, organization_id))@@ -677,10 +696,24 @@ async def _persist_cancel_turn(organization_id=organization_id,exc_info=True,)- if agent_result.updated_workflow is None and chat.proposed_workflow is not None:+ restore_failed = True+ # A failed rollback means canonical may still hold the mid-turn write — don't+ # honor keep_pending_proposal against an unverified "nothing changed" state.+ effective_keep_pending_proposal = keep_pending_proposal and not restore_failed+ # Broader than "restored-alone" (any no-new-proposal cancel); an agent-explicit+ # clear_proposed_workflow still wins below when this branch is skipped instead.+ if (+ agent_result.updated_workflow is None+ and chat.proposed_workflow is not None+ and not effective_keep_pending_proposal+ ):await asyncio.shield(_clear_proposed_workflow(chat))else:- await asyncio.shield(_persist_proposed_workflow_state(chat, agent_result, restored))+ await asyncio.shield(+ _persist_proposed_workflow_state(+ chat, agent_result, restored, keep_pending_proposal=effective_keep_pending_proposal+ )+ )user_response = agent_result.user_responseupdated_workflow = agent_result.updated_workflowupdated_global_llm_context = agent_result.global_llm_context@@ -778,11 +811,13 @@ async def _finalise_normal_turn(# applies proposals via applyWorkflowUpdate when auto-accept is# on.restored = _should_restore_persisted_workflow(chat.auto_accept, agent_result)+ restore_failed = Falseif restored:try:await _restore_workflow_definition(original_workflow, organization_id)except Exception:LOG.warning("copilot restore failed in _finalise_normal_turn", exc_info=True)+ restore_failed = Trueif _should_commit_staged_workflow(chat.auto_accept, agent_result):try:@@ -799,7 +834,14 @@ async def _finalise_normal_turn(await _restore_workflow_definition(original_workflow, organization_id)raise- await _persist_proposed_workflow_state(chat, agent_result, restored)+ await _persist_proposed_workflow_state(+ chat,+ agent_result,+ restored,+ # A failed rollback means canonical may still hold the mid-turn write —+ # don't honor keep_pending_proposal against an unverified "nothing changed" state.+ keep_pending_proposal=chat_request.keep_pending_proposal and not restore_failed,+ )await _persist_completion_criteria_state(chat, agent_result, chat_request.message)proposal_disposition = _proposal_disposition(agent_result)workflow_applied = _effective_auto_accept(chat.auto_accept, agent_result)@@ -1435,6 +1477,10 @@ async def _new_copilot_chat_post(original_workflow: Workflow | None = Nonechat = None+ # Snapshot before any write this turn — lets a recovery handler tell+ # "this turn's own write already superseded it" from "the write itself+ # is what failed", which agent_result.updated_workflow alone can't.+ proposed_workflow_at_turn_start: Any = Noneagent_result: AgentResult | None = Noneglobal_llm_context: str | None = Noneterminal_frame_emitted = False@@ -1456,6 +1502,101 @@ async def _new_copilot_chat_post(turn_id=turn_id,)+ async def _recover_from_route_exception(+ exc: BaseException,+ *,+ restore_log_context: str,+ translated_log_message: str,+ none_chat_log_message: str,+ none_chat_user_message: str,+ ) -> None:+ """Shared by the LLMProviderError and generic Exception handlers below —+ only their four log/user-facing strings differ."""+ nonlocal terminal_frame_emitted+ restored = chat is not None and _should_restore_persisted_workflow(chat.auto_accept, agent_result)+ restore_failed = False+ if restored:+ try:+ await _restore_workflow_definition(original_workflow, organization.organization_id)+ except Exception:+ LOG.warning(+ f"Workflow restore failed inside {restore_log_context}",+ organization_id=organization.organization_id,+ exc_info=True,+ )+ restore_failed = True+ if chat is not None:+ workflow_modified = bool(getattr(agent_result, "workflow_was_persisted", False)) and not restored+ # Pre-bake restored here: the recovered AgentResult has workflow_was_persisted=False,+ # so _persist_proposed_workflow_state's own restored check would always read False.+ recovered_result, failure = _build_recoverable_route_agent_result(+ exc,+ workflow_modified=workflow_modified,+ # agent_result is the real completed result when the exception hit AFTER+ # run_copilot_agent returned (e.g. mid-finalisation) — honor its own+ # explicit clear decision the same way the non-recovery path does.+ clear_proposed_workflow=(restored and not chat_request.keep_pending_proposal)+ or workflow_modified+ or getattr(agent_result, "clear_proposed_workflow", False)+ # A staged commit that already succeeded before this exception fired+ # still invalidates a stale kept proposal, same as the non-recovery path.+ or _should_commit_staged_workflow(chat.auto_accept, agent_result)+ # A failed rollback leaves canonical's true state unverified — don't+ # honor keep_pending_proposal against an assumption that didn't hold.+ or restore_failed+ # Compares against the turn-start snapshot, not agent_result.updated_workflow:+ # that field only means a write was ATTEMPTED, not that it SUCCEEDED — if the+ # write itself is what raised, chat.proposed_workflow never changed and an+ # older, legitimately keep_pending_proposal-protected proposal must survive.+ or chat.proposed_workflow is not proposed_workflow_at_turn_start,+ global_llm_context=global_llm_context,+ turn_id=turn_id,+ turn_index=turn_index,+ )+ recovered_result.turn_outcome = _with_current_copilot_code_mode_metadata(+ recovered_result.turn_outcome,+ effective_mode=effective_mode,+ code_available=current_code_available,+ turn_id=turn_id,+ )+ LOG.error(+ translated_log_message,+ organization_id=organization.organization_id,+ workflow_copilot_chat_id=chat.workflow_copilot_chat_id,+ failure_kind=failure.failure_kind,+ internal_error_id=failure.internal_error_id,+ exception_type=failure.exception_type,+ exc_info=True,+ )+ await asyncio.shield(+ _finalise_normal_turn(+ stream=stream,+ chat=chat,+ organization_id=organization.organization_id,+ original_workflow=original_workflow,+ chat_request=chat_request,+ agent_result=recovered_result,+ )+ )+ terminal_frame_emitted = True+ capture_code_mode_opt_out_after_persist()+ else:+ LOG.error(+ none_chat_log_message,+ organization_id=organization.organization_id,+ error=str(exc),+ exc_info=True,+ )+ terminal_frame_emitted = True+ await stream.send(+ WorkflowCopilotStreamErrorUpdate(+ type=WorkflowCopilotStreamMessageType.ERROR,+ error=none_chat_user_message,+ turn_id=turn_id,+ narrative_summary=None,+ )+ )+# Single-element list used as a closure flag (mutable bool by reference).# The watcher sets [0] = True before issuing handler_task.cancel() so the# except CancelledError block can distinguish a user-driven cancel from@@ -1477,6 +1618,7 @@ async def _new_copilot_chat_post(workflow_permanent_id=chat_request.workflow_permanent_id,workflow_copilot_chat_id=chat_request.workflow_copilot_chat_id,)+ proposed_workflow_at_turn_start = chat.proposed_workflowchat_request.workflow_copilot_chat_id = chat.workflow_copilot_chat_idchat_request.audio_artifact_id = await _validate_copilot_audio_artifact_id(audio_artifact_id=chat_request.audio_artifact_id,@@ -1670,6 +1812,7 @@ async def _new_copilot_chat_post(agent_result=agent_result,audio_artifact_id=chat_request.audio_artifact_id,turn_id=turn_id,+ keep_pending_proposal=chat_request.keep_pending_proposal,)terminal_frame_emitted = Truecapture_code_mode_opt_out_after_persist()@@ -1714,69 +1857,13 @@ async def _new_copilot_chat_post())except LLMProviderError as exc:- restored = chat is not None and _should_restore_persisted_workflow(chat.auto_accept, agent_result)- if restored:- try:- await _restore_workflow_definition(original_workflow, organization.organization_id)- except Exception:- LOG.warning(- "Workflow restore failed inside LLMProviderError handler",- organization_id=organization.organization_id,- exc_info=True,- )- if chat is not None:- workflow_modified = bool(getattr(agent_result, "workflow_was_persisted", False)) and not restored- recovered_result, failure = _build_recoverable_route_agent_result(- exc,- workflow_modified=workflow_modified,- clear_proposed_workflow=restored or workflow_modified,- global_llm_context=global_llm_context,- turn_id=turn_id,- turn_index=turn_index,- )- recovered_result.turn_outcome = _with_current_copilot_code_mode_metadata(- recovered_result.turn_outcome,- effective_mode=effective_mode,- code_available=current_code_available,- turn_id=turn_id,- )- LOG.error(- "LLM provider error translated to recoverable workflow copilot v2 reply",- organization_id=organization.organization_id,- workflow_copilot_chat_id=chat.workflow_copilot_chat_id,- failure_kind=failure.failure_kind,- internal_error_id=failure.internal_error_id,- exception_type=failure.exception_type,- exc_info=True,- )- await asyncio.shield(- _finalise_normal_turn(- stream=stream,- chat=chat,- organization_id=organization.organization_id,- original_workflow=original_workflow,- chat_request=chat_request,- agent_result=recovered_result,- )- )- terminal_frame_emitted = True- capture_code_mode_opt_out_after_persist()- else:- LOG.error(- "LLM provider error (copilot v2)",- organization_id=organization.organization_id,- error=str(exc),- exc_info=True,- )- terminal_frame_emitted = True- await stream.send(- WorkflowCopilotStreamErrorUpdate(- type=WorkflowCopilotStreamMessageType.ERROR,- error="Failed to process your request. Please try again.",- turn_id=turn_id,- narrative_summary=None,- )- )+ await _recover_from_route_exception(+ exc,+ restore_log_context="LLMProviderError handler",+ translated_log_message="LLM provider error translated to recoverable workflow copilot v2 reply",+ none_chat_log_message="LLM provider error (copilot v2)",+ none_chat_user_message="Failed to process your request. Please try again.",+ )except asyncio.CancelledError:if chat is not None and _should_restore_persisted_workflow(chat.auto_accept, agent_result):try:@@ -1801,6 +1888,7 @@ async def _new_copilot_chat_post(agent_result=None,audio_artifact_id=chat_request.audio_artifact_id,turn_id=turn_id,+ keep_pending_proposal=chat_request.keep_pending_proposal,))terminal_frame_emitted = True@@ -1822,69 +1910,13 @@ async def _new_copilot_chat_post()raiseexcept Exception as exc:- restored = chat is not None and _should_restore_persisted_workflow(chat.auto_accept, agent_result)- if restored:- try:- await _restore_workflow_definition(original_workflow, organization.organization_id)- except Exception:- LOG.warning(- "Workflow restore failed inside generic-error handler",- organization_id=organization.organization_id,- exc_info=True,- )- if chat is not None:- workflow_modified = bool(getattr(agent_result, "workflow_was_persisted", False)) and not restored- recovered_result, failure = _build_recoverable_route_agent_result(- exc,- workflow_modified=workflow_modified,- clear_proposed_workflow=restored or workflow_modified,- global_llm_context=global_llm_context,- turn_id=turn_id,- turn_index=turn_index,- )- recovered_result.turn_outcome = _with_current_copilot_code_mode_metadata(- recovered_result.turn_outcome,- effective_mode=effective_mode,- code_available=current_code_available,- turn_id=turn_id,- )- LOG.error(- "Unexpected workflow copilot v2 error translated to recoverable reply",- organization_id=organization.organization_id,- workflow_copilot_chat_id=chat.workflow_copilot_chat_id,- failure_kind=failure.failure_kind,- internal_error_id=failure.internal_error_id,- exception_type=failure.exception_type,- exc_info=True,- )- await asyncio.shield(- _finalise_normal_turn(- stream=stream,- chat=chat,- organization_id=organization.organization_id,- original_workflow=original_workflow,- chat_request=chat_request,- agent_result=recovered_result,- )- )- terminal_frame_emitted = True- capture_code_mode_opt_out_after_persist()- else:- LOG.error(- "Unexpected error in workflow copilot v2",- organization_id=organization.organization_id,- error=str(exc),- exc_info=True,- )- terminal_frame_emitted = True- await stream.send(- WorkflowCopilotStreamErrorUpdate(- type=WorkflowCopilotStreamMessageType.ERROR,- error="An error occurred. Please try again.",- turn_id=turn_id,- narrative_summary=None,- )- )+ await _recover_from_route_exception(+ exc,+ restore_log_context="generic-error handler",+ translated_log_message="Unexpected workflow copilot v2 error translated to recoverable reply",+ none_chat_log_message="Unexpected error in workflow copilot v2",+ none_chat_user_message="An error occurred. Please try again.",+ )finally:if cancel_watcher is not None and not cancel_watcher.done():cancel_watcher.cancel()diff --git a/skyvern/forge/sdk/schemas/workflow_copilot.py b/skyvern/forge/sdk/schemas/workflow_copilot.pyindex e4c7dc170..3436c0b0b 100644--- a/skyvern/forge/sdk/schemas/workflow_copilot.py+++ b/skyvern/forge/sdk/schemas/workflow_copilot.py@@ -124,6 +124,13 @@ class WorkflowCopilotChatRequest(BaseModel):"otherwise it is a no-op."),)+ keep_pending_proposal: bool = Field(+ False,+ description=(+ "When true, a pending proposed_workflow from an earlier turn survives turns that end without "+ "a new proposal, so the client can keep rendering an actionable review gate."+ ),+ )class WorkflowCopilotCancelRequest(BaseModel):diff --git a/tests/unit/test_workflow_copilot_cancel.py b/tests/unit/test_workflow_copilot_cancel.pyindex 9c1efb8dd..482e5beeb 100644--- a/tests/unit/test_workflow_copilot_cancel.py+++ b/tests/unit/test_workflow_copilot_cancel.py@@ -65,7 +65,9 @@ class _FakeCache:self.set_calls.append((key, value, ex))-def _make_chat_request(cancel_token: str | None = "tok_abc") -> WorkflowCopilotChatRequest:+def _make_chat_request(+ cancel_token: str | None = "tok_abc", keep_pending_proposal: bool = False+) -> WorkflowCopilotChatRequest:return WorkflowCopilotChatRequest(workflow_permanent_id="wpid-1",workflow_id="wf-1",@@ -74,6 +76,7 @@ def _make_chat_request(cancel_token: str | None = "tok_abc") -> WorkflowCopilotCmessage="please update",workflow_yaml="title: Example",cancel_token=cancel_token,+ keep_pending_proposal=keep_pending_proposal,)@@ -228,6 +231,7 @@ async def _drive_cancel_route(chat: SimpleNamespace,original_workflow: SimpleNamespace,agent_result: SimpleNamespace,+ keep_pending_proposal: bool = False,) -> 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)@@ -238,7 +242,9 @@ async def _drive_cancel_route(request.headers = {"x-api-key": "sk-test"}organization = SimpleNamespace(organization_id="org-1")- response = await workflow_copilot_chat_post(request, _make_chat_request(), organization)+ response = await workflow_copilot_chat_post(+ request, _make_chat_request(keep_pending_proposal=keep_pending_proposal), organization+ )assert response is captured["sentinel"]sent_payloads: list[Any] = []@@ -444,6 +450,115 @@ async def test_route_cancel_clears_stale_proposed_workflow_when_no_wip(assert workflow_params.update_workflow_copilot_chat.await_args.kwargs["proposed_workflow"] is None+async def test_route_cancel_keeps_stale_proposed_workflow_when_no_wip_and_keep_pending(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """keep_pending_proposal=True survives a no-WIP cancel (chip/gate stays actionable)."""+ 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",+ proposal_disposition="auto_applicable",+ turn_outcome=None,+ )+ _restore_mock, workflow_params, _sent = await _drive_cancel_route(+ monkeypatch, chat, original_workflow, agent_result, keep_pending_proposal=True+ )++ workflow_params.update_workflow_copilot_chat.assert_not_awaited()+++async def test_route_cancel_explicit_clear_overrides_keep_pending(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """keep_pending_proposal only neutralizes the restored-alone clear; an+ agent-explicit clear_proposed_workflow must still win in the cancel path too."""+ 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=True,+ cancelled=True,+ total_tokens=None,+ response_type="REPLY",+ proposal_disposition="no_proposal",+ turn_outcome=None,+ )+ _restore_mock, workflow_params, _sent = await _drive_cancel_route(+ monkeypatch, chat, original_workflow, agent_result, keep_pending_proposal=True+ )++ 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_proposal_when_rollback_itself_fails(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """A failed rollback leaves canonical's state unverified — keep_pending_proposal+ must not be honored against an assumption ("nothing changed") that didn't hold."""+ monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)+ captured = install_fake_create(monkeypatch)+ 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",+ proposal_disposition="no_proposal",+ turn_outcome=None,+ )+ restore_mock, workflow_params = setup_new_copilot_mocks(monkeypatch, chat, original_workflow, agent_result)+ restore_mock.side_effect = RuntimeError("rollback boom")++ request = MagicMock()+ request.headers = {"x-api-key": "sk-test"}+ organization = SimpleNamespace(organization_id="org-1")+ response = await workflow_copilot_chat_post(request, _make_chat_request(keep_pending_proposal=True), organization)+ assert response is captured["sentinel"]++ stream = MagicMock()+ stream.send = AsyncMock(return_value=True)+ stream.is_disconnected = AsyncMock(return_value=False)+ handler = captured["handler"]+ assert callable(handler)+ await handler(stream)++ 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++@pytest.mark.asyncioasync def test_pre_agent_cancel_clears_stale_proposed_workflow() -> None:"""Pre-agent cancel (agent_result=None) must clear any stale proposal.@@ -480,6 +595,39 @@ async def test_pre_agent_cancel_clears_stale_proposed_workflow() -> None:assert workflow_params.update_workflow_copilot_chat.await_args.kwargs["proposed_workflow"] is None+async def test_pre_agent_cancel_keeps_stale_proposed_workflow_when_keep_pending() -> None:+ """keep_pending_proposal=True survives a pre-agent cancel too."""+ 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,+ keep_pending_proposal=True,+ )++ workflow_params.update_workflow_copilot_chat.assert_not_awaited()++@pytest.mark.asyncioasync def test_timeout_wip_result_streams_normal_response_frame(monkeypatch: pytest.MonkeyPatch,diff --git a/tests/unit/test_workflow_copilot_route.py b/tests/unit/test_workflow_copilot_route.pyindex 59647c3e6..ac6b200b6 100644--- a/tests/unit/test_workflow_copilot_route.py+++ b/tests/unit/test_workflow_copilot_route.py@@ -54,7 +54,9 @@ from skyvern.forge.sdk.schemas.workflow_copilot import (from tests.unit.copilot_route_test_support import install_fake_create, setup_new_copilot_mocks-def _make_chat_request(mode: str | None = None, code_block: bool | None = None) -> WorkflowCopilotChatRequest:+def _make_chat_request(+ mode: str | None = None, code_block: bool | None = None, keep_pending_proposal: bool = False+) -> WorkflowCopilotChatRequest:return WorkflowCopilotChatRequest(workflow_permanent_id="wpid-1",workflow_id="wf-request",@@ -64,6 +66,7 @@ def _make_chat_request(mode: str | None = None, code_block: bool | None = None)workflow_yaml="title: Example",mode=mode,code_block=code_block,+ keep_pending_proposal=keep_pending_proposal,)@@ -662,6 +665,515 @@ async def test_route_error_after_restore_reports_workflow_not_modified(assert len(clear_calls) == 1+ "raised_error",+ [RuntimeError("post-agent route boom"), LLMProviderError("OPENAI_GPT5_5")],+ ids=["generic-exception-handler", "llm-provider-error-handler"],+)+async def test_route_error_after_restore_keeps_bypassed_proposal_when_keep_pending(+ monkeypatch: pytest.MonkeyPatch,+ raised_error: BaseException,+) -> None:+ # Route-level (not direct-function-call) pin: keep_pending_proposal must+ # reach both exception-recovery call sites, not just _persist_proposed_workflow_state.+ monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)+ captured = install_fake_create(monkeypatch)++ chat = SimpleNamespace(+ workflow_copilot_chat_id="chat-1",+ workflow_permanent_id="wpid-1",+ organization_id="org-1",+ proposed_workflow={"workflow_id": "stale"},+ auto_accept=False,+ )+ original_workflow = SimpleNamespace(+ workflow_id="wf-canonical",+ title="Original",+ description="Original description",+ workflow_definition=None,+ )+ agent_result = SimpleNamespace(+ user_response="unused",+ updated_workflow=None,+ global_llm_context=None,+ workflow_yaml=None,+ workflow_was_persisted=True,+ clear_proposed_workflow=False,+ unvalidated=False,+ turn_outcome=None,+ )+ restore_mock, _ = setup_new_copilot_mocks(monkeypatch, chat, original_workflow, agent_result)+ finalise_results: list[object] = []+ original_finalise = workflow_copilot_route._finalise_normal_turn++ async def flaky_finalise(*args: object, **kwargs: object) -> object:+ finalise_results.append(kwargs["agent_result"])+ if len(finalise_results) == 1:+ raise raised_error+ return await original_finalise(*args, **kwargs)++ monkeypatch.setattr(workflow_copilot_route, "_finalise_normal_turn", flaky_finalise)++ request = MagicMock()+ request.headers = {"x-api-key": "sk-test-key"}+ organization = SimpleNamespace(organization_id="org-1")++ response = await workflow_copilot_chat_post(request, _make_chat_request(keep_pending_proposal=True), organization)+ assert response is captured["sentinel"]++ stream = MagicMock()+ stream.send = AsyncMock(return_value=True)+ stream.is_disconnected = AsyncMock(return_value=False)++ handler = captured["handler"]+ assert callable(handler)+ await handler(stream)++ restore_mock.assert_awaited_once()+ assert len(finalise_results) == 2+ recovered_result = finalise_results[1]+ assert recovered_result.clear_proposed_workflow is False+ update_calls = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_list+ clear_calls = [c for c in update_calls if c.kwargs.get("proposed_workflow") is None]+ assert not clear_calls, f"keep_pending_proposal=True must survive restore-driven recovery, got {update_calls!r}"+++ "raised_error",+ [RuntimeError("post-agent route boom"), LLMProviderError("OPENAI_GPT5_5")],+ ids=["generic-exception-handler", "llm-provider-error-handler"],+)+async def test_route_error_honors_real_agent_explicit_clear_despite_keep_pending(+ monkeypatch: pytest.MonkeyPatch,+ raised_error: BaseException,+) -> None:+ # The real (pre-exception) agent_result can itself carry clear_proposed_workflow=True;+ # the recovery path must not silently drop that signal just because it's rebuilding+ # a synthetic result for the error reply.+ monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)+ captured = install_fake_create(monkeypatch)++ chat = SimpleNamespace(+ workflow_copilot_chat_id="chat-1",+ workflow_permanent_id="wpid-1",+ organization_id="org-1",+ proposed_workflow={"workflow_id": "stale"},+ auto_accept=False,+ )+ original_workflow = SimpleNamespace(+ workflow_id="wf-canonical",+ title="Original",+ description="Original description",+ workflow_definition=None,+ )+ agent_result = SimpleNamespace(+ user_response="unused",+ updated_workflow=None,+ global_llm_context=None,+ workflow_yaml=None,+ workflow_was_persisted=True,+ clear_proposed_workflow=True,+ unvalidated=False,+ turn_outcome=None,+ )+ restore_mock, _ = setup_new_copilot_mocks(monkeypatch, chat, original_workflow, agent_result)+ finalise_results: list[object] = []+ original_finalise = workflow_copilot_route._finalise_normal_turn++ async def flaky_finalise(*args: object, **kwargs: object) -> object:+ finalise_results.append(kwargs["agent_result"])+ if len(finalise_results) == 1:+ raise raised_error+ return await original_finalise(*args, **kwargs)++ monkeypatch.setattr(workflow_copilot_route, "_finalise_normal_turn", flaky_finalise)++ request = MagicMock()+ request.headers = {"x-api-key": "sk-test-key"}+ organization = SimpleNamespace(organization_id="org-1")++ response = await workflow_copilot_chat_post(request, _make_chat_request(keep_pending_proposal=True), organization)+ assert response is captured["sentinel"]++ stream = MagicMock()+ stream.send = AsyncMock(return_value=True)+ stream.is_disconnected = AsyncMock(return_value=False)++ handler = captured["handler"]+ assert callable(handler)+ await handler(stream)++ restore_mock.assert_awaited_once()+ assert len(finalise_results) == 2+ recovered_result = finalise_results[1]+ assert recovered_result.clear_proposed_workflow is True+ update_calls = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_list+ clear_calls = [c for c in update_calls if c.kwargs.get("proposed_workflow") is None]+ assert clear_calls, (+ f"real agent_result.clear_proposed_workflow=True must win even with keep_pending_proposal, got {update_calls!r}"+ )+++ "raised_error",+ [RuntimeError("post-commit route boom"), LLMProviderError("OPENAI_GPT5_5")],+ ids=["generic-exception-handler", "llm-provider-error-handler"],+)+async def test_route_error_after_staged_commit_clears_stale_proposal_despite_keep_pending(+ monkeypatch: pytest.MonkeyPatch,+ raised_error: BaseException,+) -> None:+ # An auto-accept turn eligible for a staged commit (has_staged_proposal=True) that+ # then hits an exception elsewhere in finalisation still invalidates a stale kept+ # proposal — the recovered synthetic result doesn't carry has_staged_proposal+ # forward, so this must be pre-baked into clear_proposed_workflow at the call site.+ monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)+ captured = install_fake_create(monkeypatch)++ chat = SimpleNamespace(+ workflow_copilot_chat_id="chat-1",+ workflow_permanent_id="wpid-1",+ organization_id="org-1",+ proposed_workflow={"workflow_id": "stale"},+ auto_accept=True,+ )+ original_workflow = SimpleNamespace(+ workflow_id="wf-canonical",+ title="Original",+ description="Original description",+ workflow_definition=None,+ )+ agent_result = SimpleNamespace(+ user_response="unused",+ updated_workflow=None,+ global_llm_context=None,+ workflow_yaml=None,+ workflow_was_persisted=False,+ clear_proposed_workflow=False,+ has_staged_proposal=True,+ proposal_disposition="auto_applicable",+ unvalidated=False,+ turn_outcome=None,+ )+ restore_mock, _ = setup_new_copilot_mocks(monkeypatch, chat, original_workflow, agent_result)+ finalise_results: list[object] = []+ original_finalise = workflow_copilot_route._finalise_normal_turn++ async def flaky_finalise(*args: object, **kwargs: object) -> object:+ finalise_results.append(kwargs["agent_result"])+ if len(finalise_results) == 1:+ raise raised_error+ return await original_finalise(*args, **kwargs)++ monkeypatch.setattr(workflow_copilot_route, "_finalise_normal_turn", flaky_finalise)++ request = MagicMock()+ request.headers = {"x-api-key": "sk-test-key"}+ organization = SimpleNamespace(organization_id="org-1")++ response = await workflow_copilot_chat_post(request, _make_chat_request(keep_pending_proposal=True), organization)+ assert response is captured["sentinel"]++ stream = MagicMock()+ stream.send = AsyncMock(return_value=True)+ stream.is_disconnected = AsyncMock(return_value=False)++ handler = captured["handler"]+ assert callable(handler)+ await handler(stream)++ restore_mock.assert_not_awaited()+ assert len(finalise_results) == 2+ recovered_result = finalise_results[1]+ assert recovered_result.clear_proposed_workflow is True+ update_calls = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_list+ clear_calls = [c for c in update_calls if c.kwargs.get("proposed_workflow") is None]+ assert clear_calls, (+ f"staged-commit-eligible turn must clear a stale proposal even with keep_pending_proposal, got {update_calls!r}"+ )+++async def test_finalise_normal_turn_clears_stale_proposal_when_rollback_itself_fails(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """A failed rollback leaves canonical's state unverified — keep_pending_proposal+ must not be honored against an assumption ("nothing changed") that didn't hold."""+ chat = SimpleNamespace(+ organization_id="org-1",+ workflow_copilot_chat_id="chat-1",+ proposed_workflow={"workflow_id": "stale"},+ auto_accept=False,+ )+ original_workflow = SimpleNamespace(+ workflow_id="wf-canonical",+ title="Original",+ description="Original description",+ workflow_definition=None,+ )+ agent_result = SimpleNamespace(+ user_response="Here is a plain reply.",+ updated_workflow=None,+ global_llm_context=None,+ workflow_yaml=None,+ workflow_was_persisted=True,+ clear_proposed_workflow=False,+ turn_outcome=None,+ )+ restore_mock, _ = setup_new_copilot_mocks(monkeypatch, chat, original_workflow, agent_result)+ restore_mock.side_effect = RuntimeError("rollback boom")++ stream = MagicMock()+ stream.send = AsyncMock(return_value=True)++ await workflow_copilot_route._finalise_normal_turn(+ stream=stream,+ chat=chat,+ organization_id="org-1",+ original_workflow=original_workflow,+ chat_request=_make_chat_request(keep_pending_proposal=True),+ agent_result=agent_result,+ )++ restore_mock.assert_awaited_once()+ update_calls = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_list+ clear_calls = [c for c in update_calls if c.kwargs.get("proposed_workflow") is None]+ assert clear_calls, (+ f"failed rollback must clear a stale proposal even with keep_pending_proposal, got {update_calls!r}"+ )+++ "raised_error",+ [RuntimeError("post-agent route boom"), LLMProviderError("OPENAI_GPT5_5")],+ ids=["generic-exception-handler", "llm-provider-error-handler"],+)+async def test_route_error_recovery_clears_stale_proposal_when_its_own_rollback_fails(+ monkeypatch: pytest.MonkeyPatch,+ raised_error: BaseException,+) -> None:+ # The recovery block's OWN restore attempt (not the main flow's — flaky_finalise+ # bypasses that entirely) can itself fail; that must still force-clear a kept+ # proposal rather than trust an unverified "rollback succeeded" assumption.+ monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)+ captured = install_fake_create(monkeypatch)++ chat = SimpleNamespace(+ workflow_copilot_chat_id="chat-1",+ workflow_permanent_id="wpid-1",+ organization_id="org-1",+ proposed_workflow={"workflow_id": "stale"},+ auto_accept=False,+ )+ original_workflow = SimpleNamespace(+ workflow_id="wf-canonical",+ title="Original",+ description="Original description",+ workflow_definition=None,+ )+ agent_result = SimpleNamespace(+ user_response="unused",+ updated_workflow=None,+ global_llm_context=None,+ workflow_yaml=None,+ workflow_was_persisted=True,+ clear_proposed_workflow=False,+ unvalidated=False,+ turn_outcome=None,+ )+ restore_mock, _ = setup_new_copilot_mocks(monkeypatch, chat, original_workflow, agent_result)+ restore_mock.side_effect = RuntimeError("rollback boom")+ finalise_results: list[object] = []+ original_finalise = workflow_copilot_route._finalise_normal_turn++ async def flaky_finalise(*args: object, **kwargs: object) -> object:+ finalise_results.append(kwargs["agent_result"])+ if len(finalise_results) == 1:+ raise raised_error+ return await original_finalise(*args, **kwargs)++ monkeypatch.setattr(workflow_copilot_route, "_finalise_normal_turn", flaky_finalise)++ request = MagicMock()+ request.headers = {"x-api-key": "sk-test-key"}+ organization = SimpleNamespace(organization_id="org-1")++ response = await workflow_copilot_chat_post(request, _make_chat_request(keep_pending_proposal=True), organization)+ assert response is captured["sentinel"]++ stream = MagicMock()+ stream.send = AsyncMock(return_value=True)+ stream.is_disconnected = AsyncMock(return_value=False)++ handler = captured["handler"]+ assert callable(handler)+ await handler(stream)++ assert len(finalise_results) == 2+ recovered_result = finalise_results[1]+ assert recovered_result.clear_proposed_workflow is True+ update_calls = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_list+ clear_calls = [c for c in update_calls if c.kwargs.get("proposed_workflow") is None]+ assert clear_calls, f"a failed recovery-path rollback must clear a stale proposal, got {update_calls!r}"+++ "raised_error",+ [RuntimeError("post-write route boom"), LLMProviderError("OPENAI_GPT5_5")],+ ids=["generic-exception-handler", "llm-provider-error-handler"],+)+async def test_route_error_before_write_keeps_older_proposal_despite_attempted_fresh_draft(+ monkeypatch: pytest.MonkeyPatch,+ raised_error: BaseException,+) -> None:+ # Negative companion to test_route_error_after_real_fresh_write_clears_it_even_with_+ # no_prior_proposal: agent_result.updated_workflow being SET only means a write was+ # ATTEMPTED. flaky_finalise bypasses _finalise_normal_turn's real body entirely, so+ # the write never actually reaches chat.proposed_workflow — an older, legitimately+ # keep_pending_proposal-protected proposal must survive, not get force-cleared just+ # because this turn also carried an (unpersisted) fresh draft.+ monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)+ captured = install_fake_create(monkeypatch)++ chat = SimpleNamespace(+ workflow_copilot_chat_id="chat-1",+ workflow_permanent_id="wpid-1",+ organization_id="org-1",+ proposed_workflow={"workflow_id": "older-stale"},+ auto_accept=False,+ )+ original_workflow = SimpleNamespace(+ workflow_id="wf-canonical",+ title="Original",+ description="Original description",+ workflow_definition=None,+ )+ agent_result = SimpleNamespace(+ user_response="unused",+ updated_workflow=SimpleNamespace(model_dump=lambda mode: {"title": "fresh draft"}),+ global_llm_context=None,+ workflow_yaml="title: fresh draft\n",+ workflow_was_persisted=False,+ clear_proposed_workflow=False,+ has_staged_proposal=False,+ proposal_disposition="review_untested",+ unvalidated=False,+ turn_outcome=None,+ )+ restore_mock, _ = setup_new_copilot_mocks(monkeypatch, chat, original_workflow, agent_result)+ finalise_results: list[object] = []+ original_finalise = workflow_copilot_route._finalise_normal_turn++ async def flaky_finalise(*args: object, **kwargs: object) -> object:+ finalise_results.append(kwargs["agent_result"])+ if len(finalise_results) == 1:+ raise raised_error+ return await original_finalise(*args, **kwargs)++ monkeypatch.setattr(workflow_copilot_route, "_finalise_normal_turn", flaky_finalise)++ request = MagicMock()+ request.headers = {"x-api-key": "sk-test-key"}+ organization = SimpleNamespace(organization_id="org-1")++ response = await workflow_copilot_chat_post(request, _make_chat_request(keep_pending_proposal=True), organization)+ assert response is captured["sentinel"]++ stream = MagicMock()+ stream.send = AsyncMock(return_value=True)+ stream.is_disconnected = AsyncMock(return_value=False)++ handler = captured["handler"]+ assert callable(handler)+ await handler(stream)++ restore_mock.assert_not_awaited()+ assert len(finalise_results) == 2+ recovered_result = finalise_results[1]+ assert recovered_result.clear_proposed_workflow is False+ app.DATABASE.workflow_params.update_workflow_copilot_chat.assert_not_awaited()+++async def test_route_error_after_real_fresh_write_clears_it_even_with_no_prior_proposal(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # Unlike the flaky_finalise-based tests above (which bypass _finalise_normal_turn's+ # real body entirely), this lets the REAL first attempt genuinely write the fresh+ # proposal to chat.proposed_workflow before a LATER step (chat-message creation)+ # fails — the bug this pins is that chat.proposed_workflow stayed in-memory None+ # (never synced after the write), so the retry's `elif chat.proposed_workflow is+ # not None` guard silently skipped the clear even though clear_proposed_workflow+ # correctly computed True.+ monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)+ captured = install_fake_create(monkeypatch)++ chat = SimpleNamespace(+ workflow_copilot_chat_id="chat-1",+ workflow_permanent_id="wpid-1",+ organization_id="org-1",+ proposed_workflow=None,+ auto_accept=False,+ )+ original_workflow = SimpleNamespace(+ workflow_id="wf-canonical",+ title="Original",+ description="Original description",+ workflow_definition=None,+ )+ agent_result = SimpleNamespace(+ user_response="Here is your draft.",+ updated_workflow=SimpleNamespace(model_dump=lambda mode: {"title": "fresh draft"}),+ global_llm_context=None,+ workflow_yaml="title: fresh draft\n",+ workflow_was_persisted=False,+ clear_proposed_workflow=False,+ has_staged_proposal=False,+ proposal_disposition="review_untested",+ turn_outcome=None,+ )+ _restore_mock, workflow_params = setup_new_copilot_mocks(monkeypatch, chat, original_workflow, agent_result)++ call_count = {"n": 0}+ original_return = workflow_params.create_workflow_copilot_chat_message.return_value++ async def flaky_create_message(*args: object, **kwargs: object) -> object:+ call_count["n"] += 1+ if call_count["n"] == 1:+ raise RuntimeError("chat row insert boom")+ return original_return++ workflow_params.create_workflow_copilot_chat_message = AsyncMock(side_effect=flaky_create_message)++ request = MagicMock()+ request.headers = {"x-api-key": "sk-test-key"}+ organization = SimpleNamespace(organization_id="org-1")++ response = await workflow_copilot_chat_post(request, _make_chat_request(keep_pending_proposal=True), organization)+ assert response is captured["sentinel"]++ stream = MagicMock()+ stream.send = AsyncMock(return_value=True)+ stream.is_disconnected = AsyncMock(return_value=False)++ handler = captured["handler"]+ assert callable(handler)+ await handler(stream)++ update_calls = workflow_params.update_workflow_copilot_chat.await_args_list+ write_calls = [c for c in update_calls if c.kwargs.get("proposed_workflow") is not None]+ clear_calls = [c for c in update_calls if c.kwargs.get("proposed_workflow") is None]+ assert write_calls, "the first attempt must have genuinely persisted the fresh draft"+ assert clear_calls, (+ f"the orphaned fresh draft must clear on retry even though chat had no prior proposal, got {update_calls!r}"+ )++@pytest.mark.asyncio@pytest.mark.parametrize((@@ -1250,6 +1762,273 @@ async def test_persist_state_keeps_verified_review_tested_proposal(monkeypatch:assert persisted.get("_copilot_unvalidated") is not True+def _make_bypassed_proposal_agent_result(**overrides: object) -> SimpleNamespace:+ fields: dict[str, object] = dict(+ updated_workflow=None,+ clear_proposed_workflow=False,+ proposal_disposition="review_untested",+ cancelled=False,+ apply_without_review=False,+ output_policy_diagnostics=None,+ )+ fields.update(overrides)+ return SimpleNamespace(**fields)+++async def test_persist_state_restored_keep_pending_proposal_survives(monkeypatch: pytest.MonkeyPatch) -> None:+ # Opt-in keep_pending_proposal suppresses the restored-alone clear so a bypassed+ # proposal stays actionable across a follow-up turn with no new draft.+ chat = SimpleNamespace(+ organization_id="org-1",+ workflow_copilot_chat_id="chat-1",+ auto_accept=False,+ proposed_workflow={"existing": True},+ )+ monkeypatch.setattr(+ app.DATABASE,+ "workflow_params",+ SimpleNamespace(update_workflow_copilot_chat=AsyncMock()),+ )+ agent_result = _make_bypassed_proposal_agent_result()++ await workflow_copilot_route._persist_proposed_workflow_state(+ chat, agent_result, restored=True, keep_pending_proposal=True+ )++ app.DATABASE.workflow_params.update_workflow_copilot_chat.assert_not_awaited()+++async def test_persist_state_restored_without_keep_still_clears(monkeypatch: pytest.MonkeyPatch) -> None:+ # Pin today's default (keep_pending_proposal=False) behavior next to its opt-in twin above.+ chat = SimpleNamespace(+ organization_id="org-1",+ workflow_copilot_chat_id="chat-1",+ auto_accept=False,+ proposed_workflow={"existing": True},+ )+ monkeypatch.setattr(+ app.DATABASE,+ "workflow_params",+ SimpleNamespace(update_workflow_copilot_chat=AsyncMock()),+ )+ agent_result = _make_bypassed_proposal_agent_result()++ await workflow_copilot_route._persist_proposed_workflow_state(chat, agent_result, restored=True)++ calls = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_list+ assert len(calls) == 1+ assert calls[0].kwargs["proposed_workflow"] is None+++async def test_persist_state_keep_pending_proposal_does_not_suppress_explicit_clear(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # keep_pending_proposal only neutralizes the restored-alone justification; an+ # agent-explicit clear_proposed_workflow must still win.+ chat = SimpleNamespace(+ organization_id="org-1",+ workflow_copilot_chat_id="chat-1",+ auto_accept=False,+ proposed_workflow={"existing": True},+ )+ monkeypatch.setattr(+ app.DATABASE,+ "workflow_params",+ SimpleNamespace(update_workflow_copilot_chat=AsyncMock()),+ )+ agent_result = _make_bypassed_proposal_agent_result(+ clear_proposed_workflow=True, proposal_disposition="no_proposal"+ )++ await workflow_copilot_route._persist_proposed_workflow_state(+ chat, agent_result, restored=False, keep_pending_proposal=True+ )++ calls = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_list+ assert len(calls) == 1+ assert calls[0].kwargs["proposed_workflow"] is None+++async def test_persist_state_keep_pending_proposal_does_not_block_new_proposal_overwrite(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # A fresh proposal always overwrites regardless of keep_pending_proposal/restored.+ chat = SimpleNamespace(+ organization_id="org-1",+ workflow_copilot_chat_id="chat-1",+ auto_accept=False,+ proposed_workflow={"existing": True},+ )+ monkeypatch.setattr(+ app.DATABASE,+ "workflow_params",+ SimpleNamespace(update_workflow_copilot_chat=AsyncMock()),+ )+ agent_result = _make_bypassed_proposal_agent_result(+ updated_workflow=SimpleNamespace(model_dump=lambda mode: {"title": "new draft"}),+ workflow_yaml="title: new draft\n",+ )++ await workflow_copilot_route._persist_proposed_workflow_state(+ chat, agent_result, restored=True, keep_pending_proposal=True+ )++ calls = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_list+ assert len(calls) == 1+ persisted = calls[0].kwargs["proposed_workflow"]+ assert persisted is not None+ assert persisted.get("title") == "new draft"+++async def test_persist_state_keep_pending_proposal_survives_auto_accept_stale_unvalidated_clear(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # auto_accept doesn't cover review_untested/review_tested, so a gate-worthy+ # unvalidated proposal can coexist with chat.auto_accept=True; the third+ # elif's clear must also respect keep_pending_proposal.+ chat = SimpleNamespace(+ organization_id="org-1",+ workflow_copilot_chat_id="chat-1",+ auto_accept=True,+ proposed_workflow={"existing": True, "_copilot_unvalidated": True},+ )+ monkeypatch.setattr(+ app.DATABASE,+ "workflow_params",+ SimpleNamespace(update_workflow_copilot_chat=AsyncMock()),+ )+ agent_result = _make_bypassed_proposal_agent_result()++ await workflow_copilot_route._persist_proposed_workflow_state(+ chat, agent_result, restored=False, keep_pending_proposal=True+ )++ app.DATABASE.workflow_params.update_workflow_copilot_chat.assert_not_awaited()+++async def test_persist_state_auto_accept_stale_unvalidated_still_clears_without_keep(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # Pin today's default (keep_pending_proposal=False) next to its opt-in twin above.+ chat = SimpleNamespace(+ organization_id="org-1",+ workflow_copilot_chat_id="chat-1",+ auto_accept=True,+ proposed_workflow={"existing": True, "_copilot_unvalidated": True},+ )+ monkeypatch.setattr(+ app.DATABASE,+ "workflow_params",+ SimpleNamespace(update_workflow_copilot_chat=AsyncMock()),+ )+ agent_result = _make_bypassed_proposal_agent_result()++ await workflow_copilot_route._persist_proposed_workflow_state(chat, agent_result, restored=False)++ calls = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_list+ assert len(calls) == 1+ assert calls[0].kwargs["proposed_workflow"] is None+++async def test_persist_state_staged_commit_clears_stale_proposal_despite_keep_pending(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # A later turn's own auto-commit (chat.auto_accept, not apply_without_review)+ # supersedes an earlier bypassed proposal even when the client asked to keep+ # it — the committed canonical workflow already moved past it.+ chat = SimpleNamespace(+ organization_id="org-1",+ workflow_copilot_chat_id="chat-1",+ auto_accept=True,+ proposed_workflow={"existing": True},+ )+ monkeypatch.setattr(+ app.DATABASE,+ "workflow_params",+ SimpleNamespace(update_workflow_copilot_chat=AsyncMock()),+ )+ agent_result = _make_bypassed_proposal_agent_result(+ proposal_disposition="auto_applicable",+ has_staged_proposal=True,+ )++ await workflow_copilot_route._persist_proposed_workflow_state(+ chat, agent_result, restored=False, keep_pending_proposal=True+ )++ calls = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_list+ assert len(calls) == 1+ assert calls[0].kwargs["proposed_workflow"] is None+++async def test_persist_state_staged_commit_clears_stale_proposal_on_default_path_too(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # This clause is unflagged: it also fixes SKY-12130 orphaning for every current+ # client, not just callers that opt into keep_pending_proposal. Pin the default+ # (keep_pending_proposal=False, the pre-existing behavior for all callers today)+ # path explicitly, not just the opt-in one above.+ chat = SimpleNamespace(+ organization_id="org-1",+ workflow_copilot_chat_id="chat-1",+ auto_accept=True,+ proposed_workflow={"existing": True},+ )+ monkeypatch.setattr(+ app.DATABASE,+ "workflow_params",+ SimpleNamespace(update_workflow_copilot_chat=AsyncMock()),+ )+ agent_result = _make_bypassed_proposal_agent_result(+ proposal_disposition="auto_applicable",+ has_staged_proposal=True,+ )++ await workflow_copilot_route._persist_proposed_workflow_state(chat, agent_result, restored=False)++ calls = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_list+ assert len(calls) == 1+ assert calls[0].kwargs["proposed_workflow"] is None+++async def test_persist_state_auto_applicable_without_staged_commit_still_protected_by_keep_pending(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # auto_applicable alone isn't enough — only an actual staged commit this turn+ # invalidates the earlier proposal. No staged content means nothing+ # superseded it, so keep_pending_proposal still applies.+ chat = SimpleNamespace(+ organization_id="org-1",+ workflow_copilot_chat_id="chat-1",+ auto_accept=True,+ proposed_workflow={"existing": True},+ )+ monkeypatch.setattr(+ app.DATABASE,+ "workflow_params",+ SimpleNamespace(update_workflow_copilot_chat=AsyncMock()),+ )+ agent_result = _make_bypassed_proposal_agent_result(+ proposal_disposition="auto_applicable",+ has_staged_proposal=False,+ )++ await workflow_copilot_route._persist_proposed_workflow_state(+ chat, agent_result, restored=False, keep_pending_proposal=True+ )++ app.DATABASE.workflow_params.update_workflow_copilot_chat.assert_not_awaited()++def _schema_incompatibility_ctx() -> SimpleNamespace:incompat = SchemaIncompatibility(block_label="capture_row",