Skip to content

Automate browser based workflows with AI

SKY-10581: Fix live Copilot terminal rollups (#6290)

Andrew Neilsonc7a7f736dba3
diff --git a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.ts b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.ts
index e2ea2f8ec..ce0e80426 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.ts
+++ b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.ts
@@ -344,6 +344,78 @@ describe("applyNarrativeEvent — terminal", () => {
expect(s.narrativeSummary).toBe("full text");
});
+ it("response uses backend error narrative payload when present", () => {
+ const s = applyNarrativeEvent(
+ EMPTY_NARRATIVE,
+ response({
+ message:
+ "Copilot hit an internal error before it could finish this turn.",
+ narrative_payload: {
+ turnId: "turn-1",
+ turnIndex: 0,
+ mode: "build",
+ designStarted: true,
+ designEnded: true,
+ draft: null,
+ blocks: [
+ {
+ workflowRunBlockId: "wrb_1",
+ label: "draft_workflow",
+ blockType: "task",
+ state: "running",
+ lastSeenIteration: 0,
+ activity: [],
+ startedAt: "2026-05-25T00:00:01Z",
+ endedAt: null,
+ },
+ ],
+ terminal: "error",
+ terminalMessage:
+ "Copilot hit an internal error before it could finish this turn.",
+ narrativeSummary: "Copilot hit an internal error.",
+ priorBlockCount: null,
+ designActivity: [],
+ startedAt: "2026-05-25T00:00:00Z",
+ endedAt: "2026-05-25T00:00:05Z",
+ },
+ }),
+ );
+
+ expect(s.terminal).toBe("error");
+ expect(s.narrativeSummary).toBe("Copilot hit an internal error.");
+ expect(s.blocks).toHaveLength(1);
+ expect(s.blocks[0]?.state).toBe("failed");
+ });
+
+ it("response preserves ASK_QUESTION classification for summary mode", () => {
+ const s = applyNarrativeEvent(
+ EMPTY_NARRATIVE,
+ response({
+ message: "Please provide the exact registry URL.",
+ response_type: "ASK_QUESTION",
+ narrative_payload: {
+ turnId: "turn-1",
+ turnIndex: 0,
+ mode: "diagnose",
+ designStarted: true,
+ designEnded: true,
+ draft: null,
+ blocks: [],
+ terminal: "response",
+ terminalMessage: "Please provide the exact registry URL.",
+ narrativeSummary: "Please provide the exact registry URL.",
+ priorBlockCount: null,
+ designActivity: [],
+ startedAt: "2026-05-25T00:00:00Z",
+ endedAt: "2026-05-25T00:00:05Z",
+ },
+ }),
+ );
+
+ expect(s.responseType).toBe("ASK_QUESTION");
+ expect(effectiveMode(s)).toBe("clarify");
+ });
+
it("response closes design even when design_end was never emitted (CORR-3)", () => {
let s = applyNarrativeEvent(EMPTY_NARRATIVE, designStart());
expect(s.designEnded).toBe(false);
@@ -412,6 +484,16 @@ describe("effectiveMode", () => {
}
});
+ it("reports clarify when backend classified the response as ASK_QUESTION", () => {
+ const s: TurnNarrativeState = {
+ ...EMPTY_NARRATIVE,
+ mode: "diagnose",
+ responseType: "ASK_QUESTION",
+ terminal: "response",
+ };
+ expect(effectiveMode(s)).toBe("clarify");
+ });
+
it("falls back to classifier mode while turn is still in-flight (no terminal)", () => {
const s: TurnNarrativeState = {
...EMPTY_NARRATIVE,
diff --git a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx
index d37bfc3a9..02ffaf796 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx
@@ -94,12 +94,6 @@ function liveElapsed(startedAt: string | null): string | null {
return `${m}:${s.toString().padStart(2, "0")}`;
}
-function truncate(s: string, n: number): string {
- if (!s) return "";
- if (s.length <= n) return s;
- return s.slice(0, n).replace(/[\s,;:.!?]+$/, "") + "…";
-}
-
function Spinner({ small = false }: { small?: boolean }) {
const sizeClass = small ? "h-2 w-2" : "h-2.5 w-2.5";
return (
@@ -427,28 +421,50 @@ interface TurnSummary {
isQA: boolean;
}
+function asksUserForInput(turn: TurnNarrativeState): boolean {
+ if (turn.responseType === "ASK_QUESTION") {
+ return true;
+ }
+ const text = `${turn.terminalMessage ?? ""} ${turn.narrativeSummary ?? ""}`
+ .toLowerCase()
+ .trim();
+ return (
+ text.includes("please provide") ||
+ text.includes("please share") ||
+ text.includes("could you provide") ||
+ text.includes("can you provide") ||
+ /^(which|what|where|who|when|how)\b[\s\S]*\?/.test(text)
+ );
+}
+
function computeTurnSummary(turn: TurnNarrativeState): TurnSummary {
const isFail =
turn.terminal === "error" || turn.blocks.some((b) => b.state === "failed");
const mode = effectiveMode(turn);
+ const needsInput = asksUserForInput(turn);
const isQA =
- mode === "docs_answer" || mode === "clarify" || mode === "refuse";
+ mode === "docs_answer" ||
+ mode === "diagnose" ||
+ mode === "clarify" ||
+ mode === "refuse";
const hasDrafts = (turn.draft?.blockCount ?? 0) > 0;
const hasEdited = (turn.priorBlockCount ?? 0) > 0 && hasDrafts;
const headline = isFail
? "Run halted"
- : isQA
- ? mode === "docs_answer"
- ? "Answered"
- : mode === "refuse"
+ : needsInput
+ ? "Question"
+ : isQA
+ ? mode === "refuse"
? "Declined"
- : "Followed up"
- : hasEdited
- ? "Applied edits and re-tested"
- : hasDrafts
- ? "Built and tested the workflow"
- : "Completed the run";
+ : mode === "clarify"
+ ? "Question"
+ : "Answered"
+ : hasEdited
+ ? "Applied edits and re-tested"
+ : hasDrafts
+ ? "Built and tested the workflow"
+ : "Completed the run";
const stats: string[] = [];
const turnElapsed = formatElapsed(turn.startedAt, turn.endedAt);
@@ -556,7 +572,7 @@ function RollupCard({ turn, summary, onExpand }: RollupCardProps) {
summary.isFail ? "text-rose-200/90" : "text-slate-400"
}`}
>
- {summary.isQA ? truncate(closing, 90) : closing}
+ {closing}
</div>
) : null
}
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx
index 88c22ec2c..d9cd4bcce 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx
@@ -247,6 +247,120 @@ describe("WorkflowCopilotChat — keep the chat live during a turn", () => {
expect(screen.getByText(/I encountered an error/)).toBeTruthy();
});
+ it("renders a response-only error narrative payload as halted", async () => {
+ await renderChat();
+ await submit("build me a workflow");
+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));
+
+ const call = streamCalls[0];
+ if (!call) throw new Error("no pending stream to complete");
+
+ await act(async () => {
+ call.onMessage({
+ ...terminalResponse(
+ "Copilot hit an internal error before it could finish this turn.",
+ ),
+ narrative_payload: {
+ turnId: "turn-1",
+ turnIndex: 0,
+ mode: "build",
+ designStarted: false,
+ designEnded: true,
+ draft: null,
+ blocks: [],
+ terminal: "error",
+ terminalMessage:
+ "Copilot hit an internal error before it could finish this turn.",
+ narrativeSummary: "Copilot hit an internal error.",
+ priorBlockCount: null,
+ designActivity: [],
+ startedAt: null,
+ endedAt: null,
+ },
+ });
+ call.resolve();
+ });
+
+ expect(screen.getByText("Run halted")).toBeTruthy();
+ expect(screen.queryByText("Completed the run")).toBeNull();
+ });
+
+ it("renders an ASK_QUESTION response payload as a question", async () => {
+ await renderChat();
+ await submit("build a lookup workflow");
+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));
+
+ const call = streamCalls[0];
+ if (!call) throw new Error("no pending stream to complete");
+
+ await act(async () => {
+ call.onMessage({
+ ...terminalResponse("Please provide the exact registry URL."),
+ response_type: "ASK_QUESTION",
+ narrative_payload: {
+ turnId: "turn-1",
+ turnIndex: 0,
+ mode: "diagnose",
+ responseType: "ASK_QUESTION",
+ designStarted: false,
+ designEnded: true,
+ draft: null,
+ blocks: [],
+ terminal: "response",
+ terminalMessage: "Please provide the exact registry URL.",
+ narrativeSummary: "Please provide the exact registry URL.",
+ priorBlockCount: null,
+ designActivity: [],
+ startedAt: null,
+ endedAt: null,
+ },
+ });
+ call.resolve();
+ });
+
+ expect(screen.getByText("Question")).toBeTruthy();
+ expect(screen.queryByText("Completed the run")).toBeNull();
+ });
+
+ it("renders a legacy diagnose payload asking for input as a question", async () => {
+ await renderChat();
+ await submit("build a lookup workflow");
+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));
+
+ const call = streamCalls[0];
+ if (!call) throw new Error("no pending stream to complete");
+ const longInputRequest =
+ "Please provide the exact BACB lookup/registry URL you want the workflow to use. I will build a general workflow with a person_name input after you provide it.";
+
+ await act(async () => {
+ call.onMessage({
+ ...terminalResponse(longInputRequest),
+ narrative_payload: {
+ turnId: "turn-1",
+ turnIndex: 0,
+ mode: "diagnose",
+ designStarted: false,
+ designEnded: true,
+ draft: null,
+ blocks: [],
+ terminal: "response",
+ terminalMessage: longInputRequest,
+ narrativeSummary: longInputRequest,
+ priorBlockCount: null,
+ designActivity: [],
+ startedAt: null,
+ endedAt: null,
+ },
+ });
+ call.resolve();
+ });
+
+ expect(screen.getByText("Question")).toBeTruthy();
+ expect(screen.getByText(longInputRequest)).toBeTruthy();
+ expect(screen.queryByText("Answered")).toBeNull();
+ expect(screen.queryByText("Completed the run")).toBeNull();
+ });
+
it("does not orphan a message on a same-tick double submit while working", async () => {
await renderChat();
await submit("first message");
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
index 7fda69d3c..9197ad2e5 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
@@ -1012,9 +1012,17 @@ export function WorkflowCopilotChat({
// handleSend time (pre-turn_start), so the React state binding is
// stale here.
const liveNarrative = narrativeRef.current;
+ const hasNarrativePayload =
+ response.narrative_payload !== null &&
+ typeof response.narrative_payload === "object";
const frozenNarrative: TurnNarrativeState | undefined =
- liveNarrative.turnId !== null
- ? applyNarrativeEvent(liveNarrative, response)
+ liveNarrative.turnId !== null || hasNarrativePayload
+ ? applyNarrativeEvent(
+ liveNarrative.turnId !== null
+ ? liveNarrative
+ : EMPTY_NARRATIVE,
+ response,
+ )
: undefined;
const aiMessage: ChatMessage = {
@@ -1043,9 +1051,9 @@ export function WorkflowCopilotChat({
) {
applyWorkflowUpdate(response.updated_workflow);
} else if (
- // Cancel terminal on a turn that produced staged content →
+ // Cancel/error terminal on a turn that produced staged content →
// snap canvas back to the pre-submit client snapshot.
- response.cancelled &&
+ (response.cancelled || frozenNarrative?.terminal === "error") &&
responseEntry?.hadStagedDraft &&
responseEntry?.snapshot
) {
diff --git a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts
index 385afbb79..926f078af 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts
+++ b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts
@@ -4,6 +4,7 @@
// exercised under vitest without a JSX runtime.
import {
+ CopilotResponseType,
WorkflowCopilotBlockProgressUpdate,
WorkflowCopilotDesignEndUpdate,
WorkflowCopilotDesignStartUpdate,
@@ -76,6 +77,7 @@ export interface TurnNarrativeState {
turnId: string | null;
turnIndex: number | null;
mode: string;
+ responseType: CopilotResponseType | null;
designStarted: boolean;
designEnded: boolean;
draft: {
@@ -104,6 +106,7 @@ export const EMPTY_NARRATIVE: TurnNarrativeState = Object.freeze({
turnId: null,
turnIndex: null,
mode: "unknown",
+ responseType: null,
designStarted: false,
designEnded: false,
draft: null,
@@ -375,6 +378,20 @@ export function applyNarrativeEvent(
}
case "response": {
+ const hydrated = hydrateNarrativeFromPayload(event.narrative_payload);
+ if (hydrated) {
+ return {
+ ...hydrated,
+ responseType: event.response_type ?? hydrated.responseType,
+ terminalMessage: hydrated.terminalMessage ?? event.message,
+ narrativeSummary:
+ hydrated.narrativeSummary ??
+ event.narrative_summary ??
+ event.message,
+ endedAt: hydrated.endedAt ?? event.response_time ?? prev.endedAt,
+ };
+ }
+
// Close the design phase on terminal even when DESIGN_END never
// arrived (a refusal turn can emit DESIGN_START via first
// tool_called and then exit without persisting a workflow).
@@ -389,6 +406,7 @@ export function applyNarrativeEvent(
);
return {
...prev,
+ responseType: event.response_type ?? prev.responseType,
designEnded: true,
terminal: "response",
terminalMessage: event.message,
@@ -466,6 +484,13 @@ export function hydrateNarrativeFromPayload(
if (turnId === null) return undefined;
const mode = typeof payload.mode === "string" ? payload.mode : "unknown";
+ const rawResponseType = payload.responseType;
+ const responseType: CopilotResponseType | null =
+ rawResponseType === "REPLY" ||
+ rawResponseType === "ASK_QUESTION" ||
+ rawResponseType === "REPLACE_WORKFLOW"
+ ? rawResponseType
+ : null;
const draftRaw = payload.draft;
const draft =
draftRaw && typeof draftRaw === "object"
@@ -551,6 +576,7 @@ export function hydrateNarrativeFromPayload(
turnId,
turnIndex,
mode: mode as TurnNarrativeState["mode"],
+ responseType,
designStarted: true,
designEnded: true,
draft,
@@ -581,6 +607,9 @@ export function hydrateNarrativeFromPayload(
// classifier-said-"draft_only" turn that only asked a clarification
// shows "clarify".
export function effectiveMode(turn: TurnNarrativeState): string {
+ if (turn.responseType === "ASK_QUESTION") {
+ return "clarify";
+ }
const blockCount = turn.draft?.blockCount ?? turn.blocks.length;
if (blockCount > 0) {
const priorBlocks = turn.priorBlockCount ?? 0;
diff --git a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts
index 6af6c1ecd..802864479 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts
+++ b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts
@@ -9,6 +9,7 @@ export type ProposalDisposition =
| "auto_applicable"
| "review_untested"
| "review_tested";
+export type CopilotResponseType = "REPLY" | "ASK_QUESTION" | "REPLACE_WORKFLOW";
export interface WorkflowCopilotChat {
workflow_copilot_chat_id: string;
@@ -93,6 +94,7 @@ export interface WorkflowCopilotStreamResponseUpdate {
message: string;
updated_workflow?: WorkflowApiResponse | null;
response_time: string;
+ response_type?: CopilotResponseType;
proposal_disposition: ProposalDisposition;
// Cancel forces explicit review.
cancelled?: boolean;
@@ -100,6 +102,7 @@ export interface WorkflowCopilotStreamResponseUpdate {
// turn-narrative envelope.
turn_id?: string | null;
narrative_summary?: string | null;
+ narrative_payload?: Record<string, unknown> | null;
}
export interface WorkflowCopilotStreamErrorUpdate {
diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.py
index 4726914ab..9c5f67453 100644
--- a/skyvern/forge/sdk/copilot/agent.py
+++ b/skyvern/forge/sdk/copilot/agent.py
@@ -783,6 +783,10 @@ def _make_agent_result(
if ctx is not None
else global_llm_context
)
+ narrative_payload = kwargs.get("narrative_payload")
+ response_type = kwargs.get("response_type", "REPLY")
+ if isinstance(narrative_payload, dict) and "responseType" not in narrative_payload:
+ kwargs["narrative_payload"] = {**narrative_payload, "responseType": response_type}
return AgentResult(global_llm_context=final_context, turn_outcome=turn_outcome, **kwargs)
@@ -944,7 +948,7 @@ def _build_exit_result(
narrative_summary=ctx.narrative_summary,
narrative_payload=_build_narrative_payload(
ctx,
- terminal="error" if cancelled else "response",
+ terminal="error" if cancelled or terminal_reason else "response",
terminal_message=final_text,
narrative_summary=ctx.narrative_summary,
),
diff --git a/skyvern/forge/sdk/copilot/context.py b/skyvern/forge/sdk/copilot/context.py
index d2deb8a25..7278fecec 100644
--- a/skyvern/forge/sdk/copilot/context.py
+++ b/skyvern/forge/sdk/copilot/context.py
@@ -52,6 +52,7 @@ class TurnNarrativePayload(TypedDict):
turnId: str | None
turnIndex: int
mode: str
+ responseType: NotRequired[ResponseType]
designStarted: bool
designEnded: bool
draft: NarrativeDraft | None
diff --git a/skyvern/forge/sdk/db/repositories/workflow_parameters.py b/skyvern/forge/sdk/db/repositories/workflow_parameters.py
index 1a79bdb90..1534a4741 100644
--- a/skyvern/forge/sdk/db/repositories/workflow_parameters.py
+++ b/skyvern/forge/sdk/db/repositories/workflow_parameters.py
@@ -8,6 +8,7 @@ import structlog
from sqlalchemy import select
from skyvern.config import settings
+from skyvern.forge.sdk.copilot.context import TurnNarrativePayload
from skyvern.forge.sdk.db._error_handling import db_operation
from skyvern.forge.sdk.db._sentinels import _UNSET
from skyvern.forge.sdk.db.base_repository import BaseRepository
@@ -521,7 +522,7 @@ class WorkflowParametersRepository(BaseRepository):
content: str,
global_llm_context: str | None = None,
turn_outcome: TurnOutcome | None = None,
- narrative_payload: dict | None = None,
+ narrative_payload: TurnNarrativePayload | dict[str, Any] | None = None,
) -> WorkflowCopilotChatMessage:
async with self.Session() as session:
new_message = WorkflowCopilotChatMessageModel(
diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.py
index f8db8d99e..e2a37f8f4 100644
--- a/skyvern/forge/sdk/routes/workflow_copilot.py
+++ b/skyvern/forge/sdk/routes/workflow_copilot.py
@@ -441,6 +441,9 @@ async def _persist_cancel_turn(
response_type = "REPLY"
output_policy_diagnostics = None
turn_outcome = None
+ response_turn_id = turn_id
+ narrative_summary = None
+ narrative_payload = None
if chat.proposed_workflow is not None:
await asyncio.shield(_clear_proposed_workflow(chat))
else:
@@ -461,10 +464,13 @@ async def _persist_cancel_turn(
user_response = agent_result.user_response
updated_workflow = agent_result.updated_workflow
updated_global_llm_context = agent_result.global_llm_context
- total_tokens = getattr(agent_result, "total_tokens", None)
- response_type = getattr(agent_result, "response_type", "REPLY")
- output_policy_diagnostics = getattr(agent_result, "output_policy_diagnostics", None)
+ total_tokens = agent_result.total_tokens
+ response_type = agent_result.response_type
+ output_policy_diagnostics = agent_result.output_policy_diagnostics
turn_outcome = agent_result.turn_outcome
+ response_turn_id = turn_id or agent_result.turn_id
+ narrative_summary = agent_result.narrative_summary
+ narrative_payload = agent_result.narrative_payload
await asyncio.shield(
app.DATABASE.workflow_params.create_workflow_copilot_chat_message(
@@ -482,7 +488,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),
+ narrative_payload=narrative_payload,
)
)
proposal_disposition = _proposal_disposition(agent_result)
@@ -500,8 +506,9 @@ async def _persist_cancel_turn(
proposal_disposition=proposal_disposition,
cancelled=True,
output_policy_diagnostics=output_policy_diagnostics,
- turn_id=turn_id or getattr(agent_result, "turn_id", None),
- narrative_summary=getattr(agent_result, "narrative_summary", None),
+ turn_id=response_turn_id,
+ narrative_summary=narrative_summary,
+ narrative_payload=narrative_payload,
)
)
)
@@ -581,7 +588,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),
+ narrative_payload=agent_result.narrative_payload,
)
proposal_disposition = _proposal_disposition(agent_result)
@@ -592,12 +599,13 @@ async def _finalise_normal_turn(
message=user_response,
updated_workflow=updated_workflow.model_dump(mode="json") if updated_workflow else None,
response_time=assistant_message.created_at,
- total_tokens=getattr(agent_result, "total_tokens", None),
- response_type=getattr(agent_result, "response_type", "REPLY"),
+ total_tokens=agent_result.total_tokens,
+ response_type=agent_result.response_type,
proposal_disposition=proposal_disposition,
- output_policy_diagnostics=getattr(agent_result, "output_policy_diagnostics", None),
- turn_id=getattr(agent_result, "turn_id", None),
- narrative_summary=getattr(agent_result, "narrative_summary", None),
+ output_policy_diagnostics=agent_result.output_policy_diagnostics,
+ turn_id=agent_result.turn_id,
+ narrative_summary=agent_result.narrative_summary,
+ narrative_payload=agent_result.narrative_payload,
)
)
diff --git a/skyvern/forge/sdk/schemas/workflow_copilot.py b/skyvern/forge/sdk/schemas/workflow_copilot.py
index 9526f7d44..2fa7303af 100644
--- a/skyvern/forge/sdk/schemas/workflow_copilot.py
+++ b/skyvern/forge/sdk/schemas/workflow_copilot.py
@@ -168,6 +168,10 @@ class WorkflowCopilotStreamResponseUpdate(BaseModel):
None,
description="One-line accomplishment summary for the turn. Optional; the frontend falls back to the response message when absent.",
)
+ narrative_payload: TurnNarrativePayload | None = Field(
+ None,
+ description="Terminal narrative bubble snapshot for live clients; mirrors the persisted assistant chat row.",
+ )
class WorkflowCopilotStreamErrorUpdate(BaseModel):
diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.py
index 4475ee687..7128f39fa 100644
--- a/tests/unit/test_copilot_agent_helpers.py
+++ b/tests/unit/test_copilot_agent_helpers.py
@@ -1243,6 +1243,8 @@ workflow_definition:
assert agent_result.updated_workflow is None
assert agent_result.proposal_disposition == "auto_applicable"
assert agent_result.response_type == "ASK_QUESTION"
+ assert agent_result.narrative_payload is not None
+ assert agent_result.narrative_payload["responseType"] == "ASK_QUESTION"
def test_probable_site_block_ask_question_is_concise_and_proxy_aware(self) -> None:
ctx = _ctx(
@@ -1834,6 +1836,8 @@ workflow_definition:
assert agent_result.updated_workflow is None
assert agent_result.clear_proposed_workflow is True
+ assert agent_result.narrative_payload is not None
+ assert agent_result.narrative_payload["responseType"] == "ASK_QUESTION"
def test_reply_does_not_set_clear_proposed_flag(self) -> None:
# Differential: a REPLY turn surfaces the verified workflow and leaves
diff --git a/tests/unit/test_workflow_copilot_cancel.py b/tests/unit/test_workflow_copilot_cancel.py
index c35306e92..4a4c9d245 100644
--- a/tests/unit/test_workflow_copilot_cancel.py
+++ b/tests/unit/test_workflow_copilot_cancel.py
@@ -189,6 +189,19 @@ def _setup_route_mocks(
original_workflow: SimpleNamespace,
agent_result: SimpleNamespace,
) -> tuple[AsyncMock, SimpleNamespace]:
+ if not hasattr(agent_result, "total_tokens"):
+ agent_result.total_tokens = None
+ if not hasattr(agent_result, "response_type"):
+ agent_result.response_type = "REPLY"
+ if not hasattr(agent_result, "output_policy_diagnostics"):
+ agent_result.output_policy_diagnostics = None
+ if not hasattr(agent_result, "turn_id"):
+ agent_result.turn_id = None
+ if not hasattr(agent_result, "narrative_summary"):
+ agent_result.narrative_summary = None
+ if not hasattr(agent_result, "narrative_payload"):
+ agent_result.narrative_payload = None
+
async def fake_llm_handler(*args: object, **kwargs: object) -> None:
del args, kwargs
return None
diff --git a/tests/unit/test_workflow_copilot_route.py b/tests/unit/test_workflow_copilot_route.py
index 5f3ec8d36..17368d840 100644
--- a/tests/unit/test_workflow_copilot_route.py
+++ b/tests/unit/test_workflow_copilot_route.py
@@ -140,6 +140,18 @@ def _setup_new_copilot_mocks(
Returns the restore-on-error mock so callers can assert on it.
"""
+ if not hasattr(agent_result, "response_type"):
+ agent_result.response_type = "REPLY"
+ if not hasattr(agent_result, "total_tokens"):
+ agent_result.total_tokens = None
+ if not hasattr(agent_result, "output_policy_diagnostics"):
+ agent_result.output_policy_diagnostics = None
+ if not hasattr(agent_result, "turn_id"):
+ agent_result.turn_id = None
+ if not hasattr(agent_result, "narrative_summary"):
+ agent_result.narrative_summary = None
+ if not hasattr(agent_result, "narrative_payload"):
+ agent_result.narrative_payload = None
async def fake_llm_handler(*args: object, **kwargs: object) -> None:
del args, kwargs
@@ -349,7 +361,10 @@ async def test_flag_on_pre_agent_failure_persists_recoverable_reply(monkeypatch:
assert "reference cpe_" in assistant_contents[0]
frames = [call.args[0] for call in stream.send.await_args_list if call.args]
- assert any(isinstance(frame, WorkflowCopilotStreamResponseUpdate) for frame in frames)
+ response_frames = [frame for frame in frames if isinstance(frame, WorkflowCopilotStreamResponseUpdate)]
+ assert response_frames
+ assert response_frames[-1].narrative_payload is not None
+ assert response_frames[-1].narrative_payload["terminal"] == "error"
assert not any(isinstance(frame, WorkflowCopilotStreamErrorUpdate) for frame in frames)
@@ -425,7 +440,10 @@ async def test_flag_on_route_error_after_chat_persists_recoverable_reply(
assert "reference cpe_" in assistant_contents[0]
frames = [call.args[0] for call in stream.send.await_args_list if call.args]
- assert any(isinstance(frame, WorkflowCopilotStreamResponseUpdate) for frame in frames)
+ response_frames = [frame for frame in frames if isinstance(frame, WorkflowCopilotStreamResponseUpdate)]
+ assert response_frames
+ assert response_frames[-1].narrative_payload is not None
+ assert response_frames[-1].narrative_payload["terminal"] == "error"
assert not any(isinstance(frame, WorkflowCopilotStreamErrorUpdate) for frame in frames)