Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Revert #14581: restore scout-before-code; credential-ID fix returns after the interaction is fixed (#8090)
Andrew Neilson3c3a10835d71
diff --git a/docs/integrations/cli.mdx b/docs/integrations/cli.mdxindex 2a15ba679..87bd9b0db 100644--- a/docs/integrations/cli.mdx+++ b/docs/integrations/cli.mdx@@ -22,12 +22,15 @@ The `skyvern` CLI gives you direct access to browser automation, agent managemenpip install skyvern```-Set your API key:+Set your API key and target:```bashexport SKYVERN_API_KEY="YOUR_KEY" # get one at https://app.skyvern.com+export SKYVERN_BASE_URL="https://api.skyvern.com" # or your staging / self-hosted URL```+With an API key present, the CLI refuses to fall back to the production URL silently — `SKYVERN_BASE_URL` must name your target explicitly.+Optionally run the interactive setup wizard to configure your environment:```bashdiff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsxindex 8e5a9f30f..7abb333f5 100644--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx@@ -25,6 +25,7 @@ import {import { createPortal } from "react-dom";import { stringify as convertToYAML } from "yaml";import { useWorkflowHasChangesStore } from "@/store/WorkflowHasChangesStore";+import { useWorkflowTitleStore } from "@/store/WorkflowTitleStore";import { useCopilotActionStore } from "@/store/useCopilotActionStore";import { useCopilotHeaderStore } from "@/store/useCopilotHeaderStore";import { usePasteSkillHintStore } from "@/store/usePasteSkillHintStore";@@ -53,6 +54,7 @@ import {WorkflowCopilotTurnStartUpdate,WorkflowCopilotWorkflowDraftUpdate,WorkflowCopilotCredentialRequiredUpdate,+ WorkflowCopilotTitleUpdate,WorkflowCopilotChatSender,WorkflowCopilotChatRequest,WorkflowCopilotChatSummary,@@ -394,6 +396,7 @@ type WorkflowCopilotSsePayload =| WorkflowCopilotDesignStartUpdate| WorkflowCopilotDesignEndUpdate| WorkflowCopilotWorkflowDraftUpdate+ | WorkflowCopilotTitleUpdate| WorkflowCopilotCredentialRequiredUpdate;// The live pause frame is a structural superset of the card's frame; only@@ -552,6 +555,10 @@ function RunLifecycleLine({ content }: { content: string }) {export type WorkflowUpdateOptions = {persisted?: boolean;applied?: boolean;+ // A mid-turn draft lands while the user may be renaming the agent, so its title is+ // applied only if nothing has named it yet. Discrete applies (accept, snap-back)+ // are authoritative and keep the force path.+ midTurnDraft?: boolean;};interface WorkflowCopilotChatProps {@@ -2338,6 +2345,19 @@ export function WorkflowCopilotChat({}}return false;+ case "title_update":+ // The title store is shared across workflow swaps in one Workspace, and a+ // stream survives the swap — so a frame from the workflow we left must not+ // name the one we are now looking at.+ if (payload.workflow_permanent_id !== workflowPermanentId) {+ return false;+ }+ // Backend already persisted it; reload reads canonical. This only+ // moves the live title bar, and never over a user-chosen name.+ useWorkflowTitleStore+ .getState()+ .setTitleFromCopilotIfDefault(payload.title);+ return false;case "credential_required":setLivePauseFrame(payload);return false;@@ -2372,7 +2392,9 @@ export function WorkflowCopilotChat({// succeeds — a swallowed update would otherwise trigger a// spurious snap-back at terminal.if (payload.workflow) {- const applied = applyWorkflowUpdate(payload.workflow);+ const applied = applyWorkflowUpdate(payload.workflow, {+ midTurnDraft: true,+ });if (applied) {const turnId = latestTurnId.current;if (turnId) {diff --git a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.tsindex b8a3f36d4..09f91ec29 100644--- a/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts+++ b/skyvern-frontend/src/routes/workflows/copilot/workflowCopilotTypes.ts@@ -111,6 +111,7 @@ export type WorkflowCopilotStreamMessageType =| "design_start"| "design_end"| "workflow_draft"+ | "title_update"| "credential_required";export interface WorkflowCopilotProcessingUpdate {@@ -178,6 +179,16 @@ export interface WorkflowCopilotWorkflowDraftUpdate {workflow?: WorkflowApiResponse | null;}+// Emitted once the backend has persisted a derived agent name, before any block+// exists. Clients must not treat it as authoritative over a user-chosen title.+export interface WorkflowCopilotTitleUpdate {+ type: "title_update";+ turn_id: string;+ workflow_permanent_id: string;+ title: string;+ timestamp: string;+}+// Mid-build pause frame: the turn stays open (SSE alive) while the client// surfaces a credential card. reason stays a raw string here — CredentialCard// tolerates unknown reason tokens, so a newer backend can't break the wiring.diff --git a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsxindex 32f288cc5..666a595ed 100644--- a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx+++ b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx@@ -1599,7 +1599,11 @@ function Workspace({const applyWorkflowUpdate = (workflowData: WorkflowVersion,- options?: { persisted?: boolean; userDriven?: boolean },+ options?: {+ persisted?: boolean;+ userDriven?: boolean;+ midTurnDraft?: boolean;+ },) => {const settings: WorkflowSettings = {proxyLocation: workflowData.proxy_location ?? ProxyLocation.Residential,@@ -1650,9 +1654,16 @@ function Workspace({useWorkflowParametersStore.getState().setParameters(initialParameters);// Sync title so snap-back on Reject reverts the editor's title bar- // alongside the canvas blocks.+ // alongside the canvas blocks. A mid-turn draft is not authoritative: it must+ // not clobber a rename made while the turn was running, and a draft still+ // carrying the placeholder must not mark the title as generated.if (typeof workflowData.title === "string") {- useWorkflowTitleStore.getState().setTitle(workflowData.title);+ const titleStore = useWorkflowTitleStore.getState();+ if (options?.midTurnDraft) {+ titleStore.setTitleFromCopilotIfDefault(workflowData.title);+ } else {+ titleStore.syncTitleFromWorkflow(workflowData.title);+ }}if (options?.persisted) {diff --git a/skyvern-frontend/src/store/WorkflowTitleStore.ts b/skyvern-frontend/src/store/WorkflowTitleStore.tsindex 167b75e1f..92274a682 100644--- a/skyvern-frontend/src/store/WorkflowTitleStore.ts+++ b/skyvern-frontend/src/store/WorkflowTitleStore.ts@@ -1,6 +1,16 @@import { create } from "zustand";const DEFAULT_WORKFLOW_TITLE = "New Agent" as const;+// "New Workflow" is the backend's placeholder; both mean "nobody has named this yet".+const DEFAULT_WORKFLOW_TITLES: readonly string[] = [+ DEFAULT_WORKFLOW_TITLE,+ "New Workflow",+];++// "" is the pre-hydration state, before initializeTitle runs — an unnamed agent for+// every purpose here, so a title arriving mid-hydration is applied rather than dropped.+const isDefaultTitle = (title: string) =>+ title.trim() === "" || DEFAULT_WORKFLOW_TITLES.includes(title.trim());type WorkflowTitleStore = {title: string;@@ -8,6 +18,8 @@ type WorkflowTitleStore = {isNewTitle: () => boolean;setTitle: (title: string) => void;setTitleFromGeneration: (title: string) => void;+ setTitleFromCopilotIfDefault: (title: string) => void;+ syncTitleFromWorkflow: (title: string) => void;initializeTitle: (title: string) => void;resetTitle: () => void;};@@ -17,18 +29,42 @@ const useWorkflowTitleStore = create<WorkflowTitleStore>((set, get) => {title: "",titleHasBeenGenerated: false,isNewTitle: () => {- return get().title === DEFAULT_WORKFLOW_TITLE;+ return isDefaultTitle(get().title);},setTitle: (title: string) => {set({ title: title.trim(), titleHasBeenGenerated: true });},+ // Automatic canvas syncs (canonical load, snap-back) replay a snapshot that can+ // carry the placeholder the agent has since been named past. Nothing automatic may+ // un-name an agent; only the user's own rename can.+ syncTitleFromWorkflow: (title: string) => {+ if (isDefaultTitle(title) && !isDefaultTitle(get().title)) {+ return;+ }+ set({+ title: title.trim(),+ titleHasBeenGenerated: !isDefaultTitle(title),+ });+ },setTitleFromGeneration: (title: string) => {set({ title: title.trim(), titleHasBeenGenerated: true });},initializeTitle: (title: string) => {set({title: title.trim(),- titleHasBeenGenerated: title.trim() !== DEFAULT_WORKFLOW_TITLE,+ titleHasBeenGenerated: !isDefaultTitle(title),+ });+ },+ // A Copilot push must not overwrite a name the user chose, and a push that still+ // carries a placeholder must not count as "generated" — that flag permanently+ // disarms the auto-titler.+ setTitleFromCopilotIfDefault: (title: string) => {+ if (!isDefaultTitle(get().title)) {+ return;+ }+ set({+ title: title.trim(),+ titleHasBeenGenerated: !isDefaultTitle(title),});},resetTitle: () => {diff --git a/skyvern/_cli_bootstrap.py b/skyvern/_cli_bootstrap.pyindex af4dcaf8e..9404f60e0 100644--- a/skyvern/_cli_bootstrap.py+++ b/skyvern/_cli_bootstrap.py@@ -8,6 +8,19 @@ if TYPE_CHECKING:_DEFAULT_CLI_LOG_LEVEL = "WARNING"+_CLI_RUNTIME_PREPARED = False++CLOUD_URL_OPT_IN_MESSAGE = (+ "Refusing to send an API key to the default production URL. "+ "Set SKYVERN_BASE_URL to the base URL of the Skyvern deployment that issued this API key."+)+++def is_cli_runtime() -> bool:+ """True only in processes that entered through a CLI command; API servers and temporal workers never set this."""+ return _CLI_RUNTIME_PREPARED++_QUIET_CLI_LOGGERS = ("skyvern", "httpx", "litellm", "playwright", "httpcore")_RUNTIME_LOGGING_CONFIGURED = False_SILENCED_CLI_LOGGERS = ("posthog",)@@ -54,8 +67,11 @@ def configure_cli_runtime_logging() -> None:def prepare_cli_runtime(intent: "EnvIntent | str") -> Path:"""Load intent-scoped env files, then configure logging that imports settings."""+ global _CLI_RUNTIME_PREPARED+from skyvern.utils.env_paths import load_backend_env_files # noqa: PLC0415env_path = load_backend_env_files(intent=intent)configure_cli_runtime_logging()+ _CLI_RUNTIME_PREPARED = Truereturn env_pathdiff --git a/skyvern/browser_extension/extension/manifest.json b/skyvern/browser_extension/extension/manifest.jsonindex 77686c502..a0afb7d85 100644--- a/skyvern/browser_extension/extension/manifest.json+++ b/skyvern/browser_extension/extension/manifest.json@@ -4,7 +4,7 @@"version": "0.1.0","description": "Connects explicitly shared Chrome tabs to a local Skyvern browser session.","minimum_chrome_version": "125",- "key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoSTVDWJdVfrESGVbDh+BU9SLzY0aSnGb3HmW8ZnlRm43Mo9NmIoU4zhB/pbVcoK//0QMIiGzj22242cdwCfcl7X70bJHWWYG33US9FOTPdkxqviv73Jgi0z+XM/TDGe1dUU0ztz1QsfuMFu2MT9Yk1AT5r1507oERuNXo32BzNr5V7S/Ja8OZeRlYW4x7m3hA+OAQiU/0jE2FfuJVa/xB5Gm54LpxpJ7RxM421EZShp6yJyFxxgAEEMiFuNGXfNVY6kMLlpsQ1QiKTo+8I8RJ4DLcb2/bKteCk1U0JqoV+X9nj5sZBA7UmkBmp2+HTkCEIAlKgV+fUjoi3QNrXqPOQIDAQAB",+ "key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwePOj+WEmgtKAfyF3PkP6UHuWMvHhJgt0K+53fM9I/WNgu2N4YwCqYjD0h5fr4wCZfwiElUfgwxjDBHwX/Re/Y4G7WlSEG4lDTFNSRGQczHYQnerh8NHR3Gtpq59WKtRxzbbl/e1sVxab6ZyvKjiPZlbmjL44455SldRDPBNJ/5XiSflRkgV1hJ/OOdsVT4ghzjRTJsFhFTK8X0LeMupnYbjgcLb6he0ox+dX/eeDlRt3Sq5uu9+QfJg93/yOo55c0NAa7AW+1+r37GfAtezbqk3fNSraNFfhH+GQhnA6d/KPKTC7Ut2SH1Y8NYHh1yDkyYVr2bq4T6Twr/H0YgL/wIDAQAB","permissions": ["debugger","tabs",diff --git a/skyvern/browser_extension/package_extension.py b/skyvern/browser_extension/package_extension.pynew file mode 100644index 000000000..f0bee275b--- /dev/null+++ b/skyvern/browser_extension/package_extension.py@@ -0,0 +1,54 @@+from __future__ import annotations++import argparse+import json+import zipfile+from pathlib import Path++EXTENSION_DIR = Path(__file__).with_name("extension")+DEFAULT_OUTPUT_PATH = Path("tmp/skyvern-agent-extension.zip")+# fixed metadata so identical sources produce byte-identical archives+_ZIP_EPOCH = (1980, 1, 1, 0, 0, 0)+_ZIP_FILE_MODE = 0o644 << 16+++def _normalized_entry(archive_name: str) -> zipfile.ZipInfo:+ info = zipfile.ZipInfo(archive_name, date_time=_ZIP_EPOCH)+ info.external_attr = _ZIP_FILE_MODE+ info.compress_type = zipfile.ZIP_DEFLATED+ return info+++def package_extension(output_path: str | Path = DEFAULT_OUTPUT_PATH) -> Path:+ output_path = Path(output_path).expanduser().resolve()+ output_path.parent.mkdir(parents=True, exist_ok=True)++ source_files = sorted(path for path in EXTENSION_DIR.rglob("*") if path.is_file())+ with zipfile.ZipFile(output_path, "w", compression=zipfile.ZIP_DEFLATED) as archive:+ for source_path in source_files:+ relative_path = source_path.relative_to(EXTENSION_DIR)+ if relative_path == Path("README.md") or source_path.resolve() == output_path:+ continue+ if relative_path == Path("manifest.json"):+ manifest = json.loads(source_path.read_text())+ manifest.pop("key", None)+ content = (json.dumps(manifest, indent=2) + "\n").encode()+ else:+ content = source_path.read_bytes()+ archive.writestr(_normalized_entry(relative_path.as_posix()), content)++ with zipfile.ZipFile(output_path) as archive:+ packaged_manifest = json.loads(archive.read("manifest.json"))+ assert "key" not in packaged_manifest+ return output_path+++def main() -> None:+ parser = argparse.ArgumentParser(description="Build the Skyvern Agent Chrome Web Store upload zip.")+ parser.add_argument("output", nargs="?", type=Path, default=DEFAULT_OUTPUT_PATH)+ args = parser.parse_args()+ print(package_extension(args.output))+++if __name__ == "__main__":+ main()diff --git a/skyvern/browser_extension/protocol.py b/skyvern/browser_extension/protocol.pyindex 5c13789aa..918272949 100644--- a/skyvern/browser_extension/protocol.py+++ b/skyvern/browser_extension/protocol.py@@ -8,7 +8,7 @@ from urllib.parse import urlsplitfrom skyvern.browser_extension.errors import BrowserExtensionErrorPROTOCOL_VERSION = 1-EXTENSION_ID = "fmamdhmfeihjjaiheideemihnbpnokin"+EXTENSION_ID = "dhommdmblflboaledbbfkdaapkadphlp"ALLOWED_OPS = frozenset({diff --git a/skyvern/cli/commands/browser.py b/skyvern/cli/commands/browser.pyindex 7d069312c..013da72dc 100644--- a/skyvern/cli/commands/browser.py+++ b/skyvern/cli/commands/browser.py@@ -21,6 +21,7 @@ if TYPE_CHECKING:import typer+from skyvern._cli_bootstrap import prepare_cli_runtimefrom skyvern.browser_extension.auth import load_or_create_pairing_tokenfrom skyvern.browser_extension.errors import BrowserExtensionErrorfrom skyvern.browser_extension.runtime import BrowserExtensionRuntime@@ -68,6 +69,7 @@ from skyvern.cli.lazy import SkyvernTyperGroupfrom skyvern.cli.mcp_tools.browser import skyvern_login as tool_loginfrom skyvern.cli.mcp_tools.browser import skyvern_run_task as tool_run_taskfrom skyvern.cli.mcp_tools.inspection import skyvern_har_start, skyvern_har_stop+from skyvern.utils.env_paths import EnvIntentbrowser_app = typer.Typer(cls=SkyvernTyperGroup, help="Browser automation commands.", no_args_is_help=True)session_app = typer.Typer(cls=SkyvernTyperGroup, help="Manage browser sessions.", no_args_is_help=True)@@ -82,6 +84,12 @@ browser_app.add_typer(storage_app, name="storage")browser_app.add_typer(network_app, name="network")+@browser_app.callback()+def browser_callback() -> None:+ """Load environment and mark the CLI runtime so credential-bearing clients hit the base-URL guard."""+ prepare_cli_runtime(intent=EnvIntent.CLOUD)++@dataclass(frozen=True)class ConnectionTarget:mode: Literal["cloud", "cdp"]diff --git a/skyvern/cli/core/client.py b/skyvern/cli/core/client.pyindex afb65e92b..37cd0659c 100644--- a/skyvern/cli/core/client.py+++ b/skyvern/cli/core/client.py@@ -9,6 +9,7 @@ from threading import RLockimport httpximport structlog+from skyvern._cli_bootstrap import CLOUD_URL_OPT_IN_MESSAGE, is_cli_runtimefrom skyvern.client import SkyvernEnvironmentfrom skyvern.config import settingsfrom skyvern.constants import SKYVERN_MCP_USER_AGENT@@ -64,7 +65,15 @@ def _resolve_self_base_url() -> str:def _build_cloud_client(api_key: str) -> Skyvern:from .session_manager import is_stateless_http_mode # noqa: PLC0415 — circular import- base_url: str | None = _resolve_self_base_url() if is_stateless_http_mode() else _resolve_base_url()+ if is_stateless_http_mode():+ base_url: str | None = _resolve_self_base_url()+ else:+ # Guard is CLI-scoped on purpose: prod temporal workers run with SKYVERN_BASE_URL+ # unset and reach this factory via copilot self-heal — refusing unconditionally+ # here is a production outage, not a hardening.+ if is_cli_runtime() and api_key != "PLACEHOLDER" and not settings.is_skyvern_base_url_explicitly_configured:+ raise RuntimeError(CLOUD_URL_OPT_IN_MESSAGE)+ base_url = _resolve_base_url()# Generated SDK methods send "x-user-agent": None when no per-call user_agent is given,# clobbering constructor-level headers in the merge; httpx client-level defaults survive# because None-valued headers are stripped before the request is sent (SKY-13333).diff --git a/skyvern/cli/credentials.py b/skyvern/cli/credentials.pyindex 9cf5f5573..e8eb59a92 100644--- a/skyvern/cli/credentials.py+++ b/skyvern/cli/credentials.py@@ -15,7 +15,7 @@ import osimport typerfrom rich.table import Table-from skyvern._cli_bootstrap import prepare_cli_runtime+from skyvern._cli_bootstrap import CLOUD_URL_OPT_IN_MESSAGE, prepare_cli_runtimefrom skyvern.client import Skyvernfrom skyvern.client.types.non_empty_credit_card_credential import NonEmptyCreditCardCredentialfrom skyvern.client.types.non_empty_password_credential import NonEmptyPasswordCredential@@ -46,17 +46,14 @@ def credentials_callback(ctx.obj["api_key"] = api_key-def _get_client(api_key: str | None = None) -> Skyvern:+def _get_client(api_key: str | None = None, *, action: str = "", json_mode: bool = False) -> Skyvern:"""Instantiate a Skyvern SDK client using environment variables."""prepare_cli_runtime(intent=EnvIntent.CLOUD)from skyvern.config import settings # noqa: PLC0415key = api_key or os.getenv("SKYVERN_API_KEY") or settings.SKYVERN_API_KEYif key != "PLACEHOLDER" and not settings.is_skyvern_base_url_explicitly_configured:- raise typer.BadParameter(- "Refusing to send an API key to the default production URL. "- "Set SKYVERN_BASE_URL=https://api.skyvern.com to explicitly opt in."- )+ output_error(CLOUD_URL_OPT_IN_MESSAGE, action=action, json_mode=json_mode)return Skyvern(base_url=settings.SKYVERN_BASE_URL, api_key=key)@@ -133,7 +130,7 @@ def add_credential(json_mode=json_output,)- client = _get_client(ctx.obj.get("api_key") if ctx.obj else None)+ client = _get_client(ctx.obj.get("api_key") if ctx.obj else None, action="credentials.add", json_mode=json_output)if credential_type == "password":username = require_interactive_or_flag(@@ -274,7 +271,7 @@ def list_credentials(json_output: bool = typer.Option(False, "--json", help="Output as JSON."),) -> None:"""List stored credentials (metadata only, never passwords)."""- client = _get_client(ctx.obj.get("api_key") if ctx.obj else None)+ client = _get_client(ctx.obj.get("api_key") if ctx.obj else None, action="credentials.list", json_mode=json_output)try:credentials = client.get_credentials(page=page, page_size=page_size)@@ -324,7 +321,7 @@ def get_credential(json_output: bool = typer.Option(False, "--json", help="Output as JSON."),) -> None:"""Show metadata for a single credential."""- client = _get_client(ctx.obj.get("api_key") if ctx.obj else None)+ client = _get_client(ctx.obj.get("api_key") if ctx.obj else None, action="credentials.get", json_mode=json_output)try:cred = client.get_credential(credential_id)@@ -388,7 +385,9 @@ def delete_credential(console.print("Aborted.")raise typer.Exit()- client = _get_client(ctx.obj.get("api_key") if ctx.obj else None)+ client = _get_client(+ ctx.obj.get("api_key") if ctx.obj else None, action="credentials.delete", json_mode=json_output+ )try:client.delete_credential(credential_id)diff --git a/skyvern/cli/tasks.py b/skyvern/cli/tasks.pyindex 121a25633..67a803452 100644--- a/skyvern/cli/tasks.py+++ b/skyvern/cli/tasks.py@@ -8,7 +8,7 @@ import osimport typerfrom rich.panel import Panel-from skyvern._cli_bootstrap import prepare_cli_runtime+from skyvern._cli_bootstrap import CLOUD_URL_OPT_IN_MESSAGE, prepare_cli_runtimefrom skyvern.client import Skyvernfrom skyvern.utils.env_paths import EnvIntent@@ -33,17 +33,14 @@ def tasks_callback(ctx.obj["api_key"] = api_key-def _get_client(api_key: str | None = None) -> Skyvern:+def _get_client(api_key: str | None = None, *, action: str = "", json_mode: bool = False) -> Skyvern:"""Instantiate a Skyvern SDK client using environment variables."""prepare_cli_runtime(intent=EnvIntent.CLOUD)from skyvern.config import settings # noqa: PLC0415key = api_key or os.getenv("SKYVERN_API_KEY") or settings.SKYVERN_API_KEYif key != "PLACEHOLDER" and not settings.is_skyvern_base_url_explicitly_configured:- raise typer.BadParameter(- "Refusing to send an API key to the default production URL. "- "Set SKYVERN_BASE_URL=https://api.skyvern.com to explicitly opt in."- )+ output_error(CLOUD_URL_OPT_IN_MESSAGE, action=action, json_mode=json_mode)return Skyvern(base_url=settings.SKYVERN_BASE_URL, api_key=key)@@ -70,7 +67,7 @@ def list_tasks(skyvern tasks list --workflow-run-id wr_abc123skyvern tasks list --workflow-run-id wr_abc123 --json"""- client = _get_client(ctx.obj.get("api_key") if ctx.obj else None)+ client = _get_client(ctx.obj.get("api_key") if ctx.obj else None, action="tasks.list", json_mode=json_output)try:tasks = _list_workflow_tasks(client, workflow_run_id)except Exception as e:diff --git a/skyvern/constants.py b/skyvern/constants.pyindex bf63cdf16..e65aea516 100644--- a/skyvern/constants.py+++ b/skyvern/constants.py@@ -138,3 +138,7 @@ DEFAULT_SCRIPT_RUN_ID = "default"# SkyvernPage constantsSKYVERN_PAGE_MAX_SCRAPING_RETRIES = 2++# Placeholder titles a workflow carries before anything names it. Two strings because+# the frontend and backend defaults drifted apart.+DEFAULT_WORKFLOW_TITLES = ("New Workflow", "New Agent")diff --git a/skyvern/forge/agent_functions.py b/skyvern/forge/agent_functions.pyindex efa3868a0..6152eff2a 100644--- a/skyvern/forge/agent_functions.py+++ b/skyvern/forge/agent_functions.py@@ -1612,6 +1612,7 @@ class AgentFunction:*,organization_id: str | None = None,workflow_run_id: str | None = None,+ browser_session_id: str | None = None,) -> bool:"""Solve and apply a reCAPTCHA token. OSS has no solver client."""return Falsediff --git a/skyvern/forge/failure_classifier.py b/skyvern/forge/failure_classifier.pyindex 7dc1d7a14..ef06f9a59 100644--- a/skyvern/forge/failure_classifier.py+++ b/skyvern/forge/failure_classifier.py@@ -153,6 +153,43 @@ def classify_from_failure_reason(})+ # 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:+ categories.append(+ {+ "category": "INFRASTRUCTURE_ERROR",+ "confidence_float": 0.95,+ "reason_code": "secure_codeblock_runner_internal",+ "reasoning": "Secure CodeBlock runner failed internally before completing",+ }+ )++ # The sandbox child died without delivering a result. Usually a pod/deploy fault (child OOM+ # and blocked operations get their own codes first), but user code can still self-terminate+ # the interpreter, so confidence stays below the unambiguous runner arms.+ if "secure codeblock sandbox process exited" in reason:+ categories.append(+ {+ "category": "INFRASTRUCTURE_ERROR",+ "confidence_float": 0.6,+ "reason_code": "secure_codeblock_sandbox_exited",+ "reasoning": "Secure CodeBlock sandbox child process died before completing",+ }+ )++ # Runner-slot contention, not a fault; the distinct reason_code keeps it separable from+ # real runner failures in analytics.+ if "codeblock runner is already executing another codeblock" in reason:+ categories.append(+ {+ "category": "INFRASTRUCTURE_ERROR",+ "confidence_float": 0.9,+ "reason_code": "secure_codeblock_runner_busy",+ "reasoning": "Secure CodeBlock runner was busy with another CodeBlock",+ }+ )+_is_timeout = "Timeout" in exc_name or "timeout" in reason_is_element_state_timeout = _is_timeout and bool(_ELEMENT_OPERATION_RE.search(reason))diff --git a/skyvern/forge/prompts/skyvern/workflow-copilot-turn-intent.j2 b/skyvern/forge/prompts/skyvern/workflow-copilot-turn-intent.j2index b88d18840..5495edeff 100644--- a/skyvern/forge/prompts/skyvern/workflow-copilot-turn-intent.j2+++ b/skyvern/forge/prompts/skyvern/workflow-copilot-turn-intent.j2@@ -1,5 +1,5 @@Classify the latest Workflow Copilot turn. Return only JSON:-{mode, expected_output, required_context, confidence, target_entities, missing_context_question, reason_codes}.+{mode, expected_output, required_context, confidence, target_entities, missing_context_question, reason_codes, workflow_title}.Allowed modes: {{ mode_values }}.Allowed expected_output values: {{ expected_output_values }}.@@ -8,6 +8,8 @@ Allowed reason_codes values: {{ reason_code_values }}.Classify task shape only. RequestPolicy owns credential/raw-secret/testing policy and deterministic code owns authority.+workflow_title: a 2-5 word name for what this agent does, in title case, no trailing punctuation (e.g. "Weekly Visitor Report"). If the user states a name ("call it Invoice Bot"), return exactly that name. Return null unless the turn asks to build or change an agent, and null rather than guessing when the goal is not yet clear enough to name.+Modes: build=new or blank workflow task; edit=change existing workflow/proposal; diagnose=inspect/debug/continue from run evidence; docs_answer=respond inline when no workflow, browser, or run action is requested; draft_only=workflow change but no validation run; clarify=ambiguous target/change; refuse=unsupported request; unknown=insufficient signal.Context keys: current_workflow for existing YAML; latest_assistant_proposal/workflow_change for prior draft/proposal edits; latest_run_result for diagnose/continuation; credential_metadata when safe credential metadata is relevant; docs_context for docs_answer; browser_state when current page/session is required.diff --git a/skyvern/forge/sdk/api/files.py b/skyvern/forge/sdk/api/files.pyindex 357a9b4d9..496be9a48 100644--- a/skyvern/forge/sdk/api/files.py+++ b/skyvern/forge/sdk/api/files.py@@ -26,6 +26,7 @@ from skyvern.exceptions import (SkyvernHTTPException,)from skyvern.forge import app+from skyvern.forge.sdk.artifact.signing import parse_artifact_content_urlfrom skyvern.forge.sdk.core.aiohttp_helper import (SSRFGuardedResolver,ssrf_guarded_tcp_connector,@@ -367,6 +368,9 @@ async def resolve_local_or_download_file(if max_size_mb is not None and os.path.getsize(resolved) > max_size_mb * 1024 * 1024:raise DownloadFileMaxSizeExceeded(max_size_mb)return resolved+ parsed = parse_artifact_content_url(file_url, settings.SKYVERN_BASE_URL)+ if parsed is not None:+ file_url = await app.ARTIFACT_MANAGER.remint_content_url_if_unverified(parsed, organization_id) or file_urlreturn await download_file(file_url, max_size_mb=max_size_mb, organization_id=organization_id)diff --git a/skyvern/forge/sdk/artifact/manager.py b/skyvern/forge/sdk/artifact/manager.pyindex 8e669a867..b337d729f 100644--- a/skyvern/forge/sdk/artifact/manager.py+++ b/skyvern/forge/sdk/artifact/manager.py@@ -17,10 +17,12 @@ from skyvern.forge.sdk.api.files import create_named_temporary_filefrom skyvern.forge.sdk.artifact.models import Artifact, ArtifactType, LogEntityTypefrom skyvern.forge.sdk.artifact.signing import (ARTIFACT_URL_EXPIRY_SECONDS,+ ParsedArtifactContentUrl,artifact_url_expiry_seconds_for_type,effective_artifact_url_expiry_seconds,parse_keyring,sign_artifact_url,+ verify_artifact_signature,)from skyvern.forge.sdk.artifact.utils import replace_file_extensionfrom skyvern.forge.sdk.core import skyvern_context@@ -1402,6 +1404,52 @@ class ArtifactManager:)return await app.STORAGE.get_share_link(artifact)+ async def remint_content_url_if_unverified(+ self, parsed: ParsedArtifactContentUrl, organization_id: str | None+ ) -> str | None:+ """Re-sign a first-party artifact content URL whose signature is missing or does not verify.++ ``sig`` is a 43-character capability token that has to survive the whole workflow+ value plane verbatim; one dropped character turns a readable artifact into a 403+ (SKY-13575). The server can always re-derive it, so when the carried signature+ fails to verify — corrupted, absent, or expired — and the run's organization owns+ the artifact, hand back a freshly signed URL.++ Returns None when the signature still verifies or the URL names an artifact this+ organization cannot read; the caller then keeps the URL it already had.+ """+ if not organization_id:+ return None+ # With the keyring unset there is nothing to verify against, and the only first-party+ # content URLs still in circulation are legacy bundled rows — remint those unconditionally.+ if settings.ARTIFACT_CONTENT_HMAC_KEYRING and parsed.expiry and parsed.kid and parsed.sig:+ if verify_artifact_signature(+ artifact_id=parsed.artifact_id,+ expiry=parsed.expiry,+ kid=parsed.kid,+ sig=parsed.sig,+ keyring=parse_keyring(settings.ARTIFACT_CONTENT_HMAC_KEYRING),+ ):+ return None++ artifact = await app.DATABASE.artifacts.get_artifact_by_id(+ artifact_id=parsed.artifact_id,+ organization_id=organization_id,+ )+ if artifact is None:+ return None+ expiry_seconds = await self.resolve_artifact_url_expiry_seconds(organization_id)+ reminted = await self.resolve_share_url(artifact, expiry_seconds=expiry_seconds)+ if reminted is None:+ return None+ LOG.warning(+ "Re-signed a first-party artifact URL with a missing or unverifiable signature",+ artifact_id=parsed.artifact_id,+ organization_id=organization_id,+ signature_length=len(parsed.sig) if parsed.sig else 0,+ )+ return reminted+async def get_share_link(self, artifact: Artifact) -> str | None:"""Return a customer-facing URL for one artifact.diff --git a/skyvern/forge/sdk/artifact/signing.py b/skyvern/forge/sdk/artifact/signing.pyindex 15f23e44f..4c6baf7f6 100644--- a/skyvern/forge/sdk/artifact/signing.py+++ b/skyvern/forge/sdk/artifact/signing.py@@ -25,9 +25,11 @@ import base64import hashlibimport hmacimport json+import reimport timefrom functools import lru_cache-from urllib.parse import urlencode+from typing import NamedTuple+from urllib.parse import parse_qsl, urlencode, urlsplitfrom pydantic import BaseModel, model_validator@@ -69,6 +71,44 @@ ARTIFACT_URL_EXPIRY_SECONDS_MIN = 60 * 60 # 1 hourARTIFACT_URL_EXPIRY_SECONDS_MAX = 7 * 24 * 60 * 60 # 7 days_ARTIFACT_CONTENT_PATH_TEMPLATE = "/v1/artifacts/{artifact_id}/content"+_ARTIFACT_CONTENT_PATH_RE = re.compile(r"^/v1/artifacts/(?P<artifact_id>[^/]+)/content$")+++class ParsedArtifactContentUrl(NamedTuple):+ artifact_id: str+ expiry: str | None+ kid: str | None+ sig: str | None+++def parse_artifact_content_url(url: str, base_url: str) -> ParsedArtifactContentUrl | None:+ """Return the artifact id and signing params when ``url`` addresses ``base_url``'s own+ artifact content endpoint; None for every other URL.++ Scheme, host and any path prefix must match ``base_url`` exactly, so a customer-supplied+ look-alike URL is never mistaken for a first-party one.+ """+ try:+ parts = urlsplit(url)+ base_parts = urlsplit(base_url.rstrip("/"))+ except ValueError:+ return None+ if not base_parts.scheme or not base_parts.netloc:+ return None+ if (parts.scheme, parts.netloc) != (base_parts.scheme, base_parts.netloc):+ return None+ if not parts.path.startswith(base_parts.path):+ return None+ match = _ARTIFACT_CONTENT_PATH_RE.match(parts.path[len(base_parts.path) :])+ if not match:+ return None+ query = dict(parse_qsl(parts.query))+ return ParsedArtifactContentUrl(+ artifact_id=match.group("artifact_id"),+ expiry=query.get("expiry"),+ kid=query.get("kid"),+ sig=query.get("sig"),+ )def effective_artifact_url_expiry_seconds(per_org_value: int | None) -> int:diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.pyindex 215b59cfb..2e78c8dba 100644--- a/skyvern/forge/sdk/copilot/agent.py+++ b/skyvern/forge/sdk/copilot/agent.py@@ -12,7 +12,7 @@ import osimport reimport uuidfrom collections.abc import Callable, Iterator, Mapping, Sequence-from dataclasses import dataclass+from dataclasses import dataclass, fieldfrom datetime import datetime, timezonefrom pathlib import Pathfrom typing import TYPE_CHECKING, Any, Protocol, runtime_checkable@@ -34,6 +34,7 @@ from pydantic import ValidationErrorfrom skyvern.forge import appfrom skyvern.forge.prompts import prompt_enginefrom skyvern.forge.sdk.copilot import llm_config+from skyvern.forge.sdk.copilot.agent_naming import name_agent_and_publishfrom skyvern.forge.sdk.copilot.blocker_signal import (CopilotToolBlockerSignal,assert_clean_user_facing_text,@@ -350,6 +351,9 @@ class RequestPolicyGuardrailInputs:browser_session_id: str | None = Nonefix_origin: bool = Falsestored_completion_criteria: StoredCriteriaSnapshot | None = None+ # Unlike chat_history_messages, this is not truncated to the prompt window: a site is grounded+ # by the user having written it, which does not expire when the message leaves that window.+ prior_user_messages: list[WorkflowCopilotChatHistoryMessage] = field(default_factory=list)class CopilotRequestPolicyMissingError(Exception):@@ -4424,6 +4428,7 @@ def _build_copilot_input_guardrails(organization_id=policy_inputs.organization_id,handler=policy_inputs.request_policy_handler,config=getattr(ctx, "copilot_config", None) if isinstance(ctx, CopilotContext) else None,+ prior_user_messages=policy_inputs.prior_user_messages,)if isinstance(ctx, CopilotContext):turn_intent_classifier_result = None@@ -4804,6 +4809,7 @@ async def run_copilot_agent(api_key: str | None = None,security_rules: str = "",config: CopilotConfig | None = None,+ prior_user_messages: Sequence[WorkflowCopilotChatHistoryMessage] = (),turn_index: int | None = None,turn_id: str | None = None,prior_copilot_workflow_yaml: str | None = None,@@ -4843,6 +4849,7 @@ async def run_copilot_agent(config=config,turn_id=turn_id,turn_index=normalized_turn_index,+ prior_user_messages=prior_user_messages,prior_copilot_workflow_yaml=prior_copilot_workflow_yaml,prior_block_count=prior_block_count,ctx_sink=ctx_sink,@@ -4922,6 +4929,7 @@ async def _run_copilot_turn_impl(config: CopilotConfig | None,turn_id: str,turn_index: int,+ prior_user_messages: Sequence[WorkflowCopilotChatHistoryMessage] = (),prior_copilot_workflow_yaml: str | None = None,prior_block_count: int | None = None,ctx_sink: list[CopilotContext] | None = None,@@ -5022,6 +5030,7 @@ async def _run_copilot_turn_impl(workflow_yaml=safe_workflow_yaml,chat_history_text=safe_chat_history_text,chat_history_messages=list(chat_history),+ prior_user_messages=list(prior_user_messages),global_llm_context=safe_global_llm_context,organization_id=organization_id,request_policy_handler=await _resolve_request_policy_handler(@@ -5060,6 +5069,15 @@ async def _run_copilot_turn_impl(await emit_turn_start(stream, ctx)except Exception as emit_err:LOG.warning("copilot_narrative_turn_start_emit_failed", error=str(emit_err))+ # Name the agent before the build starts. Runs here because intent is resolved and the+ # stream is live, but no tool has authored anything yet — the title is a consequence of+ # the classification, not of whenever the model first happens to emit YAML.+ _, safe_workflow_yaml = await name_agent_and_publish(+ ctx,+ chat_request,+ safe_workflow_yaml,+ classifier_result=ctx.turn_intent_classifier_result,+ )request_policy = ctx.request_policy if isinstance(ctx.request_policy, RequestPolicy) else Noneif request_policy is not None:_store_turn_context_packet_on_context(@@ -5076,6 +5094,23 @@ async def _run_copilot_turn_impl(preflight_resolution = await preflight_credential_pause(ctx, stream, copilot_config)if preflight_resolution is not None:await _resume_turn_intent_after_preflight_credential(ctx, request_policy, policy_inputs)+ # The turn had no usable intent when naming first ran; it does now.+ resumed_title, safe_workflow_yaml = await name_agent_and_publish(+ ctx,+ chat_request,+ safe_workflow_yaml,+ classifier_result=ctx.turn_intent_classifier_result,+ )+ if resumed_title:+ # The packet was assembled from the pre-naming YAML.+ _store_turn_context_packet_on_context(+ ctx,+ request_policy=request_policy,+ chat_request=chat_request,+ chat_history=chat_history,+ debug_run_info_text=debug_run_info_text,+ prior_copilot_workflow_yaml=prior_copilot_workflow_yaml,+ )if preflight_resolution is None:return _build_request_policy_clarification_result(request_policy,diff --git a/skyvern/forge/sdk/copilot/agent_naming.py b/skyvern/forge/sdk/copilot/agent_naming.pynew file mode 100644index 000000000..9d3219c39--- /dev/null+++ b/skyvern/forge/sdk/copilot/agent_naming.py@@ -0,0 +1,124 @@+from __future__ import annotations++import structlog++from skyvern.constants import DEFAULT_WORKFLOW_TITLES+from skyvern.forge import app+from skyvern.forge.sdk.copilot.context import CopilotContext+from skyvern.forge.sdk.copilot.request_policy import RequestPolicy+from skyvern.forge.sdk.copilot.streaming_adapter import emit_title_update+from skyvern.forge.sdk.copilot.turn_intent import (+ MUTATING_CLASSIFIER_MODES,+ TurnIntent,+ TurnIntentClassifierResult,+ sanitize_workflow_title_candidate,+)+from skyvern.forge.sdk.copilot.workflow_yaml import with_workflow_yaml_title, workflow_yaml_title+from skyvern.forge.sdk.schemas.workflow_copilot import WorkflowCopilotChatRequest++LOG = structlog.get_logger()++# Naming follows authoring: the modes that may mutate the workflow are the ones+# whose stated goal is worth a name.+NAMING_MODES = MUTATING_CLASSIFIER_MODES+++def derive_agent_title(classifier_result: TurnIntentClassifierResult | None) -> str | None:+ """The classifier's title for this turn, or nothing.++ Deliberately never falls back to raw user text: the only filter available here is the+ same pattern scrub the policy layer already applies, so a credential shape it misses+ would land in a durably stored, org-visible title.+ """+ classification = classifier_result.classification if classifier_result is not None else None+ return sanitize_workflow_title_candidate(classification.workflow_title if classification else None)+++def _submitted_title_is_user_chosen(workflow_yaml: str | None) -> bool:+ title = workflow_yaml_title(workflow_yaml)+ return bool(title) and title not in DEFAULT_WORKFLOW_TITLES+++async def maybe_name_agent(+ ctx: CopilotContext,+ *,+ classifier_result: TurnIntentClassifierResult | None,+) -> str | None:+ """Name a still-unnamed agent from the turn's intent; returns the title if it renamed.++ Fails open in every direction: naming is a courtesy, never a precondition for building.+ """+ intent = ctx.turn_intent+ if not isinstance(intent, TurnIntent) or intent.mode not in NAMING_MODES:+ return None+ # An unsaved rename in the editor outranks anything derived here; canonical can still+ # read as default while the user has already typed a name they have not saved.+ if _submitted_title_is_user_chosen(ctx.workflow_yaml):+ return None+ # RequestPolicy owns ambiguous credential semantics and states them in two places:+ # raw_secret_detected is the pattern verdict, raw_secret_handling the classifier's.+ # A title outlives the turn, so either one withholds naming.+ policy = ctx.request_policy+ if isinstance(policy, RequestPolicy) and (policy.raw_secret_detected or policy.raw_secret_handling != "none"):+ return None++ classification = classifier_result.classification if classifier_result is not None else None+ title = derive_agent_title(classifier_result)+ if not title:+ return None++ try:+ renamed = await app.DATABASE.workflows.rename_workflow_if_still_default(+ workflow_id=ctx.workflow_id,+ workflow_permanent_id=ctx.workflow_permanent_id,+ organization_id=ctx.organization_id,+ title=title,+ )+ except Exception:+ LOG.warning(+ "copilot_agent_naming_persist_failed",+ workflow_permanent_id=ctx.workflow_permanent_id,+ exc_info=True,+ )+ return None++ if not renamed:+ LOG.info(+ "copilot_agent_naming_skipped",+ workflow_permanent_id=ctx.workflow_permanent_id,+ turn_intent_mode=intent.mode.value,+ )+ return None++ LOG.info(+ "copilot_agent_named",+ workflow_permanent_id=ctx.workflow_permanent_id,+ turn_intent_mode=intent.mode.value,+ from_classifier=bool(classification and classification.workflow_title),+ )+ return title+++async def name_agent_and_publish(+ ctx: CopilotContext,+ chat_request: WorkflowCopilotChatRequest,+ prompt_workflow_yaml: str,+ *,+ classifier_result: TurnIntentClassifierResult | None,+) -> tuple[str | None, str]:+ """Name the agent, show the model the name, and tell the client.++ Returns ``(title, prompt YAML)``; the caller must rebind its local to the returned YAML+ so the prompt carries the derived title.+ """+ title = await maybe_name_agent(ctx, classifier_result=classifier_result)+ if not title:+ return None, prompt_workflow_yaml++ ctx.workflow_yaml = with_workflow_yaml_title(ctx.workflow_yaml, title)+ chat_request.workflow_yaml = with_workflow_yaml_title(chat_request.workflow_yaml, title)+ try:+ await emit_title_update(ctx.stream, ctx, title)+ except Exception as emit_err:+ LOG.warning("copilot_title_update_emit_failed", error=str(emit_err))+ return title, with_workflow_yaml_title(prompt_workflow_yaml, title)diff --git a/skyvern/forge/sdk/copilot/blocker_signal.py b/skyvern/forge/sdk/copilot/blocker_signal.pyindex 2e09fba65..002e1123f 100644--- a/skyvern/forge/sdk/copilot/blocker_signal.py+++ b/skyvern/forge/sdk/copilot/blocker_signal.py@@ -339,7 +339,6 @@ _LOOP_PROGRESS_TOOLS = frozenset()_ACTIVE_TERMINAL_REPLACEABLE_REASON_CODES = frozenset({"tool_error_per_tool_budget_rerun"})_TERMINAL_CHALLENGE_REPLACEABLE_REASON_CODES = frozenset({"tool_error_post_budget_challenge_result_evidence"})-SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE = "tool_error_synthesized_block_persistence_required"SCHEMA_INCOMPATIBILITY_REASON_CODE = "schema_incompatibility"DISCOVERY_EXHAUSTED_NO_ENTRY_URL_REASON_CODE = "loop_detected_discovery_exhausted_no_entry_url"_OUTPUT_CONTRACT_TERMINAL_REASON_CODES = frozenset(@@ -385,12 +384,6 @@ def _should_stash_over_existing(and existing.internal_reason_code in _TERMINAL_CHALLENGE_REPLACEABLE_REASON_CODES):return True- if (- existing.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE- and not existing.renders_final_reply- and incoming.renders_final_reply- ):- return Truereturn Falsediff --git a/skyvern/forge/sdk/copilot/credential_pause.py b/skyvern/forge/sdk/copilot/credential_pause.pyindex 5b8a5fb90..9addab0e4 100644--- a/skyvern/forge/sdk/copilot/credential_pause.py+++ b/skyvern/forge/sdk/copilot/credential_pause.py@@ -285,7 +285,7 @@ def credential_pause_reason(ctx: Any) -> str | None:def credential_pause_would_fire(ctx: Any, copilot_config: CopilotConfig | None) -> bool:"""Synchronous subset of maybe_credential_pause's guards.- Called from both here and enforcement.py's _check_enforcement so the two+ Called from both here and enforcement.py's enforcement_decision so the twocan't drift: enforcement pre-empts hygiene nudges based on this sameprediction, and a client/kill-switch/latch mismatch between the two wouldeither suppress nudges for a pause that will never fire, or let a nudge@@ -350,7 +350,12 @@ def _apply_connected_credential_to_policy(ctx: Any, policy: RequestPolicy, credecredential_id=credential.credential_id,bound_origin=loggable_origin(admitted_url) if admitted_url else None,)- policy.resolved_credentials.append(credential)+ if credential.credential_id not in {resolved.credential_id for resolved in policy.resolved_credentials}:+ policy.resolved_credentials.append(credential)+ # The card answer is the user naming this credential, and it supersedes any earlier mention:+ # the fill seam's which-credential check is set equality, so adding instead of replacing would+ # refuse the very credential the user just picked.+ policy.current_turn_named_credential_ids = {credential.credential_id}policy.allow_run_blocks = Truepolicy.clarification_reason = "none"policy.allow_missing_credentials_in_draft = Falsediff --git a/skyvern/forge/sdk/copilot/enforcement.py b/skyvern/forge/sdk/copilot/enforcement.pyindex e51c9136b..4a46f3d9a 100644--- a/skyvern/forge/sdk/copilot/enforcement.py+++ b/skyvern/forge/sdk/copilot/enforcement.py@@ -10,7 +10,7 @@ import reimport timeimport uuidfrom collections.abc import Mapping, Sequence-from dataclasses import replace+from dataclasses import dataclass, replacefrom typing import TYPE_CHECKING, Anyfrom urllib.parse import urlparse@@ -22,7 +22,6 @@ from skyvern.config import settingsfrom skyvern.forge.sdk.copilot import config as copilot_config_defaultsfrom skyvern.forge.sdk.copilot import streaming_adapterfrom skyvern.forge.sdk.copilot.blocker_signal import (- SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE,CopilotToolBlockerSignal,clear_tool_blocker_signals_for_reason_codes,stash_blocker_signal,@@ -73,24 +72,6 @@ from skyvern.forge.sdk.copilot.composition_evidence import has_bounded_page_schefrom skyvern.forge.sdk.copilot.config import (DEFAULT_ENFORCEMENT_NUDGES,DEFAULT_TOKEN_BUDGET,- POST_ANTI_BOT_FAILED_TEST_NUDGE,- POST_DISCOVERY_ENTRYPOINT_URL_QUESTION_NUDGE,- POST_EXPLORE_WITHOUT_WORKFLOW_NUDGE,- POST_FAILED_TEST_INSPECT_FIRST_NUDGE,- POST_FAILED_TEST_NUDGE,- POST_NAVIGATE_NUDGE,- POST_NO_WORKFLOW_DELIVERY_NUDGE,- POST_NON_RETRIABLE_NAV_ERROR_STOP_NUDGE,- POST_PARAMETER_BINDING_STOP_NUDGE,- POST_PARAMETER_BINDING_WARN_NUDGE,- POST_PER_TOOL_BUDGET_NUDGE,- POST_PER_TOOL_BUDGET_STOP_NUDGE,- POST_REPEATED_FRONTIER_FAILURE_STOP_NUDGE,- POST_REPEATED_FRONTIER_FAILURE_WARN_NUDGE,- POST_SUSPICIOUS_SUCCESS_NUDGE,- POST_UPDATE_NUDGE,- PRE_DISCOVERY_URL_QUESTION_NUDGE,- SCREENSHOT_DROPPED_NUDGE,SYNTHESIZED_OFFER_REFRESH_STEP_THRESHOLD,BlockAuthoringPolicy,CopilotConfig,@@ -121,7 +102,6 @@ from skyvern.forge.sdk.copilot.output_extraction_plan import (value_shown_in_selectable_evidence,)from skyvern.forge.sdk.copilot.output_policy import (- completion_criterion_requires_browser_fill_delivery,normalize_response_scaffolding,)from skyvern.forge.sdk.copilot.output_utils import (@@ -169,7 +149,7 @@ from skyvern.forge.sdk.copilot.turn_halt import (raise_if_turn_halt,stash_turn_halt_from_blocker_signal,)-from skyvern.forge.sdk.copilot.turn_intent import RequiredContextKey, TurnIntent, TurnIntentMode+from skyvern.forge.sdk.copilot.turn_intent import TurnIntent, TurnIntentModefrom skyvern.forge.sdk.copilot.turn_ownership import (TurnClaimant,claim_turn,@@ -232,13 +212,6 @@ NUDGE_SENTINEL = "[copilot:nudge] "SCREENSHOT_PLACEHOLDER = SCREENSHOT_SENTINEL + "[prior screenshot removed to save context]"TOKEN_BUDGET = DEFAULT_TOKEN_BUDGETSYNTHESIZED_BLOCK_PERSISTENCE_TOOL = "update_and_run_blocks"-_SYNTHESIZED_BLOCK_PERSISTENCE_ALLOWED_TOOLS = frozenset(- {SYNTHESIZED_BLOCK_PERSISTENCE_TOOL, "fill_credential_field", "update_workflow"}-)-_ACTUATION_OBLIGATION_REQUIRED_FILL_TOOL = "type_text"-_SYNTHESIZED_BLOCK_PERSISTENCE_MUTATING_TOOLS = frozenset(- {"click", "press_key", "type_text", "select_option", "navigate_browser"}-)# Both tools re-author the workflow draft and clear the coverage-reopen flag; the steer must fire# for either or an update_workflow re-author silently spends the one-shot rescout._SYNTHESIZED_BLOCK_REAUTHORING_TOOLS = frozenset({SYNTHESIZED_BLOCK_PERSISTENCE_TOOL, "update_workflow"})@@ -1020,7 +993,7 @@ def recycle_admits_present_completion_contract_ask(ctx: CopilotContext) -> bool:return not ctx.has_genuine_workflow_attempt()-def _present_completion_contract_ask_retry(ctx: CopilotContext, parsed: dict[str, Any]) -> str | None:+def _present_completion_contract_ask_retry(ctx: CopilotContext, parsed: dict[str, Any]) -> EnforcementDecision | None:if parsed.get("type") != "ASK_QUESTION":return Noneask_subject = coerce_ask_subject(parsed.get("ask_subject"))@@ -1030,7 +1003,7 @@ def _present_completion_contract_ask_retry(ctx: CopilotContext, parsed: dict[strif _present_completion_contract_ask_admission_base(ctx):auto_answer = _typed_ask_subject_auto_answer(ctx, ask_subject, parsed)if auto_answer is not None:- return auto_answer+ return EnforcementDecision(rule="typed_ask_subject_auto_answer", message=auto_answer)retry_admitted = recycle_admits_present_completion_contract_ask(ctx)if ask_subject is not None:LOG.info(@@ -1047,7 +1020,10 @@ def _present_completion_contract_ask_retry(ctx: CopilotContext, parsed: dict[strturn_intent_mode=ctx.turn_intent.mode if ctx.turn_intent else None,**ctx.genuine_attempt_parity_fields(),)- return PRESENT_COMPLETION_CONTRACT_ASK_RETRY+ return EnforcementDecision(+ rule="present_completion_contract_ask_retry",+ message=PRESENT_COMPLETION_CONTRACT_ASK_RETRY,+ )def _typed_ask_subject_auto_answer(ctx: CopilotContext, ask_subject: AskSubject, parsed: dict[str, Any]) -> str | None:@@ -1111,6 +1087,23 @@ def _nudge(config: CopilotConfig | None, key: str) -> str:return config.nudge(key)+@dataclass(frozen=True)+class EnforcementDecision:+ """Which enforcement rule fired, and the text it renders to.++ ``rule`` is the stable identity because nudge prose is operator-configurable;+ most rules name a key in ``DEFAULT_ENFORCEMENT_NUDGES``, but the ask-retry+ rules carry a hardcoded message and are not resolvable through ``_nudge``.+ """++ rule: str+ message: str+++def _decision(config: CopilotConfig | None, key: str) -> EnforcementDecision:+ return EnforcementDecision(rule=key, message=_nudge(config, key))++def _goal_likely_needs_more_blocks(user_message: Any, block_count: int, completion_contract: str | None = None) -> bool:"""Return True when the goal likely requires more blocks than currently exist."""if block_count >= MIN_BLOCKS_FOR_AUTO_COMPLETE:@@ -1166,7 +1159,7 @@ def _pre_discovery_url_question_nudge(ctx: Any,parsed: dict[str, Any],config: CopilotConfig | None = None,-) -> str | None:+) -> EnforcementDecision | None:"""Steer the model to discovery when it asks before discovery has run.INITIAL/DISCOVERING phase with zero discovery calls means the model went@@ -1204,14 +1197,14 @@ def _pre_discovery_url_question_nudge(build_phase=getattr(getattr(ctx, "build_phase", None), "value", None),nudge_count=ctx.pre_discovery_url_question_nudge_count,)- return _nudge(config, "pre_discovery_url_question")+ return _decision(config, "pre_discovery_url_question")def _post_discovery_entrypoint_url_question_nudge(ctx: Any,parsed: dict[str, Any],config: CopilotConfig | None = None,-) -> str | None:+) -> EnforcementDecision | None:if parsed.get("type") != "ASK_QUESTION":return Nonecandidate_url = getattr(ctx, "resolved_discovery_entrypoint_url", None)@@ -1226,17 +1219,22 @@ def _post_discovery_entrypoint_url_question_nudge(if nudge_count >= MAX_DISCOVERY_ENTRYPOINT_URL_QUESTION_NUDGES:return Nonectx.discovery_entrypoint_url_question_nudge_count = nudge_count + 1- return f"{_nudge(config, 'post_discovery_entrypoint_url_question')} Resolved candidate_url: {candidate_url}"+ return EnforcementDecision(+ rule="post_discovery_entrypoint_url_question",+ message=f"{_nudge(config, 'post_discovery_entrypoint_url_question')} Resolved candidate_url: {candidate_url}",+ )-def _response_coverage_nudge(ctx: Any, parsed: dict[str, Any], config: CopilotConfig | None = None) -> str | None:- """Peek at the model's final output and return a nudge for coverage gaps+def _response_coverage_nudge(+ ctx: Any, parsed: dict[str, Any], config: CopilotConfig | None = None+) -> EnforcementDecision | None:+ """Peek at the model's final output and return a decision for coverage gapsor progress-narration format. ASK_QUESTION is let through so the agentcan request missing credentials or disambiguation, except when discoveryresolved a candidate and the agent has not yet inspected or composed fromthat candidate.- Returns the nudge string to inject, or None to let the response through.+ Returns the decision to inject, or None to let the response through."""response_type = parsed.get("type")pre_discovery_nudge = _pre_discovery_url_question_nudge(ctx, parsed, config)@@ -1262,7 +1260,7 @@ def _response_coverage_nudge(ctx: Any, parsed: dict[str, Any], config: CopilotConudge_count = getattr(ctx, "no_workflow_nudge_count", 0)if nudge_count < MAX_NO_WORKFLOW_NUDGES:ctx.no_workflow_nudge_count = nudge_count + 1- return _nudge(config, "post_no_workflow_delivery")+ return _decision(config, "post_no_workflow_delivery")workflow_tested_ok = (getattr(ctx, "last_test_ok", None) is True@@ -1283,13 +1281,13 @@ def _response_coverage_nudge(ctx: Any, parsed: dict[str, Any], config: CopilotConudge_count = getattr(ctx, "coverage_nudge_count", 0)if nudge_count < MAX_INTERMEDIATE_NUDGES:ctx.coverage_nudge_count = nudge_count + 1- return _nudge(config, "post_intermediate_success")+ return _decision(config, "post_intermediate_success")if _is_progress_narration(parsed.get("user_response")):nudge_count = getattr(ctx, "format_nudge_count", 0)if nudge_count < MAX_FORMAT_NUDGES:ctx.format_nudge_count = nudge_count + 1- return _nudge(config, "post_format")+ return _decision(config, "post_format")return None@@ -1365,7 +1363,7 @@ def _repair_obligation_blocks_finalize(ctx: AgentContext, result: RunResultStreadef _needs_failed_test_nudge(ctx: Any) -> bool:"""Return True when the last test failed and the agent hasn't iterated yet."""# A permanent nav error cannot be 'fix the workflow and retry' material —- # the dedicated non-retriable branch in _check_enforcement owns this case.+ # the dedicated non-retriable branch in enforcement_decision owns this case.if getattr(ctx, "last_test_non_retriable_nav_error", None):return Falseif getattr(ctx, "pending_reconciliation_requires_user_input", False) is True:@@ -1457,11 +1455,11 @@ def _get_int(ctx: Any, name: str, default: int = 0) -> int:return value if isinstance(value, int) else default-def _repeated_frontier_failure_nudge(ctx: Any, config: CopilotConfig | None = None) -> str | None:- """Emit each escalation level at most once per streak. The streak itself- keeps climbing on further identical failures (incremented elsewhere by- update_repeated_failure_state), so the stop nudge fires naturally on the- next repeat after a warn."""+def _repeated_frontier_failure_nudge(ctx: Any) -> str | None:+ """Return the nudge key for each escalation level, at most once per streak.+ The streak itself keeps climbing on further identical failures (incremented+ elsewhere by update_repeated_failure_state), so the stop nudge fires+ naturally on the next repeat after a warn."""# Non-retriable nav errors get their own dedicated stop path; don't let a# repeated-frontier nudge smuggle different retry advice past the gate.if getattr(ctx, "last_test_non_retriable_nav_error", None):@@ -1472,29 +1470,19 @@ def _repeated_frontier_failure_nudge(ctx: Any, config: CopilotConfig | None = Nois_param_binding = top_category == "PARAMETER_BINDING_ERROR"if streak >= REPEATED_FRONTIER_STREAK_STOP_AT and emitted < REPEATED_FRONTIER_STREAK_STOP_AT:- return _nudge(- config,- "post_parameter_binding_stop" if is_param_binding else "post_repeated_frontier_failure_stop",- )+ return "post_parameter_binding_stop" if is_param_binding else "post_repeated_frontier_failure_stop"if streak >= REPEATED_FRONTIER_STREAK_ESCALATE_AT and emitted < REPEATED_FRONTIER_STREAK_ESCALATE_AT:- return _nudge(- config,- "post_parameter_binding_warn" if is_param_binding else "post_repeated_frontier_failure_warn",- )+ return "post_parameter_binding_warn" if is_param_binding else "post_repeated_frontier_failure_warn"return None-def _is_stop_level_frontier_nudge(nudge: str, config: CopilotConfig | None = None) -> bool:- return nudge in {- _nudge(config, "post_repeated_frontier_failure_stop"),- _nudge(config, "post_parameter_binding_stop"),- }+STOP_LEVEL_FRONTIER_RULES = frozenset({"post_repeated_frontier_failure_stop", "post_parameter_binding_stop"})-def _non_retriable_nav_error_nudge(ctx: Any, config: CopilotConfig | None = None) -> tuple[str, str] | None:- """Emit POST_NON_RETRIABLE_NAV_ERROR_STOP_NUDGE at most once per distinct- non-retriable nav-error signature. Returns ``(nudge, signature)`` when it- should fire, ``None`` otherwise. Signature normalization is shared with+def _non_retriable_nav_error_nudge(ctx: Any) -> tuple[str, str] | None:+ """Fire the non-retriable nav-error stop at most once per distinct signature.+ Returns ``(rule, signature)`` when it should fire, ``None`` otherwise.+ Signature normalization is shared with`failure_tracking.compute_failure_signature`, so a cert error after a DNSerror (or vice versa) counts as a distinct signature and re-fires."""raw = getattr(ctx, "last_test_non_retriable_nav_error", None)@@ -1504,14 +1492,19 @@ def _non_retriable_nav_error_nudge(ctx: Any, config: CopilotConfig | None = Nonelast_emitted = getattr(ctx, "non_retriable_nav_error_last_emitted_signature", None)if signature == last_emitted:return None- return _nudge(config, "post_non_retriable_nav_error_stop"), signature+ return "post_non_retriable_nav_error_stop", signature-def _check_enforcement(+def enforcement_decision(ctx: Any,result: RunResultStreaming | None = None,config: CopilotConfig | None = None,-) -> str | None:+) -> EnforcementDecision | None:+ """Resolve which enforcement rule fires for this iteration, if any.++ The ladder below is ordered: the first rule whose condition holds wins, and+ that precedence is the contract callers depend on.+ """verified = outcome_fully_verified(ctx)# Terminal failure-mode signals must pre-empt tool-call hygiene nudges.terminal_signal = getattr(ctx, "latest_tool_blocker_signal", None) or getattr(ctx, "blocker_signal", None)@@ -1531,26 +1524,26 @@ def _check_enforcement(# A permanent navigation error (DNS / cert / SSL / invalid URL) cannot be# resolved by observing a prior navigate or by testing an updated# workflow against the same bad URL, so let it speak first.- non_retriable = _non_retriable_nav_error_nudge(ctx, config)+ non_retriable = _non_retriable_nav_error_nudge(ctx)if non_retriable is not None:- nudge_msg, signature = non_retriable+ rule, signature = non_retriablectx.non_retriable_nav_error_last_emitted_signature = signature- return nudge_msg+ return _decision(config, rule)if ctx.navigate_called and not ctx.observation_after_navigate and not ctx.navigate_enforcement_done:ctx.navigate_enforcement_done = True- return _nudge(config, "post_navigate")+ return _decision(config, "post_navigate")if _needs_explore_without_workflow_nudge(ctx):ctx.explore_without_workflow_nudge_count += 1- return _nudge(config, "post_explore_without_workflow")+ return _decision(config, "post_explore_without_workflow")if (ctx.update_workflow_calledand not ctx.test_after_update_doneand getattr(ctx, "allow_untested_workflow_draft", False) is not True):- return _nudge(config, "post_update")+ return _decision(config, "post_update")# Observing the reached page is the first repair move, not the last: while the typed contract# still says REPAIR, a reply that reports the failure instead of acting on it re-enters the loop.@@ -1564,7 +1557,7 @@ def _check_enforcement(# challenge-solving loop as a long-chain budgeting problem.if _needs_failed_test_nudge(ctx) and getattr(ctx, "last_test_anti_bot", None):ctx.failed_test_nudge_count += 1- return _nudge(config, "post_anti_bot_failed_test")+ return _decision(config, "post_anti_bot_failed_test")# A budget-trip without challenge evidence is a structural problem (chain# too long), not a workflow-shape problem — emit the targeted "split the@@ -1577,25 +1570,25 @@ def _check_enforcement(# (the shrunk frontier ALSO blew the budget) is a doomed shrinking-budget spiral on a# too-heavy page — finalize the verified prefix instead of re-running into less time.if prior >= 1:- return _nudge(config, "post_per_tool_budget_stop")- return _nudge(config, "post_per_tool_budget")+ return _decision(config, "post_per_tool_budget_stop")+ return _decision(config, "post_per_tool_budget")- repeated_frontier_nudge = _repeated_frontier_failure_nudge(ctx, config)- if repeated_frontier_nudge is not None:+ frontier_rule = _repeated_frontier_failure_nudge(ctx)+ if frontier_rule is not None:# Latch the emitted level so each escalation fires at most once per streak.ctx.repeated_failure_nudge_emitted_at_streak = (REPEATED_FRONTIER_STREAK_STOP_AT- if _is_stop_level_frontier_nudge(repeated_frontier_nudge, config)+ if frontier_rule in STOP_LEVEL_FRONTIER_RULESelse REPEATED_FRONTIER_STREAK_ESCALATE_AT)- return repeated_frontier_nudge+ return _decision(config, frontier_rule)# Do NOT clear last_test_suspicious_success here. tools._record_run_blocks_result# resets it on every new run; if the agent ignores the nudge and answers- # without rerunning, we want _check_enforcement to re-emit the nudge.+ # without rerunning, we want enforcement_decision to re-emit the nudge.if _needs_suspicious_success_nudge(ctx):ctx.suspicious_success_nudge_count = getattr(ctx, "suspicious_success_nudge_count", 0) + 1- return _nudge(config, "post_suspicious_success")+ return _decision(config, "post_suspicious_success")# Checked before the generic failed-test nudge so a scrape-wall streak# emits the specific STOP text and does not also consume a@@ -1603,14 +1596,14 @@ def _check_enforcement(if _needs_failed_test_nudge(ctx):ctx.failed_test_nudge_count += 1if _needs_inspect_before_repair_nudge(ctx):- return _nudge(config, "post_failed_test_inspect_first")- return _nudge(config, "post_failed_test")+ return _decision(config, "post_failed_test_inspect_first")+ return _decision(config, "post_failed_test")# Counters exhausted but the contract still says REPAIR: keep steering rather than finalize a# draft the build test disproved.if _repair_obligation_blocks_finalize(ctx, result):ctx.repair_obligation_nudge_count = _get_int(ctx, "repair_obligation_nudge_count") + 1- return _nudge(config, "post_failed_test")+ return _decision(config, "post_failed_test")# Response-time gate: peek at the model's final output to tell ASK_QUESTION# (always allowed) from a REPLY with a coverage gap or progress-narration.@@ -1919,28 +1912,6 @@ def _is_context_window_error(exc: BaseException) -> bool:)-_NUDGE_TYPE_BY_MESSAGE: dict[str, str] = {- POST_UPDATE_NUDGE: "post_update",- POST_NAVIGATE_NUDGE: "post_navigate",- POST_EXPLORE_WITHOUT_WORKFLOW_NUDGE: "explore_without_workflow",- POST_SUSPICIOUS_SUCCESS_NUDGE: "suspicious_success",- POST_REPEATED_FRONTIER_FAILURE_WARN_NUDGE: "repeated_frontier_failure_warn",- POST_REPEATED_FRONTIER_FAILURE_STOP_NUDGE: "repeated_frontier_failure_stop",- POST_NON_RETRIABLE_NAV_ERROR_STOP_NUDGE: "non_retriable_nav_error_stop",- POST_PARAMETER_BINDING_WARN_NUDGE: "parameter_binding_warn",- POST_PARAMETER_BINDING_STOP_NUDGE: "parameter_binding_stop",- POST_ANTI_BOT_FAILED_TEST_NUDGE: "anti_bot_block",- POST_PER_TOOL_BUDGET_NUDGE: "per_tool_budget_split",- POST_PER_TOOL_BUDGET_STOP_NUDGE: "per_tool_budget_stop",- POST_NO_WORKFLOW_DELIVERY_NUDGE: "no_workflow_delivery",- POST_DISCOVERY_ENTRYPOINT_URL_QUESTION_NUDGE: "discovery_entrypoint_url_question",- PRE_DISCOVERY_URL_QUESTION_NUDGE: "pre_discovery_url_question",- POST_FAILED_TEST_NUDGE: "post_failed_test",- POST_FAILED_TEST_INSPECT_FIRST_NUDGE: "post_failed_test_inspect_first",- SCREENSHOT_DROPPED_NUDGE: "screenshot_dropped_on_recovery",-}--_NUDGE_TYPE_BY_KEY: dict[str, str] = {"post_update": "post_update","post_navigate": "post_navigate",@@ -1962,17 +1933,13 @@ _NUDGE_TYPE_BY_KEY: dict[str, str] = {"screenshot_dropped": "screenshot_dropped_on_recovery","post_intermediate_success": "intermediate_success","post_format": "format",+ # Self-mapped so the table enumerates every emittable rule; these two carry a+ # hardcoded message and so have no nudge key to shorten.+ "present_completion_contract_ask_retry": "present_completion_contract_ask_retry",+ "typed_ask_subject_auto_answer": "typed_ask_subject_auto_answer",}-def _nudge_type_for_log(nudge: str, config: CopilotConfig | None = None) -> str:- nudge_by_key = config.enforcement_nudges if config is not None else DEFAULT_ENFORCEMENT_NUDGES- for key, value in nudge_by_key.items():- if value == nudge:- return _NUDGE_TYPE_BY_KEY.get(key, key)- return _NUDGE_TYPE_BY_MESSAGE.get(nudge, "intermediate_success")--def _strip_input_images(current_input: str | list) -> tuple[str | list, bool]:"""Replace ``input_image`` parts in *current_input* with a text placeholder.@@ -3129,151 +3096,6 @@ def _should_force_advisory_run_dispatch(ctx: Any) -> bool:return not blocker_signal_is_genuinely_terminal(getattr(ctx, "blocker_signal", None))-def _should_force_synthesized_block_persistence(ctx: Any) -> bool:- if getattr(ctx, "update_workflow_called", False) and not synthesized_persistence_reopened(ctx):- return False- if not _turn_intent_can_update_and_run_without_user_input(getattr(ctx, "turn_intent", None)):- return False- if normalize_block_authoring_policy(getattr(ctx, "block_authoring_policy", None)) != (- BlockAuthoringPolicy.CODE_ONLY_BROWSER- ):- return False- if not getattr(ctx, "synthesized_block_offered", False):- return False- trajectory = getattr(ctx, "scout_trajectory", None) or []- if (getattr(ctx, "synthesized_block_offered_trajectory_len", 0) or 0) != len(trajectory):- return False- if not getattr(ctx, "synthesized_block_offered_goal_complete", False):- return False- return synthesized_trajectory_is_goal_complete(ctx)---def _should_block_mutating_tool_after_synthesized_offer(ctx: Any, tool_name: str) -> bool:- if tool_name not in _SYNTHESIZED_BLOCK_PERSISTENCE_MUTATING_TOOLS:- return False- if _active_non_method_mandated_terminal_actions(ctx) or _active_floor_rekeyed_runtime_outputs(ctx):- if not synthesized_trajectory_is_goal_complete(ctx):- return False- else:- if uncovered_requested_output_paths(ctx):- return False- if _credential_flow_scout_gap_incomplete(ctx, getattr(ctx, "scout_trajectory", None) or []):- return False- if getattr(ctx, "update_workflow_called", False) and not synthesized_persistence_reopened(ctx):- return False- if not _turn_intent_can_update_and_run_without_user_input(getattr(ctx, "turn_intent", None)):- return False- if normalize_block_authoring_policy(getattr(ctx, "block_authoring_policy", None)) != (- BlockAuthoringPolicy.CODE_ONLY_BROWSER- ):- return False- if not getattr(ctx, "synthesized_block_offered", False):- return False- trajectory = getattr(ctx, "scout_trajectory", None) or []- return (getattr(ctx, "synthesized_block_offered_trajectory_len", 0) or 0) == len(trajectory)---def _ambiguous_bare_selector_repair_context(ctx: Any) -> Any | None:- repair_context = getattr(ctx, "last_code_authoring_repair_context", None)- if getattr(repair_context, "reason_code", None) != "ambiguous_bare_selector":- return None- if getattr(repair_context, "workflow_run_id", None):- return None- if getattr(ctx, "last_run_blocks_workflow_run_id", None):- return None- return repair_context---def _ambiguous_bare_selector_rescout_key(ctx: Any) -> str | None:- repair_context = _ambiguous_bare_selector_repair_context(ctx)- if repair_context is None:- return None- if getattr(repair_context, "refiner_selector", None):- return None- selector_alternatives = getattr(repair_context, "selector_alternatives", None)- if isinstance(selector_alternatives, list) and selector_alternatives:- return None- payload = {- "block_label": str(getattr(repair_context, "block_label", "") or ""),- "selector": str(getattr(repair_context, "selector", "") or ""),- "source_url": str(getattr(repair_context, "source_url", "") or ""),- }- return json.dumps(payload, sort_keys=True, separators=(",", ":"))---def _ambiguous_bare_selector_rescout_signal_state(ctx: Any, tool_name: str) -> str | None:- if tool_name != "evaluate":- return None- repair_context = _ambiguous_bare_selector_repair_context(ctx)- if repair_context is None:- return None- if getattr(repair_context, "refiner_selector", None):- return "block"- selector_alternatives = getattr(repair_context, "selector_alternatives", None)- if isinstance(selector_alternatives, list) and selector_alternatives:- return "block"- key = _ambiguous_bare_selector_rescout_key(ctx)- if key is None:- return None- if getattr(ctx, "ambiguous_bare_selector_rescout_context_key", None) == key:- return "block"- # Track the one allowed same-page rescout for this author-time repair context.- ctx.ambiguous_bare_selector_rescout_context_key = key- return "allow"---def _actuation_obligation_live_fill_delivery_required(ctx: CopilotContext) -> bool:- turn_intent = getattr(ctx, "turn_intent", None)- if (- not isinstance(turn_intent, TurnIntent)- or turn_intent.mode != TurnIntentMode.BUILD- or RequiredContextKey.BROWSER_STATE not in turn_intent.required_context- ):- return False- if normalize_block_authoring_policy(ctx.block_authoring_policy) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:- return False- request_policy = getattr(ctx, "request_policy", None)- criteria: list[CompletionCriterion] = []- if isinstance(request_policy, RequestPolicy):- criteria.extend(request_policy.completion_criteria)- turn_state = getattr(ctx, "completion_criteria_turn_state", None)- if turn_state is not None and turn_state.decision is not None:- criteria.extend(turn_state.decision.criteria)- if any(completion_criterion_requires_browser_fill_delivery(criterion) for criterion in criteria):- return True- trajectory = getattr(ctx, "scout_trajectory", None)- return trajectory_has_browser_fill_interaction(trajectory) if isinstance(trajectory, list) else False---def _actuation_obligation_required_fill_tool(ctx: CopilotContext) -> str | None:- if _actuation_obligation_live_fill_delivery_required(ctx):- return _ACTUATION_OBLIGATION_REQUIRED_FILL_TOOL- return None---def _actuation_obligation_admits_required_fill_tool(ctx: CopilotContext, tool_name: str) -> bool:- return tool_name == _actuation_obligation_required_fill_tool(ctx)---def _actuation_obligation_admits_login_completion_tool(- ctx: CopilotContext, tool_name: str, arguments: Mapping[str, Any] | None-) -> bool:- if tool_name not in _SYNTHESIZED_BLOCK_COMMIT_TOOLS:- return False- # Only Enter commits a login form, matching what _first_stable_login_submit_index credits as a- # submit; every other keystroke stays gated. Absent arguments fail closed — the signature- # defaults them to None, so a caller that omits them must not admit arbitrary keystrokes.- if tool_name == "press_key" and (- not isinstance(arguments, Mapping) or str(arguments.get("key") or "").strip() != "Enter"- ):- return False- if not _actuation_obligation_live_fill_delivery_required(ctx):- return False- if _last_scout_credential_fill_index(ctx.scout_trajectory) is None:- return False- return not _trajectory_reaches_post_credential_commit(ctx)--def arm_credential_scout_reopen(ctx: AgentContext, identity_digest: str) -> bool:"""Arm a one-shot scout-window reopen for the first author-time credential-scout reject per(structural identity + credential binding) digest. A repeat identical reject returns False and@@ -3285,56 +3107,6 @@ def arm_credential_scout_reopen(ctx: AgentContext, identity_digest: str) -> boolreturn True-def _credential_scout_reopen_admits_evaluate(ctx: CopilotContext, tool_name: str) -> bool:- return tool_name == "evaluate" and bool(ctx.synthesized_block_reopened_for_credential_scout)---def synthesized_block_persistence_signal(- ctx: Any, tool_name: str, arguments: Mapping[str, Any] | None = None-) -> CopilotToolBlockerSignal | None:- if tool_name in _SYNTHESIZED_BLOCK_PERSISTENCE_ALLOWED_TOOLS:- return None- ambiguous_selector_rescout_state = _ambiguous_bare_selector_rescout_signal_state(ctx, tool_name)- if ambiguous_selector_rescout_state == "allow":- return None- if _credential_scout_reopen_admits_evaluate(ctx, tool_name):- claim_turn(ctx, TurnClaimant.CREDENTIAL_SCOUT_REOPEN)- return None- if _actuation_obligation_admits_required_fill_tool(ctx, tool_name):- claim_turn(ctx, TurnClaimant.ACTUATION_OBLIGATION_FILL)- return None- if _actuation_obligation_admits_login_completion_tool(ctx, tool_name, arguments):- claim_turn(ctx, TurnClaimant.ACTUATION_OBLIGATION_LOGIN_COMPLETION)- return None- if (- ambiguous_selector_rescout_state != "block"- and not _should_force_synthesized_block_persistence(ctx)- and not _should_block_mutating_tool_after_synthesized_offer(ctx, tool_name)- ):- return None- return CopilotToolBlockerSignal(- blocker_kind="tool_error",- agent_steering_text=(- "A synthesized code block offer is already available for this authoring turn. "- f"Call {SYNTHESIZED_BLOCK_PERSISTENCE_TOOL} with that block now before any more scouting, "- "reading, page evaluation, or browser interaction. This blocker clears only after "- f"{SYNTHESIZED_BLOCK_PERSISTENCE_TOOL} succeeds."- ),- user_facing_reason="I need to save and test the drafted workflow before scouting more.",- recovery_hint="retry_with_different_tool",- cleared_by_tools=frozenset({SYNTHESIZED_BLOCK_PERSISTENCE_TOOL}),- preserves_workflow_draft=True,- renders_final_reply=False,- internal_reason_code=SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE,- blocked_tool=tool_name,- extra={- "synthesized_block_offered_trajectory_len": (- getattr(ctx, "synthesized_block_offered_trajectory_len", 0) or 0- ),- },- )--def _runner_kwargs_with_forced_tool_choice(runner_kwargs: dict[str, Any], tool_name: str) -> dict[str, Any]:run_config = runner_kwargs.get("run_config")if isinstance(run_config, RunConfig):@@ -3411,16 +3183,14 @@ async def run_with_enforcement("enforcement_iteration",data={"iteration": iteration, "elapsed_seconds": round(elapsed, 3)},):- force_synthesized_block_persistence = _should_force_synthesized_block_persistence(ctx)force_advisory_run_dispatch = _should_force_advisory_run_dispatch(ctx)# The advisory-dispatch force claims the actuation ladder itself (same-claimant), so the# grant-consumption path can never self-deadlock.if force_advisory_run_dispatch:claim_turn(ctx, TurnClaimant.OUTPUT_CONTRACT_ACTUATION)- force_run_dispatch = force_synthesized_block_persistence or force_advisory_run_dispatchcurrent_runner_kwargs = (_runner_kwargs_with_forced_tool_choice(runner_kwargs, SYNTHESIZED_BLOCK_PERSISTENCE_TOOL)- if force_run_dispatch+ if force_advisory_run_dispatchelse runner_kwargs)effective_run_config = current_runner_kwargs.get("run_config")@@ -3430,11 +3200,10 @@ async def run_with_enforcement(turn_intent = getattr(ctx, "turn_intent", None)turn_intent_authority = getattr(turn_intent, "authority", None)LOG.info(- "copilot synthesized persistence force decision",- force_synthesized_block_persistence=force_synthesized_block_persistence,+ "copilot advisory run dispatch force decision",force_advisory_run_dispatch=force_advisory_run_dispatch,- forced_tool_name=(SYNTHESIZED_BLOCK_PERSISTENCE_TOOL if force_run_dispatch else None),- chosen_tool_name=(SYNTHESIZED_BLOCK_PERSISTENCE_TOOL if force_run_dispatch else None),+ forced_tool_name=(SYNTHESIZED_BLOCK_PERSISTENCE_TOOL if force_advisory_run_dispatch else None),+ chosen_tool_name=(SYNTHESIZED_BLOCK_PERSISTENCE_TOOL if force_advisory_run_dispatch else None),turn_intent_mode=getattr(getattr(turn_intent, "mode", None), "value", None),turn_intent_may_update_workflow=getattr(turn_intent_authority, "may_update_workflow", None),turn_intent_may_run_blocks=getattr(turn_intent_authority, "may_run_blocks", None),@@ -3525,10 +3294,12 @@ async def run_with_enforcement(# the agent's already-final REPLY with one synthesized from a single# browser frame.if pending_recovery_nudge is not None:- nudge: str | None = pending_recovery_nudge+ decision: EnforcementDecision | None = EnforcementDecision(+ rule="screenshot_dropped", message=pending_recovery_nudge+ )pending_recovery_nudge = Noneelse:- nudge = _check_enforcement(ctx, result, copilot_config)+ decision = enforcement_decision(ctx, result, copilot_config)# The offer is independent of the nudge: a clean scout-then-author turn# finalizes with nudge=None, so injecting it only inside the nudge branch@@ -3536,7 +3307,7 @@ async def run_with_enforcement(# nudge path and the finalize path.synthesized_msg = _maybe_synthesized_block_offer_msg(ctx)- if nudge is None:+ if decision is None:# Checked whenever there's no regular nudge, even if a synthesized# offer is also pending: a credential-blocked run's diagnosis can# coincide with a reopened synthesized-block offer, and the pause@@ -3562,13 +3333,13 @@ async def run_with_enforcement(# an uncorrected reply. Gated on the latch's own transition (not# just the outcome value) so a later iteration's unrelated# nudge=None doesn't re-trigger this off a stale "declined".- nudge = _check_enforcement(ctx, result, copilot_config)- if nudge is None and synthesized_msg is None:+ decision = enforcement_decision(ctx, result, copilot_config)+ if decision is None and synthesized_msg is None:_consume_pending_screenshots(ctx)_maybe_raise_non_retriable_nav(ctx)return result- if nudge is not None and nudge == _nudge(copilot_config, "post_update"):+ if decision is not None and decision.rule == "post_update":if ctx.post_update_nudge_count >= MAX_POST_UPDATE_NUDGES:LOG.warning("Enforcement exhausted post-update nudges, allowing response",@@ -3579,8 +3350,8 @@ async def run_with_enforcement(return resultctx.post_update_nudge_count += 1- if nudge is not None:- nudge_type = _nudge_type_for_log(nudge, copilot_config)+ if decision is not None:+ nudge_type = _NUDGE_TYPE_BY_KEY.get(decision.rule, decision.rule)else:nudge_type = "synthesized_block_offer"LOG.info("Enforcement nudge", nudge_type=nudge_type, iteration=iteration)@@ -3592,7 +3363,9 @@ async def run_with_enforcement(LOG.info("Injecting screenshot user message", count=len(screenshot_msg["content"]) - 1)with copilot_span("enforcement_nudge", data={"nudge_type": nudge_type, "iteration": iteration}):- extra_msgs = _assemble_enforcement_messages(screenshot_msg, nudge, synthesized_msg)+ extra_msgs = _assemble_enforcement_messages(+ screenshot_msg, decision.message if decision is not None else None, synthesized_msg+ )current_input = (extra_msgs if session is not None else _prune_input_list(result.to_input_list()) + extra_msgs)diff --git a/skyvern/forge/sdk/copilot/mcp_adapter.py b/skyvern/forge/sdk/copilot/mcp_adapter.pyindex cf13a1f2f..457677e4e 100644--- a/skyvern/forge/sdk/copilot/mcp_adapter.py+++ b/skyvern/forge/sdk/copilot/mcp_adapter.py@@ -39,7 +39,6 @@ from skyvern.forge.sdk.copilot.enforcement import (current_page_challenge_advisory_signal,register_no_progress_interaction_click,requested_output_paths_for_derivation,- synthesized_block_persistence_signal,)from skyvern.forge.sdk.copilot.loop_detection import (detect_failed_tool_step_loop_for_ctx,@@ -504,22 +503,6 @@ class SkyvernOverlayMCPServer(MCPServer):)return _copilot_to_call_tool_result({"ok": False, "error": challenge_advisory_payload})- persistence_signal = synthesized_block_persistence_signal(copilot_ctx, tool_name, arguments)- if persistence_signal is not None:- LOG.warning(- "Synthesized block persistence required before MCP tool",- tool_name=tool_name,- synthesized_block_offered_trajectory_len=getattr(- copilot_ctx,- "synthesized_block_offered_trajectory_len",- None,- ),- )- payload = emit_blocker_signal_payload(copilot_ctx, persistence_signal)- result = {"ok": False, "error": payload}- record_tool_step_result_for_ctx(copilot_ctx, tool_name, arguments, result)- return _copilot_to_call_tool_result(result)-loop_error = detect_failed_tool_step_loop_for_ctx(copilot_ctx, tool_name, arguments)if loop_error:LOG.warning(diff --git a/skyvern/forge/sdk/copilot/output_policy.py b/skyvern/forge/sdk/copilot/output_policy.pyindex 55fe72030..f8341e218 100644--- a/skyvern/forge/sdk/copilot/output_policy.py+++ b/skyvern/forge/sdk/copilot/output_policy.py@@ -1005,11 +1005,11 @@ def _bound_approved_credential_ids(request_policy: RequestPolicy, workflow_yaml:def _allowed_unresolved_credential_ids(request_policy: RequestPolicy) -> set[str]:- # invalid_credential_ids is negation-filtered and affirmative; credential_refs is not — a- # model-carried or negated ref must not become draft-bindable (SKY-13552).if not request_policy.allow_missing_credentials_in_draft:return set()- return set(request_policy.invalid_credential_ids)+ ids = set(request_policy.invalid_credential_ids)+ ids.update(ref for ref in request_policy.credential_refs if isinstance(ref, str) and ref.startswith("cred_"))+ return idsdef _existing_workflow_credential_ids(request_policy: RequestPolicy) -> set[str]:diff --git a/skyvern/forge/sdk/copilot/request_policy.py b/skyvern/forge/sdk/copilot/request_policy.pyindex 3c9cfe63f..6efc81059 100644--- a/skyvern/forge/sdk/copilot/request_policy.py+++ b/skyvern/forge/sdk/copilot/request_policy.py@@ -234,6 +234,14 @@ _POSTFIX_CREDENTIAL_TOKEN_RE = re.compile(r"\b(?:use|using|with)\s+(?:my\s+|the\s+)?(?:saved\s+)?([A-Za-z0-9_.@:-]{2,100})\s+credential\b",re.I,)+_EXPLICIT_CREDENTIAL_ID_CONTEXT_RE = re.compile(+ r"\b(?:use|using|with|select|choose)\s+"+ r"(?:(?:my|the|a)\s+)?(?:(?:saved|stored)\s+)?(?:credential(?:\s+id)?\s*)?"+ r"(?::\s*)?$",+ re.I,+)+_CREDENTIAL_ID_LABEL_CONTEXT_RE = re.compile(r"\bcredential\s+id\s*$", re.I)+_CREDENTIAL_ID_COORDINATOR_RE = re.compile(r"\s*,?\s*(?:and|or)\s*$", re.I)_CREDENTIAL_REPLACEMENT_TARGET_RES = (re.compile(r"\bswitch(?:ing)?\s+to\b"@@ -627,10 +635,12 @@ class RequestPolicy:# cred_ id — as distinct from approvals carried in from earlier turns. Naming a credential# answers which one to use, which is what lets the login page answer where it may be typed.current_turn_named_credential_ids: set[str] = field(default_factory=set)- # Login targets the classifier named that the user also wrote themselves. The classifier alone- # decides which URL is a sign-in page but is not authority for the origin a password reaches, so- # only the intersection may vouch for a fill.- grounded_login_target_urls: list[str] = field(default_factory=list)+ # Sites the user themselves provided anywhere in this chat, one URL per origin. A credential may+ # only be released onto one of these (or a vault/tested match); a site only a model produced is+ # never eligible.+ user_provided_site_urls: list[str] = field(default_factory=list)+ # Which user message (1-based) each of those URLs came from, so a release records its provenance.+ user_site_url_sources: dict[str, int] = field(default_factory=dict)credential_ask_candidate_ids: list[str] = field(default_factory=list)existing_workflow_credential_ids: list[str] = field(default_factory=list)# Sorted at the trace/JSON boundary; YAML traversal uses sets.@@ -660,9 +670,6 @@ class RequestPolicy:"allow_missing_credentials_in_draft": self.allow_missing_credentials_in_draft,"credential_draft_deferred_explicitly": self.credential_draft_deferred_explicitly,"resolved_credential_count": len(self.resolved_credentials),- "current_turn_named_credential_count": len(self.current_turn_named_credential_ids),- "invalid_credential_id_count": len(self.invalid_credential_ids),- "auto_bound_credential_count": len(self.auto_bound_credentials),"has_completion_contract": bool(self.completion_contract),"completion_criteria_count": len(self.graded_completion_criteria()),"completion_criteria_implicit_count": sum(@@ -928,7 +935,8 @@ TRANSCRIPT_ANCHOR_CHAR_CAP = 512_TRANSCRIPT_RETAINED_MIN_CHARS = 512_TRANSCRIPT_MARKER_RESERVE = 32_EMPTY_SLOT_SENTINEL = "(none)"-_REDACTED_REFUSED_SECRET_TURN = "[raw credentials redacted — this turn was refused]"+REDACTED_REFUSED_SECRET_TURN = "[raw credentials redacted — this turn was refused]"+_REDACTED_REFUSED_SECRET_TURN = REDACTED_REFUSED_SECRET_TURN@dataclass(frozen=True)@@ -961,7 +969,7 @@ def _safe_slot(text: str | None, cap: int) -> str:return _middle_truncate(escape_code_fences(redact_raw_secrets_for_prompt(bounded)), cap)-def _redact_refused_secret_turns(+def redact_refused_secret_turns(messages: list[WorkflowCopilotChatHistoryMessage],) -> list[WorkflowCopilotChatHistoryMessage]:"""Replace the content of any user turn answered with the raw-secret refusal.@@ -1007,7 +1015,7 @@ def build_transcript_context():filtered = filtered[:-1]- filtered = _redact_refused_secret_turns(filtered)+ filtered = redact_refused_secret_turns(filtered)user_indices = [i for i, m in enumerate(filtered) if m.sender == WorkflowCopilotChatSender.USER]ai_indices = [i for i, m in enumerate(filtered) if m.sender == WorkflowCopilotChatSender.AI]@@ -4598,12 +4606,8 @@ def _classifier_ref_stated_in_message(policy: RequestPolicy, user_message: str)def _explicit_credential_ids(user_message: str) -> list[str]:- """Credential IDs the user's own message hands to this turn.-- Accepts the canonical ``cred_`` form and ``_MALFORMED_CREDENTIAL_ID_RE``'s ``cred <12+ digits>``- normalization; surrounding prose can only withdraw a token, never qualify it.- """text = _credential_authority_text(user_message or "")+ explicit: list[str] = []matches = sorted([(match, match.group(0)) for match in _CREDENTIAL_ID_RE.finditer(text)]+ [(match, f"cred_{match.group(1)}") for match in _MALFORMED_CREDENTIAL_ID_RE.finditer(text)],@@ -4614,15 +4618,27 @@ def _explicit_credential_ids(user_message: str) -> list[str]:for match, credential_id in matchesif _credential_reference_is_negated(text, match.start())}- return _clean_list(- [- credential_id- for match, credential_id in matches- if last_negated_mention.get(credential_id, -1) <= match.start()- and not _credential_reference_is_negated(text, match.start())- and not _credential_reference_is_unrelated_replacement(text, match.start())- ]- )+ last_explicit_end: int | None = None+ for match, credential_id in matches:+ if last_negated_mention.get(credential_id, -1) > match.start():+ continue+ if _credential_reference_is_negated(text, match.start()) or _credential_reference_is_unrelated_replacement(+ text, match.start()+ ):+ continue+ context = text[max(0, match.start() - 64) : match.start()]+ if (+ _EXPLICIT_CREDENTIAL_ID_CONTEXT_RE.search(context)+ or _CREDENTIAL_ID_LABEL_CONTEXT_RE.search(context)+ or match.group(0).strip() == text.strip()+ or (+ last_explicit_end is not None+ and _CREDENTIAL_ID_COORDINATOR_RE.fullmatch(text[last_explicit_end : match.start()])+ )+ ):+ explicit.append(credential_id)+ last_explicit_end = match.end()+ return _clean_list(explicit)def credential_candidate_label(credential: Credential) -> str:@@ -4869,18 +4885,38 @@ def _prior_approved_credential_ids_from_context(global_llm_context: str) -> set[}-def _ground_login_target_urls(policy: RequestPolicy, user_message: str) -> None:- """Keep the classifier's login targets this message also carries, so neither a URL the model authored- nor one the user pasted in an older turn for another purpose becomes an origin a password reaches.+def _ground_user_provided_sites(+ policy: RequestPolicy,+ user_message: str,+ full_chat_history: Sequence[WorkflowCopilotChatHistoryMessage],+) -> None:+ """Record every site the user themselves gave this chat, so the fill seam can release a credential+ onto one of them and never onto a site only a model produced.++ Linking a later "log into pathfold" back to a URL from an earlier turn is the agent's job, not+ this function's: it reads the whole conversation. What is recorded here is only the deterministic+ part — the origins the user actually wrote."""- typed_origins = {- parts[2]- for candidate in URL_CANDIDATE_RE.findall(user_message or "")- if (parts := _url_parts(candidate.rstrip(".,;:!?")))- }- policy.grounded_login_target_urls = [- url for url in policy.login_page_urls if (parts := _url_parts(url)) and parts[2] in typed_origins+ user_texts = [+ message.content+ for message in full_chat_history+ if message.sender == WorkflowCopilotChatSender.USER and message.content]+ user_texts.append(user_message or "")+ seen_origins: set[str] = set()+ urls: list[str] = []+ sources: dict[str, int] = {}+ for index, text in enumerate(user_texts, start=1):+ for candidate in URL_CANDIDATE_RE.findall(text):+ cleaned = candidate.rstrip(".,;:!?")+ parts = _url_parts(cleaned)+ if parts is None or parts[2] in seen_origins:+ continue+ seen_origins.add(parts[2])+ urls.append(cleaned)+ sources[cleaned] = index+ policy.user_provided_site_urls = urls+ policy.user_site_url_sources = sourcesdef _record_current_turn_named_credentials(policy: RequestPolicy, credentials: Sequence[Credential]) -> None:@@ -4986,29 +5022,23 @@ async def _resolve_credentials(# regardless of the classifier label. Do not trust stale/model-authored# credential_refs that are absent from the latest user message.ids = _explicit_credential_ids(user_message)- explicit_name_candidates = _credential_name_candidates(user_message)if ids:existing = await app.DATABASE.credentials.get_credentials_by_ids(ids, organization_id=organization_id)found = {credential.credential_id for credential in existing}policy.resolved_credentials = existing_record_current_turn_named_credentials(policy, existing)policy.invalid_credential_ids = [credential_id for credential_id in ids if credential_id not in found]- # An unresolvable stated ID asks rather than falling through to the inference tiers below,- # but it never blocks another stated reference that did resolve.- if not explicit_name_candidates:- if existing or not policy.invalid_credential_ids:- return- if policy.testing_intent == "skip_test":- policy.allow_run_blocks = False- policy.allow_missing_credentials_in_draft = True- return+ if policy.invalid_credential_ids and policy.testing_intent != "skip_test":formatted = ", ".join(f"`{credential_id}`" for credential_id in policy.invalid_credential_ids)_block(policy,f"The credential ID(s) {formatted} were not found in this organization. Please provide a valid saved credential ID or explicitly ask for an unvalidated draft that will not be run yet.",reason="credential_name_unresolved",)- return+ elif policy.invalid_credential_ids:+ policy.allow_run_blocks = False+ policy.allow_missing_credentials_in_draft = True+ return# `login_intent` is a classifier-authored hint that misses on runs where the model does not set# it, so a message asking for a login in its own words must still count. A classifier that failed@@ -5019,7 +5049,7 @@ async def _resolve_credentials(or (policy.classifier_status != "fallback" and login_phrase_asserted)or _classifier_ref_stated_in_message(policy, user_message))- name_candidates = explicit_name_candidates+ name_candidates = _credential_name_candidates(user_message)credentials: list[Credential] | None = None# Only a request that already asserts a login or credential intent may be matched against the# org's saved names; without that gate an unrelated message would scan the credential list.@@ -5033,7 +5063,7 @@ async def _resolve_credentials([credential for credential in credentials if credential.name in name_candidates])if len(named_matches) == 1:- policy.resolved_credentials = _deduplicate_credentials(list(policy.resolved_credentials) + named_matches)+ policy.resolved_credentials = named_matches_record_current_turn_named_credentials(policy, named_matches)if (policy.classifier_status == "fallback"@@ -5063,9 +5093,6 @@ async def _resolve_credentials(card_answerable=True,)return- if policy.resolved_credentials:- # A stated ID already resolved; an unmatched name rides along as a finding, not a wall.- returnif policy.testing_intent == "skip_test" or defer_unresolved_credential_name:policy.allow_run_blocks, policy.allow_missing_credentials_in_draft = False, Trueif defer_unresolved_credential_name:@@ -5659,6 +5686,9 @@ async def build_request_policy(handler: LLMAPIHandler | None,active_criteria: list[CompletionCriterion] | None = None,config: CopilotConfig | None = None,+ # The whole chat, not the model-context tail `chat_history` carries: a site is grounded by the+ # user having written it, which does not expire when the message leaves the prompt window.+ prior_user_messages: Sequence[WorkflowCopilotChatHistoryMessage] = (),_preclassified_policy: RequestPolicy | None = None,) -> RequestPolicy:policy = _preclassified_policy@@ -5700,6 +5730,10 @@ async def build_request_policy(carried if carried and is_email_address(carried) else None)+ # Unconditional: a turn blocked here can resume mid-turn (credential card), and the resumed+ # fill still needs to know which sites the user has provided.+ _ground_user_provided_sites(policy, user_message, prior_user_messages or chat_history)+if policy.raw_secret_detected and policy.raw_secret_handling == "redacted_draft":policy.testing_intent = "skip_test"policy.requires_user_clarification = False@@ -5772,7 +5806,6 @@ async def build_request_policy(# conservative clarification flag; _resolve_credentials will block# again if the lookup is missing or ambiguous.policy.requires_user_clarification = False- _ground_login_target_urls(policy, user_message)await _resolve_credentials(policy,organization_id,@@ -5859,6 +5892,7 @@ async def build_request_policy_trust_floor(organization_id: str,handler: LLMAPIHandler | None,config: CopilotConfig | None = None,+ prior_user_messages: Sequence[WorkflowCopilotChatHistoryMessage] = (),) -> RequestPolicy:policy = await _classify_request_trust_floor(user_message,@@ -5876,6 +5910,7 @@ async def build_request_policy_trust_floor(organization_id=organization_id,handler=handler,config=config,+ prior_user_messages=prior_user_messages,_preclassified_policy=policy,)diff --git a/skyvern/forge/sdk/copilot/runtime.py b/skyvern/forge/sdk/copilot/runtime.pyindex a75e7bfd1..b0c193db1 100644--- a/skyvern/forge/sdk/copilot/runtime.py+++ b/skyvern/forge/sdk/copilot/runtime.py@@ -301,7 +301,7 @@ class AgentContext:workflow_verification_evidence: WorkflowVerificationEvidence = field(default_factory=WorkflowVerificationEvidence)# Enforcement state. Set lazily by streaming_adapter, tools, and- # failure_tracking; declared here so _check_enforcement can read them on a+ # failure_tracking; declared here so enforcement_decision can read them on a# fresh context without AttributeError.navigate_called: bool = Falseobservation_after_navigate: bool = False@@ -340,7 +340,6 @@ class AgentContext:last_scout_act_observe_packet: dict[str, Any] | None = Nonelast_scout_act_observe_recapture_attempted: bool = Falselast_scout_act_observe_recapture_result: str = ""- ambiguous_bare_selector_rescout_context_key: str | None = Nonepending_code_authoring_runtime_repair_context: CodeAuthoringRepairContext | None = Nonelast_code_authoring_repair_context: CodeAuthoringRepairContext | None = Nonechallenge_gated_proxy_retry_count: int = 0diff --git a/skyvern/forge/sdk/copilot/streaming_adapter.py b/skyvern/forge/sdk/copilot/streaming_adapter.pyindex 509f5f76f..7863a814c 100644--- a/skyvern/forge/sdk/copilot/streaming_adapter.py+++ b/skyvern/forge/sdk/copilot/streaming_adapter.py@@ -54,6 +54,7 @@ from skyvern.forge.sdk.schemas.workflow_copilot import (WorkflowCopilotDesignStartUpdate,WorkflowCopilotNarrationUpdate,WorkflowCopilotStreamMessageType,+ WorkflowCopilotTitleUpdate,WorkflowCopilotToolCallUpdate,WorkflowCopilotToolResultUpdate,WorkflowCopilotTurnMode,@@ -716,6 +717,17 @@ async def emit_turn_start(stream: EventSourceStream, ctx: CopilotContext) -> Non)+async def emit_title_update(stream: EventSourceStream, ctx: CopilotContext, title: str) -> None:+ await stream.send(+ WorkflowCopilotTitleUpdate(+ turn_id=ctx.turn_id,+ workflow_permanent_id=ctx.workflow_permanent_id,+ title=title,+ timestamp=datetime.now(timezone.utc),+ )+ )++async def maybe_emit_design_start(stream: EventSourceStream, ctx: CopilotContext) -> None:if ctx.design_start_emitted:returndiff --git a/skyvern/forge/sdk/copilot/tools/blockers.py b/skyvern/forge/sdk/copilot/tools/blockers.pyindex 9c4ef5c40..5f487470f 100644--- a/skyvern/forge/sdk/copilot/tools/blockers.py+++ b/skyvern/forge/sdk/copilot/tools/blockers.py@@ -28,7 +28,6 @@ from skyvern.forge.sdk.copilot.composition_evidence import interactive_challengefrom skyvern.forge.sdk.copilot.enforcement import (current_page_challenge_advisory_signal,one_time_code_fill_supersedes_challenge,- synthesized_block_persistence_signal,)from skyvern.forge.sdk.copilot.failure_tracking import (PER_TOOL_BUDGET_FAILURE_CATEGORY,@@ -1414,7 +1413,6 @@ LOOP_PLANE_REFUSAL_REASON_CODES: frozenset[str] = frozenset("credential_priority_authoring_churn","tool_error_terminal_challenge_blocker","tool_error_current_page_challenge_advisory",- "tool_error_synthesized_block_persistence_required","tool_error_challenge_gated_submit_disabled","tool_error_non_retriable_nav","tool_error_infrastructure_unavailable",@@ -1479,10 +1477,6 @@ def _tool_loop_error(ctx: AgentContext, tool_name: str, arguments: dict[str, Any),)- persistence_signal = synthesized_block_persistence_signal(ctx, tool_name, arguments)- if persistence_signal is not None:- return _emit_loop_plane_refusal(ctx, persistence_signal)-# While a typed output-contract actuation ladder is still unresolved for the authoring tools, the# bounded ladder (not a generic loop backstop) owns the turn's outcome; the guards re-engage the moment# the ladder reaches an advisory-consumed run or a typed terminal.diff --git a/skyvern/forge/sdk/copilot/tools/credential_fill.py b/skyvern/forge/sdk/copilot/tools/credential_fill.pyindex 1a417d0a4..0c8f43253 100644--- a/skyvern/forge/sdk/copilot/tools/credential_fill.py+++ b/skyvern/forge/sdk/copilot/tools/credential_fill.py@@ -8,7 +8,7 @@ import tldextractfrom skyvern.cli.core.session_manager import get_pagefrom skyvern.forge import app-from skyvern.forge.sdk.browser_action_policy import BrowserOrigin, canonicalize_origin+from skyvern.forge.sdk.browser_action_policy import canonicalize_originfrom skyvern.forge.sdk.copilot.config import BlockAuthoringPolicyfrom skyvern.forge.sdk.copilot.credential_resolution import load_credentials, url_partsfrom skyvern.forge.sdk.copilot.loop_detection import record_tool_step_result_for_ctx@@ -234,26 +234,49 @@ async def _vault_named_sites(copilot_ctx: AgentContext, credential_id: str) -> lreturn uris-def _missing_credential_origin_error(credential_id: str) -> str:+def _missing_credential_origin_error(credential_id: str, page_url: str | None) -> str:+ if page_url:+ origin = loggable_origin(page_url)+ return (+ f"Credential `{credential_id}` cannot be filled on {origin}: the user has not named this site "+ f"in this chat. Ask the user to confirm the sign-in site by pasting its URL — {origin} — "+ "then retry."+ )return (- f"Credential `{credential_id}` has no intended login origin, so it cannot be filled into the live browser. "- "Test the saved credential against its login page to bind it to that site, then inspect the page and retry."+ f"Credential `{credential_id}` cannot be filled: no live page is open. "+ "Navigate to the sign-in page first, then retry."+ )+++def _log_fill_grant(route: str, url: str, credential_id: str, source_message: int | None = None) -> None:+ LOG.info(+ "copilot credential fill grant",+ route=route,+ page_origin=loggable_origin(url),+ credential_id=credential_id,+ source_user_message=source_message,)-def _sole_grounded_login_target(policy: RequestPolicy) -> str | None:- """The one sign-in page this request grounds in the user's own words, if there is exactly one; a- request naming several grounds none, since nothing here can tell which of them a password belongs to.+def _user_provided_site_url_match(policy: RequestPolicy, page_url: str) -> tuple[str | None, bool]:+ """Match the live page against a site the user pasted, reporting whether the match was+ site-level. Hosts outside the public suffix list (internal domains, localhost) have no+ registrable site to compare, so they fall back to an exact-origin match."""- by_origin: dict[BrowserOrigin, str] = {}- for url in policy.grounded_login_target_urls:- parts = url_parts(url)- origin = canonicalize_origin(parts[2]) if parts else None- if origin is not None:- by_origin[origin] = url- if len(by_origin) != 1:- return None- return next(iter(by_origin.values()))+ for url in policy.user_provided_site_urls:+ if _same_site(page_url, url):+ return url, True+ if _still_on_admitted_site(page_url, url):+ return url, False+ return None, False+++def _request_settled_credential(policy: RequestPolicy, credential_id: str) -> bool:+ """A non-model signal already answered which credential: the user named it this turn, or it is+ the only credential resolved for this request (e.g. the one card answer, carried)."""+ if policy.current_turn_named_credential_ids == {credential_id}:+ return True+ return {credential.credential_id for credential in policy.resolved_credentials} == {credential_id}async def _sole_org_password_credential_id(@@ -300,26 +323,29 @@ async def _credential_fill_origin_grant(if not authority_error:if not isinstance(policy, RequestPolicy):- return None, _missing_credential_origin_error(credential_id)+ return None, _missing_credential_origin_error(credential_id, None)intended_url = _resolved_credential_intended_url(policy, credential_id)if intended_url:+ _log_fill_grant("admitted_or_tested", intended_url, credential_id)return _CredentialFillOriginGrant(intended_url), None- # A credential saved without a login URL has no record of where it belongs, but that is an- # absent answer rather than a contrary one. The sign-in page the user themselves named can- # supply the origin, provided something other than the model settled which credential to use.page_url = await _live_working_page_url(copilot_ctx) or ""# The vault entry names the site the user filed this credential under, so it answers where# the secret belongs without anyone having to run the test flow first.if page_url and any(_same_site(page_url, uri) for uri in await _vault_named_sites(copilot_ctx, credential_id)):+ _log_fill_grant("vault_site", page_url, credential_id)return _CredentialFillOriginGrant(page_url, whole_site=True), None- grounded_target = _sole_grounded_login_target(policy)- if not page_url or grounded_target is None or not _still_on_admitted_site(page_url, grounded_target):- return None, _missing_credential_origin_error(credential_id)- if policy.current_turn_named_credential_ids == {credential_id}:- return _CredentialFillOriginGrant(page_url), None- if credential_id == await _sole_org_password_credential_id(load_once):- return _CredentialFillOriginGrant(page_url), None- return None, _ambiguous_unbound_credential_steer(credential_id, page_url)+ if page_url:+ matched_url, site_level = _user_provided_site_url_match(policy, page_url)+ if matched_url is not None:+ if _request_settled_credential(+ policy, credential_id+ ) or credential_id == await _sole_org_password_credential_id(load_once):+ # An origin-only match (no registrable site) keeps the origin-scoped grant so the+ # release guard can still compare it; site matches travel the whole site.+ _log_fill_grant("user_url", page_url, credential_id, policy.user_site_url_sources.get(matched_url))+ return _CredentialFillOriginGrant(page_url, whole_site=site_level), None+ return None, _ambiguous_unbound_credential_steer(credential_id, page_url)+ return None, _missing_credential_origin_error(credential_id, page_url or None)if not isinstance(policy, RequestPolicy):return None, authority_error@@ -332,9 +358,10 @@ async def _credential_fill_origin_grant(load_org_credentials=load_once,)if admission.admitted and admission.page_url:+ _log_fill_grant("live_page_admission", admission.page_url, credential_id)return _CredentialFillOriginGrant(admission.page_url), Noneif admission.admitted:- return None, _missing_credential_origin_error(credential_id)+ return None, _missing_credential_origin_error(credential_id, None)return None, admission.steer or authority_error@@ -452,7 +479,7 @@ async def _fill_credential_field_impl(field=field,organization_id=copilot_ctx.organization_id,)- return finish({"ok": False, "error": policy_error or _missing_credential_origin_error(credential_id)})+ return finish({"ok": False, "error": policy_error or _missing_credential_origin_error(credential_id, None)})value, credential_name, resolve_error = await _resolve_credential_fill_value(copilot_ctx, credential_id, field)if resolve_error or value is None:diff --git a/skyvern/forge/sdk/copilot/tools/discovery.py b/skyvern/forge/sdk/copilot/tools/discovery.pyindex 45298979d..7bc9cd910 100644--- a/skyvern/forge/sdk/copilot/tools/discovery.py+++ b/skyvern/forge/sdk/copilot/tools/discovery.py@@ -92,6 +92,18 @@ def _discovery_candidate_identity_bound(return "" not in normalized_names and len(normalized_names) == 1+def _user_provided_entry_url(copilot_ctx: CopilotContext) -> str | None:+ """A site the user gave this chat, when name lookup found nothing.++ Lookup asks the world what a name refers to; this asks the conversation. A user who pasted the+ URL has already answered, so exhausting the lookup and telling them to supply a URL they already+ supplied is the dead end this avoids. Only the sole site they gave answers; several is a question.+ """+ policy = copilot_ctx.request_policy+ urls = list(policy.user_provided_site_urls) if policy is not None else []+ return urls[0] if len(urls) == 1 else None++def _resolve_discovery_entry_url(site_or_url: str) -> tuple[str | None, str]:"""Resolve the user-supplied site name/URL into a navigable URL.@@ -935,7 +947,8 @@ async def _discover_workflow_entrypoint_impl(entry_url = entrypoint_candidate.urlkind = "evidence_candidate"else:- kind = "unresolved"+ entry_url = _user_provided_entry_url(copilot_ctx)+ kind = "user_provided_site" if entry_url else "unresolved"if entry_url is None:result = _discovery_build_result(candidate_url=None,diff --git a/skyvern/forge/sdk/copilot/tools/scouting.py b/skyvern/forge/sdk/copilot/tools/scouting.pyindex ce79ad0d4..a12f65ce4 100644--- a/skyvern/forge/sdk/copilot/tools/scouting.py+++ b/skyvern/forge/sdk/copilot/tools/scouting.py@@ -91,7 +91,12 @@ from skyvern.forge.sdk.copilot.runtime import (resolve_browser_state_for_context,)from skyvern.forge.sdk.copilot.screenshot_utils import enqueue_screenshot_from_result-from skyvern.forge.sdk.workflow.models.block import CodeBlockCaptchaError, _code_block_solve_captcha_builtin+from skyvern.forge.sdk.workflow.models.block import (+ CodeBlockCaptchaError,+ _bounded_code_block_recaptcha_token_populated,+ _code_block_recaptcha_response_field_present,+ _code_block_solve_captcha_builtin,+)from ._shared import (_DISCOVERY_PER_CALL_TIMEOUT_SECONDS,@@ -792,6 +797,10 @@ def _record_scouted_interaction(_MAX_CHALLENGE_SOLVE_ATTEMPTS = 3+# Measured: a healthy turn re-enters the ladder 4-5 times for one widget, because a token+# expires long before a turn ends. Sized well clear of that so it bounds a page rotating its+# identity to outspend us, without cutting a legitimate turn short.+_MAX_CHALLENGE_SOLVE_ATTEMPTS_PER_TURN = 12def _challenge_identity(evidence: dict[str, Any]) -> str:@@ -840,7 +849,7 @@ async def solve_challenge_when_evidence_settles(url: str | None,observed_after_interaction: bool,) -> bool:- """Try the shared solve routes on a challenge in just-settled evidence; True if it passed.+ """Try the shared solve routes on a challenge in just-settled evidence; True when one landed.Call from a capture exit, never a tool hook: a settled packet describes the page as it nowis, and ``observed_after_interaction`` follows from the seam — the act-observe capture runs@@ -865,6 +874,16 @@ async def solve_challenge_when_evidence_settles(attempts = ctx.challenge_solve_attempts.get(identity, 0)if attempts >= _MAX_CHALLENGE_SOLVE_ATTEMPTS:return False+ # Every input to the identity is page-controlled, so a widget that rotates its sitekey or id+ # mints a fresh one on each observation and never reaches the per-identity cap. The solver+ # costs real money on a platform-wide account, so the turn needs a bound the page cannot move.+ if sum(ctx.challenge_solve_attempts.values()) >= _MAX_CHALLENGE_SOLVE_ATTEMPTS_PER_TURN:+ LOG.info(+ "copilot_scout_captcha_turn_budget_spent",+ url=url,+ organization_id=ctx.organization_id,+ )+ return Falsectx.challenge_solve_attempts[identity] = attempts + 1# Read once so the handlers below report a failure without touching the context again.@@ -878,8 +897,19 @@ async def solve_challenge_when_evidence_settles(page = await browser_state.get_working_page()if page is None:return False+ # Crediting a token that was already there would let an unrelated widget's response pass+ # this challenge off as solved, so the transition is what counts. An unreadable baseline+ # withholds that credit rather than failing the solve that has not happened yet.+ try:+ token_before = await _bounded_code_block_recaptcha_token_populated(page)+ except Exception:+ token_before = None# In-DOM checkbox, then the Turnstile extension, then the reCAPTCHA token route.- await _code_block_solve_captcha_builtin(page, organization_id=organization_id)+ arm_passed = await _code_block_solve_captcha_builtin(+ page,+ organization_id=organization_id,+ browser_session_id=ctx.browser_session_id,+ )except CodeBlockCaptchaError:LOG.info("copilot_scout_captcha_solve_failed",@@ -898,16 +928,29 @@ async def solve_challenge_when_evidence_settles()return False- # "attempted", not "solved": the routes return normally when their probes match nothing, so a- # carrier asserted by challenge_state alone can complete here without anything being solved.+ # A reCAPTCHA widget stays rendered after it is satisfied, so its continued presence says+ # nothing; the response token going from absent to present is what the platform treats as the+ # authoritative solve signal. An unreadable read withholds the credit rather than inventing it.+ try:+ token_after = await _bounded_code_block_recaptcha_token_populated(page)+ judged_by_token = await _code_block_recaptcha_response_field_present(page)+ except Exception:+ token_after = None+ judged_by_token = False+ # Turnstile and plain checkbox challenges carry no response field, so an arm passing is all+ # there is to go on; where a field exists, the transition is what separates this solve from a+ # response some other widget left behind.+ solved = arm_passed and (token_before is False and token_after is True if judged_by_token else True)+if observed_after_interaction:- _record_challenge_encounter(ctx, carrier, outcome="attempted")+ _record_challenge_encounter(ctx, carrier, outcome="solved" if solved else "attempted")LOG.info(- "copilot_scout_captcha_solved",+ "copilot_scout_captcha_solve_attempted",url=url,organization_id=organization_id,+ solved=solved,)- return True+ return solveddef _page_evidence_has_selector(value: Any, selector: str) -> bool:diff --git a/skyvern/forge/sdk/copilot/turn_intent.py b/skyvern/forge/sdk/copilot/turn_intent.pyindex 3e780771e..be805e6bd 100644--- a/skyvern/forge/sdk/copilot/turn_intent.py+++ b/skyvern/forge/sdk/copilot/turn_intent.py@@ -3,6 +3,7 @@ from __future__ import annotationsimport asyncioimport reimport time+import unicodedatafrom collections.abc import Mappingfrom difflib import SequenceMatcherfrom enum import StrEnum@@ -12,6 +13,7 @@ import yamlfrom pydantic import BaseModel, ConfigDict, Field, field_validator, model_validatorfrom skyvern.config import settings+from skyvern.constants import DEFAULT_WORKFLOW_TITLESfrom skyvern.forge.prompts import prompt_enginefrom skyvern.forge.sdk.api.llm.api_handler import LLMAPIHandlerfrom skyvern.forge.sdk.copilot.context import StructuredContext, sanitize_global_llm_context_for_prompt@@ -52,7 +54,10 @@ class TurnIntentMode(StrEnum):_LOW_CONFIDENCE_MUTATION_THRESHOLD = 0.5-_MUTATING_CLASSIFIER_MODES = frozenset((TurnIntentMode.BUILD, TurnIntentMode.EDIT, TurnIntentMode.DRAFT_ONLY))+_TITLE_MAX_CHARS = 60+# Cc/Cf strip control and bidi-override characters; Cs/Co/Cn drop surrogates and unassigned.+_TITLE_DISALLOWED_UNICODE_CATEGORIES = frozenset(("Cc", "Cf", "Cs", "Co", "Cn"))+MUTATING_CLASSIFIER_MODES = frozenset((TurnIntentMode.BUILD, TurnIntentMode.EDIT, TurnIntentMode.DRAFT_ONLY))_EDIT_SPECIFIC_TARGET_ENTITY_TYPES = frozenset(("block", "run", "proposed_workflow", "latest_assistant_proposal", "proposal", "workflow_change"))@@ -256,6 +261,9 @@ class TurnIntentClassification(BaseModel):target_entities: dict[str, list[str]] = Field(default_factory=dict)missing_context_question: str | None = Nonereason_codes: list[TurnIntentReasonCode] = Field(default_factory=list)+ # A naming candidate only; it never carries rename authority. Consumers must+ # route it through ``sanitize_workflow_title_candidate``.+ workflow_title: str | None = None@field_validator("required_context", mode="after")@classmethod@@ -329,6 +337,26 @@ class TurnIntentClassifierResult(BaseModel):_GOAL_MAX_CHARS = 240+def sanitize_workflow_title_candidate(value: object) -> str | None:+ """The single sink every naming candidate passes through, model-produced or derived.++ Rejects the placeholder titles outright: a candidate equal to a default would defeat+ the value-based "is it still unnamed?" test every caller relies on.+ """+ if not isinstance(value, str):+ return None+ text = unicodedata.normalize("NFKC", value)+ text = "".join(char for char in text if unicodedata.category(char) not in _TITLE_DISALLOWED_UNICODE_CATEGORIES)+ text = redact_raw_secrets_for_prompt(" ".join(text.split()).strip().strip("\"'").strip())+ if not text or text in DEFAULT_WORKFLOW_TITLES:+ return None+ if len(text) > _TITLE_MAX_CHARS:+ head = text[:_TITLE_MAX_CHARS]+ cut = head.rsplit(" ", 1)[0] if " " in head else head+ text = cut.rstrip(" ,.;:-")+ return text or None++def _normalize_user_goal(user_message: str) -> str:goal = redact_raw_secrets_for_prompt((user_message or "").strip())if len(goal) <= _GOAL_MAX_CHARS:@@ -527,6 +555,7 @@ def _turn_intent_classification_from_raw(raw: object) -> TurnIntentClassificatioif isinstance(missing_context_question, str) and missing_context_question.strip()else None,reason_codes=_coerce_reason_codes(payload.get("reason_codes")),+ workflow_title=sanitize_workflow_title_candidate(payload.get("workflow_title")),)@@ -903,7 +932,7 @@ def build_turn_intent(if (classification is not None- and mode in _MUTATING_CLASSIFIER_MODES+ and mode in MUTATING_CLASSIFIER_MODESand confidence < _LOW_CONFIDENCE_MUTATION_THRESHOLD):mode = TurnIntentMode.CLARIFYdiff --git a/skyvern/forge/sdk/copilot/turn_ownership.py b/skyvern/forge/sdk/copilot/turn_ownership.pyindex b3bb76223..c0c59605f 100644--- a/skyvern/forge/sdk/copilot/turn_ownership.py+++ b/skyvern/forge/sdk/copilot/turn_ownership.py@@ -12,7 +12,6 @@ import structlogfrom skyvern.forge.sdk.copilot.blocker_signal import (GENUINELY_TERMINAL_BLOCKER_REASON_CODES,- SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE,CopilotToolBlockerSignal,blocker_signal_is_genuinely_terminal,build_llm_tool_error_payload,@@ -36,10 +35,6 @@ class TurnClaimant(StrEnum):GENUINELY_TERMINAL = "genuinely_terminal"OUTPUT_CONTRACT_ACTUATION = "output_contract_actuation"CREDENTIAL_PRIORITY_CHURN = "credential_priority_authoring_churn"- ACTUATION_OBLIGATION_FILL = "actuation_obligation_fill"- ACTUATION_OBLIGATION_LOGIN_COMPLETION = "actuation_obligation_login_completion"- CREDENTIAL_SCOUT_REOPEN = "credential_scout_reopen"- SYNTHESIZED_BLOCK_PERSISTENCE_FORCE = "synthesized_block_persistence_force"CODE_AUTHORING_CHURN = "code_authoring_guardrail_churn"LOOP_DETECTED = "loop_detected"@@ -61,20 +56,7 @@ _PRECEDENCE_ORDER: tuple[TurnClaimant, ...] = (# Bound-8 credential-priority churn defers only to terminal evidence and the actuation ladder,# preserving the credential-scout reply.TurnClaimant.CREDENTIAL_PRIORITY_CHURN,- # Actuation-obligation fill carve-out admits the required fill tool through the persistence- # gate; predicate-gated at its call site, never an unconditional rank flip.- TurnClaimant.ACTUATION_OBLIGATION_FILL,- # Login-completion carve-out admits the submit and 2FA-confirm interactions the admitted fills- # began; predicate-gated at its call site and bounded by the post-credential commit.- TurnClaimant.ACTUATION_OBLIGATION_LOGIN_COMPLETION,- # Post-run page-path repair admits only its outcome-bound click/Enter window.- # One-shot credential-scout reopen admits evaluate through the persistence gate;- # predicate-gated at its call site.- TurnClaimant.CREDENTIAL_SCOUT_REOPEN,- # A budget-exhausted churn halt is the loop exit and outranks the persistence force, whose- # accepted-save channel has already failed by then; the pair only co-occurs on synthesized turns.TurnClaimant.CODE_AUTHORING_CHURN,- TurnClaimant.SYNTHESIZED_BLOCK_PERSISTENCE_FORCE,# Loop detectors defer to every steering gate above them.TurnClaimant.LOOP_DETECTED,)@@ -86,10 +68,6 @@ CLAIMANT_REASON_CODE_FAMILIES: dict[TurnClaimant, frozenset[str]] = {TurnClaimant.GENUINELY_TERMINAL: GENUINELY_TERMINAL_BLOCKER_REASON_CODES,TurnClaimant.OUTPUT_CONTRACT_ACTUATION: frozenset(),TurnClaimant.CREDENTIAL_PRIORITY_CHURN: frozenset({"credential_priority_authoring_churn"}),- TurnClaimant.ACTUATION_OBLIGATION_FILL: frozenset(),- TurnClaimant.ACTUATION_OBLIGATION_LOGIN_COMPLETION: frozenset(),- TurnClaimant.CREDENTIAL_SCOUT_REOPEN: frozenset(),- TurnClaimant.SYNTHESIZED_BLOCK_PERSISTENCE_FORCE: frozenset({SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE}),TurnClaimant.CODE_AUTHORING_CHURN: frozenset({"code_authoring_guardrail_churn"}),TurnClaimant.LOOP_DETECTED: frozenset({@@ -107,16 +85,6 @@ _CLAIMANT_BY_REASON_CODE: dict[str, TurnClaimant] = {for reason_code in reason_codes}-# Claimants whose ownership lasts only for the duration of the claiming call: the claim records-# reachability and conflicts, but never suppresses a later gate or render on the same turn.-_TRANSIENT_CLAIMANTS = frozenset(- {- TurnClaimant.ACTUATION_OBLIGATION_FILL,- TurnClaimant.ACTUATION_OBLIGATION_LOGIN_COMPLETION,- TurnClaimant.CREDENTIAL_SCOUT_REOPEN,- }-)-def claimant_outranks(candidate: TurnClaimant, incumbent: TurnClaimant) -> bool:return _PRECEDENCE_RANK[candidate] < _PRECEDENCE_RANK[incumbent]@@ -217,8 +185,6 @@ def effective_signal_claimant(ctx: AgentContext, signal: CopilotToolBlockerSignadef _claim_is_live(ctx: AgentContext, claim: PrecedenceClaim) -> bool:- if claim.claimant in _TRANSIENT_CLAIMANTS:- return Falseif claim.claimant is TurnClaimant.OUTPUT_CONTRACT_ACTUATION:return output_contract_ladder_unresolved(ctx)if claim.claimant is TurnClaimant.GENUINELY_TERMINAL and turn_halt_is_genuinely_terminal(ctx.turn_halt):diff --git a/skyvern/forge/sdk/copilot/workflow_yaml.py b/skyvern/forge/sdk/copilot/workflow_yaml.pyindex f066b5342..0990f8bae 100644--- a/skyvern/forge/sdk/copilot/workflow_yaml.py+++ b/skyvern/forge/sdk/copilot/workflow_yaml.py@@ -7,7 +7,7 @@ from typing import Anyimport structlogimport yaml-from skyvern.constants import DEFAULT_LOGIN_PROMPT+from skyvern.constants import DEFAULT_LOGIN_PROMPT, DEFAULT_WORKFLOW_TITLESfrom skyvern.exceptions import WorkflowNotFoundfrom skyvern.forge import appfrom skyvern.forge.sdk.copilot.block_type_aliases import normalize_copilot_block_type_alias@@ -29,6 +29,9 @@ from skyvern.utils.yaml_loader import safe_load_no_datesLOG = structlog.get_logger()+# Wide enough that safe_dump never folds a title onto a second line.+_YAML_NO_FOLD_WIDTH = 1 << 30+def _proxy_location_alias_key(value: str) -> str:return "_".join(value.strip().upper().replace("-", "_").split())@@ -366,6 +369,81 @@ def _yaml_pin_saved_session_ip(workflow_yaml: str | None) -> bool | None:return _yaml_bool_setting(workflow_yaml, "pin_saved_session_ip")+def _is_root_title_key(line: str) -> bool:+ """Whether ``line`` opens the document's own ``title`` key, quoted or not."""+ if not line or line[:1].isspace() or ":" not in line:+ return False+ return line.split(":", 1)[0].strip().strip("\"'") == "title"+++def _verified_title_edit(original: str, edited: str, title: str) -> str:+ """Keep a line-level edit only when the parser agrees it set the root title.++ The scan reasons about lines while every consumer reasons about the parsed document;+ a shape that makes those disagree keeps the original rather than a broken draft.+ """+ try:+ if workflow_yaml_title(edited) == title:+ return edited+ except Exception:+ pass+ return original+++def with_workflow_yaml_title(workflow_yaml: str | None, title: str) -> str:+ """Set ``title`` on a workflow YAML document, preserving the rest byte-for-byte.++ Line-level rather than a parse/dump round-trip, which would reflow the model's block+ scalars (code bodies, prompts) and churn every downstream diff.+ """+ # Sits on the proposal-persist path: a formatting concern must never cost the proposal.+ if not workflow_yaml or not isinstance(title, str) or not title.strip():+ return workflow_yaml or ""+ # Unbounded width: at PyYAML's default of 80 a long title folds onto a second line, and+ # a one-line replacement would leave the old continuation to be folded back into the new+ # scalar — which compounds on every accepted turn.+ serialized = yaml.safe_dump(+ {"title": title}, default_flow_style=False, allow_unicode=True, width=_YAML_NO_FOLD_WIDTH+ ).strip()+ lines = workflow_yaml.splitlines()+ trailing_newline = "\n" if workflow_yaml.endswith("\n") else ""+ for index, line in enumerate(lines):+ if not _is_root_title_key(line):+ continue+ # Drop the old value's continuation lines (a folded or block scalar), which would+ # otherwise dangle after the replacement and be absorbed into the new title. A blank+ # line inside a block scalar is part of it, so look past one for more indented text.+ end = index + 1+ while end < len(lines):+ if lines[end][:1].isspace() and lines[end].strip():+ end += 1+ continue+ following = next((offset for offset in range(end, len(lines)) if lines[offset].strip()), None)+ if not lines[end].strip() and following is not None and lines[following][:1].isspace():+ end = following + 1+ continue+ break+ lines[index:end] = [serialized]+ return _verified_title_edit(workflow_yaml, "\n".join(lines) + trailing_newline, title)+ # Prepending ahead of a leading ``---`` would make two documents, which safe_load rejects.+ insert_at = 1 if lines and lines[0].strip() == "---" else 0+ lines.insert(insert_at, serialized)+ return _verified_title_edit(workflow_yaml, "\n".join(lines) + trailing_newline, title)+++def workflow_yaml_title(workflow_yaml: str | None) -> str | None:+ if not workflow_yaml:+ return None+ try:+ parsed = yaml.safe_load(workflow_yaml)+ except yaml.YAMLError:+ return None+ if not isinstance(parsed, dict):+ return None+ title = parsed.get("title")+ return title.strip() if isinstance(title, str) and title.strip() else None++def _redact_credentials_before_persistence(workflow_yaml: str, workflow_permanent_id: str, credential_values: Collection[str]) -> str:@@ -466,11 +544,27 @@ async def _process_workflow_yaml(if pin_saved_session_ip is None:pin_saved_session_ip = bool(current_workflow and getattr(current_workflow, "pin_saved_session_ip", False))+ # Omission-must-inherit, same as the settings above: the model re-emits whatever title it was+ # handed, so a draft still carrying the placeholder must not un-name an agent already named.+ title = workflow_yaml_request.title or ""+ if title in DEFAULT_WORKFLOW_TITLES or not title:+ # First candidate that is an actual name: the submitted draft may still carry the+ # placeholder while canonical holds a rename that landed mid-turn, and vice versa.+ candidates = (+ workflow_yaml_title(settings_fallback_yaml),+ settings_fallback_workflow.title if settings_fallback_workflow is not None else None,+ )+ for candidate in candidates:+ named = (candidate or "").strip()+ if named and named not in DEFAULT_WORKFLOW_TITLES:+ title = named+ break+now = datetime.now(timezone.utc)return Workflow(workflow_id=workflow_id,organization_id=organization_id,- title=workflow_yaml_request.title or "",+ title=title,workflow_permanent_id=workflow_permanent_id,version=1,is_saved_task=workflow_yaml_request.is_saved_task,diff --git a/skyvern/forge/sdk/db/repositories/tasks.py b/skyvern/forge/sdk/db/repositories/tasks.pyindex 048c434d9..6db3823eb 100644--- a/skyvern/forge/sdk/db/repositories/tasks.py+++ b/skyvern/forge/sdk/db/repositories/tasks.py@@ -6,7 +6,7 @@ from decimal import Decimalfrom typing import Any, Sequenceimport structlog-from sqlalchemy import and_, delete, distinct, func, select, tuple_, update+from sqlalchemy import and_, delete, func, select, updatefrom skyvern.forge.sdk.db._error_handling import db_operationfrom skyvern.forge.sdk.db.base_alchemy_db import read_retry@@ -306,11 +306,14 @@ class TasksRepository(BaseRepository):where s.task_id in task_ids"""async with self.Session() as session:- query = (- select(func.count(distinct(tuple_(StepModel.task_id, StepModel.order))))+ unique_step_orders = (+ select(StepModel.task_id, StepModel.order).where(StepModel.task_id.in_(task_ids)).where(StepModel.organization_id == organization_id)+ .distinct()+ .subquery())+ query = select(func.count()).select_from(unique_step_orders)return (await session.execute(query)).scalar()@db_operation("get_task_step_models")diff --git a/skyvern/forge/sdk/db/repositories/workflows.py b/skyvern/forge/sdk/db/repositories/workflows.pyindex 40f52e9d9..0c3a7500e 100644--- a/skyvern/forge/sdk/db/repositories/workflows.py+++ b/skyvern/forge/sdk/db/repositories/workflows.py@@ -7,7 +7,7 @@ from typing import Any, castimport structlogfrom sqlalchemy import exists, func, or_, select, update-from skyvern.constants import DEFAULT_SCRIPT_RUN_ID+from skyvern.constants import DEFAULT_SCRIPT_RUN_ID, DEFAULT_WORKFLOW_TITLESfrom skyvern.forge.sdk.browser_action_policy import BrowserActionPolicy, declare_policyfrom skyvern.forge.sdk.db._error_handling import db_operationfrom skyvern.forge.sdk.db._sentinels import _UNSET@@ -115,6 +115,44 @@ class WorkflowsRepository(BaseRepository):query = query.filter_by(organization_id=organization_id)return carried_policy(await session.scalar(query))+ @db_operation("rename_workflow_if_still_default")+ async def rename_workflow_if_still_default(+ self,+ workflow_id: str,+ workflow_permanent_id: str,+ organization_id: str,+ title: str,+ ) -> bool:+ """Name a workflow only while it is still unnamed, atomically; returns whether it renamed.++ Deliberately does not stamp ``created_by``/``edited_by`` — naming is not authorship, and+ ``is_workflow_copilot_authored`` treats any copilot stamp as permanent lineage.+ """+ newer_version_exists = (+ select(WorkflowModel.workflow_id)+ .filter_by(workflow_permanent_id=workflow_permanent_id, organization_id=organization_id)+ .filter(WorkflowModel.workflow_id != workflow_id)+ .filter(WorkflowModel.deleted_at.is_(None))+ .filter(+ WorkflowModel.version+ > select(WorkflowModel.version).filter_by(workflow_id=workflow_id).scalar_subquery()+ )+ )+ rename_query = (+ update(WorkflowModel)+ .where(WorkflowModel.workflow_id == workflow_id)+ .where(WorkflowModel.workflow_permanent_id == workflow_permanent_id)+ .where(WorkflowModel.organization_id == organization_id)+ .where(WorkflowModel.deleted_at.is_(None))+ .where(WorkflowModel.title.in_(DEFAULT_WORKFLOW_TITLES))+ .where(~exists(newer_version_exists))+ .values(title=title)+ )+ async with self.Session() as session:+ result = await session.execute(rename_query)+ await session.commit()+ return bool(result.rowcount)+@db_operation("create_workflow")async def create_workflow(self,diff --git a/skyvern/forge/sdk/routes/agent_protocol.py b/skyvern/forge/sdk/routes/agent_protocol.pyindex c19448def..04978943c 100644--- a/skyvern/forge/sdk/routes/agent_protocol.py+++ b/skyvern/forge/sdk/routes/agent_protocol.py@@ -3035,6 +3035,15 @@ async def get_artifact_content(sig=sig,keyring=keyring,):+ # Signature length is the tell that separates a corrupted URL from an expired+ # one: sign_artifact_url always emits exactly 43 base64url characters.+ LOG.warning(+ "Rejected artifact content request with an unverifiable signature",+ artifact_id=artifact_id,+ kid=kid,+ expiry=expiry,+ signature_length=len(sig),+ )raise HTTPException(status_code=http_status.HTTP_403_FORBIDDEN,detail="Invalid or expired artifact URL",diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.pyindex ef9f641f0..0624004e1 100644--- a/skyvern/forge/sdk/routes/workflow_copilot.py+++ b/skyvern/forge/sdk/routes/workflow_copilot.py@@ -18,6 +18,7 @@ from sse_starlette import EventSourceResponsefrom skyvern import analyticsfrom skyvern.config import settings+from skyvern.constants import DEFAULT_WORKFLOW_TITLESfrom skyvern.forge import appfrom skyvern.forge.prompts import prompt_enginefrom skyvern.forge.sdk.api.llm.api_handler import LLMAPIHandler@@ -77,6 +78,7 @@ from skyvern.forge.sdk.copilot.turn_outcome import (from skyvern.forge.sdk.copilot.workflow_yaml import _normalize_copilot_yaml as _normalize_copilot_yamlfrom skyvern.forge.sdk.copilot.workflow_yaml import _process_workflow_yaml as _process_workflow_yamlfrom skyvern.forge.sdk.copilot.workflow_yaml import _repair_next_block_label_chain as _repair_next_block_label_chain+from skyvern.forge.sdk.copilot.workflow_yaml import with_workflow_yaml_titlefrom skyvern.forge.sdk.core import skyvern_contextfrom skyvern.forge.sdk.core.event_source_stream import EventSourceStream, FastAPIEventSourceStreamfrom skyvern.forge.sdk.routes.routers import base_router@@ -711,7 +713,9 @@ async def _persist_completion_criteria_state(chat: Any, agent_result: AgentResuldef _build_proposed_workflow_data(updated_workflow: Workflow, agent_result: AgentResult) -> dict[str, Any]:proposed_data = dict(updated_workflow.model_dump(mode="json"))if agent_result.workflow_yaml:- proposed_data["_copilot_yaml"] = agent_result.workflow_yaml+ # Accept reparses this YAML and never passes through _process_workflow_yaml, so the+ # title it carries has to be the effective one rather than whatever the model typed.+ proposed_data["_copilot_yaml"] = with_workflow_yaml_title(agent_result.workflow_yaml, updated_workflow.title)code_artifact_metadata = getattr(agent_result, "code_artifact_metadata", None)if code_artifact_metadata:proposed_data["_copilot_code_artifact_metadata"] = code_artifact_metadata@@ -1253,10 +1257,26 @@ async def _restore_workflow_definition(original_workflow: Workflow | None, organ"""if not original_workflow:return+ # Rolling a canvas back must not un-name the agent: naming is a separate, one-shot+ # write that only ever fires on a placeholder, so restoring the pre-turn placeholder+ # over a name would leave the user watching their agent revert to "New Agent".+ restored_title = original_workflow.title+ if restored_title in DEFAULT_WORKFLOW_TITLES:+ # Best-effort: preserving a name must never be the reason a rollback fails, and this+ # lookup raises rather than returning None when the workflow is gone.+ try:+ current = await app.WORKFLOW_SERVICE.get_workflow_by_permanent_id(+ workflow_permanent_id=original_workflow.workflow_permanent_id,+ organization_id=organization_id,+ )+ except Exception:+ current = None+ if current is not None and current.title not in DEFAULT_WORKFLOW_TITLES:+ restored_title = current.titleawait app.WORKFLOW_SERVICE.update_workflow_definition(workflow_id=original_workflow.workflow_id,organization_id=organization_id,- title=original_workflow.title,+ title=restored_title,description=original_workflow.description,workflow_definition=original_workflow.workflow_definition,proxy_location=original_workflow.proxy_location,@@ -2119,6 +2139,7 @@ async def _new_copilot_chat_post(organization_id=organization.organization_id,chat_request=chat_request,chat_history=convert_to_history_messages(chat_messages[-CHAT_HISTORY_CONTEXT_MESSAGES:]),+ prior_user_messages=convert_to_history_messages(chat_messages),global_llm_context=global_llm_context,debug_run_info_text=debug_run_info_text,llm_api_handler=llm_api_handler,@@ -2529,7 +2550,9 @@ async def workflow_copilot_chat_post(# _copilot_yaml is what /apply-proposed-workflow re-parses into# WorkflowCreateYAMLRequest. Without it, Accept 400s.if updated_workflow_yaml:- proposed_data["_copilot_yaml"] = updated_workflow_yaml+ proposed_data["_copilot_yaml"] = with_workflow_yaml_title(+ updated_workflow_yaml, updated_workflow.title+ )await app.DATABASE.workflow_params.update_workflow_copilot_chat(organization_id=chat.organization_id,workflow_copilot_chat_id=chat.workflow_copilot_chat_id,diff --git a/skyvern/forge/sdk/schemas/workflow_copilot.py b/skyvern/forge/sdk/schemas/workflow_copilot.pyindex 6045184a5..64b769df5 100644--- a/skyvern/forge/sdk/schemas/workflow_copilot.py+++ b/skyvern/forge/sdk/schemas/workflow_copilot.py@@ -262,6 +262,7 @@ class WorkflowCopilotStreamMessageType(StrEnum):WORKFLOW_DRAFT = "workflow_draft"CREDENTIAL_REQUIRED = "credential_required"CODEGEN_PROGRESS = "codegen_progress"+ TITLE_UPDATE = "title_update"class WorkflowCopilotProcessingUpdate(BaseModel):@@ -482,6 +483,16 @@ class WorkflowCopilotWorkflowDraftUpdate(BaseModel):)+class WorkflowCopilotTitleUpdate(BaseModel):+ type: WorkflowCopilotStreamMessageType = Field(+ WorkflowCopilotStreamMessageType.TITLE_UPDATE, description="Message type"+ )+ turn_id: str = Field(..., description="UUID for the turn that named the agent")+ workflow_permanent_id: str = Field(..., description="The agent that was named")+ title: str = Field(..., description="The persisted title; only ever emitted after the write succeeded")+ timestamp: datetime = Field(..., description="Server timestamp")++class WorkflowCopilotCredentialRequiredUpdate(BaseModel):type: WorkflowCopilotStreamMessageType = Field(WorkflowCopilotStreamMessageType.CREDENTIAL_REQUIRED, description="Message type"diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.pyindex 42aa928d0..1b2e81f45 100644--- a/skyvern/forge/sdk/workflow/models/block.py+++ b/skyvern/forge/sdk/workflow/models/block.py@@ -3783,6 +3783,10 @@ _CODE_BLOCK_RECAPTCHA_ANCHOR_PATHS = ("/recaptcha/api2/anchor", "/recaptcha/ente_CODE_BLOCK_RECAPTCHA_ANCHOR_ARM_TIMEOUT_SECONDS = 5# The extension arm polls a solver over the network; the scout caller has no enclosing bound._CODE_BLOCK_EXTENSION_ARM_TIMEOUT_SECONDS = 12+# Same reason, sized from measured solves: a correct solver task returns in ~25s, so this covers one+# with headroom while cutting the losing task of the pair, which only ends at its own 180s timeout.+_CODE_BLOCK_TOKEN_ARM_TIMEOUT_SECONDS = 90+_CODE_BLOCK_WIDGET_RESET_TIMEOUT_SECONDS = 3# Google's widget flips aria-checked after its own animation; a shorter wait reads as unsolved._CODE_BLOCK_RECAPTCHA_ANCHOR_SETTLE_MS = 2_000_CODE_BLOCK_CAPTCHA_CONTINUE_SELECTOR = ", ".join(@@ -3832,23 +3836,34 @@ async def _bounded_code_block_recaptcha_token_populated(scope: Frame | Page | Rereturn False+async def _code_block_recaptcha_response_field_present(scope: Page | RecordingPage) -> bool:+ """Whether the page carries a reCAPTCHA response field at all.++ Turnstile and plain checkbox challenges have none, so a token transition cannot judge whether+ they were solved.+ """+ return await _bounded_code_block_locator_count(scope.locator(_CODE_BLOCK_RECAPTCHA_RESPONSE_SELECTOR)) > 0++async def _code_block_solve_captcha_builtin(page: Page | RecordingPage,*,organization_id: str | None = None,workflow_run_id: str | None = None,-) -> None:- """Solve a detected challenge through the bounded platform ladder.+ browser_session_id: str | None = None,+) -> bool:+ """Solve a detected challenge through the bounded platform ladder; True when an arm passed.The initial structural probes are intentionally cheap. Solver routes are never- called when neither a challenge control nor vendor marker is present.+ called when neither a challenge control nor vendor marker is present, and False+ distinguishes that no-op from a solve so callers do not re-perceive a page nothing touched."""checkbox = page.locator(_CODE_BLOCK_CAPTCHA_CHECKBOX_SELECTOR)checkbox_count = await _bounded_code_block_locator_count(checkbox)marker = page.locator(_CODE_BLOCK_CAPTCHA_MARKER_SELECTOR)marker_count = await _bounded_code_block_locator_count(marker)if checkbox_count == 0 and marker_count == 0:- return+ return Falseif checkbox_count == 1:candidate = checkbox.first@@ -3864,14 +3879,16 @@ async def _code_block_solve_captcha_builtin(await continuation_candidate.click()await page.wait_for_timeout(100)if await _bounded_code_block_locator_count(checkbox) == 0:- return+ return Trueelse:# Checkbox challenges commonly complete on the checkbox# interaction itself and expose no associated continuation.- return+ return Trueexcept Exception:LOG.info("code block CAPTCHA checkbox arm did not solve", arm="dom_checkbox")+ anchor_clicked = False+ anchor_left_token = False# A page-level locator cannot cross into reCAPTCHA's anchor iframe. Click the checkbox in-frame.try:async with asyncio.timeout(_CODE_BLOCK_RECAPTCHA_ANCHOR_ARM_TIMEOUT_SECONDS):@@ -3894,11 +3911,12 @@ async def _code_block_solve_captcha_builtin(breakpage_url_before_click = urlparse(page.url)._replace(fragment="").geturl()await candidate.click()+ anchor_clicked = TrueLOG.info("code block CAPTCHA anchor frame clicked", arm="recaptcha_anchor_frame")await page.wait_for_timeout(_CODE_BLOCK_RECAPTCHA_ANCHOR_SETTLE_MS)if frame.is_detached() and urlparse(page.url)._replace(fragment="").geturl() != page_url_before_click:LOG.info("code block CAPTCHA anchor frame solved after navigation", arm="recaptcha_anchor_frame")- return+ return Truetoken_is_populated = await _bounded_code_block_recaptcha_token_populated(token_scope)if (await candidate.get_attribute("aria-checked") == "true"@@ -3906,27 +3924,51 @@ async def _code_block_solve_captcha_builtin(and token_is_populated is True):LOG.info("code block CAPTCHA anchor frame solved", arm="recaptcha_anchor_frame")- return+ return True+ # An inconclusive baseline fails the test above even when the click earned a token,+ # so read the widget rather than the verdict before deciding a reset is free.+ anchor_left_token = token_is_populated is Truebreakexcept Exception:LOG.info("code block CAPTCHA anchor frame arm did not solve", arm="recaptcha_anchor_frame")+ # Clicking the anchor escalates to an image challenge whose overlay covers the page and+ # outlives the arm, so every later click lands on it instead of the form. Resetting is the only+ # thing that closes it (Escape does not), and it is skipped when the click left a token behind,+ # because a reset discards one.+ if anchor_clicked and not anchor_left_token:+ # The settle window is approximate and the reset is destructive, so look once more: a solve+ # that landed just past it would otherwise be discarded and escalated all over again.+ anchor_left_token = await _bounded_code_block_recaptcha_token_populated(page) is True+ if anchor_clicked and not anchor_left_token:+ try:+ async with asyncio.timeout(_CODE_BLOCK_WIDGET_RESET_TIMEOUT_SECONDS):+ await page.evaluate(+ "() => { const g = window.grecaptcha;"+ " const api = g && g.enterprise && g.enterprise.reset ? g.enterprise : g;"+ " if (api && api.reset) api.reset(); }"+ )+ except Exception:+ LOG.info("code block CAPTCHA widget reset did not run", arm="recaptcha_anchor_frame")+try:async with asyncio.timeout(_CODE_BLOCK_EXTENSION_ARM_TIMEOUT_SECONDS):if await app.AGENT_FUNCTION.auto_solve_captchas(page):- return+ return Trueexcept Exception:LOG.info("code block CAPTCHA extension arm did not solve", arm="extension")recaptcha = page.locator(_CODE_BLOCK_RECAPTCHA_MARKER_SELECTOR)if await _bounded_code_block_locator_count(recaptcha) > 0:try:- if await app.AGENT_FUNCTION.solve_recaptcha_token(- page,- organization_id=organization_id,- workflow_run_id=workflow_run_id,- ):- return+ async with asyncio.timeout(_CODE_BLOCK_TOKEN_ARM_TIMEOUT_SECONDS):+ if await app.AGENT_FUNCTION.solve_recaptcha_token(+ page,+ organization_id=organization_id,+ workflow_run_id=workflow_run_id,+ browser_session_id=browser_session_id,+ ):+ return Trueexcept Exception:LOG.info("code block CAPTCHA token arm did not solve", arm="token")diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.pyindex b5a24499c..3f9418ab1 100644--- a/skyvern/forge/sdk/workflow/service.py+++ b/skyvern/forge/sdk/workflow/service.py@@ -36,7 +36,11 @@ import skyvernfrom skyvern import analyticsfrom skyvern.client.types.output_parameter import OutputParameter as BlockOutputParameterfrom skyvern.config import settings-from skyvern.constants import GET_DOWNLOADED_FILES_TIMEOUT, SAVE_DOWNLOADED_FILES_TIMEOUT+from skyvern.constants import (+ DEFAULT_WORKFLOW_TITLES,+ GET_DOWNLOADED_FILES_TIMEOUT,+ SAVE_DOWNLOADED_FILES_TIMEOUT,+)from skyvern.exceptions import (BlockedHost,BlockNotFound,@@ -10009,7 +10013,7 @@ class WorkflowService:# Generate meaningful title if using default and has blockstitle = request.title- if title == DEFAULT_WORKFLOW_TITLE and request.workflow_definition.blocks:+ if title in DEFAULT_WORKFLOW_TITLES and request.workflow_definition.blocks:generated_title = await generate_workflow_title(organization_id=organization_id,blocks=request.workflow_definition.blocks,diff --git a/skyvern/webeye/real_browser_manager.py b/skyvern/webeye/real_browser_manager.pyindex 607367611..4d788ea68 100644--- a/skyvern/webeye/real_browser_manager.py+++ b/skyvern/webeye/real_browser_manager.py@@ -1026,7 +1026,11 @@ class RealBrowserManager(BrowserManager):finalize: bool = True,) -> list[VideoArtifact]:if len(browser_state.browser_artifacts.video_artifacts) == 0:- LOG.warning(+ # Empty is the expected state on browsers with no local Playwright recording+ # (vendor/CDP/persistent sessions) until finalize time, when vendor recordings+ # have been attached and a still-empty list means the recording is missing.+ log = LOG.warning if finalize else LOG.debug+ log("Video data not found for task",task_id=task_id,workflow_id=workflow_id,diff --git a/tests/unit/browser_extension/test_protocol.py b/tests/unit/browser_extension/test_protocol.pyindex e2c0bfc33..321e19030 100644--- a/tests/unit/browser_extension/test_protocol.py+++ b/tests/unit/browser_extension/test_protocol.py@@ -1,10 +1,15 @@from __future__ import annotations+import base64+import hashlibimport json+import zipfile+from pathlib import Pathimport pytestfrom skyvern.browser_extension.errors import BrowserExtensionError+from skyvern.browser_extension.package_extension import package_extensionfrom skyvern.browser_extension.protocol import (ALLOWED_CDP_METHOD_PREFIXES,ALLOWED_EVENTS,@@ -62,7 +67,7 @@ def test_unknown_message_type_raises() -> None:def test_protocol_allowlists_match_contract() -> None:assert PROTOCOL_VERSION == 1- assert EXTENSION_ID == "fmamdhmfeihjjaiheideemihnbpnokin"+ assert EXTENSION_ID == "dhommdmblflboaledbbfkdaapkadphlp"assert ALLOWED_OPS == frozenset({"debugger.attach",@@ -99,6 +104,31 @@ def test_protocol_allowlists_match_contract() -> None:)+def test_manifest_key_derives_extension_id() -> None:+ extension_dir = Path(__file__).parents[3] / "skyvern" / "browser_extension" / "extension"+ manifest = json.loads((extension_dir / "manifest.json").read_text())+ public_key = base64.b64decode(manifest["key"], validate=True)+ digest_prefix = hashlib.sha256(public_key).hexdigest()[:32]+ derived_extension_id = "".join(chr(ord("a") + int(nibble, 16)) for nibble in digest_prefix)++ assert derived_extension_id == EXTENSION_ID+++def test_package_extension_builds_store_upload_zip(tmp_path: Path) -> None:+ output_path = package_extension(tmp_path / "skyvern-agent.zip")++ with zipfile.ZipFile(output_path) as archive:+ names = set(archive.namelist())+ packaged_manifest = json.loads(archive.read("manifest.json"))++ assert "key" not in packaged_manifest+ assert "service_worker.js" in names+ assert "README.md" not in names++ second_path = package_extension(tmp_path / "skyvern-agent-second.zip")+ assert output_path.read_bytes() == second_path.read_bytes()++def test_build_request_checks_operation_allowlist() -> None:assert build_request("r-3", "tabs.list", {}) == {"v": 1,diff --git a/tests/unit/browser_extension/test_relay.py b/tests/unit/browser_extension/test_relay.pyindex d1ec3f01f..d22d96265 100644--- a/tests/unit/browser_extension/test_relay.py+++ b/tests/unit/browser_extension/test_relay.py@@ -118,7 +118,7 @@ async def test_pair_begin_claim_happy_path_and_pair_page_never_contains_token(page_body = await page.text()assert page.status == 200assert TOKEN not in page_body- assert "fmamdhmfeihjjaiheideemihnbpnokin" in page_body+ assert "dhommdmblflboaledbbfkdaapkadphlp" in page_bodyclaim = await session.post(http_url(relay_harness, "/pair/claim"),diff --git a/tests/unit/conftest.py b/tests/unit/conftest.pyindex 0caa49d7c..144fb632b 100644--- a/tests/unit/conftest.py+++ b/tests/unit/conftest.py@@ -109,6 +109,16 @@ def reset_collapse_xp_assignment_memo():_clear()[email protected](autouse=True)+def reset_mcp_stateless_http_mode():+ """Keep MCP transport mode from leaking between independently collected test files."""+ from skyvern.cli.core import session_manager++ session_manager.set_stateless_http_mode(False)+ yield+ session_manager.set_stateless_http_mode(False)++# -- shared copilot agent-template rendering helper --_AGENT_TEMPLATE_DEFAULTS = dict(diff --git a/tests/unit/forge/sdk/db/test_tasks_repository.py b/tests/unit/forge/sdk/db/test_tasks_repository.pynew file mode 100644index 000000000..05d8e2182--- /dev/null+++ b/tests/unit/forge/sdk/db/test_tasks_repository.py@@ -0,0 +1,43 @@+import pytest+++async def test_total_unique_step_order_count_deduplicates_retries_in_sqlite(agent_db) -> None:+ organization = await agent_db.organizations.create_organization(+ organization_name="Test organization",+ organization_id="o_test_task_step_count",+ )+ task = await agent_db.tasks.create_task(+ url="https://example.com",+ title="Count steps",+ navigation_goal=None,+ data_extraction_goal=None,+ navigation_payload=None,+ organization_id=organization.organization_id,+ )+ excluded_task = await agent_db.tasks.create_task(+ url="https://example.com",+ title="Excluded task",+ navigation_goal=None,+ data_extraction_goal=None,+ navigation_payload=None,+ organization_id=organization.organization_id,+ )++ await agent_db.tasks.create_step(task.task_id, order=0, retry_index=0, organization_id=organization.organization_id)+ await agent_db.tasks.create_step(task.task_id, order=0, retry_index=1, organization_id=organization.organization_id)+ await agent_db.tasks.create_step(task.task_id, order=1, retry_index=0, organization_id=organization.organization_id)+ await agent_db.tasks.create_step(+ excluded_task.task_id,+ order=0,+ retry_index=0,+ organization_id=organization.organization_id,+ )++ assert (+ await agent_db.tasks.get_total_unique_step_order_count_by_task_ids(+ task_ids=[task.task_id],+ organization_id=organization.organization_id,+ )+ == 2+ )diff --git a/tests/unit/test_artifact_signing.py b/tests/unit/test_artifact_signing.pyindex f7c67808b..5ef69e65c 100644--- a/tests/unit/test_artifact_signing.py+++ b/tests/unit/test_artifact_signing.py@@ -27,6 +27,7 @@ from skyvern.forge.sdk.artifact.signing import (_hmac_b64,artifact_url_expiry_seconds_for_type,effective_artifact_url_expiry_seconds,+ parse_artifact_content_url,parse_keyring,sign_artifact_url,verify_artifact_signature,@@ -408,3 +409,46 @@ class TestVerifyArtifactSignature:keys={_KID_V2: HmacKeyEntry(secret="newsecret", created_at="2026-03-12")},)assert not verify_artifact_signature(_ARTIFACT_ID, expiry, kid, sig, new_kr)+++class TestParseArtifactContentUrl:+ def test_parses_signed_first_party_url(self) -> None:+ keyring = _make_keyring()+ url = sign_artifact_url(_BASE_URL, _ARTIFACT_ID, keyring)++ parsed = parse_artifact_content_url(url, _BASE_URL)++ assert parsed is not None+ assert parsed.artifact_id == _ARTIFACT_ID+ assert parsed.kid == _KID_V1+ assert parsed.sig is not None and len(parsed.sig) == 43++ def test_parses_unsigned_first_party_url(self) -> None:+ parsed = parse_artifact_content_url(f"{_BASE_URL}{_CONTENT_PATH}", _BASE_URL)++ assert parsed is not None+ assert parsed.artifact_id == _ARTIFACT_ID+ assert (parsed.expiry, parsed.kid, parsed.sig) == (None, None, None)++ def test_tolerates_trailing_slash_on_base_url(self) -> None:+ parsed = parse_artifact_content_url(f"{_BASE_URL}{_CONTENT_PATH}?sig=abc", f"{_BASE_URL}/")++ assert parsed is not None and parsed.sig == "abc"++ def test_honors_base_url_path_prefix(self) -> None:+ parsed = parse_artifact_content_url(f"{_BASE_URL}/api{_CONTENT_PATH}", f"{_BASE_URL}/api")++ assert parsed is not None and parsed.artifact_id == _ARTIFACT_ID++ def test_rejects_foreign_origin(self) -> None:+ assert parse_artifact_content_url(f"https://evil.example.com{_CONTENT_PATH}", _BASE_URL) is None++ def test_rejects_other_first_party_paths(self) -> None:+ assert parse_artifact_content_url(f"{_BASE_URL}/v1/artifacts/{_ARTIFACT_ID}", _BASE_URL) is None+ assert parse_artifact_content_url(f"{_BASE_URL}/v1/runs/wr_1", _BASE_URL) is None++ def test_rejects_non_http_urls(self) -> None:+ assert parse_artifact_content_url("s3://bucket/key.pdf", _BASE_URL) is None++ def test_rejects_empty_base_url(self) -> None:+ assert parse_artifact_content_url(f"{_BASE_URL}{_CONTENT_PATH}", "") is Nonediff --git a/tests/unit/test_cli_commands.py b/tests/unit/test_cli_commands.pyindex 812918791..b1deee162 100644--- a/tests/unit/test_cli_commands.py+++ b/tests/unit/test_cli_commands.py@@ -5,6 +5,8 @@ from __future__ import annotationsimport importlibimport jsonimport shlex+import subprocess+import sysfrom datetime import datetime, timezonefrom pathlib import Pathfrom types import SimpleNamespace@@ -886,7 +888,7 @@ class TestTasksCommands:) -> None:from skyvern.cli import tasks as tasks_cmd- monkeypatch.setattr(tasks_cmd, "_get_client", lambda _api_key=None: object())+ monkeypatch.setattr(tasks_cmd, "_get_client", lambda _api_key=None, **_: object())monkeypatch.setattr(tasks_cmd, "_list_workflow_tasks", lambda _client, _run_id: (_ for _ in ()).throw(RuntimeError("boom")))@@ -906,7 +908,7 @@ class TestTasksCommands:@pytest.mark.parametrize("module_name", ["skyvern.cli.credentials", "skyvern.cli.tasks"])class TestCredentialClientBaseUrlGuard:def test_rejects_credential_when_base_url_uses_untouched_default(- self, module_name: str, monkeypatch: pytest.MonkeyPatch+ self, module_name: str, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture) -> None:from skyvern import config@@ -916,9 +918,34 @@ class TestCredentialClientBaseUrlGuard:monkeypatch.setattr(cli_module, "prepare_cli_runtime", lambda **_: None)monkeypatch.setattr(config, "settings", config.Settings(_env_file=None))- with pytest.raises(typer.BadParameter, match="SKYVERN_BASE_URL=https://api.skyvern.com"):+ with pytest.raises(SystemExit, match="1"):cli_module._get_client("test-api-key")+ output = " ".join(capsys.readouterr().out.split())+ assert "Set SKYVERN_BASE_URL" in output+ # The remediation must not hand the user the production URL this guard exists to protect.+ assert "https://api.skyvern.com" not in output++ def test_rejection_uses_json_envelope_when_requested(+ self, module_name: str, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture+ ) -> None:+ from skyvern import config++ monkeypatch.delenv("SKYVERN_API_KEY", raising=False)+ monkeypatch.delenv("SKYVERN_BASE_URL", raising=False)+ cli_module = importlib.import_module(module_name)+ monkeypatch.setattr(cli_module, "prepare_cli_runtime", lambda **_: None)+ monkeypatch.setattr(config, "settings", config.Settings(_env_file=None))++ with pytest.raises(SystemExit, match="1"):+ cli_module._get_client("test-api-key", action="credentials.list", json_mode=True)++ parsed = json.loads(capsys.readouterr().out)+ assert parsed["ok"] is False+ assert parsed["action"] == "credentials.list"+ assert "Set SKYVERN_BASE_URL" in parsed["error"]["message"]+ assert "https://api.skyvern.com" not in parsed["error"]["message"]+@pytest.mark.parametrize("base_url", ["https://api.skyvern.com", "https://staging.skyvern.com"])def test_allows_credential_when_base_url_is_explicitly_configured(self, module_name: str, base_url: str, monkeypatch: pytest.MonkeyPatch@@ -955,6 +982,128 @@ class TestCredentialClientBaseUrlGuard:client_constructor.assert_called_once_with(base_url="https://api.skyvern.com", api_key="PLACEHOLDER")+class TestSharedFactoryBaseUrlGuard:+ def _client_module_with_fresh_settings(self, monkeypatch: pytest.MonkeyPatch) -> object:+ from skyvern import config+ from skyvern.cli.core import client as client_module++ # Stateless HTTP mode is process-global; pin it so tests hold under xdist+ # regardless of what an earlier test on the same worker left behind.+ monkeypatch.setattr("skyvern.cli.core.session_manager.is_stateless_http_mode", lambda: False)+ monkeypatch.setattr(client_module, "settings", config.Settings(_env_file=None))+ return client_module++ def test_cli_process_rejects_credential_on_untouched_default(self, monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.delenv("SKYVERN_BASE_URL", raising=False)+ client_module = self._client_module_with_fresh_settings(monkeypatch)+ monkeypatch.setattr(client_module, "is_cli_runtime", lambda: True)++ with pytest.raises(RuntimeError, match="Set SKYVERN_BASE_URL"):+ client_module._build_cloud_client("test-api-key")++ def test_non_cli_process_builds_client_on_untouched_default(self, monkeypatch: pytest.MonkeyPatch) -> None:+ """Prod temporal workers run with SKYVERN_BASE_URL unset and reach this factory via+ copilot self-heal; an unconditional guard here is a production outage."""+ monkeypatch.delenv("SKYVERN_BASE_URL", raising=False)+ client_module = self._client_module_with_fresh_settings(monkeypatch)+ monkeypatch.setattr(client_module, "is_cli_runtime", lambda: False)+ client_constructor = MagicMock(return_value=object())+ monkeypatch.setattr(client_module, "Skyvern", client_constructor)++ client_module._build_cloud_client("test-api-key")++ assert client_constructor.call_args.kwargs["base_url"] == "https://api.skyvern.com"++ @pytest.mark.parametrize("base_url", ["https://api.skyvern.com", "http://localhost:8000"])+ def test_cli_process_builds_client_when_base_url_is_explicit(+ self, base_url: str, monkeypatch: pytest.MonkeyPatch+ ) -> None:+ monkeypatch.setenv("SKYVERN_BASE_URL", base_url)+ client_module = self._client_module_with_fresh_settings(monkeypatch)+ monkeypatch.setattr(client_module, "is_cli_runtime", lambda: True)+ client_constructor = MagicMock(return_value=object())+ monkeypatch.setattr(client_module, "Skyvern", client_constructor)++ client_module._build_cloud_client("test-api-key")++ assert client_constructor.call_args.kwargs["base_url"] == base_url++ def test_cli_process_builds_client_with_placeholder_key(self, monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.delenv("SKYVERN_BASE_URL", raising=False)+ client_module = self._client_module_with_fresh_settings(monkeypatch)+ monkeypatch.setattr(client_module, "is_cli_runtime", lambda: True)+ client_constructor = MagicMock(return_value=object())+ monkeypatch.setattr(client_module, "Skyvern", client_constructor)++ client_module._build_cloud_client("PLACEHOLDER")++ assert client_constructor.call_args.kwargs["base_url"] == "https://api.skyvern.com"++ def test_stateless_http_mode_targets_self_and_skips_guard(self, monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.delenv("SKYVERN_BASE_URL", raising=False)+ client_module = self._client_module_with_fresh_settings(monkeypatch)+ monkeypatch.setattr(client_module, "is_cli_runtime", lambda: True)+ monkeypatch.setattr("skyvern.cli.core.session_manager.is_stateless_http_mode", lambda: True)+ client_constructor = MagicMock(return_value=object())+ monkeypatch.setattr(client_module, "Skyvern", client_constructor)++ client_module._build_cloud_client("test-api-key")++ assert client_constructor.call_args.kwargs["base_url"] == f"http://127.0.0.1:{client_module.settings.PORT}"+++class TestBrowserGroupBaseUrlGuard:+ def test_browser_session_create_refuses_credential_on_untouched_default(+ self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path+ ) -> None:+ """The browser group callback must mark the CLI runtime so the shared factory guard+ fires for nested subcommands; without it a real key silently reaches production."""+ from skyvern import _cli_bootstrap, config+ from skyvern.cli.commands import browser as browser_cmd+ from skyvern.cli.core import client as client_module++ monkeypatch.delenv("SKYVERN_BASE_URL", raising=False)+ monkeypatch.setattr(_cli_bootstrap, "_CLI_RUNTIME_PREPARED", False)+ monkeypatch.setattr("skyvern.utils.env_paths.load_backend_env_files", lambda intent: tmp_path / ".env")+ monkeypatch.setattr(_cli_bootstrap, "configure_cli_runtime_logging", lambda: None)+ monkeypatch.setattr("skyvern.cli.core.session_manager.is_stateless_http_mode", lambda: False)+ monkeypatch.setattr(client_module, "settings", config.Settings(_env_file=None))+ monkeypatch.setattr(client_module, "_resolve_api_key", lambda: "test-api-key")+ monkeypatch.setattr(client_module, "_global_skyvern_instance", None)+ monkeypatch.setattr(browser_cmd, "capture_cli_tool_call", lambda *_args, **_kwargs: None)++ token = client_module._skyvern_instance.set(None)+ try:+ result = CliRunner().invoke(browser_cmd.browser_app, ["session", "create", "--json"])+ finally:+ client_module._skyvern_instance.reset(token)++ assert result.exit_code == 1, result.output+ parsed = json.loads(result.output)+ assert parsed["ok"] is False+ assert "Set SKYVERN_BASE_URL" in parsed["error"]["message"]+++class TestCliRuntimeFlag:+ def test_prepare_cli_runtime_marks_cli_process(self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:+ from skyvern import _cli_bootstrap++ monkeypatch.setattr(_cli_bootstrap, "_CLI_RUNTIME_PREPARED", False)+ monkeypatch.setattr("skyvern.utils.env_paths.load_backend_env_files", lambda intent: tmp_path / ".env")+ monkeypatch.setattr(_cli_bootstrap, "configure_cli_runtime_logging", lambda: None)++ assert not _cli_bootstrap.is_cli_runtime()+ _cli_bootstrap.prepare_cli_runtime(intent="cloud")+ assert _cli_bootstrap.is_cli_runtime()++ def test_cli_import_defers_settings_construction(self) -> None:+ """Both base-url guards read a settings singleton that must be constructed only after+ prepare_cli_runtime has loaded env files; an eager skyvern.config import breaks that."""+ code = "import sys; import skyvern.cli; raise SystemExit(1 if 'skyvern.config' in sys.modules else 0)"+ completed = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True, timeout=120)+ assert completed.returncode == 0, completed.stderr++class TestCredentialsCommands:def test_add_credential_invalid_type_json_error_includes_action(self, capsys: pytest.CaptureFixture) -> None:from skyvern.cli import credentials as credentials_cmddiff --git a/tests/unit/test_code_block_captcha.py b/tests/unit/test_code_block_captcha.pyindex 695c69589..ea4a84558 100644--- a/tests/unit/test_code_block_captcha.py+++ b/tests/unit/test_code_block_captcha.py@@ -94,6 +94,7 @@ class FakePage:self.continue_button.click = AsyncMock(side_effect=self._continue)self.frames = list(frames or [])self.url = url+ self.evaluated: list[str] = []async def _continue(self) -> None:self.checkbox._count = 0@@ -111,6 +112,9 @@ class FakePage:async def wait_for_timeout(self, _milliseconds: int) -> None:await asyncio.sleep(0)+ async def evaluate(self, expression: str, *_args: object) -> None:+ self.evaluated.append(expression)+@pytest.mark.asyncioasync def test_real_sandbox_solve_captcha_is_fast_noop_without_challenge(monkeypatch: pytest.MonkeyPatch) -> None:@@ -514,7 +518,32 @@ async def test_real_sandbox_solve_captcha_raises_constant_sanitized_error(page,organization_id="org-1",workflow_run_id="wr-1",+ browser_session_id=None,+ )+++async def test_builtin_reports_whether_an_arm_ran(monkeypatch: pytest.MonkeyPatch) -> None:+ """A page with no challenge markers must be distinguishable from a solve, so callers that+ re-perceive after a solve do not re-perceive a page nothing touched."""+ agent_function = type(+ "AgentFunctionStub",+ (),+ {+ "auto_solve_captchas": AsyncMock(return_value=False),+ "solve_recaptcha_token": AsyncMock(return_value=True),+ },+ )()+ monkeypatch.setattr(app, "AGENT_FUNCTION", agent_function)++ assert await block_module._code_block_solve_captcha_builtin(FakePage()) is False+ assert (+ await block_module._code_block_solve_captcha_builtin(+ FakePage(recaptcha=True), organization_id="org-1", browser_session_id="bs-1"+ )+ is True)+ assert agent_function.solve_recaptcha_token.await_args.kwargs["browser_session_id"] == "bs-1"def test_solve_captcha_is_reserved_in_sandbox_namespace() -> None:@@ -552,3 +581,64 @@ async def test_extension_arm_is_bounded_and_falls_through_on_hang(agent_function.auto_solve_captchas.assert_awaited_once()agent_function.solve_recaptcha_token.assert_awaited_once()+++async def test_an_unsolved_anchor_click_closes_the_challenge_it_opened(monkeypatch: pytest.MonkeyPatch) -> None:+ """Clicking the anchor escalates to an image challenge whose overlay covers the page and+ outlives the arm, so a click that won no token has to put the widget back."""+ agent_function = type(+ "AgentFunctionStub",+ (),+ {+ "auto_solve_captchas": AsyncMock(return_value=False),+ "solve_recaptcha_token": AsyncMock(return_value=False),+ },+ )()+ monkeypatch.setattr(app, "AGENT_FUNCTION", agent_function)+ parent_frame = FakePage(token_values=["", ""])+ page = FakePage(+ recaptcha=True,+ frames=[+ FakeFrame(+ url="https://www.google.com/recaptcha/api2/anchor",+ anchor=FakeLocator(count=1),+ parent_frame=parent_frame,+ )+ ],+ )++ with pytest.raises(CodeBlockCaptchaError):+ await block_module._code_block_solve_captcha_builtin(page, organization_id="org-1")++ assert any("grecaptcha" in expression for expression in page.evaluated)+++async def test_an_anchor_click_that_left_a_token_is_not_reset_away(monkeypatch: pytest.MonkeyPatch) -> None:+ """A reset discards whatever response it finds, so it must not run over a token already there."""+ agent_function = type(+ "AgentFunctionStub",+ (),+ {+ "auto_solve_captchas": AsyncMock(return_value=False),+ "solve_recaptcha_token": AsyncMock(return_value=False),+ },+ )()+ monkeypatch.setattr(app, "AGENT_FUNCTION", agent_function)+ parent_frame = FakePage(token_values=["tok", "tok"])+ page = FakePage(+ recaptcha=True,+ frames=[+ FakeFrame(+ url="https://www.google.com/recaptcha/api2/anchor",+ anchor=FakeLocator(count=1),+ parent_frame=parent_frame,+ )+ ],+ )++ with pytest.raises(CodeBlockCaptchaError):+ await block_module._code_block_solve_captcha_builtin(page, organization_id="org-1")++ assert not any("grecaptcha" in expression for expression in page.evaluated)diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.pyindex 7d1d9032b..680fc73dd 100644--- a/tests/unit/test_copilot_agent_helpers.py+++ b/tests/unit/test_copilot_agent_helpers.py@@ -27,15 +27,10 @@ from skyvern.forge.sdk.copilot.agent import (_synthesized_block_offer_prompt,_verified_workflow_or_none,)-from skyvern.forge.sdk.copilot.authoring_parameter_binding import (- AuthoringParameterBindingCandidate,- build_authoring_parameter_binding_directive,-)from skyvern.forge.sdk.copilot.blocker_signal import CopilotToolBlockerSignalfrom skyvern.forge.sdk.copilot.build_phase import BuildPhasefrom skyvern.forge.sdk.copilot.build_test_outcome import (PostRunPagePathFailure,- PostRunPagePathTarget,RecordedBuildTestOutcome,)from skyvern.forge.sdk.copilot.code_block_preflight import SANDBOX_UNRESOLVED_NAME_REASON_CODE@@ -354,6 +349,19 @@ workflow_definition:assert evidence.verified_from_current_browser_state is Trueassert evidence.unverified_block_labels == ["expand_results"]+ def test_partial_verification_renders_as_unverified_for_the_agent(self) -> None:+ partial = WorkflowVerificationEvidence(+ block_verified=["extract_results"],+ unverified_block_labels=["expand_results"],+ live_page_state_verified=True,+ )++ rendered = partial.render_prompt_block()++ assert "full_workflow_verified: false" in rendered+ assert "unverified_block_labels:\n - expand_results" in rendered+ assert WorkflowVerificationEvidence().render_prompt_block() == ""+def test_rewrite_includes_navigation_follow_up_when_category_matches(self) -> None:from skyvern.forge.sdk.copilot.agent import _rewrite_failed_test_response@@ -695,31 +703,6 @@ workflow_definition:assert "verified part of it" in reply_bassert "has not been verified end-to-end" in reply_b- def test_runtime_verification_evidence_prompt_surfaces_state_for_agent(self) -> None:- ctx = _ctx(- workflow_verification_evidence=WorkflowVerificationEvidence(- block_verified=["extract_results"],- live_page_state_verified=True,- test_attempted_but_incomplete=True,- per_tool_budget_on_block=["search_registry"],- verified_from_current_browser_state=True,- current_url_observed_after_workflow_run=True,- current_url_may_encode_runtime_state=True,- unverified_block_labels=["search_registry"],- current_url="https://example.com/lookup",- )- )-- prompt = agent_module._runtime_verification_evidence_prompt(ctx)-- assert "RUNTIME VERIFICATION EVIDENCE" in prompt- assert "full_workflow_verified: false" in prompt- assert "test_attempted_but_incomplete: true" in prompt- assert "current_url_observed_after_workflow_run: true" in prompt- assert "current_url_may_encode_runtime_state: true" in prompt- assert "per_tool_budget_on_block:" in prompt- assert "run only missing block labels" in prompt-def test_unresolved_symbol_repair_context_prompt_is_policy_gated(self, monkeypatch: pytest.MonkeyPatch) -> None:info_calls: list[tuple[str, dict[str, str | list[str]]]] = []@@ -783,31 +766,6 @@ workflow_definition:},) in info_calls- def test_unresolved_symbol_repair_context_prompt_creates_exact_parameter_when_missing(self) -> None:- repair_context = CodeAuthoringRepairContext(- block_label="order_status",- reason_code=SANDBOX_UNRESOLVED_NAME_REASON_CODE,- unresolved_names=["confirmation_number"],- parameter_keys=[],- available_parameter_keys=[],- binding_candidates=["confirmation_number"],- )- ctx = _ctx(- block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,- last_code_authoring_repair_context=repair_context,- )-- prompt = agent_module._code_authoring_repair_context_prompt(ctx)-- assert "available_parameter_keys: (none)" in prompt- assert "binding_candidates: confirmation_number" in prompt- assert (- "confirmation_number -> create workflow string parameter key confirmation_number -> parameter_keys -> "- "bare variable confirmation_number"- ) in prompt- assert "do not hardcode the eval value" in prompt- assert "rerun via update_and_run_blocks" in prompt-def test_missing_output_dependency_prompt_uses_available_outputs_not_workflow_parameters(self) -> None:repair_context = CodeAuthoringRepairContext(block_label="read_resource_table",@@ -837,65 +795,6 @@ workflow_definition:assert "do not create a workflow parameter for missing_output_key" in promptassert "create workflow string parameter key create_resource_output" not in prompt- def test_synthesized_parameter_binding_prompt_uses_exact_key(self) -> None:- repair_context = CodeAuthoringRepairContext(- block_label="order_status",- reason_code="synthesized_parameter_binding_ambiguous",- unresolved_names=["enter_confirmation"],- parameter_keys=["enter_confirmation"],- available_parameter_keys=["confirmation_number"],- binding_candidates=["enter_confirmation", "confirmation_number"],- parameter_binding_directive=build_authoring_parameter_binding_directive(- structural_key="definition-reject",- source_origin="https://example.com",- candidates=[- AuthoringParameterBindingCandidate(- declared_key="confirmation_number",- field_selector="#confirmation",- )- ],- ),- )- ctx = _ctx(- block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,- last_code_authoring_repair_context=repair_context,- )-- prompt = agent_module._code_authoring_repair_context_prompt(ctx)-- assert "reason_code: synthesized_parameter_binding_ambiguous" in prompt- assert "binding_candidates: enter_confirmation, confirmation_number" in prompt- assert "parameter_binding_pairs:" in prompt- assert "confirmation_number -> #confirmation" in prompt- assert "declare and use the exact workflow input key" in prompt- assert "include that exact key in the code block's parameter_keys" in prompt- assert "reference it as a bare Python variable in code" in prompt- assert "do not guess or hardcode the runtime value" in prompt- assert "rerun via update_and_run_blocks" in prompt-- def test_ambiguous_selector_repair_context_prompt_includes_selector_details(self) -> None:- repair_context = CodeAuthoringRepairContext(- block_label="order_status",- reason_code="ambiguous_bare_selector",- selector="button",- source_url="https://example.com/orders",- refiner_selector='button[data-action="status"]',- repair_instruction="Use a unique selector from the same page before saving the code block.",- )- ctx = _ctx(- block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,- last_code_authoring_repair_context=repair_context,- )-- prompt = agent_module._code_authoring_repair_context_prompt(ctx)-- assert "CODE AUTHORING REPAIR CONTEXT" in prompt- assert "reason_code: ambiguous_bare_selector" in prompt- assert "selector: button" in prompt- assert "source_url: https://example.com/orders" in prompt- assert 'refiner_selector: button[data-action="status"]' in prompt- assert "Use a unique selector from the same page" in prompt-def test_ambiguous_selector_repair_context_prompt_includes_same_page_alternatives(self) -> None:repair_context = CodeAuthoringRepairContext(block_label="order_status",@@ -924,43 +823,6 @@ workflow_definition:assert "button:nth-of-type" not in promptassert "secret-token" not in prompt- def test_runtime_repair_context_prompt_includes_failure_and_page_state(self) -> None:- repair_context = CodeAuthoringRepairContext(- block_label="search_registry",- reason_code="runtime_block_failure",- runtime_failure_reason='Timeout waiting for locator("#results")',- runtime_failure_class="timeout_waiting_for_selector",- failed_block_status="failed",- workflow_run_id="wr_failed",- current_origin="https://example.test",- current_url_present=True,- current_title_present=True,- page_evidence_source="inspect_page_for_composition",- observed_after_workflow_run=True,- page_form_summaries=["Search #search"],- page_result_summaries=["No results No matching records"],- page_action_summaries=["Search button.search disabled"],- )- ctx = _ctx(- block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,- last_code_authoring_repair_context=repair_context,- )-- prompt = agent_module._code_authoring_repair_context_prompt(ctx)-- assert 'runtime_failure_reason: Timeout waiting for locator("#results")' in prompt- assert "runtime_failure_class: timeout_waiting_for_selector" in prompt- assert "failed_block_status: failed" in prompt- assert "current_origin: https://example.test" in prompt- assert "current_url_present: true" in prompt- assert "current_title_present: true" in prompt- assert "observed_after_workflow_run: true" in prompt- assert "page_forms: Search #search" in prompt- assert "page_results: No results No matching records" in prompt- assert "page_actions: Search button.search disabled" in prompt- assert "adapt the next code block to the observed page state" in prompt- assert "do not re-emit the same failing selector or name path" in prompt-def test_metadata_repair_context_prompt_includes_failure_and_contract_guidance(self) -> None:long_reason = "missing requested output child paths " + ("x" * 220)repair_context = CodeAuthoringRepairContext(@@ -1008,80 +870,6 @@ workflow_definition:assert "Declare code_artifact_metadata goal_value_paths" in promptassert "Coastal" not in prompt- def test_recorded_build_test_outcome_prompt_renders_structural_grounding(self) -> None:- ctx = _ctx(- block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,- latest_recorded_build_test_outcome=RecordedBuildTestOutcome(- phase="persisted_block_run",- attempted_tool="update_and_run_blocks",- attempted_block_label="search_records",- verdict="repairable_failure",- reason_code="runtime_block_failure",- workflow_run_id="wr_failed",- structural_failure_identity="runtime:timeout_waiting_for_selector:failed",- page_evidence_refs=["form:Search #search", "result:#results rows=unknown"],- observed_evidence_summary="Timeout waiting for results.",- ),- )-- prompt = agent_module._recorded_build_test_outcome_prompt(ctx)-- assert "RECORDED BUILD-TEST OUTCOME" in prompt- assert "phase: persisted_block_run" in prompt- assert "reason_code: runtime_block_failure" in prompt- assert "page_evidence_refs: form:Search #search, result:#results rows=unknown" in prompt- assert "change the next authored step, selector, extraction, or binding based on" in prompt-- def test_recorded_build_test_outcome_prompt_renders_exact_post_run_page_path_actions(self) -> None:- ctx = _ctx(- block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,- latest_recorded_build_test_outcome=RecordedBuildTestOutcome(- phase="persisted_block_run",- attempted_tool="update_and_run_blocks",- verdict="repairable_failure",- reason_code="outcome_not_demonstrated",- workflow_run_id="wr_failed",- structural_failure_identity="completion:page-path",- page_path_failure=PostRunPagePathFailure(- kind="challenge",- workflow_run_id="wr_failed",- current_url="https://example.test/challenge",- continuation_targets=(- PostRunPagePathTarget(kind="challenge", selector="#notRobot"),- PostRunPagePathTarget(kind="challenge", selector="#continue"),- ),- enter_allowed=True,- ),- ),- )-- prompt = agent_module._recorded_build_test_outcome_prompt(ctx)-- assert "POST-RUN PAGE-PATH CONTINUATION:" in prompt- assert "kind: challenge" in prompt- assert "- allowed click: selector=#notRobot" in prompt- assert "- allowed Enter: selector=#continue" in prompt- assert "Do not navigate away or re-author the workflow" in prompt-- def test_recorded_build_test_outcome_prompt_requires_fresh_inspection_when_page_path_is_unbound(self) -> None:- ctx = _ctx(- block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,- latest_recorded_build_test_outcome=RecordedBuildTestOutcome(- phase="persisted_block_run",- attempted_tool="update_and_run_blocks",- verdict="repairable_failure",- reason_code="outcome_not_demonstrated",- workflow_run_id="wr_failed",- structural_failure_identity="completion:page-path",- ),- )-- prompt = agent_module._recorded_build_test_outcome_prompt(ctx)-- assert "POST-RUN PAGE-PATH CONTRACT UNBOUND:" in prompt- assert 'inspect_page_for_composition with target_url="current_page"' in prompt- assert "Do not use evaluate as a substitute" in prompt-def test_recorded_build_test_outcome_prompt_does_not_offer_page_actions_for_non_page_outcome(self) -> None:ctx = _ctx(block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,@@ -1106,33 +894,6 @@ workflow_definition:assert "POST-RUN PAGE-PATH CONTINUATION:" not in promptassert "POST-RUN PAGE-PATH CONTRACT UNBOUND:" not in prompt- def test_recorded_build_test_outcome_prompt_renders_exact_missing_output_paths(self) -> None:- ctx = _ctx(- block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,- latest_recorded_build_test_outcome=RecordedBuildTestOutcome(- phase="persisted_block_run",- attempted_tool="update_and_run_blocks",- verdict="repairable_failure",- reason_code="outcome_not_demonstrated",- structural_failure_identity="completion:unsatisfied-output",- missing_requested_output_facts=[- {- "output_path": "output.confirmation_number",- "output_root": "output",- "value_status": "no_typed_value",- "reason_code": "no_evidence",- }- ],- ),- )-- prompt = agent_module._recorded_build_test_outcome_prompt(ctx)-- assert "output_path=output.confirmation_number" in prompt- assert "output_root=output" in prompt- assert "Use the exact output_path values in goal_value_paths and returned output" in prompt- assert "output_root is diagnostic grouping only" in prompt-def test_recorded_build_test_outcome_prompt_surfaces_observed_page_values(self) -> None:long_value = "Request WTR-1842-DEMO for account 100245 confirmed. " + "detail " * 40ctx = _ctx(@@ -1156,30 +917,6 @@ workflow_definition:assert "100245" in value_lineassert len(value_line) > 200- def test_recorded_build_test_outcome_prompt_binds_observed_values_on_metadata_reject(self) -> None:- ctx = _ctx(- block_authoring_policy=BlockAuthoringPolicy.CODE_ONLY_BROWSER,- latest_recorded_build_test_outcome=RecordedBuildTestOutcome(- phase="author_time_reject",- attempted_tool="update_workflow",- verdict="authoring_rejected",- reason_code="metadata_reject",- structural_failure_identity="author_time:metadata_reject",- observed_page_value_excerpt="Request WTR-1842-DEMO for account 100245 confirmed.",- missing_requested_output_facts=[- {"output_root": "output", "output_path": "output.confirmation_number"},- {"output_root": "output", "output_path": "output.account_number"},- ],- ),- )-- prompt = agent_module._recorded_build_test_outcome_prompt(ctx)-- assert "OBSERVED PAGE VALUES CONTRACT" in prompt- assert "observed_values: Request WTR-1842-DEMO for account 100245 confirmed." in prompt- assert "- output.confirmation_number: <observed value>" in prompt- assert "- output.account_number: <observed value>" in prompt-class TestVerifiedWorkflowOrNone:"""SKY-9143 strict invariant: a proposal surfaces only after a passing test this turn."""@@ -2098,6 +1835,7 @@ class TestRequestPolicyInputGuardrail:organization_id="org-1",handler=policy_inputs.request_policy_handler,config=None,+ prior_user_messages=policy_inputs.prior_user_messages,)@pytest.mark.asyncio@@ -4231,7 +3969,7 @@ class TestRequestPolicyCredentialResolution:assert policy.completion_contract_status == "present"@pytest.mark.asyncio- async def test_missing_user_supplied_credential_id_rides_along_when_another_resolves(self, monkeypatch) -> None:+ async def test_missing_user_supplied_credential_ids_ask_for_clarification(self, monkeypatch) -> None:from skyvern.forge.sdk.copilot import request_policy as policy_modulefrom skyvern.forge.sdk.copilot.request_policy import build_request_policy@@ -4254,12 +3992,14 @@ class TestRequestPolicyCredentialResolution:assert policy.credential_input_kind == "credential_id"assert policy.credential_refs == ["cred_valid", "cred_missing"]assert policy.invalid_credential_ids == ["cred_missing"]- # A stated ID that resolved carries the turn; the unresolvable one rides along as a- # recorded finding instead of walling the request (SKY-13552).- assert [credential.credential_id for credential in policy.resolved_credentials] == ["cred_valid"]- assert policy.user_response_policy != "ask_clarification"- assert policy.allow_update_workflow is True- assert policy.allow_run_blocks is True+ assert policy.user_response_policy == "ask_clarification"+ assert policy.allow_update_workflow is False+ assert policy.allow_run_blocks is False+ assert policy.allow_missing_credentials_in_draft is False+ assert policy.clarification_question+ assert "cred_missing" in policy.clarification_question+ assert "not found in this organization" in policy.clarification_question+ assert "unvalidated draft" in policy.clarification_questionget_credentials_by_ids.assert_awaited_once_with(["cred_valid", "cred_missing"], organization_id="org-1")@pytest.mark.asyncio@@ -5895,24 +5635,6 @@ class TestResponseTypeClassificationRuleReachesAgent:assert prompt.index("RESPONSE-TYPE CLASSIFICATION") < prompt.index("**Option 1: Reply to the user**")-class TestValidationClassificationOutputContractRule:- def test_build_system_prompt_requires_exact_top_level_classification_contract_keys(self) -> None:- from skyvern.forge.sdk.copilot.agent import _build_system_prompt-- prompt = _build_system_prompt(tool_usage_guide="", security_rules="")-- assert "validation_classification_output_contracts" in prompt- assert "explicitly `return` a dict" in prompt- assert "Do not rely on schema defaults or exposed top-level locals" in prompt- assert "exact `return_key` as a top-level key" in prompt- assert "goal_value_paths" in prompt- assert "extraction_schema" in prompt- assert "reject code-only output blocks without that metadata" in prompt- assert "observed_gate_phrase" in prompt- assert "Synonyms such as `path_login_only`, `classification`, or prose in `evidence_text`" in prompt- assert "do not replace the required key" in prompt--class TestCopilotConfig:def test_system_prompt_uses_custom_security_rules(self) -> None:prompt = agent_module._build_system_prompt(@@ -6206,38 +5928,6 @@ class TestRequestPolicyTranscriptContext:class TestRequestPolicyPromptRendering:- @pytest.mark.asyncio- async def test_classifier_prompt_includes_all_structural_slots(self) -> None:- captured: dict[str, str] = {}-- async def handler(prompt: str, prompt_name: str) -> dict[str, str]:- captured["prompt"] = prompt- captured["prompt_name"] = prompt_name- return {"credential_input_kind": "none", "testing_intent": "unspecified"}-- await _classify_request(- user_message="azure_credentials",- workflow_yaml="title: Test",- chat_history=_history(- ("user", "build a login workflow"),- ("ai", "drafted v1"),- ("user", "use my saved creds"),- ("ai", "Which saved credential should I use?"),- ),- global_llm_context="",- handler=_with_empty_request_slots(handler),- )-- prompt = captured["prompt"]- assert "Earliest retained user turn" in prompt- assert "Latest prior user turn" in prompt- assert "Latest assistant turn" in prompt- assert "Retained recent history" in prompt- assert "build a login workflow" in prompt- assert "use my saved creds" in prompt- assert "Which saved credential should I use?" in prompt- assert "drafted v1" in prompt-@pytest.mark.asyncioasync def test_classifier_prompt_renders_sentinel_for_empty_slots(self) -> None:captured: dict[str, str] = {}diff --git a/tests/unit/test_copilot_agent_naming.py b/tests/unit/test_copilot_agent_naming.pynew file mode 100644index 000000000..b298977ce--- /dev/null+++ b/tests/unit/test_copilot_agent_naming.py@@ -0,0 +1,210 @@+from __future__ import annotations++from datetime import datetime, timezone+from unittest.mock import AsyncMock, MagicMock++import pytest+import yaml++from skyvern.forge.sdk.copilot.agent_naming import derive_agent_title, maybe_name_agent+from skyvern.forge.sdk.copilot.request_policy import (+ RequestPolicy,+)+from skyvern.forge.sdk.copilot.turn_intent import (+ TurnIntent,+ TurnIntentClassification,+ TurnIntentClassifierResult,+ TurnIntentMode,+)+from skyvern.forge.sdk.copilot.workflow_yaml import with_workflow_yaml_title, workflow_yaml_title+from skyvern.forge.sdk.schemas.workflow_copilot import (+ WorkflowCopilotChatHistoryMessage,+ WorkflowCopilotChatSender,+)++_TS = datetime(2026, 1, 1, tzinfo=timezone.utc)++NAMED_YAML = "title: Quarterly AP\nworkflow_definition:\n blocks: []\n"+DEFAULT_YAML = "title: New Agent\nworkflow_definition:\n blocks: []\n"+++def _history(*messages: tuple[str, str]) -> list[WorkflowCopilotChatHistoryMessage]:+ return [+ WorkflowCopilotChatHistoryMessage(+ sender=WorkflowCopilotChatSender.USER if sender == "user" else WorkflowCopilotChatSender.AI,+ content=content,+ created_at=_TS,+ )+ for sender, content in messages+ ]+++def _classifier_result(title: str | None) -> TurnIntentClassifierResult:+ return TurnIntentClassifierResult.success(TurnIntentClassification(mode=TurnIntentMode.BUILD, workflow_title=title))+++def _ctx(+ mode: TurnIntentMode = TurnIntentMode.BUILD,+ workflow_yaml: str = DEFAULT_YAML,+ raw_secret_detected: bool = False,+) -> MagicMock:+ ctx = MagicMock()+ ctx.turn_intent = TurnIntent(mode=mode)+ ctx.request_policy = RequestPolicy(raw_secret_detected=raw_secret_detected)+ ctx.workflow_yaml = workflow_yaml+ ctx.workflow_id = "w_1"+ ctx.workflow_permanent_id = "wpid_1"+ ctx.organization_id = "o_1"+ return ctx+++def test_derive_uses_the_classifier_candidate() -> None:+ title = derive_agent_title(_classifier_result("Invoice Bot"))++ assert title == "Invoice Bot"+++async def test_naming_skips_non_authoring_modes(monkeypatch: pytest.MonkeyPatch) -> None:+ rename = AsyncMock(return_value=True)+ monkeypatch.setattr("skyvern.forge.app.DATABASE.workflows.rename_workflow_if_still_default", rename)++ assert (+ await maybe_name_agent(_ctx(mode=TurnIntentMode.DIAGNOSE), classifier_result=_classifier_result("Invoice Bot"))+ is None+ )+ rename.assert_not_awaited()++[email protected]("mode", [TurnIntentMode.BUILD, TurnIntentMode.EDIT, TurnIntentMode.DRAFT_ONLY])+async def test_naming_runs_for_every_authoring_mode(mode: TurnIntentMode, monkeypatch: pytest.MonkeyPatch) -> None:+ rename = AsyncMock(return_value=True)+ monkeypatch.setattr("skyvern.forge.app.DATABASE.workflows.rename_workflow_if_still_default", rename)++ title = await maybe_name_agent(_ctx(mode=mode), classifier_result=_classifier_result("Invoice Bot"))++ assert title == "Invoice Bot"+ assert rename.await_args.kwargs["title"] == "Invoice Bot"+++async def test_naming_yields_to_an_unsaved_editor_rename(monkeypatch: pytest.MonkeyPatch) -> None:+ """Canonical can still read default while the user has typed a name they have not saved."""+ rename = AsyncMock(return_value=True)+ monkeypatch.setattr("skyvern.forge.app.DATABASE.workflows.rename_workflow_if_still_default", rename)++ assert (+ await maybe_name_agent(_ctx(workflow_yaml=NAMED_YAML), classifier_result=_classifier_result("Invoice Bot"))+ is None+ )+ rename.assert_not_awaited()+++async def test_naming_reports_nothing_when_the_compare_and_set_loses(monkeypatch: pytest.MonkeyPatch) -> None:+ """A lost race must not emit a title the database does not hold."""+ monkeypatch.setattr(+ "skyvern.forge.app.DATABASE.workflows.rename_workflow_if_still_default",+ AsyncMock(return_value=False),+ )++ assert await maybe_name_agent(_ctx(), classifier_result=_classifier_result("Invoice Bot")) is None+++async def test_naming_fails_open_when_the_write_raises(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(+ "skyvern.forge.app.DATABASE.workflows.rename_workflow_if_still_default",+ AsyncMock(side_effect=RuntimeError("db down")),+ )++ assert await maybe_name_agent(_ctx(), classifier_result=_classifier_result("Invoice Bot")) is None+++def test_with_workflow_yaml_title_replaces_in_place_and_preserves_the_rest() -> None:+ original = 'title: New Agent\nworkflow_definition:\n blocks:\n - code: |\n x = " keep me "\n'++ updated = with_workflow_yaml_title(original, "Invoice Bot")++ assert workflow_yaml_title(updated) == "Invoice Bot"+ assert 'x = " keep me "' in updated+ assert "New Agent" not in updated+++def test_with_workflow_yaml_title_adds_a_title_when_absent() -> None:+ updated = with_workflow_yaml_title("workflow_definition:\n blocks: []\n", "Invoice Bot")++ assert workflow_yaml_title(updated) == "Invoice Bot"+ assert "workflow_definition:" in updated+++async def test_naming_withholds_when_the_policy_layer_flagged_a_raw_secret(monkeypatch: pytest.MonkeyPatch) -> None:+ rename = AsyncMock(return_value=True)+ monkeypatch.setattr("skyvern.forge.app.DATABASE.workflows.rename_workflow_if_still_default", rename)++ assert (+ await maybe_name_agent(_ctx(raw_secret_detected=True), classifier_result=_classifier_result("Invoice Bot"))+ is None+ )+ rename.assert_not_awaited()+++def test_with_workflow_yaml_title_replaces_a_block_scalar_title_whole() -> None:+ """The old value's continuation lines must go with it, or they fold into the new title."""+ original = "title: >-\n A wrapped\n name\nworkflow_definition:\n blocks: []\n"++ updated = with_workflow_yaml_title(original, "Invoice Bot")++ assert workflow_yaml_title(updated) == "Invoice Bot"+ assert "A wrapped" not in updated+++def test_with_workflow_yaml_title_does_not_compound_a_folded_title() -> None:+ """PyYAML folds a long title at width=80; a partial replacement grows it every turn."""+ long_title = "Download all invoices from the vendor portal and upload them to the accounting sheet"+ document = yaml.safe_dump({"title": long_title, "workflow_definition": {"blocks": []}}, sort_keys=False)++ for _ in range(3):+ document = with_workflow_yaml_title(document, workflow_yaml_title(document) or long_title)+ document = yaml.safe_dump(yaml.safe_load(document), sort_keys=False)++ assert workflow_yaml_title(document) == long_title+++def test_with_workflow_yaml_title_matches_a_quoted_root_key() -> None:+ updated = with_workflow_yaml_title('"title": Old Name\nworkflow_definition:\n blocks: []\n', "Invoice Bot")++ assert workflow_yaml_title(updated) == "Invoice Bot"+++def test_with_workflow_yaml_title_inserts_after_a_document_marker() -> None:+ """Prepending ahead of --- would make two documents, which safe_load rejects."""+ updated = with_workflow_yaml_title("---\nworkflow_definition:\n blocks: []\n", "Invoice Bot")++ assert workflow_yaml_title(updated) == "Invoice Bot"+++def test_derive_never_falls_back_to_user_text() -> None:+ """The only filter available here is the pattern scrub the policy layer already ran."""+ assert derive_agent_title(_classifier_result(None)) is None+ assert derive_agent_title(None) is None+++ ("raw_secret_detected", "raw_secret_handling"),+ [(True, "none"), (False, "redacted_draft"), (False, "block")],+)+async def test_naming_withholds_on_either_credential_verdict(+ raw_secret_detected: bool, raw_secret_handling: str, monkeypatch: pytest.MonkeyPatch+) -> None:+ """raw_secret_detected is the pattern verdict; raw_secret_handling is the classifier's."""+ rename = AsyncMock(return_value=True)+ monkeypatch.setattr("skyvern.forge.app.DATABASE.workflows.rename_workflow_if_still_default", rename)+ ctx = _ctx()+ ctx.request_policy = RequestPolicy(raw_secret_detected=raw_secret_detected, raw_secret_handling=raw_secret_handling)++ assert await maybe_name_agent(ctx, classifier_result=_classifier_result("Invoice Bot")) is None+ rename.assert_not_awaited()diff --git a/tests/unit/test_copilot_agent_prompt_chat_history_resolution.py b/tests/unit/test_copilot_agent_prompt_chat_history_resolution.pyindex 1985c50d8..f2306b405 100644--- a/tests/unit/test_copilot_agent_prompt_chat_history_resolution.py+++ b/tests/unit/test_copilot_agent_prompt_chat_history_resolution.py@@ -26,58 +26,6 @@ def test_prompt_consolidates_ask_vs_edit_routing_rule() -> None:assert "Explicit edit/debug requests remain edit requests" not in rendered-def test_prompt_routes_resolvable_builds_and_diagnostic_followups_to_edit() -> None:- rendered = _render_agent_prompt()-- assert "If the latest turn OR prior context establishes a build/edit goal" in rendered- assert "needed target, condition, value, or action is resolvable" in rendered- assert "edit the workflow and call `update_and_run_blocks`" in rendered- assert "This includes diagnostic symptom follow-ups on the same workflow after an explicit edit goal" in rendered- assert "When the workflow structure itself is enough to make the edit" in rendered- assert (- "do not use direct browser tools, `get_run_results`, `run_blocks_and_collect_debug`, `update_workflow`"- in rendered- )- assert (- "Do not copy explanatory chat-history prose, example Jinja placeholders, or diagnostic transcripts" in rendered- )- assert (- "diagnostic-after-edit - prior turn asked to consolidate login blocks and latest says "- "`navigate_to_SSO` worked but `block_placeholder` has no active browser session -> connect the existing block chain"- in rendered- )---def test_prompt_keeps_literal_positions_and_targets_ambiguous_references() -> None:- rendered = _render_agent_prompt()-- assert "mismatched/anonymized block references cannot be resolved" in rendered- assert "required target, condition, value, or action is still missing" in rendered- assert "Explicit user positions win literally" in rendered- assert "match existing labels and `block_type` first, then fall back to ordinal" in rendered- assert "latest says to add a conditional to a URL without saying when" in rendered- assert (- "latest names `WF_trigger_SSO_login` and `update_card` when the workflow only has "- "`navigate_to_SSO` and `block_placeholder` with no unique mapping" in rendered- )- assert "ask naming both missing labels and candidate blocks" in rendered---def test_classification_skips_ask_question_when_context_resolvable() -> None:- rendered = _render_agent_prompt()-- assert "not plainly resolvable from chat_history, the current workflow_yaml, or global_llm_context" in rendered- assert "and not inferable from chat_history or the current workflow_yaml" in rendered---def test_carve_out_section_headers_present_in_prompt() -> None:- rendered = _render_agent_prompt()-- assert "ASK-vs-EDIT ROUTING:" in rendered- assert "CREDENTIAL HANDLING - CRITICAL:" in rendered- assert "PARAMETERIZED REQUESTS WITHOUT A SAMPLE VALUE:" in rendered--def test_workflow_credential_inputs_unbound_branch_teaches_reply_framing() -> None:rendered = _render_agent_prompt()@@ -87,12 +35,3 @@ def test_workflow_credential_inputs_unbound_branch_teaches_reply_framing() -> Noassert "I applied your requested change. I couldn't test the modified workflow" in renderedassert "add them via the Credentials UI" in renderedassert 'data.skip_reason="workflow_credential_inputs_unbound"' in rendered---def test_prompt_treats_conditional_zero_results_as_terminal_evidence() -> None:- rendered = _render_agent_prompt()-- assert 'The user requested result handling conditionally (for example: "if any"' in rendered- assert "Treat the no-results path as a valid terminal workflow state" in rendered- assert "do not ask for a corrected value" in rendered- assert "Set `goal_reached: true` only if the workflow reached the searched/filtered state" in rendereddiff --git a/tests/unit/test_copilot_author_time_block.py b/tests/unit/test_copilot_author_time_block.pyindex 0fd7cb656..c43e4371a 100644--- a/tests/unit/test_copilot_author_time_block.py+++ b/tests/unit/test_copilot_author_time_block.py@@ -401,7 +401,6 @@ def test_every_loop_plane_refusal_it_can_emit_is_declared() -> None:constants themselves, and fails if a rename moves a value out of the declared set."""carried = {- blocker_signal_module.SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE,blocker_signal_module.DISCOVERY_EXHAUSTED_NO_ENTRY_URL_REASON_CODE,run_outcome_module.TERMINAL_CHALLENGE_BLOCKER_REASON_CODE,blockers_module._POST_BUDGET_CHALLENGE_RESULT_EVIDENCE_REASON,diff --git a/tests/unit/test_copilot_blocker_signal.py b/tests/unit/test_copilot_blocker_signal.pyindex 69295849a..8ac29b012 100644--- a/tests/unit/test_copilot_blocker_signal.py+++ b/tests/unit/test_copilot_blocker_signal.py@@ -6,7 +6,6 @@ import pytestfrom skyvern.forge.sdk.copilot.blocker_signal import (_LEAK_DENY_TOKENS,- SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE,BlockerKind,CopilotToolBlockerSignal,assert_clean_user_facing_text,@@ -342,7 +341,7 @@ def test_stash_non_final_signal_does_not_replace_a_held_final_reply_blocker() ->ctx = _Ctx()existing = _make(kind="tool_error",- internal_reason_code=SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE,+ internal_reason_code="tool_error_post_budget_upstream_replay",renders_final_reply=True,)incoming = _make(diff --git a/tests/unit/test_copilot_credential_pause.py b/tests/unit/test_copilot_credential_pause.pyindex 12098308c..1eb01c446 100644--- a/tests/unit/test_copilot_credential_pause.py+++ b/tests/unit/test_copilot_credential_pause.py@@ -67,8 +67,8 @@ from skyvern.forge.sdk.copilot.diagnosis_repair_contract import ()from skyvern.forge.sdk.copilot.enforcement import (NUDGE_SENTINEL,- _check_enforcement,_elapsed_run_seconds,+ enforcement_decision,run_with_enforcement,)from skyvern.forge.sdk.copilot.request_policy import (@@ -437,7 +437,7 @@ async def test_skip_clears_stale_last_test_ok_from_the_diagnosed_failure(monkeypassert resume_msgs is not Noneassert ctx.last_test_ok is None- assert _check_enforcement(ctx, result=None, config=config) is None+ assert enforcement_decision(ctx, result=None, config=config) is None@pytest.mark.asyncio@@ -935,7 +935,7 @@ async def test_credential_pause_preempts_a_concurrent_synthesized_offer(monkeypacalls: list[dict[str, Any]] = []def fake_synthesized_offer(_ctx: Any) -> dict[str, Any] | None:- # _check_enforcement runs after the Runner call, so `calls` already has+ # enforcement_decision runs after the Runner call, so `calls` already has# 1 entry on the pre-pause finalize check; gone by the post-resume check# (2 entries), so the test isolates the preemption itself.return {"role": "user", "content": "offer to add the reopened block"} if len(calls) <= 1 else None@@ -1343,7 +1343,7 @@ async def test_resolve_credential_pause_rejections_are_typed() -> None:def _failed_test_ctx() -> CopilotContext:"""A real CopilotContext (all enforcement fields defaulted) with just the- failed-test + missing-credential-diagnosis fields set, so _check_enforcement+ failed-test + missing-credential-diagnosis fields set, so enforcement_decisiondoesn't AttributeError on some unrelated field this hand-picked set omits."""ctx = make_copilot_context()@@ -1362,7 +1362,7 @@ def test_missing_credential_failure_suppresses_generic_failed_test_nudge_when_pactx = _failed_test_ctx()config = CopilotConfig(credential_pause_enabled=True)- nudge = _check_enforcement(ctx, result=None, config=config)+ nudge = enforcement_decision(ctx, result=None, config=config)assert nudge is Noneassert ctx.failed_test_nudge_count == 0@@ -1373,7 +1373,7 @@ def test_missing_credential_failure_still_nudges_when_kill_switch_off() -> None:ctx = _failed_test_ctx()config = CopilotConfig(credential_pause_enabled=False)- nudge = _check_enforcement(ctx, result=None, config=config)+ nudge = enforcement_decision(ctx, result=None, config=config)assert nudge is not Noneassert ctx.failed_test_nudge_count == 1@@ -1384,7 +1384,7 @@ def test_missing_credential_failure_nudges_normally_once_pause_already_used() ->ctx.credential_pause_used = Trueconfig = CopilotConfig(credential_pause_enabled=True)- nudge = _check_enforcement(ctx, result=None, config=config)+ nudge = enforcement_decision(ctx, result=None, config=config)assert nudge is not Noneassert ctx.failed_test_nudge_count == 1@@ -1413,7 +1413,7 @@ def test_skipped_run_suppresses_post_update_nudge_when_pause_enabled() -> None:ctx = _skipped_run_ctx()config = CopilotConfig(credential_pause_enabled=True)- nudge = _check_enforcement(ctx, result=None, config=config)+ nudge = enforcement_decision(ctx, result=None, config=config)assert nudge is None@@ -1422,10 +1422,10 @@ def test_skipped_run_still_nudges_post_update_when_kill_switch_off() -> None:ctx = _skipped_run_ctx()config = CopilotConfig(credential_pause_enabled=False)- nudge = _check_enforcement(ctx, result=None, config=config)+ nudge = enforcement_decision(ctx, result=None, config=config)assert nudge is not None- assert "updated the workflow but did not test it" in nudge+ assert "updated the workflow but did not test it" in nudge.messagedef test_workflow_credential_inputs_unbound_skip_does_not_nudge_post_update() -> None:@@ -1443,10 +1443,10 @@ def test_workflow_credential_inputs_unbound_skip_does_not_nudge_post_update() ->ctx.request_policy = RequestPolicy()config = CopilotConfig(credential_pause_enabled=True)- assert _check_enforcement(ctx, result=None, config=config) is None+ assert enforcement_decision(ctx, result=None, config=config) is Nonectx.credential_pause_used = True- assert _check_enforcement(ctx, result=None, config=config) is None+ assert enforcement_decision(ctx, result=None, config=config) is None@pytest.mark.asyncio@@ -1952,6 +1952,27 @@ def test_connected_resume_never_binds_an_origin_from_prose() -> None:assert policy.live_page_admitted_urls == {}+def test_connected_resume_names_the_picked_credential_superseding_an_earlier_mention() -> None:+ """The fill seam's which-credential check is set equality, so a card answer must replace an+ earlier prose mention, not join it."""+ policy = _card_answerable_policy()+ policy.current_turn_named_credential_ids = {"cred_earlier"}++ credential_pause_module._apply_connected_credential_to_policy(make_copilot_context(), policy, _make_credential())++ assert policy.current_turn_named_credential_ids == {"cred_1"}+++def test_connected_resume_does_not_duplicate_an_already_resolved_credential() -> None:+ policy = _card_answerable_policy()+ credential = _make_credential()+ policy.resolved_credentials = [credential]++ credential_pause_module._apply_connected_credential_to_policy(make_copilot_context(), policy, credential)++ assert [resolved.credential_id for resolved in policy.resolved_credentials] == ["cred_1"]++def test_a_card_connected_credential_keeps_its_durable_cross_turn_approval() -> None:ctx = make_copilot_context()policy = _card_answerable_policy()diff --git a/tests/unit/test_copilot_discovery.py b/tests/unit/test_copilot_discovery.pyindex 77ccbc678..0daa198d9 100644--- a/tests/unit/test_copilot_discovery.py+++ b/tests/unit/test_copilot_discovery.py@@ -2,12 +2,14 @@from __future__ import annotations+from types import SimpleNamespacefrom typing import Anyimport pytestfrom skyvern.forge.agent_functions import CopilotEntrypointCandidate, CopilotSiteOriginAssociationfrom skyvern.forge.sdk.copilot import tools as tools_module+from skyvern.forge.sdk.copilot.request_policy import RequestPolicy, _ground_user_provided_sitesfrom skyvern.forge.sdk.copilot.runtime import PendingBrowserInteractionObservationfrom skyvern.forge.sdk.copilot.tools import (_discovery_walk,@@ -15,7 +17,7 @@ from skyvern.forge.sdk.copilot.tools import (_rank_discovery_entrypoint_candidates,_resolve_discovery_entry_url,)-from skyvern.forge.sdk.copilot.tools.discovery import _discovery_build_result+from skyvern.forge.sdk.copilot.tools.discovery import _discovery_build_result, _user_provided_entry_urlfrom skyvern.forge.sdk.copilot.turn_origin import TurnOriginfrom skyvern.forge.sdk.copilot.verification_evidence import WorkflowVerificationEvidence@@ -790,3 +792,27 @@ async def test_capture_composition_evidence_warns_when_html_sliced_at_cap() -> Nassert error is Noneassert evidence is not Noneassert "html_sliced_at_cap" in evidence["inspection_warnings"]+++class TestUserProvidedEntryUrl:+ """Name lookup asks the world; this asks the conversation, so a URL the user already pasted is+ not answered with a request for a URL."""++ @staticmethod+ def _ctx(user_message: str) -> SimpleNamespace:+ policy = RequestPolicy()+ _ground_user_provided_sites(policy, user_message, [])+ return SimpleNamespace(request_policy=policy)++ def test_the_only_site_the_user_gave_is_opened(self) -> None:+ ctx = self._ctx("go to https://us.example.com/reports and pull the numbers")++ assert _user_provided_entry_url(ctx) == "https://us.example.com/reports"++ def test_several_sites_resolve_nothing(self) -> None:+ ctx = self._ctx("check https://a.example.com and https://b.example.net")++ assert _user_provided_entry_url(ctx) is None++ def test_nothing_the_user_provided_resolves_nothing(self) -> None:+ assert _user_provided_entry_url(self._ctx("no addresses here")) is Nonediff --git a/tests/unit/test_copilot_enforcement_hardening.py b/tests/unit/test_copilot_enforcement_hardening.pyindex 8c058ebd9..c0db84053 100644--- a/tests/unit/test_copilot_enforcement_hardening.py+++ b/tests/unit/test_copilot_enforcement_hardening.py@@ -1,10 +1,10 @@"""Tests for enforcement hardening landed in copilot-stack/06b:-* fresh ``CopilotContext`` flows through ``_check_enforcement`` without raising+* fresh ``CopilotContext`` flows through ``enforcement_decision`` without raisingAttributeError (enforcement fields have dataclass defaults).* ``_prune_input_list`` compacts the ``arguments`` field of older tool callsso large payloads (like a full workflow YAML) don't accumulate.-* ``_check_enforcement`` does NOT clear ``last_test_suspicious_success`` after+* ``enforcement_decision`` does NOT clear ``last_test_suspicious_success`` afteremitting the nudge — if the agent ignores it and replies again, the nudgemust re-fire.* ``_recover_from_context_overflow`` strips image payloads out of the current@@ -38,14 +38,7 @@ from skyvern.forge.sdk.copilot.diagnosis_repair_contract import (VerificationResult,)from skyvern.forge.sdk.copilot.enforcement import (- POST_FAILED_TEST_INSPECT_FIRST_NUDGE,- POST_FAILED_TEST_NUDGE,- POST_NAVIGATE_NUDGE,- POST_PER_TOOL_BUDGET_NUDGE,- POST_PER_TOOL_BUDGET_STOP_NUDGE,- POST_SUSPICIOUS_SUCCESS_NUDGE,SCREENSHOT_PLACEHOLDER,- _check_enforcement,_code_authoring_reject_count_resets,_is_context_window_error,_needs_inspect_before_repair_nudge,@@ -54,6 +47,7 @@ from skyvern.forge.sdk.copilot.enforcement import (_scouted_spine_missing_text,_strip_input_images,_witnessed_values_by_path,+ enforcement_decision,register_no_progress_interaction_click,reset_no_progress_interaction_count,synthesized_trajectory_reaches_goal,@@ -69,9 +63,9 @@ from tests.unit.conftest import make_copilot_context as _fresh_context# ----------------------------------------------------------------------------def test_check_enforcement_on_fresh_agent_context_returns_none() -> None:+def test_enforcement_decision_on_fresh_agent_context_returns_none() -> None:ctx = _fresh_context()- assert _check_enforcement(ctx) is None+ assert enforcement_decision(ctx) is Nonedef test_failed_test_nudge_counter_increments_on_fresh_context() -> None:@@ -82,7 +76,7 @@ def test_failed_test_nudge_counter_increments_on_fresh_context() -> None:ctx.last_test_ok = Falsectx.last_test_failure_reason = "something broke"# First call should emit and increment without AttributeError.- assert _check_enforcement(ctx) is not None+ assert enforcement_decision(ctx) is not Noneassert ctx.failed_test_nudge_count == 1@@ -126,7 +120,7 @@ def test_failed_test_routes_to_inspect_first_when_repairable_and_unobserved() ->ctx.test_after_update_done = Truectx.last_test_ok = Falsectx.latest_diagnosis_repair_contract = _repair_contract(RepairNextAction.REPAIR)- assert _check_enforcement(ctx) == POST_FAILED_TEST_INSPECT_FIRST_NUDGE+ assert enforcement_decision(ctx).rule == "post_failed_test_inspect_first"def test_second_consecutive_per_tool_budget_trip_routes_to_stop_nudge() -> None:@@ -137,11 +131,11 @@ def test_second_consecutive_per_tool_budget_trip_routes_to_stop_nudge() -> None:ctx.last_test_ok = Falsectx.last_failure_category_top = PER_TOOL_BUDGET_FAILURE_CATEGORY# First budget trip earns one smaller-frontier retry nudge.- assert _check_enforcement(ctx) == POST_PER_TOOL_BUDGET_NUDGE+ assert enforcement_decision(ctx).rule == "post_per_tool_budget"assert ctx.per_tool_budget_nudge_count == 1# Second consecutive budget trip -> finalize/STOP nudge, not another re-run.ctx.last_failure_category_top = PER_TOOL_BUDGET_FAILURE_CATEGORY- assert _check_enforcement(ctx) == POST_PER_TOOL_BUDGET_STOP_NUDGE+ assert enforcement_decision(ctx).rule == "post_per_tool_budget_stop"assert ctx.per_tool_budget_nudge_count == 2@@ -157,7 +151,7 @@ def test_failed_test_is_generic_once_reached_page_observed() -> None:ctx.post_run_page_observation_tool = "inspect_page_for_composition"ctx.post_run_page_observation_workflow_run_id = "wr_x"ctx.last_run_blocks_workflow_run_id = "wr_x"- assert _check_enforcement(ctx) == POST_FAILED_TEST_NUDGE+ assert enforcement_decision(ctx).rule == "post_failed_test"# ---------------------------------------------------------------------------@@ -214,12 +208,12 @@ def test_suspicious_success_nudge_refires_on_subsequent_turn() -> None:ctx.last_test_ok = Nonectx.last_test_suspicious_success = True- first = _check_enforcement(ctx)- assert first == POST_SUSPICIOUS_SUCCESS_NUDGE+ first = enforcement_decision(ctx)+ assert first.rule == "post_suspicious_success"# Without a rerun, the flag must still be set so the nudge fires again.assert ctx.last_test_suspicious_success is True- second = _check_enforcement(ctx)- assert second == POST_SUSPICIOUS_SUCCESS_NUDGE+ second = enforcement_decision(ctx)+ assert second.rule == "post_suspicious_success"# ---------------------------------------------------------------------------@@ -324,18 +318,18 @@ def test_update_enforcement_from_tool_resets_navigate_latch_on_new_navigate() ->assert ctx.navigate_enforcement_done is False-def test_check_enforcement_refires_navigate_nudge_after_latch_reset() -> None:+def test_enforcement_decision_refires_navigate_nudge_after_latch_reset() -> None:ctx = _fresh_context()# First navigate-without-observe: nudge fires, latch set.ctx.navigate_called = Truectx.observation_after_navigate = False- assert _check_enforcement(ctx) == POST_NAVIGATE_NUDGE+ assert enforcement_decision(ctx).rule == "post_navigate"assert ctx.navigate_enforcement_done is True# Agent re-navigates without observing; the streaming adapter re-arms the latch._update_enforcement_from_tool(ctx, "navigate_browser", {"ok": True, "data": {}})# Nudge fires again on the new cycle.- assert _check_enforcement(ctx) == POST_NAVIGATE_NUDGE+ assert enforcement_decision(ctx).rule == "post_navigate"# ---------------------------------------------------------------------------@@ -531,7 +525,7 @@ def test_observed_reply_cannot_finalize_while_repair_is_owed() -> None:ctx = _failed_run_ctx(RepairNextAction.REPAIR)result = _FakeFinalResult("REPLY", "The latest bill link opens an email-delivery form.")- assert _check_enforcement(ctx, result) is not None+ assert enforcement_decision(ctx, result) is not Nonedef test_observed_reply_finalizes_once_repair_is_discharged() -> None:@@ -541,7 +535,7 @@ def test_observed_reply_finalizes_once_repair_is_discharged() -> None:ctx = _failed_run_ctx(RepairNextAction.NO_CHANGE)result = _FakeFinalResult("REPLY", "Downloaded the latest invoice.")- assert _check_enforcement(ctx, result) is None+ assert enforcement_decision(ctx, result) is Nonedef test_ask_question_still_finalizes_while_repair_is_owed() -> None:@@ -552,7 +546,7 @@ def test_ask_question_still_finalizes_while_repair_is_owed() -> None:ctx.failed_test_nudge_count = 99 # counters exhaustedresult = _FakeFinalResult("ASK_QUESTION", "Which account should I use?")- assert _check_enforcement(ctx, result) is None+ assert enforcement_decision(ctx, result) is Nonedef test_exhausted_nudge_counters_do_not_release_an_open_repair_obligation() -> None:@@ -563,7 +557,7 @@ def test_exhausted_nudge_counters_do_not_release_an_open_repair_obligation() ->ctx.failed_test_nudge_count = 99result = _FakeFinalResult("REPLY", "I drafted the workflow; it attempts to download the bill.")- assert _check_enforcement(ctx, result) == POST_FAILED_TEST_NUDGE+ assert enforcement_decision(ctx, result).rule == "post_failed_test"def test_stop_decision_releases_the_turn_even_with_counters_exhausted() -> None:@@ -574,7 +568,7 @@ def test_stop_decision_releases_the_turn_even_with_counters_exhausted() -> None:ctx.failed_test_nudge_count = 99result = _FakeFinalResult("REPLY", "This site requires a mailed statement request.")- assert _check_enforcement(ctx, result) is None+ assert enforcement_decision(ctx, result) is Nonedef test_repair_obligation_releases_the_turn_once_its_rounds_are_spent() -> None:@@ -587,10 +581,10 @@ def test_repair_obligation_releases_the_turn_once_its_rounds_are_spent() -> Noneresult = _FakeFinalResult("REPLY", "This site offers no downloadable statement.")# Held open while rounds remain.- assert _check_enforcement(ctx, result) == POST_FAILED_TEST_NUDGE+ assert enforcement_decision(ctx, result).rule == "post_failed_test"ctx.repair_obligation_nudge_count = MAX_REPAIR_OBLIGATION_NUDGES- assert _check_enforcement(ctx, result) is None+ assert enforcement_decision(ctx, result) is Noneclass TestWitnessArbitration:diff --git a/tests/unit/test_copilot_enforcement_pruning.py b/tests/unit/test_copilot_enforcement_pruning.pyindex 83e1201ac..3587d91a0 100644--- a/tests/unit/test_copilot_enforcement_pruning.py+++ b/tests/unit/test_copilot_enforcement_pruning.py@@ -22,7 +22,6 @@ from structlog.testing import capture_logsfrom skyvern.config import Settings, settingsfrom skyvern.forge.sdk.copilot.blocker_signal import (CopilotToolBlockerSignal,- stash_blocker_signal,)from skyvern.forge.sdk.copilot.build_phase import BuildPhasefrom skyvern.forge.sdk.copilot.build_test_outcome import (@@ -43,9 +42,7 @@ from skyvern.forge.sdk.copilot.config import (from skyvern.forge.sdk.copilot.context import CodeAuthoringRepairContextfrom skyvern.forge.sdk.copilot.enforcement import (KEEP_RECENT_TOOL_OUTPUTS,- SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE,TOTAL_TIMEOUT_SECONDS,- _check_enforcement,_mark_copilot_total_timeout,_mark_copilot_total_timeout_if_elapsed,_maybe_synthesized_block_offer_msg,@@ -53,18 +50,16 @@ from skyvern.forge.sdk.copilot.enforcement import (_prune_input_list,_recover_from_context_overflow,_requested_output_paths_for_ctx,- _should_block_mutating_tool_after_synthesized_offer,_should_force_advisory_run_dispatch,- _should_force_synthesized_block_persistence,_summarize_tool_output,aggressive_prune,arm_credential_scout_reopen,+ enforcement_decision,mint_scout_observation_contract_for_ctx,pre_run_gated_outputs_without_path,record_scouted_output_coverage,requested_scalar_output_extraction_plan,run_with_enforcement,- synthesized_block_persistence_signal,synthesized_goal_completion_landing_pending,synthesized_offer_reopened_for_extraction_plan,synthesized_persistence_reopened,@@ -101,7 +96,6 @@ from skyvern.forge.sdk.copilot.tools.scouting import ()from skyvern.forge.sdk.copilot.turn_halt import stash_turn_halt_from_blocker_signalfrom skyvern.forge.sdk.copilot.turn_intent import RequiredContextKey, TurnIntent, TurnIntentAuthority, TurnIntentMode-from skyvern.forge.sdk.copilot.turn_ownership import TurnClaimant, current_turn_ownerfrom skyvern.forge.sdk.copilot.verification_evidence import WorkflowVerificationEvidencefrom tests.unit.conftest import make_copilot_context@@ -209,75 +203,6 @@ class TestSynthesizedOfferPersistenceGate:ctx.synthesized_block_offered_goal_complete = synthesized_trajectory_is_goal_complete(ctx)return ctx- @pytest.mark.asyncio- async def test_offer_retry_forces_update_and_run_blocks_tool_choice(- self,- monkeypatch: pytest.MonkeyPatch,- ) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.scout_trajectory = [- {- "tool_name": "type_text",- "selector": "input[name='q']",- "source_url": "https://example.test/start",- "accessible_name": "Search",- },- {- "tool_name": "click",- "selector": "button[data-action='search']",- "accessible_name": "Search",- },- ]- ctx.synthesized_block_offered = False- ctx.synthesized_block_offered_trajectory_len = 0- ctx.reached_download_target = None- stream = MagicMock()- stream.is_disconnected = AsyncMock(return_value=False)-- fake_result = MagicMock()- fake_result.final_output = None- fake_result.new_items = []- fake_result.to_input_list.return_value = []- run_configs: list[RunConfig | None] = []-- def fake_run_streamed(*args: Any, **kwargs: Any) -> Any:- run_configs.append(kwargs.get("run_config"))- return fake_result-- async def fake_stream_to_sse(result: Any, s: Any, c: Any) -> None:- if len(run_configs) >= 2:- c.update_workflow_called = True- c.test_after_update_done = True-- monkeypatch.setattr(- "skyvern.forge.sdk.copilot.enforcement.synthesize_code_block",- lambda *args, **kwargs: SynthesizedCodeBlock(code="await page.click('button')"),- )- monkeypatch.setattr("skyvern.forge.sdk.copilot.enforcement.Runner.run_streamed", fake_run_streamed)- monkeypatch.setattr(- "skyvern.forge.sdk.copilot.streaming_adapter.stream_to_sse",- fake_stream_to_sse,- )-- returned = await run_with_enforcement(- agent=MagicMock(),- initial_input="hello",- ctx=ctx,- stream=stream,- run_config=RunConfig(),- )-- assert returned is fake_result- assert len(run_configs) == 2- assert run_configs[0].model_settings is None- assert run_configs[1].model_settings is not None- assert run_configs[1].model_settings.tool_choice == "update_and_run_blocks"-@pytest.mark.asyncioasync def test_diagnose_offer_retry_does_not_force_update_and_run_blocks_tool_choice(self,@@ -323,390 +248,22 @@ class TestSynthesizedOfferPersistenceGate:)monkeypatch.setattr("skyvern.forge.sdk.copilot.enforcement.Runner.run_streamed", fake_run_streamed)monkeypatch.setattr(- "skyvern.forge.sdk.copilot.streaming_adapter.stream_to_sse",- fake_stream_to_sse,- )-- returned = await run_with_enforcement(- agent=MagicMock(),- initial_input="hello",- ctx=ctx,- stream=stream,- run_config=RunConfig(),- )-- assert returned is fake_result- assert len(run_configs) == 2- assert run_configs[0].model_settings is None- assert run_configs[1].model_settings is None-- def test_authoring_offer_blocks_non_persistence_tool_until_update_and_run_blocks(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 2- ctx.synthesized_block_offered_goal_complete = True- ctx.scout_trajectory = [- {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},- {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},- ]-- signal = synthesized_block_persistence_signal(ctx, "evaluate")-- assert isinstance(signal, CopilotToolBlockerSignal)- assert signal.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE- assert signal.blocked_tool == "evaluate"- assert signal.cleared_by_tools == frozenset({"update_and_run_blocks"})- assert signal.recovery_hint == "retry_with_different_tool"- assert signal.renders_final_reply is False- assert synthesized_block_persistence_signal(ctx, "update_and_run_blocks") is None-- def test_authoring_offer_blocks_page_mutating_tool_before_loop_detection(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 1- ctx.synthesized_block_offered_goal_complete = False- ctx.scout_trajectory = [{"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"}]-- signal = synthesized_block_persistence_signal(ctx, "click")-- assert isinstance(signal, CopilotToolBlockerSignal)- assert signal.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE- assert signal.blocked_tool == "click"- assert signal.cleared_by_tools == frozenset({"update_and_run_blocks"})- assert signal.renders_final_reply is False-- def test_actuation_obligation_admits_required_fill_tool_during_persistence_offer(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- required_context={RequiredContextKey.BROWSER_STATE},- )- ctx.request_policy = RequestPolicy(- completion_criteria=[- CompletionCriterion(- id="form-submit",- outcome="form fields are filled",- kind="terminal_action",- terminal_action_family="form",- )- ],- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 1- ctx.scout_trajectory = [{"tool_name": "click", "selector": "button.start", "accessible_name": "Start"}]-- assert synthesized_block_persistence_signal(ctx, "type_text") is None- click_signal = synthesized_block_persistence_signal(ctx, "click")- assert isinstance(click_signal, CopilotToolBlockerSignal)- assert click_signal.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE-- def test_actuation_obligation_fill_admission_registers_precedence_claim(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- required_context={RequiredContextKey.BROWSER_STATE},- )- ctx.request_policy = RequestPolicy(- completion_criteria=[- CompletionCriterion(- id="form-submit",- outcome="form fields are filled",- kind="terminal_action",- terminal_action_family="form",- )- ],- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 1- ctx.scout_trajectory = [{"tool_name": "click", "selector": "button.start", "accessible_name": "Start"}]-- assert synthesized_block_persistence_signal(ctx, "type_text") is None-- assert ctx.turn_ownership is not None- assert TurnClaimant.ACTUATION_OBLIGATION_FILL in ctx.turn_ownership.claims- assert current_turn_owner(ctx) is None-- def test_actuation_obligation_admits_required_fill_tool_for_method_mandated_run_contract(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- required_context={RequiredContextKey.BROWSER_STATE},- )- ctx.request_policy = RequestPolicy(- completion_criteria=[- CompletionCriterion(- id="visible-fill",- outcome="fields are visibly filled on the live page",- method_mandated=True,- level="run",- )- ],- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 1- ctx.scout_trajectory = [{"tool_name": "click", "selector": "button.start", "accessible_name": "Start"}]-- assert synthesized_block_persistence_signal(ctx, "type_text") is None- click_signal = synthesized_block_persistence_signal(ctx, "click")- assert isinstance(click_signal, CopilotToolBlockerSignal)- assert click_signal.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE-- def test_actuation_obligation_blocks_type_text_for_definition_method_contract(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- required_context={RequiredContextKey.BROWSER_STATE},- )- ctx.request_policy = RequestPolicy(- completion_criteria=[- CompletionCriterion(- id="definition-contract",- outcome="inputs are reusable",- method_mandated=True,- level="definition",- )- ],- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 1- ctx.scout_trajectory = [{"tool_name": "click", "selector": "button.start", "accessible_name": "Start"}]-- signal = synthesized_block_persistence_signal(ctx, "type_text")-- assert isinstance(signal, CopilotToolBlockerSignal)- assert signal.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE-- def test_actuation_obligation_admits_required_fill_tool_after_turn_state_reconcile(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- required_context={RequiredContextKey.BROWSER_STATE},- )- ctx.request_policy = RequestPolicy(- completion_criteria=[- CompletionCriterion(- id="form-submit",- outcome="form fields are filled",- kind="terminal_action",- terminal_action_family="form",- )- ],- )- ctx.completion_criteria_turn_state = SimpleNamespace(- decision=SimpleNamespace(- criteria=(- CompletionCriterion(- id="workflow-run",- outcome="workflow has been tested",- kind="terminal_action",- terminal_action_family="workflow_run",- ),- )- )- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 2- ctx.scout_trajectory = [- {"tool_name": "click", "selector": "button.start", "accessible_name": "Start"},- {"tool_name": "type_text", "selector": "#company", "accessible_name": "Company"},- ]-- assert synthesized_block_persistence_signal(ctx, "type_text") is None-- def test_persistence_offer_blocks_type_text_without_actuation_obligation(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- required_context={RequiredContextKey.BROWSER_STATE},- )- ctx.request_policy = RequestPolicy()- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 1- ctx.scout_trajectory = [{"tool_name": "click", "selector": "button.start", "accessible_name": "Start"}]-- signal = synthesized_block_persistence_signal(ctx, "type_text")-- assert isinstance(signal, CopilotToolBlockerSignal)- assert signal.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE- assert signal.blocked_tool == "type_text"-- def test_actuation_obligation_admits_required_fill_tool_from_fill_trajectory(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- required_context={RequiredContextKey.BROWSER_STATE},- )- ctx.request_policy = RequestPolicy()- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 1- ctx.scout_trajectory = [- {- "tool_name": "type_text",- "selector": "#company",- "typed_value": "Example Realty Labs Inc",- "accessible_name": "Company",- }- ]-- assert synthesized_block_persistence_signal(ctx, "type_text") is None-- def test_fill_less_trajectory_does_not_arm_actuation_obligation(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- required_context={RequiredContextKey.BROWSER_STATE},- )- ctx.request_policy = RequestPolicy()- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 1- ctx.scout_trajectory = [{"tool_name": "click", "selector": "button.start", "accessible_name": "Start"}]-- signal = synthesized_block_persistence_signal(ctx, "type_text")-- assert isinstance(signal, CopilotToolBlockerSignal)- assert signal.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE-- def test_prerun_ambiguous_bare_selector_repair_allows_one_evaluate(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 1- ctx.synthesized_block_offered_goal_complete = True- ctx.scout_trajectory = [{"tool_name": "click", "selector": "button"}]- ctx.last_code_authoring_repair_context = CodeAuthoringRepairContext(- block_label="lookup",- reason_code="ambiguous_bare_selector",- selector="button",- source_url="https://example.com",- )-- assert synthesized_block_persistence_signal(ctx, "evaluate") is None-- def test_repeated_ambiguous_bare_selector_context_blocks_second_evaluate(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 1- ctx.synthesized_block_offered_goal_complete = True- ctx.scout_trajectory = [{"tool_name": "click", "selector": "button"}]- ctx.last_code_authoring_repair_context = CodeAuthoringRepairContext(- block_label="lookup",- reason_code="ambiguous_bare_selector",- selector="button",- source_url="https://example.com",- )-- assert synthesized_block_persistence_signal(ctx, "evaluate") is None- signal = synthesized_block_persistence_signal(ctx, "evaluate")-- assert isinstance(signal, CopilotToolBlockerSignal)- assert signal.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE-- def test_ambiguous_bare_selector_with_stable_alternative_still_requires_persistence(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 1- ctx.synthesized_block_offered_goal_complete = True- ctx.scout_trajectory = [{"tool_name": "click", "selector": "button"}]- ctx.last_code_authoring_repair_context = CodeAuthoringRepairContext(- block_label="lookup",- reason_code="ambiguous_bare_selector",- selector="button",- source_url="https://example.com",- selector_alternatives=[{"tool_name": "click", "selector": 'role=button[name="Search"]'}],- )-- signal = synthesized_block_persistence_signal(ctx, "evaluate")-- assert isinstance(signal, CopilotToolBlockerSignal)- assert signal.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE-- @pytest.mark.parametrize(- "tool_name",- ["update_and_run_blocks", "update_workflow", "fill_credential_field"],- )- def test_authoring_offer_keeps_allowed_tools_unblocked(self, tool_name: str) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 1- ctx.synthesized_block_offered_goal_complete = False- ctx.scout_trajectory = [{"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"}]-- assert synthesized_block_persistence_signal(ctx, tool_name) is None-- def test_authoring_offer_does_not_block_mutating_tool_when_trajectory_changed(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 1- ctx.synthesized_block_offered_goal_complete = False- ctx.scout_trajectory = [- {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},- {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},- ]-- assert synthesized_block_persistence_signal(ctx, "click") is None+ "skyvern.forge.sdk.copilot.streaming_adapter.stream_to_sse",+ fake_stream_to_sse,+ )- def test_authoring_offer_does_not_block_mutating_tool_without_offer(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),+ returned = await run_with_enforcement(+ agent=MagicMock(),+ initial_input="hello",+ ctx=ctx,+ stream=stream,+ run_config=RunConfig(),)- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = False- ctx.synthesized_block_offered_trajectory_len = 1- ctx.scout_trajectory = [{"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"}]- assert synthesized_block_persistence_signal(ctx, "click") is None+ assert returned is fake_result+ assert len(run_configs) == 2+ assert run_configs[0].model_settings is None+ assert run_configs[1].model_settings is Nonedef _post_run_page_path_ctx(self,@@ -1060,408 +617,52 @@ class TestSynthesizedOfferPersistenceGate:bind_post_run_page_path_failure(ctx,- {- "workflow_run_id": "wr_129160000000000001",- "observed_after_workflow_run": True,- "current_url": "https://example.test/mfa",- "forms": [- {- "fields": [{"type": "password", "selector": "#token"}],- "submit_controls": [{"type": "submit", "selector": "#verify"}],- }- ],- },- )-- condition = ctx.latest_recorded_build_test_outcome.page_path_failure- assert condition is not None- assert condition.current_url == "https://example.test/mfa"- assert condition.continuation_targets == (PostRunPagePathTarget(kind="form_submit", selector="#verify"),)-- def test_schema_empty_screenshot_does_not_replace_post_run_page_path_contract(self) -> None:- ctx = self._post_run_page_path_ctx()- original = ctx.latest_recorded_build_test_outcome.page_path_failure-- _record_composition_page_observation(- ctx,- source_tool="get_browser_screenshot",- url="https://example.test/challenge",- title="Challenge",- )-- assert ctx.latest_recorded_build_test_outcome.page_path_failure == original- assert ctx.post_run_page_observation_generation == 1-- @pytest.mark.parametrize(- "ctx_attrs",- [- {"synthesized_block_offered": False, "synthesized_block_offered_trajectory_len": 0},- {"synthesized_block_offered": True, "synthesized_block_offered_trajectory_len": 0},- {- "synthesized_block_offered": True,- "synthesized_block_offered_trajectory_len": 4,- "update_workflow_called": True,- },- ],- ids=["no_offer", "zero_trajectory", "update_called"],- )- def test_non_persistence_tool_unaffected_without_active_offer_or_after_update(- self,- ctx_attrs: dict[str, object],- ) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- for key, value in ctx_attrs.items():- setattr(ctx, key, value)-- assert synthesized_block_persistence_signal(ctx, "evaluate") is None-- def test_recorded_workflow_update_clears_synthesized_persistence_gate(self) -> None:- ctx = make_copilot_context(workflow_yaml="title: Updated")- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 2- ctx.scout_trajectory = [- {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},- {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},- ]- ctx.synthesized_block_offered_goal_complete = True-- _record_workflow_update_result(- ctx,- {- "ok": True,- "data": {"block_count": 1},- "_workflow": SimpleNamespace(workflow_definition=SimpleNamespace(blocks=[SimpleNamespace()])),- },- )-- assert ctx.update_workflow_called is True- assert synthesized_block_persistence_signal(ctx, "evaluate") is None-- def test_recorded_workflow_update_clears_reopened_synthesized_persistence_latch(self) -> None:- ctx = make_copilot_context(workflow_yaml="title: Updated")- ctx.synthesized_block_reopened_after_failed_run = True-- _record_workflow_update_result(- ctx,- {- "ok": True,- "data": {"block_count": 1},- "_workflow": SimpleNamespace(workflow_definition=SimpleNamespace(blocks=[SimpleNamespace()])),- },- )-- assert ctx.synthesized_block_reopened_after_failed_run is False-- def test_stale_shorter_offer_does_not_force_current_goal_complete_trajectory(self) -> None:- previous_trajectory = [- {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},- ]- trajectory = [- *previous_trajectory,- {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},- ]- ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)- ctx.synthesized_block_offered_trajectory_len = len(previous_trajectory)- ctx.synthesized_block_offered_goal_complete = False-- assert synthesized_trajectory_is_goal_complete(ctx) is True- assert _should_force_synthesized_block_persistence(ctx) is False- assert synthesized_block_persistence_signal(ctx, "evaluate") is None-- def test_refreshed_goal_complete_offer_forces_current_trajectory(self) -> None:- trajectory = [- {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},- {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},- ]- ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)-- assert ctx.synthesized_block_offered_trajectory_len == len(trajectory)- assert ctx.synthesized_block_offered_goal_complete is True- assert _should_force_synthesized_block_persistence(ctx) is True- assert synthesized_block_persistence_signal(ctx, "evaluate") is not None-- def test_unlanded_goal_completion_forces_persistence_after_first_authoring_call(self) -> None:- trajectory = [- {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},- {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},- ]- ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)- ctx.impose_synthesized_code_block = True- ctx.update_workflow_called = True-- assert synthesized_goal_completion_landing_pending(ctx) is True- assert synthesized_persistence_reopened(ctx) is True- assert _should_force_synthesized_block_persistence(ctx) is True- assert synthesized_block_persistence_signal(ctx, "evaluate") is not None-- def test_landed_goal_completion_stops_forcing_on_identical_resubmission(self) -> None:- trajectory = [- {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},- {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},- ]- ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)- ctx.impose_synthesized_code_block = True- ctx.update_workflow_called = True- ctx.synthesized_goal_complete_landed = True-- assert synthesized_goal_completion_landing_pending(ctx) is False- assert synthesized_persistence_reopened(ctx) is False- assert _should_force_synthesized_block_persistence(ctx) is False- assert synthesized_block_persistence_signal(ctx, "evaluate") is None-- def test_failed_verified_run_with_new_commit_reopens_synthesized_persistence_gate(self) -> None:- previous_trajectory = [- {- "tool_name": "fill_credential_field",- "selector": "#username",- "credential_id": "cred_1",- "credential_field": "username",- },- {"tool_name": "click", "selector": "button[data-action='login']", "accessible_name": "Log in"},- ]- trajectory = [- *previous_trajectory,- {"tool_name": "click", "selector": "button[data-action='businessToggle']", "accessible_name": "Business"},- ]- ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)- ctx.update_workflow_called = True- ctx.test_after_update_done = True- ctx.last_test_ok = False- ctx.completion_verification_result = self._unsatisfied_verification()- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = len(previous_trajectory)- ctx.synthesized_block_offered_goal_complete = True-- assert synthesized_persistence_reopened_after_failed_run(ctx) is True- assert _should_force_synthesized_block_persistence(ctx) is False- assert synthesized_block_persistence_signal(ctx, "evaluate") is None-- assert _maybe_synthesized_block_offer_msg(ctx) is not None- assert ctx.synthesized_block_offered_trajectory_len == len(trajectory)- assert _should_force_synthesized_block_persistence(ctx) is True- assert synthesized_block_persistence_signal(ctx, "evaluate") is not None-- def test_failed_verified_run_without_new_commit_keeps_synthesized_persistence_gate_clear(self) -> None:- previous_trajectory = [- {- "tool_name": "fill_credential_field",- "selector": "#username",- "credential_id": "cred_1",- "credential_field": "username",- },- {"tool_name": "click", "selector": "button[data-action='login']", "accessible_name": "Log in"},- ]- trajectory = [- *previous_trajectory,- {- "tool_name": "fill_credential_field",- "selector": "#password",- "credential_id": "cred_1",- "credential_field": "password",- },- ]- ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)- ctx.update_workflow_called = True- ctx.test_after_update_done = True- ctx.last_test_ok = False- ctx.completion_verification_result = self._unsatisfied_verification()- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = len(previous_trajectory)- ctx.synthesized_block_offered_goal_complete = True-- assert synthesized_persistence_reopened_after_failed_run(ctx) is False- assert _should_force_synthesized_block_persistence(ctx) is False- assert synthesized_block_persistence_signal(ctx, "evaluate") is None-- def test_diagnose_offer_does_not_block_non_persistence_tool(self) -> None:- ctx = _Ctx()- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.DIAGNOSE,- authority=TurnIntentAuthority(may_read_run_context=True),- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 4-- assert synthesized_block_persistence_signal(ctx, "evaluate") is None-- @pytest.mark.parametrize(- "trajectory, download_target, expected_complete",- [- ([], None, False),- ([{"tool_name": "click", "selector": "a.home", "accessible_name": "Home"}], None, False),- ([{"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"}], None, False),- (- [- {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},- {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},- ],- None,- True,- ),- (- [- {"tool_name": "select_option", "selector": "select#state", "value": "CA"},- {"tool_name": "click", "selector": "button[type='submit']", "accessible_name": "Continue"},- ],- None,- True,- ),- (- [- {"tool_name": "select_option", "selector": "select#state", "value": ""},- {"tool_name": "click", "selector": "button[type='submit']", "accessible_name": "Continue"},- ],- None,- False,- ),- (- [- {"tool_name": "fill_credential_field", "credential_field": "username"},- {"tool_name": "click", "selector": "button[type='submit']", "accessible_name": "Log in"},- ],- None,- False,- ),- (- [- {"tool_name": "fill_credential_field", "credential_id": "cred", "credential_field": "username"},- {"tool_name": "fill_credential_field", "credential_id": "cred", "credential_field": "password"},- {"tool_name": "click", "selector": "button[type='submit']", "accessible_name": "Log in"},- ],- None,- True,- ),- (- [- {"tool_name": "fill_credential_field", "credential_id": "cred", "credential_field": "username"},- {"tool_name": "click", "selector": "button"},- ],- None,- False,- ),- (- [- {"tool_name": "fill_credential_field", "credential_id": "cred", "credential_field": "username"},- {"tool_name": "fill_credential_field", "credential_id": "cred", "credential_field": "password"},- {"tool_name": "click", "selector": "button[type='submit']", "accessible_name": "Log in"},- {"tool_name": "type_text", "selector": "input[name='amount']", "accessible_name": "Amount"},- {"tool_name": "click", "selector": "button[data-action='pay']", "accessible_name": "Pay"},- ],- None,- True,- ),- (- [{"tool_name": "click", "selector": "a.report", "accessible_name": "Report"}],- ReachedDownloadTarget(- selector="a.report",- affordance_text="Report",- download_kind="extension",- source_step="trajectory_recency",- already_registered=False,- ),- True,- ),- ],- ids=[- "empty",- "navigate_only",- "type_text_only",- "type_text_then_commit",- "select_with_value_then_commit",- "select_empty_value_dropped",- "credential_missing_id_dropped",- "valid_login_fill_and_submit",- "valid_fill_generic_opener_only",- "post_login_durable_entry_then_commit",- "reached_download_target",- ],- )- def test_goal_completeness_drives_force_and_blocker_in_lockstep(- self,- trajectory: list[dict[str, object]],- download_target: ReachedDownloadTarget | None,- expected_complete: bool,- ) -> None:- ctx = self._authoring_ctx(trajectory=trajectory, download_target=download_target)-- assert synthesized_trajectory_is_goal_complete(ctx) is expected_complete- assert _should_force_synthesized_block_persistence(ctx) is expected_complete- blocked = synthesized_block_persistence_signal(ctx, "evaluate") is not None- assert blocked is expected_complete-- def test_extract_shaped_lookup_submit_is_goal_complete_without_extract_step(self) -> None:- trajectory = [- {"tool_name": "type_text", "selector": "input[name='reference']", "accessible_name": "Reference"},- {"tool_name": "click", "selector": "button[data-action='lookup']", "accessible_name": "Look up"},- ]- ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)-- assert not any(- str(item.get("tool_name") or "") in {"extract", "get_run_results", "evaluate"} for item in trajectory+ {+ "workflow_run_id": "wr_129160000000000001",+ "observed_after_workflow_run": True,+ "current_url": "https://example.test/mfa",+ "forms": [+ {+ "fields": [{"type": "password", "selector": "#token"}],+ "submit_controls": [{"type": "submit", "selector": "#verify"}],+ }+ ],+ },)- assert synthesized_trajectory_is_goal_complete(ctx) is True- assert _should_force_synthesized_block_persistence(ctx) is True- @pytest.mark.parametrize(- "guard_attrs",- [- {"update_workflow_called": True},- {"synthesized_block_offered": False},- {"block_authoring_policy": BlockAuthoringPolicy.STANDARD},- ],- ids=["already_authored", "not_offered", "policy_not_code_only"],- )- def test_early_guards_never_force_even_when_goal_complete(self, guard_attrs: dict[str, object]) -> None:- trajectory = [- {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},- {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},- ]- ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)- for key, value in guard_attrs.items():- setattr(ctx, key, value)+ condition = ctx.latest_recorded_build_test_outcome.page_path_failure+ assert condition is not None+ assert condition.current_url == "https://example.test/mfa"+ assert condition.continuation_targets == (PostRunPagePathTarget(kind="form_submit", selector="#verify"),)- assert _should_force_synthesized_block_persistence(ctx) is False- assert synthesized_block_persistence_signal(ctx, "evaluate") is None+ def test_schema_empty_screenshot_does_not_replace_post_run_page_path_contract(self) -> None:+ ctx = self._post_run_page_path_ctx()+ original = ctx.latest_recorded_build_test_outcome.page_path_failure- def test_diagnose_turn_intent_never_forces_even_when_goal_complete(self) -> None:- trajectory = [- {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},- {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},- ]- ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.DIAGNOSE,- authority=TurnIntentAuthority(may_read_run_context=True),+ _record_composition_page_observation(+ ctx,+ source_tool="get_browser_screenshot",+ url="https://example.test/challenge",+ title="Challenge",)- assert _should_force_synthesized_block_persistence(ctx) is False- assert synthesized_block_persistence_signal(ctx, "evaluate") is None+ assert ctx.latest_recorded_build_test_outcome.page_path_failure == original+ assert ctx.post_run_page_observation_generation == 1- def test_update_only_turn_intent_never_forces_update_and_run_blocks_even_when_goal_complete(self) -> None:- trajectory = [- {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},- {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},- ]- ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.DRAFT_ONLY,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=False),+ def test_recorded_workflow_update_clears_reopened_synthesized_persistence_latch(self) -> None:+ ctx = make_copilot_context(workflow_yaml="title: Updated")+ ctx.synthesized_block_reopened_after_failed_run = True++ _record_workflow_update_result(+ ctx,+ {+ "ok": True,+ "data": {"block_count": 1},+ "_workflow": SimpleNamespace(workflow_definition=SimpleNamespace(blocks=[SimpleNamespace()])),+ },)- assert synthesized_trajectory_is_goal_complete(ctx) is True- assert _should_force_synthesized_block_persistence(ctx) is False- assert synthesized_block_persistence_signal(ctx, "evaluate") is None+ assert ctx.synthesized_block_reopened_after_failed_run is Falsedef test_goal_complete_commit_refreshes_offer_below_threshold(self, monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr(@@ -1584,36 +785,6 @@ class TestSynthesizedOfferPersistenceGate:assert message is Noneassert ctx.synthesized_block_offered_trajectory_len == 1- def test_terminal_blocker_replaces_nonterminal_synthesized_persistence_blocker(self) -> None:- ctx = _Ctx()- persistence_signal = CopilotToolBlockerSignal(- blocker_kind="tool_error",- agent_steering_text="Persist the drafted workflow before more scouting.",- user_facing_reason="I need to save and test the drafted workflow before scouting more.",- recovery_hint="retry_with_different_tool",- cleared_by_tools=frozenset({"update_and_run_blocks"}),- preserves_workflow_draft=True,- renders_final_reply=False,- internal_reason_code=SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE,- blocked_tool="click",- )- terminal_signal = CopilotToolBlockerSignal(- blocker_kind="loop_detected",- agent_steering_text="Report that repeated repair attempts did not verify the workflow outcome.",- user_facing_reason="The workflow was saved and tested, but repeated repairs did not reach the required review page.",- recovery_hint="report_blocker_to_user",- cleared_by_tools=frozenset(),- preserves_workflow_draft=True,- renders_final_reply=True,- internal_reason_code="loop_detected_generic",- blocked_tool="update_and_run_blocks",- )-- stash_blocker_signal(ctx, persistence_signal)- stash_blocker_signal(ctx, terminal_signal)-- assert ctx.blocker_signal is terminal_signal-def _in_progress_login_ctx(self, trajectory: list[dict[str, object]]) -> _Ctx:ctx = _Ctx()ctx.turn_intent = TurnIntent(@@ -1649,170 +820,127 @@ class TestSynthesizedOfferPersistenceGate:"source_url": source_url,}- def test_in_progress_login_admits_commit_interactions_after_credential_fills(self) -> None:- login_url = "https://portal.test/login"- ctx = self._in_progress_login_ctx(- [- self._credential_fill("username", 0, login_url),- self._credential_fill("password", 1, login_url),- self._login_submit_click(2, login_url),- ]- )+ def test_extract_shaped_lookup_submit_is_goal_complete_without_extract_step(self) -> None:+ trajectory = [+ {"tool_name": "type_text", "selector": "input[name='reference']", "accessible_name": "Reference"},+ {"tool_name": "click", "selector": "button[data-action='lookup']", "accessible_name": "Look up"},+ ]+ ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)- armed = synthesized_block_persistence_signal(ctx, "select_option", {"value": "monthly"})- assert isinstance(armed, CopilotToolBlockerSignal)- assert armed.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE+ assert not any(+ str(item.get("tool_name") or "") in {"extract", "get_run_results", "evaluate"} for item in trajectory+ )+ assert synthesized_trajectory_is_goal_complete(ctx) is True- assert synthesized_block_persistence_signal(ctx, "click", {"selector": "button[type='submit']"}) is None- assert synthesized_block_persistence_signal(ctx, "press_key", {"key": "Enter"}) is None- assert ctx.turn_ownership is not None- assert TurnClaimant.ACTUATION_OBLIGATION_LOGIN_COMPLETION in ctx.turn_ownership.claims+ def test_failed_verified_run_with_new_commit_reopens_synthesized_persistence_gate(self) -> None:+ previous_trajectory = [+ {+ "tool_name": "fill_credential_field",+ "selector": "#username",+ "credential_id": "cred_1",+ "credential_field": "username",+ },+ {"tool_name": "click", "selector": "button[data-action='login']", "accessible_name": "Log in"},+ ]+ trajectory = [+ *previous_trajectory,+ {"tool_name": "click", "selector": "button[data-action='businessToggle']", "accessible_name": "Business"},+ ]+ ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)+ ctx.update_workflow_called = True+ ctx.test_after_update_done = True+ ctx.last_test_ok = False+ ctx.completion_verification_result = self._unsatisfied_verification()+ ctx.synthesized_block_offered = True+ ctx.synthesized_block_offered_trajectory_len = len(previous_trajectory)+ ctx.synthesized_block_offered_goal_complete = True- non_submit_key = synthesized_block_persistence_signal(ctx, "press_key", {"key": "Tab"})- assert isinstance(non_submit_key, CopilotToolBlockerSignal)- assert non_submit_key.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE+ assert synthesized_persistence_reopened_after_failed_run(ctx) is True- def test_in_progress_login_refuses_press_key_when_arguments_are_absent(self) -> None:- login_url = "https://portal.test/login"- ctx = self._in_progress_login_ctx(- [- self._credential_fill("username", 0, login_url),- self._credential_fill("password", 1, login_url),- self._login_submit_click(2, login_url),- ]- )+ assert _maybe_synthesized_block_offer_msg(ctx) is not None+ assert ctx.synthesized_block_offered_trajectory_len == len(trajectory)- assert synthesized_block_persistence_signal(ctx, "press_key", {"key": "Enter"}) is None+ def test_failed_verified_run_without_new_commit_keeps_synthesized_persistence_gate_clear(self) -> None:+ previous_trajectory = [+ {+ "tool_name": "fill_credential_field",+ "selector": "#username",+ "credential_id": "cred_1",+ "credential_field": "username",+ },+ {"tool_name": "click", "selector": "button[data-action='login']", "accessible_name": "Log in"},+ ]+ trajectory = [+ *previous_trajectory,+ {+ "tool_name": "fill_credential_field",+ "selector": "#password",+ "credential_id": "cred_1",+ "credential_field": "password",+ },+ ]+ ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)+ ctx.update_workflow_called = True+ ctx.test_after_update_done = True+ ctx.last_test_ok = False+ ctx.completion_verification_result = self._unsatisfied_verification()+ ctx.synthesized_block_offered = True+ ctx.synthesized_block_offered_trajectory_len = len(previous_trajectory)+ ctx.synthesized_block_offered_goal_complete = True- for absent in (None, "Enter", ["Enter"]):- refused = synthesized_block_persistence_signal(ctx, "press_key", absent)- assert isinstance(refused, CopilotToolBlockerSignal)- assert refused.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE+ assert synthesized_persistence_reopened_after_failed_run(ctx) is False- def test_in_progress_login_admits_two_factor_confirm_after_token_fill(self) -> None:- login_url = "https://portal.test/login"- ctx = self._in_progress_login_ctx(- [- self._credential_fill("username", 0, login_url),- self._credential_fill("password", 1, login_url),- self._login_submit_click(2, login_url),- self._credential_fill("totp", 3, "https://portal.test/mfa"),- ]- )+ def test_landed_goal_completion_stops_forcing_on_identical_resubmission(self) -> None:+ trajectory = [+ {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},+ {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},+ ]+ ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)+ ctx.impose_synthesized_code_block = True+ ctx.update_workflow_called = True+ ctx.synthesized_goal_complete_landed = True- armed = synthesized_block_persistence_signal(ctx, "select_option", {"value": "monthly"})- assert isinstance(armed, CopilotToolBlockerSignal)- assert armed.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE+ assert synthesized_goal_completion_landing_pending(ctx) is False+ assert synthesized_persistence_reopened(ctx) is False- assert synthesized_block_persistence_signal(ctx, "fill_credential_field", {"credential_field": "totp"}) is None- assert synthesized_block_persistence_signal(ctx, "click", {"selector": "button[data-action='verify']"}) is None- assert synthesized_block_persistence_signal(ctx, "press_key", {"key": "Enter"}) is None- assert ctx.turn_ownership is not None- assert TurnClaimant.ACTUATION_OBLIGATION_LOGIN_COMPLETION in ctx.turn_ownership.claims+ def test_stale_shorter_offer_does_not_force_current_goal_complete_trajectory(self) -> None:+ previous_trajectory = [+ {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},+ ]+ trajectory = [+ *previous_trajectory,+ {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},+ ]+ ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)+ ctx.synthesized_block_offered_trajectory_len = len(previous_trajectory)+ ctx.synthesized_block_offered_goal_complete = False- def test_login_completion_closes_at_post_credential_commit(self) -> None:- login_url = "https://portal.test/login"- reports_url = "https://portal.test/reports"- ctx = self._in_progress_login_ctx(- [- self._credential_fill("username", 0, login_url),- self._credential_fill("password", 1, login_url),- self._login_submit_click(2, login_url),- {- "tool_name": "type_text",- "trajectory_index": 3,- "selector": "input[name='date_from']",- "typed_value": "-7d",- "source_url": reports_url,- },- {- "tool_name": "click",- "trajectory_index": 4,- "selector": "button[data-action='run-report']",- "accessible_name": "Run report",- "source_url": reports_url,- },- ]- )+ assert synthesized_trajectory_is_goal_complete(ctx) is True- blocked_click = synthesized_block_persistence_signal(ctx, "click", {"selector": "button[data-action='next']"})- assert isinstance(blocked_click, CopilotToolBlockerSignal)- assert blocked_click.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE-- blocked_key = synthesized_block_persistence_signal(ctx, "press_key", {"key": "Enter"})- assert isinstance(blocked_key, CopilotToolBlockerSignal)- assert blocked_key.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE- assert ctx.turn_ownership is None-- def test_login_completion_window_stays_open_on_read_only_post_login_surface(self) -> None:- """Characterizes the shipped bound, which is wider than 'until the login outcome is observable'.-- The close condition credits a commit only from an ordered pair whose second click is neither a- generic opener nor results-surface navigation. A read-only post-login surface never supplies one,- so the window stays open for the rest of the turn. Tracked as a follow-up, not a shipped intent.- """- login_url = "https://portal.test/login"- app_url = "https://portal.test/analytics"- ctx = self._in_progress_login_ctx(- [- self._credential_fill("username", 0, login_url),- self._credential_fill("password", 1, login_url),- self._login_submit_click(2, login_url),- {- "tool_name": "click",- "trajectory_index": 3,- "selector": "a[href='/analytics/web']",- "accessible_name": "Web analytics",- "role": "link",- "source_url": app_url,- },- {- "tool_name": "click",- "trajectory_index": 4,- "selector": "a[href='/analytics/web?range=7d']",- "accessible_name": "Last 7 days",- "role": "link",- "source_url": app_url,- },- {- "tool_name": "click",- "trajectory_index": 5,- "selector": "table tbody tr:nth-child(2)",- "accessible_name": "Row 2",- "source_url": app_url,- },- ]- )+ def test_unlanded_goal_completion_forces_persistence_after_first_authoring_call(self) -> None:+ trajectory = [+ {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},+ {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},+ ]+ ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)+ ctx.impose_synthesized_code_block = True+ ctx.update_workflow_called = True- assert synthesized_block_persistence_signal(ctx, "click", {"selector": "button#delete-account"}) is None+ assert synthesized_goal_completion_landing_pending(ctx) is True+ assert synthesized_persistence_reopened(ctx) is True- def test_click_outside_authentication_still_blocked_by_persistence_gate(self) -> None:- ctx = _Ctx()+ def test_update_only_turn_intent_never_forces_update_and_run_blocks_even_when_goal_complete(self) -> None:+ trajectory = [+ {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},+ {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},+ ]+ ctx = self._authoring_ctx(trajectory=trajectory, download_target=None)ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- required_context={RequiredContextKey.BROWSER_STATE},- )- ctx.request_policy = RequestPolicy(- completion_criteria=[- CompletionCriterion(- id="form-submit",- outcome="form fields are filled",- kind="terminal_action",- terminal_action_family="form",- )- ],+ mode=TurnIntentMode.DRAFT_ONLY,+ authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=False),)- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 1- ctx.scout_trajectory = [- {"tool_name": "click", "selector": "button.start", "accessible_name": "Start"},- ]- click_signal = synthesized_block_persistence_signal(ctx, "click")- assert isinstance(click_signal, CopilotToolBlockerSignal)- assert click_signal.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE+ assert synthesized_trajectory_is_goal_complete(ctx) is True# ---------------------------------------------------------------------------@@ -1922,7 +1050,7 @@ def test_unrecoverable_contract_stop_preempts_failed_test_nudge() -> None:)with pytest.raises(CopilotUnrecoverableToolError):- _check_enforcement(ctx)+ enforcement_decision(ctx)assert ctx.failed_test_nudge_count == 0@@ -2394,8 +1522,11 @@ def test_aggressive_prune_drops_orphan_from_eight_pair_repro() -> None:assert _call_ids(pruned, "function_call_output") == ["call_5", "call_6", "call_7"]+# tail_size samples the boundaries that change behaviour: below one pair, exactly one+# pair, either side of KEEP_RECENT_TOOL_OUTPUTS, the production default, and longer+# than the 21 non-screenshot items _tool_history(10) builds.@pytest.mark.parametrize("pair_count", [1, 2, 4, 8, 10])[email protected]("tail_size", range(1, 21))[email protected]("tail_size", [1, 2, 3, 4, 7, 25])@pytest.mark.parametrize("interleave_screenshots", [False, True])@pytest.mark.parametrize("attr_style", [False, True])def test_aggressive_prune_never_keeps_orphaned_tool_results(@@ -2478,7 +1609,7 @@ def test_copilot_config_ignores_qa_budget_in_cloud(monkeypatch: pytest.MonkeyPat@pytest.mark.asyncio[email protected]("tail_size", range(1, 21))[email protected]("tail_size", [1, 2, 3, 4, 7, 25])@pytest.mark.parametrize("attr_style", [False, True])async def test_context_overflow_session_rewrite_stores_pair_valid_history(monkeypatch: pytest.MonkeyPatch,@@ -2623,13 +1754,13 @@ class TestEnforcement:def test_no_enforcement_when_nothing_pending(self) -> None:ctx = self._make_ctx()- assert _check_enforcement(ctx) is None+ assert enforcement_decision(ctx) is Nonedef test_post_navigate_nudge(self) -> None:ctx = self._make_ctx(navigate_called=True, observation_after_navigate=False)- nudge = _check_enforcement(ctx)+ nudge = enforcement_decision(ctx)assert nudge is not None- assert "observe" in nudge.lower() or "inspect" in nudge.lower()+ assert nudge.rule == "post_navigate"assert ctx.navigate_enforcement_done is Truedef test_post_navigate_only_fires_once(self) -> None:@@ -2638,13 +1769,13 @@ class TestEnforcement:observation_after_navigate=False,navigate_enforcement_done=True,)- assert _check_enforcement(ctx) is None+ assert enforcement_decision(ctx) is Nonedef test_post_update_nudge(self) -> None:ctx = self._make_ctx(update_workflow_called=True, test_after_update_done=False)- nudge = _check_enforcement(ctx)+ nudge = enforcement_decision(ctx)assert nudge is not None- assert "test" in nudge.lower() or "run_blocks" in nudge.lower()+ assert nudge.rule == "post_update"def test_navigate_takes_priority_over_update(self) -> None:ctx = self._make_ctx(@@ -2653,8 +1784,9 @@ class TestEnforcement:update_workflow_called=True,test_after_update_done=False,)- nudge = _check_enforcement(ctx)- assert "observe" in nudge.lower() or "inspect" in nudge.lower()+ nudge = enforcement_decision(ctx)+ assert nudge is not None+ assert nudge.rule == "post_navigate"def test_intermediate_success_nudge_for_multistep_goal(self) -> None:ctx = self._make_ctx(@@ -2665,11 +1797,9 @@ class TestEnforcement:user_message="Go to france.fr and then download all french regulations",coverage_nudge_count=0,)- from skyvern.forge.sdk.copilot.enforcement import POST_INTERMEDIATE_SUCCESS_NUDGE-# Coverage gate only fires when the model tries to emit a REPLY.- nudge = _check_enforcement(ctx, self._reply_result("draft response"))- assert nudge == POST_INTERMEDIATE_SUCCESS_NUDGE+ nudge = enforcement_decision(ctx, self._reply_result("draft response"))+ assert nudge.rule == "post_intermediate_success"assert ctx.coverage_nudge_count == 1def test_no_intermediate_success_nudge_for_single_step_goal(self) -> None:@@ -2681,7 +1811,7 @@ class TestEnforcement:user_message="Go to france.fr",coverage_nudge_count=0,)- assert _check_enforcement(ctx, self._reply_result("done")) is None+ assert enforcement_decision(ctx, self._reply_result("done")) is Nonedef test_intermediate_success_nudge_fires_for_two_blocks(self) -> None:"""Key regression: nudge must fire even when block_count > 1."""@@ -2694,10 +1824,8 @@ class TestEnforcement:user_message="Go to france.fr and then download all french regulations and extract the titles",coverage_nudge_count=0,)- from skyvern.forge.sdk.copilot.enforcement import POST_INTERMEDIATE_SUCCESS_NUDGE-- nudge = _check_enforcement(ctx, self._reply_result("two-block draft"))- assert nudge == POST_INTERMEDIATE_SUCCESS_NUDGE+ nudge = enforcement_decision(ctx, self._reply_result("two-block draft"))+ assert nudge.rule == "post_intermediate_success"def test_intermediate_nudge_respects_global_cap(self) -> None:from skyvern.forge.sdk.copilot.enforcement import MAX_INTERMEDIATE_NUDGES@@ -2710,7 +1838,7 @@ class TestEnforcement:user_message="Go to france.fr and then download all french regulations",coverage_nudge_count=MAX_INTERMEDIATE_NUDGES,)- assert _check_enforcement(ctx, self._reply_result("capped")) is None+ assert enforcement_decision(ctx, self._reply_result("capped")) is Nonedef test_intermediate_nudge_does_not_fire_for_ten_plus_blocks(self) -> None:ctx = self._make_ctx(@@ -2721,7 +1849,7 @@ class TestEnforcement:user_message="Go to france.fr and then download all french regulations",coverage_nudge_count=0,)- assert _check_enforcement(ctx, self._reply_result("ten blocks")) is None+ assert enforcement_decision(ctx, self._reply_result("ten blocks")) is Nonedef test_ask_question_always_passes_even_with_coverage_gap(self) -> None:"""Regression guard: ASK_QUESTION must never be blocked by coverage."""@@ -2738,7 +1866,7 @@ class TestEnforcement:ask = MagicMock()ask.final_output = json.dumps({"type": "ASK_QUESTION", "user_response": "Which source?"})ask.new_items = []- assert _check_enforcement(ctx, ask) is None+ assert enforcement_decision(ctx, ask) is Nonedef test_plain_labeled_ask_question_passes_even_with_coverage_gap(self) -> None:ctx = self._make_ctx(@@ -2752,36 +1880,28 @@ class TestEnforcement:ask = MagicMock()ask.final_output = "ASK_QUESTION\nWhich source?"ask.new_items = []- assert _check_enforcement(ctx, ask) is None+ assert enforcement_decision(ctx, ask) is Nonedef test_explore_without_workflow_nudge(self) -> None:- from skyvern.forge.sdk.copilot.enforcement import POST_EXPLORE_WITHOUT_WORKFLOW_NUDGE-ctx = self._make_ctx(navigate_called=True,observation_after_navigate=True,update_workflow_called=False,test_after_update_done=False,)- nudge = _check_enforcement(ctx)- assert nudge == POST_EXPLORE_WITHOUT_WORKFLOW_NUDGE+ nudge = enforcement_decision(ctx)+ assert nudge.rule == "post_explore_without_workflow"assert ctx.explore_without_workflow_nudge_count == 1def test_explore_without_workflow_not_when_update_called(self) -> None:- from skyvern.forge.sdk.copilot.enforcement import (- POST_EXPLORE_WITHOUT_WORKFLOW_NUDGE,- POST_UPDATE_NUDGE,- )-ctx = self._make_ctx(navigate_called=True,observation_after_navigate=True,update_workflow_called=True,test_after_update_done=False,)- nudge = _check_enforcement(ctx)- assert nudge == POST_UPDATE_NUDGE- assert nudge != POST_EXPLORE_WITHOUT_WORKFLOW_NUDGE+ nudge = enforcement_decision(ctx)+ assert nudge.rule == "post_update"assert ctx.explore_without_workflow_nudge_count == 0def test_update_without_test_allowed_for_explicit_untested_draft(self) -> None:@@ -2791,25 +1911,20 @@ class TestEnforcement:test_after_update_done=False,)- assert _check_enforcement(ctx) is None+ assert enforcement_decision(ctx) is Nonedef test_explore_without_workflow_not_when_test_done(self) -> None:- from skyvern.forge.sdk.copilot.enforcement import POST_EXPLORE_WITHOUT_WORKFLOW_NUDGE-ctx = self._make_ctx(navigate_called=True,observation_after_navigate=True,update_workflow_called=False,test_after_update_done=True,)- nudge = _check_enforcement(ctx)- assert nudge != POST_EXPLORE_WITHOUT_WORKFLOW_NUDGE++ assert enforcement_decision(ctx) is Nonedef test_explore_without_workflow_respects_cap(self) -> None:- from skyvern.forge.sdk.copilot.enforcement import (- MAX_EXPLORE_WITHOUT_WORKFLOW_NUDGES,- POST_EXPLORE_WITHOUT_WORKFLOW_NUDGE,- )+ from skyvern.forge.sdk.copilot.enforcement import MAX_EXPLORE_WITHOUT_WORKFLOW_NUDGESctx = self._make_ctx(navigate_called=True,@@ -2818,21 +1933,18 @@ class TestEnforcement:test_after_update_done=False,explore_without_workflow_nudge_count=MAX_EXPLORE_WITHOUT_WORKFLOW_NUDGES,)- nudge = _check_enforcement(ctx)- assert nudge != POST_EXPLORE_WITHOUT_WORKFLOW_NUDGE- def test_explore_without_workflow_not_without_observation(self) -> None:- from skyvern.forge.sdk.copilot.enforcement import POST_EXPLORE_WITHOUT_WORKFLOW_NUDGE+ assert enforcement_decision(ctx) is None+ def test_explore_without_workflow_not_without_observation(self) -> None:ctx = self._make_ctx(navigate_called=True,observation_after_navigate=False,update_workflow_called=False,test_after_update_done=False,)- nudge = _check_enforcement(ctx)- # Should get navigate nudge, not explore-without-workflow- assert nudge != POST_EXPLORE_WITHOUT_WORKFLOW_NUDGE+ nudge = enforcement_decision(ctx)+ assert nudge.rule == "post_navigate"assert ctx.explore_without_workflow_nudge_count == 0@pytest.mark.asyncio@@ -3246,36 +2358,6 @@ class TestScoutOutputCoverageGate:assert uncovered_requested_output_paths(ctx) == {"output.document_name"}assert synthesized_trajectory_is_goal_complete(ctx) is False- def test_uncovered_output_keeps_gate_open_and_admits_scout_tools(self) -> None:- ctx = self._authoring_ctx(_criterion("output.document_name", "the order status document name is captured"))- assert uncovered_requested_output_paths(ctx) == {"output.document_name"}- assert synthesized_trajectory_is_goal_complete(ctx) is False- assert _should_force_synthesized_block_persistence(ctx) is False- assert synthesized_block_persistence_signal(ctx, "evaluate") is None- assert _should_block_mutating_tool_after_synthesized_offer(ctx, "click") is False- assert synthesized_block_persistence_signal(ctx, "click") is None-- def test_uncovered_output_leaves_the_trajectory_goal_reaching(self) -> None:- ctx = self._authoring_ctx(_criterion("output.document_name", "the order status document name is captured"))- assert uncovered_requested_output_paths(ctx) == {"output.document_name"}- assert synthesized_trajectory_reaches_goal(ctx) is True- assert synthesized_trajectory_is_goal_complete(ctx) is False- assert _should_force_synthesized_block_persistence(ctx) is False-- def test_value_bearing_container_coverage_without_plan_does_not_force(self) -> None:- ctx = self._authoring_ctx(_criterion("output.document_name", "the order status document name is captured"))- page_evidence = {- "result_containers": [- {"text_excerpt": "Document Name Resale Certificate 2024 for order 5591"},- ]- }- record_scouted_output_coverage(ctx, page_evidence)- assert ctx.scouted_output_covered_paths == {"output.document_name"}- assert uncovered_requested_output_paths(ctx) == set()- ctx.synthesized_block_offered_goal_complete = synthesized_trajectory_is_goal_complete(ctx)- assert synthesized_trajectory_is_goal_complete(ctx) is False- assert _should_force_synthesized_block_persistence(ctx) is False-@staticmethoddef _kv_page(*, key_text: str, url: str, value_prose: str) -> dict[str, object]:return {@@ -3597,13 +2679,6 @@ class TestScoutOutputCoverageGate:ctx.completion_criteria_turn_state = SimpleNamespace(decision=None)assert uncovered_requested_output_paths(ctx) == set()- def test_all_generic_token_path_still_requires_producer_plan(self) -> None:- ctx = self._authoring_ctx(_criterion("output.data", "the data is captured"))- assert uncovered_requested_output_paths(ctx) == set()- ctx.synthesized_block_offered_goal_complete = synthesized_trajectory_is_goal_complete(ctx)- assert synthesized_trajectory_is_goal_complete(ctx) is False- assert _should_force_synthesized_block_persistence(ctx) is False-def test_generic_path_exemption_keeps_specific_path_gating(self) -> None:ctx = self._authoring_ctx(_criterion("output.data", "the data is captured"),@@ -3646,39 +2721,6 @@ class TestScoutOutputCoverageGate:_restore_post_hook_context(ctx, snapshot)assert ctx.scout_observation_contract is None- def test_both_surfaces_grounded_still_blocks_mutating_tool(self) -> None:- ctx = self._authoring_ctx(- _criterion("output.visitors", "Visitors"),- _criterion("output.signups", "Signups"),- )- surface_one = self._kv_page(- key_text="Visitors",- url="https://analytics.example.com/dashboard",- value_prose="Visitors 1,234 recorded this week",- )- first_contract = mint_scout_observation_contract_for_ctx(- ctx, surface_one, url="https://analytics.example.com/dashboard"- )- record_scouted_output_coverage(ctx, surface_one, contract=first_contract)-- surface_two = self._kv_page(- key_text="Signups",- url="https://analytics.example.com/query",- value_prose="Signups 987 total this week",- )- second_contract = mint_scout_observation_contract_for_ctx(- ctx, surface_two, url="https://analytics.example.com/query"- )- record_scouted_output_coverage(ctx, surface_two, contract=second_contract)-- assert ctx.scouted_output_covered_paths == {"output.visitors", "output.signups"}- assert uncovered_requested_output_paths(ctx) == set()-- assert _should_block_mutating_tool_after_synthesized_offer(ctx, "click") is True- signal = synthesized_block_persistence_signal(ctx, "click")- assert signal is not None- assert signal.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE-def test_rekeyed_requested_output_compiles_the_offer_recipe_named_from_its_label(self) -> None:# Without the rekey fallback no label reaches the plan, so the offer is skipped and the# schema is left to the agent to invent.@@ -3716,6 +2758,26 @@ class TestScoutOutputCoverageGate:assert requested_scalar_output_extraction_plan(ctx) is None+ def test_pathless_runtime_output_criterion_reaching_enforcement_is_flagged(self) -> None:+ # A runtime-output criterion reaching enforcement with no identity is surfaced, not dropped.+ pathless = CompletionCriterion(+ id="c0",+ outcome="number of new signups is extracted for the past 7 days",+ output_path=None,+ requested_output_evidence_source="runtime_output",+ )+ flagged = pre_run_gated_outputs_without_path(self._authoring_ctx(pathless))+ assert [criterion.id for criterion in flagged] == ["c0"]++ with_path = _criterion("output.new_signups", "number of new signups is extracted")+ assert pre_run_gated_outputs_without_path(self._authoring_ctx(with_path)) == ()++ def test_all_generic_token_path_still_requires_producer_plan(self) -> None:+ ctx = self._authoring_ctx(_criterion("output.data", "the data is captured"))+ assert uncovered_requested_output_paths(ctx) == set()+ ctx.synthesized_block_offered_goal_complete = synthesized_trajectory_is_goal_complete(ctx)+ assert synthesized_trajectory_is_goal_complete(ctx) is False+def test_floor_rekeyed_runtime_output_stays_owed_until_grounded(self) -> None:# The rekey clears output_path but keeps floor_rekeyed_from_path; keyed only on the former,# both outputs vanish from the requested set and the blocker forecloses scouting.@@ -3752,22 +2814,30 @@ class TestScoutOutputCoverageGate:# Coverage keys on the outcome text, since the digest leaf carries no groundable tokens.assert uncovered_requested_output_paths(ctx) == {"output.request_slot_b97f_01"}ctx.synthesized_block_offered_goal_complete = synthesized_trajectory_is_goal_complete(ctx)- assert _should_block_mutating_tool_after_synthesized_offer(ctx, "click") is False- assert synthesized_block_persistence_signal(ctx, "click") is None- def test_pathless_runtime_output_criterion_reaching_enforcement_is_flagged(self) -> None:- # A runtime-output criterion reaching enforcement with no identity is surfaced, not dropped.- pathless = CompletionCriterion(- id="c0",- outcome="number of new signups is extracted for the past 7 days",- output_path=None,- requested_output_evidence_source="runtime_output",- )- flagged = pre_run_gated_outputs_without_path(self._authoring_ctx(pathless))- assert [criterion.id for criterion in flagged] == ["c0"]+ def test_uncovered_output_keeps_gate_open_and_admits_scout_tools(self) -> None:+ ctx = self._authoring_ctx(_criterion("output.document_name", "the order status document name is captured"))+ assert uncovered_requested_output_paths(ctx) == {"output.document_name"}+ assert synthesized_trajectory_is_goal_complete(ctx) is False- with_path = _criterion("output.new_signups", "number of new signups is extracted")- assert pre_run_gated_outputs_without_path(self._authoring_ctx(with_path)) == ()+ def test_uncovered_output_leaves_the_trajectory_goal_reaching(self) -> None:+ ctx = self._authoring_ctx(_criterion("output.document_name", "the order status document name is captured"))+ assert uncovered_requested_output_paths(ctx) == {"output.document_name"}+ assert synthesized_trajectory_reaches_goal(ctx) is True+ assert synthesized_trajectory_is_goal_complete(ctx) is False++ def test_value_bearing_container_coverage_without_plan_does_not_force(self) -> None:+ ctx = self._authoring_ctx(_criterion("output.document_name", "the order status document name is captured"))+ page_evidence = {+ "result_containers": [+ {"text_excerpt": "Document Name Resale Certificate 2024 for order 5591"},+ ]+ }+ record_scouted_output_coverage(ctx, page_evidence)+ assert ctx.scouted_output_covered_paths == {"output.document_name"}+ assert uncovered_requested_output_paths(ctx) == set()+ ctx.synthesized_block_offered_goal_complete = synthesized_trajectory_is_goal_complete(ctx)+ assert synthesized_trajectory_is_goal_complete(ctx) is Falseclass TestAdvisoryRunDispatchForceLane:@@ -3906,51 +2976,6 @@ class TestCredentialFlowGoalComplete:ctx.synthesized_block_offered_goal_complete = synthesized_trajectory_is_goal_complete(ctx)return ctx- def test_half_login_with_unobserved_second_screen_is_incomplete(self) -> None:- ctx = self._ctx_with_inventory(- self._two_screen_first_page(),- inventory={"cred_1": frozenset({"username", "password"})},- )- assert synthesized_trajectory_is_goal_complete(ctx) is False- assert _should_force_synthesized_block_persistence(ctx) is False- assert synthesized_block_persistence_signal(ctx, "evaluate") is None-- def test_full_login_with_post_fill_submit_is_complete(self) -> None:- ctx = self._ctx_with_inventory(- self._two_screen_full_login(),- inventory={"cred_1": frozenset({"username", "password"})},- )- assert synthesized_trajectory_is_goal_complete(ctx) is True- assert _should_force_synthesized_block_persistence(ctx) is True- assert synthesized_block_persistence_signal(ctx, "evaluate") is not None-- def test_login_only_is_incomplete_when_runtime_outputs_were_floor_rekeyed(- self, monkeypatch: pytest.MonkeyPatch- ) -> None:- ctx = self._ctx_with_inventory(- self._two_screen_full_login(),- inventory={"cred_1": frozenset({"username", "password"})},- )- ctx.completion_criteria_turn_state = _turn_state(- CompletionCriterion(- id="request-id",- outcome="the request id is output",- level="run",- requested_output_floor_rekeyed=True,- floor_rekeyed_from_path="output.request_id",- )- )-- assert synthesized_trajectory_is_goal_complete(ctx) is False- assert _should_force_synthesized_block_persistence(ctx) is False- assert synthesized_block_persistence_signal(ctx, "click", {"selector": "#gasCreate"}) is None- ctx.synthesized_block_offered = False- monkeypatch.setattr(- "skyvern.forge.sdk.copilot.enforcement.synthesize_code_block",- lambda *args, **kwargs: SynthesizedCodeBlock(code="await page.locator('#login').click()"),- )- assert _maybe_synthesized_block_offer_msg(ctx) is None-def test_floor_rekeyed_runtime_output_requires_coverage_after_post_login_business_commit(self) -> None:trajectory = [*self._two_screen_full_login(),@@ -4024,54 +3049,6 @@ class TestCredentialFlowGoalComplete:assert uncovered_requested_output_paths(ctx) == set()assert synthesized_trajectory_is_goal_complete(ctx) is True- def test_floor_rekeyed_runtime_output_rejects_create_then_submit_without_business_fill(self) -> None:- trajectory = [- *self._two_screen_full_login(),- {- "tool_name": "click",- "selector": "button[data-action='gasCreate']",- "accessible_name": "Create QuickConnect",- "source_url": "https://portal.example.test/home",- },- {- "tool_name": "click",- "selector": "button[data-action='gasSubmit']",- "accessible_name": "Submit",- "source_url": "https://portal.example.test/quickconnect",- },- ]- ctx = self._ctx_with_inventory(- trajectory,- inventory={"cred_1": frozenset({"username", "password"})},- )- ctx.completion_criteria_turn_state = _turn_state(- CompletionCriterion(- id="request-id",- outcome="the request id is output",- level="run",- requested_output_floor_rekeyed=True,- floor_rekeyed_from_path="output.request_id",- )- )- ctx.request_policy = RequestPolicy(- completion_criteria=[- CompletionCriterion(- id="submit-request",- outcome="the QuickConnect request is submitted",- kind="terminal_action",- terminal_action_family="request",- level="run",- )- ]- )- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = len(trajectory)- ctx.synthesized_block_offered_goal_complete = True-- assert synthesized_trajectory_is_goal_complete(ctx) is False- assert _should_force_synthesized_block_persistence(ctx) is False- assert synthesized_block_persistence_signal(ctx, "evaluate") is None-def test_request_terminal_action_does_not_offer_on_create_then_table_navigation(self, monkeypatch: pytest.MonkeyPatch) -> None:@@ -4120,17 +3097,6 @@ class TestCredentialFlowGoalComplete:)assert _maybe_synthesized_block_offer_msg(ctx) is None- def test_username_only_flow_completes_after_no_password_control_observation(self) -> None:- trajectory = self._two_screen_first_page()- ctx = self._ctx_with_inventory(- trajectory,- inventory={"cred_1": frozenset({"username", "password"})},- observed_at_index=len(trajectory) - 1,- observed_password_control=False,- )- assert synthesized_trajectory_is_goal_complete(ctx) is True- assert _should_force_synthesized_block_persistence(ctx) is True-def test_observed_password_screen_keeps_flow_incomplete(self) -> None:trajectory = self._two_screen_first_page()ctx = self._ctx_with_inventory(@@ -4273,22 +3239,100 @@ class TestCredentialFlowGoalComplete:)assert synthesized_trajectory_is_goal_complete(ctx) is False- def test_mutating_tools_admitted_while_credential_flow_incomplete(self) -> None:+ def test_floor_rekeyed_runtime_output_rejects_create_then_submit_without_business_fill(self) -> None:+ trajectory = [+ *self._two_screen_full_login(),+ {+ "tool_name": "click",+ "selector": "button[data-action='gasCreate']",+ "accessible_name": "Create QuickConnect",+ "source_url": "https://portal.example.test/home",+ },+ {+ "tool_name": "click",+ "selector": "button[data-action='gasSubmit']",+ "accessible_name": "Submit",+ "source_url": "https://portal.example.test/quickconnect",+ },+ ]+ ctx = self._ctx_with_inventory(+ trajectory,+ inventory={"cred_1": frozenset({"username", "password"})},+ )+ ctx.completion_criteria_turn_state = _turn_state(+ CompletionCriterion(+ id="request-id",+ outcome="the request id is output",+ level="run",+ requested_output_floor_rekeyed=True,+ floor_rekeyed_from_path="output.request_id",+ )+ )+ ctx.request_policy = RequestPolicy(+ completion_criteria=[+ CompletionCriterion(+ id="submit-request",+ outcome="the QuickConnect request is submitted",+ kind="terminal_action",+ terminal_action_family="request",+ level="run",+ )+ ]+ )+ ctx.synthesized_block_offered = True+ ctx.synthesized_block_offered_trajectory_len = len(trajectory)+ ctx.synthesized_block_offered_goal_complete = True++ assert synthesized_trajectory_is_goal_complete(ctx) is False++ def test_full_login_with_post_fill_submit_is_complete(self) -> None:+ ctx = self._ctx_with_inventory(+ self._two_screen_full_login(),+ inventory={"cred_1": frozenset({"username", "password"})},+ )+ assert synthesized_trajectory_is_goal_complete(ctx) is True++ def test_half_login_with_unobserved_second_screen_is_incomplete(self) -> None:ctx = self._ctx_with_inventory(self._two_screen_first_page(),inventory={"cred_1": frozenset({"username", "password"})},)- assert _should_block_mutating_tool_after_synthesized_offer(ctx, "click") is False- assert synthesized_block_persistence_signal(ctx, "click") is None- assert synthesized_block_persistence_signal(ctx, "type_text") is None+ assert synthesized_trajectory_is_goal_complete(ctx) is False- def test_mutating_tools_blocked_again_once_flow_completes(self) -> None:+ def test_login_only_is_incomplete_when_runtime_outputs_were_floor_rekeyed(+ self, monkeypatch: pytest.MonkeyPatch+ ) -> None:ctx = self._ctx_with_inventory(self._two_screen_full_login(),inventory={"cred_1": frozenset({"username", "password"})},)- assert _should_block_mutating_tool_after_synthesized_offer(ctx, "click") is True- assert synthesized_block_persistence_signal(ctx, "click") is not None+ ctx.completion_criteria_turn_state = _turn_state(+ CompletionCriterion(+ id="request-id",+ outcome="the request id is output",+ level="run",+ requested_output_floor_rekeyed=True,+ floor_rekeyed_from_path="output.request_id",+ )+ )++ assert synthesized_trajectory_is_goal_complete(ctx) is False+ ctx.synthesized_block_offered = False+ monkeypatch.setattr(+ "skyvern.forge.sdk.copilot.enforcement.synthesize_code_block",+ lambda *args, **kwargs: SynthesizedCodeBlock(code="await page.locator('#login').click()"),+ )+ assert _maybe_synthesized_block_offer_msg(ctx) is None++ def test_username_only_flow_completes_after_no_password_control_observation(self) -> None:+ trajectory = self._two_screen_first_page()+ ctx = self._ctx_with_inventory(+ trajectory,+ inventory={"cred_1": frozenset({"username", "password"})},+ observed_at_index=len(trajectory) - 1,+ observed_password_control=False,+ )+ assert synthesized_trajectory_is_goal_complete(ctx) is Trueclass TestCredentialScoutReopen:@@ -4313,29 +3357,6 @@ class TestCredentialScoutReopen:assert arm_credential_scout_reopen(ctx, "identity-2") is Trueassert ctx.synthesized_block_reopened_for_credential_scout is True- def test_reopen_admits_evaluate_while_offer_is_goal_complete(self) -> None:- ctx = self._offered_complete_ctx()- assert synthesized_block_persistence_signal(ctx, "evaluate") is not None- ctx.synthesized_block_reopened_for_credential_scout = True- assert synthesized_block_persistence_signal(ctx, "evaluate") is None-- def test_reopen_admission_registers_precedence_claim(self) -> None:- ctx = self._offered_complete_ctx()- ctx.synthesized_block_reopened_for_credential_scout = True-- assert synthesized_block_persistence_signal(ctx, "evaluate") is None-- assert ctx.turn_ownership is not None- assert TurnClaimant.CREDENTIAL_SCOUT_REOPEN in ctx.turn_ownership.claims- assert current_turn_owner(ctx) is None-- def test_reopen_reopens_offer_refresh_window(self) -> None:- ctx = self._offered_complete_ctx()- ctx.update_workflow_called = True- assert _should_force_synthesized_block_persistence(ctx) is False- ctx.synthesized_block_reopened_for_credential_scout = True- assert _should_force_synthesized_block_persistence(ctx) is True-def _deadline_ctx() -> SimpleNamespace:return SimpleNamespace(diff --git a/tests/unit/test_copilot_fill_credential_field.py b/tests/unit/test_copilot_fill_credential_field.pyindex 80e1e42cc..9d9cb9b95 100644--- a/tests/unit/test_copilot_fill_credential_field.py+++ b/tests/unit/test_copilot_fill_credential_field.py@@ -9,6 +9,7 @@ from __future__ import annotationsimport jsonfrom contextlib import asynccontextmanager+from datetime import UTC, datetimefrom types import SimpleNamespacefrom typing import Any, AsyncIteratorfrom unittest.mock import AsyncMock, patch@@ -20,11 +21,12 @@ from skyvern.forge import appfrom skyvern.forge.sdk.copilot import tools as tools_modulefrom skyvern.forge.sdk.copilot.build_phase import _BROWSER_PRIMITIVE_TOOLSfrom skyvern.forge.sdk.copilot.config import BlockAuthoringPolicy-from skyvern.forge.sdk.copilot.request_policy import RequestPolicy+from skyvern.forge.sdk.copilot.request_policy import RequestPolicy, _ground_user_provided_sitesfrom skyvern.forge.sdk.copilot.tools import credential_fill as credential_fill_modulefrom skyvern.forge.sdk.copilot.tools import mcp_hooks as mcp_hooks_modulefrom skyvern.forge.sdk.copilot.tools import scouting as scouting_modulefrom skyvern.forge.sdk.schemas.credentials import CredentialType, CredentialVaultType, PasswordCredential, TotpType+from skyvern.forge.sdk.schemas.workflow_copilot import WorkflowCopilotChatHistoryMessage, WorkflowCopilotChatSender_FAKE_PASSWORD = "fake-test-password-7x9"_FAKE_USERNAME = "[email protected]"@@ -682,7 +684,7 @@ class TestCredentialFillLivePageAdmission:assert page.fill_calls == []@pytest.mark.asyncio- async def test_resolved_credential_without_an_intended_origin_fails_closed(+ async def test_resolved_credential_without_a_user_provided_site_fails_closed(self, monkeypatch: pytest.MonkeyPatch) -> None:"""Mutation guard: changing missing-origin denial back to a skip must make this fail."""@@ -693,103 +695,121 @@ class TestCredentialFillLivePageAdmission:result = await tools_module._fill_credential_field_impl(ctx, "#passwordInput", "cred_123", "password")assert result["ok"] is False- assert "intended login origin" in result["error"]+ assert "cannot be filled" in result["error"]assert page.fill_calls == []async def _unbound_grant(self,*,page_url: str | None,- login_page_urls: list[str],- named: bool,- org_credentials: list[SimpleNamespace],+ user_urls: list[str] | None = None,+ user_text: str = "",+ named: bool = False,+ also_resolved: list[str] | None = None,+ org_credentials: list[SimpleNamespace] | None = None,) -> tuple[Any, str | None]:"""Grant for a resolved credential carrying no saved login URL."""- policy = RequestPolicy(- resolved_credentials=[_resolved_credential(tested_url=None)],- login_page_urls=login_page_urls,- grounded_login_target_urls=login_page_urls,- )+ resolved: list[Any] = [_resolved_credential(tested_url=None)]+ resolved.extend(SimpleNamespace(credential_id=extra, tested_url=None) for extra in also_resolved or [])+ policy = RequestPolicy(resolved_credentials=resolved)+ policy.user_provided_site_urls = list(user_urls or [])if named:policy.current_turn_named_credential_ids = {"cred_123"}ctx = _ctx(request_policy=policy)with (patch("skyvern.forge.app.DATABASE.credentials.get_credentials",- new=AsyncMock(return_value=org_credentials),+ new=AsyncMock(return_value=org_credentials or []),),patch.object(credential_fill_module, "_live_working_page_url", AsyncMock(return_value=page_url)),):return await credential_fill_module._credential_fill_origin_grant(ctx, "cred_123")@pytest.mark.asyncio- async def test_a_named_credential_is_bound_by_the_login_page_the_request_asked_for(self) -> None:- """Naming a credential answers *which* one; the login page the request named answers *where*.-- A credential saved without a login URL is unbound rather than disowned, so the request's own- login target can supply the origin once something outside the model has settled which- credential is meant.- """+ async def test_a_site_the_user_pasted_releases_the_named_credential(self) -> None:grant, error = await self._unbound_grant(page_url=_FIXTURE_LOGIN_URL,- login_page_urls=[_FIXTURE_LOGIN_URL],+ user_urls=[_FIXTURE_LOGIN_URL],named=True,- org_credentials=[_org_credential("cred_123", "authtest simple", None)],)assert error is None- assert grant is not None- assert credential_fill_module._still_on_admitted_site(_FIXTURE_LOGIN_URL, grant.intended_url)+ assert grant is not None and grant.whole_site@pytest.mark.asyncio- async def test_the_only_saved_credential_needs_no_naming(self) -> None:- """An org with one login has nothing to disambiguate, so elimination answers for the user."""+ async def test_a_site_the_user_pasted_in_an_earlier_turn_still_releases(self) -> None:+ """The production dead end: the user pasted the URL one turn after naming the credential; a+ user-provided site now counts from any message of the chat."""grant, error = await self._unbound_grant(- page_url=_FIXTURE_LOGIN_URL,- login_page_urls=[_FIXTURE_LOGIN_URL],- named=False,- org_credentials=[_org_credential("cred_123", "authtest simple", None)],+ page_url="https://us.pathfold.com/login",+ user_urls=["https://us.pathfold.com/project/1234/dashboard/5678"],)assert error is Noneassert grant is not None@pytest.mark.asyncio- async def test_a_card_or_secret_does_not_count_against_the_only_saved_login(self) -> None:- """Elimination counts logins, so a saved card leaves the one password still unambiguous."""+ async def test_a_site_the_user_never_provided_refuses_and_names_the_page(self) -> None:grant, error = await self._unbound_grant(- page_url=_FIXTURE_LOGIN_URL,- login_page_urls=[_FIXTURE_LOGIN_URL],- named=False,- org_credentials=[- _org_credential("cred_123", "authtest simple", None),- _org_credential("cred_card", "company card", None, CredentialType.CREDIT_CARD),- ],+ page_url="https://evil.example.net/login",+ user_urls=["https://us.pathfold.com/"],+ user_text="log into pathfold",+ named=True,+ )++ assert grant is None+ assert error is not None+ assert "has not named this site" in error++ @pytest.mark.asyncio+ async def test_a_lookalike_domain_does_not_match_the_user_site(self) -> None:+ grant, error = await self._unbound_grant(+ page_url="https://authenticationtest.com.example.net/simpleFormAuth/",+ user_urls=[_FIXTURE_LOGIN_URL],+ named=True,+ )++ assert grant is None+ assert error is not None++ @pytest.mark.asyncio+ async def test_either_of_two_user_provided_sites_releases(self) -> None:+ """Reversal of the old sole-origin rule: both sites are the user's own words, so standing on+ either releases; a site the user never gave still refuses."""+ for page_url in (_FIXTURE_LOGIN_URL, "https://tracker-b.example/login"):+ grant, error = await self._unbound_grant(+ page_url=page_url,+ user_urls=[_FIXTURE_LOGIN_URL, "https://tracker-b.example/login"],+ named=True,+ )++ assert error is None, page_url+ assert grant is not None++ @pytest.mark.asyncio+ async def test_a_localhost_site_the_user_pasted_releases_origin_scoped(self) -> None:+ """No public-suffix site exists for localhost/internal hosts; the exact origin the user+ pasted still releases, scoped to that origin."""+ grant, error = await self._unbound_grant(+ page_url="http://localhost:8901/analytics_console/pathfold/",+ user_urls=["http://localhost:8901/analytics_console/pathfold/?date_from=-7d"],+ named=True,)assert error is None- assert grant is not None+ assert grant is not None and not grant.whole_site@pytest.mark.asyncio- async def test_naming_several_credentials_settles_which_one_no_better_than_naming_none(self) -> None:- """Two names leave the model choosing whose password reaches the page, which is not an answer."""+ async def test_a_login_target_the_user_never_wrote_cannot_vouch(self) -> None:+ """The classifier (or any model) does not get to author the site a password reaches."""policy = RequestPolicy(resolved_credentials=[_resolved_credential(tested_url=None)],login_page_urls=[_FIXTURE_LOGIN_URL],- grounded_login_target_urls=[_FIXTURE_LOGIN_URL],)- policy.current_turn_named_credential_ids = {"cred_123", "cred_other"}+ policy.current_turn_named_credential_ids = {"cred_123"}ctx = _ctx(request_policy=policy)with (- patch(- "skyvern.forge.app.DATABASE.credentials.get_credentials",- new=AsyncMock(- return_value=[- _org_credential("cred_123", "authtest simple", None),- _org_credential("cred_other", "billing", None),- ]- ),- ),+ patch("skyvern.forge.app.DATABASE.credentials.get_credentials", new=AsyncMock(return_value=[])),patch.object(credential_fill_module, "_live_working_page_url", AsyncMock(return_value=_FIXTURE_LOGIN_URL)),):grant, error = await credential_fill_module._credential_fill_origin_grant(ctx, "cred_123")@@ -798,32 +818,25 @@ class TestCredentialFillLivePageAdmission:assert error is not None@pytest.mark.asyncio- async def test_a_credential_already_bound_elsewhere_leaves_the_unbound_one_sole(self) -> None:- """Turn start resolves the sole URL-less credential; counting bound ones too would re-ask here."""+ async def test_the_sole_resolved_credential_needs_no_renaming(self) -> None:+ """Never re-ask what's already answered: one credential resolved for the request (e.g. the+ card answer, carried) is settled even when this turn's message never names it."""grant, error = await self._unbound_grant(page_url=_FIXTURE_LOGIN_URL,- login_page_urls=[_FIXTURE_LOGIN_URL],+ user_urls=[_FIXTURE_LOGIN_URL],named=False,- org_credentials=[- _org_credential("cred_123", "authtest simple", None),- _org_credential("cred_bound", "billing", "https://billing.example.com/login"),- ],)assert error is Noneassert grant is not None@pytest.mark.asyncio- async def test_one_unnamed_credential_among_several_asks_rather_than_refusing(self) -> None:- """Ambiguity is a question. Only an absence of any non-model evidence is a refusal."""+ async def test_two_resolved_credentials_with_none_named_ask_rather_than_guess(self) -> None:grant, error = await self._unbound_grant(page_url=_FIXTURE_LOGIN_URL,- login_page_urls=[_FIXTURE_LOGIN_URL],+ user_urls=[_FIXTURE_LOGIN_URL],named=False,- org_credentials=[- _org_credential("cred_123", "authtest simple", None),- _org_credential("cred_other", "billing", None),- ],+ also_resolved=["cred_other"],)assert grant is None@@ -833,57 +846,68 @@ class TestCredentialFillLivePageAdmission:assert "exact name" in error and "cred_" in error@pytest.mark.asyncio- async def test_an_unbound_credential_is_refused_on_a_page_the_request_never_asked_for(self) -> None:- """The lookalike case: naming a credential does not license the page the browser drifted to."""+ async def test_naming_this_turn_settles_among_several_resolved(self) -> None:grant, error = await self._unbound_grant(- page_url="https://authenticationtest.com.example.net/simpleFormAuth/",- login_page_urls=[_FIXTURE_LOGIN_URL],+ page_url=_FIXTURE_LOGIN_URL,+ user_urls=[_FIXTURE_LOGIN_URL],named=True,- org_credentials=[_org_credential("cred_123", "authtest simple", None)],+ also_resolved=["cred_other"],)- assert grant is None- assert error is not None- assert "intended login origin" in error+ assert error is None+ assert grant is not None@pytest.mark.asyncio- async def test_a_request_naming_two_sign_in_sites_grounds_neither(self) -> None:- """One request routinely spans two logins — read a total here, file it there.-- Neither site can vouch for the other's password, and nothing at this seam knows which of them- the credential belongs to, so a second sign-in target withdraws the grant instead of widening it.- """- for page_url in (_FIXTURE_LOGIN_URL, "https://tracker-b.example/login"):- grant, error = await self._unbound_grant(- page_url=page_url,- login_page_urls=[_FIXTURE_LOGIN_URL, "https://tracker-b.example/login"],- named=True,- org_credentials=[_org_credential("cred_123", "authtest simple", None)],- )+ async def test_the_only_saved_org_password_settles_by_elimination(self) -> None:+ grant, error = await self._unbound_grant(+ page_url=_FIXTURE_LOGIN_URL,+ user_urls=[_FIXTURE_LOGIN_URL],+ named=False,+ also_resolved=["cred_other"],+ org_credentials=[+ _org_credential("cred_123", "authtest simple", None),+ _org_credential("cred_card", "company card", None, CredentialType.CREDIT_CARD),+ ],+ )- assert grant is None, f"a two-site request must not vouch for {page_url}"- assert error is not None- assert "intended login origin" in error+ assert error is None+ assert grant is not None@pytest.mark.asyncio- async def test_a_login_target_the_user_never_wrote_cannot_vouch(self) -> None:- """The classifier says which URL is a sign-in page; it does not get to author the URL."""- policy = RequestPolicy(- resolved_credentials=[_resolved_credential(tested_url=None)],- login_page_urls=[_FIXTURE_LOGIN_URL],- grounded_login_target_urls=[],+ async def test_the_production_transcript_reaches_a_grant(self) -> None:+ """Acceptance: prose ask -> card answer (sole resolved, carried) -> URL reply."""+ policy = RequestPolicy(resolved_credentials=[_resolved_credential(tested_url=None)])+ history = [+ WorkflowCopilotChatHistoryMessage(+ sender=WorkflowCopilotChatSender.USER,+ content=(+ "cred_123\n\ncan you use this credential to log into the pathfold website and tell me "+ "how many website visitors skyvern got in the past 7 days?"+ ),+ created_at=datetime.now(UTC),+ ),+ WorkflowCopilotChatHistoryMessage(+ sender=WorkflowCopilotChatSender.AI,+ content="I created a draft workflow with 1 block and tested it, but the test failed.",+ created_at=datetime.now(UTC),+ ),+ ]+ _ground_user_provided_sites(+ policy, "here ist he url: https://us.pathfold.com/project/1234/dashboard/5678", history)- policy.current_turn_named_credential_ids = {"cred_123"}ctx = _ctx(request_policy=policy)with (patch("skyvern.forge.app.DATABASE.credentials.get_credentials", new=AsyncMock(return_value=[])),- patch.object(credential_fill_module, "_live_working_page_url", AsyncMock(return_value=_FIXTURE_LOGIN_URL)),+ patch.object(+ credential_fill_module,+ "_live_working_page_url",+ AsyncMock(return_value="https://us.pathfold.com/login"),+ ),):grant, error = await credential_fill_module._credential_fill_origin_grant(ctx, "cred_123")- assert grant is None- assert error is not None- assert "intended login origin" in error+ assert error is None+ assert grant is not None@pytest.mark.asyncioasync def test_live_page_admitted_credential_short_circuits_without_an_org_lookup(self) -> None:diff --git a/tests/unit/test_copilot_frontier.py b/tests/unit/test_copilot_frontier.pyindex 3050cdb2b..4aafc3016 100644--- a/tests/unit/test_copilot_frontier.py+++ b/tests/unit/test_copilot_frontier.py@@ -13,9 +13,7 @@ from skyvern.forge.sdk.copilot.build_test_outcome import RecordedBuildTestOutcomfrom skyvern.forge.sdk.copilot.context import CopilotContextfrom skyvern.forge.sdk.copilot.enforcement import (MAX_FAILED_TEST_NUDGES,- POST_REPEATED_FRONTIER_FAILURE_STOP_NUDGE,- POST_REPEATED_FRONTIER_FAILURE_WARN_NUDGE,- _check_enforcement,+ enforcement_decision,)from skyvern.forge.sdk.copilot.failure_tracking import (compute_failure_signature,@@ -1378,18 +1376,18 @@ def test_repeated_failure_end_to_end_flow_1_then_warn_then_stop() -> None:update_repeated_failure_state(ctx, result)assert ctx.repeated_failure_streak_count == 1- assert _check_enforcement(ctx) is None+ assert enforcement_decision(ctx) is Noneupdate_repeated_failure_state(ctx, result)assert ctx.repeated_failure_streak_count == 2- assert _check_enforcement(ctx) == POST_REPEATED_FRONTIER_FAILURE_WARN_NUDGE+ assert enforcement_decision(ctx).rule == "post_repeated_frontier_failure_warn"assert ctx.repeated_failure_streak_count == 2- assert _check_enforcement(ctx) != POST_REPEATED_FRONTIER_FAILURE_WARN_NUDGE+ assert enforcement_decision(ctx) is Noneupdate_repeated_failure_state(ctx, result)assert ctx.repeated_failure_streak_count == 3- assert _check_enforcement(ctx) == POST_REPEATED_FRONTIER_FAILURE_STOP_NUDGE- assert _check_enforcement(ctx) != POST_REPEATED_FRONTIER_FAILURE_STOP_NUDGE+ assert enforcement_decision(ctx).rule == "post_repeated_frontier_failure_stop"+ assert enforcement_decision(ctx) is Nonedef test_compute_failure_signature_none_on_clean_success() -> None:diff --git a/tests/unit/test_copilot_loop_detection_blocker.py b/tests/unit/test_copilot_loop_detection_blocker.pyindex 22d56c1f4..d8bfadab9 100644--- a/tests/unit/test_copilot_loop_detection_blocker.py+++ b/tests/unit/test_copilot_loop_detection_blocker.py@@ -13,11 +13,9 @@ from skyvern.forge.sdk.copilot.blocker_signal import (assert_clean_user_facing_text,loop_blocker_evidence_from_ctx,)-from skyvern.forge.sdk.copilot.config import BlockAuthoringPolicyfrom skyvern.forge.sdk.copilot.context import CopilotContextfrom skyvern.forge.sdk.copilot.enforcement import (CURRENT_PAGE_CHALLENGE_ADVISORY_REASON_CODE,- SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE,)from skyvern.forge.sdk.copilot.loop_detection import tool_step_identityfrom skyvern.forge.sdk.copilot.mcp_adapter import SchemaOverlay, SkyvernOverlayMCPServer, _stash_and_emit_loop_blocker@@ -25,7 +23,6 @@ from skyvern.forge.sdk.copilot.output_policy import CopilotOutputKind, evaluate_from skyvern.forge.sdk.copilot.run_outcome import RecordedRunOutcomefrom skyvern.forge.sdk.copilot.tools import _build_loop_blocker_signal, _tool_loop_errorfrom skyvern.forge.sdk.copilot.tools.mcp_hooks import get_skyvern_mcp_alias_map-from skyvern.forge.sdk.copilot.turn_intent import TurnIntent, TurnIntentAuthority, TurnIntentMode_LEAK_TOKENS = ("safe_reason_code", "LOOP DETECTED:")@@ -431,51 +428,6 @@ async def test_mcp_browser_tool_executes_after_the_challenge_advisory_was_deliveassert ctx.turn_halt is None-async def test_mcp_synthesized_offer_gate_preempts_evaluate_loop_without_terminal_halt() -> None:- ctx = _ctx(consecutive_tool_tracker=_streak("evaluate", {"expression": "document.body.innerText"}))- ctx.turn_intent = TurnIntent(- mode=TurnIntentMode.BUILD,- authority=TurnIntentAuthority(may_update_workflow=True, may_run_blocks=True),- )- ctx.block_authoring_policy = BlockAuthoringPolicy.CODE_ONLY_BROWSER- ctx.synthesized_block_offered = True- ctx.synthesized_block_offered_trajectory_len = 2- ctx.synthesized_block_offered_goal_complete = True- ctx.scout_trajectory = [- {"tool_name": "type_text", "selector": "input[name='q']", "accessible_name": "Search"},- {"tool_name": "click", "selector": "button[data-action='search']", "accessible_name": "Search"},- ]- ctx.reached_download_target = None- ctx.update_workflow_called = False-- class _UnexpectedClient:- async def call_tool(self, name: str, args: dict, raise_on_error: bool = False) -> object:- raise AssertionError("persistence gate should skip MCP execution")-- server = SkyvernOverlayMCPServer(- transport=None,- overlays={},- alias_map={},- allowlist=frozenset({"evaluate"}),- context_provider=lambda: ctx,- )- server._client = _UnexpectedClient() # type: ignore[assignment]-- result = await server.call_tool("evaluate", {"expression": "document.body.innerText"})-- parsed = json.loads(result.content[0].text)- assert result.isError is True- assert parsed["ok"] is False- signal = ctx.blocker_signal- assert isinstance(signal, CopilotToolBlockerSignal)- assert signal.internal_reason_code == SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE- assert signal.blocked_tool == "evaluate"- assert signal.cleared_by_tools == frozenset({"update_and_run_blocks"})- assert signal.renders_final_reply is False- assert ctx.turn_halt is None--def test_tool_loop_error_entry_refreshes_stale_held_loop_signal() -> None:ctx = _ctx()stale = _build_loop_blocker_signal(_BRANCH_MESSAGES["loop_detected_consecutive_same_tool"], tool_name="evaluate")diff --git a/tests/unit/test_copilot_non_retriable_nav.py b/tests/unit/test_copilot_non_retriable_nav.pyindex 61e62d4a1..11ef42960 100644--- a/tests/unit/test_copilot_non_retriable_nav.py+++ b/tests/unit/test_copilot_non_retriable_nav.py@@ -16,15 +16,14 @@ import pytestfrom skyvern.config import settingsfrom skyvern.forge.sdk.copilot.context import CopilotContextfrom skyvern.forge.sdk.copilot.enforcement import (- POST_NON_RETRIABLE_NAV_ERROR_STOP_NUDGE,CopilotNonRetriableNavError,- _check_enforcement,_extract_url_from_nav_error,_maybe_raise_non_retriable_nav,_needs_failed_test_nudge,_needs_suspicious_success_nudge,_non_retriable_nav_error_nudge,_repeated_frontier_failure_nudge,+ enforcement_decision,)from skyvern.forge.sdk.copilot.tools import (_detect_non_retriable_nav_error,@@ -264,8 +263,8 @@ def test_nudge_helper_fires_first_time() -> None:ctx.last_test_non_retriable_nav_error = _DNS_FAILURE_REASONresult = _non_retriable_nav_error_nudge(ctx)assert result is not None- nudge, signature = result- assert nudge == POST_NON_RETRIABLE_NAV_ERROR_STOP_NUDGE+ rule, signature = result+ assert rule == "post_non_retriable_nav_error_stop"assert signature # non-empty@@ -299,11 +298,11 @@ def test_nudge_helper_returns_none_when_flag_unset() -> None:# ----------------------------------------------------------------------------# _check_enforcement — branch ordering and latching+# enforcement_decision — branch ordering and latching# ----------------------------------------------------------------------------def test_check_enforcement_returns_non_retriable_nudge_before_failed_test() -> None:+def test_enforcement_decision_returns_non_retriable_nudge_before_failed_test() -> None:# Conditions that would normally trigger POST_FAILED_TEST_NUDGE (last_test# failed, test_after_update_done=True) must yield the non-retriable stop# nudge instead when the flag is set.@@ -312,27 +311,27 @@ def test_check_enforcement_returns_non_retriable_nudge_before_failed_test() -> Nctx.last_test_ok = Falsectx.last_test_failure_reason = _DNS_FAILURE_REASONctx.last_test_non_retriable_nav_error = _DNS_FAILURE_REASON- nudge = _check_enforcement(ctx)- assert nudge == POST_NON_RETRIABLE_NAV_ERROR_STOP_NUDGE+ nudge = enforcement_decision(ctx)+ assert nudge.rule == "post_non_retriable_nav_error_stop"# Emission latched for future calls with same signature.assert ctx.non_retriable_nav_error_last_emitted_signature-def test_check_enforcement_one_shot_per_signature() -> None:+def test_enforcement_decision_one_shot_per_signature() -> None:ctx = _fresh_context()ctx.test_after_update_done = Truectx.last_test_ok = Falsectx.last_test_failure_reason = _DNS_FAILURE_REASONctx.last_test_non_retriable_nav_error = _DNS_FAILURE_REASON- first = _check_enforcement(ctx)- assert first == POST_NON_RETRIABLE_NAV_ERROR_STOP_NUDGE+ first = enforcement_decision(ctx)+ assert first.rule == "post_non_retriable_nav_error_stop"# Same signature on next iteration: nudge returns None (and does NOT# fall through to POST_FAILED_TEST_NUDGE thanks to competing-branch# suppression).- assert _check_enforcement(ctx) is None+ assert enforcement_decision(ctx) is None-def test_check_enforcement_pre_empts_post_navigate_nudge() -> None:+def test_enforcement_decision_pre_empts_post_navigate_nudge() -> None:# Codex review P2-1: a navigate-hygiene state (navigate_called=True,# observation_after_navigate=False) must not steal the nudge slot when# a non-retriable nav error is also present — POST_NAVIGATE_NUDGE tells@@ -343,10 +342,10 @@ def test_check_enforcement_pre_empts_post_navigate_nudge() -> None:ctx.observation_after_navigate = Falsectx.navigate_enforcement_done = Falsectx.last_test_non_retriable_nav_error = _DNS_FAILURE_REASON- assert _check_enforcement(ctx) == POST_NON_RETRIABLE_NAV_ERROR_STOP_NUDGE+ assert enforcement_decision(ctx).rule == "post_non_retriable_nav_error_stop"-def test_check_enforcement_pre_empts_post_update_nudge() -> None:+def test_enforcement_decision_pre_empts_post_update_nudge() -> None:# Similar P2-1 case: an update-without-test state must not pre-empt the# terminal stop. In practice P2-2 clears the flag on update, but if a# race leaves the flag set, stop-nudge still wins.@@ -354,7 +353,7 @@ def test_check_enforcement_pre_empts_post_update_nudge() -> None:ctx.update_workflow_called = Truectx.test_after_update_done = Falsectx.last_test_non_retriable_nav_error = _DNS_FAILURE_REASON- assert _check_enforcement(ctx) == POST_NON_RETRIABLE_NAV_ERROR_STOP_NUDGE+ assert enforcement_decision(ctx).rule == "post_non_retriable_nav_error_stop"# ---------------------------------------------------------------------------@@ -489,11 +488,11 @@ def test_full_flow_record_then_check_then_raise() -> None:},)# Enforcement fires the stop nudge.- nudge = _check_enforcement(ctx)- assert nudge == POST_NON_RETRIABLE_NAV_ERROR_STOP_NUDGE+ nudge = enforcement_decision(ctx)+ assert nudge.rule == "post_non_retriable_nav_error_stop"# Same signature on next iteration: no nudge, and the exit-path guard# raises because last_test_ok is still False.- assert _check_enforcement(ctx) is None+ assert enforcement_decision(ctx) is Nonewith pytest.raises(CopilotNonRetriableNavError):_maybe_raise_non_retriable_nav(ctx)@@ -505,7 +504,7 @@ def test_full_flow_cleared_after_successful_run() -> None:ctx,{"ok": False, "data": {"blocks": [{"failure_reason": _DNS_FAILURE_REASON}]}},)- assert _check_enforcement(ctx) == POST_NON_RETRIABLE_NAV_ERROR_STOP_NUDGE+ assert enforcement_decision(ctx).rule == "post_non_retriable_nav_error_stop"# Then a real success happens._record_run_blocks_result(ctx,diff --git a/tests/unit/test_copilot_output_policy.py b/tests/unit/test_copilot_output_policy.pyindex e7f8cddc3..945d2bb73 100644--- a/tests/unit/test_copilot_output_policy.py+++ b/tests/unit/test_copilot_output_policy.py@@ -24,7 +24,6 @@ from skyvern.forge.sdk.copilot.blocker_signal import CopilotToolBlockerSignalfrom skyvern.forge.sdk.copilot.build_phase import BuildPhasefrom skyvern.forge.sdk.copilot.context import CopilotContextfrom skyvern.forge.sdk.copilot.enforcement import (- PRESENT_COMPLETION_CONTRACT_ASK_RETRY,_response_coverage_nudge,)from skyvern.forge.sdk.copilot.loop_detection import tool_step_identity@@ -1241,24 +1240,6 @@ def test_allows_explicit_unresolved_credential_id_for_untested_draft() -> None:assert verdict.allowed-def test_rejects_credential_ref_the_user_did_not_state_even_for_untested_draft() -> None:- """A cred_ ref carried only in credential_refs (model-authored or negated) is not draft-bindable (SKY-13552)."""- verdict = evaluate_output_policy(- request_policy=_policy(- resolved_credentials=[],- credential_input_kind="credential_id",- credential_refs=["cred_carried"],- invalid_credential_ids=[],- allow_missing_credentials_in_draft=True,- allow_run_blocks=False,- ),- workflow_yaml=_workflow_yaml().replace("cred_safe", "cred_carried"),- )-- assert not verdict.allowed- assert OutputPolicyReason.UNAPPROVED_CREDENTIAL_REFERENCE in verdict.reason_codes--def test_rejects_unrequested_credential_id_even_when_untested_draft_allows_missing_credentials() -> None:verdict = evaluate_output_policy(request_policy=_policy(@@ -2993,7 +2974,7 @@ def test_seam_ordering_avoidable_ask_defers_then_recycle_fires() -> None:assert diagnostics["deferred_reason_codes"] == [OutputPolicyReason.AVOIDABLE_OUTPUT_FIELD_CONFIRMATION.value]assert diagnostics["final_output_policy_allowed"] is True- assert _response_coverage_nudge(ctx, _AVOIDABLE_ASK) == PRESENT_COMPLETION_CONTRACT_ASK_RETRY+ assert _response_coverage_nudge(ctx, _AVOIDABLE_ASK).rule == "present_completion_contract_ask_retry"@pytest.mark.asyncio@@ -3035,7 +3016,8 @@ def test_avoidable_deferral_iff_recycle_admits(**marker,)- recycle_admits = _response_coverage_nudge(ctx, _AVOIDABLE_ASK) == PRESENT_COMPLETION_CONTRACT_ASK_RETRY+ recycle_decision = _response_coverage_nudge(ctx, _AVOIDABLE_ASK)+ recycle_admits = recycle_decision is not None and recycle_decision.rule == "present_completion_contract_ask_retry"assert recycle_admits is expected_admit_, _, diagnostics = agent_module._evaluate_copilot_final_output_policy(ctx, _AVOIDABLE_ASK)diff --git a/tests/unit/test_copilot_page_evidence_policy.py b/tests/unit/test_copilot_page_evidence_policy.pyindex 6fe4a76d1..92b0de7a5 100644--- a/tests/unit/test_copilot_page_evidence_policy.py+++ b/tests/unit/test_copilot_page_evidence_policy.py@@ -28,15 +28,6 @@ def _render_agent_prompt() -> str:return prompt_engine.load_prompt("workflow-copilot-agent", **_AGENT_TEMPLATE_DEFAULTS)-def test_prompt_requires_evidence_grounded_form_and_result_composition() -> None:- rendered = _render_agent_prompt()-- assert "grounded in observed evidence" in rendered- assert "not one guessed site model" in rendered- assert "Do not pre-author row/detail expansion from the user's words alone" in rendered- assert "the expansion step also needs result-page evidence" in rendered--def test_agent_prompt_frames_inspection_as_build_time_context_not_workflow_shape_policy() -> None:rendered = _render_agent_prompt()@@ -53,45 +44,6 @@ def test_agent_prompt_frames_inspection_as_build_time_context_not_workflow_shapeassert "Before extraction on stateful search/result tasks" not in rendered-def test_agent_prompt_accounts_for_observed_challenges_without_blind_waits() -> None:- rendered = _render_agent_prompt()-- assert "anti-bot or challenge controls" in rendered- assert "Use `get_browser_screenshot` whenever seeing the rendered page answers the question" in rendered- assert "Prefer explicit challenge handling available to Skyvern over blind waits" in rendered- assert "report the observed blocker instead of retrying the same flow" in rendered- assert "challenge_state.gates_submit_controls=true" in rendered- assert "submit/search control disabled" in rendered---def test_agent_prompt_continues_from_observed_state_after_budgeted_runs() -> None:- rendered = _render_agent_prompt()-- assert "live browser page that advanced beyond the workflow's entrypoint" in rendered- assert "Do not turn the observed current page into a new workflow entrypoint" in rendered- assert "Preserve unchanged upstream labels and text" in rendered- assert "run only the missing downstream labels" in rendered---def test_agent_prompt_requires_result_page_evidence_before_expansion_blocks() -> None:- rendered = _render_agent_prompt()-- assert "Do not pre-author row/detail expansion from the user's words alone" in rendered- assert "Do not author or run an expansion block before you have reached or inspected the results page" in rendered- assert "Do not author downstream row expansion or extraction that depends on a future page state" in rendered- assert "add and run the expansion block before adding extraction" in rendered- assert "if the requested details are already visible" in rendered- assert "omit the expansion block and proceed to extraction from the visible results" in rendered---def test_agent_prompt_requires_details_evidence_label() -> None:- rendered = _render_agent_prompt()-- assert "details or profile page" in rendered- assert "visible details/profile page label" in rendered- assert "page label only as supporting evidence" in rendered--def test_tool_descriptions_ground_composition_without_prescribing_extra_workflow_blocks() -> None:for tool in (update_workflow_tool, run_blocks_tool, update_and_run_blocks_tool):desc = tool.description # type: ignore[attr-defined]diff --git a/tests/unit/test_copilot_parameter_binding.py b/tests/unit/test_copilot_parameter_binding.pyindex 00a2167f1..b1d93f8a9 100644--- a/tests/unit/test_copilot_parameter_binding.py+++ b/tests/unit/test_copilot_parameter_binding.py@@ -21,14 +21,10 @@ import pytestfrom skyvern.forge.failure_classifier import classify_from_failure_reasonfrom skyvern.forge.sdk.copilot.context import CopilotContextfrom skyvern.forge.sdk.copilot.enforcement import (- POST_PARAMETER_BINDING_STOP_NUDGE,- POST_PARAMETER_BINDING_WARN_NUDGE,- POST_REPEATED_FRONTIER_FAILURE_STOP_NUDGE,- POST_REPEATED_FRONTIER_FAILURE_WARN_NUDGE,REPEATED_FRONTIER_STREAK_ESCALATE_AT,REPEATED_FRONTIER_STREAK_STOP_AT,- _check_enforcement,_repeated_frontier_failure_nudge,+ enforcement_decision,)from skyvern.forge.sdk.copilot.failure_tracking import compute_failure_signaturefrom skyvern.forge.sdk.copilot.tools import _analyze_run_blocks, _parameter_binding_invariant_error@@ -274,28 +270,28 @@ def test_signature_success_returns_none() -> None:REPEATED_FRONTIER_STREAK_ESCALATE_AT,0,"PARAMETER_BINDING_ERROR",- POST_PARAMETER_BINDING_WARN_NUDGE,+ "post_parameter_binding_warn",id="warn_parameter_binding",),pytest.param(REPEATED_FRONTIER_STREAK_STOP_AT,REPEATED_FRONTIER_STREAK_ESCALATE_AT,"PARAMETER_BINDING_ERROR",- POST_PARAMETER_BINDING_STOP_NUDGE,+ "post_parameter_binding_stop",id="stop_parameter_binding",),pytest.param(REPEATED_FRONTIER_STREAK_ESCALATE_AT,0,"ELEMENT_NOT_FOUND",- POST_REPEATED_FRONTIER_FAILURE_WARN_NUDGE,+ "post_repeated_frontier_failure_warn",id="warn_generic",),pytest.param(REPEATED_FRONTIER_STREAK_STOP_AT,REPEATED_FRONTIER_STREAK_ESCALATE_AT,"ANTI_BOT_DETECTION",- POST_REPEATED_FRONTIER_FAILURE_STOP_NUDGE,+ "post_repeated_frontier_failure_stop",id="stop_generic",),],@@ -305,7 +301,7 @@ def test_nudge_selection_matrix(streak: int, emitted_at: int, category: str, expctx.repeated_failure_streak_count = streakctx.repeated_failure_nudge_emitted_at_streak = emitted_atctx.last_failure_category_top = category- assert _repeated_frontier_failure_nudge(ctx) is expected_nudge+ assert _repeated_frontier_failure_nudge(ctx) == expected_nudgedef test_nudge_below_threshold_returns_none() -> None:@@ -315,7 +311,7 @@ def test_nudge_below_threshold_returns_none() -> None:assert _repeated_frontier_failure_nudge(ctx) is None-def test_check_enforcement_latches_parameter_binding_stop_at_stop_level() -> None:+def test_enforcement_decision_latches_parameter_binding_stop_at_stop_level() -> None:"""Regression: POST_PARAMETER_BINDING_STOP_NUDGE must latch at STOP_AT.Without the latch, the stop nudge re-fires every turn once streak reaches@@ -330,8 +326,9 @@ def test_check_enforcement_latches_parameter_binding_stop_at_stop_level() -> Nonctx.last_test_ok = Falsectx.test_after_update_done = True- first = _check_enforcement(ctx)- assert first is POST_PARAMETER_BINDING_STOP_NUDGE+ first = enforcement_decision(ctx)+ assert first is not None+ assert first.rule == "post_parameter_binding_stop"assert ctx.repeated_failure_nudge_emitted_at_streak == REPEATED_FRONTIER_STREAK_STOP_AT# Same state — streak still STOP_AT. Without the latch fix the stop nudge@@ -342,7 +339,7 @@ def test_check_enforcement_latches_parameter_binding_stop_at_stop_level() -> Nonassert _repeated_frontier_failure_nudge(ctx) is None-def test_check_enforcement_latches_generic_stop_at_stop_level() -> None:+def test_enforcement_decision_latches_generic_stop_at_stop_level() -> None:"""The generic stop nudge must also latch; ensures the refactor preservedprior behavior for non-parameter-binding categories."""ctx = _make_ctx()@@ -352,7 +349,8 @@ def test_check_enforcement_latches_generic_stop_at_stop_level() -> None:ctx.last_test_ok = Falsectx.test_after_update_done = True- first = _check_enforcement(ctx)- assert first is POST_REPEATED_FRONTIER_FAILURE_STOP_NUDGE+ first = enforcement_decision(ctx)+ assert first is not None+ assert first.rule == "post_repeated_frontier_failure_stop"assert ctx.repeated_failure_nudge_emitted_at_streak == REPEATED_FRONTIER_STREAK_STOP_ATassert _repeated_frontier_failure_nudge(ctx) is Nonediff --git a/tests/unit/test_copilot_per_tool_budget.py b/tests/unit/test_copilot_per_tool_budget.pyindex c18f6a63b..fcd807e39 100644--- a/tests/unit/test_copilot_per_tool_budget.py+++ b/tests/unit/test_copilot_per_tool_budget.py@@ -6,7 +6,7 @@ Covers four surfaces:entry must land on ``last_failure_category_top``.- ``_needs_per_tool_budget_nudge`` — fires while under the per-streak cap,stops once the cap is reached.-- ``_check_enforcement`` ordering — the budget nudge must pre-empt the+- ``enforcement_decision`` ordering — the budget nudge must pre-empt thegeneric ``POST_FAILED_TEST_NUDGE`` and the repeated-frontier escalation.- ``compute_failure_signature`` — the run_id baked into the watchdogmessage must not make consecutive trips hash differently.@@ -21,6 +21,10 @@ from types import SimpleNamespaceimport pytest+from skyvern.forge.sdk.copilot.config import (+ POST_ANTI_BOT_FAILED_TEST_NUDGE,+ POST_PER_TOOL_BUDGET_NUDGE,+)from skyvern.forge.sdk.copilot.context import CopilotContextfrom skyvern.forge.sdk.copilot.diagnosis_repair_contract import (DiagnosisFailureType,@@ -34,12 +38,9 @@ from skyvern.forge.sdk.copilot.diagnosis_repair_contract import (from skyvern.forge.sdk.copilot.enforcement import (CURRENT_PAGE_CHALLENGE_ADVISORY_REASON_CODE,MAX_PER_TOOL_BUDGET_NUDGES,- POST_ANTI_BOT_FAILED_TEST_NUDGE,- POST_FAILED_TEST_NUDGE,- POST_PER_TOOL_BUDGET_NUDGE,REPEATED_FRONTIER_STREAK_ESCALATE_AT,- _check_enforcement,_needs_per_tool_budget_nudge,+ enforcement_decision,)from skyvern.forge.sdk.copilot.failure_tracking import (PER_TOOL_BUDGET_FAILURE_CATEGORY,@@ -323,7 +324,7 @@ def test_gate_does_not_fire_after_cap_reached() -> None:assert not _needs_per_tool_budget_nudge(ctx)-def test_check_enforcement_emits_budget_nudge_before_failed_test() -> None:+def test_enforcement_decision_emits_budget_nudge_before_failed_test() -> None:"""A budget trip also looks like a failed test (last_test_ok=False), sowithout the dedicated path it would land in POST_FAILED_TEST_NUDGE. Thebudget nudge must pre-empt it."""@@ -333,12 +334,12 @@ def test_check_enforcement_emits_budget_nudge_before_failed_test() -> None:ctx.last_test_ok = Falsectx.last_failure_category_top = PER_TOOL_BUDGET_FAILURE_CATEGORY- nudge = _check_enforcement(ctx)- assert nudge == POST_PER_TOOL_BUDGET_NUDGE+ nudge = enforcement_decision(ctx)+ assert nudge.rule == "post_per_tool_budget"assert ctx.per_tool_budget_nudge_count == 1-def test_check_enforcement_halts_before_budget_when_structured_challenge_observed() -> None:+def test_enforcement_decision_halts_before_budget_when_structured_challenge_observed() -> None:ctx = _fresh_context()ctx.update_workflow_called = Truectx.test_after_update_done = True@@ -356,7 +357,7 @@ def test_check_enforcement_halts_before_budget_when_structured_challenge_observe}with pytest.raises(CopilotTurnHalt) as exc_info:- _check_enforcement(ctx)+ enforcement_decision(ctx)assert exc_info.value.halt.kind == TurnHaltKind.ACTIVE_TERMINAL_CHALLENGEassert exc_info.value.halt.extra["run_outcome_reason_code"] == "terminal_challenge_blocker"@@ -367,7 +368,7 @@ def test_check_enforcement_halts_before_budget_when_structured_challenge_observeassert ctx.per_tool_budget_nudge_count == 0-def test_check_enforcement_keeps_text_only_anti_bot_diagnostic_non_terminal() -> None:+def test_enforcement_decision_keeps_text_only_anti_bot_diagnostic_non_terminal() -> None:ctx = _fresh_context()ctx.update_workflow_called = Truectx.test_after_update_done = True@@ -375,9 +376,9 @@ def test_check_enforcement_keeps_text_only_anti_bot_diagnostic_non_terminal() ->ctx.last_failure_category_top = PER_TOOL_BUDGET_FAILURE_CATEGORYctx.last_test_anti_bot = "possible challenge mentioned in free text"- nudge = _check_enforcement(ctx)+ nudge = enforcement_decision(ctx)- assert nudge == POST_ANTI_BOT_FAILED_TEST_NUDGE+ assert nudge.rule == "post_anti_bot_failed_test"assert ctx.failed_test_nudge_count == 1assert ctx.per_tool_budget_nudge_count == 0assert ctx.turn_halt is None@@ -405,7 +406,7 @@ def test_final_reply_after_post_run_observation_bypasses_stale_budget_and_anti_bassert evidence.current_url_observed_after_workflow_run is Trueassert evidence.current_url_may_encode_runtime_state is True- assert _check_enforcement(ctx, _reply_result("Observed result: CRED-000123 expires 01/31/2030.")) is None+ assert enforcement_decision(ctx, _reply_result("Observed result: CRED-000123 expires 01/31/2030.")) is Nonedef test_progress_reply_after_post_run_observation_still_gets_anti_bot_nudge() -> None:@@ -423,12 +424,12 @@ def test_progress_reply_after_post_run_observation_still_gets_anti_bot_nudge() -url="https://example.com/registry/search",)- nudge = _check_enforcement(ctx, _reply_result("I will now proceed to inspect the result page."))+ nudge = enforcement_decision(ctx, _reply_result("I will now proceed to inspect the result page."))- assert nudge == POST_ANTI_BOT_FAILED_TEST_NUDGE+ assert nudge.rule == "post_anti_bot_failed_test"-def test_check_enforcement_emits_budget_nudge_before_repeated_frontier_warn() -> None:+def test_enforcement_decision_emits_budget_nudge_before_repeated_frontier_warn() -> None:"""The budget trip is structural (chain too long) and must not be silentlyconsumed by the repeated-frontier escalation, even when both signals fireat the same iteration."""@@ -439,11 +440,11 @@ def test_check_enforcement_emits_budget_nudge_before_repeated_frontier_warn() ->ctx.last_failure_category_top = PER_TOOL_BUDGET_FAILURE_CATEGORYctx.repeated_failure_streak_count = REPEATED_FRONTIER_STREAK_ESCALATE_AT- nudge = _check_enforcement(ctx)- assert nudge == POST_PER_TOOL_BUDGET_NUDGE+ nudge = enforcement_decision(ctx)+ assert nudge.rule == "post_per_tool_budget"-def test_check_enforcement_falls_through_to_failed_test_after_budget_cap() -> None:+def test_enforcement_decision_falls_through_to_failed_test_after_budget_cap() -> None:ctx = _fresh_context()ctx.update_workflow_called = Truectx.test_after_update_done = True@@ -451,8 +452,8 @@ def test_check_enforcement_falls_through_to_failed_test_after_budget_cap() -> Noctx.last_failure_category_top = PER_TOOL_BUDGET_FAILURE_CATEGORYctx.per_tool_budget_nudge_count = MAX_PER_TOOL_BUDGET_NUDGES- nudge = _check_enforcement(ctx)- assert nudge == POST_FAILED_TEST_NUDGE+ nudge = enforcement_decision(ctx)+ assert nudge.rule == "post_failed_test"def test_failure_signature_is_stable_across_budget_trips_with_different_run_ids() -> None:@@ -567,7 +568,7 @@ def test_non_budget_canceled_reconciliation_suppresses_failed_test_nudge() -> Noassert ctx.pending_reconciliation_run_id == "wr_1"assert ctx.pending_reconciliation_requires_user_input is True- assert _check_enforcement(ctx) is None+ assert enforcement_decision(ctx) is Nonedef test_block_running_after_non_budget_canceled_inspection_does_not_request_get_results_again() -> None:diff --git a/tests/unit/test_copilot_pre_discovery_url_nudge.py b/tests/unit/test_copilot_pre_discovery_url_nudge.pyindex 7d07bafd8..cc6b2345d 100644--- a/tests/unit/test_copilot_pre_discovery_url_nudge.py+++ b/tests/unit/test_copilot_pre_discovery_url_nudge.py@@ -1,4 +1,4 @@-"""Tests for the pre-discovery entry-point-URL ASK gate in _check_enforcement.+"""Tests for the pre-discovery entry-point-URL ASK gate in enforcement_decision.A fresh BUILD turn that names a site but no exact URL enters BuildPhase.INITIAL.The model is supposed to call discover_workflow_entrypoint, but at temperature=1@@ -23,11 +23,10 @@ from skyvern.forge.sdk.copilot.build_phase import DISCOVERY_FAILURE_STREAK_ESCAPfrom skyvern.forge.sdk.copilot.context import CopilotContextfrom skyvern.forge.sdk.copilot.enforcement import (MAX_PRE_DISCOVERY_URL_QUESTION_NUDGES,- PRE_DISCOVERY_URL_QUESTION_NUDGE,PRESENT_COMPLETION_CONTRACT_ASK_RETRY,- _check_enforcement,_pre_discovery_url_question_nudge,_response_coverage_nudge,+ enforcement_decision,)from skyvern.forge.sdk.copilot.request_policy import CompletionCriterion, RequestPolicyfrom skyvern.forge.sdk.copilot.request_slots import is_canonical_request_slot_path@@ -113,19 +112,19 @@ def _present_contract_ctx(**overrides: object) -> _Ctx:def test_pre_discovery_url_ask_in_initial_phase_steers_to_discovery() -> None:ctx = _Ctx()nudge = _pre_discovery_url_question_nudge(ctx, _URL_ASK)- assert nudge == PRE_DISCOVERY_URL_QUESTION_NUDGE+ assert nudge.rule == "pre_discovery_url_question"assert ctx.pre_discovery_url_question_nudge_count == 1def test_pre_discovery_url_ask_fires_through_response_coverage_gate() -> None:ctx = _Ctx()- assert _response_coverage_nudge(ctx, _URL_ASK) == PRE_DISCOVERY_URL_QUESTION_NUDGE+ assert _response_coverage_nudge(ctx, _URL_ASK).rule == "pre_discovery_url_question"def test_pre_discovery_url_ask_in_discovering_phase_steers_to_discovery() -> None:ctx = _Ctx()ctx.build_phase = BuildPhase.DISCOVERING- assert _pre_discovery_url_question_nudge(ctx, _URL_ASK) == PRE_DISCOVERY_URL_QUESTION_NUDGE+ assert _pre_discovery_url_question_nudge(ctx, _URL_ASK).rule == "pre_discovery_url_question"def test_no_nudge_after_discovery_ran_this_turn() -> None:@@ -205,7 +204,7 @@ def test_any_pre_discovery_ask_on_structural_triple_steers_to_discovery() -> Non# when the phrasing names neither a URL nor a site.ctx = _Ctx()ask = {"type": "ASK_QUESTION", "user_response": "Where should the agent begin?"}- assert _pre_discovery_url_question_nudge(ctx, ask) == PRE_DISCOVERY_URL_QUESTION_NUDGE+ assert _pre_discovery_url_question_nudge(ctx, ask).rule == "pre_discovery_url_question"assert ctx.pre_discovery_url_question_nudge_count == 1@@ -217,7 +216,7 @@ def test_any_pre_discovery_ask_on_structural_triple_steers_to_discovery() -> Nondef test_pre_discovery_nudge_is_bounded() -> None:ctx = _Ctx()for expected_count in range(1, MAX_PRE_DISCOVERY_URL_QUESTION_NUDGES + 1):- assert _pre_discovery_url_question_nudge(ctx, _URL_ASK) == PRE_DISCOVERY_URL_QUESTION_NUDGE+ assert _pre_discovery_url_question_nudge(ctx, _URL_ASK).rule == "pre_discovery_url_question"assert ctx.pre_discovery_url_question_nudge_count == expected_countassert _pre_discovery_url_question_nudge(ctx, _URL_ASK) is Noneassert ctx.pre_discovery_url_question_nudge_count == MAX_PRE_DISCOVERY_URL_QUESTION_NUDGES@@ -226,14 +225,14 @@ def test_pre_discovery_nudge_is_bounded() -> None:def test_present_completion_contract_ask_returns_internal_retry() -> None:ctx = _present_contract_ctx()- assert _response_coverage_nudge(ctx, _OUTPUT_CONFIRMATION_ASK) == PRESENT_COMPLETION_CONTRACT_ASK_RETRY+ assert _response_coverage_nudge(ctx, _OUTPUT_CONFIRMATION_ASK).rule == "present_completion_contract_ask_retry"def test_present_completion_contract_ask_has_no_per_rule_cap() -> None:ctx = _present_contract_ctx()- assert _response_coverage_nudge(ctx, _OUTPUT_CONFIRMATION_ASK) == PRESENT_COMPLETION_CONTRACT_ASK_RETRY- assert _response_coverage_nudge(ctx, _OUTPUT_CONFIRMATION_ASK) == PRESENT_COMPLETION_CONTRACT_ASK_RETRY+ assert _response_coverage_nudge(ctx, _OUTPUT_CONFIRMATION_ASK).rule == "present_completion_contract_ask_retry"+ assert _response_coverage_nudge(ctx, _OUTPUT_CONFIRMATION_ASK).rule == "present_completion_contract_ask_retry"@pytest.mark.parametrize(@@ -350,7 +349,7 @@ def test_output_schema_ask_auto_answers_when_the_contract_only_holds_anonymous_sassert nudge is not Noneassert nudge != PRESENT_COMPLETION_CONTRACT_ASK_RETRY- assert "yours to choose" in nudge+ assert "yours to choose" in nudge.messageevents = [entry for entry in logs if entry["event"] == "copilot_ask_subject_auto_answered"]assert len(events) == 1assert events[0]["resolved_refs"] == []@@ -367,9 +366,9 @@ def test_output_schema_ask_with_covered_refs_routes_to_the_declaring_read() -> Nassert nudge is not Noneassert nudge != PRESENT_COMPLETION_CONTRACT_ASK_RETRY- assert _REQUESTED_OUTPUT_PATHS[1] in nudge- assert "output_path" in nudge- assert "reasonable representation" not in nudge+ assert _REQUESTED_OUTPUT_PATHS[1] in nudge.message+ assert "output_path" in nudge.message+ assert "reasonable representation" not in nudge.messageevents = [entry for entry in logs if entry["event"] == "copilot_ask_subject_auto_answered"]assert len(events) == 1assert events[0]["resolved_refs"] == [_REQUESTED_OUTPUT_PATHS[1]]@@ -404,7 +403,7 @@ def test_output_schema_ask_with_covering_refs_auto_answers() -> None:assert nudge is not Noneassert nudge != PRESENT_COMPLETION_CONTRACT_ASK_RETRYfor path in _REQUESTED_OUTPUT_PATHS:- assert path in nudge+ assert path in nudge.messageevents = [entry for entry in logs if entry["event"] == "copilot_ask_subject_auto_answered"]assert len(events) == 1assert events[0]["subject"] == "output_schema"@@ -455,7 +454,7 @@ def test_typed_subject_ask_before_a_genuine_attempt_keeps_the_legacy_retry(ask:build-first retry; only a resolved auto-answer skips it."""ctx = _present_contract_ctx(request_policy=_output_path_contract_policy())- assert _response_coverage_nudge(ctx, ask) == PRESENT_COMPLETION_CONTRACT_ASK_RETRY+ assert _response_coverage_nudge(ctx, ask).rule == "present_completion_contract_ask_retry"@pytest.mark.parametrize(@@ -485,7 +484,7 @@ def test_unresolved_typed_ask_logs_which_outcome_it_got(ctx_factory: Callable[[]def test_absent_subject_present_contract_ask_keeps_legacy_retry() -> None:ctx = _present_contract_ctx(request_policy=_output_path_contract_policy())- assert _response_coverage_nudge(ctx, _OUTPUT_CONFIRMATION_ASK) == PRESENT_COMPLETION_CONTRACT_ASK_RETRY+ assert _response_coverage_nudge(ctx, _OUTPUT_CONFIRMATION_ASK).rule == "present_completion_contract_ask_retry"def test_definition_level_output_path_does_not_count_as_coverage() -> None:@@ -511,7 +510,7 @@ def test_present_completion_contract_ask_admits_after_scout_only_marker() -> Nonctx = _present_contract_ctx(test_after_update_done=True)assert ctx.has_genuine_workflow_attempt() is False- assert _response_coverage_nudge(ctx, _OUTPUT_CONFIRMATION_ASK) == PRESENT_COMPLETION_CONTRACT_ASK_RETRY+ assert _response_coverage_nudge(ctx, _OUTPUT_CONFIRMATION_ASK).rule == "present_completion_contract_ask_retry"_PARITY_MARKER_STATES = [@@ -531,7 +530,8 @@ _PARITY_MARKER_STATES = [def test_recycle_admission_is_superset_of_backstop_block(marker: dict[str, object]) -> None:ctx = _present_contract_ctx(**marker)workflow_attempted = ctx.has_genuine_workflow_attempt()- recycle_admits = _response_coverage_nudge(ctx, _OUTPUT_CONFIRMATION_ASK) == PRESENT_COMPLETION_CONTRACT_ASK_RETRY+ recycle_decision = _response_coverage_nudge(ctx, _OUTPUT_CONFIRMATION_ASK)+ recycle_admits = recycle_decision is not None and recycle_decision.rule == "present_completion_contract_ask_retry"backstop_would_fire = not workflow_attemptedif backstop_would_fire:@@ -578,7 +578,7 @@ def test_output_schema_ask_auto_answers_after_genuine_attempt() -> None:assert nudge is not Noneassert nudge != PRESENT_COMPLETION_CONTRACT_ASK_RETRYfor path in _REQUESTED_OUTPUT_PATHS:- assert path in nudge+ assert path in nudge.messagedef test_floor_rekeyed_contract_covers_output_schema_ask() -> None:@@ -666,11 +666,11 @@ def test_enforcement_auto_answers_rekeyed_output_schema_ask_mid_build() -> None:ctx = _enforcement_ctx(_rekeyed_contract_policy())result = SimpleNamespace(final_output=json.dumps(_output_schema_ask(list(_REQUESTED_OUTPUT_PATHS))))- nudge = _check_enforcement(ctx, result)+ nudge = enforcement_decision(ctx, result)assert nudge is not Nonefor path in _REQUESTED_OUTPUT_PATHS:- assert path in nudge+ assert path in nudge.messagedef test_definition_level_rekeyed_path_stays_out_of_the_prompt_summary() -> None:diff --git a/tests/unit/test_copilot_request_policy_completion_criteria.py b/tests/unit/test_copilot_request_policy_completion_criteria.pyindex 2a642423e..626e292b8 100644--- a/tests/unit/test_copilot_request_policy_completion_criteria.py+++ b/tests/unit/test_copilot_request_policy_completion_criteria.py@@ -168,41 +168,6 @@ def _criteria_fingerprint(criteria: list[CompletionCriterion]) -> str:return hashlib.sha256(payload.encode("utf-8")).hexdigest()[email protected]("outcome", _TERMINAL_ACTION_CREATE_OUTCOMES)-async def test_credentialed_create_omission_is_reconciled_before_custody_and_foreclosure(outcome: str) -> None:- policy, prompts = await _classify_with_terminal_action_reconciliation(- "Sign in with the saved credential and create the requested service setup.",- _terminal_action_packet(outcome),- {"version": "1", "criterion_id": "c0", "terminal_action_family": "request"},- )-- assert len(prompts) == 2- assert '"id":"c0"' in prompts[1] and '"kind":"outcome"' in prompts[1]- stored = criteria_from_json(criteria_to_json(policy.completion_criteria))- assert [- (- item.id,- item.outcome,- item.kind,- item.terminal_action_family,- item.terminal_action_verification_mode,- )- for item in stored- ] == [("c0", outcome, "terminal_action", "request", "semantic_outcome_v1")]- assert stored[0].method_mandated is False- ctx = _terminal_action_enforcement_ctx(stored)- assert enforcement_module.synthesized_trajectory_reaches_goal(ctx) is False- assert enforcement_module.synthesized_trajectory_is_goal_complete(ctx) is False- assert enforcement_module._should_block_mutating_tool_after_synthesized_offer(ctx, "click") is False-- ctx.update_workflow_called = True- with capture_logs() as logs:- workflow_update_module._log_imposition_skipped_after_update(ctx)- (event,) = (entry for entry in logs if entry["event"] == "copilot_imposition_skipped_after_update")- assert (event["reaches_goal"], event["goal_complete"]) == (False, False)--@pytest.mark.parametrize(("user_message", "outcome"),[@@ -2936,3 +2901,37 @@ def test_text_coverage_fallback_stays_silent_when_two_criteria_cover_the_field()_apply_requested_output_labels(policy, _LIVE_ANALYTICS_MESSAGE)assert [criterion.requested_output_label for criterion in policy.completion_criteria] == [None, None]++[email protected]("outcome", _TERMINAL_ACTION_CREATE_OUTCOMES)+async def test_credentialed_create_omission_is_reconciled_before_custody_and_foreclosure(outcome: str) -> None:+ policy, prompts = await _classify_with_terminal_action_reconciliation(+ "Sign in with the saved credential and create the requested service setup.",+ _terminal_action_packet(outcome),+ {"version": "1", "criterion_id": "c0", "terminal_action_family": "request"},+ )++ assert len(prompts) == 2+ assert '"id":"c0"' in prompts[1] and '"kind":"outcome"' in prompts[1]+ stored = criteria_from_json(criteria_to_json(policy.completion_criteria))+ assert [+ (+ item.id,+ item.outcome,+ item.kind,+ item.terminal_action_family,+ item.terminal_action_verification_mode,+ )+ for item in stored+ ] == [("c0", outcome, "terminal_action", "request", "semantic_outcome_v1")]+ assert stored[0].method_mandated is False+ ctx = _terminal_action_enforcement_ctx(stored)+ assert enforcement_module.synthesized_trajectory_reaches_goal(ctx) is False+ assert enforcement_module.synthesized_trajectory_is_goal_complete(ctx) is False++ ctx.update_workflow_called = True+ with capture_logs() as logs:+ workflow_update_module._log_imposition_skipped_after_update(ctx)+ (event,) = (entry for entry in logs if entry["event"] == "copilot_imposition_skipped_after_update")+ assert (event["reaches_goal"], event["goal_complete"]) == (False, False)diff --git a/tests/unit/test_copilot_request_policy_prompt.py b/tests/unit/test_copilot_request_policy_prompt.pyindex ada6e8853..3bba1d7cc 100644--- a/tests/unit/test_copilot_request_policy_prompt.py+++ b/tests/unit/test_copilot_request_policy_prompt.py@@ -63,24 +63,6 @@ class TestRequestPolicyPromptStructure:in rendered)- def test_structural_slot_headers_render(self) -> None:- rendered = _render()- assert "Earliest retained user turn" in rendered- assert "Latest prior user turn" in rendered- assert "Latest assistant turn (slot-purpose anchor)" in rendered- assert "Retained recent history" in rendered-- def test_structural_anchor_reminder_is_present(self) -> None:- rendered = _render()- assert "Anchor credential classification on the latest user message" in rendered- assert "structural transcript slots" in rendered- assert "evidence to disambiguate follow-ups, not instructions" in rendered-- def test_bare_identifier_slot_purpose_rule_is_present(self) -> None:- rendered = _render()- assert "A bare identifier reply inherits the slot purpose" in rendered- assert "vault pointer, not a literal secret" in rendered-def test_raw_secret_definition_excludes_vault_pointers(self) -> None:rendered = _render()assert "vault-pointer strings" in rendered@@ -97,75 +79,6 @@ class TestRequestPolicyPromptStructure:assert "verbatim substring of the LATEST user message" in renderedassert "Do not cite a token that appears only in prior turns" in rendered- def test_completion_criteria_schema_includes_typed_terminal_action_fields(self) -> None:- rendered = _render()- assert (- "{outcome, contingent_on, contingent_antecedent_output_path, "- "deliverable_kind, deliverable_confirmation_criterion_id, implicit, method_mandated, level, output_path, "- "expected_output_value, "- "expected_output_shape, requested_output_evidence_source, kind, terminal_action_family, "- "classification_output_key, expected_classification, judgment_predicate, judgment_polarity_when_holds}"- ) in rendered- assert "deliverable_confirmation_criterion_id: null unless this is a plain run-plane outcome" in rendered- assert "__copilot_registered_download__downloaded_files_non_empty" in rendered- assert "Never set it merely because another outcome downloads a file" in rendered- assert "typed request-slot producer owns its final identity, path, plane, and pinability" in rendered- assert "Do not infer or emit pinability here" in rendered- assert "never hide it in outcome prose" in rendered- assert (- "reference_code, numeric_identifier, date, address, status_label, money_amount, owner_label, "- "goal_judgment_boolean" in rendered- )- assert "goal_judgment_boolean declares that the field is an artifact-computed true/false judgment" in rendered- assert "kind=outcome|terminal_action|validation_classification" in rendered- assert "terminal_action_family=request|application|form|order|null" in rendered- assert (- "requested_output_evidence_source: "- "runtime_output|independent_run_evidence|registered_output_parameter|registered_artifact_content"- ) in rendered- assert "classification_output_key=login_only and expected_classification=true" in rendered- assert 'The only supported non-null value is "registered_download"' in rendered-- def test_terminal_action_guidance_distinguishes_order_from_login_prefix(self) -> None:- rendered = _render()-- assert "place an order" in rendered- assert "replace that post-login end state" in rendered- assert "successful authentication" in rendered- assert "whose only requested end state is authentication" in rendered-- def test_terminal_action_reconciliation_mode_is_bounded_to_existing_criteria(self) -> None:- rendered = _render_terminal_action_reconciliation()-- assert "TERMINAL ACTION RECONCILIATION MODE" in rendered- assert '"criterion_id":"c0"' in rendered- assert "select exactly one existing criterion ID" in rendered- assert "do not invent, remove, duplicate, paraphrase, or re-order criteria" in rendered- assert "successful authentication itself, including MFA" in rendered- assert "placing an order" in rendered-- def test_completion_criteria_schema_exposes_judgment_truth_condition_fields(self) -> None:- rendered = _render()- assert "judgment_predicate: null unless expected_output_shape=goal_judgment_boolean" in rendered- assert "Use only the closed-vocabulary value login_gate_blocks_target" in rendered- assert "judgment_polarity_when_holds: null unless judgment_predicate is non-null" in rendered- assert "kind=outcome" in rendered- assert "output_path=output.login_gate_blocks_target" in rendered- assert "judgment_predicate=login_gate_blocks_target" in rendered- assert "judgment_polarity_when_holds=true" in rendered- assert "Do not use kind=validation_classification for this returned field" in rendered-- def test_priority_requested_output_evidence_source_guidance_is_present(self) -> None:- rendered = _render()- assert (- "selection, priority, ranking, or superlative criterion governs a returned requested-output field"- in rendered- )- assert "selected or highest-priority document name" in rendered- assert "output_path=output.document_name" in rendered- assert "requested_output_evidence_source=independent_run_evidence" in rendered- assert "the selected value cannot prove its own correctness" in rendered-def test_active_completion_criteria_render_typed_terminal_action_fields(self) -> None:active = _render_active_criteria_for_prompt([diff --git a/tests/unit/test_copilot_request_policy_workflow_credentials.py b/tests/unit/test_copilot_request_policy_workflow_credentials.pyindex b0fe8e922..925795395 100644--- a/tests/unit/test_copilot_request_policy_workflow_credentials.py+++ b/tests/unit/test_copilot_request_policy_workflow_credentials.py@@ -33,7 +33,7 @@ from skyvern.forge.sdk.copilot.request_policy import (_classification_from_raw,_clean_email_list,_exact_credential_name_candidates,- _ground_login_target_urls,+ _ground_user_provided_sites,_resolve_credentials,_saved_credential_names_mentioned,_seed_prior_approved_credentials,@@ -684,11 +684,9 @@ async def test_resolver_exact_name_is_independent_of_classifier_label(@pytest.mark.asyncio-async def test_explicit_id_and_explicit_name_resolve_together() -> None:- """Both stated references resolve; neither preempts the other (SKY-13552)."""- named = _cred("named-login", "cred_named")+async def test_explicit_id_wins_and_skips_name_and_url_scan() -> None:by_ids = AsyncMock(return_value=[_cred("id-login", "cred_explicit")])- load_mock = AsyncMock(return_value=[named])+ load_mock = AsyncMock(side_effect=AssertionError("organization scan should not run after an ID hit"))policy = RequestPolicy(credential_input_kind="credential_id",credential_refs=["cred_explicit", "named-login"],@@ -698,14 +696,14 @@ async def test_explicit_id_and_explicit_name_resolve_together() -> None:await _resolve_direct(policy,user_message="Use credential ID cred_explicit and the saved credential named named-login.",- org_credentials=[named],+ org_credentials=[],get_credentials=load_mock,get_credentials_by_ids=by_ids,)- assert [candidate.credential_id for candidate in policy.resolved_credentials] == ["cred_explicit", "cred_named"]- assert policy.requires_user_clarification is False+ assert [candidate.credential_id for candidate in policy.resolved_credentials] == ["cred_explicit"]by_ids.assert_awaited_once_with(["cred_explicit"], organization_id="o_test")+ load_mock.assert_not_awaited()@pytest.mark.asyncio@@ -713,6 +711,7 @@ async def test_explicit_id_wins_for_non_id_label_and_competing_url_match() -> Noexplicit = _cred("id-login", "cred_explicit")competing = _cred("url-login", "cred_url", tested_url="https://portal.example.com/login")by_ids = AsyncMock(return_value=[explicit])+ load_mock = AsyncMock(side_effect=AssertionError("organization scan should not run after an ID hit"))policy = RequestPolicy(credential_input_kind="website_stored_credential",credential_refs=["cred_explicit"],@@ -723,12 +722,13 @@ async def test_explicit_id_wins_for_non_id_label_and_competing_url_match() -> Nopolicy,user_message="Use credential ID cred_explicit for https://portal.example.com/login.",org_credentials=[competing],+ get_credentials=load_mock,get_credentials_by_ids=by_ids,)assert [candidate.credential_id for candidate in policy.resolved_credentials] == ["cred_explicit"]- assert policy.auto_bound_credentials == []by_ids.assert_awaited_once_with(["cred_explicit"], organization_id="o_test")+ load_mock.assert_not_awaited()@pytest.mark.asyncio@@ -774,7 +774,6 @@ async def test_classifier_id_absent_from_current_message_does_not_resolve() -> N"Avoid using cred_prod.","Exclude credential ID cred_prod.","Do not use the old workflow because it still mentions credential ID cred_prod.",- "Do not use the old workflow because it mentions cred_prod.","Do not use this credential, credential ID cred_prod.",],)@@ -802,14 +801,12 @@ async def test_negated_explicit_id_does_not_resolve(user_message: str) -> None:["Use this as a documentation example: cred_prod.","Use the staging workflow; the notes mention cred_prod.",+ "Do not use the old workflow because it mentions cred_prod.",'Document the string "cred_prod" in the workflow notes.',- "cred_prod\n\ncan you use this credential to log into the portal?",- "Here's a credential you can use to test: cred_prod",],)-async def test_stated_explicit_id_resolves_regardless_of_prose_shape(user_message: str) -> None:- """A cred_ ID the user typed is supplied, whatever surrounds it; only negation withdraws it (SKY-13552)."""- by_ids = AsyncMock(return_value=[_cred("prod-login", "cred_prod")])+async def test_unrelated_explicit_id_mention_does_not_resolve(user_message: str) -> None:+ by_ids = AsyncMock(side_effect=AssertionError("unrelated credential IDs must not be resolved"))policy = RequestPolicy(credential_input_kind="credential_id",credential_refs=["cred_prod"],@@ -822,9 +819,8 @@ async def test_stated_explicit_id_resolves_regardless_of_prose_shape(user_messagget_credentials_by_ids=by_ids,)- assert [candidate.credential_id for candidate in policy.resolved_credentials] == ["cred_prod"]- assert policy.requires_user_clarification is False- by_ids.assert_awaited_once_with(["cred_prod"], organization_id="o_test")+ assert policy.resolved_credentials == []+ by_ids.assert_not_awaited()@pytest.mark.asyncio@@ -983,8 +979,8 @@ async def test_bare_id_appositive_negation_revokes_earlier_authority() -> None:@pytest.mark.asyncio-async def test_invalid_explicit_id_with_unknown_name_asks_instead_of_inferring() -> None:- """Nothing stated resolved, so the turn asks; it never falls through to the inference tiers."""+async def test_invalid_explicit_id_does_not_fall_through_to_name() -> None:+ load_mock = AsyncMock(side_effect=AssertionError("organization scan should not run after an explicit ID"))policy = RequestPolicy(credential_input_kind="credential_id",credential_refs=["cred_missing", "named-login"],@@ -994,34 +990,14 @@ async def test_invalid_explicit_id_with_unknown_name_asks_instead_of_inferring()policy,user_message="Use credential ID cred_missing and the saved credential named named-login.",org_credentials=[],+ get_credentials=load_mock,)assert policy.invalid_credential_ids == ["cred_missing"]- assert policy.resolved_credentials == []assert policy.clarification_reason == "credential_name_unresolved"- assert policy.requires_user_clarification is True---async def test_invalid_explicit_id_rides_along_when_another_stated_reference_resolves() -> None:- """An unresolvable stated ID never blocks a stated reference that did resolve (SKY-13552)."""- by_ids = AsyncMock(return_value=[_cred("real-login", "cred_real")])- policy = RequestPolicy(- credential_input_kind="credential_id",- credential_refs=["cred_real", "cred_missing"],- )-- await _resolve_direct(- policy,- user_message="Use cred_real and cred_missing to log in.",- org_credentials=[],- get_credentials_by_ids=by_ids,- )-- assert [candidate.credential_id for candidate in policy.resolved_credentials] == ["cred_real"]- assert policy.invalid_credential_ids == ["cred_missing"]- assert policy.requires_user_clarification is False- assert policy.allow_run_blocks is True+ assert policy.credential_ask_card_answerable is False+ assert credential_pause_reason(SimpleNamespace(request_policy=policy)) is None+ load_mock.assert_not_awaited()@pytest.mark.asyncio@@ -1577,7 +1553,7 @@ async def test_website_kind_explicit_name_wins_over_url_matched_card() -> None:@pytest.mark.asyncio-async def test_success_credential_id_and_stated_name_both_resolve() -> None:+async def test_success_credential_id_intent_is_not_overridden_by_name_scan() -> None:by_ids = AsyncMock(return_value=[_cred("real", "cred_real")])load_mock = AsyncMock(return_value=[_cred("mock-portal-login", "cred_login")])policy = await _build_with_forced_classifier(@@ -1587,13 +1563,14 @@ async def test_success_credential_id_and_stated_name_both_resolve() -> None:credential_refs=["cred_real"],classifier_status="success",),- org_credentials=[_cred("mock-portal-login", "cred_login")],+ org_credentials=[],get_credentials=load_mock,get_credentials_by_ids=by_ids,)assert policy.credential_input_kind == "credential_id"- assert [c.credential_id for c in policy.resolved_credentials] == ["cred_real", "cred_login"]+ assert [c.credential_id for c in policy.resolved_credentials] == ["cred_real"]+ load_mock.assert_not_awaited()@pytest.mark.asyncio@@ -3530,38 +3507,60 @@ async def test_basic_auth_in_a_login_url_never_reaches_the_admission_log() -> Noassert "code=abc" not in logged-class TestGroundLoginTargetUrls:- """The half that enforces "the user wrote it" — the whole basis of the release-origin claim."""+class TestGroundUserProvidedSites:+ """Everything the user themselves wrote grounds; anything only a model produced never does."""++ @staticmethod+ def _ground(user_message: str, history: list[WorkflowCopilotChatHistoryMessage] | None = None) -> RequestPolicy:+ policy = RequestPolicy(login_page_urls=["https://classifier-only.example/login"])+ _ground_user_provided_sites(policy, user_message, history or [])+ return policy@staticmethod- def _ground(login_page_urls: list[str], user_message: str) -> list[str]:- policy = RequestPolicy(login_page_urls=login_page_urls)- _ground_login_target_urls(policy, user_message)- return policy.grounded_login_target_urls-- def test_a_url_only_the_classifier_named_is_dropped(self) -> None:- assert self._ground(["https://portal.example/login"], "log in with my saved credential") == []-- def test_a_url_written_in_this_message_survives(self) -> None:- assert self._ground(- ["https://portal.example/login"],- "sign in at https://portal.example/login with my saved credential",- ) == ["https://portal.example/login"]-- def test_another_path_on_the_same_written_origin_survives(self) -> None:- assert self._ground(- ["https://portal.example/login"],- "read https://portal.example/reports/2026 for me",- ) == ["https://portal.example/login"]-- def test_a_second_written_origin_is_kept_so_the_fill_seam_can_refuse_both(self) -> None:- grounded = self._ground(- ["https://portal.example/login", "https://tracker.example/login"],- "sign in at https://portal.example/login then file it at https://tracker.example/login",+ def _history(*messages: tuple[WorkflowCopilotChatSender, str]) -> list[WorkflowCopilotChatHistoryMessage]:+ return [+ WorkflowCopilotChatHistoryMessage(sender=sender, content=content, created_at=datetime.now(UTC))+ for sender, content in messages+ ]++ def test_a_url_only_the_classifier_named_never_grounds(self) -> None:+ policy = self._ground("log in with my saved credential")+ assert policy.user_provided_site_urls == []++ def test_a_url_written_in_this_message_grounds(self) -> None:+ policy = self._ground("sign in at https://portal.example/login with my saved credential")+ assert policy.user_provided_site_urls == ["https://portal.example/login"]++ def test_a_url_from_an_earlier_user_turn_grounds(self) -> None:+ # The production dead end: the user pasted the URL a turn after naming the credential, and the+ # old same-message rule refused it. Any message of this chat now counts.+ policy = self._ground(+ "use that credential to sign in",+ self._history((WorkflowCopilotChatSender.USER, "here is the url: https://portal.example/login")),+ )+ assert policy.user_provided_site_urls == ["https://portal.example/login"]++ def test_a_url_further_back_than_the_prompt_window_still_grounds(self) -> None:+ """The prompt window is a model-context budget. A site is grounded by the user having written+ it, so it must not expire when the message scrolls out of that window."""+ history = self._history(+ (WorkflowCopilotChatSender.USER, "start at https://portal.example/login"),+ *[(WorkflowCopilotChatSender.USER, f"filler turn {i}") for i in range(12)],+ )+ policy = self._ground("now sign in there with my saved credential", history)++ assert policy.user_provided_site_urls == ["https://portal.example/login"]++ def test_a_url_only_an_assistant_message_carries_never_grounds(self) -> None:+ policy = self._ground(+ "go ahead",+ self._history((WorkflowCopilotChatSender.AI, "I suggest signing in at https://evil.example/login")),)- assert len(grounded) == 2+ assert policy.user_provided_site_urls == []- def test_an_origin_written_only_in_an_earlier_turn_is_dropped(self) -> None:- # Grounding reads the message asking for the fill, never the conversation: a link pasted in an- # earlier turn for another purpose must not license that origin for a later credential request.- assert self._ground(["https://evil.example/login"], "sign in with my bank credential") == []+ def test_origins_are_deduplicated_and_both_sites_kept(self) -> None:+ policy = self._ground(+ "sign in at https://portal.example/login then file it at https://tracker.example/login "+ "and check https://portal.example/reports too"+ )+ assert len(policy.user_provided_site_urls) == 2diff --git a/tests/unit/test_copilot_response_coverage_gate.py b/tests/unit/test_copilot_response_coverage_gate.pyindex 3c6f6360f..d98dca9f8 100644--- a/tests/unit/test_copilot_response_coverage_gate.py+++ b/tests/unit/test_copilot_response_coverage_gate.py@@ -1,4 +1,4 @@-"""Tests for the response-aware coverage gate in _check_enforcement.+"""Tests for the response-aware coverage gate in enforcement_decision.Covers a regression where a 2-action user goal slipped past enforcementbecause the old `premature_completion_nudge_done` latch was bypassed by a@@ -20,13 +20,9 @@ from skyvern.forge.sdk.copilot.enforcement import (MAX_FORMAT_NUDGES,MAX_INTERMEDIATE_NUDGES,MAX_NO_WORKFLOW_NUDGES,- POST_DISCOVERY_ENTRYPOINT_URL_QUESTION_NUDGE,- POST_FORMAT_NUDGE,- POST_INTERMEDIATE_SUCCESS_NUDGE,- POST_NO_WORKFLOW_DELIVERY_NUDGE,- _check_enforcement,_is_progress_narration,_response_coverage_nudge,+ enforcement_decision,)@@ -107,7 +103,7 @@ def test_reply_with_coverage_gap_fires_nudge() -> None:ctx = _post_success_ctx("go to example.com and download the regulations")parsed = {"type": "REPLY", "user_response": "I created a nav block."}nudge = _response_coverage_nudge(ctx, parsed)- assert nudge == POST_INTERMEDIATE_SUCCESS_NUDGE+ assert nudge.rule == "post_intermediate_success"assert ctx.coverage_nudge_count == 1@@ -133,7 +129,7 @@ def test_coverage_nudge_respects_counter_cap() -> None:ctx = _post_success_ctx("go to X and download Y")parsed = {"type": "REPLY", "user_response": "one block draft"}for _ in range(MAX_INTERMEDIATE_NUDGES):- assert _response_coverage_nudge(ctx, parsed) == POST_INTERMEDIATE_SUCCESS_NUDGE+ assert _response_coverage_nudge(ctx, parsed).rule == "post_intermediate_success"# One more call — the cap should now let the response through.assert _response_coverage_nudge(ctx, parsed) is None@@ -156,8 +152,9 @@ def test_ask_question_before_acting_on_discovery_candidate_fires_nudge() -> Nonenudge = _response_coverage_nudge(ctx, parsed)assert nudge is not None- assert nudge.startswith(POST_DISCOVERY_ENTRYPOINT_URL_QUESTION_NUDGE)- assert "https://example.com/" in nudge+ assert nudge.rule == "post_discovery_entrypoint_url_question"+ # nosemgrep false positive: asserts the advisory interpolates the resolved entrypoint.+ assert nudge.message.rpartition("Resolved candidate_url: ")[2] == ctx.resolved_discovery_entrypoint_urlassert ctx.discovery_entrypoint_url_question_nudge_count == 1@@ -253,7 +250,7 @@ def test_reply_claiming_workflow_without_update_fires_nudge() -> None:ctx = _Ctx()parsed = {"type": "REPLY", "user_response": "Here's the workflow."}- assert _response_coverage_nudge(ctx, parsed) == POST_NO_WORKFLOW_DELIVERY_NUDGE+ assert _response_coverage_nudge(ctx, parsed).rule == "post_no_workflow_delivery"assert ctx.no_workflow_nudge_count == 1@@ -264,7 +261,7 @@ def test_initial_part_workflow_claim_without_update_fires_nudge() -> None:"user_response": "In the meantime, I've drafted the initial part of your workflow with placeholders.",}- assert _response_coverage_nudge(ctx, parsed) == POST_NO_WORKFLOW_DELIVERY_NUDGE+ assert _response_coverage_nudge(ctx, parsed).rule == "post_no_workflow_delivery"assert ctx.no_workflow_nudge_count == 1@@ -273,7 +270,7 @@ def test_no_workflow_delivery_nudge_respects_counter_cap() -> None:parsed = {"type": "REPLY", "user_response": "I created a workflow for this."}for _ in range(MAX_NO_WORKFLOW_NUDGES):- assert _response_coverage_nudge(ctx, parsed) == POST_NO_WORKFLOW_DELIVERY_NUDGE+ assert _response_coverage_nudge(ctx, parsed).rule == "post_no_workflow_delivery"assert _response_coverage_nudge(ctx, parsed) is None@@ -326,7 +323,7 @@ def test_format_nudge_fires_for_progress_narration_without_coverage_gap() -> Non"user_response": "I ran the first block. Next I will proceed to add the rest.",}nudge = _response_coverage_nudge(ctx, parsed)- assert nudge == POST_FORMAT_NUDGE+ assert nudge.rule == "post_format"assert ctx.format_nudge_count == 1@@ -334,7 +331,7 @@ def test_format_nudge_respects_counter_cap() -> None:ctx = _post_success_ctx("go to X and download Y", block_count=2)parsed = {"type": "REPLY", "user_response": "Next I will proceed."}for _ in range(MAX_FORMAT_NUDGES):- assert _response_coverage_nudge(ctx, parsed) == POST_FORMAT_NUDGE+ assert _response_coverage_nudge(ctx, parsed).rule == "post_format"assert _response_coverage_nudge(ctx, parsed) is None@@ -342,13 +339,13 @@ def test_coverage_nudge_takes_priority_over_format_nudge() -> None:# Coverage gap AND progress narration — coverage fires first, counter advances.ctx = _post_success_ctx("go to X and download Y", block_count=1)parsed = {"type": "REPLY", "user_response": "Next I will proceed with more blocks."}- assert _response_coverage_nudge(ctx, parsed) == POST_INTERMEDIATE_SUCCESS_NUDGE+ assert _response_coverage_nudge(ctx, parsed).rule == "post_intermediate_success"assert ctx.coverage_nudge_count == 1assert ctx.format_nudge_count == 0# ----------------------------------------------------------------------------# Integrated _check_enforcement — no-op-turn bypass closed (main regression)+# Integrated enforcement_decision — no-op-turn bypass closed (main regression)# ---------------------------------------------------------------------------@@ -365,10 +362,10 @@ def test_no_op_turn_bypass_closed_goes_to_phrasing() -> None:"I ran the first block (open_home). The navigation block completed. ""I did not attempt further blocks yet. Next I will proceed.")- nudge = _check_enforcement(ctx, result)+ nudge = enforcement_decision(ctx, result)# Either branch is a valid fix for the regression — verify the format# branch specifically since the coverage heuristic misses this phrasing.- assert nudge == POST_FORMAT_NUDGE+ assert nudge.rule == "post_format"def test_no_op_turn_bypass_closed_multi_action() -> None:@@ -376,8 +373,8 @@ def test_no_op_turn_bypass_closed_multi_action() -> None:(explicit 'go to' + 'download'). The coverage branch fires."""ctx = _post_success_ctx("go to example.com and download the regulations")result = _reply_result("Ran one block; will do the rest next.")- nudge = _check_enforcement(ctx, result)- assert nudge == POST_INTERMEDIATE_SUCCESS_NUDGE+ nudge = enforcement_decision(ctx, result)+ assert nudge.rule == "post_intermediate_success"def test_ask_question_reaches_user_after_any_state() -> None:@@ -386,20 +383,20 @@ def test_ask_question_reaches_user_after_any_state() -> None:incomplete, so the agent can ask for credentials / disambiguate."""ctx = _post_success_ctx("login and download my records")result = _ask_question_result("Which credential should I use for this login?")- assert _check_enforcement(ctx, result) is None+ assert enforcement_decision(ctx, result) is None-def test_check_enforcement_without_result_skips_response_peek() -> None:+def test_enforcement_decision_without_result_skips_response_peek() -> None:"""Pre-screenshot-handoff path passes result=None. State-based branchesstill fire; response peek is skipped."""ctx = _Ctx()ctx.navigate_called = True # but no observation_after_navigate# navigate_enforcement_done is still False- nudge = _check_enforcement(ctx, None)+ nudge = enforcement_decision(ctx, None)assert nudge is not None # navigate nudge fires-def test_check_enforcement_clean_reply_passes_through() -> None:+def test_enforcement_decision_clean_reply_passes_through() -> None:ctx = _post_success_ctx("go to example.com and extract the top 3 stories", block_count=2)result = _reply_result("I created a 2-block workflow that extracts the top 3 stories.")- assert _check_enforcement(ctx, result) is None+ assert enforcement_decision(ctx, result) is Nonediff --git a/tests/unit/test_copilot_scout_act_observe.py b/tests/unit/test_copilot_scout_act_observe.pyindex 2c3bbe685..6a846bfe6 100644--- a/tests/unit/test_copilot_scout_act_observe.py+++ b/tests/unit/test_copilot_scout_act_observe.py@@ -142,6 +142,7 @@ def _ctx(*, server: Any = None, source_url: str | None = _SOURCE_URL) -> SimpleNscout_trajectory=[],challenge_solve_attempts={},organization_id="o_test",+ browser_session_id="bs_test",prior_fill_carry=[],fill_carry_rebound_done=False,pending_scout_source_url=source_url,diff --git a/tests/unit/test_copilot_scout_captcha_solve.py b/tests/unit/test_copilot_scout_captcha_solve.pyindex a048e07c0..440561dae 100644--- a/tests/unit/test_copilot_scout_captcha_solve.py+++ b/tests/unit/test_copilot_scout_captcha_solve.py@@ -15,6 +15,8 @@ from skyvern.forge.sdk.copilot.challenge_evidence import ()from skyvern.forge.sdk.copilot.runtime import AgentContextfrom skyvern.forge.sdk.copilot.tools.scouting import (+ _MAX_CHALLENGE_SOLVE_ATTEMPTS,+ _MAX_CHALLENGE_SOLVE_ATTEMPTS_PER_TURN,_challenge_identity,solve_challenge_when_evidence_settles,)@@ -61,6 +63,7 @@ def _mock_context() -> AgentContext:"""attrs = {"organization_id": "org_123",+ "browser_session_id": "bs_789","workflow_id": "w_456","workflow_permanent_id": "wpid_456","scouted_interactions": [],@@ -75,6 +78,11 @@ def _mock_context() -> AgentContext:return SimpleNamespace(**attrs)+def _mock_page_evidence_cleared() -> dict[str, Any]:+ """What a re-perception sees once the challenge is gone."""+ return {"current_url": "https://example.com/login", "forms": [{"fields": [{"name": "u", "selector": "#u"}]}]}++@pytest.mark.asyncioasync def test_attempt_scout_captcha_solve_no_challenge() -> None:"""When no challenge is detected, returns False without attempting solve."""@@ -124,17 +132,130 @@ async def test_attempt_scout_captcha_solve_challenge_detected_and_solved() -> Nopatch("skyvern.forge.sdk.copilot.tools.scouting._code_block_solve_captcha_builtin",new_callable=AsyncMock,+ return_value=True,) as mock_solve,+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._bounded_code_block_recaptcha_token_populated",+ new_callable=AsyncMock,+ side_effect=[False, True],+ ),):result = await solve_challenge_when_evidence_settles(ctx, page_evidence, url="https://example.com/login", observed_after_interaction=True)assert result is True- mock_solve.assert_called_once()+ assert mock_solve.await_args.kwargs == {"organization_id": "org_123", "browser_session_id": "bs_789"}boundary = ctx.scout_trajectory[-1]assert boundary["tool_name"] == "click"assert composition_challenge_carrier(boundary) is ChallengeEvidenceSource.CHALLENGE_STATE+ assert boundary["challenge_state"]["outcome"] == "solved"+++async def test_an_unsatisfied_widget_is_not_recorded_as_solved() -> None:+ """A widget still rendered and carrying no token was not satisfied."""+ ctx = _mock_context()+ ctx.scout_trajectory = [{"tool_name": "click", "selector": "#s", "source_url": "u"}]+ mock_browser_state = AsyncMock()+ mock_browser_state.get_working_page.return_value = AsyncMock()++ with (+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting.resolve_browser_state_for_context",+ return_value=mock_browser_state,+ ),+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._code_block_solve_captcha_builtin",+ new_callable=AsyncMock,+ return_value=True,+ ),+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._bounded_code_block_recaptcha_token_populated",+ new_callable=AsyncMock,+ side_effect=[False, False],+ ),+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._code_block_recaptcha_response_field_present",+ new_callable=AsyncMock,+ return_value=True,+ ),+ ):+ result = await solve_challenge_when_evidence_settles(+ ctx, _mock_page_evidence_with_challenge(), url="https://example.com/login", observed_after_interaction=True+ )++ assert result is False+ assert ctx.scout_trajectory[-1]["challenge_state"]["outcome"] == "attempted"+++async def test_a_token_that_was_already_there_is_not_credited_as_this_solve() -> None:+ """A response left by an unrelated widget would otherwise let any passing arm claim this+ challenge; only a token that appears across the attempt counts."""+ ctx = _mock_context()+ ctx.scout_trajectory = [{"tool_name": "click", "selector": "#s", "source_url": "u"}]+ mock_browser_state = AsyncMock()+ mock_browser_state.get_working_page.return_value = AsyncMock()++ with (+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting.resolve_browser_state_for_context",+ return_value=mock_browser_state,+ ),+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._code_block_solve_captcha_builtin",+ new_callable=AsyncMock,+ return_value=True,+ ),+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._bounded_code_block_recaptcha_token_populated",+ new_callable=AsyncMock,+ side_effect=[True, True],+ ),+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._code_block_recaptcha_response_field_present",+ new_callable=AsyncMock,+ return_value=True,+ ),+ ):+ result = await solve_challenge_when_evidence_settles(+ ctx, _mock_page_evidence_with_challenge(), url="https://example.com/login", observed_after_interaction=True+ )++ assert result is False+ assert ctx.scout_trajectory[-1]["challenge_state"]["outcome"] == "attempted"+++async def test_an_arm_that_never_ran_is_not_credited_as_a_solve() -> None:+ """No probe matched, so nothing touched the page and nothing may be credited."""+ ctx = _mock_context()+ ctx.scout_trajectory = [{"tool_name": "click", "selector": "#s", "source_url": "u"}]+ mock_browser_state = AsyncMock()+ mock_browser_state.get_working_page.return_value = AsyncMock()++ with (+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting.resolve_browser_state_for_context",+ return_value=mock_browser_state,+ ),+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._code_block_solve_captcha_builtin",+ new_callable=AsyncMock,+ return_value=False,+ ),+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._bounded_code_block_recaptcha_token_populated",+ new_callable=AsyncMock,+ side_effect=[False, True],+ ),+ ):+ result = await solve_challenge_when_evidence_settles(+ ctx, _mock_page_evidence_with_challenge(), url="https://example.com/login", observed_after_interaction=True+ )++ assert result is False@pytest.mark.asyncio@@ -231,7 +352,13 @@ async def test_inspection_capture_solves_without_attributing_the_carrier() -> Nopatch("skyvern.forge.sdk.copilot.tools.scouting._code_block_solve_captcha_builtin",new_callable=AsyncMock,+ return_value=True,) as mock_solve,+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._bounded_code_block_recaptcha_token_populated",+ new_callable=AsyncMock,+ side_effect=[False, True],+ ),):result = await solve_challenge_when_evidence_settles(ctx,@@ -389,7 +516,7 @@ async def test_a_distinct_challenge_gets_its_own_attempts() -> None:# would make this pass whether or not per-challenge keying works.spent = dict(_mock_page_evidence_with_challenge())spent["challenge_controls"] = [{"tag": "div", "id": "c", "data_sitekey": "sitekey-already-spent"}]- ctx.challenge_solve_attempts = {_challenge_identity(spent): 99}+ ctx.challenge_solve_attempts = {_challenge_identity(spent): _MAX_CHALLENGE_SOLVE_ATTEMPTS}mock_browser_state = AsyncMock()mock_browser_state.get_working_page.return_value = AsyncMock()@@ -401,7 +528,13 @@ async def test_a_distinct_challenge_gets_its_own_attempts() -> None:patch("skyvern.forge.sdk.copilot.tools.scouting._code_block_solve_captcha_builtin",new_callable=AsyncMock,+ return_value=True,) as mock_solve,+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._bounded_code_block_recaptcha_token_populated",+ new_callable=AsyncMock,+ side_effect=[False, True],+ ),):result = await solve_challenge_when_evidence_settles(ctx,@@ -475,6 +608,7 @@ async def test_a_state_only_carrier_is_not_reported_as_solved() -> None:patch("skyvern.forge.sdk.copilot.tools.scouting._code_block_solve_captcha_builtin",new_callable=AsyncMock,+ return_value=False,),):await solve_challenge_when_evidence_settles(@@ -482,3 +616,72 @@ async def test_a_state_only_carrier_is_not_reported_as_solved() -> None:)assert ctx.scout_trajectory[-1]["challenge_state"]["outcome"] == "attempted"+++async def test_a_widget_that_rotates_its_identity_cannot_outspend_the_turn() -> None:+ """Every input to the challenge identity is page-controlled, so a page that re-keys its widget+ on each render never reaches the per-identity cap; the turn total is what actually bounds it."""+ ctx = _mock_context()+ ctx.challenge_solve_attempts = {f"spent-{index}": 1 for index in range(_MAX_CHALLENGE_SOLVE_ATTEMPTS_PER_TURN)}+ mock_browser_state = AsyncMock()+ mock_browser_state.get_working_page.return_value = AsyncMock()++ with (+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting.resolve_browser_state_for_context",+ return_value=mock_browser_state,+ ),+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._code_block_solve_captcha_builtin",+ new_callable=AsyncMock,+ return_value=True,+ ) as mock_solve,+ ):+ result = await solve_challenge_when_evidence_settles(+ ctx,+ _mock_page_evidence_with_challenge(),+ url="https://example.com/login",+ observed_after_interaction=False,+ )++ assert result is False+ mock_solve.assert_not_awaited()+++async def test_a_challenge_with_no_response_field_is_credited_on_the_arm_alone() -> None:+ """Turnstile and plain checkbox challenges carry no reCAPTCHA response field, so requiring a+ token transition would report every one of them unsolved however well the arm did."""+ ctx = _mock_context()+ ctx.scout_trajectory = [{"tool_name": "click", "selector": "#s", "source_url": "u"}]+ mock_browser_state = AsyncMock()+ mock_browser_state.get_working_page.return_value = AsyncMock()++ with (+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting.resolve_browser_state_for_context",+ return_value=mock_browser_state,+ ),+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._code_block_solve_captcha_builtin",+ new_callable=AsyncMock,+ return_value=True,+ ),+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._bounded_code_block_recaptcha_token_populated",+ new_callable=AsyncMock,+ side_effect=[False, False],+ ),+ patch(+ "skyvern.forge.sdk.copilot.tools.scouting._code_block_recaptcha_response_field_present",+ new_callable=AsyncMock,+ return_value=False,+ ),+ ):+ result = await solve_challenge_when_evidence_settles(+ ctx, _mock_page_evidence_with_challenge(), url="https://example.com/login", observed_after_interaction=True+ )++ assert result is True+ assert ctx.scout_trajectory[-1]["challenge_state"]["outcome"] == "solved"diff --git a/tests/unit/test_copilot_tools_list_integrations.py b/tests/unit/test_copilot_tools_list_integrations.pyindex 8e0c81e99..f01543e12 100644--- a/tests/unit/test_copilot_tools_list_integrations.py+++ b/tests/unit/test_copilot_tools_list_integrations.py@@ -6,7 +6,6 @@ import pytestfrom skyvern.forge.sdk.copilot.tools.integrations import _list_integrations, _serializefrom skyvern.forge.sdk.schemas.google_oauth import GoogleOAuthCredentialBasefrom skyvern.forge.sdk.schemas.microsoft_oauth import MicrosoftOAuthCredentialBase-from tests.unit.conftest import render_agent_promptORGANIZATION_ID = "o_test_org"@@ -126,11 +125,6 @@ def test_serializer_is_an_allowlist_so_a_new_token_field_cannot_leak() -> None:assert not any("SECRET_" in str(value) for value in entry.values())-def test_prompt_routes_oauth_accounts_to_the_right_lookup() -> None:- rendered = render_agent_prompt()- assert "appear ONLY in `list_integrations`" in rendered--@pytest.mark.asyncioasync def test_lists_a_google_connection_whose_grant_expired(patched_services) -> None:patched_services([_google(state="error")], [])diff --git a/tests/unit/test_copilot_turn_halt.py b/tests/unit/test_copilot_turn_halt.pyindex ef871788d..c45af9ae4 100644--- a/tests/unit/test_copilot_turn_halt.py+++ b/tests/unit/test_copilot_turn_halt.py@@ -9,8 +9,8 @@ from skyvern.forge.sdk.copilot.blocker_signal import (stash_blocker_signal,)from skyvern.forge.sdk.copilot.enforcement import (- _check_enforcement,_maybe_stash_terminal_challenge_halt,+ enforcement_decision,terminal_challenge_blocker_signal_from_current_page_evidence,terminal_challenge_blocker_signal_from_page_evidence,)@@ -125,7 +125,7 @@ def test_enforcement_backstop_converts_existing_terminal_blocker_signal() -> Nonstash_blocker_signal(ctx, signal)with pytest.raises(CopilotTurnHalt) as exc_info:- _check_enforcement(ctx)+ enforcement_decision(ctx)assert ctx.turn_halt is exc_info.value.haltassert exc_info.value.halt.kind == TurnHaltKind.ACTIVE_TERMINAL_CHALLENGEdiff --git a/tests/unit/test_copilot_turn_intent.py b/tests/unit/test_copilot_turn_intent.pyindex 4ed18b282..0bcd40655 100644--- a/tests/unit/test_copilot_turn_intent.py+++ b/tests/unit/test_copilot_turn_intent.py@@ -9,6 +9,7 @@ import pytestfrom pydantic import ValidationErrorfrom skyvern.config import settings+from skyvern.constants import DEFAULT_WORKFLOW_TITLESfrom skyvern.forge.prompts import prompt_enginefrom skyvern.forge.sdk.copilot.agent import (RequestPolicyGuardrailInputs,@@ -32,6 +33,7 @@ from skyvern.forge.sdk.copilot.turn_intent import (_turn_intent_classification_from_raw,build_turn_intent,classify_turn_intent,+ sanitize_workflow_title_candidate,turn_intent_defers_authoring_live_fill,)from skyvern.forge.sdk.schemas.workflow_copilot import (@@ -1575,12 +1577,52 @@ def test_turn_intent_prompt_carries_structural_feasibility_contract() -> None:assert "On the fence" in prompt-def test_turn_intent_prompt_carries_over_asking_guardrails() -> None:+def test_sanitize_workflow_title_candidate_normalizes_and_bounds() -> None:+ assert sanitize_workflow_title_candidate(" Weekly Visitor Report ") == "Weekly Visitor Report"+ assert sanitize_workflow_title_candidate('"Invoice Bot"') == "Invoice Bot"+ assert sanitize_workflow_title_candidate("Report\u202egnitroP") == "ReportgnitroP"+ assert sanitize_workflow_title_candidate("x" * 80) == "x" * 60+++def test_sanitize_workflow_title_candidate_rejects_defaults_and_non_strings() -> None:+ # A candidate equal to a placeholder would defeat the value-based "still unnamed?" test.+ for default_title in DEFAULT_WORKFLOW_TITLES:+ assert sanitize_workflow_title_candidate(default_title) is None+ assert sanitize_workflow_title_candidate("") is None+ assert sanitize_workflow_title_candidate(" ") is None+ assert sanitize_workflow_title_candidate(None) is None+ assert sanitize_workflow_title_candidate(123) is None+++def test_sanitize_workflow_title_candidate_passes_through_secret_redaction() -> None:+ """The persisted title goes through the same redaction seam as prompt-bound text.++ Pattern-based, so this pins the wiring rather than claiming exhaustive coverage.+ """+ title = sanitize_workflow_title_candidate("Sync report password=hunter2horse")+ assert title is not None+ assert "hunter2horse" not in title+ assert "[REDACTED_SECRET]" in title+++def test_classifier_decode_routes_workflow_title_through_the_sink() -> None:+ classification = _turn_intent_classification_from_raw(+ {"mode": "build", "workflow_title": " Weekly Visitor Report "}+ )++ assert classification is not None+ assert classification.workflow_title == "Weekly Visitor Report"+++def test_classifier_decode_drops_default_workflow_title() -> None:+ classification = _turn_intent_classification_from_raw({"mode": "build", "workflow_title": "New Agent"})++ assert classification is not None+ assert classification.workflow_title is None+++def test_turn_intent_prompt_carries_workflow_title_contract() -> None:prompt = _render_turn_intent_prompt()- assert "resolves the target even without a URL" in prompt- assert "never ask which website/URL to use" in prompt- assert "refinements, corrections, and bare-value replies" in prompt- assert "When a concrete URL is present" in prompt- assert "default harder away from structurally_infeasible" in prompt- assert "On the fence: do not use structurally_infeasible" in prompt+ assert "workflow_title" in prompt+ assert "call it Invoice Bot" in promptdiff --git a/tests/unit/test_copilot_turn_ownership.py b/tests/unit/test_copilot_turn_ownership.pyindex e4ad8ed7b..ed82f4769 100644--- a/tests/unit/test_copilot_turn_ownership.py+++ b/tests/unit/test_copilot_turn_ownership.py@@ -2,7 +2,6 @@ from __future__ import annotationsfrom skyvern.forge.sdk.copilot.blocker_signal import (GENUINELY_TERMINAL_BLOCKER_REASON_CODES,- SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE,CopilotToolBlockerSignal,stash_blocker_signal,)@@ -48,23 +47,6 @@ def _churn_signal() -> CopilotToolBlockerSignal:return _signal("code_authoring_guardrail_churn")-_REPLAY_STRUCTURAL_KEY = "b3d8aac91f1ae3a92d60ebd15ebee04216af45b03f18fe39d6deeefa77140e33"---def _synthesized_force_signal() -> CopilotToolBlockerSignal:- return _signal(SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE, blocker_kind="tool_error", renders_final_reply=False)---def _synthesized_replay_context() -> CopilotContext:- ctx = make_copilot_context()- ctx.impose_synthesized_code_block = True- ctx.recorded_build_test_outcome_history = [- {"structural_key": _REPLAY_STRUCTURAL_KEY, "phase": "build_test"},- {"structural_key": _REPLAY_STRUCTURAL_KEY, "phase": "build_test"},- ]- return ctx--def _grant_ladder(ctx: CopilotContext) -> None:ctx.output_contract_actuation_by_signature["sig_a"] = OutputContractAdvisoryState.GRANTED@@ -94,30 +76,6 @@ def test_two_claimants_one_owner_loser_recorded() -> None:assert _conflict_fingerprints(ctx) == ["code_authoring_guardrail_churn>loop_detected"]-def test_r31_three_way_contradiction_yields_single_owner() -> None:- ctx = make_copilot_context()- _grant_ladder(ctx)- claim_turn(ctx, TurnClaimant.OUTPUT_CONTRACT_ACTUATION)-- credential_churn = claim_turn(ctx, TurnClaimant.CREDENTIAL_PRIORITY_CHURN)- persistence_payload = claim_and_stash_blocker_signal(- ctx,- TurnClaimant.SYNTHESIZED_BLOCK_PERSISTENCE_FORCE,- _signal(SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE, blocker_kind="tool_error", renders_final_reply=False),- )-- assert credential_churn is ClaimOutcome.YIELDED- assert persistence_payload is None- assert ctx.blocker_signal is None- owner = current_turn_owner(ctx)- assert owner is not None- assert owner.claimant is TurnClaimant.OUTPUT_CONTRACT_ACTUATION- assert sorted(_conflict_fingerprints(ctx)) == [- "output_contract_actuation>credential_priority_authoring_churn",- "output_contract_actuation>synthesized_block_persistence_force",- ]--def test_precedence_order_covers_every_claimant_once() -> None:assert len(set(_PRECEDENCE_ORDER)) == len(_PRECEDENCE_ORDER)assert set(_PRECEDENCE_ORDER) == set(TurnClaimant)@@ -234,48 +192,6 @@ def test_unclaimed_signal_fails_open_to_render_while_owner_live() -> None:assert ctx.gate_precedence_conflict_events == []-def test_transient_claim_never_outlives_the_claiming_call() -> None:- ctx = make_copilot_context()- assert claim_turn(ctx, TurnClaimant.CREDENTIAL_SCOUT_REOPEN) is ClaimOutcome.OWNED- assert current_turn_owner(ctx) is None-- churn = _churn_signal()- assert claim_and_stash_blocker_signal(ctx, TurnClaimant.CODE_AUTHORING_CHURN, churn) is not None- assert blocker_signal_render_allowed(ctx, churn) is True---def test_transient_claim_yields_to_live_ladder_and_records_conflict() -> None:- ctx = make_copilot_context()- _grant_ladder(ctx)- assert claim_turn(ctx, TurnClaimant.CREDENTIAL_SCOUT_REOPEN) is ClaimOutcome.YIELDED- assert _conflict_fingerprints(ctx) == ["output_contract_actuation>credential_scout_reopen"]---def test_carve_out_claimants_are_transient() -> None:- ctx = make_copilot_context()- assert claim_turn(ctx, TurnClaimant.ACTUATION_OBLIGATION_FILL) is ClaimOutcome.OWNED- assert claim_turn(ctx, TurnClaimant.CREDENTIAL_SCOUT_REOPEN) is ClaimOutcome.OWNED- assert current_turn_owner(ctx) is None---def test_credential_priority_churn_yields_only_to_ladder_and_terminal() -> None:- ctx = make_copilot_context()- _grant_ladder(ctx)- signal = _signal("credential_priority_authoring_churn")- assert claim_and_stash_blocker_signal(ctx, TurnClaimant.CREDENTIAL_PRIORITY_CHURN, signal) is None-- ctx2 = make_copilot_context()- assert claim_and_stash_blocker_signal(- ctx2,- TurnClaimant.SYNTHESIZED_BLOCK_PERSISTENCE_FORCE,- _signal(SYNTHESIZED_BLOCK_PERSISTENCE_REASON_CODE, blocker_kind="tool_error", renders_final_reply=False),- )- credential_signal = _signal("credential_priority_authoring_churn")- assert claim_and_stash_blocker_signal(ctx2, TurnClaimant.CREDENTIAL_PRIORITY_CHURN, credential_signal) is not None- assert ctx2.blocker_signal is credential_signal- assert blocker_signal_render_allowed(ctx2, credential_signal) is True--def test_emit_blocker_signal_payload_returns_payload_even_on_yield() -> None:ctx = make_copilot_context()_grant_ladder(ctx)diff --git a/tests/unit/test_failure_classifier.py b/tests/unit/test_failure_classifier.pyindex 06e02ff84..a1b0bb8b6 100644--- a/tests/unit/test_failure_classifier.py+++ b/tests/unit/test_failure_classifier.py@@ -325,6 +325,60 @@ def test_ordinary_user_code_failure_is_not_infrastructure() -> None:assert "INFRASTRUCTURE_ERROR" not in categories+def test_secure_codeblock_runner_internal_failure_classifies_as_infrastructure() -> None:+ reason = "code block failed. failure reason: Secure CodeBlock runner failed before completing. Please retry."+ result = _classify(reason, fallback_to_unknown=True)++ assert result[0]["category"] == "INFRASTRUCTURE_ERROR"+ assert result[0]["reason_code"] == "secure_codeblock_runner_internal"+ assert "UNKNOWN" not in [entry["category"] for entry in result]+++def test_secure_codeblock_sandbox_exited_carries_its_own_reason_code() -> None:+ reason = (+ "code block failed. failure reason: Secure CodeBlock sandbox process exited before completing. Please retry."+ )+ result = _classify(reason, fallback_to_unknown=True)++ infra = [entry for entry in result if entry["category"] == "INFRASTRUCTURE_ERROR"]+ assert len(infra) == 1+ assert infra[0]["reason_code"] == "secure_codeblock_sandbox_exited"+++def test_secure_codeblock_sandbox_exited_ranks_below_the_unambiguous_runner_arms() -> None:+ # child_exited can also be user code self-terminating, so its confidence must stay+ # strictly below the runner-internal arm's.+ exited = _classify(+ "code block failed. failure reason: Secure CodeBlock sandbox process exited before completing. Please retry.",+ fallback_to_unknown=True,+ )+ internal = _classify(+ "code block failed. failure reason: Secure CodeBlock runner failed before completing. Please retry.",+ fallback_to_unknown=True,+ )++ assert exited[0]["confidence_float"] < internal[0]["confidence_float"]+++def test_secure_codeblock_runner_busy_classifies_as_infrastructure() -> None:+ reason = "code block failed. failure reason: CodeBlock runner is already executing another CodeBlock. Please retry."+ result = _classify(reason, fallback_to_unknown=True)++ assert result[0]["category"] == "INFRASTRUCTURE_ERROR"+ assert result[0]["reason_code"] == "secure_codeblock_runner_busy"+++def test_user_code_crash_mentioning_process_exit_is_not_infrastructure() -> None:+ # User-code detail prose can echo the words of the sandbox-exit message; only the+ # runner-authored literal may classify as infrastructure.+ reason = (+ "code block failed. failure reason: CodeBlock failed with RuntimeError at line 3: process exited with code 1."+ )+ categories = _categories_for(reason, fallback_to_unknown=True)++ assert "INFRASTRUCTURE_ERROR" not in categories++@pytest.mark.asyncioasync def test_page_analysis_timeout_reason_ranks_page_load_timeout() -> None:browser_state = MagicMock()diff --git a/tests/unit/test_files_resolve_local_or_download.py b/tests/unit/test_files_resolve_local_or_download.pyindex 3b0db06ae..00836b3e4 100644--- a/tests/unit/test_files_resolve_local_or_download.py+++ b/tests/unit/test_files_resolve_local_or_download.py@@ -1,6 +1,8 @@from __future__ import annotations+import jsonfrom collections.abc import AsyncIterator+from datetime import datetime, timezonefrom pathlib import Pathfrom unittest.mock import AsyncMock, MagicMock@@ -10,7 +12,16 @@ from multidict import CIMultiDict, CIMultiDictProxyfrom skyvern.config import settingsfrom skyvern.exceptions import DownloadFileMaxSizeExceeded+from skyvern.forge import app as forge_appfrom skyvern.forge.sdk.api import files+from skyvern.forge.sdk.artifact.manager import ArtifactManager+from skyvern.forge.sdk.artifact.models import Artifact, ArtifactType+from skyvern.forge.sdk.artifact.signing import (+ parse_artifact_content_url,+ parse_keyring,+ sign_artifact_url,+ verify_artifact_signature,+)class _FakeDownloadResponse:@@ -251,3 +262,114 @@ async def test_download_file_raises_http_error_without_aiohttp_auto_raise(assert captured_session_kwargs.get("raise_for_status") is not Trueassert not response.body_readassert list(tmp_path.iterdir()) == []+++# ---------------------------------------------------------------------------+# First-party artifact URL recovery (SKY-13575)+# ---------------------------------------------------------------------------++_BASE_URL = "https://api.example.com"+_KEYRING_JSON = json.dumps({"current_kid": "k1", "keys": {"k1": {"secret": "0" * 64}}})+++def _artifact(artifact_id: str = "a_1", organization_id: str = "org-1") -> Artifact:+ now = datetime(2026, 8, 5, tzinfo=timezone.utc)+ return Artifact(+ artifact_id=artifact_id,+ artifact_type=ArtifactType.DOWNLOAD,+ uri="s3://bucket/downloads/docs_5.pdf",+ organization_id=organization_id,+ created_at=now,+ modified_at=now,+ )+++def _patch_artifact_lookup(monkeypatch: pytest.MonkeyPatch, artifact: Artifact | None) -> AsyncMock:+ """Wire a real ArtifactManager against fake artifact/organization repositories."""+ monkeypatch.setattr(settings, "SKYVERN_BASE_URL", _BASE_URL)+ monkeypatch.setattr(settings, "ARTIFACT_CONTENT_HMAC_KEYRING", _KEYRING_JSON)+ get_artifact_by_id = AsyncMock(return_value=artifact)+ database = MagicMock()+ database.artifacts.get_artifact_by_id = get_artifact_by_id+ database.organizations.get_organization = AsyncMock(return_value=None)+ monkeypatch.setattr(forge_app, "DATABASE", database)+ monkeypatch.setattr(forge_app, "ARTIFACT_MANAGER", ArtifactManager())+ return get_artifact_by_id+++def _corrupt_signature(url: str, drop_index: int = 13) -> str:+ """Drop a single character from the middle of the URL's signature."""+ head, sig = url.split("&sig=")+ return f"{head}&sig={sig[:drop_index]}{sig[drop_index + 1 :]}"+++async def test_resolve_remints_first_party_url_with_corrupted_signature(monkeypatch: pytest.MonkeyPatch) -> None:+ get_artifact_by_id = _patch_artifact_lookup(monkeypatch, _artifact())+ download_mock = AsyncMock(return_value="/tmp/docs_5.pdf")+ monkeypatch.setattr(files, "download_file", download_mock)+ signed = sign_artifact_url(_BASE_URL, "a_1", parse_keyring(_KEYRING_JSON))+ corrupted = _corrupt_signature(signed)++ await files.resolve_local_or_download_file(corrupted, "wr_1", organization_id="org-1")++ get_artifact_by_id.assert_awaited_once_with(artifact_id="a_1", organization_id="org-1")+ downloaded_url = download_mock.await_args.args[0]+ assert downloaded_url != corrupted+ parsed = parse_artifact_content_url(downloaded_url, _BASE_URL)+ assert parsed is not None+ assert verify_artifact_signature(+ "a_1", parsed.expiry or "", parsed.kid or "", parsed.sig or "", parse_keyring(_KEYRING_JSON)+ )+++async def test_resolve_remints_expired_first_party_url(monkeypatch: pytest.MonkeyPatch) -> None:+ _patch_artifact_lookup(monkeypatch, _artifact())+ download_mock = AsyncMock(return_value="/tmp/docs_5.pdf")+ monkeypatch.setattr(files, "download_file", download_mock)+ expired = sign_artifact_url(_BASE_URL, "a_1", parse_keyring(_KEYRING_JSON), expiry_seconds=-60)++ await files.resolve_local_or_download_file(expired, "wr_1", organization_id="org-1")++ assert download_mock.await_args.args[0] != expired+++async def test_resolve_leaves_valid_first_party_url_untouched(monkeypatch: pytest.MonkeyPatch) -> None:+ get_artifact_by_id = _patch_artifact_lookup(monkeypatch, _artifact())+ download_mock = AsyncMock(return_value="/tmp/docs_5.pdf")+ monkeypatch.setattr(files, "download_file", download_mock)+ signed = sign_artifact_url(_BASE_URL, "a_1", parse_keyring(_KEYRING_JSON))++ await files.resolve_local_or_download_file(signed, "wr_1", organization_id="org-1")++ assert download_mock.await_args.args[0] == signed+ get_artifact_by_id.assert_not_awaited()+++async def test_resolve_leaves_foreign_url_untouched(monkeypatch: pytest.MonkeyPatch) -> None:+ get_artifact_by_id = _patch_artifact_lookup(monkeypatch, _artifact())+ download_mock = AsyncMock(return_value="/tmp/file.pdf")+ monkeypatch.setattr(files, "download_file", download_mock)+ foreign = "https://evil.example.com/v1/artifacts/a_1/content?expiry=1&kid=k1&sig=short"++ await files.resolve_local_or_download_file(foreign, "wr_1", organization_id="org-1")++ assert download_mock.await_args.args[0] == foreign+ get_artifact_by_id.assert_not_awaited()+++async def test_resolve_does_not_remint_artifact_owned_by_another_organization(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ _patch_artifact_lookup(monkeypatch, None)+ download_mock = AsyncMock(return_value="/tmp/docs_5.pdf")+ monkeypatch.setattr(files, "download_file", download_mock)+ corrupted = _corrupt_signature(sign_artifact_url(_BASE_URL, "a_1", parse_keyring(_KEYRING_JSON)))++ await files.resolve_local_or_download_file(corrupted, "wr_1", organization_id="org-1")++ assert download_mock.await_args.args[0] == corrupteddiff --git a/tests/unit/test_real_browser_manager.py b/tests/unit/test_real_browser_manager.pyindex 488425519..2aa1f10b1 100644--- a/tests/unit/test_real_browser_manager.py+++ b/tests/unit/test_real_browser_manager.py@@ -794,6 +794,33 @@ async def test_get_video_artifacts_non_webm_skips_ffmpeg(tmp_path) -> None:assert artifacts[0].video_data == b"mp4-bytes"+async def test_get_video_artifacts_empty_snapshot_path_does_not_warn() -> None:+ """The finalize=False snapshot path runs once per step on browsers that never record+ locally (vendor/CDP/persistent sessions), where an empty list is the expected state."""+ browser_state = MagicMock()+ browser_state.browser_artifacts.video_artifacts = []++ with patch.object(real_browser_manager, "LOG") as mock_log:+ artifacts = await RealBrowserManager().get_video_artifacts(browser_state=browser_state, finalize=False)++ assert artifacts == []+ mock_log.warning.assert_not_called()+++async def test_get_video_artifacts_empty_finalize_path_warns() -> None:+ """At finalize time the browser is closing, so a missing recording is a real signal."""+ browser_state = MagicMock()+ browser_state.browser_artifacts.video_artifacts = []++ with patch.object(real_browser_manager, "LOG") as mock_log:+ artifacts = await RealBrowserManager().get_video_artifacts(browser_state=browser_state)++ assert artifacts == []+ mock_log.warning.assert_called_once()++def _make_page_mock(video_path: str | None) -> MagicMock:page = MagicMock()if video_path is None:diff --git a/tests/unit/test_workflow_copilot_diagnostic_intent.py b/tests/unit/test_workflow_copilot_diagnostic_intent.pyindex 5a16ec652..fc6dd86b4 100644--- a/tests/unit/test_workflow_copilot_diagnostic_intent.py+++ b/tests/unit/test_workflow_copilot_diagnostic_intent.py@@ -26,36 +26,6 @@ class TestDiagnosticObservationIntent:assert "DIAGNOSTIC / OBSERVATIONAL COMPLAINTS" not in renderedassert "Explicit edit/debug requests remain edit requests" not in rendered- def test_agent_prompt_routes_diagnostic_followups_to_edit_when_context_resolves(self) -> None:- rendered = _render_agent_prompt()-- assert (- "This includes diagnostic symptom follow-ups on the same workflow after an explicit edit goal" in rendered- )- assert "diagnostic-after-edit - prior turn asked to consolidate login blocks" in rendered- assert "connect the existing block chain and call `update_and_run_blocks` once" in rendered-- def test_agent_prompt_keeps_unresolved_diagnostics_as_clarification(self) -> None:- rendered = _render_agent_prompt()-- assert "when a diagnostic complaint has no prior edit goal" in rendered- assert "mismatched/anonymized block references cannot be resolved" in rendered- assert "latest says to add a conditional to a URL without saying when -> ask for the condition" in rendered- assert "ask naming both missing labels and candidate blocks" in rendered-- def test_agent_prompt_avoids_debug_tools_before_clear_structural_edit(self) -> None:- rendered = _render_agent_prompt()-- assert "When the workflow structure itself is enough to make the edit" in rendered- assert (- "do not use direct browser tools, `get_run_results`, `run_blocks_and_collect_debug`, `update_workflow`"- in rendered- )- assert (- "Do not copy explanatory chat-history prose, example Jinja placeholders, or diagnostic transcripts"- in rendered- )-def test_block_running_tools_share_consolidated_diagnostic_routing(self) -> None:for tool in (run_blocks_tool, update_and_run_blocks_tool):desc = tool.description # type: ignore[attr-defined]@@ -100,10 +70,3 @@ class TestDiagnosticAbsentEntity:section = rendered[start:end]assert "workflow-knowledge-base prose" in sectionassert "KB" not in section-- def test_docs_inline_disambiguator_defers_to_ask_vs_edit_routing(self) -> None:- rendered = _render_agent_prompt()-- assert "Docs-inline does NOT apply to a runtime symptom" in rendered- assert "a block label, workflow parameter key, or `{{ name.output... }}` Jinja reference" in rendered- assert "use the ASK-vs-EDIT routing below" in rendereddiff --git a/tests/unit/test_workflow_copilot_route.py b/tests/unit/test_workflow_copilot_route.pyindex 5f59125d2..622f7c59b 100644--- a/tests/unit/test_workflow_copilot_route.py+++ b/tests/unit/test_workflow_copilot_route.py@@ -74,6 +74,33 @@ def _default_terminal_envelope_render_flag(monkeypatch: pytest.MonkeyPatch) -> N)+def organization() -> SimpleNamespace:+ return SimpleNamespace(organization_id="org-1")+++def anon_request() -> MagicMock:+ request = MagicMock()+ request.headers = {}+ return request+++def api_key_request() -> MagicMock:+ request = MagicMock()+ request.headers = {"x-api-key": "sk-test-key"}+ return request+++def copilot_stream() -> MagicMock:+ stream = MagicMock()+ stream.send = AsyncMock(return_value=True)+ stream.is_disconnected = AsyncMock(return_value=False)+ return stream++def _make_chat_request(mode: str | None = None, code_block: bool | None = None, keep_pending_proposal: bool = False) -> WorkflowCopilotChatRequest:@@ -1073,7 +1100,9 @@ def _install_mock_provider(@pytest.mark.asyncio-async def test_request_mode_ask_forces_v1_over_flag_on(monkeypatch: pytest.MonkeyPatch) -> None:+async def test_request_mode_ask_forces_v1_over_flag_on(+ monkeypatch: pytest.MonkeyPatch, anon_request: MagicMock, organization: SimpleNamespace+) -> None:"""mode='ask' must take the v1 path even when the settings flag is on."""monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)@@ -1085,18 +1114,16 @@ async def test_request_mode_ask_forces_v1_over_flag_on(monkeypatch: pytest.Monkecaptured = install_fake_create(monkeypatch)- request = MagicMock()- request.headers = {}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(mode="ask"), organization)+ response = await workflow_copilot_chat_post(anon_request, _make_chat_request(mode="ask"), organization)assert response is captured["sentinel"]new_copilot_mock.assert_not_awaited()@pytest.mark.asyncio-async def test_request_mode_build_forces_v2_over_flag_off(monkeypatch: pytest.MonkeyPatch) -> None:+async def test_request_mode_build_forces_v2_over_flag_off(+ monkeypatch: pytest.MonkeyPatch, anon_request: MagicMock, organization: SimpleNamespace+) -> None:"""mode='build' must take the v2 path even when the settings flag is off."""monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", False)_install_mock_provider(monkeypatch, return_value=False)@@ -1108,18 +1135,16 @@ async def test_request_mode_build_forces_v2_over_flag_off(monkeypatch: pytest.Monew_copilot_mock,)- request = MagicMock()- request.headers = {}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(mode="build"), organization)+ response = await workflow_copilot_chat_post(anon_request, _make_chat_request(mode="build"), organization)assert response is sentinelnew_copilot_mock.assert_awaited_once()@pytest.mark.asyncio-async def test_request_mode_absent_follows_flag(monkeypatch: pytest.MonkeyPatch) -> None:+async def test_request_mode_absent_follows_flag(+ monkeypatch: pytest.MonkeyPatch, anon_request: MagicMock, organization: SimpleNamespace+) -> None:"""mode absent (None) keeps following the settings flag in both directions."""new_copilot_mock = AsyncMock(return_value=object())monkeypatch.setattr(@@ -1128,18 +1153,14 @@ async def test_request_mode_absent_follows_flag(monkeypatch: pytest.MonkeyPatch))captured = install_fake_create(monkeypatch)- request = MagicMock()- request.headers = {}- organization = SimpleNamespace(organization_id="org-1")-monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", False)_install_mock_provider(monkeypatch, return_value=False)- response = await workflow_copilot_chat_post(request, _make_chat_request(mode=None), organization)+ response = await workflow_copilot_chat_post(anon_request, _make_chat_request(mode=None), organization)assert response is captured["sentinel"]new_copilot_mock.assert_not_awaited()monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)- response = await workflow_copilot_chat_post(request, _make_chat_request(mode=None), organization)+ response = await workflow_copilot_chat_post(anon_request, _make_chat_request(mode=None), organization)assert response is new_copilot_mock.return_valuenew_copilot_mock.assert_awaited_once()@@ -1167,6 +1188,8 @@ async def test_flag_on_mid_stream_disconnect_restores_when_persisted_and_not_autworkflow_was_persisted: bool,has_valid_proposal: bool,expect_restore: bool,+ api_key_request: MagicMock,+ organization: SimpleNamespace,) -> None:monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)@@ -1201,11 +1224,7 @@ async def test_flag_on_mid_stream_disconnect_restores_when_persisted_and_not_autrestore_mock, _ = setup_new_copilot_mocks(monkeypatch, chat, original_workflow, agent_result)- request = MagicMock()- request.headers = {"x-api-key": "sk-test-key"}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(), organization)+ response = await workflow_copilot_chat_post(api_key_request, _make_chat_request(), organization)assert response is captured["sentinel"]stream = MagicMock()@@ -1225,7 +1244,9 @@ async def test_flag_on_mid_stream_disconnect_restores_when_persisted_and_not_aut@pytest.mark.asyncio-async def test_flag_on_pre_agent_failure_persists_recoverable_reply(monkeypatch: pytest.MonkeyPatch) -> None:+async def test_flag_on_pre_agent_failure_persists_recoverable_reply(+ monkeypatch: pytest.MonkeyPatch, anon_request: MagicMock, copilot_stream: MagicMock, organization: SimpleNamespace+) -> None:monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)captured = install_fake_create(monkeypatch)@@ -1282,20 +1303,12 @@ async def test_flag_on_pre_agent_failure_persists_recoverable_reply(monkeypatch:app.AGENT_FUNCTION.get_copilot_config_for_request = AsyncMock(return_value=None)app.AGENT_FUNCTION.resolve_org_api_key = AsyncMock(return_value="sk-test-key")- request = MagicMock()- request.headers = {}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(), organization)+ response = await workflow_copilot_chat_post(anon_request, _make_chat_request(), organization)assert response is captured["sentinel"]- stream = MagicMock()- stream.send = AsyncMock(return_value=True)- stream.is_disconnected = AsyncMock(return_value=False)-handler = captured["handler"]assert callable(handler)- await handler(stream)+ await handler(copilot_stream)contents = [call.kwargs.get("content") for call in workflow_params.create_workflow_copilot_chat_message.await_args_list@@ -1308,7 +1321,7 @@ async def test_flag_on_pre_agent_failure_persists_recoverable_reply(monkeypatch:assert "The workflow was not modified" in assistant_contents[0]assert "reference cpe_" in assistant_contents[0]- frames = [call.args[0] for call in stream.send.await_args_list if call.args]+ frames = [call.args[0] for call in copilot_stream.send.await_args_list if call.args]response_frames = [frame for frame in frames if isinstance(frame, WorkflowCopilotStreamResponseUpdate)]assert response_framesassert response_frames[-1].narrative_payload is not None@@ -1328,6 +1341,9 @@ async def test_flag_on_route_error_after_chat_persists_recoverable_reply(monkeypatch: pytest.MonkeyPatch,error: BaseException,expected_summary: str,+ api_key_request: MagicMock,+ copilot_stream: MagicMock,+ organization: SimpleNamespace,) -> None:monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)captured = install_fake_create(monkeypatch)@@ -1361,20 +1377,12 @@ async def test_flag_on_route_error_after_chat_persists_recoverable_reply(AsyncMock(side_effect=error),)- request = MagicMock()- request.headers = {"x-api-key": "sk-test-key"}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(), organization)+ response = await workflow_copilot_chat_post(api_key_request, _make_chat_request(), organization)assert response is captured["sentinel"]- stream = MagicMock()- stream.send = AsyncMock(return_value=True)- stream.is_disconnected = AsyncMock(return_value=False)-handler = captured["handler"]assert callable(handler)- await handler(stream)+ await handler(copilot_stream)contents = [call.kwargs.get("content")@@ -1400,7 +1408,7 @@ async def test_flag_on_route_error_after_chat_persists_recoverable_reply(assert turn_outcome.copilot_effective_mode == "build"assert turn_outcome.copilot_turn_id is not None- frames = [call.args[0] for call in stream.send.await_args_list if call.args]+ frames = [call.args[0] for call in copilot_stream.send.await_args_list if call.args]response_frames = [frame for frame in frames if isinstance(frame, WorkflowCopilotStreamResponseUpdate)]assert response_framesassert response_frames[-1].narrative_payload is not None@@ -1411,6 +1419,9 @@ async def test_flag_on_route_error_after_chat_persists_recoverable_reply(@pytest.mark.asyncioasync def test_route_error_after_restore_reports_workflow_not_modified(monkeypatch: pytest.MonkeyPatch,+ api_key_request: MagicMock,+ copilot_stream: MagicMock,+ organization: SimpleNamespace,) -> None:monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)captured = install_fake_create(monkeypatch)@@ -1450,20 +1461,12 @@ async def test_route_error_after_restore_reports_workflow_not_modified(monkeypatch.setattr(workflow_copilot_route, "_finalise_normal_turn", flaky_finalise)- request = MagicMock()- request.headers = {"x-api-key": "sk-test-key"}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(), organization)+ response = await workflow_copilot_chat_post(api_key_request, _make_chat_request(), organization)assert response is captured["sentinel"]- stream = MagicMock()- stream.send = AsyncMock(return_value=True)- stream.is_disconnected = AsyncMock(return_value=False)-handler = captured["handler"]assert callable(handler)- await handler(stream)+ await handler(copilot_stream)restore_mock.assert_awaited_once()assert len(finalise_results) == 2@@ -1493,6 +1496,9 @@ async def test_route_error_after_restore_reports_workflow_not_modified(async def test_route_error_after_restore_keeps_bypassed_proposal_when_keep_pending(monkeypatch: pytest.MonkeyPatch,raised_error: BaseException,+ api_key_request: MagicMock,+ copilot_stream: MagicMock,+ organization: SimpleNamespace,) -> None:# Route-level (not direct-function-call) pin: keep_pending_proposal must# reach both exception-recovery call sites, not just _persist_proposed_workflow_state.@@ -1534,20 +1540,14 @@ async def test_route_error_after_restore_keeps_bypassed_proposal_when_keep_pendimonkeypatch.setattr(workflow_copilot_route, "_finalise_normal_turn", flaky_finalise)- request = MagicMock()- request.headers = {"x-api-key": "sk-test-key"}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(keep_pending_proposal=True), organization)+ response = await workflow_copilot_chat_post(+ api_key_request, _make_chat_request(keep_pending_proposal=True), organization+ )assert response is captured["sentinel"]- stream = MagicMock()- stream.send = AsyncMock(return_value=True)- stream.is_disconnected = AsyncMock(return_value=False)-handler = captured["handler"]assert callable(handler)- await handler(stream)+ await handler(copilot_stream)restore_mock.assert_awaited_once()assert len(finalise_results) == 2@@ -1567,6 +1567,9 @@ async def test_route_error_after_restore_keeps_bypassed_proposal_when_keep_pendiasync def test_route_error_honors_real_agent_explicit_clear_despite_keep_pending(monkeypatch: pytest.MonkeyPatch,raised_error: BaseException,+ api_key_request: MagicMock,+ copilot_stream: MagicMock,+ organization: SimpleNamespace,) -> None:# The real (pre-exception) agent_result can itself carry clear_proposed_workflow=True;# the recovery path must not silently drop that signal just because it's rebuilding@@ -1609,20 +1612,14 @@ async def test_route_error_honors_real_agent_explicit_clear_despite_keep_pendingmonkeypatch.setattr(workflow_copilot_route, "_finalise_normal_turn", flaky_finalise)- request = MagicMock()- request.headers = {"x-api-key": "sk-test-key"}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(keep_pending_proposal=True), organization)+ response = await workflow_copilot_chat_post(+ api_key_request, _make_chat_request(keep_pending_proposal=True), organization+ )assert response is captured["sentinel"]- stream = MagicMock()- stream.send = AsyncMock(return_value=True)- stream.is_disconnected = AsyncMock(return_value=False)-handler = captured["handler"]assert callable(handler)- await handler(stream)+ await handler(copilot_stream)restore_mock.assert_awaited_once()assert len(finalise_results) == 2@@ -1644,6 +1641,9 @@ async def test_route_error_honors_real_agent_explicit_clear_despite_keep_pendingasync def test_route_error_after_staged_commit_clears_stale_proposal_despite_keep_pending(monkeypatch: pytest.MonkeyPatch,raised_error: BaseException,+ api_key_request: MagicMock,+ copilot_stream: MagicMock,+ organization: SimpleNamespace,) -> None:# An auto-accept turn eligible for a staged commit (has_staged_proposal=True) that# then hits an exception elsewhere in finalisation still invalidates a stale kept@@ -1689,20 +1689,14 @@ async def test_route_error_after_staged_commit_clears_stale_proposal_despite_keemonkeypatch.setattr(workflow_copilot_route, "_finalise_normal_turn", flaky_finalise)- request = MagicMock()- request.headers = {"x-api-key": "sk-test-key"}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(keep_pending_proposal=True), organization)+ response = await workflow_copilot_chat_post(+ api_key_request, _make_chat_request(keep_pending_proposal=True), organization+ )assert response is captured["sentinel"]- stream = MagicMock()- stream.send = AsyncMock(return_value=True)- stream.is_disconnected = AsyncMock(return_value=False)-handler = captured["handler"]assert callable(handler)- await handler(stream)+ await handler(copilot_stream)restore_mock.assert_not_awaited()assert len(finalise_results) == 2@@ -1774,6 +1768,9 @@ async def test_finalise_normal_turn_clears_stale_proposal_when_rollback_itself_fasync def test_route_error_recovery_clears_stale_proposal_when_its_own_rollback_fails(monkeypatch: pytest.MonkeyPatch,raised_error: BaseException,+ api_key_request: MagicMock,+ copilot_stream: MagicMock,+ organization: SimpleNamespace,) -> None:# The recovery block's OWN restore attempt (not the main flow's — flaky_finalise# bypasses that entirely) can itself fail; that must still force-clear a kept@@ -1817,20 +1814,14 @@ async def test_route_error_recovery_clears_stale_proposal_when_its_own_rollback_monkeypatch.setattr(workflow_copilot_route, "_finalise_normal_turn", flaky_finalise)- request = MagicMock()- request.headers = {"x-api-key": "sk-test-key"}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(keep_pending_proposal=True), organization)+ response = await workflow_copilot_chat_post(+ api_key_request, _make_chat_request(keep_pending_proposal=True), organization+ )assert response is captured["sentinel"]- stream = MagicMock()- stream.send = AsyncMock(return_value=True)- stream.is_disconnected = AsyncMock(return_value=False)-handler = captured["handler"]assert callable(handler)- await handler(stream)+ await handler(copilot_stream)assert len(finalise_results) == 2recovered_result = finalise_results[1]@@ -1849,6 +1840,9 @@ async def test_route_error_recovery_clears_stale_proposal_when_its_own_rollback_async def test_route_error_before_write_keeps_older_proposal_despite_attempted_fresh_draft(monkeypatch: pytest.MonkeyPatch,raised_error: BaseException,+ api_key_request: MagicMock,+ copilot_stream: MagicMock,+ organization: SimpleNamespace,) -> None:# Negative companion to test_route_error_after_real_fresh_write_clears_it_even_with_# no_prior_proposal: agent_result.updated_workflow being SET only means a write was@@ -1874,7 +1868,7 @@ async def test_route_error_before_write_keeps_older_proposal_despite_attempted_f)agent_result = SimpleNamespace(user_response="unused",- updated_workflow=SimpleNamespace(model_dump=lambda mode: {"title": "fresh draft"}),+ updated_workflow=SimpleNamespace(title="fresh draft", model_dump=lambda mode: {"title": "fresh draft"}),global_llm_context=None,workflow_yaml="title: fresh draft\n",workflow_was_persisted=False,@@ -1896,20 +1890,14 @@ async def test_route_error_before_write_keeps_older_proposal_despite_attempted_fmonkeypatch.setattr(workflow_copilot_route, "_finalise_normal_turn", flaky_finalise)- request = MagicMock()- request.headers = {"x-api-key": "sk-test-key"}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(keep_pending_proposal=True), organization)+ response = await workflow_copilot_chat_post(+ api_key_request, _make_chat_request(keep_pending_proposal=True), organization+ )assert response is captured["sentinel"]- stream = MagicMock()- stream.send = AsyncMock(return_value=True)- stream.is_disconnected = AsyncMock(return_value=False)-handler = captured["handler"]assert callable(handler)- await handler(stream)+ await handler(copilot_stream)restore_mock.assert_not_awaited()assert len(finalise_results) == 2@@ -1921,6 +1909,9 @@ async def test_route_error_before_write_keeps_older_proposal_despite_attempted_f@pytest.mark.asyncioasync def test_route_error_after_real_fresh_write_clears_it_even_with_no_prior_proposal(monkeypatch: pytest.MonkeyPatch,+ api_key_request: MagicMock,+ copilot_stream: MagicMock,+ organization: SimpleNamespace,) -> None:# Unlike the flaky_finalise-based tests above (which bypass _finalise_normal_turn's# real body entirely), this lets the REAL first attempt genuinely write the fresh@@ -1947,7 +1938,7 @@ async def test_route_error_after_real_fresh_write_clears_it_even_with_no_prior_p)agent_result = SimpleNamespace(user_response="Here is your draft.",- updated_workflow=SimpleNamespace(model_dump=lambda mode: {"title": "fresh draft"}),+ updated_workflow=SimpleNamespace(title="fresh draft", model_dump=lambda mode: {"title": "fresh draft"}),global_llm_context=None,workflow_yaml="title: fresh draft\n",workflow_was_persisted=False,@@ -1969,20 +1960,14 @@ async def test_route_error_after_real_fresh_write_clears_it_even_with_no_prior_pworkflow_params.create_workflow_copilot_chat_message = AsyncMock(side_effect=flaky_create_message)- request = MagicMock()- request.headers = {"x-api-key": "sk-test-key"}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(keep_pending_proposal=True), organization)+ response = await workflow_copilot_chat_post(+ api_key_request, _make_chat_request(keep_pending_proposal=True), organization+ )assert response is captured["sentinel"]- stream = MagicMock()- stream.send = AsyncMock(return_value=True)- stream.is_disconnected = AsyncMock(return_value=False)-handler = captured["handler"]assert callable(handler)- await handler(stream)+ await handler(copilot_stream)update_calls = workflow_params.update_workflow_copilot_chat.await_args_listwrite_calls = [c for c in update_calls if c.kwargs.get("proposed_workflow") is not None]@@ -2042,6 +2027,9 @@ async def test_proposed_workflow_cleared_on_restore(prior_proposal: dict | None,clear_proposed_flag: bool,expect_clear_call: bool,+ api_key_request: MagicMock,+ copilot_stream: MagicMock,+ organization: SimpleNamespace,) -> None:monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)@@ -2076,20 +2064,12 @@ async def test_proposed_workflow_cleared_on_restore(setup_new_copilot_mocks(monkeypatch, chat, original_workflow, agent_result)- request = MagicMock()- request.headers = {"x-api-key": "sk-test-key"}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(), organization)+ response = await workflow_copilot_chat_post(api_key_request, _make_chat_request(), organization)assert response is captured["sentinel"]- stream = MagicMock()- stream.send = AsyncMock(return_value=True)- stream.is_disconnected = AsyncMock(return_value=False)-handler = captured["handler"]assert callable(handler)- await handler(stream)+ await handler(copilot_stream)update_calls = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_listclear_calls = [c for c in update_calls if c.kwargs.get("proposed_workflow") is None]@@ -2103,7 +2083,7 @@ async def test_proposed_workflow_cleared_on_restore(# chat.proposed_workflow, so the payload must mirror agent_result.response_frames = [call.args[0]- for call in stream.send.await_args_list+ for call in copilot_stream.send.await_args_listif isinstance(call.args[0], WorkflowCopilotStreamResponseUpdate)]assert len(response_frames) == 1, f"expected exactly one RESPONSE frame, got {response_frames!r}"@@ -2116,6 +2096,9 @@ async def test_proposed_workflow_cleared_on_restore(@pytest.mark.asyncioasync def test_verified_code_only_fix_stays_pending_without_auto_accept(monkeypatch: pytest.MonkeyPatch,+ api_key_request: MagicMock,+ copilot_stream: MagicMock,+ organization: SimpleNamespace,) -> None:# With auto_accept off, a verified CODE_ONLY_BROWSER fix (auto_applicable + staged)# must stay a pending proposal: no canonical commit, proposal persisted, terminal@@ -2183,20 +2166,12 @@ async def test_verified_code_only_fix_stays_pending_without_auto_accept(AsyncMock(return_value="copilot"),)- request = MagicMock()- request.headers = {"x-api-key": "sk-test-key"}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(), organization)+ response = await workflow_copilot_chat_post(api_key_request, _make_chat_request(), organization)assert response is captured["sentinel"]- stream = MagicMock()- stream.send = AsyncMock(return_value=True)- stream.is_disconnected = AsyncMock(return_value=False)-handler = captured["handler"]assert callable(handler)- await handler(stream)+ await handler(copilot_stream)restore_mock.assert_not_awaited()workflow_service.update_workflow_definition.assert_not_awaited()@@ -2207,7 +2182,7 @@ async def test_verified_code_only_fix_stays_pending_without_auto_accept(response_frames = [call.args[0]- for call in stream.send.await_args_list+ for call in copilot_stream.send.await_args_listif isinstance(call.args[0], WorkflowCopilotStreamResponseUpdate)]assert len(response_frames) == 1@@ -2218,6 +2193,9 @@ async def test_verified_code_only_fix_stays_pending_without_auto_accept(@pytest.mark.asyncioasync def test_output_policy_block_preserves_unvalidated_prior_proposal_under_auto_accept(monkeypatch: pytest.MonkeyPatch,+ api_key_request: MagicMock,+ copilot_stream: MagicMock,+ organization: SimpleNamespace,) -> None:monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)@@ -2255,20 +2233,12 @@ async def test_output_policy_block_preserves_unvalidated_prior_proposal_under_ausetup_new_copilot_mocks(monkeypatch, chat, original_workflow, agent_result)- request = MagicMock()- request.headers = {"x-api-key": "sk-test-key"}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(), organization)+ response = await workflow_copilot_chat_post(api_key_request, _make_chat_request(), organization)assert response is captured["sentinel"]- stream = MagicMock()- stream.send = AsyncMock(return_value=True)- stream.is_disconnected = AsyncMock(return_value=False)-handler = captured["handler"]assert callable(handler)- await handler(stream)+ await handler(copilot_stream)update_calls = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_listclear_calls = [c for c in update_calls if c.kwargs.get("proposed_workflow") is None]@@ -2283,7 +2253,7 @@ async def test_output_policy_block_preserves_unvalidated_prior_proposal_under_auresponse_frames = [call.args[0]- for call in stream.send.await_args_list+ for call in copilot_stream.send.await_args_listif isinstance(call.args[0], WorkflowCopilotStreamResponseUpdate)]assert len(response_frames) == 1@@ -2293,7 +2263,9 @@ async def test_output_policy_block_preserves_unvalidated_prior_proposal_under_au@pytest.mark.asyncio-async def test_unvalidated_timeout_wip_overrides_auto_accept(monkeypatch: pytest.MonkeyPatch) -> None:+async def test_unvalidated_timeout_wip_overrides_auto_accept(+ monkeypatch: pytest.MonkeyPatch, api_key_request: MagicMock, organization: SimpleNamespace+) -> None:monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)captured = install_fake_create(monkeypatch)@@ -2311,7 +2283,8 @@ async def test_unvalidated_timeout_wip_overrides_auto_accept(monkeypatch: pytestdescription="Original description",workflow_definition=None,)- proposal = MagicMock(spec=["model_dump"])+ proposal = MagicMock(spec=["model_dump", "title"])+ proposal.title = "WIP"proposal.model_dump.return_value = {"workflow_id": "wf-canonical"}agent_result = SimpleNamespace(user_response="I ran out of time before I could finish testing.",@@ -2337,11 +2310,7 @@ async def test_unvalidated_timeout_wip_overrides_auto_accept(monkeypatch: pytestrestore_mock, _ = setup_new_copilot_mocks(monkeypatch, chat, original_workflow, agent_result)- request = MagicMock()- request.headers = {"x-api-key": "sk-test-key"}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(), organization)+ response = await workflow_copilot_chat_post(api_key_request, _make_chat_request(), organization)assert response is captured["sentinel"]sent_frames: list[object] = []@@ -2378,7 +2347,9 @@ async def test_unvalidated_timeout_wip_overrides_auto_accept(monkeypatch: pytest@pytest.mark.asyncio-async def test_env_on_short_circuits_posthog(monkeypatch: pytest.MonkeyPatch) -> None:+async def test_env_on_short_circuits_posthog(+ monkeypatch: pytest.MonkeyPatch, anon_request: MagicMock, organization: SimpleNamespace+) -> None:"""Env var True must skip the PostHog check entirely and route to v2."""monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)mock_provider = _install_mock_provider(@@ -2393,11 +2364,7 @@ async def test_env_on_short_circuits_posthog(monkeypatch: pytest.MonkeyPatch) ->new_copilot_mock,)- request = MagicMock()- request.headers = {}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(), organization)+ response = await workflow_copilot_chat_post(anon_request, _make_chat_request(), organization)assert response is sentinelnew_copilot_mock.assert_awaited_once()@@ -2405,7 +2372,7 @@ async def test_env_on_short_circuits_posthog(monkeypatch: pytest.MonkeyPatch) ->@pytest.mark.asyncio-async def test_env_off_posthog_on_uses_v2(monkeypatch: pytest.MonkeyPatch) -> None:+async def test_env_off_posthog_on_uses_v2(monkeypatch: pytest.MonkeyPatch, anon_request: MagicMock) -> None:"""Env var False + PostHog True -> v2 path, with org_id as distinct_id."""monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", False)mock_provider = _install_mock_provider(monkeypatch, return_value=True)@@ -2417,11 +2384,9 @@ async def test_env_off_posthog_on_uses_v2(monkeypatch: pytest.MonkeyPatch) -> Nonew_copilot_mock,)- request = MagicMock()- request.headers = {}organization = SimpleNamespace(organization_id="org-abc")- response = await workflow_copilot_chat_post(request, _make_chat_request(), organization)+ response = await workflow_copilot_chat_post(anon_request, _make_chat_request(), organization)assert response is sentinelnew_copilot_mock.assert_awaited_once()@@ -2433,7 +2398,9 @@ async def test_env_off_posthog_on_uses_v2(monkeypatch: pytest.MonkeyPatch) -> No@pytest.mark.asyncio-async def test_provider_failure_falls_back_to_legacy(monkeypatch: pytest.MonkeyPatch) -> None:+async def test_provider_failure_falls_back_to_legacy(+ monkeypatch: pytest.MonkeyPatch, anon_request: MagicMock, organization: SimpleNamespace+) -> None:"""Provider errors (PostHog down, DB hiccup) must not break the endpoint."""monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", False)_install_mock_provider(monkeypatch, side_effect=RuntimeError("posthog unreachable"))@@ -2446,18 +2413,16 @@ async def test_provider_failure_falls_back_to_legacy(monkeypatch: pytest.MonkeyPcaptured = install_fake_create(monkeypatch)- request = MagicMock()- request.headers = {}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(), organization)+ response = await workflow_copilot_chat_post(anon_request, _make_chat_request(), organization)assert response is captured["sentinel"]new_copilot_mock.assert_not_awaited()@pytest.mark.asyncio-async def test_env_off_posthog_off_uses_legacy(monkeypatch: pytest.MonkeyPatch) -> None:+async def test_env_off_posthog_off_uses_legacy(+ monkeypatch: pytest.MonkeyPatch, anon_request: MagicMock, organization: SimpleNamespace+) -> None:"""Env var False + PostHog False -> legacy stream handler, v2 not reached."""monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", False)mock_provider = _install_mock_provider(monkeypatch, return_value=False)@@ -2470,11 +2435,7 @@ async def test_env_off_posthog_off_uses_legacy(monkeypatch: pytest.MonkeyPatch)captured = install_fake_create(monkeypatch)- request = MagicMock()- request.headers = {}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(), organization)+ response = await workflow_copilot_chat_post(anon_request, _make_chat_request(), organization)assert response is captured["sentinel"]new_copilot_mock.assert_not_awaited()@@ -2482,7 +2443,9 @@ async def test_env_off_posthog_off_uses_legacy(monkeypatch: pytest.MonkeyPatch)@pytest.mark.asyncio-async def test_legacy_path_persists_copilot_yaml_on_proposal(monkeypatch: pytest.MonkeyPatch) -> None:+async def test_legacy_path_persists_copilot_yaml_on_proposal(+ monkeypatch: pytest.MonkeyPatch, anon_request: MagicMock, copilot_stream: MagicMock, organization: SimpleNamespace+) -> None:"""Legacy V1 path stashes _copilot_yaml so /apply-proposed-workflow can re-create the version.Regression for #10568 + SKY-9206: Accept on the frontend now hits@@ -2502,7 +2465,8 @@ async def test_legacy_path_persists_copilot_yaml_on_proposal(monkeypatch: pytestauto_accept=False,)- proposal = MagicMock(spec=["model_dump"])+ proposal = MagicMock(spec=["model_dump", "title"])+ proposal.title = "Updated"proposal.model_dump.return_value = {"workflow_id": "wf-canonical", "title": "Updated"}workflow_yaml = "title: Updated\nworkflow_definition:\n blocks: []\n"@@ -2526,20 +2490,12 @@ async def test_legacy_path_persists_copilot_yaml_on_proposal(monkeypatch: pytest),)- request = MagicMock()- request.headers = {}- organization = SimpleNamespace(organization_id="org-1")-- response = await workflow_copilot_chat_post(request, _make_chat_request(), organization)+ response = await workflow_copilot_chat_post(anon_request, _make_chat_request(), organization)assert response is captured["sentinel"]- stream = MagicMock()- stream.send = AsyncMock(return_value=True)- stream.is_disconnected = AsyncMock(return_value=False)-handler = captured["handler"]assert callable(handler)- await handler(stream)+ await handler(copilot_stream)update_calls = app.DATABASE.workflow_params.update_workflow_copilot_chat.await_args_listpersist_calls = [c for c in update_calls if c.kwargs.get("proposed_workflow") is not None]@@ -2567,7 +2523,7 @@ async def test_persist_state_keeps_verified_review_tested_proposal(monkeypatch:SimpleNamespace(update_workflow_copilot_chat=AsyncMock()),)agent_result = SimpleNamespace(- updated_workflow=SimpleNamespace(model_dump=lambda mode: {"title": "built"}),+ updated_workflow=SimpleNamespace(title="built", model_dump=lambda mode: {"title": "built"}),workflow_yaml="title: built\n",clear_proposed_workflow=False,proposal_disposition="review_tested",@@ -2691,7 +2647,7 @@ async def test_persist_state_keep_pending_proposal_does_not_block_new_proposal_oSimpleNamespace(update_workflow_copilot_chat=AsyncMock()),)agent_result = _make_bypassed_proposal_agent_result(- updated_workflow=SimpleNamespace(model_dump=lambda mode: {"title": "new draft"}),+ updated_workflow=SimpleNamespace(title="new draft", model_dump=lambda mode: {"title": "new draft"}),workflow_yaml="title: new draft\n",)@@ -3439,7 +3395,7 @@ def test_reconcile_threshold_outlasts_the_turn_enforcement_ceiling() -> None:@pytest.mark.asyncioasync def test_marker_survives_a_finalizer_that_raises_with_no_assistant_row(- monkeypatch: pytest.MonkeyPatch,+ monkeypatch: pytest.MonkeyPatch, api_key_request: MagicMock, copilot_stream: MagicMock) -> None:"""No assistant row was written, so the turn stays recoverable instead of being orphaned."""monkeypatch.setattr(settings, "ENABLE_WORKFLOW_COPILOT_V2", True)@@ -3475,17 +3431,11 @@ async def test_marker_survives_a_finalizer_that_raises_with_no_assistant_row(AsyncMock(side_effect=RuntimeError("database down")),)- request = MagicMock()- request.headers = {"x-api-key": "sk-test-key"}-- await workflow_copilot_chat_post(request, _make_chat_request(), SimpleNamespace(organization_id="org-1"))- stream = MagicMock()- stream.send = AsyncMock(return_value=True)- stream.is_disconnected = AsyncMock(return_value=False)+ await workflow_copilot_chat_post(api_key_request, _make_chat_request(), SimpleNamespace(organization_id="org-1"))handler = captured["handler"]assert callable(handler)with contextlib.suppress(RuntimeError):- await handler(stream)+ await handler(copilot_stream)assert app.DATABASE.workflow_params.start_copilot_turn.await_count == 1app.DATABASE.workflow_params.clear_pending_copilot_turn.assert_not_awaited()