Skip to content

Automate browser based workflows with AI

Log the copilot terminal render decision per turn (SKY-12853) (#7700)

pedrohsdb44a693c892cd
diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.py
index 6e59f6503..0815f4138 100644
--- a/skyvern/forge/sdk/routes/workflow_copilot.py
+++ b/skyvern/forge/sdk/routes/workflow_copilot.py
@@ -989,6 +989,7 @@ async def _finalise_normal_turn(
should_render_terminal_from_envelope = await app.AGENT_FUNCTION.should_render_copilot_terminal_from_envelope(
organization_id
)
+ replaced = False
if should_render_terminal_from_envelope:
# rendered_from_envelope means "flag on, envelope is display
# authority" for the FE — not that this turn's text was replaced.
@@ -1007,6 +1008,17 @@ async def _finalise_normal_turn(
# The frame-level summary is preferred by the FE over the
# message on hydration, so it must carry the rendered text too.
narrative_summary = user_response
+ # The envelope's own log fields are stamped pre-render, so this is the
+ # only signal that witnesses the render decision in prod.
+ LOG.info(
+ "copilot_terminal_render_decision",
+ flag_enabled=should_render_terminal_from_envelope,
+ replaced=replaced,
+ run_verdict=terminal_envelope_model.run_verdict,
+ next_state=terminal_envelope_model.next_state,
+ response_kind=terminal_envelope_model.response_kind,
+ reason_present=bool(terminal_envelope_model.run_display_reason),
+ )
narrative_payload = _with_terminal_narrative_metadata(
narrative_payload,
cancelled=False,
diff --git a/tests/unit/test_workflow_copilot_route.py b/tests/unit/test_workflow_copilot_route.py
index 0f4fb962a..94249c095 100644
--- a/tests/unit/test_workflow_copilot_route.py
+++ b/tests/unit/test_workflow_copilot_route.py
@@ -24,6 +24,7 @@ from typing import Any
from unittest.mock import AsyncMock, MagicMock
import pytest
+import structlog.testing
from fastapi import HTTPException
from PIL import Image
@@ -412,6 +413,57 @@ async def test_cancel_turn_finalizes_terminal_envelope_to_non_completed_state(
assert persisted_payload["terminalEnvelope"]["response_kind"] == "stopped"
[email protected]("flag_enabled", [True, False], ids=["flag-on", "flag-off"])
+async def test_finalise_normal_turn_logs_render_decision(
+ monkeypatch: pytest.MonkeyPatch,
+ flag_enabled: bool,
+) -> None:
+ monkeypatch.setattr(
+ app.AGENT_FUNCTION,
+ "should_render_copilot_terminal_from_envelope",
+ AsyncMock(return_value=flag_enabled),
+ )
+ chat = SimpleNamespace(
+ organization_id="org-1",
+ workflow_copilot_chat_id="chat-1",
+ proposed_workflow=None,
+ auto_accept=False,
+ )
+ original_workflow = SimpleNamespace(workflow_id="wf-canonical")
+ updated_workflow = MagicMock()
+ updated_workflow.model_dump.return_value = {"workflow_id": "wf-draft"}
+ agent_result = AgentResult(
+ user_response="done",
+ updated_workflow=updated_workflow,
+ global_llm_context=None,
+ response_type="REPLY",
+ proposal_disposition="auto_applicable",
+ narrative_payload=_narrative_payload(),
+ terminal_envelope=_terminal_payload(verified=False, workflow_applied=False),
+ )
+ _, _ = setup_new_copilot_mocks(monkeypatch, chat, original_workflow, agent_result)
+ stream = MagicMock(send=AsyncMock(return_value=True))
+
+ with structlog.testing.capture_logs() as logs:
+ await workflow_copilot_route._finalise_normal_turn(
+ stream=stream,
+ chat=chat,
+ organization_id="org-1",
+ original_workflow=original_workflow,
+ chat_request=_make_chat_request(),
+ agent_result=agent_result,
+ )
+
+ decisions = [entry for entry in logs if entry["event"] == "copilot_terminal_render_decision"]
+ assert len(decisions) == 1
+ assert decisions[0]["flag_enabled"] is flag_enabled
+ # This fixture's envelope is a stopped/stopped shape, so flag-on replaces.
+ assert decisions[0]["replaced"] is flag_enabled
+ assert decisions[0]["next_state"] == "stopped"
+ assert decisions[0]["response_kind"] == "stopped"
+
+
@pytest.mark.asyncio
async def test_finalise_normal_turn_flag_on_stopped_envelope_renders_terminal_text(
monkeypatch: pytest.MonkeyPatch,