Skip to content

Automate browser based workflows with AI

Arm the reconnect tests' 1 s guard once the stop under test starts (SKY-18498) (#8942)

Shuchang Zheng0841e73ca33f
diff --git a/alembic/versions/2026_10_17_0114-aee363385de8_add_build_source_to_workflows.py b/alembic/versions/2026_10_17_0114-aee363385de8_add_build_source_to_workflows.py
new file mode 100644
index 000000000..c04115fa1
--- /dev/null
+++ b/alembic/versions/2026_10_17_0114-aee363385de8_add_build_source_to_workflows.py
@@ -0,0 +1,29 @@
+"""add build_source to workflows
+
+Revision ID: aee363385de8
+Revises: 0c453e5042cb
+Create Date: 2026-10-17T01:14:43.134434+00:00
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "aee363385de8"
+down_revision: Union[str, None] = "0c453e5042cb"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def upgrade() -> None:
+ op.execute("SET LOCAL lock_timeout = '5s'")
+ op.add_column("workflows", sa.Column("build_source", sa.String(), nullable=True))
+
+
+def downgrade() -> None:
+ op.execute("SET LOCAL lock_timeout = '5s'")
+ op.drop_column("workflows", "build_source")
diff --git a/docs/api-reference/openapi.json b/docs/api-reference/openapi.json
index 1561e0ded..4ce9f0835 100644
--- a/docs/api-reference/openapi.json
+++ b/docs/api-reference/openapi.json
@@ -30528,6 +30528,17 @@
],
"title": "Edited By"
},
+ "build_source": {
+ "anyOf": [
+ {
+ "$ref": "#/components/schemas/WorkflowBuildSource"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "description": "Which surface wrote this version: ui, copilot, mcp, cli, api, or internal. Null before tracking began."
+ },
"copilot_authored": {
"type": "boolean",
"title": "Copilot Authored",
@@ -30613,6 +30624,19 @@
],
"title": "Workflow"
},
+ "WorkflowBuildSource": {
+ "type": "string",
+ "enum": [
+ "ui",
+ "copilot",
+ "mcp",
+ "cli",
+ "api",
+ "internal"
+ ],
+ "title": "WorkflowBuildSource",
+ "description": "Which surface wrote a workflow version.\n\n- ui: the Skyvern app\n- copilot: the workflow copilot\n- mcp: a first-party MCP client\n- cli: the Skyvern CLI\n- api: any other API or SDK caller\n- internal: a workflow Skyvern creates for its own runs (task v2, login, credential tests)"
+ },
"WorkflowCreateYAMLRequest": {
"properties": {
"title": {
@@ -34048,7 +34072,7 @@
"job_recipe_apply"
],
"title": "WorkflowRunTriggerType",
- "description": "How a workflow run was initiated.\n\n- manual: User clicked \"Run\" in the UI\n- mcp: First-party MCP client request\n- api: Direct API call to the run endpoint\n- scheduled: Triggered by a cron schedule\n- webhook: Triggered by an external system via the webhook endpoint\n- event: Triggered by an external event source\n- job_recipe_extract: Launched by a job recipe extract request\n- job_recipe_apply: Launched by a job recipe apply request"
+ "description": "How a workflow run was initiated.\n\n- manual: User clicked \"Run\" in the UI\n- mcp: First-party MCP client or Skyvern CLI request\n- api: Direct API call to the run endpoint\n- scheduled: Triggered by a cron schedule\n- webhook: Triggered by an external system via the webhook endpoint\n- event: Triggered by an external event source\n- job_recipe_extract: Launched by a job recipe extract request\n- job_recipe_apply: Launched by a job recipe apply request"
},
"WorkflowSchedule": {
"properties": {
diff --git a/skyvern/_cli_bootstrap.py b/skyvern/_cli_bootstrap.py
index 9404f60e0..a93c8dce8 100644
--- a/skyvern/_cli_bootstrap.py
+++ b/skyvern/_cli_bootstrap.py
@@ -9,6 +9,7 @@ if TYPE_CHECKING:
_DEFAULT_CLI_LOG_LEVEL = "WARNING"
_CLI_RUNTIME_PREPARED = False
+_CLI_COMMAND_RUNNING = False
CLOUD_URL_OPT_IN_MESSAGE = (
"Refusing to send an API key to the default production URL. "
@@ -21,6 +22,16 @@ def is_cli_runtime() -> bool:
return _CLI_RUNTIME_PREPARED
+def mark_cli_command() -> None:
+ """Label this process's API calls as the CLI; `skyvern run mcp` is also a CLI runtime but stays labelled MCP."""
+ global _CLI_COMMAND_RUNNING
+ _CLI_COMMAND_RUNNING = True
+
+
+def is_cli_command() -> bool:
+ return _CLI_COMMAND_RUNNING
+
+
_QUIET_CLI_LOGGERS = ("skyvern", "httpx", "litellm", "playwright", "httpcore")
_RUNTIME_LOGGING_CONFIGURED = False
_SILENCED_CLI_LOGGERS = ("posthog",)
diff --git a/skyvern/cli/commands/_output.py b/skyvern/cli/commands/_output.py
index 7e20c2786..a648b75a5 100644
--- a/skyvern/cli/commands/_output.py
+++ b/skyvern/cli/commands/_output.py
@@ -10,6 +10,7 @@ import typer
from rich.console import Console
from rich.table import Table
+from skyvern._cli_bootstrap import mark_cli_command
from skyvern.cli.core.guards import STALE_FRAME_HINT, stale_frame_selection_in
from skyvern.cli.core.telemetry import capture_cli_tool_call
@@ -131,6 +132,7 @@ def run_tool(
telemetry_tool_name: str | None = None,
) -> None:
"""Run an async MCP tool and emit the result."""
+ mark_cli_command()
try:
result: dict[str, Any] = asyncio.run(runner())
emit_tool_result(result, json_output=json_output, telemetry_tool_name=telemetry_tool_name)
diff --git a/skyvern/cli/commands/browser.py b/skyvern/cli/commands/browser.py
index 7c8f3fd2d..7bf972688 100644
--- a/skyvern/cli/commands/browser.py
+++ b/skyvern/cli/commands/browser.py
@@ -21,7 +21,7 @@ if TYPE_CHECKING:
import typer
-from skyvern._cli_bootstrap import prepare_cli_runtime
+from skyvern._cli_bootstrap import mark_cli_command, prepare_cli_runtime
from skyvern.browser_extension.auth import load_or_create_pairing_token
from skyvern.browser_extension.broker_client import BrokerClient
from skyvern.browser_extension.broker_protocol import is_valid_nonce
@@ -105,6 +105,7 @@ def browser_callback(ctx: typer.Context) -> None:
if ctx.invoked_subcommand == "extension-broker-daemon":
return
prepare_cli_runtime(intent=EnvIntent.CLOUD)
+ mark_cli_command()
@dataclass(frozen=True)
diff --git a/skyvern/cli/core/client.py b/skyvern/cli/core/client.py
index 4724eccfc..6471947c6 100644
--- a/skyvern/cli/core/client.py
+++ b/skyvern/cli/core/client.py
@@ -10,11 +10,11 @@ from typing import Any
import httpx
import structlog
-from skyvern._cli_bootstrap import CLOUD_URL_OPT_IN_MESSAGE, is_cli_runtime
+from skyvern._cli_bootstrap import CLOUD_URL_OPT_IN_MESSAGE, is_cli_command, is_cli_runtime
from skyvern.client import RunSdkActionResponse, SkyvernEnvironment
from skyvern.client.core.request_options import RequestOptions
from skyvern.config import settings
-from skyvern.constants import SKYVERN_MCP_USER_AGENT
+from skyvern.constants import SKYVERN_CLI_USER_AGENT, SKYVERN_MCP_USER_AGENT
from skyvern.library.skyvern import Skyvern
from .api_key_hash import hash_api_key_for_cache
@@ -93,15 +93,16 @@ def _build_cloud_client(api_key: str) -> Skyvern:
# 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).
+ user_agent = SKYVERN_CLI_USER_AGENT if is_cli_command() else SKYVERN_MCP_USER_AGENT
return client_class(
api_key=api_key,
environment=SkyvernEnvironment.CLOUD,
base_url=base_url,
- headers={"x-user-agent": SKYVERN_MCP_USER_AGENT},
+ headers={"x-user-agent": user_agent},
httpx_client=httpx.AsyncClient(
timeout=60,
follow_redirects=True,
- headers={"x-user-agent": SKYVERN_MCP_USER_AGENT},
+ headers={"x-user-agent": user_agent},
),
)
diff --git a/skyvern/constants.py b/skyvern/constants.py
index 60260a810..911ddfcee 100644
--- a/skyvern/constants.py
+++ b/skyvern/constants.py
@@ -5,9 +5,12 @@ from pathlib import Path
SKYVERN_ID_ATTR: str = "unique_id"
# Values sent in the x-user-agent header by first-party Skyvern clients. Used to distinguish
-# UI- and MCP-triggered runs from API/SDK callers (e.g. for trigger_type classification and routing strategy).
+# UI-, MCP- and CLI-triggered runs from API/SDK callers (e.g. for trigger_type classification and routing strategy).
SKYVERN_UI_USER_AGENT: str = "skyvern-ui"
SKYVERN_MCP_USER_AGENT: str = "skyvern-mcp"
+SKYVERN_CLI_USER_AGENT: str = "skyvern-cli"
+# The CLI drives the same tools as the MCP server, so it gets the MCP's run treatment; only attribution tells them apart.
+SKYVERN_AGENT_USER_AGENTS: frozenset[str] = frozenset({SKYVERN_MCP_USER_AGENT, SKYVERN_CLI_USER_AGENT})
SKYVERN_DIR = Path(__file__).parent
REPO_ROOT_DIR = SKYVERN_DIR.parent
diff --git a/skyvern/forge/sdk/copilot/canonical_ownership.py b/skyvern/forge/sdk/copilot/canonical_ownership.py
index 4d119186e..d028a7a46 100644
--- a/skyvern/forge/sdk/copilot/canonical_ownership.py
+++ b/skyvern/forge/sdk/copilot/canonical_ownership.py
@@ -14,10 +14,17 @@ from typing import Any
# A write always moves these, so they carry no information about who wrote it. ``title`` is here
# because background naming renames the row out of band, which is not a content write.
VOLATILE_WORKFLOW_FIELDS = frozenset({"workflow_id", "version", "created_at", "modified_at", "title"})
+# Records which surface wrote the row, not what it holds. Hashing it would also make every fingerprint stored
+# before the column existed stop matching an unchanged workflow.
+ATTRIBUTION_ONLY_WORKFLOW_FIELDS = frozenset({"build_source"})
def workflow_content(workflow: dict[str, Any]) -> dict[str, Any]:
- return {key: value for key, value in workflow.items() if key not in VOLATILE_WORKFLOW_FIELDS}
+ return {
+ key: value
+ for key, value in workflow.items()
+ if key not in VOLATILE_WORKFLOW_FIELDS and key not in ATTRIBUTION_ONLY_WORKFLOW_FIELDS
+ }
def workflow_content_fingerprint(workflow: dict[str, Any]) -> str:
diff --git a/skyvern/forge/sdk/db/enums.py b/skyvern/forge/sdk/db/enums.py
index 56469b40a..14a61d69c 100644
--- a/skyvern/forge/sdk/db/enums.py
+++ b/skyvern/forge/sdk/db/enums.py
@@ -45,7 +45,7 @@ class WorkflowRunTriggerType(StrEnum):
"""How a workflow run was initiated.
- manual: User clicked "Run" in the UI
- - mcp: First-party MCP client request
+ - mcp: First-party MCP client or Skyvern CLI request
- api: Direct API call to the run endpoint
- scheduled: Triggered by a cron schedule
- webhook: Triggered by an external system via the webhook endpoint
@@ -64,6 +64,25 @@ class WorkflowRunTriggerType(StrEnum):
job_recipe_apply = "job_recipe_apply"
+class WorkflowBuildSource(StrEnum):
+ """Which surface wrote a workflow version.
+
+ - ui: the Skyvern app
+ - copilot: the workflow copilot
+ - mcp: a first-party MCP client
+ - cli: the Skyvern CLI
+ - api: any other API or SDK caller
+ - internal: a workflow Skyvern creates for its own runs (task v2, login, credential tests)
+ """
+
+ ui = "ui"
+ copilot = "copilot"
+ mcp = "mcp"
+ cli = "cli"
+ api = "api"
+ internal = "internal"
+
+
MANUAL_LIKE_WORKFLOW_RUN_TRIGGER_TYPES = frozenset(
{
WorkflowRunTriggerType.manual,
diff --git a/skyvern/forge/sdk/db/models.py b/skyvern/forge/sdk/db/models.py
index 9270e9f51..c61cfc12f 100644
--- a/skyvern/forge/sdk/db/models.py
+++ b/skyvern/forge/sdk/db/models.py
@@ -741,6 +741,7 @@ class WorkflowModel(SoftDeleteMixin, Base):
import_error = Column(String, nullable=True) # Error message if import failed
created_by = Column(String, nullable=True)
edited_by = Column(String, nullable=True)
+ build_source = Column(String, nullable=True) # WorkflowBuildSource
created_at = Column(DateTime, default=datetime.datetime.utcnow, nullable=False)
modified_at = Column(
diff --git a/skyvern/forge/sdk/db/repositories/workflows.py b/skyvern/forge/sdk/db/repositories/workflows.py
index d9028afb5..27c4b6d14 100644
--- a/skyvern/forge/sdk/db/repositories/workflows.py
+++ b/skyvern/forge/sdk/db/repositories/workflows.py
@@ -20,6 +20,7 @@ from skyvern.forge.sdk.db._error_handling import db_operation
from skyvern.forge.sdk.db._sentinels import _UNSET
from skyvern.forge.sdk.db._soft_delete import exclude_deleted
from skyvern.forge.sdk.db.base_repository import BaseRepository
+from skyvern.forge.sdk.db.enums import WorkflowBuildSource
from skyvern.forge.sdk.db.exceptions import NotFoundError
from skyvern.forge.sdk.db.models import (
AWSSecretParameterModel,
@@ -305,6 +306,7 @@ class WorkflowsRepository(BaseRepository):
folder_id: str | None = None,
created_by: str | None = None,
edited_by: str | None = None,
+ build_source: WorkflowBuildSource | None = None,
workflow_id: str | None = None,
) -> Workflow:
async with self.Session() as session:
@@ -353,6 +355,7 @@ class WorkflowsRepository(BaseRepository):
folder_id=folder_id,
created_by=created_by,
edited_by=edited_by,
+ build_source=build_source,
)
if workflow_permanent_id:
workflow.workflow_permanent_id = workflow_permanent_id
@@ -1020,6 +1023,7 @@ class WorkflowsRepository(BaseRepository):
sequential_key: str | None | object = _UNSET,
created_by: str | None | object = _UNSET,
edited_by: str | None | object = _UNSET,
+ build_source: WorkflowBuildSource | None | object = _UNSET,
refuse_if_pinned_by_run_group: bool = False,
) -> Workflow:
async with self.Session() as session:
@@ -1098,6 +1102,8 @@ class WorkflowsRepository(BaseRepository):
workflow.created_by = cast(str | None, created_by)
if edited_by is not _UNSET:
workflow.edited_by = cast(str | None, edited_by)
+ if build_source is not _UNSET:
+ workflow.build_source = cast(WorkflowBuildSource | None, build_source)
await session.commit()
await session.refresh(workflow)
is_template = (
@@ -1359,6 +1365,7 @@ class WorkflowsRepository(BaseRepository):
sequential_key: str | None | object = _UNSET,
created_by: str | None | object = _UNSET,
edited_by: str | None | object = _UNSET,
+ build_source: WorkflowBuildSource | None | object = _UNSET,
preserve_completion_contract: bool = True,
refuse_if_pinned_by_run_group: bool = False,
) -> Workflow:
@@ -1485,6 +1492,8 @@ class WorkflowsRepository(BaseRepository):
workflow.created_by = cast(str | None, created_by)
if edited_by is not _UNSET:
workflow.edited_by = cast(str | None, edited_by)
+ if build_source is not _UNSET:
+ workflow.build_source = cast(WorkflowBuildSource | None, build_source)
await session.commit()
await session.refresh(workflow)
diff --git a/skyvern/forge/sdk/db/utils.py b/skyvern/forge/sdk/db/utils.py
index 42b7706d0..cba567bdc 100644
--- a/skyvern/forge/sdk/db/utils.py
+++ b/skyvern/forge/sdk/db/utils.py
@@ -768,6 +768,7 @@ def convert_to_workflow(
import_error=workflow_model.import_error,
created_by=workflow_model.created_by,
edited_by=workflow_model.edited_by,
+ build_source=workflow_model.build_source,
)
diff --git a/skyvern/forge/sdk/routes/agent_protocol.py b/skyvern/forge/sdk/routes/agent_protocol.py
index d22e367a3..674cdfb47 100644
--- a/skyvern/forge/sdk/routes/agent_protocol.py
+++ b/skyvern/forge/sdk/routes/agent_protocol.py
@@ -128,6 +128,7 @@ from skyvern.forge.sdk.routes.code_samples import (
from skyvern.forge.sdk.routes.routers import base_router, legacy_base_router, legacy_v2_router
from skyvern.forge.sdk.routes.trigger_type import (
caps_run_response_values,
+ workflow_build_source_from_user_agent,
workflow_run_trigger_type_from_user_agent,
)
from skyvern.forge.sdk.schemas.ai_suggestions import AISuggestionBase, AISuggestionRequest
@@ -942,6 +943,7 @@ async def create_workflow_legacy(
folder_id: str | None = Query(None, description="Optional folder ID to assign the workflow to"),
current_org: Organization = Depends(org_auth_service.get_current_org),
user_id: str | None = Depends(org_auth_service.get_current_user_id_or_none),
+ x_user_agent: Annotated[str | None, Header(include_in_schema=False)] = None,
) -> Workflow:
analytics.capture("skyvern-oss-agent-workflow-create-legacy")
raw_yaml = await request.body()
@@ -963,6 +965,7 @@ async def create_workflow_legacy(
request=workflow_create_request,
created_by=user_id,
edited_by=user_id,
+ build_source=workflow_build_source_from_user_agent(x_user_agent),
return_write_result=True,
)
if changed_fields:
@@ -1047,8 +1050,10 @@ async def create_workflow(
current_org: Organization = Depends(org_auth_service.get_current_org),
user_id: str | None = Depends(org_auth_service.get_current_user_id_or_none),
idempotency_key: IdempotencyKey = None,
+ x_user_agent: Annotated[str | None, Header(include_in_schema=False)] = None,
) -> Workflow:
analytics.capture("skyvern-oss-agent-workflow-create")
+ build_source = workflow_build_source_from_user_agent(x_user_agent)
try:
if data.yaml_definition:
workflow_json_from_yaml = safe_load_no_dates(data.yaml_definition)
@@ -1087,6 +1092,7 @@ async def create_workflow(
new_workflow_permanent_id=workflow_permanent_id,
created_by=user_id,
edited_by=user_id,
+ build_source=build_source,
resolved_title=resolved_title,
return_write_result=True,
)
@@ -1096,6 +1102,7 @@ async def create_workflow(
request=workflow_definition,
created_by=user_id,
edited_by=user_id,
+ build_source=build_source,
return_write_result=True,
)
if changed_fields:
@@ -1137,6 +1144,7 @@ async def create_workflow_from_prompt(
user_id: str | None = Depends(org_auth_service.get_current_user_id_or_none),
x_max_iterations_override: Annotated[int | str | None, Header()] = None,
x_max_steps_override: Annotated[int | str | None, Header()] = None,
+ x_user_agent: Annotated[str | None, Header(include_in_schema=False)] = None,
) -> dict[str, Any]:
try:
body = await raw_request.json()
@@ -1195,6 +1203,7 @@ async def create_workflow_from_prompt(
generate_script=bool(request.generate_script),
actor_user_id=user_id,
created_via="prompt",
+ build_source=workflow_build_source_from_user_agent(x_user_agent),
)
except Exception as e:
LOG.error("Failed to create workflow from prompt", exc_info=True, organization_id=organization.organization_id)
@@ -1328,9 +1337,11 @@ async def import_workflow_from_pdf(
folder_id: str | None = Query(None, description="Optional folder ID to assign the imported workflow to"),
current_org: Organization = Depends(org_auth_service.get_current_org),
user_id: str | None = Depends(org_auth_service.get_current_user_id_or_none),
+ x_user_agent: Annotated[str | None, Header(include_in_schema=False)] = None,
) -> dict[str, Any]:
"""Import a workflow from a PDF file containing Standard Operating Procedures."""
analytics.capture("skyvern-oss-workflow-import-pdf")
+ build_source = workflow_build_source_from_user_agent(x_user_agent)
# Read file and validate early (before creating import record)
if not file.filename or not file.filename.lower().endswith(".pdf"):
@@ -1363,6 +1374,7 @@ async def import_workflow_from_pdf(
folder_id=folder_id,
created_by=user_id,
edited_by=user_id,
+ build_source=build_source,
)
await app.AGENT_FUNCTION.record_audit_event(
get_request_principal(),
@@ -1389,6 +1401,7 @@ async def import_workflow_from_pdf(
workflow_permanent_id=empty_workflow.workflow_permanent_id,
created_by=user_id,
edited_by=user_id,
+ build_source=build_source,
created_via="pdf",
)
@@ -1466,6 +1479,7 @@ async def update_workflow_legacy(
current_org: Organization = Depends(org_auth_service.get_current_org),
user_id: str | None = Depends(org_auth_service.get_current_user_id_or_none),
expected_version: Annotated[int | None, Query(ge=1)] = None,
+ x_user_agent: Annotated[str | None, Header(include_in_schema=False)] = None,
) -> Workflow:
analytics.capture("skyvern-oss-agent-workflow-update")
@@ -1493,6 +1507,7 @@ async def update_workflow_legacy(
workflow_permanent_id=workflow_id,
created_by=user_id,
edited_by=user_id,
+ build_source=workflow_build_source_from_user_agent(x_user_agent),
expected_version=expected_version,
return_write_result=True,
)
@@ -1573,6 +1588,7 @@ async def update_workflow(
),
current_org: Organization = Depends(org_auth_service.get_current_org),
user_id: str | None = Depends(org_auth_service.get_current_user_id_or_none),
+ x_user_agent: Annotated[str | None, Header(include_in_schema=False)] = None,
) -> Workflow:
analytics.capture("skyvern-oss-agent-workflow-update")
try:
@@ -1594,6 +1610,7 @@ async def update_workflow(
workflow_permanent_id=workflow_id,
created_by=user_id,
edited_by=user_id,
+ build_source=workflow_build_source_from_user_agent(x_user_agent),
return_write_result=True,
)
except yaml.YAMLError as exc:
diff --git a/skyvern/forge/sdk/routes/credentials.py b/skyvern/forge/sdk/routes/credentials.py
index 91838aced..15012500e 100644
--- a/skyvern/forge/sdk/routes/credentials.py
+++ b/skyvern/forge/sdk/routes/credentials.py
@@ -53,7 +53,7 @@ from skyvern.forge.agent_functions import record_request_audit_event
from skyvern.forge.sdk.api.files import discard_temp_working_dir
from skyvern.forge.sdk.core.aiohttp_helper import aiohttp_request
from skyvern.forge.sdk.db.datetime_utils import naive_utc_now, to_naive_utc
-from skyvern.forge.sdk.db.enums import OrganizationAuthTokenType
+from skyvern.forge.sdk.db.enums import OrganizationAuthTokenType, WorkflowBuildSource
from skyvern.forge.sdk.db.models import CredentialFolderModel
from skyvern.forge.sdk.executor.factory import AsyncExecutorFactory
from skyvern.forge.sdk.routes.code_samples import (
@@ -1148,6 +1148,7 @@ async def test_login(
workflow = await app.WORKFLOW_SERVICE.create_workflow_from_request(
organization=current_org,
request=workflow_create_request,
+ build_source=WorkflowBuildSource.internal,
)
credential_proxy_session_id = getattr(credential, "proxy_session_id", None)
@@ -1343,6 +1344,7 @@ async def test_credential(
workflow = await app.WORKFLOW_SERVICE.create_workflow_from_request(
organization=current_org,
request=workflow_create_request,
+ build_source=WorkflowBuildSource.internal,
)
# Boot fresh (don't seed the saved profile): a reused profile is loaded read-only and the
diff --git a/skyvern/forge/sdk/routes/run_blocks.py b/skyvern/forge/sdk/routes/run_blocks.py
index c7c8bc84d..96afe785e 100644
--- a/skyvern/forge/sdk/routes/run_blocks.py
+++ b/skyvern/forge/sdk/routes/run_blocks.py
@@ -9,6 +9,7 @@ from skyvern.exceptions import MissingBrowserAddressError
from skyvern.forge import app
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.core.permissions.permission_checker_factory import PermissionCheckerFactory
+from skyvern.forge.sdk.db.enums import WorkflowBuildSource
from skyvern.forge.sdk.routes.code_samples import (
DOWNLOAD_FILES_CODE_SAMPLE_PYTHON,
DOWNLOAD_FILES_CODE_SAMPLE_TS,
@@ -298,6 +299,7 @@ async def login(
organization=organization,
request=workflow_create_request,
workflow_permanent_id=new_workflow.workflow_permanent_id,
+ build_source=WorkflowBuildSource.internal,
)
LOG.info("Workflow created", workflow_id=workflow.workflow_id)
@@ -401,6 +403,7 @@ async def download_files(
organization=organization,
request=workflow_create_request,
workflow_permanent_id=new_workflow.workflow_permanent_id,
+ build_source=WorkflowBuildSource.internal,
)
LOG.info("Workflow created", workflow_id=workflow.workflow_id)
diff --git a/skyvern/forge/sdk/routes/trigger_type.py b/skyvern/forge/sdk/routes/trigger_type.py
index 1fd480aa4..6018e98cd 100644
--- a/skyvern/forge/sdk/routes/trigger_type.py
+++ b/skyvern/forge/sdk/routes/trigger_type.py
@@ -1,15 +1,30 @@
-from skyvern.constants import SKYVERN_MCP_USER_AGENT, SKYVERN_UI_USER_AGENT
-from skyvern.forge.sdk.db.enums import WorkflowRunTriggerType
+from skyvern.constants import (
+ SKYVERN_AGENT_USER_AGENTS,
+ SKYVERN_CLI_USER_AGENT,
+ SKYVERN_MCP_USER_AGENT,
+ SKYVERN_UI_USER_AGENT,
+)
+from skyvern.forge.sdk.db.enums import WorkflowBuildSource, WorkflowRunTriggerType
def workflow_run_trigger_type_from_user_agent(x_user_agent: str | None) -> WorkflowRunTriggerType:
if x_user_agent == SKYVERN_UI_USER_AGENT:
return WorkflowRunTriggerType.manual
- if x_user_agent == SKYVERN_MCP_USER_AGENT:
+ if x_user_agent in SKYVERN_AGENT_USER_AGENTS:
return WorkflowRunTriggerType.mcp
return WorkflowRunTriggerType.api
+def workflow_build_source_from_user_agent(x_user_agent: str | None) -> WorkflowBuildSource:
+ if x_user_agent == SKYVERN_UI_USER_AGENT:
+ return WorkflowBuildSource.ui
+ if x_user_agent == SKYVERN_MCP_USER_AGENT:
+ return WorkflowBuildSource.mcp
+ if x_user_agent == SKYVERN_CLI_USER_AGENT:
+ return WorkflowBuildSource.cli
+ return WorkflowBuildSource.api
+
+
def caps_run_response_values(x_user_agent: str | None) -> bool:
"""Whether a run-detail read should bound its output values (SKY-13015).
diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.py
index 0dd1feb09..cff3ad698 100644
--- a/skyvern/forge/sdk/routes/workflow_copilot.py
+++ b/skyvern/forge/sdk/routes/workflow_copilot.py
@@ -141,6 +141,7 @@ from skyvern.forge.sdk.copilot.workflow_yaml import (
)
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.core.event_source_stream import EventSourceStream, FastAPIEventSourceStream
+from skyvern.forge.sdk.db.enums import WorkflowBuildSource
from skyvern.forge.sdk.db.exceptions import (
CopilotProposalConflictError,
DatabaseConnectionUnavailableError,
@@ -2160,6 +2161,7 @@ async def _commit_staged_workflow(
run_sequentially=staged_workflow.run_sequentially,
sequential_key=staged_workflow.sequential_key,
edited_by="copilot",
+ build_source=WorkflowBuildSource.copilot,
preserve_completion_contract=not clear_persisted_completion_contract,
validate_code_block_templates=False,
)
@@ -2241,6 +2243,7 @@ async def _restore_workflow_definition(original_workflow: Workflow | None, organ
sequential_key=original_workflow.sequential_key,
created_by=original_workflow.created_by,
edited_by=original_workflow.edited_by,
+ build_source=original_workflow.build_source,
preserve_completion_contract=False,
validate_code_block_templates=False,
)
@@ -5235,6 +5238,7 @@ async def workflow_copilot_apply_proposed_workflow(
request=yaml_request,
workflow_permanent_id=chat.workflow_permanent_id,
edited_by="copilot",
+ build_source=WorkflowBuildSource.copilot,
validate_code_block_templates=False,
expected_version=canonical.version if metadata is not None else base_version,
return_write_result=True,
diff --git a/skyvern/forge/sdk/workflow/models/workflow.py b/skyvern/forge/sdk/workflow/models/workflow.py
index f4b1924f8..3db1d9608 100644
--- a/skyvern/forge/sdk/workflow/models/workflow.py
+++ b/skyvern/forge/sdk/workflow/models/workflow.py
@@ -17,7 +17,12 @@ from pydantic import (
)
from typing_extensions import Self, deprecated
-from skyvern.forge.sdk.db.enums import BrowserSeedSource, WorkflowRunStatus, WorkflowRunTriggerType
+from skyvern.forge.sdk.db.enums import (
+ BrowserSeedSource,
+ WorkflowBuildSource,
+ WorkflowRunStatus,
+ WorkflowRunTriggerType,
+)
from skyvern.forge.sdk.schemas.files import FileInfo
from skyvern.forge.sdk.schemas.task_v2 import TaskV2
from skyvern.forge.sdk.workflow.exceptions import (
@@ -276,6 +281,10 @@ class Workflow(BaseModel):
import_error: str | None = None
created_by: str | None = None
edited_by: str | None = None
+ build_source: WorkflowBuildSource | None = Field(
+ default=None,
+ description="Which surface wrote this version: ui, copilot, mcp, cli, api, or internal. Null before tracking began.",
+ )
# Lineage-derived (any version copilot-stamped); populated by the detail GET route only —
# user saves re-stamp created_by/edited_by, so the current version alone is not durable.
copilot_authored: bool = False
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index 6c764c788..22ce5eef5 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -112,6 +112,7 @@ from skyvern.forge.sdk.db.enums import (
BrowserSeedSource,
DispatchFinalizationStatus,
OrganizationAuthTokenType,
+ WorkflowBuildSource,
WorkflowRunTriggerType,
)
from skyvern.forge.sdk.db.id import generate_output_parameter_id, generate_workflow_id, generate_workflow_parameter_id
@@ -1312,6 +1313,7 @@ _WORKFLOW_AUDIT_METADATA_FIELDS = {
"modified_at",
"deleted_at",
"copilot_authored",
+ "build_source",
"effective_default_engine",
"original_created_by",
"original_created_at",
@@ -10887,6 +10889,7 @@ class WorkflowService:
generate_script_on_terminal: bool = False,
created_by: str | None = None,
edited_by: str | None = None,
+ build_source: WorkflowBuildSource | None = None,
workflow_id: str | None = None,
encrypt_secrets: bool = True,
) -> Workflow:
@@ -10932,6 +10935,7 @@ class WorkflowService:
generate_script_on_terminal=generate_script_on_terminal,
created_by=created_by,
edited_by=edited_by,
+ build_source=build_source,
workflow_id=workflow_id,
)
except IntegrityError as e:
@@ -10983,6 +10987,7 @@ class WorkflowService:
generate_script: bool = False,
actor_user_id: str | None = None,
created_via: str | None = None,
+ build_source: WorkflowBuildSource | None = None,
) -> Workflow:
metadata_prompt = prompt_engine.load_prompt(
"conversational_ui_goal",
@@ -11125,6 +11130,7 @@ class WorkflowService:
generate_script_on_terminal=generate_script,
created_by=actor_user_id,
edited_by=actor_user_id,
+ build_source=build_source,
)
if status == WorkflowStatus.published:
@@ -11436,6 +11442,7 @@ class WorkflowService:
sequential_key: str | None | object = _UNSET,
created_by: str | None | object = _UNSET,
edited_by: str | None | object = _UNSET,
+ build_source: WorkflowBuildSource | None | object = _UNSET,
notify_workflow_saved: bool = True,
preserve_completion_contract: bool = True,
created_via: str | None = None,
@@ -11484,6 +11491,7 @@ class WorkflowService:
sequential_key=sequential_key,
created_by=created_by,
edited_by=edited_by,
+ build_source=build_source,
preserve_completion_contract=preserve_completion_contract,
refuse_if_pinned_by_run_group=True,
)
@@ -11519,6 +11527,7 @@ class WorkflowService:
sequential_key=sequential_key,
created_by=created_by,
edited_by=edited_by,
+ build_source=build_source,
refuse_if_pinned_by_run_group=True,
)
@@ -16713,6 +16722,7 @@ class WorkflowService:
status=WorkflowStatus.auto_generated,
created_by=COPILOT_TEST_WORKFLOW_CREATOR,
edited_by="copilot",
+ build_source=WorkflowBuildSource.copilot,
description=runtime_workflow.description,
proxy_location=runtime_workflow.proxy_location,
webhook_callback_url=runtime_workflow.webhook_callback_url,
@@ -16818,6 +16828,7 @@ class WorkflowService:
cdp_connect_headers: dict[str, str] | None,
created_by: str | None,
edited_by: str | None,
+ build_source: WorkflowBuildSource | None,
workflow_permanent_id: str | None = None,
workflow_id: str | None = None,
encrypt_secrets: bool = True,
@@ -16859,6 +16870,7 @@ class WorkflowService:
generate_script_on_terminal=request.generate_script_on_terminal,
created_by=created_by,
edited_by=edited_by,
+ build_source=build_source,
workflow_id=workflow_id,
encrypt_secrets=encrypt_secrets,
)
@@ -16873,6 +16885,7 @@ class WorkflowService:
delete_script: bool,
created_by: str | None,
edited_by: str | None,
+ build_source: WorkflowBuildSource | None,
created_via: str | None = None,
) -> tuple[Workflow, bool]:
organization_id = organization.organization_id
@@ -16915,6 +16928,7 @@ class WorkflowService:
workflow_permanent_id=workflow_permanent_id,
created_by=created_by,
edited_by=edited_by,
+ build_source=build_source,
workflow_id=workflow_id,
encrypt_secrets=False,
)
@@ -16964,6 +16978,7 @@ class WorkflowService:
delete_script: bool = True,
created_by: str | None = None,
edited_by: str | None = None,
+ build_source: WorkflowBuildSource | None = None,
new_workflow_permanent_id: str | None = None,
resolved_title: str | None = None,
created_via: str | None = None,
@@ -16982,6 +16997,7 @@ class WorkflowService:
delete_script: bool = True,
created_by: str | None = None,
edited_by: str | None = None,
+ build_source: WorkflowBuildSource | None = None,
new_workflow_permanent_id: str | None = None,
resolved_title: str | None = None,
created_via: str | None = None,
@@ -16999,6 +17015,7 @@ class WorkflowService:
delete_script: bool = True,
created_by: str | None = None,
edited_by: str | None = None,
+ build_source: WorkflowBuildSource | None = None,
new_workflow_permanent_id: str | None = None,
resolved_title: str | None = None,
created_via: str | None = None,
@@ -17035,6 +17052,7 @@ class WorkflowService:
delete_script=delete_script,
created_by=created_by,
edited_by=edited_by,
+ build_source=build_source,
created_via=created_via,
)
return (workflow, ("workflow_definition",) if created else ()) if return_write_result else workflow
@@ -17228,6 +17246,7 @@ class WorkflowService:
generate_script_on_terminal=request.generate_script_on_terminal,
created_by=created_by if created_by is not None else existing_latest_workflow.created_by,
edited_by=edited_by,
+ build_source=build_source,
)
async def next_version_from_same_settings() -> int:
@@ -17262,6 +17281,7 @@ class WorkflowService:
cdp_connect_headers=new_cdp_connect_headers,
created_by=created_by,
edited_by=edited_by,
+ build_source=build_source,
)
)
# Only the steps below still need the version cleaned up by hand; a failure inside the
@@ -17437,6 +17457,7 @@ class WorkflowService:
return await app.WORKFLOW_SERVICE.create_workflow_from_request(
organization=organization,
request=workflow_create_request,
+ build_source=WorkflowBuildSource.internal,
)
async def get_workflow_run_timeline(
diff --git a/skyvern/services/task_v2_service.py b/skyvern/services/task_v2_service.py
index 2176eb840..89de74d70 100644
--- a/skyvern/services/task_v2_service.py
+++ b/skyvern/services/task_v2_service.py
@@ -39,7 +39,7 @@ from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.core.hashing import generate_url_hash
from skyvern.forge.sdk.core.security import generate_skyvern_webhook_signature
from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
-from skyvern.forge.sdk.db.enums import OrganizationAuthTokenType, WorkflowRunTriggerType
+from skyvern.forge.sdk.db.enums import OrganizationAuthTokenType, WorkflowBuildSource, WorkflowRunTriggerType
from skyvern.forge.sdk.executor.run_liveness import (
acquire_run_lease,
note_run_finalized,
@@ -1281,6 +1281,7 @@ async def run_task_v2_helper(
request=workflow_create_request,
workflow_permanent_id=workflow.workflow_permanent_id,
delete_script=False,
+ build_source=WorkflowBuildSource.internal,
)
LOG.info("Workflow created", workflow_id=workflow.workflow_id)
diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py
index cf1a77305..de800a918 100644
--- a/tests/unit/conftest.py
+++ b/tests/unit/conftest.py
@@ -265,14 +265,18 @@ def restore_interpreter_traceback_hooks() -> Iterator[None]:
def reset_cli_runtime_entry() -> Iterator[None]:
"""A CliRunner invocation marks the whole process as CLI-entered and loads a backend env file.
+ A CLI command also labels the process's API client as the CLI, which must not leak into a later MCP test.
+
Both outlive the test, and the pair trips the CLI-only guard that refuses an API key
against the default production URL in any later test that builds a cloud client. The env
load also records SKYVERN_ENV_INTENT unconditionally, which config reads for env precedence.
"""
entered = cli_bootstrap._CLI_RUNTIME_PREPARED
+ command_running = cli_bootstrap._CLI_COMMAND_RUNNING
loaded = {name: os.environ.get(name) for name in ("SKYVERN_API_KEY", "SKYVERN_BASE_URL", "SKYVERN_ENV_INTENT")}
yield
cli_bootstrap._CLI_RUNTIME_PREPARED = entered
+ cli_bootstrap._CLI_COMMAND_RUNNING = command_running
for name, value in loaded.items():
if value is None:
os.environ.pop(name, None)
diff --git a/tests/unit/forge/sdk/test_trigger_type.py b/tests/unit/forge/sdk/test_trigger_type.py
index 7781e2a20..33b03715c 100644
--- a/tests/unit/forge/sdk/test_trigger_type.py
+++ b/tests/unit/forge/sdk/test_trigger_type.py
@@ -1,4 +1,4 @@
-from skyvern.constants import SKYVERN_MCP_USER_AGENT, SKYVERN_UI_USER_AGENT
+from skyvern.constants import SKYVERN_CLI_USER_AGENT, SKYVERN_MCP_USER_AGENT, SKYVERN_UI_USER_AGENT
from skyvern.forge.sdk.db.enums import (
WorkflowRunTriggerType,
is_job_recipe_workflow_run_trigger_type,
@@ -14,6 +14,10 @@ def test_workflow_run_trigger_type_from_user_agent_mcp_returns_mcp() -> None:
assert workflow_run_trigger_type_from_user_agent(SKYVERN_MCP_USER_AGENT) == WorkflowRunTriggerType.mcp
+def test_workflow_run_trigger_type_from_user_agent_cli_keeps_mcp_run_treatment() -> None:
+ assert workflow_run_trigger_type_from_user_agent(SKYVERN_CLI_USER_AGENT) == WorkflowRunTriggerType.mcp
+
+
def test_workflow_run_trigger_type_from_user_agent_none_returns_api() -> None:
assert workflow_run_trigger_type_from_user_agent(None) == WorkflowRunTriggerType.api
diff --git a/tests/unit/test_agent_protocol_idempotency.py b/tests/unit/test_agent_protocol_idempotency.py
index a49b5c7b2..8fc013ed9 100644
--- a/tests/unit/test_agent_protocol_idempotency.py
+++ b/tests/unit/test_agent_protocol_idempotency.py
@@ -20,6 +20,7 @@ from skyvern.forge.sdk.db.repositories import workflows as workflows_repository
from skyvern.forge.sdk.routes import agent_protocol
from skyvern.forge.sdk.routes.workflow_copilot import (
_commit_staged_workflow,
+ _restore_workflow_definition,
workflow_copilot_apply_proposed_workflow,
)
from skyvern.forge.sdk.schemas.organizations import Organization
@@ -1059,3 +1060,61 @@ async def test_workflow_setting_presence_preserves_persisted_values(
)
assert persisted is not None
assert getattr(persisted, field_name) == expected
+
+
+async def test_each_surface_stamps_the_build_source_of_the_version_it_writes(
+ monkeypatch: pytest.MonkeyPatch,
+ tmp_path: Path,
+) -> None:
+ async with idempotency_lab(monkeypatch, tmp_path / "build-source.db") as lab:
+
+ async def build_source_of(workflow_id: str) -> str | None:
+ persisted = await lab.database.workflows.get_workflow(
+ workflow_id=workflow_id, organization_id=lab.organization.organization_id
+ )
+ assert persisted is not None
+ return persisted.build_source
+
+ created = await lab.client.post(
+ "/v1/agents", headers={"x-user-agent": "skyvern-mcp"}, json=WORKFLOW_CREATE_PAYLOAD
+ )
+ assert created.status_code == 200, created.text
+ assert created.json()["build_source"] == "mcp"
+ workflow_permanent_id = created.json()["workflow_permanent_id"]
+
+ definition = WORKFLOW_CREATE_PAYLOAD["json_definition"]
+ ui_save = await lab.client.put(
+ f"/v1/workflows/{workflow_permanent_id}",
+ content=yaml.safe_dump(definition),
+ headers={"Content-Type": "application/x-yaml", "x-user-agent": "skyvern-ui"},
+ )
+ cli_save = await lab.client.post(
+ f"/v1/workflows/{workflow_permanent_id}",
+ headers={"x-user-agent": "skyvern-cli"},
+ json={"json_definition": definition},
+ )
+ api_save = await lab.client.post(f"/v1/workflows/{workflow_permanent_id}", json={"json_definition": definition})
+ assert ui_save.status_code == 200 and cli_save.status_code == 200 and api_save.status_code == 200
+ assert await build_source_of(ui_save.json()["workflow_id"]) == "ui"
+ assert await build_source_of(cli_save.json()["workflow_id"]) == "cli"
+ api_version = Workflow.model_validate(api_save.json())
+ assert await build_source_of(api_version.workflow_id) == "api"
+
+ staged = await _process_workflow_yaml(
+ workflow_id=api_version.workflow_id,
+ workflow_permanent_id=workflow_permanent_id,
+ organization_id=lab.organization.organization_id,
+ workflow_yaml=yaml.safe_dump(definition),
+ )
+ await _commit_staged_workflow(
+ organization_id=lab.organization.organization_id,
+ workflow_id=api_version.workflow_id,
+ workflow_permanent_id=workflow_permanent_id,
+ staged_workflow=staged,
+ )
+ assert await build_source_of(api_version.workflow_id) == "copilot"
+
+ # A rolled-back copilot turn must not leave its stamp on the version the API caller wrote.
+ await _restore_workflow_definition(api_version, lab.organization.organization_id)
+ assert await build_source_of(api_version.workflow_id) == "api"
diff --git a/tests/unit/test_cli_commands.py b/tests/unit/test_cli_commands.py
index 482f50a26..e192a5343 100644
--- a/tests/unit/test_cli_commands.py
+++ b/tests/unit/test_cli_commands.py
@@ -16,17 +16,22 @@ from pathlib import Path
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
+import httpx
import pytest
import typer
from playwright.async_api import Error as PlaywrightError
from typer.testing import CliRunner
+from skyvern import _cli_bootstrap, config
+from skyvern.cli.commands import _output as cli_output
from skyvern.cli.commands import browser as cli_browser
from skyvern.cli.commands import cli_app
from skyvern.cli.commands._state import CLIState, clear_state, load_state, save_state
+from skyvern.cli.core import client as cli_client
from skyvern.cli.core.guards import STALE_FRAME_HINT
from skyvern.cli.status import _status_data
from skyvern.exceptions import StaleFrameSelectionError
+from skyvern.library.skyvern import Skyvern
from tests.unit._mcp_browser_fakes import StaleScopePage
# ---------------------------------------------------------------------------
@@ -1161,6 +1166,56 @@ class TestBrowserGroupBaseUrlGuard:
assert "Set SKYVERN_BASE_URL" in parsed["error"]["message"]
+class TestCliUserAgent:
+ @pytest.mark.parametrize(
+ "args",
+ [
+ pytest.param(["workflow", "list", "--json"], id="mcp_parity_command"),
+ pytest.param(["browser", "session", "create", "--json"], id="browser_command"),
+ ],
+ )
+ def test_cli_command_labels_its_api_calls_as_the_cli(
+ self, args: list[str], monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+ ) -> None:
+ """The CLI drives the MCP's own client and tools; only the header lets the server tell the two apart."""
+ requests: list[httpx.Request] = []
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ requests.append(request)
+ return httpx.Response(200, json=[])
+
+ real_build_cloud_client = cli_client._build_cloud_client
+
+ def build_cloud_client_offline(api_key: str) -> Skyvern:
+ instance = real_build_cloud_client(api_key)
+ instance._client_wrapper.httpx_client.httpx_client._transport = httpx.MockTransport(handler)
+ return instance
+
+ monkeypatch.delenv("SKYVERN_API_KEY", raising=False)
+ monkeypatch.setattr(_cli_bootstrap, "_CLI_RUNTIME_PREPARED", False)
+ monkeypatch.setattr(_cli_bootstrap, "_CLI_COMMAND_RUNNING", 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(
+ cli_client, "settings", config.Settings(_env_file=None, SKYVERN_BASE_URL="http://skyvern.test")
+ )
+ monkeypatch.setattr(cli_client, "_resolve_api_key", lambda: "test-api-key")
+ monkeypatch.setattr(cli_client, "_global_skyvern_instance", None)
+ monkeypatch.setattr(cli_client, "_build_cloud_client", build_cloud_client_offline)
+ monkeypatch.setattr(cli_output, "capture_cli_tool_call", lambda *_args, **_kwargs: None)
+ monkeypatch.setattr(cli_browser, "capture_cli_tool_call", lambda *_args, **_kwargs: None)
+
+ token = cli_client._skyvern_instance.set(None)
+ try:
+ CliRunner().invoke(cli_app, args)
+ finally:
+ cli_client._skyvern_instance.reset(token)
+
+ assert requests
+ assert {request.headers.get("x-user-agent") for request in requests} == {"skyvern-cli"}
+
+
class TestCliRuntimeFlag:
def test_prepare_cli_runtime_marks_cli_process(self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
from skyvern import _cli_bootstrap
@@ -1196,6 +1251,7 @@ class TestCliRuntimeResetFixture:
run_reset = reset_cli_runtime_entry.__wrapped__
monkeypatch.setattr(_cli_bootstrap, "_CLI_RUNTIME_PREPARED", False)
+ monkeypatch.setattr(_cli_bootstrap, "_CLI_COMMAND_RUNNING", False)
monkeypatch.delenv("SKYVERN_API_KEY", raising=False)
monkeypatch.delenv("SKYVERN_BASE_URL", raising=False)
monkeypatch.setenv("SKYVERN_ENV_INTENT", "auto")
@@ -1204,6 +1260,7 @@ class TestCliRuntimeResetFixture:
next(teardown)
_cli_bootstrap._CLI_RUNTIME_PREPARED = True
+ _cli_bootstrap.mark_cli_command()
os.environ["SKYVERN_API_KEY"] = "leaked-key"
os.environ["SKYVERN_BASE_URL"] = "https://leaked.example"
os.environ["SKYVERN_ENV_INTENT"] = "cloud"
@@ -1212,6 +1269,7 @@ class TestCliRuntimeResetFixture:
next(teardown)
assert _cli_bootstrap._CLI_RUNTIME_PREPARED is False
+ assert _cli_bootstrap.is_cli_command() is False
assert "SKYVERN_API_KEY" not in os.environ
assert "SKYVERN_BASE_URL" not in os.environ
assert os.environ["SKYVERN_ENV_INTENT"] == "auto"
diff --git a/tests/unit/test_copilot_repair_origin_run.py b/tests/unit/test_copilot_repair_origin_run.py
index 4ffa31614..59a055905 100644
--- a/tests/unit/test_copilot_repair_origin_run.py
+++ b/tests/unit/test_copilot_repair_origin_run.py
@@ -1094,6 +1094,7 @@ _WORKFLOW_FIELDS_NOT_COMPARED = frozenset(
"created_by",
"edited_by",
"copilot_authored",
+ "build_source",
# Filled only by the workflow detail endpoint; never read at run time.
"effective_default_engine",
"original_created_by",
diff --git a/tests/unit/test_workflow_copilot_route.py b/tests/unit/test_workflow_copilot_route.py
index 4d93a1fd1..2ba3205b1 100644
--- a/tests/unit/test_workflow_copilot_route.py
+++ b/tests/unit/test_workflow_copilot_route.py
@@ -9000,6 +9000,7 @@ async def test_restore_records_only_a_rollback_that_landed(
sequential_key=None,
created_by=None,
edited_by=None,
+ build_source=None,
)
update = AsyncMock(side_effect=RuntimeError("canonical write failed") if update_fails else None)
monkeypatch.setattr(
diff --git a/tests/unit/workflow/test_block_browser_state_recovery.py b/tests/unit/workflow/test_block_browser_state_recovery.py
index 1ca7c9ccd..145ff7202 100644
--- a/tests/unit/workflow/test_block_browser_state_recovery.py
+++ b/tests/unit/workflow/test_block_browser_state_recovery.py
@@ -667,6 +667,23 @@ async def test_reconnect_restores_stale_state_before_cancelled_replacement_shutd
await asyncio.gather(*list(state._detached_teardown_tasks), return_exceptions=True)
+async def _reconnect_guarded_from_stop_start(state: RealBrowserState, stop_started: asyncio.Event) -> None:
+ # reconnect arms its BROWSER_CLOSE_TIMEOUT wait on a stop before the stop runs, so a guard armed once the stop has
+ # started cannot fire ahead of that cap during a main-thread pause such as a full GC.
+ reconnect = asyncio.create_task(state.reconnect(browser_address="ws://remote-browser"))
+ stop_started_wait = asyncio.create_task(stop_started.wait())
+ await asyncio.wait({reconnect, stop_started_wait}, timeout=30, return_when=asyncio.FIRST_COMPLETED)
+ stop_started_wait.cancel()
+ if not stop_started.is_set() and not reconnect.done():
+ reconnect.cancel()
+ pytest.fail("reconnect did not reach the stop under test within 30s")
+ done, _ = await asyncio.wait({reconnect}, timeout=1)
+ if not done:
+ reconnect.cancel()
+ pytest.fail("reconnect kept waiting on a stop past BROWSER_CLOSE_TIMEOUT")
+ await reconnect
+
+
@pytest.mark.asyncio
async def test_reconnect_bounds_fresh_driver_shutdown_when_state_rebuild_fails(
monkeypatch: pytest.MonkeyPatch,
@@ -694,7 +711,7 @@ async def test_reconnect_bounds_fresh_driver_shutdown_when_state_rebuild_fails(
monkeypatch.setattr(state, "check_and_fix_state", AsyncMock(side_effect=RuntimeError("cdp handshake failed")))
with pytest.raises(RuntimeError, match="cdp handshake failed"):
- await asyncio.wait_for(state.reconnect(browser_address="ws://remote-browser"), timeout=1)
+ await _reconnect_guarded_from_stop_start(state, stop_started)
assert stop_started.is_set()
fresh_pw.stop.assert_awaited_once()
@@ -1026,7 +1043,7 @@ async def test_reconnect_bounds_stale_driver_shutdown_after_guarded_replacement(
check_and_fix = AsyncMock()
monkeypatch.setattr(state, "check_and_fix_state", check_and_fix)
- await asyncio.wait_for(state.reconnect(browser_address="ws://remote-browser"), timeout=1)
+ await _reconnect_guarded_from_stop_start(state, stop_started)
await asyncio.wait_for(retry_started.wait(), timeout=1)
await asyncio.gather(*list(state._detached_teardown_tasks), return_exceptions=True)
@@ -1041,9 +1058,11 @@ async def test_reconnect_bounds_stale_driver_shutdown_after_guarded_replacement(
async def test_reconnect_does_not_retry_until_cancellation_resistant_stale_shutdown_completes(
monkeypatch: pytest.MonkeyPatch,
) -> None:
+ stop_started = asyncio.Event()
release_stop = asyncio.Event()
async def cancellation_resistant_stop() -> None:
+ stop_started.set()
try:
await asyncio.Event().wait()
except asyncio.CancelledError:
@@ -1063,7 +1082,7 @@ async def test_reconnect_does_not_retry_until_cancellation_resistant_stale_shutd
state._connection_status = MagicMock(return_value=(False, "playwright_driver_connection_closed"))
monkeypatch.setattr(state, "check_and_fix_state", AsyncMock())
- await asyncio.wait_for(state.reconnect(browser_address="ws://remote-browser"), timeout=1)
+ await _reconnect_guarded_from_stop_start(state, stop_started)
await asyncio.sleep(0.02)
# The first stop remains owned without racing a second stop.
@@ -1080,6 +1099,7 @@ async def test_reconnect_does_not_retry_until_cancellation_resistant_stale_shutd
async def test_reconnect_retries_after_cancellation_resistant_stale_shutdown_eventually_fails(
monkeypatch: pytest.MonkeyPatch,
) -> None:
+ stop_started = asyncio.Event()
release_stop = asyncio.Event()
stop_attempts = 0
@@ -1088,6 +1108,7 @@ async def test_reconnect_retries_after_cancellation_resistant_stale_shutdown_eve
stop_attempts += 1
if stop_attempts != 1:
return
+ stop_started.set()
try:
await asyncio.Event().wait()
except asyncio.CancelledError:
@@ -1108,7 +1129,7 @@ async def test_reconnect_retries_after_cancellation_resistant_stale_shutdown_eve
state._connection_status = MagicMock(return_value=(False, "playwright_driver_connection_closed"))
monkeypatch.setattr(state, "check_and_fix_state", AsyncMock())
- await asyncio.wait_for(state.reconnect(browser_address="ws://remote-browser"), timeout=1)
+ await _reconnect_guarded_from_stop_start(state, stop_started)
await asyncio.sleep(0.02)
assert stale_pw.stop.await_count == 1