Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Let the copilot save an edit to a login step without logging in again (SKY-13709) (#8162)
Andrew Neilsonbd7f9fb6183b
diff --git a/alembic/versions/2026_08_07_1956-a3b94b945dac_add_auto_profile_disabled_to_credentials.py b/alembic/versions/2026_08_07_1956-a3b94b945dac_add_auto_profile_disabled_to_credentials.pynew file mode 100644index 000000000..696b9fa77--- /dev/null+++ b/alembic/versions/2026_08_07_1956-a3b94b945dac_add_auto_profile_disabled_to_credentials.py@@ -0,0 +1,27 @@+"""add auto profile disabled to credentials++Revision ID: a3b94b945dac+Revises: ec34d7f81ad4+Create Date: 2026-08-07 19:56:40-07:00++"""++from typing import Sequence, Union++import sqlalchemy as sa++from alembic import op++# revision identifiers, used by Alembic.+revision: str = "a3b94b945dac"+down_revision: Union[str, None] = "ec34d7f81ad4"+branch_labels: Union[str, Sequence[str], None] = None+depends_on: Union[str, Sequence[str], None] = None+++def upgrade() -> None:+ op.add_column("credentials", sa.Column("auto_profile_disabled", sa.Boolean(), nullable=True))+++def downgrade() -> None:+ op.drop_column("credentials", "auto_profile_disabled")diff --git a/docs/api-reference/openapi.json b/docs/api-reference/openapi.jsonindex c9d108ddd..b625aca00 100644--- a/docs/api-reference/openapi.json+++ b/docs/api-reference/openapi.json@@ -11389,6 +11389,12 @@"title": "Browser Profile Id","description": "Optional plain browser profile to link as this credential's saved profile. Must be an unmanaged profile in your organization that no other credential already owns."},+ "auto_profile_disabled": {+ "type": "boolean",+ "title": "Auto Profile Disabled",+ "description": "Disable automatic saving and reuse of this credential's browser profile.",+ "default": false+ },"pin_saved_session_ip": {"type": "boolean","title": "Pin Saved Session Ip",@@ -11800,6 +11806,18 @@"title": "Browser Profile Id","description": "Browser profile ID linked to this credential"},+ "auto_profile_disabled": {+ "anyOf": [+ {+ "type": "boolean"+ },+ {+ "type": "null"+ }+ ],+ "title": "Auto Profile Disabled",+ "description": "Whether automatic saving and reuse of this credential's browser profile is disabled."+ },"pin_saved_session_ip": {"type": "boolean","title": "Pin Saved Session Ip",diff --git a/docs/developers/optimization/chrome-extension.mdx b/docs/developers/optimization/chrome-extension.mdxindex d68d74b97..faec5b2a2 100644--- a/docs/developers/optimization/chrome-extension.mdx+++ b/docs/developers/optimization/chrome-extension.mdx@@ -60,14 +60,16 @@ For manual setup:## Pair the extension-With your MCP server running, start the recommended one-click flow:+With your MCP server running, ask your MCP client to create a browser session. If the extension is not paired, Skyvern+opens a secure pairing tab automatically. You can also start the flow explicitly:```bashskyvern browser extension-pair```Click **Approve** in the pairing page, then approve the pairing again in the **Skyvern Agent** confirmation tab that-opens. The pairing link expires after two minutes and can be used only once; rerun the command if it expires.+opens. The pairing link expires after two minutes and can be used only once; retry the browser-session request or rerun+the command if it expires.As a manual fallback, copy the pairing token to your clipboard:diff --git a/docs/developers/self-hosted/docker.mdx b/docs/developers/self-hosted/docker.mdxindex 0ba28a6af..28bffefdf 100644--- a/docs/developers/self-hosted/docker.mdx+++ b/docs/developers/self-hosted/docker.mdx@@ -160,9 +160,11 @@ The `.env` file controls the Skyvern server. Here are the most important variabl`docker-compose.yml` uses TWO sources for service environment:-1. **Inline `environment:` block** — compose-network values that depend on the docker network topology. Examples: `DATABASE_STRING=postgresql+psycopg://skyvern:skyvern@postgres:5432/skyvern` (the `postgres` hostname only resolves inside the compose network), `BROWSER_TYPE`, `BROWSER_STREAMING_MODE`, `ENABLE_CODE_BLOCK`.+1. **Inline `environment:` block** — compose-network values that depend on the docker network topology. Examples:+ `DATABASE_STRING=postgresql+psycopg://skyvern:skyvern@postgres:5432/skyvern` (the `postgres` hostname only resolves+ inside the compose network), `BROWSER_TYPE`, `BROWSER_STREAMING_MODE`.-2. **`env_file: .env`** — user secrets (LLM API keys, etc.).+2. **`env_file: .env`** — user settings such as `CODE_BLOCK_MODE` and secrets such as LLM API keys.**Do not move `DATABASE_STRING` from compose into `.env`.** The `.env.example` shipped with the repo uses a `localhost` host that points at the backend container itself when used inside compose — moving that value into `.env` will break the database connection.diff --git a/skyvern-frontend/src/api/types.ts b/skyvern-frontend/src/api/types.tsindex 4e52f01e9..7fd3bca74 100644--- a/skyvern-frontend/src/api/types.ts+++ b/skyvern-frontend/src/api/types.ts@@ -959,6 +959,9 @@ export type ActionsApiResponse = {created_by: string | null;text: string | null;screenshot_artifact_id?: string | null;+ // On the base Action, so it survives the timeline's `list[Action]` serialization. Subclass-only+ // fields (url, keys) do not and must not be declared here.+ file_name?: string | null;// Code block recorded actions carry code_line and duration_ms here.output?:| { code_line?: number | null; duration_ms?: number | null }@@ -1056,6 +1059,7 @@ export type CredentialApiResponse = {credential_type: "password" | "credit_card" | "secret";name: string;browser_profile_id?: string | null;+ auto_profile_disabled?: boolean | null;pin_saved_session_ip?: boolean | null;tested_url?: string | null;user_context?: string | null;@@ -1102,6 +1106,7 @@ export type CreateCredentialRequest = {proxy_session_id?: string | null;rotate_proxy_session_id?: boolean;browser_profile_id?: string | null;+ auto_profile_disabled?: boolean;pin_saved_session_ip?: boolean;};diff --git a/skyvern-frontend/src/components/BrowserStream.test.tsx b/skyvern-frontend/src/components/BrowserStream.test.tsxindex 4a6338b90..8df2971c7 100644--- a/skyvern-frontend/src/components/BrowserStream.test.tsx+++ b/skyvern-frontend/src/components/BrowserStream.test.tsx@@ -499,6 +499,36 @@ describe("BrowserStream", () => {expect(onActivity).toHaveBeenCalledTimes(1);});+ it("shows a two-sentence message on an unclean VNC disconnect", async () => {+ renderBrowserStream();++ await waitFor(() => {+ expect(mocks.rfbInstances).toHaveLength(1);+ });++ const rfb = mocks.rfbInstances[0] as unknown as {+ emit: (type: string, detail?: unknown) => void;+ };+ rfb.emit("disconnect", { clean: false });++ // The component reconnects after a disconnect, which flips isReady back+ // quickly -- assert everything about this one render in a single atomic+ // callback so a reconnect between separate awaits can't hide a failure.+ await waitFor(() => {+ expect(screen.getByText("The browser stream slipped away")).toBeTruthy();+ expect(+ screen.getByText(+ "Refresh the page or switch to local browser streaming.",+ ),+ ).toBeTruthy();+ expect(+ screen.queryByText(+ "The browser stream dropped before everything wrapped up.",+ ),+ ).toBeNull();+ });+ });+describe("recording reset lifecycle", () => {it("resets the recording store on unmount by default", async () => {const { unmount } = renderWithRecordingReset(undefined);diff --git a/skyvern-frontend/src/components/BrowserStream.tsx b/skyvern-frontend/src/components/BrowserStream.tsxindex 162a0b452..6fa214cc4 100644--- a/skyvern-frontend/src/components/BrowserStream.tsx+++ b/skyvern-frontend/src/components/BrowserStream.tsx@@ -615,8 +615,6 @@ function BrowserStream({}: {title: "The browser stream slipped away",- detail:- "The browser stream dropped before everything wrapped up.",hint: "Refresh the page or switch to local browser streaming.",}),);diff --git a/skyvern-frontend/src/routes/browserSessions/BrowserSession.tsx b/skyvern-frontend/src/routes/browserSessions/BrowserSession.tsxindex 1b5de540a..05d3948ec 100644--- a/skyvern-frontend/src/routes/browserSessions/BrowserSession.tsx+++ b/skyvern-frontend/src/routes/browserSessions/BrowserSession.tsx@@ -4,6 +4,7 @@ import { Outlet, useLocation, useParams } from "react-router-dom";import { useQuery } from "@tanstack/react-query";import { getClient } from "@/api/AxiosClient";+import { Badge } from "@/components/ui/badge";import { Button } from "@/components/ui/button";import {Dialog,@@ -17,7 +18,6 @@ import {} from "@/components/ui/dialog";import { BrowserStream } from "@/components/BrowserStream";import { BrowserIcon } from "@/components/icons/BrowserIcon";-import { LogoMinimized } from "@/components/LogoMinimized";import { SwitchBarNavigation } from "@/components/SwitchBarNavigation";import { Toaster } from "@/components/ui/toaster";import { useCredentialGetter } from "@/hooks/useCredentialGetter";@@ -44,6 +44,19 @@ import { BrowserSessionTimeline } from "./BrowserSessionTimeline";import { BrowserSessionWorkflowRuns } from "./BrowserSessionWorkflowRuns";import { getBrowserSessionTabFromPathname } from "./BrowserSession.utils";+// PersistentBrowserSessionStatus values (skyvern/forge/sdk/schemas/persistent_browser_sessions.py)+const BROWSER_SESSION_STATUS_BADGE_VARIANT: Record<+ string,+ "success" | "warning" | "destructive" | "secondary"+> = {+ completed: "success",+ failed: "destructive",+ timeout: "destructive",+ running: "warning",+ retry: "warning",+ created: "secondary",+};+function BrowserSession() {const { browserSessionId } = useParams();const location = useLocation();@@ -97,9 +110,9 @@ function BrowserSession() {if (query.isLoading) {return (<div className="h-screen w-full gap-4 p-6">- <div className="flex h-full w-full items-center justify-center">- {/* we need nice artwork here */}- Loading...+ <div className="flex h-full w-full items-center justify-center gap-2 text-muted-foreground">+ <ReloadIcon className="h-4 w-4 animate-spin" />+ Loading browser session...</div></div>);@@ -119,27 +132,22 @@ function BrowserSession() {return (<div className="h-screen w-full gap-4 p-6"><div className="flex h-full w-full flex-col items-start justify-start gap-2">- <div className="flex w-full flex-shrink-0 flex-row items-center justify-between rounded-lg border p-4">+ <div className="flex w-full flex-shrink-0 flex-row items-center justify-between p-4"><div className="flex w-full flex-row items-center justify-start gap-2">- <LogoMinimized /><div className="text-xl">Browser Session</div>{activeTab === "stream" && <StreamModeBadge mode={streamMode} />}{browserSession && (<div className="ml-auto flex flex-col items-end justify-end overflow-hidden"><div className="flex items-center justify-end gap-2">- <span- className={`rounded px-2 py-0.5 text-xs font-medium ${- browserSession.status === "running"- ? "bg-green-500/20 text-green-500"- : browserSession.status === "completed"- ? "bg-blue-500/20 text-blue-500"- : browserSession.status === "failed"- ? "bg-red-500/20 text-red-500"- : "bg-gray-500/20 text-gray-500"- }`}+ <Badge+ variant={+ BROWSER_SESSION_STATUS_BADGE_VARIANT[+ browserSession.status+ ] ?? "secondary"+ }>{browserSession.status}- </span>+ </Badge><div className="max-w-[20rem] truncate font-mono text-xs opacity-75">{browserSession.browser_session_id}</div>@@ -192,7 +200,7 @@ function BrowserSession() {</Button><Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}><DialogTrigger asChild>- <Button variant="ghost">+ <Button variant="destructive"><StopIcon className="mr-2 h-4 w-4" />Stop</Button>@@ -242,6 +250,7 @@ function BrowserSession() {browserSessionId={browserSessionId}interactive={true}showControlButtons={true}+ enableUrlInput={true}/>)}{!isCdpMode &&diff --git a/skyvern-frontend/src/routes/browserSessions/BrowserSessionStream.tsx b/skyvern-frontend/src/routes/browserSessions/BrowserSessionStream.tsxindex d824523bb..21e77f9d9 100644--- a/skyvern-frontend/src/routes/browserSessions/BrowserSessionStream.tsx+++ b/skyvern-frontend/src/routes/browserSessions/BrowserSessionStream.tsx@@ -99,6 +99,11 @@ interface Props {onReadyChange?: (isReady: boolean, browserSessionId: string | null) => void;onUrlChange?: (url: string) => void;onActivity?: () => void;+ // Opt-in: turns the read-only URL bar into a navigable input. Only the+ // hosted-browser-session live view passes this today (SKY-13683) -- the+ // workflow studio/editor callers of this component leave it unset and keep+ // today's read-only display.+ enableUrlInput?: boolean;}function BrowserSessionStream({@@ -109,6 +114,7 @@ function BrowserSessionStream({onReadyChange,onUrlChange,onActivity,+ enableUrlInput = false,}: Props) {const [streamImgSrc, setStreamImgSrc] = useState<string>("");const [streamFormat, setStreamFormat] = useState<string>("png");@@ -141,6 +147,8 @@ function BrowserSessionStream({inputReady,containerRef,handlers,+ navigate,+ navigateError,} = useCdpInput({inputWsUrl,interactive: controllable,@@ -345,6 +353,8 @@ function BrowserSessionStream({handlers={handlers}currentUrl={currentUrl}centered={centered}+ onNavigate={enableUrlInput ? navigate : undefined}+ navigateError={enableUrlInput ? navigateError : undefined}/>);}diff --git a/skyvern-frontend/src/routes/browserSessions/BrowserSessionTimeline.test.tsx b/skyvern-frontend/src/routes/browserSessions/BrowserSessionTimeline.test.tsxindex 4f56e3b15..cc18a7375 100644--- a/skyvern-frontend/src/routes/browserSessions/BrowserSessionTimeline.test.tsx+++ b/skyvern-frontend/src/routes/browserSessions/BrowserSessionTimeline.test.tsx@@ -474,9 +474,14 @@ describe("BrowserSessionTimeline", () => {{ status: "completed", downloaded_files: null, recordings: null },true,);- const emptyState =- "No timeline events available yet — actions, downloads, and recordings will appear here as they become available.";- expect(await screen.findAllByText(emptyState)).toHaveLength(2);+ expect(+ await screen.findAllByText("No timeline events available yet"),+ ).toHaveLength(2);+ expect(+ screen.getAllByText(+ "actions, downloads, and recordings will appear here as they become available.",+ ),+ ).toHaveLength(2);expect(get).toHaveBeenCalledTimes(2);});});diff --git a/skyvern-frontend/src/routes/browserSessions/BrowserSessionTimeline.tsx b/skyvern-frontend/src/routes/browserSessions/BrowserSessionTimeline.tsxindex 3fee328cb..27ef8d7db 100644--- a/skyvern-frontend/src/routes/browserSessions/BrowserSessionTimeline.tsx+++ b/skyvern-frontend/src/routes/browserSessions/BrowserSessionTimeline.tsx@@ -183,9 +183,14 @@ function BrowserSessionTimeline() {if (timeline.length === 0) {return (<div className="flex h-full w-full items-center justify-center">- <div className="max-w-lg text-center text-lg text-gray-500">- No timeline events available yet — actions, downloads, and recordings- will appear here as they become available.+ <div className="text-center">+ <div className="mb-2 text-lg text-gray-500">+ No timeline events available yet+ </div>+ <div className="text-sm text-gray-400">+ actions, downloads, and recordings will appear here as they become+ available.+ </div></div></div>);diff --git a/skyvern-frontend/src/routes/browserSessions/BrowserSessionVideo.tsx b/skyvern-frontend/src/routes/browserSessions/BrowserSessionVideo.tsxindex bfe20242f..67887ea95 100644--- a/skyvern-frontend/src/routes/browserSessions/BrowserSessionVideo.tsx+++ b/skyvern-frontend/src/routes/browserSessions/BrowserSessionVideo.tsx@@ -71,9 +71,9 @@ function BrowserSessionVideo() {}return (- <div className="h-full w-full p-4">+ <div className="h-full w-full overflow-auto p-4"><div className="mb-4">- <h2 className="text-xl font-semibold">Browser Session Videos</h2>+ <h2 className="text-xl font-semibold">Recordings</h2><p className="text-sm text-gray-500">Recorded videos from this browser session</p>diff --git a/skyvern-frontend/src/routes/credentials/CredentialsModal.test.tsx b/skyvern-frontend/src/routes/credentials/CredentialsModal.test.tsxindex a5c2845eb..5ca672671 100644--- a/skyvern-frontend/src/routes/credentials/CredentialsModal.test.tsx+++ b/skyvern-frontend/src/routes/credentials/CredentialsModal.test.tsx@@ -157,6 +157,7 @@ const editingPasswordCredential: CredentialApiResponse = {totp_identifier: null,},browser_profile_id: "existing-profile-id",+ auto_profile_disabled: false,tested_url: "https://example.com/login",user_context: null,save_browser_session_intent: true,@@ -732,7 +733,7 @@ describe("CredentialsModal browser-memory profile section (flag on)", () => {useFeatureFlagMock.mockImplementation(() => false);});- it("create mode shows the auto-save caption and no profile picker", async () => {+ it("create mode shows the auto-save caption and a collapsed Advanced section", async () => {renderPasswordCredentialsModal();await waitFor(() => {expect(screen.getByDisplayValue("credentials")).toBeTruthy();@@ -740,11 +741,35 @@ describe("CredentialsModal browser-memory profile section (flag on)", () => {expect(screen.getByText(/Skyvern saves this login.s browser state/i),).toBeTruthy();- expect(screen.queryByRole("button", { name: /advanced/i })).toBeNull();+ const advanced = screen.getByRole("button", { name: "Advanced" });+ // Both browser-memory controls live behind the disclosure.+ expect(+ screen.queryByRole("checkbox", {+ name: "Don't automatically save or reuse a browser profile for this credential",+ }),+ ).toBeNull();+ expect(+ screen.queryByRole("checkbox", {+ name: "Keep the same IP for this credential",+ }),+ ).toBeNull();+ fireEvent.click(advanced);+ expect(+ screen.getByRole("checkbox", {+ name: "Keep the same IP for this credential",+ }),+ ).toBeTruthy();+ expect(+ screen+ .getByRole("checkbox", {+ name: "Don't automatically save or reuse a browser profile for this credential",+ })+ .getAttribute("data-state"),+ ).toBe("unchecked");expect(screen.queryByText("Browser profile")).toBeNull();});- it("create mode submits browser_profile_id null and the IP-pin choice", async () => {+ it("create mode submits the IP pin and automatic-profile opt-out", async () => {postMock.mockResolvedValueOnce({data: { credential_id: "c1", name: "credentials" },});@@ -765,7 +790,17 @@ describe("CredentialsModal browser-memory profile section (flag on)", () => {document.querySelector('input[type="password"]') as HTMLInputElement,{ target: { value: "password" } },);- fireEvent.click(screen.getByRole("checkbox"));+ fireEvent.click(screen.getByRole("button", { name: "Advanced" }));+ fireEvent.click(+ screen.getByRole("checkbox", {+ name: "Keep the same IP for this credential",+ }),+ );+ fireEvent.click(+ screen.getByRole("checkbox", {+ name: "Don't automatically save or reuse a browser profile for this credential",+ }),+ );fireEvent.click(screen.getByRole("button", { name: "Save" }));await waitFor(() => {expect(postMock).toHaveBeenCalledWith(@@ -773,12 +808,13 @@ describe("CredentialsModal browser-memory profile section (flag on)", () => {expect.objectContaining({browser_profile_id: null,pin_saved_session_ip: true,+ auto_profile_disabled: true,}),);});}, 10_000);- it("edit mode shows the Browser profile select inline — no Advanced gate, no create caption", async () => {+ it("edit mode shows the Browser profile select and Advanced section", async () => {getMock.mockImplementation((url: string) =>url.includes("/browser_profiles/")? Promise.resolve({@@ -796,12 +832,59 @@ describe("CredentialsModal browser-memory profile section (flag on)", () => {expect(screen.getByText("Browser profile")).toBeTruthy();});expect(screen.getByRole("combobox")).toBeTruthy();- expect(screen.queryByRole("button", { name: /advanced/i })).toBeNull();+ expect(screen.getByRole("button", { name: "Advanced" })).toBeTruthy();expect(screen.queryByText(/Skyvern saves this login.s browser state/i),).toBeNull();});+ it("edit mode persists opting back in without unlinking the profile", async () => {+ patchMock.mockResolvedValueOnce({+ data: {+ ...editingPasswordCredential,+ auto_profile_disabled: false,+ },+ });+ const view = renderEditPasswordCredentialsModal({+ ...editingPasswordCredential,+ auto_profile_disabled: true,+ });+ await waitFor(() => {+ expect(screen.getByRole("button", { name: "Advanced" })).toBeTruthy();+ });+ fireEvent.click(screen.getByRole("button", { name: "Advanced" }));+ const optOut = screen.getByRole("checkbox", {+ name: "Don't automatically save or reuse a browser profile for this credential",+ });+ expect(optOut.getAttribute("data-state")).toBe("checked");+ fireEvent.click(optOut);+ fireEvent.click(screen.getByRole("button", { name: "Update" }));++ await waitFor(() => {+ expect(patchMock).toHaveBeenCalledWith(+ "/credentials/real-cred-id",+ expect.objectContaining({+ auto_profile_disabled: false,+ browser_profile_id: "existing-profile-id",+ }),+ );+ });++ view.unmount();+ renderEditPasswordCredentialsModal({+ ...editingPasswordCredential,+ auto_profile_disabled: false,+ });+ fireEvent.click(screen.getByRole("button", { name: "Advanced" }));+ expect(+ screen+ .getByRole("checkbox", {+ name: "Don't automatically save or reuse a browser profile for this credential",+ })+ .getAttribute("data-state"),+ ).toBe("unchecked");+ });+it("keeps the proxy IP-pin UI for card credentials under the flag", async () => {const queryClient = new QueryClient({defaultOptions: {@@ -876,8 +959,16 @@ describe("CredentialsModal browser-memory profile section (flag on)", () => {);}+ async function openAdvanced() {+ await waitFor(() => {+ expect(screen.getByRole("button", { name: "Advanced" })).toBeTruthy();+ });+ fireEvent.click(screen.getByRole("button", { name: "Advanced" }));+ }+it("shows the IP session identity and Rotate when the credential is pinned", async () => {renderEditModalFor(pinnedEditCredential);+ await openAdvanced();await waitFor(() => {expect(screen.getByText(/psi_9f8a7b/)).toBeTruthy();});@@ -886,6 +977,7 @@ describe("CredentialsModal browser-memory profile section (flag on)", () => {it("hides the IP session identity and Rotate when no session is pinned", async () => {renderEditModalFor(pinnedNoSessionCredential);+ await openAdvanced();await waitFor(() => {expect(screen.getByText("Keep the same IP for this credential"),@@ -900,6 +992,7 @@ describe("CredentialsModal browser-memory profile section (flag on)", () => {data: { credential_id: "real-cred-id", name: "Acme Login" },});renderEditModalFor(pinnedEditCredential);+ await openAdvanced();await waitFor(() => {expect(screen.getByRole("button", { name: "Rotate" })).toBeTruthy();});diff --git a/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx b/skyvern-frontend/src/routes/credentials/CredentialsModal.tsxindex a5af9b75e..8e386faa6 100644--- a/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx+++ b/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx@@ -51,6 +51,12 @@ import {import { Alert, AlertDescription } from "@/components/ui/alert";import { useCredentialsQuery } from "@/routes/workflows/hooks/useCredentialsQuery";import { Checkbox } from "@/components/ui/checkbox";+import {+ Accordion,+ AccordionContent,+ AccordionItem,+ AccordionTrigger,+} from "@/components/ui/accordion";import { useFeatureFlag } from "@/hooks/useFeatureFlag";import { useBrowserProfileQuery } from "@/routes/browserProfiles/hooks/useBrowserProfileQuery";import { useBrowserProfileUsageQuery } from "@/routes/browserProfiles/hooks/useBrowserProfileUsageQuery";@@ -414,6 +420,7 @@ function CredentialsModal({const browserMemoryEnabled = useFeatureFlag("browser_memory_v1");// Quick-win: optional browser profile + per-credential IP pin (flag-on).const [browserProfileId, setBrowserProfileId] = useState<string | null>(null);+ const [autoProfileDisabled, setAutoProfileDisabled] = useState(false);const [pinIp, setPinIp] = useState(false);const [runSequentially, setRunSequentially] = useState(false);// The inline profile Select has no scroll pagination, so pull a large first@@ -633,6 +640,7 @@ function CredentialsModal({setTestAndSave(true);}setBrowserProfileId(editingCredential.browser_profile_id ?? null);+ setAutoProfileDisabled(editingCredential.auto_profile_disabled ?? false);setPinIp(editingCredential.pin_saved_session_ip ?? false);setRunSequentially(editingCredential.run_sequentially ?? false);if (editingCredential.user_context) {@@ -706,6 +714,7 @@ function CredentialsModal({setRemovalConfirmationMessage(null);removalConfirmedRef.current = false;setBrowserProfileId(null);+ setAutoProfileDisabled(false);setPinIp(false);setRunSequentially(false);setTestAndSave(false);@@ -1068,6 +1077,7 @@ function CredentialsModal({...(includesBrowserMemoryFields? {browser_profile_id: browserProfileId,+ auto_profile_disabled: autoProfileDisabled,pin_saved_session_ip: pinIp,}: {}),@@ -1148,6 +1158,7 @@ function CredentialsModal({proxy_session_id,rotate_proxy_session_id,browser_profile_id,+ auto_profile_disabled,pin_saved_session_ip,run_sequentially,}: {@@ -1160,6 +1171,7 @@ function CredentialsModal({proxy_session_id?: string | null;rotate_proxy_session_id?: boolean;browser_profile_id?: string | null;+ auto_profile_disabled?: boolean;pin_saved_session_ip?: boolean;run_sequentially?: boolean;}) => {@@ -1188,6 +1200,9 @@ function CredentialsModal({if (browser_profile_id !== undefined) {body.browser_profile_id = browser_profile_id;}+ if (auto_profile_disabled !== undefined) {+ body.auto_profile_disabled = auto_profile_disabled;+ }if (pin_saved_session_ip !== undefined) {body.pin_saved_session_ip = pin_saved_session_ip;}@@ -1253,9 +1268,15 @@ function CredentialsModal({const bmChanged =browserMemoryFields &&(browserProfileId !== (editingCredential?.browser_profile_id ?? null) ||+ autoProfileDisabled !==+ (editingCredential?.auto_profile_disabled ?? false) ||pinIp !== (editingCredential?.pin_saved_session_ip ?? false));const bmPatch = browserMemoryFields- ? { browser_profile_id: browserProfileId, pin_saved_session_ip: pinIp }+ ? {+ browser_profile_id: browserProfileId,+ auto_profile_disabled: autoProfileDisabled,+ pin_saved_session_ip: pinIp,+ }: {};const runSequentiallyChanged =@@ -1456,6 +1477,7 @@ function CredentialsModal({...(browserMemoryEnabled? {browser_profile_id: browserProfileId,+ auto_profile_disabled: autoProfileDisabled,pin_saved_session_ip: pinIp,}: {}),@@ -1732,65 +1754,10 @@ function CredentialsModal({<div className="space-y-3">{!isEditMode && (<p className="text-xs text-muted-foreground">- Skyvern saves this login’s browser state after the first successful- sign-in and keeps it fresh automatically.+ Skyvern saves this login’s browser state after the first sign-in and+ keeps it fresh automatically. You can opt out in Advanced below.</p>)}- <label className="flex cursor-pointer items-center gap-2 text-sm">- <Checkbox- checked={pinIp}- onCheckedChange={(checked) => setPinIp(checked === true)}- />- <TooltipProvider>- <Tooltip>- <TooltipTrigger asChild>- <span- tabIndex={0}- className="rounded-sm outline-none focus-visible:ring-2 focus-visible:ring-ring"- >- Keep the same IP for this credential- </span>- </TooltipTrigger>- <TooltipContent className="max-w-[260px]">- Sign-ins with this credential use one dedicated IP, so the saved- session stays valid on sites that check for network changes.- </TooltipContent>- </Tooltip>- </TooltipProvider>- </label>- <p className="ml-6 text-xs text-muted-foreground">- Helps avoid extra 2FA, captchas, and temporary locks on sites that check- for network changes.- </p>- {pinIp && existingSessionPrefix && (- <div className="ml-6 space-y-2">- <div className="flex items-center gap-2">- <p className="text-xs text-muted-foreground">- IP session:{" "}- <span className="font-mono text-foreground">- {existingSessionPrefix}- </span>- </p>- <HelpTooltip content="This credential reuses one saved network identity so logins stay valid. Rotate to force a fresh one on the next sign-in." />- </div>- <div className="flex flex-wrap items-center gap-2">- <Button- type="button"- variant="secondary"- size="sm"- onClick={() => setRotateProxyPin(true)}- disabled={rotateProxyPin}- >- Rotate- </Button>- {rotateProxyPin && (- <span className="text-xs text-muted-foreground">- A new IP session is created when you save.- </span>- )}- </div>- </div>- )}{isEditMode && (<div className="space-y-1.5"><Label className="text-sm font-medium">Browser profile</Label>@@ -1824,6 +1791,86 @@ function CredentialsModal({)}</div>)}+ <Accordion type="single" collapsible>+ <AccordionItem value="advanced" className="border-b-0">+ <AccordionTrigger className="py-2">Advanced</AccordionTrigger>+ <AccordionContent className="space-y-3">+ <label className="flex cursor-pointer items-center gap-2 text-sm">+ <Checkbox+ checked={pinIp}+ onCheckedChange={(checked) => setPinIp(checked === true)}+ />+ <TooltipProvider>+ <Tooltip>+ <TooltipTrigger asChild>+ <span+ tabIndex={0}+ className="rounded-sm outline-none focus-visible:ring-2 focus-visible:ring-ring"+ >+ Keep the same IP for this credential+ </span>+ </TooltipTrigger>+ <TooltipContent className="max-w-[260px]">+ Sign-ins with this credential use one dedicated IP, so the+ saved session stays valid on sites that check for network+ changes.+ </TooltipContent>+ </Tooltip>+ </TooltipProvider>+ </label>+ <p className="ml-6 text-xs text-muted-foreground">+ Helps avoid extra 2FA, captchas, and temporary locks on sites that+ check for network changes.+ </p>+ {pinIp && existingSessionPrefix && (+ <div className="ml-6 space-y-2">+ <div className="flex items-center gap-2">+ <p className="text-xs text-muted-foreground">+ IP session:{" "}+ <span className="font-mono text-foreground">+ {existingSessionPrefix}+ </span>+ </p>+ <HelpTooltip content="This credential reuses one saved network identity so logins stay valid. Rotate to force a fresh one on the next sign-in." />+ </div>+ <div className="flex flex-wrap items-center gap-2">+ <Button+ type="button"+ variant="secondary"+ size="sm"+ onClick={() => setRotateProxyPin(true)}+ disabled={rotateProxyPin}+ >+ Rotate+ </Button>+ {rotateProxyPin && (+ <span className="text-xs text-muted-foreground">+ A new IP session is created when you save.+ </span>+ )}+ </div>+ </div>+ )}+ <label className="flex cursor-pointer items-center gap-2 text-sm">+ <Checkbox+ id="auto-profile-disabled"+ checked={autoProfileDisabled}+ onCheckedChange={(checked) =>+ setAutoProfileDisabled(checked === true)+ }+ />+ <span>+ Don't automatically save or reuse a browser profile for this+ credential+ </span>+ </label>+ <p className="ml-6 text-xs text-muted-foreground">+ A profile already linked to this credential stays linked — it just+ stops being used or updated automatically.+ </p>+ </AccordionContent>+ </AccordionItem>+ </Accordion></div>);@@ -1855,10 +1902,11 @@ function CredentialsModal({totpError={authenticatorKeyError}authenticatorSaveError={authenticatorSaveError}beforeCredentialFields={customVaultCheckbox}+ afterPassword={+ browserMemoryEnabled ? browserProfileModalSection : null+ }afterUrl={- browserMemoryEnabled ? (- browserProfileModalSection- ) : (+ browserMemoryEnabled ? null : (<div className="space-y-3"><div className="flex items-center gap-3"><Checkboxdiff --git a/skyvern-frontend/src/routes/credentials/PasswordCredentialContent.tsx b/skyvern-frontend/src/routes/credentials/PasswordCredentialContent.tsxindex 9db5ee342..fa353a80a 100644--- a/skyvern-frontend/src/routes/credentials/PasswordCredentialContent.tsx+++ b/skyvern-frontend/src/routes/credentials/PasswordCredentialContent.tsx@@ -64,6 +64,8 @@ type Props = {urlDisabled?: boolean;/** Slot rendered between URL and the separator before Username (e.g. browser profile checkbox) */afterUrl?: React.ReactNode;+ /** Slot rendered directly under the password input, inside the credential-fields section */+ afterPassword?: React.ReactNode;/** Slot rendered right before the separator between Name/URL and Username/Password */beforeCredentialFields?: React.ReactNode;editMode?: boolean;@@ -213,6 +215,7 @@ function PasswordCredentialContent({urlRequired,urlDisabled,afterUrl,+ afterPassword,beforeCredentialFields,editMode,configuredAdditionalTwoFactorMethod,@@ -763,6 +766,7 @@ function PasswordCredentialContent({</div>)}</div>+ {afterPassword}<Separator /><Accordiontype="single"diff --git a/skyvern-frontend/src/routes/streaming/InteractiveStreamView.test.tsx b/skyvern-frontend/src/routes/streaming/InteractiveStreamView.test.tsxnew file mode 100644index 000000000..be273e740--- /dev/null+++ b/skyvern-frontend/src/routes/streaming/InteractiveStreamView.test.tsx@@ -0,0 +1,173 @@+// @vitest-environment jsdom+import { cleanup, fireEvent, render, screen } from "@testing-library/react";+import { afterEach, describe, expect, it, vi } from "vitest";++import { InteractiveStreamView } from "./InteractiveStreamView";++afterEach(cleanup);++function baseProps() {+ return {+ streamImgSrc: "abc123",+ streamFormat: "png",+ interactive: true,+ userIsControlling: false,+ setUserIsControlling: vi.fn(),+ inputReady: true,+ containerRef: { current: null },+ showControlButtons: true,+ handlers: {+ handleMouseDown: vi.fn(),+ handleMouseUp: vi.fn(),+ handleMouseMove: vi.fn(),+ handleKeyDown: vi.fn(),+ handleKeyUp: vi.fn(),+ },+ currentUrl: "https://example.com/",+ };+}++describe("InteractiveStreamView URL bar", () => {+ it("stays a read-only display when no onNavigate is passed", () => {+ render(<InteractiveStreamView {...baseProps()} />);++ expect(screen.getByText("https://example.com/")).toBeTruthy();+ expect(screen.queryByRole("textbox")).toBeNull();+ });++ it("stays read-only when onNavigate is passed but the user has not taken control", () => {+ render(+ <InteractiveStreamView+ {...baseProps()}+ userIsControlling={false}+ onNavigate={vi.fn()}+ />,+ );++ expect(screen.getByText("https://example.com/")).toBeTruthy();+ expect(screen.queryByRole("textbox")).toBeNull();+ });++ it("becomes an editable input once the user has taken control, and submits on Enter", () => {+ const onNavigate = vi.fn();+ render(+ <InteractiveStreamView+ {...baseProps()}+ userIsControlling={true}+ onNavigate={onNavigate}+ />,+ );++ const input = screen.getByRole("textbox") as HTMLInputElement;+ expect(input.value).toBe("https://example.com/");++ fireEvent.change(input, { target: { value: "https://iana.org " } });+ fireEvent.submit(input.closest("form")!);++ expect(onNavigate).toHaveBeenCalledWith("https://iana.org");+ });++ it("does not forward keystrokes typed into the URL input to the remote page", () => {+ const handleKeyDown = vi.fn();+ render(+ <InteractiveStreamView+ {...baseProps()}+ userIsControlling={true}+ onNavigate={vi.fn()}+ handlers={{+ ...baseProps().handlers,+ handleKeyDown,+ }}+ />,+ );++ const input = screen.getByRole("textbox");+ fireEvent.keyDown(input, { key: "a" });++ expect(handleKeyDown).not.toHaveBeenCalled();+ });++ it("shows a navigate error inline without breaking the input", () => {+ render(+ <InteractiveStreamView+ {...baseProps()}+ userIsControlling={true}+ onNavigate={vi.fn()}+ navigateError="That destination isn't allowed."+ />,+ );++ expect(screen.getByText("That destination isn't allowed.")).toBeTruthy();+ expect(screen.getByRole("textbox")).toBeTruthy();+ });++ it("does not overwrite a focused, mid-typed value when the remote URL changes", () => {+ const onNavigate = vi.fn();+ const { rerender } = render(+ <InteractiveStreamView+ {...baseProps()}+ userIsControlling={true}+ onNavigate={onNavigate}+ />,+ );++ const input = screen.getByRole("textbox") as HTMLInputElement;+ fireEvent.focus(input);+ fireEvent.change(input, { target: { value: "https://draft.example" } });++ rerender(+ <InteractiveStreamView+ {...baseProps()}+ userIsControlling={true}+ onNavigate={onNavigate}+ currentUrl="https://elsewhere.example/"+ />,+ );++ expect(input.value).toBe("https://draft.example");+ });++ it("syncs to the remote URL once the input loses focus", () => {+ const onNavigate = vi.fn();+ const { rerender } = render(+ <InteractiveStreamView+ {...baseProps()}+ userIsControlling={true}+ onNavigate={onNavigate}+ />,+ );++ const input = screen.getByRole("textbox") as HTMLInputElement;+ fireEvent.focus(input);+ fireEvent.change(input, { target: { value: "https://draft.example" } });+ fireEvent.blur(input);++ rerender(+ <InteractiveStreamView+ {...baseProps()}+ userIsControlling={true}+ onNavigate={onNavigate}+ currentUrl="https://elsewhere.example/"+ />,+ );++ expect(input.value).toBe("https://elsewhere.example/");+ });++ it("ignores an empty submission", () => {+ const onNavigate = vi.fn();+ render(+ <InteractiveStreamView+ {...baseProps()}+ userIsControlling={true}+ onNavigate={onNavigate}+ />,+ );++ const input = screen.getByRole("textbox");+ fireEvent.change(input, { target: { value: " " } });+ fireEvent.submit(input.closest("form")!);++ expect(onNavigate).not.toHaveBeenCalled();+ });+});diff --git a/skyvern-frontend/src/routes/streaming/InteractiveStreamView.tsx b/skyvern-frontend/src/routes/streaming/InteractiveStreamView.tsxindex 6f98a149f..c99322bbf 100644--- a/skyvern-frontend/src/routes/streaming/InteractiveStreamView.tsx+++ b/skyvern-frontend/src/routes/streaming/InteractiveStreamView.tsx@@ -1,7 +1,8 @@-import type { RefObject } from "react";+import { useEffect, useRef, useState, type RefObject } from "react";import { ExitIcon, GlobeIcon, HandIcon } from "@radix-ui/react-icons";import { ZoomableImage } from "@/components/ZoomableImage";import { Button } from "@/components/ui/button";+import { Input } from "@/components/ui/input";import { cn } from "@/util/utils";interface InteractiveStreamViewProps {@@ -22,6 +23,11 @@ interface InteractiveStreamViewProps {};currentUrl?: string;centered?: boolean;+ // Only wired up by callers that want the URL bar to double as a navigation+ // input (currently: the hosted-browser-session live view). Omitted, the bar+ // stays the plain read-only display every other caller already gets.+ onNavigate?: (url: string) => void;+ navigateError?: string | null;}function UrlBar({ url }: { url: string }) {@@ -33,6 +39,65 @@ function UrlBar({ url }: { url: string }) {);}+function NavigableUrlBar({+ url,+ onNavigate,+ navigateError,+}: {+ url: string;+ onNavigate: (url: string) => void;+ navigateError?: string | null;+}) {+ const [value, setValue] = useState(url);+ const isFocusedRef = useRef(false);++ useEffect(() => {+ // Don't stomp a URL the user is mid-typing if the remote page navigates elsewhere+ // (e.g. an in-flight action) while they still have the input focused.+ if (!isFocusedRef.current) {+ setValue(url);+ }+ }, [url]);++ return (+ <form+ className="flex h-8 w-full items-center gap-2 rounded-t-md bg-slate-800 px-2"+ onSubmit={(e) => {+ e.preventDefault();+ const trimmed = value.trim();+ if (trimmed) {+ onNavigate(trimmed);+ }+ }}+ >+ <GlobeIcon className="h-3 w-3 flex-shrink-0 text-slate-400" />+ <Input+ value={value}+ onChange={(e) => setValue(e.target.value)}+ onFocus={() => {+ isFocusedRef.current = true;+ }}+ onBlur={() => {+ isFocusedRef.current = false;+ }}+ onClick={(e) => e.stopPropagation()}+ // The stream container forwards every keystroke to the remote CDP session+ // (see InteractiveStreamView's onKeyDown/onKeyUp); typing a URL must not+ // also leak as keyboard input to the page being viewed.+ onKeyDown={(e) => e.stopPropagation()}+ onKeyUp={(e) => e.stopPropagation()}+ placeholder="Enter a URL and press Enter"+ className="h-6 flex-1 border-none bg-transparent px-1 text-xs text-slate-100 shadow-none focus-visible:ring-1 focus-visible:ring-slate-500"+ />+ {navigateError && (+ <span className="flex-shrink-0 truncate text-xs text-red-400">+ {navigateError}+ </span>+ )}+ </form>+ );+}+function InteractiveStreamView({streamImgSrc,streamFormat,@@ -45,10 +110,17 @@ function InteractiveStreamView({handlers,currentUrl,centered,+ onNavigate,+ navigateError,}: InteractiveStreamViewProps) {const imgDataUrl = `data:image/${streamFormat};base64,${streamImgSrc}`;if (interactive) {+ const showNavigableBar =+ Boolean(onNavigate) && userIsControlling && currentUrl !== undefined;+ const showReadOnlyBar = !showNavigableBar && Boolean(currentUrl);+ const showUrlBar = showNavigableBar || showReadOnlyBar;+return (<divref={containerRef}@@ -57,7 +129,14 @@ function InteractiveStreamView({onKeyDown={handlers.handleKeyDown}onKeyUp={handlers.handleKeyUp}>- {currentUrl && <UrlBar url={currentUrl} />}+ {showNavigableBar && (+ <NavigableUrlBar+ url={currentUrl ?? ""}+ onNavigate={onNavigate!}+ navigateError={navigateError}+ />+ )}+ {showReadOnlyBar && <UrlBar url={currentUrl!} />}{showControlButtons && !userIsControlling && inputReady && (<div className="absolute inset-0 z-10 flex items-center justify-center"><Button@@ -84,7 +163,7 @@ function InteractiveStreamView({src={imgDataUrl}className={cn("w-full rounded-md object-contain",- currentUrl ? "h-[calc(100%-2rem)]" : "h-full",+ showUrlBar ? "h-[calc(100%-2rem)]" : "h-full",{ "cursor-default": userIsControlling },)}onMouseDown={handlers.handleMouseDown}diff --git a/skyvern-frontend/src/routes/streaming/useCdpInput.test.ts b/skyvern-frontend/src/routes/streaming/useCdpInput.test.tsindex 262d576c5..14348027a 100644--- a/skyvern-frontend/src/routes/streaming/useCdpInput.test.ts+++ b/skyvern-frontend/src/routes/streaming/useCdpInput.test.ts@@ -47,6 +47,13 @@ class MockWebSocket {listener(event);}}++ emitMessage(data: string) {+ const event = new MessageEvent("message", { data });+ for (const listener of this.listeners.message ?? []) {+ listener(event);+ }+ }}async function renderInputHook() {@@ -297,3 +304,148 @@ describe("useCdpInput key handling", () => {);});});++describe("useCdpInput navigate", () => {+ beforeEach(() => {+ vi.useFakeTimers();+ vi.clearAllMocks();+ MockWebSocket.instances = [];+ vi.stubGlobal("WebSocket", MockWebSocket);+ });++ afterEach(() => {+ vi.useRealTimers();+ vi.unstubAllGlobals();+ });++ it("does not send a navigateEvent when the user has not taken control", async () => {+ const { result } = renderHook(() =>+ useCdpInput({+ inputWsUrl: "wss://input.test",+ interactive: true,+ viewportWidth: 1280,+ viewportHeight: 720,+ }),+ );+ await act(async () => {+ await Promise.resolve();+ });+ const socket = MockWebSocket.instances[0]!;+ socket.send.mockClear();++ act(() => {+ result.current.navigate("https://example.com");+ });++ expect(socket.send).not.toHaveBeenCalled();+ });++ it("sends a navigateEvent over the existing input socket once controlling", async () => {+ const { result } = renderHook(() =>+ useCdpInput({+ inputWsUrl: "wss://input.test",+ interactive: true,+ viewportWidth: 1280,+ viewportHeight: 720,+ }),+ );+ await act(async () => {+ await Promise.resolve();+ });+ act(() => {+ result.current.setUserIsControlling(true);+ });+ const socket = MockWebSocket.instances[0]!;+ socket.send.mockClear();++ act(() => {+ result.current.navigate("https://example.com");+ });++ expect(socket.send).toHaveBeenCalledWith(+ JSON.stringify({ type: "navigateEvent", url: "https://example.com" }),+ );+ expect(MockWebSocket.instances).toHaveLength(1);+ });++ it("surfaces a navigate-error message the server sends back", async () => {+ const { result } = renderHook(() =>+ useCdpInput({+ inputWsUrl: "wss://input.test",+ interactive: true,+ viewportWidth: 1280,+ viewportHeight: 720,+ }),+ );+ await act(async () => {+ await Promise.resolve();+ });+ expect(result.current.navigateError).toBeNull();++ const socket = MockWebSocket.instances[0]!;+ act(() => {+ socket.emitMessage(+ JSON.stringify({ kind: "navigate-error", reason: "blocked" }),+ );+ });++ expect(result.current.navigateError).toBeTruthy();+ });++ it("clears a stale navigateError when control is ceded", async () => {+ const { result } = renderHook(() =>+ useCdpInput({+ inputWsUrl: "wss://input.test",+ interactive: true,+ viewportWidth: 1280,+ viewportHeight: 720,+ }),+ );+ await act(async () => {+ await Promise.resolve();+ });+ act(() => {+ result.current.setUserIsControlling(true);+ });+ const socket = MockWebSocket.instances[0]!;+ act(() => {+ socket.emitMessage(+ JSON.stringify({ kind: "navigate-error", reason: "blocked" }),+ );+ });+ expect(result.current.navigateError).toBeTruthy();++ act(() => {+ result.current.setUserIsControlling(false);+ });++ expect(result.current.navigateError).toBeNull();+ });++ it("clears a stale navigateError on reconnect (fresh ready message)", async () => {+ const { result } = renderHook(() =>+ useCdpInput({+ inputWsUrl: "wss://input.test",+ interactive: true,+ viewportWidth: 1280,+ viewportHeight: 720,+ }),+ );+ await act(async () => {+ await Promise.resolve();+ });+ const socket = MockWebSocket.instances[0]!;+ act(() => {+ socket.emitMessage(+ JSON.stringify({ kind: "navigate-error", reason: "blocked" }),+ );+ });+ expect(result.current.navigateError).toBeTruthy();++ act(() => {+ socket.emitMessage(JSON.stringify({ kind: "ready" }));+ });++ expect(result.current.navigateError).toBeNull();+ });+});diff --git a/skyvern-frontend/src/routes/streaming/useCdpInput.ts b/skyvern-frontend/src/routes/streaming/useCdpInput.tsindex 2891bb9bc..01175f4cd 100644--- a/skyvern-frontend/src/routes/streaming/useCdpInput.ts+++ b/skyvern-frontend/src/routes/streaming/useCdpInput.ts@@ -33,8 +33,15 @@ interface UseCdpInputReturn {handleKeyDown: (e: React.KeyboardEvent) => void;handleKeyUp: (e: React.KeyboardEvent) => void;};+ navigate: (url: string) => void;+ navigateError: string | null;}+const NAVIGATE_ERROR_MESSAGES: Record<string, string> = {+ blocked: "That destination isn't allowed.",+ invalid_url: "Enter a valid http(s) URL.",+};+export function useCdpInput({inputWsUrl,interactive,@@ -43,6 +50,7 @@ export function useCdpInput({}: UseCdpInputOptions): UseCdpInputReturn {const [userIsControlling, setUserIsControlling] = useState(false);const [inputReady, setInputReady] = useState(false);+ const [navigateError, setNavigateError] = useState<string | null>(null);const credentialGetter = useCredentialGetter();const clientId = useClientIdStore((s) => s.clientId);@@ -93,10 +101,17 @@ export function useCdpInput({);inputReconnectAttemptsRef.current = 0;setInputReady(true);+ setNavigateError(null);if (userIsControllingRef.current) {ws.send(JSON.stringify({ kind: "take-control" }));}}+ if (msg.kind === "navigate-error") {+ setNavigateError(+ NAVIGATE_ERROR_MESSAGES[msg.reason] ??+ "Couldn't navigate to that URL.",+ );+ }} catch {// ignore non-JSON messages}@@ -160,6 +175,9 @@ export function useCdpInput({}, [userIsControlling]);useEffect(() => {+ // A stale error from a prior take-control stint should not linger next to a page+ // that has since been ceded, retaken, and possibly navigated by other means.+ setNavigateError(null);const ws = inputSocketRef.current;const kind = userIsControlling ? "take-control" : "cede-control";if (!ws || ws.readyState !== WebSocket.OPEN) {@@ -357,6 +375,15 @@ export function useCdpInput({[interactive, userIsControlling, sendInputEvent],);+ const navigate = useCallback(+ (url: string) => {+ if (!interactive || !userIsControlling) return;+ setNavigateError(null);+ sendInputEvent({ type: "navigateEvent", url });+ },+ [interactive, userIsControlling, sendInputEvent],+ );+return {userIsControlling,setUserIsControlling,@@ -369,5 +396,7 @@ export function useCdpInput({handleKeyDown,handleKeyUp,},+ navigate,+ navigateError,};}diff --git a/skyvern-frontend/src/routes/workflows/components/BulkActionBar.tsx b/skyvern-frontend/src/routes/workflows/components/BulkActionBar.tsxindex 3ce7a36e1..ba46dce6a 100644--- a/skyvern-frontend/src/routes/workflows/components/BulkActionBar.tsx+++ b/skyvern-frontend/src/routes/workflows/components/BulkActionBar.tsx@@ -75,6 +75,17 @@ function sanitizeFileName(name: string): string {return name.replace(/[/\\:*?"<>|]/g, "_");}+// zipSync()'s Uint8Array return type carries whatever ArrayBuffer-ish generic+// the active @types/node augments as its default, which isn't always the+// concrete ArrayBuffer Blob's BlobPart expects. Copying into a plain+// ArrayBuffer sidesteps that: ArrayBuffer itself isn't generic, so this is+// valid under any @types/node version without a cast.+function toArrayBuffer(bytes: Uint8Array): ArrayBuffer {+ const buffer = new ArrayBuffer(bytes.byteLength);+ new Uint8Array(buffer).set(bytes);+ return buffer;+}+// Two selected agents can share a title; number repeats so every archive// entry name stays unique.function uniqueFileName(used: Set<string>, title: string, ext: string): string {@@ -269,7 +280,7 @@ function BulkActionBar({}downloadFile(`agents-export-${selectedWorkflows.length}.zip`,- zipSync(entries),+ toArrayBuffer(zipSync(entries)),"application/zip",);}diff --git a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.collapse.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.collapse.test.tsxindex 680710d3d..f27a05043 100644--- a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.collapse.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.collapse.test.tsx@@ -500,3 +500,51 @@ describe("NarrativeView rollup expand affordance", () => {expect(head.getAttribute("aria-expanded")).toBe("false");});});++describe("a stopped block stays inspectable", () => {+ const stoppedTurn = (): TurnNarrativeState => ({+ ...terminalBuildTurn(),+ cancelled: true,+ blocks: [+ {+ ...completedBlock("log_in", "wrb_stopped_log_in"),+ state: "stopped",+ activity: [+ {+ kind: "tool_result",+ text: "opened the sign-in page",+ iteration: 1,+ id: "act-1",+ success: true,+ },+ ],+ },+ ],+ });++ // The collapsed rollup is the default view, so a stopped block has to be+ // named there too — a failed block gets its own section, and dropping the+ // stopped one made it vanish from the summary entirely.+ it("names the stopped block in the collapsed rollup", () => {+ render(<NarrativeView turn={stoppedTurn()} />);++ expect(screen.getByText("Stopped", { selector: "div" })).toBeTruthy();+ expect(screen.getByText("log_in")).toBeTruthy();+ expect(screen.queryByText("Halted")).toBeNull();+ });++ // A stop is not a failure, so the row must not auto-open and shout — but what+ // the block did before the stop still has to be reachable.+ it("can be expanded to reveal what ran before the stop", () => {+ render(<NarrativeView turn={stoppedTurn()} />);++ fireEvent.click(screen.getByRole("button", { name: /Stopped/ }));++ // Not auto-opened: a stop does not shout the way a failure does.+ expect(screen.queryByText("opened the sign-in page")).toBeNull();++ fireEvent.click(screen.getByTitle("Highlight log_in on canvas"));++ expect(screen.getByText("opened the sign-in page")).toBeTruthy();+ });+});diff --git a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.ts b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.tsindex e8e1cb1f6..9678100aa 100644--- a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.test.ts@@ -1,5 +1,7 @@import { describe, expect, it } from "vitest";+import { shouldShowFixCard } from "./cards/FixCard";+import { derivePhases } from "./copilotPhases";import {ActivityEntry,BlockState,@@ -9,8 +11,10 @@ import {computeTurnSummary,condenseActivityEntries,effectiveMode,+ humanizeJudgeText,hydrateHistoryNarrative,hydrateNarrativeFromPayload,+ mapBlockStatus,} from "./narrativeState";import {WorkflowCopilotBlockProgressUpdate,@@ -291,7 +295,7 @@ describe("applyNarrativeEvent — block_progress", () => {["failed", "failed"],["terminated", "failed"],["timed_out", "failed"],- ["canceled", "failed"],+ ["canceled", "stopped"],["skipped", "skipped"],["queued", "queued"],["something_new", "queued"],@@ -1648,3 +1652,252 @@ describe("applyNarrativeEvent — terminal adjudication on live frames", () => {expect(s.verifiedSuccess).toBeNull();});});++describe("humanizeJudgeText", () => {+ // Verbatim shape of completion.py::_outcome_unverified_reason.+ const JUDGE_REASON =+ "The run completed but did not demonstrate the goal outcome(s). " ++ "Missing evidence: the number of Customer Agents is known. " ++ "Add or fix the block that produces the missing outcome evidence, then re-run.";++ // What actually reaches the client: run_outcome_display_reason truncates display_reason to+ // _DISPLAY_REASON_MAX_CHARS (160), so the trailing instruction always arrives cut mid-word.+ const TRUNCATED_JUDGE_REASON = JUDGE_REASON.slice(0, 160);++ it("rewrites the truncated verdict the backend actually sends", () => {+ expect(TRUNCATED_JUDGE_REASON).toHaveLength(160);+ expect(TRUNCATED_JUDGE_REASON.endsWith("produces the ")).toBe(true);++ expect(humanizeJudgeText(TRUNCATED_JUDGE_REASON)).toBe(+ "The run finished but didn't produce what you asked for: the number of Customer Agents is known.",+ );+ });++ it("still rewrites the untruncated verdict", () => {+ expect(humanizeJudgeText(JUDGE_REASON)).toBe(+ "The run finished but didn't produce what you asked for: the number of Customer Agents is known.",+ );+ });++ it("strips the instruction wherever the 160-char cut lands inside it", () => {+ const instruction =+ " Add or fix the block that produces the missing outcome evidence, then re-run.";+ const head =+ "The run completed but did not demonstrate the goal outcome(s): title check.";+ for (let cut = 0; cut <= instruction.length; cut += 1) {+ const humanized = humanizeJudgeText(head + instruction.slice(0, cut));+ expect(humanized).not.toContain("Add or fix");+ expect(humanized.startsWith("The run finished but didn't produce")).toBe(+ true,+ );+ }+ });++ it("rewrites the verdict where the backend appends it to the closing message", () => {+ const closing = `I ran the workflow, but I could not confirm the goal was met. Reason: ${TRUNCATED_JUDGE_REASON}`;+ const humanized = humanizeJudgeText(closing);++ expect(humanized).toContain("I ran the workflow, but I could not confirm");+ expect(humanized).not.toContain("did not demonstrate the goal outcome");+ expect(humanized).not.toContain("Add or fix");+ });++ it("leaves ordinary assistant prose alone, even when it ends like the instruction", () => {+ // The trailing-prefix scan must never reach free prose: these reach FixCard and+ // RollupCard headlines, which are not judge text.+ for (const prose of [+ "Choose option A",+ "Click the button labelled Add",+ "Add or fix the selector yourself",+ ]) {+ expect(humanizeJudgeText(prose)).toBe(prose);+ }+ });++ it("strips the instruction when the backend appends Evidence after it", () => {+ // terminal_envelope.py appends " Evidence: <blocker_reason>" after the Reason sentence.+ const withEvidence =+ "I could not confirm the goal was met. Reason: " ++ JUDGE_REASON ++ " Evidence: the login form never submitted";+ const humanized = humanizeJudgeText(withEvidence);++ expect(humanized).not.toContain("Add or fix");+ expect(humanized).not.toContain("did not demonstrate the goal outcome");+ expect(humanized).toContain("Evidence: the login form never submitted");+ });++ it("passes unrecognized text through untouched", () => {+ const other = "The run stopped because the browser session ended.";+ expect(humanizeJudgeText(other)).toBe(other);+ });++ it("is idempotent, so a re-humanized string is unchanged", () => {+ const once = humanizeJudgeText(TRUNCATED_JUDGE_REASON);+ expect(humanizeJudgeText(once)).toBe(once);+ });+});++describe("a user stop renders as stopped, never as a failure", () => {+ it("maps a canceled block to stopped, keeping the other halt states failed", () => {+ expect(mapBlockStatus("canceled")).toBe("stopped");+ expect(mapBlockStatus("failed")).toBe("failed");+ expect(mapBlockStatus("terminated")).toBe("failed");+ expect(mapBlockStatus("timed_out")).toBe("failed");+ });++ it("records a canceled block as a terminal state so its elapsed pill freezes", () => {+ let s = applyNarrativeEvent(EMPTY_NARRATIVE, turnStart());+ s = applyNarrativeEvent(+ s,+ blockProgress({ block_label: "login", status: "running" }),+ );+ s = applyNarrativeEvent(+ s,+ blockProgress({ block_label: "login", status: "canceled" }),+ );++ const block = s.blocks.find((b) => b.label === "login");+ expect(block?.state).toBe("stopped");+ expect(block?.endedAt).not.toBeNull();+ });++ it("does not treat a stopped block as a failure in the turn summary", () => {+ const summary = computeTurnSummary(+ buildTurn({+ cancelled: true,+ blocks: [+ {+ workflowRunBlockId: "wrb_1",+ label: "login",+ blockType: "task",+ state: "stopped",+ lastSeenIteration: 0,+ activity: [],+ startedAt: "2026-05-25T00:00:01Z",+ endedAt: "2026-05-25T00:00:04Z",+ } as BlockState,+ ],+ }),+ );++ expect(summary.isFail).toBe(false);+ expect(summary.isStopped).toBe(true);+ expect(summary.headline).toBe("Stopped");+ expect(summary.accent).not.toBe("fail");+ expect(summary.stats).toContain("1 stopped");+ });++ it("keeps a stopped block through hydration so a reload does not downgrade it", () => {+ const turn = hydrateNarrativeFromPayload({+ turnId: "turn-1",+ turnIndex: 0,+ mode: "build",+ designStarted: true,+ designEnded: true,+ draft: null,+ blocks: [+ {+ workflowRunBlockId: "wrb_1",+ label: "login",+ blockType: "task",+ state: "stopped",+ lastSeenIteration: 0,+ activity: [],+ startedAt: "2026-05-25T00:00:01Z",+ endedAt: "2026-05-25T00:00:04Z",+ },+ ],+ terminal: "response",+ terminalMessage: "Stopped.",+ narrativeSummary: null,+ priorBlockCount: null,+ designActivity: [],+ startedAt: "2026-05-25T00:00:00Z",+ endedAt: "2026-05-25T00:00:05Z",+ });++ expect(turn?.blocks[0]?.state).toBe("stopped");+ });+});++// The backend stamps a canceled block "failed" and a canceled turn's terminal+// "error" (_BLOCK_STATUS_TO_UI_STATE, agent.py), so these payloads are the+// shape a real cancel actually delivers — not the "stopped" shape the live+// block_progress frames produce.+describe("a real cancel's backend payload still renders neutrally", () => {+ const cancelledPayload = () => ({+ turnId: "turn-1",+ turnIndex: 0,+ mode: "build",+ designStarted: true,+ designEnded: true,+ draft: null,+ blocks: [+ {+ workflowRunBlockId: "wrb_1",+ label: "log_in",+ blockType: "task",+ state: "failed",+ lastSeenIteration: 0,+ activity: [],+ startedAt: "2026-05-25T00:00:01Z",+ endedAt: "2026-05-25T00:00:04Z",+ },+ ],+ terminal: "error",+ terminalMessage: "Cancelled by user.",+ narrativeSummary: null,+ cancelled: true,+ priorBlockCount: null,+ designActivity: [],+ startedAt: "2026-05-25T00:00:00Z",+ endedAt: "2026-05-25T00:00:05Z",+ });++ it("re-reads a backend-failed block on a cancelled turn as stopped", () => {+ const turn = hydrateNarrativeFromPayload(cancelledPayload());+ expect(turn?.blocks[0]?.state).toBe("stopped");+ });++ it("does not brand the settled cancel a failure", () => {+ const turn = hydrateNarrativeFromPayload(cancelledPayload())!;+ const summary = computeTurnSummary(turn);++ expect(summary.isFail).toBe(false);+ expect(summary.headline).not.toBe("Run halted");+ expect(summary.accent).not.toBe("fail");+ });++ it("does not offer to fix a run the user stopped, or redden the rail", () => {+ const turn = hydrateNarrativeFromPayload(cancelledPayload())!;+ expect(shouldShowFixCard(turn)).toBe(false);+ const phases = derivePhases(turn);+ const byId = Object.fromEntries(phases.map((p) => [p.id, p.status]));+ expect(byId.done).toBe("stopped");+ expect(byId.test).toBe("stopped");+ });++ // Stop pressed during the thinking phase, or on a QA turn: nothing to key a+ // block state off, so only the turn's own cancelled flag can tell the truth.+ it("does not read a blockless cancel as a clean success", () => {+ const summary = computeTurnSummary(+ buildTurn({ cancelled: true, blocks: [], draft: null }),+ );++ expect(summary.headline).toBe("Stopped");+ expect(summary.headline).not.toBe("Completed the run");+ expect(summary.glyph).not.toBe("✓");+ expect(summary.accent).not.toBe("ok");+ });++ it("still renders a genuine error turn as a failure", () => {+ const turn = hydrateNarrativeFromPayload({+ ...cancelledPayload(),+ cancelled: false,+ })!;+ expect(turn.blocks[0]?.state).toBe("failed");+ expect(computeTurnSummary(turn).isFail).toBe(true);+ expect(shouldShowFixCard(turn)).toBe(true);+ });+});diff --git a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsxindex 71b9f5d7a..cd8cba388 100644--- a/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/NarrativeView.tsx@@ -18,6 +18,7 @@ import {condenseActivityEntries,effectiveMode,formatElapsed,+ humanizeJudgeText,isBlockOk,isInterimOutcome,latestBlocksByLabel,@@ -37,7 +38,9 @@ function normalizeOutcomeReason(reason: string | null | undefined,): string | null {const trimmed = reason?.trim();- return trimmed && trimmed.length > 0 ? trimmed : null;+ if (!trimmed) return null;+ const humanized = humanizeJudgeText(trimmed);+ return humanized.length > 0 ? humanized : null;}function normalizeOutcomeReasonSearchText(@@ -368,6 +371,7 @@ function FBlockRun({!isInterimNotDemonstrated;const isOk = isBlockOk(block);const isFail = block.state === "failed";+ const isStopped = block.state === "stopped";const isDraft = block.state === "drafted";const accentBorder = isRunning@@ -432,7 +436,10 @@ function FBlockRun({const [userOpen, setUserOpen] = useState<boolean | null>(null);const defaultOpen = isRunning || isFail || (hasActions && !turnEnded);const open = userOpen === null ? defaultOpen : userOpen;- const toggleable = isOk || isOutcomeNotShown || isVerifying || isRanNeutral;+ // A stop stays inspectable but not self-opening: the user knows why it+ // stopped, so it should not demand attention the way a failure does.+ const toggleable =+ isOk || isOutcomeNotShown || isVerifying || isRanNeutral || isStopped;useTick(isRunning);useTick(hasActions && (replayingAction || elapsedReveal < totalMs), 150);const elapsed = formatElapsed(block.startedAt, block.endedAt);@@ -447,9 +454,11 @@ function FBlockRun({? `ran${elapsed ? ` · ${elapsed}` : ""}`: isFail? "halted"- : isDraft- ? "drafted"- : "queued";+ : isStopped+ ? `stopped${elapsed ? ` · ${elapsed}` : ""}`+ : isDraft+ ? "drafted"+ : "queued";const collapsedOutcomeReason = isOutcomeNotShown? normalizeOutcomeReason(block.outcomeReason ?? outcomeReasonFallback): null;@@ -481,6 +490,8 @@ function FBlockRun({"…") : isFail ? ("✕"+ ) : isStopped ? (+ "■") : isRunning ? (<Spinner />) : (@@ -587,7 +598,7 @@ function FBlockRun({!</span><div className="text-[12px] leading-[1.5] text-amber-700 dark:text-amber-200/90">- {block.outcomeReason ??+ {normalizeOutcomeReason(block.outcomeReason) ??"The step ran, but the run did not demonstrate the goal was met."}</div></div>@@ -706,6 +717,8 @@ function phaseGlyph(status: PhaseStatus): ReactNode {return "✓";case "fail":return "✕";+ case "stopped":+ return "■";case "active":return <Spinner />;default:@@ -721,6 +734,8 @@ function phasePuckClasses(status: PhaseStatus): string {return "border-rose-400/60 bg-rose-500/15 text-rose-700 dark:text-rose-300";case "active":return "border-blue-400/60 bg-blue-500/15 text-blue-700 dark:text-blue-300";+ case "stopped":+ return "border-slate-400/60 bg-slate-elevation4 text-muted-foreground";default:return "border-slate-500/60 bg-slate-elevation3 text-slate-600";}@@ -769,7 +784,8 @@ function DraftPlaceholderNote({ turn }: { turn: TurnNarrativeState }) {turn.lastRunOutcome?.verdict === "not_evaluated";const text = priorFailedVerdict? `Draft v${turn.authoringCount + 1} — revising after failed verify: ${- turn.lastRunOutcome?.displayReason ?? "outcome not confirmed"+ normalizeOutcomeReason(turn.lastRunOutcome?.displayReason) ??+ "outcome not confirmed"}`: "Writing the workflow code…";return (@@ -1094,8 +1110,11 @@ function RollupCard({onBlockSelect,uxV1,}: RollupCardProps) {- const closing =- turn.narrativeSummary?.trim() || turn.terminalMessage?.trim() || "";+ // The backend appends the judge's verdict to the closing message, so it needs the+ // same display-layer rewrite the outcome reason gets.+ const closing = humanizeJudgeText(+ turn.narrativeSummary?.trim() || turn.terminalMessage?.trim() || "",+ );const collapsedOutcomeReason = notConfirmedDisplayReason(turn);const truncatedOutcomeReason = collapsedOutcomeReason? truncateOutcomeReason(collapsedOutcomeReason)@@ -1117,6 +1136,7 @@ function RollupCard({const rollupBlocks = latestBlocksByLabel(turn.blocks);const completed = rollupBlocks.filter((b) => isBlockOk(b));const failed = rollupBlocks.filter((b) => b.state === "failed");+ const stopped = rollupBlocks.filter((b) => b.state === "stopped");const showCommit = !summary.isQA && completed.length > 0;const showChecklist = Boolean(uxV1) && showPhaseChecklist(turn);// Expand only earns a chevron when DetailView adds content beyond the head's@@ -1231,6 +1251,39 @@ function RollupCard({</ul></div>) : null}++ {stopped.length > 0 ? (+ <div className="border-t border-white/5 pb-3 pl-[52px] pr-3.5 pt-2.5">+ <div className="mb-1.5 text-[10px] font-bold uppercase tracking-[.06em] text-muted-foreground">+ Stopped+ </div>+ <ul className="m-0 flex list-none flex-col gap-1 p-0">+ {stopped.map((b) => (+ <li+ key={b.label}+ className="flex items-baseline gap-1.5 text-[12px] leading-[1.5] text-tertiary-foreground"+ >+ <span+ className="w-3.5 shrink-0 text-center text-[11px] font-bold text-muted-foreground"+ aria-hidden="true"+ >+ ■+ </span>+ <span+ className={+ uxV1+ ? "text-[11px] text-muted-foreground"+ : "font-mono text-[11px] text-muted-foreground"+ }+ title={uxV1 ? b.label : undefined}+ >+ {uxV1 ? humanizeBlockLabel(b.label) : b.label}+ </span>+ </li>+ ))}+ </ul>+ </div>+ ) : null}</div>);}@@ -1328,7 +1381,9 @@ function DetailView({{turn.terminal && (turn.narrativeSummary || turn.terminalMessage) ? (<div className="whitespace-pre-wrap pl-9 pr-8 text-[13px] leading-[1.55] text-foreground dark:text-slate-200">- {turn.narrativeSummary?.trim() || turn.terminalMessage?.trim()}+ {humanizeJudgeText(+ turn.narrativeSummary?.trim() || turn.terminalMessage?.trim() || "",+ )}</div>) : null}</div>diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.composerControls.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.composerControls.test.tsxindex e3d32b821..f467298e4 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.composerControls.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.composerControls.test.tsx@@ -307,7 +307,7 @@ describe("WorkflowCopilotChat — S4 composer, copilot_ux_v1 on", () => {expect((button as HTMLButtonElement).disabled).toBe(true);const cancel = screen.getByRole("button", {- name: "Cancel queued message",+ name: "Edit queued message",});await act(async () => {fireEvent.click(cancel);@@ -366,7 +366,7 @@ describe("WorkflowCopilotChat — S4 composer, copilot_ux_v1 off (parity)", () =).toBeTruthy();expect(screen.queryByText("Queued")).toBeNull();expect(- screen.queryByRole("button", { name: "Cancel queued message" }),+ screen.queryByRole("button", { name: "Edit queued message" }),).toBeNull();});diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsxindex c04204b35..3e6918b44 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.credentialCard.test.tsx@@ -1064,3 +1064,129 @@ describe("WorkflowCopilotChat — credential card (flag off parity)", () => {expect(screen.queryByText(/Using credential/)).toBeNull();});});++describe("WorkflowCopilotChat — auto-bound receipt chronology", () => {+ const autoBoundResponse = (+ turnId: string,+ turnIndex: number,+ message: string,+ ) => {+ const response = terminalAutoBoundResponse(turnId);+ return {+ ...response,+ message,+ narrative_payload: {+ ...response.narrative_payload,+ turnIndex,+ terminalMessage: message,+ narrativeSummary: message,+ credentialAutoBound: {+ credentialId: "cred_acme_test",+ name: "acme-test",+ },+ },+ };+ };++ const historyMessage = (+ turnId: string,+ turnIndex: number,+ message: string,+ ) => {+ const response = autoBoundResponse(turnId, turnIndex, message);+ return {+ sender: "ai" as const,+ content: message,+ created_at: response.response_time,+ narrative_payload: response.narrative_payload,+ turn_outcome: null,+ };+ };++ async function completeAutoBoundTurn(+ callIndex: number,+ turnId: string,+ message: string,+ ) {+ await submit(`prompt ${callIndex + 1}`);+ await waitFor(() =>+ expect(postStreaming).toHaveBeenCalledTimes(callIndex + 1),+ );+ await act(async () => {+ streamCalls[callIndex]!.onMessage(turnStart(turnId));+ streamCalls[callIndex]!.onMessage(+ autoBoundResponse(turnId, callIndex, message),+ );+ streamCalls[callIndex]!.resolve();+ });+ }++ it("keeps a repeated live receipt on the first turn", async () => {+ flagMap.current = { [COPILOT_UX_V1_FLAG]: true };+ await renderChat();+ await completeAutoBoundTurn(0, "turn-chronology-1", "First turn complete.");+ await completeAutoBoundTurn(+ 1,+ "turn-chronology-2",+ "Second turn complete.",+ );++ const receipts = screen.getAllByText("Using credential 'acme-test'");+ expect(receipts).toHaveLength(1);+ expect(+ screen+ .getByText("First turn complete.")+ .closest('[role="status"]')+ ?.contains(receipts[0]!),+ ).toBe(true);+ expect(+ screen+ .getByText("Second turn complete.")+ .closest('[role="status"]')+ ?.contains(receipts[0]!),+ ).toBe(false);+ });++ it("keeps the surviving scrollback receipt read-only", async () => {+ flagMap.current = { [COPILOT_UX_V1_FLAG]: true };+ await renderChat();+ await completeAutoBoundTurn(0, "turn-readonly-1", "First turn complete.");+ expect(await screen.findByRole("button", { name: "Change" })).toBeTruthy();++ await completeAutoBoundTurn(1, "turn-readonly-2", "Second turn complete.");+ await waitFor(() =>+ expect(screen.queryByRole("button", { name: "Change" })).toBeNull(),+ );+ });++ it("hydrates one receipt at the first persisted position", async () => {+ flagMap.current = { [COPILOT_UX_V1_FLAG]: true };+ historyResponse.data.chat_history = [+ historyMessage("turn-history-1", 0, "First persisted turn."),+ historyMessage("turn-history-2", 1, "Second persisted turn."),+ ];++ await renderChat();+ await waitFor(() =>+ expect(+ screen.queryAllByText("Using credential 'acme-test'"),+ ).toHaveLength(1),+ );++ const receipt = screen.getByText("Using credential 'acme-test'");+ expect(+ screen+ .getByText("First persisted turn.")+ .closest('[role="status"]')+ ?.contains(receipt),+ ).toBe(true);+ expect(+ screen+ .getByText("Second persisted turn.")+ .closest('[role="status"]')+ ?.contains(receipt),+ ).toBe(false);+ expect(screen.queryByRole("button", { name: "Change" })).toBeNull();+ expect(credentialsGets()).toHaveLength(0);+ });+});diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.historyRace.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.historyRace.test.tsxindex 9b70cb5ee..a91b3b5e1 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.historyRace.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.historyRace.test.tsx@@ -341,7 +341,7 @@ describe("WorkflowCopilotChat — queued prompt survives initial history load (i).toBeTruthy(),);expect(- screen.getByRole("button", { name: "Cancel queued message" }),+ screen.getByRole("button", { name: "Edit queued message" }),).toBeTruthy();// History resolves and replaces messages, dropping the queued bubble — the@@ -354,7 +354,7 @@ describe("WorkflowCopilotChat — queued prompt survives initial history load (iscreen.getByText("Prompt queued. Waiting for live browser..."),).toBeTruthy();const cancel = screen.getByRole("button", {- name: "Cancel queued message",+ name: "Edit queued message",});await act(async () => {diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsxindex c50844b8c..e089f8bb6 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.queue.test.tsx@@ -349,7 +349,6 @@ describe("WorkflowCopilotChat — keep the chat live during a turn", () => {await submit("first message");await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));await submit("second message");- expect(textarea().disabled).toBe(true);await act(async () => {fireEvent.keyDown(window, { key: "Escape" });@@ -956,3 +955,104 @@ describe("WorkflowCopilotChat — keep the chat live during a turn", () => {expect(postStreaming).toHaveBeenCalledTimes(1);});});++describe("WorkflowCopilotChat — a stop never replays a queued message", () => {+ it("hands the queued text back to the composer instead of auto-sending it", async () => {+ await renderChat();+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await submit("also add a login step");+ expect(screen.getAllByText("also add a login step")).toHaveLength(1);++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: /Cancel run/ }));+ });+ // The turn ends only after the stop lands — this is the edge that used to+ // drain the queue and start a whole new build turn.+ await completeOldestStream("stopped");++ expect(textarea().value).toBe("also add a login step");+ expect(postStreaming).toHaveBeenCalledTimes(1);+ expect(screen.queryByText("Queued")).toBeNull();+ });++ it("shows a stopping state on the first press so the control is not pressed repeatedly", async () => {+ await renderChat();+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));++ const stop = screen.getByRole("button", { name: /Cancel run/ });+ expect(stop.hasAttribute("disabled")).toBe(false);++ await act(async () => {+ fireEvent.click(stop);+ });++ const stopping = screen.getByRole("button", { name: /Stopping/ });+ expect(stopping.hasAttribute("disabled")).toBe(true);+ expect(cancelPost).toHaveBeenCalledTimes(1);++ // A second press cannot reach the handler, so no second cancel is posted.+ await act(async () => {+ fireEvent.click(stopping);+ });+ expect(cancelPost).toHaveBeenCalledTimes(1);++ await completeOldestStream("stopped");+ await waitFor(() =>+ expect(screen.queryByRole("button", { name: /Stopping/ })).toBeNull(),+ );+ });+});++describe("WorkflowCopilotChat — the composer stays usable while a prompt is parked", () => {+ // A turn parked on a credential/2FA ask holds the stream open, so isLoading+ // never flips and the queued prompt never drains. Disabling the composer on+ // a queued prompt therefore locked the user out exactly when they had the+ // code the copilot was waiting for.+ it("keeps the textarea typable while a prompt is queued", async () => {+ await renderChat();+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await submit("first answer");++ expect(textarea().disabled).toBe(false);+ expect(+ screen.getByPlaceholderText("Type to replace the queued message…"),+ ).toBeTruthy();+ });++ it("does not clobber half-typed composer text when a stop returns the queued one", async () => {+ await renderChat();+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await submit("queued answer");+ // Half-typed replacement, never submitted.+ fireEvent.change(textarea(), {+ target: { value: "half typed replacement" },+ });++ await act(async () => {+ fireEvent.click(screen.getByRole("button", { name: /Cancel run/ }));+ });++ expect(textarea().value).toBe("half typed replacement");+ });++ it("replaces the parked prompt rather than swallowing the second send", async () => {+ await renderChat();+ await submit("build me a workflow");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(1));+ await submit("wrong code 000000");+ await submit("correct code 123456");++ expect(screen.queryByText("wrong code 000000")).toBeNull();+ expect(screen.getAllByText("correct code 123456")).toHaveLength(1);+ // Still exactly one parked prompt, and still no second stream.+ expect(postStreaming).toHaveBeenCalledTimes(1);++ await completeOldestStream("done");+ await waitFor(() => expect(postStreaming).toHaveBeenCalledTimes(2));+ expect(streamCalls[1]!.body.message).toBe("correct code 123456");+ });+});diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsxindex 7abb333f5..db4ada68d 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx@@ -31,6 +31,7 @@ import { useCopilotHeaderStore } from "@/store/useCopilotHeaderStore";import { usePasteSkillHintStore } from "@/store/usePasteSkillHintStore";import { WorkflowCreateYAMLRequest } from "@/routes/workflows/types/workflowYamlTypes";import { WorkflowApiResponse } from "@/routes/workflows/types/workflowTypes";+import { describeRecordedAction } from "@/routes/workflows/workflowBlockUtils";import {isBlockItem,WorkflowRunTimelineItem,@@ -63,6 +64,7 @@ import {WorkflowCopilotAudioUploadResponse,} from "./workflowCopilotTypes";import { WorkflowCopilotHistory } from "./WorkflowCopilotHistory";+import { selectAutoBoundReceiptIndexes } from "./autoBoundReceiptIndexes";import { shouldWaitForLiveBrowser } from "./browserReadiness";import {QueuedPromptReason,@@ -119,12 +121,6 @@ const MAX_TURN_SNAPSHOTS = 20;// they land without hammering the timeline endpoint.const RECORDED_ACTIONS_POLL_INTERVAL_MS = 2500;-function normalizeInline(value: string | null | undefined): string | null {- if (!value) return null;- const trimmed = value.replace(/\s+/g, " ").trim();- return trimmed.length > 0 ? trimmed : null;-}-function recordedActionDurationMs(action: ActionsApiResponse): number | null {const output = action.output;if (!output || typeof output !== "object" || Array.isArray(output)) {@@ -140,11 +136,9 @@ function toRecordedActionSummary(return {actionId: action.action_id,label: getReadableActionType(action.action_type),- summary:- normalizeInline(action.reasoning) ??- normalizeInline(action.text) ??- normalizeInline(action.response) ??- normalizeInline(action.description),+ // The chat has no workflow definition in scope, so rows resolve from the action+ // itself; the run-view timeline additionally matches the definition's step text.+ summary: describeRecordedAction(action, null),durationMs: recordedActionDurationMs(action),failed: action.status === "failed",};@@ -431,6 +425,17 @@ function credentialCardFrameFor(return null;}+// A co-occurring credential ask/pause owns this turn's credential UI and its+// credentialResolutions[turnId] entry; the auto-bind receipt would double up and+// mis-adopt that ask's resolution, so it defers whenever a card frame exists.+function autoBoundReceiptFor(+ message: ChatMessage,+): TurnNarrativeState["credentialAutoBound"] {+ const turn = message.narrative;+ if (message.sender !== "ai" || !turn || turn.turnId === null) return null;+ return credentialCardFrameFor(turn) ? null : turn.credentialAutoBound;+}+function historicalCredentialOutcome(turn: TurnNarrativeState,): CredentialPauseHistorical | undefined {@@ -504,8 +509,8 @@ const MessageItem = memo(<buttontype="button"onClick={queuedStatus.onCancel}- title="Cancel queued message"- aria-label="Cancel queued message"+ title="Edit queued message"+ aria-label="Edit queued message"className="shrink-0 rounded p-0.5 text-muted-foreground hover:bg-accent hover:text-accent-foreground"><Cross2Icon className="h-3 w-3" />@@ -770,6 +775,9 @@ export function WorkflowCopilotChat({const [inputValue, setInputValue] = useState("");const dismissPasteSkillHint = usePasteSkillHintStore((s) => s.dismiss);const [isLoading, setIsLoading] = useState(false);+ // A stop is a round trip to the backend; without this the control stays+ // live-looking and users press it repeatedly.+ const [isStopping, setIsStopping] = useState(false);useTurnActivityChange(isLoading, onTurnActivityChange);const [queuedPrompt, setQueuedPrompt] = useState<QueuedPrompt | null>(null);const [narrative, setNarrative] =@@ -1309,7 +1317,9 @@ export function WorkflowCopilotChat({} = useSpeechToTextField({value: inputValue,onChange: setInputValue,- enabled: isOpen && !queuedPrompt,+ // Dictation follows the textarea: it stays live while a prompt is parked,+ // because that text is now editable and replaceable rather than frozen.+ enabled: isOpen,});const updateQueuedPrompt = useCallback((next: QueuedPrompt | null) => {@@ -1795,15 +1805,56 @@ export function WorkflowCopilotChat({applyHistoryResponse,]);+ // Set by a block's "Generate" arm step so the next send scopes regeneration to that block.+ const blockBuildTargetLabelRef = useRef<string | null>(null);+ const fixOriginPendingRef = useRef(false);+ // True only while a block-build turn is actually in flight (not a turn it queued behind).+ const blockGenInFlightRef = useRef(false);++ // The only disposal path for a queued prompt: its text goes back to the+ // composer as an editable draft. Reads the synchronous ref, not state, so a+ // stop can clear the queue before isLoading flips and the drain effect runs.+ const restoreQueuedPromptToComposer = useCallback(() => {+ const queued = queuedPromptRef.current;+ if (!queued) {+ return;+ }++ updateQueuedPrompt(null);+ // Drop the queued block-build target so it doesn't leak into the next message.+ // The fix-origin signal rode on the discarded prompt; clear the ref too in case+ // a future path set it without queuing.+ blockBuildTargetLabelRef.current = null;+ fixOriginPendingRef.current = false;+ setMessages((prev) => prev.filter((message) => message.id !== queued.id));+ // Text already in the composer is the newer intent — the user was part way+ // through replacing the queued message — so it wins over what comes back.+ setInputValue((current) => (current.trim() ? current : queued.content));+ window.requestAnimationFrame(() => {+ textareaRef.current?.focus();+ adjustTextareaHeight();+ });+ }, [adjustTextareaHeight, updateQueuedPrompt]);+const cancelSend = useCallback(async () => {+ // A stop must never let a queued message auto-fire. Hand its text back to+ // the composer synchronously, before isLoading flips and the drain effect+ // would otherwise send it as a fresh turn.+ restoreQueuedPromptToComposer();+// Capture upfront so the 15s timer below can't latch onto a next turn's controller.const controllerAtCancel = streamingAbortController.current;if (!controllerAtCancel) return;const cancelToken = pendingCancelToken.current;pendingCancelToken.current = null;+ // After the token check, not before: a turn that already finished has no+ // token, and claiming "Stopping…" for a cancel that never goes out would+ // rely on the isLoading reset as its only way back.if (!cancelToken) return;+ setIsStopping(true);+cancelInFlightController.current = controllerAtCancel;const appendCancelledBubble = () => {@@ -1844,28 +1895,15 @@ export function WorkflowCopilotChat({controllerAtCancel.abort();appendCancelledBubble();}- }, [credentialGetter]);+ }, [credentialGetter, restoreQueuedPromptToComposer]);- const cancelQueuedPrompt = useCallback(() => {- if (!queuedPrompt) {- return;+ // The turn ending is the one signal that a stop actually landed — it covers+ // the normal path, the 15s safety-timer abort, and the failed-POST abort.+ useEffect(() => {+ if (!isLoading) {+ setIsStopping(false);}-- updateQueuedPrompt(null);- // Drop the queued block-build target so it doesn't leak into the next message.- // The fix-origin signal rode on the discarded prompt; clear the ref too in case- // a future path set it without queuing.- blockBuildTargetLabelRef.current = null;- fixOriginPendingRef.current = false;- setMessages((prev) =>- prev.filter((message) => message.id !== queuedPrompt.id),- );- setInputValue(queuedPrompt.content);- window.requestAnimationFrame(() => {- textareaRef.current?.focus();- adjustTextareaHeight();- });- }, [adjustTextareaHeight, queuedPrompt, updateQueuedPrompt]);+ }, [isLoading]);useEffect(() => {const handleKeyDown = (event: KeyboardEvent) => {@@ -1873,7 +1911,7 @@ export function WorkflowCopilotChat({return;}if (queuedPrompt) {- cancelQueuedPrompt();+ restoreQueuedPromptToComposer();return;}if (isLoading) {@@ -1885,13 +1923,13 @@ export function WorkflowCopilotChat({return () => {window.removeEventListener("keydown", handleKeyDown);};- }, [cancelQueuedPrompt, cancelSend, isLoading, isOpen, queuedPrompt]);-- // Set by a block's "Generate" arm step so the next send scopes regeneration to that block.- const blockBuildTargetLabelRef = useRef<string | null>(null);- const fixOriginPendingRef = useRef(false);- // True only while a block-build turn is actually in flight (not a turn it queued behind).- const blockGenInFlightRef = useRef(false);+ }, [+ restoreQueuedPromptToComposer,+ cancelSend,+ isLoading,+ isOpen,+ queuedPrompt,+ ]);const handleSend = useCallback(async (messageOverride?: string, options: SendOptions = {}) => {@@ -1926,6 +1964,35 @@ export function WorkflowCopilotChat({messageAudioBlob = messageAudioBlob ?? takeSpeechAudioBlob();}+ if (action === "replace_queued") {+ const queued = queuedPromptRef.current;+ if (!queued) {+ return;+ }+ // New text, so the old prompt's framing must not ride along: the+ // fix-origin flag and the block-build scope both belonged to the+ // message being replaced.+ blockBuildTargetLabelRef.current = null;+ fixOriginPendingRef.current = false;+ updateQueuedPrompt({+ ...queued,+ content: candidate,+ audioBlob: messageAudioBlob,+ fixOrigin: false,+ });+ setMessages((prev) =>+ prev.map((message) =>+ message.id === queued.id+ ? { ...message, content: candidate }+ : message,+ ),+ );+ if (messageOverride === undefined) {+ setInputValue("");+ }+ return;+ }+if (action === "queue_working" || action === "queue_live_browser") {const reason: QueuedPromptReason =action === "queue_working" ? "working" : "live_browser";@@ -2857,13 +2924,20 @@ export function WorkflowCopilotChat({}}, [isOpen, buttonRef, size.width, size.height]);+ const autoBoundReceiptIndexes = useMemo(+ () =>+ selectAutoBoundReceiptIndexes(+ messages.map(+ (message) => autoBoundReceiptFor(message)?.credentialId ?? null,+ ),+ ),+ [messages],+ );+if (!isOpen) {return null;}- // Input stays usable while Copilot works; only a parked queued prompt- // disables it (the message is already captured).- const inputDisabled = Boolean(queuedPrompt);const queuedPromptWaitingStatus =queuedPrompt?.reason === "working"? "Queued — sends when this turn finishes."@@ -2897,10 +2971,11 @@ export function WorkflowCopilotChat({const gateActionable =Boolean(proposedWorkflow) && !isLoading && !isLoadingHistory;const hasComposerText = inputValue.trim().length > 0;- // A live_browser-reason queued prompt parks with no active turn to stop and- // a disabled, emptied textarea — the morph button would otherwise render as- // a live "Send" that's a guaranteed no-op; only the Queued chip's ✕ acts.- const waitingOnQueueOnly = queuedPrompt?.reason === "live_browser";+ // A live_browser-reason queued prompt parks with no active turn to stop, so+ // an empty composer's morph button would render as a guaranteed no-op "Send".+ // With text typed it does act — it rewrites the parked prompt.+ const waitingOnQueueOnly =+ queuedPrompt?.reason === "live_browser" && !hasComposerText;// When the initial history load drops the queued bubble, the composer chip// takes over its live_browser status/Cancel (footer-else-chip).const queuedBubbleOrphaned = Boolean(@@ -2908,13 +2983,17 @@ export function WorkflowCopilotChat({queuedPrompt.reason === "live_browser" &&!messages.some((message) => message.id === queuedPrompt.id),);- const morphButtonLabel = waitingOnQueueOnly- ? "Send disabled — waiting for live browser"- : !isLoading- ? "Send"- : hasComposerText- ? "Queue for next turn"- : "Stop";+ const morphButtonLabel = isStopping+ ? "Stopping…"+ : waitingOnQueueOnly+ ? "Send disabled — waiting for live browser"+ : queuedPrompt && hasComposerText+ ? "Replace queued message"+ : !isLoading+ ? "Send"+ : hasComposerText+ ? "Queue for next turn"+ : "Stop";// Shared between the legacy fused split-button and the S4 mode pill so the// three options never drift between the two composer treatments.const modeMenuItems = (@@ -3308,14 +3387,8 @@ export function WorkflowCopilotChat({turnId === null)return null;- const autoBound = message.narrative.credentialAutoBound;- // A co-occurring credential ask/pause owns this turn's credential UI and its- // credentialResolutions[turnId] entry; the auto-bind receipt would double up and- // mis-adopt that ask's resolution, so it defers whenever a card frame exists.- if (- !autoBound ||- credentialCardFrameFor(message.narrative)- )+ const autoBound = autoBoundReceiptFor(message);+ if (!autoBound || !autoBoundReceiptIndexes.has(index))return null;// The receipt renders on any message (scrollback-safe). Before a Change it shows// the auto-bound credential with a Change picker; after one, the local resolution@@ -3372,7 +3445,7 @@ export function WorkflowCopilotChat({queuedPrompt.id === message.id? {text: queuedPromptWaitingStatus,- onCancel: cancelQueuedPrompt,+ onCancel: restoreQueuedPromptToComposer,}: null}@@ -3588,9 +3661,9 @@ export function WorkflowCopilotChat({<span className="flex-1 truncate">{queuedPromptWaitingStatus}</span><buttontype="button"- onClick={cancelQueuedPrompt}- title="Cancel queued message"- aria-label="Cancel queued message"+ onClick={restoreQueuedPromptToComposer}+ title="Edit queued message"+ aria-label="Edit queued message"className="shrink-0 rounded p-0.5 text-muted-foreground hover:bg-accent hover:text-accent-foreground"><Cross2Icon className="h-3 w-3" />@@ -3617,7 +3690,6 @@ export function WorkflowCopilotChat({isSupported={isSpeechSupported}isListening={isSpeechListening}isHearingSpeech={isSpeechHearing}- disabled={inputDisabled}onToggle={toggleSpeech}className="h-10 w-10 rounded-lg"/>@@ -3626,7 +3698,7 @@ export function WorkflowCopilotChat({ref={setTextareaRef}placeholder={queuedPrompt- ? "Prompt queued..."+ ? "Type to replace the queued message…": isLoading? "Type a message to send next…": isWaitingForLiveBrowser@@ -3639,7 +3711,6 @@ export function WorkflowCopilotChat({onChange={(e) => setInputValue(e.target.value)}onPaste={() => dismissPasteSkillHint()}onKeyDown={handleKeyPress}- disabled={inputDisabled}rows={1}className={copilotUxV1Enabled && copilotV2Enabled@@ -3657,7 +3728,6 @@ export function WorkflowCopilotChat({isSupported={isSpeechSupported}isListening={isSpeechListening}isHearingSpeech={isSpeechHearing}- disabled={inputDisabled}onToggle={toggleSpeech}className="h-8 w-8 rounded-full border-0 bg-transparent"iconClassName="h-3.5 w-3.5"@@ -3671,19 +3741,22 @@ export function WorkflowCopilotChat({><buttontype="button"- disabled={waitingOnQueueOnly}+ disabled={waitingOnQueueOnly || isStopping}+ aria-busy={isStopping}onClick={() =>isLoading && !hasComposerText ? cancelSend() : handleSend()}aria-label={morphButtonLabel}className={cn("flex h-9 w-9 shrink-0 items-center justify-center rounded-lg transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50",- isLoading && !hasComposerText+ isStopping || (isLoading && !hasComposerText)? "bg-slate-elevation4 text-foreground hover:bg-slate-elevation3": "bg-cta text-cta-foreground hover:bg-cta-hover",)}>- {isLoading && !hasComposerText ? (+ {isStopping ? (+ <ReloadIcon className="h-3.5 w-3.5 animate-spin" />+ ) : isLoading && !hasComposerText ? (<StopIcon className="h-3 w-3" />) : (<ArrowUpIcon className="h-4 w-4" />@@ -3694,7 +3767,7 @@ export function WorkflowCopilotChat({) : isLoading && queuedPrompt ? (<><button- onClick={cancelQueuedPrompt}+ onClick={restoreQueuedPromptToComposer}title="Edit queued message"className="flex h-10 items-center justify-center rounded-lg border border-border px-3 text-sm font-medium text-muted-foreground hover:bg-accent hover:text-accent-foreground">@@ -3702,10 +3775,12 @@ export function WorkflowCopilotChat({</button><buttononClick={cancelSend}- title="Cancel run"- className="flex h-10 items-center justify-center rounded-lg bg-destructive px-3 text-sm font-medium text-destructive-foreground hover:bg-destructive/90"+ disabled={isStopping}+ aria-busy={isStopping}+ title={isStopping ? "Stopping…" : "Cancel run"}+ className="flex h-10 items-center justify-center rounded-lg bg-destructive px-3 text-sm font-medium text-destructive-foreground hover:bg-destructive/90 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50">- Cancel run+ {isStopping ? "Stopping…" : "Cancel run"}</button></>) : isLoading ? (@@ -3721,19 +3796,21 @@ export function WorkflowCopilotChat({<buttontype="button"onClick={cancelSend}- title="Cancel run"- className="flex h-10 items-center justify-center rounded-lg bg-destructive px-3 text-sm font-medium text-destructive-foreground hover:bg-destructive/90"+ disabled={isStopping}+ aria-busy={isStopping}+ title={isStopping ? "Stopping…" : "Cancel run"}+ className="flex h-10 items-center justify-center rounded-lg bg-destructive px-3 text-sm font-medium text-destructive-foreground hover:bg-destructive/90 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50">- Cancel run+ {isStopping ? "Stopping…" : "Cancel run"}</button></>) : queuedPrompt ? (<button- onClick={cancelQueuedPrompt}+ onClick={restoreQueuedPromptToComposer}title="Edit queued prompt"- className="flex h-10 items-center justify-center rounded-lg bg-destructive px-4 text-sm font-medium text-destructive-foreground hover:bg-destructive/90"+ className="flex h-10 items-center justify-center rounded-lg border border-border px-4 text-sm font-medium text-muted-foreground hover:bg-accent hover:text-accent-foreground">- Cancel+ Edit queued</button>) : !copilotV2Enabled ? (<buttondiff --git a/skyvern-frontend/src/routes/workflows/copilot/autoBoundReceiptIndexes.test.ts b/skyvern-frontend/src/routes/workflows/copilot/autoBoundReceiptIndexes.test.tsnew file mode 100644index 000000000..fb25f903e--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/copilot/autoBoundReceiptIndexes.test.ts@@ -0,0 +1,57 @@+import { describe, expect, it } from "vitest";++import { selectAutoBoundReceiptIndexes } from "./autoBoundReceiptIndexes";++describe("selectAutoBoundReceiptIndexes", () => {+ it("selects nothing from empty or all-null input", () => {+ expect([...selectAutoBoundReceiptIndexes([])]).toEqual([]);+ expect([...selectAutoBoundReceiptIndexes([null, null])]).toEqual([]);+ });++ it("keeps the first of consecutive repeated credentials", () => {+ expect([+ ...selectAutoBoundReceiptIndexes(["credential-a", "credential-a"]),+ ]).toEqual([0]);+ });++ it("does not let null reset the previous credential", () => {+ expect([+ ...selectAutoBoundReceiptIndexes(["credential-a", null, "credential-a"]),+ ]).toEqual([0]);+ });++ it("lets the first eligible credential claim its own index", () => {+ expect([+ ...selectAutoBoundReceiptIndexes([null, "credential-a", "credential-a"]),+ ]).toEqual([1]);+ });++ it("keeps every transition in an A to B to A sequence", () => {+ expect([+ ...selectAutoBoundReceiptIndexes([+ "credential-a",+ "credential-b",+ "credential-a",+ ]),+ ]).toEqual([0, 1, 2]);+ });++ it("preserves ascending source indexes through a long mixed sequence", () => {+ const credentialIds = Object.freeze([+ null,+ "credential-a",+ "credential-a",+ null,+ "credential-b",+ "credential-b",+ "credential-a",+ null,+ "credential-a",+ "credential-c",+ ]);++ expect([...selectAutoBoundReceiptIndexes(credentialIds)]).toEqual([+ 1, 4, 6, 9,+ ]);+ });+});diff --git a/skyvern-frontend/src/routes/workflows/copilot/autoBoundReceiptIndexes.ts b/skyvern-frontend/src/routes/workflows/copilot/autoBoundReceiptIndexes.tsnew file mode 100644index 000000000..b50e6e84b--- /dev/null+++ b/skyvern-frontend/src/routes/workflows/copilot/autoBoundReceiptIndexes.ts@@ -0,0 +1,18 @@+// Chat items are immutable in position: a repeat of the credential already bound keeps the+// receipt on its original message, while null (no receipt here) is transparent, not a reset.+export function selectAutoBoundReceiptIndexes(+ credentialIds: ReadonlyArray<string | null>,+): ReadonlySet<number> {+ const indexes = new Set<number>();+ let previousCredentialId: string | null = null;++ credentialIds.forEach((credentialId, index) => {+ if (credentialId === null || credentialId === previousCredentialId) {+ return;+ }+ indexes.add(index);+ previousCredentialId = credentialId;+ });++ return indexes;+}diff --git a/skyvern-frontend/src/routes/workflows/copilot/cards/FixCard.tsx b/skyvern-frontend/src/routes/workflows/copilot/cards/FixCard.tsxindex 586a1da2d..56a9f37c9 100644--- a/skyvern-frontend/src/routes/workflows/copilot/cards/FixCard.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/cards/FixCard.tsx@@ -1,9 +1,14 @@import { ExclamationTriangleIcon } from "@radix-ui/react-icons";-import { TurnNarrativeState } from "../narrativeState";+import { humanizeJudgeText, TurnNarrativeState } from "../narrativeState";// eslint-disable-next-line react-refresh/only-export-componentsexport function shouldShowFixCard(turn: TurnNarrativeState): boolean {+ // Never offer to diagnose a run the user deliberately stopped; a cancel also+ // lands on terminal "error".+ if (turn.cancelled) {+ return false;+ }return (turn.terminal === "error" || turn.blocks.some((b) => b.state === "failed"));@@ -31,10 +36,11 @@ type FixCardProps = {};export function FixCard({ turn, onFix, onExplain }: FixCardProps) {- const headline =+ const headline = humanizeJudgeText(turn.terminalMessage ??- failedBlockError(turn) ??- "The last run hit an error.";+ failedBlockError(turn) ??+ "The last run hit an error.",+ );return (<div className="rounded-lg border border-destructive/40 bg-destructive/10 p-3">diff --git a/skyvern-frontend/src/routes/workflows/copilot/copilotPhases.ts b/skyvern-frontend/src/routes/workflows/copilot/copilotPhases.tsindex 2037bfbab..e35825867 100644--- a/skyvern-frontend/src/routes/workflows/copilot/copilotPhases.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/copilotPhases.ts@@ -249,9 +249,12 @@ export function derivePhases(turn: TurnNarrativeState): PhaseRowModel[] {: chainActive;const isTerminal = turn.terminal !== null;- const isError = turn.terminal === "error";const isCancelled = turn.cancelled === true;+ // A cancelled turn also lands on terminal "error" — that error is the stop+ // itself, so it must not paint the rail red.+ const isError = turn.terminal === "error" && !isCancelled;const anyFailed = latestBlocks.some((b) => b.state === "failed");+ const anyStopped = latestBlocks.some((b) => b.state === "stopped");const anyNotDemonstrated = latestBlocks.some((b) => b.outcome === "not_demonstrated" && !isInterimOutcome(b.outcomeRole),);@@ -273,6 +276,11 @@ export function derivePhases(turn: TurnNarrativeState): PhaseRowModel[] {if (id === "test" && testReached && anyFailed && id !== liveActive) {return "fail";}+ // A stopped block halted the test without failing it, so the rail must not+ // fall through to "done" and claim the run finished.+ if (id === "test" && testReached && anyStopped && id !== liveActive) {+ return "stopped";+ }if (!isTerminal) {return id === liveActive ? "active" : reached(id) ? "done" : "pending";}diff --git a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.tsindex 20e00462d..9fb9b6426 100644--- a/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/narrativeState.ts@@ -71,14 +71,17 @@ export type NarrativeEvent =| CopilotPhaseHintEvent;// Block lifecycle states as observed via block_progress. The bubble groups-// failed-style states (failed, terminated, timed_out, canceled) under one-// chip and treats `skipped` as a separate neutral state.+// failed-style states (failed, terminated, timed_out) under one chip and+// treats `skipped` and `stopped` as separate neutral states. `stopped` is a+// user cancel: it ran and was interrupted, which is not a failure and must+// never be rendered as one.export type BlockUIState =| "queued"| "drafted"| "running"| "completed"| "failed"+ | "stopped"| "skipped";// Recorded per-run outcome verdict, distinct from lifecycle state: a row can@@ -93,6 +96,58 @@ export function isInterimOutcome(role: RunOutcomeRole | undefined): boolean {return role === "interim_build_test";}+// The completion judge's verdict text is a backend log/policy contract, so it is rewritten+// at the display layer rather than at the source. The backend also appends it to the+// assistant's closing message, so both paths run through this. Unrecognized text passes through.+const JUDGE_REWRITES: ReadonlyArray<readonly [RegExp, string]> = [+ [+ /The run completed but did not demonstrate the goal outcome\(s\)\.\s*Missing evidence:\s*/g,+ "The run finished but didn't produce what you asked for: ",+ ],+ [+ /The run completed but did not demonstrate the goal outcome\(s\):\s*/g,+ "The run finished but didn't produce what you asked for: ",+ ],+ [+ /The run completed but did not demonstrate the goal outcome\(s\)\./g,+ "The run finished but didn't produce what you asked for.",+ ],+];++// Repair instruction aimed at the agent; the user is not the one editing blocks. The backend+// truncates display_reason to 160 chars, so it usually arrives cut at an arbitrary point.+const JUDGE_INSTRUCTION =+ "Add or fix the block that produces the missing outcome evidence, then re-run.";++// Only ever reached for text whose judge headline was recognized. The trailing-prefix scan+// would otherwise eat the last word of ordinary prose ("Choose option A" -> "Choose option").+function stripJudgeInstruction(text: string): string {+ const found = text.indexOf(JUDGE_INSTRUCTION);+ if (found !== -1) {+ return text.slice(0, found) + text.slice(found + JUDGE_INSTRUCTION.length);+ }+ // The 160-char budget cut the instruction short, so it always runs to end of string.+ for (let i = 1; i < text.length; i += 1) {+ if (!/\s/.test(text[i - 1]!)) continue;+ if (JUDGE_INSTRUCTION.startsWith(text.slice(i))) {+ return text.slice(0, i);+ }+ }+ return text;+}++export function humanizeJudgeText(text: string): string {+ const rewritten = JUDGE_REWRITES.reduce(+ (result, [pattern, replacement]) => result.replace(pattern, replacement),+ text,+ );+ // Free assistant prose never carries the judge headline, so leave it exactly as written.+ if (rewritten === text) {+ return text;+ }+ return stripJudgeInstruction(rewritten).replace(/ {2,}/g, " ").trim();+}+export interface TerminalEnvelopeFacts {runVerdict: BlockOutcome | null;runDisplayReason: string | null;@@ -666,8 +721,9 @@ export function mapBlockStatus(raw: string): BlockUIState {case "failed":case "terminated":case "timed_out":- case "canceled":return "failed";+ case "canceled":+ return "stopped";case "skipped":return "skipped";case "queued":@@ -788,6 +844,7 @@ export function applyNarrativeEvent(const isTerminal =incomingState === "completed" ||incomingState === "failed" ||+ incomingState === "stopped" ||incomingState === "skipped";// Clear endedAt on retry-back-to-running so the elapsed pill doesn't// show stale "DONE · 2:00" while the block is active again. On each@@ -1214,6 +1271,7 @@ export function hydrateNarrativeFromPayload(s === "running" ||s === "completed" ||s === "failed" ||+ s === "stopped" ||s === "skipped")return s;@@ -1239,18 +1297,33 @@ export function hydrateNarrativeFromPayload(// doesn't show a stuck spinner after the chat is loaded from history.const endedAtIso =typeof payload.endedAt === "string" ? (payload.endedAt as string) : null;+ const cancelled = payload.cancelled === true;const sweptBlocks: BlockState[] = terminal? blocks.map((b) =>b.state === "running"? {...b,- state: terminal === "error" ? "failed" : "completed",+ state: cancelled+ ? "stopped"+ : terminal === "error"+ ? "failed"+ : "completed",endedAt: b.endedAt ?? endedAtIso,}: b,): blocks;+ // The backend stamps a canceled block "failed" and a canceled turn's terminal+ // "error" (`_BLOCK_STATUS_TO_UI_STATE`, agent.py), so the settle frame and+ // every reload would repaint a user's own stop as a failure. `cancelled` is+ // the turn-level truth the same payload already carries.+ const stoppedBlocks: BlockState[] = cancelled+ ? sweptBlocks.map((b) =>+ b.state === "failed" ? { ...b, state: "stopped" as BlockUIState } : b,+ )+ : sweptBlocks;+const priorBlockCount =typeof payload.priorBlockCount === "number"? (payload.priorBlockCount as number)@@ -1262,7 +1335,7 @@ export function hydrateNarrativeFromPayload(turnIndex,mode: mode as TurnNarrativeState["mode"],responseType,- cancelled: payload.cancelled === true,+ cancelled,proposalDisposition,responseKind: parseResponseKind(payload.responseKind),verifiedSuccess:@@ -1273,7 +1346,7 @@ export function hydrateNarrativeFromPayload(designStarted: true,designEnded: true,draft,- blocks: sweptBlocks,+ blocks: stoppedBlocks,designActivity: normalizeActivityEntries(payload.designActivity),terminal,terminalMessage:@@ -1359,6 +1432,7 @@ export interface TurnSummary {accent: "ok" | "fail" | "qa" | "warn";glyph: string;isFail: boolean;+ isStopped: boolean;isQA: boolean;isStoppedWithDraft: boolean;}@@ -1534,8 +1608,19 @@ export function computeTurnSummary(): TurnSummary {const uxV1 = opts.uxV1 ?? false;const rollupBlocks = latestBlocksByLabel(turn.blocks);+ // A cancelled turn's terminal is "error" purely because the user stopped it,+ // so that arm must not brand their own stop a failure.const isFail =- turn.terminal === "error" || rollupBlocks.some((b) => b.state === "failed");+ !turn.cancelled &&+ (turn.terminal === "error" ||+ rollupBlocks.some((b) => b.state === "failed"));+ // A stop halts the turn without failing it — a user cancel, or a budget halt+ // that cancels a block mid-run. It suppresses a success verdict exactly like+ // a failure, but never wears failure's treatment. `turn.cancelled` is load+ // bearing on its own: a stop during the thinking phase, or on a QA turn,+ // touches no block at all and would otherwise read as a clean success.+ const isStopped =+ turn.cancelled || rollupBlocks.some((b) => b.state === "stopped");const mode = effectiveMode(turn);const needsInput = asksUserForInput(turn);const isQA =@@ -1558,11 +1643,11 @@ export function computeTurnSummary((turn.proposalDisposition === "review_untested" ||turn.proposalDisposition === "review_tested" ||(turn.cancelled && turn.proposalDisposition !== "no_proposal"));- const isStoppedWithDraft = hasReviewableDraft && (isFail || turn.cancelled);+ const isStoppedWithDraft = hasReviewableDraft && (isFail || isStopped);// Fail/cancel precedence is absolute: a verdict never upgrades a halt.const adjudicated =- isStoppedWithDraft || isFail+ isStoppedWithDraft || isFail || isStopped? null: adjudicatedSummaryParts(turn,@@ -1582,41 +1667,43 @@ export function computeTurnSummary(? "Stopped with a draft": isFail? "Run halted"- : uxV1- ? needsUntestedProposalReview- ? "Draft needs review"- : needsTestedProposalReview- ? "Workflow ready for review"- : needsInput- ? "Needs your input"- : isQA- ? mode === "refuse"- ? "Declined"- : mode === "clarify"- ? "Needs your input"- : "Answered"- : hasEdited- ? "Applied edits and re-tested"- : hasDrafts- ? "Built and tested the workflow"- : "Completed the run"- : needsInput- ? "Question"- : needsUntestedProposalReview+ : isStopped+ ? "Stopped"+ : uxV1+ ? needsUntestedProposalReview? "Draft needs review": needsTestedProposalReview? "Workflow ready for review"- : isQA- ? mode === "refuse"- ? "Declined"- : mode === "clarify"- ? "Question"- : "Answered"- : hasEdited- ? "Applied edits and re-tested"- : hasDrafts- ? "Built and tested the workflow"- : "Completed the run";+ : needsInput+ ? "Needs your input"+ : isQA+ ? mode === "refuse"+ ? "Declined"+ : mode === "clarify"+ ? "Needs your input"+ : "Answered"+ : hasEdited+ ? "Applied edits and re-tested"+ : hasDrafts+ ? "Built and tested the workflow"+ : "Completed the run"+ : needsInput+ ? "Question"+ : needsUntestedProposalReview+ ? "Draft needs review"+ : needsTestedProposalReview+ ? "Workflow ready for review"+ : isQA+ ? mode === "refuse"+ ? "Declined"+ : mode === "clarify"+ ? "Question"+ : "Answered"+ : hasEdited+ ? "Applied edits and re-tested"+ : hasDrafts+ ? "Built and tested the workflow"+ : "Completed the run";const stats: string[] = [];const turnElapsed = formatElapsed(turn.startedAt, turn.endedAt);@@ -1624,10 +1711,12 @@ export function computeTurnSummary(if (!isQA) {const ok = rollupBlocks.filter((b) => isBlockOk(b)).length;const failed = rollupBlocks.filter((b) => b.state === "failed").length;+ const stopped = rollupBlocks.filter((b) => b.state === "stopped").length;const newBlocks = hasEdited ? 0 : (turn.draft?.blockCount ?? 0);if (ok) stats.push(`${ok} block${ok === 1 ? "" : "s"} ran`);if (newBlocks) stats.push(`${newBlocks} new`);if (failed) stats.push(`${failed} failed`);+ if (stopped) stats.push(`${stopped} stopped`);}const accent = adjudicated@@ -1636,7 +1725,10 @@ export function computeTurnSummary(? "qa": isFail? "fail"- : needsUntestedProposalReview || needsTestedProposalReview || isQA+ : isStopped ||+ needsUntestedProposalReview ||+ needsTestedProposalReview ||+ isQA? "qa": "ok";return {@@ -1651,10 +1743,13 @@ export function computeTurnSummary(? "!": isFail? "✕"- : isQA- ? "✦"- : "✓",+ : isStopped+ ? "■"+ : isQA+ ? "✦"+ : "✓",isFail,+ isStopped,isQA,isStoppedWithDraft,};diff --git a/skyvern-frontend/src/routes/workflows/copilot/sendQueue.test.ts b/skyvern-frontend/src/routes/workflows/copilot/sendQueue.test.tsindex 613417cc2..7e0a51468 100644--- a/skyvern-frontend/src/routes/workflows/copilot/sendQueue.test.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/sendQueue.test.ts@@ -21,9 +21,20 @@ describe("resolveSendAction", () => {expect(resolveSendAction(sendInput({ candidate: " " }))).toBe("noop");});- it("returns noop when a prompt is already queued and this is not a drain", () => {+ // A parked turn (awaiting the user) never ends, so a swallowed second send+ // left the composer permanently dead. Rewriting the parked prompt keeps the+ // one-queued-prompt invariant without the dead end.+ it("rewrites the parked prompt when a second send arrives", () => {expect(resolveSendAction(sendInput({ hasQueuedPrompt: true, isDrain: false })),+ ).toBe("replace_queued");+ });++ it("still returns noop for an empty candidate while a prompt is queued", () => {+ expect(+ resolveSendAction(+ sendInput({ hasQueuedPrompt: true, isDrain: false, candidate: " " }),+ ),).toBe("noop");});diff --git a/skyvern-frontend/src/routes/workflows/copilot/sendQueue.ts b/skyvern-frontend/src/routes/workflows/copilot/sendQueue.tsindex c9ee4f896..3e47c3dfd 100644--- a/skyvern-frontend/src/routes/workflows/copilot/sendQueue.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/sendQueue.ts@@ -9,6 +9,7 @@ export type SendAction =| "send"| "queue_working"| "queue_live_browser"+ | "replace_queued"| "noop";type ResolveSendInput = {@@ -38,8 +39,11 @@ export function resolveSendAction({if (!candidate.trim()) {return "noop";}+ // One queued prompt at a time still holds — a second send rewrites the one+ // that's parked rather than being swallowed, so the composer is never a+ // dead box while a turn is parked waiting on the user.if (hasQueuedPrompt && !isDrain) {- return "noop";+ return "replace_queued";}if (inFlight) {return "queue_working";diff --git a/skyvern-frontend/src/routes/workflows/editor/nestedConditionalSave.test.ts b/skyvern-frontend/src/routes/workflows/editor/nestedConditionalSave.test.tsindex 163db7e07..bca4ead2e 100644--- a/skyvern-frontend/src/routes/workflows/editor/nestedConditionalSave.test.ts+++ b/skyvern-frontend/src/routes/workflows/editor/nestedConditionalSave.test.ts@@ -5,14 +5,23 @@ import { ProxyLocation } from "@/api/types";import type {CodeBlock,ConditionalBlock,+ ForLoopBlock,OutputParameter,WorkflowBlock,WorkflowSettings,} from "../types/workflowTypes";+import {+ type AppNode,+ isWorkflowBlockNode,+ type WorkflowBlockNode,+} from "./nodes";+import { rewireBlockDropInScope } from "./sortable/rewire";+import { TOP_LEVEL_SCOPE } from "./sortable/scope";import {getElements,getWorkflowBlocks,+ getWorkflowSettings,validateWorkflowBlocks,} from "./workflowEditorUtils";@@ -89,6 +98,73 @@ function conditional(};}+function forLoop(+ label: string,+ loopBlocks: Array<WorkflowBlock>,+): ForLoopBlock {+ return {+ label,+ block_type: "for_loop",+ continue_on_failure: false,+ model: null,+ next_block_label: null,+ output_parameter: op(label),+ loop_over: { key: "items" } as never,+ loop_blocks: loopBlocks,+ loop_variable_reference: null,+ complete_if_empty: false,+ data_schema: null,+ };+}++const FINALLY_SETTINGS: WorkflowSettings = {+ ...DEFAULT_SETTINGS,+ finallyBlockLabel: "finally_notify",+};++function finallyConditionalBlocks(): Array<WorkflowBlock> {+ return [+ code("start_step", "decide_update"),+ conditional("decide_update", null, [+ { id: "b1", next: "Save_file_api" },+ { id: "b2", next: "unexpected_outcome" },+ { id: "b3", next: "finally_notify" },+ { id: "b4", next: null, isDefault: true },+ ]),+ code("Save_file_api", "finally_notify"),+ code("unexpected_outcome", "finally_notify"),+ code("finally_notify", null),+ ];+}++function getNodeByLabel(+ nodes: Array<AppNode>,+ label: string,+): WorkflowBlockNode {+ const node = nodes.find(+ (candidate): candidate is WorkflowBlockNode =>+ isWorkflowBlockNode(candidate) && candidate.data.label === label,+ );+ if (!node) {+ throw new Error(`Missing workflow node: ${label}`);+ }+ return node;+}++function routingOf(blocks: ReturnType<typeof getWorkflowBlocks>) {+ return blocks.map((block) => ({+ label: block.label,+ nextBlockLabel: block.next_block_label,+ branches:+ block.block_type === "conditional"+ ? block.branch_conditions.map((branch) => ({+ id: branch.id,+ nextBlockLabel: branch.next_block_label,+ }))+ : null,+ }));+}+/*** SKY-10460: a conditional nested inside another conditional, with a block* inside the inner conditional's branch. The inner branch's next_block_label@@ -156,4 +232,359 @@ describe("nested conditional save round-trip", () => {validateWorkflowBlocks(saved as Array<WorkflowBlock>),).not.toThrow();});++ test("finally block stays outside a conditional without a merge point", () => {+ const { nodes } = getElements(+ finallyConditionalBlocks(),+ FINALLY_SETTINGS,+ true,+ );+ const finallyNode = getNodeByLabel(nodes, "finally_notify");++ expect(finallyNode.parentId).toBeUndefined();+ expect(finallyNode.data.conditionalBranchId).toBeNull();+ expect(finallyNode.data.conditionalLabel).toBeNull();+ expect(finallyNode.data.conditionalNodeId).toBeNull();+ });++ test("finally block stays root-owned when reached from a conditional inside a loop", () => {+ const blocks: Array<WorkflowBlock> = [+ forLoop("loop", [+ conditional("inner_decision", null, [+ { id: "inner-a", next: "inner_terminal" },+ { id: "inner-b", next: null, isDefault: true },+ ]),+ code("inner_terminal", "finally_notify"),+ ]),+ code("finally_notify", null),+ ];++ const { nodes } = getElements(blocks, FINALLY_SETTINGS, false);++ expect(getNodeByLabel(nodes, "finally_notify").parentId).toBeUndefined();+ });++ test("a branch pointing directly at the finally block becomes empty", () => {+ const { nodes, edges } = getElements(+ finallyConditionalBlocks(),+ FINALLY_SETTINGS,+ true,+ );+ const conditionalNode = getNodeByLabel(nodes, "decide_update");+ const startNode = nodes.find(+ (node) => node.type === "start" && node.parentId === conditionalNode.id,+ );+ const adderNode = nodes.find(+ (node) =>+ node.type === "nodeAdder" && node.parentId === conditionalNode.id,+ );+ const branchEdge = edges.find(+ (edge) =>+ (edge.data as { conditionalBranchId?: string | null } | undefined)+ ?.conditionalBranchId === "b3",+ );++ expect(startNode).toBeDefined();+ expect(adderNode).toBeDefined();+ expect(branchEdge).toBeDefined();+ expect(branchEdge?.source).toBe(startNode?.id);+ expect(branchEdge?.target).toBe(adderNode?.id);+ });++ test("finally block and setting survive serialization", () => {+ const { nodes, edges } = getElements(+ finallyConditionalBlocks(),+ FINALLY_SETTINGS,+ true,+ );+ const saved = getWorkflowBlocks(nodes, edges);++ expect(saved.some((block) => block.label === "finally_notify")).toBe(true);+ expect(getWorkflowSettings(nodes).finallyBlockLabel).toBe("finally_notify");+ });++ test("a conditional with a merge point keeps the merge outside its branches", () => {+ const blocks: Array<WorkflowBlock> = [+ code("start_step", "decide_update"),+ conditional("decide_update", "merge", [+ { id: "b1", next: "branch_step" },+ { id: "b2", next: "merge", isDefault: true },+ ]),+ code("branch_step", "merge"),+ code("merge", "finally_notify"),+ code("finally_notify", null),+ ];++ const { nodes } = getElements(blocks, FINALLY_SETTINGS, true);+ const conditionalNode = getNodeByLabel(nodes, "decide_update");++ expect(getNodeByLabel(nodes, "branch_step").parentId).toBe(+ conditionalNode.id,+ );+ expect(getNodeByLabel(nodes, "merge").parentId).toBeUndefined();+ expect(getNodeByLabel(nodes, "finally_notify").parentId).toBeUndefined();+ });++ test("branch collection is unchanged without a finally setting", () => {+ const blocks: Array<WorkflowBlock> = [+ code("start_step", "decide_update"),+ conditional("decide_update", null, [+ { id: "b1", next: "ordinary_terminal" },+ { id: "b2", next: null, isDefault: true },+ ]),+ code("ordinary_terminal", null),+ ];++ const { nodes } = getElements(blocks, DEFAULT_SETTINGS, true);+ const conditionalNode = getNodeByLabel(nodes, "decide_update");+ const terminalNode = getNodeByLabel(nodes, "ordinary_terminal");++ expect(terminalNode.parentId).toBe(conditionalNode.id);+ expect(terminalNode.data.conditionalBranchId).toBe("b1");+ });++ test("the trailing adder skips the finally block even when it is listed first", () => {+ const blocks: Array<WorkflowBlock> = [+ code("finally_notify", null),+ code("start_step", "decide_update"),+ conditional("decide_update", null, [+ { id: "b1", next: "Save_file_api" },+ { id: "b2", next: null, isDefault: true },+ ]),+ code("Save_file_api", "finally_notify"),+ ];++ const { nodes, edges } = getElements(blocks, FINALLY_SETTINGS, true);+ const rootAdder = nodes.find(+ (node) => node.type === "nodeAdder" && !node.parentId,+ );+ const conditionalNode = getNodeByLabel(nodes, "decide_update");+ const finallyNode = getNodeByLabel(nodes, "finally_notify");++ // Array position must not decide the chain tail: the finally block is+ // chained after the real main-chain tail, and the adder follows it.+ expect(+ edges.some(+ (edge) =>+ edge.source === conditionalNode.id && edge.target === finallyNode.id,+ ),+ ).toBe(true);+ expect(+ edges.some(+ (edge) =>+ edge.source === finallyNode.id && edge.target === rootAdder?.id,+ ),+ ).toBe(true);+ expect(+ edges.some(+ (edge) =>+ edge.source === conditionalNode.id && edge.target === rootAdder?.id,+ ),+ ).toBe(false);+ });++ test("an orphan finally block is chained last instead of floating", () => {+ const blocks: Array<WorkflowBlock> = [+ code("start_step", "decide_update"),+ conditional("decide_update", "after_step", [+ { id: "b1", next: null },+ { id: "b2", next: null, isDefault: true },+ ]),+ code("after_step", null),+ code("finally_notify", null),+ ];++ const { nodes, edges } = getElements(blocks, FINALLY_SETTINGS, true);+ const finallyNode = getNodeByLabel(nodes, "finally_notify");+ const afterStep = getNodeByLabel(nodes, "after_step");+ const rootAdder = nodes.find(+ (node) => node.type === "nodeAdder" && !node.parentId,+ );++ const inbound = edges.filter((edge) => edge.target === finallyNode.id);+ expect(inbound).toHaveLength(1);+ expect(inbound[0]?.source).toBe(afterStep.id);+ // Must be indistinguishable from a native chain edge.+ expect(inbound[0]?.type).toBe("edgeWithAddButton");+ expect(+ edges.some(+ (edge) =>+ edge.source === finallyNode.id && edge.target === rootAdder?.id,+ ),+ ).toBe(true);+ });++ test("chaining an orphan finally block does not change what is saved", () => {+ const blocks: Array<WorkflowBlock> = [+ code("start_step", "decide_update"),+ conditional("decide_update", "after_step", [+ { id: "b1", next: null },+ { id: "b2", next: null, isDefault: true },+ ]),+ code("after_step", null),+ code("finally_notify", null),+ ];++ const { nodes, edges } = getElements(blocks, FINALLY_SETTINGS, true);+ const saved = getWorkflowBlocks(nodes, edges);+ const routing = Object.fromEntries(+ saved.map((block) => [block.label, block.next_block_label]),+ );++ expect(routing).toEqual({+ start_step: "decide_update",+ decide_update: "after_step",+ after_step: null,+ finally_notify: null,+ });+ });++ test("an orphan finally block is chained even with no conditional present", () => {+ const blocks: Array<WorkflowBlock> = [+ code("step_a", null),+ code("finally_notify", null),+ ];++ const { nodes, edges } = getElements(blocks, FINALLY_SETTINGS, true);+ const finallyNode = getNodeByLabel(nodes, "finally_notify");+ const stepA = getNodeByLabel(nodes, "step_a");++ expect(+ edges.some(+ (edge) => edge.source === stepA.id && edge.target === finallyNode.id,+ ),+ ).toBe(true);+ // Sequential defaulting must no longer materialize that edge on save.+ const saved = getWorkflowBlocks(nodes, edges);+ expect(+ saved.find((block) => block.label === "step_a")?.next_block_label,+ ).toBeNull();+ });++ test("a real edge into the finally block is not duplicated by a synthetic one", () => {+ const blocks: Array<WorkflowBlock> = [+ code("step_a", "step_b"),+ code("step_b", "finally_notify"),+ code("finally_notify", null),+ ];++ const { nodes, edges } = getElements(blocks, FINALLY_SETTINGS, true);+ const finallyNode = getNodeByLabel(nodes, "finally_notify");+ const stepB = getNodeByLabel(nodes, "step_b");++ const inbound = edges.filter((edge) => edge.target === finallyNode.id);+ expect(inbound).toHaveLength(1);+ expect(inbound[0]?.source).toBe(stepB.id);+ const saved = getWorkflowBlocks(nodes, edges);+ expect(+ saved.find((block) => block.label === "step_b")?.next_block_label,+ ).toBe("finally_notify");+ });++ test("reordering the main chain keeps the finally edge display-only", () => {+ const blocks: Array<WorkflowBlock> = [+ code("step_a", null),+ code("step_b", null),+ code("finally_notify", null),+ ];++ const { nodes, edges } = getElements(blocks, FINALLY_SETTINGS, true);+ const stepA = getNodeByLabel(nodes, "step_a");+ const stepB = getNodeByLabel(nodes, "step_b");++ const rewired = rewireBlockDropInScope({+ nodes,+ edges,+ scope: TOP_LEVEL_SCOPE,+ activeId: stepB.id,+ overId: stepA.id,+ finallyBlockId: getNodeByLabel(nodes, "finally_notify").id,+ });+ expect(rewired).not.toBeNull();++ const saved = getWorkflowBlocks(nodes, rewired!.edges);+ const routing = Object.fromEntries(+ saved.map((block) => [block.label, block.next_block_label]),+ );+ // step_b now leads, and the edge into the finally block stays synthetic:+ // the reordered tail must NOT gain a real next_block_label.+ expect(routing.step_b).toBe("step_a");+ expect(routing.step_a).toBeNull();+ expect(routing.finally_notify).toBeNull();+ });++ test("a workflow whose only block is the finally block still connects to START", () => {+ const { nodes, edges } = getElements(+ [code("finally_notify", null)],+ FINALLY_SETTINGS,+ true,+ );+ const rootStart = nodes.find(+ (node) => node.type === "start" && !node.parentId,+ );+ const finallyNode = getNodeByLabel(nodes, "finally_notify");++ expect(+ edges.some(+ (edge) =>+ edge.source === rootStart?.id && edge.target === finallyNode.id,+ ),+ ).toBe(true);+ });++ test("finally routing is stable through save reload and save", () => {+ const firstLoad = getElements(+ finallyConditionalBlocks(),+ FINALLY_SETTINGS,+ true,+ );+ const firstSave = getWorkflowBlocks(firstLoad.nodes, firstLoad.edges);+ const saveFile = firstSave.find((block) => block.label === "Save_file_api");+ const unexpectedOutcome = firstSave.find(+ (block) => block.label === "unexpected_outcome",+ );+ const conditionalBlock = firstSave.find(+ (block) => block.label === "decide_update",+ );++ expect(saveFile?.next_block_label).toBeNull();+ expect(unexpectedOutcome?.next_block_label).toBeNull();+ expect(conditionalBlock?.block_type).toBe("conditional");+ expect(+ conditionalBlock?.block_type === "conditional"+ ? conditionalBlock.branch_conditions.find(+ (branch) => branch.id === "b3",+ )?.next_block_label+ : undefined,+ ).toBeNull();++ const secondLoad = getElements(+ firstSave as Array<WorkflowBlock>,+ FINALLY_SETTINGS,+ true,+ );+ const rootStart = secondLoad.nodes.find(+ (node) => node.type === "start" && !node.parentId,+ );+ const startStep = getNodeByLabel(secondLoad.nodes, "start_step");+ const conditionalNode = getNodeByLabel(secondLoad.nodes, "decide_update");+ const finallyNode = getNodeByLabel(secondLoad.nodes, "finally_notify");++ expect(rootStart).toBeDefined();+ expect(+ secondLoad.edges.some(+ (edge) => edge.source === rootStart?.id && edge.target === startStep.id,+ ),+ ).toBe(true);+ expect(+ secondLoad.edges.some(+ (edge) =>+ edge.source === startStep.id && edge.target === conditionalNode.id,+ ),+ ).toBe(true);+ expect(finallyNode.parentId).toBeUndefined();++ const secondSave = getWorkflowBlocks(secondLoad.nodes, secondLoad.edges);+ expect(routingOf(secondSave)).toEqual(routingOf(firstSave));+ });});diff --git a/skyvern-frontend/src/routes/workflows/editor/sortable/loadValidation.test.ts b/skyvern-frontend/src/routes/workflows/editor/sortable/loadValidation.test.tsindex 19385d252..4fc42b566 100644--- a/skyvern-frontend/src/routes/workflows/editor/sortable/loadValidation.test.ts+++ b/skyvern-frontend/src/routes/workflows/editor/sortable/loadValidation.test.ts@@ -232,6 +232,55 @@ describe("validateWorkflowBlocks", () => {).toThrow(/Duplicate block label/);});+ test("allows an out-of-band finally block with no inbound edges", () => {+ const blocks: Array<WorkflowBlock> = [+ code("B1", "B2"),+ code("B2", null),+ code("F", null),+ ];++ expect(() => validateWorkflowBlocks(blocks, null, "F")).not.toThrow();+ expect(() => validateWorkflowBlocks(blocks)).toThrow(+ /Disconnected blocks detected: blocks \(B1, F\)/,+ );+ });++ test("ignores an edge targeting the finally block", () => {+ expect(() =>+ validateWorkflowBlocks(+ [code("start", "finally"), code("finally", null)],+ null,+ "finally",+ ),+ ).not.toThrow();+ });++ test("rejects duplicate labels when the duplicated label is the finally block", () => {+ expect(() =>+ validateWorkflowBlocks(+ [code("start", null), code("finally", null), code("finally", null)],+ null,+ "finally",+ ),+ ).toThrow(/Duplicate block label/);+ });++ // Both halves of the getElements pipeline are needed: defaulting must not+ // invent an edge into the finally block, and the validator must strip it.+ test("an all-null chain around a non-last finally block stays valid", () => {+ const blocks = applySequentialDefaulting(+ [code("B1", null), code("F", null), code("B2", null)],+ "F",+ );++ expect(blocks.map((b) => [b.label, b.next_block_label])).toEqual([+ ["B1", "B2"],+ ["F", null],+ ["B2", null],+ ]);+ expect(() => validateWorkflowBlocks(blocks, null, "F")).not.toThrow();+ });+test("rejects dangling next_block_label", () => {expect(() =>validateWorkflowBlocks([code("B1", "DOES_NOT_EXIST")]),@@ -261,24 +310,18 @@ describe("validateWorkflowBlocks", () => {});test("recurses into loop_blocks and reports the failing nesting level", () => {- const innerBad: Array<WorkflowBlock> = [code("L1", "L2"), code("L2", "L1")];- const loop: ForLoopBlock = {- label: "FOR1",- block_type: "for_loop",- continue_on_failure: false,- model: null,- next_block_label: null,- output_parameter: op("FOR1"),- loop_over: { key: "items" } as never,- loop_blocks: innerBad,- loop_variable_reference: null,- complete_if_empty: false,- data_schema: null,- };+ const loop = forLoop("FOR1", [code("L1", "L2"), code("L2", "L1")]);expect(() => validateWorkflowBlocks([loop])).toThrow(/Circular reference.+inside loop FOR1|inside loop FOR1.+cycle/,);});++ test("still recurses into loop_blocks when the only block is the finally block", () => {+ const loop = forLoop("FOR1", [code("L1", "L2"), code("L2", "L1")]);+ expect(() => validateWorkflowBlocks([loop], null, "FOR1")).toThrow(+ /inside loop FOR1/,+ );+ });});function conditional(diff --git a/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts b/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.tsindex 50dc2b868..8716a93d5 100644--- a/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts+++ b/skyvern-frontend/src/routes/workflows/editor/workflowEditorUtils.ts@@ -183,6 +183,10 @@ import {replaceJinjaReference,type AffectedBlock,} from "./jinjaReferences";+import {+ findFinallyBlockNodeId,+ isBlockFinallyGated,+} from "./sortable/finallyBlockGate";/** If the trimmed expression is exactly one `{{ ... }}` wrapper, use `jinja2_template`; otherwise `prompt`. */export function inferBranchCriteriaTypeFromExpression(@@ -1473,21 +1477,42 @@ export class WorkflowValidationError extends Error {}}+function validateNestedLoopBlocks(blocks: Array<WorkflowBlock>): void {+ for (const block of blocks) {+ if (isNestedLoopWorkflowBlock(block)) {+ validateWorkflowBlocks(block.loop_blocks, block.label);+ }+ }+}+export function validateWorkflowBlocks(blocks: Array<WorkflowBlock>,loopLabel: string | null = null,+ finallyBlockLabel: string | null = null,): void {if (blocks.length === 0) return;const labelToBlock = new Map<string, WorkflowBlock>();+ const seenLabels = new Set<string>();const where = loopLabel ? ` inside loop ${loopLabel}` : "";for (const block of blocks) {- if (labelToBlock.has(block.label)) {+ if (seenLabels.has(block.label)) {throw new WorkflowValidationError(`Duplicate block label detected${where}: ${block.label}`,);}- labelToBlock.set(block.label, block);+ seenLabels.add(block.label);+ // A finally block runs out-of-band, so nothing points at it and it would+ // read as a second root. Mirrors _strip_finally_block_references on the BE.+ if (block.label !== finallyBlockLabel) {+ labelToBlock.set(block.label, block);+ }+ }+ // Only the finally block remained, so there is no graph to check — falling+ // through would report zero roots as a circular reference.+ if (labelToBlock.size === 0) {+ validateNestedLoopBlocks(blocks);+ return;}const adjacency = new Map<string, Set<string>>();@@ -1498,7 +1523,7 @@ export function validateWorkflowBlocks(}const addEdge = (source: string, target: string | null | undefined): void => {- if (!target) return;+ if (!target || target === finallyBlockLabel) return;if (!labelToBlock.has(target)) {throw new WorkflowValidationError(`Block ${source} references unknown next_block_label ${target}${where}`,@@ -1554,15 +1579,12 @@ export function validateWorkflowBlocks();}- for (const block of blocks) {- if (isNestedLoopWorkflowBlock(block)) {- validateWorkflowBlocks(block.loop_blocks, block.label);- }- }+ validateNestedLoopBlocks(blocks);}export function applySequentialDefaulting(blocks: Array<WorkflowBlock>,+ finallyBlockLabel: string | null = null,): Array<WorkflowBlock> {if (blocks.length === 0) return blocks;const hasConditional = blocks.some((b) => b.block_type === "conditional");@@ -1571,11 +1593,23 @@ export function applySequentialDefaulting(// (skip_sequential_defaulting=True) for already-explicit chains while still// upgrading legacy v1 lists where every next_block_label is null.const needsDefaulting = !hasConditional && findChainRoot(blocks) === null;+ // The finally block is never part of the sequential chain: getElements draws+ // its inbound edge as a display-only synthetic edge instead, so defaulting a+ // real edge into it here would materialize that edge on the next save.+ const sequence = blocks.filter(+ (block) => !isBlockFinallyGated(block.label, finallyBlockLabel),+ );+ const nextInSequence = new Map<string, string | null>(+ sequence.map((block, index) => [+ block.label,+ index < sequence.length - 1 ? sequence[index + 1]!.label : null,+ ]),+ );- return blocks.map((block, index) => {+ return blocks.map((block) => {let next = block.next_block_label ?? null;- if (needsDefaulting && next === null && index < blocks.length - 1) {- next = blocks[index + 1]!.label;+ if (needsDefaulting && next === null) {+ next = nextInSequence.get(block.label) ?? null;}if (isNestedLoopWorkflowBlock(block)) {return {@@ -1595,6 +1629,7 @@ function collectLabelsForBranch(startLabel: string | null,stopLabel: string | null,blocksByLabel: Map<string, WorkflowBlock>,+ finallyBlockLabel: string | null,excludeLabels?: Set<string>,): Array<string> {const labels: Array<string> = [];@@ -1602,6 +1637,9 @@ function collectLabelsForBranch(let current = startLabel ?? null;while (current && current !== stopLabel && !visited.has(current)) {+ if (isBlockFinallyGated(current, finallyBlockLabel)) {+ break;+ }if (excludeLabels?.has(current)) {break;}@@ -1676,6 +1714,7 @@ function reconstructConditionalStructure(nodes: Array<AppNode>,labelToNodeMap: Map<string, AppNode>,blocksByLabel: Map<string, WorkflowBlock>,+ finallyBlockLabel: string | null,): { nodes: Array<AppNode>; edges: Array<Edge> } {const newNodes = [...nodes];const newEdges: Array<Edge> = [];@@ -1702,6 +1741,7 @@ function reconstructConditionalStructure(newNodes,labelToNodeMap,blocksByLabel,+ finallyBlockLabel,);// Merge edges from recursive callnewEdges.push(...recursiveResult.edges);@@ -1754,6 +1794,7 @@ function reconstructConditionalStructure(branch.next_block_label,block.next_block_label ?? null,blocksByLabel,+ finallyBlockLabel,excludeLabels,);@@ -1929,7 +1970,9 @@ function findConditionalMergeTargetId(while (iterations < maxIterations) {iterations++;- const nextEdge = allEdges.find((edge) => edge.source === currentSource);+ const nextEdge = allEdges.find(+ (edge) => edge.source === currentSource && !isSyntheticEdge(edge),+ );if (!nextEdge) {return null;}@@ -2000,6 +2043,12 @@ export function edgeWithAddButton(source: string, target: string): Edge {} as Edge;}+// Display-only edges: rendered like any chain edge but never serialized into a+// real next_block_label. Currently only the finally-block chaining edge.+export function isSyntheticEdge(edge: Edge): boolean {+ return (edge.data as { synthetic?: boolean } | undefined)?.synthetic === true;+}+export function startNode(id: string,data: StartNodeData,@@ -2043,7 +2092,10 @@ function getElements(edges: Array<Edge>;validationError: WorkflowValidationError | null;} {- blocks = applySequentialDefaulting(blocks);+ blocks = applySequentialDefaulting(+ blocks,+ settings.finallyBlockLabel ?? null,+ );// In editor / debugger contexts, surface the same shape errors the backend// raises at execute-time. Comparison/visualization views (editable=false)@@ -2054,7 +2106,7 @@ function getElements(let validationError: WorkflowValidationError | null = null;if (editable) {try {- validateWorkflowBlocks(blocks);+ validateWorkflowBlocks(blocks, null, settings.finallyBlockLabel ?? null);} catch (err) {if (err instanceof WorkflowValidationError) {validationError = err;@@ -2068,6 +2120,14 @@ function getElements(const nodes: Array<AppNode> = [];const edges: Array<Edge> = [];const blocksByLabel = buildLabelToBlockMap(blocks);+ // The finally block runs out-of-band, so it never competes as the chain root+ // unless it is the only block in the workflow.+ const nonFinallyBlocks = blocks.filter(+ (block) =>+ !isBlockFinallyGated(block.label, settings.finallyBlockLabel ?? null),+ );+ const blocksForRootDiscovery =+ nonFinallyBlocks.length > 0 ? nonFinallyBlocks : blocks;const startNodeId = nanoid();nodes.push(@@ -2155,6 +2215,7 @@ function getElements(branch.next_block_label,child.next_block_label ?? null,blocksByLabel,+ settings.finallyBlockLabel ?? null,loopExclude,).forEach((label) => branchLabels.add(label));});@@ -2203,6 +2264,7 @@ function getElements(nodes,labelToNode,blocksByLabel,+ settings.finallyBlockLabel ?? null,);nodes.length = 0;nodes.push(...conditionalResult.nodes);@@ -2216,7 +2278,7 @@ function getElements(const cycleBackEdgeLabels = new Set<string>();{const visited = new Set<string>();- const chainRoot = findChainRoot(blocks);+ const chainRoot = findChainRoot(blocksForRootDiscovery);let current = chainRoot?.label ?? null;while (current && !visited.has(current)) {visited.add(current);@@ -2260,7 +2322,7 @@ function getElements(// Connect workflow START to the chain root (computed via adjacency, not// array order - see findChainRoot / Approach B in SKY-9051 design doc).if (blocks.length > 0) {- const chainRoot = findChainRoot(blocks);+ const chainRoot = findChainRoot(blocksForRootDiscovery);const rootNode = chainRoot ? labelToNode.get(chainRoot.label) : null;if (rootNode) {edges.push(edgeWithAddButton(startNodeId, rootNode.id));@@ -2277,7 +2339,7 @@ function getElements(// Find a top-level terminal block: one whose next_block_label is null OR// whose chain edge was skipped as a cycle break, and not inside a// conditional branch. Position in blocks[] is irrelevant.- const lastBlock = blocks.find((block) => {+ const lastBlock = blocksForRootDiscovery.find((block) => {if (block.next_block_label !== null &&!cycleBackEdgeLabels.has(block.label)@@ -2287,12 +2349,28 @@ function getElements(const node = labelToNode.get(block.label);return node && isWorkflowBlockNode(node) && !node.data.conditionalNodeId;});+ const lastNode = lastBlock ? labelToNode.get(lastBlock.label) : undefined;- if (lastBlock) {- const lastNode = labelToNode.get(lastBlock.label);- if (lastNode) {- edges.push(defaultEdge(lastNode.id, adderNodeId));- }+ // The finally block always renders last in the main chain. When no real+ // edge reaches it, chain the main-chain tail into it with a synthetic+ // edge so it never renders detached; `synthetic` keeps it out of+ // serialization (findNextBlockLabel), so saves stay byte-equivalent.+ const finallyNodeId = findFinallyBlockNodeId(+ nodes,+ settings.finallyBlockLabel ?? null,+ );+ const finallyHasRealInboundEdge = finallyNodeId+ ? edges.some((edge) => edge.target === finallyNodeId)+ : false;+ if (finallyNodeId && !finallyHasRealInboundEdge && lastNode) {+ const syntheticEdge = edgeWithAddButton(lastNode.id, finallyNodeId);+ syntheticEdge.data = { ...syntheticEdge.data, synthetic: true };+ edges.push(syntheticEdge);+ }++ const chainTailNodeId = finallyNodeId ?? lastNode?.id;+ if (chainTailNodeId) {+ edges.push(defaultEdge(chainTailNodeId, adderNodeId));}}@@ -2816,8 +2894,12 @@ function findNextBlockLabel(return findNextBlockLabel(conditionalNodeId, nodes, edges);};- // Find the outgoing edge from this node- const outgoingEdge = edges.find((edge) => edge.source === nodeId);+ // Find the outgoing edge from this node. Synthetic edges are display-only+ // (see the finally-block chaining in getElements) and must never become a+ // real next_block_label.+ const outgoingEdge = edges.find(+ (edge) => edge.source === nodeId && !isSyntheticEdge(edge),+ );if (!outgoingEdge) {// No outgoing edge - check if this node is inside a conditional branchdiff --git a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.ts b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.tsindex e80df6476..ff70d3285 100644--- a/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.ts+++ b/skyvern-frontend/src/routes/workflows/hooks/useWorkflowRunTimelineQuery.ts@@ -1,12 +1,7 @@-import { useEffect, useRef } from "react";import { getClient } from "@/api/AxiosClient";import { useCredentialGetter } from "@/hooks/useCredentialGetter";import { statusIsNotFinalized } from "@/routes/tasks/types";-import {- keepPreviousData,- useQuery,- useQueryClient,-} from "@tanstack/react-query";+import { keepPreviousData, useQuery } from "@tanstack/react-query";import { WorkflowRunTimelineItem } from "../types/workflowRunTypes";import { useWorkflowRunWithWorkflowQuery } from "./useWorkflowRunWithWorkflowQuery";import { useGlobalWorkflowsQuery } from "./useGlobalWorkflowsQuery";@@ -17,47 +12,21 @@ import {useActiveOrgId,} from "@/store/ActiveOrgContext";+// Mirrors RECORDED_ACTIONS_POLL_INTERVAL_MS in WorkflowCopilotChat, the other reader of+// this endpoint; the two must not drift apart or their counts disagree on screen.+const RUNNING_TIMELINE_REFETCH_INTERVAL_MS = 2500;+function useWorkflowRunTimelineQuery(options?: { workflowRunId?: string }) {const urlWorkflowRunId = useFirstParam("workflowRunId", "runId");const workflowRunId = options?.workflowRunId ?? urlWorkflowRunId;const credentialGetter = useCredentialGetter();const activeOrgId = useActiveOrgId();const activeOrgQueryKeyScope = getActiveOrgQueryKeyScope(activeOrgId);- const queryClient = useQueryClient();const { data: globalWorkflows } = useGlobalWorkflowsQuery();- const { data: workflowRun, dataUpdatedAt } =- useWorkflowRunWithWorkflowQuery(options);+ const { data: workflowRun } = useWorkflowRunWithWorkflowQuery(options);const workflow = workflowRun?.workflow;const workflowPermanentId = workflow?.workflow_permanent_id;- // Track when workflow run data was last updated- const prevDataUpdatedAtRef = useRef<number>(dataUpdatedAt);-- // Refetch timeline whenever the workflow run query gets new data.- // This keeps the timeline perfectly synchronized with the workflow run status,- // ensuring we never miss updates (e.g., when workflow completes).- useEffect(() => {- if (- dataUpdatedAt !== prevDataUpdatedAtRef.current &&- workflowPermanentId &&- workflowRunId- ) {- queryClient.invalidateQueries({- queryKey: getOrgScopedQueryKey(- ["workflowRunTimeline", workflowPermanentId, workflowRunId],- activeOrgQueryKeyScope,- ),- });- }- prevDataUpdatedAtRef.current = dataUpdatedAt;- }, [- activeOrgQueryKeyScope,- dataUpdatedAt,- workflowPermanentId,- workflowRunId,- queryClient,- ]);-return useQuery<Array<WorkflowRunTimelineItem>>({queryKey: getOrgScopedQueryKey(["workflowRunTimeline", workflowPermanentId, workflowRunId],@@ -83,8 +52,13 @@ function useWorkflowRunTimelineQuery(options?: { workflowRunId?: string }) {}return data;},- // No independent refetchInterval - timeline follows workflow run query's timing- // via the useEffect above that invalidates on dataUpdatedAt changes+ // The header counted off this data used to refetch only when the 5s run query landed,+ // so it trailed the copilot chat's own 2.5s timeline poll and could report no actions+ // while rows were visibly executing. Polling directly replaces that chained refetch.+ refetchInterval:+ workflowRun && statusIsNotFinalized(workflowRun)+ ? RUNNING_TIMELINE_REFETCH_INTERVAL_MS+ : false,placeholderData: keepPreviousData,refetchOnMount:workflowRun && statusIsNotFinalized(workflowRun) ? "always" : false,diff --git a/skyvern-frontend/src/routes/workflows/workflowBlockUtils.test.ts b/skyvern-frontend/src/routes/workflows/workflowBlockUtils.test.tsindex f46f78eb4..0002aacb7 100644--- a/skyvern-frontend/src/routes/workflows/workflowBlockUtils.test.ts+++ b/skyvern-frontend/src/routes/workflows/workflowBlockUtils.test.ts@@ -2,10 +2,12 @@ import { describe, expect, it } from "vitest";import {buildCodeStepsByLabel,+ describeRecordedAction,findCodeStepForLine,getCodeStepPlainText,visitWorkflowBlocks,} from "./workflowBlockUtils";+import type { ActionsApiResponse } from "@/api/types";import type {CodeBlock,CodeBlockStep,@@ -180,3 +182,110 @@ describe("findCodeStepForLine", () => {expect(findCodeStepForLine(steps, 99)).toBeNull();});});++describe("describeRecordedAction", () => {+ // Shaped like the timeline wire payload: WorkflowRunBlock.actions serializes as list[Action],+ // so only base-Action fields exist here. url/keys are subclass-only and never arrive.+ function action(+ overrides: Partial<ActionsApiResponse> = {},+ ): ActionsApiResponse {+ return {+ action_id: "act_1",+ action_type: "click",+ status: "completed",+ task_id: null,+ step_id: null,+ step_order: null,+ action_order: 0,+ confidence_float: null,+ description: "locator.click div:nth-of-type(1) > button:nth-of-type(1)",+ reasoning: null,+ intention: null,+ response: null,+ created_by: null,+ text: null,+ ...overrides,+ } as ActionsApiResponse;+ }++ it("names the action from the definition step it fired from", () => {+ const step: CodeBlockStep = {+ action_type: "click",+ description: "Click the Sign in button",+ line_start: 4,+ };+ expect(describeRecordedAction(action(), step)).toBe(+ "Click the Sign in button",+ );+ });++ it("ignores a step whose kind disagrees, so a drifted outline cannot mislabel", () => {+ const drifted: CodeBlockStep = {+ action_type: "input_text",+ description: "Type into the username field",+ line_start: 4,+ };+ expect(describeRecordedAction(action(), drifted)).toBeNull();+ });++ it("returns null rather than the type name, so a caller showing the label is not doubled", () => {+ expect(describeRecordedAction(action(), null)).toBeNull();+ expect(+ describeRecordedAction(+ action({ action_type: "input_text", text: "" }),+ null,+ ),+ ).toBeNull();+ });++ it("never renders the raw selector, with or without a step", () => {+ for (const step of [null, { action_type: "extract" } as CodeBlockStep]) {+ const text = describeRecordedAction(action(), step) ?? "";+ expect(text).not.toContain("locator.");+ expect(text).not.toContain("nth-of-type");+ }+ });++ it("falls back to the prose a failed recorded action carries in response", () => {+ // The recorder only ever populates response, and only in its except branch.+ expect(+ describeRecordedAction(+ action({ response: "TimeoutError: locator not visible" }),+ null,+ ),+ ).toBe("TimeoutError: locator not visible");+ });++ it("keeps the author's own prompt, which the recorder stores in description", () => {+ expect(+ describeRecordedAction(+ action({+ action_type: "extract",+ description: "Read the order confirmation number",+ }),+ null,+ ),+ ).toBe("Read the order confirmation number");+ });++ it("reads a navigation target out of the recorder trace, the only argument worth showing", () => {+ expect(+ describeRecordedAction(+ action({+ action_type: "goto_url",+ description: "page.goto https://example.com/login",+ }),+ null,+ ),+ ).toBe("Open https://example.com/login");+ });++ it("names a download from file_name, which the base Action does carry", () => {+ expect(+ describeRecordedAction(+ action({ action_type: "download_file", file_name: "invoice.pdf" }),+ null,+ ),+ ).toBe("Download invoice.pdf");+ });+});diff --git a/skyvern-frontend/src/routes/workflows/workflowBlockUtils.ts b/skyvern-frontend/src/routes/workflows/workflowBlockUtils.tsindex 68a100052..23dd03215 100644--- a/skyvern-frontend/src/routes/workflows/workflowBlockUtils.ts+++ b/skyvern-frontend/src/routes/workflows/workflowBlockUtils.ts@@ -1,4 +1,8 @@-import { getReadableActionType } from "@/api/types";+import {+ ActionTypes,+ getReadableActionType,+ type ActionsApiResponse,+} from "@/api/types";import {isNestedLoopWorkflowBlock,@@ -111,3 +115,82 @@ export function findCodeStepForLine() ?? null);}++export function normalizeInlineText(+ value: string | null | undefined,+): string | null {+ const normalized = value?.replace(/\s+/g, " ").trim();+ return normalized ? normalized : null;+}++// The code-block recorder writes `description` two ways: a "<receiver>.<method> <selector>"+// trace for ordinary calls, or the author's own prompt text for page.extract/complete. Only+// the first is machine syntax, and only these three receivers are ever emitted.+const RECORDER_CALL_TEXT = /^(?:locator|page|keyboard)\.[A-Za-z_]+(?:\s|$)/;++export function isRecorderCallText(+ description: string | null | undefined,+): boolean {+ const text = normalizeInlineText(description);+ return text !== null && RECORDER_CALL_TEXT.test(text);+}++/**+ * 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 one+ * recorder argument worth reading.+ *+ * Returns null when nothing beats the action's own type name, so a caller that already renders+ * the type does not print it twice. A raw Playwright selector is never returned.+ */+export function describeRecordedAction(+ action: ActionsApiResponse,+ matchedStep: CodeBlockStep | null,+): string | null {+ // Line numbers drift when a code block is edited between runs, so a step only names+ // this action when their kinds also agree; otherwise a stale outline labels the wrong step.+ if (matchedStep && matchedStep.action_type === action.action_type) {+ const stepText = normalizeInlineText(getCodeStepPlainText(matchedStep));+ if (stepText) {+ return stepText;+ }+ }++ const authored =+ normalizeInlineText(action.reasoning) ??+ normalizeInlineText(action.text) ??+ normalizeInlineText(action.response);+ if (authored) {+ return authored;+ }++ if (!isRecorderCallText(action.description)) {+ const prompt = normalizeInlineText(action.description);+ if (prompt) {+ return prompt;+ }+ }++ // Only base-Action fields are readable here: the timeline serializes actions as+ // `list[Action]`, so subclass-only fields (url, keys) never reach the client.+ if (action.action_type === ActionTypes.DownloadFile) {+ const fileName = normalizeInlineText(action.file_name);+ if (fileName) {+ return `Download ${fileName}`;+ }+ }++ // `_describe` builds "<receiver>.<method> <argument>"; for a navigation the argument is the+ // destination, which is the one recorder argument worth reading.+ if (action.action_type === ActionTypes.GotoUrl) {+ const target = normalizeInlineText(action.description)+ ?.split(/\s+/)+ .slice(1)+ .join(" ");+ if (target) {+ return `Open ${target}`;+ }+ }++ return null;+}diff --git a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimeline.test.tsx b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimeline.test.tsxindex 1b1db07fa..f62ea02de 100644--- a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimeline.test.tsx+++ b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimeline.test.tsx@@ -459,10 +459,10 @@ describe("WorkflowRunTimeline", () => {});describe("timeline header elapsed", () => {- function seed() {+ function seed(totalSteps = 0) {mocks.workflowRun = {status: Status.Completed,- total_steps: 0,+ total_steps: totalSteps,credits_used: 0,cached_credits_used: 0,workflow: {@@ -495,6 +495,24 @@ describe("timeline header elapsed", () => {expect(screen.queryByText(/· \d+m/)).toBeNull();});++ // total_steps counts task steps, which a code-first run never produces, so the+ // chip read "0 steps" beside rows that were visibly executing.+ it("omits the steps chip when the run reports no steps", () => {+ seed();++ renderTimeline(null);++ expect(screen.queryByText(/· 0 steps/)).toBeNull();+ });++ it("still shows the steps chip once the run reports steps", () => {+ seed(3);++ renderTimeline(null);++ expect(screen.getByText("· 3 steps")).toBeTruthy();+ });});describe("timeline block search", () => {diff --git a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimeline.tsx b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimeline.tsxindex 11d8626c0..2a1da19e4 100644--- a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimeline.tsx+++ b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimeline.tsx@@ -422,10 +422,14 @@ function WorkflowRunTimeline({· {numberOfActions} {numberOfActions === 1 ? "action" : "actions"}</span>)}- <span className="text-muted-foreground dark:text-slate-500">- · {workflowRun.total_steps ?? 0}{" "}- {(workflowRun.total_steps ?? 0) === 1 ? "step" : "steps"}- </span>+ {/* total_steps counts task steps only, so a code-first run has none however many+ actions it fires; a literal "0 steps" beside live rows reads as "nothing ran". */}+ {(workflowRun.total_steps ?? 0) > 0 && (+ <span className="text-muted-foreground dark:text-slate-500">+ · {workflowRun.total_steps}{" "}+ {workflowRun.total_steps === 1 ? "step" : "steps"}+ </span>+ )}<spanclassName="text-muted-foreground dark:text-slate-500"title="Credits consumed by this run (live + cached)"diff --git a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.test.tsx b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.test.tsxindex 9bfd09a9f..ac68ad344 100644--- a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.test.tsx+++ b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.test.tsx@@ -422,12 +422,17 @@ describe("WorkflowRunTimelineBlockItem", () => {/>,);+ // Rows read as plain language; the raw recorder trace is demoted to the row tooltip.const gotoRow = screen.getByText(- /page\.goto https:\/\/example\.com · line 1 · 1m 5s/,- );- const clickRow = screen.getByText(- /locator\.click #submit · line 3 · 1\.5s/,+ /Open https:\/\/example\.com · line 1 · 1m 5s/,);+ const clickRow = screen.getByText(/Click · line 3 · 1\.5s/);+ expect(screen.queryByText(/locator\.click/)).toBeNull();+ expect(+ within(container)+ .getAllByRole("button")+ .some((el) => el.getAttribute("title") === "locator.click #submit"),+ ).toBe(true);expect(gotoRow.compareDocumentPosition(clickRow) &Node.DOCUMENT_POSITION_FOLLOWING,@@ -438,7 +443,7 @@ describe("WorkflowRunTimelineBlockItem", () => {expect(within(container).getByText("Click").className).toContain("sr-only");expect(within(container).getByRole("button", {- name: /Goto URL.*page\.goto https:\/\/example\.com/,+ name: /Goto URL.*Open https:\/\/example\.com/,}),).toBeDefined();});@@ -884,7 +889,7 @@ describe("WorkflowRunTimelineBlockItem", () => {] as unknown as WorkflowRunBlock["actions"],});const steps: Array<CodeBlockStep> = [- { action_type: "goto", title: "Open the homepage", line_start: 1 },+ { action_type: "goto_url", title: "Open the homepage", line_start: 1 },{ action_type: "click", title: "Submit the form", line_start: 3 },{ action_type: "extract", title: "Read the result", line_start: 5 },{@@ -1118,7 +1123,7 @@ describe("WorkflowRunTimelineBlockItem", () => {);expect(- screen.getByText(/page\.goto https:\/\/example\.com · line 1/),+ screen.getByText(/Open https:\/\/example\.com · line 1/),).toBeDefined();expect(screen.queryByText("Outline step that should be hidden")).toBeNull();});diff --git a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.tsx b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.tsxindex 8041d35ce..348d982b1 100644--- a/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.tsx+++ b/skyvern-frontend/src/routes/workflows/workflowRun/WorkflowRunTimelineBlockItem.tsx@@ -32,7 +32,12 @@ import {WorkflowRunTimelineItem,} from "../types/workflowRunTypes";import { type CodeBlockStep, WorkflowBlockTypes } from "../types/workflowTypes";-import { findCodeStepForLine } from "../workflowBlockUtils";+import {+ describeRecordedAction,+ findCodeStepForLine,+ isRecorderCallText,+ normalizeInlineText,+} from "../workflowBlockUtils";import {ActionItem,WorkflowRunOverviewActiveElement,@@ -146,11 +151,6 @@ function StatusDot({);}-function normalizeInlineText(value: string | null | undefined): string | null {- const normalized = value?.replace(/\s+/g, " ").trim();- return normalized ? normalized : null;-}-function getActionSummary(action: ActionsApiResponse): string | null {return (normalizeInlineText(action.reasoning) ??@@ -187,6 +187,8 @@ type ActionRowPresentation = {icon: ReactNode;label: string;summary: string | null;+ // Raw recorder text (a Playwright selector); shown on hover, never as the primary line.+ detail: string | null;tone: "default" | "error";};@@ -214,15 +216,13 @@ function getCodeActionRowPresentation(timelineActionIcons[action.action_type]);const { codeLine, durationMs } = getRecordedActionMeta(action);- const stepText =- !isCodeError && matchedStep- ? (normalizeInlineText(matchedStep.title) ??- normalizeInlineText(matchedStep.description))- : null;const parts = [- stepText ??- getActionSummary(action) ??- normalizeInlineText(action.description),+ // 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)+ : (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);@@ -230,6 +230,10 @@ function getCodeActionRowPresentation(icon,label,summary: parts.length > 0 ? parts.join(" · ") : null,+ detail:+ !isCodeError && isRecorderCallText(action.description)+ ? normalizeInlineText(action.description)+ : null,tone: isCodeError ? "error" : "default",};}@@ -511,7 +515,7 @@ function TimelineActionRows({const isActive =isAction(activeItem) && activeItem.action_id === action.action_id;const displayIndex = index + 1;- const { icon, label, summary, tone } = isCodeBlock+ const { icon, label, summary, detail, tone } = isCodeBlock? getCodeActionRowPresentation(action,findCodeStepForLine(@@ -523,6 +527,7 @@ function TimelineActionRows({icon: timelineActionIcons[action.action_type],label: getReadableActionType(action.action_type),summary: getActionSummary(action),+ detail: null,tone: "default" as const,};@@ -548,6 +553,7 @@ function TimelineActionRows({onActionClick({ block, action });}}aria-pressed={isActive}+ title={detail ?? undefined}className="flex min-w-0 flex-1 cursor-pointer items-center gap-1.5 rounded text-left outline-none focus-visible:ring-1 focus-visible:ring-foreground/40"><StatusDotdiff --git a/skyvern-frontend/src/routes/workflows/workflowRun/blockDetail/BlockInspector.tsx b/skyvern-frontend/src/routes/workflows/workflowRun/blockDetail/BlockInspector.tsxindex 049cee617..629399b23 100644--- a/skyvern-frontend/src/routes/workflows/workflowRun/blockDetail/BlockInspector.tsx+++ b/skyvern-frontend/src/routes/workflows/workflowRun/blockDetail/BlockInspector.tsx@@ -11,6 +11,7 @@ import {getReadableActionType,type ActionsApiResponse,} from "@/api/types";+import { isRecorderCallText } from "@/routes/workflows/workflowBlockUtils";import { CopyButton } from "@/components/CopyButton";import { Input } from "@/components/ui/input";import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";@@ -425,6 +426,11 @@ function getActionSummaryFields();pushField(fields, "Reasoning", action.reasoning);pushField(fields, "Intention", action.intention);+ // The row demotes this to a hover title, which keyboard and screen-reader users never get;+ // this panel is the reachable home for it.+ if (isRecorderCallText(action.description)) {+ pushField(fields, "Recorded call", action.description);+ }return fields;}diff --git a/skyvern/browser_extension/extension/README.md b/skyvern/browser_extension/extension/README.mdindex c071b8511..973fdc040 100644--- a/skyvern/browser_extension/extension/README.md+++ b/skyvern/browser_extension/extension/README.md@@ -25,14 +25,10 @@ To set it up manually:skyvern run mcp --browser-extension```-5. Start the recommended one-click pairing flow:-- ```bash- skyvern browser extension-pair- ```-+5. Ask your MCP client to create a browser session. If the extension is not paired, Skyvern automatically opens a secure+ pairing tab. You can also start pairing explicitly with `skyvern browser extension-pair`.6. Click **Approve** in the pairing page, then approve the pairing in the **Skyvern Agent** confirmation tab that opens.- The link expires after two minutes and is single-use, so rerun the command when needed.+ The link expires after two minutes and is single-use, so retry the browser-session request or rerun the command when needed.7. Add any tab you want Skyvern to control to the **Skyvern Controlled** tab group. Dragging a tab into the group grantsaccess and dragging it out revokes access. The popup's **Add to Skyvern Controlled** and**Remove from Skyvern Controlled** buttons perform the same actions.diff --git a/skyvern/browser_extension/extension/pairing_confirm.html b/skyvern/browser_extension/extension/pairing_confirm.htmlindex cf737c3cb..cc3ff14fe 100644--- a/skyvern/browser_extension/extension/pairing_confirm.html+++ b/skyvern/browser_extension/extension/pairing_confirm.html@@ -113,18 +113,10 @@</div><p id="result-message" class="result-message"></p><div id="recovery" class="recovery" hidden>- <span class="recovery-label">Start a new pairing link</span>- <button- id="copy-command"- class="copy-chip"- type="button"- aria-label="Copy skyvern browser extension-pair"- >- <code id="recovery-command"- >skyvern browser extension-pair</code- >- <span id="copy-label" class="copy-label">Copy</span>- </button>+ <span class="recovery-label">+ Retry the browser session request in your MCP client, or run+ <code>skyvern browser extension-pair</code> again.+ </span></div></section>diff --git a/skyvern/browser_extension/extension/pairing_confirm.js b/skyvern/browser_extension/extension/pairing_confirm.jsindex cf8a0d941..81607c69c 100644--- a/skyvern/browser_extension/extension/pairing_confirm.js+++ b/skyvern/browser_extension/extension/pairing_confirm.js@@ -1,5 +1,4 @@const PENDING_PAIRING_STORAGE_KEY = "pendingPairingOffer";-const RECOVERY_COMMAND = "skyvern browser extension-pair";const elements = {question: document.querySelector("#pairing-question"),@@ -13,9 +12,6 @@ const elements = {resultTitle: document.querySelector("#result-title"),resultMessage: document.querySelector("#result-message"),recovery: document.querySelector("#recovery"),- recoveryCommand: document.querySelector("#recovery-command"),- copyButton: document.querySelector("#copy-command"),- copyLabel: document.querySelector("#copy-label"),};let renderGeneration = 0;@@ -80,24 +76,6 @@ function clearResult() {elements.recovery.hidden = true;}-async function copyRecoveryCommand() {- try {- await navigator.clipboard.writeText(RECOVERY_COMMAND);- } catch (_error) {- const range = document.createRange();- range.selectNodeContents(elements.recoveryCommand);- const selection = globalThis.getSelection();- selection.removeAllRanges();- selection.addRange(range);- document.execCommand("copy");- selection.removeAllRanges();- }- elements.copyLabel.textContent = "Copied";- setTimeout(() => {- elements.copyLabel.textContent = "Copy";- }, 1600);-}-async function renderPendingOffer() {const generation = ++renderGeneration;const stored = await chrome.storage.session.get(PENDING_PAIRING_STORAGE_KEY);@@ -112,7 +90,7 @@ async function renderPendingOffer() {showResult({state: "error",title: "Pairing request expired",- message: "Start a new pairing request from your terminal to continue.",+ message: "The secure pairing request expired before approval.",recoverable: true,terminal: true,});@@ -196,10 +174,6 @@ elements.cancelButton.addEventListener("click", () => {});});-elements.copyButton.addEventListener("click", () => {- void copyRecoveryCommand();-});-chrome.storage.onChanged.addListener((changes, areaName) => {if (!operationInProgress &&diff --git a/skyvern/browser_extension/relay.py b/skyvern/browser_extension/relay.pyindex 2b49217d2..a17131321 100644--- a/skyvern/browser_extension/relay.py+++ b/skyvern/browser_extension/relay.py@@ -280,31 +280,6 @@ _PAIR_PAGE_TEMPLATE = """<!doctype html>[data-state="success"] .approval-controls,[data-state="error"] .approval-controls { display: none; }- .recovery { display: grid; gap: .4rem; }- .recovery[hidden] { display: none; }- .recovery-label { color: var(--subtle); font-size: .67rem; font-weight: 650; }-- .copy-chip {- display: flex;- width: fit-content;- max-width: 100%;- min-width: 0;- align-items: center;- justify-content: space-between;- gap: .75rem;- padding: .7rem .8rem;- border: 1px solid var(--border);- border-radius: var(--radius);- color: var(--text);- background: var(--surface-subtle);- font: inherit;- cursor: copy;- }-- .copy-chip:hover { border-color: var(--border-strong); background: #202126; }- .copy-chip code { overflow: hidden; font: 600 .68rem/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; text-overflow: ellipsis; white-space: nowrap; }- .copy-label { flex: none; color: var(--muted); font-size: .66rem; font-weight: 650; }-@keyframes spin { to { transform: rotate(360deg); } }@media (max-width: 34rem) {.page-shell { padding: 1rem; }@@ -338,7 +313,6 @@ _PAIR_PAGE_TEMPLATE = """<!doctype html>body { background: #f5f5f7; }.pairing-card { box-shadow: 0 1rem 2.5rem rgb(32 33 36 / 10%); }- .copy-chip:hover { background: #ececef; }}</style></head>@@ -396,12 +370,12 @@ _PAIR_PAGE_TEMPLATE = """<!doctype html></div></div>- <div class="approval-controls">+ <div id="approval-controls" class="approval-controls"><button id="approve" class="approve-button" type="button"><span class="spinner" aria-hidden="true"></span><span id="approve-label">Approve connection</span></button>- <p class="fine-print">This link expires in 2 minutes and can be used once.</p>+ <p class="fine-print">Secure pairing links expire after 2 minutes and can be used once.</p></div><section id="status" class="status-panel" role="status" aria-live="polite" hidden>@@ -418,13 +392,6 @@ _PAIR_PAGE_TEMPLATE = """<!doctype html><h2 id="status-title" class="status-title"></h2></div><p id="status-message" class="status-message"></p>- <div id="recovery" class="recovery" hidden>- <span class="recovery-label">Start a new pairing link</span>- <button id="copy-command" class="copy-chip" type="button" aria-label="Copy skyvern browser extension-pair">- <code id="recovery-command">skyvern browser extension-pair</code>- <span id="copy-label" class="copy-label">Copy</span>- </button>- </div></section></div></main>@@ -432,12 +399,9 @@ _PAIR_PAGE_TEMPLATE = """<!doctype html><script>(() => {const nonce = location.hash.slice(1);- const command = "skyvern browser extension-pair";+ const approvalControls = document.getElementById("approval-controls");const button = document.getElementById("approve");const buttonLabel = document.getElementById("approve-label");- const copyButton = document.getElementById("copy-command");- const copyLabel = document.getElementById("copy-label");- const recovery = document.getElementById("recovery");const serverAddress = document.getElementById("server-address");const status = document.getElementById("status");const statusTitle = document.getElementById("status-title");@@ -445,20 +409,27 @@ _PAIR_PAGE_TEMPLATE = """<!doctype html>serverAddress.textContent = `127.0.0.1:${location.port}`;- function showState(kind, title, message, showRecovery = false) {+ function showState(kind, title, message) {document.body.dataset.state = kind;status.hidden = false;statusTitle.textContent = title;statusMessage.textContent = message;- recovery.hidden = !showRecovery;+ }++ function showMissingRequest() {+ approvalControls.hidden = true;+ showState(+ "error",+ "Pairing request required",+ "Retry the browser session request in your MCP client, or run skyvern browser extension-pair again.",+ );}function showExpired() {showState("error","Pairing link expired",- "This one-time link is no longer valid. Start a new pairing request to continue.",- true,+ "Retry the browser session request in your MCP client, or run skyvern browser extension-pair again.",);}@@ -466,34 +437,17 @@ _PAIR_PAGE_TEMPLATE = """<!doctype html>showState("error","Skyvern Agent isn’t available",- "Install or enable the Skyvern Agent Chrome extension, then start a new pairing request.",- true,+ "Install or enable the Skyvern Agent Chrome extension, then retry the browser session request.",);}- async function copyRecoveryCommand() {- try {- await navigator.clipboard.writeText(command);- } catch (_error) {- const range = document.createRange();- range.selectNodeContents(document.getElementById("recovery-command"));- const selection = globalThis.getSelection();- selection.removeAllRanges();- selection.addRange(range);- document.execCommand("copy");- selection.removeAllRanges();- }- copyLabel.textContent = "Copied";- setTimeout(() => { copyLabel.textContent = "Copy"; }, 1600);- }-button.addEventListener("click", async () => {button.disabled = true;button.setAttribute("aria-busy", "true");buttonLabel.textContent = "Approving…";document.body.dataset.state = "approving";if (!nonce) {- showExpired();+ showMissingRequest();return;}try {@@ -530,8 +484,7 @@ _PAIR_PAGE_TEMPLATE = """<!doctype html>showState("error","Pairing request wasn’t accepted",- "The extension could not open the confirmation step. Start a new pairing request to try again.",- true,+ "Retry the browser session request in your MCP client, or run skyvern browser extension-pair again.",);}},@@ -540,13 +493,14 @@ _PAIR_PAGE_TEMPLATE = """<!doctype html>showState("error","Pairing couldn’t be completed",- "Check that the local MCP server is running, then start a new pairing request.",- true,+ "Check that the local MCP server is running, then retry the browser session request or run skyvern browser extension-pair again.",);}});- copyButton.addEventListener("click", () => { void copyRecoveryCommand(); });+ if (!nonce) {+ showMissingRequest();+ }})();</script></body>@@ -591,6 +545,33 @@ class ExtensionRelayServer:self._pairing_nonce_created_at = time.monotonic()return nonce+ def get_or_create_pairing_nonce(self) -> str:+ nonce = self._pairing_nonce+ created_at = self._pairing_nonce_created_at+ if nonce is not None and created_at is not None and time.monotonic() - created_at < _PAIRING_NONCE_TTL_SECONDS:+ return nonce+ return self.create_pairing_nonce()++ def _is_interactive_pairing_request(self, request: web.Request) -> bool:+ try:+ origin = urlsplit(request.headers.get("Origin", ""))+ origin_port = origin.port or (80 if origin.scheme == "http" else None)+ except ValueError:+ return False+ return (+ request.content_type == "application/json"+ and origin.scheme == "http"+ and origin.hostname == "127.0.0.1"+ and origin_port == self.bound_port+ and not origin.username+ and not origin.password+ and not origin.path+ and not origin.query+ and not origin.fragment+ and request.headers.get("Sec-Fetch-Mode") == "cors"+ and request.headers.get("Sec-Fetch-Site") == "same-origin"+ )+async def start(self) -> None:if self._runner is not None:return@@ -679,10 +660,11 @@ class ExtensionRelayServer:"Cache-Control": "no-store","Content-Security-Policy": ("default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; "- "img-src data:; connect-src 'self'"+ "img-src data:; connect-src 'self'; frame-ancestors 'none'"),"Referrer-Policy": "no-referrer","X-Content-Type-Options": "nosniff",+ "X-Frame-Options": "DENY",},)@@ -700,6 +682,10 @@ class ExtensionRelayServer:)async def _handle_pair_claim(self, request: web.Request) -> web.Response:+ if not self._is_interactive_pairing_request(request):+ LOG.info("browser_extension_pair_claim", outcome="invalid_source")+ return web.json_response({"error": "invalid_source"}, status=403, headers={"Cache-Control": "no-store"})+active_nonce = self._pairing_noncecreated_at = self._pairing_nonce_created_atself._pairing_nonce = Nonediff --git a/skyvern/browser_extension/runtime.py b/skyvern/browser_extension/runtime.pyindex 775bdaedd..eaf9f152f 100644--- a/skyvern/browser_extension/runtime.py+++ b/skyvern/browser_extension/runtime.py@@ -3,9 +3,12 @@ from __future__ import annotationsimport asyncioimport errnoimport os+import shutil+import subprocess+import sysfrom collections.abc import Awaitable, Callablefrom pathlib import Path-from typing import Protocol+from typing import Any, Protocolimport structlog@@ -40,6 +43,21 @@ _relay_factory: Callable[_adapter_factory: Callable[[VirtualTargetRegistry, ExtensionRelayServer], _Adapter] | None = None+def _open_browser_process(command: list[str], *, windows: bool) -> bool:+ options: dict[str, Any] = {+ "stdin": subprocess.DEVNULL,+ "stdout": subprocess.DEVNULL,+ "stderr": subprocess.DEVNULL,+ "close_fds": True,+ }+ options["creationflags" if windows else "start_new_session"] = 0x00000208 if windows else True+ try:+ subprocess.Popen(command, **options)+ except OSError:+ return False+ return True++class BrowserExtensionRuntime:_instance: BrowserExtensionRuntime | None = None_lock = asyncio.Lock()@@ -105,6 +123,55 @@ class BrowserExtensionRuntime:async def wait_for_extension(self, timeout: float = 10.0) -> bool:return await self._relay.wait_connected(timeout)+ @staticmethod+ def open_extension_url(url: str) -> bool:+ if sys.platform == "darwin":+ executable = shutil.which("open")+ if executable is None:+ return False+ for app_name in ("Google Chrome", "Google Chrome Beta", "Google Chrome Dev", "Google Chrome Canary"):+ try:+ subprocess.run(+ [executable, "-a", app_name, url],+ check=True,+ stdout=subprocess.DEVNULL,+ stderr=subprocess.DEVNULL,+ )+ except (OSError, subprocess.CalledProcessError):+ continue+ return True+ return False++ if sys.platform.startswith("linux"):+ for browser_name in ("google-chrome", "google-chrome-stable", "chromium", "chromium-browser"):+ executable = shutil.which(browser_name)+ if executable is None:+ continue+ if _open_browser_process([executable, url], windows=False):+ return True+ return False++ if sys.platform == "win32":+ roots = (+ os.environ.get("LOCALAPPDATA"),+ os.environ.get("PROGRAMFILES"),+ os.environ.get("PROGRAMFILES(X86)"),+ )+ for root in roots:+ if not root:+ continue+ executable = Path(root) / "Google" / "Chrome" / "Application" / "chrome.exe"+ if not executable.is_file():+ continue+ if _open_browser_process([str(executable), url], windows=True):+ return True+ return False++ def open_pairing_page(self) -> bool:+ nonce = self._relay.get_or_create_pairing_nonce()+ url = f"http://127.0.0.1:{self._relay.bound_port}/pair#{nonce}"+ return self.open_extension_url(url)+async def shutdown(self) -> None:cls = type(self)async with cls._lock:diff --git a/skyvern/cli/commands/browser.py b/skyvern/cli/commands/browser.pyindex 013da72dc..866aec4e5 100644--- a/skyvern/cli/commands/browser.py+++ b/skyvern/cli/commands/browser.py@@ -312,22 +312,7 @@ def _request_pairing_nonce(port: int, token: str) -> str:def _open_pairing_url(url: str) -> bool:- command: list[str] | None = None- if sys.platform == "darwin":- executable = shutil.which("open")- if executable:- command = [executable, url]- elif sys.platform.startswith("linux"):- executable = shutil.which("xdg-open")- if executable:- command = [executable, url]- if command is None:- return False- try:- subprocess.run(command, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)- except (OSError, subprocess.CalledProcessError):- return False- return True+ return BrowserExtensionRuntime.open_extension_url(url)def _launch_extension_pairing(port: int) -> None:diff --git a/skyvern/cli/credential_placeholders.py b/skyvern/cli/credential_placeholders.pynew file mode 100644index 000000000..44ddd6184--- /dev/null+++ b/skyvern/cli/credential_placeholders.py@@ -0,0 +1,37 @@+"""Shared placeholder/sentinel handling for credentials."""++from __future__ import annotations++CREDENTIAL_PLACEHOLDERS: tuple[str, ...] = (+ "",+ "PLACEHOLDER",+ "YOUR_API_KEY",+)++FRONTEND_BUNDLE_PLACEHOLDERS: frozenset[str] = frozenset(+ {+ "__VITE_API_BASE_URL_PLACEHOLDER__",+ "__VITE_WSS_BASE_URL_PLACEHOLDER__",+ "__VITE_ARTIFACT_API_BASE_URL_PLACEHOLDER__",+ "__SKYVERN_API_KEY_PLACEHOLDER__",+ }+)+++def is_placeholder_credential_value(value: str) -> bool:+ """Return true when an env-like credential value is a known unusable placeholder."""+ normalized = value.strip()+ return (+ normalized in CREDENTIAL_PLACEHOLDERS+ or normalized in FRONTEND_BUNDLE_PLACEHOLDERS+ or (normalized.startswith("__") and normalized.endswith("__") and "PLACEHOLDER" in normalized)+ )+++_FRONTEND_API_KEY_PLACEHOLDERS = frozenset(CREDENTIAL_PLACEHOLDERS) - {"PLACEHOLDER"}+++def is_frontend_api_key_placeholder(value: str) -> bool:+ """Return true when an env API key should be treated as missing by doctor checks."""+ normalized = value.strip()+ return normalized in _FRONTEND_API_KEY_PLACEHOLDERSdiff --git a/skyvern/cli/doctor.py b/skyvern/cli/doctor.pyindex 30e1d1c7b..79d215cea 100644--- a/skyvern/cli/doctor.py+++ b/skyvern/cli/doctor.py@@ -26,6 +26,11 @@ from rich.panel import Panelfrom rich.table import Tablefrom skyvern.cli.console import console+from skyvern.cli.credential_placeholders import (+ CREDENTIAL_PLACEHOLDERS,+ is_frontend_api_key_placeholder,+ is_placeholder_credential_value,+)doctor_app = typer.Typer(help="Check Skyvern installation health.")@@ -52,13 +57,6 @@ FRONTEND_STREAMING_MODE_VAR = "VITE_BROWSER_STREAMING_MODE"BACKEND_STREAMING_MODE_VAR = "BROWSER_STREAMING_MODE"ALLOWED_STREAMING_MODES = {"cdp", "vnc"}-FRONTEND_BUNDLE_PLACEHOLDERS = {- "__VITE_API_BASE_URL_PLACEHOLDER__",- "__VITE_WSS_BASE_URL_PLACEHOLDER__",- "__VITE_ARTIFACT_API_BASE_URL_PLACEHOLDER__",- "__SKYVERN_API_KEY_PLACEHOLDER__",-}-# ---------------------------------------------------------------------------# Individual checks@@ -557,8 +555,8 @@ def _check_api_key_consistency() -> CheckResult:frontend_env = Path("skyvern-frontend/.env")backend_key = dotenv_values(backend_env).get("SKYVERN_API_KEY", "") if backend_env.exists() else ""- frontend_raw = dotenv_values(frontend_env).get("VITE_SKYVERN_API_KEY", "") if frontend_env.exists() else ""- frontend_key = "" if frontend_raw in ("", "YOUR_API_KEY") else frontend_raw+ frontend_raw = str(dotenv_values(frontend_env).get("VITE_SKYVERN_API_KEY", "")) if frontend_env.exists() else ""+ frontend_key = "" if is_frontend_api_key_placeholder(frontend_raw) else frontend_rawgenerated_key = _read_generated_credential()legacy_key = _read_legacy_streamlit_credential()@@ -674,12 +672,7 @@ def _docker_compose_available() -> bool:def _is_placeholder_env_value(value: str) -> bool:- normalized = value.strip()- return (- normalized in ("", "PLACEHOLDER", "YOUR_API_KEY")- or normalized in FRONTEND_BUNDLE_PLACEHOLDERS- or (normalized.startswith("__") and normalized.endswith("__") and "PLACEHOLDER" in normalized)- )+ return is_placeholder_credential_value(value)def _validate_frontend_runtime_url_values(values: dict[str, str], source: str) -> list[str]:@@ -997,14 +990,18 @@ def _check_docker_local_auth() -> CheckResult:if not _docker_compose_available():return CheckResult(name="Docker Local Auth", status="ok", detail="not checked (no docker-compose.yml)")- script = r"""+ docker_placeholder_values = json.dumps(CREDENTIAL_PLACEHOLDERS)+ script = (+ r"""import jsonimport osimport urllib.errorimport urllib.requesttoken = os.environ.get("SKYVERN_API_KEY", "").strip()-if not token or token == "PLACEHOLDER":+if not token or token in """+ + docker_placeholder_values+ + r""":print(json.dumps({"status": "missing_container_api_key"}))raise SystemExit(0)@@ -1032,6 +1029,7 @@ except urllib.error.HTTPError as exc:"detail": payload.get("detail") if isinstance(payload, dict) else body or str(exc),}))"""+ )try:result = _run_docker_compose_exec("skyvern", script, timeout=60)diff --git a/skyvern/cli/mcp_tools/__init__.py b/skyvern/cli/mcp_tools/__init__.pyindex 294094986..815914e19 100644--- a/skyvern/cli/mcp_tools/__init__.py+++ b/skyvern/cli/mcp_tools/__init__.py@@ -373,12 +373,11 @@ BAD: One giant block trying to do everything at once.- When in doubt, split into multiple 1.0 blocks (cheaper, more observable)### Caching-MCP-created workflows default to run_with="agent". Set run_with="code" to opt into cached script \-execution: the first run still uses the AI agent (recording a script), but subsequent runs replay \-the cached script (10-100x faster, no LLM calls). For JSON definitions, code_version=2 is also \-injected by default; YAML definitions use the backend schema (currently leaves code_version unset). \-Use script tools to inspect: script_list_for_workflow -> script_get_code -> script_versions -> \-script_fallback_episodes.+When omitted, MCP-created workflows default to run_with="agent" and code_version=2 for both JSON \+and YAML definitions. Set run_with="code" to opt into cached script execution: the first run still \+uses the AI agent (recording a script), but subsequent runs replay the cached script (10-100x faster, \+no LLM calls). Use script tools to inspect: script_list_for_workflow -> script_get_code -> \+script_versions -> script_fallback_episodes.### Block Typesnavigation (most common), extraction, for_loop, conditional, code, text_prompt, action, goto_url, \diff --git a/skyvern/cli/mcp_tools/blocks.py b/skyvern/cli/mcp_tools/blocks.pyindex e10c8017f..20aa0a736 100644--- a/skyvern/cli/mcp_tools/blocks.py+++ b/skyvern/cli/mcp_tools/blocks.py@@ -562,6 +562,11 @@ async def skyvern_block_validate(warnings.append(f"'{block.block_type}' block type is deprecated. Use 'navigation' for actions and 'extraction' for data extraction.")+ if raw.get("block_type") == "code" and "prompt" not in raw:+ warnings.append(+ "Code block omits 'prompt'. Workflow create, or workflow update when adding this code block under a "+ 'new label, will inject the missing default `prompt: ""`; existing code block labels are not migrated.'+ )return make_result(action,data={diff --git a/skyvern/cli/mcp_tools/code_block.py b/skyvern/cli/mcp_tools/code_block.pyindex 1ee5a970e..e44e88e9e 100644--- a/skyvern/cli/mcp_tools/code_block.py+++ b/skyvern/cli/mcp_tools/code_block.py@@ -217,6 +217,7 @@ async def skyvern_code_block_synthesize()data = {+ "prompt": "",# The synthesizer emits wrapper-indented code for the copilot stitch path; this tool's# contract is a standalone block, so dedent before returning."code": textwrap.dedent(synthesized.code),diff --git a/skyvern/cli/mcp_tools/prompts.py b/skyvern/cli/mcp_tools/prompts.pyindex 0d806206f..ab384c721 100644--- a/skyvern/cli/mcp_tools/prompts.py+++ b/skyvern/cli/mcp_tools/prompts.py@@ -406,11 +406,9 @@ or what the data looks like), not the schema.## Caching Considerations-Workflows created via MCP default to AI agent execution (run_with="agent"). For JSON definitions \-code_version=2 is also injected by default; YAML definitions use the backend schema (currently \-leaves code_version unset, so pass `code_version: 2` explicitly in YAML if you want to opt into \-the v2 caching framework). Set run_with="code" on the workflow (or at run time) to opt into cached \-script execution.+When omitted, run_with defaults to "agent" and code_version defaults to 2 for workflows created \+via MCP, for both JSON and YAML definitions. Set run_with="code" on the workflow (or at run time) \+to opt into cached script execution.{CODE_ONLY_SCHEMA_GUIDANCE}### What this means for workflow designdiff --git a/skyvern/cli/mcp_tools/session.py b/skyvern/cli/mcp_tools/session.pyindex 69ec41af0..e5ed60bc9 100644--- a/skyvern/cli/mcp_tools/session.py+++ b/skyvern/cli/mcp_tools/session.py@@ -33,11 +33,18 @@ from ._session import (set_current_session,)-_EXTENSION_NOT_CONNECTED_GUIDANCE = (- "Skyvern browser extension is not connected. Install it: run `skyvern browser extension-path`, open "- "chrome://extensions, enable Developer mode, Load unpacked, and select that directory. Then run "- "`skyvern browser extension-token`, paste the token into the extension popup, and click Connect. Then retry."-)++def _extension_not_connected_guidance(*, pairing_opened: bool) -> str:+ if pairing_opened:+ return (+ "Skyvern browser extension is not connected. A secure pairing tab was opened. Approve the connection, "+ "approve pairing in the Skyvern Agent confirmation tab, and retry."+ )+ return (+ "Skyvern browser extension is not connected and the pairing tab could not be opened automatically. Run "+ "`skyvern browser extension-pair`, approve the connection, approve pairing in the Skyvern Agent confirmation "+ "tab, and retry."+ )def _session_api_key_hash() -> str | None:@@ -145,6 +152,7 @@ async def skyvern_browser_session_create(try:runtime = await BrowserExtensionRuntime.get_or_start()if not await runtime.wait_for_extension(10.0):+ pairing_opened = runtime.open_pairing_page()timer.mark("sdk")return make_result("skyvern_browser_session_create",@@ -152,7 +160,7 @@ async def skyvern_browser_session_create(timing_ms=timer.timing_ms,error=make_error(ErrorCode.BROWSER_NOT_FOUND,- _EXTENSION_NOT_CONNECTED_GUIDANCE,+ _extension_not_connected_guidance(pairing_opened=pairing_opened),"",),)@@ -178,7 +186,7 @@ async def skyvern_browser_session_create(timing_ms=timer.timing_ms,error=make_error(ErrorCode.BROWSER_NOT_FOUND,- _EXTENSION_NOT_CONNECTED_GUIDANCE,+ _extension_not_connected_guidance(pairing_opened=False),"",),)diff --git a/skyvern/cli/mcp_tools/workflow.py b/skyvern/cli/mcp_tools/workflow.pyindex 60d9d0c8b..cf08670ea 100644--- a/skyvern/cli/mcp_tools/workflow.py+++ b/skyvern/cli/mcp_tools/workflow.py@@ -666,28 +666,6 @@ def _inject_missing_top_level_defaults(definition: str, fmt: str, defaults: dictreturn _dump_definition_dict(raw, parsed_format) if changed else definition-def _inject_code_v2_defaults(definition: str, fmt: str) -> str:- """Inject Code 2.0 defaults (code_version=2, run_with=agent) when not explicitly set.-- Only modifies JSON definitions (or auto-detected JSON). YAML is returned unchanged.- """- if fmt == "yaml":- return definition-- try:- raw = json.loads(definition)- except (json.JSONDecodeError, TypeError):- return definition # let _parse_definition handle the error-- changed = False- for key, value in _CODE_V2_DEFAULTS.items():- if key not in raw:- raw[key] = value- changed = True-- return json.dumps(raw) if changed else definition--def _collect_code_block_labels(blocks: Any) -> frozenset[str]:if not isinstance(blocks, list):return frozenset()@@ -1720,8 +1698,8 @@ async def skyvern_workflow_create(One block per step: "navigation" for actions, "extraction" for data. Do NOT use deprecated "task" type.Omit `code_only` or pass null to use this server's default; organization policy may enforce code-only, making rejection intentional.Call skyvern_block_schema() for block types and schemas. Use {{parameter_key}} for input references.- Defaults to AI agent execution (run_with="agent"). For JSON definitions, code_version=2 is also- injected (YAML definitions go through the backend schema, which currently leaves code_version unset).+ When omitted, run_with defaults to "agent" and code_version defaults to 2 for both JSON and YAML+ definitions.Pass run_with="code" to opt into cached script execution. Blocks share a browser session automatically.Give every code block a `prompt`: its plain-language goal, shown as the block's Goal in theeditor. Code blocks that omit `prompt` are defaulted to prompt="" so they render the current@@ -1750,13 +1728,12 @@ async def skyvern_workflow_create(# Default MCP-created workflows to the same editor defaults while preserving# any explicit user-supplied values.- definition = _inject_code_v2_defaults(definition, format)definition = _inject_code_block_prompt_defaults(definition, format, existing_code_labels=frozenset())definition = _inject_code_block_derived_steps(definition, format)definition = _inject_missing_top_level_defaults(definition,format,- {"proxy_location": _DEFAULT_MCP_PROXY_LOCATION},+ {**_CODE_V2_DEFAULTS, "proxy_location": _DEFAULT_MCP_PROXY_LOCATION},)json_def, yaml_def, parse_err = _parse_definition(definition, format)diff --git a/skyvern/config.py b/skyvern/config.pyindex 95f966b08..5c8a35157 100644--- a/skyvern/config.py+++ b/skyvern/config.py@@ -1,10 +1,11 @@import loggingimport osimport platform+from enum import StrEnumfrom pathlib import Pathfrom typing import Any, Literal-from pydantic import AliasChoices, Field, field_validator+from pydantic import AliasChoices, Field, field_validator, model_validatorfrom pydantic_settings import BaseSettings, SettingsConfigDictfrom skyvern import constants@@ -72,6 +73,12 @@ _DEFAULT_ENV_FILES = tuple(LOG = logging.getLogger(__name__)+class CodeBlockMode(StrEnum):+ enabled = "enabled"+ entitlement = "entitlement"+ disabled = "disabled"++class Settings(BaseSettings):model_config = SettingsConfigDict(env_file=_DEFAULT_ENV_FILES, extra="ignore")@@ -691,9 +698,9 @@ class Settings(BaseSettings):ENABLE_LOG_ARTIFACTS: bool = FalseENABLE_SECRET_ARTIFACT_REDACTION: bool = TrueENABLE_SECRET_VISUAL_MASKING: bool = True- # Deployment-level fail-closed override; takes precedence over all CodeBlock entitlements.- DISABLE_CODE_BLOCK_EXECUTION: bool = False- ENABLE_CODE_BLOCK: bool = True+ CODE_BLOCK_MODE: CodeBlockMode = CodeBlockMode.enabled+ DISABLE_CODE_BLOCK_EXECUTION: bool | None = None+ ENABLE_CODE_BLOCK: bool | None = NoneTASK_BLOCKED_SITE_FALLBACK_URL: str = "https://www.google.com"@@ -858,6 +865,39 @@ class Settings(BaseSettings):# script generation settingsWORKFLOW_START_BLOCK_LABEL: str = "__start_block__"+ @model_validator(mode="after")+ def _resolve_legacy_code_block_settings(self) -> "Settings":+ # Must be captured before CODE_BLOCK_MODE is assigned below: pydantic adds an assigned+ # field to model_fields_set immediately, so reading it after would mark every mode explicit.+ explicit_code_block_mode = "CODE_BLOCK_MODE" in self.model_fields_set+ if not explicit_code_block_mode:+ if self.DISABLE_CODE_BLOCK_EXECUTION is True:+ self.CODE_BLOCK_MODE = CodeBlockMode.disabled+ elif self.ENABLE_CODE_BLOCK is False:+ self.CODE_BLOCK_MODE = CodeBlockMode.entitlement+ elif self.ENABLE_CODE_BLOCK is True:+ self.CODE_BLOCK_MODE = CodeBlockMode.enabled++ for legacy_name in ("DISABLE_CODE_BLOCK_EXECUTION", "ENABLE_CODE_BLOCK"):+ if legacy_name not in self.model_fields_set:+ continue+ if explicit_code_block_mode:+ LOG.warning(+ "%s=%r is deprecated and ignored; using CODE_BLOCK_MODE=%s",+ legacy_name,+ getattr(self, legacy_name),+ self.CODE_BLOCK_MODE.value,+ )+ else:+ LOG.warning(+ "%s=%r is deprecated; using CODE_BLOCK_MODE=%s",+ legacy_name,+ getattr(self, legacy_name),+ self.CODE_BLOCK_MODE.value,+ )++ return self+@field_validator("WORKER_STALL_DUMP_SECONDS", mode="before")@classmethoddef _worker_stall_dump_seconds_or_default(cls, value: Any) -> int:diff --git a/skyvern/constants.py b/skyvern/constants.pyindex e65aea516..079b9daca 100644--- a/skyvern/constants.py+++ b/skyvern/constants.py@@ -123,6 +123,11 @@ MAX_FILE_PARSE_INPUT_TOKENS = 256_000MAX_PDF_OCR_PAGES = 50# Max concurrent per-page OCR calls when transcribing a scanned PDF.PDF_OCR_PAGE_CONCURRENCY = 5+# Wall-clock ceiling for one blocking file-parse step (validation, text extraction, page+# rendering). Parse cost tracks document complexity more than raw byte size, so a size cap+# alone cannot bound it; this keeps a pathological document to one failed block instead of+# letting it consume the run's whole activity budget.+FILE_PARSE_STEP_TIMEOUT_SECONDS = 300MAX_IMAGE_MESSAGES = 10SCROLL_AMOUNT_MULTIPLIER = 100EXTRACT_ACTION_SCROLL_AMOUNT = 500 # pixels per scroll action from extract-action promptdiff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.pyindex f4b61a2f4..66fd82150 100644--- a/skyvern/forge/agent_functions.py+++ b/skyvern/forge/agent_functions.py@@ -18,7 +18,7 @@ from cachetools import TTLCachefrom google.oauth2.credentials import Credentialsfrom playwright.async_api import Frame, Page-from skyvern.config import settings+from skyvern.config import CodeBlockMode, settingsfrom skyvern.constants import CUSTOMER_STORAGE_UPLOAD_MAX_BYTES, SKYVERN_ID_ATTRfrom skyvern.core.script_generations.fuzzy_matcher import match_option_exact_or_stem_with_tierfrom skyvern.exceptions import (@@ -2146,7 +2146,7 @@ class AgentFunction:return cleanup_element_tree_funcasync def has_code_block_access(self, organization_id: str | None = None) -> bool:- return settings.ENABLE_CODE_BLOCK and not settings.DISABLE_CODE_BLOCK_EXECUTION+ return settings.CODE_BLOCK_MODE is CodeBlockMode.enabledasync def validate_code_block(self, organization_id: str | None = None) -> None:if not await self.has_code_block_access(organization_id):diff --git a/skyvern/forge/sdk/api/files.py b/skyvern/forge/sdk/api/files.pyindex 496be9a48..a56ce430b 100644--- a/skyvern/forge/sdk/api/files.py+++ b/skyvern/forge/sdk/api/files.py@@ -6,9 +6,10 @@ import reimport shutilimport tempfileimport zipfile+from dataclasses import dataclassfrom http import HTTPStatusfrom pathlib import Path-from typing import TYPE_CHECKING+from typing import TYPE_CHECKING, TypeAliasfrom urllib.parse import parse_qsl, unquote, urlparseimport aiohttp@@ -23,17 +24,25 @@ from skyvern.exceptions import (BlockedHost,DownloadFileMaxSizeExceeded,DownloadFileMaxWaitingTime,+ HttpException,SkyvernHTTPException,)from skyvern.forge import appfrom skyvern.forge.sdk.artifact.signing import parse_artifact_content_url+from skyvern.forge.sdk.browser_action_policy import canonicalize_originfrom skyvern.forge.sdk.core.aiohttp_helper import (SSRFGuardedResolver,+ _url_origin,ssrf_guarded_tcp_connector,strip_cross_origin_redirect_credentials,validate_and_pin_fetch_url,validate_and_pin_redirect_url,)+from skyvern.forge.sdk.core.http_request_authorization import (+ RedirectHopAuthorization,+ RedirectHopAuthorizer,+ authorize_request_hop_once,+)from skyvern.utils.url_validators import (MAX_SAFE_REDIRECTS,SAFE_REDIRECT_STATUS_CODES,@@ -136,6 +145,107 @@ def _raise_download_response_for_status(response: aiohttp.ClientResponse) -> Non)+@dataclass(frozen=True, slots=True)+class GuardedFileRedirect:+ """Internal hop result surfaced only through the authorization dispatcher."""++ location: str+++@dataclass(frozen=True, slots=True)+class GuardedFileResponse:+ """Bounded response returned by :func:`fetch_file_bytes` to a trusted adapter."""++ body: bytes+ content_type: str+ filename: str+++GuardedFileFetchHopResult: TypeAlias = GuardedFileRedirect | GuardedFileResponse+++async def fetch_file_bytes(+ url: str,+ *,+ max_size_mb: int = 100,+ headers: dict[str, str] | None = None,+ filename: str | None = None,+ allowed_redirect_origin: str | None = None,+ authorize_request_hop: RedirectHopAuthorizer[GuardedFileFetchHopResult],+) -> GuardedFileResponse:+ """Fetch a bounded HTTP file through the validated, pinned, per-hop authorization seam.++ This helper accepts HTTP(S) only. It validates and pins each target before invoking+ ``authorize_request_hop``; validation failures therefore consume no approval and perform no+ network request. The body is intentionally bounded because it is returned in memory.+ """+ if not url or not url.strip():+ raise ValueError("Download URL is empty — no file download was triggered by the browser")+ if max_size_mb <= 0:+ raise ValueError("max_size_mb must be greater than zero")++ resolver = SSRFGuardedResolver()+ current_url = await validate_and_pin_fetch_url(url, resolver)+ if canonicalize_origin(current_url) is None:+ raise HttpException(400, "[redacted]", "URL has no browser-canonicalizable HTTP origin")+ if allowed_redirect_origin is not None and _url_origin(current_url) != _url_origin(allowed_redirect_origin):+ raise HttpException(400, "[redacted]", "Cross-origin redirect blocked by policy")++ request_headers = dict(headers or {})+ source_url: str | None = None+ max_size_bytes = max_size_mb * 1024 * 1024+ async with aiohttp.ClientSession(connector=ssrf_guarded_tcp_connector(resolver)) as session:+ for _ in range(MAX_SAFE_REDIRECTS + 1):+ encoded_url = encode_url(current_url)++ async def dispatch(_resolved_values: tuple[str, ...]) -> GuardedFileFetchHopResult:+ async with session.get(+ URL(encoded_url, encoded=True), headers=request_headers, allow_redirects=False+ ) as response:+ location = response.headers.get("Location")+ if response.status in SAFE_REDIRECT_STATUS_CODES and location:+ return GuardedFileRedirect(location=location)++ _raise_download_response_for_status(response)+ if response.content_length and response.content_length > max_size_bytes:+ raise DownloadFileMaxSizeExceeded(max_size_mb)++ body = bytearray()+ async for chunk in response.content.iter_chunked(1024):+ body.extend(chunk)+ if len(body) > max_size_bytes:+ raise DownloadFileMaxSizeExceeded(max_size_mb)++ return GuardedFileResponse(+ body=bytes(body),+ content_type=response.headers.get("Content-Type", ""),+ filename=_determine_download_filename(filename, response.headers, current_url),+ )++ result = await authorize_request_hop_once(+ authorize_request_hop,+ RedirectHopAuthorization(source_url=source_url, target_url=current_url, method="GET"),+ dispatch,+ )+ if isinstance(result, GuardedFileResponse):+ return result++ next_url = await validate_and_pin_redirect_url(current_url, result.location, resolver)+ if canonicalize_origin(next_url) is None:+ raise HttpException(400, "[redacted]", "Redirect has no browser-canonicalizable HTTP origin")+ if allowed_redirect_origin is not None and _url_origin(next_url) != _url_origin(allowed_redirect_origin):+ raise HttpException(400, "[redacted]", "Cross-origin redirect blocked by policy")+ request_headers, _ = strip_cross_origin_redirect_credentials(+ request_headers,+ None,+ current_url,+ next_url,+ )+ source_url, current_url = current_url, next_url++ raise HttpException(400, "[redacted]", "Too many redirects while downloading file")++def validate_download_url(url: str, organization_id: str | None = None) -> bool:"""Validate if a URL is supported for downloading.@@ -196,6 +306,8 @@ async def download_file(output_dir: str | None = None,filename: str | None = None,organization_id: str | None = None,+ allowed_redirect_origin: str | None = None,+ authorize_request_hop: RedirectHopAuthorizer[str | GuardedFileRedirect] | None = None,) -> str:if not url or not url.strip():raise ValueError("Download URL is empty — no file download was triggered by the browser")@@ -243,81 +355,102 @@ async def download_file(resolver = SSRFGuardedResolver()current_url = await validate_and_pin_fetch_url(url, resolver)+ if canonicalize_origin(current_url) is None:+ raise HttpException(400, "[redacted]", "URL has no browser-canonicalizable HTTP origin")+ if allowed_redirect_origin is not None and _url_origin(current_url) != _url_origin(allowed_redirect_origin):+ raise HttpException(400, "[redacted]", "Cross-origin redirect blocked by policy")request_headers = dict(headers or {})+ source_url: str | None = Noneasync with aiohttp.ClientSession(connector=ssrf_guarded_tcp_connector(resolver)) as session:- LOG.info("Starting to download file", url=url)+ LOG.info("Starting guarded file download")for _ in range(MAX_SAFE_REDIRECTS + 1):encoded_url = encode_url(current_url)- async with session.get(- URL(encoded_url, encoded=True), headers=request_headers, allow_redirects=False- ) as response:- if response.status in SAFE_REDIRECT_STATUS_CODES and response.headers.get("Location"):- next_url = await validate_and_pin_redirect_url(- current_url, response.headers["Location"], resolver- )- request_headers, _ = strip_cross_origin_redirect_credentials(- request_headers, None, current_url, next_url- )- current_url = next_url- continue-- _raise_download_response_for_status(response)-- # Check the content length if available- if max_size_mb and response.content_length and response.content_length > max_size_mb * 1024 * 1024:- # todo: move to root exception.py- raise DownloadFileMaxSizeExceeded(max_size_mb)- # Get the file name- if output_dir:- download_dir_path = Path(output_dir)- download_dir_path.mkdir(parents=True, exist_ok=True)- else:- download_dir_path = Path(make_temp_directory(prefix="skyvern_downloads_"))-- download_dir_resolved = download_dir_path.resolve()- # response.headers stays a CIMultiDictProxy: dict() would make header- # lookups case-sensitive and miss lowercase wire headers.- file_name = _determine_download_filename(filename, response.headers, url)- allowed_dir = os.path.realpath(download_dir_resolved)- resolved_final_path = os.path.realpath(os.path.join(allowed_dir, file_name))- # sanitize_filename strips separators but keeps dots, so a dots-only name can- # still resolve outside the download dir; require a direct child. Checked- # before streaming so a rejected name downloads zero bytes.- if (- resolved_final_path == allowed_dir- or not resolved_final_path.startswith(allowed_dir + os.sep)- or os.path.dirname(resolved_final_path) != allowed_dir- ):- raise ValueError(f"Unsafe filename derived from download: {file_name!r}")- final_path = Path(resolved_final_path)-- temp_file = tempfile.NamedTemporaryFile(mode="wb", dir=download_dir_resolved, delete=False)- file_path = Path(temp_file.name).resolve()- if file_path != download_dir_resolved and not file_path.is_relative_to(download_dir_resolved):- temp_file.close()- raise ValueError("Unsafe temporary file path created for download")-- LOG.info("Downloading file to temporary path", file_path=str(file_path))- try:- with temp_file as f:- # Write the content of the request into the file- total_bytes_downloaded = 0- async for chunk in response.content.iter_chunked(1024):- f.write(chunk)- total_bytes_downloaded += len(chunk)- if max_size_mb and total_bytes_downloaded > max_size_mb * 1024 * 1024:- raise DownloadFileMaxSizeExceeded(max_size_mb)-- file_path.replace(final_path)- except BaseException:- # An orphaned temp file in a run download dir would get synced to storage- # under the tmpXXXX name; drop it on any failure (incl. cancellation).- file_path.unlink(missing_ok=True)- raise-- LOG.info(f"File downloaded successfully to {final_path}")- return str(final_path)+ async def dispatch(_resolved_values: tuple[str, ...]) -> str | GuardedFileRedirect:+ async with session.get(+ URL(encoded_url, encoded=True), headers=request_headers, allow_redirects=False+ ) as response:+ location = response.headers.get("Location")+ if response.status in SAFE_REDIRECT_STATUS_CODES and location:+ return GuardedFileRedirect(location=location)++ _raise_download_response_for_status(response)+ if (+ max_size_mb+ and response.content_length+ and response.content_length > max_size_mb * 1024 * 1024+ ):+ raise DownloadFileMaxSizeExceeded(max_size_mb)++ if output_dir:+ download_dir_path = Path(output_dir)+ download_dir_path.mkdir(parents=True, exist_ok=True)+ else:+ download_dir_path = Path(make_temp_directory(prefix="skyvern_downloads_"))++ download_dir_resolved = download_dir_path.resolve()+ file_name = _determine_download_filename(filename, response.headers, url)+ allowed_dir = os.path.realpath(download_dir_resolved)+ resolved_final_path = os.path.realpath(os.path.join(allowed_dir, file_name))+ if (+ resolved_final_path == allowed_dir+ or not resolved_final_path.startswith(allowed_dir + os.sep)+ or os.path.dirname(resolved_final_path) != allowed_dir+ ):+ raise ValueError(f"Unsafe filename derived from download: {file_name!r}")+ final_path = Path(resolved_final_path)++ temp_file = tempfile.NamedTemporaryFile(mode="wb", dir=download_dir_resolved, delete=False)+ file_path = Path(temp_file.name).resolve()+ if file_path != download_dir_resolved and not file_path.is_relative_to(download_dir_resolved):+ temp_file.close()+ raise ValueError("Unsafe temporary file path created for download")++ LOG.info("Downloading file to temporary path", file_path=str(file_path))+ try:+ with temp_file as f:+ total_bytes_downloaded = 0+ async for chunk in response.content.iter_chunked(1024):+ f.write(chunk)+ total_bytes_downloaded += len(chunk)+ if max_size_mb and total_bytes_downloaded > max_size_mb * 1024 * 1024:+ raise DownloadFileMaxSizeExceeded(max_size_mb)++ file_path.replace(final_path)+ except BaseException:+ file_path.unlink(missing_ok=True)+ raise++ LOG.info(f"File downloaded successfully to {final_path}")+ return str(final_path)++ authorization = RedirectHopAuthorization(+ source_url=source_url,+ target_url=current_url,+ method="GET",+ )+ result = (+ await authorize_request_hop_once(authorize_request_hop, authorization, dispatch)+ if authorize_request_hop is not None+ else await dispatch(())+ )+ if isinstance(result, str):+ return result++ next_url = await validate_and_pin_redirect_url(current_url, result.location, resolver)+ if canonicalize_origin(next_url) is None:+ raise HttpException(400, "[redacted]", "Redirect has no browser-canonicalizable HTTP origin")+ if allowed_redirect_origin is not None and _url_origin(next_url) != _url_origin(+ allowed_redirect_origin+ ):+ raise HttpException(400, "[redacted]", "Cross-origin redirect blocked by policy")+ request_headers, _ = strip_cross_origin_redirect_credentials(+ request_headers,+ None,+ current_url,+ next_url,+ )+ source_url, current_url = current_url, next_urlraise SkyvernHTTPException(message=f"Too many redirects while downloading file: {current_url}",status_code=HTTPStatus.BAD_REQUEST,diff --git a/skyvern/forge/sdk/copilot/code_block_synthesis.py b/skyvern/forge/sdk/copilot/code_block_synthesis.pyindex 5112f878c..c4870b810 100644--- a/skyvern/forge/sdk/copilot/code_block_synthesis.py+++ b/skyvern/forge/sdk/copilot/code_block_synthesis.py@@ -197,8 +197,7 @@ _CODE_SUBMIT_ACTION_RE = re.compile(r"\.(?:click|press)\s*\(")def _is_submit_interaction(interaction: Mapping[str, Any]) -> bool:- """A submit is a click, or an Enter keypress; other keys (Tab between fields) are not submits, so- both the synthesis submit boundary and the persist-time credential-scout gate share one definition."""+ """A submit is a click, or an Enter keypress; other keys (Tab between fields) are not submits."""tool_name = str(interaction.get("tool_name") or "").strip()if tool_name == "click":return Truediff --git a/skyvern/forge/sdk/copilot/enforcement.py b/skyvern/forge/sdk/copilot/enforcement.pyindex 54e41e13c..d4696a8fe 100644--- a/skyvern/forge/sdk/copilot/enforcement.py+++ b/skyvern/forge/sdk/copilot/enforcement.py@@ -2291,8 +2291,6 @@ def synthesized_offer_reopened_for_extraction_plan(def synthesized_persistence_reopened(ctx: AgentContext) -> bool:- if ctx.synthesized_block_reopened_for_credential_scout:- return Trueif synthesized_goal_completion_landing_pending(ctx):return Truereturn synthesized_persistence_reopened_after_failed_run(ctx)@@ -2851,9 +2849,8 @@ def _credential_password_demand_holds(ctx: Any, interactions: list[dict[str, Anydef _credential_flow_scout_gap_incomplete(ctx: Any, trajectory: list[Any]) -> bool:- """Trajectory- and inventory-scoped mirror of the persist seam's credential scout gate: engaged- credentials (username/password fills) must have every required field filled plus a post-fill- submit before the synthesized trajectory may grade goal-complete."""+ """Engaged credentials (username/password fills) must have every required field filled plus a+ post-fill submit before the synthesized trajectory may grade goal-complete."""interactions = [item for item in trajectory if isinstance(item, dict)]filled_by_credential = _credential_flow_filled_fields_by_credential(interactions)if not filled_by_credential:@@ -3142,17 +3139,6 @@ def _should_force_advisory_run_dispatch(ctx: Any) -> bool:return not blocker_signal_is_genuinely_terminal(getattr(ctx, "blocker_signal", None))-def arm_credential_scout_reopen(ctx: AgentContext, identity_digest: str) -> bool:- """Arm a one-shot scout-window reopen for the first author-time credential-scout reject per- (structural identity + credential binding) digest. A repeat identical reject returns False and- falls through so it counts normally toward the repair ceiling."""- if ctx.credential_scout_rescout_context_key == identity_digest:- return False- ctx.credential_scout_rescout_context_key = identity_digest- ctx.synthesized_block_reopened_for_credential_scout = True- return True--def _runner_kwargs_with_forced_tool_choice(runner_kwargs: dict[str, Any], tool_name: str) -> dict[str, Any]:run_config = runner_kwargs.get("run_config")if isinstance(run_config, RunConfig):diff --git a/skyvern/forge/sdk/copilot/runtime.py b/skyvern/forge/sdk/copilot/runtime.pyindex 4e07821df..1e383b29d 100644--- a/skyvern/forge/sdk/copilot/runtime.py+++ b/skyvern/forge/sdk/copilot/runtime.py@@ -519,7 +519,6 @@ class AgentContext:# returns. The freehand persist-seam surface leg exempts exactly this label and gates its siblings.spine_imposition_carrier_label: str | None = Nonesynthesized_block_reopened_after_failed_run: bool = False- synthesized_block_reopened_for_credential_scout: bool = False# Business inputs proven required by an earlier synthesized-draft rejection stay required for the# rest of the turn. A later retry cannot evade the floor by deleting those parameters from its YAML.synthesized_business_required_parameter_keys: set[str] = field(default_factory=set)@@ -528,7 +527,6 @@ class AgentContext:# login prefix; releases the is_goal_complete terminal-action gate mirroring reached_download_target.scout_observed_terminal_criterion_ids: set[str] = field(default_factory=set)scout_observation_contract: ScoutObservationContract | None = None- credential_scout_rescout_context_key: str | None = None# Which requires-live-scout fields (username/password, non-empty) each scouted credential# 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)diff --git a/skyvern/forge/sdk/copilot/tools/guardrails.py b/skyvern/forge/sdk/copilot/tools/guardrails.pyindex 25d0b03c6..573b7abcc 100644--- a/skyvern/forge/sdk/copilot/tools/guardrails.py+++ b/skyvern/forge/sdk/copilot/tools/guardrails.py@@ -237,19 +237,6 @@ _WORKFLOW_YAML_OUTPUT_POLICY_GUARDRAIL = ToolInputGuardrail()-def _request_policy_allows_untested_code_block_draft(ctx: Any) -> bool:- policy = getattr(ctx, "request_policy", None)- return (- getattr(ctx, "allow_untested_workflow_draft", False) is True- and isinstance(policy, RequestPolicy)- and policy.allow_update_workflow- and not policy.allow_run_blocks- and policy.testing_intent == "skip_test"- and policy.allow_missing_credentials_in_draft- and policy.credential_draft_deferred_explicitly- )--def _request_policy_tool_error(ctx: AgentContext, tool_name: str) -> CopilotToolBlockerSignal | None:policy = getattr(ctx, "request_policy", None)if not isinstance(policy, RequestPolicy):diff --git a/skyvern/forge/sdk/copilot/tools/workflow_update.py b/skyvern/forge/sdk/copilot/tools/workflow_update.pyindex d2c212d40..fed71fc7f 100644--- a/skyvern/forge/sdk/copilot/tools/workflow_update.py+++ b/skyvern/forge/sdk/copilot/tools/workflow_update.py@@ -54,7 +54,6 @@ from skyvern.forge.sdk.copilot.authoring_parameter_binding import (derive_same_month_file_match_transform,)from skyvern.forge.sdk.copilot.blocker_signal import (- CREDENTIAL_SCOUT_VERIFY_REPLY,CopilotToolBlockerSignal,clear_terminal_evidence_on_workflow_edit,)@@ -63,7 +62,6 @@ from skyvern.forge.sdk.copilot.build_test_outcome import (RecordedBuildTestOutcome,RecordedOutcomeBindingConstraint,RecordedOutcomeGroundingRequirement,- _stable_hash,authored_structure_signature_from_workflow,record_build_test_outcome,recorded_outcome_from_author_time_reject,@@ -80,7 +78,6 @@ from skyvern.forge.sdk.copilot.code_block_preflight import (from skyvern.forge.sdk.copilot.code_block_security import CodeBlockSecurityError, author_time_code_security_errorsfrom skyvern.forge.sdk.copilot.code_block_steps import apply_derived_code_block_steps, fill_code_block_prompts_in_yamlfrom skyvern.forge.sdk.copilot.code_block_synthesis import (- _CODE_SUBMIT_ACTION_RE,_INTERNAL_SCOUT_VARS,_SYNTHESIZED_BLOCK_LABEL,CREDENTIAL_FILL_TOOL_NAME,@@ -96,7 +93,6 @@ from skyvern.forge.sdk.copilot.code_block_synthesis import (SynthesisDiagnostics,SynthesizedCodeBlock,_bare_drop_superseded_on_screen,- _credential_field_accesses,_get_by_role_expr_strict,_is_ignorable_entry_opener_drop,_is_positional_selector,@@ -106,7 +102,6 @@ from skyvern.forge.sdk.copilot.code_block_synthesis import (artifact_observation_ref_id,build_input_templated_locator,build_same_month_file_match_locator,- credential_scout_gap,dynamic_row_evidence_fingerprint,dynamic_row_period_matches_match_selected_row,freeze_requested_output_extraction_candidate,@@ -127,7 +122,6 @@ from skyvern.forge.sdk.copilot.code_block_synthesis import (synthesize_code_block_with_extraction,templated_selection_locator_binding,uncovered_required_emitted_interactions,- uncovered_rung_records,validated_dynamic_row_period_matches,)from skyvern.forge.sdk.copilot.code_block_synthesis import wrapped_code_ast as _wrapped_code_ast@@ -147,7 +141,6 @@ from skyvern.forge.sdk.copilot.enforcement import (_CHURN_REASON_CODES,_record_code_authoring_guardrail_reject,_scouted_spine_open_obligation,- arm_credential_scout_reopen,download_satisfied_requested_output_paths,requested_output_extraction_plan,requested_output_extraction_plan_changed,@@ -251,10 +244,7 @@ from .frontier import (_invalidate_verified_state_on_edit,_workflow_requires_canonical_persist,)-from .guardrails import (- _authority_tool_error,- _request_policy_allows_untested_code_block_draft,-)+from .guardrails import _authority_tool_errorLOG = structlog.get_logger()@@ -1268,51 +1258,6 @@ def _code_safety_reject_payload(errors: list[str | CodeBlockSecurityError]) -> Mreturn {"code_safety_errors": entries}-def _credential_scout_reject_payload(workflow_yaml: str) -> Mapping[str, object] | None:- entries: list[dict[str, object]] = []- for label, block in _workflow_yaml_code_blocks_by_label(workflow_yaml).items():- code = str(block.get("code") or "")- if not code.strip():- continue- accesses = [- {- "parameter_key": access.parameter_key,- "field": access.field,- "requires_live_scout": access.requires_live_scout,- }- for access in _credential_field_accesses(code)- ]- if accesses or _CODE_SUBMIT_ACTION_RE.search(code):- entries.append(- {- "block_label": label,- "credential_field_accesses": accesses,- "requires_submit": bool(_CODE_SUBMIT_ACTION_RE.search(code)),- }- )- if not entries:- return None- return {"credential_scout_requirements": entries}---def _credential_scout_reopen_identity_digest(workflow_yaml: str) -> str:- structural_identity = "author_time:" + _stable_hash(_credential_scout_reject_payload(workflow_yaml) or {})- accessed_parameter_keys: set[str] = set()- for block in _workflow_yaml_code_blocks_by_label(workflow_yaml).values():- code = str(block.get("code") or "")- accessed_parameter_keys.update(- access.parameter_key for access in _credential_field_accesses(code) if access.requires_live_scout- )- credential_params_by_key: dict[str, set[str]] = {}- parsed = parse_workflow_yaml(workflow_yaml)- if isinstance(parsed, dict):- workflow_definition = parsed.get("workflow_definition")- if isinstance(workflow_definition, dict):- credential_params_by_key = credential_param_ids(workflow_definition.get("parameters"))- binding = {key: sorted(ids) for key, ids in credential_params_by_key.items() if key in accessed_parameter_keys}- return f"{structural_identity}|{_stable_hash(binding)}"--def _metadata_item_extraction_schema_paths(item: Mapping[str, Any]) -> set[str]:for field_name in ("claimed_outcomes", "terminal_verifier_expectations"):for row in _artifact_rows(item.get(field_name)):@@ -5887,7 +5832,6 @@ def _should_impose_after_update_attempt(ctx: AgentContext, *, repeated_identicaland bool(target.selector.strip()))or synthesized_persistence_reopened_after_failed_run(ctx)- or ctx.synthesized_block_reopened_for_credential_scoutor repeated_identical_omissionor _author_time_reject_reopens_synthesized_imposition(ctx))@@ -7192,35 +7136,6 @@ _SCOUTED_SPINE_REASON_CODES = frozenset()-def _synthesized_resubmission_credential_scout_requirements(- ctx: AgentContext, synthesized: SynthesizedCodeBlock, *, block_label: str-) -> list[str]:- # Runs the credential-scout gate against the verbatim resubmission the under-build reject asks- # for, so the reject names every step of a route that is admissible end-to-end.- credential_parameters = [- parameter for parameter in synthesized.parameters if str(parameter.get("credential_id") or "").strip()- ]- if not credential_parameters:- return []- probe_yaml = yaml.safe_dump(- {- "workflow_definition": {- "parameters": [- {- "parameter_type": "credential",- "key": str(parameter.get("key") or ""),- "credential_id": str(parameter.get("credential_id") or ""),- }- for parameter in credential_parameters- ],- "blocks": [{"block_type": "code", "label": block_label, "code": synthesized.code}],- }- },- sort_keys=False,- )- return _credentialed_code_block_scout_gate_errors(probe_yaml, ctx)--def _scouted_spine_omission_digest(records: Sequence[Mapping[str, Any]]) -> str:items = sorted((@@ -7255,9 +7170,6 @@ def _scouted_spine_under_build_result(uncovered_required_emitted_interactions(diagnostics.emitted_interactions, draft_calls) if required else [])if uncovered:- credential_scout_requirements = _synthesized_resubmission_credential_scout_requirements(- ctx, synthesized, block_label=block_label- )missing_text = missing_rung_text(uncovered)LOG.info("copilot_scouted_spine_under_build",@@ -7266,7 +7178,6 @@ def _scouted_spine_under_build_result(required_rung_count=len(required),covered_rung_count=len(required) - len(uncovered),missing_rungs=missing_text,- credential_scout_precondition_pending=bool(credential_scout_requirements),)first_uncovered = uncovered[0]pass_route = render_missing_rung_call_sources(uncovered)@@ -7275,18 +7186,10 @@ def _scouted_spine_under_build_result(f"({_SCOUTED_SPINE_UNDER_BUILD_REASON_CODE}): the draft covers "f"{len(required) - len(uncovered)} of {len(required)} scouted rung(s); missing: {missing_text}. ")- if credential_scout_requirements:- violation += (- "Two steps are required, in order. Step 1 — satisfy the credential-scout precondition: "- + " ".join(credential_scout_requirements)- + " Step 2 — resubmit the code block, reusing the synthesized rung source verbatim so every "- "scouted rung is replayed."- )- else:- violation += (- "Author the remaining synthesized rungs — reuse the synthesized code block verbatim so every "- "scouted rung is replayed."- )+ violation += (+ "Author the remaining synthesized rungs — reuse the synthesized code block verbatim so every "+ "scouted rung is replayed."+ )if pass_route:violation += "\n" + pass_routereturn _SynthesizedCodeImpositionResult(@@ -10251,134 +10154,10 @@ def _artifact_string_list(value: Any) -> list[str]:return [str(item).strip() for item in value if str(item).strip()]-def _credentialed_code_block_scout_gate_errors(- workflow_yaml: str | None,- ctx: AgentContext,- *,- block_labels: Iterable[str] | None = None,-) -> list[str]:- if _copilot_block_authoring_policy(ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:- return []- if not workflow_yaml:- return []- parsed = parse_workflow_yaml(workflow_yaml)- if not isinstance(parsed, dict):- return []- workflow_definition = parsed.get("workflow_definition")- if not isinstance(workflow_definition, dict):- return []- credential_params_by_key = credential_param_ids(workflow_definition.get("parameters"))- if not credential_params_by_key:- return []- prior_blocks_by_label = {}- prior_credential_params_by_key = {}- prior_workflow_yaml = ctx.workflow_yaml- if isinstance(prior_workflow_yaml, str) and prior_workflow_yaml.strip():- prior_parsed = parse_workflow_yaml(prior_workflow_yaml)- if isinstance(prior_parsed, dict):- prior_workflow_definition = prior_parsed.get("workflow_definition")- if isinstance(prior_workflow_definition, dict):- prior_credential_params_by_key = credential_param_ids(prior_workflow_definition.get("parameters"))- for prior_block in workflow_blocks(prior_parsed):- if _enum_or_string_name(prior_block.get("block_type")) != BlockType.CODE.value:- continue- prior_label = str(prior_block.get("label") or "").strip()- if prior_label:- prior_blocks_by_label[prior_label] = prior_block- scout_trajectory = getattr(ctx, "scout_trajectory", None)- if not isinstance(scout_trajectory, list):- scout_trajectory = []- selected_block_labels = {label.strip() for label in block_labels or [] if label.strip()}-- errors: list[str] = []- for block in workflow_blocks(parsed):- if _enum_or_string_name(block.get("block_type")) != BlockType.CODE.value:- continue- code = str(block.get("code") or "")- if not code.strip():- continue- block_label = str(block.get("label") or "").strip()- if selected_block_labels and block_label not in selected_block_labels:- continue- matching_prior_block = prior_blocks_by_label.get(block_label) if block_label else None- if (- isinstance(matching_prior_block, dict)- and str(matching_prior_block.get("code") or "") == code- and _code_block_parameter_keys(matching_prior_block) == _code_block_parameter_keys(block)- ):- accessed_parameter_keys = {- access.parameter_key for access in _credential_field_accesses(code) if access.requires_live_scout- }- if accessed_parameter_keys and all(- prior_credential_params_by_key.get(parameter_key) == credential_params_by_key.get(parameter_key)- for parameter_key in accessed_parameter_keys- ):- continue- required_fields_by_parameter: dict[str, tuple[set[str], set[str]]] = {}- for access in _credential_field_accesses(code):- if not access.requires_live_scout:- continue- credential_ids = credential_params_by_key.get(access.parameter_key)- if credential_ids:- allowed_credential_ids, required_fields = required_fields_by_parameter.setdefault(- access.parameter_key, (credential_ids, set())- )- required_fields.add(access.field)- if not required_fields_by_parameter:- continue-- gap = credential_scout_gap(- scout_trajectory,- list(required_fields_by_parameter.values()),- requires_submit=bool(_CODE_SUBMIT_ACTION_RE.search(code)),- )- missing_fields = gap.missing_fields- missing_submit = gap.missing_submit-- if not missing_fields and not missing_submit:- continue-- block_label = block_label or "this code block"- requirements: list[str] = []- if missing_fields:- joined_fields = ", ".join(f"`{field}`" for field in missing_fields)- requirements.append(f"successful `fill_credential_field` scouting for {joined_fields}")- if missing_submit:- requirements.append("a later submit action on the same page")- requirement_text = " and ".join(requirements)- errors.append(- f"Code block `{block_label}` reads saved credential fields, but the current debug-browser scout "- f"record is missing {requirement_text}. First scout the live form with `fill_credential_field` for "- "each referenced credential field, then click the submit control or press Enter in the debug browser "- "before retrying `update_workflow` or `update_and_run_blocks`."- )- return errors--def _missing_scouted_rung_violation_text(artifact: str) -> str:return "The persisted draft is missing scouted rung(s). " + artifact-def _open_scouted_spine_obligation_artifact(ctx: AgentContext) -> str:- try:- findings = _scouted_spine_open_obligation(ctx)- except Exception:- LOG.warning("copilot_scouted_spine_obligation_read_failed", exc_info=True)- return ""- artifact = render_missing_rung_call_sources(uncovered_rung_records(findings))- if not artifact:- return ""- return _missing_scouted_rung_violation_text(artifact)---def _credential_scout_reject_error_text(ctx: AgentContext, credential_scout_errors: list[str]) -> str:- error_text = "\n".join(credential_scout_errors)- obligation_artifact = _open_scouted_spine_obligation_artifact(ctx)- if obligation_artifact:- error_text += "\n" + obligation_artifact- return error_text--def carry_author_time_findings(update_result: dict[str, Any], result: dict[str, Any]) -> dict[str, Any]:"""The combined update-and-run tool returns the run's result, not the update's, so findingslabelled on the persisted draft reach the model only if they are carried across."""@@ -10567,7 +10346,6 @@ async def _update_workflow(ctx,runtime_parameters if isinstance(runtime_parameters, Mapping) else None,)- ctx.synthesized_block_reopened_for_credential_scout = Falseif not imposition.violations:workflow_yaml = imposition.workflow_yamlstripped_sandbox_imports: list[str] = []@@ -10639,11 +10417,6 @@ async def _update_workflow(if code_safety_errors and _unresolved_symbol_repair_context_enabled(ctx)else None)- credential_scout_errors = (- []- if _request_policy_allows_untested_code_block_draft(ctx)- else _credentialed_code_block_scout_gate_errors(workflow_yaml, ctx, block_labels=params.get("block_labels"))- )if code_safety_errors:_set_code_authoring_repair_context(ctx, code_authoring_repair_context)if code_authoring_repair_context is None:@@ -10673,38 +10446,6 @@ async def _update_workflow(params["code_artifact_metadata"] = merged_metadataworkflow_yaml = _apply_metadata_contract_schema_to_workflow_yaml(ctx, workflow_yaml, merged_metadata)params["workflow_yaml"] = workflow_yaml- if credential_scout_errors and code_safety_errors:- if _is_unresolved_symbol_repair_context(code_authoring_repair_context):- return _blocked(- AuthorTimeBlock(- block_id=CODE_SAFETY_BLOCK_ID,- error="\n".join(str(error) for error in code_safety_errors if error),- user_facing_summary=_code_seam_rejection_user_summary(- metadata_rejected=False,- code_rejected=True,- ),- data=_code_repair_progress_data(code_authoring_repair_context),- ),- repair_context=code_authoring_repair_context,- )- arm_credential_scout_reopen(ctx, _credential_scout_reopen_identity_digest(workflow_yaml))- return _blocked(- AuthorTimeBlock(- block_id=CREDENTIAL_SCOUT_BLOCK_ID,- error=_credential_scout_reject_error_text(ctx, credential_scout_errors),- user_facing_summary=CREDENTIAL_SCOUT_VERIFY_REPLY,- data={- "failure_type": "missing_credential_or_init",- "diagnostic_code_safety_errors": code_safety_errors,- **(- {"authoring_repair_context": code_authoring_repair_context.model_dump(mode="json")}- if code_authoring_repair_context is not None- else {}- ),- },- ),- repair_context=code_authoring_repair_context,- )human_facing_code_safety_errors = _human_facing_code_safety_errors(code_safety_errors)if human_facing_code_safety_errors:return _blocked(@@ -10719,22 +10460,6 @@ async def _update_workflow(),repair_context=code_authoring_repair_context,)- if credential_scout_errors:- _record_author_time_reject_outcome(- ctx,- reason_code="credential_scout_reject",- summary=CREDENTIAL_SCOUT_VERIFY_REPLY,- structural_payload=_credential_scout_reject_payload(workflow_yaml),- )- arm_credential_scout_reopen(ctx, _credential_scout_reopen_identity_digest(workflow_yaml))- return _blocked(- AuthorTimeBlock(- block_id=CREDENTIAL_SCOUT_BLOCK_ID,- error=_credential_scout_reject_error_text(ctx, credential_scout_errors),- user_facing_summary=CREDENTIAL_SCOUT_VERIFY_REPLY,- data={"failure_type": "missing_credential_or_init"},- )- )if allow_missing_credentials is None:allow_missing_credentials = ctx.allow_untested_workflow_draft is Trueif not allow_missing_credentials:diff --git a/skyvern/forge/sdk/core/http_request_authorization.py b/skyvern/forge/sdk/core/http_request_authorization.pynew file mode 100644index 000000000..8fa5cc5f6--- /dev/null+++ b/skyvern/forge/sdk/core/http_request_authorization.py@@ -0,0 +1,66 @@+"""Authorization boundary for one direct HTTP request or redirect hop."""++from __future__ import annotations++import asyncio+from collections.abc import Awaitable, Callable+from dataclasses import dataclass+from typing import TypeAlias, TypeVar++T = TypeVar("T")+++@dataclass(frozen=True, slots=True)+class RedirectHopAuthorization:+ """The already-validated live effect presented to the trusted approval consumer."""++ source_url: str | None+ target_url: str+ method: str+++RedirectHopDispatcher: TypeAlias = Callable[[tuple[str, ...]], Awaitable[T]]+RedirectHopAuthorizer: TypeAlias = Callable[+ [RedirectHopAuthorization, RedirectHopDispatcher[T]],+ Awaitable[T],+]+++async def authorize_request_hop_once(+ authorizer: RedirectHopAuthorizer[T],+ authorization: RedirectHopAuthorization,+ dispatcher: RedirectHopDispatcher[T],+) -> T:+ """Authorize exactly one in-scope network dispatch for a validated hop."""+ authorizing_task = asyncio.current_task()+ active = True+ dispatched = False++ async def dispatch_once(resolved_values: tuple[str, ...]) -> T:+ nonlocal dispatched+ if not active:+ raise RuntimeError("Redirect hop dispatcher is no longer active")+ if asyncio.current_task() is not authorizing_task:+ raise RuntimeError("Redirect hop dispatcher must run in the authorizing task")+ if dispatched:+ raise RuntimeError("Redirect hop dispatcher can only be invoked once")+ dispatched = True+ return await dispatcher(resolved_values)++ try:+ return await authorizer(authorization, dispatch_once)+ finally:+ active = False+++async def deny_unenrolled_redirect_hop(+ _authorization: RedirectHopAuthorization, _dispatch: RedirectHopDispatcher[T]+) -> T:+ """The named sentinel for a call site with no run-scoped authority to enroll yet.++ A missing authorizer and a deliberately-unenrolled one must be distinguishable at the type+ level, so a bare construction site passes this rather than leaving the argument out or+ defaulting to one that dispatches. It never calls ``dispatch``, so it never lets a hop reach+ the network.+ """+ raise RuntimeError("Redirect hop authorization is not enrolled for this browser session")diff --git a/skyvern/forge/sdk/db/models.py b/skyvern/forge/sdk/db/models.pyindex 035eb19c5..eb200a024 100644--- a/skyvern/forge/sdk/db/models.py+++ b/skyvern/forge/sdk/db/models.py@@ -1557,6 +1557,7 @@ class CredentialModel(Base):card_brand = Column(String, nullable=True)secret_label = Column(String, nullable=True)browser_profile_id = Column(String, nullable=True)+ auto_profile_disabled = Column(Boolean, nullable=True, default=False)tested_url = Column(String, nullable=True)user_context = Column(String(1000), nullable=True)save_browser_session_intent = Column(Boolean, nullable=True, default=False)diff --git a/skyvern/forge/sdk/db/repositories/credentials.py b/skyvern/forge/sdk/db/repositories/credentials.pyindex f31645358..518751638 100644--- a/skyvern/forge/sdk/db/repositories/credentials.py+++ b/skyvern/forge/sdk/db/repositories/credentials.py@@ -203,6 +203,7 @@ class CredentialRepository(BaseRepository):tested_url: str | None = None,user_context: str | None = None,save_browser_session_intent: bool | None = None,+ auto_profile_disabled: bool | None = None,run_sequentially: bool | None = None,proxy_location: ProxyLocationInput | object = _UNSET,proxy_session_id: str | None | object = _UNSET,@@ -233,6 +234,8 @@ class CredentialRepository(BaseRepository):credential.user_context = user_contextif save_browser_session_intent is not None:credential.save_browser_session_intent = save_browser_session_intent+ if auto_profile_disabled is not None:+ credential.auto_profile_disabled = auto_profile_disabledif run_sequentially is not None:credential.run_sequentially = run_sequentiallyapply_proxy_pin_to_model(diff --git a/skyvern/forge/sdk/routes/credentials.py b/skyvern/forge/sdk/routes/credentials.pyindex 6b6426084..3bbb83173 100644--- a/skyvern/forge/sdk/routes/credentials.py+++ b/skyvern/forge/sdk/routes/credentials.py@@ -730,7 +730,8 @@ async def create_credential(pin_provided = "pin_saved_session_ip" in data.model_fields_setbpid_provided = "browser_profile_id" in data.model_fields_set- if bpid_provided or pin_provided:+ auto_profile_disabled_provided = "auto_profile_disabled" in data.model_fields_set+ if bpid_provided or pin_provided or auto_profile_disabled_provided:# Only pass browser_profile_id when the caller sent it, so a pin-only request never passes the# omitted default (None) into the repo's unlink sentinel.profile_update: dict[str, Any] = {"browser_profile_id": data.browser_profile_id} if bpid_provided else {}@@ -740,6 +741,7 @@ async def create_credential(**profile_update,# Only touch the pin when the caller sent it — supplying a profile alone must not reset it.pin_saved_session_ip=data.pin_saved_session_ip if pin_provided else None,+ auto_profile_disabled=data.auto_profile_disabled if auto_profile_disabled_provided else None,)return _convert_to_response(credential)@@ -888,6 +890,8 @@ async def rename_credential(update_kwargs["user_context"] = data.user_contextif data.save_browser_session_intent is not None:update_kwargs["save_browser_session_intent"] = data.save_browser_session_intent+ if data.auto_profile_disabled is not None:+ update_kwargs["auto_profile_disabled"] = data.auto_profile_disabledif data.run_sequentially is True and not app.AGENT_FUNCTION.supports_sequential_credentials():raise HTTPException(status_code=400,@@ -2010,7 +2014,8 @@ async def update_credential(# (pin omitted) must not reset the pin.pin_provided = "pin_saved_session_ip" in data.model_fields_setbpid_provided = "browser_profile_id" in data.model_fields_set- if bpid_provided or pin_provided:+ auto_profile_disabled_provided = "auto_profile_disabled" in data.model_fields_set+ if bpid_provided or pin_provided or auto_profile_disabled_provided:# Only pass browser_profile_id when the caller sent it — a pin-only update must not pass the# field's omitted default (None) and unlink the profile via the repo's unlink sentinel.profile_update: dict[str, Any] = {"browser_profile_id": data.browser_profile_id} if bpid_provided else {}@@ -2019,6 +2024,7 @@ async def update_credential(organization_id=current_org.organization_id,**profile_update,pin_saved_session_ip=data.pin_saved_session_ip if pin_provided else None,+ auto_profile_disabled=data.auto_profile_disabled if auto_profile_disabled_provided else None,)return _convert_to_response(updated_credential)@@ -3405,6 +3411,7 @@ def _convert_to_response(credential: Credential) -> CredentialResponse:name=credential.name,vault_type=credential.vault_type,browser_profile_id=credential.browser_profile_id,+ auto_profile_disabled=credential.auto_profile_disabled,pin_saved_session_ip=credential.pin_saved_session_ip,tested_url=credential.tested_url,user_context=credential.user_context,@@ -3426,6 +3433,7 @@ def _convert_to_response(credential: Credential) -> CredentialResponse:name=credential.name,vault_type=credential.vault_type,browser_profile_id=credential.browser_profile_id,+ auto_profile_disabled=credential.auto_profile_disabled,pin_saved_session_ip=credential.pin_saved_session_ip,tested_url=credential.tested_url,user_context=credential.user_context,@@ -3444,6 +3452,7 @@ def _convert_to_response(credential: Credential) -> CredentialResponse:name=credential.name,vault_type=credential.vault_type,browser_profile_id=credential.browser_profile_id,+ auto_profile_disabled=credential.auto_profile_disabled,pin_saved_session_ip=credential.pin_saved_session_ip,tested_url=credential.tested_url,user_context=credential.user_context,diff --git a/skyvern/forge/sdk/routes/scripts.py b/skyvern/forge/sdk/routes/scripts.pyindex 97c2a26fa..f62e8563b 100644--- a/skyvern/forge/sdk/routes/scripts.py+++ b/skyvern/forge/sdk/routes/scripts.py@@ -8,7 +8,6 @@ from fastapi import BackgroundTasks, Depends, HTTPException, Path, Query, Requesif TYPE_CHECKING:from skyvern.forge.sdk.db.models import WorkflowScriptModel-from skyvern.config import settingsfrom skyvern.forge import appfrom skyvern.forge.sdk.executor.factory import AsyncExecutorFactoryfrom skyvern.forge.sdk.routes.routers import base_router@@ -1316,17 +1315,8 @@ async def review_script_with_instructions("""organization_id = current_org.organization_id- # Enforce CODE_BLOCK_ENABLED feature flag server-side (mirrors frontend gating).- # When ENABLE_CODE_BLOCK=True (self-hosted), all orgs have code block access by default- # so the PostHog check is skipped — self-hosted operators control their own deployment.- if not settings.ENABLE_CODE_BLOCK and app.EXPERIMENTATION_PROVIDER:- code_block_enabled = await app.EXPERIMENTATION_PROVIDER.is_feature_enabled_cached(- "CODE_BLOCK_ENABLED",- organization_id,- properties={"organization_id": organization_id},- )- if not code_block_enabled:- raise HTTPException(status_code=403, detail="Script editing is not enabled for this organization")+ if not await app.AGENT_FUNCTION.has_code_block_access(organization_id):+ raise HTTPException(status_code=403, detail="Script editing is not enabled for this organization")# Load the workflowworkflow = await app.DATABASE.workflows.get_workflow_by_permanent_id(diff --git a/skyvern/forge/sdk/routes/streaming/cdp_input.py b/skyvern/forge/sdk/routes/streaming/cdp_input.pyindex 2f4209d03..a72b8885c 100644--- a/skyvern/forge/sdk/routes/streaming/cdp_input.py+++ b/skyvern/forge/sdk/routes/streaming/cdp_input.py@@ -13,6 +13,7 @@ from fastapi import WebSocket, WebSocketDisconnectfrom playwright.async_api import CDPSessionfrom websockets.exceptions import ConnectionClosedError, ConnectionClosedOK+from skyvern.exceptions import BlockedNavigationDestination, InvalidUrlfrom skyvern.forge import appfrom skyvern.forge.sdk.routes.routers import base_router, legacy_base_routerfrom skyvern.forge.sdk.routes.streaming.auth import auth, require_client_id@@ -29,7 +30,9 @@ from skyvern.forge.sdk.routes.streaming.screencast import ()from skyvern.forge.sdk.schemas.persistent_browser_sessions import is_final_statusfrom skyvern.forge.sdk.workflow.models.workflow import WorkflowRunStatus+from skyvern.utils.url_validators import prepend_scheme_and_validate_urlfrom skyvern.webeye.browser_state import BrowserState+from skyvern.webeye.navigation import revalidate_redirect_chain, validate_navigation_destinationLOG = structlog.get_logger()@@ -42,6 +45,8 @@ _MAX_KEY_LEN = 32_MAX_CODE_LEN = 32_MODIFIER_MASK = 0xF_MAX_VK_CODE = 0xFE+# Matches the length Skyvern's own InvalidUrl exception documents as its supported max.+_MAX_URL_LEN = 2083ACTIVE_PAGE_INPUT_REFRESH_INTERVAL = 0.5@@ -253,13 +258,64 @@ _EVENT_DISPATCH_MAP: dict[str, tuple[t.Callable[[dict], dict | None], str]] = {}+async def _dispatch_navigate_event(+ page: object,+ msg: dict,+ log_id_key: str,+ log_id_value: str,+ websocket: WebSocket,+) -> None:+ raw_url = msg.get("url")+ if not isinstance(raw_url, str) or not raw_url.strip() or len(raw_url) > _MAX_URL_LEN:+ LOG.warning("CDP input: navigate validation failed", **{log_id_key: log_id_value}, reason="malformed_url")+ await websocket.send_json({"kind": "navigate-error", "reason": "invalid_url"})+ return++ try:+ # Normalize (bare host -> https://, well-formedness) before validating or navigating so+ # the browser is never asked to load exactly what the user typed unvalidated.+ url = await asyncio.to_thread(prepend_scheme_and_validate_url, raw_url)+ except InvalidUrl:+ LOG.warning("CDP input: navigate validation failed", **{log_id_key: log_id_value}, reason="invalid_url")+ await websocket.send_json({"kind": "navigate-error", "reason": "invalid_url"})+ return++ try:+ # Fail closed before any request reaches the remote browser -- the same choke point+ # every real page.goto in the codebase funnels through (see navigate_with_retry).+ await asyncio.to_thread(validate_navigation_destination, url)+ except BlockedNavigationDestination as error:+ LOG.info("CDP input: navigate blocked", **{log_id_key: log_id_value}, reason=error.reason)+ await websocket.send_json({"kind": "navigate-error", "reason": "blocked"})+ return++ response = await page.goto(url) # type: ignore[attr-defined]+ try:+ # page.goto follows redirects at the network layer, so a validated public entry point+ # can still land on an internal host -- re-check the followed chain (SKY-13112 pattern).+ await revalidate_redirect_chain(response, validate_navigation_destination)+ except BlockedNavigationDestination as error:+ LOG.info("CDP input: navigate blocked via redirect", **{log_id_key: log_id_value}, reason=error.reason)+ try:+ await page.goto("about:blank") # type: ignore[attr-defined]+ except Exception:+ LOG.exception("CDP input: failed to reset page after redirect refusal", **{log_id_key: log_id_value})+ await websocket.send_json({"kind": "navigate-error", "reason": "blocked"})++async def _dispatch_event(cdp_session: CDPSession,+ page: object,kind: str,msg: dict,log_id_key: str,log_id_value: str,+ websocket: WebSocket,) -> None:+ if kind == "navigateEvent":+ await _dispatch_navigate_event(page, msg, log_id_key, log_id_value, websocket)+ return+entry = _EVENT_DISPATCH_MAP.get(kind)if entry is None:return@@ -338,7 +394,7 @@ async def _run_input_loop(continuetry:- await _dispatch_event(cdp_session, kind, msg, log_id_key, log_id_value)+ await _dispatch_event(cdp_session, input_session.page, kind, msg, log_id_key, log_id_value, websocket)except Exception:LOG.warning("CDP input: failed to dispatch event; closing input channel",diff --git a/skyvern/forge/sdk/schemas/credentials.py b/skyvern/forge/sdk/schemas/credentials.pyindex 5630b6feb..65c027d1c 100644--- a/skyvern/forge/sdk/schemas/credentials.py+++ b/skyvern/forge/sdk/schemas/credentials.py@@ -255,6 +255,10 @@ class CreateCredentialRequest(BaseModel):description="Optional plain browser profile to link as this credential's saved profile. Must be ""an unmanaged profile in your organization that no other credential already owns.",)+ auto_profile_disabled: bool = Field(+ default=False,+ description="Disable automatic saving and reuse of this credential's browser profile.",+ )pin_saved_session_ip: bool = Field(default=False,description="Keep the same IP across sign-ins that reuse this credential's saved profile.",@@ -286,6 +290,10 @@ class CredentialResponse(BaseModel):description="Which vault stores this credential (e.g., 'skyvern', 'bitwarden', 'azure_vault', 'custom')",)browser_profile_id: str | None = Field(default=None, description="Browser profile ID linked to this credential")+ auto_profile_disabled: bool | None = Field(+ default=None,+ description="Whether automatic saving and reuse of this credential's browser profile is disabled.",+ )pin_saved_session_ip: bool = Field(default=False,description="Whether sign-ins reusing this credential's saved profile keep the same IP",@@ -386,6 +394,10 @@ class Credential(BaseModel):card_brand: str | None = Field(..., description="For credit_card credentials: the card brand")secret_label: str | None = Field(default=None, description="For secret credentials: optional label")browser_profile_id: str | None = Field(default=None, description="Browser profile ID linked to this credential")+ auto_profile_disabled: bool | None = Field(+ default=None,+ description="Whether automatic saving and reuse of this credential's browser profile is disabled.",+ )pin_saved_session_ip: bool = Field(default=False,description="Whether sign-ins reusing this credential's saved profile keep the same IP",@@ -428,6 +440,11 @@ class Credential(BaseModel):return validate_proxy_session_id(value)+def credential_auto_profile_disabled(credential: Credential) -> bool:+ """Opt-out is explicit-True only; False and legacy NULL keep automatic behavior."""+ return credential.auto_profile_disabled is True++class UpdateCredentialRequest(BaseModel):"""Request model for updating credential metadata."""@@ -451,6 +468,10 @@ class UpdateCredentialRequest(BaseModel):default=None,description="Whether the user intends to save a browser session, regardless of test outcome",)+ auto_profile_disabled: bool | None = Field(+ default=None,+ description="Disable automatic saving and reuse of this credential's browser profile.",+ )run_sequentially: bool | None = Field(default=None,description=(diff --git a/skyvern/forge/sdk/services/google_oauth_service.py b/skyvern/forge/sdk/services/google_oauth_service.pyindex 37d25f44d..292df5a79 100644--- a/skyvern/forge/sdk/services/google_oauth_service.py+++ b/skyvern/forge/sdk/services/google_oauth_service.py@@ -497,7 +497,6 @@ def build_authorize_url(url, _returned_state = flow.authorization_url(access_type="offline",prompt="consent",- include_granted_scopes="true",)# Flow.code_verifier is None when autogenerate is False AND we didn't set# it; that path is unreachable here, but raise (not assert) so a futurediff --git a/skyvern/forge/sdk/services/org_auth_service.py b/skyvern/forge/sdk/services/org_auth_service.pyindex 6e7b3266a..74b4b2339 100644--- a/skyvern/forge/sdk/services/org_auth_service.py+++ b/skyvern/forge/sdk/services/org_auth_service.py@@ -1,11 +1,11 @@import asyncioimport time+import weakreffrom dataclasses import dataclassfrom typing import Annotated, Sequenceimport jwtimport structlog-from asyncache import cachedfrom cachetools import TTLCachefrom fastapi import Header, HTTPException, statusfrom jwt.exceptions import PyJWTError@@ -22,7 +22,7 @@ from skyvern.forge.sdk.workflow.models.tags import CallerTypeLOG = structlog.get_logger()-AUTHENTICATION_TTL = 60 * 60 # one hour+AUTHENTICATION_TTL = 60 # one minuteCACHE_SIZE = 128ALGORITHM = "HS256"SKYVERN_UI_USER_AGENT = "skyvern-ui"@@ -536,26 +536,45 @@ async def resolve_org_from_api_key(_current_org_cache: TTLCache = TTLCache(maxsize=CACHE_SIZE, ttl=AUTHENTICATION_TTL)+_CurrentOrgCacheKey = tuple[str, AgentDB]+_current_org_cache_locks: weakref.WeakValueDictionary[_CurrentOrgCacheKey, asyncio.Lock] = weakref.WeakValueDictionary()+_current_org_cache_invalidation_generation = 0+++def _get_current_org_cache_lock(cache_key: _CurrentOrgCacheKey) -> asyncio.Lock:+ lock = _current_org_cache_locks.get(cache_key)+ if lock is None:+ lock = asyncio.Lock()+ _current_org_cache_locks[cache_key] = lock+ return lock-@cached(cache=_current_org_cache)async def _get_current_org_cached(x_api_key: str, db: AgentDB) -> Organization:- """Authentication is cached for one hour."""- validation = await resolve_org_from_api_key(- x_api_key,- db,- token_types=(- OrganizationAuthTokenType.api,- OrganizationAuthTokenType.ui_session,- ),- )+ """Validate API-key authentication and cache successful results for one minute."""+ cache_key = (x_api_key, db)+ try:+ return _current_org_cache[cache_key]+ except KeyError:+ pass- # set organization_id in skyvern context and log context- context = skyvern_context.current()- if context:- context.organization_id = validation.organization.organization_id- context.organization_name = validation.organization.organization_name- return validation.organization+ async with _get_current_org_cache_lock(cache_key):+ try:+ return _current_org_cache[cache_key]+ except KeyError:+ pass++ invalidation_generation = _current_org_cache_invalidation_generation+ validation = await resolve_org_from_api_key(x_api_key, db)++ # set organization_id in skyvern context and log context+ context = skyvern_context.current()+ if context:+ context.organization_id = validation.organization.organization_id+ context.organization_name = validation.organization.organization_name++ if invalidation_generation == _current_org_cache_invalidation_generation:+ _current_org_cache[cache_key] = validation.organization+ return validation.organizationdef _claims_ui_session(payload: dict[str, object] | None) -> bool:@@ -580,9 +599,10 @@ async def get_current_org_cached(x_api_key: str, db: AgentDB) -> Organization:def invalidate_cached_org(organization_id: str) -> None:- """- Drop every cached ``Organization`` entry whose id matches.- """+ """Drop every cached ``Organization`` entry whose id matches."""+ global _current_org_cache_invalidation_generation++ _current_org_cache_invalidation_generation += 1keys_to_remove = [keyfor key, value in list(_current_org_cache.items())diff --git a/skyvern/forge/sdk/workflow/exceptions.py b/skyvern/forge/sdk/workflow/exceptions.pyindex 8d43fe174..9299203eb 100644--- a/skyvern/forge/sdk/workflow/exceptions.py+++ b/skyvern/forge/sdk/workflow/exceptions.py@@ -126,6 +126,15 @@ class InvalidFileType(BaseWorkflowHTTPException):)+class FileParseTimeout(BaseWorkflowHTTPException):+ def __init__(self, file_url: str, step: str, timeout_seconds: float) -> None:+ super().__init__(+ f"Timed out after {timeout_seconds}s while running {step} on file URL {file_url}. "+ "The file is too large or too complex to parse within the time budget for a single block.",+ status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,+ )++class WorkflowDefinitionValidationException(BaseWorkflowHTTPException):"""Base exception for workflow definition validation errors."""diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.pyindex 2dc9394b9..a82bf5b7f 100644--- a/skyvern/forge/sdk/workflow/models/block.py+++ b/skyvern/forge/sdk/workflow/models/block.py@@ -27,7 +27,7 @@ from email.message import EmailMessagefrom functools import partialfrom pathlib import Path, PurePosixPathfrom types import SimpleNamespace-from typing import TYPE_CHECKING, Annotated, Any, Awaitable, Callable, ClassVar, Literal, Union, cast+from typing import TYPE_CHECKING, Annotated, Any, Awaitable, Callable, ClassVar, Literal, TypeVar, Union, castfrom urllib.parse import quote, urlparseimport aiofiles@@ -51,6 +51,7 @@ from sqlalchemy.exc import InterfaceError, OperationalErrorfrom skyvern.config import settingsfrom skyvern.constants import (AZURE_BLOB_STORAGE_MAX_UPLOAD_FILE_COUNT,+ FILE_PARSE_STEP_TIMEOUT_SECONDS,GET_DOWNLOADED_FILES_TIMEOUT,MAX_FILE_PARSE_INPUT_TOKENS,MAX_PDF_OCR_PAGES,@@ -145,6 +146,7 @@ from skyvern.forge.sdk.workflow.context_manager import BlockMetadata, WorkflowRufrom skyvern.forge.sdk.workflow.exceptions import (CustomizedCodeException,FailedToFormatJinjaStyleParameter,+ FileParseTimeout,InsecureCodeDetected,InvalidEmailClientConfiguration,InvalidFileType,@@ -201,7 +203,7 @@ from skyvern.schemas.workflows import (FileType,FileUploadDestination,)-from skyvern.services import otp_service+from skyvern.services import otp_service, planner_leversfrom skyvern.services.error_detection_service import detect_user_defined_errors_for_taskfrom skyvern.services.self_heal_cap import check_and_increment_self_heal_capfrom skyvern.utils.prompt_engine import PROMPT_HARD_CEILING_TOKENS@@ -2554,7 +2556,7 @@ class ForLoopBlock(Block):None and an empty set must stay distinct: closing everything opened after an emptybaseline closes every tab, so a failed snapshot has to suppress the reset entirely."""- if not settings.RESET_BROWSER_TABS_BETWEEN_LOOP_ITERATIONS:+ if not await planner_levers.reset_browser_tabs_between_loop_iterations(organization_id):return Nonetry:browser_state = await self._get_loop_browser_state(workflow_run_id, organization_id, browser_session_id)@@ -2575,7 +2577,9 @@ class ForLoopBlock(Block):browser_session_id: str | None,baseline_pages: set[Page] | None,) -> None:- if not settings.RESET_BROWSER_TABS_BETWEEN_LOOP_ITERATIONS or baseline_pages is None:+ if baseline_pages is None or not await planner_levers.reset_browser_tabs_between_loop_iterations(+ organization_id+ ):returntry:browser_state = await self._get_loop_browser_state(workflow_run_id, organization_id, browser_session_id)@@ -8176,6 +8180,45 @@ class SendEmailBlock(Block):)+_ParseStepResult = TypeVar("_ParseStepResult")+++async def _run_blocking_parse_step(+ step: str,+ file_url: str,+ func: Callable[..., _ParseStepResult],+ *args: Any,+ **kwargs: Any,+) -> _ParseStepResult:+ """Run a blocking file-parse step in a worker thread under a wall-clock ceiling.++ Parsing on the event loop starves the workflow activity's heartbeat task, and Temporal+ reaps a run whose activity stops heartbeating. On timeout the worker thread is left to+ run to completion — threads cannot be cancelled — so the ceiling bounds the run's+ exposure to a pathological document rather than the CPU work itself.+ """+ try:+ return await asyncio.wait_for(+ asyncio.to_thread(func, *args, **kwargs),+ timeout=FILE_PARSE_STEP_TIMEOUT_SECONDS,+ )+ except asyncio.TimeoutError:+ LOG.warning(+ "File parse step exceeded its time budget",+ step=step,+ file_url=file_url,+ timeout_seconds=FILE_PARSE_STEP_TIMEOUT_SECONDS,+ )+ raise FileParseTimeout(file_url=file_url, step=step, timeout_seconds=FILE_PARSE_STEP_TIMEOUT_SECONDS)+++# csv.field_size_limit is process-global. Raising it once at import (rather than+# set/restore around each parse) avoids a race between concurrent CSV parses on+# separate worker threads stepping on each other's limit.+_MAX_CSV_FIELD_SIZE_BYTES = 10 * 1024 * 1024+csv.field_size_limit(_MAX_CSV_FIELD_SIZE_BYTES)++class FileParserBlock(Block):# There is a mypy bug with Literal. Without the type: ignore, mypy will raise an error:# Parameter 1 of Literal[...] cannot be of type "Any"@@ -8185,8 +8228,6 @@ class FileParserBlock(Block):_CSV_SNIFF_LINES = 5_CSV_BINARY_PREFIX_BYTES = 4096_CSV_UTF_BOMS = (codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE, codecs.BOM_UTF32_LE, codecs.BOM_UTF32_BE)- # Bounded cap for legitimate wide cells (JSON blobs, long descriptions); applied only while parsing.- _MAX_CSV_FIELD_SIZE_BYTES = 10 * 1024 * 1024# ZIP extraction guards (zip-bomb protection; sizes from central-directory metadata).# ClassVar keeps these plain class attributes — without it pydantic wraps underscore# names in ModelPrivateAttr and class-level access breaks.@@ -8418,14 +8459,13 @@ class FileParserBlock(Block):async def _parse_csv_file(self, file_path: str) -> list[dict[str, Any]]:"""Parse CSV/TSV file and return list of dictionaries."""+ return await _run_blocking_parse_step("CSV parsing", self.file_url, self._parse_csv_file_sync, file_path)++ def _parse_csv_file_sync(self, file_path: str) -> list[dict[str, Any]]:delimiter, encoding = self._sniff_csv_delimiter(file_path)- previous_limit = csv.field_size_limit(self._MAX_CSV_FIELD_SIZE_BYTES)- try:- with open(file_path, encoding=encoding, errors="replace", newline="") as file:- reader = csv.DictReader(file, delimiter=delimiter)- return list(reader)- finally:- csv.field_size_limit(previous_limit)+ with open(file_path, encoding=encoding, errors="replace", newline="") as file:+ reader = csv.DictReader(file, delimiter=delimiter)+ return list(reader)def _clean_dataframe_for_json(self, df: pd.DataFrame) -> list[dict[str, Any]]:"""Clean DataFrame to ensure it can be serialized to JSON."""@@ -8451,6 +8491,9 @@ class FileParserBlock(Block):async def _parse_excel_file(self, file_path: str) -> list[dict[str, Any]]:"""Parse Excel file and return list of dictionaries."""+ return await _run_blocking_parse_step("Excel parsing", self.file_url, self._parse_excel_file_sync, file_path)++ def _parse_excel_file_sync(self, file_path: str) -> list[dict[str, Any]]:try:# Read Excel file with pandas, specifying engine explicitlydf = pd.read_excel(file_path, engine="calamine")@@ -8481,7 +8524,13 @@ class FileParserBlock(Block):renders pages as images and sends them to a vision LLM for OCR."""try:- extracted_text = extract_pdf_file(file_path, file_identifier=self.file_url)+ extracted_text = await _run_blocking_parse_step(+ "PDF text extraction",+ self.file_url,+ extract_pdf_file,+ file_path,+ file_identifier=self.file_url,+ )except PDFParsingError as e:raise InvalidFileType(file_url=self.file_url, file_type=self.file_type, error=str(e))@@ -8497,7 +8546,9 @@ class FileParserBlock(Block):file_url=self.file_url,)try:- page_images = await asyncio.to_thread(+ page_images = await _run_blocking_parse_step(+ "PDF page rendering",+ self.file_url,render_pdf_pages_as_images,file_path,file_identifier=self.file_url,@@ -8727,6 +8778,11 @@ class FileParserBlock(Block):Extracts text from all paragraphs and tables in the document,respecting the token limit."""+ return await _run_blocking_parse_step(+ "DOCX parsing", self.file_url, self._parse_docx_file_sync, file_path, max_tokens+ )++ def _parse_docx_file_sync(self, file_path: str, max_tokens: int = MAX_FILE_PARSE_INPUT_TOKENS) -> str:try:document = docx.Document(file_path)text_parts = []@@ -9141,8 +9197,10 @@ class FileParserBlock(Block):if self.file_type not in (FileType.IMAGE, FileType.EXCEL, FileType.PDF, FileType.DOCX, FileType.ZIP):self.file_type = self._detect_file_type_from_url(self.file_url, file_path=file_path)- # Validate the file type- self.validate_file_type(self.file_url, file_path)+ # Validation opens the document, so on a large file it is as slow as the parse itself.+ await _run_blocking_parse_step(+ "file type validation", self.file_url, self.validate_file_type, self.file_url, file_path+ )except Exception as e:return await self._record_failure(workflow_run_context,@@ -9320,7 +9378,22 @@ class PDFParserBlock(Block):)try:- extracted_text = extract_pdf_file(file_path, file_identifier=self.file_url)+ extracted_text = await _run_blocking_parse_step(+ "PDF text extraction",+ self.file_url,+ extract_pdf_file,+ file_path,+ file_identifier=self.file_url,+ )+ except FileParseTimeout as e:+ return await self.build_block_result(+ success=False,+ failure_reason=str(e),+ output_parameter_value=None,+ status=BlockStatus.failed,+ workflow_run_block_id=workflow_run_block_id,+ organization_id=organization_id,+ )except PDFParsingError:return await self.build_block_result(success=False,diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.pyindex 1634346c5..91c9adf8a 100644--- a/skyvern/forge/sdk/workflow/service.py+++ b/skyvern/forge/sdk/workflow/service.py@@ -86,7 +86,7 @@ from skyvern.forge.sdk.experimentation.transient_ui_capture import resolve_transfrom skyvern.forge.sdk.forge_log import exception_log_fieldsfrom skyvern.forge.sdk.models import Step, StepStatusfrom skyvern.forge.sdk.schemas.browser_profiles import BrowserProfile-from skyvern.forge.sdk.schemas.credentials import Credential+from skyvern.forge.sdk.schemas.credentials import Credential, credential_auto_profile_disabledfrom skyvern.forge.sdk.schemas.files import FileInfofrom skyvern.forge.sdk.schemas.organizations import Organizationfrom skyvern.forge.sdk.schemas.persistent_browser_sessions import (@@ -3100,6 +3100,10 @@ class WorkflowService:)if not (db_cred and db_cred.browser_profile_id):continue+ # An opt-out makes this credential seed like one with no linked profile, so a sibling+ # credential in the same pool still resolves normally.+ if credential_auto_profile_disabled(db_cred):+ continue# Verify the profile still exists (mirrors the mid-run resolver). Best-effort: a# transient repository failure degrades to a fresh seed rather than failing setup.profile = await app.DATABASE.browser_sessions.get_browser_profile(@@ -6058,6 +6062,7 @@ class WorkflowService:workflow_run_id=workflow_run_id,organization_id=organization_id,workflow_permanent_id=workflow_run.workflow_permanent_id,+ workflow_run=workflow_run,)# Save the original navigation goal before any mutation so# retries don't stack the browser-session prefix repeatedly.@@ -6271,6 +6276,7 @@ class WorkflowService:workflow_run_id: str | None,organization_id: str | None,workflow_permanent_id: str | None,+ workflow_run: WorkflowRun | None = None,) -> str | None:"""Inspect the block-level parameters and return the browser_profile_idfrom the credential parameter bound to this specific block."""@@ -6294,6 +6300,14 @@ class WorkflowService:organization_id=organization_id,)if db_cred and db_cred.browser_profile_id:+ if workflow_run is not None:+ engine_enabled = await app.AGENT_FUNCTION.is_browser_memory_engine_enabled(workflow_run)+ else:+ engine_enabled = await app.AGENT_FUNCTION.is_browser_memory_engine_enabled_for_org(+ organization_id+ )+ if engine_enabled and credential_auto_profile_disabled(db_cred):+ continue# Verify the browser profile still exists before using itprofile = await app.DATABASE.browser_sessions.get_browser_profile(profile_id=db_cred.browser_profile_id,diff --git a/skyvern/services/planner_levers.py b/skyvern/services/planner_levers.pynew file mode 100644index 000000000..ef9d19ea5--- /dev/null+++ b/skyvern/services/planner_levers.py@@ -0,0 +1,43 @@+import structlog++from skyvern.config import settings+from skyvern.forge import app++LOG = structlog.get_logger()+# Tracks TASK_V2_CONVERGE_PCT in infra/k8s/temporal-worker/values-benchmark-hetzner.yaml; the flag+# carries no value of its own, so re-tuning the fleet means re-tuning this too.+DEFAULT_CONVERGE_PCT = 20+++async def is_planner_levers_enabled(organization_id: str | None) -> bool:+ if not organization_id:+ return False+ try:+ return bool(+ await app.EXPERIMENTATION_PROVIDER.is_feature_enabled_cached(+ "TASK_V2_PLANNER_LEVERS",+ organization_id,+ properties={"organization_id": organization_id},+ )+ )+ except Exception:+ LOG.warning("Failed to resolve TASK_V2_PLANNER_LEVERS feature flag", exc_info=True)+ return False+++async def skip_completion_check_after_navigate(organization_id: str | None) -> bool:+ return settings.TASK_V2_SKIP_COMPLETION_CHECK_AFTER_NAVIGATE or await is_planner_levers_enabled(organization_id)+++async def carry_subgoals(organization_id: str | None) -> bool:+ return settings.TASK_V2_CARRY_SUBGOALS or await is_planner_levers_enabled(organization_id)+++async def reset_browser_tabs_between_loop_iterations(organization_id: str | None) -> bool:+ return settings.RESET_BROWSER_TABS_BETWEEN_LOOP_ITERATIONS or await is_planner_levers_enabled(organization_id)+++async def converge_pct(organization_id: str | None) -> int:+ if settings.TASK_V2_CONVERGE_PCT:+ return settings.TASK_V2_CONVERGE_PCT+ return DEFAULT_CONVERGE_PCT if await is_planner_levers_enabled(organization_id) else 0diff --git a/skyvern/services/task_v2_service.py b/skyvern/services/task_v2_service.pyindex 527e1b3f3..dddc62ae8 100644--- a/skyvern/services/task_v2_service.py+++ b/skyvern/services/task_v2_service.py@@ -87,6 +87,7 @@ from skyvern.schemas.workflows import (WorkflowDefinitionYAML,WorkflowStatus,)+from skyvern.services import planner_leversfrom skyvern.services.webhook_delivery import deliver_webhook_with_retries, describe_delivery_errorfrom skyvern.utils.prompt_engine import load_prompt_with_elementsfrom skyvern.utils.strings import generate_random_string@@ -685,14 +686,11 @@ def _should_run_post_block_completion_check(task_type: str,*,navigate_completion_check_enabled: bool,+ skip_completion_check_after_navigate: bool,) -> bool:if block_success is not True:return False- if (- settings.TASK_V2_SKIP_COMPLETION_CHECK_AFTER_NAVIGATE- and task_type == "navigate"- and navigate_completion_check_enabled- ):+ if skip_completion_check_after_navigate and task_type == "navigate" and navigate_completion_check_enabled:return Falsereturn True@@ -939,6 +937,10 @@ async def run_task_v2_helper(except Exception:LOG.exception("Failed to load Google fallback", exc_info=True, url=url, current_url=current_url)+ skip_completion_check_after_navigate_enabled = await planner_levers.skip_completion_check_after_navigate(+ organization_id+ )+if i == 0 and current_url != url:if should_fallback:plan = f"Go to Google because the intended website ({url}) failed to load properly."@@ -978,13 +980,16 @@ async def run_task_v2_helper(continuecurrent_url = current_url if current_url else str(await SkyvernFrame.get_url(frame=page) if page else url)- iterations_remaining = _converge_iterations_remaining(i, max_iterations, settings.TASK_V2_CONVERGE_PCT)+ iterations_remaining = _converge_iterations_remaining(+ i, max_iterations, await planner_levers.converge_pct(organization_id)+ )try:open_tabs_context = await build_open_tabs_context(browser_state, page)except Exception:LOG.warning("Failed to build open-tabs context for the planner", exc_info=True)open_tabs_context = Noneplanner_mini_goal_improvements = await _is_planner_mini_goal_improvements_enabled(organization_id)+ carry_subgoals_enabled = await planner_levers.carry_subgoals(organization_id)task_v2_prompt = load_prompt_with_elements(scraped_page,prompt_engine,@@ -995,7 +1000,7 @@ async def run_task_v2_helper(open_tabs_context=open_tabs_context,local_datetime=datetime.now(context.tz_info).isoformat(),compute_enabled=compute_enabled,- prior_required_subgoals=prior_required_subgoals if settings.TASK_V2_CARRY_SUBGOALS else None,+ prior_required_subgoals=prior_required_subgoals if carry_subgoals_enabled else None,iterations_remaining=iterations_remaining,step_budget=organization.max_steps_per_run or settings.MAX_STEPS_PER_RUN,planner_mini_goal_improvements=planner_mini_goal_improvements,@@ -1037,7 +1042,7 @@ async def run_task_v2_helper((task_v2_response.get("complete_criterion") or None) if planner_mini_goal_improvements else None)complete_criterion_is_untrusted = bool(complete_criterion)- if settings.TASK_V2_CARRY_SUBGOALS:+ if carry_subgoals_enabled:_subgoals = task_v2_response.get("required_subgoals")if _subgoals:prior_required_subgoals = _subgoals@@ -1289,6 +1294,7 @@ async def run_task_v2_helper(block_result.success,task_type,navigate_completion_check_enabled=bool(complete_criterion),+ skip_completion_check_after_navigate=skip_completion_check_after_navigate_enabled,):completion_screenshots: list[bytes] = []completion_scraped_page: ScrapedPage | None = Nonediff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex 4bc7a48f2..61a61c0b7 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -12,7 +12,7 @@ from collections import dequefrom datetime import datetime, timedelta, timezonefrom enum import StrEnumfrom pathlib import Path-from typing import Any, Awaitable, Callable, List, NamedTuple, TypedDict+from typing import Any, AsyncIterator, Awaitable, Callable, List, NamedTuple, TypedDict, castimport structlogfrom cachetools import TTLCache@@ -50,6 +50,7 @@ from skyvern.exceptions import (FailToSelectByIndex,FailToSelectByLabel,FailToSelectByValue,+ HttpException,IllegitComplete,ImaginaryFileUrl,ImaginarySecretValue,@@ -74,6 +75,7 @@ from skyvern.exceptions import (PhoneNumberInputMismatch,SecretInputMismatch,SkyvernException,+ SkyvernHTTPException,SkyvernPageAnalysisTimeout,UnresolvableHost,)@@ -81,8 +83,10 @@ from skyvern.experimentation.wait_utils import get_or_create_wait_config, get_wafrom skyvern.forge import appfrom skyvern.forge.prompts import prompt_enginefrom skyvern.forge.sdk.api.files import (+ GuardedFileFetchHopResult,calculate_sha256_for_file,check_downloading_files_and_wait_for_download_to_complete,+ fetch_file_bytes,get_download_dir,list_files_in_directory,make_temp_directory,@@ -101,6 +105,7 @@ from skyvern.forge.sdk.cache import extraction_cache, extraction_shadowfrom skyvern.forge.sdk.copilot.block_goal_wrapping import unwrap_goal_fieldsfrom skyvern.forge.sdk.core import skyvern_contextfrom skyvern.forge.sdk.core.hashing import diagnostic_fingerprint+from skyvern.forge.sdk.core.http_request_authorization import RedirectHopAuthorizerfrom skyvern.forge.sdk.core.skyvern_context import PendingFileChooserListener, ensure_contextfrom skyvern.forge.sdk.db.datetime_utils import naive_utc_nowfrom skyvern.forge.sdk.event.factory import EventStrategyFactory@@ -158,12 +163,10 @@ from skyvern.webeye.browser_engine import UNSET_SELECTION, BrowserEngineSelectiofrom skyvern.webeye.browser_factory import initialize_download_dir, resolve_artifact_pathfrom skyvern.webeye.browser_state import BrowserStatefrom skyvern.webeye.cdp_download_interceptor import (- DOWNLOAD_DESTINATION_ERRORS,DOWNLOAD_MIME_TYPES,MAX_FILE_SIZE_BYTES,download_filename_from_suffix,extract_filename,- fetch_download_through_request_context,is_download_response,normalize_download_filename,settle_browser_downloads_for_context,@@ -954,10 +957,64 @@ async def _close_eager_capture_then_teardown_retention(LOG.debug("Failed to tear down blob URL retention", workflow_run_id=workflow_run_id)+async def _adopted_session_download_binding(+ download: Download,+ active_page: Page,+) -> AsyncIterator[tuple[Any, "RedirectHopAuthorizer[GuardedFileFetchHopResult]"]]:+ """Lease the exact context binding that owns an adopted-session download."""+ download_page = download.page+ if download_page is None:+ raise RuntimeError("Adopted-session download has no owning page")+ download_context = download_page.context+ if download_context is not active_page.context:+ raise RuntimeError("Adopted-session download page context does not match the active page context")++ try:+ bind_lock = download_context._skyvern_cdp_download_interceptor_bind_lock # type: ignore[attr-defined]+ except AttributeError as exc:+ raise RuntimeError("Adopted-session download context has no interceptor ownership lock") from exc+ if not isinstance(bind_lock, asyncio.Lock):+ raise RuntimeError("Adopted-session download context has an invalid interceptor ownership lock")++ # Narrow exception to the usual explicit-injection rule: bind_to_context already stores the+ # constructor-injected interceptor on its owned context. Holding its established bind lock+ # keeps that exact Page -> BrowserContext -> interceptor association live through the fallback.+ async with bind_lock:+ try:+ download_interceptor = download_context._skyvern_cdp_download_interceptor # type: ignore[attr-defined]+ except AttributeError as exc:+ raise RuntimeError(+ "Adopted-session download recovery requires the page context's CDP download interceptor"+ ) from exc+ try:+ interceptor_context = download_interceptor._page_context+ except AttributeError as exc:+ raise RuntimeError("Bound CDP download interceptor has no page-context ownership binding") from exc+ if interceptor_context is not download_context:+ raise RuntimeError("Bound CDP download interceptor does not own the adopted-session download page context")+ try:+ authorize_request_hop = download_interceptor._redirect_hop_authorizer+ except AttributeError as exc:+ raise RuntimeError("Bound CDP download interceptor has no redirect hop authorizer") from exc+ if not callable(authorize_request_hop):+ raise RuntimeError("Bound CDP download interceptor has an invalid redirect hop authorizer")+ yield (+ download_interceptor,+ cast(+ "RedirectHopAuthorizer[GuardedFileFetchHopResult]",+ authorize_request_hop,+ ),+ )++async def _save_adopted_session_download(download: Download,page: Page,download_dir: Path,+ *,+ authorize_request_hop: "RedirectHopAuthorizer[GuardedFileFetchHopResult]",+ request_headers: dict[str, str],workflow_run_id: str | None = None,eager_blob_bytes: bytes | None = None,download_binding: DownloadBinding = DownloadBinding.RUN_DIR,@@ -1009,9 +1066,8 @@ async def _save_adopted_session_download()# Ordering: ``save_as`` above has already run and failed (empty or raised).- # ``blob:`` URLs cannot be fetched via APIRequestContext (Playwright rejects- # the scheme), so route them through an in-page fetch from the document that- # owns the blob. That document may be a different tab than the one clicked, so+ # ``blob:`` URLs cannot be fetched by the guarded HTTP client, so route them+ # through an in-page fetch from the document that owns the blob. That document may be a different tab, so# probe every open page (owner first) rather than reading from ``page`` alone.if download.url.startswith("blob:"):blob_bytes = await _read_adopted_session_blob_bytes(download, page, workflow_run_id=workflow_run_id)@@ -1050,16 +1106,12 @@ async def _save_adopted_session_download(return download_targettry:- response = await fetch_download_through_request_context(page.context.request, download.url)- if response.status != 200:- LOG.error(- "Adopted-session download url re-fetch returned non-200 status",- status=response.status,- workflow_run_id=workflow_run_id,- )- return None- # APIResponse.body() has no streaming variant, so a large download peaks at 2x its size in RSS.- body = await response.body()+ response = await fetch_file_bytes(+ download.url,+ headers=request_headers,+ authorize_request_hop=authorize_request_hop,+ )+ body = response.bodyif not body:LOG.error("Adopted-session download url re-fetch returned an empty body",@@ -1068,7 +1120,7 @@ async def _save_adopted_session_download(return Nonedownload_target.write_bytes(body)return download_target- except DOWNLOAD_DESTINATION_ERRORS as e:+ except (SkyvernHTTPException, HttpException) as e:LOG.error("Adopted-session download destination refused",download_dir=str(download_dir),@@ -3519,19 +3571,30 @@ class ActionHandler:else DownloadBinding.RUN_DIR)download_event_fallback_attempted = True- saved_path = await _save_adopted_session_download(- captured_download,- page,- download_dir,- workflow_run_id=task.workflow_run_id,- eager_blob_bytes=await eager_blob_capture.result(- timeout=min(- EAGER_BLOB_READ_TIMEOUT_SECONDS,- _remaining_download_wait_seconds(),- )- ),- download_binding=resolved_download_binding,+ eager_blob_bytes = await eager_blob_capture.result(+ timeout=min(+ EAGER_BLOB_READ_TIMEOUT_SECONDS,+ _remaining_download_wait_seconds(),+ ))+ async with _adopted_session_download_binding(captured_download, page) as (+ download_interceptor,+ authorize_request_hop,+ ):+ cookie_header = await download_interceptor._cookie_header_for_url(+ captured_download.url+ )+ request_headers = {"Cookie": cookie_header} if cookie_header else {}+ saved_path = await _save_adopted_session_download(+ captured_download,+ page,+ download_dir,+ authorize_request_hop=authorize_request_hop,+ request_headers=request_headers,+ workflow_run_id=task.workflow_run_id,+ eager_blob_bytes=eager_blob_bytes,+ download_binding=resolved_download_binding,+ )if saved_path is not None:download_event_fallback_used = Truedownload_triggered = Truediff --git a/skyvern/webeye/actions/handler_utils.py b/skyvern/webeye/actions/handler_utils.pyindex 466eb9efd..d73e65bbf 100644--- a/skyvern/webeye/actions/handler_utils.py+++ b/skyvern/webeye/actions/handler_utils.py@@ -22,7 +22,19 @@ async def download_file() -> str | list[str]:if file_url.startswith("/"):run_id = resolve_run_download_id(skyvern_context.current())- return validate_local_file_path(file_url, run_id)+ try:+ return validate_local_file_path(file_url, run_id)+ except PermissionError:+ # Expected when the LLM/user supplies an absolute path outside the run's download+ # directory. The containment guard is doing its job; the action fails cleanly rather+ # than surfacing as an unhandled ERROR with a traceback.+ LOG.warning(+ "Local file path is outside the allowed download directory, continuing without it",+ action=action,+ file_url=file_url,+ run_id=run_id,+ )+ return []try:return await download_file_api(file_url, organization_id=organization_id)diff --git a/skyvern/webeye/browser_factory.py b/skyvern/webeye/browser_factory.pyindex 0955aeb32..4c56c1771 100644--- a/skyvern/webeye/browser_factory.py+++ b/skyvern/webeye/browser_factory.py@@ -45,6 +45,8 @@ from skyvern.exceptions import ()from skyvern.forge import appfrom skyvern.forge.sdk.api.files import get_download_dir, make_temp_directory, resolve_run_download_id+from skyvern.forge.sdk.browser_network_egress_monitor import BrowserNetworkEgressMonitor+from skyvern.forge.sdk.core.http_request_authorization import deny_unenrolled_redirect_hopfrom skyvern.forge.sdk.core.skyvern_context import current, ensure_contextfrom skyvern.schemas.runs import ProxyLocation, ProxyLocationInput, get_tzinfo_from_proxyfrom skyvern.webeye.browser_artifacts import BrowserArtifacts, DownloadBinding, VideoArtifact@@ -1205,7 +1207,13 @@ async def _connect_to_cdp_browser(# This captures downloads via the Fetch domain and saves them locally.if parsed_headers.enable_download:download_dir = initialize_download_dir()- interceptor = CDPDownloadInterceptor(output_dir=download_dir)+ # No run-scoped network authority is threaded into this OSS creator; the interceptor stays+ # unenrolled, which fails closed on every intercepted request rather than defaulting open.+ interceptor = CDPDownloadInterceptor(+ output_dir=download_dir,+ network_egress_monitor=BrowserNetworkEgressMonitor.unenrolled(),+ redirect_hop_authorizer=deny_unenrolled_redirect_hop,+ )# Enable interception on all existing pagesfor page in browser_context.pages:diff --git a/skyvern/webeye/cdp_download_interceptor.py b/skyvern/webeye/cdp_download_interceptor.pyindex c2e2f2f88..8b6e74b97 100644--- a/skyvern/webeye/cdp_download_interceptor.py+++ b/skyvern/webeye/cdp_download_interceptor.py@@ -9,9 +9,9 @@ ignoring Linux paths).Flow:1. Enable Fetch interception for each page:- Response stage: detect and intercept downloads- - Request stage (when proxy auth configured): enable Fetch.authRequired for proxy 407 challenges+ - Request stage: mediate every active request before dispatch and enable proxy auth challenges2. On each paused request:- - Request stage → Fetch.continueRequest (pass through to server)+ - Request stage → authorize, then continue or fail closed before dispatch- Response non-download → Fetch.continueResponse (pass through)- Response download → extract body via stream → save to disk → Fetch.fulfillRequest"""@@ -21,38 +21,37 @@ from __future__ import annotationsimport asyncioimport base64import binascii+import errnoimport hashlibimport inspectimport osimport re-import ssl+import statimport time-import urllib.error-import urllib.requestimport uuid-from collections.abc import AsyncIterator, Coroutine+from collections.abc import AsyncIterator, Callable, Coroutinefrom contextlib import asynccontextmanagerfrom dataclasses import dataclassfrom pathlib import Path-from typing import IO, Any, Literal+from typing import IO, TYPE_CHECKING, Any, Literalfrom urllib.parse import unquote, urlparseimport structlogfrom playwright.async_api import Browser, BrowserContext, CDPSession, Pagefrom skyvern.constants import BROWSER_DOWNLOADING_SUFFIX, BROWSER_INTERCEPTOR_DISABLE_TIMEOUT-from skyvern.exceptions import HttpException, SkyvernHTTPException-from skyvern.forge.sdk.core.aiohttp_helper import strip_cross_origin_redirect_credentials-from skyvern.utils.url_validators import (- MAX_SAFE_REDIRECTS,- SAFE_REDIRECT_STATUS_CODES,- validate_fetch_url,- validate_redirect_url,-)+from skyvern.forge.sdk.api import files as file_apifrom skyvern.webeye.utils.page import SkyvernFrame+if TYPE_CHECKING:+ from skyvern.forge.sdk.api.files import GuardedFileFetchHopResult+ from skyvern.forge.sdk.browser_network_egress_monitor import BrowserNetworkEgressMonitor+ from skyvern.forge.sdk.core.http_request_authorization import RedirectHopAuthorizer+LOG = structlog.get_logger()+CDP_DOWNLOAD_HTTP_SINK_KIND = "browser.download.http"+_DETACHED_DISABLE_TASKS: set[asyncio.Task[None]] = set()@@ -109,6 +108,13 @@ class _StreamOutcome:total_bytes: int+@dataclass+class _ConfinedTemporaryFile:+ directory_fd: int+ name: str+ handle: IO[bytes]++class _StreamStartError(Exception):"""Fetch.takeResponseBodyAsStream failed before any body bytes were consumed (body not taken)."""@@ -362,61 +368,6 @@ def _body_starts_with_html(data: bytes) -> bool:return head.startswith(b"<!doctype html") or bool(_HTML_START_TAG_RE.match(head))-def _has_control_chars(text: str) -> bool:- return any(ord(ch) < 0x20 or ord(ch) == 0x7F for ch in text)---# A refused destination and an exhausted hop budget both mean "do not fetch this"; callers must-# treat them as a decision, not as a transport failure worth retrying on another client.-DOWNLOAD_DESTINATION_ERRORS = (SkyvernHTTPException, HttpException)---def _header_value(headers: dict[str, str], name: str) -> str | None:- lowered = name.lower()- return next((value for key, value in headers.items() if key.lower() == lowered), None)---class _NoRedirectHandler(urllib.request.HTTPRedirectHandler):- def redirect_request(self, *args: Any, **kwargs: Any) -> None:- return None---def _urlopen_single_hop(- request: urllib.request.Request, ssl_context: ssl.SSLContext-) -> tuple[int, dict[str, str], bytes]:- """Perform one HTTP hop without following redirects, so the next hop can be checked first."""- opener = urllib.request.build_opener(_NoRedirectHandler, urllib.request.HTTPSHandler(context=ssl_context))- try:- with opener.open(request) as response:- return response.status, dict(response.headers), response.read()- except urllib.error.HTTPError as error:- # Suppressing redirects turns them into errors here, so a redirect is returned for the- # caller to check. Every other error status stays an error: its body is not the file.- if error.code not in SAFE_REDIRECT_STATUS_CODES:- raise- with error:- return error.code, dict(error.headers), error.read()---async def fetch_download_through_request_context(request_context: Any, url: str) -> Any:- """Return the final response for a page-supplied download URL.-- The destination is checked before the initial request and again before every redirect hop, so- no hop carries cookies or headers to a host that has not been cleared. Raises on a refused- destination, a redirect without a target, or an exhausted hop budget.- """- current_url = await asyncio.to_thread(validate_fetch_url, url)- for _ in range(MAX_SAFE_REDIRECTS + 1):- response = await request_context.get(current_url, max_redirects=0)- if response.status not in SAFE_REDIRECT_STATUS_CODES:- return response- location = _header_value(dict(response.headers), "location")- if not location:- raise HttpException(response.status, current_url, "Redirected download provided no target")- current_url = await asyncio.to_thread(validate_redirect_url, current_url, location)- raise HttpException(400, current_url, "Too many redirects while fetching a download")--def _payload_is_html_login_masquerade(data: bytes, content_type: str, filename: str) -> bool:"""True when a download's bytes are an HTML document but the download does not claim to be HTML.@@ -471,6 +422,27 @@ def normalize_download_filename(filename: str, content_type: str = "") -> str:return filename+def _validated_download_basename(filename: str, content_type: str = "") -> str:+ decoded_filename = filename.strip()+ while True:+ next_filename = unquote(decoded_filename)+ if next_filename == decoded_filename:+ break+ decoded_filename = next_filename+ if not decoded_filename:+ return ""+ if (+ decoded_filename in {".", ".."}+ or bool(_FILENAME_CONTROL_CHAR_RE.search(decoded_filename))+ or "/" in decoded_filename+ or "\\" in decoded_filename+ or bool(_WINDOWS_DRIVE_RE.match(decoded_filename))+ or Path(decoded_filename).is_absolute()+ ):+ raise ValueError("download filename must be a basename")+ return normalize_download_filename(decoded_filename, content_type)++def download_filename_from_suffix(download_suffix: str, source_extension: str, existing_names: set[str]) -> str:"""Filename for a download whose block configured ``download_suffix``"""existing_names = {Path(n).name for n in existing_names} # contract: dedup on basenames, never full paths@@ -596,11 +568,21 @@ class CDPDownloadInterceptor:output_dir: str | None = None,proxy_username: str | None = None,proxy_password: str | None = None,+ *,+ network_egress_monitor: BrowserNetworkEgressMonitor,+ redirect_hop_authorizer: RedirectHopAuthorizer[GuardedFileFetchHopResult],) -> None:+ if network_egress_monitor is None or redirect_hop_authorizer is None:+ raise TypeError("browser download required collaborators must be enrolled")self._output_dir: Path | None = Path(output_dir) if output_dir else None+ self._download_directory_identities: dict[Path, tuple[int, int]] = {}self._proxy_username: str | None = proxy_usernameself._proxy_password: str | None = proxy_password+ self._network_egress_monitor = network_egress_monitor+ self._redirect_hop_authorizer = redirect_hop_authorizerself._cdp_sessions: list[CDPSession] = []+ self._active_request_interceptors: dict[CDPSession, tuple[Page, Callable[[Page], None]]] = {}+ self._enrolling_pages: list[Page] = []self._enabled = Falseself._download_index = 0# Track auth attempts per requestId to prevent infinite retry loops@@ -648,6 +630,7 @@ class CDPDownloadInterceptor:if dir_changed:self._downloaded_urls.clear()self._output_dir.mkdir(parents=True, exist_ok=True)+ self._remember_download_directory(self._output_dir)LOG.info("CDP download interceptor download dir set", download_dir=download_dir, dir_changed=dir_changed)def is_monitoring_browser_downloads(self) -> bool:@@ -658,17 +641,18 @@ class CDPDownloadInterceptor:def _resolve_save_path(self, filename: str = "", content_type: str = "") -> tuple[Path, str]:"""Generate a unique save path under _output_dir.- Sanitizes the filename (path traversal prevention), falls back to a UUID-based+ Rejects non-basename filenames, falls back to a UUID-basedname when empty, increments _download_index, and logs a warning if a file with- the same name already exists. Returns (save_path, sanitized_filename).+ the same name already exists. Returns (save_path, validated_filename).Callers can pass a raw or empty filename — this method handles all normalization."""assert self._output_dir is not Noneself._output_dir.mkdir(parents=True, exist_ok=True)+ self._remember_download_directory(self._output_dir)self._download_index += 1- filename = normalize_download_filename(filename, content_type)+ filename = _validated_download_basename(filename, content_type)if not filename:filename = f"download_{uuid.uuid4().hex[:8]}{_download_extension_for_content_type(content_type)}"@@ -678,7 +662,9 @@ class CDPDownloadInterceptor:save_path = self._output_dir / filename# TODO: implement proper filename dedup (e.g., content hash or UUID suffix)if save_path.exists():- LOG.warning("Download filename collision, overwriting", filename=filename, save_path=str(save_path))+ LOG.warning(+ "Download filename collision; write will fail closed", filename=filename, save_path=str(save_path)+ )return save_path, filename@@ -690,6 +676,17 @@ class CDPDownloadInterceptor:Playwright uses Request-stage interception with handleAuthRequests to receiveproxy 407 challenges via Fetch.authRequired."""+ if any(candidate is page for candidate in self._enrolling_pages) or any(+ registered_page is page for registered_page, _ in self._active_request_interceptors.values()+ ):+ raise RuntimeError("CDP interception is already active or enrolling for this page")+ self._enrolling_pages.append(page)+ try:+ await self._enable_for_page(page)+ finally:+ self._enrolling_pages = [candidate for candidate in self._enrolling_pages if candidate is not page]++ async def _enable_for_page(self, page: Page) -> None:self._accepting_cdp_handlers = Truecdp_session = await page.context.new_cdp_session(page)cdp_session.on("Fetch.requestPaused", lambda event: self._on_request_paused(event, cdp_session))@@ -699,24 +696,46 @@ class CDPDownloadInterceptor:if has_proxy_auth:cdp_session.on("Fetch.authRequired", lambda event: self._on_auth_required(event, cdp_session))- # Always intercept Response stage for download detection.- # When proxy auth is needed, also intercept Request stage (like Playwright's- # CRNetworkManager) — Chrome requires Request-stage patterns for- # Fetch.authRequired to fire on proxy 407 challenges.- patterns: list[dict[str, str]] = [{"requestStage": "Response"}]- if has_proxy_auth:- # urlPattern "*" intercepts all requests at Request stage, which adds overhead.- # This is required: Chrome only fires Fetch.authRequired for proxy 407 challenges- # when a Request-stage pattern is registered.- patterns.append({"urlPattern": "*", "requestStage": "Request"})-- await cdp_session.send(- "Fetch.enable",- {- "patterns": patterns,- "handleAuthRequests": has_proxy_auth,- },- )+ # Request-stage interception is the last boundary before browser credentials go on the+ # wire. It is mandatory even without proxy auth; the active request must consume the+ # monitor's exact one-shot slot before Fetch.continueRequest can dispatch it.+ patterns: list[dict[str, str]] = [+ {"requestStage": "Response"},+ {"urlPattern": "*", "requestStage": "Request"},+ ]++ try:+ await cdp_session.send(+ "Fetch.enable",+ {+ "patterns": patterns,+ "handleAuthRequests": has_proxy_auth,+ },+ )+ self._network_egress_monitor.register_active_request_interceptor(page=page, owner=self)++ def close_listener(closed_page: Page) -> None:+ # This listener is bound to `page`, so a close event carrying any other page means+ # the registration is misrouted and the monitor can no longer be trusted to describe+ # this page's egress. Normal self-close takes the plain unregister path.+ if closed_page is not page:+ self._network_egress_monitor.invalidate()+ self._unregister_active_request_interceptor(cdp_session)++ self._active_request_interceptors[cdp_session] = (page, close_listener)+ if page.is_closed() is True:+ raise RuntimeError("page closed during interceptor registration")+ page.on("close", close_listener)+ except BaseException:+ self._accepting_cdp_handlers = False+ if cdp_session in self._active_request_interceptors:+ self._unregister_active_request_interceptor(cdp_session)+ self._network_egress_monitor.invalidate()+ try:+ await cdp_session.send("Fetch.disable")+ except Exception:+ pass+ raiseself._cdp_sessions.append(cdp_session)self._enabled = TrueLOG.info(@@ -727,6 +746,21 @@ class CDPDownloadInterceptor:proxy_auth_enabled=has_proxy_auth,)+ def _unregister_active_request_interceptor(self, cdp_session: CDPSession) -> None:+ registration = self._active_request_interceptors.pop(cdp_session, None)+ if registration is None:+ return+ page, close_listener = registration+ try:+ page.remove_listener("close", close_listener)+ except Exception:+ pass+ try:+ self._network_egress_monitor.unregister_active_request_interceptor(page=page, owner=self)+ except Exception as error:+ self._network_egress_monitor.invalidate()+ LOG.error("Failed to unregister active request interceptor", error_type=type(error).__name__)+async def enable_browser_download_monitor(self, browser: Browser, browser_context: BrowserContext) -> None:"""Monitor browser-initiated downloads and save them directly via HTTP.@@ -852,6 +886,7 @@ class CDPDownloadInterceptor:except BaseException:for task in snapshot:task.cancel()+ await asyncio.gather(*snapshot, return_exceptions=True)raiseasync def _handle_browser_download(self, event: dict[str, Any]) -> None:@@ -863,6 +898,8 @@ class CDPDownloadInterceptor:logged_url = urlif url.lower().startswith("data:"):logged_url = "data:<payload omitted>"+ elif url.lower().startswith(("http:", "https:")):+ logged_url = f"{urlparse(url).scheme}:<redacted>"LOG.info("Browser download detected",url=logged_url,@@ -879,7 +916,7 @@ class CDPDownloadInterceptor:returnif url in self._downloaded_urls:- LOG.debug("URL already captured via Fetch, skipping direct download", url=url)+ LOG.debug("URL already captured via Fetch, skipping direct download")returnif url.startswith("blob:"):@@ -896,7 +933,7 @@ class CDPDownloadInterceptor:if is_data_url:LOG.warning("Error handling data URL download event", error_type=type(exc).__name__)else:- LOG.warning("Error handling browser download event", exc_info=True)+ LOG.warning("Error handling browser download event", error_type=type(exc).__name__)async def _run_data_worker(self, function: Any, *args: Any) -> tuple[Any, bool]:worker = asyncio.create_task(asyncio.to_thread(function, *args))@@ -966,145 +1003,170 @@ class CDPDownloadInterceptor:return _download_identity(url), content_type, data@staticmethod- def _atomically_write_bytes(save_path: Path, data: bytes | bytearray) -> None:- temporary_path = save_path.with_name(f"{save_path.name}.{uuid.uuid4().hex}{BROWSER_DOWNLOADING_SUFFIX}")+ def _open_download_directory(directory: Path) -> int:+ required_flags = ("O_DIRECTORY", "O_NOFOLLOW")+ if any(not hasattr(os, flag) for flag in required_flags):+ raise OSError(errno.ENOTSUP, "confined download writes are unsupported")+ flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW+ flags |= getattr(os, "O_CLOEXEC", 0)+ return os.open(directory, flags)++ def _remember_download_directory(self, directory: Path) -> None:+ directory_fd = self._open_download_directory(directory)try:- with open(temporary_path, "wb") as file:- file.write(data)- os.replace(temporary_path, save_path)+ directory_stat = os.fstat(directory_fd)+ identity = (directory_stat.st_dev, directory_stat.st_ino)+ expected_identity = self._download_directory_identities.setdefault(directory, identity)+ if identity != expected_identity:+ raise OSError(errno.ESTALE, "download directory changed after binding")finally:- temporary_path.unlink(missing_ok=True)+ os.close(directory_fd)- async def _cookie_header_for_url(self, url: str) -> str:- """Build a Cookie header from the browser context's cookies applicable to ``url``.+ def _open_confined_temporary_file(self, save_path: Path) -> _ConfinedTemporaryFile:+ expected_identity = self._download_directory_identities.get(save_path.parent)+ if expected_identity is None:+ raise OSError(errno.ESTALE, "download directory is not bound")- The urllib fallback in ``_download_url_directly`` does not share the BrowserContext, so- without this it fetches unauthenticated and a session-gated endpoint answers with its- login page. Best-effort: returns "" if there is no context or cookies can't be read.- """- if self._browser_context is None:- return ""+ directory_fd = self._open_download_directory(save_path.parent)try:- cookies = await self._browser_context.cookies(url)- except Exception as e:- LOG.debug("Could not read browser cookies for download fallback", url=url, error=str(e))- return ""- parts: list[str] = []- for cookie in cookies:- name, value = cookie.get("name") or "", cookie.get("value") or ""- # Skip control chars (CR/LF/NUL/DEL) so a stored value can't inject into the header line.- if name and not _has_control_chars(name) and not _has_control_chars(value):- parts.append(f"{name}={value}")- return "; ".join(parts)+ directory_stat = os.fstat(directory_fd)+ if (directory_stat.st_dev, directory_stat.st_ino) != expected_identity:+ raise OSError(errno.ESTALE, "download directory changed after binding")++ temporary_name = f"{save_path.name}.{uuid.uuid4().hex}{BROWSER_DOWNLOADING_SUFFIX}"+ flags = os.O_RDWR | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW+ flags |= getattr(os, "O_CLOEXEC", 0)+ file_descriptor = os.open(temporary_name, flags, 0o600, dir_fd=directory_fd)+ try:+ handle = os.fdopen(file_descriptor, "w+b")+ except BaseException:+ os.close(file_descriptor)+ os.unlink(temporary_name, dir_fd=directory_fd)+ raise+ except BaseException:+ os.close(directory_fd)+ raise+ return _ConfinedTemporaryFile(+ directory_fd=directory_fd,+ name=temporary_name,+ handle=handle,+ )- async def _download_url_via_urllib(self, url: str) -> tuple[bytes, str]:- """Fetch a download over urllib, checking the destination before every hop.+ @staticmethod+ def _publish_confined_temporary_file(temporary_file: _ConfinedTemporaryFile, filename: str) -> None:+ temporary_file.handle.flush()+ source_stat = os.fstat(temporary_file.handle.fileno())+ if not stat.S_ISREG(source_stat.st_mode) or source_stat.st_nlink != 1:+ raise OSError(errno.EPERM, "download temporary file must be a regular single-link file")+ os.link(+ temporary_file.name,+ filename,+ src_dir_fd=temporary_file.directory_fd,+ dst_dir_fd=temporary_file.directory_fd,+ follow_symlinks=False,+ )- The session cookie rides a normal header and is dropped when a hop crosses origin, so it- reaches the original host across a same-host redirect but is never replayed to another one.- """- current_url = await asyncio.to_thread(validate_fetch_url, url)- headers = {"User-Agent": "Mozilla/5.0"}- cookie_header = await self._cookie_header_for_url(current_url)- if cookie_header:- headers["Cookie"] = cookie_header- ssl_ctx = ssl.create_default_context()-- for _ in range(MAX_SAFE_REDIRECTS + 1):- request = urllib.request.Request(current_url, headers=dict(headers))- status, response_headers, body = await asyncio.to_thread(_urlopen_single_hop, request, ssl_ctx)- if status not in SAFE_REDIRECT_STATUS_CODES:- return body, _header_value(response_headers, "content-type") or ""- location = _header_value(response_headers, "location")- if not location:- raise HttpException(status, current_url, "Redirected download provided no target")- next_url = await asyncio.to_thread(validate_redirect_url, current_url, location)- headers, _ = strip_cross_origin_redirect_credentials(headers, None, current_url, next_url)- current_url = next_url- raise HttpException(400, current_url, "Too many redirects while fetching a download")+ try:+ # The pre-link nlink check is not atomic with os.link, so re-read the source inode:+ # the temp name plus the name we just published are the only two links that may exist.+ # A third means someone aliased the inode inside that window.+ published_stat = os.fstat(temporary_file.handle.fileno())+ if published_stat.st_nlink != 2:+ raise OSError(errno.EPERM, "download temporary file gained an unexpected link during publication")++ destination_flags = os.O_RDONLY | os.O_NONBLOCK | os.O_NOFOLLOW+ destination_flags |= getattr(os, "O_CLOEXEC", 0)+ destination_fd = os.open(filename, destination_flags, dir_fd=temporary_file.directory_fd)+ with os.fdopen(destination_fd, "rb") as destination:+ destination_stat = os.fstat(destination.fileno())+ if not stat.S_ISREG(destination_stat.st_mode) or (+ destination_stat.st_dev,+ destination_stat.st_ino,+ ) != (source_stat.st_dev, source_stat.st_ino):+ raise OSError(errno.ESTALE, "download destination changed during publication")+ except BaseException:+ # Rolls back the link created above. os.link refuses collisions, so this name did not+ # exist before this call; unlinking it assumes the confined directory grants no other+ # writer the right to claim `filename` mid-publication.+ os.unlink(filename, dir_fd=temporary_file.directory_fd)+ raise++ @staticmethod+ def _cleanup_confined_temporary_file(temporary_file: _ConfinedTemporaryFile) -> None:+ try:+ temporary_file.handle.close()+ finally:+ try:+ os.unlink(temporary_file.name, dir_fd=temporary_file.directory_fd)+ finally:+ os.close(temporary_file.directory_fd)++ def _atomically_write_bytes(self, save_path: Path, data: bytes | bytearray) -> None:+ temporary_file = self._open_confined_temporary_file(save_path)+ try:+ temporary_file.handle.write(data)+ self._publish_confined_temporary_file(temporary_file, save_path.name)+ finally:+ self._cleanup_confined_temporary_file(temporary_file)async def _download_url_directly(self, url: str, suggested_filename: str) -> None:- """Download a URL directly via HTTP and save to the output directory.+ """Download through the validated, pinned, per-hop-authorized shared fetch seam.- Tries Playwright's APIRequestContext first (shares browser context cookies),- falls back to urllib for pre-signed URLs or when APIRequestContext fails.+ This seam is the only transport; there is deliberately no raw fallback, so a fetch that the+ guard rejects (or that fails for any other reason) drops the download rather than retrying+ it unmediated."""if not self._output_dir:- LOG.warning("No output_dir set, skipping direct download", url=url)+ LOG.warning("No output_dir set, skipping direct download")+ return+ if self._browser_context is None:+ LOG.error("Browser download context is unenrolled")returnt0 = time.monotonic()- data: bytes | None = None- method = ""- content_type = ""-- # Try Playwright's APIRequestContext which shares the BrowserContext's cookies.- # We use the BrowserContext (not a Page) so this survives individual page closes.- if self._browser_context:- try:- response = await fetch_download_through_request_context(self._browser_context.request, url)- if response.ok:- data = await response.body()- content_type = response.headers.get("content-type", "")- method = "playwright_api"- else:- LOG.debug(- "Playwright APIRequestContext returned non-OK status, trying urllib",- url=url,- status=response.status,- )- except DOWNLOAD_DESTINATION_ERRORS as e:- # A refused destination is a decision about this URL, so the urllib transport must- # not be tried as a second chance at the same host.- LOG.error("Download destination refused", url=url, reason=str(e))- return- except Exception as e:- LOG.debug("Playwright APIRequestContext download failed, trying urllib", url=url, error=str(e))-- # Fallback: direct HTTP via urllib (works for pre-signed URLs). This does not share the- # BrowserContext, so it must carry the session cookies itself — otherwise a session-gated- # endpoint answers an unauthenticated request with its login page (saved as a corrupt file).- if data is None:- try:- data, content_type = await self._download_url_via_urllib(url)- method = "urllib"- except DOWNLOAD_DESTINATION_ERRORS as e:- LOG.error("Download destination refused", url=url, reason=str(e))- return- except Exception as e:- LOG.error("Direct HTTP download failed", url=url, error=str(e), exc_info=True)- return-- if data is None:- LOG.error("Download produced no data", url=url)+ try:+ validated_filename = _validated_download_basename(suggested_filename)+ cookie_header = await self._cookie_header_for_url(url)+ headers = {"Cookie": cookie_header} if cookie_header else None+ response = await file_api.fetch_file_bytes( # type: ignore[attr-defined]+ url,+ max_size_mb=MAX_FILE_SIZE_BYTES // (1024 * 1024),+ headers=headers,+ filename=validated_filename or None,+ authorize_request_hop=self._redirect_hop_authorizer,+ )+ except Exception as exc:+ LOG.error("Guarded direct download failed", error_type=type(exc).__name__)return+ data = response.body+ content_type = response.content_type+ response_filename = response.filename+ if not isinstance(data, bytes) or not isinstance(content_type, str) or not isinstance(response_filename, str):+ LOG.error("Guarded direct download returned an invalid response")+ returnif len(data) > MAX_FILE_SIZE_BYTES:LOG.warning("Direct download exceeds size limit, discarding",- url=url,size=len(data),max_size=MAX_FILE_SIZE_BYTES,)return- normalized_filename = normalize_download_filename(suggested_filename, content_type)+ normalized_filename = normalize_download_filename(response_filename, content_type)if _payload_is_html_login_masquerade(data, content_type, normalized_filename):LOG.error("Direct download returned an HTML page for a non-HTML file; not saving ""(likely an unauthenticated fetch landing on a login/session-gate page)",- url=url,suggested_filename=normalized_filename,content_type=content_type,size=len(data),- method=method,)return- save_path, filename = self._resolve_save_path(suggested_filename, content_type)+ save_path, filename = self._resolve_save_path(response_filename, content_type)- with open(save_path, "wb") as f:- f.write(data)+ self._atomically_write_bytes(save_path, data)elapsed_ms = (time.monotonic() - t0) * 1000LOG.info(@@ -1114,9 +1176,30 @@ class CDPDownloadInterceptor:duration_ms=round(elapsed_ms, 1),save_path=str(save_path),download_index=self._download_index,- method=method,+ method="guarded_http",)+ async def _cookie_header_for_url(self, url: str) -> str:+ if self._browser_context is None:+ return ""+ try:+ cookies = await self._browser_context.cookies(url)+ except Exception:+ return ""+ parts: list[str] = []+ for cookie in cookies:+ name = cookie.get("name")+ value = cookie.get("value")+ if (+ isinstance(name, str)+ and name+ and isinstance(value, str)+ and not _FILENAME_CONTROL_CHAR_RE.search(name)+ and not _FILENAME_CONTROL_CHAR_RE.search(value)+ ):+ parts.append(f"{name}={value}")+ return "; ".join(parts)+async def _download_blob_url(self, url: str, suggested_filename: str) -> None:"""Save a blob: URL download by reading its bytes back from a same-origin page.@@ -1167,8 +1250,7 @@ class CDPDownloadInterceptor:)returnsave_path, filename = self._resolve_save_path(suggested_filename)- with open(save_path, "wb") as f:- f.write(data)+ self._atomically_write_bytes(save_path, data)self._downloaded_urls.add(url)LOG.info("CDP download saved (blob)",@@ -1189,18 +1271,33 @@ class CDPDownloadInterceptor:except Exception:passself._page_listener = None- await self._drain_tasks(self._page_enable_tasks)-self._accepting_cdp_handlers = False- await self._drain_tasks(self._cdp_handler_tasks)-- session_count = len(self._cdp_sessions)- for cdp_session in self._cdp_sessions:- try:- await cdp_session.send("Fetch.disable")- except Exception:- pass- self._cdp_sessions.clear()+ try:+ await self._drain_tasks(self._page_enable_tasks)+ await self._drain_tasks(self._cdp_handler_tasks)+ finally:+ sessions = tuple(self._cdp_sessions)+ for cdp_session in sessions:+ self._unregister_active_request_interceptor(cdp_session)+ for cdp_session in sessions:+ try:+ await cdp_session.send("Fetch.disable")+ except Exception as error:+ if _is_stale_interception_error(error):+ # Normal teardown usually finds the target already closed/detached+ # (SKY-11964); that benign race carries no live interception to leave+ # untracked, so it doesn't warrant invalidating the monitor.+ LOG.debug(+ "CDP Fetch interception was already stale at disable (benign race)",+ error_type=type(error).__name__,+ )+ continue+ # Interception may still be live on a session we have already unregistered,+ # which would leave requests egressing untracked. Mirror the enable path.+ self._network_egress_monitor.invalidate()+ LOG.warning("Failed to disable CDP Fetch interception", error_type=type(error).__name__)+ self._cdp_sessions.clear()+ session_count = len(sessions)async with self._browser_download_monitor_lock:self._accepting_browser_downloads = False@@ -1372,8 +1469,7 @@ class CDPDownloadInterceptor:"""Async handler for paused requests.Handles both Request-stage and Response-stage events:- - Request stage (no responseStatusCode): continue the request immediately.- We intercept at Request stage only to make Fetch.authRequired fire for proxy auth.+ - Request stage (no responseStatusCode): authorize before continuing, otherwise abort.- Response stage: check for downloads and intercept if needed."""request_id = event["requestId"]@@ -1381,10 +1477,27 @@ class CDPDownloadInterceptor:url = event.get("request", {}).get("url", "<unknown>")try:- # Request stage: no response yet (responseStatusCode absent). Continue the request- # so it proceeds to the server. We only intercept Request stage to enable- # Fetch.authRequired for proxy 407 challenges.+ # Request stage: authorize before continueRequest can put browser credentials on the+ # wire. A missing, failed, or negative collaborator is unenrolled and fails closed.if response_status is None:+ request = event.get("request", {})+ try:+ monitor = self._network_egress_monitor+ authorized = monitor is not None and monitor.authorize_request(+ method=request.get("method", ""),+ url=request.get("url", ""),+ resource_type=event.get("resourceType", "").lower(),+ frame=event.get("frameId"),+ )+ except Exception:+ authorized = False+ if not authorized:+ await self._fail_request(+ cdp_session,+ request_id,+ error_reason="BlockedByClient",+ )+ returnawait cdp_session.send("Fetch.continueRequest", {"requestId": request_id})return@@ -1395,7 +1508,6 @@ class CDPDownloadInterceptor:LOG.debug("CDP Fetch response paused",- url=url,resource_type=resource_type,status_code=response_status,content_type=response_headers.get("content-type", ""),@@ -1405,7 +1517,6 @@ class CDPDownloadInterceptor:if is_download_response(response_headers, response_status, resource_type):LOG.info("CDP download response detected",- url=url,resource_type=resource_type,status_code=response_status,content_type=response_headers.get("content-type", ""),@@ -1425,16 +1536,13 @@ class CDPDownloadInterceptor:LOG.debug("CDP interception went stale before response (benign race)",request_id=request_id,- url=url,- error=str(e),+ error_type=type(e).__name__,)returnLOG.error("Error handling CDP request",request_id=request_id,- url=url,- exc_info=True,- error=str(e),+ error_type=type(e).__name__,)# For Response-stage errors (e.g. download handling failed), try to let the# response through so the request doesn't hang indefinitely.@@ -1466,19 +1574,24 @@ class CDPDownloadInterceptor:unchanged. Large responses stream straight to a temp file and are fulfilled with an empty body,so the browser never materializes a large base64 payload (the OOM in SKY-12642)."""if not self._output_dir:- LOG.warning("CDP download intercepted but no output_dir set, passing through", url=url)+ LOG.warning("CDP download intercepted but no output_dir set, passing through")await self._continue_response(cdp_session, request_id)returncontent_length = _parse_content_length(headers)content_type = _normalized_content_type(headers.get("content-type", ""))raw_filename = extract_filename(headers, url)- save_path, filename = self._resolve_save_path(raw_filename, content_type)+ try:+ save_path, filename = self._resolve_save_path(raw_filename, content_type)+ except (OSError, ValueError) as error:+ LOG.error("Unsafe CDP download output path", error_type=type(error).__name__)+ self._downloaded_urls.add(url)+ await self._fail_request(cdp_session, request_id, url=url)+ returnLOG.info("CDP download detected",filename=filename,- url=url,content_type=content_type,content_length=content_length,)@@ -1524,7 +1637,6 @@ class CDPDownloadInterceptor:LOG.error("CDP download exceeds stream cap, aborting",filename=filename,- url=url,total_bytes=e.total_bytes,cap=MAX_STREAMED_FILE_SIZE_BYTES,)@@ -1538,8 +1650,7 @@ class CDPDownloadInterceptor:LOG.error("takeResponseBodyAsStream failed, failing request (no whole-body fallback)",filename=filename,- url=url,- error=str(e.__cause__ or e),+ error_type=type(e.__cause__ or e).__name__,)await self._fail_request(cdp_session, request_id, filename=filename, url=url)return@@ -1552,11 +1663,9 @@ class CDPDownloadInterceptor:LOG.error("Failed to extract CDP download",filename=filename,- url=url,content_type=content_type,content_length=content_length,- error=str(e),- exc_info=True,+ error_type=type(e).__name__,)await self._fail_request(cdp_session, request_id, filename=filename, url=url)return@@ -1643,9 +1752,13 @@ class CDPDownloadInterceptor:)except Exception as e:if _is_stale_interception_error(e):- LOG.debug("failRequest hit stale interception (benign race)", filename=filename, url=url, error=str(e))+ LOG.debug(+ "failRequest hit stale interception (benign race)",+ filename=filename,+ error_type=type(e).__name__,+ )else:- LOG.warning("failRequest failed", filename=filename, url=url, error=str(e))+ LOG.warning("failRequest failed", filename=filename, error_type=type(e).__name__)async def _finalize_download(self,@@ -1668,7 +1781,7 @@ class CDPDownloadInterceptor:# matching the data-URL path (_download_data_url)._, cancelled = await self._run_data_worker(self._atomically_write_bytes, save_path, data)except Exception as e:- LOG.error("Failed to save CDP download", filename=filename, url=url, error=str(e), exc_info=True)+ LOG.error("Failed to save CDP download", filename=filename, error_type=type(e).__name__)await self._fail_request(cdp_session, request_id, filename=filename, url=url)returnelapsed_ms = (time.monotonic() - t0) * 1000@@ -1709,17 +1822,15 @@ class CDPDownloadInterceptor:LOG.debug("fulfillRequest hit stale interception after download (benign race)",filename=filename,- url=url,- error=str(e),+ error_type=type(e).__name__,)else:- LOG.warning("fulfillRequest failed after download", filename=filename, url=url, error=str(e))+ LOG.warning("fulfillRequest failed after download", filename=filename, error_type=type(e).__name__)- def _open_stream_temp_file(self, save_path: Path) -> tuple[Path, IO[bytes]]:- """Open a same-directory ``.crdownload`` temp file for streaming (atomic os.replace on finalize;+ def _open_stream_temp_file(self, save_path: Path) -> _ConfinedTemporaryFile:+ """Open a descriptor-bound ``.crdownload`` temp file for streaming (hard-link publication;the suffix also makes the download waiter block until the stream completes)."""- temp_path = save_path.with_name(f"{save_path.name}.{uuid.uuid4().hex}{BROWSER_DOWNLOADING_SUFFIX}")- return temp_path, open(temp_path, "wb")+ return self._open_confined_temporary_file(save_path)@staticmethoddef _write_chunks(handle: IO[bytes], chunks: list[bytes]) -> None:@@ -1750,13 +1861,12 @@ class CDPDownloadInterceptor:raise _StreamStartError() from ebuffer: list[bytes] | None = None if start_on_disk else []- temp_path: Path | None = None- temp_file: IO[bytes] | None = None+ temp_file: _ConfinedTemporaryFile | None = Nonetotal = 0try:if start_on_disk:- temp_path, temp_file = self._open_stream_temp_file(save_path)+ temp_file = self._open_stream_temp_file(save_path)while True:# Bound each read so a stalled IO.read can't hold the extraction lock (and hang teardown's@@ -1786,39 +1896,33 @@ class CDPDownloadInterceptor:# single loop iteration (no interleaved IO.read await), so off-load it via# _run_data_worker to keep it off the event loop. The worker is shielded, so# on cancellation the write finishes before the sync finally removes the temp.- temp_path, temp_file = self._open_stream_temp_file(save_path)+ temp_file = self._open_stream_temp_file(save_path)spilled, buffer = buffer, None- _, cancelled = await self._run_data_worker(self._write_chunks, temp_file, spilled)+ _, cancelled = await self._run_data_worker(self._write_chunks, temp_file.handle, spilled)# Release the spilled chunks (up to the threshold) now they are on disk, so a# slow subsequent stream never retains a whole buffered copy in memory.del spilledif cancelled:raise asyncio.CancelledErrorelse:- temp_file.write(chunk)+ temp_file.handle.write(chunk)if eof:breakif temp_file is not None:- assert temp_path is not None # temp_file and temp_path are always opened together- temp_file.flush()- temp_file.close()- temp_file = None- os.replace(temp_path, save_path)+ self._publish_confined_temporary_file(temp_file, save_path.name)return _StreamOutcome(mode="streamed", data=None, save_path=save_path, total_bytes=total)return _StreamOutcome(mode="buffered", data=b"".join(buffer or []), save_path=None, total_bytes=total)finally:# Sync cleanup first (uninterruptible): close the temp handle and remove any unfinalized temp- # file, so no partial artifact survives an error, abort, or cancellation. The unlink is- # unconditional — after os.replace the temp name no longer exists, so it is a missing_ok no-op.+ # file, so no partial artifact survives an error, abort, or cancellation. Publication creates+ # the final hard link; the temporary name is always removed here.if temp_file is not None:try:- temp_file.close()+ self._cleanup_confined_temporary_file(temp_file)except Exception:pass- if temp_path is not None:- temp_path.unlink(missing_ok=True)try:# Bound the close too: if the session/target is dead (the same condition that stalls a# read), an unbounded IO.close would hold the extraction lock and hang teardown's drain.diff --git a/skyvern/webeye/cdp_frame_publisher.py b/skyvern/webeye/cdp_frame_publisher.pyindex dad3d293c..5be1f2c6f 100644--- a/skyvern/webeye/cdp_frame_publisher.py+++ b/skyvern/webeye/cdp_frame_publisher.py@@ -125,6 +125,17 @@ class CDPFramePublisher:"""Spawn the background publish loop. Idempotent."""if self.is_running:return+ # Written once (never removed) so display capture stays off this key even if this publisher+ # dies mid-run; a capture tick racing this first write may still publish one stray black frame.+ try:+ await asyncio.to_thread(_touch_remote_browser_sentinel, self._organization_id, self._stream_key)+ except OSError:+ LOG.warning(+ "Could not write the remote-browser sentinel; display capture may overwrite frames",+ stream_key=self._stream_key,+ organization_id=self._organization_id,+ exc_info=True,+ )self._stopped.clear()self._task = asyncio.create_task(self._run(), name=f"cdp-frame-publisher:{self._stream_key}")LOG.info(@@ -361,6 +372,20 @@ class CDPFramePublisher:pass+REMOTE_BROWSER_SENTINEL_SUFFIX = ".remote"+++def remote_browser_sentinel_path(organization_id: str, stream_key: str) -> Path:+ """Marks the stream key as fed by a remote browser, so display-capture workers skip it."""+ return Path(get_skyvern_temp_dir()) / organization_id / f"{stream_key}{REMOTE_BROWSER_SENTINEL_SUFFIX}"+++def _touch_remote_browser_sentinel(organization_id: str, stream_key: str) -> None:+ path = remote_browser_sentinel_path(organization_id, stream_key)+ path.parent.mkdir(parents=True, exist_ok=True)+ path.touch()++def stream_key_for_workflow_run(workflow_run_id: str) -> str:return f"{workflow_run_id}.png"diff --git a/tests/unit/browser_extension/test_mcp_glue.py b/tests/unit/browser_extension/test_mcp_glue.pyindex 7bee08b02..5a424d4a8 100644--- a/tests/unit/browser_extension/test_mcp_glue.py+++ b/tests/unit/browser_extension/test_mcp_glue.py@@ -22,10 +22,14 @@ from skyvern.cli.core import session_managerfrom skyvern.cli.core.result import BrowserContextfrom skyvern.cli.mcp_tools import session as mcp_session-_NOT_CONNECTED_GUIDANCE = (- "Skyvern browser extension is not connected. Install it: run `skyvern browser extension-path`, open "- "chrome://extensions, enable Developer mode, Load unpacked, and select that directory. Then run "- "`skyvern browser extension-token`, paste the token into the extension popup, and click Connect. Then retry."+_PAIRING_OPENED_GUIDANCE = (+ "Skyvern browser extension is not connected. A secure pairing tab was opened. Approve the connection, approve "+ "pairing in the Skyvern Agent confirmation tab, and retry."+)+_PAIRING_FALLBACK_GUIDANCE = (+ "Skyvern browser extension is not connected and the pairing tab could not be opened automatically. Run "+ "`skyvern browser extension-pair`, approve the connection, approve pairing in the Skyvern Agent confirmation "+ "tab, and retry.")@@ -34,6 +38,14 @@ def _use_stdio_transport(monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr(session_manager, "_stateless_http_mode", False)[email protected]("pairing_opened", [True, False])+def test_extension_not_connected_guidance_omits_manual_token_flow(pairing_opened: bool) -> None:+ guidance = mcp_session._extension_not_connected_guidance(pairing_opened=pairing_opened)++ assert "extension-token" not in guidance+ assert "paste the token" not in guidance++@pytest.mark.parametrize(("browser_type", "expected"),[@@ -118,10 +130,19 @@ async def test_session_create_extension_takes_precedence_over_cdp(monkeypatch: p@pytest.mark.asyncio+ ("pairing_opened", "expected_guidance"),+ [(True, _PAIRING_OPENED_GUIDANCE), (False, _PAIRING_FALLBACK_GUIDANCE)],+)async def test_session_create_extension_not_connected_returns_pinned_guidance(monkeypatch: pytest.MonkeyPatch,+ pairing_opened: bool,+ expected_guidance: str,) -> None:- runtime = SimpleNamespace(wait_for_extension=AsyncMock(return_value=False))+ runtime = SimpleNamespace(+ wait_for_extension=AsyncMock(return_value=False),+ open_pairing_page=MagicMock(return_value=pairing_opened),+ )get_or_start = AsyncMock(return_value=runtime)resolve_browser = AsyncMock()monkeypatch.setenv("BROWSER_TYPE", "extension-connect")@@ -131,8 +152,11 @@ async def test_session_create_extension_not_connected_returns_pinned_guidance(result = await mcp_session.skyvern_browser_session_create()assert result["ok"] is False- assert result["error"]["message"] == _NOT_CONNECTED_GUIDANCE+ assert result["error"]["message"] == expected_guidance+ assert "extension-token" not in result["error"]["message"]+ assert "paste the token" not in result["error"]["message"]get_or_start.assert_awaited_once_with()+ runtime.open_pairing_page.assert_called_once_with()runtime.wait_for_extension.assert_awaited_once_with(10.0)resolve_browser.assert_not_awaited()@@ -207,7 +231,7 @@ async def test_session_create_extension_connection_error_redacts_capability_url(session_manager._current_session.reset(current_token)assert result["ok"] is False- assert result["error"]["message"] == _NOT_CONNECTED_GUIDANCE+ assert result["error"]["message"] == _PAIRING_FALLBACK_GUIDANCEassert "/cdp/" not in repr(result)assert capability_token not in repr(result)connect_over_cdp.assert_awaited_once_with(capability_url)@@ -324,6 +348,20 @@ async def test_connect_to_browser_extension_uses_runtime_cdp_url_and_context_falbrowser.new_context.assert_awaited_once_with()+def test_pairing_confirmation_recovery_retries_through_mcp_without_cli_command() -> None:+ extension_dir = BrowserExtensionRuntime.extension_dir()+ confirmation_html = (extension_dir / "pairing_confirm.html").read_text()+ confirmation_js = (extension_dir / "pairing_confirm.js").read_text()++ assert "Retry the browser session request in your MCP client" in confirmation_html+ assert "skyvern browser extension-pair" in confirmation_html+ assert "Start a new pairing link" not in confirmation_html+ assert "copy-command" not in confirmation_html+ assert "expired before approval" in confirmation_js+ assert "skyvern browser extension-pair" not in confirmation_js+ assert "openPairingPage" not in confirmation_js++def test_browser_extension_path_command_prints_real_absolute_directory() -> None:result = CliRunner().invoke(browser_app, ["extension-path"])diff --git a/tests/unit/browser_extension/test_relay.py b/tests/unit/browser_extension/test_relay.pyindex d22d96265..547898b5d 100644--- a/tests/unit/browser_extension/test_relay.py+++ b/tests/unit/browser_extension/test_relay.py@@ -6,6 +6,8 @@ import hashlibimport hmacimport secretsfrom collections.abc import AsyncGenerator+from types import SimpleNamespace+from unittest.mock import AsyncMockimport pytestimport pytest_asyncio@@ -54,6 +56,15 @@ def pair_begin_proof(token: str) -> str:return hmac.new(token.encode(), b"skyvern-pair-begin-v1", hashlib.sha256).hexdigest()+def interactive_pairing_headers(harness: RelayHarness) -> dict[str, str]:+ return {+ "Content-Type": "application/json",+ "Origin": http_url(harness, ""),+ "Sec-Fetch-Mode": "cors",+ "Sec-Fetch-Site": "same-origin",+ }++async def authenticate(session: ClientSession,harness: RelayHarness,@@ -119,25 +130,104 @@ async def test_pair_begin_claim_happy_path_and_pair_page_never_contains_token(assert page.status == 200assert TOKEN not in page_bodyassert "dhommdmblflboaledbbfkdaapkadphlp" in page_body+ assert "Pairing request required" in page_body+ assert "skyvern browser extension-pair" in page_body+ assert "Start a new pairing link" not in page_body+ assert "copy-command" not in page_body+ assert "frame-ancestors 'none'" in page.headers["Content-Security-Policy"]claim = await session.post(http_url(relay_harness, "/pair/claim"),json={"v": 1, "nonce": begin_payload["nonce"]},+ headers=interactive_pairing_headers(relay_harness),)assert claim.status == 200assert claim.headers["Cache-Control"] == "no-store"assert await claim.json() == {"v": 1, "port": relay_harness.server.bound_port, "token": TOKEN}+async def test_bare_pair_page_requires_authenticated_pairing_request(+ relay_harness: RelayHarness,+) -> None:+ async with ClientSession() as session:+ page = await session.get(http_url(relay_harness, "/pair"))+ page_body = await page.text()++ assert page.status == 200+ assert relay_harness.server._pairing_nonce is None+ assert "showMissingRequest();" in page_body+ assert "approvalControls.hidden = true" in page_body++ begin = await session.post(+ http_url(relay_harness, "/pair/begin"),+ json={"v": 1},+ headers=interactive_pairing_headers(relay_harness),+ )+ assert begin.status == 403+ assert relay_harness.server._pairing_nonce is None+++def test_interactive_pairing_source_accepts_canonical_default_http_port() -> None:+ server = ExtensionRelayServer(TOKEN, 80, AsyncMock())+ request = SimpleNamespace(+ content_type="application/json",+ headers={+ "Origin": "http://127.0.0.1",+ "Sec-Fetch-Mode": "cors",+ "Sec-Fetch-Site": "same-origin",+ },+ )++ assert server._is_interactive_pairing_request(request)+++async def test_pair_claim_rejects_cross_site_request_without_consuming_nonce(+ relay_harness: RelayHarness,+) -> None:+ nonce = relay_harness.server.create_pairing_nonce()+ async with ClientSession() as session:+ rejected = await session.post(+ http_url(relay_harness, "/pair/claim"),+ json={"v": 1, "nonce": nonce},+ headers={"Origin": "https://evil.example", "Sec-Fetch-Mode": "cors", "Sec-Fetch-Site": "cross-site"},+ )+ accepted = await session.post(+ http_url(relay_harness, "/pair/claim"),+ json={"v": 1, "nonce": nonce},+ headers=interactive_pairing_headers(relay_harness),+ )++ assert rejected.status == 403+ assert accepted.status == 200+++def test_get_or_create_pairing_nonce_reuses_live_nonce_and_rotates_expired_nonce(+ relay_harness: RelayHarness,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ now = 10_000.0+ monkeypatch.setattr(relay_module.time, "monotonic", lambda: now)++ first = relay_harness.server.get_or_create_pairing_nonce()+ assert relay_harness.server.get_or_create_pairing_nonce() == first++ now += 121.0+ assert relay_harness.server.get_or_create_pairing_nonce() != first++@pytest.mark.asyncioasync def test_pair_claim_wrong_nonce_is_forbidden_and_consumes_active_nonce(relay_harness: RelayHarness,) -> None:nonce = relay_harness.server.create_pairing_nonce()+ headers = interactive_pairing_headers(relay_harness)async with ClientSession() as session:wrong = await session.post(http_url(relay_harness, "/pair/claim"),json={"v": 1, "nonce": secrets.token_urlsafe(32)},+ headers=headers,)assert wrong.status == 403assert await wrong.json() == {"error": "invalid_nonce"}@@ -145,6 +235,7 @@ async def test_pair_claim_wrong_nonce_is_forbidden_and_consumes_active_nonce(consumed = await session.post(http_url(relay_harness, "/pair/claim"),json={"v": 1, "nonce": nonce},+ headers=headers,)assert consumed.status == 403@@ -152,14 +243,17 @@ async def test_pair_claim_wrong_nonce_is_forbidden_and_consumes_active_nonce(@pytest.mark.asyncioasync def test_pair_claim_nonce_is_single_use(relay_harness: RelayHarness) -> None:nonce = relay_harness.server.create_pairing_nonce()+ headers = interactive_pairing_headers(relay_harness)async with ClientSession() as session:first = await session.post(http_url(relay_harness, "/pair/claim"),json={"v": 1, "nonce": nonce},+ headers=headers,)second = await session.post(http_url(relay_harness, "/pair/claim"),json={"v": 1, "nonce": nonce},+ headers=headers,)assert first.status == 200@@ -181,6 +275,7 @@ async def test_pair_claim_expired_nonce_is_forbidden(response = await session.post(http_url(relay_harness, "/pair/claim"),json={"v": 1, "nonce": nonce},+ headers=interactive_pairing_headers(relay_harness),)assert response.status == 403@@ -194,11 +289,13 @@ async def test_pair_claim_bad_payload_is_forbidden_and_consumes_active_nonce(payload: object,) -> None:nonce = relay_harness.server.create_pairing_nonce()+ headers = interactive_pairing_headers(relay_harness)async with ClientSession() as session:- response = await session.post(http_url(relay_harness, "/pair/claim"), json=payload)+ response = await session.post(http_url(relay_harness, "/pair/claim"), json=payload, headers=headers)consumed = await session.post(http_url(relay_harness, "/pair/claim"),json={"v": 1, "nonce": nonce},+ headers=headers,)assert response.status == 403diff --git a/tests/unit/browser_extension/test_runtime.py b/tests/unit/browser_extension/test_runtime.pyindex f5024e96a..9d7776531 100644--- a/tests/unit/browser_extension/test_runtime.py+++ b/tests/unit/browser_extension/test_runtime.py@@ -2,8 +2,10 @@ from __future__ import annotationsimport asyncioimport errno+import subprocessfrom collections.abc import AsyncGenerator, Awaitable, Callablefrom pathlib import Path+from unittest.mock import MagicMockimport pytestimport pytest_asyncio@@ -26,6 +28,7 @@ class StubRelay:) -> None:self.token = tokenself.port = port+ self.bound_port = portself.on_event = on_eventself.on_disconnect = on_disconnectself.calls = calls@@ -33,6 +36,9 @@ class StubRelay:self.connected = Trueself.stop_count = 0+ def get_or_create_pairing_nonce(self) -> str:+ return "runtime-pairing-nonce"+async def start(self) -> None:self.calls.append("relay.start")if self.start_error is not None:@@ -136,6 +142,70 @@ async def test_singleton_is_idempotent_and_late_binds_adapter_callbacks(monkeypaassert adapters[0].disconnect_count == 1+async def test_open_pairing_page_uses_relay_nonce_without_exposing_token(monkeypatch: pytest.MonkeyPatch) -> None:+ install_stubs(monkeypatch)+ opener = MagicMock(return_value=True)+ monkeypatch.setattr(BrowserExtensionRuntime, "open_extension_url", staticmethod(opener))+ runtime = await BrowserExtensionRuntime.get_or_start(21003)++ assert runtime.open_pairing_page()+ assert runtime.open_pairing_page()+ opener.assert_called_with("http://127.0.0.1:21003/pair#runtime-pairing-nonce")+ assert opener.call_count == 2+ assert "runtime-test-token" not in opener.call_args.args[0]+++def test_open_extension_url_targets_google_chrome_on_macos(monkeypatch: pytest.MonkeyPatch) -> None:+ run = MagicMock()+ monkeypatch.setattr(runtime_module.sys, "platform", "darwin")+ monkeypatch.setattr(runtime_module.shutil, "which", lambda name: "/usr/bin/open" if name == "open" else None)+ monkeypatch.setattr(runtime_module.subprocess, "run", run)++ assert BrowserExtensionRuntime.open_extension_url("http://127.0.0.1:19777/pair#nonce")+ run.assert_called_once_with(+ ["/usr/bin/open", "-a", "Google Chrome", "http://127.0.0.1:19777/pair#nonce"],+ check=True,+ stdout=subprocess.DEVNULL,+ stderr=subprocess.DEVNULL,+ )++[email protected]("platform", ["linux", "win32"])+def test_open_extension_url_launches_direct_browser_without_waiting(+ monkeypatch: pytest.MonkeyPatch,+ tmp_path: Path,+ platform: str,+) -> None:+ popen = MagicMock()+ monkeypatch.setattr(runtime_module.sys, "platform", platform)+ monkeypatch.setattr(runtime_module.subprocess, "Popen", popen)+ if platform == "linux":+ executable = Path("/usr/bin/chromium")+ monkeypatch.setattr(+ runtime_module.shutil, "which", lambda name: str(executable) if name == "chromium" else None+ )+ platform_options = {"start_new_session": True}+ else:+ executable = tmp_path / "Google" / "Chrome" / "Application" / "chrome.exe"+ executable.parent.mkdir(parents=True)+ executable.touch()+ monkeypatch.setenv("LOCALAPPDATA", str(tmp_path))+ monkeypatch.delenv("PROGRAMFILES", raising=False)+ monkeypatch.delenv("PROGRAMFILES(X86)", raising=False)+ platform_options = {"creationflags": 0x00000208}++ assert BrowserExtensionRuntime.open_extension_url("http://127.0.0.1:19777/pair#nonce")+ popen.assert_called_once_with(+ [str(executable), "http://127.0.0.1:19777/pair#nonce"],+ stdin=subprocess.DEVNULL,+ stdout=subprocess.DEVNULL,+ stderr=subprocess.DEVNULL,+ close_fds=True,+ **platform_options,+ )++@pytest.mark.asyncioasync def test_port_resolution_prefers_explicit_then_environment_then_default(monkeypatch: pytest.MonkeyPatch) -> None:relays, _, _ = install_stubs(monkeypatch)diff --git a/tests/unit/forge/sdk/routes/streaming/test_cdp_input_session.py b/tests/unit/forge/sdk/routes/streaming/test_cdp_input_session.pyindex df6e5f742..9ecad801d 100644--- a/tests/unit/forge/sdk/routes/streaming/test_cdp_input_session.py+++ b/tests/unit/forge/sdk/routes/streaming/test_cdp_input_session.py@@ -2,9 +2,12 @@treats None as "no active page" and silently skips the event while the channel stays open, so theuser keeps interacting with a surface that no longer receives input."""+import json+from types import SimpleNamespacefrom typing import Anyimport pytest+from fastapi import WebSocketDisconnectfrom skyvern.forge.sdk.routes.streaming import cdp_input@@ -13,10 +16,14 @@ class _FakeSession:def __init__(self, name: str) -> None:self.name = nameself.detached = False+ self.sent: list[tuple[str, dict]] = []async def detach(self) -> None:self.detached = True+ async def send(self, method: str, params: dict) -> None:+ self.sent.append((method, params))+class _FakeContext:def __init__(self, session: _FakeSession) -> None:@@ -80,3 +87,162 @@ async def test_rebinds_when_the_working_page_changes(monkeypatch: pytest.MonkeyPassert await input_session.get_session(force_refresh=True) is secondassert first.detached is True+++class _FakeNavigablePage:+ """Stands in for the Playwright page `_dispatch_navigate_event` calls `goto()` on."""++ def __init__(self, response: object = None) -> None:+ self.goto_calls: list[str] = []+ self._response = response++ async def goto(self, url: str) -> object:+ self.goto_calls.append(url)+ return self._response+++class _FakeInputSession:+ def __init__(self, cdp_session: _FakeSession, page: object = None) -> None:+ self._cdp_session = cdp_session+ self.page = page if page is not None else _FakeNavigablePage()++ async def get_session(self, *, force_refresh: bool = False) -> _FakeSession:+ return self._cdp_session+++class _FakeWebSocket:+ """`_run_input_loop` only touches `receive_text`, `send_json`, and `close`."""++ def __init__(self, messages: list[str]) -> None:+ self._messages = list(messages)+ self.sent_json: list[dict] = []+ self.closed: tuple[int, str] | None = None++ async def receive_text(self) -> str:+ if not self._messages:+ raise WebSocketDisconnect()+ return self._messages.pop(0)++ async def send_json(self, data: dict) -> None:+ self.sent_json.append(data)++ async def close(self, code: int = 1000, reason: str = "") -> None:+ self.closed = (code, reason)+++class TestNavigateEvent:+ """SKY-13683: a live-view URL input navigates the remote page over the existing+ cdp_input WebSocket, gated by the same take-control check as mouse/keyboard input,+ and validated through the same SSRF guard every real page navigation goes through."""++ @pytest.mark.asyncio+ async def test_dropped_when_not_in_control(self) -> None:+ """The interactor gate lives server-side (cdp_input._run_input_loop), not just in+ the frontend hiding the input box -- anyone dialing the websocket directly without+ having taken control must not be able to redirect the page."""+ page = _FakeNavigablePage()+ input_session = _FakeInputSession(_FakeSession("s"), page=page)+ channel = SimpleNamespace(interactor="agent", client_id="c1")+ websocket = _FakeWebSocket([json.dumps({"type": "navigateEvent", "url": "https://example.com"})])++ await cdp_input._run_input_loop(websocket, channel, input_session, "browser_session_id", "pbs_test")++ assert page.goto_calls == []+ assert websocket.sent_json == []++ @pytest.mark.asyncio+ async def test_dispatches_page_navigate_after_take_control(self) -> None:+ page = _FakeNavigablePage()+ input_session = _FakeInputSession(_FakeSession("s"), page=page)+ channel = SimpleNamespace(interactor="agent", client_id="c1")+ websocket = _FakeWebSocket(+ [+ json.dumps({"kind": "take-control"}),+ json.dumps({"type": "navigateEvent", "url": "https://example.com/path"}),+ ]+ )++ await cdp_input._run_input_loop(websocket, channel, input_session, "browser_session_id", "pbs_test")++ assert page.goto_calls == ["https://example.com/path"]+ assert websocket.sent_json == []++ @pytest.mark.asyncio+ async def test_rejects_blocked_destination_via_the_real_ssrf_guard(self) -> None:+ """Uses the real validate_navigation_destination (no monkeypatch) against a known+ cloud-metadata IP: if the guard call is ever deleted or bypassed, page.goto WOULD+ get dispatched here and this assertion goes red -- that is the point of the test."""+ page = _FakeNavigablePage()+ input_session = _FakeInputSession(_FakeSession("s"), page=page)+ channel = SimpleNamespace(interactor="user", client_id="c1")+ websocket = _FakeWebSocket(+ [json.dumps({"type": "navigateEvent", "url": "http://169.254.169.254/latest/meta-data/"})]+ )++ await cdp_input._run_input_loop(websocket, channel, input_session, "browser_session_id", "pbs_test")++ assert page.goto_calls == []+ assert websocket.sent_json == [{"kind": "navigate-error", "reason": "blocked"}]+ assert websocket.closed is None++ @pytest.mark.asyncio+ async def test_rejects_empty_url_without_dispatching(self) -> None:+ page = _FakeNavigablePage()+ input_session = _FakeInputSession(_FakeSession("s"), page=page)+ channel = SimpleNamespace(interactor="user", client_id="c1")+ websocket = _FakeWebSocket([json.dumps({"type": "navigateEvent", "url": " "})])++ await cdp_input._run_input_loop(websocket, channel, input_session, "browser_session_id", "pbs_test")++ assert page.goto_calls == []+ assert websocket.sent_json == [{"kind": "navigate-error", "reason": "invalid_url"}]++ @pytest.mark.asyncio+ async def test_allows_a_public_destination_through_the_real_guard(self) -> None:+ page = _FakeNavigablePage()+ input_session = _FakeInputSession(_FakeSession("s"), page=page)+ channel = SimpleNamespace(interactor="user", client_id="c1")+ websocket = _FakeWebSocket([json.dumps({"type": "navigateEvent", "url": "https://example.org/"})])++ await cdp_input._run_input_loop(websocket, channel, input_session, "browser_session_id", "pbs_test")++ assert page.goto_calls == ["https://example.org/"]+ assert websocket.sent_json == []++ @pytest.mark.asyncio+ async def test_normalizes_a_bare_host_before_dispatch(self) -> None:+ """A schemeless entry like `example.org` passes validation because a scheme is+ prepended for the check; the browser must be sent that same normalized value, not+ the raw user text, or a scheme-less string reaching page.goto behaves differently+ (and any dispatch failure would close the whole channel instead of erroring inline)."""+ page = _FakeNavigablePage()+ input_session = _FakeInputSession(_FakeSession("s"), page=page)+ channel = SimpleNamespace(interactor="user", client_id="c1")+ websocket = _FakeWebSocket([json.dumps({"type": "navigateEvent", "url": "example.org"})])++ await cdp_input._run_input_loop(websocket, channel, input_session, "browser_session_id", "pbs_test")++ assert page.goto_calls == ["https://example.org"]+ assert websocket.sent_json == []++ @pytest.mark.asyncio+ async def test_navigate_blocked_via_redirect_chain_resets_the_page(self) -> None:+ """page.goto follows redirects at the network layer, so a destination that itself+ passes validate_navigation_destination can still land on a blocked host after a+ redirect. Uses the real revalidate_redirect_chain/validate_navigation_destination+ (no monkeypatch): remove the revalidation call and this test's second assertion on+ goto_calls goes red, since the page would be left sitting on the blocked content."""+ final_request = SimpleNamespace(+ url="http://169.254.169.254/latest/meta-data/",+ redirected_from=SimpleNamespace(url="https://example.org/redirect", redirected_from=None),+ )+ response = SimpleNamespace(request=final_request)+ page = _FakeNavigablePage(response=response)+ input_session = _FakeInputSession(_FakeSession("s"), page=page)+ channel = SimpleNamespace(interactor="user", client_id="c1")+ websocket = _FakeWebSocket([json.dumps({"type": "navigateEvent", "url": "https://example.org/redirect"})])++ await cdp_input._run_input_loop(websocket, channel, input_session, "browser_session_id", "pbs_test")++ assert page.goto_calls == ["https://example.org/redirect", "about:blank"]+ assert websocket.sent_json == [{"kind": "navigate-error", "reason": "blocked"}]diff --git a/tests/unit/google/test_google_oauth.py b/tests/unit/google/test_google_oauth.pyindex 65a049a5a..1310a96c4 100644--- a/tests/unit/google/test_google_oauth.py+++ b/tests/unit/google/test_google_oauth.py@@ -63,11 +63,14 @@ def test_coerce_scopes_accepts_strings_and_iterables() -> None:assert google_oauth_service._coerce_scopes("") == _default_scopes_list()-def test_google_sheets_scopes_includes_drive_file_and_metadata_readonly() -> None:- scopes = google_oauth_service.GOOGLE_SHEETS_SCOPES- assert "https://www.googleapis.com/auth/spreadsheets" in scopes- assert "https://www.googleapis.com/auth/drive.file" in scopes- assert "https://www.googleapis.com/auth/drive.metadata.readonly" in scopes+def test_google_oauth_scope_profiles_are_exact() -> None:+ assert google_oauth_service.GOOGLE_SHEETS_SCOPES == (+ "https://www.googleapis.com/auth/spreadsheets",+ "https://www.googleapis.com/auth/drive.file",+ "https://www.googleapis.com/auth/drive.metadata.readonly",+ )+ assert google_oauth_service.GOOGLE_GMAIL_SCOPES == ("https://www.googleapis.com/auth/gmail.readonly",)+ assert google_oauth_service.GOOGLE_DRIVE_SCOPES == ("https://www.googleapis.com/auth/drive",)def test_google_drive_scope_profile_uses_full_drive_scope_for_folder_uploads() -> None:@@ -2087,6 +2090,7 @@ def test_build_authorize_url_includes_required_params(monkeypatch: pytest.Monkeyassert params["scope"] == " ".join(google_oauth_service.GOOGLE_SHEETS_SCOPES)assert params["access_type"] == "offline"assert params["prompt"] == "consent"+ assert "include_granted_scopes" not in paramsassert params["state"] == "abc123"# PKCE: a code_challenge must be on the URL and the verifier returned for replay.assert params["code_challenge_method"] == "S256"@@ -2094,6 +2098,27 @@ def test_build_authorize_url_includes_required_params(monkeypatch: pytest.Monkeyassert code_verifier and len(code_verifier) >= 43[email protected]("profile", ["google_sheets", "gmail", "google_drive"])+def test_build_authorize_url_uses_exact_scope_profile(+ monkeypatch: pytest.MonkeyPatch,+ profile: str,+) -> None:+ monkeypatch.setattr(google_oauth_service.settings, "GOOGLE_OAUTH_CLIENT_ID", "cid", raising=False)+ monkeypatch.setattr(google_oauth_service.settings, "GOOGLE_OAUTH_CLIENT_SECRET", "csecret", raising=False)+ monkeypatch.setattr(google_oauth_service.settings, "GOOGLE_OAUTH_REDIRECT_HOSTS", ["app"], raising=False)+ scopes = google_oauth_service.scopes_for_profile(profile)++ url, _ = google_oauth_service.build_authorize_url(+ redirect_uri="https://app/settings/google/callback",+ state="abc123",+ scopes=scopes,+ )++ params = {k: v[0] for k, v in parse_qs(urlparse(url).query).items()}+ assert params["scope"] == " ".join(scopes)+ assert "include_granted_scopes" not in params++def test_build_authorize_url_passes_autogenerate_code_verifier_explicitly(monkeypatch: pytest.MonkeyPatch) -> None:"""Explicit ``autogenerate_code_verifier=True`` so a library default flip can't silently drop PKCE."""monkeypatch.setattr(google_oauth_service.settings, "GOOGLE_OAUTH_CLIENT_ID", "cid", raising=False)diff --git a/tests/unit/test_adopted_session_download_save.py b/tests/unit/test_adopted_session_download_save.pyindex 6780da745..6bd41f498 100644--- a/tests/unit/test_adopted_session_download_save.py+++ b/tests/unit/test_adopted_session_download_save.py@@ -3,15 +3,17 @@On an adopted session the run connection owns the download artifact, but in prod theworker pod can tear the shared browser down before a deferred save_as runs. The helpersaves eagerly and, when save_as raises (TargetClosedError) or yields a 0-byte file,-re-fetches the replayable download url through the run page's request context.+re-fetches the replayable download URL through the guarded file-fetch contract.-For ``blob:`` URLs (client-side blobs minted by the page) the request-context fetch-cannot be used; the helper runs an in-page ``fetch`` from a frame whose origin owns-the blob and returns the bytes that way.+For ``blob:`` URLs (client-side blobs minted by the page) guarded HTTP fetch cannot be+used; the helper runs an in-page ``fetch`` from a frame whose origin owns the blob and+returns the bytes that way."""import asyncioimport base64+from collections.abc import Awaitable, Callable+from functools import partialfrom pathlib import Pathfrom unittest.mock import AsyncMock, MagicMock, patch@@ -20,13 +22,35 @@ import pytestfrom skyvern.webeye.actions.handler import (_EagerAdoptedBlobCapture,_read_adopted_session_blob_bytes,- _save_adopted_session_download,)+from skyvern.webeye.actions.handler import _save_adopted_session_download as _save_adopted_session_download_implfrom skyvern.webeye.browser_artifacts import DownloadBindingPDF_BODY = b"%PDF-1.4\n" + b"x" * 830+async def _authorize_request_hop(+ _authorization: object,+ dispatch: Callable[[tuple[str, ...]], Awaitable[object]],+) -> object:+ return await dispatch(())+++# Most tests below exercise save_as/blob recovery, never the guarded refetch itself, so they don't+# care about the authorizer or headers — supply the permissive defaults here and let the handful of+# tests that DO exercise the refetch pass their own.+_save_adopted_session_download = partial(+ _save_adopted_session_download_impl,+ authorize_request_hop=_authorize_request_hop,+ request_headers={},+)+++def _guarded_fetch(*, body: bytes = PDF_BODY, side_effect: Exception | None = None):+ fetch_file_bytes = AsyncMock(return_value=MagicMock(body=body), side_effect=side_effect)+ return patch("skyvern.webeye.actions.handler.fetch_file_bytes", new=fetch_file_bytes)++def _download(suggested: str = "153743777.pdf", url: str = "https://example.com/download") -> MagicMock:download = MagicMock()download.suggested_filename = suggested@@ -39,6 +63,9 @@ def _download(suggested: str = "153743777.pdf", url: str = "https://example.com/def _page_with_refetch(status: int = 200, body: bytes = PDF_BODY) -> MagicMock:+ # ``page.context.request.get`` is vestigial: the refetch path no longer calls the raw+ # Playwright APIRequestContext, but keeping the mock harmless lets ``assert_not_awaited``+ # assertions elsewhere in this file keep proving the raw path is never touched.response = MagicMock()response.status = statusresponse.body = AsyncMock(return_value=body)@@ -124,11 +151,13 @@ async def test_save_as_raises_target_closed_falls_back_to_refetch(tmp_path) -> Ndownload.save_as.side_effect = Exception("Target page, context or browser has been closed")page = _page_with_refetch()- saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")+ with _guarded_fetch() as fetch_file_bytes:+ saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")assert saved is not None and saved.exists()assert saved.read_bytes() == PDF_BODY- page.context.request.get.assert_awaited_once_with(download.url, max_redirects=0)+ fetch_file_bytes.assert_awaited_once_with(download.url, headers={}, authorize_request_hop=_authorize_request_hop)+ page.context.request.get.assert_not_awaited()@pytest.mark.asyncio@@ -141,11 +170,12 @@ async def test_zero_byte_save_as_falls_back_to_refetch(tmp_path) -> None:download.save_as.side_effect = _save_emptypage = _page_with_refetch()- saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")+ with _guarded_fetch() as fetch_file_bytes:+ saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")assert saved is not None and saved.exists()assert saved.read_bytes() == PDF_BODY- page.context.request.get.assert_awaited_once_with(download.url, max_redirects=0)+ fetch_file_bytes.assert_awaited_once_with(download.url, headers={}, authorize_request_hop=_authorize_request_hop)# the empty placeholder must not survive alongside the recovered fileassert sorted(p.name for p in tmp_path.iterdir()) == [saved.name]@@ -154,9 +184,10 @@ async def test_zero_byte_save_as_falls_back_to_refetch(tmp_path) -> None:async def test_refetch_non_200_returns_none(tmp_path) -> None:download = _download()download.save_as.side_effect = Exception("closed")- page = _page_with_refetch(status=403, body=b"forbidden")+ page = _page_with_refetch()- saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")+ with _guarded_fetch(side_effect=Exception("403 forbidden")):+ saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")assert saved is Noneassert list(tmp_path.iterdir()) == []@@ -166,9 +197,10 @@ async def test_refetch_non_200_returns_none(tmp_path) -> None:async def test_refetch_empty_body_returns_none(tmp_path) -> None:download = _download()download.save_as.side_effect = Exception("closed")- page = _page_with_refetch(status=200, body=b"")+ page = _page_with_refetch()- saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")+ with _guarded_fetch(body=b""):+ saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")assert saved is Noneassert list(tmp_path.iterdir()) == []@@ -179,9 +211,9 @@ async def test_refetch_raises_returns_none(tmp_path) -> None:download = _download()download.save_as.side_effect = Exception("closed")page = MagicMock()- page.context.request.get = AsyncMock(side_effect=Exception("connection gone"))- saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")+ with _guarded_fetch(side_effect=Exception("connection gone")):+ saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")assert saved is Noneassert list(tmp_path.iterdir()) == []@@ -199,13 +231,13 @@ async def test_partial_save_as_then_failed_refetch_leaves_no_orphan(tmp_path) ->download.save_as.side_effect = _save_partial_then_raisepage = MagicMock()- page.context.request.get = AsyncMock(side_effect=Exception("connection gone"))page.frames = []page.main_frame = MagicMock()page.main_frame.url = "https://example.com/"page.main_frame.evaluate = AsyncMock()- saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")+ with _guarded_fetch(side_effect=Exception("connection gone")):+ saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")assert saved is Noneassert list(tmp_path.iterdir()) == []@@ -843,11 +875,10 @@ def _resolvable_example_host(monkeypatch: pytest.MonkeyPatch) -> None:@pytest.mark.asyncio-async def test_refetch_refuses_internal_destination(tmp_path, download_destinations, fake_api_request_context) -> None:+async def test_refetch_refuses_internal_destination(tmp_path, download_destinations) -> None:download = _download(url=f"{download_destinations.internal_base}/internal")download.save_as.side_effect = Exception("Target page, context or browser has been closed")page = _page_with_refetch()- page.context.request = fake_api_request_context()saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")@@ -857,13 +888,10 @@ async def test_refetch_refuses_internal_destination(tmp_path, download_destinati@pytest.mark.asyncio-async def test_refetch_refuses_redirect_hop_to_internal_destination(- tmp_path, download_destinations, fake_api_request_context-) -> None:+async def test_refetch_refuses_redirect_hop_to_internal_destination(tmp_path, download_destinations) -> None:download = _download(url=f"{download_destinations.public_base}/redirect-to-internal")download.save_as.side_effect = Exception("Target page, context or browser has been closed")page = _page_with_refetch()- page.context.request = fake_api_request_context()saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")@@ -873,14 +901,49 @@ async def test_refetch_refuses_redirect_hop_to_internal_destination(@pytest.mark.asyncio-async def test_refetch_allows_permitted_destination(tmp_path, download_destinations, fake_api_request_context) -> None:+async def test_refetch_allows_permitted_destination(tmp_path, download_destinations) -> None:# Non-vacuity: a permitted destination must still round-trip through the re-fetch path.download = _download(url=f"{download_destinations.public_base}/attachment")download.save_as.side_effect = Exception("Target page, context or browser has been closed")page = _page_with_refetch()- page.context.request = fake_api_request_context()saved = await _save_adopted_session_download(download, page, tmp_path, workflow_run_id="wr")assert saved is not None and saved.exists()assert saved.read_bytes() == download_destinations.PUBLIC_BODY+++def test_construction_without_collaborators_fails_closed(tmp_path) -> None:+ """The production interceptor now requires an explicit network monitor and redirect-hop+ authorizer; a bare construction must never silently substitute a default, which is how a+ missing collaborator previously reached production undetected."""+ from skyvern.webeye.cdp_download_interceptor import CDPDownloadInterceptor++ with pytest.raises(TypeError):+ CDPDownloadInterceptor(output_dir=str(tmp_path)) # type: ignore[call-arg]+++async def test_adopted_session_download_binding_yields_owning_interceptors_authorizer(tmp_path) -> None:+ """The adopted-session refetch path must forward the exact authorizer bound on the page's+ owning interceptor, not a default of its own."""+ from skyvern.forge.sdk.browser_network_egress_monitor import BrowserNetworkEgressMonitor+ from skyvern.webeye.actions.handler import _adopted_session_download_binding+ from skyvern.webeye.cdp_download_interceptor import CDPDownloadInterceptor++ interceptor = CDPDownloadInterceptor(+ output_dir=str(tmp_path),+ network_egress_monitor=BrowserNetworkEgressMonitor.unenrolled(),+ redirect_hop_authorizer=_authorize_request_hop,+ )++ download = _download()+ page = _page_with_refetch()+ download.page = page+ interceptor._page_context = page.context+ page.context._skyvern_cdp_download_interceptor = interceptor+ page.context._skyvern_cdp_download_interceptor_bind_lock = asyncio.Lock()++ async with _adopted_session_download_binding(download, page) as (bound_interceptor, authorize_request_hop):+ assert bound_interceptor is interceptor+ assert authorize_request_hop is _authorize_request_hopdiff --git a/tests/unit/test_carry_subgoals_planner_prompt.py b/tests/unit/test_carry_subgoals_planner_prompt.pyindex 46467f290..7121b039c 100644--- a/tests/unit/test_carry_subgoals_planner_prompt.py+++ b/tests/unit/test_carry_subgoals_planner_prompt.py@@ -1,11 +1,40 @@+import pytest+from skyvern.config import Settingsfrom skyvern.forge.prompts import prompt_engine+from tests.unit.test_task_v2_completion_gate import _run_task_v2_wiring_casedef test_carry_subgoals_ships_off() -> None:assert Settings.model_fields["TASK_V2_CARRY_SUBGOALS"].default is False[email protected]("lever_enabled", [True, False])+async def test_required_subgoals_are_forwarded_to_the_next_planner_iteration(+ lever_enabled: bool,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ required_subgoals = [{"subgoal": "submit the form", "satisfied": False}]+ planner_inputs, _ = await _run_task_v2_wiring_case(+ monkeypatch,+ [0, 1],+ shared_flag_enabled=lever_enabled,+ planner_response={+ "user_goal_achieved": False,+ "should_terminate": False,+ "plan": "continue",+ "task_type": "navigate",+ "required_subgoals": required_subgoals,+ },+ )++ assert [inputs["prior_required_subgoals"] for inputs in planner_inputs] == [+ None,+ required_subgoals if lever_enabled else None,+ ]++def _render(prior_required_subgoals: list | None) -> str:return prompt_engine.load_prompt("task_v2",diff --git a/tests/unit/test_cdp_download_interceptor.py b/tests/unit/test_cdp_download_interceptor.pyindex dd87dd611..41c42544e 100644--- a/tests/unit/test_cdp_download_interceptor.py+++ b/tests/unit/test_cdp_download_interceptor.py@@ -25,6 +25,42 @@ from skyvern.webeye.cdp_download_interceptor import ()+def _make_interceptor(+ *args: Any,+ network_egress_monitor: Any | None = None,+ redirect_hop_authorizer: Any | None = None,+ **kwargs: Any,+) -> CDPDownloadInterceptor:+ if network_egress_monitor is None:+ network_egress_monitor = MagicMock()+ network_egress_monitor.authorize_request.return_value = True+ if redirect_hop_authorizer is None:+ redirect_hop_authorizer = AsyncMock(side_effect=AssertionError("unexpected direct HTTP request"))+ return mod.CDPDownloadInterceptor(+ *args,+ network_egress_monitor=network_egress_monitor,+ redirect_hop_authorizer=redirect_hop_authorizer,+ **kwargs,+ )+++ ("network_egress_monitor", "redirect_hop_authorizer"),+ [+ pytest.param(None, AsyncMock(), id="missing-monitor"),+ pytest.param(MagicMock(), None, id="missing-authorizer"),+ ],+)+def test_constructor_rejects_missing_required_collaborator(+ network_egress_monitor: Any, redirect_hop_authorizer: Any+) -> None:+ with pytest.raises(TypeError, match="required collaborators"):+ CDPDownloadInterceptor(+ network_egress_monitor=network_egress_monitor,+ redirect_hop_authorizer=redirect_hop_authorizer,+ )++class TestIsDownloadResponse:"""Tests for is_download_response()."""@@ -378,7 +414,7 @@ class TestResolveSavePath:"""Tests for CDPDownloadInterceptor._resolve_save_path()."""def _make_interceptor(self, tmp_path: Path) -> CDPDownloadInterceptor:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))return interceptor@pytest.mark.parametrize(@@ -425,19 +461,31 @@ class TestResolveSavePath:assert filename.endswith(".pdf")assert save_path == tmp_path / filename- def test_path_traversal_sanitized(self, tmp_path: Path) -> None:- """Path traversal components should be stripped — only the final name is kept."""+ @pytest.mark.parametrize(+ "filename",+ [+ pytest.param("../../etc/cron.d/evil", id="traversal"),+ pytest.param("../evil", id="parent"),+ pytest.param("nested/report.pdf", id="posix-separator"),+ pytest.param(r"nested\report.pdf", id="windows-separator"),+ pytest.param("/tmp/report.pdf", id="absolute-posix"),+ pytest.param(r"C:\temp\report.pdf", id="absolute-windows"),+ pytest.param("%2Ftmp%2Freport.pdf", id="encoded-posix-separator"),+ pytest.param(r"%5Cserver%5Cshare%5Creport.pdf", id="encoded-windows-separator"),+ pytest.param("%252Ftmp%252Freport.pdf", id="double-encoded-posix-separator"),+ pytest.param(r"%255Cserver%255Cshare%255Creport.pdf", id="double-encoded-windows-separator"),+ pytest.param("%252e%252e%252freport.pdf", id="double-encoded-traversal"),+ pytest.param(".", id="current-directory"),+ pytest.param("..", id="parent-directory"),+ pytest.param("report\x00.pdf", id="nul"),+ pytest.param("report\n.pdf", id="control-character"),+ ],+ )+ def test_non_basename_filename_fails_closed(self, tmp_path: Path, filename: str) -> None:interceptor = self._make_interceptor(tmp_path)- save_path, filename = interceptor._resolve_save_path("../../etc/cron.d/evil")- assert filename == "evil"- assert save_path == tmp_path / "evil"- def test_header_date_separators_are_filename_chars(self, tmp_path: Path) -> None:- """Invoice-style slashes in Content-Disposition should not collapse to the last date segment."""- interceptor = self._make_interceptor(tmp_path)- save_path, filename = interceptor._resolve_save_path("invoice_5/19/2026", "application/pdf")- assert filename == "invoice_5_19_2026.pdf"- assert save_path == tmp_path / "invoice_5_19_2026.pdf"+ with pytest.raises(ValueError, match="basename"):+ interceptor._resolve_save_path(filename)def test_missing_extension_uses_pdf_content_type(self, tmp_path: Path) -> None:interceptor = self._make_interceptor(tmp_path)@@ -447,9 +495,138 @@ class TestResolveSavePath:def test_existing_pdf_extension_not_duplicated(self, tmp_path: Path) -> None:interceptor = self._make_interceptor(tmp_path)- save_path, filename = interceptor._resolve_save_path("invoice_5/19/2026.pdf", "application/pdf")- assert filename == "invoice_5_19_2026.pdf"- assert save_path == tmp_path / "invoice_5_19_2026.pdf"+ save_path, filename = interceptor._resolve_save_path("invoice_2026.pdf", "application/pdf")+ assert filename == "invoice_2026.pdf"+ assert save_path == tmp_path / "invoice_2026.pdf"+++class TestConfinedDownloadWrites:+ def test_symlinked_output_directory_fails_closed(self, tmp_path: Path) -> None:+ outside = tmp_path / "outside"+ outside.mkdir()+ output_dir = tmp_path / "downloads"+ output_dir.symlink_to(outside, target_is_directory=True)+ interceptor = _make_interceptor(output_dir=str(output_dir))++ with pytest.raises(OSError):+ save_path, _ = interceptor._resolve_save_path("report.pdf")+ interceptor._atomically_write_bytes(save_path, b"private report")++ assert not (outside / "report.pdf").exists()++ def test_symlink_destination_fails_closed(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ save_path, _ = interceptor._resolve_save_path("report.pdf")+ outside = tmp_path.parent / f"{tmp_path.name}-outside.txt"+ outside.write_bytes(b"outside")+ save_path.symlink_to(outside)++ with pytest.raises(OSError):+ interceptor._atomically_write_bytes(save_path, b"private report")++ assert outside.read_bytes() == b"outside"++ def test_hard_link_destination_fails_closed(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ save_path, _ = interceptor._resolve_save_path("report.pdf")+ outside = tmp_path.parent / f"{tmp_path.name}-hard-link-outside.txt"+ outside.write_bytes(b"outside")+ save_path.hardlink_to(outside)++ with pytest.raises(FileExistsError):+ interceptor._atomically_write_bytes(save_path, b"private report")++ assert outside.read_bytes() == b"outside"++ def test_fdopen_failure_leaks_neither_fd_nor_temp_file(self, tmp_path: Path) -> None:+ """A failure between os.open and os.fdopen (e.g. MemoryError) must still close the raw fd,+ unlink the just-created temp file, and close the directory fd — not just the directory fd."""+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ save_path, _ = interceptor._resolve_save_path("report.pdf")++ with patch("os.fdopen", side_effect=OSError("no file descriptors available")):+ with pytest.raises(OSError, match="no file descriptors available"):+ interceptor._atomically_write_bytes(save_path, b"private report")++ assert list(tmp_path.iterdir()) == []++ def test_collision_hard_link_race_fails_before_existing_inode_is_modified(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ save_path, _ = interceptor._resolve_save_path("report.pdf")+ save_path.write_bytes(b"existing report")+ outside = tmp_path.parent / f"{tmp_path.name}-racing-hard-link.txt"+ real_ftruncate = mod.os.ftruncate++ def add_outside_link_before_truncate(file_descriptor: int, length: int) -> None:+ outside.hardlink_to(save_path)+ real_ftruncate(file_descriptor, length)++ with (+ patch.object(mod.os, "ftruncate", side_effect=add_outside_link_before_truncate),+ pytest.raises(FileExistsError),+ ):+ interceptor._atomically_write_bytes(save_path, b"private report")++ assert save_path.read_bytes() == b"existing report"+ assert not outside.exists()++ def test_post_check_destination_swap_fails_at_publication(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ save_path, _ = interceptor._resolve_save_path("report.pdf")+ outside = tmp_path.parent / f"{tmp_path.name}-outside.txt"+ outside.write_bytes(b"outside")+ real_link = mod.os.link+ swapped = False++ def swap_destination_then_link(src: str, dst: str, **kwargs: Any) -> None:+ nonlocal swapped+ if dst == save_path.name:+ save_path.symlink_to(outside)+ swapped = True+ real_link(src, dst, **kwargs)++ with (+ patch.object(mod.os, "link", side_effect=swap_destination_then_link),+ pytest.raises(OSError),+ ):+ interceptor._atomically_write_bytes(save_path, b"private report")++ assert swapped+ assert save_path.is_symlink()+ assert outside.read_bytes() == b"outside"++ def test_alias_on_temporary_inode_during_link_fails_at_publication(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ save_path, _ = interceptor._resolve_save_path("report.pdf")+ alias = tmp_path.parent / f"{tmp_path.name}-temporary-alias.txt"+ real_link = mod.os.link++ def alias_temporary_inode_then_link(src: str, dst: str, **kwargs: Any) -> None:+ real_link(tmp_path / src, alias)+ real_link(src, dst, **kwargs)++ with (+ patch.object(mod.os, "link", side_effect=alias_temporary_inode_then_link),+ pytest.raises(OSError, match="unexpected link"),+ ):+ interceptor._atomically_write_bytes(save_path, b"private report")++ assert not save_path.exists()+ assert list(tmp_path.iterdir()) == []++ def test_post_check_output_directory_swap_fails_closed(self, tmp_path: Path) -> None:+ output_dir = tmp_path / "downloads"+ interceptor = _make_interceptor(output_dir=str(output_dir))+ save_path, _ = interceptor._resolve_save_path("report.pdf")+ original_dir = tmp_path / "original-downloads"+ output_dir.rename(original_dir)+ output_dir.mkdir()++ with pytest.raises(OSError, match="changed"):+ interceptor._atomically_write_bytes(save_path, b"private report")++ assert list(output_dir.iterdir()) == []+ assert list(original_dir.iterdir()) == []class TestCDPDownloadInterceptorProxyAuth:@@ -460,10 +637,13 @@ class TestCDPDownloadInterceptorProxyAuth:proxy_username: str | None = None,proxy_password: str | None = None,) -> CDPDownloadInterceptor:- return CDPDownloadInterceptor(+ network_egress_monitor = MagicMock()+ network_egress_monitor.authorize_request.return_value = True+ return _make_interceptor(output_dir="/tmp/test_downloads",proxy_username=proxy_username,proxy_password=proxy_password,+ network_egress_monitor=network_egress_monitor,)def _make_cdp_session(self) -> MagicMock:@@ -622,7 +802,7 @@ class TestCDPDownloadInterceptorProxyAuth:@pytest.mark.asyncioasync def test_enable_for_page_without_proxy_auth(self) -> None:- """enable_for_page without credentials should only intercept Response stage."""+ """Network mediation requires Request-stage interception even without proxy auth."""interceptor = self._make_interceptor()mock_cdp_session = self._make_cdp_session()@@ -632,11 +812,14 @@ class TestCDPDownloadInterceptorProxyAuth:await interceptor.enable_for_page(mock_page)- # Verify Fetch.enable with Response-only pattern, no auth+ # Request-stage mediation is always enabled; auth handling remains proxy-only.mock_cdp_session.send.assert_called_once_with("Fetch.enable",{- "patterns": [{"requestStage": "Response"}],+ "patterns": [+ {"requestStage": "Response"},+ {"urlPattern": "*", "requestStage": "Request"},+ ],"handleAuthRequests": False,},)@@ -646,6 +829,279 @@ class TestCDPDownloadInterceptorProxyAuth:assert "Fetch.requestPaused" in event_namesassert "Fetch.authRequired" not in event_names+ @pytest.mark.asyncio+ async def test_enable_registers_page_only_after_fetch_interception_is_live(self) -> None:+ interceptor = self._make_interceptor()+ monitor = interceptor._network_egress_monitor+ cdp_session = self._make_cdp_session()+ page = MagicMock(url="about:blank")+ page.context.new_cdp_session = AsyncMock(return_value=cdp_session)+ ordering: list[str] = []+ cdp_session.send.side_effect = lambda method, *_: ordering.append(method)+ monitor.register_active_request_interceptor.side_effect = lambda **_: ordering.append("register")++ await interceptor.enable_for_page(page)++ assert ordering == ["Fetch.enable", "register"]+ monitor.register_active_request_interceptor.assert_called_once_with(page=page, owner=interceptor)++ @pytest.mark.asyncio+ async def test_fetch_enable_failure_never_registers_page(self) -> None:+ interceptor = self._make_interceptor()+ monitor = interceptor._network_egress_monitor+ cdp_session = self._make_cdp_session()+ page = MagicMock(url="about:blank")+ page.context.new_cdp_session = AsyncMock(return_value=cdp_session)+ ordering: list[str] = []++ async def ambiguous_send(method: str, *_: object) -> None:+ ordering.append(method)+ if method == "Fetch.enable":+ raise RuntimeError("Fetch unavailable")++ cdp_session.send.side_effect = ambiguous_send+ monitor.invalidate.side_effect = lambda: ordering.append("invalidate")++ with pytest.raises(RuntimeError, match="Fetch unavailable"):+ await interceptor.enable_for_page(page)++ monitor.register_active_request_interceptor.assert_not_called()+ assert ordering == ["Fetch.enable", "invalidate", "Fetch.disable"]+ assert not interceptor._accepting_cdp_handlers+ assert interceptor._cdp_sessions == []++ @pytest.mark.asyncio+ async def test_registration_failure_invalidates_and_disables_fetch(self) -> None:+ interceptor = self._make_interceptor()+ monitor = interceptor._network_egress_monitor+ monitor.register_active_request_interceptor.side_effect = RuntimeError("registration failed")+ cdp_session = self._make_cdp_session()+ page = MagicMock(url="about:blank")+ page.context.new_cdp_session = AsyncMock(return_value=cdp_session)++ with pytest.raises(RuntimeError, match="registration failed"):+ await interceptor.enable_for_page(page)++ monitor.invalidate.assert_called_once_with()+ cdp_session.send.assert_awaited_with("Fetch.disable")+ assert interceptor._cdp_sessions == []++ @pytest.mark.asyncio+ async def test_page_closed_during_registration_unregisters_and_disables_fetch(self) -> None:+ interceptor = self._make_interceptor()+ monitor = interceptor._network_egress_monitor+ cdp_session = self._make_cdp_session()+ page = MagicMock(url="about:blank")+ page.is_closed.return_value = True+ page.context.new_cdp_session = AsyncMock(return_value=cdp_session)++ with pytest.raises(RuntimeError, match="closed during interceptor registration"):+ await interceptor.enable_for_page(page)++ monitor.unregister_active_request_interceptor.assert_called_once_with(page=page, owner=interceptor)+ cdp_session.send.assert_awaited_with("Fetch.disable")+ assert interceptor._cdp_sessions == []++ @pytest.mark.asyncio+ async def test_page_close_unregisters_exact_owner_once(self) -> None:+ interceptor = self._make_interceptor()+ monitor = interceptor._network_egress_monitor+ cdp_session = self._make_cdp_session()+ page = MagicMock(url="about:blank")+ page.context.new_cdp_session = AsyncMock(return_value=cdp_session)+ await interceptor.enable_for_page(page)+ close_listener = next(call.args[1] for call in page.on.call_args_list if call.args[0] == "close")++ close_listener(page)+ await interceptor.disable()++ monitor.unregister_active_request_interceptor.assert_called_once_with(page=page, owner=interceptor)++ @pytest.mark.asyncio+ async def test_page_close_identity_mismatch_invalidates_and_unregisters_bound_page(self) -> None:+ interceptor = self._make_interceptor()+ monitor = interceptor._network_egress_monitor+ cdp_session = self._make_cdp_session()+ page = MagicMock(url="about:blank")+ page.context.new_cdp_session = AsyncMock(return_value=cdp_session)+ await interceptor.enable_for_page(page)+ close_listener = next(call.args[1] for call in page.on.call_args_list if call.args[0] == "close")++ close_listener(MagicMock(url="about:blank"))++ monitor.invalidate.assert_called_once_with()+ monitor.unregister_active_request_interceptor.assert_called_once_with(page=page, owner=interceptor)++ @pytest.mark.asyncio+ async def test_sequential_duplicate_page_enable_fails_before_second_cdp_session(self) -> None:+ interceptor = self._make_interceptor()+ cdp_session = self._make_cdp_session()+ page = MagicMock(url="about:blank")+ page.context.new_cdp_session = AsyncMock(return_value=cdp_session)+ await interceptor.enable_for_page(page)++ with pytest.raises(RuntimeError, match="already active or enrolling"):+ await interceptor.enable_for_page(page)++ page.context.new_cdp_session.assert_awaited_once_with(page)+ assert len(interceptor._cdp_sessions) == 1++ @pytest.mark.asyncio+ async def test_concurrent_duplicate_page_enable_fails_while_first_is_in_flight(self) -> None:+ interceptor = self._make_interceptor()+ cdp_session = self._make_cdp_session()+ page = MagicMock(url="about:blank")+ first_started = asyncio.Event()+ release_first = asyncio.Event()+ session_calls = 0++ async def paused_new_session(_: object) -> MagicMock:+ nonlocal session_calls+ session_calls += 1+ if session_calls == 1:+ first_started.set()+ await release_first.wait()+ return cdp_session++ page.context.new_cdp_session = AsyncMock(side_effect=paused_new_session)+ first_enable = asyncio.create_task(interceptor.enable_for_page(page))+ await first_started.wait()+ try:+ with pytest.raises(RuntimeError, match="already active or enrolling"):+ await interceptor.enable_for_page(page)+ finally:+ release_first.set()+ await first_enable++ page.context.new_cdp_session.assert_awaited_once_with(page)+ assert len(interceptor._cdp_sessions) == 1++ @pytest.mark.asyncio+ async def test_disable_unregisters_every_page_before_disabling_fetch(self) -> None:+ interceptor = self._make_interceptor()+ monitor = interceptor._network_egress_monitor+ first_session = self._make_cdp_session()+ second_session = self._make_cdp_session()+ first_page = MagicMock(url="about:blank")+ second_page = MagicMock(url="about:blank")+ first_page.context.new_cdp_session = AsyncMock(return_value=first_session)+ second_page.context.new_cdp_session = AsyncMock(return_value=second_session)+ await interceptor.enable_for_page(first_page)+ await interceptor.enable_for_page(second_page)+ ordering: list[str] = []+ monitor.unregister_active_request_interceptor.side_effect = lambda **_: ordering.append("unregister")+ first_session.send.side_effect = lambda method, *_: ordering.append(method)+ second_session.send.side_effect = lambda method, *_: ordering.append(method)++ await interceptor.disable()++ assert ordering == ["unregister", "unregister", "Fetch.disable", "Fetch.disable"]++ @pytest.mark.asyncio+ async def test_unregister_failure_invalidates_before_fetch_disable(self) -> None:+ interceptor = self._make_interceptor()+ monitor = interceptor._network_egress_monitor+ cdp_session = self._make_cdp_session()+ page = MagicMock(url="about:blank")+ page.context.new_cdp_session = AsyncMock(return_value=cdp_session)+ await interceptor.enable_for_page(page)+ ordering: list[str] = []+ monitor.unregister_active_request_interceptor.side_effect = lambda **_: (+ ordering.append("unregister") or (_ for _ in ()).throw(RuntimeError("unregister failed"))+ )+ monitor.invalidate.side_effect = lambda: ordering.append("invalidate")+ cdp_session.send.side_effect = lambda method, *_: ordering.append(method)++ await interceptor.disable()++ assert ordering == ["unregister", "invalidate", "Fetch.disable"]++ @pytest.mark.asyncio+ async def test_fetch_disable_failure_invalidates_egress_monitor(self) -> None:+ interceptor = self._make_interceptor()+ monitor = interceptor._network_egress_monitor+ cdp_session = self._make_cdp_session()+ page = MagicMock(url="about:blank")+ page.context.new_cdp_session = AsyncMock(return_value=cdp_session)+ await interceptor.enable_for_page(page)+ monitor.invalidate.reset_mock()+ cdp_session.send.side_effect = RuntimeError("session detached")++ await interceptor.disable()++ monitor.invalidate.assert_called_once_with()+ assert interceptor._cdp_sessions == []++ @pytest.mark.asyncio+ async def test_fetch_disable_stale_interception_error_does_not_invalidate(self) -> None:+ """Normal teardown usually finds the target already closed, so Fetch.disable raising a+ stale-close error is a benign race, not a live-interception leak — it must not invalidate."""+ interceptor = self._make_interceptor()+ monitor = interceptor._network_egress_monitor+ cdp_session = self._make_cdp_session()+ page = MagicMock(url="about:blank")+ page.context.new_cdp_session = AsyncMock(return_value=cdp_session)+ await interceptor.enable_for_page(page)+ monitor.invalidate.reset_mock()+ cdp_session.send.side_effect = RuntimeError("Target closed")++ await interceptor.disable()++ monitor.invalidate.assert_not_called()+ assert interceptor._cdp_sessions == []++ @pytest.mark.asyncio+ async def test_cancelled_disable_still_unregisters_before_propagating(self) -> None:+ interceptor = self._make_interceptor()+ monitor = interceptor._network_egress_monitor+ cdp_session = self._make_cdp_session()+ page = MagicMock(url="about:blank")+ page.context.new_cdp_session = AsyncMock(return_value=cdp_session)+ await interceptor.enable_for_page(page)++ with (+ patch.object(interceptor, "_drain_tasks", new=AsyncMock(side_effect=asyncio.CancelledError)),+ pytest.raises(asyncio.CancelledError),+ ):+ await interceptor.disable()++ monitor.unregister_active_request_interceptor.assert_called_once_with(page=page, owner=interceptor)+ cdp_session.send.assert_awaited_with("Fetch.disable")++ @pytest.mark.asyncio+ async def test_cancelled_disable_waits_for_inflight_fetch_enable_cleanup(self) -> None:+ interceptor = self._make_interceptor()+ context = MagicMock()+ context._skyvern_cdp_download_interceptor = None+ page = MagicMock(url="about:blank", context=context)+ cdp_session = self._make_cdp_session()+ fetch_enable_started = asyncio.Event()+ methods: list[str] = []++ async def paused_send(method: str, *_: object) -> None:+ methods.append(method)+ if method == "Fetch.enable":+ fetch_enable_started.set()+ await asyncio.Event().wait()++ cdp_session.send.side_effect = paused_send+ context.new_cdp_session = AsyncMock(return_value=cdp_session)+ await interceptor.bind_to_context(context)+ page_listener = context.on.call_args.args[1]+ page_listener(page)+ await fetch_enable_started.wait()+ disabling = asyncio.create_task(interceptor.disable())+ await asyncio.sleep(0)+ disabling.cancel()++ with pytest.raises(asyncio.CancelledError):+ await disabling++ assert methods == ["Fetch.enable", "Fetch.disable"]+ assert not interceptor._page_enable_tasks+ assert not interceptor._accepting_cdp_handlers+ assert interceptor._cdp_sessions == []+@pytest.mark.asyncioasync def test_page_events_do_not_use_browser_download_admission(self) -> None:interceptor = self._make_interceptor(proxy_username="user", proxy_password="pass")@@ -669,22 +1125,140 @@ class TestCDPDownloadInterceptorProxyAuth:assert interceptor._browser_download_listener is None@pytest.mark.asyncio- async def test_request_stage_continues_request(self) -> None:- """Request-stage events (no responseStatusCode) should be continued with Fetch.continueRequest."""+ async def test_request_stage_authorizes_before_continuing(self) -> None:+ """The monitor must authorize before a request carrying browser credentials is dispatched."""interceptor = self._make_interceptor(proxy_username="user", proxy_password="pass")cdp_session = self._make_cdp_session()+ ordering: list[str] = []+ interceptor._network_egress_monitor.authorize_request.side_effect = lambda **_: (+ ordering.append("authorize") or True+ )+ cdp_session.send.side_effect = lambda *_: ordering.append("dispatch")event = {"requestId": "req-1",- "request": {"url": "https://example.com/page"},+ "request": {"method": "GET", "url": "https://example.com/page"},"resourceType": "Document",+ "frameId": "frame-1",# No responseStatusCode — this is a Request-stage event}await interceptor._handle_request_paused(event, cdp_session)+ assert ordering == ["authorize", "dispatch"]+ interceptor._network_egress_monitor.authorize_request.assert_called_once_with(+ method="GET",+ url="https://example.com/page",+ resource_type="document",+ frame="frame-1",+ )cdp_session.send.assert_called_once_with("Fetch.continueRequest", {"requestId": "req-1"})+ @pytest.mark.asyncio+ async def test_request_stage_dispatch_failure_does_not_log_url_or_exception_secret(self) -> None:+ interceptor = self._make_interceptor()+ cdp_session = self._make_cdp_session()+ cdp_session.send.side_effect = RuntimeError("transport-exception-secret")+ event = {+ "requestId": "req-secret",+ "request": {"method": "GET", "url": "https://example.com/private?sig=url-secret"},+ "resourceType": "Document",+ "frameId": "frame-1",+ }++ with capture_logs() as logs:+ await interceptor._handle_request_paused(event, cdp_session)++ serialized_logs = repr(logs)+ assert "url-secret" not in serialized_logs+ assert "private" not in serialized_logs+ assert "transport-exception-secret" not in serialized_logs++ @pytest.mark.asyncio+ async def test_unsafe_download_name_failure_does_not_log_url_secret(self) -> None:+ interceptor = self._make_interceptor()+ cdp_session = self._make_cdp_session()+ event = {+ "requestId": "download-secret",+ "request": {"method": "GET", "url": "https://example.com/private?sig=url-secret"},+ "resourceType": "Document",+ "frameId": "frame-1",+ "responseStatusCode": 200,+ "responseHeaders": [+ {"name": "content-type", "value": "application/pdf"},+ {"name": "content-disposition", "value": 'attachment; filename="../report.pdf"'},+ ],+ }++ with capture_logs() as logs:+ await interceptor._handle_request_paused(event, cdp_session)++ serialized_logs = repr(logs)+ assert "url-secret" not in serialized_logs+ assert "private" not in serialized_logs++ @pytest.mark.asyncio+ @pytest.mark.parametrize("monitor_verdict", [False, RuntimeError("monitor unavailable")])+ async def test_request_stage_denial_aborts_without_continuing(self, monitor_verdict: object) -> None:+ interceptor = self._make_interceptor()+ cdp_session = self._make_cdp_session()+ if isinstance(monitor_verdict, Exception):+ interceptor._network_egress_monitor.authorize_request.side_effect = monitor_verdict+ else:+ interceptor._network_egress_monitor.authorize_request.return_value = monitor_verdict+ event = {+ "requestId": "req-denied",+ "request": {"method": "GET", "url": "https://example.com/report.pdf"},+ "resourceType": "Document",+ "frameId": "frame-1",+ }++ await interceptor._handle_request_paused(event, cdp_session)++ cdp_session.send.assert_awaited_once_with(+ "Fetch.failRequest",+ {"requestId": "req-denied", "errorReason": "BlockedByClient"},+ )++ @pytest.mark.asyncio+ async def test_request_stage_missing_monitor_aborts_without_continuing(self) -> None:+ interceptor = self._make_interceptor()+ interceptor._network_egress_monitor = None+ cdp_session = self._make_cdp_session()+ event = {+ "requestId": "req-unenrolled",+ "request": {"method": "GET", "url": "https://example.com/report.pdf"},+ "resourceType": "Document",+ "frameId": "frame-1",+ }++ await interceptor._handle_request_paused(event, cdp_session)++ cdp_session.send.assert_awaited_once_with(+ "Fetch.failRequest",+ {"requestId": "req-unenrolled", "errorReason": "BlockedByClient"},+ )++ @pytest.mark.asyncio+ async def test_same_origin_redirect_without_fresh_slot_is_denied(self) -> None:+ interceptor = self._make_interceptor()+ interceptor._network_egress_monitor.authorize_request.return_value = False+ cdp_session = self._make_cdp_session()+ event = {+ "requestId": "redirect-hop",+ "redirectedRequestId": "initial-hop",+ "request": {"method": "GET", "url": "https://example.com/final.pdf"},+ "resourceType": "Document",+ "frameId": "frame-1",+ }++ await interceptor._handle_request_paused(event, cdp_session)++ cdp_session.send.assert_awaited_once_with(+ "Fetch.failRequest",+ {"requestId": "redirect-hop", "errorReason": "BlockedByClient"},+ )+@pytest.mark.asyncioasync def test_request_stage_error_does_not_retry(self) -> None:"""Request-stage errors should not attempt recovery (no duplicate continueRequest)."""@@ -754,7 +1328,7 @@ class TestStaleInterceptionRace:_MOD = "skyvern.webeye.cdp_download_interceptor"def _make_interceptor(self) -> CDPDownloadInterceptor:- return CDPDownloadInterceptor(output_dir="/tmp/test_downloads")+ return _make_interceptor(output_dir="/tmp/test_downloads")def _make_cdp_session(self) -> MagicMock:session = MagicMock()@@ -844,7 +1418,7 @@ class TestBlobDownloadCapture:@pytest.mark.asyncioasync def test_blob_download_read_and_saved(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))interceptor._browser_context = self._context()pdf_bytes = b"%PDF-1.4 fake blob invoice bytes"@@ -858,9 +1432,26 @@ class TestBlobDownloadCapture:assert saved[0].name == "invoice.pdf"assert saved[0].read_bytes() == pdf_bytes+ @pytest.mark.asyncio+ async def test_blob_download_rejects_destination_symlink(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ interceptor._browser_context = self._context()+ save_path, _ = interceptor._resolve_save_path("invoice.pdf")+ outside = tmp_path.parent / f"{tmp_path.name}-blob-outside.pdf"+ outside.write_bytes(b"outside")+ save_path.symlink_to(outside)++ with patch(self._READ_BLOB, new=AsyncMock(return_value=b"private blob")):+ await interceptor._handle_browser_download(+ {"url": "blob:https://example.com/confined", "suggestedFilename": "invoice.pdf"}+ )++ assert save_path.is_symlink()+ assert outside.read_bytes() == b"outside"+@pytest.mark.asyncioasync def test_blob_download_falls_through_pages_until_readable(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))interceptor._browser_context = self._context(num_pages=2)pdf_bytes = b"%PDF blob"read = AsyncMock(side_effect=[None, pdf_bytes])@@ -877,7 +1468,7 @@ class TestBlobDownloadCapture:@pytest.mark.asyncioasync def test_blob_download_threads_max_size_and_guards_oversize(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))interceptor._browser_context = self._context()read = AsyncMock(return_value=b"x" * 2048) # exceeds the patched limit (defense-in-depth)@@ -894,7 +1485,7 @@ class TestBlobDownloadCapture:@pytest.mark.asyncioasync def test_blob_download_unreadable_is_noop(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))interceptor._browser_context = self._context()with patch(self._READ_BLOB, new=AsyncMock(return_value=None)):@@ -908,7 +1499,7 @@ class TestBlobDownloadCapture:async def test_blob_download_saves_distinct_file_with_identical_bytes(self, tmp_path: Path) -> None:# Two independent downloads can share bytes but differ by name — the second must not be# dropped just because matching bytes already exist on disk.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))interceptor._browser_context = self._context()pdf_bytes = b"%PDF identical bytes, different download"(tmp_path / "prior.pdf").write_bytes(pdf_bytes)@@ -923,7 +1514,7 @@ class TestBlobDownloadCapture:@pytest.mark.asyncioasync def test_blob_download_no_context_is_noop(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))with patch(self._READ_BLOB, new=AsyncMock()) as read:await interceptor._handle_browser_download({"url": "blob:https://example.com/none", "suggestedFilename": "x.pdf"}@@ -933,7 +1524,7 @@ class TestBlobDownloadCapture:@pytest.mark.asyncioasync def test_blob_download_already_captured_via_fetch_is_skipped(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))url = "blob:https://example.com/dup"interceptor._downloaded_urls.add(url)interceptor._browser_context = self._context()@@ -949,7 +1540,7 @@ class TestBlobDownloadCapture:# A large-response empty-body fulfill can make page JS re-emit a 0-byte blob with the same# suggestedFilename as a just-captured real download. Since _resolve_save_path overwrites on# collision, an empty blob must never be persisted — else it truncates the real artifact.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))interceptor._browser_context = self._context()real_bytes = b"%PDF-1.4 real captured document bytes"(tmp_path / "report.pdf").write_bytes(real_bytes)@@ -965,7 +1556,7 @@ class TestBlobDownloadCapture:@pytest.mark.asyncioasync def test_empty_blob_writes_no_artifact(self, tmp_path: Path) -> None:# A genuinely empty blob is not a useful download and is skipped (no 0-byte file created).- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))interceptor._browser_context = self._context()with patch(self._READ_BLOB, new=AsyncMock(return_value=b"")):@@ -996,7 +1587,7 @@ class TestDataUrlDownloadCapture:async def test_data_url_download_saved(self, tmp_path: Path, url: str, expected_filename: str, expected_bytes: bytes) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))await interceptor._handle_browser_download({"url": url, "suggestedFilename": "report"})@@ -1011,16 +1602,31 @@ class TestDataUrlDownloadCapture:assert len(dedupe_key) == len("data:sha256:") + 64@pytest.mark.asyncio- async def test_data_url_uses_safe_generated_filename(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ async def test_data_url_rejects_non_basename_filename(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))await interceptor._handle_browser_download({"url": "data:application/pdf;base64,JVBERg==", "suggestedFilename": "../../report.pdf"})- assert [path.name for path in tmp_path.iterdir()] == ["report.pdf"]+ assert list(tmp_path.iterdir()) == []assert not (tmp_path.parent / "report.pdf").exists()+ @pytest.mark.asyncio+ async def test_data_url_rejects_destination_symlink(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ save_path, _ = interceptor._resolve_save_path("report.pdf")+ outside = tmp_path.parent / f"{tmp_path.name}-data-outside.pdf"+ outside.write_bytes(b"outside")+ save_path.symlink_to(outside)++ await interceptor._handle_browser_download(+ {"url": "data:application/pdf;base64,JVBERg==", "suggestedFilename": "report.pdf"}+ )++ assert save_path.is_symlink()+ assert outside.read_bytes() == b"outside"+@pytest.mark.asyncio@pytest.mark.parametrize("url",@@ -1035,7 +1641,7 @@ class TestDataUrlDownloadCapture:],)async def test_malformed_data_url_does_not_create_artifact(self, tmp_path: Path, url: str) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))await interceptor._handle_browser_download({"url": url, "suggestedFilename": "report.pdf"})@@ -1044,7 +1650,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_duplicate_data_url_event_is_saved_once(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))event = {"url": "data:text/plain,hello", "suggestedFilename": "note.txt"}await interceptor._handle_browser_download(event)@@ -1055,7 +1661,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_duplicate_data_url_logs_never_include_payload(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))payload = "private-inline-payload"event = {"url": f"data:text/plain,{payload}", "suggestedFilename": "note.txt"}@@ -1067,7 +1673,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_non_base64_oversize_rejected_before_percent_decode(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))with (patch.object(mod, "MAX_FILE_SIZE_BYTES", 4),patch.object(mod, "_percent_decode_payload", wraps=mod._percent_decode_payload) as decode,@@ -1081,7 +1687,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_concurrent_duplicate_is_reserved_and_failure_can_retry(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))event = {"url": "data:text/plain,hello", "suggestedFilename": "note.txt"}started = asyncio.Event()release = asyncio.Event()@@ -1103,7 +1709,7 @@ class TestDataUrlDownloadCapture:assert [path.name for path in tmp_path.iterdir()] == ["note.txt"]- retry_interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path / "retry"))+ retry_interceptor = _make_interceptor(output_dir=str(tmp_path / "retry"))with patch.object(retry_interceptor, "_decode_data_url", side_effect=OSError("transient")):await retry_interceptor._handle_browser_download(event)await retry_interceptor._handle_browser_download(event)@@ -1113,7 +1719,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_data_url_over_size_limit_does_not_create_artifact(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))url = "data:application/octet-stream;base64,eHh4eHg="with patch.object(mod, "MAX_FILE_SIZE_BYTES", 4):@@ -1123,46 +1729,46 @@ class TestDataUrlDownloadCapture:assert url not in interceptor._downloaded_urls@staticmethod- def _paused_replace() -> tuple[threading.Event, threading.Event, Any]:+ def _paused_publication() -> tuple[threading.Event, threading.Event, Any]:entered, release = threading.Event(), threading.Event()- real_replace = mod.os.replace+ real_link = mod.os.link- def replace(source: Path, destination: Path) -> None:+ def link(source: str, destination: str, **kwargs: Any) -> None:entered.set()assert release.wait(timeout=2)- real_replace(source, destination)+ real_link(source, destination, **kwargs)- return entered, release, patch.object(mod.os, "replace", side_effect=replace)+ return entered, release, patch.object(mod.os, "link", side_effect=link)@pytest.mark.asyncioasync def test_data_url_is_atomically_published_from_incomplete_path(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- entered_replace, release_replace, replace_patch = self._paused_replace()- with replace_patch:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ entered_publication, release_publication, publication_patch = self._paused_publication()+ with publication_patch:task = asyncio.create_task(interceptor._handle_browser_download({"url": "data:text/plain,complete", "suggestedFilename": "note.txt"}))- assert await asyncio.to_thread(entered_replace.wait, 2)+ assert await asyncio.to_thread(entered_publication.wait, 2)visible = list(tmp_path.iterdir())assert len(visible) == 1assert visible[0].name.startswith("note.txt.")assert visible[0].name.endswith(".crdownload")assert not (tmp_path / "note.txt").exists()- release_replace.set()+ release_publication.set()await asyncio.wait_for(task, timeout=2)assert [path.name for path in tmp_path.iterdir()] == ["note.txt"]@pytest.mark.asyncioasync def test_cancellation_drains_publication_before_retry(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))event = {"url": "data:text/plain,complete", "suggestedFilename": "note.txt"}- entered_replace, release_replace, replace_patch = self._paused_replace()- with replace_patch:+ entered_publication, release_publication, publication_patch = self._paused_publication()+ with publication_patch:first = asyncio.create_task(interceptor._handle_browser_download(event))- assert await asyncio.to_thread(entered_replace.wait, 2)+ assert await asyncio.to_thread(entered_publication.wait, 2)first.cancel()retry = asyncio.create_task(interceptor._handle_browser_download(event))await asyncio.sleep(0)@@ -1170,7 +1776,7 @@ class TestDataUrlDownloadCapture:visible = list(tmp_path.iterdir())assert len(visible) == 1assert visible[0].name.endswith(".crdownload")- release_replace.set()+ release_publication.set()with pytest.raises(asyncio.CancelledError):await asyncio.wait_for(first, timeout=2)await asyncio.wait_for(retry, timeout=2)@@ -1180,8 +1786,10 @@ class TestDataUrlDownloadCapture:assert len(interceptor._downloaded_urls) == 1@pytest.mark.asyncio- async def test_concurrent_distinct_data_urls_with_same_filename_are_serialized(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ async def test_concurrent_distinct_data_urls_with_same_filename_fail_closed_on_collision(+ self, tmp_path: Path+ ) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))first = {"url": "data:text/plain,first", "suggestedFilename": "note.txt"}second = {"url": "data:text/plain,second", "suggestedFilename": "note.txt"}@@ -1194,12 +1802,12 @@ class TestDataUrlDownloadCapture:)assert (tmp_path / "note.txt").read_bytes() in {b"first", b"second"}- assert len(interceptor._downloaded_urls) == 2+ assert len(interceptor._downloaded_urls) == 1assert not list(tmp_path.glob("*.crdownload"))@pytest.mark.asyncioasync def test_digest_is_off_loop_and_invalid_shape_rejected_before_digest(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))event_loop_thread = threading.get_ident()real_identity = mod._download_identity@@ -1223,7 +1831,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_disable_drains_active_browser_download_task(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))browser_session = MagicMock()browser_session.send = AsyncMock()browser_session.detach = AsyncMock()@@ -1254,7 +1862,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_settle_browser_downloads_includes_event_admitted_while_draining(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))interceptor._accepting_browser_downloads = Truefirst_started = asyncio.Event()release_first = asyncio.Event()@@ -1288,7 +1896,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_settle_browser_downloads_drains_event_admitted_inside_context(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))interceptor._accepting_browser_downloads = Truehandler_started = asyncio.Event()release_handler = asyncio.Event()@@ -1316,7 +1924,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_cancelled_settle_does_not_poison_reused_interceptor(self) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()interceptor._accepting_browser_downloads = Truefirst_started = asyncio.Event()never_release = asyncio.Event()@@ -1350,7 +1958,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_cancelled_settle_body_cancels_admitted_handler_and_remains_reusable(self) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()interceptor._accepting_browser_downloads = Truefirst_started = asyncio.Event()second_handled = asyncio.Event()@@ -1383,7 +1991,7 @@ class TestDataUrlDownloadCapture:assert interceptor._accepting_browser_downloadsdef test_maximum_size_percent_encoded_payload_is_accepted(self, monkeypatch: pytest.MonkeyPatch) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()monkeypatch.setattr(mod, "MAX_FILE_SIZE_BYTES", 12)url = "data:text/plain," + "%41" * 12@@ -1393,7 +2001,7 @@ class TestDataUrlDownloadCapture:assert data == b"A" * 12def test_percent_escaped_base64_payload_is_accepted(self, monkeypatch: pytest.MonkeyPatch) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()monkeypatch.setattr(mod, "MAX_FILE_SIZE_BYTES", 1)url = "data:text/plain;base64,%51%51%3D%3D"@@ -1403,7 +2011,7 @@ class TestDataUrlDownloadCapture:assert data == b"A"def test_maximum_size_percent_escaped_base64_payload_is_accepted(self, monkeypatch: pytest.MonkeyPatch) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()monkeypatch.setattr(mod, "MAX_FILE_SIZE_BYTES", 4)url = "data:text/plain;base64," + "".join(f"%{byte:02X}" for byte in b"QUJDRA==")@@ -1413,7 +2021,7 @@ class TestDataUrlDownloadCapture:assert data == b"ABCD"def test_percent_escaped_base64_decoded_overflow_is_rejected(self, monkeypatch: pytest.MonkeyPatch) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()monkeypatch.setattr(mod, "MAX_FILE_SIZE_BYTES", 4)url = "data:text/plain;base64," + "".join(f"%{byte:02X}" for byte in b"QUJDREU=")@@ -1429,7 +2037,7 @@ class TestDataUrlDownloadCapture:],)def test_malformed_percent_escaped_base64_is_rejected(self, monkeypatch: pytest.MonkeyPatch, payload: str) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()monkeypatch.setattr(mod, "MAX_FILE_SIZE_BYTES", 4)url = f"data:text/plain;base64,{payload}"@@ -1450,7 +2058,7 @@ class TestDataUrlDownloadCapture:decoded_length.assert_not_called()def test_oversized_percent_payload_rejected_before_decode_allocation(self, monkeypatch: pytest.MonkeyPatch) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()monkeypatch.setattr(mod, "MAX_FILE_SIZE_BYTES", 12)url = "data:text/plain," + "A" * 12 + "%41"@@ -1463,7 +2071,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_disable_waits_for_racing_browser_monitor_enable(self) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()send_started = asyncio.Event()release_send = asyncio.Event()browser_session = MagicMock()@@ -1494,7 +2102,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_browser_monitor_can_reenable_after_disable(self) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()first_session = MagicMock(send=AsyncMock(), detach=AsyncMock())second_session = MagicMock(send=AsyncMock(), detach=AsyncMock())browser = MagicMock()@@ -1516,7 +2124,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_context_binding_owns_new_page_listener_and_tasks(self) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()context = MagicMock()context._skyvern_cdp_download_interceptor = Nonepage = MagicMock()@@ -1548,8 +2156,8 @@ class TestDataUrlDownloadCapture:self, monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr(mod, "BROWSER_INTERCEPTOR_DISABLE_TIMEOUT", 0.01)- old_interceptor = CDPDownloadInterceptor()- new_interceptor = CDPDownloadInterceptor()+ old_interceptor = _make_interceptor()+ new_interceptor = _make_interceptor()context = MagicMock()context._skyvern_cdp_download_interceptor = old_interceptorentered_cancel = asyncio.Event()@@ -1604,8 +2212,8 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_context_rebind_awaits_fast_disable_before_binding(self) -> None:- old_interceptor = CDPDownloadInterceptor()- new_interceptor = CDPDownloadInterceptor()+ old_interceptor = _make_interceptor()+ new_interceptor = _make_interceptor()context = MagicMock()context._skyvern_cdp_download_interceptor = old_interceptordisabled = False@@ -1624,8 +2232,8 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_context_rebind_propagates_fast_disable_failure(self) -> None:- old_interceptor = CDPDownloadInterceptor()- new_interceptor = CDPDownloadInterceptor()+ old_interceptor = _make_interceptor()+ new_interceptor = _make_interceptor()context = MagicMock()context._skyvern_cdp_download_interceptor = old_interceptorold_interceptor.disable = AsyncMock(side_effect=RuntimeError("disable failed")) # type: ignore[method-assign]@@ -1639,8 +2247,8 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_context_rebind_cancellation_owns_old_disable_task(self) -> None:- old_interceptor = CDPDownloadInterceptor()- new_interceptor = CDPDownloadInterceptor()+ old_interceptor = _make_interceptor()+ new_interceptor = _make_interceptor()context = MagicMock()context._skyvern_cdp_download_interceptor = old_interceptorstarted = asyncio.Event()@@ -1676,7 +2284,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_cancelled_rebind_detached_disable_has_external_gc_root(self) -> None:- old_interceptor = CDPDownloadInterceptor()+ old_interceptor = _make_interceptor()context = MagicMock()context._skyvern_cdp_download_interceptor = old_interceptorstarted = asyncio.Event()@@ -1692,7 +2300,7 @@ class TestDataUrlDownloadCapture:await asyncio.wait_for(release.wait(), timeout=2)old_interceptor.disable = stuck_disable # type: ignore[method-assign]- new_interceptor = CDPDownloadInterceptor()+ new_interceptor = _make_interceptor()new_ref = weakref.ref(new_interceptor)binding = asyncio.create_task(new_interceptor.bind_to_context(context))binding_ref = weakref.ref(binding)@@ -1726,7 +2334,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_context_binding_same_interceptor_is_idempotent(self) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()context = MagicMock()context._skyvern_cdp_download_interceptor = None@@ -1741,9 +2349,9 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_concurrent_context_rebinds_leave_only_last_listener(self) -> None:- old_interceptor = CDPDownloadInterceptor()- first_interceptor = CDPDownloadInterceptor()- second_interceptor = CDPDownloadInterceptor()+ old_interceptor = _make_interceptor()+ first_interceptor = _make_interceptor()+ second_interceptor = _make_interceptor()context = MagicMock()context._skyvern_cdp_download_interceptor = old_interceptorold_disable_started = asyncio.Event()@@ -1789,7 +2397,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_context_page_enable_failure_is_retrieved_and_logged(self) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()context = MagicMock()context._skyvern_cdp_download_interceptor = None@@ -1815,7 +2423,7 @@ class TestDataUrlDownloadCapture:@pytest.mark.asyncioasync def test_disable_drains_admitted_fetch_handler(self) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()started = asyncio.Event()release = asyncio.Event()session = MagicMock(send=AsyncMock())@@ -1838,46 +2446,21 @@ class TestDataUrlDownloadCapture:class TestDirectHttpDownloadAuthAndHtmlGuard:- """_download_url_directly falls back from the cookie-sharing Playwright APIRequestContext to a- raw urllib fetch. That fallback must (1) still carry the browser session's cookies, and (2) not- silently save an HTML login/session-gate page under a binary filename.+ """Direct HTTP downloads fail closed without an enrolled backend and reject HTML login masquerades."""- Regression: a session-gated download endpoint fetched without cookies returns its HTML login- page (HTTP 200); the old fallback issued a cookieless request and saved that HTML as e.g. a- .zip while reporting the download as successful.- """-- _URLOPEN = "skyvern.webeye.cdp_download_interceptor._urlopen_single_hop"+ _URLOPEN = "urllib.request.urlopen"+ _BUILD_OPENER = "urllib.request.build_opener"@staticmethod- def _fake_urlopen(body: bytes, content_type: str) -> MagicMock:- return MagicMock(return_value=(200, {"content-type": content_type}, body))+ def _context() -> MagicMock:+ context = MagicMock()+ context.cookies = AsyncMock(return_value=[])+ context.request.get = AsyncMock(side_effect=AssertionError("raw BrowserContext request bypass"))+ return context@staticmethod- def _context_forcing_urllib(cookies: list[dict]) -> MagicMock:- """Browser context whose APIRequestContext returns proxy-407 (non-OK), forcing the urllib- fallback, and whose cookies() returns the given session cookies."""- ctx = MagicMock()- non_ok = MagicMock()- non_ok.ok = False- non_ok.status = 407- ctx.request.get = AsyncMock(return_value=non_ok)- ctx.cookies = AsyncMock(return_value=cookies)- return ctx-- @pytest.fixture(autouse=True)- def _resolvable_site_host(self, monkeypatch: pytest.MonkeyPatch) -> None:- """Keep this class's ``site.example`` fixtures off real DNS now destinations are checked."""- import socket-- real_getaddrinfo = socket.getaddrinfo-- def fake_getaddrinfo(host: str, port: object = None, *args: object, **kwargs: object) -> list:- if host == "site.example":- return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", port or 0))]- return real_getaddrinfo(host, port, *args, **kwargs)-- monkeypatch.setattr(socket, "getaddrinfo", fake_getaddrinfo)+ def _guarded_fetch(body: bytes, content_type: str, filename: str) -> AsyncMock:+ return AsyncMock(return_value=MagicMock(body=body, content_type=content_type, filename=filename))_LOGIN_HTML = (b'\n<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">\n'@@ -1886,152 +2469,197 @@ class TestDirectHttpDownloadAuthAndHtmlGuard:)@pytest.mark.asyncio- async def test_urllib_fallback_forwards_browser_cookies(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib(- [{"name": "ASP.NET_SessionId", "value": "sess123"}, {"name": "auth", "value": "tok"}]- )- zip_bytes = b"PK\x03\x04 real zip payload"- urlopen = self._fake_urlopen(zip_bytes, "application/zip")-- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly("https://site.example/download?f=statement.zip", "statement.zip")-- sent_request = urlopen.call_args.args[0]- assert sent_request.get_header("Cookie") == "ASP.NET_SessionId=sess123; auth=tok"- saved = list(tmp_path.iterdir())- assert [p.name for p in saved] == ["statement.zip"]- assert saved[0].read_bytes() == zip_bytes-- @pytest.mark.asyncio- async def test_cookie_header_skips_control_char_values(self, tmp_path: Path) -> None:- # A stored cookie value with CR/LF must not inject extra directives into the Cookie line.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib(- [{"name": "good", "value": "ok"}, {"name": "bad", "value": "x\r\nSet-Cookie: evil=1"}]- )- urlopen = self._fake_urlopen(b"PK\x03\x04 zip payload", "application/zip")-- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly("https://site.example/download?f=statement.zip", "statement.zip")-- assert urlopen.call_args.args[0].get_header("Cookie") == "good=ok"-- @pytest.mark.asyncio- async def test_html_login_page_for_binary_filename_not_saved(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib([])- urlopen = self._fake_urlopen(self._LOGIN_HTML, "text/html; charset=utf-8")-- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly("https://site.example/download?f=statement.zip", "statement.zip")-- assert list(tmp_path.iterdir()) == []-- @pytest.mark.asyncio- async def test_html_login_page_for_encoded_binary_filename_not_saved(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib([])- urlopen = self._fake_urlopen(self._LOGIN_HTML, "text/html; charset=utf-8")+ async def test_failed_guarded_helper_does_not_fall_back_to_unenrolled_clients(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ context = self._context()+ interceptor._browser_context = context+ guarded_fetch = AsyncMock(side_effect=RuntimeError("guarded backend unavailable"))+ urlopen = MagicMock(side_effect=AssertionError("raw urllib bypass"))+ build_opener = MagicMock(side_effect=AssertionError("raw urllib opener bypass"))- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly("https://site.example/download?f=statement.zip", "statement%2Ezip")+ with (+ patch.object(mod.file_api, "fetch_file_bytes", guarded_fetch, create=True),+ patch(self._URLOPEN, urlopen),+ patch(self._BUILD_OPENER, build_opener),+ ):+ await interceptor._download_url_directly("https://site.example/report.pdf", "report.pdf")+ context.request.get.assert_not_called()+ urlopen.assert_not_called()+ build_opener.assert_not_called()assert list(tmp_path.iterdir()) == []@pytest.mark.asyncio- async def test_html_payload_for_encoded_html_filename_is_saved(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib([])- urlopen = self._fake_urlopen(b"<!DOCTYPE html><html><body>report</body></html>", "text/html")-- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly("https://site.example/report", "report%2Ehtml")+ async def test_direct_download_uses_only_guarded_helper(self, tmp_path: Path) -> None:+ authorizer = AsyncMock()+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ interceptor._redirect_hop_authorizer = authorizer+ context = MagicMock()+ context.cookies = AsyncMock(return_value=[{"name": "session", "value": "cookie-secret"}])+ context.request.get = AsyncMock(side_effect=AssertionError("raw BrowserContext request bypass"))+ interceptor._browser_context = context+ guarded_response = MagicMock(+ body=b"private report",+ content_type="application/pdf",+ filename="report.pdf",+ )+ guarded_fetch = AsyncMock(return_value=guarded_response)+ urlopen = MagicMock(side_effect=AssertionError("raw urllib bypass"))+ build_opener = MagicMock(side_effect=AssertionError("raw urllib opener bypass"))- assert [path.name for path in tmp_path.iterdir()] == ["report.html"]+ with (+ patch("skyvern.forge.sdk.api.files.fetch_file_bytes", guarded_fetch, create=True),+ patch(self._URLOPEN, urlopen),+ patch(self._BUILD_OPENER, build_opener),+ ):+ await interceptor._download_url_directly("https://site.example/report.pdf?sig=secret", "report.pdf")++ context.request.get.assert_not_called()+ urlopen.assert_not_called()+ build_opener.assert_not_called()+ guarded_fetch.assert_awaited_once_with(+ "https://site.example/report.pdf?sig=secret",+ max_size_mb=100,+ headers={"Cookie": "session=cookie-secret"},+ filename="report.pdf",+ authorize_request_hop=authorizer,+ )+ assert (tmp_path / "report.pdf").read_bytes() == b"private report"@pytest.mark.asyncio- async def test_html_payload_for_double_encoded_binary_filename_is_not_saved(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib([])- urlopen = self._fake_urlopen(self._LOGIN_HTML, "text/html")+ async def test_guarded_download_failure_does_not_log_url_or_exception_secret(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ interceptor._browser_context = self._context()+ guarded_fetch = AsyncMock(side_effect=RuntimeError("exception-secret"))- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly(- "https://site.example/download?f=statement%252Ezip", "statement%252Ezip"+ with (+ patch.object(mod.file_api, "fetch_file_bytes", guarded_fetch, create=True),+ capture_logs() as logs,+ ):+ await interceptor._handle_browser_download(+ {+ "url": "https://site.example/protected-path?sig=url-secret",+ "suggestedFilename": "report.pdf",+ })- assert list(tmp_path.iterdir()) == []+ serialized_logs = repr(logs)+ assert "url-secret" not in serialized_logs+ assert "protected-path" not in serialized_logs+ assert "exception-secret" not in serialized_logs+ assert any(+ log.get("event") == "Guarded direct download failed" and log.get("error_type") == "RuntimeError"+ for log in logs+ )@pytest.mark.asyncio- async def test_html_body_without_html_content_type_still_rejected(self, tmp_path: Path) -> None:- # Some servers mislabel the login page's content-type; sniff the body markup too.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib([])- urlopen = self._fake_urlopen(self._LOGIN_HTML, "application/octet-stream")+ async def test_cookie_header_omits_control_characters(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ context = self._context()+ context.cookies.return_value = [+ {"name": "session", "value": "safe"},+ {"name": "injected\r\nHeader", "value": "bad"},+ {"name": "unsafe", "value": "bad\x00value"},+ ]+ interceptor._browser_context = context- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly("https://site.example/download?f=statement.zip", "statement.zip")-- assert list(tmp_path.iterdir()) == []+ assert await interceptor._cookie_header_for_url("https://site.example/report.pdf") == "session=safe"@pytest.mark.parametrize(- "body",+ ("body", "content_type", "filename", "expected_filename"),[- pytest.param(b"<!-- generated --><!DOCTYPE html><html><body>login</body></html>", id="comment"),- pytest.param(b'<?xml version="1.0"?><html><body>login</body></html>', id="xml-declaration"),- pytest.param(b"<head><title>Login</title></head><body>login</body>", id="omitted-html-root"),- pytest.param(b"<head\n><title>Login</title></head><body>login</body>", id="tag-newline"),+ pytest.param(_LOGIN_HTML, "text/html", "statement.zip", None, id="binary-name"),+ pytest.param(_LOGIN_HTML, "text/html", "statement%2Ezip", None, id="encoded-binary-name"),+ pytest.param(_LOGIN_HTML, "text/html", "statement%252Ezip", None, id="double-encoded-binary-name"),+ pytest.param(_LOGIN_HTML, "application/octet-stream", "statement.zip", None, id="mislabelled-html"),+ pytest.param(_LOGIN_HTML, "application/octet-stream", "", None, id="nameless-binary-claim"),+ pytest.param(_LOGIN_HTML, "text/html", "statement", None, id="extensionless-name"),+ pytest.param(_LOGIN_HTML, "text/html", "report%2Ehtml", "report.html", id="encoded-html-name"),+ pytest.param(_LOGIN_HTML, "text/html", "report.html", "report.html", id="html-name"),+ pytest.param(_LOGIN_HTML, "text/html", "", "<generated>", id="nameless-html"),+ pytest.param(_LOGIN_HTML, "", "", "<generated>", id="nameless-no-content-type"),+ pytest.param(b"%PDF-1.7 report", "application/pdf", "invoice.pdf", "invoice.pdf", id="binary"),+ pytest.param(b"PK\x03\x04 archive", "text/html", "archive.zip", "archive.zip", id="mislabeled-binary"),+ pytest.param(+ b"<!-- generated --><!DOCTYPE html><html><body>login</body></html>",+ "application/octet-stream",+ "statement.zip",+ None,+ id="leading-comment",+ ),+ pytest.param(+ b'<?xml version="1.0"?><html><body>login</body></html>',+ "application/octet-stream",+ "statement.zip",+ None,+ id="xml-declaration",+ ),+ pytest.param(+ b"<head><title>Login</title></head><body>login</body>",+ "application/octet-stream",+ "statement.zip",+ None,+ id="omitted-html-root",+ ),+ pytest.param(+ b"<head\n><title>Login</title></head><body>login</body>",+ "application/octet-stream",+ "statement.zip",+ None,+ id="tag-newline",+ ),],)@pytest.mark.asyncio- async def test_html_body_with_legal_leading_markup_is_rejected(self, tmp_path: Path, body: bytes) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib([])- urlopen = self._fake_urlopen(body, "application/octet-stream")-- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly("https://site.example/download", "statement.zip")-- assert list(tmp_path.iterdir()) == []-- @pytest.mark.asyncio- async def test_real_binary_payload_is_saved(self, tmp_path: Path) -> None:- # Guard must not over-reject genuine binary downloads.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib([])- pdf = b"%PDF-1.7 real invoice bytes"- urlopen = self._fake_urlopen(pdf, "application/pdf")+ async def test_html_masquerade_guard(+ self,+ tmp_path: Path,+ body: bytes,+ content_type: str,+ filename: str,+ expected_filename: str | None,+ ) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ interceptor._browser_context = self._context()+ guarded_fetch = self._guarded_fetch(body, content_type, filename)- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly("https://site.example/download?i=1", "invoice.pdf")+ with patch.object(mod.file_api, "fetch_file_bytes", guarded_fetch, create=True):+ await interceptor._download_url_directly("https://site.example/download", filename)saved = list(tmp_path.iterdir())- assert [p.name for p in saved] == ["invoice.pdf"]- assert saved[0].read_bytes() == pdf+ if expected_filename is None:+ assert saved == []+ else:+ assert len(saved) == 1+ if expected_filename != "<generated>":+ assert saved[0].name == expected_filename+ assert saved[0].read_bytes() == body@pytest.mark.asyncio- async def test_html_payload_for_html_filename_is_saved(self, tmp_path: Path) -> None:- # An HTML document downloaded under an .html name is honest, not a masquerade — keep it.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib([])- urlopen = self._fake_urlopen(b"<!DOCTYPE html><html><body>report</body></html>", "text/html")+ async def test_direct_download_rejects_destination_symlink(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ interceptor._browser_context = self._context()+ guarded_fetch = self._guarded_fetch(b"private report", "application/pdf", "report.pdf")+ save_path, _ = interceptor._resolve_save_path("report.pdf")+ outside = tmp_path.parent / f"{tmp_path.name}-direct-outside.pdf"+ outside.write_bytes(b"outside")+ save_path.symlink_to(outside)- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly("https://site.example/report", "report.html")+ with patch.object(mod.file_api, "fetch_file_bytes", guarded_fetch, create=True):+ await interceptor._handle_browser_download(+ {"url": "https://example.com/report.pdf", "suggestedFilename": "report.pdf"}+ )- saved = list(tmp_path.iterdir())- assert [p.name for p in saved] == ["report.html"]+ assert save_path.is_symlink()+ assert outside.read_bytes() == b"outside"@pytest.mark.asyncioasync def test_http_browser_download_routes_through_direct_download(self, tmp_path: Path) -> None:- # Real wiring: an http(s) browser download goes through _download_url_directly, which- # forwards cookies and rejects the HTML login masquerade.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib([{"name": "sid", "value": "x"}])- urlopen = self._fake_urlopen(self._LOGIN_HTML, "text/html")+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ interceptor._browser_context = self._context()+ guarded_fetch = self._guarded_fetch(self._LOGIN_HTML, "text/html", "report.zip")- with patch(self._URLOPEN, urlopen):+ with patch.object(mod.file_api, "fetch_file_bytes", guarded_fetch, create=True):await interceptor._handle_browser_download({"url": "https://site.example/download?f=report.zip",@@ -2040,71 +2668,6 @@ class TestDirectHttpDownloadAuthAndHtmlGuard:)assert list(tmp_path.iterdir()) == []- assert urlopen.call_args.args[0].get_header("Cookie") == "sid=x"-- @pytest.mark.asyncio- async def test_nameless_binary_content_type_html_body_not_saved(self, tmp_path: Path) -> None:- # No filename extension but a binary content-type + HTML body is still a masquerade.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib([])- urlopen = self._fake_urlopen(self._LOGIN_HTML, "application/octet-stream")-- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly("https://site.example/download", "")-- assert list(tmp_path.iterdir()) == []-- @pytest.mark.asyncio- async def test_nameless_html_content_type_is_saved(self, tmp_path: Path) -> None:- # A nameless download that is honestly HTML (html content-type, no binary claim) is kept.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib([])- urlopen = self._fake_urlopen(self._LOGIN_HTML, "text/html; charset=utf-8")-- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly("https://site.example/page", "")-- assert len(list(tmp_path.iterdir())) == 1-- @pytest.mark.asyncio- async def test_extensionless_named_html_login_page_is_not_saved(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib([])- urlopen = self._fake_urlopen(self._LOGIN_HTML, "text/html; charset=utf-8")-- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly("https://site.example/download", "statement")-- assert list(tmp_path.iterdir()) == []-- @pytest.mark.asyncio- async def test_binary_payload_mislabeled_as_html_is_saved(self, tmp_path: Path) -> None:- # A real binary payload a server mislabels as text/html must be saved, not discarded:- # the body is the ground truth, so a non-HTML body is never treated as a masquerade.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib([])- zip_bytes = b"PK\x03\x04 genuine archive, not html"- urlopen = self._fake_urlopen(zip_bytes, "text/html")-- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly("https://site.example/download?f=statement.zip", "statement.zip")-- saved = list(tmp_path.iterdir())- assert [p.name for p in saved] == ["statement.zip"]- assert saved[0].read_bytes() == zip_bytes-- @pytest.mark.asyncio- async def test_nameless_no_content_type_html_is_saved(self, tmp_path: Path) -> None:- # Nameless download with no content-type and an HTML body makes no binary claim, so it is- # saved as-is (intentional "no claim, no mismatch"), not rejected.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context_forcing_urllib([])- urlopen = self._fake_urlopen(self._LOGIN_HTML, "")-- with patch(self._URLOPEN, urlopen):- await interceptor._download_url_directly("https://site.example/page", "")-- assert len(list(tmp_path.iterdir())) == 1class TestDownloadDirRebindDedup:@@ -2120,7 +2683,7 @@ class TestDownloadDirRebindDedup:async def test_data_url_reprocessed_after_dir_change(self, tmp_path: Path) -> None:dir_a = tmp_path / "run_a"dir_b = tmp_path / "run_b"- interceptor = CDPDownloadInterceptor(output_dir=str(dir_a))+ interceptor = _make_interceptor(output_dir=str(dir_a))event = {"url": "data:text/plain,hello", "suggestedFilename": "note.txt"}await interceptor._handle_browser_download(event)@@ -2135,7 +2698,7 @@ class TestDownloadDirRebindDedup:@pytest.mark.asyncioasync def test_same_dir_set_preserves_data_url_dedupe(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))event = {"url": "data:text/plain,hello", "suggestedFilename": "note.txt"}await interceptor._handle_browser_download(event)@@ -2148,7 +2711,7 @@ class TestDownloadDirRebindDedup:assert [path.name for path in tmp_path.iterdir()] == ["note.txt"]def test_real_dir_change_clears_cross_path_dedupe(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path / "run_a"))+ interceptor = _make_interceptor(output_dir=str(tmp_path / "run_a"))interceptor._downloaded_urls.update({"https://site.example/report.pdf", "blob:https://site.example/abc", "data:sha256:deadbeef"})@@ -2158,7 +2721,7 @@ class TestDownloadDirRebindDedup:assert interceptor._downloaded_urls == set()def test_same_dir_set_preserves_cross_path_dedupe(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))seeded = {"https://site.example/report.pdf", "blob:https://site.example/abc"}interceptor._downloaded_urls.update(seeded)@@ -2167,7 +2730,7 @@ class TestDownloadDirRebindDedup:assert interceptor._downloaded_urls == seededdef test_first_dir_set_from_none_does_not_touch_dedupe(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor()+ interceptor = _make_interceptor()assert interceptor._output_dir is Noneinterceptor.set_download_dir(str(tmp_path))@@ -2182,16 +2745,16 @@ class TestDownloadDirRebindDedup:in dir A, so dir B could otherwise skip an identical download and miss its artifact."""dir_a = tmp_path / "run_a"dir_b = tmp_path / "run_b"- interceptor = CDPDownloadInterceptor(output_dir=str(dir_a))+ interceptor = _make_interceptor(output_dir=str(dir_a))event = {"url": "data:text/plain,hello", "suggestedFilename": "note.txt"}- entered_replace, release_replace, replace_patch = TestDataUrlDownloadCapture._paused_replace()- with replace_patch:+ entered_publication, release_publication, publication_patch = TestDataUrlDownloadCapture._paused_publication()+ with publication_patch:writing = asyncio.create_task(interceptor._handle_browser_download(event))- assert await asyncio.to_thread(entered_replace.wait, 2)+ assert await asyncio.to_thread(entered_publication.wait, 2)interceptor.set_download_dir(str(dir_b))assert interceptor._downloaded_urls == set()- release_replace.set()+ release_publication.set()await asyncio.wait_for(writing, timeout=2)assert (dir_a / "note.txt").read_bytes() == b"hello"@@ -2209,7 +2772,7 @@ class TestDownloadDirRebindDedup:"""A failed mkdir during a real dir change must not leave the new scope carrying the priorrun's dedupe: the clear happens on scope assignment, before mkdir, so a same-dir retry(dir_changed=False) still starts from an empty set and can write."""- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path / "run_a"))+ interceptor = _make_interceptor(output_dir=str(tmp_path / "run_a"))interceptor._downloaded_urls.update({"https://site.example/report.pdf", "data:sha256:deadbeef"})target = tmp_path / "run_b"event = {"url": "data:text/plain,hello", "suggestedFilename": "note.txt"}@@ -2401,7 +2964,7 @@ class TestTwoPathDownloadStreaming:@pytest.mark.asyncioasync def test_small_body_buffered_full_replay(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))payload = b"A" * 500cdp = _StreamCDP(chunks=[payload])with _stream_limits():@@ -2412,9 +2975,26 @@ class TestTwoPathDownloadStreaming:assert cdp.count("Fetch.getResponseBody") == 0assert cdp.count("Fetch.failRequest") == 0+ @pytest.mark.asyncio+ async def test_buffered_download_rejects_destination_symlink(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ save_path, _ = interceptor._resolve_save_path("doc.pdf")+ outside = tmp_path.parent / f"{tmp_path.name}-buffered-outside.pdf"+ outside.write_bytes(b"outside")+ save_path.symlink_to(outside)+ cdp = _StreamCDP(chunks=[b"private report"])++ with _stream_limits(threshold=1024):+ await _drive_download(interceptor, cdp, _raw_headers(content_length=14))++ assert save_path.is_symlink()+ assert outside.read_bytes() == b"outside"+ assert cdp.count("Fetch.failRequest") == 1+ assert cdp.count("Fetch.fulfillRequest") == 0+@pytest.mark.asyncioasync def test_at_threshold_streams_to_disk_empty_fulfill(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))payload = b"B" * 1024 # exactly the threshold → large pathcdp = _StreamCDP(chunks=[payload])with _stream_limits(threshold=1024):@@ -2425,9 +3005,53 @@ class TestTwoPathDownloadStreaming:assert _fulfill_headers(cdp).get("content-length") == "0"assert cdp.count("Fetch.failRequest") == 0+ @pytest.mark.asyncio+ async def test_streamed_download_rejects_symlinked_output_directory(self, tmp_path: Path) -> None:+ outside = tmp_path / "outside"+ outside.mkdir()+ output_dir = tmp_path / "downloads"+ output_dir.symlink_to(outside, target_is_directory=True)+ interceptor = _make_interceptor(output_dir=str(output_dir))+ cdp = _StreamCDP(chunks=[b"B" * 1024])++ with _stream_limits(threshold=1024):+ await _drive_download(interceptor, cdp, _raw_headers(content_length=1024))++ assert list(outside.iterdir()) == []+ assert cdp.count("Fetch.failRequest") == 1+ assert cdp.count("Fetch.fulfillRequest") == 0++ @pytest.mark.asyncio+ async def test_streamed_download_destination_swap_fails_at_publication(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))+ outside = tmp_path.parent / f"{tmp_path.name}-stream-outside.txt"+ outside.write_bytes(b"outside")+ cdp = _StreamCDP(chunks=[b"B" * 1024])+ real_link = mod.os.link+ swapped = False++ def swap_destination_then_link(src: str, dst: str, **kwargs: Any) -> None:+ nonlocal swapped+ if dst == "doc.pdf":+ (tmp_path / dst).symlink_to(outside)+ swapped = True+ real_link(src, dst, **kwargs)++ with (+ _stream_limits(threshold=1024),+ patch.object(mod.os, "link", side_effect=swap_destination_then_link),+ ):+ await _drive_download(interceptor, cdp, _raw_headers(content_length=1024))++ assert swapped+ assert (tmp_path / "doc.pdf").is_symlink()+ assert outside.read_bytes() == b"outside"+ assert cdp.count("Fetch.failRequest") == 1+ assert cdp.count("Fetch.fulfillRequest") == 0+@pytest.mark.asyncioasync def test_above_threshold_known_large_streams(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))payload = b"C" * 2000cdp = _StreamCDP(chunks=[b"C" * 800, b"C" * 800, b"C" * 400])with _stream_limits(threshold=1024, cap=4096):@@ -2438,7 +3062,7 @@ class TestTwoPathDownloadStreaming:@pytest.mark.asyncioasync def test_unknown_content_length_spills_to_disk(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))# No Content-Length: must not assume small. Buffers, then spills once bytes cross the threshold.chunks = [b"D" * 400, b"D" * 400, b"D" * 400, b"D" * 400, b"D" * 400] # 2000 totalcdp = _StreamCDP(chunks=list(chunks))@@ -2450,7 +3074,7 @@ class TestTwoPathDownloadStreaming:@pytest.mark.asyncioasync def test_lying_small_content_length_spills(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))# Header claims 100 bytes but the body is 2000 → must spill to the large path, not trust the header.cdp = _StreamCDP(chunks=[b"E" * 700, b"E" * 700, b"E" * 600])with _stream_limits(threshold=1024, cap=4096):@@ -2461,7 +3085,7 @@ class TestTwoPathDownloadStreaming:@pytest.mark.asyncioasync def test_lying_large_content_length_tiny_body_finalizes(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))# Header claims large (>= threshold) → disk path from chunk 1, but the actual body is tiny.cdp = _StreamCDP(chunks=[b"F" * 300])with _stream_limits(threshold=1024, cap=4096):@@ -2473,7 +3097,7 @@ class TestTwoPathDownloadStreaming:@pytest.mark.asyncioasync def test_cap_breach_unknown_cl_aborts_no_artifact(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(chunks=[b"G" * 1500, b"G" * 1500, b"G" * 1500, b"G" * 1500]) # 6000 > cap 4096with _stream_limits(threshold=1024, cap=4096):await _drive_download(interceptor, cdp, _raw_headers(content_length=None))@@ -2484,7 +3108,7 @@ class TestTwoPathDownloadStreaming:@pytest.mark.asyncioasync def test_header_content_length_over_cap_fast_fails(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(chunks=[b"H" * 10])with _stream_limits(threshold=1024, cap=4096):await _drive_download(interceptor, cdp, _raw_headers(content_length=5000)) # > cap@@ -2499,7 +3123,7 @@ class TestTwoPathDownloadStreaming:# Browser.downloadWillBegin lets _handle_browser_download re-fetch it via _download_url_directly# (which reads the whole body before its 100 MiB check) and materializes the very payload the cap# exists to block (SKY-12642).- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(chunks=[b"H" * 10])url = "https://example.com/huge.pdf"with _stream_limits(threshold=1024, cap=4096):@@ -2510,7 +3134,7 @@ class TestTwoPathDownloadStreaming:@pytest.mark.asyncioasync def test_midstream_read_error_cleans_temp_and_fails(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))# Known-large → disk path; the second IO.read raises after one chunk is on disk.cdp = _StreamCDP(chunks=[b"I" * 800, b"I" * 800, b"I" * 800],@@ -2526,7 +3150,7 @@ class TestTwoPathDownloadStreaming:@pytest.mark.asyncioasync def test_cancelled_error_cleans_temp_and_propagates(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(chunks=[b"J" * 800, b"J" * 800, b"J" * 800],read_error=asyncio.CancelledError(),@@ -2541,7 +3165,7 @@ class TestTwoPathDownloadStreaming:@pytest.mark.asyncioasync def test_streamed_empty_fulfill_normalizes_headers(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(chunks=[b"K" * 2000])raw = _raw_headers(content_length=2000,@@ -2566,7 +3190,7 @@ class TestTwoPathDownloadStreaming:async def test_stream_start_fail_fails_request_never_falls_back_to_whole_body(self, tmp_path: Path) -> None:# takeResponseBodyAsStream failing must fail the request — we never fall back to getResponseBody,# whose whole-body materialization could OOM on a lying/understated Content-Length (SKY-12642).- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(take_error=RuntimeError("takeResponseBodyAsStream unavailable"), getbody=b"L" * 300)with _stream_limits(threshold=1024, cap=4096):await _drive_download(interceptor, cdp, _raw_headers(content_length=300))@@ -2578,7 +3202,7 @@ class TestTwoPathDownloadStreaming:@pytest.mark.asyncioasync def test_stream_start_fail_with_content_encoding_no_direct_fallback(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))# Encoded small CL can decode to an unbounded body → getResponseBody would materialize it. Refuse.cdp = _StreamCDP(take_error=RuntimeError("take failed"), getbody=b"L" * 300)with _stream_limits(threshold=1024, cap=4096):@@ -2592,7 +3216,7 @@ class TestTwoPathDownloadStreaming:async def test_extraction_serialized_second_download_waits_for_lock(self, tmp_path: Path) -> None:# Single-active guard: while one extraction holds the lock, a second _handle_download must block# (not stream concurrently), so N parallel downloads cannot each write up to the cap to disk.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(chunks=[b"A" * 200])await interceptor._download_extraction_lock.acquire()task = asyncio.create_task(_drive_download(interceptor, cdp, _raw_headers(content_length=200)))@@ -2610,7 +3234,7 @@ class TestTwoPathDownloadStreaming:async def test_extraction_lock_released_on_success_error_and_start_failure(self, tmp_path: Path) -> None:# The single-active lock must be released on every exit path, or one failed download deadlocks all# later downloads on this interceptor.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))await _drive_download(interceptor, _StreamCDP(chunks=[b"S" * 100]), _raw_headers(content_length=100))assert not interceptor._download_extraction_lock.locked() # successwith _stream_limits(threshold=1024, cap=4096):@@ -2625,7 +3249,7 @@ class TestTwoPathDownloadStreaming:# not yet classified as a download). settle drains those to quiescence, so a capture queued behind# the lock — which owns no .crdownload yet — is still waited on before artifact collection instead# of being silently omitted.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(chunks=[b"Q" * 150])await interceptor._download_extraction_lock.acquire() # hold the lock so the capture queuesinterceptor._schedule_cdp_handler(_drive_download(interceptor, cdp, _raw_headers(content_length=150)))@@ -2643,7 +3267,7 @@ class TestTwoPathDownloadStreaming:async def test_stalled_close_times_out_and_releases_lock(self, tmp_path: Path) -> None:# The IO.close cleanup is bounded too: a hung close on a dead session must not hold the extraction# lock or hang teardown. The streamed file is already published, so the download still finalizes.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(chunks=[b"C" * 2000], close_hang=True) # IO.close never returnswith _stream_limits(threshold=1024, cap=4096), patch.object(mod, "STREAM_IO_READ_TIMEOUT_SECONDS", 0.05):await asyncio.wait_for(_drive_download(interceptor, cdp, _raw_headers(content_length=2000)), timeout=5)@@ -2654,7 +3278,7 @@ class TestTwoPathDownloadStreaming:async def test_settle_cancellation_does_not_cancel_capture(self, tmp_path: Path) -> None:# A settle-timeout cancellation must NOT cancel the in-flight/queued capture handlers (gather would,# destroying the temp mid-stream); the capture keeps running and its artifact still lands.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(chunks=[b"K" * 150])await interceptor._download_extraction_lock.acquire() # hold the lock so the capture queuesinterceptor._schedule_cdp_handler(_drive_download(interceptor, cdp, _raw_headers(content_length=150)))@@ -2674,7 +3298,7 @@ class TestTwoPathDownloadStreaming:async def test_stalled_take_stream_times_out_and_releases_lock(self, tmp_path: Path) -> None:# takeResponseBodyAsStream on a dead session is bounded too: on timeout it becomes _StreamStartError# → failRequest, so it can't hold the lock or hang settle's drain.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(take_hang=True) # takeResponseBodyAsStream never returnswith _stream_limits(threshold=1024, cap=4096), patch.object(mod, "STREAM_IO_READ_TIMEOUT_SECONDS", 0.05):await asyncio.wait_for(_drive_download(interceptor, cdp, _raw_headers(content_length=300)), timeout=5)@@ -2686,7 +3310,7 @@ class TestTwoPathDownloadStreaming:async def test_stalled_fulfill_times_out_and_releases_lock(self, tmp_path: Path) -> None:# A hung fulfillRequest (after the streamed file is already published) is bounded: the artifact is# saved, the download still finalizes, and the lock is released.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(chunks=[b"F" * 2000], fulfill_hang=True) # fulfillRequest never returnswith _stream_limits(threshold=1024, cap=4096), patch.object(mod, "STREAM_IO_READ_TIMEOUT_SECONDS", 0.05):await asyncio.wait_for(_drive_download(interceptor, cdp, _raw_headers(content_length=2000)), timeout=5)@@ -2697,7 +3321,7 @@ class TestTwoPathDownloadStreaming:async def test_stalled_read_times_out_and_releases_lock(self, tmp_path: Path) -> None:# A stalled IO.read must not hold the extraction lock forever (which would deadlock later captures# and hang teardown's drain). The per-read timeout aborts it, cleans the temp, and releases the lock.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(read_hang=True) # IO.read never returnswith _stream_limits(threshold=1024, cap=4096), patch.object(mod, "STREAM_IO_READ_TIMEOUT_SECONDS", 0.05):# Outer bound so a regression (no per-read timeout) fails fast instead of hanging the suite.@@ -2708,7 +3332,7 @@ class TestTwoPathDownloadStreaming:@pytest.mark.asyncioasync def test_stream_start_fail_unknown_cl_no_direct_fallback(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(take_error=RuntimeError("take failed"), getbody=b"M" * 300)with _stream_limits(threshold=1024, cap=4096):await _drive_download(interceptor, cdp, _raw_headers(content_length=None))@@ -2719,11 +3343,11 @@ class TestTwoPathDownloadStreaming:assert cdp.count("Fetch.failRequest") == 1@pytest.mark.asyncio- async def test_os_replace_failure_cleans_temp_and_fails(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ async def test_publication_failure_cleans_temp_and_fails(self, tmp_path: Path) -> None:+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(chunks=[b"N" * 2000])with _stream_limits(threshold=1024, cap=4096):- with patch.object(mod.os, "replace", side_effect=OSError("rename failed")):+ with patch.object(mod.os, "link", side_effect=OSError("publication failed")):await _drive_download(interceptor, cdp, _raw_headers(content_length=2000))assert list(tmp_path.iterdir()) == [] # temp cleaned even when finalize fails@@ -2732,7 +3356,7 @@ class TestTwoPathDownloadStreaming:@pytest.mark.asyncioasync def test_read_data_and_eof_in_same_message_not_dropped(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))# Single IO.read returns the whole body AND eof in one message; the last chunk must not be lost.cdp = _StreamCDP(chunks=[b"O" * 900])with _stream_limits(threshold=1024, cap=4096):@@ -2743,7 +3367,7 @@ class TestTwoPathDownloadStreaming:@pytest.mark.asyncioasync def test_zero_byte_download_saved_and_replayed(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))cdp = _StreamCDP(chunks=[]) # immediate eof, no datawith _stream_limits(threshold=1024, cap=4096):await _drive_download(interceptor, cdp, _raw_headers(content_length=0))@@ -2754,7 +3378,7 @@ class TestTwoPathDownloadStreaming:@pytest.mark.asyncioasync def test_stale_interception_on_fulfill_is_benign_after_save(self, tmp_path: Path) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))+ interceptor = _make_interceptor(output_dir=str(tmp_path))stale = Exception("Protocol error (Fetch.fulfillRequest): Invalid InterceptionId")cdp = _StreamCDP(chunks=[b"P" * 2000], fulfill_error=stale)with _stream_limits(threshold=1024, cap=4096):@@ -2763,136 +3387,3 @@ class TestTwoPathDownloadStreaming:assert _only_file(tmp_path).read_bytes() == b"P" * 2000 # file saved despite the benign fulfill raceassert not [log for log in logs if log.get("log_level") == "error"]---class TestDownloadDestinationValidation:- """A download URL is chosen by the page, so it must be checked against the destination rules- before the worker fetches it — on the initial URL and on every redirect hop, and on both the- Playwright and urllib transports."""-- @staticmethod- def _context(request_context: object | None = None, cookies: list[dict] | None = None) -> MagicMock:- ctx = MagicMock()- if request_context is None:- # Non-OK forces _download_url_directly onto its urllib transport.- non_ok = MagicMock()- non_ok.ok = False- non_ok.status = 407- ctx.request.get = AsyncMock(return_value=non_ok)- else:- ctx.request = request_context- ctx.cookies = AsyncMock(return_value=cookies or [])- return ctx-- @pytest.mark.asyncio- async def test_playwright_transport_refuses_internal_destination(- self, tmp_path: Path, download_destinations: Any, fake_api_request_context: Any- ) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context(fake_api_request_context())-- await interceptor._download_url_directly(f"{download_destinations.internal_base}/internal", "statement.pdf")-- assert download_destinations.reached_internal() is False- assert list(tmp_path.iterdir()) == []-- @pytest.mark.asyncio- async def test_playwright_transport_refuses_redirect_hop_to_internal_destination(- self, tmp_path: Path, download_destinations: Any, fake_api_request_context: Any- ) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context(fake_api_request_context())-- await interceptor._download_url_directly(- f"{download_destinations.public_base}/redirect-to-internal", "statement.pdf"- )-- assert download_destinations.reached_internal() is False- saved = list(tmp_path.iterdir())- assert [path.read_bytes() for path in saved] == []-- @pytest.mark.asyncio- async def test_urllib_transport_refuses_internal_destination(- self, tmp_path: Path, download_destinations: Any- ) -> None:- # No browser context, so the urllib transport is entered directly and owns the initial- # destination check itself rather than inheriting one from the Playwright transport.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = None-- await interceptor._download_url_directly(f"{download_destinations.internal_base}/internal", "statement.pdf")-- assert download_destinations.reached_internal() is False- assert list(tmp_path.iterdir()) == []-- @pytest.mark.asyncio- async def test_urllib_transport_refuses_redirect_hop_to_internal_destination(- self, tmp_path: Path, download_destinations: Any- ) -> None:- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context(cookies=[{"name": "sess", "value": "tok"}])-- await interceptor._download_url_directly(- f"{download_destinations.public_base}/redirect-to-internal", "statement.pdf"- )-- assert download_destinations.reached_internal() is False- saved = list(tmp_path.iterdir())- assert [path.read_bytes() for path in saved] == []-- @pytest.mark.asyncio- async def test_allowed_destination_still_downloads(- self, tmp_path: Path, download_destinations: Any, fake_api_request_context: Any- ) -> None:- # Non-vacuity: the harness must be able to complete a download that is not refused,- # otherwise the refusal assertions above would pass for the wrong reason.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context(fake_api_request_context())-- await interceptor._download_url_directly(f"{download_destinations.public_base}/attachment", "statement.pdf")-- saved = list(tmp_path.iterdir())- assert [path.name for path in saved] == ["statement.pdf"]- assert saved[0].read_bytes() == download_destinations.PUBLIC_BODY-- @pytest.mark.asyncio- async def test_urllib_transport_drops_session_cookie_on_cross_origin_hop(- self, tmp_path: Path, download_destinations: Any- ) -> None:- # The session cookie belongs to the host the download started on. A hop to a different- # origin — permitted or not — must not replay it.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = self._context(cookies=[{"name": "sess", "value": "tok"}])-- await interceptor._download_url_directly(- f"{download_destinations.public_base}/redirect-to-other", "statement.pdf"- )-- assert download_destinations.cookies_by_path["public-host.test"] == "sess=tok"- assert download_destinations.cookies_by_path["other-host.test"] == ""- saved = list(tmp_path.iterdir())- assert [path.name for path in saved] == ["statement.pdf"]-- @pytest.mark.asyncio- async def test_urllib_transport_reads_content_type_case_insensitively(- self, tmp_path: Path, download_destinations: Any- ) -> None:- # Servers send "Content-Type"; the extension for a suffix-less download comes from it.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = None-- await interceptor._download_url_directly(f"{download_destinations.public_base}/attachment", "statement")-- assert [path.name for path in tmp_path.iterdir()] == ["statement.pdf"]-- @pytest.mark.asyncio- async def test_urllib_transport_does_not_save_an_error_response_body(- self, tmp_path: Path, download_destinations: Any- ) -> None:- # An error page is not the file. Saving its body would report a corrupt download as success.- interceptor = CDPDownloadInterceptor(output_dir=str(tmp_path))- interceptor._browser_context = None-- await interceptor._download_url_directly(f"{download_destinations.public_base}/notfound", "statement.pdf")-- assert list(tmp_path.iterdir()) == []diff --git a/tests/unit/test_cli_doctor.py b/tests/unit/test_cli_doctor.pyindex a3e3e7933..8a742e175 100644--- a/tests/unit/test_cli_doctor.py+++ b/tests/unit/test_cli_doctor.py@@ -5,6 +5,11 @@ from pathlib import Pathimport pytestfrom skyvern.cli import doctor+from skyvern.cli.credential_placeholders import (+ CREDENTIAL_PLACEHOLDERS,+ is_frontend_api_key_placeholder,+ is_placeholder_credential_value,+)def _prepare_workspace(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:@@ -68,3 +73,54 @@ def test_legacy_streamlit_fix_removes_matching_deprecated_file(tmp_path: Path, massert "deprecated compatibility file" in result.detailassert doctor._fix_legacy_streamlit_secrets() is Trueassert not legacy.exists()+++def test_credential_placeholder_set_is_stable() -> None:+ assert CREDENTIAL_PLACEHOLDERS == ("", "PLACEHOLDER", "YOUR_API_KEY")+++ ("value", "expected"),+ [+ ("", True),+ ("PLACEHOLDER", True),+ ("YOUR_API_KEY", True),+ ("__SKYVERN_API_KEY_PLACEHOLDER__", True),+ ("__VITE_API_BASE_URL_PLACEHOLDER__", True),+ ("real-value", False),+ ],+)+def test_placeholder_credential_value_classification(value: str, expected: bool) -> None:+ assert is_placeholder_credential_value(value) is expected+++def test_api_key_consistency_treats_frontend_api_key_sentinel_as_missing(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ _prepare_workspace(tmp_path, monkeypatch)+ (tmp_path / ".env").write_text('SKYVERN_API_KEY="backend-key"\n')+ (tmp_path / "skyvern-frontend" / ".env").write_text("VITE_SKYVERN_API_KEY=YOUR_API_KEY\n")++ result = doctor._check_api_key_consistency()++ assert result.status == "error"+ assert result.detail == "VITE_SKYVERN_API_KEY not set in frontend .env"+++def test_api_key_consistency_treats_frontend_api_key_SENTINEL_PLACEHOLDER_as_value_to_match(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ _prepare_workspace(tmp_path, monkeypatch)+ (tmp_path / ".env").write_text('SKYVERN_API_KEY="backend-key"\n')+ (tmp_path / "skyvern-frontend" / ".env").write_text("VITE_SKYVERN_API_KEY=PLACEHOLDER\n")++ result = doctor._check_api_key_consistency()++ assert result.status == "error"+ assert "frontend .env differs from backend" in result.detail+++def test_frontend_api_key_placeholder_only_filter_is_consistent() -> None:+ assert is_frontend_api_key_placeholder("YOUR_API_KEY")+ assert is_frontend_api_key_placeholder("")+ assert is_frontend_api_key_placeholder("PLACEHOLDER") is Falsediff --git a/tests/unit/test_code_block_steps.py b/tests/unit/test_code_block_steps.pyindex 169776d7a..9d5d7a8a0 100644--- a/tests/unit/test_code_block_steps.py+++ b/tests/unit/test_code_block_steps.py@@ -1,3 +1,5 @@+import textwrap+import pytestimport yaml@@ -8,6 +10,7 @@ from skyvern.forge.sdk.copilot.code_block_steps import (derive_code_block_steps_in_yaml,fill_code_block_prompts_in_yaml,)+from skyvern.forge.sdk.copilot.code_block_synthesis import synthesize_code_blockfrom skyvern.webeye.actions.action_types import ActionType@@ -84,6 +87,37 @@ def test_derive_steps_returns_dicts_with_templated_descriptions():]+def test_synthesized_and_derived_steps_share_exact_field_set():+ trajectory = [+ {+ "tool_name": "type_text",+ "selector": "#search",+ "source_url": "https://example.com/catalog",+ "typed_value": "widget",+ "role": "textbox",+ "accessible_name": "Search",+ },+ {+ "tool_name": "click",+ "selector": "#search-submit",+ "source_url": "https://example.com/catalog",+ "role": "button",+ "accessible_name": "Submit",+ },+ ]+ synthesized = synthesize_code_block(trajectory)+ assert synthesized is not None++ standalone_code = textwrap.dedent(synthesized.code)+ derived_steps = derive_code_block_steps(standalone_code)+ expected_fields = {"description", "action_type", "line_start", "line_end"}++ for producer_name, steps in (("synthesize", synthesized.steps), ("derive", derived_steps)):+ assert steps, f"{producer_name} must produce representative steps"+ for step in steps:+ assert set(step) == expected_fields, producer_name++def test_derive_steps_empty_code_is_empty():assert derive_code_block_steps("") == []assert derive_code_block_steps("x = 1\n") == []diff --git a/tests/unit/test_code_block_synthesize_mcp_tool.py b/tests/unit/test_code_block_synthesize_mcp_tool.pyindex 743cd65b8..d4415e692 100644--- a/tests/unit/test_code_block_synthesize_mcp_tool.py+++ b/tests/unit/test_code_block_synthesize_mcp_tool.py@@ -79,6 +79,7 @@ def _expected_synthesis_data(*, strict_selectors: bool) -> dict[str, Any]:synthesized = synthesize_code_block(_fixture_trajectory(), strict_selectors=strict_selectors)assert synthesized is not Nonereturn {+ "prompt": "","code": textwrap.dedent(synthesized.code),"parameters": synthesized.parameters,"steps": synthesized.steps,diff --git a/tests/unit/test_config.py b/tests/unit/test_config.pynew file mode 100644index 000000000..c889e1e36--- /dev/null+++ b/tests/unit/test_config.py@@ -0,0 +1,88 @@+import logging++import pytest++from skyvern.config import CodeBlockMode, Settings+++ ("legacy_setting", "legacy_value", "expected_mode"),+ [+ ("DISABLE_CODE_BLOCK_EXECUTION", True, CodeBlockMode.disabled),+ ("ENABLE_CODE_BLOCK", False, CodeBlockMode.entitlement),+ ("ENABLE_CODE_BLOCK", True, CodeBlockMode.enabled),+ ],+)+def test_legacy_code_block_settings_map_to_mode_with_deprecation_warning(+ caplog: pytest.LogCaptureFixture,+ legacy_setting: str,+ legacy_value: bool,+ expected_mode: CodeBlockMode,+) -> None:+ with caplog.at_level(logging.WARNING, logger="skyvern.config"):+ configured = Settings(_env_file=None, **{legacy_setting: legacy_value})++ assert configured.CODE_BLOCK_MODE is expected_mode+ assert legacy_setting in caplog.text+ assert str(legacy_value) in caplog.text+ assert f"CODE_BLOCK_MODE={expected_mode.value}" in caplog.text+ assert "deprecated" in caplog.text+++def test_disable_code_block_execution_false_emits_deprecation_warning(+ caplog: pytest.LogCaptureFixture,+) -> None:+ with caplog.at_level(logging.WARNING, logger="skyvern.config"):+ configured = Settings(_env_file=None, DISABLE_CODE_BLOCK_EXECUTION=False)++ assert configured.CODE_BLOCK_MODE is CodeBlockMode.enabled+ assert "DISABLE_CODE_BLOCK_EXECUTION=False" in caplog.text+ assert "CODE_BLOCK_MODE=enabled" in caplog.text+ assert "deprecated" in caplog.text+++def test_legacy_code_block_setting_warns_when_another_legacy_setting_wins(+ caplog: pytest.LogCaptureFixture,+) -> None:+ with caplog.at_level(logging.WARNING, logger="skyvern.config"):+ configured = Settings(+ _env_file=None,+ DISABLE_CODE_BLOCK_EXECUTION=True,+ ENABLE_CODE_BLOCK=False,+ )++ assert configured.CODE_BLOCK_MODE is CodeBlockMode.disabled+ deprecation_messages = [record.getMessage() for record in caplog.records if "deprecated" in record.getMessage()]+ assert len(deprecation_messages) == 2+ assert any("DISABLE_CODE_BLOCK_EXECUTION=True" in message for message in deprecation_messages)+ assert any("ENABLE_CODE_BLOCK=False" in message for message in deprecation_messages)+ assert all("CODE_BLOCK_MODE=disabled" in message for message in deprecation_messages)+++def test_code_block_mode_defaults_to_enabled_without_deprecation_warning(+ caplog: pytest.LogCaptureFixture,+) -> None:+ with caplog.at_level(logging.WARNING, logger="skyvern.config"):+ configured = Settings(_env_file=None)++ assert configured.CODE_BLOCK_MODE is CodeBlockMode.enabled+ assert "ENABLE_CODE_BLOCK" not in caplog.text+ assert "DISABLE_CODE_BLOCK_EXECUTION" not in caplog.text+++def test_explicit_code_block_mode_ignores_legacy_setting_with_warning(+ caplog: pytest.LogCaptureFixture,+) -> None:+ with caplog.at_level(logging.WARNING, logger="skyvern.config"):+ configured = Settings(+ _env_file=None,+ CODE_BLOCK_MODE=CodeBlockMode.disabled,+ ENABLE_CODE_BLOCK=True,+ )++ assert configured.CODE_BLOCK_MODE is CodeBlockMode.disabled+ assert "ENABLE_CODE_BLOCK" in caplog.text+ assert "True" in caplog.text+ assert "CODE_BLOCK_MODE=disabled" in caplog.text+ assert "deprecated" in caplog.text+ assert "ignored" in caplog.textdiff --git a/tests/unit/test_copilot_author_time_block.py b/tests/unit/test_copilot_author_time_block.pyindex c43e4371a..b230fbcbe 100644--- a/tests/unit/test_copilot_author_time_block.py+++ b/tests/unit/test_copilot_author_time_block.py@@ -242,15 +242,15 @@ async def test_banned_block_type_still_blocks(monkeypatch: pytest.MonkeyPatch) -@pytest.mark.asyncio-async def test_credential_scout_gate_still_blocks(monkeypatch: pytest.MonkeyPatch) -> None:+async def test_credential_reference_validation_still_blocks(monkeypatch: pytest.MonkeyPatch) -> None:_stub_persist(monkeypatch)ctx = _ctx()ctx.scout_trajectory = []- monkeypatch.setattr(- workflow_update_module,- "_credentialed_code_block_scout_gate_errors",- lambda *_a, **_k: ["Scout the login form before authoring a credential fill."],- )++ async def _unresolved(*_a: object, **_k: object) -> str:+ return "Credential `cred_missing` was not found in this organization."++ monkeypatch.setattr(workflow_update_module, "_credential_reference_validation_error", _unresolved)result = await _update_workflow({"workflow_yaml": _CREDENTIAL_LOGIN_YAML}, ctx)diff --git a/tests/unit/test_copilot_code_block_persist_seam.py b/tests/unit/test_copilot_code_block_persist_seam.pyindex 2d48d0e8a..b9ba6db5d 100644--- a/tests/unit/test_copilot_code_block_persist_seam.py+++ b/tests/unit/test_copilot_code_block_persist_seam.py@@ -26,6 +26,7 @@ from skyvern.forge.sdk.copilot import code_block_synthesis as code_block_synthesfrom skyvern.forge.sdk.copilot import enforcement as enforcement_modulefrom skyvern.forge.sdk.copilot import request_policy as request_policy_modulefrom skyvern.forge.sdk.copilot import tools as tools_module+from skyvern.forge.sdk.copilot.author_time_block import CODE_SAFETY_BLOCK_IDfrom skyvern.forge.sdk.copilot.authoring_parameter_binding import (AuthoringParameterBindingCandidate,build_authoring_parameter_binding_directive,@@ -5151,7 +5152,7 @@ class TestCodeRepairProgressClassification:assert ctx.last_code_authoring_repair_context is None@pytest.mark.asyncio- async def test_credential_scout_reject_is_not_classified_as_progress(self) -> None:+ async def test_credential_reference_reject_is_not_classified_as_progress(self) -> None:ctx = _code_only_ctx()ctx.scout_trajectory = []result = await _update_workflow(@@ -5169,8 +5170,8 @@ class TestCodeRepairProgressClassification:ctx,)assert result["ok"] is False- assert result["data"]["failure_type"] == "missing_credential_or_init"- assert result["data"].get("surface_kind") is None+ assert "was not found in this organization" in result["error"]+ assert (result.get("data") or {}).get("surface_kind") is None_SPINE_SYNTH_CODE = 'await page.locator("#stage-a").click()\nawait page.locator("#stage-b").click()'@@ -7249,7 +7250,7 @@ class TestCredentialScoutPersistGate:)@pytest.mark.asyncio- async def test_rejects_credential_submit_code_without_matching_fill_scouts(self) -> None:+ async def test_credential_submit_code_needs_no_fill_scouts_before_reference_validation(self) -> None:ctx = _code_only_ctx()ctx.scout_trajectory = []ctx.last_code_authoring_repair_context = _stale_unresolved_repair_context()@@ -7257,33 +7258,25 @@ class TestCredentialScoutPersistGate:result = await _update_workflow({"workflow_yaml": self._SUBMIT_CODE_YAML}, ctx)assert result["ok"] is False- assert "fill_credential_field" in result["error"]- assert "click the submit control or press Enter" in result["error"]- assert "authoring_repair_context" not in result["data"]+ error_text = str(result.get("error") or "")+ assert "fill_credential_field" not in error_text+ assert "was not found in this organization" in error_textassert ctx.last_code_authoring_repair_context is None- assert result["user_facing_summary"] == (- "I need to verify the saved-credential login in the browser before I can save or run this code."- )@pytest.mark.asyncio- async def test_credential_scout_blocker_takes_precedence_over_code_safety(self) -> None:+ async def test_code_safety_reject_surfaces_for_unscouted_credential_code(self) -> None:ctx = _code_only_ctx()ctx.scout_trajectory = []result = await _update_workflow({"workflow_yaml": self._UNSAFE_SUBMIT_CODE_YAML}, ctx)assert result["ok"] is False- assert "fill_credential_field" in result["error"]- assert "Insecure code detected" not in result["error"]- assert result["user_facing_summary"] == (- "I need to verify the saved-credential login in the browser before I can save or run this code."- )- assert result["data"]["failure_type"] == "missing_credential_or_init"- code_safety_diagnostics = result["data"]["diagnostic_code_safety_errors"]- assert any("private methods or attributes" in error for error in code_safety_diagnostics)+ assert result["block_id"] == CODE_SAFETY_BLOCK_ID+ assert "fill_credential_field" not in result["error"]+ assert result["user_facing_summary"] != CREDENTIAL_SCOUT_VERIFY_REPLY@pytest.mark.asyncio- async def test_allows_submit_code_gate_once_matching_fills_and_submit_are_scouted(self) -> None:+ async def test_scouted_submit_code_reaches_reference_validation(self) -> None:ctx = _code_only_ctx()ctx.scout_trajectory = [_credential_fill_interaction("username"),@@ -7301,7 +7294,7 @@ class TestCredentialScoutPersistGate:assert "saved-credential login flow" not in error_text@pytest.mark.asyncio- async def test_submit_code_still_requires_later_submit_after_matching_fills(self) -> None:+ async def test_submit_code_needs_no_scouted_submit_before_reference_validation(self) -> None:ctx = _code_only_ctx()ctx.scout_trajectory = [_credential_fill_interaction("username"),@@ -7312,11 +7305,12 @@ class TestCredentialScoutPersistGate:result = await _update_workflow({"workflow_yaml": self._SUBMIT_CODE_YAML}, ctx)assert result["ok"] is False- assert "later submit action on the same page" in result["error"]- assert "click the submit control or press Enter" in result["error"]+ error_text = str(result.get("error") or "")+ assert "later submit action on the same page" not in error_text+ assert "was not found in this organization" in error_text@pytest.mark.asyncio- async def test_fill_only_code_requires_matching_fill_scouts_but_not_submit(self) -> None:+ async def test_fill_only_code_reaches_reference_validation(self) -> None:ctx = _code_only_ctx()ctx.scout_trajectory = [_credential_fill_interaction("username"),@@ -7333,7 +7327,7 @@ class TestCredentialScoutPersistGate:assert "saved-credential login flow" not in error_text@pytest.mark.asyncio- async def test_runtime_otp_method_does_not_require_impossible_live_otp_fill(self) -> None:+ async def test_runtime_otp_code_reaches_reference_validation(self) -> None:ctx = _code_only_ctx()ctx.scout_trajectory = [_credential_fill_interaction("username"),@@ -7375,137 +7369,6 @@ class TestCredentialScoutPersistGate:assert "fill_credential_field" not in error_textassert "saved-credential login flow" not in error_text- @pytest.mark.asyncio- async def test_targeted_run_labels_scope_credential_scout_gate(self, monkeypatch: pytest.MonkeyPatch) -> None:- _stub_successful_update(monkeypatch)- ctx = _code_only_ctx()- ctx.scout_trajectory = []-- accepted = await _update_workflow(- {- "workflow_yaml": self._MULTI_BLOCK_TARGETED_CREDENTIAL_YAML,- "block_labels": ["open_matching_statement"],- },- ctx,- allow_missing_credentials=True,- )-- assert accepted["ok"] is True-- selected_credential_ctx = _code_only_ctx()- selected_credential_ctx.scout_trajectory = []- rejected = await _update_workflow(- {- "workflow_yaml": self._MULTI_BLOCK_TARGETED_CREDENTIAL_YAML,- "block_labels": ["sign_in_to_business_center"],- },- selected_credential_ctx,- )-- assert rejected["ok"] is False- assert rejected["data"]["failure_type"] == "missing_credential_or_init"- assert "sign_in_to_business_center" in rejected["error"]- assert rejected["user_facing_summary"] == CREDENTIAL_SCOUT_VERIFY_REPLY-- @pytest.mark.asyncio- async def test_persisted_parameter_shape_does_not_rescout_unchanged_selected_credential_block(self) -> None:- sign_in_code = """- await page.locator("#email").fill(login_credential.username)- await page.locator("input[type='password']").fill(login_credential.password)- await page.locator("#sign-in").click()- """- sign_in_code = textwrap.dedent(sign_in_code).strip()- login_parameter = {- "parameter_type": "workflow",- "workflow_parameter_type": "credential_id",- "key": "login_credential",- "default_value": "cred_missing",- }- prior_yaml = yaml.safe_dump(- {- "title": "Saved credential login",- "workflow_definition": {- "parameters": [login_parameter],- "blocks": [- {- "block_type": "code",- "label": "sign_in_to_business_center",- "parameters": [login_parameter],- "code": sign_in_code,- }- ],- },- },- sort_keys=False,- )- submitted_yaml = yaml.safe_dump(- {- "title": "Saved credential login",- "workflow_definition": {- "parameters": [- login_parameter,- {- "parameter_type": "workflow",- "workflow_parameter_type": "string",- "key": "account_number",- "default_value": "100245",- },- ],- "blocks": [- {- "block_type": "code",- "label": "sign_in_to_business_center",- "parameter_keys": ["login_credential"],- "code": sign_in_code,- },- {- "block_type": "code",- "label": "open_matching_statement",- "parameter_keys": ["account_number"],- "code": 'await page.get_by_text("View Printable Statement").wait_for(timeout=5000)',- },- ],- },- },- sort_keys=False,- )- ctx = _code_only_ctx()- ctx.workflow_yaml = prior_yaml- ctx.scout_trajectory = []-- result = await _update_workflow(- {- "workflow_yaml": submitted_yaml,- "block_labels": ["sign_in_to_business_center", "open_matching_statement"],- },- ctx,- )-- assert result["ok"] is False- error_text = str(result.get("error") or "")- assert "open_matching_statement" in error_text- assert "fill_credential_field" not in error_text-- changed_yaml = submitted_yaml.replace(- 'await page.locator("#sign-in").click()',- 'await page.locator("#sign-in").click()\n await page.locator("#post-login").click()',- )- changed_ctx = _code_only_ctx()- changed_ctx.workflow_yaml = prior_yaml- changed_ctx.scout_trajectory = []-- changed_result = await _update_workflow(- {- "workflow_yaml": changed_yaml,- "block_labels": ["sign_in_to_business_center"],- },- changed_ctx,- )-- assert changed_result["ok"] is False- assert changed_result["data"]["failure_type"] == "missing_credential_or_init"- assert changed_result["user_facing_summary"] == CREDENTIAL_SCOUT_VERIFY_REPLY-def test_run_id_leak_check_covers_non_numeric_ids() -> None:with pytest.raises(ValueError):@@ -7692,7 +7555,7 @@ def _terminal_challenge_signal() -> CopilotToolBlockerSignal:class TestCodeAuthoringGuardrailChurnBackstop:@pytest.mark.asyncio- async def test_counter_climbs_through_credential_scout_branch(self) -> None:+ async def test_counter_climbs_for_unsafe_credential_code(self) -> None:ctx = _code_only_ctx()ctx.scout_trajectory = []unsafe_credential_yaml = _credential_code_yaml(@@ -7707,7 +7570,7 @@ class TestCodeAuthoringGuardrailChurnBackstop:result = await _update_workflow({"workflow_yaml": unsafe_credential_yaml}, ctx)assert result["ok"] is False- assert result["data"]["failure_type"] == "missing_credential_or_init"+ assert result["block_id"] == CODE_SAFETY_BLOCK_IDassert ctx.code_authoring_guardrail_reject_count == 1@pytest.mark.asyncio@@ -7760,31 +7623,16 @@ class TestCodeAuthoringGuardrailChurnBackstop:) in info_calls@pytest.mark.asyncio- async def test_single_credential_priority_reject_defers_to_credential_scout_reply(self) -> None:- ctx = _code_only_ctx()- ctx.scout_trajectory = []-- result = await _update_workflow({"workflow_yaml": _distinct_credential_collision_yaml(1)}, ctx)-- assert ctx.code_authoring_guardrail_reject_count == 1- assert result["ok"] is False- assert result["data"]["failure_type"] == "missing_credential_or_init"- assert result["user_facing_summary"] == CREDENTIAL_SCOUT_VERIFY_REPLY- assert ctx.blocker_signal is None- assert ctx.latest_tool_blocker_signal is None-- @pytest.mark.asyncio- async def test_mixed_credential_and_non_name_guardrail_uses_credential_priority_path(self) -> None:+ async def test_mixed_credential_and_non_name_guardrail_uses_code_safety_path(self) -> None:ctx = _code_only_ctx()ctx.scout_trajectory = []result = await _update_workflow({"workflow_yaml": _distinct_credential_collision_yaml(1)}, ctx)assert result["ok"] is False- assert result["data"]["failure_type"] == "missing_credential_or_init"- assert result["user_facing_summary"] == CREDENTIAL_SCOUT_VERIFY_REPLY+ assert result["block_id"] == CODE_SAFETY_BLOCK_IDassert ctx.last_code_authoring_repair_context is None- assert ctx.last_code_authoring_reject_was_credential_priority is True+ assert ctx.last_code_authoring_reject_was_credential_priority is False@pytest.mark.asyncioasync def test_standard_policy_mixed_credential_and_unresolved_name_omits_repair_context(self) -> None:@@ -7798,7 +7646,7 @@ class TestCodeAuthoringGuardrailChurnBackstop:assert ctx.last_code_authoring_repair_context is None@pytest.mark.asyncio- async def test_single_pure_credential_reject_defers_to_credential_scout_reply(self) -> None:+ async def test_single_pure_credential_reject_still_routes_credential_priority(self) -> None:ctx = _code_only_ctx()ctx.scout_trajectory = []@@ -7806,9 +7654,8 @@ class TestCodeAuthoringGuardrailChurnBackstop:assert ctx.code_authoring_guardrail_reject_count == 1assert result["ok"] is False- assert result["data"]["failure_type"] == "missing_credential_or_init"- assert "diagnostic_code_safety_errors" not in result["data"]- assert result["user_facing_summary"] == CREDENTIAL_SCOUT_VERIFY_REPLY+ assert "was not found in this organization" in result["error"]+ assert ctx.last_code_authoring_reject_was_credential_priority is Trueassert ctx.blocker_signal is Noneassert ctx.latest_tool_blocker_signal is None@@ -7829,16 +7676,19 @@ class TestCodeAuthoringGuardrailChurnBackstop:assert ctx.code_authoring_guardrail_reject_count == 0@pytest.mark.asyncio- async def test_changed_persisted_credential_block_still_requires_new_scout(self) -> None:+ async def test_changed_credential_block_needs_no_new_scout(self, monkeypatch: pytest.MonkeyPatch) -> None:+ _stub_successful_update(monkeypatch)ctx = _code_only_ctx()ctx.workflow_yaml = _safe_credential_collision_yaml(0)ctx.scout_trajectory = []- result = await _update_workflow({"workflow_yaml": _safe_credential_collision_yaml(1)}, ctx)+ result = await _update_workflow(+ {"workflow_yaml": _safe_credential_collision_yaml(1)}, ctx, allow_missing_credentials=True+ )- assert result["ok"] is False- assert result["data"]["failure_type"] == "missing_credential_or_init"- assert result["user_facing_summary"] == CREDENTIAL_SCOUT_VERIFY_REPLY+ assert result["ok"] is True+ assert result.get("user_facing_summary") != CREDENTIAL_SCOUT_VERIFY_REPLY+ assert ctx.code_authoring_guardrail_reject_count == 0_RESALE_URL = "https://example.com/orders"@@ -10690,21 +10540,6 @@ class TestScoutedSpinePersistSeamCoverage:assert len(events) == 1assert events[0]["site"] == "separated_split"- @pytest.mark.asyncio- async def test_credential_scout_reject_carries_open_obligation_artifact(self) -> None:- ctx = _credential_spine_ctx()- synthesized = workflow_update_module.synthesize_code_block(ctx.scout_trajectory, strict_selectors=True)- assert synthesized is not None-- result = await _update_workflow({"workflow_yaml": _credential_spine_block_yaml(synthesized)}, ctx)-- assert result["ok"] is False- assert result["data"]["failure_type"] == "missing_credential_or_init"- assert "later submit action on the same page" in result["error"]- assert "The persisted draft is missing scouted rung(s)." in result["error"]- assert "Missing rung source to reuse verbatim" in result["error"]- assert ".password" in result["error"]-class TestPartitionAwareImpositionFastPath:def _synthesize(self, trajectory: list[dict[str, object]]) -> SynthesizedCodeBlock:@@ -10952,51 +10787,6 @@ class TestCredentialScoutGapMatcher:assert gap == ScoutGap(missing_fields=[], missing_submit=False)-class TestCredentialScoutGatePredicateCoherence:- _TWO_FIELD_LOGIN_YAML = _credential_code_yaml(- code="""- await page.locator("#user").fill(login_credential.username)- await page.locator("#pass").fill(login_credential.password)- await page.locator("button[type='submit']").click()- """,- credential_id="cred_1",- )-- @staticmethod- def _trajectory(*steps: dict[str, object]) -> list[dict[str, object]]:- return list(steps)-- def _gate_ctx(self, trajectory: list[dict[str, object]]) -> CopilotContext:- ctx = _code_only_ctx()- ctx.scout_trajectory = trajectory- ctx.scouted_credential_field_inventory_by_credential_id = {"cred_1": frozenset({"username", "password"})}- return ctx-- def test_predicate_complete_trajectory_passes_the_real_gate(self) -> None:- helper = TestCredentialScoutGapMatcher- trajectory = self._trajectory(- helper._fill("cred_1", "username", helper._PAGE_ONE),- helper._click(helper._PAGE_ONE),- helper._fill("cred_1", "password", helper._PAGE_TWO),- helper._click(helper._PAGE_TWO),- )- ctx = self._gate_ctx(trajectory)- assert enforcement_module.synthesized_trajectory_is_goal_complete(ctx) is True- assert workflow_update_module._credentialed_code_block_scout_gate_errors(self._TWO_FIELD_LOGIN_YAML, ctx) == []-- def test_predicate_incomplete_half_login_is_also_gate_rejected(self) -> None:- helper = TestCredentialScoutGapMatcher- trajectory = self._trajectory(- helper._fill("cred_1", "username", helper._PAGE_ONE),- helper._click(helper._PAGE_ONE),- )- ctx = self._gate_ctx(trajectory)- assert enforcement_module.synthesized_trajectory_is_goal_complete(ctx) is False- errors = workflow_update_module._credentialed_code_block_scout_gate_errors(self._TWO_FIELD_LOGIN_YAML, ctx)- assert errors- assert "password" in errors[0]--class TestTerminalActionScoutGate:_BUSINESS_URL = "https://portal.example.test/business/start-service"@@ -11260,91 +11050,6 @@ class TestTerminalActionScoutGate:assert enforcement_module.synthesized_trajectory_is_goal_complete(ctx) is False-class TestCredentialScoutReopenSeam:- @pytest.mark.asyncio- async def test_pure_credential_reject_arms_then_same_identity_does_not_re_arm(self) -> None:- ctx = _code_only_ctx()- ctx.scout_trajectory = []- yaml_text = TestCredentialScoutPersistGate._SUBMIT_CODE_YAML-- result = await _update_workflow({"workflow_yaml": yaml_text}, ctx)-- assert result["ok"] is False- assert result["data"]["failure_type"] == "missing_credential_or_init"- assert ctx.synthesized_block_reopened_for_credential_scout is True- first_key = ctx.credential_scout_rescout_context_key- assert first_key-- result = await _update_workflow({"workflow_yaml": yaml_text}, ctx)-- assert result["ok"] is False- assert ctx.synthesized_block_reopened_for_credential_scout is False- assert ctx.credential_scout_rescout_context_key == first_key-- @pytest.mark.asyncio- async def test_new_binding_identity_re_arms_reopen(self) -> None:- ctx = _code_only_ctx()- ctx.scout_trajectory = []-- await _update_workflow({"workflow_yaml": TestCredentialScoutPersistGate._SUBMIT_CODE_YAML}, ctx)- first_key = ctx.credential_scout_rescout_context_key- assert ctx.synthesized_block_reopened_for_credential_scout is True-- rebound_yaml = _credential_code_yaml(- code="""- await page.locator("#email").fill(login_credential.username)- await page.locator("input[type='password']").fill(login_credential.password)- await page.locator("#totpmfa").fill(login_credential.totp)- await page.locator("input[type='submit']").click()- await page.wait_for_load_state("load")- """,- credential_id="cred_rebound",- )- result = await _update_workflow({"workflow_yaml": rebound_yaml}, ctx)-- assert result["ok"] is False- assert ctx.synthesized_block_reopened_for_credential_scout is True- assert ctx.credential_scout_rescout_context_key != first_key-- @pytest.mark.asyncio- async def test_combined_credential_and_code_safety_reject_arms_reopen(self) -> None:- ctx = _code_only_ctx()- ctx.scout_trajectory = []-- result = await _update_workflow({"workflow_yaml": TestCredentialScoutPersistGate._UNSAFE_SUBMIT_CODE_YAML}, ctx)-- assert result["ok"] is False- assert result["data"]["failure_type"] == "missing_credential_or_init"- assert ctx.synthesized_block_reopened_for_credential_scout is True- assert ctx.credential_scout_rescout_context_key-- @pytest.mark.asyncio- async def test_gate_passing_attempt_leaves_window_closed(self) -> None:- ctx = _code_only_ctx()- ctx.scout_trajectory = []-- await _update_workflow({"workflow_yaml": TestCredentialScoutPersistGate._SUBMIT_CODE_YAML}, ctx)- assert ctx.synthesized_block_reopened_for_credential_scout is True-- ctx.scout_trajectory = [- _credential_fill_interaction("username"),- _credential_fill_interaction("password"),- _credential_fill_interaction("totp"),- _submit_interaction(),- ]- result = await _update_workflow({"workflow_yaml": TestCredentialScoutPersistGate._SUBMIT_CODE_YAML}, ctx)-- assert ctx.synthesized_block_reopened_for_credential_scout is False- error_text = str(result.get("error") or "")- assert "fill_credential_field" not in error_text-- def test_should_impose_after_update_attempt_honors_reopen_flag(self) -> None:- ctx = _code_only_ctx()- assert workflow_update_module._should_impose_after_update_attempt(ctx) is False- ctx.synthesized_block_reopened_for_credential_scout = True- assert workflow_update_module._should_impose_after_update_attempt(ctx) is True--def _persisted_workflow_result() -> dict[str, object]:return {"ok": True,diff --git a/tests/unit/test_copilot_enforcement_pruning.py b/tests/unit/test_copilot_enforcement_pruning.pyindex beb1ac09e..c95797d65 100644--- a/tests/unit/test_copilot_enforcement_pruning.py+++ b/tests/unit/test_copilot_enforcement_pruning.py@@ -54,7 +54,6 @@ from skyvern.forge.sdk.copilot.enforcement import (_should_force_advisory_run_dispatch,_summarize_tool_output,aggressive_prune,- arm_credential_scout_reopen,enforcement_decision,mint_scout_observation_contract_for_ctx,pre_run_gated_outputs_without_path,@@ -135,8 +134,6 @@ class _Ctx:self.latest_diagnosis_repair_contract = Noneself.last_code_authoring_repair_context = Noneself.synthesized_block_reopened_after_failed_run = False- self.synthesized_block_reopened_for_credential_scout = False- self.credential_scout_rescout_context_key = Noneself.synthesized_goal_complete_landed = Falseself.impose_synthesized_code_block = Falseself.scouted_output_covered_paths: set[str] = set()@@ -3262,29 +3259,6 @@ class TestCredentialFlowGoalComplete:assert synthesized_trajectory_is_goal_complete(ctx) is True-class TestCredentialScoutReopen:- def _offered_complete_ctx(self) -> _Ctx:- helper = TestCredentialFlowGoalComplete()- return helper._ctx_with_inventory(- helper._two_screen_full_login(),- inventory={"cred_1": frozenset({"username", "password"})},- )-- def test_arm_is_one_shot_per_identity_digest(self) -> None:- ctx = make_copilot_context()- assert arm_credential_scout_reopen(ctx, "identity-1") is True- assert ctx.synthesized_block_reopened_for_credential_scout is True- assert synthesized_persistence_reopened(ctx) is True-- ctx.synthesized_block_reopened_for_credential_scout = False- assert arm_credential_scout_reopen(ctx, "identity-1") is False- assert ctx.synthesized_block_reopened_for_credential_scout is False- assert synthesized_persistence_reopened(ctx) is False-- assert arm_credential_scout_reopen(ctx, "identity-2") is True- assert ctx.synthesized_block_reopened_for_credential_scout is True--def _deadline_ctx() -> SimpleNamespace:return SimpleNamespace(copilot_total_timeout_exceeded=False,diff --git a/tests/unit/test_credential_api_response.py b/tests/unit/test_credential_api_response.pyindex 8853a9360..b4e4699b0 100644--- a/tests/unit/test_credential_api_response.py+++ b/tests/unit/test_credential_api_response.py@@ -27,6 +27,7 @@ def _make_credential(**overrides: object) -> Credential:"card_brand": None,"secret_label": None,"browser_profile_id": None,+ "auto_profile_disabled": None,"tested_url": None,"user_context": None,"save_browser_session_intent": False,@@ -44,6 +45,12 @@ def test_convert_to_response_includes_totp_identifier() -> None:assert response.credential.totp_identifier == "login_otp"+def test_convert_to_response_includes_auto_profile_disabled() -> None:+ assert _convert_to_response(_make_credential(auto_profile_disabled=True)).auto_profile_disabled is True+ assert _convert_to_response(_make_credential(auto_profile_disabled=False)).auto_profile_disabled is False+ assert _convert_to_response(_make_credential(auto_profile_disabled=None)).auto_profile_disabled is None++def test_convert_to_response_totp_identifier_none_when_not_set() -> None:credential = _make_credential(totp_identifier=None)response = _convert_to_response(credential)diff --git a/tests/unit/test_credential_update_params.py b/tests/unit/test_credential_update_params.pyindex 3dde1afa7..cddc1bb7b 100644--- a/tests/unit/test_credential_update_params.py+++ b/tests/unit/test_credential_update_params.py@@ -57,6 +57,7 @@ def _make_password_credential(**overrides: object) -> Credential:"card_brand": None,"secret_label": None,"browser_profile_id": None,+ "auto_profile_disabled": None,"tested_url": None,"user_context": None,"save_browser_session_intent": False,@@ -300,6 +301,23 @@ async def test_repo_update_credential_accepts_save_browser_session_intent() -> Nassert mock_credential.save_browser_session_intent is True+async def test_repo_update_credential_accepts_auto_profile_disabled() -> None:+ mock_credential = MagicMock()+ mock_credential.name = "test"+ mock_credential.auto_profile_disabled = False+ repo = _make_credential_repo(mock_credential)++ with patch("skyvern.forge.sdk.schemas.credentials.Credential.model_validate", return_value=MagicMock()):+ await repo.update_credential(+ credential_id="cred_123",+ organization_id="org_123",+ auto_profile_disabled=True,+ )++ assert mock_credential.auto_profile_disabled is True++@pytest.mark.asyncioasync def test_repo_update_credential_accepts_tested_url() -> None:mock_credential = MagicMock()@@ -1096,7 +1114,11 @@ async def test_validate_browser_profile_id_allows_relink_to_same_credential(monk)-def _stored_credential(browser_profile_id: str | None = None, pin: bool = False) -> Credential:+def _stored_credential(+ browser_profile_id: str | None = None,+ pin: bool = False,+ auto_profile_disabled: bool | None = None,+) -> Credential:return Credential(credential_id="cred_123",organization_id="org_123",@@ -1109,6 +1131,7 @@ def _stored_credential(browser_profile_id: str | None = None, pin: bool = False)card_brand=None,secret_label=None,browser_profile_id=browser_profile_id,+ auto_profile_disabled=auto_profile_disabled,pin_saved_session_ip=pin,created_at=datetime(2026, 1, 1),modified_at=datetime(2026, 1, 1),@@ -1154,6 +1177,28 @@ async def test_create_credential_links_validated_browser_profile_id(monkeypatch:assert response.pin_saved_session_ip is True+async def test_create_credential_persists_auto_profile_disabled(monkeypatch: pytest.MonkeyPatch) -> None:+ vault_service = SimpleNamespace(create_credential=AsyncMock(return_value=_stored_credential()))+ monkeypatch.setattr(credentials_routes, "_get_credential_vault_service", AsyncMock(return_value=vault_service))+ update_credential = AsyncMock(return_value=_stored_credential(auto_profile_disabled=True))+ monkeypatch.setattr(forge_app.DATABASE.credentials, "update_credential", update_credential)++ response = await credentials_routes.create_credential(+ background_tasks=BackgroundTasks(),+ data=CreateCredentialRequest(+ name="test",+ credential_type=CredentialType.PASSWORD,+ credential={"username": "user", "password": "pw"},+ auto_profile_disabled=True,+ ),+ current_org=SimpleNamespace(organization_id="org_123"),+ )++ assert update_credential.await_args.kwargs["auto_profile_disabled"] is True+ assert response.auto_profile_disabled is True++@pytest.mark.asyncioasync def test_create_credential_rejects_managed_browser_profile_id(monkeypatch: pytest.MonkeyPatch) -> None:vault_create = AsyncMock(return_value=_stored_credential())@@ -1221,6 +1266,30 @@ async def test_rename_credential_sets_pin_and_validated_browser_profile_id(monkeassert kwargs["pin_saved_session_ip"] is True[email protected]("disabled", [True, False])+async def test_rename_credential_sets_auto_profile_disabled_without_unlinking(+ monkeypatch: pytest.MonkeyPatch,+ disabled: bool,+) -> None:+ existing = _stored_credential(browser_profile_id="bp_existing", auto_profile_disabled=not disabled)+ updated = _stored_credential(browser_profile_id="bp_existing", auto_profile_disabled=disabled)+ update_credential = AsyncMock(return_value=updated)+ monkeypatch.setattr(forge_app.DATABASE.credentials, "get_credential", AsyncMock(return_value=existing))+ monkeypatch.setattr(forge_app.DATABASE.credentials, "update_credential", update_credential)++ response = await credentials_routes.rename_credential(+ credential_id="cred_123",+ data=UpdateCredentialRequest(auto_profile_disabled=disabled),+ current_org=SimpleNamespace(organization_id="org_123"),+ )++ assert update_credential.await_args.kwargs["auto_profile_disabled"] is disabled+ assert "browser_profile_id" not in update_credential.await_args.kwargs+ assert response.browser_profile_id == "bp_existing"+ assert response.auto_profile_disabled is disabled++def test_credential_route_treats_null_advanced_key_as_no_explicit_identity() -> None:update_kwargs: dict[str, object] = {}@@ -1334,6 +1403,7 @@ async def test_update_credential_route_applies_validated_browser_profile_and_pincredential=NonEmptyPasswordCredential(username="[email protected]", password="pw"),browser_profile_id="bp_plain",pin_saved_session_ip=True,+ auto_profile_disabled=True,)response = await credentials_routes.update_credential(@@ -1346,6 +1416,7 @@ async def test_update_credential_route_applies_validated_browser_profile_and_pinkwargs = update_credential.await_args.kwargsassert kwargs["browser_profile_id"] == "bp_plain"assert kwargs["pin_saved_session_ip"] is True+ assert kwargs["auto_profile_disabled"] is Trueassert response.browser_profile_id == "bp_plain"assert response.pin_saved_session_ip is Truediff --git a/tests/unit/test_download_file_action_handler.py b/tests/unit/test_download_file_action_handler.pyindex 7bc11c349..d7076ecfa 100644--- a/tests/unit/test_download_file_action_handler.py+++ b/tests/unit/test_download_file_action_handler.py@@ -46,6 +46,21 @@ from tests.unit.helpers import make_organization, make_step, make_taskCI_TEST_RUNAWAY_TIMEOUT_SECONDS = 5.0+def _bind_adopted_download_authorizer(+ page: MagicMock,+ authorize_request_hop: object,+ *,+ owner_context: object | None = None,+) -> None:+ interceptor = MagicMock()+ interceptor._page_context = page.context if owner_context is None else owner_context+ interceptor._redirect_hop_authorizer = authorize_request_hop+ interceptor._cookie_header_for_url = AsyncMock(return_value="session=authenticated")+ page.context._skyvern_cdp_download_interceptor_bind_lock = asyncio.Lock()+ page.context._skyvern_cdp_download_interceptor = interceptor+ page.is_closed.return_value = False++async def _assert_background_tasks_drained(tasks: set[asyncio.Task[None]]) -> None:if tasks:await asyncio.wait(tuple(tasks), timeout=0.25)@@ -1042,7 +1057,16 @@ async def test_handle_action_timeout_bounds_browser_download_handler_drain()task.download_timeout = 0.01page.on.side_effect = lambda *args: None- interceptor = CDPDownloadInterceptor()+ inert_monitor = MagicMock(name="inert_network_egress_monitor")+ inert_monitor.authorize_request.return_value = False+ inert_authorizer = AsyncMock(+ name="inert_redirect_hop_authorizer",+ side_effect=AssertionError("direct HTTP download is outside this drain test"),+ )+ interceptor = CDPDownloadInterceptor(+ network_egress_monitor=inert_monitor,+ redirect_hop_authorizer=inert_authorizer,+ )interceptor._accepting_browser_downloads = Truepage.context._skyvern_cdp_download_interceptor = interceptorhandler_started = asyncio.Event()@@ -3805,11 +3829,41 @@ async def test_handle_action_logs_warning_when_late_native_appears_after_xhr_fal@pytest.mark.asyncio-async def test_handle_action_adopted_session_lands_download_via_eager_save(- span_exporter: InMemorySpanExporter,+ ("binding_mode", "expected_error"),+ [+ pytest.param(+ "missing-interceptor",+ "requires the page context's CDP download interceptor",+ id="missing-interceptor-fails-closed",+ ),+ pytest.param(+ "missing-authorizer",+ "has no redirect hop authorizer",+ id="missing-authorizer-fails-closed",+ ),+ pytest.param(+ "stale-interceptor",+ "does not own the adopted-session download page context",+ id="stale-context-fails-closed",+ ),+ pytest.param(+ "wrong-download-context",+ "download page context does not match the active page context",+ id="wrong-download-context-fails-closed",+ ),+ pytest.param(+ "missing-download-page",+ "download has no owning page",+ id="missing-download-page-fails-closed",+ ),+ ],+)+async def test_handle_action_adopted_session_requires_context_owned_authorizer(+ binding_mode: str,+ expected_error: str,) -> None:- """On an adopted persistent session the run connection saves the download eagerly at event- time and the bytes land in the run dir."""+ """The adopted-session path follows only the interceptor bound to the download's page context."""now = datetime.now(UTC)organization = make_organization(now)task = make_task(@@ -3852,6 +3906,23 @@ async def test_handle_action_adopted_session_lands_download_via_eager_save(download = MagicMock()download.suggested_filename = "report.pdf"download.url = "https://example.com/presigned/report.pdf"+ download.page = page+ authorize_request_hop = AsyncMock()+ if binding_mode == "missing-interceptor":+ page.context._skyvern_cdp_download_interceptor_bind_lock = asyncio.Lock()+ delattr(page.context, "_skyvern_cdp_download_interceptor")+ elif binding_mode == "missing-authorizer":+ page.context._skyvern_cdp_download_interceptor_bind_lock = asyncio.Lock()+ page.context._skyvern_cdp_download_interceptor = SimpleNamespace(_page_context=page.context)+ elif binding_mode == "stale-interceptor":+ _bind_adopted_download_authorizer(page, authorize_request_hop, owner_context=object())+ elif binding_mode == "wrong-download-context":+ _bind_adopted_download_authorizer(page, authorize_request_hop)+ download.page = SimpleNamespace(context=object())+ elif binding_mode == "missing-download-page":+ _bind_adopted_download_authorizer(page, authorize_request_hop)+ download.page = None+ page.is_closed.return_value = Falseasync def save_download(target_path: str | os.PathLike[str]) -> None:with open(target_path, "wb") as f:@@ -3887,25 +3958,19 @@ async def test_handle_action_adopted_session_lands_download_via_eager_save(),patch("skyvern.webeye.actions.handler.app", mock_app),):- results = await ActionHandler.handle_action(- scraped_page=scraped_page,- task=task,- step=step,- page=page,- action=action,- )+ with pytest.raises(RuntimeError, match=expected_error):+ await ActionHandler.handle_action(+ scraped_page=scraped_page,+ task=task,+ step=step,+ page=page,+ action=action,+ )landed_files = sorted(os.listdir(primary_dir))- assert results[-1].download_triggered is True- assert action.download_triggered is True- assert len(landed_files) == 1 and landed_files[0].endswith("-report.pdf")- assert results[-1].downloaded_files == landed_files- download.save_as.assert_awaited_once()- # eager save wins; the url re-fetch is not needed when save_as succeeds- page.context.request.get.assert_not_called()- span_attrs = _download_wait_span_attrs(span_exporter)- assert span_attrs["download_event_fallback_used"] is True+ assert landed_files == []+ download.save_as.assert_not_awaited()@pytest.mark.asyncio@@ -3913,7 +3978,7 @@ async def test_handle_action_adopted_session_refetches_when_save_as_target_closespan_exporter: InMemorySpanExporter,) -> None:"""When the worker tears the shared browser down and the run's save_as raises while the run- request context is still alive, the adopted-session path re-fetches the replayable url and the+ guarded HTTP client is still available, the adopted-session path re-fetches the replayable URL and thebytes still land in the run dir."""now = datetime.now(UTC)organization = make_organization(now)@@ -3933,12 +3998,16 @@ async def test_handle_action_adopted_session_refetches_when_save_as_target_closepage.expose_binding = AsyncMock()download_callbacks: dict[str, Callable[[object], None]] = {}page.on.side_effect = lambda event, callback: download_callbacks.__setitem__(event, callback)+ authorize_request_hop = AsyncMock()+ _bind_adopted_download_authorizer(page, authorize_request_hop)refetched_bytes = b"%PDF-1.4 refetched report bytes"- refetch_response = MagicMock()- refetch_response.status = 200- refetch_response.body = AsyncMock(return_value=refetched_bytes)- page.context.request.get = AsyncMock(return_value=refetch_response)++ async def guarded_fetch(*args: object, **kwargs: object) -> MagicMock:+ assert page.context._skyvern_cdp_download_interceptor_bind_lock.locked()+ return MagicMock(body=refetched_bytes)++ fetch_file_bytes = AsyncMock(side_effect=guarded_fetch)browser_state = MagicMock()browser_state.list_valid_pages = AsyncMock(return_value=[page])@@ -3963,6 +4032,7 @@ async def test_handle_action_adopted_session_refetches_when_save_as_target_closedownload = MagicMock()download.suggested_filename = "report.pdf"download.url = "https://example.com/presigned/report.pdf"+ download.page = pagedownload.save_as = AsyncMock(side_effect=Exception("Target page, context or browser has been closed"))async def mock_inner_handle_action(*args: object, **kwargs: object) -> list[ActionSuccess]:@@ -3991,6 +4061,10 @@ async def test_handle_action_adopted_session_refetches_when_save_as_target_close"skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",new=wait_for_downloads,),+ patch(+ "skyvern.webeye.actions.handler.fetch_file_bytes",+ new=fetch_file_bytes,+ ),patch("skyvern.webeye.actions.handler.app", mock_app),):results = await ActionHandler.handle_action(@@ -4010,7 +4084,13 @@ async def test_handle_action_adopted_session_refetches_when_save_as_target_closeassert landed_bytes == refetched_bytesassert results[-1].downloaded_files == landeddownload.save_as.assert_awaited_once()- page.context.request.get.assert_awaited_once_with(download.url, max_redirects=0)+ fetch_file_bytes.assert_awaited_once_with(+ download.url,+ headers={"Cookie": "session=authenticated"},+ authorize_request_hop=authorize_request_hop,+ )+ page.context._skyvern_cdp_download_interceptor._cookie_header_for_url.assert_awaited_once_with(download.url)+ page.context.request.get.assert_not_called()span_attrs = _download_wait_span_attrs(span_exporter)assert span_attrs["download_event_fallback_used"] is True@@ -4042,11 +4122,9 @@ async def test_handle_action_adopted_session_falls_through_to_session_folder_whedownload_callbacks: dict[str, Callable[[object], None]] = {}page.on.side_effect = lambda event, callback: download_callbacks.__setitem__(event, callback)- # helper save_as raises and refetch returns non-200 → helper returns None- failed_refetch = MagicMock()- failed_refetch.status = 403- failed_refetch.body = AsyncMock(return_value=b"forbidden")- page.context.request.get = AsyncMock(return_value=failed_refetch)+ authorize_request_hop = AsyncMock()+ _bind_adopted_download_authorizer(page, authorize_request_hop)+ fetch_file_bytes = AsyncMock(side_effect=Exception("guarded refetch failed"))browser_state = MagicMock()browser_state.list_valid_pages = AsyncMock(return_value=[page])@@ -4071,6 +4149,7 @@ async def test_handle_action_adopted_session_falls_through_to_session_folder_whedownload = MagicMock()download.suggested_filename = "report.pdf"download.url = "https://example.com/presigned/report.pdf"+ download.page = pagedownload.save_as = AsyncMock(side_effect=Exception("Target page, context or browser has been closed"))async def mock_inner_handle_action(*args: object, **kwargs: object) -> list[ActionSuccess]:@@ -4111,6 +4190,10 @@ async def test_handle_action_adopted_session_falls_through_to_session_folder_whe"skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",new=wait_for_downloads,),+ patch(+ "skyvern.webeye.actions.handler.fetch_file_bytes",+ new=fetch_file_bytes,+ ),patch("skyvern.webeye.actions.handler.app", mock_app),):results = await ActionHandler.handle_action(@@ -4125,7 +4208,11 @@ async def test_handle_action_adopted_session_falls_through_to_session_folder_wheassert action.download_triggered is True# helper attempted and failed; recovery came from the browser-session folder polldownload.save_as.assert_awaited_once()- page.context.request.get.assert_awaited_once_with(download.url, max_redirects=0)+ fetch_file_bytes.assert_awaited_once_with(+ download.url,+ headers={"Cookie": "session=authenticated"},+ authorize_request_hop=authorize_request_hop,+ )# storage was polled at least twice: once before the action, again on a later loop iterationassert mock_app.STORAGE.list_downloaded_files_in_browser_session.await_count >= 2span_attrs = _download_wait_span_attrs(span_exporter)@@ -4165,6 +4252,11 @@ async def test_handle_action_adopted_session_helper_failure_does_not_short_circupage.expose_binding = AsyncMock()download_callbacks: dict[str, Callable[[object], None]] = {}page.on.side_effect = lambda event, callback: download_callbacks.__setitem__(event, callback)+ # The real helper is replaced below with an instant-None stand-in, so the authorizer it would have+ # received is never exercised — but the caller still resolves it via the page's bound interceptor+ # *before* invoking the (mocked) helper, so that binding still needs to be real.+ _bind_adopted_download_authorizer(page, AsyncMock())+ fetch_file_bytes = AsyncMock(side_effect=Exception("connection gone"))browser_state = MagicMock()browser_state.list_valid_pages = AsyncMock(return_value=[page])@@ -4189,6 +4281,7 @@ async def test_handle_action_adopted_session_helper_failure_does_not_short_circudownload = MagicMock()download.suggested_filename = "report.pdf"download.url = "https://example.com/presigned/report.pdf"+ download.page = page# Isolate the stated contract: the adopted-session save helper returns None (could not save), and# the poll loop must keep polling the browser-session folder afterwards. Replacing the real helper@@ -4222,6 +4315,10 @@ async def test_handle_action_adopted_session_helper_failure_does_not_short_circu"skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",new=wait_for_downloads,),+ patch(+ "skyvern.webeye.actions.handler.fetch_file_bytes",+ new=fetch_file_bytes,+ ),patch("skyvern.webeye.actions.handler.app", mock_app),):results = await ActionHandler.handle_action(@@ -4273,7 +4370,9 @@ async def test_handle_action_adopted_session_xhr_staging_recovered_when_helper_fpage.expose_binding = AsyncMock()download_callbacks: dict[str, Callable[[object], None]] = {}page.on.side_effect = lambda event, callback: download_callbacks.__setitem__(event, callback)- page.context.request.get = AsyncMock(side_effect=Exception("connection gone"))+ authorize_request_hop = AsyncMock()+ _bind_adopted_download_authorizer(page, authorize_request_hop)+ fetch_file_bytes = AsyncMock(side_effect=Exception("connection gone"))browser_state = MagicMock()browser_state.list_valid_pages = AsyncMock(return_value=[page])@@ -4298,6 +4397,7 @@ async def test_handle_action_adopted_session_xhr_staging_recovered_when_helper_fdownload = MagicMock()download.suggested_filename = "report.pdf"download.url = "https://example.com/presigned/report.pdf"+ download.page = pagedownload.save_as = AsyncMock(side_effect=Exception("Target page, context or browser has been closed"))async def mock_inner_handle_action(*args: object, **kwargs: object) -> list[ActionSuccess]:@@ -4338,6 +4438,10 @@ async def test_handle_action_adopted_session_xhr_staging_recovered_when_helper_f"skyvern.webeye.actions.handler.check_downloading_files_and_wait_for_download_to_complete",new=wait_for_downloads,),+ patch(+ "skyvern.webeye.actions.handler.fetch_file_bytes",+ new=fetch_file_bytes,+ ),patch("skyvern.webeye.actions.handler.app", mock_app),):results = await ActionHandler.handle_action(@@ -4356,7 +4460,12 @@ async def test_handle_action_adopted_session_xhr_staging_recovered_when_helper_ftimeout_seconds=download_timeout if download_timeout is not None else BROWSER_DOWNLOAD_TIMEOUT,)download.save_as.assert_awaited_once()- page.context.request.get.assert_awaited_once_with(download.url, max_redirects=0)+ fetch_file_bytes.assert_awaited_once_with(+ download.url,+ headers={"Cookie": "session=authenticated"},+ authorize_request_hop=authorize_request_hop,+ )+ page.context.request.get.assert_not_called()# file moved from staging to download dirlanded_files = sorted(os.listdir(primary_dir))assert landed_files == ["xhr-captured.pdf"]@@ -5154,6 +5263,7 @@ async def test_handle_action_adopted_popup_blob_download_wired_end_to_end() -> Npage.context.browser = Nonepage_listeners: dict[str, Callable[[object], None]] = {}page.on.side_effect = lambda event, cb: page_listeners.__setitem__(event, cb)+ _bind_adopted_download_authorizer(page, AsyncMock())browser_state = MagicMock()browser_state.list_valid_pages = AsyncMock(return_value=[page])@@ -5175,6 +5285,7 @@ async def test_handle_action_adopted_popup_blob_download_wired_end_to_end() -> Npopup_page = MagicMock()popup_page.url = "about:blank"+ popup_page.context = page.contextpopup_listeners: dict[str, Callable[[object], None]] = {}popup_page.on.side_effect = lambda event, cb: popup_listeners.__setitem__(event, cb)@@ -5263,6 +5374,7 @@ async def test_handle_action_eager_read_timeout_does_not_starve_fallback() -> Nopage.context.browser = Nonepage_listeners: dict[str, Callable[[object], None]] = {}page.on.side_effect = lambda event, cb: page_listeners.__setitem__(event, cb)+ _bind_adopted_download_authorizer(page, AsyncMock())browser_state = MagicMock()browser_state.list_valid_pages = AsyncMock(return_value=[page])diff --git a/tests/unit/test_forloop_incremental_persist.py b/tests/unit/test_forloop_incremental_persist.pyindex 09cd0e4b7..bc40691e8 100644--- a/tests/unit/test_forloop_incremental_persist.py+++ b/tests/unit/test_forloop_incremental_persist.py@@ -460,12 +460,18 @@ class TestResetBrowserTabsBetweenIterations:mock_bs = MagicMock(spec=RealBrowserState)mock_bs.close_pages_opened_after = AsyncMock()baseline = {MagicMock()}+ reset_browser_tabs = AsyncMock(return_value=True)with (- patch.object(block_module.settings, "RESET_BROWSER_TABS_BETWEEN_LOOP_ITERATIONS", True),+ patch.object(+ block_module.planner_levers,+ "reset_browser_tabs_between_loop_iterations",+ reset_browser_tabs,+ ),patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,):mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(return_value=mock_bs)await loop_block._reset_browser_tabs_for_iteration("wr_test", "org_test", "pbs_test", baseline)+ reset_browser_tabs.assert_awaited_once_with("org_test")mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state.assert_awaited_once_with("pbs_test", "org_test")mock_bs.close_pages_opened_after.assert_awaited_once_with(baseline)@@ -473,7 +479,12 @@ class TestResetBrowserTabsBetweenIterations:async def test_reset_helper_noop_when_flag_off(self) -> None:loop_block = self._loop_block()with (- patch.object(block_module.settings, "RESET_BROWSER_TABS_BETWEEN_LOOP_ITERATIONS", False),+ patch.object(+ block_module.planner_levers,+ "reset_browser_tabs_between_loop_iterations",+ new_callable=AsyncMock,+ return_value=False,+ ),patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,):await loop_block._reset_browser_tabs_for_iteration("wr_test", "org_test", "pbs_test", set())@@ -483,19 +494,30 @@ class TestResetBrowserTabsBetweenIterations:@pytest.mark.asyncioasync def test_snapshot_none_when_flag_off(self) -> None:loop_block = self._loop_block()+ reset_browser_tabs = AsyncMock(return_value=False)with (- patch.object(block_module.settings, "RESET_BROWSER_TABS_BETWEEN_LOOP_ITERATIONS", False),+ patch.object(+ block_module.planner_levers,+ "reset_browser_tabs_between_loop_iterations",+ reset_browser_tabs,+ ),patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,):result = await loop_block._snapshot_loop_baseline_pages("wr_test", "org_test", None)assert result is None+ reset_browser_tabs.assert_awaited_once_with("org_test")mock_app.BROWSER_MANAGER.get_for_workflow_run.assert_not_called()@pytest.mark.asyncioasync def test_snapshot_returns_none_when_it_fails(self) -> None:loop_block = self._loop_block()with (- patch.object(block_module.settings, "RESET_BROWSER_TABS_BETWEEN_LOOP_ITERATIONS", True),+ patch.object(+ block_module.planner_levers,+ "reset_browser_tabs_between_loop_iterations",+ new_callable=AsyncMock,+ return_value=True,+ ),patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,):mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state = AsyncMock(side_effect=Exception("boom"))@@ -505,11 +527,17 @@ class TestResetBrowserTabsBetweenIterations:async def test_reset_helper_noop_when_baseline_unknown(self) -> None:"""A failed snapshot must suppress the reset — an empty baseline would close every tab."""loop_block = self._loop_block()+ reset_browser_tabs = AsyncMock(return_value=True)with (- patch.object(block_module.settings, "RESET_BROWSER_TABS_BETWEEN_LOOP_ITERATIONS", True),+ patch.object(+ block_module.planner_levers,+ "reset_browser_tabs_between_loop_iterations",+ reset_browser_tabs,+ ),patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,):await loop_block._reset_browser_tabs_for_iteration("wr_test", "org_test", "pbs_test", None)+ reset_browser_tabs.assert_not_awaited()mock_app.PERSISTENT_SESSIONS_MANAGER.get_browser_state.assert_not_called()mock_app.BROWSER_MANAGER.get_for_workflow_run.assert_not_called()diff --git a/tests/unit/test_handler_utils_download_file.py b/tests/unit/test_handler_utils_download_file.pyindex facd743e5..2381ec558 100644--- a/tests/unit/test_handler_utils_download_file.py+++ b/tests/unit/test_handler_utils_download_file.py@@ -1,12 +1,12 @@from __future__ import annotationsfrom pathlib import Path-from types import SimpleNamespacefrom unittest.mock import AsyncMockimport pytestfrom skyvern.config import settings+from skyvern.forge.sdk.core.skyvern_context import SkyvernContextfrom skyvern.webeye.actions import handler_utils@@ -23,7 +23,7 @@ async def test_download_file_accepts_run_local_absolute_path(tmp_path: Path, monmonkeypatch.setattr(handler_utils.skyvern_context,"current",- lambda: SimpleNamespace(run_id=run_id, workflow_run_id=None, task_id=None),+ lambda: SkyvernContext(run_id=run_id, workflow_run_id=None, task_id=None),)download_mock = AsyncMock(side_effect=AssertionError("remote download must not be called"))monkeypatch.setattr(handler_utils, "download_file_api", download_mock)@@ -32,3 +32,28 @@ async def test_download_file_accepts_run_local_absolute_path(tmp_path: Path, monassert result == str(local_file.resolve())download_mock.assert_not_awaited()+++async def test_download_file_absolute_path_outside_run_dir_recovers(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ run_id = "wr_upload"+ download_root = tmp_path / "downloads"+ (download_root / run_id).mkdir(parents=True)+ outside_file = tmp_path / "outside.csv"+ outside_file.write_text("name\nAlice")++ monkeypatch.setattr(settings, "DOWNLOAD_PATH", str(download_root))+ monkeypatch.setattr(+ handler_utils.skyvern_context,+ "current",+ lambda: SkyvernContext(run_id=run_id, workflow_run_id=None, task_id=None),+ )+ download_mock = AsyncMock(side_effect=AssertionError("remote download must not be called"))+ monkeypatch.setattr(handler_utils, "download_file_api", download_mock)++ result = await handler_utils.download_file(str(outside_file), action={"action_type": "upload_file"})++ assert result == []+ download_mock.assert_not_awaited()diff --git a/tests/unit/test_http_request_authorization.py b/tests/unit/test_http_request_authorization.pynew file mode 100644index 000000000..1f1866221--- /dev/null+++ b/tests/unit/test_http_request_authorization.py@@ -0,0 +1,108 @@+import asyncio+import dataclasses+from typing import get_args++import pytest++from skyvern.forge.sdk.core.http_request_authorization import (+ RedirectHopAuthorization,+ RedirectHopAuthorizer,+ RedirectHopDispatcher,+ authorize_request_hop_once,+)+++def test_redirect_hop_authorization_contract_is_immutable_and_dispatch_bound() -> None:+ assert dataclasses.is_dataclass(RedirectHopAuthorization)+ assert RedirectHopAuthorization.__dataclass_params__.frozen+ assert tuple(field.name for field in dataclasses.fields(RedirectHopAuthorization)) == (+ "source_url",+ "target_url",+ "method",+ )++ callback_parameters, _callback_result = get_args(RedirectHopAuthorizer)+ assert callback_parameters[0] is RedirectHopAuthorization+ dispatcher_parameters, _dispatcher_result = get_args(callback_parameters[1])+ assert dispatcher_parameters == [tuple[str, ...]]+++async def test_authorized_redirect_hop_dispatcher_is_single_use() -> None:+ attempts: list[tuple[str, ...]] = []++ async def dispatch(resolved_values: tuple[str, ...]) -> str:+ attempts.append(resolved_values)+ return "dispatched"++ async def authorize(+ _authorization: RedirectHopAuthorization,+ guarded_dispatch: RedirectHopDispatcher[str],+ ) -> str:+ result = await guarded_dispatch(("first",))+ with pytest.raises(RuntimeError, match="only be invoked once"):+ await guarded_dispatch(("second",))+ return result++ result = await authorize_request_hop_once(+ authorize,+ RedirectHopAuthorization(None, "https://example.com", "POST"),+ dispatch,+ )++ assert result == "dispatched"+ assert attempts == [("first",)]+++async def test_authorized_redirect_hop_dispatcher_cannot_escape_callback_scope() -> None:+ retained: list[RedirectHopDispatcher[str]] = []+ attempts: list[tuple[str, ...]] = []++ async def dispatch(resolved_values: tuple[str, ...]) -> str:+ attempts.append(resolved_values)+ return "dispatched"++ async def authorize(+ _authorization: RedirectHopAuthorization,+ guarded_dispatch: RedirectHopDispatcher[str],+ ) -> str:+ retained.append(guarded_dispatch)+ return "not-dispatched"++ result = await authorize_request_hop_once(+ authorize,+ RedirectHopAuthorization(None, "https://example.com", "GET"),+ dispatch,+ )++ assert result == "not-dispatched"+ with pytest.raises(RuntimeError, match="no longer active"):+ await retained[0](())+ assert attempts == []+++async def test_authorized_redirect_hop_dispatcher_rejects_background_tasks() -> None:+ attempts: list[tuple[str, ...]] = []++ async def dispatch(resolved_values: tuple[str, ...]) -> str:+ attempts.append(resolved_values)+ return "dispatched"++ async def authorize(+ _authorization: RedirectHopAuthorization,+ guarded_dispatch: RedirectHopDispatcher[str],+ ) -> str:+ with pytest.raises(RuntimeError, match="authorizing task"):+ await asyncio.create_task(guarded_dispatch(()))+ return "blocked"++ result = await authorize_request_hop_once(+ authorize,+ RedirectHopAuthorization(None, "https://example.com", "GET"),+ dispatch,+ )++ assert result == "blocked"+ assert attempts == []diff --git a/tests/unit/test_mcp_block_tools.py b/tests/unit/test_mcp_block_tools.pyindex 33182ab08..b5ff8732c 100644--- a/tests/unit/test_mcp_block_tools.py+++ b/tests/unit/test_mcp_block_tools.py@@ -50,6 +50,46 @@ async def test_block_validate_task_type_warns_deprecated() -> None:assert any("deprecated" in w.lower() for w in result["warnings"])+async def test_block_validate_code_without_prompt_warns_without_mutating_response() -> None:+ block = {+ "block_type": "code",+ "label": "transform",+ "code": "return 1",+ }+ result = await skyvern_block_validate(block_json=json.dumps(block))++ assert result["ok"] is True+ assert result["data"] == {+ "valid": True,+ "block_type": "code",+ "label": "transform",+ "field_count": 2,+ }+ assert len(result["warnings"]) == 1+ warning = result["warnings"][0]+ assert "prompt" in warning+ assert "Workflow create" in warning+ assert "new label" in warning+ assert "not migrated" in warning+++async def test_block_validate_code_with_explicit_null_prompt_does_not_warn() -> None:+ block = {+ "block_type": "code",+ "label": "transform",+ "code": "return 1",+ "prompt": None,+ }+ result = await skyvern_block_validate(block_json=json.dumps(block))++ assert result["ok"] is True+ assert result["data"]["valid"] is True+ assert result["data"]["field_count"] == 3+ assert result["warnings"] == []++@pytest.mark.asyncioasync def test_block_schema_no_type_lists_all() -> None:"""Calling without a block_type should list all available types."""diff --git a/tests/unit/test_mcp_workflow_create_defaults.py b/tests/unit/test_mcp_workflow_create_defaults.pyindex 872ca40a9..be2780776 100644--- a/tests/unit/test_mcp_workflow_create_defaults.py+++ b/tests/unit/test_mcp_workflow_create_defaults.py@@ -5,15 +5,19 @@ import jsonimport pytestimport yaml+from skyvern.cli.mcp_tools import workflow as workflow_toolsfrom skyvern.cli.mcp_tools.workflow import (+ _CODE_V2_DEFAULTS,_inject_code_block_derived_steps,_inject_code_block_prompt_defaults,- _inject_code_v2_defaults,_inject_missing_top_level_defaults,_inject_workflow_update_code_block_prompt_defaults,_parse_definition,)+from skyvern.forge.sdk.db.models import WorkflowModel+from skyvern.forge.sdk.db.repositories import workflows as workflow_repositoryfrom skyvern.schemas.runs import ProxyLocation+from skyvern.schemas.workflows import WorkflowCreateYAMLRequest_ACTION_CODE = 'await page.goto("https://example.com")\nawait page.click("button.submit")'@@ -39,41 +43,42 @@ def _minimal_workflow_json(**overrides: object) -> str:return json.dumps(base)-def test_defaults_injected_when_not_specified() -> None:- """When code_version and run_with are omitted, _inject_code_v2_defaults adds them."""+def test_code_defaults_injected_when_not_specified() -> None:+ """The shared top-level injector adds omitted Code 2.0 defaults."""definition = _minimal_workflow_json()- result = _inject_code_v2_defaults(definition, "json")+ result = _inject_missing_top_level_defaults(definition, "json", _CODE_V2_DEFAULTS)parsed = json.loads(result)assert parsed["code_version"] == 2assert parsed["run_with"] == "agent"-def test_defaults_injected_in_auto_mode() -> None:+def test_code_defaults_injected_in_auto_mode() -> None:"""Auto format also injects defaults for JSON input."""definition = _minimal_workflow_json()- result = _inject_code_v2_defaults(definition, "auto")+ result = _inject_missing_top_level_defaults(definition, "auto", _CODE_V2_DEFAULTS)parsed = json.loads(result)assert parsed["code_version"] == 2assert parsed["run_with"] == "agent"-def test_explicit_values_preserved() -> None:- """When the user explicitly sets these fields, their values are preserved."""- definition = _minimal_workflow_json(code_version=1, run_with="code")- result = _inject_code_v2_defaults(definition, "json")- parsed = json.loads(result)- assert parsed["code_version"] == 1- assert parsed["run_with"] == "code"---def test_explicit_null_run_with_preserved() -> None:- """When the user explicitly sets run_with to null, it stays null."""- definition = _minimal_workflow_json(run_with=None)- result = _inject_code_v2_defaults(definition, "json")- parsed = json.loads(result)- assert parsed["run_with"] is None- # code_version was not set, so it gets the default- assert parsed["code_version"] == 2+ ("overrides", "expected_code_version", "expected_run_with"),+ [+ ({"code_version": 1, "run_with": "code"}, 1, "code"),+ ({"code_version": None, "run_with": None}, None, None),+ ],+)+def test_explicit_code_default_values_and_nulls_preserved_for_yaml(+ overrides: dict[str, object],+ expected_code_version: int | None,+ expected_run_with: str | None,+) -> None:+ """Top-level default injection is membership-based, including for YAML nulls."""+ definition = yaml.safe_dump(json.loads(_minimal_workflow_json(**overrides)), sort_keys=False)+ result = _inject_missing_top_level_defaults(definition, "yaml", _CODE_V2_DEFAULTS)+ parsed = yaml.safe_load(result)+ assert parsed["code_version"] == expected_code_version+ assert parsed["run_with"] == expected_run_withdef test_proxy_default_injected_when_not_specified_json() -> None:@@ -126,10 +131,99 @@ workflow_definition:def test_invalid_json_passthrough() -> None:"""Invalid JSON is passed through (let _parse_definition handle the error)."""bad_json = "not valid json {"- result = _inject_code_v2_defaults(bad_json, "json")+ result = _inject_missing_top_level_defaults(bad_json, "json", _CODE_V2_DEFAULTS)assert result == bad_json+async def test_create_yaml_persists_code_v2_defaults_without_disturbing_code_block_derivation(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """YAML defaults reach the repository model after the full MCP create pipeline."""+ backend_requests: list[WorkflowCreateYAMLRequest] = []+ repository_rows: list[WorkflowModel] = []++ class FakeSession:+ async def __aenter__(self) -> "FakeSession":+ return self++ async def __aexit__(+ self,+ exc_type: type[BaseException] | None,+ exc_value: BaseException | None,+ traceback: object,+ ) -> None:+ return None++ def add(self, workflow: WorkflowModel) -> None:+ repository_rows.append(workflow)++ async def commit(self) -> None:+ return None++ async def refresh(self, workflow: WorkflowModel) -> None:+ return None++ repository = workflow_repository.WorkflowsRepository(FakeSession) # type: ignore[arg-type]+ monkeypatch.setattr(workflow_repository, "convert_to_workflow", lambda model, debug_enabled: model)++ async def fake_create_workflow_raw(+ *,+ json_definition: dict[str, object] | None,+ yaml_definition: str | None,+ folder_id: str | None,+ ) -> dict[str, object]:+ assert json_definition is None+ assert yaml_definition is not None+ assert folder_id is None+ backend_request = WorkflowCreateYAMLRequest.model_validate(yaml.safe_load(yaml_definition))+ backend_requests.append(backend_request)+ await repository.create_workflow(+ title=backend_request.title,+ workflow_definition=backend_request.workflow_definition.model_dump(mode="json"),+ proxy_location=backend_request.proxy_location,+ run_with=backend_request.run_with,+ code_version=backend_request.code_version,+ )+ repository_row = repository_rows[-1]+ return {+ "workflow_permanent_id": "wpid_test",+ "workflow_id": "wf_test",+ "title": repository_row.title,+ "version": 1,+ "status": "published",+ "run_with": repository_row.run_with,+ "code_version": repository_row.code_version,+ }++ monkeypatch.setattr(workflow_tools, "create_workflow_raw", fake_create_workflow_raw)+ definition = f"""+title: YAML Code Workflow+workflow_definition:+ parameters: []+ blocks:+ - block_type: code+ label: step1+ code: |+ {_ACTION_CODE.replace(chr(10), chr(10) + " ")}+"""++ result = await workflow_tools.skyvern_workflow_create(definition=definition, format="yaml")++ assert result["ok"] is True, result+ assert len(backend_requests) == 1+ assert len(repository_rows) == 1+ backend_request = backend_requests[0]+ repository_row = repository_rows[0]+ assert repository_row.code_version == 2+ assert repository_row.run_with == "agent"+ assert backend_request.code_version == 2+ block = backend_request.workflow_definition.blocks[0]+ assert block.prompt == ""+ assert block.steps is not None+ assert [step.action_type for step in block.steps] == ["goto_url", "click"]++def _code_workflow_json(blocks: list[dict[str, object]]) -> str:return json.dumps({diff --git a/tests/unit/test_org_auth_service.py b/tests/unit/test_org_auth_service.pyindex 1a6194178..71d40d296 100644--- a/tests/unit/test_org_auth_service.py+++ b/tests/unit/test_org_auth_service.py@@ -1,3 +1,4 @@+import asynciofrom collections.abc import Iteratorfrom datetime import datetime, timedeltafrom time import time as current_time@@ -12,9 +13,8 @@ from freezegun import freeze_timefrom skyvern.config import settingsfrom skyvern.forge.agent_functions import AgentFunctionfrom skyvern.forge.sdk.core.security import create_access_token-from skyvern.forge.sdk.db.enums import OrganizationAuthTokenTypefrom skyvern.forge.sdk.routes.routers import legacy_base_router-from skyvern.forge.sdk.schemas.organizations import Organization, OrganizationAuthToken+from skyvern.forge.sdk.schemas.organizations import Organization, OrganizationAuthToken, OrganizationAuthTokenTypefrom skyvern.forge.sdk.services import org_auth_service, org_auth_token_servicefrom skyvern.forge.sdk.services.org_auth_service import (_get_api_key_debug_fields,@@ -196,6 +196,144 @@ def _make_org(organization_id: str, name: str = "test-org") -> Organization:)+def _make_auth_token(token: str, *, valid: bool) -> OrganizationAuthToken:+ now = datetime.utcnow()+ return OrganizationAuthToken(+ id="oat_test",+ organization_id="org-a",+ token_type=OrganizationAuthTokenType.api,+ token=token,+ valid=valid,+ created_at=now,+ modified_at=now,+ )+++class _RotatingAuthOrganizations:+ def __init__(self, api_keys: dict[str, bool]) -> None:+ self.api_keys = api_keys+ self.organization = _make_org("org-a")+ self.validate_calls = 0++ async def get_organization(self, organization_id: str) -> Organization | None:+ return self.organization if organization_id == self.organization.organization_id else None++ async def validate_org_auth_token(+ self,+ organization_id: str,+ token_type: OrganizationAuthTokenType,+ token: str,+ valid: bool | None = True,+ ) -> OrganizationAuthToken | None:+ self.validate_calls += 1+ assert organization_id == "org-a"+ assert token_type == OrganizationAuthTokenType.api+ assert valid is None+ token_valid = self.api_keys.get(token)+ return _make_auth_token(token, valid=token_valid) if token_valid is not None else None++ def rotate_api_key(self, old_api_key: str, replacement_api_key: str) -> None:+ self.api_keys[old_api_key] = False+ self.api_keys[replacement_api_key] = True+++class _AuthDatabase:+ def __init__(self, organizations: _RotatingAuthOrganizations) -> None:+ self.organizations = organizations+++async def test_get_current_org_cached_rejects_rotated_key_after_invalidation() -> None:+ old_api_key = create_access_token("org-a", expires_delta=timedelta(hours=1))+ replacement_api_key = create_access_token("org-a", expires_delta=timedelta(hours=2))+ organizations = _RotatingAuthOrganizations({old_api_key: True})+ db = _AuthDatabase(organizations)++ original_organization = await org_auth_service.get_current_org_cached(old_api_key, db)+ organizations.rotate_api_key(old_api_key, replacement_api_key)++ # Control: this establishes the positive cache is active rather than a guard+ # that always revalidates. Rotation alone does not invalidate this process.+ cached_organization = await org_auth_service.get_current_org_cached(old_api_key, db)+ assert cached_organization.organization_id == original_organization.organization_id == "org-a"+ assert organizations.validate_calls == 1++ org_auth_service.invalidate_cached_org("org-a")++ replacement_organization = await org_auth_service.get_current_org_cached(replacement_api_key, db)+ assert original_organization.organization_id == replacement_organization.organization_id == "org-a"++ with pytest.raises(HTTPException) as exc_info:+ await org_auth_service.get_current_org_cached(old_api_key, db)++ assert exc_info.value.status_code == 403+ assert organizations.validate_calls == 3+++async def test_get_current_org_cached_coalesces_concurrent_cache_misses(monkeypatch) -> None:+ cache = org_auth_service._current_org_cache+ cache.clear()+ organization = _make_org("org-a")+ validation_started = asyncio.Event()+ release_validation = asyncio.Event()+ resolve_calls = 0++ async def resolve_api_key(_api_key: str, _db: object) -> SimpleNamespace:+ nonlocal resolve_calls+ resolve_calls += 1+ validation_started.set()+ await release_validation.wait()+ return SimpleNamespace(organization=organization)++ monkeypatch.setattr(org_auth_service, "resolve_org_from_api_key", resolve_api_key)+ db = object()+ first_request = asyncio.create_task(org_auth_service.get_current_org_cached("api-key", db))+ await validation_started.wait()+ second_request = asyncio.create_task(org_auth_service.get_current_org_cached("api-key", db))++ try:+ await asyncio.sleep(0)+ assert resolve_calls == 1+ finally:+ release_validation.set()+ results = await asyncio.gather(first_request, second_request)+ cache.clear()++ assert results == [organization, organization]+++async def test_get_current_org_cached_does_not_recache_after_invalidation_during_validation(monkeypatch) -> None:+ cache = org_auth_service._current_org_cache+ cache.clear()+ organization = _make_org("org-a")+ validation_started = asyncio.Event()+ release_validation = asyncio.Event()+ resolve_calls = 0++ async def resolve_api_key(_api_key: str, _db: object) -> SimpleNamespace:+ nonlocal resolve_calls+ resolve_calls += 1+ if resolve_calls == 1:+ validation_started.set()+ await release_validation.wait()+ return SimpleNamespace(organization=organization)++ monkeypatch.setattr(org_auth_service, "resolve_org_from_api_key", resolve_api_key)+ db = object()+ in_flight_request = asyncio.create_task(org_auth_service.get_current_org_cached("api-key", db))+ await validation_started.wait()+ org_auth_service.invalidate_cached_org("org-a")+ release_validation.set()+ await in_flight_request++ await org_auth_service.get_current_org_cached("api-key", db)++ assert resolve_calls == 2+ cache.clear()++def test_invalidate_cached_org_drops_only_matching_entries() -> None:cache = org_auth_service._current_org_cachecache.clear()diff --git a/tests/unit/test_planner_levers.py b/tests/unit/test_planner_levers.pynew file mode 100644index 000000000..dda9cbc43--- /dev/null+++ b/tests/unit/test_planner_levers.py@@ -0,0 +1,151 @@+from collections.abc import Awaitable, Callable+from unittest.mock import AsyncMock++import pytest++from skyvern.services import planner_levers++BoolResolver = Callable[[str | None], Awaitable[bool]]++BOOL_LEVERS: list[tuple[str, BoolResolver]] = [+ (+ "TASK_V2_SKIP_COMPLETION_CHECK_AFTER_NAVIGATE",+ planner_levers.skip_completion_check_after_navigate,+ ),+ ("TASK_V2_CARRY_SUBGOALS", planner_levers.carry_subgoals),+ (+ "RESET_BROWSER_TABS_BETWEEN_LOOP_ITERATIONS",+ planner_levers.reset_browser_tabs_between_loop_iterations,+ ),+]+BOOL_LEVER_IDS = ["skip_completion_check_after_navigate", "carry_subgoals", "reset_browser_tabs"]++[email protected](("setting_name", "resolver"), BOOL_LEVERS, ids=BOOL_LEVER_IDS)+ ("settings_default", "provider_enabled", "expected", "provider_called"),+ [+ (True, False, True, False),+ (False, True, True, True),+ (False, False, False, True),+ ],+)+async def test_bool_lever_resolves_env_or_org_flag(+ setting_name: str,+ resolver: BoolResolver,+ settings_default: bool,+ provider_enabled: bool,+ expected: bool,+ provider_called: bool,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ provider = AsyncMock(return_value=provider_enabled)+ monkeypatch.setattr(planner_levers.settings, setting_name, settings_default)+ monkeypatch.setattr(planner_levers.app.EXPERIMENTATION_PROVIDER, "is_feature_enabled_cached", provider)++ assert await resolver("org_test") is expected+ if provider_called:+ provider.assert_awaited_once_with(+ "TASK_V2_PLANNER_LEVERS",+ "org_test",+ properties={"organization_id": "org_test"},+ )+ else:+ provider.assert_not_awaited()++[email protected](("setting_name", "resolver"), BOOL_LEVERS, ids=BOOL_LEVER_IDS)+async def test_bool_lever_falls_back_to_env_when_provider_raises(+ setting_name: str,+ resolver: BoolResolver,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ provider = AsyncMock(side_effect=RuntimeError("provider unavailable"))+ monkeypatch.setattr(planner_levers.settings, setting_name, False)+ monkeypatch.setattr(planner_levers.app.EXPERIMENTATION_PROVIDER, "is_feature_enabled_cached", provider)++ assert await resolver("org_test") is False+ provider.assert_awaited_once_with(+ "TASK_V2_PLANNER_LEVERS",+ "org_test",+ properties={"organization_id": "org_test"},+ )++[email protected](("setting_name", "resolver"), BOOL_LEVERS, ids=BOOL_LEVER_IDS)[email protected]("settings_default", [False, True])+async def test_bool_lever_without_org_uses_env_only(+ setting_name: str,+ resolver: BoolResolver,+ settings_default: bool,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ provider = AsyncMock()+ monkeypatch.setattr(planner_levers.settings, setting_name, settings_default)+ monkeypatch.setattr(planner_levers.app.EXPERIMENTATION_PROVIDER, "is_feature_enabled_cached", provider)++ assert await resolver(None) is settings_default+ provider.assert_not_awaited()+++ ("settings_default", "provider_enabled", "expected", "provider_called"),+ [+ (0, False, 0, True),+ (0, True, 20, True),+ (37, False, 37, False),+ (37, True, 37, False),+ ],+)+async def test_converge_pct_resolves_env_or_org_flag(+ settings_default: int,+ provider_enabled: bool,+ expected: int,+ provider_called: bool,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ provider = AsyncMock(return_value=provider_enabled)+ monkeypatch.setattr(planner_levers.settings, "TASK_V2_CONVERGE_PCT", settings_default)+ monkeypatch.setattr(planner_levers.app.EXPERIMENTATION_PROVIDER, "is_feature_enabled_cached", provider)++ assert await planner_levers.converge_pct("org_test") == expected+ if provider_called:+ provider.assert_awaited_once_with(+ "TASK_V2_PLANNER_LEVERS",+ "org_test",+ properties={"organization_id": "org_test"},+ )+ else:+ provider.assert_not_awaited()+++async def test_converge_pct_falls_back_to_env_when_provider_raises(monkeypatch: pytest.MonkeyPatch) -> None:+ provider = AsyncMock(side_effect=RuntimeError("provider unavailable"))+ monkeypatch.setattr(planner_levers.settings, "TASK_V2_CONVERGE_PCT", 0)+ monkeypatch.setattr(planner_levers.app.EXPERIMENTATION_PROVIDER, "is_feature_enabled_cached", provider)++ assert await planner_levers.converge_pct("org_test") == 0+ provider.assert_awaited_once_with(+ "TASK_V2_PLANNER_LEVERS",+ "org_test",+ properties={"organization_id": "org_test"},+ )++[email protected]("settings_default", [0, 37])+async def test_converge_pct_without_org_uses_env_only(+ settings_default: int,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ provider = AsyncMock()+ monkeypatch.setattr(planner_levers.settings, "TASK_V2_CONVERGE_PCT", settings_default)+ monkeypatch.setattr(planner_levers.app.EXPERIMENTATION_PROVIDER, "is_feature_enabled_cached", provider)++ assert await planner_levers.converge_pct(None) == settings_default+ provider.assert_not_awaited()diff --git a/tests/unit/test_remote_cdp_driver_release.py b/tests/unit/test_remote_cdp_driver_release.pyindex b616f88a8..d3e3dad40 100644--- a/tests/unit/test_remote_cdp_driver_release.py+++ b/tests/unit/test_remote_cdp_driver_release.py@@ -126,7 +126,15 @@ async def test_close_timeout_bounds_interceptor_drain_and_cleans_suspended_task() -> None:pw = _pw_stub()context = _context_stub()- interceptor = CDPDownloadInterceptor()+ inert_monitor = MagicMock(name="inert_network_egress_monitor")+ inert_authorizer = AsyncMock(+ name="inert_redirect_hop_authorizer",+ side_effect=AssertionError("unexpected direct HTTP request"),+ )+ interceptor = CDPDownloadInterceptor(+ network_egress_monitor=inert_monitor,+ redirect_hop_authorizer=inert_authorizer,+ )interceptor._accepting_browser_downloads = Truecontext._skyvern_cdp_download_interceptor = interceptorstarted = asyncio.Event()diff --git a/tests/unit/test_seed_precedence_engine.py b/tests/unit/test_seed_precedence_engine.pyindex 26a99b5fc..9f8d962c7 100644--- a/tests/unit/test_seed_precedence_engine.py+++ b/tests/unit/test_seed_precedence_engine.py@@ -296,6 +296,27 @@ async def test_credential_override_heal_only_under_engine(monkeypatch: pytest.Mo)+async def test_explicit_pick_never_consults_the_credential_opt_out_gate(monkeypatch: pytest.MonkeyPatch) -> None:+ # A deliberate pick outranks the automatic opt-out: both the workflow dropdown pick and the run-form+ # override must resolve without ever reaching the credential resolver that carries the gate, so no+ # future gate placement there can suppress a pick. Seeds, but never as a workflow sink (read-only).+ svc = _svc(monkeypatch, pick_role="credential", credential=None)+ credential_seed = AsyncMock(return_value=None)+ monkeypatch.setattr(svc, "_resolve_credential_browser_profile_id_for_setup", credential_seed)++ seed, source, sink = await _resolve(svc, _workflow(pick="bp_opted_out_cred"))+ assert (seed, sink) == ("bp_opted_out_cred", None)+ assert source is not BrowserSeedSource.fresh++ assert await _resolve(svc, _workflow(), override="bp_opted_out_cred") == (+ "bp_opted_out_cred",+ BrowserSeedSource.override,+ None,+ )+ credential_seed.assert_not_awaited()++@pytest.mark.asyncioasync def test_pick_beats_browser_profile_key(monkeypatch: pytest.MonkeyPatch) -> None:# Correction 3: an explicit plain pick wins deterministically over browser_profile_key. The key only@@ -864,12 +885,12 @@ async def test_setup_credential_resolves_single_login_block(monkeypatch: pytest.monkeypatch.setattr(app.DATABASE.credentials,"get_credential",- AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_cred")),+ AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_cred", auto_profile_disabled=False)),)monkeypatch.setattr(app.DATABASE.browser_sessions,"get_browser_profile",- AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_cred")),+ AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_cred", auto_profile_disabled=False)),)workflow = _workflow_with_blocks(_login_block("login", _credential_parameter("login", "cred_1")))@@ -883,6 +904,66 @@ async def test_setup_credential_resolves_single_login_block(monkeypatch: pytest.assert result == "bp_cred"+async def test_setup_credential_opt_out_resolves_fresh(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(app.WORKFLOW_CONTEXT_MANAGER, "workflow_run_contexts", {})+ monkeypatch.setattr(+ app.DATABASE.credentials,+ "get_credential",+ AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_cred", auto_profile_disabled=True)),+ )+ get_browser_profile = AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_cred"))+ monkeypatch.setattr(app.DATABASE.browser_sessions, "get_browser_profile", get_browser_profile)+ workflow = _workflow_with_blocks(_login_block("login", _credential_parameter("login", "cred_1")))++ result = await WorkflowService()._resolve_setup_credential_seed(+ workflow=workflow, # type: ignore[arg-type]+ workflow_run_id="wr_test",+ organization_id="o_test",+ engine_enabled=True,+ parameter_values={},+ )++ assert result is None+ get_browser_profile.assert_not_awaited()+++async def test_mid_run_credential_opt_out_is_engine_gated(monkeypatch: pytest.MonkeyPatch) -> None:+ svc = WorkflowService()+ monkeypatch.setattr(svc, "_resolve_login_block_credential_ids", AsyncMock(return_value=["cred_1"]))+ monkeypatch.setattr(+ app.DATABASE.credentials,+ "get_credential",+ AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_cred", auto_profile_disabled=True)),+ )+ get_browser_profile = AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_cred"))+ monkeypatch.setattr(app.DATABASE.browser_sessions, "get_browser_profile", get_browser_profile)+ engine_enabled = AsyncMock(side_effect=[True, False])+ monkeypatch.setattr(app.AGENT_FUNCTION, "is_browser_memory_engine_enabled", engine_enabled)+ workflow_run = SimpleNamespace(organization_id="o_test")+ block = SimpleNamespace(parameters=[])++ opted_out = await svc._resolve_login_block_browser_profile_id(+ block=block, # type: ignore[arg-type]+ workflow_run_id="wr_test",+ organization_id="o_test",+ workflow_permanent_id="wpid_test",+ workflow_run=workflow_run, # type: ignore[arg-type]+ )+ flag_off = await svc._resolve_login_block_browser_profile_id(+ block=block, # type: ignore[arg-type]+ workflow_run_id="wr_test",+ organization_id="o_test",+ workflow_permanent_id="wpid_test",+ workflow_run=workflow_run, # type: ignore[arg-type]+ )++ assert opted_out is None+ assert flag_off == "bp_cred"+ assert get_browser_profile.await_count == 1++@pytest.mark.asyncioasync def test_setup_credential_defers_when_multiple_login_blocks(monkeypatch: pytest.MonkeyPatch) -> None:get_credential = AsyncMock()@@ -910,7 +991,7 @@ async def test_setup_credential_best_effort_on_profile_lookup_failure(monkeypatcmonkeypatch.setattr(app.DATABASE.credentials,"get_credential",- AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_cred")),+ AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_cred", auto_profile_disabled=False)),)monkeypatch.setattr(app.DATABASE.browser_sessions,@@ -944,7 +1025,7 @@ async def test_setup_credential_resolves_pool_credential(monkeypatch: pytest.Mon"get_selection",AsyncMock(return_value="cred_pool"),)- get_credential = AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_pool"))+ get_credential = AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_pool", auto_profile_disabled=False))monkeypatch.setattr(app.DATABASE.credentials, "get_credential", get_credential)monkeypatch.setattr(app.DATABASE.browser_sessions,@@ -977,7 +1058,9 @@ async def test_setup_credential_resolves_fallback_db_selection(monkeypatch: pyte"get_selection",AsyncMock(return_value="cred_fallback"),)- get_credential = AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_fallback"))+ get_credential = AsyncMock(+ return_value=SimpleNamespace(browser_profile_id="bp_fallback", auto_profile_disabled=False)+ )monkeypatch.setattr(app.DATABASE.credentials, "get_credential", get_credential)monkeypatch.setattr(app.DATABASE.browser_sessions,@@ -1009,7 +1092,7 @@ async def test_setup_credential_uses_same_rich_resolver_as_block(monkeypatch: pymonkeypatch.setattr(app.DATABASE.credentials,"get_credential",- AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_rich")),+ AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_rich", auto_profile_disabled=False)),)monkeypatch.setattr(app.DATABASE.browser_sessions,@@ -1071,7 +1154,9 @@ async def test_setup_credential_prefers_request_value_over_default_for_workflow_# value lives only in the in-memory render params. Seeding must use it, not the parameter default, or a# cached-script run seeds the wrong account.monkeypatch.setattr(app.WORKFLOW_CONTEXT_MANAGER, "workflow_run_contexts", {})- get_credential = AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_request"))+ get_credential = AsyncMock(+ return_value=SimpleNamespace(browser_profile_id="bp_request", auto_profile_disabled=False)+ )monkeypatch.setattr(app.DATABASE.credentials, "get_credential", get_credential)monkeypatch.setattr(app.DATABASE.browser_sessions,@@ -1102,7 +1187,7 @@ async def test_setup_credential_resolves_dereferenced_binding_for_fallback_param# yet, the rich resolver would return the raw reference; setup must use the dereferenced value.monkeypatch.setattr(app.WORKFLOW_CONTEXT_MANAGER, "workflow_run_contexts", {})monkeypatch.setattr(app.DATABASE.workflow_run_credential_selections, "get_selection", AsyncMock(return_value=None))- get_credential = AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_deref"))+ get_credential = AsyncMock(return_value=SimpleNamespace(browser_profile_id="bp_deref", auto_profile_disabled=False))monkeypatch.setattr(app.DATABASE.credentials, "get_credential", get_credential)monkeypatch.setattr(app.DATABASE.browser_sessions,diff --git a/tests/unit/test_task_v2_completion_gate.py b/tests/unit/test_task_v2_completion_gate.pyindex db879c862..ed12573b0 100644--- a/tests/unit/test_task_v2_completion_gate.py+++ b/tests/unit/test_task_v2_completion_gate.py@@ -7,18 +7,111 @@ from __future__ import annotationsimport hashlibfrom datetime import UTC, datetime, timezonefrom types import SimpleNamespace+from typing import Anyfrom unittest.mock import AsyncMock, MagicMockimport pytest-from skyvern.config import Settings, settings+from skyvern.config import Settingsfrom skyvern.forge.sdk.prompting import PromptEnginefrom skyvern.forge.sdk.schemas.task_v2 import TaskV2Statusfrom skyvern.forge.sdk.workflow.models.workflow import WorkflowRunStatus-from skyvern.services import task_v2_service+from skyvern.services import planner_levers, task_v2_servicefrom skyvern.services.task_v2_service import _should_run_post_block_completion_check+class _TaskV2WiringObserved(Exception):+ pass+++async def _run_task_v2_wiring_case(+ monkeypatch: pytest.MonkeyPatch,+ iterations: list[int],+ *,+ shared_flag_enabled: bool,+ planner_response: dict[str, Any] | None = None,+ max_iterations: int | None = None,+) -> tuple[list[dict[str, Any]], AsyncMock]:+ completion_case = len(iterations) == 1+ organization = SimpleNamespace(organization_id="org_test", max_steps_per_run=None)+ task_v2 = MagicMock(observer_cruise_id="t", status=TaskV2Status.queued, prompt="goal")+ task_v2.url, task_v2.workflow_run_id = "https://example.test", "wr_test"+ queued_run = MagicMock(workflow_run_id="wr_test", workflow_id="wf_test", status=WorkflowRunStatus.queued)+ running_run = MagicMock(workflow_run_id="wr_test", workflow_id="wf_test", status=WorkflowRunStatus.running)+ workflow = MagicMock(workflow_id="wf_test", workflow_permanent_id="wp_test", title="Test")+ block_result = MagicMock(failure_reason=None, output_parameter_value={}, success=completion_case)+ block = SimpleNamespace(execute_safe=AsyncMock(return_value=block_result))+ scraped_page = SimpleNamespace(screenshots=[])+ browser_state = MagicMock(engine_selection=None)+ browser_state.get_working_page = AsyncMock(return_value=MagicMock())+ browser_state.validate_browser_context = AsyncMock(return_value=True)+ browser_state.scrape_website = AsyncMock(return_value=scraped_page)+ test_app = MagicMock()+ test_app.scrape_exclude = []+ test_app.WORKFLOW_SERVICE.get_workflow_run = AsyncMock(side_effect=[queued_run, *[running_run for _ in iterations]])+ test_app.WORKFLOW_SERVICE.get_workflow = AsyncMock(return_value=workflow)+ test_app.WORKFLOW_SERVICE.mark_workflow_run_as_running = AsyncMock()+ test_app.WORKFLOW_SERVICE.create_workflow_from_request = AsyncMock(return_value=workflow)+ test_app.BROWSER_MANAGER.get_or_create_for_workflow_run = AsyncMock(return_value=browser_state)+ test_app.AGENT_FUNCTION.has_code_block_access = AsyncMock(return_value=False)+ test_app.AGENT_FUNCTION.validate_task_execution = AsyncMock()+ test_app.AGENT_FUNCTION.cleanup_element_tree_factory = MagicMock(return_value=MagicMock())+ test_app.DATABASE.observer.update_task_v2 = AsyncMock(return_value=task_v2)+ test_app.DATABASE.observer.create_thought = AsyncMock(return_value=SimpleNamespace(observer_thought_id="thought"))+ test_app.DATABASE.observer.update_thought = AsyncMock()+ test_app.DATABASE.tasks.get_tasks_by_workflow_run_id = AsyncMock(+ side_effect=_TaskV2WiringObserved if completion_case else None,+ return_value=[],+ )+ test_app.DATABASE.tasks.get_total_unique_step_order_count_by_task_ids = AsyncMock(return_value=0)+ provider = AsyncMock(return_value=shared_flag_enabled)+ planner_inputs: list[dict[str, Any]] = []+ planner_response = planner_response or dict(+ user_goal_achieved=False, should_terminate=False, plan="continue", task_type="navigate"+ )+ llm_handler = AsyncMock(return_value=planner_response)++ def capture_planner_inputs(*_args: object, **kwargs: Any) -> str:+ planner_inputs.append(kwargs)+ if not completion_case and len(planner_inputs) == len(iterations):+ raise _TaskV2WiringObserved+ return "prompt"++ context = SimpleNamespace(run_id=None, root_workflow_run_id=None, tz_info=timezone.utc)+ monkeypatch.setattr(task_v2_service, "app", test_app)+ monkeypatch.setattr(planner_levers.app.EXPERIMENTATION_PROVIDER, "is_feature_enabled_cached", provider)+ monkeypatch.setattr(planner_levers.settings, "TASK_V2_CONVERGE_PCT", 0)+ monkeypatch.setattr(planner_levers.settings, "TASK_V2_CARRY_SUBGOALS", False)+ monkeypatch.setattr(planner_levers.settings, "TASK_V2_SKIP_COMPLETION_CHECK_AFTER_NAVIGATE", False)+ monkeypatch.setattr(task_v2_service.skyvern_context, "ensure_context", lambda: context)+ monkeypatch.setattr(task_v2_service, "initialize_task_v2_metadata", AsyncMock(return_value=task_v2))+ monkeypatch.setattr(task_v2_service, "_set_up_workflow_context", AsyncMock())+ monkeypatch.setattr(task_v2_service, "_resolve_max_iterations", lambda _override: max_iterations or len(iterations))+ monkeypatch.setattr(task_v2_service, "range", lambda _max_iterations: iterations, raising=False)+ monkeypatch.setattr(+ task_v2_service,+ "_is_planner_mini_goal_improvements_enabled",+ AsyncMock(return_value=completion_case),+ )+ monkeypatch.setattr(task_v2_service, "build_open_tabs_context", AsyncMock(return_value=None))+ monkeypatch.setattr(task_v2_service, "load_prompt_with_elements", capture_planner_inputs)+ monkeypatch.setattr(task_v2_service, "_get_task_v2_llm_api_handler", lambda _task_v2: llm_handler)+ monkeypatch.setattr(task_v2_service.SkyvernFrame, "get_url", AsyncMock(return_value=task_v2.url))+ monkeypatch.setattr(task_v2_service, "_generate_navigation_task", AsyncMock(return_value=(block, [], [])))+ monkeypatch.setattr(task_v2_service, "WorkflowDefinitionYAML", MagicMock())+ monkeypatch.setattr(task_v2_service, "WorkflowCreateYAMLRequest", MagicMock())+ monkeypatch.setattr(task_v2_service, "runtime_proxy_location", lambda _location: None)+ monkeypatch.setattr(task_v2_service, "_get_extracted_data_from_block_result", lambda *_args, **_kwargs: None)+ monkeypatch.setattr(task_v2_service, "handle_block_result", AsyncMock(return_value=running_run))++ with pytest.raises(_TaskV2WiringObserved):+ await task_v2_service.run_task_v2_helper(+ organization, task_v2, max_iterations_override=max_iterations or len(iterations)+ )++ return planner_inputs, llm_handler++def test_skip_after_navigate_ships_off() -> None:assert Settings.model_fields["TASK_V2_SKIP_COMPLETION_CHECK_AFTER_NAVIGATE"].default is False@@ -341,10 +434,40 @@ async def test_planner_mini_goal_improvements_resolver_falls_back_when_provider_assert await task_v2_service._is_planner_mini_goal_improvements_enabled("org_test") is True[email protected]("lever_enabled", [True, False])+async def test_completion_gate_resolver_controls_post_navigate_check(+ lever_enabled: bool,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ _, llm_handler = await _run_task_v2_wiring_case(+ monkeypatch,+ [0],+ shared_flag_enabled=lever_enabled,+ planner_response={+ "user_goal_achieved": False,+ "should_terminate": False,+ "plan": "continue",+ "task_type": "navigate",+ "complete_criterion": "the navigation succeeded",+ },+ )++ prompt_names = [call.kwargs["prompt_name"] for call in llm_handler.await_args_list]+ assert ("task_v2_check_completion" in prompt_names) is not lever_enabled++@pytest.mark.parametrize("task_type", ["navigate", "extract", "loop"])-def test_default_off_runs_check_for_every_block_type(task_type: str, monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(settings, "TASK_V2_SKIP_COMPLETION_CHECK_AFTER_NAVIGATE", False)- assert _should_run_post_block_completion_check(True, task_type, navigate_completion_check_enabled=True) is True+def test_default_off_runs_check_for_every_block_type(task_type: str) -> None:+ assert (+ _should_run_post_block_completion_check(+ True,+ task_type,+ navigate_completion_check_enabled=True,+ skip_completion_check_after_navigate=False,+ )+ is True+ )@pytest.mark.parametrize(@@ -360,27 +483,26 @@ def test_flag_on_skips_only_navigate_with_embedded_check(task_type: str,navigate_completion_check_enabled: bool,expected: bool,- monkeypatch: pytest.MonkeyPatch,) -> None:- monkeypatch.setattr(settings, "TASK_V2_SKIP_COMPLETION_CHECK_AFTER_NAVIGATE", True)assert (_should_run_post_block_completion_check(True,task_type,navigate_completion_check_enabled=navigate_completion_check_enabled,+ skip_completion_check_after_navigate=True,)is expected)@pytest.mark.parametrize("block_success", [False, None])-def test_no_check_when_block_did_not_succeed(block_success: bool | None, monkeypatch: pytest.MonkeyPatch) -> None:- monkeypatch.setattr(settings, "TASK_V2_SKIP_COMPLETION_CHECK_AFTER_NAVIGATE", True)+def test_no_check_when_block_did_not_succeed(block_success: bool | None) -> None:assert (_should_run_post_block_completion_check(block_success,"extract",navigate_completion_check_enabled=False,+ skip_completion_check_after_navigate=True,)is False)diff --git a/tests/unit/test_task_v2_converge_prompt.py b/tests/unit/test_task_v2_converge_prompt.pyindex c55246c0c..3d6c40771 100644--- a/tests/unit/test_task_v2_converge_prompt.py+++ b/tests/unit/test_task_v2_converge_prompt.py@@ -1,12 +1,26 @@+import pytest+from skyvern.config import Settingsfrom skyvern.forge.prompts import prompt_engine+from skyvern.services import planner_leversfrom skyvern.services.task_v2_service import _converge_iterations_remaining+from tests.unit.test_task_v2_completion_gate import _run_task_v2_wiring_casedef test_converge_ships_disabled() -> None:assert Settings.model_fields["TASK_V2_CONVERGE_PCT"].default == 0+async def test_converge_resolver_drives_final_window_in_planner_inputs(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(planner_levers.settings, "TASK_V2_CONVERGE_PCT", 0)+ planner_inputs, _ = await _run_task_v2_wiring_case(+ monkeypatch, [39, 40], shared_flag_enabled=True, max_iterations=50+ )++ assert [inputs["iterations_remaining"] for inputs in planner_inputs] == [None, 10]++def _render(iterations_remaining: int | None) -> str:return prompt_engine.load_prompt("task_v2",diff --git a/tests/unit/test_worker_cdp_frame_publisher.py b/tests/unit/test_worker_cdp_frame_publisher.pyindex 29be61dea..db99d3a46 100644--- a/tests/unit/test_worker_cdp_frame_publisher.py+++ b/tests/unit/test_worker_cdp_frame_publisher.py@@ -681,3 +681,21 @@ async def test_gated_skip_does_not_advance_dedupe_digest(streaming_temp_dir: Patawait _drive_publish_once(pub)assert pub._last_published_digest is not Noneassert fake_storage.await_count == 2+++async def test_start_writes_remote_browser_sentinel_and_stop_leaves_it(streaming_temp_dir: Path) -> None:+ pub = CDPFramePublisher(+ browser_state=_make_browser_state(None, connected=False),+ stream_key=STREAM_KEY,+ organization_id=ORG_ID,+ )++ await pub.start()+ sentinel = streaming_temp_dir / ORG_ID / f"{STREAM_KEY}.remote"+ assert sentinel.exists()++ # Deliberately left in place: the display never shows a remote browser, so a dead+ # publisher must not let display capture resume publishing black frames.+ await pub.stop()+ assert sentinel.exists()diff --git a/tests/unit/test_workflow_service_profile_writeback.py b/tests/unit/test_workflow_service_profile_writeback.pyindex 84852910d..85c65b86b 100644--- a/tests/unit/test_workflow_service_profile_writeback.py+++ b/tests/unit/test_workflow_service_profile_writeback.py@@ -15,6 +15,7 @@ from unittest.mock import AsyncMock, MagicMockimport pytestfrom skyvern.forge import app+from skyvern.forge.sdk.schemas.credentials import credential_auto_profile_disabledfrom skyvern.forge.sdk.workflow.browser_profile_key import build_workflow_browser_session_storage_keyfrom skyvern.forge.sdk.workflow.models.workflow import WorkflowRunStatusfrom skyvern.forge.sdk.workflow.service import WorkflowService@@ -61,6 +62,12 @@ def _make_browser_state(applied_browser_profile_id: str | None = None) -> MagicMreturn bs+def test_auto_profile_opt_out_predicate_is_explicit_true_only() -> None:+ assert credential_auto_profile_disabled(SimpleNamespace(auto_profile_disabled=True)) is True+ assert credential_auto_profile_disabled(SimpleNamespace(auto_profile_disabled=False)) is False+ assert credential_auto_profile_disabled(SimpleNamespace(auto_profile_disabled=None)) is False++def _patch_clean_up_deps(monkeypatch: pytest.MonkeyPatch, browser_state: MagicMock) -> AsyncMock:"""Patch all external dependencies of clean_up_workflow. Returns the store mock."""store_mock = AsyncMock()diff --git a/tests/unit/workflow/test_file_parser_block.py b/tests/unit/workflow/test_file_parser_block.pyindex d3165117b..e512bac61 100644--- a/tests/unit/workflow/test_file_parser_block.py+++ b/tests/unit/workflow/test_file_parser_block.py@@ -9,16 +9,20 @@ from __future__ import annotationsimport asyncioimport json+import time+from collections.abc import Awaitable+from contextlib import suppressfrom datetime import datetime, timezonefrom pathlib import Pathfrom typing import Anyfrom unittest.mock import AsyncMock, MagicMockimport docx+import pandas as pdimport pytestfrom skyvern.forge.sdk.api.llm.exceptions import InvalidLLMResponseFormat-from skyvern.forge.sdk.workflow.exceptions import InvalidFileType+from skyvern.forge.sdk.workflow.exceptions import FileParseTimeout, InvalidFileTypefrom skyvern.forge.sdk.workflow.models.block import BlockType, FileParserBlock, PDFParserBlockfrom skyvern.forge.sdk.workflow.models.parameter import OutputParameter, ParameterTypefrom skyvern.schemas.workflows import BlockResult, BlockStatus, FileType@@ -55,6 +59,14 @@ def _make_pdf_parser_block(file_url: str) -> PDFParserBlock:)+def _mock_workflow_run_context() -> MagicMock:+ ctx = MagicMock()+ ctx.has_parameter.return_value = False+ ctx.has_value.return_value = False+ ctx.register_output_parameter_value_post_execution = AsyncMock()+ return ctx++def _create_docx(path: Path,paragraphs: list[str] | None = None,@@ -778,3 +790,282 @@ class TestOcrPdfPages:assert handler.await_count == 2assert "page A" in result and "page B" in result+++BLOCKING_PARSE_SECONDS = 0.3+TICK_INTERVAL_SECONDS = 0.01+++async def _count_event_loop_ticks_during(awaitable: Awaitable[Any]) -> tuple[Any, int]:+ """Await ``awaitable`` while a sibling task ticks; returns (result, tick_count).++ Zero ticks means the awaited work held the event loop for its entire duration — the+ same starvation that stops the workflow activity's heartbeat task.+ """+ ticks = 0+ stop = asyncio.Event()++ async def ticker() -> None:+ nonlocal ticks+ while not stop.is_set():+ await asyncio.sleep(TICK_INTERVAL_SECONDS)+ ticks += 1++ ticker_task = asyncio.create_task(ticker())+ # Let the ticker reach its first await before the blocking work starts.+ await asyncio.sleep(0)+ try:+ result = await awaitable+ finally:+ stop.set()+ ticker_task.cancel()+ with suppress(asyncio.CancelledError):+ await ticker_task+ return result, ticks+++def _blocking(return_value: Any = None, seconds: float = BLOCKING_PARSE_SECONDS) -> Any:+ def _call(*args: Any, **kwargs: Any) -> Any:+ time.sleep(seconds)+ return return_value++ return _call+++def _cpu_bound(return_value: Any = None, seconds: float = BLOCKING_PARSE_SECONDS) -> Any:+ """Like _blocking, but burns CPU in pure Python so it holds the GIL.++ time.sleep releases the GIL; the PDF text extractors do not. Only a busy loop shows+ that offloading keeps the loop responsive against the real workload.+ """++ def _call(*args: Any, **kwargs: Any) -> Any:+ deadline = time.time() + seconds+ while time.time() < deadline:+ pass+ return return_value++ return _call+++class TestParsingDoesNotBlockEventLoop:+ """A slow parse must not hold the event loop.++ The workflow activity heartbeats from an asyncio task on the same loop. Parsing+ synchronously starves that task, and Temporal reaps the whole run once the heartbeat+ timeout elapses, so every blocking parse step has to run off the loop.+ """++ async def test_pdf_text_extraction_yields_to_event_loop(self) -> None:+ block = _make_file_parser_block("https://example.com/large.pdf", FileType.PDF)++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(+ "skyvern.forge.sdk.workflow.models.block.extract_pdf_file",+ _blocking("extracted text"),+ )+ text, ticks = await _count_event_loop_ticks_during(block._parse_pdf_file("/tmp/large.pdf"))++ assert text == "extracted text"+ assert ticks > 0++ async def test_gil_holding_pdf_extraction_yields_to_event_loop(self) -> None:+ """The extractors are pure Python, so they hold the GIL for the whole parse."""+ block = _make_file_parser_block("https://example.com/large.pdf", FileType.PDF)++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(+ "skyvern.forge.sdk.workflow.models.block.extract_pdf_file",+ _cpu_bound("extracted text"),+ )+ text, ticks = await _count_event_loop_ticks_during(block._parse_pdf_file("/tmp/large.pdf"))++ assert text == "extracted text"+ assert ticks > 0++ async def test_docx_parse_yields_to_event_loop(self) -> None:+ block = _make_file_parser_block("https://example.com/large.docx", FileType.DOCX)+ document = MagicMock(paragraphs=[], tables=[])++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr("skyvern.forge.sdk.workflow.models.block.docx.Document", _blocking(document))+ _, ticks = await _count_event_loop_ticks_during(block._parse_docx_file("/tmp/large.docx"))++ assert ticks > 0++ async def test_excel_parse_yields_to_event_loop(self) -> None:+ block = _make_file_parser_block("https://example.com/large.xlsx", FileType.EXCEL)++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(+ "skyvern.forge.sdk.workflow.models.block.pd.read_excel",+ _blocking(pd.DataFrame([{"name": "Alice"}])),+ )+ rows, ticks = await _count_event_loop_ticks_during(block._parse_excel_file("/tmp/large.xlsx"))++ assert rows == [{"name": "Alice"}]+ assert ticks > 0++ async def test_csv_parse_yields_to_event_loop(self, tmp_path: Path) -> None:+ block = _make_file_parser_block("https://example.com/large.csv", FileType.CSV)+ csv_path = tmp_path / "large.csv"+ csv_path.write_text("name,city\nAlice,Denver\n")++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(FileParserBlock, "_sniff_csv_delimiter", _blocking((",", "utf-8")))+ rows, ticks = await _count_event_loop_ticks_during(block._parse_csv_file(str(csv_path)))++ assert rows == [{"name": "Alice", "city": "Denver"}]+ assert ticks > 0++ async def test_pdf_page_rendering_yields_to_event_loop(self) -> None:+ """The scanned-PDF fallback renders every page to PNG before any LLM call."""+ block = _make_file_parser_block("https://example.com/scan.pdf", FileType.PDF)++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr("skyvern.forge.sdk.workflow.models.block.extract_pdf_file", lambda *a, **kw: "")+ mp.setattr(+ "skyvern.forge.sdk.workflow.models.block.render_pdf_pages_as_images",+ _blocking([]),+ )+ _, ticks = await _count_event_loop_ticks_during(block._parse_pdf_file("/tmp/scan.pdf"))++ assert ticks > 0++ async def test_execute_validation_yields_to_event_loop(self) -> None:+ """Validation opens the document too, so it is as slow as the parse on a large file."""+ block = _make_file_parser_block("https://example.com/large.pdf", FileType.PDF)+ mock_ctx = _mock_workflow_run_context()++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(FileParserBlock, "get_workflow_run_context", lambda *a, **kw: mock_ctx)+ mp.setattr(FileParserBlock, "format_potential_template_parameters", lambda *a, **kw: None)+ mp.setattr(+ "skyvern.forge.sdk.workflow.models.block.resolve_local_or_download_file",+ AsyncMock(return_value="/tmp/large.pdf"),+ )+ mp.setattr(FileParserBlock, "validate_file_type", _blocking(None))+ mp.setattr("skyvern.forge.sdk.workflow.models.block.extract_pdf_file", lambda *a, **kw: "text")+ mp.setattr(FileParserBlock, "record_output_parameter_value", AsyncMock())+ mp.setattr(FileParserBlock, "build_block_result", AsyncMock(return_value=MagicMock(success=True)))++ result, ticks = await _count_event_loop_ticks_during(+ block.execute(workflow_run_id="wr_test", workflow_run_block_id="wrb_test", organization_id="org_test")+ )++ assert result.success is True+ assert ticks > 0++ async def test_deprecated_pdf_parser_block_yields_to_event_loop(self) -> None:+ block = _make_pdf_parser_block("https://example.com/large.pdf")+ mock_ctx = _mock_workflow_run_context()++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(PDFParserBlock, "get_workflow_run_context", lambda *a, **kw: mock_ctx)+ mp.setattr(PDFParserBlock, "format_potential_template_parameters", lambda *a, **kw: None)+ mp.setattr(+ "skyvern.forge.sdk.workflow.models.block.resolve_local_or_download_file",+ AsyncMock(return_value="/tmp/large.pdf"),+ )+ mp.setattr(+ "skyvern.forge.sdk.workflow.models.block.extract_pdf_file",+ _blocking("extracted text"),+ )+ mp.setattr(+ "skyvern.forge.sdk.workflow.models.block.LLMAPIHandlerFactory.get_override_llm_api_handler",+ lambda *a, **kw: AsyncMock(return_value={"extracted_information": "ok"}),+ )+ mp.setattr(+ "skyvern.forge.sdk.workflow.models.block.prompt_engine.load_prompt",+ MagicMock(return_value="prompt"),+ )+ mp.setattr(PDFParserBlock, "record_output_parameter_value", AsyncMock())+ mp.setattr(PDFParserBlock, "build_block_result", AsyncMock(return_value=MagicMock(success=True)))++ _, ticks = await _count_event_loop_ticks_during(+ block.execute(workflow_run_id="wr_test", workflow_run_block_id="wrb_test", organization_id="org_test")+ )++ assert ticks > 0+++class TestParseStepTimeout:+ """A pathological document must fail its own block instead of consuming the whole run."""++ async def test_pdf_text_extraction_times_out(self) -> None:+ block = _make_file_parser_block("https://example.com/huge.pdf", FileType.PDF)++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr("skyvern.forge.sdk.workflow.models.block.FILE_PARSE_STEP_TIMEOUT_SECONDS", 0.05)+ mp.setattr("skyvern.forge.sdk.workflow.models.block.extract_pdf_file", _blocking("never returned"))++ with pytest.raises(FileParseTimeout) as exc_info:+ await block._parse_pdf_file("/tmp/huge.pdf")++ assert "huge.pdf" in str(exc_info.value)++ async def test_execute_records_timeout_as_block_failure(self) -> None:+ """The block fails with FILE_PARSER_ERROR; the run survives to its next block."""+ block = _make_file_parser_block("https://example.com/huge.pdf", FileType.PDF)+ mock_ctx = _mock_workflow_run_context()++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(FileParserBlock, "get_workflow_run_context", lambda *a, **kw: mock_ctx)+ mp.setattr(FileParserBlock, "format_potential_template_parameters", lambda *a, **kw: None)+ mp.setattr(+ "skyvern.forge.sdk.workflow.models.block.resolve_local_or_download_file",+ AsyncMock(return_value="/tmp/huge.pdf"),+ )+ mp.setattr(FileParserBlock, "validate_file_type", lambda *a, **kw: None)+ mp.setattr("skyvern.forge.sdk.workflow.models.block.FILE_PARSE_STEP_TIMEOUT_SECONDS", 0.05)+ mp.setattr("skyvern.forge.sdk.workflow.models.block.extract_pdf_file", _blocking("never returned"))+ mp.setattr(FileParserBlock, "record_output_parameter_value", AsyncMock())++ captured: dict[str, Any] = {}++ async def fake_build_block_result(*args: Any, **kwargs: Any) -> Any:+ captured.update(kwargs)+ return MagicMock(success=kwargs.get("success"))++ mp.setattr(FileParserBlock, "build_block_result", fake_build_block_result)++ result = await block.execute(+ workflow_run_id="wr_test", workflow_run_block_id="wrb_test", organization_id="org_test"+ )++ assert result.success is False+ assert captured["error_codes"] == ["FILE_PARSER_ERROR"]+ assert "timed out" in captured["failure_reason"].lower()++ async def test_validation_timeout_fails_the_block(self) -> None:+ block = _make_file_parser_block("https://example.com/huge.pdf", FileType.PDF)+ mock_ctx = _mock_workflow_run_context()++ with pytest.MonkeyPatch.context() as mp:+ mp.setattr(FileParserBlock, "get_workflow_run_context", lambda *a, **kw: mock_ctx)+ mp.setattr(FileParserBlock, "format_potential_template_parameters", lambda *a, **kw: None)+ mp.setattr(+ "skyvern.forge.sdk.workflow.models.block.resolve_local_or_download_file",+ AsyncMock(return_value="/tmp/huge.pdf"),+ )+ mp.setattr("skyvern.forge.sdk.workflow.models.block.FILE_PARSE_STEP_TIMEOUT_SECONDS", 0.05)+ mp.setattr(FileParserBlock, "validate_file_type", _blocking(None))+ mp.setattr(FileParserBlock, "record_output_parameter_value", AsyncMock())++ captured: dict[str, Any] = {}++ async def fake_build_block_result(*args: Any, **kwargs: Any) -> Any:+ captured.update(kwargs)+ return MagicMock(success=kwargs.get("success"))++ mp.setattr(FileParserBlock, "build_block_result", fake_build_block_result)++ result = await block.execute(+ workflow_run_id="wr_test", workflow_run_block_id="wrb_test", organization_id="org_test"+ )++ assert result.success is False+ assert "timed out" in captured["failure_reason"].lower()+ assert "file type validation" in captured["failure_reason"]