Skip to content

Automate browser based workflows with AI

SKY-10318/10142: stage workflow proposals + persist turn-narrative bubble across reloads (BE, 1/2) (#6257)

Andrew Neilsoncc2cb603ad67
diff --git a/alembic/versions/2026_05_29_2154-ed48b2c9cab2_add_narrative_payload_column_to_workflow_copilot_chat_messages.py b/alembic/versions/2026_05_29_2154-ed48b2c9cab2_add_narrative_payload_column_to_workflow_copilot_chat_messages.py
new file mode 100644
index 000000000..e7b849986
--- /dev/null
+++ b/alembic/versions/2026_05_29_2154-ed48b2c9cab2_add_narrative_payload_column_to_workflow_copilot_chat_messages.py
@@ -0,0 +1,45 @@
+"""add narrative_payload column to workflow_copilot_chat_messages
+
+Revision ID: ed48b2c9cab2
+Revises: c636e511753d
+Create Date: 2026-05-29T21:54:45.368634+00:00
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "ed48b2c9cab2"
+down_revision: Union[str, None] = "c636e511753d"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ conn = op.get_bind()
+ inspector = sa.inspect(conn)
+
+ columns = {col["name"] for col in inspector.get_columns("workflow_copilot_chat_messages")}
+ if "narrative_payload" in columns:
+ return
+
+ if conn.dialect.name == "postgresql":
+ op.execute(sa.text("SET lock_timeout = '2s'"))
+
+ op.add_column(
+ "workflow_copilot_chat_messages",
+ sa.Column("narrative_payload", sa.JSON(), nullable=True),
+ )
+
+
+def downgrade() -> None:
+ conn = op.get_bind()
+ inspector = sa.inspect(conn)
+ columns = {col["name"] for col in inspector.get_columns("workflow_copilot_chat_messages")}
+ if "narrative_payload" not in columns:
+ return
+ op.drop_column("workflow_copilot_chat_messages", "narrative_payload")
diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.py
index 9997f1b23..a9bf86a31 100644
--- a/skyvern/forge/sdk/copilot/agent.py
+++ b/skyvern/forge/sdk/copilot/agent.py
@@ -43,8 +43,12 @@ from skyvern.forge.sdk.copilot.context import (
COPILOT_RESPONSE_TYPES,
AgentResult,
CopilotContext,
+ NarrativeActivityEntry,
+ NarrativeBlock,
+ NarrativeDraft,
ResponseType,
StructuredContext,
+ TurnNarrativePayload,
finalize_discovery_counter_in_global_llm_context,
)
from skyvern.forge.sdk.copilot.output_policy import (
@@ -717,6 +721,94 @@ def _make_agent_result(
return AgentResult(global_llm_context=final_context, turn_outcome=turn_outcome, **kwargs)
+_BLOCK_STATUS_TO_UI_STATE: dict[str, str] = {
+ "running": "running",
+ "completed": "completed",
+ "skipped": "skipped",
+ "failed": "failed",
+ "terminated": "failed",
+ "timed_out": "failed",
+ "canceled": "failed",
+ "queued": "queued",
+}
+
+
+def _block_ui_state(raw_status: str | None, *, drafted_fallback: bool) -> str:
+ # No status + drafted_fallback => stage-only block, distinct from "queued".
+ if raw_status is None:
+ return "drafted" if drafted_fallback else "queued"
+ return _BLOCK_STATUS_TO_UI_STATE.get(raw_status, "queued")
+
+
+def _build_narrative_payload(
+ ctx: CopilotContext,
+ *,
+ terminal: str,
+ terminal_message: str | None,
+ narrative_summary: str | None,
+) -> TurnNarrativePayload:
+ mode_value = ctx.turn_intent.mode.value if ctx.turn_intent is not None else "unknown"
+ narrator_state = ctx.narrator_state
+ block_activity: dict[str, list[NarrativeActivityEntry]] = (
+ narrator_state.block_activity if narrator_state is not None else {}
+ )
+ design_activity: list[NarrativeActivityEntry] = narrator_state.design_activity if narrator_state is not None else []
+ block_labels: list[str] = []
+ blocks: list[NarrativeBlock] = []
+ staged = ctx.staged_workflow
+ if staged is not None and getattr(staged, "workflow_definition", None) is not None:
+ for block in staged.workflow_definition.blocks:
+ label = getattr(block, "label", None)
+ if not isinstance(label, str) or not label:
+ continue
+ block_labels.append(label)
+ block_type_value = getattr(block, "block_type", None)
+ if block_type_value is not None and hasattr(block_type_value, "value"):
+ block_type = block_type_value.value
+ else:
+ block_type = str(block_type_value or "task")
+ raw_status = ctx.block_state_map.get(label)
+ blocks.append(
+ {
+ "label": label,
+ "blockType": block_type,
+ "state": _block_ui_state(
+ raw_status,
+ drafted_fallback=ctx.has_staged_proposal,
+ ),
+ "lastSeenIteration": 0,
+ "activity": list(block_activity.get(label, [])),
+ "startedAt": ctx.block_started_at_map.get(label),
+ "endedAt": ctx.block_ended_at_map.get(label),
+ }
+ )
+ draft: NarrativeDraft | None = (
+ {"blockCount": len(block_labels), "blockLabels": block_labels, "summary": None}
+ if ctx.has_staged_proposal
+ else None
+ )
+ # First terminal builder to reach here seals the turn-level end time;
+ # later exit paths reuse it so the persisted elapsed matches the live one.
+ if ctx.turn_ended_at is None:
+ ctx.turn_ended_at = datetime.now(timezone.utc).isoformat()
+ return {
+ "turnId": ctx.turn_id,
+ "turnIndex": ctx.turn_index,
+ "mode": mode_value,
+ "designStarted": True,
+ "designEnded": True,
+ "draft": draft,
+ "blocks": blocks,
+ "terminal": terminal,
+ "terminalMessage": terminal_message,
+ "narrativeSummary": narrative_summary or terminal_message,
+ "priorBlockCount": ctx.prior_block_count,
+ "designActivity": list(design_activity),
+ "startedAt": ctx.turn_started_at,
+ "endedAt": ctx.turn_ended_at,
+ }
+
+
def _build_exit_result(
ctx: CopilotContext,
user_response: str,
@@ -741,11 +833,21 @@ def _build_exit_result(
global_llm_context=global_llm_context,
workflow_yaml=verified_yaml,
workflow_was_persisted=ctx.workflow_persisted,
+ has_staged_proposal=ctx.has_staged_proposal,
+ staged_workflow_yaml=ctx.staged_workflow_yaml,
+ staged_workflow=ctx.staged_workflow,
+ canonical_was_persisted_due_to_param_change=ctx.canonical_was_persisted_due_to_param_change,
total_tokens=ctx.total_tokens_used,
cancelled=cancelled,
turn_outcome=outcome,
turn_id=ctx.turn_id,
narrative_summary=ctx.narrative_summary,
+ narrative_payload=_build_narrative_payload(
+ ctx,
+ terminal="error" if cancelled else "response",
+ terminal_message=final_text,
+ narrative_summary=ctx.narrative_summary,
+ ),
),
exit_site="exit_result",
)
@@ -964,6 +1066,12 @@ def _finalize_result_with_blocker_override(
turn_outcome=turn_outcome,
turn_id=ctx.turn_id,
narrative_summary=ctx.narrative_summary,
+ narrative_payload=_build_narrative_payload(
+ ctx,
+ terminal="response",
+ terminal_message=final_text,
+ narrative_summary=ctx.narrative_summary,
+ ),
)
@@ -1180,12 +1288,22 @@ def _build_wip_exit_result(
global_llm_context=global_llm_context,
workflow_yaml=ctx.last_good_workflow_yaml,
workflow_was_persisted=ctx.workflow_persisted,
+ has_staged_proposal=ctx.has_staged_proposal,
+ staged_workflow_yaml=ctx.staged_workflow_yaml,
+ staged_workflow=ctx.staged_workflow,
+ canonical_was_persisted_due_to_param_change=ctx.canonical_was_persisted_due_to_param_change,
total_tokens=ctx.total_tokens_used,
proposal_disposition="review_tested",
cancelled=cancelled,
turn_outcome=outcome,
turn_id=ctx.turn_id,
narrative_summary=ctx.narrative_summary,
+ narrative_payload=_build_narrative_payload(
+ ctx,
+ terminal="error",
+ terminal_message=final_text,
+ narrative_summary=ctx.narrative_summary,
+ ),
),
exit_site="wip_last_good_workflow",
)
@@ -1210,12 +1328,22 @@ def _build_wip_exit_result(
global_llm_context=global_llm_context,
workflow_yaml=ctx.last_workflow_yaml,
workflow_was_persisted=ctx.workflow_persisted,
+ has_staged_proposal=ctx.has_staged_proposal,
+ staged_workflow_yaml=ctx.staged_workflow_yaml,
+ staged_workflow=ctx.staged_workflow,
+ canonical_was_persisted_due_to_param_change=ctx.canonical_was_persisted_due_to_param_change,
total_tokens=ctx.total_tokens_used,
proposal_disposition="review_untested" if unvalidated else "auto_applicable",
cancelled=cancelled,
turn_outcome=outcome,
turn_id=ctx.turn_id,
narrative_summary=ctx.narrative_summary,
+ narrative_payload=_build_narrative_payload(
+ ctx,
+ terminal="error",
+ terminal_message=final_text,
+ narrative_summary=ctx.narrative_summary,
+ ),
),
exit_site="wip_last_workflow",
)
@@ -1323,7 +1451,7 @@ def _build_cancel_exit_result(ctx: CopilotContext, global_llm_context: str | Non
)
-def _translate_to_agent_result(
+async def _translate_to_agent_result(
result: RunResultStreaming,
ctx: CopilotContext,
global_llm_context: str | None,
@@ -1444,6 +1572,17 @@ def _translate_to_agent_result(
ctx.last_workflow = last_workflow
ctx.last_workflow_yaml = last_workflow_yaml
ctx.last_test_ok = None
+ # Inline REPLACE_WORKFLOW is untested by construction; emit a draft
+ # envelope without staging onto ctx so terminal auto-accept can't fire,
+ # and suppress the workflow payload so the canvas does not render it.
+ if last_workflow is not None and ctx.stream is not None:
+ try:
+ await maybe_emit_design_end(ctx.stream, ctx)
+ await emit_workflow_draft(ctx.stream, ctx, last_workflow, include_workflow=False)
+ except Exception as emit_err:
+ LOG.warning("copilot_narrative_inline_replace_emit_failed", error=str(emit_err))
+ ctx.design_start_emitted = False
+ ctx.design_end_emitted = False
# An unverified edit/run sits in ``last_workflow`` after a recorded
# failure — surface the verified prior shape and skip the failure rewrite
@@ -1631,6 +1770,10 @@ def _translate_to_agent_result(
response_type=resp_type,
workflow_yaml=last_workflow_yaml,
workflow_was_persisted=ctx.workflow_persisted,
+ has_staged_proposal=ctx.has_staged_proposal,
+ staged_workflow_yaml=ctx.staged_workflow_yaml,
+ staged_workflow=ctx.staged_workflow,
+ canonical_was_persisted_due_to_param_change=ctx.canonical_was_persisted_due_to_param_change,
total_tokens=ctx.total_tokens_used,
clear_proposed_workflow=resp_type == "ASK_QUESTION",
proposal_disposition=(
@@ -1644,6 +1787,12 @@ def _translate_to_agent_result(
turn_outcome=turn_outcome,
turn_id=ctx.turn_id,
narrative_summary=ctx.narrative_summary,
+ narrative_payload=_build_narrative_payload(
+ ctx,
+ terminal="response",
+ terminal_message=final_user_response,
+ narrative_summary=ctx.narrative_summary,
+ ),
),
exit_site="translate_to_agent_result",
)
@@ -1691,6 +1840,12 @@ def _build_feasibility_clarification_result(
turn_outcome=outcome,
turn_id=ctx.turn_id,
narrative_summary=ctx.narrative_summary,
+ narrative_payload=_build_narrative_payload(
+ ctx,
+ terminal="response",
+ terminal_message=final_text,
+ narrative_summary=ctx.narrative_summary,
+ ),
),
exit_site="feasibility_clarification",
)
@@ -1774,6 +1929,12 @@ def _build_request_policy_clarification_result(
turn_outcome=outcome,
turn_id=ctx.turn_id,
narrative_summary=ctx.narrative_summary,
+ narrative_payload=_build_narrative_payload(
+ ctx,
+ terminal="response",
+ terminal_message=final_text,
+ narrative_summary=ctx.narrative_summary,
+ ),
),
exit_site="request_policy_clarification",
)
@@ -2076,6 +2237,10 @@ def _build_output_policy_blocked_result(
response_type="ASK_QUESTION",
workflow_yaml=preserved_workflow_yaml or prior_workflow_yaml,
workflow_was_persisted=ctx.workflow_persisted,
+ has_staged_proposal=ctx.has_staged_proposal,
+ staged_workflow_yaml=ctx.staged_workflow_yaml,
+ staged_workflow=ctx.staged_workflow,
+ canonical_was_persisted_due_to_param_change=ctx.canonical_was_persisted_due_to_param_change,
total_tokens=ctx.total_tokens_used,
clear_proposed_workflow=False,
proposal_disposition=(
@@ -2096,6 +2261,12 @@ def _build_output_policy_blocked_result(
turn_outcome=output_policy_outcome,
turn_id=ctx.turn_id,
narrative_summary=ctx.narrative_summary,
+ narrative_payload=_build_narrative_payload(
+ ctx,
+ terminal="response",
+ terminal_message=final_user_response,
+ narrative_summary=ctx.narrative_summary,
+ ),
)
@@ -2113,6 +2284,7 @@ async def run_copilot_agent(
turn_index: int | None = None,
turn_id: str | None = None,
prior_copilot_workflow_yaml: str | None = None,
+ prior_block_count: int | None = None,
) -> AgentResult:
# One id per turn — passed to every downstream AgentResult and
# CopilotContext so the envelope and terminal frames correlate. The
@@ -2146,6 +2318,7 @@ async def run_copilot_agent(
turn_id=turn_id,
turn_index=normalized_turn_index,
prior_copilot_workflow_yaml=prior_copilot_workflow_yaml,
+ prior_block_count=prior_block_count,
)
except Exception as exc:
LOG.error(
@@ -2209,6 +2382,7 @@ async def _run_copilot_turn_impl(
turn_id: str,
turn_index: int,
prior_copilot_workflow_yaml: str | None = None,
+ prior_block_count: int | None = None,
) -> AgentResult:
copilot_config = config or CopilotConfig(security_rules=security_rules)
chat_history_text = _format_chat_history(chat_history)
@@ -2269,6 +2443,7 @@ async def _run_copilot_turn_impl(
workflow_copilot_chat_id=chat_request.workflow_copilot_chat_id,
turn_id=turn_id,
turn_index=turn_index,
+ prior_block_count=prior_block_count,
)
# Fail loud if a future caller skips the kwarg and gets a fresh UUID from
# the default_factory — the envelope and terminal frames would then carry
@@ -2554,7 +2729,7 @@ async def _run_copilot_turn_impl(
)
ctx.supports_vision = fallback_supports_vision
result = await _run_attempt(fallback_model_name, fallback_run_config, fallback_resolved_key)
- agent_result = _translate_to_agent_result(
+ agent_result = await _translate_to_agent_result(
result,
ctx,
global_llm_context,
@@ -2638,10 +2813,20 @@ async def _run_copilot_turn_impl(
global_llm_context=global_llm_context,
workflow_yaml=None,
workflow_was_persisted=ctx.workflow_persisted,
+ has_staged_proposal=ctx.has_staged_proposal,
+ staged_workflow_yaml=ctx.staged_workflow_yaml,
+ staged_workflow=ctx.staged_workflow,
+ canonical_was_persisted_due_to_param_change=ctx.canonical_was_persisted_due_to_param_change,
total_tokens=ctx.total_tokens_used,
turn_outcome=nav_outcome,
turn_id=ctx.turn_id,
narrative_summary=ctx.narrative_summary,
+ narrative_payload=_build_narrative_payload(
+ ctx,
+ terminal="response",
+ terminal_message=final_nav_text,
+ narrative_summary=ctx.narrative_summary,
+ ),
),
exit_site="non_retriable_nav",
)
diff --git a/skyvern/forge/sdk/copilot/context.py b/skyvern/forge/sdk/copilot/context.py
index 5edabccec..16f4048c1 100644
--- a/skyvern/forge/sdk/copilot/context.py
+++ b/skyvern/forge/sdk/copilot/context.py
@@ -8,6 +8,7 @@ from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Literal, get_args
from pydantic import BaseModel, Field
+from typing_extensions import NotRequired, TypedDict
from skyvern.forge.sdk.copilot.build_phase import BuildPhase
from skyvern.forge.sdk.copilot.runtime import AgentContext
@@ -17,6 +18,52 @@ ResponseType = Literal["REPLY", "ASK_QUESTION", "REPLACE_WORKFLOW"]
COPILOT_RESPONSE_TYPES: tuple[ResponseType, ...] = get_args(ResponseType)
ProposalDisposition = Literal["no_proposal", "auto_applicable", "review_untested", "review_tested"]
+
+class NarrativeDraft(TypedDict):
+ blockCount: int
+ blockLabels: list[str]
+ summary: str | None
+
+
+# Shape must match the FE ``ActivityEntry`` in narrativeState.ts; toolName is
+# present only for tool_call/tool_result and success only for tool_result.
+class NarrativeActivityEntry(TypedDict):
+ kind: str
+ text: str
+ iteration: int
+ toolName: NotRequired[str]
+ success: NotRequired[bool]
+ id: str
+
+
+class NarrativeBlock(TypedDict):
+ label: str
+ blockType: str
+ state: str
+ lastSeenIteration: int
+ activity: list[NarrativeActivityEntry]
+ startedAt: str | None
+ endedAt: str | None
+
+
+# Mirror of the FE TurnNarrativeState; camelCase keys match the wire shape.
+class TurnNarrativePayload(TypedDict):
+ turnId: str | None
+ turnIndex: int
+ mode: str
+ designStarted: bool
+ designEnded: bool
+ draft: NarrativeDraft | None
+ blocks: list[NarrativeBlock]
+ terminal: str
+ terminalMessage: str | None
+ narrativeSummary: str | None
+ priorBlockCount: int | None
+ designActivity: list[NarrativeActivityEntry]
+ startedAt: str | None
+ endedAt: str | None
+
+
if TYPE_CHECKING:
from skyvern.forge.sdk.copilot.diagnosis_repair_contract import DiagnosisRepairContract
from skyvern.forge.sdk.copilot.narration import NarratorState
@@ -172,6 +219,14 @@ class AgentResult:
turn_outcome: TurnOutcome | None = None
turn_id: str | None = None
narrative_summary: str | None = None
+ # Persisted on the assistant chat message so the bubble survives a reload.
+ narrative_payload: TurnNarrativePayload | None = None
+ staged_workflow_yaml: str | None = None
+ staged_workflow: Workflow | None = None
+ has_staged_proposal: bool = False
+ # Set when ``_update_workflow`` wrote canonical mid-turn (param / top-level
+ # settings changes); terminal handlers roll back on non-auto-accept.
+ canonical_was_persisted_due_to_param_change: bool = False
@dataclass
@@ -227,6 +282,7 @@ class CopilotContext(AgentContext):
# Workflow state
last_workflow: Workflow | None = None
last_workflow_yaml: str | None = None
+ # Always False under staging; ``has_staged_proposal`` carries the signal.
workflow_persisted: bool = False
last_update_block_count: int | None = None
last_test_ok: bool | None = None
@@ -343,3 +399,16 @@ class CopilotContext(AgentContext):
design_start_emitted: bool = False
design_end_emitted: bool = False
narrative_summary: str | None = None
+
+ staged_workflow_yaml: str | None = None
+ staged_workflow: Workflow | None = None
+ has_staged_proposal: bool = False
+ # Set when ``_update_workflow`` wrote canonical mid-turn (param / top-level
+ # settings changes); terminal handlers roll back on non-auto-accept.
+ canonical_was_persisted_due_to_param_change: bool = False
+ prior_block_count: int | None = None
+ block_state_map: dict[str, str] = field(default_factory=dict)
+ block_started_at_map: dict[str, str] = field(default_factory=dict)
+ block_ended_at_map: dict[str, str] = field(default_factory=dict)
+ turn_started_at: str | None = None
+ turn_ended_at: str | None = None
diff --git a/skyvern/forge/sdk/copilot/narration.py b/skyvern/forge/sdk/copilot/narration.py
index 9c0103276..5cc2f97b9 100644
--- a/skyvern/forge/sdk/copilot/narration.py
+++ b/skyvern/forge/sdk/copilot/narration.py
@@ -36,6 +36,7 @@ from skyvern.forge.sdk.schemas.workflow_copilot import (
)
if TYPE_CHECKING:
+ from skyvern.forge.sdk.copilot.context import NarrativeActivityEntry
from skyvern.forge.sdk.routes.event_source_stream import EventSourceStream
LOG = structlog.get_logger()
@@ -55,6 +56,52 @@ MAX_TOOL_ACTIVITY_BUFFER = 8
# rather than delaying narration further.
NARRATOR_TIMEOUT_SECONDS = 8.0
+# Caps on the persisted per-block and design activity logs. Mirror the FE
+# MAX_ACTIVITY_ENTRIES / MAX_DESIGN_ACTIVITY_ENTRIES in narrativeState.ts so
+# the rehydrated bubble matches what the live stream rendered.
+MAX_BLOCK_ACTIVITY_ENTRIES = 30
+MAX_DESIGN_ACTIVITY_ENTRIES = 50
+
+# Tools whose calls/results are never surfaced in the user-facing activity log.
+# Mirror of the FE ACTIVITY_TOOL_DENYLIST in narrativeState.ts.
+ACTIVITY_TOOL_DENYLIST = frozenset({"list_credentials", "get_run_results", "get_browser_screenshot"})
+
+
+def build_tool_call_activity(tool_name: str, iteration: int, tool_call_id: str) -> NarrativeActivityEntry | None:
+ if tool_name in ACTIVITY_TOOL_DENYLIST:
+ return None
+ return {
+ "kind": "tool_call",
+ "text": f"Calling {tool_name}…",
+ "iteration": iteration,
+ "toolName": tool_name,
+ "id": f"tc-{tool_call_id}",
+ }
+
+
+def build_tool_result_activity(
+ tool_name: str, summary: str, success: bool, iteration: int, tool_call_id: str
+) -> NarrativeActivityEntry | None:
+ if tool_name in ACTIVITY_TOOL_DENYLIST:
+ return None
+ return {
+ "kind": "tool_result",
+ "text": summary or tool_name,
+ "iteration": iteration,
+ "toolName": tool_name,
+ "success": success,
+ "id": f"tr-{tool_call_id}",
+ }
+
+
+def build_narration_activity(narration: str, iteration: int, timestamp: datetime) -> NarrativeActivityEntry:
+ return {
+ "kind": "narration",
+ "text": narration,
+ "iteration": iteration,
+ "id": f"n-{iteration}-{timestamp.isoformat()}",
+ }
+
class TransitionKind(StrEnum):
# Ordered by ascending priority: higher-priority transitions overwrite a
@@ -105,6 +152,26 @@ class NarratorState:
# Narrator handler resolved once per stream so per-emission calls
# don't re-hit PostHog.
resolved_handler: Any = None
+ # Persisted activity log routed to the running block (else design). The FE
+ # routes each entry to the block whose state is "running"; we cache the
+ # single running label here since copilot block runs are sequential.
+ block_activity: dict[str, list[NarrativeActivityEntry]] = field(default_factory=dict)
+ design_activity: list[NarrativeActivityEntry] = field(default_factory=list)
+ running_block_label: str | None = None
+
+ def record_activity(self, entry: NarrativeActivityEntry | None) -> None:
+ if entry is None:
+ return
+ label = self.running_block_label
+ if label is None:
+ self.design_activity.append(entry)
+ if len(self.design_activity) > MAX_DESIGN_ACTIVITY_ENTRIES:
+ del self.design_activity[:-MAX_DESIGN_ACTIVITY_ENTRIES]
+ return
+ bucket = self.block_activity.setdefault(label, [])
+ bucket.append(entry)
+ if len(bucket) > MAX_BLOCK_ACTIVITY_ENTRIES:
+ del bucket[:-MAX_BLOCK_ACTIVITY_ENTRIES]
def record_tool(
self,
@@ -261,18 +328,20 @@ async def _narration_task_body(
if not narration:
return
+ narration_ts = datetime.now(timezone.utc)
try:
await stream.send(
WorkflowCopilotNarrationUpdate(
type=WorkflowCopilotStreamMessageType.NARRATION,
narration=narration,
iteration=iteration,
- timestamp=datetime.now(timezone.utc),
+ timestamp=narration_ts,
)
)
except Exception as exc:
LOG.warning("copilot narrator send failed", error=str(exc), transition=transition_value)
return
+ state.record_activity(build_narration_activity(narration, iteration, narration_ts))
# Only advance last_emitted_at after a real delivery. A failed /
# empty / leak-dropped emission leaves the clock where it was so the
# next valid transition can emit immediately instead of waiting 10s
@@ -639,6 +708,17 @@ _BLOCK_STATUS_TO_TRANSITION: dict[str, TransitionKind] = {
"canceled": TransitionKind.BLOCK_FAILED,
}
+_TERMINAL_BLOCK_STATUSES: frozenset[str] = frozenset(
+ {
+ "completed",
+ "failed",
+ "terminated",
+ "timed_out",
+ "canceled",
+ "skipped",
+ }
+)
+
_BLOCK_TRANSITION_TO_SYNTHETIC_TOOL: dict[TransitionKind, str] = {
TransitionKind.BLOCK_STARTED: "block_started",
TransitionKind.BLOCK_COMPLETED: "block_completed",
@@ -707,7 +787,6 @@ class NarratorPollTickResult(NamedTuple):
"""Updated bookkeeping the polling loop must thread into its next call."""
prior_block_ts: datetime | None
- prior_step_ts: datetime | None
last_block_fetch_monotonic: float
@@ -715,26 +794,25 @@ async def narrator_poll_tick(
state: NarratorState,
*,
current_block_ts: datetime | None,
- current_step_ts: datetime | None,
prior_block_ts: datetime | None,
- prior_step_ts: datetime | None,
last_block_fetch_monotonic: float,
seen_block_states: dict[str, str],
fetch_block_statuses: FetchBlockStatusesCallable,
stream: EventSourceStream,
+ block_state_map: dict[str, str] | None = None,
+ block_started_at_map: dict[str, str] | None = None,
+ block_ended_at_map: dict[str, str] | None = None,
) -> NarratorPollTickResult:
- """Per-tick narrator bookkeeping; returns updated (prior_block_ts, prior_step_ts, last_block_fetch_monotonic).
+ """Per-tick narrator bookkeeping; returns updated (prior_block_ts, last_block_fetch_monotonic).
`prior_block_ts` advances only on a successful fetch so rate-limited and failed ticks retry on the next call.
"""
now = time.monotonic()
block_changed = current_block_ts != prior_block_ts
- step_changed = current_step_ts != prior_step_ts
gate_open = (now - last_block_fetch_monotonic) >= MIN_NARRATION_GAP_SECONDS
next_prior_block_ts = prior_block_ts
next_last_fetch = last_block_fetch_monotonic
- block_transition_recorded = False
if block_changed and gate_open:
next_last_fetch = now
@@ -770,13 +848,32 @@ async def narrator_poll_tick(
# Repository returns DESC by created_at; reverse for chronological order.
snapshot.reverse()
new_events = record_block_transitions(state, snapshot, seen_block_states, state.current_iteration)
- block_transition_recorded = bool(new_events)
next_prior_block_ts = current_block_ts
for event in new_events:
if not event.block_label:
# Without a label the FE has nothing readable to render; skip
# rather than ship empty bullets.
continue
+ event_ts = datetime.now(timezone.utc)
+ event_ts_iso = event_ts.isoformat()
+ if block_state_map is not None:
+ block_state_map[event.block_label] = event.status
+ if event.status == "running":
+ state.running_block_label = event.block_label
+ elif event.status in _TERMINAL_BLOCK_STATUSES and state.running_block_label == event.block_label:
+ state.running_block_label = None
+ if (
+ block_started_at_map is not None
+ and event.status == "running"
+ and event.block_label not in block_started_at_map
+ ):
+ block_started_at_map[event.block_label] = event_ts_iso
+ # Clear endedAt on retry-back-to-running; overwrite on terminal
+ # events to keep latest-terminal semantics.
+ if block_ended_at_map is not None and event.status == "running":
+ block_ended_at_map.pop(event.block_label, None)
+ if block_ended_at_map is not None and event.status in _TERMINAL_BLOCK_STATUSES:
+ block_ended_at_map[event.block_label] = event_ts_iso
try:
await stream.send(
WorkflowCopilotBlockProgressUpdate(
@@ -786,19 +883,15 @@ async def narrator_poll_tick(
block_type=event.block_type,
status=event.status,
iteration=state.current_iteration,
- timestamp=datetime.now(timezone.utc),
+ timestamp=event_ts,
)
)
except Exception:
LOG.debug("copilot block_progress send failed", exc_info=True)
- if not block_transition_recorded and step_changed:
- state.record_transition(TransitionKind.TOOL_IN_PROGRESS)
-
schedule_narration(state, stream, state.current_iteration)
return NarratorPollTickResult(
prior_block_ts=next_prior_block_ts,
- prior_step_ts=current_step_ts,
last_block_fetch_monotonic=next_last_fetch,
)
diff --git a/skyvern/forge/sdk/copilot/runtime.py b/skyvern/forge/sdk/copilot/runtime.py
index 612eaf155..17bce2351 100644
--- a/skyvern/forge/sdk/copilot/runtime.py
+++ b/skyvern/forge/sdk/copilot/runtime.py
@@ -100,6 +100,10 @@ class AgentContext:
workflow_persisted: bool = False
last_workflow: Any | None = None
last_workflow_yaml: str | None = None
+ staged_workflow_yaml: str | None = None
+ staged_workflow: Any | None = None
+ has_staged_proposal: bool = False
+ canonical_was_persisted_due_to_param_change: bool = False
allow_untested_workflow_draft: bool = False
request_policy: RequestPolicy | None = None
effective_workflow_proxy_location: Any | None = None
diff --git a/skyvern/forge/sdk/copilot/streaming_adapter.py b/skyvern/forge/sdk/copilot/streaming_adapter.py
index 28fd4976a..8ddf7c011 100644
--- a/skyvern/forge/sdk/copilot/streaming_adapter.py
+++ b/skyvern/forge/sdk/copilot/streaming_adapter.py
@@ -15,6 +15,8 @@ from skyvern.forge.request_logging import redact_sensitive_fields
from skyvern.forge.sdk.copilot.narration import (
NarratorState,
TransitionKind,
+ build_tool_call_activity,
+ build_tool_result_activity,
cancel_in_flight,
detect_transitions,
extract_tool_details,
@@ -133,6 +135,7 @@ async def stream_to_sse(
call_id = _get_raw_field(raw, "call_id") or _get_raw_field(raw, "id") or ""
tool_name = _get_raw_field(raw, "name") or "unknown"
call_id_to_name[call_id] = tool_name
+ narrator_state.record_activity(build_tool_call_activity(tool_name, iteration, call_id))
if not client_gone:
raw_args = _get_raw_field(raw, "arguments")
@@ -176,6 +179,9 @@ async def stream_to_sse(
summary = format_tool_result_for_user(tool_name, parsed)
success = parsed.get("ok", True)
detail = summarize_tool_result_detail(parsed)
+ narrator_state.record_activity(
+ build_tool_result_activity(tool_name, summary, success, iteration, call_id)
+ )
if not client_gone:
await stream.send(
@@ -338,12 +344,16 @@ def _sanitize_input(raw_args: dict[str, Any]) -> dict[str, Any]:
async def emit_turn_start(stream: EventSourceStream, ctx: CopilotContext) -> None:
mode_value = ctx.turn_intent.mode.value if ctx.turn_intent is not None else WorkflowCopilotTurnMode.UNKNOWN.value
+ now = datetime.now(timezone.utc)
+ if ctx.turn_started_at is None:
+ ctx.turn_started_at = now.isoformat()
await stream.send(
WorkflowCopilotTurnStartUpdate(
turn_id=ctx.turn_id,
turn_index=ctx.turn_index,
mode=WorkflowCopilotTurnMode(mode_value),
- timestamp=datetime.now(timezone.utc),
+ timestamp=now,
+ prior_block_count=ctx.prior_block_count,
)
)
@@ -365,9 +375,16 @@ async def maybe_emit_design_end(stream: EventSourceStream, ctx: CopilotContext)
await stream.send(WorkflowCopilotDesignEndUpdate(timestamp=datetime.now(timezone.utc)))
-async def emit_workflow_draft(stream: EventSourceStream, ctx: CopilotContext, workflow: Workflow) -> None:
- # Summary payload only: the full workflow definition is transported via
- # terminal updated_workflow on RESPONSE or via the chat's proposed_workflow.
+async def emit_workflow_draft(
+ stream: EventSourceStream,
+ ctx: CopilotContext,
+ workflow: Workflow,
+ *,
+ include_workflow: bool = True,
+) -> None:
+ """Emit a WORKFLOW_DRAFT envelope; ``include_workflow=False`` suppresses
+ the canvas auto-render for untested paths (inline REPLACE_WORKFLOW).
+ """
block_count = 0
block_labels: list[str] = []
try:
@@ -377,14 +394,19 @@ async def emit_workflow_draft(stream: EventSourceStream, ctx: CopilotContext, wo
if isinstance(label, str) and label:
block_labels.append(label)
except AttributeError:
- # Defensive: workflow without a blocks-bearing definition. Emit with
- # whatever we could collect rather than skipping the event entirely.
pass
+ workflow_dump: dict | None = None
+ if include_workflow:
+ try:
+ workflow_dump = workflow.model_dump(mode="json")
+ except Exception as dump_err:
+ LOG.warning("copilot_workflow_draft_serialization_failed", error=str(dump_err))
await stream.send(
WorkflowCopilotWorkflowDraftUpdate(
block_count=block_count,
block_labels=block_labels,
summary=None,
timestamp=datetime.now(timezone.utc),
+ workflow=workflow_dump,
)
)
diff --git a/skyvern/forge/sdk/copilot/tools.py b/skyvern/forge/sdk/copilot/tools.py
index 30b25ec74..d5ae14a6a 100644
--- a/skyvern/forge/sdk/copilot/tools.py
+++ b/skyvern/forge/sdk/copilot/tools.py
@@ -108,6 +108,7 @@ from skyvern.forge.sdk.workflow.exceptions import BaseWorkflowHTTPException
from skyvern.forge.sdk.workflow.models.parameter import (
RESERVED_PARAMETER_KEYS,
OutputParameter,
+ Parameter,
WorkflowParameter,
WorkflowParameterType,
)
@@ -1562,29 +1563,43 @@ async def _update_workflow(
)
_record_workflow_proxy_location_span(workflow_yaml, workflow)
- created_by_stamp = await resolve_copilot_created_by_stamp(ctx.workflow_id, ctx.organization_id)
-
- await app.WORKFLOW_SERVICE.update_workflow_definition(
- workflow_id=ctx.workflow_id,
- organization_id=ctx.organization_id,
- title=workflow.title,
- description=workflow.description,
- workflow_definition=workflow.workflow_definition,
- proxy_location=workflow.proxy_location,
- webhook_callback_url=workflow.webhook_callback_url,
- persist_browser_session=workflow.persist_browser_session,
- browser_profile_id=workflow.browser_profile_id,
- model=workflow.model,
- max_screenshot_scrolling_times=workflow.max_screenshot_scrolls,
- extra_http_headers=workflow.extra_http_headers,
- run_with=workflow.run_with,
- ai_fallback=workflow.ai_fallback,
- cache_key=workflow.cache_key,
- run_sequentially=workflow.run_sequentially,
- sequential_key=workflow.sequential_key,
- created_by=created_by_stamp,
- edited_by="copilot",
- )
+ # Param / top-level setting changes go through canonical because
+ # prepare_workflow and the runtime parameter-row read consume canonical
+ # values; terminal handlers roll back on non-auto-accept.
+ prior_workflow = await _get_prior_workflow(ctx)
+ requires_canonical_persist = _workflow_requires_canonical_persist(prior_workflow, workflow)
+ if requires_canonical_persist:
+ created_by_stamp = await resolve_copilot_created_by_stamp(ctx.workflow_id, ctx.organization_id)
+ await app.WORKFLOW_SERVICE.update_workflow_definition(
+ workflow_id=ctx.workflow_id,
+ organization_id=ctx.organization_id,
+ title=workflow.title,
+ description=workflow.description,
+ workflow_definition=workflow.workflow_definition,
+ proxy_location=workflow.proxy_location,
+ webhook_callback_url=workflow.webhook_callback_url,
+ totp_verification_url=workflow.totp_verification_url,
+ totp_identifier=workflow.totp_identifier,
+ persist_browser_session=workflow.persist_browser_session,
+ browser_profile_id=workflow.browser_profile_id,
+ model=workflow.model,
+ max_screenshot_scrolling_times=workflow.max_screenshot_scrolls,
+ extra_http_headers=workflow.extra_http_headers,
+ cdp_connect_headers=workflow.cdp_connect_headers,
+ run_with=workflow.run_with,
+ ai_fallback=workflow.ai_fallback,
+ cache_key=workflow.cache_key,
+ adaptive_caching=workflow.adaptive_caching,
+ code_version=workflow.code_version,
+ run_sequentially=workflow.run_sequentially,
+ sequential_key=workflow.sequential_key,
+ created_by=created_by_stamp,
+ edited_by="copilot",
+ )
+ ctx.canonical_was_persisted_due_to_param_change = True
+ ctx.staged_workflow_yaml = workflow_yaml
+ ctx.staged_workflow = workflow
+ ctx.has_staged_proposal = True
ctx.workflow_yaml = workflow_yaml
# Best-effort — narrative emit failures must never abort an
# otherwise-successful update_workflow tool call. ``isinstance``
@@ -1596,12 +1611,6 @@ async def _update_workflow(
await emit_workflow_draft(ctx.stream, ctx, workflow)
except Exception as emit_err:
LOG.warning("copilot_narrative_workflow_draft_emit_failed", error=str(emit_err))
- # Reset phase flags so a subsequent ``tool_called`` re-edge-triggers
- # ``design_start`` and a subsequent ``update_workflow`` re-emits
- # ``design_end`` — multi-iteration designs (draft → test → redraft)
- # would otherwise collapse into a single phase.
- ctx.design_start_emitted = False
- ctx.design_end_emitted = False
return {
"ok": True,
"data": {
@@ -2128,6 +2137,99 @@ async def _get_prior_workflow_definition(ctx: AgentContext) -> Any:
return None
+async def _get_prior_workflow(ctx: AgentContext) -> Workflow | None:
+ """Return the prior Workflow; in-memory > re-parsed yaml > DB."""
+ last_workflow = ctx.last_workflow
+ if last_workflow is not None:
+ return last_workflow
+ last_yaml = ctx.last_workflow_yaml
+ if last_yaml:
+ try:
+ return _process_workflow_yaml(
+ workflow_id=ctx.workflow_id,
+ workflow_permanent_id=ctx.workflow_permanent_id,
+ organization_id=ctx.organization_id,
+ workflow_yaml=last_yaml,
+ )
+ except (yaml.YAMLError, ValidationError, BaseWorkflowHTTPException):
+ pass
+ try:
+ return await app.DATABASE.workflows.get_workflow_by_permanent_id(
+ workflow_permanent_id=ctx.workflow_permanent_id,
+ organization_id=ctx.organization_id,
+ )
+ except Exception:
+ LOG.warning(
+ "Failed to fetch prior workflow for staging comparison; staging may skip a needed canonical write",
+ exc_info=True,
+ )
+ return None
+
+
+# Must stay in lockstep with the writers calling update_workflow_definition;
+# missing fields silently drop accepted settings on auto-accept.
+_CANONICAL_WORKFLOW_SETTING_FIELDS: tuple[str, ...] = (
+ "title",
+ "description",
+ "proxy_location",
+ "webhook_callback_url",
+ "totp_verification_url",
+ "totp_identifier",
+ "persist_browser_session",
+ "browser_profile_id",
+ "model",
+ "max_screenshot_scrolls",
+ "extra_http_headers",
+ "cdp_connect_headers",
+ "run_with",
+ "ai_fallback",
+ "cache_key",
+ "adaptive_caching",
+ "code_version",
+ "run_sequentially",
+ "sequential_key",
+)
+
+# convert_workflow_definition regenerates ids/timestamps per call; ignore
+# them when comparing parameters to react only to user intent.
+_PARAMETER_FINGERPRINT_VOLATILE_KEYS = frozenset(
+ {
+ "workflow_parameter_id",
+ "output_parameter_id",
+ "aws_secret_parameter_id",
+ "azure_secret_parameter_id",
+ "azure_vault_credential_parameter_id",
+ "bitwarden_credit_card_data_parameter_id",
+ "bitwarden_login_credential_parameter_id",
+ "bitwarden_sensitive_information_parameter_id",
+ "credential_parameter_id",
+ "onepassword_credential_parameter_id",
+ "created_at",
+ "modified_at",
+ }
+)
+
+
+def _stable_parameter_fingerprint(parameter: Parameter) -> dict[str, Any]:
+ dump = parameter.model_dump(mode="json")
+ return {k: v for k, v in dump.items() if k not in _PARAMETER_FINGERPRINT_VOLATILE_KEYS}
+
+
+def _workflow_requires_canonical_persist(prior: Workflow | None, new: Workflow) -> bool:
+ if prior is None:
+ return False
+ for field_name in _CANONICAL_WORKFLOW_SETTING_FIELDS:
+ if getattr(prior, field_name, None) != getattr(new, field_name, None):
+ return True
+ prior_params = prior.workflow_definition.parameters
+ new_params = new.workflow_definition.parameters
+ if len(prior_params) != len(new_params):
+ return True
+ prior_fingerprints = [_stable_parameter_fingerprint(p) for p in prior_params]
+ new_fingerprints = [_stable_parameter_fingerprint(p) for p in new_params]
+ return prior_fingerprints != new_fingerprints
+
+
def _plan_frontier(
ctx: Any,
requested_labels: list[str],
@@ -2450,10 +2552,13 @@ async def _run_blocks_and_collect_debug(
# right moment to drop stale outputs. Full success at the end of this
# function updates verified state in place (overwriting re-run labels).
- workflow = await app.DATABASE.workflows.get_workflow_by_permanent_id(
- workflow_permanent_id=ctx.workflow_permanent_id,
- organization_id=ctx.organization_id,
- )
+ # Common-case staging leaves the canonical row stale; prefer the staged copy.
+ workflow = ctx.staged_workflow
+ if workflow is None:
+ workflow = await app.DATABASE.workflows.get_workflow_by_permanent_id(
+ workflow_permanent_id=ctx.workflow_permanent_id,
+ organization_id=ctx.organization_id,
+ )
if not workflow:
return {"ok": False, "error": f"Workflow not found: {ctx.workflow_permanent_id}"}
@@ -2616,7 +2721,6 @@ async def _run_blocks_and_collect_debug(
narrator_enabled = narrator_state is not None and narration_handler_available()
seen_block_states: dict[str, str] = {}
prior_block_ts: datetime | None = initial_block_ts
- prior_step_ts: datetime | None = initial_step_ts
last_block_fetch_monotonic = 0.0
try:
@@ -2630,9 +2734,7 @@ async def _run_blocks_and_collect_debug(
tick_result = await narrator_poll_tick(
narrator_state,
current_block_ts=block_ts,
- current_step_ts=step_ts,
prior_block_ts=prior_block_ts,
- prior_step_ts=prior_step_ts,
last_block_fetch_monotonic=last_block_fetch_monotonic,
seen_block_states=seen_block_states,
fetch_block_statuses=lambda: app.DATABASE.observer.get_workflow_run_blocks(
@@ -2640,9 +2742,11 @@ async def _run_blocks_and_collect_debug(
organization_id=ctx.organization_id,
),
stream=ctx.stream,
+ block_state_map=ctx.block_state_map,
+ block_started_at_map=ctx.block_started_at_map,
+ block_ended_at_map=ctx.block_ended_at_map,
)
prior_block_ts = tick_result.prior_block_ts
- prior_step_ts = tick_result.prior_step_ts
last_block_fetch_monotonic = tick_result.last_block_fetch_monotonic
if run and WorkflowRunStatus(run.status).is_final():
@@ -3552,7 +3656,6 @@ def _record_workflow_update_result(copilot_ctx: Any, result: dict[str, Any]) ->
# to "verify the URL" for a URL they just corrected in the new draft.
copilot_ctx.last_test_non_retriable_nav_error = None
copilot_ctx.non_retriable_nav_error_last_emitted_signature = None
- copilot_ctx.workflow_persisted = True
# Block-running failures keyed off (labels, parameters) go stale once the
# workflow itself changes — without this clear, a user who fixes the bug
diff --git a/skyvern/forge/sdk/db/models.py b/skyvern/forge/sdk/db/models.py
index c86c318ce..bb7ad6a5e 100644
--- a/skyvern/forge/sdk/db/models.py
+++ b/skyvern/forge/sdk/db/models.py
@@ -1437,6 +1437,7 @@ class WorkflowCopilotChatMessageModel(Base):
content = Column(UnicodeText, nullable=False)
global_llm_context = Column(UnicodeText, nullable=True)
turn_outcome = Column(JSON, nullable=True)
+ narrative_payload = Column(JSON, nullable=True)
created_at = Column(DateTime, default=datetime.datetime.utcnow, nullable=False)
modified_at = Column(
diff --git a/skyvern/forge/sdk/db/repositories/workflow_parameters.py b/skyvern/forge/sdk/db/repositories/workflow_parameters.py
index c894212e0..1a79bdb90 100644
--- a/skyvern/forge/sdk/db/repositories/workflow_parameters.py
+++ b/skyvern/forge/sdk/db/repositories/workflow_parameters.py
@@ -521,6 +521,7 @@ class WorkflowParametersRepository(BaseRepository):
content: str,
global_llm_context: str | None = None,
turn_outcome: TurnOutcome | None = None,
+ narrative_payload: dict | None = None,
) -> WorkflowCopilotChatMessage:
async with self.Session() as session:
new_message = WorkflowCopilotChatMessageModel(
@@ -530,6 +531,7 @@ class WorkflowParametersRepository(BaseRepository):
content=content,
global_llm_context=global_llm_context,
turn_outcome=turn_outcome.model_dump(mode="json") if turn_outcome is not None else None,
+ narrative_payload=narrative_payload,
)
session.add(new_message)
await session.commit()
diff --git a/skyvern/forge/sdk/db/repositories/workflows.py b/skyvern/forge/sdk/db/repositories/workflows.py
index fe4d9ced7..171a8f9b3 100644
--- a/skyvern/forge/sdk/db/repositories/workflows.py
+++ b/skyvern/forge/sdk/db/repositories/workflows.py
@@ -626,11 +626,13 @@ class WorkflowsRepository(BaseRepository):
version: int | None = None,
run_with: str | None = None,
cache_key: str | None = None,
- code_version: int | None = None,
+ code_version: int | None | object = _UNSET,
status: str | None = None,
import_error: str | None = None,
proxy_location: ProxyLocationInput | object = _UNSET,
webhook_callback_url: str | None | object = _UNSET,
+ totp_verification_url: str | None | object = _UNSET,
+ totp_identifier: str | None | object = _UNSET,
persist_browser_session: bool | None = None,
browser_profile_id: str | None | object = _UNSET,
model: dict[str, Any] | None | object = _UNSET,
@@ -639,6 +641,7 @@ class WorkflowsRepository(BaseRepository):
extra_http_headers: dict[str, str] | None | object = _UNSET,
cdp_connect_headers: dict[str, str] | None | object = _UNSET,
ai_fallback: bool | None = None,
+ adaptive_caching: bool | object = _UNSET,
run_sequentially: bool | None = None,
sequential_key: str | None | object = _UNSET,
created_by: str | None | object = _UNSET,
@@ -663,8 +666,8 @@ class WorkflowsRepository(BaseRepository):
workflow.run_with = run_with
if cache_key is not None:
workflow.cache_key = cache_key
- if code_version is not None:
- workflow.code_version = code_version
+ if code_version is not _UNSET:
+ workflow.code_version = cast(int | None, code_version)
if status is not None:
workflow.status = status
if import_error is not None:
@@ -673,6 +676,10 @@ class WorkflowsRepository(BaseRepository):
workflow.proxy_location = serialize_proxy_location(cast(ProxyLocationInput, proxy_location))
if webhook_callback_url is not _UNSET:
workflow.webhook_callback_url = webhook_callback_url
+ if totp_verification_url is not _UNSET:
+ workflow.totp_verification_url = cast(str | None, totp_verification_url)
+ if totp_identifier is not _UNSET:
+ workflow.totp_identifier = cast(str | None, totp_identifier)
if persist_browser_session is not None:
workflow.persist_browser_session = persist_browser_session
if browser_profile_id is not _UNSET:
@@ -689,6 +696,8 @@ class WorkflowsRepository(BaseRepository):
workflow.cdp_connect_headers = cdp_connect_headers
if ai_fallback is not None:
workflow.ai_fallback = ai_fallback
+ if adaptive_caching is not _UNSET:
+ workflow.adaptive_caching = cast(bool, adaptive_caching)
if run_sequentially is not None:
workflow.run_sequentially = run_sequentially
if sequential_key is not _UNSET:
@@ -927,10 +936,13 @@ class WorkflowsRepository(BaseRepository):
version: int | None = None,
run_with: str | None = None,
cache_key: str | None = None,
+ code_version: int | None | object = _UNSET,
status: str | None = None,
import_error: str | None = None,
proxy_location: ProxyLocationInput | object = _UNSET,
webhook_callback_url: str | None | object = _UNSET,
+ totp_verification_url: str | None | object = _UNSET,
+ totp_identifier: str | None | object = _UNSET,
persist_browser_session: bool | None = None,
browser_profile_id: str | None | object = _UNSET,
model: dict[str, Any] | None | object = _UNSET,
@@ -939,6 +951,7 @@ class WorkflowsRepository(BaseRepository):
extra_http_headers: dict[str, str] | None | object = _UNSET,
cdp_connect_headers: dict[str, str] | None | object = _UNSET,
ai_fallback: bool | None = None,
+ adaptive_caching: bool | object = _UNSET,
run_sequentially: bool | None = None,
sequential_key: str | None | object = _UNSET,
created_by: str | None | object = _UNSET,
@@ -999,6 +1012,8 @@ class WorkflowsRepository(BaseRepository):
workflow.run_with = run_with
if cache_key is not None:
workflow.cache_key = cache_key
+ if code_version is not _UNSET:
+ workflow.code_version = cast(int | None, code_version)
if status is not None:
workflow.status = status
if import_error is not None:
@@ -1007,6 +1022,10 @@ class WorkflowsRepository(BaseRepository):
workflow.proxy_location = serialize_proxy_location(cast(ProxyLocationInput, proxy_location))
if webhook_callback_url is not _UNSET:
workflow.webhook_callback_url = webhook_callback_url
+ if totp_verification_url is not _UNSET:
+ workflow.totp_verification_url = cast(str | None, totp_verification_url)
+ if totp_identifier is not _UNSET:
+ workflow.totp_identifier = cast(str | None, totp_identifier)
if persist_browser_session is not None:
workflow.persist_browser_session = persist_browser_session
if browser_profile_id is not _UNSET:
@@ -1023,6 +1042,8 @@ class WorkflowsRepository(BaseRepository):
workflow.cdp_connect_headers = cdp_connect_headers
if ai_fallback is not None:
workflow.ai_fallback = ai_fallback
+ if adaptive_caching is not _UNSET:
+ workflow.adaptive_caching = cast(bool, adaptive_caching)
if run_sequentially is not None:
workflow.run_sequentially = run_sequentially
if sequential_key is not _UNSET:
diff --git a/skyvern/forge/sdk/db/utils.py b/skyvern/forge/sdk/db/utils.py
index 335cac726..7e0b44d49 100644
--- a/skyvern/forge/sdk/db/utils.py
+++ b/skyvern/forge/sdk/db/utils.py
@@ -1,6 +1,6 @@
import json
import typing
-from typing import Any
+from typing import TYPE_CHECKING, Any
import pydantic
import pydantic.json
@@ -100,6 +100,9 @@ from skyvern.webeye.actions.actions import (
WaitAction,
)
+if TYPE_CHECKING:
+ from skyvern.forge.sdk.copilot.context import TurnNarrativePayload
+
LOG = structlog.get_logger()
@@ -375,6 +378,10 @@ def convert_to_workflow_copilot_chat_message(
exc_info=exc,
)
parsed_outcome = None
+ raw_narrative = message_model.narrative_payload
+ parsed_narrative = typing.cast(
+ "TurnNarrativePayload | None", raw_narrative if isinstance(raw_narrative, dict) else None
+ )
return WorkflowCopilotChatMessageSchema(
workflow_copilot_chat_message_id=message_model.workflow_copilot_chat_message_id,
workflow_copilot_chat_id=message_model.workflow_copilot_chat_id,
@@ -382,6 +389,7 @@ def convert_to_workflow_copilot_chat_message(
content=message_model.content,
global_llm_context=message_model.global_llm_context,
turn_outcome=parsed_outcome,
+ narrative_payload=parsed_narrative,
created_at=message_model.created_at,
modified_at=message_model.modified_at,
)
diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.py
index 5611023eb..f8db8d99e 100644
--- a/skyvern/forge/sdk/routes/workflow_copilot.py
+++ b/skyvern/forge/sdk/routes/workflow_copilot.py
@@ -24,10 +24,10 @@ from skyvern.forge.sdk.api.llm.api_handler import LLMAPIHandler
from skyvern.forge.sdk.api.llm.exceptions import LLMProviderError
from skyvern.forge.sdk.artifact.models import Artifact, ArtifactType
from skyvern.forge.sdk.copilot.agent import run_copilot_agent
-from skyvern.forge.sdk.copilot.attribution import is_copilot_born_initial_write
+from skyvern.forge.sdk.copilot.attribution import is_copilot_born_initial_write, resolve_copilot_created_by_stamp
from skyvern.forge.sdk.copilot.block_type_aliases import normalize_copilot_block_type_alias
from skyvern.forge.sdk.copilot.config import CopilotConfig
-from skyvern.forge.sdk.copilot.context import AgentResult, ProposalDisposition
+from skyvern.forge.sdk.copilot.context import AgentResult, ProposalDisposition, TurnNarrativePayload
from skyvern.forge.sdk.copilot.output_utils import truncate_output
from skyvern.forge.sdk.copilot.recoverable_failure import (
RecoverableFailure,
@@ -287,14 +287,25 @@ def _effective_auto_accept(auto_accept: bool | None, agent_result: object | None
def _should_restore_persisted_workflow(auto_accept: bool | None, agent_result: object | None) -> bool:
- """Restore when a mid-turn ``_update_workflow`` commit isn't covered by an accepted proposal."""
- if not bool(getattr(agent_result, "workflow_was_persisted", False)):
+ """Restore when a mid-turn canonical write isn't covered by an accepted proposal."""
+ legacy_persisted = bool(getattr(agent_result, "workflow_was_persisted", False))
+ degraded_persisted = bool(getattr(agent_result, "canonical_was_persisted_due_to_param_change", False))
+ if not (legacy_persisted or degraded_persisted):
return False
if getattr(agent_result, "updated_workflow", None) is None:
return True
return not _effective_auto_accept(auto_accept, agent_result)
+def _should_commit_staged_workflow(auto_accept: bool | None, agent_result: object | None) -> bool:
+ """Auto-accept commits the final staged workflow even after a mid-turn degraded
+ write: a later blocks-only edit stages without persisting, so only this terminal
+ commit reconciles canonical with the proposal the user sees."""
+ if not _effective_auto_accept(auto_accept, agent_result):
+ return False
+ return bool(getattr(agent_result, "has_staged_proposal", False))
+
+
def _record_recoverable_failure_span_attrs(
failure: RecoverableFailure,
*,
@@ -310,6 +321,25 @@ def _record_recoverable_failure_span_attrs(
current_span.set_attribute("copilot.error_reply_proposal_disposition", proposal_disposition)
+def _make_error_narrative_payload(turn_id: str | None, turn_index: int | None, message: str) -> TurnNarrativePayload:
+ return {
+ "turnId": turn_id,
+ "turnIndex": turn_index if turn_index is not None else 0,
+ "mode": "unknown",
+ "designStarted": False,
+ "designEnded": True,
+ "draft": None,
+ "blocks": [],
+ "terminal": "error",
+ "terminalMessage": message,
+ "narrativeSummary": message,
+ "priorBlockCount": None,
+ "designActivity": [],
+ "startedAt": None,
+ "endedAt": None,
+ }
+
+
def _build_recoverable_route_agent_result(
error: BaseException,
*,
@@ -317,16 +347,19 @@ def _build_recoverable_route_agent_result(
clear_proposed_workflow: bool,
global_llm_context: str | None,
turn_id: str | None = None,
+ turn_index: int | None = None,
) -> tuple[AgentResult, RecoverableFailure]:
failure = build_recoverable_failure(error, workflow_modified=workflow_modified)
+ user_response = format_recoverable_failure_reply(failure)
agent_result = AgentResult(
- user_response=format_recoverable_failure_reply(failure),
+ user_response=user_response,
updated_workflow=None,
global_llm_context=merge_failure_into_context(global_llm_context, failure),
workflow_was_persisted=False,
proposal_disposition="no_proposal",
clear_proposed_workflow=clear_proposed_workflow,
turn_id=turn_id,
+ narrative_payload=_make_error_narrative_payload(turn_id, turn_index, user_response),
)
_record_recoverable_failure_span_attrs(failure, proposal_disposition="no_proposal")
return agent_result, failure
@@ -413,7 +446,14 @@ async def _persist_cancel_turn(
else:
restored = _should_restore_persisted_workflow(chat.auto_accept, agent_result)
if restored:
- await asyncio.shield(_restore_workflow_definition(original_workflow, organization_id))
+ try:
+ await asyncio.shield(_restore_workflow_definition(original_workflow, organization_id))
+ except Exception:
+ LOG.warning(
+ "Workflow restore failed inside cancel-turn handler",
+ organization_id=organization_id,
+ exc_info=True,
+ )
if agent_result.updated_workflow is None and chat.proposed_workflow is not None:
await asyncio.shield(_clear_proposed_workflow(chat))
else:
@@ -442,6 +482,7 @@ async def _persist_cancel_turn(
content=user_response,
global_llm_context=updated_global_llm_context,
turn_outcome=turn_outcome,
+ narrative_payload=getattr(agent_result, "narrative_payload", None),
)
)
proposal_disposition = _proposal_disposition(agent_result)
@@ -504,7 +545,25 @@ async def _finalise_normal_turn(
# on.
restored = _should_restore_persisted_workflow(chat.auto_accept, agent_result)
if restored:
- await _restore_workflow_definition(original_workflow, organization_id)
+ try:
+ await _restore_workflow_definition(original_workflow, organization_id)
+ except Exception:
+ LOG.warning("copilot restore failed in _finalise_normal_turn", exc_info=True)
+
+ if _should_commit_staged_workflow(chat.auto_accept, agent_result):
+ try:
+ await _commit_staged_workflow(
+ organization_id=organization_id,
+ workflow_id=chat_request.workflow_id,
+ staged_workflow=agent_result.staged_workflow,
+ )
+ except Exception:
+ # Undo any mid-turn degraded write so a failed commit fails the turn
+ # atomically instead of leaving canonical on a partial intermediate.
+ LOG.warning("copilot auto-accept commit failed; rolling back canonical", exc_info=True)
+ with contextlib.suppress(Exception):
+ await _restore_workflow_definition(original_workflow, organization_id)
+ raise
await _persist_proposed_workflow_state(chat, agent_result, restored)
@@ -522,6 +581,7 @@ async def _finalise_normal_turn(
content=user_response,
global_llm_context=updated_global_llm_context,
turn_outcome=agent_result.turn_outcome,
+ narrative_payload=getattr(agent_result, "narrative_payload", None),
)
proposal_disposition = _proposal_disposition(agent_result)
@@ -542,34 +602,82 @@ async def _finalise_normal_turn(
)
+async def _commit_staged_workflow(
+ *,
+ organization_id: str,
+ workflow_id: str,
+ staged_workflow: Workflow | None,
+) -> None:
+ """Overwrite the current workflow version in place (auto-accept path).
+
+ Manual Accept via /workflow/copilot/apply-proposed-workflow creates a new
+ version instead. Field list must stay in lockstep with ``_update_workflow``.
+ """
+ if staged_workflow is None:
+ return
+ created_by_stamp = await resolve_copilot_created_by_stamp(workflow_id, organization_id)
+ await app.WORKFLOW_SERVICE.update_workflow_definition(
+ workflow_id=workflow_id,
+ organization_id=organization_id,
+ title=staged_workflow.title,
+ description=staged_workflow.description,
+ workflow_definition=staged_workflow.workflow_definition,
+ proxy_location=staged_workflow.proxy_location,
+ webhook_callback_url=staged_workflow.webhook_callback_url,
+ totp_verification_url=staged_workflow.totp_verification_url,
+ totp_identifier=staged_workflow.totp_identifier,
+ persist_browser_session=staged_workflow.persist_browser_session,
+ browser_profile_id=staged_workflow.browser_profile_id,
+ model=staged_workflow.model,
+ max_screenshot_scrolling_times=staged_workflow.max_screenshot_scrolls,
+ extra_http_headers=staged_workflow.extra_http_headers,
+ cdp_connect_headers=staged_workflow.cdp_connect_headers,
+ run_with=staged_workflow.run_with,
+ ai_fallback=staged_workflow.ai_fallback,
+ cache_key=staged_workflow.cache_key,
+ adaptive_caching=staged_workflow.adaptive_caching,
+ code_version=staged_workflow.code_version,
+ run_sequentially=staged_workflow.run_sequentially,
+ sequential_key=staged_workflow.sequential_key,
+ created_by=created_by_stamp,
+ edited_by="copilot",
+ )
+
+
async def _restore_workflow_definition(original_workflow: Workflow | None, organization_id: str) -> None:
"""Roll the workflow back to ``original_workflow``.
- Unconditional restore helper. Callers must first gate this with
- ``_should_restore_persisted_workflow`` so success, disconnect, and exception
- paths all apply the same rollback rule: only restore when the user did not
- opt into auto-accept AND the agent loop actually persisted a mid-request
- draft.
+ Field list must stay in lockstep with ``_update_workflow``. May raise; callers
+ treat a restore failure as best-effort (log and continue), not a hard error.
"""
if not original_workflow:
return
- try:
- # Forward attribution so rollback reverts it alongside the definition.
- await app.WORKFLOW_SERVICE.update_workflow_definition(
- workflow_id=original_workflow.workflow_id,
- organization_id=organization_id,
- title=original_workflow.title,
- description=original_workflow.description,
- workflow_definition=original_workflow.workflow_definition,
- created_by=original_workflow.created_by,
- edited_by=original_workflow.edited_by,
- )
- except Exception:
- LOG.warning(
- "Failed to restore original workflow",
- workflow_id=original_workflow.workflow_id,
- exc_info=True,
- )
+ await app.WORKFLOW_SERVICE.update_workflow_definition(
+ workflow_id=original_workflow.workflow_id,
+ organization_id=organization_id,
+ title=original_workflow.title,
+ description=original_workflow.description,
+ workflow_definition=original_workflow.workflow_definition,
+ proxy_location=original_workflow.proxy_location,
+ webhook_callback_url=original_workflow.webhook_callback_url,
+ totp_verification_url=original_workflow.totp_verification_url,
+ totp_identifier=original_workflow.totp_identifier,
+ persist_browser_session=original_workflow.persist_browser_session,
+ browser_profile_id=original_workflow.browser_profile_id,
+ model=original_workflow.model,
+ max_screenshot_scrolling_times=original_workflow.max_screenshot_scrolls,
+ extra_http_headers=original_workflow.extra_http_headers,
+ cdp_connect_headers=original_workflow.cdp_connect_headers,
+ run_with=original_workflow.run_with,
+ ai_fallback=original_workflow.ai_fallback,
+ cache_key=original_workflow.cache_key,
+ adaptive_caching=original_workflow.adaptive_caching,
+ code_version=original_workflow.code_version,
+ run_sequentially=original_workflow.run_sequentially,
+ sequential_key=original_workflow.sequential_key,
+ created_by=original_workflow.created_by,
+ edited_by=original_workflow.edited_by,
+ )
async def _get_debug_artifact(organization_id: str, workflow_run_id: str) -> Artifact | None:
@@ -1165,14 +1273,19 @@ def _process_workflow_yaml(
workflow_definition=updated_workflow_definition,
proxy_location=workflow_yaml_request.proxy_location,
webhook_callback_url=workflow_yaml_request.webhook_callback_url,
+ totp_verification_url=workflow_yaml_request.totp_verification_url,
+ totp_identifier=workflow_yaml_request.totp_identifier,
persist_browser_session=workflow_yaml_request.persist_browser_session or False,
browser_profile_id=workflow_yaml_request.browser_profile_id,
model=workflow_yaml_request.model,
max_screenshot_scrolls=workflow_yaml_request.max_screenshot_scrolls,
extra_http_headers=workflow_yaml_request.extra_http_headers,
+ cdp_connect_headers=workflow_yaml_request.cdp_connect_headers,
run_with=workflow_yaml_request.run_with,
ai_fallback=workflow_yaml_request.ai_fallback,
cache_key=workflow_yaml_request.cache_key,
+ adaptive_caching=workflow_yaml_request.adaptive_caching,
+ code_version=workflow_yaml_request.code_version,
run_sequentially=workflow_yaml_request.run_sequentially,
sequential_key=workflow_yaml_request.sequential_key,
created_at=now,
@@ -1525,6 +1638,15 @@ async def _new_copilot_chat_post(
# about-to-start turn.
turn_index = sum(1 for m in chat_messages if m.sender == WorkflowCopilotChatSender.USER)
+ # Prefer the FE-submitted yaml — canonical still has 0 blocks
+ # mid-iteration before Accept and would mis-classify the chip.
+ prior_block_count: int | None = None
+ submitted_count = _workflow_yaml_block_count(chat_request.workflow_yaml)
+ if submitted_count > 0:
+ prior_block_count = submitted_count
+ elif original_workflow is not None and original_workflow.workflow_definition is not None:
+ prior_block_count = len(original_workflow.workflow_definition.blocks or [])
+
with bind_copilot_session_id(chat.workflow_copilot_chat_id):
agent_result = await run_copilot_agent(
stream=stream,
@@ -1539,6 +1661,7 @@ async def _new_copilot_chat_post(
turn_index=turn_index,
turn_id=turn_id,
prior_copilot_workflow_yaml=prior_copilot_workflow_yaml,
+ prior_block_count=prior_block_count,
)
if getattr(agent_result, "cancelled", False):
@@ -1576,7 +1699,14 @@ async def _new_copilot_chat_post(
terminal_frame_emitted = True
except HTTPException 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)
+ try:
+ await _restore_workflow_definition(original_workflow, organization.organization_id)
+ except Exception:
+ LOG.warning(
+ "Workflow restore failed inside HTTPException handler",
+ organization_id=organization.organization_id,
+ exc_info=True,
+ )
terminal_frame_emitted = True
await stream.send(
WorkflowCopilotStreamErrorUpdate(
@@ -1589,7 +1719,14 @@ 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:
- await _restore_workflow_definition(original_workflow, organization.organization_id)
+ 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(
@@ -1598,6 +1735,7 @@ async def _new_copilot_chat_post(
clear_proposed_workflow=restored or workflow_modified,
global_llm_context=global_llm_context,
turn_id=turn_id,
+ turn_index=turn_index,
)
LOG.error(
"LLM provider error translated to recoverable workflow copilot v2 reply",
@@ -1637,7 +1775,14 @@ async def _new_copilot_chat_post(
)
except asyncio.CancelledError:
if chat is not None and _should_restore_persisted_workflow(chat.auto_accept, agent_result):
- await asyncio.shield(_restore_workflow_definition(original_workflow, organization.organization_id))
+ try:
+ await asyncio.shield(_restore_workflow_definition(original_workflow, organization.organization_id))
+ except Exception:
+ LOG.warning(
+ "Workflow restore failed inside cancel-error handler",
+ organization_id=organization.organization_id,
+ exc_info=True,
+ )
if user_cancel_observed[0] and chat is not None and agent_result is None:
# User cancel landed before the agent started running, so
# the agent_result.cancelled branch above couldn't run.
@@ -1674,7 +1819,14 @@ async def _new_copilot_chat_post(
except Exception as exc:
restored = chat is not None and _should_restore_persisted_workflow(chat.auto_accept, agent_result)
if restored:
- await _restore_workflow_definition(original_workflow, organization.organization_id)
+ 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(
@@ -1683,6 +1835,7 @@ async def _new_copilot_chat_post(
clear_proposed_workflow=restored or workflow_modified,
global_llm_context=global_llm_context,
turn_id=turn_id,
+ turn_index=turn_index,
)
LOG.error(
"Unexpected workflow copilot v2 error translated to recoverable reply",
@@ -2079,6 +2232,7 @@ def convert_to_history_messages(
content=message.content,
turn_outcome=message.turn_outcome,
created_at=message.created_at,
+ narrative_payload=message.narrative_payload,
)
for message in messages
]
diff --git a/skyvern/forge/sdk/schemas/workflow_copilot.py b/skyvern/forge/sdk/schemas/workflow_copilot.py
index 02a11cb1e..9526f7d44 100644
--- a/skyvern/forge/sdk/schemas/workflow_copilot.py
+++ b/skyvern/forge/sdk/schemas/workflow_copilot.py
@@ -4,7 +4,7 @@ from typing import Any
from pydantic import BaseModel, ConfigDict, Field
-from skyvern.forge.sdk.copilot.context import ProposalDisposition, ResponseType
+from skyvern.forge.sdk.copilot.context import ProposalDisposition, ResponseType, TurnNarrativePayload
from skyvern.forge.sdk.schemas.copilot_turn_outcome import TurnOutcome
@@ -49,6 +49,10 @@ class WorkflowCopilotChatMessage(BaseModel):
content: str = Field(..., description="Message content")
global_llm_context: str | None = Field(None, description="Optional global LLM context for the message")
turn_outcome: TurnOutcome | None = Field(None, description="Typed turn outcome (assistant rows)")
+ narrative_payload: TurnNarrativePayload | None = Field(
+ None,
+ description="Persisted narrative bubble snapshot; lets a reload re-render per-block cards.",
+ )
created_at: datetime = Field(..., description="When the message was created")
modified_at: datetime = Field(..., description="When the message was last modified")
@@ -94,6 +98,10 @@ class WorkflowCopilotChatHistoryMessage(BaseModel):
sender: WorkflowCopilotChatSender = Field(..., description="Message sender")
content: str = Field(..., description="Message content")
turn_outcome: TurnOutcome | None = Field(None, description="Typed turn outcome (assistant rows only)")
+ narrative_payload: TurnNarrativePayload | None = Field(
+ None,
+ description="Persisted narrative bubble snapshot; lets a reload re-render per-block cards.",
+ )
created_at: datetime = Field(..., description="When the message was created")
@@ -247,6 +255,10 @@ class WorkflowCopilotTurnStartUpdate(BaseModel):
turn_index: int = Field(..., description="Zero-based ordinal of this turn within the chat")
mode: WorkflowCopilotTurnMode = Field(..., description="TurnIntent mode for this turn")
timestamp: datetime = Field(..., description="Server timestamp")
+ prior_block_count: int | None = Field(
+ None,
+ description="Block count of the canonical workflow at turn entry; drives the FE edit-vs-build chip.",
+ )
class WorkflowCopilotDesignStartUpdate(BaseModel):
@@ -264,10 +276,6 @@ class WorkflowCopilotDesignEndUpdate(BaseModel):
class WorkflowCopilotWorkflowDraftUpdate(BaseModel):
- # Summary-only payload. The staged/persisted workflow definition is
- # transported elsewhere (terminal `updated_workflow` on RESPONSE, or
- # the chat's `proposed_workflow` field); this event signals only that a
- # draft was produced and what shape it has.
type: WorkflowCopilotStreamMessageType = Field(
WorkflowCopilotStreamMessageType.WORKFLOW_DRAFT, description="Message type"
)
@@ -275,6 +283,10 @@ class WorkflowCopilotWorkflowDraftUpdate(BaseModel):
block_labels: list[str] = Field(default_factory=list, description="Ordered block labels in the drafted workflow")
summary: str | None = Field(None, description="Optional one-line description; populated by a follow-up PR")
timestamp: datetime = Field(..., description="Server timestamp")
+ workflow: dict | None = Field(
+ None,
+ description="Staged workflow API response (same shape as terminal RESPONSE.updated_workflow). Drives mid-turn canvas updates.",
+ )
class WorkflowYAMLConversionRequest(BaseModel):
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index 8511a12c4..bb249e486 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -4117,6 +4117,8 @@ class WorkflowService:
workflow_definition: WorkflowDefinition | None = None,
proxy_location: ProxyLocationInput | object = _UNSET,
webhook_callback_url: str | None | object = _UNSET,
+ totp_verification_url: str | None | object = _UNSET,
+ totp_identifier: str | None | object = _UNSET,
persist_browser_session: bool | None = None,
browser_profile_id: str | None | object = _UNSET,
model: dict[str, Any] | None | object = _UNSET,
@@ -4127,6 +4129,8 @@ class WorkflowService:
run_with: str | None = None,
ai_fallback: bool | None = None,
cache_key: str | None = None,
+ adaptive_caching: bool | object = _UNSET,
+ code_version: int | None | object = _UNSET,
run_sequentially: bool | None = None,
sequential_key: str | None | object = _UNSET,
created_by: str | None | object = _UNSET,
@@ -4141,6 +4145,8 @@ class WorkflowService:
workflow_definition=workflow_definition,
proxy_location=proxy_location,
webhook_callback_url=webhook_callback_url,
+ totp_verification_url=totp_verification_url,
+ totp_identifier=totp_identifier,
persist_browser_session=persist_browser_session,
browser_profile_id=browser_profile_id,
model=model,
@@ -4151,6 +4157,8 @@ class WorkflowService:
run_with=run_with,
ai_fallback=ai_fallback,
cache_key=cache_key,
+ adaptive_caching=adaptive_caching,
+ code_version=code_version,
run_sequentially=run_sequentially,
sequential_key=sequential_key,
created_by=created_by,
@@ -4166,6 +4174,8 @@ class WorkflowService:
workflow_definition=None,
proxy_location=proxy_location,
webhook_callback_url=webhook_callback_url,
+ totp_verification_url=totp_verification_url,
+ totp_identifier=totp_identifier,
persist_browser_session=persist_browser_session,
browser_profile_id=browser_profile_id,
model=model,
@@ -4176,6 +4186,8 @@ class WorkflowService:
run_with=run_with,
ai_fallback=ai_fallback,
cache_key=cache_key,
+ adaptive_caching=adaptive_caching,
+ code_version=code_version,
run_sequentially=run_sequentially,
sequential_key=sequential_key,
created_by=created_by,
diff --git a/tests/unit/forge/sdk/db/test_update_workflow_definition_fields.py b/tests/unit/forge/sdk/db/test_update_workflow_definition_fields.py
index b84759b46..a9b7a13f2 100644
--- a/tests/unit/forge/sdk/db/test_update_workflow_definition_fields.py
+++ b/tests/unit/forge/sdk/db/test_update_workflow_definition_fields.py
@@ -1,13 +1,14 @@
"""Regression tests for ``WorkflowsRepository.update_workflow_and_reconcile_definition_params``.
-Covers the 12 workflow-level fields that were newly threaded through
+Covers the 16 workflow-level fields that were newly threaded through
``WorkflowService.update_workflow_definition`` in the copilot-v2 stack:
-- ``_UNSET``-guarded (7): ``proxy_location``, ``webhook_callback_url``,
+- ``_UNSET``-guarded (11): ``proxy_location``, ``webhook_callback_url``,
``model``, ``max_screenshot_scrolling_times``, ``extra_http_headers``,
- ``sequential_key``, ``browser_profile_id``. Omitting the kwarg must
+ ``sequential_key``, ``browser_profile_id``, ``totp_verification_url``,
+ ``totp_identifier``, ``adaptive_caching``, ``code_version``. Omitting the kwarg must
leave the persisted value unchanged; passing explicit ``None`` clears
- the column.
+ nullable columns.
- bare-``None`` (5): ``persist_browser_session``, ``run_with``,
``ai_fallback``, ``cache_key``, ``run_sequentially``. Both omitting
the kwarg and passing ``None`` must leave the persisted value
@@ -51,7 +52,7 @@ async def agent_db(db_engine: Any) -> AsyncGenerator[AgentDB]:
@pytest_asyncio.fixture
async def seeded_workflow(agent_db: AgentDB) -> dict[str, str]:
- """Create a workflow with every one of the 11 threaded fields pre-set.
+ """Create a workflow with every one of the threaded fields pre-set.
Distinct, non-default values are chosen so a silent clobber to ``None``
or the column default is observable in assertions.
@@ -67,6 +68,8 @@ async def seeded_workflow(agent_db: AgentDB) -> dict[str, str]:
organization_id=org.organization_id,
proxy_location=ProxyLocation.RESIDENTIAL,
webhook_callback_url="https://example.com/webhook",
+ totp_verification_url="https://example.com/totp",
+ totp_identifier="seed-totp-identifier",
max_screenshot_scrolling_times=7,
extra_http_headers={"X-Seed": "yes"},
persist_browser_session=True,
@@ -75,6 +78,8 @@ async def seeded_workflow(agent_db: AgentDB) -> dict[str, str]:
run_with="agent",
ai_fallback=False,
cache_key="seed-cache-key",
+ adaptive_caching=True,
+ code_version=1,
run_sequentially=True,
sequential_key="seed-sequential-key",
)
@@ -205,7 +210,7 @@ async def test_update_workflow_dispatch_state_if_latest_rejects_stale_version(ag
async def test_omitting_all_workflow_level_fields_preserves_seed(
agent_db: AgentDB, seeded_workflow: dict[str, str]
) -> None:
- """Omitting every kwarg except title must leave all 11 fields intact."""
+ """Omitting every kwarg except title must leave all threaded fields intact."""
await agent_db.workflows.update_workflow_and_reconcile_definition_params(
workflow_id=seeded_workflow["workflow_id"],
organization_id=seeded_workflow["organization_id"],
@@ -217,6 +222,8 @@ async def test_omitting_all_workflow_level_fields_preserves_seed(
# Every threaded field survives the omit-kwargs call:
assert workflow.proxy_location == ProxyLocation.RESIDENTIAL
assert workflow.webhook_callback_url == "https://example.com/webhook"
+ assert workflow.totp_verification_url == "https://example.com/totp"
+ assert workflow.totp_identifier == "seed-totp-identifier"
assert workflow.max_screenshot_scrolls == 7
assert workflow.extra_http_headers == {"X-Seed": "yes"}
assert workflow.persist_browser_session is True
@@ -225,6 +232,8 @@ async def test_omitting_all_workflow_level_fields_preserves_seed(
assert workflow.run_with == "agent"
assert workflow.ai_fallback is False
assert workflow.cache_key == "seed-cache-key"
+ assert workflow.adaptive_caching is True
+ assert workflow.code_version == 1
assert workflow.run_sequentially is True
assert workflow.sequential_key == "seed-sequential-key"
@@ -268,21 +277,27 @@ async def test_passing_none_to_unset_guarded_fields_clears_them(
organization_id=seeded_workflow["organization_id"],
proxy_location=None,
webhook_callback_url=None,
+ totp_verification_url=None,
+ totp_identifier=None,
model=None,
max_screenshot_scrolling_times=None,
extra_http_headers=None,
sequential_key=None,
browser_profile_id=None,
+ code_version=None,
)
workflow = await _get(agent_db, seeded_workflow)
assert workflow.proxy_location is None
assert workflow.webhook_callback_url is None
+ assert workflow.totp_verification_url is None
+ assert workflow.totp_identifier is None
assert workflow.model is None
assert workflow.max_screenshot_scrolls is None
assert workflow.extra_http_headers is None
assert workflow.sequential_key is None
assert workflow.browser_profile_id is None
+ assert workflow.code_version is None
async def test_setting_new_values_persists_across_all_fields(
@@ -294,6 +309,8 @@ async def test_setting_new_values_persists_across_all_fields(
organization_id=seeded_workflow["organization_id"],
proxy_location=ProxyLocation.US_CA,
webhook_callback_url="https://example.com/webhook/new",
+ totp_verification_url="https://example.com/totp/new",
+ totp_identifier="new-totp-identifier",
max_screenshot_scrolling_times=12,
extra_http_headers={"X-Updated": "yes"},
persist_browser_session=False,
@@ -302,6 +319,8 @@ async def test_setting_new_values_persists_across_all_fields(
run_with="code",
ai_fallback=True,
cache_key="new-cache-key",
+ adaptive_caching=False,
+ code_version=2,
run_sequentially=False,
sequential_key="new-sequential-key",
)
@@ -309,6 +328,8 @@ async def test_setting_new_values_persists_across_all_fields(
assert workflow.proxy_location == ProxyLocation.US_CA
assert workflow.webhook_callback_url == "https://example.com/webhook/new"
+ assert workflow.totp_verification_url == "https://example.com/totp/new"
+ assert workflow.totp_identifier == "new-totp-identifier"
assert workflow.max_screenshot_scrolls == 12
assert workflow.extra_http_headers == {"X-Updated": "yes"}
assert workflow.persist_browser_session is False
@@ -317,5 +338,7 @@ async def test_setting_new_values_persists_across_all_fields(
assert workflow.run_with == "code"
assert workflow.ai_fallback is True
assert workflow.cache_key == "new-cache-key"
+ assert workflow.adaptive_caching is False
+ assert workflow.code_version == 2
assert workflow.run_sequentially is False
assert workflow.sequential_key == "new-sequential-key"
diff --git a/tests/unit/test_copilot_agent_blocker_renderer.py b/tests/unit/test_copilot_agent_blocker_renderer.py
index 3846e335d..7340127b5 100644
--- a/tests/unit/test_copilot_agent_blocker_renderer.py
+++ b/tests/unit/test_copilot_agent_blocker_renderer.py
@@ -132,6 +132,18 @@ def test_shim_no_op_when_no_signal() -> None:
assert overridden is result
+def test_shim_persists_narrative_payload_for_blocker_terminal() -> None:
+ ctx = _ctx()
+ ctx.turn_id = "turn-1"
+ ctx.blocker_signal = _signal()
+ result = _agent_result()
+ overridden = _finalize_result_with_blocker_override(ctx, result)
+ assert overridden.narrative_payload is not None
+ assert overridden.narrative_payload["turnId"] == "turn-1"
+ assert overridden.narrative_payload["terminal"] == "response"
+ assert overridden.narrative_payload["terminalMessage"] == overridden.user_response
+
+
def test_shim_overrides_proposal_even_when_pre_override_result_carries_workflow() -> None:
ctx = _ctx()
ctx.blocker_signal = _signal()
diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.py
index 8f37239c1..15f52dfb6 100644
--- a/tests/unit/test_copilot_agent_helpers.py
+++ b/tests/unit/test_copilot_agent_helpers.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+import asyncio
import json
from datetime import datetime, timezone
from types import SimpleNamespace
@@ -562,6 +563,7 @@ class TestShouldRestorePersistedWorkflow:
def _result(self, *, persisted: bool, updated_workflow: object | None):
r = MagicMock()
r.workflow_was_persisted = persisted
+ r.canonical_was_persisted_due_to_param_change = False
r.updated_workflow = updated_workflow
r.proposal_disposition = "auto_applicable"
r.cancelled = False
@@ -763,8 +765,10 @@ class TestTranslateToAgentResultGating:
ctx = _ctx()
result = SimpleNamespace(final_output="ASK_QUESTION\nWhich account should I use?", new_items=[])
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.response_type == "ASK_QUESTION"
@@ -790,8 +794,10 @@ class TestTranslateToAgentResultGating:
"workflow_yaml": "new: yaml",
}
)
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert ctx.last_test_ok is None
@@ -835,8 +841,10 @@ workflow_definition:
result = _fake_run_result(
{"type": "REPLACE_WORKFLOW", "user_response": "Here you go.", "workflow_yaml": submitted_yaml}
)
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
process_mock.assert_not_called()
@@ -861,8 +869,10 @@ workflow_definition:
result = _fake_run_result(
{"type": "REPLACE_WORKFLOW", "user_response": "here", "workflow_yaml": "::: not yaml"}
)
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert ctx.last_workflow is tested_wf
@@ -884,8 +894,10 @@ workflow_definition:
)
specific_question = "I need credentials for site.example — can you link one in Settings?"
result = _fake_run_result({"type": "ASK_QUESTION", "user_response": specific_question})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.user_response == specific_question
@@ -911,8 +923,10 @@ workflow_definition:
)
result = _fake_run_result({"type": "ASK_QUESTION", "user_response": verbose_response})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.response_type == "ASK_QUESTION"
@@ -1036,8 +1050,10 @@ workflow_definition:
last_failure_category_top="NAVIGATION_FAILURE",
)
result = _fake_run_result({"type": "REPLY", "user_response": "All done — your workflow is ready."})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert "test failed" in agent_result.user_response.lower()
@@ -1055,8 +1071,10 @@ workflow_definition:
last_test_failure_reason="A verification challenge is preventing submission.",
)
result = _fake_run_result({"type": "REPLY", "user_response": "Done."})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.updated_workflow is wf
@@ -1075,8 +1093,10 @@ workflow_definition:
last_test_suspicious_success=True,
)
result = _fake_run_result({"type": "REPLY", "user_response": "Done."})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.updated_workflow is wf
@@ -1095,8 +1115,10 @@ workflow_definition:
last_test_ok=None,
)
result = _fake_run_result({"type": "REPLY", "user_response": "Please provide credentials before I continue."})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.updated_workflow is wf
@@ -1118,8 +1140,10 @@ workflow_definition:
)
response = "I have a draft proposal. Use Review to inspect it, Accept to save it, or Reject it."
result = _fake_run_result({"type": "REPLY", "user_response": response})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert response in agent_result.user_response
@@ -1143,8 +1167,10 @@ workflow_definition:
"goal_reached": False,
}
)
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.updated_workflow is wf
@@ -1162,8 +1188,10 @@ workflow_definition:
last_update_block_count=3,
)
result = _fake_run_result({"type": "REPLY", "user_response": "All set."})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.updated_workflow is wf
@@ -1178,8 +1206,10 @@ workflow_definition:
last_update_block_count=3,
)
result = _fake_run_result({"type": "REPLY", "user_response": "All set.", "goal_reached": True})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.updated_workflow is wf
@@ -1198,8 +1228,10 @@ workflow_definition:
result = _fake_run_result(
{"type": "REPLY", "user_response": "Cookie modal blocked the form.", "goal_reached": "false"}
)
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.updated_workflow is wf
@@ -1213,8 +1245,10 @@ workflow_definition:
result = _fake_run_result(
{"type": "REPLY", "user_response": "I couldn't find the form.", "goal_reached": False}
)
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.updated_workflow is None
@@ -1224,8 +1258,10 @@ workflow_definition:
def test_unbacked_workflow_claim_is_rewritten_without_proposal(self) -> None:
ctx = _ctx(last_test_ok=None)
result = _fake_run_result({"type": "REPLY", "user_response": "Here's the workflow."})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert "here's the workflow" not in agent_result.user_response.lower()
@@ -1250,8 +1286,10 @@ workflow_definition:
),
)
result = _fake_run_result({"type": "REPLY", "user_response": "Here's the workflow."})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert "here's the workflow" not in agent_result.user_response.lower()
@@ -1269,8 +1307,10 @@ workflow_definition:
),
)
result = _fake_run_result({"type": "REPLY", "user_response": "I've drafted a workflow for you."})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert "Required context was unavailable: the workflow run ID and the block run results." in (
@@ -1300,8 +1340,10 @@ workflow_definition:
),
)
result = _fake_run_result({"type": "REPLY", "user_response": "I've drafted a workflow for you."})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert "Required context was unavailable: the current browser tab or page state." in agent_result.user_response
@@ -1314,8 +1356,10 @@ workflow_definition:
"user_response": "In the meantime, I've drafted the initial part of your workflow with placeholders.",
}
)
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert "initial part of your workflow" not in agent_result.user_response.lower()
@@ -1328,8 +1372,10 @@ workflow_definition:
wf = SimpleNamespace(name="drafted")
ctx = _ctx(last_workflow=wf, last_workflow_yaml="title: drafted", last_test_ok=True)
result = _fake_run_result({"type": "REPLY", "user_response": "Here's the workflow."})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.user_response == "Here's the workflow."
@@ -1348,8 +1394,10 @@ workflow_definition:
last_test_failure_reason="A verification challenge is preventing submission.",
)
result = _fake_run_result({"type": "REPLY", "user_response": "Tried but blocked.", "goal_reached": False})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.updated_workflow is wf
@@ -1370,8 +1418,10 @@ workflow_definition:
result = _fake_run_result(
{"type": "REPLACE_WORKFLOW", "user_response": "Here you go.", "workflow_yaml": "raw: yaml"}
)
- agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert captured["yaml"] == "raw: yaml"
@@ -1393,8 +1443,10 @@ workflow_definition:
result = _fake_run_result(
{"type": "REPLACE_WORKFLOW", "user_response": "Here you go.", "workflow_yaml": "raw: yaml"}
)
- agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert captured["yaml"] == "raw: yaml"
@@ -1405,8 +1457,10 @@ workflow_definition:
verified_wf = SimpleNamespace(name="verified-partial")
ctx = _ctx(last_workflow=verified_wf, last_workflow_yaml="verified: yaml", last_test_ok=True)
result = _fake_run_result({"type": "ASK_QUESTION", "user_response": "Need credentials before I can continue."})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.updated_workflow is None
@@ -1419,8 +1473,10 @@ workflow_definition:
# prior persisted proposal so reload stays coherent.
ctx = _ctx()
result = _fake_run_result({"type": "ASK_QUESTION", "user_response": "Which site?"})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.updated_workflow is None
@@ -1432,8 +1488,10 @@ workflow_definition:
verified_wf = SimpleNamespace(name="final")
ctx = _ctx(last_workflow=verified_wf, last_workflow_yaml="final: yaml", last_test_ok=True)
result = _fake_run_result({"type": "REPLY", "user_response": "Here you go."})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.updated_workflow is verified_wf
@@ -2508,8 +2566,10 @@ workflow_definition:
)
result = _fake_run_result({"type": "REPLY", "user_response": "Done."})
- agent_result = agent_module._translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_chat_request(), organization_id="org-1"
+ )
)
assert agent_result.updated_workflow is wf
diff --git a/tests/unit/test_copilot_mid_correction_guard.py b/tests/unit/test_copilot_mid_correction_guard.py
index 251e58601..c277682ff 100644
--- a/tests/unit/test_copilot_mid_correction_guard.py
+++ b/tests/unit/test_copilot_mid_correction_guard.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+import asyncio
import time
from types import SimpleNamespace
from typing import Any
@@ -190,8 +191,10 @@ def test_translate_to_agent_result_salvages_last_good_on_failed_reply() -> None:
ctx.last_good_workflow_yaml = "yaml-good"
agent_text = "Built 4 verified blocks; block 5 failed."
result = _fake_run_result({"type": "REPLY", "user_response": agent_text, "goal_reached": False})
- agent_result = _translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_fake_chat_request(), organization_id="o"
+ agent_result = asyncio.run(
+ _translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_fake_chat_request(), organization_id="o"
+ )
)
assert agent_result.updated_workflow is ctx.last_good_workflow
assert agent_result.workflow_yaml == "yaml-good"
@@ -218,8 +221,10 @@ def test_translate_to_agent_result_salvages_after_failure_then_update_workflow()
ctx.last_good_workflow_yaml = "yaml-good"
agent_text = "Built 4 blocks; block 5 failed; ran out of time."
result = _fake_run_result({"type": "REPLY", "user_response": agent_text, "goal_reached": False})
- agent_result = _translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_fake_chat_request(), organization_id="o"
+ agent_result = asyncio.run(
+ _translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_fake_chat_request(), organization_id="o"
+ )
)
assert agent_result.updated_workflow is ctx.last_good_workflow
assert agent_result.proposal_disposition == "review_untested"
@@ -241,8 +246,10 @@ def test_translate_to_agent_result_does_not_salvage_on_standalone_edit() -> None
ctx.last_good_workflow = SimpleNamespace(workflow_id="wf-good")
ctx.last_good_workflow_yaml = "yaml-good"
result = _fake_run_result({"type": "REPLY", "user_response": "Drafted.", "goal_reached": False})
- agent_result = _translate_to_agent_result(
- result, ctx, global_llm_context=None, chat_request=_fake_chat_request(), organization_id="o"
+ agent_result = asyncio.run(
+ _translate_to_agent_result(
+ result, ctx, global_llm_context=None, chat_request=_fake_chat_request(), organization_id="o"
+ )
)
assert agent_result.updated_workflow is ctx.last_workflow
assert agent_result.workflow_yaml == "yaml-edited"
diff --git a/tests/unit/test_copilot_narration.py b/tests/unit/test_copilot_narration.py
index 36ec60674..e7b368f57 100644
--- a/tests/unit/test_copilot_narration.py
+++ b/tests/unit/test_copilot_narration.py
@@ -864,12 +864,10 @@ async def test_poll_tick_block_ts_change_triggers_fetch_and_records_transition()
# Repository returns DESC; helper reverses internally.
return [_StubBlock("b1", "completed"), _StubBlock("b0", "running")]
- new_block_ts, new_step_ts, new_last_fetch = await narrator_poll_tick(
+ new_block_ts, new_last_fetch = await narrator_poll_tick(
state,
current_block_ts=_ts(2.0),
- current_step_ts=_ts(1.0),
prior_block_ts=_ts(1.0),
- prior_step_ts=_ts(1.0),
last_block_fetch_monotonic=0.0,
seen_block_states=seen,
fetch_block_statuses=fetch,
@@ -887,7 +885,6 @@ async def test_poll_tick_block_ts_change_triggers_fetch_and_records_transition()
# the gate was open (no prior emission, no in-flight task).
assert state.last_attempted_at is not None
assert new_block_ts == _ts(2.0)
- assert new_step_ts == _ts(1.0)
assert new_last_fetch > 0.0
# Drain the spawned narrator task so it doesn't outlive the test.
if state.in_flight_task is not None:
@@ -899,10 +896,7 @@ async def test_poll_tick_block_ts_change_triggers_fetch_and_records_transition()
@pytest.mark.asyncio
-async def test_poll_tick_step_ts_change_alone_records_tool_in_progress() -> None:
- # Pre-populate last_attempted_at so the gate is closed and the
- # TOOL_IN_PROGRESS transition stays in pending_transition for inspection
- # rather than being drained by schedule_narration.
+async def test_poll_tick_block_unchanged_does_not_fetch_or_record() -> None:
state = NarratorState(last_attempted_at=time.monotonic())
stream = _StubStream()
seen: dict[str, str] = {}
@@ -917,17 +911,15 @@ async def test_poll_tick_step_ts_change_alone_records_tool_in_progress() -> None
await narrator_poll_tick(
state,
current_block_ts=same_block_ts,
- current_step_ts=_ts(2.0),
prior_block_ts=same_block_ts,
- prior_step_ts=_ts(1.0),
last_block_fetch_monotonic=0.0,
seen_block_states=seen,
fetch_block_statuses=fetch,
stream=stream, # type: ignore[arg-type]
)
- assert fetch_calls == 0 # block_ts didn't change
- assert state.pending_transition == TransitionKind.TOOL_IN_PROGRESS
+ assert fetch_calls == 0
+ assert state.pending_transition is None
@pytest.mark.asyncio
@@ -944,9 +936,7 @@ async def test_poll_tick_no_change_still_calls_schedule_narration() -> None:
await narrator_poll_tick(
state,
current_block_ts=same_ts,
- current_step_ts=same_ts,
prior_block_ts=same_ts,
- prior_step_ts=same_ts,
last_block_fetch_monotonic=0.0,
seen_block_states={},
fetch_block_statuses=fetch,
@@ -980,12 +970,10 @@ async def test_poll_tick_rate_limited_block_change_retries_on_next_tick() -> Non
return [_StubBlock("b1", "completed")]
fresh_now = time.monotonic()
- next_block_ts, _, next_last_fetch = await narrator_poll_tick(
+ next_block_ts, next_last_fetch = await narrator_poll_tick(
state,
current_block_ts=_ts(2.0),
- current_step_ts=_ts(1.0),
prior_block_ts=_ts(1.0),
- prior_step_ts=_ts(1.0),
# Last fetch a moment ago -- gate closed.
last_block_fetch_monotonic=fresh_now,
seen_block_states=seen,
@@ -1012,12 +1000,10 @@ async def test_poll_tick_fetch_exception_retries_on_next_tick() -> None:
fetch_calls += 1
raise RuntimeError("db transient failure")
- next_block_ts, _, next_last_fetch = await narrator_poll_tick(
+ next_block_ts, next_last_fetch = await narrator_poll_tick(
state,
current_block_ts=_ts(2.0),
- current_step_ts=_ts(1.0),
prior_block_ts=_ts(1.0),
- prior_step_ts=_ts(1.0),
last_block_fetch_monotonic=0.0,
seen_block_states=seen,
fetch_block_statuses=fetch,
@@ -1043,12 +1029,10 @@ async def test_poll_tick_first_tick_with_zero_initial_fetch_marker_does_not_rais
async def fetch() -> list[_StubBlock]:
return [_StubBlock("b1", "running")]
- new_block_ts, new_step_ts, new_last_fetch = await narrator_poll_tick(
+ new_block_ts, new_last_fetch = await narrator_poll_tick(
state,
current_block_ts=_ts(2.0),
- current_step_ts=_ts(1.0),
prior_block_ts=_ts(1.0),
- prior_step_ts=_ts(1.0),
last_block_fetch_monotonic=0.0,
seen_block_states={},
fetch_block_statuses=fetch,
@@ -1071,9 +1055,7 @@ async def test_poll_tick_uses_state_current_iteration_for_synthetic_entries() ->
await narrator_poll_tick(
state,
current_block_ts=_ts(2.0),
- current_step_ts=_ts(1.0),
prior_block_ts=_ts(1.0),
- prior_step_ts=_ts(1.0),
last_block_fetch_monotonic=0.0,
seen_block_states=seen,
fetch_block_statuses=fetch,
@@ -1116,9 +1098,7 @@ async def test_poll_tick_emits_block_progress_for_each_new_transition() -> None:
await narrator_poll_tick(
state,
current_block_ts=_ts(2.0),
- current_step_ts=_ts(1.0),
prior_block_ts=_ts(1.0),
- prior_step_ts=_ts(1.0),
last_block_fetch_monotonic=0.0,
seen_block_states=seen,
fetch_block_statuses=fetch,
@@ -1160,9 +1140,7 @@ async def test_poll_tick_does_not_emit_block_progress_for_unchanged_blocks() ->
await narrator_poll_tick(
state,
current_block_ts=_ts(2.0),
- current_step_ts=_ts(1.0),
prior_block_ts=_ts(1.0),
- prior_step_ts=_ts(1.0),
last_block_fetch_monotonic=0.0,
seen_block_states=seen,
fetch_block_statuses=fetch,
@@ -1187,9 +1165,7 @@ async def test_poll_tick_skips_block_progress_when_label_blank() -> None:
await narrator_poll_tick(
state,
current_block_ts=_ts(2.0),
- current_step_ts=_ts(1.0),
prior_block_ts=_ts(1.0),
- prior_step_ts=_ts(1.0),
last_block_fetch_monotonic=0.0,
seen_block_states=seen,
fetch_block_statuses=fetch,
@@ -1206,3 +1182,98 @@ async def test_poll_tick_skips_block_progress_when_label_blank() -> None:
await state.in_flight_task
except (asyncio.CancelledError, Exception):
pass
+
+
+async def test_poll_tick_block_ended_at_overwritten_by_latest_terminal_and_cleared_on_retry() -> None:
+ """A retry-then-succeed sequence captures the final terminal timestamp;
+ the running event in between clears the prior terminal so a stale value
+ can't survive into the hydrated payload."""
+ state = NarratorState()
+ stream = _StubStream()
+ seen: dict[str, str] = {}
+ started_at_map: dict[str, str] = {}
+ ended_at_map: dict[str, str] = {}
+
+ # Tick 1: block goes running.
+ async def fetch_running() -> list[_StubBlock]:
+ return [_StubBlock("b0", "running", label="step_navigate")]
+
+ await narrator_poll_tick(
+ state,
+ current_block_ts=_ts(1.0),
+ prior_block_ts=_ts(0.0),
+ last_block_fetch_monotonic=0.0,
+ seen_block_states=seen,
+ fetch_block_statuses=fetch_running,
+ stream=stream, # type: ignore[arg-type]
+ block_started_at_map=started_at_map,
+ block_ended_at_map=ended_at_map,
+ )
+ assert "step_navigate" in started_at_map
+ assert "step_navigate" not in ended_at_map
+ first_started = started_at_map["step_navigate"]
+
+ # Tick 2: block fails.
+ async def fetch_failed() -> list[_StubBlock]:
+ return [_StubBlock("b0", "failed", label="step_navigate")]
+
+ await narrator_poll_tick(
+ state,
+ current_block_ts=_ts(2.0),
+ prior_block_ts=_ts(1.0),
+ last_block_fetch_monotonic=0.0,
+ seen_block_states=seen,
+ fetch_block_statuses=fetch_failed,
+ stream=stream, # type: ignore[arg-type]
+ block_started_at_map=started_at_map,
+ block_ended_at_map=ended_at_map,
+ )
+ first_terminal = ended_at_map["step_navigate"]
+ assert first_terminal is not None
+
+ # Tick 3: block goes running again (retry). endedAt must clear so it
+ # can't leak into a hydrated narrative payload as "DONE · old time".
+ async def fetch_running_again() -> list[_StubBlock]:
+ return [_StubBlock("b0", "running", label="step_navigate")]
+
+ await narrator_poll_tick(
+ state,
+ current_block_ts=_ts(3.0),
+ prior_block_ts=_ts(2.0),
+ last_block_fetch_monotonic=0.0,
+ seen_block_states=seen,
+ fetch_block_statuses=fetch_running_again,
+ stream=stream, # type: ignore[arg-type]
+ block_started_at_map=started_at_map,
+ block_ended_at_map=ended_at_map,
+ )
+ assert "step_navigate" not in ended_at_map
+ # startedAt is first-seen, not overwritten on retry.
+ assert started_at_map["step_navigate"] == first_started
+
+ # Tick 4: block completes. endedAt is the latest terminal, not the
+ # first-failure timestamp.
+ async def fetch_completed() -> list[_StubBlock]:
+ return [_StubBlock("b0", "completed", label="step_navigate")]
+
+ await narrator_poll_tick(
+ state,
+ current_block_ts=_ts(4.0),
+ prior_block_ts=_ts(3.0),
+ last_block_fetch_monotonic=0.0,
+ seen_block_states=seen,
+ fetch_block_statuses=fetch_completed,
+ stream=stream, # type: ignore[arg-type]
+ block_started_at_map=started_at_map,
+ block_ended_at_map=ended_at_map,
+ )
+ assert ended_at_map["step_navigate"] != first_terminal
+
+ # Drain any in-flight narrator task spawned along the way.
+ if state.in_flight_task is not None:
+ state.in_flight_task.cancel()
+ try:
+ await state.in_flight_task
+ except (asyncio.CancelledError, Exception):
+ pass
diff --git a/tests/unit/test_copilot_narrative_activity.py b/tests/unit/test_copilot_narrative_activity.py
new file mode 100644
index 000000000..76c1c9f39
--- /dev/null
+++ b/tests/unit/test_copilot_narrative_activity.py
@@ -0,0 +1,161 @@
+from __future__ import annotations
+
+from datetime import datetime, timezone
+from types import SimpleNamespace
+
+from skyvern.forge.sdk.copilot.agent import _build_narrative_payload
+from skyvern.forge.sdk.copilot.context import CopilotContext
+from skyvern.forge.sdk.copilot.narration import (
+ MAX_BLOCK_ACTIVITY_ENTRIES,
+ MAX_DESIGN_ACTIVITY_ENTRIES,
+ NarratorState,
+ build_narration_activity,
+ build_tool_call_activity,
+ build_tool_result_activity,
+)
+
+
+def _ctx() -> CopilotContext:
+ return CopilotContext(
+ organization_id="org",
+ workflow_id="wf",
+ workflow_permanent_id="wfp",
+ workflow_yaml="",
+ browser_session_id=None,
+ stream=SimpleNamespace(), # type: ignore[arg-type]
+ )
+
+
+def _staged(*labels: str) -> SimpleNamespace:
+ return SimpleNamespace(
+ workflow_definition=SimpleNamespace(
+ blocks=[SimpleNamespace(label=label, block_type="task") for label in labels]
+ )
+ )
+
+
+def test_tool_call_activity_shape_and_denylist() -> None:
+ entry = build_tool_call_activity("update_workflow", 3, "abc")
+ assert entry == {
+ "kind": "tool_call",
+ "text": "Calling update_workflow…",
+ "iteration": 3,
+ "toolName": "update_workflow",
+ "id": "tc-abc",
+ }
+ assert "success" not in entry
+ assert build_tool_call_activity("list_credentials", 0, "x") is None
+
+
+def test_tool_result_activity_shape_falls_back_to_tool_name_and_denylist() -> None:
+ entry = build_tool_result_activity("update_workflow", "Updated 2 blocks", True, 4, "abc")
+ assert entry == {
+ "kind": "tool_result",
+ "text": "Updated 2 blocks",
+ "iteration": 4,
+ "toolName": "update_workflow",
+ "success": True,
+ "id": "tr-abc",
+ }
+ assert build_tool_result_activity("update_workflow", "", False, 4, "abc")["text"] == "update_workflow"
+ assert build_tool_result_activity("get_browser_screenshot", "s", True, 0, "x") is None
+ assert build_tool_result_activity("get_run_results", "s", True, 0, "x") is None
+
+
+def test_narration_activity_shape() -> None:
+ entry = build_narration_activity("Doing the thing", 5, datetime(2026, 1, 1, tzinfo=timezone.utc))
+ assert entry == {
+ "kind": "narration",
+ "text": "Doing the thing",
+ "iteration": 5,
+ "id": "n-5-2026-01-01T00:00:00+00:00",
+ }
+ assert "toolName" not in entry
+
+
+def test_record_activity_routes_to_design_when_no_block_running() -> None:
+ state = NarratorState()
+ state.record_activity(build_tool_call_activity("update_workflow", 0, "c1"))
+ assert [e["id"] for e in state.design_activity] == ["tc-c1"]
+ assert state.block_activity == {}
+
+
+def test_record_activity_routes_to_running_block() -> None:
+ state = NarratorState()
+ state.running_block_label = "step_1"
+ state.record_activity(build_tool_result_activity("run_blocks_and_collect_debug", "ran", True, 1, "c2"))
+ assert [e["id"] for e in state.block_activity["step_1"]] == ["tr-c2"]
+ assert state.design_activity == []
+
+
+def test_record_activity_drops_denylisted_entries() -> None:
+ state = NarratorState()
+ state.running_block_label = "step_1"
+ state.record_activity(build_tool_call_activity("list_credentials", 0, "c1"))
+ state.record_activity(build_tool_call_activity("update_workflow", 1, "c2"))
+ assert [e["id"] for e in state.block_activity["step_1"]] == ["tc-c2"]
+
+
+def test_record_activity_caps_keep_most_recent() -> None:
+ state = NarratorState()
+ state.running_block_label = "b"
+ for i in range(MAX_BLOCK_ACTIVITY_ENTRIES + 10):
+ state.record_activity(build_tool_call_activity("t", i, f"c{i}"))
+ bucket = state.block_activity["b"]
+ assert len(bucket) == MAX_BLOCK_ACTIVITY_ENTRIES
+ assert bucket[0]["iteration"] == 10
+ assert bucket[-1]["iteration"] == MAX_BLOCK_ACTIVITY_ENTRIES + 9
+
+ design_state = NarratorState()
+ for i in range(MAX_DESIGN_ACTIVITY_ENTRIES + 5):
+ design_state.record_activity(build_narration_activity(f"n{i}", i, datetime(2026, 1, 1, tzinfo=timezone.utc)))
+ assert len(design_state.design_activity) == MAX_DESIGN_ACTIVITY_ENTRIES
+ assert design_state.design_activity[0]["text"] == "n5"
+
+
+def test_build_narrative_payload_serializes_block_and_design_activity() -> None:
+ ctx = _ctx()
+ ctx.staged_workflow = _staged("step_1", "step_2") # type: ignore[assignment]
+ ctx.has_staged_proposal = True
+ ctx.block_state_map = {"step_1": "completed", "step_2": "running"}
+ ctx.turn_id = "turn-1"
+ ctx.turn_index = 2
+
+ state = NarratorState()
+ state.design_activity = [
+ build_narration_activity("Planning the build", 0, datetime(2026, 1, 1, tzinfo=timezone.utc))
+ ]
+ state.block_activity = {
+ "step_1": [build_tool_result_activity("run_blocks_and_collect_debug", "ran step_1", True, 1, "c1")]
+ }
+ ctx.narrator_state = state
+
+ payload = _build_narrative_payload(ctx, terminal="response", terminal_message="done", narrative_summary="summary")
+
+ assert payload["designActivity"] == [
+ {"kind": "narration", "text": "Planning the build", "iteration": 0, "id": "n-0-2026-01-01T00:00:00+00:00"}
+ ]
+ blocks_by_label = {b["label"]: b for b in payload["blocks"]}
+ assert blocks_by_label["step_1"]["activity"] == [
+ {
+ "kind": "tool_result",
+ "text": "ran step_1",
+ "iteration": 1,
+ "toolName": "run_blocks_and_collect_debug",
+ "success": True,
+ "id": "tr-c1",
+ }
+ ]
+ assert blocks_by_label["step_2"]["activity"] == []
+
+
+def test_build_narrative_payload_empty_when_no_narrator_state() -> None:
+ ctx = _ctx()
+ ctx.staged_workflow = _staged("step_1") # type: ignore[assignment]
+ ctx.has_staged_proposal = True
+ ctx.narrator_state = None
+
+ payload = _build_narrative_payload(ctx, terminal="response", terminal_message="done", narrative_summary=None)
+
+ assert payload["designActivity"] == []
+ assert payload["blocks"][0]["activity"] == []
diff --git a/tests/unit/test_copilot_output_policy.py b/tests/unit/test_copilot_output_policy.py
index 8c5a97f28..a22006c5a 100644
--- a/tests/unit/test_copilot_output_policy.py
+++ b/tests/unit/test_copilot_output_policy.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import asyncio
import json
from types import SimpleNamespace
from unittest.mock import MagicMock, patch
@@ -1074,18 +1075,20 @@ workflow_definition:
- azure_credentials
"""
- result = agent_module._translate_to_agent_result(
- _fake_run_result(
- {
- "type": "ASK_QUESTION",
- "user_response": "I found cred_unrelated in an internal tool observation. Should I use it?",
- "global_llm_context": "{}",
- }
- ),
- ctx,
- "{}",
- _chat_request(),
- "org-1",
+ result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ _fake_run_result(
+ {
+ "type": "ASK_QUESTION",
+ "user_response": "I found cred_unrelated in an internal tool observation. Should I use it?",
+ "global_llm_context": "{}",
+ }
+ ),
+ ctx,
+ "{}",
+ _chat_request(),
+ "org-1",
+ )
)
assert result.response_type == "REPLY"
@@ -1181,12 +1184,14 @@ workflow_definition:
}
)
- agent_result = agent_module._translate_to_agent_result(
- result,
- _ctx(),
- global_llm_context=None,
- chat_request=_chat_request(),
- organization_id="org-1",
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result,
+ _ctx(),
+ global_llm_context=None,
+ chat_request=_chat_request(),
+ organization_id="org-1",
+ )
)
assert agent_result.response_type == "ASK_QUESTION"
@@ -1199,12 +1204,14 @@ workflow_definition:
def test_translate_to_agent_result_blocks_raw_secret_final_text() -> None:
result = _fake_run_result({"type": "REPLY", "user_response": "I used password: hunter2."})
- agent_result = agent_module._translate_to_agent_result(
- result,
- _ctx(),
- global_llm_context=None,
- chat_request=_chat_request(),
- organization_id="org-1",
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result,
+ _ctx(),
+ global_llm_context=None,
+ chat_request=_chat_request(),
+ organization_id="org-1",
+ )
)
assert agent_result.response_type == "ASK_QUESTION"
@@ -1218,12 +1225,14 @@ def test_translate_to_agent_result_blocks_raw_secret_final_text() -> None:
def test_translate_to_agent_result_rewrites_unbacked_workflow_claim() -> None:
result = _fake_run_result({"type": "REPLY", "user_response": "I've drafted a workflow for you."})
- agent_result = agent_module._translate_to_agent_result(
- result,
- _ctx(),
- global_llm_context=None,
- chat_request=_chat_request(),
- organization_id="org-1",
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result,
+ _ctx(),
+ global_llm_context=None,
+ chat_request=_chat_request(),
+ organization_id="org-1",
+ )
)
assert "wasn't able to produce a workflow proposal" in agent_result.user_response
@@ -1254,12 +1263,14 @@ def test_translate_to_agent_result_rewrites_deprecated_block_taxonomy() -> None:
)
with patch("skyvern.forge.sdk.copilot.agent.LOG.info") as log_info:
- agent_result = agent_module._translate_to_agent_result(
- result,
- _ctx(),
- global_llm_context=None,
- chat_request=_chat_request(),
- organization_id="org-1",
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result,
+ _ctx(),
+ global_llm_context=None,
+ chat_request=_chat_request(),
+ organization_id="org-1",
+ )
)
assert "task_v2" not in agent_result.user_response
@@ -1296,12 +1307,14 @@ def test_translate_to_agent_result_prioritizes_unbacked_workflow_claim_over_taxo
}
)
- agent_result = agent_module._translate_to_agent_result(
- result,
- _ctx(),
- global_llm_context=None,
- chat_request=_chat_request(),
- organization_id="org-1",
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result,
+ _ctx(),
+ global_llm_context=None,
+ chat_request=_chat_request(),
+ organization_id="org-1",
+ )
)
assert "wasn't able to produce a workflow proposal" in agent_result.user_response
@@ -1326,12 +1339,14 @@ def test_translate_to_agent_result_rewrites_block_yaml_pasted_in_reply() -> None
result = _fake_run_result({"type": "REPLY", "user_response": leak})
with patch("skyvern.forge.sdk.copilot.agent.LOG.info") as log_info:
- agent_result = agent_module._translate_to_agent_result(
- result,
- _ctx(),
- global_llm_context=None,
- chat_request=_chat_request(),
- organization_id="org-1",
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result,
+ _ctx(),
+ global_llm_context=None,
+ chat_request=_chat_request(),
+ organization_id="org-1",
+ )
)
assert "block_type" not in agent_result.user_response
@@ -1364,12 +1379,14 @@ def test_translate_to_agent_result_rewrites_block_yaml_when_workflow_attached()
ctx.last_workflow_yaml = _workflow_yaml()
ctx.last_test_ok = True
- agent_result = agent_module._translate_to_agent_result(
- result,
- ctx,
- global_llm_context=None,
- chat_request=_chat_request(),
- organization_id="org-1",
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result,
+ ctx,
+ global_llm_context=None,
+ chat_request=_chat_request(),
+ organization_id="org-1",
+ )
)
assert "block_type" not in agent_result.user_response
@@ -1384,12 +1401,14 @@ def test_translate_to_agent_result_adds_unvalidated_affordance() -> None:
workflow = SimpleNamespace(name="draft")
result = _fake_run_result({"type": "REPLY", "user_response": "I drafted the workflow."})
- agent_result = agent_module._translate_to_agent_result(
- result,
- _ctx(last_workflow=workflow, last_workflow_yaml="title: draft", last_test_ok=None),
- global_llm_context=None,
- chat_request=_chat_request(),
- organization_id="org-1",
+ agent_result = asyncio.run(
+ agent_module._translate_to_agent_result(
+ result,
+ _ctx(last_workflow=workflow, last_workflow_yaml="title: draft", last_test_ok=None),
+ global_llm_context=None,
+ chat_request=_chat_request(),
+ organization_id="org-1",
+ )
)
assert agent_result.updated_workflow is workflow
diff --git a/tests/unit/test_copilot_session_injection.py b/tests/unit/test_copilot_session_injection.py
index ed1ef0029..3373b848f 100644
--- a/tests/unit/test_copilot_session_injection.py
+++ b/tests/unit/test_copilot_session_injection.py
@@ -541,7 +541,14 @@ workflow_definition:
class TestWorkflowUpdatePersistence:
- def test_record_marks_persisted_even_when_workflow_has_zero_blocks(self) -> None:
+ def test_record_marks_last_workflow_but_not_legacy_persisted(self) -> None:
+ """SKY-10318: ``_record_workflow_update_result`` no longer flips the
+ legacy ``workflow_persisted`` flag (canonical write is now gated by
+ ``_update_workflow``'s degraded-path check, not by tool success). The
+ staging-aware signal is ``ctx.has_staged_proposal``, assigned inside
+ ``_update_workflow`` itself rather than this bookkeeping helper. The
+ helper still records ``last_workflow`` and ``last_workflow_yaml`` for
+ downstream callers (e.g. frontier diff, terminal AgentResult)."""
from skyvern.forge.sdk.copilot.tools import _record_workflow_update_result
ctx = MagicMock()
@@ -563,4 +570,4 @@ class TestWorkflowUpdatePersistence:
assert ctx.last_workflow is workflow
assert ctx.last_workflow_yaml == "title: Empty workflow"
- assert ctx.workflow_persisted is True
+ assert ctx.workflow_persisted is False
diff --git a/tests/unit/test_workflow_copilot_route_helpers.py b/tests/unit/test_workflow_copilot_route_helpers.py
index cc780527d..5dbf7d4a4 100644
--- a/tests/unit/test_workflow_copilot_route_helpers.py
+++ b/tests/unit/test_workflow_copilot_route_helpers.py
@@ -32,6 +32,7 @@ def _agent_result(
proposal_disposition: str = "auto_applicable",
cancelled: bool = False,
updated_workflow: Any = None,
+ canonical_was_persisted_due_to_param_change: bool = False,
**kwargs: Any,
) -> MagicMock:
"""MagicMock with override flags explicitly set so a forgotten attr can't pass via MagicMock truthiness."""
@@ -40,6 +41,10 @@ def _agent_result(
r.proposal_disposition = proposal_disposition
r.cancelled = cancelled
r.updated_workflow = updated_workflow
+ # SKY-10318: explicitly set the new staging flag so MagicMock truthiness
+ # doesn't accidentally trigger the degraded-path branch in
+ # `_should_restore_persisted_workflow`.
+ r.canonical_was_persisted_due_to_param_change = canonical_was_persisted_due_to_param_change
for k, v in kwargs.items():
setattr(r, k, v)
return r