Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Register per-org OpenRouter gpt-5.6-luna configs and fix the responses-bridge guard they expose (SKY-16623) (#8618)
pedrohsdb32618406e87d
diff --git a/skyvern-frontend/src/api/AxiosClient.ts b/skyvern-frontend/src/api/AxiosClient.tsindex e6e33c5e6..c54afd7ab 100644--- a/skyvern-frontend/src/api/AxiosClient.ts+++ b/skyvern-frontend/src/api/AxiosClient.ts@@ -57,6 +57,49 @@ const artifactApiClient = axios.create({const clients = [client, v2Client, clientSansApiV1] as const;+type AttributionResolver = (+ authorization: string | undefined,+) => string | undefined;+let attributionResolver: AttributionResolver | undefined;++// Cloud supplies the ownership check; shared API code has no analytics dependency.+export function setPostHogAttributionResolver(+ resolver: AttributionResolver,+): void {+ attributionResolver = resolver;+ removeHeaderForAllClients(POSTHOG_ATTRIBUTION_HEADER);+}++export function getPostHogAttributionHeaders(+ authorization: string,+): Record<string, string> {+ try {+ const value = attributionResolver?.(authorization);+ return value ? { [POSTHOG_ATTRIBUTION_HEADER]: value } : {};+ } catch {+ return {};+ }+}++clients.forEach((instance) => {+ instance.interceptors.request.use((config) => {+ if (attributionResolver) {+ const authorization = config.headers.get("Authorization");+ const attribution = getPostHogAttributionHeaders(+ typeof authorization === "string" ? authorization : "",+ );+ config.headers.delete(POSTHOG_ATTRIBUTION_HEADER);+ if (attribution[POSTHOG_ATTRIBUTION_HEADER]) {+ config.headers.set(+ POSTHOG_ATTRIBUTION_HEADER,+ attribution[POSTHOG_ATTRIBUTION_HEADER],+ );+ }+ }+ return config;+ });+});+type UISessionResponse = {token: string;expires_at: number;@@ -298,14 +341,6 @@ function removeHeaderForAllClients(header: string) {});}-export function setPostHogAttributionHeader(value: string | undefined) {- if (value) {- setHeaderForAllClients(POSTHOG_ATTRIBUTION_HEADER, value);- } else {- removeHeaderForAllClients(POSTHOG_ATTRIBUTION_HEADER);- }-}-export function setAuthorizationHeader(token: string) {setHeaderForAllClients("Authorization", `Bearer ${token}`);}diff --git a/skyvern-frontend/src/api/types.ts b/skyvern-frontend/src/api/types.tsindex f875e906c..05f256ce9 100644--- a/skyvern-frontend/src/api/types.ts+++ b/skyvern-frontend/src/api/types.ts@@ -684,8 +684,28 @@ export type ActionApiResponse = {screenshot_artifact_id?: string | null;};+export type ActionSummaryBody = {+ // Trimmed but not collapsed: paragraph breaks survive for a card that renders blocks. A caller+ // rendering one line must collapse them itself (normalizeInlineText) — InlineMarkdown joins+ // paragraphs with no separator at all.+ text: string;+ // Only the model's own prose is markdown. A typed value or a recorded outcome is literal text and+ // must render verbatim — a password containing "*" is not emphasis.+ isProse: boolean;+};++export type ActionSummary = {+ // What the action set out to do.+ body: ActionSummaryBody | null;+ // What it actually did, when the run recorded it. Shown beside the body, never instead of it: a+ // card that let the plan stand for the effect reads a dead end as a successful navigation.+ outcome: string | null;+};+export type Action = {- reasoning: string;+ // The text the card shows, already resolved through getActionSummary: a Task V3 action often+ // carries no reasoning, and then its intention or its recorded outcome is all it has.+ summary: ActionSummary | null;confidence?: number;type: ActionType;input: string;diff --git a/skyvern-frontend/src/routes/tasks/detail/ScrollableActionList.test.tsx b/skyvern-frontend/src/routes/tasks/detail/ScrollableActionList.test.tsxindex 7c5981dd5..404296f14 100644--- a/skyvern-frontend/src/routes/tasks/detail/ScrollableActionList.test.tsx+++ b/skyvern-frontend/src/routes/tasks/detail/ScrollableActionList.test.tsx@@ -1,7 +1,7 @@// @vitest-environment jsdomimport { cleanup, render, screen } from "@testing-library/react";-import { afterEach, describe, it, vi } from "vitest";+import { afterEach, describe, expect, it, vi } from "vitest";import { QueryClient, QueryClientProvider } from "@tanstack/react-query";vi.mock("@/api/AxiosClient", () => ({@@ -19,7 +19,10 @@ import { ScrollableActionList } from "./ScrollableActionList";function buildAction(overrides: Partial<Action> = {}): Action {return {- reasoning: "Click the submit button",+ summary: {+ body: { text: "Click the submit button", isProse: true },+ outcome: null,+ },confidence: 0.9,type: ActionTypes.Click,input: "",@@ -66,4 +69,62 @@ describe("ScrollableActionList", () => {screen.getByText("Paste Text");});++ // A goto_url row's only text is its intention or its outcome, and an input_text row's is the+ // value typed: neither is markdown, and a value containing "*" is not emphasis.+ it("renders a summary that is not the model's prose verbatim", () => {+ const { container } = renderList(+ buildAction({+ summary: {+ body: {+ text: "https://example.com/a*b*c (HTTP 404, dead end)",+ isProse: false,+ },+ outcome: null,+ },+ }),+ );++ screen.getByText("https://example.com/a*b*c (HTTP 404, dead end)");+ expect(container.querySelector("em")).toBeNull();+ });++ // The card that showed only the plan read a dead end as a completed navigation.+ it("shows the recorded outcome next to what the action set out to do", () => {+ renderList(+ buildAction({+ type: ActionTypes.GotoUrl,+ summary: {+ body: {+ text: "Tried to navigate to https://example.com/contact-us/",+ isProse: true,+ },+ outcome: "https://example.com/contact-us/ (HTTP 404, dead end)",+ },+ }),+ );++ screen.getByText(+ /Tried to navigate to https:\/\/example\.com\/contact-us\//,+ );+ screen.getByText(/HTTP 404, dead end/);+ });++ // A provider summary arrives as multiple paragraphs; collapsing them would run the heading into+ // the body it introduces.+ it("keeps a multi-paragraph reasoning in separate blocks", () => {+ const { container } = renderList(+ buildAction({+ summary: {+ body: {+ text: "**Investigating the iframe**\n\nThen reading the table",+ isProse: true,+ },+ outcome: null,+ },+ }),+ );++ expect(container.querySelectorAll("span.block").length).toBe(2);+ });});diff --git a/skyvern-frontend/src/routes/tasks/detail/ScrollableActionList.tsx b/skyvern-frontend/src/routes/tasks/detail/ScrollableActionList.tsxindex 979a812d5..3fd45a4bc 100644--- a/skyvern-frontend/src/routes/tasks/detail/ScrollableActionList.tsx+++ b/skyvern-frontend/src/routes/tasks/detail/ScrollableActionList.tsx@@ -120,9 +120,21 @@ function ScrollableActionList({)}</div></div>- <div className="text-xs text-slate-400">- <BlockMarkdown text={action.reasoning} />- </div>+ {action.summary?.body && (+ <div className="break-words text-xs text-slate-400">+ {action.summary.body.isProse ? (+ <BlockMarkdown text={action.summary.body.text} />+ ) : (+ action.summary.body.text+ )}+ </div>+ )}+ {action.summary?.outcome && (+ <div className="break-words text-xs text-slate-400">+ <span className="text-slate-500">Outcome: </span>+ {action.summary.outcome}+ </div>+ )}{action.type === ActionTypes.InputText && (<><Separator />diff --git a/skyvern-frontend/src/routes/tasks/detail/hooks/useActions.ts b/skyvern-frontend/src/routes/tasks/detail/hooks/useActions.tsindex 8ec30783b..6ca34b139 100644--- a/skyvern-frontend/src/routes/tasks/detail/hooks/useActions.ts+++ b/skyvern-frontend/src/routes/tasks/detail/hooks/useActions.ts@@ -8,6 +8,10 @@ import {TaskApiResponse,} from "@/api/types";import { isActionSuccess } from "@/routes/workflows/components/actionStatus";+import {+ getActionInputValue,+ getActionSummary,+} from "@/routes/workflows/workflowBlockUtils";import { useCredentialGetter } from "@/hooks/useCredentialGetter";import { keepPreviousData, useQuery } from "@tanstack/react-query";import { statusIsNotFinalized } from "../../types";@@ -105,7 +109,7 @@ function useActions({ id }: Props): {return null;}return {- reasoning: action.reasoning,+ summary: getActionSummary(action),confidence: action.confidence_float,input: getActionInput(action),type: action.action_type,@@ -125,9 +129,9 @@ function useActions({ id }: Props): {.flat(): taskActions?.map((action, index) => {return {- reasoning: action.reasoning ?? "",+ summary: getActionSummary(action),confidence: action.confidence_float ?? undefined,- input: action.response ?? "",+ input: getActionInputValue(action) ?? "",type: action.action_type,success: isActionSuccess(action),stepId: action.step_id ?? "",diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsxindex 2ecee4c35..d126851b4 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsx@@ -59,6 +59,14 @@ const {workflow_copilot_chat_id: "chat-1" as string | null,chat_history: [] as unknown[],proposed_workflow: null as Record<string, unknown> | null,+ proposed_claim_expires_in_seconds: null as number | null | undefined,+ proposed_workflow_metadata: null as {+ owner_turn_id: string;+ revision: number;+ canonical_fingerprint: string;+ disposition: "accepting";+ workflow_run_id: string | null;+ } | null,auto_accept: false,pending_credential_requests:[] as WorkflowCopilotCredentialRequiredUpdate[],@@ -277,7 +285,14 @@ const saveData = {vi.mock("@/store/WorkflowHasChangesStore", () => ({useWorkflowHasChangesStore: Object.assign(() => ({ getSaveData: () => saveData }),- { getState: () => ({ hasChanges: true }) },+ {+ getState: () => ({+ hasChanges: true,+ // The Accept fence publishes its reason here; this harness arms the fence, so the+ // setter has to exist or the component throws on hydration.+ setSaveBlockedReason: () => {},+ }),+ },),}));@@ -460,6 +475,8 @@ beforeEach(() => {workflow_copilot_chat_id: "chat-1",chat_history: [],proposed_workflow: null,+ proposed_claim_expires_in_seconds: null,+ proposed_workflow_metadata: null,auto_accept: false,pending_credential_requests: [],question_interactions: [],@@ -1006,6 +1023,51 @@ describe("WorkflowCopilotChat — credential card wiring", () => {);});+ it("does not receipt a terminal credential connect that the Accept fence blocked", async () => {+ credentialsData.current = [+ { credential_id: "cred_hn", name: "HN login", tested_url: null },+ ];+ // A terminal credential ask recovered from history while the server still reports a live+ // Accept claim, so the fence is already up when the card renders. Seeding from history is+ // what makes this reachable: the fence disables the composer a turn would need.+ historyResponse.data.proposed_claim_expires_in_seconds = 120;+ // The claim has to be one the row can tie to a proposal, or it is not OUR Accept and the+ // fence deliberately does not arm for it - a claim held by another writer holds Save, not+ // this chat's sending. Before the server reported claim liveness independently of the+ // proposal, a bare claim implied ours; it no longer does.+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-9",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "accepting",+ workflow_run_id: null,+ };+ historyResponse.data.chat_history = [+ {+ sender: "ai",+ content: "Connect a credential to continue.",+ created_at: new Date().toISOString(),+ narrative_payload: terminalPromptResponse("turn-9").narrative_payload,+ },+ ];+ await renderChat();++ await act(async () => {+ fireEvent.click(await screen.findByRole("combobox"));+ });+ await act(async () => {+ fireEvent.click(await screen.findByRole("button", { name: "HN login" }));+ });++ // The fence blocked the continuation, so the card must not show a success-shaped receipt+ // for a turn that did not move. The picker stays actionable so the user can choose again+ // once the fence lifts, instead of holding a checkmark for a stopped step.+ expect(postStreaming).not.toHaveBeenCalled();+ expect(screen.queryByText(/Credential .*added/)).toBeNull();+ expect(screen.queryByText("Continuing with 'HN login'…")).toBeNull();+ expect(await screen.findByRole("combobox")).toBeTruthy();+ });+it("restores the terminal picker when the auto-continue send fails", async () => {credentialsData.current = [{ credential_id: "cred_hn", name: "HN login", tested_url: null },diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.historyRace.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.historyRace.test.tsxindex 8d51d54de..8dd0b0065 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.historyRace.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.historyRace.test.tsx@@ -152,6 +152,7 @@ vi.mock("@/store/WorkflowHasChangesStore", () => {// off getState() so it never overwrites unsaved local edits.useWorkflowHasChangesStore.getState = () => ({hasChanges: hasLocalChanges.current,+ setSaveBlockedReason: () => {},});return { useWorkflowHasChangesStore };});@@ -591,7 +592,7 @@ describe("WorkflowCopilotChat — auto-accept Turn off across a chat switch", ()expect(screen.getByText("Proposed changes")).toBeTruthy();});- it("clears the accepted chat's server proposal even when the user switched away", async () => {+ it("reconciles the accepted chat's row even when the user switched away", async () => {const pendingChat = (chatId: string) =>historyData({workflow_copilot_chat_id: chatId,@@ -603,7 +604,12 @@ describe("WorkflowCopilotChat — auto-accept Turn off across a chat switch", ()},chat_history: [aiHistoryMessage(null, "Here is a draft.")],});- // The atomic apply fails, so Accept falls back to applying locally and clearing the row itself.+ // CHANGED BY THE #17099 MERGE. This asserted that a failed atomic apply fell back to a+ // CLIENT-SIDE apply which cleared the accepted chat's row itself, addressed to that chat+ // rather than the one on screen. Slice 1 removes the fallback - a failed apply now opens the+ // review gate instead - so the clear is gone. The concern is not: the work an Accept triggers+ // after it fails must still be addressed to the chat the Accept BEGAN on, never the chat the+ // user has since switched to. On this path that work is the reconciling READ of the row.let failApply: (reason: unknown) => void = () => {};cancelPost.mockImplementation((path: string) =>path === "/workflow/copilot/apply-proposed-workflow"@@ -622,76 +628,31 @@ describe("WorkflowCopilotChat — auto-accept Turn off across a chat switch", ()});await flushHistory(pendingChat("chat_other"));+ const readsBeforeFailure = historyParams.length;await act(async () => {failApply({ response: { status: 500 } });await Promise.resolve();});+ // The reconcile reads the ACCEPTED chat's row, not the one now on screen.await waitFor(() =>- expect(cancelPost).toHaveBeenCalledWith(- "/workflow/copilot/clear-proposed-workflow",- expect.objectContaining({ workflow_copilot_chat_id: "chat-1" }),- ),+ expect(+ historyParams+ .slice(readsBeforeFailure)+ .map((params) => params?.workflow_copilot_chat_id),+ ).toContain("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();- });+ // RETIRED BY THE #17099 MERGE, WITH THE GUARD IT PROTECTED. This asserted that a 404 on the+ // fallback clear must not retry against whatever chat is now on screen. Slice 1 removes the+ // accept-path clears, leaving clearProposedWorkflow with one caller that names no chat - so+ // there is no named-chat clear left to retry, and clearProposedWorkflow's forChatId parameter+ // and its `status === 404 && !forChatId` guard are removed here too. Unlike the three tests+ // rewritten alongside it, the concern does not survive the mechanism: the mechanism was the+ // only way to reach it. Restoring both is a git revert if a later slice names a chat again.it("does not hold one chat's Turn off behind an Accept still running in another chat", async () => {await renderChat();diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsxindex f588efff6..2ae9549ed 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx@@ -173,9 +173,17 @@ const saveData = {workflowDefinitionVersion: 1,};-vi.mock("@/store/WorkflowHasChangesStore", () => ({- useWorkflowHasChangesStore: () => ({ getSaveData: () => saveData }),-}));+vi.mock("@/store/WorkflowHasChangesStore", () => {+ const state = {+ getSaveData: () => saveData,+ setSaveBlockedReason: () => {},+ };+ return {+ useWorkflowHasChangesStore: Object.assign(() => state, {+ getState: () => state,+ }),+ };+});// Unrelated to this file's tests; the real hook needs a QueryClientProvider// this harness doesn't set up.diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.reviewGate.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.reviewGate.test.tsxindex 3e00961e0..1b033f245 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.reviewGate.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.reviewGate.test.tsx@@ -5,6 +5,7 @@ import {render,screen,waitFor,+ within,} from "@testing-library/react";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";@@ -47,9 +48,10 @@ const { streamCalls, postStreaming, cancelPost, historyGet, historyResponse } =owner_turn_id: string;revision: number;canonical_fingerprint: string;- disposition: "review_untested";+ disposition: "review_untested" | "accepting";workflow_run_id: string | null;} | null,+ proposed_claim_expires_in_seconds: null as number | null | undefined,proposed_workflow_run: null as {workflow_run_id: string;status: string | null;@@ -58,6 +60,7 @@ const { streamCalls, postStreaming, cancelPost, historyGet, historyResponse } =outputs: Array<{ output_parameter_id: string; value: unknown }>;} | null,auto_accept: false as boolean,+ question_interactions: [] as unknown[],},};const get = vi.fn().mockImplementation(() => Promise.resolve(history));@@ -85,7 +88,8 @@ vi.mock("@/hooks/useCredentialGetter", () => ({useCredentialGetter: () => null,}));-vi.mock("@/components/ui/use-toast", () => ({ toast: vi.fn() }));+const { toast } = vi.hoisted(() => ({ toast: vi.fn() }));+vi.mock("@/components/ui/use-toast", () => ({ toast }));vi.mock("react-router-dom", async (importOriginal) => {const actual = await importOriginal<typeof import("react-router-dom")>();@@ -139,8 +143,10 @@ const saveData = {workflowDefinitionVersion: 1,};-vi.mock("@/store/WorkflowHasChangesStore", () => ({- useWorkflowHasChangesStore: () => ({ getSaveData: () => saveData }),+// The real SaveButton renders beside the chat, so the save-block handoff runs through+// the real store; only its save hook is stubbed.+vi.mock("@/routes/workflows/editor/hooks/useSaveWorkflow", () => ({+ useSaveWorkflow: () => vi.fn(() => Promise.resolve()),}));// Unrelated to this file's tests; the real hook needs a QueryClientProvider@@ -149,18 +155,35 @@ vi.mock("@/routes/workflows/hooks/useWorkflowRunQuery", () => ({useWorkflowRunQuery: () => ({ data: undefined }),}));-import { toast } from "@/components/ui/use-toast";+import { TooltipProvider } from "@/components/ui/tooltip";+import { SaveButton } from "@/routes/workflows/studio/StudioTopBar";+import { useCopilotActionStore } from "@/store/useCopilotActionStore";+import { useCopilotHeaderStore } from "@/store/useCopilotHeaderStore";+import {+ useWorkflowHasChangesStore,+ type WorkflowSaveData,+} from "@/store/WorkflowHasChangesStore";+import { type WorkflowApiResponse } from "@/routes/workflows/types/workflowTypes";+import {+ fenceBaselineFor,+ hydratedGateFailure,+ extendedClaimHold,+ unattributedClaimDeadline,+} from "./acceptFence";import {ACCEPT_SETTLE_CEILING_MS,WorkflowCopilotChat,} from "./WorkflowCopilotChat";-async function renderChat(props: { docked?: boolean } = {}) {+type ChatProps = NonNullable<Parameters<typeof WorkflowCopilotChat>[0]>;++async function renderChat(props: Partial<ChatProps> = {}) {// docked renders via a portal; without a target it intentionally renders null.const portalTarget = props.docked ? document.body : undefined;const view = render(<WorkflowCopilotChat+ {...props}docked={props.docked ?? false}portalTarget={portalTarget}/>,@@ -169,6 +192,32 @@ async function renderChat(props: { docked?: boolean } = {}) {return view;}+function leaseDecrementingFrom(seconds: number | null | undefined): number {+ if (typeof seconds !== "number") {+ // null is a server saying no claim is held and undefined is one that cannot say. Neither+ // decrements, and the difference between them is load-bearing elsewhere.+ historyResponse.data.proposed_claim_expires_in_seconds = seconds;+ return 0;+ }+ const endsAt = Date.now() + seconds * 1000;+ Object.defineProperty(+ historyResponse.data,+ "proposed_claim_expires_in_seconds",+ {+ configurable: true,+ get: () => (endsAt - Date.now()) / 1000,+ set: (value: number | null | undefined) => {+ Object.defineProperty(+ historyResponse.data,+ "proposed_claim_expires_in_seconds",+ { configurable: true, enumerable: true, writable: true, value },+ );+ },+ },+ );+ return endsAt;+}+function textarea(): HTMLTextAreaElement {return screen.getByRole("textbox") as HTMLTextAreaElement;}@@ -300,20 +349,31 @@ const plainReplyResponse = (}) as WorkflowCopilotStreamResponseUpdate;beforeEach(() => {+ useWorkflowHasChangesStore.setState({+ getSaveData: () => saveData as unknown as WorkflowSaveData,+ hasChanges: false,+ saveIsPending: false,+ saveBlockedReason: null,+ });+ toast.mockClear();HTMLElement.prototype.scrollIntoView = vi.fn();HTMLElement.prototype.scrollTo = vi.fn();streamCalls.length = 0;postStreaming.mockClear();- cancelPost.mockClear();+ // Reset, not clear: a test may queue a once-response its code path never consumes.+ cancelPost.mockReset();cancelPost.mockResolvedValue({});- historyGet.mockClear();+ historyGet.mockReset();+ historyGet.mockImplementation(() => Promise.resolve(historyResponse));historyResponse.data = {workflow_copilot_chat_id: "chat-1",chat_history: [],proposed_workflow: null,proposed_workflow_metadata: null,+ proposed_claim_expires_in_seconds: null as number | null | undefined,proposed_workflow_run: null,auto_accept: false,+ question_interactions: [],};});@@ -370,6 +430,10 @@ describe("WorkflowCopilotChat — g2 review gate", () => {"/workflow/copilot/apply-proposed-workflow",expect.objectContaining({ owner_turn_id: "turn-1", revision: 1 }),);+ // The reload swapped in a different proposal: the failure shows, but only a+ // fresh Accept of the proposal now on screen may save it.+ expect(await screen.findByRole("alert")).toBeTruthy();+ expect(screen.queryByRole("button", { name: "Try again" })).toBeNull();expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();});@@ -400,14 +464,39 @@ describe("WorkflowCopilotChat — g2 review gate", () => {workflow_run_id: null,};cancelPost.mockRejectedValueOnce({ response: { status: 500 } });+ let finishReload: () => void = () => {};+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ finishReload = () => resolve(historyResponse);+ }),+ );await act(async () => {fireEvent.click(screen.getByRole("button", { name: "Accept" }));});- await waitFor(() => expect(historyGet).toHaveBeenCalled());+ // A rejected request may still have saved, so nothing is claimed until the chat row answers.+ expect(screen.queryByRole("alert")).toBeNull();+ await act(async () => {+ finishReload();+ });+ expect(await screen.findByRole("alert")).toBeTruthy();expect(cancelPost).toHaveBeenCalledTimes(1);expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();++ cancelPost.mockResolvedValueOnce({ data: proposedWorkflowPayload() });+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });++ expect(cancelPost).toHaveBeenLastCalledWith(+ "/workflow/copilot/apply-proposed-workflow",+ expect.objectContaining({ owner_turn_id: "turn-1", revision: 1 }),+ );+ expect(+ await screen.findByText("Accepted — saved to the workflow"),+ ).toBeTruthy();});it("does not locally accept a typed proposal when its chat cannot be resolved", async () => {@@ -430,797 +519,4668 @@ describe("WorkflowCopilotChat — g2 review gate", () => {expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();});- it("interrupted_draft reload shows an actionable candidate and its exact run result", async () => {- // A hard kill can leave only the submitted user row. MessageItem does not- // render footers on user bubbles, so the durable candidate needs a- // standalone review gate until an assistant-owned row exists.- historyResponse.data.chat_history = [- {- sender: "user",- content: "build the candidate",- created_at: "2026-09-08T12:00:00Z",- },- ];- historyResponse.data.proposed_workflow = proposedWorkflowPayload({- title: "Recovered draft",- });- historyResponse.data.proposed_workflow_metadata = {- owner_turn_id: "turn-interrupted",- revision: 3,- canonical_fingerprint: "canonical-1",- disposition: "review_untested",- workflow_run_id: "wr-exact",- };- historyResponse.data.proposed_workflow_run = {- workflow_run_id: "wr-exact",- status: "completed",- available: true,- failure_reason: null,- outputs: [{ output_parameter_id: "op-metric", value: { metric: "42" } }],- };-- await renderChat();-- expect(await screen.findByText("Associated test: completed")).toBeTruthy();- expect(screen.getByText(/op-metric:.*42/)).toBeTruthy();- expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();- expect(screen.getByRole("button", { name: "Reject" })).toBeTruthy();- });-- it("restores an actionable gate via the chip after a bypassed proposal (old code: buttons vanish forever)", async () => {+ it("keeps a legacy proposal pending when Accept returns non-2xx, and only a fresh Accept can save what the reload shows", 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();});+ // A legacy proposal has no token, so the reload cannot prove it is the one that failed.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload({+ title: "Replacement draft",+ });+ cancelPost.mockRejectedValueOnce({+ response: {+ status: 400,+ data: { detail: "Proposed copilot YAML is invalid" },+ },+ });- expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();- expect(screen.getByRole("button", { name: "Reject" })).toBeTruthy();- expect(screen.getByRole("button", { name: "Always accept" })).toBeTruthy();- expect(screen.getByRole("button", { name: "Review" })).toBeTruthy();-- // Bypass: send a follow-up instead of acting on the gate.- historyResponse.data.proposed_workflow = proposedWorkflowPayload();- await submit("also grab the story scores");- await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));-- // keep_pending_proposal must ride along on the bypassing request.- 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.getByRole("button", { name: /1 proposal pending/ }),- ).toBeTruthy();-- // Turn 2 ends with no new proposal; resync picks the row back up.await act(async () => {- streamCalls[1]!.onMessage(- plainReplyResponse("I'll fold that into the draft above."),- );- streamCalls[1]!.resolve();+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));});- await waitFor(() => expect(historyGet).toHaveBeenCalled());- await waitFor(() =>- expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy(),+ expect(screen.queryByText("Accepted — saved to the workflow")).toBeNull();+ expect(cancelPost).not.toHaveBeenCalledWith(+ "/workflow/copilot/clear-proposed-workflow",+ expect.anything(),);- // 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.getByRole("button", { name: /1 proposal pending/ });- await act(async () => {- fireEvent.click(chip);- });- expect(HTMLElement.prototype.scrollIntoView).toHaveBeenCalled();+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ expect(screen.getByRole("alert")).toBeTruthy();+ expect(screen.queryByRole("button", { name: "Try again" })).toBeNull();+ cancelPost.mockResolvedValueOnce({ data: proposedWorkflowPayload() });await act(async () => {fireEvent.click(screen.getByRole("button", { name: "Accept" }));});- await waitFor(() =>- expect(cancelPost).toHaveBeenCalledWith(- "/workflow/copilot/apply-proposed-workflow",- expect.objectContaining({ workflow_copilot_chat_id: "chat-1" }),- ),- );++ expect(+ await screen.findByText("Accepted — saved to the workflow"),+ ).toBeTruthy();+ expect(screen.queryByRole("alert")).toBeNull();});- 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;+ it("locks the gate while Accept is in flight so a second click cannot race it", async () => {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 submit("build me a workflow");await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));-- cancelPost.mockRejectedValueOnce({ response: { status: 500 } });await act(async () => {- fireEvent.click(chip);+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();});- // 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 = () => {};+ let resolveApply: (value: unknown) => void = () => {};cancelPost.mockImplementationOnce(- () => new Promise((resolve) => (finishTurnOff = resolve)),+ () =>+ new Promise((resolve) => {+ resolveApply = resolve;+ }),);+await act(async () => {- fireEvent.click(screen.getByRole("button", { name: /Auto-accepting/ }));+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));});- 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",- }),- }),+ for (const name of ["Accept", "Always accept", "Reject"]) {+ expect(screen.getByRole("button", { name }).matches(":disabled")).toBe(+ true,);- streamCalls[0]!.resolve();- });- // The gate offers no actions while Turn off is pending; the draft must still be waiting when it finishes.+ }+ expect(screen.getByText("Accepting…")).toBeTruthy();+await act(async () => {- finishTurnOff({});+ resolveApply({ data: proposedWorkflowPayload() });});- await waitFor(() =>- expect(- screen.queryByRole("button", { name: /Auto-accepting/ }),- ).toBeNull(),- );- expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ expect(+ await screen.findByText("Accepted — saved to the workflow"),+ ).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();-+ it.each([false, true])(+ "locks History and New chat while an Accept is in flight (docked: %s)",+ async (docked) => {+ await renderChat({ docked });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();+ let resolveApply: (value: unknown) => void = () => {};+ cancelPost.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ resolveApply = resolve;+ }),+ );+ const navigationLocked = () => {+ const controls = useCopilotHeaderStore.getState().controls;+ return docked+ ? [controls!.disabled, controls!.navigationLockedReason !== null]+ : ["History", "New chat"].map((name) =>+ // The reason joins the accessible name once a control is locked.+ screen+ .getByRole("button", { name: new RegExp(`^${name}`) })+ .matches(":disabled"),+ );+ };+ expect(navigationLocked()).toEqual([false, false]);- // 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();+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));});- historyResponse.data.auto_accept = rowKept;++ // The server finishes an Accept regardless, so another chat must not take over the gate mid-apply.+ expect(navigationLocked()).toEqual([true, true]);+ // Nor may a new turn stage a proposal the late Accept would then clobber.+ await submit("change it again");+ expect(textarea().value).toBe("change it again");await act(async () => {- fireEvent.click(await screen.findByRole("button", { name: "Accept" }));+ resolveApply({ data: proposedWorkflowPayload() });});-- 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),- );+ expect(navigationLocked()).toEqual([false, false]);},);- 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 live path that reaches this is the recovery poll, which no test in this file drives, so+ // the contract is asserted directly instead. See the PR's coverage-gaps note.+ describe("hydratedGateFailure", () => {+ // A claim the row can TIE TO A PROPOSAL. This fixture used to carry no metadata, which was+ // harmless only because the server derived the claim FROM the metadata - the two could not+ // disagree. It reports claim liveness independently now, so the combination below is a real+ // state with its own answer, and the assertions here would otherwise be testing it by+ // accident.+ const claimed = {+ proposed_claim_expires_in_seconds: 120,+ proposed_workflow_metadata: {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "accepting" as const,+ workflow_run_id: null,+ },+ auto_accept: false,+ };+ const unclaimed = { ...claimed, proposed_claim_expires_in_seconds: null };+ const claimedByAnother = {+ ...claimed,+ proposed_workflow_metadata: null,+ };+ const held = {+ kind: "recover",+ alwaysAccept: false,+ token: null,+ holdExpiresAt: 4_242,+ claimExpiresAtSeen: null,+ } as const;++ it("arms the fence when the row reports a claim", () => {+ const armed = hydratedGateFailure(claimed, null, 9_000);+ expect(armed?.kind).toBe("recover");});- // 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({});++ it("leaves an active hold alone when the row reports no claim", () => {+ // A read can overtake a POST that has not claimed yet, so "no claim" is not proof that+ // nothing is writing — releasing here is the hazard direction.+ expect(hydratedGateFailure(unclaimed, held, 9_000)).toBe(held);});- 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" }),- );+ it("stays clear when there is no hold and no claim", () => {+ expect(hydratedGateFailure(unclaimed, null, 9_000)).toBeNull();});- await act(async () => {- fireEvent.click(screen.getByRole("button", { name: "Always accept" }));++ it("keeps a confirmed save, which is the only copy of that workflow", () => {+ // `saved` means the apply returned 200 and its workflow is in hand while the canvas holds+ // the older draft. A recovery poll hydrating a row with no live claim must not drop it:+ // that discards the retry's only copy AND releases Save over a canvas known to be stale,+ // which is the overwrite this fence exists to prevent.+ const confirmed = {+ kind: "saved",+ alwaysAccept: false,+ token: null,+ ownerTurnId: "turn-1",+ savedWorkflow: { workflow_id: "wf_saved" } as unknown as never,+ } as const;+ expect(hydratedGateFailure(unclaimed, confirmed, 9_000)).toBe(confirmed);});- await act(async () => {- fireEvent.click(screen.getByRole("button", { name: /Auto-accepting/ }));++ it("does not downgrade a confirmed save to a claim that may still be writing", () => {+ // The route clears the claim AFTER the workflow write, best-effort, so a row can still+ // report a claim for an Accept that already landed. Arming over `saved` would drop the+ // only copy of the confirmed workflow and give the hold a deadline that can release over+ // a stale canvas - trading what we know for what we are guessing.+ const confirmed = {+ kind: "saved",+ alwaysAccept: false,+ token: null,+ ownerTurnId: "turn-1",+ savedWorkflow: { workflow_id: "wf_saved" } as unknown as never,+ } as const;+ expect(hydratedGateFailure(claimed, confirmed, 9_000)).toBe(confirmed);});- expect(finishApplies).toHaveLength(2);- await act(async () => {- finishApplies[1]!({});++ it("still arms over every other hold when a row reports a claim", () => {+ // The suppression in `does not downgrade a confirmed save to a claim that may still be+ // writing` is specific to `saved`, which knows more than the row does.+ expect(hydratedGateFailure(claimed, held, 9_000)?.kind).toBe("recover");+ expect(hydratedGateFailure(claimed, null, 9_000)?.kind).toBe("recover");});- expect(requestedPaths()).not.toContain(++ it("lets a fresh row restate an outcome that claims nothing was saved", () => {+ const notSaved = {+ kind: "accept",+ alwaysAccept: false,+ token: null,+ } as const;+ expect(hydratedGateFailure(unclaimed, notSaved, 9_000)).toBeNull();+ });++ it("does not narrate a claim the row cannot tie to a proposal", () => {+ // `recover` says "this may have saved" and sends the user to Try again. Both are false+ // about another writer's claim - and with no proposal there is no card to press it on, so+ // the message would name a button that is not rendered. Save still holds, via+ // unattributedClaimDeadline, which says only what is true.+ expect(hydratedGateFailure(claimedByAnother, null, 9_000)).toBeNull();+ });+ });++ describe("fenceBaselineFor", () => {+ it("takes its reading only when no fence is open yet", () => {+ expect(fenceBaselineFor(undefined, 120_000)).toBe(120_000);+ });++ it("never moves an open fence's baseline", () => {+ expect(fenceBaselineFor(400, 300_000)).toBe(400);+ });++ it("keeps a blind fence blind", () => {+ // null is the ANSWER, not a missing one: the terminal pass treats an absent baseline as+ // grounds to hold, so letting an ordinary poll supply one from another writer's claim+ // would hand that rule a number to compare against and release.+ expect(fenceBaselineFor(null, 300_000)).toBeNull();+ });+ });++ describe("extendedClaimHold", () => {+ it("arms from a row when nothing is held yet", () => {+ expect(extendedClaimHold(null, 121_000)).toBe(121_000);+ });++ it("keeps the hold when a row reports no claim", () => {+ // A row covers ONE chat. The server reads the claim off that chat's own proposal blob, so+ // "no claim here" is not "no claim on this workflow" - and retiring on it releases Save+ // under another writer's live Accept.+ expect(extendedClaimHold(121_000, null)).toBe(121_000);+ });++ it("stays retired when nothing has ever armed it", () => {+ expect(extendedClaimHold(null, null)).toBeNull();+ });++ it("extends to a later claim", () => {+ expect(extendedClaimHold(121_000, 300_000)).toBe(300_000);+ });++ it("keeps the LATER deadline when a row reports a shorter one", () => {+ // MAX, NOT LAST. A chat-scoped read cannot say whose claim it is seeing, so a second+ // chat's shorter live claim is not evidence the first writer finished - taking the last+ // reading would release Save at 250 while a writer holding until 300 is still writing.+ expect(extendedClaimHold(300_000, 250_000)).toBe(300_000);+ });+ });++ describe("unattributedClaimDeadline", () => {+ const row = {+ proposed_claim_expires_in_seconds: 120,+ proposed_workflow_metadata: null,+ };++ it("holds until the reported lease runs out", () => {+ expect(unattributedClaimDeadline(row, 1_000)).toBe(121_000);+ });++ it("stays out of the way when the claim belongs to a visible proposal", () => {+ // That claim is the gate's business, not the workflow's - arming both would hold Save+ // twice for one write and leave this one outliving the gate that explains it.+ expect(+ unattributedClaimDeadline(+ {+ ...row,+ proposed_workflow_metadata: {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "accepting",+ workflow_run_id: null,+ },+ },+ 1_000,+ ),+ ).toBeNull();+ });++ it("does not hold when no claim is reported", () => {+ expect(+ unattributedClaimDeadline(+ { ...row, proposed_claim_expires_in_seconds: null },+ 1_000,+ ),+ ).toBeNull();+ });+ });++ it("never replaces the canvas during recovery, and keeps Save held once all it can say is that the canvas may be stale", async () => {+ const onWorkflowUpdate = vi.fn();+ await renderChat({ onWorkflowUpdate });+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ // A real streamed draft, so the canvas is dirty exactly the way it is in production.+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ // The server saved and cleared the proposal; the canonical workflow is readable.+ const savedWorkflow = proposedWorkflowPayload({ workflow_id: "wf_saved" });+ historyResponse.data.proposed_workflow = null;+ historyResponse.data.proposed_claim_expires_in_seconds = 0.4;+ historyGet.mockImplementation((path: string) =>+ Promise.resolve(+ path === "/workflows/wpid_1"+ ? { data: savedWorkflow }+ : historyResponse,+ ),+ );+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });++ // The fence locks Save, sending and navigation but not canvas editing, and nothing tells+ // the user's work apart from the staged draft — so recovery must not overwrite it.+ expect(await screen.findByRole("alert")).toBeTruthy();+ expect(saveHeld()).toBe(true);+ expect(onWorkflowUpdate).not.toHaveBeenCalledWith(+ savedWorkflow,+ expect.anything(),+ );++ // Waiting on the server ends; the hold does not. The gate now says what the user is+ // looking at may be older than the server, and Save stays blocked, because a card cannot+ // tell the user to reload and leave live the one control that would overwrite what the+ // reload would bring back. The exit is the user's: Try again, or the reload it names.+ expect(+ await screen.findByText("Couldn't reload", undefined, { timeout: 4000 }),+ ).toBeTruthy();+ expect(saveHeld()).toBe(true);+ expect(screen.getByRole("button", { name: "Try again" })).toBeTruthy();+ expect(onWorkflowUpdate).not.toHaveBeenCalledWith(+ savedWorkflow,+ expect.anything(),+ );+ });++ it("keeps the stale-canvas Save hold when a new send clears the gate card", async () => {+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ // The Accept's response is lost and the row comes back with no proposal, so recovery runs+ // out of attempts and the gate lands on "Couldn't reload" with Save held.+ historyResponse.data.proposed_workflow = null;+ leaseDecrementingFrom(0.4);+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(+ await screen.findByText(/Couldn't reload this proposal/, undefined, {+ timeout: 4000,+ }),+ ).toBeTruthy();+ expect(saveHeld()).toBe(true);++ // Sending is deliberately still allowed in this state, and it clears the gate card. The+ // hold is a fact about the CANVAS, not about the card, so it has to survive the card:+ // otherwise any message at all re-opens the destructive Save this state exists to prevent.+ await submit("actually, add a second step");+ expect(screen.queryByText(/Couldn't reload this proposal/)).toBeNull();+ expect(saveHeld()).toBe(true);+ });++ it("leaves the canvas current when the terminal recheck proves the Accept never saved", async () => {+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ // The row still carries the proposal, so the last-chance read proves canonical never moved+ // and this Accept wrote nothing.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ leaseDecrementingFrom(0.4);+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(await screen.findByRole("alert")).toBeTruthy();+ expect(saveHeld()).toBe(true);++ // staleCanvas is never SET in this sequence - the terminal read succeeds and reaches a+ // definite verdict, so recovery never gives up and never marks the canvas out of date. The+ // hold here is the FENCE's, and the fence ending is what releases Save.+ expect(+ await screen.findByText("Not saved", undefined, { timeout: 4000 }),+ ).toBeTruthy();+ await waitFor(() => expect(saveHeld()).toBe(false));+ });++ it("locks proposal actions after a failed reload, and Try again reconciles instead of re-sending Accept", 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();+ });+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ cancelPost.mockRejectedValueOnce({ response: { status: 500 } });+ historyGet.mockRejectedValueOnce(new Error("network down"));++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(await screen.findByRole("alert")).toBeTruthy();+ // The shown proposal may be stale, so nothing but the reload may act on it.+ for (const name of ["Accept", "Always accept", "Reject"]) {+ expect(screen.getByRole("button", { name }).matches(":disabled")).toBe(+ true,+ );+ }++ // A re-sent Accept would succeed here; Try again must only re-read the chat row.+ cancelPost.mockResolvedValueOnce({ data: proposedWorkflowPayload() });+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });++ await waitFor(() =>+ expect(+ screen.getByRole("button", { name: "Accept" }).matches(":disabled"),+ ).toBe(false),+ );+ expect(screen.queryByText("Accepted — saved to the workflow")).toBeNull();+ });++ it("shows a failed reload after a follow-up turn inline, with only the reload actionable", 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();+ });+ await submit("also grab the story scores");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ historyGet.mockRejectedValueOnce(new Error("network down"));++ // The turn ends without a new draft, so the kept proposal is re-read from the chat row.+ await act(async () => {+ streamCalls[1]!.onMessage(+ plainReplyResponse("I'll fold that into the draft above."),+ );+ streamCalls[1]!.resolve();+ });++ expect(await screen.findByRole("alert")).toBeTruthy();+ expect(+ screen.getByRole("button", { name: "Accept" }).matches(":disabled"),+ ).toBe(true);++ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });++ await waitFor(() => expect(screen.queryByRole("alert")).toBeNull());+ expect(+ screen.getByRole("button", { name: "Accept" }).matches(":disabled"),+ ).toBe(false);+ });++ it("holds a browser-queued prompt until a pending Accept settles, then sends it", async () => {+ const view = await renderChat({+ requiresLiveBrowser: true,+ isLiveBrowserReady: true,+ });+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ view.rerender(+ <WorkflowCopilotChat+ docked={false}+ requiresLiveBrowser+ isLiveBrowserReady={false}+ />,+ );+ await submit("queued follow-up");+ let resolveApply: (value: unknown) => void = () => {};+ cancelPost.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ resolveApply = resolve;+ }),+ );+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });++ view.rerender(+ <WorkflowCopilotChat+ docked={false}+ requiresLiveBrowser+ isLiveBrowserReady+ liveBrowserSessionId="pbs_harness"+ />,+ );+ const sent = (message: string) =>+ streamCalls.some((call) => call.body.message === message);+ // Let any send the effect would start reach the stream before asserting it did not.+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 50));+ });+ expect(sent("queued follow-up")).toBe(false);++ await act(async () => {+ resolveApply({ data: proposedWorkflowPayload() });+ });+ await waitFor(() => expect(sent("queued follow-up")).toBe(true));+ });++ it("holds an initial message that arrives during a pending Accept, then sends it", async () => {+ const view = 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 resolveApply: (value: unknown) => void = () => {};+ cancelPost.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ resolveApply = resolve;+ }),+ );+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });++ // A slow Accept outlives the auto-send fallback window, which must not consume the message.+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });+ view.rerender(+ <WorkflowCopilotChat docked={false} initialMessage="test the run" />,+ );+ await act(async () => {+ vi.advanceTimersByTime(6000);+ });+ vi.useRealTimers();+ const sent = (message: string) =>+ streamCalls.some((call) => call.body.message === message);+ // Let any send the effect would start reach the stream before asserting it did not.+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 50));+ });+ expect(sent("test the run")).toBe(false);++ await act(async () => {+ resolveApply({ data: proposedWorkflowPayload() });+ });+ await waitFor(() => expect(sent("test the run")).toBe(true));+ });++ it.each([+ ["live", 240, true],+ ["expired", null, false],+ // An API instance that predates the field omits it; its row still names the claim.+ ["unreported by an older API", undefined, true],+ ] as const)(+ "after a lost Accept, a %s server claim decides whether the gate stays locked",+ async (_label, claimTtlSeconds, stillSaving) => {+ const metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "review_untested" as const,+ workflow_run_id: null,+ };+ await renderChat();+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(+ proposalResponse("Draft ready.", {+ proposed_workflow_metadata: metadata,+ }),+ );+ streamCalls[0]!.resolve();+ });+ // The server claimed the proposal; within its lease it may still be writing the version,+ // past it the claim is abandoned and can be taken over or rejected.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ ...metadata,+ disposition: "accepting" as unknown as "review_untested",+ } as typeof metadata;+ leaseDecrementingFrom(claimTtlSeconds);+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });++ expect(await screen.findByRole("alert")).toBeTruthy();+ // Try again is the only live control while the row is locked, so it must not be+ // swept into whatever disables the rest of the gate.+ expect(+ screen.getByRole("button", { name: "Try again" }).matches(":disabled"),+ ).toBe(false);+ for (const name of ["Accept", "Reject"]) {+ expect(screen.getByRole("button", { name }).matches(":disabled")).toBe(+ stillSaving,+ );+ }+ },+ );++ it("does not revive the previous chat's gate when a reload retry lands after New chat", 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();+ });+ await submit("also grab the story scores");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ historyGet.mockRejectedValueOnce(new Error("network down"));+ await act(async () => {+ streamCalls[1]!.onMessage(+ plainReplyResponse("I'll fold that into the draft above."),+ );+ streamCalls[1]!.resolve();+ });+ expect(await screen.findByRole("alert")).toBeTruthy();++ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ let finishReload: () => void = () => {};+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ finishReload = () => resolve(historyResponse);+ }),+ );+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "New chat" }));+ });+ await act(async () => {+ finishReload();+ });++ expect(screen.queryByRole("button", { name: "Accept" })).toBeNull();+ expect(screen.queryByRole("alert")).toBeNull();+ });++ it("keeps a newer chat's reload failure when the previous chat's reload retry 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();+ });+ await submit("also grab the story scores");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ historyGet.mockRejectedValueOnce(new Error("network down"));+ await act(async () => {+ streamCalls[1]!.onMessage(+ plainReplyResponse("I'll fold that into the draft above."),+ );+ streamCalls[1]!.resolve();+ });+ expect(await screen.findByRole("alert")).toBeTruthy();+ let finishOldReload: () => void = () => {};+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ finishOldReload = () => resolve(historyResponse);+ }),+ );+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "New chat" }));+ });++ // The new chat stages its own proposal, and its Reject hits a conflict whose reload fails.+ await submit("build a different workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(3));+ await act(async () => {+ streamCalls[2]!.onMessage(+ proposalResponse("Second draft ready.", {+ workflow_copilot_chat_id: "chat-2",+ turn_id: "turn-2",+ narrative_payload: proposalNarrativePayload({ turnId: "turn-2" }),+ }),+ );+ streamCalls[2]!.resolve();+ });+ cancelPost.mockRejectedValueOnce({ response: { status: 409 } });+ historyGet.mockRejectedValueOnce(new Error("network down"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Reject" }));+ });+ expect(await screen.findByRole("alert")).toBeTruthy();++ await act(async () => {+ finishOldReload();+ });++ expect(screen.getByRole("alert")).toBeTruthy();+ expect(+ screen.getByRole("button", { name: "Accept" }).matches(":disabled"),+ ).toBe(true);+ });++ it.each(["the chat unmounts", "the claim lease expires"] as const)(+ "holds saves and chat navigation from Accept until %s",+ async (release) => {+ const view = await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ const locked = (name: string) =>+ screen+ .getByRole("button", { name: new RegExp(`^${name}`) })+ .matches(":disabled");+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ historyGet.mockImplementation((path: string) =>+ path !== "/workflows/wpid_1"+ ? Promise.resolve(historyResponse)+ : Promise.reject(new Error("network down")),+ );+ if (release === "the claim lease expires") {+ // The server still holds the claim, with 1.5s of its lease left to run.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "accepting" as unknown as "review_untested",+ workflow_run_id: null,+ } as typeof historyResponse.data.proposed_workflow_metadata;+ leaseDecrementingFrom(1.5);+ }+ let loseApplyResponse: () => void = () => {};+ cancelPost.mockImplementationOnce(+ () =>+ new Promise((_resolve, reject) => {+ loseApplyResponse = () => reject(new Error("Network Error"));+ }),+ );+ expect(saveHeld()).toBe(false);++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ // The server may be writing the accepted version from the moment Accept is sent.+ expect(saveHeld()).toBe(true);++ await act(async () => {+ loseApplyResponse();+ });+ expect(await screen.findByRole("alert")).toBeTruthy();+ expect(saveHeld()).toBe(true);+ expect(locked("History")).toBe(true);+ expect(locked("New chat")).toBe(true);+ expect(+ screen.getByRole("button", { name: "New chat" }).title,+ ).toBeTruthy();++ // A chat switch must not be able to release the hold.+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "New chat" }));+ });+ expect(saveHeld()).toBe(true);++ // Try again cannot end this either: recovery no longer adopts canonical, so retrying+ // re-reads and finds the same unresolved outcome.+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });+ expect(saveHeld()).toBe(true);++ await act(async () => {+ if (release === "the chat unmounts") {+ view.unmount();+ }+ });+ await waitFor(() => expect(saveHeld()).toBe(false), { timeout: 4000 });+ if (release !== "the chat unmounts") {+ expect(locked("History")).toBe(false);+ expect(locked("New chat")).toBe(false);+ }+ },+ 10_000,+ );++ it.each([+ ["a live server claim", "live-claim"],+ ["a chat row that will not load", "row-unreadable"],+ ["an API that cannot report the claim", "older-api"],+ ] as const)(+ "keeps saves and sends held while %s leaves the Accept unaccounted for",+ async (_label, mode) => {+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ if (mode === "row-unreadable") {+ historyGet.mockImplementation(() =>+ Promise.reject(new Error("network down")),+ );+ } else {+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_claim_expires_in_seconds =+ mode === "live-claim" ? 120 : undefined;+ if (mode === "older-api") {+ // An instance that predates the field omits it; the row still names the claim,+ // and that claim can run for the backend's full five-minute lease.+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "accepting" as unknown as "review_untested",+ workflow_run_id: null,+ } as typeof historyResponse.data.proposed_workflow_metadata;+ }+ }+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(await screen.findByRole("alert")).toBeTruthy();++ // A new turn would stage a proposal over the one the server may be writing, and+ // sending clears the gate failure, which would release the hold with it.+ await submit("change it again");+ expect(postStreaming).toHaveBeenCalledTimes(1);+ expect(saveHeld()).toBe(true);++ // A fence sized to a re-read rather than to the lease would have lapsed by now and let+ // the next Save duplicate the version the backend is still writing.+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 3_500));+ });+ expect(saveHeld()).toBe(true);+ },+ 10_000,+ );++ it("keeps the fence when the editor cannot apply the workflow the server already saved", async () => {+ const onWorkflowUpdate = vi.fn(() => {+ throw new Error("could not parse the saved workflow");+ });+ await renderChat({ onWorkflowUpdate });+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ // The server saved and cleared the proposal, but the response was lost, so recovery+ // reads canonical — and the editor cannot take it.+ historyResponse.data.proposed_workflow = null;+ historyGet.mockImplementation((path: string) =>+ Promise.resolve(+ path === "/workflows/wpid_1"+ ? { data: proposedWorkflowPayload() }+ : historyResponse,+ ),+ );+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });++ // The canvas still holds the pre-Accept graph, so releasing here would write it back+ // over the version the server kept.+ expect(await screen.findByRole("alert")).toBeTruthy();+ expect(+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled"),+ ).toBe(true);+ });++ it("retries the workflow the Accept confirmed, instead of re-reading the chat row", async () => {+ let editorAccepts = false;+ const applied: WorkflowApiResponse[] = [];+ const onWorkflowUpdate = vi.fn((workflow: WorkflowApiResponse) => {+ if (!editorAccepts) {+ throw new Error("editor could not load it");+ }+ applied.push(workflow);+ });+ await renderChat({ onWorkflowUpdate });+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ // The apply SUCCEEDS - 200, the saved workflow is in hand - but the editor throws.+ const savedWorkflow = proposedWorkflowPayload({+ workflow_id: "wf_saved",+ }) as unknown as WorkflowApiResponse;+ cancelPost.mockResolvedValueOnce({ data: savedWorkflow });+ const readsBefore = historyGet.mock.calls.length;++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });++ // Not an ambiguous outcome, so recovery must not run: no chat-row read at all.+ expect(await screen.findByText("Saved, not shown")).toBeTruthy();+ expect(historyGet.mock.calls.length).toBe(readsBefore);+ expect(saveHeld()).toBe(true);+ // Hydration refuses to arm a claim over this gate, which is only safe because no SECOND+ // Accept can be started from it to claim one - the action row is locked here.+ expect(+ screen.getByRole("button", { name: "Accept" }).matches(":disabled"),+ ).toBe(true);++ // Try again re-applies the workflow the server confirmed.+ editorAccepts = true;+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });++ expect(applied).toContainEqual(savedWorkflow);+ await waitFor(() => expect(saveHeld()).toBe(false));+ expect(+ await screen.findByText("Accepted — saved to the workflow"),+ ).toBeTruthy();+ });++ it("keeps auto-accept on when a row read from before a saved Always accept lands last", async () => {+ // The apply's 200 means the server may have written auto_accept, so a row read issued before+ // it can no longer be trusted, even though the editor refused the workflow. The held row+ // reports an unattributed claim, which arms the Save hold from any row: that is how the test+ // knows the read landed.+ let editorAccepts = false;+ const onWorkflowUpdate = vi.fn((workflow: WorkflowApiResponse) => {+ if (!editorAccepts) {+ throw new Error("editor could not load it");+ }+ return workflow;+ });+ historyResponse.data.auto_accept = false;+ await renderChat({ onWorkflowUpdate });+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ // A follow-up turn with no new draft re-reads the row for the bypassed proposal. Hold it open.+ let staleReadIssued = false;+ let releaseStaleRead: () => void = () => {};+ const staleRow = {+ data: {+ ...historyResponse.data,+ proposed_workflow: null,+ proposed_workflow_metadata: null,+ proposed_claim_expires_in_seconds: 100,+ auto_accept: false,+ },+ };+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ staleReadIssued = true;+ releaseStaleRead = () => resolve(staleRow);+ }),+ );+ await submit("also grab the story scores");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ await act(async () => {+ streamCalls[1]!.onMessage(+ plainReplyResponse("I'll fold that into the draft above."),+ );+ streamCalls[1]!.resolve();+ });+ await waitFor(() => expect(staleReadIssued).toBe(true));++ cancelPost.mockResolvedValueOnce({+ data: proposedWorkflowPayload({ workflow_id: "wf_saved" }),+ });+ historyResponse.data.auto_accept = true;+ historyResponse.data.proposed_workflow = null;+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Always accept" }));+ });+ expect(await screen.findByText("Saved, not shown")).toBeTruthy();+ editorAccepts = true;+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });+ expect(await screen.findByText("Auto-accepting")).toBeTruthy();+ expect(saveHeld()).toBe(false);++ await act(async () => {+ releaseStaleRead();+ });+ await waitFor(() => expect(saveHeld()).toBe(true));+ expect(screen.queryByRole("button", { name: "Accept" })).toBeNull();+ expect(screen.getByText("Auto-accepting")).toBeTruthy();+ });++ it("does not hold Save when a post-Accept read fails, since no card is shown", async () => {+ const applied: WorkflowApiResponse[] = [];+ const onWorkflowUpdate = vi.fn((workflow: WorkflowApiResponse) => {+ applied.push(workflow);+ return workflow;+ });+ historyResponse.data.auto_accept = false;+ await renderChat({ onWorkflowUpdate });+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });++ // A SUCCESSFUL Always accept: 200, the editor takes the workflow. auto_accept was off, so+ // `alwaysAccept !== autoAcceptRef.current` and the post-Accept re-read is issued. It fails.+ cancelPost.mockResolvedValueOnce({+ data: proposedWorkflowPayload({ workflow_id: "wf_ok" }),+ });+ historyGet.mockRejectedValueOnce(new Error("network down"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Always accept" }));+ });+ await waitFor(() => expect(applied.length).toBeGreaterThan(0));++ // The Accept SUCCEEDED and the editor loaded the result as its clean baseline.+ expect(screen.queryByText("Saved, not shown")).toBeNull();+ // No card renders here (the proposal is cleared and the gate is not `saved`), so there is no+ // Try again and no named exit. A hold would be unreleasable until a reload - and a failed row+ // read is an availability fact, not evidence that canonical moved under the editor.+ expect(screen.queryByText("Couldn't reload")).toBeNull();+ expect(screen.queryByRole("button", { name: "Try again" })).toBeNull();+ expect(saveHeld()).toBe(false);+ });++ it("holds Save whenever the reload card is on screen, whichever read armed it", async () => {+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });++ // A follow-up turn with no new draft re-reads the row for the bypassed proposal (:5132).+ // That read FAILS. The proposal is still staged, so the reload card has a subject.+ historyGet.mockRejectedValueOnce(new Error("network down"));+ await submit("also grab the story scores");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ await act(async () => {+ streamCalls[1]!.onMessage(+ plainReplyResponse("I'll fold that into the draft above."),+ );+ streamCalls[1]!.resolve();+ });++ // This caller has no evidence canonical moved, but the CARD is what carries the contract:+ // it tells the user the canvas may be stale, so Save must not sit live beside it.+ expect(screen.queryByText("Couldn't reload")).toBeTruthy();+ expect(saveHeld()).toBe(true);++ // And the hold ENDS WITH THE CARD, which is the property this shape was chosen for. Try again+ // re-reads successfully, the gate clears, the card goes - and Save must come back. A hold+ // derived from the failed READ rather than from the card outlives the card here, which is how+ // the earlier shape stranded the user with a message that was no longer on screen.+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });+ await waitFor(() =>+ expect(screen.queryByText("Couldn't reload")).toBeNull(),+ );+ expect(saveHeld()).toBe(false);+ });++ it("takes auto-accept from the server after a saved retry, not from the attempt", async () => {+ let editorAccepts = false;+ const applied: WorkflowApiResponse[] = [];+ const onWorkflowUpdate = vi.fn((workflow: WorkflowApiResponse) => {+ if (!editorAccepts) {+ throw new Error("editor could not load it");+ }+ applied.push(workflow);+ });+ // Mount with auto-accept OFF, the way the session actually starts: the server only+ // persists it during the apply below. Seeding it true before mount would let hydration+ // set the flag and the test would pass without reading anything.+ historyResponse.data.auto_accept = false;+ await renderChat({ onWorkflowUpdate });+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ const savedWorkflow = proposedWorkflowPayload({+ workflow_id: "wf_saved",+ }) as unknown as WorkflowApiResponse;+ cancelPost.mockResolvedValueOnce({ data: savedWorkflow });+ // The apply persists auto_accept server-side. Its 200 does not carry that fact - the+ // route writes it after the workflow write, inside a best-effort swallow - so the only+ // way the client can know is to read it back.+ historyResponse.data.auto_accept = true;+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Always accept" }));+ });+ expect(await screen.findByText("Saved, not shown")).toBeTruthy();++ editorAccepts = true;+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });+ expect(applied).toContainEqual(savedWorkflow);++ // Reading one field must not re-hydrate the row: the gate this retry just cleared may+ // not come back.+ await waitFor(() => expect(historyGet).toHaveBeenCalled());+ expect(screen.queryByRole("alert")).toBeNull();+ expect(screen.queryByText("Confirming\u2026")).toBeNull();+ expect(screen.queryByText("Couldn't reload")).toBeNull();+ expect(screen.queryByText("Saved, not shown")).toBeNull();++ // And the client now holds the server's value, which is what the user experiences.+ //+ // CHANGED BY THE #17099 MERGE, AND THE MECHANISM IS GONE WHILE THE CONCERN SURVIVES. This+ // used to assert that the next auto-applicable proposal AUTO-APPLIED, because the client's+ // flag drove that decision. #17099 moved the decision to the server -+ // shouldAutoApplyWorkflowResponse no longer takes autoAccept and now requires+ // `workflow_applied === true` - so that assertion can no longer distinguish a client that+ // re-read auto_accept from one that did not. Adding `workflow_applied: true` to the response+ // would turn this green in a line and make it VACUOUS FOR ITS OWN PURPOSE: it would pass+ // whether or not the read ever happened.+ //+ // The concern - the client must not carry a STALE auto_accept forward - is still live, so it+ // is asserted where the value still has an observable effect. The chip renders on exactly+ // `autoAccept && workflowCopilotChatId`, so its presence IS the client's flag, and it is the+ // control through which the user turns auto-accept off. Asserting the request body would be+ // stronger, but nothing sends this state: the apply POST sends the button (`alwaysAccept`)+ // and clearProposedWorkflow's only caller passes a literal false.+ expect(await screen.findByText("Auto-accepting")).toBeTruthy();+ expect(screen.getByRole("button", { name: /Turn off/ })).toBeTruthy();+ });++ it("does not carry a saved retry's auto-accept read into a new chat", async () => {+ let editorAccepts = false;+ const onWorkflowUpdate = vi.fn((workflow: WorkflowApiResponse) => {+ if (!editorAccepts) {+ throw new Error("editor could not load it");+ }+ return workflow;+ });+ historyResponse.data.auto_accept = false;+ await renderChat({ onWorkflowUpdate });+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ const savedWorkflow = proposedWorkflowPayload({+ workflow_id: "wf_saved",+ }) as unknown as WorkflowApiResponse;+ cancelPost.mockResolvedValueOnce({ data: savedWorkflow });+ historyResponse.data.auto_accept = true;+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Always accept" }));+ });+ expect(await screen.findByText("Saved, not shown")).toBeTruthy();++ // Hold the post-retry read open so the chat switch happens while it is in flight.+ let releaseRow: () => void = () => {};+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ releaseRow = () => resolve(historyResponse);+ }),+ );+ editorAccepts = true;+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });+ // Clearing the gate unlocks navigation, so the user can start a new chat before the old+ // chat's row comes back - and New chat resets auto-accept to off.+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "New chat" }));+ });+ await act(async () => {+ releaseRow();+ });++ // The old chat's setting must not land in the new one: its next auto-applicable proposal+ // waits for review instead of being applied locally past the gate.+ const nextWorkflow = proposedWorkflowPayload({+ workflow_id: "wf_next",+ }) as unknown as WorkflowApiResponse;+ await submit("start something else");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ await act(async () => {+ streamCalls[1]!.onMessage(+ proposalResponse("New draft.", {+ turn_id: "turn-2",+ proposal_disposition: "auto_applicable",+ updated_workflow: nextWorkflow,+ }),+ );+ streamCalls[1]!.resolve();+ });+ expect(onWorkflowUpdate).not.toHaveBeenCalledWith(+ nextWorkflow,+ expect.objectContaining({ applied: true }),+ );+ });++ it("does not let a late Reject revert the turn that replaced it", 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();+ });++ // Reject is not in the fence, so the composer stays live while its POST is in flight.+ let releaseReject: () => void = () => {};+ cancelPost.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ releaseReject = () => resolve({ data: {} });+ }),+ );+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Reject" }));+ });++ // A new turn lands before the reject comes back, and keeps its own snapshot.+ await submit("actually, do it differently");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ await act(async () => {+ streamCalls[1]!.onMessage(+ proposalResponse("Second draft.", { turn_id: "turn-2" }),+ );+ streamCalls[1]!.resolve();+ });+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();++ await act(async () => {+ releaseReject();+ });++ // The reject belonged to the turn the user rejected, not to the one that replaced it:+ // landing it here would revert the new proposal off the canvas and mark it rejected.+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ expect(screen.queryByText("Rejected")).toBeNull();+ });++ it("does not let a late Reject clear the gate of a chat the user navigated to", async () => {+ await renderChat({ docked: true });+ 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 releaseReject: () => void = () => {};+ cancelPost.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ releaseReject = () => resolve({ data: {} });+ }),+ );+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Reject" }));+ });++ // The user picks another chat from History. That chat already has a proposal pending, so+ // it hydrates its own review gate - nothing here bumps the send epoch.+ historyResponse.data.workflow_copilot_chat_id = "chat-2";+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ await act(async () => {+ await useCopilotHeaderStore.getState().controls?.onSelectChat?.({+ workflow_copilot_chat_id: "chat-2",+ } as Parameters<+ NonNullable<+ NonNullable<+ ReturnType<typeof useCopilotHeaderStore.getState>["controls"]+ >["onSelectChat"]+ >+ >[0]);+ });+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();++ await act(async () => {+ releaseReject();+ });++ // The reject belonged to the chat the user left. Landing it here clears the selected+ // chat's proposal while its candidate is still pending on the server, leaving no gate.+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ });++ it("still completes a Reject when the history navigation it raced fails", async () => {+ await renderChat({ docked: true });+ 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 releaseReject: () => void = () => {};+ cancelPost.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ releaseReject = () => resolve({ data: {} });+ }),+ );+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Reject" }));+ });++ // The user tries another chat and the load FAILS, so they stay where they are.+ historyGet.mockImplementationOnce(() =>+ Promise.reject(new Error("network down")),+ );+ await act(async () => {+ await useCopilotHeaderStore.getState().controls?.onSelectChat?.({+ workflow_copilot_chat_id: "chat-2",+ } as Parameters<+ NonNullable<+ NonNullable<+ ReturnType<typeof useCopilotHeaderStore.getState>["controls"]+ >["onSelectChat"]+ >+ >[0]);+ });++ await act(async () => {+ releaseReject();+ });++ // Nothing moved: the user is in the chat they rejected from, the server cleared the+ // candidate, so the draft and its gate must go. Bailing here would leave the rejected+ // draft on the canvas and saveable.+ await waitFor(() =>+ expect(screen.queryByRole("button", { name: "Accept" })).toBeNull(),+ );+ });++ it("discards a history load the user abandoned", async () => {+ const onWorkflowUpdate = vi.fn();+ await renderChat({ docked: true, onWorkflowUpdate });+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });++ // A history load the user will walk away from, carrying that chat's own settings.+ let releaseHistory: () => void = () => {};+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ releaseHistory = () =>+ resolve({+ data: {+ ...historyResponse.data,+ workflow_copilot_chat_id: "chat-2",+ auto_accept: true,+ proposed_workflow: proposedWorkflowPayload(),+ },+ });+ }),+ );+ await act(async () => {+ void useCopilotHeaderStore.getState().controls?.onSelectChat?.({+ workflow_copilot_chat_id: "chat-2",+ } as Parameters<+ NonNullable<+ NonNullable<+ ReturnType<typeof useCopilotHeaderStore.getState>["controls"]+ >["onSelectChat"]+ >+ >[0]);+ });++ // The user gives up on it and starts a new chat, which clears the board.+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "New chat" }));+ });+ // Only now does the abandoned load answer.+ await act(async () => {+ releaseHistory();+ });++ // It must not write the chat the user left back over the one they are in: no gate from a+ // proposal belonging to chat-2.+ expect(screen.queryByRole("button", { name: "Accept" })).toBeNull();+ expect(screen.queryByRole("button", { name: "Reject" })).toBeNull();++ // And it must not restore that chat's Always accept either, which would auto-apply the+ // next proposal here without review.+ const nextWorkflow = proposedWorkflowPayload({+ workflow_id: "wf_next",+ }) as unknown as WorkflowApiResponse;+ await submit("something else entirely");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ await act(async () => {+ streamCalls[1]!.onMessage(+ proposalResponse("Fresh draft.", {+ turn_id: "turn-2",+ proposal_disposition: "auto_applicable",+ updated_workflow: nextWorkflow,+ }),+ );+ streamCalls[1]!.resolve();+ });+ expect(onWorkflowUpdate).not.toHaveBeenCalledWith(+ nextWorkflow,+ expect.objectContaining({ applied: true }),+ );+ });++ it("lifts the stale-canvas hold when the server itself commits a later turn", async () => {+ await renderChat({ onWorkflowUpdate: vi.fn() });+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ 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.proposed_workflow = null;+ leaseDecrementingFrom(0.4);+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(+ await screen.findByText(/Couldn't reload this proposal/, undefined, {+ timeout: 4000,+ }),+ ).toBeTruthy();+ expect(saveHeld()).toBe(true);++ // A later turn the SERVER commits itself. The canvas is canonical again, so the hold has+ // nothing left to protect - keeping Save disabled would tell the user their canvas is out+ // of date immediately after the server refreshed it.+ await submit("add a second step");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ await act(async () => {+ streamCalls[1]!.onMessage(+ proposalResponse("Committed.", {+ turn_id: "turn-2",+ proposal_disposition: "auto_applicable",+ workflow_applied: true,+ }),+ );+ streamCalls[1]!.resolve();+ });+ await waitFor(() => expect(saveHeld()).toBe(false));+ });++ it("keeps the recovery gate's exit on screen after hydration clears the proposal, with its actions still locked", async () => {+ await renderChat({ docked: true });+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });++ // The Accept's outcome is unknown and the row reports a live claim, so the gate parks on+ // `recover` - whose Save reason tells the user to use Try again on the review gate.+ historyResponse.data.proposed_claim_expires_in_seconds = 120;+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(await screen.findByText("Confirming…")).toBeTruthy();+ expect(saveHeld()).toBe(true);++ // A hydration lands with no proposal and no live claim: the gate is PRESERVED while its+ // subject goes away. Without this fix the card stops rendering and the Save reason names a+ // Try again that is not on screen - a hold with no exit but a page reload.+ historyGet.mockImplementationOnce(() =>+ Promise.resolve({+ data: {+ ...historyResponse.data,+ workflow_copilot_chat_id: "chat-2",+ proposed_workflow: null,+ proposed_workflow_metadata: null,+ proposed_claim_expires_in_seconds: null,+ },+ }),+ );+ await act(async () => {+ await useCopilotHeaderStore.getState().controls?.onSelectChat?.({+ workflow_copilot_chat_id: "chat-2",+ } as Parameters<+ NonNullable<+ NonNullable<+ ReturnType<typeof useCopilotHeaderStore.getState>["controls"]+ >["onSelectChat"]+ >+ >[0]);+ });++ // Half one: the exit the Save reason names is on screen.+ const retry = await screen.findByRole("button", { name: "Try again" });+ expect(retry.matches(":disabled")).toBe(false);+ // Half two, and it is the half that stops this fix becoming a defect: rendering the card must+ // NOT re-enable the proposal actions. `gateActionable` is true here; what holds the line is+ // ReviewGateCard's own disabled fieldset.+ expect(+ screen.getByRole("button", { name: "Accept" }).matches(":disabled"),+ ).toBe(true);+ expect(+ screen.getByRole("button", { name: "Reject" }).matches(":disabled"),+ ).toBe(true);+ });++ it("keeps the confirmed-save retry reachable after hydration clears the proposal", async () => {+ let editorAccepts = true;+ const applied: WorkflowApiResponse[] = [];+ const onWorkflowUpdate = vi.fn((workflow: WorkflowApiResponse) => {+ if (!editorAccepts) {+ throw new Error("editor could not load it");+ }+ applied.push(workflow);+ });+ await renderChat({ docked: true, onWorkflowUpdate });+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });++ // The apply returns 200 and the editor throws, so the gate parks on `saved`.+ const savedWorkflow = proposedWorkflowPayload({+ workflow_id: "wf_saved",+ }) as unknown as WorkflowApiResponse;+ editorAccepts = false;+ cancelPost.mockResolvedValueOnce({ data: savedWorkflow });+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(await screen.findByText("Saved, not shown")).toBeTruthy();+ expect(saveHeld()).toBe(true);++ // A hydration lands carrying no proposal - which is the truth after a save: the server+ // cleared it. The recovery poll reaches this path without touching the fence.+ historyGet.mockImplementationOnce(() =>+ Promise.resolve({+ data: {+ ...historyResponse.data,+ workflow_copilot_chat_id: "chat-2",+ proposed_workflow: null,+ },+ }),+ );+ await act(async () => {+ await useCopilotHeaderStore.getState().controls?.onSelectChat?.({+ workflow_copilot_chat_id: "chat-2",+ } as Parameters<+ NonNullable<+ NonNullable<+ ReturnType<typeof useCopilotHeaderStore.getState>["controls"]+ >["onSelectChat"]+ >+ >[0]);+ });++ // The hold has no deadline, so its exit has to survive the proposal going away. A card+ // with no Try again would be a hold the user cannot end except by reloading the page.+ const retry = await screen.findByRole("button", { name: "Try again" });+ expect(retry.matches(":disabled")).toBe(false);++ editorAccepts = true;+ await act(async () => {+ fireEvent.click(retry);+ });+ expect(applied).toContainEqual(savedWorkflow);+ await waitFor(() => expect(saveHeld()).toBe(false));+ });++ it("does not let an abandoned initial history read restore Always accept", async () => {+ const onWorkflowUpdate = vi.fn();+ // The INITIAL history read pends, carrying the old chat's Always accept.+ let releaseMount: () => void = () => {};+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ releaseMount = () =>+ resolve({+ data: {+ ...historyResponse.data,+ workflow_copilot_chat_id: "chat-old",+ auto_accept: true,+ },+ });+ }),+ );+ await renderChat({ docked: true, onWorkflowUpdate });+ // New chat is reachable here: it neither unmounts the component nor is disabled while+ // history loads, because `newChatDisabled` keys on the Accept hold only.+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "New chat" }));+ });+ await act(async () => {+ releaseMount();+ });++ const nextWorkflow = proposedWorkflowPayload({+ workflow_id: "wf_next",+ }) as unknown as WorkflowApiResponse;+ await submit("something new");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(+ proposalResponse("Draft.", {+ turn_id: "turn-9",+ proposal_disposition: "auto_applicable",+ updated_workflow: nextWorkflow,+ }),+ );+ streamCalls[0]!.resolve();+ });++ // The abandoned read must not put the old chat's Always accept back: if it does, this+ // proposal is applied locally and the user never sees the review gate.+ //+ // READ THIS BEFORE DELETING THIS TEST. Dropping the New chat click above makes it fail,+ // and that is NOT the test failing to discriminate. Without New chat the hydration+ // legitimately sets Always accept and applying IS correct. The discriminator is the pair:+ // WITH New chat the right answer is "not applied", and before the epoch guard we observed+ // "applied". The single useful control is the stale row carrying auto_accept false, which+ // makes this assertion pass for the boring reason.+ expect(onWorkflowUpdate).not.toHaveBeenCalledWith(+ nextWorkflow,+ expect.objectContaining({ applied: true }),+ );+ });++ // The gate owner's row decides WHICH site renders the card, so both shapes are driven here+ // against each other. A narrative row renders it inline at the narrative site; a plain row+ // has no narrative branch and must fall to the message footer. Both suppress the standalone+ // fallback, so each arm also asserts EXACTLY ONE card - a second card is as wrong as none,+ // and a single-element query would pass either way.+ it.each([+ ["a narrative row", true],+ ["a plain row with no narrative", false],+ ])(+ "keeps the confirmed-save exit reachable when the owner card is inline: %s",+ async (_label, withNarrative) => {+ let editorAccepts = true;+ const applied: WorkflowApiResponse[] = [];+ const onWorkflowUpdate = vi.fn((workflow: WorkflowApiResponse) => {+ if (!editorAccepts) {+ throw new Error("editor could not load it");+ }+ applied.push(workflow);+ });+ await renderChat({ docked: true, onWorkflowUpdate });+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });++ const savedWorkflow = proposedWorkflowPayload({+ workflow_id: "wf_saved",+ }) as unknown as WorkflowApiResponse;+ editorAccepts = false;+ cancelPost.mockResolvedValueOnce({ data: savedWorkflow });+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(await screen.findByText("Saved, not shown")).toBeTruthy();+ expect(saveHeld()).toBe(true);++ // A hydration for THIS chat lands: the proposal is correctly gone - the server cleared it+ // on the save - but the turn that owned it is still in the history, so its card still+ // renders inline, which suppresses the standalone gate.+ historyGet.mockImplementationOnce(() =>+ Promise.resolve({+ data: {+ ...historyResponse.data,+ proposed_workflow: null,+ chat_history: [+ {+ sender: "ai",+ content: "Draft ready.",+ created_at: new Date().toISOString(),+ ...(withNarrative+ ? { narrative_payload: proposalNarrativePayload() }+ : {}),+ },+ ],+ },+ }),+ );+ await act(async () => {+ await useCopilotHeaderStore.getState().controls?.onSelectChat?.({+ workflow_copilot_chat_id: "chat-rehydrate",+ } as Parameters<+ NonNullable<+ NonNullable<+ ReturnType<typeof useCopilotHeaderStore.getState>["controls"]+ >["onSelectChat"]+ >+ >[0]);+ });++ // The hold has no deadline, so the exit has to survive the proposal going away - including+ // when the inline owner card is the one rendering the gate.+ expect(await screen.findByText("Saved, not shown")).toBeTruthy();+ expect(screen.getAllByText("Saved, not shown")).toHaveLength(1);+ const retry = await screen.findByRole("button", { name: "Try again" });+ expect(retry.matches(":disabled")).toBe(false);++ editorAccepts = true;+ await act(async () => {+ fireEvent.click(retry);+ });+ expect(applied).toContainEqual(savedWorkflow);+ await waitFor(() => expect(saveHeld()).toBe(false));+ // The RECEIPT, which is the whole reason the `saved` gate carries its own owner turn:+ // hydration cleared pendingProposalTurnId, so without the carried id the retry marks+ // nothing and a save that definitely landed reports nothing. Asserting the applied+ // workflow alone leaves that regression invisible - both arms stay green with the carried+ // id replaced by null.+ //+ // Only the narrative row can show it: `settled` is computed at the narrative render site+ // and hardcoded to null at the message-footer and fallback sites, so a plain row's card+ // simply disappears when the gate clears. That asymmetry is a separate gap, recorded here+ // rather than asserted away - the plain arm checks the half it CAN observe.+ if (withNarrative) {+ expect(+ await screen.findByText("Accepted — saved to the workflow"),+ ).toBeTruthy();+ } else {+ await waitFor(() =>+ expect(screen.queryByText("Saved, not shown")).toBeNull(),+ );+ }+ },+ );++ it("keeps the stale-canvas hold, and its exit, when only an Accept outcome is proven", async () => {+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ 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.proposed_workflow = null;+ leaseDecrementingFrom(0.4);+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(+ await screen.findByText(/Couldn't reload this proposal/, undefined, {+ timeout: 4000,+ }),+ ).toBeTruthy();+ expect(saveHeld()).toBe(true);++ // Try again now reads a row whose proposal is still there AND carries metadata. The server+ // gates a tokenized proposal on the canonical fingerprint, so its survival proves canonical+ // never moved - nothing was saved, and the canvas nothing overtook is not stale.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "review_untested",+ workflow_run_id: null,+ };+ historyResponse.data.proposed_claim_expires_in_seconds = null;+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });++ // The hold STANDS: no persisted workflow reached the editor anywhere in this sequence.+ await waitFor(() => expect(saveHeld()).toBe(true));++ // And its exit is ON SCREEN in this state rather than merely written somewhere: the disabled+ // Save control carries the reload instruction in its ACCESSIBLE NAME, which is what a user+ // who presses Save actually meets. This is the difference between "the exit exists" and+ // "the exit renders in the state this hold makes more common".+ expect(+ screen.getByRole("button", { name: /reload the page first/i }),+ ).toBeTruthy();+ });++ it("keeps the fence when the apply succeeds but the editor cannot take the saved workflow", async () => {+ const onWorkflowUpdate = vi.fn(() => {+ throw new Error("could not parse the saved workflow");+ });+ await renderChat({ onWorkflowUpdate });+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ // The apply returns 200 — the version exists — and the canonical re-read that would+ // catch the canvas up is unavailable.+ cancelPost.mockResolvedValueOnce({ data: proposedWorkflowPayload() });+ historyGet.mockImplementation(() =>+ Promise.reject(new Error("network down")),+ );++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });++ // The studio's own handler shows "Update failed" and re-throws so this boolean is honest;+ // the wrapper must not add a second toast for the same event.+ expect(toast).not.toHaveBeenCalledWith(+ expect.objectContaining({ title: "Update failed" }),+ );+ // The server holds the accepted version and the canvas still holds the pre-Accept graph,+ // so releasing here would write the stale one back over it.+ await waitFor(() =>+ expect(+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled"),+ ).toBe(true),+ );+ });++ it("does not let a manual retry extend the fence past the lease it was opened for", async () => {+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ 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.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_claim_expires_in_seconds = 0.5;+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(await screen.findByRole("alert")).toBeTruthy();+ expect(saveHeld()).toBe(true);++ // Clicking Try again against a chat row that will not load must not open a fresh+ // five-minute fence: the claim it is waiting on expires when it always did, and the gate+ // hands over to the one the user ends. A retry that reopened the lease would still be+ // saying "Confirming…" here.+ historyGet.mockImplementation(() =>+ Promise.reject(new Error("network down")),+ );+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });+ expect(+ await screen.findByText(/Couldn't reload this proposal/, undefined, {+ timeout: 4000,+ }),+ ).toBeTruthy();+ expect(screen.queryByText("Confirming…")).toBeNull();+ // Waiting on the server ended on schedule; the stale canvas it left behind still holds.+ expect(saveHeld()).toBe(true);+ });++ it.each([+ ["reports the remainder", 120],+ ["is too old to report it", undefined],+ ] as const)(+ "restores the Accept fence when a reload lands while a server that %s still holds its claim",+ async (_label, claimTtlSeconds) => {+ // The tab reloads after Accept reached the backend and before it finished.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "accepting" as unknown as "review_untested",+ workflow_run_id: null,+ } as typeof historyResponse.data.proposed_workflow_metadata;+ leaseDecrementingFrom(claimTtlSeconds);++ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );++ // Hydrating this as an ordinary pending proposal would leave Save and the gate live+ // while the server may still be creating the version.+ await waitFor(() =>+ expect(+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled"),+ ).toBe(true),+ );+ for (const name of ["Accept", "Reject"]) {+ expect(screen.getByRole("button", { name }).matches(":disabled")).toBe(+ true,+ );+ }+ },+ );++ it("does not call a failed Accept applied when the surviving proposal still matches canonical", async () => {+ const onWorkflowUpdate = vi.fn();+ useWorkflowHasChangesStore.setState({+ getSaveData: () =>+ ({+ ...saveData,+ workflow: { ...saveData.workflow, version: 3 },+ }) as unknown as WorkflowSaveData,+ });+ await renderChat({ onWorkflowUpdate });+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ // The row still carries the proposal AND its metadata, which the history route only+ // returns while canonical matches the fingerprint the proposal was built against — so+ // canonical being ahead of the editor says the editor is stale, not that this Accept won.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "review_untested" as const,+ workflow_run_id: null,+ };+ historyGet.mockImplementation((path: string) =>+ Promise.resolve(+ path === "/workflows/wpid_1"+ ? { data: { ...proposedWorkflowPayload(), version: 4 } }+ : historyResponse,+ ),+ );+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });++ // Whichever failure it settles on, it may not be the one that claims success.+ expect(await screen.findByRole("alert")).toBeTruthy();+ expect(screen.queryByText("Accepted — saved to the workflow")).toBeNull();+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ });++ it("never reports a failed Accept as applied, even when a newer canonical version appeared", async () => {+ const onWorkflowUpdate = vi.fn();+ useWorkflowHasChangesStore.setState({+ getSaveData: () =>+ ({+ ...saveData,+ workflow: { ...saveData.workflow, version: 3 },+ }) as unknown as WorkflowSaveData,+ });+ await renderChat({ onWorkflowUpdate });+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });+ // The version was created but its proposal cleanup failed, and the response was lost.+ const created = { ...proposedWorkflowPayload(), version: 4 };+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyGet.mockImplementation((path: string) =>+ Promise.resolve(+ path === "/workflows/wpid_1" ? { data: created } : historyResponse,+ ),+ );+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });++ // The newer canonical is neither adopted - recovery never replaces the canvas - nor read+ // as evidence THIS Accept produced it: another writer's save looks identical, and a+ // proposal with no fingerprint carries less identity, not more.+ expect(await screen.findByRole("alert")).toBeTruthy();+ expect(onWorkflowUpdate).not.toHaveBeenCalledWith(+ created,+ expect.anything(),+ );+ expect(screen.queryByText("Accepted — saved to the workflow")).toBeNull();+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ expect(+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled"),+ ).toBe(false);+ });++ it("holds a block Generate requested during a pending Accept, then sends it", 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 resolveApply: (value: unknown) => void = () => {};+ cancelPost.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ resolveApply = resolve;+ }),+ );+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });++ await act(async () => {+ useCopilotActionStore+ .getState()+ .requestBuild({ blockLabel: "extract_titles", prompt: "read titles" });+ });+ const generateSent = () =>+ streamCalls.some((call) => call.body.message.includes("extract_titles"));+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 50));+ });+ // A turn started now could stage a proposal the pending Accept then clears.+ expect(generateSent()).toBe(false);++ await act(async () => {+ resolveApply({ data: proposedWorkflowPayload() });+ });+ await waitFor(() => expect(generateSent()).toBe(true));+ });++ it("ignores a reload retry that lands after a newer turn in the same chat", 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();+ });+ await submit("also grab the story scores");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ historyGet.mockRejectedValueOnce(new Error("network down"));+ await act(async () => {+ streamCalls[1]!.onMessage(+ plainReplyResponse("I'll fold that into the draft above."),+ );+ streamCalls[1]!.resolve();+ });+ expect(await screen.findByRole("alert")).toBeTruthy();+ // The held reload will answer with a row that no longer has a proposal.+ let finishOldReload: () => void = () => {};+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ finishOldReload = () => resolve(historyResponse);+ }),+ );+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });++ await submit("rebuild it from scratch");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(3));+ await act(async () => {+ streamCalls[2]!.onMessage(+ proposalResponse("Rebuilt draft ready.", {+ turn_id: "turn-3",+ narrative_payload: proposalNarrativePayload({ turnId: "turn-3" }),+ }),+ );+ streamCalls[2]!.resolve();+ });+ await act(async () => {+ finishOldReload();+ });++ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ expect(screen.queryByRole("alert")).toBeNull();+ });++ it("interrupted_draft reload shows an actionable candidate and its exact run result", async () => {+ // A hard kill can leave only the submitted user row. MessageItem does not+ // render footers on user bubbles, so the durable candidate needs a+ // standalone review gate until an assistant-owned row exists.+ historyResponse.data.chat_history = [+ {+ sender: "user",+ content: "build the candidate",+ created_at: "2026-09-08T12:00:00Z",+ },+ ];+ historyResponse.data.proposed_workflow = proposedWorkflowPayload({+ title: "Recovered draft",+ });+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-interrupted",+ revision: 3,+ canonical_fingerprint: "canonical-1",+ disposition: "review_untested",+ workflow_run_id: "wr-exact",+ };+ historyResponse.data.proposed_workflow_run = {+ workflow_run_id: "wr-exact",+ status: "completed",+ available: true,+ failure_reason: null,+ outputs: [{ output_parameter_id: "op-metric", value: { metric: "42" } }],+ };++ await renderChat();++ expect(await screen.findByText("Associated test: completed")).toBeTruthy();+ expect(screen.getByText(/op-metric:.*42/)).toBeTruthy();+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ expect(screen.getByRole("button", { name: "Reject" })).toBeTruthy();+ });++ it("restores an actionable gate via the chip after a bypassed proposal (old code: buttons vanish forever)", 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();+ });++ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ expect(screen.getByRole("button", { name: "Reject" })).toBeTruthy();+ expect(screen.getByRole("button", { name: "Always accept" })).toBeTruthy();+ expect(screen.getByRole("button", { name: "Review" })).toBeTruthy();++ // Bypass: send a follow-up instead of acting on the gate.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ await submit("also grab the story scores");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));++ // keep_pending_proposal must ride along on the bypassing request.+ 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.getByRole("button", { name: /1 proposal pending/ }),+ ).toBeTruthy();++ // Turn 2 ends with no new proposal; resync picks the row back up.+ await act(async () => {+ streamCalls[1]!.onMessage(+ plainReplyResponse("I'll fold that into the draft above."),+ );+ streamCalls[1]!.resolve();+ });+ await waitFor(() => expect(historyGet).toHaveBeenCalled());++ await waitFor(() =>+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy(),+ );+ // 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.getByRole("button", { name: /1 proposal pending/ });+ await act(async () => {+ fireEvent.click(chip);+ });+ expect(HTMLElement.prototype.scrollIntoView).toHaveBeenCalled();++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ await waitFor(() =>+ expect(cancelPost).toHaveBeenCalledWith(+ "/workflow/copilot/apply-proposed-workflow",+ expect.objectContaining({ workflow_copilot_chat_id: "chat-1" }),+ ),+ );+ });++ 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 accept's row write", async () => {+ // CHANGED BY THE #17099 MERGE. This asserted the same ordering against the CLIENT-SIDE+ // FALLBACK's row write (a v1 proposal whose apply 500s, applied locally and cleared by the+ // client). Slice 1 removes that fallback, so the vehicle is gone - but the concern is not:+ // the apply POST itself carries `auto_accept`, so it is still a write that can overwrite a+ // Turn off sent while it is in flight. Re-pointed at that write; the ordering it guards, and+ // the acceptsInFlight chain that enforces it, are unchanged.+ 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("Draft ready."));+ streamCalls[0]!.resolve();+ });+ let finishRowWrite: (value: unknown) => void = () => {};+ cancelPost.mockImplementation((path: string) => {+ if (path === "/workflow/copilot/apply-proposed-workflow") {+ return new Promise((resolve) => (finishRowWrite = resolve));+ }+ 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 () => {- finishApplies[0]!({});+ finishRowWrite({+ data: proposedWorkflowPayload({ workflow_id: "wf_saved" }),+ });+ });+ 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,+ });+ historyResponse.data.chat_history = [+ {+ sender: "ai",+ content: "Here is a draft.",+ created_at: "2026-07-09T00:00:05Z",+ narrative_payload: null,+ turn_outcome: null,+ },+ ];+ await renderChat();++ await waitFor(() => expect(screen.getByText("Untested")).toBeTruthy());+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ });++ it("renders a legacy (no-narrative) pending turn as pill + footer, no changes body", async () => {+ historyResponse.data.proposed_workflow = proposedWorkflowPayload({+ _copilot_unvalidated: true,+ });+ historyResponse.data.chat_history = [+ {+ sender: "ai",+ content: "Here is a draft.",+ created_at: "2026-07-09T00:00:05Z",+ narrative_payload: null,+ turn_outcome: null,+ },+ ];+ await renderChat();++ await waitFor(() =>+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy(),+ );+ expect(screen.getByText("Proposed changes")).toBeTruthy();+ expect(screen.queryByText("Added")).toBeNull();+ 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.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 () => {+ 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();+ });+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();++ // Always-accept echo: this follow-up comes back auto-applied instead of another gate.+ await submit("actually just fix the typo directly");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ expect(streamCalls[1]!.body.keep_pending_proposal).toBe(true);++ await act(async () => {+ streamCalls[1]!.onMessage(+ proposalResponse("Fixed and applied.", {+ proposal_disposition: "auto_applicable",+ workflow_applied: true,+ turn_id: "turn-2",+ narrative_payload: proposalNarrativePayload({+ turnId: "turn-2",+ turnIndex: 1,+ proposalDisposition: "auto_applicable",+ terminalMessage: "Fixed and applied.",+ narrativeSummary: "Fixed and applied.",+ }),+ }),+ );+ streamCalls[1]!.resolve();+ });++ await waitFor(() =>+ expect(screen.queryByRole("button", { name: "Accept" })).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 () => {+ await renderChat();++ await submit("rename the blocks.");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(legacyProposalResponse("Renamed the blocks."));+ streamCalls[0]!.resolve();+ });++ await waitFor(() =>+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy(),+ );+ expect(screen.getByRole("button", { name: "Reject" })).toBeTruthy();+ expect(screen.getByText("Proposed changes")).toBeTruthy();+ // The gate itself still appears live (SKY-14099); with no turn behind it there are+ // no facts to project, so it carries no verdict pill either way.+ expect(screen.queryByText("Tested")).toBeNull();+ });++ it("clears the proposal and shows a discarded receipt on a late Reject", 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();+ });+ await waitFor(() =>+ expect(screen.getByRole("button", { name: "Reject" })).toBeTruthy(),+ );++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Reject" }));+ });++ await waitFor(() =>+ expect(cancelPost).toHaveBeenCalledWith(+ "/workflow/copilot/clear-proposed-workflow",+ expect.objectContaining({ workflow_copilot_chat_id: "chat-1" }),+ ),+ );+ expect(+ screen.getByText("Discarded — canvas reverted to the previous version"),+ ).toBeTruthy();+ expect(screen.queryByRole("button", { name: "Reject" })).toBeNull();+ });++ it("surfaces per-block coverage tags through the mounted chat, not only in an isolated card", async () => {+ await renderChat();++ await submit("edit one block after it passed.");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(+ proposalResponse("Draft needs review.", {+ narrative_payload: {+ ...editOneOfTwoBundle,+ turnId: "turn-1",+ },+ }),+ );+ streamCalls[0]!.resolve();+ });++ await waitFor(() =>+ expect(screen.getByText("Different source")).toBeTruthy(),+ );+ expect(screen.queryByRole("button", { name: "Collapse turn" })).toBeNull();+ expect(+ screen.queryByRole("button", { name: /Draft needs review/ }),+ ).toBeNull();+ expect(screen.queryByText("Built and tested the workflow")).toBeNull();+ });++ it("keeps the newest structured response in its expanded reading state", 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();});+await waitFor(() =>- expect(requestedPaths()).toContain(- "/workflow/copilot/disable-auto-accept",- ),+ expect(+ screen.getByText("Here is a draft workflow for you to review."),+ ).toBeTruthy(),);+ expect(screen.queryByRole("button", { name: "Collapse turn" })).toBeNull();expect(- requestedPaths().lastIndexOf("/workflow/copilot/apply-proposed-workflow"),- ).toBeLessThan(- requestedPaths().indexOf("/workflow/copilot/disable-auto-accept"),+ screen.queryByRole("button", { name: /Draft needs review/ }),+ ).toBeNull();+ });++ // The terminal pass is what stops "Confirming…" sitting there ONCE IT IS REACHED, and it gets+ // there by ignoring the claim. Reaching it is a separate problem, pinned by `does not re-arm+ // against its own expired deadline when a superseded pass settles first` and the two `re-arms+ // for a Try again ...` tests: a gate replaced after the last re-arm ran gets no timer unless+ // the effect is keyed on the gate itself.+ //+ // CHECK THE NEXT TWO PARAGRAPHS AGAINST THE CODE BEFORE RELYING ON THEM; the PR body records+ // their history. No running count here on purpose - a number you must maintain is a claim you+ // must re-verify every round, and this one went stale twice.+ //+ // `keeps Save held when another writer holds a fresh claim at the terminal pass` and `still+ // holds Save at the terminal pass when a fresh claim has no surviving proposal` share the fresh+ // 300-second claim but are NOT a minimal pair: the first adds `proposed_workflow` AND+ // `proposed_workflow_metadata`, the second adds neither. They are not separated by MECHANISM+ // either. Both rows REPORT a lease, and `claimHoldDeadline` returns a deadline from that number+ // without ever consulting metadata - its metadata branch is reached only when the field is+ // ABSENT. So the renewed-claim hold is the same in both, and the second test holds by three+ // independent routes, staying green under mutations the first catches.+ //+ // The pair is not minimal BECAUSE THE MINIMAL PAIR DOES NOT EXIST. The obvious repair is to+ // make the two differ in one field; that is impossible rather than merely unwanted, so do not+ // add a third test for the missing corner. A CHAT-HISTORY ROW never reports metadata with a+ // null proposal - the scope that matters, because these tests drive history rows. There is+ // exactly ONE history producer: `WorkflowCopilotChatHistoryResponse` is constructed in a single+ // place, fed by `_history_proposal_state`, which returns non-null metadata on exactly one of its+ // four paths - and that path returns the proposal beside it. The other two producers of the+ // field are STREAM frames, which is why the stream is NOT so constrained: each takes metadata+ // from the STORED proposal while sending this turn's `updated_workflow`, so a reply-turn frame+ // can carry metadata beside a null workflow. The canonical-moved branch is the real shape of "a+ // fresh claim with no surviving proposal" - it reports the claim while returning no metadata,+ // deliberately, because hiding a live claim behind a hidden proposal is the lie that branch+ // exists to avoid. That is the state the second test covers.+ //+ // `releases Save at the terminal pass when an older instance omits the claim lease, which is+ // deliberate` reports the accepting claim WITHOUT its lease and asserts a release. Absent and+ // null are different answers there, and collapsing them into "no claim" is the coalescing+ // `claimHoldDeadline` exists to prevent.+ const armTerminalPassWithRenewedClaim = async () => {+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ 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.proposed_workflow = null;+ historyResponse.data.proposed_claim_expires_in_seconds = 0.4;+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ // Held BEFORE the terminal pass, so a later release is that pass and not something earlier.+ await waitFor(() => expect(saveHeld()).toBe(true));+ historyResponse.data.proposed_claim_expires_in_seconds = 300;+ return saveHeld;+ };++ it("ignores a superseded reconciliation response instead of releasing Save under a live claim", async () => {+ // Two reads can be in flight at once: the scheduled re-check and a manual Try again, which+ // stays enabled while that read is out. Neither call checked whether a newer one had already+ // answered, so arrival order alone decided the gate - a stale row landing last could report+ // "Nothing was saved" and release Save while another writer was mid-write. That is the silent+ // release this branch exists to prevent, reached from inside rather than from the server.+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,);+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await act(async () => {+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });++ // A short lease, so the scheduled pass is the terminal one and fires in hundreds of+ // milliseconds rather than at HOLD_RECHECK_MS (10s).+ historyResponse.data.proposed_claim_expires_in_seconds = 0.4;+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(await screen.findByText("Confirming…")).toBeTruthy();+ expect(saveHeld()).toBe(true);++ // The scheduled re-check fires and is held open mid-flight.+ let releaseScheduled: () => void = () => {};+ let scheduledCaptured = false;+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ scheduledCaptured = true;+ releaseScheduled = () =>+ resolve({+ data: {+ ...historyResponse.data,+ // A stale view: the claim looks gone and the proposal survives, which reads as+ // "nothing was saved" and would release the gate.+ proposed_workflow: proposedWorkflowPayload(),+ proposed_claim_expires_in_seconds: null,+ },+ });+ }),+ );+ const callsBeforeWait = historyGet.mock.calls.length;+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 900));+ });+ // SETUP ASSERTIONS: prove the scheduled read actually fired and is held open. Without these+ // a green result cannot distinguish "no race" from "the probe never created one".+ expect(historyGet.mock.calls.length).toBeGreaterThan(callsBeforeWait);+ expect(scheduledCaptured).toBe(true);++ // Try again is still enabled during that read, and its own read sees a live claim. A short+ // lease so the gate it establishes has a reachable next deadline.+ historyResponse.data.proposed_claim_expires_in_seconds = 1.5;+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });+ await waitFor(() => expect(saveHeld()).toBe(true));++ // The superseded scheduled response lands last. It must not overwrite what the newer read+ // established.+ await act(async () => {+ releaseScheduled();+ });+ // Assert the GATE, not only Save: `staleCanvas` can hold Save independently, so a Save-only+ // assertion would pass even if the superseded response had overwritten the gate.+ expect(screen.queryByText(/Nothing was saved/i)).toBeNull();+ expect(screen.queryByText(/Proposal changed/i)).toBeNull();+ expect(saveHeld()).toBe(true);++ // And the newer gate still has a TIMER. A terminal pass does not normally re-arm, so a+ // superseded one has to re-arm for the gate that replaced it - otherwise "Confirming…" sits+ // here with nothing scheduled and only a click ends it, which is the strand this branch+ // removes everywhere else. Asserting the instant after the stale response lands cannot see+ // that; only running past the next deadline can.+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 2600));+ });await waitFor(() =>- expect(- screen.queryByRole("button", { name: /Auto-accepting/ }),- ).toBeNull(),+ expect(screen.queryByText("Confirming\u2026")).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;+ it("does not re-arm against its own expired deadline when a superseded pass settles first", async () => {+ // The reverse-order case for `ignores a superseded reconciliation response instead of+ // releasing Save under a live claim`: the superseded response settles while the manual+ // read is still out. It pins that no request is issued in that window - re-arming there would+ // supersede a read that has not answered yet.+ //+ // It also separates the guarded re-arm from an UNCONDITIONAL one - but only because the wait+ // below sits in its OWN act() scope. A re-arm's re-render lands when the releasing scope+ // exits, so a wait sharing that scope passes through a window in which no re-arm timer exists+ // yet and can never observe the spurious read. That is why three earlier attempts here failed+ // to discriminate: a harness artefact, not a property of the code under test.await renderChat();- await submit("add a step");+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ await submit("build me a workflow");await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));await act(async () => {- streamCalls[0]!.onMessage(proposalResponse("Untested draft."));+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));+ streamCalls[0]!.resolve();+ });++ historyResponse.data.proposed_claim_expires_in_seconds = 0.4;+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(await screen.findByText("Confirming…")).toBeTruthy();++ // Hold the scheduled terminal read open.+ let releaseScheduled: () => void = () => {};+ let scheduledCaptured = false;+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ scheduledCaptured = true;+ releaseScheduled = () =>+ resolve({+ data: {+ ...historyResponse.data,+ proposed_workflow: proposedWorkflowPayload(),+ proposed_claim_expires_in_seconds: null,+ },+ });+ }),+ );+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 900));+ });+ expect(scheduledCaptured).toBe(true);++ // Start the manual read and hold it too, so the two can be released in a chosen order.+ let releaseManual: () => void = () => {};+ let manualCaptured = false;+ historyResponse.data.proposed_claim_expires_in_seconds = 1.5;+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ manualCaptured = true;+ releaseManual = () => resolve({ data: { ...historyResponse.data } });+ }),+ );+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });+ expect(manualCaptured).toBe(true);++ // The stale one settles FIRST, while the manual read is still out.+ const readsBeforeRelease = historyGet.mock.calls.length;+ await act(async () => {+ releaseScheduled();+ });+ // Separate scope, deliberately: see the note at the top of this test. The spurious re-arm is+ // scheduled rather than synchronous, so the timer needs a window to fire in a scope where it+ // actually exists before asserting that it did not.+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 250));+ });+ // No request may be issued here: the gate still carries this pass's own expired deadline, so+ // re-arming now would supersede the manual read that has not answered yet.+ expect(historyGet.mock.calls.length).toBe(readsBeforeRelease);++ await act(async () => {+ releaseManual();+ });+ });++ it("re-arms for a Try again that inherited this pass's own deadline", async () => {+ // The gate a Try again installs carries the SAME `holdExpiresAt` by design - `retryGateFailure`+ // hands the old deadline back as `holdUntil`, and a read that finds no claim, finds no proposal+ // or fails outright returns it unchanged. So "the deadline moved" is not the same proposition as+ // "the gate was replaced", and a re-arm keyed on the deadline never fires for the one+ // replacement the user performs by hand: the superseded pass sees equal deadlines and skips the+ // bump, the effect sees an unchanged dependency and never re-runs, and nothing is left to end+ // "Confirming…" but another click - which does not exit either, because it reproduces this+ // exact state. `ignores a superseded reconciliation response instead of releasing Save under+ // a live claim` passes only because its Try again reads a LIVE CLAIM,+ // which mints a different deadline.+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ 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.proposed_claim_expires_in_seconds = 0.4;+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(await screen.findByText("Confirming…")).toBeTruthy();++ // Hold the scheduled pass open so its timer is spent and a read is in flight.+ let releaseScheduled: () => void = () => {};+ let scheduledCaptured = false;+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ scheduledCaptured = true;+ releaseScheduled = () =>+ resolve({+ data: {+ ...historyResponse.data,+ proposed_workflow: proposedWorkflowPayload(),+ proposed_claim_expires_in_seconds: null,+ },+ });+ }),+ );+ const callsBeforeWait = historyGet.mock.calls.length;+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 900));+ });+ // SETUP ASSERTIONS: without these a green result cannot tell "re-armed correctly" from "the+ // probe never spent the original timer", which is the only thing making the gate depend on a+ // re-arm at all.+ expect(historyGet.mock.calls.length).toBeGreaterThan(callsBeforeWait);+ expect(scheduledCaptured).toBe(true);++ // Try again, and its read FAILS - the route to `unreadable()`, which returns the inherited+ // deadline unchanged. This is the step the sibling test does differently.+ historyGet.mockRejectedValueOnce(new Error("Network Error"));+ const callsBeforeRetry = historyGet.mock.calls.length;+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });+ // SETUP ASSERTIONS: the retry read actually fired, and it left the gate still holding. A+ // green result would otherwise be consistent with the click doing nothing at all.+ expect(historyGet.mock.calls.length).toBeGreaterThan(callsBeforeRetry);+ expect(screen.queryByText("Confirming…")).not.toBeNull();++ // The superseded pass lands last and must re-arm for the gate that replaced it.+ historyGet.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ releaseScheduled();+ });++ // Assert the GATE reaches a terminal state on its own. Nothing is clicked here: if the re-arm+ // is keyed on the deadline, no timer exists and "Confirming…" sits indefinitely.+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 1500));+ });+ await waitFor(() => expect(screen.queryByText("Confirming…")).toBeNull());+ });++ it("re-arms for a Try again whose gate lands after the superseded pass settled", async () => {+ // STALE-FIRST. `re-arms for a Try again that inherited this pass's own deadline` releases the+ // superseded pass AFTER Try again has already+ // installed its gate, so the `.finally` sees a replacement and bumps. In THIS order the stale+ // read settles while Try again's read is still out - so the gate is still the one that pass was+ // serving, the `.finally` correctly declines to bump, and the replacement lands LATER, when Try+ // again's own read resolves. By then the only re-arm path has already run. Nothing else can+ // notice the new gate unless the effect is keyed on the gate ITSELF: its deadline is unchanged+ // by design. This is the order that refuted "a recover gate always has either a pending timer+ // or an in-flight read".+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ 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.proposed_claim_expires_in_seconds = 0.4;+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(await screen.findByText("Confirming…")).toBeTruthy();++ // The scheduled terminal pass fires and is held open.+ let releaseScheduled: () => void = () => {};+ let scheduledCaptured = false;+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ scheduledCaptured = true;+ releaseScheduled = () =>+ resolve({+ data: {+ ...historyResponse.data,+ proposed_workflow: proposedWorkflowPayload(),+ proposed_claim_expires_in_seconds: null,+ },+ });+ }),+ );+ const callsBeforeWait = historyGet.mock.calls.length;+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 900));+ });+ expect(historyGet.mock.calls.length).toBeGreaterThan(callsBeforeWait);+ expect(scheduledCaptured).toBe(true);++ // Try again, held open too, so the two can be released in a chosen order. Its read REJECTS,+ // which is the route to `unreadable()` and so to a gate carrying the inherited deadline.+ let rejectManual: () => void = () => {};+ let manualCaptured = false;+ historyGet.mockImplementationOnce(+ () =>+ new Promise((_resolve, reject) => {+ manualCaptured = true;+ rejectManual = () => reject(new Error("Network Error"));+ }),+ );+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });+ expect(manualCaptured).toBe(true);++ // The stale pass settles FIRST, while Try again is still out. Its `.finally` must NOT bump:+ // the gate it served is still the installed one.+ const readsBeforeStale = historyGet.mock.calls.length;+ await act(async () => {+ releaseScheduled();+ });+ // The window has to be its OWN act(): a re-arm's re-render lands when the releasing scope+ // exits, so counting reads immediately after it sees a tick in which no timer existed yet.+ // Without this the assertion below cannot detect a re-arm at all - it would be a vacuity+ // check that is itself vacuous.+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 250));+ });+ // SETUP ASSERTION, and it is the precondition the whole test rests on: the stale pass issued+ // no re-arm here. If it had, the gate would already have a timer and the exit below would+ // prove nothing about a gate installed afterwards.+ expect(historyGet.mock.calls.length).toBe(readsBeforeStale);+ expect(screen.queryByText("Confirming…")).not.toBeNull();++ // Only now does Try again install its gate - after the last re-arm opportunity has passed.+ historyGet.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ rejectManual();+ });++ // Nothing is clicked from here. The gate must still reach a terminal state on its own.+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 1500));+ });+ await waitFor(() => expect(screen.queryByText("Confirming…")).toBeNull());+ });++ it("locks the pending question card while an Accept is unresolved", async () => {+ // `handleQuestionAnswer` is fenced and returns false SILENTLY - no request, no toast. So a card+ // that stays clickable under an unresolved Accept reports a submit that never happened, which+ // is the false receipt this whole slice exists to remove, reached without the server at all.+ // The Cancel control beside it already gates on the same fence; this card was the outlier.+ historyResponse.data.question_interactions = [+ {+ interaction_id: "q-1",+ turn_id: "turn-1",+ tool_call_id: "ask-1",+ status: "pending",+ response: null,+ created_at: "2026-09-17T00:00:01Z",+ resolved_at: null,+ parts: [+ {+ part_id: "p-1",+ prompt: "Which one?",+ choices: [{ choice_id: "a", text: "Column A" }],+ },+ ],+ },+ ];+ // The proposal arrives by HYDRATION rather than the composer: a question card renders its own+ // textarea, and the shared `submit` helper resolves a single textbox by role.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "review_untested",+ workflow_run_id: null,+ };+ await renderChat();++ // SETUP ASSERTION: the card is actionable BEFORE the fence closes. Without this the assertion+ // below passes for a card that was never enabled, or never rendered at all.+ const skip = await screen.findByRole("button", { name: "Skip" });+ expect(skip.matches(":disabled")).toBe(false);++ historyResponse.data.proposed_claim_expires_in_seconds = 300;+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(await screen.findByText("Confirming\u2026")).toBeTruthy();++ // Both controls inert. Send is scoped to the card's own action row - the composer has a Send+ // too, and an unscoped query would assert against whichever came first.+ await waitFor(() =>+ expect(+ screen.getByRole("button", { name: "Skip" }).matches(":disabled"),+ ).toBe(true),+ );+ const actionRow = screen.getByRole("button", { name: "Skip" })+ .parentElement!.parentElement!;+ expect(+ within(actionRow)+ .getByRole("button", { name: "Send" })+ .matches(":disabled"),+ ).toBe(true);+ // And the hold names itself where the choice count used to be, so the card is distinguishable+ // from a broken one.+ expect(within(actionRow).queryByText(/choices selected/)).toBeNull();+ });++ it("keeps the stale-canvas hold through a saved-gate retry that never refreshed the canvas", async () => {+ // THE COMPOUND CASE, and the only one the `fresh` narrowing changes. It is NOT the path Codex+ // filed: that one never arms `staleCanvas` at all, because both producers end in kind+ // "reload" and the saved gate is reached only when an apply 200s and the editor throws. Here+ // an EARLIER recovery arms the flag, and a LATER saved-gate retry used to clear it - applying+ // a workflow captured before that recovery, which says nothing about whether this canvas is+ // current. `persisted` was carrying that claim; only `fresh` should.+ //+ // Asserted at the user-visible outcome rather than the flag: during the saved gate+ // `acceptHoldReason` short-circuits the staleCanvas branch of `saveHoldReason`, so the flag is+ // unobservable until the retry clears the gate and the branch goes live again.+ let editorAccepts = true;+ const onWorkflowUpdate = vi.fn(() => {+ if (!editorAccepts) {+ throw new Error("editor could not load it");+ }+ });+ await renderChat({ onWorkflowUpdate });+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");++ // 1. Arm staleCanvas the only way anything does: recovery giving up into `reload`.+ 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.proposed_workflow = null;+ leaseDecrementingFrom(0.4);+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });expect(- await screen.findByRole("button", { name: "Always accept" }),+ await screen.findByText(/Couldn't reload this proposal/, undefined, {+ timeout: 4000,+ }),).toBeTruthy();- let finishTurnOff: (value: unknown) => void = () => {};- cancelPost.mockImplementation((path: string) =>- path === "/workflow/copilot/disable-auto-accept"- ? new Promise((resolve) => (finishTurnOff = resolve))- : Promise.resolve({}),+ expect(saveHeld()).toBe(true);++ // 2. A new turn. Sending drops the gate card; the canvas fact outlives it.+ leaseDecrementingFrom(null);+ await submit("another change");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ await act(async () => {+ streamCalls[1]!.onMessage(proposalResponse("Second draft."));+ streamCalls[1]!.resolve();+ });+ expect(screen.queryByText(/Couldn't reload this proposal/)).toBeNull();+ // SETUP ASSERTION: the hold must still be up here, or step 4 proves nothing about it.+ expect(saveHeld()).toBe(true);++ // 3. Accept: the server saves it and the editor refuses it. That is the `saved` gate.+ const savedWorkflow = proposedWorkflowPayload({+ workflow_id: "wf_saved",+ }) as unknown as WorkflowApiResponse;+ editorAccepts = false;+ cancelPost.mockResolvedValueOnce({ data: savedWorkflow });+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(await screen.findByText("Saved, not shown")).toBeTruthy();++ // 4. The retry succeeds and clears the gate. It installed a workflow captured in step 3, so+ // it has refreshed nothing: the hold from step 1 must survive it.+ editorAccepts = true;+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));+ });+ await waitFor(() =>+ expect(screen.queryByText("Saved, not shown")).toBeNull(),);+ expect(saveHeld()).toBe(true);+ });+ it("locks the continuation cards while an Accept is unresolved", async () => {+ // Same class as the question card, one hop further: `handleSend` returns silently when the+ // fence is up, so a Confirm click under an unresolved Accept reports an action that never+ // happened - a false receipt with no server involved. Reported by Codex and by a human+ // reviewer independently. The human also described the account picker as greying out for the+ // turn; that half does not hold, because `handleSend`'s early return RESOLVES and the+ // `.finally` clears the latch. The silent no-op is the real defect.+ // The pending proposal arrives by HYDRATION so a later reply turn cannot clear it - the+ // coexistence the finding describes: an older proposal still pending while the newest turn+ // ends in a confirmation request.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "review_untested",+ workflow_run_id: null,+ };+ await renderChat();+ await submit("and one more thing");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));await act(async () => {- fireEvent.click(screen.getByRole("button", { name: /Auto-accepting/ }));+ streamCalls[0]!.onMessage(+ plainReplyResponse("Ready - confirm and I'll apply the change.", {+ updated_workflow:+ proposedWorkflowPayload() as unknown as WorkflowApiResponse,+ proposal_disposition: "review_untested",+ }),+ );+ streamCalls[0]!.resolve();});- 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();+ // SETUP ASSERTION: actionable BEFORE the fence closes, so a green below cannot come from a+ // card that never rendered or was always inert.+ const confirm = await screen.findByRole("button", { name: "Confirm" });+ expect(confirm.matches(":disabled")).toBe(false);+ historyResponse.data.proposed_claim_expires_in_seconds = 300;+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));await act(async () => {- finishTurnOff({});+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));});+ expect(await screen.findByText("Confirming\u2026")).toBeTruthy();+await waitFor(() =>expect(- screen.queryByRole("button", { name: /Auto-accepting/ }),- ).toBeNull(),+ screen.getByRole("button", { name: "Confirm" }).matches(":disabled"),+ ).toBe(true),);- expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ // And the hold names itself on the card, not only in a title a disabled button may never fire.+ expect(+ screen.getAllByText(/Copilot is checking|may have saved/i).length,+ ).toBeGreaterThan(0);});- 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");+ it("names the unsaved canvas edits that a saved-gate retry would discard", async () => {+ // `saved`'s Try again REPLACES the canvas with the workflow the server confirmed. The card+ // already said "this replaces what's on the canvas" generically; it said the same thing when+ // there was nothing to lose and when there was. The editor already tracks the difference -+ // `hasChanges`, guarded by beginInternalUpdate so our own applies do not set it - and+ // `reconcileCanonicalWorkflow` already refuses to overwrite on exactly that signal. This card+ // names the cost while the work still exists.+ const onWorkflowUpdate = vi.fn(() => {+ throw new Error("editor could not load it");+ });+ await renderChat({ onWorkflowUpdate });+ await submit("build me a workflow");await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));await act(async () => {- streamCalls[0]!.onMessage(proposalResponse("Untested draft."));+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));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({}),- );+ const savedWorkflow = proposedWorkflowPayload({+ workflow_id: "wf_saved",+ }) as unknown as WorkflowApiResponse;+ cancelPost.mockResolvedValueOnce({ data: savedWorkflow });await act(async () => {- fireEvent.click(- await screen.findByRole("button", { name: "Always accept" }),- );+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));});- vi.useFakeTimers();+ expect(await screen.findByText("Saved, not shown")).toBeTruthy();++ // NEGATIVE ARM: a clean canvas has nothing to lose, so the card must not invent a cost.await act(async () => {- fireEvent.click(screen.getByRole("button", { name: /Auto-accepting/ }));+ useWorkflowHasChangesStore.setState({ hasChanges: false });});- expect(screen.queryByRole("button", { name: "Always accept" })).toBeNull();+ // Scoped to the ADDED sentence: the card's base copy already ends with "...which discards+ // unsaved canvas edits", so a loose match here would assert against the wrong text.+ expect(screen.queryByText(/canvas has unsaved changes/i)).toBeNull();+ // POSITIVE ARM: the user repaired something under the fence. Now it has to say so.await act(async () => {- await vi.advanceTimersByTimeAsync(ACCEPT_SETTLE_CEILING_MS + 1_000);+ useWorkflowHasChangesStore.setState({ hasChanges: true });});- 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" }),- );+ expect(await screen.findByText(/canvas has unsaved changes/i)).toBeTruthy();});- it("does not leak an Always accept into a New chat opened before it lands", async () => {+ it("does not let a stale reload retry resurrect the card a newer one cleared", async () => {+ // Try again on a `reload` with no attempt sets no `isAccepting` and disables nothing, so two+ // of these reads can be in flight. Only call identity separates them: the chat id and send+ // epoch this path already checks are identical across a double-click. Same guard, and the same+ // reason, as `reconcileFailedAccept`.await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");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({}),- );++ // A follow-up turn with no new draft re-reads the row for the bypassed proposal; that read+ // fails, which is the only way this gate is reached.+ historyGet.mockRejectedValueOnce(new Error("network down"));+ await submit("also grab the story scores");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));await act(async () => {- fireEvent.click(screen.getByRole("button", { name: "Always accept" }));+ streamCalls[1]!.onMessage(+ plainReplyResponse("I'll fold that into the draft above."),+ );+ streamCalls[1]!.resolve();});+ expect(screen.queryByText("Couldn't reload")).toBeTruthy();+ expect(saveHeld()).toBe(true);- // New chat clears the chat and its auto-accept while that apply is still in flight.- historyResponse.data.auto_accept = false;+ // Two retries, both held open, released newest-first.+ let failOlder: () => void = () => {};+ let succeedNewer: () => void = () => {};+ historyGet.mockImplementationOnce(+ () =>+ new Promise((_resolve, reject) => {+ failOlder = () => reject(new Error("older read lost"));+ }),+ );await act(async () => {- fireEvent.click(screen.getByRole("button", { name: "New chat" }));+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));});+ // The newer read SUCCEEDS and the proposal survives it. That matters: the reload card needs a+ // subject, so if the proposal were cleared here the card could not render whether or not the+ // stale catch restored the gate - and the assertion below would pass for the wrong reason.+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ succeedNewer = () =>+ resolve({+ data: {+ ...historyResponse.data,+ proposed_workflow: proposedWorkflowPayload(),+ },+ });+ }),+ );await act(async () => {- finishApply({ data: { workflow_id: "wf_proposed" } });+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));});- // 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();+ // SETUP ASSERTION: both reads really are in flight, or the ordering below proves nothing.+ expect(typeof failOlder).toBe("function");+ expect(typeof succeedNewer).toBe("function");- // 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();+ succeedNewer();+ });+ await waitFor(() =>+ expect(screen.queryByText("Couldn't reload")).toBeNull(),+ );++ // The older read now fails. Its catch must not restore a gate it no longer owns - that would+ // report a failure for a reload the user watched succeed, and hold Save behind it.+ await act(async () => {+ failOlder();+ });+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 100));});+ expect(screen.queryByText("Couldn't reload")).toBeNull();+ expect(saveHeld()).toBe(false);+ });- expect(await screen.findByRole("button", { name: "Accept" })).toBeTruthy();- expect(screen.queryByRole("button", { name: /Auto-accepting/ })).toBeNull();+ const runOutTerminalPass = async () => {+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 1500));+ });+ };++ it("keeps Save held when another writer holds a fresh claim at the terminal pass", async () => {+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = await armTerminalPassWithRenewedClaim();+ // Another writer took a full lease. Its proposal is still there because canonical has not+ // moved YET - that writer is mid-write, which is precisely when Save must not be released.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "accepting",+ workflow_run_id: null,+ };+ await runOutTerminalPass();+ expect(saveHeld()).toBe(true);});- it("ignores a read that started before a plain Accept turned auto-accept off", async () => {- historyResponse.data.auto_accept = true;+ it("releases Save at the terminal pass when an older instance omits the claim lease, which is deliberate", async () => {+ // The terminal-pass hold compares the REPORTED remainder, because at this pass our own+ // residual and another writer's renewal both compute to the same deadline and only the+ // reported number tells them apart. An instance predating that field reports nothing, so the+ // hold cannot fire and Save is released - a takeover claim goes unseen for the length of the+ // backend rollout. Declined rather than overlooked: holding on any omitted-field `accepting`+ // would also hold Save over the leftover row a crashed Accept leaves behind, which is a likely+ // state for a recovery to be in, and that is the strand this branch exists to remove.+ //+ // The remedy that avoids that cost is claim IDENTITY - comparing `claimed_at` rather than+ // doing clock arithmetic on a remainder. The client type does not declare that field today.+ // THIS PIN RETIRES once every instance reports the lease.+ //+ // Pinned so a change that makes this guard fire on an absent value fails here rather than+ // silently. Its control is `keeps Save held when another writer holds a fresh claim at the+ // terminal pass`, identical but for the field being reported.await renderChat();- await submit("add a step");+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = await armTerminalPassWithRenewedClaim();+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "accepting",+ workflow_run_id: null,+ };+ // The only difference from the control: an older instance cannot report the lease.+ historyResponse.data.proposed_claim_expires_in_seconds = undefined;+ await runOutTerminalPass();+ expect(saveHeld()).toBe(false);+ });++ it("still holds Save at the terminal pass when a fresh claim has no surviving proposal", async () => {+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = await armTerminalPassWithRenewedClaim();+ await runOutTerminalPass();+ expect(saveHeld()).toBe(true);+ });++ // Accept with no chat id yet: it resolves one mid-flight, and the ref that names it is+ // assigned by a passive effect, so the ref still reads empty for the rest of the handler.+ // `reconciles a failed Accept against the chat id it just resolved` and `holds the fence when+ // the chat row read comes back empty` both drive that window; they differ in what the row finds,+ // because+ // that is the only thing that tells the two halves of this fix apart.+ const acceptWithChatIdResolvedMidFlight = async () => {+ historyResponse.data.workflow_copilot_chat_id = null;+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ await submit("build me a workflow");await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));await act(async () => {- streamCalls[0]!.onMessage(proposalResponse("Untested draft."));+ streamCalls[0]!.onMessage(+ proposalResponse("Draft ready.", {+ workflow_copilot_chat_id: undefined,+ }),+ );streamCalls[0]!.resolve();});- expect(- await screen.findByRole("button", { name: /Auto-accepting/ }),- ).toBeTruthy();+ expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();+ historyResponse.data.workflow_copilot_chat_id = "chat-1";+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ };- // 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 } });+ const saveIsHeld = () =>+ screen.getByRole("button", { name: /^Save workflow/ }).matches(":disabled");++ it("reconciles a failed Accept against the chat id it just resolved", async () => {+ await acceptWithChatIdResolvedMidFlight();+ // The row still carries the proposal, so reading it settles the outcome DEFINITELY.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();await act(async () => {fireEvent.click(screen.getByRole("button", { name: "Accept" }));});+ // Reading the row requires the id this handler is holding; the ref is still empty. Without+ // it the read is skipped and the gate can only say "may have saved" - so this asserts the+ // DEFINITE verdict, not merely that some fence exists.+ expect(await screen.findByText("Not saved")).toBeTruthy();+ expect(screen.queryByText("Confirming\u2026")).toBeNull();+ });- // The retry succeeds: a plain Accept writes auto_accept=false on the row.- historyResponse.data.auto_accept = false;+ it("holds the fence when the chat row read comes back empty", async () => {+ await acceptWithChatIdResolvedMidFlight();+ // Resolve the id, then answer the row read with no row at all.+ historyGet.mockImplementationOnce(() => Promise.resolve(historyResponse));+ historyGet.mockImplementationOnce(() => Promise.resolve({ data: null }));await act(async () => {- fireEvent.click(await screen.findByRole("button", { name: "Accept" }));+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));});- await waitFor(() =>- expect(- screen.queryByRole("button", { name: /Auto-accepting/ }),- ).toBeNull(),+ // Nothing identified the outcome, and this handler cleared its own gate before the apply,+ // so it owes a replacement. Unknown may not release Save.+ await waitFor(() => expect(saveIsHeld()).toBe(true));+ });++ it("keeps Save held when a reload's own fence reaches its terminal pass", async () => {+ // A reload lands on a claim with almost no lease left, so the fence hydration arms schedules+ // a TERMINAL recheck immediately - the first one, with no earlier read behind it. That is+ // the only path to the terminal pass that reconciliation never touches.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "accepting",+ workflow_run_id: null,+ };+ historyResponse.data.proposed_claim_expires_in_seconds = 0.4;+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,);+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await waitFor(() => expect(saveHeld()).toBe(true));+ // Another writer takes a fresh full lease before that terminal read runs.+ historyResponse.data.proposed_claim_expires_in_seconds = 300;await act(async () => {- finishStaleRead({ data: { ...historyResponse.data, auto_accept: true } });+ await new Promise((resolve) => setTimeout(resolve, 1500));});-- expect(screen.queryByRole("button", { name: /Auto-accepting/ })).toBeNull();+ expect(saveHeld()).toBe(true);});- it("keeps Accept withheld until every Turn off for the chat settles, not just the first", async () => {- historyResponse.data.auto_accept = true;+ it("holds Save when a terminal pass finds a live claim it has no baseline for", async () => {await renderChat();- await submit("add a step");+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));await act(async () => {- streamCalls[0]!.onMessage(proposalResponse("Untested draft."));+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));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/,- });+ // A failed Accept arms the fence against a claim of ours.+ historyResponse.data.proposed_workflow = null;+ historyResponse.data.proposed_claim_expires_in_seconds = 1.5;+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));await act(async () => {- fireEvent.click(chip);- fireEvent.click(chip);+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));});- expect(heldDisables).toHaveLength(2);- expect(screen.queryByRole("button", { name: "Always accept" })).toBeNull();+ await waitFor(() => expect(saveHeld()).toBe(true));- // The first settles while the second is still in flight: the gate must stay withheld.+ // Try again finds NO claim: ours resolved, so the remembered remainder is gone with it.+ historyResponse.data.proposed_claim_expires_in_seconds = null;await act(async () => {- heldDisables[0]!({});+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));});- expect(screen.queryByRole("button", { name: "Always accept" })).toBeNull();- expect(screen.queryByRole("button", { name: "Accept" })).toBeNull();+ // Another writer then takes a lease before the terminal pass. It is SMALLER than the one+ // this fence first saw, so a comparison against a stale remainder would read it as our own+ // claim decaying and release Save over an active writer.+ //+ // The row carries METADATA on purpose: without it the claim is unattributable and the+ // WORKFLOW-level Save hold arms instead, which would hold Save for a different reason and+ // leave this test green whatever the renewal rule does. It has to be a claim the row can+ // tie to a proposal, so the only thing that can hold Save here is the renewal check.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "accepting",+ workflow_run_id: null,+ };+ historyResponse.data.proposed_claim_expires_in_seconds = 100;await act(async () => {- heldDisables[1]!({});+ await new Promise((resolve) => setTimeout(resolve, 2500));});- await waitFor(() =>- expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy(),- );+ expect(saveHeld()).toBe(true);});- it("gives the gate its Accept back when Turn off fails", async () => {- historyResponse.data.auto_accept = true;+ it("holds Save when a Reject-409 resync finds a claim it cannot attribute", async () => {+ // The unattributed-claim hold is a fact about the WORKFLOW, so it cannot live on the+ // hydration path alone: this row is learned through the resync a 409 drives, and another+ // writer holding the workflow is just as true when that is how we found out.await renderChat();- await submit("add a step");+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));await act(async () => {- streamCalls[0]!.onMessage(proposalResponse("Untested draft."));+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));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({}),+ expect(saveHeld()).toBe(false);++ // A concurrent save moved canonical, so the proposal is withheld - but its claim is live,+ // and the server reports it independently now.+ historyResponse.data.proposed_workflow = null;+ historyResponse.data.proposed_workflow_metadata = null;+ historyResponse.data.proposed_claim_expires_in_seconds = 100;+ cancelPost.mockRejectedValueOnce({ response: { status: 409 } });+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Reject" }));+ });++ await waitFor(() => expect(saveHeld()).toBe(true));+ });++ it("keeps the baseline a renewal is measured against when an ordinary poll sees one", async () => {+ // Every other renewal test enters at the TERMINAL pass. This one drives the renewal arriving+ // on an ORDINARY re-check, which re-arms the fence - and a re-arm that adopted the new+ // reading as its baseline would leave the terminal pass comparing that writer's own lease+ // against itself, finding it smaller as it decays, and releasing Save under an active write.+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,);+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ 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.proposed_workflow = null;+ historyResponse.data.proposed_claim_expires_in_seconds = 1.2;+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));await act(async () => {- fireEvent.click(- await screen.findByRole("button", { name: /Auto-accepting/ }),- );+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ await waitFor(() => expect(saveHeld()).toBe(true));++ // An ordinary re-check finds a LARGER remainder: our claim lapsed and another writer took+ // one. Try again is exactly that re-check - it re-arms the fence without being terminal.+ historyResponse.data.proposed_claim_expires_in_seconds = 2;+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));});- expect(screen.queryByRole("button", { name: "Always accept" })).toBeNull();+ // The terminal pass then sees that writer's lease part-way through decaying, with its+ // proposal still visible because canonical has not moved yet.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "accepting",+ workflow_run_id: null,+ };+ historyResponse.data.proposed_claim_expires_in_seconds = 1.6;await act(async () => {- failTurnOff({ response: { status: 500 } });+ await new Promise((resolve) => setTimeout(resolve, 3000));});+ expect(saveHeld()).toBe(true);+ }, 15000);- // 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;+ it("holds when a claim reappears after a read that reported none", async () => {+ // A read reporting NO claim is positive evidence that the lease ended - not absence of+ // information. Any claim seen afterwards was taken later, so with ABSOLUTE expiries it+ // cannot be the old one decaying: a lease is a fixed length added to claimed_at, so a newer+ // claim always expires later. A RELATIVE remainder cannot express that - a smaller number+ // read later looks like decay - which is why this sequence releases Save under it.await renderChat();- await submit("add a step");+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");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]!.onMessage(proposalResponse("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({});- });++ historyResponse.data.proposed_workflow = null;+ const oursEndsAt = leaseDecrementingFrom(1.2);+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));await act(async () => {- fireEvent.click(- await screen.findByRole("button", { name: "Always accept" }),- );+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));});+ await waitFor(() => expect(saveHeld()).toBe(true));- // That write ends with auto_accept=true, so a Turn off sent first is silently overwritten.+ // A re-check finds NO claim at all: ours is provably over.+ historyResponse.data.proposed_claim_expires_in_seconds = null;await act(async () => {- fireEvent.click(screen.getByRole("button", { name: /Auto-accepting/ }));+ fireEvent.click(screen.getByRole("button", { name: "Try again" }));});- expect(cancelPost.mock.calls.map(([path]) => path)).not.toContain(- "/workflow/copilot/disable-auto-accept",- );+ const heldAfterNullRead = saveHeld();++ // Another writer then claims. Its remainder is SMALLER than the one this fence opened with,+ // so a relative comparison reads it as our own lease decaying.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "accepting",+ workflow_run_id: null,+ };+ // A LEASE IS A FIXED LENGTH added to claimed_at, so a claim taken LATER always expires+ // later - even though its REMAINDER read at any instant is smaller than ours was at ours.+ // That is the whole of the distinction: 0.5s past our expiry, and a smaller remainder.+ leaseDecrementingFrom((oursEndsAt + 500 - Date.now()) / 1000);await act(async () => {- finishRowWrite({ data: {} });+ await new Promise((resolve) => setTimeout(resolve, 2500));});- await waitFor(() =>- expect(cancelPost.mock.calls.map(([path]) => path)).toContain(- "/workflow/copilot/disable-auto-accept",- ),+ expect(heldAfterNullRead).toBe(true);+ expect(saveHeld()).toBe(true);+ }, 15000);++ it("holds Save when mount hydration finds a claim it cannot attribute", async () => {+ // The THIRD row-learning site. Reconciliation and the Reject-409 resync are driven+ // elsewhere; this one is hydration, and removing its recordClaimState call left the whole+ // file green before this test existed.+ historyResponse.data.proposed_workflow = null;+ historyResponse.data.proposed_workflow_metadata = null;+ historyResponse.data.proposed_claim_expires_in_seconds = 100;+ await renderChat();+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,);await waitFor(() =>expect(- screen.queryByRole("button", { name: /Auto-accepting/ }),- ).toBeNull(),+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled"),+ ).toBe(true),);});- it("ignores a read-back that started before Turn off finished", async () => {- historyResponse.data.auto_accept = true;+ it("REVIEW PROBE: holds Save after an ordinary no-claim poll then a smaller later claim", async () => {await renderChat();- await submit("add a step");+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");++ await submit("build me a workflow");await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));await act(async () => {- streamCalls[0]!.onMessage(proposalResponse("Untested draft."));+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));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(),- );+ // The production lease is minutes, not the sub-10-second value that makes+ // every re-check terminal. Fake timers also fake Date, which this probe+ // asserts below before trusting comparisons that use absolute instants.+ vi.useFakeTimers();+ try {+ const openedAt = Date.now();+ historyResponse.data.proposed_workflow = null;+ historyResponse.data.proposed_workflow_metadata = null;+ historyResponse.data.proposed_claim_expires_in_seconds = 180;+ cancelPost.mockRejectedValueOnce(new Error("Network Error"));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });+ expect(saveHeld()).toBe(true);++ // The first timer pass is ordinary (170 seconds remain), and finds the+ // original claim gone. Its 10 seconds must advance Date.now too.+ historyResponse.data.proposed_claim_expires_in_seconds = null;+ await act(async () => {+ await vi.advanceTimersByTimeAsync(10_000);+ });+ expect(Date.now()).toBe(openedAt + 10_000);+ expect(saveHeld()).toBe(true);++ // A later writer now owns a shorter, still-live lease. The ordinary pass+ // re-arms a deadline from that claim, but must preserve safety after the+ // previous no-claim answer invalidated the opening claim's identity.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "accepting",+ workflow_run_id: null,+ };+ historyResponse.data.proposed_claim_expires_in_seconds = 100;+ await act(async () => {+ await vi.advanceTimersByTimeAsync(10_000);+ });+ expect(saveHeld()).toBe(true);++ // Let ordinary polls see no claim until the final 10-second interval;+ // they keep the deadline established by the later writer's lease.+ historyResponse.data.proposed_workflow = null;+ historyResponse.data.proposed_workflow_metadata = null;+ historyResponse.data.proposed_claim_expires_in_seconds = null;+ for (let poll = 0; poll < 9; poll += 1) {+ await act(async () => {+ await vi.advanceTimersByTimeAsync(10_000);+ });+ }+ expect(saveHeld()).toBe(true);++ // The terminal read finds the same later live claim, now shorter than+ // the opening 180-second remainder. Save must remain held.+ historyResponse.data.proposed_workflow = proposedWorkflowPayload();+ historyResponse.data.proposed_workflow_metadata = {+ owner_turn_id: "turn-1",+ revision: 1,+ canonical_fingerprint: "canonical-1",+ disposition: "accepting",+ workflow_run_id: null,+ };+ historyResponse.data.proposed_claim_expires_in_seconds = 90;+ await act(async () => {+ await vi.advanceTimersByTimeAsync(10_000);+ await Promise.resolve();+ await Promise.resolve();+ await Promise.resolve();+ });+ expect(historyGet).toHaveBeenCalledTimes(14);+ expect(screen.getByText("Couldn't reload")).toBeTruthy();+ expect(saveHeld()).toBe(true);+ } finally {+ vi.useRealTimers();+ }+ }, 15_000);++ // CALLBACK-LEVEL, deliberately. These drive onSelectChat directly, which is where the guard+ // resolves. Whether today's DOM lets a user reach the overlapping window is NOT established -+ // WorkflowCopilotHistory intends to disable and close once loadChatInPlace sets+ // isLoadingHistory, and the docked store is updated by a passive effect, so the window is+ // bounded by that effect. These assert the guard's resolution, not a user-reachable path.+ const selectChat = async (id: string) => {await act(async () => {- finishStaleRead({ data: { ...historyResponse.data, auto_accept: true } });+ await useCopilotHeaderStore.getState().controls?.onSelectChat?.({+ workflow_copilot_chat_id: id,+ } as Parameters<+ NonNullable<+ NonNullable<+ ReturnType<typeof useCopilotHeaderStore.getState>["controls"]+ >["onSelectChat"]+ >+ >[0]);});+ };- expect(screen.queryByRole("button", { name: /Auto-accepting/ })).toBeNull();+ const chatPayload = (id: string, text: string) => ({+ data: {+ ...historyResponse.data,+ workflow_copilot_chat_id: id,+ proposed_workflow: null,+ chat_history: [+ {+ sender: "ai",+ content: text,+ created_at: new Date().toISOString(),+ },+ ],+ },});- it("shows the Untested pill on hydration of an old payload lacking proposalDisposition", async () => {- historyResponse.data.proposed_workflow = proposedWorkflowPayload({- _copilot_unvalidated: true,- });- historyResponse.data.chat_history = [- {- sender: "ai",- content: "Here is a draft.",- created_at: "2026-07-09T00:00:05Z",- narrative_payload: null,- turn_outcome: null,- },- ];- await renderChat();+ it("lets the NEWER chat selection win when two loads overlap", async () => {+ await renderChat({ docked: true });+ let releaseSecond: ((value: unknown) => void) | null = null;+ let releaseThird: ((value: unknown) => void) | null = null;+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ releaseSecond = resolve as (value: unknown) => void;+ }),+ );+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ releaseThird = resolve as (value: unknown) => void;+ }),+ );- await waitFor(() => expect(screen.getByText("Untested")).toBeTruthy());- expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();- });+ await selectChat("chat-2");+ await selectChat("chat-3");+ expect(releaseSecond).not.toBeNull();+ expect(releaseThird).not.toBeNull();- it("renders a legacy (no-narrative) pending turn as pill + footer, no changes body", async () => {- historyResponse.data.proposed_workflow = proposedWorkflowPayload({- _copilot_unvalidated: true,+ // The older load returns FIRST, then the newer one.+ await act(async () => {+ releaseSecond!(chatPayload("chat-2", "SECOND chat body"));+ await Promise.resolve();+ });+ await act(async () => {+ releaseThird!(chatPayload("chat-3", "THIRD chat body"));+ await Promise.resolve();});- historyResponse.data.chat_history = [- {- sender: "ai",- content: "Here is a draft.",- created_at: "2026-07-09T00:00:05Z",- narrative_payload: null,- turn_outcome: null,- },- ];- await renderChat();+ // The user's most recent selection is what they get.await waitFor(() =>- expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy(),+ expect(screen.queryByText("THIRD chat body")).not.toBeNull(),);- expect(screen.getByText("Proposed changes")).toBeTruthy();- expect(screen.queryByText("Added")).toBeNull();- 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.queryByRole("button", { name: /1 proposal pending/ }),- ).toBeNull();+ expect(screen.queryByText("SECOND chat body")).toBeNull();});- it("clears a stale pending gate when a later turn is auto-applied (stale Accept can't reapply over the newer canvas)", async () => {- await renderChat();+ it("lets a re-selection of the CURRENT chat cancel a pending load", async () => {+ // The equality early return makes no second read, so an increment placed after it would+ // never run for this case - and this is the case where a user undoes a mis-click.+ await renderChat({ docked: true });+ let releaseOther: ((value: unknown) => void) | null = null;+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ releaseOther = resolve as (value: unknown) => void;+ }),+ );++ await selectChat("chat-2");+ expect(releaseOther).not.toBeNull();+ await selectChat("chat-1");++ await act(async () => {+ releaseOther!(chatPayload("chat-2", "OTHER chat body"));+ await Promise.resolve();+ });+ expect(screen.queryByText("OTHER chat body")).toBeNull();+ });+ it("attaches the auto-commit saved gate to the committing turn, not the bypassed one", async () => {+ // From reviewer 1677. POSITIVE assertion on purpose: "the status is not inside turn 1's gate"+ // passes when the element is absent, when the render changed shape, and when turn 2 never+ // rendered a gate at all - an earlier attempt of mine did exactly that and pinned nothing.+ // Walking up from the status node and naming the host gate says what IS true.+ //+ // PARTIAL PIN, and the limit is worth knowing: it fails when the turn-id handle survives, and+ // it also passes when only `setProposedWorkflow(null)` is dropped. It pins the gate's+ // ATTACHMENT, not the staged draft handle.+ const onWorkflowUpdate = vi.fn((workflow: { workflow_id?: string }) => {+ if (workflow?.workflow_id === "wf_turn2")+ throw new Error("editor could not load it");+ });+ await renderChat({+ onWorkflowUpdate:+ onWorkflowUpdate as unknown as ChatProps["onWorkflowUpdate"],+ });await submit("build me a workflow");await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));await act(async () => {streamCalls[0]!.onMessage(proposalResponse("Draft ready."));streamCalls[0]!.resolve();});- expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy();-- // Always-accept echo: this follow-up comes back auto-applied instead of another gate.- await submit("actually just fix the typo directly");+ await submit("now change it again");await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));- expect(streamCalls[1]!.body.keep_pending_proposal).toBe(true);-await act(async () => {streamCalls[1]!.onMessage(- proposalResponse("Fixed and applied.", {+ proposalResponse("Committed.", {+ turn_id: "turn-2",proposal_disposition: "auto_applicable",workflow_applied: true,- turn_id: "turn-2",+ updated_workflow: proposedWorkflowPayload({+ workflow_id: "wf_turn2",+ }) as unknown as WorkflowApiResponse,narrative_payload: proposalNarrativePayload({turnId: "turn-2",turnIndex: 1,proposalDisposition: "auto_applicable",- terminalMessage: "Fixed and applied.",- narrativeSummary: "Fixed and applied.",}),}),);streamCalls[1]!.resolve();});-- await waitFor(() =>- expect(screen.queryByRole("button", { name: "Accept" })).toBeNull(),+ const status = await screen.findByText("Saved, not shown");+ expect(status.closest('[id^="copilot-gate-"]')?.id).toBe(+ "copilot-gate-turn-2",);- 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 () => {- await renderChat();+ it("holds Save when the server committed a version the editor cannot take", async () => {+ // The auto-commit path ignored applyWorkflowUpdate's boolean: with `workflow_applied: true`+ // the server HAS written this version, so an editor that rejects it leaves the canvas older+ // than canonical - and clearing the proposal handles there released Save over it with+ // nothing on screen saying so. That is the state `saved` exists for, reached by auto-commit+ // instead of Accept, so it arms the same gate.+ const onWorkflowUpdate = vi.fn(() => {+ throw new Error("editor could not load it");+ });+ await renderChat({ onWorkflowUpdate });+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");- await submit("rename the blocks.");+ await submit("build me a workflow");await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));await act(async () => {- streamCalls[0]!.onMessage(legacyProposalResponse("Renamed the blocks."));+ streamCalls[0]!.onMessage(+ proposalResponse("Committed.", {+ proposal_disposition: "auto_applicable",+ workflow_applied: true,+ }),+ );streamCalls[0]!.resolve();});- await waitFor(() =>- expect(screen.getByRole("button", { name: "Accept" })).toBeTruthy(),- );- expect(screen.getByRole("button", { name: "Reject" })).toBeTruthy();- expect(screen.getByText("Proposed changes")).toBeTruthy();- // The gate itself still appears live (SKY-14099); with no turn behind it there are- // no facts to project, so it carries no verdict pill either way.- expect(screen.queryByText("Tested")).toBeNull();+ expect(await screen.findByText("Saved, not shown")).toBeTruthy();+ expect(saveHeld()).toBe(true);});- it("clears the proposal and shows a discarded receipt on a late Reject", async () => {- await renderChat();+ it("keeps the chat's controls locked while a newer load is still in flight", async () => {+ // The selection epoch stops a SUPERSEDED response before it applies - but its `finally`+ // cleared the loading flag anyway, and that flag gates History, New chat and the gate's+ // action row. So the abandoned load unlocked the chat while the newer one was still running,+ // and an Accept started in that window races a response that will swap the chat underneath+ // it. The same shape was in the mount loader's `finally` too; both now release through+ // endHistoryLoad, which ignores a load that no longer owns the flag.+ //+ // Callback level, like `lets the NEWER chat selection win when two loads overlap` and `lets a+ // re-selection of the CURRENT chat cancel a pending load`: this asserts the guard's+ // resolution. DOM reachability of the overlapping window is bounded by the passive effect+ // that publishes these controls and is NOT established.+ await renderChat({ docked: true });+ let releaseOlder: ((value: unknown) => void) | null = null;+ let releaseNewer: ((value: unknown) => void) | null = null;+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ releaseOlder = resolve as (value: unknown) => void;+ }),+ );+ historyGet.mockImplementationOnce(+ () =>+ new Promise((resolve) => {+ releaseNewer = resolve as (value: unknown) => void;+ }),+ );- await submit("build me a workflow");- await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await selectChat("chat-2");+ await selectChat("chat-3");+ expect(releaseOlder).not.toBeNull();+ expect(releaseNewer).not.toBeNull();++ // The superseded load answers first and must not unlock anything.await act(async () => {- streamCalls[0]!.onMessage(proposalResponse("Draft ready."));- streamCalls[0]!.resolve();+ releaseOlder!(chatPayload("chat-2", "SECOND chat body"));+ await Promise.resolve();});- await waitFor(() =>- expect(screen.getByRole("button", { name: "Reject" })).toBeTruthy(),- );+ expect(useCopilotHeaderStore.getState().controls?.disabled).toBe(true);+ // The load that still owns the flag releases it, so this does not latch.await act(async () => {- fireEvent.click(screen.getByRole("button", { name: "Reject" }));+ releaseNewer!(chatPayload("chat-3", "THIRD chat body"));+ await Promise.resolve();});-await waitFor(() =>- expect(cancelPost).toHaveBeenCalledWith(- "/workflow/copilot/clear-proposed-workflow",- expect.objectContaining({ workflow_copilot_chat_id: "chat-1" }),- ),+ expect(useCopilotHeaderStore.getState().controls?.disabled).toBe(false),);- expect(- screen.getByText("Discarded — canvas reverted to the previous version"),- ).toBeTruthy();- expect(screen.queryByRole("button", { name: "Reject" })).toBeNull();});- it("surfaces per-block coverage tags through the mounted chat, not only in an isolated card", async () => {- await renderChat();-- await submit("edit one block after it passed.");+ it("keeps the workflow-claim hold when ANOTHER chat's row reports no claim", async () => {+ // A chat row is CHAT-SCOPED evidence for a WORKFLOW-SCOPED fact: the server reads the claim+ // off THAT chat's own proposal blob, so a second chat reports none while the first writer's+ // claim is still live. Retiring the hold on that answer releases Save under an active write,+ // which is the hold's entire purpose - existence needs one witness, absence needs coverage+ // no row has. Only the lease timer may end it.+ await renderChat({ docked: true });+ render(+ <TooltipProvider>+ <SaveButton />+ </TooltipProvider>,+ );+ const saveHeld = () =>+ screen+ .getByRole("button", { name: /^Save workflow/ })+ .matches(":disabled");+ await submit("build me a workflow");await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));await act(async () => {- streamCalls[0]!.onMessage(- proposalResponse("Draft needs review.", {- narrative_payload: {- ...editOneOfTwoBundle,- turnId: "turn-1",- },- }),- );+ streamCalls[0]!.onMessage(proposalResponse("Draft ready."));streamCalls[0]!.resolve();});+ // Another writer holds this workflow and the server cannot tie the claim to a proposal.+ historyResponse.data.proposed_workflow = null;+ historyResponse.data.proposed_workflow_metadata = null;+ historyResponse.data.proposed_claim_expires_in_seconds = 100;+ cancelPost.mockRejectedValueOnce({ response: { status: 409 } });+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Reject" }));+ });+ await waitFor(() => expect(saveHeld()).toBe(true));++ // History stays enabled under this hold by design, so the switch is reachable. Chat B has no+ // proposal of its own, so the server reports no claim FOR CHAT B - not for the workflow.+ historyGet.mockImplementationOnce(() =>+ Promise.resolve({+ data: {+ ...historyResponse.data,+ workflow_copilot_chat_id: "chat-2",+ proposed_workflow: null,+ proposed_workflow_metadata: null,+ proposed_claim_expires_in_seconds: null,+ chat_history: [+ {+ sender: "ai",+ content: "SECOND chat body",+ created_at: new Date().toISOString(),+ },+ ],+ },+ }),+ );+ await selectChat("chat-2");await waitFor(() =>- expect(screen.getByText("Different source")).toBeTruthy(),+ expect(screen.queryByText("SECOND chat body")).not.toBeNull(),);- expect(screen.queryByRole("button", { name: "Collapse turn" })).toBeNull();- expect(- screen.queryByRole("button", { name: /Draft needs review/ }),- ).toBeNull();- expect(screen.queryByText("Built and tested the workflow")).toBeNull();+ expect(saveHeld()).toBe(true);});- it("keeps the newest structured response in its expanded reading state", async () => {+ it("keeps the History lock reason reachable on the floating header", async () => {+ // The floating header is outside any TooltipProvider, so the reason is a native title -+ // and a DISABLED button receives no hover and takes no focus, so a title ON IT can never+ // be read. It has to hang off something that is not disabled.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();});+ // An Accept that never settles holds the fence, which is what locks History.+ cancelPost.mockImplementationOnce(() => new Promise(() => {}));+ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: "Accept" }));+ });- await waitFor(() =>- expect(- screen.getByText("Here is a draft workflow for you to review."),- ).toBeTruthy(),- );- expect(screen.queryByRole("button", { name: "Collapse turn" })).toBeNull();- expect(- screen.queryByRole("button", { name: /Draft needs review/ }),- ).toBeNull();+ const history = screen.getByRole("button", { name: /^History/ });+ expect(history.matches(":disabled")).toBe(true);+ const explained = screen+ .getAllByTitle("Copilot is saving your accepted changes.")+ .find((el) => el.contains(history))!;+ expect(explained).toBeTruthy();+ // Whatever carries the reason must be able to receive a hover: not the disabled control.+ expect(explained.matches(":disabled")).toBe(false);+ expect(explained.contains(history)).toBe(true);});});diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsxindex e737a9263..5da1dc93b 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx@@ -99,6 +99,7 @@ import {type RecordingRefinementStatus,} from "./RecordingRefinementProgressCard";import { useRunLifecycleAnnouncements } from "./useRunLifecycleAnnouncements";+import { useHistoryLoad } from "./useHistoryLoad";import { ConfirmCard, shouldShowConfirmCard } from "./cards/ConfirmCard";import { ConnectedAccountChoiceCard } from "./cards/ConnectedAccountChoiceCard";import { QuestionPartsCard } from "./cards/QuestionPartsCard";@@ -640,6 +641,32 @@ const getLatestDiffCardTurnId = (messages: ChatMessage[]): string | null => {return null;};+import {+ type AcceptAttempt,+ claimHoldDeadline,+ fenceBaselineFor,+ type GateFailure,+ HOLD_RECHECK_MS,+ hydratedGateFailure,+ proposalTokenOf,+ extendedClaimHold,+ unattributedClaimDeadline,+ UNREPORTED_CLAIM_LEASE_MS,+} from "./acceptFence";++const ACCEPT_IN_FLIGHT_SAVE_REASON = "Copilot is saving your accepted changes.";++const ACCEPT_SAVED_NOT_SHOWN_SAVE_REASON =+ "Copilot saved this change but the editor couldn't load it, so saving now would write an older workflow over it.";++const ACCEPT_UNCONFIRMED_SAVE_REASON =+ "Copilot couldn't confirm whether an Accept already saved. Use Try again on the review gate first.";++const WORKFLOW_CLAIMED_SAVE_REASON =+ "Another change to this workflow is being saved right now. Reload the page before saving, so you don't overwrite it \u2014 reloading discards unsaved canvas edits.";++const ACCEPT_STALE_CANVAS_SAVE_REASON =+ "Copilot couldn't confirm an earlier Accept, so this canvas may be out of date. Saving it could overwrite newer changes \u2014 reload the page first, which discards unsaved canvas edits.";const getLatestDiffCardTurnIdFromHistory = (messages: WorkflowCopilotChatHistoryMessage[],): string | null => {@@ -915,6 +942,11 @@ export type WorkflowUpdateOptions = {// applied only if nothing has named it yet. Discrete applies (accept, snap-back)// are authoritative and keep the force path.midTurnDraft?: boolean;+ // This workflow was read from the server IN THE SAME ACT as the apply, so the canvas it+ // installs is current. `persisted` does NOT imply this: it also marks a workflow held from an+ // earlier read and applied later, which is current only if nothing has written since. Opt-in+ // rather than opt-out, so a caller that cannot make the claim makes it by saying nothing.+ fresh?: boolean;};interface WorkflowCopilotChatProps {@@ -1101,6 +1133,109 @@ export function WorkflowCopilotChat({const [pendingProposalTurnId, setPendingProposalTurnId] = useState<string | null>(null);+ // What the pending gate reports after a failed Accept or proposal reload;+ // its Retry re-runs that call.+ const [gateFailure, setGateFailure] = useState<GateFailure | null>(null);+ const [isAccepting, setIsAccepting] = useState(false);+ // Unknown server state plus a write path: hold workflow saves and chat navigation from+ // the moment Accept is sent until its outcome is confirmed. A chat switch cannot release the+ // hold. It ends on the outcome resolving, on the lease lapsing, on unmount - or on Try again,+ // which DOES clear the gate when the re-read finds the proposal still there. What none of+ // those cover is a request that never settles: every one of this component's in-flight flags+ // is lowered only inside a `finally`, so a hung request lowers none of them. Bounding those+ // requests is ticketed.+ const recoverHold = gateFailure?.kind === "recover" ? gateFailure : null;+ // Whether the canvas may be out of date is a fact about the CANVAS, not about the gate card,+ // so it has to outlive the card: sending and New chat clear the card deliberately and neither+ // reconciles the graph. Only the editor taking a workflow from the server does - that is what+ // `persisted` marks - or a page reload, since this is not stored anywhere.+ const [staleCanvas, setStaleCanvas] = useState(false);+ // A live claim the server could not tie to a proposal: someone else is writing this workflow,+ // which is a fact about the WORKFLOW rather than about our Accept. It holds Save only - locking+ // sending or navigation would hold a user out of their own chat because another tab is busy.+ const [unattributedClaimUntil, setUnattributedClaimUntil] = useState<+ number | null+ >(null);+ // What a chat row says about a claim THIS CHAT CANNOT ACCOUNT FOR, recorded in one place.+ // Whether another writer holds this workflow is a fact about the WORKFLOW, true regardless of+ // which read learned it, so every reader of a row calls this and they agree by construction+ // rather than by three call sites remembering.+ //+ // The remainder used to compare claims does NOT live here: that one belongs to a single fence+ // and rides on the `recover` gate, which is the fence.+ const recordClaimState = useCallback(+ (+ row: Pick<+ WorkflowCopilotChatHistoryResponse,+ "proposed_claim_expires_in_seconds" | "proposed_workflow_metadata"+ >,+ ): void => {+ // Arm from any row, extend from any row, retire from none - the rule and its reasoning+ // live with the fence's other pure rules. The lease timer below is the sole releaser, at+ // the cost of holding Save to the deadline when the other writer finishes early.+ setUnattributedClaimUntil((current) =>+ extendedClaimHold(current, unattributedClaimDeadline(row)),+ );+ },+ // Only a stable setter, so this never needs to change.+ [],+ );+ // Every send stages a proposal the unresolved Accept could clear, and handleSend drops the+ // gate failure, which would release this hold with the write still unaccounted for.+ const acceptHoldReason = isAccepting+ ? ACCEPT_IN_FLIGHT_SAVE_REASON+ : gateFailure?.kind === "saved"+ ? ACCEPT_SAVED_NOT_SHOWN_SAVE_REASON+ : recoverHold+ ? ACCEPT_UNCONFIRMED_SAVE_REASON+ : null;+ // Save is held wider than the rest of the fence. Once recovery gives up, the gate tells the+ // user the canvas may be out of date and to reload; leaving Save live under that message+ // puts two controls on one card giving opposite advice about the same fact - reloading is+ // safe because the canvas comes back from canonical, and saving is destructive for exactly+ // that reason. Sending and navigation stay open, because neither writes the workflow and+ // moving on is the user's call - which is also why this reads `staleCanvas` rather than the+ // gate kind: both of those clear the card, and neither makes the canvas current. It ends+ // when the editor takes a workflow from the server, or on a page reload, which cannot re-arm+ // it - hydration only ever arms `recover`, and only while the server still reports a claim.+ // `reload` renders a card only when it has a subject, and that card tells the user to reload+ // because the canvas may be stale. Save is held for as long as it is on screen, so the hold ends+ // with the card rather than outliving it: a failed row read on its own says nothing about+ // canonical, and holding on one strands the user behind a message that never rendered.+ const reloadCardShowing =+ gateFailure?.kind === "reload" && Boolean(proposedWorkflow);+ const saveHoldReason =+ acceptHoldReason ??+ (staleCanvas || reloadCardShowing+ ? ACCEPT_STALE_CANVAS_SAVE_REASON+ : null) ??+ (unattributedClaimUntil !== null ? WORKFLOW_CLAIMED_SAVE_REASON : null);+ // The lock always ends. Hydration is the only thing that sets this, and there may never be+ // another one, so the hold expires on the lease the server reported rather than waiting for a+ // read that never comes.+ useEffect(() => {+ if (unattributedClaimUntil === null) {+ return;+ }+ const timer = setTimeout(+ () => setUnattributedClaimUntil(null),+ Math.max(0, unattributedClaimUntil - Date.now()),+ );+ return () => clearTimeout(timer);+ }, [unattributedClaimUntil]);+ useEffect(() => {+ if (!saveHoldReason) {+ return;+ }+ const { setSaveBlockedReason } = useWorkflowHasChangesStore.getState();+ setSaveBlockedReason(saveHoldReason);+ return () => setSaveBlockedReason(null);+ }, [saveHoldReason]);++ const acceptUnresolved = acceptHoldReason !== null;+ // Bumped by every send, so a plain proposal reload started before a newer turn+ // cannot apply its older row over that turn's proposal.+ const sendEpochRef = useRef(0);// Transient ring highlight on the gate the pending-proposal chip just// scrolled to; cleared after the flash window.const [gateFlashTurnId, setGateFlashTurnId] = useState<string | null>(null);@@ -1205,7 +1340,8 @@ export function WorkflowCopilotChat({},[],);- const [isLoadingHistory, setIsLoadingHistory] = useState(false);+ const { isLoadingHistory, beginHistoryLoad, endHistoryLoad } =+ useHistoryLoad();// Active mid-build credential pause frame for the in-flight turn. Cleared at// turn_start and at every terminal so a dead resume_token can never render.const [livePauseFrame, setLivePauseFrame] =@@ -1363,6 +1499,23 @@ export function WorkflowCopilotChat({useEffect(() => {workflowCopilotChatIdRef.current = workflowCopilotChatId;}, [workflowCopilotChatId]);+ // workflowCopilotChatIdRef is assigned by the effect above, so it names the OLD chat until a+ // commit after navigation starts. Anything comparing it can therefore pass in that window.+ // This counter moves the instant a user navigates, the way sendEpochRef does for sends, so an+ // in-flight write can tell that the ground moved without waiting for the ref to catch up.+ const chatNavEpochRef = useRef(0);+ // A SEPARATE counter, because this answers a DIFFERENT QUESTION. chatNavEpochRef answers "did+ // we actually navigate", and it moves when a hydration APPLIES a different chat - so a+ // navigation that FAILS correctly leaves it alone and an in-flight Reject still completes.+ // This one answers "is there a newer user selection", which must move the instant the user+ // selects, whether or not that load ever succeeds. One counter cannot answer both: advancing+ // the navigation counter on selection would abandon a Reject over a navigation that never+ // happened.+ const chatSelectionEpochRef = useRef(0);+ // The chat id the last hydration actually applied. Maintained here rather than read back from+ // workflowCopilotChatIdRef, which lags by a commit - comparing against a lagging value reports+ // a change every time hydration runs twice before the effect catches up.+ const hydratedChatIdRef = useRef<string | null>(null);useEffect(() => {const activePolls = actionPollRef.current;const activeRecoveryPolls = recoveryPolls.current;@@ -1715,8 +1868,21 @@ export function WorkflowCopilotChat({name: string | undefined,canContinue: boolean,) => {+ const couldContinue = canContinue && Boolean(name);+ // ONLY the Accept fence. A turn in flight also blocks the continuation, but that case+ // records its receipt deliberately and is covered by its own test; widening this to cover+ // it would change behaviour nobody asked to change.+ const blockedForNow = couldContinue && acceptUnresolved;const shouldContinue =- canContinue && !isLoading && !isLoadingHistory && Boolean(name);+ couldContinue && !isLoading && !isLoadingHistory && !acceptUnresolved;+ if (blockedForNow) {+ // Recording a resolution here would swap the picker for a "Credential added" ✓ on a turn+ // that never continued, and nothing re-attempts once the block lifts. Leaving it+ // unrecorded keeps the card in ask mode so the user can choose again. Note the distinction+ // from `!couldContinue`: there the receipt is correct, because there is nothing to+ // continue and the credential really was added.+ return;+ }resolveTerminalCredential(turnId,"connected",@@ -1740,7 +1906,7 @@ export function WorkflowCopilotChat({);}},- [isLoading, isLoadingHistory, resolveTerminalCredential],+ [acceptUnresolved, isLoading, isLoadingHistory, resolveTerminalCredential],);// Auto-continue send failed: undo the optimistic "connected" resolution and// mark the ask stranded so its card renders actionable again for a retry.@@ -1800,6 +1966,14 @@ export function WorkflowCopilotChat({);const textareaRef = useRef<HTMLTextAreaElement | null>(null);const { getSaveData } = useWorkflowHasChangesStore();+ // The editor already tracks this. It is NOT user-authorship: Workspace sets it on any apply+ // without `persisted`, the copilot's own mid-turn draft included, and beginInternalUpdate does+ // not gate that branch - so the card says "the canvas has unsaved changes", never "your edits".+ // What it does attest is that something unsaved is on the canvas, which is what Try again+ // discards - the same fact reconcileCanonicalWorkflow already refuses to overwrite on.+ const canvasHasEdits = useWorkflowHasChangesStore(+ (state) => state.hasChanges,+ );const hasInitializedPosition = useRef(false);const hasAutoSentRef = useRef(false);const isWaitingForLiveBrowser = shouldWaitForLiveBrowser({@@ -1923,6 +2097,8 @@ export function WorkflowCopilotChat({}, [returnFilesToTray, updateQueuedPrompt]);const handleNewChat = () => {+ chatNavEpochRef.current += 1;+ setGateFailure(null);streamingAbortController.current?.abort();streamingAbortController.current = null;inFlightRef.current = false;@@ -2116,6 +2292,20 @@ export function WorkflowCopilotChat({}return nextMessages;});+ // Bumped here rather than when navigation starts, so a history load that FAILS does not+ // strand an in-flight write: the user is still in the old chat, and a completion clearing+ // that chat's state is correct. Synchronous and in the same block as the state write, so+ // it lands a commit before the effect that syncs workflowCopilotChatIdRef. Compared against+ // what hydration last applied, never against that lagging ref, and only once a chat has+ // been applied - a first hydration is not navigation, and New chat bumps for itself.+ const previouslyHydratedChatId = hydratedChatIdRef.current;+ hydratedChatIdRef.current = data.workflow_copilot_chat_id;+ if (+ previouslyHydratedChatId !== null &&+ data.workflow_copilot_chat_id !== previouslyHydratedChatId+ ) {+ chatNavEpochRef.current += 1;+ }setWorkflowCopilotChatId(data.workflow_copilot_chat_id);setProposedWorkflow(data.proposed_workflow ?? null);setPendingProposalMetadata(data.proposed_workflow_metadata ?? null);@@ -2123,6 +2313,22 @@ export function WorkflowCopilotChat({setPendingProposalTurnId(data.proposed_workflow ? restoredPendingProposalTurnId : null,);+ // A reload that lands mid-Accept finds the server still holding its claim. The fence has+ // to come back with it, or the gate, Save and sending are all live over a write that is+ // still running — the same hazard as a lost response, reached by remounting instead.+ setGateFailure((current) =>+ hydratedGateFailure(+ data,+ current,+ Date.now() + UNREPORTED_CLAIM_LEASE_MS,+ ),+ );+ // A fence armed HERE can reach the terminal pass on its FIRST recheck: hydration sets the+ // deadline to now + the reported remainder, so a reload that sees ten seconds left+ // schedules a terminal one immediately.+ recordClaimState(data);+ // A read taken before a Turn off landed describes the row before that write, so it may not+ // overwrite it.if (autoAcceptWritesAtRead === undefined ||autoAcceptWritesAtRead === autoAcceptWrites.current@@ -2131,8 +2337,8 @@ export function WorkflowCopilotChat({}setWorkPlan(data.work_plan ?? []);},- // Only stable state setters and refs are referenced, so the callback never needs to change.- [rememberTurnOwnedRun],+ // Only stable state setters, refs and stable callbacks, so this never needs to change.+ [rememberTurnOwnedRun, recordClaimState],);const stopRecoveryPolls = useCallback(() => {@@ -2514,7 +2720,7 @@ export function WorkflowCopilotChat({setIsLoading(false);}stopRecoveryPolls();- setIsLoadingHistory(true);+ const loadSeq = beginHistoryLoad();discardQueuedPrompt();setRejectedTurnIds(new Set());setAcceptedTurnIds(new Set());@@ -2523,6 +2729,13 @@ export function WorkflowCopilotChat({pendingSubmitSnapshot.current = null;latestTurnId.current = null;repin();+ // This function is the one the identity guards exist to defend against, and it did not+ // guard itself: its response was applied unconditionally, so a user who hit New chat while+ // it was loading got the abandoned chat written back over them. Captured before the fetch+ // and checked before the apply - the apply is what moves this counter, so a legitimate+ // load still passes its own check.+ const navEpochAtStart = chatNavEpochRef.current;+ const selectionAtStart = chatSelectionEpochRef.current;try {const client = await getClient(credentialGetter, "sans-api-v1");const response =@@ -2536,6 +2749,12 @@ export function WorkflowCopilotChat({},},);+ if (+ chatNavEpochRef.current !== navEpochAtStart ||+ chatSelectionEpochRef.current !== selectionAtStart+ ) {+ return;+ }applyHistoryResponse(response.data, false);adoptRecoveredTurns(response.data);// Mark history loaded for this workflow so the mount effect won't reload@@ -2545,7 +2764,7 @@ export function WorkflowCopilotChat({console.error("Failed to load chat:", error);toast({ title: "Failed to load chat", variant: "destructive" });} finally {- setIsLoadingHistory(false);+ endHistoryLoad(loadSeq);}},[@@ -2556,6 +2775,8 @@ export function WorkflowCopilotChat({stopRecoveryPolls,repin,discardQueuedPrompt,+ beginHistoryLoad,+ endHistoryLoad,],);@@ -2564,6 +2785,16 @@ export function WorkflowCopilotChat({const handleSelectHistoryChat = useCallback((chat: WorkflowCopilotChatSummary) => {+ // ADVANCED BEFORE THE EQUALITY RETURN BELOW, and only here. A user selection must+ // invalidate loads already in flight the moment it is made, including a RE-SELECTION OF+ // THE CHAT ALREADY SHOWN - that is how a user undoes a mis-click, and it takes the early+ // return, so an increment placed after it would never run for exactly that case and a+ // pending load of the other chat would still win.+ //+ // Only USER selections move this. loadChatInPlace has three programmatic callers, and a+ // background reload advancing this counter would cancel a selection the user is waiting+ // on - a worse failure than the one this prevents.+ chatSelectionEpochRef.current += 1;if (chat.workflow_copilot_chat_id === workflowCopilotChatIdRef.current) {return;}@@ -2576,7 +2807,10 @@ export function WorkflowCopilotChat({// Stable wrappers over refs keep the registration limited to value changes.const headerHandlersRef = useRef({ handleSelectHistoryChat, handleNewChat });headerHandlersRef.current = { handleSelectHistoryChat, handleNewChat };- const headerControlsDisabled = isLoading || isLoadingHistory;+ // An Accept cannot be cancelled once sent, so no other chat may take over the+ // gate until it settles (New chat stays usable during a turn only to abort it).+ const headerControlsDisabled =+ isLoading || isLoadingHistory || acceptHoldReason !== null;useEffect(() => {if (!docked) {return;@@ -2589,6 +2823,8 @@ export function WorkflowCopilotChat({headerHandlersRef.current.handleSelectHistoryChat(chat),onNewChat: () => headerHandlersRef.current.handleNewChat(),disabled: headerControlsDisabled,+ newChatDisabled: acceptHoldReason !== null,+ navigationLockedReason: acceptHoldReason,});return () => store.setControls(null);}, [@@ -2596,6 +2832,7 @@ export function WorkflowCopilotChat({workflowPermanentId,workflowCopilotChatId,headerControlsDisabled,+ acceptHoldReason,]);const applyWorkflowUpdate = useCallback(@@ -2608,14 +2845,20 @@ export function WorkflowCopilotChat({}try {onWorkflowUpdate(workflow, options);+ // THE ONLY PERMITTED CLEAR: the editor now holds a workflow the server gave us JUST NOW.+ // `persisted` alone is not that claim - it is also true of a workflow captured earlier and+ // applied after an arbitrary delay, which asserts a freshness nobody checked. Evidence+ // that some particular Accept did not save is about THAT ACCEPT, not about whether THIS+ // CANVAS was refreshed - a different subject, however conclusive it is about its own.+ if (options?.persisted && options?.fresh) {+ setStaleCanvas(false);+ }return true;} catch (updateError) {+ // No toast here: the one implementation of onWorkflowUpdate (the studio's, in+ // editor/Workspace.tsx) shows "Update failed" itself and then re-throws so this+ // boolean is honest. Toasting again would stack two failure toasts on one event.console.error("Failed to update workflow:", updateError);- toast({- title: "Update failed",- description: "Failed to apply agent changes. Please try again.",- variant: "destructive",- });return false;}},@@ -2626,42 +2869,55 @@ export function WorkflowCopilotChat({// recovered turn never had that frame, so the editor can still hold the graph// from before the drop and a later save would write it back over the commit.// Reading canonical is the same thing the reload used to do.- const reconcileCanonicalWorkflow = useCallback(async () => {+ const applyCanonicalWorkflow = useCallback(async (): Promise<+ "applied" | "unreadable"+ > => {if (!workflowPermanentId) {- return;- }- // Unsaved local edits outrank a stale graph: overwriting them would lose- // work the user can see, which is worse than the staleness being fixed.- if (useWorkflowHasChangesStore.getState().hasChanges) {- return;+ return "unreadable";}try {const client = await getClient(credentialGetter);const response = await client.get<WorkflowApiResponse>(`/workflows/${workflowPermanentId}`,);- applyWorkflowUpdate(response.data, { persisted: true, applied: true });+ return applyWorkflowUpdate(response.data, {+ persisted: true,+ applied: true,+ fresh: true,+ })+ ? "applied"+ : "unreadable";} catch (error) {console.warn("Failed to re-read the workflow after recovery:", error);+ return "unreadable";}}, [applyWorkflowUpdate, credentialGetter, workflowPermanentId]);+ const reconcileCanonicalWorkflow = useCallback(async () => {+ // Unsaved local edits outrank a stale graph: overwriting them would lose+ // work the user can see, which is worse than the staleness being fixed.+ if (useWorkflowHasChangesStore.getState().hasChanges) {+ return;+ }+ await applyCanonicalWorkflow();+ }, [applyCanonicalWorkflow]);reconcileCanonicalWorkflowRef.current = reconcileCanonicalWorkflow;// Records the accepted turn (for the "Applied changes" relabel) before- // clearing the pending-gate handle, shared by all three accept outcomes.- const markProposalAccepted = () => {- if (pendingProposalTurnId) {- setAcceptedTurnIds((prev) => new Set(prev).add(pendingProposalTurnId));+ // clearing the pending-gate handle.+ // Takes an explicit owner when the caller held one: the `saved` gate survives a hydration+ // that clears pendingProposalTurnId, and without its own copy a confirmed save would clear+ // the gate with no turn to mark - a save that landed, reported as nothing.+ const markProposalAccepted = (ownerTurnId?: string | null) => {+ const owner = ownerTurnId ?? pendingProposalTurnId;+ if (owner) {+ setAcceptedTurnIds((prev) => new Set(prev).add(owner));}setPendingProposalTurnId(null);};- const handleAcceptWorkflow = (- workflow: WorkflowApiResponse,- alwaysAccept: boolean = false,- ) => {+ const handleAcceptWorkflow = (alwaysAccept: boolean = false) => {const chatKey = workflowCopilotChatIdRef.current?.trim() ?? "";- const accepting = acceptWorkflow(workflow, alwaysAccept);+ const accepting = acceptWorkflow(alwaysAccept);// Chain rather than replace: a second click must not let Turn off skip the first apply.acceptsInFlight.current.set(chatKey,@@ -2673,10 +2929,10 @@ export function WorkflowCopilotChat({return accepting;};- const acceptWorkflow = async (- workflow: WorkflowApiResponse,- alwaysAccept: boolean,- ) => {+ const acceptWorkflow = async (alwaysAccept: boolean) => {+ setGateFailure(null);+ setIsAccepting(true);+ const proposalToken = proposalTokenOf(pendingProposalMetadata);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@@ -2698,31 +2954,8 @@ export function WorkflowCopilotChat({}if (!chatId) {- if (pendingProposalMetadata) {- toast({- title: "Accept failed",- description:- "Copilot could not verify the current proposal. Please try again.",- variant: "destructive",- });- return;- }- // No chat id: apply locally and best-effort clear the server proposal so reload doesn't resurrect it.- if (!applyWorkflowUpdate(workflow, { applied: true })) {- return;- }- 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);- }+ setIsAccepting(false);+ setGateFailure({ kind: "accept", alwaysAccept, token: proposalToken });return;}@@ -2739,8 +2972,31 @@ export function WorkflowCopilotChat({);// persisted=true loads as clean baseline; without it, Save would create a duplicate version.if (- !applyWorkflowUpdate(response.data, { persisted: true, applied: true })+ !applyWorkflowUpdate(response.data, {+ persisted: true,+ applied: true,+ fresh: true,+ })) {+ // The server answered 200, so this is not ambiguous and must not go through recovery:+ // the accepted workflow is in hand. Hold it and retry THIS apply rather than re-reading+ // the chat row, which would throw away the one answer we have.+ //+ // This arm's other differences from the success path describe what the EDITOR did, so the+ // retry performs them when it succeeds. The counter describes a write only the SERVER can+ // make - the route persists auto_accept best-effort, so a 200 means it MAY have landed and+ // an earlier read cannot be trusted either way. Only the counter moves here: the value+ // still comes from the retry's read-back, never from the attempt.+ noteAutoAcceptWrite();+ setGateFailure({+ kind: "saved",+ savedWorkflow: response.data,+ // Captured now, while the turn is still known: this gate outlives a hydration that+ // clears the pending-turn field, and its retry has to mark the right turn accepted.+ ownerTurnId: pendingProposalTurnId,+ alwaysAccept,+ token: proposalToken,+ });return;}if (!stillOnAcceptedChat()) {@@ -2761,59 +3017,392 @@ export function WorkflowCopilotChat({void resyncProposalFromChatRow();}} catch (applyError) {- if (getErrorStatus(applyError) === 409) {- await resyncProposalFromChatRow();- toast({- title: "Proposal changed",- description:- "Copilot reloaded the current proposal. Review it before applying.",+ console.error("Failed to apply proposed workflow:", applyError);+ const settled = await reconcileFailedAccept(+ { alwaysAccept, token: proposalToken },+ // No fence is open yet: this read opens one, so it may set the baseline.+ { chatId, claimSeen: undefined },+ );+ if (!settled) {+ // UNKNOWN MUST NOT RELEASE: `finally` clears isAccepting below, so without this an+ // unreconciled Accept ends with Save live over an outcome nobody knows.+ //+ // It lives here rather than in reconcileFailedAccept because this caller cleared its own+ // gate and is the only one with no prior gate to fall back on; centralising it would+ // downgrade a definite `accept` on the retry path and delete the timer's guarded replace.+ setGateFailure({+ kind: "recover",+ alwaysAccept,+ token: proposalToken,+ holdExpiresAt: Date.now() + UNREPORTED_CLAIM_LEASE_MS,+ // Nothing was read, so this fence knows no remainder - and must therefore hold on any+ // live claim its terminal pass finds.+ claimExpiresAtSeen: null,});- return;}- if (pendingProposalMetadata) {- await resyncProposalFromChatRow();- toast({- title: "Accept failed",- description:- "Copilot kept the current proposal for review. Please try again.",- variant: "destructive",- });- return;+ } finally {+ setIsAccepting(false);+ }+ };++ // Try again may only re-send the proposal that failed. A reload that swapped+ // in a different one, or a legacy proposal with no token to prove it did not,+ // needs the user to review what is shown and Accept it fresh.+ const gateFailureKind =+ gateFailure?.kind === "accept" &&+ gateFailure.token !== proposalTokenOf(pendingProposalMetadata)+ ? "changed"+ : (gateFailure?.kind ?? null);+ const gateFailureRetryable =+ gateFailureKind === "reload" ||+ gateFailureKind === "recover" ||+ gateFailureKind === "saved" ||+ (gateFailureKind === "accept" &&+ gateFailure?.kind === "accept" &&+ gateFailure.token !== null);++ // A rejected Accept may still have saved, so the chat row decides: a pending+ // proposal means nothing was saved; a vanished one keeps the gate until the+ // canonical workflow is the clean baseline, or a later Save would duplicate it.+ // Returns whether it reached a definite state. The caller cannot infer that from component+ // state: it runs before React has re-rendered, so every ref it could read still describes the+ // world as it was before this call.+ // Two reconciliations can be in flight at once - the scheduled re-check and a manual Try again,+ // which stays enabled while that read is out. Without this, arrival order alone decided the gate:+ // a stale row landing after a newer read had seen a live claim could report "nothing was saved"+ // and release Save mid-write. A superseded call reports SETTLED so the terminal fallback in its+ // caller does not fire either; its `finally` clears the latch, and re-arms when the gate it was+ // serving has since been replaced.+ const reconcileGeneration = useRef(0);+ const resyncGeneration = useRef(0);++ const reconcileFailedAccept = async (+ attempt: AcceptAttempt,+ {+ lastChance = false,+ holdUntil,+ chatId,+ claimSeen,+ }: {+ lastChance?: boolean;+ holdUntil?: number;+ chatId?: string | null;+ // REQUIRED, so a caller cannot silently default it. Absent means no fence is open and this+ // read opens one; null means a fence opened blind and stays blind. A re-check that forgot+ // to hand its fence's instant back would let the read it triggers become the baseline -+ // which was one of the three defects here - so omitting this is a compile error and+ // callers with no fence pass `undefined` deliberately.+ claimSeen: number | null | undefined;+ },+ ): Promise<boolean> => {+ // One fence for the whole unresolved Accept: a re-check keeps the deadline it inherited+ // rather than starting a fresh one, so re-checking can never extend the hold.+ const fenceUntil = holdUntil ?? Date.now() + UNREPORTED_CLAIM_LEASE_MS;+ // Out of attempts, the gate stops waiting on the server and hands the exit to the user:+ // an unreadable workflow is a stale proposal, not an outcome more re-reads will settle.+ // It keeps holding Save, because a proposal we could not re-read may be out of date.+ // The claim's ABSOLUTE EXPIRY as this pass read it, carried onto whatever fence it arms.+ // Absolute rather than relative so the comparison cannot depend on WHEN either read ran: a+ // throttled tab resuming late computes the same instant. Null when the read failed or found+ // no claim, and a fence that saw nothing must hold on a live claim later.+ let seenNow: number | null = null;+ // Fixed at the fence's opening. A re-check inherits it untouched, so no later read can make+ // an open fence better informed than it was when it opened.+ let fenceBaseline: number | null = claimSeen ?? null;+ const unreadable = (): GateFailure => {+ if (!lastChance) {+ return {+ kind: "recover",+ ...attempt,+ holdExpiresAt: fenceUntil,+ claimExpiresAtSeen: fenceBaseline,+ };}- // Atomic accept can fail if the server-side proposal is missing- // _copilot_yaml (SKY-9310 — V1 path didn't stash it). Fall back to the- // pre-#10568 client-side apply so users aren't blocked while a backend- // deploy catches up. Logged so we can still spot regressions.- console.error(- "Atomic apply failed; falling back to client-side apply:",- applyError,- );- if (!applyWorkflowUpdate(workflow, { applied: true })) {- toast({- title: "Accept failed",- description: "Could not apply the proposed agent. Please try again.",- variant: "destructive",- });- return;+ setStaleCanvas(true);+ return { kind: "reload", attempt };+ };+ let row: WorkflowCopilotChatHistoryResponse | null;+ const autoAcceptWritesAtRead = autoAcceptWrites.current;+ const generation = (reconcileGeneration.current += 1);+ const superseded = () => reconcileGeneration.current !== generation;+ try {+ row = await fetchChatRow(chatId);+ } catch (error) {+ if (superseded()) {+ return true;}- if (stillOnAcceptedChat()) {- markProposalAccepted();+ console.error("Failed to resync pending proposal:", error);+ setGateFailure(unreadable());+ return true;+ }+ if (superseded()) {+ return true;+ }+ if (!row) {+ return false;+ }+ // An unreported claim inherits this fence's deadline rather than opening a new one, so+ // re-reading and retrying cannot extend the hold past the lease it was opened for.+ const holdExpiresAt = claimHoldDeadline(row, fenceUntil);+ // A LEASE'S REMAINING TIME NEVER INCREASES, so a row reporting MORE than the last read is+ // not our claim decaying - it is a NEW claim, taken by another writer after ours lapsed.+ // Read before the arming branch below, which returns early: recorded after it, the remainder+ // would never be captured on the read that OPENS the fence, leaving the check below inert.+ const reportedRemaining = row.proposed_claim_expires_in_seconds;+ seenNow = holdExpiresAt;+ fenceBaseline = fenceBaselineFor(claimSeen, seenNow);+ recordClaimState(row);+ if (!lastChance && holdExpiresAt !== null && holdExpiresAt > Date.now()) {+ setGateFailure({+ kind: "recover",+ ...attempt,+ holdExpiresAt,+ claimExpiresAtSeen: fenceBaseline,+ });+ return true;+ }+ // The one thing the terminal pass may not bypass: a surviving proposal proves only that+ // canonical has not moved YET, so reading it as "nothing was saved" under a renewed claim+ // releases Save mid-write. It still lands terminal, on the reload exit with Save held.+ //+ // Compared against what we last SAW, not against the fence deadline, which carries no+ // information here: at this pass our own residual and a renewal BOTH compute to+ // `now + reported`, so only `reported` itself tells them apart. An absent value cannot,+ // so it does not fire - a fence armed by hydration has no previous read to compare.+ if (+ lastChance &&+ typeof reportedRemaining === "number" &&+ reportedRemaining > 0 &&+ holdExpiresAt !== null &&+ // An expiry LATER than the one this fence opened against is a new lease - our own can only+ // decay toward its instant, never past it. Comparing instants rather than remainders is+ // what makes this independent of when the two reads happened, so a tab throttled past the+ // deadline still gets the right answer. No reference at all means hold.+ (fenceBaseline === null || holdExpiresAt > fenceBaseline)+ ) {+ setGateFailure(unreadable());+ return true;+ }+ if (!row.proposed_workflow) {+ // Recovery does NOT re-read canonical onto the canvas. This fence blocks Save, sending and+ // navigation but not canvas editing, so the user can have work in progress here — and+ // nothing available tells that work apart from the staged draft, which is what made the+ // canvas dirty in the first place. Replacing it would silently destroy what they can see,+ // so the gate keeps holding and, at its deadline, says the canvas may be out of date.+ //+ // Not marked accepted either: a missing proposal is not evidence that THIS Accept cleared+ // it. _history_proposal_state also returns none when canonical no longer matches the+ // proposal's fingerprint - a concurrent save, whose 409 the client may never have seen.+ setGateFailure(unreadable());+ return true;+ }+ // Recovery never resolves as applied. A canonical version ahead of the editor is not+ // evidence that THIS Accept produced it — another writer's save reads identically, and a+ // proposal with no fingerprint carries LESS identity to prove the outcome, not more. Only+ // the server can attest that a given Accept landed; until it does, the gate says what it+ // knows instead of claiming a save it cannot prove.+ applyChatRowProposal(row, autoAcceptWritesAtRead);+ setGateFailure({ kind: "accept", ...attempt });+ return true;+ };++ // "Confirming…" is not allowed to sit there: at its deadline the outcome is resolved one last+ // time, and that pass cannot hold again, so the gate lands terminal - never on saved, because+ // recovery never resolves as applied. One exception, described below: a read that never settles+ // runs neither the fallback nor the re-arm.+ //+ // On Try again (never `lastChance`), the claim check runs before either proposal branch, so a+ // reported live claim keeps this Confirming whatever the re-read finds about the proposal.+ //+ // None of these reads is bounded - no timeout, no abort signal, and no axios client sets a+ // default - and the terminal fallback lives in `.then` while the re-arm lives in `.finally`,+ // so a hung read runs neither and leaves `holdRecheckInFlight` set, blocking every later re-arm.+ const recoverHoldRef = useRef(recoverHold);+ recoverHoldRef.current = recoverHold;+ const reconcileFailedAcceptRef = useRef(reconcileFailedAccept);+ reconcileFailedAcceptRef.current = reconcileFailedAccept;+ // Bumped once a re-read settles, so the next one arms; the gate object alone does not change.+ const [holdRecheck, setHoldRecheck] = useState(0);+ const holdRecheckInFlight = useRef(false);+ // Keyed on the gate OBJECT, never on its deadline: Try again re-arms with the SAME+ // `holdExpiresAt` by design, so a deadline key cannot see that replacement at all. It decides+ // the case where a superseded pass settles FIRST - the replacement then lands when Try again's+ // own read resolves, after that pass has run the `.finally` below, which is the only other+ // place that re-arms. Keyed on the deadline, that gate gets no timer and "Confirming…" sits.+ useEffect(() => {+ if (!recoverHold) {+ holdRecheckInFlight.current = false;+ return;+ }+ // Re-arming while a re-read is still settling would schedule against an already-expired+ // deadline and spin a timer-and-request loop until one response won.+ if (holdRecheckInFlight.current) {+ return;+ }+ const dueIn = recoverHold.holdExpiresAt - Date.now();+ // Inside the last interval there is nothing left to wait for, so that pass resolves+ // without holding; before it, a re-read keeps the deadline it inherited.+ const terminal = dueIn <= HOLD_RECHECK_MS;+ const timer = setTimeout(+ () => {+ const hold = recoverHoldRef.current;+ if (!hold) {+ return;+ }+ const attempt = {+ alwaysAccept: hold.alwaysAccept,+ token: hold.token,+ };+ holdRecheckInFlight.current = true;+ void reconcileFailedAcceptRef+ .current(+ attempt,+ terminal+ ? { lastChance: true, claimSeen: hold.claimExpiresAtSeen }+ : {+ holdUntil: hold.holdExpiresAt,+ claimSeen: hold.claimExpiresAtSeen,+ },+ )+ .then((settled) => {+ // The fence may not outlive its deadline on the server's behalf: if the last pass+ // could not reach a definite state, fall back to the gate whose hold the user ends+ // themselves. Driven by what the call REPORTED, never by a ref: this runs before+ // React re-renders, so a ref here still describes the pre-call world and would mark+ // the canvas stale even when the pass just proved the Accept never saved.+ if (!terminal || settled) {+ return;+ }+ setStaleCanvas(true);+ setGateFailure((current) =>+ current === hold ? { kind: "reload", attempt } : current,+ );+ })+ .finally(() => {+ holdRecheckInFlight.current = false;+ // A terminal pass is normally the last one, so it does not re-arm. But a newer gate+ // may have replaced this one while this read was out, and the effect skips arming+ // while a read is in flight - so without a bump here that gate gets no timer and+ // "Confirming…" sits there with only a click to end it. Compared by IDENTITY, never+ // by deadline - Try again re-arms with the SAME `holdExpiresAt` by design, so a+ // deadline test reports "unchanged" for the one replacement a user makes by hand -+ // and not unconditionally, which re-arms against this pass's own expired deadline and+ // supersedes a manual read that is still pending.+ if (!terminal || recoverHoldRef.current !== hold) {+ setHoldRecheck((count) => count + 1);+ }+ });+ },+ Math.max(0, Math.min(dueIn, HOLD_RECHECK_MS)),+ );+ return () => clearTimeout(timer);+ }, [recoverHold, holdRecheck]);++ const retryGateFailure = () => {+ if (!gateFailure) {+ return;+ }+ if (gateFailure.kind === "accept") {+ void handleAcceptWorkflow(gateFailure.alwaysAccept);+ return;+ }+ // The workflow this Accept saved is in hand, so the retry is to apply it — not to re-read+ // the chat row, which is the move for outcomes we do not know.+ if (gateFailure.kind === "saved") {+ if (+ applyWorkflowUpdate(gateFailure.savedWorkflow, {+ persisted: true,+ applied: true,+ })+ ) {+ markProposalAccepted(gateFailure.ownerTurnId);setProposedWorkflow(null);setPendingProposalMetadata(null);setPendingProposalRun(null);- if (alwaysAccept) {- setAutoAcceptFromWrite(true);- }+ setGateFailure(null);+ // The apply's 200 does not carry auto_accept: the route persists it after the workflow+ // write inside a best-effort swallow, so a 200 proves the version was created and+ // nothing about this flag. Read the server's value rather than keep the attempt's.+ // Only this field - hydrating the whole row here would re-arm the gate just cleared.+ // Clearing the gate unlocks navigation, so this read has to belong to the chat that+ // asked for it: New chat resets auto-accept, and a late answer from the old chat would+ // turn it back on in the new one and auto-apply past its review gate.+ const readChatId = workflowCopilotChatIdRef.current;+ const readSendEpoch = sendEpochRef.current;+ const readNavEpoch = chatNavEpochRef.current;+ const readAutoAcceptWrites = autoAcceptWrites.current;+ void fetchChatRow()+ .then((row) => {+ if (+ workflowCopilotChatIdRef.current !== readChatId ||+ sendEpochRef.current !== readSendEpoch ||+ chatNavEpochRef.current !== readNavEpoch+ ) {+ return;+ }+ if (row && autoAcceptWrites.current === readAutoAcceptWrites) {+ setAutoAccept(row.auto_accept ?? false);+ }+ })+ .catch((error) => {+ console.warn("Failed to re-read auto-accept after a retry:", error);+ });}- // The row write belongs to the accepted chat even if the pane has moved on.- await clearProposedWorkflow(alwaysAccept, chatId ?? undefined);+ return;}+ const attempt =+ gateFailure.kind === "reload"+ ? gateFailure.attempt+ : { alwaysAccept: gateFailure.alwaysAccept, token: gateFailure.token };+ if (!attempt) {+ void resyncProposalFromChatRow().then((row) => {+ if (row) {+ setGateFailure(null);+ }+ });+ return;+ }+ // The Accept's outcome is still unresolved, so the gate and navigation stay locked.+ // A manual retry inherits the fence's deadline for the same reason an automatic+ // re-read does: retrying may not extend a hold past the lease it was opened for.+ setIsAccepting(true);+ void reconcileFailedAccept(+ attempt,+ gateFailure.kind === "recover"+ ? {+ holdUntil: gateFailure.holdExpiresAt,+ // Try again RE-CHECKS this fence, it does not open a new one, so it hands back the+ // baseline rather than letting the read it triggers become one.+ claimSeen: gateFailure.claimExpiresAtSeen,+ }+ : { claimSeen: undefined },+ ).finally(() => setIsAccepting(false));};const handleRejectWorkflow = async () => {+ setGateFailure(null);+ // Reject is not inside the Accept fence, so History, New chat and the composer all stay+ // live while this POST is in flight. Everything below belongs to the chat and the turn+ // that asked for it: a later turn keeps its own snapshot, so landing this then would+ // revert THAT turn's proposal off the canvas and mark it rejected.+ const rejectChatId = workflowCopilotChatIdRef.current;+ const rejectSendEpoch = sendEpochRef.current;+ const rejectNavEpoch = chatNavEpochRef.current;if (!(await clearProposedWorkflow(false))) {return;}+ if (+ // A pane still resolving its chat id reads null here and its own id after the await -+ // `clearProposedWorkflow` resolves it - and that is NOT a switch. Comparing a captured+ // null against the resolved id aborted every Reject issued before the id was known.+ // `clearProposedWorkflow` already applies this rule to its own starting id.+ (rejectChatId !== null &&+ workflowCopilotChatIdRef.current !== rejectChatId) ||+ sendEpochRef.current !== rejectSendEpoch ||+ chatNavEpochRef.current !== rejectNavEpoch+ ) {+ return;+ }// The staged proposal was rendered onto the canvas mid-turn (via// WORKFLOW_DRAFT). Reject must revert the canvas to the pre-submit// canvas state captured client-side at submit time.@@ -3146,53 +3735,104 @@ export function WorkflowCopilotChat({// backend (keep_pending_proposal) may have kept it alive server-side.// useCallback-stable: handleSend depends on it and is itself a dependency// of other effects, so a churning identity here would cascade into them.- const resyncProposalFromChatRow = useCallback(async (): Promise<void> => {- const chatId = workflowCopilotChatIdRef.current?.trim();- if (!chatId) {- return;- }- const writesAtRead = autoAcceptWrites.current;- try {+ const fetchChatRow = useCallback(+ async (chatIdOverride?: string | null) => {+ // A caller's own id wins. workflowCopilotChatIdRef is assigned by a passive effect, so a+ // handler that just resolved an id is holding a better answer than the ref can give it for+ // another commit - and reading the ref there returns null and loses the read entirely.+ const chatId = (+ chatIdOverride ?? workflowCopilotChatIdRef.current+ )?.trim();+ if (!chatId) {+ return null;+ }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;+ return response.data;+ },+ [credentialGetter],+ );++ const applyChatRowProposal = useCallback(+ (+ row: WorkflowCopilotChatHistoryResponse,+ // The value of autoAcceptWrites when the READ was issued. A row fetched before a Turn off+ // landed describes the chat before that write, so it may not put auto-accept back on. The+ // read and the apply are separate functions here, so the caller carries the count.+ autoAcceptWritesAtRead?: number,+ ) => {+ if (+ autoAcceptWritesAtRead === undefined ||+ autoAcceptWritesAtRead === autoAcceptWrites.current+ ) {+ setAutoAccept(row.auto_accept ?? false);}- const nextProposal = response.data.proposed_workflow ?? null;+ const nextProposal = row.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);- }+ setPendingProposalMetadata(row.proposed_workflow_metadata ?? null);+ setPendingProposalRun(row.proposed_workflow_run ?? null);setPendingProposalTurnId((currentTurnId) =>nextProposal- ? (response.data.proposed_workflow_metadata?.owner_turn_id ??- getLatestDiffCardTurnIdFromHistory(response.data.chat_history) ??+ ? (row.proposed_workflow_metadata?.owner_turn_id ??+ getLatestDiffCardTurnIdFromHistory(row.chat_history) ??currentTurnId): null,);+ },+ [],+ );++ const resyncProposalFromChatRow = useCallback(async () => {+ // Navigation stays open during a plain reload, so its result belongs only to+ // the chat that started it.+ const chatId = workflowCopilotChatIdRef.current;+ const sendEpoch = sendEpochRef.current;+ const autoAcceptWritesAtRead = autoAcceptWrites.current;+ // Try again on a `reload` with no attempt neither locks the card nor sets `isAccepting`, so+ // two of these can be in flight at once. The chat id and send epoch below are unchanged+ // between them, so only call identity separates a late answer from the current one - the same+ // guard `reconcileFailedAccept` carries, for the same reason.+ const generation = (resyncGeneration.current += 1);+ const superseded = () => resyncGeneration.current !== generation;+ try {+ const row = await fetchChatRow();+ if (+ superseded() ||+ workflowCopilotChatIdRef.current !== chatId ||+ sendEpochRef.current !== sendEpoch+ ) {+ return null;+ }+ if (row) {+ recordClaimState(row);+ applyChatRowProposal(row, autoAcceptWritesAtRead);+ // This path does NOT clear staleCanvas. Only a persisted refresh the server gave us in+ // the same act does - applyWorkflowUpdate requires `persisted` AND `fresh` - because a+ // surviving proposal is not evidence the canvas is current.+ }+ return row;} catch (error) {console.error("Failed to resync pending proposal:", error);+ if (+ !superseded() &&+ workflowCopilotChatIdRef.current === chatId &&+ sendEpochRef.current === sendEpoch+ ) {+ setGateFailure({ kind: "reload", attempt: null });+ }+ return null;}- }, [credentialGetter]);+ }, [applyChatRowProposal, fetchChatRow, recordClaimState]);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> => {// 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 startingChatId = workflowCopilotChatIdRef.current?.trim() || null;const clearProposalByChatId = async (chatId: string): Promise<boolean> => {const client = await getClient(credentialGetter, "sans-api-v1");await client.post<WorkflowCopilotClearProposedWorkflowRequest>(@@ -3233,9 +3873,10 @@ export function WorkflowCopilotChat({return await clearProposalByChatId(chatId);} catch (error) {const status = getErrorStatus(error);- // 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) {+ // A caller that NAMES its chat must not fall back here: the latest chat is someone else's+ // row, and clearing it deletes that chat's pending review. Only Reject reaches this today,+ // and it names nothing; re-add that guard with any caller that does.+ if (status === 404) {try {const refreshedChatId = await fetchLatestChatId();if (refreshedChatId && refreshedChatId !== chatId) {@@ -3313,8 +3954,13 @@ export function WorkflowCopilotChat({let isMounted = true;const fetchHistory = async () => {- setIsLoadingHistory(true);+ const loadSeq = beginHistoryLoad();repin();+ // New chat neither unmounts this component nor is disabled while history loads, so+ // `isMounted` alone does not tell us the user is still where this read was sent. Without+ // this the late response restores the abandoned chat - including its Always accept, which+ // then lets the next auto-applicable proposal skip the review gate entirely.+ const navEpochAtStart = chatNavEpochRef.current;try {const client = await getClient(credentialGetter, "sans-api-v1");const response =@@ -3327,7 +3973,7 @@ export function WorkflowCopilotChat({{ retryTransientFailure: true },);- if (!isMounted) return;+ if (!isMounted || chatNavEpochRef.current !== navEpochAtStart) return;applyHistoryResponse(response.data);adoptRecoveredTurns(response.data);@@ -3336,7 +3982,7 @@ export function WorkflowCopilotChat({console.error("Failed to load chat history:", error);} finally {if (isMounted) {- setIsLoadingHistory(false);+ endHistoryLoad(loadSeq);}}};@@ -3355,6 +4001,8 @@ export function WorkflowCopilotChat({updateQueuedPrompt,workflowPermanentId,applyHistoryResponse,+ beginHistoryLoad,+ endHistoryLoad,discardQueuedPrompt,]);@@ -3524,7 +4172,15 @@ export function WorkflowCopilotChat({const submittingQuestion = useRef(false);const handleQuestionAnswer = useCallback(async (interaction: QuestionInteraction, response: QuestionResponse) => {- if (!workflowCopilotChatId || submittingQuestion.current) return false;+ // An answer RESUMES the turn, which stages a newer proposal - and a `saved` gate can then+ // replay its older confirmed workflow over that draft, on a Try again with no deadline.+ // The fence is enforced at handleSend, and this path does not go through it.+ if (+ !workflowCopilotChatId ||+ submittingQuestion.current ||+ acceptUnresolved+ )+ return false;submittingQuestion.current = true;setIsSubmittingQuestion(true);try {@@ -3566,6 +4222,7 @@ export function WorkflowCopilotChat({}},[+ acceptUnresolved,credentialGetter,workflowCopilotChatId,loadChatInPlace,@@ -3717,6 +4374,14 @@ export function WorkflowCopilotChat({const handleSend = useCallback(async (messageOverride?: string, options: SendOptions = {}) => {if (authoringInProgress) return;+ // A turn started now could stage a proposal the pending Accept then clears, and the+ // setGateFailure below would drop a hold whose write is still unaccounted for.+ // Callers that consume their input first (queue, auto-send, Generate) wait for it instead.+ if (acceptUnresolved) {+ return;+ }+ sendEpochRef.current += 1;+ setGateFailure(null);const candidate = messageOverride ?? inputValue;const pendingQuestion = questionInteractions.find((item) => item.status === "pending",@@ -4506,14 +5171,36 @@ export function WorkflowCopilotChat({response.updated_workflow &&shouldAutoApplyWorkflowResponse(response, userCancelledThisTurn)) {- applyWorkflowUpdate(response.updated_workflow, { applied: true });+ const autoApplied = applyWorkflowUpdate(response.updated_workflow, {+ applied: true,+ fresh: true,+ // Deliberately redundant, not an oversight: `shouldAutoApplyWorkflowResponse` already+ // requires `workflow_applied`, and restating it here keeps `persisted` from becoming a+ // claim the server never backed if that predicate is ever widened again.+ persisted: response.workflow_applied === true,+ });// This turn's auto-commit already moved canonical past any earlier// bypassed proposal — drop the stale handle so its gate cannot- // reapply an outdated draft over what was just committed.+ // reapply an outdated draft over what was just committed. This runs+ // whether or not the editor took the workflow: those handles belong to+ // an OLDER turn either way, and leaving them would let the card below+ // render that turn's evidence while its retry writes this one's.setProposedWorkflow(null);setPendingProposalMetadata(null);setPendingProposalRun(null);setPendingProposalTurnId(null);+ if (!autoApplied && response.workflow_applied === true) {+ // The server committed this version and the editor could not take it: the state+ // `saved` exists for, reached by auto-commit rather than Accept. Save stays held+ // by this gate's kind, not by the handles cleared above.+ setGateFailure({+ kind: "saved",+ savedWorkflow: response.updated_workflow,+ ownerTurnId: responseTurnId,+ alwaysAccept: false,+ token: null,+ });+ }} else if (response.updated_workflow) {setProposedWorkflow(response.updated_workflow);setPendingProposalMetadata(@@ -4991,6 +5678,7 @@ export function WorkflowCopilotChat({}},[+ acceptUnresolved,applyStoredNarrativeEvent,applyWorkflowUpdate,armStop,@@ -5042,7 +5730,13 @@ export function WorkflowCopilotChat({const handleConnectedAccountChoice = useCallback((turnId: string, connectionId: string) => {- if (hasPendingQuestion || connectedAccountChoiceLatch.current !== null) {+ // `handleSend` drops the send when the fence is up, and it does so AFTER this latch would+ // be taken - which greys the picker out for the turn without sending anything.+ if (+ hasPendingQuestion ||+ acceptUnresolved ||+ connectedAccountChoiceLatch.current !== null+ ) {return;}// Build this before taking the latch so an unavailable UUID API cannot@@ -5062,7 +5756,7 @@ export function WorkflowCopilotChat({}});},- [handleSend, hasPendingQuestion],+ [handleSend, hasPendingQuestion, acceptUnresolved],);// A code block's "Generate" button asks the copilot to (re)build that one block@@ -5098,7 +5792,7 @@ export function WorkflowCopilotChat({if (blockBuildArmNonce === 0 || blockBuildMessageRef.current === null) {return;}- if (!codeWorkflow) {+ if (!codeWorkflow || acceptUnresolved) {return;}const message = blockBuildMessageRef.current;@@ -5111,7 +5805,13 @@ export function WorkflowCopilotChat({return;}void handleSend(message);- }, [blockBuildArmNonce, codeWorkflow, handleSend, finishBlockGenerating]);+ }, [+ blockBuildArmNonce,+ codeWorkflow,+ acceptUnresolved,+ handleSend,+ finishBlockGenerating,+ ]);const blockGenLoadingRef = useRef(isLoading);useEffect(() => {@@ -5166,7 +5866,13 @@ export function WorkflowCopilotChat({};useEffect(() => {- if (!queuedPrompt || hasPendingQuestion || authoringInProgress) {+ // A pending Accept would clear whatever proposal this turn stages, so wait for it.+ if (+ !queuedPrompt ||+ hasPendingQuestion ||+ acceptUnresolved ||+ authoringInProgress+ ) {return;}// isLoading (reactive state) is the in-flight signal here so the effect@@ -5226,6 +5932,7 @@ export function WorkflowCopilotChat({console.error("Queued send failed:", error);});}, [+ acceptUnresolved,authoringInProgress,codeBlockModeEnabled,codeBlockRequestOverride,@@ -5242,7 +5949,13 @@ export function WorkflowCopilotChat({if (!autoSendMessage || hasAutoSentRef.current) {return;}- if (isLoadingHistory || isLoading || !workflowPermanentId || queuedPrompt) {+ if (+ isLoadingHistory ||+ isLoading ||+ acceptUnresolved ||+ !workflowPermanentId ||+ queuedPrompt+ ) {return;}// Synchronous gate: isLoadingHistory state is stale in this effect's@@ -5280,6 +5993,7 @@ export function WorkflowCopilotChat({handleSend,autoSendMessage,initialAction,+ acceptUnresolved,isLoading,isLoadingHistory,queuedPrompt,@@ -5294,6 +6008,7 @@ export function WorkflowCopilotChat({if (isLoadingHistory ||isLoading ||+ acceptUnresolved ||isWaitingForLiveBrowser ||queuedPrompt) {@@ -5321,6 +6036,7 @@ export function WorkflowCopilotChat({}, [autoSendMessage,initialAction,+ acceptUnresolved,isLoadingHistory,isLoading,isWaitingForLiveBrowser,@@ -5547,8 +6263,32 @@ export function WorkflowCopilotChat({);// Mid-turn Accept would be clobbered by the in-flight turn's terminal// restore, so gate actions wait for idle.- const gateActionable =- Boolean(proposedWorkflow) && !isLoading && !isLoadingHistory;+ // Is there anything for the gate to show? A staged proposal, or either of the two gates that+ // OUTLIVE their proposal: `saved`, because the Accept succeeded and hydration then clears the+ // staged row while the fence still holds Save, and `recover`, which hydration preserves while+ // the proposal goes away. Every place that gates on this asks HERE, with one real exception:+ // the Save hold's `reloadCardShowing` re-derives the proposal check because it is computed+ // above this declaration and NOT YET HOISTED - a deferral, not a constraint; both its inputs are+ // in scope there. Elsewhere a local copy falls out of step and strands the user with a held Save+ // and no Try again.+ //+ // To check this is still exhaustive, grep the gating EXPRESSION - every `Boolean(proposedWorkflow)`+ // and `proposedWorkflow &&` in a gating position - not the components that render the gate. A copy+ // can be lifted into a variable and passed down as a prop, where a scan of render sites misses it.+ // `saved` and `recover` both OUTLIVE the proposal they were opened over, and each names an+ // on-screen exit in its Save reason, so the card must render or that exit does not exist. Keyed+ // on the gate rather than on how the subject became null, so any route that strands one of them+ // without a proposal is covered.+ //+ // Actionability does NOT follow from this flag: `ReviewGateCard`'s fieldset is disabled for+ // `reload`, `recover` and `saved` regardless of `actionsEnabled`, and that attribute is what+ // keeps a second Accept from starting under an unresolved one.+ const gateHasSubject =+ Boolean(proposedWorkflow) ||+ gateFailure?.kind === "saved" ||+ gateFailure?.kind === "recover";+ // The action row stays locked for `saved` by its own fieldset, so this enables the exit only.+ const gateActionable = gateHasSubject && !isLoading && !isLoadingHistory;const turningOffThisChat =workflowCopilotChatId !== null &&(turningOffCounts.get(workflowCopilotChatId) ?? 0) > 0;@@ -5665,7 +6405,11 @@ export function WorkflowCopilotChat({<QuestionPartsCardkey={interaction.interaction_id}interaction={interaction}- disabled={isSubmittingQuestion}+ // The answer path is fenced in `handleQuestionAnswer` and returns false silently, so a card+ // that stays clickable under an unresolved Accept reports a submit that never happened -+ // the false receipt this slice exists to remove. Same gate and same reason as Cancel.+ disabled={isSubmittingQuestion || acceptUnresolved}+ lockReason={acceptUnresolved ? acceptHoldReason : null}onAnswer={(response) => void handleQuestionAnswer(interaction, response)}/>);@@ -5722,13 +6466,16 @@ export function WorkflowCopilotChat({workflowPermanentId={workflowPermanentId}currentChatId={workflowCopilotChatId}onSelect={handleSelectHistoryChat}- disabled={isLoading || isLoadingHistory}+ disabled={headerControlsDisabled}+ lockedReason={acceptHoldReason ?? undefined}/><buttontype="button"+ disabled={acceptHoldReason !== null}+ title={acceptHoldReason ?? undefined}onClick={handleNewChat}onMouseDown={(e) => e.stopPropagation()}- className="rounded border border-border px-2 py-1 text-xs text-muted-foreground hover:bg-accent hover:text-accent-foreground"+ className="rounded border border-border px-2 py-1 text-xs text-muted-foreground hover:bg-accent hover:text-accent-foreground disabled:opacity-50">New chat</button>@@ -5971,6 +6718,7 @@ export function WorkflowCopilotChat({hasPendingQuestion ||hasUnconsumedAdjacentMessage ||selectedConnectionId !== null ||+ acceptUnresolved ||connectedAccountChoicePendingTurnId === turnId}onSelect={(connectionId) =>@@ -5993,6 +6741,7 @@ export function WorkflowCopilotChat({isLoading ||hasPendingQuestion ||turnId === null ||+ acceptUnresolved ||connectedAccountChoicePendingTurnId === turnId}onSelect={(connectionId) => {@@ -6011,10 +6760,9 @@ export function WorkflowCopilotChat({<ProposalRunFactsLine facts={pendingProposalRun} />) : null}<ReviewGateCard+ canvasHasEdits={canvasHasEdits}turn={message.narrative}- pending={- index === gateIndex && Boolean(proposedWorkflow)- }+ pending={index === gateIndex && gateHasSubject}verdict={getReviewGateVerdict(message.narrative,proposedWorkflow,@@ -6028,20 +6776,21 @@ export function WorkflowCopilotChat({}actionsEnabled={gateActionable}acceptsEnabled={gateAcceptsEnabled}- onAccept={() =>- proposedWorkflow &&- handleAcceptWorkflow(proposedWorkflow)- }- onAlwaysAccept={() =>- proposedWorkflow &&- handleAcceptWorkflow(proposedWorkflow, true)- }+ onAccept={() => handleAcceptWorkflow()}+ onAlwaysAccept={() => handleAcceptWorkflow(true)}onReject={handleRejectWorkflow}onReview={() =>proposedWorkflow &&handleReviewWorkflow(proposedWorkflow)}onTestEndToEnd={handleTestEndToEnd}+ accepting={isAccepting}+ failure={gateFailureKind}+ onRetry={+ gateFailureRetryable+ ? retryGateFailure+ : undefined+ }gateId={turnId ? `copilot-gate-${turnId}` : undefined}@@ -6055,6 +6804,8 @@ export function WorkflowCopilotChat({isLastMessage &&shouldShowConfirmCard(message.narrative) ? (<ConfirmCard+ disabled={acceptUnresolved}+ lockReason={acceptHoldReason}onConfirm={() => handleSend("Confirmed.")}onChangeInstead={() => {textareaRef.current?.focus();@@ -6161,8 +6912,7 @@ export function WorkflowCopilotChat({</div>);}- const isGateOwnerOrLast =- index === gateIndex && Boolean(proposedWorkflow);+ const isGateOwnerOrLast = index === gateIndex && gateHasSubject;const selectionReceipt = connectedAccountSelectionReceipt(messages,index,@@ -6191,6 +6941,7 @@ export function WorkflowCopilotChat({<ProposalRunFactsLine facts={pendingProposalRun} />) : null}<ReviewGateCard+ canvasHasEdits={canvasHasEdits}pendingverdict={getReviewGateVerdict(gateOwnerNarrative,@@ -6199,20 +6950,21 @@ export function WorkflowCopilotChat({settled={null}actionsEnabled={gateActionable}acceptsEnabled={gateAcceptsEnabled}- onAccept={() =>- proposedWorkflow &&- handleAcceptWorkflow(proposedWorkflow)- }- onAlwaysAccept={() =>- proposedWorkflow &&- handleAcceptWorkflow(proposedWorkflow, true)- }+ onAccept={() => handleAcceptWorkflow()}+ onAlwaysAccept={() => handleAcceptWorkflow(true)}onReject={handleRejectWorkflow}onReview={() =>proposedWorkflow &&handleReviewWorkflow(proposedWorkflow)}onTestEndToEnd={handleTestEndToEnd}+ accepting={isAccepting}+ failure={gateFailureKind}+ onRetry={+ gateFailureRetryable+ ? retryGateFailure+ : undefined+ }/></div>) : null@@ -6225,24 +6977,33 @@ export function WorkflowCopilotChat({.map(renderQuestionCard);return [...questions, rendered];})}- {proposedWorkflow && !gateOwnerRendersInline ? (+ {gateHasSubject && !gateOwnerRendersInline ? (<div className="space-y-2">{pendingProposalRun ? (<ProposalRunFactsLine facts={pendingProposalRun} />) : null}<ReviewGateCard+ canvasHasEdits={canvasHasEdits}pendingverdict={getReviewGateVerdict(undefined, proposedWorkflow)}settled={null}actionsEnabled={gateActionable}acceptsEnabled={gateAcceptsEnabled}- onAccept={() => handleAcceptWorkflow(proposedWorkflow)}- onAlwaysAccept={() =>- handleAcceptWorkflow(proposedWorkflow, true)- }+ onAccept={() => handleAcceptWorkflow()}+ onAlwaysAccept={() => handleAcceptWorkflow(true)}onReject={handleRejectWorkflow}- onReview={() => handleReviewWorkflow(proposedWorkflow)}+ onReview={+ proposedWorkflow+ ? () => handleReviewWorkflow(proposedWorkflow)+ : // Reached in `saved` and `recover`, the two gates that outlive the proposal+ // they were opened over. There is nothing staged to open, and Review sits+ // inside this card's disabled fieldset in both, so nothing in the UI calls this.+ () => {}+ }onTestEndToEnd={handleTestEndToEnd}+ accepting={isAccepting}+ failure={gateFailureKind}+ onRetry={gateFailureRetryable ? retryGateFailure : undefined}/></div>) : null}@@ -6259,7 +7020,10 @@ export function WorkflowCopilotChat({<Buttonvariant="ghost"size="sm"- disabled={isLoading}+ // Cancelling reloads the turn, which can stage a newer proposal for a pending+ // Accept to overwrite. Same reason the answer path is fenced.+ disabled={isLoading || acceptUnresolved}+ title={acceptHoldReason ?? undefined}onClick={async () => {try {const client = await getClient(@@ -6708,14 +7472,25 @@ export function WorkflowCopilotChat({/><TooltipProvider><ControlTooltip- content={morphButtonLabel}- blocked={waitingOnQueueOnly || authoringBlocksComposerAction}+ content={+ acceptHoldReason ? (+ <span className="block max-w-xs">{acceptHoldReason}</span>+ ) : (+ morphButtonLabel+ )+ }+ blocked={+ waitingOnQueueOnly ||+ acceptUnresolved ||+ authoringBlocksComposerAction+ }><buttontype="button"disabled={waitingOnQueueOnly ||isStopping ||+ acceptUnresolved ||authoringBlocksComposerAction}aria-busy={isStopping}diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotHistory.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotHistory.tsxindex 2bcec26bf..d863f4c0f 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotHistory.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotHistory.tsx@@ -11,12 +11,8 @@ import {PopoverContent,PopoverTrigger,} from "@/components/ui/popover";-import {- Tooltip,- TooltipContent,- TooltipTrigger,-} from "@/components/ui/tooltip";import { Skeleton } from "@/components/ui/skeleton";+import { ControlTooltip } from "@/routes/workflows/studio/ControlTooltip";import { handleInfiniteScroll } from "@/util/utils";import { compactLocalDateTime } from "@/util/timeFormat";import { useDebounce } from "use-debounce";@@ -28,6 +24,8 @@ interface WorkflowCopilotHistoryProps {currentChatId: string | null;onSelect: (chat: WorkflowCopilotChatSummary) => void;disabled?: boolean;+ // Shown on the disabled trigger so the lock states its own reason.+ lockedReason?: string;// Icon-only trigger (narrow pane headers); the label moves to a tooltip.compact?: boolean;}@@ -156,6 +154,7 @@ function WorkflowCopilotHistory({currentChatId,onSelect,disabled = false,+ lockedReason,compact = false,}: WorkflowCopilotHistoryProps) {const [open, setOpen] = useState(false);@@ -178,12 +177,17 @@ function WorkflowCopilotHistory({<buttontype="button"disabled={disabled}- aria-label="History"+ // No title here: this button is disabled when there IS a reason, and a disabled+ // button receives no hover and takes no focus, so a title on it can never be read.+ // The reason hangs off a wrapper below instead.+ aria-label={+ lockedReason ? `History unavailable: ${lockedReason}` : "History"+ }onMouseDown={(e) => e.stopPropagation()}className={compact- ? "inline-flex h-7 w-7 shrink-0 items-center justify-center rounded-md text-muted-foreground hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:opacity-40"- : "flex items-center gap-1 rounded border border-border px-2 py-1 text-xs text-muted-foreground hover:bg-accent hover:text-accent-foreground disabled:opacity-50"+ ? "inline-flex h-7 w-7 shrink-0 items-center justify-center rounded-md text-muted-foreground hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-40"+ : "flex items-center gap-1 rounded border border-border px-2 py-1 text-xs text-muted-foreground hover:bg-accent hover:text-accent-foreground disabled:pointer-events-none disabled:opacity-50"}><CounterClockwiseClockIcon@@ -198,12 +202,32 @@ function WorkflowCopilotHistory({return (<Popover open={open} onOpenChange={setOpen}>{compact ? (- <Tooltip>- <TooltipTrigger asChild>{trigger}</TooltipTrigger>- <TooltipContent side="bottom">History</TooltipContent>- </Tooltip>+ // A disabled trigger cannot open its own tooltip, so the reason hangs off the+ // focusable wrapper instead (see ControlTooltip).+ <ControlTooltip+ content={+ lockedReason ? (+ <span className="block max-w-xs">{lockedReason}</span>+ ) : (+ "History"+ )+ }+ blocked={disabled}+ >+ {trigger}+ </ControlTooltip>) : (- trigger+ // The floating chat's header is outside any TooltipProvider, so ControlTooltip cannot+ // render here - but the reason still has to be reachable. A wrapper that is NOT+ // disabled receives the hover the button cannot, and takes focus while the control is+ // blocked so a keyboard reaches it too.+ <span+ title={lockedReason}+ tabIndex={disabled && lockedReason ? 0 : undefined}+ className="inline-flex"+ >+ {trigger}+ </span>)}<PopoverContentclassName="w-80 p-0"diff --git a/skyvern-frontend/src/routes/workflows/copilot/acceptFence.ts b/skyvern-frontend/src/routes/workflows/copilot/acceptFence.tsnew file mode 100644index 000000000..3e56e3651--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/copilot/acceptFence.ts@@ -0,0 +1,201 @@+import { type WorkflowApiResponse } from "@/routes/workflows/types/workflowTypes";++import {+ type CopilotProposalMetadata,+ type WorkflowCopilotChatHistoryResponse,+} from "./workflowCopilotTypes";++/**+ * The Accept fence's pure rules: whether the server may still be writing an Accept, how long the+ * fence covering it lasts, and what a hydrated chat row may do to it. They live outside the chat+ * component so the contract can be exercised directly.+ */++export type AcceptAttempt = { alwaysAccept: boolean; token: string | null };++export const proposalTokenOf = (+ metadata: CopilotProposalMetadata | null,+): string | null =>+ metadata ? `${metadata.owner_turn_id}:${metadata.revision}` : null;++// Mirrors the backend's COPILOT_PROPOSAL_CLAIM_LEASE. This is NOT used to interpret a server+// timestamp — comparing clocks is the bug this slice removed — only as a local upper bound on+// how long to keep the fence when the server cannot say what is left of its own lease: an+// instance that predates the field, or a read we cannot make at all.+export const UNREPORTED_CLAIM_LEASE_MS = 5 * 60 * 1000;++// How often the fence looks again while it lasts. A read that fails is usually a transient+// abort, so re-reading recovers long before the bound above does.+export const HOLD_RECHECK_MS = 10_000;++export type GateFailure =+ | ({ kind: "accept" } & AcceptAttempt)+ // holdExpiresAt is derived from the server's remaining lease, never from its clock.+ // claimExpiresAtSeen is the claim's ABSOLUTE EXPIRY as the read that OPENED this fence saw it,+ // carried on the gate so the comparison can only use a value belonging to this fence. Absolute,+ // because a remainder measured at two unknown times is not a discriminator at all.+ | ({+ kind: "recover";+ holdExpiresAt: number;+ claimExpiresAtSeen: number | null;+ } & AcceptAttempt)+ // The one outcome the client KNOWS: the apply returned 200 and its workflow is in hand, but+ // the editor could not take it. Distinct from `recover` because all three of its behaviours+ // differ — the copy is "saved", not "may have saved"; the retry re-applies this workflow+ // rather than re-reading the chat row; and there is no server claim to expire, so no+ // deadline may release it over a canvas we know is stale.+ // ownerTurnId is carried here because hydration clears the component's pending-turn field+ // whenever the row has no proposal - which is the truth after a save - and this gate+ // deliberately outlives that. Without it the retry has no turn to mark accepted, so a save+ // that definitely landed reports nothing.+ | ({+ kind: "saved";+ savedWorkflow: WorkflowApiResponse;+ ownerTurnId: string | null;+ } & AcceptAttempt)+ | { kind: "reload"; attempt: AcceptAttempt | null };++// The one rule for "the server may still be writing this Accept", and the deadline the fence+// ends at. A reported remainder is the server's own; an absent field (not an explicit null) is+// an instance that predates it, which can only name the claim — and that claim can run for the+// whole lease, so it holds until `unreportedDeadline`.+export const claimHoldDeadline = (+ row: Pick<+ WorkflowCopilotChatHistoryResponse,+ "proposed_claim_expires_in_seconds" | "proposed_workflow_metadata"+ >,+ unreportedDeadline: number,+): number | null => {+ // Absent and null are different answers: null is a current server saying no claim is held,+ // absent is a server that cannot say. Coalescing them releases the fence mid-rollout.+ const reported = row.proposed_claim_expires_in_seconds;+ if (reported !== undefined) {+ return reported !== null && reported > 0+ ? Date.now() + reported * 1000+ : null;+ }+ return row.proposed_workflow_metadata?.disposition === "accepting"+ ? unreportedDeadline+ : null;+};++/**+ * What a hydrated chat row does to the Accept fence. It ARMS the hold when the row reports a+ * claim, and preserves the two holds a chat row cannot disprove:+ *+ * - `recover`, because "the server reports no claim" is not proof no write is in flight: the+ * apply route acquires its claim only after its canonical lookup and normalization, so a read+ * can overtake a POST that is about to write. Releasing is that hold's own job, via its deadline.+ * - `saved`, because that gate carries the ONLY copy of a workflow the server confirmed. Dropping+ * it discards the retry's copy and releases Save over a canvas known to be older than the+ * server - the exact overwrite this fence exists to prevent.+ *+ * It does not preserve `accept` or `reload`: `accept` says nothing was saved, which a fresh row+ * restates for itself, and `reload`'s Save hold lives in `staleCanvas`, which hydration never+ * touches. This is NOT "monotone, never disarms a hold" - that would be true only of `recover`+ * and would hide the `saved` case from a reader checking this function for something else.+ */+export const hydratedGateFailure = (+ row: Pick<+ WorkflowCopilotChatHistoryResponse,+ | "proposed_claim_expires_in_seconds"+ | "proposed_workflow_metadata"+ | "auto_accept"+ >,+ current: GateFailure | null,+ unreportedDeadline: number,+): GateFailure | null => {+ const holdExpiresAt = claimHoldDeadline(row, unreportedDeadline);+ // Only over a claim this row can TIE TO A PROPOSAL. A claim with no metadata is very likely+ // another writer's, and `recover` would tell the user their own Accept may have saved and send+ // them to a Try again that does not render. That hold lives in `unattributedClaimDeadline`.+ const claimIsAttributable = Boolean(row.proposed_workflow_metadata);+ // Not over a `saved` hold. That gate KNOWS the write landed - it holds the workflow the+ // server returned - which is strictly more information than a claim saying one MAY be in+ // flight, so arming here would trade certainty for uncertainty: it would drop the only copy+ // of that workflow and give the hold a deadline that can release over a stale canvas. The+ // claim can outlive the write, because the route clears it after the write, best-effort.+ if (+ holdExpiresAt !== null &&+ claimIsAttributable &&+ current?.kind !== "saved"+ ) {+ return {+ kind: "recover",+ alwaysAccept: row.auto_accept ?? false,+ token: proposalTokenOf(row.proposed_workflow_metadata ?? null),+ holdExpiresAt,+ // Hydration RE-ARMS an open fence as readily as it opens one, so it may not move a+ // baseline either: an existing recovery hold keeps the remainder it opened with.+ claimExpiresAtSeen: fenceBaselineFor(+ current?.kind === "recover" ? current.claimExpiresAtSeen : undefined,+ holdExpiresAt,+ ),+ };+ }+ return current?.kind === "recover" || current?.kind === "saved"+ ? current+ : null;+};++/**+ * When the server reports a live claim it cannot tie to a displayable proposal. Someone is+ * writing this workflow and it is almost certainly not this turn, so Save must hold - but+ * nothing here is about OUR Accept, so it arms no gate and shows no card. Returns the deadline+ * the hold ends at, so the lock always ends rather than waiting for a hydration that may never+ * come.+ */+export const unattributedClaimDeadline = (+ row: Pick<+ WorkflowCopilotChatHistoryResponse,+ "proposed_claim_expires_in_seconds" | "proposed_workflow_metadata"+ >,+ now: number = Date.now(),+): number | null => {+ const reported = row.proposed_claim_expires_in_seconds;+ if (typeof reported !== "number" || reported <= 0) {+ return null;+ }+ return row.proposed_workflow_metadata ? null : now + reported * 1000;+};++/**+ * The instant a fence compares against, fixed AT ITS OPENING and never moved afterwards.+ *+ * `undefined` means no fence exists yet, so this read opens one and its reading becomes the+ * baseline. Anything else belongs to a fence already open and is returned untouched - INCLUDING+ * null, which is not "no answer yet" but the answer itself: a fence that opened blind must stay+ * blind, because the terminal pass treats a missing baseline as grounds to HOLD, and letting a+ * later poll supply one from ANOTHER writer's claim would hand that rule a number to compare+ * against and release. A fence may not become better informed than it was when it opened.+ */+export const fenceBaselineFor = (+ claimSeen: number | null | undefined,+ seenNow: number | null,+): number | null => (claimSeen === undefined ? seenNow : claimSeen);++/**+ * How a chat row may move the unattributed-claim hold: it may ARM it, it may EXTEND it, and it+ * may never RETIRE it.+ *+ * The evidence is narrower than the fact it maintains. "Another writer holds this workflow" is+ * workflow-scoped, but the server reads the claim off ONE CHAT'S proposal blob, so a row proves+ * only that SOME claim ends no earlier than it says - and a row reporting none covers its own+ * chat alone. Existence needs a single witness; absence needs coverage no row has.+ *+ * Hence MAX, NOT LAST. A chat-scoped read cannot tell you whose claim it is seeing, so the+ * latest deadline ever observed is the earliest instant it is safe to release; taking the last+ * would let a second chat's shorter claim release Save while the first writer is still writing.+ * The decaying reads of ONE lease all compute the same absolute instant, so max costs nothing+ * there - it holds longer only where two claims are genuinely indistinguishable, which is+ * exactly where holding is correct.+ */+export const extendedClaimHold = (+ current: number | null,+ deadline: number | null,+): number | null => {+ if (deadline === null) {+ return current;+ }+ return current === null || deadline > current ? deadline : current;+};diff --git a/skyvern-frontend/src/routes/workflows/copilot/cards/ConfirmCard.tsx b/skyvern-frontend/src/routes/workflows/copilot/cards/ConfirmCard.tsxindex 019aba604..8aa658f18 100644--- a/skyvern-frontend/src/routes/workflows/copilot/cards/ConfirmCard.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/cards/ConfirmCard.tsx@@ -22,15 +22,25 @@ export function shouldShowConfirmCard(turn: TurnNarrativeState): boolean {type ConfirmCardProps = {onConfirm: () => void;onChangeInstead: () => void;+ disabled?: boolean;+ // Why Confirm is inert, when that is a hold rather than a missing handler.+ lockReason?: string | null;};-export function ConfirmCard({ onConfirm, onChangeInstead }: ConfirmCardProps) {+export function ConfirmCard({+ onConfirm,+ onChangeInstead,+ disabled = false,+ lockReason,+}: ConfirmCardProps) {return (- <div className="flex flex-wrap gap-2 pl-1">+ <div className="flex flex-wrap gap-2 pl-1" title={lockReason ?? undefined}><buttontype="button"onClick={onConfirm}- className="rounded-md bg-success px-3 py-1.5 text-xs font-semibold text-success-foreground hover:opacity-90"+ disabled={disabled}+ title={lockReason ?? undefined}+ className="rounded-md bg-success px-3 py-1.5 text-xs font-semibold text-success-foreground hover:opacity-90 disabled:cursor-default disabled:opacity-50">Confirm</button>diff --git a/skyvern-frontend/src/routes/workflows/copilot/cards/QuestionPartsCard.tsx b/skyvern-frontend/src/routes/workflows/copilot/cards/QuestionPartsCard.tsxindex 713933fa0..fc55487ea 100644--- a/skyvern-frontend/src/routes/workflows/copilot/cards/QuestionPartsCard.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/cards/QuestionPartsCard.tsx@@ -10,10 +10,14 @@ import type {export function QuestionPartsCard({interaction,disabled = false,+ lockReason,onAnswer,}: {interaction: QuestionInteraction;disabled?: boolean;+ // Why the controls are inert, when that is a hold rather than an in-flight submit. Without it a+ // disabled card is indistinguishable from a broken one.+ lockReason?: string | null;onAnswer: (response: QuestionResponse) => void;}) {const [answers, setAnswers] = useState<Record<string, QuestionAnswer>>({});@@ -129,15 +133,19 @@ export function QuestionPartsCard({</p>) : null}{interaction.status === "pending" ? (- <div className="flex items-center justify-between gap-2 border-t border-border px-3 py-2">+ <div+ className="flex items-center justify-between gap-2 border-t border-border px-3 py-2"+ title={lockReason ?? undefined}+ ><span className="text-[11px] text-muted-foreground">- {selectedAnswers.length} choices selected+ {lockReason ?? `${selectedAnswers.length} choices selected`}</span><div className="flex gap-2"><Buttonsize="sm"variant="ghost"disabled={disabled}+ title={lockReason ?? undefined}onClick={() => onAnswer({ skipped: true })}>Skip@@ -148,6 +156,7 @@ export function QuestionPartsCard({disabled={disabled || (selectedAnswers.length === 0 && freeText === "")}+ title={lockReason ?? undefined}onClick={() =>onAnswer({answers: selectedAnswers,diff --git a/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.tsx b/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.tsxindex aef9755f0..b8b40e6d8 100644--- a/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/cards/ReviewGateCard.tsx@@ -1,5 +1,6 @@import { MagicWandIcon } from "@radix-ui/react-icons";+import { Badge, type BadgeProps } from "@/components/ui/badge";import { WorkflowApiResponse } from "@/routes/workflows/types/workflowTypes";import { humanizeBlockLabel } from "../blockLabel";@@ -76,6 +77,45 @@ const VERDICT_PILL_LABELS: Record<"tested" | "untested", string> = {untested: "Untested",};+// Cause-coded per the 2026-07-13 ruling on terminal states: red only for a write that+// failed, amber for one whose outcome we cannot read yet. Most Accept "failures" are a+// second click on a first click that had already saved, so amber is the honest colour.+const GATE_STATUS: Record<+ "accepting" | "accept" | "changed" | "recover" | "reload" | "saved",+ { label: string; variant: BadgeProps["variant"]; line: string }+> = {+ accepting: {+ label: "Accepting…",+ variant: "secondary",+ line: "Saving your accepted changes.",+ },+ accept: {+ label: "Not saved",+ variant: "destructive",+ line: "Accept failed, so nothing was saved. The proposal is still pending.",+ },+ changed: {+ label: "Proposal changed",+ variant: "warning",+ line: "Nothing was saved because this proposal changed. Review it before accepting.",+ },+ recover: {+ label: "Confirming…",+ variant: "warning",+ line: "This may have saved. Don't save the workflow until Copilot confirms.",+ },+ saved: {+ label: "Saved, not shown",+ variant: "warning",+ line: "Copilot saved this change, but the editor couldn't load it. Try again \u2014 this replaces what's on the canvas \u2014 or reload the page, which discards unsaved canvas edits.",+ },+ reload: {+ label: "Couldn't reload",+ variant: "warning",+ line: "Couldn't reload this proposal, so it may be out of date. Try again, or reload the page \u2014 reloading discards unsaved canvas edits.",+ },+};+interface ReviewGateCardProps {turn?: TurnNarrativeState;pending: boolean;@@ -90,9 +130,18 @@ interface ReviewGateCardProps {onReject: () => void;onReview: () => void;onTestEndToEnd?: () => void;+ accepting?: boolean;+ failure?: "accept" | "changed" | "recover" | "reload" | "saved" | null;+ onRetry?: () => void;gateId?: string;// Transient highlight when the pending-proposal chip scrolls to this gate.flash?: boolean;+ // The canvas holds unsaved changes. `saved`'s Try again REPLACES the canvas with the workflow+ // the server confirmed, so they go with it - a destructive control has to say so while the work+ // still exists. Deliberately not phrased as the USER's edits: the editor sets this flag on any+ // apply without `persisted`, including the copilot's own mid-turn draft, so authorship is not+ // something this flag can attest.+ canvasHasEdits?: boolean;}const REVIEW_SECTIONS = [@@ -136,10 +185,19 @@ export function ReviewGateCard({onReject,onReview,onTestEndToEnd,+ accepting = false,+ failure = null,+ onRetry,gateId,flash = false,+ canvasHasEdits = false,}: ReviewGateCardProps) {const draft = turn?.draft ?? null;+ const gateStatus = failure+ ? GATE_STATUS[failure]+ : accepting+ ? GATE_STATUS.accepting+ : null;const billingCreditRefusal =turn?.turnFacts?.terminalCause === "billing_credit_admission_refusal";const rejected = settled === "rejected";@@ -239,72 +297,118 @@ export function ReviewGateCard({</div>) : null}{pending && actionsEnabled ? (- <div className="flex flex-wrap gap-2 border-t border-border/55 bg-slate-elevation1/55 px-3 py-2">- {billingCreditRefusal ? (- <p className="basis-full text-[11px] leading-snug text-muted-foreground">- No browser or run started because credits are exhausted.{" "}- <a- href="/billing"- className="font-medium text-sky-700 underline underline-offset-2 dark:text-sky-300"- >- Go to Billing- </a>- .- </p>- ) : null}- <button- type="button"- onClick={onReview}- className="rounded-md border border-border px-3 py-1.5 text-xs text-foreground hover:bg-slate-elevation4 dark:text-slate-200"- >- Review- </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"+ <div className="border-t border-border/55 bg-slate-elevation1/55 px-3 py-2">+ {gateStatus ? (+ <div+ role={failure ? "alert" : "status"}+ className="mb-2 flex flex-wrap items-center gap-x-2 gap-y-1"+ >+ <Badge+ variant={gateStatus.variant}+ className="shrink-0 px-1.5 py-0 text-[10px]">- Always accept- </button>- </>+ {gateStatus.label}+ </Badge>+ <span className="min-w-0 flex-1 text-[11px] leading-snug text-muted-foreground">+ {gateStatus.line}+ {failure === "saved" && canvasHasEdits ? (+ <strong className="font-semibold text-foreground">+ {" "}+ The canvas has unsaved changes; Try again discards them.+ </strong>+ ) : null}+ </span>+ {/* In recover and reload this is the only live control on the card, so it is a+ button in its own right and sits outside the disabled action row. */}+ {onRetry ? (+ <button+ type="button"+ disabled={accepting}+ onClick={onRetry}+ className="shrink-0 rounded-md border border-border px-2 py-0.5 text-[11px] font-medium text-foreground hover:bg-slate-elevation4 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:opacity-60 dark:text-slate-200"+ >+ Try again+ </button>+ ) : null}+ </div>) : null}- <button- type="button"- onClick={onReject}- className="rounded-md px-3 py-1.5 text-xs text-red-700 hover:bg-red-500/10 hover:text-red-800 dark:text-red-300 dark:hover:text-red-400"+ {/* A second Accept while one is in flight loses the race server-side, and a+ proposal that could not be re-read may be stale, so the row stays locked. */}+ <fieldset+ disabled={+ accepting ||+ failure === "reload" ||+ failure === "recover" ||+ failure === "saved"+ }+ className="flex min-w-0 flex-wrap gap-2 disabled:opacity-60">- Reject- </button>- {onTestEndToEnd && !billingCreditRefusal ? (+ {billingCreditRefusal ? (+ <p className="basis-full text-[11px] leading-snug text-muted-foreground">+ No browser or run started because credits are exhausted.{" "}+ <a+ href="/billing"+ className="font-medium text-sky-700 underline underline-offset-2 dark:text-sky-300"+ >+ Go to Billing+ </a>+ .+ </p>+ ) : null}<buttontype="button"- onClick={onTestEndToEnd}+ onClick={onReview}className="rounded-md border border-border px-3 py-1.5 text-xs text-foreground hover:bg-slate-elevation4 dark:text-slate-200">- {isBuildTestConnectFailureState(turn?.turnFacts?.terminalCause)- ? "Retry in a fresh session"- : "Test end-to-end"}+ Review</button>- ) : null}- {onTestEndToEnd && !billingCreditRefusal ? (- <p className="basis-full text-[11px] leading-snug text-muted-foreground">- {verdict === "untested" &&- turn &&- everyTestBlockExecuted(turn) &&- !hasFailedTestBlock(turn)- ? END_TO_END_EXPLAINER- : END_TO_END_REAL_ACTIONS}- </p>- ) : null}+ {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}+ <button+ type="button"+ onClick={onReject}+ className="rounded-md px-3 py-1.5 text-xs text-red-700 hover:bg-red-500/10 hover:text-red-800 dark:text-red-300 dark:hover:text-red-400"+ >+ Reject+ </button>+ {onTestEndToEnd && !billingCreditRefusal ? (+ <button+ type="button"+ onClick={onTestEndToEnd}+ className="rounded-md border border-border px-3 py-1.5 text-xs text-foreground hover:bg-slate-elevation4 dark:text-slate-200"+ >+ {isBuildTestConnectFailureState(turn?.turnFacts?.terminalCause)+ ? "Retry in a fresh session"+ : "Test end-to-end"}+ </button>+ ) : null}+ {onTestEndToEnd && !billingCreditRefusal ? (+ <p className="basis-full text-[11px] leading-snug text-muted-foreground">+ {verdict === "untested" &&+ turn &&+ everyTestBlockExecuted(turn) &&+ !hasFailedTestBlock(turn)+ ? END_TO_END_EXPLAINER+ : END_TO_END_REAL_ACTIONS}+ </p>+ ) : null}+ </fieldset></div>) : null}{settled ? (@@ -325,7 +429,7 @@ export function ReviewGateCard({{accepted ? "✓" : "↺"}</span>{accepted- ? "Accepted — saved as a new workflow version"+ ? "Accepted — saved to the workflow": "Discarded — canvas reverted to the previous version"}</div>) : null}diff --git a/skyvern-frontend/src/routes/workflows/copilot/useHistoryLoad.ts b/skyvern-frontend/src/routes/workflows/copilot/useHistoryLoad.tsnew file mode 100644index 000000000..ab8ce4b92--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/copilot/useHistoryLoad.ts@@ -0,0 +1,38 @@+import { useCallback, useRef, useState } from "react";++/**+ * The chat-history loading flag, owned together with the sequence number that says WHICH load+ * owns it.+ *+ * The flag and the sequence are not separable: `beginHistoryLoad` is the only way to raise the flag+ * and the only source of a sequence number, so a claimant cannot take one without the other, and a+ * releaser that never began cannot type-check. A releaser that clears unconditionally lets a+ * superseded load switch the chat's actions back on while a newer load is still in flight.+ *+ * The comparison lives HERE and callers release unconditionally: a caller writing+ * `if (seq === current) endHistoryLoad(seq)` would put the same forgettable check in a new place.+ */+export function useHistoryLoad(): {+ isLoadingHistory: boolean;+ beginHistoryLoad: () => number;+ endHistoryLoad: (seq: number) => void;+} {+ const [isLoadingHistory, setIsLoadingHistory] = useState(false);+ const seqRef = useRef(0);++ const beginHistoryLoad = useCallback((): number => {+ seqRef.current += 1;+ setIsLoadingHistory(true);+ return seqRef.current;+ }, []);++ const endHistoryLoad = useCallback((seq: number): void => {+ // Only the load that still owns the flag may lower it.+ if (seqRef.current !== seq) {+ return;+ }+ setIsLoadingHistory(false);+ }, []);++ return { isLoadingHistory, beginHistoryLoad, endHistoryLoad };+}diff --git a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.tsindex 91d9e8530..34367267b 100644--- a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts@@ -199,6 +199,9 @@ export interface WorkflowCopilotChatHistoryResponse {chat_history: WorkflowCopilotChatHistoryMessage[];proposed_workflow?: WorkflowApiResponse | null;proposed_workflow_metadata?: CopilotProposalMetadata | null;+ // Seconds the server's accepting claim has left; null when no live claim holds the+ // proposal. A duration, so a skewed client clock still agrees with the server.+ proposed_claim_expires_in_seconds?: number | null;proposed_workflow_run?: CopilotProposalRunFacts | null;auto_accept?: boolean | null;work_plan?: string[];diff --git a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsxindex ddad1feac..d2586b5d7 100644--- a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx+++ b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx@@ -3233,6 +3233,10 @@ function Workspace({description: "Failed to apply agent update. Please try again.",variant: "destructive",});+ // The chat reads a clean return as "the editor now holds this workflow" and+ // releases the Accept fence on it. Swallowing this leaves the stale draft on+ // the canvas with the server already saved, and the next save duplicates it.+ throw error;}}}/>diff --git a/skyvern-frontend/src/routes/workflows/studio/CopilotPaneHeader.test.tsx b/skyvern-frontend/src/routes/workflows/studio/CopilotPaneHeader.test.tsxnew file mode 100644index 000000000..ea6c571cd--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/studio/CopilotPaneHeader.test.tsx@@ -0,0 +1,44 @@+import { render, screen } from "@testing-library/react";+import { describe, expect, it } from "vitest";++import { TooltipProvider } from "@/components/ui/tooltip";+import { useCopilotHeaderStore } from "@/store/useCopilotHeaderStore";++import { CopilotPaneControls } from "./CopilotPaneHeader";++const noop = () => {};++function registerControls(navigationLockedReason: string | null) {+ useCopilotHeaderStore.setState({+ controls: {+ workflowPermanentId: "wpid_1",+ currentChatId: "chat-1",+ onSelectChat: noop,+ onNewChat: noop,+ disabled: navigationLockedReason !== null,+ newChatDisabled: navigationLockedReason !== null,+ navigationLockedReason,+ },+ });+}++describe("CopilotPaneControls", () => {+ it("keeps a locked control's reason reachable instead of hanging it off the disabled button", () => {+ const reason = "Copilot couldn't confirm whether an Accept already saved.";+ registerControls(reason);+ render(+ <TooltipProvider>+ <CopilotPaneControls />+ </TooltipProvider>,+ );++ for (const name of [/^New chat/, /^History/]) {+ const button = screen.getByRole("button", { name });+ expect(button.matches(":disabled")).toBe(true);+ expect(button.getAttribute("aria-label")).toContain(reason);+ // A disabled button swallows the pointer and focus events its own tooltip+ // trigger needs, so the reason has to hang off a focusable wrapper.+ expect(button.closest("[tabindex='0']")).not.toBeNull();+ }+ });+});diff --git a/skyvern-frontend/src/routes/workflows/studio/CopilotPaneHeader.tsx b/skyvern-frontend/src/routes/workflows/studio/CopilotPaneHeader.tsxindex 8c817dfd3..56370e8c2 100644--- a/skyvern-frontend/src/routes/workflows/studio/CopilotPaneHeader.tsx+++ b/skyvern-frontend/src/routes/workflows/studio/CopilotPaneHeader.tsx@@ -1,14 +1,10 @@import { PlusIcon } from "@radix-ui/react-icons";-import {- Tooltip,- TooltipContent,- TooltipTrigger,-} from "@/components/ui/tooltip";import { useCopilotHeaderStore } from "@/store/useCopilotHeaderStore";import { WorkflowCopilotHistory } from "../copilot/WorkflowCopilotHistory";import { PANE_HEADER_ICON_BUTTON_CLASS } from "./constants";+import { ControlTooltip } from "./ControlTooltip";/*** Copilot pane header chrome: the History and New-chat controls the docked@@ -29,21 +25,37 @@ export function CopilotPaneControls() {currentChatId={controls.currentChatId}onSelect={controls.onSelectChat}disabled={controls.disabled}+ lockedReason={controls.navigationLockedReason ?? undefined}compact/>- <Tooltip>- <TooltipTrigger asChild>- <button- type="button"- onClick={controls.onNewChat}- aria-label="New chat"- className={PANE_HEADER_ICON_BUTTON_CLASS}- >- <PlusIcon className="h-3.5 w-3.5" aria-hidden="true" />- </button>- </TooltipTrigger>- <TooltipContent side="bottom">New chat</TooltipContent>- </Tooltip>+ {/* Disabled buttons swallow the trigger's events, so the reason a locked control gives+ has to hang off a focusable wrapper or a keyboard user never reaches it. */}+ <ControlTooltip+ content={+ controls.navigationLockedReason ? (+ <span className="block max-w-xs">+ {controls.navigationLockedReason}+ </span>+ ) : (+ "New chat"+ )+ }+ blocked={controls.newChatDisabled}+ >+ <button+ type="button"+ onClick={controls.onNewChat}+ disabled={controls.newChatDisabled}+ aria-label={+ controls.navigationLockedReason+ ? `New chat unavailable: ${controls.navigationLockedReason}`+ : "New chat"+ }+ className={PANE_HEADER_ICON_BUTTON_CLASS}+ >+ <PlusIcon className="h-3.5 w-3.5" aria-hidden="true" />+ </button>+ </ControlTooltip></>);}diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.tsxindex 70fd493e3..e04d726c1 100644--- a/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.tsx+++ b/skyvern-frontend/src/routes/workflows/studio/StudioTopBar.tsx@@ -116,6 +116,9 @@ export function TitleSection({ editable = true }: { editable?: boolean }) {export function SaveButton() {const saving = useWorkflowHasChangesStore((s) => s.saveIsPending);+ const saveBlockedReason = useWorkflowHasChangesStore(+ (s) => s.saveBlockedReason,+ );const getSaveData = useWorkflowHasChangesStore((s) => s.getSaveData);// contentDirty reflects real user edits vs the clean baseline snapshot, so// post-load canvas materialization (login autofill) doesn't light the dot.@@ -144,12 +147,21 @@ export function SaveButton() {return (<>- <ControlTooltip content="Save workflow" blocked={isRecording}>+ <ControlTooltip+ content={+ saveBlockedReason ? (+ <span className="block max-w-xs">{saveBlockedReason}</span>+ ) : (+ "Save workflow"+ )+ }+ blocked={isRecording || saveBlockedReason !== null}+ ><Buttonvariant="ghost"size="icon"className="relative h-8 w-8 text-muted-foreground"- disabled={isRecording}+ disabled={isRecording || saveBlockedReason !== null}onClick={() => {// Recompute dirtiness synchronously from the same source as the// summary (incl. the YAML draft). contentDirty is debounced and@@ -171,7 +183,11 @@ export function SaveButton() {}}}aria-label={- contentDirty ? "Save workflow (unsaved changes)" : "Save workflow"+ saveBlockedReason+ ? `Save workflow unavailable: ${saveBlockedReason}`+ : contentDirty+ ? "Save workflow (unsaved changes)"+ : "Save workflow"}>{saving ? (diff --git a/skyvern-frontend/src/routes/workflows/workflowBlockUtils.test.ts b/skyvern-frontend/src/routes/workflows/workflowBlockUtils.test.tsindex 73140ecfa..fb9fa9851 100644--- a/skyvern-frontend/src/routes/workflows/workflowBlockUtils.test.ts+++ b/skyvern-frontend/src/routes/workflows/workflowBlockUtils.test.ts@@ -4,11 +4,12 @@ import {buildCodeStepsByLabel,describeRecordedAction,findCodeStepForLine,+ getActionSummary,getCodeStepPlainText,taskV3CallText,visitWorkflowBlocks,} from "./workflowBlockUtils";-import type { ActionsApiResponse } from "@/api/types";+import { ActionTypes, type ActionsApiResponse } from "@/api/types";import type {CodeBlock,CodeBlockStep,@@ -364,3 +365,110 @@ describe("taskV3CallText", () => {expect(taskV3CallText(null)).toBeNull();});});++describe("getActionSummary", () => {+ it("falls back to the intention when the model left no reasoning", () => {+ expect(+ getActionSummary({+ action_type: ActionTypes.GotoUrl,+ reasoning: null,+ intention: "Navigated to https://example.com/get-in-touch/",+ response: null,+ text: null,+ }),+ ).toEqual({+ body: {+ text: "Navigated to https://example.com/get-in-touch/",+ isProse: true,+ },+ outcome: null,+ });+ });++ // The dead-end navigation is why the outcome is not a fallback: the intention says where the+ // agent meant to go, and only the response says the page was a 404.+ it("keeps the recorded outcome beside an intention instead of behind it", () => {+ expect(+ getActionSummary({+ action_type: ActionTypes.GotoUrl,+ reasoning: null,+ intention: "Tried to navigate to https://example.com/contact-us/",+ response: "https://example.com/contact-us/ (HTTP 404, dead end)",+ text: null,+ }),+ ).toEqual({+ body: {+ text: "Tried to navigate to https://example.com/contact-us/",+ isProse: true,+ },+ outcome: "https://example.com/contact-us/ (HTTP 404, dead end)",+ });+ });++ it("keeps the recorded outcome beside the model's own reasoning", () => {+ expect(+ getActionSummary({+ action_type: ActionTypes.GotoUrl,+ reasoning: "**Following the footer link** to the contact page",+ intention: "Tried to navigate to https://example.com/contact-us/",+ response: "https://example.com/contact-us/ (HTTP 404, dead end)",+ }),+ ).toEqual({+ body: {+ text: "**Following the footer link** to the contact page",+ isProse: true,+ },+ outcome: "https://example.com/contact-us/ (HTTP 404, dead end)",+ });+ });++ // goto_url is the case that made this chain necessary: url is subclass-only and never reaches+ // the client, so a navigation row with no intention holds its destination only in response.+ it("carries the response alone, as literal text, when that is all a row has", () => {+ expect(+ getActionSummary({+ action_type: ActionTypes.GotoUrl,+ reasoning: " ",+ intention: null,+ response: "https://example.com/contact-us/ (HTTP 404, dead end)",+ }),+ ).toEqual({+ body: null,+ outcome: "https://example.com/contact-us/ (HTTP 404, dead end)",+ });+ });++ // A cached run writes the answer it typed to both text and response, and the card already prints+ // it on its Input line.+ it("does not report a typed value back as an outcome", () => {+ expect(+ getActionSummary({+ action_type: ActionTypes.InputText,+ reasoning: null,+ intention: "Enter your zip code",+ response: "90210",+ text: "90210",+ }),+ ).toEqual({+ body: { text: "Enter your zip code", isProse: true },+ outcome: null,+ });+ });++ // Paragraph breaks survive for a card that renders blocks; collapsing them here would flatten a+ // multi-paragraph provider summary into one run-on line.+ it("keeps the prose body as written", () => {+ expect(+ getActionSummary({+ reasoning: "**Investigating the iframe**\n\nThen reading the table",+ })?.body,+ ).toEqual({+ text: "**Investigating the iframe**\n\nThen reading the table",+ isProse: true,+ });+ });++ it("returns null when a row carries no text at all, so the type pill is not doubled", () => {+ expect(getActionSummary({ reasoning: "", text: null })).toBeNull();+ });+});diff --git a/skyvern-frontend/src/routes/workflows/workflowBlockUtils.ts b/skyvern-frontend/src/routes/workflows/workflowBlockUtils.tsindex ab23e2672..3ec21b111 100644--- a/skyvern-frontend/src/routes/workflows/workflowBlockUtils.ts+++ b/skyvern-frontend/src/routes/workflows/workflowBlockUtils.ts@@ -2,6 +2,8 @@ import {ActionTypes,getReadableActionType,type ActionsApiResponse,+ type ActionSummary,+ type ActionSummaryBody,} from "@/api/types";import {@@ -179,6 +181,84 @@ export function taskV3CallText(return normalizeInlineText(text.slice(TASK_V3_CALL_PREFIX.length));}+export type ActionSummarySource = Partial<+ Pick<+ ActionsApiResponse,+ "action_type" | "reasoning" | "intention" | "response" | "text"+ >+>;++export function getActionInputValue(+ action: ActionSummarySource,+): string | null {+ // Script-generated input text lives in response, not text.+ if (action.action_type === ActionTypes.InputText) {+ return action.text ?? action.response ?? null;+ }+ return action.text ?? null;+}++/**+ * What the action actually did, when the run recorded it: a navigation's landing and HTTP status, a+ * recorded call's return value, the exception that ended a code block.+ *+ * `response` doubles as the stored input on input-text actions — a cached run writes the same answer+ * to both — so it is not echoed as an outcome there. Every other action type legitimately records+ * its result in `response` even when that equals `text`.+ */+export function getActionOutcome(action: ActionSummarySource): string | null {+ if (+ action.action_type === ActionTypes.InputText &&+ typeof action.response === "string" &&+ action.response === getActionInputValue(action)+ ) {+ return null;+ }+ return action.response ?? null;+}++/**+ * The reader-facing text for one action: what it meant to do, and what came of it.+ *+ * The body is the most specific account of the intent the row carries — the model's own reasoning,+ * else the deterministic intention the agent recorded, else the value it typed. A Task V3 turn+ * often emits no prose at all, and only `intention` and `response` carry a navigation's URL:+ * `GotoUrlAction.url` is subclass-only and never reaches the client.+ *+ * A recorded outcome is returned alongside that body rather than behind it, so a card never shows+ * the plan in place of the effect — an action whose intention reads "Tried to navigate to X" and+ * whose response reads "HTTP 404, dead end" must not render as the first alone.+ *+ * Returns null when the action carries neither, so a caller that already shows the action type does+ * not print it twice.+ */+export function getActionSummary(+ action: ActionSummarySource,+): ActionSummary | null {+ const candidates: Array<[string | null | undefined, boolean]> = [+ [action.reasoning, true],+ [action.intention, true],+ [action.text, false],+ ];+ let body: ActionSummaryBody | null = null;+ for (const [value, isProse] of candidates) {+ const text = value?.trim();+ if (text) {+ body = { text, isProse };+ break;+ }+ }+ const recorded = normalizeInlineText(getActionOutcome(action));+ const outcome =+ recorded !== null && recorded !== normalizeInlineText(body?.text)+ ? recorded+ : null;+ if (body === null && outcome === null) {+ return null;+ }+ return { body, outcome };+}+/*** Reader-facing text for one recorded action, in descending order of specificity: the definition* step it fired from, whatever prose the action carries, the author's own prompt, then the onediff --git a/skyvern-frontend/src/routes/workflows/workflowRun/ActionCard.tsx b/skyvern-frontend/src/routes/workflows/workflowRun/ActionCard.tsxindex 361ad7a77..f71479d86 100644--- a/skyvern-frontend/src/routes/workflows/workflowRun/ActionCard.tsx+++ b/skyvern-frontend/src/routes/workflows/workflowRun/ActionCard.tsx@@ -1,5 +1,9 @@import { ActionsApiResponse, ActionTypes } from "@/api/types";import { getActionDisplayKind } from "@/routes/workflows/components/actionStatus";+import {+ getActionInputValue,+ getActionSummary,+} from "@/routes/workflows/workflowBlockUtils";import { TerminatedIcon, terminatedTone } from "@/components/terminatedVisual";import { StatusPill } from "@/components/ui/status-pill";import {@@ -15,6 +19,7 @@ import {CrossCircledIcon,LightningBoltIcon,} from "@radix-ui/react-icons";+import { BlockMarkdown } from "@/components/AgentMarkdown";import { RunCard } from "./RunCard";type Props = {@@ -27,6 +32,7 @@ type Props = {function ActionCard({ action, onClick, active, index, cardClassName }: Props) {const kind = getActionDisplayKind(action);+ const summary = getActionSummary(action);return (<RunCard@@ -75,17 +81,28 @@ function ActionCard({ action, onClick, active, index, cardClassName }: Props) {)}</div></div>- <div className="break-words text-xs text-neutral-600 dark:text-slate-400">- {action.reasoning}- </div>+ {summary?.body && (+ <div className="break-words text-xs text-neutral-600 dark:text-slate-400">+ {summary.body.isProse ? (+ <BlockMarkdown text={summary.body.text} />+ ) : (+ summary.body.text+ )}+ </div>+ )}+ {summary?.outcome && (+ <div className="break-words text-xs text-neutral-600 dark:text-slate-400">+ <span className="text-neutral-500 dark:text-slate-500">+ Outcome:{" "}+ </span>+ {summary.outcome}+ </div>+ )}{action.action_type === ActionTypes.InputText && (<><Separator /><div className="text-xs text-neutral-600 dark:text-slate-400">- Input:{" "}- {action.action_type === "input_text"- ? (action.text ?? action.response)- : action.response}+ Input: {getActionInputValue(action)}</div></>)}diff --git a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.test.tsx b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.test.tsxindex f449812ff..e55ca16d4 100644--- a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.test.tsx+++ b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.test.tsx@@ -1587,6 +1587,42 @@ describe("WorkflowRunTimelineBlockItem", () => {expect(screen.getByText(/plan is active/)).toBeDefined();});+ // Task V3 persists its own navigations as goto_url rows whose url never reaches the client: the+ // intention says where it meant to go and only the response says the page was a dead end.+ it("shows a recorded outcome alongside the intention it followed", () => {+ const block = buildBlock({+ workflow_run_block_id: "wrb_dead_end",+ block_type: "task",+ label: "contact",+ actions: [+ {+ action_id: "act_dead_end",+ action_type: ActionTypes.GotoUrl,+ status: Status.Failed,+ reasoning: null,+ text: null,+ intention: "Tried to navigate to https://example.com/contact-us/",+ response: "https://example.com/contact-us/ (HTTP 404, dead end)",+ created_by: null,+ confidence_float: null,+ },+ ] as unknown as WorkflowRunBlock["actions"],+ });++ render(+ <WorkflowRunTimelineBlockItem+ activeItem={null}+ block={block}+ subItems={[]}+ onActionClick={noop}+ onBlockItemClick={noop}+ />,+ );++ expect(screen.getByText(/Tried to navigate to/)).toBeDefined();+ expect(screen.getByText(/HTTP 404, dead end/)).toBeDefined();+ });+// A Task V3 turn that emits only tool calls persists every action of that round with no prose at// all, which used to leave the row as a bare icon and index.it("falls back to a visible action type when an action carries no prose", () => {diff --git a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.tsx b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.tsxindex 5aa1244b4..3c02570ea 100644--- a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.tsx+++ b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.tsx@@ -10,6 +10,7 @@ import { type ReactNode, useEffect, useMemo, useRef, useState } from "react";import {type ActionsApiResponse,+ type ActionSummary,ActionTypes,getReadableActionType,Status,@@ -38,6 +39,7 @@ import { type CodeBlockStep, WorkflowBlockTypes } from "../types/workflowTypes";import {describeRecordedAction,findCodeStepForLine,+ getActionSummary,isRecorderCallText,normalizeInlineText,taskV3CallText,@@ -194,29 +196,6 @@ function StatusDot({);}-type ActionSummary = {- text: string;- // Only the model's own prose is markdown. A typed value or a recorder trace is literal text and- // must render verbatim — a password containing "*" is not emphasis.- isProse: boolean;-};--function getActionSummary(action: ActionsApiResponse): ActionSummary | null {- const candidates: Array<[string | null | undefined, boolean]> = [- [action.reasoning, true],- [action.text, false],- [action.response, false],- [action.intention, true],- ];- for (const [value, isProse] of candidates) {- const text = normalizeInlineText(value);- if (text !== null) {- return { text, isProse };- }- }- return null;-}-function getRecordedActionMeta(action: ActionsApiResponse): {codeLine: number | null;durationMs: number | null;@@ -274,13 +253,23 @@ function getCodeActionRowPresentation(timelineActionIcons[action.action_type]);const { codeLine, durationMs } = getRecordedActionMeta(action);+ // An error row prints both halves of its summary: the exception that ended the block is+ // recorded as the outcome, not the body.+ const errorSummary = isCodeError ? getActionSummary(action) : null;const parts = [- // This row hides its label from sighted users, so it falls back to the readable type- // where the chat, which always shows the label, prints nothing.- isCodeError- ? (getActionSummary(action)?.text ?? null)- : (describeRecordedAction(action, matchedStep) ??- getReadableActionType(action.action_type, { nullActionLabel: "Step" })),+ // Every other row hides its label from sighted users, so it falls back to the readable+ // type where the chat, which always shows the label, prints nothing.+ ...(isCodeError+ ? [+ normalizeInlineText(errorSummary?.body?.text),+ errorSummary?.outcome ?? null,+ ]+ : [+ describeRecordedAction(action, matchedStep) ??+ getReadableActionType(action.action_type, {+ nullActionLabel: "Step",+ }),+ ]),codeLine !== null ? `line ${codeLine}` : null,durationMs !== null ? formatActionDurationMs(durationMs) : null,].filter((part): part is string => part !== null);@@ -290,7 +279,9 @@ function getCodeActionRowPresentation(// Literal even when the leading part came from prose: the row's text is that part joined with// machine suffixes (line N, duration), and half-markdown-half-not would render as neither.summary:- parts.length > 0 ? { text: parts.join(" · "), isProse: false } : null,+ parts.length > 0+ ? { body: { text: parts.join(" · "), isProse: false }, outcome: null }+ : null,detail:!isCodeError && isRecorderCallText(action.description)? normalizeInlineText(action.description)@@ -637,25 +628,29 @@ function TimelineActionRows({) : (<span className="sr-only">{label}</span>)}- {summary !== null ? (- <span className="min-w-0 flex-1 truncate text-muted-foreground dark:text-slate-500">+ {summary?.body ? (+ <span className="min-w-0 truncate text-muted-foreground dark:text-slate-500">·{" "}- {summary.isProse ? (+ {summary.body.isProse ? (<InlineMarkdown- text={summary.text}+ // One line: InlineMarkdown drops every paragraph break, so prose that+ // kept its own would render as glued-together words.+ text={normalizeInlineText(summary.body.text) ?? ""}// Prose that renders to nothing must not blank the row; the sr-only// label above already names it, so this copy is decorative.fallback={<span aria-hidden="true">{label}</span>}/>) : (- summary.text+ summary.body.text)}</span>) : (// A Task V3 turn that emitted only tool calls leaves every action of that round// with no prose, which used to render as a bare icon and index. Show the type// label sighted readers were missing; it duplicates the sr-only label above, so- // assistive tech must not read it twice. Error rows already show theirs.+ // assistive tech must not read it twice. Error rows already show theirs, and a+ // row that recorded an outcome says something better below.+ !summary?.outcome &&tone !== "error" && (<spanaria-hidden="true"@@ -665,6 +660,19 @@ function TimelineActionRows({</span>))}+ {summary?.outcome && (+ // Never squeezed out by the plan: a row whose intention reads "Tried to+ // navigate to X" must still show the 404 it landed on, so the body truncates+ // first and this keeps its width.+ <span+ className={cn(+ "truncate text-muted-foreground dark:text-slate-500",+ summary.body ? "max-w-[60%] shrink-0" : "min-w-0 flex-1",+ )}+ >+ · Outcome: {summary.outcome}+ </span>+ )}</button></div></div>diff --git a/skyvern-frontend/src/routes/workflows/workflowRun/blockDetail/BlockInspector.tsx b/skyvern-frontend/src/routes/workflows/workflowRun/blockDetail/BlockInspector.tsxindex eaa1f7692..0766ac6c9 100644--- a/skyvern-frontend/src/routes/workflows/workflowRun/blockDetail/BlockInspector.tsx+++ b/skyvern-frontend/src/routes/workflows/workflowRun/blockDetail/BlockInspector.tsx@@ -7,12 +7,10 @@ import {import { useEffect, useMemo, useState } from "react";import { Link } from "react-router-dom";+import { getReadableActionType, type ActionsApiResponse } from "@/api/types";import {- ActionTypes,- getReadableActionType,- type ActionsApiResponse,-} from "@/api/types";-import {+ getActionInputValue,+ getActionOutcome,isRecorderCallText,taskV3CallText,} from "@/routes/workflows/workflowBlockUtils";@@ -443,14 +441,6 @@ function getSummaryFields(block: WorkflowRunBlock): Array<InspectorField> {return fields;}-function getActionInputValue(action: ActionsApiResponse): string | null {- // Script-generated input text lives in response, not text.- if (action.action_type === ActionTypes.InputText) {- return action.text ?? action.response;- }- return action.text;-}-function getActionSummaryFields(action: ActionsApiResponse,): Array<InspectorField> {@@ -474,20 +464,6 @@ function getActionSummaryFields(return fields;}-function getActionOutputValue(action: ActionsApiResponse): unknown {- // response doubles as stored input only for input-text actions — don't echo- // it back as output there. Other action types legitimately carry their- // result in response even when it equals text, so never suppress for them.- if (- action.action_type === ActionTypes.InputText &&- typeof action.response === "string" &&- action.response === getActionInputValue(action)- ) {- return null;- }- return action.response;-}-function isSameStepInstance(previousAction: ActionsApiResponse | null,action: ActionsApiResponse,@@ -596,9 +572,7 @@ function BlockInspector({);const showsExtractedInformation =!action && shouldShowExtractedInformation(block);- const outputValue = action- ? getActionOutputValue(action)- : getOutputValue(block);+ const outputValue = action ? getActionOutcome(action) : getOutputValue(block);const hasOutput = showsExtractedInformation || !isEmptyValue(outputValue);const outputRootLabel = showsExtractedInformation? "extracted_information"diff --git a/skyvern-frontend/src/store/WorkflowHasChangesStore.recording.test.tsx b/skyvern-frontend/src/store/WorkflowHasChangesStore.recording.test.tsxindex dcebc2cb1..01ca1f903 100644--- a/skyvern-frontend/src/store/WorkflowHasChangesStore.recording.test.tsx+++ b/skyvern-frontend/src/store/WorkflowHasChangesStore.recording.test.tsx@@ -179,3 +179,25 @@ describe("workflow recording attachment", () => {});});});++describe("blocked saves", () => {+ it("refuses to write the workflow and keeps it dirty while a save is blocked", async () => {+ mocks.getClient.mockResolvedValue({ delete: mocks.delete, put: mocks.put });+ useWorkflowHasChangesStore.setState({+ getSaveData: () => saveData,+ hasChanges: true,+ pendingRecordingId: null,+ pendingRecordingWorkflowPermanentId: null,+ saveBlockedReason: "An Accept's outcome is still unconfirmed.",+ });+ const { result } = renderHook(() => useWorkflowSave(), { wrapper });++ await act(async () => {+ await expect(result.current.mutateAsync(undefined)).rejects.toBeTruthy();+ });++ expect(mocks.put).not.toHaveBeenCalled();+ expect(useWorkflowHasChangesStore.getState().hasChanges).toBe(true);+ useWorkflowHasChangesStore.setState({ saveBlockedReason: null });+ });+});diff --git a/skyvern-frontend/src/store/WorkflowHasChangesStore.ts b/skyvern-frontend/src/store/WorkflowHasChangesStore.tsindex b077ff0b1..302ae2370 100644--- a/skyvern-frontend/src/store/WorkflowHasChangesStore.ts+++ b/skyvern-frontend/src/store/WorkflowHasChangesStore.ts@@ -31,6 +31,8 @@ type WorkflowHasChangesStore = {getSaveData: () => SaveData | null;hasChanges: boolean;saveIsPending: boolean;+ // Why workflow saves are held (an Accept whose server outcome is unconfirmed), or null.+ saveBlockedReason: string | null;saidOkToCodeCacheDeletion: boolean;showConfirmCodeCacheDeletion: boolean;pendingRecordingId: string | null;@@ -41,6 +43,7 @@ type WorkflowHasChangesStore = {setGetSaveData: (getSaveData: () => SaveData) => void;setHasChanges: (hasChanges: boolean) => void;setSaveIsPending: (isPending: boolean) => void;+ setSaveBlockedReason: (reason: string | null) => void;setSaidOkToCodeCacheDeletion: (saidOkToCodeCacheDeletion: boolean) => void;setShowConfirmCodeCacheDeletion: (show: boolean) => void;setPendingRecording: (@@ -66,6 +69,7 @@ const useWorkflowHasChangesStore = create<WorkflowHasChangesStore>((set) => {return {hasChanges: false,saveIsPending: false,+ saveBlockedReason: null,saidOkToCodeCacheDeletion: false,showConfirmCodeCacheDeletion: false,pendingRecordingId: null,@@ -98,6 +102,9 @@ const useWorkflowHasChangesStore = create<WorkflowHasChangesStore>((set) => {setSaveIsPending: (isPending: boolean) => {set({ saveIsPending: isPending });},+ setSaveBlockedReason: (reason: string | null) => {+ set({ saveBlockedReason: reason });+ },setSaidOkToCodeCacheDeletion: (saidOkToCodeCacheDeletion: boolean) => {set({ saidOkToCodeCacheDeletion });},@@ -165,6 +172,18 @@ const useWorkflowSave = (opts?: WorkflowSaveOpts) => {const saveWorkflowMutation = useMutation({mutationFn: async (override?: Partial<SaveData>) => {+ // Every persist path funnels here (top bar, Cmd+S, YAML mode, nav blocker), so the+ // hold is enforced once: a write now could duplicate or overwrite a saved Accept.+ const blockedReason =+ useWorkflowHasChangesStore.getState().saveBlockedReason;+ if (blockedReason) {+ toast({+ title: "Save is paused",+ description: blockedReason,+ variant: "destructive",+ });+ throw new WorkflowSaveValidationError();+ }const base = getSaveData();if (!base) {diff --git a/skyvern-frontend/src/store/useCopilotHeaderStore.ts b/skyvern-frontend/src/store/useCopilotHeaderStore.tsindex 1e13832e3..19d52dc6d 100644--- a/skyvern-frontend/src/store/useCopilotHeaderStore.ts+++ b/skyvern-frontend/src/store/useCopilotHeaderStore.ts@@ -8,6 +8,9 @@ export type CopilotHeaderControls = {onSelectChat: (chat: WorkflowCopilotChatSummary) => void;onNewChat: () => void;disabled: boolean;+ newChatDisabled: boolean;+ // Why chat navigation is locked (an Accept whose outcome is unresolved), or null.+ navigationLockedReason: string | null;};/**diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.pyindex b9527989b..f8c630b60 100644--- a/skyvern/forge/agent.py+++ b/skyvern/forge/agent.py@@ -395,11 +395,26 @@ _TASKV3_TOOL_ACTION_TYPES = {}+def _taskv3_label_secret_values(task: Task, secret_values: Collection[str] = ()) -> set[str]:+ """Drop-only: the set that decides whether a page-supplied element name is dropped from text that+ leaves the run. Unfloored and blind to the mask-secrets opt-in on purpose -- it reaches no redaction+ path, and a matching name is dropped whole rather than scrubbed, so a 3-char PIN must still match.+ Both frames that print a page-supplied name (the timeline label below, the loop's action-loop+ verdict) check against this one set, so a label dropped from the row cannot survive in the verdict."""+ return (+ set(secret_values)+ | app.WORKFLOW_CONTEXT_MANAGER.get_secret_values_for_run(+ task.workflow_run_id, respect_artifact_redaction_flag=False+ )+ | app.WORKFLOW_CONTEXT_MANAGER.secret_values_for_drop_check(task.workflow_run_id)+ )++def _taskv3_row_intention(task: Task, round_action: RoundAction, secret_values: set[str], redacted_args: dict[str, Any]) -> str | None:- """The persisted label for one v3 action. This is the only frame that holds the unfloored drop-check- secrets, and a failure here is logged without exc_info, so no traceback renderer can print them."""+ """The persisted label for one v3 action. This frame holds the unfloored drop-check secrets, and a+ failure here is logged without exc_info, so no traceback renderer can print them."""if round_action.tool == "navigate":# A navigation names no element, so the element-label composer has nothing to say about it:# the URL is the whole subject. Read from the already-redacted args, never the raw ones.@@ -408,19 +423,11 @@ def _taskv3_row_intention(return Nonereturn f"Navigated to {url}" if round_action.succeeded else f"Tried to navigate to {url}"try:- # Drop-only: this set decides whether to drop a page-supplied name and reaches no redaction path.- label_secret_values = (- secret_values- | app.WORKFLOW_CONTEXT_MANAGER.get_secret_values_for_run(- task.workflow_run_id, respect_artifact_redaction_flag=False- )- | app.WORKFLOW_CONTEXT_MANAGER.secret_values_for_drop_check(task.workflow_run_id)- )return compose_target_intention(round_action.tool,round_action.target_name,round_action.target_kind,- label_secret_values,+ _taskv3_label_secret_values(task, secret_values),succeeded=round_action.succeeded,)except Exception:@@ -1774,12 +1781,18 @@ class ForgeAgent:close_browser_on_completion: bool,browser_session_id: str | None,task_block: BaseTaskBlock | None = None,+ workflow_owned_recovery: bool = False,) -> tuple[Step, Task]:"""Run a whole task via the native Task V3 tool-loop (one persistent conversation).The loop owns the page for the entire task and returns a single outcome, which wemap onto the task's terminal state here. Runs exactly once: the caller returnsnext_step=None for this engine, so neither retry nor execute-all-steps recursion fires.++ `workflow_owned_recovery` marks a run a workflow drives on its own live browser without a+ block of its own (the code block AI fallback). It gets the block treatment — the caller's+ step budget, the workflow-run ceiling, per-call page re-resolution and child-frame reach —+ because it must not outspend or under-reach the v1 run it replaces."""from skyvern.forge.sdk.api.files import get_download_dir, resolve_run_download_idfrom skyvern.forge.sdk.api.llm.schema_validator import (@@ -1803,6 +1816,8 @@ class ForgeAgent:)from skyvern.forge.taskv3.handoff_redaction import (MAX_PERSISTED_FINISH_REASON_CHARS,+ caller_authored_block_urls,+ caller_known_published_urls,mask_signed_urls_in_text,sanitize_handoff_url,)@@ -1815,7 +1830,10 @@ class ForgeAgent:# Workflow-block tasks re-resolve the live working page on every tool call, so a click that# opens a new tab/popup is followed (mirrors the step engine's get_working_page re-fetch).# Bare tasks keep today's exact semantics: one page grabbed up front, for the run's duration.- if task_block is not None:+ # A block reuses the browser across blocks, so neither the landing status nor its URL belongs+ # to this block's start; only a bare task names a starting page in the dead-end verdict.+ initial_navigation_url: str | None = None+ if task_block is not None or workflow_owned_recovery:# Fail fast (with the same recovery attempt bare tasks get) when the page is already# gone at block start; mid-run losses surface through the per-call provider instead.await browser_state.must_get_working_page()@@ -1833,6 +1851,11 @@ class ForgeAgent:else:initial_page = await browser_state.must_get_working_page()+ # The pair setup recorded, not the page's URL now: the status belongs to the landed response+ # (page.goto returns the last redirect hop), and the settle and challenge-solver waits that+ # follow it give a client-side redirect time to move the page somewhere the status was never+ # about. Reading the page here would name that destination as the page that 404ed.+ initial_navigation_url = browser_state.last_navigation_urlasync def _page_provider() -> Any:return initial_page@@ -1846,16 +1869,18 @@ class ForgeAgent:llm_caller = LLMCaller(llm_key=await _resolve_task_v3_llm_key(task))parameters = coerce_v3_parameters(task.navigation_payload)context = skyvern_context.current()+ pinned_frame_arm: tuple[bool, str | None] | None = Noneif context:# Once for the whole run, before anything reads it: the tool list bakes one of these# reads into the observe description at build time, and a value that could change# afterwards would leave that description describing a different run than the one# executing.- await resolve_frame_perception(- context,- distinct_id=task.workflow_run_id or task.task_id,- organization_id=task.organization_id,- )+ if not workflow_owned_recovery:+ await resolve_frame_perception(+ context,+ distinct_id=task.workflow_run_id or task.task_id,+ organization_id=task.organization_id,+ )await resolve_run_arm(context,OBSERVE_DROP_OFFVIEWPORT_UNNAMED_FLAG,@@ -1960,6 +1985,27 @@ class ForgeAgent:),)+ # A BARE task's `url`/`navigation_goal` are the caller's own typed text -- not the composed+ # `goal` above, which also carries a prior block's handoff prose. Inside a workflow run neither+ # field is caller-typed any more: both are Jinja-rendered from a context holding prior blocks'+ # page-derived output, so the sources are the block's unrendered definition strings, read at the+ # block seam before that render. Nothing pinned for this task -> host only, never the rendered+ # fields (SKY-16271).+ verdict_known_urls = (+ caller_known_published_urls(task.url, task.navigation_goal)+ if task_block is None and not task.workflow_run_id+ else caller_authored_block_urls(+ context,+ workflow_run_id=task.workflow_run_id,+ block_label=task_block.label if task_block is not None else None,+ )+ )++ def _label_secret_values() -> Collection[str]:+ # Read per verdict, not once here: a run resolves credentials and codes as it goes, and a+ # verdict composed late must check the page's name against the registry as it stands then.+ return _taskv3_label_secret_values(task)+async def _should_cancel() -> bool:refreshed = await app.DATABASE.tasks.get_task(task_id=task.task_id, organization_id=organization.organization_id@@ -2131,9 +2177,12 @@ class ForgeAgent:# step-engine steps, which starves the less round-efficient v3 loop. Either signal alone marks# the budget as owned — the block class settles it when task_type was left at its `general`# default, which would otherwise floor an action block to many times its intended rounds.- atomic_block_budget = task_block is not None and (- isinstance(task_block, (ActionBlock, ValidationBlock))- or (task.task_type or TaskType.general) != TaskType.general+ atomic_block_budget = workflow_owned_recovery or (+ task_block is not None+ and (+ isinstance(task_block, (ActionBlock, ValidationBlock))+ or (task.task_type or TaskType.general) != TaskType.general+ ))if not atomic_block_budget:floored_step_cap = max(step_cap, MIN_ACTION_STEPS)@@ -2149,7 +2198,7 @@ class ForgeAgent:)step_cap = floored_step_capworkflow_step_ceiling: int | None = None- if task_block is not None:+ if task_block is not None or workflow_owned_recovery:# The org's workflow-run-wide step ceiling binds v3 blocks too: an action round is the# budget unit (round-stamped action rows make prior v3 rounds count exactly).workflow_run_budget = await self._check_workflow_run_step_budget(organization, task)@@ -2425,6 +2474,14 @@ class ForgeAgent:prev_active_credential_parameter_key = context.active_credential_parameter_key if context else Noneif context and credential_parameter_key is not None:context.active_credential_parameter_key = credential_parameter_key+ if context and workflow_owned_recovery:+ # The v1 run this replaces scraped child frames unconditionally, and the failing call it+ # recovers may target one, so the arm is pinned on rather than sampled. Pinned here, with+ # the credential key, so the finally below frees it on every exit; the only reader before+ # the loop is a closure that runs inside it.+ pinned_frame_arm = (context.frame_perception_flag, context.frame_perception_resolved_run_id)+ context.frame_perception_flag = True+ context.frame_perception_resolved_run_id = task.task_idtry:# Built AFTER the credential pin: the tool-offer gate (has_credential_totp_candidate)# must see the pinned key, or a multi-credential context hides get_verification_code.@@ -2479,7 +2536,9 @@ class ForgeAgent:# The 2026-08-18 scoping decision kept the completed-side settle probe off the# bare-task arm. Fenced by its own deferral budget so it does not also disable# the failure-evidence gate that shares the sampler (SKY-14598).- max_settle_deferrals=DEFAULT_MAX_SETTLE_DEFERRALS if task_block is not None else 0,+ max_settle_deferrals=(+ DEFAULT_MAX_SETTLE_DEFERRALS if task_block is not None or workflow_owned_recovery else 0+ ),llm_caller=llm_caller,goal=goal,parameters=parameters,@@ -2514,11 +2573,18 @@ class ForgeAgent:# status unambiguously belongs to the starting posting. A workflow block reuses the# browser_state across blocks, so its last status may be a prior block's — skip it there# and let the in-loop `navigate` classifier cover block-driven dead-ends.- initial_navigation_status=(browser_state.last_navigation_status if task_block is None else None),+ initial_navigation_status=(+ browser_state.last_navigation_status if task_block is None and not workflow_owned_recovery else None+ ),+ initial_navigation_url=initial_navigation_url,+ caller_known_urls=verdict_known_urls,+ label_secret_values=_label_secret_values,)finally:if context and credential_parameter_key is not None:context.active_credential_parameter_key = prev_active_credential_parameter_key+ if context and pinned_frame_arm is not None:+ context.frame_perception_flag, context.frame_perception_resolved_run_id = pinned_frame_arm# Frames are already in memory, so a loop that raised or ran out of budget still# persists what it captured; bounded so the unwind of a cancelled run is not held up.with contained_effect("task_v3 pre-submit frame persist", task_id=task.task_id):@@ -2923,6 +2989,7 @@ class ForgeAgent:browser_session_id: str | None = None,complete_verification: bool = True,engine: RunEngine = RunEngine.skyvern_v1,+ workflow_owned_recovery: bool = False,cua_response: OpenAIResponse | None = None,llm_caller: LLMCaller | None = None,download_baseline_files: list[str] | None = None,@@ -3124,6 +3191,7 @@ class ForgeAgent:close_browser_on_completion=close_browser_on_completion,browser_session_id=browser_session_id,task_block=task_block,+ workflow_owned_recovery=workflow_owned_recovery,)finally:await app.ARTIFACT_MANAGER.flush_step_archive(step.step_id)@@ -3329,6 +3397,7 @@ class ForgeAgent:task_block=task_block,complete_verification=complete_verification,engine=engine,+ workflow_owned_recovery=workflow_owned_recovery,cua_response=cua_response_param,llm_caller=llm_caller,download_baseline_files=list_files_before,@@ -3346,6 +3415,7 @@ class ForgeAgent:task_block=task_block,complete_verification=complete_verification,engine=engine,+ workflow_owned_recovery=workflow_owned_recovery,cua_response=cua_response_param,llm_caller=llm_caller,download_baseline_files=list_files_before,@@ -6103,8 +6173,10 @@ class ForgeAgent:browser_state = pre_resolved_browser_state# An inherited browser_state was NOT navigated to this task's url here, so its# last_navigation_status belongs to an earlier navigation, not this task's starting URL.- # Clear it so the Task V3 loop never reads a stale status as this run's starting-URL dead-end.+ # Clear it so the Task V3 loop never reads a stale status as this run's starting-URL dead-end,+ # and the URL with it: the two are only ever read as a pair.browser_state.last_navigation_status = None+ browser_state.last_navigation_url = Noneelif workflow_run:browser_state = await app.BROWSER_MANAGER.get_or_create_for_workflow_run(workflow_run=workflow_run,diff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.pyindex 063465f46..ba4de4576 100644--- a/skyvern/forge/agent_functions.py+++ b/skyvern/forge/agent_functions.py@@ -38,6 +38,11 @@ from skyvern.forge.sdk.api.llm.api_handler import LLMAPIHandlerfrom skyvern.forge.sdk.api.llm.api_handler_factory import get_org_aware_secondary_llm_api_handlerfrom skyvern.forge.sdk.api.llm.exceptions import LLMProviderErrorfrom skyvern.forge.sdk.cache.base import CACHE_EXPIRE_TIME+from skyvern.forge.sdk.copilot.browser_ablation import CopilotBrowserCodeMode+from skyvern.forge.sdk.copilot.browser_code_contract import (+ BrowserCodeSession,+ BrowserCodeSessionUnavailableError,+)from skyvern.forge.sdk.copilot.code_block_preflight import CodeBlockScanFindingfrom skyvern.forge.sdk.copilot.config import (CopilotConfig,@@ -255,11 +260,19 @@ class CodeBlockEngineFailure:nav_error_code: str | None = None+DownloadClaimOutcome = Literal["returned_proven", "returned_unproven", "raised"]++@dataclass(frozen=True)class CodeBlockDownloadOperationReceipt:- """Structured proof that the secure runner invoked the brokered download operation."""+ """Structured proof that the secure runner invoked the brokered download operation.++ ``outcome`` records how the claim resolved so settlement can tell a proven delivery from the+ branch-5 placeholder return without string-matching the filename. ``None`` means an old producer+ that did not record it — settlement must leave the truthful-completion verdict unarmed."""operation: Literal["click_and_claim_download", "expect_download"] = "click_and_claim_download"+ outcome: DownloadClaimOutcome | None = None@dataclass@@ -1308,6 +1321,27 @@ class AgentFunction:"""Base no-op; callers fail closed when worker dispatch is unavailable."""return False+ async def copilot_browser_code_mode(+ self, *, organization_id: str, workflow_permanent_id: str+ ) -> CopilotBrowserCodeMode:+ """Whether run_browser_code joins the copilot surface, and whether it replaces the browser tools;+ OSS has no isolated interpreter to host it."""+ return CopilotBrowserCodeMode.OFF++ async def open_copilot_browser_code_session(+ self,+ *,+ page: Page,+ lifetime_seconds: float,+ organization_id: str,+ chat_id: str,+ turn_id: str,+ browser_session_id: str | None,+ ) -> BrowserCodeSession:+ raise BrowserCodeSessionUnavailableError(+ "Browser code is not available on this deployment.", error_code="unavailable"+ )+def resolve_copilot_dispatch_trigger_type(self) -> WorkflowRunTriggerType | None:"""Base no-op (no dispatch routing hint); overridden per deployment."""return Nonediff --git a/skyvern/forge/sdk/api/llm/api_handler_factory.py b/skyvern/forge/sdk/api/llm/api_handler_factory.pyindex 214425f39..0cfab8168 100644--- a/skyvern/forge/sdk/api/llm/api_handler_factory.py+++ b/skyvern/forge/sdk/api/llm/api_handler_factory.py@@ -75,6 +75,7 @@ from skyvern.schemas.llm import (LLMAllowedFailsPolicy,LLMConfig,LLMRouterConfig,+ LLMRouterModelConfig,)from skyvern.utils.image_resizer import Resolution, get_resize_target_dimension, resize_screenshotsfrom skyvern.utils.image_token_estimator import estimate_image_cost, estimate_image_tokens, provider_image_tokens@@ -177,6 +178,29 @@ def _json_error_body_length(error: JSONDecodeError) -> int | None:return len(doc) if isinstance(doc, str) else None+def _dispatchable_deployments(llm_config: LLMRouterConfig) -> list[LLMRouterModelConfig]:+ """The deployments a router can actually serve a call from: the main group plus any fallback+ group. A deployment outside both is unreachable, so judging a call by it would be wrong.+ """+ groups = {llm_config.main_model_group}+ fallback_group = llm_config.fallback_model_group+ if isinstance(fallback_group, str):+ groups.add(fallback_group)+ elif fallback_group:+ groups.update(fallback_group)+ return [deployment for deployment in llm_config.model_list if deployment.model_name in groups]+++def _endpoint_cannot_serve_responses_api(model: str | None, api_base: str | None) -> bool:+ """Whether an endpoint is one litellm's chat->responses bridge must not be used against.++ An explicit api_base points litellm at somewhere that is not guaranteed to implement+ /v1/responses, whatever the model is named; only Azure is known to serve it. Shared so the+ single-config and router-deployment checks cannot drift apart.+ """+ return bool(api_base) and not str(model or "").startswith("azure/")++async def _validate_custom_llm_api_base(llm_key: str, llm_config: LLMConfig | LLMRouterConfig) -> None:if not is_custom_llm_key(llm_key) or SettingsManager.get_settings().ALLOW_CUSTOM_LLM_LOCAL_API_BASES:return@@ -1521,16 +1545,9 @@ class LLMAPIHandlerFactory:# exactly that and nothing else (a model_info label saying gpt-5.6 would answer True where# litellm will not bridge, sending the dict to a chat-completions endpoint).if isinstance(llm_config, LLMRouterConfig):- # EVERY deployment that can serve the call (main + fallback groups, same filter as- # _resolve_tool_choice_support) must be a bridge model: a mixed router could hand the- # dict reasoning_effort to a non-bridge fallback, which rejects it.- groups = {llm_config.main_model_group}- fallback_group = llm_config.fallback_model_group- if isinstance(fallback_group, str):- groups.add(fallback_group)- elif fallback_group:- groups.update(fallback_group)- deployments = [deployment for deployment in llm_config.model_list if deployment.model_name in groups]+ # EVERY deployment that can serve the call must be a bridge model: a mixed router+ # could hand the dict reasoning_effort to a non-bridge fallback, which rejects it.+ deployments = _dispatchable_deployments(llm_config)if not deployments:return Falsereturn all(_is_bridge_model(deployment.litellm_params.get("model")) for deployment in deployments)@@ -3289,7 +3306,9 @@ class LLMCaller:self._warned_unsupported_tool_choice = Falseopenrouter_model_name = LLMAPIHandlerFactory._openrouter_model_name(self.llm_key, self.llm_config)self._custom_openrouter = bool(openrouter_model_name and is_custom_llm_key(self.original_llm_key))- # openrouter/ keys always resolve to LLMConfig, never LLMRouterConfig+ # _openrouter_model_name only recognises the LLMConfig shape, so only that shape has its+ # llm_key rewritten to the bare model id. A router config whose deployments are+ # openrouter/ models keeps its group name here, and so logs under a different key.if openrouter_model_name and isinstance(self.llm_config, LLMConfig):self.llm_key = openrouter_model_name.replace("openrouter/", "")if not self._custom_openrouter:@@ -3337,11 +3356,17 @@ class LLMCaller:# OPENAI_COMPATIBLE registration, whose key name is configurable): only real OpenAI and# Azure endpoints are known to serve the bridge, and real OpenAI needs no api_base.litellm_params = getattr(self.llm_config, "litellm_params", None)- if (- isinstance(self.llm_config, LLMConfig)- and litellm_params is not None- and litellm_params.get("api_base")- and not self.llm_config.model_name.startswith("azure/")+ if isinstance(self.llm_config, LLMConfig) and litellm_params is not None:+ if _endpoint_cannot_serve_responses_api(self.llm_config.model_name, litellm_params.get("api_base")):+ return False+ # A router config carries its api_base per deployment rather than on the config, so the+ # same test has to run over the deployments it can dispatch to. One that cannot serve the+ # bridge is enough to deny: the router picks the deployment, this call cannot.+ if isinstance(self.llm_config, LLMRouterConfig) and any(+ _endpoint_cannot_serve_responses_api(+ deployment.litellm_params.get("model"), deployment.litellm_params.get("api_base")+ )+ for deployment in _dispatchable_deployments(self.llm_config)):return Falsereturn LLMAPIHandlerFactory.uses_openai_responses_bridge(self.llm_config)@@ -3366,15 +3391,7 @@ class LLMCaller:return Falsetry:if isinstance(self.llm_config, LLMRouterConfig):- groups = {self.llm_config.main_model_group}- fallback_group = self.llm_config.fallback_model_group- if isinstance(fallback_group, str):- groups.add(fallback_group)- elif fallback_group:- groups.update(fallback_group)- deployments = [- deployment for deployment in self.llm_config.model_list if deployment.model_name in groups- ]+ deployments = _dispatchable_deployments(self.llm_config)if not deployments:return Falsereturn all(diff --git a/skyvern/forge/sdk/api/llm/config_registry.py b/skyvern/forge/sdk/api/llm/config_registry.pyindex 576999ce6..7457b7a04 100644--- a/skyvern/forge/sdk/api/llm/config_registry.py+++ b/skyvern/forge/sdk/api/llm/config_registry.py@@ -24,6 +24,9 @@ XAI_GROK_4_5_MAX_OUTPUT_TOKENS = 128_000# OpenRouter's own provider slug (https://openrouter.ai/api/v1/providers) - a name it does not# recognize in `ignore` is skipped silently rather than rejected.OPENINFERENCE_PROVIDER_SLUG = "open-inference"+# OpenRouter routes by provider SLUG and skips an unrecognised one silently, so a name here+# that no longer matches enforces nothing. Both are asserted in tests for that reason.+AMAZON_BEDROCK_PROVIDER_SLUG = "amazon-bedrock"@dataclass(frozen=True)diff --git a/skyvern/forge/sdk/browser_egress_policy.py b/skyvern/forge/sdk/browser_egress_policy.pyindex 83daa3ae0..9b64f8414 100644--- a/skyvern/forge/sdk/browser_egress_policy.py+++ b/skyvern/forge/sdk/browser_egress_policy.py@@ -152,6 +152,23 @@ def _ip_is_public(ip: ipaddress.IPv4Address | ipaddress.IPv6Address) -> bool:)+def _locally_allowed_hosts() -> frozenset[str]:+ """Hosts the operator named in ALLOWED_HOSTS, honoured only on a developer checkout.++ The SSRF host guard already exempts these, and without the same exemption here brokered code+ cannot reach a host-local fixture: the refusal surfaces to a code block as a bare+ ERR_ADDRESS_UNREACHABLE. Empty for cloud and packaged deployments, and empty by default.+ """+ # Imported here rather than at module scope: this is a leaf policy module and agent_functions+ # reaches back through it.+ from skyvern.config import settings+ from skyvern.forge.agent_functions import running_in_packaged_deployment++ if settings.is_cloud_environment() or running_in_packaged_deployment():+ return frozenset()+ return frozenset(host.lower().rstrip(".") for host in settings.ALLOWED_HOSTS if host)++def resolve_public_destination(url: str) -> ResolvedDestination | None:"""Resolve one browser destination and reject every unusable or non-public peer."""# urlsplit matches the WHATWG parser's whitespace handling (strips C0/space,@@ -183,18 +200,22 @@ def resolve_public_destination(url: str) -> ResolvedDestination | None:if not host:raise DestinationBlockedError("blocked egress: URL has no host")+ allowed_locally = host.lower().rstrip(".") in _locally_allowed_hosts()+literal_ip: ipaddress.IPv4Address | ipaddress.IPv6Address | Nonetry:literal_ip = ipaddress.ip_address(host)except ValueError:literal_ip = _parse_whatwg_ipv4(host)if literal_ip is not None:- if not _ip_is_public(literal_ip):+ if not allowed_locally and not _ip_is_public(literal_ip):raise DestinationBlockedError(f"blocked egress to internal address {raw_host}")return ResolvedDestination(host, port, (str(literal_ip),))normalized_host = to_ascii_host(host).lower()- if normalized_host in BLOCKED_HOST_NAMES or normalized_host.endswith(BLOCKED_HOST_SUFFIXES):+ if not allowed_locally and (+ normalized_host in BLOCKED_HOST_NAMES or normalized_host.endswith(BLOCKED_HOST_SUFFIXES)+ ):raise DestinationBlockedError(f"blocked egress to internal hostname {raw_host}")try:@@ -208,7 +229,7 @@ def resolve_public_destination(url: str) -> ResolvedDestination | None:resolved_ip = ipaddress.ip_address(address)except ValueError:continue- if not _ip_is_public(resolved_ip):+ if not allowed_locally and not _ip_is_public(resolved_ip):raise DestinationBlockedError(f"blocked egress to internal hostname {host} (resolved {address})")numeric_address = str(resolved_ip)if numeric_address not in addresses:diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.pyindex a6dfab432..88070a049 100644--- a/skyvern/forge/sdk/copilot/agent.py+++ b/skyvern/forge/sdk/copilot/agent.py@@ -50,9 +50,12 @@ from skyvern.forge.sdk.copilot.blocker_signal import ()from skyvern.forge.sdk.copilot.blocker_signal import to_trace_data as blocker_signal_to_trace_datafrom skyvern.forge.sdk.copilot.browser_ablation import (+ CopilotBrowserCodeMode,CopilotEvalMode,CopilotToolSurface,+ CopilotToolSurfaceIdentity,config_for_eval_mode,+ dispatch_allowlist_enforced,resolve_copilot_tool_surface,)from skyvern.forge.sdk.copilot.budget_expiry import BudgetExpiryState, serialize_prior_budget_expiry@@ -221,6 +224,7 @@ from skyvern.forge.sdk.copilot.streaming_adapter import (maybe_emit_design_end,)from skyvern.forge.sdk.copilot.tools.blockers import _goal_value_paths_for_code_block+from skyvern.forge.sdk.copilot.tools.browser_code import close_browser_code_sessionfrom skyvern.forge.sdk.copilot.tools.credentials import _server_verified_google_account_choicesfrom skyvern.forge.sdk.copilot.tools.guardrails import _record_output_policy_guardrail_outcomefrom skyvern.forge.sdk.copilot.tools.run_execution import (@@ -1388,15 +1392,22 @@ def _recorded_build_test_outcome_prompt(ctx: CopilotContext | None) -> str:and outcome.reason_code == "no_meaningful_output"and outcome.workflow_run_id):- lines.extend(- [- "POST-RUN PAGE-PATH CONTRACT UNBOUND:",+ if ctx is not None and ctx.tool_surface_identity == CopilotToolSurfaceIdentity.REQUIRED_CODE:+ observe_first = (+ "Before acting on the page again from browser code, call inspect_page_for_composition to observe "+ "the current page. If the fresh same-run observation is page-path-shaped, it will emit the exact "+ "allowed click or Enter selector; use only that selector, from browser code with "+ 'target="last_run" when the run executed in its own browser, without navigating or re-authoring '+ "first. Otherwise the existing blocker remains in force."+ )+ else:+ observe_first = ('Before any click or key press, call inspect_page_for_composition with target_url="current_page". '"Do not use evaluate as a substitute. If the fresh same-run observation is page-path-shaped, it ""will emit the exact allowed click or Enter selector; use only that selector without navigating or "- "re-authoring first. Otherwise the existing blocker remains in force.",- ]- )+ "re-authoring first. Otherwise the existing blocker remains in force."+ )+ lines.extend(["POST-RUN PAGE-PATH CONTRACT UNBOUND:", observe_first])if outcome.observed_evidence_summary:lines.append(f"observed_evidence: {_clean_authoring_repair_prompt_atom(outcome.observed_evidence_summary)}")if outcome.observed_page_value_excerpt:@@ -4242,7 +4253,7 @@ async def _run_agent_loop_with_surface(context_provider=lambda: ctx,ordered_allowlist=(tuple(alias_map.values()) if ctx.eval_mode == CopilotEvalMode.BROWSER_ABLATION else None),enforce_dispatch_allowlist=(- ctx.eval_mode == CopilotEvalMode.BROWSER_ABLATION or ctx.turn_origin == TurnOrigin.runtime_self_heal+ dispatch_allowlist_enforced(ctx.tool_surface_identity) or ctx.turn_origin == TurnOrigin.runtime_self_heal),)ctx.discovery_mcp_server = mcp_server@@ -4274,6 +4285,7 @@ async def _run_agent_loop_with_surface(# Without the ablation's ordered allowlist the server publishes in its own# order, so record the order advertised rather than asserting one.ordered_mcp_names=tuple(tool.name for tool in advertised_mcp_tools),+ identity=ctx.tool_surface_identity or CopilotToolSurfaceIdentity.OPTIONAL,).advertised_sha256(advertised_mcp_tools)attempts = 2 if allow_untested_retry else 1for attempt in range(attempts):@@ -5398,8 +5410,8 @@ async def _run_copilot_turn_impl()from skyvern.forge.sdk.copilot.model_resolver import resolve_model_configfrom skyvern.forge.sdk.copilot.tools import (- NATIVE_TOOLS,_build_skyvern_mcp_overlays,+ copilot_native_tools,get_skyvern_mcp_alias_map,)@@ -5517,21 +5529,35 @@ async def _run_copilot_turn_impl(registered_mcp_tools = (await skyvern_mcp.list_tools(run_middleware=False) if eval_mode == CopilotEvalMode.BROWSER_ABLATION else None)+ browser_code_mode = await app.AGENT_FUNCTION.copilot_browser_code_mode(+ organization_id=organization_id,+ workflow_permanent_id=chat_request.workflow_permanent_id,+ )surface = resolve_copilot_tool_surface(mode=eval_mode,- native_tools=[tool for tool in NATIVE_TOOLS if tool.name != "ask_user" or chat_request.supports_question_tool],+ native_tools=copilot_native_tools(+ supports_question_tool=chat_request.supports_question_tool,+ browser_code_available=browser_code_mode != CopilotBrowserCodeMode.OFF,+ ),alias_map=alias_map,overlays=overlays,registered_mcp_tools=registered_mcp_tools,browser_tools_available=copilot_config.browser_tools_available,+ browser_code_mode=browser_code_mode,)native_tools = list(surface.native_tools)alias_map = surface.alias_mapoverlays = surface.overlays- if eval_mode is not None:- ctx.eval_tool_surface_sha256 = surface.sha256- ctx.eval_native_tool_names = surface.ordered_native_names- ctx.eval_mcp_tool_names = surface.ordered_mcp_names+ ctx.tool_surface_identity = surface.identity+ ctx.eval_tool_surface_sha256 = surface.sha256+ ctx.eval_native_tool_names = surface.ordered_native_names+ ctx.eval_mcp_tool_names = surface.ordered_mcp_names+ LOG.info(+ "copilot_tool_surface_resolved",+ tool_surface_identity=surface.identity.value,+ tool_surface_sha256=surface.sha256,+ eval_mode=eval_mode.value if eval_mode is not None else None,+ )tool_info: list[tuple[str, str]] = [(tool.name, tool.description or "") for tool in native_tools]tool_info.extend((name, overlay.description or "") for name, overlay in overlays.items())@@ -5783,3 +5809,4 @@ async def _run_copilot_turn_impl(finally:if model_session is not None:model_session.close()+ await close_browser_code_session(ctx)diff --git a/skyvern/forge/sdk/copilot/browser_ablation.py b/skyvern/forge/sdk/copilot/browser_ablation.pyindex a72a719af..85951231e 100644--- a/skyvern/forge/sdk/copilot/browser_ablation.py+++ b/skyvern/forge/sdk/copilot/browser_ablation.py@@ -21,6 +21,26 @@ class CopilotEvalMode(StrEnum):REPAIR_PROBE_ON = "repair_probe_on"+class CopilotBrowserCodeMode(StrEnum):+ OFF = "off"+ # run_browser_code is advertised beside the browser tools.+ ADD = "add"+ # run_browser_code is the only way to act on the browser; the direct and mixed tools are withdrawn.+ REPLACE = "replace"+++class CopilotToolSurfaceIdentity(StrEnum):+ REQUIRED_CODE = "required_code"+ OPTIONAL = "optional"+ BROWSER_ABLATION = "browser_ablation"+++def dispatch_allowlist_enforced(identity: CopilotToolSurfaceIdentity | None) -> bool:+ """Only a narrowed surface refuses names at dispatch; the optional surface keeps the parent's+ open dispatch, so a deployment without the code tool behaves exactly as before."""+ return identity in (CopilotToolSurfaceIdentity.REQUIRED_CODE, CopilotToolSurfaceIdentity.BROWSER_ABLATION)++REPAIR_PROBE_TOOL = "inspect_locator_matches"REPAIR_PROBE_MODES = (CopilotEvalMode.REPAIR_PROBE_OFF, CopilotEvalMode.REPAIR_PROBE_ON)@@ -67,6 +87,20 @@ BROWSER_ABLATION_REQUIRED_EXTENSION_TAGS = frozenset({"tab_management", "page_reBROWSER_ABLATION_MCP_TOOL_EXCLUSIONS = frozenset({"skyvern_open_tabs"})+# The browser-bound aliases that survive when mutations must go through code: page and console+# reads, observe-only waits and frame enumeration. Every alias that needs no browser survives too,+# so a tool added later is withdrawn only if it can act on the page.+REQUIRED_CODE_BROWSER_READ_ALIASES = frozenset(+ {+ "get_browser_screenshot",+ "console_messages",+ "wait_for_either_state",+ "skyvern_frame_list",+ }+)+REQUIRED_CODE_REMOVED_NATIVE_TOOLS = frozenset({"discover_workflow_entrypoint"})++@dataclass(frozen=True, slots=True)class CopilotToolSurface:native_tools: tuple[Any, ...]@@ -74,6 +108,10 @@ class CopilotToolSurface:overlays: dict[str, Any]ordered_native_names: tuple[str, ...]ordered_mcp_names: tuple[str, ...]+ # Kept out of the hash payload below: the identity names which projection ran, while the hash+ # must stay comparable across surfaces that advertise the same tools. OPTIONAL never narrows+ # dispatch, so a surface that does not name a projection keeps the parent's behavior.+ identity: CopilotToolSurfaceIdentity = CopilotToolSurfaceIdentity.OPTIONAL@propertydef sha256(self) -> str:@@ -237,6 +275,54 @@ def _registered_browser_mcp_surface(return selected_aliases, selected_overlays, tuple(selected_aliases)+def _optional_surface(+ selected_native: list[Any],+ alias_map: dict[str, str],+ overlays: dict[str, Any],+) -> CopilotToolSurface:+ return CopilotToolSurface(+ native_tools=tuple(selected_native),+ alias_map=alias_map,+ overlays=overlays,+ ordered_native_names=tuple(tool.name for tool in selected_native),+ ordered_mcp_names=tuple(alias_map),+ identity=CopilotToolSurfaceIdentity.OPTIONAL,+ )+++def _required_code_surface(+ selected_native: list[Any],+ alias_map: dict[str, str],+ overlays: dict[str, Any],+) -> CopilotToolSurface:+ from skyvern.forge.sdk.copilot.tools.composition_capture import (+ COMPOSITION_INSPECTION_TOOL_NAME,+ current_page_inspection_tool,+ )++ missing_aliases = sorted(REQUIRED_CODE_BROWSER_READ_ALIASES.difference(alias_map.keys() & overlays.keys()))+ if missing_aliases:+ raise ValueError(f"missing MCP tool names: {', '.join(missing_aliases)}")+ projected_native = [+ current_page_inspection_tool(tool) if tool.name == COMPOSITION_INSPECTION_TOOL_NAME else tool+ for tool in selected_native+ if tool.name not in REQUIRED_CODE_REMOVED_NATIVE_TOOLS+ ]+ selected_aliases = {+ name: transport+ for name, transport in alias_map.items()+ if name in REQUIRED_CODE_BROWSER_READ_ALIASES or not overlays[name].requires_browser+ }+ return CopilotToolSurface(+ native_tools=tuple(projected_native),+ alias_map=selected_aliases,+ overlays={name: overlays[name] for name in selected_aliases},+ ordered_native_names=tuple(tool.name for tool in projected_native),+ ordered_mcp_names=tuple(selected_aliases),+ identity=CopilotToolSurfaceIdentity.REQUIRED_CODE,+ )++def resolve_copilot_tool_surface(*,mode: CopilotEvalMode | None,@@ -245,6 +331,7 @@ def resolve_copilot_tool_surface(overlays: dict[str, Any],registered_mcp_tools: Sequence[Any] | None = None,browser_tools_available: bool = True,+ browser_code_mode: CopilotBrowserCodeMode = CopilotBrowserCodeMode.ADD,) -> CopilotToolSurface:if not browser_tools_available:# Local import: tools/__init__ reaches this leaf back through CopilotContext.@@ -268,6 +355,8 @@ def resolve_copilot_tool_surface()if mode is None or mode in REPAIR_PROBE_MODES:+ from skyvern.forge.sdk.copilot.tools.browser_code import TOOL_NAME as BROWSER_CODE_TOOL_NAME+selected = [toolfor tool in native_tools@@ -278,13 +367,11 @@ def resolve_copilot_tool_surface(# run against a surface without the probe would report an OFF-vs-OFF contrast as real.if mode in REPAIR_PROBE_MODES and REPAIR_PROBE_TOOL not in {tool.name for tool in native_tools}:raise ValueError(f"{REPAIR_PROBE_TOOL} is not on the production native surface")- return CopilotToolSurface(- native_tools=tuple(selected),- alias_map=alias_map,- overlays=overlays,- ordered_native_names=tuple(tool.name for tool in selected),- ordered_mcp_names=tuple(alias_map),- )+ if browser_code_mode == CopilotBrowserCodeMode.REPLACE and BROWSER_CODE_TOOL_NAME in {+ tool.name for tool in selected+ }:+ return _required_code_surface(selected, alias_map, overlays)+ return _optional_surface(selected, alias_map, overlays)native_by_name = {tool.name: tool for tool in native_tools}if len(native_by_name) != len(native_tools):@@ -303,4 +390,5 @@ def resolve_copilot_tool_surface(overlays=selected_overlays,ordered_native_names=BROWSER_ABLATION_NATIVE_TOOLS,ordered_mcp_names=selected_names,+ identity=CopilotToolSurfaceIdentity.BROWSER_ABLATION,)diff --git a/skyvern/forge/sdk/copilot/browser_code_contract.py b/skyvern/forge/sdk/copilot/browser_code_contract.pynew file mode 100644index 000000000..85f24e02c--- /dev/null+++ b/skyvern/forge/sdk/copilot/browser_code_contract.py@@ -0,0 +1,169 @@+"""Types shared by the copilot browser-code tool and the deployment that hosts its interpreter."""++from __future__ import annotations++import uuid+from collections.abc import Mapping+from dataclasses import dataclass, field+from typing import Any, Protocol++from playwright.async_api import Page++# Operations that demonstrate an interaction the scout trajectory must keep, even past the+# model-facing operation cap.+DEMONSTRATED_OPERATIONS = frozenset(+ {"click", "dblclick", "tap", "fill", "type", "press", "press_sequentially", "check", "uncheck", "set_checked"}+ | {"select_option"}+)+++class BrowserCodeSessionUnavailableError(Exception):+ """The deployment could not open or keep a code session; the message is safe to show the model."""++ def __init__(+ self,+ message: str,+ *,+ error_code: str,+ retry_after_seconds: float | None = None,+ stage: str | None = None,+ session_ended: bool = False,+ ) -> None:+ super().__init__(message)+ self.error_code = error_code+ # Set when the refusal is a capacity one, so the model is told when to try again.+ self.retry_after_seconds = retry_after_seconds+ # Which step refused: dispatch, session_open or cell_admission.+ self.stage = stage+ # Set when the session this refusal came from is over, so the caller drops it now rather than+ # spending the retry it was just told to make on discovering that.+ self.session_ended = session_ended+++@dataclass(frozen=True)+class BrowserCodeOperation:+ operation: str+ selector: str | None+ # None means the operation reached the browser and no reply came back, so it may have applied.+ succeeded: bool | None+ error: str | None = None+ # Set only when a trusted readback of the filled field equals the value the fill sent.+ input_value: str | None = None+ source_url: str | None = None+ result_url: str | None = None++ def to_json(self) -> dict[str, Any]:+ return {+ "operation": self.operation,+ "selector": self.selector,+ "succeeded": self.succeeded,+ "error": self.error,+ "input_value": self.input_value,+ "source_url": self.source_url,+ "result_url": self.result_url,+ }++ @classmethod+ def from_json(cls, payload: Mapping[str, Any]) -> BrowserCodeOperation:+ return cls(+ operation=str(payload["operation"]),+ selector=payload.get("selector"),+ succeeded=payload.get("succeeded"),+ error=payload.get("error"),+ input_value=payload.get("input_value"),+ source_url=payload.get("source_url"),+ result_url=payload.get("result_url"),+ )+++@dataclass(frozen=True)+class BrowserCodeCellResult:+ ok: bool+ value: Any+ stdout: str | None+ stdout_truncated: bool+ error_code: str | None+ error: str | None+ failing_line: int | None+ operations: tuple[BrowserCodeOperation, ...]+ operations_omitted: int+ session_alive: bool+ current_url: str | None+ # Every demonstrated action the cell ran, uncapped, so action evidence survives the model-facing cap.+ actions: tuple[BrowserCodeOperation, ...] = ()+ # Set when this cell ran on a fresh interpreter, which the caller cannot see for itself: the host it+ # is talking to is unchanged, and only the side that reopened knows the earlier values are gone.+ interpreter_restarted: bool = False+ # Set when the interpreter was moved to another tab since the last result, which drops every page,+ # locator and frame handle the cell holds; a move the caller did not ask for is otherwise invisible.+ handles_invalidated: bool = False++ def to_json(self) -> dict[str, Any]:+ return {+ "ok": self.ok,+ "value": self.value,+ "stdout": self.stdout,+ "stdout_truncated": self.stdout_truncated,+ "error_code": self.error_code,+ "error": self.error,+ "failing_line": self.failing_line,+ "operations": [operation.to_json() for operation in self.operations],+ "operations_omitted": self.operations_omitted,+ "session_alive": self.session_alive,+ "current_url": self.current_url,+ "actions": [action.to_json() for action in self.actions],+ "interpreter_restarted": self.interpreter_restarted,+ "handles_invalidated": self.handles_invalidated,+ }++ @classmethod+ def from_json(cls, payload: Mapping[str, Any]) -> BrowserCodeCellResult:+ return cls(+ ok=bool(payload["ok"]),+ value=payload.get("value"),+ stdout=payload.get("stdout"),+ stdout_truncated=bool(payload.get("stdout_truncated", False)),+ error_code=payload.get("error_code"),+ error=payload.get("error"),+ failing_line=payload.get("failing_line"),+ operations=tuple(BrowserCodeOperation.from_json(item) for item in payload.get("operations") or ()),+ operations_omitted=int(payload.get("operations_omitted", 0)),+ session_alive=bool(payload.get("session_alive", False)),+ current_url=payload.get("current_url"),+ actions=tuple(BrowserCodeOperation.from_json(item) for item in payload.get("actions") or ()),+ interpreter_restarted=bool(payload.get("interpreter_restarted", False)),+ handles_invalidated=bool(payload.get("handles_invalidated", False)),+ )+++class BrowserCodeSession(Protocol):+ @property+ def session_id(self) -> str: ...++ @property+ def page(self) -> Page: ...++ async def rebind(self, page: Page) -> None: ...++ async def run_cell(+ self, code: str, *, timeout_seconds: float, deny_pixels: bool = False+ ) -> BrowserCodeCellResult: ...++ def last_dispatched_operation(self) -> BrowserCodeOperation | None: ...++ async def close(self) -> None: ...+++@dataclass+class BrowserCodeHost:+ """The turn's code session and the browser binding it was opened or last rebound for."""++ # Names this turn's interpreter to whoever hosts it. Minted here because the host is created once+ # per turn, so the identifier lives exactly as long as the session it names.+ turn_key: str = field(default_factory=lambda: uuid.uuid4().hex)+ session: BrowserCodeSession | None = None+ browser_session_id: str | None = None+ browser_session_generation: int = 0+ sessions_opened: int = 0+ interrupted: bool = False+ interrupted_operation: BrowserCodeOperation | None = Nonediff --git a/skyvern/forge/sdk/copilot/browser_target.py b/skyvern/forge/sdk/copilot/browser_target.pyindex 0ed5e9163..b612643a7 100644--- a/skyvern/forge/sdk/copilot/browser_target.py+++ b/skyvern/forge/sdk/copilot/browser_target.py@@ -12,7 +12,7 @@ from dataclasses import dataclassfrom enum import StrEnumfrom typing import Any-from skyvern.forge.sdk.copilot.context import CopilotContext+from skyvern.forge.sdk.copilot.runtime import AgentContextBROWSER_TARGET_PARAM_NAME = "target"@@ -52,7 +52,7 @@ class BrowserSessionBinding:source_matches_target: boolunavailable_reason: str | None = None- def session_id_for(self, copilot_ctx: CopilotContext) -> str | None:+ def session_id_for(self, copilot_ctx: AgentContext) -> str | None:return self.session_id_override or copilot_ctx.browser_session_iddef provenance(self) -> dict[str, Any]:@@ -69,7 +69,7 @@ class BrowserSessionBinding:return stamp-def resolve_browser_session_binding(copilot_ctx: CopilotContext, arguments: dict[str, Any]) -> BrowserSessionBinding:+def resolve_browser_session_binding(copilot_ctx: AgentContext, arguments: dict[str, Any]) -> BrowserSessionBinding:"""Bind this call to the browser the model named, or report why that browser is not addressable.``last_run`` is a promise about identity, so it is kept only against the exact recorded rundiff --git a/skyvern/forge/sdk/copilot/composition_evidence.py b/skyvern/forge/sdk/copilot/composition_evidence.pyindex 41883178d..64f6fab2e 100644--- a/skyvern/forge/sdk/copilot/composition_evidence.py+++ b/skyvern/forge/sdk/copilot/composition_evidence.py@@ -16,6 +16,7 @@ except ImportError: # pragma: no cover - bs4 is a transitive dep but inspectionBeautifulSoup = None # type: ignore[assignment, misc]from skyvern.config import settings+from skyvern.forge.sdk.copilot.browser_ablation import CopilotToolSurfaceIdentityfrom skyvern.forge.sdk.copilot.challenge_evidence import (CHALLENGE_EVIDENCE_SOURCE_KEY,CHALLENGE_KIND_KEY,@@ -1339,10 +1340,15 @@ def composition_page_evidence_error(block_observation_refs=block_observation_refs,)if string_step is not None:+ observation_source = (+ "inspect_page_for_composition"+ if ctx.tool_surface_identity == CopilotToolSurfaceIdentity.REQUIRED_CODE+ else "inspect_page_for_composition or evaluate"+ )return (f"{INTERNAL_VALIDATION_FAILURE_PREFIX}a block_observation_refs entry uses observation_step "f"{string_step!r} as a string. Pass the integer observation_step returned by "- "inspect_page_for_composition or evaluate for click-reached blocks. "+ f"{observation_source} for click-reached blocks. "f"Offending blocks: {_format_page_block_findings([block])}")if _required_observation_ref_missing(block, block_observation_refs):@@ -1370,13 +1376,20 @@ def composition_page_evidence_error(return (f"{INTERNAL_VALIDATION_FAILURE_PREFIX}a block references observation_step "f"{missing_step}, but {missing_reason}. "- "Inspect or evaluate the reached page again and pass the new observation_step in "+ "Inspect the reached page again and pass the new observation_step in ""block_observation_refs before composing page-dependent blocks. "f"Offending blocks: {_format_page_block_findings([block])}")+ if ctx.tool_surface_identity == CopilotToolSurfaceIdentity.REQUIRED_CODE:+ inspect_hint = (+ f"Open {target_url!r} from browser code if the browser is not already there, then call "+ "inspect_page_for_composition"+ )+ else:+ inspect_hint = f"Call inspect_page_for_composition(target_url={target_url!r})"return (f"{INTERNAL_VALIDATION_FAILURE_PREFIX}page-dependent build blocks need observed page evidence before they are "- f"authored. Call inspect_page_for_composition(target_url={target_url!r}) before composing page-dependent "+ f"authored. {inspect_hint} before composing page-dependent ""blocks, or save only the initial goto_url block and inspect the reached page before the next mutation. "f"Offending blocks: {_format_page_block_findings([block])}")diff --git a/skyvern/forge/sdk/copilot/config.py b/skyvern/forge/sdk/copilot/config.pyindex 440b0f8a7..b024a4317 100644--- a/skyvern/forge/sdk/copilot/config.py+++ b/skyvern/forge/sdk/copilot/config.py@@ -47,7 +47,7 @@ DEFAULT_TOKEN_BUDGET = 90_000SCREENSHOT_DROPPED_NUDGE = ("Your previous screenshot was dropped from context to recover from a token-budget overflow. ""Do NOT reason about the page from memory. Re-take the screenshot "- "(get_browser_screenshot) or call evaluate before deciding your next step."+ "(get_browser_screenshot) or read the page again before deciding your next step.")DEFAULT_ENFORCEMENT_NUDGES: dict[str, str] = {diff --git a/skyvern/forge/sdk/copilot/context.py b/skyvern/forge/sdk/copilot/context.pyindex 91ce5bea9..6ad4c19b2 100644--- a/skyvern/forge/sdk/copilot/context.py+++ b/skyvern/forge/sdk/copilot/context.py@@ -17,7 +17,10 @@ from typing_extensions import NotRequired, TypedDictfrom skyvern.forge.sdk.browser_action_policy import canonicalize_originfrom skyvern.forge.sdk.copilot.authoring_parameter_binding import AuthoringParameterBindingDirective-from skyvern.forge.sdk.copilot.browser_ablation import BrowserAblationMetadata+from skyvern.forge.sdk.copilot.browser_ablation import (+ BrowserAblationMetadata,+ CopilotToolSurfaceIdentity,+)from skyvern.forge.sdk.copilot.budget_expiry import BudgetExpiryStatefrom skyvern.forge.sdk.copilot.code_block_synthesis import CREDENTIAL_FILL_TOOL_NAMEfrom skyvern.forge.sdk.copilot.code_write_diff import TURN_PATCH_CHAR_BUDGET, CodeWriteDiff@@ -549,6 +552,13 @@ class StructuredContext(BaseModel):elif tool == "update_workflow":self.workflow_state = summary+ elif tool == "run_browser_code":+ self.decisions_made.append(f"{tool}: {summary}")+ if summary.startswith("Ran browser code") and " at " in summary:+ url = summary.rsplit(" at ", 1)[1].strip()+ if url and not any(v.url == url for v in self.urls_visited):+ self.urls_visited.append(UrlVisit(url=url, summary="browser code"))+elif tool in ("click","evaluate",@@ -1119,6 +1129,7 @@ class CopilotContext(AgentContext):human_input_wait: HumanInputWait = field(default_factory=HumanInputWait)eval_capture_case_id: str | None = Noneeval_prompt_sha256: str | None = None+ tool_surface_identity: CopilotToolSurfaceIdentity | None = Noneeval_tool_surface_sha256: str | None = Noneeval_native_tool_names: tuple[str, ...] = ()eval_mcp_tool_names: tuple[str, ...] = ()diff --git a/skyvern/forge/sdk/copilot/narration.py b/skyvern/forge/sdk/copilot/narration.pyindex 4f02440a1..b64f31dbc 100644--- a/skyvern/forge/sdk/copilot/narration.py+++ b/skyvern/forge/sdk/copilot/narration.py@@ -122,6 +122,7 @@ _TOOL_ACTIVITY_DISPLAY_LABELS = {"skyvern_frame_switch": "Opening embedded page","skyvern_frame_main": "Returning to main page","fill_credential_field": "Entering saved credentials",+ "run_browser_code": "Running browser code","edit_block": "Editing block","add_block": "Adding block","delete_block": "Deleting block",diff --git a/skyvern/forge/sdk/copilot/output_utils.py b/skyvern/forge/sdk/copilot/output_utils.pyindex 4de6b02cc..3557f1207 100644--- a/skyvern/forge/sdk/copilot/output_utils.py+++ b/skyvern/forge/sdk/copilot/output_utils.py@@ -2035,6 +2035,12 @@ def summarize_tool_result(tool_name: str, result: dict[str, Any], *, for_displayif field_count:return f"Inspected the page ({field_count} form field(s))"return "Inspected the page"+ if tool_name == "run_browser_code":+ operations = result.get("operations")+ count = len(operations) if isinstance(operations, list) else 0+ url = result.get("current_url")+ summary = f"Ran browser code ({count} operation(s))"+ return f"{summary} at {url[:80]}" if isinstance(url, str) and url else summaryreturn "OK"diff --git a/skyvern/forge/sdk/copilot/runtime.py b/skyvern/forge/sdk/copilot/runtime.pyindex 3049c37a0..79c3df85d 100644--- a/skyvern/forge/sdk/copilot/runtime.py+++ b/skyvern/forge/sdk/copilot/runtime.py@@ -12,6 +12,7 @@ from dataclasses import dataclass, fieldfrom enum import StrEnumfrom types import MappingProxyTypefrom typing import TYPE_CHECKING, Any, Literal, NotRequired, TypeAlias, TypedDict, cast+from urllib.parse import urlsplitimport structlog@@ -31,7 +32,8 @@ from skyvern.cli.core.session_manager import ()from skyvern.config import settingsfrom skyvern.forge import app-from skyvern.forge.sdk.copilot.browser_ablation import CopilotEvalMode+from skyvern.forge.sdk.copilot.browser_ablation import CopilotEvalMode, CopilotToolSurfaceIdentity+from skyvern.forge.sdk.copilot.browser_code_contract import BrowserCodeHostfrom skyvern.forge.sdk.copilot.budget_expiry import BudgetExpiryStatefrom skyvern.forge.sdk.copilot.build_test_connect_failure import (SUPERSEDED_BY_NEWER_TEST_REASON,@@ -412,6 +414,9 @@ class AgentContext:admitted_browser_operations: dict[str, set[asyncio.Task[object]]] = field(default_factory=dict)heal_workflow_run_id: str | None = Noneeval_mode: CopilotEvalMode | None = None+ # The self-heal turn advertises its own browser-only surface rather than resolving one, so it+ # names no projection; the shared agent loop still reads this to decide dispatch enforcement.+ tool_surface_identity: CopilotToolSurfaceIdentity | None = None# True only while a card is on screen. credential_pause_used stays true for the rest of the# turn once one has been raised, which cannot tell a concurrent sibling ask from a later one.credential_ask_in_flight: bool = False@@ -629,6 +634,7 @@ class AgentContext:# carries; recorded at credential resolve time and rehydrated from FillCarry across turns.scouted_credential_field_inventory_by_credential_id: dict[str, frozenset[str]] = field(default_factory=dict)credential_fill_lock: asyncio.Lock = field(default_factory=asyncio.Lock)+ browser_code_host: BrowserCodeHost = field(default_factory=BrowserCodeHost)# Serializes model-visible browser evidence commits with sensitive-run custody changes. Raw# browser work may overlap, but a post-hook or native credential fill owns this lock while it# decides whether its facts are admissible, so rollback cannot erase another call's evidence.@@ -650,6 +656,10 @@ class AgentContext:unbound_required_parameter_keys: list[str] = field(default_factory=list)# Source page of an in-flight scout action, captured before it may navigate away.pending_scout_source_url: str | None = None+ # The withheld page's URL, read before a navigation meant to leave it, keyed by the browser the+ # call acts in so concurrent calls on different browsers cannot read each other's. Compared to+ # the result to tell a fresh document from a fragment hop; never recorded or returned.+ pending_taint_source_urls: dict[str, str] = field(default_factory=dict)pending_scout_selector_candidates: list[ScoutedSelectorCandidate] | None = Nonepending_scout_input_value: str | None = None# (selector, role, accessible_name) read before an in-flight click that may navigate: a post-action@@ -1009,6 +1019,29 @@ def sensitive_origin_page_facts_withheld(ctx: AgentContext, run_id: str | None)return not tainting_run_ids <= origin_runs_bound_to_scrubber(ctx)+def navigation_document(url: str) -> str:+ """The URL without its fragment: a fragment-only change is a same-document navigation."""+ return urlsplit(url)._replace(fragment="").geturl()+++def navigation_replaced_document(source_url: str | None, result_url: str | None) -> bool:+ """Whether a navigation left the document it started on. Unknown URLs never count as leaving."""+ if not isinstance(source_url, str) or not isinstance(result_url, str) or not source_url or not result_url:+ return False+ return navigation_document(result_url) != navigation_document(source_url)+++def clear_sensitive_origin_page_taint_after_navigation(+ ctx: AgentContext, *, source_url: str | None, result_url: str | None+) -> bool:+ """Lift the withholding only when the navigation replaced the sensitive document; fragment hops+ and unknown URLs keep it, since the DOM that must not be read is still up."""+ if not navigation_replaced_document(source_url, result_url):+ return False+ clear_sensitive_origin_page_taint(ctx)+ return True++def clear_sensitive_origin_page_taint(ctx: AgentContext) -> None:session_id = effective_browser_session_id(ctx)active_session_ids = active_sensitive_origin_page_sessions(ctx)@@ -1154,9 +1187,10 @@ async def close_browser_session_quietly(session_id: str,*,reason: BrowserSessionCloseReason = BrowserSessionCloseReason.aborted,-) -> None:+) -> bool:"""Bounded: the session-manager backend is often the reason we are closing at all, so an- unbounded close could hang the request."""+ unbounded close could hang the request. Returns whether the close landed; a caller that needs+ the browser gone, not merely asked to go, has nothing else to tell it."""try:await asyncio.wait_for(app.PERSISTENT_SESSIONS_MANAGER.close_session(organization_id, session_id, reason=reason),@@ -1164,6 +1198,8 @@ async def close_browser_session_quietly()except Exception:LOG.debug("Failed to close browser session", session_id=session_id, exc_info=True)+ return False+ return Truedef _discard_finished_driver_release(task: asyncio.Task[bool] | asyncio.Task[None]) -> None:diff --git a/skyvern/forge/sdk/copilot/session_factory.py b/skyvern/forge/sdk/copilot/session_factory.pyindex 9cbf8cfb2..781fee4cd 100644--- a/skyvern/forge/sdk/copilot/session_factory.py+++ b/skyvern/forge/sdk/copilot/session_factory.py@@ -262,6 +262,7 @@ def _maybe_dump_model_input(data: CallModelData[Any], model_data: ModelInputDatapayload = {"capture_case_id": getattr(ctx, "eval_capture_case_id", None),"eval_mode": getattr(ctx, "eval_mode", None),+ "tool_surface_identity": getattr(ctx, "tool_surface_identity", None),"ordered_native_tool_names": list(getattr(ctx, "eval_native_tool_names", ())),"ordered_mcp_tool_names": list(getattr(ctx, "eval_mcp_tool_names", ())),"prompt_sha256": (diff --git a/skyvern/forge/sdk/copilot/tools/__init__.py b/skyvern/forge/sdk/copilot/tools/__init__.pyindex 1aa6fb6b7..1759e1909 100644--- a/skyvern/forge/sdk/copilot/tools/__init__.py+++ b/skyvern/forge/sdk/copilot/tools/__init__.py@@ -95,6 +95,8 @@ from .banned_blocks import _record_banned_block_reject_span as _record_banned_blfrom .blockers import _analyze_run_blocks as _analyze_run_blocksfrom .blockers import _run_blocks_structured_blocker_message as _run_blocks_structured_blocker_messagefrom .blockers import _trusted_post_drain_status as _trusted_post_drain_status+from .browser_code import TOOL_NAME as BROWSER_CODE_TOOL_NAME+from .browser_code import run_browser_code_toolfrom .completion import _build_run_evidence_snapshot as _build_run_evidence_snapshotfrom .completion import _completion_verification_handler as _completion_verification_handlerfrom .completion import _is_outcome_evidence_candidate as _is_outcome_evidence_candidate@@ -1151,8 +1153,8 @@ async def update_and_run_blocks_tool(for the reusable actions/checks the workflow actually needs.When you compose no-url blocks from a page reached by prior clicks, include`block_observation_refs` entries with each block label and the- `observation_step` returned by inspect_page_for_composition or evaluate for- the page that block acts on.+ `observation_step` returned by inspect_page_for_composition (or another+ page read) for the page that block acts on.For authored code blocks, include `code_artifact_metadata` rows describingdeclared goals, claimed outcomes, page dependencies, criteria, evidencerefs, observation refs, and terminal verifier expectations.@@ -1571,12 +1573,16 @@ async def fill_credential_field_tool(credential_id: str,field: str,submit_selector: str | None = None,+ target: BrowserTarget = BrowserTarget.DEBUG,) -> str:- """Fill ONE field of a SAVED credential into the live debug browser during code-only scouting.+ """Fill ONE field of a SAVED credential into a live browser during code-only scouting.++ `target="debug"` (the default) fills the browser this chat drives; `target="last_run"` fills the+ browser the most recent test run executed in, the same one `run_browser_code(target="last_run")` acts on.The secret value is resolved server-side from the stored credential and neverenters the conversation; the result reports only `typed_length`. Use this- instead of `type_text` whenever a login form field should receive a saved+ rather than typing the value yourself whenever a login form field should receive a savedcredential's username, password, or authenticator-app one-time code. Email/SMSOTP credentials are not filled during scouting because scouting has noworkflow run/task context for safe polling.@@ -1632,8 +1638,13 @@ async def fill_credential_field_tool(address string to `otp()` and ensure an active Gmail or Outlook connection existsfor that mailbox."""- result = await _fill_credential_field_impl(ctx.context, selector, credential_id, field, submit_selector)- return json.dumps(scrub_secrets_from_structure(ctx.context, result))+ binding = resolve_browser_session_binding(ctx.context, {"target": target.value})+ if binding.unavailable_reason:+ # Never fall back to the chat's browser: a credential filled there lands on a page the model did not name.+ return json.dumps({"ok": False, "error": binding.unavailable_reason, **binding.provenance()})+ with bound_call_browser_session(binding.session_id_override):+ result = await _fill_credential_field_impl(ctx.context, selector, credential_id, field, submit_selector)+ return json.dumps(scrub_secrets_from_structure(ctx.context, {**result, **binding.provenance()}))async def _inspect_locator_matches_invoke(ctx: RunContextWrapper, arguments: str) -> str:@@ -1735,8 +1746,10 @@ NATIVE_TOOLS = [inspect_locator_matches_tool,fill_credential_field_tool,request_credential_tool,+ run_browser_code_tool,]+# Native tools that cannot do their job without a browser: they dispatch a run, drive the# scouting tab, or read a live page. Membership is by hand because FunctionTool carries no# capability metadata; a new tool that touches a browser belongs here.@@ -1747,5 +1760,15 @@ BROWSER_BOUND_TOOL_NAMES = BLOCK_RUNNING_TOOLS | frozenset("inspect_page_for_composition",LOCATOR_INSPECTION_TOOL_NAME,"fill_credential_field",+ BROWSER_CODE_TOOL_NAME,})+++def copilot_native_tools(*, supports_question_tool: bool, browser_code_available: bool) -> list[FunctionTool]:+ return [+ tool+ for tool in NATIVE_TOOLS+ if (tool.name != "ask_user" or supports_question_tool)+ and (tool.name != BROWSER_CODE_TOOL_NAME or browser_code_available)+ ]diff --git a/skyvern/forge/sdk/copilot/tools/browser_code.py b/skyvern/forge/sdk/copilot/tools/browser_code.pynew file mode 100644index 000000000..2257eb7f7--- /dev/null+++ b/skyvern/forge/sdk/copilot/tools/browser_code.py@@ -0,0 +1,575 @@+"""Persistent Python browser code for the copilot, run in the deployment's isolated interpreter."""++from __future__ import annotations++import ast+import asyncio+import json+import math+import uuid+from collections.abc import Callable+from typing import Any++import structlog+from agents import FunctionTool+from agents.run_context import RunContextWrapper+from playwright.async_api import Page++from skyvern.cli.core.session_manager import get_page+from skyvern.forge import app+from skyvern.forge.sdk.copilot.browser_code_contract import (+ DEMONSTRATED_OPERATIONS,+ BrowserCodeCellResult,+ BrowserCodeHost,+ BrowserCodeOperation,+ BrowserCodeSession,+ BrowserCodeSessionUnavailableError,+)+from skyvern.forge.sdk.copilot.browser_target import (+ BROWSER_TARGET_PARAM,+ BROWSER_TARGET_PARAM_NAME,+ BrowserSessionBinding,+ resolve_browser_session_binding,+)+from skyvern.forge.sdk.copilot.enforcement import HARD_BACKSTOP_ALLOWANCE_SECONDS, TOTAL_TIMEOUT_SECONDS+from skyvern.forge.sdk.copilot.loop_detection import record_tool_step_result_for_ctx+from skyvern.forge.sdk.copilot.mcp_adapter import (+ _browser_call_outcome_from_mapping,+ _browser_session_error_disposition,+ _browser_session_loss_result,+ _prepare_browser_session_for_dispatch,+ _record_browser_call_outcome,+ scrub_model_facing_tool_result,+)+from skyvern.forge.sdk.copilot.runtime import (+ SENSITIVE_ORIGIN_ACTIVE_RUN_PAGE_ERROR,+ SENSITIVE_ORIGIN_PAGE_ERROR,+ AgentContext,+ CopilotBrowserGenerationRetired,+ CopilotBrowserSessionUnavailable,+ bound_call_browser_session,+ browser_evidence_commit_lock,+ browser_page_custody_lock,+ clear_sensitive_origin_page_taint,+ effective_browser_session_id,+ mcp_browser_context,+ navigation_replaced_document,+ sensitive_origin_page_facts_withheld,+ sensitive_origin_page_has_active_run,+ sensitive_origin_page_is_tainted,+)++from .guardrails import _authority_tool_error+from .scouting import _mark_pending_browser_interaction_observation, _record_scouted_interaction++LOG = structlog.get_logger()++TOOL_NAME = "run_browser_code"++# The live URL rides in every cell result, so an unreasonable one is omitted rather than returned.+CURRENT_URL_MAX_CHARS = 4096+DEFAULT_TIMEOUT_SECONDS = 60.0+MAX_TIMEOUT_SECONDS = 300.0+MAX_CODE_CHARS = 20_000+MAX_VALUE_CHARS = 32_000+SESSION_LIFETIME_SECONDS = float(TOTAL_TIMEOUT_SECONDS + HARD_BACKSTOP_ALLOWANCE_SECONDS)++TOOL_DESCRIPTION = """Run Python against the live browser in a persistent interpreter.++`target` names the browser: 'debug' (default) is the one this chat drives; 'last_run' is the one the+most recent test run executed in, when that run minted its own. A continuation on the page a run+stopped on needs 'last_run'; acting there changes that page and any submit it triggers is real.++Top-level `await` works. Variables, functions, and classes defined in one call stay available to later+run_browser_code calls in this chat turn. They are gone when the turn ends or when a result reports+`session_restarted` or `session_ended`. The value of a final bare expression is returned as `value`;+`print` output is returned as `stdout`.++Browser API (async, Playwright-shaped):+- `page` is the current tab. Locators: `page.locator(css)`, `page.get_by_role(role, name=...)`,+ `get_by_text`, `get_by_label`, `get_by_placeholder`, `.first`, `.last`, `.nth(i)`, `.filter(...)`.+- Actions: `.click()`, `.fill(text)`, `.press(key)`, `.check()`, `.select_option(...)`, `.hover()`,+ `page.keyboard.press(key)`.+- Waits: `page.wait_for_selector(...)`, `page.wait_for_url(...)`, `page.wait_for_load_state(...)`,+ `locator.wait_for(...)`.+- DOM reads: `.text_content()`, `.inner_text()`, `.get_attribute(name)`, `.input_value()`, `.count()`,+ `.is_visible()`, `page.content()`, `page.evaluate(js)`, `page.title()`, and `page.url`.+- Navigation: `page.goto(url)`, `page.go_back()`, `page.reload()`. Internal, private-network, and+ non-web destinations are refused.+- Frames: `page.frames`, `page.main_frame`, `page.frame_locator(css)`.+- After a sensitive sign-in on this page, screenshots and `page.evaluate` are refused for the rest of+ the turn. Reading text still works; that is the way to inspect such a page.+- Tabs and popups: each call starts on the browser's current tab, the one the direct browser tools act on.+ `await tabs()` lists open tabs; `await switch_tab(index)` makes that tab `page` for the rest of the call;+ `await click_and_wait_for_popup(selector)` clicks and returns the new tab's `index` and `url`.+- Downloads and files: `await click_and_download(selector)` clicks and returns `{file_id, name, size}`+ for the downloaded file; `await files.read(file_id)` returns its bytes; `await files.write(name, data)`+ stores text or bytes as a new file (at most 1 MiB of text or 768 KiB of bytes per call);+ `await files.upload(selector, [file_id, ...])` sets files on a file input (at most 50 MiB in total).+ Files belong to this chat turn.+- Saved credentials are not filled from here. Call `fill_credential_field` between calls to this tool,+ with the same `target`; every call starts on the tab that tool acts on.++Not available: imports, names starting with `_`, event listeners (`page.on`, `expect_*`), cookies,+request interception, and new browser contexts. Using one returns an error that says so.++Every result lists the browser operations the call sent, in order. A failed call returns the error,+the failing line, and the operations that already ran; nothing is retried. A call that exceeds+`timeout_seconds` (default 60, at most 300) or is cancelled stops the interpreter; when an operation+reached the browser without a reply, the result names it and the page must be read again before+acting on its state.++Limits: code up to 20,000 characters; `value` up to 32,000 characters of JSON and `stdout` up to 16 KB,+cut beyond that, so return or print a summary; the first 50 operations are listed; a chat turn holds at+most 32 files of 16 MB each."""++TOOL_SCHEMA: dict[str, Any] = {+ "type": "object",+ "properties": {+ "code": {"type": "string", "description": "Python source to run in the persistent interpreter."},+ "timeout_seconds": {+ "type": "number",+ "exclusiveMinimum": 0,+ "maximum": MAX_TIMEOUT_SECONDS,+ "description": "Wall-clock limit for this call, in seconds.",+ },+ BROWSER_TARGET_PARAM_NAME: BROWSER_TARGET_PARAM,+ },+ "required": ["code"],+ "additionalProperties": False,+}+++SENSITIVE_ORIGIN_RECOVERY_HINT = (+ ' On this surface that navigation is a cell containing only `await page.goto("<url>")`; it returns the'+ " resulting URL and nothing else."+)+++def _navigation_only_cell_url(code: str) -> str | None:+ """The URL when the cell is exactly one ``await page.goto("<literal>")``, else None.++ This is the one cell shape a withheld page accepts: it can carry nothing off the page, and a+ successful fresh navigation is what lifts the withholding, as it does for the direct tool.+ """+ try:+ tree = ast.parse(code)+ except SyntaxError:+ return None+ if len(tree.body) != 1 or not isinstance(tree.body[0], ast.Expr) or not isinstance(tree.body[0].value, ast.Await):+ return None+ call = tree.body[0].value.value+ if not (+ isinstance(call, ast.Call)+ and isinstance(call.func, ast.Attribute)+ and call.func.attr == "goto"+ and isinstance(call.func.value, ast.Name)+ and call.func.value.id == "page"+ and not call.keywords+ and len(call.args) == 1+ and isinstance(call.args[0], ast.Constant)+ and isinstance(call.args[0].value, str)+ ):+ return None+ return call.args[0].value+++def _recovery_navigated(cell: BrowserCodeCellResult) -> str | None:+ """The URL a tainted page was replaced by, from the worker's own record, else None.++ The cell's text proves nothing: a persisted rebinding of `page.goto` runs no operation at all.+ Only the broker's record of exactly one successful `goto` whose document differs from the one it+ started on shows that the sensitive document is gone.+ """+ if not cell.ok or cell.operations_omitted or len(cell.operations) != 1:+ return None+ op = cell.operations[0]+ if op.operation != "goto" or op.succeeded is not True or not op.source_url or not op.result_url:+ return None+ if not navigation_replaced_document(op.source_url, op.result_url):+ return None+ return op.result_url+++def _timeout_seconds(value: object) -> float | None:+ if value is None:+ return DEFAULT_TIMEOUT_SECONDS+ if isinstance(value, bool) or not isinstance(value, int | float):+ return None+ if not math.isfinite(value) or not 0 < value <= MAX_TIMEOUT_SECONDS:+ return None+ return float(value)+++def _operation_fact(operation: BrowserCodeOperation) -> dict[str, Any]:+ status = "ok" if operation.succeeded else "failed" if operation.succeeded is False else "no_reply"+ fact: dict[str, Any] = {"operation": operation.operation, "status": status}+ if operation.selector:+ fact["selector"] = operation.selector+ if operation.error:+ fact["error"] = operation.error+ return fact+++def _cell_payload(cell: BrowserCodeCellResult) -> dict[str, Any]:+ payload: dict[str, Any] = {"ok": cell.ok}+ # A page can push a URL of any length, and this one is copied into every result.+ if cell.current_url and len(cell.current_url) <= CURRENT_URL_MAX_CHARS:+ payload["current_url"] = cell.current_url+ if cell.ok and cell.value is not None:+ rendered = json.dumps(cell.value, default=str)+ if len(rendered) > MAX_VALUE_CHARS:+ LOG.info("copilot_browser_code_value_truncated", value_chars=len(rendered), limit_chars=MAX_VALUE_CHARS)+ payload["value"] = rendered[:MAX_VALUE_CHARS]+ payload["value_truncated"] = (+ f"value was cut at {MAX_VALUE_CHARS} characters; return a smaller value or print a summary"+ )+ else:+ payload["value"] = cell.value+ if cell.stdout:+ payload["stdout"] = cell.stdout+ if cell.stdout_truncated:+ payload["stdout_truncated"] = True+ if not cell.ok:+ payload["error"] = cell.error+ payload["error_code"] = cell.error_code+ if cell.failing_line is not None:+ payload["failing_line"] = cell.failing_line+ payload["operations"] = [_operation_fact(operation) for operation in cell.operations]+ if cell.operations_omitted:+ payload["operations_omitted"] = cell.operations_omitted+ if not cell.session_alive:+ payload["session_ended"] = "the interpreter stopped; values and functions from earlier calls are gone"+ return payload+++def _take_interruption_note(host: BrowserCodeHost) -> dict[str, Any]:+ if not host.interrupted:+ return {}+ operation = host.interrupted_operation+ host.interrupted = False+ host.interrupted_operation = None+ note: dict[str, Any] = {"state": "the previous run_browser_code call was cancelled and its interpreter stopped"}+ if operation is not None:+ note["last_operation"] = _operation_fact(operation)+ if operation.succeeded is None:+ note["page_state"] = "unknown: that operation reached the browser without a reply"+ return {"previous_call_interrupted": note}+++async def _discard_session(host: BrowserCodeHost) -> None:+ session, host.session = host.session, None+ if session is None:+ return+ LOG.info("Copilot browser code session closing", browser_code_session_id=session.session_id)+ try:+ await session.close()+ except Exception:+ LOG.warning(+ "Failed to close the copilot browser code session",+ browser_code_session_id=session.session_id,+ exc_info=True,+ )+++async def _session_for_page(+ copilot_ctx: AgentContext,+ page: Page,+ *,+ browser_session_id: str | None,+ generation: int,+ notes: dict[str, Any],+) -> BrowserCodeSession:+ host = copilot_ctx.browser_code_host+ session = host.session+ if session is not None and host.browser_session_id != browser_session_id:+ # A rebind moves the interpreter to a tab, not to a different browser. The worker resolves tabs+ # against the browser session it was started with, so a replaced one has to reopen.+ await _discard_session(host)+ session = None+ if session is not None and (+ session.page is not page+ or (host.browser_session_id, host.browser_session_generation) != (browser_session_id, generation)+ ):+ try:+ await session.rebind(page)+ except BrowserCodeSessionUnavailableError:+ await _discard_session(host)+ raise+ notes["handles_invalidated"] = (+ "the browser tab or session changed; page, locator, and frame objects from earlier calls no longer "+ "work, other values are kept"+ )+ elif session is None:+ session = await app.AGENT_FUNCTION.open_copilot_browser_code_session(+ page=page,+ lifetime_seconds=SESSION_LIFETIME_SECONDS,+ organization_id=copilot_ctx.organization_id,+ chat_id=copilot_ctx.workflow_permanent_id,+ turn_id=host.turn_key,+ browser_session_id=browser_session_id,+ )+ host.session = session+ LOG.info(+ "Copilot browser code session opened",+ browser_code_session_id=session.session_id,+ organization_id=copilot_ctx.organization_id,+ workflow_permanent_id=copilot_ctx.workflow_permanent_id,+ )+ if host.sessions_opened:+ notes["session_restarted"] = "this call started a new interpreter; values from earlier calls are gone"+ host.sessions_opened += 1+ host.browser_session_id = browser_session_id+ host.browser_session_generation = generation+ return session+++async def _run_cell(+ host: BrowserCodeHost, session: BrowserCodeSession, code: str, timeout: float, *, deny_pixels: bool = False+) -> BrowserCodeCellResult:+ try:+ return await session.run_cell(code, timeout_seconds=timeout, deny_pixels=deny_pixels)+ except asyncio.CancelledError:+ host.interrupted = True+ host.interrupted_operation = session.last_dispatched_operation()+ await asyncio.shield(_discard_session(host))+ raise+++def _was_demonstrated(operation: BrowserCodeOperation) -> bool:+ """Whether this action left something the workbench can promote.++ A handle-backed locator has no selector, so nothing is recorded for it, and the evidence that+ follows must not then be credited to an interaction there is no saved form of.+ """+ return bool(operation.succeeded and operation.selector and operation.operation in DEMONSTRATED_OPERATIONS)+++def _record_outcome(+ copilot_ctx: AgentContext,+ cell: BrowserCodeCellResult,+ result: dict[str, Any],+ *,+ browser_session_id: str | None,+ generation: int,+) -> None:+ _record_browser_call_outcome(+ copilot_ctx,+ _browser_call_outcome_from_mapping(+ raw_tool_name=TOOL_NAME,+ source_browser_session_id=browser_session_id,+ source_browser_session_generation=generation,+ raw_result=result,+ ),+ call_path="model",+ )+ recorded = [operation for operation in cell.actions if _was_demonstrated(operation)]+ for operation in recorded:+ typed = operation.input_value or ""+ _record_scouted_interaction(+ copilot_ctx,+ tool_name=f"{TOOL_NAME}.{operation.operation}",+ selector=operation.selector or "",+ source_url=operation.source_url,+ result_url=operation.result_url,+ input_id=f"input_{uuid.uuid4().hex}" if typed else "",+ input_value=typed,+ typed_length=len(typed),+ )+ if recorded:+ _mark_pending_browser_interaction_observation(+ copilot_ctx, tool_name=f"{TOOL_NAME}.{recorded[-1].operation}", url=cell.current_url or ""+ )+++async def run_browser_code(+ copilot_ctx: AgentContext, code: object, timeout_seconds: object = None, target: object = None+) -> dict[str, Any]:+ arguments: dict[str, Any] = {"code": code, "timeout_seconds": timeout_seconds, BROWSER_TARGET_PARAM_NAME: target}++ def finish(result: dict[str, Any]) -> dict[str, Any]:+ scrubbed = scrub_model_facing_tool_result(copilot_ctx, result)+ record_tool_step_result_for_ctx(copilot_ctx, TOOL_NAME, arguments, scrubbed)+ return scrubbed++ authority_error = _authority_tool_error(copilot_ctx, TOOL_NAME)+ if authority_error:+ return finish({"ok": False, "error": authority_error})+ if not isinstance(code, str) or not code.strip():+ return finish({"ok": False, "error": "code must be non-empty Python source."})+ if len(code) > MAX_CODE_CHARS:+ return finish({"ok": False, "error": f"code is limited to {MAX_CODE_CHARS} characters."})+ timeout = _timeout_seconds(timeout_seconds)+ if timeout is None:+ return finish(+ {"ok": False, "error": f"timeout_seconds must be a number above 0 and at most {MAX_TIMEOUT_SECONDS:g}."}+ )+ binding = resolve_browser_session_binding(copilot_ctx, {BROWSER_TARGET_PARAM_NAME: target})+ if binding.unavailable_reason:+ return finish({"ok": False, "error": binding.unavailable_reason, **binding.provenance()})+ # The binding scopes everything below: preparation, the lease, the page lookup and the+ # interpreter all read the targeted browser rather than the chat's.+ with bound_call_browser_session(binding.session_id_override):+ return await _run_bound_cell(copilot_ctx, code, timeout, finish, binding)+++async def _run_bound_cell(+ copilot_ctx: AgentContext,+ code: str,+ timeout: float,+ finish: Callable[[dict[str, Any]], dict[str, Any]],+ binding: BrowserSessionBinding,+) -> dict[str, Any]:+ err, continuity_result, _disposition = await _prepare_browser_session_for_dispatch(+ copilot_ctx,+ tool_name=TOOL_NAME,+ call_path="model",+ observed_generation=copilot_ctx.browser_session_continuity_generation,+ )+ session_failure = err if err is not None else continuity_result+ if session_failure is not None:+ return finish(session_failure)+ host = copilot_ctx.browser_code_host+ # The same three, in the same order, as the direct credential fill takes. No cell fills a credential+ # any more, but that tool types into this browser between calls, and these are what stop a cell+ # driving the page while it does — which is what makes "every call starts on the tab that tool acts+ # on" a fact rather than the usual case.+ async with (+ copilot_ctx.credential_fill_lock,+ browser_page_custody_lock(copilot_ctx),+ browser_evidence_commit_lock(copilot_ctx),+ ):+ notes = _take_interruption_note(host)+ run_id = copilot_ctx.last_run_blocks_workflow_run_id+ recovering = False+ if sensitive_origin_page_facts_withheld(copilot_ctx, run_id):+ if sensitive_origin_page_has_active_run(copilot_ctx):+ return finish({"ok": False, "error": SENSITIVE_ORIGIN_ACTIVE_RUN_PAGE_ERROR, **notes})+ if _navigation_only_cell_url(code) is None:+ return finish(+ {"ok": False, "error": SENSITIVE_ORIGIN_PAGE_ERROR + SENSITIVE_ORIGIN_RECOVERY_HINT, **notes}+ )+ # The recovery runs in a fresh interpreter: a `page.goto` rebound before the page turned+ # sensitive could otherwise read the withheld page's URL, which the recorder does not log,+ # navigate once so the recovery passes, and hand the value to a later cell.+ await _discard_session(host)+ notes["session_ended"] = "the interpreter stopped; values and functions from earlier calls are gone"+ recovering = True+ browser_session_id = effective_browser_session_id(copilot_ctx)+ # The continuity generation counts re-establishments of the chat's own browser. A cell pinned to+ # another browser does not follow it, so a debug re-establishment must not rebind that cell's+ # interpreter; a real tab change there is still caught by the page identity check.+ generation = copilot_ctx.browser_session_continuity_generation if binding.session_id_override is None else 0+ try:+ # The lease is held for the whole cell so a browser retirement cancels it the way it cancels a+ # direct tool, instead of letting the cell act on a closing browser for up to its timeout.+ async with mcp_browser_context(copilot_ctx, session_id_override=binding.session_id_override):+ current_page, _browser_context = await get_page(session_id=browser_session_id)+ session = await _session_for_page(+ copilot_ctx,+ current_page.page,+ browser_session_id=browser_session_id,+ generation=generation,+ notes=notes,+ )+ # A page this turn visited a sensitive origin on stays tainted after its facts stop+ # being withheld. Scrubbing works on values, and pixels are not values, so a cell that+ # can screenshot it can carry the page out of here a chunk at a time.+ pixels_denied = sensitive_origin_page_is_tainted(copilot_ctx)+ cell = await _run_cell(host, session, code, timeout, deny_pixels=pixels_denied)+ except BrowserCodeSessionUnavailableError as exc:+ if exc.session_ended:+ # Dropped now, or the retry this refusal asks for is spent rediscovering that the+ # session is over, and only the call after that reopens one.+ await _discard_session(host)+ refusal: dict[str, Any] = {"ok": False, "error": str(exc), "error_code": exc.error_code, **notes}+ if exc.retry_after_seconds is not None:+ refusal["retry_after_seconds"] = exc.retry_after_seconds+ return finish(refusal)+ except (CopilotBrowserGenerationRetired, CopilotBrowserSessionUnavailable) as exc:+ disposition = await _browser_session_error_disposition(+ copilot_ctx, exc, tool_name=TOOL_NAME, call_path="model"+ )+ return finish(+ _browser_session_loss_result(+ dict(notes),+ disposition=disposition,+ deadline_expired=copilot_ctx.browser_session_continuity_deadline_expired,+ )+ )+ if cell.interpreter_restarted:+ # The host this call holds is unchanged, so a restart on the far side is invisible here+ # unless it is said. Claiming a continuity the interpreter cannot honour is the failure.+ notes["session_restarted"] = "this call started a new interpreter; values from earlier calls are gone"+ if cell.handles_invalidated:+ # A move this side asked for inside the call — after dropping a tab report it could not+ # confirm — drops the cell's handles the same as one the model asked for, and is said the same way.+ notes.setdefault(+ "handles_invalidated",+ "the browser tab or session changed; page, locator, and frame objects from earlier calls no longer "+ "work, other values are kept",+ )+ if not cell.session_alive:+ # A session that ended under the cell can return a result with no operations at all — a+ # fallback written over one that had landed. The session still knows what last reached the+ # browser; said now, before it is discarded, or the model retries an action that happened.+ last = session.last_dispatched_operation()+ if last is not None and not cell.operations:+ ended: dict[str, Any] = {"last_operation": _operation_fact(last)}+ if last.succeeded is None:+ ended["page_state"] = "unknown: that operation reached the browser without a reply"+ notes["session_ended_during_call"] = ended+ await _discard_session(host)+ if recovering:+ replaced_by = _recovery_navigated(cell)+ # A failed recovery leaves the page on the sensitive origin, so not even its URL is returned;+ # a successful one returns only the new document's URL, never a value or stdout.+ if replaced_by is None:+ return finish(+ {"ok": False, "error": SENSITIVE_ORIGIN_PAGE_ERROR + SENSITIVE_ORIGIN_RECOVERY_HINT, **notes}+ )+ clear_sensitive_origin_page_taint(copilot_ctx)+ return finish({"ok": True, "current_url": replaced_by, **notes})+ if sensitive_origin_page_facts_withheld(copilot_ctx, run_id):+ operations_sent = len(cell.operations) + cell.operations_omitted+ return finish(+ {+ "ok": False,+ "error": SENSITIVE_ORIGIN_PAGE_ERROR + SENSITIVE_ORIGIN_RECOVERY_HINT,+ "operations_sent": operations_sent,+ **notes,+ }+ )+ if pixels_denied and _recovery_navigated(cell) is not None:+ # On the replace surface no other tool navigates, so without this a finished run's taint+ # denies pixels on this browser for the rest of the chat.+ clear_sensitive_origin_page_taint(copilot_ctx)+ result = finish({**_cell_payload(cell), **notes, **binding.provenance()})+ _record_outcome(copilot_ctx, cell, result, browser_session_id=browser_session_id, generation=generation)+ return result+++async def close_browser_code_session(copilot_ctx: AgentContext) -> None:+ await _discard_session(copilot_ctx.browser_code_host)+++async def _run_browser_code_invoke(ctx: RunContextWrapper, arguments: str) -> str:+ try:+ parsed = json.loads(arguments) if arguments else {}+ except json.JSONDecodeError:+ parsed = {}+ if not isinstance(parsed, dict):+ parsed = {}+ result = await run_browser_code(+ ctx.context, parsed.get("code"), parsed.get("timeout_seconds"), parsed.get(BROWSER_TARGET_PARAM_NAME)+ )+ return json.dumps(result, default=str)+++run_browser_code_tool = FunctionTool(+ name=TOOL_NAME,+ description=TOOL_DESCRIPTION,+ params_json_schema=TOOL_SCHEMA,+ on_invoke_tool=_run_browser_code_invoke,+ strict_json_schema=False,+)diff --git a/skyvern/forge/sdk/copilot/tools/composition_capture.py b/skyvern/forge/sdk/copilot/tools/composition_capture.pyindex 41a5e1f6a..de2d5f196 100644--- a/skyvern/forge/sdk/copilot/tools/composition_capture.py+++ b/skyvern/forge/sdk/copilot/tools/composition_capture.py@@ -11,6 +11,8 @@ from typing import Anyfrom urllib.parse import urlparseimport structlog+from agents import FunctionTool+from agents.tool_context import ToolContextfrom skyvern.forge.sdk.copilot.challenge_evidence import (CHALLENGE_FRAMES_KEY,@@ -53,11 +55,12 @@ from skyvern.forge.sdk.copilot.runtime import (bound_call_browser_session,browser_evidence_commit_lock,browser_page_custody_lock,- clear_sensitive_origin_page_taint,+ clear_sensitive_origin_page_taint_after_navigation,effective_browser_session_id,resolve_browser_state_for_context,sensitive_origin_page_facts_withheld,sensitive_origin_page_has_active_run,+ sensitive_origin_page_is_tainted,)from skyvern.forge.sdk.copilot.runtime_authoring_repair import (finalize_runtime_authoring_repair_context_from_page_observation,@@ -98,6 +101,7 @@ from .mcp_hooks import _bind_login_credential_for_observed_url, _record_scouted_from .scouting import (_clear_pending_browser_interaction_observation,_consume_pending_browser_interaction_observation,+ _live_working_page_url,_mark_post_run_page_observed,)@@ -1286,6 +1290,11 @@ async def _inspect_page_for_composition_under_custody()evidence, observation_error, visual_fallback_frame = _capture_result_parts(capture)else:+ # Read, compared and dropped: a withheld page's URL decides whether the navigation left+ # it, and is never part of the evidence.+ taint_source_url = (+ await _live_working_page_url(copilot_ctx) if sensitive_origin_page_is_tainted(copilot_ctx) else None+ )nav_result = await _discovery_navigate(copilot_ctx,entry_url,@@ -1318,7 +1327,13 @@ async def _inspect_page_for_composition_under_custody(current_url = str(evidence.get("current_url") or entry_url)else:current_url = _discovery_extract_current_url(nav_result, entry_url)- clear_sensitive_origin_page_taint(copilot_ctx)+ if taint_source_url is not None:+ # Raw against raw: the navigate result's URL is secret-scrubbed, the before-URL is not.+ clear_sensitive_origin_page_taint_after_navigation(+ copilot_ctx,+ source_url=taint_source_url,+ result_url=await _live_working_page_url(copilot_ctx),+ )capture = await _capture_composition_evidence(copilot_ctx,inspected_url=entry_url,@@ -1495,3 +1510,69 @@ def _attach_author_time_levers(copilot_ctx: Any, evidence: dict[str, Any]) -> Nochallenge_state["levers"] = [lever.model_dump(mode="json", exclude_none=True) for lever in author_time_levers(copilot_ctx)]+++COMPOSITION_INSPECTION_TOOL_NAME = "inspect_page_for_composition"+CURRENT_PAGE_INSPECTION_TARGET = "current_page"+_CURRENT_PAGE_INSPECTION_DESCRIPTION = """Inspect the page the browser is currently on before composing workflow blocks.++This is a bounded read of current page state, not a way to reach a page: it cannot navigate, and+it observes whichever page the browser already shows. Use it for uncertainty about controls,+selectors, visible state or layout, before authoring blocks that fill fields, submit searches,+filter results or expand result rows, and after a run to read the page that run stopped on.+`target="debug"` (the default) reads the browser this chat drives; `target="last_run"` reads the+browser used by the most recent test run. The packet describes the page only as it is at that+moment: a control that appears solely after an interaction -- a Delete control after an Add click,+a cart after add-to-cart, the secure area after login -- is absent from it until that interaction+has happened.++Returns observed page evidence: current URL, title, navigation targets, form fields with labels and+selectors, submit/search controls, result containers, compact visible text excerpts, anti-bot+indicators, and bounded visual challenge evidence when DOM evidence shows challenge state. The+returned `observation_step` is the side-channel id to pass in `block_observation_refs` when a newly+authored block acts on this observed page. Do NOT paste the evidence into workflow YAML; use it to+ground concise block prompts. If a select reports `options_omitted=true`, `option_count` is the+observed total and its selector remains available. If those options are needed, read that one select+from browser code; do not repeat the full-page inspection. If a block run changes pages, inspect the+reached page before authoring downstream form/search/result blocks. If the evidence shows required+fields or controls that the user did not supply enough information for, ASK_QUESTION with that+observed missing input. If evidence is sufficient, compose and run workflow blocks from the observed+fields. `challenge_state` reports what the page looks like, which is not what a run will do: it does+not establish that a submit/search path is closed, and a run settles that.++When the page visibly shows a requested output but its markup is unclear, pass+`requested_output_reads` with the `output_path` your block will return, the exact rendered+`value_text`, and its visible `label`. The browser verifies the designation and returns every+observed selector candidate with its cardinality as facts; you remain responsible for choosing a+selector and authoring the workflow read."""+++def current_page_inspection_tool(tool: FunctionTool) -> FunctionTool:+ """The URL target is dropped from the advertised schema and forced at dispatch, so no+ navigation is reachable through this tool even if a model supplies the argument anyway."""+ schema = copy.deepcopy(tool.params_json_schema)+ properties = schema.get("properties")+ if isinstance(properties, dict):+ properties.pop("target_url", None)+ required = schema.get("required")+ if isinstance(required, list):+ schema["required"] = [name for name in required if name != "target_url"]+ navigating_invoke = tool.on_invoke_tool++ # The agents runner hands a wrapper annotated with RunContextWrapper a forked bare context, and+ # the delegate's generated invoke reads tool_name off it; only a ToolContext annotation keeps it.+ async def invoke_on_current_page(ctx: ToolContext[CopilotContext], arguments: str) -> Any:+ try:+ parsed = json.loads(arguments) if arguments else {}+ except ValueError:+ return await navigating_invoke(ctx, arguments)+ if not isinstance(parsed, dict):+ parsed = {}+ parsed["target_url"] = CURRENT_PAGE_INSPECTION_TARGET+ return await navigating_invoke(ctx, json.dumps(parsed))++ projected = copy.copy(tool)+ projected.description = _CURRENT_PAGE_INSPECTION_DESCRIPTION+ projected.params_json_schema = schema+ projected.on_invoke_tool = invoke_on_current_page+ return projecteddiff --git a/skyvern/forge/sdk/copilot/tools/credential_fill.py b/skyvern/forge/sdk/copilot/tools/credential_fill.pyindex cebe26d2d..02b94a06f 100644--- a/skyvern/forge/sdk/copilot/tools/credential_fill.py+++ b/skyvern/forge/sdk/copilot/tools/credential_fill.py@@ -38,6 +38,7 @@ from skyvern.forge.sdk.copilot.runtime import (ScoutedSelectorCandidate,browser_evidence_commit_lock,browser_page_custody_lock,+ effective_browser_session_id,ensure_browser_session,mcp_browser_context,sensitive_origin_page_facts_withheld,@@ -793,8 +794,9 @@ async def _submit_after_credential_fill(error_text: str | None = Noneclicked = Falsetry:- async with mcp_browser_context(copilot_ctx):- page, _ = await get_page(session_id=copilot_ctx.browser_session_id)+ session_id = effective_browser_session_id(copilot_ctx)+ async with mcp_browser_context(copilot_ctx, session_id_override=session_id):+ page, _ = await get_page(session_id=session_id)engine_selection = page.engine_selectiontry:await page.click(probe.selector, mode="direct", timeout=_CREDENTIAL_SUBMIT_TIMEOUT_MS)@@ -901,9 +903,11 @@ async def _fill_credential_field_impl_serial()return finish({"ok": False, "error": policy_error or _missing_credential_origin_error(credential_id, None)})- session_error = await ensure_browser_session(copilot_ctx)- if session_error:- return finish(session_error)+ # A fill aimed at a run's own browser must not provision the chat's; that browser is checked by the fill.+ if effective_browser_session_id(copilot_ctx) == copilot_ctx.browser_session_id:+ session_error = await ensure_browser_session(copilot_ctx)+ if session_error:+ return finish(session_error)if sensitive_origin_page_facts_withheld(copilot_ctx, origin_run_id):return finish({"ok": False, "error": SENSITIVE_ORIGIN_PAGE_ERROR})await _capture_scout_source_url(copilot_ctx)@@ -942,8 +946,9 @@ async def _fill_credential_field_impl_serial(return finish(error_result)fill_outcome: ScoutReadbackOutcome | None = Nonetry:- async with mcp_browser_context(copilot_ctx):- page, _ = await get_page(session_id=copilot_ctx.browser_session_id)+ session_id = effective_browser_session_id(copilot_ctx)+ async with mcp_browser_context(copilot_ctx, session_id_override=session_id):+ page, _ = await get_page(session_id=session_id)await page.fill(selector,value,diff --git a/skyvern/forge/sdk/copilot/tools/frontier.py b/skyvern/forge/sdk/copilot/tools/frontier.pyindex 4b210d71e..f3193a1af 100644--- a/skyvern/forge/sdk/copilot/tools/frontier.py+++ b/skyvern/forge/sdk/copilot/tools/frontier.py@@ -2,10 +2,12 @@from __future__ import annotations+import astimport jsonimport re+import textwrapfrom collections import Counter-from typing import Any+from typing import Any, Protocol, runtime_checkablefrom urllib.parse import urlparseimport structlog@@ -651,9 +653,48 @@ def _nearest_upstream_state_establisher(return None+@runtime_checkable+class _HasCode(Protocol):+ code: str | None+++def _code_block_opens_on_static_url(block: _HasCode) -> bool:+ code = block.code+ if not isinstance(code, str):+ return False+ try:+ body = ast.parse(textwrap.dedent(code)).body+ except (SyntaxError, ValueError, RecursionError, MemoryError):+ return False+ if not body:+ return False+ first = body[0]+ if not (isinstance(first, ast.Expr) and isinstance(first.value, ast.Await)):+ return False+ call = first.value.value+ if not (+ isinstance(call, ast.Call)+ and isinstance(call.func, ast.Attribute)+ and call.func.attr == "goto"+ and isinstance(call.func.value, ast.Name)+ and call.func.value.id == "page"+ ):+ return False+ url_node = call.args[0] if call.args else next((kw.value for kw in call.keywords if kw.arg == "url"), None)+ if not (isinstance(url_node, ast.Constant) and isinstance(url_node.value, str)):+ return False+ # CodeBlock.code is Jinja-templated before it runs, so a templated literal is a parameter URL.+ if "{{" in url_node.value or "{%" in url_node.value:+ return False+ return _valid_runtime_anchor_url(url_node.value) is not None++def _block_can_start_browser_run(block: object) -> bool:- if _block_type_name(block) == BlockType.GOTO_URL.value:+ block_type = _block_type_name(block)+ if block_type == BlockType.GOTO_URL.value:return True+ if block_type == BlockType.CODE.value:+ return isinstance(block, _HasCode) and _code_block_opens_on_static_url(block)return _valid_runtime_anchor_url(getattr(block, "url", None)) is not Nonediff --git a/skyvern/forge/sdk/copilot/tools/guardrails.py b/skyvern/forge/sdk/copilot/tools/guardrails.pyindex f7de50618..9216ed5a2 100644--- a/skyvern/forge/sdk/copilot/tools/guardrails.py+++ b/skyvern/forge/sdk/copilot/tools/guardrails.py@@ -170,6 +170,7 @@ def _authority_tool_error("edit_block_and_run","discover_workflow_entrypoint","search_web",+ "run_browser_code",}and isinstance(policy, RequestPolicy)and policy.raw_secret_detecteddiff --git a/skyvern/forge/sdk/copilot/tools/mcp_hooks.py b/skyvern/forge/sdk/copilot/tools/mcp_hooks.pyindex 6c4dadf06..6f27ccf52 100644--- a/skyvern/forge/sdk/copilot/tools/mcp_hooks.py+++ b/skyvern/forge/sdk/copilot/tools/mcp_hooks.py@@ -21,7 +21,6 @@ from skyvern.forge.sdk.copilot.config import (download_scout_act_required_for_policy,normalize_block_authoring_policy,)-from skyvern.forge.sdk.copilot.context import CopilotContextfrom skyvern.forge.sdk.copilot.credential_resolution import is_resolved_page_url, load_credentialsfrom skyvern.forge.sdk.copilot.enforcement import (requested_output_paths_for_derivation,@@ -54,7 +53,8 @@ from skyvern.forge.sdk.copilot.runtime import (AgentContext,ScoutedInteraction,ScoutedSelectorCandidate,- clear_sensitive_origin_page_taint,+ clear_sensitive_origin_page_taint_after_navigation,+ effective_browser_session_id,sensitive_origin_page_facts_withheld,sensitive_origin_page_has_active_run,sensitive_origin_page_is_tainted,@@ -114,6 +114,7 @@ from .scouting import (_clear_pending_scout_selector_facts,_close_scout_challenge_baseline,_consume_scout_source_url,+ _live_working_page_url,_mark_pending_browser_interaction_observation,_maybe_attach_observed_challenge,_maybe_attach_observed_download_target,@@ -669,25 +670,6 @@ def _demonstrated_step_facts(ctx: AgentContext) -> list[dict[str, Any]]:return [scrub(fact) for fact in facts]-def _code_only_pre_run_results_error(ctx: CopilotContext) -> dict[str, Any] | None:- if _copilot_block_authoring_policy(ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:- return None- if ctx.workflow_persisted or ctx.update_workflow_called:- return None- for value in (ctx.last_run_blocks_workflow_run_id, ctx.last_successful_run_blocks_workflow_run_id):- if isinstance(value, str) and value:- return None- return {- "ok": False,- "error": (- "CODE-ONLY EXPLORATION PHASE: get_run_results is unavailable before a real workflow run exists. "- "Use MCP browser tools such as navigate_browser, evaluate, click, type_text, get_browser_screenshot, "- "console_messages, scroll, select_option, or press_key to understand the page, then call "- "update_and_run_blocks with real focused code blocks."- ),- }--async def _evaluate_pre_hook(params: dict[str, Any],ctx: AgentContext,@@ -889,12 +871,19 @@ async def _navigate_post_hook(_clear_pending_browser_interaction_observation(ctx)sensitive_origin_page_was_tainted = sensitive_origin_page_is_tainted(ctx)captured_source_url = _consume_scout_source_url(ctx)+ taint_source_url = ctx.pending_taint_source_urls.pop(effective_browser_session_id(ctx) or "", None)source_url = None if sensitive_origin_page_was_tainted else captured_source_urlif result.get("ok"):data = result.pop("data", {})result["url"] = data.get("url", "")- if sensitive_origin_page_was_tainted:- clear_sensitive_origin_page_taint(ctx)+ # Raw against raw: `result["url"]` is already secret-scrubbed, so against the raw before-URL a+ # fragment hop on a page whose URL holds a registered value would differ only by the redaction.+ if sensitive_origin_page_was_tainted and not clear_sensitive_origin_page_taint_after_navigation(+ ctx, source_url=taint_source_url, result_url=await _live_working_page_url(ctx)+ ):+ # Still on the withheld document, so not even its URL goes back; the code tool and the+ # navigating inspection refuse the same way.+ return {"ok": False, "error": SENSITIVE_ORIGIN_PAGE_ERROR}_record_scouted_interaction(ctx,tool_name="navigate_browser",@@ -930,6 +919,10 @@ async def _navigate_pre_hook(return {"ok": False, "error": SENSITIVE_ORIGIN_ACTIVE_RUN_PAGE_ERROR}if sensitive_origin_page_is_tainted(ctx):ctx.pending_scout_source_url = None+ session_id = effective_browser_session_id(ctx)+ source_url = await _live_working_page_url(ctx)+ if session_id and isinstance(source_url, str):+ ctx.pending_taint_source_urls[session_id] = source_urlreturn Noneawait _capture_scout_source_url(ctx)return Nonediff --git a/skyvern/forge/sdk/copilot/tools/run_execution.py b/skyvern/forge/sdk/copilot/tools/run_execution.pyindex 95cca473e..4968609d3 100644--- a/skyvern/forge/sdk/copilot/tools/run_execution.py+++ b/skyvern/forge/sdk/copilot/tools/run_execution.py@@ -459,6 +459,19 @@ def _log_detached_cleanup_failure(task: asyncio.Task) -> None:LOG.warning("Detached cancel fallback failed", exc_info=exc)+def resolve_copilot_run_lane(*, rollout_dispatches: bool, inline_opt_in: bool) -> tuple[bool, bool]:+ """Return (dispatch_to_worker, allow_inline_code_execution) for one copilot test run.++ The rollout decides where a run *may* execute; an explicit inline opt-in decides where this one+ does. Honouring the opt-in under an enrolled runner is what lets a developer stack exercise a+ workflow whose target is host-local, which the runner's egress guard refuses. Only a developer+ checkout reaches it: the hook returns False for cloud and packaged deployments.+ """+ if inline_opt_in:+ return False, True+ return rollout_dispatches, False++def _copilot_sandbox_unavailable_result(*, organization_id: str, workflow_permanent_id: str) -> dict[str, Any]:# No repair can make the sandbox reachable, so the result carries# UNRECOVERABLE_TOOL_ERROR to reach the contract's STOP lane. Without it the@@ -3239,10 +3252,11 @@ async def _run_blocks_and_collect_debug(organization_id=ctx.organization_id,workflow_permanent_id=ctx.workflow_permanent_id,)- # Compared against the literal True so anything other than an explicit opt-in — including a- # test double that auto-mocks the hook into a truthy object — still fails closed.- allow_inline_code_execution = (- app.AGENT_FUNCTION.allow_copilot_inline_code_execution() is True if not dispatch_to_worker else False+ dispatch_to_worker, allow_inline_code_execution = resolve_copilot_run_lane(+ rollout_dispatches=dispatch_to_worker,+ # Compared against the literal True so anything other than an explicit opt-in — including a+ # test double that auto-mocks the hook into a truthy object — still fails closed.+ inline_opt_in=app.AGENT_FUNCTION.allow_copilot_inline_code_execution() is True,)if requires_sandbox and not dispatch_to_worker and not allow_inline_code_execution:return _copilot_sandbox_unavailable_result(diff --git a/skyvern/forge/sdk/copilot/unrecoverable_tool_error.py b/skyvern/forge/sdk/copilot/unrecoverable_tool_error.pyindex 9d374ffa5..605614487 100644--- a/skyvern/forge/sdk/copilot/unrecoverable_tool_error.py+++ b/skyvern/forge/sdk/copilot/unrecoverable_tool_error.py@@ -42,6 +42,7 @@ _BROWSER_SESSION_TOOL_NAMES = frozenset("skyvern_frame_list","skyvern_frame_switch","skyvern_frame_main",+ "run_browser_code",})_UNRECOVERABLE_TOOL_ERROR_CATEGORY = "UNRECOVERABLE_TOOL_ERROR"@@ -85,6 +86,10 @@ def _unrecoverable_tool_error_reason(output: dict[str, Any]) -> str:def _is_unrecoverable_browser_session_error(tool_name: str, output: dict[str, Any]) -> bool:if tool_name not in _BROWSER_SESSION_TOOL_NAMES or output.get("ok", True):return False+ # The typed code comes first: run_browser_code names a lost browser this way, in prose that carries+ # neither "not found" nor a status, and the same dead browser must count whichever tool met it.+ if output.get("error_code") == "browser_session_unavailable":+ return Truelowered = " ".join(_result_text_values(output)).lower()if "no browser context" in lowered:return Truediff --git a/skyvern/forge/sdk/core/skyvern_context.py b/skyvern/forge/sdk/core/skyvern_context.pyindex 5a6c0544b..bbed8a373 100644--- a/skyvern/forge/sdk/core/skyvern_context.py+++ b/skyvern/forge/sdk/core/skyvern_context.py@@ -398,6 +398,14 @@ class SkyvernContext:# Both sites for a run run sequentially, so the read-modify-write needs no lock; verification /# extraction / error-detection scrapes never touch it.transient_ui_consecutive_suppressions: int = 0+ # The URLs the workflow AUTHOR typed into the running task block's own `url`/`navigation_goal`,+ # read before those fields are rendered, and the (workflow_run_id, block label) the reading was+ # done for. A Task V3 guard verdict publishes a landed URL's path only when it is one of these, so+ # a URL that only exists after a template rendered a prior block's page-derived output is not one.+ # Written and read only through skyvern.forge.taskv3.handoff_redaction; a stale owner reads as+ # empty, which is host-only in the verdict.+ caller_authored_block_urls: frozenset[str] = frozenset()+ caller_authored_block_urls_owner: tuple[str, str] | None = None# WORKFLOW_TASK_V3_AB arm, resolved once per workflow run: the engine every default-engine# task block of that run dispatches to, or None for control.workflow_block_engine_override: RunEngine | None = Nonediff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.pyindex 36d971770..7b1cc1431 100644--- a/skyvern/forge/sdk/routes/workflow_copilot.py+++ b/skyvern/forge/sdk/routes/workflow_copilot.py@@ -3155,12 +3155,20 @@ def _bounded_output_value(value: Any) -> Any:async def _history_proposal_state(chat: WorkflowCopilotChat | None, organization_id: str-) -> tuple[dict[str, Any] | None, CopilotProposalMetadata | None, CopilotProposalRunFacts | None]:+) -> tuple[dict[str, Any] | None, CopilotProposalMetadata | None, CopilotProposalRunFacts | None, float | None]:+ """Returns the proposal to display, its metadata, its run facts, and the claim's remaining lease.++ The claim is reported SEPARATELY from the proposal on purpose: whether a write is in flight is+ a fact about the workflow, while the proposal is only displayable when canonical still matches+ it. Deriving one from the other hides a live claim behind a hidden proposal.+ """if chat is None or not isinstance(chat.proposed_workflow, dict):- return None, None, None+ return None, None, None, Nonemetadata = copilot_proposal_metadata(chat.proposed_workflow)if metadata is None:- return chat.proposed_workflow, None, None+ return chat.proposed_workflow, None, None, None+ # Read from the RAW metadata, before any decision about whether the proposal is displayable.+ claim_expires_in = metadata.claim_expires_in(datetime.now(UTC))# A live claim is reported, not hidden. A server that dies mid-accept leaves the claim behind# for the length of the lease, and withholding the card for that long is the invisible-work# problem this store exists to remove; Accept meanwhile answers 409 and the client refetches.@@ -3172,7 +3180,11 @@ async def _history_proposal_state(canonical is Noneor workflow_content_fingerprint(canonical.model_dump(mode="json")) != metadata.canonical_fingerprint):- return None, None, None+ # Canonical moved, so the proposal is no longer displayable - but a concurrent Accept may+ # still be writing under this claim, and that is exactly when a client must not save over+ # it. Hiding the proposal here MUST NOT hide the claim; reporting no claim while one is+ # live is not ambiguity, it is a client-trusted lie.+ return None, None, None, claim_expires_inrun_facts = Noneif metadata.workflow_run_id is not None:run = await app.DATABASE.workflow_runs.get_workflow_run(@@ -3195,7 +3207,7 @@ async def _history_proposal_state(for row in output_rows],)- return chat.proposed_workflow, metadata, run_facts+ return chat.proposed_workflow, metadata, run_facts, claim_expires_in@base_router.get("/workflow/copilot/chat-history", include_in_schema=False)@@ -3235,9 +3247,12 @@ async def workflow_copilot_chat_history()else:chat_messages = []- proposed_workflow, proposed_workflow_metadata, proposed_workflow_run = await _history_proposal_state(- chat, organization.organization_id- )+ (+ proposed_workflow,+ proposed_workflow_metadata,+ proposed_workflow_run,+ proposed_claim_expires_in_seconds,+ ) = await _history_proposal_state(chat, organization.organization_id)request_turn_id = Noneif chat is not None and request_cancel_token is not None:request_turn_id = next(@@ -3298,6 +3313,7 @@ async def workflow_copilot_chat_history(),proposed_workflow=proposed_workflow,proposed_workflow_metadata=proposed_workflow_metadata,+ proposed_claim_expires_in_seconds=proposed_claim_expires_in_seconds,proposed_workflow_run=proposed_workflow_run,auto_accept=chat.auto_accept if chat else None,work_plan=chat.work_plan if chat else [],diff --git a/skyvern/forge/sdk/schemas/workflow_copilot.py b/skyvern/forge/sdk/schemas/workflow_copilot.pyindex 64de06697..a93974b58 100644--- a/skyvern/forge/sdk/schemas/workflow_copilot.py+++ b/skyvern/forge/sdk/schemas/workflow_copilot.py@@ -60,6 +60,12 @@ class CopilotProposalMetadata(BaseModel):return Falsereturn now - self.claimed_at < COPILOT_PROPOSAL_CLAIM_LEASE+ def claim_expires_in(self, now: datetime) -> float | None:+ """Seconds left on a live claim, or None when no claim holds the candidate."""+ if self.claimed_at is None or not self.claim_is_live(now):+ return None+ return (self.claimed_at + COPILOT_PROPOSAL_CLAIM_LEASE - now).total_seconds()+class CopilotProposalRunOutput(BaseModel):output_parameter_id: str@@ -840,6 +846,13 @@ class WorkflowCopilotChatHistoryResponse(BaseModel):chat_history: list[WorkflowCopilotChatHistoryMessage] = Field(default_factory=list, description="Chat messages")proposed_workflow: dict | None = Field(None, description="Latest workflow proposed by the copilot")proposed_workflow_metadata: CopilotProposalMetadata | None = None+ proposed_claim_expires_in_seconds: float | None = Field(+ None,+ description=(+ "Seconds the server's accepting claim has left. None when no live claim holds the proposal. "+ "A duration rather than a deadline, so a client with a skewed clock still agrees with the server."+ ),+ )proposed_workflow_run: CopilotProposalRunFacts | None = Noneauto_accept: bool | None = Field(None, description="Whether copilot auto-accepts workflow updates")diff --git a/skyvern/forge/sdk/workflow/code_block_safety.py b/skyvern/forge/sdk/workflow/code_block_safety.pyindex c2d2c9266..97acdb5ae 100644--- a/skyvern/forge/sdk/workflow/code_block_safety.py+++ b/skyvern/forge/sdk/workflow/code_block_safety.py@@ -13,8 +13,9 @@ import textwrapfrom collections import Counterfrom collections.abc import Callablefrom string import Formatter+from time import struct_timefrom types import SimpleNamespace-from typing import Any+from typing import Any, Selffrom skyvern.forge.sdk.workflow.exceptions import InsecureCodeDetected@@ -220,6 +221,108 @@ def safe_builtins() -> dict[str, Any]:def module_shims() -> dict[str, SimpleNamespace]:"""Fresh module-shaped namespaces both CodeBlock executors bind as globals."""++ # Built per call so the writable heap types stay contained to a single execution.+ class _SandboxTime(datetime.time):+ """CPython's C date methods resolve ``__import__`` from the calling frame's globals, which a+ CodeBlock's globals deliberately lack; these delegates put a trusted frame in between."""++ __slots__ = ()++ def strftime(self, format: str) -> str:+ return super().strftime(format)++ # Python 3.14 gives date and time their own C strptime, which imports from the calling frame.+ if "strptime" in vars(datetime.time):++ @classmethod+ def strptime(cls, date_string: str, format: str, /) -> Self:+ return super().strptime(date_string, format) # type: ignore[misc]++ class _SandboxDate(datetime.date):+ __slots__ = ()++ @classmethod+ def today(cls) -> Self:+ return super().today()++ def strftime(self, format: str) -> str:+ return super().strftime(format)++ def timetuple(self) -> struct_time:+ return super().timetuple()++ if "strptime" in vars(datetime.date):++ @classmethod+ def strptime(cls, date_string: str, format: str, /) -> Self:+ return super().strptime(date_string, format) # type: ignore[misc]++ # ``_SandboxDate`` precedes ``datetime.datetime`` in the MRO, so any delegate added to+ # ``_SandboxDate`` must be mirrored here or it shadows ``datetime.datetime``'s version.+ class _SandboxDateTime(_SandboxDate, datetime.datetime):+ __slots__ = ()++ @classmethod+ def today(cls) -> Self:+ return super().today()++ @classmethod+ def strptime(cls, date_string: str, format: str, /) -> Self:+ return super().strptime(date_string, format)++ # Their DeprecationWarning is raised from the calling frame, and the warning machinery+ # imports from that frame, so these reach the same hidden import under any active filter.+ @classmethod+ def utcnow(cls) -> Self:+ return super().utcnow()++ @classmethod+ def utcfromtimestamp(cls, timestamp: float, /) -> Self:+ return super().utcfromtimestamp(timestamp)++ def strftime(self, format: str) -> str:+ return super().strftime(format)++ def timetuple(self) -> struct_time:+ return super().timetuple()++ def utctimetuple(self) -> struct_time:+ return super().utctimetuple()++ def date(self) -> datetime.date:+ return _SandboxDate(self.year, self.month, self.day)++ def time(self) -> datetime.time:+ return _SandboxTime(self.hour, self.minute, self.second, self.microsecond, fold=self.fold)++ def timetz(self) -> datetime.time:+ return _SandboxTime(self.hour, self.minute, self.second, self.microsecond, self.tzinfo, fold=self.fold)++ # Instance reprs print ``tp_name``, which follows ``__name__``, and ``repr(type)`` prints+ # ``__module__`` plus ``__qualname__``; both match the stdlib while ``__name__`` itself does not.+ # A delegate's qualname reaches the block's failure_reason, where the defining frame would leak.+ for sandbox_type, stdlib_name in ((_SandboxTime, "time"), (_SandboxDate, "date"), (_SandboxDateTime, "datetime")):+ sandbox_type.__module__ = "datetime"+ sandbox_type.__name__ = f"datetime.{stdlib_name}"+ sandbox_type.__qualname__ = stdlib_name+ for attribute, member in vars(sandbox_type).items():+ if attribute.startswith("_"):+ continue+ function = member.__func__ if isinstance(member, classmethod) else member+ if callable(function):+ function.__module__ = "datetime"+ function.__qualname__ = f"datetime.{stdlib_name}.{attribute}"++ # The C types hand these bounds out as plain instances, and a plain instance re-breaks strftime+ # one call later.+ _SandboxTime.min = _SandboxTime.fromisoformat(datetime.time.min.isoformat())+ _SandboxTime.max = _SandboxTime.fromisoformat(datetime.time.max.isoformat())+ _SandboxDate.min = _SandboxDate.fromisoformat(datetime.date.min.isoformat())+ _SandboxDate.max = _SandboxDate.fromisoformat(datetime.date.max.isoformat())+ _SandboxDateTime.min = _SandboxDateTime.fromisoformat(datetime.datetime.min.isoformat())+ _SandboxDateTime.max = _SandboxDateTime.fromisoformat(datetime.datetime.max.isoformat())+return {"asyncio": SimpleNamespace(sleep=asyncio.sleep),"re": SimpleNamespace(@@ -241,8 +344,8 @@ def module_shims() -> dict[str, SimpleNamespace]:"json": SimpleNamespace(dumps=json.dumps, loads=json.loads),"html": SimpleNamespace(escape=html.escape),"datetime": SimpleNamespace(- datetime=datetime.datetime,- date=datetime.date,+ datetime=_SandboxDateTime,+ date=_SandboxDate,timedelta=datetime.timedelta,timezone=datetime.timezone,UTC=datetime.UTC,diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.pyindex cad0e2df1..53fafbb60 100644--- a/skyvern/forge/sdk/workflow/models/block.py+++ b/skyvern/forge/sdk/workflow/models/block.py@@ -257,6 +257,7 @@ from skyvern.forge.sdk.workflow.secret_encryption import (is_encrypted_secret,is_full_template_reference,)+from skyvern.forge.taskv3.handoff_redaction import pin_caller_authored_block_urlsfrom skyvern.schemas.browser_session_close import BrowserSessionCloseReasonfrom skyvern.schemas.emails import EmailBodyFormatfrom skyvern.schemas.runs import RunEngine, read_browser_type@@ -322,7 +323,7 @@ from skyvern.webeye.utils.captcha_solver import CaptchaChallengeUnsolvedError, sfrom skyvern.webeye.utils.page import ScreenshotMode, SkyvernFrameif TYPE_CHECKING:- from skyvern.forge.agent_functions import CodeBlockEngineFailure+ from skyvern.forge.agent_functions import CodeBlockEngineFailure, DownloadClaimOutcomefrom skyvern.forge.sdk.workflow.models.workflow import WorkflowRunfrom skyvern.webeye.browser_engine import BrowserEngineSelectionfrom skyvern.webeye.cdp_download_interceptor import CDPDownloadInterceptor@@ -415,9 +416,23 @@ async def capture_block_download_baseline(DOWNLOAD_BINDING_FAILURE_REASON = ("A file downloaded into the run directory but no registered download landed in the block output.")+DOWNLOAD_CLAIM_FAILURE_REASON = (+ "The block ran a download operation but no downloaded file was registered for this workflow run."+)UNBOUND_DOWNLOAD_OUTPUT_KEY = "download_registration_failed"+@dataclass+class DownloadClaimOutcomeRecorder:+ """A one-shot channel the brokered claim writes its outcome into so the block-owned settlement can+ tell a proven delivery from the branch-5 placeholder without inspecting the returned filename."""++ outcome: DownloadClaimOutcome | None = None++ def record(self, outcome: DownloadClaimOutcome) -> None:+ self.outcome = outcome++def bind_downloaded_files_to_output(result: Any, downloaded_files: list[FileInfo]) -> Any:"""Attach registration evidence to whatever the block's code returned.@@ -2100,6 +2115,17 @@ class BaseTaskBlock(Block):if block_context:await capture_block_download_baseline(block_context, organization_id or "", workflow_run_id, self.label)+ # Read the block's caller-known URLs off its own definition strings before anything replaces+ # them: both the parameter substitution below and the Jinja render after it resolve against the+ # run context, which holds prior blocks' page-derived output (SKY-16271).+ pin_caller_authored_block_urls(+ block_context,+ workflow_run_id=workflow_run_id,+ block_label=self.label,+ url=self.url,+ navigation_goal=self.navigation_goal,+ )+# Get workflow from context if available, otherwise query databaseworkflow = workflow_run_context.workflowif workflow is None:@@ -5229,6 +5255,7 @@ async def _code_block_click_and_claim_download_builtin(download_evidence: DownloadEvidenceProbe | None = None,action: Callable[[], Awaitable[None]] | None = None,timeout_seconds: float | None = None,+ outcome_recorder: DownloadClaimOutcomeRecorder | None = None,) -> str:"""Click ``selector`` once and confirm the browser download it fires, returning the sanitizedsuggested filename as a summary.@@ -5238,13 +5265,24 @@ async def _code_block_click_and_claim_download_builtin(provider-owned destination — and the execution layer registers from there. Copying or replayinghere would add a second writer to a single-writer system (SKY-11371) and double-register thedelivered file, so the only thing this operation owns is the trigger and the event. ``action``- replaces the click when the trigger runs elsewhere, as for a brokered ``page.expect_download``."""+ replaces the click when the trigger runs elsewhere, as for a brokered ``page.expect_download``.++ ``outcome_recorder`` receives which branch resolved the claim so the block-owned settlement can+ arm its truthful-completion verdict on the branch-5 placeholder alone; the return value and every+ branch's compatibility behavior are unchanged."""++ def _record(outcome: DownloadClaimOutcome) -> None:+ if outcome_recorder is not None:+ outcome_recorder.record(outcome)+if download_binding is None or download_binding not in (DownloadBinding.RUN_DIR, DownloadBinding.SESSION_DIR):+ _record("raised")raise CodeBlockDownloadClaimError("click_and_claim_download is only supported when downloads are bound to a known destination.")resolved_selector = str(selector or "").strip()if action is None and not resolved_selector:+ _record("raised")raise CodeBlockDownloadClaimError("click_and_claim_download requires the selector of the affordance to click.")click_error: BaseException | None = None# Only a binding whose delivery this page cannot observe can reach the grace branch below, so@@ -5306,17 +5344,22 @@ async def _code_block_click_and_claim_download_builtin(if delivered_name is None:# A selector that no longer matches is a page failure, not a download that failed to# fire. Re-raise Playwright's own error so the healing path still recognises its type.+ _record("raised")raise click_error# The click did not return, but this binding's writer registered a file it started, so# failing here would fail a download that arrived.+ _record("returned_proven")return delivered_nameif download_binding is DownloadBinding.SESSION_DIR or claim_monitor_owns_binding:# The click landed, and this binding's delivery belongs to the session watcher or to the# monitor -- which denies browser-native downloads and fetches the bytes itself, so no# Download event need ever reach this page. Failing here would fail a download that# succeeded; the execution layer still reports an unregistered intent when nothing- # arrives.+ # arrives. A returned placeholder with no registered delta is the only success-shaped+ # value without proof, so settlement arms its verdict on exactly that.+ _record("returned_proven" if delivered_name is not None else "returned_unproven")return delivered_name or _DOWNLOAD_CLAIM_FALLBACK_STEM+ _record("raised")raise CodeBlockDownloadClaimError("Clicking the affordance did not fire a browser download."if action is None@@ -5346,12 +5389,17 @@ async def _code_block_click_and_claim_download_builtin(summary = normalize_download_filename(download.suggested_filename or "") or _DOWNLOAD_CLAIM_FALLBACK_STEMif monitor_owns_binding:# The monitor denies browser-native downloads and fetches the file itself, so Playwright- # reports a delivered download as cancelled. Delivery is the monitor's to prove here.+ # reports a delivered download as cancelled. Delivery is the monitor's to prove here. An event+ # fired, so this is not the branch-5 no-event placeholder the settlement verdict arms on.+ _record("returned_proven")return summaryif not outcome_readable:+ _record("raised")raise CodeBlockDownloadClaimError("The browser download outcome could not be confirmed.")if failure:+ _record("raised")raise CodeBlockDownloadClaimError(f"The browser download did not complete: {failure}")+ _record("returned_proven")return summary@@ -5361,6 +5409,7 @@ def _bind_code_block_download_claim(workflow_run_id: str | None,download_binding: DownloadBinding | None,download_evidence: DownloadEvidenceProbe | None = None,+ outcome_recorder: DownloadClaimOutcomeRecorder | None = None,) -> Callable[[Page | RecordingPage, str], Awaitable[str]]:"""Two-argument closure rather than a `partial`: keyworddefaults on a `partial` would let block code override the run identity it is bound to."""@@ -5373,6 +5422,7 @@ def _bind_code_block_download_claim(workflow_run_id=workflow_run_id,download_binding=download_binding,download_evidence=download_evidence,+ outcome_recorder=outcome_recorder,)return click_and_claim_download@@ -5808,6 +5858,7 @@ class CodeBlock(Block):download_run_id: str | None = None,download_binding: DownloadBinding | None = None,download_evidence: DownloadEvidenceProbe | None = None,+ download_claim_outcome_recorder: DownloadClaimOutcomeRecorder | None = None,authorized_file_materializations: Mapping[str, AuthorizedFileMaterialization] | None = None,) -> Callable[[], Awaitable[dict[str, Any]]]:# SECURITY: validate before exec(). The AST check must run on the raw@@ -5850,6 +5901,7 @@ class CodeBlock(Block):workflow_run_id=workflow_run_id,download_binding=download_binding,download_evidence=download_evidence,+ outcome_recorder=download_claim_outcome_recorder,)safe_vars["attach_authorized_file"] = bind_inline_attach_authorized_file(page,@@ -6264,16 +6316,18 @@ async def wrapper({default_args}):download_run_id=storage_run_id,)if session_bound and registered_files == []:- registered_files = (- await self._await_in_flight_session_download(- organization_id=organization_id,- workflow_run_id=workflow_run_id,- workflow_run_block_id=workflow_run_block_id,- run_id=storage_run_id,- download_operation_invoked=download_operation_invoked,- )- or registered_files+ settled, settle_reads_failed = await self._await_in_flight_session_download(+ organization_id=organization_id,+ workflow_run_id=workflow_run_id,+ workflow_run_block_id=workflow_run_block_id,+ run_id=storage_run_id,+ download_operation_invoked=download_operation_invoked,)+ if settle_reads_failed:+ # A settle-phase read/claim failed after a readable-empty first read: the registration+ # is now unreadable, not proven-empty, so abstain rather than collapse it to [].+ return None, set()+ registered_files = settled or registered_filesreturn registered_files, set()async def _await_in_flight_session_download(@@ -6284,24 +6338,28 @@ async def wrapper({default_args}):workflow_run_block_id: str,run_id: str,download_operation_invoked: bool = False,- ) -> list[FileInfo] | None:- """Return a session download once its final watcher row becomes visible.+ ) -> tuple[list[FileInfo] | None, bool]:+ """Return a session download once its final watcher row becomes visible, plus whether a+ settle-phase read failed.The watcher drops the partial's artifact row on Chrome's rename and writes the final row fromthe same event batch, in either order. A visible partial or a typed broker receipt thereforeauthorizes the same bounded settle poll; neither a vanished partial nor completed browser- bytes alone prove that the final artifact row is committed."""+ bytes alone prove that the final artifact row is committed. The second element is ``True`` when+ a claim/read raised or a settle read came back unreadable, so the caller abstains instead of+ reading the exhausted poll as a proven-empty registration."""context = skyvern_context.current()browser_session_id = context.browser_session_id if context else Noneif not browser_session_id:- return None+ return None, False+ reads_failed = Falsetry:in_flight = await app.STORAGE.list_downloading_files_in_browser_session(organization_id=organization_id,browser_session_id=browser_session_id,)if not in_flight and not download_operation_invoked:- return None+ return None, Falseif in_flight:LOG.info("Code block finished with a session download still in flight; waiting for it",@@ -6328,7 +6386,9 @@ async def wrapper({default_args}):download_run_id=run_id,)if settled:- return settled+ return settled, False+ if settled is None:+ reads_failed = Trueawait asyncio.sleep(_CODE_BLOCK_SESSION_DOWNLOAD_SETTLE_INTERVAL_SECONDS)except Exception:LOG.warning(@@ -6338,7 +6398,8 @@ async def wrapper({default_args}):browser_session_id=browser_session_id,exc_info=True,)- return None+ return None, True+ return None, reads_failedasync def _read_back_downloaded_files(self,@@ -6392,6 +6453,7 @@ async def wrapper({default_args}):skipped_file_names: set[str],attempt_started_at: datetime | None = None,authored_registration: bool = False,+ download_claim_outcome: DownloadClaimOutcome | None = None,) -> tuple[Any, str | None]:"""Bind registration evidence into the block output, returning a failure reason if it cannot.@@ -6459,6 +6521,22 @@ async def wrapper({default_args}):if downloaded_files:_log_verdict_skipped_for_partial_save()return output, None+ if download_claim_outcome == "returned_unproven" and not registered_file_names and not skipped_file_names:+ # A brokered claim returned the branch-5 placeholder and, after the bounded in-flight+ # wait, nothing registered to this run's current attempt. Completing here would report a+ # phantom success on a download that never landed; fail truthfully instead. Keyed on the+ # pre-filter read so a file attributed to another loop iteration is never read as "nothing+ # registered", and gated above on ``downloaded_files is not None`` so an unreadable read+ # still abstains.+ LOG.error(+ "codeblock.download_claim_unproven",+ engine=engine,+ workflow_run_id=workflow_run_id,+ workflow_run_block_id=workflow_run_block_id,+ block_label=self.label,+ organization_id=organization_id,+ )+ return unbound_download_output(output), DOWNLOAD_CLAIM_FAILURE_REASONdownload_dir_after = current_attempt_local_download_dir_file_identities(resolved_download_id,attempt_started_at,@@ -7000,6 +7078,8 @@ async def wrapper({default_args}):browser_session_id=browser_session_id,close_browser_on_completion=False,pre_resolved_browser_state=browser_state,+ engine=RunEngine.skyvern_v3,+ workflow_owned_recovery=True,)finally:current_context.task_id = previous_task_id@@ -7640,6 +7720,7 @@ async def wrapper({default_args}):download_dir_before: set[tuple[str, int, int]] | None = None,attempt_started_at: datetime | None = None,redaction_parameters: dict[str, Any] | None = None,+ download_claim_outcome: DownloadClaimOutcome | None = None,) -> BlockResult:resolved_redaction_parameters = redaction_parameters or {}staged_frame: _StagedFrame | None = None@@ -7692,6 +7773,7 @@ async def wrapper({default_args}):workflow_run_block_id=workflow_run_block_id,session_bound=session_download_lane_active(browser_state),download_run_id=resolved_download_id,+ download_operation_invoked=download_claim_outcome is not None,)bound_output, binding_failure_reason = await self._bind_and_grade_downloads(engine="inline",@@ -7705,6 +7787,7 @@ async def wrapper({default_args}):workflow_run_id=workflow_run_id,workflow_run_block_id=workflow_run_block_id,organization_id=organization_id,+ download_claim_outcome=download_claim_outcome,)except asyncio.CancelledError:# A cancel inside the storage round-trips must not book the healed block@@ -8407,6 +8490,9 @@ async def wrapper({default_args}):credential_release_guard.log_armed()await recorder.create_task_and_step()recording_page = recorder.recording_page+ # Bound before the try so the shared except arms can read it even when the secure path raises+ # before the inline claim is wired; a claim that never ran leaves it unarmed (outcome None).+ inline_download_claim_outcome = DownloadClaimOutcomeRecorder()try:await recorder.link_block()@@ -8567,6 +8653,11 @@ async def wrapper({default_args}):download_dir_before=download_dir_before,attempt_started_at=attempt_started_at,redaction_parameters=serialized_parameter_values,+ download_claim_outcome=(+ secure_code_block_result.download_operation_receipt.outcome+ if secure_code_block_result.download_operation_receipt is not None+ else None+ ),)if secure_code_block_result.block_result is None:LOG.warning(@@ -8673,6 +8764,11 @@ async def wrapper({default_args}):workflow_run_block_id=workflow_run_block_id,organization_id=organization_id,authored_registration=block_output_has_registered_download(secure_output),+ download_claim_outcome=(+ secure_code_block_result.download_operation_receipt.outcome+ if secure_code_block_result.download_operation_receipt is not None+ else None+ ),)except asyncio.CancelledError:# A cancel inside the storage round-trips must not book the executed block@@ -8734,6 +8830,7 @@ async def wrapper({default_args}):download_run_id=resolved_download_id,download_binding=download_binding_of(browser_state),download_evidence=download_evidence,+ download_claim_outcome_recorder=inline_download_claim_outcome,authorized_file_materializations=authorized_file_materializations,)try:@@ -8896,6 +8993,7 @@ async def wrapper({default_args}):download_dir_before=download_dir_before,attempt_started_at=attempt_started_at,redaction_parameters=serialized_parameter_values,+ download_claim_outcome=inline_download_claim_outcome.outcome,)if (type(e) is ErrorCode@@ -9046,6 +9144,7 @@ async def wrapper({default_args}):download_dir_before=download_dir_before,attempt_started_at=attempt_started_at,redaction_parameters=serialized_parameter_values,+ download_claim_outcome=inline_download_claim_outcome.outcome,)else:@@ -9071,6 +9170,7 @@ async def wrapper({default_args}):workflow_run_block_id=workflow_run_block_id,session_bound=session_download_lane_active(browser_state),download_run_id=resolved_download_id,+ download_operation_invoked=inline_download_claim_outcome.outcome is not None,)result, binding_failure_reason = await self._bind_and_grade_downloads(engine="inline",@@ -9084,6 +9184,7 @@ async def wrapper({default_args}):workflow_run_id=workflow_run_id,workflow_run_block_id=workflow_run_block_id,organization_id=organization_id,+ download_claim_outcome=inline_download_claim_outcome.outcome,)except asyncio.CancelledError:# A cancel inside the storage round-trips must not book the executed block asdiff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.pyindex de27262c1..0640ab08a 100644--- a/skyvern/forge/sdk/workflow/service.py+++ b/skyvern/forge/sdk/workflow/service.py@@ -3239,7 +3239,7 @@ class WorkflowService:version=version,)if workflow is None:- LOG.error(f"Workflow {workflow_permanent_id} not found", workflow_version=version)+ LOG.warning(f"Workflow {workflow_permanent_id} not found", workflow_version=version)raise WorkflowNotFound(workflow_permanent_id=workflow_permanent_id, version=version)workflow_id = workflow.workflow_idif workflow_request.proxy_location is None and workflow.proxy_location is not None:@@ -13170,7 +13170,7 @@ class WorkflowService:)if workflow is None:- LOG.error(f"Workflow {workflow_permanent_id} not found")+ LOG.warning(f"Workflow {workflow_permanent_id} not found")raise WorkflowNotFound(workflow_permanent_id=workflow_permanent_id)attempt_view = compute_attempt_view(diff --git a/skyvern/forge/taskv3/auth_tools.py b/skyvern/forge/taskv3/auth_tools.pyindex 79c62e8d8..52ad0542a 100644--- a/skyvern/forge/taskv3/auth_tools.py+++ b/skyvern/forge/taskv3/auth_tools.py@@ -13,7 +13,6 @@ artifacts/logs.from __future__ import annotationsimport asyncio-import reimport timefrom dataclasses import dataclass, fieldfrom datetime import datetime@@ -36,6 +35,7 @@ from skyvern.forge.sdk.core.skyvern_context import SkyvernContextfrom skyvern.forge.sdk.schemas.tasks import Taskfrom skyvern.forge.sdk.schemas.totp_codes import OTPTypefrom skyvern.forge.taskv3.loop import ToolResult, ToolSpec+from skyvern.forge.taskv3.opaque_refs import _MIN_REDACTED_QUERY_VALUE_CHARS, _OPAQUE_QUERY_VALUE_REfrom skyvern.forge.taskv3.tools import OBSERVE_URL_MAX_CHARS, PageProviderfrom skyvern.services.otp_service import OTPValue, has_otp_source, resolve_otp_valuefrom skyvern.utils.url_validators import strip_query_params, validate_fetch_url@@ -61,12 +61,6 @@ _NOT_YET_RECENCY_SECONDS = 180.0# advance spend by as little as _MIN_SLICE_SECONDS, which would admit ~90 holds. 8 >= ceil(900/120),# so this never binds on the healthy path._MAX_GIVEUP_DEFERRALS = 8-# Shorter values are codes/flags (lang=en, v=2), not link secrets, and a real link secret is at-# least this long; redacting the short ones would blank harmless text across the run's artifacts.-_MIN_REDACTED_QUERY_VALUE_CHARS = 16-# The charset an opaque token draws from. Excludes emails, URLs, and prose, which are readable-# values the model needs and redaction is global for the run.-_OPAQUE_QUERY_VALUE_RE = re.compile(r"[A-Za-z0-9._~+/=-]+")# A body-less landing must not hold the tool for a full navigation timeout._BODY_TEXT_TIMEOUT_MS = 5000diff --git a/skyvern/forge/taskv3/engine.py b/skyvern/forge/taskv3/engine.pyindex 3f90d2059..31496ad36 100644--- a/skyvern/forge/taskv3/engine.py+++ b/skyvern/forge/taskv3/engine.py@@ -24,7 +24,7 @@ from __future__ import annotationsimport jsonimport timefrom datetime import UTC, datetime-from typing import Any, Awaitable, Callable+from typing import Any, Awaitable, Callable, Collectionimport structlog@@ -207,6 +207,9 @@ async def run_task_v3_agent_loop(staged_downloads: set[str] | None = None,verification_blocker: VerificationBlocker | None = None,initial_navigation_status: int | None = None,+ initial_navigation_url: str | None = None,+ caller_known_urls: frozenset[str] = frozenset(),+ label_secret_values: Callable[[], Collection[str]] | None = None,page_probe: Callable[[], Awaitable[str | None]] | None = None,reload_page: Callable[[], Awaitable[None]] | None = None,restore_page_url: Callable[[Any, str], Awaitable[None]] | None = None,@@ -412,6 +415,9 @@ async def run_task_v3_agent_loop(verification_blocker=verification_blocker,staged_downloads=staged_downloads,initial_navigation_status=initial_navigation_status,+ initial_navigation_url=initial_navigation_url,+ caller_known_urls=caller_known_urls,+ label_secret_values=label_secret_values,page_probe=None if page_free else page_probe,page_fingerprint=None if page_free else page_fingerprint,reload_page=None if page_free else reload_page,@@ -448,6 +454,9 @@ async def run_task_v3_agent_loop(LOG.info("taskv3 engine loop finished",status=outcome.status,+ # The per-run home of the guard class that used to be a prefix on the customer-facing reason:+ # one row per run, so "how often did this policy end a run" is a facet, not a string match.+ guard=outcome.guard,turns=outcome.turns,tool_calls=outcome.tool_calls,tool_seconds=outcome.tool_seconds,diff --git a/skyvern/forge/taskv3/handoff_redaction.py b/skyvern/forge/taskv3/handoff_redaction.pyindex 89bc1f456..a499f9ac4 100644--- a/skyvern/forge/taskv3/handoff_redaction.py+++ b/skyvern/forge/taskv3/handoff_redaction.py@@ -9,9 +9,16 @@ finish_reason is masked as it is written, because that row is what the next blocfrom __future__ import annotationsimport re+from typing import Collectionfrom urllib.parse import unquote, urlsplit-from skyvern.forge.taskv3.opaque_refs import _is_hex_blob, _is_high_entropy_blob, is_signed_url+from skyvern.forge.sdk.core.skyvern_context import SkyvernContext+from skyvern.forge.taskv3.opaque_refs import (+ _is_hex_blob,+ _is_high_entropy_blob,+ is_signed_url,+ urls_in_text,+)# Prior-block prose is model output that went through a page: data, never instructions. It is# rendered inside a labelled data section, single-line, capped, and with signed URLs masked.@@ -22,6 +29,7 @@ MAX_PERSISTED_FINISH_REASON_CHARS = 2000_URL_RE = re.compile(r"https?://\S+")_WS_RE = re.compile(r"\s+")_TRAILING_PUNCT = ")]}>.,;:!?'\""+_DEFAULT_PORTS = {"http": 80, "https": 443}def mask_signed_urls_in_text(text: str) -> str:@@ -45,9 +53,15 @@ def sanitize_handoff_reason(reason: str | None) -> str | None:return text or None-def sanitize_handoff_url(url: str | None) -> str | None:- """Keep only scheme://host[:port]/path: userinfo, query strings and fragments are where- credentials and signed tokens live."""+def _bare_parts(url: str | None) -> tuple[str, str, str] | None:+ """(scheme, host[:port], path) with userinfo, query and fragment dropped — the parts of a URL that+ are never a credential — or None when there is no host to name.++ Reduced to ONE spelling per page, because a caller-known match is a string comparison: urlsplit+ lowercases scheme and host, a default port is dropped, and the path is trimmed of trailing slashes+ down to `/`. Without that the commonest task URL of all — `https://example.test`, whose path is+ empty — never matches the `https://example.test/` the browser reports landing on.+ """if not url:return Nonetry:@@ -57,14 +71,120 @@ def sanitize_handoff_url(url: str | None) -> str | None:return Noneif not parts.scheme or not hostname:return None- host = f"{hostname}:{port}" if port else hostname- # A magic link / password reset / signed download can carry its server-minted credential in the- # PATH, which no secret registry knows; scrub token-shaped segments rather than trust them.- path = "/".join(+ host = hostname if port is None or _DEFAULT_PORTS.get(parts.scheme) == port else f"{hostname}:{port}"+ return parts.scheme, host, parts.path.rstrip("/") or "/"+++def _joined(scheme: str, host: str, path: str) -> str:+ return f"{scheme}://{host}{path}"+++def _scrub_token_segments(path: str) -> str:+ """A magic link / password reset / signed download can carry its server-minted credential in the+ PATH, which no secret registry knows; scrub token-shaped segments rather than trust them."""+ return "/".join("***" if segment and (_is_hex_blob(unquote(segment)) or _is_high_entropy_blob(unquote(segment))) else segment- for segment in parts.path.split("/")+ for segment in path.split("/"))- bare = f"{parts.scheme}://{host}{path}"+++def sanitize_handoff_url(url: str | None) -> str | None:+ """Keep only scheme://host[:port]/path: userinfo, query strings and fragments are where+ credentials and signed tokens live."""+ bare_parts = _bare_parts(url)+ if bare_parts is None:+ return None+ scheme, host, raw_path = bare_parts+ bare = _joined(scheme, host, _scrub_token_segments(raw_path))if len(bare) > MAX_HANDOFF_URL_CHARS:bare = bare[: MAX_HANDOFF_URL_CHARS - 1] + "…"return bare+++# Stands in for a path that could not be published, so the sentence naming the URL cannot be read as a+# claim about the site's root -- the page is a page on this host, and this says which parts are withheld.+ELIDED_URL_PATH = "/…"+++def _normalized_published_url(url: str | None) -> str | None:+ """The one form both sides of the caller-known comparison are reduced to, so the comparison is+ between the same text and the same text. None when there is no host to name."""+ bare_parts = _bare_parts(url)+ return None if bare_parts is None else _joined(*bare_parts)+++def caller_known_published_urls(*sources: str | None) -> frozenset[str]:+ """The normalized URLs a guard verdict may publish a PATH for: each source is either a URL the+ caller configured or caller-authored text whose URL literals are read out of it.++ Sources are what the AUTHOR TYPED, never what a render produced. A composed goal carries a prior+ block's handoff prose; a workflow block's own `url`/`navigation_goal` are Jinja templates rendered+ from a run context that holds prior blocks' outputs. Either way the URL came off a page rather than+ from the caller, so a block's sources are read before `format_potential_template_parameters`+ (`pin_caller_authored_block_urls`). The disclosed limit: a URL that reaches a block only through+ templating is not caller-known, and a verdict names its host alone.+ """+ candidates: list[str | None] = []+ for source in sources:+ if not source:+ continue+ candidates.append(source)+ candidates.extend(urls_in_text(source))+ return frozenset(normalized for normalized in map(_normalized_published_url, candidates) if normalized)+++def sanitize_published_url(url: str | None, caller_known_urls: Collection[str] = frozenset()) -> str | None:+ """A URL fit to publish VERBATIM to a customer, or None when there is no host to name.++ The invariant: **a guard verdict publishes only what the caller already gave us, plus the host.**+ Scheme and host are always named -- a customer needs to know which site the run was on -- and the+ path is named only when the landed URL, under this same normalization, is byte-identical to one the+ caller supplied (`caller_known_published_urls`). Anything else is elided to `ELIDED_URL_PATH`, never+ cut down.++ Provenance, not shape, is what decides the branch -- deliberately. `opaque_refs` documents what a+ shape rule misses (a signing token that is short or single-case rides a path segment+ unrecognized), and a sink persisted as `task.failure_reason` and carried on the customer webhook+ cannot be defended by a predicate that has to recognize the next token shape. Shape is still+ applied ON TOP of a caller-known path, as `sanitize_handoff_url` does: the caller typing a URL+ that carries a signing token is no reason to print the token back.+ """+ bare_parts = _bare_parts(url)+ if bare_parts is None:+ return None+ scheme, host, path = bare_parts+ published = _joined(scheme, host, path)+ if published in caller_known_urls:+ return _joined(scheme, host, _scrub_token_segments(path))+ return _joined(scheme, host, ELIDED_URL_PATH)+++def pin_caller_authored_block_urls(+ ctx: SkyvernContext | None,+ *,+ workflow_run_id: str,+ block_label: str,+ url: str | None,+ navigation_goal: str | None,+) -> None:+ """Record a task block's caller-known URLs from its UNRENDERED definition strings, before+ `format_potential_template_parameters` (and the parameter-key substitution before it) replaces+ them with text a prior block read off a page. Blocks run sequentially, so one pin per run-scoped+ context is enough; the owner it is stamped with is what keeps the next block from reading it."""+ if ctx is None:+ return+ ctx.caller_authored_block_urls_owner = (workflow_run_id, block_label)+ ctx.caller_authored_block_urls = caller_known_published_urls(url, navigation_goal)+++def caller_authored_block_urls(+ ctx: SkyvernContext | None, *, workflow_run_id: str | None, block_label: str | None+) -> frozenset[str]:+ """What `pin_caller_authored_block_urls` recorded for THIS block: nothing when no pin was written+ or the pin belongs to another block. A task that reached the loop without passing the block seam+ names a host and no path, rather than falling back to its own rendered fields."""+ if ctx is None or workflow_run_id is None or block_label is None:+ return frozenset()+ if ctx.caller_authored_block_urls_owner != (workflow_run_id, block_label):+ return frozenset()+ return ctx.caller_authored_block_urlsdiff --git a/skyvern/forge/taskv3/loop.py b/skyvern/forge/taskv3/loop.pyindex 4e3e59af6..311ff7365 100644--- a/skyvern/forge/taskv3/loop.py+++ b/skyvern/forge/taskv3/loop.py@@ -24,13 +24,16 @@ from collections import dequefrom contextvars import ContextVarfrom dataclasses import dataclass, fieldfrom enum import Enum-from typing import Any, Awaitable, Callable, Literal, NamedTuple, TypeVar+from typing import Any, Awaitable, Callable, Collection, Iterator, Literal, NamedTuple, TypeVarimport structlogfrom skyvern.exceptions import SkyvernContextWindowExceededErrorfrom skyvern.forge.sdk.core import skyvern_contextfrom skyvern.forge.sdk.core.skyvern_context import SkyvernContext+from skyvern.forge.sdk.workflow.context_manager import RANDOM_SECRET_ID_PREFIX+from skyvern.forge.taskv3.handoff_redaction import MAX_HANDOFF_URL_CHARS, sanitize_published_url+from skyvern.forge.taskv3.target_label import describe_targetLOG = structlog.get_logger()@@ -416,6 +419,10 @@ class ToolSpec:class LoopOutcome:status: Literal["completed", "failed", "terminated", "budget_exhausted", "loop_error", "canceled"]reason: str+ # Which loop guard authored this verdict (`PERCEPTION_STALL_GUARD` and its siblings), or None for a+ # terminal the model or a budget produced. This is the machine-readable class that used to be a+ # prefix on `reason`; `reason` itself is customer-facing text (see `_guard_verdict`).+ guard: str | None = Noneextracted_output: Any = None# A user-defined error code the MODEL chose when finishing, drawn from the task's# error_code_mapping. Only ever set on a deliberate finish; None means the model was offered codes@@ -539,9 +546,12 @@ PERCEPTION_STALL_TERMINATE_AFTER = 15# and exactly one of each survives, as before — whatever a non-strict provider adds to the call.PERCEPTION_SNAPSHOT_RETAIN = 2-# Stable, facetable prefix for the stall verdict's reason — telemetry queries key on it to measure-# how often the policy fires; change it only with the dashboards that read it.-PERCEPTION_STALL_REASON_PREFIX = "perception_stall:"+# The stall verdict's machine class. Stable and facetable — telemetry keys on it to measure how often+# the policy fires — and it travels beside the verdict (`LoopOutcome.guard`, the guard's log line, the+# engine's terminal record), never inside its text: a verdict's `reason` is persisted verbatim as the+# customer-facing `task.failure_reason`, so a dashboard facet in it is a facet the customer reads+# (SKY-16271). Change it only with the dashboards that read it.+PERCEPTION_STALL_GUARD = "perception_stall"# Action-loop policy: N repeated executions of the same billable action (same tool + same args)# with no new evidence the page changed mean the run is re-trying against an unchanged outcome —@@ -566,8 +576,8 @@ ACTION_LOOP_NUDGE_AFTER = 3# goal progress, only of page change.ACTION_LOOP_TERMINATE_AFTER = 8-# Facetable sibling of PERCEPTION_STALL_REASON_PREFIX; same dashboard contract.-ACTION_LOOP_REASON_PREFIX = "action_loop:"+# Facetable sibling of PERCEPTION_STALL_GUARD; same dashboard contract.+ACTION_LOOP_GUARD = "action_loop"# Progress-gated action-step budget extension (SKY-15264): a run that hits its action-step cap while# the page is still demonstrably changing (a repeated probe returned fresh content, a navigation or@@ -588,6 +598,7 @@ ACTION_BUDGET_EXTENSION_MAX_FACTOR = 3# precision is measurable on the canary; change only with the dashboards that read them.ACTION_BUDGET_EXTENDED_EVENT = "taskv3 loop action budget extended"ACTION_BUDGET_EXTENSION_REFUSED_EVENT = "taskv3 loop action budget extension refused"+CREDENTIAL_RESUBMIT_REFUSED_EVENT = "taskv3 loop credential resubmit refused"EXTRACTION_ENTRY_REFUSED_EVENT = "taskv3 loop extraction entry refused"# Every tool that authors input on the page. Defined here rather than in tools.py because tools.py imports# this module; the extraction-block refusal and tools.py's frame-work ledger both read this one set.@@ -615,8 +626,9 @@ PAGE_STATE_STALL_TERMINATE_AFTER = 12# live (benign direction); the shadow event measures the would-terminate precision on the canary,# and promotion to a live verdict is a separate release decision on that data.PAGE_STATE_STALL_SHADOW_EVENT = "taskv3 loop page state stall would terminate"-# Facetable sibling of PERCEPTION_STALL_REASON_PREFIX; reserved for the future live verdict.-PAGE_STATE_STALL_REASON_PREFIX = "page_state_stall:"+# Facetable sibling of PERCEPTION_STALL_GUARD; reserved for the future live verdict, which owes a+# customer-facing sentence of its own the day it stops being shadow-only.+PAGE_STATE_STALL_GUARD = "page_state_stall"# Hard "the resource does not exist / is gone" HTTP statuses. A navigation landing on one of these is# a genuine non-capability dead-end (a dead or removed posting), which v1 routes to `terminated`. Both@@ -625,6 +637,15 @@ PAGE_STATE_STALL_REASON_PREFIX = "page_state_stall:"# capability failures, not dead-ends, and are left to the model / stay `failed`.NAVIGATION_DEAD_END_STATUSES = frozenset({404, 410})++class _NavDeadEnd(NamedTuple):+ """A navigation that landed on a hard dead-end status, and the URL it landed on when the tool+ reported one."""++ status: int+ url: str | None++# Defined here (not tools.py) so the batch-dispatch poisoning check below can compare against it# without an import cycle -- tools.py already imports ToolResult/ToolSpec from this module.PAGE_UNAVAILABLE_ERROR = "browser page unavailable"@@ -632,11 +653,17 @@ PAGE_UNAVAILABLE_ERROR = "browser page unavailable"# A navigation landed on a hard dead-end (HTTP 404/410): the target posting does not exist or was# removed, so the goal cannot be completed there. Ends the run as `terminated`, matching v1's terminate# verdict for the same condition. Covers both the in-loop `navigate` tool and the pre-loop initial-URL-# navigation. Facetable sibling of the prefixes above.-NAV_DEAD_END_REASON_PREFIX = "navigation_dead_end:"+# navigation. Facetable sibling of the classes above.+NAV_DEAD_END_GUARD = "navigation_dead_end"# A page-level handler kept asking for the page to be reloaded past the per-run cap: the page cannot# be stabilized, and acting on it would mean acting on a page declared stale.-PAGE_REFRESH_EXHAUSTED_REASON_PREFIX = "page_refresh_exhausted:"+PAGE_REFRESH_EXHAUSTED_GUARD = "page_refresh_exhausted"++# A URL long enough to be a payload rather than a place is DROPPED from a verdict, never cut: the+# customer-facing reason is redacted by whole-value match, so a truncated URL would no longer match a+# registered secret it was cut from (the rule `clean_target_label` applies to an element name). The cap+# is the handoff one rather than a second number: both answer "how much URL may leave the run".+VERDICT_URL_MAX_CHARS = MAX_HANDOFF_URL_CHARS# Emitted, never acted on, when the oscillation rule WOULD have terminated. The step engine's# tripwires (skyvern/forge/sdk/fail_fast/shadow.py) earn the right to act by publishing this event@@ -1132,6 +1159,52 @@ def _may_submit(tool_name: str, args: dict[str, Any]) -> bool:return tool_name in ("click", CODE_TOOL_NAME) or _is_enter_submit(tool_name, args)+# Secret values reach the model only as `placeholder_...` tokens, so the guard below keys on the+# token the model passed back and never on what it resolves to — it cannot handle a plaintext secret.+# The prefix covers every registered secret, not only a login: AWS and Azure parameters and card+# fields are minted the same way, which is why the refusal names a rejected value rather than a+# failed sign-in. This matches greedily where the registry matches longest-known-key, so it shares+# find_embedded_placeholder_tokens' boundary limitation and adds one: two tokens concatenated with no+# separator merge into one, and a token the model reproduces with a different trailing fragment reads+# as a different key and goes uncounted. Both fail toward under-counting. It cannot call the registry,+# because loop.py must not import `app`; thread a token callable from agent.py if either is observed.+_CREDENTIAL_PLACEHOLDER_RE = re.compile(re.escape(RANDOM_SECRET_ID_PREFIX) + r"[A-Za-z0-9_]+")+# file_upload is excluded: its placeholder names a file, not a credential, and re-attaching a file+# after a submit is ordinary recovery rather than a second authentication attempt. The code tool is+# included for the reason _may_submit includes it — its body is opaque, so it is treated as possibly+# having entered the credential rather than as certainly not.+CREDENTIAL_ENTRY_TOOLS = (FILL_TOOLS | frozenset({CODE_TOOL_NAME})) - frozenset({"file_upload"})+# How many times one credential may be submitted within a task, and the whole knob. Replaying the+# rule over every v3 block in a 22h window: budget 1 refuses 38 blocks of which 23 COMPLETED, so it+# would break more runs than it protects; budget 3 reaches the bottom of the range the observed+# lockouts came from (3-5 submissions). 2 is the largest budget that stays under the observed harm+# threshold. It is chosen against today's submit precision — _may_submit counts any click, so a+# click that submits nothing still spends budget — and must be re-derived if that precision improves.+CREDENTIAL_SUBMIT_BUDGET = 2+++def _credential_placeholders(args: dict[str, Any]) -> set[str]:+ """Every credential placeholder token this call's arguments carry, across all of them.++ Scanning the values rather than naming `text`/`value`/`chosen` keeps the guard from being a+ list of argument conventions that a new fill tool can be added without. Nested because the+ arguments are the model's JSON: `select_option` carries `values`/`labels` as arrays, so a+ top-level-strings-only scan would let a credential through in a list.+ """++ def walk(value: Any) -> Iterator[str]:+ if isinstance(value, str):+ yield from _CREDENTIAL_PLACEHOLDER_RE.findall(value)+ elif isinstance(value, dict):+ for nested in value.values():+ yield from walk(nested)+ elif isinstance(value, (list, tuple)):+ for nested in value:+ yield from walk(nested)++ return set(walk(args))++def _is_finish(tool_name: str) -> bool:return tool_name == "finish"@@ -1526,6 +1599,88 @@ def _budget_exhausted_observation(cap: str, recency: ActivityRecency | None) ->return f"{json_line}\nThe run's budget cap has tripped; this is the final turn before the run ends."+def _guard_verdict(guard: str, reason: str) -> LoopOutcome:+ """The one way a loop guard ends a run: the customer-facing sentence in `reason`, the machine class+ in `guard`.++ They are separate because `reason` leaves the system verbatim as the task's `failure_reason` — a+ field on the customer webhook and the run view — so the facet a dashboard groups on, the counter+ that tripped, the tool that reported it and the selector it compared all belong on the guard's own+ log line instead (SKY-16271). `_budget_exhausted_reason` below holds the same standard for the+ budget exits.+ """+ return LoopOutcome("terminated", reason, guard=guard)+++def _verdict_url(url: Any, caller_known_urls: Collection[str]) -> str | None:+ """A URL fit to name in a customer-facing verdict, or None when nothing can be named.++ `sanitize_published_url` owns the invariant this surface rests on -- a verdict publishes only what+ the caller already gave us, plus the host -- so the scheme and host are always named and the path+ only when the landed URL is one of `caller_known_urls`, elided to `/…` otherwise.+ """+ if not isinstance(url, str) or not url.startswith(("http://", "https://")) or any(ch.isspace() for ch in url):+ return None+ published = sanitize_published_url(url, caller_known_urls)+ # Only a caller URL can be this long once the path is elided, and the caller can read it in their+ # own task config; cutting it would name a page that does not exist, so it is dropped whole.+ if published is not None and len(published) > VERDICT_URL_MAX_CHARS:+ return None+ return published+++def _dead_end_reason(status: int, url: Any, *, page_noun: str, caller_known_urls: Collection[str]) -> str:+ """The HTTP status stays in the sentence: `classify_from_failure_reason` derives NAVIGATION_FAILURE+ from it, and it is the one fact separating a page that is gone from a page that is merely wrong."""+ where = _verdict_url(url, caller_known_urls)+ named = f"{page_noun} ({where})" if where else page_noun+ return (+ f"{named} returned HTTP {status}: it no longer exists or has been removed, so the task could "+ "not be completed there."+ )+++def _verdict_target(+ tool: str,+ name: Any,+ kind: Any,+ label_secret_values: Callable[[], Collection[str]] | None,+ ctx: SkyvernContext | None,+) -> str | None:+ """The control a verdict names, or None when the acting call reported neither a name nor a kind.++ A page-supplied name is only printable against the run's full drop-check secret set -- its+ registered parameters included, unfloored, since a name is dropped whole rather than scrubbed --+ and only the caller can see that registry, so it arrives as a resolver read at verdict time. Same+ set the persisted timeline label is checked against: a credential-shaped label must not be dropped+ from the row and printed here. With no resolver, no context, or a resolver that fails, the verdict+ falls back to the kind floor rather than publishing page text it could not check -- a missing+ context is a missing HALF of that set (the codes minted this turn), not a reason to skip it.+ """+ if not name and not kind:+ return None+ if label_secret_values is None or ctx is None:+ return describe_target(tool, None, kind, ())+ try:+ secret_values = set(label_secret_values())+ except Exception:+ LOG.warning("taskv3 loop could not resolve a verdict's drop-check secrets", tool=tool)+ return describe_target(tool, None, kind, ())+ # Unfloored, and read live: a code minted this turn is not in the resolver's floored view.+ secret_values |= set(ctx.runtime_secret_values)+ return describe_target(tool, name, kind, secret_values)+++def _action_loop_reason(target: str | None) -> str:+ """`target` is the control the run kept acting on, or None when the repeated call reported no name+ (a failed call reports none) — then the verdict names no place rather than guessing one."""+ where = f" on {target}" if target else ""+ return (+ f"The run repeated the same action{where} and the page did not change in response, so the task "+ "could not make progress — commonly the site rejecting the action and showing the same page again."+ )++def _budget_exhausted_reason(cap_trip: str) -> str:"""Human sentence for a no-finish budget exit. Never includes the raw cap literal (that livesonly in `cap_trip` and the logs) so a status/reason readout doesn't leak an internal counter."""@@ -2146,7 +2301,7 @@ class LoopState:"""outcome: LoopOutcome | None = None- pending_nav_dead_end: int | None = None+ pending_nav_dead_end: _NavDeadEnd | None = Nonestall_nudges_due: list[tuple[str, int]] = field(default_factory=list)# Page-state stall detector (SKY-15265): consecutive billable rounds on a byte-identical# fingerprint, whether the one re-plan nudge went out, and whether one is due this turn.@@ -2230,6 +2385,12 @@ class LoopState:action_counts: dict[tuple[str, str], tuple[int, int]] = field(default_factory=dict)action_warned: set[tuple[str, str]] = field(default_factory=set)billable_actions: list[str] = field(default_factory=list)+ # Credential re-submit guard (SKY-16594), keyed on the placeholder token rather than the field:+ # the page re-renders between attempts, so an element-keyed rule is evaded by the very re-render+ # that precedes the second submit. Tokens entered since the last submit, and how many times each+ # has been submitted; entering one that has spent CREDENTIAL_SUBMIT_BUDGET is refused.+ credentials_entered: set[str] = field(default_factory=set)+ credential_submits: dict[str, int] = field(default_factory=dict)async def run_agent_tool_loop(@@ -2265,6 +2426,16 @@ async def run_agent_tool_loop(verification_blocker: VerificationBlocker | None = None,staged_downloads: set[str] | None = None,initial_navigation_status: int | None = None,+ # Only ever named in the dead-end verdict's text, so a caller that has the status but not the URL+ # (or whose URL is unfit to print) still gets the same verdict, minus the place.+ initial_navigation_url: str | None = None,+ # Every URL the CALLER gave this run, normalized by `caller_known_published_urls`. A guard verdict+ # publishes a landed URL's path only when it is one of these; every other path is elided to its+ # host. Computed once by the caller (it owns the task config) and never read by a tool.+ caller_known_urls: frozenset[str] = frozenset(),+ # Resolves the run's drop-check secret values when a verdict is about to name a page-supplied+ # element. Read at verdict time, not loop start: the registry grows as a run resolves credentials.+ label_secret_values: Callable[[], Collection[str]] | None = None,page_probe: Callable[[], Awaitable[str | None]] | None = None,reload_page: Callable[[], Awaitable[None]] | None = None,max_refresh_cycles: int = 3,@@ -2323,12 +2494,18 @@ async def run_agent_tool_loop(if st.refresh_cycles > max_refresh_cycles:# The queued calls were chosen on a page declared stale, so they are voided rather than# run; a page that keeps demanding a reload cannot be stabilized, and the run ends there.- LOG.warning("taskv3 loop refresh signal past cap", tool=tool_name, turn=st.turns)+ LOG.warning(+ "taskv3 loop refresh signal past cap",+ tool=tool_name,+ turn=st.turns,+ guard=PAGE_REFRESH_EXHAUSTED_GUARD,+ refresh_cycles=st.refresh_cycles,+ )_append_skipped_tool_results(st.messages, remaining, "the page could not be stabilized")- st.outcome = LoopOutcome(- "terminated",- f"{PAGE_REFRESH_EXHAUSTED_REASON_PREFIX} a page-level handler requested a page reload "- f"{st.refresh_cycles} times — the page cannot be stabilized, so the goal cannot progress on it",+ st.outcome = _guard_verdict(+ PAGE_REFRESH_EXHAUSTED_GUARD,+ "The page kept having to be reloaded and never settled into a state the run could act "+ "on, so the task could not continue there.",)return Trueif reload_page is not None:@@ -2554,15 +2731,16 @@ async def run_agent_tool_loop(tool=tool_name,identical_count=snap.live,turn=st.turns,+ guard=PERCEPTION_STALL_GUARD,**attribution,)return (- LoopOutcome(- "terminated",- f"{PERCEPTION_STALL_REASON_PREFIX} {snap.live} consecutive identical snapshots from "- f"one {tool_name} probe — the page stopped changing in response to actions, so the goal "- "cannot progress (commonly a blocker the run cannot perceive or operate, e.g. inside a "- "cross-origin frame)",+ _guard_verdict(+ PERCEPTION_STALL_GUARD,+ "The page stopped changing in response to the run's actions — every fresh read of it "+ "came back with the same content — so the task could not make progress there, "+ "commonly because something on the page is in the way that the run cannot see or "+ "operate, such as a prompt inside an embedded frame.",),stall_nudges,)@@ -2640,11 +2818,19 @@ async def run_agent_tool_loop(if should_cancel is not None and await should_cancel():st.outcome = LoopOutcome("canceled", "run canceled")else:- LOG.info("taskv3 loop initial navigation dead end", http_status=initial_navigation_status)- st.outcome = LoopOutcome(- "terminated",- f"{NAV_DEAD_END_REASON_PREFIX} the task's starting URL returned HTTP {initial_navigation_status} "- "— the target no longer exists or has been removed, so the goal cannot be completed there",+ LOG.info(+ "taskv3 loop initial navigation dead end",+ http_status=initial_navigation_status,+ guard=NAV_DEAD_END_GUARD,+ )+ st.outcome = _guard_verdict(+ NAV_DEAD_END_GUARD,+ _dead_end_reason(+ initial_navigation_status,+ initial_navigation_url,+ page_noun="The task's starting page",+ caller_known_urls=caller_known_urls,+ ),)while st.outcome is None:@@ -2916,6 +3102,36 @@ async def run_agent_tool_loop(})continue+ spent_credentials = (+ {+ token+ for token in _credential_placeholders(args)+ if st.credential_submits.get(token, 0) >= CREDENTIAL_SUBMIT_BUDGET+ }+ if tool_name in CREDENTIAL_ENTRY_TOOLS+ else set()+ )+ if spent_credentials:+ LOG.info(CREDENTIAL_RESUBMIT_REFUSED_EVENT, tool=tool_name, turn=st.turns)+ # Same reason the extraction refusal marks the batch: a click queued behind this call+ # would submit the value still sitting in the field, which is the act being refused.+ batch_had_failure = True+ st.messages.append(+ {+ "role": "tool",+ "tool_call_id": tool_call_id,+ "name": tool_name,+ "content": (+ "refused: this secret value has already been entered and submitted in this task. The "+ "page asking for it again means it was not accepted, and every further attempt spends a "+ "real allowance on the account behind it — a sign-in lockout budget, a card's retry "+ "limit — so it will not be entered again: retyping it, rewording it or putting it in a "+ "different field will all be refused the same way. Finish with a status that reflects "+ "the rejected value, or continue with whatever does not need it"+ ),+ }+ )+ continueif batch_had_failure and _may_submit(tool_name, args):st.messages.append({@@ -3207,6 +3423,19 @@ async def run_agent_tool_loop(result = ToolResult.error(f"tool_error: {type(exc).__name__}: {exc}", error_class=raised_class)tool_duration_seconds = time.monotonic() - tool_started_atst.tool_seconds += tool_duration_seconds+ # Entry is recorded only when the tool succeeded (a stale-ref failure put nothing in the+ # field), but the submit is recorded on DISPATCH whatever the verdict: the loop ran the+ # click itself, so nothing here asks the page whether a submission completed. Entry first,+ # so a type that pressed Enter submits the value it just entered.+ if spec is not None:+ if tool_name in CREDENTIAL_ENTRY_TOOLS and result.status == "ok":+ st.credentials_entered |= _credential_placeholders(args)+ if _may_submit(tool_name, args):+ # Cleared, so a second click on an untouched form is not a second submission of+ # a credential that was only ever entered once.+ for token in st.credentials_entered:+ st.credential_submits[token] = st.credential_submits.get(token, 0) + 1+ st.credentials_entered.clear()# Observe's summary counters are the only trace a perception change leaves on this# record; its content is deliberately never logged. Gated on the tool, not the payload,# so every other tool's record keeps exactly today's fields.@@ -3460,15 +3689,20 @@ async def run_agent_tool_loop(tool=tool_name,repeat_count=repeat_count,turn=st.turns,+ guard=ACTION_LOOP_GUARD,**attribution,)- st.outcome = LoopOutcome(- "terminated",- f"{ACTION_LOOP_REASON_PREFIX} {repeat_count} repeated {tool_name} attempts on "- f"{_action_target(args)} with no observed page change between attempts — the same "- "action against an unchanged outcome (commonly re-submitting into the same "- "rejection banner) cannot progress the goal",+ # The customer-facing half names the control by the page's own accessible name, never+ # the selector the model's nudge uses: that selector is usually v3's own `data-tv3`+ # marker, which matches nothing in the customer's markup (SKY-16271).+ named_target = _verdict_target(+ tool_name,+ result_data.get(TARGET_LABEL_DATA_KEY),+ result_data.get(TARGET_KIND_DATA_KEY),+ label_secret_values,+ skyvern_ctx,)+ st.outcome = _guard_verdict(ACTION_LOOP_GUARD, _action_loop_reason(named_target))_append_skipped_tool_results(st.messages, tool_calls[idx + 1 :], "action loop")breakif (@@ -3542,7 +3776,7 @@ async def run_agent_tool_loop(# A hard 404/410 landing is a non-capability dead-end (a dead/removed posting). Remember# it but do NOT break the batch: a later navigate in the same turn can land the run on a# live page and clear it below. Applied once the batch settles (after this for-loop).- st.pending_nav_dead_end = dead_end_status+ st.pending_nav_dead_end = _NavDeadEnd(dead_end_status, result_data.get("navigation_dead_end_url"))elif result_data.get("page_state_changed"):# A successful navigate moved the run off any dead page seen earlier this batch.st.pending_nav_dead_end = None@@ -3617,11 +3851,20 @@ async def run_agent_tool_loop(# recovered): end the run as terminated deterministically, matching v1, rather than leaving the# failed/terminated choice to the model's finish tool (which does not converge on this class).if st.outcome is None and st.pending_nav_dead_end is not None:- LOG.info("taskv3 loop navigation dead end", http_status=st.pending_nav_dead_end, turn=st.turns)- st.outcome = LoopOutcome(- "terminated",- f"{NAV_DEAD_END_REASON_PREFIX} navigate landed on a dead page (HTTP {st.pending_nav_dead_end}) — "- "the target no longer exists or has been removed, so the goal cannot be completed there",+ LOG.info(+ "taskv3 loop navigation dead end",+ http_status=st.pending_nav_dead_end.status,+ turn=st.turns,+ guard=NAV_DEAD_END_GUARD,+ )+ st.outcome = _guard_verdict(+ NAV_DEAD_END_GUARD,+ _dead_end_reason(+ st.pending_nav_dead_end.status,+ st.pending_nav_dead_end.url,+ page_noun="The page the run opened",+ caller_known_urls=caller_known_urls,+ ),)# Page-state stall detector (SKY-15265): tool-independent — any batch of billable work thatdiff --git a/skyvern/forge/taskv3/opaque_refs.py b/skyvern/forge/taskv3/opaque_refs.pyindex 16a41be90..35c8c35c4 100644--- a/skyvern/forge/taskv3/opaque_refs.py+++ b/skyvern/forge/taskv3/opaque_refs.py@@ -1,7 +1,9 @@"""Signed payload URLs reach the model only as opaque tokens, minted here and resolved inside the toolhandlers the way credential placeholders are.-Never import this from tools.py, loop.py, or auth_tools.py: it imports auth_tools, so that would be a cycle."""+This module owns the URL/token SHAPE predicates the rest of v3 redacts against, so it must stay a+leaf: it imports nothing else from taskv3, and auth_tools/handoff_redaction import from it. Reversing+any of those edges reintroduces a cycle through loop.py."""from __future__ import annotations@@ -14,7 +16,6 @@ from urllib.parse import unquote, unquote_plus, urlsplitimport structlogfrom skyvern.forge.sdk.core.skyvern_context import mask_opaque_urls_in_text-from skyvern.forge.taskv3.auth_tools import _MIN_REDACTED_QUERY_VALUE_CHARS, _OPAQUE_QUERY_VALUE_RELOG = structlog.get_logger()@@ -87,6 +88,12 @@ _HEX_BLOB_RE = re.compile(r"[0-9a-fA-F]{32,}")# above (a lower bar catches more benign short/single-case identifiers, not just more real tokens)._MIN_SHAPE_ONLY_SIGNING_VALUE_CHARS = 32_DIGIT_RUN_RE = re.compile(r"\d{4,}")+# Shorter values are codes/flags (lang=en, v=2), not link secrets, and a real link secret is at+# least this long; redacting the short ones would blank harmless text across the run's artifacts.+_MIN_REDACTED_QUERY_VALUE_CHARS = 16+# The charset an opaque token draws from. Excludes emails, URLs, and prose, which are readable+# values the model needs and redaction is global for the run.+_OPAQUE_QUERY_VALUE_RE = re.compile(r"[A-Za-z0-9._~+/=-]+")def _is_signing_value(decoded: str) -> bool:@@ -127,6 +134,13 @@ def _is_high_entropy_blob(decoded: str) -> bool:)+def urls_in_text(text: str) -> list[str]:+ """Every http(s) URL literal in free ``text``, with sentence punctuation split off the end the way+ ``OpaqueUrlRefs.mint_in_text`` splits it — one scan, so a reader that has to decide whether a URL+ was written by the caller sees exactly the URLs the masker would have seen."""+ return [match.group(0).rstrip(_TRAILING_PUNCTUATION) for match in _URL_IN_TEXT_RE.finditer(text)]++def _signing_values(part: str) -> list[tuple[str, str]]:"""Return (raw, decoded) for each ``key=value`` pair of a query/fragment string that is a signingsecret: either the key names one, or - when it doesn't - the decoded value's own shape (a JWT, or adiff --git a/skyvern/forge/taskv3/target_label.py b/skyvern/forge/taskv3/target_label.pyindex 62237583b..0b2920af0 100644--- a/skyvern/forge/taskv3/target_label.py+++ b/skyvern/forge/taskv3/target_label.py@@ -218,25 +218,33 @@ _FAILED_TARGET_VERBS = {}-def compose_target_intention(- tool: str, name: Any, kind: Any, secret_values: Collection[str], *, succeeded: bool = True-) -> str | None:- """The LLM-free timeline label for one v3 tool call. `kind` is only a lookup key, so the floor never- contains page text; `name` enriches it only after passing the shape filter and the secret matcher."""- verb = (TARGET_VERBS if succeeded else _FAILED_TARGET_VERBS).get(tool)- if verb is None:- return None+def describe_target(tool: str, name: Any, kind: Any, secret_values: Collection[str]) -> str:+ """How one acted-on element reads to a person: `the "Continue" button`, or the kind floor+ (`a button`) when the page's own name cannot be shown. `kind` is only a lookup key, so the floor+ never contains page text, and `name` enriches it only after passing the shape filter and the secret+ matcher -- shared by the timeline label below and the loop's action-loop verdict, so a control+ reads the same way in both."""resolved_kind = kind if isinstance(kind, str) and kind in _KIND_NOUNS else _DEFAULT_KIND_BY_TOOL.get(tool)floor_phrase, named_noun = _GENERIC_KIND_NOUNS if resolved_kind is None else _KIND_NOUNS[resolved_kind]display = _enrichable_name(name, secret_values)if display is None:- return f"{verb} {floor_phrase}"+ return floor_phraseif named_noun is None:- return f'{verb} "{display}"'+ return f'"{display}"'folded = display.casefold()if folded == named_noun or folded.endswith(f" {named_noun}"):- return f'{verb} the "{display}"'- return f'{verb} the "{display}" {named_noun}'+ return f'the "{display}"'+ return f'the "{display}" {named_noun}'+++def compose_target_intention(+ tool: str, name: Any, kind: Any, secret_values: Collection[str], *, succeeded: bool = True+) -> str | None:+ """The LLM-free timeline label for one v3 tool call."""+ verb = (TARGET_VERBS if succeeded else _FAILED_TARGET_VERBS).get(tool)+ if verb is None:+ return None+ return f"{verb} {describe_target(tool, name, kind, secret_values)}"def _enrichable_name(name: Any, secret_values: Collection[str]) -> str | None:diff --git a/skyvern/forge/taskv3/tools.py b/skyvern/forge/taskv3/tools.pyindex f3faa6f45..606e899de 100644--- a/skyvern/forge/taskv3/tools.py+++ b/skyvern/forge/taskv3/tools.py@@ -4275,6 +4275,76 @@ _ANCHOR_LIST_SEMANTICS_JS = (}""")+# A segmented date input's group: the target must itself be a spinbutton whose own aria-label is+# month/day/year -- not merely sitting near a group that has one, e.g. an unrelated ID spinbutton+# sharing a fieldset with a real date group -- and its nearest fieldset/[role=group] ancestor (or,+# failing that, its plain parent) must contain exactly one editable spinbutton per month/day/year+# aria-label. Meaning comes from each segment's aria-label,+# never DOM position, so a DD/MM/YYYY visual layout still resolves each component to the right+# segment. Tags the three winning elements data-tv3-dateseg="month"/"day"/"year" so the caller can+# address them individually; returns ok:false (and clears any stale tags) on anything short of a+# strict bijection, so a duplicate, missing, or readonly/disabled segment falls back to today's path.+_DATE_SEGMENT_GROUP_JS = (+ r"""(arg) => {+ const _q = """+ + _ROOT_QUERY_JS+ + r""";+ const LABELS = ['month', 'day', 'year'];+ const norm = (el) => String(el.getAttribute('aria-label') || '').trim().toLowerCase();+ const isSpinbutton = (el) => (el.getAttribute('role') || '').trim().toLowerCase() === 'spinbutton';+ const editable = (el) => {+ try { if (el.disabled === true || el.readOnly === true) return false; } catch (e) { /* not a form control */ }+ if (el.hasAttribute('disabled') || el.hasAttribute('readonly')) return false;+ const ro = String(el.getAttribute('aria-readonly') || '').trim().toLowerCase();+ const da = String(el.getAttribute('aria-disabled') || '').trim().toLowerCase();+ return ro !== 'true' && da !== 'true';+ };+ _q.all('[data-tv3-dateseg]').forEach((e) => e.removeAttribute('data-tv3-dateseg'));+ const target = _q.find(arg.sel) || (arg.el && arg.el.isConnected ? arg.el : null);+ if (!target || !isSpinbutton(target)) return { ok: false, reason: 'not_spinbutton', targetLabel: null };+ const targetLabel = norm(target) || null;+ if (LABELS.indexOf(targetLabel) === -1) return { ok: false, reason: 'target_not_date_segment', targetLabel };+ let group = target.parentElement;+ let depth = 0;+ while (+ group && depth < 8 && group.tagName !== 'FIELDSET'+ && !/(^|\s)group(\s|$)/i.test(group.getAttribute('role') || '')+ ) {+ group = group.parentElement;+ depth++;+ }+ if (!group) group = target.parentElement;+ if (!group) return { ok: false, reason: 'no_group', targetLabel };+ const found = {};+ // Document order as the scan actually encounters the segments -- this is the widget's own+ // day-vs-month arrangement, and it is what a caller uses to disambiguate an ambiguous value.+ const order = [];+ let duplicate = false;+ let notEditable = false;+ Array.from(group.querySelectorAll('[role]')).forEach((el) => {+ if (!isSpinbutton(el)) return;+ const lab = norm(el);+ if (LABELS.indexOf(lab) === -1) return;+ if (found[lab]) { duplicate = true; return; }+ if (!editable(el)) notEditable = true;+ found[lab] = el;+ order.push(lab);+ });+ if (duplicate) return { ok: false, reason: 'duplicate', targetLabel };+ if (LABELS.some((l) => !found[l])) return { ok: false, reason: 'missing', targetLabel };+ if (notEditable) return { ok: false, reason: 'not_editable', targetLabel };+ LABELS.forEach((l) => found[l].setAttribute('data-tv3-dateseg', l));+ return { ok: true, reason: null, targetLabel, order };+}"""+)++# Both `value` and `textContent` -- some spinbutton variants drop page-level keystrokes and only+# render the committed digits as textContent, so a check reading `value` alone reports failure on a+# segment that actually filled.+_DATE_SEGMENT_READBACK_JS = (+ "el => [el.value, el.textContent].filter(v => v != null && String(v).trim() !== '').join('|')"+)+# Read back after a forced select_option so a styled proxy that silently didn't sync from its# native control is caught rather than reported as a successful selection._SELECT_READBACK_JS = (@@ -10800,6 +10870,164 @@ def build_browser_tools(except Exception:return "text"+ _DATE_SEGMENT_ORDER: tuple[str, str, str] = ("month", "day", "year")+ _DATE_SEGMENT_DIGITS_RE = re.compile(r"\d+")+ # The 4-digit year's position is what disambiguates the text's ARRANGEMENT: last means the other+ # two components are a month/day pair in some order (still ambiguous by text alone), first means+ # year-month-day (unambiguous ISO order). A separator-less run (e.g. "09182026") carries no such+ # signal, so it matches neither shape and falls through rather than being guessed.+ _DATE_TEXT_YEAR_LAST_RE = re.compile(r"^(\d{1,2})[\s/\-.](\d{1,2})[\s/\-.](\d{4})$")+ _DATE_TEXT_YEAR_FIRST_RE = re.compile(r"^(\d{4})[\s/\-.](\d{1,2})[\s/\-.](\d{1,2})$")++ def _parse_typed_date_text(text: str) -> tuple[str, str, str, str] | None:+ # Stage 1 (cheap, no DOM access): extract the three numeric components and how they're+ # arranged in the text. For the year-last form this deliberately leaves open which of the+ # first two is month and which is day -- stage 2 (_resolve_date_components) decides that from+ # the widget's own segment order, not from the text.+ stripped = text.strip()+ if not stripped.isascii():+ return None+ match = _DATE_TEXT_YEAR_LAST_RE.fullmatch(stripped)+ if match:+ a, b, year_s = match.groups()+ return "year_last", a, b, year_s+ match = _DATE_TEXT_YEAR_FIRST_RE.fullmatch(stripped)+ if match:+ year_s, month_s, day_s = match.groups()+ return "year_first", month_s, day_s, year_s+ return None++ def _resolve_date_components(parsed: tuple[str, str, str, str], order: Any) -> dict[str, str] | None:+ # Stage 2: year-first is unambiguous (ISO year-month-day) regardless of the widget. Year-last+ # is resolved from the widget's own document order -- if it renders day before month, the+ # text's first numeric component is the day. With no usable order, default to month-first+ # (today's historical reading) rather than guess a flip.+ kind, a, b, year_s = parsed+ if kind == "year_first":+ month_s, day_s = a, b+ else:+ has_both = isinstance(order, list) and "day" in order and "month" in order+ day_first = has_both and order.index("day") < order.index("month")+ month_s, day_s = (b, a) if day_first else (a, b)+ try:+ month, day = int(month_s), int(day_s)+ except ValueError:+ return None+ # Ranges are validated AFTER assignment, not before: a leading component over 12 is only+ # invalid once we know it was assigned to month, and a day-first widget may assign it to day.+ if not (1 <= month <= 12) or not (1 <= day <= 31):+ return None+ return {"month": f"{month:02d}", "day": f"{day:02d}", "year": year_s}++ async def _date_segment_group(page: Any, selector: str) -> dict[str, Any]:+ try:+ probe = await page.evaluate(_DATE_SEGMENT_GROUP_JS, await _probe_arg(page, selector))+ except Exception:+ return {"ok": False, "reason": None, "targetLabel": None}+ return probe if isinstance(probe, dict) else {"ok": False, "reason": None, "targetLabel": None}++ async def _read_date_segment(page: Any, selector: str) -> str:+ try:+ raw = await page.locator(selector).first.evaluate(_DATE_SEGMENT_READBACK_JS, timeout=2000)+ except Exception:+ return ""+ return str(raw) if isinstance(raw, str) else ""++ def _date_segment_committed(rendered: str, expected_digits: str) -> bool:+ match = _DATE_SEGMENT_DIGITS_RE.search(rendered)+ if not match:+ return False+ digits = match.group(0)+ # A read-back longer than the expected width means digits piled up on top of something already+ # in the segment (an unclearer retry, a pre-filled value) -- e.g. "010" for an expected "10"+ # normalizes to the same int but is not the same commit, so length must agree too.+ if len(digits) > len(expected_digits):+ return False+ try:+ return int(digits) == int(expected_digits)+ except ValueError:+ return False++ async def _date_segment_holds(page: Any, selector: str, expected_digits: str) -> bool:+ for _ in range(6):+ if _date_segment_committed(await _read_date_segment(page, selector), expected_digits):+ return True+ await asyncio.sleep(0.15)+ return False++ async def _clear_date_segment(locator: Any) -> None:+ # A spinbutton's focus does not necessarily select its contents, so without an explicit clear a+ # retry or a pre-filled segment would have its new digits appended rather than replacing what's+ # already there. Backspace is what a real accessible date-segment spinbutton clears itself with+ # (a select-all chord is not universally safe here -- a real widget lost its whole render on+ # one); four presses covers the widest (4-digit year) segment even on a widget that only+ # deletes one character per press, and is a no-op once a segment is already empty.+ try:+ for _ in range(4):+ await locator.press("Backspace")+ except Exception:+ pass++ async def _type_one_date_segment(page: Any, selector: str, digits: str) -> bool:+ locator = page.locator(selector).first+ try:+ await locator.scroll_into_view_if_needed(timeout=2000)+ except Exception:+ pass+ try:+ await locator.focus(timeout=2000)+ except Exception:+ return False+ await _clear_date_segment(locator)+ # locator.fill() does not commit digits into a date spinbutton segment; only real keystrokes+ # advance it, so this and the element-focused fallback below both TYPE rather than fill.+ try:+ await _current_page().keyboard.type(digits, delay=40)+ except Exception:+ pass+ if await _date_segment_holds(page, selector, digits):+ return True+ try:+ await locator.focus(timeout=2000)+ await _clear_date_segment(locator)+ await locator.press_sequentially(digits, delay=40)+ except Exception:+ return False+ return await _date_segment_holds(page, selector, digits)++ async def _fill_date_segment_group(page: Any, selector: str, components: dict[str, str]) -> ToolResult:+ for label in _DATE_SEGMENT_ORDER:+ committed = await _type_one_date_segment(page, f'[data-tv3-dateseg="{label}"]', components[label])+ LOG.info("taskv3 date segment fill", segment=label, committed=committed)+ if not committed:+ return ToolResult.error(+ f"typed a date into {selector}'s segmented date field, but the {label} segment did not "+ "commit its value afterward -- the field is NOT filled and may hold a partial date. "+ "Re-observe and retry, filling the remaining segment(s) yourself if this one held."+ )+ # The last segment typed still holds focus here, so a widget that normalizes or clamps a+ # segment's value only on blur has not fired that yet -- blur it and let the widget settle+ # briefly before the read-back that decides success, so that drift surfaces as the error below+ # instead of a claimed success the very next click then clamps or clears.+ try:+ await page.evaluate("() => { const a = document.activeElement; if (a) a.blur(); }")+ except Exception:+ pass+ await asyncio.sleep(0.15)+ # A segment can also be rewritten on an EARLIER segment's blur, which fires only once focus+ # moves to the next segment -- so this re-check catches drift there too, not just on the last one.+ for label in _DATE_SEGMENT_ORDER:+ rendered = await _read_date_segment(page, f'[data-tv3-dateseg="{label}"]')+ if not _date_segment_committed(rendered, components[label]):+ return ToolResult.error(+ f"typed a date into {selector}'s segmented date field; all three segments committed, but "+ f"the {label} segment changed afterward, so the field may now hold a different date than "+ "requested. The page changed it, and it was left as the page set it. Re-observe it to see "+ "what value the field holds.",+ error_class="value_changed_by_page",+ )+ return ToolResult.ok(f"typed into {selector}; filled its month/day/year segments from one date")+async def type_text(args: dict[str, Any]) -> ToolResult:page, error = await _resolve_page()if error is not None:@@ -10814,6 +11042,27 @@ def build_browser_tools(text = _resolve_text(args.get("text", ""))press_enter = args.get("press_enter")clear = args.get("clear", True)+ # A segmented date input truncates a whole date typed into one segment at that segment's+ # maxlength, so a confirmed month/day/year group is filled segment by segment instead. The+ # textual shape check is free and runs first; the DOM probe only runs once the text could+ # possibly be a date, so ordinary typing never pays for it.+ if text and clear and not press_enter:+ parsed_date = _parse_typed_date_text(text)+ if parsed_date is not None:+ group = await _date_segment_group(page, selector)+ if group.get("ok"):+ date_components = _resolve_date_components(parsed_date, group.get("order"))+ if date_components is not None:+ # Mirrors the reachability/occluder guard the normal typing path below+ # performs -- this path types via focus()+keyboard too, so a date field under+ # a modal or consent wall must not silently report success either.+ try:+ reachable, _, occluder = await _reachable_for_typing(page, selector)+ except _FieldNotEditable as exc:+ return _not_editable_error(exc)+ if not reachable:+ return _covered_error(selector, occluder)+ return await _fill_date_segment_group(page, selector, date_components)# A typeahead silently rejects raw typed text — it only accepts a picked suggestion — and the# model does not reliably reach for select_combobox on its own. So after typing into a plain text# field, check whether the page REACTED with a suggestion list and, if so, commit the best match@@ -12156,9 +12405,17 @@ def build_browser_tools(_recent_nav_canonicals.append(pre_nav_canonical)_recent_nav_canonicals.append(landed_canonical)# A hard 404/410 landing is a dead/removed target: flag it so the loop ends the run as- # terminated (v1's behavior) rather than defaulting the outcome to failed.+ # terminated (v1's behavior) rather than defaulting the outcome to failed. The landing URL rides+ # along because the loop's verdict names the dead page to the customer, and only this call knows+ # where the status came from -- after a redirect that is not the URL it was asked for.if response is not None and response.status in NAVIGATION_DEAD_END_STATUSES:data["navigation_dead_end"] = response.status+ # The response's OWN url, not the page's: a dead document that rewrites the address bar as+ # it loads (history.replaceState) moves `landed` off the URL the status came back on, and+ # the verdict would name a page no response was received for. The page URL remains the+ # fallback for an engine whose response object does not carry one.+ response_url = getattr(response, "url", None)+ data["navigation_dead_end_url"] = response_url if isinstance(response_url, str) and response_url else landed# What the persisted action row says happened. The requested URL is the model's own argument# (a placeholder or payload ref must not be unwrapped into a row), and the dead-end status is# repeated from the loop's signal above because the row never sees that one.diff --git a/skyvern/webeye/browser_state.py b/skyvern/webeye/browser_state.pyindex d54f1b11a..65bc258b3 100644--- a/skyvern/webeye/browser_state.py+++ b/skyvern/webeye/browser_state.py@@ -32,6 +32,9 @@ class BrowserState(Protocol):# HTTP status of the most recent navigate_to_url (None until one runs, or when it produced no# response). The Task V3 loop reads it to classify a dead/removed starting URL.last_navigation_status: int | None+ # The URL that status came back on, recorded from the same response: the verdict naming a dead page+ # must name the page the status belongs to, not wherever the page went afterwards.+ last_navigation_url: str | None# The proxy this browser was actually built with. A consumer reading a flattened failure# sentence cannot tell which hop it went through; this carries that fact from where it is known.built_with_proxy_location: ProxyLocationInputdiff --git a/skyvern/webeye/real_browser_state.py b/skyvern/webeye/real_browser_state.pyindex 698cbfd09..cbd6b7208 100644--- a/skyvern/webeye/real_browser_state.py+++ b/skyvern/webeye/real_browser_state.py@@ -165,6 +165,8 @@ class RealBrowserState(BrowserState):# HTTP status of the most recent navigate_to_url (None until one runs, or when it produced no# response). Read by the Task V3 loop to classify a dead/removed starting URL; v1 ignores it.self.last_navigation_status: int | None = None+ # The URL that status came back on, recorded from the same response so the pair cannot disagree.+ self.last_navigation_url: str | None = None# The proxy this browser was actually built with. Downstream layers see only a flattened# sentence, which cannot say which hop it went through.self.built_with_proxy_location: ProxyLocationInput = None@@ -470,13 +472,28 @@ class RealBrowserState(BrowserState):retry_times: int = NAVIGATION_MAX_RETRY_TIME,wait_until: Literal["load", "domcontentloaded", "commit"] = "load",) -> None:+ landed_url: str | None = None++ async def goto(strategy: str) -> object:+ nonlocal landed_url+ response = await page.goto(url, timeout=settings.BROWSER_LOADING_TIMEOUT_MS, wait_until=strategy)+ # Read off the SAME response the status is read from (duck-typed like _navigation_status:+ # whichever engine's response object this is, its url is the last redirect hop's). The settle+ # wait inside navigate_with_retry and the challenge-solver wait below both give a client-side+ # redirect time to move page.url off the URL the status belongs to, so a later read of the+ # page would pair the status with a page the status was never about (SKY-16271).+ response_url = getattr(response, "url", None)+ landed_url = response_url if isinstance(response_url, str) else None+ return response+self.last_navigation_status = await navigate_with_retry(- navigate=lambda strategy: page.goto(url, timeout=settings.BROWSER_LOADING_TIMEOUT_MS, wait_until=strategy),+ navigate=goto,url=url,retry_times=retry_times,settle=self._wait_for_settle,wait_until=wait_until,)+ self.last_navigation_url = landed_urlawait self._wait_for_challenge_solver(page=page)async def _wait_for_challenge_solver(self, page: Page) -> None:diff --git a/tests/unit/force_stub_app.py b/tests/unit/force_stub_app.pyindex 6ac91e8d3..047f586a2 100644--- a/tests/unit/force_stub_app.py+++ b/tests/unit/force_stub_app.py@@ -6,6 +6,7 @@ from skyvern.config import settingsfrom skyvern.forge import set_force_app_instancefrom skyvern.forge.agent_functions import AgentFunctionfrom skyvern.forge.forge_app import ForgeApp+from skyvern.forge.sdk.copilot.browser_ablation import CopilotBrowserCodeModefrom skyvern.forge.sdk.workflow.models.workflow import WorkflowRunStatus@@ -57,6 +58,9 @@ def create_forge_stub_app() -> ForgeApp:# truthy AsyncMocks and hijack CodeBlock.execute into the runner path. Match# the real OSS base no-op so unit tests exercise the legacy in-process path.fake_app_module.AGENT_FUNCTION.should_use_codeblock_runner = AsyncMock(return_value=False)+ # Same footgun on the copilot browser-code gate: an auto-mocked mode would offer run_browser_code+ # instead of the OSS default.+ fake_app_module.AGENT_FUNCTION.copilot_browser_code_mode = AsyncMock(return_value=CopilotBrowserCodeMode.OFF)fake_app_module.AGENT_FUNCTION.execute_code_block_override = AsyncMock(return_value=None)base_agent_function = AgentFunction()# Class constant, not a method — _LazyNamespace would auto-mock it into a non-iterable AsyncMockdiff --git a/tests/unit/forge/sdk/copilot/test_browser_ablation_mode.py b/tests/unit/forge/sdk/copilot/test_browser_ablation_mode.pyindex 96a2107c5..060704102 100644--- a/tests/unit/forge/sdk/copilot/test_browser_ablation_mode.py+++ b/tests/unit/forge/sdk/copilot/test_browser_ablation_mode.py@@ -18,6 +18,7 @@ from skyvern.forge.sdk.copilot.browser_ablation import (REPAIR_PROBE_TOOL,CopilotEvalMode,CopilotToolSurface,+ CopilotToolSurfaceIdentity,config_for_eval_mode,prompt_sha256,prompt_template_for_mode,@@ -27,8 +28,10 @@ from skyvern.forge.sdk.copilot.config import CopilotConfigfrom skyvern.forge.sdk.copilot.mcp_adapter import BROWSER_TARGET_PARAM_NAME, SchemaOverlay, SkyvernOverlayMCPServerfrom skyvern.forge.sdk.copilot.tools import (BROWSER_BOUND_TOOL_NAMES,+ BROWSER_CODE_TOOL_NAME,NATIVE_TOOLS,_build_skyvern_mcp_overlays,+ copilot_native_tools,get_skyvern_mcp_alias_map,)from skyvern.forge.sdk.schemas.workflow_copilot import (@@ -150,6 +153,7 @@ def test_a_turn_without_browser_authority_advertises_no_browser_tool() -> None:)assert BROWSER_BOUND_TOOL_NAMES.isdisjoint(withheld.ordered_native_names)+ assert BROWSER_CODE_TOOL_NAME not in withheld.ordered_native_namesassert withheld.ordered_native_namesassert withheld.ordered_mcp_names == ("get_workflow_knowledge",@@ -239,9 +243,11 @@ async def _advertised_schemas(def _production_surfaces(registered: list[RegisteredTool]) -> tuple[CopilotToolSurface, CopilotToolSurface]:aliases = get_skyvern_mcp_alias_map()overlays = _build_skyvern_mcp_overlays()+ # The direct browser aliases this file compares live on the surface a deployment gets when the+ # browser-code tool is not available to it; where it is, they are withdrawn.normal = resolve_copilot_tool_surface(mode=None,- native_tools=list(NATIVE_TOOLS),+ native_tools=copilot_native_tools(supports_question_tool=True, browser_code_available=False),alias_map=aliases,overlays=overlays,)@@ -590,3 +596,23 @@ def test_the_on_arm_keeps_the_probe_tool() -> None:)assert surface.ordered_native_names == ("run_blocks", REPAIR_PROBE_TOOL, "update_workflow")+++async def test_browser_ablation_keeps_its_catalog_though_the_code_tool_is_on_the_native_list() -> None:+ """The ablation arms measure the direct-browser surface, so the code tool being available must+ not divert them to the required-code projection."""+ registered = await mcp.list_tools(run_middleware=False)+ native = list(NATIVE_TOOLS)+ assert "run_browser_code" in {tool.name for tool in native}++ surface = resolve_copilot_tool_surface(+ mode=CopilotEvalMode.BROWSER_ABLATION,+ native_tools=native,+ alias_map=get_skyvern_mcp_alias_map(),+ overlays=_build_skyvern_mcp_overlays(),+ registered_mcp_tools=registered,+ )++ assert surface.identity == CopilotToolSurfaceIdentity.BROWSER_ABLATION+ assert surface.ordered_mcp_names == _EXPECTED_BROWSER_ABLATION_MCP_TOOLSdiff --git a/tests/unit/forge/sdk/copilot/test_browser_code_required_surface.py b/tests/unit/forge/sdk/copilot/test_browser_code_required_surface.pynew file mode 100644index 000000000..5c91d3dee--- /dev/null+++ b/tests/unit/forge/sdk/copilot/test_browser_code_required_surface.py@@ -0,0 +1,251 @@+from __future__ import annotations++import dataclasses+import json+from types import SimpleNamespace+from typing import Any++import pytest+from agents import RunContextWrapper+from agents.tool import invoke_function_tool+from agents.tool_context import ToolContext+from mcp.types import Tool as MCPTool++from skyvern.forge.sdk.copilot.browser_ablation import (+ CopilotBrowserCodeMode,+ CopilotEvalMode,+ CopilotToolSurface,+ CopilotToolSurfaceIdentity,+ dispatch_allowlist_enforced,+ resolve_copilot_tool_surface,+)+from skyvern.forge.sdk.copilot.mcp_adapter import SkyvernOverlayMCPServer+from skyvern.forge.sdk.copilot.tools import (+ NATIVE_TOOLS,+ _build_skyvern_mcp_overlays,+ copilot_native_tools,+ get_skyvern_mcp_alias_map,+)+from skyvern.forge.sdk.copilot.tools._shared import _CURRENT_PAGE_INSPECTION_TARGETS+from skyvern.forge.sdk.copilot.tools.composition_capture import (+ CURRENT_PAGE_INSPECTION_TARGET,+ current_page_inspection_tool,+)+from skyvern.forge.sdk.copilot.unrecoverable_tool_error import _is_unrecoverable_browser_session_error+from tests.unit.copilot_test_helpers import make_copilot_ctx+from tests.unit.test_copilot_secret_scrub import _FakeClient++# The browser-bound tools the surface still advertises. Tools that need no browser are kept by+# rule rather than by name, so this is the whole browser-facing keep list.+_REQUIRED_BROWSER_READ_NAMES = {+ "get_browser_screenshot",+ "console_messages",+ "wait_for_either_state",+ "skyvern_frame_list",+}+_REQUIRED_WORKFLOW_KNOWLEDGE_NAMES = {"get_workflow_knowledge", "get_block_schema", "validate_block"}+_REMOVED_MODEL_FACING_NAMES = {+ "navigate_browser",+ "click",+ "type_text",+ "scroll",+ "select_option",+ "press_key",+ "skyvern_frame_switch",+ "skyvern_frame_main",+ "evaluate",+ "discover_workflow_entrypoint",+}+++def _surface(*, browser_code_available: bool) -> CopilotToolSurface:+ return resolve_copilot_tool_surface(+ mode=None,+ native_tools=copilot_native_tools(supports_question_tool=True, browser_code_available=browser_code_available),+ alias_map=get_skyvern_mcp_alias_map(),+ overlays=_build_skyvern_mcp_overlays(),+ browser_code_mode=CopilotBrowserCodeMode.REPLACE if browser_code_available else CopilotBrowserCodeMode.OFF,+ )+++def test_the_required_surface_drops_the_direct_and_mixed_action_routes() -> None:+ optional = _surface(browser_code_available=False)+ required = _surface(browser_code_available=True)++ overlays = _build_skyvern_mcp_overlays()++ assert required.identity == CopilotToolSurfaceIdentity.REQUIRED_CODE+ assert {name for name in required.ordered_mcp_names if overlays[name].requires_browser} == (+ _REQUIRED_BROWSER_READ_NAMES+ )+ assert _REQUIRED_WORKFLOW_KNOWLEDGE_NAMES <= set(required.ordered_mcp_names)+ removed = (set(optional.ordered_mcp_names) | set(optional.ordered_native_names)) - (+ set(required.ordered_mcp_names) | set(required.ordered_native_names)+ )+ assert removed == _REMOVED_MODEL_FACING_NAMES+ assert {+ "run_browser_code",+ "fill_credential_field",+ "request_credential",+ "inspect_locator_matches",+ "inspect_page_for_composition",+ "ask_user",+ "update_workflow",+ "run_blocks_and_collect_debug",+ "get_run_results",+ } <= set(required.ordered_native_names)+ assert required.sha256 != optional.sha256+ assert set(required.alias_map) == set(required.overlays) == set(required.ordered_mcp_names)+++def test_a_deployment_without_the_code_tool_keeps_the_browser_tools() -> None:+ """Where the code tool is not offered, the surface is the parent's, so a deployment the rollout+ does not reach is untouched by this change."""+ optional = _surface(browser_code_available=False)++ assert optional.identity == CopilotToolSurfaceIdentity.OPTIONAL+ assert "run_browser_code" not in optional.ordered_native_names+ assert {"click", "type_text", "evaluate", "navigate_browser"} <= set(optional.ordered_mcp_names)+++def test_the_locator_probe_arms_follow_the_production_identity() -> None:+ surface = resolve_copilot_tool_surface(+ mode=CopilotEvalMode.REPAIR_PROBE_OFF,+ native_tools=copilot_native_tools(supports_question_tool=True, browser_code_available=True),+ alias_map=get_skyvern_mcp_alias_map(),+ overlays=_build_skyvern_mcp_overlays(),+ browser_code_mode=CopilotBrowserCodeMode.REPLACE,+ )++ assert surface.identity == CopilotToolSurfaceIdentity.REQUIRED_CODE+ assert "inspect_locator_matches" not in surface.ordered_native_names+ assert surface.ordered_mcp_names == _surface(browser_code_available=True).ordered_mcp_names+++def test_only_a_narrowed_surface_enforces_the_dispatch_allowlist() -> None:+ assert dispatch_allowlist_enforced(_surface(browser_code_available=True).identity)+ assert dispatch_allowlist_enforced(CopilotToolSurfaceIdentity.BROWSER_ABLATION)+ assert not dispatch_allowlist_enforced(_surface(browser_code_available=False).identity)+ assert not dispatch_allowlist_enforced(None)+++async def test_the_optional_surface_dispatches_a_raw_transport_name_as_before() -> None:+ optional = _surface(browser_code_available=False)+ dispatched: list[str] = []+ server = SkyvernOverlayMCPServer(+ transport=SimpleNamespace(),+ overlays=optional.overlays,+ alias_map=optional.alias_map,+ allowlist=frozenset(optional.alias_map.values()),+ context_provider=lambda: make_copilot_ctx(browser_session_id="pbs_1"),+ enforce_dispatch_allowlist=dispatch_allowlist_enforced(optional.identity),+ )+ server._client = _FakeClient({"ok": True}, on_call=lambda: dispatched.append("called"))++ result = await server._call_tool("skyvern_workflow_knowledge", {})++ assert dispatched == ["called"]+ assert result.isError is False+++def _required_surface_server(client: _FakeClient) -> SkyvernOverlayMCPServer:+ required = _surface(browser_code_available=True)+ server = SkyvernOverlayMCPServer(+ transport=SimpleNamespace(),+ overlays=required.overlays,+ alias_map=required.alias_map,+ allowlist=frozenset(required.alias_map.values()),+ context_provider=lambda: make_copilot_ctx(browser_session_id="pbs_1"),+ enforce_dispatch_allowlist=dispatch_allowlist_enforced(required.identity),+ )+ server._client = client+ return server+++ "tool_name", ["click", "type_text", "evaluate", "navigate_browser", "skyvern_click", "skyvern_evaluate"]+)+async def test_a_removed_or_raw_name_is_refused_before_the_transport(tool_name: str) -> None:+ dispatched: list[str] = []+ server = _required_surface_server(_FakeClient({"ok": True}, on_call=lambda: dispatched.append(tool_name)))++ with pytest.raises(ValueError, match="not available"):+ await server._call_tool(tool_name, {})++ assert dispatched == []+++async def test_a_kept_tool_still_dispatches_on_the_required_surface() -> None:+ dispatched: list[str] = []+ server = _required_surface_server(_FakeClient({"ok": True}, on_call=lambda: dispatched.append("called")))++ result = await server._call_tool("get_workflow_knowledge", {})++ assert dispatched == ["called"]+ assert result.isError is False+++async def test_the_required_surface_advertises_only_its_own_catalog() -> None:+ required = _surface(browser_code_available=True)+ server = _required_surface_server(_FakeClient({"ok": True}))+ server._cached_raw_tools = [+ MCPTool(name=transport, description=name, inputSchema={"type": "object", "properties": {}})+ for name, transport in get_skyvern_mcp_alias_map().items()+ ]++ advertised = {tool.name for tool in await server.list_tools()}++ assert advertised == set(required.ordered_mcp_names)+++async def test_the_projected_inspection_reads_the_current_page_whatever_the_model_asks_for() -> None:+ source = next(tool for tool in NATIVE_TOOLS if tool.name == "inspect_page_for_composition")+ projected = current_page_inspection_tool(source)+ observed: list[Any] = []++ async def record(_ctx: object, arguments: str) -> str:+ observed.append(json.loads(arguments))+ return "{}"++ delegating = current_page_inspection_tool(dataclasses.replace(source, on_invoke_tool=record))++ await delegating.on_invoke_tool(SimpleNamespace(context=None), json.dumps({"target_url": "https://example.com/a"}))++ assert "target_url" not in projected.params_json_schema["properties"]+ assert "target_url" not in projected.params_json_schema["required"]+ assert "target_url" in source.params_json_schema["properties"]+ assert observed == [{"target_url": CURRENT_PAGE_INSPECTION_TARGET}]+ assert CURRENT_PAGE_INSPECTION_TARGET in _CURRENT_PAGE_INSPECTION_TARGETS+++async def test_the_projected_inspection_runs_under_the_runner_tool_context() -> None:+ """The runner forks a bare context for a wrapper that declares RunContextWrapper, and the real+ delegate cannot run on one; invoking through the runner's path proves the real body executed."""+ source = next(tool for tool in NATIVE_TOOLS if tool.name == "inspect_page_for_composition")+ projected = current_page_inspection_tool(source)+ arguments = json.dumps({"target_url": "https://example.com/a"})+ tool_ctx = ToolContext.from_agent_context(+ RunContextWrapper(context=make_copilot_ctx(browser_session_id="pbs_1")),+ tool_call_id="call_1",+ tool_name=projected.name,+ tool_arguments=arguments,+ )++ result = json.loads(await invoke_function_tool(function_tool=projected, context=tool_ctx, arguments=arguments))++ assert result["browser_target"] == "debug"+ assert "RunContextWrapper" not in str(result.get("error", ""))+++def test_a_lost_browser_session_reported_through_code_ends_the_loop_like_a_direct_tool() -> None:+ lost = {"ok": False, "error": "browser session pbs_1 not found"}+ code_error = {"ok": False, "error": "locator timed out at line 2"}++ assert _is_unrecoverable_browser_session_error("run_browser_code", lost)+ assert not _is_unrecoverable_browser_session_error("run_browser_code", code_error)diff --git a/tests/unit/forge/sdk/copilot/test_browser_code_tool.py b/tests/unit/forge/sdk/copilot/test_browser_code_tool.pynew file mode 100644index 000000000..9d881aa01--- /dev/null+++ b/tests/unit/forge/sdk/copilot/test_browser_code_tool.py@@ -0,0 +1,773 @@+from __future__ import annotations++import asyncio+import json+from collections.abc import AsyncIterator+from contextlib import asynccontextmanager+from dataclasses import replace+from types import SimpleNamespace+from typing import Any++import pytest+from playwright.async_api import Page++from skyvern.forge import app+from skyvern.forge.agent_functions import AgentFunction+from skyvern.forge.sdk.copilot.browser_ablation import (+ CopilotBrowserCodeMode,+ CopilotToolSurface,+ resolve_copilot_tool_surface,+)+from skyvern.forge.sdk.copilot.browser_code_contract import (+ BrowserCodeCellResult,+ BrowserCodeOperation,+ BrowserCodeSession,+ BrowserCodeSessionUnavailableError,+)+from skyvern.forge.sdk.copilot.runtime import CopilotBrowserGenerationRetired+from skyvern.forge.sdk.copilot.tools import _build_skyvern_mcp_overlays+from skyvern.forge.sdk.copilot.tools import browser_code as browser_code_module+from skyvern.forge.sdk.copilot.tools import copilot_native_tools, get_skyvern_mcp_alias_map+from skyvern.webeye.persistent_sessions_manager import BrowserRetirementReason+from tests.unit.conftest import make_copilot_context+++async def test_the_code_tool_mode_decides_whether_it_joins_or_replaces_the_direct_tools() -> None:+ aliases = get_skyvern_mcp_alias_map()+ overlays = _build_skyvern_mcp_overlays()+ oss_mode = await AgentFunction().copilot_browser_code_mode(organization_id="o", workflow_permanent_id="wp")++ def surface(mode: CopilotBrowserCodeMode) -> CopilotToolSurface:+ return resolve_copilot_tool_surface(+ mode=None,+ native_tools=copilot_native_tools(+ supports_question_tool=True, browser_code_available=mode != CopilotBrowserCodeMode.OFF+ ),+ alias_map=aliases,+ overlays=overlays,+ browser_code_mode=mode,+ )++ oss = surface(oss_mode)+ added = surface(CopilotBrowserCodeMode.ADD)+ replaced = surface(CopilotBrowserCodeMode.REPLACE)+ direct = {"click", "type_text", "evaluate", "navigate_browser"}++ assert oss_mode == CopilotBrowserCodeMode.OFF+ assert "run_browser_code" not in oss.ordered_native_names+ assert direct <= set(oss.ordered_mcp_names)+ # Added: the code tool sits beside every direct tool the surface already advertised.+ assert "run_browser_code" in added.ordered_native_names+ assert direct <= set(added.ordered_mcp_names)+ assert set(oss.ordered_native_names) | {"run_browser_code"} == set(added.ordered_native_names)+ # Replaced: the browser is reachable only through the code tool.+ assert "run_browser_code" in replaced.ordered_native_names+ assert direct.isdisjoint(replaced.ordered_mcp_names)+ assert {"fill_credential_field", "inspect_page_for_composition", "inspect_locator_matches"} <= set(+ replaced.ordered_native_names+ )+ assert len({oss.sha256, added.sha256, replaced.sha256}) == 3+++class _LeaseProbeSession:+ """Records whether the browser lease was still held when each cell ran."""++ def __init__(+ self,+ lease_held: list[bool] | None = None,+ retire_mid_cell: bool = False,+ *,+ denials_sink: list[bool] | None = None,+ ) -> None:+ self._lease_held = lease_held if lease_held is not None else []+ self._retire_mid_cell = retire_mid_cell+ self._denials_sink = denials_sink+ self.session_id = "bcs_probe"+ self.page = SimpleNamespace(url="https://example.com/")+ self.closed = False++ async def rebind(self, page: Page) -> None:+ return None++ async def run_cell(self, code: str, *, timeout_seconds: float, deny_pixels: bool = False) -> BrowserCodeCellResult:+ self._lease_held.append(bool(_LEASE_DEPTH))+ if self._denials_sink is not None:+ self._denials_sink.append(deny_pixels)+ if self._retire_mid_cell:+ raise asyncio.CancelledError()+ return BrowserCodeCellResult(+ ok=True,+ value=1,+ stdout=None,+ stdout_truncated=False,+ error_code=None,+ error=None,+ failing_line=None,+ operations=(),+ operations_omitted=0,+ session_alive=True,+ current_url="https://example.com/",+ )++ def last_dispatched_operation(self) -> BrowserCodeOperation | None:+ return None++ async def close(self) -> None:+ self.closed = True+++_LEASE_DEPTH: list[int] = []++[email protected]("retire_mid_cell", [False, True])+async def test_the_cell_runs_inside_the_browser_lease_and_a_retired_lease_reports_session_loss(+ monkeypatch: pytest.MonkeyPatch, retire_mid_cell: bool+) -> None:+ lease_held: list[bool] = []+ probe = _LeaseProbeSession(lease_held, retire_mid_cell=retire_mid_cell)++ @asynccontextmanager+ async def lease(_ctx: object, **_kwargs: object) -> AsyncIterator[None]:+ _LEASE_DEPTH.append(1)+ try:+ yield+ except asyncio.CancelledError:+ raise CopilotBrowserGenerationRetired("pbs_1", BrowserRetirementReason.session_ending) from None+ finally:+ _LEASE_DEPTH.pop()++ async def current_page(session_id: str | None = None) -> tuple[SimpleNamespace, None]:+ return SimpleNamespace(page=probe.page), None++ async def open_session(+ *,+ page: Page,+ lifetime_seconds: float,+ organization_id: str,+ chat_id: str,+ turn_id: str,+ browser_session_id: str | None,+ ) -> BrowserCodeSession:+ return probe++ async def prepared(*_args: object, **_kwargs: object) -> tuple[None, None, None]:+ return None, None, None++ async def loss_disposition(*_args: object, **_kwargs: object) -> str:+ return "failed"++ monkeypatch.setattr(browser_code_module, "mcp_browser_context", lease)+ monkeypatch.setattr(browser_code_module, "get_page", current_page)+ monkeypatch.setattr(browser_code_module, "_prepare_browser_session_for_dispatch", prepared)+ monkeypatch.setattr(browser_code_module, "_browser_session_error_disposition", loss_disposition)+ monkeypatch.setattr(app.AGENT_FUNCTION, "open_copilot_browser_code_session", open_session)+ ctx = make_copilot_context()+ ctx.browser_session_id = "pbs_1"++ result = await browser_code_module.run_browser_code(ctx, "1")++ assert lease_held == [True]+ if retire_mid_cell:+ assert result["ok"] is False+ assert probe.closed+ assert ctx.browser_code_host.session is None+ assert result["data"]["browser_session_continuity"]["disposition"] == "failed"+ else:+ assert result["ok"] is True+ assert result["value"] == 1++[email protected]("session_ended", [True, False], ids=["session over", "capacity refusal"])+async def test_a_refusal_that_says_the_session_is_over_drops_it_now(+ monkeypatch: pytest.MonkeyPatch, session_ended: bool+) -> None:+ """A typed refusal from a session the API has already marked dead is dropped on the spot. Kept, the+ retry the refusal asks for is spent rediscovering that, and only the call after reopens one. A+ refusal that leaves the session alive — a full runner — keeps it, so the retry lands on it."""++ class RefusingSession(_LeaseProbeSession):+ async def run_cell(+ self, code: str, *, timeout_seconds: float, deny_pixels: bool = False+ ) -> BrowserCodeCellResult:+ raise BrowserCodeSessionUnavailableError(+ "The code session's relay could not be reached." if session_ended else "The runner is full.",+ error_code="relay_unavailable" if session_ended else "busy",+ session_ended=session_ended,+ retry_after_seconds=None if session_ended else 5.0,+ )++ probe = RefusingSession()++ @asynccontextmanager+ async def lease(_ctx: object, **_kwargs: object) -> AsyncIterator[None]:+ yield++ async def current_page(session_id: str | None = None) -> tuple[SimpleNamespace, None]:+ return SimpleNamespace(page=probe.page), None++ async def open_session(**_kwargs: object) -> BrowserCodeSession:+ return probe++ async def prepared(*_args: object, **_kwargs: object) -> tuple[None, None, None]:+ return None, None, None++ monkeypatch.setattr(browser_code_module, "mcp_browser_context", lease)+ monkeypatch.setattr(browser_code_module, "get_page", current_page)+ monkeypatch.setattr(browser_code_module, "_prepare_browser_session_for_dispatch", prepared)+ monkeypatch.setattr(app.AGENT_FUNCTION, "open_copilot_browser_code_session", open_session)+ ctx = make_copilot_context()+ ctx.browser_session_id = "pbs_1"++ result = await browser_code_module.run_browser_code(ctx, "1")++ assert result["ok"] is False+ if session_ended:+ assert probe.closed+ assert ctx.browser_code_host.session is None+ else:+ assert not probe.closed+ assert ctx.browser_code_host.session is probe+ assert result["retry_after_seconds"] == 5.0+++async def test_a_session_that_ended_under_a_cell_still_tells_the_model_what_reached_the_browser(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """A session can end under a cell and return a result with no operations — a fallback written over+ one that had landed. The session knows what last reached the browser; told only that the session+ ended, the model retries a click or a submission that already happened."""+ click = BrowserCodeOperation(operation="click", selector="#submit", succeeded=None)++ class EndedSession(_LeaseProbeSession):+ async def run_cell(+ self, code: str, *, timeout_seconds: float, deny_pixels: bool = False+ ) -> BrowserCodeCellResult:+ return BrowserCodeCellResult(+ ok=False,+ value=None,+ stdout=None,+ stdout_truncated=False,+ error_code="session_lost",+ error="The code session failed.",+ failing_line=None,+ operations=(),+ operations_omitted=0,+ session_alive=False,+ current_url=None,+ )++ def last_dispatched_operation(self) -> BrowserCodeOperation | None:+ return click++ probe = EndedSession()++ @asynccontextmanager+ async def lease(_ctx: object, **_kwargs: object) -> AsyncIterator[None]:+ yield++ async def current_page(session_id: str | None = None) -> tuple[SimpleNamespace, None]:+ return SimpleNamespace(page=probe.page), None++ async def open_session(**_kwargs: object) -> BrowserCodeSession:+ return probe++ async def prepared(*_args: object, **_kwargs: object) -> tuple[None, None, None]:+ return None, None, None++ monkeypatch.setattr(browser_code_module, "mcp_browser_context", lease)+ monkeypatch.setattr(browser_code_module, "get_page", current_page)+ monkeypatch.setattr(browser_code_module, "_prepare_browser_session_for_dispatch", prepared)+ monkeypatch.setattr(app.AGENT_FUNCTION, "open_copilot_browser_code_session", open_session)+ ctx = make_copilot_context()+ ctx.browser_session_id = "pbs_1"++ result = await browser_code_module.run_browser_code(ctx, "1")++ assert result["ok"] is False+ evidence = result["session_ended_during_call"]+ assert evidence["last_operation"]["operation"] == "click"+ assert evidence["page_state"].startswith("unknown")+ assert probe.closed and ctx.browser_code_host.session is None+++async def test_a_replaced_browser_session_reopens_the_interpreter_rather_than_rebinding_it(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """The worker resolves tabs against the browser session it was started with.++ Rebinding only moves the interpreter to a tab, so a session pointed at a browser that has been+ replaced would keep resolving against the retired one. It has to be reopened instead.+ """+ rebinds: list[Page] = []+ opened: list[str | None] = []++ class _Session:+ def __init__(self) -> None:+ self.session_id = "bcs_probe"+ self.page = SimpleNamespace(url="https://example.com/")+ self.closed = False++ async def rebind(self, page: Page) -> None:+ rebinds.append(page)++ async def run_cell(+ self, code: str, *, timeout_seconds: float, deny_pixels: bool = False+ ) -> BrowserCodeCellResult:+ return BrowserCodeCellResult(+ ok=True,+ value=1,+ stdout=None,+ stdout_truncated=False,+ error_code=None,+ error=None,+ failing_line=None,+ operations=(),+ operations_omitted=0,+ session_alive=True,+ current_url="https://example.com/",+ )++ def last_dispatched_operation(self) -> BrowserCodeOperation | None:+ return None++ async def close(self) -> None:+ self.closed = True++ sessions: list[_Session] = []++ @asynccontextmanager+ async def lease(_ctx: object, **_kwargs: object) -> AsyncIterator[None]:+ yield++ async def current_page(session_id: str | None = None) -> tuple[SimpleNamespace, None]:+ return SimpleNamespace(+ page=sessions[-1].page if sessions else SimpleNamespace(url="https://example.com/")+ ), None++ async def open_session(+ *,+ page: Page,+ lifetime_seconds: float,+ organization_id: str,+ chat_id: str,+ turn_id: str,+ browser_session_id: str | None,+ ) -> BrowserCodeSession:+ opened.append(browser_session_id)+ session = _Session()+ session.page = page+ sessions.append(session)+ return session++ async def prepared(*_args: object, **_kwargs: object) -> tuple[None, None, None]:+ return None, None, None++ monkeypatch.setattr(browser_code_module, "mcp_browser_context", lease)+ monkeypatch.setattr(browser_code_module, "get_page", current_page)+ monkeypatch.setattr(browser_code_module, "_prepare_browser_session_for_dispatch", prepared)+ monkeypatch.setattr(app.AGENT_FUNCTION, "open_copilot_browser_code_session", open_session)++ ctx = make_copilot_context()+ ctx.browser_session_id = "pbs_1"+ assert (await browser_code_module.run_browser_code(ctx, "1"))["ok"] is True++ # Continuity recovery hands the turn a different browser.+ ctx.browser_session_id = "pbs_2"+ second = await browser_code_module.run_browser_code(ctx, "2")++ assert second["ok"] is True+ assert opened == ["pbs_1", "pbs_2"], "the interpreter was not reopened against the new browser"+ assert rebinds == [], "a rebind cannot move the worker to a different browser"+ assert sessions[0].closed is True++[email protected]("selector", ["#pay", None])+async def test_interaction_credit_follows_what_the_workbench_can_actually_promote(selector: str | None) -> None:+ """A handle-backed locator leaves no selector, so nothing is recorded for it.++ Crediting the evidence that follows to an interaction anyway offers workflow authoring an action+ there is no saved form of.+ """+ from skyvern.forge.sdk.copilot.tools import browser_code as browser_code_module++ ctx = make_copilot_context()+ cell = BrowserCodeCellResult(+ ok=True,+ value=None,+ stdout=None,+ stdout_truncated=False,+ error_code=None,+ error=None,+ failing_line=None,+ operations=(),+ operations_omitted=0,+ actions=(BrowserCodeOperation(operation="click", selector=selector, succeeded=True),),+ session_alive=True,+ current_url="https://example.com/checkout",+ )++ browser_code_module._record_outcome(ctx, cell, {"ok": True}, browser_session_id="pbs_1", generation=0)++ armed = ctx.pending_browser_interaction_observation is not None+ assert armed is (selector is not None), "interaction credit did not follow what was recorded"++[email protected]("tainted", [True, False])+async def test_a_page_used_for_a_sensitive_sign_in_denies_the_cell_its_pixels(+ monkeypatch: pytest.MonkeyPatch, tainted: bool+) -> None:+ """Facts stop being withheld once the secret registry is complete, but the page stays tainted.++ Result scrubbing compares values and an image is not a value, so a cell that can photograph the+ page can carry it out a chunk at a time. The refusal is decided here, where the taint is known.+ """+ denials: list[bool] = []+ probe = _LeaseProbeSession(denials_sink=denials)++ @asynccontextmanager+ async def lease(_ctx: object, **_kwargs: object) -> AsyncIterator[None]:+ yield++ async def current_page(session_id: str | None = None) -> tuple[SimpleNamespace, None]:+ return SimpleNamespace(page=probe.page), None++ async def open_session(**_kwargs: object) -> BrowserCodeSession:+ return probe++ async def prepared(*_args: object, **_kwargs: object) -> tuple[None, None, None]:+ return None, None, None++ monkeypatch.setattr(browser_code_module, "mcp_browser_context", lease)+ monkeypatch.setattr(browser_code_module, "get_page", current_page)+ monkeypatch.setattr(browser_code_module, "_prepare_browser_session_for_dispatch", prepared)+ monkeypatch.setattr(app.AGENT_FUNCTION, "open_copilot_browser_code_session", open_session)+ # The state the gap lives in: the run has finished and its values are bound, so facts are no longer+ # withheld. Stubbed because reaching it otherwise means rebuilding the whole secret registry; the+ # taint below is the real thing, and it is what this test is about.+ monkeypatch.setattr(browser_code_module, "sensitive_origin_page_facts_withheld", lambda *_a, **_k: False)++ ctx = make_copilot_context()+ ctx.browser_session_id = "pbs_1"+ # The real taint, not a stand-in for it: this is what a sensitive-origin run leaves behind.+ ctx.sensitive_origin_browser_session_ids = {"pbs_1"} if tainted else set()++ result = await browser_code_module.run_browser_code(ctx, "1")++ assert result["ok"] is True+ assert denials == [tainted]++[email protected](("result_url", "lifted"), [("https://x.test/", True), ("https://portal.test/a#top", False)])+async def test_a_readable_tainted_page_regains_pixels_only_after_a_recorded_navigation_off_it(+ monkeypatch: pytest.MonkeyPatch, result_url: str, lifted: bool+) -> None:+ """With the navigate tool withdrawn, a cell's own navigation is the only way off a tainted page+ whose facts are readable again; a fragment hop leaves the sensitive document up."""+ from skyvern.forge.sdk.copilot.runtime import sensitive_origin_page_is_tainted++ denials: list[bool] = []++ class _NavigatingSession(_LeaseProbeSession):+ async def run_cell(+ self, code: str, *, timeout_seconds: float, deny_pixels: bool = False+ ) -> BrowserCodeCellResult:+ cell = await super().run_cell(code, timeout_seconds=timeout_seconds, deny_pixels=deny_pixels)+ op = BrowserCodeOperation(+ operation="goto",+ selector=None,+ succeeded=True,+ source_url="https://portal.test/a",+ result_url=result_url,+ )+ return replace(cell, operations=(op,), current_url=result_url)++ probe = _NavigatingSession(denials_sink=denials)++ @asynccontextmanager+ async def lease(_ctx: object, **_kwargs: object) -> AsyncIterator[None]:+ yield++ async def current_page(session_id: str | None = None) -> tuple[SimpleNamespace, None]:+ return SimpleNamespace(page=probe.page), None++ async def open_session(**_kwargs: object) -> BrowserCodeSession:+ return probe++ async def prepared(*_args: object, **_kwargs: object) -> tuple[None, None, None]:+ return None, None, None++ monkeypatch.setattr(browser_code_module, "mcp_browser_context", lease)+ monkeypatch.setattr(browser_code_module, "get_page", current_page)+ monkeypatch.setattr(browser_code_module, "_prepare_browser_session_for_dispatch", prepared)+ monkeypatch.setattr(app.AGENT_FUNCTION, "open_copilot_browser_code_session", open_session)+ monkeypatch.setattr(browser_code_module, "sensitive_origin_page_facts_withheld", lambda *_a, **_k: False)+ ctx = make_copilot_context()+ ctx.browser_session_id = "pbs_1"+ ctx.sensitive_origin_browser_session_ids = {"pbs_1"}++ await browser_code_module.run_browser_code(ctx, f'await page.goto("{result_url}")')+ await browser_code_module.run_browser_code(ctx, "1")++ assert denials == [True, not lifted]+ assert sensitive_origin_page_is_tainted(ctx) is not lifted++[email protected]("restarted", [True, False])+async def test_an_interpreter_restarted_on_the_worker_is_reported_to_the_model(+ monkeypatch: pytest.MonkeyPatch, restarted: bool+) -> None:+ """This side's handle is unchanged when the far side reopens, so silence reads as continuity.++ The promise is that values survive between calls, and a call that cannot keep it has to say so+ rather than let the model discover it by a NameError.+ """++ class RestartingSession(_LeaseProbeSession):+ async def run_cell(self, code: str, *, timeout_seconds: float, deny_pixels: bool = False) -> Any:+ cell = await super().run_cell(code, timeout_seconds=timeout_seconds, deny_pixels=deny_pixels)+ return replace(cell, interpreter_restarted=restarted)++ probe = RestartingSession()++ @asynccontextmanager+ async def lease(_ctx: object, **_kwargs: object) -> AsyncIterator[None]:+ yield++ async def current_page(session_id: str | None = None) -> tuple[SimpleNamespace, None]:+ return SimpleNamespace(page=probe.page), None++ async def open_session(**_kwargs: object) -> BrowserCodeSession:+ return probe++ async def prepared(*_args: object, **_kwargs: object) -> tuple[None, None, None]:+ return None, None, None++ monkeypatch.setattr(browser_code_module, "mcp_browser_context", lease)+ monkeypatch.setattr(browser_code_module, "get_page", current_page)+ monkeypatch.setattr(browser_code_module, "_prepare_browser_session_for_dispatch", prepared)+ monkeypatch.setattr(app.AGENT_FUNCTION, "open_copilot_browser_code_session", open_session)++ ctx = make_copilot_context()+ ctx.browser_session_id = "pbs_1"++ result = await browser_code_module.run_browser_code(ctx, "1")++ assert result["ok"] is True+ assert ("session_restarted" in result) is restarted, "the model was not told its earlier values are gone"+++async def test_a_cell_targeted_at_the_last_run_acts_in_that_runs_browser(monkeypatch: pytest.MonkeyPatch) -> None:+ """A test run that minted its own browser leaves the page the model just observed there; the+ replace surface has no other action route to it, so the cell must follow the same target the+ read tools honour rather than the chat's browser."""+ probe = _LeaseProbeSession([], retire_mid_cell=False)+ looked_up: list[str | None] = []+ opened_for: list[str | None] = []+ leased: list[object] = []++ @asynccontextmanager+ async def lease(_ctx: object, **kwargs: object) -> AsyncIterator[None]:+ leased.append(kwargs.get("session_id_override"))+ yield++ async def current_page(session_id: str | None = None) -> tuple[SimpleNamespace, None]:+ looked_up.append(session_id)+ return SimpleNamespace(page=probe.page), None++ async def open_session(*, browser_session_id: str | None, **_kwargs: object) -> BrowserCodeSession:+ opened_for.append(browser_session_id)+ return probe++ async def prepared(*_args: object, **_kwargs: object) -> tuple[None, None, None]:+ return None, None, None++ monkeypatch.setattr(browser_code_module, "mcp_browser_context", lease)+ monkeypatch.setattr(browser_code_module, "get_page", current_page)+ monkeypatch.setattr(browser_code_module, "_prepare_browser_session_for_dispatch", prepared)+ monkeypatch.setattr(app.AGENT_FUNCTION, "open_copilot_browser_code_session", open_session)+ ctx = make_copilot_context()+ ctx.browser_session_id = "pbs_chat"++ unavailable = await browser_code_module.run_browser_code(ctx, "1", target="last_run")++ ctx.last_run_blocks_workflow_run_id = "wr_1"+ ctx.last_run_blocks_browser_session_id = "pbs_run"+ on_run = await browser_code_module.run_browser_code(ctx, "1", target="last_run")+ on_chat = await browser_code_module.run_browser_code(ctx, "1")++ assert unavailable["ok"] is False+ assert "No test run has recorded a browser session" in unavailable["error"]+ assert on_run["ok"] is True and on_run["browser_target"] == "last_run"+ assert on_chat["ok"] is True and on_chat["browser_target"] == "debug"+ assert looked_up == ["pbs_run", "pbs_chat"]+ assert opened_for == ["pbs_run", "pbs_chat"]+ # The lease admits the same browser the cell drives; a ContextVar alone does not reach it.+ assert leased == ["pbs_run", None]+++async def test_a_withheld_page_lifts_only_on_a_recorded_navigation_to_another_document(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """The replace surface withdraws every other route off a tainted page, so the code tool offers the+ recovery the error names, keyed on the worker's record of the navigation rather than on the cell's+ text: a rebound `page.goto` runs no operation, and a fragment hop leaves the sensitive document up."""+ from skyvern.forge.sdk.copilot.runtime import (+ record_sensitive_origin_run_taint,+ register_sensitive_origin_run_lease,+ release_sensitive_origin_run_lease,+ sensitive_origin_page_facts_withheld,+ )++ tainted_url = "https://portal.test/account?tab=billing"+ scripted: list[tuple[BrowserCodeOperation, ...]] = []+ denials: list[bool] = []+ sessions_discarded: list[bool] = []++ def goto(result_url: str) -> BrowserCodeOperation:+ return BrowserCodeOperation(+ operation="goto", selector=None, succeeded=True, source_url=tainted_url, result_url=result_url+ )++ class _ScriptedSession(_LeaseProbeSession):+ async def run_cell(+ self, code: str, *, timeout_seconds: float, deny_pixels: bool = False+ ) -> BrowserCodeCellResult:+ denials.append(deny_pixels)+ operations = scripted.pop(0)+ current_url = operations[-1].result_url if operations else tainted_url+ return BrowserCodeCellResult(+ ok=True,+ value="<response>",+ stdout="leaked?",+ stdout_truncated=False,+ error_code=None,+ error=None,+ failing_line=None,+ operations=operations,+ operations_omitted=0,+ session_alive=True,+ current_url=current_url,+ )++ probe = _ScriptedSession()++ @asynccontextmanager+ async def lease(_ctx: object, **_kwargs: object) -> AsyncIterator[None]:+ yield++ async def current_page(session_id: str | None = None) -> tuple[SimpleNamespace, None]:+ return SimpleNamespace(page=probe.page), None++ opened: list[bool] = []++ async def open_session(**_kwargs: object) -> BrowserCodeSession:+ opened.append(True)+ return probe++ async def prepared(*_args: object, **_kwargs: object) -> tuple[None, None, None]:+ return None, None, None++ monkeypatch.setattr(browser_code_module, "mcp_browser_context", lease)+ monkeypatch.setattr(browser_code_module, "get_page", current_page)+ monkeypatch.setattr(browser_code_module, "_prepare_browser_session_for_dispatch", prepared)+ monkeypatch.setattr(app.AGENT_FUNCTION, "open_copilot_browser_code_session", open_session)+ ctx = make_copilot_context()+ ctx.browser_session_id = "pbs_1"+ # An interpreter from before the page turned sensitive, whose namespace a recovery must not inherit.+ ctx.browser_code_host.session = probe+ ctx.browser_code_host.browser_session_id = "pbs_1"+ record_sensitive_origin_run_taint(ctx, workflow_run_id="wr_secret", session_id="pbs_1")+ assert sensitive_origin_page_facts_withheld(ctx, None)+ bare = 'await page.goto("https://x.test/")'++ mixed = await browser_code_module.run_browser_code(ctx, f"print(await page.title())\n{bare}")+ register_sensitive_origin_run_lease(ctx, workflow_run_id="wr_secret", session_id="pbs_1")+ while_active = await browser_code_module.run_browser_code(ctx, bare)+ release_sensitive_origin_run_lease(ctx, workflow_run_id="wr_secret")+ scripted.append(())+ no_operation = await browser_code_module.run_browser_code(ctx, bare)+ still_withheld_after_no_operation = sensitive_origin_page_facts_withheld(ctx, None)+ sessions_discarded.append(ctx.browser_code_host.session is None)+ scripted.append((goto(tainted_url + "#top"),))+ fragment = await browser_code_module.run_browser_code(ctx, bare)+ still_withheld_after_fragment = sensitive_origin_page_facts_withheld(ctx, None)+ sessions_discarded.append(ctx.browser_code_host.session is None)+ scripted.append((goto("https://x.test/"),))+ recovered = await browser_code_module.run_browser_code(ctx, bare)++ assert mixed["ok"] is False and 'await page.goto("<url>")' in mixed["error"]+ assert while_active["ok"] is False and "while a run with sensitive inputs is active" in while_active["error"]+ assert denials == [True, True, True], "every recovery attempt runs with pixels denied"+ for refused in (no_operation, fragment):+ assert refused["ok"] is False+ assert "current_url" not in refused and "value" not in refused and "stdout" not in refused+ assert tainted_url not in json.dumps(refused)+ assert "session_ended" in refused+ assert "session_ended" in recovered+ # Every recovery ran in a fresh interpreter, never the one that predates the taint.+ assert len(opened) == 3 and sessions_discarded == [False, False]+ assert still_withheld_after_no_operation and still_withheld_after_fragment+ assert recovered["ok"] is True and recovered["current_url"] == "https://x.test/"+ assert "value" not in recovered and "stdout" not in recovered+ assert not sensitive_origin_page_facts_withheld(ctx, None)+++async def test_a_debug_reestablishment_does_not_rebind_a_cell_pinned_to_the_last_run_browser(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Only the chat's own browser moves the continuity generation, so a last-run cell keeps its+ interpreter and the handles it saved across cells; a debug cell still rebinds on the same bump."""+ rebinds: list[str] = []++ class _RebindCounting(_LeaseProbeSession):+ async def rebind(self, page: Page) -> None:+ rebinds.append(ctx.browser_code_host.browser_session_id or "")++ probe = _RebindCounting()++ @asynccontextmanager+ async def lease(_ctx: object, **_kwargs: object) -> AsyncIterator[None]:+ yield++ async def current_page(session_id: str | None = None) -> tuple[SimpleNamespace, None]:+ return SimpleNamespace(page=probe.page), None++ async def open_session(**_kwargs: object) -> BrowserCodeSession:+ return probe++ async def prepared(*_args: object, **_kwargs: object) -> tuple[None, None, None]:+ return None, None, None++ monkeypatch.setattr(browser_code_module, "mcp_browser_context", lease)+ monkeypatch.setattr(browser_code_module, "get_page", current_page)+ monkeypatch.setattr(browser_code_module, "_prepare_browser_session_for_dispatch", prepared)+ monkeypatch.setattr(app.AGENT_FUNCTION, "open_copilot_browser_code_session", open_session)+ ctx = make_copilot_context()+ ctx.browser_session_id = "pbs_chat"+ ctx.last_run_blocks_workflow_run_id = "wr_1"+ ctx.last_run_blocks_browser_session_id = "pbs_run"++ await browser_code_module.run_browser_code(ctx, "1", target="last_run")+ ctx.browser_session_continuity_generation += 1+ pinned = await browser_code_module.run_browser_code(ctx, "1", target="last_run")++ assert pinned["ok"] is True and "handles_invalidated" not in pinned+ assert rebinds == []++ await browser_code_module.run_browser_code(ctx, "1")+ ctx.browser_session_continuity_generation += 1+ debug = await browser_code_module.run_browser_code(ctx, "1")++ assert "handles_invalidated" in debug and rebinds == ["pbs_chat"]diff --git a/tests/unit/test_agent_task_v3.py b/tests/unit/test_agent_task_v3.pyindex ed5ecaccf..d25d58f56 100644--- a/tests/unit/test_agent_task_v3.py+++ b/tests/unit/test_agent_task_v3.py@@ -50,9 +50,16 @@ from skyvern.forge.sdk.workflow.models.block import ()from skyvern.forge.sdk.workflow.models.parameter import CredentialParameter, OutputParameter, ParameterTypefrom skyvern.forge.sdk.workflow.models.workflow import WorkflowRunStatus-from skyvern.forge.taskv3.engine import MIN_ACTION_STEPS+from skyvern.forge.taskv3.engine import DEFAULT_MAX_SETTLE_DEFERRALS, MIN_ACTION_STEPSfrom skyvern.forge.taskv3.frame_perception import FRAME_PERCEPTION_FLAG, frame_perception_enabled-from skyvern.forge.taskv3.loop import LoopOutcome, RoundAction+from skyvern.forge.taskv3.handoff_redaction import pin_caller_authored_block_urls+from skyvern.forge.taskv3.loop import (+ ACTION_LOOP_GUARD,+ NAV_DEAD_END_GUARD,+ LoopOutcome,+ RoundAction,+ _dead_end_reason,+)from skyvern.forge.taskv3.run_arms import (OBSERVE_DROP_OFFVIEWPORT_UNNAMED_FLAG,TYPE_COORDINATE_CLICK_FLAG,@@ -94,6 +101,13 @@ async def _run_execute_task_v3(context_overrides: dict[str, Any] | None = None,own_block_row: WorkflowRunBlock | None = None,own_block_lookup_raises: BaseException | None = None,+ workflow_owned_recovery: bool = False,+ landed_url: str | None = None,+ navigation_status: int | None = None,+ page_url_after_settle: str | None = None,+ # The block's own (url, navigation_goal) as the AUTHOR typed them, pinned through the real block+ # seam before the render that produced the task fields above.+ unrendered_block_fields: tuple[str | None, str | None] | None = None,**task_overrides: Any,) -> tuple[Step, Any, AsyncMock, AsyncMock]:agent = ForgeAgent()@@ -103,6 +117,13 @@ async def _run_execute_task_v3(step = make_step(now, task, step_id="step-v3", status=StepStatus.created, order=0, output=None)browser_state, _, page = make_browser_state()+ # What setup's navigation RECORDED: the response's own URL beside the status it came back with,+ # which after a redirect is not the URL it asked for.+ browser_state.last_navigation_status = navigation_status+ browser_state.last_navigation_url = landed_url if landed_url is not None else task.url+ # Where the page is by the time the loop starts -- a client-side redirect during the settle or+ # challenge-solver wait moves it off the URL the status belongs to.+ page.url = page_url_after_settle if page_url_after_settle is not None else browser_state.last_navigation_urlbrowser_state.must_get_working_page = AsyncMock(return_value=page, side_effect=must_get_working_page_side_effect)if get_working_page_side_effect is not None:browser_state.get_working_page = AsyncMock(side_effect=get_working_page_side_effect)@@ -194,6 +215,15 @@ async def _run_execute_task_v3()for name, value in (context_overrides or {}).items():setattr(context, name, value)+ if unrendered_block_fields is not None:+ assert task_block is not None+ pin_caller_authored_block_urls(+ context,+ workflow_run_id=task.workflow_run_id or "",+ block_label=task_block.label,+ url=unrendered_block_fields[0],+ navigation_goal=unrendered_block_fields[1],+ )skyvern_context.set(context)try:out_step, out_task = await agent._execute_task_v3(@@ -205,6 +235,7 @@ async def _run_execute_task_v3(close_browser_on_completion=True,browser_session_id=None,task_block=task_block,+ workflow_owned_recovery=workflow_owned_recovery,)finally:skyvern_context.reset()@@ -1075,7 +1106,10 @@ async def test_execute_task_v3_persists_a_dead_end_navigation_as_a_failed_row(}outcome = LoopOutcome(status="terminated",- reason="navigation_dead_end: navigate landed on a dead page (HTTP 404)",+ reason=_dead_end_reason(+ 404, dead_end["url"], page_noun="The page the run opened", caller_known_urls=frozenset()+ ),+ guard=NAV_DEAD_END_GUARD,billable_actions=[],)rounds = [@@ -2663,6 +2697,179 @@ async def test_execute_task_v3_bare_task_provider_resolves_page_once(monkeypatchassert loop_mock.browser_state.get_working_page.await_count <= 1+async def test_a_workflow_owned_recovery_keeps_its_caller_s_step_cap(monkeypatch: pytest.MonkeyPatch) -> None:+ # The code block AI fallback sizes its own budget from the org's cap, so the v3 floor must not+ # raise it — an org capped at 4 rounds cannot get 24 page-mutating ones.+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=["click"])+ _step, _task, loop_mock, _post = await _run_execute_task_v3(+ monkeypatch,+ outcome,+ workflow_owned_recovery=True,+ max_steps_per_run=4,+ data_extraction_goal=None,+ extracted_information_schema=None,+ )+ assert loop_mock.await_args.kwargs["max_action_steps"] == 4+ assert loop_mock.await_args.kwargs["max_action_steps_ceiling"] == 4+++async def test_a_workflow_owned_recovery_spends_from_the_workflow_run_pool(monkeypatch: pytest.MonkeyPatch) -> None:+ # Recovery is part of the workflow run, so what the run has already spent bounds it: 3 rounds+ # remain of the org's pool and the caller's own cap of 9 cannot overdraw it.+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=["click"])+ monkeypatch.setattr(ForgeAgent, "_check_workflow_run_step_budget", AsyncMock(return_value=(18, 20)))+ _step, _task, loop_mock, _post = await _run_execute_task_v3(+ monkeypatch,+ outcome,+ workflow_owned_recovery=True,+ workflow_run_id="wr_recovery_pool",+ max_steps_per_run=9,+ data_extraction_goal=None,+ extracted_information_schema=None,+ )+ assert loop_mock.await_args.kwargs["max_action_steps"] == 3+ assert loop_mock.await_args.kwargs["max_action_steps_ceiling"] == 3+++async def test_a_workflow_owned_recovery_reaches_child_frames(monkeypatch: pytest.MonkeyPatch) -> None:+ # The v1 run it replaces scraped child frames unconditionally, and the failed call may target+ # one, so the arm is on for this run whatever the flag samples.+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=["click"])+ _step, _task, loop_mock, _post = await _run_execute_task_v3(+ monkeypatch,+ outcome,+ workflow_owned_recovery=True,+ data_extraction_goal=None,+ extracted_information_schema=None,+ )+ assert loop_mock.frame_perception_enabled_during_loop is True+++async def test_a_workflow_owned_recovery_hands_the_frame_arm_back(monkeypatch: pytest.MonkeyPatch) -> None:+ # The context outlives the recovery, so a later block of the same run must resolve its own arm+ # instead of inheriting the pin.+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=["click"])+ _step, _task, loop_mock, _post = await _run_execute_task_v3(+ monkeypatch,+ outcome,+ workflow_owned_recovery=True,+ data_extraction_goal=None,+ extracted_information_schema=None,+ )+ assert loop_mock.frame_perception_enabled_during_loop is True+ context = loop_mock.context+ assert (context.frame_perception_flag, context.frame_perception_resolved_run_id) == (False, None)+++async def test_a_workflow_owned_recovery_hands_the_frame_arm_back_when_the_pool_is_spent(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # The exhausted-pool exit returns before the loop runs, so it restores the arm on its own way+ # out; otherwise the run's later work inherits recovery-only state.+ seen: list[SkyvernContext] = []+ real_set = skyvern_context.set++ def _capture(context: SkyvernContext) -> None:+ seen.append(context)+ real_set(context)++ monkeypatch.setattr(skyvern_context, "set", _capture)+ monkeypatch.setattr(ForgeAgent, "_check_workflow_run_step_budget", AsyncMock(return_value=(21, 20)))+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=["click"])+ _step, _task, loop_mock, _post = await _run_execute_task_v3(+ monkeypatch,+ outcome,+ workflow_owned_recovery=True,+ workflow_run_id="wr_spent_pool",+ data_extraction_goal=None,+ extracted_information_schema=None,+ )+ loop_mock.assert_not_awaited()+ assert (seen[0].frame_perception_flag, seen[0].frame_perception_resolved_run_id) == (False, None)+++async def test_a_workflow_owned_recovery_hands_the_frame_arm_back_when_the_run_blows_up(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # Anything between the pin and the outcome can raise, and the arm must not survive the failure.+ seen: list[SkyvernContext] = []+ real_set = skyvern_context.set++ def _capture(context: SkyvernContext) -> None:+ seen.append(context)+ real_set(context)++ monkeypatch.setattr(skyvern_context, "set", _capture)+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=["click"])+ with pytest.raises(RuntimeError):+ await _run_execute_task_v3(+ monkeypatch,+ outcome,+ workflow_owned_recovery=True,+ loop_raises=RuntimeError("loop blew up"),+ data_extraction_goal=None,+ extracted_information_schema=None,+ )+ assert (seen[0].frame_perception_flag, seen[0].frame_perception_resolved_run_id) == (False, None)+++async def test_a_workflow_owned_recovery_ignores_an_earlier_blocks_navigation_status(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # The browser state is reused across blocks, so a 404 it recorded earlier is not this recovery's+ # starting page and would end the loop before its first model call.+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=["click"])+ _step, _task, loop_mock, _post = await _run_execute_task_v3(+ monkeypatch,+ outcome,+ workflow_owned_recovery=True,+ navigation_status=404,+ data_extraction_goal=None,+ extracted_information_schema=None,+ )+ assert loop_mock.await_args.kwargs["initial_navigation_status"] is None+++async def test_a_workflow_owned_recovery_waits_for_the_page_to_settle(monkeypatch: pytest.MonkeyPatch) -> None:+ # Recovery clicks controls that start async transitions, so it gets the workflow-owned settle+ # deferrals rather than the bare arm's immediate verdict.+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=["click"])+ _step, _task, loop_mock, _post = await _run_execute_task_v3(+ monkeypatch,+ outcome,+ workflow_owned_recovery=True,+ data_extraction_goal=None,+ extracted_information_schema=None,+ )+ assert loop_mock.await_args.kwargs["max_settle_deferrals"] == DEFAULT_MAX_SETTLE_DEFERRALS+++async def test_a_workflow_owned_recovery_follows_the_live_working_page(monkeypatch: pytest.MonkeyPatch) -> None:+ # Recovery can continue in a popup the failing code opened, so each tool call re-resolves the+ # working page instead of holding the page the run started on.+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=[])+ probe, page_a, page_b, page_c = MagicMock(), MagicMock(), MagicMock(), MagicMock()+ _step, _task, loop_mock, _post = await _run_execute_task_v3(+ monkeypatch,+ outcome,+ workflow_owned_recovery=True,+ provider_probe_calls=3,+ must_get_working_page_side_effect=[probe, page_a, page_b, page_c],+ data_extraction_goal=None,+ extracted_information_schema=None,+ )+ assert loop_mock.resolved_pages == [page_a, page_b, page_c]++@pytest.mark.asyncioasync def test_execute_task_v3_workflow_provider_resolves_live_working_page(monkeypatch: pytest.MonkeyPatch,@@ -4021,11 +4228,13 @@ async def test_execute_task_v3_cap_tripped_finish_with_unclassifiable_reason_falasync def test_execute_task_v3_cap_tripped_guard_termination_is_not_budget_categorized(monkeypatch: pytest.MonkeyPatch,) -> None:- # A repeat-guard termination on the granted turn terminated for the guard's reason (its typed- # prefix rides failure_reason); stamping BUDGET_EXHAUSTED over it would mix the label axes.+ # A repeat-guard termination on the granted turn terminated for the guard's reason (which rides+ # `guard`, beside the customer-facing sentence in failure_reason); stamping BUDGET_EXHAUSTED over+ # it would mix the label axes.outcome = LoopOutcome(status="terminated",- reason="action_loop: repeated identical click on the same target",+ reason='The run repeated the same action on the "Continue" button and the page did not change.',+ guard=ACTION_LOOP_GUARD,cap_trip="max_turns (40) reached",billable_actions=["click"],extracted_output=None,@@ -5094,3 +5303,148 @@ async def test_execute_task_v3_persists_reload_row_with_its_own_reason(monkeypatassert agent_mod.app.DATABASE.workflow_params.create_action.await_count == 2persisted = agent_mod.app.DATABASE.workflow_params.create_action.await_args_list[0].kwargs["action"]assert persisted.reasoning == "a page-level handler requested a refresh"+++async def test_execute_task_v3_hands_a_bare_tasks_landed_url_to_the_engine(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # The dead-end verdict names the starting page to the customer (SKY-16271), which it can only do if a+ # URL travels beside the status that classifies it. That status is the LANDED response's, so a+ # redirected start must hand over where the browser ended -- naming the requested URL would make the+ # sentence a false statement about it. And a page that moves AFTER the response (a client-side+ # redirect during the settle or challenge-solver wait) must not be the page the sentence names: the+ # status was never about that one.+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=[])+ landed = "https://jobs.example.test/acme/expired"+ redirected_to = "https://jobs.example.test/acme/careers"+ _step, task, bare_loop_mock, _post = await _run_execute_task_v3(+ monkeypatch,+ outcome,+ landed_url=landed,+ navigation_status=404,+ page_url_after_settle=redirected_to,+ )+ assert task.url != landed+ # The pair, not one of each: the sentence the loop builds from it is about the page that returned+ # the status (test_initial_navigation_dead_end_verdict_names_the_starting_page pins that half).+ kwargs = bare_loop_mock.await_args.kwargs+ assert kwargs["initial_navigation_status"] == 404+ assert kwargs["initial_navigation_url"] == landed+ assert kwargs["initial_navigation_url"] != redirected_to++ # A workflow block reuses the browser across blocks, so neither the status nor a URL belongs to this+ # block's start.+ _step, _task, block_loop_mock, _post = await _run_execute_task_v3(+ monkeypatch,+ outcome,+ task_block=_make_block(TaskBlock, label="shipping"),+ data_extraction_goal=None,+ extracted_information_schema=None,+ )+ assert block_loop_mock.await_args.kwargs["initial_navigation_url"] is None+++async def test_execute_task_v3_hands_the_loop_only_the_callers_own_urls(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # A guard verdict publishes a landed URL's path only when the caller already had that URL+ # (SKY-16271), so who counts as the caller is decided here: the task's own URL and the URL literals+ # in the task's own navigation goal. The COMPOSED goal the loop is prompted with is deliberately not+ # the source — it also carries a prior block's handoff prose, whose URLs came off a page.+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=[])+ elsewhere = "https://cdn.example.test/terms.pdf"+ _step, _task, loop_mock, _post = await _run_execute_task_v3(+ monkeypatch,+ outcome,+ url="https://jobs.example.test/acme/apply?ref=email",+ navigation_goal="Read https://jobs.example.test/acme/faq, then apply.",+ data_extraction_goal=f"Extract what {elsewhere} says",+ )++ kwargs = loop_mock.await_args.kwargs+ # The task URL is normalized the way the verdict normalizes a landed URL, so the two can be+ # compared: the query it arrived with is not part of what may be published.+ assert kwargs["caller_known_urls"] == frozenset(+ {"https://jobs.example.test/acme/apply", "https://jobs.example.test/acme/faq"}+ )+ # The composed prompt carries a URL from elsewhere in the task config; the set is narrower than+ # the prompt on purpose, because the prompt is also where page-derived prose arrives.+ assert elsewhere in kwargs["goal"] # nosemgrep: incomplete-url-substring-sanitization+++async def test_execute_task_v3_reads_a_blocks_caller_urls_from_its_unrendered_definition(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # Inside a workflow run the task's own url/navigation_goal are no longer the caller's text: both are+ # Jinja templates the block renders against a context holding prior blocks' recorded output, so a+ # reset link an earlier block read off a page arrives on the task looking configured. The caller is+ # the AUTHOR, so the sources are the block's definition strings as typed (SKY-16271).+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=[])+ off_the_page = "https://mail.example.test/reset/9f2c8a1b4d6e"+ _step, _task, loop_mock, _post = await _run_execute_task_v3(+ monkeypatch,+ outcome,+ task_block=_make_block(NavigationBlock, navigation_goal="rendered"),+ workflow_run_id="wr_templated_url",+ url=off_the_page,+ navigation_goal=f"Open {off_the_page} and finish.",+ unrendered_block_fields=(+ "https://portal.example.test/start",+ "Open {{ mail_output }}, then check https://portal.example.test/faq.",+ ),+ data_extraction_goal=None,+ extracted_information_schema=None,+ )++ kwargs = loop_mock.await_args.kwargs+ # Typed by the author, so publishable; the one the template produced is not in the set even though+ # it is sitting right there on the task the loop was handed.+ assert kwargs["caller_known_urls"] == frozenset(+ {"https://portal.example.test/start", "https://portal.example.test/faq"}+ )+ assert off_the_page in kwargs["goal"] # nosemgrep: incomplete-url-substring-sanitization+++async def test_execute_task_v3_publishes_no_path_for_a_block_that_never_reached_the_pin(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # Fail-closed by construction: a workflow-run task whose block seam did not run -- a reconstructed+ # block, another entry point -- gets the empty set and a host-only verdict, never a fallback to the+ # rendered task fields.+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=[])+ _step, _task, loop_mock, _post = await _run_execute_task_v3(+ monkeypatch,+ outcome,+ task_block=_make_block(NavigationBlock, navigation_goal="rendered"),+ workflow_run_id="wr_no_pin",+ url="https://mail.example.test/reset/9f2c8a1b4d6e",+ data_extraction_goal=None,+ extracted_information_schema=None,+ )++ assert loop_mock.await_args.kwargs["caller_known_urls"] == frozenset()+++async def test_execute_task_v3_hands_the_loop_the_drop_check_secrets_the_timeline_label_uses(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # A guard verdict names a page-supplied control by the page's own accessible name, so it has to be+ # checked against the same registry the persisted label is -- unfloored and blind to the mask-secrets+ # opt-in -- or a credential-shaped label is dropped from the timeline row and published verbatim on+ # the webhook (SKY-16271). The loop cannot see that registry, so it arrives as a resolver.+ manager = "skyvern.forge.agent.app.WORKFLOW_CONTEXT_MANAGER"+ monkeypatch.setattr(f"{manager}.artifact_redaction_enabled", lambda *_a, **_k: False)+ monkeypatch.setattr(f"{manager}.get_secret_values_for_run", lambda *_a, **_k: set())+ monkeypatch.setattr(f"{manager}.runtime_secret_values_for_artifacts", lambda *_a, **_k: set())+ monkeypatch.setattr(f"{manager}.secret_values_for_drop_check", lambda *_a, **_k: {"123"})++ outcome = LoopOutcome(status="completed", reason="done", billable_actions=[])+ _step, _task, loop_mock, _post = await _run_execute_task_v3(monkeypatch, outcome)++ resolve = loop_mock.await_args.kwargs["label_secret_values"]+ assert set(resolve()) == {"123"}diff --git a/tests/unit/test_code_block_downloads.py b/tests/unit/test_code_block_downloads.pyindex c1529014a..8ba7760ce 100644--- a/tests/unit/test_code_block_downloads.py+++ b/tests/unit/test_code_block_downloads.py@@ -156,11 +156,14 @@ def _wire_secure_runner(on_execute: Callable[[], None] | None = None,downgrade: bool = False,download_operation_invoked: bool = False,+ download_operation_outcome: str | None = None,) -> None:"""Route execute() down the secure sidecar arm, whose returned payload is what the host binds.``downgrade`` returns no result from the override, the one way the runner arm falls through to- inline execution."""+ inline execution. ``download_operation_outcome`` stamps the receipt's brokered-claim outcome+ (``returned_unproven`` / ``returned_proven`` / ``raised``); ``download_operation_invoked`` with no+ outcome models an old producer whose receipt carries presence but no verdict-arming state."""block_result = BlockResult(success=True,output_parameter=_output_parameter("code_out"),@@ -168,6 +171,11 @@ def _wire_secure_runner(status=BlockStatus.completed,workflow_run_block_id="",)+ receipt = (+ SimpleNamespace(operation="click_and_claim_download", outcome=download_operation_outcome)+ if download_operation_invoked or download_operation_outcome is not None+ else None+ )async def _execute_override(**kwargs: object) -> SimpleNamespace | None:if on_execute is not None:@@ -177,7 +185,7 @@ def _wire_secure_runner(return SimpleNamespace(block_result=block_result,failure=None,- download_operation_receipt=object() if download_operation_invoked else None,+ download_operation_receipt=receipt,)fake_app = block_module.app@@ -2697,6 +2705,191 @@ async def test_secure_runner_typed_download_with_no_final_row_is_bounded_and_invassert "downloaded_file_artifact_ids" not in result.output_parameter_value+async def test_secure_unproven_session_claim_with_no_registration_fails_the_block(+ monkeypatch: pytest.MonkeyPatch, _isolated_download_path: str+) -> None:+ """Production shape: a brokered SESSION_DIR claim returned a success-shaped value while nothing+ registered to this run. The block must fail truthfully with a typed reason instead of completing+ on the fabricated placeholder name, and the armed receipt must run the full bounded wait even+ though the first watcher-row read was empty."""+ skyvern_context.set(_session_context())++ sleep = AsyncMock()+ monkeypatch.setattr(block_module, "asyncio", ScopedAsyncio(sleep=sleep))+ claim = AsyncMock(return_value=0)+ _fake_storage_app(+ monkeypatch,+ save=AsyncMock(),+ get=AsyncMock(return_value=[]),+ claim=claim,+ in_flight=AsyncMock(return_value=[]),+ )+ _artifact_first_downloads(monkeypatch, enabled=True)+ _wire_block_runtime(monkeypatch, download_binding=DownloadBinding.SESSION_DIR)+ _wire_secure_runner(+ monkeypatch,+ output={"status": "ok"},+ download_operation_invoked=True,+ download_operation_outcome="returned_unproven",+ )++ block = CodeBlock(+ label="download_statement",+ code='await click_and_claim_download(page, "a.download")',+ output_parameter=_output_parameter("code_out"),+ )+ result = await block.execute(workflow_run_id="wr_1", workflow_run_block_id="", organization_id="o_1")++ assert result.success is False+ assert result.status == BlockStatus.failed+ assert result.failure_reason == block_module.DOWNLOAD_CLAIM_FAILURE_REASON+ assert result.failure_reason != block_module.DOWNLOAD_BINDING_FAILURE_REASON+ persisted = _persisted_output()+ assert persisted[block_module.UNBOUND_DOWNLOAD_OUTPUT_KEY] is True+ assert block_output_has_registered_download(persisted) is False+ assert "downloaded_file_artifact_ids" not in persisted+ assert "downloaded_file_urls" not in persisted+ # Armed receipt entered the bounded wait/settle path despite an empty first watcher-row read.+ assert claim.await_count == 1 + block_module._CODE_BLOCK_SESSION_DOWNLOAD_SETTLE_ATTEMPTS+++async def test_secure_unproven_session_claim_binds_a_run_registered_file(+ monkeypatch: pytest.MonkeyPatch, _isolated_download_path: str+) -> None:+ """A run-bound registration authorizes success even when the claim returned an unproven summary:+ the file is what the run accounts for, not the claim's return value."""+ skyvern_context.set(_session_context())++ monkeypatch.setattr(block_module, "asyncio", ScopedAsyncio(sleep=AsyncMock()))+ claim = AsyncMock(return_value=1)+ read = _claim_gated_read(claim, before=[], after=[_SESSION_FILE])+ _fake_storage_app(monkeypatch, save=AsyncMock(), get=read, claim=claim)+ _artifact_first_downloads(monkeypatch, enabled=True)+ _wire_block_runtime(monkeypatch, download_binding=DownloadBinding.SESSION_DIR)+ _wire_secure_runner(monkeypatch, output={"status": "ok"}, download_operation_outcome="returned_unproven")++ block = CodeBlock(+ label="download_statement",+ code='await click_and_claim_download(page, "a.download")',+ output_parameter=_output_parameter("code_out"),+ )+ result = await block.execute(workflow_run_id="wr_1", workflow_run_block_id="", organization_id="o_1")++ assert result.success is True+ assert result.failure_reason is None+ assert result.output_parameter_value["downloaded_file_urls"] == [_SESSION_FILE.url]+++async def test_secure_unproven_claim_with_unreadable_registration_abstains(+ monkeypatch: pytest.MonkeyPatch, _isolated_download_path: str+) -> None:+ """An unreadable registration read is not an empty one: storage trouble must abstain (today's+ behavior), never be collapsed into a false failure."""+ skyvern_context.set(_session_context())++ monkeypatch.setattr(block_module, "asyncio", ScopedAsyncio(sleep=AsyncMock()))+ _fake_storage_app(+ monkeypatch,+ save=AsyncMock(),+ get=AsyncMock(side_effect=RuntimeError("storage unavailable")),+ in_flight=AsyncMock(return_value=[]),+ )+ _artifact_first_downloads(monkeypatch, enabled=True)+ _wire_block_runtime(monkeypatch, download_binding=DownloadBinding.SESSION_DIR)+ _wire_secure_runner(monkeypatch, output={"status": "ok"}, download_operation_outcome="returned_unproven")++ block = CodeBlock(+ label="download_statement",+ code='await click_and_claim_download(page, "a.download")',+ output_parameter=_output_parameter("code_out"),+ )+ result = await block.execute(workflow_run_id="wr_1", workflow_run_block_id="", organization_id="o_1")++ assert result.success is True+ assert result.failure_reason is None+++async def test_secure_unproven_claim_abstains_when_settle_reads_fail(+ monkeypatch: pytest.MonkeyPatch, _isolated_download_path: str+) -> None:+ """A readable-empty first read followed by a settle-phase storage failure is unreadable, not+ proven-empty: the block must abstain (today's fail-open), never fail the claim on storage trouble."""+ skyvern_context.set(_session_context())++ monkeypatch.setattr(block_module, "asyncio", ScopedAsyncio(sleep=AsyncMock()))+ reads = {"n": 0}++ async def _get(**_kwargs: object) -> list[FileInfo]:+ reads["n"] += 1+ if reads["n"] == 1:+ return []+ raise RuntimeError("storage blip during settle")++ _fake_storage_app(+ monkeypatch,+ save=AsyncMock(),+ get=AsyncMock(side_effect=_get),+ in_flight=AsyncMock(return_value=[]),+ )+ _artifact_first_downloads(monkeypatch, enabled=True)+ _wire_block_runtime(monkeypatch, download_binding=DownloadBinding.SESSION_DIR)+ _wire_secure_runner(monkeypatch, output={"status": "ok"}, download_operation_outcome="returned_unproven")++ block = CodeBlock(+ label="download_statement",+ code='await click_and_claim_download(page, "a.download")',+ output_parameter=_output_parameter("code_out"),+ )+ result = await block.execute(workflow_run_id="wr_1", workflow_run_block_id="", organization_id="o_1")++ assert result.success is True+ assert result.failure_reason is None+++ "outcome",+ ["returned_proven", "raised", None],+ ids=["returned_proven", "raised", "legacy_no_outcome"],+)+async def test_secure_non_unproven_outcomes_leave_the_block_unarmed(+ monkeypatch: pytest.MonkeyPatch, _isolated_download_path: str, outcome: str | None+) -> None:+ """Only ``returned_unproven`` arms the settlement verdict. A proven claim, a claim whose raise the+ author swallowed, and an old-producer receipt with no outcome must all preserve today's success."""+ skyvern_context.set(_session_context())++ monkeypatch.setattr(block_module, "asyncio", ScopedAsyncio(sleep=AsyncMock()))+ _fake_storage_app(+ monkeypatch,+ save=AsyncMock(),+ get=AsyncMock(return_value=[]),+ in_flight=AsyncMock(return_value=[]),+ )+ _artifact_first_downloads(monkeypatch, enabled=True)+ _wire_block_runtime(monkeypatch, download_binding=DownloadBinding.SESSION_DIR)+ _wire_secure_runner(+ monkeypatch,+ output={"status": "ok"},+ download_operation_invoked=True,+ download_operation_outcome=outcome,+ )++ block = CodeBlock(+ label="download_statement",+ code='await click_and_claim_download(page, "a.download")',+ output_parameter=_output_parameter("code_out"),+ )+ result = await block.execute(workflow_run_id="wr_1", workflow_run_block_id="", organization_id="o_1")++ assert result.success is True+ assert result.failure_reason is None++@pytest.mark.asyncioasync def test_in_flight_partial_is_never_bound_as_the_users_file(monkeypatch: pytest.MonkeyPatch, _isolated_download_path: strdiff --git a/tests/unit/test_code_block_sandbox.py b/tests/unit/test_code_block_sandbox.pyindex b0d753f4f..9ce31c9e5 100644--- a/tests/unit/test_code_block_sandbox.py+++ b/tests/unit/test_code_block_sandbox.py@@ -9,11 +9,19 @@ Verifies that the CodeBlock safety layer:import asyncioimport inspectimport json+import operatorimport re-from collections.abc import AsyncIterator+import warnings+from collections.abc import AsyncIterator, Callable, Mappingfrom contextlib import asynccontextmanager-from datetime import UTC, datetime, timezone-from types import SimpleNamespace+from datetime import UTC+from datetime import date as stdlib_date+from datetime import datetime+from datetime import time as stdlib_time+from datetime import timezone+from functools import partial+from types import FunctionType, SimpleNamespace+from typing import Any, NoReturnfrom unittest.mock import AsyncMock, MagicMockimport pytest@@ -59,6 +67,168 @@ from skyvern.webeye.skycdp.errors import CdpErrorfrom tests.unit.conftest import FakeClearingBrowserContext, FakeSearchBrowserContextfrom tests.unit.fake_workflow_run_context import FakeWorkflowRunContext+RAW_DATETIME_TYPES = (stdlib_date, datetime, stdlib_time)+FACADE_MEMBERS = {"datetime", "date", "timedelta", "timezone", "UTC"}++_DATE_PROBE_ARGS: dict[str, tuple[Any, ...]] = {+ "__format__": ("%m/%d/%Y",),+ "ctime": (),+ "fromisocalendar": (2026, 38, 1),+ "fromisoformat": ("2026-09-13",),+ "fromordinal": (739872,),+ "fromtimestamp": (1789603200,),+ "isocalendar": (),+ "isoformat": (),+ "isoweekday": (),+ "replace": (),+ "strftime": ("%m/%d/%Y",),+ "strptime": ("2026-09-13", "%Y-%m-%d"),+ "timetuple": (),+ "today": (),+ "toordinal": (),+ "weekday": (),+}+_DATETIME_PROBE_ARGS: dict[str, tuple[Any, ...]] = _DATE_PROBE_ARGS | {+ "astimezone": (UTC,),+ "combine": (stdlib_date(2026, 9, 13), stdlib_time(4, 5, 6)),+ "date": (),+ "dst": (),+ "fromisoformat": ("2026-09-13T04:05:06",),+ "now": (),+ "time": (),+ "timestamp": (),+ "timetz": (),+ "tzname": (),+ "utcfromtimestamp": (1789603200,),+ "utcnow": (),+ "utcoffset": (),+ "utctimetuple": (),+}+_TIME_PROBE_ARGS: dict[str, tuple[Any, ...]] = {+ "__format__": ("%H:%M",),+ "dst": (),+ "fromisoformat": ("04:05:06",),+ "isoformat": (),+ "replace": (),+ "strftime": ("%H:%M",),+ "strptime": ("04:05", "%H:%M"),+ "tzname": (),+ "utcoffset": (),+}+++class NoPageOperationPage:+ def __getattr__(self, name: str) -> NoReturn:+ raise AssertionError(f"a date read must not reach the page: {name}")+++def _call_from_sandbox_frame(member: Callable[..., Any], args: tuple[Any, ...]) -> Any:+ return member(*args)+++def _accepts(member: Callable[..., Any], args: tuple[Any, ...], kwargs: dict[str, Any]) -> bool:+ try:+ member(*args, **kwargs)+ except TypeError:+ return False+ except Exception:+ return True+ return True+++def _delegate_parameter_kind_defects(+ label: str,+ instance: stdlib_date | stdlib_time,+ stdlib_type: type,+ probes: Mapping[str, tuple[Any, ...]],+) -> list[str]:+ """A delegate bound in place of a C stdlib method must accept the same arguments by keyword as+ the method it shadows, or already-deployed blocks passing that argument by name start failing."""+ defects: list[str] = []+ sandbox_type = type(instance)+ for name in sorted(vars(sandbox_type)):+ if name.startswith("_"):+ continue+ sandbox_member = getattr(sandbox_type, name)+ if not inspect.isroutine(sandbox_member):+ continue+ stdlib_member = getattr(stdlib_type, name, None)+ if stdlib_member is None:+ defects.append(f"{label}.{name} shadows nothing on {stdlib_type.__name__}")+ continue+ parameters = [+ parameter for parameter in inspect.signature(sandbox_member).parameters if parameter not in ("self", "cls")+ ]+ keywords = dict(zip(parameters, probes.get(name, ())))+ if len(keywords) != len(parameters):+ defects.append(f"{label}.{name} has no probe arguments")+ continue+ arguments = () if inspect.ismethod(sandbox_member) else (instance,)+ if _accepts(sandbox_member, arguments, keywords) != _accepts(stdlib_member, arguments, keywords):+ defects.append(f"{label}.{name} does not accept the same keyword arguments as {stdlib_type.__name__}")+ return defects+++def datetime_facade_defects(namespace: SimpleNamespace, sandbox_builtins: Mapping[str, Any]) -> list[str]:+ """Exercise every public member of a CodeBlock datetime facade from a frame carrying the sandbox+ builtins, where a member reaching CPython's hidden import raises and a raw C return re-breaks it."""+ call = FunctionType(_call_from_sandbox_frame.__code__, {"__builtins__": sandbox_builtins})+ defects: list[str] = []+ if set(vars(namespace)) != FACADE_MEMBERS:+ defects.append(f"facade members are {sorted(vars(namespace))}")+ if not issubclass(namespace.datetime, namespace.date):+ defects.append("datetime is not a subclass of date")+ if not isinstance(call(namespace.datetime.now, ()), namespace.date):+ defects.append("datetime.now() is not an instance of date")+ # ``date`` precedes ``datetime`` in the facade MRO, so an unmirrored date delegate shadows datetime's.+ unmirrored = {name for name in vars(namespace.date) if not name.startswith("_")} - set(vars(namespace.datetime))+ if unmirrored:+ defects.append(f"date members not mirrored on datetime: {sorted(unmirrored)}")+ for label, instance, stdlib_type, probes in (+ ("date", namespace.date(2026, 9, 13), stdlib_date, _DATE_PROBE_ARGS),+ ("datetime", namespace.datetime(2026, 9, 13, 4, 5, 6, tzinfo=UTC), datetime, _DATETIME_PROBE_ARGS),+ ("time", namespace.datetime(2026, 9, 13, 4, 5, 6).time(), stdlib_time, _TIME_PROBE_ARGS),+ ):+ defects += _delegate_parameter_kind_defects(label, instance, stdlib_type, probes)+ try:+ call(partial(instance.strftime, format=probes["strftime"][0]), ())+ except Exception as error:+ defects.append(f"{label}.strftime(format=) raised {type(error).__name__}: {error}")+ for name in sorted(name for name in dir(instance) if not name.startswith("_") or name == "__format__"):+ member = getattr(instance, name)+ if not callable(member):+ value = member+ elif name not in probes:+ defects.append(f"{label}.{name} has no probe arguments")+ continue+ else:+ try:+ # utcnow/utcfromtimestamp only reach the hidden import when their+ # DeprecationWarning is actually emitted, which the default filter suppresses.+ with warnings.catch_warnings():+ warnings.simplefilter("always", DeprecationWarning)+ value = call(member, probes[name])+ except Exception as error:+ defects.append(f"{label}.{name} raised {type(error).__name__}: {error}")+ continue+ if type(value) in RAW_DATETIME_TYPES:+ defects.append(f"{label}.{name} returned a raw {type(value).__name__}")+ for label, instance in (+ ("date", namespace.date(2026, 9, 13)),+ ("datetime", namespace.datetime(2026, 9, 13, 4, 5, 6)),+ ):+ for symbol, operation in (("+", operator.add), ("-", operator.sub)):+ try:+ shifted = call(operation, (instance, namespace.timedelta(days=1)))+ call(shifted.strftime, ("%m/%d/%Y",))+ except Exception as error:+ defects.append(f"{label} {symbol} timedelta raised {type(error).__name__}: {error}")+ continue+ if type(shifted) in RAW_DATETIME_TYPES:+ defects.append(f"{label} {symbol} timedelta returned a raw {type(shifted).__name__}")+ return defects++# ---------------------------------------------------------------------------# is_safe_code — rejection tests# ---------------------------------------------------------------------------@@ -600,6 +770,36 @@ class TestBuildSafeVars:assert safe_vars["datetime"].UTC is UTCassert not hasattr(safe_vars["datetime"], "now")+ def test_datetime_facade_members_all_run_under_restricted_globals(self) -> None:+ safe_vars = CodeBlock.build_safe_vars()+ assert datetime_facade_defects(safe_vars["datetime"], safe_vars["__builtins__"]) == []++ def test_datetime_facade_types_name_themselves_as_the_stdlib_types(self) -> None:+ namespace = CodeBlock.build_safe_vars()["datetime"]+ assert f"{namespace.date}" == f"{stdlib_date}"+ assert f"{namespace.datetime}" == f"{datetime}"+ assert repr(namespace.date(2026, 9, 13)) == repr(stdlib_date(2026, 9, 13))+ assert (namespace.date.__name__, stdlib_date.__name__) == ("datetime.date", "date")+ assert (namespace.datetime.__name__, datetime.__name__) == ("datetime.datetime", "datetime")++ def test_datetime_facade_types_are_rebuilt_for_every_execution(self) -> None:+ """The facade types are writable heap types; a block mutating one must not reach the next."""+ assert CodeBlock.build_safe_vars()["datetime"].date is not CodeBlock.build_safe_vars()["datetime"].date++ def test_datetime_facade_does_not_reopen_imports_or_builtins(self) -> None:+ safe_vars = CodeBlock.build_safe_vars()+ assert "__import__" not in safe_vars["__builtins__"]+ for rejected in (+ "import os",+ "from os import path",+ 'x = __import__("os")',+ "x = datetime.datetime.__mro__",+ "x = datetime.date._SandboxDate",+ 'x = datetime.date.today.__globals__["os"]',+ ):+ with pytest.raises(InsecureCodeDetected):+ CodeBlock.is_safe_code(rejected)+def test_no_safe_var_exposes_dangerous_module(self) -> None:"""No value in safe_vars should be a module that has subprocess/OS capabilities."""import types@@ -1318,6 +1518,45 @@ async def wrapper({default_args}):assert await user_function() == {"zip_value": "94105", "len_value": "persisted-len"}+ @pytest.mark.asyncio+ async def test_datetime_reads_execute_through_the_real_legacy_user_function(self) -> None:+ code = (+ "utc_today = datetime.datetime.now(datetime.UTC).strftime('%m/%d/%Y')\n"+ "local_today = datetime.date.today()\n"+ "local_via_today = datetime.datetime.today().date()\n"+ "return {\n"+ " 'utc_today': utc_today,\n"+ " 'local_today': local_today.isoformat(),\n"+ " 'local_via_today': local_via_today.isoformat(),\n"+ " 'formatted': f'{local_today:%Y-%m-%d}',\n"+ "}"+ )+ now = datetime.now(UTC)+ block = CodeBlock(+ label="datetime_block",+ code=code,+ output_parameter=OutputParameter(+ parameter_type=ParameterType.OUTPUT,+ key="datetime_output",+ description="test output",+ output_parameter_id="op_datetime",+ workflow_id="w_test",+ created_at=now,+ modified_at=now,+ ),+ )+ for _ in range(3):+ utc_before = datetime.now(UTC).date()+ local_before = datetime.now().date()+ result = await block.generate_async_user_function(block.code, NoPageOperationPage(), parameters={})()+ utc_after = datetime.now(UTC).date()+ local_after = datetime.now().date()++ assert utc_before <= datetime.strptime(result["utc_today"], "%m/%d/%Y").date() <= utc_after+ assert local_before <= stdlib_date.fromisoformat(result["local_today"]) <= local_after+ assert local_before <= stdlib_date.fromisoformat(result["local_via_today"]) <= local_after+ assert result["formatted"] == result["local_today"]+@pytest.mark.asyncioasync def test_parameters_cannot_override_sandbox_internals(self) -> None:"""Workflow parameters must not overwrite sandbox-controlled names."""diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.pyindex 9e5a33b6e..a3828770d 100644--- a/tests/unit/test_copilot_agent_helpers.py+++ b/tests/unit/test_copilot_agent_helpers.py@@ -990,8 +990,9 @@ workflow_definition:"`await click_and_claim_download(page, selector)` for a download, ""`await page.wait_for_url(url, timeout=...)` for navigation, or ""`await page.wait_for_selector(selector, timeout=...)` for whatever the event renders on the "- "page. There is no brokered way to wait on a network response; wait on what the response "- "renders instead."+ "page. Network responses can be recorded, with page.on('response', handler) whose handler only "+ "appends response fields to a list defined in the block, but there is still no brokered way to "+ "wait on one; wait on what the response renders instead.")replacement = "await page.wait_for_selector(selector, timeout=...)"# A runner denial names the sanctioned replacement after the denied call, so a bound thatdiff --git a/tests/unit/test_copilot_composition_evidence.py b/tests/unit/test_copilot_composition_evidence.pyindex d2275f7a0..9e1ffd605 100644--- a/tests/unit/test_copilot_composition_evidence.py+++ b/tests/unit/test_copilot_composition_evidence.py@@ -18,6 +18,7 @@ from playwright.async_api import Page, Route, async_playwrightfrom skyvern.config import settingsfrom skyvern.forge.sdk.copilot import tools as tools_module+from skyvern.forge.sdk.copilot.browser_ablation import CopilotToolSurfaceIdentityfrom skyvern.forge.sdk.copilot.challenge_evidence import (CHALLENGE_EVIDENCE_SOURCE_KEY,CHALLENGE_KIND_KEY,@@ -86,6 +87,7 @@ class _Ctx:workflow_verification_evidence: WorkflowVerificationEvidence = field(default_factory=WorkflowVerificationEvidence)post_run_page_observation_after_failed_test: bool = Falselast_failure_category_top: str | None = None+ tool_surface_identity: CopilotToolSurfaceIdentity | None = Nonedef _flow_entry(@@ -1630,6 +1632,58 @@ def test_composition_gate_error_names_nearest_url_before_new_page_block() -> Nonassert "target_url='https://example.com/registry/search'" in error+ ("identity", "expected", "forbidden"),+ [+ (CopilotToolSurfaceIdentity.REQUIRED_CODE, "inspect_page_for_composition for", "evaluate"),+ (None, "inspect_page_for_composition or evaluate", None),+ ],+ ids=["required_code", "optional"],+)+def test_a_string_observation_ref_is_corrected_with_tools_the_surface_offers(+ identity: CopilotToolSurfaceIdentity | None, expected: str, forbidden: str | None+) -> None:+ workflow_yaml = _yaml(+ {"block_type": "goto_url", "label": "open_home", "url": "https://example.com/"},+ {"block_type": "action", "label": "search_product", "navigation_goal": "Search for the product."},+ {"block_type": "action", "label": "add_to_cart", "navigation_goal": "Click the Add to Cart button."},+ )+ ctx = _Ctx(+ flow_evidence=[_flow_entry("https://example.com/", reached_via="navigate", step=0)],+ block_observation_refs={},+ raw_block_observation_refs={"add_to_cart": "1"},+ tool_surface_identity=identity,+ )++ error = composition_page_evidence_error(ctx, workflow_yaml, raw_block_observation_refs={"add_to_cart": "1"})++ assert error is not None+ assert "as a string" in error+ assert expected in error+ if forbidden is not None:+ assert forbidden not in error+++def test_composition_gate_error_routes_the_open_through_browser_code_on_the_required_surface() -> None:+ workflow_yaml = _yaml(+ {"block_type": "goto_url", "label": "open_home", "url": "https://example.com/"},+ {+ "block_type": "navigation",+ "label": "search_standard_record",+ "url": "https://example.com/registry/search",+ "navigation_goal": "Enter the observed First Name and Last Name fields and submit.",+ },+ )+ ctx = _Ctx(composition_page_evidence=None, tool_surface_identity=CopilotToolSurfaceIdentity.REQUIRED_CODE)+ ctx.workflow_yaml = _yaml({"block_type": "goto_url", "label": "open_home", "url": "https://example.com/"})++ error = composition_page_evidence_error(ctx, workflow_yaml)++ assert error is not None+ assert "Open 'https://example.com/registry/search' from browser code" in error+ assert "target_url=" not in error++def test_composition_gate_rejects_same_origin_browser_observation_before_run_continuation() -> None:existing_yaml = _yaml({"block_type": "goto_url", "label": "open_lookup", "url": "https://example.com/lookup"})workflow_yaml = _yaml(diff --git a/tests/unit/test_copilot_context.py b/tests/unit/test_copilot_context.pyindex 8c99f4f35..3ee37e1a5 100644--- a/tests/unit/test_copilot_context.py+++ b/tests/unit/test_copilot_context.py@@ -65,6 +65,22 @@ def test_blank_browser_run_survives_context_roundtrip() -> None:assert restored.decisions_made == ["test_workflow_from_blank_browser: Login failed", " output: Missing login"]+def test_browser_code_summary_records_the_page_and_the_decision() -> None:+ ctx = StructuredContext()+ ctx.merge_turn_summary(+ [+ {"tool": "run_browser_code", "summary": "Ran browser code (3 operation(s)) at https://example.com/a"},+ {"tool": "run_browser_code", "summary": "Failed: locator timed out at line 2"},+ ]+ )++ assert [v.url for v in ctx.urls_visited] == ["https://example.com/a"]+ assert ctx.decisions_made == [+ "run_browser_code: Ran browser code (3 operation(s)) at https://example.com/a",+ "run_browser_code: Failed: locator timed out at line 2",+ ]++def test_merge_turn_summary_records_resolved_credential_ids() -> None:ctx = StructuredContext()activity = [diff --git a/tests/unit/test_copilot_diagnosis_repair_contract.py b/tests/unit/test_copilot_diagnosis_repair_contract.pyindex 90e103d5c..1761c08a3 100644--- a/tests/unit/test_copilot_diagnosis_repair_contract.py+++ b/tests/unit/test_copilot_diagnosis_repair_contract.py@@ -898,7 +898,9 @@ def test_runtime_repair_context_keeps_a_runner_denials_named_replacement_whole()"`await click_and_claim_download(page, selector)` for a download, ""`await page.wait_for_url(url, timeout=...)` for navigation, or ""`await page.wait_for_selector(selector, timeout=...)` for whatever the event renders on the page. "- "There is no brokered way to wait on a network response; wait on what the response renders instead."+ "Network responses can be recorded, with page.on('response', handler) whose handler only appends "+ "response fields to a list defined in the block, but there is still no brokered way to wait on one; "+ "wait on what the response renders instead.")result = {"ok": False,diff --git a/tests/unit/test_copilot_evaluate_page_evidence.py b/tests/unit/test_copilot_evaluate_page_evidence.pyindex 6b9be60fe..b4980feb1 100644--- a/tests/unit/test_copilot_evaluate_page_evidence.py+++ b/tests/unit/test_copilot_evaluate_page_evidence.py@@ -250,6 +250,11 @@ async def test_current_page_inspection_withholds_when_a_disclosure_prerequisite_async def test_sensitive_named_url_inspection_clears_only_its_successfully_navigated_session(monkeypatch: pytest.MonkeyPatch,) -> None:+ # The mock page has no string URL; the live read would return the withheld page's.+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.tools.composition_capture._live_working_page_url",+ AsyncMock(side_effect=["https://private.example.test/account", "https://public.example.test/search"]),+ )ctx = _ctx()ctx.browser_session_id = "pbs-debug"ctx.last_run_blocks_workflow_run_id = "wr-sensitive"@@ -302,10 +307,141 @@ async def test_sensitive_named_url_inspection_clears_only_its_successfully_navigcapture.assert_awaited_once()+async def test_sensitive_named_url_inspection_that_only_moves_the_fragment_keeps_the_page_withheld(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # The mock page has no string URL; the live read would return the withheld page's.+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.tools.composition_capture._live_working_page_url",+ AsyncMock(side_effect=["https://private.example.test/account", "https://private.example.test/account#top"]),+ )+ ctx = _ctx()+ ctx.browser_session_id = "pbs-debug"+ ctx.last_run_blocks_workflow_run_id = "wr-sensitive"+ ctx.last_run_blocks_browser_session_id = "pbs-debug"+ ctx.origin_run_redaction_registry = OriginRunRedactionRegistry(+ "wr-sensitive",+ {"password": "origin-secret"},+ contains_sensitive_values=True,+ contains_all_sensitive_values=True,+ )+ ctx.sensitive_origin_browser_session_ids = {"pbs-debug", "pbs-other"}++ navigate = AsyncMock(return_value={"ok": True, "data": {"url": "https://private.example.test/account#top"}})+ capture = AsyncMock(+ return_value=(+ {+ "inspected_url": "https://private.example.test/account#top",+ "current_url": "https://private.example.test/account#top",+ "source_tool": "inspect_page_for_composition",+ "forms": [],+ "navigation_targets": [],+ "result_containers": [],+ "challenge_controls": [],+ },+ None,+ )+ )+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.tools.composition_capture._authority_tool_error",+ lambda *_args: None,+ )+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.tools.composition_capture._discovery_navigate",+ navigate,+ )+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.tools.composition_capture._capture_composition_evidence",+ capture,+ )+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.tools.composition_capture._bind_login_credential_for_observed_url",+ AsyncMock(),+ )++ result = await _inspect_page_for_composition_impl(ctx, "https://private.example.test/account#top")++ # The registry is complete, so scrubbed facts may be disclosed; but a fragment hop leaves the+ # sensitive document on screen, so the taint stays and pixels remain denied.+ assert result["ok"] is True+ assert ctx.sensitive_origin_browser_session_ids == {"pbs-debug", "pbs-other"}+ navigate.assert_awaited_once()+++async def test_a_fragment_hop_on_a_page_whose_url_holds_a_registered_value_keeps_the_page_withheld(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # The mock page has no string URL; the live read would return the withheld page's.+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.tools.composition_capture._live_working_page_url",+ AsyncMock(+ side_effect=["https://private.example.test/u/alice-4412", "https://private.example.test/u/alice-4412#top"]+ ),+ )+ ctx = _ctx()+ ctx.browser_session_id = "pbs-debug"+ ctx.last_run_blocks_workflow_run_id = "wr-sensitive"+ ctx.last_run_blocks_browser_session_id = "pbs-debug"+ ctx.origin_run_redaction_registry = OriginRunRedactionRegistry(+ "wr-sensitive",+ {"password": "origin-secret"},+ contains_sensitive_values=True,+ contains_all_sensitive_values=True,+ )+ ctx.sensitive_origin_browser_session_ids = {"pbs-debug", "pbs-other"}++ navigate = AsyncMock(return_value={"ok": True, "data": {"url": "https://private.example.test/u/****#top"}})+ capture = AsyncMock(+ return_value=(+ {+ "inspected_url": "https://private.example.test/u/****#top",+ "current_url": "https://private.example.test/u/****#top",+ "source_tool": "inspect_page_for_composition",+ "forms": [],+ "navigation_targets": [],+ "result_containers": [],+ "challenge_controls": [],+ },+ None,+ )+ )+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.tools.composition_capture._authority_tool_error",+ lambda *_args: None,+ )+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.tools.composition_capture._discovery_navigate",+ navigate,+ )+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.tools.composition_capture._capture_composition_evidence",+ capture,+ )+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.tools.composition_capture._bind_login_credential_for_observed_url",+ AsyncMock(),+ )++ result = await _inspect_page_for_composition_impl(ctx, "https://private.example.test/u/****#top")++ # The registry is complete, so scrubbed facts may be disclosed; but a fragment hop leaves the+ # sensitive document on screen, so the taint stays and pixels remain denied.+ assert result["ok"] is True+ assert ctx.sensitive_origin_browser_session_ids == {"pbs-debug", "pbs-other"}+ navigate.assert_awaited_once()++@pytest.mark.asyncioasync def test_sensitive_registration_waits_for_named_navigation_capture_transaction(monkeypatch: pytest.MonkeyPatch,) -> None:+ # The mock page has no string URL; the live read would return the withheld page's.+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.tools.composition_capture._live_working_page_url",+ AsyncMock(side_effect=["https://private.example.test/account", "https://public.example.test/search"]),+ )ctx = _ctx()ctx.browser_session_id = "pbs-debug"ctx.sensitive_origin_browser_session_ids = {"pbs-debug"}diff --git a/tests/unit/test_copilot_fill_credential_field.py b/tests/unit/test_copilot_fill_credential_field.pyindex 6a1e9d585..c3a84ff0b 100644--- a/tests/unit/test_copilot_fill_credential_field.py+++ b/tests/unit/test_copilot_fill_credential_field.py@@ -89,6 +89,7 @@ def _ctx(**overrides: Any) -> SimpleNamespace:carried_trajectory_rebound_done=False,observed_browser_urls=[],pending_scout_source_url=None,+ pending_taint_source_urls={},pending_scout_download_snapshot=None,pending_scout_download=False,pending_scout_download_detachers=[],@@ -485,7 +486,7 @@ def _wire_impl(return None@asynccontextmanager- async def fake_browser_context(_ctx: Any) -> AsyncIterator[None]:+ async def fake_browser_context(_ctx: object, **_kwargs: object) -> AsyncIterator[None]:yieldasync def fake_get_page(session_id: str | None = None) -> tuple[_FakePage, None]:@@ -1381,6 +1382,49 @@ class TestPublicToolCall:assert result["data"]["submit_selector"] == "#verifyButton"assert "123456" not in payload+ @pytest.mark.asyncio+ @pytest.mark.parametrize(("target", "acting_session"), [("debug", "pbs_debug"), ("last_run", "pbs_run")])+ async def test_the_fill_and_its_submit_land_in_the_browser_the_call_named(+ self, monkeypatch: pytest.MonkeyPatch, target: str, acting_session: str+ ) -> None:+ """A run that minted its own browser leaves two pages on the same login form. A `last_run`+ cell followed by a fill that silently acts on the chat's browser submits a credential there."""+ pages = {"pbs_debug": _FakePage(), "pbs_run": _FakePage()}+ _wire_impl(monkeypatch, pages["pbs_debug"], secret_value="123456")+ provisioned: list[bool] = []++ async def get_page_for(session_id: str | None = None) -> tuple[_FakePage, None]:+ return pages[session_id or ""], None++ async def ensure(_ctx: object) -> None:+ provisioned.append(True)++ monkeypatch.setattr(credential_fill_module, "get_page", get_page_for)+ monkeypatch.setattr(credential_fill_module, "ensure_browser_session", ensure)+ ctx = RunContextWrapper(_ctx(browser_session_id="pbs_debug", last_run_blocks_browser_session_id="pbs_run"))+ ctx.tool_name = "fill_credential_field"++ tool = next(t for t in tools_module.NATIVE_TOOLS if t.name == "fill_credential_field")+ payload = await tool.on_invoke_tool(+ ctx,+ json.dumps(+ {+ "selector": "#totpCode",+ "credential_id": "cred_123",+ "field": "totp",+ "submit_selector": "#verifyButton",+ "target": target,+ }+ ),+ )++ result = json.loads(payload)+ idle_session = next(s for s in pages if s != acting_session)+ assert result["ok"] is True and result["browser_target"] == target+ assert pages[acting_session].fill_calls and pages[acting_session].click_calls == [("#verifyButton",)]+ assert not pages[idle_session].fill_calls and not pages[idle_session].click_calls+ assert provisioned == ([True] if target == "debug" else [])+@pytest.mark.asyncioasync def test_a_credential_short_enough_to_sit_inside_an_outcome_word_cannot_forge_another_outcome(self, monkeypatch: pytest.MonkeyPatch@@ -1418,7 +1462,7 @@ class TestToolRegistration:description = tool.description or ""assert "server-side" in descriptionassert "never" in description- assert "type_text" in description+ assert "rather than typing the value yourself" in descriptionassert "code_artifact_metadata.input_bindings" in descriptionassert "credential_parameter.key" not in descriptionassert "credential_parameter.otp_accessor" not in descriptiondiff --git a/tests/unit/test_copilot_frontier.py b/tests/unit/test_copilot_frontier.pyindex 169c6d0f8..31b6ca9e5 100644--- a/tests/unit/test_copilot_frontier.py+++ b/tests/unit/test_copilot_frontier.py@@ -23,6 +23,7 @@ from skyvern.forge.sdk.copilot import agent as agent_modulefrom skyvern.forge.sdk.copilot import toolsfrom skyvern.forge.sdk.copilot.agent import _verified_workflow_or_nonefrom skyvern.forge.sdk.copilot.build_test_outcome import BuildTestFailedOperation, RecordedBuildTestOutcome+from skyvern.forge.sdk.copilot.code_block_synthesis import synthesize_goto_code_blockfrom skyvern.forge.sdk.copilot.config import BlockAuthoringPolicy, CopilotConfigfrom skyvern.forge.sdk.copilot.context import CopilotContext, upsert_narrative_block_attemptfrom skyvern.forge.sdk.copilot.mcp_adapter import SkyvernOverlayMCPServer@@ -1303,6 +1304,114 @@ def test_plan_frontier_edit_with_no_upstream_anchor_falls_back_to_full_list() ->assert seed == {}+_DYNAMIC_GOTO_CODE = "await page.goto(start_url)\n"+++def _static_goto_code() -> str:+ synthesized = synthesize_goto_code_block("https://example.com/orders")+ assert synthesized is not None+ return synthesized.code + ' await page.click("#order-total")\n'+++ "code",+ [None, 'await page.goto(url="https://example.com/orders")\n'],+ ids=["synthesized_indented", "url_keyword"],+)+def test_plan_frontier_head_code_block_with_static_goto_starts_initial(code: str | None) -> None:+ code = code or _static_goto_code()+ new = _FakeDefinition([_FakeBlock("open_orders", "code", {"code": code}), _FakeBlock("read", "extraction")])++ labels, _seed, frontier, provenance = _plan_frontier(_make_ctx(), ["open_orders", "read"], None, new)++ assert labels == ["open_orders", "read"]+ assert frontier == "open_orders"+ assert provenance == "initial"+++ ("code_b", "expected"),+ [(None, "replayed"), (_DYNAMIC_GOTO_CODE, "unanchored")],+ ids=["static_goto", "dynamic_goto"],+)+def test_plan_frontier_mid_workflow_code_block_start_replays_only_on_static_goto(+ code_b: str | None, expected: str+) -> None:+ code_b = code_b or _static_goto_code()+ old = _FakeDefinition(+ [+ _FakeBlock("nav", "navigation"),+ _FakeBlock("code_b", "code", {"code": 'await page.click("#old")\n'}),+ _FakeBlock("next", "extraction"),+ ]+ )+ new = _FakeDefinition(+ [+ _FakeBlock("nav", "navigation"),+ _FakeBlock("code_b", "code", {"code": code_b}),+ _FakeBlock("next", "extraction"),+ ]+ )+ ctx = _make_ctx()+ ctx.verified_prefix_labels = ["nav", "code_b", "next"]++ labels, _seed, frontier, provenance = _plan_frontier(ctx, ["code_b", "next"], old, new)++ assert labels == ["code_b", "next"]+ assert frontier == "code_b"+ assert provenance == expected+++ "code",+ [+ 'await page.click("#go")\n',+ 'await page.goto(f"https://example.com/{order_id}")\n',+ _DYNAMIC_GOTO_CODE,+ "await page.goto(config.url)\n",+ 'await page.goto("https://example.com/{{ order_id }}")\n',+ 'start = 1\nawait page.goto("https://example.com")\n',+ '"""Open orders."""\nawait page.goto("https://example.com")\n',+ 'if ready:\n await page.goto("https://example.com")\n',+ 'try:\n await page.goto("https://example.com")\nexcept Exception:\n pass\n',+ 'for _ in range(2):\n await page.goto("https://example.com")\n',+ 'async with page.expect_navigation():\n await page.goto("https://example.com")\n',+ 'await page.goto("https://example.com"\n',+ 'await page.goto("about:blank")\n',+ 'await page.goto("/orders")\n',+ 'page.goto("https://example.com")\n',+ 'await other_page.goto("https://example.com")\n',+ "",+ ],+ ids=[+ "no_goto",+ "f_string_url",+ "variable_url",+ "attribute_url",+ "jinja_parameter_url",+ "goto_after_assignment",+ "goto_after_docstring",+ "goto_inside_if",+ "goto_inside_try",+ "goto_inside_loop",+ "goto_inside_with",+ "unparseable",+ "about_blank_url",+ "relative_url",+ "not_awaited",+ "other_receiver",+ "empty_code",+ ],+)+def test_plan_frontier_head_code_block_without_static_first_goto_stays_unanchored(code: str) -> None:+ new = _FakeDefinition([_FakeBlock("open_orders", "code", {"code": code}), _FakeBlock("read", "extraction")])++ _labels, _seed, frontier, provenance = _plan_frontier(_make_ctx(), ["open_orders", "read"], None, new)++ assert frontier == "open_orders"+ assert provenance == "unanchored"++def test_plan_frontier_without_verified_prefix_falls_back_to_full() -> None:old = _FakeDefinition([_FakeBlock("a", "navigation"), _FakeBlock("b", "extraction")])new = _FakeDefinition([_FakeBlock("a", "navigation"), _FakeBlock("b", "extraction", {"prompt": "changed"})])diff --git a/tests/unit/test_copilot_hooks.py b/tests/unit/test_copilot_hooks.pyindex bf2323080..bba2f4c0d 100644--- a/tests/unit/test_copilot_hooks.py+++ b/tests/unit/test_copilot_hooks.py@@ -1225,6 +1225,7 @@ class TestBrowserInteractionObservationHooks:scouted_interactions=[],scout_trajectory=[],pending_scout_source_url=None,+ pending_taint_source_urls={},last_run_blocks_workflow_run_id=None,browser_session_id=None,request_policy=None,@@ -1277,6 +1278,7 @@ class TestBrowserInteractionObservationHooks:scouted_interactions=[],scout_trajectory=[],pending_scout_source_url=None,+ pending_taint_source_urls={},last_run_blocks_workflow_run_id=None,browser_session_id=None,)@@ -1308,6 +1310,7 @@ class TestBrowserInteractionObservationHooks:scouted_interactions=[],scout_trajectory=[],pending_scout_source_url=None,+ pending_taint_source_urls={},last_run_blocks_workflow_run_id=None,browser_session_id=None,)@@ -1388,6 +1391,7 @@ class TestScoutedInteractionCapture:completion_criteria_turn_state=None,observed_browser_urls=[],pending_scout_source_url=source_url,+ pending_taint_source_urls={},prior_carried_trajectory=[],carried_trajectory_rebound_done=False,request_policy=None,@@ -1732,10 +1736,16 @@ class TestScoutedInteractionCapture:capture = AsyncMock(return_value=True)monkeypatch.setattr(mcp_hooks, "_bind_login_credential_for_observed_url", AsyncMock())monkeypatch.setattr(mcp_hooks, "_capture_post_interaction_screenshot", capture)+ # The live page the post-hook re-reads after the navigation landed.+ monkeypatch.setattr(+ mcp_hooks, "_live_working_page_url", AsyncMock(return_value="https://safe.example.test/start")+ )ctx = self._ctx(source_url="https://private.example.test/account")ctx.browser_session_id = "pbs-debug"ctx.sensitive_origin_browser_session_ids = {"pbs-debug", "pbs-run"}ctx.codeblock_redaction_parameters = {}+ # What the pre-hook captures on a withheld page: the URL the navigation has to leave.+ ctx.pending_taint_source_urls = {"pbs-run": "https://private.example.test/account"}with bound_call_browser_session("pbs-run"):result = await mcp_hooks._navigate_post_hook(diff --git a/tests/unit/test_copilot_output_utils.py b/tests/unit/test_copilot_output_utils.pyindex 205259f95..086677d8d 100644--- a/tests/unit/test_copilot_output_utils.py+++ b/tests/unit/test_copilot_output_utils.py@@ -1641,6 +1641,20 @@ class TestFormatToolResultForUser:)assert agent_summary == "Clicked 'xpath=//button[2]'"+ def test_summarize_tool_result_reports_browser_code_operations_and_page(self) -> None:+ agent_summary = summarize_tool_result(+ "run_browser_code",+ {+ "ok": True,+ "current_url": "https://example.com/members?page=3",+ "operations": [+ {"operation": "goto", "status": "ok"},+ {"operation": "click", "status": "ok", "selector": "#nextBtn"},+ ],+ },+ )+ assert agent_summary == "Ran browser code (2 operation(s)) at https://example.com/members?page=3"+class TestUserFacingSuccess:@staticmethoddiff --git a/tests/unit/test_copilot_raw_secret_safety.py b/tests/unit/test_copilot_raw_secret_safety.pyindex 05e0febf3..67d0eb800 100644--- a/tests/unit/test_copilot_raw_secret_safety.py+++ b/tests/unit/test_copilot_raw_secret_safety.py@@ -779,7 +779,9 @@ async def test_uncited_deterministic_redaction_preserves_run_authority() -> None@pytest.mark.asyncio[email protected]("tool_name", ["run_blocks_and_collect_debug", "discover_workflow_entrypoint", "search_web"])+ "tool_name", ["run_blocks_and_collect_debug", "discover_workflow_entrypoint", "search_web", "run_browser_code"]+)async def test_verified_cited_raw_secret_blocks_browser_acting_tools(tool_name: str) -> None:literal = "Hunter2Portal!"policy, _ = await _build(diff --git a/tests/unit/test_copilot_sensitive_origin_disclosure.py b/tests/unit/test_copilot_sensitive_origin_disclosure.pyindex 2ffe24bb2..9aac894f8 100644--- a/tests/unit/test_copilot_sensitive_origin_disclosure.py+++ b/tests/unit/test_copilot_sensitive_origin_disclosure.py@@ -2,6 +2,8 @@from __future__ import annotations+import pytest+from skyvern.forge.sdk.copilot.runtime import (OriginRunRedactionRegistry,clear_sensitive_origin_page_taint,@@ -81,3 +83,113 @@ def test_a_named_navigation_drops_the_page_and_its_run_attribution() -> None:assert "pbs_run" not in ctx.sensitive_origin_browser_session_idsassert sensitive_origin_runs_for_session(ctx, "pbs_run") == set()+++def test_only_a_navigation_to_another_document_lifts_the_withholding() -> None:+ """Every route that lifts the taint goes through this decision: a fragment hop keeps the+ sensitive DOM on screen, and an unknown starting URL is not evidence of leaving it."""+ from skyvern.forge.sdk.copilot.runtime import clear_sensitive_origin_page_taint_after_navigation++ page = "https://portal.example/account?tab=billing"+ # The last case is a URL that is not a string at all, which a mocked or broken page read can+ # hand back: it keeps the taint instead of reaching the URL parser.+ unreadable = object()+ for source, result in (+ (page, page + "#top"),+ (None, "https://elsewhere.example/"),+ (page, None),+ (unreadable, "https://elsewhere.example/"),+ ):+ ctx = _ctx_after_run()+ assert clear_sensitive_origin_page_taint_after_navigation(ctx, source_url=source, result_url=result) is False+ assert "pbs_run" in ctx.sensitive_origin_browser_session_ids, (source, result)++ ctx = _ctx_after_run()+ assert clear_sensitive_origin_page_taint_after_navigation(+ ctx, source_url=page, result_url="https://elsewhere.example/"+ )+ assert "pbs_run" not in ctx.sensitive_origin_browser_session_ids+++async def test_the_navigate_tool_lifts_the_withholding_only_when_it_leaves_the_document(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ from unittest.mock import AsyncMock++ from skyvern.forge.sdk.copilot.tools import mcp_hooks++ page = "https://portal.example/account?tab=billing"+ live = {"url": page}+ monkeypatch.setattr(mcp_hooks, "_bind_login_credential_for_observed_url", AsyncMock())+ monkeypatch.setattr(mcp_hooks, "_capture_post_interaction_screenshot", AsyncMock(return_value=False))++ async def live_url(_ctx: object) -> str:+ return live["url"]++ monkeypatch.setattr(mcp_hooks, "_live_working_page_url", live_url)++ async def navigate(ctx: object, lands_on: str, reported: str | None = None) -> dict:+ live["url"] = page+ assert await mcp_hooks._navigate_pre_hook({"url": lands_on}, ctx) is None+ live["url"] = lands_on+ # The adapter hands the hook a secret-scrubbed result, so the reported URL can differ from the page.+ return await mcp_hooks._navigate_post_hook({"ok": True, "data": {"url": reported or lands_on}}, {}, ctx)++ fragment_ctx = _ctx_after_run()+ fragment = await navigate(fragment_ctx, page + "#top")+ left_ctx = _ctx_after_run()+ left = await navigate(left_ctx, "https://elsewhere.example/")+ # A registered value in the URL is redacted in the reported URL only; the hop is still same-document.+ page = "https://portal.example/u/alice-4412/account"+ redacted_ctx = _ctx_after_run()+ redacted = await navigate(redacted_ctx, page + "#top", reported="https://portal.example/u/****/account#top")++ assert fragment["ok"] is False and "url" not in fragment and "portal.example" not in str(fragment)+ assert "pbs_run" in fragment_ctx.sensitive_origin_browser_session_ids+ assert redacted["ok"] is False and "pbs_run" in redacted_ctx.sensitive_origin_browser_session_ids+ assert left["ok"] is True and "pbs_run" not in left_ctx.sensitive_origin_browser_session_ids+ # The withheld page's URL is compared and dropped, never recorded as a scouting fact.+ assert all(step.get("source_url") is None for step in left_ctx.scout_trajectory)+ assert left_ctx.pending_taint_source_urls == {} and fragment_ctx.pending_taint_source_urls == {}+++async def test_concurrent_navigations_on_two_tainted_browsers_judge_each_by_its_own_page(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """The before-URL is kept per browser: a fragment hop in one browser is never compared against the+ other browser's page, so it cannot borrow that page's difference to clear its own taint."""+ from unittest.mock import AsyncMock++ from skyvern.forge.sdk.copilot.runtime import bound_call_browser_session+ from skyvern.forge.sdk.copilot.tools import mcp_hooks++ pages = {"pbs_run": "https://portal.example/account", "pbs_other": "https://other.example/statement"}+ ctx = _ctx_after_run()+ record_sensitive_origin_run_taint(ctx, workflow_run_id="wr_other", session_id="pbs_other")+ monkeypatch.setattr(mcp_hooks, "_bind_login_credential_for_observed_url", AsyncMock())+ monkeypatch.setattr(mcp_hooks, "_capture_post_interaction_screenshot", AsyncMock(return_value=False))++ async def live_url(inner: object) -> str:+ from skyvern.forge.sdk.copilot.runtime import effective_browser_session_id++ return pages[effective_browser_session_id(inner) or ""]++ monkeypatch.setattr(mcp_hooks, "_live_working_page_url", live_url)++ # Both pre-hooks run before either post-hook, as two concurrent calls would.+ with bound_call_browser_session("pbs_other"):+ await mcp_hooks._navigate_pre_hook({"url": pages["pbs_other"] + "#x"}, ctx)+ with bound_call_browser_session("pbs_run"):+ await mcp_hooks._navigate_pre_hook({"url": "https://elsewhere.example/"}, ctx)+ pages["pbs_other"] += "#x"+ pages["pbs_run"] = "https://elsewhere.example/"+ with bound_call_browser_session("pbs_other"):+ hop = await mcp_hooks._navigate_post_hook({"ok": True, "data": {"url": pages["pbs_other"]}}, {}, ctx)+ with bound_call_browser_session("pbs_run"):+ left = await mcp_hooks._navigate_post_hook({"ok": True, "data": {"url": "https://elsewhere.example/"}}, {}, ctx)++ assert hop["ok"] is False and "pbs_other" in ctx.sensitive_origin_browser_session_ids+ assert left["ok"] is True and "pbs_run" not in ctx.sensitive_origin_browser_session_ids+ assert ctx.pending_taint_source_urls == {}diff --git a/tests/unit/test_copilot_streaming_adapter.py b/tests/unit/test_copilot_streaming_adapter.pyindex f501ec4e0..d642a65e2 100644--- a/tests/unit/test_copilot_streaming_adapter.py+++ b/tests/unit/test_copilot_streaming_adapter.py@@ -541,22 +541,34 @@ async def test_tool_result_sse_uses_latest_blocker_signal_for_activity_surface()@pytest.mark.asyncio-async def test_stream_to_sse_raises_and_cancels_on_repeated_unrecoverable_tool_error() -> None:[email protected]("tool_name", ["get_browser_screenshot", "run_browser_code"])+ "error_json",+ [+ '{"ok": false, "error": "Browser session pbs_123 not found while taking screenshot (404)."}',+ # run_browser_code names a lost browser by its typed code, in prose with no status or "not found".+ '{"ok": false, "error": "The chat\'s browser session is no longer available.", '+ '"error_code": "browser_session_unavailable"}',+ ],+ ids=["prose", "typed"],+)+async def test_stream_to_sse_raises_and_cancels_on_repeated_unrecoverable_tool_error(+ tool_name: str, error_json: str+) -> None:from agents.items import RunItemfrom agents.stream_events import RunItemStreamEventfrom skyvern.forge.sdk.copilot.enforcement import CopilotUnrecoverableToolError- error_text = "Browser session pbs_123 not found while taking screenshot (404)."events = []for call_id in ("c1", "c2"):call_item = MagicMock(spec=RunItem)- call_item.raw_item = {"call_id": call_id, "name": "get_browser_screenshot", "arguments": "{}"}+ call_item.raw_item = {"call_id": call_id, "name": tool_name, "arguments": "{}"}events.append(RunItemStreamEvent(name="tool_called", item=call_item))output_item = MagicMock(spec=RunItem)- output_item.raw_item = {"call_id": call_id, "name": "get_browser_screenshot"}- output_item.output = [{"type": "text", "text": f'{{"ok": false, "error": "{error_text}"}}'}]+ output_item.raw_item = {"call_id": call_id, "name": tool_name}+ output_item.output = [{"type": "text", "text": error_json}]events.append(RunItemStreamEvent(name="tool_output", item=output_item))result = MagicMock()diff --git a/tests/unit/test_copilot_typed_value_policy.py b/tests/unit/test_copilot_typed_value_policy.pyindex 278525066..4741f614f 100644--- a/tests/unit/test_copilot_typed_value_policy.py+++ b/tests/unit/test_copilot_typed_value_policy.py@@ -15,6 +15,7 @@ async def test_type_text_uses_exact_registered_secret_fact_and_stashes_ordinary_browser_session_id=None,last_run_blocks_workflow_run_id=None,pending_scout_source_url=None,+ pending_taint_source_urls={},pending_scout_input_value=None,discovery_mcp_server=None,block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,@@ -42,6 +43,7 @@ async def test_type_text_pre_hook_does_not_infer_secret_status_from_text_selectobrowser_session_id=None,last_run_blocks_workflow_run_id=None,pending_scout_source_url=None,+ pending_taint_source_urls={},pending_scout_input_value=None,discovery_mcp_server=None,block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,diff --git a/tests/unit/test_iframe_mcp_e2e.py b/tests/unit/test_iframe_mcp_e2e.pyindex 998e15ff4..f743ed124 100644--- a/tests/unit/test_iframe_mcp_e2e.py+++ b/tests/unit/test_iframe_mcp_e2e.py@@ -46,7 +46,7 @@ from skyvern.cli.mcp_tools.browser import ()from skyvern.exceptions import StaleFrameSelectionErrorfrom skyvern.forge.sdk.copilot import mcp_adapter-from skyvern.forge.sdk.copilot.browser_ablation import CopilotToolSurface+from skyvern.forge.sdk.copilot.browser_ablation import CopilotToolSurface, CopilotToolSurfaceIdentityfrom skyvern.forge.sdk.copilot.mcp_adapter import SkyvernOverlayMCPServerfrom skyvern.forge.sdk.copilot.runtime import AgentContextfrom skyvern.forge.sdk.copilot.tools.mcp_hooks import _build_skyvern_mcp_overlays, get_skyvern_mcp_alias_map@@ -282,6 +282,7 @@ async def test_copilot_advertised_frame_tool_chain_real_browser(overlays=overlays,ordered_native_names=(),ordered_mcp_names=surface_names,+ identity=CopilotToolSurfaceIdentity.OPTIONAL,)without_frames = [tool for tool in advertised if not tool.name.startswith("skyvern_frame_")]old_surface = CopilotToolSurface(@@ -290,6 +291,7 @@ async def test_copilot_advertised_frame_tool_chain_real_browser(overlays={tool.name: overlays[tool.name] for tool in without_frames},ordered_native_names=(),ordered_mcp_names=tuple(tool.name for tool in without_frames),+ identity=CopilotToolSurfaceIdentity.OPTIONAL,)assert surface.advertised_sha256(advertised) != old_surface.advertised_sha256(without_frames)diff --git a/tests/unit/test_missing_starter_url.py b/tests/unit/test_missing_starter_url.pyindex 5e17b54f2..844684480 100644--- a/tests/unit/test_missing_starter_url.py+++ b/tests/unit/test_missing_starter_url.py@@ -19,10 +19,13 @@ import pytestfrom skyvern.exceptions import FailedToNavigateToUrl, InvalidWorkflowTaskURLState, MissingStarterUrlfrom skyvern.forge.agent import ForgeAgent, resolve_inherited_workflow_task_page+from skyvern.forge.sdk.core import skyvern_context+from skyvern.forge.sdk.core.skyvern_context import SkyvernContextfrom skyvern.forge.sdk.schemas.tasks import TaskStatusfrom skyvern.forge.sdk.workflow.context_manager import WorkflowRunContextfrom skyvern.forge.sdk.workflow.models.block import TaskBlockfrom skyvern.forge.sdk.workflow.models.parameter import OutputParameter, ParameterType+from skyvern.forge.taskv3.handoff_redaction import caller_authored_block_urlsfrom skyvern.webeye.real_browser_state import RealBrowserState@@ -39,18 +42,23 @@ def _output_parameter(key: str = "task_output") -> OutputParameter:)-def _workflow_run_context() -> WorkflowRunContext:- return WorkflowRunContext(+def _workflow_run_context(values: dict[str, Any] | None = None) -> WorkflowRunContext:+ ctx = WorkflowRunContext(workflow_title="test",workflow_id="w_missing_starter_url_test",workflow_permanent_id="wpid_missing_starter_url_test",workflow_run_id="wr_missing_starter_url_test",aws_client=MagicMock(),)+ if values:+ ctx.values.update(values)+ return ctx@contextmanager-def _mock_block_execute_deps(working_page_url: str) -> Iterator[dict[str, Any]]:+def _mock_block_execute_deps(+ working_page_url: str, run_context_values: dict[str, Any] | None = None+) -> Iterator[dict[str, Any]]:"""Patch the app-level singletons used by TaskBlock.execute() for a first-taskscenario and hand the test back the mocks it needs to assert on."""@@ -105,7 +113,7 @@ def _mock_block_execute_deps(working_page_url: str) -> Iterator[dict[str, Any]]:patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,patch("skyvern.forge.sdk.workflow.models.block.Block.get_workflow_run_context",- return_value=_workflow_run_context(),+ return_value=_workflow_run_context(run_context_values),),patch("skyvern.forge.sdk.workflow.models.block.capture_block_download_baseline",@@ -440,3 +448,37 @@ async def test_execute_does_not_raise_when_profile_loaded_a_page() -> None:# Other downstream failures (e.g. artifact lookup) are fine — we only# care that MissingStarterUrl is NOT raised in this configuration.pass+++async def test_execute_pins_the_blocks_caller_urls_before_the_template_render() -> None:+ """A block's url/navigation_goal are Jinja templates rendered against a context carrying prior+ blocks' recorded output, so a reset link an earlier block read off a page renders into them. The+ caller-known set is read before that render, so only the author's own literals are in it+ (SKY-16271)."""++ off_the_page = "https://mail.example.test/reset/9f2c8a1b4d6e"+ block = TaskBlock(+ label="open_vendor_url",+ output_parameter=_output_parameter(),+ title="Open vendor URL",+ url=None,+ navigation_goal="Open {{ mail_output }}, then check https://portal.example.test/faq.",+ )++ context = SkyvernContext(workflow_run_id="wr_missing_starter_url_test")+ skyvern_context.set(context)+ with _mock_block_execute_deps(working_page_url="about:blank", run_context_values={"mail_output": off_the_page}):+ with pytest.raises(MissingStarterUrl):+ await block.execute(+ workflow_run_id="wr_missing_starter_url_test",+ workflow_run_block_id="wrb_test",+ organization_id="o_test",+ )++ # The render really happened and really produced the page-derived URL...+ assert off_the_page in block.navigation_goal # nosemgrep: incomplete-url-substring-sanitization+ # ...and what may be published is still only what the author typed.+ assert caller_authored_block_urls(+ context, workflow_run_id="wr_missing_starter_url_test", block_label="open_vendor_url"+ ) == frozenset({"https://portal.example.test/faq"})diff --git a/tests/unit/test_real_browser_state_navigate.py b/tests/unit/test_real_browser_state_navigate.pyindex 243e1957d..2949f21d0 100644--- a/tests/unit/test_real_browser_state_navigate.py+++ b/tests/unit/test_real_browser_state_navigate.py@@ -117,25 +117,37 @@ async def test_navigation_accepts_public_caller_destination(@pytest.mark.parametrize("http_status", [200, 404, 410])@pytest.mark.asyncio-async def test_navigate_to_url_records_last_navigation_status(+async def test_navigate_to_url_records_last_navigation_status_with_the_url_it_came_back_on(browser_state: RealBrowserState,monkeypatch: pytest.MonkeyPatch,http_status: int,) -> None:- # The Task V3 loop reads last_navigation_status to classify a dead/removed starting URL, so a- # navigation must record the final response's HTTP status on the state (not leave it unset).+ # The Task V3 loop reads last_navigation_status to classify a dead/removed starting URL and names+ # the page in the customer's failure_reason, so the two must be recorded from the SAME response.+ # A page that moves during the settle or challenge-solver wait (a client-side redirect off a 404)+ # would otherwise leave a later read of page.url standing in for the URL the status belongs to --+ # telling the customer the redirect destination returned 404.def resolves_public(host: str, port: int | None, *args: object, **kwargs: object) -> list[object]:return [(socket.AF_INET, socket.SOCK_STREAM, 0, "", ("93.184.216.34", port or 0))]monkeypatch.setattr("skyvern.utils.url_validators.socket.getaddrinfo", resolves_public)url = "https://public.example.test/path"- response = SimpleNamespace(status=http_status, request=SimpleNamespace(url=url, redirected_from=None))+ redirected_to = "https://public.example.test/somewhere-else"+ response = SimpleNamespace(status=http_status, url=url, request=SimpleNamespace(url=url, redirected_from=None))page = MagicMock()- page.goto = AsyncMock(return_value=response)+ page.url = url++ async def goto(*_args: object, **_kwargs: object) -> object:+ # The client-side redirect lands while the navigation is still settling.+ page.url = redirected_to+ return response++ page.goto = AsyncMock(side_effect=goto)await browser_state.navigate_to_url(page=page, url=url)assert browser_state.last_navigation_status == http_status+ assert browser_state.last_navigation_url == url@pytest.mark.asynciodiff --git a/tests/unit/test_taskv3_engine.py b/tests/unit/test_taskv3_engine.pyindex 4171ac6b9..3362a8a46 100644--- a/tests/unit/test_taskv3_engine.py+++ b/tests/unit/test_taskv3_engine.py@@ -9,6 +9,7 @@ from __future__ import annotationsimport asyncioimport jsonfrom pathlib import Path+from types import SimpleNamespacefrom typing import Anyfrom unittest.mock import AsyncMock@@ -37,6 +38,7 @@ from skyvern.forge.taskv3.engine import (from skyvern.forge.taskv3.llm_call_params import reasoning_effort_with_summaryfrom skyvern.forge.taskv3.loop import (CODE_TOOL_NAME,+ NAV_DEAD_END_GUARD,LoopOutcome,SemanticCommitStats,ToolResult,@@ -1420,6 +1422,34 @@ def test_caller_level_bridge_check_denies_openai_provider_with_custom_api_base()assert LLMCaller.uses_openai_responses_bridge(azure_self) is True+async def test_terminal_log_carries_the_guard_class_that_ended_the_run() -> None:+ # The class a guard verdict used to prefix onto the customer-facing reason lives here now, one row+ # per run (SKY-16271): a dashboard counting how often a policy ends a run reads this field, so it+ # has to survive the trip out of the loop.+ with capture_logs() as logs:+ outcome = await run_task_v3_agent_loop(+ page_provider=_fixed_page_provider(_FakePage()),+ llm_caller=_ScriptedCaller([[("finish", {"status": "completed", "reason": "never runs"})]]),+ goal="x",+ initial_navigation_status=404,+ )+ terminal = [e for e in logs if e.get("event") == "taskv3 engine loop finished"]+ assert outcome.status == "terminated"+ assert terminal[0]["status"] == "terminated"+ assert terminal[0]["guard"] == NAV_DEAD_END_GUARD++ # A model-authored verdict carries no guard, so the field partitions cleanly.+ with capture_logs() as logs:+ await run_task_v3_agent_loop(+ page_provider=_fixed_page_provider(_FakePage()),+ llm_caller=_ScriptedCaller([[("finish", {"status": "completed", "reason": "ok"})]]),+ goal="x",+ )+ terminal = [e for e in logs if e.get("event") == "taskv3 engine loop finished"]+ assert terminal[0]["guard"] is None++@pytest.mark.asyncioasync def test_terminal_log_carries_duration_and_block_type() -> None:# The v1-vs-v3 wall-time dashboard reads this log line; it needs the loop's own wall-clock and@@ -1896,3 +1926,100 @@ async def test_engine_repairs_a_blank_page_when_the_loop_ends_without_another_toassert outcome.status != "completed" # ran out of turns rather than finishingassert restored == [(page, before)]assert page.url == before+++def test_caller_level_bridge_check_denies_a_router_config_with_a_non_azure_api_base() -> None:+ # The single-config check reads api_base off the config; a router config carries it per+ # deployment instead, so a router pointed at OpenRouter used to slip past and report that it+ # bridges. It does not, and the dict reasoning_effort that verdict unlocks 400s there.+ from skyvern.forge.sdk.api.llm.api_handler_factory import LLMCaller+ from skyvern.schemas.llm import LLMRouterConfig, LLMRouterModelConfig++ def _router(+ model: str,+ api_base: str | None,+ fallback_model: str | None = None,+ fallback_api_base: str | None = None,+ ) -> LLMRouterConfig:+ return LLMRouterConfig(+ model_name="group-flex-fallback-router",+ required_env_vars=[],+ supports_vision=True,+ add_assistant_prefix=False,+ model_list=[+ LLMRouterModelConfig(+ model_name="group-flex",+ litellm_params={"model": model, "api_base": api_base, "service_tier": "flex"},+ ),+ LLMRouterModelConfig(+ model_name="group-fallback",+ litellm_params={+ "model": fallback_model if fallback_model is not None else model,+ "api_base": fallback_api_base if fallback_model is not None else api_base,+ },+ ),+ ],+ main_model_group="group-flex",+ fallback_model_group="group-fallback",+ )++ def _verdict(config: LLMRouterConfig) -> bool:+ return LLMCaller.uses_openai_responses_bridge(+ SimpleNamespace(openai_client=None, _custom_openrouter=False, llm_config=config, original_llm_key="K")+ )++ assert _verdict(_router("openrouter/openai/gpt-5.6-luna", "https://openrouter.ai/api/v1")) is False+ # the two shapes that must keep bridging: real OpenAI needs no api_base, and Azure serves it+ assert _verdict(_router("gpt-5.6-luna", None)) is True+ assert _verdict(_router("azure/gpt-5.6-luna", "https://example.openai.azure.com")) is True++ # A MIXED router: one deployment can serve the bridge and one cannot. This is what separates+ # `any` from `all` — the router picks the deployment, the caller cannot, so one leg that+ # would reject the dict form has to deny the whole config. Without this case, swapping the+ # guard to `all` leaves the test green.+ assert (+ _verdict(+ _router(+ "gpt-5.6-luna",+ None,+ fallback_model="openrouter/openai/gpt-5.6-luna",+ fallback_api_base="https://openrouter.ai/api/v1",+ )+ )+ is False+ )+++def test_dispatchable_deployments_covers_every_fallback_group_shape() -> None:+ # The bridge guard judges a router by the deployments it can actually serve a call from.+ # No config in the repo today has a deployment outside its groups, or a list-valued fallback,+ # so these branches are only reachable from here — and a guard that silently widens or+ # narrows its own input would still look correct on every existing config.+ from skyvern.forge.sdk.api.llm.api_handler_factory import _dispatchable_deployments+ from skyvern.schemas.llm import LLMRouterConfig, LLMRouterModelConfig++ def _leg(name: str) -> LLMRouterModelConfig:+ return LLMRouterModelConfig(model_name=name, litellm_params={"model": "m"})++ def _config(fallback: str | list[str] | None, names: list[str]) -> LLMRouterConfig:+ return LLMRouterConfig(+ model_name="router",+ required_env_vars=[],+ supports_vision=True,+ add_assistant_prefix=False,+ model_list=[_leg(name) for name in names],+ main_model_group="main",+ fallback_model_group=fallback,+ )++ def _names(fallback: str | list[str] | None, names: list[str]) -> set[str]:+ return {d.model_name for d in _dispatchable_deployments(_config(fallback, names))}++ # a deployment in neither group is unreachable and must be excluded+ assert _names("fb", ["main", "fb", "retired"]) == {"main", "fb"}+ # every fallback shape the type allows+ assert _names(None, ["main", "fb"]) == {"main"}+ assert _names([], ["main", "fb"]) == {"main"}+ assert _names(["fb1", "fb2"], ["main", "fb1", "fb2", "other"]) == {"main", "fb1", "fb2"}+ # the main group is never dropped+ assert "main" in _names(["fb1"], ["main", "fb1"])diff --git a/tests/unit/test_taskv3_loop.py b/tests/unit/test_taskv3_loop.pyindex a636df32a..d05a12597 100644--- a/tests/unit/test_taskv3_loop.py+++ b/tests/unit/test_taskv3_loop.py@@ -13,12 +13,13 @@ import copyimport hashlibimport jsonimport random+import reimport subprocessimport sysimport timefrom datetime import UTC, datetimefrom types import SimpleNamespace-from typing import Any+from typing import Any, Collectionimport pytestfrom structlog.testing import capture_logs@@ -30,34 +31,37 @@ from skyvern.forge.sdk.core.skyvern_context import SkyvernContextfrom skyvern.forge.taskv3 import loop as loop_modulefrom skyvern.forge.taskv3.auth_tools import _COMPLETION_BLOCKED, VerificationFailure, VerificationStatefrom skyvern.forge.taskv3.engine import MAX_TOKENS_CEILING, MAX_TOKENS_PER_ACTION_STEP, taskv3_runaway_backstops+from skyvern.forge.taskv3.handoff_redaction import ELIDED_URL_PATH, caller_known_published_urlsfrom skyvern.forge.taskv3.loop import (ACTION_BUDGET_EXTENDED_EVENT,ACTION_BUDGET_EXTENSION_MAX_FACTOR,ACTION_BUDGET_EXTENSION_REFUSED_EVENT,+ ACTION_LOOP_GUARD,ACTION_LOOP_NUDGE_AFTER,- ACTION_LOOP_REASON_PREFIX,ACTION_LOOP_TERMINATE_AFTER,ACTION_OUTCOME_DATA_KEY,CODE_TOOL_NAME,FAILURE_EVIDENCE_MIN_TOOL_CALLS,FAILURE_EVIDENCE_MIN_TURNS,FINAL_TURN_RELEASED_EVENT,- NAV_DEAD_END_REASON_PREFIX,+ NAV_DEAD_END_GUARD,NAVIGATION_DEAD_END_STATUSES,NO_TOOL_CALL_NUDGE,- PAGE_REFRESH_EXHAUSTED_REASON_PREFIX,+ PAGE_REFRESH_EXHAUSTED_GUARD,+ PAGE_STATE_STALL_GUARD,PAGE_STATE_STALL_SHADOW_EVENT,PAGE_UNAVAILABLE_ERROR,PERCEPTION_REVISIT_EVENT,PERCEPTION_REVISIT_LOG_AFTER,PERCEPTION_RING,+ PERCEPTION_STALL_GUARD,PERCEPTION_STALL_NUDGE_AFTER,- PERCEPTION_STALL_REASON_PREFIX,PERCEPTION_STALL_SHADOW_EVENT,PERCEPTION_STALL_SUPPRESSED_EVENT,PERCEPTION_STALL_TERMINATE_AFTER,PROGRESS_LEDGER_SHADOW_EVENT,PROGRESS_LEDGER_WINDOW,+ VERDICT_URL_MAX_CHARS,ActivityRecency,LoopOutcome,RoundAction,@@ -421,11 +425,14 @@ async def test_terminal_finish_with_failed_status() -> None:assert outcome.reason == "blocked by captcha"-def _navigate_tool(dead_end_status: int | None = None) -> ToolSpec:+def _navigate_tool(dead_end_status: int | None = None, *, landed_url: str | None = None) -> ToolSpec:async def handler(args: dict[str, Any]) -> ToolResult:data: dict[str, Any] = {"page_state_changed": True}if dead_end_status is not None:data["navigation_dead_end"] = dead_end_status+ # The real tool reports the URL it LANDED on next to the status, which after a redirect is+ # not the one it was asked for; the loop names that page in its verdict.+ data["navigation_dead_end_url"] = landed_url or args.get("url")return ToolResult.ok("navigated", data=data)return ToolSpec(@@ -448,7 +455,7 @@ async def test_navigate_dead_end_terminates_run() -> None:outcome, _ = await _run(script, tools)assert outcome.status == "terminated"- assert outcome.reason.startswith(NAV_DEAD_END_REASON_PREFIX)+ assert outcome.guard == NAV_DEAD_END_GUARD@pytest.mark.asyncio@@ -502,7 +509,7 @@ async def test_initial_navigation_dead_end_terminates_before_loop(status: int) -)assert outcome.status == "terminated"- assert outcome.reason.startswith(NAV_DEAD_END_REASON_PREFIX)+ assert outcome.guard == NAV_DEAD_END_GUARDassert caller.calls == 0 # short-circuited before the first LLM call — deterministic, not model-drivenassert outcome.turns == 0@@ -985,7 +992,7 @@ async def test_guard_terminal_on_the_granted_turn_carries_the_cap_and_staged_extoutcome, _ = await _run(script, tools, max_turns=1, action_nudge_after=None, action_terminate_after=2)assert outcome.status == "terminated"- assert outcome.reason.startswith(ACTION_LOOP_REASON_PREFIX)+ assert outcome.guard == ACTION_LOOP_GUARDassert outcome.cap_trip == "max_turns (1) reached"assert outcome.extracted_output == {"rows": [3]}@@ -3611,7 +3618,7 @@ async def test_perception_stall_terminates_with_bounded_verdict() -> None:tools = [_perception_tool("observe", "url=x (0 elements)"), make_finish_tool()]outcome, caller = await _run(script, tools, max_turns=200, max_tool_calls=500)assert outcome.status == "terminated"- assert "identical" in outcome.reason and "observe" in outcome.reason+ assert outcome.guard == PERCEPTION_STALL_GUARDassert caller.calls <= 20 # bounded well below the 90-observe runaway@@ -3707,19 +3714,6 @@ async def test_perception_stall_counter_resets_and_reclimbs_without_tripping() -assert outcome.status == "completed"-async def test_perception_stall_verdict_reason_carries_facetable_prefix() -> None:- # Telemetry counts policy firings by this prefix; a bounded verdict nobody can query is a- # silent policy.- from skyvern.forge.taskv3.loop import PERCEPTION_STALL_REASON_PREFIX-- script = [[("observe", {})] for _ in range(20)]- tools = [_perception_tool("observe", "url=x frozen"), make_finish_tool()]- outcome, _ = await _run(script, tools, max_turns=200, max_tool_calls=500)- assert outcome.status == "terminated"- assert outcome.reason.startswith(PERCEPTION_STALL_REASON_PREFIX)--def _billable_tool(name: str, sink: list[tuple[str, dict[str, Any]]], *, data: dict[str, Any] | None = None) -> ToolSpec:@@ -3750,14 +3744,13 @@ async def test_action_loop_terminates_with_bounded_verdict_on_resubmit_signature# the same submit button (132 tool calls) while every observe showed the same unchanged banner.# The perception stream varied enough (interleaved actions) that the stall policy never fired.# Repeating the same action against unchanged observed state must end with a bounded verdict.- from skyvern.forge.taskv3.loop import ACTION_LOOP_REASON_PREFIX-clicks: list[tuple[str, dict[str, Any]]] = []tools = [_billable_tool("click", clicks), _perception_tool("observe", _REJECTION_OBSERVE), make_finish_tool()]outcome, caller = await _run(_resubmit_script(12), tools, max_turns=200, max_tool_calls=500)assert outcome.status == "terminated"- assert outcome.reason.startswith(ACTION_LOOP_REASON_PREFIX)- assert "click" in outcome.reason and "#submit" in outcome.reason+ assert outcome.guard == ACTION_LOOP_GUARD+ # The selector belongs to the model's nudge (below), never to the customer-facing verdict.+ assert "#submit" not in outcome.reasonassert len(clicks) < 12 # bounded well below the live 12-resubmit runawayassert caller.calls <= 15@@ -3801,8 +3794,6 @@ async def test_action_loop_catches_varied_probe_evasion() -> None:# get_html probes each return different content, so every probe resets the per-content stall# streak, while the same click keeps repeating. A first-time probe is evidence of nothing (no# baseline), so it must NOT reset the action counter, and the cap must land.- from skyvern.forge.taskv3.loop import ACTION_LOOP_REASON_PREFIX, PERCEPTION_STALL_REASON_PREFIX-probe_contents = [f"<div>fragment {i}</div>" for i in range(12)]script: list[list[tuple[str, dict[str, Any]]]] = []for i in range(12):@@ -3812,8 +3803,7 @@ async def test_action_loop_catches_varied_probe_evasion() -> None:tools = [_billable_tool("click", clicks), _perception_tool("get_html", probe_contents), make_finish_tool()]outcome, caller = await _run(script, tools, max_turns=200, max_tool_calls=500)assert outcome.status == "terminated"- assert outcome.reason.startswith(ACTION_LOOP_REASON_PREFIX)- assert not outcome.reason.startswith(PERCEPTION_STALL_REASON_PREFIX)+ assert outcome.guard == ACTION_LOOP_GUARDassert len(clicks) < 12assert caller.calls <= 15@@ -3837,8 +3827,6 @@ async def test_action_loop_survives_a_page_that_oscillates_between_two_known_sta# than freezes moves on every probe, so `snap.progressed` held every round and wiped the whole# repeat ledger — the action driving the oscillation reset its own counter forever. Returning to# a state this probe has already seen is not progress and must not clear the guard.- from skyvern.forge.taskv3.loop import ACTION_LOOP_REASON_PREFIX-panel = ["url=x text: 'filters panel open'", "url=x text: 'filters panel shut'"]contents = [panel[i % 2] for i in range(16)]script: list[list[tuple[str, dict[str, Any]]]] = []@@ -3850,8 +3838,8 @@ async def test_action_loop_survives_a_page_that_oscillates_between_two_known_staoutcome, _ = await _run(script, tools, max_turns=200, max_tool_calls=500)assert outcome.status == "terminated"- assert outcome.reason.startswith(ACTION_LOOP_REASON_PREFIX)- assert "#apply" in outcome.reason+ assert outcome.guard == ACTION_LOOP_GUARD+ assert "#apply" not in outcome.reason# Bounded well below the 16 the script offers, and below the 11 the production run reached.assert len(clicks) <= 10, len(clicks)@@ -4046,7 +4034,7 @@ async def test_frozen_observe_with_a_ticking_sibling_probe_still_terminates_at_tscript.append([("finish", {"status": "completed", "reason": "done"})])outcome, caller = await _run(script, tools, max_turns=200, max_tool_calls=500)assert outcome.status == "terminated"- assert outcome.reason.startswith(PERCEPTION_STALL_REASON_PREFIX)+ assert outcome.guard == PERCEPTION_STALL_GUARDassert caller.calls <= 2 * PERCEPTION_STALL_TERMINATE_AFTER@@ -4227,7 +4215,7 @@ async def test_period_two_oscillation_is_a_stall_even_though_no_two_calls_match(assert len(would_fire) == 1assert would_fire[0]["snapshots"] >= PERCEPTION_STALL_TERMINATE_AFTERassert outcome.status == "completed"- assert not outcome.reason.startswith(PERCEPTION_STALL_REASON_PREFIX)+ assert outcome.guard != PERCEPTION_STALL_GUARD@pytest.mark.asyncio@@ -4284,7 +4272,7 @@ async def test_stall_verdict_is_preceded_by_exactly_one_warning_even_when_live_sscript.append([("finish", {"status": "failed", "reason": "blocked"})])outcome, _ = await _run(script, [probe, make_finish_tool()], max_turns=200, max_tool_calls=500)assert outcome.status == "terminated"- assert outcome.reason.startswith(PERCEPTION_STALL_REASON_PREFIX)+ assert outcome.guard == PERCEPTION_STALL_GUARDassert len(_stall_warnings(outcome)) == 1@@ -4382,8 +4370,6 @@ async def test_warn_always_precedes_terminate_even_after_single_batch_burst() -># A burst that crosses the terminate threshold before any warning could be delivered must not# be terminated on the spot: the verdict waits until the model has seen the warning and# repeated anyway.- from skyvern.forge.taskv3.loop import ACTION_LOOP_REASON_PREFIX-script = [# The burst must cross the terminate threshold inside ONE turn, or the property under test# (no verdict before a delivered warning) is never exercised.@@ -4395,7 +4381,7 @@ async def test_warn_always_precedes_terminate_even_after_single_batch_burst() ->tools = [_billable_tool("click", clicks), make_finish_tool()]outcome, _ = await _run(script, tools, max_turns=200, max_tool_calls=500)assert outcome.status == "terminated"- assert outcome.reason.startswith(ACTION_LOOP_REASON_PREFIX)+ assert outcome.guard == ACTION_LOOP_GUARD# burst + 1 post-warn-queue + 1 post-warn-deliveryassert len(clicks) == ACTION_LOOP_TERMINATE_AFTER + 2warns = [m for m in outcome.messages if m.get("role") == "user" and "#submit" in str(m.get("content"))]@@ -4407,15 +4393,13 @@ async def test_warn_always_precedes_terminate_even_after_single_batch_burst() ->async def test_action_loop_counts_errored_attempts() -> None:# A submit whose click errors on every attempt burns budget exactly like one that returns ok —# and a dispatched error already consumes the action-step budget, so the guard counts it too.- from skyvern.forge.taskv3.loop import ACTION_LOOP_REASON_PREFIX-clicks: list[tuple[str, dict[str, Any]]] = []click = _recording_tool("click", clicks, raises=True)click.billable = Truescript = [[("click", {"selector": "#dead"})] for _ in range(ACTION_LOOP_TERMINATE_AFTER + 4)]outcome, _ = await _run(script, [click, make_finish_tool()], max_turns=200, max_tool_calls=500)assert outcome.status == "terminated"- assert outcome.reason.startswith(ACTION_LOOP_REASON_PREFIX)+ assert outcome.guard == ACTION_LOOP_GUARDassert len(clicks) == ACTION_LOOP_TERMINATE_AFTER@@ -4512,7 +4496,7 @@ async def test_a_navigation_that_answered_an_http_error_is_recorded_as_a_failed_assert [action.outcome for round_actions in rounds for action in round_actions] == [nav_outcome]# The dead-end verdict still ends the run (the row explains it; it does not replace it), and the# model still read the tool's own ok result.- assert outcome.status == "terminated" and outcome.reason.startswith(NAV_DEAD_END_REASON_PREFIX)+ assert outcome.status == "terminated" and outcome.guard == NAV_DEAD_END_GUARDassert [m["content"] for m in outcome.messages if m.get("name") == "navigate"] == ["navigated"]@@ -5921,7 +5905,7 @@ async def test_stall_verdict_on_a_live_jump_past_both_thresholds_still_carries_oscript.append([("finish", {"status": "failed", "reason": "blocked"})])outcome, _ = await _run(script, [probe, make_finish_tool()], max_turns=200, max_tool_calls=500)assert outcome.status == "terminated"- assert outcome.reason.startswith(PERCEPTION_STALL_REASON_PREFIX)+ assert outcome.guard == PERCEPTION_STALL_GUARDassert len(_stall_warnings(outcome)) == 1@@ -6344,7 +6328,7 @@ async def test_marker_churn_on_a_frozen_page_still_trips_the_stall_guard_throughtools = [_perception_tool("observe", contents), make_finish_tool()]outcome, _ = await _run(script, tools, max_turns=200, max_tool_calls=500)assert outcome.status == "terminated"- assert outcome.reason.startswith(PERCEPTION_STALL_REASON_PREFIX)+ assert outcome.guard == PERCEPTION_STALL_GUARD@pytest.mark.asyncio@@ -6358,7 +6342,7 @@ async def test_marker_churn_on_a_frozen_page_still_trips_the_stall_guard_throughtools = [_perception_tool("get_html", contents), make_finish_tool()]outcome, _ = await _run(script, tools, max_turns=200, max_tool_calls=500)assert outcome.status == "terminated"- assert outcome.reason.startswith(PERCEPTION_STALL_REASON_PREFIX)+ assert outcome.guard == PERCEPTION_STALL_GUARD@pytest.mark.asyncio@@ -6371,7 +6355,7 @@ async def test_semantic_change_under_marker_churn_still_reads_as_progress() -> Ntools = [_perception_tool("observe", contents), make_finish_tool()]outcome, _ = await _run(script, tools, max_turns=200, max_tool_calls=500)assert outcome.status == "completed"- assert not outcome.reason.startswith(PERCEPTION_STALL_REASON_PREFIX)+ assert outcome.guard != PERCEPTION_STALL_GUARDdef test_canonicalization_normalizes_the_ref_observe_addresses_by() -> None:@@ -6439,7 +6423,7 @@ async def test_a_marker_cut_open_by_the_get_html_truncation_does_not_leak_churn(tools = [_perception_tool("get_html", contents), make_finish_tool()]outcome, _ = await _run(script, tools, max_turns=200, max_tool_calls=500)assert outcome.status == "terminated"- assert outcome.reason.startswith(PERCEPTION_STALL_REASON_PREFIX)+ assert outcome.guard == PERCEPTION_STALL_GUARDdef test_canonicalization_covers_a_marker_fragment_left_open_at_the_tail() -> None:@@ -6786,8 +6770,8 @@ async def test_progress_ledger_shadow_fires_on_varied_action_zero_net_progress()assert fires[0]["actions"] >= PROGRESS_LEDGER_WINDOWassert fires[0]["invalid_fields"] == 3assert outcome.status == "completed"- assert not outcome.reason.startswith(PERCEPTION_STALL_REASON_PREFIX)- assert not outcome.reason.startswith(ACTION_LOOP_REASON_PREFIX)+ assert outcome.guard != PERCEPTION_STALL_GUARD+ assert outcome.guard != ACTION_LOOP_GUARD@pytest.mark.asyncio@@ -6946,8 +6930,8 @@ async def test_progress_ledger_silent_on_url_stable_spa_advance() -> None:outcome, _ = await _run(script, tools, max_turns=200, max_tool_calls=500)assert outcome.status == "completed"assert not [e for e in logs if e.get("event") == PROGRESS_LEDGER_SHADOW_EVENT]- assert not outcome.reason.startswith(PERCEPTION_STALL_REASON_PREFIX)- assert not outcome.reason.startswith(ACTION_LOOP_REASON_PREFIX)+ assert outcome.guard != PERCEPTION_STALL_GUARD+ assert outcome.guard != ACTION_LOOP_GUARD@pytest.mark.asyncio@@ -7570,7 +7554,7 @@ async def test_refresh_signal_reload_failure_keeps_the_guards_and_tells_the_mode# One attempt allowed: the failed reload voids the batch and re-arms; the re-armed signal then# exhausts the cap on the next turn's first call and the run ends there rather than acting stale.assert outcome.status == "terminated"- assert outcome.reason.startswith(PAGE_REFRESH_EXHAUSTED_REASON_PREFIX)+ assert outcome.guard == PAGE_REFRESH_EXHAUSTED_GUARDassert type_calls == []assert watch.selector == "#submit"assert ctx.refresh_working_page is False@@ -7788,7 +7772,7 @@ async def test_refresh_cycles_are_capped() -> None:skyvern_context.reset()assert outcome.status == "terminated"- assert outcome.reason.startswith(PAGE_REFRESH_EXHAUSTED_REASON_PREFIX)+ assert outcome.guard == PAGE_REFRESH_EXHAUSTED_GUARDassert len(click_calls) == 3assert len(reload_calls) == 2assert type_calls == []@@ -8149,7 +8133,7 @@ async def test_model_only_perception_stall_still_terminates_at_the_configured_thoutcome, _ = await _run(script, tools, stall_terminate_after=4, max_turns=30)assert outcome.status == "terminated"- assert outcome.reason.startswith(PERCEPTION_STALL_REASON_PREFIX)+ assert outcome.guard == PERCEPTION_STALL_GUARDassert len(clicks) < 10@@ -8167,7 +8151,7 @@ async def test_model_observe_perception_stall_still_terminates_the_same_digest_soutcome, caller = await _run(script, tools, max_turns=30, max_tool_calls=200)assert outcome.status == "terminated"- assert outcome.reason.startswith(PERCEPTION_STALL_REASON_PREFIX)+ assert outcome.guard == PERCEPTION_STALL_GUARDassert len(clicks) < 20 # bounded well below the full scriptassert caller.calls <= 20@@ -8978,8 +8962,6 @@ async def test_a_cycling_page_still_accumulates_the_action_repeat_count() -> Non# (making the clear unconditional) leaves this test's cycle looking like progress every round,# the click count never reaches the threshold, and the run dies of budget instead -- so the two# assertions below both flip.- from skyvern.forge.taskv3.loop import ACTION_LOOP_REASON_PREFIX-rounds = 40# Two states, alternating: every probe differs from the one before it, and every probe after the# first two is a return to a state already in this probe's ring.@@ -8993,7 +8975,7 @@ async def test_a_cycling_page_still_accumulates_the_action_repeat_count() -> Nonoutcome, _ = await _run(script, tools, max_turns=200, max_tool_calls=500)- assert outcome.reason is not None and outcome.reason.startswith(ACTION_LOOP_REASON_PREFIX)+ assert outcome.guard == ACTION_LOOP_GUARDassert len(clicks) < rounds # the cap landed rather than the run burning its whole budget@@ -9028,8 +9010,6 @@ async def test_the_action_loop_verdict_never_lands_before_its_warning_was_delive# nudge threshold and the terminate threshold inside the same batch has been warned zero times.# Terminating there would deliver a verdict the model never had a chance to act on, which is the# whole reason the verdict is gated on the delivered-warning set rather than on the counter.- from skyvern.forge.taskv3.loop import ACTION_LOOP_REASON_PREFIX-nudge_after, terminate_after = 4, 6script = [[("click", {"selector": "#next"})] * 3, # turn 0: counter reaches 3, below the nudge@@ -9046,7 +9026,7 @@ async def test_the_action_loop_verdict_never_lands_before_its_warning_was_delivescript, tools, max_turns=10, action_nudge_after=nudge_after, action_terminate_after=terminate_after)- assert outcome.reason is None or not outcome.reason.startswith(ACTION_LOOP_REASON_PREFIX)+ assert outcome.guard != ACTION_LOOP_GUARDassert len(clicks) == 8@@ -10214,8 +10194,6 @@ async def test_rendered_text_never_folds_a_head_however_far_into_the_page_it_sta## Asserted through the guard rather than by calling the canonicalizer, because the canonicalizer# defaults to not folding — a direct call would pass whatever the loop decides and prove nothing.- from skyvern.forge.taskv3.loop import PERCEPTION_STALL_REASON_PREFIX, PERCEPTION_STALL_TERMINATE_AFTER-reads = PERCEPTION_STALL_TERMINATE_AFTER + 3async def handler(args: dict[str, Any]) -> ToolResult:@@ -10238,7 +10216,7 @@ async def test_rendered_text_never_folds_a_head_however_far_into_the_page_it_stascript.append([("finish", {"status": "completed", "reason": "ok"})])outcome, _ = await _run(script, [spec, make_finish_tool()], max_turns=reads + 5, max_tool_calls=reads + 5)- assert not (outcome.reason or "").startswith(PERCEPTION_STALL_REASON_PREFIX), outcome.reason+ assert outcome.guard != PERCEPTION_STALL_GUARD, outcome.reasonassert outcome.status == "completed", (outcome.status, outcome.reason)@@ -10273,8 +10251,6 @@ async def test_page_authored_notice_shaped_text_never_folds_into_a_frozen_verdic## Asserted through the guard, not against the canonicalizer: a direct call passes whatever the# loop decides to hand it and would go green under the broken version.- from skyvern.forge.taskv3.loop import PERCEPTION_STALL_REASON_PREFIX, PERCEPTION_STALL_TERMINATE_AFTER-reads = PERCEPTION_STALL_TERMINATE_AFTER + 3async def handler(_args: dict[str, Any]) -> ToolResult:@@ -10292,7 +10268,7 @@ async def test_page_authored_notice_shaped_text_never_folds_into_a_frozen_verdicscript.append([("finish", {"status": "completed", "reason": "ok"})])outcome, _ = await _run(script, [spec, make_finish_tool()], max_turns=reads + 5, max_tool_calls=reads + 5)- assert not (outcome.reason or "").startswith(PERCEPTION_STALL_REASON_PREFIX), outcome.reason+ assert outcome.guard != PERCEPTION_STALL_GUARD, outcome.reasonassert outcome.status == "completed", (outcome.status, outcome.reason)@@ -10352,3 +10328,507 @@ async def test_refused_fill_call_stops_the_rest_of_its_batch(fill_tool: str) ->assert outcome.status == "failed", outcome.reasonbatch_results = [m["content"].split(":")[0] for m in caller.message_history if m.get("role") == "tool"][:3]assert batch_results == ["refused", "skipped", "skipped"]+++# --- SKY-16594: the credential re-submit guard ---------------------------------++_PASSWORD = "placeholder_TlK9_password"+_USERNAME = "placeholder_TlK9_username"+++async def test_a_credential_runs_out_of_budget_even_from_a_new_field() -> None:+ # The live three-submit signature: the page re-renders after each rejection, so every retry+ # enters the SAME credential at a DIFFERENT ref. A field-keyed rule misses all of them.+ # The budget lets the first retry through and stops the runaway at the third submission.+ calls: list[tuple[str, dict[str, Any]]] = []+ tools = [_billable_tool("type", calls), _billable_tool("click", calls), make_finish_tool()]+ script = [+ [("type", {"selector": "ref=1", "text": _USERNAME})],+ [("click", {"selector": "ref=2"})],+ [("type", {"selector": "ref=8", "text": _PASSWORD})],+ [("click", {"selector": "ref=10"})],+ [("type", {"selector": "ref=15", "text": _PASSWORD})],+ [("click", {"selector": "ref=17"})],+ [("type", {"selector": "ref=22", "text": _PASSWORD})],+ [("click", {"selector": "ref=24"})],+ [("finish", {"status": "failed", "reason": "the site rejected the credential"})],+ ]+ with capture_logs() as logs:+ outcome, caller = await _run(script, tools)++ assert [args.get("text") for name, args in calls if name == "type"] == [_USERNAME, _PASSWORD, _PASSWORD]+ assert [e["tool"] for e in logs if e["event"] == loop_module.CREDENTIAL_RESUBMIT_REFUSED_EVENT] == ["type"]+ refusal = next(+ m+ for m in caller.message_history+ if m.get("role") == "tool" and m["name"] == "type" and m["content"].startswith("refused")+ )+ # The model must be told the rule and given an exit, or it thrashes against the guard for the+ # rest of its budget -- the failure mode the guard exists to remove. The promise is "not+ # ENTERED again", which is what an entry-anchored guard can actually keep.+ assert "will not be entered again" in refusal["content"]+ assert "Finish with a status" in refusal["content"]+ assert outcome.status == "failed", outcome.reason+++async def test_a_credential_carried_in_a_list_argument_still_spends_its_budget() -> None:+ # `select_option` carries its payload in the `values`/`labels` ARRAYS, so a scan of only the+ # top-level string arguments would let a credential through a list and never refuse it.+ calls: list[tuple[str, dict[str, Any]]] = []+ tools = [_billable_tool("select_option", calls), _billable_tool("click", calls), make_finish_tool()]+ script = [+ [("select_option", {"selector": "ref=1", "values": [_PASSWORD]})],+ [("click", {"selector": "ref=2"})],+ [("select_option", {"selector": "ref=3", "values": [_PASSWORD]})],+ [("click", {"selector": "ref=4"})],+ [("select_option", {"selector": "ref=5", "values": [_PASSWORD]})],+ [("finish", {"status": "failed", "reason": "the site rejected the credential"})],+ ]+ with capture_logs() as logs:+ outcome, _ = await _run(script, tools)++ assert [args.get("values") for name, args in calls if name == "select_option"] == [[_PASSWORD], [_PASSWORD]]+ assert [e["tool"] for e in logs if e["event"] == loop_module.CREDENTIAL_RESUBMIT_REFUSED_EVENT] == ["select_option"]+ assert outcome.status == "failed", outcome.reason+++async def test_a_credential_retyped_with_no_submit_between_is_still_allowed() -> None:+ # Confirm-password, and re-typing a field the page cleared before anything was submitted. The+ # guard is anchored on a dispatched submit precisely so these keep working.+ calls: list[tuple[str, dict[str, Any]]] = []+ tools = [_billable_tool("type", calls), _billable_tool("click", calls), make_finish_tool()]+ script = [+ [("type", {"selector": "ref=8", "text": _PASSWORD})],+ [("type", {"selector": "ref=9", "text": _PASSWORD})],+ [("click", {"selector": "ref=10"})],+ [("finish", {"status": "completed", "reason": "signed in"})],+ ]+ with capture_logs() as logs:+ outcome, _ = await _run(script, tools)++ assert [name for name, _ in calls] == ["type", "type", "click"]+ assert [e for e in logs if e["event"] == loop_module.CREDENTIAL_RESUBMIT_REFUSED_EVENT] == []+ assert outcome.status == "completed", outcome.reason+++async def test_an_entry_that_failed_does_not_arm_the_guard() -> None:+ # The live timeline types into a stale ref and errors, which put nothing in the field. Counting+ # that as an entry would refuse the recovery that follows it.+ calls: list[tuple[str, dict[str, Any]]] = []++ async def type_handler(args: dict[str, Any]) -> ToolResult:+ calls.append(("type", args))+ if args["selector"] == "ref=stale":+ return ToolResult.error("ref=stale is no longer on the page; re-observe")+ return ToolResult.ok("typed")++ tools = [+ ToolSpec(name="type", description="type", parameters={}, handler=type_handler, billable=True),+ _billable_tool("click", calls),+ make_finish_tool(),+ ]+ script = [+ [("type", {"selector": "ref=stale", "text": _PASSWORD})],+ [("click", {"selector": "ref=2"})],+ [("type", {"selector": "ref=8", "text": _PASSWORD})],+ [("click", {"selector": "ref=10"})],+ [("finish", {"status": "completed", "reason": "signed in"})],+ ]+ with capture_logs() as logs:+ outcome, _ = await _run(script, tools)++ assert [name for name, _ in calls] == ["type", "click", "type", "click"]+ assert [e for e in logs if e["event"] == loop_module.CREDENTIAL_RESUBMIT_REFUSED_EVENT] == []+ assert outcome.status == "completed", outcome.reason+++async def test_ordinary_text_is_resubmitted_freely() -> None:+ # The guard's floor: it engages only on a credential placeholder. Widening what counts as one+ # would refuse re-entry on every ordinary form the fleet fills.+ calls: list[tuple[str, dict[str, Any]]] = []+ tools = [_billable_tool("type", calls), _billable_tool("click", calls), make_finish_tool()]+ script = [+ [("type", {"selector": "ref=1", "text": "Jane Doe"})],+ [("click", {"selector": "ref=2"})],+ [("type", {"selector": "ref=3", "text": "Jane Doe"})],+ [("click", {"selector": "ref=4"})],+ [("finish", {"status": "completed", "reason": "submitted"})],+ ]+ with capture_logs() as logs:+ outcome, _ = await _run(script, tools)++ assert [name for name, _ in calls] == ["type", "click", "type", "click"]+ assert [e for e in logs if e["event"] == loop_module.CREDENTIAL_RESUBMIT_REFUSED_EVENT] == []+ assert outcome.status == "completed", outcome.reason+++# --- SKY-16271: what a guard verdict says to the customer ----------------------+# `LoopOutcome.reason` is persisted verbatim as `task.failure_reason`, a field on the customer webhook+# and the run view (`test_execute_task_v3_leaves_failure_reason_byte_identical_when_redaction_is_off`+# pins that it travels unchanged), so each guard owes a sentence a customer can act on — with the+# machine class it used to prefix onto that sentence carried on the outcome instead.++_INTERNAL_VERDICT_TOKENS = (+ PERCEPTION_STALL_GUARD,+ ACTION_LOOP_GUARD,+ NAV_DEAD_END_GUARD,+ PAGE_REFRESH_EXHAUSTED_GUARD,+ PAGE_STATE_STALL_GUARD,+ "data-tv3",+ "get_html",+ "select_option",+ "select_combobox",+ "file_upload",+ "press_key",+ "solve_captcha",+)+_ELIDED_TEST_URL = "https://mail.example.test" + ELIDED_URL_PATH+++def _assert_customer_facing_verdict(+ outcome: Any, *, guard: str, names: str | None = None, hides: tuple[str, ...] = ()+) -> None:+ """The verdict contract: the class on the outcome, and a sentence carrying no facet prefix, no+ internal vocabulary, and nothing the customer cannot locate on their own page."""+ assert outcome.status == "terminated"+ assert outcome.guard == guard+ reason = outcome.reason+ # A machine prefix is a first token ending in a colon ("perception_stall: ..."); a colon later in+ # the sentence is ordinary punctuation.+ assert not reason.split(" ", 1)[0].endswith(":"), reason+ assert reason[0].isupper() and reason.endswith("."), reason+ for token in _INTERNAL_VERDICT_TOKENS + hides:+ assert token not in reason, (token, reason)+ if names is not None:+ assert names in reason, (names, reason)+++async def test_perception_stall_verdict_is_a_customer_sentence_about_the_page() -> None:+ script = [[("observe", {})] for _ in range(20)]+ tools = [_perception_tool("observe", "url=x frozen"), make_finish_tool()]+ outcome, _ = await _run(script, tools, max_turns=200, max_tool_calls=500)++ _assert_customer_facing_verdict(outcome, guard=PERCEPTION_STALL_GUARD, names="page", hides=("observe",))+ # The streak length is the threshold by construction, so printing it publishes the threshold —+ # the standard `_budget_exhausted_reason` already holds for the cap literal.+ assert not any(ch.isdigit() for ch in outcome.reason), outcome.reason+++async def test_action_loop_verdict_names_a_control_the_customer_can_find() -> None:+ # The audited leak: the verdict named `[data-tv3="t223"]` — v3 stamps that marker on the page+ # itself, so it matches nothing in the customer's markup and points at an element they cannot+ # locate. The control's own accessible name is what they can.+ clicks: list[tuple[str, dict[str, Any]]] = []+ tools = [+ _billable_tool("click", clicks, data={"target_label": "Continue", "target_kind": "button"}),+ _perception_tool("observe", _REJECTION_OBSERVE),+ make_finish_tool(),+ ]+ skyvern_context.set(SkyvernContext(task_id="tsk_action_loop_target"))+ outcome, _ = await _run(+ _resubmit_script(12), tools, max_turns=200, max_tool_calls=500, label_secret_values=lambda: ()+ )++ _assert_customer_facing_verdict(+ outcome, guard=ACTION_LOOP_GUARD, names='the "Continue" button', hides=("#submit", "[data-tv3")+ )+++async def test_action_loop_verdict_falls_to_the_kind_floor_with_no_run_context() -> None:+ # The run's codes are minted into the context as the run goes, so half the set a page-supplied name+ # is checked against lives there. No context is not "check the other half" -- it is the same+ # unchecked state as a missing resolver, and it floors the verdict the same way.+ clicks: list[tuple[str, dict[str, Any]]] = []+ tools = [+ _billable_tool("click", clicks, data={"target_label": "Continue", "target_kind": "button"}),+ _perception_tool("observe", _REJECTION_OBSERVE),+ make_finish_tool(),+ ]+ outcome, _ = await _run(+ _resubmit_script(12), tools, max_turns=200, max_tool_calls=500, label_secret_values=lambda: ()+ )++ _assert_customer_facing_verdict(outcome, guard=ACTION_LOOP_GUARD, names="a button", hides=("Continue",))+ assert not any(ch.isdigit() for ch in outcome.reason), outcome.reason+ # Model-facing text is untouched: the nudge still names the selector the model addressed.+ nudges = [m for m in outcome.messages if m.get("role") == "user" and "#submit" in str(m.get("content"))]+ assert len(nudges) == 1+++async def test_action_loop_verdict_names_no_control_when_the_call_reported_none() -> None:+ # A repeated call that reported no element name (a failed dispatch reports none) must still read as a+ # sentence, and must not fall back to the selector it was given.+ clicks: list[tuple[str, dict[str, Any]]] = []+ tools = [_billable_tool("click", clicks), _perception_tool("observe", _REJECTION_OBSERVE), make_finish_tool()]+ outcome, _ = await _run(_resubmit_script(12), tools, max_turns=200, max_tool_calls=500)++ _assert_customer_facing_verdict(outcome, guard=ACTION_LOOP_GUARD, hides=("#submit",))+++def _dead_end_run(landed: str, *, status: int = 404, **kwargs: Any) -> Any:+ """One `navigate` that lands dead on ``landed``, driven through the real loop to its verdict."""+ tools = [_navigate_tool(dead_end_status=status, landed_url=landed), make_finish_tool()]+ script = [+ [("navigate", {"url": "https://mail.example.test/r"})],+ [("finish", {"status": "completed", "reason": "should never run"})],+ ]+ return _run(script, tools, **kwargs)+++def _verdict_place(reason: str) -> str | None:+ """The one URL a dead-end verdict names, read back out of the sentence exactly as published."""+ match = re.search(r"\((https?://[^)\s]+)\)", reason)+ return match.group(1) if match else None+++async def test_navigate_dead_end_verdict_names_the_dead_page_and_its_status() -> None:+ # A URL literal in the caller's own goal text is a URL the caller already has, so the page it+ # names is published whole.+ dead = "https://jobs.example.test/acme/closed"+ outcome, _ = await _dead_end_run(+ dead, caller_known_urls=caller_known_published_urls(None, f"Apply for the role at {dead}.")+ )++ _assert_customer_facing_verdict(outcome, guard=NAV_DEAD_END_GUARD, names=dead)+ # The status stays: it is the fact that says the page is gone rather than merely wrong, and+ # `classify_from_failure_reason` derives NAVIGATION_FAILURE from it.+ assert "404" in outcome.reason+++async def test_navigate_dead_end_verdict_drops_a_caller_url_too_long_to_print() -> None:+ # Cutting a URL down would name a page that does not exist, so one too long to print is dropped+ # whole — the verdict names no place rather than a truncated one.+ # Deliberately not token-shaped ("z" is not hex, and one case with no digits is not a signing+ # blob), so it is the print cap that drops this URL and not the segment scrub shortening it first.+ long_url = "https://files.example.test/download/" + "z" * VERDICT_URL_MAX_CHARS+ outcome, _ = await _dead_end_run(long_url, status=410, caller_known_urls=caller_known_published_urls(long_url))++ _assert_customer_facing_verdict(outcome, guard=NAV_DEAD_END_GUARD, hides=("zzzz",))+ assert _verdict_place(outcome.reason) is None+ assert "410" in outcome.reason+++# Every alphabet, separator and grouping a landed path can be built from. The point is that NONE of+# them is consulted: the property below holds across the whole space because provenance, not shape,+# decides what a verdict publishes.+_PATH_ALPHABETS = (+ "abcdefghijklmnopqrstuvwxyz",+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ",+ "0123456789",+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",+ "abcdef0123456789",+)+_PATH_SEPARATORS = ("", "-", "_", ".", "~")+++def _random_path(rng: random.Random, *, max_chars: int) -> str:+ """Re-rolled until it fits the print cap, so the property measures the invariant rather than the+ length rule that has its own arm above."""+ while True:+ segments = []+ for _ in range(rng.randint(1, 4)):+ alphabet = rng.choice(_PATH_ALPHABETS)+ groups = ["".join(rng.choices(alphabet, k=rng.randint(5, 12))) for _ in range(rng.randint(1, 6))]+ segments.append(rng.choice(_PATH_SEPARATORS).join(groups))+ path = "/" + "/".join(segments)+ if len(path) <= max_chars:+ return path+++async def test_dead_end_verdict_publishes_a_path_only_for_a_url_the_caller_gave_us() -> None:+ # The invariant, over the class rather than a table of examples: whatever the landed path is made+ # of, the verdict names the host, and names the path only when the landed URL is one the caller+ # supplied — their starting URL, or a URL literal in their own goal text. No shape rule decides+ # it, which is why there is no (N+1)th token shape for the next review to find.+ rng = random.Random(16271)+ host = "https://mail.example.test"+ for index in range(200):+ path = _random_path(rng, max_chars=VERDICT_URL_MAX_CHARS - len(host))+ landed = host + path++ unknown, _ = await _dead_end_run(landed)+ assert _verdict_place(unknown.reason) == host + ELIDED_URL_PATH, path+ assert path not in unknown.reason, path++ # Alternating source: the caller's starting URL on one sample, a URL literal in the caller's+ # own goal prose on the next — both are things the caller already has.+ sources = (landed, None) if index % 2 else (None, f"Start at {landed} and apply.")+ known, _ = await _dead_end_run(landed, caller_known_urls=caller_known_published_urls(*sources))+ place = _verdict_place(known.reason)+ assert place is not None and place.startswith(host) and not place.endswith(ELIDED_URL_PATH), path+ # Per segment the whole space collapses to two outcomes and no third: the caller's own text+ # back, or scrubbed. Nothing the caller did not supply is ever printed.+ assert all(+ published in (original, "***")+ for published, original in zip(place[len(host) :].split("/"), path.split("/"), strict=True)+ ), path+++ ("configured", "landed", "published"),+ [+ ("https://jobs.example.test", "https://jobs.example.test/", "https://jobs.example.test/"),+ ("https://jobs.example.test/acme/", "https://jobs.example.test/acme", "https://jobs.example.test/acme"),+ ("https://jobs.example.test:443/acme", "https://jobs.example.test/acme", "https://jobs.example.test/acme"),+ ("http://jobs.example.test/acme", "http://jobs.example.test:80/acme", "http://jobs.example.test/acme"),+ ],+ ids=["bare_host_vs_root", "trailing_slash", "default_https_port", "default_http_port"],+)+async def test_dead_end_verdict_matches_the_callers_url_across_equivalent_spellings(+ configured: str, landed: str, published: str+) -> None:+ # The caller-known test is a string comparison, so both sides have to reduce to one spelling of the+ # page. Without that the commonest task URL of all -- a bare host, whose path is empty, against the+ # `/` the browser reports landing on -- elides the very URL the caller typed.+ outcome, _ = await _dead_end_run(landed, caller_known_urls=caller_known_published_urls(configured))++ assert _verdict_place(outcome.reason) == published+++async def test_dead_end_verdict_scrubs_a_signing_token_out_of_the_callers_own_url() -> None:+ # Provenance decides whether a path is published at all; shape still decides what inside it is+ # printable. A caller who pasted a magic link into their own goal did not thereby ask us to print+ # its token back on the webhook.+ magic = "https://mail.example.test/verify/" + "9f2c" * 10 + "/continue"+ outcome, _ = await _dead_end_run(magic, caller_known_urls=caller_known_published_urls(magic))++ assert _verdict_place(outcome.reason) == "https://mail.example.test/verify/***/continue"+++async def test_initial_navigation_dead_end_verdict_names_the_starting_page() -> None:+ start = "https://jobs.example.test/acme/expired"+ outcome, caller = await _run(+ [[("finish", {"status": "completed", "reason": "should never run"})]],+ [make_finish_tool()],+ initial_navigation_status=404,+ initial_navigation_url=start,+ caller_known_urls=caller_known_published_urls(start),+ )++ _assert_customer_facing_verdict(outcome, guard=NAV_DEAD_END_GUARD, names=start)+ assert "404" in outcome.reason+ assert caller.calls == 0+++async def test_page_refresh_exhausted_verdict_is_a_customer_sentence_about_the_page() -> None:+ async def reload_page() -> None:+ return None++ clicks: list[tuple[str, dict[str, Any]]] = []+ tools = [_refresh_signaling_click(clicks), make_finish_tool()]+ script: list[list[tuple[str, dict[str, Any]]]] = [+ [("click", {"selector": "#submit"})],+ [("finish", {"status": "completed", "reason": "should never run"})],+ ]+ skyvern_context.set(SkyvernContext(task_id="tsk_refresh_verdict"))+ try:+ outcome, _ = await _run(script, tools, reload_page=reload_page, max_refresh_cycles=0)+ finally:+ skyvern_context.reset()++ _assert_customer_facing_verdict(outcome, guard=PAGE_REFRESH_EXHAUSTED_GUARD, names="page")+ # The reload count is the cap plus one by construction; it belongs on the guard's log line.+ assert not any(ch.isdigit() for ch in outcome.reason), outcome.reason+++async def test_action_loop_verdict_drops_a_control_name_that_touches_a_run_secret() -> None:+ # A page-supplied accessible name can BE a registered run parameter ("Continue as <username>"), and+ # the whole-value redaction downstream of this field cannot save it: the name has already been+ # reshaped (whitespace collapsed, quotes swapped) by the time it is composed, which is exactly why a+ # touched name is DROPPED rather than scrubbed. The registry lives with the caller, so the loop gets+ # it as a resolver read at verdict time; the kind floor that remains carries no page text.+ clicks: list[tuple[str, dict[str, Any]]] = []+ tools = [+ _billable_tool("click", clicks, data={"target_label": "Continue as swordfish", "target_kind": "button"}),+ _perception_tool("observe", _REJECTION_OBSERVE),+ make_finish_tool(),+ ]+ outcome, _ = await _run(+ _resubmit_script(12),+ tools,+ max_turns=200,+ max_tool_calls=500,+ label_secret_values=lambda: {"swordfish"},+ )++ _assert_customer_facing_verdict(outcome, guard=ACTION_LOOP_GUARD, names="a button", hides=("swordfish", "Continue"))+++async def test_action_loop_verdict_drops_a_control_name_carrying_a_short_runtime_code() -> None:+ # A code minted this turn reaches the verdict only through the live, UNFLOORED read of the run's+ # runtime secrets: the resolver's own view drops a numeric value shorter than six characters, and the+ # label's shape filter only rejects runs of four or more digits -- so a three-character code on the+ # page ("Continue as K7x") is the case neither of those covers.+ clicks: list[tuple[str, dict[str, Any]]] = []+ tools = [+ _billable_tool("click", clicks, data={"target_label": "Continue as K7x", "target_kind": "button"}),+ _perception_tool("observe", _REJECTION_OBSERVE),+ make_finish_tool(),+ ]+ skyvern_context.set(SkyvernContext(task_id="tsk_short_code_verdict", runtime_secret_values={"K7x"}))+ try:+ outcome, _ = await _run(+ _resubmit_script(12),+ tools,+ max_turns=200,+ max_tool_calls=500,+ label_secret_values=lambda: set(),+ )+ finally:+ skyvern_context.reset()++ _assert_customer_facing_verdict(outcome, guard=ACTION_LOOP_GUARD, names="a button", hides=("K7x", "Continue"))+++async def test_action_loop_verdict_falls_back_to_the_kind_floor_when_the_secret_read_fails() -> None:+ # An unusable registry costs the run a label rather than publishing an unchecked one: the name is+ # only printable against a set that was actually read.+ def _explode() -> Collection[str]:+ raise RuntimeError("registry unavailable")++ clicks: list[tuple[str, dict[str, Any]]] = []+ tools = [+ _billable_tool("click", clicks, data={"target_label": "Continue as swordfish", "target_kind": "button"}),+ _perception_tool("observe", _REJECTION_OBSERVE),+ make_finish_tool(),+ ]+ outcome, _ = await _run(+ _resubmit_script(12), tools, max_turns=200, max_tool_calls=500, label_secret_values=_explode+ )++ _assert_customer_facing_verdict(outcome, guard=ACTION_LOOP_GUARD, names="a button", hides=("swordfish",))+++async def test_navigate_dead_end_verdict_names_the_place_without_its_credentials() -> None:+ # A 404 can land on a magic link or a signed download, and this sentence is persisted and+ # API-visible. Userinfo, query and fragment are dropped even from a URL the caller supplied+ # themselves: that is where the credential lives, and what the caller gave us is a place.+ landed = "https://reset:[email protected]/reset/step-two?token=abc123def456#frag"+ outcome, _ = await _dead_end_run(landed, status=410, caller_known_urls=caller_known_published_urls(landed))++ _assert_customer_facing_verdict(+ outcome,+ guard=NAV_DEAD_END_GUARD,+ names="https://files.example.test/reset/step-two",+ hides=("reset:pw@", "token=", "abc123def456", "#frag"),+ )diff --git a/tests/unit/test_taskv3_tools.py b/tests/unit/test_taskv3_tools.pyindex a8c8c92f8..eaef3b443 100644--- a/tests/unit/test_taskv3_tools.py+++ b/tests/unit/test_taskv3_tools.py@@ -616,16 +616,24 @@ async def test_navigate_and_wait(monkeypatch: pytest.MonkeyPatch) -> None:assert any(c[0] == "wait_for_selector" for c in page.calls)-async def _navigate_status(monkeypatch: pytest.MonkeyPatch, status: int | None) -> Any:+async def _navigate_status(+ monkeypatch: pytest.MonkeyPatch,+ status: int | None,+ landed_url: str | None = None,+ page_url_after_load: str | None = None,+) -> Any:import skyvern.utils.url_validators as urlvmonkeypatch.setattr(urlv, "validate_fetch_url", lambda url: url)page = _FakePage()async def _goto(url: str, timeout: int | None = None, wait_until: str | None = None) -> Any:- page.url = url+ responded_from = landed_url or url+ # Where the page ends up, which a document rewriting its own address bar moves off the+ # URL the response came back on.+ page.url = page_url_after_load or responded_frompage.calls.append(("goto", {"url": url}))- return None if status is None else SimpleNamespace(status=status)+ return None if status is None else SimpleNamespace(status=status, url=responded_from)page.goto = _goto # type: ignore[assignment]tools = build_browser_tools(_fixed_page_provider(page))@@ -643,6 +651,18 @@ async def test_navigate_flags_dead_end_on_hard_404_or_410(monkeypatch: pytest.Moassert (r.data or {}).get("navigation_dead_end") == status+async def test_navigate_dead_end_reports_the_page_the_status_came_back_on(monkeypatch: pytest.MonkeyPatch) -> None:+ # The loop names this page in the customer-facing verdict (SKY-16271), so it must be where the+ # status came from -- after a redirect, that is not the URL the model asked for, and after a+ # document rewrites its own address bar during load it is not where the page ends up either.+ expired = "https://jobs.example.test/acme/expired"+ rewritten = "https://jobs.example.test/acme/browse"+ r = await _navigate_status(monkeypatch, 404, landed_url=expired, page_url_after_load=rewritten)+ assert (r.data or {}).get("navigation_dead_end") == 404+ assert (r.data or {}).get("navigation_dead_end_url") == expired++@pytest.mark.parametrize("status", [None, 200, 302, 401, 403, 429, 500, 503])@pytest.mark.asyncioasync def test_navigate_does_not_flag_dead_end_for_non_dead_statuses(@@ -654,6 +674,7 @@ async def test_navigate_does_not_flag_dead_end_for_non_dead_statuses(r = await _navigate_status(monkeypatch, status)assert r.status == "ok"assert "navigation_dead_end" not in (r.data or {})+ assert "navigation_dead_end_url" not in (r.data or {})def _reload_guard_tools(monkeypatch: pytest.MonkeyPatch, filled: int) -> tuple[Any, list[Any]]:@@ -1330,6 +1351,367 @@ async def test_type_non_text_input_skips_probe_fast_path() -> None:assert not page.clicked_suggestion+class _DateSegmentFakePage(_TypeaheadFakePage):+ """Extends the typeahead fake with the segmented-date group probe (_DATE_SEGMENT_GROUP_JS,+ identified by its `targetLabel` field) and a per-segment locator.++ By default (no `committed_digits`), a segment's readback reflects what was ACTUALLY typed into it+ via `keyboard.type`/`press_sequentially` while it held focus -- a segment the fill loop never+ focuses or types reads back empty, so a dropped segment is detectable. `committed_digits`, when+ given, overrides that and reports a fixed value per label regardless of what was typed (a page that+ silently drops/rejects specific segments); a label missing from that override reads back empty.+ `blur_clamp` simulates a widget that rewrites one segment's value the moment focus leaves it for+ another segment (the segment's real blur event), independent of what was typed there.+ Everything else (the plain typeahead path a rejected/untriggered call falls back to) is inherited+ unchanged from `_TypeaheadFakePage`."""++ def __init__(+ self,+ *,+ group_probe: dict[str, Any],+ committed_digits: dict[str, str] | None = None,+ broken_segment: str | None = None,+ blur_clamp: tuple[str, str, str] | None = None,+ ) -> None:+ super().__init__(field_type="text", suggestion=None)+ self._group_probe = group_probe+ self._committed_override = committed_digits+ self._broken_segment = broken_segment+ self._blur_clamp = blur_clamp # (label, from_value, to_value)+ self.group_probe_calls = 0+ self.focused_segment: str | None = None+ self.typed_digits: dict[str, str] = {}+ # Ordered (kind, label, ...) log of clear/type calls per segment locator, so a test can assert+ # a segment's clear happened BEFORE its digits were typed, not just that both happened.+ self.log: list[tuple[str, ...]] = []++ async def evaluate(self, js: str, arg: Any = None) -> Any:+ if "targetLabel" in js:+ self.group_probe_calls += 1+ return self._group_probe+ if ".blur()" in js:+ self._blur_current_segment()+ self.focused_segment = None+ return None+ return await super().evaluate(js, arg)++ def _blur_current_segment(self) -> None:+ if self._blur_clamp:+ clamp_label, from_value, to_value = self._blur_clamp+ if self.focused_segment == clamp_label and self.typed_digits.get(clamp_label) == from_value:+ self.typed_digits[clamp_label] = to_value++ def _focus_segment(self, label: str) -> None:+ self._blur_current_segment()+ if label != self._broken_segment:+ self.focused_segment = label++ def locator(self, selector: str) -> Any:+ if 'data-tv3-dateseg="' not in selector:+ return super().locator(selector)+ label = selector.split('"')[1]+ outer = self++ class _FakeSegLocator:+ def __init__(self) -> None:+ self.first = self++ async def scroll_into_view_if_needed(self, timeout: int | None = None) -> None:+ pass++ async def focus(self, timeout: int | None = None) -> None:+ outer._focus_segment(label)++ async def press(self, key: str, timeout: int | None = None) -> None:+ outer.log.append(("press", label, key))++ async def press_sequentially(self, digits: str, delay: int | None = None) -> None:+ if label != outer._broken_segment:+ outer.typed_digits[label] = digits+ outer.log.append(("type", label, digits))++ async def evaluate(self, js: str, timeout: int | None = None) -> str:+ if outer._committed_override is not None:+ return outer._committed_override.get(label) or ""+ return outer.typed_digits.get(label, "")++ return _FakeSegLocator()++ class _KB:+ def __init__(self, outer: _DateSegmentFakePage) -> None:+ self._outer = outer++ async def type(self, digits: str, delay: int | None = None) -> None:+ seg = self._outer.focused_segment+ if seg and seg != self._outer._broken_segment:+ self._outer.typed_digits[seg] = digits+ self._outer.log.append(("type", seg, digits))++ @property+ def keyboard(self) -> _DateSegmentFakePage._KB:+ return _DateSegmentFakePage._KB(self)+++async def test_type_date_fills_all_three_segments_from_one_call(monkeypatch: pytest.MonkeyPatch) -> None:+ # A segmented date input rejects a whole "MM/DD/YYYY" typed into just one segment. Once the target+ # resolves as a confirmed month/day/year spinbutton group, one type() call must fill all three+ # segments and report success.+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(+ group_probe={"ok": True, "reason": None, "targetLabel": "month"},+ committed_digits={"month": "09", "day": "18", "year": "2026"},+ )+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "09/18/2026"})+ assert r.status == "ok"+ assert page.group_probe_calls == 1+++async def test_type_date_group_rejected_by_bijection_falls_back_to_todays_path(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # A missing/duplicate/readonly segment must fail the bijection closed: the call falls through to+ # today's single-field path (which still truncates, but that is the existing, unchanged behavior).+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(group_probe={"ok": False, "reason": "missing", "targetLabel": "month"})+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "09/18/2026"})+ assert r.status == "ok"+ assert ("type", ("#month-segment", "09/18/2026")) in page.calls+ assert page.focused_segment is None+++async def test_type_single_component_never_probes_date_segment_group(monkeypatch: pytest.MonkeyPatch) -> None:+ # The most important safety property: a single component typed into a single segment (e.g. "09"+ # into the month segment) must be completely untouched by the new path -- it never even runs the+ # DOM probe, let alone routes into the group fill.+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(group_probe={"ok": True, "reason": None, "targetLabel": "month"})+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "09"})+ assert r.status == "ok"+ assert page.group_probe_calls == 0+ assert ("type", ("#month-segment", "09")) in page.calls+++async def test_type_date_group_partial_commit_errors_naming_the_segment(monkeypatch: pytest.MonkeyPatch) -> None:+ # A segment that never reads back its component must fail closed: an error naming which segment+ # did not commit, never a false "typed into" success on a partially-filled date.+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(+ group_probe={"ok": True, "reason": None, "targetLabel": "month"},+ committed_digits={"month": "09"},+ )+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "09/18/2026"})+ assert r.status == "error"+ assert "day" in r.content+ assert "NOT filled" in r.content+++async def test_type_date_iso_format_is_parsed_by_structure_not_assumed(monkeypatch: pytest.MonkeyPatch) -> None:+ # An ISO "2026-09-18" (LLMs emit this constantly) must resolve via the 4-digit year's POSITION+ # (leading, so year-month-day), never via a blind month/day/year digit slice -- the previous blind+ # slice read this as month=20, day=26, year=0918 and silently corrupted the date.+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(group_probe={"ok": True, "reason": None, "targetLabel": "month"})+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "2026-09-18"})+ assert r.status == "ok", r.content+ assert page.typed_digits == {"month": "09", "day": "18", "year": "2026"}+++async def test_type_date_single_digit_month_day_zero_pads_correctly(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(group_probe={"ok": True, "reason": None, "targetLabel": "month"})+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "9/8/2026"})+ assert r.status == "ok", r.content+ assert page.typed_digits == {"month": "09", "day": "08", "year": "2026"}+++async def test_type_date_separator_less_run_never_probes_date_segment_group(monkeypatch: pytest.MonkeyPatch) -> None:+ # A separator-less digit run can't be resolved to an arrangement at all (09182026 vs. 20260918),+ # so it never even reaches the DOM probe -- exactly like a month name or a single component.+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(group_probe={"ok": True, "reason": None, "targetLabel": "month"})+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "09182026"})+ assert r.status == "ok", r.content+ assert page.group_probe_calls == 0+ assert ("type", ("#month-segment", "09182026")) in page.calls+++ "text",+ ["13/01/2026", "01/32/2026"],+ ids=["invalid-under-the-default-month-first-reading", "invalid-under-either-reading"],+)+async def test_type_date_out_of_range_after_assignment_still_falls_back(+ text: str, monkeypatch: pytest.MonkeyPatch+) -> None:+ # A shape-matching text DOES now reach the DOM probe -- the probe is what supplies the order that+ # decides whether a leading component over 12 is a bad month or a valid day. With no "order" on+ # the probe result (this fake's default), assignment defaults to month-first, so both of these+ # remain invalid after assignment and fall back to today's single-field path -- probed, not blind.+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(group_probe={"ok": True, "reason": None, "targetLabel": "month"})+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": text})+ assert r.status == "ok", r.content+ assert page.group_probe_calls == 1+ assert ("type", ("#month-segment", text)) in page.calls+++async def test_type_date_ambiguous_value_defaults_month_first_when_widget_order_is_unknown(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # No "order" on the probe result (e.g. an older probe payload) must not crash or guess a flip --+ # it defaults to today's historical month-first reading.+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(group_probe={"ok": True, "reason": None, "targetLabel": "month"})+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "05/09/2026"})+ assert r.status == "ok", r.content+ assert page.typed_digits == {"month": "05", "day": "09", "year": "2026"}+++async def test_type_date_ambiguous_value_reads_day_first_from_the_widgets_own_order(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # The reviewer's exact scenario: both leading components are <= 12, so the text alone cannot+ # disambiguate. A widget whose group scan encounters "day" before "month" must land the FIRST+ # numeric component in the day segment, not always month.+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(+ group_probe={"ok": True, "reason": None, "targetLabel": "month", "order": ["day", "month", "year"]}+ )+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "05/09/2026"})+ assert r.status == "ok", r.content+ assert page.typed_digits == {"month": "09", "day": "05", "year": "2026"}+++async def test_type_date_day_first_widget_accepts_a_leading_component_over_twelve(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # Widened coverage: "18/09/2026" is rejected on a month-first reading (month=18), but is a valid+ # day-first date (day=18, month=09) -- and the widget's own order says it should be read that way.+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(+ group_probe={"ok": True, "reason": None, "targetLabel": "month", "order": ["day", "month", "year"]}+ )+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "18/09/2026"})+ assert r.status == "ok", r.content+ assert page.typed_digits == {"month": "09", "day": "18", "year": "2026"}+++async def test_type_date_group_clears_each_segment_before_typing_its_component(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # A retry or a pre-filled segment already holds digits when focus lands on it; without an explicit+ # clear those keystrokes append rather than replace. Each segment's clear must happen before its+ # digits are typed.+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(+ group_probe={"ok": True, "reason": None, "targetLabel": "month"},+ committed_digits={"month": "09", "day": "18", "year": "2026"},+ )+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "09/18/2026"})+ assert r.status == "ok", r.content+ for label in ("month", "day", "year"):+ type_idx = next(i for i, entry in enumerate(page.log) if entry[0] == "type" and entry[1] == label)+ clear_idx = next(+ (i for i, entry in enumerate(page.log[:type_idx]) if entry[0] == "press" and entry[1] == label),+ None,+ )+ assert clear_idx is not None, f"{label} segment was typed into without clearing its content first"+++async def test_type_date_group_rejects_a_read_back_longer_than_the_expected_width(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # A page that reads back "010" for an expected "10" normalizes to the same int but is not the same+ # commit -- it is exactly the shape an unclearer append would produce. Length must agree too.+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(+ group_probe={"ok": True, "reason": None, "targetLabel": "month"},+ committed_digits={"month": "09", "day": "010", "year": "2026"},+ )+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "09/10/2026"})+ assert r.status == "error", r.content+ assert "day" in r.content+++async def test_type_date_widget_that_normalizes_the_final_segment_on_blur_is_caught(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # The final (year) segment still holds focus right after the fill loop, so a widget that+ # normalizes/clamps a value only on blur has not fired that yet. The tool must blur it and only+ # then take the read-back that decides success.+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(+ group_probe={"ok": True, "reason": None, "targetLabel": "month"},+ blur_clamp=("year", "2026", "2020"),+ )+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "09/18/2026"})+ assert r.status == "error", r.content+ assert "year" in r.content+++async def test_type_date_widget_that_clamps_a_segment_on_blur_is_not_reported_as_filled(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ # _date_segment_holds only checks a segment right after typing it, while it still has focus. A+ # day segment that clamps 31 -> 28 only once focus leaves it (its blur, which fires only once the+ # loop focuses year) still reads "31" at that moment; the final re-verify must catch the drift.+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(+ group_probe={"ok": True, "reason": None, "targetLabel": "month"},+ blur_clamp=("day", "31", "28"),+ )+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "02/31/2026"})+ assert r.status == "error", r.content+ assert "day" in r.content+++async def test_type_date_dropped_segment_never_reads_back_as_filled(monkeypatch: pytest.MonkeyPatch) -> None:+ # The fake's read-back must reflect what was actually typed: a segment the fill loop never+ # focuses/types (simulated here by a widget that refuses focus on `day`) must read back empty, so+ # a regression that drops a segment from the loop is caught rather than reported as a success.+ monkeypatch.setattr(taskv3_tools, "asyncio", ScopedAsyncio(sleep=_instant_sleep))+ page = _DateSegmentFakePage(+ group_probe={"ok": True, "reason": None, "targetLabel": "month"},+ broken_segment="day",+ )+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#month-segment", "text": "09/18/2026"})+ assert r.status == "error", r.content+ assert "day" in r.content+ assert "NOT filled" in r.content++@pytest.mark.asyncioasync def test_select_combobox_commits_reacting_suggestion(monkeypatch: pytest.MonkeyPatch) -> None:import asyncio as _a@@ -7934,6 +8316,63 @@ async def test_type_into_an_unclickable_field_never_reports_a_fill_that_did_not_assert await page.eval_on_selector("#month", "el => el.value") == ""+# A fieldset holding a real month/day/year spinbutton group ALSO holds an unrelated+# spinbutton (e.g. an employee ID). The bijection probe must reject a target that is not itself a+# date segment rather than hijacking the sibling group just because the fieldset contains one.+_FIELDSET_WITH_UNRELATED_SPINBUTTON_HTML = """+<fieldset>+ <input id="employee-id" type="text" role="spinbutton" aria-label="Employee ID">+ <input id="month" type="text" role="spinbutton" aria-label="Month">+ <input id="day" type="text" role="spinbutton" aria-label="Day">+ <input id="year" type="text" role="spinbutton" aria-label="Year">+</fieldset>+"""+++@_skip_no_browser+async def test_type_into_an_unrelated_spinbutton_never_hijacks_a_sibling_date_group() -> None:+ # An 8-digit ID typed into an unrelated spinbutton must land in THAT field; it must never silently+ # overwrite a sibling month/day/year group just because they share a fieldset.+ async with _content_page(_FIELDSET_WITH_UNRELATED_SPINBUTTON_HTML) as page:+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": "#employee-id", "text": "09/18/2026"})+ assert r.status == "ok", r.content+ assert await page.eval_on_selector("#employee-id", "el => el.value") == "09/18/2026"+ assert await page.eval_on_selector("#month", "el => el.value") == ""+ assert await page.eval_on_selector("#day", "el => el.value") == ""+ assert await page.eval_on_selector("#year", "el => el.value") == ""+++# A day segment that clamps its value on blur -- which fires only once focus leaves it for+# the next segment -- must not be reported as a successful fill once the committed value has drifted+# from what was requested.+_DATE_GROUP_CLAMPS_DAY_ON_BLUR_HTML = """+<div role="group" aria-label="Start date">+ <input id="month" type="text" role="spinbutton" aria-label="Month">+ <input id="day" type="text" role="spinbutton" aria-label="Day">+ <input id="year" type="text" role="spinbutton" aria-label="Year">+</div>+<script>+ document.getElementById('day').addEventListener('blur', () => {+ const day = document.getElementById('day');+ if (day.value === '31') { day.value = '28'; }+ });+</script>+"""+++@_skip_no_browser+async def test_type_date_widget_clamping_a_segment_on_blur_is_not_reported_as_filled() -> None:+ async with _content_page(_DATE_GROUP_CLAMPS_DAY_ON_BLUR_HTML) as page:+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": '[aria-label="Month"]', "text": "02/31/2026"})+ assert r.status == "error", r.content+ assert "day" in r.content+ assert await page.eval_on_selector("#day", "el => el.value") == "28"++# The same unclickable shape on a field that only commits a picked suggestion: the raw query sits in# the input until blur, so reading it back proves nothing._UNCLICKABLE_TYPEAHEAD_HTML = """@@ -8593,6 +9032,31 @@ async def test_type_refuses_a_covered_field_whose_type_skips_the_typeahead_probeassert await page.eval_on_selector("#em", "el => el.value") == ""+# The segmented-date fill path's early return used to skip the reachability/occluder guard entirely,+# so a date field under a consent wall could be driven by programmatic focus()+keyboard input and+# report success on a control a person could not have reached.+_COVERED_DATE_SEGMENT_GROUP_HTML = """+<div role="group" style="position:absolute;left:0;top:0;width:300px;height:40px">+ <span role="spinbutton" aria-label="Month" tabindex="0">MM</span>+ <span role="spinbutton" aria-label="Day" tabindex="0">DD</span>+ <span role="spinbutton" aria-label="Year" tabindex="0">YYYY</span>+</div>+<div id="consent" style="position:fixed;left:0;top:0;width:100%;height:100%;background:#fff">Accept cookies</div>+"""+++@_skip_no_browser+async def test_type_refuses_a_covered_segmented_date_group_without_typing_into_it() -> None:+ async with _content_page(_COVERED_DATE_SEGMENT_GROUP_HTML) as page:+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "type").handler({"selector": '[aria-label="Month"]', "text": "10/25/2020"})+ assert r.status == "error", r.content+ assert r.error_class == "covered", r.error_class+ assert await page.eval_on_selector('[aria-label="Month"]', "el => el.textContent") == "MM"+ assert await page.eval_on_selector('[aria-label="Day"]', "el => el.textContent") == "DD"++# The occluding host's shadowRoot getter throws, the same poisoning shape as# test_a_host_whose_shadow_root_read_throws_is_disclosed. The occlusion probe's naming walk reads# `.shadowRoot` on every layer candidate; an unguarded read there crashes the whole page.evaluate(),diff --git a/tests/unit/test_workflow_copilot_route.py b/tests/unit/test_workflow_copilot_route.pyindex 333e1563c..2f94b24ae 100644--- a/tests/unit/test_workflow_copilot_route.py+++ b/tests/unit/test_workflow_copilot_route.py@@ -71,6 +71,7 @@ from skyvern.forge.sdk.routes.workflow_copilot import ()from skyvern.forge.sdk.schemas.copilot_turn_outcome import ConnectedAccountChoice, ResponseKind, TurnOutcomefrom skyvern.forge.sdk.schemas.workflow_copilot import (+ COPILOT_PROPOSAL_CLAIM_LEASE,COPILOT_PROPOSAL_METADATA_KEY,CopilotAttachedFile,CopilotPendingTurn,@@ -4202,6 +4203,43 @@ async def _load_history(chat_id: str = "chat-1") -> Any:)+ ("claim_age", "still_holding"),+ [+ (timedelta(seconds=30), True),+ (COPILOT_PROPOSAL_CLAIM_LEASE + timedelta(seconds=1), False),+ ],+)+async def test_chat_history_reports_the_claim_lease_as_a_remaining_duration(+ monkeypatch: pytest.MonkeyPatch,+ claim_age: timedelta,+ still_holding: bool,+) -> None:+ """A client that compared claimed_at to its own clock disagreed with the server whenever it+ drifted, so the row carries what is left of the lease instead of when it started."""+ canonical = _make_copilot_workflow("Saved", _NOW)+ proposal = canonical.model_dump(mode="json")+ proposal["_copilot_yaml"] = "title: Candidate\n"+ proposal[COPILOT_PROPOSAL_METADATA_KEY] = {+ "owner_turn_id": "turn-owner",+ "revision": 1,+ "canonical_fingerprint": _fingerprint_of(canonical),+ "disposition": "accepting",+ "claimed_at": (datetime.now(timezone.utc) - claim_age).isoformat(),+ }+ chat = _make_persisted_chat([], proposed_workflow=proposal)+ _install_reconcile_store(monkeypatch, chat, canonical=canonical)++ remaining = (await _load_history()).proposed_claim_expires_in_seconds++ if not still_holding:+ assert remaining is None+ else:+ assert remaining is not None+ assert 0 < remaining <= COPILOT_PROPOSAL_CLAIM_LEASE.total_seconds()++@pytest.mark.asyncioasync def test_chat_history_marks_abandoned_turn_interrupted_not_cancelled(monkeypatch: pytest.MonkeyPatch,@@ -6523,12 +6561,62 @@ async def test_history_still_shows_a_candidate_whose_accept_died_holding_its_claSimpleNamespace(now=lambda tz=None: _NOW, fromisoformat=datetime.fromisoformat),)- proposal, metadata, _run = await workflow_copilot_route._history_proposal_state(chat, "org-1")+ proposal, metadata, _run, _claim = await workflow_copilot_route._history_proposal_state(chat, "org-1")assert proposal is storedassert metadata is not None and metadata.disposition == "accepting"+async def test_a_live_claim_is_still_reported_when_canonical_moved_under_the_proposal(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Hiding the proposal must not hide the claim.++ A concurrent save moves canonical, so the fingerprint no longer matches and the candidate is+ correctly withheld - but an Accept may still be writing under that claim, which is exactly when+ a client must not save over it. Reporting no claim while one is live is not ambiguity; the+ client reads an explicit null as "the server says nobody is writing" and releases its fence.+ """+ canonical = _make_copilot_workflow("Base", _NOW)+ stored = {+ "workflow_id": "wf-1",+ "_copilot_yaml": "title: Base\n",+ COPILOT_PROPOSAL_METADATA_KEY: {+ "owner_turn_id": "turn-a",+ "revision": 1,+ "canonical_fingerprint": _fingerprint_of(canonical),+ "disposition": "accepting",+ "claimed_at": _NOW.isoformat(),+ },+ }+ chat = SimpleNamespace(+ organization_id="org-1",+ workflow_copilot_chat_id="chat-1",+ workflow_permanent_id="wpid-1",+ proposed_workflow=stored,+ auto_accept=False,+ )+ # Someone else's save landed, so canonical is no longer what this proposal was built against.+ moved = _make_copilot_workflow("Moved by another writer", _NOW)+ monkeypatch.setattr(+ app.DATABASE,+ "workflows",+ SimpleNamespace(get_workflow_by_permanent_id=AsyncMock(return_value=moved)),+ )+ monkeypatch.setattr(+ workflow_copilot_route,+ "datetime",+ SimpleNamespace(now=lambda tz=None: _NOW, fromisoformat=datetime.fromisoformat),+ )++ proposal, metadata, _run, claim_expires_in = await workflow_copilot_route._history_proposal_state(chat, "org-1")++ assert proposal is None+ assert metadata is None+ assert claim_expires_in == COPILOT_PROPOSAL_CLAIM_LEASE.total_seconds()++@pytest.mark.asyncioasync def test_a_large_run_output_is_bounded_before_it_reaches_every_history_response(monkeypatch: pytest.MonkeyPatch,@@ -6575,7 +6663,7 @@ async def test_a_large_run_output_is_bounded_before_it_reaches_every_history_res),)- _proposal, _metadata, run_facts = await workflow_copilot_route._history_proposal_state(chat, "org-1")+ _proposal, _metadata, run_facts, _claim = await workflow_copilot_route._history_proposal_state(chat, "org-1")assert run_facts is not Noneby_id = {row.output_parameter_id: row.value for row in run_facts.outputs}diff --git a/tests/unit/workflow/test_code_block_escalation.py b/tests/unit/workflow/test_code_block_escalation.pyindex 524fe75e1..75e07ec39 100644--- a/tests/unit/workflow/test_code_block_escalation.py+++ b/tests/unit/workflow/test_code_block_escalation.py@@ -36,6 +36,7 @@ from skyvern.forge.sdk.workflow.models.block import ()from skyvern.forge.sdk.workflow.models.parameter import OutputParameter, ParameterTypefrom skyvern.forge.sdk.workflow.service import _merge_workflow_run_errors+from skyvern.schemas.runs import RunEnginefrom skyvern.schemas.self_heal import HealClassification, HealSkipReason, OutputObligationfrom skyvern.webeye.actions.actions import Actionfrom skyvern.webeye.browser_artifacts import BrowserArtifacts@@ -1169,6 +1170,22 @@ async def test_workflow_setting_enables_heal_when_env_off(monkeypatch: pytest.Moassert state["execute_step_calls"] == 1+async def test_the_ai_fallback_runs_its_escalation_task_on_task_v3(monkeypatch: pytest.MonkeyPatch) -> None:+ state = _install_db_fakes(monkeypatch, final_status=TaskStatus.completed)+ block = _make_code_block(steps=[CodeBlockStep(description="download", line_start=1, line_end=1)])++ exc = RuntimeError("rotted selector")++ result = await _heal(block, _make_context(enable_self_healing=True), exc, _recording_page(exc))++ assert result is not None+ assert state["execute_step_kwargs"]["engine"] is RunEngine.skyvern_v3+ # Without this the engine sizes the run like a bare task: the org's caps stop binding it and+ # it stops following popups and reaching into child frames.+ assert state["execute_step_kwargs"]["workflow_owned_recovery"] is True++@pytest.mark.asyncioasync def test_non_copilot_workflow_never_heals_from_the_toggle(monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", False, raising=False)