Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Delete the unused drafting-gap timer and phase-checklist code from the copilot chat (SKY-13002) (#8574)
Aaron Perezbfb2a8312266
diff --git a/skyvern-frontend/src/routes/workflows/copilot/AutoAcceptChip.tsx b/skyvern-frontend/src/routes/workflows/copilot/AutoAcceptChip.tsxnew file mode 100644index 000000000..9ba398e36--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/copilot/AutoAcceptChip.tsx@@ -0,0 +1,71 @@+import { useState } from "react";++import { getClient } from "@/api/AxiosClient";+import { toast } from "@/components/ui/use-toast";+import { useCredentialGetter } from "@/hooks/useCredentialGetter";++type Props = {+ chatId: string;+ waitForAccept: (chatId: string) => Promise<void>;+ // This chat's Turn off is already running, including one started before this chip mounted.+ pendingFromChat: boolean;+ onPendingChange: (chatId: string, pending: boolean) => void;+ onTurnedOff: (chatId: string) => void;+};++export function AutoAcceptChip({+ chatId,+ waitForAccept,+ pendingFromChat,+ onPendingChange,+ onTurnedOff,+}: Props) {+ const credentialGetter = useCredentialGetter();+ const [pending, setPending] = useState(false);+ const busy = pending || pendingFromChat;++ const turnOff = async () => {+ if (busy) {+ return;+ }+ setPending(true);+ onPendingChange(chatId, true);+ try {+ await waitForAccept(chatId);+ const client = await getClient(credentialGetter, "sans-api-v1");+ await client.post("/workflow/copilot/disable-auto-accept", {+ workflow_copilot_chat_id: chatId,+ });+ onTurnedOff(chatId);+ } catch (error) {+ console.error("Failed to turn off auto-accept:", error);+ toast({+ title: "Auto-accept is still on",+ description: "Could not turn it off. Please try again.",+ variant: "destructive",+ });+ } finally {+ setPending(false);+ onPendingChange(chatId, false);+ }+ };++ return (+ <button+ type="button"+ onClick={turnOff}+ // Not `disabled`: that drops keyboard focus, and a failed request would leave the user nowhere.+ aria-disabled={busy}+ className="flex min-w-0 items-center gap-1.5 rounded-sm text-muted-foreground hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring aria-disabled:opacity-50"+ >+ <span+ className="h-1.5 w-1.5 shrink-0 rounded-full bg-emerald-400"+ aria-hidden="true"+ />+ <span className="truncate">Auto-accepting</span>+ <span className="shrink-0 text-foreground underline underline-offset-2">+ Turn off+ </span>+ </button>+ );+}diff --git a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.ts b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.tsindex 2c1b0a193..37d8eddfc 100644--- a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.ts@@ -1,6 +1,5 @@import { describe, expect, it } from "vitest";-import { derivePhases } from "./copilotPhases";import {ActivityEntry,EMPTY_NARRATIVE,@@ -1412,14 +1411,6 @@ describe("a real cancel's backend payload still renders neutrally", () => {expect(turn?.blocks[0]?.state).toBe("stopped");});- it("does not redden the rail for a run the user stopped", () => {- const turn = hydrateNarrativeFromPayload(cancelledPayload())!;- const phases = derivePhases(turn);- const byId = Object.fromEntries(phases.map((p) => [p.id, p.status]));- expect(byId.done).toBe("stopped");- expect(byId.test).toBe("stopped");- });-it("keeps a genuine error turn's block failed when it was not cancelled", () => {const turn = hydrateNarrativeFromPayload({...cancelledPayload(),diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.historyRace.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.historyRace.test.tsxindex 706aba9be..8d51d54de 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.historyRace.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.historyRace.test.tsx@@ -178,11 +178,16 @@ vi.mock("./WorkflowCopilotHistory", () => ({}: {onSelect: (chat: { workflow_copilot_chat_id: string }) => void;}) => (- <button- onClick={() => onSelect({ workflow_copilot_chat_id: "chat_other" })}- >- mock-select-history-chat- </button>+ <>+ <button+ onClick={() => onSelect({ workflow_copilot_chat_id: "chat_other" })}+ >+ mock-select-history-chat+ </button>+ <button onClick={() => onSelect({ workflow_copilot_chat_id: "chat-1" })}>+ mock-select-history-chat-1+ </button>+ </>),}));@@ -372,6 +377,419 @@ describe("WorkflowCopilotChat — history-race action-card gating (item 1)", ()});});+describe("WorkflowCopilotChat — auto-accept Turn off across a chat switch", () => {+ it.each([+ { action: "Turn off", button: /Auto-accepting/ },+ { action: "Reject", button: "Reject" },+ ])(+ "leaves the new chat's auto-accept and pending review alone when the old chat's $action lands late",+ async ({ button }) => {+ await renderChat();+ await flushHistory(+ historyData({+ auto_accept: true,+ proposed_workflow: {+ workflow_id: "wf_pending",+ title: "Pending draft",+ _copilot_unvalidated: true,+ },+ chat_history: [aiHistoryMessage(null, "Here is a draft.")],+ }),+ );+ let finishRequest: (value: unknown) => void = () => {};+ cancelPost.mockImplementationOnce(+ () => new Promise((resolve) => (finishRequest = resolve)),+ );+ await act(async () => {+ fireEvent.click(await screen.findByRole("button", { name: button }));+ });++ await act(async () => {+ fireEvent.click(screen.getByText("mock-select-history-chat"));+ await Promise.resolve();+ });+ await flushHistory(+ historyData({+ workflow_copilot_chat_id: "chat_other",+ auto_accept: true,+ proposed_workflow: {+ workflow_id: "wf_other_pending",+ title: "Other chat draft",+ _copilot_unvalidated: true,+ },+ chat_history: [aiHistoryMessage(null, "Other chat draft.")],+ }),+ );+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ await act(async () => {+ finishRequest({});+ await Promise.resolve();+ });++ expect(cancelPost).toHaveBeenCalledWith(+ expect.stringMatching(+ /\/workflow\/copilot\/(disable-auto-accept|clear-proposed-workflow)/,+ ),+ expect.objectContaining({ workflow_copilot_chat_id: "chat-1" }),+ );+ expect(+ screen.getByRole("button", { name: /Auto-accepting/ }),+ ).toBeTruthy();+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ },+ );++ it("keeps a chat's Accept withheld while its own Turn off is still pending, after another chat's Turn off starts", async () => {+ const pendingChat = (chatId: string) =>+ historyData({+ workflow_copilot_chat_id: chatId,+ auto_accept: true,+ proposed_workflow: {+ workflow_id: `wf_${chatId}`,+ title: "Pending draft",+ _copilot_unvalidated: true,+ },+ chat_history: [aiHistoryMessage(null, "Here is a draft.")],+ });+ const heldDisables: Array<(value: unknown) => void> = [];+ cancelPost.mockImplementation((path: string) =>+ path === "/workflow/copilot/disable-auto-accept"+ ? new Promise((resolve) => heldDisables.push(resolve))+ : Promise.resolve({}),+ );+ await renderChat();+ await flushHistory(pendingChat("chat-1"));+ await act(async () => {+ fireEvent.click(+ await screen.findByRole("button", { name: /Auto-accepting/ }),+ );+ });+ expect(screen.queryByRole("button", { name: "Always accept" })).toBeNull();++ // A second chat's Turn off starts while the first one is still in flight.+ await act(async () => {+ fireEvent.click(screen.getByText("mock-select-history-chat"));+ await Promise.resolve();+ });+ await flushHistory(pendingChat("chat_other"));+ await act(async () => {+ fireEvent.click(+ await screen.findByRole("button", { name: /Auto-accepting/ }),+ );+ });++ // Back to the first chat, whose disable has still not answered.+ await act(async () => {+ fireEvent.click(screen.getByText("mock-select-history-chat-1"));+ await Promise.resolve();+ });+ await flushHistory(pendingChat("chat-1"));++ expect(heldDisables).toHaveLength(2);+ expect(screen.queryByRole("button", { name: "Always accept" })).toBeNull();+ expect(screen.queryByRole("button", { name: "Accept" })).toBeNull();+ expect(screen.getByRole("button", { name: "Reject" })).toBeTruthy();+ });++ it("shows the chat's Turn off still pending after a round trip to another chat, and refuses a second one", async () => {+ const pendingChat = (chatId: string) =>+ historyData({+ workflow_copilot_chat_id: chatId,+ auto_accept: true,+ proposed_workflow: {+ workflow_id: `wf_${chatId}`,+ title: "Pending draft",+ _copilot_unvalidated: true,+ },+ chat_history: [aiHistoryMessage(null, "Here is a draft.")],+ });+ const heldDisables: Array<(value: unknown) => void> = [];+ cancelPost.mockImplementation((path: string) =>+ path === "/workflow/copilot/disable-auto-accept"+ ? new Promise((resolve) => heldDisables.push(resolve))+ : Promise.resolve({}),+ );+ await renderChat();+ await flushHistory(pendingChat("chat-1"));+ await act(async () => {+ fireEvent.click(+ await screen.findByRole("button", { name: /Auto-accepting/ }),+ );+ });+ expect(heldDisables).toHaveLength(1);++ // Away and back while chat-1's disable is still in flight: the chip remounts.+ await act(async () => {+ fireEvent.click(screen.getByText("mock-select-history-chat"));+ await Promise.resolve();+ });+ await flushHistory(pendingChat("chat_other"));+ await act(async () => {+ fireEvent.click(screen.getByText("mock-select-history-chat-1"));+ await Promise.resolve();+ });+ await flushHistory(pendingChat("chat-1"));++ const chip = await screen.findByRole("button", {+ name: /Auto-accepting/,+ });+ expect(chip.getAttribute("aria-disabled")).toBe("true");+ await act(async () => {+ fireEvent.click(chip);+ });+ expect(heldDisables).toHaveLength(1);+ expect(screen.queryByRole("button", { name: "Always accept" })).toBeNull();++ await act(async () => {+ heldDisables[0]!({});+ });+ await waitFor(() =>+ expect(+ screen.queryByRole("button", { name: /Auto-accepting/ }),+ ).toBeNull(),+ );+ expect(screen.getByRole("button", { name: "Always accept" })).toBeTruthy();+ });++ it("does not clear the new chat's pending review when the old chat's Accept lands late", async () => {+ const pendingChat = (chatId: string) =>+ historyData({+ workflow_copilot_chat_id: chatId,+ auto_accept: false,+ proposed_workflow: {+ workflow_id: `wf_${chatId}`,+ title: "Pending draft",+ _copilot_unvalidated: true,+ },+ chat_history: [aiHistoryMessage(null, "Here is a draft.")],+ });+ let finishApply: (value: unknown) => void = () => {};+ cancelPost.mockImplementation((path: string) =>+ path === "/workflow/copilot/apply-proposed-workflow"+ ? new Promise((resolve) => (finishApply = resolve))+ : Promise.resolve({}),+ );+ await renderChat();+ await flushHistory(pendingChat("chat-1"));+ await act(async () => {+ fireEvent.click(await screen.findByRole("button", { name: "Accept" }));+ });++ await act(async () => {+ fireEvent.click(screen.getByText("mock-select-history-chat"));+ await Promise.resolve();+ });+ await flushHistory(pendingChat("chat_other"));+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();++ await act(async () => {+ finishApply({ data: { workflow_id: "wf_chat-1" } });+ });++ // chat_other's proposal is untouched on the server, so its gate must survive.+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ expect(screen.getByText("Proposed changes")).toBeTruthy();+ });++ it("clears the accepted chat's server proposal even when the user switched away", async () => {+ const pendingChat = (chatId: string) =>+ historyData({+ workflow_copilot_chat_id: chatId,+ auto_accept: false,+ proposed_workflow: {+ workflow_id: `wf_${chatId}`,+ title: "Pending draft",+ _copilot_unvalidated: true,+ },+ chat_history: [aiHistoryMessage(null, "Here is a draft.")],+ });+ // The atomic apply fails, so Accept falls back to applying locally and clearing the row itself.+ let failApply: (reason: unknown) => void = () => {};+ cancelPost.mockImplementation((path: string) =>+ path === "/workflow/copilot/apply-proposed-workflow"+ ? new Promise((_resolve, reject) => (failApply = reject))+ : Promise.resolve({}),+ );+ await renderChat();+ await flushHistory(pendingChat("chat-1"));+ await act(async () => {+ fireEvent.click(await screen.findByRole("button", { name: "Accept" }));+ });++ await act(async () => {+ fireEvent.click(screen.getByText("mock-select-history-chat"));+ await Promise.resolve();+ });+ await flushHistory(pendingChat("chat_other"));++ await act(async () => {+ failApply({ response: { status: 500 } });+ await Promise.resolve();+ });++ await waitFor(() =>+ expect(cancelPost).toHaveBeenCalledWith(+ "/workflow/copilot/clear-proposed-workflow",+ expect.objectContaining({ workflow_copilot_chat_id: "chat-1" }),+ ),+ );+ // The chat now on screen keeps its own pending review.+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ });++ it("does not retry an accepted chat's clear against the chat the user switched to", async () => {+ const pendingChat = (chatId: string) =>+ historyData({+ workflow_copilot_chat_id: chatId,+ auto_accept: false,+ proposed_workflow: {+ workflow_id: `wf_${chatId}`,+ title: "Pending draft",+ _copilot_unvalidated: true,+ },+ chat_history: [aiHistoryMessage(null, "Here is a draft.")],+ });+ let failApply: (reason: unknown) => void = () => {};+ const clearBodies: Array<Record<string, unknown>> = [];+ cancelPost.mockImplementation(+ (path: string, body: Record<string, unknown>) => {+ if (path === "/workflow/copilot/apply-proposed-workflow") {+ return new Promise((_resolve, reject) => (failApply = reject));+ }+ if (path === "/workflow/copilot/clear-proposed-workflow") {+ clearBodies.push(body);+ // The accepted chat's row is gone by the time the fallback clear lands.+ return Promise.reject({ response: { status: 404 } });+ }+ return Promise.resolve({});+ },+ );+ await renderChat();+ await flushHistory(pendingChat("chat-1"));+ await act(async () => {+ fireEvent.click(await screen.findByRole("button", { name: "Accept" }));+ });++ await act(async () => {+ fireEvent.click(screen.getByText("mock-select-history-chat"));+ await Promise.resolve();+ });+ await flushHistory(pendingChat("chat_other"));++ await act(async () => {+ failApply({ response: { status: 500 } });+ await Promise.resolve();+ });+ await waitFor(() => expect(clearBodies.length).toBeGreaterThan(0));+ // The 404 branch looks up the latest chat; answer it with the chat now on screen.+ if (historyQueue.length > 0) {+ await flushHistory(pendingChat("chat_other"));+ }++ // The retry must not fall back to whatever chat is current: that would delete its pending review.+ expect(clearBodies.map((body) => body.workflow_copilot_chat_id)).toEqual([+ "chat-1",+ ]);+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ });++ it("does not hold one chat's Turn off behind an Accept still running in another chat", async () => {+ await renderChat();+ await flushHistory(+ historyData({+ auto_accept: true,+ proposed_workflow: {+ workflow_id: "wf_pending",+ title: "Pending draft",+ _copilot_unvalidated: true,+ },+ chat_history: [aiHistoryMessage(null, "Here is a draft.")],+ }),+ );+ cancelPost.mockImplementation((path: string) =>+ path === "/workflow/copilot/apply-proposed-workflow"+ ? new Promise(() => {})+ : Promise.resolve({}),+ );+ await act(async () => {+ fireEvent.click(await screen.findByRole("button", { name: "Accept" }));+ });++ await act(async () => {+ fireEvent.click(screen.getByText("mock-select-history-chat"));+ await Promise.resolve();+ });+ await flushHistory(+ historyData({+ workflow_copilot_chat_id: "chat_other",+ auto_accept: true,+ }),+ );+ await act(async () => {+ fireEvent.click(+ await screen.findByRole("button", { name: /Auto-accepting/ }),+ );+ });++ await waitFor(() =>+ expect(cancelPost).toHaveBeenCalledWith(+ "/workflow/copilot/disable-auto-accept",+ { workflow_copilot_chat_id: "chat_other" },+ ),+ );+ await waitFor(() =>+ expect(+ screen.queryByRole("button", { name: /Auto-accepting/ }),+ ).toBeNull(),+ );+ });++ it("does not let a late post-Accept read of the old chat overwrite the chat the user switched to", async () => {+ await renderChat();+ await flushHistory(+ historyData({+ auto_accept: true,+ proposed_workflow: {+ workflow_id: "wf_pending",+ title: "Pending draft",+ _copilot_unvalidated: true,+ },+ chat_history: [aiHistoryMessage(null, "Here is a draft.")],+ }),+ );+ await act(async () => {+ fireEvent.click(await screen.findByRole("button", { name: "Accept" }));+ });+ // A plain Accept reads chat-1's row back; that read is still open when the user switches.+ await waitFor(() => expect(historyQueue.length).toBe(1));+ const lateChatOneRead = historyQueue.shift()!;+ historyRejects.shift();++ await act(async () => {+ fireEvent.click(screen.getByText("mock-select-history-chat"));+ await Promise.resolve();+ });+ await flushHistory(+ historyData({+ workflow_copilot_chat_id: "chat_other",+ auto_accept: true,+ }),+ );+ await act(async () => {+ lateChatOneRead({+ data: historyData({+ auto_accept: false,+ proposed_workflow: { workflow_id: "wf_chat_one", title: "Chat one" },+ }),+ });+ await Promise.resolve();+ });++ expect(screen.getByRole("button", { name: /Auto-accepting/ })).toBeTruthy();+ expect(screen.queryByRole("button", { name: "Accept" })).toBeNull();+ });+});+// Item 2 (SKY-12384): a live_browser prompt queued before the initial history// load must keep one owner — footer while the bubble exists, else the chip.describe("WorkflowCopilotChat — queued prompt survives initial history load (item 2)", () => {@@ -549,6 +967,34 @@ describe("WorkflowCopilotChat — recovery poll after a non-terminal stream clos});}+ it("does not let a recovery read that started before Turn off turn the chip back on", async () => {+ await renderChat();+ await flushHistory(historyData({ auto_accept: true }));+ await submit("build me a flow");+ await waitFor(() => expect(streamCalls.length).toBe(1));+ await emitTurnStart();+ vi.useFakeTimers();+ await closeStreamWithoutTerminal();+ await advance(2_000);+ expect(historyQueue.length).toBe(1);+ const stalePoll = historyQueue.shift()!;+ historyRejects.shift();++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: /Auto-accepting/ }));+ });+ await advance(0);+ expect(screen.queryByRole("button", { name: /Auto-accepting/ })).toBeNull();++ await act(async () => {+ stalePoll({ data: { ...recoveredHistory(), auto_accept: true } });+ await Promise.resolve();+ });++ expect(renderedText()).toContain(capturedAiText);+ expect(screen.queryByRole("button", { name: /Auto-accepting/ })).toBeNull();+ });+it("renders the assistant row the server persisted, replacing the error bubble", async () => {await startTurn();await closeStreamWithoutTerminal();diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.reviewGate.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.reviewGate.test.tsxindex 5aff3a242..3e00961e0 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.reviewGate.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.reviewGate.test.tsx@@ -57,7 +57,7 @@ const { streamCalls, postStreaming, cancelPost, historyGet, historyResponse } =failure_reason: string | null;outputs: Array<{ output_parameter_id: string; value: unknown }>;} | null,- auto_accept: false,+ auto_accept: false as boolean,},};const get = vi.fn().mockImplementation(() => Promise.resolve(history));@@ -149,7 +149,12 @@ vi.mock("@/routes/workflows/hooks/useWorkflowRunQuery", () => ({useWorkflowRunQuery: () => ({ data: undefined }),}));-import { WorkflowCopilotChat } from "./WorkflowCopilotChat";+import { toast } from "@/components/ui/use-toast";++import {+ ACCEPT_SETTLE_CEILING_MS,+ WorkflowCopilotChat,+} from "./WorkflowCopilotChat";async function renderChat(props: { docked?: boolean } = {}) {// docked renders via a portal; without a target it intentionally renders null.@@ -486,7 +491,9 @@ describe("WorkflowCopilotChat — g2 review gate", () => {expect(streamCalls[1]!.body.keep_pending_proposal).toBe(true);// Mid-flight: the gate's own actions are not accessible while loading.expect(screen.queryByRole("button", { name: "Accept" })).toBeNull();- expect(screen.getByText("1 proposal pending · Review")).toBeTruthy();+ expect(+ screen.getByRole("button", { name: /1 proposal pending/ }),+ ).toBeTruthy();// Turn 2 ends with no new proposal; resync picks the row back up.await act(async () => {@@ -502,7 +509,7 @@ describe("WorkflowCopilotChat — g2 review gate", () => {);// The gate's owning turn is still not the last message, so the chip// (and its jump-back affordance) stays up even once actions re-enable.- const chip = screen.getByText("1 proposal pending · Review");+ const chip = screen.getByRole("button", { name: /1 proposal pending/ });await act(async () => {fireEvent.click(chip);});@@ -519,6 +526,518 @@ describe("WorkflowCopilotChat — g2 review gate", () => {);});+ it("shows auto-accept in the composer and Turn off mid-turn makes that turn's verified fix wait for review", async () => {+ historyResponse.data.auto_accept = true;+ await renderChat();+ const chip = await screen.findByRole("button", {+ name: /Auto-accepting/,+ });++ // The turn starts while auto-accept is still on.+ await submit("fix the selector");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));++ cancelPost.mockRejectedValueOnce({ response: { status: 500 } });+ await act(async () => {+ fireEvent.click(chip);+ });+ // A failed write leaves the server auto-accepting, so the chip must not claim otherwise.+ expect(screen.getByRole("button", { name: /Auto-accepting/ })).toBeTruthy();++ let finishTurnOff: (value: unknown) => void = () => {};+ cancelPost.mockImplementationOnce(+ () => new Promise((resolve) => (finishTurnOff = resolve)),+ );+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: /Auto-accepting/ }));+ });+ expect(cancelPost).toHaveBeenCalledWith(+ "/workflow/copilot/disable-auto-accept",+ { workflow_copilot_chat_id: "chat-1" },+ );+ historyResponse.data.auto_accept = false;++ // The server committed the write and finished the turn before the browser saw the POST resolve.+ await act(async () => {+ streamCalls[0]!.onMessage(+ proposalResponse("Fixed and verified.", {+ proposal_disposition: "auto_applicable",+ workflow_applied: false,+ narrative_payload: proposalNarrativePayload({+ proposalDisposition: "auto_applicable",+ }),+ }),+ );+ streamCalls[0]!.resolve();+ });+ // The gate offers no actions while Turn off is pending; the draft must still be waiting when it finishes.+ await act(async () => {+ finishTurnOff({});+ });+ await waitFor(() =>+ expect(+ screen.queryByRole("button", { name: /Auto-accepting/ }),+ ).toBeNull(),+ );+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ });++ it.each([+ { rowKept: false, chipAfterAccept: "hidden" },+ // Apply writes auto-accept best-effort after creating the version, so the row can keep it on.+ { rowKept: true, chipAfterAccept: "shown" },+ ])(+ "after a plain Accept the chip follows the chat row (row kept auto_accept=$rowKept: $chipAfterAccept)",+ async ({ rowKept }) => {+ await renderChat();+ expect(+ screen.queryByRole("button", { name: /Auto-accepting/ }),+ ).toBeNull();++ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ historyResponse.data.auto_accept = true;+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Always accept" }));+ });+ expect(+ await screen.findByRole("button", { name: /Auto-accepting/ }),+ ).toBeTruthy();++ // Auto-accept never covers an untested draft, so this one still gets a gate.+ await submit("add a step");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ await act(async () => {+ streamCalls[1]!.onMessage(+ proposalResponse("Untested draft.", {+ turn_id: "turn-2",+ narrative_payload: proposalNarrativePayload({+ turnId: "turn-2",+ turnIndex: 1,+ }),+ }),+ );+ streamCalls[1]!.resolve();+ });+ historyResponse.data.auto_accept = rowKept;+ await act(async () => {+ fireEvent.click(await screen.findByRole("button", { name: "Accept" }));+ });++ expect(cancelPost).toHaveBeenLastCalledWith(+ "/workflow/copilot/apply-proposed-workflow",+ expect.objectContaining({ auto_accept: false }),+ );+ await waitFor(() =>+ expect(+ Boolean(screen.queryByRole("button", { name: /Auto-accepting/ })),+ ).toBe(rowKept),+ );+ },+ );++ it("sends Turn off only after every in-flight Always accept settles, so auto-accept ends off", async () => {+ historyResponse.data.auto_accept = true;+ await renderChat();+ await submit("add a step");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Untested draft."));+ streamCalls[0]!.resolve();+ });+ // The chat row takes each request's auto_accept when that request completes, as the routes do.+ const finishApplies: Array<(value: unknown) => void> = [];+ cancelPost.mockImplementation((path: string) => {+ if (path === "/workflow/copilot/apply-proposed-workflow") {+ return new Promise((resolve) => {+ finishApplies.push((value) => {+ historyResponse.data.auto_accept = true;+ resolve(value);+ });+ });+ }+ if (path === "/workflow/copilot/disable-auto-accept") {+ historyResponse.data.auto_accept = false;+ }+ return Promise.resolve({});+ });+ const requestedPaths = () => cancelPost.mock.calls.map(([path]) => path);++ // A double click starts two applies; the second can come back first.+ await act(async () => {+ fireEvent.click(+ await screen.findByRole("button", { name: "Always accept" }),+ );+ });+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Always accept" }));+ });+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: /Auto-accepting/ }));+ });+ expect(finishApplies).toHaveLength(2);+ await act(async () => {+ finishApplies[1]!({});+ });+ expect(requestedPaths()).not.toContain(+ "/workflow/copilot/disable-auto-accept",+ );++ await act(async () => {+ finishApplies[0]!({});+ });+ await waitFor(() =>+ expect(requestedPaths()).toContain(+ "/workflow/copilot/disable-auto-accept",+ ),+ );+ expect(+ requestedPaths().lastIndexOf("/workflow/copilot/apply-proposed-workflow"),+ ).toBeLessThan(+ requestedPaths().indexOf("/workflow/copilot/disable-auto-accept"),+ );+ await waitFor(() =>+ expect(+ screen.queryByRole("button", { name: /Auto-accepting/ }),+ ).toBeNull(),+ );+ expect(historyResponse.data.auto_accept).toBe(false);+ });++ it("offers no Accept while Turn off is pending, so no Accept can start after it", async () => {+ historyResponse.data.auto_accept = true;+ await renderChat();+ await submit("add a step");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Untested draft."));+ streamCalls[0]!.resolve();+ });+ expect(+ await screen.findByRole("button", { name: "Always accept" }),+ ).toBeTruthy();+ let finishTurnOff: (value: unknown) => void = () => {};+ cancelPost.mockImplementation((path: string) =>+ path === "/workflow/copilot/disable-auto-accept"+ ? new Promise((resolve) => (finishTurnOff = resolve))+ : Promise.resolve({}),+ );++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: /Auto-accepting/ }));+ });++ expect(screen.queryByRole("button", { name: "Always accept" })).toBeNull();+ expect(screen.queryByRole("button", { name: "Accept" })).toBeNull();+ // Neither writes auto_accept, so the user keeps them while Turn off runs.+ expect(screen.getByRole("button", { name: "Reject" })).toBeTruthy();+ expect(screen.getByRole("button", { name: "Review" })).toBeTruthy();++ await act(async () => {+ finishTurnOff({});+ });+ await waitFor(() =>+ expect(+ screen.queryByRole("button", { name: /Auto-accepting/ }),+ ).toBeNull(),+ );+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ });++ it("gives the gate its Accept back when an Accept never settles, instead of waiting forever", async () => {+ historyResponse.data.auto_accept = true;+ await renderChat();+ await submit("add a step");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Untested draft."));+ streamCalls[0]!.resolve();+ });+ // An apply that never answers: without a ceiling the chat's gate would stay without Accept.+ cancelPost.mockImplementation((path: string) =>+ path === "/workflow/copilot/apply-proposed-workflow"+ ? new Promise(() => {})+ : Promise.resolve({}),+ );+ await act(async () => {+ fireEvent.click(+ await screen.findByRole("button", { name: "Always accept" }),+ );+ });+ vi.useFakeTimers();+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: /Auto-accepting/ }));+ });+ expect(screen.queryByRole("button", { name: "Always accept" })).toBeNull();++ await act(async () => {+ await vi.advanceTimersByTimeAsync(ACCEPT_SETTLE_CEILING_MS + 1_000);+ });+ vi.useRealTimers();++ expect(+ await screen.findByRole("button", { name: "Always accept" }),+ ).toBeTruthy();+ // The disable never went out, so the chip still reports the row's state.+ expect(cancelPost.mock.calls.map(([path]) => path)).not.toContain(+ "/workflow/copilot/disable-auto-accept",+ );+ expect(screen.getByRole("button", { name: /Auto-accepting/ })).toBeTruthy();+ expect(toast).toHaveBeenCalledWith(+ expect.objectContaining({ variant: "destructive" }),+ );+ });++ it("does not leak an Always accept into a New chat opened before it lands", async () => {+ await renderChat();+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ let finishApply: (value: unknown) => void = () => {};+ cancelPost.mockImplementation((path: string) =>+ path === "/workflow/copilot/apply-proposed-workflow"+ ? new Promise((resolve) => (finishApply = resolve))+ : Promise.resolve({}),+ );+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Always accept" }));+ });++ // New chat clears the chat and its auto-accept while that apply is still in flight.+ historyResponse.data.auto_accept = false;+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "New chat" }));+ });+ await act(async () => {+ finishApply({ data: { workflow_id: "wf_proposed" } });+ });++ // The apply landed for a chat the user already left: it must not arm the blank chat.+ expect(screen.queryByRole("button", { name: /Auto-accepting/ })).toBeNull();++ // And it stays off once the blank chat's own turn assigns it a chat id to read back.+ await submit("start over");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ await act(async () => {+ streamCalls[1]!.onMessage(+ proposalResponse("Renamed the blocks.", {+ turn_id: "turn-2",+ }),+ );+ streamCalls[1]!.resolve();+ });++ expect(await screen.findByRole("button", { name: "Accept" })).toBeTruthy();+ expect(screen.queryByRole("button", { name: /Auto-accepting/ })).toBeNull();+ });++ it("ignores a read that started before a plain Accept turned auto-accept off", async () => {+ historyResponse.data.auto_accept = true;+ await renderChat();+ await submit("add a step");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Untested draft."));+ streamCalls[0]!.resolve();+ });+ expect(+ await screen.findByRole("button", { name: /Auto-accepting/ }),+ ).toBeTruthy();++ // A conflicted Accept leaves a row read in flight, taken while the row still said true.+ let finishStaleRead: (value: unknown) => void = () => {};+ historyGet.mockImplementationOnce(+ () => new Promise((resolve) => (finishStaleRead = resolve)),+ );+ cancelPost.mockRejectedValueOnce({ response: { status: 409 } });+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });++ // The retry succeeds: a plain Accept writes auto_accept=false on the row.+ historyResponse.data.auto_accept = false;+ await act(async () => {+ fireEvent.click(await screen.findByRole("button", { name: "Accept" }));+ });+ await waitFor(() =>+ expect(+ screen.queryByRole("button", { name: /Auto-accepting/ }),+ ).toBeNull(),+ );++ await act(async () => {+ finishStaleRead({ data: { ...historyResponse.data, auto_accept: true } });+ });++ expect(screen.queryByRole("button", { name: /Auto-accepting/ })).toBeNull();+ });++ it("keeps Accept withheld until every Turn off for the chat settles, not just the first", async () => {+ historyResponse.data.auto_accept = true;+ await renderChat();+ await submit("add a step");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Untested draft."));+ streamCalls[0]!.resolve();+ });+ const heldDisables: Array<(value: unknown) => void> = [];+ cancelPost.mockImplementation((path: string) =>+ path === "/workflow/copilot/disable-auto-accept"+ ? new Promise((resolve) => heldDisables.push(resolve))+ : Promise.resolve({}),+ );++ // Two clicks in one tick, before React re-renders: both start, as a double click does.+ const chip = await screen.findByRole("button", {+ name: /Auto-accepting/,+ });+ await act(async () => {+ fireEvent.click(chip);+ fireEvent.click(chip);+ });+ expect(heldDisables).toHaveLength(2);+ expect(screen.queryByRole("button", { name: "Always accept" })).toBeNull();++ // The first settles while the second is still in flight: the gate must stay withheld.+ await act(async () => {+ heldDisables[0]!({});+ });+ expect(screen.queryByRole("button", { name: "Always accept" })).toBeNull();+ expect(screen.queryByRole("button", { name: "Accept" })).toBeNull();++ await act(async () => {+ heldDisables[1]!({});+ });+ await waitFor(() =>+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy(),+ );+ });++ it("gives the gate its Accept back when Turn off fails", async () => {+ historyResponse.data.auto_accept = true;+ await renderChat();+ await submit("add a step");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Untested draft."));+ streamCalls[0]!.resolve();+ });+ let failTurnOff: (reason: unknown) => void = () => {};+ cancelPost.mockImplementation((path: string) =>+ path === "/workflow/copilot/disable-auto-accept"+ ? new Promise((_resolve, reject) => (failTurnOff = reject))+ : Promise.resolve({}),+ );++ await act(async () => {+ fireEvent.click(+ await screen.findByRole("button", { name: /Auto-accepting/ }),+ );+ });+ expect(screen.queryByRole("button", { name: "Always accept" })).toBeNull();++ await act(async () => {+ failTurnOff({ response: { status: 500 } });+ });++ // The request failed, so auto-accept is still on and the gate must be usable again.+ expect(+ await screen.findByRole("button", { name: "Always accept" }),+ ).toBeTruthy();+ expect(screen.getByRole("button", { name: /Auto-accepting/ })).toBeTruthy();+ });++ it("keeps Turn off behind the fallback accept's row write", async () => {+ historyResponse.data.auto_accept = true;+ await renderChat();+ await submit("add a step");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ // A v1 proposal carries no metadata, so a failed apply falls back to the client-side apply,+ // and that path writes the row itself instead of letting the apply route do it.+ streamCalls[0]!.onMessage(legacyProposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ let finishRowWrite: (value: unknown) => void = () => {};+ cancelPost.mockImplementation((path: string) => {+ if (path === "/workflow/copilot/clear-proposed-workflow") {+ return new Promise((resolve) => (finishRowWrite = resolve));+ }+ if (path === "/workflow/copilot/apply-proposed-workflow") {+ return Promise.reject({ response: { status: 500 } });+ }+ return Promise.resolve({});+ });+ await act(async () => {+ fireEvent.click(+ await screen.findByRole("button", { name: "Always accept" }),+ );+ });++ // That write ends with auto_accept=true, so a Turn off sent first is silently overwritten.+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: /Auto-accepting/ }));+ });+ expect(cancelPost.mock.calls.map(([path]) => path)).not.toContain(+ "/workflow/copilot/disable-auto-accept",+ );++ await act(async () => {+ finishRowWrite({ data: {} });+ });+ await waitFor(() =>+ expect(cancelPost.mock.calls.map(([path]) => path)).toContain(+ "/workflow/copilot/disable-auto-accept",+ ),+ );+ await waitFor(() =>+ expect(+ screen.queryByRole("button", { name: /Auto-accepting/ }),+ ).toBeNull(),+ );+ });++ it("ignores a read-back that started before Turn off finished", async () => {+ historyResponse.data.auto_accept = true;+ await renderChat();+ await submit("add a step");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Untested draft."));+ streamCalls[0]!.resolve();+ });+ let finishStaleRead: (value: unknown) => void = () => {};+ historyGet.mockImplementationOnce(+ () => new Promise((resolve) => (finishStaleRead = resolve)),+ );+ // A plain Accept reads the row back; that read captures auto_accept=true before Turn off lands.+ await act(async () => {+ fireEvent.click(await screen.findByRole("button", { name: "Accept" }));+ });+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: /Auto-accepting/ }));+ });+ await waitFor(() =>+ expect(+ screen.queryByRole("button", { name: /Auto-accepting/ }),+ ).toBeNull(),+ );++ await act(async () => {+ finishStaleRead({ data: { ...historyResponse.data, auto_accept: true } });+ });++ expect(screen.queryByRole("button", { name: /Auto-accepting/ })).toBeNull();+ });+it("shows the Untested pill on hydration of an old payload lacking proposalDisposition", async () => {historyResponse.data.proposed_workflow = proposedWorkflowPayload({_copilot_unvalidated: true,@@ -561,7 +1080,9 @@ describe("WorkflowCopilotChat — g2 review gate", () => {expect(screen.queryByText("Removed")).toBeNull();// No narrative turn id to derive an owning turn from — the chip would be// a dead no-op affordance here, not a working jump-back.- expect(screen.queryByText("1 proposal pending · Review")).toBeNull();+ expect(+ screen.queryByRole("button", { name: /1 proposal pending/ }),+ ).toBeNull();});it("clears a stale pending gate when a later turn is auto-applied (stale Accept can't reapply over the newer canvas)", async () => {@@ -601,7 +1122,9 @@ describe("WorkflowCopilotChat — g2 review gate", () => {await waitFor(() =>expect(screen.queryByRole("button", { name: "Accept" })).toBeNull(),);- expect(screen.queryByText("1 proposal pending · Review")).toBeNull();+ expect(+ screen.queryByRole("button", { name: /1 proposal pending/ }),+ ).toBeNull();});it("shows the gate live on a narrative-less turn, not only after a reload (SKY-14099)", async () => {diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.test.ts b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.test.tsindex 9cabe18b4..189c2c728 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.test.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.test.ts@@ -16,28 +16,34 @@ const response = (});describe("shouldAutoApplyWorkflowResponse", () => {- it("auto-applies auto_applicable proposals when auto accept is enabled", () => {- expect(shouldAutoApplyWorkflowResponse(response(), true, false)).toBe(true);- });-- it("auto-applies proposals already committed by the backend", () => {+ it("auto-applies only what the backend says it applied", () => {expect(shouldAutoApplyWorkflowResponse(response({ workflow_applied: true }),false,- false,),).toBe(true);});- it("keeps a verified fix pending when auto accept is off and the backend did not apply it", () => {- // A verified fix the backend left pending (workflow_applied=false) must NOT- // auto-apply when auto accept is off — it lands as a pending proposal for- // the review gate.+ it("keeps a verified fix pending when the backend did not apply it", () => {+ // The server owns this: Turn off can commit there before the browser sees its request resolve.expect(shouldAutoApplyWorkflowResponse(response({ workflow_applied: false }),false,+ ),+ ).toBe(false);+ });++ it("does not auto-apply a frame that carries no workflow_applied at all", () => {+ // Every frame the backend emits carries the field; without it there is no authority to apply.+ const frame = response() as unknown as Record<string, unknown>;+ delete frame.workflow_applied;+ expect(+ shouldAutoApplyWorkflowResponse(+ frame as unknown as Parameters<+ typeof shouldAutoApplyWorkflowResponse+ >[0],false,),).toBe(false);@@ -49,7 +55,6 @@ describe("shouldAutoApplyWorkflowResponse", () => {expect(shouldAutoApplyWorkflowResponse(response({ proposal_disposition }),- true,false,),).toBe(false);@@ -60,7 +65,6 @@ describe("shouldAutoApplyWorkflowResponse", () => {expect(shouldAutoApplyWorkflowResponse(response({ proposal_disposition: "no_proposal" }),- true,false,),).toBe(false);@@ -68,12 +72,8 @@ describe("shouldAutoApplyWorkflowResponse", () => {it("does not auto-apply cancelled turns", () => {expect(- shouldAutoApplyWorkflowResponse(- response({ cancelled: true }),- true,- false,- ),+ shouldAutoApplyWorkflowResponse(response({ cancelled: true }), false),).toBe(false);- expect(shouldAutoApplyWorkflowResponse(response(), true, true)).toBe(false);+ expect(shouldAutoApplyWorkflowResponse(response(), true)).toBe(false);});});diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsxindex a58a31d50..1b9a4b67c 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx@@ -79,6 +79,7 @@ import {CopilotProductAction,} from "./workflowCopilotTypes";import { WorkflowCopilotHistory } from "./WorkflowCopilotHistory";+import { AutoAcceptChip } from "./AutoAcceptChip";import { SelectedBlockChip } from "./SelectedBlockChip";import { readSelectedBlockLabel } from "./selectedBlockLabel";import { selectAutoBoundReceiptIndexes } from "./autoBoundReceiptIndexes";@@ -89,7 +90,6 @@ import {resolveSendAction,} from "./sendQueue";import { shouldAutoApplyWorkflowResponse } from "./proposalDisposition";-import { shouldArmDraftingGapTimer } from "./copilotPhases";import { InstantAckPlaceholder, NarrativeView } from "./NarrativeView";import { CopilotMarkdown } from "./CopilotMarkdown";import { CopilotWorkingStatus } from "./CopilotWorkingStatus";@@ -165,6 +165,30 @@ const MAX_TURN_SNAPSHOTS = 20;// A stream that closes with no terminal frame is usually a lost client// connection while the server handler runs on to persist the real reply, so the// ladder is sized to the server's own turn budget rather than to a short wait.+// How long Turn off waits for a chat's in-flight Accepts before giving up and reporting failure, so an+// apply that never answers cannot leave that chat's gate without its Accept actions. Accept p95 is ~11s.+export const ACCEPT_SETTLE_CEILING_MS = 30_000;++async function settledWithin(+ promise: Promise<unknown>,+ ms: number,+): Promise<boolean> {+ let timer: ReturnType<typeof setTimeout> | undefined;+ try {+ return await Promise.race([+ promise.then(+ () => true,+ () => true,+ ),+ new Promise<boolean>((resolve) => {+ timer = setTimeout(() => resolve(false), Math.max(0, ms));+ }),+ ]);+ } finally {+ clearTimeout(timer);+ }+}+const RECOVERY_POLL_DELAYS_MS = [2_000, 3_000, 5_000, 8_000, 12_000, 20_000];const RECOVERY_POLL_STEADY_MS = 30_000;// The server's RECONCILE_ABANDON_AFTER_SECONDS is 1_320_000ms. The margin holds@@ -1073,6 +1097,29 @@ export function WorkflowCopilotChat({new Set(),);const [autoAccept, setAutoAccept] = useState<boolean>(false);+ // A running turn's stream handler reads this, so Turn off reaches the turn already in flight.+ const autoAcceptRef = useRef(autoAccept);+ useEffect(() => {+ autoAcceptRef.current = autoAccept;+ }, [autoAccept]);+ // Counts the user's own auto-accept writes, so a chat-row read that started before one cannot undo it.+ const autoAcceptWrites = useRef(0);+ // Accepts still running, per chat. Each apply writes its chat's auto_accept when it lands, so that chat's+ // Turn off must go after it; another chat's Accept cannot write it and must not hold it back.+ const acceptsInFlight = useRef(new Map<string, Promise<void>>());+ // How many Turn offs are in flight per chat. Their review gates offer no Accept until every one finishes, so+ // no Accept can start after a disable and write auto_accept back on. Counted, not a flag: one chat's Turn off+ // must not free another's, and a chip that remounts on a chat switch must not free the request still running.+ const [turningOffCounts, setTurningOffCounts] = useState<+ ReadonlyMap<string, number>+ >(() => new Map());+ const noteAutoAcceptWrite = () => {+ autoAcceptWrites.current += 1;+ };+ const setAutoAcceptFromWrite = (value: boolean) => {+ noteAutoAcceptWrite();+ setAutoAccept(value);+ };const [inputValue, setInputValue] = useState("");const [attachments, setAttachments] = useState<CopilotAttachedFile[]>([]);// A file returned to the tray after a failed send may already be saved on that message, so@@ -1731,27 +1778,6 @@ export function WorkflowCopilotChat({},[respondToCredentialPause, continueAfterTerminalConnect],);- // Explore/Draft boundary is unobservable (the LLM writes code with no- // frames emitted); after DRAFTING_GAP_MS of silence with no pending block- // run, assume Draft has started. Re-arms per narrative update; the reducer- // guard makes a stale or double-fired timer a no-op.- const DRAFTING_GAP_MS = 8000;- useEffect(() => {- if (!shouldArmDraftingGapTimer(narrative)) return;- const wait = Math.max(- 0,- DRAFTING_GAP_MS - (Date.now() - narrative.lastActivityAtMs!),- );- const t = setTimeout(- () =>- applyStoredNarrativeEvent({- type: "client_phase_hint",- hintedAtMs: Date.now(),- }),- wait,- );- return () => clearTimeout(t);- }, [narrative, applyStoredNarrativeEvent]);const textareaRef = useRef<HTMLTextAreaElement | null>(null);const { getSaveData } = useWorkflowHasChangesStore();const hasInitializedPosition = useRef(false);@@ -1888,6 +1914,7 @@ export function WorkflowCopilotChat({setMessages([]);discardQueuedPrompt();setWorkflowCopilotChatId(null);+ workflowCopilotChatIdRef.current = null;setProposedWorkflow(null);setPendingProposalMetadata(null);setPendingProposalRun(null);@@ -1915,6 +1942,8 @@ export function WorkflowCopilotChat({messageId: string;status: RecordingRefinementStatus;},+ // Pass for a re-read of the chat on screen; a chat switch or first load always takes the row's value.+ autoAcceptWritesAtRead?: number,) => {setRecoveredPauseFrames(data.pending_credential_requests ?? []);setQuestionInteractions(data.question_interactions ?? []);@@ -2074,7 +2103,12 @@ export function WorkflowCopilotChat({setPendingProposalTurnId(data.proposed_workflow ? restoredPendingProposalTurnId : null,);- setAutoAccept(data.auto_accept ?? false);+ if (+ autoAcceptWritesAtRead === undefined ||+ autoAcceptWritesAtRead === autoAcceptWrites.current+ ) {+ setAutoAccept(data.auto_accept ?? false);+ }setWorkPlan(data.work_plan ?? []);},// Only stable state setters and refs are referenced, so the callback never needs to change.@@ -2201,6 +2235,7 @@ export function WorkflowCopilotChat({return;}const sendEpochBeforeRead = sendEpoch.current;+ const autoAcceptWritesBeforeRead = autoAcceptWrites.current;try {const client = await getClient(credentialGetter, "sans-api-v1");const controller = new AbortController();@@ -2351,6 +2386,7 @@ export function WorkflowCopilotChat({status: recoveredStatus,}: undefined,+ autoAcceptWritesBeforeRead,);setIsLoading(false);}@@ -2600,11 +2636,36 @@ export function WorkflowCopilotChat({setPendingProposalTurnId(null);};- const handleAcceptWorkflow = async (+ const handleAcceptWorkflow = (workflow: WorkflowApiResponse,alwaysAccept: boolean = false,+ ) => {+ const chatKey = workflowCopilotChatIdRef.current?.trim() ?? "";+ const accepting = acceptWorkflow(workflow, alwaysAccept);+ // Chain rather than replace: a second click must not let Turn off skip the first apply.+ acceptsInFlight.current.set(+ chatKey,+ Promise.allSettled([+ acceptsInFlight.current.get(chatKey),+ accepting,+ ]).then(() => undefined),+ );+ return accepting;+ };++ const acceptWorkflow = async (+ workflow: WorkflowApiResponse,+ alwaysAccept: boolean,) => {let chatId = workflowCopilotChatIdRef.current?.trim() || null;+ // The pane can move to another chat, or to a blank New chat, while this runs. Their proposal and+ // auto-accept are their own, so the per-chat state below is skipped unless the pane still shows the chat+ // this accept began on (null included: a pane that had no chat id yet still does not).+ const startedOnChatId = chatId;+ const stillOnAcceptedChat = () => {+ const shown = workflowCopilotChatIdRef.current?.trim() || null;+ return shown === startedOnChatId || shown === chatId;+ };if (!chatId) {try {chatId = await fetchLatestChatId();@@ -2630,14 +2691,18 @@ export function WorkflowCopilotChat({if (!applyWorkflowUpdate(workflow, { applied: true })) {return;}- markProposalAccepted();- setProposedWorkflow(null);- setPendingProposalMetadata(null);- setPendingProposalRun(null);- if (alwaysAccept) {- setAutoAccept(true);+ if (stillOnAcceptedChat()) {+ markProposalAccepted();+ setProposedWorkflow(null);+ setPendingProposalMetadata(null);+ setPendingProposalRun(null);+ if (alwaysAccept) {+ setAutoAcceptFromWrite(true);+ }+ // This accept never resolved a chat id, so it has none to name. Only attempt the best-effort clear+ // while the pane still has none either: any id it holds now is one this accept cannot claim.+ await clearProposedWorkflow(alwaysAccept);}- void clearProposedWorkflow(alwaysAccept);return;}@@ -2658,12 +2723,22 @@ export function WorkflowCopilotChat({) {return;}+ if (!stillOnAcceptedChat()) {+ return;+ }markProposalAccepted();setProposedWorkflow(null);setPendingProposalMetadata(null);setPendingProposalRun(null);if (alwaysAccept) {- setAutoAccept(true);+ setAutoAcceptFromWrite(true);+ } else {+ // A plain Accept writes auto_accept=false on the row, so reads taken before it are stale too.+ noteAutoAcceptWrite();+ }+ if (alwaysAccept !== autoAcceptRef.current) {+ // Apply writes auto-accept best-effort after creating the version, so show what the chat row kept.+ void resyncProposalFromChatRow();}} catch (applyError) {if (getErrorStatus(applyError) === 409) {@@ -2701,14 +2776,17 @@ export function WorkflowCopilotChat({});return;}- markProposalAccepted();- setProposedWorkflow(null);- setPendingProposalMetadata(null);- setPendingProposalRun(null);- if (alwaysAccept) {- setAutoAccept(true);+ if (stillOnAcceptedChat()) {+ markProposalAccepted();+ setProposedWorkflow(null);+ setPendingProposalMetadata(null);+ setPendingProposalRun(null);+ if (alwaysAccept) {+ setAutoAcceptFromWrite(true);+ }}- void clearProposedWorkflow(alwaysAccept);+ // The row write belongs to the accepted chat even if the pane has moved on.+ await clearProposedWorkflow(alwaysAccept, chatId ?? undefined);}};@@ -3044,18 +3122,26 @@ export function WorkflowCopilotChat({if (!chatId) {return;}+ const writesAtRead = autoAcceptWrites.current;try {const client = await getClient(credentialGetter, "sans-api-v1");const response = await client.get<WorkflowCopilotChatHistoryResponse>("/workflow/copilot/chat-history",{ params: { workflow_copilot_chat_id: chatId } },);+ // The read can outlive a switch to another chat, whose pane this row does not describe.+ if (workflowCopilotChatIdRef.current?.trim() !== chatId) {+ return;+ }const nextProposal = response.data.proposed_workflow ?? null;setProposedWorkflow(nextProposal);setPendingProposalMetadata(response.data.proposed_workflow_metadata ?? null,);setPendingProposalRun(response.data.proposed_workflow_run ?? null);+ if (autoAcceptWrites.current === writesAtRead) {+ setAutoAccept(response.data.auto_accept ?? false);+ }setPendingProposalTurnId((currentTurnId) =>nextProposal? (response.data.proposed_workflow_metadata?.owner_turn_id ??@@ -3070,8 +3156,15 @@ export function WorkflowCopilotChat({const clearProposedWorkflow = async (autoAcceptValue: boolean,+ // The chat this clear belongs to. Passed by a caller whose chat may no longer be the one on screen,+ // so the write still lands on the right row; omitted, it clears the chat the pane shows.+ forChatId?: string,): Promise<boolean> => {- const clearProposalByChatId = async (chatId: string) => {+ // Resolves false when the pane switched to a chat this write did not touch, so callers leave it alone.+ // A pane still resolving its chat id reads null, or either id, until the next render; that is no switch.+ const startingChatId =+ forChatId?.trim() || workflowCopilotChatIdRef.current?.trim() || null;+ const clearProposalByChatId = async (chatId: string): Promise<boolean> => {const client = await getClient(credentialGetter, "sans-api-v1");await client.post<WorkflowCopilotClearProposedWorkflowRequest>("/workflow/copilot/clear-proposed-workflow",@@ -3082,9 +3175,15 @@ export function WorkflowCopilotChat({revision: pendingProposalMetadata?.revision ?? null,} as WorkflowCopilotClearProposedWorkflowRequest,);+ const shownChatId = workflowCopilotChatIdRef.current?.trim() || null;+ if (shownChatId !== chatId && shownChatId !== startingChatId) {+ return false;+ }+ setAutoAcceptFromWrite(autoAcceptValue);+ return true;};- let chatId = workflowCopilotChatIdRef.current?.trim() || null;+ let chatId = startingChatId;if (!chatId) {try {chatId = await fetchLatestChatId();@@ -3102,16 +3201,16 @@ export function WorkflowCopilotChat({}try {- await clearProposalByChatId(chatId);- return true;+ return await clearProposalByChatId(chatId);} catch (error) {const status = getErrorStatus(error);- if (status === 404) {+ // A caller that named its chat has no fallback target: the latest chat is someone else's row,+ // and clearing it would delete that chat's pending review.+ if (status === 404 && !forChatId) {try {const refreshedChatId = await fetchLatestChatId();if (refreshedChatId && refreshedChatId !== chatId) {- await clearProposalByChatId(refreshedChatId);- return true;+ return await clearProposalByChatId(refreshedChatId);}} catch (retryError) {console.error("Retry to clear proposed workflow failed:", retryError);@@ -4376,11 +4475,7 @@ export function WorkflowCopilotChat({: null;if (response.updated_workflow &&- shouldAutoApplyWorkflowResponse(- response,- autoAccept,- userCancelledThisTurn,- )+ shouldAutoApplyWorkflowResponse(response, userCancelledThisTurn)) {applyWorkflowUpdate(response.updated_workflow, { applied: true });// This turn's auto-commit already moved canonical past any earlier@@ -4870,7 +4965,6 @@ export function WorkflowCopilotChat({applyWorkflowUpdate,armStop,authoringInProgress,- autoAccept,codeBlockModeEnabled,codeBlockRequestOverride,credentialGetter,@@ -5425,6 +5519,11 @@ export function WorkflowCopilotChat({// restore, so gate actions wait for idle.const gateActionable =Boolean(proposedWorkflow) && !isLoading && !isLoadingHistory;+ const turningOffThisChat =+ workflowCopilotChatId !== null &&+ (turningOffCounts.get(workflowCopilotChatId) ?? 0) > 0;+ // Only the two accepts wait for Turn off; Review and Reject write no auto_accept.+ const gateAcceptsEnabled = !turningOffThisChat;// A staged attachment counts as content: with only a file in the tray the button would// otherwise read as Stop during a turn, and clicking Send would cancel the turn instead.const hasComposerText =@@ -5898,6 +5997,7 @@ export function WorkflowCopilotChat({: null}actionsEnabled={gateActionable}+ acceptsEnabled={gateAcceptsEnabled}onAccept={() =>proposedWorkflow &&handleAcceptWorkflow(proposedWorkflow)@@ -6068,6 +6168,7 @@ export function WorkflowCopilotChat({)}settled={null}actionsEnabled={gateActionable}+ acceptsEnabled={gateAcceptsEnabled}onAccept={() =>proposedWorkflow &&handleAcceptWorkflow(proposedWorkflow)@@ -6104,6 +6205,7 @@ export function WorkflowCopilotChat({verdict={getReviewGateVerdict(undefined, proposedWorkflow)}settled={null}actionsEnabled={gateActionable}+ acceptsEnabled={gateAcceptsEnabled}onAccept={() => handleAcceptWorkflow(proposedWorkflow)}onAlwaysAccept={() =>handleAcceptWorkflow(proposedWorkflow, true)@@ -6258,58 +6360,119 @@ export function WorkflowCopilotChat({{/* Input */}<div className="border-t border-border p-3">{codeOptionAvailable ? (- <div className="mb-2">- <DropdownMenu>- <DropdownMenuTrigger asChild>- <button- type="button"- title="Switch mode"- aria-label="Switch mode"- className="flex items-center gap-1.5 rounded-full border border-border bg-slate-elevation2 px-2.5 py-1 text-[11px] font-medium text-muted-foreground hover:bg-slate-elevation3 hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"+ <div className="mb-2 flex flex-wrap items-center gap-1.5">+ {codeOptionAvailable ? (+ <DropdownMenu>+ <DropdownMenuTrigger asChild>+ <button+ type="button"+ title="Switch mode"+ aria-label="Switch mode"+ className="flex items-center gap-1.5 rounded-full border border-border bg-slate-elevation2 px-2.5 py-1 text-[11px] font-medium text-muted-foreground hover:bg-slate-elevation3 hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"+ >+ <ModeGlyph glow={codeStateActive} />+ <span className="text-foreground">+ {codeStateActive ? "Build with code" : "Build"}+ </span>+ <ChevronDownIcon className="h-3 w-3" />+ </button>+ </DropdownMenuTrigger>+ <DropdownMenuContent+ side="top"+ align="start"+ className="w-[272px] p-1.5"+ onCloseAutoFocus={(event) => event.preventDefault()}>- <span>Mode:</span>- <ModeGlyph glow={codeStateActive} />- <span className="text-foreground">- {codeStateActive ? "Build with code" : "Build"}- </span>- <ChevronDownIcon className="h-3 w-3" />- </button>- </DropdownMenuTrigger>- <DropdownMenuContent- side="top"- align="start"- className="w-[272px] p-1.5"- onCloseAutoFocus={(event) => event.preventDefault()}- >- {modeMenuItems}- </DropdownMenuContent>- </DropdownMenu>+ {modeMenuItems}+ </DropdownMenuContent>+ </DropdownMenu>+ ) : null}</div>) : null}- {proposedWorkflow &&- pendingProposalTurnId &&- (gateOwnerIndex !== lastTurnIndex || isLoading) ? (- <button- type="button"- onClick={() => {- if (!pendingProposalTurnId) return;- document- .getElementById(`copilot-gate-${pendingProposalTurnId}`)- ?.scrollIntoView({ behavior: "smooth", block: "center" });- if (gateFlashTimer.current !== null) {- clearTimeout(gateFlashTimer.current);- }- setGateFlashTurnId(pendingProposalTurnId);- gateFlashTimer.current = setTimeout(() => {- setGateFlashTurnId(null);- gateFlashTimer.current = null;- }, 1100);- }}- className="mb-2 flex items-center gap-1.5 rounded-full border border-border px-2.5 py-1 text-[10.5px] text-muted-foreground hover:bg-slate-elevation3"- >- <span className="h-1.5 w-1.5 rounded-full bg-sky-400" />1 proposal- pending · Review- </button>+ {(proposedWorkflow &&+ pendingProposalTurnId &&+ (gateOwnerIndex !== lastTurnIndex || isLoading)) ||+ (autoAccept && workflowCopilotChatId) ? (+ // One status strip: what Copilot is doing, separate from the mode control above it.+ <div className="mb-2 flex items-center gap-2 border-t border-border/60 pt-1.5 text-[10.5px] text-muted-foreground">+ {proposedWorkflow &&+ pendingProposalTurnId &&+ (gateOwnerIndex !== lastTurnIndex || isLoading) ? (+ <button+ type="button"+ onClick={() => {+ if (!pendingProposalTurnId) return;+ document+ .getElementById(`copilot-gate-${pendingProposalTurnId}`)+ ?.scrollIntoView({ behavior: "smooth", block: "center" });+ if (gateFlashTimer.current !== null) {+ clearTimeout(gateFlashTimer.current);+ }+ setGateFlashTurnId(pendingProposalTurnId);+ gateFlashTimer.current = setTimeout(() => {+ setGateFlashTurnId(null);+ gateFlashTimer.current = null;+ }, 1100);+ }}+ className="flex min-w-0 items-center gap-1.5 rounded-sm text-muted-foreground hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"+ >+ <span+ className="h-1.5 w-1.5 shrink-0 rounded-full bg-sky-400"+ aria-hidden="true"+ />+ <span className="truncate">1 proposal pending</span>+ <span className="shrink-0 text-foreground underline underline-offset-2">+ Review+ </span>+ </button>+ ) : null}+ {autoAccept && workflowCopilotChatId ? (+ <div className="ml-auto flex min-w-0 items-center">+ <AutoAcceptChip+ key={workflowCopilotChatId}+ chatId={workflowCopilotChatId}+ pendingFromChat={turningOffThisChat}+ waitForAccept={async (turnOffChatId) => {+ // An Accept clicked while Turn off waits joins the chain, so wait until it stops growing.+ const deadline = Date.now() + ACCEPT_SETTLE_CEILING_MS;+ let settled: Promise<void> | undefined;+ do {+ settled = acceptsInFlight.current.get(turnOffChatId);+ if (+ settled &&+ !(await settledWithin(settled, deadline - Date.now()))+ ) {+ throw new Error("Accept is still running");+ }+ } while (+ settled !== acceptsInFlight.current.get(turnOffChatId)+ );+ }}+ onPendingChange={(pendingChatId, pending) =>+ setTurningOffCounts((current) => {+ const next = new Map(current);+ const count =+ (next.get(pendingChatId) ?? 0) + (pending ? 1 : -1);+ if (count > 0) {+ next.set(pendingChatId, count);+ } else {+ next.delete(pendingChatId);+ }+ return next;+ })+ }+ onTurnedOff={(turnedOffChatId) => {+ // The request can land after a switch to a chat whose setting it did not change.+ if (turnedOffChatId !== workflowCopilotChatIdRef.current) {+ return;+ }+ setAutoAcceptFromWrite(false);+ textareaRef.current?.focus();+ }}+ />+ </div>+ ) : null}+ </div>) : null}{showWorkingRow ? (<CopilotWorkingStatusdiff --git a/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.test.tsxindex d985d2c11..51ae013a4 100644--- a/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.test.tsx@@ -9,7 +9,6 @@ import {type TurnNarrativeState,} from "../narrativeState";import { WorkflowApiResponse } from "@/routes/workflows/types/workflowTypes";-import { derivePhases } from "../copilotPhases";import { ReviewGateCard, getReviewGateVerdict } from "./ReviewGateCard";const failedBlock = {@@ -149,15 +148,13 @@ describe("getReviewGateVerdict", () => {expect(getReviewGateVerdict(undefined, proposal)).toBe(null);});- it("never reports tested while the turn's own rail computes a failed test phase", () => {+ it("never reports tested for a turn with a failed test block", () => {const failedTurn = turn({proposalDisposition: "review_tested",+ turnFacts: coveredFacts,blocks: [failedBlock],});- expect(- derivePhases(failedTurn).find((row) => row.id === "test")?.status,- ).toBe("fail");expect(getReviewGateVerdict(failedTurn, null)).not.toBe("tested");});});diff --git a/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.tsx b/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.tsxindex 9408643db..aef9755f0 100644--- a/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.tsx@@ -82,6 +82,9 @@ interface ReviewGateCardProps {verdict: ReviewGateVerdict;settled?: ReviewGateSettled;actionsEnabled: boolean;+ // False only while this chat's Turn off is in flight: an Accept started then could write auto_accept+ // back on after it. Review and Reject write no auto_accept and stay available.+ acceptsEnabled?: boolean;onAccept: () => void;onAlwaysAccept: () => void;onReject: () => void;@@ -127,6 +130,7 @@ export function ReviewGateCard({verdict,settled = null,actionsEnabled,+ acceptsEnabled = true,onAccept,onAlwaysAccept,onReject,@@ -255,20 +259,24 @@ export function ReviewGateCard({>Review</button>- <button- type="button"- onClick={onAccept}- className="rounded-md bg-success px-3 py-1.5 text-xs font-semibold text-success-foreground hover:opacity-90"- >- Accept- </button>- <button- type="button"- onClick={onAlwaysAccept}- className="rounded-md px-3 py-1.5 text-xs text-muted-foreground hover:bg-slate-elevation4 hover:text-foreground dark:hover:text-slate-200"- >- Always accept- </button>+ {acceptsEnabled ? (+ <>+ <button+ type="button"+ onClick={onAccept}+ className="rounded-md bg-success px-3 py-1.5 text-xs font-semibold text-success-foreground hover:opacity-90"+ >+ Accept+ </button>+ <button+ type="button"+ onClick={onAlwaysAccept}+ className="rounded-md px-3 py-1.5 text-xs text-muted-foreground hover:bg-slate-elevation4 hover:text-foreground dark:hover:text-slate-200"+ >+ Always accept+ </button>+ </>+ ) : null}<buttontype="button"onClick={onReject}diff --git a/skyvern-frontend/src/routes/workflows/copilot/copilotActivityLog.ts b/skyvern-frontend/src/routes/workflows/copilot/copilotActivityLog.tsindex efc7ca4bb..182c5ce56 100644--- a/skyvern-frontend/src/routes/workflows/copilot/copilotActivityLog.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/copilotActivityLog.ts@@ -71,16 +71,13 @@ export interface ActivityLog {}// Block-scoped authoring tools, absent from narrativeState's AUTHORING_TOOLS.-// The rail buckets them as pre-authoring today (SKY-14524); this set diverges-// deliberately rather than widening AUTHORING_TOOLS and moving the rail.const LOG_AUTHORING_TOOLS = new Set(["edit_block","add_block","delete_block",]);-// update_and_run_blocks belongs to both sets, so RUN_TOOLS has to win — the-// same precedence bucketActivity already applies.+// update_and_run_blocks belongs to both sets, so RUN_TOOLS has to win.export function kindOf(entry: ActivityEntry): ActivityKind | null {const toolName = entry.toolName;if (entry.kind === "narration" || toolName === undefined) {diff --git a/skyvern-frontend/src/routes/workflows/copilot/copilotPhases.test.ts b/skyvern-frontend/src/routes/workflows/copilot/copilotPhases.test.tsindex d7f9b39ef..d3cb2b4dd 100644--- a/skyvern-frontend/src/routes/workflows/copilot/copilotPhases.test.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/copilotPhases.test.ts@@ -3,38 +3,14 @@ import { describe, expect, it } from "vitest";import {AUTHORING_TOOLS,RUN_TOOLS,- derivePhases,- shouldArmDraftingGapTimer,showPhaseChecklist,} from "./copilotPhases";import {- ActivityEntry,- BlockState,EMPTY_NARRATIVE,TurnNarrativeState,toolActivityDisplayLabel,} from "./narrativeState";-const entry = (- overrides: Partial<ActivityEntry> & Pick<ActivityEntry, "id" | "kind">,-): ActivityEntry => ({- text: "did something",- iteration: 0,- ...overrides,-});--const block = (overrides: Partial<BlockState> = {}): BlockState => ({- workflowRunBlockId: "wrb_1",- label: "block_1",- blockType: "task",- state: "completed",- lastSeenIteration: 0,- activity: [],- startedAt: "2026-06-10T00:00:00Z",- endedAt: "2026-06-10T00:00:10Z",- ...overrides,-});-const turn = (overrides: Partial<TurnNarrativeState> = {},): TurnNarrativeState => ({@@ -45,10 +21,6 @@ const turn = (...overrides,});-function phase(rows: ReturnType<typeof derivePhases>, id: string) {- return rows.find((r) => r.id === id)!;-}-describe("AUTHORING_TOOLS / RUN_TOOLS", () => {it("update_workflow is authoring-only, not a run tool (Codex catch)", () => {expect(AUTHORING_TOOLS.has("update_workflow")).toBe(true);@@ -101,488 +73,6 @@ describe("toolActivityDisplayLabel — discovery tools (SKY-12385)", () => {});});-describe("derivePhases — bucket split keeps update_workflow in Draft (Codex catch)", () => {- it("an update_workflow tool_call on a draft-only turn lands in the Draft bucket, not Test", () => {- const t = turn({- designEnded: true,- designActivity: [- entry({ id: "1", kind: "tool_call", toolName: "navigate_browser" }),- entry({ id: "2", kind: "tool_call", toolName: "update_workflow" }),- ],- });- const rows = derivePhases(t);- expect(phase(rows, "draft").entries.map((e) => e.id)).toEqual(["2"]);- expect(phase(rows, "test").entries).toEqual([]);- });-});--describe("derivePhases — composite scoped edit and run", () => {- it.each(["edit_block_and_run", "test_workflow_from_blank_browser"])(- "places %s activity in Test before block rows hydrate",- (toolName) => {- const rows = derivePhases(- turn({- designEnded: true,- designActivity: [- entry({- id: "1",- kind: "tool_call",- toolName,- }),- ],- }),- );-- expect(phase(rows, "test").entries.map((e) => e.id)).toEqual(["1"]);- if (toolName === "edit_block_and_run") {- expect(phase(rows, "test").status).not.toBe("pending");- }- },- );-});--describe("derivePhases — bucket split", () => {- it("routes pre-authoring activity to explore, RUN_TOOLS to test, post-authoring narration to draft", () => {- const t = turn({- designActivity: [- entry({ id: "1", kind: "tool_call", toolName: "navigate_browser" }),- entry({ id: "2", kind: "narration" }),- entry({- id: "3",- kind: "tool_call",- toolName: "update_and_run_blocks",- }),- entry({ id: "4", kind: "narration" }),- ],- });- const rows = derivePhases(t);- expect(phase(rows, "explore").entries.map((e) => e.id)).toEqual(["1", "2"]);- expect(phase(rows, "test").entries.map((e) => e.id)).toEqual(["3"]);- expect(phase(rows, "draft").entries.map((e) => e.id)).toEqual(["4"]);- });-});--describe("derivePhases — condenses each phase's entries (SKY-11971)", () => {- it("folds a failed-then-retried tool_call/tool_result pair in the explore bucket into one row", () => {- const t = turn({- designActivity: [- entry({- id: "tc-1",- kind: "tool_call",- toolName: "evaluate",- }),- entry({- id: "tr-1",- kind: "tool_result",- toolName: "evaluate",- success: false,- }),- entry({- id: "tc-2",- kind: "tool_call",- toolName: "evaluate",- }),- entry({- id: "tr-2",- kind: "tool_result",- toolName: "evaluate",- success: true,- }),- ],- });- const rows = derivePhases(t);- const exploreEntries = phase(rows, "explore").entries;- expect(exploreEntries).toHaveLength(1);- expect(exploreEntries[0]).toMatchObject({- id: "tr-2",- success: true,- attempts: 2,- });- });-- it("does not fold a failed edit of one block into a later edit of a different block", () => {- const t = turn({- designActivity: [- entry({- id: "tr-1",- kind: "tool_result",- toolName: "edit_block",- displayLabel: 'Editing block "Login Form"',- success: false,- }),- entry({- id: "tr-2",- kind: "tool_result",- toolName: "edit_block",- displayLabel: 'Editing block "Search Box"',- success: true,- }),- ],- });- const rows = derivePhases(t);- const exploreEntries = phase(rows, "explore").entries;- expect(exploreEntries).toHaveLength(2);- expect(exploreEntries.some((e) => e.attempts !== undefined)).toBe(false);- });-- it("REGRESSION PIN: the explore 'N steps' stub matches the condensed row count, not the raw event count (Claude catch)", () => {- const t = turn({- terminal: "response",- designActivity: [- entry({ id: "tc-1", kind: "tool_call", toolName: "evaluate" }),- entry({- id: "tr-1",- kind: "tool_result",- toolName: "evaluate",- success: false,- }),- entry({ id: "tc-2", kind: "tool_call", toolName: "evaluate" }),- entry({- id: "tr-2",- kind: "tool_result",- toolName: "evaluate",- success: true,- }),- ],- });- const rows = derivePhases(t);- expect(phase(rows, "explore").entries).toHaveLength(1);- expect(phase(rows, "explore").stub).toBe("1 step");- });-- it("leaves the earlier bucket-routing test's un-paired hand-rolled ids unaffected", () => {- // Regression pin: short test ids ("1"/"2"/"3"/"4") don't collide with- // the tc-/tr- correlation convention, so condensing is a no-op there.- const t = turn({- designActivity: [- entry({ id: "1", kind: "tool_call", toolName: "navigate_browser" }),- entry({ id: "2", kind: "tool_call", toolName: "update_workflow" }),- ],- designEnded: true,- });- const rows = derivePhases(t);- expect(phase(rows, "draft").entries.map((e) => e.id)).toEqual(["2"]);- });-});--describe("derivePhases — live progression", () => {- it("explore is active before any authoring activity", () => {- const t = turn({- designActivity: [- entry({ id: "1", kind: "tool_call", toolName: "navigate_browser" }),- ],- });- expect(phase(derivePhases(t), "explore").status).toBe("active");- expect(phase(derivePhases(t), "draft").status).toBe("pending");- });-- it("REGRESSION PIN (SKY-12969): the drafting-gap silence hint alone never completes Explore mid-scout", () => {- // The 8s drafting-gap heuristic fires on any tool-call silence — including a- // scout thinking-pause during login/2FA before the synthesis lane sets- // turn.draft. A phase claim must derive from recorded authoring activity,- // not the absence of frames, so a bare draftingSignaledAt leaves Explore- // active and Draft pending.- const t = turn({- draftingSignaledAt: 1000,- designActivity: [- entry({ id: "1", kind: "tool_call", toolName: "navigate_browser" }),- ],- });- const rows = derivePhases(t);- expect(phase(rows, "explore").status).toBe("active");- expect(phase(rows, "draft").status).toBe("pending");- expect(phase(rows, "test").status).toBe("pending");- });-- it("test goes active once a block starts running", () => {- const t = turn({- draft: { blockCount: 1, blockLabels: ["block_1"], summary: null },- blocks: [block({ state: "running", endedAt: null })],- });- const rows = derivePhases(t);- expect(phase(rows, "draft").status).toBe("done");- expect(phase(rows, "test").status).toBe("active");- });-});--describe("derivePhases — terminal", () => {- it("terminal success: all done, Done gets a green check", () => {- const t = turn({- terminal: "response",- designEnded: true,- draft: { blockCount: 1, blockLabels: ["block_1"], summary: null },- blocks: [block()],- });- const rows = derivePhases(t);- expect(rows.map((r) => r.status)).toEqual(["done", "done", "done", "done"]);- });-- it("a failed block marks Test fail with a failed-count stub", () => {- const t = turn({- terminal: "response",- designEnded: true,- draft: { blockCount: 1, blockLabels: ["block_1"], summary: null },- blocks: [block({ state: "failed" })],- });- const rows = derivePhases(t);- expect(phase(rows, "test").status).toBe("fail");- expect(phase(rows, "test").stub).toBe("1 block · failed");- });-- it("a deadline halt after a clean run stops the rail instead of failing it", () => {- const t = turn({- terminal: "error",- designEnded: true,- draft: { blockCount: 1, blockLabels: ["block_1"], summary: null },- blocks: [block()],- turnFacts: {- factsAvailable: true,- authoredBlockCount: 1,- matchingSourceBlockCount: 1,- evaluationState: "not_evaluated",- runId: "wr_1",- runCompleted: true,- terminalCause: "deadline_expired",- blocksRunThisTurn: 1,- ranCleanOnCurrentSource: false,- },- });- const rows = derivePhases(t);- expect(phase(rows, "test").status).toBe("stopped");- expect(phase(rows, "test").stub).toBe("1 block · time limit");- expect(phase(rows, "done").status).toBe("stopped");- expect(rows.map((r) => r.status)).not.toContain("fail");- });-- it("a deadline halt with no block receipts never paints a green Test row", () => {- const t = turn({- terminal: "error",- designEnded: true,- draft: { blockCount: 1, blockLabels: ["block_1"], summary: null },- designActivity: [- entry({- id: "1",- kind: "tool_call",- toolName: "update_and_run_blocks",- }),- ],- turnFacts: {- factsAvailable: true,- authoredBlockCount: 1,- matchingSourceBlockCount: 0,- evaluationState: null,- runId: null,- runCompleted: null,- terminalCause: "deadline_expired",- blocksRunThisTurn: 0,- ranCleanOnCurrentSource: false,- },- });- const rows = derivePhases(t);- expect(phase(rows, "test").status).toBe("stopped");- expect(phase(rows, "test").stub).toBe("time limit");- });-- it("a deadline halt with a failed block keeps the failure row", () => {- const t = turn({- terminal: "error",- designEnded: true,- draft: { blockCount: 1, blockLabels: ["block_1"], summary: null },- blocks: [block({ state: "failed" })],- turnFacts: {- factsAvailable: true,- authoredBlockCount: 1,- matchingSourceBlockCount: 1,- evaluationState: "not_demonstrated",- runId: "wr_1",- runCompleted: false,- terminalCause: "deadline_expired",- blocksRunThisTurn: 1,- ranCleanOnCurrentSource: false,- },- });- const rows = derivePhases(t);- expect(phase(rows, "test").status).toBe("fail");- expect(phase(rows, "test").stub).toBe("1 block · failed");- });-- it("draft-only turn (update_workflow, no run): Test renders notrun, Done still succeeds", () => {- const t = turn({- terminal: "response",- designEnded: true,- draft: { blockCount: 1, blockLabels: ["block_1"], summary: null },- designActivity: [- entry({ id: "1", kind: "tool_call", toolName: "update_workflow" }),- ],- });- const rows = derivePhases(t);- expect(phase(rows, "test").status).toBe("notrun");- expect(phase(rows, "test").stub).toBe("not run");- expect(phase(rows, "done").status).toBe("done");- });-- it("cancel-mid-explore: explore stops, Done renders dim", () => {- const t = turn({- terminal: "response",- cancelled: true,- designActivity: [- entry({ id: "1", kind: "tool_call", toolName: "navigate_browser" }),- ],- });- const rows = derivePhases(t);- expect(phase(rows, "explore").status).toBe("stopped");- expect(phase(rows, "done").status).toBe("stopped");- });-- it("error terminal marks the active-at-end phase failed", () => {- const t = turn({- terminal: "error",- designActivity: [- entry({ id: "1", kind: "tool_call", toolName: "navigate_browser" }),- ],- });- const rows = derivePhases(t);- expect(phase(rows, "explore").status).toBe("fail");- expect(phase(rows, "done").status).toBe("fail");- });-});--describe("derivePhases — SKY-12969 code-only incremental build (concurrent draft)", () => {- // Mirrors the production session: scout tool completions interleaved with the- // synthesis lane's offer renders (turn.draft) at trajectory 1 and 4, zero- // AUTHORING_TOOLS entries, user-cancel terminal.- const OFFER = { blockCount: 1, blockLabels: ["block_1"], summary: null };- const scoutTools = [- "navigate_browser",- "evaluate",- "evaluate",- "take_screenshot",- "evaluate",- "fill_credential_field",- "fill_credential_field",- "evaluate",- "click",- "fill_credential_field",- "click",- ];- const scout = (n: number): ActivityEntry[] =>- scoutTools- .slice(0, n)- .map((toolName, i) =>- entry({ id: `s${i}`, kind: "tool_call", toolName }),- );-- it("AC1: Explore stays active (never done) while the scout is mid-login and an offer has already rendered", () => {- // Screenshot-1 instant: 10 scout completions, offer rendered (turn.draft set- // by the synthesis lane), no authoring tool, live browser still on 2FA.- const midScout = turn({- terminal: null,- draft: OFFER,- designActivity: scout(10),- });- const rows = derivePhases(midScout);- expect(phase(rows, "explore").status).toBe("active");- expect(phase(rows, "draft").status).toBe("pending");- // An active Explore row shows no done-stub — its count cannot be claimed- // complete while it is still growing.- expect(phase(rows, "explore").stub).toBeNull();- });-- it("AC1: the growing step count never lands under a completed row", () => {- const at10 = derivePhases(- turn({ terminal: null, draft: OFFER, designActivity: scout(10) }),- );- const at11 = derivePhases(- turn({ terminal: null, draft: OFFER, designActivity: scout(11) }),- );- expect(phase(at10, "explore").status).toBe("active");- expect(phase(at11, "explore").status).toBe("active");- expect(phase(at11, "explore").entries.length).toBeGreaterThan(- phase(at10, "explore").entries.length,- );- });-- it("AC1: a user-cancel mid-login marks Explore stopped (not done); Draft stays pending", () => {- const cancelled = turn({- terminal: "response",- cancelled: true,- designEnded: true,- draft: OFFER,- designActivity: scout(11),- });- const rows = derivePhases(cancelled);- expect(phase(rows, "explore").status).toBe("stopped");- expect(phase(rows, "draft").status).toBe("pending");- expect(phase(rows, "done").status).toBe("stopped");- });-- it("AC1/ruling: a healthy (non-cancelled) response terminal carrying the proposal resolves the phases done, count frozen", () => {- // The user let the same turn finish: scout completed, proposal offered, no- // authoring tool, no run. The turn is over — Explore genuinely ended, so the- // row completes WITH its final count (count-immutability holds).- const healthy = turn({- terminal: "response",- cancelled: false,- designEnded: true,- draft: OFFER,- designActivity: scout(11),- });- const rows = derivePhases(healthy);- expect(phase(rows, "explore").status).toBe("done");- expect(phase(rows, "draft").status).toBe("done");- expect(phase(rows, "test").status).toBe("notrun");- expect(phase(rows, "done").status).toBe("done");- expect(phase(rows, "explore").stub).toBe("11 steps");- });-- it("AC1: hydration parity — a draft-only authoring turn resolves identically live and after reload", () => {- // authoringCount is client-only and resets to 0 on a history reload; the- // hydrated designActivity still carries the update_workflow entry, so- // authoringSeen keeps Explore/Draft resolved to done both ways (no jump).- const authored = [- entry({ id: "1", kind: "tool_call", toolName: "navigate_browser" }),- entry({ id: "2", kind: "tool_call", toolName: "update_workflow" }),- ];- const live = turn({- terminal: "response",- designEnded: true,- authoringCount: 1,- draft: OFFER,- designActivity: authored,- });- const hydrated = turn({- terminal: "response",- designEnded: true,- authoringCount: 0,- draft: OFFER,- designActivity: authored,- });- expect(derivePhases(hydrated).map((r) => r.status)).toEqual(- derivePhases(live).map((r) => r.status),- );- expect(phase(derivePhases(hydrated), "explore").status).toBe("done");- expect(phase(derivePhases(hydrated), "draft").status).toBe("done");- });-- it("AC1/parity: a cancelled terminal with authoring evidence but an aged-out activity list still resolves Explore done, Draft stopped", () => {- // The reducer grafts the uncapped authoringCount across the terminal swap;- // the derivation must honor it so an authoring turn whose sole authoring- // entry evicted from the 50-capped activity list doesn't flip Explore back- // to stopped after the swap (SKY-11970 live/terminal parity).- const cancelledEvicted = turn({- terminal: "response",- cancelled: true,- designEnded: true,- authoringCount: 1,- draft: OFFER,- designActivity: [],- });- const rows = derivePhases(cancelledEvicted);- expect(phase(rows, "explore").status).toBe("done");- expect(phase(rows, "draft").status).toBe("stopped");- });-});-describe("showPhaseChecklist", () => {it("false for a clarify terminal turn with no draft and no blocks", () => {expect(@@ -607,143 +97,3 @@ describe("showPhaseChecklist", () => {expect(showPhaseChecklist(turn({ terminal: null }))).toBe(true);});});--describe("shouldArmDraftingGapTimer", () => {- it("arms once a tool_call round-trip has completed and gone quiet", () => {- const t = turn({- designActivity: [- entry({ id: "1", kind: "tool_call", toolName: "navigate_browser" }),- entry({ id: "2", kind: "tool_result", toolName: "navigate_browser" }),- ],- lastActivityAtMs: 1000,- });- expect(shouldArmDraftingGapTimer(t)).toBe(true);- });-- it("REGRESSION PIN: does not arm while a tool_call is still pending its result (Codex catch)", () => {- // A slow navigate_browser taking >8s must not be mistaken for silent- // LLM code generation — the FE can tell "tool executing" from "LLM- // generating" by whether the last frame is an unresolved tool_call.- const t = turn({- designActivity: [- entry({ id: "1", kind: "tool_call", toolName: "navigate_browser" }),- ],- lastActivityAtMs: 1000,- });- expect(shouldArmDraftingGapTimer(t)).toBe(false);- });-- it("REGRESSION PIN: does not arm when a mid-flight progress narration is the last frame but the tool call is still pending (Codex catch)", () => {- // schedule_narration can emit a TOOL_STARTED progress narration right- // after the tool_call, before the matching tool_result — a check on- // only the last entry's kind misses this; id-matching does not.- const t = turn({- designActivity: [- entry({- id: "tc-1",- kind: "tool_call",- toolName: "navigate_browser",- }),- entry({ id: "n-1", kind: "narration", text: "Opening the page…" }),- ],- lastActivityAtMs: 1000,- });- expect(shouldArmDraftingGapTimer(t)).toBe(false);- });-- it("arms once the pending tool_call's matching tool_result arrives", () => {- const t = turn({- designActivity: [- entry({- id: "tc-1",- kind: "tool_call",- toolName: "navigate_browser",- }),- entry({ id: "n-1", kind: "narration", text: "Opening the page…" }),- entry({- id: "tr-1",- kind: "tool_result",- toolName: "navigate_browser",- }),- ],- lastActivityAtMs: 1000,- });- expect(shouldArmDraftingGapTimer(t)).toBe(true);- });-- it("does not arm once draftingSignaledAt is already set", () => {- const t = turn({- designActivity: [- entry({ id: "1", kind: "tool_call", toolName: "navigate_browser" }),- entry({ id: "2", kind: "tool_result", toolName: "navigate_browser" }),- ],- lastActivityAtMs: 1000,- draftingSignaledAt: 500,- });- expect(shouldArmDraftingGapTimer(t)).toBe(false);- });-- it("does not arm once a draft or block exists", () => {- const t = turn({- designActivity: [- entry({ id: "1", kind: "tool_call", toolName: "navigate_browser" }),- entry({ id: "2", kind: "tool_result", toolName: "navigate_browser" }),- ],- lastActivityAtMs: 1000,- draft: { blockCount: 1, blockLabels: ["a"], summary: null },- });- expect(shouldArmDraftingGapTimer(t)).toBe(false);- });-});--describe("derivePhases — test-run count on the not-confirmed stub (SKY-11339)", () => {- const notConfirmedTurn = (- runToolNames: string[],- outcomeRole?: BlockState["outcomeRole"],- ): TurnNarrativeState =>- turn({- terminal: "response",- designEnded: true,- draft: { blockCount: 1, blockLabels: ["block_1"], summary: null },- blocks: [- block({- state: "completed",- outcome: "not_demonstrated",- outcomeRole,- }),- ],- designActivity: runToolNames.map((toolName, i) =>- entry({ id: `r${i}`, kind: "tool_call", toolName }),- ),- });-- it("surfaces the run count when the workflow was tested multiple times", () => {- const rows = derivePhases(- notConfirmedTurn([- "update_and_run_blocks",- "update_and_run_blocks",- "run_blocks_and_collect_debug",- ]),- );- expect(phase(rows, "test").stub).toBe("3 runs · not confirmed");- });-- it("omits the count for a single run, keeping today's look", () => {- const rows = derivePhases(notConfirmedTurn(["update_and_run_blocks"]));- expect(phase(rows, "test").stub).toBe("· not confirmed");- });-- it("keeps an explicitly adjudicated negative outcome in the alarm stub", () => {- const rows = derivePhases(- notConfirmedTurn(["update_and_run_blocks"], "adjudicated"),- );- expect(phase(rows, "test").stub).toBe("· not confirmed");- });-- it("suppresses the alarm stub for an interim build test", () => {- const rows = derivePhases(- notConfirmedTurn(["update_and_run_blocks"], "interim_build_test"),- );- expect(phase(rows, "test").stub).not.toContain("not confirmed");- });-});diff --git a/skyvern-frontend/src/routes/workflows/copilot/copilotPhases.ts b/skyvern-frontend/src/routes/workflows/copilot/copilotPhases.tsindex 7faaf6d5d..2eaf92b66 100644--- a/skyvern-frontend/src/routes/workflows/copilot/copilotPhases.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/copilotPhases.ts@@ -1,51 +1,12 @@-// Pure derivation of the 4-phase build checklist (Explore / Draft / Test-run-// / Done) from TurnNarrativeState. Kept dependency-light and side-effect-free-// (mirrors actionReveal.ts) so the same rule produces identical rows live,-// at the terminal payload swap, and on a hydrated history reload — the-// reducer's `response` case REPLACES client state with the BE payload, so any-// live-only derivation would make rows visibly jump at that instant.import {- ActivityEntry,AUTHORING_TOOLS,- BlockState,RUN_TOOLS,TurnNarrativeState,- condenseActivityEntries,- formatElapsed,- hasPendingToolCall,- isDeadlineHalt,- isInterimOutcome,latestBlocksByLabel,- parseUtcIsoMs,} from "./narrativeState";export { AUTHORING_TOOLS, RUN_TOOLS };-export type CopilotPhaseId = "explore" | "draft" | "test" | "done";-export type PhaseStatus =- | "pending"- | "active"- | "done"- | "fail"- | "stopped"- | "notrun";--export interface PhaseRowModel {- id: CopilotPhaseId;- label: string;- status: PhaseStatus;- stub: string | null;- // Hosted stream for explore/draft; test-tool rows (not block cards) for test.- entries: ActivityEntry[];-}--const PHASE_LABELS: Record<CopilotPhaseId, string> = {- explore: "Explore site",- draft: "Draft code",- test: "Test-run",- done: "Done",-};-// Byte-for-byte the existing showDesign gate (NarrativeView.tsx) so Q&A /// clarify turns keep today's behavior: checklist appears live, disappears at// a no-build terminal.@@ -58,123 +19,6 @@ export function showPhaseChecklist(turn: TurnNarrativeState): boolean {);}-// Whether the 8s drafting-gap timer should arm for this narrative snapshot.-// A tool call with no matching result yet is still executing (e.g. a slow-// navigate_browser) — that's "tool executing", not "LLM silently drafting",-// so a pending round-trip must not arm the timer.-export function shouldArmDraftingGapTimer(turn: TurnNarrativeState): boolean {- if (- turn.turnId === null ||- turn.terminal !== null ||- turn.draftingSignaledAt !== null- ) {- return false;- }- if (- turn.draft !== null ||- turn.designEnded ||- turn.blocks.some((b) => b.state !== "drafted")- ) {- return false;- }- if (!turn.designActivity.some((e) => e.kind === "tool_call")) return false;- if (hasPendingToolCall(turn.designActivity)) return false;- return turn.lastActivityAtMs !== null;-}--function bucketActivity(designActivity: ActivityEntry[]): {- explore: ActivityEntry[];- draft: ActivityEntry[];- test: ActivityEntry[];- authoringSeen: boolean;-} {- const explore: ActivityEntry[] = [];- const draft: ActivityEntry[] = [];- const test: ActivityEntry[] = [];- let authoringSeen = false;- for (const entry of designActivity) {- // Set authoringSeen from AUTHORING_TOOLS independent of the RUN_TOOLS- // bucket check below — update_workflow is authoring but not a run tool,- // so it must still flip the explore→draft boundary even though its own- // entry falls through to the draft push, not the test push.- if (entry.toolName && AUTHORING_TOOLS.has(entry.toolName)) {- authoringSeen = true;- }- if (entry.toolName && RUN_TOOLS.has(entry.toolName)) {- test.push(entry);- continue;- }- (authoringSeen ? draft : explore).push(entry);- }- return { explore, draft, test, authoringSeen };-}--function lastAuthoringToolName(- designActivity: ActivityEntry[],-): string | undefined {- for (let i = designActivity.length - 1; i >= 0; i--) {- const entry = designActivity[i]!;- if (- entry.kind === "tool_call" &&- entry.toolName &&- AUTHORING_TOOLS.has(entry.toolName)- ) {- return entry.toolName;- }- }- return undefined;-}--// Raw designActivity count of tool_calls in `tools` — drives the Draft-// "N drafts" (AUTHORING_TOOLS) and Test-run "N runs" (RUN_TOOLS) stubs, since-// each authoring or block-running tool call lands in designActivity.-function countToolCalls(- designActivity: ActivityEntry[],- tools: Set<string>,-): number {- let count = 0;- for (const entry of designActivity) {- if (- entry.kind === "tool_call" &&- entry.toolName &&- tools.has(entry.toolName)- ) {- count += 1;- }- }- return count;-}--function pluralize(n: number, noun: string): string {- return `${n} ${noun}${n === 1 ? "" : "s"}`;-}--// Earliest startedAt / latest endedAt across a block set, as ISO strings so-// the existing formatElapsed can render them — mixing client/server clocks-// per-block would lie, so this only ever combines same-source timestamps.-function spanIso(blocks: BlockState[]): {- start: string | null;- end: string | null;-} {- let startMs: number | null = null;- let start: string | null = null;- let endMs: number | null = null;- let end: string | null = null;- for (const b of blocks) {- const sMs = parseUtcIsoMs(b.startedAt);- if (sMs !== null && (startMs === null || sMs < startMs)) {- startMs = sMs;- start = b.startedAt;- }- const eMs = parseUtcIsoMs(b.endedAt);- if (eMs !== null && (endMs === null || eMs > endMs)) {- endMs = eMs;- end = b.endedAt;- }- }- return { start, end };-}-export function hasFailedTestBlock(turn: TurnNarrativeState): boolean {return latestBlocksByLabel(turn.blocks).filter((b) => b.state !== "drafted")@@ -189,168 +33,3 @@ export function everyTestBlockExecuted(turn: TurnNarrativeState): boolean {const proposed = review.blocks.filter((b) => b.change !== "removed");return proposed.length > 0 && proposed.every((b) => b.neverTested !== true);}--export function derivePhases(turn: TurnNarrativeState): PhaseRowModel[] {- const {- explore,- draft: draftEntries,- test,- authoringSeen,- } = bucketActivity(turn.designActivity);- const lastAuthoring = lastAuthoringToolName(turn.designActivity);- const latestBlocks = latestBlocksByLabel(turn.blocks).filter(- (b) => b.state !== "drafted",- );- const testReached =- latestBlocks.length > 0 ||- (turn.designEnded &&- lastAuthoring !== undefined &&- RUN_TOOLS.has(lastAuthoring));- // Explore completes only on recorded authoring (authoringCount live /- // authoringSeen after hydration resets the client-only count) or a run.- // draftingSignaledAt can fire on a mid-scout pause and designEnded is forced- // true at every terminal, so neither completes Explore mid-turn; turn.draft- // resolves the phases only at a healthy, non-cancelled response terminal.- const draftReached =- turn.authoringCount > 0 ||- authoringSeen ||- testReached ||- (turn.terminal === "response" &&- turn.cancelled !== true &&- turn.draft !== null);-- const running = turn.blocks.some(- (b) => b.state === "running" || b.state === "queued",- );- const lastRunOutcome = turn.lastRunOutcome;-- const chainActive: CopilotPhaseId = testReached- ? "test"- : draftReached- ? "draft"- : "explore";- const liveActive: CopilotPhaseId | null =- turn.terminal !== null- ? null- : running || lastRunOutcome?.verdict === "evaluating"- ? "test"- : chainActive;-- const isTerminal = turn.terminal !== null;- const isCancelled = turn.cancelled === true;- const deadlineHalt = isDeadlineHalt(turn);- // A cancelled turn and a deadline halt both land on terminal "error" — that- // error is the stop itself, so neither may paint the rail red.- const isError = turn.terminal === "error" && !isCancelled && !deadlineHalt;- const anyFailed = hasFailedTestBlock(turn);- const anyStopped = latestBlocks.some((b) => b.state === "stopped");- const anyNotDemonstrated = latestBlocks.some(- (b) => b.outcome === "not_demonstrated" && !isInterimOutcome(b.outcomeRole),- );-- function reached(id: CopilotPhaseId): boolean {- if (id === "explore") return true;- if (id === "draft") return draftReached;- return testReached;- }-- function statusFor(id: CopilotPhaseId): PhaseStatus {- if (id === "done") {- if (!isTerminal) return "pending";- if (turn.terminal === "response" && !isCancelled) return "done";- return isError ? "fail" : "stopped";- }- // A hard block failure wins regardless of live/terminal — unless this- // phase is still the live-active row (a block is still running/queued).- if (id === "test" && testReached && anyFailed && id !== liveActive) {- return "fail";- }- // A stopped block halted the test without failing it, so the rail must not- // fall through to "done" and claim the run finished.- if (id === "test" && testReached && anyStopped && id !== liveActive) {- return "stopped";- }- if (!isTerminal) {- return id === liveActive ? "active" : reached(id) ? "done" : "pending";- }- if (!reached(id)) return id === "test" ? "notrun" : "pending";- if (chainActive === id) {- if (isError) return "fail";- if (isCancelled || deadlineHalt) return "stopped";- }- return "done";- }-- // Condensed for display only; phase derivation reads the raw activity groups.- const entriesFor: Record<CopilotPhaseId, ActivityEntry[]> = {- explore: condenseActivityEntries(explore),- draft: condenseActivityEntries(draftEntries),- test: condenseActivityEntries(test),- done: [],- };-- function stubFor(id: CopilotPhaseId, status: PhaseStatus): string | null {- if (id === "done") return null;- if (id === "explore") {- if (status !== "done" && status !== "fail" && status !== "stopped") {- return null;- }- // Condensed count, not raw: a folded retry now renders as one row,- // so the stub must match what expanding the row actually shows.- const n = entriesFor.explore.filter(- (e) => e.toolName !== undefined,- ).length;- return n > 0 ? pluralize(n, "step") : null;- }- if (id === "draft") {- if (status !== "done" && status !== "fail" && status !== "stopped") {- return null;- }- if (!turn.draft) return null;- const base = pluralize(turn.draft.blockCount, "block");- if (!isTerminal) return base;- // Intentionally a raw authoring-attempt count, not a rendered-row- // count: it spans update_workflow (draft bucket) AND- // update_and_run_blocks (test bucket), so there's no single- // condensed bucket to count against the way the explore stub does.- const drafts = countToolCalls(turn.designActivity, AUTHORING_TOOLS);- return drafts >= 2 ? `${base} · ${drafts} drafts` : base;- }- // test- if (status === "notrun") return "not run";- if (status === "pending" || status === "active") return null;- if (status === "fail")- return `${pluralize(latestBlocks.length, "block")} · failed`;- if (status === "stopped") {- const halt = deadlineHalt ? "time limit" : "stopped";- return latestBlocks.length > 0- ? `${pluralize(latestBlocks.length, "block")} · ${halt}`- : halt;- }- if (anyNotDemonstrated) {- // Surface how many test runs the loop actually made — otherwise a- // 6-run "not confirmed" turn looks identical to a 1-run one. Omit at 1 to- // keep today's look (meaningful-or-nothing).- const runs = countToolCalls(turn.designActivity, RUN_TOOLS);- return runs >= 2- ? `${pluralize(runs, "run")} · not confirmed`- : "· not confirmed";- }- const { start, end } = spanIso(latestBlocks);- const elapsed = formatElapsed(start, end);- return elapsed- ? `${pluralize(latestBlocks.length, "block")} · ${elapsed}`- : pluralize(latestBlocks.length, "block");- }-- return (["explore", "draft", "test", "done"] as const).map((id) => {- const status = statusFor(id);- return {- id,- label: PHASE_LABELS[id],- status,- stub: stubFor(id, status),- entries: entriesFor[id],- };- });-}diff --git a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.phaseHint.test.ts b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.phaseHint.test.tsindex 7f1f40dd2..489ad7692 100644--- a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.phaseHint.test.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.phaseHint.test.ts@@ -1,14 +1,12 @@import { describe, expect, it } from "vitest";import {- CopilotPhaseHintEvent,EMPTY_NARRATIVE,TurnNarrativeState,applyNarrativeEvent,hydrateNarrativeFromPayload,} from "./narrativeState";import {- WorkflowCopilotBlockProgressUpdate,WorkflowCopilotRunOutcomeUpdate,WorkflowCopilotStreamResponseUpdate,WorkflowCopilotToolCallUpdate,@@ -34,18 +32,6 @@ const toolCall = (...overrides,});-const blockProgress = (- overrides: Partial<WorkflowCopilotBlockProgressUpdate> &- Pick<WorkflowCopilotBlockProgressUpdate, "block_label" | "status">,-): WorkflowCopilotBlockProgressUpdate => ({- type: "block_progress",- workflow_run_block_id: `wrb_${overrides.block_label}`,- block_type: "task",- iteration: 0,- timestamp: "2026-06-10T00:00:04Z",- ...overrides,-});-const runOutcome = (overrides: Partial<WorkflowCopilotRunOutcomeUpdate> &Pick<WorkflowCopilotRunOutcomeUpdate, "verdict">,@@ -72,111 +58,33 @@ const response = (...overrides,});-const phaseHint = (hintedAtMs: number): CopilotPhaseHintEvent => ({- type: "client_phase_hint",- hintedAtMs,-});--describe("applyNarrativeEvent — client_phase_hint", () => {- it("sets draftingSignaledAt once", () => {- const s = applyNarrativeEvent(EMPTY_NARRATIVE, phaseHint(5000));- expect(s.draftingSignaledAt).toBe(5000);- });-- it("is idempotent — a second hint never overwrites the first timestamp", () => {- let s: TurnNarrativeState = applyNarrativeEvent(- EMPTY_NARRATIVE,- phaseHint(5000),- );- s = applyNarrativeEvent(s, phaseHint(9000));- expect(s.draftingSignaledAt).toBe(5000);- });-- it("no-ops once a block has started running (design already progressed)", () => {+describe("applyNarrativeEvent — designActivity", () => {+ it("keeps denylisted internal tools out of the visible activity log", () => {let s: TurnNarrativeState = applyNarrativeEvent(- EMPTY_NARRATIVE,- turnStart(),- );- s = applyNarrativeEvent(- s,- blockProgress({ block_label: "block_1", status: "running" }),- );- s = applyNarrativeEvent(s, phaseHint(5000));- expect(s.draftingSignaledAt).toBeNull();- });-- it("no-ops once designEnded", () => {- let s: TurnNarrativeState = applyNarrativeEvent(EMPTY_NARRATIVE, {- type: "design_end",- timestamp: "2026-06-10T00:00:02Z",- });- s = applyNarrativeEvent(s, phaseHint(5000));- expect(s.draftingSignaledAt).toBeNull();- });-- it("no-ops once a draft already exists", () => {- let s: TurnNarrativeState = applyNarrativeEvent(EMPTY_NARRATIVE, {- type: "workflow_draft",- block_count: 1,- block_labels: ["block_1"],- summary: null,- timestamp: "2026-06-10T00:00:03Z",- });- s = applyNarrativeEvent(s, phaseHint(5000));- expect(s.draftingSignaledAt).toBeNull();- });-});--describe("applyNarrativeEvent — lastActivityAtMs", () => {- it("stamps with the injected nowMs on tool_call/tool_result/narration", () => {- const s = applyNarrativeEvent(EMPTY_NARRATIVE, toolCall(), 12345);- expect(s.lastActivityAtMs).toBe(12345);- });-- it("stamps even when the tool is denylisted from the visible activity log", () => {- const s = applyNarrativeEvent(EMPTY_NARRATIVE,toolCall({ tool_name: "get_run_results" }),- 777,);- expect(s.lastActivityAtMs).toBe(777);+ s = applyNarrativeEvent(s, {+ type: "tool_result",+ tool_name: "get_browser_screenshot",+ success: true,+ summary: "",+ iteration: 0,+ tool_call_id: "call-2",+ });expect(s.designActivity).toHaveLength(0);});- it("defaults to Date.now() when nowMs is omitted", () => {- const before = Date.now();- const s = applyNarrativeEvent(EMPTY_NARRATIVE, toolCall());- expect(s.lastActivityAtMs).toBeGreaterThanOrEqual(before);- });-});--describe("applyNarrativeEvent — authoringCount", () => {- it("increments only on AUTHORING_TOOLS tool_calls", () => {- let s: TurnNarrativeState = applyNarrativeEvent(- EMPTY_NARRATIVE,- toolCall({ tool_name: "navigate_browser" }),- );- expect(s.authoringCount).toBe(0);- s = applyNarrativeEvent(s, toolCall({ tool_name: "update_workflow" }));- expect(s.authoringCount).toBe(1);- s = applyNarrativeEvent(- s,- toolCall({ tool_name: "update_and_run_blocks", tool_call_id: "call-2" }),- );- expect(s.authoringCount).toBe(2);- });-- it("survives the MAX_DESIGN_ACTIVITY_ENTRIES eviction cap (does not reset when old entries scroll off)", () => {+ it("evicts the oldest entries at the MAX_DESIGN_ACTIVITY_ENTRIES cap", () => {let s: TurnNarrativeState = EMPTY_NARRATIVE;- s = applyNarrativeEvent(s, toolCall({ tool_name: "update_workflow" }));- for (let i = 0; i < 60; i++) {+ for (let i = 0; i < 61; i++) {s = applyNarrativeEvent(s,toolCall({ tool_name: "navigate_browser", tool_call_id: `c${i}` }),);}- expect(s.authoringCount).toBe(1);expect(s.designActivity.length).toBeLessThan(61);+ expect(s.designActivity[s.designActivity.length - 1]?.id).toBe("tc-c60");});});@@ -210,77 +118,13 @@ describe("applyNarrativeEvent — lastRunOutcome", () => {});});-describe("applyNarrativeEvent — response hydration resets phase-hint fields", () => {- it("regression pin: a response frame for the SAME turnId preserves draftingSignaledAt (the terminal-wipe trap)", () => {- let s: TurnNarrativeState = applyNarrativeEvent(- EMPTY_NARRATIVE,- turnStart(),- );- s = applyNarrativeEvent(s, phaseHint(5000));- expect(s.draftingSignaledAt).toBe(5000);-- s = applyNarrativeEvent(- s,- response({- turn_id: "turn-1",- narrative_payload: {- turnId: "turn-1",- turnIndex: 0,- designStarted: true,- designEnded: true,- draft: null,- blocks: [],- terminal: "response",- terminalMessage: "Done.",- narrativeSummary: "Done.",- priorBlockCount: null,- designActivity: [],- startedAt: "2026-06-10T00:00:00Z",- endedAt: "2026-06-10T00:02:00Z",- },- }),- );- expect(s.draftingSignaledAt).toBe(5000);- });-- it("no graft for a DIFFERENT turnId — fresh terminal payload starts at null", () => {- let s: TurnNarrativeState = applyNarrativeEvent(- EMPTY_NARRATIVE,- turnStart(),- );- s = applyNarrativeEvent(s, phaseHint(5000));-- s = applyNarrativeEvent(- s,- response({- narrative_payload: {- turnId: "turn-2",- turnIndex: 1,- designStarted: true,- designEnded: true,- draft: null,- blocks: [],- terminal: "response",- terminalMessage: "Done.",- narrativeSummary: "Done.",- priorBlockCount: null,- designActivity: [],- startedAt: "2026-06-10T00:00:00Z",- endedAt: "2026-06-10T00:02:00Z",- },- }),- );- expect(s.draftingSignaledAt).toBeNull();- });-- it("authoringCount is grafted across the terminal swap; lastRunOutcome is not", () => {+describe("applyNarrativeEvent — response hydration", () => {+ it("lastRunOutcome is not grafted across the terminal swap", () => {let s: TurnNarrativeState = applyNarrativeEvent(EMPTY_NARRATIVE,turnStart(),);- s = applyNarrativeEvent(s, toolCall({ tool_name: "update_workflow" }));s = applyNarrativeEvent(s, runOutcome({ verdict: "not_demonstrated" }));- expect(s.authoringCount).toBe(1);expect(s.lastRunOutcome).not.toBeNull();s = applyNarrativeEvent(@@ -304,11 +148,6 @@ describe("applyNarrativeEvent — response hydration resets phase-hint fields",},}),);- // Grafted (same turnId): the uncapped counter survives even though the- // payload's capped designActivity has aged out the authoring entry, so- // Explore stays resolved at the swap rather than flipping back.- expect(s.authoringCount).toBe(1);- // Still not grafted: the terminal payload owns the final run fact.expect(s.lastRunOutcome).toBeNull();});});diff --git a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.tsindex ff0a80ff7..d50f85a99 100644--- a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts@@ -97,15 +97,6 @@ export interface CopilotBlockActionsEvent {receivedAtMs: number;}-// Client-synthesized event marking that the drafting silence (no frames-// while the LLM writes code) has lasted long enough to assume Draft has-// started. Idempotent in the reducer so a re-armed timer or StrictMode-// double-fire is a no-op.-export interface CopilotPhaseHintEvent {- type: "client_phase_hint";- hintedAtMs: number;-}-// Discriminated union of every event the reducer below consumes. The bubble// derives all of its rendering from these payloads.export type NarrativeEvent =@@ -120,8 +111,7 @@ export type NarrativeEvent =| WorkflowCopilotNarrationUpdate| WorkflowCopilotToolCallUpdate| WorkflowCopilotToolResultUpdate- | CopilotBlockActionsEvent- | CopilotPhaseHintEvent;+ | CopilotBlockActionsEvent;// Block lifecycle states as observed via block_progress. The bubble groups// failed-style states (failed, terminated, timed_out) under one chip and@@ -409,15 +399,6 @@ export interface TurnNarrativeState {// Activity events fired BEFORE any block started running this turn// (design phase + pre-execution tool calls), rendered inside the Design card.designActivity: ActivityEntry[];- // Client-only phase-progress state (never persisted): epoch ms of the most- // recent tool_call/tool_result/narration, and when the 8s drafting-gap- // heuristic fired. Grafted across the terminal payload swap by turnId so a- // cancel-mid-silence doesn't visually un-check the Draft phase.- lastActivityAtMs: number | null;- draftingSignaledAt: number | null;- // Count of AUTHORING_TOOLS tool_calls this turn, kept outside designActivity- // so it survives the MAX_DESIGN_ACTIVITY_ENTRIES eviction cap.- authoringCount: number;// Snapshot of the most recent factual run outcome.lastRunOutcome: {verdict: BlockOutcome;@@ -469,9 +450,6 @@ export const EMPTY_NARRATIVE: TurnNarrativeState = Object.freeze({startedAt: null,endedAt: null,designActivity: [],- lastActivityAtMs: null,- draftingSignaledAt: null,- authoringCount: 0,lastRunOutcome: null,credentialPrompt: null,credentialPause: null,@@ -650,8 +628,7 @@ export function parseGoogleConnectionNotices(// Tool calls that write the workflow definition. update_workflow only// validates/saves the draft; update_and_run_blocks also runs it, so it's-// the one AUTHORING_TOOLS member that's also a RUN_TOOLS member (its-// activity lands in the Test phase bucket, not Draft — see copilotPhases.ts).+// the one AUTHORING_TOOLS member that's also a RUN_TOOLS member.export const AUTHORING_TOOLS = new Set(["update_workflow","update_and_run_blocks",@@ -1277,13 +1254,9 @@ export function applyNarrativeEvent(case "tool_call": {const entry = buildActivityFromToolCall(event);- const authoringCount =- prev.authoringCount + (AUTHORING_TOOLS.has(event.tool_name) ? 1 : 0);if (!entry) {return {...prev,- lastActivityAtMs: nowMs,- authoringCount,};}const { blocks, designActivity } = appendActivity(@@ -1295,14 +1268,12 @@ export function applyNarrativeEvent(...prev,blocks,designActivity,- lastActivityAtMs: nowMs,- authoringCount,};}case "tool_result": {const entry = buildActivityFromToolResult(event);- if (!entry) return { ...prev, lastActivityAtMs: nowMs };+ if (!entry) return { ...prev };const { blocks, designActivity } = appendActivity(prev.blocks,prev.designActivity,@@ -1312,7 +1283,6 @@ export function applyNarrativeEvent(...prev,blocks,designActivity,- lastActivityAtMs: nowMs,};}@@ -1327,25 +1297,9 @@ export function applyNarrativeEvent(...prev,blocks,designActivity,- lastActivityAtMs: nowMs,};}- case "client_phase_hint": {- // No-op once drafting is already signaled or the turn has moved past- // pure exploration — idempotent by construction so a re-armed timer or- // a StrictMode double-fire never overwrites an earlier timestamp.- if (- prev.draftingSignaledAt !== null ||- prev.draft !== null ||- prev.designEnded ||- prev.blocks.some((b) => b.state !== "drafted")- ) {- return prev;- }- return { ...prev, draftingSignaledAt: event.hintedAtMs };- }-case "response": {const hydrated = hydrateNarrativeFromPayload(event.narrative_payload);if (hydrated) {@@ -1388,18 +1342,6 @@ export function applyNarrativeEvent(return {...hydrated,blocks,- // Graft across the terminal replacement so a cancel mid-silence- // doesn't visually un-check the Draft phase (hydrated payloads never- // carry these client-only fields). authoringCount is grafted too so a- // turn whose only authoring entry aged out of the capped activity- // list still completes Explore at the swap. lastRunOutcome remains- // sourced from the hydrated terminal payload.- draftingSignaledAt:- hydrated.turnId === prev.turnId ? prev.draftingSignaledAt : null,- authoringCount:- hydrated.turnId === prev.turnId- ? prev.authoringCount- : hydrated.authoringCount,responseType: event.response_type ?? hydrated.responseType,cancelled: event.cancelled ?? hydrated.cancelled,proposalDisposition:diff --git a/skyvern-frontend/src/routes/workflows/copilot/proposalDisposition.ts b/skyvern-frontend/src/routes/workflows/copilot/proposalDisposition.tsindex 642432e3e..ef2cd6fc8 100644--- a/skyvern-frontend/src/routes/workflows/copilot/proposalDisposition.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/proposalDisposition.ts@@ -2,12 +2,12 @@ import type { WorkflowCopilotStreamResponseUpdate } from "./workflowCopilotTypesexport const shouldAutoApplyWorkflowResponse = (response: WorkflowCopilotStreamResponseUpdate,- autoAccept: boolean,userCancelledThisTurn: boolean,) => {return Boolean(response.updated_workflow &&- (autoAccept || response.workflow_applied === true) &&+ // The server decides: it wrote the row and committed (or did not commit) canonical for this turn.+ response.workflow_applied === true &&response.proposal_disposition === "auto_applicable" &&!response.cancelled &&!userCancelledThisTurn,diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.pyindex 63be79bd2..96cd13251 100644--- a/skyvern/forge/sdk/routes/workflow_copilot.py+++ b/skyvern/forge/sdk/routes/workflow_copilot.py@@ -108,6 +108,7 @@ from skyvern.forge.sdk.schemas.workflow_copilot import (WorkflowCopilotChatSummary,WorkflowCopilotClearProposedWorkflowRequest,WorkflowCopilotCredentialResponseRequest,+ WorkflowCopilotDisableAutoAcceptRequest,WorkflowCopilotProcessingUpdate,WorkflowCopilotQuestionResponseRequest,WorkflowCopilotStreamErrorUpdate,@@ -1213,7 +1214,7 @@ async def _persist_interrupted_turn(narrative_payload = _with_terminal_narrative_metadata(_make_error_narrative_payload(turn_id, None, message),# An interrupted turn halted; it did not fail. The FE reads this flag to- # keep the row out of failure treatment (derivePhases, copilotPhases.ts).+ # keep the row out of failure treatment.cancelled=True,proposal_disposition=_proposal_disposition(None),)@@ -1452,6 +1453,19 @@ async def _persist_cancel_turn()+async def _honor_auto_accept_turned_off_mid_turn(chat: Any) -> None:+ # ``chat`` was read when the turn started. Only a switch to off is honored: a turn that staged for+ # review told the model so, and a mid-turn opt-in must not apply that draft unseen.+ if chat.auto_accept is not True:+ return+ current_chat = await app.DATABASE.workflow_params.get_workflow_copilot_chat_by_id(+ organization_id=chat.organization_id,+ workflow_copilot_chat_id=chat.workflow_copilot_chat_id,+ )+ if current_chat is not None and current_chat.auto_accept is not True:+ chat.auto_accept = current_chat.auto_accept++async def _finalise_normal_turn(stream: EventSourceStream,chat: Any,@@ -1493,6 +1507,7 @@ async def _finalise_normal_turn(# panel state below stays gated on auto_accept — the frontend# applies proposals via applyWorkflowUpdate when auto-accept is# on.+ await _honor_auto_accept_turned_off_mid_turn(chat)restored = _should_restore_persisted_workflow(chat.auto_accept, agent_result)restore_failed = Falseif restored:@@ -3408,6 +3423,23 @@ async def workflow_copilot_clear_proposed_workflow(raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Copilot proposal changed; reload required")+@base_router.post(+ "/workflow/copilot/disable-auto-accept", include_in_schema=False, status_code=status.HTTP_204_NO_CONTENT+)+async def workflow_copilot_disable_auto_accept(+ disable_request: WorkflowCopilotDisableAutoAcceptRequest,+ organization: Organization = Depends(org_auth_service.get_current_org),+) -> None:+ # Settings-only write: the clear route would also discard a pending review the user has not answered.+ chat = await app.DATABASE.workflow_params.update_workflow_copilot_chat(+ organization_id=organization.organization_id,+ workflow_copilot_chat_id=disable_request.workflow_copilot_chat_id,+ auto_accept=False,+ )+ if chat is None:+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Chat not found")++@base_router.post("/workflow/copilot/apply-proposed-workflow", include_in_schema=False)async def workflow_copilot_apply_proposed_workflow(apply_request: WorkflowCopilotApplyProposedWorkflowRequest,diff --git a/skyvern/forge/sdk/schemas/workflow_copilot.py b/skyvern/forge/sdk/schemas/workflow_copilot.pyindex 0dc30009e..62b94bbe8 100644--- a/skyvern/forge/sdk/schemas/workflow_copilot.py+++ b/skyvern/forge/sdk/schemas/workflow_copilot.py@@ -402,6 +402,10 @@ class WorkflowCopilotClearProposedWorkflowRequest(BaseModel):revision: int | None = Field(None, ge=1, description="Revision token returned with a typed proposal")+class WorkflowCopilotDisableAutoAcceptRequest(BaseModel):+ workflow_copilot_chat_id: str = Field(..., description="The chat whose auto-accept should be turned off")++class WorkflowCopilotApplyProposedWorkflowRequest(BaseModel):workflow_copilot_chat_id: str = Field(..., description="The chat whose proposed workflow should be applied")auto_accept: bool = Field(diff --git a/tests/unit/test_workflow_copilot_route.py b/tests/unit/test_workflow_copilot_route.pyindex ab76c6e11..64788947c 100644--- a/tests/unit/test_workflow_copilot_route.py+++ b/tests/unit/test_workflow_copilot_route.py@@ -19,6 +19,7 @@ from unittest.mock import AsyncMock, MagicMockimport pytestfrom fastapi import HTTPException+from sqlalchemy.ext.asyncio import AsyncEnginefrom skyvern.config import settingsfrom skyvern.forge import app@@ -51,8 +52,10 @@ from skyvern.forge.sdk.copilot.interruption import (InterruptedTurnFacts,)from skyvern.forge.sdk.copilot.turn_outcome import build_minimal_turn_outcome+from skyvern.forge.sdk.db.base_alchemy_db import BaseAlchemyDBfrom skyvern.forge.sdk.db.exceptions import CopilotProposalConflictError, DatabaseConnectionUnavailableErrorfrom skyvern.forge.sdk.db.repositories.workflow_parameters import (+ WorkflowParametersRepository,_completed_turn_id_for_idempotency_digest,_pending_turn_id_for_idempotency_digest,)@@ -78,6 +81,7 @@ from skyvern.forge.sdk.schemas.workflow_copilot import (WorkflowCopilotChatRequest,WorkflowCopilotChatSender,WorkflowCopilotClearProposedWorkflowRequest,+ WorkflowCopilotDisableAutoAcceptRequest,WorkflowCopilotStreamErrorUpdate,WorkflowCopilotStreamResponseUpdate,)@@ -664,7 +668,9 @@ async def test_auto_accept_does_not_commit_canonical_for_a_superseded_candidate(agent_result.proposal_owner_turn_id = "turn-stale"agent_result.proposal_revision = 1_, workflow_params = setup_new_copilot_mocks(monkeypatch, chat, original_workflow, agent_result)- workflow_params.get_workflow_copilot_chat_by_id = AsyncMock(return_value=SimpleNamespace(proposed_workflow=newer))+ workflow_params.get_workflow_copilot_chat_by_id = AsyncMock(+ return_value=SimpleNamespace(proposed_workflow=newer, auto_accept=True)+ )commit = AsyncMock()monkeypatch.setattr(workflow_copilot_route, "_commit_staged_workflow", commit)stream = MagicMock(send=AsyncMock(return_value=True))@@ -818,6 +824,100 @@ async def test_a_client_without_proposal_tokens_can_still_reject(assert clear.await_args.kwargs["expected_revision"] == 3+async def test_turning_off_auto_accept_mid_turn_keeps_the_pending_review_and_gates_the_turns_proposal(+ sqlite_engine: AsyncEngine, monkeypatch: pytest.MonkeyPatch+) -> None:+ """Reusing the reject route would discard a review the user has not answered yet, and a write that+ leaves the row alone would auto-apply the next verified fix after the user switched it off."""+ repo = WorkflowParametersRepository(BaseAlchemyDB(sqlite_engine).Session)+ monkeypatch.setattr(app.DATABASE, "workflow_params", repo)+ monkeypatch.setattr(app, "CACHE", None)+ canonical = MagicMock()+ canonical.model_dump.return_value = {"title": "Canonical"}+ canonical_fingerprint = workflow_content_fingerprint({"title": "Canonical"})+ monkeypatch.setattr(+ app.DATABASE,+ "workflows",+ SimpleNamespace(get_workflow_by_permanent_id=AsyncMock(return_value=canonical)),+ )+ organization = SimpleNamespace(organization_id="org-1")+ chat = await repo.create_workflow_copilot_chat(organization_id="org-1", workflow_permanent_id="wpid-1")+ chat_id = chat.workflow_copilot_chat_id+ await repo.publish_workflow_copilot_candidate(+ "org-1",+ chat_id,+ proposal={"title": "Pending review", "_copilot_yaml": "title: Pending review\n"},+ owner_turn_id="turn-a",+ canonical_fingerprint=canonical_fingerprint,+ disposition="review_untested",+ expected_owner_turn_id=None,+ expected_revision=None,+ )+ await repo.update_workflow_copilot_chat("org-1", chat_id, auto_accept=True)+ # A turn already running when the user clicks Turn off holds the row it read at turn start.+ turn_start_chat = await repo.get_workflow_copilot_chat_by_id(+ organization_id="org-1", workflow_copilot_chat_id=chat_id+ )+ assert turn_start_chat is not None and turn_start_chat.auto_accept is True++ await workflow_copilot_route.workflow_copilot_disable_auto_accept(+ WorkflowCopilotDisableAutoAcceptRequest(workflow_copilot_chat_id=chat_id), organization+ )++ reloaded = await workflow_copilot_chat_history(+ credential_recovery_token=None, workflow_copilot_chat_id=chat_id, organization=organization+ )+ assert reloaded.auto_accept is False+ assert reloaded.proposed_workflow_metadata is not None+ assert reloaded.proposed_workflow_metadata.owner_turn_id == "turn-a"++ # That turn's tool then publishes a verified fix over the candidate.+ await repo.publish_workflow_copilot_candidate(+ "org-1",+ chat_id,+ proposal={"title": "Verified fix", "_copilot_yaml": "title: Verified fix\n"},+ owner_turn_id="turn-b",+ canonical_fingerprint=canonical_fingerprint,+ disposition="auto_applicable",+ expected_owner_turn_id="turn-a",+ expected_revision=1,+ )+ updated_workflow = MagicMock(title="Verified fix")+ updated_workflow.model_dump.return_value = {"title": "Verified fix"}+ agent_result = AgentResult(+ user_response="Fixed it.",+ updated_workflow=updated_workflow,+ global_llm_context=None,+ response_type="REPLY",+ proposal_disposition="auto_applicable",+ proposal_owner_turn_id="turn-b",+ proposal_revision=1,+ narrative_payload=_narrative_payload(),+ )+ stream = MagicMock(send=AsyncMock(return_value=True))++ await workflow_copilot_route._finalise_normal_turn(+ stream=stream,+ chat=turn_start_chat,+ organization_id="org-1",+ original_workflow=SimpleNamespace(workflow_id="wf-canonical"),+ chat_request=_make_chat_request(),+ agent_result=agent_result,+ )++ frame = stream.send.await_args.args[0]+ assert isinstance(frame, WorkflowCopilotStreamResponseUpdate)+ assert frame.updated_workflow is not None+ assert frame.workflow_applied is False+ after_turn = await workflow_copilot_chat_history(+ credential_recovery_token=None, workflow_copilot_chat_id=chat_id, organization=organization+ )+ assert after_turn.auto_accept is False+ assert after_turn.proposed_workflow_metadata is not None+ assert after_turn.proposed_workflow_metadata.owner_turn_id == "turn-b"++def test_chat_history_serves_a_saved_envelope_back_untouched() -> None:now = datetime.now(timezone.utc)payload = {