Skip to content

Automate browser based workflows with AI

fix(copilot): render terminal headline from the typed turn adjudication (SKY-10871) (#6547)

Andrew Neilson2f8541c4721c
diff --git a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.ts b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.ts
index 801f09acd..e03c50389 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.ts
+++ b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.ts
@@ -1,10 +1,14 @@
import { describe, expect, it } from "vitest";
import {
+ BlockState,
EMPTY_NARRATIVE,
TurnNarrativeState,
applyNarrativeEvent,
+ computeTurnSummary,
effectiveMode,
+ hydrateHistoryNarrative,
+ hydrateNarrativeFromPayload,
} from "./narrativeState";
import {
WorkflowCopilotBlockProgressUpdate,
@@ -564,3 +568,366 @@ describe("effectiveMode", () => {
expect(effectiveMode(s)).toBe("build");
});
});
+
+const summaryBlock = (
+ label: string,
+ state: BlockState["state"] = "completed",
+): BlockState => ({
+ workflowRunBlockId: `wrb_${label}`,
+ label,
+ blockType: "task",
+ state,
+ lastSeenIteration: 0,
+ activity: [],
+ startedAt: null,
+ endedAt: null,
+});
+
+const reproBlock = (label: string): Record<string, unknown> => ({
+ workflowRunBlockId: "",
+ label,
+ blockType: "code",
+ state: "completed",
+ lastSeenIteration: 0,
+ activity: [],
+ startedAt: "2026-06-10T07:27:57.458136+00:00",
+ endedAt: "2026-06-10T07:28:37.384095+00:00",
+});
+
+// Sanitized copy of a persisted false-green repro payload: a build turn that
+// drafted and ran 3 blocks but terminated via the loop-guard clarify ask.
+const reproClarifyPayload = (
+ overrides: Record<string, unknown> = {},
+): Record<string, unknown> => ({
+ turnId: "turn-repro",
+ turnIndex: 0,
+ mode: "build",
+ responseType: "REPLY",
+ cancelled: false,
+ proposalDisposition: "no_proposal",
+ designStarted: true,
+ designEnded: true,
+ draft: {
+ blockCount: 3,
+ blockLabels: [
+ "open_registry_find_registrant",
+ "search_jane_doe_credential_a",
+ "expand_and_extract_certifications",
+ ],
+ summary: null,
+ },
+ blocks: [
+ reproBlock("open_registry_find_registrant"),
+ reproBlock("search_jane_doe_credential_a"),
+ reproBlock("expand_and_extract_certifications"),
+ ],
+ terminal: "response",
+ terminalMessage:
+ "I'm stuck retrying the same step. Tell me what to change and I'll try a different approach.",
+ narrativeSummary:
+ "I'm stuck retrying the same step. Tell me what to change and I'll try a different approach.",
+ priorBlockCount: 0,
+ designActivity: [],
+ startedAt: "2026-06-10T07:22:55.699474+00:00",
+ endedAt: "2026-06-10T07:37:57.457019+00:00",
+ ...overrides,
+});
+
+const buildTurn = (
+ overrides: Partial<TurnNarrativeState> = {},
+): TurnNarrativeState => ({
+ ...EMPTY_NARRATIVE,
+ mode: "build",
+ terminal: "response",
+ ...overrides,
+});
+
+const draft3 = {
+ blockCount: 3,
+ blockLabels: ["block_one", "block_two", "block_three"],
+ summary: null,
+};
+
+describe("hydrateNarrativeFromPayload — terminal adjudication fields", () => {
+ it("hydrates responseKind and verifiedSuccess from the payload", () => {
+ const turn = hydrateNarrativeFromPayload(
+ reproClarifyPayload({ responseKind: "build", verifiedSuccess: true }),
+ );
+ expect(turn?.responseKind).toBe("build");
+ expect(turn?.verifiedSuccess).toBe(true);
+ });
+
+ it("treats absent fields as null", () => {
+ const turn = hydrateNarrativeFromPayload(reproClarifyPayload());
+ expect(turn?.responseKind).toBeNull();
+ expect(turn?.verifiedSuccess).toBeNull();
+ });
+
+ it("treats unknown responseKind and non-boolean verifiedSuccess as absent", () => {
+ const turn = hydrateNarrativeFromPayload(
+ reproClarifyPayload({
+ responseKind: "celebrate",
+ verifiedSuccess: "yes",
+ }),
+ );
+ expect(turn?.responseKind).toBeNull();
+ expect(turn?.verifiedSuccess).toBeNull();
+ });
+});
+
+describe("computeTurnSummary — typed terminal adjudication", () => {
+ it("renders the loop-guard clarify repro as a Question, not a green built-and-tested claim", () => {
+ const turn = hydrateNarrativeFromPayload(
+ reproClarifyPayload({ responseKind: "clarify", verifiedSuccess: false }),
+ );
+ expect(turn).toBeDefined();
+ const summary = computeTurnSummary(turn!);
+ expect(summary.headline).toBe("Question");
+ expect(summary.accent).toBe("qa");
+ expect(summary.glyph).toBe("✦");
+ });
+
+ it("keeps the factual stats line on an adjudicated clarify build turn", () => {
+ const turn = hydrateNarrativeFromPayload(
+ reproClarifyPayload({ responseKind: "clarify", verifiedSuccess: false }),
+ )!;
+ const summary = computeTurnSummary(turn);
+ expect(summary.stats).toEqual(["15:02", "3 blocks ran", "3 new"]);
+ });
+
+ it("never claims re-tested edits on an adjudicated clarify edit turn", () => {
+ const turn = hydrateNarrativeFromPayload(
+ reproClarifyPayload({
+ responseKind: "clarify",
+ verifiedSuccess: false,
+ priorBlockCount: 2,
+ }),
+ )!;
+ const summary = computeTurnSummary(turn);
+ expect(summary.headline).toBe("Question");
+ expect(summary.headline).not.toBe("Applied edits and re-tested");
+ expect(summary.accent).not.toBe("ok");
+ });
+
+ it.each([
+ ["clarify", "Question"],
+ ["recover", "Question"],
+ ["refuse", "Declined"],
+ ["diagnose", "Answered"],
+ ] as const)(
+ "maps non-build kind %s to %s with qa accent",
+ (kind, headline) => {
+ const summary = computeTurnSummary(
+ buildTurn({ responseKind: kind, verifiedSuccess: false }),
+ );
+ expect(summary.headline).toBe(headline);
+ expect(summary.accent).toBe("qa");
+ expect(summary.glyph).toBe("✦");
+ },
+ );
+
+ it("renders a verdict-authorized tested success green with drafts", () => {
+ const summary = computeTurnSummary(
+ buildTurn({
+ draft: draft3,
+ proposalDisposition: "no_proposal",
+ responseKind: "build",
+ verifiedSuccess: true,
+ }),
+ );
+ expect(summary.headline).toBe("Built and tested the workflow");
+ expect(summary.accent).toBe("ok");
+ expect(summary.glyph).toBe("✓");
+ });
+
+ it("renders a verdict-authorized edit turn green", () => {
+ const summary = computeTurnSummary(
+ buildTurn({
+ draft: draft3,
+ priorBlockCount: 2,
+ responseKind: "build",
+ verifiedSuccess: true,
+ }),
+ );
+ expect(summary.headline).toBe("Applied edits and re-tested");
+ expect(summary.accent).toBe("ok");
+ });
+
+ it("renders a verdict-authorized draftless turn as a completed run", () => {
+ const summary = computeTurnSummary(
+ buildTurn({ responseKind: "build", verifiedSuccess: true }),
+ );
+ expect(summary.headline).toBe("Completed the run");
+ expect(summary.accent).toBe("ok");
+ });
+
+ it("ignores the prose question heuristic when a typed verdict authorizes success", () => {
+ const summary = computeTurnSummary(
+ buildTurn({
+ draft: draft3,
+ terminalMessage: "Could you provide feedback on the result?",
+ responseKind: "build",
+ verifiedSuccess: true,
+ }),
+ );
+ expect(summary.headline).toBe("Built and tested the workflow");
+ expect(summary.accent).toBe("ok");
+ });
+
+ it.each([
+ [buildTurn({ responseKind: "build", verifiedSuccess: false }), "Stopped"],
+ [
+ buildTurn({
+ draft: draft3,
+ proposalDisposition: "auto_applicable",
+ responseKind: "build",
+ verifiedSuccess: false,
+ }),
+ "Stopped",
+ ],
+ [
+ buildTurn({
+ draft: draft3,
+ priorBlockCount: 2,
+ responseKind: "build",
+ verifiedSuccess: false,
+ }),
+ "Stopped",
+ ],
+ [
+ buildTurn({
+ draft: draft3,
+ proposalDisposition: "review_untested",
+ responseKind: "build",
+ verifiedSuccess: false,
+ }),
+ "Draft needs review",
+ ],
+ [
+ buildTurn({
+ draft: draft3,
+ proposalDisposition: "review_tested",
+ responseKind: "build",
+ verifiedSuccess: false,
+ }),
+ "Workflow ready for review",
+ ],
+ ])(
+ "never renders green when the verdict refused the success claim (%#)",
+ (turn, headline) => {
+ const summary = computeTurnSummary(turn);
+ expect(summary.headline).toBe(headline);
+ expect(summary.accent).toBe("qa");
+ expect(summary.glyph).not.toBe("✓");
+ },
+ );
+
+ it("a failed block still renders Run halted even with a verdict-authorized success", () => {
+ const summary = computeTurnSummary(
+ buildTurn({
+ draft: draft3,
+ blocks: [
+ summaryBlock("block_one"),
+ summaryBlock("block_two", "failed"),
+ ],
+ responseKind: "build",
+ verifiedSuccess: true,
+ }),
+ );
+ expect(summary.headline).toBe("Run halted");
+ expect(summary.accent).toBe("fail");
+ expect(summary.glyph).toBe("✕");
+ });
+
+ it("cancelled with a reviewable draft still renders Stopped with a draft", () => {
+ const summary = computeTurnSummary(
+ buildTurn({
+ cancelled: true,
+ draft: draft3,
+ proposalDisposition: "review_untested",
+ responseKind: "build",
+ verifiedSuccess: true,
+ }),
+ );
+ expect(summary.headline).toBe("Stopped with a draft");
+ expect(summary.accent).toBe("qa");
+ });
+
+ it("legacy payload without adjudication renders via the unchanged inference chain", () => {
+ const turn = hydrateNarrativeFromPayload(reproClarifyPayload())!;
+ const summary = computeTurnSummary(turn);
+ expect(summary.headline).toBe("Built and tested the workflow");
+ expect(summary.accent).toBe("ok");
+ expect(summary.stats).toEqual(["15:02", "3 blocks ran", "3 new"]);
+ });
+});
+
+describe("hydrateHistoryNarrative — persisted turn_outcome graft", () => {
+ it("grafts clarify from the adjacent turn_outcome onto a pre-fix payload", () => {
+ const turn = hydrateHistoryNarrative(reproClarifyPayload(), {
+ response_kind: "clarify",
+ })!;
+ expect(turn.responseKind).toBe("clarify");
+ expect(turn.verifiedSuccess).toBeNull();
+ const summary = computeTurnSummary(turn);
+ expect(summary.headline).toBe("Question");
+ expect(summary.accent).toBe("qa");
+ });
+
+ it("keeps the payload's own responseKind over the graft", () => {
+ const turn = hydrateHistoryNarrative(
+ reproClarifyPayload({ responseKind: "refuse", verifiedSuccess: false }),
+ { response_kind: "clarify" },
+ )!;
+ expect(turn.responseKind).toBe("refuse");
+ });
+
+ it("renders grafted build-kind history via the legacy chain (no downgrade)", () => {
+ const turn = hydrateHistoryNarrative(reproClarifyPayload(), {
+ response_kind: "build",
+ })!;
+ expect(turn.responseKind).toBe("build");
+ expect(turn.verifiedSuccess).toBeNull();
+ const summary = computeTurnSummary(turn);
+ expect(summary.headline).toBe("Built and tested the workflow");
+ expect(summary.accent).toBe("ok");
+ });
+
+ it("tolerates missing or unknown turn_outcome", () => {
+ expect(
+ hydrateHistoryNarrative(reproClarifyPayload(), null)?.responseKind,
+ ).toBeNull();
+ expect(
+ hydrateHistoryNarrative(reproClarifyPayload(), {
+ response_kind: "celebrate",
+ })?.responseKind,
+ ).toBeNull();
+ expect(
+ hydrateHistoryNarrative(null, { response_kind: "clarify" }),
+ ).toBeUndefined();
+ });
+});
+
+describe("applyNarrativeEvent — terminal adjudication on live frames", () => {
+ it("carries the adjudication through the response reducer", () => {
+ const s = applyNarrativeEvent(
+ EMPTY_NARRATIVE,
+ response({
+ message: "I'm stuck retrying the same step.",
+ narrative_payload: reproClarifyPayload({
+ responseKind: "clarify",
+ verifiedSuccess: false,
+ }),
+ }),
+ );
+ expect(s.responseKind).toBe("clarify");
+ expect(s.verifiedSuccess).toBe(false);
+ expect(computeTurnSummary(s).headline).toBe("Question");
+ });
+
+ it("leaves both fields null on frames from an older backend", () => {
+ const s = applyNarrativeEvent(EMPTY_NARRATIVE, response());
+ expect(s.responseKind).toBeNull();
+ expect(s.verifiedSuccess).toBeNull();
+ });
+});
diff --git a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx
index 4fc55909d..79c605242 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx
@@ -4,8 +4,12 @@ import {
ActivityEntry,
BlockState,
TurnNarrativeState,
+ TurnSummary,
+ computeTurnSummary,
effectiveMode,
+ formatElapsed,
isBlockOk,
+ latestBlocksByLabel,
parseUtcIsoMs,
toolActivityDisplayLabel,
} from "./narrativeState";
@@ -74,19 +78,6 @@ function paletteFor(blockType: string): BlockPalette {
return PALETTE_TASK;
}
-function formatElapsed(
- startedAt: string | null,
- endedAt: string | null,
-): string | null {
- const startMs = parseUtcIsoMs(startedAt);
- const endMs = parseUtcIsoMs(endedAt);
- if (startMs === null || endMs === null) return null;
- const seconds = Math.max(0, Math.round((endMs - startMs) / 1000));
- const m = Math.floor(seconds / 60);
- const s = seconds % 60;
- return `${m}:${s.toString().padStart(2, "0")}`;
-}
-
function liveElapsed(startedAt: string | null): string | null {
const ms = parseUtcIsoMs(startedAt);
if (ms === null) return null;
@@ -463,125 +454,6 @@ function FDesignRow({ done, blockLabels, activity }: FDesignRowProps) {
);
}
-interface TurnSummary {
- headline: string;
- stats: string[];
- accent: "ok" | "fail" | "qa";
- glyph: string;
- isFail: boolean;
- isQA: boolean;
- isStoppedWithDraft: boolean;
-}
-
-function latestBlocksByLabel(blocks: BlockState[]): BlockState[] {
- const latest = new Map<string, BlockState>();
- for (const block of blocks) {
- latest.set(block.label, block);
- }
- return Array.from(latest.values());
-}
-
-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 rollupBlocks = latestBlocksByLabel(turn.blocks);
- const isFail =
- turn.terminal === "error" || rollupBlocks.some((b) => b.state === "failed");
- const mode = effectiveMode(turn);
- const needsInput = asksUserForInput(turn);
- const isQA =
- mode === "docs_answer" ||
- mode === "diagnose" ||
- mode === "clarify" ||
- mode === "refuse";
- const hasDrafts = (turn.draft?.blockCount ?? 0) > 0;
- const needsUntestedProposalReview =
- hasDrafts && turn.proposalDisposition === "review_untested";
- const needsTestedProposalReview =
- hasDrafts && turn.proposalDisposition === "review_tested";
- const hasEdited = (turn.priorBlockCount ?? 0) > 0 && hasDrafts;
- const hasReviewableDraft =
- hasDrafts &&
- (turn.proposalDisposition === "review_untested" ||
- turn.proposalDisposition === "review_tested" ||
- (turn.cancelled && turn.proposalDisposition !== "no_proposal"));
- const isStoppedWithDraft = hasReviewableDraft && (isFail || turn.cancelled);
-
- const headline = isStoppedWithDraft
- ? "Stopped with a draft"
- : isFail
- ? "Run halted"
- : needsInput
- ? "Question"
- : needsUntestedProposalReview
- ? "Draft needs review"
- : needsTestedProposalReview
- ? "Workflow ready for review"
- : isQA
- ? mode === "refuse"
- ? "Declined"
- : 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);
- if (turnElapsed) stats.push(turnElapsed);
- if (!isQA) {
- const ok = rollupBlocks.filter((b) => isBlockOk(b)).length;
- const failed = rollupBlocks.filter((b) => b.state === "failed").length;
- const newBlocks = hasEdited ? 0 : (turn.draft?.blockCount ?? 0);
- if (ok) stats.push(`${ok} block${ok === 1 ? "" : "s"} ran`);
- if (newBlocks) stats.push(`${newBlocks} new`);
- if (failed) stats.push(`${failed} failed`);
- }
-
- const accent = isStoppedWithDraft
- ? "qa"
- : isFail
- ? "fail"
- : needsUntestedProposalReview || needsTestedProposalReview || isQA
- ? "qa"
- : "ok";
- return {
- headline,
- stats,
- accent,
- glyph:
- isStoppedWithDraft ||
- needsUntestedProposalReview ||
- needsTestedProposalReview
- ? "!"
- : isFail
- ? "✕"
- : isQA
- ? "✦"
- : "✓",
- isFail,
- isQA,
- isStoppedWithDraft,
- };
-}
-
function accentBg(accent: TurnSummary["accent"]): string {
if (accent === "fail") {
return "border-rose-400/60 bg-rose-500/15 text-rose-300";
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
index 03844d5ce..f38a99f6c 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
@@ -57,7 +57,7 @@ import {
NarrativeEvent,
TurnNarrativeState,
applyNarrativeEvent,
- hydrateNarrativeFromPayload,
+ hydrateHistoryNarrative,
parseUtcIsoMs,
} from "./narrativeState";
import { computeFollowSignature, useStickToBottom } from "./useStickToBottom";
@@ -898,8 +898,9 @@ export function WorkflowCopilotChat({
content: message.content,
timestamp: message.created_at,
narrative: (() => {
- const hydrated = hydrateNarrativeFromPayload(
+ const hydrated = hydrateHistoryNarrative(
message.narrative_payload,
+ message.turn_outcome,
);
if (!hydrated) return undefined;
// Fall back to the legacy message body when the persisted
diff --git a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts
index f4a05777b..f6a14c69a 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts
+++ b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts
@@ -89,12 +89,27 @@ export interface ActivityEntry {
id: string;
}
+// Closed vocabulary of the backend TurnOutcome.response_kind enum. Unknown
+// wire values parse to null so a newer backend cannot crash the renderer.
+export type TurnResponseKind =
+ | "build"
+ | "clarify"
+ | "diagnose"
+ | "refuse"
+ | "recover";
+
export interface TurnNarrativeState {
turnId: string | null;
turnIndex: number | null;
mode: string;
responseType: CopilotResponseType | null;
proposalDisposition: ProposalDisposition | null;
+ // Typed terminal adjudication of the turn (TurnOutcome.response_kind).
+ // Null on legacy rows and frames from an older backend.
+ responseKind: TurnResponseKind | null;
+ // Outcome-evidence verdict authorizing tested-success claims (ADR 0005).
+ // Null means unknown (legacy/grafted rows) — distinct from false.
+ verifiedSuccess: boolean | null;
designStarted: boolean;
designEnded: boolean;
draft: {
@@ -126,6 +141,8 @@ export const EMPTY_NARRATIVE: TurnNarrativeState = Object.freeze({
mode: "unknown",
responseType: null,
proposalDisposition: null,
+ responseKind: null,
+ verifiedSuccess: null,
designStarted: false,
designEnded: false,
draft: null,
@@ -157,6 +174,16 @@ export function parseUtcIsoMs(iso: string | null | undefined): number | null {
return Number.isFinite(ms) ? ms : null;
}
+export function parseResponseKind(value: unknown): TurnResponseKind | null {
+ return value === "build" ||
+ value === "clarify" ||
+ value === "diagnose" ||
+ value === "refuse" ||
+ value === "recover"
+ ? value
+ : null;
+}
+
// Tool names we never surface in the user-facing activity log. Internal
// observation/maintenance tools are not interesting context for the user.
const ACTIVITY_TOOL_DENYLIST = new Set([
@@ -688,6 +715,11 @@ export function hydrateNarrativeFromPayload(
responseType,
cancelled: payload.cancelled === true,
proposalDisposition,
+ responseKind: parseResponseKind(payload.responseKind),
+ verifiedSuccess:
+ typeof payload.verifiedSuccess === "boolean"
+ ? payload.verifiedSuccess
+ : null,
designStarted: true,
designEnded: true,
draft,
@@ -738,3 +770,228 @@ export function effectiveMode(turn: TurnNarrativeState): string {
}
return turn.mode;
}
+
+// History rows persisted before narrative_payload carried responseKind still
+// have the adjacent persisted turn_outcome; graft its response_kind so
+// adjudicated clarify/refuse/recover history corrects retroactively.
+// verifiedSuccess stays null (unknown), so grafted build-kind rows render
+// via the legacy inference chain and tested successes never downgrade.
+export function hydrateHistoryNarrative(
+ payload: Record<string, unknown> | null | undefined,
+ turnOutcome: { response_kind?: string | null } | null | undefined,
+): TurnNarrativeState | undefined {
+ const hydrated = hydrateNarrativeFromPayload(payload);
+ if (!hydrated || hydrated.responseKind !== null) return hydrated;
+ const grafted = parseResponseKind(turnOutcome?.response_kind);
+ if (grafted === null) return hydrated;
+ return { ...hydrated, responseKind: grafted };
+}
+
+export function formatElapsed(
+ startedAt: string | null,
+ endedAt: string | null,
+): string | null {
+ const startMs = parseUtcIsoMs(startedAt);
+ const endMs = parseUtcIsoMs(endedAt);
+ if (startMs === null || endMs === null) return null;
+ const seconds = Math.max(0, Math.round((endMs - startMs) / 1000));
+ const m = Math.floor(seconds / 60);
+ const s = seconds % 60;
+ return `${m}:${s.toString().padStart(2, "0")}`;
+}
+
+export interface TurnSummary {
+ headline: string;
+ stats: string[];
+ accent: "ok" | "fail" | "qa";
+ glyph: string;
+ isFail: boolean;
+ isQA: boolean;
+ isStoppedWithDraft: boolean;
+}
+
+export function latestBlocksByLabel(blocks: BlockState[]): BlockState[] {
+ const latest = new Map<string, BlockState>();
+ for (const block of blocks) {
+ latest.set(block.label, block);
+ }
+ return Array.from(latest.values());
+}
+
+// Legacy prose heuristic, consulted only when the turn has no typed
+// adjudication (responseKind null).
+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)
+ );
+}
+
+interface AdjudicatedParts {
+ headline: string;
+ accent: TurnSummary["accent"];
+ glyph: string;
+}
+
+// Headline parts from the typed terminal adjudication. Returns null when the
+// turn predates the typed signal; a build kind without a verdict also falls
+// back to the legacy inference chain so genuinely-tested historical turns
+// never downgrade.
+function adjudicatedSummaryParts(
+ turn: TurnNarrativeState,
+ flags: {
+ needsUntestedProposalReview: boolean;
+ needsTestedProposalReview: boolean;
+ hasEdited: boolean;
+ hasDrafts: boolean;
+ },
+): AdjudicatedParts | null {
+ if (turn.responseKind === null) return null;
+ if (turn.responseKind !== "build") {
+ return {
+ headline:
+ turn.responseKind === "refuse"
+ ? "Declined"
+ : turn.responseKind === "diagnose"
+ ? "Answered"
+ : "Question",
+ accent: "qa",
+ glyph: "✦",
+ };
+ }
+ if (turn.verifiedSuccess === null) return null;
+ if (flags.needsUntestedProposalReview) {
+ return { headline: "Draft needs review", accent: "qa", glyph: "!" };
+ }
+ if (flags.needsTestedProposalReview) {
+ return { headline: "Workflow ready for review", accent: "qa", glyph: "!" };
+ }
+ if (!turn.verifiedSuccess) {
+ return { headline: "Stopped", accent: "qa", glyph: "!" };
+ }
+ if (flags.hasEdited) {
+ return {
+ headline: "Applied edits and re-tested",
+ accent: "ok",
+ glyph: "✓",
+ };
+ }
+ if (flags.hasDrafts) {
+ return {
+ headline: "Built and tested the workflow",
+ accent: "ok",
+ glyph: "✓",
+ };
+ }
+ return { headline: "Completed the run", accent: "ok", glyph: "✓" };
+}
+
+export function computeTurnSummary(turn: TurnNarrativeState): TurnSummary {
+ const rollupBlocks = latestBlocksByLabel(turn.blocks);
+ const isFail =
+ turn.terminal === "error" || rollupBlocks.some((b) => b.state === "failed");
+ const mode = effectiveMode(turn);
+ const needsInput = asksUserForInput(turn);
+ const isQA =
+ mode === "docs_answer" ||
+ mode === "diagnose" ||
+ mode === "clarify" ||
+ mode === "refuse";
+ const hasDrafts = (turn.draft?.blockCount ?? 0) > 0;
+ const needsUntestedProposalReview =
+ hasDrafts && turn.proposalDisposition === "review_untested";
+ const needsTestedProposalReview =
+ hasDrafts && turn.proposalDisposition === "review_tested";
+ const hasEdited = (turn.priorBlockCount ?? 0) > 0 && hasDrafts;
+ const hasReviewableDraft =
+ hasDrafts &&
+ (turn.proposalDisposition === "review_untested" ||
+ turn.proposalDisposition === "review_tested" ||
+ (turn.cancelled && turn.proposalDisposition !== "no_proposal"));
+ const isStoppedWithDraft = hasReviewableDraft && (isFail || turn.cancelled);
+
+ // Fail/cancel precedence is absolute: a verdict never upgrades a halt.
+ const adjudicated =
+ isStoppedWithDraft || isFail
+ ? null
+ : adjudicatedSummaryParts(turn, {
+ needsUntestedProposalReview,
+ needsTestedProposalReview,
+ hasEdited,
+ hasDrafts,
+ });
+
+ const headline = adjudicated
+ ? adjudicated.headline
+ : isStoppedWithDraft
+ ? "Stopped with a draft"
+ : isFail
+ ? "Run halted"
+ : needsInput
+ ? "Question"
+ : needsUntestedProposalReview
+ ? "Draft needs review"
+ : needsTestedProposalReview
+ ? "Workflow ready for review"
+ : isQA
+ ? mode === "refuse"
+ ? "Declined"
+ : 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);
+ if (turnElapsed) stats.push(turnElapsed);
+ if (!isQA) {
+ const ok = rollupBlocks.filter((b) => isBlockOk(b)).length;
+ const failed = rollupBlocks.filter((b) => b.state === "failed").length;
+ const newBlocks = hasEdited ? 0 : (turn.draft?.blockCount ?? 0);
+ if (ok) stats.push(`${ok} block${ok === 1 ? "" : "s"} ran`);
+ if (newBlocks) stats.push(`${newBlocks} new`);
+ if (failed) stats.push(`${failed} failed`);
+ }
+
+ const accent = adjudicated
+ ? adjudicated.accent
+ : isStoppedWithDraft
+ ? "qa"
+ : isFail
+ ? "fail"
+ : needsUntestedProposalReview || needsTestedProposalReview || isQA
+ ? "qa"
+ : "ok";
+ return {
+ headline,
+ stats,
+ accent,
+ glyph: adjudicated
+ ? adjudicated.glyph
+ : isStoppedWithDraft ||
+ needsUntestedProposalReview ||
+ needsTestedProposalReview
+ ? "!"
+ : isFail
+ ? "✕"
+ : isQA
+ ? "✦"
+ : "✓",
+ isFail,
+ isQA,
+ isStoppedWithDraft,
+ };
+}
diff --git a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts
index e31e40ce8..b2e34449c 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts
+++ b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts
@@ -53,6 +53,9 @@ export interface WorkflowCopilotChatHistoryMessage {
content: string;
audio_artifact_id?: string | null;
created_at: string;
+ // Typed turn outcome persisted on assistant rows; optional so the FE
+ // tolerates an older backend that does not serve it.
+ turn_outcome?: { response_kind?: string | null } | null;
narrative_payload?: Record<string, unknown> | null;
}