Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
SKY-15908: Let Copilot edit pending workflow drafts (#8485)
Andrew Neilson90dee482fc12
diff --git a/docs/changelog-emails/2026-08-august.md b/docs/changelog-emails/2026-08-august.mdindex 5b423c061..a9375669a 100644--- a/docs/changelog-emails/2026-08-august.md+++ b/docs/changelog-emails/2026-08-august.md@@ -6,117 +6,117 @@----## Workflow Copilot, Rebuilt+## Workflow Copilot Updates-The refreshed Copilot chat is now **the default everywhere**, in both the workflow editor and Workflow Studio, and it builds with more honesty and less friction:+The updated Copilot chat is now the default everywhere, including the workflow editor and Workflow Studio.-- **See what happened, not a plan**: build progress is a **flat activity log** of what Copilot looked at, wrote, and ran, with **code diffs for write steps** and narrator lines that explain steps in place-- **Fix one block and rerun**: Copilot can repair one block and rerun it in a single step, and when the browser is still on the edited block's page it re-runs only that block instead of replaying the login. When a code block fails, Copilot gets the recorded browser error, plus the screenshot and any blocking overlay when they were captured-- **Truthful review**: the Accept card shows exactly which blocks are added, changed, or unchanged, Accept and Reject show up **without a reload**, and a new **Test end-to-end** action runs the whole draft in a fresh browser-- **Fewer questions**: pick a connected Google account inline, choose credentials from a **searchable picker**, hand Copilot a saved login by name, and let it **solve captchas** and read **sign-in codes from a connected inbox** instead of stopping to ask+- Build progress uses a **flat activity log** of what Copilot looked at, wrote, and ran. Write steps show code diffs. Narrator lines explain steps in place.+- Copilot can **repair and rerun one block** in a single step. If the browser remains on that block's page, Copilot reruns only that block without replaying the login. After a code block fails, Copilot receives the recorded browser error. It also receives the screenshot and any blocking overlay when they were captured.+- The Accept card lists added, changed, and unchanged blocks. Accept and Reject appear without a reload. The new **Test end-to-end** action runs the whole draft in a fresh browser.+- Select a connected Google account inline or choose credentials from a **searchable picker**. You can give Copilot a saved login by name. Copilot solves captchas and reads sign-in codes from a connected inbox without stopping to ask.-Copilot also learned the human-approval block, knows today's date, keeps the page facts it gathered across long sessions, and stops cleanly when you press the new stop button.+Copilot supports the human-approval block and knows today's date. It retains gathered page facts across long sessions. The new stop button ends the current Copilot run.-+---## Skyvern 3.0 Engine (Beta)-Meet the next agent engine. **Skyvern 3.0** drives the browser through a persistent tool loop, and only actions count against your step budget, never perception turns. It is in beta, and you can opt in today:+The opt-in **Skyvern 3.0** beta controls the browser through a persistent tool loop. Only actions count against your step budget. Perception turns do not count.-- **Pin a block to it**: pick **Skyvern 3.0** (marked Beta) in the engine selector on task, navigation, login, action, and extraction blocks, or pass `engine="skyvern-3.0"` on a task run-- **On-demand vision**: when the page's text digest has gaps, the agent takes a **marked-up screenshot** and acts on the numbered marks-- **Logins and challenges built in**: it solves captchas (including hCaptcha), resolves **TOTP and emailed one-time codes**, and follows **emailed magic links** without the link ever reaching the model-- **Form-savvy**: custom comboboxes, click-to-open selects, typeaheads, and shadow-DOM controls commit in one action, it checks that file uploads and toggles took effect instead of assuming, and it waits for the page to settle before calling a block complete+- Select Skyvern 3.0 (marked Beta) in the engine selector for task, navigation, login, action, and extraction blocks. You can also pass `engine="skyvern-3.0"` on a task run.+- When the page's text digest has gaps, the agent takes a **marked-up screenshot** and acts on its numbered marks.+- The engine solves captchas, including hCaptcha, and resolves **TOTP and emailed one-time codes**. It follows emailed magic links without those links reaching the model.+- Custom comboboxes, click-to-open selects, typeaheads, and shadow-DOM controls commit in **one action**. The engine verifies file uploads and toggle changes. It waits for the page to settle before completing a block.-Run replay and typed action history are supported on the new engine.+The engine supports run replay and typed action history.----## Drive Your Own Chrome from MCP+## Control Chrome through MCP-Your real, logged-in browser is now a target. The new **Skyvern Agent** Chrome extension (on the Chrome Web Store) lets Skyvern MCP drive **your own Chrome**, existing sessions, cookies, and profiles included, with `BROWSER_TYPE=extension-connect`. You share only the tabs you choose, pairing takes **one approval** and persists per workstation, and **several coding agents can share one Chrome** at the same time.+The new **Skyvern Agent** Chrome extension is available on the Chrome Web Store. It lets Skyvern MCP control your logged-in Chrome through `BROWSER_TYPE=extension-connect`. This includes existing sessions, cookies, and profiles. You share only the tabs you choose. Pairing requires one approval and persists per workstation. Several coding agents can share one Chrome at the same time.-The MCP surface grew up too:+You can select MCP scopes at install time or on the hosted URL.-- **Tool scopes**: choose `--scope all|operate|build|browser` at install, or pick a scope on the hosted URL, plus an opt-in **`lean`** scope of browser-only tools-- **New tools** in the `lean` and `all` scopes: `skyvern_page` reads a large page with cursor pagination, `skyvern_extract_structured` returns **schema-validated** results, and `skyvern_finish` declares a task's final outcome-- **Hosted MCP**: add Skyvern as a custom connector from **Claude.ai and Claude Desktop**, renew access silently with refresh tokens, and connect to the organization you picked at sign-in+- Set `--scope all|operate|build|browser` during installation. You can also opt into the **`lean`** scope for browser-only tools.+- The `lean` and `all` scopes include new tools. `skyvern_page` reads large pages with cursor pagination. `skyvern_extract_structured` returns schema-validated results. `skyvern_finish` declares a task's final outcome.+- Add Skyvern as a custom connector in **Claude.ai and Claude Desktop** through hosted MCP. Refresh tokens renew access without prompting you. Hosted MCP connects to the organization you picked at sign-in.-+----## Credentials: 1Password, Passkeys & More+## Credentials and Login Updates-Logins keep getting easier to wire up and harder to break:+The Login block now lists 1Password items in its credential dropdown.-- **1Password in the Login block**: the credential dropdown now lists your 1Password items and binds one directly, a five-step guide walks you through the service account token, and items that keep one-time codes in a plain field can name a **custom TOTP field**-- **Passkey 2FA for every cloud organization**, plus **Outlook** as a source for email two-factor codes-- **Password-less and at-will credentials**: store a credential with a username but no password, and leave a workflow credential's default blank so it **runs unauthenticated unless you pass one at run time**-- **Per-credential controls**: run workflows **sequentially per credential** (scheduled runs included), and Save & Reuse Session keeps **one browser profile per rotating credential**+- Bind a **1Password** item directly from the Login block's credential dropdown. A five-step guide explains the service account token. Items that store one-time codes in a plain field can specify a custom TOTP field.+- **Passkey 2FA** is available for every cloud organization. You can use Outlook as a source for email two-factor codes.+- Store a credential with a username and no password. Leave a workflow credential's default blank to **run unauthenticated unless you pass one at run time**.+- Run workflows **sequentially per credential**, including scheduled runs. Save & Reuse Session keeps one browser profile per rotating credential.-Under the hood, magic-link and OTP values are stored exactly as received, code blocks and cached scripts can complete an emailed sign-in with a new `magic_link` verb, Bitwarden credential fetches are faster and more reliable, and OTP logins use their full 15-minute polling budget.+Skyvern stores magic-link and OTP values exactly as received. Code blocks and cached scripts can complete an emailed sign-in with the new `magic_link` verb. Bitwarden updates reduce credential fetch time and failures. OTP logins use their full 15-minute polling budget.-+----## Files, Email & Storage, Your Way+## Files, Email & Storage-Bring your own infrastructure:+S3-compatible storage endpoints must use public HTTPS by default. Self-hosted deployments can allow internal hosts.-- **S3-compatible storage**: point the File Upload and File Download blocks at your own S3-compatible storage with a new `endpoint_url` field (public HTTPS endpoints by default; self-hosted deployments can allow internal hosts)-- **Custom SMTP**: the Send Email block can send from **your own mail server** under Advanced Settings-- **File retention you control**: set `retention_days` when you upload a file with `POST /v1/upload_file`, delete any upload with `DELETE /v1/files/{id}`, or attach uploads to a run with `file_ids` and let Skyvern delete them when the run ends-- **Google, done right**: File Download uses your connected Drive account for **private Drive files**, the Drive folder ID is optional (blank saves to My Drive root), Google Sheets appends land in the **column you asked for**, and Sheets, Drive, Docs, and Gmail blocks resolve a connection **by name or email**+- Set the new **`endpoint_url`** field on File Upload and File Download blocks to use your S3-compatible storage.+- Configure **Custom SMTP** under Advanced Settings to send from your own mail server through the Send Email block.+- Set **`retention_days`** when uploading a file with `POST /v1/upload_file`. Delete any upload with `DELETE /v1/files/{id}`. Attach uploads to a run with `file_ids` to have Skyvern delete them when the run ends.+- File Download uses your connected Drive account for private Drive files. The Drive folder ID is optional. Leave it blank to save to My Drive root. Google Sheets appends use the column you specify. Sheets, Drive, Docs, and Gmail blocks resolve connections **by name or email**.-The File Parser inside a loop can skip files it cannot parse, it follows Drive links behind confirm pages, and a slow parse of a huge document fails only its own block instead of the whole run.+Inside a loop, the File Parser can skip files it cannot parse. It follows Drive links behind confirm pages. A slow parse of a large document fails only its own block, not the whole run.-+---## Browser Sessions & Live View-Sessions that behave like a real desk:+Workflow runs can share one browser while retaining logged-in state and tabs.-- **Reuse one browser across runs**: the new `reuse_browser_session` workflow setting (with a per-run override) carries logged-in state and tabs from run to run-- **Live view, wider**: externally hosted browsers (US cloud) and session-backed runs now stream a live view, the full page height is reachable in take-control, and the view tells you when its stream stops instead of freezing-- **Friendlier API**: over-limit timeouts are **capped at 240 minutes** and reported in a `warning` field instead of rejected, and a closed session explains its status and what to do next+- Enable the new **`reuse_browser_session`** workflow setting. Each run can override the setting.+- Externally hosted browsers (US cloud) and session-backed runs now stream a **live view**. Take-control reaches the full page height. The view reports when its stream stops instead of freezing.+- The API **caps over-limit timeouts at 240 minutes** and reports them in a `warning` field instead of rejecting them. A closed session reports its status and what to do next.-Recordings from external providers are retained after teardown, and a renewal never shortens a session.+Recordings from external providers are retained after teardown. A renewal never shortens a session.----## Code Blocks That Explain Themselves+## Code Block Errors and Outputs-Code blocks got a real error contract and more honesty when things go wrong:+Run status and webhooks identify typed business errors from code blocks.-- **Typed business errors**: declare an `error_code_mapping` on a code block and `raise ErrorCode("out_of_stock", "why")` so run status and webhooks say **which condition failed**-- **Real failure facts**: a failed block reports the failing line, a plain-language next step, and the raised exception when redaction allows, and user-code and browser failures link to the **block screenshot** when one was captured-- **Outputs that show up**: run detail displays persisted code-block output instead of a disabled Outputs panel-- **Sandbox parity**: downloads, `page.url`, `Locator.or_`/`and_`, Python `class` statements, `page.frames`, and the timeouts you wrote all work in the sandboxed runner, and advisory **security-scan warnings** appear when you save code in Copilot or lint through MCP+- Declare **`error_code_mapping`** on a code block. Use `raise ErrorCode("out_of_stock", "why")` to identify the failed condition.+- A failed block reports the failing line and a plain-language next step. It includes the raised exception when redaction allows. User-code and browser failures link to the **block screenshot** when one was captured.+- Run detail displays **persisted code-block output** instead of a disabled Outputs panel.+- The sandboxed runner supports downloads, `page.url`, `Locator.or_`/`and_`, Python `class` statements, `page.frames`, and your configured timeouts. Advisory **security-scan warnings** appear when you save code in Copilot or lint through MCP.-+----## Analytics & Run History, Sharper+## Analytics & Run History-Better questions, better answers:+The run failure rate now excludes runs you cancelled.-- **Group by workflow tag** on the analytics dashboard, alongside the existing run-metadata grouping, for per-tag run and credit chargeback-- **Actions at a glance**: an **Actions** tile with action counts and failure rate joins the dashboard and the workflow health panel, and the run chart switches to a **Failures view** with agents ranked by failures first-- **Status that sticks**: choosing a run status scopes every chart, tile, table, and CSV export, carries into drill-downs, and accepts several statuses at once, and the failure rate now excludes runs you cancelled-- **Run history**: search and status filters persist in the URL, and `GET /runs` accepts a new `failure_category` filter for workflow runs+- **Group by workflow tag** on the analytics dashboard for per-tag run and credit chargeback. The existing run-metadata grouping remains available.+- An **Actions** tile shows action counts and failure rate on the dashboard and workflow health panel. The run chart switches to a **Failures view** with agents ranked by failures first.+- The selected **run status** applies to every chart, tile, table, and CSV export, including drill-downs. You can select several statuses at once.+- Run history search and status filters persist in the URL. `GET /runs` accepts the new `failure_category` filter for workflow runs.-+---@@ -124,60 +124,60 @@ Better questions, better answers:**New features**-- **Child runs in the workflow runs list** — `GET /v1/workflows/{workflow_id}/runs` and the MCP workflow run list accept `include_child_runs`-- **Send Email via API, SDK, and MCP** — `send_email` blocks no longer need platform SMTP parameters declared-- **Bulk agent export** — exporting several agents gives you a ZIP with one file per agent-- **Workflow schema view** — open a workflow's schema/YAML from the header menu in the classic workflow editor-- **Self-hosted stuck-run reaper** — deployments without a cron worker can enable an in-process reaper-- **Cloudflare challenge solving for self-hosted** — self-hosted deployments now get it from the bundled solver extension+- `GET /v1/workflows/{workflow_id}/runs` and the MCP workflow run list accept `include_child_runs` to list child runs.+- Send Email works through the API, SDK, and MCP without declaring platform SMTP parameters for `send_email` blocks.+- Export several agents as a ZIP with one file per agent.+- Open a workflow's schema/YAML from the header menu in the classic workflow editor.+- Self-hosted deployments without a cron worker can enable an in-process reaper for stuck runs.+- Self-hosted deployments now solve Cloudflare challenges through the bundled solver extension.**Improvements**-- Workflow Inputs are shown in a card where the flow starts, with the input count in the header-- Studio panes have clearer borders and simpler timeline controls, and the Run Timeline shows elapsed time, blocks, actions, and credits-- Paste a full workflow export into the editor's YAML pane-- New code blocks generated by browser recording, Browser Task steps, and PDF/SOP imports open in the new code block editor-- Delete confirmations use one consistent dialog across the app-- Run detail pages no longer freeze on multi-megabyte output values-- A workflow run with no blocks fails immediately with a specific reason, and an undefined for-loop parameter returns a clear 422-- Ordinary text inputs are filled atomically, and toggles and checkboxes respect their intended state-- Dropdown and custom-select fills verify the value actually committed instead of reporting a false success-- Runs recover from CDN access-denied interstitials and can click data-grid row checkboxes-- Prompts that overflow because of a huge page trim the element tree instead of failing the step-- Proxied stealth browser launches start about 30 seconds faster, and timezone and locale default to the proxy's region unless you set them-- Job-application fills on a major applicant-tracking platform stop fabricating values, commit dates and pickers correctly, recognize submit confirmation, and terminate fast on already-applied postings-- Revoked or rotated API keys stop working within 60 seconds-- The CLI refuses to send an API key to the cloud default unless `SKYVERN_BASE_URL` is set explicitly-- Concurrent SDK `run_action` calls are capped per organization with a 429 instead of degrading everyone-- The API answers 503 with `Retry-After` when the database connection drops, so SDK clients retry-- MCP tools return a structured `SESSION_EXPIRED` result, and large MCP responses are compacted while keeping identifiers and errors-- The standalone MCP server binds to localhost by default (`--host 0.0.0.0` to expose it)-- Connecting a Google integration asks only for that integration's scopes+- A card shows Workflow Inputs where the flow starts. Its header shows the input count.+- Studio makes pane borders more distinct and simplifies timeline controls. The Run Timeline shows elapsed time, blocks, actions, and credits.+- Paste a full workflow export into the editor's YAML pane.+- New code blocks from browser recording, Browser Task steps, and PDF/SOP imports open in the new code block editor.+- Delete confirmations use the same dialog across the app.+- Run detail pages no longer freeze on multi-megabyte output values.+- A workflow run with no blocks fails immediately with a specific reason. An undefined for-loop parameter returns a 422.+- Ordinary text inputs fill atomically. Toggles and checkboxes use their intended state.+- Dropdown and custom-select fills verify that the value committed instead of reporting a false success.+- Runs recover from CDN access-denied interstitials and can click data-grid row checkboxes.+- When a large page causes prompt overflow, the step trims the element tree instead of failing.+- Proxied stealth browser launches start about 30 seconds faster. Timezone and locale default to the proxy's region unless you set them.+- Job-application fills on a major applicant-tracking platform no longer fabricate values. They commit dates and pickers correctly and recognize submit confirmation. They stop early when a posting shows you already applied.+- Revoked or rotated API keys stop working within 60 seconds.+- The CLI refuses to send an API key to the cloud default unless `SKYVERN_BASE_URL` is set explicitly.+- The API caps concurrent SDK `run_action` calls per organization with a 429 to prevent degraded service for everyone.+- The API returns 503 with `Retry-After` when the database connection drops, so SDK clients retry.+- MCP tools return a structured `SESSION_EXPIRED` result. Large MCP responses are compacted while keeping identifiers and errors.+- The standalone MCP server binds to localhost by default (`--host 0.0.0.0` to expose it).+- Connecting a Google integration requests only that integration's scopes.**Bug fixes**-- Fixed scheduled runs failing for workflows whose credential is set to run sequentially-- Fixed a workflow parameter that matched a credential being typed into the page as `[REDACTED_SECRET]`-- Fixed long text typed into free-text fields keeping only its last ten characters-- Fixed saved browser profiles that failed to load, dropped the sign-in, or showed the "Restore pages?" bubble-- Fixed magic-link and OTP values being altered before use, and Gmail sign-in link emails not parsing-- Fixed Bitwarden logins without a two-factor secret failing to list or fill-- Fixed downloads lost on adopted, vendor-hosted, and reused browser sessions, in-page PDF blobs, and after a download closed the working page-- Fixed a downloaded file being materialized twice and uploaded as duplicates-- Fixed an aborted browser download being reported as a success with zero files-- Fixed webhook deliveries leaving from a different IP than the documented relay-- Fixed SDK and MCP actions against cloud browser sessions returning HTTP 500-- Fixed live view flickering to black, showing a black screen on session-backed runs, and frames stopping after a reconnect-- Fixed Delete and Backspace not working in the hosted browser live view-- Fixed browser session recordings not being saved after long sessions-- Fixed runs stuck in `queued` after a sequential gate deadline, and runs retrying forever when the workflow body raised-- Fixed saving a workflow with many cached scripts hanging for minutes-- Fixed selecting a block marking the workflow as unsaved, and the Inputs card opening Workflow Settings-- Fixed "Execute on any outcome" blocks being flagged as disconnected or swallowed by a conditional branch-- Fixed run and task view tabs blanking the content area, and shared Studio run links opening the wrong run-- Fixed the Add Credential modal not opening from the Credentials page-- Fixed the analytics dashboard counting cancelled runs as failures, and enterprise plans seeing always-zero credit tiles-- Fixed self-serve customers being able to start a duplicate subscription checkout-- Fixed self-hosted deployments failing to launch browsers, initialize encryption, or authenticate the UI behind a proxy-- Fixed the `skyvern doctor` check not recognizing `ENABLE_OPENAI_COMPATIBLE`-- Patched third-party dependency vulnerabilities+- Fixed scheduled runs failing for workflows whose credential is set to run sequentially.+- Fixed a workflow parameter that matched a credential being typed into the page as `[REDACTED_SECRET]`.+- Fixed long text typed into free-text fields keeping only its last ten characters.+- Fixed saved browser profiles that failed to load, dropped the sign-in, or showed the "Restore pages?" bubble.+- Fixed magic-link and OTP values changing before use. Fixed Gmail sign-in link emails failing to parse.+- Fixed Bitwarden logins without a two-factor secret failing to list or fill.+- Fixed downloads lost on adopted, vendor-hosted, and reused browser sessions. Fixed lost downloads from in-page PDF blobs and after a download closed the working page.+- Fixed a downloaded file being materialized twice and uploaded as duplicates.+- Fixed an aborted browser download reporting success with zero files.+- Fixed webhook deliveries leaving from a different IP than the documented relay.+- Fixed SDK and MCP actions against cloud browser sessions returning HTTP 500.+- Fixed live view flickering to black and showing a black screen on session-backed runs. Fixed frames stopping after a reconnect.+- Fixed Delete and Backspace not working in the hosted browser live view.+- Fixed browser session recordings failing to save after long sessions.+- Fixed runs stuck in `queued` after a sequential gate deadline. Fixed runs retrying indefinitely when the workflow body raised an exception.+- Fixed saving a workflow with many cached scripts hanging for minutes.+- Fixed selecting a block marking the workflow as unsaved. Fixed the Inputs card opening Workflow Settings.+- Fixed "Execute on any outcome" blocks being marked as disconnected or skipped by a conditional branch.+- Fixed run and task view tabs blanking the content area. Fixed shared Studio run links opening the wrong run.+- Fixed the Add Credential modal not opening from the Credentials page.+- Fixed the analytics dashboard counting cancelled runs as failures. Fixed enterprise plans seeing always-zero credit tiles.+- Fixed self-serve customers being able to start a duplicate subscription checkout.+- Fixed self-hosted deployments failing to launch browsers, initialize encryption, or authenticate the UI behind a proxy.+- Fixed the `skyvern doctor` check not recognizing `ENABLE_OPENAI_COMPATIBLE`.+- Patched third-party dependency vulnerabilities.diff --git a/docs/changelog-emails/images/2026-08-01-copilot-updates.png b/docs/changelog-emails/images/2026-08-01-copilot-updates.pngnew file mode 100644index 000000000..e587fe9f5Binary files /dev/null and b/docs/changelog-emails/images/2026-08-01-copilot-updates.png differdiff --git a/docs/changelog-emails/images/2026-08-01-copilot.png b/docs/changelog-emails/images/2026-08-01-copilot.pngdeleted file mode 100644index 6e2bc6ed1..000000000Binary files a/docs/changelog-emails/images/2026-08-01-copilot.png and /dev/null differdiff --git a/docs/changelog-emails/images/2026-08-03-bring-your-own-chrome.png b/docs/changelog-emails/images/2026-08-03-bring-your-own-chrome.pngdeleted file mode 100644index a9bd66fe1..000000000Binary files a/docs/changelog-emails/images/2026-08-03-bring-your-own-chrome.png and /dev/null differdiff --git a/docs/changelog-emails/images/2026-08-03-control-chrome-mcp.png b/docs/changelog-emails/images/2026-08-03-control-chrome-mcp.pngnew file mode 100644index 000000000..7b61e7d13Binary files /dev/null and b/docs/changelog-emails/images/2026-08-03-control-chrome-mcp.png differdiff --git a/docs/changelog-emails/images/2026-08-04-credentials-login-updates.png b/docs/changelog-emails/images/2026-08-04-credentials-login-updates.pngnew file mode 100644index 000000000..602f96e77Binary files /dev/null and b/docs/changelog-emails/images/2026-08-04-credentials-login-updates.png differdiff --git a/docs/changelog-emails/images/2026-08-04-credentials.png b/docs/changelog-emails/images/2026-08-04-credentials.pngdeleted file mode 100644index 116e8f2b6..000000000Binary files a/docs/changelog-emails/images/2026-08-04-credentials.png and /dev/null differdiff --git a/docs/changelog-emails/images/2026-08-05-files-email-storage-blocks.png b/docs/changelog-emails/images/2026-08-05-files-email-storage-blocks.pngnew file mode 100644index 000000000..3905ac4b2Binary files /dev/null and b/docs/changelog-emails/images/2026-08-05-files-email-storage-blocks.png differdiff --git a/docs/changelog-emails/images/2026-08-05-files-email-storage.png b/docs/changelog-emails/images/2026-08-05-files-email-storage.pngdeleted file mode 100644index 01a902337..000000000Binary files a/docs/changelog-emails/images/2026-08-05-files-email-storage.png and /dev/null differdiff --git a/docs/changelog-emails/images/2026-08-07-code-block-errors-outputs.png b/docs/changelog-emails/images/2026-08-07-code-block-errors-outputs.pngnew file mode 100644index 000000000..1a8c429f6Binary files /dev/null and b/docs/changelog-emails/images/2026-08-07-code-block-errors-outputs.png differdiff --git a/docs/changelog-emails/images/2026-08-07-code-blocks.png b/docs/changelog-emails/images/2026-08-07-code-blocks.pngdeleted file mode 100644index 068cefeb8..000000000Binary files a/docs/changelog-emails/images/2026-08-07-code-blocks.png and /dev/null differdiff --git a/docs/changelog-emails/images/2026-08-08-analytics-run-history-filters.png b/docs/changelog-emails/images/2026-08-08-analytics-run-history-filters.pngnew file mode 100644index 000000000..352eb6412Binary files /dev/null and b/docs/changelog-emails/images/2026-08-08-analytics-run-history-filters.png differdiff --git a/docs/changelog-emails/images/2026-08-08-analytics-run-history.png b/docs/changelog-emails/images/2026-08-08-analytics-run-history.pngdeleted file mode 100644index 05157b1a9..000000000Binary files a/docs/changelog-emails/images/2026-08-08-analytics-run-history.png and /dev/null differdiff --git a/fern/openapi/skyvern_openapi.json b/fern/openapi/skyvern_openapi.jsonindex 9cf5f7ee9..cc526959e 100644--- a/fern/openapi/skyvern_openapi.json+++ b/fern/openapi/skyvern_openapi.json@@ -290,6 +290,7 @@"anthropic_cua": "#/components/schemas/TaskRunResponse","ui_tars": "#/components/schemas/TaskRunResponse","yutori_navigator": "#/components/schemas/TaskRunResponse",+ "task_v3": "#/components/schemas/TaskRunResponse","workflow_run": "#/components/schemas/WorkflowRunResponse"}},@@ -19855,6 +19856,7 @@"enum": ["task_v1","task_v2",+ "task_v3","workflow_run","openai_cua","anthropic_cua",@@ -23075,6 +23077,7 @@"enum": ["task_v1","task_v2",+ "task_v3","openai_cua","anthropic_cua","ui_tars",diff --git a/pyproject.toml b/pyproject.tomlindex 42012ba85..e8917fc1d 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -1,6 +1,6 @@[project]name = "skyvern"-version = "1.0.53"+version = "1.0.52"description = ""authors = [{ name = "Skyvern AI", email = "[email protected]" }]requires-python = ">=3.11,<3.15"diff --git a/skyvern-frontend/src/routes/browserSessions/hooks/useProcessRecordingMutation.ts b/skyvern-frontend/src/routes/browserSessions/hooks/useProcessRecordingMutation.tsindex 4a22efdbe..67e0b889d 100644--- a/skyvern-frontend/src/routes/browserSessions/hooks/useProcessRecordingMutation.ts+++ b/skyvern-frontend/src/routes/browserSessions/hooks/useProcessRecordingMutation.ts@@ -103,6 +103,7 @@ const useProcessRecordingMutation = ({compressed_chunks: string[];draft_steps?: Array<RecordingDraftStep>;code_first: boolean;+ supports_credential_tokens: boolean;},{data: {@@ -114,6 +115,9 @@ const useProcessRecordingMutation = ({compressed_chunks: compressedChunks,workflow_permanent_id: workflowPermanentId,code_first: codeFirst,+ // This build substitutes credential tokens in a code block's code; a build that+ // does not must not be handed blocks whose code reads a token it cannot rename.+ supports_credential_tokens: true,...(draftSteps !== null ? { draft_steps: draftSteps } : {}),}).then((response) => ({diff --git a/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigForm/FileDownloadBlockForm.test.tsx b/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigForm/FileDownloadBlockForm.test.tsxindex 206ae38f4..f4f02e688 100644--- a/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigForm/FileDownloadBlockForm.test.tsx+++ b/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigForm/FileDownloadBlockForm.test.tsx@@ -511,6 +511,18 @@ describe("FileDownloadBlockForm (SKY-9361)", () => {).toBeDefined();});+ test("does not treat a URL containing the Drive hostname as a Drive link", () => {+ setFileDownloadNode("d1", {+ downloadTarget: "sftp",+ url: "https://example.com/drive.google.com/file/d/abc123/view",+ });+ render(<FileDownloadBlockForm blockId="d1" />);++ expect(+ screen.queryByText("Google Drive Source Account (Optional)"),+ ).toBeNull();+ });+test("selects a Google Drive source account for SFTP delivery", () => {setFileDownloadNode("d1", {downloadTarget: "sftp",diff --git a/skyvern-frontend/src/routes/workflows/editor/recording/applyRecordedBlocksToGraph.ts b/skyvern-frontend/src/routes/workflows/editor/recording/applyRecordedBlocksToGraph.tsindex 31be4662a..4efb3714a 100644--- a/skyvern-frontend/src/routes/workflows/editor/recording/applyRecordedBlocksToGraph.ts+++ b/skyvern-frontend/src/routes/workflows/editor/recording/applyRecordedBlocksToGraph.ts@@ -17,16 +17,40 @@ import { convertToNode, generateNodeLabel } from "../workflowEditorUtils";* The recorder cannot see the target workflow, so it keys each credential by its id — a* token, not a key. Allocation happens here, the one place the live parameter set is known:* a parameter that already wraps the same credential is reused, otherwise the next free- * auto-generated key is taken. Two recorded credentials can never land on one key, and a- * recorded credential can never shadow a different one the workflow already owns.+ * auto-generated key is taken.+ *+ * A recorded plain parameter is keyed from a field label, so it can read `credentials` and+ * collide with a credential key from either direction. Both are resolved here: a new+ * credential key never takes a recorded parameter's key, and a recorded parameter that lands+ * on a credential key is renamed. Sharing a key would make `str(key)` in the recorded code+ * stringify the credential — its password with it — into a page field.*/-function allocateCredentialKeys(+function nextFreeKey(base: string, taken: Set<string>): string {+ let suffix = 2;+ let candidate = `${base}_${suffix}`;+ while (taken.has(candidate)) {+ suffix += 1;+ candidate = `${base}_${suffix}`;+ }+ return candidate;+}++function allocateRecordedKeys(recordedParameters: Array<RecordedParameter>,existingParameters: ParametersState,): Map<string, string> {const keyByToken = new Map<string, string>();- const takenKeys = new Set(- existingParameters.map((parameter) => parameter.key),+ const recordedPlainKeys = recordedParameters+ .filter((parameter) => parameter.parameter_type !== "credential")+ .map((parameter) => parameter.key);+ const takenKeys = new Set([+ ...existingParameters.map((parameter) => parameter.key),+ ...recordedPlainKeys,+ ]);+ const credentialKeys = new Set(+ existingParameters+ .filter((parameter) => parameter.parameterType === "credential")+ .map((parameter) => parameter.key),);for (const parameter of recordedParameters) {@@ -39,12 +63,38 @@ function allocateCredentialKeys(const key =wrapper?.key ?? generateDefaultCredentialParameterKey([...takenKeys]);takenKeys.add(key);+ credentialKeys.add(key);keyByToken.set(parameter.key, key);}+ for (const key of recordedPlainKeys) {+ if (!credentialKeys.has(key)) {+ continue;+ }+ const renamed = nextFreeKey(key, takenKeys);+ takenKeys.add(renamed);+ keyByToken.set(key, renamed);+ }+return keyByToken;}+/**+ * One alternation over every token rather than a replacement per token: an allocated key can+ * itself be another token (a user-authored parameter may be named after a credential id), and+ * sequential passes would rewrite the first substitution again and point it at the wrong+ * credential.+ */+function tokenPattern(+ keyByToken: Map<string, string>,+ wrap: (alternation: string) => string,+): RegExp {+ const alternation = [...keyByToken.keys()]+ .map((token) => token.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))+ .join("|");+ return new RegExp(wrap(alternation), "g");+}+/*** A recorded secret fill carries the token inside its instruction text — the recorder writes* `Type 'API token' with {{ cred_123.secret_value }}.` — so the declaration and the reference@@ -55,11 +105,31 @@ function substituteGoalTokens(navigationGoal: string,keyByToken: Map<string, string>,): string {- let goal = navigationGoal;- for (const [token, key] of keyByToken) {- goal = goal.replace(new RegExp(`(\\{\\{\\s*)${token}\\b`, "g"), `$1${key}`);- }- return goal;+ const pattern = tokenPattern(+ keyByToken,+ (alternation) => `(\\{\\{\\s*)(?:${alternation})\\b`,+ );+ return navigationGoal.replace(pattern, (match, prefix: string) => {+ const key = keyByToken.get(match.slice(prefix.length));+ return key === undefined ? match : `${prefix}${key}`;+ });+}++/**+ * A recorded code block reads the credential through the token as a Python identifier —+ * `await page.locator("#pw").fill(cred_123.password)` — so the code moves with the+ * declaration. A credential id is never a substring of another identifier, so the+ * word-boundary match is exact.+ */+function substituteCodeTokens(+ code: string,+ keyByToken: Map<string, string>,+): string {+ const pattern = tokenPattern(+ keyByToken,+ (alternation) => `\\b(?:${alternation})\\b`,+ );+ return code.replace(pattern, (match) => keyByToken.get(match) ?? match);}function substituteCredentialTokens(@@ -67,18 +137,20 @@ function substituteCredentialTokens(keyByToken: Map<string, string>,): WorkflowBlock {const rename = (key: string) => keyByToken.get(key) ?? key;- // One structural cast rather than a switch over every block variant: only these three+ // One structural cast rather than a switch over every block variant: only these four// fields are touched, and they carry the same meaning on every variant that has them.const withParameters = block as WorkflowBlock & {parameters?: Array<{ key: string }>;parameter_keys?: Array<string>;navigation_goal?: string | null;+ code?: string;};if (!withParameters.parameters &&!withParameters.parameter_keys &&- !withParameters.navigation_goal+ !withParameters.navigation_goal &&+ !withParameters.code) {return block;}@@ -104,6 +176,9 @@ function substituteCredentialTokens(),}: {}),+ ...(withParameters.code+ ? { code: substituteCodeTokens(withParameters.code, keyByToken) }+ : {}),} as WorkflowBlock;}@@ -135,7 +210,7 @@ function applyRecordedBlocksToGraph({const newNodes: Array<AppNode> = [];const newEdges: Array<Edge> = [];- const credentialKeyByToken = allocateCredentialKeys(+ const credentialKeyByToken = allocateRecordedKeys(recordedParameters ?? [],existingParameters,);@@ -154,9 +229,15 @@ function applyRecordedBlocksToGraph({blocks.forEach((block, index) => {const id = nanoid();- const label = generateNodeLabel(existingLabels);+ const baseLabel = block.label || generateNodeLabel(existingLabels);+ let label = baseLabel;+ let suffix = 2;+ while (existingLabels.includes(label)) {+ label = `${baseLabel}_${suffix}`;+ suffix += 1;+ }existingLabels = [...existingLabels, label];- const blockWithLabel = { ...block, label: block.label || label };+ const blockWithLabel = { ...block, label };const node = convertToNode({ id, parentId: parent }, blockWithLabel, true);newNodes.push(node);diff --git a/skyvern-frontend/src/routes/workflows/editor/recording/useApplyRecordedBlocks.test.tsx b/skyvern-frontend/src/routes/workflows/editor/recording/useApplyRecordedBlocks.test.tsxindex 16b7c5f9b..989e799a1 100644--- a/skyvern-frontend/src/routes/workflows/editor/recording/useApplyRecordedBlocks.test.tsx+++ b/skyvern-frontend/src/routes/workflows/editor/recording/useApplyRecordedBlocks.test.tsx@@ -4,10 +4,14 @@ import { Edge } from "@xyflow/react";import { renderHook } from "@testing-library/react";import { afterEach, describe, expect, it, vi } from "vitest";-import { useRecordedBlocksStore } from "@/store/RecordedBlocksStore";+import {+ useRecordedBlocksStore,+ type RecordedParameter,+} from "@/store/RecordedBlocksStore";import { useWorkflowParametersStore } from "@/store/WorkflowParametersStore";import type { WorkflowBlock } from "@/routes/workflows/types/workflowTypes";import { AppNode } from "../nodes";+import { applyRecordedBlocksToGraph } from "./applyRecordedBlocksToGraph";import { useApplyRecordedBlocks } from "./useApplyRecordedBlocks";const initialRecordedBlocksState = useRecordedBlocksStore.getState();@@ -62,6 +66,38 @@ describe("useApplyRecordedBlocks", () => {expect(mergedEdges.length).toBeGreaterThan(0);});+ it("uniquifies a recorded label already used by the workflow", () => {+ const existing = {+ id: "existing",+ type: "codeBlock",+ data: { label: "log_in" },+ } as AppNode;+ const recorded = {+ block_type: "code",+ label: "log_in",+ code: "await page.wait_for_timeout(1000)",+ prompt: "",+ } as unknown as WorkflowBlock;++ const result = applyRecordedBlocksToGraph({+ nodes: [existing],+ edges: [],+ recordedBlocks: [recorded],+ recordedInsertionPoint: {+ previous: null,+ next: null,+ connectingEdgeType: "default",+ },+ recordedParameters: [],+ existingParameters: [],+ });++ expect(result.nodes.map((node) => node.data.label)).toEqual([+ "log_in",+ "log_in_2",+ ]);+ });+it("does not apply recorded blocks when disabled", () => {const doLayout = vi.fn();@@ -326,6 +362,269 @@ describe("useApplyRecordedBlocks", () => {"Type 'API token' with {{ credentials_1.secret_value }}.",);});+ it("substitutes the allocated key into a recorded code block's code", () => {+ const doLayout = vi.fn();++ useWorkflowParametersStore.setState({+ parameters: [+ {+ key: "credentials",+ parameterType: "credential",+ credentialId: "cred_already_here",+ },+ ],+ });++ // A code-first recording reads the credential through the token as an identifier.+ useRecordedBlocksStore.getState().setRecordedBlocks(+ {+ blocks: [+ {+ block_type: "code",+ label: "recorded_example_com",+ code: 'await page.locator("#pw").fill(cred_just_recorded.password)',+ prompt: "",+ parameters: [{ key: "cred_just_recorded" }],+ parameter_keys: ["cred_just_recorded"],+ } as unknown as WorkflowBlock,+ ],+ parameters: [+ {+ key: "cred_just_recorded",+ parameter_type: "credential",+ credential_id: "cred_just_recorded",+ description: "",+ },+ ],+ },+ {+ previous: "start",+ next: null,+ connectingEdgeType: "edgeWithAddButton",+ },+ );++ renderHook(() =>+ useApplyRecordedBlocks({+ enabled: true,+ nodes: [{ id: "start", data: { label: "start" } }] as Array<AppNode>,+ edges: [] as Array<Edge>,+ doLayout,+ }),+ );++ const [mergedNodes] = doLayout.mock.calls[0]!;+ const codeNode = (mergedNodes as Array<AppNode>).find(+ (node) => node.type === "codeBlock",+ );+ // The code moves with the declaration, or the fill reads the other credential.+ expect(codeNode?.data.parameterKeys).toEqual(["credentials_1"]);+ expect(codeNode?.data.code).toBe(+ 'await page.locator("#pw").fill(credentials_1.password)',+ );+ });+ it("does not allocate a key a recorded workflow parameter already claims", () => {+ const doLayout = vi.fn();++ // A field labelled "Credentials" mints the same key the credential allocator hands out.+ useRecordedBlocksStore.getState().setRecordedBlocks(+ {+ blocks: [+ {+ block_type: "code",+ label: "recorded_example_com",+ code: 'await page.locator("#pw").fill(cred_recorded.password)',+ prompt: "",+ parameters: [{ key: "credentials" }, { key: "cred_recorded" }],+ parameter_keys: ["credentials", "cred_recorded"],+ } as unknown as WorkflowBlock,+ ],+ parameters: [+ // The recording response carries only these fields, not a persisted WorkflowParameter.+ {+ key: "credentials",+ parameter_type: "workflow",+ workflow_parameter_type: "string",+ default_value: "",+ description: "",+ } as unknown as RecordedParameter,+ {+ key: "cred_recorded",+ parameter_type: "credential",+ credential_id: "cred_recorded",+ description: "",+ },+ ],+ },+ {+ previous: "start",+ next: null,+ connectingEdgeType: "edgeWithAddButton",+ },+ );++ renderHook(() =>+ useApplyRecordedBlocks({+ enabled: true,+ nodes: [{ id: "start", data: { label: "start" } }] as Array<AppNode>,+ edges: [] as Array<Edge>,+ doLayout,+ }),+ );++ const stored = useWorkflowParametersStore.getState().parameters;+ const keys = stored.map((parameter) => parameter.key);+ // Two parameters under one key make the workflow unsavable.+ expect(new Set(keys).size).toBe(keys.length);+ expect(keys).toContain("credentials");+ const credential = stored.find((p) => p.parameterType === "credential");+ expect(credential?.key).not.toBe("credentials");+ });++ it("renames a recorded parameter that lands on a reused credential key", () => {+ const doLayout = vi.fn();++ // The workflow already wraps this credential under the default key.+ useWorkflowParametersStore.setState({+ parameters: [+ {+ key: "credentials",+ parameterType: "credential",+ credentialId: "cred_same",+ },+ ],+ });++ // A field labelled "Credentials" mints the same key the wrapper already holds.+ useRecordedBlocksStore.getState().setRecordedBlocks(+ {+ blocks: [+ {+ block_type: "code",+ label: "recorded_example_com",+ code:+ 'await page.locator("#note").fill(str(credentials))\n' ++ 'await page.locator("#pw").fill(cred_same.password)',+ prompt: "",+ parameters: [{ key: "credentials" }, { key: "cred_same" }],+ parameter_keys: ["credentials", "cred_same"],+ } as unknown as WorkflowBlock,+ ],+ parameters: [+ {+ key: "credentials",+ parameter_type: "workflow",+ workflow_parameter_type: "string",+ default_value: "",+ description: "",+ } as unknown as RecordedParameter,+ {+ key: "cred_same",+ parameter_type: "credential",+ credential_id: "cred_same",+ description: "",+ },+ ],+ },+ {+ previous: "start",+ next: null,+ connectingEdgeType: "edgeWithAddButton",+ },+ );++ renderHook(() =>+ useApplyRecordedBlocks({+ enabled: true,+ nodes: [{ id: "start", data: { label: "start" } }] as Array<AppNode>,+ edges: [] as Array<Edge>,+ doLayout,+ }),+ );++ const [mergedNodes] = doLayout.mock.calls[0]!;+ const codeNode = (mergedNodes as Array<AppNode>).find(+ (node) => node.type === "codeBlock",+ );+ // Sharing the key would make str(credentials) stringify the credential, password included,+ // into the page field.+ expect(codeNode?.data.code).toContain('#note").fill(str(credentials_2))');+ expect(codeNode?.data.code).toContain('#pw").fill(credentials.password)');+ const stored = useWorkflowParametersStore.getState().parameters;+ expect(stored.map((parameter) => parameter.key).sort()).toEqual([+ "credentials",+ "credentials_2",+ ]);+ });++ it("substitutes tokens in one pass when an allocated key is another token", () => {+ const doLayout = vi.fn();++ // The user named their own credential parameter after a credential id.+ useWorkflowParametersStore.setState({+ parameters: [+ {+ key: "cred_second",+ parameterType: "credential",+ credentialId: "cred_first",+ },+ ],+ });++ useRecordedBlocksStore.getState().setRecordedBlocks(+ {+ blocks: [+ {+ block_type: "code",+ label: "recorded_example_com",+ code:+ 'await page.locator("#a").fill(cred_first.password)\n' ++ 'await page.locator("#b").fill(cred_second.password)',+ prompt: "",+ parameters: [{ key: "cred_first" }, { key: "cred_second" }],+ parameter_keys: ["cred_first", "cred_second"],+ } as unknown as WorkflowBlock,+ ],+ parameters: [+ {+ key: "cred_first",+ parameter_type: "credential",+ credential_id: "cred_first",+ description: "",+ },+ {+ key: "cred_second",+ parameter_type: "credential",+ credential_id: "cred_second",+ description: "",+ },+ ],+ },+ {+ previous: "start",+ next: null,+ connectingEdgeType: "edgeWithAddButton",+ },+ );++ renderHook(() =>+ useApplyRecordedBlocks({+ enabled: true,+ nodes: [{ id: "start", data: { label: "start" } }] as Array<AppNode>,+ edges: [] as Array<Edge>,+ doLayout,+ }),+ );++ const [mergedNodes] = doLayout.mock.calls[0]!;+ const codeNode = (mergedNodes as Array<AppNode>).find(+ (node) => node.type === "codeBlock",+ );+ // Cascading passes would rewrite the first fill twice and point both at cred_second.+ expect(codeNode?.data.code).toContain('#a").fill(cred_second.password)');+ expect(codeNode?.data.code).toContain('#b").fill(credentials.password)');+ });+it("gives two recorded logins distinct keys over an existing credentials key", () => {const doLayout = vi.fn();diff --git a/skyvern-frontend/src/routes/workflows/studio/runview/RunView.test.tsx b/skyvern-frontend/src/routes/workflows/studio/runview/RunView.test.tsxindex 420a09f1a..f37bac72a 100644--- a/skyvern-frontend/src/routes/workflows/studio/runview/RunView.test.tsx+++ b/skyvern-frontend/src/routes/workflows/studio/runview/RunView.test.tsx@@ -1389,6 +1389,31 @@ describe("RunView failure presentation", () => {expect(onRetry).toHaveBeenCalledTimes(1);});+ test("an input reassembly memory limit offers a retry instead of a copilot fix", () => {+ seedFailedCodeRun(+ "CodeBlock inputs exhausted the sandbox memory limit before the block started.",+ "parameter_reassembly_memory_limit_exceeded",+ );+ const { getByTestId } = renderRunView({ onFix: vi.fn(), onRetry: vi.fn() });+ const line = within(getByTestId("run-failure-line"));++ expect(line.queryByRole("button", { name: "Fix with Copilot" })).toBeNull();+ expect(line.getByRole("button", { name: "Retry" })).not.toBeNull();+ });++ test("a code memory limit still offers a copilot fix", () => {+ seedFailedCodeRun(+ "CodeBlock exceeded the configured memory limit.",+ "memory_limit_exceeded",+ );+ const { getByTestId } = renderRunView({ onFix: vi.fn(), onRetry: vi.fn() });+ const line = within(getByTestId("run-failure-line"));++ expect(+ line.getByRole("button", { name: "Fix with Copilot" }),+ ).not.toBeNull();+ });+test("a code block that continued on failure does not retitle the line", () => {seedCompletedRun({status: Status.Failed,diff --git a/skyvern-frontend/src/routes/workflows/workflowRun/codeBlockFailure.test.ts b/skyvern-frontend/src/routes/workflows/workflowRun/codeBlockFailure.test.tsindex da427e35d..aa1d72226 100644--- a/skyvern-frontend/src/routes/workflows/workflowRun/codeBlockFailure.test.ts+++ b/skyvern-frontend/src/routes/workflows/workflowRun/codeBlockFailure.test.ts@@ -100,6 +100,32 @@ describe("describeCodeBlockFailure", () => {).toMatchObject({ kind: "infrastructure", recovery: "retry" });});+ test("treats an input reassembly memory limit as a retry, not a code fix", () => {+ expect(+ describeCodeBlockFailure(+ block({ error_codes: ["parameter_reassembly_memory_limit_exceeded"] }),+ ),+ ).toMatchObject({+ kind: "infrastructure",+ code: "parameter_reassembly_memory_limit_exceeded",+ recovery: "retry",+ guidance: expect.stringContaining("earlier block pass in less data"),+ });+ });++ test("keeps a code memory limit repairable", () => {+ expect(+ describeCodeBlockFailure(+ block({ error_codes: ["memory_limit_exceeded"] }),+ ),+ ).toMatchObject({+ kind: "limit",+ code: "memory_limit_exceeded",+ recovery: "fix",+ guidance: expect.stringContaining("Process data in smaller batches"),+ });+ });+test("names the exception and line for a user code error", () => {const failure = describeCodeBlockFailure(block({diff --git a/skyvern-frontend/src/routes/workflows/workflowRun/codeBlockFailure.ts b/skyvern-frontend/src/routes/workflows/workflowRun/codeBlockFailure.tsindex 6f213a038..e2efa006d 100644--- a/skyvern-frontend/src/routes/workflows/workflowRun/codeBlockFailure.ts+++ b/skyvern-frontend/src/routes/workflows/workflowRun/codeBlockFailure.ts@@ -121,9 +121,15 @@ const FAILURE_TEMPLATES: Record<string, CodeBlockFailureTemplate> = {kind: "limit",title: "The block ran out of memory",guidance:- "Process the data in smaller batches rather than holding it all at once.",+ "Process data in smaller batches, or return only the fields the next block needs.",recovery: "fix",},+ parameter_reassembly_memory_limit_exceeded: {+ ...INFRASTRUCTURE,+ title: "The code sandbox ran out of memory while receiving inputs",+ guidance:+ "Retry the run. If it keeps happening, have an earlier block pass in less data to this block.",+ },busy: {kind: "infrastructure",title: "The code sandbox was already busy",diff --git a/skyvern/client/__init__.py b/skyvern/client/__init__.pyindex f2b338c2c..0754476d0 100644--- a/skyvern/client/__init__.py+++ b/skyvern/client/__init__.py@@ -259,6 +259,7 @@ if typing.TYPE_CHECKING:GetRunResponse_OpenaiCua,GetRunResponse_TaskV1,GetRunResponse_TaskV2,+ GetRunResponse_TaskV3,GetRunResponse_UiTars,GetRunResponse_WorkflowRun,GetRunResponse_YutoriNavigator,@@ -1032,6 +1033,7 @@ _dynamic_imports: typing.Dict[str, str] = {"GetRunResponse_OpenaiCua": ".types","GetRunResponse_TaskV1": ".types","GetRunResponse_TaskV2": ".types",+ "GetRunResponse_TaskV3": ".types","GetRunResponse_UiTars": ".types","GetRunResponse_WorkflowRun": ".types","GetRunResponse_YutoriNavigator": ".types",@@ -1825,6 +1827,7 @@ __all__ = ["GetRunResponse_OpenaiCua","GetRunResponse_TaskV1","GetRunResponse_TaskV2",+ "GetRunResponse_TaskV3","GetRunResponse_UiTars","GetRunResponse_WorkflowRun","GetRunResponse_YutoriNavigator",diff --git a/skyvern/client/types/__init__.py b/skyvern/client/types/__init__.pyindex 68752a4ab..712935f9b 100644--- a/skyvern/client/types/__init__.py+++ b/skyvern/client/types/__init__.py@@ -279,6 +279,7 @@ if typing.TYPE_CHECKING:GetRunResponse_OpenaiCua,GetRunResponse_TaskV1,GetRunResponse_TaskV2,+ GetRunResponse_TaskV3,GetRunResponse_UiTars,GetRunResponse_WorkflowRun,GetRunResponse_YutoriNavigator,@@ -1080,6 +1081,7 @@ _dynamic_imports: typing.Dict[str, str] = {"GetRunResponse_OpenaiCua": ".get_run_response","GetRunResponse_TaskV1": ".get_run_response","GetRunResponse_TaskV2": ".get_run_response",+ "GetRunResponse_TaskV3": ".get_run_response","GetRunResponse_UiTars": ".get_run_response","GetRunResponse_WorkflowRun": ".get_run_response","GetRunResponse_YutoriNavigator": ".get_run_response",@@ -1858,6 +1860,7 @@ __all__ = ["GetRunResponse_OpenaiCua","GetRunResponse_TaskV1","GetRunResponse_TaskV2",+ "GetRunResponse_TaskV3","GetRunResponse_UiTars","GetRunResponse_WorkflowRun","GetRunResponse_YutoriNavigator",diff --git a/skyvern/client/types/get_run_response.py b/skyvern/client/types/get_run_response.pyindex 2933c6095..7169c675d 100644--- a/skyvern/client/types/get_run_response.py+++ b/skyvern/client/types/get_run_response.py@@ -84,6 +84,40 @@ class GetRunResponse_TaskV2(UniversalBaseModel):extra = pydantic.Extra.allow+class GetRunResponse_TaskV3(UniversalBaseModel):+ run_type: typing.Literal["task_v3"] = "task_v3"+ run_id: str+ status: RunStatus+ output: typing.Optional[TaskRunResponseOutput] = None+ downloaded_files: typing.Optional[typing.List[FileInfo]] = None+ recording_url: typing.Optional[str] = None+ recording_archived: typing.Optional[bool] = None+ screenshot_urls: typing.Optional[typing.List[str]] = None+ failure_reason: typing.Optional[str] = None+ created_at: dt.datetime+ modified_at: dt.datetime+ queued_at: typing.Optional[dt.datetime] = None+ started_at: typing.Optional[dt.datetime] = None+ finished_at: typing.Optional[dt.datetime] = None+ app_url: typing.Optional[str] = None+ browser_session_id: typing.Optional[str] = None+ browser_profile_id: typing.Optional[str] = None+ max_screenshot_scrolls: typing.Optional[int] = None+ script_run: typing.Optional[ScriptRunResponse] = None+ errors: typing.Optional[typing.List[typing.Dict[str, typing.Optional[typing.Any]]]] = None+ step_count: typing.Optional[int] = None+ run_request: typing.Optional[TaskRunRequestOutput] = None++ if IS_PYDANTIC_V2:+ model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(extra="allow", frozen=True) # type: ignore # Pydantic v2+ else:++ class Config:+ frozen = True+ smart_union = True+ extra = pydantic.Extra.allow++class GetRunResponse_OpenaiCua(UniversalBaseModel):run_type: typing.Literal["openai_cua"] = "openai_cua"run_id: str@@ -260,6 +294,7 @@ class GetRunResponse_WorkflowRun(UniversalBaseModel):GetRunResponse = typing.Union[GetRunResponse_TaskV1,GetRunResponse_TaskV2,+ GetRunResponse_TaskV3,GetRunResponse_OpenaiCua,GetRunResponse_AnthropicCua,GetRunResponse_UiTars,diff --git a/skyvern/client/types/run_type.py b/skyvern/client/types/run_type.pyindex 29b8f40cb..aa553c2a7 100644--- a/skyvern/client/types/run_type.py+++ b/skyvern/client/types/run_type.py@@ -3,6 +3,6 @@import typingRunType = typing.Union[- typing.Literal["task_v1", "task_v2", "workflow_run", "openai_cua", "anthropic_cua", "ui_tars", "yutori_navigator"],+ typing.Literal["task_v1", "task_v2", "task_v3", "workflow_run", "openai_cua", "anthropic_cua", "ui_tars", "yutori_navigator"],typing.Any,]diff --git a/skyvern/config.py b/skyvern/config.pyindex f2a30dfb6..f0deffaf7 100644--- a/skyvern/config.py+++ b/skyvern/config.py@@ -91,6 +91,22 @@ class Settings(BaseSettings):ADDITIONAL_MODULES: list[str] = []+ # Whole-display recording is opt-in and intended for packaged Linux workers only. Default OFF keeps+ # ordinary local/OSS users on the existing Playwright per-page recording path. Enabling replaces+ # per-page recording with one change-driven whole-display MP4 for an explicitly selected cohort.+ EXCLUSIVE_DISPLAY_RECORDING: bool = False+ # Narrow, validated whole-display recording profile (safe defaults; invalid values fall back explicitly).+ DISPLAY_RECORDING_OUTPUT_WIDTH: int = 1280+ DISPLAY_RECORDING_OUTPUT_HEIGHT: int = 720+ DISPLAY_RECORDING_MAX_FPS: int = 15+ DISPLAY_RECORDING_CRF: int = 28+ DISPLAY_RECORDING_KEYFRAME_SECONDS: int = 5+ # How many browser runs one worker drives at once (mirrors the worker env of the same name; default 1 =+ # historical pod-per-run). Whole-display recording captures the ENTIRE display, and the per-display flock+ # only blocks a second recorder, not a second browser sharing the display — so it is display-ownership-safe+ # only at concurrency == 1. The recorder reads this to fail closed above 1.+ BROWSER_WORKER_MAX_CONCURRENT_ACTIVITIES: int = 1+BROWSER_TYPE: str = "chromium-headful"BROWSER_REMOTE_DEBUGGING_URL: str = "http://127.0.0.1:9222"BROWSER_REMOTE_DEBUGGING_HOST_HEADER: str | None = Nonediff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.pyindex 29d2fb980..979796f36 100644--- a/skyvern/forge/agent.py+++ b/skyvern/forge/agent.py@@ -5674,11 +5674,23 @@ class ForgeAgent:for idx, video_artifact in enumerate(video_artifacts):if video_artifact.video_artifact_id:continue- video_artifact_id = await app.ARTIFACT_MANAGER.create_artifact(- step=step,- artifact_type=ArtifactType.RECORDING,- data=video_artifact.video_data,- )+ if video_artifact.video_file_extension:+ # Seed the first-step RECORDING artifact with the real container (mp4 for the+ # whole-display recorder) so its uri matches the bytes from step 0 and per-step+ # prefixes/terminal finalize address the same .mp4 key (SKY-15466). Playwright+ # per-page recordings leave it None and keep their .webm default.+ video_artifact_id = await app.ARTIFACT_MANAGER.create_artifact(+ step=step,+ artifact_type=ArtifactType.RECORDING,+ data=video_artifact.video_data,+ file_extension=video_artifact.video_file_extension,+ )+ else:+ video_artifact_id = await app.ARTIFACT_MANAGER.create_artifact(+ step=step,+ artifact_type=ArtifactType.RECORDING,+ data=video_artifact.video_data,+ )video_artifacts[idx].video_artifact_id = video_artifact_idapp.BROWSER_MANAGER.set_video_artifact_for_task(task, video_artifacts)diff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.pyindex 95cd0b76b..2bc4c6324 100644--- a/skyvern/forge/agent_functions.py+++ b/skyvern/forge/agent_functions.py@@ -884,6 +884,21 @@ async def _convert_css_shape_to_string(return None+@dataclass(frozen=True)+class RecordingVideoSizeResolution:+ """Outcome of resolving the recording resolution for a run.++ ``record_video_size`` is the Playwright/Patchright ``record_video_size`` (a feature-selected+ profile is capped to the effective viewport; an operator override passes through untouched).+ ``raw_output_bound`` is the UNCAPPED selection — the raw profile or the operator override — used+ as the whole-display recorder's FFmpeg output bound so it never inherits the viewport-capped+ Playwright size (which would fit the window twice). ``None`` on either field means "unchanged":+ keep the existing ``record_video_size`` / fall back to the static display-recording default."""++ record_video_size: dict[str, int] | None+ raw_output_bound: dict[str, int] | None++class AgentFunction:# OSS default honors the requested engine; cloud overrides to A/B-route eligible# traffic onto the native task_v3 engine.@@ -1087,13 +1102,14 @@ class AgentFunction:organization_id: str | None,workflow_permanent_id: str | None = None,viewport: dict[str, int] | None = None,- ) -> dict[str, int] | None:+ ) -> RecordingVideoSizeResolution:"""Resolve the browser recording resolution for this run.- Returns ``current_size`` unchanged. Cloud overrides this to opt runs into- an elevated resolution behind a feature flag.+ Returns ``current_size`` unchanged as both the Playwright size and the raw output bound (an+ operator-set ``BROWSER_RECORDING_WIDTH/HEIGHT`` is the only selection OSS knows). Cloud+ overrides this to opt runs into an elevated resolution behind a feature flag."""- return current_size+ return RecordingVideoSizeResolution(current_size, current_size)async def should_keep_code_mode_for_workflow_run(self,diff --git a/skyvern/forge/failure_classifier.py b/skyvern/forge/failure_classifier.pyindex f3e8c559a..c50e172db 100644--- a/skyvern/forge/failure_classifier.py+++ b/skyvern/forge/failure_classifier.py@@ -153,6 +153,18 @@ def classify_from_failure_reason(})+ # The framed relay can exhaust the child address-space limit while it receives inputs,+ # before executing the block. Its message is distinct from a user-code memory limit.+ if "codeblock inputs exhausted the sandbox memory limit before the block started" in reason:+ categories.append(+ {+ "category": "INFRASTRUCTURE_ERROR",+ "confidence_float": 0.95,+ "reason_code": "secure_codeblock_input_memory_limit",+ "reasoning": "Secure CodeBlock sandbox ran out of memory before executing the block",+ }+ )+# The runner's fail-closed message for its internal faults (protocol errors, handshake# failures, runner-side exceptions); user code cannot author this literal.if "secure codeblock runner failed before completing" in reason:diff --git a/skyvern/forge/request_logging.py b/skyvern/forge/request_logging.pyindex 30b9cd2bd..19f0724f0 100644--- a/skyvern/forge/request_logging.py+++ b/skyvern/forge/request_logging.py@@ -51,7 +51,11 @@ _SENSITIVE_ENDPOINTS = {"POST /v1/workflow/copilot/credential-response","POST /v1/workflow/copilot/convert-yaml-to-blocks",}-_SENSITIVE_ENDPOINT_PATTERNS = (re.compile(r"^(?:POST|PUT) /(?:api/)?v1/credentials(?:/.*)?$"),)+_SENSITIVE_ENDPOINT_PATTERNS = (+ re.compile(r"^(?:POST|PUT) /(?:api/)?v1/credentials(?:/.*)?$"),+ # MCP arguments and results can contain arbitrary secrets, including malformed JSON.+ re.compile(r"^[^ ]+ /mcp(?:/.*)?$", re.DOTALL),+)_MAX_BODY_LENGTH = 1000_READ_METHODS = frozenset({"GET", "HEAD", "OPTIONS"})# 404/405 are dominated by internet scanners and MCP clients probing GET for an SSE stream;diff --git a/skyvern/forge/sdk/api/aws.py b/skyvern/forge/sdk/api/aws.pyindex 0282b3fc3..8b2eadb86 100644--- a/skyvern/forge/sdk/api/aws.py+++ b/skyvern/forge/sdk/api/aws.py@@ -34,6 +34,16 @@ add_type("application/json", ".har")add_type("text/plain", ".log")add_type("application/zstd", ".zst")++_RECORDING_CONTENT_TYPES = {".mp4": "video/mp4", ".webm": "video/webm"}+++def _recording_content_type(uri_or_path: str) -> str | None:+ """Allowlist MIME for RECORDING artifacts ONLY (.mp4/.webm -> video/*, for <video> playback); never infers+ html/svg/other from a (possibly user-named) key — unknown extensions return None (never minting a renderable MIME)."""+ return _RECORDING_CONTENT_TYPES.get(os.path.splitext(uri_or_path)[1].lower())++_S3_OPERATION_RETRIES = 2# get_object on a missing key raises NoSuchKey; head-style paths use 404/NotFound.S3_NOT_FOUND_ERROR_CODES = frozenset({"NoSuchKey", "NotFound", "404"})@@ -583,15 +593,20 @@ class AsyncAWSClient:tags: dict[str, str] | None = None,serialize_key: str | None = None,supersede_queued: bool = False,+ content_type: str | None = None,) -> str | None:# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/put_object.htmlif storage_class not in S3StorageClass:raise ValueError(f"Invalid storage class: {storage_class}. Must be one of {list(S3StorageClass)}")+ effective_content_type = content_type+async def _op() -> str:async with self._s3_client() as client:parsed_uri = S3Uri(uri)- extra_args = {"Tagging": self._create_tag_string(tags)} if tags else {}+ extra_args: dict[str, Any] = {"Tagging": self._create_tag_string(tags)} if tags else {}+ if effective_content_type:+ extra_args["ContentType"] = effective_content_typeawait client.put_object(Body=data,Bucket=parsed_uri.bucket,@@ -660,15 +675,20 @@ class AsyncAWSClient:tags: dict[str, str] | None = None,close_file_obj: bool = False,serialize_key: str | None = None,+ content_type: str | None = None,) -> str | None:# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3/client/upload_fileobj.html#upload-fileobjif storage_class not in S3StorageClass:raise ValueError(f"Invalid storage class: {storage_class}. Must be one of {list(S3StorageClass)}")+ effective_content_type = content_type+async def _op() -> str:async with self._s3_client() as client:parsed_uri = S3Uri(uri)extra_args: dict[str, Any] = {"StorageClass": str(storage_class)}+ if effective_content_type:+ extra_args["ContentType"] = effective_content_typeif tags:extra_args["Tagging"] = self._create_tag_string(tags)await client.upload_fileobj(@@ -789,11 +809,15 @@ class AsyncAWSClient:async def _fallback_upload() -> Any:async def _op() -> None:async with self._s3_client() as client:+ fb_extra: dict[str, Any] = {"StorageClass": str(storage_class)}+ fb_ct = _recording_content_type(uri)+ if fb_ct:+ fb_extra["ContentType"] = fb_ctawait client.upload_fileobj(reader,parsed_uri.bucket,parsed_uri.key,- ExtraArgs={"StorageClass": str(storage_class)},+ ExtraArgs=fb_extra,Config=_STREAM_UPLOAD_TRANSFER_CONFIG,)@@ -899,11 +923,15 @@ class AsyncAWSClient:full-replaces again."""token = uuid.uuid4().hexreader = _RangedFileReader(fh, 0, length) # non-owning: the outer store_recording_prefix owns fh+ reseed_extra: dict[str, Any] = {"StorageClass": str(sc), "Metadata": {_COMPOSE_GEN_META_KEY: token}}+ reseed_ct = _recording_content_type(uri)+ if reseed_ct:+ reseed_extra["ContentType"] = reseed_ctawait client.upload_fileobj(reader,parsed_uri.bucket,parsed_uri.key,- ExtraArgs={"StorageClass": str(sc), "Metadata": {_COMPOSE_GEN_META_KEY: token}},+ ExtraArgs=reseed_extra,Config=_STREAM_UPLOAD_TRANSFER_CONFIG,)head = await client.head_object(Bucket=parsed_uri.bucket, Key=parsed_uri.key)@@ -960,12 +988,14 @@ class AsyncAWSClient:# A single copy part / tail part cannot exceed 5 GiB; fall back rather than split.raise ComposeUnsupportedError()token = uuid.uuid4().hex+ create_ct = _recording_content_type(uri) # set on create so S3 carries it onto the completed objecttry:create = await client.create_multipart_upload(Bucket=parsed_uri.bucket,Key=parsed_uri.key,StorageClass=str(sc),Metadata={_COMPOSE_GEN_META_KEY: token},+ **({"ContentType": create_ct} if create_ct else {}),)except Exception as e:if self._is_expired_token_error(e):diff --git a/skyvern/forge/sdk/artifact/manager.py b/skyvern/forge/sdk/artifact/manager.pyindex 53e107e24..c14ba7704 100644--- a/skyvern/forge/sdk/artifact/manager.py+++ b/skyvern/forge/sdk/artifact/manager.py@@ -777,6 +777,7 @@ class ArtifactManager:artifact_type: ArtifactType,data: bytes | None = None,path: str | None = None,+ file_extension: str | None = None,) -> tuple[str, str]:artifact_id = generate_artifact_id()uri = app.STORAGE.build_workflow_run_block_uri(@@ -785,6 +786,9 @@ class ArtifactManager:workflow_run_block=workflow_run_block,artifact_type=artifact_type,)+ # Match create_artifact: an MP4 RECORDING must key under .mp4 so mid-run bytes serve video/mp4 (SKY-15466).+ if file_extension and artifact_type == ArtifactType.RECORDING:+ uri = replace_file_extension(uri, file_extension)await self._create_artifact(aio_task_primary_key=workflow_run_block.workflow_run_block_id,artifact_id=artifact_id,@@ -804,12 +808,14 @@ class ArtifactManager:artifact_type: ArtifactType,data: bytes | None = None,path: str | None = None,+ file_extension: str | None = None,) -> str:artifact_id, _ = await self._create_workflow_run_block_artifact_internal(workflow_run_block=workflow_run_block,artifact_type=artifact_type,data=data,path=path,+ file_extension=file_extension,)return artifact_iddiff --git a/skyvern/forge/sdk/artifact/storage/s3.py b/skyvern/forge/sdk/artifact/storage/s3.pyindex 1aa417d8d..5402e7e93 100644--- a/skyvern/forge/sdk/artifact/storage/s3.py+++ b/skyvern/forge/sdk/artifact/storage/s3.py@@ -15,7 +15,7 @@ from skyvern.config import settingsfrom skyvern.constants import BROWSER_DOWNLOADING_SUFFIX, DOWNLOAD_FILE_PREFIXfrom skyvern.exceptions import DownloadSaveIncompleteErrorfrom skyvern.forge import app-from skyvern.forge.sdk.api.aws import AsyncAWSClient, S3StorageClass, S3Uri+from skyvern.forge.sdk.api.aws import AsyncAWSClient, S3StorageClass, S3Uri, _recording_content_typefrom skyvern.forge.sdk.api.files import (calculate_sha256_for_file,create_named_temporary_file,@@ -221,7 +221,12 @@ class S3Storage(BaseStorage):# compose base cached for this key is now stale and must not seed a later copy.self.async_client.forget_compose_state(serialize_key)await self.async_client.upload_file(- uri, data, storage_class=sc, serialize_key=serialize_key, supersede_queued=supersede_queued_prefixes+ uri,+ data,+ storage_class=sc,+ serialize_key=serialize_key,+ supersede_queued=supersede_queued_prefixes,+ content_type=_recording_content_type(uri) if artifact.artifact_type == ArtifactType.RECORDING else None,)async def _get_storage_class_for_org(@@ -329,12 +334,14 @@ class S3Storage(BaseStorage):# close the reader only after the real transfer finishes.reader = BoundedFileReader(path, length)# serialize_key fences this prefix ahead of the terminal write to the same uri (see store_artifact).+ rec_ct = _recording_content_type(artifact.uri) if artifact.artifact_type == ArtifactType.RECORDING else Noneawait self.async_client.upload_file_stream(artifact.uri,cast(BinaryIO, reader),storage_class=sc,close_file_obj=True,serialize_key=artifact.uri,+ content_type=rec_ct,)async def save_streaming_file(self, organization_id: str, file_name: str) -> bool | None:diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.pyindex 897fca23c..62c2f5d02 100644--- a/skyvern/forge/sdk/copilot/agent.py+++ b/skyvern/forge/sdk/copilot/agent.py@@ -22,6 +22,7 @@ from typing import TYPE_CHECKING, Any, Protocol, get_args, runtime_checkablefrom opentelemetry import trace as otel_traceif TYPE_CHECKING:+ from agents.memory import Sessionfrom agents.result import RunResultStreamingfrom skyvern.forge.sdk.core.event_source_stream import EventSourceStream@@ -4047,6 +4048,7 @@ async def _run_agent_loop_with_surface(output_guardrails: list[Any],allow_untested_retry: bool = False,is_fallback: bool = False,+ session: Session | None = None,) -> Any:# No model owns the attempt until setup completes and enforcement is ready to# enter the model loop. This also clears a prior model before fallback setup.@@ -4079,7 +4081,9 @@ async def _run_agent_loop_with_surface(model=model_name,output_guardrails=output_guardrails,)- session = create_copilot_session(chat_id)+ owns_session = session is None+ if session is None:+ session = create_copilot_session(chat_id)model_token = _copilot_model_name.set(model_name)tool_call_count_start = ctx.tool_calls_this_turntry:@@ -4191,7 +4195,8 @@ async def _run_agent_loop_with_surface(return resultfinally:_copilot_model_name.reset(model_token)- session.close()+ if owns_session:+ session.close()def _build_request_policy_clarification_result(@@ -5277,6 +5282,7 @@ async def _run_copilot_turn_impl()chat_id = chat_request.workflow_copilot_chat_id or chat_request.workflow_permanent_id+ model_session: Session | None = Noneasync def _run_attempt(attempt_model_name: str,@@ -5289,7 +5295,7 @@ async def _run_copilot_turn_impl(ctx=ctx,stream=stream,chat_id=chat_id,- initial_input=initial_input,+ initial_input=[] if is_fallback else initial_input,system_prompt=system_prompt,model_name=attempt_model_name,run_config=attempt_run_config,@@ -5301,10 +5307,14 @@ async def _run_copilot_turn_impl(output_guardrails=output_guardrails,allow_untested_retry=ctx.allow_untested_workflow_draft,is_fallback=is_fallback,+ session=model_session,)return attempttry:+ from skyvern.forge.sdk.copilot.session_factory import create_copilot_session++ model_session = create_copilot_session(chat_id)with trace_context:try:try:@@ -5426,3 +5436,6 @@ async def _run_copilot_turn_impl(error=e,workflow_permanent_id=chat_request.workflow_permanent_id,)+ finally:+ if model_session is not None:+ model_session.close()diff --git a/skyvern/forge/sdk/copilot/build_test_connect_failure.py b/skyvern/forge/sdk/copilot/build_test_connect_failure.pyindex a0425751c..618332c4e 100644--- a/skyvern/forge/sdk/copilot/build_test_connect_failure.py+++ b/skyvern/forge/sdk/copilot/build_test_connect_failure.py@@ -20,6 +20,7 @@ class BuildTestConnectFailure(BaseModel):task_id: str | None = Nonebrowser_session_id: str | None = Noneoccupier_run_id: str | None = None+ diagnostic: str | None = Noneretry_action: Literal["test_end_to_end"] = "test_end_to_end"diff --git a/skyvern/forge/sdk/copilot/build_test_outcome.py b/skyvern/forge/sdk/copilot/build_test_outcome.pyindex 412957430..4ab712019 100644--- a/skyvern/forge/sdk/copilot/build_test_outcome.py+++ b/skyvern/forge/sdk/copilot/build_test_outcome.py@@ -126,9 +126,9 @@ _BROWSER_OPERATION_FAILED: BuildTestFailedOperationKind = "browser_operation_fai_EXECUTED_BLOCK_STATUSES = frozenset(status.value for status in BlockStatus if status != BlockStatus.skipped)_FAILED_BLOCK_STATUSES = frozenset({"failed", "terminated", "canceled", "timed_out"})_COMPLETED_BLOCK_STATUSES = frozenset({BlockStatus.completed.value})-# Sandbox-process faults, not authored-code faults. ``timeout`` and ``user_code_error`` stay-# out: both are repairable despite also carrying ``runner_internal_error``. ``busy`` is in —-# a saturated runner gate says nothing about the code, so rewriting it cannot help.+# Sandbox-process faults, not authored-code faults. ``timeout``, ``user_code_error``, and input+# reassembly OOM stay out: workflow edits can make each repairable. ``busy`` is in — a saturated+# runner gate says nothing about the code, so rewriting it cannot help.INFRASTRUCTURE_RUNNER_ERROR_CODES: frozenset[str] = frozenset({"runner_unavailable",diff --git a/skyvern/forge/sdk/copilot/code_block_synthesis.py b/skyvern/forge/sdk/copilot/code_block_synthesis.pyindex 22686fb9b..c875284c4 100644--- a/skyvern/forge/sdk/copilot/code_block_synthesis.py+++ b/skyvern/forge/sdk/copilot/code_block_synthesis.py@@ -1912,6 +1912,9 @@ def synthesize_code_block(parameter_binding_snapshot: AuthoringParameterBindingSnapshot | None = None,file_match_transform: SameMonthFileMatchTransform | None = None,emit_read_return: bool = True,+ # Recordings can bind a `secret` credential, whose single value field is not a login field;+ # copilot's own gating sets stay untouched by passing the wider set only from that caller.+ allowed_credential_fields: AbstractSet[str] = _CREDENTIAL_FIELDS,_segment_pass: bool = False,) -> SynthesizedCodeBlock | None:"""Deterministically synthesize a code block from a scout trajectory, or None if empty."""@@ -2612,7 +2615,7 @@ def synthesize_code_block(elif tool_name == CREDENTIAL_FILL_TOOL_NAME:credential_id = str(interaction.get("credential_id") or "").strip()credential_field = str(interaction.get("credential_field") or "").strip()- if not credential_id or credential_field not in _CREDENTIAL_FIELDS:+ if not credential_id or credential_field not in allowed_credential_fields:notes.append("dropped a credential fill with no usable credential reference")diagnostics.dropped_interactions.append({@@ -2825,6 +2828,7 @@ def synthesize_code_block(parameter_binding_snapshot=parameter_binding_snapshot,file_match_transform=file_match_transform if segment_download_target is not None else None,emit_read_return=emit_read_return,+ allowed_credential_fields=allowed_credential_fields,_segment_pass=True,)if segment is None or not segment.diagnostics.emitted_interaction_count:diff --git a/skyvern/forge/sdk/copilot/enforcement.py b/skyvern/forge/sdk/copilot/enforcement.pyindex 59e825f20..4e376d2ce 100644--- a/skyvern/forge/sdk/copilot/enforcement.py+++ b/skyvern/forge/sdk/copilot/enforcement.py@@ -782,6 +782,10 @@ def _summarize_tool_output(output: str) -> str:synopsis["error"] = str(parsed["error"])[:200]data = parsed.get("data")+ # Session continuation can compact the same output again. Re-bound the+ # structured synopsis instead of discarding its already-retained facts.+ if not isinstance(data, dict) and isinstance(parsed.get("page_evidence"), dict):+ data = parsed["page_evidence"]if isinstance(data, dict) and _is_page_evidence(data):synopsis["page_evidence"] = _summarize_page_evidence(parsed, data)synopsis["_summarized"] = "older page evidence — bounded facts retained, raw excerpts dropped"diff --git a/skyvern/forge/sdk/copilot/llm_errors.py b/skyvern/forge/sdk/copilot/llm_errors.pyindex a4efb8b41..f2ec400e7 100644--- a/skyvern/forge/sdk/copilot/llm_errors.py+++ b/skyvern/forge/sdk/copilot/llm_errors.py@@ -3,6 +3,21 @@ from __future__ import annotationsfrom collections.abc import Iteratorfrom typing import TYPE_CHECKING+import anthropic+import litellm+import openai+from anthropic import AuthenticationError as AnthropicAuthenticationError+from anthropic import BadRequestError as AnthropicBadRequestError+from anthropic import NotFoundError as AnthropicNotFoundError+from anthropic import PermissionDeniedError as AnthropicPermissionDeniedError+from anthropic import UnprocessableEntityError as AnthropicUnprocessableEntityError+from litellm.exceptions import MidStreamFallbackError+from openai import AuthenticationError as OpenAIAuthenticationError+from openai import BadRequestError as OpenAIBadRequestError+from openai import NotFoundError as OpenAINotFoundError+from openai import PermissionDeniedError as OpenAIPermissionDeniedError+from openai import UnprocessableEntityError as OpenAIUnprocessableEntityError+if TYPE_CHECKING:from skyvern.forge.sdk.copilot.model_telemetry import CopilotModelStopMetadata@@ -23,11 +38,14 @@ class CopilotEmptyCompletionError(RuntimeError):self.retry_allowed = retry_allowed-def iter_exception_chain(exc: BaseException) -> Iterator[BaseException]:+def _iter_classification_chain(exc: BaseException) -> Iterator[BaseException]:seen: set[int] = set()current: BaseException | None = excwhile current is not None and id(current) not in seen:seen.add(id(current))+ if isinstance(current, MidStreamFallbackError) and current.original_exception is not None:+ current = current.original_exception+ continueyield currentcurrent = current.__cause__ or current.__context__@@ -54,16 +72,49 @@ _RETRIABLE_LLM_ERROR_TEXT = ("timeout",)_LLM_ERROR_MODULE_MARKERS = ("openai", "litellm", "anthropic")+_PERMANENT_LLM_ERRORS = (+ AnthropicAuthenticationError,+ AnthropicBadRequestError,+ AnthropicNotFoundError,+ AnthropicPermissionDeniedError,+ AnthropicUnprocessableEntityError,+ OpenAIAuthenticationError,+ OpenAIBadRequestError,+ OpenAINotFoundError,+ OpenAIPermissionDeniedError,+ OpenAIUnprocessableEntityError,+)def is_retriable_llm_error(exc: BaseException) -> bool:- for item in iter_exception_chain(exc):+ chain = list(_iter_classification_chain(exc))+ for item in chain:if isinstance(item, CopilotEmptyCompletionError):return (item.retry_allowedand item.stop_metadata.refusal is not Trueand item.stop_metadata.content_filter is not True)+ for item in chain:+ if isinstance(item, _PERMANENT_LLM_ERRORS):+ return False+ if isinstance(+ item,+ (+ litellm.APIConnectionError,+ litellm.Timeout,+ litellm.RateLimitError,+ litellm.ServiceUnavailableError,+ litellm.InternalServerError,+ openai.APIConnectionError,+ openai.RateLimitError,+ openai.InternalServerError,+ anthropic.APIConnectionError,+ anthropic.RateLimitError,+ anthropic.InternalServerError,+ ),+ ):+ return Truemodule = type(item).__module__.lower()name = type(item).__name__text = str(item).lower()diff --git a/skyvern/forge/sdk/copilot/runtime.py b/skyvern/forge/sdk/copilot/runtime.pyindex 02278bbaf..aa1380588 100644--- a/skyvern/forge/sdk/copilot/runtime.py+++ b/skyvern/forge/sdk/copilot/runtime.py@@ -55,7 +55,11 @@ from skyvern.forge.sdk.core import skyvern_contextfrom skyvern.forge.sdk.schemas.credentials import Credentialfrom skyvern.library.skyvern_browser import SkyvernBrowserfrom skyvern.schemas.browser_session_close import BrowserSessionCloseReason-from skyvern.webeye.browser_errors import BrowserCdpConnectionError, BrowserTargetClosedError+from skyvern.webeye.browser_errors import (+ BrowserCdpAcquisitionError,+ BrowserCdpConnectionError,+ BrowserTargetClosedError,+)from skyvern.webeye.browser_retirement import BrowserOperationRejected, BrowserRetirement, BrowserRetirementReasonfrom skyvern.webeye.browser_state import BrowserState@@ -1636,6 +1640,14 @@ async def verify_build_test_browser_session_by_attaching(return _build_test_connect_failure_result(BuildTestConnectFailure(state="already_closed", browser_session_id=examined_session_id))+ except BrowserCdpAcquisitionError as exc:+ return _build_test_connect_failure_result(+ BuildTestConnectFailure(+ state="cdp_connect_failed",+ browser_session_id=examined_session_id,+ diagnostic=str(exc),+ )+ )except BrowserCdpConnectionError:# A failed attach does not prove the remote session is dead; keep its id as# evidence and let the explicit fresh-session action choose replacement.diff --git a/skyvern/forge/sdk/copilot/turn_context.py b/skyvern/forge/sdk/copilot/turn_context.pyindex b9743a160..4571a00a9 100644--- a/skyvern/forge/sdk/copilot/turn_context.py+++ b/skyvern/forge/sdk/copilot/turn_context.py@@ -295,10 +295,8 @@ class TurnContextAssembler:return Nonelabels_csv = ", ".join(labels)summary = (- "A prior turn proposed a workflow draft that was never committed to the canvas, so the"- " CURRENT WORKFLOW YAML above is empty. The user is asking to run/re-test that draft. To"- " run it, call run_blocks_and_collect_debug with these block labels: "- f"{labels_csv}. Do not call update_and_run_blocks and do not ask the user to rebuild it."+ "A prior turn produced an uncommitted workflow draft. It is not the current canvas workflow. "+ f"The draft remains runnable by its top-level block labels: {labels_csv}.")return RunnableDraftContext(rendered_summary=summary, block_labels=labels)diff --git a/skyvern/forge/sdk/routes/browser_sessions.py b/skyvern/forge/sdk/routes/browser_sessions.pyindex ac9a43617..01def11e3 100644--- a/skyvern/forge/sdk/routes/browser_sessions.py+++ b/skyvern/forge/sdk/routes/browser_sessions.py@@ -679,6 +679,7 @@ async def process_recording(workflow_permanent_id=recording_request.workflow_permanent_id,draft_steps=recording_request.draft_steps,code_first=recording_request.code_first,+ supports_credential_tokens=recording_request.supports_credential_tokens,)return ProcessBrowserSessionRecordingResponse(blocks=blocks, parameters=parameters)diff --git a/skyvern/forge/sdk/routes/streaming/channels/exfiltration.py b/skyvern/forge/sdk/routes/streaming/channels/exfiltration.pyindex 8d53980b0..847021a8f 100644--- a/skyvern/forge/sdk/routes/streaming/channels/exfiltration.py+++ b/skyvern/forge/sdk/routes/streaming/channels/exfiltration.py@@ -64,7 +64,6 @@ class ExfiltrationChannel(CdpChannel):"""BINDING_NAME: t.ClassVar[str] = "__skyvern_exfiltrate_event"- CONSOLE_DEDUP_TTL_SECONDS: t.ClassVar[float] = 5.0REFRESH_INTERVAL_SECONDS: t.ClassVar[float] = 1.0# Tighter than the re-injection refresh so a navigation destroys few queued events.QUEUE_DRAIN_INTERVAL_SECONDS: t.ClassVar[float] = 0.25@@ -101,7 +100,6 @@ class ExfiltrationChannel(CdpChannel):self.cdp_session: CDPSession | None = Noneself.on_event = on_eventself._page_console_captures: weakref.WeakKeyDictionary[Page, PageConsoleCapture] = weakref.WeakKeyDictionary()- self._recent_console_event_fingerprints: dict[str, float] = {}self._seen_event_dedup_keys: dict[tuple[str, int], None] = {}self._pending_event_tasks: set[asyncio.Task[None]] = set()self._refresh_task: asyncio.Task | None = None@@ -202,39 +200,6 @@ class ExfiltrationChannel(CdpChannel):return arg- def _prune_console_dedup_cache(self, now: float) -> None:- expired = [- fingerprint- for fingerprint, emitted_at in self._recent_console_event_fingerprints.items()- if now - emitted_at > self.CONSOLE_DEDUP_TTL_SECONDS- ]- for fingerprint in expired:- self._recent_console_event_fingerprints.pop(fingerprint, None)-- # Excluded from the fingerprint: advances by ms when one interaction is re-captured across a reconnect.- _CONSOLE_FINGERPRINT_VOLATILE_KEYS: t.ClassVar[frozenset[str]] = frozenset({"timestamp"})-- def _console_fingerprint(self, event_data: dict[str, t.Any]) -> str:- # Drop None values (recursively) as well as volatile keys: the binding,- # Playwright-console, and raw-CDP transports materialize the same event- # with None-vs-absent differences (JSON.stringify drops undefined keys;- # the binding serializes them to null), which otherwise defeats dedup.- stable = self._strip_none(- {k: v for k, v in event_data.items() if k not in self._CONSOLE_FINGERPRINT_VOLATILE_KEYS}- )- try:- return json.dumps(stable, sort_keys=True, separators=(",", ":"))- except TypeError:- return json.dumps(stable, sort_keys=True, separators=(",", ":"), default=str)-- @classmethod- def _strip_none(cls, value: t.Any) -> t.Any:- if isinstance(value, dict):- return {k: cls._strip_none(v) for k, v in value.items() if v is not None}- if isinstance(value, list):- return [cls._strip_none(item) for item in value]- return value-def _event_dedup_key(self, event_data: dict[str, t.Any]) -> tuple[str, int] | None:doc_id = event_data.get("exfilDocId")seq = event_data.get("exfilSeq")@@ -244,25 +209,15 @@ class ExfiltrationChannel(CdpChannel):return Nonedef _should_emit_console_event(self, event_data: dict[str, t.Any]) -> bool:- # Stamped events dedup exactly across transports; the fingerprint heuristic below is only for unstamped (older in-flight) events.dedup_key = self._event_dedup_key(event_data)- if dedup_key is not None:- if dedup_key in self._seen_event_dedup_keys:- return False- self._seen_event_dedup_keys[dedup_key] = None- while len(self._seen_event_dedup_keys) > self.SEEN_EVENT_DEDUP_KEY_LIMIT:- self._seen_event_dedup_keys.pop(next(iter(self._seen_event_dedup_keys)))+ if dedup_key is None:return True- fingerprint = self._console_fingerprint(event_data)-- now = time.monotonic()- self._prune_console_dedup_cache(now)- previous = self._recent_console_event_fingerprints.get(fingerprint)- if previous is not None and now - previous <= self.CONSOLE_DEDUP_TTL_SECONDS:+ if dedup_key in self._seen_event_dedup_keys:return False-- self._recent_console_event_fingerprints[fingerprint] = now+ self._seen_event_dedup_keys[dedup_key] = None+ while len(self._seen_event_dedup_keys) > self.SEEN_EVENT_DEDUP_KEY_LIMIT:+ self._seen_event_dedup_keys.pop(next(iter(self._seen_event_dedup_keys)))return Truedef _emit_console_event(self, event_data: dict[str, t.Any], capture_seq: int) -> None:@@ -887,7 +842,6 @@ class ExfiltrationChannel(CdpChannel):await self.undecorate(page)- self._recent_console_event_fingerprints.clear()finally:# Release the dedicated Playwright driver + browser graph even if page-level# cleanup above raised on an already-dead page. _closing keeps this close()diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.pyindex 3d67b41c5..874d1a910 100644--- a/skyvern/forge/sdk/routes/workflow_copilot.py+++ b/skyvern/forge/sdk/routes/workflow_copilot.py@@ -1352,6 +1352,7 @@ async def _commit_staged_workflow(sequential_key=staged_workflow.sequential_key,edited_by="copilot",preserve_completion_contract=not clear_persisted_completion_contract,+ validate_code_block_templates=False,)@@ -1410,6 +1411,7 @@ async def _restore_workflow_definition(original_workflow: Workflow | None, organcreated_by=original_workflow.created_by,edited_by=original_workflow.edited_by,preserve_completion_contract=False,+ validate_code_block_templates=False,)@@ -2848,6 +2850,7 @@ async def workflow_copilot_apply_proposed_workflow(request=yaml_request,workflow_permanent_id=chat.workflow_permanent_id,edited_by="copilot",+ validate_code_block_templates=False,)try:diff --git a/skyvern/forge/sdk/workflow/exceptions.py b/skyvern/forge/sdk/workflow/exceptions.pyindex 482bacfaa..db2187eef 100644--- a/skyvern/forge/sdk/workflow/exceptions.py+++ b/skyvern/forge/sdk/workflow/exceptions.py@@ -192,6 +192,18 @@ class InvalidCodeBlockStep(WorkflowDefinitionValidationException):)+class CodeBlockTemplateSyntaxError(WorkflowDefinitionValidationException):+ def __init__(self, block_label: str, original: BaseException) -> None:+ self.block_label = block_label+ self.original = original+ self.line = getattr(original, "lineno", None)+ line_suffix = f" on line {self.line}" if self.line is not None else ""+ super().__init__(+ f"Invalid Jinja2 in code block '{block_label}'{line_suffix}: {original}",+ status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,+ )++class InvalidWaitBlockTime(WorkflowDefinitionValidationException):def __init__(self, block_label: str, wait_sec: int, max_sec: int) -> None:super().__init__(diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.pyindex d631e9fc8..af4cca397 100644--- a/skyvern/forge/sdk/workflow/models/block.py+++ b/skyvern/forge/sdk/workflow/models/block.py@@ -169,6 +169,7 @@ from skyvern.forge.sdk.workflow.context_manager import (WorkflowRunContext,)from skyvern.forge.sdk.workflow.exceptions import (+ CodeBlockTemplateSyntaxError,CustomizedCodeException,CustomSMTPAuthenticationFailed,CustomSMTPConnectionFailed,@@ -1011,6 +1012,7 @@ class Block(BaseModel, abc.ABC):workflow_run_id: str,workflow_run_block_id: str | None,organization_id: str | None,+ can_continue_after_failure: bool = True,) -> BlockResult:failure_reason = self._redact_registered_secrets(failure_reason, workflow_run_context)error_codes = self.get_failure_error_codes()@@ -1032,6 +1034,7 @@ class Block(BaseModel, abc.ABC):workflow_run_block_id=workflow_run_block_id,organization_id=organization_id,error_codes=error_codes or None,+ can_continue_after_failure=can_continue_after_failure,)async def build_block_result(@@ -1048,6 +1051,7 @@ class Block(BaseModel, abc.ABC):executed_branch_next_block: str | None = None,error_codes: list[str] | None = None,is_synthetic_loop_failure: bool = False,+ can_continue_after_failure: bool = True,) -> BlockResult:# Every arm that reports a block failure lands here -- the raise path and the ones that# return an unsuccessful result -- so this is where the reason is scrubbed. It is persisted@@ -1095,6 +1099,7 @@ class Block(BaseModel, abc.ABC):status=status,workflow_run_block_id=workflow_run_block_id,is_synthetic_loop_failure=is_synthetic_loop_failure,+ can_continue_after_failure=can_continue_after_failure,)async def get_or_create_browser_state(@@ -2513,6 +2518,9 @@ class LoopBlockExecutedResult(BaseModel):"""Last appended result is a loop-structural / safety-limit failure, not a child."""return bool(self.block_outputs) and self.block_outputs[-1].is_synthetic_loop_failure+ def can_continue_after_failure(self) -> bool:+ return not self.block_outputs or self.block_outputs[-1].can_continue_after_failure+def is_completed(self) -> bool:if len(self.block_outputs) == 0:return False@@ -2528,8 +2536,8 @@ class LoopBlockExecutedResult(BaseModel):return True# Swallow flags apply only on natural-completion paths whose last result- # is a real child failure; structural/safety synthetics must propagate.- if not self.natural_completion or self.is_synthetic_loop_failure():+ # is a recoverable child failure; structural and deterministic failures propagate.+ if not self.natural_completion or self.is_synthetic_loop_failure() or not last_ouput.can_continue_after_failure:return Falseif self.last_block.continue_on_failure:@@ -2564,6 +2572,7 @@ class LoopBlockExecutedResult(BaseModel):and self.natural_completionand not self.is_canceled()and not self.is_synthetic_loop_failure()+ and self.can_continue_after_failure())if self.is_canceled():@@ -3498,11 +3507,13 @@ class ForLoopBlock(Block):last_block=current_block,)- if (- not block_output.success- and not loop_block.continue_on_failure- and not loop_block.next_loop_on_failure- and not self.next_loop_on_failure+ if not block_output.success and (+ not block_output.can_continue_after_failure+ or (+ not loop_block.continue_on_failure+ and not loop_block.next_loop_on_failure+ and not self.next_loop_on_failure+ )):LOG.info(f"ForLoopBlock Encountered a failure processing block {block_idx} during loop {loop_idx}, terminating early",@@ -3520,7 +3531,7 @@ class ForLoopBlock(Block):last_block=current_block,)- if block_output.success or loop_block.continue_on_failure:+ if block_output.success or (loop_block.continue_on_failure and block_output.can_continue_after_failure):next_label: str | None = Noneif loop_block.block_type == BlockType.CONDITIONAL:branch_metadata = (@@ -3557,7 +3568,9 @@ class ForLoopBlock(Block):block_idx += 1continue- if loop_block.next_loop_on_failure or self.next_loop_on_failure:+ if block_output.can_continue_after_failure and (+ loop_block.next_loop_on_failure or self.next_loop_on_failure+ ):LOG.info(f"ForLoopBlock Block {block_idx} during loop {loop_idx} failed but will continue to next iteration",block_output_count=len(block_outputs),@@ -3721,6 +3734,7 @@ class ForLoopBlock(Block):status=block_status,workflow_run_block_id=workflow_run_block_id,organization_id=organization_id,+ can_continue_after_failure=loop_executed_result.can_continue_after_failure(),)@@ -4213,11 +4227,13 @@ class WhileLoopBlock(Block):last_block=current_block,)- if (- not block_output.success- and not loop_block.continue_on_failure- and not loop_block.next_loop_on_failure- and not self.next_loop_on_failure+ if not block_output.success and (+ not block_output.can_continue_after_failure+ or (+ not loop_block.continue_on_failure+ and not loop_block.next_loop_on_failure+ and not self.next_loop_on_failure+ )):LOG.info("WhileLoopBlock encountered a failure processing block, terminating early",@@ -4235,7 +4251,7 @@ class WhileLoopBlock(Block):last_block=current_block,)- if block_output.success or loop_block.continue_on_failure:+ if block_output.success or (loop_block.continue_on_failure and block_output.can_continue_after_failure):next_label: str | None = Noneif loop_block.block_type == BlockType.CONDITIONAL:branch_metadata = (@@ -4272,7 +4288,9 @@ class WhileLoopBlock(Block):block_idx += 1continue- if loop_block.next_loop_on_failure or self.next_loop_on_failure:+ if block_output.can_continue_after_failure and (+ loop_block.next_loop_on_failure or self.next_loop_on_failure+ ):LOG.info("WhileLoopBlock child block failed but will continue to next iteration",block_output_count=len(block_outputs),@@ -4398,6 +4416,7 @@ class WhileLoopBlock(Block):status=block_status,workflow_run_block_id=workflow_run_block_id,organization_id=organization_id,+ can_continue_after_failure=loop_executed_result.can_continue_after_failure(),)@@ -5112,6 +5131,13 @@ class CodeBlock(Block):TEMPLATABLE_FIELDS: ClassVar[frozenset[str]] = frozenset({"code", "error_code_mapping", "prompt"})+ def validate_code_template(self) -> None:+ masked_code, _ = mask_jinja_in_python_comments(self.code)+ try:+ jinja_sandbox_env.from_string(masked_code)+ except TemplateSyntaxError as exc:+ raise CodeBlockTemplateSyntaxError(self.label, exc) from exc+def _effective_error_code_mapping(self, workflow_run_context: WorkflowRunContext) -> dict[str, str]:return dict(self.error_code_mapping or {})@@ -5406,6 +5432,7 @@ async def wrapper({default_args}):workflow_run_block=workflow_run_block,artifact_type=ArtifactType.RECORDING,data=video_artifacts[idx].video_data,+ file_extension=video_artifacts[idx].video_file_extension,)except Exception:LOG.warning(@@ -7413,6 +7440,7 @@ async def wrapper({default_args}):workflow_run_id,workflow_run_block_id,organization_id,+ can_continue_after_failure=False,)# get all parameters into a dictionary@@ -8904,6 +8932,29 @@ class TextPromptBlock(Block):)+def _resolve_uploads_organization_id(organization_id: str | None) -> str:+ if organization_id:+ return organization_id+ context = skyvern_context.current()+ if context and context.organization_id:+ return context.organization_id+ raise ValueError("An organization is required to store a file in the managed uploads bucket")+++def _build_managed_uploads_uri(organization_id: str, workflow_run_id: str, file_name: str) -> str:+ """Key the file under the organization prefix so per-org retention rules can match it.++ Managed uploads are addressed by prefix: reads are authorized against+ `{env}/{organization_id}/`, and a retention policy configured for an organization matches+ that same prefix. A key written outside it matches no policy and is unreadable through+ managed storage, with nothing failing either way. The date segment carries no meaning of+ its own; it keeps this layout the same as the other managed-uploads writer's.+ """+ today = datetime.now(UTC).strftime("%Y-%m-%d")+ key = f"{settings.ENV}/{organization_id}/{today}/{workflow_run_id}/{file_name}"+ return f"s3://{settings.AWS_S3_BUCKET_UPLOADS}/{key}"++class DownloadToS3Block(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"@@ -8927,6 +8978,10 @@ class DownloadToS3Block(Block):def format_potential_template_parameters(self, workflow_run_context: WorkflowRunContext) -> None:self.url = self.render_templatable_field("url", self.url, workflow_run_context)+ @staticmethod+ def _get_s3_uri(organization_id: str, workflow_run_id: str) -> str:+ return _build_managed_uploads_uri(organization_id, workflow_run_id, str(uuid.uuid4()))+async def _upload_file_to_s3(self, uri: str, file_path: str, cleanup_file: bool = True) -> None:try:client = self.get_async_aws_client()@@ -8973,6 +9028,10 @@ class DownloadToS3Block(Block):organization_id,)+ # Resolved before the download: a raise after it would strand the delete=False temp file,+ # whose only cleanup is in the upload's finally.+ uploads_organization_id = _resolve_uploads_organization_id(organization_id)+try:context = skyvern_context.current()run_id = context.run_id if context and context.run_id else workflow_run_id@@ -8985,7 +9044,7 @@ class DownloadToS3Block(Block):uri = Nonetry:- uri = f"s3://{settings.AWS_S3_BUCKET_UPLOADS}/{settings.ENV}/{workflow_run_id}/{uuid.uuid4()}"+ uri = self._get_s3_uri(uploads_organization_id, workflow_run_id)await self._upload_file_to_s3(uri, file_path, cleanup_file=not self.url.startswith("/"))except Exception as e:LOG.error("DownloadToS3Block Failed to upload file to S3", uri=uri, error=str(e))@@ -9029,10 +9088,8 @@ class UploadToS3Block(Block):self.path = self.render_templatable_field("path", self.path, workflow_run_context)@staticmethod- def _get_s3_uri(workflow_run_id: str, path: str) -> str:- s3_bucket = settings.AWS_S3_BUCKET_UPLOADS- s3_key = f"{settings.ENV}/{workflow_run_id}/{uuid.uuid4()}_{Path(path).name}"- return f"s3://{s3_bucket}/{s3_key}"+ def _get_s3_uri(organization_id: str, workflow_run_id: str, path: str) -> str:+ return _build_managed_uploads_uri(organization_id, workflow_run_id, f"{uuid.uuid4()}_{Path(path).name}")async def execute(self,@@ -9089,6 +9146,7 @@ class UploadToS3Block(Block):if not os.path.exists(resolved_path):raise FileNotFoundError(f"UploadToS3Block File not found at path: {resolved_path}")+ uploads_organization_id = _resolve_uploads_organization_id(organization_id)s3_uris = []try:client = self.get_async_aws_client()@@ -9103,11 +9161,11 @@ class UploadToS3Block(Block):LOG.warning("UploadToS3Block Skipping directory", file=file)continuefile_path = os.path.join(resolved_path, file)- s3_uri = self._get_s3_uri(workflow_run_id, file_path)+ s3_uri = self._get_s3_uri(uploads_organization_id, workflow_run_id, file_path)s3_uris.append(s3_uri)await client.upload_file_from_path(uri=s3_uri, file_path=file_path)else:- s3_uri = self._get_s3_uri(workflow_run_id, resolved_path)+ s3_uri = self._get_s3_uri(uploads_organization_id, workflow_run_id, resolved_path)s3_uris.append(s3_uri)await client.upload_file_from_path(uri=s3_uri, file_path=resolved_path)except Exception as e:diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.pyindex 0ef809c52..2c4f8cc08 100644--- a/skyvern/forge/sdk/workflow/service.py+++ b/skyvern/forge/sdk/workflow/service.py@@ -140,6 +140,7 @@ from skyvern.forge.sdk.workflow.models.block import (BaseTaskBlock,Block,BlockTypeVar,+ CodeBlock,ConditionalBlock,ExtractionBlock,FileParserBlock,@@ -8019,7 +8020,7 @@ class WorkflowService:block_type_var=block.block_type,block_label=block.label,)- if block.continue_on_failure:+ if block.continue_on_failure and getattr(block_result, "can_continue_after_failure", True):LOG.warning(f"Block with type {block.block_type} at index {block_idx}/{blocks_cnt - 1} failed but will continue executing the workflow run {workflow_run_id}",block_type=block.block_type,@@ -8043,7 +8044,7 @@ class WorkflowService:block_label=block.label,)- if block.continue_on_failure:+ if block.continue_on_failure and getattr(block_result, "can_continue_after_failure", True):LOG.warning(f"Block with type {block.block_type} at index {block_idx}/{blocks_cnt - 1} was terminated for workflow run {workflow_run_id}, but will continue executing the workflow run",block_type=block.block_type,@@ -8067,7 +8068,7 @@ class WorkflowService:block_label=block.label,)- if block.continue_on_failure:+ if block.continue_on_failure and getattr(block_result, "can_continue_after_failure", True):LOG.warning(f"Block with type {block.block_type} at index {block_idx}/{blocks_cnt - 1} timed out for workflow run {workflow_run_id}, but will continue executing the workflow run",block_type=block.block_type,@@ -8123,7 +8124,7 @@ class WorkflowService:if block_result.status == BlockStatus.canceled:# Cancellation is never recoverable via continue_on_failure, matching normal block execution.return WorkflowRunStatus.canceled, None, failure_category- if block.continue_on_failure:+ if block.continue_on_failure and getattr(block_result, "can_continue_after_failure", True):return None, None, Noneif block_result.status == BlockStatus.failed:failure_reason = f"{block.block_type} block failed. failure reason: {block_result.failure_reason}"@@ -8417,6 +8418,12 @@ class WorkflowService:if isinstance(block, WorkflowTriggerBlock):block.validate_payload_templates()+ @staticmethod+ def _validate_code_block_templates(workflow_definition: WorkflowDefinition) -> None:+ for block in get_all_blocks(workflow_definition.blocks):+ if isinstance(block, CodeBlock):+ block.validate_code_template()+async def create_workflow(self,organization_id: str,@@ -8458,6 +8465,7 @@ class WorkflowService:encrypt_secrets: bool = True,) -> Workflow:try:+ self._validate_code_block_templates(workflow_definition)if encrypt_secrets:await encrypt_workflow_definition_secrets(workflow_definition, organization_id)return await app.DATABASE.workflows.create_workflow(@@ -8978,8 +8986,11 @@ class WorkflowService:notify_workflow_saved: bool = True,preserve_completion_contract: bool = True,created_via: str | None = None,+ validate_code_block_templates: bool = True,) -> Workflow:if workflow_definition is not None:+ if validate_code_block_templates:+ self._validate_code_block_templates(workflow_definition)if organization_id is not None:organization = await app.DATABASE.organizations.get_organization(organization_id=organization_id)if organization is not None:@@ -12190,6 +12201,7 @@ class WorkflowService:workflow_id=placeholder.workflow_id,organization_id=organization_id,workflow_definition=dispatch_definition,+ validate_code_block_templates=False,)except Exception:# The placeholder row already exists as the latest version; if definition@@ -12378,6 +12390,7 @@ class WorkflowService:new_workflow_permanent_id: str | None = None,resolved_title: str | None = None,created_via: str | None = None,+ validate_code_block_templates: bool = True,) -> Workflow:organization_id = organization.organization_idtitle = resolved_title@@ -12537,6 +12550,7 @@ class WorkflowService:workflow_definition=workflow_definition,edited_by=edited_by,created_via=created_via,+ validate_code_block_templates=validate_code_block_templates,)await self.maybe_delete_cached_code(diff --git a/skyvern/forge/taskv3/tools.py b/skyvern/forge/taskv3/tools.pyindex f3b5f3b39..c223ba63f 100644--- a/skyvern/forge/taskv3/tools.py+++ b/skyvern/forge/taskv3/tools.py@@ -609,6 +609,46 @@ _STOPWORDS_JS = ("'inc','llc','ltd','corp'])")+# The page's own statement, ON THE ELEMENT ITSELF, that it is out of BOTH the accessibility tree and+# the tab order. Both together, because either alone is ordinary: a decorative icon carries only the+# first, a control driven by a roving tabindex only the second. The tab order is read off el.tabIndex+# -- the value the UA already parsed -- rather than a regex over the attribute, which called+# tabindex="-0" negative and tabindex="-1x" positive and was wrong both times. tabIndex is declared+# per interface, so SVGElement's getter is tried when HTMLElement's does not answer: an icon control+# authored as <svg role="button"> is not an HTMLElement. Accessors come off the prototype, never off+# the node -- a form exposes its named controls as own properties that override built-ins, so+# `<input name="hasAttribute">` would otherwise make this throw for every control in that form, and+# the caller answers a throw by dropping the control.+#+# Deliberately the element's own attributes and nothing else. aria-hidden inherits and `inert` covers+# a whole region, so reading the ancestor chain is strictly more faithful -- and five review rounds+# running found a demonstrated defect in that walk: clobbered traversal getters that deleted a live+# field, the inert-vs-aria-hidden priority, a namespace-blind inert read, an unguarded post-drain+# read that failed the entire digest. Each fix was right and the next round found the next one. The+# case this exists for carries both attributes on the element itself, so the ancestor forms buy no+# coverage here; they are SKY-15894, to be built once with their own tests rather than as the Nth+# amendment to this one.+_A11Y_REMOVED_JS = r"""(el) => {+ // No guard of its own: both callers already answer a throw from this by saying nothing, which is+ // the only sensible answer to a page that has deleted a descriptor or replaced an intrinsic. A+ // second guard here would only be a branch no test can reach.+ const _getter = (proto, name) => {+ const d = Object.getOwnPropertyDescriptor(proto, name);+ return (d && d.get) || null;+ };+ // Per-read, because a getter that does not apply is not an error: SVGElement's tabIndex throws on+ // an HTMLElement and vice versa, and that throw is what selects the other one.+ const _read = (fn) => { try { return fn ? fn.call(el) : null; } catch (e) { return null; } };+ if (!Element.prototype.hasAttribute.call(el, 'tabindex')) return '';+ let ti = _read(_getter(HTMLElement.prototype, 'tabIndex'));+ if (typeof ti !== 'number') {+ ti = _read(typeof SVGElement === 'function' ? _getter(SVGElement.prototype, 'tabIndex') : null);+ }+ if (!(typeof ti === 'number' && ti < 0)) return '';+ const hidden = Element.prototype.getAttribute.call(el, 'aria-hidden');+ return String(hidden || '').toLowerCase() === 'true' ? 'aria-hidden' : '';+}"""+# The roles this engine treats as controls. observe enumerates exactly these (its `q` selector is# this list expanded) and reports them on each record, so it is the single answer to "is this a# control?" rather than each probe keeping its own.@@ -4896,6 +4936,9 @@ async () => {let truncatedInComponents = 0;let lastGroup = '';let groupTotal = 0;+ const _a11yRemoved = """+ + _A11Y_REMOVED_JS+ + r""";const _PHANTOM_TEXT_TYPES = /^(?:text|search|email|tel|url|number|password|date|datetime-local|month|week|time)$/;// Our own witness for the walk: every marker write can run page code, synchronously or through// the page's own MutationObservers after we yield. Anything it changed is re-validated below;@@ -5053,8 +5096,15 @@ async () => {let slottedName = false;if (!strongLabel) strongLabel = (el.innerText || '').trim();if (!strongLabel && host) { strongLabel = slottedText(el, host); slottedName = !!strongLabel; }+ // A skinned native is exempt: the zero-rect carve-out above already established that the page+ // draws a visible proxy for it and that the tools drive it directly, which is the opposite+ // finding to this one.+ let removedBy = '';+ try { if (!hidden) removedBy = _a11yRemoved(el); } catch (e) { removedBy = ''; }// A text control the page itself hides from assistive tech, takes out of the tab order and- // leaves unnamed is one no person can reach; a non-zero box does not make it a field.+ // leaves unnamed is one no person can reach; a non-zero box does not make it a field. Left+ // exactly as it was: a DROP is not widened on a statement that leaves the control working, and+ // an aria-hidden field with a negative tabindex still fills.const isTextLike = el.tagName === 'TEXTAREA' || (el.tagName === 'INPUT' && _PHANTOM_TEXT_TYPES.test(String(el.type || '').toLowerCase()));const unnamed = !strongLabel && !['placeholder', 'aria-labelledby', 'title'].some((a) => (el.getAttribute(a) || '').trim());if (isTextLike && el.getAttribute('aria-hidden') === 'true' && el.getAttribute('tabindex') === '-1' && unnamed) {@@ -5118,6 +5168,7 @@ async () => {const rec = { i, tag: el.tagName.toLowerCase(), type: (_typed && el.type) || null, selector, label: label.slice(0, _RETAIN_WIDTH) };if (placeholder && placeholder !== label) rec.placeholder = placeholder.slice(0, _RETAIN_WIDTH);if (hidden) rec.hidden = true;+ if (removedBy) rec.a11yRemoved = removedBy;// A widget role is what the element IS -- a <div role="switch"> renders as a bare div otherwise,// and the model cannot tell it from decoration. The role travels with its state below, or it is// not worth surfacing: an on switch and an off one that read identically invite toggling the@@ -5301,6 +5352,15 @@ async () => {} else { checkInconclusive = false; ok = resolvesTo(rec.selector, el) || checkInconclusive; }}if (!ok) { labelOfControl.delete(el); out.splice(k, 1); dropped++; }+ // Recomputed after the drain rather than added to the fingerprint above: a page's own observer+ // can change either attribute in response to a marker write, and the line has to describe the+ // page as it stands when the line is printed. Only the MARK is re-decided -- the omission is the+ // pre-existing rule on its pre-existing predicate, so it has nothing new to re-decide.+ else if (el && !rec.hidden) {+ let now = '';+ try { now = _a11yRemoved(el); } catch (e) { now = ''; }+ if (now) rec.a11yRemoved = now; else delete rec.a11yRemoved;+ }}// Page-text digest: outcome states (submission confirmations, rejection banners, validation// summaries) live in non-interactive nodes the element list can never carry. Three sources in@@ -6432,6 +6492,13 @@ def build_browser_tools(extra += " [hidden-native: styled proxy; click acts on it directly]"if e.get("group"):extra += f" group={_field(e['group'], OBSERVE_DISPLAY_WIDTHS['group'])!r}"+ if e.get("a11yRemoved"):+ # What the page declared, not a prediction: aria-hidden with a negative tabindex does+ # not stop a click landing or a field filling (measured), and a control the page+ # disowned can still be the only one there is. What it does do is tell a design+ # system's own clickable surface apart from the native control it stands in for, when+ # the two render the same caption and the digest lines would otherwise be identical.+ extra += " [aria-hidden: the page keeps this out of its accessibility tree and tab order]"# INVARIANT for this line and every line above it: no page-controlled byte reaches the# digest un-escaped, and the header's count and the number of element lines come from the# same list. Everything else here is either repr'd or a literal. `type` is the trap --@@ -6461,6 +6528,7 @@ def build_browser_tools("markers_minted": data.get("markersMinted") or 0,"markers_reused": data.get("markersReused") or 0,"group_texts_found": sum(1 for e in elements if e.get("group")),+ "a11y_removed_listed": sum(1 for e in elements if e.get("a11yRemoved")),}# Mask the whole rendered payload, not just url=: a signed payload ref can surface as page# text or a field value the model previously typed (a token resolved back to its URL), anddiff --git a/skyvern/schemas/browser_sessions.py b/skyvern/schemas/browser_sessions.pyindex 37c7454b7..e431629d6 100644--- a/skyvern/schemas/browser_sessions.py+++ b/skyvern/schemas/browser_sessions.py@@ -130,6 +130,14 @@ class ProcessBrowserSessionRecordingRequest(BaseModel):default=False,description="When true, synthesize deterministic code blocks from the recording instead of agent blocks.",)+ supports_credential_tokens: bool = Field(+ default=False,+ description=(+ "Whether the caller substitutes credential tokens in a code block's code. A frontend that "+ "does not would save the renamed parameters beside code still reading the token, which fails "+ "at run time; recorded credentials stay unbound for those callers."+ ),+ )class ProcessBrowserSessionRecordingResponse(BaseModel):diff --git a/skyvern/schemas/workflows.py b/skyvern/schemas/workflows.pyindex bb00d45fd..b77378957 100644--- a/skyvern/schemas/workflows.py+++ b/skyvern/schemas/workflows.py@@ -543,6 +543,9 @@ class BlockResult:# missing block label) so callers can distinguish them from real child-block# results. Set explicitly at the synthetic construction sites in loop helpers.is_synthetic_loop_failure: bool = False+ # False when retry/continuation cannot change the outcome, such as invalid+ # CodeBlock source that fails before execution.+ can_continue_after_failure: bool = Trueclass FileType(StrEnum):diff --git a/skyvern/services/browser_recording/code_first.py b/skyvern/services/browser_recording/code_first.pyindex 611bff9dc..55cefe145 100644--- a/skyvern/services/browser_recording/code_first.py+++ b/skyvern/services/browser_recording/code_first.py@@ -14,19 +14,31 @@ from urllib.parse import urlparseimport structlogfrom skyvern.client.types.workflow_definition_yaml_blocks_item import WorkflowDefinitionYamlBlocksItem_Code-from skyvern.client.types.workflow_definition_yaml_parameters_item import WorkflowDefinitionYamlParametersItem_Workflow+from skyvern.client.types.workflow_definition_yaml_parameters_item import (+ WorkflowDefinitionYamlParametersItem,+ WorkflowDefinitionYamlParametersItem_Credential,+ WorkflowDefinitionYamlParametersItem_Workflow,+)from skyvern.forge.sdk.copilot.code_block_steps import derive_code_block_steps-from skyvern.forge.sdk.copilot.code_block_synthesis import synthesize_code_block, synthesize_goto_code_block+from skyvern.forge.sdk.copilot.code_block_synthesis import (+ CREDENTIAL_FILL_TOOL_NAME,+ synthesize_code_block,+ synthesize_goto_code_block,+)+from skyvern.forge.sdk.credential_site_policy import site_offrom skyvern.forge.sdk.workflow.exceptions import InsecureCodeDetectedfrom skyvern.forge.sdk.workflow.models.block import CodeBlock+from skyvern.services.browser_recording.redact import is_identifier_field, texts_are_labelsfrom skyvern.services.browser_recording.types import (Action,ActionClick,ActionHover,ActionInputText,ActionPressKey,+ ActionTarget,ActionUrlChange,ActionWait,+ CredentialKind,RecordingDraftStep,)@@ -45,11 +57,34 @@ ActionDraftPair = tuple[Action, RecordingDraftStep | None]# Interactions that silently vanish from a workflow if the synthesizer cannot# locate them; any unlocatable one forces the legacy (LLM agent block) fallback.# Dropped hovers are tolerated: they are usually incidental to a located click.-_REQUIRED_LOCATOR_TOOLS = frozenset({"click", "type_text", "select_option"})+_REQUIRED_LOCATOR_TOOLS = frozenset({"click", "type_text", "select_option", CREDENTIAL_FILL_TOOL_NAME})++# The credential field each recorded kind fills. `secret` mirrors the legacy path's single+# `secret_value`; `magic_link` resolves at runtime rather than as a field, and `credit_card`+# spreads over six fields the recorder cannot tell apart, so both stay plain parameters.+CREDENTIAL_FILL_FIELD_BY_KIND: dict[CredentialKind, str] = {+ "password": "password",+ "totp": "totp",+ "secret": "secret_value",+}+_ALLOWED_CREDENTIAL_FILL_FIELDS = frozenset(CREDENTIAL_FILL_FIELD_BY_KIND.values()) | {"username"}++# Interactions that can sit between the identifier and the password without ending the form:+# other fields of the same form, a stray hover, a dwell.+_FORM_INTERNAL_TOOLS = frozenset({"type_text", "hover", "wait"})++# A click that only moves focus into a field. The ordinary mouse-driven login clicks the+# password box between typing the identifier and typing the password, and the recorder emits+# an action for it; treating that as the end of the form loses the binding on the common flow.+_TEXT_ENTRY_TAGS = frozenset({"textarea", "select"})+_TEXT_ENTRY_ROLES = frozenset({"textbox", "searchbox", "combobox"})+# An <input> is only a field to focus for these types: submit, button, reset and image are+# controls that end the form, and they carry the same tag name.+_TEXT_ENTRY_INPUT_TYPES = frozenset({"", "text", "email", "tel", "url", "search", "password", "number", "date", "time"})CodeFirstResult = tuple[list[WorkflowDefinitionYamlBlocksItem_Code],- list[WorkflowDefinitionYamlParametersItem_Workflow],+ list[WorkflowDefinitionYamlParametersItem],]@@ -97,6 +132,40 @@ def apply_draft_overlay(return pairs+def transfer_focus_click_credentials(pairs: list[ActionDraftPair]) -> list[ActionDraftPair]:+ """Move a credential bound on a click onto the fill of the same field.++ The recording panel offers the credential prompt on every step it classifies, and the+ mouse-driven login clicks the password box before typing into it, so the user can bind on+ the click card - after which the panel dismisses the prompt on the fill. The legacy path+ reads the credential off whichever step carries it; a code block has to fill it.+ """+ transferred = list(pairs)+ for index, (action, draft) in enumerate(pairs):+ if not isinstance(action, ActionClick) or draft is None or draft.credential_kind is None:+ continue+ credential_id = (draft.credential_id or "").strip()+ selector = (action.target.selector or "").strip()+ if not credential_id or not selector:+ continue+ for later in range(index + 1, len(transferred)):+ later_action, later_draft = transferred[later]+ if not isinstance(later_action, ActionInputText):+ continue+ if (later_action.target.selector or "").strip() != selector:+ continue+ if later_draft is None or (later_draft.credential_id or "").strip():+ break+ transferred[later] = (+ later_action,+ later_draft.model_copy(+ update={"credential_id": credential_id, "credential_kind": draft.credential_kind}+ ),+ )+ break+ return transferred++def segment_actions(pairs: list[ActionDraftPair]) -> list[RecordingSegment]:"""Split at user-initiated navigations; click-caused navigations stay inside their segment."""segments = [RecordingSegment()]@@ -131,7 +200,27 @@ def segment_actions(pairs: list[ActionDraftPair]) -> list[RecordingSegment]:return [segment for segment in segments if segment.pairs or segment.source_url]-def _interaction_for_action(action: Action) -> dict[str, t.Any] | None:+def _credential_fill(draft: RecordingDraftStep | None) -> dict[str, t.Any] | None:+ """The credential fill this recorded step was bound to in the recording panel, if any."""+ if draft is None:+ return None+ credential_id = (draft.credential_id or "").strip()+ if not credential_id or draft.credential_kind is None:+ return None+ field = CREDENTIAL_FILL_FIELD_BY_KIND.get(draft.credential_kind)+ if field is None:+ return None+ return {+ "tool_name": CREDENTIAL_FILL_TOOL_NAME,+ "credential_id": credential_id,+ "credential_field": field,+ # The synthesizer derives the parameter key from this name; the credential id is a+ # token the editor swaps for a real key once it can see the workflow's parameters.+ "credential_name": credential_id,+ }+++def _interaction_for_action(action: Action, draft: RecordingDraftStep | None = None) -> dict[str, t.Any] | None:target = action.targetbase: dict[str, t.Any] = {}if target.selector:@@ -146,6 +235,9 @@ def _interaction_for_action(action: Action) -> dict[str, t.Any] | None:if isinstance(action, ActionHover):return {"tool_name": "hover", **base}if isinstance(action, ActionInputText):+ credential_fill = _credential_fill(draft)+ if credential_fill is not None:+ return {**credential_fill, **base}if (target.tag_name or "").upper() == "SELECT":return {"tool_name": "select_option", "value": action.input_value, **base}interaction: dict[str, t.Any] = {"tool_name": "type_text", **base}@@ -163,28 +255,181 @@ def _interaction_for_action(action: Action) -> dict[str, t.Any] | None:return None-def segment_trajectory(segment: RecordingSegment) -> list[dict[str, t.Any]]:+def _is_focus_click(action: Action) -> bool:+ if not isinstance(action, ActionClick):+ return False+ target = action.target+ tag_name = (target.tag_name or "").lower()+ if tag_name in _TEXT_ENTRY_TAGS:+ return True+ if tag_name == "input":+ return (target.input_type or "").lower() in _TEXT_ENTRY_INPUT_TYPES+ return (target.role or "").lower() in _TEXT_ENTRY_ROLES+++def _identifier_fill_index(+ trajectory: list[dict[str, t.Any]],+ sources: list[ActionDraftPair],+ index: int,+) -> int | None:+ """The index of the login identifier typed into the same form before `index`, if any.++ The search reaches back over other fields of that form - a login page can ask for a+ tenant domain beside the identifier, and a signup form for a name, and past a click that+ only moves focus into a field - but stops at anything that ends the form: a click on+ something other than a field, a navigation, another credential fill. A field already bound+ to a credential of the user's own is skipped rather than claimed.+ """+ for candidate in range(index - 1, -1, -1):+ tool_name = trajectory[candidate]["tool_name"]+ action, draft = sources[candidate]+ if tool_name == "click" and _is_focus_click(action):+ continue+ if tool_name not in _FORM_INTERNAL_TOOLS:+ return None+ if tool_name != "type_text":+ continue+ if draft is not None and (draft.credential_id or "").strip():+ continue+ if is_identifier_field(action):+ return candidate+ return None+++def _bind_identifier_fills(+ trajectory: list[dict[str, t.Any]],+ sources: list[ActionDraftPair],+) -> None:+ """Point the login identifier typed before a password fill at the credential's username.++ Only the credential's password field is bound in the recording panel, so without this the+ identifier stays an empty workflow parameter and the login types nothing into it. The+ field has to look like an identifier to be claimed: "the field before the password" also+ describes a tenant domain or a search box, and filling a username into one of those types+ a wrong value while leaving the real identifier empty.+ """+ for index, interaction in enumerate(trajectory):+ if interaction.get("credential_field") != "password":+ continue+ candidate = _identifier_fill_index(trajectory, sources, index)+ if candidate is None:+ continue+ previous = trajectory[candidate]+ trajectory[candidate] = {+ key: value for key, value in previous.items() if key not in ("tool_name", "typed_value")+ } | {+ "tool_name": CREDENTIAL_FILL_TOOL_NAME,+ "credential_id": interaction["credential_id"],+ "credential_field": "username",+ "credential_name": interaction["credential_name"],+ }+++def segment_trajectory(segment: RecordingSegment, *, bind_credentials: bool = True) -> list[dict[str, t.Any]]:trajectory: list[dict[str, t.Any]] = []+ sources: list[ActionDraftPair] = []for action, draft in segment.pairs:- interaction = _interaction_for_action(action)+ interaction = _interaction_for_action(action, draft if bind_credentials else None)if interaction is None:continueif interaction["tool_name"] == "wait" and draft is not None and draft.wait_sec:interaction["duration_ms"] = int(draft.wait_sec) * 1000trajectory.append(interaction)+ sources.append((action, draft))+ if bind_credentials:+ _bind_identifier_fills(trajectory, sources)if trajectory and segment.source_url:trajectory[0] = {**trajectory[0], "source_url": segment.source_url}return trajectory-def _segment_label(segment: RecordingSegment, used: set[str]) -> str:- host = ""- if segment.source_url:- try:- host = urlparse(segment.source_url).netloc- except ValueError:- pass- base = re.sub(r"\W+", "_", f"recorded_{host}" if host else "recorded_steps").strip("_").lower()+_SEARCH_FIELD_HINTS = frozenset({"search", "query", "keyword"})+_MAX_NAME_WORDS = 4+++def _slugify(text: str, max_words: int = _MAX_NAME_WORDS) -> str:+ words = [word for word in re.split(r"\W+", text.lower()) if word]+ return "_".join(words[:max_words])+++def _site_slug(url: str | None) -> str:+ """A readable site name from the segment's entry URL; empty for IPs and localhost."""+ if not url:+ return ""+ site = site_of(url)+ if site is not None:+ return _slugify(site[2].split(".", 1)[0])+ try:+ host = urlparse(url).hostname or ""+ except ValueError:+ return ""+ if ":" in host:+ # An IPv6 literal; no name to read off it.+ return ""+ labels = [label for label in host.split(".") if label]+ if not labels or all(label.isdigit() for label in labels) or labels == ["localhost"]:+ return ""+ # A host with no registrable domain is useful only when it is a named intranet host.+ return _slugify(labels[0]) if len(labels) == 1 else ""+++def _target_name(target: ActionTarget) -> str:+ candidates = [target.accessible_name, target.id]+ if texts_are_labels(target.tag_name):+ candidates.extend(target.texts)+ for candidate in candidates:+ slug = _slugify(candidate or "")+ if slug:+ return slug+ match = re.fullmatch(r"[#.]([\w-]+)", (target.selector or "").strip())+ return _slugify(match.group(1)) if match else ""+++def _segment_summary(segment: RecordingSegment, emitted_indices: set[int] | None = None) -> str:+ """A general description of what the segment's interactions do, as a snake_case phrase."""+ site = _site_slug(segment.source_url)+ actions: list[Action] = []+ trajectory_index = 0+ for action, draft in segment.pairs:+ if _interaction_for_action(action, draft) is None:+ continue+ if emitted_indices is None or trajectory_index in emitted_indices:+ actions.append(action)+ trajectory_index += 1+ typed = [action for action in actions if isinstance(action, ActionInputText)]+ clicks = [action for action in actions if isinstance(action, ActionClick)]++ if any((action.target.input_type or "").lower() == "password" for action in typed):+ return f"log_in_to_{site}" if site else "log_in"+ if any(not _SEARCH_FIELD_HINTS.isdisjoint(_target_name(action.target).split("_")) for action in typed):+ return f"search_{site}" if site else "search"+ if typed and all((action.target.tag_name or "").upper() == "SELECT" for action in typed):+ name = _target_name(typed[0].target)+ return f"select_{name}" if name else "select_an_option"+ if typed:+ if len(typed) > 1:+ return "fill_out_the_form"+ name = _target_name(typed[0].target)+ return f"fill_in_{name}" if name else "fill_in_a_field"+ if clicks:+ # The last click is the one that advances the page (submit, next, a link).+ name = _target_name(clicks[-1].target)+ return f"click_{name}" if name else "click_through_the_page"+ presses = [action for action in actions if isinstance(action, ActionPressKey)]+ if presses:+ key = _slugify(presses[-1].key)+ return f"press_{key}" if key else "press_a_key"+ hovers = [action for action in actions if isinstance(action, ActionHover)]+ if hovers:+ name = _target_name(hovers[0].target)+ return f"hover_over_{name}" if name else "hover_over_the_page"+ if any(isinstance(action, ActionWait) for action in actions):+ return "wait"+ return f"open_{site}" if site else "open_the_page"+++def _segment_label(segment: RecordingSegment, used: set[str], emitted_indices: set[int]) -> str:+ base = re.sub(r"\W+", "_", _segment_summary(segment, emitted_indices)).strip("_").lower() or "recorded_steps"label = basesuffix = 2while label in used:@@ -197,13 +442,15 @@ def _segment_label(segment: RecordingSegment, used: set[str]) -> str:def actions_to_code_first_blocks(actions: list[Action],draft_steps: list[RecordingDraftStep] | None,+ *,+ bind_credentials: bool = True,) -> CodeFirstResult | None:"""Convert recorded actions into code blocks and parameters; None means fall back to legacy blocks."""if draft_steps is not None and not draft_steps:# The user deleted every interpreted step; commit an empty workflow rather# than falling back to blocks derived from the raw actions.return [], []- pairs = apply_draft_overlay(actions, draft_steps)+ pairs = transfer_focus_click_credentials(apply_draft_overlay(actions, draft_steps))segments = segment_actions(pairs)blocks: list[WorkflowDefinitionYamlBlocksItem_Code] = []@@ -211,10 +458,12 @@ def actions_to_code_first_blocks(# (selector, role, accessible name). The same field re-filled in a later# segment reuses its key; a different same-labeled field gets a fresh key.parameter_identities: dict[str, tuple[str, str, str] | None] = {}+ # Credential parameter key (the credential id, a token the editor re-keys) -> credential id.+ credential_ids_by_key: dict[str, str] = {}used_labels: set[str] = set()for segment in segments:- trajectory = segment_trajectory(segment)+ trajectory = segment_trajectory(segment, bind_credentials=bind_credentials)for interaction in trajectory:if interaction["tool_name"] in _REQUIRED_LOCATOR_TOOLS and not (interaction.get("selector") or (interaction.get("role") and interaction.get("accessible_name"))@@ -225,7 +474,7 @@ def actions_to_code_first_blocks()return Noneif trajectory:- synthesized = synthesize_code_block(trajectory)+ synthesized = synthesize_code_block(trajectory, allowed_credential_fields=_ALLOWED_CREDENTIAL_FILL_FIELDS)elif segment.source_url:synthesized = synthesize_goto_code_block(segment.source_url)else:@@ -254,10 +503,12 @@ def actions_to_code_first_blocks(key = str(parameter.get("key") or "").strip()if not key:continue- if parameter.get("credential_id"):- # Recordings cannot bind credentials yet; the synthesizer only emits- # these for fill_credential_field interactions, which we never map.- LOG.warning("record_browser.code_first_unexpected_credential_parameter", parameter_key=key)+ credential_id = str(parameter.get("credential_id") or "").strip()+ if credential_id:+ # The key is the credential id, so two credentials cannot share one and the+ # cross-block rename below has nothing to resolve.+ credential_ids_by_key[key] = credential_id+ block_parameter_keys.append(key)continueidentity = identity_by_key.get(key)resolved_key = key@@ -296,9 +547,14 @@ def actions_to_code_first_blocks()return None+ emitted_indices = {+ index+ for record in synthesized.diagnostics.emitted_interactions+ if isinstance((index := record.get("trajectory_index")), int) and index >= 0+ }blocks.append(WorkflowDefinitionYamlBlocksItem_Code(- label=_segment_label(segment, used_labels),+ label=_segment_label(segment, used_labels, emitted_indices),code=code,parameter_keys=block_parameter_keys or None,# Editor's convertToNode reads block.parameters.map(p => p.key); mirror the action block.@@ -314,7 +570,7 @@ def actions_to_code_first_blocks(if not blocks:return None- parameters = [+ parameters: list[WorkflowDefinitionYamlParametersItem] = [WorkflowDefinitionYamlParametersItem_Workflow(key=key,workflow_parameter_type="string",@@ -326,4 +582,12 @@ def actions_to_code_first_blocks()for key in parameter_identities]+ parameters.extend(+ WorkflowDefinitionYamlParametersItem_Credential(+ key=key,+ credential_id=credential_id,+ description="",+ )+ for key, credential_id in sorted(credential_ids_by_key.items())+ )return blocks, parametersdiff --git a/skyvern/services/browser_recording/interpretation.py b/skyvern/services/browser_recording/interpretation.pyindex f609798ab..4fbdd34f6 100644--- a/skyvern/services/browser_recording/interpretation.py+++ b/skyvern/services/browser_recording/interpretation.py@@ -16,7 +16,7 @@ from skyvern.client.types.workflow_definition_yaml_blocks_item import (from skyvern.config import settingsfrom skyvern.forge.sdk.routes.streaming.channels import exfiltration as streaming_exfiltrationfrom skyvern.forge.sdk.routes.streaming.channels.exfiltration import ExfiltratedEventSource-from skyvern.services.browser_recording.redact import credential_kind_for_target, redact_console_event+from skyvern.services.browser_recording.redact import credential_kind_for_action, redact_console_eventfrom skyvern.services.browser_recording.service import (Processor,deterministic_input_text_parameter_key,@@ -25,11 +25,9 @@ from skyvern.services.browser_recording.service import (from skyvern.services.browser_recording.types import (Action,ActionBlockable,- ActionClick,ActionInputText,ActionKind,ActionPressKey,- CredentialKind,ExfiltratedCdpEvent,ExfiltratedConsoleEvent,ExfiltratedEvent,@@ -114,19 +112,6 @@ def _extra_field(block: OutputBlock, field_name: str, fallback: t.Any) -> t.Any:return fallback-def _credential_kind_for_action(action: Action) -> CredentialKind | None:- if not isinstance(action, (ActionClick, ActionInputText)):- return None- return credential_kind_for_target(- action.target.input_type,- action.target.autocomplete,- field_id=action.target.id,- accessible_name=action.target.accessible_name,- texts=action.target.texts,- tag_name=action.target.tag_name,- )--def _draft_step_from_block(*,browser_session_id: str,@@ -155,7 +140,7 @@ def _draft_step_from_block(parameter_keys=block.parameter_keys or [],timestamp_start=action.timestamp_start,timestamp_end=action.timestamp_end,- credential_kind=_credential_kind_for_action(action),+ credential_kind=credential_kind_for_action(action),)if isinstance(block, WorkflowDefinitionYamlBlocksItem_GotoUrl):@@ -261,7 +246,7 @@ def _placeholder_step_from_action(parameter_keys=parameter_keys,timestamp_start=action.timestamp_start,timestamp_end=action.timestamp_end,- credential_kind=_credential_kind_for_action(action),+ credential_kind=credential_kind_for_action(action),)diff --git a/skyvern/services/browser_recording/redact.py b/skyvern/services/browser_recording/redact.pyindex 2cda34e69..2788c26fa 100644--- a/skyvern/services/browser_recording/redact.py+++ b/skyvern/services/browser_recording/redact.py@@ -9,7 +9,14 @@ from __future__ import annotationsimport reimport typing as t-from skyvern.services.browser_recording.types import CredentialKind, ExfiltratedConsoleEvent, ExfiltratedEvent+from skyvern.services.browser_recording.types import (+ Action,+ ActionClick,+ ActionInputText,+ CredentialKind,+ ExfiltratedConsoleEvent,+ ExfiltratedEvent,+)SECRET_INPUT_TYPES = frozenset({"password"})SECRET_AUTOCOMPLETE_TOKENS = frozenset(@@ -64,6 +71,13 @@ SECRET_HINT_PHRASES = ("bearer token","secret value",)+# A login identifier, recognized only from the one attribute that says a field holds an+# account identifier rather than describing its value: `autocomplete="username"`. An email+# input, or `autocomplete="email"`, is also how an account-settings form collects a new+# contact address - claiming those writes the account's username over the intended value.+# Ids and labels are deliberately not read: deciding credential semantics from page prose is+# a classifier in the harness. A field without the attribute stays unbound.+IDENTIFIER_AUTOCOMPLETE_TOKENS = frozenset({"username"})MAGIC_LINK_HINT_PHRASES = ("magic link","email me a link",@@ -140,6 +154,33 @@ def is_secret_field(return kind in {"password", "totp", "credit_card", "secret"}+def is_identifier_field(action: Action) -> bool:+ """Whether this recorded field is a login identifier, so a username may be typed into it.++ Positive rather than residual: "not a secret" also describes a tenant domain or a search+ box, and filling a username into one of those types the wrong value into the page while+ leaving the real identifier empty. Typed markup only - see the constants above.+ """+ if not isinstance(action, ActionInputText):+ return False+ target = action.target+ tokens = _autocomplete_tokens(target.autocomplete)+ return any(token in IDENTIFIER_AUTOCOMPLETE_TOKENS for token in tokens)+++def credential_kind_for_action(action: Action) -> CredentialKind | None:+ if not isinstance(action, (ActionClick, ActionInputText)):+ return None+ return credential_kind_for_target(+ action.target.input_type,+ action.target.autocomplete,+ field_id=action.target.id,+ accessible_name=action.target.accessible_name,+ texts=action.target.texts,+ tag_name=action.target.tag_name,+ )++def credential_kind_for_target(input_type: str | None,autocomplete: str | None,diff --git a/skyvern/services/browser_recording/service.py b/skyvern/services/browser_recording/service.pyindex befa51096..dce302bdd 100644--- a/skyvern/services/browser_recording/service.py+++ b/skyvern/services/browser_recording/service.py@@ -875,6 +875,7 @@ class Processor:compressed_chunks: list[str],draft_steps: list[RecordingDraftStep] | None = None,code_first: bool = False,+ supports_credential_tokens: bool = False,) -> tuple[list[ProcessedBlock], list[WorkflowDefinitionYamlParametersItem]]:"""Process the compressed browser session recording into workflow definition blocks.@@ -884,7 +885,9 @@ class Processor:# draft steps carry no locators and act only as an edit overlay.events = self.compressed_chunks_to_events(compressed_chunks)actions = self.events_to_actions(events)- code_first_result = actions_to_code_first_blocks(actions, draft_steps)+ code_first_result = actions_to_code_first_blocks(+ actions, draft_steps, bind_credentials=supports_credential_tokens+ )if code_first_result is not None:code_blocks, code_parameters = code_first_resultLOG.info(@@ -936,6 +939,7 @@ class BrowserSessionRecordingService:compressed_chunks: list[str],draft_steps: list[RecordingDraftStep] | None = None,code_first: bool = False,+ supports_credential_tokens: bool = False,) -> tuple[list[ProcessedBlock], list[WorkflowDefinitionYamlParametersItem]]:"""Process compressed browser session recording events into workflow definition blocks.@@ -946,7 +950,12 @@ class BrowserSessionRecordingService:workflow_permanent_id,)- return await processor.process(compressed_chunks, draft_steps=draft_steps, code_first=code_first)+ return await processor.process(+ compressed_chunks,+ draft_steps=draft_steps,+ code_first=code_first,+ supports_credential_tokens=supports_credential_tokens,+ )async def smoke() -> None:diff --git a/skyvern/services/script_service.py b/skyvern/services/script_service.pyindex 091f45426..6c90a1a3f 100644--- a/skyvern/services/script_service.py+++ b/skyvern/services/script_service.py@@ -827,6 +827,7 @@ async def _create_video_artifact(step=step,artifact_type=ArtifactType.RECORDING,data=video_artifact.video_data,+ file_extension=video_artifact.video_file_extension,)video_artifacts[idx].video_artifact_id = video_artifact_idapp.BROWSER_MANAGER.set_video_artifact_for_task(task, video_artifacts)diff --git a/skyvern/webeye/actions/handler.py b/skyvern/webeye/actions/handler.pyindex 8f6ea9956..99d70afd7 100644--- a/skyvern/webeye/actions/handler.py+++ b/skyvern/webeye/actions/handler.py@@ -573,6 +573,14 @@ def _normalized_select_shadow_field(text: str | None) -> str | None:return _truncate_select_shadow_field(_normalize_select_shadow_text(text))+def _collapse_select_shadow_field(text: str | None) -> str | None:+ # Collapse whitespace runs before the length cap so a padded option row's identity (name + digits)+ # survives truncation instead of the padding consuming it; case and punctuation are preserved verbatim.+ if text is None:+ return None+ return _truncate_select_shadow_field(" ".join(text.split()))++class SelectShadowAgreement(BaseModel):agrees: bool | Nonellm_index: int | None = None@@ -737,8 +745,8 @@ def _autocomplete_commit_evidence(return Noneif not _is_boundary_fragment(normalized_post, normalized_label):return None- committed_option = _truncate_select_shadow_field(option_label)- committed_value = _truncate_select_shadow_field(post_value)+ committed_option = _collapse_select_shadow_field(option_label)+ committed_value = _collapse_select_shadow_field(post_value)if not committed_option or not committed_value:return Nonereturn committed_option, committed_value@@ -7068,8 +7076,8 @@ def _ui_select_commit_result(def _accept(observed: object) -> ActionResult:result = ActionSuccess(- committed_option=_truncate_select_shadow_field(candidate),- committed_value=_truncate_select_shadow_field(str(observed)),+ committed_option=_collapse_select_shadow_field(candidate),+ committed_value=_collapse_select_shadow_field(str(observed)),)action.set_has_mini_agent()if action.stop_batch_after_dropdown_select:diff --git a/skyvern/webeye/browser_artifacts.py b/skyvern/webeye/browser_artifacts.pyindex 6ce957ad1..e08d2b171 100644--- a/skyvern/webeye/browser_artifacts.py+++ b/skyvern/webeye/browser_artifacts.py@@ -85,6 +85,15 @@ class BrowserArtifacts(BaseModel):# None when none was requested or the chosen creator could not load one (remote/vendor browsers,# storage miss, corruption fallback) — consumers must treat None as "profile not applied".applied_browser_profile_id: str | None = None+ local_display_recording_eligible: bool = False+ # ((input_w, input_h), (output_w, output_h)) for the whole-display recorder, resolved from the final+ # launch --window-size at the configure seam and carried to the acquire seam. None → fixed full screen.+ _display_capture_sizes: tuple[tuple[int, int], tuple[int, int]] | None = PrivateAttr(default=None)+ _display_recorder: object | None = PrivateAttr(default=None)+ # The pre-context acquire seam stashes its DisplayRecorderAcquisition here so the shared consumer can set+ # up ``started_display_recorder`` for the outer cleanup (release a newly-started recorder on a later+ # context-creation failure; leave a reused/adopted one live). Object-typed to avoid a display_recorder import.+ _display_recorder_acquisition: object | None = PrivateAttr(default=None)_browser_console_log_lock: asyncio.Lock = PrivateAttr(default_factory=asyncio.Lock)# Tombstoned synchronously before any await, so set_popup_video_listener can't# re-register a page's video after RealBrowserState decides to discard it.diff --git a/skyvern/webeye/browser_errors.py b/skyvern/webeye/browser_errors.pyindex 0d6edb5f5..894c1d220 100644--- a/skyvern/webeye/browser_errors.py+++ b/skyvern/webeye/browser_errors.py@@ -62,6 +62,10 @@ class BrowserTargetClosedError(BrowserAutomationError):"""The target page, frame, context, or browser was closed underneath an operation."""+class BrowserCdpAcquisitionError(BrowserAutomationError):+ """A selected browser driver failed during the CDP connect invocation."""++class BrowserCdpConnectionError(BrowserAutomationError):"""A CDP/transport-level failure talking to the browser."""diff --git a/skyvern/webeye/browser_factory.py b/skyvern/webeye/browser_factory.pyindex 5c351db98..31f672b5e 100644--- a/skyvern/webeye/browser_factory.py+++ b/skyvern/webeye/browser_factory.py@@ -70,6 +70,13 @@ from skyvern.webeye.cdp_connection import ()from skyvern.webeye.cdp_download_interceptor import CDPDownloadInterceptor, bind_download_interceptor_to_contextfrom skyvern.webeye.dialog_handler import set_dialog_handler+from skyvern.webeye.display_recorder import (+ DisplayRecorderAcquisition,+ carry_display_recording,+ prepare_local_display_recording,+ release_display_recorder,+ release_started_display_recording,+)from skyvern.webeye.playwright_input import register_playwright_input_contextfrom skyvern.webeye.session_cookies import restore_banked_cookies, restore_session_cookies@@ -753,6 +760,7 @@ class BrowserContextFactory:browser_type = settings.BROWSER_TYPEbrowser_context: BrowserContext | None = Nonecleanup_func: BrowserCleanupFunc = None+ started_display_recorder = Nonebrowser_internal_headers, scoped_headers = _partition_browser_headers(kwargs.get("extra_http_headers"))creator_kwargs = {**kwargs, "extra_http_headers": browser_internal_headers}try:@@ -760,6 +768,14 @@ class BrowserContextFactory:if not creator:raise UnknownBrowserType(browser_type)browser_context, browser_artifacts, cleanup_func = await creator(playwright, **creator_kwargs)+ # The creator acquired the whole-display recorder before creating the context+ # (prepare_local_display_recording) and stashed the acquisition. Register a NEWLY-started recorder+ # for the outer cleanup below so a later context-setup failure releases it; a reused/adopted recorder+ # (started=False) belongs to a live owner and is deliberately left running.+ acquisition = browser_artifacts._display_recorder_acquisition+ if isinstance(acquisition, DisplayRecorderAcquisition) and acquisition.started:+ started_display_recorder = acquisition.recorder+ browser_artifacts._display_recorder_acquisition = Nonerequested_profile_id = cast(str | None, kwargs.get("browser_profile_id"))if requested_profile_id and browser_artifacts.applied_browser_profile_id != requested_profile_id:LOG.warning(@@ -780,16 +796,11 @@ class BrowserContextFactory:await _capture_seed_profile_state(browser_context, browser_artifacts, kwargs)if settings.BROWSER_LOGS_ENABLED:set_browser_console_log(browser_context=browser_context, browser_artifacts=browser_artifacts)- # Gated on the process MODE, not the browser type. The earlier version keyed on- # is_attach_only_browser_type(settings.BROWSER_TYPE) and justified it with "an attached- # browser was configured by whoever launched it, so recording could only no-op" -- which- # is false: Playwright records video on a context IT created over connect_over_cdp- # regardless of who launched the browser. That gate therefore dropped video for every- # process using an attach-capable BROWSER_TYPE, and since this listener is the only- # producer of video_artifacts it took the main page's recording with it, not just popups.- # The thing that genuinely cannot record is the attach-only worker, which is what- # is_enforcing() names.- if not attach_only_enforcing():+ # Playwright popup/page video is the ONLY producer of video_artifacts here, so register it whenever no+ # whole-display recorder was acquired (an eligible-but-refused acquisition must still fall back). Gate+ # on the attach-only WORKER mode, not BROWSER_TYPE (Playwright records a connect_over_cdp context it+ # created regardless of who launched the browser).+ if not attach_only_enforcing() and browser_artifacts._display_recorder is None:set_popup_video_listener(browser_context=browser_context, browser_artifacts=browser_artifacts)set_download_file_listener(browser_context=browser_context, **kwargs)set_dialog_handler(browser_context=browser_context)@@ -820,17 +831,35 @@ class BrowserContextFactory:context = ensure_context()context.tz_info = get_tzinfo_from_proxy(proxy_location)+ # The whole-display recorder was already acquired by the creator before context creation and+ # consumed into ``started_display_recorder`` above; nothing to acquire here.return browser_context, browser_artifacts, cleanup_funcexcept BaseException as e:+ # Close the context FIRST (unmap the window) so the display fence is never freed while the window is+ # still mapped; a close cancel is latched so the recorder still releases and cancellation wins after.+ close_cancelled = Falseif browser_context is not None:# FIXME: sometimes it can't close the browser context?LOG.error("unexpected error happens after created browser context, going to close the context")- with suppress(Exception):+ try:await browser_context.close()+ except asyncio.CancelledError:+ close_cancelled = True+ except Exception:+ LOG.warning("Failed to close browser context after creation failure", exc_info=True)+ if started_display_recorder is not None:+ try:+ await release_display_recorder(started_display_recorder)+ except asyncio.CancelledError:+ close_cancelled = True # release finishes-then-re-raises; still run cleanup_func, cancel wins+ except Exception:+ LOG.warning("Failed to release display recorder after creation failure", exc_info=True)if cleanup_func:with suppress(Exception):await cleanup_func()+ if close_cancelled:+ raise asyncio.CancelledError()if not isinstance(e, Exception) or isinstance(e, (UnknownBrowserType, BrowserEngineBootstrapError)):raise e@@ -1000,7 +1029,6 @@ async def _create_headless_chromium("downloads_path": download_dir,})-browser_artifacts = BrowserContextFactory.build_browser_artifacts(har_path=browser_args["record_har_path"],browser_session_dir=user_data_dir,@@ -1104,30 +1132,51 @@ async def _create_headful_chromium()if loaded_from_saved_profile:browser_artifacts.applied_browser_profile_id = browser_profile_id+ # Acquire the whole-display recorder BEFORE the context is created; the Playwright record_video_* args are+ # popped ONLY if a live recorder is obtained, so a refusal/startup failure keeps Playwright per-page+ # recording (exactly one recording either way).+ await prepare_local_display_recording(+ browser_args,+ browser_artifacts,+ owner_id_override=cast(str | None, kwargs.get("display_recording_owner_id")),+ workflow_run_id=cast(str | None, kwargs.get("workflow_run_id")),+ task_id=cast(str | None, kwargs.get("task_id")),+ script_id=cast(str | None, kwargs.get("script_id")),+ )+ # Outer BaseException holder: release the already-started whole-display recorder for ANY escape (including a+ # Temporal CancelledError) from launch through return, so the bridge/flock/registry never leak (SKY-15466).try:- browser_context = await playwright.chromium.launch_persistent_context(**browser_args)- except Exception as launch_error:- if loaded_from_saved_profile and _is_browser_profile_corruption_error(launch_error):- LOG.warning(- "Browser launch failed with saved profile — profile may be corrupted, falling back to fresh profile",- browser_profile_id=browser_profile_id,- organization_id=organization_id_for_profile,- error=str(launch_error),- )- fallback_dir = make_temp_directory(prefix="skyvern_browser_")- BrowserContextFactory.update_chromium_browser_preferences(- user_data_dir=fallback_dir,- download_dir=download_dir,- )- browser_args["user_data_dir"] = fallback_dir- browser_artifacts = BrowserContextFactory.build_browser_artifacts(- har_path=browser_args["record_har_path"],- browser_session_dir=fallback_dir,- )- browser_artifacts.mark_seed_load_failed()+ try:browser_context = await playwright.chromium.launch_persistent_context(**browser_args)- else:- raise+ except Exception as launch_error:+ if loaded_from_saved_profile and _is_browser_profile_corruption_error(launch_error):+ LOG.warning(+ "Browser launch failed with saved profile — profile may be corrupted, falling back to fresh profile",+ browser_profile_id=browser_profile_id,+ organization_id=organization_id_for_profile,+ error=str(launch_error),+ )+ fallback_dir = make_temp_directory(prefix="skyvern_browser_")+ BrowserContextFactory.update_chromium_browser_preferences(+ user_data_dir=fallback_dir,+ download_dir=download_dir,+ )+ browser_args["user_data_dir"] = fallback_dir+ fallback_artifacts = BrowserContextFactory.build_browser_artifacts(+ har_path=browser_args["record_har_path"],+ browser_session_dir=fallback_dir,+ )+ fallback_artifacts.mark_seed_load_failed()+ # Recorder already started before the first launch; carry it onto the rebuilt artifacts so it is not+ # orphaned. record_video_* was already popped from the shared browser_args, so no double recording.+ carry_display_recording(browser_artifacts, fallback_artifacts)+ browser_artifacts = fallback_artifacts+ browser_context = await playwright.chromium.launch_persistent_context(**browser_args)+ else:+ raise+ except BaseException:+ await release_started_display_recording(browser_artifacts)+ raisereturn browser_context, browser_artifacts, Nonediff --git a/skyvern/webeye/cdp_retry.py b/skyvern/webeye/cdp_retry.pyindex e87e215b7..3267623a1 100644--- a/skyvern/webeye/cdp_retry.py+++ b/skyvern/webeye/cdp_retry.py@@ -15,6 +15,7 @@ from skyvern.config import settingsfrom skyvern.exceptions import BlockedHostfrom skyvern.utils.url_validators import is_allowed_local_browser_host, resolve_fetch_host_ips, validate_browser_hostfrom skyvern.webeye.browser_errors import (+ BrowserCdpAcquisitionError,BrowserCdpConnectionError,BrowserTargetClosedError,BrowserTimeoutError,@@ -26,6 +27,8 @@ if TYPE_CHECKING:LOG = structlog.get_logger()+_CDP_ACQUISITION_DIAGNOSTIC = "Browser driver failed during CDP connect to [remote browser endpoint]."+_CDP_CONNECTION_ERROR_SUBSTR_FALLBACK = ("econnrefused","econnreset",@@ -94,6 +97,12 @@ def is_cdp_connection_error(exc: Exception, selection: BrowserEngineSelection |return False+def _is_selected_engine_error(exc: BaseException, selection: BrowserEngineSelection | None) -> bool:+ if selection is not None:+ return selection.is_engine_error(exc)+ return isinstance(exc, PWError)++# Patch this module alias in tests so shard-wide asyncio.sleep mocks do not leak call counts._sleep = asyncio.sleep@@ -143,10 +152,12 @@ async def connect_over_cdp_with_retry(max_attempts = 1address_validated = not validate_browser_addressfor attempt in range(1, max_attempts + 1):+ connect_started = Falsetry:if not address_validated:await _validate_browser_address_host(browser_address)address_validated = True+ connect_started = Truebrowser = await playwright.chromium.connect_over_cdp(browser_address, headers=headers)if attempt > 1:LOG.info(@@ -162,6 +173,8 @@ async def connect_over_cdp_with_retry(is_cdp_error = is_cdp_connection_error(e, selection)is_retryable_error = is_resolution_error or is_cdp_errorif not is_retryable_error or attempt == max_attempts:+ if not is_retryable_error and connect_started and _is_selected_engine_error(e, selection):+ raise BrowserCdpAcquisitionError(_CDP_ACQUISITION_DIAGNOSTIC) from Noneif log_browser_address is not None:error_type = BrowserCdpConnectionError if is_retryable_error else RuntimeErrorraise error_type(f"CDP connection to {log_browser_address} failed ({type(e).__name__})") from Nonediff --git a/skyvern/webeye/display_capture_bridge.py b/skyvern/webeye/display_capture_bridge.pynew file mode 100644index 000000000..fd7ded424--- /dev/null+++ b/skyvern/webeye/display_capture_bridge.py@@ -0,0 +1,802 @@+"""Change-driven whole-display capture source for the exclusive display recorder.++Watches XDamage across the window tree, captures the root image via MIT-SHM (zero-copy) at most ``--max-fps``/s+(coalescing bursts), and pipes raw frames to a single FFmpeg encoding a wall-clock VFR fragmented H.264 MP4 in+one pass (no second transcode). A static screen emits no frames (~0 CPU); PTS gaps + a terminal frame preserve+the wall-clock timeline. Invoked as a subprocess (``python -m skyvern.webeye.display_capture_bridge ...``) that+owns its FFmpeg child; ctypes loads inside functions so this imports cleanly on non-Linux (OSS surface).++Reliability: NO ``+faststart`` + ``-flush_packets 1`` so fragments hit disk as they close (an abrupt kill leaves+an MP4 decodable to the last fragment); a SIGINT/SIGTERM terminal frame keeps the finalized duration truthful;+the SysV shm segment is IPC_RMID'd at attach and detached on exit (no leak).+"""++from __future__ import annotations++import argparse+import ctypes+import ctypes.util+import json+import os+import select+import signal+import subprocess+import sys+import time+from pathlib import Path+from typing import Any++ZPixmap = 2+AllPlanes = 0xFFFFFFFF+XDamageReportRawRectangles = 0+SubstructureNotifyMask = 1 << 19+IsViewable = 2 # XWindowAttributes.map_state: mapped AND all ancestors mapped -> contributes visible pixels+DestroyNotify = 17 # X core event type; a destroyed window's damage object is auto-freed by the server+IPC_CREAT = 0o1000+IPC_RMID = 0++# Distinct FATAL exit codes so the readiness gate fails closed with an attributable reason — a nonzero exit in+# the startup window aborts acquisition (Playwright suppression already happened), beating a zero-frame file.+EXIT_OPEN_DISPLAY = 2+EXIT_NO_XDAMAGE = 3+EXIT_UNSUPPORTED_BPP = 5+EXIT_NO_XSHM = 6+EXIT_XSHMCREATEIMAGE = 7+EXIT_SHMGET = 8+EXIT_SHMAT = 9+EXIT_XSHMATTACH = 10+EXIT_READINESS = 11+EXIT_STRIDE_MISMATCH = 12++# Fragment flush cadence (s), DECOUPLED from the larger keyframe interval so an early per-step read sees a+# decodable fragment (~1s) not a 0-frame header until the second keyframe. Cost +3.4% size; crash loss ~1s.+FRAGMENT_FLUSH_SECONDS = 1+++def _shmat_failed(addr: int | None) -> bool:+ """shmat returns (void*)-1 on failure (and ctypes may surface it as None or the all-ones mask)."""+ if addr is None:+ return True+ return addr in (0, 0xFFFFFFFF, 0xFFFFFFFFFFFFFFFF, -1)+++def _stride_consistent(bytes_per_line: int, width: int, bpp: int) -> bool:+ """FFmpeg gets -video_size WxH + bytes_per_line*height bytes, so require a packed stride (no row padding)."""+ return bpp % 8 == 0 and bytes_per_line == width * (bpp // 8)+++def _allocate_and_attach_shm(+ X: Any, XE: Any, LIBC: Any, dpy: Any, root: int, shm_image: Any, shm_info: Any, frame_bytes: int, err: dict+) -> tuple[int | None, int]:+ """Allocate + attach the MIT-SHM segment and prove readiness, failing closed with an attributable code and+ NO leaked segment/attach (each branch reclaims exactly what it created). Returns (mapped_address, 0) or+ (None, EXIT_*). Ordering: shmget<0 -> EXIT_SHMGET; shmat==(void*)-1 -> EXIT_SHMAT; XShmAttach error ->+ EXIT_XSHMATTACH; initial XShmGetImage fails -> EXIT_READINESS. Extracted for direct failure-branch testing."""+ shmid = LIBC.shmget(0, frame_bytes, IPC_CREAT | 0o600)+ if shmid < 0:+ return None, EXIT_SHMGET+ addr = LIBC.shmat(shmid, None, 0)+ if _shmat_failed(addr):+ LIBC.shmctl(shmid, IPC_RMID, None) # reclaim the segment we just created+ return None, EXIT_SHMAT+ shm_info.shmid = shmid+ shm_info.shmaddr = addr+ shm_image.contents.data = addr # XShmGetImage writes into the shm via the XImage data pointer+ shm_info.readOnly = 0+ err_before_attach = err["n"]+ XE.XShmAttach(dpy, ctypes.byref(shm_info))+ X.XSync(dpy, 0) # flush so a failed XShmAttach reaches the swallowing error handler and bumps err["n"]+ if err["n"] != err_before_attach:+ LIBC.shmdt(addr)+ LIBC.shmctl(shmid, IPC_RMID, None)+ return None, EXIT_XSHMATTACH+ LIBC.shmctl(shmid, IPC_RMID, None) # free on detach; segment persists while attached (no leak)+ # Readiness receipt: the FIRST XShmGetImage must succeed before FFmpeg is spawned — otherwise the+ # recorder would suppress Playwright and then silently produce a zero-frame recording.+ if not XE.XShmGetImage(dpy, root, shm_image, 0, 0, AllPlanes):+ XE.XShmDetach(dpy, ctypes.byref(shm_info))+ LIBC.shmdt(addr)+ return None, EXIT_READINESS+ return addr, 0+++_SHM_FATAL_MESSAGE = {+ EXIT_SHMGET: "shmget failed",+ EXIT_SHMAT: "shmat failed",+ EXIT_XSHMATTACH: "XShmAttach failed",+ EXIT_READINESS: "initial XShmGetImage failed",+}+++class _XImage(ctypes.Structure):+ _fields_ = [+ ("width", ctypes.c_int),+ ("height", ctypes.c_int),+ ("xoffset", ctypes.c_int),+ ("format", ctypes.c_int),+ ("data", ctypes.c_void_p),+ ("byte_order", ctypes.c_int),+ ("bitmap_unit", ctypes.c_int),+ ("bitmap_bit_order", ctypes.c_int),+ ("bitmap_pad", ctypes.c_int),+ ("depth", ctypes.c_int),+ ("bytes_per_line", ctypes.c_int),+ ("bits_per_pixel", ctypes.c_int),+ ("red_mask", ctypes.c_ulong),+ ("green_mask", ctypes.c_ulong),+ ("blue_mask", ctypes.c_ulong),+ ("obdata", ctypes.c_void_p),+ ]+++class _XShmSegmentInfo(ctypes.Structure):+ _fields_ = [+ ("shmseg", ctypes.c_ulong),+ ("shmid", ctypes.c_int),+ ("shmaddr", ctypes.c_void_p),+ ("readOnly", ctypes.c_int),+ ]+++class _XEvent(ctypes.Union):+ _fields_ = [("type", ctypes.c_int), ("pad", ctypes.c_long * 24)] # noqa: RUF012 - ctypes field spec+++class _XErrorEvent(ctypes.Structure):+ # Leading prefix of Xlib's XErrorEvent; only error_code is read, to tell a tolerable BadDamage race on a+ # since-reaped window from an unrelated X error that must not be silently swallowed.+ _fields_ = [+ ("type", ctypes.c_int),+ ("display", ctypes.c_void_p),+ ("resourceid", ctypes.c_ulong),+ ("serial", ctypes.c_ulong),+ ("error_code", ctypes.c_ubyte),+ ("request_code", ctypes.c_ubyte),+ ("minor_code", ctypes.c_ubyte),+ ]+++class _XWindowAttributes(ctypes.Structure):+ # Full Xlib XWindowAttributes: XGetWindowAttributes writes the WHOLE struct, so every field must be present to+ # size the buffer correctly (a short struct would be a buffer overrun). Only map_state is read.+ _fields_ = [+ ("x", ctypes.c_int),+ ("y", ctypes.c_int),+ ("width", ctypes.c_int),+ ("height", ctypes.c_int),+ ("border_width", ctypes.c_int),+ ("depth", ctypes.c_int),+ ("visual", ctypes.c_void_p),+ ("root", ctypes.c_ulong),+ ("win_class", ctypes.c_int),+ ("bit_gravity", ctypes.c_int),+ ("win_gravity", ctypes.c_int),+ ("backing_store", ctypes.c_int),+ ("backing_planes", ctypes.c_ulong),+ ("backing_pixel", ctypes.c_ulong),+ ("save_under", ctypes.c_int),+ ("colormap", ctypes.c_ulong),+ ("map_installed", ctypes.c_int),+ ("map_state", ctypes.c_int),+ ("all_event_masks", ctypes.c_long),+ ("your_event_mask", ctypes.c_long),+ ("do_not_propagate_mask", ctypes.c_long),+ ("override_redirect", ctypes.c_int),+ ("screen", ctypes.c_void_p),+ ]+++class _XSubstructureEvent(ctypes.Structure):+ # Shared leading layout of XDestroyWindowEvent/XUnmapEvent delivered via SubstructureNotifyMask; ``window`` is+ # the affected child (``event`` is the parent the mask was selected on). Only ``window`` is read.+ _fields_ = [+ ("type", ctypes.c_int),+ ("serial", ctypes.c_ulong),+ ("send_event", ctypes.c_int),+ ("display", ctypes.c_void_p),+ ("event", ctypes.c_ulong),+ ("window", ctypes.c_ulong),+ ]+++def windows_to_prune(tracked: set[int] | dict[int, int], viewable: set[int], root: int) -> list[int]:+ """XIDs whose damage handle must be dropped: any tracked window that is no longer viewable — destroyed (absent+ from the tree) OR unmapped/unviewable (present but not IsViewable). The root is always retained."""+ return [w for w in tracked if w != root and w not in viewable]+++def _destroy_owned_handle(+ XD: Any, X: Any, dpy: Any, dh: int, w: int, err: dict, bad_damage_code: int, context: str+) -> None:+ """Destroy one owned XDamage handle, bracketed by XSync so a queued error is attributable to this request.+ ONLY the exact BadDamage code (a window the server already reaped) is tolerated silently; any other code — or+ an unreadable/unknown code (``None``) — keeps a scoped stderr diagnostic rather than being masked as BadDamage."""+ before = err["n"]+ XD.XDamageDestroy(dpy, dh)+ X.XSync(dpy, 0) # flush so a queued destroy error reaches the swallowing handler and bumps err["n"]/err["code"]+ if err["n"] != before and err.get("code") != bad_damage_code:+ print(+ f"WARN: unexpected X error code {err.get('code')} destroying {context} damage handle for 0x{w:x}",+ file=sys.stderr,+ flush=True,+ )+++def prune_unviewable_windows(+ XD: Any, X: Any, dpy: Any, damaged: dict[int, int], viewable: set[int], root: int, err: dict, bad_damage_code: int+) -> int:+ """Destroy and drop the handles of tracked windows that are no longer viewable (destroyed OR unmapped), never+ the root. Pop-before-destroy makes each owned handle destroyed at most once, so a re-mapped or XID-reused+ window re-arms fresh instead of inheriting stale state. Returns the count pruned."""+ stale = windows_to_prune(damaged, viewable, root)+ for w in stale:+ _destroy_owned_handle(XD, X, dpy, damaged.pop(w), w, err, bad_damage_code, "unviewable")+ return len(stale)+++def reconcile_destroyed_window(damaged: dict[int, int], w: int, root: int) -> bool:+ """Event-driven: on a DestroyNotify, drop the destroyed window's entry immediately (before any rescan) so a+ same-XID reuse in the sub-rescan interval cannot inherit its now-invalid handle. The server auto-frees the+ damage object with the window, so no XDamageDestroy is issued here (it would only queue a guaranteed+ BadDamage). No-op for the root or an untracked window. Returns True if an entry was dropped."""+ if w == root or w not in damaged:+ return False+ damaged.pop(w)+ return True+++def _destroy_all_handles(XD: Any, X: Any, dpy: Any, damaged: dict[int, int]) -> None:+ """Teardown: destroy every remaining owned XDamage handle (child + root) exactly once. Best-effort — a+ handle whose window is already gone raises BadDamage into the swallowing handler, not out of teardown."""+ while damaged:+ _w, dh = damaged.popitem()+ try:+ XD.XDamageDestroy(dpy, dh)+ except Exception: # noqa: BLE001,S110 - teardown must not raise; leftover handles die with the connection+ pass+ try:+ X.XSync(dpy, 0)+ except Exception: # noqa: BLE001,S110 - best-effort flush at teardown+ pass+++def build_bridge_command(+ *,+ display: str,+ output_path: str,+ input_width: int,+ input_height: int,+ output_width: int,+ output_height: int,+ max_fps: int,+ crf: int,+ keyframe_seconds: int,+ stats_path: str | None = None,+ python_executable: str | None = None,+) -> list[str]:+ """Argv running this bridge as a subprocess — the single narrow seam a future native-C source can replace."""+ argv = [+ python_executable or sys.executable,+ "-m",+ "skyvern.webeye.display_capture_bridge",+ "--display",+ display,+ "--size",+ f"{input_width}x{input_height}",+ "--out-size",+ f"{output_width}x{output_height}",+ "--max-fps",+ str(max_fps),+ "--crf",+ str(crf),+ "--keyframe-sec",+ str(keyframe_seconds),+ "--out",+ output_path,+ ]+ if stats_path:+ argv += ["--stats", stats_path]+ return argv+++def _load_x() -> tuple[ctypes.CDLL, ctypes.CDLL, ctypes.CDLL, ctypes.CDLL]:+ X = ctypes.CDLL(ctypes.util.find_library("X11") or "libX11.so.6")+ XD = ctypes.CDLL(ctypes.util.find_library("Xdamage") or "libXdamage.so.1")+ XE = ctypes.CDLL(ctypes.util.find_library("Xext") or "libXext.so.6")+ LIBC = ctypes.CDLL("libc.so.6", use_errno=True)+ D = ctypes.c_void_p+ W = ctypes.c_ulong+ X.XOpenDisplay.restype = D+ X.XOpenDisplay.argtypes = [ctypes.c_char_p]+ X.XDefaultRootWindow.restype = W+ X.XDefaultRootWindow.argtypes = [D]+ X.XDefaultScreen.restype = ctypes.c_int+ X.XDefaultScreen.argtypes = [D]+ X.XDefaultVisual.restype = D+ X.XDefaultVisual.argtypes = [D, ctypes.c_int]+ X.XDefaultDepth.restype = ctypes.c_int+ X.XDefaultDepth.argtypes = [D, ctypes.c_int]+ X.XConnectionNumber.restype = ctypes.c_int+ X.XConnectionNumber.argtypes = [D]+ X.XPending.restype = ctypes.c_int+ X.XPending.argtypes = [D]+ X.XNextEvent.argtypes = [D, ctypes.POINTER(_XEvent)]+ X.XSync.argtypes = [D, ctypes.c_int]+ X.XSelectInput.argtypes = [D, W, ctypes.c_long]+ X.XGetWindowAttributes.restype = ctypes.c_int+ X.XGetWindowAttributes.argtypes = [D, W, ctypes.POINTER(_XWindowAttributes)]+ X.XQueryTree.restype = ctypes.c_int+ X.XQueryTree.argtypes = [+ D,+ W,+ ctypes.POINTER(W),+ ctypes.POINTER(W),+ ctypes.POINTER(ctypes.POINTER(W)),+ ctypes.POINTER(ctypes.c_uint),+ ]+ X.XFree.argtypes = [ctypes.c_void_p]+ X.XSetErrorHandler.restype = ctypes.c_void_p+ X.XSetErrorHandler.argtypes = [ctypes.c_void_p]+ XD.XDamageQueryExtension.restype = ctypes.c_int+ XD.XDamageQueryExtension.argtypes = [D, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)]+ XD.XDamageCreate.restype = W+ XD.XDamageCreate.argtypes = [D, W, ctypes.c_int]+ XD.XDamageSubtract.argtypes = [D, W, ctypes.c_ulong, ctypes.c_ulong]+ XD.XDamageDestroy.argtypes = [D, W]+ XE.XShmQueryExtension.restype = ctypes.c_int+ XE.XShmQueryExtension.argtypes = [D]+ XE.XShmCreateImage.restype = ctypes.POINTER(_XImage)+ XE.XShmCreateImage.argtypes = [+ D,+ D,+ ctypes.c_uint,+ ctypes.c_int,+ ctypes.c_char_p,+ ctypes.POINTER(_XShmSegmentInfo),+ ctypes.c_uint,+ ctypes.c_uint,+ ]+ XE.XShmAttach.argtypes = [D, ctypes.POINTER(_XShmSegmentInfo)]+ XE.XShmDetach.argtypes = [D, ctypes.POINTER(_XShmSegmentInfo)]+ XE.XShmGetImage.restype = ctypes.c_int+ XE.XShmGetImage.argtypes = [D, W, ctypes.POINTER(_XImage), ctypes.c_int, ctypes.c_int, ctypes.c_ulong]+ LIBC.shmget.restype = ctypes.c_int+ LIBC.shmget.argtypes = [ctypes.c_int, ctypes.c_size_t, ctypes.c_int]+ LIBC.shmat.restype = ctypes.c_void_p+ LIBC.shmat.argtypes = [ctypes.c_int, ctypes.c_void_p, ctypes.c_int]+ LIBC.shmdt.argtypes = [ctypes.c_void_p]+ LIBC.shmctl.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_void_p]+ return X, XD, XE, LIBC+++def _enumerate_windows(X: ctypes.CDLL, dpy: Any, w: int, depth: int, acc: list[int], maxdepth: int = 6) -> None:+ acc.append(w)+ if depth >= maxdepth:+ return+ rr = ctypes.c_ulong()+ pr = ctypes.c_ulong()+ ch = ctypes.POINTER(ctypes.c_ulong)()+ n = ctypes.c_uint()+ if X.XQueryTree(dpy, w, ctypes.byref(rr), ctypes.byref(pr), ctypes.byref(ch), ctypes.byref(n)):+ for i in range(n.value):+ _enumerate_windows(X, dpy, ch[i], depth + 1, acc, maxdepth)+ if ch:+ X.XFree(ch)+++def _write_all(fd: int, buf: Any) -> int:+ """Write the whole frame, tolerating short writes and EINTR. Returns bytes written, or -1 on EPIPE."""+ mv = memoryview(buf)+ total = 0+ n = len(mv)+ while total < n:+ try:+ w = os.write(fd, mv[total:])+ except InterruptedError:+ continue+ except BrokenPipeError:+ return -1+ if w == 0:+ return -1+ total += w+ return total+++def build_ffmpeg_command(+ *,+ pix_fmt: str,+ input_width: int,+ input_height: int,+ output_width: int,+ output_height: int,+ max_fps: int,+ crf: int,+ keyframe_seconds: int,+ output_path: str,+) -> list[str]:+ no_scale = (input_width, input_height) == (output_width, output_height)+ vf = "format=yuv420p" if no_scale else f"scale={output_width}:{output_height},format=yuv420p"+ return [+ "ffmpeg",+ "-hide_banner",+ "-loglevel",+ "warning",+ "-y",+ "-f",+ "rawvideo",+ "-pix_fmt",+ pix_fmt,+ "-video_size",+ f"{input_width}x{input_height}",+ "-framerate",+ str(max_fps),+ "-use_wallclock_as_timestamps",+ "1",+ "-i",+ "-",+ "-vf",+ vf,+ "-an",+ "-c:v",+ "libx264",+ "-preset",+ "ultrafast",+ "-tune",+ "zerolatency",+ "-crf",+ str(crf),+ "-pix_fmt",+ "yuv420p",+ "-threads",+ "1",+ "-force_key_frames",+ f"expr:gte(t,n_forced*{keyframe_seconds})",+ "-flush_packets",+ "1",+ # NO +faststart (a close-time moov rewrite an abrupt kill would leave unusable); empty_moov streams a+ # header up front, frag_keyframe/frag_duration + flush_packets close+flush fragments (see FRAGMENT_FLUSH_SECONDS).+ "-movflags",+ "+frag_keyframe+empty_moov+default_base_moof",+ "-frag_duration",+ str(FRAGMENT_FLUSH_SECONDS * 1_000_000),+ "-fps_mode",+ "vfr",+ output_path,+ ]+++# Two-phase capture (SKY-15466): writing the first XDamage before the browser context exists would hold the empty+# (about:blank/black) window as a long startup head, so NO frames write until arm; frame zero = ready page (PTS 0).+def arm_transition(armed: bool, arm_consumed: bool) -> bool:+ return armed and not arm_consumed+++def should_emit(arm_consumed: bool, pending_dirty: bool, interval_ok: bool) -> bool:+ """Steady-state: emit a frame only after arm, on pending damage, once the min-interval has elapsed."""+ return arm_consumed and pending_dirty and interval_ok+++def should_write_terminal(frames_written: int) -> bool:+ return frames_written > 0+++def should_unlink_output(frames_written: int) -> bool:+ return frames_written == 0+++def _finalize_return_code(frames_written: int, ffmpeg_returncode: int | None, arm_consumed: bool) -> int:+ """frames>0: preserve the ffmpeg result. frames==0: clean (0) only for an UNARMED teardown, else nonzero."""+ if frames_written > 0:+ return 0 if ffmpeg_returncode == 0 else 1+ return 1 if arm_consumed else 0+++def _run(args: argparse.Namespace) -> int:+ gw, gh = (int(v) for v in args.size.split("x"))+ ow, oh = (int(v) for v in args.out_size.split("x"))+ # Install ONLY the SIGUSR1 arm handler before startup, then unblock it (the PDEATHSIG trampoline blocked it+ # pre-exec, so an arm in the exec window stays pending, not default-killing the bridge). A failed unblock must+ # propagate so the bridge startup-fails and acquisition falls back rather than run with SIGUSR1 blocked.+ armed = {"v": False}+ signal.signal(signal.SIGUSR1, lambda *a: armed.__setitem__("v", True))+ signal.pthread_sigmask(signal.SIG_UNBLOCK, {signal.SIGUSR1})+ X, XD, XE, LIBC = _load_x()+ dpy = X.XOpenDisplay(args.display.encode())+ if not dpy:+ print("FATAL: XOpenDisplay failed", file=sys.stderr, flush=True)+ return EXIT_OPEN_DISPLAY+ scr = X.XDefaultScreen(dpy)+ root = X.XDefaultRootWindow(dpy)+ eb = ctypes.c_int(0)+ erb = ctypes.c_int(0)+ if not XD.XDamageQueryExtension(dpy, ctypes.byref(eb), ctypes.byref(erb)):+ print("FATAL: XDamage unavailable", file=sys.stderr, flush=True)+ return EXIT_NO_XDAMAGE+ if not XE.XShmQueryExtension(dpy):+ print("FATAL: MIT-SHM unavailable", file=sys.stderr, flush=True)+ return EXIT_NO_XSHM+ damage_notify = eb.value+ bad_damage_code = erb.value # XDamage's first (only) error code; a destroy on a reaped window raises it++ # Chromium creates InputOnly windows; XDamageCreate on them throws BadMatch, fatal under the default+ # handler. Swallow X errors and only keep damage handles that create cleanly.+ ERRH = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p)+ err: dict = {"n": 0, "code": None}++ def _on_x_error(_display: Any, event: Any) -> int:+ err["n"] += 1+ try:+ err["code"] = ctypes.cast(event, ctypes.POINTER(_XErrorEvent)).contents.error_code+ except Exception: # noqa: BLE001 - the error handler must never raise back into Xlib+ err["code"] = None+ return 0++ _eh = ERRH(_on_x_error)+ X.XSetErrorHandler(ctypes.cast(_eh, ctypes.c_void_p))+ X.XSelectInput(dpy, root, SubstructureNotifyMask)++ damaged: dict[int, int] = {}+ windows_pruned = 0+ windows_reconciled = 0+ attrs = _XWindowAttributes()++ def is_viewable(w: int) -> bool:+ # A window contributes visible pixels only when mapped with all ancestors mapped (IsViewable). A window+ # that vanished between enumeration and this query returns 0 here -> treated as gone (not viewable).+ if not X.XGetWindowAttributes(dpy, w, ctypes.byref(attrs)):+ return False+ return attrs.map_state == IsViewable++ def scan_and_arm() -> int:+ nonlocal windows_pruned+ acc: list[int] = []+ _enumerate_windows(X, dpy, root, 0, acc)+ added = 0+ # Drain BadDamage queued from the prior loop's XDamageSubtract on since-destroyed windows BEFORE+ # snapshotting err["n"]; otherwise the first per-window XSync charges those stale errors to this scan's+ # XDamageCreate, dropping a valid new window from ``damaged`` and leaking its handle every 1s rescan.+ X.XSync(dpy, 0)+ # Arm/keep only viewable windows. Pruning by viewability (not mere tree membership) drops BOTH destroyed+ # windows (absent from the tree) AND unmapped-but-live windows, so a remap re-arms fresh with no retained+ # hidden-window state; the root is always viewable and is never pruned.+ viewable = [w for w in acc if w == root or is_viewable(w)]+ windows_pruned += prune_unviewable_windows(XD, X, dpy, damaged, set(viewable), root, err, bad_damage_code)+ for w in viewable:+ if w not in damaged:+ before = err["n"]+ dh = XD.XDamageCreate(dpy, w, XDamageReportRawRectangles)+ X.XSync(dpy, 0)+ if err["n"] == before:+ damaged[w] = dh+ # Select SubstructureNotify so this window's parent reports its DestroyNotify; armed top-down,+ # every armed window's parent is armed too, so destroy events propagate up to the root handler.+ X.XSelectInput(dpy, w, SubstructureNotifyMask)+ added += 1+ return added++ fd = X.XConnectionNumber(dpy)++ visual = X.XDefaultVisual(dpy, scr)+ depth = X.XDefaultDepth(dpy, scr)+ shm_info = _XShmSegmentInfo()+ shm_image = XE.XShmCreateImage(dpy, visual, depth, ZPixmap, None, ctypes.byref(shm_info), gw, gh)+ if not shm_image:+ print("FATAL: XShmCreateImage failed", file=sys.stderr, flush=True)+ return EXIT_XSHMCREATEIMAGE+ img = shm_image.contents+ frame_bytes = img.bytes_per_line * img.height+ bpp = img.bits_per_pixel+ pix_fmt = "bgra" if bpp == 32 else ("bgr24" if bpp == 24 else "")+ if not pix_fmt:+ print(f"FATAL: unsupported bpp={bpp}", file=sys.stderr, flush=True)+ return EXIT_UNSUPPORTED_BPP+ if not _stride_consistent(img.bytes_per_line, gw, bpp):+ # Fail closed rather than stream padded rows as garbage (deployed depth-24 Xvfb is 32bpp / packed).+ print(+ f"FATAL: row-stride mismatch bytes_per_line={img.bytes_per_line} width={gw} bpp={bpp}",+ file=sys.stderr,+ flush=True,+ )+ return EXIT_STRIDE_MISMATCH+ addr, shm_code = _allocate_and_attach_shm(X, XE, LIBC, dpy, root, shm_image, shm_info, frame_bytes, err)+ if shm_code != 0:+ print(f"FATAL: {_SHM_FATAL_MESSAGE.get(shm_code, 'shm setup failed')}", file=sys.stderr, flush=True)+ return shm_code++ ff = build_ffmpeg_command(+ pix_fmt=pix_fmt,+ input_width=gw,+ input_height=gh,+ output_width=ow,+ output_height=oh,+ max_fps=args.max_fps,+ crf=args.crf,+ keyframe_seconds=args.keyframe_sec,+ output_path=args.out,+ )+ # Kept open for the FFmpeg child's whole lifetime (a context manager would close it before finalize).+ ff_log = open(str(Path(args.out).with_suffix(".ffmpeg.log")), "wb") # noqa: SIM115+ ffp = subprocess.Popen(ff, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=ff_log)+ assert ffp.stdin is not None # stdin=PIPE always yields a writable pipe+ wfd = ffp.stdin.fileno()++ # Now that X/SHM/FFmpeg are up, replace the default SIGINT/SIGTERM disposition with the graceful-stop flag+ # (a terminal frame is injected on stop). Kept AFTER init so a death/stop during a hung init still fast-kills.+ stop = {"v": False}+ signal.signal(signal.SIGINT, lambda *a: stop.__setitem__("v", True))+ signal.signal(signal.SIGTERM, lambda *a: stop.__setitem__("v", True))++ frames = {"n": 0, "bytes": 0, "short_writes": 0, "grab_fail": 0}+ first_frame_epoch: float | None = None++ def capture_and_write() -> None:+ nonlocal first_frame_epoch+ if not XE.XShmGetImage(dpy, root, shm_image, 0, 0, AllPlanes):+ frames["grab_fail"] += 1+ return+ view = (ctypes.c_char * frame_bytes).from_address(addr)+ w = _write_all(wfd, view)+ if w < 0:+ stop["v"] = True+ return+ if w != frame_bytes:+ frames["short_writes"] += 1+ if frames["n"] == 0:+ first_frame_epoch = time.time()+ if args.stats:+ Path(args.stats).write_text(json.dumps({"first_frame_epoch": first_frame_epoch, "frames_written": 1}))+ frames["n"] += 1+ frames["bytes"] += w++ min_interval = 1.0 / args.max_fps if args.max_fps > 0 else 0.0+ last_capture = time.monotonic()+ last_rescan = time.monotonic()+ windows_armed = 0+ pending_dirty = False+ arm_consumed = False++ # Own the XDamage handles inside try/finally: the initial arm happens only after the FATAL-returning X/SHM/FFmpeg+ # setup above (which owned no handles, so its early returns cannot leak), and _destroy_all_handles then runs on+ # EVERY capture-phase exit — normal stop, SIGINT/SIGTERM, or an exception — within the handles' owning lifetime.+ try:+ scan_and_arm()+ windows_armed = len(damaged)+ # Positive readiness: X/XDamage/XShm are up, the initial XShmGetImage receipt passed, FFmpeg is spawned, and+ # the graceful-stop signal handlers are installed — every known pre-loop wedge point is cleared. Emit exactly+ # one READY line on stdout so the parent accepts the recorder only now; all diagnostics stay on stderr.+ print("READY", flush=True)++ while not stop["v"]:+ try:+ select.select([fd], [], [], 0.2) # blocking optimization + ~5fps floor; readability is not consulted+ except (InterruptedError, OSError):+ pass+ # Drain Xlib's queue after EVERY select return, not only when the socket was readable: the periodic+ # scan_and_arm() XSync below can pull socket bytes into Xlib's internal event queue, leaving a transient+ # damage event queued with no later socket readability. XPending sees both the socket and that queue.+ saw_damage = False+ while X.XPending(dpy) > 0:+ ev = _XEvent()+ X.XNextEvent(dpy, ctypes.byref(ev))+ if ev.type == damage_notify:+ saw_damage = True+ elif ev.type == DestroyNotify:+ # Reconcile a destroyed window immediately (before any rescan): drop its entry so a same-XID+ # reuse in the sub-rescan interval re-arms fresh instead of inheriting the auto-freed handle.+ gone = ctypes.cast(ctypes.byref(ev), ctypes.POINTER(_XSubstructureEvent)).contents.window+ if reconcile_destroyed_window(damaged, gone, root):+ windows_reconciled += 1+ if saw_damage:+ pending_dirty = True+ for _w, dh in list(damaged.items()):+ XD.XDamageSubtract(dpy, dh, 0, 0)+ now = time.monotonic()+ if now - last_rescan >= 1.0:+ if scan_and_arm():+ # A window that finished its initial paint before this rescan emits no later XDamage, so mark+ # dirty here or the next FPS-coalesced emit would never capture it (e.g. a popup/native dialog).+ pending_dirty = True+ windows_armed = len(damaged) # reflects newly armed windows and pruned/reconciled departures+ last_rescan = now+ if arm_transition(armed["v"], arm_consumed):+ # Flush EVERY pre-arm (about:blank / pre-nav) damage without capturing, then IMMEDIATELY grab the+ # current ready (navigated) page as frame zero. Do not wait for a later damage. A DestroyNotify+ # queued in this arm window must STILL be reconciled here (only the pre-arm DAMAGE is discarded), or+ # a window destroyed just before arm keeps a stale entry a same-XID reuse would then inherit.+ X.XSync(dpy, 0)+ while X.XPending(dpy) > 0:+ ev = _XEvent()+ X.XNextEvent(dpy, ctypes.byref(ev))+ if ev.type == DestroyNotify:+ gone = ctypes.cast(ctypes.byref(ev), ctypes.POINTER(_XSubstructureEvent)).contents.window+ if reconcile_destroyed_window(damaged, gone, root):+ windows_reconciled += 1+ for _w, dh in list(damaged.items()):+ XD.XDamageSubtract(dpy, dh, 0, 0)+ arm_consumed = True+ pending_dirty = False+ capture_and_write() # frame zero = the ready page+ last_capture = time.monotonic()+ continue+ if should_emit(arm_consumed, pending_dirty, (now - last_capture) >= min_interval):+ capture_and_write()+ last_capture = now+ pending_dirty = False++ if should_write_terminal(frames["n"]):+ capture_and_write() # terminal frame -> finalized duration reflects real elapsed time+ try:+ if ffp.stdin is not None:+ ffp.stdin.close()+ except OSError:+ pass+ try:+ ffp.wait(timeout=90)+ except subprocess.TimeoutExpired:+ ffp.kill()+ finally:+ # Exactly-once release of every owned XDamage handle (root + survivors); reached on normal stop,+ # cancellation, or an exception, so no owned handle outlives its capture phase.+ _destroy_all_handles(XD, X, dpy, damaged)++ try:+ XE.XShmDetach(dpy, ctypes.byref(shm_info))+ LIBC.shmdt(addr)+ except Exception: # noqa: BLE001,S110 - best-effort shm detach at process exit; segment is IPC_RMID-marked+ pass++ # Zero-frame truth: remove the header-only output so the parent publishes no bogus recording. UNARMED+0 is a+ # clean pre-page teardown (rc 0); a consumed OR delivered-but-not-yet-consumed arm (SIGUSR1 latched in the+ # stop's loop interval) with zero frames is a capture failure (nonzero + unlink), not a graceful teardown.+ arm_effective = armed["v"] or arm_consumed+ rc = _finalize_return_code(frames["n"], ffp.returncode, arm_effective)+ if should_unlink_output(frames["n"]):+ try:+ os.unlink(args.out)+ except OSError:+ pass++ stats = {+ "frames_written": frames["n"],+ "bytes_written": frames["bytes"],+ "short_writes": frames["short_writes"],+ "grab_fail": frames["grab_fail"],+ "armed": arm_effective,+ "expected_frame_bytes": frame_bytes,+ "bytes_consistent": frames["short_writes"] == 0 and frames["bytes"] == frames["n"] * frame_bytes,+ "pix_fmt": pix_fmt,+ "bpp": bpp,+ "windows_armed": windows_armed,+ "windows_pruned": windows_pruned,+ "windows_reconciled": windows_reconciled,+ "damage_err_count": err["n"],+ "first_frame_epoch": first_frame_epoch,+ "last_frame_epoch": time.time(),+ "ffmpeg_returncode": ffp.returncode,+ "codec": "h264",+ "container": "mp4",+ }+ if args.stats:+ Path(args.stats).write_text(json.dumps(stats))+ return rc+++def _parse_args(argv: list[str] | None = None) -> argparse.Namespace:+ ap = argparse.ArgumentParser(prog="skyvern.webeye.display_capture_bridge")+ ap.add_argument("--display", default=os.environ.get("DISPLAY", ":99"))+ ap.add_argument("--size", default="1920x1080")+ ap.add_argument("--out-size", default="1280x720")+ ap.add_argument("--max-fps", type=int, default=15)+ ap.add_argument("--crf", type=int, default=28)+ ap.add_argument("--keyframe-sec", type=int, default=5)+ ap.add_argument("--out", required=True)+ ap.add_argument("--stats", default="")+ return ap.parse_args(argv)+++def main(argv: list[str] | None = None) -> int:+ return _run(_parse_args(argv))+++if __name__ == "__main__":+ sys.exit(main())diff --git a/skyvern/webeye/display_recorder.py b/skyvern/webeye/display_recorder.pynew file mode 100644index 000000000..52f871615--- /dev/null+++ b/skyvern/webeye/display_recorder.py@@ -0,0 +1,692 @@+"""Exclusive whole-display recorder (default OFF, cohort-gated).++Replaces per-page Playwright recording (only when ``EXCLUSIVE_DISPLAY_RECORDING`` is on and an owner is present)+with ONE change-driven whole-display MP4 covering every tab, browser chrome, and native dialogs. Capture/encode+is delegated to a replaceable source (``build_capture_command``); the default is the ctypes XDamage+XShm →+single-pass libx264 fragmented-MP4 bridge (:mod:`skyvern.webeye.display_capture_bridge`). No second transcode —+the MP4 on disk is the upload artifact (``video_utils.prepare_recording_for_upload`` skips ``.mp4``). Lifecycle+(flock fencing, owner registry + reservation, PDEATHSIG trampoline, SIGINT→SIGTERM→kill escalation,+cancellation-safe reap, owner-exact unlink) is preserved from the reviewed prior seam.+"""++from __future__ import annotations++import asyncio+import hashlib+import os+import platform+import re+import signal+import sys+from contextlib import suppress+from dataclasses import dataclass+from datetime import datetime+from pathlib import Path++import structlog++from skyvern.config import settings+from skyvern.webeye import attach_only+from skyvern.webeye.browser_artifacts import BrowserArtifacts, VideoArtifact+from skyvern.webeye.display_capture_bridge import build_bridge_command++try:+ import fcntl+except ImportError:+ # fcntl is POSIX-only. This module is part of the OSS `skyvern/` import surface, which must import+ # cleanly on non-POSIX platforms; whole-display recording only ever runs on the Linux worker family,+ # so acquisition fails closed when fcntl is unavailable.+ fcntl = None # type: ignore[assignment]++LOG = structlog.get_logger()++SIGINT_TIMEOUT = 5.0+SIGTERM_TIMEOUT = 2.0+# Deadline for the bridge's positive `READY` stdout ACK (emitted only after full X/XShm/FFmpeg/signal setup).+# A miss is a startup REFUSAL (kill + fall back to Playwright), so it must cover trampoline re-exec + interpreter+# start + ctypes CDLL loads + X handshake + FFmpeg Popen on a loaded worker; the 5s is paid only when wedged.+STARTUP_ACK_TIMEOUT = 5.0++# The Xvfb screen / base window is 1920x1080; an absent/unparseable/larger-than-screen window falls back to+# capturing the full screen. Output is bounded by the configured recording profile, aspect-preserved.+CAPTURE_DISPLAY_SIZE = (1920, 1080)+CaptureSizes = tuple[tuple[int, int], tuple[int, int]]++# Private browser_args carrier for the RAW (uncapped) recording output bound the caller selected for+# this run — the BROWSER_RECORDING_RESOLUTION profile or an operator override, NOT the viewport-capped+# Playwright record_video_size. Set only on the local-new-launch path and consumed+removed by+# prepare_local_display_recording before browser launch, so it never reaches Playwright/Patchright+# launch kwargs, remote/CDP/vendor contexts, reconnect, retry, storage, or cleanup.+DISPLAY_RECORDING_OUTPUT_BOUND_KEY = "_display_recording_output_bound"++_REGISTRY: dict[tuple[str, str], DisplayRecorder] = {}++# Parent-death via an exec'd python trampoline, NOT a fork-time preexec_fn (prctl/ctypes in the multithreaded+# worker's fork child can inherit locks and hang before exec). The parent passes its pid (argv[1]); the+# trampoline exits unless the live parent still matches before AND after prctl, then execvp's the bridge (pid+# kept; PDEATHSIG survives non-setuid exec), which owns and finalizes its own ffmpeg child on SIGTERM/EOF.+_PDEATHSIG_TRAMPOLINE = (+ "import ctypes,os,signal,sys\n"+ "e=int(sys.argv[1])\n"+ "os.getppid()==e or os._exit(0)\n"+ "try:ctypes.CDLL(None,use_errno=True).prctl(1,signal.SIGTERM)\n" # 1 == PR_SET_PDEATHSIG+ "except Exception:pass\n"+ "signal.pthread_sigmask(signal.SIG_BLOCK,{signal.SIGUSR1})\n" # hold arm pending until bridge installs handler+ "os.getppid()==e or os._exit(0)\n"+ "os.execvp(sys.argv[2],sys.argv[2:])\n"+)+++@dataclass(frozen=True)+class RecordingProfile:+ """Narrow, validated whole-display recording controls. ``resolve`` clamps/falls back explicitly so an+ invalid operator value can never produce an out-of-range or odd-dimension encode."""++ output_width: int+ output_height: int+ max_fps: int+ crf: int+ keyframe_seconds: int++ @staticmethod+ def _even_clamp(value: int, lo: int, hi: int, default: int) -> int:+ if not isinstance(value, int) or value < lo or value > hi:+ value = default+ return value - (value % 2) # libx264/libvpx need even dimensions++ @classmethod+ def resolve(cls) -> RecordingProfile:+ max_w, max_h = CAPTURE_DISPLAY_SIZE++ def clamp(value: int, lo: int, hi: int, default: int) -> int:+ return value if isinstance(value, int) and lo <= value <= hi else default++ return cls(+ output_width=cls._even_clamp(settings.DISPLAY_RECORDING_OUTPUT_WIDTH, 2, max_w, 1280),+ output_height=cls._even_clamp(settings.DISPLAY_RECORDING_OUTPUT_HEIGHT, 2, max_h, 720),+ max_fps=clamp(settings.DISPLAY_RECORDING_MAX_FPS, 1, 60, 15),+ crf=clamp(settings.DISPLAY_RECORDING_CRF, 0, 51, 28),+ keyframe_seconds=clamp(settings.DISPLAY_RECORDING_KEYFRAME_SECONDS, 1, 30, 5),+ )+++def normalize_display(display: str) -> str:+ # Strip only the ".<screen>" that trails the ":<display>" segment; never split on a dot inside a+ # hostname (e.g. "host.example.com:0.0" -> "host.example.com:0", ":99.0" -> ":99").+ display = display.strip()+ head, sep, tail = display.rpartition(":")+ if not sep:+ return display+ return f"{head}:{tail.split('.', 1)[0]}"+++def resolve_owner_id(+ *,+ owner_id_override: str | None = None,+ workflow_run_id: str | None = None,+ task_id: str | None = None,+ script_id: str | None = None,+ browser_session_id: str | None = None,+) -> str | None:+ # owner_id_override is FALLBACK-ONLY (used only when no canonical owner is present, e.g. a standalone+ # reconnect), so a stray/hostile override can never displace an explicit canonical owner and weaken isolation.+ return workflow_run_id or task_id or script_id or browser_session_id or owner_id_override+++def configure_local_display_recording(+ browser_args: dict[str, object],+ *,+ owner_id_override: str | None = None,+ workflow_run_id: str | None = None,+ task_id: str | None = None,+ script_id: str | None = None,+ browser_session_id: str | None = None,+) -> bool:+ # Eligibility ONLY — the Playwright record_video_* pop is deferred to ``prepare_local_display_recording`` and+ # happens only after a live recorder is acquired, so a refusal leaves per-page recording intact.+ return bool(+ platform.system() == "Linux"+ and settings.EXCLUSIVE_DISPLAY_RECORDING+ # Display-ownership guard: the flock blocks a second recorder, not a second browser sharing the display.+ # Above concurrency 1 the whole-display capture could catch another run's browser, so fail closed and keep+ # per-page recording. Only concurrency == 1 (one browser per display) is ownership-safe.+ and settings.BROWSER_WORKER_MAX_CONCURRENT_ACTIVITIES == 1+ # VIDEO_PATH is required up front (acquire_display_recorder needs it too): a falsy path must NOT enable+ # whole-display recording, or the run would end up with neither per-page nor whole-display recording.+ and settings.VIDEO_PATH+ and os.environ.get("DISPLAY")+ and not attach_only.is_enforcing()+ and resolve_owner_id(+ owner_id_override=owner_id_override,+ workflow_run_id=workflow_run_id,+ task_id=task_id,+ script_id=script_id,+ browser_session_id=browser_session_id,+ )+ )+++async def prepare_local_display_recording(+ browser_args: dict[str, object],+ browser_artifacts: BrowserArtifacts,+ *,+ owner_id_override: str | None = None,+ workflow_run_id: str | None = None,+ task_id: str | None = None,+ script_id: str | None = None,+ browser_session_id: str | None = None,+) -> None:+ """Pre-context acquire seam shared by every local browser creator: decide eligibility, acquire the+ whole-display recorder BEFORE the browser context is created, and pop the Playwright record_video_* args+ ONLY if a live recorder was obtained. Every outcome yields exactly one recording:+ * ineligible / no owner-display-path -> args intact, Playwright records;+ * refusal or startup failure (recorder is None) -> args intact, Playwright records (fallback preserved);+ * success / reuse / adoption (recorder present) -> args popped, the display recorder owns the single MP4.+ The acquisition is stashed on ``browser_artifacts`` so the shared consumer can register a NEWLY-started+ recorder for outer cleanup (reused/adopted recorders are left live). Never raises into browser creation.+ """+ eligible = configure_local_display_recording(+ browser_args,+ owner_id_override=owner_id_override,+ workflow_run_id=workflow_run_id,+ task_id=task_id,+ script_id=script_id,+ browser_session_id=browser_session_id,+ )+ # Consume the raw-output-bound carrier on EVERY path (before the eligibility short-circuit) so it+ # never survives into the browser launch kwargs — eligible, ineligible, and refusal alike.+ output_bound = browser_args.pop(DISPLAY_RECORDING_OUTPUT_BOUND_KEY, None)+ browser_artifacts.local_display_recording_eligible = eligible+ if not eligible:+ return+ capture_sizes = resolve_display_capture_sizes(browser_args, output_bound=output_bound)+ browser_artifacts._display_capture_sizes = capture_sizes+ owner_id = resolve_owner_id(+ owner_id_override=owner_id_override,+ workflow_run_id=workflow_run_id,+ task_id=task_id,+ script_id=script_id,+ browser_session_id=browser_session_id,+ )+ display = os.environ.get("DISPLAY")+ if not (owner_id and display and settings.VIDEO_PATH):+ return+ video_dir = Path(settings.VIDEO_PATH) / datetime.utcnow().strftime("%Y-%m-%d")+ acquisition = await acquire_display_recorder(display, owner_id, video_dir, owner_id_override, capture_sizes)+ if acquisition.recorder is not None and acquisition.video_artifact is not None:+ # A live recorder exists (started, reused, or adopted): suppress Playwright per-page recording now.+ browser_args.pop("record_video_dir", None)+ browser_args.pop("record_video_size", None)+ browser_artifacts._display_recorder = acquisition.recorder+ browser_artifacts.video_artifacts = [acquisition.video_artifact]+ browser_artifacts._display_recorder_acquisition = acquisition+++def carry_display_recording(src: BrowserArtifacts, dst: BrowserArtifacts) -> None:+ """Carry a prepared recording onto a rebuilt BrowserArtifacts (profile-corruption fallback rebuilds the+ artifacts after the recorder already started) so the run keeps its single recorder instead of orphaning it.+ The Playwright record_video_* args were already popped from the shared browser_args, so the fallback+ re-launch will not double-record."""+ dst.local_display_recording_eligible = src.local_display_recording_eligible+ dst._display_capture_sizes = src._display_capture_sizes+ dst._display_recorder = src._display_recorder+ dst.video_artifacts = src.video_artifacts+ dst._display_recorder_acquisition = src._display_recorder_acquisition+++async def release_started_display_recording(browser_artifacts: BrowserArtifacts) -> None:+ """Release ONLY a freshly-started recorder stashed by ``prepare_local_display_recording`` — call from a+ creator's launch-failure path before re-raising so a newly-started recorder never leaks. A reused/adopted+ recorder (started=False) belongs to a live owner and is deliberately left running."""+ acquisition = browser_artifacts._display_recorder_acquisition+ if isinstance(acquisition, DisplayRecorderAcquisition) and acquisition.started and acquisition.recorder is not None:+ with suppress(Exception):+ await release_display_recorder(acquisition.recorder)+ browser_artifacts._display_recorder_acquisition = None+ browser_artifacts._display_recorder = None+ browser_artifacts.video_artifacts = []+++def _safe_owner_id(owner_id: str) -> str:+ sanitized = re.sub(r"[^A-Za-z0-9._-]", "_", owner_id).strip("._")+ prefix = (sanitized or "run")[:48]+ digest = hashlib.sha256(owner_id.encode("utf-8")).hexdigest()+ return f"{prefix}-{digest}"+++def _parse_window_size(browser_args: dict[str, object]) -> tuple[int, int] | None:+ args = browser_args.get("args")+ if not isinstance(args, list):+ return None+ resolved: str | None = None+ for arg in args:+ text = str(arg)+ if text.startswith("--window-size="):+ resolved = text # the launch keeps one entry; take the last so a rewrite always wins+ if resolved is None:+ return None+ try:+ width_str, height_str = resolved.split("=", 1)[1].split(",", 1)+ return int(width_str), int(height_str)+ except (ValueError, IndexError):+ return None+++def _even_floor(value: float) -> int:+ floored = int(value)+ return floored - (floored % 2)+++def _bounded_even_output(width: int, height: int, bound: tuple[int, int]) -> tuple[int, int]:+ # Fit within `bound`, aspect-preserved and never upscaled; binding dimension chosen by integer+ # cross-multiplication (lands exactly on the bound, no float drift), the other floored to even.+ max_w, max_h = bound+ if width * max_h >= height * max_w:+ out_w: float = min(width, max_w)+ out_h: float = out_w * height / width+ else:+ out_h = min(height, max_h)+ out_w = out_h * width / height+ return _even_floor(out_w), _even_floor(out_h)+++def _resolve_output_bound(profile: RecordingProfile, output_bound: object) -> tuple[int, int]:+ # The raw recording selection (BROWSER_RECORDING_RESOLUTION profile or operator override) is the+ # output bound ONLY when the whole pair is valid together: both width and height present, real+ # (non-bool) ints, and within the capture display. Any missing/partial/mixed-validity carrier+ # falls back to the COMPLETE static profile pair — never a carrier axis mixed with a profile axis,+ # which would distort the aspect. Each accepted axis is floored even for libx264/libvpx.+ fallback = (profile.output_width, profile.output_height)+ if not isinstance(output_bound, dict):+ return fallback+ width, height = output_bound.get("width"), output_bound.get("height")+ max_w, max_h = CAPTURE_DISPLAY_SIZE+ if (+ isinstance(width, int)+ and not isinstance(width, bool)+ and isinstance(height, int)+ and not isinstance(height, bool)+ and 2 <= width <= max_w+ and 2 <= height <= max_h+ ):+ return width - (width % 2), height - (height % 2)+ return fallback+++def resolve_display_capture_sizes(+ browser_args: dict[str, object],+ profile: RecordingProfile | None = None,+ output_bound: object = None,+) -> CaptureSizes:+ """Derive immutable ((input_w, input_h), (output_w, output_h)) from the FINAL validated ``--window-size``+ and the recording output bound. ``output_bound`` (the raw, uncapped recording selection carried from the+ browser-args seam) takes precedence over the static profile output; it is NEVER the viewport-capped+ Playwright ``record_video_size``, which would fit the window twice. Input is the browser window at the+ display origin; output is that rectangle fit within the bound, even-dimensioned, no pad/crop/distortion. A+ missing/unparseable/larger-than-display window falls back to the fixed full-screen capture at the bound.+ When input equals output the capture bridge skips scaling entirely (the X4 no-scale profile)."""+ profile = profile or RecordingProfile.resolve()+ bound = _resolve_output_bound(profile, output_bound)+ window = _parse_window_size(browser_args)+ if window is None:+ return CAPTURE_DISPLAY_SIZE, _bounded_even_output(*CAPTURE_DISPLAY_SIZE, bound)+ width, height = window+ if not (0 < width <= CAPTURE_DISPLAY_SIZE[0] and 0 < height <= CAPTURE_DISPLAY_SIZE[1]):+ return CAPTURE_DISPLAY_SIZE, _bounded_even_output(*CAPTURE_DISPLAY_SIZE, bound)+ return (width, height), _bounded_even_output(width, height, bound)+++def build_capture_command(+ display: str,+ owner_id: str,+ video_dir: Path,+ capture_sizes: CaptureSizes | None = None,+ profile: RecordingProfile | None = None,+ stats_path: str | None = None,+) -> tuple[list[str], Path]:+ """Narrow capture-source seam: returns (argv, output_path) for the default XDamage+XShm bridge.+ Swapping in a native-C source means replacing this one function."""+ profile = profile or RecordingProfile.resolve()+ output_path = video_dir / f"{_safe_owner_id(owner_id)}.mp4"+ (input_w, input_h), (output_w, output_h) = capture_sizes or (+ CAPTURE_DISPLAY_SIZE,+ _bounded_even_output(*CAPTURE_DISPLAY_SIZE, (profile.output_width, profile.output_height)),+ )+ command = build_bridge_command(+ display=normalize_display(display),+ output_path=str(output_path),+ input_width=input_w,+ input_height=input_h,+ output_width=output_w,+ output_height=output_h,+ max_fps=profile.max_fps,+ crf=profile.crf,+ keyframe_seconds=profile.keyframe_seconds,+ stats_path=stats_path,+ )+ return command, output_path+++def _kill_process_group(process: asyncio.subprocess.Process) -> None:+ """SIGKILL the bridge AND its ffmpeg child together. The bridge is spawned in its own session+ (``start_new_session``), so its pid is the process-group leader; killing the group prevents an orphaned+ ffmpeg when a wedged bridge cannot finalize on SIGINT/SIGTERM. Falls back to a single-process kill."""+ pid = process.pid+ if pid is None:+ return+ killed_group = False+ with suppress(ProcessLookupError, PermissionError, OSError):+ pgid = os.getpgid(pid)+ # Only kill the group when the bridge is its OWN group leader (start_new_session); never risk+ # SIGKILLing the worker's own process group.+ if pgid == pid:+ os.killpg(pgid, signal.SIGKILL)+ killed_group = True+ if not killed_group:+ with suppress(ProcessLookupError):+ process.kill()+++async def _reap_surviving_cancellation(process: asyncio.subprocess.Process) -> bool:+ """Await the child's reaping to completion even under repeated cancellation. Reports whether any cancel+ arrived so the caller re-raises exactly once — after it has released its resources."""+ waiter = asyncio.ensure_future(process.wait())+ cancelled = False+ while True:+ try:+ await asyncio.shield(waiter)+ return cancelled+ except asyncio.CancelledError:+ cancelled = True+++@dataclass(frozen=True)+class DisplayRecorderAcquisition:+ recorder: DisplayRecorder | None+ video_artifact: VideoArtifact | None+ started: bool+++class DisplayRecorder:+ def __init__(+ self,+ *,+ display: str,+ owner_id: str,+ process: asyncio.subprocess.Process,+ lock_fd: int,+ video_artifact: VideoArtifact,+ ) -> None:+ self.display = normalize_display(display)+ self.owner_id = owner_id+ self.process = process+ self.lock_fd = lock_fd+ self.video_artifact = video_artifact+ self._stop_lock = asyncio.Lock()+ self._stop_result: bool | None = None+ # Set when the recorder is stopped for upload but the display must stay fenced to this owner until a+ # later, separate teardown (a deferred live-view browser close).+ self._reserved = False+ # Latched once the arm signal is successfully delivered to the bridge (parent-side exactly-once).+ self._armed = False++ def arm_capture(self) -> bool:+ """Signal the bridge to begin its timeline at the current ready page (SIGUSR1). Parent-side+ EXACTLY-ONCE: the first successful send latches, later calls are idempotent no-ops; a FAILED send does+ NOT latch, so a later call may retry. Returns True iff the bridge is/was armed, False if the signal+ could not be delivered (bridge already exited or send raised). Never touches the registry, display+ flock, reservation, or ownership."""+ if self._armed:+ return True+ if self.process.returncode is not None:+ return False+ try:+ self.process.send_signal(signal.SIGUSR1)+ except Exception: # noqa: BLE001 - a failed send must stay retryable, so do not latch+ return False+ self._armed = True+ return True++ @property+ def is_stopped(self) -> bool:+ """True once ``stop()`` has run to completion, so the MP4 on disk is finalized and uploadable."""+ return self._stop_result is not None++ async def finalize_keeping_reservation(self) -> bool:+ """Stop the capture so the MP4 is complete and uploadable, but keep the display lock and registry+ reservation (deferred live-view close: the run's browser is still mapped on the display)."""+ self._reserved = True+ return await self.stop(release_lock=False)++ @staticmethod+ def lock_path_for_display(display: str) -> str:+ safe_display = re.sub(r"[^A-Za-z0-9_-]", "_", normalize_display(display))+ return f"/tmp/skyvern-display-recording-{safe_display}.lock"++ def _release_lock(self) -> None:+ if self.lock_fd is not None and self.lock_fd >= 0:+ with suppress(OSError):+ os.close(self.lock_fd)+ self.lock_fd = -1++ async def stop(self, release_lock: bool = True) -> bool:+ async with self._stop_lock:+ if self._stop_result is not None:+ return self._stop_result+ graceful = self.process.returncode == 0+ pending_cancel = False+ try:+ graceful = await self._escalate_shutdown(graceful)+ except asyncio.CancelledError:+ pending_cancel = True+ finally:+ # Reap the child and (unless reserved) release the lock even under repeated cancellation, so a+ # cancelled stop never leaks the bridge/ffmpeg or blocks the next run.+ if self.process.returncode is None:+ _kill_process_group(self.process)+ if await _reap_surviving_cancellation(self.process):+ pending_cancel = True+ graceful = False+ if release_lock:+ self._release_lock()+ self._stop_result = graceful+ if pending_cancel:+ raise asyncio.CancelledError()+ return graceful++ async def _escalate_shutdown(self, graceful: bool) -> bool:+ if self.process.returncode is not None:+ return graceful+ # SIGINT: the bridge injects a terminal frame, closes ffmpeg stdin, and lets ffmpeg finalize the MP4.+ self.process.send_signal(signal.SIGINT)+ try:+ await asyncio.wait_for(self.process.wait(), timeout=SIGINT_TIMEOUT)+ # Report the truth: a nonzero bridge exit (failed finalize) is NOT graceful, so callers relying+ # on stop() -> False can surface it rather than treating any timely exit as success.+ return self.process.returncode == 0+ except TimeoutError:+ pass+ self.process.terminate()+ try:+ await asyncio.wait_for(self.process.wait(), timeout=SIGTERM_TIMEOUT)+ except TimeoutError:+ return False+ return self.process.returncode == 0+++def _prune_dead_entries(normalized_display: str, owner_id: str) -> None:+ # Drop exited recorders of a DIFFERENT owner on this display so a previous run's dead recorder never+ # fail-closes the next one. Same-owner is handled by reuse; a live owner is left entirely alone.+ dead_keys = [+ key+ for key, rec in _REGISTRY.items()+ if key[0] == normalized_display+ and key[1] != owner_id+ and rec.process.returncode is not None+ and not rec._reserved+ ]+ for key in dead_keys:+ dead = _REGISTRY.pop(key)+ dead._release_lock()+++async def acquire_display_recorder(+ display: str,+ owner_id: str,+ video_dir: Path,+ owner_id_override: str | None = None,+ capture_sizes: CaptureSizes | None = None,+ profile: RecordingProfile | None = None,+) -> DisplayRecorderAcquisition:+ if fcntl is None:+ LOG.warning("Whole-display recording unavailable: fcntl not present on this platform", display=display)+ return DisplayRecorderAcquisition(None, None, False)++ normalized_display = normalize_display(display)+ key = (normalized_display, owner_id)++ existing = _REGISTRY.get(key)+ if existing is not None:+ # Same (display, owner): reuse the exact recorder + VideoArtifact (never restart a dead one — a fresh+ # capture would overwrite the partial MP4 and mint a second RECORDING row); the partial uploads as-is.+ return DisplayRecorderAcquisition(existing, existing.video_artifact, False)++ _prune_dead_entries(normalized_display, owner_id)++ conflicting = next((rec for reg_key, rec in _REGISTRY.items() if reg_key[0] == normalized_display), None)+ if conflicting is not None:+ if owner_id_override is not None and conflicting.owner_id == owner_id_override:+ # A shared-state reconnect rebuilds under the aliased child id but carries its own live recorder's+ # owner as the override; re-adopt that exact recorder + artifact (same object, no restart).+ return DisplayRecorderAcquisition(conflicting, conflicting.video_artifact, False)+ LOG.error(+ "Whole-display recording refused because the display has a live owner",+ display=normalized_display,+ owner_id=owner_id,+ holder_owner_id=conflicting.owner_id,+ )+ return DisplayRecorderAcquisition(None, None, False)++ # Any failure below must fail closed WITHOUT leaking the lock fd and WITHOUT propagating into the+ # browser-creation path — a run whose recorder cannot start simply continues unrecorded.+ lock_fd: int | None = None+ process: asyncio.subprocess.Process | None = None+ try:+ lock_fd = os.open(DisplayRecorder.lock_path_for_display(normalized_display), os.O_CREAT | os.O_RDWR, 0o600)+ try:+ fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)+ except BlockingIOError:+ os.close(lock_fd)+ LOG.error("Whole-display recording refused because the display lock is held", display=normalized_display)+ return DisplayRecorderAcquisition(None, None, False)++ video_dir.mkdir(parents=True, exist_ok=True)+ stats_path = str(video_dir / f"{_safe_owner_id(owner_id)}.stats.json")+ command, output_path = build_capture_command(+ normalized_display, owner_id, video_dir, capture_sizes, profile, stats_path=stats_path+ )+ # The bridge emits a directly-usable fragmented MP4 (no second transcode); the existing upload path+ # consumes .mp4 without recompression.+ prefix = [sys.executable, "-c", _PDEATHSIG_TRAMPOLINE, str(os.getpid())] if os.name == "posix" else []+ process = await asyncio.create_subprocess_exec(+ *prefix,+ *command,+ stdin=asyncio.subprocess.DEVNULL,+ stdout=asyncio.subprocess.PIPE,+ stderr=asyncio.subprocess.DEVNULL,+ # Own process group so a force-kill reaps the bridge AND its ffmpeg child together (no orphan);+ # PDEATHSIG still binds the bridge to worker death independently of the group.+ start_new_session=(os.name == "posix"),+ )+ # Positive readiness: accept the recorder ONLY on the bridge's single exact `READY\n` stdout line from a+ # still-live child. EOF / wrong line / early exit / a timeout (wedged pre-loop) all fail closed — the+ # timeout raises and the mismatch raises, both funneling through the BaseException path below (kill group,+ # cancellation-safe reap, close lock, no recorder) so Playwright's own record_video_* args survive.+ assert process.stdout is not None # stdout=PIPE always yields a readable stream+ ack = await asyncio.wait_for(process.stdout.readline(), timeout=STARTUP_ACK_TIMEOUT)+ if ack != b"READY\n" or process.returncode is not None:+ raise RuntimeError(f"whole-display recorder did not signal readiness (ack={ack!r})")+ except BaseException as exc:+ cancelled_during_reap = False+ if process is not None and process.returncode is None:+ _kill_process_group(process)+ cancelled_during_reap = await _reap_surviving_cancellation(process)+ if lock_fd is not None:+ with suppress(OSError):+ os.close(lock_fd)+ if not isinstance(exc, Exception):+ raise+ if cancelled_during_reap:+ raise asyncio.CancelledError()+ LOG.warning("Failed to start whole-display recorder", display=normalized_display, exc_info=True)+ return DisplayRecorderAcquisition(None, None, False)++ # Advertise the container up front: the bridge always writes a fragmented MP4, so the in-progress+ # artifact ArtifactManager creates on step 0 must be `.mp4`, not the default `.webm`, before any bytes+ # exist on disk (SKY-15466).+ video_artifact = VideoArtifact(video_path=str(output_path), video_file_extension="mp4")+ recorder = DisplayRecorder(+ display=normalized_display,+ owner_id=owner_id,+ process=process,+ lock_fd=lock_fd,+ video_artifact=video_artifact,+ )+ _REGISTRY[key] = recorder+ return DisplayRecorderAcquisition(recorder, video_artifact, True)+++async def release_display_recorder(recorder: DisplayRecorder | None) -> bool:+ if recorder is None:+ return True+ # One stop task per release, awaited through repeated caller cancellation: the reservation/lock/registry+ # entry must never be freed until the serialized stop is terminal.+ stop_task = asyncio.ensure_future(recorder.stop())+ cancelled = False+ try:+ while True:+ try:+ return await asyncio.shield(stop_task)+ except asyncio.CancelledError:+ cancelled = True+ if stop_task.done():+ return stop_task.result()+ finally:+ if recorder.is_stopped:+ recorder._reserved = False+ recorder._release_lock()+ key = (recorder.display, recorder.owner_id)+ # Only drop OUR entry: a delayed release of a superseded recorder must never evict a replacement+ # that a later same-owner acquisition registered under the same key.+ if _REGISTRY.get(key) is recorder:+ del _REGISTRY[key]+ if cancelled:+ raise asyncio.CancelledError()+++async def stop_display_recorders_for_owner(owner_id: str) -> int:+ """Stop and de-register every live whole-display recorder this owner still holds. Call BEFORE unlinking+ so a run whose teardown never reached ``release_display_recorder`` (Temporal cancel / mid-run crash) is+ reaped inside ``stop()`` instead of orphaned into the next activity."""+ released = 0+ cancelled = False+ for key, recorder in list(_REGISTRY.items()):+ if key[1] != owner_id:+ continue+ if recorder._reserved:+ # Reserved (deferred live-view close): reaping here would free a display still mapped to this+ # owner's browser. Only browser teardown or process death releases the reservation.+ continue+ try:+ await release_display_recorder(recorder)+ except asyncio.CancelledError:+ # release_display_recorder finishes the shielded stop (lock/registry freed) THEN re-raises the+ # caller's cancellation. Record it, keep sweeping the remaining owners, and re-raise after the sweep+ # so the cancellation is never discarded (finish-then-re-raise, as in stop()/_reap/_reclaim).+ cancelled = True+ except Exception:+ LOG.warning("Failed to stop whole-display recorder during owner cleanup", owner_id=owner_id, exc_info=True)+ released += 1+ if cancelled:+ raise asyncio.CancelledError()+ return releaseddiff --git a/skyvern/webeye/real_browser_manager.py b/skyvern/webeye/real_browser_manager.pyindex 41a246ba7..c8e512ea5 100644--- a/skyvern/webeye/real_browser_manager.py+++ b/skyvern/webeye/real_browser_manager.py@@ -46,6 +46,10 @@ from skyvern.webeye.cdp_frame_publisher import (stream_key_for_task,stream_key_for_workflow_run,)+from skyvern.webeye.display_recorder import (+ DisplayRecorder,+ stop_display_recorders_for_owner,+)from skyvern.webeye.persistent_sessions_manager import PBS_TASK_RUNNABLE_TYPEfrom skyvern.webeye.real_browser_state import RealBrowserStatefrom skyvern.webeye.session_cookies import persist_session_cookies@@ -1177,12 +1181,10 @@ class RealBrowserManager(BrowserManager):return Nonedef set_video_artifact_for_task(self, task: Task, artifacts: list[VideoArtifact]) -> None:- if task.workflow_run_id and task.workflow_run_id in self.pages:- self.pages[task.workflow_run_id].browser_artifacts.video_artifacts = artifacts- return- if task.task_id in self.pages:- self.pages[task.task_id].browser_artifacts.video_artifacts = artifacts- return+ for run_key in (task.workflow_run_id, task.task_id):+ if run_key and run_key in self.pages:+ self.pages[run_key].browser_artifacts.video_artifacts = artifacts+ returnraise MissingBrowserState(task_id=task.task_id,@@ -1212,24 +1214,31 @@ class RealBrowserManager(BrowserManager):)return []- for i, video_artifact in enumerate(browser_state.browser_artifacts.video_artifacts):+ display_recorder = browser_state.browser_artifacts._display_recorder+ display_video_artifact = (+ display_recorder.video_artifact if isinstance(display_recorder, DisplayRecorder) else None+ )+ # The whole-display recording is the legacy index-0 recording: per-step syncs read its current+ # (growing) bytes and terminal finalize remuxes the SAME artifact/id, exactly like a Playwright+ # per-page recording. It additionally finalizes once its recorder has stopped even when a deferred+ # stream left finalize=False, so the deferred-close upload is finalized rather than a partial prefix.+ display_recorder_stopped = isinstance(display_recorder, DisplayRecorder) and display_recorder.is_stopped++ for video_artifact in browser_state.browser_artifacts.video_artifacts:+ finalize_this = finalize or (video_artifact is display_video_artifact and display_recorder_stopped)path = video_artifact.video_pathif path and os.path.exists(path=path):is_webm = path.lower().endswith(".webm")- if finalize and is_webm:+ if finalize_this and is_webm:async with prepare_recording_for_upload(path) as prepared:with open(prepared.path, "rb") as f:- browser_state.browser_artifacts.video_artifacts[i].video_data = f.read()- browser_state.browser_artifacts.video_artifacts[- i- ].video_file_extension = prepared.file_extension+ video_artifact.video_data = f.read()+ video_artifact.video_file_extension = prepared.file_extensionelse:# Non-WebM sources are already container-valid; per-step WebM snapshots are still incomplete.with open(path, "rb") as f:- browser_state.browser_artifacts.video_artifacts[i].video_data = f.read()- browser_state.browser_artifacts.video_artifacts[i].video_file_extension = (- os.path.splitext(path)[1].lstrip(".").lower() or "webm"- )+ video_artifact.video_data = f.read()+ video_artifact.video_file_extension = os.path.splitext(path)[1].lstrip(".").lower() or "webm"else:LOG.debug("Video path not found",@@ -1253,17 +1262,32 @@ class RealBrowserManager(BrowserManager):size snapshot of each ordinary growing WebM recording) for the fast streaming path, or ``None`` tofall back to the byte-based path whenever an artifact is non-WebM, not yet registered, or missing ondisk.++ The single admitted non-WebM source is the whole-display recorder's own fragmented-MP4 artifact+ (SKY-15466): a fragmented MP4 grows append-only, so streaming ``[0, len)`` is a valid decodable+ prefix exactly like a growing WebM. Eligibility is gated on the explicit producer signal — the+ artifact is the live ``DisplayRecorder``'s owned ``video_artifact`` — never on the ``.mp4`` extension+ alone, so unrelated ``.mp4`` sources and the stopped-recorder finalization case keep the byte path."""video_artifacts = browser_state.browser_artifacts.video_artifactsif len(video_artifacts) == 0:return []+ display_recorder = browser_state.browser_artifacts._display_recorder+ owned_mp4_artifact = (+ display_recorder.video_artifact+ if isinstance(display_recorder, DisplayRecorder) and not display_recorder.is_stopped+ else None+ )+snapshots: list[RecordingPrefixSnapshot] = []for video_artifact in video_artifacts:path = video_artifact.video_pathif not video_artifact.video_artifact_id or not path or not os.path.exists(path=path):return None- if not path.lower().endswith(".webm"):+ is_webm = path.lower().endswith(".webm")+ is_owned_growing_mp4 = video_artifact is owned_mp4_artifact and path.lower().endswith(".mp4")+ if not is_webm and not is_owned_growing_mp4:return Nonesnapshots.append(RecordingPrefixSnapshot(@@ -1387,6 +1411,15 @@ class RealBrowserManager(BrowserManager):else:LOG.warning("Organization ID not specified, cannot release browser session", task_id=task_id)+ # Whole-display recorder orphan sweep: reap a recorder whose per-browser release was bypassed by a+ # mid-run cancel/crash. Gated on the terminal-CLOSE path only — on keep-open+ # (close_browser_on_completion=False) the recorder is intentionally still live and must not be+ # stopped. The local MP4 is intentionally left under VIDEO_PATH (matching Playwright's file+ # lifetime): the terminal upload reads it AFTER this cleanup returns, so unlinking here would+ # truncate the recording.+ if close_browser_on_completion:+ await stop_display_recorders_for_owner(task_id)+return browser_state_to_closedef _shared_with_another_workflow_run(self, workflow_run_id: str, browser_state_to_close: BrowserState) -> bool:@@ -1452,6 +1485,9 @@ class RealBrowserManager(BrowserManager):# stream key. ``dict.pop(key, None)`` makes the second pop a no-op.streams_active = stream_ref_active(workflow_run_id)+ # Owners whose close was EFFECTIVE (not suppressed by cross-run sharing); only these are swept below.+ sweep_owner_ids: set[str] = set()+if browser_state_to_close:# If another workflow run still references this browser state (e.g. a# parent whose in-memory browser was shared via use_parent_browser_session),@@ -1524,6 +1560,9 @@ class RealBrowserManager(BrowserManager):# eventual ``close(True)`` fires the on-close callback that# stops it; ``close(False)`` is covered by the publisher's# own disconnect-driven self-termination.+ # Whole-display recording is per-run and decoupled from the deferred close, so finalize it now:+ # the run is terminal and the activity finally unlinks the file, so a later finalize=False loses it.+ await self._finalize_deferred_display_recording(browser_state_to_close, workflow_run_id, task_ids)else:# Detach the publisher's CDP session before the Playwright context# closes; otherwise the stale session can race the teardown.@@ -1534,6 +1573,8 @@ class RealBrowserManager(BrowserManager):)finalization_attempted = effective_closerecording_finalized = effective_close and bool(close_succeeded)+ if effective_close:+ sweep_owner_ids.add(workflow_run_id)if not streams_active:self.pages.pop(workflow_run_id, None)@@ -1541,13 +1582,14 @@ class RealBrowserManager(BrowserManager):task_browser_state = self.pages.pop(task_id, None)if task_browser_state is None or streams_active:continue- if task_browser_state is browser_state_to_close and finalization_attempted:- continue- # Same liveness-qualified ownership predicate as the run-level close: a distinct- # task-level state must not be held open by a ghost alias, and it must still yield to a- # genuinely live cross-run sharer.+ # Compute before the already-finalized continue so a task whose browser IS the run's browser still+ # contributes its (task-id-owned) recorder to the effective-close sweep set.shared = self._shared_with_another_workflow_run(task_id, task_browser_state)effective_close = close_browser_on_completion and not shared+ if effective_close:+ sweep_owner_ids.add(task_id)+ if task_browser_state is browser_state_to_close and finalization_attempted:+ continueif shared:LOG.info("Browser state is shared with another workflow run, skipping browser close",@@ -1573,6 +1615,21 @@ class RealBrowserManager(BrowserManager):)LOG.info("Workflow run is cleaned up", sampling=True)+ # Orphan sweep: reap recorders whose per-browser release was bypassed by a mid-run cancel/crash so a dead+ # bridge/ffmpeg + display lock never leaks into the next activity. Only ``sweep_owner_ids`` (effective-close+ # owners, accumulated above; empty on keep-open) are swept — a shared/deferred owner's browser is still+ # live, so freeing its display flock would let the next run capture it. The MP4 stays under VIDEO_PATH; the+ # terminal upload reads it after cleanup returns, so unlinking here would truncate it.+ sweep_cancelled = False+ if not streams_active:+ for sweep_owner_id in sweep_owner_ids:+ try:+ await stop_display_recorders_for_owner(sweep_owner_id)+ except asyncio.CancelledError:+ # Latch a mid-sweep cancel but finish sweeping every sibling owner (else its bridge/flock leaks);+ # session release + stream teardown below still run once, then re-raise before return.+ sweep_cancelled = True+release_complete = Trueif browser_session_id and not streams_active:if organization_id:@@ -1592,11 +1649,40 @@ class RealBrowserManager(BrowserManager):if not streams_active and release_complete:complete_stream_teardown(workflow_run_id)+ if sweep_cancelled:+ raise asyncio.CancelledError()return BrowserCleanupResult(browser_state=browser_state_to_close,recording_finalized=recording_finalized,)+ async def _finalize_deferred_display_recording(+ self, browser_state: BrowserState, workflow_run_id: str, task_ids: list[str]+ ) -> None:+ """Finalize this run's whole-display recorder when its browser close is deferred for an active stream.++ Stops the recorder so the MP4 is complete/uploadable, but KEEPS the display reserved to this owner:+ the browser is still mapped on the shared display, so freeing it now would let a later different-owner+ run acquire the display and capture this tenant's window. The reservation is released only when the+ deferred browser is actually torn down (real_browser_state.close -> release_display_recorder) or at+ process death. Gated on the workflow-tree-owned task set (workflow_run_id or one of its task_ids — a+ task-created browser owns its recorder under the task id) so an inherited/shared browser never has a+ sibling run's recording stopped out from under it. Best-effort and never raising (cancellation still+ propagates, preserving cancellation ownership)."""+ recorder = browser_state.browser_artifacts._display_recorder+ if not isinstance(recorder, DisplayRecorder) or recorder.owner_id not in {workflow_run_id, *task_ids}:+ return+ try:+ await recorder.finalize_keeping_reservation()+ except asyncio.CancelledError:+ raise+ except Exception:+ LOG.warning(+ "Failed to finalize whole-display recording on deferred close",+ workflow_run_id=workflow_run_id,+ exc_info=True,+ )+async def get_or_create_for_script(self,script_id: str | None = None,@@ -1713,6 +1799,9 @@ class RealBrowserManager(BrowserManager):LOG.warning("Failed to drop engine owner during script cleanup", script_id=script_id, exc_info=True)async def _reclaim() -> BrowserState | None:+ # A session-backed script is released, not closed, so its browser stays mapped for reuse. Both the+ # close and the recorder sweep gate on this, or the sweep would free the display flock under it.+ effective_close = close_browser_on_completion and not browser_session_idbrowser_state_to_close = self.pages.pop(script_id, None)if browser_state_to_close:if browser_state_to_close.browser_context and browser_state_to_close.browser_artifacts.traces_dir:@@ -1723,8 +1812,6 @@ class RealBrowserManager(BrowserManager):except Exception:LOG.warning("Failed to stop tracing during script cleanup", script_id=script_id, exc_info=True)try:- # Persistent session survives cleanup for reuse: don't close its context/driver, only release.- effective_close = close_browser_on_completion and not browser_session_idawait browser_state_to_close.close(close_browser_on_completion=effective_close,release_driver=False if browser_session_id else None,@@ -1755,6 +1842,11 @@ class RealBrowserManager(BrowserManager):self._discard_session_lease(script_id, session_lease)elif browser_session_id:LOG.warning("Organization ID not specified, cannot release browser session", script_id=script_id)+ # Whole-display recorder orphan sweep (shielded so a cancel cannot skip it and leak). Gated on+ # EFFECTIVE close — a keep-open or session-backed script keeps its live recorder. The local MP4 is+ # left under VIDEO_PATH; the terminal upload reads it after this returns, so unlinking here truncates.+ if effective_close:+ await stop_display_recorders_for_owner(script_id)return browser_state_to_close# Shield the page/trace/close/release reclamation as one unit: a caller cancellation (shutdown ordiff --git a/skyvern/webeye/real_browser_state.py b/skyvern/webeye/real_browser_state.pyindex 53c618bc9..b858944ad 100644--- a/skyvern/webeye/real_browser_state.py+++ b/skyvern/webeye/real_browser_state.py@@ -38,6 +38,7 @@ from skyvern.webeye.browser_factory import BrowserCleanupFunc, BrowserContextFacfrom skyvern.webeye.browser_health import BrowserOperationfrom skyvern.webeye.browser_state import BLANK_PAGE_URLS, BrowserStatefrom skyvern.webeye.cdp_download_interceptor import disable_download_interceptor_for_context+from skyvern.webeye.display_recorder import DisplayRecorder, release_display_recorderfrom skyvern.webeye.driver_connection import close_driver_connection_on_transport_lossfrom skyvern.webeye.navigation import is_permanent_navigation_error, navigate_with_retryfrom skyvern.webeye.scraper import scraper@@ -274,6 +275,7 @@ class RealBrowserState(BrowserState):browser_address: str | None = None,browser_profile_id: str | None = None,download_binding: DownloadBinding | None = None,+ display_recording_owner_id: str | None = None,) -> None:if self.browser_context is None:LOG.info("creating browser context")@@ -305,6 +307,7 @@ class RealBrowserState(BrowserState):browser_profile_id=browser_profile_id,engine_selection=self.engine_selection,download_binding=effective_download_binding,+ display_recording_owner_id=display_recording_owner_id,)self.browser_context = browser_contextself.browser_artifacts = browser_artifacts@@ -341,7 +344,27 @@ class RealBrowserState(BrowserState):await self._close_all_other_pages(discard_orphaned_videos=True)if url and not _same_page_ignoring_fragment(page.url, url):- await self.navigate_to_url(page=page, url=url)+ try:+ await self.navigate_to_url(page=page, url=url)+ finally:+ # Arm ONLY around the optional initial navigate: a successful first navigation records the+ # target page; a permanent navigation failure still records the current diagnostic/error+ # page; the original navigation exception propagates unchanged (arm never masks it).+ self._arm_display_recorder()+ return++ # No initial navigation ran (page already existed, or no/same URL): arm now that a working page exists.+ self._arm_display_recorder()++ def _arm_display_recorder(self) -> None:+ """Begin the whole-display recorder's timeline once a working page exists (two-phase capture,+ SKY-15466). Exactly-once and ownership-neutral in the recorder; a failed arm is surfaced, never raised."""+ recorder = self.browser_artifacts._display_recorder if self.browser_artifacts else None+ if isinstance(recorder, DisplayRecorder) and not recorder.arm_capture():+ LOG.warning(+ "Whole-display recorder arm signal was not delivered; run may record no frames",+ display_recording_owner_id=recorder.owner_id,+ )async def _wait_for_settle(self) -> None:total_wait_ms = SETTLE_TIME_MS@@ -731,6 +754,11 @@ class RealBrowserState(BrowserState):prior_download_binding = (self.browser_artifacts.download_binding if self.browser_artifacts else DownloadBinding.RUN_DIR)+ # Preserve the active whole-display recorder's owner across the rebuild, derived from THIS state's own+ # recorder (never a caller id), so a standalone task/script reconnect re-acquires the SAME recorder+ # instead of a fresh Playwright recording. A different run cannot inherit it (id is this run's own).+ recorder = self.browser_artifacts._display_recorder if self.browser_artifacts else None+ display_recording_owner_id = recorder.owner_id if isinstance(recorder, DisplayRecorder) else Noneself.browser_context = Noneawait self.set_working_page(None)# Reconnect on the SAME engine this state was pinned to at creation; never silently switch@@ -751,6 +779,7 @@ class RealBrowserState(BrowserState):browser_address=browser_address,browser_profile_id=browser_profile_id,download_binding=prior_download_binding,+ display_recording_owner_id=display_recording_owner_id,)except Exception:# The caller abandons this state on failure, so stop the just-started driver too or it leaks.@@ -923,11 +952,28 @@ class RealBrowserState(BrowserState):BROWSER_CLOSE_TIMEOUT,"browser context teardown",)+ # The display recorder is stopped/finalized on its own, decoupled from context teardown:+ # ``recording_finalized`` reports only that the browser context closed, which is what gates+ # profile persistence. A recorder that exited non-zero mid-run (or needed a forced kill)+ # must not clear that flag and suppress an otherwise-clean run's profile write-back, and a+ # context-teardown failure must not stop us from finalizing the WebM for upload. The stop+ # runs even when teardown failed above, so the recording is always finalized best-effort.+ recorder = self.browser_artifacts._display_recorder if self.browser_artifacts else None+ if isinstance(recorder, DisplayRecorder):+ await self._run_bounded_detachable(+ self._stop_display_recorder(recorder),+ BROWSER_CLOSE_TIMEOUT,+ "whole-display recorder stop",+ )await self._run_browser_cleanup_bounded()await self._stop_driver_bounded(release_driver)return recording_finalized+ async def _stop_display_recorder(self, recorder: DisplayRecorder) -> None:+ if not await release_display_recorder(recorder):+ raise RuntimeError("Whole-display recorder required forced termination")+async def detach_remote_driver(self) -> None:"""Release this process's adopted-remote resources without closing the remote browser.diff --git a/skyvern/webeye/utils/page.py b/skyvern/webeye/utils/page.pyindex aa135b839..10c4b77df 100644--- a/skyvern/webeye/utils/page.py+++ b/skyvern/webeye/utils/page.py@@ -628,18 +628,19 @@ async def _wait_for_screenshot_load_state(timeout_ms: float,engine_selection: BrowserEngineSelection | None = None,) -> None:- # Best-effort readiness guard before capturing. 'domcontentloaded' fires far- # earlier than 'load'; pages with streaming/long-polling/SSE/websockets or a- # persistent spinner may never fire 'load', so a timeout here must be- # non-fatal — the capture has its own (separate) timeout budget.+ # Best-effort readiness guard before capturing. Waiting for 'load' (rather than the+ # earlier 'domcontentloaded') lets late subresources settle before capture; but pages+ # with streaming/long-polling/SSE/websockets or a persistent spinner may never fire+ # 'load', so a timeout here must stay non-fatal — the capture has its own (separate)+ # timeout budget.if timeout_ms <= 0:returntry:- await page.wait_for_load_state("domcontentloaded", timeout=timeout_ms)+ await page.wait_for_load_state("load", timeout=timeout_ms)except Exception as exc:if not _is_engine_error(exc, engine_selection):raise- LOG.warning("Page did not reach domcontentloaded before screenshot; capturing current state anyway")+ LOG.warning("Page did not reach load before screenshot; capturing current state anyway")def _is_screenshot_target_closed(diff --git a/tests/unit/forge/sdk/artifact/storage/test_s3_storage.py b/tests/unit/forge/sdk/artifact/storage/test_s3_storage.pyindex b67cd596f..54501c8bb 100644--- a/tests/unit/forge/sdk/artifact/storage/test_s3_storage.py+++ b/tests/unit/forge/sdk/artifact/storage/test_s3_storage.py@@ -803,6 +803,67 @@ class TestS3StorageContentType:assert obj_meta["ContentType"] == expected_content_type+def _recording_artifact_with_ext(s3_storage: S3Storage, ext: str) -> Artifact:+ """A RECORDING artifact whose URI ends in the given extension (the whole-display recorder registers .mp4)."""+ artifact_id_val = generate_artifact_id()+ step = create_fake_step(f"s_ct_{ext}")+ base = s3_storage.build_uri(+ organization_id=TEST_ORGANIZATION_ID,+ artifact_id=artifact_id_val,+ step=step,+ artifact_type=ArtifactType.RECORDING,+ )+ uri = base.rsplit(".", 1)[0] + "." + ext+ return Artifact(+ artifact_id=artifact_id_val,+ artifact_type=ArtifactType.RECORDING,+ uri=uri,+ organization_id=TEST_ORGANIZATION_ID,+ step_id=step.step_id,+ task_id=step.task_id,+ created_at=datetime.utcnow(),+ modified_at=datetime.utcnow(),+ )+++class TestS3StorageRecordingContentType:+ """SKY-15466: the whole-display recorder's S3 lifecycle must set ``ContentType=video/mp4`` for ``.mp4``+ objects so a signed-S3 GET / Chrome <video> plays them. Real-S3 v7 proved every settled recording object+ was served ``binary/octet-stream`` (FAIL_PRODUCT_CONTRACT); these end-to-end moto head_object checks would+ fail on the current head."""++ __test__ = False # Collected with moto fixtures in test_s3_storage_moto.py.++ @pytest.mark.parametrize("supersede", [False, True], ids=["initial_store", "terminal_replacement"])+ async def test_store_artifact_mp4_sets_video_mp4(+ self, s3_storage: S3Storage, boto3_test_client: S3Client, supersede: bool+ ) -> None:+ # Both write paths (initial store and the terminal supersede/replacement) must settle .mp4 as video/mp4.+ art = _recording_artifact_with_ext(s3_storage, "mp4")+ await s3_storage.store_artifact(art, b"final-mp4-bytes", supersede_queued_prefixes=supersede)+ meta = boto3_test_client.head_object(Bucket=TEST_BUCKET, Key=S3Uri(art.uri).key)+ assert meta["ContentType"] == "video/mp4"++ async def test_store_artifact_prefix_mp4_sets_video_mp4(+ self, s3_storage: S3Storage, boto3_test_client: S3Client, tmp_path: Path+ ) -> None:+ art = _recording_artifact_with_ext(s3_storage, "mp4")+ f = tmp_path / "rec.mp4"+ f.write_bytes(b"m" * 4096)+ await s3_storage.store_artifact_prefix_from_path(art, str(f), 4096)+ meta = boto3_test_client.head_object(Bucket=TEST_BUCKET, Key=S3Uri(art.uri).key)+ assert meta["ContentType"] == "video/mp4"++ async def test_unknown_extension_no_invented_content_type(+ self, s3_storage: S3Storage, boto3_test_client: S3Client+ ) -> None:+ art = _recording_artifact_with_ext(s3_storage, "skyvernunknown15466") # project-unique ext -> guess_type None+ await s3_storage.store_artifact(art, b"opaque-bytes")+ ct = boto3_test_client.head_object(Bucket=TEST_BUCKET, Key=S3Uri(art.uri).key)["ContentType"]+ assert ct != "video/mp4" and not ct.startswith("video/") # unknown -> not invented (S3 default)++@pytest.mark.asyncioclass TestS3StorageHARCompression:"""Test S3Storage HAR file compression with zstd."""@@ -1494,6 +1555,7 @@ async def test_store_artifact_prefix_from_path_streams_bounded_reader(s3_storagestorage_class: object = None,close_file_obj: bool = False,serialize_key: str | None = None,+ content_type: str | None = None,) -> str:captured["uri"] = uricaptured["close_file_obj"] = close_file_obj@@ -1656,8 +1718,9 @@ async def test_store_artifact_serializes_only_recording_writes(storage_class: object = None,serialize_key: str | None = None,supersede_queued: bool = False,+ content_type: str | None = None,) -> str:- seen[uri] = (serialize_key, supersede_queued)+ seen[uri] = (serialize_key, supersede_queued, content_type)return uris3_storage.async_client = MagicMock()@@ -1668,10 +1731,9 @@ async def test_store_artifact_serializes_only_recording_writes(await s3_storage.store_artifact(rec, b"data", supersede_queued_prefixes=True)await s3_storage.store_artifact(other, b"data", supersede_queued_prefixes=True)- # recording terminal write is fenced by uri and (as a finalize) seals queued prefixes;- # a non-recording write is never fenced (serialize_key=None), so the seal flag is inert.- assert seen[rec.uri] == (rec.uri, True)- assert seen[other.uri][0] is None+ # RECORDING terminal write is fenced+sealed and opts into video MIME; a non-recording (HTML) write is neither.+ assert seen[rec.uri] == (rec.uri, True, "video/webm")+ assert seen[other.uri] == (None, True, None)@pytest.mark.asynciodiff --git a/tests/unit/forge/sdk/artifact/storage/test_s3_storage_moto.py b/tests/unit/forge/sdk/artifact/storage/test_s3_storage_moto.pyindex 0c725190c..a273078b3 100644--- a/tests/unit/forge/sdk/artifact/storage/test_s3_storage_moto.py+++ b/tests/unit/forge/sdk/artifact/storage/test_s3_storage_moto.py@@ -26,6 +26,9 @@ from tests.unit.forge.sdk.artifact.storage.test_s3_storage import (from tests.unit.forge.sdk.artifact.storage.test_s3_storage import (TestS3StoragePerRunRecordingClips as _PerRunRecordingContracts,)+from tests.unit.forge.sdk.artifact.storage.test_s3_storage import (+ TestS3StorageRecordingContentType as _RecordingContentTypeContracts,+)from tests.unit.forge.sdk.artifact.storage.test_s3_storage import TestS3StorageStore as _StoreContractsfrom tests.unit.forge.sdk.artifact.storage.test_s3_storage import (TestS3StorageZIPArchiveRetrieve as _ZIPArchiveContracts,@@ -97,6 +100,10 @@ class TestS3StorageContentTypeMoto(_ContentTypeContracts):__test__ = True+class TestS3StorageRecordingContentTypeMoto(_RecordingContentTypeContracts):+ __test__ = True++class TestS3StorageHARCompressionMoto(_HARCompressionContracts):__test__ = Truediff --git a/tests/unit/forge/sdk/routes/streaming/test_exfiltration_channel.py b/tests/unit/forge/sdk/routes/streaming/test_exfiltration_channel.pyindex d16acdb0f..b5c2610f2 100644--- a/tests/unit/forge/sdk/routes/streaming/test_exfiltration_channel.py+++ b/tests/unit/forge/sdk/routes/streaming/test_exfiltration_channel.py@@ -186,7 +186,7 @@ class TestExfiltrationChannelEvents:async def test_duplicate_binding_console_and_runtime_events_emit_once(self) -> None:channel, on_event = _make_channel()page = _make_page()- event_data = _make_event_data()+ event_data = {**_make_event_data(), "exfilDocId": "doc-a", "exfilSeq": 0}ExfiltrationChannel._active_binding_channels[page] = channelmessage = MagicMock()@@ -339,78 +339,16 @@ class TestExfiltrationChannelEvents:page.add_init_script.assert_not_awaited()assert page.evaluate.await_count == 2- def test_console_fingerprint_ignores_volatile_timestamp(self) -> None:- channel, _ = _make_channel()- first = {**_make_event_data(), "timestamp": 1000.0}- jittered = {**_make_event_data(), "timestamp": 1002.0}-- assert channel._console_fingerprint(first) == channel._console_fingerprint(jittered)-- def test_should_emit_console_event_dedupes_across_ms_jitter(self) -> None:- channel, _ = _make_channel()- first = {**_make_event_data(), "timestamp": 1000.0}- jittered = {**_make_event_data(), "timestamp": 1002.0}-- assert channel._should_emit_console_event(first) is True- assert channel._should_emit_console_event(jittered) is False-- def test_should_emit_console_event_keeps_distinct_interactions(self) -> None:- channel, _ = _make_channel()- first = {**_make_event_data(), "timestamp": 1000.0}- distinct_target = {- **_make_event_data(),- "timestamp": 1002.0,- "target": {"tagName": "BUTTON", "id": "cancel", "skyId": "sky-2", "text": ["Cancel"]},- }-- assert channel._should_emit_console_event(first) is True- assert channel._should_emit_console_event(distinct_target) is True-- def test_reconnect_recapture_with_jitter_emits_single_interaction(self) -> None:+ def test_unstamped_events_always_emit(self) -> None:channel, on_event = _make_channel()page = _make_page()ExfiltrationChannel._active_binding_channels[page] = channel- # A reconnect re-injects the exfiltrate script, re-emitting the same DOM- # interaction with an advanced browser clock; the stable fingerprint dedupes- # it even though the channel instance (and its cache) survived the reconnect.- channel._handle_binding_event({"page": page}, {**_make_event_data(), "timestamp": 1000.0})- channel._handle_binding_event({"page": page}, {**_make_event_data(), "timestamp": 1002.0})-- on_event.assert_called_once()-- def test_fingerprint_treats_none_and_absent_keys_as_equal(self) -> None:- # The binding transport serializes JS undefined to None while the console- # transports drop those keys entirely (JSON.stringify). Both shapes must- # dedup as one event or every interaction is delivered 2-3x.- channel, _ = _make_channel()- via_binding = {- **_make_event_data(),- "inputValue": None,- "mousePosition": {"xa": 10, "ya": 20, "xp": None, "yp": None},- }- via_console = {- **_make_event_data(),- "mousePosition": {"xa": 10, "ya": 20},- }-- assert channel._console_fingerprint(via_binding) == channel._console_fingerprint(via_console)-- def test_should_emit_console_event_dedupes_none_vs_absent_duplicates(self) -> None:- channel, _ = _make_channel()- via_binding = {**_make_event_data(), "inputValue": None}- via_console = _make_event_data()-- assert channel._should_emit_console_event(via_binding) is True- assert channel._should_emit_console_event(via_console) is False-- def test_should_emit_console_event_keeps_distinct_values_over_none(self) -> None:- channel, _ = _make_channel()- first = {**_make_event_data(), "inputValue": None}- distinct = {**_make_event_data(), "inputValue": "hello"}+ event_data = _make_event_data()+ channel._handle_binding_event({"page": page}, event_data)+ channel._handle_binding_event({"page": page}, event_data)- assert channel._should_emit_console_event(first) is True- assert channel._should_emit_console_event(distinct) is True+ assert on_event.call_count == 2def _make_stamped_event_data(seq: int = 0, doc_id: str = "doc-a") -> dict[str, object]:diff --git a/tests/unit/services/test_browser_recording_code_first.py b/tests/unit/services/test_browser_recording_code_first.pyindex 244f29312..e3f225701 100644--- a/tests/unit/services/test_browser_recording_code_first.py+++ b/tests/unit/services/test_browser_recording_code_first.py@@ -1,6 +1,7 @@import pytestfrom skyvern.services.browser_recording.code_first import (+ _site_slug,actions_to_code_first_blocks,apply_draft_overlay,segment_actions,@@ -411,6 +412,33 @@ def test_segment_actions_uses_first_action_url_as_entry() -> None:assert segments[0].source_url == START_URL+async def test_process_binds_credentials_only_for_a_caller_that_substitutes_tokens(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ email = make_input(1000, "[email protected]", selector="#email", accessible_name="Email", autocomplete="username")+ password = make_input(2000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ actions: list[Action] = [email, password]+ drafts = [draft_for(email), draft_for(password, credential_id="cred_123", credential_kind="password")]+ monkeypatch.setattr(Processor, "compressed_chunks_to_events", lambda self, chunks: [])+ monkeypatch.setattr(+ Processor,+ "events_to_actions",+ lambda self, events, machines=None, initial_actions=None: actions,+ )+ processor = Processor(PBS_ID, ORG_ID, WP_ID)++ unsupported_blocks, _ = await processor.process(["chunk"], draft_steps=drafts, code_first=True)+ supported_blocks, _ = await processor.process(+ ["chunk"], draft_steps=drafts, code_first=True, supports_credential_tokens=True+ )++ # The route defaults to off, so an old frontend never receives code reading a token it+ # cannot rename - which would persist a block that fails at run time with a NameError.+ assert "cred_123" not in unsupported_blocks[0].code+ assert "cred_123.password" in supported_blocks[0].code++@pytest.mark.asyncioasync def test_process_code_first_returns_code_blocks(monkeypatch: pytest.MonkeyPatch) -> None:actions: list[Action] = [make_click(1000, selector="#submit", accessible_name="Go")]@@ -464,6 +492,274 @@ async def test_process_code_first_prefers_draft_overlay_over_drafts_to_blocks(assert "#delete" not in blocks[0].code+def test_recorded_login_binds_the_credential_and_its_identifier() -> None:+ email = make_input(1000, "[email protected]", selector="#email", accessible_name="Email", autocomplete="username")+ password = make_input(2000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ submit = make_click(3000, selector="#login", role="button", accessible_name="Log in")+ drafts = [+ draft_for(email),+ draft_for(password, credential_id="cred_123", credential_kind="password"),+ draft_for(submit),+ ]++ result = actions_to_code_first_blocks([email, password, submit], drafts)++ assert result is not None+ blocks, parameters = result+ code = blocks[0].code+ assert 'await page.locator("#pw").fill(cred_123.password)' in code+ # The identifier typed just before the credential fill is bound too, or an empty+ # parameter lands in it at run time.+ assert 'await page.locator("#email").fill(cred_123.username)' in code+ assert "[email protected]" not in code+ # One credential parameter keyed by the credential id: a token the editor re-keys.+ assert [(parameter.key, parameter.parameter_type) for parameter in parameters] == [("cred_123", "credential")]+ assert parameters[0].credential_id == "cred_123"+ assert blocks[0].parameter_keys == ["cred_123"]+++def test_recorded_secret_fill_binds_the_credentials_single_value_field() -> None:+ token = make_input(1000, "sk-live-abc", selector="#token", accessible_name="API token")+ save = make_click(2000, selector="#save", role="button", accessible_name="Save")+ drafts = [draft_for(token, credential_id="cred_secret", credential_kind="secret"), draft_for(save)]++ result = actions_to_code_first_blocks([token, save], drafts)++ assert result is not None+ blocks, parameters = result+ # A secret credential holds one value, so the fill can name it - as the legacy path does.+ assert 'await page.locator("#token").fill(cred_secret.secret_value)' in blocks[0].code+ assert "sk-live-abc" not in blocks[0].code+ assert [(parameter.key, parameter.parameter_type) for parameter in parameters] == [("cred_secret", "credential")]+++def test_a_classified_field_before_a_password_is_not_claimed_as_the_identifier() -> None:+ current = make_input(1000, "old-pw", selector="#current", accessible_name="Current password", input_type="password")+ new = make_input(2000, "new-pw", selector="#new", accessible_name="New password", input_type="password")+ drafts = [draft_for(current), draft_for(new, credential_id="cred_login", credential_kind="password")]++ result = actions_to_code_first_blocks([current, new], drafts)++ assert result is not None+ blocks, _ = result+ # A second password box is not an identifier; filling the username into it types the wrong value.+ assert "cred_login.username" not in blocks[0].code+ assert 'await page.locator("#new").fill(cred_login.password)' in blocks[0].code+++def test_recorded_credit_card_fill_stays_an_unbound_parameter() -> None:+ card = make_input(1000, "4111111111111111", selector="#card", accessible_name="Card number")+ drafts = [draft_for(card, credential_id="cred_card", credential_kind="credit_card")]++ result = actions_to_code_first_blocks([card], drafts)++ assert result is not None+ blocks, parameters = result+ # A credential parameter resolves only username/password/totp, and the recorder never+ # captures which card field was typed, so there is nothing to point the fill at.+ assert 'await page.locator("#card").fill(str(card_number))' in blocks[0].code+ assert [parameter.parameter_type for parameter in parameters] == ["workflow"]+++def test_a_field_bound_to_another_credential_is_not_claimed_as_the_identifier() -> None:+ token = make_input(1000, "abc123", selector="#token", accessible_name="API token")+ password = make_input(2000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ drafts = [+ draft_for(token, credential_id="cred_secret", credential_kind="secret"),+ draft_for(password, credential_id="cred_login", credential_kind="password"),+ ]++ result = actions_to_code_first_blocks([token, password], drafts)++ assert result is not None+ blocks, _ = result+ assert "cred_login.username" not in blocks[0].code+ assert 'await page.locator("#pw").fill(cred_login.password)' in blocks[0].code+++def test_recorded_totp_fill_reads_the_credentials_runtime_code() -> None:+ email = make_input(1000, "[email protected]", selector="#email", accessible_name="Email", autocomplete="username")+ password = make_input(2000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ submit = make_click(3000, selector="#login", role="button", accessible_name="Log in")+ code = make_input(4000, "123456", selector="#otp", accessible_name="One-time code")+ drafts = [+ draft_for(email),+ draft_for(password, credential_id="cred_123", credential_kind="password"),+ draft_for(submit),+ draft_for(code, credential_id="cred_123", credential_kind="totp"),+ ]++ result = actions_to_code_first_blocks([email, password, submit, code], drafts)++ assert result is not None+ blocks, parameters = result+ # A one-time code has no stored value: it resolves at run time through the credential.+ assert 'await page.locator("#otp").fill(await cred_123.otp())' in blocks[0].code+ assert "123456" not in blocks[0].code+ # Both fills read the one credential, so it stays a single parameter.+ assert [parameter.key for parameter in parameters] == ["cred_123"]+++def test_only_an_identifier_field_is_claimed_for_the_username() -> None:+ email = make_input(1000, "[email protected]", selector="#email", accessible_name="Email", autocomplete="username")+ company = make_input(2000, "acme", selector="#company", accessible_name="Company domain", input_type="text")+ password = make_input(3000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ drafts = [+ draft_for(email),+ draft_for(company),+ draft_for(password, credential_id="cred_123", credential_kind="password"),+ ]++ result = actions_to_code_first_blocks([email, company, password], drafts)++ assert result is not None+ blocks, parameters = result+ # The identifier is claimed even with another field of the same form between the two, and+ # the tenant field keeps its own parameter instead of receiving the username.+ assert 'await page.locator("#email").fill(cred_123.username)' in blocks[0].code+ assert 'await page.locator("#company").fill(str(company_domain))' in blocks[0].code+ assert [parameter.key for parameter in parameters] == ["company_domain", "cred_123"]+++def test_a_search_box_before_a_password_is_not_claimed_for_the_username() -> None:+ search = make_input(1000, "widgets", selector="#q", accessible_name="Search", input_type="text")+ password = make_input(2000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ drafts = [draft_for(search), draft_for(password, credential_id="cred_123", credential_kind="password")]++ result = actions_to_code_first_blocks([search, password], drafts)++ assert result is not None+ blocks, _ = result+ assert "cred_123.username" not in blocks[0].code+ assert 'await page.locator("#q").fill(str(search))' in blocks[0].code+++def test_a_hover_between_the_identifier_and_the_password_keeps_the_binding() -> None:+ email = make_input(1000, "[email protected]", selector="#email", accessible_name="Email", autocomplete="username")+ hover = make_hover(2000, selector="#tooltip", accessible_name="Help")+ password = make_input(3000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ drafts = [+ draft_for(email),+ draft_for(hover),+ draft_for(password, credential_id="cred_123", credential_kind="password"),+ ]++ result = actions_to_code_first_blocks([email, hover, password], drafts)++ assert result is not None+ blocks, _ = result+ # A stray hover has a 2s dwell of its own; it must not disable the binding.+ assert 'await page.locator("#email").fill(cred_123.username)' in blocks[0].code+++def test_a_caller_that_cannot_substitute_tokens_gets_no_credential_binding() -> None:+ email = make_input(1000, "[email protected]", selector="#email", accessible_name="Email", autocomplete="username")+ password = make_input(2000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ drafts = [draft_for(email), draft_for(password, credential_id="cred_123", credential_kind="password")]++ result = actions_to_code_first_blocks([email, password], drafts, bind_credentials=False)++ assert result is not None+ blocks, parameters = result+ # An old frontend renames the parameters but not the code, which would persist a block+ # whose code reads a token nothing declares - a NameError that saves clean.+ assert "cred_123" not in blocks[0].code+ assert [parameter.parameter_type for parameter in parameters] == ["workflow", "workflow"]+++def test_an_email_field_is_not_assumed_to_be_a_login_identifier() -> None:+ login = make_input(+ 1000,+ "[email protected]",+ selector="#login",+ accessible_name="Email",+ id="user_email",+ input_type="email",+ autocomplete="email",+ )+ password = make_input(2000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ drafts = [draft_for(login), draft_for(password, credential_id="cred_123", credential_kind="password")]++ result = actions_to_code_first_blocks([login, password], drafts)++ assert result is not None+ blocks, _ = result+ # An email input is also how an account-settings form collects a new contact address, and+ # the confirmation below it is credential-bound. `type=email` and `autocomplete=email`+ # describe the value, not that it authenticates, so this field keeps its own parameter.+ assert "cred_123.username" not in blocks[0].code+ assert 'await page.locator("#login").fill(str(email))' in blocks[0].code+++def test_an_autocomplete_username_field_is_claimed_for_the_username() -> None:+ login = make_input(1000, "someone", selector="#login", input_type="text", autocomplete="username")+ password = make_input(2000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ drafts = [draft_for(login), draft_for(password, credential_id="cred_123", credential_kind="password")]++ result = actions_to_code_first_blocks([login, password], drafts)++ assert result is not None+ blocks, _ = result+ # The other typed identifier signal: a text input that declares what it holds.+ assert 'await page.locator("#login").fill(cred_123.username)' in blocks[0].code+++def test_clicking_into_the_password_field_keeps_the_identifier_binding() -> None:+ email = make_input(1000, "[email protected]", selector="#email", autocomplete="username")+ focus = make_click(1500, selector="#pw", tag_name="INPUT", role="textbox")+ password = make_input(2000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ drafts = [+ draft_for(email),+ draft_for(focus),+ draft_for(password, credential_id="cred_123", credential_kind="password"),+ ]++ result = actions_to_code_first_blocks([email, focus, password], drafts)++ assert result is not None+ blocks, _ = result+ # The ordinary mouse-driven login clicks the password box; the recorder emits that click.+ assert 'await page.locator("#email").fill(cred_123.username)' in blocks[0].code+++def test_a_button_click_before_the_password_ends_the_form() -> None:+ email = make_input(1000, "[email protected]", selector="#email", autocomplete="username")+ submit = make_click(1500, selector="#continue", tag_name="BUTTON", role="button", accessible_name="Continue")+ password = make_input(2000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ drafts = [+ draft_for(email),+ draft_for(submit),+ draft_for(password, credential_id="cred_123", credential_kind="password"),+ ]++ result = actions_to_code_first_blocks([email, submit, password], drafts)++ assert result is not None+ blocks, _ = result+ # A two-page login already leaves its first page a separate step; reaching over the button+ # would let one credential absorb whatever came before an unrelated form.+ assert "cred_123.username" not in blocks[0].code+++def test_a_submit_input_before_the_password_ends_the_form() -> None:+ newsletter = make_input(1000, "[email protected]", selector="#news", autocomplete="username")+ submit = make_click(1500, selector="#subscribe", tag_name="INPUT", input_type="submit", role="button")+ password = make_input(2000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ drafts = [+ draft_for(newsletter),+ draft_for(submit),+ draft_for(password, credential_id="cred_123", credential_kind="password"),+ ]++ result = actions_to_code_first_blocks([newsletter, submit, password], drafts)++ assert result is not None+ blocks, _ = result+ # `<input type="submit">` carries the same tag name as a text box but submits a form; an+ # email typed into that form belongs to it, not to the login that follows.+ assert "cred_123.username" not in blocks[0].code++def test_enter_submit_emits_a_press_and_keeps_its_navigation_in_segment() -> None:actions: list[Action] = [make_input(1000, "boots", selector="#search"),@@ -492,3 +788,157 @@ def test_press_key_without_a_locator_falls_back_to_the_keyboard() -> None:assert result is not Noneblocks, _ = resultassert 'await page.keyboard.press("Escape")' in blocks[0].code+++def test_block_labels_describe_what_each_segment_does() -> None:+ actions: list[Action] = [+ make_url_change(0, "http://127.0.0.1:8899/login"),+ make_input(1000, "user", url="http://127.0.0.1:8899/login", selector="#user", accessible_name="Username"),+ make_input(+ 2000,+ "hunter2",+ url="http://127.0.0.1:8899/login",+ selector="#pw",+ accessible_name="Password",+ input_type="password",+ ),+ make_click(3000, url="http://127.0.0.1:8899/login", selector="#submit", accessible_name="Log in"),+ make_url_change(30000, "https://shop.example.com/"),+ make_input(+ 31000, "widget", url="https://shop.example.com/", selector="#q", accessible_name="Search for a product"+ ),+ make_url_change(60000, "https://shop.example.com/cart"),+ make_click(61000, url="https://shop.example.com/cart", selector="#pay", accessible_name="Proceed to checkout"),+ make_url_change(90000, "https://shop.example.com/cart?retry=1"),+ make_click(+ 91000, url="https://shop.example.com/cart?retry=1", selector="#pay", accessible_name="Proceed to checkout"+ ),+ ]++ result = actions_to_code_first_blocks(actions, None)++ assert result is not None+ blocks, _ = result+ # A local fixture host contributes no site name, so the login segment is just the verb;+ # the trailing "_2" keeps two identical checkout segments unique.+ assert [block.label for block in blocks] == [+ "log_in",+ "search_example",+ "click_proceed_to_checkout",+ "click_proceed_to_checkout_2",+ ]+++def test_key_press_only_segment_is_named_after_the_key() -> None:+ actions: list[Action] = [+ make_url_change(0, "https://example.com/viewer"),+ make_press_key(1000, "Escape"),+ ]++ result = actions_to_code_first_blocks(actions, None)++ assert result is not None+ blocks, _ = result+ assert [block.label for block in blocks] == ["press_escape"]+++def test_block_label_matches_search_as_a_complete_word() -> None:+ actions: list[Action] = [+ make_input(1000, "automation", selector="#topic", accessible_name="Research topic"),+ ]++ result = actions_to_code_first_blocks(actions, None)++ assert result is not None+ blocks, _ = result+ assert [block.label for block in blocks] == ["fill_in_research_topic"]+++def test_block_label_does_not_use_textarea_content() -> None:+ actions: list[Action] = [+ make_input(+ 1000,+ "replacement",+ selector='textarea[name="notes"]',+ tag_name="TEXTAREA",+ texts=["private customer content"],+ ),+ ]++ result = actions_to_code_first_blocks(actions, None)++ assert result is not None+ blocks, _ = result+ assert [block.label for block in blocks] == ["fill_in_a_field"]+++def test_block_label_ignores_an_interaction_dropped_by_synthesis() -> None:+ actions: list[Action] = [+ make_url_change(0, "https://example.com/start"),+ make_hover(1000, accessible_name="Private menu copy"),+ ]++ result = actions_to_code_first_blocks(actions, None)++ assert result is not None+ blocks, _ = result+ assert [block.label for block in blocks] == ["open_example"]+++ ("url", "expected"),+ [+ ("https://app.example.com/x", "example"),+ # A multi-label public suffix must not name the block after "co".+ ("https://shop.example.co.uk/x", "example"),+ ("https://shop.example.ai/x", "example"),+ ("https://shop.example.co.kr/x", "example"),+ ("https://intranet/x", "intranet"),+ # Hosts with no name in them contribute nothing to the label.+ ("http://127.0.0.1:8899/login", ""),+ ("http://[::1]:8080/login", ""),+ ("http://localhost:3000/login", ""),+ ],+)+def test_site_slug_reads_a_name_only_from_named_hosts(url: str, expected: str) -> None:+ assert _site_slug(url) == expected+++def test_a_credential_bound_on_the_focus_click_fills_the_field() -> None:+ login = make_input(1000, "someone", selector="#login", autocomplete="username")+ focus = make_click(1500, selector="#pw", tag_name="INPUT", input_type="password")+ password = make_input(2000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ drafts = [+ draft_for(login),+ draft_for(focus, credential_id="cred_123", credential_kind="password"),+ draft_for(password),+ ]++ result = actions_to_code_first_blocks([login, focus, password], drafts)++ assert result is not None+ blocks, parameters = result+ # The panel offers the prompt on the click card and dismisses the fill's once bound, so the+ # binding arrives on the click; a code block still has to fill the field.+ assert 'await page.locator("#pw").fill(cred_123.password)' in blocks[0].code+ assert 'await page.locator("#login").fill(cred_123.username)' in blocks[0].code+ assert [parameter.key for parameter in parameters] == ["cred_123"]+++def test_a_focus_click_credential_only_moves_to_its_own_field() -> None:+ focus = make_click(1000, selector="#pw", tag_name="INPUT", input_type="password")+ other = make_input(1500, "widgets", selector="#search", accessible_name="Search", input_type="text")+ password = make_input(2000, "hunter2", selector="#pw", accessible_name="Password", input_type="password")+ drafts = [+ draft_for(focus, credential_id="cred_123", credential_kind="password"),+ draft_for(other),+ draft_for(password),+ ]++ result = actions_to_code_first_blocks([focus, other, password], drafts)++ assert result is not None+ blocks, _ = result+ # The next fill after the click is not necessarily the field that was clicked.+ assert 'await page.locator("#search").fill(str(search))' in blocks[0].code+ assert 'await page.locator("#pw").fill(cred_123.password)' in blocks[0].codediff --git a/tests/unit/test_browser_context_factory_marker.py b/tests/unit/test_browser_context_factory_marker.pyindex fb873221f..5456c5840 100644--- a/tests/unit/test_browser_context_factory_marker.py+++ b/tests/unit/test_browser_context_factory_marker.py@@ -9,6 +9,8 @@ auto-stamp.from __future__ import annotations+import asyncio+from types import SimpleNamespacefrom typing import Anyfrom unittest.mock import AsyncMock, MagicMock@@ -17,7 +19,8 @@ from playwright.async_api import BrowserContext, Locator, Pagefrom skyvern.forge.sdk.workflow.models.code_block_recorder import RecordingPagefrom skyvern.webeye import browser_factory as factory_module-from skyvern.webeye.browser_artifacts import BrowserArtifacts+from skyvern.webeye import display_recorder as dr+from skyvern.webeye.browser_artifacts import BrowserArtifacts, VideoArtifactfrom skyvern.webeye.browser_factory import BrowserContextFactoryfrom skyvern.webeye.playwright_input import playwright_input_defaults_for_page@@ -130,6 +133,39 @@ def _factory_harness(monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr(factory_module, "app", _FakeApp())+ "recorder, eligible, expect_listener",+ [+ (None, True, True), # eligible but acquisition REFUSED -> real call site MUST register Playwright fallback+ (object(), False, False), # whole-display recorder acquired -> suppress Playwright (no dual recording)+ ],+ ids=["refused_registers_fallback", "acquired_suppresses"],+)+async def test_create_browser_context_gates_playwright_video_on_acquired_recorder(+ monkeypatch: pytest.MonkeyPatch, recorder: object | None, eligible: bool, expect_listener: bool+) -> None:+ """Drives the REAL create_browser_context call site: the Playwright video listener registers iff no+ whole-display recorder was acquired, so an eligible-but-refused acquisition still falls back to Playwright."""+ listener = MagicMock()+ _factory_harness(monkeypatch)+ monkeypatch.setattr(factory_module, "set_popup_video_listener", listener)+ monkeypatch.setattr(factory_module, "attach_only_enforcing", lambda: False)++ async def _creator(playwright: Any, **kwargs: Any) -> tuple[Any, BrowserArtifacts, None]:+ artifacts = BrowserArtifacts()+ artifacts.local_display_recording_eligible = eligible+ artifacts._display_recorder = recorder+ return MagicMock(), artifacts, None++ BrowserContextFactory.register_type("test-recorder-gate", _creator)+ monkeypatch.setattr(factory_module.settings, "BROWSER_TYPE", "test-recorder-gate")++ await BrowserContextFactory.create_browser_context(playwright=object())++ assert listener.called is expect_listener++@pytest.mark.asyncioasync def test_factory_registers_authoritative_playwright_input_defaults(monkeypatch: pytest.MonkeyPatch) -> None:context = MagicMock()@@ -290,3 +326,115 @@ async def test_bootstrap_error_propagates_unwrapped_but_other_errors_are_wrappedmonkeypatch.setattr(factory_module.settings, "BROWSER_TYPE", "test-generic-fail")with pytest.raises(UnknownErrorWhileCreatingBrowserContext):await BrowserContextFactory.create_browser_context(playwright=object())+++async def test_headful_creator_releases_recorder_when_launch_is_cancelled(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Any+) -> None:+ # P1: a Temporal cancel (BaseException) landing on the pending launch_persistent_context must release the+ # already-started whole-display recorder — otherwise the bridge+ffmpeg keep recording and the display flock+ # and _REGISTRY entry leak pod-wide. RED at head: except-Exception does not catch CancelledError.+ monkeypatch.setattr(factory_module, "make_temp_directory", lambda **_: str(tmp_path / "ud"))+ monkeypatch.setattr(factory_module, "initialize_download_dir", lambda: str(tmp_path / "dl"))+ monkeypatch.setattr(BrowserContextFactory, "update_chromium_browser_preferences", MagicMock())+ monkeypatch.setattr(+ BrowserContextFactory,+ "build_browser_args",+ MagicMock(return_value={"record_har_path": str(tmp_path / "h.har")}),+ )++ rec = dr.DisplayRecorder(+ display=":99",+ owner_id="wr_t1",+ process=SimpleNamespace(returncode=0), # already exited: stop() completes without signalling+ lock_fd=-1,+ video_artifact=VideoArtifact(video_path=str(tmp_path / "r.mp4")),+ )+ dr._REGISTRY[(":99", "wr_t1")] = rec++ async def _fake_prepare(browser_args: dict, browser_artifacts: BrowserArtifacts, **_: Any) -> None:+ browser_artifacts._display_recorder = rec+ browser_artifacts.video_artifacts = [rec.video_artifact]+ browser_artifacts._display_recorder_acquisition = dr.DisplayRecorderAcquisition(rec, rec.video_artifact, True)++ monkeypatch.setattr(factory_module, "prepare_local_display_recording", _fake_prepare)+ playwright = MagicMock()+ playwright.chromium.launch_persistent_context = AsyncMock(side_effect=asyncio.CancelledError())++ try:+ with pytest.raises(asyncio.CancelledError):+ await factory_module._create_headful_chromium(playwright, workflow_run_id="wr_t1")+ assert (":99", "wr_t1") not in dr._REGISTRY # released + deregistered, not orphaned+ assert rec.is_stopped+ finally:+ dr._REGISTRY.pop((":99", "wr_t1"), None)+++ "release_cancels, expected_exc",+ [+ (False, Exception), # ordinary post-return error -> close before release, cleanup runs, error wraps+ (True, asyncio.CancelledError), # a release cancel: cleanup_func STILL runs once, then cancellation wins+ ],+ ids=["ordinary_error", "release_cancel_runs_cleanup"],+)+async def test_factory_closes_context_before_releasing_recorder_on_post_return_failure(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Any, release_cancels: bool, expected_exc: type[BaseException]+) -> None:+ # A post-return setup failure must unmap the tenant's window (context.close) BEFORE the display fence is+ # released (freeing the fence while mapped lets a later different-owner acquire capture it), and cleanup_func+ # must run exactly once afterward. A CancelledError from the release still runs cleanup_func, then wins.+ order: list[str] = []+ _factory_harness(monkeypatch)++ rec = dr.DisplayRecorder(+ display=":99",+ owner_id="wr_t1",+ process=SimpleNamespace(returncode=0), # already exited: stop() completes without signalling+ lock_fd=-1,+ video_artifact=VideoArtifact(video_path=str(tmp_path / "r.mp4")),+ )+ dr._REGISTRY[(":99", "wr_t1")] = rec++ fake_context = AsyncMock()++ async def _close() -> None:+ order.append("close")++ fake_context.close = _close+ cleanup = AsyncMock(side_effect=lambda: order.append("cleanup"))++ async def _creator(playwright: Any, **kwargs: Any) -> tuple[Any, BrowserArtifacts, Any]:+ artifacts = BrowserArtifacts()+ artifacts._display_recorder = rec+ artifacts._display_recorder_acquisition = dr.DisplayRecorderAcquisition(rec, rec.video_artifact, True)+ return fake_context, artifacts, cleanup++ BrowserContextFactory.register_type("test-close-before-release", _creator)+ monkeypatch.setattr(factory_module.settings, "BROWSER_TYPE", "test-close-before-release")++ real_release = factory_module.release_display_recorder++ async def _release(recorder: Any) -> None:+ order.append("release")+ await real_release(recorder) # actually stop + deregister+ if release_cancels: # finish-then-re-raise: recorder fully released, then the cancel surfaces+ raise asyncio.CancelledError()++ monkeypatch.setattr(factory_module, "release_display_recorder", _release)++ def _boom(*_a: Any, **_k: Any) -> None:+ raise RuntimeError("post-return-boom")++ monkeypatch.setattr(factory_module, "register_playwright_input_context", _boom)++ try:+ with pytest.raises(expected_exc):+ await BrowserContextFactory.create_browser_context(playwright=object())+ assert order == ["close", "release", "cleanup"] # unmap -> fence release -> cleanup_func, in that order+ cleanup.assert_awaited_once() # cleanup_func runs exactly once even when the release cancels+ assert (":99", "wr_t1") not in dr._REGISTRY and rec.is_stopped # released + deregistered+ finally:+ dr._REGISTRY.pop((":99", "wr_t1"), None)diff --git a/tests/unit/test_cdp_connection_retry.py b/tests/unit/test_cdp_connection_retry.pyindex 6a43f370e..fafb951fd 100644--- a/tests/unit/test_cdp_connection_retry.py+++ b/tests/unit/test_cdp_connection_retry.py@@ -1,6 +1,8 @@from __future__ import annotations+import asyncioimport socket+import tracebackfrom typing import Any, castfrom unittest.mock import AsyncMock, MagicMock, patch@@ -11,7 +13,7 @@ from playwright._impl._errors import TimeoutError as PWTimeoutErrorfrom skyvern.config import settingsfrom skyvern.exceptions import BlockedHostfrom skyvern.webeye.browser_engine import BrowserEngineMetadata, BrowserEngineSelection-from skyvern.webeye.browser_errors import BrowserCdpConnectionError+from skyvern.webeye.browser_errors import BrowserCdpAcquisitionError, BrowserCdpConnectionErrorfrom skyvern.webeye.cdp_retry import _resolve_retry_budget, connect_over_cdp_with_retry, is_cdp_connection_error@@ -23,12 +25,16 @@ class _FakeEngineRetryable(_FakeEngineError):pass+class _FakeEngineTimeout(_FakeEngineError):+ pass++def _fake_selection() -> BrowserEngineSelection:return BrowserEngineSelection(name="fake",start_driver=cast(Any, lambda: None),error_type=_FakeEngineError,- timeout_error_type=_FakeEngineError,+ timeout_error_type=_FakeEngineTimeout,metadata=BrowserEngineMetadata(name="fake"),selection_reason="test-fake",retryable_error_types=(_FakeEngineRetryable,),@@ -47,6 +53,20 @@ def _set_budget(monkeypatch: pytest.MonkeyPatch, attempts: int, backoff: list[flclass TestRetryBehavior:+ @pytest.mark.asyncio+ async def test_cancellation_is_not_wrapped_as_acquisition_failure(self) -> None:+ pw = _make_playwright(asyncio.CancelledError())++ with pytest.raises(asyncio.CancelledError):+ await connect_over_cdp_with_retry(+ pw,+ "wss://cdp.example.test",+ validate_browser_address=False,+ retry=False,+ )++ pw.chromium.connect_over_cdp.assert_awaited_once()+@pytest.mark.asyncioasync def test_validates_browser_address_by_default(self, monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr(settings, "ENV", "prod")@@ -167,10 +187,59 @@ class TestRetryBehavior:assert pw.chromium.connect_over_cdp.call_count == 3@pytest.mark.asyncio- async def test_non_retryable_error_raises_immediately(self):- pw = _make_playwright([PWError("net::ERR_NAME_NOT_RESOLVED")])- with pytest.raises(PWError):+ async def test_native_non_retryable_connect_failure_is_an_acquisition_fact(self):+ raw_address = "wss://[email protected]/devtools/browser/session?token=hidden"+ native_error = PWError(f"connect failed at {raw_address}; authorization: bare-secret")+ pw = _make_playwright([native_error])++ with pytest.raises(BrowserCdpAcquisitionError) as excinfo:await connect_over_cdp_with_retry(pw, "http://10.0.0.1:9224", validate_browser_address=False)++ assert pw.chromium.connect_over_cdp.call_count == 1+ assert excinfo.value.__cause__ is None+ assert excinfo.value.__suppress_context__ is True+ assert "secret" not in str(excinfo.value)+ assert "token=hidden" not in str(excinfo.value)+ assert "cdp.example.test" not in str(excinfo.value)+ assert "bare-secret" not in str(excinfo.value)+ assert str(excinfo.value) == "Browser driver failed during CDP connect to [remote browser endpoint]."+ rendered = "".join(traceback.format_exception(excinfo.value))+ assert "bare-secret" not in rendered+ assert "token=hidden" not in rendered+ assert "cdp.example.test" not in rendered+ assert not is_cdp_connection_error(excinfo.value)++ @pytest.mark.asyncio+ async def test_selected_engine_native_connect_failure_is_an_acquisition_fact(self):+ native_error = _FakeEngineError("selected engine connect failed")+ pw = _make_playwright([native_error])++ with pytest.raises(BrowserCdpAcquisitionError) as excinfo:+ await connect_over_cdp_with_retry(+ pw,+ "http://10.0.0.1:9224",+ selection=_fake_selection(),+ validate_browser_address=False,+ )++ assert excinfo.value.__cause__ is None+ assert excinfo.value.__suppress_context__ is True+ assert pw.chromium.connect_over_cdp.call_count == 1++ @pytest.mark.asyncio+ async def test_selected_engine_does_not_claim_a_foreign_native_connect_failure(self):+ native_error = PWError("foreign driver connect failed")+ pw = _make_playwright([native_error])++ with pytest.raises(PWError) as excinfo:+ await connect_over_cdp_with_retry(+ pw,+ "http://10.0.0.1:9224",+ selection=_fake_selection(),+ validate_browser_address=False,+ )++ assert excinfo.value is native_errorassert pw.chromium.connect_over_cdp.call_count == 1@pytest.mark.asynciodiff --git a/tests/unit/test_code_block_recording_artifact.py b/tests/unit/test_code_block_recording_artifact.pyindex c4848c8bf..7419922eb 100644--- a/tests/unit/test_code_block_recording_artifact.py+++ b/tests/unit/test_code_block_recording_artifact.py@@ -15,10 +15,11 @@ from datetime import datetime, timezonefrom pathlib import Pathfrom types import SimpleNamespacefrom typing import Any-from unittest.mock import AsyncMock, patch+from unittest.mock import AsyncMock, MagicMock, patchimport pytest+from skyvern.forge import appfrom skyvern.forge.sdk.artifact.manager import ArtifactManagerfrom skyvern.forge.sdk.artifact.models import Artifact, ArtifactTypefrom skyvern.forge.sdk.workflow.models.block import CodeBlock@@ -597,3 +598,34 @@ async def test_at_failure_url_masks_registered_secrets() -> None:assert "123456" not in persisted_url# The rest of the URL is the diagnostic payload and must survive masking.assert persisted_url == "https://portal.example.com/mfa?token=*****&step=verify"+++ "extension, expected_suffix",+ [("mp4", ".mp4"), ("webm", ".webm"), (None, ".webm")],+ ids=["mp4_video_creates_mp4_uri", "webm_video_keeps_webm_uri", "default_video_keeps_webm_uri"],+)+async def test_run_recording_artifact_uri_matches_video_extension(+ monkeypatch: pytest.MonkeyPatch, extension: str | None, expected_suffix: str+) -> None:+ # Entry-point (real ArtifactManager URI, not mock-arg): an mp4 VideoArtifact must key the RECORDING row .mp4.+ va = [VideoArtifact(video_path="/tmp/recording.mp4", video_data=b"partial", video_file_extension=extension)]+ block = SimpleNamespace(workflow_run_block_id="wrb", workflow_run_id="wr", organization_id="o")+ captured: dict[str, str] = {}++ async def capture(*, uri: str, **_: object) -> str:+ captured["uri"] = uri+ return "a_recording"++ real = ArtifactManager()+ monkeypatch.setattr(real, "_create_artifact", AsyncMock(side_effect=capture))+ monkeypatch.setattr(app, "ARTIFACT_MANAGER", real)+ monkeypatch.setattr(app.STORAGE, "build_workflow_run_block_uri", MagicMock(return_value="s3://b/recording.webm"))+ monkeypatch.setattr(app.BROWSER_MANAGER, "get_video_artifacts", AsyncMock(return_value=va))+ monkeypatch.setattr(app.DATABASE.workflow_runs, "get_workflow_run", AsyncMock(return_value=None))+ monkeypatch.setattr(app.DATABASE.observer, "get_workflow_run_block", AsyncMock(return_value=block))+ await _code_block()._ensure_run_recording_artifact(+ browser_state=_browser_state(va), workflow_run_id="wr", workflow_run_block_id="wrb", organization_id="o"+ )+ assert captured["uri"].endswith(expected_suffix)diff --git a/tests/unit/test_commit_evidence.py b/tests/unit/test_commit_evidence.pyindex 9bdc65bed..c7a79ad0c 100644--- a/tests/unit/test_commit_evidence.py+++ b/tests/unit/test_commit_evidence.py@@ -12,6 +12,7 @@ capture, or a secret value all leave a bare success with no evidence.from __future__ import annotations+import jsonfrom datetime import UTC, datetimefrom types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMock, patch@@ -39,6 +40,12 @@ _COMMITTED_VALUE = "Backend Engineer (Remote)"_COMPOSITE_LABEL = "CA - California"_TYPED_PRE = "California"+# Incident-shaped padded row: the account digits fall after ~90 chars of inter-span whitespace, so the raw+# label overflows the 120-char field cap with the digits last. Synthetic values only.+_PADDED_NAME = "SYNTHETIC WELLNESS GROUP EAP"+_PADDED_DIGITS = "1234567890123"+_PADDED_LABEL = _PADDED_NAME + "\n" + " " * 40 + "\n" + " " * 40 + "\n" + " " * 8 + "Account: " + _PADDED_DIGITS+def _control(tag: str = "input") -> MagicMock:element = MagicMock()@@ -158,6 +165,17 @@ class TestCommitEvidenceGate:# boundary-delimited fragments of the clicked option label rejects them.assert handler._autocomplete_commit_evidence(pre, post, label) is None+ def test_committed_option_collapses_whitespace_so_late_digits_survive_truncation(self) -> None:+ # SKY-6657 F1: a padded row whose account digits fall after ~90 whitespace chars overflows the 120-char+ # cap; the receipt must collapse whitespace before truncation so the full identity survives.+ evidence = handler._autocomplete_commit_evidence(_PADDED_DIGITS, _PADDED_NAME, _PADDED_LABEL)+ assert evidence is not None+ committed_option, committed_value = evidence+ assert committed_option == f"{_PADDED_NAME} Account: {_PADDED_DIGITS}"+ assert _PADDED_DIGITS in committed_option+ assert len(committed_option) <= handler.SELECT_SHADOW_MATCH_FIELD_MAX_CHARS+ assert committed_value == _PADDED_NAME+def test_no_evidence_when_post_equals_pre(self) -> None:assert handler._autocomplete_commit_evidence(_TYPED_PRE, _TYPED_PRE, _COMPOSITE_LABEL) is None@@ -707,3 +725,39 @@ class TestCommitEvidenceSerialization:assert "committed_value" not in history[0]["result"]assert history[1]["result"]["committed_option"] == _OPTION_TEXTassert history[1]["result"]["committed_value"] == _COMMITTED_VALUE+++class TestPaddedReceiptReachesVerifierPrompt:+ """Chromium-less complement for the autocomplete-click producer: the collapsed receipt survives real+ action-history serialization and the real completion-verifier prompt so the account identity stays readable."""++ @pytest.mark.asyncio+ async def test_padded_receipt_identity_survives_history_and_verifier_prompt(self) -> None:+ # The name+digits identity comes only from the receipt (the action's typed text is digits-only and the+ # history projection omits text), so its presence in the rendered prompt proves the receipt survived.+ result = await _run_producer(+ pre_values=[_PADDED_DIGITS, _PADDED_NAME, _PADDED_NAME], option_label=_PADDED_LABEL+ )+ assert isinstance(result, ActionSuccess)+ identity = f"{_PADDED_NAME} Account: {_PADDED_DIGITS}"+ assert result.committed_option == identity++ action = InputTextAction(element_id="AADC", text=_PADDED_DIGITS, reasoning="type the account number")+ history = await _history_for([(action, [result])])+ assert history[0]["result"]["committed_option"] == identity++ from skyvern.forge.prompts import prompt_engine++ rendered = prompt_engine.load_prompt(+ "check-user-goal-with-termination",+ action_history=json.dumps(history),+ navigation_goal="Select the requested account in the dropdown",+ navigation_payload="{}",+ complete_criterion="",+ terminate_criterion="",+ elements="",+ local_datetime="",+ big_goal_context="",+ new_elements_ids="",+ )+ assert identity in rendered # the verifier reads the full identity from the Action History receiptdiff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.pyindex 41bea9a5c..8af566cb0 100644--- a/tests/unit/test_copilot_agent_helpers.py+++ b/tests/unit/test_copilot_agent_helpers.py@@ -11,6 +11,8 @@ from types import SimpleNamespacefrom typing import Any, ClassVarfrom unittest.mock import AsyncMock, MagicMock+import httpx+import litellmimport pytestimport yamlfrom agents import GuardrailFunctionOutput, InputGuardrail@@ -5462,6 +5464,95 @@ class TestCopilotConfig:assert agent_module._is_retriable_llm_error(FakeRateLimitError("rate limit"))+ def test_retriable_llm_error_uses_litellm_midstream_original_exception(self) -> None:+ transient = litellm.APIConnectionError(message="azure", llm_provider="azure", model="test")+ wrapped_transient = litellm.exceptions.MidStreamFallbackError(+ message=str(transient),+ llm_provider="azure",+ model="test",+ original_exception=transient,+ )+ wrapped_transient.__cause__ = httpx.ReadError("")+ wrapped_with_original_as_cause = litellm.exceptions.MidStreamFallbackError(+ message=str(transient),+ llm_provider="azure",+ model="test",+ original_exception=transient,+ )+ wrapped_with_original_as_cause.__cause__ = transient+ permanent = litellm.BadRequestError(+ message="connection error while validating a malformed request",+ llm_provider="azure",+ model="test",+ )+ wrapped_permanent = litellm.exceptions.MidStreamFallbackError(+ message=str(permanent),+ llm_provider="azure",+ model="test",+ original_exception=permanent,+ )++ assert agent_module._is_retriable_llm_error(transient)+ assert agent_module._is_retriable_llm_error(wrapped_transient)+ assert agent_module._is_retriable_llm_error(wrapped_with_original_as_cause)+ assert not agent_module._is_retriable_llm_error(permanent)+ assert not agent_module._is_retriable_llm_error(wrapped_permanent)++ @pytest.mark.parametrize("wrapped", [False, True])+ def test_retriable_llm_error_after_handled_context_overflow(self, wrapped: bool) -> None:+ try:+ raise litellm.ContextWindowExceededError(+ message="context window exceeded", llm_provider="azure", model="test"+ )+ except litellm.ContextWindowExceededError as overflow:+ try:+ raise litellm.APIConnectionError(message="azure", llm_provider="azure", model="test")+ except litellm.APIConnectionError as transient:+ assert transient.__context__ is overflow+ failure = (+ litellm.exceptions.MidStreamFallbackError(+ message=str(transient), llm_provider="azure", model="test", original_exception=transient+ )+ if wrapped+ else transient+ )+ assert agent_module._is_retriable_llm_error(failure)++ def test_retriable_llm_error_handles_a_cyclic_midstream_original_exception(self) -> None:+ wrapped = litellm.exceptions.MidStreamFallbackError(+ message="stream failed",+ llm_provider="azure",+ model="test",+ )+ wrapped.original_exception = wrapped++ assert not agent_module._is_retriable_llm_error(wrapped)++ @pytest.mark.parametrize(+ "permanent",+ [+ pytest.param(+ litellm.AuthenticationError(message="invalid credentials", llm_provider="azure", model="test"),+ id="authentication",+ ),+ pytest.param(+ litellm.ContentPolicyViolationError(+ message="refused by content policy", llm_provider="azure", model="test"+ ),+ id="content-policy",+ ),+ ],+ )+ def test_midstream_wrapper_does_not_promote_permanent_original(self, permanent: BaseException) -> None:+ wrapped = litellm.exceptions.MidStreamFallbackError(+ message=str(permanent),+ llm_provider="azure",+ model="test",+ original_exception=permanent,+ )++ assert not agent_module._is_retriable_llm_error(wrapped)+def test_empty_completion_is_typed_retriable_and_tool_calls_are_attempt_local(self) -> None:ctx = _ctx()error = agent_module._empty_completion_error(@@ -5762,14 +5853,9 @@ class TestCopilotConfig:assert stopped[0]["log_level"] == "info"@pytest.mark.asyncio- async def test_run_copilot_agent_retries_retriable_failure_with_fallback(+ async def test_run_copilot_agent_continues_wrapped_transient_on_shared_session(self, monkeypatch: pytest.MonkeyPatch) -> None:- class FakeRateLimitError(Exception):- pass-- FakeRateLimitError.__module__ = "openai"-class FakeMCPServerManager:def __init__(self, servers):self.active_servers = servers@@ -5788,12 +5874,46 @@ class TestCopilotConfig:resolved_keys.append(key)return f"model-{key}", object(), key, True- run_with_enforcement = AsyncMock(- side_effect=[- FakeRateLimitError("rate limit"),- _fake_run_result({"type": "REPLY", "user_response": "ok"}),- ]- )+ seen_sessions: list[Any] = []+ seen_inputs: list[str | list[Any]] = []++ async def run_with_retained_session(**kwargs: Any) -> Any:+ session = kwargs["session"]+ seen_sessions.append(session)+ seen_inputs.append(kwargs["initial_input"])+ if len(seen_sessions) == 1:+ await session.add_items(+ [+ {"role": "user", "content": "build it"},+ {+ "type": "function_call",+ "call_id": "call_browser_action",+ "name": "browser_action",+ "arguments": "{}",+ },+ {+ "type": "function_call_output",+ "call_id": "call_browser_action",+ "output": "completed once",+ },+ ]+ )+ transient = litellm.APIConnectionError(message="azure", llm_provider="azure", model="test")+ wrapped = litellm.exceptions.MidStreamFallbackError(+ message=str(transient),+ llm_provider="azure",+ model="test",+ original_exception=transient,+ )+ wrapped.__cause__ = httpx.ReadError("")+ raise wrapped++ retained = await session.get_items()+ assert [item.get("type") for item in retained].count("function_call_output") == 1+ assert retained[-1]["output"] == "completed once"+ return _fake_run_result({"type": "REPLY", "user_response": "ok"})++ run_with_enforcement = AsyncMock(side_effect=run_with_retained_session)monkeypatch.setattr("skyvern.forge.sdk.copilot.agent._resolve_live_browser_session_id",@@ -5836,9 +5956,136 @@ class TestCopilotConfig:assert result.user_response == "ok"assert resolved_keys == ["PRIMARY", "SECONDARY"]assert run_with_enforcement.await_count == 2+ assert seen_sessions[0] is seen_sessions[1]+ assert isinstance(seen_inputs[0], str) and seen_inputs[0]+ assert seen_inputs[1] == []for call in run_with_enforcement.await_args_list:assert not getattr(call.kwargs["agent"], "input_guardrails", None)+ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ ("primary_original", "fallback_fails", "expected_attempts"),+ [+ pytest.param(+ litellm.BadRequestError(+ message="connection error while validating a malformed request",+ llm_provider="azure",+ model="test",+ ),+ False,+ 1,+ id="wrapped-permanent",+ ),+ pytest.param(+ litellm.APIConnectionError(message="primary stream failed", llm_provider="azure", model="test"),+ True,+ 2,+ id="fallback-failure",+ ),+ ],+ )+ async def test_wrapped_failure_is_bounded_and_preserves_staged_work(+ self,+ monkeypatch: pytest.MonkeyPatch,+ primary_original: BaseException,+ fallback_fails: bool,+ expected_attempts: int,+ ) -> None:+ class FakeMCPServerManager:+ def __init__(self, servers):+ self.active_servers = servers++ async def __aenter__(self):+ return self++ async def __aexit__(self, exc_type, exc, tb):+ return None++ staged_workflow = MagicMock()+ staged_yaml = "workflow_definition:\n parameters: []\n blocks: []"++ async def restore_proposal(ctx: Any) -> None:+ ctx.has_staged_proposal = True+ ctx.staged_workflow = staged_workflow+ ctx.staged_workflow_yaml = staged_yaml+ ctx.last_workflow = staged_workflow+ ctx.last_workflow_yaml = staged_yaml++ def fake_resolve_model_config(+ _handler: Any, *, copilot_config: Any = None, llm_key_override: str | None = None+ ):+ del copilot_config+ key = llm_key_override or "PRIMARY"+ return f"model-{key}", object(), key, True++ primary = litellm.exceptions.MidStreamFallbackError(+ message=str(primary_original),+ llm_provider="azure",+ model="test",+ original_exception=primary_original,+ )+ failures: list[BaseException] = [primary]+ if fallback_fails:+ fallback_original = litellm.APIConnectionError(+ message="fallback stream failed", llm_provider="azure", model="test"+ )+ failures.append(+ litellm.exceptions.MidStreamFallbackError(+ message=str(fallback_original),+ llm_provider="azure",+ model="test",+ original_exception=fallback_original,+ )+ )+ run_with_enforcement = AsyncMock(side_effect=failures)+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.agent.restore_pending_workflow_proposal",+ restore_proposal,+ )+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.agent._resolve_live_browser_session_id",+ AsyncMock(return_value=None),+ )+ monkeypatch.setattr("agents.mcp.MCPServerManager", FakeMCPServerManager)+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.model_resolver.resolve_model_config",+ fake_resolve_model_config,+ )+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.enforcement.run_with_enforcement",+ run_with_enforcement,+ )++ result = await agent_module.run_copilot_agent(+ stream=MagicMock(),+ organization_id="org-1",+ chat_request=WorkflowCopilotChatRequest(+ message="build it",+ workflow_id="wf-1",+ workflow_permanent_id="wfp-1",+ workflow_copilot_chat_id="chat-1",+ workflow_run_id=None,+ workflow_yaml="",+ browser_session_id=None,+ product_action=None,+ ),+ chat_history=[],+ global_llm_context=None,+ llm_api_handler=SimpleNamespace(llm_key="PRIMARY"),+ raw_secret_safety_handler=AsyncMock(+ return_value={"version": "1", "state": "clean", "handling": "none", "citations": []}+ ),+ api_key="sk-test",+ config=CopilotConfig(fallback_llm_key="SECONDARY"),+ )++ assert run_with_enforcement.await_count == expected_attempts+ assert result.updated_workflow is staged_workflow+ assert result.staged_workflow is staged_workflow+ assert result.has_staged_proposal is True+ assert result.proposal_disposition == "review_untested"+ assert "unexpected issue" in result.user_response+@pytest.mark.asyncio@pytest.mark.parametrize("fallback_enabled", [True, False])async def test_empty_completion_fallback_and_truthful_exhaustion(diff --git a/tests/unit/test_copilot_enforcement_pruning.py b/tests/unit/test_copilot_enforcement_pruning.pyindex edbba4b46..83cb5452f 100644--- a/tests/unit/test_copilot_enforcement_pruning.py+++ b/tests/unit/test_copilot_enforcement_pruning.py@@ -1359,3 +1359,29 @@ def test_a_compacted_output_without_a_packet_gains_no_empty_one() -> None:assert "build_test_packet" not in parsedassert "failing_code_line" not in parsed+++def test_repeated_compaction_preserves_page_evidence_for_fallback() -> None:+ payload = json.dumps(+ {+ "ok": True,+ "data": {+ "source_tool": "inspect_page_for_composition",+ "current_url": "http://localhost/copilot_stream_fallback/",+ "inspected_url": "http://localhost/copilot_stream_fallback/",+ "page_title": "Stream recovery QA",+ "clickable_controls": [+ {+ "text": "Continue to statements",+ "tag": "button",+ "selector_candidates": [{"selector": "#continue", "source": "id"}],+ "identity": {"tag": "button", "role": "button", "label_context": "Continue to statements"},+ }+ ],+ "rendered_text": "discarded raw text " * 500,+ },+ }+ )+ once = _summarize_tool_output(payload)+ assert "page_evidence" in json.loads(once)+ assert _summarize_tool_output(once) == oncediff --git a/tests/unit/test_copilot_run_output_content_gate.py b/tests/unit/test_copilot_run_output_content_gate.pyindex f8bc7be59..8d05db23b 100644--- a/tests/unit/test_copilot_run_output_content_gate.py+++ b/tests/unit/test_copilot_run_output_content_gate.py@@ -1349,7 +1349,10 @@ def test_infrastructure_runner_code_prepends_unrecoverable_tool_error(error_codeassert ctx.last_infrastructure_tool_error == error_code[email protected]("error_code", ["timeout", "user_code_error", "insecure_code_detected"])+ "error_code",+ ["timeout", "user_code_error", "insecure_code_detected", "memory_limit_exceeded"],+)def test_repairable_runner_code_injects_no_unrecoverable_category(error_code: str) -> None:result = _failed_code_block_run_result(error_code)ctx = _ctx(result["data"]["blocks"])@@ -1362,6 +1365,35 @@ def test_repairable_runner_code_injects_no_unrecoverable_category(error_code: stassert ctx.last_infrastructure_tool_error is None+def test_input_reassembly_memory_limit_stays_available_for_copilot_repair() -> None:+ error_code = "parameter_reassembly_memory_limit_exceeded"+ reason = "CodeBlock inputs exhausted the sandbox memory limit before the block started."+ result = _failed_code_block_run_result(error_code)+ result["data"]["blocks"][0]["failure_reason"] = reason+ result["data"]["failure_categories"] = [+ {+ "category": "INFRASTRUCTURE_ERROR",+ "confidence_float": 0.95,+ "reason_code": "secure_codeblock_input_memory_limit",+ "reasoning": "Secure CodeBlock sandbox ran out of memory before executing the block",+ }+ ]+ ctx = _ctx(result["data"]["blocks"])++ _record_run_blocks_result(ctx, result, completion_verification=None)++ assert result["data"]["failure_categories"] == [+ {+ "category": "INFRASTRUCTURE_ERROR",+ "confidence_float": 0.95,+ "reason_code": "secure_codeblock_input_memory_limit",+ "reasoning": "Secure CodeBlock sandbox ran out of memory before executing the block",+ }+ ]+ assert ctx.last_infrastructure_tool_error is None+ assert ctx.last_test_failure_reason == reason++def test_unstructured_failure_prose_leaves_category_consumers_clear() -> None:result = {"ok": False,diff --git a/tests/unit/test_copilot_turn_context.py b/tests/unit/test_copilot_turn_context.pyindex 06077ac7b..6762775c1 100644--- a/tests/unit/test_copilot_turn_context.py+++ b/tests/unit/test_copilot_turn_context.py@@ -6,7 +6,11 @@ from unittest.mock import MagicMockimport pytest-from skyvern.forge.sdk.copilot.agent import _prior_run_debug_text, _store_turn_context_packet_on_context+from skyvern.forge.sdk.copilot.agent import (+ _build_user_context,+ _prior_run_debug_text,+ _store_turn_context_packet_on_context,+)from skyvern.forge.sdk.copilot.context import CopilotContextfrom skyvern.forge.sdk.copilot.request_policy import RequestPolicyfrom skyvern.forge.sdk.copilot.turn_context import TurnContextAssembler, TurnContextInputs@@ -165,6 +169,81 @@ _WORKFLOW_V2 = _WORKFLOW_V1 + (" - block_type: text_prompt\n label: summarize_result\n llm_key: x\n prompt: ok\n")+_EMPTY_WORKFLOW = "workflow_definition:\n parameters: []\n blocks: []\n"+++def _render_runnable_draft_context(+ *,+ user_message: str,+ workflow_yaml: str = _EMPTY_WORKFLOW,+ prior_workflow_yaml: str = _WORKFLOW_V2,+ allow_run_blocks: bool = True,+) -> tuple[str, str | None]:+ packet = TurnContextAssembler().assemble(+ TurnContextInputs(+ request_policy=RequestPolicy(allow_run_blocks=allow_run_blocks),+ user_message=user_message,+ workflow_yaml=workflow_yaml,+ prior_workflow_yaml=prior_workflow_yaml,+ )+ )+ summary = packet.runnable_draft_context.rendered_summary if packet.runnable_draft_context else None+ rendered = _build_user_context(+ workflow_yaml=workflow_yaml,+ chat_history_text="",+ global_llm_context="",+ debug_run_info_text="",+ user_message=user_message,+ runnable_draft_summary=summary or "",+ )+ return rendered, summary+++ "user_message",+ [+ pytest.param("Run the draft again unchanged.", id="rerun"),+ pytest.param("Replace the saved binding with credential B, then continue.", id="replacement"),+ ],+)+def test_runnable_draft_context_is_the_same_factual_packet_for_follow_up_requests(user_message: str) -> None:+ rendered, summary = _render_runnable_draft_context(user_message=user_message)++ assert summary is not None+ assert "uncommitted workflow draft" in summary+ assert "not the current canvas workflow" in summary+ assert "remains runnable by its top-level block labels" in summary+ assert "open_site, summarize_result" in summary+ assert "The user is asking" not in summary+ assert "run_blocks_and_collect_debug" not in summary+ assert "update_and_run_blocks" not in summary+ assert "RUNNABLE UNCOMMITTED DRAFT (not on the canvas):" in rendered+ assert summary in rendered+++ ("workflow_yaml", "prior_workflow_yaml", "allow_run_blocks"),+ [+ pytest.param(_WORKFLOW_V1, _WORKFLOW_V2, True, id="current-canvas"),+ pytest.param(_EMPTY_WORKFLOW, _EMPTY_WORKFLOW, True, id="no-draft"),+ pytest.param(_EMPTY_WORKFLOW, _WORKFLOW_V2, False, id="no-run-authority"),+ ],+)+def test_runnable_draft_context_controls_do_not_project_a_draft_section(+ workflow_yaml: str,+ prior_workflow_yaml: str,+ allow_run_blocks: bool,+) -> None:+ rendered, summary = _render_runnable_draft_context(+ user_message="Continue.",+ workflow_yaml=workflow_yaml,+ prior_workflow_yaml=prior_workflow_yaml,+ allow_run_blocks=allow_run_blocks,+ )++ assert summary is None+ assert "RUNNABLE UNCOMMITTED DRAFT (not on the canvas):" not in rendered+def test_workflow_change_context_reports_user_edit() -> None:packet = TurnContextAssembler().assemble(diff --git a/tests/unit/test_display_capture_bridge.py b/tests/unit/test_display_capture_bridge.pynew file mode 100644index 000000000..3be2959a9--- /dev/null+++ b/tests/unit/test_display_capture_bridge.py@@ -0,0 +1,532 @@+"""Contract tests for the change-driven capture bridge command builders and pipe-write safety.++These guard the exact encode/reliability contract the recorder depends on: single-pass H.264 fragmented+MP4 (no second transcode), wall-clock VFR, bounded keyframe/fragment cadence, flushed packets, NO faststart+(crash-salvage), no-scale when capture==output, and short-write/EINTR-tolerant frame writes.+"""++import pytest++from skyvern.webeye import display_capture_bridge as bridge+++def _ff(no_scale: bool = True, **overrides):+ kwargs = {+ "pix_fmt": "bgra",+ "input_width": 1280 if no_scale else 1920,+ "input_height": 720 if no_scale else 1080,+ "output_width": 1280,+ "output_height": 720,+ "max_fps": 15,+ "crf": 28,+ "keyframe_seconds": 5,+ "output_path": "/tmp/out.mp4",+ }+ kwargs.update(overrides)+ return bridge.build_ffmpeg_command(**kwargs)+++def test_ffmpeg_is_single_pass_h264_fragmented_mp4_no_faststart() -> None:+ cmd = " ".join(_ff(no_scale=True))+ assert "-c:v libx264" in cmd+ assert "-preset ultrafast" in cmd and "-tune zerolatency" in cmd+ assert "-use_wallclock_as_timestamps 1" in cmd # wall-clock timeline+ assert "-fps_mode vfr" in cmd # change-driven VFR, not fixed-rate+ assert "+frag_keyframe" in cmd and "empty_moov" in cmd+ assert "-flush_packets 1" in cmd # fragments hit disk as they close+ assert "faststart" not in cmd # would defer moov to close-time rewrite -> unusable on abrupt kill+ assert cmd.endswith("/tmp/out.mp4")+ assert _ff(no_scale=True).count("-c:v") == 1+++def test_ffmpeg_keyframe_and_crf_are_wired_from_params() -> None:+ joined = " ".join(_ff(crf=40, keyframe_seconds=3))+ assert "-crf 40" in joined+ assert "expr:gte(t,n_forced*3)" in joined+++def test_fragment_flush_is_decoupled_from_keyframe_cadence() -> None:+ # Fixed 1s frag-flush INDEPENDENT of the keyframe interval, so an early per-step read hits a decodable fragment.+ for kf in (1, 3, 5, 30):+ joined = " ".join(_ff(keyframe_seconds=kf))+ assert "-frag_duration 1000000" in joined # fixed 1s flush, regardless of keyframe interval+ assert f"expr:gte(t,n_forced*{kf})" in joined # keyframe cadence preserved (compression unchanged)+++def test_ffmpeg_scale_only_when_capture_differs_from_output() -> None:+ assert "scale=1280:720" not in " ".join(_ff(no_scale=True))+ assert "format=yuv420p" in " ".join(_ff(no_scale=True))+ scaled = " ".join(_ff(no_scale=False))+ assert "scale=1280:720,format=yuv420p" in scaled # 1920x1080 -> 1280x720+++def test_build_bridge_command_targets_the_module_with_geometry_and_profile() -> None:+ argv = bridge.build_bridge_command(+ display=":99",+ output_path="/v/run.mp4",+ input_width=1920,+ input_height=1080,+ output_width=1280,+ output_height=720,+ max_fps=15,+ crf=28,+ keyframe_seconds=5,+ stats_path="/v/run.stats.json",+ python_executable="/usr/bin/python3",+ )+ assert argv[0] == "/usr/bin/python3"+ assert argv[1:3] == ["-m", "skyvern.webeye.display_capture_bridge"]+ joined = " ".join(argv)+ assert "--display :99" in joined+ assert "--size 1920x1080" in joined and "--out-size 1280x720" in joined+ assert "--max-fps 15" in joined and "--crf 28" in joined and "--keyframe-sec 5" in joined+ assert "--out /v/run.mp4" in joined and "--stats /v/run.stats.json" in joined+++def test_write_all_handles_short_writes_and_eintr(monkeypatch: pytest.MonkeyPatch) -> None:+ payload = b"x" * 100+ calls = {"n": 0}++ def fake_write(fd: int, data) -> int:+ calls["n"] += 1+ if calls["n"] == 1:+ raise InterruptedError() # EINTR: must be retried, not counted as progress+ if calls["n"] == 2:+ return 30 # short write+ return len(bytes(data)) # remainder++ monkeypatch.setattr(bridge.os, "write", fake_write)+ written = bridge._write_all(7, payload)+ assert written == 100 # full payload delivered despite EINTR + short write+++def test_write_all_reports_broken_pipe(monkeypatch: pytest.MonkeyPatch) -> None:+ def fake_write(fd: int, data) -> int:+ raise BrokenPipeError()++ monkeypatch.setattr(bridge.os, "write", fake_write)+ assert bridge._write_all(7, b"abc") == -1+++def test_shmat_failure_detection() -> None:+ # shmat returns (void*)-1 on failure; ctypes may surface it as None, 0, or the all-ones mask.+ assert bridge._shmat_failed(None) is True+ assert bridge._shmat_failed(0) is True+ assert bridge._shmat_failed(-1) is True+ assert bridge._shmat_failed(0xFFFFFFFFFFFFFFFF) is True+ assert bridge._shmat_failed(0x7F1234560000) is False # a plausible mapped address+++def test_stride_consistency_guard() -> None:+ assert bridge._stride_consistent(1280 * 4, 1280, 32) is True # deployed depth-24 == 32bpp packed+ assert bridge._stride_consistent(1280 * 3, 1280, 24) is True # packed 24bpp+ assert bridge._stride_consistent(1280 * 4, 1280, 24) is False # padded stride -> fail closed+ assert bridge._stride_consistent(1284 * 3, 1280, 24) is False # row padding -> fail closed+++def test_fatal_exit_codes_are_distinct() -> None:+ codes = [+ bridge.EXIT_OPEN_DISPLAY,+ bridge.EXIT_NO_XDAMAGE,+ bridge.EXIT_UNSUPPORTED_BPP,+ bridge.EXIT_NO_XSHM,+ bridge.EXIT_XSHMCREATEIMAGE,+ bridge.EXIT_SHMGET,+ bridge.EXIT_SHMAT,+ bridge.EXIT_XSHMATTACH,+ bridge.EXIT_READINESS,+ bridge.EXIT_STRIDE_MISMATCH,+ ]+ assert len(set(codes)) == len(codes) # each failure branch is attributable+ assert all(c != 0 for c in codes)+++# Two-phase arm capture (SKY-15466): no frames until the parent arms; the arm captures the ready page as frame zero.+ "arm_consumed,pending_dirty,interval_ok,expected",+ [+ (False, True, True, False),+ (True, True, True, True),+ (True, False, True, False),+ (True, True, False, False),+ ],+ ids=["pre_arm_no_emit", "post_arm_emits", "no_pending", "interval_not_ok"],+)+def test_should_emit_truth_table(arm_consumed: bool, pending_dirty: bool, interval_ok: bool, expected: bool) -> None:+ assert (+ bridge.should_emit(arm_consumed=arm_consumed, pending_dirty=pending_dirty, interval_ok=interval_ok) is expected+ )+++def test_arm_transition_fires_once_then_is_idempotent() -> None:+ assert bridge.arm_transition(armed=True, arm_consumed=False) is True+ assert bridge.arm_transition(armed=True, arm_consumed=True) is False+ assert bridge.arm_transition(armed=False, arm_consumed=False) is False+++def test_terminal_frame_only_when_content_captured() -> None:+ assert bridge.should_write_terminal(0) is False+ assert bridge.should_write_terminal(1) is True+++def test_zero_frame_unlink_and_return_code_truth() -> None:+ # Zero-frame teardown unlinks the header-only output; rc 0 ONLY for an unarmed clean teardown, else nonzero.+ assert bridge.should_unlink_output(0) is True+ assert bridge.should_unlink_output(1) is False+ assert bridge._finalize_return_code(0, None, arm_consumed=False) == 0 # UNARMED + 0 -> clean+ assert bridge._finalize_return_code(0, 0, arm_consumed=True) == 1 # ARMED + 0 -> capture failure+ assert bridge._finalize_return_code(0, None, arm_consumed=True) == 1+ assert bridge._finalize_return_code(5, 0, arm_consumed=True) == 0+ assert bridge._finalize_return_code(5, 1, arm_consumed=True) == 1+ assert bridge._finalize_return_code(5, None, arm_consumed=True) == 1+++def test_unblock_precedes_ffmpeg_spawn_in_source() -> None:+ """S4: the SIGUSR1 handler is installed and unblocked before the ffmpeg Popen, so an early arm can never+ default-kill the bridge and no frame is written before ffmpeg exists."""+ import inspect++ src = inspect.getsource(bridge._run)+ assert "SIG_UNBLOCK" in src and "subprocess.Popen(ff" in src+ assert src.index("SIG_UNBLOCK") < src.index("subprocess.Popen(ff")+ # SIGINT/SIGTERM stop handlers are installed only AFTER the ffmpeg spawn, so a parent death / stop during+ # X/SHM/FFmpeg init keeps default fast termination instead of merely setting a flag while init may hang.+ assert src.index("subprocess.Popen(ff") < src.index("signal.SIGINT")+++def test_ready_ack_follows_setup_and_precedes_loop_in_source() -> None:+ """Positive readiness: the parent accepts the recorder only on the bridge's single stdout READY line, so that+ line must be emitted AFTER the FFmpeg spawn and the SIGINT/SIGTERM handler install, and immediately BEFORE the+ main capture loop — every known pre-loop wedge point cleared first. Diagnostics stay on stderr."""+ import inspect++ src = inspect.getsource(bridge._run)+ assert src.count('print("READY"') == 1 # exactly one stdout ACK write+ ready = src.index('print("READY"')+ assert src.index("subprocess.Popen(ff") < ready # after the FFmpeg spawn+ assert src.index("signal.SIGTERM") < ready # after the graceful-stop signal handlers+ assert ready < src.index("while not stop") # immediately before the main capture loop+ assert "flush=True" in src[ready : ready + 40] and "file=" not in src[ready : ready + 40] # flushed, stdout+++def test_xpending_drain_is_direct_child_of_main_loop_source_wiring() -> None:+ # Source-wiring (Codex P2 3951275783): the XPending drain must be a direct child of the main `while not stop`+ # loop (drained after every select return), NOT nested under `if r`, with XDamageSubtract gated by saw_damage.+ import ast+ import inspect+ import textwrap++ tree = ast.parse(textwrap.dedent(inspect.getsource(bridge._run)))+ loop = next(n for n in ast.walk(tree) if isinstance(n, ast.While) and "not stop" in ast.unparse(n.test))++ def _xpending(n: ast.AST) -> bool:+ return isinstance(n, ast.While) and "XPending" in ast.unparse(n.test)++ assert len([s for s in loop.body if _xpending(s)]) == 1 # one unconditional drain; `if r` nesting drops it to 0+ if_r = [s for s in loop.body if isinstance(s, ast.If) and isinstance(s.test, ast.Name)]+ assert not any(_xpending(d) for s in if_r for d in ast.walk(s)) # no bare `if r:` wraps the drain+ saw = [s for s in loop.body if isinstance(s, ast.If) and "saw_damage" in ast.unparse(s.test)]+ assert len(saw) == 1 and "XDamageSubtract" in ast.unparse(saw[0]) # subtract gated by observed damage+++def test_rescan_that_arms_new_windows_marks_dirty() -> None:+ """Source-coupled WIRING assertion (Codex 3939710637): a periodic rescan that newly arms window(s) must set+ pending_dirty, or a popup/native dialog that painted before the rescan (no later XDamage) is never captured+ by the FPS-coalesced emit. Loop is a ctypes closure, so the wiring is asserted at the source seam."""+ import inspect++ src = inspect.getsource(bridge._run)+ branch = src[src.index("if scan_and_arm():") : src.index("last_rescan = now")]+ assert "pending_dirty = True" in branch+++def test_scan_and_arm_flushes_pending_errors_before_snapshotting() -> None:+ """scan_and_arm drains the prior loop's XDamageSubtract batch (an XSync) BEFORE it snapshots err["n"] and+ calls XDamageCreate, so a since-destroyed window's BadDamage is never charged to a valid window's create."""+ import inspect++ src = inspect.getsource(bridge._run)+ start = src.index("def scan_and_arm")+ body = src[start : src.index("\n fd = X.XConnectionNumber", start)]+ flush = body.index("X.XSync(dpy, 0)")+ assert flush < body.index('before = err["n"]')+ assert flush < body.index("XD.XDamageCreate(")+++def test_pixel_heuristics_are_retired() -> None:+ # Retired pixel/color gate symbols must stay gone (representative function + constant).+ for name in ("should_discard", "_GATE_GRID"):+ assert not hasattr(bridge, name)+++# --- SKY-15708: stale per-window XDamage handle pruning ------------------------------------------------------------+++class _FakeXD:+ """Records XDamageDestroy calls; can simulate an async X error (BadDamage or unrelated) surfaced on the+ NEXT XSync, mirroring how the real server queues a per-request error the swallowing handler bumps."""++ def __init__(self, *, err: dict | None = None, error_code_on_destroy: int | None = None):+ self._err = err+ self._error_code = error_code_on_destroy+ self.destroyed: list[int] = []++ def XDamageDestroy(self, dpy, dh):+ self.destroyed.append(dh)+ if self._err is not None and self._error_code is not None:+ self._err["n"] += 1+ self._err["code"] = self._error_code+++class _FakeXSync:+ def XSync(self, dpy, discard): # the destroy path flushes to surface a queued error; fakes bump on destroy+ return None+++# Requirement 1: prune destroyed AND unmapped child windows by viewability (not mere tree membership), keep root.+++def test_windows_to_prune_drops_non_viewable_children_and_keeps_root() -> None:+ root = 0x1+ # 0x222 = destroyed (absent from viewable), 0x333 = unmapped-but-live (present in tree, not viewable): both prune.+ assert bridge.windows_to_prune({root, 0x111, 0x222, 0x333}, viewable={root, 0x111}, root=root) == [0x222, 0x333]+++def test_windows_to_prune_never_returns_root_even_if_not_viewable() -> None:+ root = 0x1+ assert bridge.windows_to_prune({root, 0x111}, viewable=set(), root=root) == [0x111] # root retained, child pruned+++def test_prune_unviewable_destroys_and_drops_exactly_once_keeping_root_and_viewable() -> None:+ err = {"n": 0, "code": None}+ root = 0x1+ damaged = {root: 100, 0x111: 101, 0x222: 102, 0x333: 103} # root + viewable child + destroyed + unmapped+ xd = _FakeXD()+ pruned = bridge.prune_unviewable_windows(+ xd, _FakeXSync(), dpy=1, damaged=damaged, viewable={root, 0x111}, root=root, err=err, bad_damage_code=152+ )+ assert pruned == 2+ assert damaged == {root: 100, 0x111: 101} # root + viewable child retained; destroyed + unmapped dropped+ assert sorted(xd.destroyed) == [102, 103] # each non-viewable handle destroyed exactly once+++# Requirement 2: a DestroyNotify drops the entry immediately (before any rescan), so a same-XID reuse re-arms fresh.+++def test_reconcile_destroyed_drops_entry_immediately_without_reissuing_destroy() -> None:+ root = 0x1+ damaged = {root: 100, 0x555: 101}+ reconciled = bridge.reconcile_destroyed_window(damaged, 0x555, root)+ assert reconciled is True+ assert 0x555 not in damaged # dropped at DestroyNotify time -> a reused 0x555 fails `w not in damaged` -> re-arms+ assert damaged == {root: 100}+++def test_reconcile_destroyed_is_noop_for_root_and_untracked_windows() -> None:+ root = 0x1+ damaged = {root: 100, 0x555: 101}+ assert bridge.reconcile_destroyed_window(damaged, root, root) is False # never drop the root+ assert bridge.reconcile_destroyed_window(damaged, 0x999, root) is False # untracked -> no-op+ assert damaged == {root: 100, 0x555: 101}+++# Requirement 3: only the exact BadDamage code is tolerated; unknown or unreadable codes keep a scoped diagnostic.+++def test_destroy_owned_handle_tolerates_exact_bad_damage_without_warning(capsys: pytest.CaptureFixture[str]) -> None:+ err = {"n": 0, "code": None}+ xd = _FakeXD(err=err, error_code_on_destroy=152) # server already reaped the window -> BadDamage+ bridge._destroy_owned_handle(xd, _FakeXSync(), 1, 101, 0x222, err, bad_damage_code=152, context="unviewable")+ assert "unexpected X error" not in capsys.readouterr().err # the one tolerated race+++def test_destroy_owned_handle_surfaces_unknown_code(capsys: pytest.CaptureFixture[str]) -> None:+ err = {"n": 0, "code": None}+ xd = _FakeXD(err=err, error_code_on_destroy=9) # unrelated code -> must not be masked+ bridge._destroy_owned_handle(xd, _FakeXSync(), 1, 101, 0x222, err, bad_damage_code=152, context="unviewable")+ assert "unexpected X error" in capsys.readouterr().err+++def test_destroy_owned_handle_surfaces_unreadable_none_code_not_silently_tolerated(+ capsys: pytest.CaptureFixture[str],+) -> None:+ # A struct-read failure leaves code == None; it must be surfaced, NOT silently tolerated as if it were BadDamage.+ err = {"n": 0, "code": None}++ class _ErrBumpNoCode:+ def XDamageDestroy(self, dpy, dh):+ err["n"] += 1 # an error occurred but its code could not be read (stays None)++ bridge._destroy_owned_handle(_ErrBumpNoCode(), _FakeXSync(), 1, 101, 0x222, err, bad_damage_code=152, context="x")+ assert "unexpected X error" in capsys.readouterr().err+++def test_destroy_owned_handle_silent_when_no_error(capsys: pytest.CaptureFixture[str]) -> None:+ err = {"n": 0, "code": None}+ bridge._destroy_owned_handle(_FakeXD(), _FakeXSync(), 1, 101, 0x222, err, bad_damage_code=152, context="x")+ assert capsys.readouterr().err == "" # a clean destroy emits nothing+++def test_destroy_all_handles_destroys_every_owned_handle_once_including_root() -> None:+ damaged = {0x1: 100, 0x111: 101, 0x222: 102} # root + children+ xd = _FakeXD()+ bridge._destroy_all_handles(xd, _FakeXSync(), dpy=1, damaged=damaged)+ assert damaged == {} # nothing left to leak+ assert sorted(xd.destroyed) == [100, 101, 102] # each owned handle destroyed exactly once (pop-before-destroy)+++# Wiring guards for the ctypes `_run` closure (not unit-executable without a real X server; the real-X11 churn e2e+# is the executing contract test). Kept minimal — behavior is covered by the helper tests above.+++def test_bindings_for_map_state_and_destroy_are_declared() -> None:+ import inspect++ src = inspect.getsource(bridge._load_x)+ assert "XD.XDamageDestroy" in src # destroy entrypoint bound+ assert "XGetWindowAttributes" in src # map-state query bound (to prune unmapped windows, not just absent ones)+++def test_scan_and_arm_prunes_by_viewability_before_arming() -> None:+ import inspect++ src = inspect.getsource(bridge._run)+ start = src.index("def scan_and_arm")+ body = src[start : src.index("\n fd = X.XConnectionNumber", start)]+ assert "prune_unviewable_windows" in body # prune destroyed + unmapped before re-arm+ assert body.index("prune_unviewable_windows") < body.index("XD.XDamageCreate(")+++def test_both_xpending_drains_reconcile_destroy_notify() -> None:+ # F1 regression: BOTH event drains — the main loop drain AND the one-shot arm-transition drain — must reconcile+ # DestroyNotify. A destroy queued in the arm window that the arm drain merely discards would leave a stale entry+ # that a same-XID reuse then inherits until the window departs (the exact XID-reuse failure mode).+ import ast+ import inspect+ import textwrap++ tree = ast.parse(textwrap.dedent(inspect.getsource(bridge._run)))+ drains = [n for n in ast.walk(tree) if isinstance(n, ast.While) and "XPending" in ast.unparse(n.test)]+ assert len(drains) == 2 # main-loop drain + arm-transition drain+ for drain in drains:+ assert "reconcile_destroyed_window" in ast.unparse(drain) # every drain path drops destroyed windows+++def test_owned_handles_cleaned_in_finally_for_exceptional_exit() -> None:+ import ast+ import inspect+ import textwrap++ tree = ast.parse(textwrap.dedent(inspect.getsource(bridge._run)))+ tries = [n for n in ast.walk(tree) if isinstance(n, ast.Try) and "_destroy_all_handles" in ast.unparse(n.finalbody)]+ assert tries, "_destroy_all_handles must run in a finally so early/exceptional exits still release owned handles"+++def test_x_error_handler_captures_error_code() -> None:+ import inspect++ src = inspect.getsource(bridge._run)+ assert 'err["code"]' in src # the handler records the error code so BadDamage can be told from unrelated errors+ assert hasattr(bridge, "_XErrorEvent") # a typed error-event struct to read error_code from+++class _FakeLibc:+ def __init__(self, *, shmid: int = 42, shmat_addr: int = 0x7F00_0000):+ self._shmid = shmid+ self._shmat_addr = shmat_addr+ self.calls: list[tuple] = []++ def shmget(self, key, size, flags):+ self.calls.append(("shmget", size))+ return self._shmid++ def shmat(self, shmid, addr, flags):+ self.calls.append(("shmat", shmid))+ return self._shmat_addr++ def shmdt(self, addr):+ self.calls.append(("shmdt", addr))++ def shmctl(self, shmid, cmd, buf):+ self.calls.append(("shmctl", shmid, cmd))+++class _FakeXext:+ def __init__(self, *, getimage_ok: int = 1):+ self._getimage_ok = getimage_ok+ self.calls: list[str] = []++ def XShmAttach(self, dpy, info):+ self.calls.append("XShmAttach")++ def XShmDetach(self, dpy, info):+ self.calls.append("XShmDetach")++ def XShmGetImage(self, dpy, root, image, x, y, planes):+ self.calls.append("XShmGetImage")+ return self._getimage_ok+++class _FakeX:+ def __init__(self, err: dict, *, bump_err_on_sync: bool = False):+ self._err = err+ self._bump = bump_err_on_sync++ def XSync(self, dpy, discard):+ if self._bump:+ self._err["n"] += 1 # simulate an async XShmAttach BadAccess surfacing through the handler+++def _shm_image_stub():+ from types import SimpleNamespace++ return SimpleNamespace(contents=SimpleNamespace(data=None))+++def _run_attach(libc, xe, x, err):+ info = bridge._XShmSegmentInfo() # real ctypes struct so ctypes.byref works+ return bridge._allocate_and_attach_shm(x, xe, libc, 1, 2, _shm_image_stub(), info, 1000, err)+++def test_shm_attach_shmat_failure_reclaims_and_fails_closed() -> None:+ err = {"n": 0}+ libc = _FakeLibc(shmat_addr=0xFFFFFFFFFFFFFFFF) # shmat == (void*)-1+ xe = _FakeXext()+ addr, code = _run_attach(libc, xe, _FakeX(err), err)+ assert addr is None and code == bridge.EXIT_SHMAT+ assert ("shmctl", 42, bridge.IPC_RMID) in libc.calls # created segment reclaimed+ assert "XShmAttach" not in xe.calls # never attached+ assert not any(c[0] == "shmdt" for c in libc.calls) # nothing was mapped to detach+++def test_shm_attach_failure_detaches_and_removes() -> None:+ err = {"n": 0}+ libc = _FakeLibc()+ xe = _FakeXext()+ addr, code = _run_attach(libc, xe, _FakeX(err, bump_err_on_sync=True), err) # XShmAttach errors+ assert addr is None and code == bridge.EXIT_XSHMATTACH+ assert "XShmAttach" in xe.calls+ assert ("shmdt", 0x7F00_0000) in libc.calls+ assert ("shmctl", 42, bridge.IPC_RMID) in libc.calls+++def test_shm_readiness_failure_detaches() -> None:+ err = {"n": 0}+ libc = _FakeLibc()+ xe = _FakeXext(getimage_ok=0) # initial XShmGetImage fails+ addr, code = _run_attach(libc, xe, _FakeX(err), err)+ assert addr is None and code == bridge.EXIT_READINESS+ assert "XShmDetach" in xe.calls+ assert ("shmdt", 0x7F00_0000) in libc.calls+++def test_shm_attach_success_returns_mapped_address() -> None:+ err = {"n": 0}+ libc = _FakeLibc()+ xe = _FakeXext()+ addr, code = _run_attach(libc, xe, _FakeX(err), err)+ assert code == 0 and addr == 0x7F00_0000+ assert xe.calls == ["XShmAttach", "XShmGetImage"] # attached then readiness-probed+ # segment IPC_RMID-marked (freed on detach) but NOT detached while healthy+ assert ("shmctl", 42, bridge.IPC_RMID) in libc.calls+ assert not any(c[0] == "shmdt" for c in libc.calls)diff --git a/tests/unit/test_display_recorder.py b/tests/unit/test_display_recorder.pynew file mode 100644index 000000000..19498e492--- /dev/null+++ b/tests/unit/test_display_recorder.py@@ -0,0 +1,635 @@+"""Contract/lifecycle tests for the exclusive whole-display recorder.++Covers the required product behavior: OFF-path leaves Playwright recording untouched (default); ON-path+suppresses per-page recording and builds the exact X4 bridge command to a single owner-exact ``.mp4``;+narrow profile controls validate/fall back; and ``stop()`` escalation is idempotent and reap-safe.+"""++import asyncio+import os+import signal+import sys+from pathlib import Path++import pytest++from skyvern.config import settings+from skyvern.webeye import display_recorder as dr+++def test_profile_defaults_are_safe(monkeypatch: pytest.MonkeyPatch) -> None:+ p = dr.RecordingProfile.resolve()+ assert (p.output_width, p.output_height) == (1280, 720)+ assert p.max_fps == 15 and p.crf == 28 and p.keyframe_seconds == 5+++ "attr,value,field,expected",+ [+ ("DISPLAY_RECORDING_MAX_FPS", 0, "max_fps", 15), # below range -> default+ ("DISPLAY_RECORDING_MAX_FPS", 999, "max_fps", 15), # above range -> default+ ("DISPLAY_RECORDING_CRF", -1, "crf", 28), # invalid -> default+ ("DISPLAY_RECORDING_CRF", 99, "crf", 28),+ ("DISPLAY_RECORDING_KEYFRAME_SECONDS", 0, "keyframe_seconds", 5),+ ("DISPLAY_RECORDING_OUTPUT_WIDTH", 1281, "output_width", 1280), # odd -> floored even+ ("DISPLAY_RECORDING_OUTPUT_HEIGHT", 99999, "output_height", 720), # > display -> default+ ],+)+def test_profile_invalid_values_fall_back(monkeypatch, attr, value, field, expected) -> None:+ monkeypatch.setattr(settings, attr, value, raising=False)+ p = dr.RecordingProfile.resolve()+ assert getattr(p, field) == expected+++def _args_with_playwright_recording() -> dict:+ return {"record_video_dir": "/tmp/vid", "record_video_size": {"width": 800, "height": 450}, "args": []}+++def _all_on_gate(monkeypatch) -> None:+ monkeypatch.setattr(dr.platform, "system", lambda: "Linux")+ monkeypatch.setattr(settings, "EXCLUSIVE_DISPLAY_RECORDING", True)+ monkeypatch.setattr(settings, "VIDEO_PATH", "/tmp/artifacts")+ monkeypatch.setenv("DISPLAY", ":99")+ monkeypatch.setattr(dr.attach_only, "is_enforcing", lambda: False)+++def _gate_default_off(mp) -> None:+ assert settings.EXCLUSIVE_DISPLAY_RECORDING is False # default OFF+++def _gate_conc1(mp) -> None:+ _all_on_gate(mp)+ mp.setattr(settings, "BROWSER_WORKER_MAX_CONCURRENT_ACTIVITIES", 1, raising=False)+++def _gate_falsy_video(mp) -> None:+ _all_on_gate(mp)+ mp.setattr(settings, "VIDEO_PATH", "") # falsy: recorder can't write+++def _gate_conc2(mp) -> None:+ _all_on_gate(mp)+ mp.setattr(settings, "BROWSER_WORKER_MAX_CONCURRENT_ACTIVITIES", 2, raising=False) # prod browserConcurrency>1+++def _gate_flag_off(mp) -> None:+ # Everything eligible (Linux, DISPLAY, VIDEO_PATH, concurrency 1, owner present); ONLY the default-OFF flag+ # decides. Single-mutation-precise: deleting the EXCLUSIVE_DISPLAY_RECORDING conjunct flips this row to eligible.+ _gate_conc1(mp)+ mp.setattr(settings, "EXCLUSIVE_DISPLAY_RECORDING", False)+++ "setup,owner,expected",+ [+ (_gate_default_off, "wr_123", False),+ (_gate_conc1, "wr_conc1", True),+ (_gate_falsy_video, "wr_novideo", False),+ (_all_on_gate, None, False),+ (_gate_conc2, "wr_conc2", False),+ (_gate_flag_off, "wr_flagoff", False),+ ],+ ids=["default_off", "conc1_eligible", "falsy_video_path", "no_owner", "conc2_declines", "flag_off_declines"],+)+def test_configure_eligibility_gate(monkeypatch, setup, owner, expected) -> None:+ # Eligibility-ONLY (never pops record_video_*); every fail-closed case (falsy VIDEO_PATH, no owner,+ # concurrency != 1) keeps per-page recording so a run never ends with NEITHER recording.+ setup(monkeypatch)+ args = _args_with_playwright_recording()+ kw = {"workflow_run_id": owner} if owner else {}+ assert dr.configure_local_display_recording(args, **kw) is expected+ assert args["record_video_dir"] == "/tmp/vid" # eligibility never pops+ assert args["record_video_size"] == {"width": 800, "height": 450}+++async def test_prepare_pops_playwright_args_only_on_acquired_recorder(monkeypatch) -> None:+ # Success: a live recorder is acquired, so prepare pops record_video_* and seeds one VideoArtifact.+ from skyvern.webeye.browser_artifacts import BrowserArtifacts, VideoArtifact+ from skyvern.webeye.display_recorder import DisplayRecorderAcquisition++ _gate_conc1(monkeypatch) # eligible: all gates on, concurrency 1+ va = VideoArtifact(video_path="/tmp/artifacts/rec.mp4", video_file_extension="mp4")+ fake_recorder = object()++ async def _fake_acquire(*a, **k):+ return DisplayRecorderAcquisition(fake_recorder, va, True)++ monkeypatch.setattr(dr, "acquire_display_recorder", _fake_acquire)+ args = _args_with_playwright_recording()+ artifacts = BrowserArtifacts()+ await dr.prepare_local_display_recording(args, artifacts, workflow_run_id="wr_ok")+ assert "record_video_dir" not in args and "record_video_size" not in args # popped on success+ assert artifacts._display_recorder is fake_recorder+ assert artifacts.video_artifacts == [va] # exactly one recording+ assert artifacts._display_recorder_acquisition is not None+++async def test_prepare_keeps_playwright_args_on_refusal(monkeypatch) -> None:+ # Refusal (e.g. display has a live owner): recorder is None, so prepare must NOT pop record_video_* — the+ # run falls back to Playwright per-page recording.+ from skyvern.webeye.browser_artifacts import BrowserArtifacts+ from skyvern.webeye.display_recorder import DisplayRecorderAcquisition++ _gate_conc1(monkeypatch) # eligible: all gates on, concurrency 1++ async def _refuse(*a, **k):+ return DisplayRecorderAcquisition(None, None, False)++ monkeypatch.setattr(dr, "acquire_display_recorder", _refuse)+ args = _args_with_playwright_recording()+ args[dr.DISPLAY_RECORDING_OUTPUT_BOUND_KEY] = {"width": 1920, "height": 1080}+ artifacts = BrowserArtifacts()+ await dr.prepare_local_display_recording(args, artifacts, workflow_run_id="wr_refused")+ assert artifacts.local_display_recording_eligible is True # was eligible...+ assert args["record_video_dir"] == "/tmp/vid" # ...but refusal leaves Playwright recording intact+ assert args["record_video_size"] == {"width": 800, "height": 450}+ # The carrier is consumed even on the eligible-but-refused path, so it never leaks into launch kwargs+ # on a run that falls back to Playwright per-page recording.+ assert dr.DISPLAY_RECORDING_OUTPUT_BOUND_KEY not in args+ assert artifacts._display_recorder is None+ assert artifacts._display_recorder_acquisition is None+++def test_build_capture_command_emits_single_owner_exact_mp4() -> None:+ cmd, out = dr.build_capture_command(":99", "wr_abc", Path("/video"))+ assert out.suffix == ".mp4"+ assert out.name.startswith("wr_abc-") # owner-derived, collision-safe digest+ joined = " ".join(cmd)+ assert "skyvern.webeye.display_capture_bridge" in joined+ assert "--out-size 1280x720" in joined+ assert str(out) in joined+++def test_acquired_video_artifact_advertises_mp4_before_first_artifact_creation(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ # The recorder always writes a fragmented MP4, so the seeded VideoArtifact must advertise ``mp4`` up front,+ # or the step-0 artifact uri defaults to ``.webm`` for H.264 bytes.+ if dr.fcntl is None:+ pytest.skip("fcntl unavailable on this platform")++ async def _fake_exec(*_args: object, **_kwargs: object) -> "_FakeProc":+ return _FakeProc(exits_on="never") # emits the default READY ack, stays live -> healthy startup++ monkeypatch.setattr(dr, "STARTUP_ACK_TIMEOUT", 0.5)+ monkeypatch.setattr(dr.os, "open", lambda *a, **k: 7)+ monkeypatch.setattr(dr.fcntl, "flock", lambda *a, **k: None)+ monkeypatch.setattr(dr.asyncio, "create_subprocess_exec", _fake_exec)++ async def run() -> dr.DisplayRecorderAcquisition:+ return await dr.acquire_display_recorder(":99", "wr_ext", tmp_path)++ try:+ acq = asyncio.run(run())+ assert acq.started is True+ assert acq.video_artifact is not None+ assert acq.video_artifact.video_file_extension == "mp4"+ assert (acq.video_artifact.video_path or "").endswith(".mp4")+ finally:+ dr._REGISTRY.pop((":99", "wr_ext"), None)+++def test_owner_id_is_deterministic_and_owner_exact() -> None:+ a = dr._safe_owner_id("wr_abc")+ assert a == dr._safe_owner_id("wr_abc") # deterministic+ assert a != dr._safe_owner_id("wr_abd") # different owner -> different filename+++def test_capture_sizes_scale_from_window_and_fall_back_when_invalid() -> None:+ (iw, ih), (ow, oh) = dr.resolve_display_capture_sizes({"args": ["--window-size=1600,900"]})+ assert (iw, ih) == (1600, 900)+ assert (ow, oh) == (1280, 720) # aspect-preserved fit, even+ (iw2, ih2), _ = dr.resolve_display_capture_sizes({"args": ["--window-size=99999,99999"]})+ assert (iw2, ih2) == dr.CAPTURE_DISPLAY_SIZE+++def test_capture_sizes_honor_raw_output_bound_not_capped_playwright_size() -> None:+ # 1000x700 window + raw 1080p bound: the window fits the raw bound whole, so output == window.+ # Passing the viewport-CAPPED Playwright size (1000x562) as the bound instead fits the window a+ # SECOND time and corrupts it to 802x562 — the double-aspect-fit defect the raw carrier prevents.+ args = {"args": ["--window-size=1000,700"]}+ (iw, ih), (ow, oh) = dr.resolve_display_capture_sizes(args, output_bound={"width": 1920, "height": 1080})+ assert (iw, ih) == (1000, 700)+ assert (ow, oh) == (1000, 700)+ _, corrupted = dr.resolve_display_capture_sizes(args, output_bound={"width": 1000, "height": 562})+ assert corrupted == (802, 562) # what using the capped Playwright size would produce (never used here)+++def test_capture_sizes_fit_raw_profile_cross_aspect() -> None:+ # 800x600 (4:3) window + raw 480p (854x480) bound: largest even 4:3 fitting the profile, no pad/crop.+ (_input, output) = dr.resolve_display_capture_sizes(+ {"args": ["--window-size=800,600"]}, output_bound={"width": 854, "height": 480}+ )+ assert output == (640, 480)+++def test_capture_sizes_reject_malformed_or_mixed_validity_bound_atomically() -> None:+ # Atomic PAIR validation: any absent / non-dict / partial / non-int / bool / out-of-bounds axis+ # falls back to the COMPLETE static profile pair (1280x720), never mixing a valid carrier axis+ # with a profile axis. Proven discriminating by a 1900x300 window: per-axis mixing would let the+ # width-valid cases leak through as 1900x300 (carrier width 1920 + profile height 720); the atomic+ # fallback yields 1280x202 for every malformed input.+ args = {"args": ["--window-size=1900,300"]}+ for bad in (+ None,+ 123,+ "1280x720",+ {},+ {"width": 1920}, # height missing+ {"height": 1080}, # width missing+ {"width": 1920, "height": "1080"}, # height non-int+ {"width": "x", "height": "y"}, # both non-int+ {"width": True, "height": 1080}, # bool is not a real dimension+ {"width": 1920, "height": 99999}, # height out of display bounds+ {"width": 0, "height": 1080}, # width below the even minimum+ ):+ (_input, output) = dr.resolve_display_capture_sizes(args, output_bound=bad)+ assert output == (1280, 202)+++async def test_prepare_consumes_output_bound_carrier_into_capture_sizes(monkeypatch) -> None:+ # Eligible/acquired path: the raw-output-bound carrier is REMOVED from browser_args (never reaching+ # launch kwargs) and freezes the capture output. A 1000x900 window under a raw 1080p bound yields+ # output 1000x900; the static 720p default would instead shrink it to 800x720, so this fails if the+ # carrier is dropped, resolved before the pop, or replaced by the capped Playwright size.+ from skyvern.webeye.browser_artifacts import BrowserArtifacts, VideoArtifact+ from skyvern.webeye.display_recorder import DisplayRecorderAcquisition++ _gate_conc1(monkeypatch)+ va = VideoArtifact(video_path="/tmp/artifacts/rec.mp4", video_file_extension="mp4")++ async def _fake_acquire(*a, **k):+ return DisplayRecorderAcquisition(object(), va, True)++ monkeypatch.setattr(dr, "acquire_display_recorder", _fake_acquire)+ args = {+ "record_video_dir": "/tmp/vid",+ "record_video_size": {"width": 1000, "height": 562}, # the viewport-capped Playwright size+ "args": ["--window-size=1000,900"],+ dr.DISPLAY_RECORDING_OUTPUT_BOUND_KEY: {"width": 1920, "height": 1080}, # raw 1080p selection+ }+ artifacts = BrowserArtifacts()+ await dr.prepare_local_display_recording(args, artifacts, workflow_run_id="wr_bound")+ assert dr.DISPLAY_RECORDING_OUTPUT_BOUND_KEY not in args # consumed + removed before launch+ assert artifacts._display_capture_sizes == ((1000, 900), (1000, 900)) # raw bound, not capped/static+++async def test_prepare_pops_output_bound_carrier_even_when_ineligible(monkeypatch) -> None:+ # Ineligible (flag off): the carrier is consumed BEFORE the eligibility short-circuit so it can never+ # leak into the browser launch kwargs on a run that keeps Playwright per-page recording.+ from skyvern.webeye.browser_artifacts import BrowserArtifacts++ _gate_flag_off(monkeypatch)+ args = {+ "record_video_dir": "/tmp/vid",+ "record_video_size": {"width": 800, "height": 450},+ "args": ["--window-size=1000,700"],+ dr.DISPLAY_RECORDING_OUTPUT_BOUND_KEY: {"width": 1920, "height": 1080},+ }+ artifacts = BrowserArtifacts()+ await dr.prepare_local_display_recording(args, artifacts, workflow_run_id="wr_ineligible")+ assert artifacts.local_display_recording_eligible is False+ assert dr.DISPLAY_RECORDING_OUTPUT_BOUND_KEY not in args # removed even on the ineligible path+ assert args["record_video_dir"] == "/tmp/vid" # Playwright recording intact+++def test_resolve_owner_id_prefers_canonical_over_override() -> None:+ assert dr.resolve_owner_id(owner_id_override="ovr", task_id="tsk_1") == "tsk_1"+ assert dr.resolve_owner_id(owner_id_override="ovr") == "ovr"+++class _FakeStdout:+ """Models the bridge's readiness pipe: one line, or "wedge" to block forever (parent readline times out)."""++ def __init__(self, ack: bytes | str) -> None:+ self._ack = ack++ async def readline(self) -> bytes:+ if self._ack == "wedge":+ while True:+ await asyncio.sleep(0.005) # never returns: wait_for(readline) times out or is cancelled+ return self._ack # type: ignore[return-value]+++class _FakeProc:+ """Controllable asyncio-process double: real stop() escalation logic runs against it."""++ def __init__(+ self,+ *,+ exits_on: str,+ exit_code: int = 0,+ ack: bytes | str = b"READY\n",+ ack_returncode: int | None = None,+ ) -> None:+ self.returncode: int | None = ack_returncode # already-exited child when set before the readiness read+ self._exits_on = exits_on # "SIGINT" | "SIGTERM" | "never"+ self._exit_code = exit_code+ self.signals: list[int] = []+ self.killed = False+ self.pid = 2_147_483_646 # non-existent pid: getpgid fails -> _kill_process_group falls back to kill()+ self.stdout = _FakeStdout(ack)++ def send_signal(self, sig: int) -> None:+ self.signals.append(sig)+ if self._exits_on == "SIGINT" and sig == signal.SIGINT:+ self.returncode = self._exit_code++ def terminate(self) -> None:+ self.signals.append(signal.SIGTERM)+ if self._exits_on == "SIGTERM":+ self.returncode = self._exit_code++ def kill(self) -> None:+ self.killed = True+ self.returncode = -9++ async def wait(self) -> int:+ # Block while returncode is None so wait_for(timeout) times out during SIGINT/SIGTERM escalation.+ while self.returncode is None:+ await asyncio.sleep(0.005)+ return self.returncode+++def _recorder(proc: _FakeProc) -> dr.DisplayRecorder:+ from skyvern.webeye.browser_artifacts import VideoArtifact++ return dr.DisplayRecorder(+ display=":99",+ owner_id="wr_x",+ process=proc,+ lock_fd=-1,+ video_artifact=VideoArtifact(video_path="/video/wr_x.mp4"),+ )+++async def test_stop_graceful_on_sigint() -> None:+ proc = _FakeProc(exits_on="SIGINT")+ rec = _recorder(proc)+ assert await rec.stop() is True+ assert signal.SIGINT in proc.signals+ assert not proc.killed+ assert rec.is_stopped+++async def test_stop_reports_nonzero_bridge_exit_as_not_graceful() -> None:+ # A prompt SIGINT exit with a NONZERO code (failed finalize) must report not-graceful, not masked as success.+ proc = _FakeProc(exits_on="SIGINT", exit_code=1)+ rec = _recorder(proc)+ assert await rec.stop() is False+ assert signal.SIGINT in proc.signals+ assert not proc.killed # it DID exit; escalation should not have force-killed+++async def test_stop_is_idempotent() -> None:+ proc = _FakeProc(exits_on="SIGINT")+ rec = _recorder(proc)+ first = await rec.stop()+ n_signals = len(proc.signals)+ second = await rec.stop() # must short-circuit, not re-signal+ assert first == second+ assert len(proc.signals) == n_signals+++async def test_stop_owner_sweep_finishes_then_reraises_cancellation(monkeypatch) -> None:+ # Finish-then-re-raise (mirrors stop()/_reap/_reclaim): a per-owner release that COMPLETES then raises+ # CancelledError must neither abort nor swallow the sweep — the remaining entry is still released and the+ # caller's cancellation surfaces after the sweep, not silently discarded.+ r1 = _recorder(_FakeProc(exits_on="never"))+ r2 = _recorder(_FakeProc(exits_on="never"))+ r2.display = ":100"+ dr._REGISTRY[(":99", "wr_x")] = r1+ dr._REGISTRY[(":100", "wr_x")] = r2++ async def _fake_release(rec: dr.DisplayRecorder) -> bool:+ rec._stop_result = True # the shielded stop completed+ dr._REGISTRY.pop((rec.display, rec.owner_id), None) # ...and deregistered+ if rec is r1:+ raise asyncio.CancelledError() # ...then the caller's cancel re-raises after the finish+ return True++ monkeypatch.setattr(dr, "release_display_recorder", _fake_release)+ try:+ with pytest.raises(asyncio.CancelledError):+ await dr.stop_display_recorders_for_owner("wr_x")+ assert not any(key[1] == "wr_x" for key in dr._REGISTRY) # BOTH released despite the mid-sweep cancel+ finally:+ dr._REGISTRY.pop((":99", "wr_x"), None)+ dr._REGISTRY.pop((":100", "wr_x"), None)+++# Leader: force-kill the whole GROUP (bridge + ffmpeg child) so ffmpeg never orphans. Non-leader: NEVER killpg+# (would hit the worker's own group) — fall back to a single-process kill. Both branches stay distinct rows.+ "getpgid_of,expected_killpg,expected_kill",+ [(lambda pid: pid, 1, 0), (lambda pid: 1, 0, 1)],+ ids=["bridge_is_leader_group_kill", "not_leader_falls_back"],+)+def test_kill_process_group_leader_vs_fallback(monkeypatch, getpgid_of, expected_killpg, expected_kill) -> None:+ proc = _FakeProc(exits_on="never")+ proc.pid = 4321+ calls = {"killpg": [], "kill": 0}+ monkeypatch.setattr(dr.os, "getpgid", getpgid_of)+ monkeypatch.setattr(dr.os, "killpg", lambda pgid, sig: calls["killpg"].append((pgid, sig)))+ monkeypatch.setattr(proc, "kill", lambda: calls.__setitem__("kill", calls["kill"] + 1))+ dr._kill_process_group(proc)+ assert len(calls["killpg"]) == expected_killpg+ assert calls["kill"] == expected_kill+ if expected_killpg:+ assert calls["killpg"] == [(4321, signal.SIGKILL)] # SIGKILL to the group, not a single process+++async def test_stop_escalates_to_kill_when_process_never_exits(monkeypatch) -> None:+ monkeypatch.setattr(dr, "SIGINT_TIMEOUT", 0.05) # shrink escalation timeouts so the test is fast+ monkeypatch.setattr(dr, "SIGTERM_TIMEOUT", 0.05)+ proc = _FakeProc(exits_on="never")+ rec = _recorder(proc)+ assert await rec.stop() is False+ assert proc.killed # SIGINT -> SIGTERM -> kill escalation reached the reap+++def test_arm_capture_sends_sigusr1_exactly_once() -> None:+ proc = _FakeProc(exits_on="never")+ rec = _recorder(proc)+ assert rec.arm_capture() is True+ assert proc.signals == [signal.SIGUSR1]+ assert rec.arm_capture() is True # idempotent: no second delivery+ assert proc.signals == [signal.SIGUSR1]+++def test_arm_capture_noops_on_exited_bridge() -> None:+ proc = _FakeProc(exits_on="never")+ proc.returncode = 0 # bridge already gone+ rec = _recorder(proc)+ assert rec.arm_capture() is False+ assert proc.signals == []+++def test_arm_capture_failed_send_does_not_latch_and_retries() -> None:+ class _RaiseOnce:+ def __init__(self) -> None:+ self.returncode: int | None = None+ self.calls = 0++ def send_signal(self, sig: int) -> None:+ self.calls += 1+ if self.calls == 1:+ raise OSError("transient")++ proc = _RaiseOnce()+ rec = _recorder(proc) # type: ignore[arg-type]+ assert rec.arm_capture() is False # first send raised -> not armed+ assert rec._armed is False+ assert rec.arm_capture() is True # retry succeeds+ assert proc.calls == 2+ assert rec._armed is True+++def test_arm_capture_is_ownership_neutral() -> None:+ proc = _FakeProc(exits_on="never")+ rec = _recorder(proc)+ before_lock = rec.lock_fd+ before_registry = dict(dr._REGISTRY)+ rec.arm_capture()+ assert rec.lock_fd == before_lock # no flock change+ assert dict(dr._REGISTRY) == before_registry # no registry / ownership change+++def test_trampoline_blocks_sigusr1_before_exec() -> None:+ """S4: the PDEATHSIG trampoline must block SIGUSR1 before execvp so an arm during the exec window stays+ pending (delivered after the bridge installs its handler) rather than default-killing the bridge."""+ src = dr._PDEATHSIG_TRAMPOLINE+ assert "pthread_sigmask" in src and "SIG_BLOCK" in src and "SIGUSR1" in src+ assert src.index("SIG_BLOCK") < src.index("execvp")+++def _install_acquire_stubs(monkeypatch, proc: _FakeProc, closed: list[int]) -> None:+ monkeypatch.setattr(dr.os, "open", lambda *a, **k: 7)+ monkeypatch.setattr(dr.fcntl, "flock", lambda *a, **k: None)+ monkeypatch.setattr(dr.os, "close", lambda fd: closed.append(fd))++ async def _fake_exec(*_a: object, **_k: object) -> _FakeProc:+ return proc++ monkeypatch.setattr(dr.asyncio, "create_subprocess_exec", _fake_exec)+++ "ack, ack_returncode, expect_acquired, expect_killed",+ [+ (b"READY\n", None, True, False), # exact READY, live child -> acquired, Playwright args popped+ (b"", 1, False, False), # EOF + nonzero rc (early exit pre-ACK) -> refusal, dead child not re-killed+ (b"garbage\n", None, False, True), # wrong line, live child -> refusal + group kill+ (b"READY\n", 0, False, False), # READY but already exited (live-child guard, R3) -> refusal, not killed+ ("wedge", None, False, True), # no line before the deadline (wedged pre-loop) -> refusal + group kill+ ],+ ids=["ready", "early_exit_eof", "wrong_line", "exit_after_ack", "wedged_timeout"],+)+async def test_acquire_readiness_gates_playwright_fallback(+ monkeypatch, tmp_path, ack, ack_returncode, expect_acquired, expect_killed+) -> None:+ # One outcome table over the READY-pipe protocol, asserted at the prepare entry point so the Playwright-args+ # outcome is pinned: only an exact READY line from a still-live child acquires (args popped); every other+ # outcome fails closed (args intact, nothing registered, lock released, a live child force-killed).+ if dr.fcntl is None:+ pytest.skip("fcntl unavailable on this platform")+ from skyvern.webeye.browser_artifacts import BrowserArtifacts++ _gate_conc1(monkeypatch) # eligible: Linux, DISPLAY=:99, concurrency 1+ monkeypatch.setattr(settings, "VIDEO_PATH", str(tmp_path))+ monkeypatch.setattr(dr, "STARTUP_ACK_TIMEOUT", 0.1) # wedged case pays this once+ closed: list[int] = []+ proc = _FakeProc(exits_on="never", ack=ack, ack_returncode=ack_returncode)+ _install_acquire_stubs(monkeypatch, proc, closed)++ args = _args_with_playwright_recording()+ artifacts = BrowserArtifacts()+ key = (":99", "wr_ready")+ try:+ await dr.prepare_local_display_recording(args, artifacts, workflow_run_id="wr_ready")+ if expect_acquired:+ assert "record_video_dir" not in args and "record_video_size" not in args # popped: recorder owns MP4+ assert artifacts._display_recorder is not None+ assert dr._REGISTRY.get(key) is not None+ else:+ assert args["record_video_dir"] == "/tmp/vid" # Playwright per-page recording preserved+ assert artifacts._display_recorder is None+ assert key not in dr._REGISTRY # nothing registered on refusal+ assert 7 in closed # lock fd released+ assert proc.killed is expect_killed # live child force-killed; an already-exited child is not+ finally:+ dr._REGISTRY.pop(key, None)+++async def test_acquire_cancel_during_readiness_reraises_after_cleanup(monkeypatch, tmp_path) -> None:+ # A cancel delivered while awaiting the READY line flows through the existing BaseException path: kill the+ # group, reap, close the lock, then re-raise CancelledError. No recorder is registered.+ if dr.fcntl is None:+ pytest.skip("fcntl unavailable on this platform")+ closed: list[int] = []+ proc = _FakeProc(exits_on="never", ack="wedge")+ _install_acquire_stubs(monkeypatch, proc, closed)++ task = asyncio.ensure_future(dr.acquire_display_recorder(":99", "wr_cancel", tmp_path))+ await asyncio.sleep(0.02) # let it reach the readline await+ task.cancel()+ with pytest.raises(asyncio.CancelledError):+ await task+ assert 7 in closed # lock released+ assert proc.killed is True # child force-killed and reaped+ assert (":99", "wr_cancel") not in dr._REGISTRY+++async def test_acquire_timeout_reap_swallowed_cancel_reraises(monkeypatch, tmp_path) -> None:+ # If a cancel is swallowed DURING the post-timeout kill-reap, acquire must still re-raise CancelledError+ # (cancellation wins) rather than returning a fallback — after closing the lock.+ if dr.fcntl is None:+ pytest.skip("fcntl unavailable on this platform")+ closed: list[int] = []+ proc = _FakeProc(exits_on="never", ack="wedge")+ _install_acquire_stubs(monkeypatch, proc, closed)+ monkeypatch.setattr(dr, "STARTUP_ACK_TIMEOUT", 0.05)++ async def _reap_swallows_cancel(_p: object) -> bool:+ return True # a cancel landed during the reap and was absorbed by the finish-then-reraise contract++ monkeypatch.setattr(dr, "_reap_surviving_cancellation", _reap_swallows_cancel)++ with pytest.raises(asyncio.CancelledError):+ await dr.acquire_display_recorder(":99", "wr_reapcancel", tmp_path)+ assert 7 in closed+ assert (":99", "wr_reapcancel") not in dr._REGISTRY+++async def test_acquire_kills_real_wedged_subprocess_and_falls_back(monkeypatch, tmp_path) -> None:+ # Real OS process that never prints READY: acquire must time out, kill the process group, reap, and refuse+ # (no recorder), proving the group-kill + reap works against a live child, not just the fake double.+ if dr.fcntl is None or os.name != "posix":+ pytest.skip("posix + fcntl required")+ monkeypatch.setattr(dr, "STARTUP_ACK_TIMEOUT", 0.3)++ def _wedged_cmd(*_a: object, **_k: object) -> tuple[list[str], Path]:+ return [sys.executable, "-c", "import time; time.sleep(30)"], tmp_path / "wr_real.mp4"++ monkeypatch.setattr(dr, "build_capture_command", _wedged_cmd)++ key = (":99", "wr_real")+ try:+ acq = await dr.acquire_display_recorder(":99", "wr_real", tmp_path)+ assert acq.recorder is None and acq.started is False # wedged real child -> refusal+ assert key not in dr._REGISTRY+ finally:+ dr._REGISTRY.pop(key, None)diff --git a/tests/unit/test_jinja.py b/tests/unit/test_jinja.pyindex b226b3395..dc6b68e70 100644--- a/tests/unit/test_jinja.py+++ b/tests/unit/test_jinja.py@@ -1,17 +1,18 @@from __future__ import annotationsfrom datetime import datetime-from unittest.mock import MagicMock+from unittest.mock import AsyncMock, MagicMock, patchimport pytest-from skyvern.forge.sdk.workflow.exceptions import FailedToFormatJinjaStyleParameter+from skyvern.forge.sdk.workflow.exceptions import CodeBlockTemplateSyntaxError, FailedToFormatJinjaStyleParameterfrom skyvern.forge.sdk.workflow.models._jinja import (mask_jinja_in_python_comments,restore_jinja_masked_comments,)from skyvern.forge.sdk.workflow.models.block import CodeBlockfrom skyvern.forge.sdk.workflow.models.parameter import OutputParameter, ParameterType+from skyvern.schemas.workflows import BlockResult, BlockStatusdef _round_trip(source: str) -> str:@@ -143,3 +144,45 @@ def test_code_block_still_raises_when_the_executable_source_is_unrenderable() ->with pytest.raises(FailedToFormatJinjaStyleParameter):block.format_potential_template_parameters(_mock_context({}))+++def test_code_block_template_validation_reports_the_executable_source_line() -> None:+ block = _code_block("# {{ }} is inert\nvalue = 1\nraw = r'''{{ }}'''\n")++ with pytest.raises(CodeBlockTemplateSyntaxError) as excinfo:+ block.validate_code_template()++ assert excinfo.value.block_label == "prefill"+ assert excinfo.value.line == 3+++def test_code_block_template_validation_ignores_jinja_delimiters_in_comments() -> None:+ block = _code_block("# code blocks use {{ }} templating\nvalue = '{{ email }}'\n")++ block.validate_code_template()+++async def test_code_block_marks_runtime_template_failure_as_nonrecoverable() -> None:+ block = _code_block("value = {{ }}")+ browser_state = MagicMock()+ browser_state.get_working_page = AsyncMock(return_value=MagicMock())++ async def failure_result(*args: object, **kwargs: object) -> BlockResult:+ return BlockResult(+ success=False,+ output_parameter=block.output_parameter,+ status=BlockStatus.failed,+ can_continue_after_failure=bool(kwargs["can_continue_after_failure"]),+ )++ with (+ patch("skyvern.forge.sdk.workflow.models.block.app.AGENT_FUNCTION.validate_code_block", new_callable=AsyncMock),+ patch.object(CodeBlock, "get_workflow_run_context", return_value=_mock_context({})),+ patch.object(CodeBlock, "get_or_create_browser_state", new_callable=AsyncMock, return_value=browser_state),+ patch.object(CodeBlock, "_ensure_run_recording_artifact", new_callable=AsyncMock),+ patch.object(CodeBlock, "_template_format_failure_result", side_effect=failure_result),+ ):+ result = await block._execute("wr_test", "wrb_test", organization_id="org_test")++ assert result.can_continue_after_failure is Falsediff --git a/tests/unit/test_loop_next_loop_on_failure_swallow.py b/tests/unit/test_loop_next_loop_on_failure_swallow.pyindex 6ba972442..6763fb541 100644--- a/tests/unit/test_loop_next_loop_on_failure_swallow.py+++ b/tests/unit/test_loop_next_loop_on_failure_swallow.py@@ -13,6 +13,7 @@ import pytestfrom skyvern.forge.sdk.workflow.models.block import (Block,+ CodeBlock,FileParserBlock,ForLoopBlock,JinjaBranchCriteria,@@ -657,6 +658,50 @@ class TestLoopBlockExecutedResultIsCompletedRespectsNaturalCompletion:class TestLoopBlockExecutedResultIsCompletedWithNaturalCompletion:+ def test_empty_loop_result_does_not_crash_with_parent_swallow_flag(self) -> None:+ result = LoopBlockExecutedResult(+ outputs_with_loop_values=[],+ block_outputs=[],+ last_block=None,+ natural_completion=True,+ )++ assert result.resolve_status(parent_next_loop_on_failure=True) == (+ BlockStatus.completed,+ True,+ None,+ )++ def test_nonrecoverable_failure_is_not_swallowed_by_any_loop_flag(self) -> None:+ inner = NavigationBlock(+ label="inner",+ output_parameter=_make_output_param("inner"),+ url="https://example.com",+ navigation_goal="g",+ continue_on_failure=True,+ next_loop_on_failure=True,+ )+ failure = BlockResult(+ success=False,+ output_parameter=inner.output_parameter,+ status=BlockStatus.failed,+ failure_reason="template render failed",+ can_continue_after_failure=False,+ )+ result = LoopBlockExecutedResult(+ outputs_with_loop_values=[],+ block_outputs=[failure],+ last_block=inner,+ natural_completion=True,+ )++ assert result.is_completed() is False+ assert result.resolve_status(parent_next_loop_on_failure=True) == (+ BlockStatus.failed,+ False,+ "template render failed",+ )+def test_is_completed_true_when_natural_completion_and_inner_next_loop_on_failure(self) -> None:inner = NavigationBlock(label="inner",@@ -774,6 +819,61 @@ class TestForLoopSkipsUnparseableFile:assert success is Trueassert failure_reason is None+ @pytest.mark.asyncio+ async def test_nonrecoverable_code_failure_stops_all_loop_continuation(self) -> None:+ code = CodeBlock(+ label="render_code",+ code="pass",+ output_parameter=_make_output_param("render_code"),+ continue_on_failure=True,+ next_loop_on_failure=True,+ )+ after = TaskBlock(label="after", output_parameter=_make_output_param("after"))+ loop_block = ForLoopBlock(+ label="each_item",+ output_parameter=_make_output_param("each_item"),+ loop_blocks=[code, after],+ next_loop_on_failure=True,+ )+ executed: list[tuple[str, str]] = []++ async def fake_execute_safe(block: Block, **kwargs: Any) -> BlockResult:+ loop_value = kwargs["current_value"]+ executed.append((block.label, loop_value))+ return BlockResult(+ success=False,+ output_parameter=block.output_parameter,+ status=BlockStatus.failed,+ failure_reason="template render failed",+ can_continue_after_failure=False,+ )++ workflow_run_context = MagicMock()+ workflow_run_context.has_value.return_value = False++ with (+ patch.object(Block, "execute_safe", autospec=True, side_effect=fake_execute_safe),+ patch.object(CodeBlock, "execute_safe", autospec=True, side_effect=fake_execute_safe),+ patch.object(ForLoopBlock, "get_loop_block_context_parameters", return_value=[]),+ patch.object(Block, "record_output_parameter_value", new_callable=AsyncMock),+ patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app,+ patch("skyvern.forge.sdk.workflow.models.block.skyvern_context") as mock_skyvern_ctx,+ ):+ mock_skyvern_ctx.current.return_value = None+ mock_app.DATABASE.workflow_runs.create_or_update_workflow_run_output_parameter = AsyncMock()+ mock_app.DATABASE.observer.update_workflow_run_block = AsyncMock()+ result = await loop_block.execute_loop_helper(+ workflow_run_id="wr_test",+ workflow_run_block_id="wrb_loop",+ workflow_run_context=workflow_run_context,+ loop_over_values=["first", "second"],+ organization_id="org_test",+ )++ assert executed == [("render_code", "first")]+ assert result.natural_completion is False+ assert result.can_continue_after_failure() is False+@pytest.mark.asyncioasync def test_without_the_flag_the_first_unparseable_file_stops_the_loop(self) -> None:parse_file = FileParserBlock(diff --git a/tests/unit/test_proxy_url_custom.py b/tests/unit/test_proxy_url_custom.pyindex 78f391c17..66abee66f 100644--- a/tests/unit/test_proxy_url_custom.py+++ b/tests/unit/test_proxy_url_custom.py@@ -59,12 +59,12 @@ def test_build_browser_args_uses_configured_recording_size(monkeypatch: pytest.Masync def test_resolve_recording_video_size_is_noop_in_oss() -> None:agent_function = AgentFunction()- assert await agent_function.resolve_recording_video_size(None, distinct_id="wr_1", organization_id="o_1") is None+ none_result = await agent_function.resolve_recording_video_size(None, distinct_id="wr_1", organization_id="o_1")+ assert none_result.record_video_size is None and none_result.raw_output_bound is Noneexisting = {"width": 1280, "height": 720}- assert (- await agent_function.resolve_recording_video_size(existing, distinct_id="wr_1", organization_id="o_1")- == existing- )+ result = await agent_function.resolve_recording_video_size(existing, distinct_id="wr_1", organization_id="o_1")+ # OSS no-op: the operator size passes through as both the Playwright size and the raw whole-display bound.+ assert result.record_video_size == existing and result.raw_output_bound == existingdef test_deserialize_proxy_location_custom_url_returns_dict() -> None:diff --git a/tests/unit/test_real_browser_manager.py b/tests/unit/test_real_browser_manager.pyindex f8e1774ad..6f80ee5fc 100644--- a/tests/unit/test_real_browser_manager.py+++ b/tests/unit/test_real_browser_manager.py@@ -2724,3 +2724,240 @@ def test_snapshot_recording_prefixes_none_when_path_absent(tmp_path) -> None:browser_state = _browser_state_for_snapshot([artifact])assert manager.snapshot_recording_prefixes(browser_state=browser_state, task_id="t") is None+++def _owned_display_recorder_state(mp4_artifact: VideoArtifact, stopped: bool = False) -> MagicMock:+ """A browser_state whose _display_recorder is a REAL DisplayRecorder owning mp4_artifact (the strict+ producer signal snapshot_recording_prefixes uses to admit an MP4 to the bounded prefix path)."""+ from skyvern.webeye.display_recorder import DisplayRecorder++ class _P:+ returncode = None+ pid = 2_147_483_646++ rec = DisplayRecorder(display=":99", owner_id="wr_x", process=_P(), lock_fd=-1, video_artifact=mp4_artifact)+ if stopped:+ rec._stop_result = True # is_stopped -> True (finalization case)+ browser_state = MagicMock()+ browser_state.browser_artifacts.video_artifacts = [mp4_artifact]+ browser_state.browser_artifacts._display_recorder = rec+ return browser_state+++def test_snapshot_recording_prefixes_plans_owned_display_mp4(tmp_path) -> None:+ """SKY-15466: the owned, registered, growing whole-display fragmented-MP4 uses the bounded prefix path,+ snapshotting the current on-disk length (not the live EOF)."""+ manager = RealBrowserManager()+ src = tmp_path / "rec.mp4"+ src.write_bytes(b"m" * 500)+ mp4 = VideoArtifact(video_path=str(src), video_artifact_id="vid-mp4", video_file_extension="mp4")+ browser_state = _owned_display_recorder_state(mp4)++ plan = manager.snapshot_recording_prefixes(browser_state=browser_state, task_id="t")++ src.write_bytes(b"m" * 9_999) # later growth must not enlarge the already-snapshotted bound+ assert plan == [RecordingPrefixSnapshot(video_artifact_id="vid-mp4", path=str(src), prefix_len=500)]+++ "make_state",+ [+ lambda mp4: _browser_state_for_snapshot([mp4]), # unowned: _display_recorder is not a DisplayRecorder+ lambda mp4: _owned_display_recorder_state(mp4, stopped=True), # stopped: terminal byte/supersede case+ ],+ ids=["unowned_mp4", "stopped_display_mp4"],+)+def test_snapshot_recording_prefixes_none_for_non_streaming_mp4(tmp_path, make_state) -> None:+ # A bare .mp4 that is NOT an owned, live display-recorder artifact falls back to the byte path: the bounded+ # MP4 prefix path is gated on the explicit live-producer signal (owned + not stopped), not the extension.+ manager = RealBrowserManager()+ src = tmp_path / "rec.mp4"+ src.write_bytes(b"m" * 500)+ mp4 = VideoArtifact(video_path=str(src), video_artifact_id="vid-mp4", video_file_extension="mp4")+ assert manager.snapshot_recording_prefixes(browser_state=make_state(mp4), task_id="t") is None+++# --- SKY-15466 whole-display recorder: cleanup must not truncate the recording, and the orphan sweep+# --- must not stop a still-live recorder on keep-open paths. ---+async def test_cleanup_then_terminal_read_persists_finalized_mp4_bytes(tmp_path, monkeypatch) -> None:+ """Outer-contract regression: real terminal cleanup must NOT delete the finalized MP4, and the real+ terminal artifact-read consumer used by persistence (``get_video_artifacts``) must then read those+ finalized bytes into ``video_data`` — not the stale last-step snapshot. The reverted bug unlinked in+ cleanup, so the read missed the file and re-uploaded stale/empty bytes. Neither the cleanup nor the read+ is mocked."""+ from types import SimpleNamespace++ from skyvern.config import settings+ from skyvern.webeye import display_recorder+ from skyvern.webeye.browser_artifacts import BrowserArtifacts, VideoArtifact++ monkeypatch.setattr(settings, "VIDEO_PATH", str(tmp_path))+ day = tmp_path / "2026-09-03"+ day.mkdir()+ owner = "tsk_persist"+ recording = day / f"{display_recorder._safe_owner_id(owner)}.mp4"+ finalized = b"\x00\x00\x00\x18ftypisom" + b"FINALIZED-DISPLAY-MP4-BYTES" * 8+ recording.write_bytes(finalized)++ va = VideoArtifact(video_path=str(recording))+ va.video_data = b"STALE-LAST-STEP-SNAPSHOT" # what a per-step sync left behind before finalize+ browser_state = SimpleNamespace(browser_artifacts=BrowserArtifacts(video_artifacts=[va]))++ manager = RealBrowserManager()+ # (1) real terminal cleanup for this owner (no-unlink + gated sweep) — the finalized file must survive.+ await manager.cleanup_for_task(owner, close_browser_on_completion=True)+ assert recording.exists(), "terminal cleanup must not delete the finalized recording"++ # (2) real terminal artifact read/finalize consumer used by persistence — NOT mocked.+ result = await manager.get_video_artifacts(browser_state, task_id=owner, finalize=True)+ assert len(result) == 1+ assert result[0].video_data == finalized # byte-for-byte the finalized on-disk MP4+ assert result[0].video_data != b"STALE-LAST-STEP-SNAPSHOT" # not the pre-finalize snapshot+ assert result[0].video_file_extension == "mp4"+++def _patch_sweep(monkeypatch, swept: list[str]) -> None:+ async def _fake_sweep(owner_id: str) -> int:+ swept.append(owner_id)+ return 0++ monkeypatch.setattr(real_browser_manager, "stop_display_recorders_for_owner", _fake_sweep)+++async def test_cleanup_for_task_sweeps_recorder_only_on_terminal_close(monkeypatch) -> None:+ """Keep-open regression: the orphan sweep may run only on the actual-close path; on keep-open+ (close_browser_on_completion=False) the recorder is intentionally still live and must not be stopped."""+ swept: list[str] = []+ _patch_sweep(monkeypatch, swept)+ manager = RealBrowserManager()++ await manager.cleanup_for_task("tsk_keepopen", close_browser_on_completion=False)+ assert swept == [], "keep-open cleanup must NOT stop the live recorder"++ await manager.cleanup_for_task("tsk_close", close_browser_on_completion=True)+ assert swept == ["tsk_close"], "terminal-close cleanup should sweep exactly its own owner"+++async def test_cleanup_for_script_sweeps_recorder_only_on_effective_close(monkeypatch) -> None:+ """A session-backed script is released (not closed) so the session's browser is reused; its recorder must+ stay live/locked. The sweep gates on EFFECTIVE close (close and not browser_session_id), not raw close."""+ swept: list[str] = []+ _patch_sweep(monkeypatch, swept)+ manager = RealBrowserManager()+ monkeypatch.setattr(manager, "_drop_engine_owner", AsyncMock())++ await manager.cleanup_for_script("scr_shared", close_browser_on_completion=True, browser_session_id="pbs_x")+ assert swept == [], "a session-backed script's live recorder must not be swept"++ await manager.cleanup_for_script("scr_solo", close_browser_on_completion=True)+ assert swept == ["scr_solo"], "standalone terminal close should sweep exactly its own owner"+++def _cleanup_state() -> MagicMock:+ # A browser state whose close path needs no real Playwright objects: no context/traces so tracing is+ # skipped, and close() succeeds.+ st = MagicMock()+ st.browser_context = None+ st.browser_artifacts.traces_dir = None+ st.close = AsyncMock(return_value=True)+ return st+++def _install_cleanup_stubs(manager: RealBrowserManager, monkeypatch, swept: list[str], *, streams: bool) -> None:+ _patch_sweep(monkeypatch, swept)+ monkeypatch.setattr("skyvern.webeye.real_browser_manager.stream_ref_active", lambda wrid: streams)+ monkeypatch.setattr(manager, "_stop_frame_publisher", AsyncMock())+ monkeypatch.setattr(manager, "_drop_engine_owner", AsyncMock())+++# Owner-sweep contract: the orphan sweep reaps a recorder ONLY for an owner whose close was EFFECTIVE (present+# in-process, not suppressed by cross-run sharing). A shared browser is still rendering, so its recorder stays+# live/registered/locked (freeing the flock would let the next run capture it); a non-shared task/run owner IS+# reaped. Both outcomes stay distinct named rows with the exact swept set.+ "pages,shared_pred,run_owner,task_ids,expected_swept",+ [+ (+ ["wr_shared", "tsk_owned"],+ lambda wrid, state: wrid == "wr_shared",+ "wr_shared",+ ["tsk_owned"],+ ["tsk_owned"],+ ),+ (["wr_solo"], lambda wrid, state: False, "wr_solo", [], ["wr_solo"]),+ ],+ ids=["shared_owner_not_reaped_task_reaped", "solo_run_owner_reaped"],+)+async def test_orphan_sweep_reaps_only_effective_close_owners(+ pages, shared_pred, run_owner, task_ids, expected_swept, monkeypatch+) -> None:+ swept: list[str] = []+ manager = RealBrowserManager()+ for p in pages:+ manager.pages[p] = _cleanup_state()+ _install_cleanup_stubs(manager, monkeypatch, swept, streams=False)+ monkeypatch.setattr(manager, "_shared_with_another_workflow_run", shared_pred)+ await manager.cleanup_for_workflow_run(run_owner, task_ids, close_browser_on_completion=True)+ assert sorted(swept) == sorted(expected_swept) # shared owner absent; effective-close owners reaped exactly+++async def test_orphan_sweep_skips_keep_open_and_deferred_paths(monkeypatch) -> None:+ """Keep-open (close=False) and deferred (active CDP stream) paths must sweep NEITHER — the recorder is+ intentionally still live and the later real close handles release."""+ swept: list[str] = []+ manager = RealBrowserManager()+ manager.pages["wr_keep"] = _cleanup_state()+ _install_cleanup_stubs(manager, monkeypatch, swept, streams=False)+ monkeypatch.setattr(manager, "_shared_with_another_workflow_run", lambda wrid, state: False)+ await manager.cleanup_for_workflow_run("wr_keep", ["tsk_k"], close_browser_on_completion=False)+ assert swept == []++ swept.clear()+ manager2 = RealBrowserManager()+ manager2.pages["wr_defer"] = _cleanup_state()+ _install_cleanup_stubs(manager2, monkeypatch, swept, streams=True)+ monkeypatch.setattr(manager2, "_shared_with_another_workflow_run", lambda wrid, state: False)+ # Keep the deferred branch actually deferred: set_deferred_close_params must report streams still active.+ monkeypatch.setattr("skyvern.webeye.real_browser_manager.set_deferred_close_params", lambda *a, **k: True)+ monkeypatch.setattr("skyvern.webeye.real_browser_manager.persist_session_cookies", AsyncMock())+ monkeypatch.setattr(manager2, "_finalize_deferred_display_recording", AsyncMock())+ await manager2.cleanup_for_workflow_run("wr_defer", ["tsk_d"], close_browser_on_completion=True)+ assert swept == []+++async def test_workflow_sweep_cancel_still_sweeps_siblings_releases_session_and_completes_teardown(monkeypatch) -> None:+ # A second cancel landing mid-sweep must not abandon sibling owners' recorders (leaking bridge/flock) nor skip+ # the run's session release + stream teardown: latch it, finish every owner + terminal work once, then re-raise.+ swept: list[str] = []++ async def _sweep(owner_id: str) -> int:+ swept.append(owner_id)+ if len(swept) == 1: # first owner finishes (appended) THEN the caller's cancel is delivered+ raise asyncio.CancelledError()+ return 0++ manager = RealBrowserManager()+ manager.pages["wr_run"] = _cleanup_state()+ manager.pages["tsk_owned"] = _cleanup_state()+ _install_cleanup_stubs(manager, monkeypatch, swept, streams=False)+ monkeypatch.setattr("skyvern.webeye.real_browser_manager.stop_display_recorders_for_owner", _sweep)+ monkeypatch.setattr(manager, "_shared_with_another_workflow_run", lambda wrid, state: False)+ release = AsyncMock(return_value=True)+ monkeypatch.setattr(manager, "_release_persistent_session", release)+ teardown = MagicMock()+ monkeypatch.setattr("skyvern.webeye.real_browser_manager.complete_stream_teardown", teardown)++ with pytest.raises(asyncio.CancelledError):+ await manager.cleanup_for_workflow_run(+ "wr_run", ["tsk_owned"], close_browser_on_completion=True, browser_session_id="pbs_x", organization_id="o"+ )++ assert sorted(swept) == ["tsk_owned", "wr_run"] # sibling still swept despite the first owner's mid-sweep cancel+ release.assert_awaited_once() # session release ran once+ teardown.assert_called_once_with("wr_run") # stream teardown completed oncediff --git a/tests/unit/test_real_browser_state_arm.py b/tests/unit/test_real_browser_state_arm.pynew file mode 100644index 000000000..bca4bf5fd--- /dev/null+++ b/tests/unit/test_real_browser_state_arm.py@@ -0,0 +1,101 @@+"""Arm-seam tests (SKY-15466 two-phase capture). ``check_and_fix_state`` arms the recorder exactly once, only+after a working page exists and the optional initial navigate completes — wrapping ONLY the navigate in+try/finally so a permanent nav failure still records the error page and propagates. A failed arm is logged.+"""++import weakref+from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock++import pytest++from skyvern.webeye import real_browser_state+from skyvern.webeye.display_recorder import DisplayRecorder+from skyvern.webeye.real_browser_state import RealBrowserState+++def _rec(arm_result: bool = True) -> MagicMock:+ r = MagicMock(spec=DisplayRecorder) # spec makes isinstance(r, DisplayRecorder) True+ r.arm_capture.return_value = arm_result+ r.owner_id = "wr_x"+ return r+++def _state(recorder: object, working_page: object) -> RealBrowserState:+ st = RealBrowserState.__new__(RealBrowserState) # bypass __init__+ st._disconnect_listener_contexts = weakref.WeakSet() # constructor invariant the browser_context setter reads+ st.browser_context = MagicMock() # non-None -> creation branch skipped+ st.browser_context.pages = []+ st.browser_artifacts = SimpleNamespace(_display_recorder=recorder, remote_browser_session_id=None)+ st.get_working_page = AsyncMock(return_value=working_page)+ st.set_working_page = AsyncMock()+ st._close_all_other_pages = AsyncMock()+ st.list_valid_pages = AsyncMock(return_value=[])+ return st+++async def test_arm_after_working_page_when_no_navigation() -> None:+ rec = _rec()+ page = MagicMock()+ page.url = "http://127.0.0.1/dynamic.html"+ st = _state(rec, working_page=page) # page already exists -> creation block skipped+ await st.check_and_fix_state(url=None)+ rec.arm_capture.assert_called_once()++[email protected]("nav_raises", [False, True], ids=["nav_ok", "nav_fails"])+async def test_arm_strictly_after_navigation(nav_raises: bool) -> None:+ # arm runs in the navigate finally, so the order is ["nav", "arm"] whether the first navigation succeeds or+ # raises permanently; the failing run still records the error page and the original exception propagates.+ events: list[str] = []+ rec = _rec()+ rec.arm_capture.side_effect = lambda: (events.append("arm"), True)[1]+ newpage = MagicMock()+ newpage.url = "about:blank"+ st = _state(rec, working_page=None) # triggers page creation + navigate+ st.browser_context.new_page = AsyncMock(return_value=newpage)++ def _nav(**kwargs: object) -> None:+ events.append("nav")+ if nav_raises:+ raise RuntimeError("dead url")++ st.navigate_to_url = AsyncMock(side_effect=_nav)+ if nav_raises:+ with pytest.raises(RuntimeError, match="dead url"): # original exception preserved+ await st.check_and_fix_state(url="http://127.0.0.1/dynamic.html")+ else:+ await st.check_and_fix_state(url="http://127.0.0.1/dynamic.html")+ assert events == ["nav", "arm"] # arm strictly AFTER navigation — fails if arm moves before navigate+++async def test_arm_failure_is_logged_and_does_not_raise(monkeypatch: pytest.MonkeyPatch) -> None:+ warnings: list = []+ monkeypatch.setattr(real_browser_state.LOG, "warning", lambda *a, **k: warnings.append((a, k)))+ rec = _rec(arm_result=False)+ page = MagicMock()+ page.url = "http://127.0.0.1/dynamic.html"+ st = _state(rec, working_page=page)+ await st.check_and_fix_state(url=None)+ assert warnings # S1: a failed arm is surfaced as a warning+++async def test_no_recorder_means_no_arm_and_no_error() -> None:+ page = MagicMock()+ page.url = "http://127.0.0.1/dynamic.html"+ st = _state(recorder=None, working_page=page)+ await st.check_and_fix_state(url=None) # must not raise+++async def test_page_creation_failure_does_not_arm() -> None:+ rec = _rec()+ st = _state(rec, working_page=None)+ st.browser_context.new_page = AsyncMock(side_effect=RuntimeError("no page"))+ with pytest.raises(RuntimeError, match="no page"):+ await st.check_and_fix_state(url="http://127.0.0.1/dynamic.html")+ rec.arm_capture.assert_not_called() # no meaningful page yet -> no armdiff --git a/tests/unit/test_recording_artifact_update.py b/tests/unit/test_recording_artifact_update.pyindex b75435977..7de92aac3 100644--- a/tests/unit/test_recording_artifact_update.py+++ b/tests/unit/test_recording_artifact_update.py@@ -80,6 +80,36 @@ async def test_update_recording_artifact_data_defaults_to_not_superseding() -> N)+async def test_update_recording_artifact_data_mp4_terminal_is_same_key_supersede() -> None:+ """SKY-15466: the whole-display recorder registers as .mp4 and streams per-step prefixes to that same+ .mp4 key. Its terminal finalize therefore renames nothing (.mp4 -> .mp4): the authoritative write and+ the queued-prefix key coincide, so prefix_uri stays None and supersede seals the SAME key (no stale+ pre-rename key to seal, no second WebM->MP4 transcode)."""+ manager = ArtifactManager()+ artifact = _make_recording_artifact("s3://bucket/path/recording.mp4")+ updated = artifact.model_copy(update={"file_size": 42})++ with patch("skyvern.forge.sdk.artifact.manager.app") as mock_app:+ mock_app.DATABASE.artifacts.get_artifact_by_id = AsyncMock(return_value=artifact)+ mock_app.DATABASE.artifacts.update_artifact_uri = AsyncMock(return_value=updated)+ mock_app.STORAGE.store_artifact = AsyncMock()++ await manager.update_artifact_data(+ artifact_id=artifact.artifact_id,+ organization_id=artifact.organization_id,+ data=b"final-mp4-bytes",+ file_extension="mp4",+ supersede_queued_prefixes=True,+ )+ await asyncio.gather(*manager.upload_aiotasks_map["task_1"])++ # No rename (.mp4 -> .mp4): store on the same key, supersede queued prefixes, prefix_uri stays None.+ mock_app.STORAGE.store_artifact.assert_awaited_once_with(+ updated, b"final-mp4-bytes", supersede_queued_prefixes=True, prefix_uri=None+ )++@pytest.mark.asyncioasync def test_update_recording_artifact_data_same_extension_updates_file_size() -> None:manager = ArtifactManager()diff --git a/tests/unit/test_recording_incremental_compose.py b/tests/unit/test_recording_incremental_compose.pyindex a45d0e248..d25700923 100644--- a/tests/unit/test_recording_incremental_compose.py+++ b/tests/unit/test_recording_incremental_compose.py@@ -27,6 +27,7 @@ from skyvern.forge.sdk.api.aws import (AsyncAWSClient,_ComposeState,_ComposeStateCache,+ _recording_content_type,)from skyvern.forge.sdk.artifact.models import Artifact, ArtifactTypefrom skyvern.forge.sdk.artifact.storage.s3 import S3Storage@@ -34,6 +35,10 @@ from skyvern.forge.sdk.artifact.storage.s3 import S3StorageMIB = 1024 * 1024KEY = "v1/prod/org/task/rec.webm"URI = f"s3://bucket/{KEY}"+KEY_MP4 = "v1/prod/org/task/rec.mp4"+URI_MP4 = f"s3://bucket/{KEY_MP4}"+KEY_XYZ = "v1/prod/org/task/rec.skyvernunknown15466" # project-unique ext, guess_type -> None on all platforms+URI_XYZ = f"s3://bucket/{KEY_XYZ}"GEN = "skyvern-compose-gen"@@ -51,6 +56,7 @@ class FakeS3:self.abort_transient_failures = 0 # first N abort calls raise a transient (non-expired) error, then workself.ops: list[str] = [] # ordered op log (cleanup-before-replacement ordering assertions)self.completed: list[str] = []+ self.content_types: dict[str, str] = {} # key -> ContentType seen on the write that authored itself._n = 0self.fail: dict[str, Exception] = {} # op -> exception to raise once (ClientError OR transport error)self.on_complete: Any = None # optional hook(key, uploadid) to simulate ambiguity/foreign writes@@ -70,6 +76,8 @@ class FakeS3:self._n += 1etag = f'"put-{self._n}"'self.objects[Key] = (data, etag, {})+ if kw.get("ContentType"):+ self.content_types[Key] = kw["ContentType"]return {"ETag": etag}async def upload_fileobj(@@ -86,6 +94,8 @@ class FakeS3:self._n += 1meta = dict((ExtraArgs or {}).get("Metadata") or {}) # real S3 preserves user metadata on put/multipartself.objects[Key] = (b"".join(chunks), f'"stream-{self._n}"', meta)+ if (ExtraArgs or {}).get("ContentType"):+ self.content_types[Key] = ExtraArgs["ContentType"]async def create_multipart_upload(self, *, Bucket: str, Key: str, Metadata: dict | None = None, **kw: Any@@ -95,6 +105,8 @@ class FakeS3:self._n += 1uid = f"mpu-{self._n}"self.mpus[uid] = {"key": Key, "parts": {}, "meta": dict(Metadata or {})}+ if kw.get("ContentType"): # S3 carries the MPU ContentType onto the completed object+ self.content_types[Key] = kw["ContentType"]return {"UploadId": uid}async def upload_part_copy(@@ -306,6 +318,35 @@ async def test_compose_chains_and_uses_cached_state(tmpfile: str) -> None:assert len(_obj(fake)) == 13 * MIB+# SKY-15466: generic S3 seams never infer ContentType from a (user-named) key; RECORDING opts in via allowlist.[email protected]("uri,key", [("s3://b/x.html", "x.html"), ("s3://b/x.svg", "x.svg"), (URI_MP4, KEY_MP4)])+async def test_generic_seams_never_infer_recording_opts_in(tmpfile: str, uri: str, key: str) -> None:+ fake = FakeS3()+ c = _make_client(fake)+ _write(tmpfile, 4096)+ await c.upload_file(uri, b"x") # generic put: no content_type+ with open(tmpfile, "rb") as fh:+ await c.upload_file_stream(uri, fh) # save_legacy_file path: no content_type+ assert key not in fake.content_types # never inferred from any (untrusted) name; both seams+ assert _recording_content_type("a.webm") == "video/webm" and _recording_content_type("x.html") is None+++async def test_recording_prefix_reseed_then_compose_advance_mp4_content_type(tmpfile: str) -> None:+ fake = FakeS3()+ c = _make_client(fake)+ # first prefix reseeds via full replacement (_full_replace_and_seed -> upload_fileobj)+ _write(tmpfile, 11 * MIB)+ await c.store_recording_prefix(URI_MP4, tmpfile, 11 * MIB, serialize_key=URI_MP4)+ assert fake.content_types.get(KEY_MP4) == "video/mp4"+ # second, larger prefix triggers a real compose advance (create_multipart_upload must carry ContentType)+ _write(tmpfile, 13 * MIB)+ await c.store_recording_prefix(URI_MP4, tmpfile, 13 * MIB, serialize_key=URI_MP4)+ assert fake.completed # a compose advance actually ran+ assert fake.content_types.get(KEY_MP4) == "video/mp4"++# --- conflict preservation (blocker 2) ---------------------------------------------------------diff --git a/tests/unit/test_request_logging.py b/tests/unit/test_request_logging.pyindex 3bbf329e7..10f36f479 100644--- a/tests/unit/test_request_logging.py+++ b/tests/unit/test_request_logging.py@@ -389,6 +389,29 @@ def test_sanitize_headers_removes_posthog_attribution_but_keeps_ordinary_header(class TestSanitizeResponseBody:+ @pytest.mark.parametrize("method", ["POST", "GET", "DELETE", "OPTIONS", "PATCH"])+ @pytest.mark.parametrize(+ "path",+ [+ "/mcp",+ "/mcp/",+ "/mcp/x/browser",+ "/mcp/x/browser/",+ "/mcp/.well-known/oauth-protected-resource",+ "/mcp/x/browser/.well-known/openid-configuration",+ "/mcp/unknown\npath",+ ],+ )+ def test_mcp_response_fully_redacted_before_json_parsing(+ self, method: str, path: str, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ parse = MagicMock(side_effect=AssertionError("MCP bodies must stay opaque"))+ monkeypatch.setattr(request_logging.json, "loads", parse)+ request = _make_request(method, path)+ for body in ['{"result":"SYNTHETIC_SECRET"}', '[{"result":"SYNTHETIC_SECRET"}]', "{malformed"]:+ assert _sanitize_response_body(request, body, "application/json") == REDACTED+ parse.assert_not_called()+def test_sensitive_endpoint_fully_redacted(self) -> None:request = _make_request("POST", "/api/v1/credentials")result = _sanitize_response_body(request, '{"token": "abc"}', "application/json")@@ -477,6 +500,36 @@ class TestSanitizeResponseBody:class TestSanitizeBody:+ @pytest.mark.parametrize("method", ["POST", "GET", "DELETE", "OPTIONS", "PATCH"])+ @pytest.mark.parametrize(+ "path",+ [+ "/mcp",+ "/mcp/",+ "/mcp/x/browser",+ "/mcp/x/browser/",+ "/mcp/.well-known/oauth-authorization-server",+ "/mcp/x/browser/.well-known/oauth-protected-resource",+ "/mcp/unknown\npath",+ ],+ )+ def test_mcp_request_fully_redacted_before_json_parsing(+ self, method: str, path: str, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ parse = MagicMock(side_effect=AssertionError("MCP bodies must stay opaque"))+ monkeypatch.setattr(request_logging.json, "loads", parse)+ request = _make_request(method, path)+ for body in [b'{"params":"SYNTHETIC_SECRET"}', b'[{"params":"SYNTHETIC_SECRET"}]', b"{malformed"]:+ assert _sanitize_body(request, body, "application/json") == REDACTED+ parse.assert_not_called()++ @pytest.mark.parametrize("path", ["/mcpx", "/mcpserver", "/api/v1/mcp-something", "/v1/mcp"])+ def test_mcp_similarly_named_routes_remain_loggable(self, path: str) -> None:+ request = _make_request("POST", path)+ body = '{"message":"public diagnostic"}'+ assert _sanitize_body(request, body.encode(), "application/json") == body+ assert _sanitize_response_body(request, body, "application/json") == body+def test_sensitive_endpoint_request_fully_redacted(self) -> None:request = _make_request("POST", "/v1/credentials")result = _sanitize_body(request, b'{"password": "hunter2"}', "application/json")@@ -667,6 +720,21 @@ def _make_app(unhandled_exception_status: int = 500) -> FastAPI:return StreamingResponse(body(), media_type="text/event-stream")+ @app.api_route("/mcp", methods=["POST", "GET", "DELETE", "OPTIONS"])+ @app.api_route("/mcp/{path:path}", methods=["POST", "GET", "DELETE", "OPTIONS"])+ async def mcp_response(request: Request) -> Response:+ app.state.mcp_request_body = await request.body()+ if request.query_params.get("stream"):++ async def events() -> typing.AsyncIterator[bytes]:+ yield b"data: SYNTHETIC_SECRET_RESULT\n\n"++ return StreamingResponse(events(), media_type="text/event-stream", status_code=202)+ return JSONResponse(+ {"jsonrpc": "2.0", "result": {"content": "SYNTHETIC_SECRET_RESULT"}},+ status_code=int(request.query_params.get("status", "200")),+ )+return app@@ -680,6 +748,94 @@ def log_mock(monkeypatch: pytest.MonkeyPatch) -> MagicMock:class TestMiddlewareLogVolume:+ @pytest.mark.parametrize("status_code", [200, 403, 500])+ @pytest.mark.parametrize(+ "body",+ [+ (+ '{"jsonrpc":"2.0","method":"tools/call","params":{"name":"skyvern_evaluate",'+ '"arguments":{"expression":"() => fetch(\\"sk-test-evaluate\\")"}}}'+ ),+ '{"params":{"prompt":"Use SYNTHETIC_SECRET_PROMPT to continue"}}',+ '{"params":{"arguments":{"nested":[{"value":"SYNTHETIC_SECRET_NESTED"}]}}}',+ "{malformed SYNTHETIC_SECRET_MALFORMED",+ '[{"params":{"prompt":"SYNTHETIC_SECRET_BATCH"}}]',+ ],+ ids=["evaluate", "prompt", "nested", "malformed", "batch"],+ )+ def test_mcp_log_payload_excludes_request_and_result_secrets(+ self, log_mock: MagicMock, body: str, status_code: int+ ) -> None:+ app = _make_app()+ response = TestClient(app).post(+ f"/mcp/x/browser/?status={status_code}",+ content=body,+ headers={+ "content-type": "application/json",+ "authorization": "Bearer sk-test-auth",+ "x-api-key": "sk-test-api-key",+ },+ )+ assert response.status_code == status_code+ assert response.json()["result"]["content"] == "SYNTHETIC_SECRET_RESULT"+ assert app.state.mcp_request_body == body.encode()+ assert len(log_mock.mock_calls) == 1+ call = log_mock.mock_calls[0]+ assert call[0] == ("error" if status_code >= 500 else "warning" if status_code >= 400 else "info")+ assert call.args == ("api.raw_request",)+ fields = call.kwargs+ assert fields["body"] == fields["response_body"] == REDACTED+ assert fields["error_body"] == (REDACTED if status_code >= 400 else None)+ assert fields["method"] == "POST"+ assert fields["path"] == "/mcp/x/browser/"+ assert fields["status_code"] == status_code+ assert fields["duration_seconds"] >= 0+ assert "authorization" not in fields["headers"]+ assert "x-api-key" not in fields["headers"]+ assert "SYNTHETIC_SECRET" not in repr(call)+ assert "sk-test-" not in repr(call)++ @pytest.mark.parametrize("method", ["POST", "GET"])+ def test_mcp_stream_logs_once_without_reading_body(+ self, log_mock: MagicMock, method: str, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ read_body = AsyncMock(side_effect=AssertionError("SSE must not be buffered for logging"))+ monkeypatch.setattr(request_logging, "_get_response_body_str", read_body)+ response = TestClient(_make_app()).request(+ method,+ "/mcp/?stream=1",+ content="SYNTHETIC_SECRET_REQUEST",+ )+ assert response.status_code == 202+ assert response.content == b"data: SYNTHETIC_SECRET_RESULT\n\n"+ read_body.assert_not_awaited()+ assert len(log_mock.mock_calls) == 1+ call = log_mock.info.call_args+ assert call.args == ("api.raw_request",)+ assert call.kwargs["body"] == REDACTED+ assert call.kwargs["response_body"] == "<streaming>"+ assert call.kwargs["status_code"] == 202+ assert call.kwargs["duration_seconds"] >= 0+ assert "SYNTHETIC_SECRET" not in repr(call)++ def test_mcp_exception_logs_once_with_redacted_request(self, log_mock: MagicMock) -> None:+ response = TestClient(_make_app(), raise_server_exceptions=False).request(+ "GET",+ "/mcp/boom",+ content="SYNTHETIC_SECRET_REQUEST",+ headers={"authorization": "Bearer sk-test-auth"},+ )+ assert response.status_code == 500+ assert len(log_mock.mock_calls) == 1+ call = log_mock.error.call_args+ assert call.args == ("api.raw_request",)+ assert call.kwargs["body"] == REDACTED+ assert call.kwargs["exc_info"] is True+ assert call.kwargs["status_code"] == 500+ assert call.kwargs["duration_seconds"] >= 0+ assert "SYNTHETIC_SECRET" not in repr(call)+ assert "sk-test-auth" not in repr(call)+def test_scope_rewrite_keeps_unhandled_exception_logging(self, log_mock: MagicMock) -> None:app = _make_app()app.add_middleware(_ScopeCopyingMiddleware)diff --git a/tests/unit/test_run_response_types.py b/tests/unit/test_run_response_types.pyindex 44de05b60..6de015b71 100644--- a/tests/unit/test_run_response_types.py+++ b/tests/unit/test_run_response_types.py@@ -1,8 +1,15 @@from datetime import UTC, datetime+from typing import Any+from unittest.mock import AsyncMock, MagicMock+import httpx+import pytestfrom pydantic import TypeAdapter+from skyvern.client import AsyncSkyvern, GetRunResponsefrom skyvern.forge.sdk.workflow.models.workflow import WorkflowRunStatus+from skyvern.library import skyvern_browser_page_agent+from skyvern.library.skyvern_browser_page_agent import SkyvernBrowserPageAgentfrom skyvern.schemas.runs import RunResponse, RunStatus, RunType, TaskRunResponse, WorkflowRunResponse@@ -51,7 +58,7 @@ def test_workflow_run_response_preserves_run_type_enum() -> None:def test_run_response_discriminator_preserves_run_type_enum() -> None:- response = TypeAdapter(RunResponse).validate_python(+ response: TaskRunResponse | WorkflowRunResponse = TypeAdapter(RunResponse).validate_python({"run_id": "tr_123","run_type": "task_v2",@@ -63,3 +70,52 @@ def test_run_response_discriminator_preserves_run_type_enum() -> None:assert isinstance(response, TaskRunResponse)assert response.run_type is RunType.task_v2+++def _run_payload(run_type: RunType, status: RunStatus) -> dict[str, Any]:+ return (+ TypeAdapter(RunResponse)+ .validate_python(+ {+ "run_id": "run_synthetic",+ "run_type": run_type,+ "status": status,+ "created_at": "2026-01-01T00:00:00Z",+ "modified_at": "2026-01-01T00:00:00Z",+ }+ )+ .model_dump(mode="json")+ )++[email protected]("run_type", [RunType.task_v1, RunType.task_v2, RunType.task_v3, RunType.workflow_run])[email protected]("status", [RunStatus.running, RunStatus.completed])+async def test_sdk_get_run_parses_backend_response(run_type: RunType, status: RunStatus) -> None:+ payload = _run_payload(run_type, status)+ transport = httpx.MockTransport(lambda request: httpx.Response(200, json=payload))+ async with httpx.AsyncClient(transport=transport) as http:+ sdk = AsyncSkyvern(api_key="synthetic", httpx_client=http)+ response = await sdk.get_run(payload["run_id"])++ assert response.run_type == run_type.value+ assert response.status == status.value+ assert response.run_id == payload["run_id"]++[email protected]("run_type", [RunType.task_v1, RunType.task_v2, RunType.task_v3, RunType.workflow_run])+async def test_agent_polls_running_to_completed(run_type: RunType, monkeypatch: pytest.MonkeyPatch) -> None:+ responses: list[GetRunResponse] = [+ TypeAdapter(GetRunResponse).validate_python(_run_payload(run_type, status))+ for status in (RunStatus.running, RunStatus.completed)+ ]+ browser = MagicMock()+ browser.skyvern.get_run = AsyncMock(side_effect=responses)+ monkeypatch.setattr(skyvern_browser_page_agent, "DEFAULT_AGENT_HEARTBEAT_INTERVAL", 0)+ agent = SkyvernBrowserPageAgent(browser, MagicMock())++ response = await agent._wait_for_run_completion("run_synthetic", timeout=1)++ assert response.run_type == run_type.value+ assert response.status == "completed"diff --git a/tests/unit/test_run_script_lifecycle.py b/tests/unit/test_run_script_lifecycle.pyindex 7781c7d10..9cf29f900 100644--- a/tests/unit/test_run_script_lifecycle.py+++ b/tests/unit/test_run_script_lifecycle.py@@ -11,14 +11,17 @@ from __future__ import annotationsimport asynciofrom datetime import datetimefrom pathlib import Path+from types import SimpleNamespacefrom unittest.mock import AsyncMock, MagicMockimport pytestfrom skyvern.forge import app+from skyvern.forge.sdk.artifact.manager import ArtifactManagerfrom skyvern.forge.sdk.core import skyvern_contextfrom skyvern.schemas.workflows import BlockType-from skyvern.services.script_service import _create_workflow_block_run_and_task, run_script+from skyvern.services.script_service import _create_video_artifact, _create_workflow_block_run_and_task, run_script+from skyvern.webeye.browser_artifacts import BrowserArtifacts, VideoArtifactfrom skyvern.webeye.real_browser_manager import RealBrowserManagerSUCCESS_SCRIPT = "async def run_workflow(parameters=None):\n return None\n"@@ -208,3 +211,34 @@ async def test_run_script_ordinary_cleanup_error_preserves_outcome(else:await run_script(path, **kwargs)manager.cleanup_for_script.assert_awaited_once()+++ "extension, expected_suffix",+ [("mp4", ".mp4"), ("webm", ".webm"), (None, ".webm")],+ ids=["mp4_video_creates_mp4_uri", "webm_video_keeps_webm_uri", "default_video_keeps_webm_uri"],+)+async def test_create_video_artifact_uri_matches_video_extension(+ monkeypatch: pytest.MonkeyPatch, extension: str | None, expected_suffix: str+) -> None:+ # Entry-point (real ArtifactManager URI, not mock-arg): script_service must key the mp4 RECORDING row .mp4.+ va = [VideoArtifact(video_path="/tmp/recording.mp4", video_data=b"partial", video_file_extension=extension)]+ browser_state = SimpleNamespace(browser_artifacts=BrowserArtifacts(video_artifacts=va))+ task = SimpleNamespace(task_id="tsk", workflow_run_id="wr")+ step = SimpleNamespace(organization_id="o", task_id="tsk", step_id="stp")+ captured: dict[str, str] = {}++ async def capture(*, uri: str, **_: object) -> str:+ captured["uri"] = uri+ return "a_recording"++ real = ArtifactManager()+ monkeypatch.setattr(real, "_create_artifact", AsyncMock(side_effect=capture))+ monkeypatch.setattr(app, "ARTIFACT_MANAGER", real)+ monkeypatch.setattr(app.STORAGE, "build_uri", MagicMock(return_value="s3://b/recording.webm"))+ monkeypatch.setattr(app.BROWSER_MANAGER, "get_for_workflow_run", MagicMock(return_value=browser_state))+ monkeypatch.setattr(app.BROWSER_MANAGER, "get_video_artifacts", AsyncMock(return_value=va))+ monkeypatch.setattr(app.BROWSER_MANAGER, "set_video_artifact_for_task", MagicMock())+ await _create_video_artifact(task, step)+ assert captured["uri"].endswith(expected_suffix)diff --git a/tests/unit/test_taskv3_tools.py b/tests/unit/test_taskv3_tools.pyindex c6e8fb9b3..cf47d06f4 100644--- a/tests/unit/test_taskv3_tools.py+++ b/tests/unit/test_taskv3_tools.py@@ -1764,6 +1764,7 @@ async def test_observe_result_carries_count_only_summary_for_the_call_record() -"markers_minted","markers_reused","group_texts_found",+ "a11y_removed_listed",}assert all(type(v) is int for v in summary.values())assert summary["invalid_fields"] == 1@@ -11291,6 +11292,218 @@ async def test_observe_still_lists_aria_hidden_tabindex_negative_text_input_whenassert _ref_line(r.content, "Promo code")+_MARK = "[aria-hidden: the page keeps this out of its accessibility tree and tab order]"++# One control, two nodes. A design system builds its own clickable surface and leaves the native+# control underneath, marked out of the accessibility tree and out of the tab order. Both carry the+# same caption, so observe printed two byte-identical lines and the model had nothing to tell them+# apart -- and the one the page disowned is routinely the one that does not work.+_TWO_RENDERINGS = """+<!doctype html><html><body>+ <div id="wrapper" role="button" tabindex="0" style="width:376px;height:40px">+ <button id="native" type="submit" {removal} style="width:376px;height:40px">Sign In</button>+ </div>+</body></html>+"""+++def _lines(content: str) -> list[str]:+ return [line for line in content.splitlines() if line.startswith("ref=")]+++@_skip_no_browser+ "removal",+ [+ # The tab order is whatever the UA parsed, not what the attribute spells: "-1x" is -1 to the+ # browser, and "-0" (in the negative-control test below) is not negative at all.+ pytest.param('aria-hidden="true" tabindex="-1"', id="tabindex-1"),+ pytest.param('aria-hidden="true" tabindex="-2"', id="tabindex-2"),+ pytest.param('aria-hidden="true" tabindex="-99"', id="tabindex-99"),+ pytest.param('aria-hidden="true" tabindex="-1x"', id="tabindex-trailing-junk"),+ pytest.param('aria-hidden="TRUE" tabindex="-1"', id="aria-hidden-uppercase"),+ ],+)+async def test_observe_tells_a_removed_rendering_apart_from_the_control_the_page_drives(removal: str) -> None:+ async with _content_page(_TWO_RENDERINGS.format(removal=removal)) as page:+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "observe").handler({})+ assert r.status == "ok", r.content+ named = [line for line in _lines(r.content) if "'Sign In'" in line]+ # Both stay listed -- deciding which of two same-named controls to use is the model's job,+ # and the removed one is sometimes the only one that works. What changes is that the two+ # lines are no longer the same bytes.+ assert len(named) == 2, r.content+ [marked] = [line for line in named if _MARK in line]+ assert marked.split(" ")[1] == "button/submit", r.content+ assert r.data["summary"]["a11y_removed_listed"] == 1, r.data+++@_skip_no_browser+ "attrs",+ [+ 'aria-hidden="true"',+ 'tabindex="-2"',+ 'tabindex="0"',+ 'tabindex="-0"',+ 'aria-hidden="false" tabindex="-2"',+ 'aria-hidden="true" tabindex="1"',+ ],+)+async def test_observe_does_not_mark_a_control_the_page_removed_from_only_one(attrs: str) -> None:+ # The positive control, one per conjunct: neither half is the cause on its own. Without this a+ # green result above could be a blanket mark on anything carrying either attribute.+ async with _content_page(_TWO_RENDERINGS.format(removal=attrs)) as page:+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "observe").handler({})+ assert r.status == "ok", r.content+ assert len([line for line in _lines(r.content) if "'Sign In'" in line]) == 2, r.content+ assert _MARK not in r.content, r.content+++@_skip_no_browser+ "ancestor",+ [+ pytest.param('<div aria-hidden="true">', id="inherited-aria-hidden"),+ pytest.param("<div inert>", id="inherited-inert"),+ ],+)+async def test_observe_reads_only_the_controls_own_removal_attributes(ancestor: str) -> None:+ # aria-hidden inherits and `inert` covers a region, so reading the ancestor chain would be+ # strictly more faithful -- and five review rounds running found a demonstrated defect in that+ # walk. This pins the narrower rule that shipped instead, so the day someone builds SKY-15894+ # this test is the thing that has to change, deliberately, rather than a silent widening.+ html = f"""<!doctype html><html><body>+ {ancestor}<button id="native" type="submit" tabindex="-1"+ style="width:200px;height:40px">Continue</button></div></body></html>"""+ async with _content_page(html) as page:+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "observe").handler({})+ assert r.status == "ok", r.content+ assert _MARK not in r.content, r.content+++@_skip_no_browser+async def test_observe_marks_a_removed_svg_control() -> None:+ # tabIndex is declared per interface. Reading only HTMLElement's getter leaves an icon control+ # authored as <svg role="button"> unable to carry the mark at all.+ html = """<!doctype html><html><body>+ <svg role="button" aria-hidden="true" tabindex="-1" width="40" height="40">+ <rect width="40" height="40"/></svg></body></html>"""+ async with _content_page(html) as page:+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "observe").handler({})+ assert r.status == "ok", r.content+ [line] = _lines(r.content)+ assert _MARK in line, r.content+++@_skip_no_browser+async def test_observe_survives_a_page_that_removed_the_tabindex_descriptor() -> None:+ # The getters are captured in the page's own realm on each call. A page that has deleted one+ # leaves the capture undefined, and calling it would throw inside the per-element guard -- which+ # answers by dropping the control, for every control on the page.+ html = """<!doctype html><html><body>+ <script>delete HTMLElement.prototype.tabIndex;</script>+ <form>+ <label for="e">Email</label><input id="e" type="text" style="width:200px;height:30px">+ <button style="width:80px;height:30px">Continue</button>+ </form></body></html>"""+ async with _content_page(html) as page:+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "observe").handler({})+ assert r.status == "ok", r.content+ assert len(_lines(r.content)) == 2, r.content+ assert _ref_line(r.content, "Email")+++@_skip_no_browser+async def test_observe_rereads_the_removal_state_a_page_changed_during_the_walk() -> None:+ # Marking a control for addressing runs the page's own observers, and one of them can restore+ # the control it had removed. The line has to describe the page as it stands when the line is+ # printed, not as it stood when the record was built.+ html = """<!doctype html><html><body>+ <button aria-hidden="true" tabindex="-1" style="width:200px;height:40px">Continue</button>+ <script>+ new MutationObserver(() => {+ // Only the tabindex: the element's own aria-hidden is already fingerprinted, so changing+ // it would drop the record instead of exercising the re-read.+ for (const b of document.querySelectorAll('button[tabindex]')) b.removeAttribute('tabindex');+ }).observe(document.body, {attributes: true, subtree: true});+ </script></body></html>"""+ async with _content_page(html) as page:+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "observe").handler({})+ assert r.status == "ok", r.content+ [line] = [ln for ln in _lines(r.content) if "'Continue'" in ln]+ assert _MARK not in line, r.content+ assert r.data["summary"]["a11y_removed_listed"] == 0, r.data+++@_skip_no_browser+async def test_observe_does_not_mark_a_skinned_native_the_tools_drive_directly() -> None:+ # A design system skins a native at zero size behind a styled widget and marks it exactly this+ # way. That case is already answered on the same line -- "the tool acts on it directly" -- and+ # the two verdicts contradict each other, so the removal mark stops at the carve-out.+ html = """<!doctype html><html><body>+ <label for="country">Country</label>+ <select id="country" aria-hidden="true" tabindex="-1" style="display:none">+ <option>United States</option></select>+ <div role="listbox" style="width:200px;height:30px">Pick</div></body></html>"""+ async with _content_page(html) as page:+ tools = build_browser_tools(_fixed_page_provider(page))+ r = await _tool(tools, "observe").handler({})+ assert r.status == "ok", r.content+ [line] = [ln for ln in _lines(r.content) if "'Country'" in ln]+ assert "[hidden-native" in line, r.content+ assert _MARK not in line, r.content+ assert r.data["summary"]["a11y_removed_listed"] == 0, r.data+++# Being removed is not being useless: a styled proxy hides the native control a tool drives through+# the element itself, and a removed control can be the only one on the page. Nothing here may be+# dropped -- the pre-existing rule still drops only the unnamed text controls it always did.+_REMOVED_BUT_NEEDED = {+ "the_only_fillable_field": """+ <div role="combobox" tabindex="0" aria-expanded="false" style="width:260px;height:36px">+ <span>Country</span>+ <input type="text" aria-label="Country" aria-hidden="true" tabindex="-1"></div>""",+ "the_native_behind_a_file_proxy": """+ <div style="position:relative;width:220px;height:40px">+ <button type="button" style="width:220px;height:40px">Attach Resume</button>+ <input type="file" aria-label="Attach Resume" aria-hidden="true" tabindex="-1"+ style="position:absolute;inset:0;width:220px;height:40px;opacity:0"></div>""",+ "the_only_control_on_the_page": """+ <button aria-hidden="true" tabindex="-1" style="width:32px;height:32px">Close</button>""",+}+++@_skip_no_browser[email protected]("shape", sorted(_REMOVED_BUT_NEEDED))+async def test_observe_still_lists_every_removed_control_it_marks(shape: str) -> None:+ html = f"<!doctype html><html><body>{_REMOVED_BUT_NEEDED[shape]}</body></html>"+ async with _content_page(html) as page:+ tools = build_browser_tools(_fixed_page_provider(page))+ expected = await page.evaluate(+ "() => document.querySelectorAll('input,select,button,[role=button],[role=combobox]').length"+ )+ r = await _tool(tools, "observe").handler({})+ assert r.status == "ok", r.content+ assert len(_lines(r.content)) == expected, r.content+ assert r.data["summary"]["phantom_dropped"] == 0, r.data+ assert r.data["summary"]["a11y_removed_listed"] == 1, r.data++# A consent wall is the common shape of a layer that covers a whole form: a fixed, view-sized# dialog whose own controls are what dismiss it. Naming the layer alone is not enough for the# model -- a container id says nothing about what to click -- so the message must list thediff --git a/tests/unit/webeye/test_browser_errors.py b/tests/unit/webeye/test_browser_errors.pyindex 1291aa9f7..d2c907bc2 100644--- a/tests/unit/webeye/test_browser_errors.py+++ b/tests/unit/webeye/test_browser_errors.py@@ -10,6 +10,7 @@ from skyvern.webeye.browser_errors import (_CONTEXT_LOST_MESSAGE_MARKERS,_TARGET_CLOSED_MESSAGE_MARKERS,BrowserAutomationError,+ BrowserCdpAcquisitionError,BrowserCdpConnectionError,BrowserEngineErrorFamilies,BrowserErrorFamiliesConfigError,@@ -45,12 +46,14 @@ def test_taxonomy_subclasses_skyvern_exception() -> None:for cls in (BrowserTimeoutError,BrowserTargetClosedError,+ BrowserCdpAcquisitionError,BrowserCdpConnectionError,BrowserRetryableCdpError,):assert issubclass(cls, BrowserAutomationError)# Retryable CDP error is a specialization of the transport error.assert issubclass(BrowserRetryableCdpError, BrowserCdpConnectionError)+ assert not issubclass(BrowserCdpAcquisitionError, BrowserCdpConnectionError)def test_timeout_type_classifies_to_timeout_with_cause() -> None:diff --git a/tests/unit/webeye/test_screenshot_load_state_nonfatal.py b/tests/unit/webeye/test_screenshot_load_state_nonfatal.pyindex 1a52a60d4..a4504bd5a 100644--- a/tests/unit/webeye/test_screenshot_load_state_nonfatal.py+++ b/tests/unit/webeye/test_screenshot_load_state_nonfatal.py@@ -154,13 +154,19 @@ class TestScreenshotLoadStateNonFatal:page.screenshot.assert_not_awaited()@pytest.mark.asyncio- async def test_detailed_mode_waits_for_domcontentloaded(self) -> None:+ async def test_detailed_mode_waits_for_load(self) -> None:page = _make_page(b"image-bytes")- await _current_viewpoint_screenshot_helper(page, mode=ScreenshotMode.DETAILED)+ result = await _current_viewpoint_screenshot_helper(page, mode=ScreenshotMode.DETAILED)+ assert result == b"image-bytes"page.wait_for_load_state.assert_awaited_once()- assert page.wait_for_load_state.await_args.args[0] == "domcontentloaded"+ assert page.wait_for_load_state.await_args.args[0] == "load"+ # The configured (default 5s) budget is preserved — the event changed, not the timeout policy.+ assert (+ page.wait_for_load_state.await_args.kwargs["timeout"]+ == SettingsManager.get_settings().BROWSER_SCREENSHOT_LOAD_STATE_TIMEOUT_MS+ )@pytest.mark.asyncioasync def test_lite_mode_skips_load_state_wait(self) -> None:diff --git a/tests/unit/workflow/test_code_block_template_validation.py b/tests/unit/workflow/test_code_block_template_validation.pynew file mode 100644index 000000000..4c9e07f94--- /dev/null+++ b/tests/unit/workflow/test_code_block_template_validation.py@@ -0,0 +1,117 @@+from __future__ import annotations++from datetime import UTC, datetime+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from skyvern.forge.sdk.workflow.exceptions import CodeBlockTemplateSyntaxError+from skyvern.forge.sdk.workflow.models.block import CodeBlock, ForLoopBlock+from skyvern.forge.sdk.workflow.models.parameter import OutputParameter+from skyvern.forge.sdk.workflow.models.workflow import WorkflowDefinition+from skyvern.forge.sdk.workflow.service import WorkflowService+++def _output_parameter(label: str) -> OutputParameter:+ now = datetime.now(UTC)+ return OutputParameter(+ output_parameter_id=f"op_{label}",+ key=f"{label}_output",+ workflow_id="wf_test",+ created_at=now,+ modified_at=now,+ )+++def _code_block(label: str, code: str) -> CodeBlock:+ return CodeBlock(label=label, code=code, output_parameter=_output_parameter(label))+++def _definition(blocks: list[CodeBlock | ForLoopBlock]) -> WorkflowDefinition:+ return WorkflowDefinition(parameters=[], blocks=blocks)+++def test_save_validator_rejects_nested_code_block_with_line_number() -> None:+ nested = _code_block("nested_code", "value = 1\nraw = r'''{{ }}'''\n")+ loop = ForLoopBlock(label="loop", loop_blocks=[nested], output_parameter=_output_parameter("loop"))++ with pytest.raises(CodeBlockTemplateSyntaxError) as excinfo:+ WorkflowService._validate_code_block_templates(_definition([loop]))++ assert excinfo.value.block_label == "nested_code"+ assert excinfo.value.line == 2+++def test_save_validator_allows_syntactically_valid_undefined_reference() -> None:+ WorkflowService._validate_code_block_templates(_definition([_code_block("code", "value = {{ future_value }}")]))+++def test_save_validator_rejects_unknown_jinja_filter() -> None:+ definition = _definition([_code_block("code", "value = {{ future_value | missing_filter }}")])++ with pytest.raises(CodeBlockTemplateSyntaxError) as excinfo:+ WorkflowService._validate_code_block_templates(definition)++ assert excinfo.value.block_label == "code"+ assert excinfo.value.line == 1+++async def test_create_workflow_rejects_before_database_write() -> None:+ service = WorkflowService()+ create = AsyncMock()+ definition = _definition([_code_block("code", "value = {{ }}")])++ with patch("skyvern.forge.sdk.workflow.service.app") as mock_app:+ mock_app.DATABASE.workflows.create_workflow = create+ with pytest.raises(CodeBlockTemplateSyntaxError):+ await service.create_workflow(+ organization_id="org_test",+ title="Invalid code template",+ workflow_definition=definition,+ encrypt_secrets=False,+ )++ create.assert_not_awaited()+++async def test_update_workflow_rejects_before_database_write() -> None:+ service = WorkflowService()+ update = AsyncMock()+ definition = _definition([_code_block("code", "value = {{ }}")])++ with patch("skyvern.forge.sdk.workflow.service.app") as mock_app:+ mock_app.DATABASE.organizations.get_organization = AsyncMock(return_value=None)+ mock_app.DATABASE.workflows.update_workflow_and_reconcile_definition_params = update+ with pytest.raises(CodeBlockTemplateSyntaxError):+ await service.update_workflow_definition(+ workflow_id="wf_test",+ organization_id="org_test",+ workflow_definition=definition,+ )++ update.assert_not_awaited()+++async def test_ephemeral_update_can_bypass_code_template_validation() -> None:+ service = WorkflowService()+ saved = MagicMock()+ saved.organization_id = "org_test"+ update = AsyncMock(return_value=saved)+ definition = _definition([_code_block("code", "value = {{ }}")])++ with patch("skyvern.forge.sdk.workflow.service.app") as mock_app:+ mock_app.DATABASE.organizations.get_organization = AsyncMock(return_value=None)+ mock_app.DATABASE.workflows.update_workflow_and_reconcile_definition_params = update+ result = await service.update_workflow_definition(+ workflow_id="wf_test",+ organization_id="org_test",+ workflow_definition=definition,+ validate_code_block_templates=False,+ notify_workflow_saved=False,+ )++ assert result is saved+ update.assert_awaited_once()diff --git a/tests/unit/workflow/test_execute_single_block_phases.py b/tests/unit/workflow/test_execute_single_block_phases.pyindex 004e89fbe..891f46132 100644--- a/tests/unit/workflow/test_execute_single_block_phases.py+++ b/tests/unit/workflow/test_execute_single_block_phases.py@@ -22,6 +22,7 @@ from skyvern.forge import appfrom skyvern.forge.sdk.db.enums import BrowserSeedSourcefrom skyvern.forge.sdk.workflow.models.block import (BranchCondition,+ CodeBlock,ConditionalBlock,JinjaBranchCriteria,LoginBlock,@@ -73,6 +74,15 @@ def _login_block(label: str, url: str) -> LoginBlock:)+def _code_block(label: str, *, continue_on_failure: bool = False) -> CodeBlock:+ return CodeBlock(+ label=label,+ code="pass",+ continue_on_failure=continue_on_failure,+ output_parameter=_output_parameter(f"{label}_output"),+ )++def _workflow(run_with: str = "agent") -> MagicMock:workflow = MagicMock()workflow.run_with = run_with@@ -465,6 +475,63 @@ async def test_block_terminal_outcome_cannot_overwrite_concurrent_cancellation(conditional_terminal.assert_awaited_once()+async def test_code_render_failure_stops_despite_continue_on_failure(monkeypatch: pytest.MonkeyPatch) -> None:+ service = WorkflowService()+ block = _code_block("code", continue_on_failure=True)+ block_result = BlockResult(+ success=False,+ failure_reason="Failed to format CodeBlock parameters.",+ output_parameter=block.output_parameter,+ status=BlockStatus.failed,+ can_continue_after_failure=False,+ )+ failed_run = MagicMock()+ mark_failed = AsyncMock(return_value=failed_run)+ monkeypatch.setattr(service, "mark_workflow_run_as_failed_if_not_final", mark_failed)++ workflow_run, should_stop = await service._handle_block_result_status(+ block=block,+ block_idx=0,+ blocks_cnt=1,+ block_result=block_result,+ workflow_run=_workflow_run(),+ workflow_run_id="wr_test",+ )++ assert workflow_run is failed_run+ assert should_stop is True+ mark_failed.assert_awaited_once()+++async def test_ordinary_code_failure_still_honors_continue_on_failure(monkeypatch: pytest.MonkeyPatch) -> None:+ service = WorkflowService()+ block = _code_block("code", continue_on_failure=True)+ block_result = BlockResult(+ success=False,+ failure_reason="Code failed after execution.",+ output_parameter=block.output_parameter,+ status=BlockStatus.failed,+ )+ mark_failed = AsyncMock()+ monkeypatch.setattr(service, "mark_workflow_run_as_failed_if_not_final", mark_failed)+ running = _workflow_run()++ workflow_run, should_stop = await service._handle_block_result_status(+ block=block,+ block_idx=0,+ blocks_cnt=1,+ block_result=block_result,+ workflow_run=running,+ workflow_run_id="wr_test",+ )++ assert workflow_run is running+ assert should_stop is False+ mark_failed.assert_not_awaited()++@pytest.mark.asyncioasync def test_block_exception_marks_run_failed_with_block_type_reason(monkeypatch: pytest.MonkeyPatch) -> None:service = WorkflowService()diff --git a/uv.lock b/uv.lockindex d591b78d3..2c2bae3cc 100644--- a/uv.lock+++ b/uv.lock@@ -6840,7 +6840,7 @@ wheels = [[[package]]name = "skyvern"-version = "1.0.53"+version = "1.0.52"source = { editable = "." }dependencies = [{ name = "cachetools" },