Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(SKY-9310): persist copilot YAML on V1 proposal so Accept can apply (#5702)
Andrew Neilson7521b8bf1aff
diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.pyindex d7cd742d6..a3d9524b1 100644--- a/skyvern/forge/sdk/routes/workflow_copilot.py+++ b/skyvern/forge/sdk/routes/workflow_copilot.py@@ -454,7 +454,13 @@ async def copilot_call_llm(chat_history: list[WorkflowCopilotChatHistoryMessage],global_llm_context: str | None,debug_run_info_text: str,-) -> tuple[str, Workflow | None, str | None]:+) -> tuple[str, Workflow | None, str | None, str | None]:+ """Returns (user_response, updated_workflow, global_llm_context, workflow_yaml).++ workflow_yaml is the raw YAML used to build updated_workflow — callers stash+ it on the persisted proposal so /apply-proposed-workflow can re-create the+ workflow version. Without it the V1 proposal can't be applied (SKY-9206).+ """chat_history_text = _format_chat_history(chat_history)workflow_knowledge_base = WORKFLOW_KNOWLEDGE_BASE_PATH.read_text(encoding="utf-8")@@ -544,6 +550,7 @@ async def copilot_call_llm(if action_type == "REPLACE_WORKFLOW":llm_workflow_yaml = action_data.get("workflow_yaml", "")+ applied_workflow_yaml = llm_workflow_yamltry:updated_workflow = _process_workflow_yaml(workflow_id=chat_request.workflow_id,@@ -575,19 +582,20 @@ async def copilot_call_llm(organization_id=organization_id,workflow_yaml=corrected_workflow_yaml,)+ applied_workflow_yaml = corrected_workflow_yaml- return user_response, updated_workflow, global_llm_context+ return user_response, updated_workflow, global_llm_context, applied_workflow_yamlelif action_type == "REPLY":- return user_response, None, global_llm_context+ return user_response, None, global_llm_context, Noneelif action_type == "ASK_QUESTION":- return user_response, None, global_llm_context+ return user_response, None, global_llm_context, Noneelse:LOG.error("Unknown action type from LLM",organization_id=organization_id,action_type=action_type,)- return "I received your request but I'm not sure how to help. Could you rephrase?", None, None+ return "I received your request but I'm not sure how to help. Could you rephrase?", None, None, Noneasync def _auto_correct_workflow_yaml(@@ -1318,7 +1326,12 @@ async def workflow_copilot_chat_post(# call and the DB persistence below must complete so the reply# is in the chat history when the user reconnects.with bind_copilot_session_id(chat.workflow_copilot_chat_id):- user_response, updated_workflow, updated_global_llm_context = await copilot_call_llm(+ (+ user_response,+ updated_workflow,+ updated_global_llm_context,+ updated_workflow_yaml,+ ) = await copilot_call_llm(stream,organization.organization_id,chat_request,@@ -1328,10 +1341,15 @@ async def workflow_copilot_chat_post()if updated_workflow and chat.auto_accept is not True:+ proposed_data = updated_workflow.model_dump(mode="json")+ # _copilot_yaml is what /apply-proposed-workflow re-parses into+ # WorkflowCreateYAMLRequest. Without it, Accept 400s.+ if updated_workflow_yaml:+ proposed_data["_copilot_yaml"] = updated_workflow_yamlawait app.DATABASE.workflow_params.update_workflow_copilot_chat(organization_id=chat.organization_id,workflow_copilot_chat_id=chat.workflow_copilot_chat_id,- proposed_workflow=updated_workflow.model_dump(mode="json"),+ proposed_workflow=proposed_data,)await app.DATABASE.workflow_params.create_workflow_copilot_chat_message(diff --git a/tests/unit/test_workflow_copilot_route.py b/tests/unit/test_workflow_copilot_route.pyindex 38b80ab40..3821918ac 100644--- a/tests/unit/test_workflow_copilot_route.py+++ b/tests/unit/test_workflow_copilot_route.py@@ -440,3 +440,75 @@ async def test_env_off_posthog_off_uses_legacy(monkeypatch: pytest.MonkeyPatch)assert response is captured["sentinel"]new_copilot_mock.assert_not_awaited()mock_provider.is_feature_enabled_cached.assert_awaited_once()+++async def test_legacy_path_persists_copilot_yaml_on_proposal(monkeypatch: pytest.MonkeyPatch) -> None:+ """Legacy V1 path stashes _copilot_yaml so /apply-proposed-workflow can re-create the version.++ Regression for #10568 + SKY-9206: Accept on the frontend now hits+ /apply-proposed-workflow, which 400s when _copilot_yaml is missing from the+ proposal. V2 sets it (line 921); V1 must too or non-V2 users can never accept.+ """+ monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", False)+ _install_mock_provider(monkeypatch, return_value=False)++ 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,+ )++ proposal = MagicMock(spec=["model_dump"])+ proposal.model_dump.return_value = {"workflow_id": "wf-canonical", "title": "Updated"}++ workflow_yaml = "title: Updated\nworkflow_definition:\n blocks: []\n"++ copilot_call_llm_mock = AsyncMock(return_value=("ok", proposal, None, workflow_yaml))+ monkeypatch.setattr(+ "skyvern.forge.sdk.routes.workflow_copilot.copilot_call_llm",+ copilot_call_llm_mock,+ )+ monkeypatch.setattr(+ "skyvern.forge.sdk.routes.workflow_copilot._get_debug_run_info",+ AsyncMock(return_value=None),+ )++ app.DATABASE.workflow_params = SimpleNamespace(+ get_workflow_copilot_chat_by_id=AsyncMock(return_value=chat),+ get_workflow_copilot_chat_messages=AsyncMock(return_value=[]),+ update_workflow_copilot_chat=AsyncMock(),+ create_workflow_copilot_chat_message=AsyncMock(+ return_value=SimpleNamespace(created_at=datetime(2026, 4, 28, tzinfo=timezone.utc))+ ),+ )++ request = MagicMock()+ request.headers = {}+ organization = SimpleNamespace(organization_id="org-1")++ response = await workflow_copilot_chat_post(request, _make_chat_request(), 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 = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_list+ persist_calls = [c for c in update_calls if c.kwargs.get("proposed_workflow") is not None]+ assert len(persist_calls) == 1, f"expected exactly one proposed_workflow persist, got {update_calls!r}"++ persisted = persist_calls[0].kwargs["proposed_workflow"]+ assert isinstance(persisted, dict)+ assert persisted.get("_copilot_yaml") == workflow_yaml, (+ "legacy V1 path must stash the LLM-emitted YAML on the proposal so "+ "/apply-proposed-workflow can re-create the workflow version"+ )