Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
feat(SKY-11973): copilot narrates studio runs happening next to it (#7233)
Celal Zamanoğlue9ef9529cac4
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.blockActions.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.blockActions.test.tsxindex 0c7d08f6e..bd5eb4d06 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.blockActions.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.blockActions.test.tsx@@ -99,9 +99,16 @@ vi.mock("react-router-dom", async (importOriginal) => {return {...actual,useParams: () => routeParams.current,+ useSearchParams: () => [new URLSearchParams(), vi.fn()],};});+// Unrelated to this file's tests; the real hook needs a QueryClientProvider+// this harness doesn't set up.+vi.mock("../hooks/useWorkflowRunQuery", () => ({+ useWorkflowRunQuery: () => ({ data: undefined }),+}));+const saveData = {title: "Test WF",workflow: {diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.composerMode.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.composerMode.test.tsxindex 9c4ba8074..660e83daa 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.composerMode.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.composerMode.test.tsx@@ -81,9 +81,14 @@ vi.mock("react-router-dom", async (importOriginal) => {workflowPermanentId: "wpid_1",workflowRunId: undefined,}),+ useSearchParams: () => [new URLSearchParams(), vi.fn()],};});+vi.mock("posthog-js/react", () => ({+ useFeatureFlagEnabled: () => true,+}));+const saveData = {title: "Test WF",workflow: {@@ -120,6 +125,12 @@ vi.mock("@/store/WorkflowHasChangesStore", () => ({useWorkflowHasChangesStore: () => ({ getSaveData: () => saveData }),}));+// Unrelated to this file's tests; the real hook needs a QueryClientProvider+// this harness doesn't set up.+vi.mock("@/routes/workflows/hooks/useWorkflowRunQuery", () => ({+ useWorkflowRunQuery: () => ({ data: undefined }),+}));+import { WorkflowCopilotChat } from "./WorkflowCopilotChat";type FlagConfig = {diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsxindex 164f6b389..0f4bef355 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx@@ -95,9 +95,14 @@ vi.mock("react-router-dom", async (importOriginal) => {workflowPermanentId: "wpid_1",workflowRunId: undefined,}),+ useSearchParams: () => [new URLSearchParams(), vi.fn()],};});+vi.mock("posthog-js/react", () => ({+ useFeatureFlagEnabled: () => true,+}));+const saveData = {title: "Test WF",workflow: {@@ -134,6 +139,12 @@ vi.mock("@/store/WorkflowHasChangesStore", () => ({useWorkflowHasChangesStore: () => ({ getSaveData: () => saveData }),}));+// Unrelated to this file's tests; the real hook needs a QueryClientProvider+// this harness doesn't set up.+vi.mock("@/routes/workflows/hooks/useWorkflowRunQuery", () => ({+ useWorkflowRunQuery: () => ({ data: undefined }),+}));+import { WorkflowCopilotChat } from "./WorkflowCopilotChat";const terminalResponse = (diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.runGrounding.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.runGrounding.test.tsxindex a4a9b7125..f1fe22ff2 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.runGrounding.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.runGrounding.test.tsx@@ -84,9 +84,14 @@ vi.mock("react-router-dom", async (importOriginal) => {return {...actual,useParams: () => routeParams.current,+ useSearchParams: () => [new URLSearchParams(), vi.fn()],};});+vi.mock("posthog-js/react", () => ({+ useFeatureFlagEnabled: () => true,+}));+const saveData = {title: "Test WF",workflow: {@@ -123,6 +128,12 @@ vi.mock("@/store/WorkflowHasChangesStore", () => ({useWorkflowHasChangesStore: () => ({ getSaveData: () => saveData }),}));+// Unrelated to this file's tests; the real hook needs a QueryClientProvider+// this harness doesn't set up.+vi.mock("@/routes/workflows/hooks/useWorkflowRunQuery", () => ({+ useWorkflowRunQuery: () => ({ data: undefined }),+}));+import { WorkflowCopilotChat } from "./WorkflowCopilotChat";const BOOLEAN_FLAGS: Record<string, boolean> = {diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.runLifecycle.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.runLifecycle.test.tsxnew file mode 100644index 000000000..ffc21a397--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.runLifecycle.test.tsx@@ -0,0 +1,397 @@+import {+ cleanup,+ fireEvent,+ render,+ screen,+ waitFor,+} from "@testing-library/react";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++import { Status } from "@/api/types";+import {+ FeatureFlagContext,+ FeatureFlagValueContext,+} from "@/hooks/useFeatureFlag";++type StreamBody = {+ message: string;+ workflow_run_id?: string | null;+ fix_origin?: boolean;+};+type StreamCall = {+ body: StreamBody;+ onMessage: (payload: unknown) => boolean;+ resolve: () => void;+ reject: (error: unknown) => void;+};++const {+ streamCalls,+ postStreaming,+ cancelPost,+ historyResponse,+ routeParams,+ workflowRunQueryMock,+ mockFeatureFlagEnabled,+} = vi.hoisted(() => {+ const calls: StreamCall[] = [];+ const post = vi.fn().mockResolvedValue({});+ const streaming = vi.fn(+ (+ _path: string,+ body: StreamBody,+ onMessage: (payload: unknown) => boolean,+ ) =>+ new Promise<void>((resolve, reject) => {+ calls.push({ body, onMessage, resolve, reject });+ }),+ );+ const history = {+ data: {+ workflow_copilot_chat_id: null as string | null,+ chat_history: [] as unknown[],+ proposed_workflow: null as Record<string, unknown> | null,+ auto_accept: false,+ },+ };+ const params = {+ current: {+ workflowPermanentId: "wpid_1",+ workflowRunId: undefined as string | undefined,+ },+ };+ return {+ streamCalls: calls,+ postStreaming: streaming,+ cancelPost: post,+ historyResponse: history,+ routeParams: params,+ workflowRunQueryMock: vi.fn(),+ mockFeatureFlagEnabled: vi.fn().mockReturnValue(true),+ };+});++vi.mock("posthog-js/react", () => ({+ useFeatureFlagEnabled: mockFeatureFlagEnabled,+}));++vi.mock("@/api/sse", () => ({+ getSseClient: vi.fn().mockResolvedValue({ postStreaming }),+}));++vi.mock("@/api/AxiosClient", () => ({+ getClient: vi.fn().mockResolvedValue({+ get: vi.fn().mockImplementation(() => Promise.resolve(historyResponse)),+ post: cancelPost,+ }),+}));++vi.mock("@/hooks/useCredentialGetter", () => ({+ useCredentialGetter: () => null,+}));++vi.mock("@/components/ui/use-toast", () => ({ toast: vi.fn() }));++vi.mock("react-router-dom", async (importOriginal) => {+ const actual = await importOriginal<typeof import("react-router-dom")>();+ return {+ ...actual,+ useParams: () => routeParams.current,+ useSearchParams: () => [new URLSearchParams(), vi.fn()],+ };+});++vi.mock("../hooks/useWorkflowRunQuery", () => ({+ useWorkflowRunQuery: (options?: { workflowRunId?: string }) =>+ workflowRunQueryMock(options),+}));++const saveData = {+ title: "Test WF",+ workflow: {+ workflow_id: "wf_1",+ workflow_permanent_id: "wpid_1",+ description: "",+ totp_verification_url: null,+ is_saved_task: false,+ status: "published",+ },+ settings: {+ proxyLocation: null,+ webhookCallbackUrl: null,+ persistBrowserSession: false,+ browserProfileId: null,+ browserProfileKey: null,+ model: null,+ maxScreenshotScrolls: null,+ extraHttpHeaders: null,+ runWith: "agent",+ scriptCacheKey: "",+ aiFallback: true,+ codeVersion: 2,+ runSequentially: false,+ sequentialKey: null,+ },+ parameters: [],+ blocks: [],+ workflowDefinitionVersion: 1,+};++vi.mock("@/store/WorkflowHasChangesStore", () => ({+ useWorkflowHasChangesStore: () => ({ getSaveData: () => saveData }),+}));++// Real WorkflowCopilotHistory needs an infinite-query + debounced Popover;+// a button standing in for "pick a different past chat" is enough here.+vi.mock("./WorkflowCopilotHistory", () => ({+ WorkflowCopilotHistory: ({+ onSelect,+ }: {+ onSelect: (chat: { workflow_copilot_chat_id: string }) => void;+ }) => (+ <button+ onClick={() => onSelect({ workflow_copilot_chat_id: "chat_other" })}+ >+ mock-select-history-chat+ </button>+ ),+}));++import { WorkflowCopilotChat } from "./WorkflowCopilotChat";++const BOOLEAN_FLAGS: Record<string, boolean> = {+ ENABLE_WORKFLOW_COPILOT_V2: true,+ WORKFLOW_COPILOT_CODE_BLOCK_MODE: false,+ CODE_BLOCK_ACCESS: false,+};++type ChatProps = {+ workflowRunId?: string | null;+ docked?: boolean;+ portalTarget?: HTMLElement | null;+};++function chatUi(props: ChatProps) {+ return (+ <FeatureFlagContext.Provider value={(name) => BOOLEAN_FLAGS[name]}>+ <FeatureFlagValueContext.Provider value={() => undefined}>+ <WorkflowCopilotChat {...props} />+ </FeatureFlagValueContext.Provider>+ </FeatureFlagContext.Provider>+ );+}++// docked renders via createPortal(content, portalTarget) and is null without+// one, so docked tests need a real, body-attached portal target to query into.+function makeDockedProps(props: ChatProps = {}): ChatProps {+ const portalTarget = document.createElement("div");+ document.body.appendChild(portalTarget);+ return { docked: true, portalTarget, ...props };+}++async function renderChat(props: ChatProps = {}) {+ const view = render(chatUi(props));+ await waitFor(() =>+ expect(screen.getByPlaceholderText(/Message Skyvern Copilot/)).toBeTruthy(),+ );+ return view;+}++function makeRun(overrides: Record<string, unknown> = {}) {+ return {+ workflow_run_id: "wr_1",+ status: Status.Running,+ created_at: "2026-01-01T00:00:00Z",+ started_at: null,+ finished_at: "2026-01-01T00:00:10Z",+ outputs: null,+ failure_reason: null,+ ...overrides,+ };+}++beforeEach(() => {+ HTMLElement.prototype.scrollIntoView = vi.fn();+ HTMLElement.prototype.scrollTo = vi.fn();+ streamCalls.length = 0;+ postStreaming.mockClear();+ cancelPost.mockClear();+ historyResponse.data = {+ workflow_copilot_chat_id: null,+ chat_history: [],+ proposed_workflow: null,+ auto_accept: false,+ };+ routeParams.current = {+ workflowPermanentId: "wpid_1",+ workflowRunId: undefined,+ };+ workflowRunQueryMock.mockReset();+ workflowRunQueryMock.mockReturnValue({ data: undefined });+ mockFeatureFlagEnabled.mockReturnValue(true);+});++afterEach(() => {+ cleanup();+});++describe("WorkflowCopilotChat — run lifecycle lines", () => {+ it("REGRESSION: a docked chat renders a lifecycle line when its focused run transitions running -> completed", async () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const props = makeDockedProps({ workflowRunId: "wr_1" });+ const view = await renderChat(props);++ await waitFor(() =>+ expect(screen.getByText("Run started — watching it now.")).toBeTruthy(),+ );++ workflowRunQueryMock.mockReturnValue({+ data: makeRun({+ status: Status.Completed,+ started_at: "2026-01-01T00:00:00Z",+ finished_at: "2026-01-01T00:00:42Z",+ }),+ });+ view.rerender(chatUi(props));++ await waitFor(() =>+ expect(screen.getByText(/Run completed in 0:42\./)).toBeTruthy(),+ );+ });++ it("keeps proposal actions attached to the last real ai message when a lifecycle entry trails it", async () => {+ historyResponse.data = {+ workflow_copilot_chat_id: "chat_1",+ chat_history: [+ {+ sender: "user",+ content: "build something",+ created_at: "2026-01-01T00:00:00Z",+ },+ {+ sender: "ai",+ content: "Here's a draft.",+ created_at: "2026-01-01T00:00:01Z",+ },+ ],+ proposed_workflow: saveData,+ auto_accept: false,+ };+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const props = makeDockedProps({ workflowRunId: "wr_1" });+ const view = await renderChat(props);++ await waitFor(() =>+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy(),+ );+ await waitFor(() =>+ expect(screen.getByText("Run started — watching it now.")).toBeTruthy(),+ );++ workflowRunQueryMock.mockReturnValue({+ data: makeRun({ status: Status.Completed }),+ });+ view.rerender(chatUi(props));++ await waitFor(() =>+ expect(screen.getByText(/Run completed in/)).toBeTruthy(),+ );+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ expect(screen.getByRole("button", { name: "Reject" })).toBeTruthy();+ });++ it("renders no lifecycle UI for a non-docked (legacy) chat even with a route-level run id and live data, and never queries it", async () => {+ routeParams.current = {+ workflowPermanentId: "wpid_1",+ workflowRunId: "wr_route",+ };+ workflowRunQueryMock.mockReturnValue({+ data: makeRun({ workflow_run_id: "wr_route" }),+ });+ await renderChat({ docked: false });++ // Give any (incorrect) announcement a chance to land before asserting absence.+ await new Promise((resolve) => setTimeout(resolve, 0));+ expect(screen.queryByText(/watching it now/)).toBeNull();++ // REGRESSION: a plain omitted workflowRunId used to still let+ // useWorkflowRunQuery fall back to the route's own :workflowRunId and poll it.+ expect(workflowRunQueryMock.mock.calls.length).toBeGreaterThan(0);+ for (const call of workflowRunQueryMock.mock.calls) {+ expect(call[0]).toEqual({ workflowRunId: undefined, enabled: false });+ }+ });++ it("renders no lifecycle line when copilot_ux_v1 is off, even for a docked chat with a live run", async () => {+ mockFeatureFlagEnabled.mockReturnValue(false);+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const props = makeDockedProps({ workflowRunId: "wr_1" });+ const view = await renderChat(props);++ // Give any (incorrect) announcement a chance to land before asserting absence.+ await new Promise((resolve) => setTimeout(resolve, 0));+ expect(screen.queryByText(/watching it now/)).toBeNull();++ workflowRunQueryMock.mockReturnValue({+ data: makeRun({+ status: Status.Completed,+ started_at: "2026-01-01T00:00:00Z",+ finished_at: "2026-01-01T00:00:42Z",+ }),+ });+ view.rerender(chatUi(props));++ await new Promise((resolve) => setTimeout(resolve, 0));+ expect(screen.queryByText(/Run completed in/)).toBeNull();++ // Flag off must genuinely disable the run query, not just hide its output —+ // otherwise a docked-off/flag-off chat still polls the run in the background.+ expect(workflowRunQueryMock.mock.calls.length).toBeGreaterThan(0);+ for (const call of workflowRunQueryMock.mock.calls) {+ expect(call[0]).toEqual({ workflowRunId: undefined, enabled: false });+ }+ });++ it("New chat wipes a rendered lifecycle line", async () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ await renderChat(makeDockedProps({ workflowRunId: "wr_1" }));++ await waitFor(() =>+ expect(screen.getByText("Run started — watching it now.")).toBeTruthy(),+ );++ fireEvent.click(screen.getByRole("button", { name: /New chat/i }));++ expect(screen.queryByText(/watching it now/)).toBeNull();+ });++ it("REGRESSION: switching to a different past chat drops the lifecycle line instead of carrying it forward", async () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ await renderChat(makeDockedProps({ workflowRunId: "wr_1" }));++ await waitFor(() =>+ expect(screen.getByText("Run started — watching it now.")).toBeTruthy(),+ );++ historyResponse.data = {+ workflow_copilot_chat_id: "chat_other",+ chat_history: [+ {+ sender: "ai",+ content: "An old unrelated reply.",+ created_at: "2025-01-01T00:00:00Z",+ },+ ],+ proposed_workflow: null,+ auto_accept: false,+ };+ fireEvent.click(+ screen.getByRole("button", { name: "mock-select-history-chat" }),+ );++ await waitFor(() =>+ expect(screen.getByText("An old unrelated reply.")).toBeTruthy(),+ );+ expect(screen.queryByText(/watching it now/)).toBeNull();+ });+});diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.scroll.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.scroll.test.tsxindex 1cd7703b0..e7d8c6a2b 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.scroll.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.scroll.test.tsx@@ -52,13 +52,24 @@ vi.mock("react-router-dom", async (importOriginal) => {workflowPermanentId: "wpid_1",workflowRunId: undefined,}),+ useSearchParams: () => [new URLSearchParams(), vi.fn()],};});+vi.mock("posthog-js/react", () => ({+ useFeatureFlagEnabled: () => true,+}));+vi.mock("@/store/WorkflowHasChangesStore", () => ({useWorkflowHasChangesStore: () => ({ getSaveData: () => ({}) }),}));+// Unrelated to this file's tests; the real hook needs a QueryClientProvider+// this harness doesn't set up.+vi.mock("@/routes/workflows/hooks/useWorkflowRunQuery", () => ({+ useWorkflowRunQuery: () => ({ data: undefined }),+}));+import { WorkflowCopilotChat } from "./WorkflowCopilotChat";async function renderChat() {diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsxindex 527adb901..1349b4126 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx@@ -63,6 +63,7 @@ import {} from "./sendQueue";import { shouldAutoApplyWorkflowResponse } from "./proposalDisposition";import { NarrativeView } from "./NarrativeView";+import { useRunLifecycleAnnouncements } from "./useRunLifecycleAnnouncements";import { DiffCard, shouldShowDiffCard } from "./cards/DiffCard";import { FixCard, shouldShowFixCard } from "./cards/FixCard";import {@@ -80,14 +81,14 @@ import { useSpeechToTextField } from "@/hooks/useSpeechToTextField";import { SpeechInputButton } from "@/components/SpeechInputButton";import { useFeatureFlag, useFeatureFlagValue } from "@/hooks/useFeatureFlag";import { useFeatureFlagEnabled } from "posthog-js/react";-import { COPILOT_UX_V1_FLAG } from "@/util/featureFlags";import {DropdownMenu,DropdownMenuTrigger,DropdownMenuContent,DropdownMenuItem,} from "@/components/ui/dropdown-menu";-import { cn } from "@/util/utils";+import { cn, formatElapsedSeconds } from "@/util/utils";+import { COPILOT_UX_V1_FLAG } from "@/util/featureFlags";// Cap on retained per-turn snap-back snapshots. A typical session has a// handful of turns; this ceiling guards a runaway long-running chat.@@ -185,13 +186,6 @@ function defaultCodeBlockRequestOverride(return null;}-function formatElapsedSeconds(ms: number): string {- const seconds = Math.max(0, Math.round(ms / 1000));- const m = Math.floor(seconds / 60);- const s = seconds % 60;- return `${m}:${s.toString().padStart(2, "0")}`;-}-// Ask's mark is a text dingbat; Build's is a color emoji that ModeGlyph flattens// to a tone-adaptive monochrome silhouette so both read flat on the dark UI.const ASK_GLYPH = "\u275D\uFE0E";@@ -299,7 +293,7 @@ function ConvoAggregatePill({);}-interface ChatMessage {+export interface ChatMessage {id: string;sender: WorkflowCopilotChatSender;content: string;@@ -308,6 +302,8 @@ interface ChatMessage {// so the per-block cards persist as the user scrolls back through past// turns. Live in-flight narrative is rendered separately at the bottom.narrative?: TurnNarrativeState;+ // FE-synthetic run status line (never persisted, never sent to the LLM).+ kind?: "run_lifecycle";}const getLatestDiffCardTurnId = (messages: ChatMessage[]): string | null => {@@ -320,6 +316,17 @@ const getLatestDiffCardTurnId = (messages: ChatMessage[]): string | null => {return null;};+// messages.length - 1 with any trailing run_lifecycle lines skipped, so+// proposal actions / FixCard keep attaching to the last real turn.+const findLastTurnIndex = (messages: ChatMessage[]): number => {+ for (let index = messages.length - 1; index >= 0; index -= 1) {+ if (messages[index]?.kind !== "run_lifecycle") {+ return index;+ }+ }+ return -1;+};+type QueuedPrompt = {id: string;content: string;@@ -394,6 +401,23 @@ const MessageItem = memo(({ message, footer }: MessageItemProps) => {);});+// Studio-only run status line, distinct from ai prose: no bubble, no footer.+function RunLifecycleLine({ content }: { content: string }) {+ return (+ <div+ className="flex items-center gap-2 pl-1 text-xs text-muted-foreground"+ role="status"+ aria-live="polite"+ >+ <span+ aria-hidden="true"+ className="inline-block h-1.5 w-1.5 shrink-0 rounded-full bg-slate-500"+ />+ <span>{content}</span>+ </div>+ );+}+// `persisted` true = atomic accept (server already wrote new version); false/undefined = local edit.// `applied` marks a turn's accepted terminal apply; drafts and snap-backs omit it.export type WorkflowUpdateOptions = {@@ -671,6 +695,18 @@ export function WorkflowCopilotChat({// The studio focuses a run via ?wr= (not a path param), so the route param is// empty there; an explicit prop grounds the chat in that run and wins.const workflowRunId = workflowRunIdProp ?? routeWorkflowRunId;+ const announceRunLifecycle = useCallback((message: ChatMessage) => {+ setMessages((prev) =>+ prev.some((existing) => existing.id === message.id)+ ? prev+ : [...prev, message],+ );+ }, []);+ useRunLifecycleAnnouncements({+ workflowRunId: docked && copilotUxV1Enabled ? workflowRunId : undefined,+ turnInFlightRef: inFlightRef,+ announce: announceRunLifecycle,+ });// Recorded actions arrive well after block_progress (they're persisted in// batch at block end), so fetch them once a run reaches adjudication// instead of waiting on the narrower block_progress/tool-call cadence.@@ -854,7 +890,10 @@ export function WorkflowCopilotChat({};const applyHistoryResponse = useCallback(- (data: WorkflowCopilotChatHistoryResponse) => {+ (+ data: WorkflowCopilotChatHistoryResponse,+ carryForwardLifecycle = true,+ ) => {const historyMessages = data.chat_history.map((message, index) => ({id: `${index}-${Date.now()}`,sender: message.sender,@@ -882,7 +921,14 @@ export function WorkflowCopilotChat({? getLatestDiffCardTurnId(historyMessages): null;latestTurnId.current = pendingProposalTurnId;- setMessages(historyMessages);+ // History never carries run_lifecycle lines (local-only); carry them+ // forward only for the mount-race caller, not an explicit chat switch.+ setMessages((prev) => [+ ...historyMessages,+ ...(carryForwardLifecycle+ ? prev.filter((message) => message.kind === "run_lifecycle")+ : []),+ ]);setWorkflowCopilotChatId(data.workflow_copilot_chat_id);setProposedWorkflow(data.proposed_workflow ?? null);setAutoAccept(data.auto_accept ?? false);@@ -913,7 +959,7 @@ export function WorkflowCopilotChat({},},);- applyHistoryResponse(response.data);+ applyHistoryResponse(response.data, false);// Mark history loaded for this workflow so the mount effect won't reload// the latest chat over the one the user just selected.historyLoadedForRef.current = workflowPermanentId;@@ -2233,6 +2279,7 @@ export function WorkflowCopilotChat({? `Listening… · ${browserStatusText}`: "Listening…": browserStatusText;+ const lastTurnIndex = findLastTurnIndex(messages);const content = (<div@@ -2338,9 +2385,17 @@ export function WorkflowCopilotChat({}/>{messages.map((message, index) => {- const isLastMessage = index === messages.length - 1;+ const isLastMessage = index === lastTurnIndex;const showQueuedFooter =isQueuedPromptWaiting && message.id === queuedPrompt?.id;+ if (message.kind === "run_lifecycle") {+ return (+ <RunLifecycleLine+ key={message.id}+ content={message.content}+ />+ );+ }// Per-message frozen narrative. When an AI message carries a// frozen narrative, render the narrative card stack in place// of the legacy text bubble so the per-block cards survivediff --git a/skyvern-frontend/src/routes/workflows/copilot/useRunLifecycleAnnouncements.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/useRunLifecycleAnnouncements.test.tsxnew file mode 100644index 000000000..e6aad8fa5--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/copilot/useRunLifecycleAnnouncements.test.tsx@@ -0,0 +1,260 @@+// @vitest-environment jsdom++import { renderHook } from "@testing-library/react";+import { MemoryRouter } from "react-router-dom";+import { beforeEach, describe, expect, it, vi } from "vitest";++import { Status } from "@/api/types";++const workflowRunQueryMock = vi.hoisted(() => vi.fn());++vi.mock("../hooks/useWorkflowRunQuery", () => ({+ useWorkflowRunQuery: (options?: { workflowRunId?: string }) =>+ workflowRunQueryMock(options),+}));++import { useRunLifecycleAnnouncements } from "./useRunLifecycleAnnouncements";++type RunFixture = {+ workflow_run_id: string;+ status: Status;+ created_at: string;+ started_at: string | null;+ finished_at: string;+ outputs: Record<string, unknown> | null;+ failure_reason: string | null;+};++function makeRun(overrides: Partial<RunFixture> = {}): RunFixture {+ return {+ workflow_run_id: "wr_1",+ status: Status.Running,+ created_at: "2026-01-01T00:00:00Z",+ started_at: null,+ finished_at: "2026-01-01T00:00:10Z",+ outputs: null,+ failure_reason: null,+ ...overrides,+ };+}++function renderLifecycle(initial: {+ workflowRunId: string | undefined;+ inFlight?: boolean;+ search?: string;+}) {+ const turnInFlightRef = { current: initial.inFlight ?? false };+ const announce = vi.fn();+ const view = renderHook(+ ({ workflowRunId }: { workflowRunId: string | undefined }) =>+ useRunLifecycleAnnouncements({+ workflowRunId,+ turnInFlightRef,+ announce,+ }),+ {+ initialProps: { workflowRunId: initial.workflowRunId },+ wrapper: ({ children }) => (+ <MemoryRouter initialEntries={[`/?${initial.search ?? ""}`]}>+ {children}+ </MemoryRouter>+ ),+ },+ );+ return { ...view, announce, turnInFlightRef };+}++beforeEach(() => {+ workflowRunQueryMock.mockReset();+});++describe("useRunLifecycleAnnouncements", () => {+ it("announces start once on first non-finalized observation; a same-data rerender does not double-announce", () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const { announce, rerender } = renderLifecycle({ workflowRunId: "wr_1" });++ expect(announce).toHaveBeenCalledTimes(1);+ expect(announce.mock.calls[0]![0]).toMatchObject({+ id: "run-lifecycle-wr_1-start",+ kind: "run_lifecycle",+ content: "Run started — watching it now.",+ });++ rerender({ workflowRunId: "wr_1" });+ expect(announce).toHaveBeenCalledTimes(1);+ });++ it("announces a terminal message with duration and extracted count on running -> completed", () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const { announce, rerender } = renderLifecycle({ workflowRunId: "wr_1" });+ expect(announce).toHaveBeenCalledTimes(1);++ workflowRunQueryMock.mockReturnValue({+ data: makeRun({+ status: Status.Completed,+ started_at: "2026-01-01T00:00:00Z",+ finished_at: "2026-01-01T00:01:05Z",+ outputs: { extracted_information: [1, 2, 3, 4, 5] },+ }),+ });+ rerender({ workflowRunId: "wr_1" });++ expect(announce).toHaveBeenCalledTimes(2);+ expect(announce.mock.calls[1]![0]).toMatchObject({+ id: "run-lifecycle-wr_1-terminal",+ content:+ "Run completed in 1:05 — extracted 5 item(s). Want to review or change anything?",+ });+ });++ it("counts a single named array wrapping the extracted output (object-shaped extracted_information)", () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const { announce, rerender } = renderLifecycle({ workflowRunId: "wr_1" });+ expect(announce).toHaveBeenCalledTimes(1);++ workflowRunQueryMock.mockReturnValue({+ data: makeRun({+ status: Status.Completed,+ outputs: { extracted_information: { rows: [1, 2, 3] } },+ }),+ });+ rerender({ workflowRunId: "wr_1" });++ expect(announce.mock.calls[1]![0].content).toContain("extracted 3 item(s)");+ });++ it("omits the count when extracted_information has zero or multiple array-valued keys", () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const { announce, rerender } = renderLifecycle({ workflowRunId: "wr_1" });+ expect(announce).toHaveBeenCalledTimes(1);++ workflowRunQueryMock.mockReturnValue({+ data: makeRun({+ status: Status.Completed,+ outputs: { extracted_information: { a: [1], b: [2] } },+ }),+ });+ rerender({ workflowRunId: "wr_1" });++ expect(announce.mock.calls[1]![0].content).toBe(+ "Run completed in 0:10. Want to review or change anything?",+ );+ });++ it("stays permanently silent when a run is first observed already finalized", () => {+ workflowRunQueryMock.mockReturnValue({+ data: makeRun({ status: Status.Completed }),+ });+ const { announce, rerender } = renderLifecycle({ workflowRunId: "wr_1" });+ expect(announce).not.toHaveBeenCalled();++ rerender({ workflowRunId: "wr_1" });+ expect(announce).not.toHaveBeenCalled();+ });++ it("announces a failed-run terminal message with a truncated reason", () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const { announce, rerender } = renderLifecycle({ workflowRunId: "wr_1" });+ expect(announce).toHaveBeenCalledTimes(1);++ const longReason = "x".repeat(250);+ workflowRunQueryMock.mockReturnValue({+ data: makeRun({+ status: Status.Failed,+ started_at: "2026-01-01T00:00:00Z",+ finished_at: "2026-01-01T00:00:30Z",+ failure_reason: longReason,+ }),+ });+ rerender({ workflowRunId: "wr_1" });++ expect(announce).toHaveBeenCalledTimes(2);+ const message = announce.mock.calls[1]![0].content as string;+ expect(message.startsWith("Run failed after 0:30 — ")).toBe(true);+ expect(message).toContain(longReason.slice(0, 200));+ expect(message).not.toContain(longReason.slice(0, 201));+ expect(message.endsWith("Ask me to diagnose and fix it.")).toBe(true);+ });++ it("announces a canceled-run terminal message", () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const { announce, rerender } = renderLifecycle({ workflowRunId: "wr_1" });+ expect(announce).toHaveBeenCalledTimes(1);++ workflowRunQueryMock.mockReturnValue({+ data: makeRun({ status: Status.Canceled }),+ });+ rerender({ workflowRunId: "wr_1" });++ expect(announce).toHaveBeenCalledTimes(2);+ expect(announce.mock.calls[1]![0].content).toBe("Run canceled.");+ });++ it("ignores a stale placeholder payload still keyed to the previous run id (keepPreviousData trap)", () => {+ // wr_a observed already-finalized on first sight: permanently silent.+ workflowRunQueryMock.mockReturnValue({+ data: makeRun({ workflow_run_id: "wr_a", status: Status.Completed }),+ });+ const { announce, rerender } = renderLifecycle({ workflowRunId: "wr_a" });+ expect(announce).not.toHaveBeenCalled();++ // Studio focus moves to wr_b, but keepPreviousData is still serving wr_a's+ // payload as a placeholder while the new query is in flight — the hook+ // input changed, the payload's own workflow_run_id did not.+ rerender({ workflowRunId: "wr_b" });+ expect(announce).not.toHaveBeenCalled();+ });++ it("stays permanently silent for a run first observed while a copilot turn is in flight", () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const { announce, rerender } = renderLifecycle({+ workflowRunId: "wr_1",+ inFlight: true,+ });+ expect(announce).not.toHaveBeenCalled();++ workflowRunQueryMock.mockReturnValue({+ data: makeRun({ status: Status.Completed }),+ });+ rerender({ workflowRunId: "wr_1" });+ expect(announce).not.toHaveBeenCalled();+ });++ it("does nothing when workflowRunId is undefined, and disables the query instead of letting it fall back to the route", () => {+ workflowRunQueryMock.mockReturnValue({ data: undefined });+ const { announce } = renderLifecycle({ workflowRunId: undefined });++ expect(announce).not.toHaveBeenCalled();+ expect(workflowRunQueryMock).toHaveBeenCalledWith({+ workflowRunId: undefined,+ enabled: false,+ });+ });++ it("uses the joined copy when started_at is more than 15s old", () => {+ workflowRunQueryMock.mockReturnValue({+ data: makeRun({+ started_at: new Date(Date.now() - 20_000).toISOString(),+ }),+ });+ const { announce } = renderLifecycle({ workflowRunId: "wr_1" });++ expect(announce).toHaveBeenCalledTimes(1);+ expect(announce.mock.calls[0]![0].content).toBe(+ "Run in progress — watching it now.",+ );+ });++ it("uses the block-run copy variant when ?bl= is present", () => {+ workflowRunQueryMock.mockReturnValue({ data: makeRun() });+ const { announce } = renderLifecycle({+ workflowRunId: "wr_1",+ search: "bl=block_1",+ });++ expect(announce).toHaveBeenCalledTimes(1);+ expect(announce.mock.calls[0]![0].content).toBe(+ "Block run started — watching it now.",+ );+ });+});diff --git a/skyvern-frontend/src/routes/workflows/copilot/useRunLifecycleAnnouncements.ts b/skyvern-frontend/src/routes/workflows/copilot/useRunLifecycleAnnouncements.tsnew file mode 100644index 000000000..d82800c7e--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/copilot/useRunLifecycleAnnouncements.ts@@ -0,0 +1,153 @@+import { useEffect, useRef } from "react";+import { useSearchParams } from "react-router-dom";++import { Status, WorkflowRunStatusApiResponse } from "@/api/types";+import { statusIsAFailureType, statusIsFinalized } from "@/routes/tasks/types";+import { formatElapsedSeconds, isRecord } from "@/util/utils";++import { useWorkflowRunQuery } from "../hooks/useWorkflowRunQuery";+import type { ChatMessage } from "./WorkflowCopilotChat";++type RunLifecycleMessage = ChatMessage & { kind: "run_lifecycle" };++const START_JOIN_THRESHOLD_MS = 15_000;+const FAILURE_REASON_MAX_CHARS = 200;++function elapsedMs(startIso: string | null, endIso: string | null): number {+ const start = startIso ? Date.parse(startIso) : NaN;+ const end = endIso ? Date.parse(endIso) : NaN;+ if (Number.isNaN(start) || Number.isNaN(end)) {+ return 0;+ }+ return end - start;+}++function extractedCount(+ outputs: Record<string, unknown> | null,+): number | undefined {+ if (!outputs || !("extracted_information" in outputs)) {+ return undefined;+ }+ const info = outputs.extracted_information;+ if (Array.isArray(info)) {+ return info.length;+ }+ if (isRecord(info)) {+ const arrays = Object.values(info).filter(Array.isArray);+ return arrays.length === 1 ? arrays[0]?.length : undefined;+ }+ return undefined;+}++function truncate(value: string, max: number): string {+ return value.length > max ? `${value.slice(0, max)}…` : value;+}++function failureVerb(status: Status): string {+ if (status === Status.TimedOut) return "timed out";+ if (status === Status.Terminated) return "was terminated";+ return "failed";+}++function buildStartMessage(+ id: string,+ data: WorkflowRunStatusApiResponse,+ isBlockRun: boolean,+): RunLifecycleMessage {+ const age = data.started_at ? Date.now() - Date.parse(data.started_at) : 0;+ const joined = age > START_JOIN_THRESHOLD_MS;+ const content = isBlockRun+ ? joined+ ? "Block run in progress…"+ : "Block run started — watching it now."+ : joined+ ? "Run in progress — watching it now."+ : "Run started — watching it now.";+ return {+ id: `run-lifecycle-${id}-start`,+ sender: "ai",+ kind: "run_lifecycle",+ content,+ };+}++function buildTerminalMessage(+ id: string,+ data: WorkflowRunStatusApiResponse,+): RunLifecycleMessage {+ const dur = formatElapsedSeconds(+ elapsedMs(data.started_at ?? data.created_at, data.finished_at),+ );+ let content: string;+ if (data.status === Status.Canceled) {+ content = "Run canceled.";+ } else if (statusIsAFailureType({ status: data.status })) {+ const reason = data.failure_reason+ ? ` — ${truncate(data.failure_reason, FAILURE_REASON_MAX_CHARS)}`+ : "";+ content = `Run ${failureVerb(data.status)} after ${dur}${reason}. Ask me to diagnose and fix it.`;+ } else {+ const count = extractedCount(data.outputs);+ const extracted = count ? ` — extracted ${count} item(s)` : "";+ content = `Run completed in ${dur}${extracted}. Want to review or change anything?`;+ }+ return {+ id: `run-lifecycle-${id}-terminal`,+ sender: "ai",+ kind: "run_lifecycle",+ content,+ };+}++type SeenEntry = { start: boolean; terminal: boolean };++/**+ * Watches the studio's focused run and appends presentational lifecycle lines+ * into the chat's own message list. Never persisted, never sent to the LLM —+ * grounding for the LLM already travels as workflow_run_id on each turn.+ */+export function useRunLifecycleAnnouncements({+ workflowRunId,+ turnInFlightRef,+ announce,+}: {+ workflowRunId: string | undefined;+ turnInFlightRef: { current: boolean };+ announce: (message: RunLifecycleMessage) => void;+}): void {+ // enabled: false (not just an omitted workflowRunId) stops useWorkflowRunQuery+ // from falling back to the route's own :workflowRunId and polling a run this+ // chat renders no line for.+ const { data } = useWorkflowRunQuery({+ workflowRunId,+ enabled: workflowRunId !== undefined,+ });+ const seen = useRef(new Map<string, SeenEntry>());+ const [searchParams] = useSearchParams();+ const isBlockRun = searchParams.get("bl") !== null;++ useEffect(() => {+ // Disabling the query above doesn't clear data left over from a prior+ // enabled fetch, so still gate announcing on our own current input.+ if (!workflowRunId || !data) {+ return;+ }+ const id = data.workflow_run_id;+ let entry = seen.current.get(id);+ if (!entry) {+ const finalized = statusIsFinalized({ status: data.status });+ const silent = finalized || turnInFlightRef.current;+ entry = { start: silent, terminal: silent };+ seen.current.set(id, entry);+ if (!silent) {+ entry.start = true;+ announce(buildStartMessage(id, data, isBlockRun));+ }+ return;+ }+ if (!entry.terminal && statusIsFinalized({ status: data.status })) {+ entry.terminal = true;+ announce(buildTerminalMessage(id, data));+ }+ }, [workflowRunId, data, announce, isBlockRun, turnInFlightRef]);+}diff --git a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunQuery.ts b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunQuery.tsindex e73c236ad..53d14dd87 100644--- a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunQuery.ts+++ b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunQuery.ts@@ -15,7 +15,10 @@ import {useActiveOrgId,} from "@/store/ActiveOrgContext";-function useWorkflowRunQuery(options?: { workflowRunId?: string }) {+function useWorkflowRunQuery(options?: {+ workflowRunId?: string;+ enabled?: boolean;+}) {const urlWorkflowRunId = useFirstParam("workflowRunId", "runId");const workflowRunId = options?.workflowRunId ?? urlWorkflowRunId;const { workflowPermanentId } = useParams();@@ -69,7 +72,11 @@ function useWorkflowRunQuery(options?: { workflowRunId?: string }) {}return statusIsRunningOrQueued(query.state.data);},- enabled: !!globalWorkflows && !!workflowPermanentId && !!workflowRunId,+ enabled:+ (options?.enabled ?? true) &&+ !!globalWorkflows &&+ !!workflowPermanentId &&+ !!workflowRunId,});}diff --git a/skyvern-frontend/src/util/utils.ts b/skyvern-frontend/src/util/utils.tsindex 14c59457d..525f958d2 100644--- a/skyvern-frontend/src/util/utils.ts+++ b/skyvern-frontend/src/util/utils.ts@@ -9,6 +9,13 @@ export function isRecord(value: unknown): value is Record<string, unknown> {return typeof value === "object" && value !== null && !Array.isArray(value);}+export function formatElapsedSeconds(ms: number): string {+ const seconds = Math.max(0, Math.round(ms / 1000));+ const m = Math.floor(seconds / 60);+ const s = seconds % 60;+ return `${m}:${s.toString().padStart(2, "0")}`;+}+const clampToZero = (n: number) => Math.max(n, 0);export const formatMs = (elapsed: number) => {