Skip to content

Automate browser based workflows with AI

Build Twilio test fixture ids at runtime so the OSS sync passes push protection (SKY-16550) (#8612)

Shuchang Zhenge2f4014e48e6
diff --git a/alembic/versions/2026_09_18_0344-0aa6bda1df12_add_totp_identifiers_to_vault_credential_parameters.py b/alembic/versions/2026_09_18_0344-0aa6bda1df12_add_totp_identifiers_to_vault_credential_parameters.py
new file mode 100644
index 000000000..da2d8adf7
--- /dev/null
+++ b/alembic/versions/2026_09_18_0344-0aa6bda1df12_add_totp_identifiers_to_vault_credential_parameters.py
@@ -0,0 +1,44 @@
+"""add TOTP identifiers to vault credential parameters
+
+Revision ID: 0aa6bda1df12
+Revises: f65c97b21be8
+Create Date: 2026-09-18T03:44:30.102805+00:00
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "0aa6bda1df12"
+down_revision: Union[str, None] = "f65c97b21be8"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def _set_local_ddl_timeouts() -> None:
+ """Bound live-table DDL waits, including after OSS code generation."""
+ if op.get_bind().dialect.name == "postgresql":
+ op.execute("SET LOCAL lock_timeout = '5s'")
+ op.execute("SET LOCAL statement_timeout = '3h'")
+
+
+def upgrade() -> None:
+ _set_local_ddl_timeouts()
+ op.add_column(
+ "onepassword_credential_parameters",
+ sa.Column("totp_identifier", sa.String(), nullable=True),
+ )
+ op.add_column(
+ "bitwarden_login_credential_parameters",
+ sa.Column("totp_identifier", sa.String(), nullable=True),
+ )
+
+
+def downgrade() -> None:
+ _set_local_ddl_timeouts()
+ op.drop_column("bitwarden_login_credential_parameters", "totp_identifier")
+ op.drop_column("onepassword_credential_parameters", "totp_identifier")
diff --git a/alembic/versions/2026_09_18_0344-214808602675_add_sms_2fa_foundation.py b/alembic/versions/2026_09_18_0344-214808602675_add_sms_2fa_foundation.py
new file mode 100644
index 000000000..dca61e258
--- /dev/null
+++ b/alembic/versions/2026_09_18_0344-214808602675_add_sms_2fa_foundation.py
@@ -0,0 +1,227 @@
+"""add SMS 2FA foundation
+
+Revision ID: 214808602675
+Revises: eee46e1b1dbf
+Create Date: 2026-09-18T03:44:30.100226+00:00
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "214808602675"
+down_revision: Union[str, None] = "eee46e1b1dbf"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def _set_local_lock_timeout() -> None:
+ """Bound lock waits for the current transaction, including after OSS codegen."""
+ if op.get_bind().dialect.name == "postgresql":
+ op.execute("SET LOCAL lock_timeout = '5s'")
+
+
+_CREDENTIALS_UNIQUE_INDEX = "uq_credentials_id_org"
+_CREDENTIALS_UNIQUE_CONSTRAINT = "uq_credentials_id_org"
+
+
+def _credentials_constraint_exists() -> bool:
+ return bool(
+ op.get_bind()
+ .execute(
+ sa.text("SELECT 1 FROM pg_constraint WHERE conname = :name AND conrelid = 'credentials'::regclass"),
+ {"name": _CREDENTIALS_UNIQUE_CONSTRAINT},
+ )
+ .scalar()
+ )
+
+
+def _credentials_invalid_index() -> str | None:
+ return (
+ op.get_bind()
+ .execute(
+ sa.text(
+ "SELECT c.oid::regclass::text FROM pg_class c "
+ "JOIN pg_index i ON i.indexrelid = c.oid "
+ "WHERE c.relname = :name "
+ "AND i.indrelid = 'credentials'::regclass "
+ "AND NOT i.indisvalid"
+ ),
+ {"name": _CREDENTIALS_UNIQUE_INDEX},
+ )
+ .scalar()
+ )
+
+
+def _reset_index_timeouts() -> None:
+ for statement in ("RESET statement_timeout;", "RESET lock_timeout;"):
+ try:
+ op.execute(statement)
+ except Exception:
+ # A failed concurrent statement can leave the transaction aborted.
+ # Never replace its useful provider/driver exception with cleanup noise.
+ pass
+
+
+def _build_credentials_unique_index() -> None:
+ invalid_leftover = _credentials_invalid_index()
+ with op.get_context().autocommit_block():
+ try:
+ op.execute("SET lock_timeout = '5s';")
+ op.execute("SET statement_timeout = '3h';")
+ if invalid_leftover:
+ op.execute(f"DROP INDEX CONCURRENTLY IF EXISTS {invalid_leftover};")
+ op.execute(
+ f"CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS {_CREDENTIALS_UNIQUE_INDEX} "
+ "ON credentials (credential_id, organization_id);"
+ )
+ except Exception:
+ _reset_index_timeouts()
+ raise
+ _reset_index_timeouts()
+
+
+def _ensure_credentials_unique_constraint() -> None:
+ if op.get_bind().dialect.name != "postgresql":
+ op.create_unique_constraint(
+ _CREDENTIALS_UNIQUE_CONSTRAINT,
+ "credentials",
+ ["credential_id", "organization_id"],
+ )
+ return
+ if _credentials_constraint_exists():
+ return
+ _build_credentials_unique_index()
+ # Attach in its own transaction so the live-table lock ends before the
+ # remaining foundation DDL starts.
+ with op.get_context().autocommit_block():
+ op.execute("BEGIN")
+ try:
+ _set_local_lock_timeout()
+ op.execute(
+ 'ALTER TABLE "credentials" ADD CONSTRAINT "uq_credentials_id_org" '
+ 'UNIQUE USING INDEX "uq_credentials_id_org"'
+ )
+ op.execute("COMMIT")
+ except Exception:
+ try:
+ op.execute("ROLLBACK")
+ except Exception:
+ pass
+ raise
+
+
+def upgrade() -> None:
+ _ensure_credentials_unique_constraint()
+ op.create_table(
+ "organization_sms_configs",
+ sa.Column("sms_config_id", sa.String(), nullable=False),
+ sa.Column("organization_id", sa.String(), nullable=False),
+ sa.Column("mode", sa.String(), nullable=False),
+ sa.Column("encrypted_webhook_secret", sa.String(), nullable=False),
+ sa.Column(
+ "webhook_secret_encrypted_method",
+ sa.String(),
+ server_default=sa.text("'aes'"),
+ nullable=False,
+ ),
+ sa.Column("daily_ingest_cap", sa.Integer(), server_default=sa.text("100"), nullable=False),
+ sa.Column("created_at", sa.DateTime(), nullable=False),
+ sa.Column("modified_at", sa.DateTime(), nullable=False),
+ sa.Column("deleted_at", sa.DateTime(), nullable=True),
+ sa.ForeignKeyConstraint(["organization_id"], ["organizations.organization_id"]),
+ sa.PrimaryKeyConstraint("sms_config_id"),
+ sa.UniqueConstraint(
+ "sms_config_id",
+ "organization_id",
+ name="uq_organization_sms_configs_id_org",
+ ),
+ )
+ op.create_index(
+ "ix_organization_sms_configs_organization_id",
+ "organization_sms_configs",
+ ["organization_id"],
+ unique=False,
+ )
+ op.create_index(
+ "uq_org_sms_configs_one_connected",
+ "organization_sms_configs",
+ ["organization_id"],
+ unique=True,
+ postgresql_where=sa.text("mode = 'connected' AND deleted_at IS NULL"),
+ sqlite_where=sa.text("mode = 'connected' AND deleted_at IS NULL"),
+ )
+ op.create_table(
+ "organization_phone_numbers",
+ sa.Column("phone_number_id", sa.String(), nullable=False),
+ sa.Column("organization_id", sa.String(), nullable=False),
+ sa.Column("sms_config_id", sa.String(), nullable=False),
+ sa.Column("phone_number", sa.String(), nullable=False),
+ sa.Column("provider", sa.String(), server_default=sa.text("'twilio'"), nullable=False),
+ sa.Column("provider_number_sid", sa.String(), nullable=True),
+ sa.Column("previous_sms_url", sa.String(), nullable=True),
+ sa.Column("previous_sms_method", sa.String(), nullable=True),
+ sa.Column("previous_sms_application_sid", sa.String(), nullable=True),
+ sa.Column("credential_id", sa.String(), nullable=True),
+ sa.Column("provider_cost_cents", sa.Integer(), nullable=True),
+ sa.Column("price_cents", sa.Integer(), nullable=True),
+ sa.Column("status", sa.String(), server_default=sa.text("'active'"), nullable=False),
+ sa.Column("quarantined_until", sa.DateTime(), nullable=True),
+ sa.Column("created_at", sa.DateTime(), nullable=False),
+ sa.Column("modified_at", sa.DateTime(), nullable=False),
+ sa.Column("deleted_at", sa.DateTime(), nullable=True),
+ sa.ForeignKeyConstraint(
+ ["credential_id", "organization_id"],
+ ["credentials.credential_id", "credentials.organization_id"],
+ name="fk_organization_phone_numbers_credential_org",
+ ),
+ sa.ForeignKeyConstraint(["organization_id"], ["organizations.organization_id"]),
+ sa.ForeignKeyConstraint(
+ ["sms_config_id", "organization_id"],
+ [
+ "organization_sms_configs.sms_config_id",
+ "organization_sms_configs.organization_id",
+ ],
+ name="fk_organization_phone_numbers_sms_config_org",
+ ),
+ sa.PrimaryKeyConstraint("phone_number_id"),
+ )
+ op.create_index(
+ "ix_organization_phone_numbers_organization_id",
+ "organization_phone_numbers",
+ ["organization_id"],
+ unique=False,
+ )
+ op.create_index(
+ "ix_organization_phone_numbers_sms_config_id",
+ "organization_phone_numbers",
+ ["sms_config_id"],
+ unique=False,
+ )
+ op.create_index(
+ "uq_org_phone_numbers_org_number",
+ "organization_phone_numbers",
+ ["organization_id", "phone_number"],
+ unique=True,
+ postgresql_where=sa.text("status = 'active' AND deleted_at IS NULL"),
+ sqlite_where=sa.text("status = 'active' AND deleted_at IS NULL"),
+ )
+ _set_local_lock_timeout()
+ op.add_column("totp_codes", sa.Column("external_message_id", sa.String(), nullable=True))
+
+
+def downgrade() -> None:
+ _set_local_lock_timeout()
+ op.drop_column("totp_codes", "external_message_id")
+ op.drop_index("uq_org_phone_numbers_org_number", table_name="organization_phone_numbers")
+ op.drop_index("ix_organization_phone_numbers_sms_config_id", table_name="organization_phone_numbers")
+ op.drop_index("ix_organization_phone_numbers_organization_id", table_name="organization_phone_numbers")
+ op.drop_table("organization_phone_numbers")
+ op.drop_constraint("uq_credentials_id_org", "credentials", type_="unique")
+ op.drop_index("ix_organization_sms_configs_organization_id", table_name="organization_sms_configs")
+ op.drop_index("uq_org_sms_configs_one_connected", table_name="organization_sms_configs")
+ op.drop_table("organization_sms_configs")
diff --git a/alembic/versions/2026_09_18_0344-2dace0960323_add_non_secret_totp_seed_presence_to_credentials.py b/alembic/versions/2026_09_18_0344-2dace0960323_add_non_secret_totp_seed_presence_to_credentials.py
new file mode 100644
index 000000000..bd0fbd701
--- /dev/null
+++ b/alembic/versions/2026_09_18_0344-2dace0960323_add_non_secret_totp_seed_presence_to_credentials.py
@@ -0,0 +1,39 @@
+"""add non-secret TOTP seed presence to credentials
+
+Revision ID: 2dace0960323
+Revises: 0aa6bda1df12
+Create Date: 2026-09-18T03:44:30.103718+00:00
+
+"""
+
+from typing import Sequence, Union
+
+import sqlalchemy as sa
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "2dace0960323"
+down_revision: Union[str, None] = "0aa6bda1df12"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+
+def _set_local_ddl_timeouts() -> None:
+ """Bound live-table DDL waits, including after OSS code generation."""
+ if op.get_bind().dialect.name == "postgresql":
+ op.execute("SET LOCAL lock_timeout = '5s'")
+ op.execute("SET LOCAL statement_timeout = '3h'")
+
+
+def upgrade() -> None:
+ _set_local_ddl_timeouts()
+ op.add_column(
+ "credentials",
+ sa.Column("has_totp_seed", sa.Boolean(), nullable=True),
+ )
+
+
+def downgrade() -> None:
+ _set_local_ddl_timeouts()
+ op.drop_column("credentials", "has_totp_seed")
diff --git a/alembic/versions/2026_09_18_0344-f65c97b21be8_add_totp_external_message_dedupe_index.py b/alembic/versions/2026_09_18_0344-f65c97b21be8_add_totp_external_message_dedupe_index.py
new file mode 100644
index 000000000..c6cadf806
--- /dev/null
+++ b/alembic/versions/2026_09_18_0344-f65c97b21be8_add_totp_external_message_dedupe_index.py
@@ -0,0 +1,60 @@
+"""add TOTP external-message dedupe index
+
+Revision ID: f65c97b21be8
+Revises: 214808602675
+Create Date: 2026-09-18T03:44:30.101783+00:00
+
+"""
+
+from typing import Sequence, Union
+
+from sqlalchemy import text
+
+from alembic import op
+
+# revision identifiers, used by Alembic.
+revision: str = "f65c97b21be8"
+down_revision: Union[str, None] = "214808602675"
+branch_labels: Union[str, Sequence[str], None] = None
+depends_on: Union[str, Sequence[str], None] = None
+
+INDEX_NAME = "uq_totp_codes_org_external_message_id"
+
+
+def upgrade() -> None:
+ invalid_leftover = (
+ op.get_bind()
+ .execute(
+ text(
+ "SELECT c.oid::regclass::text FROM pg_class c JOIN pg_index i ON i.indexrelid = c.oid "
+ "WHERE c.relname = :name AND i.indrelid = 'totp_codes'::regclass AND NOT i.indisvalid"
+ ),
+ {"name": INDEX_NAME},
+ )
+ .scalar()
+ )
+ with op.get_context().autocommit_block():
+ op.execute("SET lock_timeout = '5s';")
+ op.execute("SET statement_timeout = '3h';")
+ try:
+ if invalid_leftover:
+ op.execute(f"DROP INDEX CONCURRENTLY IF EXISTS {invalid_leftover};")
+ op.execute(
+ f"CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS {INDEX_NAME} "
+ "ON totp_codes (organization_id, external_message_id) "
+ "WHERE external_message_id IS NOT NULL"
+ )
+ finally:
+ op.execute("RESET statement_timeout;")
+ op.execute("RESET lock_timeout;")
+
+
+def downgrade() -> None:
+ with op.get_context().autocommit_block():
+ op.execute("SET lock_timeout = '5s';")
+ op.execute("SET statement_timeout = '3h';")
+ try:
+ op.execute(f"DROP INDEX CONCURRENTLY IF EXISTS {INDEX_NAME};")
+ finally:
+ op.execute("RESET statement_timeout;")
+ op.execute("RESET lock_timeout;")
diff --git a/docs/api-reference/openapi.json b/docs/api-reference/openapi.json
index 75b874c48..ffbe29643 100644
--- a/docs/api-reference/openapi.json
+++ b/docs/api-reference/openapi.json
@@ -7842,6 +7842,37 @@
"other_information": {
"title": "Other Information"
},
+ "terminate_criterion": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "Terminate Criterion",
+ "description": "Condition that stops Apply. Omit to use the recipe default; null or empty text disables it.",
+ "default": "Terminate when the requested job posting explicitly states that it is expired, closed, filled, or no longer accepting applications. A missing Apply button, redirect, or HTTP 404 alone is insufficient."
+ },
+ "error_code_mapping": {
+ "anyOf": [
+ {
+ "additionalProperties": {
+ "type": "string"
+ },
+ "type": "object"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "Error Code Mapping",
+ "description": "Error codes and descriptions. Omit for the recipe default; null or {} disables mapping. A supplied mapping replaces the default independently of terminate_criterion.",
+ "default": {
+ "job_expired": "The requested job posting explicitly states that it is expired, closed, filled, or no longer accepting applications."
+ }
+ },
"username": {
"anyOf": [
{
@@ -7926,6 +7957,32 @@
],
"title": "Other Information"
},
+ "terminate_criterion": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "Terminate Criterion",
+ "description": "Condition that stops Apply. Omit for the recipe default; empty text disables it.",
+ "default": "Terminate when the requested job posting explicitly states that it is expired, closed, filled, or no longer accepting applications. A missing Apply button, redirect, or HTTP 404 alone is insufficient."
+ },
+ "error_code_mapping": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "Error Code Mapping",
+ "description": "JSON text containing an object of error codes and descriptions. Omit for the recipe default; JSON null or {} disables mapping. A supplied mapping replaces the default independently of terminate_criterion.",
+ "default": "{\"job_expired\": \"The requested job posting explicitly states that it is expired, closed, filled, or no longer accepting applications.\"}"
+ },
"username": {
"anyOf": [
{
@@ -9971,6 +10028,17 @@
],
"title": "Bitwarden Item Id"
},
+ "totp_identifier": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "Totp Identifier"
+ },
"created_at": {
"type": "string",
"format": "date-time",
@@ -10074,6 +10142,17 @@
}
],
"title": "Bitwarden Item Id"
+ },
+ "totp_identifier": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "Totp Identifier"
}
},
"type": "object",
@@ -20655,6 +20734,17 @@
],
"title": "Totp Field Name"
},
+ "totp_identifier": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "Totp Identifier"
+ },
"created_at": {
"type": "string",
"format": "date-time",
@@ -20731,6 +20821,17 @@
}
],
"title": "Totp Field Name"
+ },
+ "totp_identifier": {
+ "anyOf": [
+ {
+ "type": "string"
+ },
+ {
+ "type": "null"
+ }
+ ],
+ "title": "Totp Identifier"
}
},
"type": "object",
@@ -21173,6 +21274,12 @@
"+14155550123"
]
+ },
+ "has_totp": {
+ "type": "boolean",
+ "title": "Has Totp",
+ "description": "Whether the stored password credential includes a non-empty TOTP seed. Legacy rows with an unknown value report false until the credential is updated.",
+ "default": false
}
},
"type": "object",
diff --git a/docs/cookbooks/job-application-filler.mdx b/docs/cookbooks/job-application-filler.mdx
index 081a3798b..182d8de65 100644
--- a/docs/cookbooks/job-application-filler.mdx
+++ b/docs/cookbooks/job-application-filler.mdx
@@ -40,6 +40,8 @@ You can use the [recipe page](https://app.skyvern.com/recipes/job-apps) or the A
If the site uses two-factor authentication, enable **Uses 2FA?** and supply your configured 2FA identifier when needed.
</Step>
<Step title="Start and track the application">
+ Open **Advanced options** to edit **Terminate if** or **Error code mapping**. The defaults stop an application when the posting explicitly says it is closed and classify that outcome as `job_expired`. Use **Reset to defaults** to restore both settings.
+
Submit the form, then open the returned run link. The **Runs** panel lists applications and job extractions with their status.
</Step>
</Steps>
@@ -83,6 +85,8 @@ The response contains the new run ID:
The run executes asynchronously. This response confirms that the run started; poll its status to get the extraction output.
+Extract can complete for a closed posting when its details remain readable. The normal `extract_output.extracted_information` object includes `availability`: `closed` for explicit closure, `open` for evidence that applications are accepted, or `unknown` when the page is ambiguous. Readable details remain in that object. Closure does not itself make Extract fail.
+
### Apply with a resume URL
[Apply to a job](/api-reference/recipes/apply-to-a-job) accepts JSON with `job_url`, `resume_url`, and `custom_prompt`. Include `custom_prompt` even when you have no instructions; use an empty string.
@@ -123,6 +127,39 @@ The API supports PDF, DOCX, and image resumes. The default upload limit is 30 Mi
Both Apply request formats return only `workflow_run_id`. Check the run result to determine whether the application was submitted.
+### Configure termination and error codes
+
+Apply accepts two optional settings in either request format. If you omit them, it uses these defaults:
+
+```json
+{
+ "terminate_criterion": "Terminate when the requested job posting explicitly states that it is expired, closed, filled, or no longer accepting applications. A missing Apply button, redirect, or HTTP 404 alone is insufficient.",
+ "error_code_mapping": {
+ "job_expired": "The requested job posting explicitly states that it is expired, closed, filled, or no longer accepting applications."
+ }
+}
+```
+
+`terminate_criterion` tells the agent when to stop. `error_code_mapping` defines the codes that the failure classifier may return. Keep both when you want early stopping and a stable code. A prompt alone does not define a machine-readable error code.
+
+| Setting | Omitted | Disabled | Custom value |
+|-----------------------|-------------------------------|----------------|-----------------------------|
+| `terminate_criterion` | Default closure rule | `null` or `""` | Replaces the default rule |
+| `error_code_mapping` | Default `job_expired` mapping | `null` or `{}` | Replaces the entire mapping |
+
+The settings are independent. Clearing a rule does not clear the mapping, and clearing the mapping does not clear the rule. Use a JSON object with nonempty string keys and string descriptions for the mapping. Only configured codes can be returned; replacing the mapping can remove `job_expired`.
+
+In multipart requests, send the criterion as text and the mapping as JSON text:
+
+```bash
+--form 'terminate_criterion=Stop when the posting explicitly says applications are closed.' \
+--form 'error_code_mapping={"job_expired":"The posting explicitly says applications are closed."}'
+```
+
+Send text `null` or `{}` to disable the multipart mapping, or an empty criterion field to disable the rule. In the dashboard, clear **Terminate if** or enter `null` or `{}` in **Error code mapping**. Advanced settings apply to each URL in a bulk submission and are not saved in browser storage.
+
+Apply checks closure when it observes the posting; resume parsing or login may already have occurred. The default rule applies only to explicit expiry, closure, filled positions, or statements that applications are no longer accepted. A missing button, redirect, or HTTP 404 alone is insufficient. Editing or disabling the rule uses agent execution when a deterministic adapter cannot represent it.
+
### Supply credentials and a 2FA identifier
For a site that needs login, add these fields to the JSON Apply body:
@@ -158,6 +195,8 @@ curl 'https://api.skyvern.com/v1/runs/wr_123' \
Poll this endpoint while the run is active. When it finishes, inspect its status and output. A successful launch response alone does not confirm extraction or application completion.
+With the default Apply settings, an explicitly closed posting terminates the run. Inspect the standard `status`, `failure_reason`, and `errors` fields. A classified expiry has `error_code: "job_expired"`, with `reasoning` and `confidence_float`, in `errors`. The same fields use the normal workflow webhook payload. Classification only returns configured codes when it can identify a match; an empty `errors` list is not proof that a posting is open. Extract availability appears in the normal workflow `outputs`, not in the recipe run list.
+
To list recipe runs for your organization, use [List job recipe runs](/api-reference/recipes/list-job-recipe-runs):
```bash
diff --git a/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx b/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx
index 36069391c..1c9333e42 100644
--- a/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx
+++ b/skyvern-frontend/src/routes/credentials/CredentialsModal.tsx
@@ -2277,7 +2277,7 @@ function CredentialsModal({
setIsOpen(open);
}}
>
- <DialogContent className="max-h-[90vh] w-[700px] max-w-[700px] overflow-y-auto [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:border-2 [&::-webkit-scrollbar-thumb]:border-slate-100 [&::-webkit-scrollbar-thumb]:bg-slate-300 dark:[&::-webkit-scrollbar-thumb]:border-slate-800 dark:[&::-webkit-scrollbar-thumb]:bg-slate-600 [&::-webkit-scrollbar-track]:bg-slate-100 dark:[&::-webkit-scrollbar-track]:bg-slate-800 [&::-webkit-scrollbar]:w-2">
+ <DialogContent className="ph-no-capture max-h-[90vh] w-[700px] max-w-[700px] overflow-y-auto [&::-webkit-scrollbar-thumb]:rounded-full [&::-webkit-scrollbar-thumb]:border-2 [&::-webkit-scrollbar-thumb]:border-slate-100 [&::-webkit-scrollbar-thumb]:bg-slate-300 dark:[&::-webkit-scrollbar-thumb]:border-slate-800 dark:[&::-webkit-scrollbar-thumb]:bg-slate-600 [&::-webkit-scrollbar-track]:bg-slate-100 dark:[&::-webkit-scrollbar-track]:bg-slate-800 [&::-webkit-scrollbar]:w-2">
<DialogHeader>
<DialogTitle className="font-bold">
{isEditMode ? "Edit Credential" : (heading ?? "Add Credential")}
diff --git a/skyvern-frontend/src/routes/runs/RunRouter.test.tsx b/skyvern-frontend/src/routes/runs/RunRouter.test.tsx
index f2e436573..5d9d351a4 100644
--- a/skyvern-frontend/src/routes/runs/RunRouter.test.tsx
+++ b/skyvern-frontend/src/routes/runs/RunRouter.test.tsx
@@ -7,6 +7,8 @@ import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { useWorkflowPermanentId } from "@/routes/workflows/WorkflowPermanentIdContext";
+import { StudioPaneDefaultsProvider } from "../workflows/studio/StudioPaneDefaults";
+import { useStudioPanes } from "../workflows/studio/useStudioPanes";
import { RunRouter } from "./RunRouter";
type RunQueryResult = {
@@ -71,10 +73,18 @@ vi.mock(
// verify both the branch choice and that the provider fed the id through.
vi.mock("@/routes/workflows/editor/WorkflowEditor", () => ({
WorkflowEditor: () => (
- <div data-testid="studio">studio:{useWorkflowPermanentId()}</div>
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <div data-testid="studio">studio:{useWorkflowPermanentId()}</div>
+ <PanesProbe />
+ </StudioPaneDefaultsProvider>
),
}));
+function PanesProbe() {
+ const { panes } = useStudioPanes();
+ return <output data-testid="panes">{panes.join(",")}</output>;
+}
+
function LocationProbe() {
const location = useLocation();
return (
@@ -144,8 +154,9 @@ describe("RunRouter", () => {
renderAt(`/runs/wr_1/${legacySubview}?active=act_1`);
expect(screen.getByTestId("location").textContent).toBe(
- `/runs/wr_1?active=act_1&view=${studioView}&panes=${panes}`,
+ `/runs/wr_1?active=act_1&view=${studioView}`,
);
+ expect(screen.getByTestId("panes").textContent).toBe(panes);
});
test.each([
@@ -157,8 +168,9 @@ describe("RunRouter", () => {
renderAt(`/runs/wr_1/${legacySubview}`);
expect(screen.getByTestId("location").textContent).toBe(
- `/runs/wr_1?view=${studioView}&panes=overview,browser`,
+ `/runs/wr_1?view=${studioView}`,
);
+ expect(screen.getByTestId("panes").textContent).toBe("overview,browser");
},
);
@@ -210,8 +222,9 @@ describe("RunRouter", () => {
test("embed=true renders a chrome-free Overview-only studio run", async () => {
renderAt("/runs/wr_1?embed=true");
expect(screen.getByTestId("location").textContent).toBe(
- "/runs/wr_1?embed=true&panes=overview",
+ "/runs/wr_1?embed=true",
);
+ expect(screen.getByTestId("panes").textContent).toBe("overview");
await waitFor(() => {
expect(screen.getByTestId("studio").textContent).toBe("studio:wpid_123");
});
@@ -220,8 +233,9 @@ describe("RunRouter", () => {
test("embedded recording links focus only the Browser pane", () => {
renderAt("/runs/wr_1/recording?embed=true");
expect(screen.getByTestId("location").textContent).toBe(
- "/runs/wr_1?embed=true&view=recording&panes=browser",
+ "/runs/wr_1?embed=true&view=recording",
);
+ expect(screen.getByTestId("panes").textContent).toBe("browser");
});
test("embedded in-app pane changes are not normalized away", () => {
@@ -237,8 +251,9 @@ describe("RunRouter", () => {
renderAt("/runs/wr_1?embed=true&panes=editor,browser");
expect(screen.getByTestId("location").textContent).toBe(
- "/runs/wr_1?embed=true&panes=overview",
+ "/runs/wr_1?embed=true&panes=editor,browser",
);
+ expect(screen.getByTestId("panes").textContent).toBe("overview");
});
test("a permanently failed run fetch lands on 404, not an endless spinner", () => {
diff --git a/skyvern-frontend/src/routes/runs/RunRouter.tsx b/skyvern-frontend/src/routes/runs/RunRouter.tsx
index aa9695c9c..eef175da6 100644
--- a/skyvern-frontend/src/routes/runs/RunRouter.tsx
+++ b/skyvern-frontend/src/routes/runs/RunRouter.tsx
@@ -23,12 +23,7 @@ import { TaskRecording } from "@/routes/tasks/detail/TaskRecording";
import { WorkflowEditor } from "@/routes/workflows/editor/WorkflowEditor";
import { WorkflowPermanentIdContext } from "@/routes/workflows/WorkflowPermanentIdContext";
import { useWorkflowRunWithWorkflowQuery } from "@/routes/workflows/hooks/useWorkflowRunWithWorkflowQuery";
-import {
- parsePanesParam,
- RUN_APPEND_PANES,
- STUDIO_PANES_PARAM,
- toReadableSearch,
-} from "@/routes/workflows/studio/panes";
+import { toReadableSearch } from "@/routes/workflows/studio/panes";
import { useTaskV2Query } from "@/routes/runs/useTaskV2Query";
const loadingIndicator = (
@@ -110,33 +105,8 @@ function RunRouter() {
(studioView === "outputs" || studioView === "inputs")
? "timeline"
: studioView;
- const embedded = searchParams.get("embed") === "true";
- const requestedView = routedStudioView ?? searchParams.get("view");
- const requiredPane = requestedView === "recording" ? "browser" : "overview";
- const embeddedPanes = parsePanesParam(searchParams.get(STUDIO_PANES_PARAM));
- const embeddedLayoutInvalid =
- embeddedPanes === null ||
- embeddedPanes.length === 0 ||
- embeddedPanes.some((pane) => pane !== "overview" && pane !== "browser");
- const normalizeEmbeddedLayout =
- embedded && (legacySubview !== undefined || embeddedLayoutInvalid);
- if (isWorkflowRun && (routedStudioView || normalizeEmbeddedLayout)) {
- if (routedStudioView) {
- searchParams.set("view", routedStudioView);
- }
- const panes = embedded
- ? [requiredPane]
- : (parsePanesParam(searchParams.get(STUDIO_PANES_PARAM)) ?? [
- ...RUN_APPEND_PANES,
- ]);
- searchParams.set(
- STUDIO_PANES_PARAM,
- embedded
- ? requiredPane
- : [requiredPane, ...panes.filter((pane) => pane !== requiredPane)].join(
- ",",
- ),
- );
+ if (isWorkflowRun && routedStudioView) {
+ searchParams.set("view", routedStudioView);
return (
<Navigate
to={{
diff --git a/skyvern-frontend/src/routes/workflows/StudioRouteGates.test.tsx b/skyvern-frontend/src/routes/workflows/StudioRouteGates.test.tsx
index 174b5a42a..4e22ef70d 100644
--- a/skyvern-frontend/src/routes/workflows/StudioRouteGates.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/StudioRouteGates.test.tsx
@@ -4,6 +4,8 @@ import { render, screen } from "@testing-library/react";
import { MemoryRouter, Route, Routes, useLocation } from "react-router-dom";
import { describe, expect, test, vi } from "vitest";
+import { StudioPaneDefaultsProvider } from "./studio/StudioPaneDefaults";
+import { useStudioPanes } from "./studio/useStudioPanes";
import { WorkflowRunRoute } from "./StudioRouteGates";
vi.mock("./editor/WorkflowEditor", () => ({
@@ -16,10 +18,14 @@ vi.mock("./LegacyBuildRedirect", () => ({
function LocationProbe() {
const location = useLocation();
+ const { panes } = useStudioPanes();
return (
- <div data-testid="location">
- {location.pathname + location.search + location.hash}
- </div>
+ <>
+ <output data-testid="panes">{panes.join(",")}</output>
+ <div data-testid="location">
+ {location.pathname + location.search + location.hash}
+ </div>
+ </>
);
}
@@ -36,13 +42,20 @@ describe("WorkflowRunRoute", () => {
path="/agents/:workflowPermanentId/:workflowRunId/*"
element={<WorkflowRunRoute />}
/>
- <Route path="/runs/:runId" element={<LocationProbe />} />
+ <Route
+ path="/runs/:runId"
+ element={
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <LocationProbe />
+ </StudioPaneDefaultsProvider>
+ }
+ />
</Routes>
</MemoryRouter>,
);
expect(screen.getByTestId("location").textContent).toBe(
- "/runs/wr_1?active=act_1&iteration=2&view=timeline&panes=overview,browser#frame",
+ "/runs/wr_1?active=act_1&iteration=2&view=timeline#frame",
);
});
@@ -64,21 +77,29 @@ describe("WorkflowRunRoute", () => {
path="/agents/:workflowPermanentId/:workflowRunId/*"
element={<WorkflowRunRoute />}
/>
- <Route path="/runs/:runId" element={<LocationProbe />} />
+ <Route
+ path="/runs/:runId"
+ element={
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <LocationProbe />
+ </StudioPaneDefaultsProvider>
+ }
+ />
</Routes>
</MemoryRouter>,
);
expect(screen.getByTestId("location").textContent).toBe(
- `/runs/wr_1?active=act_1&view=${studioView}&panes=${panes}#frame`,
+ `/runs/wr_1?active=act_1&view=${studioView}#frame`,
);
+ expect(screen.getByTestId("panes").textContent).toBe(panes);
});
test.each([
["code", "browser", "overview,browser"],
["recording", "overview", "browser,overview"],
])(
- "adds the pane needed for %s to an explicit layout",
+ "opens the pane needed for %s while preserving the explicit URL",
(legacySubview, initialPanes, expectedPanes) => {
render(
<MemoryRouter
@@ -91,14 +112,22 @@ describe("WorkflowRunRoute", () => {
path="/agents/:workflowPermanentId/:workflowRunId/*"
element={<WorkflowRunRoute />}
/>
- <Route path="/runs/:runId" element={<LocationProbe />} />
+ <Route
+ path="/runs/:runId"
+ element={
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <LocationProbe />
+ </StudioPaneDefaultsProvider>
+ }
+ />
</Routes>
</MemoryRouter>,
);
expect(screen.getByTestId("location").textContent).toContain(
- `panes=${expectedPanes}`,
+ `panes=${initialPanes}`,
);
+ expect(screen.getByTestId("panes").textContent).toBe(expectedPanes);
},
);
@@ -115,13 +144,20 @@ describe("WorkflowRunRoute", () => {
path="/agents/:workflowPermanentId/:workflowRunId/*"
element={<WorkflowRunRoute />}
/>
- <Route path="/runs/:runId" element={<LocationProbe />} />
+ <Route
+ path="/runs/:runId"
+ element={
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <LocationProbe />
+ </StudioPaneDefaultsProvider>
+ }
+ />
</Routes>
</MemoryRouter>,
);
expect(screen.getByTestId("location").textContent).toBe(
- `/runs/wr_1?view=${studioView}&panes=overview,browser`,
+ `/runs/wr_1?view=${studioView}`,
);
},
);
@@ -136,13 +172,21 @@ describe("WorkflowRunRoute", () => {
path="/agents/:workflowPermanentId/:workflowRunId/*"
element={<WorkflowRunRoute />}
/>
- <Route path="/runs/:runId" element={<LocationProbe />} />
+ <Route
+ path="/runs/:runId"
+ element={
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <LocationProbe />
+ </StudioPaneDefaultsProvider>
+ }
+ />
</Routes>
</MemoryRouter>,
);
expect(screen.getByTestId("location").textContent).toBe(
- "/runs/wr_1?embed=true&panes=overview&view=code",
+ "/runs/wr_1?embed=true&panes=browser&view=code",
);
+ expect(screen.getByTestId("panes").textContent).toBe("overview");
});
});
diff --git a/skyvern-frontend/src/routes/workflows/StudioRouteGates.tsx b/skyvern-frontend/src/routes/workflows/StudioRouteGates.tsx
index 5395a61f2..1d4d04503 100644
--- a/skyvern-frontend/src/routes/workflows/StudioRouteGates.tsx
+++ b/skyvern-frontend/src/routes/workflows/StudioRouteGates.tsx
@@ -2,12 +2,7 @@ import { Navigate, useLocation, useParams } from "react-router-dom";
import { LegacyBuildRedirect } from "./LegacyBuildRedirect";
import { WorkflowEditor } from "./editor/WorkflowEditor";
-import {
- parsePanesParam,
- RUN_APPEND_PANES,
- STUDIO_PANES_PARAM,
- toReadableSearch,
-} from "./studio/panes";
+import { toReadableSearch } from "./studio/panes";
export function BuildRoute() {
return <LegacyBuildRedirect />;
@@ -65,22 +60,6 @@ export function WorkflowRunRoute() {
: studioView;
if (routedStudioView) {
searchParams.set("view", routedStudioView);
- const embedded = searchParams.get("embed") === "true";
- const requiredPane =
- routedStudioView === "recording" ? "browser" : "overview";
- const panes = embedded
- ? [requiredPane]
- : (parsePanesParam(searchParams.get(STUDIO_PANES_PARAM)) ?? [
- ...RUN_APPEND_PANES,
- ]);
- searchParams.set(
- STUDIO_PANES_PARAM,
- embedded
- ? requiredPane
- : [requiredPane, ...panes.filter((pane) => pane !== requiredPane)].join(
- ",",
- ),
- );
}
return (
diff --git a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
index f29ce3661..e737a9263 100644
--- a/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
+++ b/skyvern-frontend/src/routes/workflows/copilot/WorkflowCopilotChat.tsx
@@ -153,7 +153,7 @@ import {
liveLocationState,
liveSearch,
} from "@/routes/workflows/studio/liveSearch";
-import { resolveOpenPanes } from "@/routes/workflows/studio/panes";
+import { useStudioPanes } from "@/routes/workflows/studio/useStudioPanes";
import { useRecordingStore } from "@/store/useRecordingStore";
import { useRecordingRefinementEvidenceStore } from "@/store/RecordingRefinementEvidenceStore";
import { useWorkflowBlockSearchStore } from "@/store/WorkflowBlockSearchStore";
@@ -1354,6 +1354,7 @@ export function WorkflowCopilotChat({
const lastFollowedLabelRef = useRef<string | null>(null);
// Focusing the turn's run is the copilot acting for the user, not a
// navigation they asked for, so it must not add a Back step.
+ const { resolveLivePanes } = useStudioPanes();
const switchStudioRun = useSwitchStudioRun({
replace: true,
systemFocus: true,
@@ -1579,7 +1580,7 @@ export function WorkflowCopilotChat({
return;
}
const nodeId = resolveTimelineBlockJumpNodeId({
- editorOpen: resolveOpenPanes(window.location.search).includes("editor"),
+ editorOpen: resolveLivePanes().includes("editor"),
targets: handle.getTargets(),
label,
});
@@ -1614,6 +1615,7 @@ export function WorkflowCopilotChat({
location.search,
location.hash,
location.state,
+ resolveLivePanes,
],
);
const recoverCredentialTurn = useRef<
diff --git a/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx b/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx
index 406fa872f..9d50ed765 100644
--- a/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/FlowRenderer.tsx
@@ -101,6 +101,7 @@ import {
} from "./collapse/useNodeCollapseStore";
import { isHeightCollapseAnimation } from "./collapse/collapseRelayoutAnimations";
import {
+ centeredNodeViewport,
isMeaningfulPaneResize,
isViewportStranded,
PANE_FIT_DEBOUNCE_MS,
@@ -108,7 +109,6 @@ import {
paneRefitDuration,
relayoutDriftCorrection,
START_ANCHOR_MIN_ZOOM,
- startAnchoredViewport,
} from "./paneFit";
import { useBlockerExit } from "./useBlockerExit";
import { WorkflowScopeContext } from "./WorkflowScopeContext";
@@ -439,6 +439,8 @@ type Props = {
// Studio pane-layout key (open set + committed divider widths); a change
// recenters the canvas once the layout settles.
paneLayoutKey?: string;
+ // A new Studio visit can reuse this mounted canvas.
+ paneEntryKey?: number;
};
function FlowRenderer({
@@ -465,6 +467,7 @@ function FlowRenderer({
centerOffsetX = 0,
embedded = false,
paneLayoutKey,
+ paneEntryKey,
}: Props) {
const { blockLabel: targettedBlockLabel } = useParams();
const reactFlowInstance = useReactFlow();
@@ -650,6 +653,7 @@ function FlowRenderer({
const meta = event.metaKey || event.ctrlKey;
if (event.shiftKey && (event.key === "!" || event.key === "1")) {
event.preventDefault();
+ lastCanvasInteractionAtRef.current = Date.now();
runFitViewRef.current?.();
return;
}
@@ -1761,11 +1765,12 @@ function FlowRenderer({
// Studio pane fit. The editor pane can mount hidden (display:none while
// closed) and resizes in discrete steps as sibling panes toggle. Fit once
// when the initial Dagre pass has settled AND the pane is visible; after
- // that, a pane-layout change (?panes=) recenters once the flex layout
+ // that, a pane-layout change recenters once the flex layout
// settles, and a debounced ResizeObserver re-fits only when a real
// geometry change leaves the viewport stranded, so neither path fights a
// deliberate pan/zoom.
const hasInitialPaneFitRef = useRef(false);
+ const initialPaneInteractionAtRef = useRef(0);
const lastPaneSizeRef = useRef<{ width: number; height: number } | null>(
null,
);
@@ -1773,8 +1778,13 @@ function FlowRenderer({
const paneLayoutChangedAtRef = useRef(0);
const paneSettleTimerRef = useRef<number | null>(null);
const paneSettleRef = useRef<(() => void) | null>(null);
+ const initialPaneTargetRef = useRef<{
+ nodeId: string;
+ ready: boolean;
+ interactionAt: number;
+ } | null>(null);
const layoutSettledRef = useRef(false);
- layoutSettledRef.current = layoutPhase !== "pre-layout";
+ layoutSettledRef.current = layoutPhase === "ready" && nodesInitialized;
// One debounce shared by the ResizeObserver and pane-layout changes, so a
// recenter fires once, after the last of the layout event and its resize
@@ -1789,30 +1799,28 @@ function FlowRenderer({
}, PANE_FIT_DEBOUNCE_MS);
}, []);
- // Anchors the flow's start at the pane top instead of centering the whole
- // graph, matching the legacy editor's default zoom on long workflows.
- const runStartAnchoredFit = useCallback(
+ const centerEntryTarget = useCallback(
(pane: { width: number; height: number }) => {
- const visibleNodes = reactFlowInstance
- .getNodes()
- .filter((node) => !node.hidden);
- if (visibleNodes.length === 0) {
- return;
- }
- const viewport = startAnchoredViewport({
+ const target = initialPaneTargetRef.current;
+ if (!target?.ready) return false;
+ const node = reactFlowInstance.getNode(target.nodeId);
+ const internal = reactFlowInstance.getInternalNode(target.nodeId);
+ if (!node || node.hidden || !internal) return false;
+ const viewport = centeredNodeViewport({
pane,
- bounds: getNodesBounds(visibleNodes),
+ bounds: {
+ ...internal.internals.positionAbsolute,
+ width: internal.measured.width ?? 0,
+ height: internal.measured.height ?? 0,
+ },
});
- if (viewport === null) {
- return;
- }
- // The initial anchor is deliberately instant (no animation, unlike the
- // recenter below); 50ms just covers the viewport state flush.
+ if (!viewport) return false;
fitViewInProgressRef.current = true;
- reactFlowInstance.setViewport(viewport);
+ void reactFlowInstance.setViewport(viewport);
window.setTimeout(() => {
fitViewInProgressRef.current = false;
}, 50);
+ return true;
},
[reactFlowInstance],
);
@@ -1862,10 +1870,16 @@ function FlowRenderer({
}, []);
const focusBlockForSearch = useCallback(
- (nodeId: string) => {
+ (nodeId: string, entryIsCurrent?: () => boolean) => {
const duration = blockJumpDuration();
- const getNodes = () => reactFlowInstance.getNodes() as Array<AppNode>;
- void focusBlockTarget(nodeId, {
+ if (!entryIsCurrent) {
+ lastCanvasInteractionAtRef.current = Date.now();
+ }
+ const getNodes = () =>
+ entryIsCurrent && !entryIsCurrent()
+ ? []
+ : (reactFlowInstance.getNodes() as Array<AppNode>);
+ return focusBlockTarget(nodeId, {
getNodes,
getInternalNode: (id) => reactFlowInstance.getInternalNode(id),
getPaneWidth: () =>
@@ -1873,6 +1887,8 @@ function FlowRenderer({
viewportZoom: reactFlowInstance.getViewport().zoom,
duration,
setViewport: (viewport, options) => {
+ // Entry reuses reveal/settle, then centers once at fixed zoom.
+ if (entryIsCurrent) return;
// An explicit jump outranks any pending pane-layout recenter and,
// like runFitView, must not be clamped by constrainPan mid-flight.
lastCanvasInteractionAtRef.current = Date.now();
@@ -1886,8 +1902,11 @@ function FlowRenderer({
fitViewInProgressRef.current = false;
}, options.duration + 50);
},
- selectBlock: setSelectedBlockId,
+ selectBlock: (id) => {
+ if (!entryIsCurrent) setSelectedBlockId(id);
+ },
beforeExpand: (label) => {
+ if (entryIsCurrent && !entryIsCurrent()) return;
const workflowId = workflow.workflow_permanent_id ?? "__global__";
if (
isBlockCollapsedAt(
@@ -1899,11 +1918,14 @@ function FlowRenderer({
collapseRelayoutBeforeDebounceRef.current = true;
}
},
- expandBlock: (label) =>
+ expandBlock: (label) => {
+ if (entryIsCurrent && !entryIsCurrent()) return;
useNodeCollapseStore
.getState()
- .expandBlock(workflow.workflow_permanent_id ?? "__global__", label),
+ .expandBlock(workflow.workflow_permanent_id ?? "__global__", label);
+ },
switchBranch: (conditionalId, branchId) => {
+ if (entryIsCurrent && !entryIsCurrent()) return;
// Same write and dirty-state guard as the branch tab click
// (BranchesEditor.handleSelectBranch): switching branches is UI
// state, so the `replace` change must not mark the workflow dirty.
@@ -1927,6 +1949,7 @@ function FlowRenderer({
getInternalNode: (id) => reactFlowInstance.getInternalNode(id),
isRelayoutPending: () =>
collapseRelayoutBeforeDebounceRef.current ||
+ isLayoutingRef.current ||
debouncedLayoutForDimensions.isPending(),
}),
});
@@ -1959,27 +1982,30 @@ function FlowRenderer({
};
}, [embedded, readOnly, reactFlowInstance, focusBlockForSearch]);
- // "initial-load" lands one frame after Dagre positions commit (mid fade-in),
- // so fitting here can't read pre-layout node positions. layoutPhase only
- // advances once nodesInitialized flips, but guard explicitly so a future
- // layout-phase refactor can't reintroduce a zero-size fit.
useEffect(() => {
- if (
- !embedded ||
- hasInitialPaneFitRef.current ||
- layoutPhase === "pre-layout" ||
- !nodesInitialized
- ) {
- return;
- }
- const rect = editorElementRef.current?.getBoundingClientRect();
- if (!rect || rect.width === 0 || rect.height === 0) {
- return;
- }
- hasInitialPaneFitRef.current = true;
- lastPaneSizeRef.current = { width: rect.width, height: rect.height };
- runStartAnchoredFit({ width: rect.width, height: rect.height });
- }, [embedded, layoutPhase, nodesInitialized, runStartAnchoredFit]);
+ if (!embedded) return;
+ hasInitialPaneFitRef.current = false;
+ initialPaneInteractionAtRef.current = lastCanvasInteractionAtRef.current;
+ initialPaneTargetRef.current = null;
+ pendingPaneRecenterRef.current = false;
+ lastPaneSizeRef.current = null;
+ schedulePaneSettle();
+ return () => {
+ // Invalidate any reveal still awaiting branch/container layout.
+ initialPaneTargetRef.current = null;
+ };
+ }, [
+ embedded,
+ paneEntryKey,
+ workflow.workflow_permanent_id,
+ schedulePaneSettle,
+ ]);
+
+ // Final node measurements can arrive after the pane's own resize. Keep the
+ // entry target centered through those passes, until the user takes control.
+ useEffect(() => {
+ if (embedded) schedulePaneSettle();
+ }, [embedded, nodes, layoutPhase, nodesInitialized, schedulePaneSettle]);
// Pane-set/order changes and committed divider resizes (drag release,
// double-click reset, keyboard step) are explicit recenter triggers: they
@@ -2008,16 +2034,66 @@ function FlowRenderer({
if (!el) {
return;
}
- // Covers a pane that was hidden while layout settled; before that, the
- // layout-phase effect above owns the first fit.
const initialFit = (size: { width: number; height: number }) => {
- if (!layoutSettledRef.current) {
+ if (!layoutSettledRef.current) return;
+ if (!initialPaneTargetRef.current) {
+ const currentNodes = reactFlowInstance.getNodes() as Array<AppNode>;
+ const selectedId = useWorkflowPanelStore.getState().selectedBlockId;
+ const node =
+ currentNodes.find(
+ (candidate) =>
+ candidate.id === selectedId && isWorkflowBlockNode(candidate),
+ ) ??
+ currentNodes.find(
+ (candidate) => candidate.type === "start" && !candidate.parentId,
+ );
+ if (!node) return;
+ const target = {
+ nodeId: node.id,
+ ready: false,
+ interactionAt: initialPaneInteractionAtRef.current,
+ };
+ initialPaneTargetRef.current = target;
+ const isCurrent = () =>
+ initialPaneTargetRef.current === target &&
+ lastCanvasInteractionAtRef.current === target.interactionAt;
+ const reveal = async () => {
+ if (isWorkflowBlockNode(node)) {
+ await focusBlockForSearch(node.id, isCurrent);
+ }
+ if (!isCurrent()) return;
+ // focusBlockTarget expands the selected block last. Wait again for
+ // that expansion's measurements, including nested absolute positions.
+ await waitForNodeSettle(node.id, {
+ getNodes: () =>
+ isCurrent()
+ ? (reactFlowInstance.getNodes() as Array<AppNode>)
+ : [],
+ getInternalNode: (id) => reactFlowInstance.getInternalNode(id),
+ isRelayoutPending: () =>
+ isLayoutingRef.current ||
+ collapseRelayoutBeforeDebounceRef.current ||
+ debouncedLayoutForDimensions.isPending(),
+ });
+ if (!isCurrent()) return;
+ target.ready = true;
+ schedulePaneSettle();
+ };
+ void reveal();
+ return;
+ }
+ if (
+ isLayoutingRef.current ||
+ collapseRelayoutBeforeDebounceRef.current ||
+ debouncedLayoutForDimensions.isPending()
+ ) {
+ schedulePaneSettle();
return;
}
+ if (!centerEntryTarget(size)) return;
hasInitialPaneFitRef.current = true;
pendingPaneRecenterRef.current = false;
lastPaneSizeRef.current = size;
- runStartAnchoredFit(size);
};
const paneRecenter = (size: { width: number; height: number }) => {
lastPaneSizeRef.current = size;
@@ -2054,10 +2130,28 @@ function FlowRenderer({
return;
}
const size = { width: rect.width, height: rect.height };
+ const target = initialPaneTargetRef.current;
+ if (
+ (!hasInitialPaneFitRef.current || target) &&
+ lastCanvasInteractionAtRef.current !==
+ initialPaneInteractionAtRef.current
+ ) {
+ // A user gesture also cancels an entry reveal that is still waiting.
+ initialPaneTargetRef.current = null;
+ hasInitialPaneFitRef.current = true;
+ // Do not treat cancellation during entry as a stranded resize.
+ lastPaneSizeRef.current = size;
+ }
if (!hasInitialPaneFitRef.current) {
initialFit(size);
return;
}
+ if (initialPaneTargetRef.current) {
+ // Initial pane-key effects must not replace the selected-node center
+ // with a whole-chain fit-width recenter.
+ initialFit(size);
+ return;
+ }
if (pendingPaneRecenterRef.current) {
pendingPaneRecenterRef.current = false;
paneRecenter(size);
@@ -2066,6 +2160,7 @@ function FlowRenderer({
strandedRefit(size);
};
paneSettleRef.current = settle;
+ schedulePaneSettle();
const markInteraction = () => {
lastCanvasInteractionAtRef.current = Date.now();
};
@@ -2074,12 +2169,15 @@ function FlowRenderer({
passive: true,
});
el.addEventListener("pointerdown", markInteraction, { capture: true });
+ // Click also covers keyboard activation without intercepting key presses.
+ el.addEventListener("click", markInteraction, { capture: true });
const observer = new ResizeObserver(() => {
schedulePaneSettle();
});
observer.observe(el);
return () => {
observer.disconnect();
+ el.removeEventListener("click", markInteraction, { capture: true });
el.removeEventListener("wheel", markInteraction, { capture: true });
el.removeEventListener("pointerdown", markInteraction, {
capture: true,
@@ -2095,7 +2193,9 @@ function FlowRenderer({
reactFlowInstance,
runFitView,
runPaneRecenter,
- runStartAnchoredFit,
+ centerEntryTarget,
+ focusBlockForSearch,
+ debouncedLayoutForDimensions,
schedulePaneSettle,
]);
@@ -2394,7 +2494,7 @@ function FlowRenderer({
nodeTypes={nodeTypes}
edgeTypes={edgeTypes}
// colorMode="dark"
- fitView={true}
+ fitView={!embedded}
fitViewOptions={{
maxZoom: 1,
}}
diff --git a/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.tsx b/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.tsx
index 7cebb814e..98f7697ce 100644
--- a/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/WorkflowEditor.tsx
@@ -43,7 +43,10 @@ function WorkflowEditor() {
studioEnabled && workflowQueryFailed && fallbackRun?.workflow?.deleted_at
? fallbackRun.workflow
: undefined;
- const effectiveWorkflow = fetchedWorkflow ?? deletedWorkflowSnapshot;
+ const effectiveWorkflow =
+ (fetchedWorkflow?.workflow_permanent_id === workflowPermanentId
+ ? fetchedWorkflow
+ : undefined) ?? deletedWorkflowSnapshot;
const { data: globalWorkflows, isLoading: isGlobalWorkflowsLoading } =
useGlobalWorkflowsQuery();
@@ -72,7 +75,13 @@ function WorkflowEditor() {
workflowQueryFailed &&
Boolean(deepLinkRunId) &&
fallbackRunIsLoading;
- if (isLoading || isGlobalWorkflowsLoading || awaitingRunFallback) {
+ if (
+ isLoading ||
+ isGlobalWorkflowsLoading ||
+ awaitingRunFallback ||
+ (fetchedWorkflow &&
+ fetchedWorkflow.workflow_permanent_id !== workflowPermanentId)
+ ) {
return (
<div className="flex h-screen w-full items-center justify-center">
<div className="animate-pulse">
diff --git a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
index d8eac3dca..ddad1feac 100644
--- a/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/Workspace.tsx
@@ -73,7 +73,7 @@ import { useCacheKeyValueStore } from "@/store/CacheKeyValueStore";
import { useRecordingStore } from "@/store/useRecordingStore";
import { useRecordedBlocksStore } from "@/store/RecordedBlocksStore";
import { useWorkflowSettingsStore } from "@/store/WorkflowSettingsStore";
-import { useStudioShellStore } from "@/store/StudioShellStore";
+import { useStudioPaneDefaults } from "../studio/StudioPaneDefaultsContext";
import { useCopilotActionStore } from "@/store/useCopilotActionStore";
import { useShowAllCodeStore } from "@/store/ShowAllCodeStore";
import { useSidebarSaveStateStore } from "@/store/SidebarSaveStateStore";
@@ -194,10 +194,6 @@ import {
useDiscoverCopilotPromptRecovery,
withoutDiscoverViaParam,
} from "../discoverCopilotHandoff";
-import {
- initialEditorAutoOpenState,
- shouldAutoOpenEditor,
-} from "./editorAutoOpen";
import { useStudioShellContext } from "../studio/StudioShellContext";
import { StudioShellPanelPortal } from "../studio/StudioShellPanelPortal";
import { useRecordingLauncherStore } from "@/store/useRecordingLauncherStore";
@@ -415,14 +411,9 @@ function Workspace({
const workflowPermanentId = useWorkflowPermanentId();
const { copilotPortalEl: studioCopilotPortalEl } = useStudioShellContext();
const { panes: studioPanes, openPane: openStudioPane } = useStudioPanes();
- const studioPaneWidths = useStudioShellStore((s) => s.paneWidths);
+ const { paneWidths: studioPaneWidths, entryId: studioEntryId } =
+ useStudioPaneDefaults();
const studioCopilotOpen = studioPanes.includes("copilot");
- // Armed iff the workflow has no blocks at mount — the studio shell remounts
- // Workspace per workflow, so `workflow` is always populated here. The first
- // copilot build that lands blocks auto-opens the Editor pane, exactly once.
- const editorAutoOpenStateRef = useRef(
- initialEditorAutoOpenState(workflow.workflow_definition.blocks.length),
- );
const location = useLocation();
const navigate = useNavigate();
const [searchParams] = useSearchParams();
@@ -2409,6 +2400,7 @@ function Workspace({
initialTitle={initialTitle}
workflow={workflow}
embedded={embedded}
+ paneEntryKey={embedded ? studioEntryId : undefined}
paneLayoutKey={
embedded
? `${studioPanes.join(",")}|${paneWidthsKey(studioPaneWidths)}`
@@ -3230,18 +3222,6 @@ function Workspace({
// snap-back); only version-restore/load call applyWorkflowUpdate
// without this and stay a clean baseline.
applyWorkflowUpdate(workflowData, { ...options, userDriven: true });
- const { fire, nextState } = shouldAutoOpenEditor(
- editorAutoOpenStateRef.current,
- {
- embedded,
- applied: options?.applied,
- blockCount: workflowData.workflow_definition.blocks.length,
- },
- );
- editorAutoOpenStateRef.current = nextState;
- if (fire) {
- openStudioPane("editor");
- }
} catch (error) {
console.error(
"Failed to parse and apply agent",
diff --git a/skyvern-frontend/src/routes/workflows/editor/nodes/components/NodeHeader.tsx b/skyvern-frontend/src/routes/workflows/editor/nodes/components/NodeHeader.tsx
index 85e2a0cb5..9eb45d73d 100644
--- a/skyvern-frontend/src/routes/workflows/editor/nodes/components/NodeHeader.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/nodes/components/NodeHeader.tsx
@@ -65,7 +65,6 @@ import { useBlockOutputStore } from "@/store/BlockOutputStore";
import { useDebugStore } from "@/store/useDebugStore";
import {
RUN_APPEND_PANES,
- STUDIO_PANES_PARAM,
withPanesOpen,
} from "@/routes/workflows/studio/panes";
import { useStudioPanes } from "@/routes/workflows/studio/useStudioPanes";
@@ -278,7 +277,7 @@ function NodeHeader({
const studioEnabled = useWorkflowStudioEnabled();
const queryClient = useQueryClient();
const location = useLocation();
- const { resolveLivePanes } = useStudioPanes();
+ const { resolveLivePanes, preserveNextEntry } = useStudioPanes();
const isDebuggable = debuggableWorkflowBlockTypes.has(type);
const isScriptable = scriptableWorkflowBlockTypes.has(type);
const {
@@ -561,7 +560,7 @@ function NodeHeader({
wr: response.data.run_id,
bl: label,
});
- search.set(STUDIO_PANES_PARAM, panes.join(","));
+ preserveNextEntry(`?${search.toString()}`, panes);
// Under the short /runs/{wr} URL the run id is the pathname, so keep it
// and only swap the search; from the editor it is a full studio path.
if (location.pathname.startsWith("/runs/")) {
diff --git a/skyvern-frontend/src/routes/workflows/editor/paneFit.ts b/skyvern-frontend/src/routes/workflows/editor/paneFit.ts
index c55bfa1b3..72fa75fbf 100644
--- a/skyvern-frontend/src/routes/workflows/editor/paneFit.ts
+++ b/skyvern-frontend/src/routes/workflows/editor/paneFit.ts
@@ -159,6 +159,30 @@ export function startAnchoredViewport({
};
}
+// Entry positioning uses one measured node's absolute canvas bounds. A large
+// block may extend past the pane edges; entry zoom remains exactly 100%.
+export function centeredNodeViewport({
+ pane,
+ bounds,
+}: {
+ pane: Size;
+ bounds: Rect;
+}): Viewport | null {
+ if (
+ pane.width <= 0 ||
+ pane.height <= 0 ||
+ bounds.width <= 0 ||
+ bounds.height <= 0
+ ) {
+ return null;
+ }
+ return {
+ x: pane.width / 2 - bounds.x - bounds.width / 2,
+ y: pane.height / 2 - bounds.y - bounds.height / 2,
+ zoom: 1,
+ };
+}
+
export const END_ANCHOR_BOTTOM_PX = 24;
/**
diff --git a/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigSidebar.test.tsx b/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigSidebar.test.tsx
index 9027a6861..63c142735 100644
--- a/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigSidebar.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/editor/panels/BlockConfigSidebar.test.tsx
@@ -81,7 +81,6 @@ import {
BLOCK_SIDEBAR_WIDTH_MAX,
useBlockSidebarWidthStore,
} from "@/store/BlockSidebarWidthStore";
-import { useStudioShellStore } from "@/store/StudioShellStore";
import { useWorkflowPanelStore } from "@/store/WorkflowPanelStore";
import { BLOCK_FORMS, type WorkflowBlockNodeType } from "./BlockConfigForm";
@@ -123,7 +122,6 @@ beforeEach(() => {
content: "parameters",
});
useBlockSidebarWidthStore.getState().reset();
- useStudioShellStore.getState().reset();
for (const key of BLOCK_FORM_KEYS) {
BLOCK_FORMS[key] = StubFormForBlockType(key);
}
diff --git a/skyvern-frontend/src/routes/workflows/studio/BrowserTab.test.tsx b/skyvern-frontend/src/routes/workflows/studio/BrowserTab.test.tsx
index 72d9cd784..491814e03 100644
--- a/skyvern-frontend/src/routes/workflows/studio/BrowserTab.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/BrowserTab.test.tsx
@@ -306,8 +306,8 @@ describe("BrowserTab view machine", () => {
expect(screen.getByTestId("location-search").textContent).toContain(
"view=recording",
);
- expect(screen.getByTestId("location-search").textContent).toContain(
- "panes=browser,overview",
+ expect(screen.getByTestId("location-search").textContent).not.toContain(
+ "panes=",
);
});
});
diff --git a/skyvern-frontend/src/routes/workflows/studio/PastRunsList.test.tsx b/skyvern-frontend/src/routes/workflows/studio/PastRunsList.test.tsx
index 3b4f98f72..81b3cbb5e 100644
--- a/skyvern-frontend/src/routes/workflows/studio/PastRunsList.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/PastRunsList.test.tsx
@@ -172,7 +172,7 @@ describe("PastRunsList", () => {
).toBe("true");
});
- test("clicking a different run switches the run and signals close", () => {
+ test("switches runs, drops the pane override, and signals close", () => {
mocks.runs = [makeRun({ workflow_run_id: "wr_2" })];
useRunViewStore.getState().pinFrame("act_5");
renderList("/agents/wpid_1?panes=browser&wr=wr_9&active=act_5&bl=blk");
@@ -181,7 +181,7 @@ describe("PastRunsList", () => {
const params = searchParams();
expect(params.get("wr")).toBe("wr_2");
- expect(params.get("panes")).toBe("browser");
+ expect(params.get("panes")).toBeNull();
expect(params.get("active")).toBeNull();
expect(params.get("bl")).toBeNull();
expect(useRunViewStore.getState().pinnedFrameId).toBeNull();
@@ -266,21 +266,21 @@ describe("PastRunsList", () => {
});
describe("searchWithRunSwitched", () => {
- test("sets ?wr=, clears ?active=, ?bl= and ?iteration=, preserves ?panes=", () => {
+ test("sets ?wr= and clears run selection and pane overrides", () => {
expect(
searchWithRunSwitched(
"?panes=browser&wr=wr_9&active=act_5&bl=blk&iteration=2",
"wr_2",
),
- ).toBe("?panes=browser&wr=wr_2");
+ ).toBe("?wr=wr_2");
});
- test("adds ?wr= when the url carries none, keeping other params", () => {
+ test("adds ?wr= and drops ?panes= when no run is selected", () => {
const params = new URLSearchParams(
searchWithRunSwitched("?panes=browser", "wr_7"),
);
expect(params.get("wr")).toBe("wr_7");
- expect(params.get("panes")).toBe("browser");
+ expect(params.get("panes")).toBeNull();
});
});
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioBrowserStream.test.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioBrowserStream.test.tsx
index 9db3ab70f..d6b78b7d0 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioBrowserStream.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioBrowserStream.test.tsx
@@ -9,6 +9,7 @@ import { useRecordingStore } from "@/store/useRecordingStore";
import { useStudioBrowserStore } from "@/store/useStudioBrowserStore";
import { StudioBrowserStream } from "./StudioBrowserStream";
+import { StudioPaneDefaultsProvider } from "./StudioPaneDefaults";
import { type StudioPaneId } from "./panes";
import { useStudioPanes } from "./useStudioPanes";
@@ -94,7 +95,7 @@ vi.mock("@/routes/browserSessions/BrowserSessionStream", () => ({
const initialBrowserState = useStudioBrowserStore.getState();
const initialRecordingState = useRecordingStore.getState();
-// Drives a real pane-state URL write, so the effect chain under test is the
+// Drives a real runtime pane change, so the effect chain under test is the
// same one a spine click goes through.
function OpenBrowserPaneButton() {
const { openPane } = useStudioPanes();
@@ -105,7 +106,7 @@ function OpenBrowserPaneButton() {
);
}
-// The browser pane's visibility comes from ?panes= in the URL.
+// The incoming URL seeds visit-scoped pane visibility.
function renderStudioBrowserStream(
initialPath: string,
visiblePanes?: readonly StudioPaneId[],
@@ -116,10 +117,10 @@ function renderStudioBrowserStream(
<Route
path="/workflows/:workflowPermanentId/studio"
element={
- <>
+ <StudioPaneDefaultsProvider hasBlocks={true}>
<StudioBrowserStream visiblePanes={visiblePanes} />
<OpenBrowserPaneButton />
- </>
+ </StudioPaneDefaultsProvider>
}
/>
</Routes>
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.test.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.test.tsx
index 50609ca90..b473de045 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.test.tsx
@@ -7,7 +7,6 @@ import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { useMountEffect } from "@/hooks/useMountEffect";
import { useStudioFirstRunStore } from "@/store/StudioFirstRunStore";
-import { useStudioShellStore } from "@/store/StudioShellStore";
import { shouldOpenCopilotPaneForHandoff } from "../discoverCopilotHandoff";
import { StudioPaneDefaultsProvider } from "./StudioPaneDefaults";
@@ -111,19 +110,18 @@ beforeEach(() => {
coachMarkSeen: false,
narrowNudgeSeen: false,
});
- useStudioShellStore.getState().reset();
toastMock.mockReset();
});
describe("cold-entry default panes (the four contexts)", () => {
- test("an empty agent starts on Editor + Browser", () => {
+ test("an empty agent starts on Copilot + Browser", () => {
renderStudio({ hasBlocks: false });
- expect(panesText()).toBe("editor,browser");
+ expect(panesText()).toBe("copilot,browser");
});
- test("a built agent also starts on Editor + Browser", () => {
+ test("a built agent starts on Copilot + Browser + Editor", () => {
renderStudio({ hasBlocks: true });
- expect(panesText()).toBe("editor,browser");
+ expect(panesText()).toBe("copilot,browser,editor");
});
test("a run in the URL lands on Browser + Overview", () => {
@@ -131,22 +129,24 @@ describe("cold-entry default panes (the four contexts)", () => {
expect(panesText()).toBe("browser,overview");
});
- test("a block-run deep link lands on Editor + Browser + Overview", () => {
+ test("a cold block-run deep link lands on Browser + Overview", () => {
renderStudio({ path: "/workflows/wpid_1/studio?wr=wr_1&bl=block_1" });
- expect(panesText()).toBe("editor,browser,overview");
+ expect(panesText()).toBe("browser,overview");
});
test("a blocks signal that changes after mount does not reshuffle the panes", () => {
const { rerender } = renderStudio({ hasBlocks: false });
- expect(panesText()).toBe("editor,browser");
+ expect(panesText()).toBe("copilot,browser");
rerender(
<MemoryRouter initialEntries={["/workflows/wpid_1/studio"]}>
- <StudioPaneDefaultsProvider hasBlocks={true}>
- <PanesProbe />
- </StudioPaneDefaultsProvider>
+ <StudioWorkflowDeletedContext.Provider value={null}>
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <PanesProbe />
+ </StudioPaneDefaultsProvider>
+ </StudioWorkflowDeletedContext.Provider>
</MemoryRouter>,
);
- expect(panesText()).toBe("editor,browser");
+ expect(panesText()).toBe("copilot,browser");
});
test("an explicit ?panes= is never overridden by the state default", () => {
@@ -159,10 +159,11 @@ describe("cold-entry default panes (the four contexts)", () => {
expect(panesText()).toBe("copilot,overview");
});
- test("toggling from the state default writes the default plus the change", () => {
+ test("toggling from the state default updates runtime panes without changing the URL", () => {
renderStudio({ hasBlocks: false });
fireEvent.click(screen.getByText("toggle-overview"));
- expect(panesText()).toBe("editor,browser,overview");
+ expect(panesText()).toBe("copilot,browser,overview");
+ expect(addressText()).toBe("/workflows/wpid_1/studio");
});
});
@@ -227,14 +228,14 @@ function messageText(): string {
}
describe("Discover → Studio handoff opens the Copilot pane", () => {
- test("a seeded handoff opens Copilot on top of the default editor+browser", () => {
+ test("a seeded handoff retains the default Copilot and Browser", () => {
renderHandoff();
- expect(panesText()).toBe("editor,browser,copilot");
+ expect(panesText()).toBe("copilot,browser");
});
test("no handoff prompt leaves the default panes untouched", () => {
renderHandoff({ hasInitialCopilotMessage: false });
- expect(panesText()).toBe("editor,browser");
+ expect(panesText()).toBe("copilot,browser");
});
test("an explicit ?panes=copilot handoff is left as-is (no duplicate open)", () => {
@@ -253,10 +254,11 @@ describe("Discover → Studio handoff opens the Copilot pane", () => {
{},
{
pathname: "/workflows/wpid_1/studio",
+ search: "?panes=browser",
state: { copilotMessage: "Fill out the contact form" },
},
);
- expect(panesText()).toBe("editor,browser,copilot");
+ expect(panesText()).toBe("browser,copilot");
expect(messageText()).toBe("Fill out the contact form");
});
@@ -265,10 +267,11 @@ describe("Discover → Studio handoff opens the Copilot pane", () => {
{ threadState: false },
{
pathname: "/workflows/wpid_1/studio",
+ search: "?panes=browser",
state: { copilotMessage: "Fill out the contact form" },
},
);
- expect(panesText()).toBe("editor,browser,copilot");
+ expect(panesText()).toBe("browser,copilot");
expect(messageText()).toBe("Fill out the contact form");
});
});
@@ -298,22 +301,16 @@ describe("narrow-viewport clamp of shared links", () => {
expect(panesText()).toBe("copilot,editor,overview");
});
- test("clamps the runtime-restored Copilot selection", () => {
- useStudioShellStore
- .getState()
- .setCopilotSelection("edit", { open: true, index: 0 });
+ test("live panes include Copilot from the clamped shared selection", () => {
renderStudio({
- path: "/workflows/wpid_1/studio?panes=editor,browser,copilot",
+ path: "/workflows/wpid_1/studio?panes=copilot,editor,browser",
stageWidth: 600,
});
expect(panesText()).toBe("copilot,editor");
- expect(livePanesText()).toBe("editor,browser");
+ expect(livePanesText()).toBe("copilot,editor");
});
test("clamps a deleted workflow without restoring blocked Copilot", () => {
- useStudioShellStore
- .getState()
- .setCopilotSelection("run", { open: true, index: 0 });
renderStudio({
path: "/workflows/wpid_1/studio?wr=wr_1",
stageWidth: 500,
@@ -358,9 +355,7 @@ describe("narrow-viewport clamp of shared links", () => {
fireEvent.click(screen.getByText("show-editor-only"));
expect(panesText()).toBe("editor");
- expect(addressText()).toBe(
- "/workflows/wpid_1/studio?panes=copilot,editor#proof",
- );
+ expect(addressText()).toBe(address);
});
});
@@ -404,132 +399,3 @@ describe("narrow-viewport nudge", () => {
expect(useStudioFirstRunStore.getState().coachMarkSeen).toBe(true);
});
});
-
-// GestureLearningProbe calls togglePane with and without the learn flag so
-// tests can verify which writes reach the store.
-function GestureLearningProbe() {
- const { panes, togglePane } = useStudioPanes();
- return (
- <div>
- <output data-testid="panes">{panes.join(",")}</output>
- <button onClick={() => togglePane("editor", { learn: true })}>
- gesture-toggle-editor
- </button>
- <button onClick={() => togglePane("overview")}>
- system-toggle-overview
- </button>
- </div>
- );
-}
-
-function renderGestureStudio({
- path = "/workflows/wpid_1/studio",
- hasBlocks = true,
-}: { path?: string; hasBlocks?: boolean } = {}) {
- return render(
- <MemoryRouter initialEntries={[path]}>
- <StudioPaneDefaultsProvider hasBlocks={hasBlocks}>
- <GestureLearningProbe />
- </StudioPaneDefaultsProvider>
- </MemoryRouter>,
- );
-}
-
-describe("gesture learning — edit-class writes reach the store", () => {
- test("a gesture toggle on an edit URL learns the resulting layout", () => {
- renderGestureStudio();
- fireEvent.click(screen.getByText("gesture-toggle-editor"));
- // default built-agent: editor,browser → toggle closes editor → browser
- expect(useStudioShellStore.getState().paneLayouts["edit"]).toEqual([
- "browser",
- ]);
- });
-
- test("a system (non-gesture) toggle does NOT update the store", () => {
- renderGestureStudio({
- path: "/workflows/wpid_1/studio?panes=copilot,browser,overview",
- });
- fireEvent.click(screen.getByText("system-toggle-overview"));
- expect(useStudioShellStore.getState().paneLayouts["edit"]).toBeUndefined();
- });
-
- test("a gesture on a run URL learns the run class", () => {
- renderGestureStudio({
- path: "/workflows/wpid_1/studio?wr=wr_1&panes=copilot,browser,overview",
- });
- fireEvent.click(screen.getByText("gesture-toggle-editor"));
- expect(useStudioShellStore.getState().paneLayouts["run"]).toBeDefined();
- expect(useStudioShellStore.getState().paneLayouts["edit"]).toBeUndefined();
- });
-
- test("a gesture on a block-iterate URL (wr+bl) does not learn any class", () => {
- renderGestureStudio({
- path: "/workflows/wpid_1/studio?wr=wr_1&bl=block_1&panes=copilot,browser,overview",
- });
- fireEvent.click(screen.getByText("gesture-toggle-editor"));
- expect(useStudioShellStore.getState().paneLayouts).toEqual({});
- });
-});
-
-describe("restore from learned edit layout", () => {
- test("a built agent restores the last learned edit layout", () => {
- // Pre-seed a layout distinct from the factory default so the restore is provable.
- useStudioShellStore.getState().setPaneLayout("edit", ["browser", "editor"]);
-
- renderStudio({ hasBlocks: true });
- expect(panesText()).toBe("browser,editor");
- });
-
- test("an empty agent ignores the learned layout and always shows factory default", () => {
- useStudioShellStore
- .getState()
- .setPaneLayout("edit", ["overview", "browser"]);
-
- renderStudio({ hasBlocks: false });
- expect(panesText()).toBe("editor,browser");
- });
-
- test("an explicit ?panes= beats the learned layout", () => {
- useStudioShellStore.getState().setPaneLayout("edit", ["editor", "browser"]);
-
- renderStudio({ path: "/workflows/wpid_1/studio?panes=overview" });
- expect(panesText()).toBe("overview");
- });
-
- test("a deep-link run URL beats the learned layout", () => {
- useStudioShellStore.getState().setPaneLayout("edit", ["editor", "browser"]);
-
- renderStudio({ path: "/workflows/wpid_1/studio?wr=wr_1" });
- expect(panesText()).toBe("browser,overview");
- });
-
- test("a learned layout with an unknown pane id is sanitized to known ids only", () => {
- // Inject a stale persisted layout that contains a no-longer-valid id.
- useStudioShellStore.setState({
- paneLayouts: {
- edit: ["editor", "bogus" as never, "browser"],
- },
- });
-
- renderStudio({ hasBlocks: true });
- expect(panesText()).toBe("editor,browser");
- });
-
- test("a learned layout that becomes all-unknown falls back to the factory default", () => {
- useStudioShellStore.setState({
- paneLayouts: { edit: ["bogus" as never] },
- });
-
- renderStudio({ hasBlocks: true });
- expect(panesText()).toBe("editor,browser");
- });
-
- test("a corrupted non-array learned layout falls back without throwing", () => {
- useStudioShellStore.setState({
- paneLayouts: { edit: "corrupt" as never },
- });
-
- renderStudio({ hasBlocks: true });
- expect(panesText()).toBe("editor,browser");
- });
-});
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.tsx
index 5229d3320..99494c472 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaults.tsx
@@ -1,57 +1,39 @@
-import { useCallback, useMemo, useRef, useState, type ReactNode } from "react";
+import {
+ useCallback,
+ useLayoutEffect,
+ useMemo,
+ useRef,
+ useState,
+ type ReactNode,
+} from "react";
import { useLocation } from "react-router-dom";
import { toast } from "@/components/ui/use-toast";
+import { useFirstParam } from "@/hooks/useFirstParam";
import { useStudioFirstRunStore } from "@/store/StudioFirstRunStore";
-import { useStudioShellStore } from "@/store/StudioShellStore";
+import { sanitizePaneWidth, type PaneWidths } from "@/store/paneWidths";
-import { liveSearch } from "./liveSearch";
import {
- copilotContextForSearch,
+ CREATE_STUDIO_PANES,
DEFAULT_STUDIO_PANES,
fitPanesToWidth,
panesFitWidth,
+ panesListEqual,
+ panesWithoutDeletedBlocked,
resolveOpenPanes,
searchWithRunReference,
- STUDIO_PANE_IDS,
type StudioPaneId,
- withCopilotSelection,
} from "./panes";
-import {
- StudioPaneDefaultsContext,
- type PaneClamp,
- type PaneWrite,
-} from "./StudioPaneDefaultsContext";
-import { useStudioRunId } from "./useStudioRunId";
+import { StudioPaneDefaultsContext } from "./StudioPaneDefaultsContext";
import { useStudioWorkflowDeletedAt } from "./StudioShellContext";
-// Drop unknown ids and duplicates; return null when the result is empty/invalid.
-function sanitizeLearnedPanes(
- raw: StudioPaneId[] | undefined,
-): readonly StudioPaneId[] | null {
- // Guard the localStorage boundary: corrupted/foreign values may not be arrays.
- if (!Array.isArray(raw) || raw.length === 0) return null;
- const seen = new Set<string>();
- const result: StudioPaneId[] = [];
- for (const id of raw) {
- if ((STUDIO_PANE_IDS as readonly string[]).includes(id) && !seen.has(id)) {
- seen.add(id);
- result.push(id);
- }
- }
- return result.length > 0 ? result : null;
-}
+type PaneState = {
+ key: string;
+ panes: StudioPaneId[];
+ paneWidths: PaneWidths;
+ entryId: number;
+};
-/**
- * First-visit pane policy for one studio mount. The state-aware default and
- * the narrow-viewport clamp are both latched exactly once (the shell remounts
- * per workflow via its key), so panes never reshuffle after first paint. The
- * blocks signal is synchronous — the shell only mounts with the workflow
- * loaded — so the latch decides from real data, never a placeholder.
- *
- * Built agents restore the user's last edit-class pane arrangement; empty
- * agents always start on the factory Editor + Browser default.
- */
export function StudioPaneDefaultsProvider({
hasBlocks,
children,
@@ -60,130 +42,184 @@ export function StudioPaneDefaultsProvider({
children: ReactNode;
}) {
const location = useLocation();
- const studioRunId = useStudioRunId();
+ const pathRunId = useFirstParam("workflowRunId", "runId");
const workflowDeleted = useStudioWorkflowDeletedAt() !== null;
+ const stageElRef = useRef<HTMLElement | null>(null);
+ const transitionRef = useRef<{
+ key: string;
+ panes?: readonly StudioPaneId[];
+ } | null>(null);
+ const locationKeyRef = useRef(location.key);
+ const measuredEntryRef = useRef<number | null>(null);
+ const wroteEntryRef = useRef<number | null>(null);
- // hasBlocks gates only the learned-layout lookup, not the factory default
- // itself — empty agents never restore a saved arrangement.
- const [defaultPanes] = useState<readonly StudioPaneId[]>(() => {
- if (!hasBlocks) {
- return [...DEFAULT_STUDIO_PANES];
- }
- const learnedEdit = sanitizeLearnedPanes(
- useStudioShellStore.getState().paneLayouts["edit"],
- );
- return learnedEdit ?? [...DEFAULT_STUDIO_PANES];
- });
-
- const [learnedRunPanes] = useState<readonly StudioPaneId[] | null>(() =>
- sanitizeLearnedPanes(useStudioShellStore.getState().paneLayouts.run),
- );
-
- const [initialUrlPanes] = useState<readonly StudioPaneId[]>(() =>
- resolveOpenPanes(
- searchWithRunReference(liveSearch(location.search), studioRunId),
- defaultPanes,
- learnedRunPanes,
- ),
+ const keyForSearch = useCallback(
+ (search: string) => {
+ const params = new URLSearchParams(
+ searchWithRunReference(search, pathRunId),
+ );
+ return JSON.stringify([
+ location.pathname,
+ params.get("wr") ?? (params.has("active") ? "active" : null),
+ params.get("panes"),
+ params.get("embed") === "true" ? ["embed", params.get("view")] : null,
+ ]);
+ },
+ [location.pathname, pathRunId],
);
+ const key = keyForSearch(location.search);
- const [initialPanes] = useState<readonly StudioPaneId[]>(() => {
- const search = searchWithRunReference(
- liveSearch(location.search),
- studioRunId,
+ const initialPanes = () => {
+ const resolved = resolveOpenPanes(
+ searchWithRunReference(location.search, pathRunId),
+ hasBlocks ? DEFAULT_STUDIO_PANES : CREATE_STUDIO_PANES,
);
- const resolved = initialUrlPanes;
- const selection =
- useStudioShellStore.getState().copilotSelectionByLayout[
- copilotContextForSearch(search)
- ];
- return workflowDeleted
- ? resolved
- : withCopilotSelection(resolved, selection);
- });
-
- const [clamp, setClamp] = useState<PaneClamp | null>(null);
- const stageElRef = useRef<HTMLElement | null>(null);
- const measuredRef = useRef(false);
- const wroteRef = useRef(false);
+ const panes = workflowDeleted
+ ? panesWithoutDeletedBlocked(resolved)
+ : resolved;
+ const width = stageElRef.current?.clientWidth ?? 0;
+ return width > 0 ? fitPanesToWidth(panes, width) : panes;
+ };
+ const [state, setState] = useState<PaneState>(() => ({
+ key,
+ panes: initialPanes(),
+ paneWidths: {},
+ entryId: 0,
+ }));
+ const latestRef = useRef(state);
+ let current = state;
+ if (state.key !== key) {
+ const transition = transitionRef.current;
+ current =
+ transition?.key === key
+ ? {
+ ...state,
+ key,
+ panes: transition.panes ? [...transition.panes] : state.panes,
+ }
+ : {
+ key,
+ panes: initialPanes(),
+ paneWidths: {},
+ entryId: state.entryId + 1,
+ };
+ setState(current);
+ }
+ latestRef.current = current;
+ // React can restart a navigation render before committing its state.
+ useLayoutEffect(() => {
+ if (locationKeyRef.current !== location.key) {
+ transitionRef.current = null;
+ locationKeyRef.current = location.key;
+ }
+ }, [location.key]);
- // Ref callbacks run before paint, so an over-wide shared link is clamped to
- // its fitting prefix without ever flashing the full list.
- const registerStageElement = useCallback(
- (el: HTMLElement | null) => {
- stageElRef.current = el;
- if (!el || measuredRef.current || wroteRef.current) {
- return;
+ const getPanes = useCallback(() => latestRef.current.panes, []);
+ const updatePanes = useCallback(
+ (compute: (panes: StudioPaneId[]) => StudioPaneId[]) => {
+ const previous = latestRef.current;
+ const computed = compute([...previous.panes]);
+ const panes = workflowDeleted
+ ? panesWithoutDeletedBlocked(computed)
+ : computed;
+ wroteEntryRef.current = previous.entryId;
+ if (!panesListEqual(previous.panes, panes)) {
+ latestRef.current = { ...previous, panes };
+ setState(latestRef.current);
}
- const width = el.clientWidth;
- if (width <= 0) {
- return;
- }
- measuredRef.current = true;
- const fitted = fitPanesToWidth(initialPanes, width);
- const urlFitted = fitPanesToWidth(initialUrlPanes, width);
+ const firstRun = useStudioFirstRunStore.getState();
+ if (!firstRun.coachMarkSeen) firstRun.markCoachMarkSeen();
+ const width = stageElRef.current?.clientWidth ?? 0;
if (
- fitted.length !== initialPanes.length ||
- urlFitted.length !== initialUrlPanes.length
+ width > 0 &&
+ panes.length > previous.panes.length &&
+ !panesFitWidth(panes, width) &&
+ !firstRun.narrowNudgeSeen
) {
- setClamp({
- source: initialPanes,
- presented: fitted,
- urlSource: initialUrlPanes,
- urlPresented: urlFitted,
+ firstRun.markNarrowNudgeSeen();
+ toast({
+ title: "Tight fit",
+ description:
+ "Panes keep a minimum width, so this view may feel cramped. Close a pane from the left rail any time.",
});
}
},
- [initialPanes, initialUrlPanes],
+ [workflowDeleted],
);
- const notePaneWrite = useCallback((change: PaneWrite) => {
- wroteRef.current = true;
- setClamp((current) => {
- if (current === null) {
- return current;
- }
- if (change.nextRuntimeSource !== undefined) {
- return {
- ...current,
- source: [...change.nextRuntimeSource],
- presented: [...change.next],
- };
- }
- return null;
- });
- const firstRun = useStudioFirstRunStore.getState();
- // Toggling a pane is the lesson the coach mark teaches.
- if (!firstRun.coachMarkSeen) {
- firstRun.markCoachMarkSeen();
- }
- const stageWidth = stageElRef.current?.clientWidth ?? 0;
+ const registerStageElement = useCallback((el: HTMLElement | null) => {
+ stageElRef.current = el;
+ const current = latestRef.current;
if (
- stageWidth > 0 &&
- change.next.length > change.previous.length &&
- !panesFitWidth(change.next, stageWidth) &&
- !firstRun.narrowNudgeSeen
- ) {
- firstRun.markNarrowNudgeSeen();
- toast({
- title: "Tight fit",
- description:
- "Panes keep a minimum width, so this view may feel cramped. Close a pane from the left rail any time.",
- });
+ !el ||
+ el.clientWidth <= 0 ||
+ measuredEntryRef.current === current.entryId ||
+ wroteEntryRef.current === current.entryId
+ )
+ return;
+ measuredEntryRef.current = current.entryId;
+ const panes = fitPanesToWidth(current.panes, el.clientWidth);
+ if (!panesListEqual(current.panes, panes)) {
+ latestRef.current = { ...current, panes };
+ setState(latestRef.current);
+ }
+ }, []);
+
+ const setPaneWidths = useCallback((widths: PaneWidths) => {
+ const paneWidths = { ...latestRef.current.paneWidths };
+ for (const [id, raw] of Object.entries(widths)) {
+ const width = sanitizePaneWidth(raw);
+ if (width !== undefined) paneWidths[id] = width;
}
+ latestRef.current = { ...latestRef.current, paneWidths };
+ setState(latestRef.current);
}, []);
+ const resetPaneWidths = useCallback(() => {
+ latestRef.current = { ...latestRef.current, paneWidths: {} };
+ setState(latestRef.current);
+ }, []);
+ const preserveNextEntry = useCallback(
+ (search: string | null, panes?: readonly StudioPaneId[]) => {
+ if (search === null) {
+ transitionRef.current = null;
+ return;
+ }
+ const key = keyForSearch(search);
+ if (key === latestRef.current.key) {
+ transitionRef.current = null;
+ if (panes) updatePanes(() => [...panes]);
+ } else {
+ transitionRef.current = { key, panes };
+ }
+ },
+ [keyForSearch, updatePanes],
+ );
const value = useMemo(
() => ({
- defaultPanes,
- clamp,
- notePaneWrite,
+ isStudio: true,
+ panes: current.panes,
+ paneWidths: current.paneWidths,
+ entryId: current.entryId,
+ getPanes,
+ updatePanes,
registerStageElement,
- learnedRunPanes,
+ setPaneWidths,
+ resetPaneWidths,
+ preserveNextEntry,
}),
- [defaultPanes, clamp, notePaneWrite, registerStageElement, learnedRunPanes],
+ [
+ current.panes,
+ current.paneWidths,
+ current.entryId,
+ getPanes,
+ updatePanes,
+ registerStageElement,
+ setPaneWidths,
+ resetPaneWidths,
+ preserveNextEntry,
+ ],
);
-
return (
<StudioPaneDefaultsContext.Provider value={value}>
{children}
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaultsContext.ts b/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaultsContext.ts
index 1ec2dba42..edd03b1e7 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaultsContext.ts
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioPaneDefaultsContext.ts
@@ -1,43 +1,38 @@
import { createContext, useContext } from "react";
+import { type PaneWidths } from "@/store/paneWidths";
import { DEFAULT_STUDIO_PANES, type StudioPaneId } from "./panes";
-export type PaneClamp = {
- // Runtime-selected panes and their fitting prefix.
- source: readonly StudioPaneId[];
- presented: readonly StudioPaneId[];
- // URL/default panes stay independent from runtime Copilot selection.
- urlSource: readonly StudioPaneId[];
- urlPresented: readonly StudioPaneId[];
-};
-
-export type PaneWrite = {
- previous: readonly StudioPaneId[];
- next: readonly StudioPaneId[];
- // A write that changes only Copilot updates the runtime clamp without
- // exposing panes that the mount-time viewport clamp hid.
- nextRuntimeSource?: readonly StudioPaneId[];
-};
-
export type StudioPaneDefaultsValue = {
- defaultPanes: readonly StudioPaneId[];
- clamp: PaneClamp | null;
- notePaneWrite: (change: PaneWrite) => void;
+ isStudio: boolean;
+ panes: readonly StudioPaneId[];
+ paneWidths: PaneWidths;
+ entryId: number;
+ getPanes: () => readonly StudioPaneId[];
+ updatePanes: (compute: (panes: StudioPaneId[]) => StudioPaneId[]) => void;
+ setPaneWidths: (widths: PaneWidths) => void;
+ resetPaneWidths: () => void;
+ preserveNextEntry: (
+ search: string | null,
+ panes?: readonly StudioPaneId[],
+ ) => void;
registerStageElement: (el: HTMLElement | null) => void;
- learnedRunPanes: readonly StudioPaneId[] | null;
};
const noop = () => undefined;
-// Module default keeps useStudioPanes safe outside the studio shell (legacy
-// Workspace, tests): legacy default panes, no clamping, no nudges.
export const StudioPaneDefaultsContext = createContext<StudioPaneDefaultsValue>(
{
- defaultPanes: DEFAULT_STUDIO_PANES,
- clamp: null,
- notePaneWrite: noop,
+ isStudio: false,
+ panes: DEFAULT_STUDIO_PANES,
+ paneWidths: {},
+ entryId: 0,
+ getPanes: () => DEFAULT_STUDIO_PANES,
+ updatePanes: noop,
+ setPaneWidths: noop,
+ resetPaneWidths: noop,
+ preserveNextEntry: noop,
registerStageElement: noop,
- learnedRunPanes: null,
},
);
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioPaneToggles.test.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioPaneToggles.test.tsx
index 160ce756b..967fd176f 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioPaneToggles.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioPaneToggles.test.tsx
@@ -20,9 +20,10 @@ import { Status } from "@/api/types";
import { TooltipProvider } from "@/components/ui/tooltip";
import { WorkflowPermanentIdContext } from "@/routes/workflows/WorkflowPermanentIdContext";
import { useStudioBrowserStore } from "@/store/useStudioBrowserStore";
-import { useStudioShellStore } from "@/store/StudioShellStore";
import { useStudioRunSignals } from "./useStudioRunSignals";
+import { StudioPaneDefaultsProvider } from "./StudioPaneDefaults";
+import { useStudioPanes } from "./useStudioPanes";
import { StudioPaneToggles } from "./StudioPaneToggles";
import { claimRunCompletionNotice } from "../workflowRun/runCompletionNotices";
@@ -92,8 +93,10 @@ const initialBrowserState = useStudioBrowserStore.getState();
function LocationProbe() {
const location = useLocation();
+ const { panes } = useStudioPanes();
return (
<>
+ <output data-testid="panes">{panes.join(",")}</output>
<output data-testid="search">{location.search}</output>
<output data-testid="route-state">
{JSON.stringify(location.state ?? null)}
@@ -124,7 +127,9 @@ function renderAt(path = "/workflows/wpid_abc/studio", state?: unknown) {
state === undefined ? path : { ...parsePath(path), state },
]}
>
- <ToggleHarness />
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <ToggleHarness />
+ </StudioPaneDefaultsProvider>
</MemoryRouter>,
);
}
@@ -133,7 +138,9 @@ function renderInBrowser(path: string, state: unknown) {
window.history.replaceState({ usr: state, key: "seed", idx: 0 }, "", path);
return render(
<BrowserRouter>
- <ToggleHarness />
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <ToggleHarness />
+ </StudioPaneDefaultsProvider>
</BrowserRouter>,
);
}
@@ -152,15 +159,18 @@ function runsTab(): HTMLButtonElement {
);
}
-function currentPanes(): string | null {
+function urlPanes(): string | null {
const search = screen.getByTestId("search").textContent ?? "";
return new URLSearchParams(search).get("panes");
}
+function currentPanes(): string {
+ return screen.getByTestId("panes").textContent ?? "";
+}
+
afterEach(cleanup);
beforeEach(() => {
localStorage.clear();
- useStudioShellStore.getState().reset();
window.history.replaceState(null, "", "/");
useStudioBrowserStore.setState(initialBrowserState, true);
runsQueryMock.mockReturnValue({ data: [] });
@@ -176,11 +186,11 @@ describe("StudioPaneToggles structure", () => {
}
});
- test("reflects the default panes (editor + browser) as expanded", () => {
+ test("reflects the built-agent default panes as expanded", () => {
renderAt();
expect(tab(/^Editor/).getAttribute("aria-expanded")).toBe("true");
expect(tab(/^Browser/).getAttribute("aria-expanded")).toBe("true");
- expect(tab(/^Copilot/).getAttribute("aria-expanded")).toBe("false");
+ expect(tab(/^Copilot/).getAttribute("aria-expanded")).toBe("true");
});
test("reflects an explicit ?panes= list", () => {
@@ -206,9 +216,9 @@ describe("StudioPaneToggles structure", () => {
expect(runsTab().getAttribute("aria-pressed")).toBe("false");
});
- test("a block-run deep link opens Editor, Browser and the run pane", () => {
+ test("a block-run deep link opens Browser and the run pane", () => {
renderAt("/workflows/wpid_abc/studio?wr=run_1&bl=block_1");
- expect(tab(/^Editor/).getAttribute("aria-expanded")).toBe("true");
+ expect(tab(/^Editor/).getAttribute("aria-expanded")).toBe("false");
expect(tab(/^Browser/).getAttribute("aria-expanded")).toBe("true");
expect(runsTab().getAttribute("aria-pressed")).toBe("true");
expect(tab(/^Copilot/).getAttribute("aria-expanded")).toBe("false");
@@ -217,23 +227,25 @@ describe("StudioPaneToggles structure", () => {
describe("StudioPaneToggles pane toggling", () => {
test("opening Copilot updates runtime state without creating a panes param", () => {
- renderAt();
+ renderAt("/workflows/wpid_abc/studio?wr=wr_1");
fireEvent.click(tab(/^Copilot/));
- expect(currentPanes()).toBeNull();
+ expect(urlPanes()).toBeNull();
expect(tab(/^Copilot/).getAttribute("aria-expanded")).toBe("true");
});
test("closing Copilot keeps the committed panes URL unchanged", () => {
renderAt("/workflows/wpid_abc/studio?panes=editor,copilot,browser");
fireEvent.click(tab(/^Copilot/));
- expect(currentPanes()).toBe("editor,copilot,browser");
+ expect(urlPanes()).toBe("editor,copilot,browser");
+ expect(currentPanes()).toBe("editor,browser");
expect(tab(/^Copilot/).getAttribute("aria-expanded")).toBe("false");
});
- test("closing the last pane leaves an explicit empty list", () => {
+ test("closing the last pane leaves an empty runtime list and preserves the shared URL", () => {
renderAt("/workflows/wpid_abc/studio?panes=editor");
fireEvent.click(tab(/^Editor/));
expect(currentPanes()).toBe("");
+ expect(urlPanes()).toBe("editor");
});
test("Copilot toggling preserves unrelated params without adding panes", () => {
@@ -329,6 +341,7 @@ describe("StudioPaneToggles run selector", () => {
fireEvent.click(tab("View Run: wr_current"));
expect(currentPanes()?.split(",")).toContain("overview");
+ expect(urlPanes()).toBe("copilot");
expect(screen.queryByText("Past runs")).toBeNull();
});
@@ -364,11 +377,9 @@ describe("StudioPaneToggles run selector", () => {
fireEvent.click(tab("Past Runs"));
fireEvent.click(await screen.findByText("wr_pick"));
- // Selecting opens the run pane (overview). The row's switchRun also sets
- // ?wr= (covered in PastRunsList.test); under MemoryRouter window.location
- // doesn't sync between the two navigations, so ?wr= can't be co-asserted
- // here, but the openPane merge is exercised.
- expect(currentPanes()?.split(",")).toContain("overview");
+ expect(currentPanes()).toBe("browser,overview");
+ expect(urlPanes()).toBeNull();
+ expect(screen.getByTestId("search").textContent).toBe("?wr=wr_pick");
await waitFor(() => expect(screen.queryByText("wr_pick")).toBeNull());
});
@@ -394,7 +405,8 @@ describe("StudioPaneToggles run selector", () => {
screen.getByTestId("search").textContent ?? "",
);
expect(params.get("wr")).toBe("wr_pick");
- expect(params.get("panes")?.split(",")).toContain("overview");
+ expect(params.has("panes")).toBe(false);
+ expect(currentPanes()).toBe("browser,overview");
expect(screen.getByTestId("route-state").textContent).toBe("null");
});
expect(window.history.state.usr).toBeNull();
@@ -417,6 +429,7 @@ describe("StudioPaneToggles run selector", () => {
fireEvent.click(await screen.findByText("wr_same"));
expect(currentPanes()?.split(",")).toContain("overview");
+ expect(urlPanes()).toBe("copilot");
});
});
@@ -506,6 +519,7 @@ describe("StudioPaneToggles browser activity", () => {
fireEvent.click(tab(/Browser/));
expect(useStudioBrowserStore.getState().hasUnseenActivity).toBe(false);
expect(currentPanes()).toBe("copilot,browser");
+ expect(urlPanes()).toBe("copilot");
});
});
@@ -579,7 +593,7 @@ describe("StudioPaneToggles keyboard navigation", () => {
test("arrow keys move focus without toggling panes", () => {
renderAt();
fireEvent.keyDown(tab(/^Copilot/), { key: "ArrowRight" });
- expect(currentPanes()).toBeNull();
+ expect(urlPanes()).toBeNull();
expect(tab(/^Editor/).getAttribute("aria-expanded")).toBe("true");
});
});
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioPaneToggles.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioPaneToggles.tsx
index 28f9b04b1..43ef6bcc7 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioPaneToggles.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioPaneToggles.tsx
@@ -101,7 +101,7 @@ export function StudioPaneToggles() {
// handler pushes ?wr= first; openPane then merges against the live URL, so
// this materializes the run pane (overview) without dropping the new ?wr=.
const onSelectRun = () => {
- openPane("overview", { learn: true });
+ openPane("overview");
setRunsSelectorOpen(false);
};
@@ -109,7 +109,7 @@ export function StudioPaneToggles() {
if (id === "browser" && !panes.includes("browser")) {
clearBrowserActivity();
}
- togglePane(id, { learn: true });
+ togglePane(id);
};
const paneBlockedByDeletion = (id: StudioPaneId) =>
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioShell.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioShell.tsx
index a0013f655..1e13a2d9d 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioShell.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioShell.tsx
@@ -27,7 +27,6 @@ import {
import { RecordingPanel } from "@/routes/workflows/editor/recording/RecordingPanel";
import { useRecordedBlocksStore } from "@/store/RecordedBlocksStore";
import { useRecordingStore } from "@/store/useRecordingStore";
-import { useStudioShellStore } from "@/store/StudioShellStore";
import { useWorkflowTitleStore } from "@/store/WorkflowTitleStore";
import { cn } from "@/util/utils";
@@ -746,7 +745,13 @@ function StudioStage(props: StudioWorkspaceProps) {
const embedded = searchParams.get("embed") === "true";
const { panes, closePane, openPane, setOpenPanes, setPanesOrder } =
useStudioPanes();
- const { registerStageElement } = useStudioPaneDefaults();
+ const {
+ registerStageElement,
+ paneWidths,
+ setPaneWidths,
+ resetPaneWidths,
+ entryId,
+ } = useStudioPaneDefaults();
const workflowPermanentId = useWorkflowPermanentId();
const runId = useStudioRunId();
const workflowDeleted = Boolean(props.workflow.deleted_at);
@@ -765,10 +770,6 @@ function StudioStage(props: StudioWorkspaceProps) {
enabled: false,
});
const browserSessionId = debugSession?.browser_session_id ?? null;
- const pipMinimized = useStudioShellStore((s) => s.pipMinimized);
- const paneWidths = useStudioShellStore((s) => s.paneWidths);
- const setPaneWidths = useStudioShellStore((s) => s.setPaneWidths);
- const resetPaneWidths = useStudioShellStore((s) => s.resetPaneWidths);
const [draggingPaneId, setDraggingPaneId] = useState<StudioPaneId | null>(
null,
);
@@ -835,6 +836,10 @@ function StudioStage(props: StudioWorkspaceProps) {
setPaneTransition(null);
}, []);
+ useEffect(() => {
+ restoreExpandedPane();
+ }, [entryId, restoreExpandedPane]);
+
useEffect(() => {
const previousPanes = previousPanesRef.current;
previousPanesRef.current = panes;
@@ -852,7 +857,7 @@ function StudioStage(props: StudioWorkspaceProps) {
? browserStreamSlot
: overviewOpen && runStreamSlot
? runStreamSlot
- : editorOpen && !pipMinimized
+ : editorOpen
? editorStreamSlot
: null;
const dest = activeSlot ?? streamHolderEl;
@@ -865,7 +870,6 @@ function StudioStage(props: StudioWorkspaceProps) {
browserOpen,
editorOpen,
overviewOpen,
- pipMinimized,
editorStreamSlot,
browserStreamSlot,
runStreamSlot,
@@ -889,7 +893,7 @@ function StudioStage(props: StudioWorkspaceProps) {
const closeWithFocus = (id: StudioPaneId) => {
setExpandedPaneId((current) => (current === id ? null : current));
setPaneTransition((current) => (current?.id === id ? null : current));
- closePane(id, { learn: true });
+ closePane(id);
document.getElementById(studioTabId(id))?.focus();
};
@@ -900,7 +904,7 @@ function StudioStage(props: StudioWorkspaceProps) {
if (panesListEqual(next, panes)) {
return;
}
- setPanesOrder(next, { learn: true });
+ setPanesOrder(next);
setReorderAnnouncement(
`${paneAccessibleName(movedId)} pane moved to position ${
next.indexOf(movedId) + 1
@@ -1016,7 +1020,11 @@ function StudioStage(props: StudioWorkspaceProps) {
only affects Radix tooltips inside the shell. */}
<TooltipProvider delayDuration={200} skipDelayDuration={300}>
<div className="flex h-full w-full flex-col">
- {embedded ? null : <StudioTopBar />}
+ {embedded ? null : (
+ <div className="shrink-0 overflow-x-auto">
+ <StudioTopBar />
+ </div>
+ )}
<div className="flex min-h-0 min-w-0 flex-1">
{/* Panes keep a fixed DOM order (stable mounts for the canvas, chat and
stream slots); the CSS order carries the layout order instead, so
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioStageLauncher.test.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioStageLauncher.test.tsx
index edbf15928..05f067ee0 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioStageLauncher.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioStageLauncher.test.tsx
@@ -7,6 +7,8 @@ import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { TooltipProvider } from "@/components/ui/tooltip";
import { useStudioBrowserStore } from "@/store/useStudioBrowserStore";
+import { StudioPaneDefaultsProvider } from "./StudioPaneDefaults";
+import { useStudioPanes } from "./useStudioPanes";
import { StudioStageLauncher } from "./StudioStageLauncher";
vi.mock("../hooks/useWorkflowRunsQuery", () => ({
@@ -36,23 +38,31 @@ const initialBrowserState = useStudioBrowserStore.getState();
function LocationProbe() {
const location = useLocation();
- return <output data-testid="search">{location.search}</output>;
+ const { panes } = useStudioPanes();
+ return (
+ <>
+ <output data-testid="search">{location.search}</output>
+ <output data-testid="panes">{panes.join(",")}</output>
+ </>
+ );
}
function renderAt(path = "/workflows/wpid_1/studio?panes=") {
return render(
<TooltipProvider delayDuration={0}>
<MemoryRouter initialEntries={[path]}>
- <StudioStageLauncher />
- <LocationProbe />
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <StudioStageLauncher />
+ <LocationProbe />
+ </StudioPaneDefaultsProvider>
</MemoryRouter>
</TooltipProvider>,
);
}
function currentPanes(): string | null {
- const search = screen.getByTestId("search").textContent ?? "";
- return new URLSearchParams(search).get("panes");
+ expect(screen.getByTestId("search").textContent).toBe("?panes=");
+ return screen.getByTestId("panes").textContent;
}
afterEach(cleanup);
diff --git a/skyvern-frontend/src/routes/workflows/studio/StudioStageLauncher.tsx b/skyvern-frontend/src/routes/workflows/studio/StudioStageLauncher.tsx
index 976eb1639..8f0340482 100644
--- a/skyvern-frontend/src/routes/workflows/studio/StudioStageLauncher.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/StudioStageLauncher.tsx
@@ -36,11 +36,11 @@ export function StudioStageLauncher() {
if (id === "browser") {
clearBrowserActivity();
}
- openPane(id, { learn: true });
+ openPane(id);
};
const onSelectRun = () => {
- openPane("overview", { learn: true });
+ openPane("overview");
setRunsSelectorOpen(false);
};
diff --git a/skyvern-frontend/src/routes/workflows/studio/paneLayout.test.ts b/skyvern-frontend/src/routes/workflows/studio/paneLayout.test.ts
index c534fca03..e19345b0c 100644
--- a/skyvern-frontend/src/routes/workflows/studio/paneLayout.test.ts
+++ b/skyvern-frontend/src/routes/workflows/studio/paneLayout.test.ts
@@ -9,7 +9,6 @@ import {
paneResizable,
paneWidthsKey,
STUDIO_PANE_DEFAULT_WIDTH,
- STUDIO_TWO_PANE_SIDE_BASIS,
} from "./paneLayout";
import { STUDIO_PANE_MIN_WIDTH, type StudioPaneId } from "./panes";
@@ -48,15 +47,32 @@ describe("paneResizable", () => {
describe("paneFlex", () => {
const noWidths = {};
- test("browser takes all remaining space; others sit at the default width", () => {
+ test("built-agent panes use 25/45/30 of usable width", () => {
const panes: StudioPaneId[] = ["copilot", "browser", "editor"];
expect(paneFlex("browser", panes, noWidths)).toBe("1 1 0%");
- expect(paneFlex("copilot", panes, noWidths)).toBe(
- `0 1 ${STUDIO_PANE_DEFAULT_WIDTH}px`,
- );
- expect(paneFlex("editor", panes, noWidths)).toBe(
- `0 1 ${STUDIO_PANE_DEFAULT_WIDTH}px`,
+ expect(paneFlex("copilot", panes, noWidths)).toBe("0 1 calc(25% - 6px)");
+ expect(paneFlex("editor", panes, noWidths)).toBe("0 1 calc(30% - 7.2px)");
+ });
+
+ test("blank-agent panes use 30/70 even after reordering", () => {
+ for (const panes of [
+ ["copilot", "browser"],
+ ["browser", "copilot"],
+ ] as StudioPaneId[][]) {
+ expect(paneFlex("copilot", panes, noWidths)).toBe(
+ "0 1 calc(30% - 3.6px)",
+ );
+ expect(paneFlex("browser", panes, noWidths)).toBe("1 1 0%");
+ }
+ });
+
+ test("reordered built-agent panes keep ratios and pinned widths win", () => {
+ const panes: StudioPaneId[] = ["editor", "browser", "copilot"];
+ expect(paneFlex("editor", panes, {})).toBe("0 1 calc(30% - 7.2px)");
+ expect(paneFlex("copilot", panes, { editor: 480 })).toBe(
+ "0 1 calc(25% - 6px)",
);
+ expect(paneFlex("editor", panes, { editor: 480 })).toBe("0 1 480px");
});
test("editor becomes the greedy pane when the browser is closed", () => {
@@ -95,11 +111,11 @@ describe("paneFlex", () => {
test("a two-pane default gives the side pane ~35% and the browser the rest", () => {
expect(paneFlex("editor", ["editor", "browser"], noWidths)).toBe(
- `0 1 ${STUDIO_TWO_PANE_SIDE_BASIS}`,
+ "0 1 calc(35% - 4.2px)",
);
expect(paneFlex("browser", ["editor", "browser"], noWidths)).toBe("1 1 0%");
expect(paneFlex("overview", ["browser", "overview"], noWidths)).toBe(
- `0 1 ${STUDIO_TWO_PANE_SIDE_BASIS}`,
+ "0 1 calc(35% - 4.2px)",
);
expect(paneFlex("browser", ["browser", "overview"], noWidths)).toBe(
"1 1 0%",
@@ -109,7 +125,7 @@ describe("paneFlex", () => {
test("the ~35% side split holds for any greedy two-pane row, not just the factory combos", () => {
expect(paneFlex("editor", ["copilot", "editor"], noWidths)).toBe("1 1 0%");
expect(paneFlex("copilot", ["copilot", "editor"], noWidths)).toBe(
- `0 1 ${STUDIO_TWO_PANE_SIDE_BASIS}`,
+ "0 1 calc(35% - 4.2px)",
);
});
@@ -129,13 +145,13 @@ describe("paneFlex", () => {
);
});
- test("garbage persisted widths fall back to the default", () => {
+ test("invalid widths fall back to the default", () => {
const panes: StudioPaneId[] = ["copilot", "browser", "editor"];
expect(paneFlex("copilot", panes, { copilot: Number.NaN })).toBe(
- `0 1 ${STUDIO_PANE_DEFAULT_WIDTH}px`,
+ "0 1 calc(25% - 6px)",
);
expect(paneFlex("copilot", panes, { copilot: -50 })).toBe(
- `0 1 ${STUDIO_PANE_DEFAULT_WIDTH}px`,
+ "0 1 calc(25% - 6px)",
);
});
});
diff --git a/skyvern-frontend/src/routes/workflows/studio/paneLayout.ts b/skyvern-frontend/src/routes/workflows/studio/paneLayout.ts
index 902f06e9b..5bcc3f091 100644
--- a/skyvern-frontend/src/routes/workflows/studio/paneLayout.ts
+++ b/skyvern-frontend/src/routes/workflows/studio/paneLayout.ts
@@ -2,7 +2,11 @@ import { arrayMove } from "@dnd-kit/sortable";
import { sanitizePaneWidth, type PaneWidths } from "@/store/paneWidths";
-import { STUDIO_PANE_MIN_WIDTH, type StudioPaneId } from "./panes";
+import {
+ STUDIO_PANE_MIN_WIDTH,
+ STUDIO_STAGE_GAP_PX,
+ type StudioPaneId,
+} from "./panes";
// Re-exported for the shell: pinned widths ride along with the layout math.
export type { PaneWidths };
@@ -11,11 +15,6 @@ export type { PaneWidths };
// rest of the row.
export const STUDIO_PANE_DEFAULT_WIDTH = 300;
-// The unpinned side pane of ANY two-pane row with a greedy pane opens at this
-// share, so the greedy pane keeps ~65% — mirrors the legacy split on the
-// factory defaults (editor+browser, browser+overview).
-export const STUDIO_TWO_PANE_SIDE_BASIS = "35%";
-
export type PaneBounds = Pick<DOMRect, "left" | "top" | "width" | "height">;
export function paneExpansionKeyframes(
@@ -82,11 +81,24 @@ export function paneFlex(
// Without a greedy pane, unpinned panes share the free space equally.
return `1 1 ${STUDIO_PANE_DEFAULT_WIDTH}px`;
}
- // Any two-pane row gives the unpinned side pane ~35% (the greedy pane keeps
- // ~65%); 3+ panes keep the fixed default so the greedy pane isn't starved.
- return panes.length === 2
- ? `0 1 ${STUDIO_TWO_PANE_SIDE_BASIS}`
- : `0 1 ${STUDIO_PANE_DEFAULT_WIDTH}px`;
+ // Percentages apply to the content box minus the fixed-width dividers.
+ // Browser remains greedy so pinned widths and minimums take precedence.
+ let share: number;
+ if (panes.length === 2) {
+ share = id === "copilot" && greedy === "browser" ? 30 : 35;
+ } else if (
+ panes.length === 3 &&
+ panes.includes("copilot") &&
+ panes.includes("browser") &&
+ panes.includes("editor")
+ ) {
+ share = id === "copilot" ? 25 : 30;
+ } else {
+ return `0 1 ${STUDIO_PANE_DEFAULT_WIDTH}px`;
+ }
+ const separatorShare =
+ (STUDIO_STAGE_GAP_PX * (panes.length - 1) * share) / 100;
+ return `0 1 calc(${share}% - ${separatorShare}px)`;
}
// Clamp a divider drag so neither neighbor goes under its min width. The
diff --git a/skyvern-frontend/src/routes/workflows/studio/panes.test.ts b/skyvern-frontend/src/routes/workflows/studio/panes.test.ts
index 2236dab3b..08d22eea4 100644
--- a/skyvern-frontend/src/routes/workflows/studio/panes.test.ts
+++ b/skyvern-frontend/src/routes/workflows/studio/panes.test.ts
@@ -7,51 +7,22 @@ import {
STUDIO_STAGE_PADDING_PX,
STUDIO_PANE_MIN_WIDTH,
WORKFLOW_AUTHORING_PANES,
- copilotContextForSearch,
fitPanesToWidth,
isAuthoringLayout,
- layoutClassForSearch,
panesFitWidth,
panesFromDeepLink,
panesListEqual,
panesWithoutDeletedBlocked,
parsePanesParam,
resolveOpenPanes,
- searchWithPanes,
searchWithRunReference,
toReadableSearch,
togglePane,
- withCopilotSelection,
withPaneClosed,
withPaneOpen,
withPanesOpen,
} from "./panes";
-describe("Copilot pane context and selection", () => {
- test("uses one edit context and one run context", () => {
- expect(copilotContextForSearch("?panes=copilot")).toBe("edit");
- expect(copilotContextForSearch("?bl=block_1")).toBe("edit");
- expect(copilotContextForSearch("?wr=wr_1")).toBe("run");
- expect(copilotContextForSearch("?active=act_1")).toBe("run");
- expect(copilotContextForSearch("?wr=wr_1&bl=block_1")).toBe("run");
- });
-
- test("applies runtime membership and preserves a remembered position", () => {
- expect(
- withCopilotSelection(["copilot", "editor", "browser"], {
- open: false,
- index: 0,
- }),
- ).toEqual(["editor", "browser"]);
- expect(
- withCopilotSelection(["editor", "browser"], {
- open: true,
- index: 1,
- }),
- ).toEqual(["editor", "copilot", "browser"]);
- });
-});
-
describe("parsePanesParam", () => {
test("returns null when the param is absent", () => {
expect(parsePanesParam(null)).toBeNull();
@@ -117,14 +88,14 @@ describe("panesFromDeepLink", () => {
).toEqual(["browser", "overview"]);
});
- test("a block run opens iterate: Editor, Browser, Overview", () => {
+ test("a cold block run opens Browser and Overview", () => {
expect(
panesFromDeepLink({
runId: "wr_123",
active: null,
blockLabel: "block_1",
}),
- ).toEqual(["editor", "browser", "overview"]);
+ ).toEqual(["browser", "overview"]);
});
test("a block label without a run does not force the run layout", () => {
@@ -136,73 +107,21 @@ describe("panesFromDeepLink", () => {
test("no deep link falls back to the default panes", () => {
expect(
panesFromDeepLink({ runId: null, active: null, blockLabel: null }),
- ).toEqual(["editor", "browser"]);
- });
-
- test("a learned run layout restores on a bare ?wr= entry", () => {
- const learned = ["copilot", "browser", "editor", "overview"] as const;
- expect(
- panesFromDeepLink(
- { runId: "wr_123", active: null, blockLabel: null },
- DEFAULT_STUDIO_PANES,
- learned,
- ),
- ).toEqual(["copilot", "browser", "editor", "overview"]);
- });
-
- test("learned run layout including editor is restored as-is", () => {
- expect(
- panesFromDeepLink(
- { runId: "wr_123", active: null, blockLabel: null },
- DEFAULT_STUDIO_PANES,
- ["editor", "overview"],
- ),
- ).toEqual(["editor", "overview"]);
- });
-
- test("block-iterate (?wr= + ?bl=) ignores the learned run layout", () => {
- expect(
- panesFromDeepLink(
- { runId: "wr_123", active: null, blockLabel: "block_1" },
- DEFAULT_STUDIO_PANES,
- ["copilot", "editor"],
- ),
- ).toEqual(["editor", "browser", "overview"]);
- });
-
- test("?active= alone ignores the learned run layout", () => {
- expect(
- panesFromDeepLink(
- { runId: null, active: "act_1", blockLabel: null },
- DEFAULT_STUDIO_PANES,
- ["editor", "overview"],
- ),
- ).toEqual(["browser", "overview"]);
- });
-
- test("null learned layout falls back to factory run panes", () => {
- expect(
- panesFromDeepLink(
- { runId: "wr_123", active: null, blockLabel: null },
- DEFAULT_STUDIO_PANES,
- null,
- ),
- ).toEqual(["browser", "overview"]);
+ ).toEqual(["copilot", "browser", "editor"]);
});
});
describe("resolveOpenPanes", () => {
test("no params resolves to the default panes", () => {
- expect(resolveOpenPanes("")).toEqual(["editor", "browser"]);
+ expect(resolveOpenPanes("")).toEqual(["copilot", "browser", "editor"]);
});
test("?wr= resolves to Browser and Overview", () => {
expect(resolveOpenPanes("?wr=wr_123")).toEqual(["browser", "overview"]);
});
- test("?wr= plus ?bl= resolves to Editor, Browser and Overview", () => {
+ test("?wr= plus ?bl= resolves to Browser and Overview", () => {
expect(resolveOpenPanes("?wr=wr_123&bl=block_1")).toEqual([
- "editor",
"browser",
"overview",
]);
@@ -231,39 +150,12 @@ describe("resolveOpenPanes", () => {
});
test("unrelated params do not affect the default", () => {
- expect(resolveOpenPanes("?embed=true&cache-key-value=x")).toEqual([
- "editor",
+ expect(resolveOpenPanes("?cache-key-value=x")).toEqual([
+ "copilot",
"browser",
+ "editor",
]);
});
-
- test("?wr= with a learned run layout restores it", () => {
- expect(
- resolveOpenPanes("?wr=wr_123", DEFAULT_STUDIO_PANES, [
- "browser",
- "overview",
- "editor",
- ]),
- ).toEqual(["browser", "overview", "editor"]);
- });
-
- test("explicit ?panes= wins over the learned run layout", () => {
- expect(
- resolveOpenPanes("?wr=wr_123&panes=copilot", DEFAULT_STUDIO_PANES, [
- "browser",
- "overview",
- ]),
- ).toEqual(["copilot"]);
- });
-
- test("?wr= + ?bl= ignores the learned run layout", () => {
- expect(
- resolveOpenPanes("?wr=wr_123&bl=block_1", DEFAULT_STUDIO_PANES, [
- "copilot",
- "editor",
- ]),
- ).toEqual(["editor", "browser", "overview"]);
- });
});
describe("resolveOpenPanes with custom defaults", () => {
@@ -281,7 +173,7 @@ describe("resolveOpenPanes with custom defaults", () => {
]);
expect(
resolveOpenPanes("?wr=wr_123&bl=block_1", ["copilot", "editor"]),
- ).toEqual(["editor", "browser", "overview"]);
+ ).toEqual(["browser", "overview"]);
});
test("an explicit ?panes= is never overridden by custom defaults", () => {
@@ -440,43 +332,6 @@ describe("block-run starts append the run surfaces (continuity rule)", () => {
});
});
-describe("searchWithPanes", () => {
- test("adds ?panes= to an empty search, with readable commas", () => {
- expect(searchWithPanes("", ["copilot", "browser"])).toBe(
- "?panes=copilot,browser",
- );
- });
-
- test("preserves unrelated params and replaces an existing ?panes=", () => {
- const next = searchWithPanes("?wr=wr_1&panes=timeline", [
- "overview",
- "editor",
- ]);
- const params = new URLSearchParams(next);
- expect(params.get("wr")).toBe("wr_1");
- expect(params.get("panes")).toBe("overview,editor");
- });
-
- test("serializes an empty list as an explicit empty value", () => {
- const params = new URLSearchParams(searchWithPanes("?wr=wr_1", []));
- expect(params.get("panes")).toBe("");
- expect(params.get("wr")).toBe("wr_1");
- });
-
- test("round-trips through resolveOpenPanes", () => {
- const next = searchWithPanes("?wr=wr_1", ["browser", "copilot"]);
- expect(resolveOpenPanes(next)).toEqual(["browser", "copilot"]);
- });
-
- test("an aliased ?panes=run re-serializes canonically after a write", () => {
- const parsed = parsePanesParam("run");
- expect(parsed).toEqual(["overview"]);
- expect(
- searchWithPanes("?panes=run", withPaneOpen(parsed!, "copilot")),
- ).toBe("?panes=overview,copilot");
- });
-});
-
describe("panesWithoutDeletedBlocked", () => {
test("drops the workflow-mutating panes and keeps run-view order", () => {
expect(
@@ -510,58 +365,15 @@ describe("isAuthoringLayout", () => {
});
});
-describe("layoutClassForSearch", () => {
- test("no params → edit class", () => {
- expect(layoutClassForSearch("")).toBe("edit");
- });
-
- test("?panes= only → edit class (URL params other than wr/bl don't matter)", () => {
- expect(layoutClassForSearch("?panes=copilot,browser")).toBe("edit");
- });
-
- test("?wr= without ?bl= → run class", () => {
- expect(layoutClassForSearch("?wr=wr_123")).toBe("run");
- });
-
- test("?wr= with ?bl= → null (block-iterate, never learned)", () => {
- expect(layoutClassForSearch("?wr=wr_123&bl=block_1")).toBeNull();
- });
-
- test("?bl= alone (no wr) → null", () => {
- expect(layoutClassForSearch("?bl=block_1")).toBeNull();
- });
-
- test("?wr= with unrelated params → run class", () => {
- expect(layoutClassForSearch("?wr=wr_123&panes=copilot")).toBe("run");
- });
-
- test("?active= without ?wr= → run class (same run test as panesFromDeepLink)", () => {
- expect(layoutClassForSearch("?active=act_1")).toBe("run");
- });
-
- test("?active= with ?bl= → null", () => {
- expect(layoutClassForSearch("?active=act_1&bl=block_1")).toBeNull();
- });
-});
-
describe("system run focus (?wrs=)", () => {
- test("keeps the edit layout class while the run stays inspectable", () => {
- expect(copilotContextForSearch("?wr=wr_1&wrs=copilot")).toBe("edit");
- expect(layoutClassForSearch("?wr=wr_1&wrs=copilot")).toBe("edit");
- });
-
test("does not remap deep-link panes to the run layout", () => {
- const learnedRun = ["overview", "browser"] as const;
expect(
- resolveOpenPanes(
- "?wr=wr_1&wrs=copilot",
- ["editor", "browser"],
- [...learnedRun],
- ),
+ resolveOpenPanes("?wr=wr_1&wrs=copilot", ["editor", "browser"]),
).toEqual(["editor", "browser"]);
- expect(
- resolveOpenPanes("?wr=wr_1", ["editor", "browser"], [...learnedRun]),
- ).toEqual([...learnedRun]);
+ expect(resolveOpenPanes("?wr=wr_1", ["editor", "browser"])).toEqual([
+ "browser",
+ "overview",
+ ]);
});
});
@@ -571,18 +383,12 @@ describe("searchWithRunReference", () => {
expect(resolveOpenPanes(searchWithRunReference("", "wr_123"))).toEqual(
RUN_APPEND_PANES,
);
- expect(layoutClassForSearch(searchWithRunReference("", "wr_123"))).toBe(
- "run",
- );
});
test("drops a stale marker so a path run keeps its run class", () => {
// The marker belongs to a copilot focus that is gone; leaving it would pin
// a run the user opened to the edit layout.
expect(searchWithRunReference("?wrs=copilot", "wr_123")).toBe("?wr=wr_123");
- expect(
- layoutClassForSearch(searchWithRunReference("?wrs=copilot", "wr_123")),
- ).toBe("run");
expect(
resolveOpenPanes(searchWithRunReference("?wrs=copilot", "wr_123")),
).toEqual(RUN_APPEND_PANES);
diff --git a/skyvern-frontend/src/routes/workflows/studio/panes.ts b/skyvern-frontend/src/routes/workflows/studio/panes.ts
index 1abae8a18..61c8921bd 100644
--- a/skyvern-frontend/src/routes/workflows/studio/panes.ts
+++ b/skyvern-frontend/src/routes/workflows/studio/panes.ts
@@ -1,38 +1,7 @@
export type StudioPaneId = "copilot" | "editor" | "browser" | "overview";
-// "edit" = no run in URL; "run" = run present, no block label; null = block-iterate (never learned).
-export type StudioLayoutClass = "edit" | "run";
-export type CopilotPaneSelection = {
- open: boolean;
- index: number | undefined;
-};
-
-// Marks ?wr= as a system-transient focus (the copilot following its own test
-// run) rather than a user opening a run. Layout classification ignores such a
-// run reference: the user never navigated, so their arrangement must not remap.
export const SYSTEM_RUN_FOCUS_PARAM = "wrs";
-// Copilot has one selection for live editing and one for inspected runs.
-// Block-run labels do not create a third context.
-export function copilotContextForSearch(search: string): StudioLayoutClass {
- const params = new URLSearchParams(search);
- if (params.get(SYSTEM_RUN_FOCUS_PARAM) !== null) return "edit";
- return params.get("wr") !== null || params.get("active") !== null
- ? "run"
- : "edit";
-}
-
-export function layoutClassForSearch(search: string): StudioLayoutClass | null {
- const params = new URLSearchParams(search);
- if (params.get("bl") !== null) return null;
- if (params.get(SYSTEM_RUN_FOCUS_PARAM) !== null) return "edit";
- // Same run test as panesFromDeepLink: ?active= is a run reference too.
- if (params.get("wr") !== null || params.get("active") !== null) {
- return "run";
- }
- return "edit";
-}
-
export const STUDIO_PANE_IDS: readonly StudioPaneId[] = [
"copilot",
"editor",
@@ -49,11 +18,14 @@ const STUDIO_PANE_ID_ALIASES: Record<string, StudioPaneId> = {
timeline: "overview",
};
-// Cold-entry edit-class default when no deep link or saved layout decides:
-// Editor on the left, Browser on the right — the legacy studio edit view.
+export const CREATE_STUDIO_PANES: readonly StudioPaneId[] = [
+ "copilot",
+ "browser",
+];
export const DEFAULT_STUDIO_PANES: readonly StudioPaneId[] = [
- "editor",
+ "copilot",
"browser",
+ "editor",
];
// The run surfaces: cold-entry run-class views open exactly these, and in-app
@@ -160,9 +132,6 @@ export function parsePanesParam(raw: string | null): StudioPaneId[] | null {
return result;
}
-// Deep-link → panes mapping when ?panes= is absent: a block-run link lands on
-// iterate (Editor leads); a bare run reference restores the learned run layout
-// (or falls back to watch-and-review); ?active= alone always opens watch-and-review.
export function panesFromDeepLink(
params: {
runId: string | null;
@@ -171,47 +140,46 @@ export function panesFromDeepLink(
systemFocus?: boolean;
},
defaultPanes: readonly StudioPaneId[] = DEFAULT_STUDIO_PANES,
- learnedRunPanes?: readonly StudioPaneId[] | null,
): StudioPaneId[] {
- if (params.runId && params.blockLabel) {
- return ["editor", "browser", "overview"];
- }
- if (params.runId && params.systemFocus) {
- return [...defaultPanes];
- }
- if (params.runId) {
- return learnedRunPanes ? [...learnedRunPanes] : [...RUN_APPEND_PANES];
- }
- if (params.active) {
- // Unlike a bare ?wr=, ?active= does not restore the learned run layout — an
- // action reference always opens the default watch-and-review surfaces.
- return [...RUN_APPEND_PANES];
- }
- return [...defaultPanes];
+ return (params.runId || params.active) && !params.systemFocus
+ ? [...RUN_APPEND_PANES]
+ : [...defaultPanes];
}
-// Open panes for a studio search string. An explicit ?panes= wins over the
-// legacy deep-link params (?wr= / ?active= / ?bl=); both win over defaultPanes.
export function resolveOpenPanes(
search: string,
defaultPanes: readonly StudioPaneId[] = DEFAULT_STUDIO_PANES,
- learnedRunPanes?: readonly StudioPaneId[] | null,
): StudioPaneId[] {
const params = new URLSearchParams(search);
const explicit = parsePanesParam(params.get(STUDIO_PANES_PARAM));
- if (explicit !== null) {
- return explicit;
+ let panes =
+ explicit ??
+ panesFromDeepLink(
+ {
+ runId: params.get("wr"),
+ active: params.get("active"),
+ blockLabel: params.get("bl"),
+ systemFocus: params.has(SYSTEM_RUN_FOCUS_PARAM),
+ },
+ defaultPanes,
+ );
+ const view = params.get("view");
+ const requiredPane = view === "recording" ? "browser" : "overview";
+ const hasContentView =
+ view !== null &&
+ ["recording", "timeline", "outputs", "inputs", "code"].includes(view);
+ if (params.get("embed") === "true") {
+ if (hasContentView) return [requiredPane];
+ panes =
+ explicit?.length &&
+ explicit.every((id) => id === "browser" || id === "overview")
+ ? explicit
+ : [requiredPane];
}
- return panesFromDeepLink(
- {
- runId: params.get("wr"),
- active: params.get("active"),
- blockLabel: params.get("bl"),
- systemFocus: params.get(SYSTEM_RUN_FOCUS_PARAM) !== null,
- },
- defaultPanes,
- learnedRunPanes,
- );
+ if (hasContentView) {
+ panes = [requiredPane, ...panes.filter((id) => id !== requiredPane)];
+ }
+ return panes;
}
// Open panes append, close panes splice: list order is the layout order.
@@ -245,24 +213,6 @@ export function withPaneClosed(
): StudioPaneId[] {
return panes.filter((p) => p !== id);
}
-export function withCopilotSelection(
- panes: readonly StudioPaneId[],
- selection: CopilotPaneSelection | undefined,
-): StudioPaneId[] {
- if (selection === undefined) {
- return [...panes];
- }
- if (!selection.open) {
- return withPaneClosed(panes, "copilot");
- }
- const currentIndex = panes.indexOf("copilot");
- const targetIndex =
- selection.index ?? (currentIndex === -1 ? panes.length : currentIndex);
- const next = withPaneClosed(panes, "copilot");
- next.splice(Math.min(Math.max(targetIndex, 0), next.length), 0, "copilot");
- return next;
-}
-
// Commas are legal unencoded in query values and parse back identically; keep
// ?panes=copilot,browser readable no matter which writer serialized last.
export function toReadableSearch(params: URLSearchParams): string {
@@ -291,13 +241,3 @@ export function searchWithRunReference(
params.delete(SYSTEM_RUN_FOCUS_PARAM);
return toReadableSearch(params);
}
-
-// Serialize the open list into a search string, preserving unrelated params.
-export function searchWithPanes(
- search: string,
- panes: readonly StudioPaneId[],
-): string {
- const params = new URLSearchParams(search);
- params.set(STUDIO_PANES_PARAM, panes.join(","));
- return toReadableSearch(params);
-}
diff --git a/skyvern-frontend/src/routes/workflows/studio/runSwitchNavigation.test.ts b/skyvern-frontend/src/routes/workflows/studio/runSwitchNavigation.test.ts
index 8ea045b08..c77f0eec4 100644
--- a/skyvern-frontend/src/routes/workflows/studio/runSwitchNavigation.test.ts
+++ b/skyvern-frontend/src/routes/workflows/studio/runSwitchNavigation.test.ts
@@ -60,9 +60,7 @@ describe("searchWithRunSwitched system focus", () => {
systemFocus: true,
});
expect(focused).toBe("?panes=browser&wr=wr_1&wrs=copilot");
- expect(searchWithRunSwitched(focused, "wr_2")).toBe(
- "?panes=browser&wr=wr_2",
- );
+ expect(searchWithRunSwitched(focused, "wr_2")).toBe("?wr=wr_2");
});
it("keeps the marker when the copilot hands off between its own runs", () => {
@@ -92,7 +90,7 @@ describe("useSwitchStudioRun", () => {
});
result.current("wr_1");
expect(navigate).toHaveBeenCalledWith(
- { search: "?panes=copilot,browser&wr=wr_1" },
+ { search: "?wr=wr_1" },
{ replace: false },
);
});
diff --git a/skyvern-frontend/src/routes/workflows/studio/runSwitchNavigation.ts b/skyvern-frontend/src/routes/workflows/studio/runSwitchNavigation.ts
index 748785350..990a54383 100644
--- a/skyvern-frontend/src/routes/workflows/studio/runSwitchNavigation.ts
+++ b/skyvern-frontend/src/routes/workflows/studio/runSwitchNavigation.ts
@@ -10,10 +10,11 @@ import {
toReadableSearch,
} from "./panes";
import { useStudioRunId } from "./useStudioRunId";
+import { useStudioPaneDefaults } from "./StudioPaneDefaultsContext";
// Point the studio at a different run: set ?wr=, drop the per-run selection
-// params (?active=, ?bl=, ?iteration=), and keep everything else — notably
-// ?panes=, so the layout rides through untouched. The caller merges this
+// params (?active=, ?bl=, ?iteration=). User navigation drops pane overrides.
+// The caller merges this
// against the LIVE URL string, never a render-closure (a concurrent navigate is
// already visible there), same rule as useStudioPanes.
export function searchWithRunSwitched(
@@ -30,6 +31,7 @@ export function searchWithRunSwitched(
params.get(SYSTEM_RUN_FOCUS_PARAM) !== null ||
(params.get("wr") === null && params.get("active") === null);
params.set("wr", runId);
+ if (!options?.systemFocus) params.delete("panes");
if (options?.systemFocus && copilotOwnsFocus) {
params.set(SYSTEM_RUN_FOCUS_PARAM, "copilot");
} else {
@@ -65,6 +67,7 @@ export function searchWithRunCleared(search: string): string {
export function useReleaseStudioRun(): (runId: string) => void {
const navigate = useNavigate();
const location = useLocation();
+ const { preserveNextEntry } = useStudioPaneDefaults();
return useCallback(
(runId: string) => {
const search = liveSearch(location.search);
@@ -72,9 +75,11 @@ export function useReleaseStudioRun(): (runId: string) => void {
useRunViewStore.getState().reset();
// Replace, not push: a release is not a user navigation, and pushing it
// would let Back re-focus the run we just let go of.
- navigate({ search: searchWithRunCleared(search) }, { replace: true });
+ const nextSearch = searchWithRunCleared(search);
+ preserveNextEntry(nextSearch);
+ navigate({ search: nextSearch }, { replace: true });
},
- [navigate, location.search],
+ [navigate, location.search, preserveNextEntry],
);
}
@@ -95,6 +100,7 @@ export function useSwitchStudioRun(options?: {
}): (runId: string) => void {
const navigate = useNavigate();
const location = useLocation();
+ const { preserveNextEntry } = useStudioPaneDefaults();
const studioRunId = useStudioRunId();
const replace = options?.replace ?? false;
const systemFocus = options?.systemFocus ?? false;
@@ -111,15 +117,19 @@ export function useSwitchStudioRun(options?: {
// Push by default (unlike the pane-toggle writes in useStudioPanes): a
// run switch the user asked for is a real navigation, so browser
// back/forward steps through the runs they have viewed.
- navigate(
- {
- search: searchWithRunSwitched(effectiveSearch, runId, {
- systemFocus,
- }),
- },
- { replace },
- );
+ const nextSearch = searchWithRunSwitched(effectiveSearch, runId, {
+ systemFocus,
+ });
+ preserveNextEntry(systemFocus ? nextSearch : null);
+ navigate({ search: nextSearch }, { replace });
},
- [navigate, location.search, studioRunId, replace, systemFocus],
+ [
+ navigate,
+ location.search,
+ studioRunId,
+ replace,
+ systemFocus,
+ preserveNextEntry,
+ ],
);
}
diff --git a/skyvern-frontend/src/routes/workflows/studio/runview/RunPaneHeader.tsx b/skyvern-frontend/src/routes/workflows/studio/runview/RunPaneHeader.tsx
index 7c7735541..6a7455c57 100644
--- a/skyvern-frontend/src/routes/workflows/studio/runview/RunPaneHeader.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/runview/RunPaneHeader.tsx
@@ -48,11 +48,7 @@ import { useStudioPaneCompact } from "../StudioShellContext";
import { useStudioInspectedRun } from "../useStudioInspectedRun";
import { useStudioPanes } from "../useStudioPanes";
import { ViewToggle } from "../ViewToggle";
-import {
- parsePanesParam,
- STUDIO_PANES_PARAM,
- toReadableSearch,
-} from "../panes";
+import { toReadableSearch } from "../panes";
/**
* Left header cluster of the Overview pane: the Timeline / Inputs / Outputs
@@ -92,15 +88,10 @@ export function RunPaneViewToggles() {
setView(nextView);
const next = new URLSearchParams(location.search);
next.set("view", nextView);
- const panes =
- parsePanesParam(next.get(STUDIO_PANES_PARAM)) ?? resolveLivePanes();
- next.set(
- STUDIO_PANES_PARAM,
- ["overview", ...panes.filter((pane) => pane !== "overview")].join(","),
- );
+ if (!resolveLivePanes().includes("overview")) openPane("overview");
navigate({ search: toReadableSearch(next) }, { replace: true });
},
- [location.search, navigate, resolveLivePanes, setView],
+ [location.search, navigate, openPane, resolveLivePanes, setView],
);
const focusBrowserPane = useCallback(() => {
diff --git a/skyvern-frontend/src/routes/workflows/studio/runview/RunView.test.tsx b/skyvern-frontend/src/routes/workflows/studio/runview/RunView.test.tsx
index 5fb12b233..0b43c22de 100644
--- a/skyvern-frontend/src/routes/workflows/studio/runview/RunView.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/runview/RunView.test.tsx
@@ -1,5 +1,8 @@
// @vitest-environment jsdom
+import { StudioPaneDefaultsProvider } from "../StudioPaneDefaults";
+import { useStudioPanes } from "../useStudioPanes";
+
import {
QueryClient,
QueryClientProvider,
@@ -349,9 +352,11 @@ function seedRunningRun() {
}
function LocationSpy() {
+ const { panes } = useStudioPanes();
const location = useLocation();
return (
<>
+ <output data-testid="runtime-panes">{panes.join(",")}</output>
<div data-testid="location-pathname">{location.pathname}</div>
<div data-testid="location-search">{location.search}</div>
<div data-testid="location-state">{JSON.stringify(location.state)}</div>
@@ -452,30 +457,32 @@ function renderRunView(
<QueryClientProvider client={queryClient}>
<PageSlotsProvider value={pageSlots}>
<MemoryRouter initialEntries={[initialEntry]}>
- {/* The toggles live in the pane header (StudioShell); render them
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ {/* The toggles live in the pane header (StudioShell); render them
alongside the body, under a TooltipProvider, the way the shell
composes them. Only headerExtras (the toggles) sit under the
compact context in production (StudioShell.tsx), not the body. */}
- <TooltipProvider delayDuration={0}>
- <StudioPaneCompactContext.Provider value={compact}>
- <RunPaneViewToggles />
- </StudioPaneCompactContext.Provider>
- {(typeof initialEntry === "string"
- ? initialEntry
- : initialEntry.pathname
- ).startsWith("/runs/") ? (
- <Routes>
- <Route
- path="/runs/:runId"
- element={<RunView workflowRunId="wr_1" {...props} />}
- />
- </Routes>
- ) : (
- <RunView workflowRunId="wr_1" {...props} />
- )}
- </TooltipProvider>
- <LocationSpy />
- {extra}
+ <TooltipProvider delayDuration={0}>
+ <StudioPaneCompactContext.Provider value={compact}>
+ <RunPaneViewToggles />
+ </StudioPaneCompactContext.Provider>
+ {(typeof initialEntry === "string"
+ ? initialEntry
+ : initialEntry.pathname
+ ).startsWith("/runs/") ? (
+ <Routes>
+ <Route
+ path="/runs/:runId"
+ element={<RunView workflowRunId="wr_1" {...props} />}
+ />
+ </Routes>
+ ) : (
+ <RunView workflowRunId="wr_1" {...props} />
+ )}
+ </TooltipProvider>
+ <LocationSpy />
+ {extra}
+ </StudioPaneDefaultsProvider>
</MemoryRouter>
</PageSlotsProvider>
</QueryClientProvider>
@@ -724,15 +731,21 @@ describe("RunView view toggles", () => {
});
});
- test("writes the required Overview pane when selecting a run view", async () => {
+ test("opens Overview without writing panes when selecting a run view", async () => {
seedCompletedRun();
renderRunView({}, "/?wr=wr_1");
fireEvent.click(screen.getByRole("button", { name: "Inputs" }));
await waitFor(() => {
+ expect(screen.getByTestId("runtime-panes").textContent).toContain(
+ "overview",
+ );
+ expect(screen.getByTestId("location-search").textContent).not.toContain(
+ "panes=",
+ );
expect(screen.getByTestId("location-search").textContent).toContain(
- "panes=overview,browser",
+ "view=inputs",
);
});
});
@@ -1681,7 +1694,10 @@ describe("RunView failure presentation", () => {
"CodeBlock failed because a browser operation failed at line 4.",
"browser_operation_failed",
);
- const { container, getByTestId } = renderRunView({}, "/?wr=wr_1");
+ const { container, getByTestId } = renderRunView(
+ {},
+ "/?wr=wr_1&panes=overview",
+ );
expect(useRunViewStore.getState().pinnedFrameId).toBe("wrb_code");
fireEvent.click(
@@ -1691,7 +1707,10 @@ describe("RunView failure presentation", () => {
);
await waitFor(() =>
- expect(getByTestId("location-search").textContent).toContain("browser"),
+ expect(getByTestId("runtime-panes").textContent).toContain("browser"),
+ );
+ expect(getByTestId("location-search").textContent).toContain(
+ "panes=overview",
);
await waitFor(() =>
expect(useRunViewStore.getState().pinnedFrameId).toBe("wrb_code"),
@@ -1729,11 +1748,9 @@ describe("RunView failure presentation", () => {
);
await waitFor(() =>
- expect(getByTestId("location-search").textContent).toContain(
- "panes=browser",
- ),
+ expect(getByTestId("runtime-panes").textContent).toBe("browser"),
);
- expect(getByTestId("location-search").textContent).not.toContain(
+ expect(getByTestId("location-search").textContent).toContain(
"panes=overview",
);
});
@@ -2002,7 +2019,10 @@ describe("RunView live affordances", () => {
// Park the Browser pane on a pinned replay view: the Live CTA promises
// live, so the pinned pill must not swallow the handoff.
useStudioBrowserStore.setState({ view: "screenshots" });
- const { container, getByTestId } = renderRunView();
+ const { container, getByTestId } = renderRunView(
+ {},
+ "/?wr=wr_1&panes=overview",
+ );
const scope = within(container);
fireEvent.click(
@@ -2012,7 +2032,10 @@ describe("RunView live affordances", () => {
// Unpins to the live edge and pins the Browser pane's view intent to live.
expect(useRunViewStore.getState().pinnedFrameId).toBeNull();
expect(useStudioBrowserStore.getState().view).toBe("live");
- expect(getByTestId("location-search").textContent).toContain("browser");
+ expect(getByTestId("runtime-panes").textContent).toContain("browser");
+ expect(getByTestId("location-search").textContent).toBe(
+ "?wr=wr_1&panes=overview",
+ );
});
test("a queued run shows only the queued status pill — no Live chip, no banner", () => {
diff --git a/skyvern-frontend/src/routes/workflows/studio/runview/RunView.tsx b/skyvern-frontend/src/routes/workflows/studio/runview/RunView.tsx
index 60c066083..3ed71d936 100644
--- a/skyvern-frontend/src/routes/workflows/studio/runview/RunView.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/runview/RunView.tsx
@@ -297,7 +297,12 @@ export function RunView({
const pinFrame = useRunViewStore((s) => s.pinFrame);
const jumpToLive = useRunViewStore((s) => s.jumpToLive);
const resetRunView = useRunViewStore((s) => s.reset);
- const { panes: studioPanes, openPane, setOpenPanes } = useStudioPanes();
+ const {
+ panes: studioPanes,
+ openPane,
+ setOpenPanes,
+ preserveNextEntry,
+ } = useStudioPanes();
const runPaneOpen = studioPanes.includes("overview");
const navigate = useNavigate();
const location = useLocation();
@@ -516,8 +521,16 @@ export function RunView({
} else {
next.delete("iteration");
}
- navigate({ search: toReadableSearch(next) }, { replace: true });
- }, [activeIteration, pinnedFrameId, workflowRunId, navigate]);
+ const search = toReadableSearch(next);
+ preserveNextEntry(search);
+ navigate({ search }, { replace: true });
+ }, [
+ activeIteration,
+ pinnedFrameId,
+ workflowRunId,
+ navigate,
+ preserveNextEntry,
+ ]);
// Stabilize an ?active=-only deep link by ADDING ?wr= when it's absent. Gated on
// the Overview pane being open: RunView stays mounted while its pane is closed.
@@ -547,11 +560,10 @@ export function RunView({
if (new URLSearchParams(live).get("wr")) {
return;
}
- navigate(
- { search: searchWithRunReference(live, workflowRunId) },
- { replace: true },
- );
- }, [runPaneOpen, workflowRunId, pathRunId, navigate]);
+ const search = searchWithRunReference(live, workflowRunId);
+ preserveNextEntry(search);
+ navigate({ search }, { replace: true });
+ }, [runPaneOpen, workflowRunId, pathRunId, navigate, preserveNextEntry]);
const frames = useMemo(
() => buildFilmstrip(currentTimeline),
diff --git a/skyvern-frontend/src/routes/workflows/studio/useBrowserPaneView.ts b/skyvern-frontend/src/routes/workflows/studio/useBrowserPaneView.ts
index 3584e5ff3..3a80b7ba1 100644
--- a/skyvern-frontend/src/routes/workflows/studio/useBrowserPaneView.ts
+++ b/skyvern-frontend/src/routes/workflows/studio/useBrowserPaneView.ts
@@ -15,12 +15,7 @@ import {
resolveLiveSurface,
type BrowserPaneView,
} from "./browserPaneView";
-import {
- parsePanesParam,
- STUDIO_PANES_PARAM,
- SYSTEM_RUN_FOCUS_PARAM,
- toReadableSearch,
-} from "./panes";
+import { SYSTEM_RUN_FOCUS_PARAM, toReadableSearch } from "./panes";
import { useRunVisuals, type RunVisuals } from "./useRunVisuals";
import { useStudioInspectedRun } from "./useStudioInspectedRun";
import { useStudioPanes } from "./useStudioPanes";
@@ -51,7 +46,7 @@ export function useBrowserPaneView(): BrowserPaneViewState {
const workflowPermanentId = useWorkflowPermanentId();
const [searchParams] = useSearchParams();
const navigate = useNavigate();
- const { resolveLivePanes } = useStudioPanes();
+ const { openPane, resolveLivePanes } = useStudioPanes();
const { runId, explicit } = useStudioInspectedRun();
const visuals = useRunVisuals(runId);
const { data: debugSession } = useDebugSessionQuery({
@@ -114,18 +109,13 @@ export function useBrowserPaneView(): BrowserPaneViewState {
const next = new URLSearchParams(searchParams);
if (nextView === "recording") {
next.set("view", "recording");
- const panes =
- parsePanesParam(next.get(STUDIO_PANES_PARAM)) ?? resolveLivePanes();
- next.set(
- STUDIO_PANES_PARAM,
- ["browser", ...panes.filter((pane) => pane !== "browser")].join(","),
- );
+ if (!resolveLivePanes().includes("browser")) openPane("browser");
} else if (next.get("view") === "recording") {
next.delete("view");
}
navigate({ search: toReadableSearch(next) }, { replace: true });
},
- [navigate, resolveLivePanes, searchParams, setViewIntent],
+ [navigate, openPane, resolveLivePanes, searchParams, setViewIntent],
);
const runInDebugSession =
diff --git a/skyvern-frontend/src/routes/workflows/studio/useStudioPanes.ts b/skyvern-frontend/src/routes/workflows/studio/useStudioPanes.ts
index 98b709d22..433bb4423 100644
--- a/skyvern-frontend/src/routes/workflows/studio/useStudioPanes.ts
+++ b/skyvern-frontend/src/routes/workflows/studio/useStudioPanes.ts
@@ -5,22 +5,15 @@ import {
type NavigateOptions,
} from "react-router-dom";
-import { useStudioShellStore } from "@/store/StudioShellStore";
import { liveLocationState, liveSearch } from "./liveSearch";
import {
- copilotContextForSearch,
- layoutClassForSearch,
- panesListEqual,
panesWithoutDeletedBlocked,
resolveOpenPanes,
- searchWithPanes,
searchWithRunReference,
toReadableSearch,
togglePane as togglePaneIn,
- withCopilotSelection,
withPaneClosed,
withPaneOpen,
- type CopilotPaneSelection,
type StudioPaneId,
} from "./panes";
import { SELECTED_BLOCK_SEARCH_PARAM } from "../editor/hooks/useSelectedBlockUrlSync";
@@ -32,360 +25,108 @@ import {
} from "./StudioShellContext";
type ApplyPanesOptions = Pick<NavigateOptions, "state"> & {
- // When true the resulting pane list is stored as the learned default for this
- // layout class (edit/run). System writes leave this unset so they never
- // overwrite a user's last-chosen arrangement.
- learn?: boolean;
- // Written in the SAME navigation as the pane change. null clears the value;
- // undefined leaves it untouched.
selectedBlockLabel?: string | null;
};
-function withSelectedBlockLabel(
- search: string,
- selectedBlockLabel: string | null | undefined,
-): string {
- if (selectedBlockLabel === undefined) {
- return search;
- }
- const params = new URLSearchParams(search);
- if (selectedBlockLabel === null) {
- params.delete(SELECTED_BLOCK_SEARCH_PARAM);
- } else {
- params.set(SELECTED_BLOCK_SEARCH_PARAM, selectedBlockLabel);
- }
- return toReadableSearch(params);
-}
-
-type PaneWriteKind = "normal" | "copilot-only" | "exact" | "reorder";
-
-function withUrlCopilotPreserved(
- nonCopilotPanes: readonly StudioPaneId[],
- urlPanes: readonly StudioPaneId[],
-): StudioPaneId[] {
- const next = [...nonCopilotPanes];
- const copilotIndex = urlPanes.indexOf("copilot");
- if (copilotIndex !== -1) {
- next.splice(Math.min(copilotIndex, next.length), 0, "copilot");
- }
- return next;
-}
-
-/**
- * Non-Copilot pane state remains shareable in `?panes=`. Copilot selection is
- * runtime-only, with independent edit and run memories layered over that URL.
- * Cross-route writers continue to receive only the committed URL/default list.
- */
export function useStudioPanes() {
const location = useLocation();
const navigate = useNavigate();
const studioRunId = useStudioRunId();
- const effectiveSearch = searchWithRunReference(location.search, studioRunId);
- const copilotContext = copilotContextForSearch(effectiveSearch);
- const copilotSelection = useStudioShellStore(
- (state) => state.copilotSelectionByLayout[copilotContext],
- );
- const setCopilotSelection = useStudioShellStore(
- (state) => state.setCopilotSelection,
- );
- const setPaneLayout = useStudioShellStore((state) => state.setPaneLayout);
- const { defaultPanes, clamp, notePaneWrite, learnedRunPanes } =
- useStudioPaneDefaults();
+ const {
+ isStudio,
+ panes: currentPanes,
+ getPanes,
+ updatePanes,
+ preserveNextEntry,
+ } = useStudioPaneDefaults();
const workflowDeleted = useStudioWorkflowDeletedAt() !== null;
const { restoreExpandedPane } = useStudioShellContext();
-
- const present = useCallback(
- (resolved: StudioPaneId[]): StudioPaneId[] => {
- const presented =
- clamp && panesListEqual(resolved, clamp.source)
- ? [...clamp.presented]
- : resolved;
- return workflowDeleted
- ? panesWithoutDeletedBlocked(presented)
- : presented;
- },
- [clamp, workflowDeleted],
- );
-
- const presentUrlPanes = useCallback(
- (resolved: StudioPaneId[]): StudioPaneId[] => {
- const presented =
- clamp && panesListEqual(resolved, clamp.urlSource)
- ? [...clamp.urlPresented]
- : resolved;
- return workflowDeleted
- ? panesWithoutDeletedBlocked(presented)
- : presented;
- },
- [clamp, workflowDeleted],
- );
-
const panes = useMemo(() => {
- const committed = resolveOpenPanes(
- effectiveSearch,
- defaultPanes,
- learnedRunPanes,
- );
- const selected = workflowDeleted
- ? committed
- : withCopilotSelection(committed, copilotSelection);
- return present(selected);
- }, [
- copilotSelection,
- defaultPanes,
- effectiveSearch,
- learnedRunPanes,
- present,
- workflowDeleted,
- ]);
-
- // Keep the established URL/default-only contract for block-run and run-form
- // transitions. A runtime Copilot choice must not leak into a destination URL.
+ const resolved = isStudio
+ ? [...currentPanes]
+ : resolveOpenPanes(searchWithRunReference(location.search, studioRunId));
+ return workflowDeleted ? panesWithoutDeletedBlocked(resolved) : resolved;
+ }, [isStudio, currentPanes, location.search, studioRunId, workflowDeleted]);
const resolveLivePanes = useCallback((): StudioPaneId[] => {
- const search = searchWithRunReference(
- liveSearch(location.search),
- studioRunId,
- );
- return presentUrlPanes(
- resolveOpenPanes(search, defaultPanes, learnedRunPanes),
- );
- }, [
- defaultPanes,
- learnedRunPanes,
- location.search,
- presentUrlPanes,
- studioRunId,
- ]);
+ const resolved = isStudio
+ ? [...getPanes()]
+ : resolveOpenPanes(
+ searchWithRunReference(liveSearch(location.search), studioRunId),
+ );
+ return workflowDeleted ? panesWithoutDeletedBlocked(resolved) : resolved;
+ }, [isStudio, getPanes, location.search, studioRunId, workflowDeleted]);
const applyPanes = useCallback(
(
compute: (current: StudioPaneId[]) => StudioPaneId[],
options?: ApplyPanesOptions,
- writeKind: PaneWriteKind = "normal",
) => {
restoreExpandedPane?.();
- const search = liveSearch(location.search);
- const resolvedSearch = searchWithRunReference(search, studioRunId);
- const context = copilotContextForSearch(resolvedSearch);
- const urlPanes = resolveOpenPanes(
- resolvedSearch,
- defaultPanes,
- learnedRunPanes,
- );
- const storedSelection =
- useStudioShellStore.getState().copilotSelectionByLayout[context];
- const selected = workflowDeleted
- ? urlPanes
- : withCopilotSelection(urlPanes, storedSelection);
- const current = present(selected);
- const currentCopilotIndex = current.indexOf("copilot");
- const rememberedCopilotIndex =
- storedSelection?.index ??
- (currentCopilotIndex === -1 ? undefined : currentCopilotIndex);
- const computed = compute(current);
- let next = workflowDeleted
- ? panesWithoutDeletedBlocked(computed)
- : computed;
-
- // Opening Copilot restores its remembered position instead of appending.
+ updatePanes(compute);
if (
- writeKind === "copilot-only" &&
- currentCopilotIndex === -1 &&
- next.includes("copilot") &&
- rememberedCopilotIndex !== undefined
- ) {
- next = withCopilotSelection(next, {
- open: true,
- index: rememberedCopilotIndex,
- });
- }
-
- const nextCopilotIndex = next.indexOf("copilot");
- let nextSelection = storedSelection;
- if (!workflowDeleted) {
- let selection: CopilotPaneSelection | undefined;
- if (writeKind === "copilot-only" || writeKind === "exact") {
- selection = {
- open: nextCopilotIndex !== -1,
- index:
- nextCopilotIndex === -1
- ? rememberedCopilotIndex
- : nextCopilotIndex,
- };
- } else if (writeKind === "reorder" && nextCopilotIndex !== -1) {
- selection = { open: true, index: nextCopilotIndex };
- } else if (
- writeKind === "normal" &&
- storedSelection !== undefined &&
- nextCopilotIndex !== -1
- ) {
- selection = { open: true, index: nextCopilotIndex };
- }
- if (selection !== undefined) {
- nextSelection = selection;
- }
- if (
- selection !== undefined &&
- (selection.open !== storedSelection?.open ||
- selection.index !== storedSelection?.index)
- ) {
- setCopilotSelection(context, selection);
- }
- }
-
- const currentWithoutCopilot = current.filter(
- (pane) => pane !== "copilot",
- );
- const nextWithoutCopilot = next.filter((pane) => pane !== "copilot");
- const urlPanesForWrite = workflowDeleted
- ? panesWithoutDeletedBlocked(urlPanes)
- : urlPanes;
- // An exact override replaces the full committed non-Copilot set, even
- // when the viewport clamp currently hides part of that set. Other
- // operations build on the panes the user can see.
- const nonCopilotBaseline =
- writeKind === "exact"
- ? urlPanesForWrite.filter((pane) => pane !== "copilot")
- : currentWithoutCopilot;
- const nonCopilotChanged = !panesListEqual(
- nonCopilotBaseline,
- nextWithoutCopilot,
- );
- if (!panesListEqual(current, next)) {
- notePaneWrite({
- previous: current,
- next,
- nextRuntimeSource:
- !nonCopilotChanged && !workflowDeleted
- ? withCopilotSelection(urlPanes, nextSelection)
- : undefined,
- });
- }
- if (!nonCopilotChanged && writeKind !== "normal") {
- if (
- options !== undefined &&
- ("state" in options || options.selectedBlockLabel !== undefined)
- ) {
- navigate(
- {
- pathname: location.pathname,
- search: withSelectedBlockLabel(
- search,
- options.selectedBlockLabel,
- ),
- hash: location.hash,
- },
- { replace: true, state: options.state },
- );
- }
+ !options ||
+ (!("state" in options) && options.selectedBlockLabel === undefined)
+ )
return;
- }
-
- // Until the user makes a Copilot choice, its URL position is still the
- // source of truth. Preserve the existing write behavior for normal
- // non-Copilot actions. Once runtime memory exists, keep it out of the URL.
- const urlNext =
- writeKind === "normal" && storedSelection === undefined
- ? next
- : withUrlCopilotPreserved(nextWithoutCopilot, urlPanesForWrite);
+ const search = liveSearch(location.search);
+ const params = new URLSearchParams(search);
+ if (options.selectedBlockLabel === null)
+ params.delete(SELECTED_BLOCK_SEARCH_PARAM);
+ else if (options.selectedBlockLabel !== undefined)
+ params.set(SELECTED_BLOCK_SEARCH_PARAM, options.selectedBlockLabel);
navigate(
{
pathname: location.pathname,
- search: withSelectedBlockLabel(
- searchWithPanes(search, urlNext),
- options?.selectedBlockLabel,
- ),
+ search: toReadableSearch(params),
hash: location.hash,
},
{
replace: true,
state:
- options !== undefined && "state" in options
+ "state" in options
? options.state
: liveLocationState(location.search, location.state),
},
);
- if (options?.learn && urlNext.length > 0 && !workflowDeleted) {
- const cls = layoutClassForSearch(resolvedSearch);
- if (cls !== null) {
- setPaneLayout(cls, urlNext);
- }
- }
},
[
- defaultPanes,
- learnedRunPanes,
- location.hash,
- location.pathname,
+ restoreExpandedPane,
+ updatePanes,
location.search,
+ location.pathname,
+ location.hash,
location.state,
navigate,
- notePaneWrite,
- present,
- restoreExpandedPane,
- setCopilotSelection,
- setPaneLayout,
- workflowDeleted,
- studioRunId,
],
);
const togglePane = useCallback(
- (id: StudioPaneId, opts?: Pick<ApplyPanesOptions, "learn">) =>
- applyPanes(
- (current) => togglePaneIn(current, id),
- opts,
- id === "copilot" ? "copilot-only" : "normal",
- ),
+ (id: StudioPaneId) => applyPanes((current) => togglePaneIn(current, id)),
[applyPanes],
);
-
const openPane = useCallback(
(id: StudioPaneId, options?: ApplyPanesOptions) =>
- applyPanes(
- (current) => withPaneOpen(current, id),
- options,
- id === "copilot" ? "copilot-only" : "normal",
- ),
+ applyPanes((current) => withPaneOpen(current, id), options),
[applyPanes],
);
-
const closePane = useCallback(
- (id: StudioPaneId, opts?: Pick<ApplyPanesOptions, "learn">) =>
- applyPanes(
- (current) => withPaneClosed(current, id),
- opts,
- id === "copilot" ? "copilot-only" : "normal",
- ),
+ (id: StudioPaneId) => applyPanes((current) => withPaneClosed(current, id)),
[applyPanes],
);
-
const setOpenPanes = useCallback(
- (panes: readonly StudioPaneId[]) =>
- applyPanes(() => [...panes], undefined, "exact"),
+ (panes: readonly StudioPaneId[]) => applyPanes(() => [...panes]),
[applyPanes],
);
-
const setPanesOrder = useCallback(
- (order: readonly StudioPaneId[], opts?: Pick<ApplyPanesOptions, "learn">) =>
- applyPanes(
- (current) => {
- const next = order.filter(
- (id, index) => current.includes(id) && order.indexOf(id) === index,
- );
- for (const id of current) {
- if (!next.includes(id)) {
- next.push(id);
- }
- }
- return next;
- },
- opts,
- "reorder",
- ),
+ (order: readonly StudioPaneId[]) =>
+ applyPanes((current) => {
+ const next = order.filter(
+ (id, index) => current.includes(id) && order.indexOf(id) === index,
+ );
+ return [...next, ...current.filter((id) => !next.includes(id))];
+ }),
[applyPanes],
);
-
return {
panes,
resolveLivePanes,
@@ -394,5 +135,6 @@ export function useStudioPanes() {
closePane,
setOpenPanes,
setPanesOrder,
+ preserveNextEntry,
};
}
diff --git a/skyvern-frontend/src/routes/workflows/studio/useStudioPanesOrder.test.tsx b/skyvern-frontend/src/routes/workflows/studio/useStudioPanesOrder.test.tsx
index c501d51a7..ea7e8dc73 100644
--- a/skyvern-frontend/src/routes/workflows/studio/useStudioPanesOrder.test.tsx
+++ b/skyvern-frontend/src/routes/workflows/studio/useStudioPanesOrder.test.tsx
@@ -10,22 +10,22 @@ import {
} from "react-router-dom";
import { beforeEach, describe, expect, test, vi } from "vitest";
-import { useStudioShellStore } from "@/store/StudioShellStore";
-
+import { StudioPaneDefaultsProvider } from "./StudioPaneDefaults";
import { type StudioPaneId } from "./panes";
import { StudioShellContext } from "./StudioShellContext";
import { useStudioPanes } from "./useStudioPanes";
beforeEach(() => {
localStorage.clear();
- useStudioShellStore.getState().reset();
});
function OrderProbe({ order }: { order: StudioPaneId[] }) {
const { panes, setPanesOrder, setOpenPanes } = useStudioPanes();
+ const location = useLocation();
return (
<div>
<output data-testid="panes">{panes.join(",")}</output>
+ <output data-testid="search">{location.search}</output>
<button onClick={() => setPanesOrder(order)}>set-order</button>
<button onClick={() => setOpenPanes(order)}>set-open</button>
</div>
@@ -35,12 +35,14 @@ function OrderProbe({ order }: { order: StudioPaneId[] }) {
function renderWithPanes(search: string, order: StudioPaneId[]) {
return render(
<MemoryRouter initialEntries={[`/studio${search}`]}>
- <OrderProbe order={order} />
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <OrderProbe order={order} />
+ </StudioPaneDefaultsProvider>
</MemoryRouter>,
);
}
-function CopilotMemoryProbe() {
+function CopilotVisitProbe() {
const { panes, togglePane, openPane } = useStudioPanes();
const location = useLocation();
const navigate = useNavigate();
@@ -85,15 +87,17 @@ function CopilotMemoryProbe() {
);
}
-function renderCopilotMemory(entry: string) {
+function renderCopilotVisit(entry: string) {
return render(
<MemoryRouter initialEntries={[entry]}>
- <CopilotMemoryProbe />
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <CopilotVisitProbe />
+ </StudioPaneDefaultsProvider>
</MemoryRouter>,
);
}
-describe("useStudioPanes Copilot context memory", () => {
+describe("useStudioPanes visit-scoped Copilot selection", () => {
test("restores an expanded pane before a nested pane action, even when the target is already open", () => {
const restoreExpandedPane = vi.fn();
render(
@@ -108,7 +112,9 @@ describe("useStudioPanes Copilot context memory", () => {
}}
>
<MemoryRouter initialEntries={["/studio?panes=editor,browser"]}>
- <CopilotMemoryProbe />
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <CopilotVisitProbe />
+ </StudioPaneDefaultsProvider>
</MemoryRouter>
</StudioShellContext.Provider>,
);
@@ -119,12 +125,12 @@ describe("useStudioPanes Copilot context memory", () => {
expect(screen.getByTestId("panes").textContent).toBe("editor,browser");
});
- test("restores independent Studio and past-run selections without changing the URL", () => {
+ test("reapplies shared entry panes when navigating between Studio and runs", () => {
const studioAddress =
"/studio?via=blank&panes=copilot,editor,browser#proof";
const runAddress =
"/studio?via=blank&panes=copilot,editor,browser&wr=wr_1#proof";
- renderCopilotMemory(studioAddress);
+ renderCopilotVisit(studioAddress);
expect(screen.getByTestId("panes").textContent).toBe(
"copilot,editor,browser",
@@ -139,17 +145,19 @@ describe("useStudioPanes Copilot context memory", () => {
);
expect(screen.getByTestId("address").textContent).toBe(runAddress);
- // Record an explicit open choice for the run context.
+ // Reopening appends Copilot during this visit.
fireEvent.click(screen.getByText("toggle-copilot"));
expect(screen.getByTestId("address").textContent).toBe(runAddress);
fireEvent.click(screen.getByText("toggle-copilot"));
expect(screen.getByTestId("panes").textContent).toBe(
- "copilot,editor,browser",
+ "editor,browser,copilot",
);
expect(screen.getByTestId("address").textContent).toBe(runAddress);
fireEvent.click(screen.getByText("return-to-studio"));
- expect(screen.getByTestId("panes").textContent).toBe("editor,browser");
+ expect(screen.getByTestId("panes").textContent).toBe(
+ "copilot,editor,browser",
+ );
expect(screen.getByTestId("address").textContent).toBe(studioAddress);
fireEvent.click(screen.getByText("inspect-run"));
@@ -161,7 +169,7 @@ describe("useStudioPanes Copilot context memory", () => {
test("keeps route-state handoffs while preserving pathname, search, and hash", () => {
const address = "/studio?via=blank&panes=editor,browser#proof";
- renderCopilotMemory(address);
+ renderCopilotVisit(address);
fireEvent.click(screen.getByText("open-copilot-with-state"));
@@ -175,46 +183,44 @@ describe("useStudioPanes Copilot context memory", () => {
fireEvent.click(screen.getByText("toggle-browser"));
expect(screen.getByTestId("panes").textContent).toBe("editor,copilot");
- expect(screen.getByTestId("address").textContent).toBe(
- "/studio?via=blank&panes=editor#proof",
- );
+ expect(screen.getByTestId("address").textContent).toBe(address);
expect(screen.getByTestId("route-state").textContent).toBe(
'{"copilotMessage":"Fix this run"}',
);
});
- test("does not leak a runtime Copilot choice through a later URL pane write", () => {
- renderCopilotMemory("/studio?via=blank&panes=copilot,editor,browser#proof");
+ test("preserves the incoming URL through multiple pane toggles", () => {
+ renderCopilotVisit("/studio?via=blank&panes=copilot,editor,browser#proof");
fireEvent.click(screen.getByText("toggle-copilot"));
fireEvent.click(screen.getByText("toggle-browser"));
expect(screen.getByTestId("panes").textContent).toBe("editor");
expect(screen.getByTestId("address").textContent).toBe(
- "/studio?via=blank&panes=copilot,editor#proof",
+ "/studio?via=blank&panes=copilot,editor,browser#proof",
);
});
- test("keeps URL-owned Copilot order through a non-Copilot pane write", () => {
- renderCopilotMemory("/studio?panes=editor,copilot,browser#proof");
+ test("keeps Copilot order through a non-Copilot pane toggle", () => {
+ renderCopilotVisit("/studio?panes=editor,copilot,browser#proof");
fireEvent.click(screen.getByText("toggle-editor"));
expect(screen.getByTestId("panes").textContent).toBe("copilot,browser");
expect(screen.getByTestId("address").textContent).toBe(
- "/studio?panes=copilot,browser#proof",
+ "/studio?panes=editor,copilot,browser#proof",
);
});
- test("restores Copilot at its remembered position after reopening", () => {
+ test("appends Copilot after reopening", () => {
const address = "/studio?via=blank&panes=editor,copilot,browser#proof";
- renderCopilotMemory(address);
+ renderCopilotVisit(address);
fireEvent.click(screen.getByText("toggle-copilot"));
fireEvent.click(screen.getByText("toggle-copilot"));
expect(screen.getByTestId("panes").textContent).toBe(
- "editor,copilot,browser",
+ "editor,browser,copilot",
);
expect(screen.getByTestId("address").textContent).toBe(address);
});
@@ -229,21 +235,23 @@ describe("useStudioPanes setOpenPanes", () => {
});
describe("useStudioPanes setPanesOrder", () => {
- test("commits a reordered list to the URL", () => {
+ test("reorders runtime panes while preserving the URL", () => {
renderWithPanes("?panes=copilot,editor,browser", [
"editor",
"browser",
"copilot",
]);
+ const search = screen.getByTestId("search").textContent;
fireEvent.click(screen.getByText("set-order"));
+ expect(screen.getByTestId("search").textContent).toBe(search);
expect(screen.getByTestId("panes").textContent).toBe(
"editor,browser,copilot",
);
});
- test("keeps the open set from the URL: closed panes in the order are dropped, missing ones appended", () => {
+ test("keeps the current open set: closed panes in the order are dropped, missing ones appended", () => {
// "overview" is not open, so it must not open; "browser" is open but
// absent from the requested order, so it keeps a slot at the end.
renderWithPanes("?panes=copilot,editor,browser", [
@@ -252,7 +260,9 @@ describe("useStudioPanes setPanesOrder", () => {
"copilot",
]);
+ const search = screen.getByTestId("search").textContent;
fireEvent.click(screen.getByText("set-order"));
+ expect(screen.getByTestId("search").textContent).toBe(search);
expect(screen.getByTestId("panes").textContent).toBe(
"editor,copilot,browser",
@@ -266,7 +276,9 @@ describe("useStudioPanes setPanesOrder", () => {
"copilot",
]);
+ const search = screen.getByTestId("search").textContent;
fireEvent.click(screen.getByText("set-order"));
+ expect(screen.getByTestId("search").textContent).toBe(search);
expect(screen.getByTestId("panes").textContent).toBe("browser,copilot");
});
@@ -281,7 +293,14 @@ function renderAtRunRoute(entry: string) {
return render(
<MemoryRouter initialEntries={[entry]}>
<Routes>
- <Route path="/runs/:runId/*" element={<RunRouteProbe />} />
+ <Route
+ path="/runs/:runId/*"
+ element={
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <RunRouteProbe />
+ </StudioPaneDefaultsProvider>
+ }
+ />
</Routes>
</MemoryRouter>,
);
@@ -341,7 +360,9 @@ describe("useStudioPanes selectedBlockLabel", () => {
test("openPane carries selectedBlockLabel in the same navigation", () => {
render(
<MemoryRouter initialEntries={["/studio?panes=editor"]}>
- <OpenWithParamsProbe />
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <OpenWithParamsProbe />
+ </StudioPaneDefaultsProvider>
</MemoryRouter>,
);
fireEvent.click(screen.getByText("open-with-params"));
@@ -353,15 +374,18 @@ describe("useStudioPanes selectedBlockLabel", () => {
expect(screen.getByTestId("panes").textContent).toContain("copilot");
});
- test("a pane write that changes the URL pane list carries them too", () => {
+ test("opening Browser preserves the pane URL while setting the selected block", () => {
render(
<MemoryRouter initialEntries={["/studio?panes=editor"]}>
- <OpenWithParamsProbe />
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <OpenWithParamsProbe />
+ </StudioPaneDefaultsProvider>
</MemoryRouter>,
);
fireEvent.click(screen.getByText("open-browser-with-params"));
const search = screen.getByTestId("search").textContent ?? "";
- expect(search).toContain("panes=editor,browser");
+ expect(new URLSearchParams(search).get("panes")).toBe("editor");
+ expect(screen.getByTestId("panes").textContent).toBe("editor,browser");
expect(search).toContain("selected-block=checkout");
});
@@ -370,7 +394,9 @@ describe("useStudioPanes selectedBlockLabel", () => {
<MemoryRouter
initialEntries={["/studio?panes=editor&selected-block=stale"]}
>
- <OpenWithParamsProbe />
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <OpenWithParamsProbe />
+ </StudioPaneDefaultsProvider>
</MemoryRouter>,
);
fireEvent.click(screen.getByText("open-clearing-params"));
@@ -382,7 +408,9 @@ describe("useStudioPanes selectedBlockLabel", () => {
test("openPane carries selectedBlockLabel even when the pane is already open", () => {
render(
<MemoryRouter initialEntries={["/studio?panes=editor,copilot"]}>
- <OpenWithParamsProbe />
+ <StudioPaneDefaultsProvider hasBlocks={true}>
+ <OpenWithParamsProbe />
+ </StudioPaneDefaultsProvider>
</MemoryRouter>,
);
fireEvent.click(screen.getByText("open-with-params"));
diff --git a/skyvern-frontend/src/store/StudioShellStore.test.ts b/skyvern-frontend/src/store/StudioShellStore.test.ts
deleted file mode 100644
index 8003c5067..000000000
--- a/skyvern-frontend/src/store/StudioShellStore.test.ts
+++ /dev/null
@@ -1,175 +0,0 @@
-// @vitest-environment jsdom
-
-import { beforeEach, describe, expect, test } from "vitest";
-
-import {
- STUDIO_SHELL_STORAGE_KEY,
- useStudioShellStore,
-} from "./StudioShellStore";
-
-beforeEach(() => {
- localStorage.clear();
- useStudioShellStore.getState().reset();
-});
-
-describe("StudioShellStore", () => {
- test("defaults PiP to expanded", () => {
- expect(useStudioShellStore.getState().pipMinimized).toBe(false);
- });
-
- test("keeps edit and run Copilot selections independent and unpersisted", () => {
- useStudioShellStore
- .getState()
- .setCopilotSelection("edit", { open: false, index: 0 });
- useStudioShellStore
- .getState()
- .setCopilotSelection("run", { open: true, index: 2 });
-
- expect(useStudioShellStore.getState().copilotSelectionByLayout).toEqual({
- edit: { open: false, index: 0 },
- run: { open: true, index: 2 },
- });
- const raw = localStorage.getItem(STUDIO_SHELL_STORAGE_KEY);
- expect(raw).not.toBeNull();
- expect(JSON.parse(raw!).state).not.toHaveProperty(
- "copilotSelectionByLayout",
- );
- });
- test("persists PiP, pane widths, and pane layouts", () => {
- useStudioShellStore.getState().setPipMinimized(true);
-
- const raw = localStorage.getItem(STUDIO_SHELL_STORAGE_KEY);
- expect(raw).not.toBeNull();
- expect(JSON.parse(raw!).state).toEqual({
- pipMinimized: true,
- paneWidths: {},
- paneLayouts: {},
- });
- });
-
- test("migrates stale v0 Copilot collapse without restoring it", async () => {
- localStorage.setItem(
- STUDIO_SHELL_STORAGE_KEY,
- JSON.stringify({
- state: { copilotCollapsed: true, pipMinimized: true },
- version: 0,
- }),
- );
-
- await useStudioShellStore.persist.rehydrate();
-
- const state = useStudioShellStore.getState();
- expect(state.pipMinimized).toBe(true);
- expect(
- "copilotCollapsed" in (state as unknown as Record<string, unknown>),
- ).toBe(false);
- });
-
- test("migrates v1 → v2: keeps paneWidths, adds paneLayouts: {}", async () => {
- localStorage.setItem(
- STUDIO_SHELL_STORAGE_KEY,
- JSON.stringify({
- state: { pipMinimized: true, paneWidths: { copilot: 400 } },
- version: 1,
- }),
- );
-
- await useStudioShellStore.persist.rehydrate();
-
- const state = useStudioShellStore.getState();
- expect(state.pipMinimized).toBe(true);
- expect(state.paneWidths).toEqual({ copilot: 400 });
- expect(state.paneLayouts).toEqual({});
- });
-
- test("merges and persists pane widths; reset clears them", () => {
- useStudioShellStore.getState().setPaneWidths({ copilot: 412 });
- useStudioShellStore.getState().setPaneWidths({ editor: 350.4 });
-
- expect(useStudioShellStore.getState().paneWidths).toEqual({
- copilot: 412,
- editor: 350,
- });
- const raw = localStorage.getItem(STUDIO_SHELL_STORAGE_KEY);
- expect(JSON.parse(raw!).state.paneWidths).toEqual({
- copilot: 412,
- editor: 350,
- });
-
- useStudioShellStore.getState().resetPaneWidths();
- expect(useStudioShellStore.getState().paneWidths).toEqual({});
- });
-
- test("drops non-numeric persisted pane widths on rehydrate", async () => {
- localStorage.setItem(
- STUDIO_SHELL_STORAGE_KEY,
- JSON.stringify({
- state: {
- pipMinimized: false,
- paneWidths: { copilot: 320, editor: "wide", browser: -10 },
- },
- version: 0,
- }),
- );
-
- await useStudioShellStore.persist.rehydrate();
-
- expect(useStudioShellStore.getState().paneWidths).toEqual({
- copilot: 320,
- });
- });
-
- test("setPaneLayout stores the layout for the given class", () => {
- useStudioShellStore
- .getState()
- .setPaneLayout("edit", ["copilot", "editor", "browser"]);
-
- expect(useStudioShellStore.getState().paneLayouts["edit"]).toEqual([
- "copilot",
- "editor",
- "browser",
- ]);
- const raw = localStorage.getItem(STUDIO_SHELL_STORAGE_KEY);
- expect(JSON.parse(raw!).state.paneLayouts).toEqual({
- edit: ["copilot", "editor", "browser"],
- });
- });
-
- test("setPaneLayout ignores empty lists", () => {
- useStudioShellStore
- .getState()
- .setPaneLayout("edit", ["copilot", "browser"]);
- useStudioShellStore.getState().setPaneLayout("edit", []);
-
- expect(useStudioShellStore.getState().paneLayouts["edit"]).toEqual([
- "copilot",
- "browser",
- ]);
- });
-
- test("setPaneLayout overwrites a previous value for the same class", () => {
- useStudioShellStore
- .getState()
- .setPaneLayout("edit", ["copilot", "browser"]);
- useStudioShellStore.getState().setPaneLayout("edit", ["editor", "browser"]);
-
- expect(useStudioShellStore.getState().paneLayouts["edit"]).toEqual([
- "editor",
- "browser",
- ]);
- });
-
- test("setPaneLayout keeps classes independent", () => {
- useStudioShellStore
- .getState()
- .setPaneLayout("edit", ["copilot", "browser"]);
- useStudioShellStore
- .getState()
- .setPaneLayout("run", ["browser", "overview"]);
-
- expect(useStudioShellStore.getState().paneLayouts).toEqual({
- edit: ["copilot", "browser"],
- run: ["browser", "overview"],
- });
- });
-});
diff --git a/skyvern-frontend/src/store/StudioShellStore.ts b/skyvern-frontend/src/store/StudioShellStore.ts
deleted file mode 100644
index e010103a3..000000000
--- a/skyvern-frontend/src/store/StudioShellStore.ts
+++ /dev/null
@@ -1,124 +0,0 @@
-import { create } from "zustand";
-import { persist, createJSONStorage } from "zustand/middleware";
-
-import {
- type CopilotPaneSelection,
- type StudioLayoutClass,
- type StudioPaneId,
-} from "@/routes/workflows/studio/panes";
-import { sanitizePaneWidth, type PaneWidths } from "@/store/paneWidths";
-
-type StudioShellState = {
- pipMinimized: boolean;
- // User-pinned pane widths (px) from divider drags; per user, NOT in the URL
- // so shared ?panes= links never impose someone else's widths.
- paneWidths: PaneWidths;
- // Last user-gesture pane list per layout class; used to restore defaults.
- paneLayouts: Partial<Record<StudioLayoutClass, StudioPaneId[]>>;
- // Copilot is selected independently for live editing and inspected runs.
- // This state is intentionally excluded from persisted storage.
- copilotSelectionByLayout: Partial<
- Record<StudioLayoutClass, CopilotPaneSelection>
- >;
- setPipMinimized: (minimized: boolean) => void;
- togglePip: () => void;
- setPaneWidths: (widths: PaneWidths) => void;
- resetPaneWidths: () => void;
- setPaneLayout: (
- cls: StudioLayoutClass,
- panes: readonly StudioPaneId[],
- ) => void;
- setCopilotSelection: (
- cls: StudioLayoutClass,
- selection: CopilotPaneSelection,
- ) => void;
- reset: () => void;
-};
-
-const DEFAULTS = {
- pipMinimized: false,
- paneWidths: {} as PaneWidths,
- paneLayouts: {} as Partial<Record<StudioLayoutClass, StudioPaneId[]>>,
- copilotSelectionByLayout: {} as Partial<
- Record<StudioLayoutClass, CopilotPaneSelection>
- >,
-};
-
-export const STUDIO_SHELL_STORAGE_KEY = "skyvern.studioShell";
-
-function sanitizePaneWidths(raw: unknown): PaneWidths {
- if (raw === null || typeof raw !== "object") {
- return {};
- }
- const result: PaneWidths = {};
- for (const [key, value] of Object.entries(raw)) {
- const width = sanitizePaneWidth(value);
- if (width !== undefined) {
- result[key] = width;
- }
- }
- return result;
-}
-
-export const useStudioShellStore = create<StudioShellState>()(
- persist(
- (set) => ({
- ...DEFAULTS,
- setPipMinimized: (pipMinimized) => set({ pipMinimized }),
- togglePip: () => set((state) => ({ pipMinimized: !state.pipMinimized })),
- setPaneWidths: (widths) =>
- set((state) => ({
- paneWidths: sanitizePaneWidths({ ...state.paneWidths, ...widths }),
- })),
- resetPaneWidths: () => set({ paneWidths: {} }),
- setPaneLayout: (cls, panes) => {
- if (panes.length === 0) return;
- set((state) => ({
- paneLayouts: { ...state.paneLayouts, [cls]: [...panes] },
- }));
- },
- setCopilotSelection: (cls, selection) =>
- set((state) => ({
- copilotSelectionByLayout: {
- ...state.copilotSelectionByLayout,
- [cls]: selection,
- },
- })),
- reset: () => set(DEFAULTS),
- }),
- {
- name: STUDIO_SHELL_STORAGE_KEY,
- storage: createJSONStorage(() => localStorage),
- // v0 also persisted copilotCollapsed; pane visibility now lives in the URL.
- // v2 adds paneLayouts for per-class layout memory.
- version: 2,
- migrate: (persisted) => {
- const state = persisted as
- | {
- pipMinimized?: unknown;
- paneWidths?: unknown;
- paneLayouts?: unknown;
- }
- | undefined;
- return {
- pipMinimized: Boolean(state?.pipMinimized),
- paneWidths: sanitizePaneWidths(state?.paneWidths),
- // Inner pane id arrays are not sanitized here; sanitizeLearnedPanes
- // in StudioPaneDefaults.tsx drops stale ids at read time.
- paneLayouts:
- state?.paneLayouts !== null &&
- typeof state?.paneLayouts === "object"
- ? (state.paneLayouts as Partial<
- Record<StudioLayoutClass, StudioPaneId[]>
- >)
- : {},
- };
- },
- partialize: (state) => ({
- pipMinimized: state.pipMinimized,
- paneWidths: state.paneWidths,
- paneLayouts: state.paneLayouts,
- }),
- },
- ),
-);
diff --git a/skyvern-frontend/src/store/paneWidths.ts b/skyvern-frontend/src/store/paneWidths.ts
index 96174031f..ac5160d2a 100644
--- a/skyvern-frontend/src/store/paneWidths.ts
+++ b/skyvern-frontend/src/store/paneWidths.ts
@@ -2,9 +2,6 @@
// to pane renames (stale keys are simply never read).
export type PaneWidths = Record<string, number>;
-// Pane widths round-trip through localStorage, so treat each value as
-// untrusted. Lives at the store layer so both StudioShellStore (persistence)
-// and the studio layout math share one validation contract.
export function sanitizePaneWidth(value: unknown): number | undefined {
return typeof value === "number" && Number.isFinite(value) && value > 0
? Math.round(value)
diff --git a/skyvern/config.py b/skyvern/config.py
index 1dfbf69e1..9de093ac6 100644
--- a/skyvern/config.py
+++ b/skyvern/config.py
@@ -541,6 +541,11 @@ class Settings(BaseSettings):
# Kill switch for the tier-1 semantic commit read (SKY-15322): decisive-accept-only ARIA/value
# probe consulted before the shape heuristics, which remain the fallback either way.
TASK_V3_SEMANTIC_COMMIT_VERIFY: bool = True
+ # When type's click is refused only by the viewport check (a sub-pixel input under its own display
+ # layer), press the mouse at the field's centre before focusing, then Tab and read the value back
+ # (SKY-16501). Force-on term only: runs are randomized per run by the flag of the same name, read
+ # through run_arm_enabled(TYPE_COORDINATE_CLICK_FLAG, ...). Off: the field is reached by focus() alone.
+ TASK_V3_TYPE_COORDINATE_CLICK: bool = False
# Render the previous block's outcome (status / finish reason / final URL) and whether this is the
# last block into a v3 block's goal. Costs prompt tokens on every turn of the block, so it is
# measured via taskv3_block_context_tokens before it earns default-on. The outcome itself is
@@ -773,6 +778,7 @@ class Settings(BaseSettings):
TOTP_LIFESPAN_MINUTES: int = 10
TOTP_RAW_CONTENT_MAX_LENGTH: int = 65536
TOTP_MULTI_FIELD_MIN_REMAINING_SECONDS: int = 20
+ TWILIO_SMS_2FA_ENABLED: bool = False
VERIFICATION_CODE_INITIAL_WAIT_TIME_SECS: int = 40
VERIFICATION_CODE_POLLING_TIMEOUT_MINS: int = 15
diff --git a/skyvern/core/script_generations/script_skyvern_page.py b/skyvern/core/script_generations/script_skyvern_page.py
index 0dc0f69bf..ca9ea3ab9 100644
--- a/skyvern/core/script_generations/script_skyvern_page.py
+++ b/skyvern/core/script_generations/script_skyvern_page.py
@@ -14,7 +14,7 @@ from cachetools import TTLCache
from playwright.async_api import Page
from skyvern.config import settings
-from skyvern.constants import BROWSER_DOWNLOAD_TIMEOUT, NAVIGATION_MAX_RETRY_TIME
+from skyvern.constants import BROWSER_DOWNLOAD_TIMEOUT, ERROR_CODE_REASONING_MAX_LENGTH, NAVIGATION_MAX_RETRY_TIME
from skyvern.core.script_generations.real_skyvern_page_ai import RealSkyvernPageAi, render_template
from skyvern.core.script_generations.skyvern_page import (
ActionCall,
@@ -24,7 +24,7 @@ from skyvern.core.script_generations.skyvern_page import (
SkyvernPage,
)
from skyvern.core.script_generations.skyvern_page_ai import SkyvernPageAi
-from skyvern.errors.errors import UserDefinedError
+from skyvern.errors.errors import UserDefinedError, filter_to_user_defined_codes
from skyvern.exceptions import (
BrowserSessionSwitchNotAllowed,
IllegitCompleteScriptTermination,
@@ -49,6 +49,8 @@ from skyvern.forge.sdk.schemas.totp_codes import OTPType
from skyvern.forge.sdk.services.credentials import generate_totp_code
from skyvern.schemas.steps import AgentStepOutput
from skyvern.services.otp_service import MAGIC_LINK_ANCHOR_GRACE, poll_otp_value
+from skyvern.utils.contained_effects import contained_effect
+from skyvern.utils.secret_redaction import redact_secrets_from_text
from skyvern.utils.url_validators import validate_fetch_url
from skyvern.webeye.actions.action_types import ActionType
from skyvern.webeye.actions.actions import (
@@ -80,6 +82,34 @@ LOG = structlog.get_logger()
action_wrap = SkyvernPage.action_wrap
+def _redact_termination_payload(
+ reason: str, errors: list[UserDefinedError] | None = None
+) -> tuple[str, list[UserDefinedError] | None]:
+ context = skyvern_context.current()
+ workflow_run_id = context.workflow_run_id if context else None
+ manager = app.WORKFLOW_CONTEXT_MANAGER
+ secrets = (
+ manager.get_secret_values_for_run(workflow_run_id)
+ if manager.artifact_redaction_enabled(workflow_run_id)
+ else manager.runtime_secret_values_for_artifacts()
+ )
+ reason = redact_secrets_from_text(reason, secrets)
+ if errors is not None:
+ errors = [
+ error.model_copy(
+ update={
+ "reasoning": redact_secrets_from_text(error.reasoning, secrets)[
+ :ERROR_CODE_REASONING_MAX_LENGTH
+ ].strip()
+ }
+ )
+ for error in errors
+ if not any(secret and secret in error.error_code for secret in secrets)
+ and redact_secrets_from_text(error.error_code, secrets) == error.error_code
+ ]
+ return reason, errors
+
+
class ScriptSkyvernPage(SkyvernPage):
"""
A minimal adapter around the chosen driver that:
@@ -311,6 +341,13 @@ class ScriptSkyvernPage(SkyvernPage):
except Exception as e:
call.error = e
action_status = ActionStatus.failed
+ if action == ActionType.TERMINATE and isinstance(e, ScriptTerminationException):
+ prompt, _ = _redact_termination_payload(prompt)
+ args = ()
+ kwargs = {"errors": e.user_defined_errors or [], "reasoning": str(e)}
+ call.args = args
+ call.kwargs = kwargs
+ call.meta = ActionMetadata(prompt, None)
# Build a readable representation of the failed call.
# Only log the first positional arg (selector) — the second arg
@@ -375,42 +412,48 @@ class ScriptSkyvernPage(SkyvernPage):
except Exception:
pass # Don't block if download detection fails
- self._record(call)
- # Bind positional args to parameter names so subclass-specific fields
- # (e.g. MoveAction.x/y, ScrollAction.scroll_x/scroll_y) are accessible
- # by name in _create_action_and_result_after_execution. Copy to avoid
- # mutating the caller's dict.
- recording_kwargs = dict(kwargs)
try:
- bound = inspect.signature(fn).bind_partial(self, *args, **kwargs)
- for name, value in bound.arguments.items():
- if name in ("self", "kwargs"):
- continue
- recording_kwargs.setdefault(name, value)
- except TypeError:
- if "selector" not in recording_kwargs and args:
- first_arg = args[0]
- if isinstance(first_arg, str):
- recording_kwargs["selector"] = first_arg
- # Auto-create action after execution and store result
- await self._create_action_and_result_after_execution(
- action_type=action,
- intention=prompt,
- status=action_status,
- kwargs=recording_kwargs,
- call_result=call.result,
- call_error=call.error,
- download_triggered=download_triggered,
- downloaded_files=downloaded_files,
- started_at=started_at,
- finished_at=finished_at,
- )
+ self._record(call)
+ # Bind positional args to parameter names so subclass-specific fields
+ # (e.g. MoveAction.x/y, ScrollAction.scroll_x/scroll_y) are accessible
+ # by name in _create_action_and_result_after_execution. Copy to avoid
+ # mutating the caller's dict.
+ recording_kwargs = dict(kwargs)
+ try:
+ bound = inspect.signature(fn).bind_partial(self, *args, **kwargs)
+ for name, value in bound.arguments.items():
+ if name in ("self", "kwargs"):
+ continue
+ recording_kwargs.setdefault(name, value)
+ except TypeError:
+ if "selector" not in recording_kwargs and args:
+ first_arg = args[0]
+ if isinstance(first_arg, str):
+ recording_kwargs["selector"] = first_arg
+ # Auto-create action after execution and store result
+ await self._create_action_and_result_after_execution(
+ action_type=action,
+ intention=prompt,
+ status=action_status,
+ kwargs=recording_kwargs,
+ call_result=call.result,
+ call_error=call.error,
+ download_triggered=download_triggered,
+ downloaded_files=downloaded_files,
+ started_at=started_at,
+ finished_at=finished_at,
+ )
- # Auto-create screenshot artifact after execution
- await self._create_screenshot_after_execution()
+ # Auto-create screenshot artifact after execution
+ await self._create_screenshot_after_execution()
- # Auto-create HTML artifact after execution
- await self._create_html_action_after_execution()
+ # Auto-create HTML artifact after execution
+ await self._create_html_action_after_execution()
+ except Exception:
+ if not isinstance(call.error, ScriptTerminationException):
+ raise
+ with contained_effect("script termination recording failure"):
+ LOG.warning("Failed to record script termination", action_type=action)
async def _update_action_reasoning(
self,
@@ -553,6 +596,12 @@ class ScriptSkyvernPage(SkyvernPage):
data_extraction_goal=data_extraction_goal,
data_extraction_schema=data_extraction_schema,
)
+ elif action_type == ActionType.TERMINATE and isinstance(call_error, ScriptTerminationException):
+ action = TerminateAction(
+ **common_fields,
+ errors=call_error.user_defined_errors or [],
+ reasoning=str(call_error),
+ )
else:
subclass = ACTION_TYPE_TO_CLASS.get(action_type, Action)
subclass_extra_fields = {
@@ -579,7 +628,9 @@ class ScriptSkyvernPage(SkyvernPage):
created_action = await app.DATABASE.workflow_params.create_action(action)
# Skip LLM reasoning in script mode — use static string instead.
# Build a descriptive label from the selector for the timeline.
- if context and context.script_mode:
+ if action_type == ActionType.TERMINATE and isinstance(call_error, ScriptTerminationException):
+ pass # Keep the scrubbed termination reason recorded with the action.
+ elif context and context.script_mode:
label = intention[:80] if intention else ""
if not label and selector:
# Extract a human-readable name from the selector
@@ -1207,44 +1258,32 @@ class ScriptSkyvernPage(SkyvernPage):
@action_wrap(ActionType.TERMINATE)
async def terminate(self, errors: list[str], **kwargs: Any) -> None:
+ reasoning, _ = _redact_termination_payload("; ".join(errors))
+ msg = "Terminate called" + (": " + reasoning if errors else "")
+ user_defined_errors: list[UserDefinedError] | None = None
context = skyvern_context.current()
- # Only run handler inside a full workflow context (DB lookups + LLM extraction)
- if (
- not context
- or not context.organization_id
- or not context.workflow_run_id
- or not context.task_id
- or not context.step_id
- ):
- msg = "Terminate called"
- if errors:
- msg += ": " + "; ".join(errors)
- raise ScriptTerminationException(msg)
-
- task = await app.DATABASE.tasks.get_task(context.task_id, context.organization_id)
- step = await app.DATABASE.tasks.get_step(context.step_id, context.organization_id)
- if task and step:
- action = TerminateAction(
- organization_id=context.organization_id,
- workflow_run_id=context.workflow_run_id,
- task_id=context.task_id,
- step_id=context.step_id,
- step_order=step.order,
- action_order=context.action_order,
- # errors=[] is list[UserDefinedError] for LLM-extracted error codes (populated by
- # handle_terminate_action); errors param above is list[str] for exception messaging.
- errors=[],
- reasoning="; ".join(errors) if errors else None,
- )
+ if context and context.organization_id and context.workflow_run_id and context.task_id and context.step_id:
try:
- await handle_terminate_action(action, self.page, self.scraped_page, task, step)
- except Exception:
- LOG.warning("handle_terminate_action failed during script terminate()", exc_info=True)
+ task = await app.DATABASE.tasks.get_task(context.task_id, context.organization_id)
+ step = await app.DATABASE.tasks.get_step(context.step_id, context.organization_id)
+ if task and step:
+ action = TerminateAction(
+ organization_id=context.organization_id,
+ workflow_run_id=context.workflow_run_id,
+ task_id=context.task_id,
+ step_id=context.step_id,
+ step_order=step.order,
+ action_order=context.action_order,
+ errors=[],
+ reasoning=reasoning or None,
+ )
+ await handle_terminate_action(action, self.page, self.scraped_page, task, step)
+ user_defined_errors, _ = filter_to_user_defined_codes(action.errors, task.error_code_mapping)
+ except Exception as exc: # noqa: BLE001 - Termination must survive classifier failures without logging secrets.
+ LOG.warning("Failed to classify script termination", error_type=type(exc).__name__)
- msg = "Terminate called"
- if errors:
- msg += ": " + "; ".join(errors)
- raise ScriptTerminationException(msg)
+ msg, user_defined_errors = _redact_termination_payload(msg, user_defined_errors)
+ raise ScriptTerminationException(msg, user_defined_errors=user_defined_errors)
async def _update_step_output_before_complete(self, context: skyvern_context.SkyvernContext) -> None:
"""Update step.output with actions_and_results before complete validation.
diff --git a/skyvern/exceptions.py b/skyvern/exceptions.py
index d730739b2..1284179ff 100644
--- a/skyvern/exceptions.py
+++ b/skyvern/exceptions.py
@@ -5,7 +5,10 @@ from dataclasses import dataclass
from datetime import UTC, datetime
from http import HTTPStatus
from importlib.util import find_spec
-from typing import NoReturn
+from typing import TYPE_CHECKING, NoReturn
+
+if TYPE_CHECKING:
+ from skyvern.errors.errors import UserDefinedError
# Representative modules that indicate the local extra is installed enough for
# embedded/browser import graphs. Keep this list intentionally small, but include
@@ -1828,7 +1831,10 @@ class AzureConfigurationError(AzureBaseError):
class ScriptTerminationException(SkyvernException):
- def __init__(self, reason: str | None = None) -> None:
+ def __init__(
+ self, reason: str | None = None, *, user_defined_errors: list["UserDefinedError"] | None = None
+ ) -> None:
+ self.user_defined_errors = user_defined_errors
super().__init__(reason)
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.py
index 9b92d07a9..b9527989b 100644
--- a/skyvern/forge/agent.py
+++ b/skyvern/forge/agent.py
@@ -180,7 +180,11 @@ from skyvern.forge.sdk.workflow.models.workflow import Workflow, WorkflowRun, Wo
from skyvern.forge.taskv3.frame_perception import frame_perception_enabled, resolve_frame_perception
from skyvern.forge.taskv3.loop import LoopOutcome, RoundAction
from skyvern.forge.taskv3.pre_submit_capture import PreSubmitCaptureRing, is_run_sampled, pre_submit_screenshot
-from skyvern.forge.taskv3.run_arms import OBSERVE_DROP_OFFVIEWPORT_UNNAMED_FLAG, resolve_run_arm
+from skyvern.forge.taskv3.run_arms import (
+ OBSERVE_DROP_OFFVIEWPORT_UNNAMED_FLAG,
+ TYPE_COORDINATE_CLICK_FLAG,
+ resolve_run_arm,
+)
from skyvern.forge.taskv3.target_label import compose_target_intention
from skyvern.forge.validation_evidence_router import (
ValidationRouterMode,
@@ -1859,6 +1863,13 @@ class ForgeAgent:
organization_id=task.organization_id,
forced=settings.TASK_V3_OBSERVE_DROP_OFFVIEWPORT_UNNAMED,
)
+ await resolve_run_arm(
+ context,
+ TYPE_COORDINATE_CLICK_FLAG,
+ distinct_id=task.workflow_run_id or task.task_id,
+ organization_id=task.organization_id,
+ forced=settings.TASK_V3_TYPE_COORDINATE_CLICK,
+ )
offer_error_codes = False
if task.error_code_mapping:
try:
diff --git a/skyvern/forge/api_app.py b/skyvern/forge/api_app.py
index 4626ec277..b1b80b570 100644
--- a/skyvern/forge/api_app.py
+++ b/skyvern/forge/api_app.py
@@ -51,6 +51,8 @@ from skyvern.forge.sdk.routes.google_oauth import google_oauth_router
from skyvern.forge.sdk.routes.google_sheets import google_sheets_router
from skyvern.forge.sdk.routes.microsoft_oauth import microsoft_oauth_router
from skyvern.forge.sdk.routes.routers import base_router, legacy_base_router, legacy_v2_router
+from skyvern.forge.sdk.routes.sms_inbound import sms_inbound_router
+from skyvern.forge.sdk.routes.twilio_integration import twilio_integration_router
from skyvern.forge.sdk.services.local_org_auth_token_service import (
ensure_local_api_key,
ensure_local_org,
@@ -500,6 +502,13 @@ def create_api_app() -> FastAPI:
fastapi_app.include_router(microsoft_oauth_router, prefix="/api/v1/microsoft", include_in_schema=False)
fastapi_app.include_router(google_sheets_router, prefix="/v1/google/sheets", include_in_schema=False)
fastapi_app.include_router(google_sheets_router, prefix="/api/v1/google/sheets", include_in_schema=False)
+ # Twilio SMS 2FA: public inbound receiver (secret-URL + signature auth inside
+ # the handler) and the org-scoped integration management routes. Same
+ # dual-prefix + include_in_schema=False pattern as the OAuth routers above.
+ fastapi_app.include_router(sms_inbound_router, prefix="/v1/sms", include_in_schema=False)
+ fastapi_app.include_router(sms_inbound_router, prefix="/api/v1/sms", include_in_schema=False)
+ fastapi_app.include_router(twilio_integration_router, prefix="/v1/integrations/twilio", include_in_schema=False)
+ fastapi_app.include_router(twilio_integration_router, prefix="/api/v1/integrations/twilio", include_in_schema=False)
# local dev endpoints
if settings.ENV == "local":
diff --git a/skyvern/forge/request_logging.py b/skyvern/forge/request_logging.py
index 19f0724f0..5ce1beb3e 100644
--- a/skyvern/forge/request_logging.py
+++ b/skyvern/forge/request_logging.py
@@ -53,6 +53,8 @@ _SENSITIVE_ENDPOINTS = {
}
_SENSITIVE_ENDPOINT_PATTERNS = (
re.compile(r"^(?:POST|PUT) /(?:api/)?v1/credentials(?:/.*)?$"),
+ # Twilio credentials and inbound SMS codes must stay opaque even with malformed bodies.
+ re.compile(r"^(?:POST|PUT|PATCH|DELETE) /(?:api/)?v1/(?:integrations/twilio|sms)(?:/.*)?$"),
# MCP arguments and results can contain arbitrary secrets, including malformed JSON.
re.compile(r"^[^ ]+ /mcp(?:/.*)?$", re.DOTALL),
)
diff --git a/skyvern/forge/sdk/db/agent_db.py b/skyvern/forge/sdk/db/agent_db.py
index a0ccbc99d..04495ccc3 100644
--- a/skyvern/forge/sdk/db/agent_db.py
+++ b/skyvern/forge/sdk/db/agent_db.py
@@ -39,6 +39,7 @@ from skyvern.forge.sdk.db.repositories.otp import OTPRepository
from skyvern.forge.sdk.db.repositories.schedules import SchedulesRepository
from skyvern.forge.sdk.db.repositories.scripts import ScriptsRepository
from skyvern.forge.sdk.db.repositories.self_heal import SelfHealRepository
+from skyvern.forge.sdk.db.repositories.sms import SMSRepository
from skyvern.forge.sdk.db.repositories.tags import TagsRepository
from skyvern.forge.sdk.db.repositories.tasks import TasksRepository
from skyvern.forge.sdk.db.repositories.uploaded_files import UploadedFilesRepository
@@ -411,6 +412,7 @@ class AgentDB(BaseAlchemyDB):
self.credentials = CredentialRepository(self.Session, debug_enabled, self.is_retryable_error)
self.credential_folders = CredentialFoldersRepository(self.Session, debug_enabled, self.is_retryable_error)
self.otp = OTPRepository(self.Session, debug_enabled, self.is_retryable_error)
+ self.sms = SMSRepository(self.Session, debug_enabled, self.is_retryable_error)
self.debug = DebugRepository(self.Session, debug_enabled, self.is_retryable_error)
self.organizations = OrganizationsRepository(self.Session, debug_enabled, self.is_retryable_error)
self.scripts = ScriptsRepository(self.Session, debug_enabled, self.is_retryable_error)
diff --git a/skyvern/forge/sdk/db/enums.py b/skyvern/forge/sdk/db/enums.py
index 20ed67249..ea4978b51 100644
--- a/skyvern/forge/sdk/db/enums.py
+++ b/skyvern/forge/sdk/db/enums.py
@@ -10,6 +10,7 @@ class OrganizationAuthTokenType(StrEnum):
bitwarden_credential = "bitwarden_credential"
custom_llm = "custom_llm"
google_oauth_client_config = "google_oauth_client_config"
+ twilio_credential = "twilio_credential"
class TaskType(StrEnum):
diff --git a/skyvern/forge/sdk/db/id.py b/skyvern/forge/sdk/db/id.py
index 674b5ad9c..781fb3c03 100644
--- a/skyvern/forge/sdk/db/id.py
+++ b/skyvern/forge/sdk/db/id.py
@@ -65,6 +65,8 @@ TASK_GENERATION_PREFIX = "tg"
TASK_PREFIX = "tsk"
TASK_RUN_PREFIX = "tr"
TOTP_CODE_PREFIX = "totp"
+SMS_CONFIG_PREFIX = "smsc"
+PHONE_NUMBER_PREFIX = "pn"
USER_PREFIX = "u"
WORKFLOW_PARAMETER_PREFIX = "wp"
WORKFLOW_PERMANENT_ID_PREFIX = "wpid"
@@ -209,6 +211,16 @@ def generate_totp_code_id() -> str:
return f"{TOTP_CODE_PREFIX}_{int_id}"
+def generate_sms_config_id() -> str:
+ int_id = generate_id()
+ return f"{SMS_CONFIG_PREFIX}_{int_id}"
+
+
+def generate_phone_number_id() -> str:
+ int_id = generate_id()
+ return f"{PHONE_NUMBER_PREFIX}_{int_id}"
+
+
def generate_action_id() -> str:
int_id = generate_id()
return f"{ACTION_PREFIX}_{int_id}"
diff --git a/skyvern/forge/sdk/db/models.py b/skyvern/forge/sdk/db/models.py
index 08e60bec0..fe763197b 100644
--- a/skyvern/forge/sdk/db/models.py
+++ b/skyvern/forge/sdk/db/models.py
@@ -9,6 +9,7 @@ from sqlalchemy import (
Column,
DateTime,
ForeignKey,
+ ForeignKeyConstraint,
Index,
Integer,
Numeric,
@@ -51,12 +52,14 @@ from skyvern.forge.sdk.db.id import (
generate_organization_bitwarden_collection_id,
generate_output_parameter_id,
generate_persistent_browser_session_id,
+ generate_phone_number_id,
generate_run_tag_event_id,
generate_script_block_id,
generate_script_fallback_episode_id,
generate_script_file_id,
generate_script_id,
generate_script_revision_id,
+ generate_sms_config_id,
generate_step_id,
generate_tag_event_id,
generate_tag_key_id,
@@ -253,6 +256,89 @@ class OrganizationAuthTokenModel(Base):
deleted_at = Column(DateTime, nullable=True)
+class OrganizationSMSConfigModel(Base):
+ __tablename__ = "organization_sms_configs"
+ __table_args__ = (
+ Index(
+ "uq_org_sms_configs_one_connected",
+ "organization_id",
+ unique=True,
+ postgresql_where=text("mode = 'connected' AND deleted_at IS NULL"),
+ sqlite_where=text("mode = 'connected' AND deleted_at IS NULL"),
+ ),
+ UniqueConstraint(
+ "sms_config_id",
+ "organization_id",
+ name="uq_organization_sms_configs_id_org",
+ ),
+ )
+
+ sms_config_id = Column(String, primary_key=True, default=generate_sms_config_id)
+ organization_id = Column(String, ForeignKey("organizations.organization_id"), nullable=False, index=True)
+ mode = Column(String, nullable=False)
+ encrypted_webhook_secret = Column(String, nullable=False)
+ webhook_secret_encrypted_method = Column(String, nullable=False, default="aes", server_default="aes")
+ daily_ingest_cap = Column(Integer, nullable=False, default=100, server_default="100")
+ created_at = Column(DateTime, default=datetime.datetime.utcnow, nullable=False)
+ modified_at = Column(
+ DateTime,
+ default=datetime.datetime.utcnow,
+ onupdate=datetime.datetime.utcnow,
+ nullable=False,
+ )
+ deleted_at = Column(DateTime, nullable=True)
+
+
+class OrganizationPhoneNumberModel(Base):
+ __tablename__ = "organization_phone_numbers"
+ __table_args__ = (
+ ForeignKeyConstraint(
+ ["sms_config_id", "organization_id"],
+ [
+ "organization_sms_configs.sms_config_id",
+ "organization_sms_configs.organization_id",
+ ],
+ name="fk_organization_phone_numbers_sms_config_org",
+ ),
+ ForeignKeyConstraint(
+ ["credential_id", "organization_id"],
+ ["credentials.credential_id", "credentials.organization_id"],
+ name="fk_organization_phone_numbers_credential_org",
+ ),
+ Index(
+ "uq_org_phone_numbers_org_number",
+ "organization_id",
+ "phone_number",
+ unique=True,
+ postgresql_where=text("status = 'active' AND deleted_at IS NULL"),
+ sqlite_where=text("status = 'active' AND deleted_at IS NULL"),
+ ),
+ )
+
+ phone_number_id = Column(String, primary_key=True, default=generate_phone_number_id)
+ organization_id = Column(String, ForeignKey("organizations.organization_id"), nullable=False, index=True)
+ sms_config_id = Column(String, nullable=False, index=True)
+ phone_number = Column(String, nullable=False)
+ provider = Column(String, nullable=False, default="twilio", server_default="twilio")
+ provider_number_sid = Column(String, nullable=True)
+ previous_sms_url = Column(String, nullable=True)
+ previous_sms_method = Column(String, nullable=True)
+ previous_sms_application_sid = Column(String, nullable=True)
+ credential_id = Column(String, nullable=True)
+ provider_cost_cents = Column(Integer, nullable=True)
+ price_cents = Column(Integer, nullable=True)
+ status = Column(String, nullable=False, default="active", server_default="active")
+ quarantined_until = Column(DateTime, nullable=True)
+ created_at = Column(DateTime, default=datetime.datetime.utcnow, nullable=False)
+ modified_at = Column(
+ DateTime,
+ default=datetime.datetime.utcnow,
+ onupdate=datetime.datetime.utcnow,
+ nullable=False,
+ )
+ deleted_at = Column(DateTime, nullable=True)
+
+
class ArtifactModel(Base):
__tablename__ = "artifacts"
__table_args__ = (
@@ -977,6 +1063,7 @@ class BitwardenLoginCredentialParameterModel(Base):
bitwarden_collection_id = Column(String, nullable=True, default=None)
bitwarden_item_id = Column(String, nullable=True, default=None)
url_parameter_key = Column(String, nullable=True, default=None)
+ totp_identifier = Column(String, nullable=True, default=None)
created_at = Column(DateTime, default=datetime.datetime.utcnow, nullable=False)
modified_at = Column(
DateTime,
@@ -1091,7 +1178,7 @@ class OnePasswordCredentialParameterModel(Base):
description = Column(String, nullable=True)
vault_id = Column(String, nullable=False)
item_id = Column(String, nullable=False)
-
+ totp_identifier = Column(String, nullable=True, default=None)
created_at = Column(DateTime, default=datetime.datetime.utcnow, nullable=False)
modified_at = Column(
DateTime,
@@ -1204,6 +1291,14 @@ class TOTPCodeModel(Base):
__table_args__ = (
Index("ix_totp_codes_org_created_at", "organization_id", "created_at"),
Index("ix_totp_codes_otp_type", "organization_id", "otp_type"),
+ Index(
+ "uq_totp_codes_org_external_message_id",
+ "organization_id",
+ "external_message_id",
+ unique=True,
+ postgresql_where=text("external_message_id IS NOT NULL"),
+ sqlite_where=text("external_message_id IS NOT NULL"),
+ ),
)
totp_code_id = Column(String, primary_key=True, default=generate_totp_code_id)
@@ -1215,6 +1310,7 @@ class TOTPCodeModel(Base):
content = Column(String, nullable=False)
code = Column(String)
source = Column(String)
+ external_message_id = Column(String, nullable=True)
created_at = Column(DateTime, default=datetime.datetime.utcnow, nullable=False, index=True)
modified_at = Column(DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow, nullable=False)
expired_at = Column(DateTime, index=True)
@@ -1683,6 +1779,7 @@ class CredentialModel(Base):
__tablename__ = "credentials"
__table_args__ = (
Index("credential_folder_id_idx", "folder_id"),
+ UniqueConstraint("credential_id", "organization_id", name="uq_credentials_id_org"),
Index(
"uq_credentials_browser_profile_id",
"browser_profile_id",
@@ -1702,6 +1799,7 @@ class CredentialModel(Base):
username = Column(String, nullable=True)
totp_type = Column(String, nullable=False, default="none")
totp_identifier = Column(String, nullable=True, default=None)
+ has_totp_seed = Column(Boolean, nullable=True)
card_last4 = Column(String, nullable=True)
card_brand = Column(String, nullable=True)
secret_label = Column(String, nullable=True)
diff --git a/skyvern/forge/sdk/db/repositories/credentials.py b/skyvern/forge/sdk/db/repositories/credentials.py
index 518751638..73bd86360 100644
--- a/skyvern/forge/sdk/db/repositories/credentials.py
+++ b/skyvern/forge/sdk/db/repositories/credentials.py
@@ -39,6 +39,7 @@ class CredentialRepository(BaseRepository):
card_last4: str | None,
card_brand: str | None,
totp_identifier: str | None = None,
+ has_totp_seed: bool = False,
secret_label: str | None = None,
tested_url: str | None = None,
proxy_location: ProxyLocationInput = None,
@@ -58,6 +59,7 @@ class CredentialRepository(BaseRepository):
credential_type=credential_type,
username=username,
totp_type=totp_type,
+ has_totp_seed=has_totp_seed,
totp_identifier=totp_identifier,
card_last4=card_last4,
card_brand=card_brand,
@@ -267,6 +269,7 @@ class CredentialRepository(BaseRepository):
username: str | None = None,
totp_type: str = "none",
totp_identifier: str | None = None,
+ has_totp_seed: bool | None = None,
card_last4: str | None = None,
card_brand: str | None = None,
secret_label: str | None = None,
@@ -293,6 +296,8 @@ class CredentialRepository(BaseRepository):
credential.username = username
credential.totp_type = totp_type
credential.totp_identifier = totp_identifier
+ if has_totp_seed is not None:
+ credential.has_totp_seed = has_totp_seed
credential.card_last4 = card_last4
credential.card_brand = card_brand
credential.secret_label = secret_label
diff --git a/skyvern/forge/sdk/db/repositories/organizations.py b/skyvern/forge/sdk/db/repositories/organizations.py
index ab593ddcd..f41645b13 100644
--- a/skyvern/forge/sdk/db/repositories/organizations.py
+++ b/skyvern/forge/sdk/db/repositories/organizations.py
@@ -32,6 +32,8 @@ from skyvern.forge.sdk.schemas.organizations import (
BitwardenOrganizationAuthToken,
Organization,
OrganizationAuthToken,
+ TwilioCredential,
+ TwilioOrganizationAuthToken,
)
from skyvern.forge.sdk.schemas.tasks import TaskStatus
from skyvern.forge.sdk.workflow.models.workflow import WorkflowRunStatus
@@ -165,6 +167,7 @@ class OrganizationsRepository(BaseRepository):
try:
await session.commit()
except IntegrityError as exc:
+ exc.hide_parameters = True
await session.rollback()
if slug is not None or not derive_slug or not is_org_slug_unique_violation(exc):
raise
@@ -196,7 +199,11 @@ class OrganizationsRepository(BaseRepository):
)
if organization is None:
raise NotFoundError
- await session.commit()
+ try:
+ await session.commit()
+ except IntegrityError as exc:
+ exc.hide_parameters = True
+ raise
await session.refresh(organization)
return convert_to_organization(organization)
@@ -286,6 +293,13 @@ class OrganizationsRepository(BaseRepository):
token_type: Literal["bitwarden_credential"],
) -> BitwardenOrganizationAuthToken | None: ...
+ @overload
+ async def get_valid_org_auth_token( # type: ignore
+ self,
+ organization_id: str,
+ token_type: Literal["twilio_credential"],
+ ) -> TwilioOrganizationAuthToken | None: ...
+
@db_operation("get_valid_org_auth_token")
async def get_valid_org_auth_token(
self,
@@ -298,8 +312,15 @@ class OrganizationsRepository(BaseRepository):
"custom_credential_service",
"custom_llm",
"google_oauth_client_config",
+ "twilio_credential",
],
- ) -> OrganizationAuthToken | AzureOrganizationAuthToken | BitwardenOrganizationAuthToken | None:
+ ) -> (
+ OrganizationAuthToken
+ | AzureOrganizationAuthToken
+ | BitwardenOrganizationAuthToken
+ | TwilioOrganizationAuthToken
+ | None
+ ):
async with self.Session() as session:
if token := (
await session.scalars(
@@ -319,9 +340,14 @@ class OrganizationsRepository(BaseRepository):
self,
organization_id: str,
token_type: OrganizationAuthTokenType,
- token: str | AzureClientSecretCredential | BitwardenCredential,
+ token: str | AzureClientSecretCredential | BitwardenCredential | TwilioCredential,
encrypted_method: EncryptMethod | None = None,
- ) -> OrganizationAuthToken | AzureOrganizationAuthToken | BitwardenOrganizationAuthToken:
+ ) -> (
+ OrganizationAuthToken
+ | AzureOrganizationAuthToken
+ | BitwardenOrganizationAuthToken
+ | TwilioOrganizationAuthToken
+ ):
"""Atomically invalidate existing tokens and create a new one in a single transaction."""
if token_type is OrganizationAuthTokenType.azure_client_secret_credential:
if not isinstance(token, AzureClientSecretCredential):
@@ -331,6 +357,14 @@ class OrganizationsRepository(BaseRepository):
if not isinstance(token, BitwardenCredential):
raise TypeError("Expected BitwardenCredential for this token_type")
plaintext_token = token.model_dump_json()
+ elif token_type is OrganizationAuthTokenType.twilio_credential:
+ if not isinstance(token, TwilioCredential):
+ raise TypeError("Expected TwilioCredential for this token_type")
+ if encrypted_method is None:
+ encrypted_method = EncryptMethod.AES
+ elif encrypted_method != EncryptMethod.AES:
+ raise ValueError("Twilio credentials must use AES encryption")
+ plaintext_token = token.model_dump_json()
else:
if not isinstance(token, str):
raise TypeError("Expected str token for this token_type")
@@ -454,6 +488,8 @@ class OrganizationsRepository(BaseRepository):
token: str | AzureClientSecretCredential | BitwardenCredential,
encrypted_method: EncryptMethod | None = None,
) -> OrganizationAuthToken | AzureOrganizationAuthToken | BitwardenOrganizationAuthToken:
+ if token_type is OrganizationAuthTokenType.twilio_credential:
+ raise ValueError("Twilio credentials must use replace_org_auth_token")
if token_type is OrganizationAuthTokenType.azure_client_secret_credential:
if not isinstance(token, AzureClientSecretCredential):
raise TypeError("Expected AzureClientSecretCredential for this token_type")
@@ -496,6 +532,8 @@ class OrganizationsRepository(BaseRepository):
token: str | AzureClientSecretCredential | BitwardenCredential,
encrypted_method: EncryptMethod | None = None,
) -> OrganizationAuthToken | AzureOrganizationAuthToken | BitwardenOrganizationAuthToken:
+ if token_type is OrganizationAuthTokenType.twilio_credential:
+ raise ValueError("Twilio credentials must use replace_org_auth_token")
if token_type is OrganizationAuthTokenType.azure_client_secret_credential:
if not isinstance(token, AzureClientSecretCredential):
raise TypeError("Expected AzureClientSecretCredential for this token_type")
diff --git a/skyvern/forge/sdk/db/repositories/otp.py b/skyvern/forge/sdk/db/repositories/otp.py
index 5629d2f7d..14a03fc43 100644
--- a/skyvern/forge/sdk/db/repositories/otp.py
+++ b/skyvern/forge/sdk/db/repositories/otp.py
@@ -3,20 +3,38 @@ from __future__ import annotations
from datetime import datetime, timedelta, timezone
from sqlalchemy import and_, asc, func, or_, select, update
+from sqlalchemy.exc import IntegrityError
from sqlalchemy.sql.elements import ColumnElement
from skyvern.config import settings
from skyvern.forge.sdk.db._error_handling import db_operation
from skyvern.forge.sdk.db.base_repository import BaseRepository
+from skyvern.forge.sdk.db.datetime_utils import naive_utc_now, to_naive_utc
from skyvern.forge.sdk.db.models import TOTPCodeModel
from skyvern.forge.sdk.schemas.totp_codes import OTPType, RawTOTPCode, TOTPCode
from skyvern.utils.email_validation import SAFE_EMAIL_ADDRESS_PATTERN, normalize_email_address
+from skyvern.utils.phone_validation import looks_like_phone_identifier, phone_identifier_candidates
+
+_EXTERNAL_MESSAGE_DEDUPE_INDEX = "uq_totp_codes_org_external_message_id"
+
+
+def _is_duplicate_external_message(error: IntegrityError) -> bool:
+ constraint_name = getattr(getattr(error.orig, "diag", None), "constraint_name", None)
+ if constraint_name == _EXTERNAL_MESSAGE_DEDUPE_INDEX:
+ return True
+ error_text = str(error.orig).lower()
+ return (
+ _EXTERNAL_MESSAGE_DEDUPE_INDEX in error_text
+ or "unique constraint failed: totp_codes.organization_id, totp_codes.external_message_id" in error_text
+ )
def _identifier_filter(totp_identifier: str) -> ColumnElement[bool]:
stripped_identifier = totp_identifier.strip()
if SAFE_EMAIL_ADDRESS_PATTERN.fullmatch(stripped_identifier):
return func.lower(TOTPCodeModel.totp_identifier) == normalize_email_address(totp_identifier)
+ if looks_like_phone_identifier(stripped_identifier):
+ return TOTPCodeModel.totp_identifier.in_({totp_identifier, *phone_identifier_candidates(stripped_identifier)})
return TOTPCodeModel.totp_identifier == totp_identifier
@@ -141,6 +159,28 @@ class OTPRepository(BaseRepository):
totp_codes = (await session.scalars(query)).all()
return [TOTPCode.model_validate(totp_code) for totp_code in totp_codes]
+ @db_operation("count_otp_codes_since")
+ async def count_otp_codes_since(
+ self,
+ organization_id: str,
+ totp_identifier: str,
+ created_after: datetime,
+ source: str | None = None,
+ ) -> int:
+ async with self.Session() as session:
+ query = (
+ select(func.count())
+ .select_from(TOTPCodeModel)
+ .where(
+ TOTPCodeModel.organization_id == organization_id,
+ _identifier_filter(totp_identifier),
+ TOTPCodeModel.created_at >= to_naive_utc(created_after),
+ )
+ )
+ if source is not None:
+ query = query.where(TOTPCodeModel.source == source)
+ return int((await session.scalar(query)) or 0)
+
@db_operation("create_otp_code")
async def create_otp_code(
self,
@@ -175,6 +215,56 @@ class OTPRepository(BaseRepository):
await session.refresh(new_totp_code)
return TOTPCode.model_validate(new_totp_code)
+ @db_operation("create_otp_code_if_new", log_errors=False)
+ async def create_otp_code_if_new(
+ self,
+ organization_id: str,
+ totp_identifier: str,
+ content: str,
+ code: str,
+ otp_type: OTPType,
+ task_id: str | None = None,
+ workflow_id: str | None = None,
+ workflow_run_id: str | None = None,
+ source: str | None = None,
+ external_message_id: str | None = None,
+ expired_at: datetime | None = None,
+ ) -> TOTPCode | None:
+ async with self.Session() as session:
+ if external_message_id is not None:
+ existing = await session.scalar(
+ select(TOTPCodeModel).filter_by(
+ organization_id=organization_id,
+ external_message_id=external_message_id,
+ )
+ )
+ if existing is not None:
+ return None
+ row = TOTPCodeModel(
+ organization_id=organization_id,
+ totp_identifier=totp_identifier,
+ content=content,
+ code=code,
+ task_id=task_id or None,
+ workflow_id=workflow_id or None,
+ workflow_run_id=workflow_run_id or None,
+ source=source,
+ external_message_id=external_message_id,
+ expired_at=to_naive_utc(expired_at),
+ otp_type=otp_type,
+ )
+ session.add(row)
+ try:
+ await session.commit()
+ except IntegrityError as error:
+ error.hide_parameters = True
+ await session.rollback()
+ if _is_duplicate_external_message(error):
+ return None
+ raise
+ await session.refresh(row)
+ return TOTPCode.model_validate(row)
+
@db_operation("create_raw_otp_code")
async def create_raw_otp_code(
self,
@@ -206,6 +296,55 @@ class OTPRepository(BaseRepository):
await session.refresh(row)
return RawTOTPCode.model_validate(row)
+ @db_operation("create_raw_otp_code_if_new", log_errors=False)
+ async def create_raw_otp_code_if_new(
+ self,
+ organization_id: str,
+ totp_identifier: str,
+ content: str,
+ task_id: str | None = None,
+ workflow_id: str | None = None,
+ workflow_run_id: str | None = None,
+ source: str | None = None,
+ external_message_id: str | None = None,
+ expired_at: datetime | None = None,
+ ) -> RawTOTPCode | None:
+ async with self.Session() as session:
+ if external_message_id is not None:
+ existing = await session.scalar(
+ select(TOTPCodeModel).filter_by(
+ organization_id=organization_id,
+ external_message_id=external_message_id,
+ )
+ )
+ if existing is not None:
+ return None
+ row = TOTPCodeModel(
+ organization_id=organization_id,
+ totp_identifier=totp_identifier,
+ content=content,
+ code=None,
+ otp_type=None,
+ parse_status="raw",
+ task_id=task_id or None,
+ workflow_id=workflow_id or None,
+ workflow_run_id=workflow_run_id or None,
+ source=source,
+ external_message_id=external_message_id,
+ expired_at=to_naive_utc(expired_at),
+ )
+ session.add(row)
+ try:
+ await session.commit()
+ except IntegrityError as error:
+ error.hide_parameters = True
+ await session.rollback()
+ if _is_duplicate_external_message(error):
+ return None
+ raise
+ await session.refresh(row)
+ return RawTOTPCode.model_validate(row)
+
@db_operation("get_raw_otp_codes")
async def get_raw_otp_codes(
self,
@@ -262,7 +401,7 @@ class OTPRepository(BaseRepository):
TOTPCodeModel.organization_id == organization_id,
TOTPCodeModel.parse_status == "raw",
)
- .values(code=code, otp_type=otp_type, parse_status="parsed", modified_at=datetime.now(timezone.utc))
+ .values(code=code, otp_type=otp_type, parse_status="parsed", modified_at=naive_utc_now())
.returning(TOTPCodeModel)
)
row = (await session.scalars(query)).one_or_none()
diff --git a/skyvern/forge/sdk/db/repositories/sms.py b/skyvern/forge/sdk/db/repositories/sms.py
new file mode 100644
index 000000000..21b677f90
--- /dev/null
+++ b/skyvern/forge/sdk/db/repositories/sms.py
@@ -0,0 +1,576 @@
+from __future__ import annotations
+
+import asyncio
+from collections import defaultdict
+from collections.abc import AsyncIterator
+from contextlib import asynccontextmanager
+from datetime import datetime
+from typing import cast
+
+from sqlalchemy import case, func, select, text
+from sqlalchemy.exc import IntegrityError
+from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession
+from sqlalchemy.pool import QueuePool
+
+from skyvern.forge.sdk.db._error_handling import db_operation, register_passthrough_exception
+from skyvern.forge.sdk.db._sentinels import _UNSET
+from skyvern.forge.sdk.db.base_repository import BaseRepository
+from skyvern.forge.sdk.db.datetime_utils import naive_utc_now, to_naive_utc
+from skyvern.forge.sdk.db.models import CredentialModel, OrganizationPhoneNumberModel, OrganizationSMSConfigModel
+from skyvern.forge.sdk.encrypt import EncryptMethod, encryptor
+from skyvern.forge.sdk.schemas.sms import PHONE_NUMBER_STATUSES, SMS_CONFIG_MODES, OrganizationPhoneNumber, SMSConfig
+from skyvern.forge.sdk.workflow.secret_encryption import encrypt_secret_field_value
+from skyvern.utils.phone_validation import (
+ is_e164_phone_number,
+ looks_like_phone_identifier,
+ normalize_phone_identifier,
+)
+
+_PHONE_NUMBER_UNIQUE_INDEX = "uq_org_phone_numbers_org_number"
+_PHONE_NUMBER_SQLITE_UNIQUE_ERROR = (
+ "UNIQUE constraint failed: organization_phone_numbers.organization_id, organization_phone_numbers.phone_number"
+)
+
+
+class PhoneNumberAlreadyExists(ValueError):
+ """Raised when an active phone number is already registered for an organization."""
+
+
+class InvalidPhoneNumberStatus(ValueError):
+ """Raised when a phone number update specifies an unsupported status."""
+
+
+class InvalidSMSConfigMode(ValueError):
+ """Raised when an SMS config specifies an unsupported mode."""
+
+
+class SMSConfigHasActivePhoneNumbers(ValueError):
+ """Raised when an SMS config still has active phone number references."""
+
+
+register_passthrough_exception(InvalidPhoneNumberStatus)
+register_passthrough_exception(InvalidSMSConfigMode)
+register_passthrough_exception(PhoneNumberAlreadyExists)
+register_passthrough_exception(SMSConfigHasActivePhoneNumbers)
+
+
+def _is_phone_number_unique_violation(error: IntegrityError) -> bool:
+ original: BaseException | None = error.orig
+ seen: set[int] = set()
+ while original is not None and id(original) not in seen:
+ seen.add(id(original))
+ diagnostic = getattr(original, "diag", None)
+ constraint_name = getattr(original, "constraint_name", None) or getattr(diagnostic, "constraint_name", None)
+ if constraint_name is not None:
+ return constraint_name == _PHONE_NUMBER_UNIQUE_INDEX
+ original = original.__cause__
+ return str(error.orig) == _PHONE_NUMBER_SQLITE_UNIQUE_ERROR
+
+
+def _hide_integrity_error_parameters(error: IntegrityError) -> None:
+ error.hide_parameters = True
+
+
+async def _lock_sms_configs(
+ session: AsyncSession,
+ organization_id: str,
+ sms_config_ids: set[str],
+) -> dict[str, OrganizationSMSConfigModel]:
+ rows = (
+ await session.scalars(
+ select(OrganizationSMSConfigModel)
+ .where(
+ OrganizationSMSConfigModel.organization_id == organization_id,
+ OrganizationSMSConfigModel.sms_config_id.in_(sms_config_ids),
+ OrganizationSMSConfigModel.deleted_at.is_(None),
+ )
+ .order_by(OrganizationSMSConfigModel.sms_config_id)
+ .with_for_update()
+ )
+ ).all()
+ return {row.sms_config_id: row for row in rows}
+
+
+_POSTGRES_LIFECYCLE_SERIALIZER = asyncio.Lock()
+_POSTGRES_LIFECYCLE_SEMAPHORES: dict[int, asyncio.Semaphore] = {}
+
+
+async def _encrypt_previous_sms_url(value: str | None, organization_id: str) -> str | None:
+ return await encrypt_secret_field_value(
+ value, organization_id=organization_id, field_name="previous_sms_url", full_template_reference_only=True
+ )
+
+
+class SMSRepository(BaseRepository):
+ _local_advisory_locks: defaultdict[str, asyncio.Lock] = defaultdict(asyncio.Lock)
+
+ def _postgres_pool_info(self) -> tuple[bool, int | None, int | None]:
+ bind: AsyncEngine | None = self.Session.kw.get("bind")
+ if bind is None:
+ return True, None, None
+ engine = bind.sync_engine
+ if engine.dialect.name != "postgresql":
+ return False, None, None
+ pool = engine.pool
+ capacity = pool.size() + pool._max_overflow if isinstance(pool, QueuePool) and pool._max_overflow >= 0 else None
+ return True, id(pool), capacity
+
+ @asynccontextmanager
+ async def _lifecycle_permit(self) -> AsyncIterator[None]:
+ is_postgres, pool_key, pool_capacity = self._postgres_pool_info()
+ if not is_postgres:
+ yield
+ return
+ if pool_key is None or pool_capacity is None or pool_capacity <= 1:
+ async with _POSTGRES_LIFECYCLE_SERIALIZER:
+ yield
+ return
+ semaphore = _POSTGRES_LIFECYCLE_SEMAPHORES.setdefault(
+ pool_key,
+ asyncio.Semaphore(pool_capacity - 1),
+ )
+ async with semaphore:
+ yield
+
+ @asynccontextmanager
+ async def lifecycle_lock(self, organization_id: str, phone_number: str | None = None) -> AsyncIterator[None]:
+ """Hold ordered organization and optional phone locks on one database session."""
+ lock_keys = [f"twilio-sms:organization:{organization_id}"]
+ if phone_number is not None:
+ normalized_phone_number = normalize_phone_identifier(phone_number)
+ lock_keys.append(f"twilio-sms:phone:{organization_id}:{normalized_phone_number}")
+ async with self._lifecycle_permit():
+ async with self.Session() as session:
+ bind = session.get_bind()
+ is_postgres = bind.dialect.name == "postgresql"
+ if not is_postgres:
+ acquired_locks: list[asyncio.Lock] = []
+ try:
+ for lock_key in lock_keys:
+ lock = self._local_advisory_locks[lock_key]
+ await lock.acquire()
+ acquired_locks.append(lock)
+ yield
+ finally:
+ for lock in reversed(acquired_locks):
+ lock.release()
+ return
+ lock_sql = text("SELECT pg_advisory_lock(hashtextextended(:lock_key, 0))")
+ unlock_sql = text("SELECT pg_advisory_unlock(hashtextextended(:lock_key, 0))")
+ acquired_keys: list[str] = []
+ try:
+ for lock_key in lock_keys:
+ await session.execute(lock_sql, {"lock_key": lock_key})
+ acquired_keys.append(lock_key)
+ yield
+ finally:
+ for lock_key in reversed(acquired_keys):
+ await session.execute(unlock_sql, {"lock_key": lock_key})
+
+ @asynccontextmanager
+ async def organization_lock(self, organization_id: str) -> AsyncIterator[None]:
+ """Hold the organization lifecycle lock for all Twilio mutations."""
+ async with self.lifecycle_lock(organization_id):
+ yield
+
+ @db_operation("create_sms_config")
+ async def create_sms_config(
+ self,
+ organization_id: str,
+ mode: str,
+ webhook_secret: str,
+ daily_ingest_cap: int = 100,
+ ) -> tuple[SMSConfig, str]:
+ if mode not in SMS_CONFIG_MODES:
+ raise InvalidSMSConfigMode("Invalid SMS config mode")
+ encrypted_webhook_secret = await encryptor.encrypt(webhook_secret, EncryptMethod.AES)
+ async with self.Session() as session:
+ now = naive_utc_now()
+ row = OrganizationSMSConfigModel(
+ organization_id=organization_id,
+ mode=mode,
+ encrypted_webhook_secret=encrypted_webhook_secret,
+ webhook_secret_encrypted_method=EncryptMethod.AES.value,
+ daily_ingest_cap=daily_ingest_cap,
+ created_at=now,
+ modified_at=now,
+ )
+ session.add(row)
+ try:
+ await session.commit()
+ except IntegrityError as error:
+ _hide_integrity_error_parameters(error)
+ await session.rollback()
+ if mode != "connected":
+ raise
+ winner = await session.scalar(
+ select(OrganizationSMSConfigModel).where(
+ OrganizationSMSConfigModel.organization_id == organization_id,
+ OrganizationSMSConfigModel.mode == "connected",
+ OrganizationSMSConfigModel.deleted_at.is_(None),
+ )
+ )
+ if winner is None:
+ raise
+ winner_secret = await encryptor.decrypt(
+ winner.encrypted_webhook_secret,
+ EncryptMethod(winner.webhook_secret_encrypted_method),
+ )
+ return SMSConfig.model_validate(winner), winner_secret
+ await session.refresh(row)
+ return SMSConfig.model_validate(row), webhook_secret
+
+ @db_operation("get_sms_config_with_secret")
+ async def get_sms_config_with_secret(
+ self,
+ organization_id: str,
+ sms_config_id: str,
+ ) -> tuple[SMSConfig, str] | None:
+ async with self.Session() as session:
+ row = await session.scalar(
+ select(OrganizationSMSConfigModel).where(
+ OrganizationSMSConfigModel.organization_id == organization_id,
+ OrganizationSMSConfigModel.sms_config_id == sms_config_id,
+ OrganizationSMSConfigModel.deleted_at.is_(None),
+ )
+ )
+ if row is None:
+ return None
+ webhook_secret = await encryptor.decrypt(
+ row.encrypted_webhook_secret,
+ EncryptMethod(row.webhook_secret_encrypted_method),
+ )
+ return SMSConfig.model_validate(row), webhook_secret
+
+ @db_operation("get_sms_config_with_secret_for_inbound")
+ async def get_sms_config_with_secret_for_inbound(self, sms_config_id: str) -> tuple[SMSConfig, str] | None:
+ """Only for the unauthenticated inbound webhook; this lookup is intentionally unscoped.
+
+ The webhook binds the tenant by sms_config_id plus destination number.
+ """
+ async with self.Session() as session:
+ row = await session.scalar(
+ select(OrganizationSMSConfigModel).where(
+ OrganizationSMSConfigModel.sms_config_id == sms_config_id,
+ OrganizationSMSConfigModel.deleted_at.is_(None),
+ )
+ )
+ if row is None:
+ return None
+ webhook_secret = await encryptor.decrypt(
+ row.encrypted_webhook_secret,
+ EncryptMethod(row.webhook_secret_encrypted_method),
+ )
+ return SMSConfig.model_validate(row), webhook_secret
+
+ @db_operation("get_sms_config")
+ async def get_sms_config(
+ self,
+ sms_config_id: str,
+ organization_id: str,
+ include_deleted: bool = False,
+ ) -> SMSConfig | None:
+ async with self.Session() as session:
+ query = select(OrganizationSMSConfigModel).where(
+ OrganizationSMSConfigModel.sms_config_id == sms_config_id,
+ OrganizationSMSConfigModel.organization_id == organization_id,
+ )
+ if not include_deleted:
+ query = query.where(OrganizationSMSConfigModel.deleted_at.is_(None))
+ row = await session.scalar(query)
+ return SMSConfig.model_validate(row) if row is not None else None
+
+ @db_operation("get_connected_sms_config")
+ async def get_connected_sms_config(self, organization_id: str) -> tuple[SMSConfig, str] | None:
+ async with self.Session() as session:
+ row = await session.scalar(
+ select(OrganizationSMSConfigModel)
+ .where(
+ OrganizationSMSConfigModel.organization_id == organization_id,
+ OrganizationSMSConfigModel.mode == "connected",
+ OrganizationSMSConfigModel.deleted_at.is_(None),
+ )
+ .order_by(OrganizationSMSConfigModel.created_at)
+ .limit(1)
+ )
+ if row is None:
+ return None
+ webhook_secret = await encryptor.decrypt(
+ row.encrypted_webhook_secret,
+ EncryptMethod(row.webhook_secret_encrypted_method),
+ )
+ return SMSConfig.model_validate(row), webhook_secret
+
+ @db_operation("list_sms_configs")
+ async def list_sms_configs(self, organization_id: str) -> list[SMSConfig]:
+ async with self.Session() as session:
+ rows = (
+ await session.scalars(
+ select(OrganizationSMSConfigModel)
+ .where(
+ OrganizationSMSConfigModel.organization_id == organization_id,
+ OrganizationSMSConfigModel.deleted_at.is_(None),
+ )
+ .order_by(OrganizationSMSConfigModel.created_at)
+ )
+ ).all()
+ return [SMSConfig.model_validate(row) for row in rows]
+
+ @db_operation("count_active_phone_numbers_for_config")
+ async def count_active_phone_numbers_for_config(self, sms_config_id: str, organization_id: str) -> int:
+ async with self.Session() as session:
+ count = await session.scalar(
+ select(func.count())
+ .select_from(OrganizationPhoneNumberModel)
+ .where(
+ OrganizationPhoneNumberModel.sms_config_id == sms_config_id,
+ OrganizationPhoneNumberModel.organization_id == organization_id,
+ OrganizationPhoneNumberModel.status.in_(("active", "quarantined")),
+ OrganizationPhoneNumberModel.deleted_at.is_(None),
+ )
+ )
+ return int(count or 0)
+
+ @db_operation("count_active_twilio_phone_numbers_for_config")
+ async def count_active_twilio_phone_numbers_for_config(
+ self,
+ sms_config_id: str,
+ organization_id: str,
+ ) -> int:
+ async with self.Session() as session:
+ count = await session.scalar(
+ select(func.count())
+ .select_from(OrganizationPhoneNumberModel)
+ .where(
+ OrganizationPhoneNumberModel.sms_config_id == sms_config_id,
+ OrganizationPhoneNumberModel.organization_id == organization_id,
+ OrganizationPhoneNumberModel.provider == "twilio",
+ OrganizationPhoneNumberModel.status.in_(("active", "quarantined")),
+ OrganizationPhoneNumberModel.deleted_at.is_(None),
+ )
+ )
+ return int(count or 0)
+
+ @db_operation("delete_sms_config")
+ async def delete_sms_config(self, sms_config_id: str, organization_id: str) -> None:
+ async with self.Session() as session:
+ row = await session.scalar(
+ select(OrganizationSMSConfigModel)
+ .where(
+ OrganizationSMSConfigModel.sms_config_id == sms_config_id,
+ OrganizationSMSConfigModel.organization_id == organization_id,
+ OrganizationSMSConfigModel.deleted_at.is_(None),
+ )
+ .with_for_update()
+ )
+ if row is None:
+ return
+ active_phone_id = await session.scalar(
+ select(OrganizationPhoneNumberModel.phone_number_id)
+ .where(
+ OrganizationPhoneNumberModel.sms_config_id == sms_config_id,
+ OrganizationPhoneNumberModel.organization_id == organization_id,
+ OrganizationPhoneNumberModel.status == "active",
+ OrganizationPhoneNumberModel.deleted_at.is_(None),
+ )
+ .limit(1)
+ )
+ if active_phone_id is not None:
+ raise SMSConfigHasActivePhoneNumbers("SMS config has active phone numbers")
+ row.deleted_at = naive_utc_now()
+ row.modified_at = row.deleted_at
+ await session.commit()
+
+ @db_operation("create_phone_number")
+ async def create_phone_number(
+ self,
+ organization_id: str,
+ sms_config_id: str,
+ phone_number: str,
+ provider: str = "twilio",
+ provider_number_sid: str | None = None,
+ previous_sms_url: str | None = None,
+ previous_sms_method: str | None = None,
+ previous_sms_application_sid: str | None = None,
+ credential_id: str | None = None,
+ status: str = "active",
+ ) -> OrganizationPhoneNumber:
+ if not looks_like_phone_identifier(phone_number):
+ raise ValueError("phone_number must include a country code and normalize to E.164")
+ phone_number = normalize_phone_identifier(phone_number)
+ if not is_e164_phone_number(phone_number):
+ raise ValueError("phone_number must include a country code and normalize to E.164")
+ async with self.Session() as session:
+ configs = await _lock_sms_configs(session, organization_id, {sms_config_id})
+ if sms_config_id not in configs:
+ raise ValueError("SMS config does not belong to the organization")
+ if credential_id is not None:
+ credential_exists = await session.scalar(
+ select(CredentialModel.credential_id).where(
+ CredentialModel.credential_id == credential_id,
+ CredentialModel.organization_id == organization_id,
+ CredentialModel.deleted_at.is_(None),
+ )
+ )
+ if credential_exists is None:
+ raise ValueError("Credential does not belong to the organization")
+ now = naive_utc_now()
+ row = OrganizationPhoneNumberModel(
+ organization_id=organization_id,
+ sms_config_id=sms_config_id,
+ phone_number=phone_number,
+ provider=provider,
+ provider_number_sid=provider_number_sid,
+ previous_sms_url=await _encrypt_previous_sms_url(previous_sms_url, organization_id),
+ previous_sms_method=previous_sms_method,
+ previous_sms_application_sid=previous_sms_application_sid,
+ credential_id=credential_id,
+ created_at=now,
+ modified_at=now,
+ status=status,
+ )
+ session.add(row)
+ try:
+ await session.commit()
+ except IntegrityError as error:
+ _hide_integrity_error_parameters(error)
+ await session.rollback()
+ if _is_phone_number_unique_violation(error):
+ raise PhoneNumberAlreadyExists(
+ "Phone number is already registered for this organization"
+ ) from error
+ raise
+ await session.refresh(row)
+ return OrganizationPhoneNumber.model_validate(row)
+
+ @db_operation("get_phone_number_by_number")
+ async def get_phone_number_by_number(
+ self,
+ phone_number: str,
+ organization_id: str,
+ ) -> OrganizationPhoneNumber | None:
+ if not looks_like_phone_identifier(phone_number):
+ return None
+ phone_number = normalize_phone_identifier(phone_number)
+ async with self.Session() as session:
+ row = await session.scalar(
+ select(OrganizationPhoneNumberModel)
+ .where(
+ OrganizationPhoneNumberModel.phone_number == phone_number,
+ OrganizationPhoneNumberModel.organization_id == organization_id,
+ OrganizationPhoneNumberModel.deleted_at.is_(None),
+ )
+ .order_by(
+ case((OrganizationPhoneNumberModel.status == "active", 0), else_=1),
+ OrganizationPhoneNumberModel.modified_at.desc(),
+ OrganizationPhoneNumberModel.phone_number_id.desc(),
+ )
+ )
+ return OrganizationPhoneNumber.model_validate(row) if row is not None else None
+
+ @db_operation("get_phone_number")
+ async def get_phone_number(self, phone_number_id: str, organization_id: str) -> OrganizationPhoneNumber | None:
+ async with self.Session() as session:
+ row = await session.scalar(
+ select(OrganizationPhoneNumberModel).where(
+ OrganizationPhoneNumberModel.phone_number_id == phone_number_id,
+ OrganizationPhoneNumberModel.organization_id == organization_id,
+ OrganizationPhoneNumberModel.deleted_at.is_(None),
+ )
+ )
+ return OrganizationPhoneNumber.model_validate(row) if row is not None else None
+
+ @db_operation("list_phone_numbers")
+ async def list_phone_numbers(
+ self,
+ organization_id: str,
+ sms_config_id: str | None = None,
+ include_deleted: bool = False,
+ ) -> list[OrganizationPhoneNumber]:
+ async with self.Session() as session:
+ query = select(OrganizationPhoneNumberModel).where(
+ OrganizationPhoneNumberModel.organization_id == organization_id
+ )
+ if sms_config_id is not None:
+ query = query.where(OrganizationPhoneNumberModel.sms_config_id == sms_config_id)
+ if not include_deleted:
+ query = query.where(OrganizationPhoneNumberModel.deleted_at.is_(None))
+ rows = (await session.scalars(query.order_by(OrganizationPhoneNumberModel.created_at))).all()
+ return [OrganizationPhoneNumber.model_validate(row) for row in rows]
+
+ @db_operation("update_phone_number")
+ async def update_phone_number(
+ self,
+ phone_number_id: str,
+ organization_id: str,
+ status: str | None = None,
+ provider: str | object = _UNSET,
+ sms_config_id: str | object = _UNSET,
+ provider_number_sid: str | None | object = _UNSET,
+ previous_sms_url: str | None | object = _UNSET,
+ previous_sms_method: str | None | object = _UNSET,
+ previous_sms_application_sid: str | None | object = _UNSET,
+ credential_id: str | None | object = _UNSET,
+ quarantined_until: datetime | None | object = _UNSET,
+ ) -> OrganizationPhoneNumber | None:
+ if status is not None and status not in PHONE_NUMBER_STATUSES:
+ raise InvalidPhoneNumberStatus("Invalid phone number status")
+ async with self.Session() as session:
+ row = await session.scalar(
+ select(OrganizationPhoneNumberModel)
+ .where(
+ OrganizationPhoneNumberModel.phone_number_id == phone_number_id,
+ OrganizationPhoneNumberModel.organization_id == organization_id,
+ OrganizationPhoneNumberModel.deleted_at.is_(None),
+ )
+ .with_for_update()
+ )
+ if row is None:
+ return None
+ target_sms_config_id = row.sms_config_id if sms_config_id is _UNSET else cast(str, sms_config_id)
+ configs = await _lock_sms_configs(session, organization_id, {row.sms_config_id, target_sms_config_id})
+ if target_sms_config_id not in configs:
+ raise ValueError("SMS config does not belong to the organization")
+ if credential_id is not _UNSET and credential_id is not None:
+ credential_exists = await session.scalar(
+ select(CredentialModel.credential_id).where(
+ CredentialModel.credential_id == credential_id,
+ CredentialModel.organization_id == organization_id,
+ CredentialModel.deleted_at.is_(None),
+ )
+ )
+ if credential_exists is None:
+ raise ValueError("Credential does not belong to the organization")
+ if status is not None:
+ row.status = status
+ if provider is not _UNSET:
+ row.provider = cast(str, provider)
+ if sms_config_id is not _UNSET:
+ row.sms_config_id = target_sms_config_id
+ if provider_number_sid is not _UNSET:
+ row.provider_number_sid = cast(str | None, provider_number_sid)
+ if previous_sms_url is not _UNSET:
+ row.previous_sms_url = await _encrypt_previous_sms_url(
+ cast(str | None, previous_sms_url), organization_id
+ )
+ if previous_sms_method is not _UNSET:
+ row.previous_sms_method = cast(str | None, previous_sms_method)
+ if previous_sms_application_sid is not _UNSET:
+ row.previous_sms_application_sid = cast(str | None, previous_sms_application_sid)
+ if credential_id is not _UNSET:
+ row.credential_id = cast(str | None, credential_id)
+ if quarantined_until is not _UNSET:
+ row.quarantined_until = to_naive_utc(cast(datetime | None, quarantined_until))
+ row.modified_at = naive_utc_now()
+ try:
+ await session.commit()
+ except IntegrityError as error:
+ _hide_integrity_error_parameters(error)
+ await session.rollback()
+ if _is_phone_number_unique_violation(error):
+ raise PhoneNumberAlreadyExists(
+ "Phone number is already registered for this organization"
+ ) from error
+ raise
+ await session.refresh(row)
+ return OrganizationPhoneNumber.model_validate(row)
diff --git a/skyvern/forge/sdk/db/repositories/workflow_parameters.py b/skyvern/forge/sdk/db/repositories/workflow_parameters.py
index 29df14cea..5f055a293 100644
--- a/skyvern/forge/sdk/db/repositories/workflow_parameters.py
+++ b/skyvern/forge/sdk/db/repositories/workflow_parameters.py
@@ -26,6 +26,7 @@ from skyvern.forge.sdk.db._error_handling import db_operation
from skyvern.forge.sdk.db._sentinels import _UNSET
from skyvern.forge.sdk.db.base_alchemy_db import read_with_disconnect_recovery
from skyvern.forge.sdk.db.base_repository import BaseRepository
+from skyvern.forge.sdk.db.datetime_utils import naive_utc_now, to_naive_utc
from skyvern.forge.sdk.db.exceptions import (
CopilotProposalConflictError,
DatabaseConnectionUnavailableError,
@@ -377,6 +378,7 @@ class WorkflowParametersRepository(BaseRepository):
deleted_at=parameter.deleted_at,
)
elif isinstance(parameter, BitwardenLoginCredentialParameter):
+ now = naive_utc_now()
return BitwardenLoginCredentialParameterModel(
bitwarden_login_credential_parameter_id=parameter.bitwarden_login_credential_parameter_id,
workflow_id=parameter.workflow_id,
@@ -388,7 +390,10 @@ class WorkflowParametersRepository(BaseRepository):
bitwarden_collection_id=parameter.bitwarden_collection_id,
bitwarden_item_id=parameter.bitwarden_item_id,
url_parameter_key=parameter.url_parameter_key,
- deleted_at=parameter.deleted_at,
+ totp_identifier=parameter.totp_identifier,
+ created_at=to_naive_utc(parameter.created_at) or now,
+ modified_at=to_naive_utc(parameter.modified_at) or now,
+ deleted_at=to_naive_utc(parameter.deleted_at),
)
elif isinstance(parameter, BitwardenSensitiveInformationParameter):
return BitwardenSensitiveInformationParameterModel(
@@ -431,6 +436,7 @@ class WorkflowParametersRepository(BaseRepository):
deleted_at=parameter.deleted_at,
)
elif isinstance(parameter, OnePasswordCredentialParameter):
+ now = naive_utc_now()
return OnePasswordCredentialParameterModel(
onepassword_credential_parameter_id=parameter.onepassword_credential_parameter_id,
workflow_id=parameter.workflow_id,
@@ -438,7 +444,10 @@ class WorkflowParametersRepository(BaseRepository):
description=parameter.description,
vault_id=parameter.vault_id,
item_id=parameter.item_id,
- deleted_at=parameter.deleted_at,
+ totp_identifier=parameter.totp_identifier,
+ created_at=to_naive_utc(parameter.created_at) or now,
+ modified_at=to_naive_utc(parameter.modified_at) or now,
+ deleted_at=to_naive_utc(parameter.deleted_at),
)
elif isinstance(parameter, AzureVaultCredentialParameter):
return AzureVaultCredentialParameterModel(
diff --git a/skyvern/forge/sdk/db/utils.py b/skyvern/forge/sdk/db/utils.py
index 376cc5dcd..945110c28 100644
--- a/skyvern/forge/sdk/db/utils.py
+++ b/skyvern/forge/sdk/db/utils.py
@@ -43,6 +43,8 @@ from skyvern.forge.sdk.schemas.organizations import (
BitwardenOrganizationAuthToken,
Organization,
OrganizationAuthToken,
+ TwilioCredential,
+ TwilioOrganizationAuthToken,
)
from skyvern.forge.sdk.schemas.task_v2 import TaskV2
from skyvern.forge.sdk.schemas.tasks import Task, TaskStatus
@@ -505,7 +507,7 @@ def convert_to_organization(org_model: OrganizationModel) -> Organization:
async def convert_to_organization_auth_token(
org_auth_token: OrganizationAuthTokenModel, token_type: str
-) -> OrganizationAuthToken | AzureOrganizationAuthToken | BitwardenOrganizationAuthToken:
+) -> OrganizationAuthToken | AzureOrganizationAuthToken | BitwardenOrganizationAuthToken | TwilioOrganizationAuthToken:
token = org_auth_token.token
if org_auth_token.encrypted_token and org_auth_token.encrypted_method:
token = await encryptor.decrypt(org_auth_token.encrypted_token, EncryptMethod(org_auth_token.encrypted_method))
@@ -532,6 +534,17 @@ async def convert_to_organization_auth_token(
created_at=org_auth_token.created_at,
modified_at=org_auth_token.modified_at,
)
+ elif token_type == OrganizationAuthTokenType.twilio_credential:
+ credential = TwilioCredential.model_validate_json(token)
+ return TwilioOrganizationAuthToken(
+ id=org_auth_token.id,
+ organization_id=org_auth_token.organization_id,
+ token_type=OrganizationAuthTokenType(org_auth_token.token_type),
+ credential=credential,
+ valid=org_auth_token.valid,
+ created_at=org_auth_token.created_at,
+ modified_at=org_auth_token.modified_at,
+ )
else:
return OrganizationAuthToken(
id=org_auth_token.id,
@@ -780,6 +793,7 @@ def convert_to_bitwarden_login_credential_parameter(
bitwarden_collection_id=bitwarden_login_credential_parameter_model.bitwarden_collection_id,
bitwarden_item_id=bitwarden_login_credential_parameter_model.bitwarden_item_id,
url_parameter_key=bitwarden_login_credential_parameter_model.url_parameter_key,
+ totp_identifier=bitwarden_login_credential_parameter_model.totp_identifier,
created_at=bitwarden_login_credential_parameter_model.created_at,
modified_at=bitwarden_login_credential_parameter_model.modified_at,
deleted_at=bitwarden_login_credential_parameter_model.deleted_at,
diff --git a/skyvern/forge/sdk/forge_log.py b/skyvern/forge/sdk/forge_log.py
index 59bcafa4a..9f67b4e94 100644
--- a/skyvern/forge/sdk/forge_log.py
+++ b/skyvern/forge/sdk/forge_log.py
@@ -2,12 +2,14 @@ import logging
import random
import sys
import threading
+from collections.abc import Mapping
from contextlib import contextmanager
from contextvars import ContextVar
+from copy import deepcopy
from decimal import Decimal
from pathlib import Path
from types import TracebackType
-from typing import Any, Callable, Iterator
+from typing import Any, Callable, Iterator, SupportsIndex
from weakref import WeakSet
import structlog
@@ -92,6 +94,9 @@ def _install_codeblock_fastmcp_trace_guard() -> None:
def _render_opaque_log_values(value: Any) -> Any:
+ if type(value) is _GeneratedLogValue:
+ # The code-block redactor retains its own policy for these strings.
+ return str(value)
if type(value) in (str, int, float, bool, type(None)):
return value
if type(value) is dict:
@@ -399,54 +404,100 @@ SEARCHABLE_LOG_ID_KEYS: tuple[str, ...] = (
_SEARCHABLE_ID_MAX_CHARS = 256
+class _GeneratedLogValue(str):
+ """Per-event provenance that survives shallow formatter copies and JSON encoding."""
+
+ field: str
+ parts: tuple[tuple[str, bool], ...]
+
+ def __new__(cls, field: str, parts: tuple[tuple[str, bool], ...]) -> "_GeneratedLogValue":
+ value = super().__new__(cls, "".join(text for text, _generated in parts))
+ value.field = field
+ value.parts = parts
+ return value
+
+ def scrub_caller_text(self, scrub: Callable[[str], str]) -> "_GeneratedLogValue":
+ return _GeneratedLogValue(
+ self.field, tuple((text if generated else scrub(text), generated) for text, generated in self.parts)
+ )
+
+ def __reduce__(self) -> tuple[Any, ...]:
+ return type(self), (self.field, self.parts)
+
+ def __getitem__(self, key: SupportsIndex | slice) -> str:
+ if not isinstance(key, slice) or key.step not in (None, 1):
+ return super().__getitem__(key)
+ start, stop, _ = key.indices(len(self))
+ offset = 0
+ parts = []
+ for text, generated in self.parts:
+ end = offset + len(text)
+ if offset < stop and end > start:
+ parts.append((text[max(0, start - offset) : stop - offset], generated))
+ offset = end
+ return _GeneratedLogValue(self.field, tuple(parts))
+
+
+class _LogTimeStamper(structlog.processors.TimeStamper):
+ def __call__(self, logger: Any, method_name: str, event_dict: EventDict) -> EventDict:
+ event_dict = super().__call__(logger, method_name, event_dict)
+ event_dict[self.key] = _GeneratedLogValue(self.key, ((event_dict[self.key], True),))
+ return event_dict
+
+
+# These context fields come from authenticated/persisted entities: org_auth_service,
+# workflow/service, agent.execute_step, and script_service's task/step creation.
+# run_id carries a parent execution identity. request_id originates in API middleware.
+# Context membership alone is insufficient: organization_name, Copilot chat_id,
+# browser_session_id, and browser routing values can come from caller input.
+_GENERATED_CONTEXT_ID_KEYS = frozenset(
+ {
+ "request_id",
+ "organization_id",
+ "step_id",
+ "task_id",
+ "run_id",
+ "workflow_id",
+ "workflow_run_id",
+ "workflow_permanent_id",
+ "task_v2_id",
+ }
+)
+
+
def add_log_context(logger: logging.Logger, method_name: str, event_dict: EventDict) -> EventDict:
"""Add request and process context, appending only the correlation ids to ``msg``."""
- # Add context to the log
+ context_fields: EventDict = {key: event_dict[key] for key in SEARCHABLE_LOG_ID_KEYS if key in event_dict}
+ context_fields.update(env=settings.ENV, version=__version__)
context = skyvern_context.current()
if context:
- if getattr(context, "request_id", None):
- event_dict["request_id"] = context.request_id
- if getattr(context, "organization_id", None):
- event_dict["organization_id"] = context.organization_id
- if getattr(context, "organization_name", None):
- event_dict["organization_name"] = context.organization_name
- if getattr(context, "step_id", None):
- event_dict["step_id"] = context.step_id
- if getattr(context, "task_id", None):
- event_dict["task_id"] = context.task_id
- if getattr(context, "run_id", None):
- event_dict["run_id"] = context.run_id
- if getattr(context, "workflow_id", None):
- event_dict["workflow_id"] = context.workflow_id
- if getattr(context, "workflow_run_id", None):
- event_dict["workflow_run_id"] = context.workflow_run_id
- if getattr(context, "workflow_permanent_id", None):
- event_dict["workflow_permanent_id"] = context.workflow_permanent_id
- if getattr(context, "task_v2_id", None):
- event_dict["task_v2_id"] = context.task_v2_id
- if getattr(context, "browser_session_id", None):
- event_dict["browser_session_id"] = context.browser_session_id
- if getattr(context, "copilot_session_id", None):
- event_dict["copilot_session_id"] = context.copilot_session_id
- if getattr(context, "codeblock_execution_path", None):
- event_dict["codeblock_execution_path"] = context.codeblock_execution_path
- if getattr(context, "browser_container_ip", None):
- event_dict["browser_container_ip"] = context.browser_container_ip
- if getattr(context, "browser_container_task_arn", None):
- event_dict["browser_container_task_arn"] = context.browser_container_task_arn
-
- # Add process-level context to the log
- event_dict["env"] = settings.ENV
- event_dict["version"] = __version__
-
- searchable_ids = [
- f"{key}={value[:_SEARCHABLE_ID_MAX_CHARS]}"
- for key in SEARCHABLE_LOG_ID_KEYS
- if isinstance((value := event_dict.get(key)), str) and value
- ]
+ for key in (*SEARCHABLE_LOG_ID_KEYS, "codeblock_execution_path"):
+ value = getattr(context, key, None)
+ if value:
+ context_fields[key] = (
+ _GeneratedLogValue(key, ((value, True),))
+ if key in _GENERATED_CONTEXT_ID_KEYS and type(value) is str
+ else value
+ )
+ # Scrub complete caller values before slicing the searchable suffix. Replace
+ # their original keys too: masking can change a key's spelling.
+ event_dict = {key: value for key, value in event_dict.items() if key not in context_fields}
+ event_dict.update(redact_registered_secrets(logger, method_name, context_fields))
+
+ searchable_ids: list[tuple[str, bool]] = []
+ for key in SEARCHABLE_LOG_ID_KEYS:
+ value = event_dict.get(key)
+ if isinstance(value, str) and value:
+ generated = type(value) is _GeneratedLogValue and value.field == key
+ searchable_ids.append((f"{key}={value[:_SEARCHABLE_ID_MAX_CHARS]}", generated))
msg = event_dict.get("msg")
if searchable_ids and isinstance(msg, str):
- event_dict["msg"] = f"{msg} | {', '.join(searchable_ids)}"
+ parts = [(str(msg), False), (" | ", True)]
+ for index, part in enumerate(searchable_ids):
+ if index:
+ parts.append((", ", True))
+ parts.append(part)
+ event_dict["msg"] = _GeneratedLogValue("msg", tuple(parts))
return event_dict
@@ -524,35 +575,149 @@ def render_bounded_json(logger: logging.Logger, method_name: str, event_dict: Ev
)
-def redact_registered_secrets(logger: logging.Logger, method_name: str, event_dict: EventDict) -> EventDict:
- """Redact credential values the copilot filled during a turn from every string in the event dict.
-
- Imported lazily: this module is imported far earlier in boot than the copilot package.
- """
- from skyvern.forge.sdk.copilot.secret_scrub import REDACTED_SECRET_PLACEHOLDER, all_registered_secret_values
-
- secrets = all_registered_secret_values()
- if not secrets:
- return event_dict
+def _registered_secret_scrubber(
+ *, logging_envelope: bool = False, protocol_level: str | None = None
+) -> Callable[[Any, int], Any] | None:
+ # Logging starts before the application exists. Import only the holder and the
+ # existing scrub helpers; never initialize the app or log from this processor.
+ from skyvern.forge.sdk.copilot.secret_scrub import (
+ REDACTED_SECRET_PLACEHOLDER,
+ all_registered_secret_values,
+ encoded_secret_variants,
+ )
- def scrub(node: Any) -> Any:
- # Structured nested values are rendered in full, so recurse before JSON serialization.
+ variants = set(all_registered_secret_values())
+ registered: set[str] = set()
+ context = skyvern_context.current()
+ if context is not None:
+ registered.update(getattr(context, "runtime_secret_values", ()))
+ workflow_run_id = getattr(context, "workflow_run_id", None)
+ if workflow_run_id:
+ from skyvern.forge import app
+
+ manager = None
+ try:
+ manager = app.WORKFLOW_CONTEXT_MANAGER
+ except (AttributeError, RuntimeError):
+ pass
+ if manager is not None:
+ registered.update(
+ manager.get_secret_values_for_run(workflow_run_id, respect_artifact_redaction_flag=False)
+ )
+ run_context = manager.workflow_run_contexts.get(workflow_run_id)
+ if run_context is not None:
+ # Artifact collection filters short credentials. Diagnostic
+ # logs must protect every nonempty registered string instead.
+ registered.update(
+ value for value in run_context.secrets.values() if isinstance(value, str) and value
+ )
+ for value in registered:
+ if isinstance(value, str) and value:
+ variants.update(encoded_secret_variants(value))
+ if not variants:
+ return None
+ secrets = sorted(variants, key=len, reverse=True)
+ seen: set[int] = set()
+ remaining = 10_000
+
+ def scrub(node: Any, depth: int = 0) -> Any:
+ nonlocal remaining
+ remaining -= 1
+ if remaining < 0:
+ raise ValueError("Log redaction node limit exceeded")
+ if depth > 20:
+ return REDACTED_SECRET_PLACEHOLDER
if isinstance(node, str):
for secret in secrets:
- if secret in node:
- node = node.replace(secret, REDACTED_SECRET_PLACEHOLDER)
+ node = node.replace(secret, REDACTED_SECRET_PLACEHOLDER)
+ return node
+ if node is None or isinstance(node, bool):
return node
- if isinstance(node, dict):
- return {key: scrub(item) for key, item in node.items()}
+ if isinstance(node, (int, float, Decimal)):
+ # Keep ordinary numbers numeric. Decimal is emitted as a float by
+ # the JSON renderer, so check that representation as well.
+ text = str(node)
+ if isinstance(node, Decimal):
+ text += " " + str(float(node))
+ return REDACTED_SECRET_PLACEHOLDER if any(secret in text for secret in secrets) else node
+
+ marker = id(node)
+ if marker in seen:
+ return REDACTED_SECRET_PLACEHOLDER
+ seen.add(marker)
+ if isinstance(node, Mapping):
+ result = {}
+ for key, item in node.items():
+ # These exact root names belong to the logging envelope:
+ # EventRenamer and renderers consume them. Their spelling
+ # can coincide with a short secret; their data still cannot.
+ # No nested/model mapping inherits this structural exemption.
+ protocol_field = (
+ logging_envelope and depth == 0 and type(key) is str and key in ("event", "msg", "level")
+ )
+ generated_field = depth == 0 and type(item) is _GeneratedLogValue and item.field == key
+ output_key = key if protocol_field or generated_field else scrub(key, depth + 1)
+ if generated_field:
+ result[output_key] = item.scrub_caller_text(lambda text: scrub(text, depth + 1))
+ elif protocol_field and key == "level" and type(item) is str and item == protocol_level:
+ result[output_key] = protocol_level
+ else:
+ result[output_key] = scrub(item, depth + 1)
+ return result
if isinstance(node, list):
- return [scrub(item) for item in node]
+ return [scrub(item, depth + 1) for item in node]
if isinstance(node, tuple):
- return tuple(scrub(item) for item in node)
- return node
+ return tuple(scrub(item, depth + 1) for item in node)
+ if isinstance(node, (set, frozenset)):
+ return type(node)(scrub(item, depth + 1) for item in node)
+ # Freeze opaque renderable values before context teardown. Calling the
+ # renderer later could expose a secret through repr or __structlog__.
+ return scrub(_json_log_default(node), depth + 1)
+
+ return scrub
+
+
+def redact_registered_log_payload(body: Any, attributes: Mapping[str, Any]) -> tuple[Any, dict[str, Any]]:
+ """Copy both export payloads using one collection; propagate failures to the export boundary."""
+ scrub = _registered_secret_scrubber()
+ if scrub is None:
+ safe_body, safe_attributes = deepcopy((body, dict(attributes)))
+ else:
+ safe_body = (
+ body.scrub_caller_text(lambda text: scrub(text, 1))
+ if type(body) is _GeneratedLogValue and body.field == "msg"
+ else scrub(body, 1)
+ )
+ safe_attributes = scrub(attributes, 0)
+ return (
+ str(safe_body) if isinstance(safe_body, str) else safe_body,
+ {key: str(value) if type(value) is _GeneratedLogValue else value for key, value in safe_attributes.items()},
+ )
- for key, value in list(event_dict.items()):
- event_dict[key] = scrub(value)
- return event_dict
+
+def redact_registered_secrets(logger: logging.Logger, method_name: str, event_dict: EventDict) -> EventDict:
+ """Copy and scrub normalized diagnostics using existing registrations, regardless of artifact policy."""
+ from skyvern.forge.sdk.copilot.secret_scrub import REDACTED_SECRET_PLACEHOLDER
+
+ # Only a known logging method establishes severity provenance. Caller data
+ # under a nested "level" key (or an unexpected root value) is still scrubbed.
+ protocol_level = None
+ if method_name in ("debug", "info", "warning", "warn", "error", "exception", "critical", "fatal", "notset"):
+ protocol_level = structlog.stdlib.add_log_level(logger, method_name, {})["level"]
+
+ try:
+ scrub = _registered_secret_scrubber(logging_envelope=True, protocol_level=protocol_level)
+ return event_dict if scrub is None else scrub(event_dict, 0)
+ except Exception:
+ # A failed collection, iterator, or renderer must never return raw data.
+ # Keep both message slots if present: downstream EventRenamer still needs
+ # "event" even when a caller also supplied "msg". Severity comes only from
+ # the logging method, never from the failed traversal's data.
+ message_keys = [key for key in ("event", "msg") if key in event_dict] or ["event"]
+ fallback = dict.fromkeys(message_keys, REDACTED_SECRET_PLACEHOLDER)
+ if "level" in event_dict and protocol_level is not None:
+ fallback["level"] = protocol_level
+ return fallback
def redact_codeblock_parameters(logger: logging.Logger, method_name: str, event_dict: EventDict) -> EventDict:
@@ -573,7 +738,9 @@ def redact_bearer_tokens(logger: logging.Logger, method_name: str, event_dict: E
by ``redact_sensitive_event_fields`` below, which recurses into their strings.
"""
for key, value in list(event_dict.items()):
- if isinstance(value, str):
+ if type(value) is _GeneratedLogValue and value.field == key:
+ event_dict[key] = value.scrub_caller_text(redact_bearer_tokens_in_text)
+ elif isinstance(value, str):
event_dict[key] = redact_bearer_tokens_in_text(value)
return event_dict
@@ -1012,7 +1179,6 @@ def setup_logger() -> None:
additional_processors = (
[
redact_bearer_tokens,
- redact_registered_secrets,
# After compaction: that pass is a log-volume control that trims Action
# models down to a few fields, and redaction would otherwise expand them
# into full dicts before it ran.
@@ -1034,10 +1200,10 @@ def setup_logger() -> None:
if settings.JSON_LOGGING
else [
redact_bearer_tokens,
- redact_registered_secrets,
compact_action_objects,
redact_sensitive_event_fields,
redact_codeblock_parameters,
+ add_log_context,
structlog.processors.CallsiteParameterAdder(
{
structlog.processors.CallsiteParameter.FILENAME,
@@ -1054,19 +1220,26 @@ def setup_logger() -> None:
logger_factory=structlog.stdlib.LoggerFactory(),
processors=[
structlog.stdlib.add_log_level,
- structlog.processors.TimeStamper(fmt="iso"),
+ _LogTimeStamper(fmt="iso"),
_add_entrypoint,
add_error_processor,
structlog.processors.format_exc_info,
]
+ additional_processors
- + [skyvern_logs_processor, sample_logs_processor, structlog.stdlib.ProcessorFormatter.wrap_for_formatter],
+ + [
+ # Models and containers must be normalized before value scrubbing;
+ # capture only copied, scrubbed values for later artifact serialization.
+ redact_registered_secrets,
+ skyvern_logs_processor,
+ sample_logs_processor,
+ structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
+ ],
)
# Foreign stdlib records never run the structlog chain above, so without these two a
# record reaches Datadog with an empty message (its remapper reads `msg`, not `event`)
# and no `organization_id` to group on.
foreign_msg_chain: list[Processor] = (
- [structlog.processors.EventRenamer("msg"), add_log_context] if settings.JSON_LOGGING else []
+ [structlog.processors.EventRenamer("msg"), add_log_context] if settings.JSON_LOGGING else [add_log_context]
)
handler = logging.StreamHandler()
@@ -1086,7 +1259,7 @@ def setup_logger() -> None:
structlog.stdlib.add_log_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.ProcessorFormatter.remove_processors_meta,
- structlog.processors.TimeStamper(fmt="iso"),
+ _LogTimeStamper(fmt="iso"),
# Every record on this handler — native structlog AND foreign stdlib (temporal,
# asyncio, sqlalchemy, uvicorn) — is serialized here, so this is the one seam that
# covers both. `format_exc_info` in `foreign_pre_chain` has already rendered
diff --git a/skyvern/forge/sdk/routes/credentials.py b/skyvern/forge/sdk/routes/credentials.py
index 445fd38a0..4fb5216fe 100644
--- a/skyvern/forge/sdk/routes/credentials.py
+++ b/skyvern/forge/sdk/routes/credentials.py
@@ -8,7 +8,7 @@ credit card billing/contact fields, credit card metadata, or secret values)
in any API response. The only fields that may be returned are non-sensitive
metadata:
- - Password credentials: ``username``, ``totp_type``, ``totp_identifier``
+ - Password credentials: ``username``, ``totp_type``, ``totp_identifier``, ``has_totp``
- Credit card credentials: ``last_four``, ``brand``
- Secret credentials: ``secret_label``
@@ -3468,6 +3468,7 @@ def _convert_to_response(credential: Credential) -> CredentialResponse:
username=credential.username or credential.credential_id,
totp_type=credential.totp_type,
totp_identifier=credential.totp_identifier,
+ has_totp=bool(credential.has_totp_seed),
)
return CredentialResponse(
credential=credential_response,
diff --git a/skyvern/forge/sdk/routes/run_blocks.py b/skyvern/forge/sdk/routes/run_blocks.py
index 03fce06a3..4323c1de6 100644
--- a/skyvern/forge/sdk/routes/run_blocks.py
+++ b/skyvern/forge/sdk/routes/run_blocks.py
@@ -215,13 +215,14 @@ async def login(
yaml_parameters = [
BitwardenLoginCredentialParameterYAML(
key=parameter_key,
- collection_id=login_request.bitwarden_collection_id,
- item_id=login_request.bitwarden_item_id,
- url=login_request.url,
+ bitwarden_collection_id=login_request.bitwarden_collection_id,
+ bitwarden_item_id=login_request.bitwarden_item_id,
+ url_parameter_key=login_request.url,
description="The ID of the bitwarden collection to use for login",
bitwarden_client_id_aws_secret_key="SKYVERN_BITWARDEN_CLIENT_ID",
bitwarden_client_secret_aws_secret_key="SKYVERN_BITWARDEN_CLIENT_SECRET",
bitwarden_master_password_aws_secret_key="SKYVERN_BITWARDEN_MASTER_PASSWORD",
+ totp_identifier=resolved_totp_identifier,
)
]
elif login_request.credential_type == CredentialType.onepassword:
@@ -239,6 +240,7 @@ async def login(
vault_id=login_request.onepassword_vault_id,
item_id=login_request.onepassword_item_id,
totp_field_name=login_request.onepassword_totp_field_name,
+ totp_identifier=resolved_totp_identifier,
)
]
elif login_request.credential_type == CredentialType.azure_vault:
diff --git a/skyvern/forge/sdk/routes/sms_inbound.py b/skyvern/forge/sdk/routes/sms_inbound.py
new file mode 100644
index 000000000..b9d72dfa1
--- /dev/null
+++ b/skyvern/forge/sdk/routes/sms_inbound.py
@@ -0,0 +1,185 @@
+import hmac
+from typing import Annotated
+
+import structlog
+from fastapi import APIRouter, BackgroundTasks, Header, HTTPException, Query, Request, Response, status
+
+from skyvern.config import settings
+from skyvern.forge import app
+from skyvern.forge.sdk.db.datetime_utils import naive_utc_now
+from skyvern.forge.sdk.db.enums import OrganizationAuthTokenType
+from skyvern.forge.sdk.schemas.totp_codes import OTPType
+from skyvern.forge.sdk.services.twilio_service import build_inbound_sms_url, validate_twilio_signature
+from skyvern.services.otp_service import parse_otp_login, redact_otp_identifier_for_log
+from skyvern.utils.phone_validation import normalize_phone_identifier
+
+LOG = structlog.get_logger()
+
+sms_inbound_router = APIRouter()
+
+SMS_SIGNING_URL_UNAVAILABLE = "SMS signing URL is unavailable"
+
+_EMPTY_TWIML = '<?xml version="1.0" encoding="UTF-8"?><Response></Response>'
+_TWILIO_SMS_BODY_MAX_LENGTH = 1600
+
+
+def _empty_twiml_response() -> Response:
+ return Response(content=_EMPTY_TWIML, media_type="text/xml")
+
+
+async def _parse_and_promote(organization_id: str, totp_code_id: str, content: str) -> None:
+ try:
+ otp_value = await parse_otp_login(content, organization_id)
+ if otp_value is None:
+ return
+ await app.DATABASE.otp.promote_raw_otp_code(
+ totp_code_id=totp_code_id,
+ organization_id=organization_id,
+ code=otp_value.value,
+ otp_type=otp_value.get_otp_type(),
+ )
+ except Exception as exc:
+ LOG.warning(
+ "Failed to parse and promote inbound SMS OTP",
+ organization_id=organization_id,
+ totp_code_id=totp_code_id,
+ exception_type=type(exc).__name__,
+ )
+
+
+@sms_inbound_router.post("/inbound/{sms_config_id}")
+async def receive_inbound_sms(
+ sms_config_id: str,
+ request: Request,
+ background_tasks: BackgroundTasks,
+ token: Annotated[str | None, Query()] = None,
+ twilio_signature: Annotated[str | None, Header(alias="X-Twilio-Signature")] = None,
+) -> Response:
+ config_with_secret = await app.DATABASE.sms.get_sms_config_with_secret_for_inbound(sms_config_id=sms_config_id)
+ if config_with_secret is None:
+ raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="SMS configuration not found")
+
+ sms_config, webhook_secret = config_with_secret
+ if not hmac.compare_digest(token or "", webhook_secret):
+ raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Invalid webhook token")
+
+ if sms_config.mode == "managed":
+ LOG.warning(
+ "Rejecting managed inbound SMS until managed signing credentials are persisted",
+ organization_id=sms_config.organization_id,
+ sms_config_id=sms_config.sms_config_id,
+ )
+ raise HTTPException(
+ status_code=status.HTTP_403_FORBIDDEN,
+ detail="Managed SMS signing token is unavailable",
+ )
+
+ form = await request.form()
+ form_params = {key: value if isinstance(value, str) else str(value) for key, value in form.items()}
+
+ if sms_config.mode == "connected":
+ twilio_token = await app.DATABASE.organizations.get_valid_org_auth_token(
+ sms_config.organization_id,
+ OrganizationAuthTokenType.twilio_credential.value,
+ )
+ auth_token = twilio_token.credential.auth_token if twilio_token is not None else None
+ if not auth_token:
+ LOG.warning(
+ "Rejecting inbound SMS because the connected Twilio auth token is unavailable",
+ organization_id=sms_config.organization_id,
+ sms_config_id=sms_config.sms_config_id,
+ )
+ raise HTTPException(
+ status_code=status.HTTP_403_FORBIDDEN,
+ detail="Connected SMS signing token is unavailable",
+ )
+
+ base_url = settings.SKYVERN_BASE_URL.strip()
+ if not base_url:
+ LOG.error(
+ "Connected inbound SMS requires SKYVERN_BASE_URL for signature verification",
+ organization_id=sms_config.organization_id,
+ sms_config_id=sms_config.sms_config_id,
+ )
+ raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail=SMS_SIGNING_URL_UNAVAILABLE)
+ canonical_url = build_inbound_sms_url(base_url, sms_config_id, webhook_secret)
+ if not validate_twilio_signature(auth_token, canonical_url, form_params, twilio_signature or ""):
+ LOG.warning(
+ "Rejecting inbound SMS with invalid Twilio signature",
+ organization_id=sms_config.organization_id,
+ sms_config_id=sms_config.sms_config_id,
+ )
+ raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid Twilio signature")
+
+ to_identifier = normalize_phone_identifier(form_params.get("To", ""))
+ redacted_identifier = redact_otp_identifier_for_log(to_identifier)
+ phone_number = await app.DATABASE.sms.get_phone_number_by_number(
+ to_identifier,
+ sms_config.organization_id,
+ )
+ if (
+ phone_number is None
+ or phone_number.organization_id != sms_config.organization_id
+ or phone_number.sms_config_id != sms_config.sms_config_id
+ or phone_number.status not in {"active", "releasing"}
+ ):
+ LOG.info(
+ "Dropping SMS for unregistered number",
+ organization_id=sms_config.organization_id,
+ totp_identifier=redacted_identifier,
+ )
+ return _empty_twiml_response()
+ external_message_id = form_params.get("MessageSid")
+ if not external_message_id:
+ raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="MessageSid is required")
+
+ utc_midnight = naive_utc_now().replace(hour=0, minute=0, second=0, microsecond=0)
+ daily_count = await app.DATABASE.otp.count_otp_codes_since(
+ sms_config.organization_id,
+ to_identifier,
+ created_after=utc_midnight,
+ source="twilio",
+ )
+ # Count-then-insert is not atomic; bounded overshoot under burst is accepted (see PR review).
+ if daily_count >= sms_config.daily_ingest_cap:
+ LOG.warning(
+ "Dropping SMS because the daily ingest cap was reached",
+ organization_id=sms_config.organization_id,
+ totp_identifier=redacted_identifier,
+ )
+ return _empty_twiml_response()
+
+ body = form_params.get("Body", "")
+ stripped_body = body.strip()
+ content = body[:_TWILIO_SMS_BODY_MAX_LENGTH]
+
+ if stripped_body.isdigit() and len(stripped_body) <= 10:
+ otp_row = await app.DATABASE.otp.create_otp_code_if_new(
+ organization_id=sms_config.organization_id,
+ totp_identifier=to_identifier,
+ content=content,
+ code=stripped_body,
+ otp_type=OTPType.TOTP,
+ source="twilio",
+ external_message_id=external_message_id,
+ )
+ if otp_row is None:
+ return _empty_twiml_response()
+ else:
+ raw_row = await app.DATABASE.otp.create_raw_otp_code_if_new(
+ organization_id=sms_config.organization_id,
+ totp_identifier=to_identifier,
+ content=content,
+ source="twilio",
+ external_message_id=external_message_id,
+ )
+ if raw_row is None:
+ return _empty_twiml_response()
+ background_tasks.add_task(
+ _parse_and_promote,
+ sms_config.organization_id,
+ raw_row.totp_code_id,
+ content,
+ )
+
+ return _empty_twiml_response()
diff --git a/skyvern/forge/sdk/routes/twilio_integration.py b/skyvern/forge/sdk/routes/twilio_integration.py
new file mode 100644
index 000000000..ddca0ba25
--- /dev/null
+++ b/skyvern/forge/sdk/routes/twilio_integration.py
@@ -0,0 +1,925 @@
+import asyncio
+import secrets
+from collections.abc import AsyncIterator
+from contextlib import asynccontextmanager
+from typing import Annotated, Any
+
+import structlog
+from fastapi import APIRouter, Depends, HTTPException
+from pydantic import BaseModel, ConfigDict, model_validator
+
+from skyvern.config import settings
+from skyvern.forge import app
+from skyvern.forge.sdk.db.enums import OrganizationAuthTokenType
+from skyvern.forge.sdk.encrypt.base import EncryptMethod
+from skyvern.forge.sdk.routes.sms_inbound import SMS_SIGNING_URL_UNAVAILABLE
+from skyvern.forge.sdk.schemas.organizations import (
+ CreateTwilioCredentialRequest,
+ Organization,
+ TwilioCredential,
+ TwilioCredentialSafe,
+ _TwilioJSONModel,
+ _validate_twilio_without_error_inputs,
+)
+from skyvern.forge.sdk.schemas.sms import (
+ CreateSMSConfigRequest,
+ DisablePhoneNumberRequest,
+ EnablePhoneNumberRequest,
+ OrganizationPhoneNumber,
+ RegisterManualPhoneNumberRequest,
+ SMSConfig,
+ SMSConfigCreated,
+ TwilioPhoneNumberInfo,
+)
+from skyvern.forge.sdk.services import org_auth_service
+from skyvern.forge.sdk.services.twilio_service import (
+ TwilioApiError,
+ build_client_from_credential,
+ build_inbound_sms_url,
+)
+from skyvern.forge.sdk.settings_manager import SettingsManager
+from skyvern.forge.sdk.workflow.secret_encryption import decrypt_secret_field_value, is_encrypted_secret
+from skyvern.utils.phone_validation import is_e164_phone_number, normalize_phone_identifier
+from skyvern.utils.url_validators import redact_url_for_display, redact_url_query
+
+twilio_integration_router = APIRouter()
+LOG = structlog.get_logger()
+
+
+class OrganizationPhoneNumberResponse(BaseModel):
+ """Safe phone-number metadata without provider routing or cost secrets."""
+
+ model_config = ConfigDict(from_attributes=True)
+
+ phone_number_id: str
+ organization_id: str
+ sms_config_id: str
+ phone_number: str
+ provider: str
+ provider_number_sid: str | None = None
+ credential_id: str | None = None
+ price_cents: int | None = None
+ status: str
+ quarantined_until: Any | None = None
+ created_at: Any
+ modified_at: Any
+ deleted_at: Any | None = None
+
+
+class TwilioCredentialResponse(_TwilioJSONModel):
+ model_config = ConfigDict(hide_input_in_errors=True)
+
+ credential: TwilioCredentialSafe | None = None
+
+ _hide_validation_inputs = model_validator(mode="wrap")(_validate_twilio_without_error_inputs)
+
+
+def _require_twilio_sms_enabled() -> None:
+ if not SettingsManager.get_settings().TWILIO_SMS_2FA_ENABLED:
+ raise HTTPException(status_code=404, detail="Twilio SMS 2FA is not available")
+
+
+def _twilio_api_http_exception(error: TwilioApiError) -> HTTPException:
+ if error.status_code in {401, 403}:
+ return HTTPException(status_code=400, detail="Invalid Twilio credentials")
+ if error.status_code == 404:
+ return HTTPException(status_code=404, detail="Phone number not found on Twilio account")
+ if error.status_code == 429:
+ return HTTPException(status_code=503, detail="Twilio rate limit, retry later")
+ return HTTPException(status_code=502, detail="Twilio API error")
+
+
+def _safe_credential(credential: TwilioCredential) -> TwilioCredentialResponse:
+ return TwilioCredentialResponse(
+ credential=TwilioCredentialSafe(
+ account_sid=credential.account_sid,
+ api_key_sid=credential.api_key_sid,
+ has_auth_token=bool(credential.auth_token),
+ )
+ )
+
+
+async def _get_credential(organization_id: str) -> TwilioCredential:
+ token = await app.DATABASE.organizations.get_valid_org_auth_token(
+ organization_id,
+ OrganizationAuthTokenType.twilio_credential.value,
+ )
+ if token is None:
+ raise HTTPException(status_code=404, detail="Twilio credentials not found")
+ return token.credential
+
+
+async def _validate_twilio_credential(credential: TwilioCredential) -> None:
+ clients = [build_client_from_credential(credential)]
+ if credential.auth_token and credential.api_key_sid:
+ clients.append(
+ build_client_from_credential(
+ TwilioCredential(
+ account_sid=credential.account_sid,
+ auth_token=credential.auth_token,
+ )
+ )
+ )
+ try:
+ for client in clients:
+ await client.get_account()
+ except TwilioApiError as error:
+ raise _twilio_api_http_exception(error) from None
+
+
+def _sms_capable(number: dict[str, Any]) -> bool:
+ if "sms_capable" in number:
+ return bool(number["sms_capable"])
+ capabilities = number.get("capabilities") or {}
+ return bool(capabilities.get("SMS", capabilities.get("sms", False)))
+
+
+def _current_sms_url(number: dict[str, Any]) -> str | None:
+ value = number.get("sms_url")
+ return str(value) if value else None
+
+
+def _current_sms_method(number: dict[str, Any]) -> str | None:
+ value = number.get("sms_method")
+ return str(value) if value else None
+
+
+def _current_sms_application_sid(number: dict[str, Any]) -> str | None:
+ value = number.get("sms_application_sid")
+ return str(value) if value else None
+
+
+def _routing_is_ours(
+ *,
+ current_sms_url: str | None,
+ current_sms_method: str | None,
+ current_sms_application_sid: str | None,
+ inbound_url: str,
+) -> bool:
+ return (
+ current_sms_url == inbound_url
+ and (current_sms_method is None or current_sms_method.upper() == "POST")
+ and not current_sms_application_sid
+ )
+
+
+async def _get_registered_phone_number(phone_number: str, organization_id: str) -> OrganizationPhoneNumber | None:
+ return await app.DATABASE.sms.get_phone_number_by_number(phone_number, organization_id)
+
+
+def _require_twilio_provider(phone_number: OrganizationPhoneNumber | None) -> None:
+ if phone_number is not None and phone_number.provider != "twilio":
+ raise HTTPException(
+ status_code=409,
+ detail="Skyvern-managed numbers must be released from the Skyvern Numbers integration",
+ )
+
+
+def _require_sms_base_url() -> None:
+ if not settings.SKYVERN_BASE_URL.strip():
+ raise HTTPException(status_code=503, detail=SMS_SIGNING_URL_UNAVAILABLE)
+
+
+def _inbound_url(sms_config_id: str, webhook_secret: str) -> str:
+ return build_inbound_sms_url(settings.SKYVERN_BASE_URL, sms_config_id, webhook_secret)
+
+
+async def _load_sms_capable_provider_number(
+ client: Any,
+ phone_number_sid: str,
+) -> tuple[dict[str, Any], str]:
+ try:
+ provider_number = await client.get_incoming_phone_number(phone_number_sid)
+ except TwilioApiError as error:
+ raise _twilio_api_http_exception(error) from None
+ if not _sms_capable(provider_number):
+ raise HTTPException(status_code=400, detail="Phone number is not SMS-capable")
+ phone_number = normalize_phone_identifier(str(provider_number.get("phone_number") or ""))
+ if not is_e164_phone_number(phone_number):
+ raise HTTPException(status_code=502, detail="Twilio returned an invalid phone number")
+ return provider_number, phone_number
+
+
+async def _active_connected_phone_count(organization_id: str) -> int:
+ connected_config = await app.DATABASE.sms.get_connected_sms_config(organization_id)
+ if connected_config is None:
+ return 0
+ config, _ = connected_config
+ return await app.DATABASE.sms.count_active_twilio_phone_numbers_for_config(
+ config.sms_config_id,
+ organization_id,
+ )
+
+
+async def _shielded(awaitable: Any) -> Any:
+ task = asyncio.ensure_future(awaitable)
+ while not task.done():
+ try:
+ await asyncio.shield(task)
+ except asyncio.CancelledError:
+ continue
+ return task.result()
+
+
+async def _run_lifecycle(awaitable: Any) -> Any:
+ """Finish a lifecycle child task before propagating cancellation to its caller."""
+ child = asyncio.create_task(awaitable)
+ cancellation_requested = False
+ child_error: BaseException | None = None
+ result: Any = None
+ try:
+ result = await asyncio.shield(child)
+ except asyncio.CancelledError:
+ cancellation_requested = True
+ while not child.done():
+ try:
+ result = await asyncio.shield(child)
+ except asyncio.CancelledError:
+ cancellation_requested = True
+ except BaseException as error:
+ child_error = error
+ break
+ if child_error is None and child.done() and not child.cancelled():
+ child_error = child.exception()
+ if cancellation_requested:
+ if child_error is not None and not isinstance(child_error, asyncio.CancelledError):
+ LOG.warning("Twilio SMS lifecycle failed while caller cancellation was pending", error=repr(child_error))
+ raise asyncio.CancelledError
+ if child_error is not None:
+ raise child_error
+ return result
+
+
+async def _compensate_phone_number_state(
+ *,
+ organization_id: str,
+ phone_number_id: str,
+ registered: OrganizationPhoneNumber | None,
+) -> None:
+ if registered is None:
+ fields: dict[str, Any] = {"status": "disabled", "quarantined_until": None}
+ else:
+ fields = {
+ "status": registered.status,
+ "sms_config_id": registered.sms_config_id,
+ "provider_number_sid": registered.provider_number_sid,
+ "previous_sms_url": registered.previous_sms_url,
+ "previous_sms_method": registered.previous_sms_method,
+ "previous_sms_application_sid": registered.previous_sms_application_sid,
+ "credential_id": registered.credential_id,
+ "quarantined_until": None,
+ }
+ try:
+ result = await app.DATABASE.sms.update_phone_number(phone_number_id, organization_id, **fields)
+ if result is None:
+ LOG.warning("Twilio SMS lifecycle compensation found no local row", phone_number_id=phone_number_id)
+ except BaseException:
+ LOG.exception("Twilio SMS lifecycle compensation failed", phone_number_id=phone_number_id)
+
+
+_DISABLE_STAGING_FENCE_FIELDS = set(
+ (
+ "organization_id phone_number_id phone_number sms_config_id provider provider_number_sid "
+ "previous_sms_url previous_sms_method previous_sms_application_sid credential_id"
+ ).split()
+)
+
+
+async def _compensate_disable_staging(organization_id: str, registered: OrganizationPhoneNumber) -> None:
+ try:
+ current = await app.DATABASE.sms.get_phone_number(registered.phone_number_id, organization_id)
+ except BaseException:
+ LOG.exception("Twilio SMS disable compensation could not read local state")
+ return
+ if (
+ current is None
+ or current.status != "quarantined"
+ or current.model_dump(include=_DISABLE_STAGING_FENCE_FIELDS)
+ != registered.model_dump(include=_DISABLE_STAGING_FENCE_FIELDS)
+ ):
+ return
+ await _compensate_phone_number_state(
+ organization_id=organization_id, phone_number_id=registered.phone_number_id, registered=registered
+ )
+
+
+_Routing = tuple[str | None, str | None, str | None]
+
+
+def _routing_matches(number: dict[str, Any], expected: _Routing) -> bool:
+ current = (
+ _current_sms_url(number) or "",
+ (_current_sms_method(number) or "POST").upper(),
+ _current_sms_application_sid(number) or "",
+ )
+ return current == (expected[0] or "", (expected[1] or "POST").upper(), expected[2] or "")
+
+
+async def _provider_mutation_outcome(
+ client: Any,
+ phone_number_sid: str,
+ *,
+ desired: _Routing,
+ prior: _Routing,
+) -> str:
+ try:
+ provider_number = await _shielded(client.get_incoming_phone_number(phone_number_sid))
+ except TwilioApiError as error:
+ if error.status_code == 404:
+ return "absent"
+ LOG.warning("Twilio SMS provider outcome could not be read back", status_code=error.status_code)
+ return "unknown"
+ except BaseException:
+ LOG.exception("Twilio SMS provider outcome could not be read back")
+ return "unknown"
+ if _routing_matches(provider_number, desired):
+ return "desired"
+ if _routing_matches(provider_number, prior):
+ return "prior"
+ return "unknown"
+
+
+async def _reconcile_provider_failure(
+ client: Any,
+ phone_number_sid: str,
+ *,
+ desired: _Routing,
+ prior: _Routing,
+ phone_number_id: str,
+ organization_id: str,
+ registered: OrganizationPhoneNumber | None,
+ status: str,
+ absent_is_desired: bool,
+) -> None:
+ outcome = await _provider_mutation_outcome(client, phone_number_sid, desired=desired, prior=prior)
+ if outcome == "desired" or (outcome == "absent" and absent_is_desired):
+ await _finalize_phone_number(phone_number_id, organization_id, status=status)
+ elif outcome in {"prior", "absent"}:
+ await _compensate_phone_number_state(
+ organization_id=organization_id,
+ phone_number_id=phone_number_id,
+ registered=registered,
+ )
+
+
+async def _finalize_phone_number(
+ phone_number_id: str,
+ organization_id: str,
+ *,
+ status: str,
+) -> OrganizationPhoneNumber:
+ updated = await app.DATABASE.sms.update_phone_number(
+ phone_number_id,
+ organization_id,
+ status=status,
+ quarantined_until=None,
+ )
+ if updated is None:
+ raise HTTPException(status_code=404, detail="Phone number not found")
+ return updated
+
+
+@asynccontextmanager
+async def _sms_lifecycle_lock(organization_id: str, phone_number: str | None = None) -> AsyncIterator[None]:
+ if phone_number is None:
+ async with app.DATABASE.sms.organization_lock(organization_id):
+ yield
+ else:
+ async with app.DATABASE.sms.lifecycle_lock(organization_id, normalize_phone_identifier(phone_number)):
+ yield
+
+
+@asynccontextmanager
+async def _organization_sms_lifecycle_lock(organization_id: str) -> AsyncIterator[None]:
+ async with _sms_lifecycle_lock(organization_id):
+ yield
+
+
+@twilio_integration_router.post(
+ "/phone-numbers/enable",
+ response_model=OrganizationPhoneNumberResponse,
+ openapi_extra={
+ "x-fern-sdk-group-name": "twilio",
+ "x-fern-sdk-method-name": "enable_phone_number",
+ },
+)
+async def enable_twilio_phone_number(
+ request: EnablePhoneNumberRequest,
+ current_org: Annotated[Organization, Depends(org_auth_service.get_current_org_for_credential_routes)],
+) -> OrganizationPhoneNumber:
+ _require_twilio_sms_enabled()
+ _require_sms_base_url()
+ return await _run_lifecycle(_enable_twilio_phone_number_locked(request, current_org.organization_id))
+
+
+async def _enable_twilio_phone_number_locked(
+ request: EnablePhoneNumberRequest,
+ organization_id: str,
+) -> OrganizationPhoneNumber:
+ if request.credential_id is not None:
+ bound_credential = await app.DATABASE.credentials.get_credential(
+ request.credential_id, organization_id=organization_id
+ )
+ if bound_credential is None:
+ raise HTTPException(status_code=400, detail="Credential not found")
+
+ credential = await _get_credential(organization_id)
+ if not credential.auth_token:
+ raise HTTPException(status_code=400, detail="Twilio auth token is required for signed inbound delivery")
+ client = build_client_from_credential(credential)
+ _, phone_number = await _load_sms_capable_provider_number(client, request.phone_number_sid)
+ async with _sms_lifecycle_lock(organization_id, phone_number):
+ credential = await _get_credential(organization_id)
+ if not credential.auth_token:
+ raise HTTPException(status_code=400, detail="Twilio auth token is required for signed inbound delivery")
+ client = build_client_from_credential(credential)
+ provider_number, refreshed_phone_number = await _load_sms_capable_provider_number(
+ client, request.phone_number_sid
+ )
+ if refreshed_phone_number != phone_number:
+ raise HTTPException(status_code=409, detail="Phone number changed during lifecycle operation")
+ registered = await _get_registered_phone_number(phone_number, organization_id)
+ _require_twilio_provider(registered)
+ connected_config = await app.DATABASE.sms.get_connected_sms_config(organization_id)
+ if connected_config is None:
+ connected_config = await app.DATABASE.sms.create_sms_config(
+ organization_id=organization_id, mode="connected", webhook_secret=secrets.token_urlsafe(32)
+ )
+ config, webhook_secret = connected_config
+ inbound_url = _inbound_url(config.sms_config_id, webhook_secret)
+ current_sms_url = _current_sms_url(provider_number)
+ current_sms_method = _current_sms_method(provider_number)
+ current_sms_application_sid = _current_sms_application_sid(provider_number)
+ routing_is_ours = _routing_is_ours(
+ current_sms_url=current_sms_url,
+ current_sms_method=current_sms_method,
+ current_sms_application_sid=current_sms_application_sid,
+ inbound_url=inbound_url,
+ )
+ takeover_required = (
+ bool(current_sms_url and current_sms_url != inbound_url)
+ or bool(current_sms_method and current_sms_method.upper() != "POST")
+ or bool(current_sms_application_sid)
+ )
+ if takeover_required and not request.confirm_takeover:
+ raise HTTPException(
+ status_code=409,
+ detail={
+ "current_sms_url": redact_url_query(redact_url_for_display(current_sms_url) or "") or None,
+ "current_sms_method": current_sms_method,
+ "current_sms_application_sid": current_sms_application_sid,
+ },
+ )
+
+ saved: OrganizationPhoneNumber | None = None
+ if registered is None:
+ try:
+ saved = await app.DATABASE.sms.create_phone_number(
+ organization_id=organization_id,
+ sms_config_id=config.sms_config_id,
+ phone_number=phone_number,
+ provider="twilio",
+ provider_number_sid=request.phone_number_sid,
+ previous_sms_url=current_sms_url if not routing_is_ours else None,
+ previous_sms_method=current_sms_method if not routing_is_ours else None,
+ previous_sms_application_sid=current_sms_application_sid if not routing_is_ours else None,
+ credential_id=request.credential_id,
+ status="quarantined",
+ )
+ except BaseException:
+ if saved is not None:
+ await _compensate_phone_number_state(
+ organization_id=organization_id,
+ phone_number_id=saved.phone_number_id,
+ registered=None,
+ )
+ raise
+ else:
+ update_fields: dict[str, Any] = {
+ "status": "quarantined",
+ "provider": "twilio",
+ "provider_number_sid": request.phone_number_sid,
+ "sms_config_id": config.sms_config_id,
+ "credential_id": request.credential_id,
+ "quarantined_until": None,
+ }
+ if (
+ registered.status == "active"
+ and routing_is_ours
+ and registered.sms_config_id == config.sms_config_id
+ and registered.provider_number_sid == request.phone_number_sid
+ and registered.credential_id == request.credential_id
+ ):
+ update_fields["status"] = "active"
+ if not routing_is_ours:
+ update_fields.update(
+ previous_sms_url=current_sms_url,
+ previous_sms_method=current_sms_method,
+ previous_sms_application_sid=current_sms_application_sid,
+ )
+ try:
+ saved = await app.DATABASE.sms.update_phone_number(
+ registered.phone_number_id, organization_id, **update_fields
+ )
+ if saved is None:
+ raise HTTPException(status_code=404, detail="Phone number not found")
+ except BaseException:
+ if registered.status != "quarantined":
+ await _compensate_phone_number_state(
+ organization_id=organization_id,
+ phone_number_id=registered.phone_number_id,
+ registered=registered,
+ )
+ raise
+
+ try:
+ await _shielded(client.update_inbound_sms_config(request.phone_number_sid, inbound_url, "POST", ""))
+ except BaseException as error:
+ await _reconcile_provider_failure(
+ client,
+ request.phone_number_sid,
+ desired=(inbound_url, "POST", ""),
+ prior=(current_sms_url, current_sms_method, current_sms_application_sid),
+ phone_number_id=saved.phone_number_id,
+ organization_id=organization_id,
+ registered=registered,
+ status="active",
+ absent_is_desired=False,
+ )
+ if isinstance(error, TwilioApiError):
+ raise _twilio_api_http_exception(error) from None
+ raise
+ return await _finalize_phone_number(saved.phone_number_id, organization_id, status="active")
+
+
+@twilio_integration_router.post(
+ "/credentials",
+ response_model=TwilioCredentialResponse,
+ openapi_extra={
+ "x-fern-sdk-group-name": "twilio",
+ "x-fern-sdk-method-name": "create_credentials",
+ },
+)
+async def create_twilio_credential(
+ request: CreateTwilioCredentialRequest,
+ current_org: Annotated[Organization, Depends(org_auth_service.get_current_org_for_credential_routes)],
+) -> TwilioCredentialResponse:
+ _require_twilio_sms_enabled()
+ organization_id = current_org.organization_id
+ async with _organization_sms_lifecycle_lock(organization_id):
+ active_number_count = await _active_connected_phone_count(organization_id)
+ if active_number_count:
+ existing_credential = await _get_credential(organization_id)
+ if request.credential.account_sid != existing_credential.account_sid:
+ raise HTTPException(
+ status_code=409,
+ detail="Disable all connected Twilio phone numbers before changing Twilio accounts",
+ )
+ if not request.credential.auth_token:
+ raise HTTPException(
+ status_code=400,
+ detail="Twilio auth token is required while connected phone numbers are active or quarantined",
+ )
+ await _validate_twilio_credential(request.credential)
+
+ await app.DATABASE.organizations.replace_org_auth_token(
+ organization_id=organization_id,
+ token_type=OrganizationAuthTokenType.twilio_credential,
+ token=request.credential,
+ encrypted_method=EncryptMethod.AES,
+ )
+ return _safe_credential(request.credential)
+
+
+@twilio_integration_router.get(
+ "/credentials",
+ response_model=TwilioCredentialResponse,
+ openapi_extra={
+ "x-fern-sdk-group-name": "twilio",
+ "x-fern-sdk-method-name": "get_credentials",
+ },
+)
+async def get_twilio_credential(
+ current_org: Annotated[Organization, Depends(org_auth_service.get_current_org_for_credential_routes)],
+) -> TwilioCredentialResponse:
+ _require_twilio_sms_enabled()
+ token = await app.DATABASE.organizations.get_valid_org_auth_token(
+ current_org.organization_id,
+ OrganizationAuthTokenType.twilio_credential.value,
+ )
+ if token is None:
+ return TwilioCredentialResponse(credential=None)
+ return _safe_credential(token.credential)
+
+
+@twilio_integration_router.delete(
+ "/credentials",
+ openapi_extra={
+ "x-fern-sdk-group-name": "twilio",
+ "x-fern-sdk-method-name": "delete_credentials",
+ },
+)
+async def delete_twilio_credential(
+ current_org: Annotated[Organization, Depends(org_auth_service.get_current_org_for_credential_routes)],
+) -> dict[str, bool]:
+ _require_twilio_sms_enabled()
+ organization_id = current_org.organization_id
+ async with _organization_sms_lifecycle_lock(organization_id):
+ if await _active_connected_phone_count(organization_id):
+ raise HTTPException(
+ status_code=409,
+ detail="Disable all connected Twilio phone numbers before disconnecting Twilio",
+ )
+ await app.DATABASE.organizations.invalidate_org_auth_tokens(
+ organization_id=organization_id,
+ token_type=OrganizationAuthTokenType.twilio_credential,
+ )
+ return {"success": True}
+
+
+@twilio_integration_router.get(
+ "/phone-number-registry",
+ response_model=list[OrganizationPhoneNumberResponse],
+ openapi_extra={
+ "x-fern-sdk-group-name": "twilio",
+ "x-fern-sdk-method-name": "list_registered_phone_numbers",
+ },
+)
+async def list_registered_phone_numbers(
+ current_org: Annotated[Organization, Depends(org_auth_service.get_current_org_for_credential_routes)],
+) -> list[OrganizationPhoneNumber]:
+ _require_twilio_sms_enabled()
+ return await app.DATABASE.sms.list_phone_numbers(current_org.organization_id)
+
+
+@twilio_integration_router.get(
+ "/phone-numbers",
+ response_model=list[TwilioPhoneNumberInfo],
+ openapi_extra={
+ "x-fern-sdk-group-name": "twilio",
+ "x-fern-sdk-method-name": "list_phone_numbers",
+ },
+)
+async def list_twilio_phone_numbers(
+ current_org: Annotated[Organization, Depends(org_auth_service.get_current_org_for_credential_routes)],
+) -> list[TwilioPhoneNumberInfo]:
+ _require_twilio_sms_enabled()
+ credential = await _get_credential(current_org.organization_id)
+ client = build_client_from_credential(credential)
+ try:
+ provider_numbers = await client.list_incoming_phone_numbers()
+ except TwilioApiError as error:
+ raise _twilio_api_http_exception(error) from None
+ registry_numbers = await app.DATABASE.sms.list_phone_numbers(current_org.organization_id)
+ registry_by_number = {normalize_phone_identifier(number.phone_number): number for number in registry_numbers}
+ connected_config = await app.DATABASE.sms.get_connected_sms_config(current_org.organization_id)
+ connected_config_id: str | None = None
+ connected_inbound_url: str | None = None
+ if connected_config is not None:
+ config, webhook_secret = connected_config
+ connected_config_id = config.sms_config_id
+ connected_inbound_url = _inbound_url(config.sms_config_id, webhook_secret)
+ result: list[TwilioPhoneNumberInfo] = []
+ for provider_number in provider_numbers:
+ phone_number = normalize_phone_identifier(str(provider_number.get("phone_number") or ""))
+ registered = registry_by_number.get(phone_number)
+ current_sms_url = _current_sms_url(provider_number)
+ routing_is_ours = connected_inbound_url is not None and _routing_is_ours(
+ current_sms_url=current_sms_url,
+ current_sms_method=_current_sms_method(provider_number),
+ current_sms_application_sid=_current_sms_application_sid(provider_number),
+ inbound_url=connected_inbound_url,
+ )
+ result.append(
+ TwilioPhoneNumberInfo(
+ sid=str(provider_number.get("sid") or ""),
+ phone_number=phone_number,
+ friendly_name=str(provider_number.get("friendly_name") or phone_number),
+ sms_capable=_sms_capable(provider_number),
+ current_sms_url=redact_url_query(redact_url_for_display(current_sms_url) or "") or None,
+ enabled=(
+ registered is not None
+ and registered.status == "active"
+ and registered.sms_config_id == connected_config_id
+ and routing_is_ours
+ ),
+ phone_number_id=registered.phone_number_id if registered is not None else None,
+ )
+ )
+ return result
+
+
+@twilio_integration_router.post(
+ "/phone-numbers/disable",
+ response_model=OrganizationPhoneNumberResponse,
+ openapi_extra={
+ "x-fern-sdk-group-name": "twilio",
+ "x-fern-sdk-method-name": "disable_phone_number",
+ },
+)
+async def disable_twilio_phone_number(
+ request: DisablePhoneNumberRequest,
+ current_org: Annotated[Organization, Depends(org_auth_service.get_current_org_for_credential_routes)],
+) -> OrganizationPhoneNumber:
+ _require_twilio_sms_enabled()
+ return await _run_lifecycle(_disable_twilio_phone_number_locked(request, current_org.organization_id))
+
+
+async def _disable_twilio_phone_number_locked(
+ request: DisablePhoneNumberRequest,
+ organization_id: str,
+) -> OrganizationPhoneNumber:
+ initial = await app.DATABASE.sms.get_phone_number(request.phone_number_id, organization_id)
+ if initial is None:
+ raise HTTPException(status_code=404, detail="Phone number not found")
+ _require_twilio_provider(initial)
+ async with _sms_lifecycle_lock(organization_id, initial.phone_number):
+ registered = await app.DATABASE.sms.get_phone_number(request.phone_number_id, organization_id)
+ if registered is None:
+ raise HTTPException(status_code=404, detail="Phone number not found")
+ _require_twilio_provider(registered)
+ was_quarantined = registered.status == "quarantined"
+ client: Any | None = None
+ if registered.provider_number_sid:
+ credential = await _get_credential(organization_id)
+ client = build_client_from_credential(credential)
+ if not was_quarantined:
+ staged_row = await app.DATABASE.sms.update_phone_number(
+ registered.phone_number_id,
+ organization_id,
+ status="quarantined",
+ quarantined_until=None,
+ )
+ if staged_row is None:
+ raise HTTPException(status_code=404, detail="Phone number not found")
+
+ if registered.provider_number_sid and client is not None:
+ try:
+ provider_number = await client.get_incoming_phone_number(registered.provider_number_sid)
+ except TwilioApiError as error:
+ if error.status_code != 404:
+ if not was_quarantined:
+ await _compensate_disable_staging(organization_id, registered)
+ raise _twilio_api_http_exception(error) from None
+ else:
+ config_with_secret = await app.DATABASE.sms.get_sms_config_with_secret(
+ sms_config_id=registered.sms_config_id, organization_id=organization_id
+ )
+ if config_with_secret is not None:
+ config, webhook_secret = config_with_secret
+ inbound_url = _inbound_url(config.sms_config_id, webhook_secret)
+ if _routing_is_ours(
+ current_sms_url=_current_sms_url(provider_number),
+ current_sms_method=_current_sms_method(provider_number),
+ current_sms_application_sid=_current_sms_application_sid(provider_number),
+ inbound_url=inbound_url,
+ ):
+ prior_url = _current_sms_url(provider_number)
+ prior_method = _current_sms_method(provider_number)
+ prior_application_sid = _current_sms_application_sid(provider_number)
+ desired_url = registered.previous_sms_url or ""
+ try:
+ if is_encrypted_secret(desired_url):
+ desired_url = await decrypt_secret_field_value(
+ desired_url, organization_id=organization_id, field_name="previous_sms_url"
+ )
+ except BaseException:
+ if not was_quarantined:
+ await _compensate_disable_staging(organization_id, registered)
+ raise
+ desired_method = registered.previous_sms_method or "POST"
+ desired_application_sid = registered.previous_sms_application_sid or ""
+ try:
+ await _shielded(
+ client.update_inbound_sms_config(
+ registered.provider_number_sid,
+ desired_url,
+ desired_method,
+ desired_application_sid,
+ )
+ )
+ except BaseException as error:
+ await _reconcile_provider_failure(
+ client,
+ registered.provider_number_sid,
+ desired=(desired_url, desired_method, desired_application_sid),
+ prior=(prior_url, prior_method, prior_application_sid),
+ phone_number_id=registered.phone_number_id,
+ organization_id=organization_id,
+ registered=registered,
+ status="disabled",
+ absent_is_desired=True,
+ )
+ if isinstance(error, TwilioApiError):
+ raise _twilio_api_http_exception(error) from None
+ raise
+ return await _finalize_phone_number(registered.phone_number_id, organization_id, status="disabled")
+
+
+@twilio_integration_router.post("/sms-configs", response_model=SMSConfigCreated)
+async def create_sms_config(
+ request: CreateSMSConfigRequest,
+ current_org: Annotated[Organization, Depends(org_auth_service.get_current_org_for_credential_routes)],
+) -> SMSConfigCreated:
+ _require_twilio_sms_enabled()
+ _require_sms_base_url()
+ organization_id = current_org.organization_id
+ async with _organization_sms_lifecycle_lock(organization_id):
+ config, webhook_secret = await app.DATABASE.sms.create_sms_config(
+ organization_id=organization_id,
+ mode=request.mode,
+ webhook_secret=secrets.token_urlsafe(32),
+ )
+ return SMSConfigCreated(
+ **config.model_dump(),
+ inbound_url=_inbound_url(config.sms_config_id, webhook_secret),
+ )
+
+
+@twilio_integration_router.get("/sms-configs", response_model=list[SMSConfig])
+async def list_sms_configs(
+ current_org: Annotated[Organization, Depends(org_auth_service.get_current_org_for_credential_routes)],
+) -> list[SMSConfig]:
+ _require_twilio_sms_enabled()
+ return await app.DATABASE.sms.list_sms_configs(current_org.organization_id)
+
+
+@twilio_integration_router.delete("/sms-configs/{sms_config_id}")
+async def delete_sms_config(
+ sms_config_id: str,
+ current_org: Annotated[Organization, Depends(org_auth_service.get_current_org_for_credential_routes)],
+) -> dict[str, bool]:
+ _require_twilio_sms_enabled()
+ organization_id = current_org.organization_id
+ async with _organization_sms_lifecycle_lock(organization_id):
+ config = await app.DATABASE.sms.get_sms_config(
+ sms_config_id,
+ organization_id,
+ include_deleted=True,
+ )
+ if config is None:
+ raise HTTPException(status_code=404, detail="SMS config not found")
+ if config.mode == "managed":
+ raise HTTPException(
+ status_code=409,
+ detail="Skyvern-managed SMS configurations must be released from the Skyvern Numbers integration",
+ )
+ active_phone_count = await app.DATABASE.sms.count_active_phone_numbers_for_config(
+ sms_config_id,
+ organization_id,
+ )
+ if active_phone_count > 0:
+ raise HTTPException(status_code=409, detail="SMS configuration still has active phone numbers")
+ await app.DATABASE.sms.delete_sms_config(sms_config_id, organization_id)
+ return {"success": True}
+
+
+@twilio_integration_router.post(
+ "/sms-configs/{sms_config_id}/phone-numbers",
+ response_model=OrganizationPhoneNumberResponse,
+)
+async def register_manual_phone_number(
+ sms_config_id: str,
+ request: RegisterManualPhoneNumberRequest,
+ current_org: Annotated[Organization, Depends(org_auth_service.get_current_org_for_credential_routes)],
+) -> OrganizationPhoneNumber:
+ _require_twilio_sms_enabled()
+ organization_id = current_org.organization_id
+ config = await app.DATABASE.sms.get_sms_config(sms_config_id, organization_id)
+ if config is None:
+ raise HTTPException(status_code=404, detail="SMS config not found")
+ if config.mode != "manual":
+ raise HTTPException(status_code=400, detail="SMS config is not manual")
+ phone_number = normalize_phone_identifier(request.phone_number)
+ if not is_e164_phone_number(phone_number):
+ raise HTTPException(status_code=400, detail="Phone number must use E.164 format")
+ async with _sms_lifecycle_lock(organization_id, phone_number):
+ config = await app.DATABASE.sms.get_sms_config(sms_config_id, organization_id)
+ if config is None:
+ raise HTTPException(status_code=404, detail="SMS config not found")
+ if config.mode != "manual":
+ raise HTTPException(status_code=400, detail="SMS config is not manual")
+ registered = await _get_registered_phone_number(phone_number, organization_id)
+ _require_twilio_provider(registered)
+ if registered is None:
+ return await app.DATABASE.sms.create_phone_number(
+ organization_id=organization_id,
+ sms_config_id=sms_config_id,
+ phone_number=phone_number,
+ provider="twilio",
+ )
+ if (
+ registered.status != "disabled"
+ or registered.provider_number_sid is not None
+ or registered.provider != "twilio"
+ ):
+ raise HTTPException(status_code=409, detail="Phone number is already registered")
+ updated = await app.DATABASE.sms.update_phone_number(
+ registered.phone_number_id,
+ organization_id,
+ sms_config_id=sms_config_id,
+ status="active",
+ )
+ if updated is None:
+ raise HTTPException(status_code=404, detail="Phone number not found")
+ return updated
diff --git a/skyvern/forge/sdk/schemas/credentials.py b/skyvern/forge/sdk/schemas/credentials.py
index 88ae6acd4..c5b17c00c 100644
--- a/skyvern/forge/sdk/schemas/credentials.py
+++ b/skyvern/forge/sdk/schemas/credentials.py
@@ -55,6 +55,13 @@ class PasswordCredentialResponse(BaseModel):
description="Identifier (email or phone number) used to fetch TOTP codes",
examples=["[email protected]", "+14155550123"],
)
+ has_totp: bool = Field(
+ default=False,
+ description=(
+ "Whether the stored password credential includes a non-empty TOTP seed. "
+ "Legacy rows with an unknown value report false until the credential is updated."
+ ),
+ )
class CredentialTotpCodeResponse(BaseModel):
@@ -394,6 +401,10 @@ class Credential(BaseModel):
description="Identifier (email or phone number) used to fetch TOTP codes",
examples=["[email protected]", "+14155550123"],
)
+ has_totp_seed: bool | None = Field(
+ default=None,
+ description="Whether the stored vault credential contains a non-empty TOTP seed",
+ )
card_last4: str | None = Field(..., description="For credit_card credentials: the last four digits of the card")
card_brand: str | None = Field(..., description="For credit_card credentials: the card brand")
secret_label: str | None = Field(default=None, description="For secret credentials: optional label")
diff --git a/skyvern/forge/sdk/schemas/organizations.py b/skyvern/forge/sdk/schemas/organizations.py
index a85c38a46..b23fd59b7 100644
--- a/skyvern/forge/sdk/schemas/organizations.py
+++ b/skyvern/forge/sdk/schemas/organizations.py
@@ -1,7 +1,18 @@
+import re
from datetime import datetime
-from typing import Literal
-
-from pydantic import BaseModel, ConfigDict, EmailStr, Field, field_validator
+from typing import Any, Literal, Self, cast
+
+from pydantic import (
+ BaseModel,
+ ConfigDict,
+ EmailStr,
+ Field,
+ ModelWrapValidatorHandler,
+ ValidationError,
+ field_validator,
+ model_validator,
+)
+from pydantic_core import InitErrorDetails
from skyvern.forge.sdk.db.enums import OrganizationAuthTokenType
from skyvern.utils.organization_slug import is_valid_org_slug
@@ -119,6 +130,108 @@ class BitwardenCredentialResponse(BaseModel):
)
+def _twilio_error_without_inputs(error: ValidationError) -> ValidationError:
+ # hide_input_in_errors only affects text; remove inputs from structured errors too.
+ return ValidationError.from_exception_data(
+ error.title,
+ cast(list[InitErrorDetails], error.errors(include_input=False, include_url=False)),
+ hide_input=True,
+ )
+
+
+def _validate_twilio_without_error_inputs(value: object, handler: ModelWrapValidatorHandler[BaseModel]) -> BaseModel:
+ try:
+ return handler(value)
+ except ValidationError as error:
+ raise _twilio_error_without_inputs(error) from None
+
+
+class _TwilioJSONModel(BaseModel):
+ @classmethod
+ def model_validate_json(cls, json_data: str | bytes | bytearray, **kwargs: Any) -> Self:
+ # Malformed JSON fails before model validators can remove secret inputs.
+ try:
+ return super().model_validate_json(json_data, **kwargs)
+ except ValidationError as error:
+ raise _twilio_error_without_inputs(error) from None
+
+
+class TwilioCredential(_TwilioJSONModel):
+ model_config = ConfigDict(hide_input_in_errors=True)
+
+ account_sid: str
+ api_key_sid: str | None = None
+ api_key_secret: str | None = Field(default=None, repr=False)
+ auth_token: str | None = Field(default=None, repr=False)
+
+ @field_validator("account_sid")
+ @classmethod
+ def validate_account_sid(cls, value: str) -> str:
+ if re.fullmatch(r"AC[0-9a-fA-F]{32}", value) is None:
+ raise ValueError("Twilio account_sid must match AC followed by 32 hexadecimal characters")
+ return value
+
+ @field_validator("api_key_sid")
+ @classmethod
+ def validate_api_key_sid(cls, value: str | None) -> str | None:
+ if value is not None and re.fullmatch(r"SK[0-9a-fA-F]{32}", value) is None:
+ raise ValueError("Twilio api_key_sid must match SK followed by 32 hexadecimal characters")
+ return value
+
+ @field_validator("api_key_secret", "auth_token")
+ @classmethod
+ def validate_secret(cls, value: str | None) -> str | None:
+ if value is not None and not value.strip():
+ raise ValueError("Twilio secrets must not be blank")
+ return value
+
+ @model_validator(mode="after")
+ def validate_authentication(self) -> "TwilioCredential":
+ has_api_key_sid = self.api_key_sid is not None
+ has_api_key_secret = self.api_key_secret is not None
+ if has_api_key_sid != has_api_key_secret:
+ raise ValueError("Provide both api_key_sid and api_key_secret")
+ if self.auth_token or (has_api_key_sid and has_api_key_secret):
+ return self
+ raise ValueError("Provide auth_token or both api_key_sid and api_key_secret")
+
+ _hide_validation_inputs = model_validator(mode="wrap")(_validate_twilio_without_error_inputs)
+
+
+class TwilioCredentialSafe(_TwilioJSONModel):
+ model_config = ConfigDict(hide_input_in_errors=True)
+
+ account_sid: str
+ api_key_sid: str | None = None
+ has_auth_token: bool
+
+ _hide_validation_inputs = model_validator(mode="wrap")(_validate_twilio_without_error_inputs)
+
+
+class TwilioOrganizationAuthToken(OrganizationAuthTokenBase, _TwilioJSONModel):
+ model_config = ConfigDict(hide_input_in_errors=True)
+
+ credential: TwilioCredential
+
+ _hide_validation_inputs = model_validator(mode="wrap")(_validate_twilio_without_error_inputs)
+
+
+class CreateTwilioCredentialRequest(_TwilioJSONModel):
+ model_config = ConfigDict(hide_input_in_errors=True)
+
+ credential: TwilioCredential
+
+ _hide_validation_inputs = model_validator(mode="wrap")(_validate_twilio_without_error_inputs)
+
+
+class TwilioCredentialResponse(_TwilioJSONModel):
+ model_config = ConfigDict(hide_input_in_errors=True)
+
+ credential: TwilioCredentialSafe
+
+ _hide_validation_inputs = model_validator(mode="wrap")(_validate_twilio_without_error_inputs)
+
+
class CreateOnePasswordTokenRequest(BaseModel):
"""Request model for creating or updating a 1Password service account token."""
diff --git a/skyvern/forge/sdk/schemas/sms.py b/skyvern/forge/sdk/schemas/sms.py
new file mode 100644
index 000000000..b4b827781
--- /dev/null
+++ b/skyvern/forge/sdk/schemas/sms.py
@@ -0,0 +1,99 @@
+from __future__ import annotations
+
+from datetime import datetime
+from typing import Literal, get_args
+
+from pydantic import BaseModel, ConfigDict, field_validator
+
+from skyvern.utils.phone_validation import (
+ is_e164_phone_number,
+ is_twilio_phone_number_sid,
+ looks_like_phone_identifier,
+ normalize_phone_identifier,
+)
+
+PhoneNumberStatus = Literal["active", "disabled", "released", "quarantined"]
+PHONE_NUMBER_STATUSES: frozenset[str] = frozenset(get_args(PhoneNumberStatus))
+SMSConfigMode = Literal["connected", "manual", "managed"]
+SMS_CONFIG_MODES: frozenset[str] = frozenset(get_args(SMSConfigMode))
+
+
+class SMSConfig(BaseModel):
+ model_config = ConfigDict(from_attributes=True)
+
+ sms_config_id: str
+ organization_id: str
+ mode: SMSConfigMode
+ daily_ingest_cap: int
+ created_at: datetime
+
+
+class SMSConfigCreated(SMSConfig):
+ inbound_url: str
+
+
+class OrganizationPhoneNumber(BaseModel):
+ model_config = ConfigDict(from_attributes=True)
+
+ phone_number_id: str
+ organization_id: str
+ sms_config_id: str
+ phone_number: str
+ provider: str
+ provider_number_sid: str | None = None
+ previous_sms_url: str | None = None
+ previous_sms_method: str | None = None
+ previous_sms_application_sid: str | None = None
+ credential_id: str | None = None
+ provider_cost_cents: int | None = None
+ price_cents: int | None = None
+ status: PhoneNumberStatus
+ quarantined_until: datetime | None = None
+ created_at: datetime
+ modified_at: datetime
+ deleted_at: datetime | None = None
+
+
+class TwilioPhoneNumberInfo(BaseModel):
+ sid: str
+ phone_number: str
+ friendly_name: str
+ sms_capable: bool
+ current_sms_url: str | None = None
+ enabled: bool
+ phone_number_id: str | None = None
+
+
+class EnablePhoneNumberRequest(BaseModel):
+ phone_number_sid: str
+ confirm_takeover: bool = False
+ credential_id: str | None = None
+
+ @field_validator("phone_number_sid")
+ @classmethod
+ def validate_phone_number_sid(cls, value: str) -> str:
+ if not is_twilio_phone_number_sid(value):
+ raise ValueError("phone_number_sid must be a valid Twilio incoming phone number SID")
+ return value
+
+
+class DisablePhoneNumberRequest(BaseModel):
+ phone_number_id: str
+
+
+class RegisterManualPhoneNumberRequest(BaseModel):
+ phone_number: str
+
+ @field_validator("phone_number")
+ @classmethod
+ def validate_phone_number(cls, value: str) -> str:
+ if not looks_like_phone_identifier(value):
+ raise ValueError("phone_number must include a country code and normalize to E.164")
+ normalized = normalize_phone_identifier(value)
+ if not is_e164_phone_number(normalized):
+ raise ValueError("phone_number must include a country code and normalize to E.164")
+ return normalized
+
+
+class CreateSMSConfigRequest(BaseModel):
+ mode: Literal["manual"] = "manual"
diff --git a/skyvern/forge/sdk/services/credential/credential_vault_service.py b/skyvern/forge/sdk/services/credential/credential_vault_service.py
index 4cb258e91..a86a50b58 100644
--- a/skyvern/forge/sdk/services/credential/credential_vault_service.py
+++ b/skyvern/forge/sdk/services/credential/credential_vault_service.py
@@ -1,6 +1,6 @@
import asyncio
from abc import ABC, abstractmethod
-from typing import Any, Awaitable, Callable
+from typing import Any, Awaitable, Callable, TypedDict
import structlog
@@ -20,6 +20,10 @@ from skyvern.forge.sdk.schemas.credentials import (
LOG = structlog.get_logger()
+class _HasTotpSeedKwargs(TypedDict, total=False):
+ has_totp_seed: bool
+
+
class CredentialVaultService(ABC):
"""Abstract interface for credential vault services.
@@ -199,10 +203,13 @@ class CredentialVaultService(ABC):
`password` defaults to "" so a login with no password can be stored, which makes an omitted
password indistinguishable from an explicitly blanked one by value alone. Only an explicitly
- set password may overwrite the stored one.
+ set password may overwrite the stored one. An omitted `totp` likewise preserves a stored
+ TOTP seed.
"""
updated_fields = updated_credential.model_fields_set
- if {"metadata", "password"}.issubset(updated_fields):
+ if {"metadata", "password"}.issubset(updated_fields) and (
+ "totp" in updated_fields or credential.has_totp_seed is False
+ ):
return updated_credential
existing_item = await self.get_credential_item(credential)
@@ -217,11 +224,13 @@ class CredentialVaultService(ABC):
)
return updated_credential
+ if "totp" not in updated_fields:
+ # Assignment adds totp to the original request's model_fields_set so _update_db_credential derives has_totp_seed.
+ updated_credential.totp = existing_item.credential.totp
preserved_fields: dict[str, object] = {}
- for field_name in ("metadata", "password"):
+ for field_name in ("metadata", "password", "totp"):
if field_name not in updated_fields:
preserved_fields[field_name] = getattr(existing_item.credential, field_name)
-
return updated_credential.model_copy(update=preserved_fields)
@staticmethod
@@ -265,6 +274,7 @@ class CredentialVaultService(ABC):
username=data.credential.username,
totp_type=data.credential.totp_type,
totp_identifier=data.credential.totp_identifier,
+ has_totp_seed=bool((data.credential.totp or "").strip()),
card_last4=None,
card_brand=None,
tested_url=data.tested_url,
@@ -283,6 +293,7 @@ class CredentialVaultService(ABC):
card_last4=data.credential.card_number[-4:],
card_brand=data.credential.card_brand,
totp_identifier=None,
+ has_totp_seed=False,
tested_url=data.tested_url,
proxy_location=data.proxy_location,
proxy_session_id=data.proxy_session_id,
@@ -299,6 +310,7 @@ class CredentialVaultService(ABC):
card_last4=None,
card_brand=None,
totp_identifier=None,
+ has_totp_seed=False,
secret_label=data.credential.secret_label,
tested_url=data.tested_url,
proxy_location=data.proxy_location,
@@ -322,6 +334,11 @@ class CredentialVaultService(ABC):
proxy_kwargs["rotate_proxy_session_id"] = True
if data.credential_type == CredentialType.PASSWORD:
+ has_totp_seed_kwargs: _HasTotpSeedKwargs = (
+ {"has_totp_seed": bool((data.credential.totp or "").strip())}
+ if "totp" in data.credential.model_fields_set
+ else {}
+ )
return await app.DATABASE.credentials.update_credential_vault_data(
credential_id=credential.credential_id,
organization_id=credential.organization_id,
@@ -334,6 +351,7 @@ class CredentialVaultService(ABC):
card_last4=None,
card_brand=None,
tested_url=data.tested_url,
+ **has_totp_seed_kwargs,
**proxy_kwargs,
)
elif data.credential_type == CredentialType.CREDIT_CARD:
@@ -349,6 +367,7 @@ class CredentialVaultService(ABC):
card_brand=data.credential.card_brand,
totp_identifier=None,
tested_url=data.tested_url,
+ has_totp_seed=False,
**proxy_kwargs,
)
elif data.credential_type == CredentialType.SECRET:
@@ -365,6 +384,7 @@ class CredentialVaultService(ABC):
totp_identifier=None,
secret_label=data.credential.secret_label,
tested_url=data.tested_url,
+ has_totp_seed=False,
**proxy_kwargs,
)
else:
diff --git a/skyvern/forge/sdk/services/twilio_service.py b/skyvern/forge/sdk/services/twilio_service.py
new file mode 100644
index 000000000..b4d5105e7
--- /dev/null
+++ b/skyvern/forge/sdk/services/twilio_service.py
@@ -0,0 +1,179 @@
+import base64
+import hashlib
+import hmac
+from collections.abc import Mapping
+from typing import Any
+from urllib.parse import urljoin, urlsplit
+
+import httpx
+
+from skyvern.forge.sdk.schemas.organizations import TwilioCredential
+from skyvern.utils.phone_validation import is_twilio_phone_number_sid
+
+TWILIO_API_BASE = "https://api.twilio.com"
+_TWILIO_API_VERSION = "2010-04-01"
+_TWILIO_TIMEOUT_SECONDS = 15.0
+_TWILIO_MAX_PAGES = 100
+
+
+class TwilioApiError(Exception):
+ def __init__(self, status_code: int, message: str) -> None:
+ self.status_code = status_code
+ self.message = message
+ super().__init__(message)
+
+
+class TwilioClient:
+ def __init__(
+ self,
+ account_sid: str,
+ *,
+ api_key_sid: str | None = None,
+ api_key_secret: str | None = None,
+ auth_token: str | None = None,
+ ) -> None:
+ self.account_sid = account_sid
+ self._username: str
+ self._password: str | None
+ if api_key_sid and api_key_secret:
+ self._username = api_key_sid
+ self._password = api_key_secret
+ else:
+ self._username = account_sid
+ self._password = auth_token
+
+ def _new_http_client(self) -> httpx.AsyncClient:
+ if self._password is None:
+ raise TwilioApiError(0, "Twilio API credentials are incomplete")
+ return httpx.AsyncClient(
+ auth=(self._username, self._password),
+ timeout=_TWILIO_TIMEOUT_SECONDS,
+ )
+
+ async def _request_json(
+ self,
+ client: httpx.AsyncClient,
+ method: str,
+ url: str,
+ **kwargs: Any,
+ ) -> dict[str, Any]:
+ try:
+ response = await client.request(method, url, **kwargs)
+ except httpx.HTTPError as exc:
+ raise TwilioApiError(0, "Twilio API request failed") from exc
+
+ if not response.is_success:
+ raise TwilioApiError(response.status_code, _error_message(response))
+
+ try:
+ payload = response.json()
+ except ValueError as exc:
+ raise TwilioApiError(response.status_code, "Twilio API returned invalid JSON") from exc
+ if not isinstance(payload, dict):
+ raise TwilioApiError(response.status_code, "Twilio API returned an invalid response")
+ return payload
+
+ def _account_api_url(self, resource: str = "") -> str:
+ account_url = f"{TWILIO_API_BASE}/{_TWILIO_API_VERSION}/Accounts/{self.account_sid}"
+ return f"{account_url}/{resource}" if resource else f"{account_url}.json"
+
+ async def get_account(self) -> dict[str, Any]:
+ async with self._new_http_client() as client:
+ return await self._request_json(client, "GET", self._account_api_url())
+
+ async def list_incoming_phone_numbers(self) -> list[dict[str, Any]]:
+ url = self._account_api_url("IncomingPhoneNumbers.json")
+ params: dict[str, str] | None = {"PageSize": "100"}
+ phone_numbers: list[dict[str, Any]] = []
+ page_count = 0
+
+ async with self._new_http_client() as client:
+ while url:
+ payload = await self._request_json(client, "GET", url, params=params)
+ page_count += 1
+ page = payload.get("incoming_phone_numbers", [])
+ if not isinstance(page, list) or not all(isinstance(number, dict) for number in page):
+ raise TwilioApiError(200, "Twilio API returned an invalid phone number list")
+ phone_numbers.extend(page)
+
+ next_page_uri = payload.get("next_page_uri")
+ if next_page_uri is not None and not isinstance(next_page_uri, str):
+ raise TwilioApiError(200, "Twilio API returned an invalid next page URI")
+ if not next_page_uri:
+ url = ""
+ continue
+ if page_count >= _TWILIO_MAX_PAGES:
+ raise TwilioApiError(0, "Twilio pagination exceeded page limit")
+ next_page_url = urljoin(f"{TWILIO_API_BASE}/", next_page_uri)
+ parsed_next_page = urlsplit(next_page_url)
+ if parsed_next_page.scheme != "https" or parsed_next_page.netloc != "api.twilio.com":
+ raise TwilioApiError(0, "Unexpected Twilio pagination URL")
+ url = next_page_url
+ params = None
+
+ return phone_numbers
+
+ async def get_incoming_phone_number(self, number_sid: str) -> dict[str, Any]:
+ if not is_twilio_phone_number_sid(number_sid):
+ raise ValueError("phone_number_sid must be a valid Twilio incoming phone number SID")
+ url = self._account_api_url(f"IncomingPhoneNumbers/{number_sid}.json")
+ async with self._new_http_client() as client:
+ return await self._request_json(client, "GET", url)
+
+ async def update_inbound_sms_config(
+ self,
+ phone_number_sid: str,
+ sms_url: str,
+ sms_method: str = "POST",
+ sms_application_sid: str = "",
+ ) -> dict[str, Any]:
+ if not is_twilio_phone_number_sid(phone_number_sid):
+ raise ValueError("phone_number_sid must be a valid Twilio incoming phone number SID")
+ url = self._account_api_url(f"IncomingPhoneNumbers/{phone_number_sid}.json")
+ async with self._new_http_client() as client:
+ return await self._request_json(
+ client,
+ "POST",
+ url,
+ data={
+ "SmsUrl": sms_url,
+ "SmsMethod": sms_method,
+ "SmsApplicationSid": sms_application_sid,
+ },
+ )
+
+
+def _error_message(response: httpx.Response) -> str:
+ # Provider error bodies can contain webhook URLs, credentials, SMS bodies, or full SIDs.
+ # Keep the exception safe to expose to API callers and logs.
+ return f"Twilio API returned HTTP {response.status_code}"
+
+
+def compute_twilio_signature(auth_token: str, url: str, params: Mapping[str, str]) -> str:
+ signed_payload = url + "".join(key + params[key] for key in sorted(params))
+ # Twilio signs webhook requests with HMAC-SHA1 (https://www.twilio.com/docs/usage/webhooks/webhooks-security).
+ digest = hmac.new(auth_token.encode(), signed_payload.encode(), hashlib.sha1).digest()
+ return base64.b64encode(digest).decode()
+
+
+def validate_twilio_signature(
+ auth_token: str,
+ url: str,
+ params: Mapping[str, str],
+ signature: str,
+) -> bool:
+ expected_signature = compute_twilio_signature(auth_token, url, params).encode("ascii")
+ return hmac.compare_digest(expected_signature, signature.encode("utf-8", errors="surrogatepass"))
+
+
+def build_client_from_credential(credential: TwilioCredential) -> TwilioClient:
+ return TwilioClient(
+ credential.account_sid,
+ api_key_sid=credential.api_key_sid,
+ api_key_secret=credential.api_key_secret,
+ auth_token=credential.auth_token,
+ )
+
+
+def build_inbound_sms_url(base_url: str, sms_config_id: str, webhook_secret: str) -> str:
+ return f"{base_url.rstrip('/')}/v1/sms/inbound/{sms_config_id}?token={webhook_secret}"
diff --git a/skyvern/forge/sdk/workflow/context_manager.py b/skyvern/forge/sdk/workflow/context_manager.py
index a07b48160..efe652196 100644
--- a/skyvern/forge/sdk/workflow/context_manager.py
+++ b/skyvern/forge/sdk/workflow/context_manager.py
@@ -66,6 +66,7 @@ from skyvern.forge.sdk.workflow.models.parameter import (
WorkflowParameter,
WorkflowParameterType,
)
+from skyvern.utils.phone_validation import looks_like_phone_identifier, normalize_identifier
from skyvern.utils.secret_redaction import collect_redactable_secret_values, is_redactable_secret_value
from skyvern.utils.strings import generate_random_string
from skyvern.utils.templating import get_missing_variables
@@ -75,6 +76,19 @@ if TYPE_CHECKING:
LOG = structlog.get_logger()
+
+def _normalize_credential_totp_identifier(value: object) -> str | None:
+ if not isinstance(value, str):
+ return None
+ normalized = normalize_identifier(value)
+ if normalized.startswith("+") or not looks_like_phone_identifier(normalized):
+ return normalized
+ digits = re.sub(r"\D", "", normalized)
+ if len(digits) == 10:
+ return f"+1{digits}"
+ return normalized
+
+
BlockMetadata = dict[str, str | int | float | bool | dict | list | None]
BitwardenCredentials = tuple[str | None, str | None, str | None, str | None]
@@ -954,9 +968,13 @@ class WorkflowRunContext:
)
credential = credential_item.credential
- credential_totp_identifier = db_credential.totp_identifier or getattr(credential, "totp_identifier", None)
+ credential_totp_identifier = db_credential.totp_identifier
+ if not credential_totp_identifier and isinstance(credential, PasswordCredential):
+ credential_totp_identifier = credential.totp_identifier
if credential_totp_identifier:
- self.credential_totp_identifiers[parameter.key] = credential_totp_identifier
+ normalized_totp_identifier = _normalize_credential_totp_identifier(credential_totp_identifier)
+ if normalized_totp_identifier is not None:
+ self.credential_totp_identifiers[parameter.key] = normalized_totp_identifier
self.parameters[parameter.key] = parameter
self.values[parameter.key] = {
@@ -1148,6 +1166,10 @@ class WorkflowRunContext:
if item is None:
LOG.error("No 1Password item found", vault_id=vault_id, item_id=item_id)
raise ValueError(f"1Password item not found. {lookup_context}")
+ if parameter.totp_identifier:
+ normalized_totp_identifier = _normalize_credential_totp_identifier(parameter.totp_identifier)
+ if normalized_totp_identifier is not None:
+ self.credential_totp_identifiers[parameter.key] = normalized_totp_identifier
self.parameters[parameter.key] = parameter
self.values[parameter.key] = {
@@ -1399,7 +1421,6 @@ class WorkflowRunContext:
# username secret
username_secret_id = f"{random_secret_id}_username"
self.secrets[username_secret_id] = secret_credentials[BitwardenConstants.USERNAME]
- # password secret
password_secret_id = f"{random_secret_id}_password"
self.secrets[password_secret_id] = secret_credentials[BitwardenConstants.PASSWORD]
self.values[parameter.key] = {
@@ -1408,6 +1429,10 @@ class WorkflowRunContext:
"password": password_secret_id,
}
self.parameters[parameter.key] = parameter
+ if parameter.totp_identifier:
+ normalized_totp_identifier = _normalize_credential_totp_identifier(parameter.totp_identifier)
+ if normalized_totp_identifier is not None:
+ self.credential_totp_identifiers[parameter.key] = normalized_totp_identifier
if BitwardenConstants.TOTP in secret_credentials and secret_credentials[BitwardenConstants.TOTP]:
totp_secret_id = f"{random_secret_id}_totp"
diff --git a/skyvern/forge/sdk/workflow/models/parameter.py b/skyvern/forge/sdk/workflow/models/parameter.py
index c4d4a93e9..c8467ccca 100644
--- a/skyvern/forge/sdk/workflow/models/parameter.py
+++ b/skyvern/forge/sdk/workflow/models/parameter.py
@@ -126,6 +126,7 @@ class BitwardenLoginCredentialParameter(Parameter):
bitwarden_collection_id: str | None = None
# bitwarden item id to request the login credential
bitwarden_item_id: str | None = None
+ totp_identifier: str | None = None
created_at: datetime
modified_at: datetime
@@ -204,6 +205,7 @@ class OnePasswordCredentialParameter(Parameter):
# detection. Needed for items whose OTP is stored in a plain field (e.g. named "digits") rather
# than a native 1Password one-time-password field.
totp_field_name: str | None = None
+ totp_identifier: str | None = None
created_at: datetime
modified_at: datetime
diff --git a/skyvern/forge/sdk/workflow/workflow_definition_converter.py b/skyvern/forge/sdk/workflow/workflow_definition_converter.py
index fe1b065ec..ae2340723 100644
--- a/skyvern/forge/sdk/workflow/workflow_definition_converter.py
+++ b/skyvern/forge/sdk/workflow/workflow_definition_converter.py
@@ -188,6 +188,7 @@ def convert_workflow_definition(
vault_id=parameter.vault_id,
item_id=parameter.item_id,
totp_field_name=parameter.totp_field_name,
+ totp_identifier=parameter.totp_identifier,
created_at=now,
modified_at=now,
)
@@ -232,6 +233,7 @@ def convert_workflow_definition(
description=parameter.description,
bitwarden_collection_id=parameter.bitwarden_collection_id,
bitwarden_item_id=parameter.bitwarden_item_id,
+ totp_identifier=parameter.totp_identifier,
created_at=now,
modified_at=now,
)
diff --git a/skyvern/forge/taskv3/loop.py b/skyvern/forge/taskv3/loop.py
index cbffe40c4..4e3e59af6 100644
--- a/skyvern/forge/taskv3/loop.py
+++ b/skyvern/forge/taskv3/loop.py
@@ -59,6 +59,8 @@ ToolErrorClass = Literal[
# The target resolved, but the page will not let the act happen.
"disabled",
"not_editable",
+ # `type`: the page replaced the typed text with a non-empty value of its own; left in place.
+ "value_changed_by_page",
"covered",
"inert",
"unreachable",
diff --git a/skyvern/forge/taskv3/run_arms.py b/skyvern/forge/taskv3/run_arms.py
index 8e4bdba63..2157e77a7 100644
--- a/skyvern/forge/taskv3/run_arms.py
+++ b/skyvern/forge/taskv3/run_arms.py
@@ -17,6 +17,7 @@ from skyvern.forge.sdk.experimentation.providers import NoOpExperimentationProvi
LOG = structlog.get_logger()
OBSERVE_DROP_OFFVIEWPORT_UNNAMED_FLAG = "TASK_V3_OBSERVE_DROP_OFFVIEWPORT_UNNAMED"
+TYPE_COORDINATE_CLICK_FLAG = "TASK_V3_TYPE_COORDINATE_CLICK"
def _pinned_arm(context: skyvern_context.SkyvernContext, flag: str, distinct_id: str) -> RunArm | None:
diff --git a/skyvern/forge/taskv3/tools.py b/skyvern/forge/taskv3/tools.py
index ef3d1bcd8..f3faa6f45 100644
--- a/skyvern/forge/taskv3/tools.py
+++ b/skyvern/forge/taskv3/tools.py
@@ -28,7 +28,7 @@ from collections import Counter, defaultdict, deque
from contextvars import ContextVar
from datetime import datetime
from enum import Enum
-from typing import TYPE_CHECKING, Any, Awaitable, Callable, NamedTuple
+from typing import TYPE_CHECKING, Any, Awaitable, Callable, Literal, NamedTuple
from urllib.parse import urlparse
import structlog
@@ -67,7 +67,11 @@ from skyvern.forge.taskv3.loop import (
set_driver_timeout_predicate,
)
from skyvern.forge.taskv3.preflight import PREFLIGHT_TOOL_NAMES, preflight_tool_action
-from skyvern.forge.taskv3.run_arms import OBSERVE_DROP_OFFVIEWPORT_UNNAMED_FLAG, run_arm_enabled
+from skyvern.forge.taskv3.run_arms import (
+ OBSERVE_DROP_OFFVIEWPORT_UNNAMED_FLAG,
+ TYPE_COORDINATE_CLICK_FLAG,
+ run_arm_enabled,
+)
from skyvern.forge.taskv3.target_label import TARGET_KIND_TOKENS, TARGET_NAME_CAP
from skyvern.webeye.actions.key_names import normalize_key_chord
from skyvern.webeye.browser_driver_errors import is_driver_timeout_error
@@ -4173,6 +4177,18 @@ def _click_blocked_only_by_viewport(exc: BaseException) -> bool:
return "outside of the viewport" in message and not any(b in message for b in _OTHER_CLICK_BLOCKERS)
+# How type put the caret in a field: a checked click, focus() alone, or an unchecked press at its centre.
+_Reach = Literal["click", "focus", "point"]
+
+
+_CHANGED_VALUE_ECHO_MAX = 80
+
+# Same rule observe applies before it shows a field's value.
+_FIELD_VALUE_IS_SECRET_JS = (
+ "(el) => {" + OTP_INPUT_PRIVACY_JS + "return el.type === 'password' || isOtpInputValueSecret(el);}"
+)
+
+
def _typed_text_landed(read: str | None, typed: str) -> bool:
# Exact, whitespace included: a field that reformatted or trimmed the text ("3" -> "03", "abc " ->
# "abc") is refused rather than judged equivalent; a refusal costs one retry, a wrong equivalence is
@@ -9969,7 +9985,39 @@ def build_browser_tools(
return True
return False
- async def _focus_in_place_of_click(page: Any, selector: str, exc: Exception, *, focus_fallback: bool) -> None:
+ async def _click_at_box_centre(page: Any, selector: str) -> bool:
+ # No actionability or hit-target check: the press lands on whatever paints at the field's centre,
+ # which for a sub-pixel input is the display layer the probe has just ruled its own skin. Some
+ # segment widgets move their section cursor only on a trusted pointer event, so focus() alone
+ # leaves the keys rendering in the display while the input stays empty.
+ top = _current_page()
+ try:
+ # bounding_box() is relative to the main viewport even for an element inside a frame.
+ box = await page.locator(selector).first.bounding_box(timeout=2000)
+ width, height = await top.evaluate("() => [innerWidth, innerHeight]")
+ if not box:
+ return False
+ x = box["x"] + box["width"] / 2
+ y = box["y"] + box["height"] / 2
+ if not (0 <= x < width and 0 <= y < height):
+ return False
+ realm = page if _acted_realm else None
+ while realm is not None and realm.parent_frame is not None:
+ # A frame clips its content, so a point outside its element lands on the parent page.
+ frame_box = await (await realm.frame_element()).bounding_box()
+ if not frame_box or not (
+ frame_box["x"] <= x < frame_box["x"] + frame_box["width"]
+ and frame_box["y"] <= y < frame_box["y"] + frame_box["height"]
+ ):
+ return False
+ realm = realm.parent_frame
+ await page.evaluate("() => { window.__tv3_doc = 1; }")
+ await top.mouse.click(x, y)
+ except Exception:
+ return False
+ return True
+
+ async def _focus_in_place_of_click(page: Any, selector: str, exc: Exception, *, focus_fallback: bool) -> _Reach:
# focus() needs no hit target, so it stands in for a click refused only by the viewport check.
# A widget that hands the caret to another segment would take the keys there, so a caret that
# does not stay put re-raises the click's error; the caller must still prove the keystrokes landed.
@@ -9979,25 +10027,44 @@ def build_browser_tools(
# not tell a fill from a query; with no click to reach its rows, keep the error.
if await _declares_a_list(page, selector):
raise exc
+ reach: _Reach = "focus"
+ if run_arm_enabled(TYPE_COORDINATE_CLICK_FLAG, settings.TASK_V3_TYPE_COORDINATE_CLICK) and (
+ await _click_at_box_centre(page, selector)
+ ):
+ reach = "point"
+ try:
+ await page.wait_for_load_state("domcontentloaded", timeout=1000)
+ except Exception:
+ pass
+ try:
+ same_document = bool(await page.evaluate("() => window.__tv3_doc === 1"))
+ except Exception:
+ same_document = False
+ if not same_document:
+ # The press followed a link: the selector may match something on the destination.
+ raise exc
try:
+ # Still focused explicitly: the press need not move the caret, and the display it landed on
+ # may have no handler that forwards focus to the input.
await page.focus(selector, timeout=15000)
held = await page.evaluate(_ACTIVE_IS_JS, await _probe_arg(page, selector))
except Exception:
held = None
if held is not True:
raise exc
+ return reach
async def _focus_for_typing(
page: Any, selector: str, *, focus_fallback: bool = False
- ) -> tuple[bool, dict[str, Any] | None, bool]:
+ ) -> tuple[bool, dict[str, Any] | None, _Reach]:
"""Put the caret in `selector`. A False first element means the field is genuinely covered and
must not be typed into. A click is how a widget learns to open its suggestion list, so it stays
- the first move. The third element is True when the caret got there by focus() alone, after the
- click could not reach the field -- nothing the page did in response to a click has been seen."""
+ the first move. A third element other than "click" means the checked click could not reach the
+ field -- nothing the page did in response to a click has been seen."""
reachable, occluded, occluder = await _reachable_for_typing(page, selector)
if not reachable:
- return False, occluder, False
- focused_without_click = False
+ return False, occluder, "click"
+ reach: _Reach = "click"
if occluded:
# Forcing skips the hit-target check but still dispatches at coordinates, so the wrapper
# can take the event; the focus check below is what makes the outcome deterministic.
@@ -10013,8 +10080,7 @@ def build_browser_tools(
except Exception as exc:
# Force skips the hit-target check but not the viewport one, which rejects any box of
# at most one square pixel: a segment input kept sub-pixel under its own display layer.
- await _focus_in_place_of_click(page, selector, exc, focus_fallback=focus_fallback)
- return True, None, True
+ return True, None, await _focus_in_place_of_click(page, selector, exc, focus_fallback=focus_fallback)
try:
await page.wait_for_load_state("domcontentloaded", timeout=1000)
except Exception:
@@ -10027,14 +10093,14 @@ def build_browser_tools(
# The wrapper was a link and the click followed it. The selector may well match
# something on the destination, so typing now would put the text somewhere nobody
# asked for.
- return False, occluder, False
+ return False, occluder, "click"
try:
# The click may have remounted or hidden the field -- a wrapper that swaps its input
# on click is an ordinary SPA shape. fill() would wait its own full timeout for a
# node that is gone or invisible, which is the cost this whole path exists to avoid.
await page.wait_for_selector(selector, state="visible", timeout=1200)
except Exception:
- return False, occluder, False
+ return False, occluder, "click"
else:
try:
await page.click(selector, timeout=15000)
@@ -10042,8 +10108,7 @@ def build_browser_tools(
# A segmented control can keep its real input off-viewport with tabindex=-1 under an
# aria-hidden display layer: the probe finds nothing on top of it, but the click's
# hit-test has no point to land on.
- await _focus_in_place_of_click(page, selector, exc, focus_fallback=focus_fallback)
- focused_without_click = True
+ reach = await _focus_in_place_of_click(page, selector, exc, focus_fallback=focus_fallback)
try:
focused = await page.evaluate(_ACTIVE_IS_JS, await _probe_arg(page, selector))
except Exception:
@@ -10054,7 +10119,7 @@ def build_browser_tools(
# focus() needs no hit target, so it repairs a skin that swallowed the click without
# forwarding it. Typing then goes to the field rather than wherever the caret was.
await page.focus(selector, timeout=15000)
- return True, None, focused_without_click
+ return True, None, reach
def _occluder_labels_hold(occluder: dict[str, Any] | None, value: str) -> bool:
# The covering layer IS the committed-selection surface only when its own accessible naming
@@ -10544,7 +10609,7 @@ def build_browser_tools(
async def _type_and_commit(
page: Any, selector: str, value: str, rounds: int, *, focus_fallback: bool = False
- ) -> tuple[_TypeaheadPick, str | None, str | None, bool]:
+ ) -> tuple[_TypeaheadPick, str | None, str | None, _Reach]:
# Keystroke-type (so a widget's async suggestion fetch fires on real key events). Snapshot the
# visible DOM BEFORE the focus click, not just before typing: a widget that opens its full list on
# focus and then filters it in place keeps the same row nodes, so a snapshot taken after the click
@@ -10565,7 +10630,7 @@ def build_browser_tools(
LOG.info("taskv3 typeahead pre-snapshot failed; skipping suggestion probe", selector=selector)
# The focus-only path tells a typeahead from a plain field by rows reacting to the typing, which
# needs the pre-snapshot; without one it keeps today's error.
- focused, occluder, focused_without_click = await _focus_for_typing(
+ focused, occluder, reach = await _focus_for_typing(
page, selector, focus_fallback=focus_fallback and presnapshot_ok
)
if not focused:
@@ -10579,7 +10644,7 @@ def build_browser_tools(
pass
await page.fill(selector, "", timeout=15000)
await page.type(selector, value, delay=15, timeout=15000)
- if not presnapshot_ok or focused_without_click:
+ if not presnapshot_ok or reach != "click":
# Without the pre-snapshot the reaction-gate can't tell a new suggestion from static page
# text, so don't run the finder ungated (it could click unrelated content) — leave the typed
# value and let the caller re-observe. A field no click can reach gets no suggestion clicks
@@ -10588,10 +10653,10 @@ def build_browser_tools(
_TypeaheadPick(None, None, False, None, clicked=False, declared=False),
pre_value,
pre_own,
- focused_without_click,
+ reach,
)
pick = await _commit_typeahead(page, selector, value, rounds, pre_own=pre_own)
- return pick, pre_value, pre_own, False
+ return pick, pre_value, pre_own, reach
async def _close_lingering_typeahead_list(
page: Any, selector: str, committed: str | None, *, surface_vouched_pre_click: bool = False
@@ -10763,7 +10828,7 @@ def build_browser_tools(
if not await _anchor_typeable(page, selector) and await _anchor_has_list_semantics(page, selector):
return await _open_observe_pick(page, selector, text)
opened_by_typing = await _list_opened_on_an_empty_field(page, selector)
- result = await _type_typeahead_commit(page, selector, text)
+ result = await _type_typeahead_commit(page, selector, text, text_is_secret=text != args.get("text", ""))
return await _close_own_list_on_exit(page, selector, result, opened_by_typing=opened_by_typing)
# The types that skip the typeahead probe still must not be typed into through an overlay.
# They reach fill()/type(), which do no hit-testing, so nothing here would fail on its own --
@@ -10782,27 +10847,75 @@ def build_browser_tools(
await page.press(selector, "Enter")
return ToolResult.ok(f"typed into {selector}")
- async def _type_typeahead_commit(page: Any, selector: str, text: str) -> ToolResult:
+ async def _value_changed_by_page_error(
+ page: Any, selector: str, typed: str, held: str, *, echo: bool
+ ) -> ToolResult:
+ # The page committed a value of its own ("3" -> "03", "13" -> "12"). Whether that is the value the
+ # task needs is the model's call, so the field is left as the page set it and both values are
+ # reported; only an exact read-back is ever a success.
+ if echo:
+ try:
+ echo = not await page.locator(selector).first.evaluate(_FIELD_VALUE_IS_SECRET_JS, timeout=2000)
+ except Exception:
+ echo = False
+ if not echo:
+ what = "a different value than the one typed"
+ verdict = "Re-observe it to see whether that value is the one the task needs"
+ else:
+ shown = _mask_refs(held if len(held) <= _CHANGED_VALUE_ECHO_MAX else held[:_CHANGED_VALUE_ECHO_MAX] + "…")
+ what = f"'{shown}' (you typed '{_mask_refs(typed)}')"
+ verdict = (
+ f"If '{shown}' is the value the task needs, the field is filled; do not type it again. If it is "
+ "not, the page did not accept the text as typed"
+ )
+ return ToolResult.error(
+ f"typed into {selector}; after focus left the field it holds {what}. The page changed it, and it "
+ f"was left as the page set it. {verdict}.",
+ data={"release_own_list": True},
+ error_class="value_changed_by_page",
+ )
+
+ async def _type_typeahead_commit(page: Any, selector: str, text: str, *, text_is_secret: bool) -> ToolResult:
# keystroke-type (via _type_and_commit) so a widget that fetches suggestions on key events —
# not just on a single `input` from fill — still surfaces them, then commit the best match.
try:
- pick, pre_value, _pre_own, focused_without_click = await _type_and_commit(
+ pick, pre_value, _pre_own, reach = await _type_and_commit(
page, selector, text, rounds=3, focus_fallback=True
)
except _FieldCovered as exc:
return _covered_error(exc.selector, exc.occluder)
except _FieldNotEditable as exc:
return _not_editable_error(exc)
- if focused_without_click:
+ if reach != "click":
# focus() without a click cannot show the field took the keystrokes: a segmented control
# may move the caret to a sibling segment or drop the keys. So only a read-back that holds
# the typed text counts as filled; anything else, including an unreadable field, is an
# error, never a success. Rows reacting to the typing mean the field commits a picked
# suggestion, which this path never clicks, so its raw text is no fill either. The poll's
# wait also lets a widget that clears a rejected entry on a timer do so before the read.
- # The value is not echoed: typed text may be a resolved secret.
reacted = await _await_suggestion_rows(page, selector, text, rounds=8, any_region=True) is not None
- if not reacted and _typed_text_landed(await _read_field_value(page, selector), text):
+ if not reacted and reach == "point":
+ # Look for a slow list BEFORE leaving the field: Tab closes it. Tab is what commits a
+ # segment widget's assembled value, so the read-back comes after it.
+ await asyncio.sleep(0.3)
+ reacted = await _find_suggestion_rows(page, selector, text, any_region=True) is not None
+ if not reacted:
+ try:
+ await _current_page().keyboard.press("Tab")
+ except Exception:
+ pass
+ held = await _read_field_value(page, selector)
+ landed = _typed_text_landed(held, text)
+ LOG.info(
+ "taskv3 type coordinate click fallback", landed=landed, page_changed=bool(held) and not landed
+ )
+ if landed:
+ return ToolResult.ok(f"typed into {selector}")
+ if held:
+ # No restore: Tab already committed the page's value to the widget, and fill() fires no
+ # blur, so taking the input back would leave the input and the widget disagreeing.
+ return await _value_changed_by_page_error(page, selector, text, held, echo=not text_is_secret)
+ elif not reacted and _typed_text_landed(await _read_field_value(page, selector), text):
# The read-back costs a pause anyway, so spend it looking once more: a list slower than
# the poll would otherwise read as "no list" while its uncommitted query sits in the
# field. Slower than this is a bounded residual, not something a longer wait fixes.
@@ -10812,8 +10925,9 @@ def build_browser_tools(
await _restore_pre_type_value(page, selector, pre_value, [text])
return ToolResult.error(
f"typed into {selector}, but it does not hold the typed text afterwards — the field is NOT "
- "filled and may hold part of it. It sits outside the viewport and could only be focused, "
- "not clicked; re-observe and fill it through the control the page shows instead",
+ "filled and may hold part of it. It sits outside the viewport and could only be "
+ + ("clicked at its position" if reach == "point" else "focused, not clicked")
+ + "; re-observe and fill it through the control the page shows instead",
data={"release_own_list": True},
)
if pick.suggestion is None and pick.candidates:
diff --git a/skyvern/schemas/workflows.py b/skyvern/schemas/workflows.py
index 55ccd8c84..d33c54d92 100644
--- a/skyvern/schemas/workflows.py
+++ b/skyvern/schemas/workflows.py
@@ -698,6 +698,7 @@ class BitwardenLoginCredentialParameterYAML(ParameterYAML):
bitwarden_collection_id: str | None = None
# bitwarden item id to request the login credential
bitwarden_item_id: str | None = None
+ totp_identifier: str | None = None
class CredentialParameterYAML(ParameterYAML):
@@ -749,6 +750,7 @@ class OnePasswordCredentialParameterYAML(ParameterYAML):
vault_id: str
item_id: str
totp_field_name: str | None = None
+ totp_identifier: str | None = None
class AzureVaultCredentialParameterYAML(ParameterYAML):
diff --git a/skyvern/services/script_service.py b/skyvern/services/script_service.py
index d24af8cf1..d65218d9c 100644
--- a/skyvern/services/script_service.py
+++ b/skyvern/services/script_service.py
@@ -692,12 +692,15 @@ async def _create_workflow_block_run_and_task(
# (e.g. file upload) can find URLs like resume_link in the payload,
# plus the current loop value so a fallback search uses the intended value.
nav_payload = _build_fallback_navigation_payload(context)
+ terminate_criterion, error_code_mapping = await _resolve_block_termination_config(label)
task = await app.DATABASE.tasks.create_task(
# fix HACK: changed the type of url to str | None to support None url. url is not used in the script right now.
url=url or "",
title=f"Script {block_type.value} task",
navigation_goal=prompt,
complete_criterion=None,
+ terminate_criterion=terminate_criterion,
+ error_code_mapping=error_code_mapping,
data_extraction_goal=prompt if block_type == BlockType.EXTRACTION else None,
extracted_information_schema=schema,
navigation_payload=nav_payload,
@@ -928,6 +931,7 @@ async def _handle_script_termination(
step_status=StepStatus.failed,
label=cache_key,
failure_reason=str(e),
+ user_defined_errors=e.user_defined_errors,
)
@@ -943,6 +947,7 @@ async def _update_workflow_block(
failure_reason: str | None = None,
output: dict[str, Any] | list | str | None = None,
ai_fallback_triggered: bool | None = None,
+ user_defined_errors: list[UserDefinedError] | None = None,
) -> None:
"""Update workflow_run_block status, optionally setting `script_run`.
@@ -999,6 +1004,7 @@ async def _update_workflow_block(
status=task_status,
failure_reason=failure_reason,
extracted_information=output,
+ errors=[error.model_dump() for error in user_defined_errors] if user_defined_errors else None,
)
downloaded_files: list[FileInfo] = []
try:
@@ -2176,6 +2182,37 @@ def _find_block_definition(blocks: list[Any], label: str) -> Any | None:
return None
+async def _resolve_block_termination_config(label: str | None) -> tuple[str | None, dict[str, str] | None]:
+ context = skyvern_context.current()
+ if (
+ not label
+ or not context
+ or not context.workflow_id
+ or not context.organization_id
+ or not context.workflow_run_id
+ ):
+ return None, None
+ workflow = await app.DATABASE.workflows.get_workflow(
+ workflow_id=context.workflow_id, organization_id=context.organization_id
+ )
+ if not workflow:
+ return None, None
+ block = _find_block_definition(workflow.workflow_definition.blocks, label)
+ if not isinstance(block, BaseTaskBlock):
+ return None, None
+ workflow_run_context = app.WORKFLOW_CONTEXT_MANAGER.get_workflow_run_context(context.workflow_run_id)
+ criterion = block.terminate_criterion
+ if criterion:
+ criterion = block.render_templatable_field("terminate_criterion", criterion, workflow_run_context)
+ mapping = block.error_code_mapping
+ workflow_mapping = workflow.workflow_definition.error_code_mapping
+ if mapping or workflow_mapping:
+ mapping = block._render_error_code_mapping(
+ mapping, workflow_mapping, workflow_run_context, for_generated_code=False
+ )
+ return criterion, mapping
+
+
async def _resolve_block_otp_config(
label: str | None,
totp_identifier: str | None,
diff --git a/skyvern/utils/phone_validation.py b/skyvern/utils/phone_validation.py
new file mode 100644
index 000000000..a71d85ebc
--- /dev/null
+++ b/skyvern/utils/phone_validation.py
@@ -0,0 +1,60 @@
+import re
+
+from skyvern.utils.email_validation import SAFE_EMAIL_ADDRESS_PATTERN, normalize_email_address
+
+PHONE_IDENTIFIER_PATTERN = re.compile(r"^\+?[0-9()\-\.\s]+$")
+E164_PHONE_NUMBER_PATTERN = re.compile(r"^\+[1-9]\d{6,14}$")
+TWILIO_PHONE_NUMBER_SID_PATTERN = re.compile(r"PN[0-9a-fA-F]{32}")
+REPEATED_PHONE_PUNCTUATION_PATTERN = re.compile(r"[().-]{2,}")
+MIN_PHONE_IDENTIFIER_DIGITS = 7
+MAX_PHONE_IDENTIFIER_DIGITS = 15
+
+
+def looks_like_phone_identifier(value: str) -> bool:
+ stripped_value = value.strip()
+ if SAFE_EMAIL_ADDRESS_PATTERN.fullmatch(stripped_value):
+ return False
+ digits = re.sub(r"\D", "", stripped_value)
+ return (
+ PHONE_IDENTIFIER_PATTERN.fullmatch(stripped_value) is not None
+ and MIN_PHONE_IDENTIFIER_DIGITS <= len(digits) <= MAX_PHONE_IDENTIFIER_DIGITS
+ and REPEATED_PHONE_PUNCTUATION_PATTERN.search(stripped_value) is None
+ )
+
+
+def normalize_phone_identifier(value: str) -> str:
+ stripped_value = value.strip()
+ if not looks_like_phone_identifier(stripped_value):
+ return stripped_value
+ digits = re.sub(r"\D", "", stripped_value)
+ if stripped_value.startswith("+"):
+ return f"+{digits}"
+ if len(digits) == 11 and digits.startswith("1"):
+ return f"+{digits}"
+ return stripped_value
+
+
+def is_e164_phone_number(value: str) -> bool:
+ return E164_PHONE_NUMBER_PATTERN.fullmatch(value) is not None
+
+
+def is_twilio_phone_number_sid(value: str) -> bool:
+ return isinstance(value, str) and TWILIO_PHONE_NUMBER_SID_PATTERN.fullmatch(value) is not None
+
+
+def phone_identifier_candidates(value: str) -> list[str]:
+ stripped_value = value.strip()
+ if not looks_like_phone_identifier(stripped_value):
+ return [stripped_value]
+ digits = re.sub(r"\D", "", stripped_value)
+ normalized = normalize_phone_identifier(stripped_value)
+ return sorted({stripped_value, normalized, digits})
+
+
+def normalize_identifier(value: str) -> str:
+ stripped_value = value.strip()
+ if SAFE_EMAIL_ADDRESS_PATTERN.fullmatch(stripped_value):
+ return normalize_email_address(stripped_value)
+ if looks_like_phone_identifier(stripped_value):
+ return normalize_phone_identifier(stripped_value)
+ return stripped_value
diff --git a/skyvern/utils/url_validators.py b/skyvern/utils/url_validators.py
index 995982f45..bbf5af2cc 100644
--- a/skyvern/utils/url_validators.py
+++ b/skyvern/utils/url_validators.py
@@ -58,6 +58,37 @@ def strip_query_params(url: str) -> str:
return f"{parsed.scheme}://{host}{port_str}{parsed.path}"
+def redact_url_query(url: str) -> str:
+ """Remove the query string while preserving the other URL components."""
+ parsed = urlsplit(url)
+ if not parsed.query:
+ return url
+ return urlunsplit(parsed._replace(query=""))
+
+
+def redact_url_for_display(url: str | None) -> str | None:
+ """Remove URL secrets while preserving enough routing context for display."""
+ if not url:
+ return url
+
+ try:
+ parsed = urlsplit(url)
+ hostname = parsed.hostname
+ port = parsed.port
+ except ValueError:
+ return "[invalid URL]"
+
+ if not parsed.scheme or not hostname:
+ return "[invalid URL]"
+
+ display_host = f"[{hostname}]" if ":" in hostname else hostname
+ if port is not None:
+ display_host = f"{display_host}:{port}"
+ path_marker = parsed.path if parsed.path in {"", "/"} else "/…"
+ query_marker = "?…" if "?" in url.partition("#")[0] else ""
+ return f"{parsed.scheme}://{display_host}{path_marker}{query_marker}"
+
+
def collapse_duplicate_www_prefix(url: str) -> str:
try:
parts = urlsplit(url)
diff --git a/tests/unit/forge/sdk/db/test_sms_repository.py b/tests/unit/forge/sdk/db/test_sms_repository.py
new file mode 100644
index 000000000..4dfab4c55
--- /dev/null
+++ b/tests/unit/forge/sdk/db/test_sms_repository.py
@@ -0,0 +1,888 @@
+import importlib.util
+import sqlite3
+from contextlib import contextmanager
+from datetime import datetime, timedelta, timezone
+from pathlib import Path
+from types import SimpleNamespace
+from typing import Any, cast
+from unittest.mock import AsyncMock
+
+import pytest
+from asyncpg.exceptions import UniqueViolationError
+from sqlalchemy import event, select
+from sqlalchemy.exc import IntegrityError
+from sqlalchemy.ext.asyncio import AsyncSession
+
+from skyvern.forge.sdk.db.agent_db import AgentDB
+from skyvern.forge.sdk.db.enums import OrganizationAuthTokenType
+from skyvern.forge.sdk.db.models import (
+ CredentialModel,
+ OrganizationAuthTokenModel,
+ OrganizationPhoneNumberModel,
+ OrganizationSMSConfigModel,
+ TOTPCodeModel,
+)
+from skyvern.forge.sdk.db.repositories import organizations as organizations_repository_module
+from skyvern.forge.sdk.db.repositories import sms as sms_repository_module
+from skyvern.forge.sdk.encrypt.base import EncryptMethod
+from skyvern.forge.sdk.schemas.organizations import TwilioCredential
+from skyvern.forge.sdk.schemas.totp_codes import OTPType
+
+
+class _FakeEncryptor:
+ async def encrypt(self, plaintext: str, method: object) -> str:
+ assert getattr(method, "value", None) == "aes"
+ return f"encrypted::{plaintext}"
+
+ async def decrypt(self, ciphertext: str, method: object) -> str:
+ assert getattr(method, "value", None) == "aes"
+ return ciphertext.removeprefix("encrypted::")
+
+
[email protected]("driver", ["sqlite", "psycopg", "asyncpg", "asyncpg-direct"])
[email protected]("matches", [True, False])
+def test_phone_number_unique_violation_uses_driver_constraint_metadata(driver: str, matches: bool) -> None:
+ constraint = "uq_org_phone_numbers_org_number" if matches else "unrelated_unique_constraint"
+ original: Exception
+ if driver == "sqlite":
+ original = sqlite3.IntegrityError(
+ "UNIQUE constraint failed: organization_phone_numbers.organization_id, organization_phone_numbers.phone_number"
+ if matches
+ else "UNIQUE constraint failed: organization_phone_numbers.phone_number_id"
+ )
+ elif driver == "psycopg":
+ original = Exception("duplicate key")
+ original.diag = SimpleNamespace(constraint_name=constraint) # type: ignore[attr-defined]
+ else:
+ violation = UniqueViolationError("duplicate key")
+ violation.constraint_name = constraint
+ original = violation if driver == "asyncpg-direct" else Exception("adapted driver error")
+ if driver == "asyncpg":
+ original.__cause__ = violation
+ assert sms_repository_module._is_phone_number_unique_violation(IntegrityError("INSERT", {}, original)) is matches
+
+
+async def test_reactivating_phone_with_credential_reports_duplicate(agent_db: AgentDB) -> None:
+ config, _ = await agent_db.sms.create_sms_config("o_test", "manual", "secret-token")
+ async with agent_db.Session() as session:
+ session.add(
+ CredentialModel(
+ credential_id="cred_test", organization_id="o_test", name="Test", credential_type="password"
+ )
+ )
+ await session.commit()
+ original = await agent_db.sms.create_phone_number("o_test", config.sms_config_id, "+14155552671")
+ await agent_db.sms.update_phone_number(original.phone_number_id, "o_test", status="disabled")
+ await agent_db.sms.create_phone_number("o_test", config.sms_config_id, "+14155552671")
+ with pytest.raises(sms_repository_module.PhoneNumberAlreadyExists):
+ await agent_db.sms.update_phone_number(
+ original.phone_number_id, "o_test", status="active", credential_id="cred_test"
+ )
+ unchanged = await agent_db.sms.get_phone_number(original.phone_number_id, "o_test")
+ assert unchanged is not None and unchanged.status == "disabled" and unchanged.credential_id is None
+
+
+async def test_otp_lookup_preserves_exact_legacy_whitespace_identifier(agent_db: AgentDB) -> None:
+ identifier = " +14155552671 "
+ created = await agent_db.otp.create_otp_code(
+ organization_id="o_test",
+ totp_identifier=identifier,
+ content="123456",
+ code="123456",
+ otp_type=OTPType.TOTP,
+ )
+ results = await agent_db.otp.get_otp_codes(organization_id="o_test", totp_identifier=identifier)
+ assert [result.totp_code_id for result in results] == [created.totp_code_id]
+
+
[email protected](autouse=True)
+def fake_sms_encryptor(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(sms_repository_module, "encryptor", _FakeEncryptor())
+
+
+async def test_twilio_org_credentials_default_to_aes_and_reject_other_methods(
+ agent_db: AgentDB,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr(organizations_repository_module, "encryptor", _FakeEncryptor())
+ monkeypatch.setattr("skyvern.forge.sdk.db.utils.encryptor", _FakeEncryptor())
+ credential = TwilioCredential(account_sid="AC" + "0" * 32, auth_token="TWILIO_AUTH_SENTINEL")
+
+ stored = await agent_db.organizations.replace_org_auth_token(
+ "o_test",
+ OrganizationAuthTokenType.twilio_credential,
+ credential,
+ )
+
+ assert stored.credential == credential
+ async with agent_db.Session() as session:
+ row = await session.scalar(
+ select(OrganizationAuthTokenModel).where(
+ OrganizationAuthTokenModel.organization_id == "o_test",
+ OrganizationAuthTokenModel.token_type == OrganizationAuthTokenType.twilio_credential,
+ )
+ )
+ assert row is not None
+ assert row.token == ""
+ assert row.encrypted_method == EncryptMethod.AES.value
+ assert row.encrypted_token == f"encrypted::{credential.model_dump_json()}"
+
+ with pytest.raises(ValueError, match="AES"):
+ await agent_db.organizations.replace_org_auth_token(
+ "o_test",
+ OrganizationAuthTokenType.twilio_credential,
+ credential,
+ encrypted_method=cast(EncryptMethod, object()),
+ )
+
+
+async def test_generic_twilio_auth_token_writers_reject_plaintext(
+ agent_db: AgentDB, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.setattr(organizations_repository_module, "encryptor", _FakeEncryptor())
+ monkeypatch.setattr("skyvern.forge.sdk.db.utils.encryptor", _FakeEncryptor())
+ credential = TwilioCredential(account_sid="AC" + "0" * 32, auth_token="TWILIO_AUTH_SENTINEL")
+ stored = await agent_db.organizations.replace_org_auth_token(
+ "o_test", OrganizationAuthTokenType.twilio_credential, credential
+ )
+ serialized_credential = credential.model_dump_json()
+
+ with pytest.raises(ValueError, match="replace_org_auth_token"):
+ await agent_db.organizations.create_org_auth_token(
+ "o_test", OrganizationAuthTokenType.twilio_credential, serialized_credential
+ )
+ with pytest.raises(ValueError, match="replace_org_auth_token"):
+ await agent_db.organizations.update_org_auth_token(
+ "o_test", OrganizationAuthTokenType.twilio_credential, stored.id, serialized_credential
+ )
+
+ async with agent_db.Session() as session:
+ rows = (
+ await session.scalars(
+ select(OrganizationAuthTokenModel).where(
+ OrganizationAuthTokenModel.organization_id == "o_test",
+ OrganizationAuthTokenModel.token_type == OrganizationAuthTokenType.twilio_credential,
+ )
+ )
+ ).all()
+ assert len(rows) == 1
+ assert rows[0].token == ""
+ assert rows[0].encrypted_token == f"encrypted::{serialized_credential}"
+
+
+async def test_invalid_sms_config_mode_leaves_no_poisoned_row(agent_db: AgentDB) -> None:
+ created, _ = await agent_db.sms.create_sms_config("o_test", "manual", "secret-token")
+ with pytest.raises(sms_repository_module.InvalidSMSConfigMode, match="Invalid SMS config mode"):
+ await agent_db.sms.create_sms_config("o_test", "bogus", "secret-token")
+ assert await agent_db.sms.list_sms_configs("o_test") == [created]
+ assert await agent_db.sms.get_sms_config(created.sms_config_id, "o_test") == created
+
+
[email protected]("operation", ["create", "update", "disable", "delete"])
+async def test_sms_timestamp_writes_are_naive_utc(agent_db: AgentDB, db_engine: Any, operation: str) -> None:
+ writes: list[dict[str, datetime]] = []
+
+ def capture_timestamps(_conn: Any, _cursor: Any, _sql: Any, _parameters: Any, context: Any, _many: Any) -> None:
+ if context.isinsert or context.isupdate:
+ for parameters in context.compiled_parameters:
+ writes.append({key: value for key, value in parameters.items() if isinstance(value, datetime)})
+
+ event.listen(db_engine.sync_engine, "before_cursor_execute", capture_timestamps)
+ config, _ = await agent_db.sms.create_sms_config("o_test", "manual", "secret-token")
+ phone = await agent_db.sms.create_phone_number("o_test", config.sms_config_id, "+14155552671")
+ timestamp = datetime(2026, 9, 10, 15, tzinfo=timezone(timedelta(hours=5, minutes=30)))
+ expected = datetime(2026, 9, 10, 9, 30)
+ if operation == "update":
+ await agent_db.sms.update_phone_number(phone.phone_number_id, "o_test", quarantined_until=timestamp)
+ elif operation == "disable":
+ await agent_db.sms.update_phone_number(phone.phone_number_id, "o_test", status="disabled")
+ elif operation == "delete":
+ await agent_db.sms.delete_sms_config(
+ (await agent_db.sms.create_sms_config("o_test", "manual", "other-secret"))[0].sms_config_id, "o_test"
+ )
+
+ assert {"created_at", "modified_at"} <= {key for write in writes for key in write}
+ for write in writes:
+ for key, value in write.items():
+ assert value.tzinfo is None, f"{operation}: {key} must be naive UTC before SQL encoding"
+ async with agent_db.Session() as session:
+ rows = [
+ *(await session.scalars(select(OrganizationSMSConfigModel))).all(),
+ *(await session.scalars(select(OrganizationPhoneNumberModel))).all(),
+ ]
+ for row in rows:
+ for column in row.__table__.columns:
+ value = getattr(row, column.name)
+ if isinstance(value, datetime):
+ assert value.tzinfo is None, f"{column.name} must persist as naive UTC"
+ if operation == "update":
+ loaded = await agent_db.sms.get_phone_number(phone.phone_number_id, "o_test")
+ assert loaded is not None and loaded.quarantined_until == expected
+ await agent_db.sms.update_phone_number(phone.phone_number_id, "o_test", quarantined_until=expected)
+ await agent_db.sms.update_phone_number(phone.phone_number_id, "o_test", quarantined_until=None)
+ loaded = await agent_db.sms.get_phone_number(phone.phone_number_id, "o_test")
+ assert loaded is not None and loaded.quarantined_until is None
+ if operation == "delete":
+ assert any("deleted_at" in write for write in writes)
+
+
+async def test_sms_config_create_get_list_and_delete(agent_db: AgentDB) -> None:
+ created, plaintext_secret = await agent_db.sms.create_sms_config(
+ organization_id="o_test",
+ mode="manual",
+ webhook_secret="secret-token",
+ daily_ingest_cap=25,
+ )
+
+ assert plaintext_secret == "secret-token"
+ assert created.organization_id == "o_test"
+ assert created.mode == "manual"
+ assert created.daily_ingest_cap == 25
+ assert not hasattr(created, "webhook_secret")
+ assert not hasattr(created, "encrypted_webhook_secret")
+
+ async with agent_db.Session() as session:
+ stored = await session.scalar(
+ select(OrganizationSMSConfigModel).where(OrganizationSMSConfigModel.sms_config_id == created.sms_config_id)
+ )
+ assert stored is not None
+ assert stored.encrypted_webhook_secret == "encrypted::secret-token"
+ assert stored.webhook_secret_encrypted_method == "aes"
+
+ loaded = await agent_db.sms.get_sms_config_with_secret(
+ organization_id="o_test", sms_config_id=created.sms_config_id
+ )
+ assert loaded == (created, "secret-token")
+ assert await agent_db.sms.get_sms_config_with_secret_for_inbound(sms_config_id=created.sms_config_id) == loaded
+ assert (
+ await agent_db.sms.get_sms_config_with_secret(organization_id="o_other", sms_config_id=created.sms_config_id)
+ is None
+ )
+ assert await agent_db.sms.get_sms_config(created.sms_config_id, "o_test") == created
+ assert await agent_db.sms.list_sms_configs("o_test") == [created]
+
+ await agent_db.sms.delete_sms_config(created.sms_config_id, "o_test")
+
+ assert (
+ await agent_db.sms.get_sms_config_with_secret(organization_id="o_test", sms_config_id=created.sms_config_id)
+ is None
+ )
+ assert await agent_db.sms.get_sms_config_with_secret_for_inbound(sms_config_id=created.sms_config_id) is None
+ assert await agent_db.sms.get_sms_config(created.sms_config_id, "o_test") is None
+ assert await agent_db.sms.get_sms_config(created.sms_config_id, "o_test", include_deleted=True) == created
+ assert await agent_db.sms.list_sms_configs("o_test") == []
+
+
+async def test_create_connected_sms_config_conflict_returns_existing_winner(agent_db: AgentDB) -> None:
+ winner, winner_secret = await agent_db.sms.create_sms_config(
+ organization_id="o_test",
+ mode="connected",
+ webhook_secret="winner-secret",
+ )
+
+ conflicted, conflicted_secret = await agent_db.sms.create_sms_config(
+ organization_id="o_test",
+ mode="connected",
+ webhook_secret="loser-secret",
+ )
+
+ assert winner_secret == "winner-secret"
+ assert conflicted == winner
+ assert conflicted_secret == "winner-secret"
+ assert await agent_db.sms.get_connected_sms_config("o_test") == (winner, "winner-secret")
+ assert await agent_db.sms.list_sms_configs("o_test") == [winner]
+
+
+async def test_phone_number_invalid_status_leaves_row_unchanged(agent_db: AgentDB) -> None:
+ config, _ = await agent_db.sms.create_sms_config(
+ organization_id="o_test", mode="manual", webhook_secret="secret-token"
+ )
+ created = await agent_db.sms.create_phone_number(
+ organization_id="o_test", sms_config_id=config.sms_config_id, phone_number="+14155552671"
+ )
+
+ with pytest.raises(sms_repository_module.InvalidPhoneNumberStatus, match="Invalid phone number status"):
+ await agent_db.sms.update_phone_number(created.phone_number_id, "o_test", status="bogus")
+
+ assert await agent_db.sms.get_phone_number(created.phone_number_id, "o_test") == created
+ assert await agent_db.sms.list_phone_numbers("o_test") == [created]
+
+
+async def test_phone_number_uniqueness_lookup_updates_and_active_count(agent_db: AgentDB) -> None:
+ config, _ = await agent_db.sms.create_sms_config(
+ organization_id="o_test",
+ mode="manual",
+ webhook_secret="secret-token",
+ )
+ other_config, _ = await agent_db.sms.create_sms_config(
+ organization_id="o_other",
+ mode="manual",
+ webhook_secret="other-secret-token",
+ )
+ async with agent_db.Session() as session:
+ session.add(
+ CredentialModel(
+ credential_id="cred_other",
+ organization_id="o_other",
+ name="Other credential",
+ credential_type="password",
+ )
+ )
+ await session.commit()
+ created = await agent_db.sms.create_phone_number(
+ organization_id="o_test",
+ sms_config_id=config.sms_config_id,
+ phone_number="+14155552671",
+ provider_number_sid="PN_original",
+ previous_sms_url="https://example.test/original",
+ previous_sms_method="GET",
+ previous_sms_application_sid="AP_original",
+ )
+
+ assert await agent_db.sms.get_phone_number_by_number("+14155552671", "o_test") == created
+ assert await agent_db.sms.get_phone_number_by_number("+14155552671", "o_other") is None
+
+ other_org_number = await agent_db.sms.create_phone_number(
+ organization_id="o_other",
+ sms_config_id=other_config.sms_config_id,
+ phone_number="+14155552671",
+ )
+ assert await agent_db.sms.get_phone_number_by_number("+14155552671", "o_other") == other_org_number
+
+ with pytest.raises(ValueError, match="does not belong"):
+ await agent_db.sms.create_phone_number(
+ organization_id="o_test",
+ sms_config_id=other_config.sms_config_id,
+ phone_number="+14155552672",
+ )
+
+ with pytest.raises(ValueError, match="Credential does not belong"):
+ await agent_db.sms.create_phone_number(
+ organization_id="o_test",
+ sms_config_id=config.sms_config_id,
+ phone_number="+14155552673",
+ credential_id="cred_other",
+ )
+
+ with pytest.raises(sms_repository_module.PhoneNumberAlreadyExists, match="already registered"):
+ await agent_db.sms.create_phone_number(
+ organization_id="o_test",
+ sms_config_id=config.sms_config_id,
+ phone_number="+14155552671",
+ )
+
+ updated = await agent_db.sms.update_phone_number(
+ created.phone_number_id,
+ "o_test",
+ status="disabled",
+ provider_number_sid="PN_refreshed",
+ previous_sms_method="POST",
+ previous_sms_application_sid="AP_refreshed",
+ )
+ assert updated is not None
+ assert updated.status == "disabled"
+ assert updated.provider_number_sid == "PN_refreshed"
+ assert updated.previous_sms_method == "POST"
+ assert updated.previous_sms_application_sid == "AP_refreshed"
+
+ replacement = await agent_db.sms.create_phone_number(
+ organization_id="o_test",
+ sms_config_id=config.sms_config_id,
+ phone_number="+14155552671",
+ )
+ assert replacement.status == "active"
+ with pytest.raises(sms_repository_module.PhoneNumberAlreadyExists, match="already registered"):
+ await agent_db.sms.update_phone_number(created.phone_number_id, "o_test", status="active")
+ assert await agent_db.sms.count_active_phone_numbers_for_config(config.sms_config_id, "o_test") == 1
+ assert await agent_db.sms.count_active_phone_numbers_for_config(config.sms_config_id, "o_other") == 0
+ with pytest.raises(ValueError, match="Credential does not belong"):
+ await agent_db.sms.update_phone_number(
+ replacement.phone_number_id,
+ "o_test",
+ credential_id="cred_other",
+ )
+ assert await agent_db.sms.count_active_phone_numbers_for_config(other_config.sms_config_id, "o_other") == 1
+ assert await agent_db.sms.get_phone_number_by_number("+1 (415) 555-2671", "o_test") == replacement
+ assert await agent_db.sms.get_phone_number_by_number("+1-EXT-415-555-2671", "o_test") is None
+
+
+async def test_delete_sms_config_rejects_active_phone_references(agent_db: AgentDB) -> None:
+ config, _ = await agent_db.sms.create_sms_config("o_test", "manual", "secret-token")
+ phone = await agent_db.sms.create_phone_number("o_test", config.sms_config_id, "+14155552671")
+ with pytest.raises(sms_repository_module.SMSConfigHasActivePhoneNumbers, match="active phone"):
+ await agent_db.sms.delete_sms_config(config.sms_config_id, "o_test")
+ assert await agent_db.sms.get_sms_config(config.sms_config_id, "o_test") == config
+ await agent_db.sms.update_phone_number(phone.phone_number_id, "o_test", status="disabled")
+ await agent_db.sms.delete_sms_config(config.sms_config_id, "o_test")
+ assert await agent_db.sms.get_sms_config(config.sms_config_id, "o_test") is None
+
+
+async def test_phone_number_create_normalizes_and_validates_e164(agent_db: AgentDB) -> None:
+ config, _ = await agent_db.sms.create_sms_config(
+ organization_id="o_test",
+ mode="manual",
+ webhook_secret="secret-token",
+ )
+
+ created = await agent_db.sms.create_phone_number(
+ organization_id="o_test",
+ sms_config_id=config.sms_config_id,
+ phone_number="+1 (415) 555-2671",
+ )
+
+ assert created.phone_number == "+14155552671"
+ with pytest.raises(ValueError, match="country code"):
+ await agent_db.sms.create_phone_number(
+ organization_id="o_test",
+ sms_config_id=config.sms_config_id,
+ phone_number="4155552672",
+ )
+ with pytest.raises(ValueError, match="country code"):
+ await agent_db.sms.create_phone_number(
+ organization_id="o_test",
+ sms_config_id=config.sms_config_id,
+ phone_number="+1-EXT-415-555-2673",
+ )
+
+
+ ("stored_identifier", "lookup_identifier"),
+ [
+ pytest.param("4155552671", "+14155552671", id="legacy-bare-row"),
+ pytest.param("+14155552671", "4155552671", id="e164-row-with-legacy-lookup"),
+ ],
+)
+async def test_otp_repository_does_not_cross_match_country_ambiguous_phone_identifiers(
+ agent_db: AgentDB,
+ stored_identifier: str,
+ lookup_identifier: str,
+) -> None:
+ async with agent_db.Session() as session:
+ session.add(
+ TOTPCodeModel(
+ totp_code_id="totp_phone_identifier",
+ organization_id="o_test",
+ totp_identifier=stored_identifier,
+ content="123456",
+ code="123456",
+ otp_type="totp",
+ parse_status="parsed",
+ )
+ )
+ await session.commit()
+
+ results = await agent_db.otp.get_otp_codes(
+ organization_id="o_test",
+ totp_identifier=lookup_identifier,
+ )
+
+ assert results == []
+
+
+async def test_if_new_otp_creators_return_none_for_duplicate_external_message_id(
+ agent_db: AgentDB,
+ caplog: pytest.LogCaptureFixture,
+) -> None:
+ created = await agent_db.otp.create_otp_code_if_new(
+ organization_id="o_test",
+ totp_identifier="ID_SENTINEL",
+ content="BODY_SENTINEL",
+ code="CODE_SENTINEL",
+ otp_type=OTPType.TOTP,
+ source="twilio",
+ external_message_id="MSG_PARSED_SENTINEL",
+ )
+ raw_created = await agent_db.otp.create_raw_otp_code_if_new(
+ organization_id="o_test",
+ totp_identifier="ID_SENTINEL",
+ content="BODY_SENTINEL",
+ source="twilio",
+ external_message_id="MSG_RAW_SENTINEL",
+ )
+ caplog.clear()
+ duplicate = await agent_db.otp.create_otp_code_if_new(
+ organization_id="o_test",
+ totp_identifier="ID_SENTINEL",
+ content="654321",
+ code="654321",
+ otp_type=OTPType.TOTP,
+ source="twilio",
+ external_message_id="MSG_PARSED_SENTINEL",
+ )
+ raw_duplicate = await agent_db.otp.create_raw_otp_code_if_new(
+ organization_id="o_test",
+ totp_identifier="ID_SENTINEL",
+ content="BODY_SENTINEL",
+ source="twilio",
+ external_message_id="MSG_RAW_SENTINEL",
+ )
+
+ assert created is not None
+ assert duplicate is None
+ assert raw_created is not None
+ assert raw_duplicate is None
+ assert not any(sentinel in caplog.text for sentinel in ("ID_SENTINEL", "BODY_SENTINEL", "CODE_SENTINEL"))
+
+
[email protected]("raw", [False, True], ids=["parsed", "raw"])
+async def test_if_new_otp_creators_reraise_non_dedupe_integrity_errors(
+ agent_db: AgentDB,
+ monkeypatch: pytest.MonkeyPatch,
+ raw: bool,
+) -> None:
+ monkeypatch.setattr(
+ AsyncSession,
+ "commit",
+ AsyncMock(side_effect=IntegrityError("INSERT", {}, Exception("foreign key constraint failed"))),
+ )
+ kwargs = {
+ "organization_id": "o_test",
+ "totp_identifier": "+14155552671",
+ "content": "123456",
+ "task_id": "tsk_missing",
+ "source": "twilio",
+ "external_message_id": f"SM_invalid_fk_{raw}",
+ }
+
+ with pytest.raises(IntegrityError) as exc_info:
+ if raw:
+ await agent_db.otp.create_raw_otp_code_if_new(**kwargs)
+ else:
+ await agent_db.otp.create_otp_code_if_new(
+ **kwargs,
+ code="123456",
+ otp_type=OTPType.TOTP,
+ )
+ assert exc_info.value.hide_parameters
+
+
+async def test_count_otp_codes_since_filters_source_and_counts_raw_rows(
+ agent_db: AgentDB, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ threshold = datetime.now(timezone.utc) - timedelta(hours=1)
+ common = {
+ "organization_id": "o_test",
+ "totp_identifier": "+14155552671",
+ }
+ await agent_db.otp.create_otp_code(
+ **common,
+ content="123456",
+ code="123456",
+ otp_type=OTPType.TOTP,
+ source="twilio",
+ )
+ await agent_db.otp.create_otp_code(
+ **common,
+ content="654321",
+ code="654321",
+ otp_type=OTPType.TOTP,
+ source="twilio",
+ )
+ await agent_db.otp.create_raw_otp_code(
+ **common,
+ content="Your code is 111222",
+ source="twilio",
+ )
+ await agent_db.otp.create_otp_code(
+ **common,
+ content="333444",
+ code="333444",
+ otp_type=OTPType.TOTP,
+ source="email",
+ )
+ async with agent_db.Session() as session:
+ session.add(
+ TOTPCodeModel(
+ totp_code_id="totp_before_threshold",
+ organization_id="o_test",
+ totp_identifier="+14155552671",
+ content="999000",
+ code="999000",
+ otp_type="totp",
+ parse_status="parsed",
+ source="twilio",
+ created_at=threshold - timedelta(seconds=1),
+ )
+ )
+ await session.commit()
+
+ original_scalar = AsyncSession.scalar
+
+ async def scalar_with_naive_timestamp(session: AsyncSession, statement: Any, **kwargs: Any) -> Any:
+ assert statement.compile().params["created_at_1"] == threshold.replace(tzinfo=None)
+ return await original_scalar(session, statement, **kwargs)
+
+ monkeypatch.setattr(AsyncSession, "scalar", scalar_with_naive_timestamp)
+ assert (
+ await agent_db.otp.count_otp_codes_since(
+ **common,
+ created_after=threshold,
+ source="twilio",
+ )
+ == 3
+ )
+ assert (
+ await agent_db.otp.count_otp_codes_since(
+ **common,
+ created_after=threshold,
+ source="email",
+ )
+ == 1
+ )
+ assert (
+ await agent_db.otp.count_otp_codes_since(
+ **common,
+ created_after=threshold,
+ )
+ == 4
+ )
+
+
+async def test_otp_repository_matches_seven_digit_phone_identifier_candidates(agent_db: AgentDB) -> None:
+ async with agent_db.Session() as session:
+ session.add_all(
+ [
+ TOTPCodeModel(
+ totp_code_id="totp_formatted_label",
+ organization_id="o_test",
+ totp_identifier="1234-567",
+ content="123456",
+ code="123456",
+ otp_type="totp",
+ parse_status="parsed",
+ ),
+ TOTPCodeModel(
+ totp_code_id="totp_digits_only",
+ organization_id="o_test",
+ totp_identifier="1234567",
+ content="654321",
+ code="654321",
+ otp_type="totp",
+ parse_status="parsed",
+ ),
+ ]
+ )
+ await session.commit()
+
+ results = await agent_db.otp.get_otp_codes(
+ organization_id="o_test",
+ totp_identifier="1234-567",
+ )
+
+ assert {result.totp_identifier for result in results} == {"1234-567", "1234567"}
+
+
+def _load_sms_migration() -> Any:
+ migrations = list((Path(__file__).resolve().parents[5] / "alembic/versions").glob("*_add_sms_2fa_foundation.py"))
+ assert len(migrations) == 1, f"Expected exactly one SMS foundation migration, found {len(migrations)}: {migrations}"
+ migration_path = migrations[0]
+ spec = importlib.util.spec_from_file_location("sms_foundation_migration", migration_path)
+ assert spec is not None
+ assert spec.loader is not None
+ migration = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(migration)
+ return migration
+
+
+class _FakeMigrationResult:
+ def __init__(self, value: object) -> None:
+ self.value = value
+
+ def scalar(self) -> object:
+ return self.value
+
+
+class _FakeMigrationBind:
+ class _Dialect:
+ name = "postgresql"
+
+ dialect = _Dialect()
+
+ def __init__(self, *, constraint_exists: bool = False, invalid_index: str | None = None) -> None:
+ self.constraint_exists = constraint_exists
+ self.invalid_index = invalid_index
+
+ def execute(self, statement: object, _parameters: object = None) -> _FakeMigrationResult:
+ sql = str(statement)
+ if "FROM pg_constraint" in sql:
+ return _FakeMigrationResult(1 if self.constraint_exists else None)
+ if "FROM pg_class" in sql:
+ return _FakeMigrationResult(self.invalid_index)
+ return _FakeMigrationResult(None)
+
+
+class _FakeMigrationContext:
+ # Track requested context placement only; this does not exercise PostgreSQL transactions.
+ in_autocommit = False
+
+ @contextmanager
+ def autocommit_block(self):
+ assert not self.in_autocommit
+ self.in_autocommit = True
+ try:
+ yield
+ finally:
+ self.in_autocommit = False
+
+
+class _FakeMigrationOp:
+ def __init__(
+ self,
+ *,
+ constraint_exists: bool = False,
+ invalid_index: str | None = None,
+ failure_statement: str | None = None,
+ failure: Exception | None = None,
+ ) -> None:
+ self.bind = _FakeMigrationBind(constraint_exists=constraint_exists, invalid_index=invalid_index)
+ self.context = _FakeMigrationContext()
+ self.statements: list[str] = []
+ self.calls: list[tuple[str, str]] = []
+ self.failure_statement = failure_statement
+ self.failure = failure or RuntimeError("migration operation failed")
+
+ def get_bind(self) -> _FakeMigrationBind:
+ return self.bind
+
+ def get_context(self) -> _FakeMigrationContext:
+ return self.context
+
+ def execute(self, statement: object) -> None:
+ sql = str(statement)
+ if "INDEX CONCURRENTLY" in sql:
+ assert self.context.in_autocommit, "Concurrent index DDL must request an autocommit block"
+ self.statements.append(sql)
+ if self.failure_statement and self.failure_statement in sql:
+ raise self.failure
+
+ def create_unique_constraint(self, name: str, _table: str, _columns: list[str]) -> None:
+ self.calls.append(("create_unique_constraint", name))
+
+ def create_table(self, name: str, *_args: object, **_kwargs: object) -> None:
+ self.calls.append(("create_table", name))
+
+ def create_index(self, name: str, _table: str, _columns: list[str], **_kwargs: object) -> None:
+ self.calls.append(("create_index", name))
+
+ def add_column(self, table: str, _column: object) -> None:
+ self.calls.append(("add_column", table))
+
+ def drop_column(self, table: str, column: str) -> None:
+ self.calls.append(("drop_column", f"{table}.{column}"))
+
+ def drop_index(self, name: str, **_kwargs: object) -> None:
+ self.calls.append(("drop_index", name))
+
+ def drop_table(self, name: str) -> None:
+ self.calls.append(("drop_table", name))
+
+ def drop_constraint(self, name: str, _table: str, **_kwargs: object) -> None:
+ self.calls.append(("drop_constraint", name))
+
+
+def test_sms_migration_upgrade_builds_credentials_constraint_from_concurrent_index(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ migration = _load_sms_migration()
+ fake_op = _FakeMigrationOp()
+ monkeypatch.setattr(migration, "op", fake_op)
+
+ migration.upgrade()
+
+ normalized = " ".join(" ".join(fake_op.statements).split())
+ assert "CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS uq_credentials_id_org" in normalized
+ assert (
+ 'ALTER TABLE "credentials" ADD CONSTRAINT "uq_credentials_id_org" UNIQUE USING INDEX "uq_credentials_id_org"'
+ in (normalized)
+ )
+ assert "SET LOCAL lock_timeout = '5s'" in normalized
+ assert not any(call[0] == "create_unique_constraint" for call in fake_op.calls)
+
+
+def test_sms_migration_upgrade_rebuilds_invalid_credentials_index(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ migration = _load_sms_migration()
+ fake_op = _FakeMigrationOp(invalid_index="uq_credentials_id_org")
+ monkeypatch.setattr(migration, "op", fake_op)
+
+ migration.upgrade()
+
+ statements = [statement.replace("\n", " ") for statement in fake_op.statements]
+ drop_position = next(index for index, statement in enumerate(statements) if "DROP INDEX CONCURRENTLY" in statement)
+ create_position = next(
+ index for index, statement in enumerate(statements) if "CREATE UNIQUE INDEX CONCURRENTLY" in statement
+ )
+ assert drop_position < create_position
+
+
+def test_sms_migration_upgrade_preserves_index_timeout_exception(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ migration = _load_sms_migration()
+ timeout = RuntimeError("canceling statement due to lock timeout")
+ fake_op = _FakeMigrationOp(failure_statement="CREATE UNIQUE INDEX", failure=timeout)
+ monkeypatch.setattr(migration, "op", fake_op)
+
+ with pytest.raises(RuntimeError) as exc_info:
+ migration.upgrade()
+
+ assert exc_info.value is timeout
+
+
+def test_sms_migration_upgrade_preserves_attach_timeout_exception(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ migration = _load_sms_migration()
+ timeout = RuntimeError("canceling statement due to lock timeout")
+ fake_op = _FakeMigrationOp(failure_statement='ALTER TABLE "credentials"', failure=timeout)
+ monkeypatch.setattr(migration, "op", fake_op)
+
+ with pytest.raises(RuntimeError) as exc_info:
+ migration.upgrade()
+
+ assert exc_info.value is timeout
+ assert "ROLLBACK" in fake_op.statements
+
+
+def test_sms_migration_upgrade_reuses_attached_credentials_constraint(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ migration = _load_sms_migration()
+ fake_op = _FakeMigrationOp(constraint_exists=True)
+ monkeypatch.setattr(migration, "op", fake_op)
+
+ migration.upgrade()
+
+ assert not any("CREATE UNIQUE INDEX" in statement for statement in fake_op.statements)
+ assert not any("ADD CONSTRAINT" in statement for statement in fake_op.statements)
+
+
+def test_sms_migration_downgrade_drops_credentials_constraint_without_index_drop(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ migration = _load_sms_migration()
+ fake_op = _FakeMigrationOp()
+ monkeypatch.setattr(migration, "op", fake_op)
+
+ migration.downgrade()
+
+ assert ("drop_constraint", "uq_credentials_id_org") in fake_op.calls
+ assert ("drop_index", "uq_credentials_id_org") not in fake_op.calls
diff --git a/tests/unit/test_agent_task_v3.py b/tests/unit/test_agent_task_v3.py
index 12bed8202..ed5ecaccf 100644
--- a/tests/unit/test_agent_task_v3.py
+++ b/tests/unit/test_agent_task_v3.py
@@ -53,7 +53,11 @@ from skyvern.forge.sdk.workflow.models.workflow import WorkflowRunStatus
from skyvern.forge.taskv3.engine import MIN_ACTION_STEPS
from skyvern.forge.taskv3.frame_perception import FRAME_PERCEPTION_FLAG, frame_perception_enabled
from skyvern.forge.taskv3.loop import LoopOutcome, RoundAction
-from skyvern.forge.taskv3.run_arms import OBSERVE_DROP_OFFVIEWPORT_UNNAMED_FLAG, run_arm_enabled
+from skyvern.forge.taskv3.run_arms import (
+ OBSERVE_DROP_OFFVIEWPORT_UNNAMED_FLAG,
+ TYPE_COORDINATE_CLICK_FLAG,
+ run_arm_enabled,
+)
from skyvern.schemas.workflows import BlockStatus, BlockType
from skyvern.utils.secret_redaction import REDACTED_SECRET_PLACEHOLDER
from skyvern.webeye.actions.actions import (
@@ -118,6 +122,7 @@ async def _run_execute_task_v3(
loop_mock.observe_offviewport_drop_during_loop = run_arm_enabled(
OBSERVE_DROP_OFFVIEWPORT_UNNAMED_FLAG, forced=False
)
+ loop_mock.type_coordinate_click_enabled_during_loop = run_arm_enabled(TYPE_COORDINATE_CLICK_FLAG, forced=False)
cb = kwargs.get("on_action_round")
if cb is not None and action_rounds:
for i, round_actions in enumerate(action_rounds):
@@ -274,6 +279,32 @@ async def test_execute_task_v3_resolves_frame_perception_before_the_loop(monkeyp
)
+async def test_execute_task_v3_buckets_the_type_coordinate_click_arm_per_run(monkeypatch: pytest.MonkeyPatch) -> None:
+ # Bucketing by block (task_id) would give one workflow run several draws and inflate exposure.
+ monkeypatch.setattr(settings, "TASK_V3_TYPE_COORDINATE_CLICK", False)
+ provider = AsyncMock(return_value="treatment")
+ monkeypatch.setattr(app.EXPERIMENTATION_PROVIDER, "get_value_cached", provider)
+
+ outcome = LoopOutcome(status="completed", reason="done", billable_actions=[])
+ _step, task, loop_mock, _post = await _run_execute_task_v3(
+ monkeypatch,
+ outcome,
+ workflow_run_id="wr_type_coordinate_click_reach",
+ data_extraction_goal=None,
+ extracted_information_schema=None,
+ )
+
+ assert task.workflow_run_id != task.task_id
+ assert loop_mock.type_coordinate_click_enabled_during_loop is True
+ assert loop_mock.context.run_arms[TYPE_COORDINATE_CLICK_FLAG] == (task.workflow_run_id, "treatment")
+ provider.assert_any_await(
+ TYPE_COORDINATE_CLICK_FLAG,
+ task.workflow_run_id,
+ properties={"organization_id": task.organization_id},
+ )
+
+
@pytest.mark.asyncio
async def test_execute_task_v3_bills_per_browser_action(monkeypatch: pytest.MonkeyPatch) -> None:
outcome = LoopOutcome(
diff --git a/tests/unit/test_bitwarden_credential_vault_service.py b/tests/unit/test_bitwarden_credential_vault_service.py
index 263287770..3889e3959 100644
--- a/tests/unit/test_bitwarden_credential_vault_service.py
+++ b/tests/unit/test_bitwarden_credential_vault_service.py
@@ -30,6 +30,7 @@ def _credential() -> Credential:
username="user_test",
totp_type="none",
totp_identifier=None,
+ has_totp_seed=False,
card_last4=None,
card_brand=None,
secret_label=None,
diff --git a/tests/unit/test_forge_log_redaction.py b/tests/unit/test_forge_log_redaction.py
index 2d5d6fb87..2186a2d5e 100644
--- a/tests/unit/test_forge_log_redaction.py
+++ b/tests/unit/test_forge_log_redaction.py
@@ -1,3 +1,6 @@
+import asyncio
+import copy
+import io
import json
import logging
import re
@@ -5,15 +8,20 @@ import subprocess
import sys
import textwrap
import time
-from collections.abc import Iterator
+from collections.abc import Callable, Iterator
+from datetime import datetime
+from decimal import Decimal
+from logging.handlers import BufferingHandler
from types import MappingProxyType
+from unittest.mock import AsyncMock
+from urllib.parse import quote
import pytest
import structlog
from pydantic import BaseModel
from skyvern.config import settings
-from skyvern.forge import log_redaction
+from skyvern.forge import app, log_redaction
from skyvern.forge.log_redaction import (
REDACTED,
is_proxy_observability_key,
@@ -22,17 +30,26 @@ from skyvern.forge.log_redaction import (
redact_sensitive_fields,
strip_artifact_url_query,
)
+from skyvern.forge.sdk import log_artifacts
+from skyvern.forge.sdk.artifact.models import ArtifactType
from skyvern.forge.sdk.copilot import secret_scrub
from skyvern.forge.sdk.copilot.secret_scrub import REDACTED_SECRET_PLACEHOLDER
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
from skyvern.forge.sdk.forge_log import (
+ _GeneratedLogValue,
+ add_filename_section,
+ add_log_context,
compact_action_objects,
redact_bearer_tokens,
+ redact_registered_log_payload,
redact_registered_secrets,
redact_sensitive_event_fields,
+ render_bounded_json,
setup_logger,
)
+from skyvern.forge.sdk.workflow.context_manager import WorkflowContextManager
+from skyvern.forge.skyvern_json_encoder import SkyvernJSONLogEncoder
from skyvern.schemas.proxy_pinning import ProxyObservabilityField, RedactedProxyLogValue, redact_proxy_location
from skyvern.schemas.runs import GeoTarget
@@ -253,6 +270,688 @@ def test_redacts_a_credential_inside_a_tuple_value() -> None:
assert out["pair"] == ("user", REDACTED_SECRET_PLACEHOLDER)
+def registered_log_stream(monkeypatch: pytest.MonkeyPatch, request: pytest.FixtureRequest) -> Iterator[io.StringIO]:
+ monkeypatch.setattr(settings, "JSON_LOGGING", getattr(request, "param", True))
+ root = logging.getLogger()
+ saved_config = structlog.get_config()
+ saved_handlers = root.handlers[:]
+ saved_level = root.level
+ stream = io.StringIO()
+ try:
+ setup_logger()
+ handler = root.handlers[0]
+ assert isinstance(handler, logging.StreamHandler)
+ handler.setStream(stream)
+ yield stream
+ finally:
+ structlog.configure(**saved_config)
+ root.handlers[:] = saved_handlers
+ root.setLevel(saved_level)
+
+
+class _DiagnosticModel(BaseModel):
+ detail: str
+ number: int
+
+
+class _RenderedDiagnostic:
+ def __init__(self, detail: str) -> None:
+ self.detail = detail
+
+ def __structlog__(self) -> dict[str, str]:
+ return {"detail": self.detail}
+
+
+class _ProtocolNamedDiagnostic(BaseModel):
+ event: str
+ msg: str
+ level: str
+ warning: str
+
+
[email protected]("registered_log_stream", [True, False], indirect=True)
[email protected]("route", ["native", "stdlib", "downstream_renamer"])
[email protected]("protocol_word", ["event", "msg", "level", "warning", "warn"])
+def test_registered_protocol_words_preserve_emitted_messages_and_severity(
+ registered_log_stream: io.StringIO, route: str, protocol_word: str
+) -> None:
+ context = SkyvernContext(runtime_secret_values={protocol_word})
+ payload = dict.fromkeys(("event", "msg", "level", "warning"), protocol_word)
+ original = payload.copy()
+ model = _ProtocolNamedDiagnostic(**payload)
+ fields = {
+ "payload": payload,
+ "model": model,
+ f"user:{protocol_word}": protocol_word,
+ "url": f"https://example.invalid/verify?value={protocol_word}",
+ }
+ logger = structlog.get_logger("skyvern.test.protocol")
+ if route == "downstream_renamer":
+ # Reuse the native chain, then replace its final formatter seam with an
+ # EventRenamer/JSON renderer. This models a downstream consumer without
+ # importing cloud or assuming that lack of an exception proves success.
+ config = structlog.get_config()
+ processors = [
+ processor
+ for processor in config["processors"][:-1]
+ if not isinstance(processor, structlog.processors.CallsiteParameterAdder)
+ and processor is not add_filename_section
+ ]
+ if not settings.JSON_LOGGING:
+ processors.append(structlog.processors.EventRenamer("msg"))
+ processors.append(render_bounded_json)
+ logger = structlog.wrap_logger(
+ structlog.PrintLogger(registered_log_stream),
+ processors=processors,
+ wrapper_class=config["wrapper_class"],
+ )
+
+ def emit(message: str, exc_info: bool = False) -> None:
+ if route == "stdlib":
+ logging.getLogger("skyvern.test.protocol").warning(message, extra=fields, exc_info=exc_info)
+ else:
+ logger.warning(message, **fields, exc_info=exc_info)
+
+ with skyvern_context.scoped(context):
+ emit("Diagnostic emission")
+ try:
+ raise ValueError(protocol_word)
+ except ValueError:
+ emit(f"Diagnostic emission: {protocol_word}", exc_info=True)
+ context.runtime_secret_values.clear()
+ assert payload == original
+ assert model.model_dump() == original
+ assert fields[f"user:{protocol_word}"] == protocol_word
+ payload["late"] = protocol_word
+ model.event = "changed after capture"
+
+ masked_payload = {
+ key.replace(protocol_word, REDACTED_SECRET_PLACEHOLDER): REDACTED_SECRET_PLACEHOLDER for key in original
+ }
+ expected_messages = ["Diagnostic emission", f"Diagnostic emission: {REDACTED_SECRET_PLACEHOLDER}"]
+ emitted = re.sub(r"\x1b\[[0-9;]*m", "", registered_log_stream.getvalue())
+ record_groups = []
+ if settings.JSON_LOGGING or route == "downstream_renamer":
+ record_groups.append([json.loads(line) for line in emitted.splitlines()])
+ else:
+ for message in expected_messages:
+ assert message in emitted
+ assert len(re.findall(r"\[warning\s*\]", emitted)) == 2
+ assert f"payload={masked_payload!r}" in emitted
+ assert f"model={masked_payload!r}" in emitted
+ assert f"user:{REDACTED_SECRET_PLACEHOLDER}={REDACTED_SECRET_PLACEHOLDER}" in emitted
+ assert f"url=https://example.invalid/verify?value={REDACTED_SECRET_PLACEHOLDER}" in emitted
+ assert f"ValueError: {REDACTED_SECRET_PLACEHOLDER}" in emitted
+ assert f"Diagnostic emission: {protocol_word}" not in emitted
+ assert f"ValueError: {protocol_word}" not in emitted
+ if route != "stdlib":
+ record_groups.append(json.loads(json.dumps(context.log, cls=SkyvernJSONLogEncoder)))
+ for records in record_groups:
+ assert len(records) == 2
+ assert [record.get("msg", record.get("event")) for record in records] == expected_messages
+ for record in records:
+ assert record["level"] == "warning"
+ assert record["payload"] == masked_payload
+ assert record["model"] == masked_payload
+ assert record[f"user:{REDACTED_SECRET_PLACEHOLDER}"] == REDACTED_SECRET_PLACEHOLDER
+ assert record["url"] == f"https://example.invalid/verify?value={REDACTED_SECRET_PLACEHOLDER}"
+ assert records[1]["exception"].endswith(f"ValueError: {REDACTED_SECRET_PLACEHOLDER}")
+
+
[email protected]("method", ["debug", "info", "warning", "warn", "error", "exception", "critical"])
+def test_registered_processor_preserves_only_method_derived_level(method: str) -> None:
+ logger = logging.getLogger("skyvern.test.protocol")
+ level = structlog.stdlib.add_log_level(logger, method, {})["level"]
+ context = SkyvernContext(runtime_secret_values={"event", "msg", "level", level})
+ event = {
+ "event": "Diagnostic emission: event",
+ "msg": "Diagnostic emission: msg",
+ "level": level,
+ level: "caller field",
+ "payload": {"event": "event", "msg": "msg", "level": level},
+ }
+ original = copy.deepcopy(event)
+ with skyvern_context.scoped(context):
+ out = redact_registered_secrets(logger, method, event)
+ # A familiar level under the wrong method is still data. So is a value
+ # from a custom method, whose provenance this processor cannot establish.
+ unexpected = redact_registered_secrets(logger, "info" if level != "info" else "warning", event)
+ custom = redact_registered_secrets(logger, "custom", event)
+ assert event == original
+ assert out["event"] == f"Diagnostic emission: {REDACTED_SECRET_PLACEHOLDER}"
+ assert out["msg"] == f"Diagnostic emission: {REDACTED_SECRET_PLACEHOLDER}"
+ assert out["level"] == level
+ assert out[REDACTED_SECRET_PLACEHOLDER] == "caller field"
+ assert out["payload"] == {REDACTED_SECRET_PLACEHOLDER: REDACTED_SECRET_PLACEHOLDER}
+ assert unexpected["level"] == REDACTED_SECRET_PLACEHOLDER
+ assert custom["level"] == REDACTED_SECRET_PLACEHOLDER
+
+
[email protected]("registered_log_stream", [True, False], indirect=True)
[email protected](("global_mask", "workflow_mask"), [(False, True), (True, False), (False, False)])
+def test_registered_secrets_protect_output_and_log_artifacts_after_teardown(
+ monkeypatch: pytest.MonkeyPatch,
+ registered_log_stream: io.StringIO,
+ workflow_context_manager_factory: Callable[..., WorkflowContextManager],
+ global_mask: bool,
+ workflow_mask: bool,
+) -> None:
+ workflow_value = 'synthetic-workflow-credential<& /"\\\n'
+ runtime_value = "synthetic-runtime-credential"
+ monkeypatch.setattr(settings, "ENABLE_SECRET_ARTIFACT_REDACTION", global_mask)
+ manager = workflow_context_manager_factory(
+ workflow_run_id="wr_diagnostic",
+ mask_secrets=workflow_mask,
+ secrets={"long": workflow_value, "short": "q7", "pin": "587", "number": "483920", "empty": ""},
+ )
+ monkeypatch.setattr(app, "WORKFLOW_CONTEXT_MANAGER", manager)
+ context = SkyvernContext(workflow_run_id="wr_diagnostic")
+ context.register_secret_value(runtime_value)
+ _register_credential(_FAKE_CREDENTIAL)
+ model = _DiagnosticModel(detail=runtime_value, number=483920)
+ opaque = _RenderedDiagnostic(runtime_value)
+ variants = secret_scrub.encoded_secret_variants(workflow_value)
+ response = {
+ f"key:{workflow_value}": {"models": [model]},
+ "forms": variants,
+ "numbers": [483920, 483920.0, Decimal("483920"), 587],
+ "short": "q7",
+ "safe": [42, 1.25, True, False, None],
+ "copilot": _FAKE_CREDENTIAL,
+ }
+ original = copy.deepcopy(response)
+ fields = {
+ f"field:{workflow_value}": "safe diagnostic",
+ "response": response,
+ "details": opaque,
+ "reason": ValueError(runtime_value),
+ "url": f"https://example.invalid/verify?value={variants[-1]}",
+ }
+ with skyvern_context.scoped(context):
+ # Diagnostic protection must not change the existing artifact opt-out.
+ assert manager.get_secret_values_for_run(context.workflow_run_id) == set()
+ try:
+ raise ValueError(f"Malformed response: {workflow_value} {runtime_value}")
+ except ValueError:
+ structlog.get_logger("skyvern.test.registered").warning("rejected codes", exc_info=True, **fields)
+ logging.getLogger("skyvern.test.foreign_registered").warning(
+ "rejected codes %s", runtime_value, exc_info=True, extra=fields
+ )
+ assert response == original
+ assert model.detail == runtime_value
+ assert opaque.detail == runtime_value
+ assert manager.get_secret_values_for_run(context.workflow_run_id) == set()
+
+ manager.workflow_run_contexts.clear()
+ context.runtime_secret_values.clear()
+ secret_scrub._SESSION_SCRUB_VALUES.clear()
+ # Later caller mutations must not change the captured event or its artifact.
+ response["late"] = runtime_value
+ model.detail = "changed after capture"
+ opaque.detail = "changed after capture"
+ persisted = json.dumps(context.log, cls=SkyvernJSONLogEncoder)
+ emitted = registered_log_stream.getvalue()
+ for output in (emitted, persisted):
+ assert "synthetic-workflow-credential" not in output
+ assert runtime_value not in output
+ assert _FAKE_CREDENTIAL not in output
+ assert "changed after capture" not in output
+ assert "rejected codes" in output
+ assert "Malformed response:" in output
+ assert REDACTED_SECRET_PLACEHOLDER in output
+
+ captured = next(entry for entry in context.log if "response" in entry)
+ records = [captured]
+ if settings.JSON_LOGGING:
+ records.extend(json.loads(line) for line in emitted.splitlines() if '"response"' in line)
+ assert len(records) == 3 # One captured native event and both emitted events.
+ else:
+ assert "skyvern.test.foreign_registered" in emitted
+ for record in records:
+ assert record[f"field:{REDACTED_SECRET_PLACEHOLDER}"] == "safe diagnostic"
+ redacted = record["response"]
+ assert redacted[f"key:{REDACTED_SECRET_PLACEHOLDER}"]["models"] == [
+ {"detail": REDACTED_SECRET_PLACEHOLDER, "number": REDACTED_SECRET_PLACEHOLDER}
+ ]
+ assert redacted["forms"] == [REDACTED_SECRET_PLACEHOLDER] * len(variants)
+ assert redacted["numbers"] == [REDACTED_SECRET_PLACEHOLDER] * 4
+ assert redacted["short"] == REDACTED_SECRET_PLACEHOLDER
+ assert redacted["safe"] == [42, 1.25, True, False, None]
+ assert "late" not in redacted
+ assert record["details"] == {"detail": REDACTED_SECRET_PLACEHOLDER}
+
+
+def test_registered_processor_copies_keys_containers_and_preserves_unmatched_scalars() -> None:
+ context = SkyvernContext()
+ for value in ("q7", "587", "483920", "synthetic-long-value", "synthetic-long"):
+ context.register_secret_value(value)
+ nested = {"key:q7": ["synthetic-long-value", 483920], 587: "ordinary"}
+ event = {
+ "event": "diagnostic",
+ "mapping": MappingProxyType(nested),
+ "tuple": ("q7", 8),
+ "set": {"q7", "ordinary"},
+ "frozen": frozenset({"q7"}),
+ "safe": [9, 0.5, Decimal("0.125"), True, False, None],
+ }
+ with skyvern_context.scoped(context):
+ out = redact_registered_secrets(None, "info", event) # type: ignore[arg-type]
+ assert out is not event
+ assert out["mapping"] == {
+ f"key:{REDACTED_SECRET_PLACEHOLDER}": [REDACTED_SECRET_PLACEHOLDER, REDACTED_SECRET_PLACEHOLDER],
+ REDACTED_SECRET_PLACEHOLDER: "ordinary",
+ }
+ assert out["tuple"] == (REDACTED_SECRET_PLACEHOLDER, 8)
+ assert out["set"] == {REDACTED_SECRET_PLACEHOLDER, "ordinary"}
+ assert out["frozen"] == frozenset({REDACTED_SECRET_PLACEHOLDER})
+ assert out["safe"] == event["safe"]
+ assert out["safe"] is not event["safe"]
+ assert nested == {"key:q7": ["synthetic-long-value", 483920], 587: "ordinary"}
+ assert event["tuple"] == ("q7", 8)
+ assert event["set"] == {"q7", "ordinary"}
+
+
[email protected]("encoded", [False, True])
+def test_context_suffix_scrubs_complete_caller_values_without_mutating_input(encoded: bool) -> None:
+ secret = "<" * 100 + "X" if encoded else "s" * 256 + "X"
+ emitted = quote(secret, safe="") if encoded else secret
+ task_id = "tsk_" + "g" * 300
+ context = SkyvernContext(task_id=task_id, runtime_secret_values={secret, "g"})
+ event = {"msg": "notice", "organization_name": emitted, "browser_session_id": emitted, "workflow_id": "u" * 300}
+ original = copy.deepcopy(event)
+ with skyvern_context.scoped(context):
+ result = add_log_context(logging.getLogger(), "warning", event)
+ result = redact_registered_secrets(logging.getLogger(), "warning", result)
+ assert event == original
+ assert emitted[:256] not in repr(result)
+ assert result["or[REDACTED_SECRET]anization_name"] == REDACTED_SECRET_PLACEHOLDER
+ assert result["browser_session_id"] == REDACTED_SECRET_PLACEHOLDER
+ assert result["task_id"] == task_id
+ assert f"task_id={task_id[:256]}" in result["msg"]
+ assert f"workflow_id={'u' * 256}" in result["msg"]
+ assert "g" * 257 not in result["msg"] and "u" * 257 not in result["msg"]
+
+
+def test_export_payload_scrubs_copied_provenance_and_preserves_sliced_generated_parts() -> None:
+ context = SkyvernContext(task_id="tsk_123", runtime_secret_values={"123", "id"})
+ with skyvern_context.scoped(context):
+ event = add_log_context(None, "warning", {"msg": "caller 123"}) # type: ignore[arg-type]
+ message = event["msg"]
+ assert type(message) is _GeneratedLogValue
+ attributes = {
+ "task_id": event["task_id"],
+ "payload": dict(event),
+ "copied": event["task_id"],
+ "forged_id": "tsk_123",
+ }
+ original = copy.deepcopy(attributes)
+ # OTel slices attributes even when the configured limit exceeds their length.
+ body, result = redact_registered_log_payload(message[:4096], attributes)
+ truncated, _ = redact_registered_log_payload(message[:-1], {})
+ assert body == "caller [REDACTED_SECRET] | task_id=tsk_123"
+ assert truncated == "caller [REDACTED_SECRET] | task_id=tsk_12"
+ assert type(body) is str and type(result["task_id"]) is str
+ assert result["task_id"] == "tsk_123"
+ assert result["copied"] == "tsk_[REDACTED_SECRET]"
+ assert result["forged_[REDACTED_SECRET]"] == "tsk_[REDACTED_SECRET]"
+ assert "123" not in json.dumps(result["payload"])
+ assert attributes == original and message.startswith("caller 123")
+
+
[email protected]("registered_log_stream", [True, False], indirect=True)
+async def test_generated_metadata_survives_short_secrets_and_actual_artifact_selection(
+ monkeypatch: pytest.MonkeyPatch,
+ registered_log_stream: io.StringIO,
+ workflow_context_manager_factory: Callable[..., WorkflowContextManager],
+) -> None:
+ ids = {
+ "organization_id": "o_2012309",
+ "workflow_run_id": "wr_2012309",
+ "task_id": "tsk_2012309",
+ "step_id": "stp_2012309",
+ }
+ manager = workflow_context_manager_factory(
+ workflow_run_id=ids["workflow_run_id"], mask_secrets=False, secrets={"month": "09", "pin": "123"}
+ )
+ monkeypatch.setattr(app, "WORKFLOW_CONTEXT_MANAGER", manager)
+ monkeypatch.setattr(settings, "ENABLE_SECRET_ARTIFACT_REDACTION", False)
+ monkeypatch.setattr(settings, "ENABLE_LOG_ARTIFACTS", True)
+ context = SkyvernContext(
+ organization_id=ids["organization_id"],
+ workflow_run_id=ids["workflow_run_id"],
+ task_id=ids["task_id"],
+ step_id=ids["step_id"],
+ organization_name="09",
+ copilot_session_id="123",
+ browser_session_id="pbs_09",
+ runtime_secret_values={"20"},
+ )
+ payload = {**ids, "timestamp": "2020-09-20T09:12:30Z", "number": 123}
+ original = payload.copy()
+ fields = {"payload": payload, "workflow_id": "spoof-09", "task_id": "spoof-123", "timestamp": "spoof-20"}
+ masked = REDACTED_SECRET_PLACEHOLDER
+ expected_payload = {
+ key: value.replace("123", masked).replace("09", masked).replace("20", masked)
+ if isinstance(value, str)
+ else masked
+ for key, value in original.items()
+ }
+ expected_message = f"correlated {masked} {masked} {masked}"
+ root = logging.getLogger()
+ buffer = BufferingHandler(10)
+ root.addHandler(buffer)
+ formatter = root.handlers[0].formatter
+ assert formatter is not None
+ static_stream = io.StringIO()
+ config = structlog.get_config()
+ processors = [
+ processor
+ for processor in config["processors"][:-1]
+ if not isinstance(processor, structlog.processors.CallsiteParameterAdder)
+ and processor is not add_filename_section
+ ]
+ if not settings.JSON_LOGGING:
+ # Match the static-script logger's console-to-JSON renderer seam.
+ processors.extend((structlog.processors.EventRenamer("msg"), add_log_context))
+ processors.append(render_bounded_json)
+ static_logger = structlog.wrap_logger(
+ structlog.PrintLogger(static_stream), processors=processors, wrapper_class=config["wrapper_class"]
+ )
+ with skyvern_context.scoped(context), skyvern_context.workflow_log_attempt(ids["workflow_run_id"], 123):
+ structlog.get_logger("skyvern.test.correlation.native").warning("correlated 09 123 20", **fields)
+ logging.getLogger("skyvern.test.correlation.foreign").warning("correlated 09 123 20", extra=fields)
+ static_logger.warning("correlated 09 123 20", **fields)
+ assert payload == original
+ payload["late"] = "09"
+ manager.workflow_run_contexts.clear()
+ context.runtime_secret_values.clear()
+ # A later formatter pass must preserve the original generated IDs even when
+ # the originating context is gone and another scrub source still matches.
+ for value in ("09", "123", "20"):
+ _register_credential(value)
+ native_record = next(record for record in buffer.buffer if record.name.endswith(".native"))
+ with skyvern_context.scoped(SkyvernContext(workflow_run_id="wr_other")):
+ deferred = formatter.format(native_record)
+
+ def check_records(records: list[dict]) -> None:
+ for record in records:
+ assert {key: record[key] for key in ids} == ids
+ assert record["payload"] == expected_payload
+ assert record["workflow_id"] == f"spoof-{masked}"
+ assert record["organization_name"] == masked
+ assert record["copilot_session_id"] == masked
+ assert record["browser_session_id"] == f"pbs_{masked}"
+ assert record["level"] == "warning"
+ assert "20" in record["timestamp"]
+ assert datetime.fromisoformat(record["timestamp"]).tzinfo is not None
+ message = record.get("msg", record.get("event"))
+ assert message.split(" | ", 1)[0] == expected_message
+ if " | " in message:
+ for key, value in ids.items():
+ assert f"{key}={value}" in message
+
+ assert len(context.log) == 2
+ assert all(entry["workflow_run_attempt_number"] == 123 for entry in context.log)
+ check_records(context.log)
+ check_records([json.loads(static_stream.getvalue())])
+ emitted = registered_log_stream.getvalue()
+ if settings.JSON_LOGGING:
+ records = [json.loads(line) for line in emitted.splitlines()]
+ assert len(records) == 2
+ check_records([*records, json.loads(deferred)])
+ else:
+ for output in (emitted, deferred):
+ output = re.sub(r"\x1b\[[0-9;]*m", "", output)
+ assert expected_message in output
+ assert f"payload={expected_payload!r}" in output
+ for key, value in ids.items():
+ assert f"{key}={value}" in output
+ assert "spoof-09" not in output
+ assert "spoof-123" not in output
+ assert "pbs_09" not in output
+
+ # Keep the real selectors, attempt filter, and raw/human artifact encoders.
+ # Stub only the storage I/O beneath them, after the emitting context is gone.
+ create_artifact = AsyncMock()
+ save_workflow_artifact = AsyncMock()
+ monkeypatch.setattr(app.DATABASE.artifacts, "get_artifact_by_entity_id", AsyncMock(return_value=None))
+ monkeypatch.setattr(app.ARTIFACT_MANAGER, "create_log_artifact", create_artifact)
+ monkeypatch.setattr(log_artifacts, "_save_workflow_log_artifact", save_workflow_artifact)
+ monkeypatch.setattr(log_artifacts, "resolve_current_attempt", AsyncMock(return_value=(123, False)))
+ cleanup = SkyvernContext(organization_id=ids["organization_id"], log=list(context.log))
+ cleanup.log.append(
+ {"workflow_run_id": "wr_other", "task_id": "tsk_other", "step_id": "stp_other", "event": "other"}
+ )
+ with skyvern_context.scoped(cleanup), skyvern_context.workflow_log_attempt(ids["workflow_run_id"], 123):
+ await log_artifacts.save_workflow_run_logs(ids["workflow_run_id"])
+ await log_artifacts.save_task_logs(ids["task_id"])
+ await log_artifacts.save_step_logs(ids["step_id"])
+ assert save_workflow_artifact.await_count == 2
+ assert create_artifact.await_count == 4
+ artifacts = [(call.args[-2], call.args[-1]) for call in save_workflow_artifact.await_args_list]
+ artifacts.extend((call.kwargs["artifact_type"], call.kwargs["data"]) for call in create_artifact.await_args_list)
+ for artifact_type, data in artifacts:
+ if artifact_type == ArtifactType.SKYVERN_LOG_RAW:
+ records = json.loads(data)
+ assert len(records) == 2
+ check_records(records)
+ assert all(record["workflow_run_attempt_number"] == 123 for record in records)
+ else:
+ assert data.decode().count(expected_message) == 2
+ assert "spoof-09" not in data.decode()
+ assert "pbs_09" not in data.decode()
+
+
[email protected]("with_context", [False, True])
+def test_plain_metadata_names_and_copied_generated_values_are_still_caller_data(
+ monkeypatch: pytest.MonkeyPatch, registered_log_stream: io.StringIO, with_context: bool
+) -> None:
+ _register_credential("123")
+ _register_credential("id")
+ context = SkyvernContext(workflow_run_id="wr_123", task_id="tsk_123") if with_context else None
+ monkeypatch.setattr(skyvern_context, "current", lambda: context)
+ metadata = {
+ "workflow_run_id": "wr_123",
+ "task_id": "tsk_123",
+ "timestamp": "123",
+ "copilot_session_id": "123",
+ }
+ event = {"msg": "123", **metadata, "payload": metadata}
+ original = copy.deepcopy(event)
+ result = redact_registered_secrets(None, "warning", event) # type: ignore[arg-type]
+ masked = REDACTED_SECRET_PLACEHOLDER
+ expected = {key.replace("id", masked): value.replace("123", masked) for key, value in metadata.items()}
+ assert result == {"msg": masked, **expected, "payload": expected}
+ assert event == original
+ structlog.get_logger("skyvern.test.metadata").warning("123", payload=metadata, **metadata)
+ logging.getLogger("skyvern.test.metadata.foreign").warning("123", extra={"payload": metadata, **metadata})
+ records = [json.loads(line) for line in registered_log_stream.getvalue().splitlines()]
+ assert len(records) == 2
+ for record in records:
+ assert record["payload"] == expected
+ assert record["msg"].split(" | ", 1)[0] == masked
+ assert datetime.fromisoformat(record["timestamp"]).tzinfo is not None
+ assert record[f"copilot_session_{masked}"] == masked
+ for key in ("workflow_run_id", "task_id"):
+ if with_context:
+ assert record[key] == metadata[key]
+ else:
+ assert record[key.replace("id", masked)] == expected[key.replace("id", masked)]
+ if with_context:
+ enriched = add_log_context(None, "warning", {"msg": "123"}) # type: ignore[arg-type]
+ result = redact_registered_secrets(
+ None, "warning", {**enriched, "payload": dict(enriched), "foreign": enriched["workflow_run_id"]}
+ ) # type: ignore[arg-type]
+ assert result["workflow_run_id"] == "wr_123"
+ assert result["task_id"] == "tsk_123"
+ assert "123" not in json.dumps(result["payload"])
+ assert result["foreign"] == f"wr_{masked}"
+ assert result["msg"] == f"{masked} | task_id=tsk_123, workflow_run_id=wr_123"
+
+
[email protected]("failure", ["collection", "iterator", "renderer", "node_limit"])
[email protected]("message_keys", [("event",), ("msg",), ("event", "msg"), ()])
+def test_registered_processor_fails_closed_with_usable_renderer_protocol(
+ monkeypatch: pytest.MonkeyPatch, failure: str, message_keys: tuple[str, ...]
+) -> None:
+ class BrokenMapping(dict):
+ def items(self): # type: ignore[override]
+ raise RuntimeError(_FAKE_CREDENTIAL)
+
+ class BrokenRenderer:
+ def __structlog__(self) -> object:
+ raise RuntimeError(_FAKE_CREDENTIAL)
+
+ payloads = {
+ "collection": _FAKE_CREDENTIAL,
+ "iterator": BrokenMapping({"detail": _FAKE_CREDENTIAL}),
+ "renderer": BrokenRenderer(),
+ "node_limit": ["safe"] * 10_001 + [_FAKE_CREDENTIAL],
+ }
+ if failure == "collection":
+
+ def fail_collection() -> list[str]:
+ raise RuntimeError(_FAKE_CREDENTIAL)
+
+ monkeypatch.setattr(secret_scrub, "all_registered_secret_values", fail_collection)
+ context = SkyvernContext(runtime_secret_values={_FAKE_CREDENTIAL, "event", "msg", "level", "warning"})
+ logger = logging.getLogger("skyvern.test.protocol")
+ event = {**dict.fromkeys(message_keys, "diagnostic"), "level": "warning", "payload": payloads[failure]}
+ with skyvern_context.scoped(context):
+ out = redact_registered_secrets(logger, "warning", event)
+ assert out == {**dict.fromkeys(message_keys or ("event",), REDACTED_SECRET_PLACEHOLDER), "level": "warning"}
+ assert all(event[key] == "diagnostic" for key in message_keys)
+ assert event["payload"] is payloads[failure]
+ json_event = out.copy()
+ if "event" in json_event:
+ json_event = structlog.processors.EventRenamer("msg")(logger, "warning", json_event)
+ assert json.loads(render_bounded_json(logger, "warning", json_event)) == {
+ "msg": REDACTED_SECRET_PLACEHOLDER,
+ "level": "warning",
+ }
+ console = structlog.dev.ConsoleRenderer(colors=False, event_key="event" if "event" in out else "msg")
+ rendered = console(logger, "warning", out.copy())
+ assert REDACTED_SECRET_PLACEHOLDER in rendered
+ assert "[warning" in rendered
+ assert _FAKE_CREDENTIAL not in rendered
+
+
+def test_registered_processor_bounds_cycles_and_depth() -> None:
+ cyclic: dict = {"detail": _FAKE_CREDENTIAL}
+ cyclic["self"] = cyclic
+ cyclic["shared"] = cyclic
+ context = SkyvernContext()
+ context.register_secret_value(_FAKE_CREDENTIAL)
+ with skyvern_context.scoped(context):
+ out = redact_registered_secrets(
+ None, "warning", {"event": "diagnostic", "cycle": cyclic, "deep": _nested_mapping(_FAKE_CREDENTIAL)}
+ ) # type: ignore[arg-type]
+ assert _FAKE_CREDENTIAL not in json.dumps(out)
+ assert out["cycle"] == dict.fromkeys(("detail", "self", "shared"), REDACTED_SECRET_PLACEHOLDER)
+ assert cyclic["self"] is cyclic
+
+
+async def test_registered_secrets_stay_with_current_concurrent_run(
+ monkeypatch: pytest.MonkeyPatch,
+ registered_log_stream: io.StringIO,
+ workflow_context_manager_factory: Callable[..., WorkflowContextManager],
+) -> None:
+ run_ids = ("wr_log_a", "wr_log_b")
+ values = ("synthetic-run-alpha", "synthetic-run-beta")
+ runtime_values = ("synthetic-runtime-alpha", "synthetic-runtime-beta")
+ manager = workflow_context_manager_factory(workflow_run_id=run_ids[0], secrets={"ref": values[0]})
+ other = workflow_context_manager_factory(workflow_run_id=run_ids[1], secrets={"ref": values[1]})
+ manager.workflow_run_contexts.update(other.workflow_run_contexts)
+ monkeypatch.setattr(app, "WORKFLOW_CONTEXT_MANAGER", manager)
+ ready = [asyncio.Event(), asyncio.Event()]
+
+ async def emit(index: int) -> SkyvernContext:
+ context = SkyvernContext(workflow_run_id=run_ids[index])
+ context.register_secret_value(runtime_values[index])
+ with skyvern_context.scoped(context):
+ ready[index].set()
+ await ready[1 - index].wait()
+ fields = {
+ "own": values[index],
+ "runtime": runtime_values[index],
+ "unregistered": values[1 - index],
+ "unregistered_runtime": runtime_values[1 - index],
+ }
+ structlog.get_logger("skyvern.test.isolation").warning("run isolation", **fields)
+ logging.getLogger("skyvern.test.foreign_isolation").warning("run isolation", extra=fields)
+ await asyncio.sleep(0)
+ return context
+
+ contexts = await asyncio.gather(emit(0), emit(1))
+ manager.workflow_run_contexts.clear()
+ for context in contexts:
+ context.runtime_secret_values.clear()
+ records = [json.loads(line) for line in registered_log_stream.getvalue().splitlines()]
+ for index, context in enumerate(contexts):
+ own_records = [record for record in records if record.get("workflow_run_id") == run_ids[index]]
+ assert len(own_records) == 2
+ own_records.extend(json.loads(json.dumps(context.log, cls=SkyvernJSONLogEncoder)))
+ for record in own_records:
+ assert record["own"] == REDACTED_SECRET_PLACEHOLDER
+ assert record["runtime"] == REDACTED_SECRET_PLACEHOLDER
+ assert record["unregistered"] == values[1 - index]
+ assert record["unregistered_runtime"] == runtime_values[1 - index]
+
+ fresh = SkyvernContext(workflow_run_id=run_ids[0])
+ with skyvern_context.scoped(fresh):
+ structlog.get_logger("skyvern.test.isolation").warning("after teardown", detail=values[0])
+ assert fresh.log[-1]["detail"] == values[0]
+ assert secret_scrub.all_registered_secret_values() == []
+
+
+def test_registered_logging_starts_without_app_or_cloud_imports() -> None:
+ script = textwrap.dedent(
+ """
+ import json
+ import logging
+ import sys
+
+ class BlockApplicationImports:
+ def find_spec(self, name, path=None, target=None):
+ if name in {"skyvern.forge.forge_app", "skyvern.forge.forge_app_initializer", "cloud", "starlette"} or name.startswith(("cloud.", "starlette.")):
+ raise ModuleNotFoundError(name)
+ return None
+
+ sys.meta_path.insert(0, BlockApplicationImports())
+ import structlog
+ from skyvern.config import settings
+ from skyvern.forge.sdk.core import skyvern_context
+ from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
+ from skyvern.forge.sdk.forge_log import setup_logger
+ from skyvern.forge.skyvern_json_encoder import SkyvernJSONLogEncoder
+
+ settings.JSON_LOGGING = True
+ settings.ENABLE_SECRET_ARTIFACT_REDACTION = False
+ setup_logger()
+ structlog.get_logger("skyvern.test.boot").warning("logging before app")
+ context = SkyvernContext(workflow_run_id="wr_boot")
+ context.register_secret_value("synthetic-boot-value")
+ context.register_secret_value("q7")
+ with skyvern_context.scoped(context):
+ structlog.get_logger("skyvern.test.boot").warning("native boot", detail="synthetic-boot-value", note="q7")
+ logging.getLogger("skyvern.test.boot").warning("foreign boot %s", "synthetic-boot-value")
+ context.runtime_secret_values.clear()
+ print(json.dumps(context.log, cls=SkyvernJSONLogEncoder))
+ """
+ )
+ result = subprocess.run([sys.executable, "-c", script], capture_output=True, text=True)
+ output = result.stdout + result.stderr
+ assert result.returncode == 0, output
+ for message in ("logging before app", "native boot", "foreign boot"):
+ assert message in output
+ assert "synthetic-boot-value" not in output
+ assert '"note": "q7"' not in output
+ assert REDACTED_SECRET_PLACEHOLDER in output
+
+
def test_redacts_authorization_bearer_header_value() -> None:
event = {"headers_line": "Authorization: Bearer eyJhbGciOi.JIUzI1NiJ9.sig123"}
out = redact_bearer_tokens(None, "error", event) # type: ignore[arg-type]
@@ -601,6 +1300,8 @@ def test_setup_logger_pins_redactor_processor_order() -> None:
# nested containers. Both must precede the field redactor for the chain to be total.
assert names.index("redact_bearer_tokens") < names.index("redact_sensitive_event_fields")
assert names.index("compact_action_objects") < names.index("redact_sensitive_event_fields")
+ assert names.index("redact_sensitive_event_fields") < names.index("redact_registered_secrets")
+ assert names.index("redact_registered_secrets") + 1 == names.index("skyvern_logs_processor")
finally:
structlog.configure(**saved_config)
root.handlers[:] = saved_handlers
diff --git a/tests/unit/test_handle_script_termination.py b/tests/unit/test_handle_script_termination.py
index cbe03bfbc..8b80a5b1d 100644
--- a/tests/unit/test_handle_script_termination.py
+++ b/tests/unit/test_handle_script_termination.py
@@ -2,18 +2,34 @@
IllegitCompleteScriptTermination -> BlockStatus.failed (so AI fallback fires);
plain ScriptTerminationException -> BlockStatus.terminated (no fallback)."""
-from unittest.mock import AsyncMock, patch
+from datetime import UTC, datetime
+from unittest.mock import AsyncMock, Mock, patch
import pytest
+from sqlalchemy.ext.asyncio import AsyncEngine, async_sessionmaker
+from skyvern.core.script_generations.skyvern_page import RunContext
+from skyvern.errors.errors import UserDefinedError
from skyvern.exceptions import (
IllegitCompleteScriptTermination,
ScriptTerminationException,
)
+from skyvern.forge import app
+from skyvern.forge.agent_functions import AgentFunction
+from skyvern.forge.sdk.core import skyvern_context
+from skyvern.forge.sdk.db.repositories.observer import ObserverRepository
+from skyvern.forge.sdk.db.repositories.tasks import TasksRepository
+from skyvern.forge.sdk.db.repositories.workflows import WorkflowsRepository
from skyvern.forge.sdk.models import StepStatus
from skyvern.forge.sdk.schemas.tasks import TaskStatus
-from skyvern.schemas.workflows import BlockStatus
+from skyvern.forge.sdk.workflow.context_manager import WorkflowContextManager, WorkflowRunContext
+from skyvern.forge.sdk.workflow.models.block import NavigationBlock
+from skyvern.forge.sdk.workflow.models.parameter import OutputParameter
+from skyvern.forge.sdk.workflow.models.workflow import WorkflowDefinition
+from skyvern.schemas.workflows import BlockStatus, BlockType
+from skyvern.services import script_service
from skyvern.services.script_service import _handle_script_termination
+from skyvern.webeye.actions.actions import TerminateAction
@pytest.mark.asyncio
@@ -43,7 +59,8 @@ async def test_helper_writes_failed_for_illegit_complete():
@pytest.mark.asyncio
async def test_helper_writes_terminated_for_plain_termination():
- e = ScriptTerminationException("Terminate called: no results found")
+ error = UserDefinedError(error_code="no_results", reasoning="No results found", confidence_float=1.0)
+ e = ScriptTerminationException("Terminate called: no results found", user_defined_errors=[error])
with patch(
"skyvern.services.script_service._update_workflow_block",
new_callable=AsyncMock,
@@ -64,6 +81,7 @@ async def test_helper_writes_terminated_for_plain_termination():
assert kwargs["task_status"] == TaskStatus.terminated
assert kwargs["step_status"] == StepStatus.failed
assert kwargs["failure_reason"] == "Terminate called: no results found"
+ assert kwargs["user_defined_errors"] == [error]
@pytest.mark.asyncio
@@ -82,3 +100,133 @@ async def test_helper_skips_db_write_when_no_workflow_run_block_id():
cache_key="MyTaskBlock",
)
mock_update.assert_not_awaited()
+
+
+ "criterion,block_mapping,workflow_mapping,expected_criterion,expected_mapping",
+ [
+ pytest.param(
+ "Stop when {{ state }}",
+ {"{{ code }}": "Block {{ state }}"},
+ {"unavailable": "Workflow collision", "global": "Inherited condition"},
+ "Stop when unavailable",
+ {"unavailable": "Block unavailable", "global": "Inherited condition"},
+ id="templated-block-precedence",
+ ),
+ pytest.param(None, None, {"global": "Inherited"}, None, {"global": "Inherited"}, id="null-map-inherits"),
+ pytest.param("", {}, {"global": "Inherited"}, "", {"global": "Inherited"}, id="empty-map-inherits"),
+ pytest.param(None, None, None, None, None, id="null-settings"),
+ pytest.param("", {}, None, "", {}, id="empty-settings"),
+ ],
+)
+async def test_cached_creation_and_termination_persist_contract(
+ sqlite_engine: AsyncEngine,
+ monkeypatch: pytest.MonkeyPatch,
+ criterion: str | None,
+ block_mapping: dict[str, str] | None,
+ workflow_mapping: dict[str, str] | None,
+ expected_criterion: str | None,
+ expected_mapping: dict[str, str] | None,
+) -> None:
+ factory = async_sessionmaker(sqlite_engine, expire_on_commit=False)
+ tasks = TasksRepository(session_factory=factory, debug_enabled=False)
+ workflows = WorkflowsRepository(session_factory=factory, debug_enabled=False)
+ observer = ObserverRepository(session_factory=factory, task_reader=tasks)
+ output = OutputParameter(
+ key="target_output",
+ output_parameter_id="op_target",
+ workflow_id="wf_cached",
+ created_at=datetime(2026, 1, 1, tzinfo=UTC),
+ modified_at=datetime(2026, 1, 1, tzinfo=UTC),
+ )
+ target = NavigationBlock(
+ label="target",
+ navigation_goal="Inspect the requested item",
+ output_parameter=output,
+ terminate_criterion=criterion,
+ error_code_mapping=block_mapping,
+ )
+ unrelated = target.model_copy(
+ update={
+ "label": "unrelated",
+ "terminate_criterion": "Stop for another condition",
+ "error_code_mapping": {"other": "Unrelated condition"},
+ }
+ )
+ definition = WorkflowDefinition(
+ parameters=[output], blocks=[unrelated, target], error_code_mapping=workflow_mapping
+ )
+ workflow = await workflows.create_workflow(
+ title="Cached contract",
+ organization_id="o_cached",
+ workflow_id="wf_cached",
+ workflow_definition=definition.model_dump(mode="json"),
+ run_with="code",
+ )
+ context = skyvern_context.SkyvernContext(
+ organization_id="o_cached",
+ workflow_id=workflow.workflow_id,
+ workflow_run_id="wr_cached",
+ script_mode=True,
+ )
+ manager = WorkflowContextManager()
+ workflow_context = WorkflowRunContext(
+ workflow_title=workflow.title,
+ workflow_id=workflow.workflow_id,
+ workflow_permanent_id=workflow.workflow_permanent_id,
+ workflow_run_id="wr_cached",
+ aws_client=Mock(),
+ attempt_number=3,
+ )
+ workflow_context.values.update(state="unavailable", code="unavailable")
+ manager.workflow_run_contexts["wr_cached"] = workflow_context
+ monkeypatch.setattr(skyvern_context, "current", lambda: context)
+ monkeypatch.setattr(app, "WORKFLOW_CONTEXT_MANAGER", manager)
+ monkeypatch.setattr(app, "AGENT_FUNCTION", AgentFunction())
+ monkeypatch.setattr(app.DATABASE, "tasks", tasks)
+ monkeypatch.setattr(app.DATABASE, "workflows", workflows)
+ monkeypatch.setattr(app.DATABASE, "observer", observer)
+ monkeypatch.setattr(tasks, "sync_task_run_status", AsyncMock())
+ for name in ("_create_video_artifact", "_take_workflow_run_block_screenshot", "_record_output_parameter_value"):
+ monkeypatch.setattr(script_service, name, AsyncMock())
+ monkeypatch.setattr(app.ARTIFACT_MANAGER, "flush_step_archive", AsyncMock())
+ monkeypatch.setattr(app.STORAGE, "get_current_attempt_downloaded_files", AsyncMock(return_value=[]))
+ for name in ("get_recent_task_screenshot_artifacts", "get_recent_workflow_screenshot_artifacts"):
+ monkeypatch.setattr(app.WORKFLOW_SERVICE, name, AsyncMock(return_value=[]))
+
+ block_id, task_id, step_id = await script_service._create_workflow_block_run_and_task(
+ BlockType.NAVIGATION, label="target"
+ )
+ assert block_id is not None and task_id is not None and step_id is not None
+ created = await tasks.get_task(task_id, organization_id="o_cached")
+ assert created is not None
+ assert created.terminate_criterion == expected_criterion
+ assert created.error_code_mapping == expected_mapping
+ assert (created.organization_id, created.workflow_run_id, created.attempt_number) == ("o_cached", "wr_cached", 3)
+ existing_error = {"error_code": "existing", "reasoning": "Earlier condition", "confidence_float": 0.5}
+ await tasks.update_task(task_id, organization_id="o_cached", errors=[existing_error])
+ error = UserDefinedError(error_code="unavailable", reasoning="Requested item unavailable", confidence_float=0.9)
+ run_context = RunContext(parameters={}, page=Mock())
+ run_context.actions_and_results.append((TerminateAction(errors=[error]), []))
+ monkeypatch.setattr(script_service.script_run_context_manager, "get_run_context", lambda: run_context)
+
+ await _handle_script_termination(
+ ScriptTerminationException("Requested item unavailable", user_defined_errors=[error]),
+ "task block",
+ block_id,
+ task_id,
+ step_id,
+ "target",
+ )
+ finalized = await tasks.get_task(task_id, organization_id="o_cached")
+ assert finalized is not None
+ assert finalized.status == TaskStatus.terminated
+ assert finalized.attempt_number == 3
+ assert finalized.errors == [existing_error, error.model_dump()]
+ step = await tasks.get_step(step_id, organization_id="o_cached")
+ assert step is not None and step.output is not None
+ assert step.output.errors == [error]
+ block = await observer.get_workflow_run_block(block_id, organization_id="o_cached")
+ assert block.status == BlockStatus.terminated
+ assert block.attempt_number == 3
diff --git a/tests/unit/test_phone_identifier_normalization.py b/tests/unit/test_phone_identifier_normalization.py
new file mode 100644
index 000000000..b76e737e7
--- /dev/null
+++ b/tests/unit/test_phone_identifier_normalization.py
@@ -0,0 +1,79 @@
+import pytest
+
+from skyvern.utils.phone_validation import (
+ is_e164_phone_number,
+ looks_like_phone_identifier,
+ normalize_identifier,
+ normalize_phone_identifier,
+ phone_identifier_candidates,
+)
+
+
+ ("value", "expected"),
+ [
+ pytest.param("4155552671", "4155552671", id="bare-ten-digit"),
+ pytest.param("14155552671", "+14155552671", id="leading-one-eleven-digit"),
+ pytest.param("+14155552671", "+14155552671", id="already-e164"),
+ pytest.param("(415) 555-2671", "(415) 555-2671", id="country-ambiguous"),
+ ],
+)
+def test_normalize_phone_identifier_only_normalizes_explicit_country_codes(value: str, expected: str) -> None:
+ assert normalize_phone_identifier(value) == expected
+
+
+def test_normalize_phone_identifier_does_not_guess_non_nanp_country_code() -> None:
+ assert normalize_phone_identifier("44 20 7946 0958") == "44 20 7946 0958"
+
+
[email protected]("value", ["+14155552671", "+442079460958", "+6831234"])
+def test_is_e164_phone_number_accepts_normalized_international_numbers(value: str) -> None:
+ assert is_e164_phone_number(value)
+
+
[email protected]("value", ["4155552671", "+", "+01234567", "+1415555267100000"])
+def test_is_e164_phone_number_rejects_ambiguous_or_invalid_numbers(value: str) -> None:
+ assert not is_e164_phone_number(value)
+
+
+def test_email_is_not_treated_as_phone_identifier() -> None:
+ assert not looks_like_phone_identifier("[email protected]")
+ assert normalize_identifier(" [email protected] ") == "[email protected]"
+
+
[email protected]("value", ["+1-EXT-415-555-2671", "+1--415-555-2671"])
+def test_malformed_phone_identifier_is_rejected_without_lossy_normalization(value: str) -> None:
+ assert not looks_like_phone_identifier(value)
+ assert normalize_phone_identifier(value) == value
+
+
+def test_phone_identifier_candidates_do_not_guess_legacy_country_code() -> None:
+ assert phone_identifier_candidates("+1 (415) 555-2671") == [
+ "+1 (415) 555-2671",
+ "+14155552671",
+ "14155552671",
+ ]
+
+
+def test_phone_identifier_candidates_do_not_add_legacy_candidate_for_non_nanp_number() -> None:
+ assert phone_identifier_candidates("+44 20 7946 0958") == [
+ "+44 20 7946 0958",
+ "+442079460958",
+ "442079460958",
+ ]
+
+
+ ("value", "expected"),
+ [
+ pytest.param("123-456", False, id="six-digits-with-punctuation"),
+ pytest.param("123 456", False, id="six-digits-with-whitespace"),
+ pytest.param("123-4567", True, id="seven-digits-with-punctuation"),
+ ],
+)
+def test_phone_detection_counts_digits_not_formatted_characters(value: str, expected: bool) -> None:
+ assert looks_like_phone_identifier(value) is expected
+
+
+def test_short_numeric_labels_do_not_generate_punctuation_free_candidates() -> None:
+ assert phone_identifier_candidates("123-456") == ["123-456"]
diff --git a/tests/unit/test_script_skyvern_page.py b/tests/unit/test_script_skyvern_page.py
index 1d9a5bb52..fb4ac00e1 100644
--- a/tests/unit/test_script_skyvern_page.py
+++ b/tests/unit/test_script_skyvern_page.py
@@ -33,6 +33,7 @@ from skyvern.config import settings
from skyvern.core.script_generations.real_skyvern_page_ai import RealSkyvernPageAi
from skyvern.core.script_generations.script_skyvern_page import ScriptSkyvernPage
from skyvern.core.script_generations.skyvern_page import ResolvedSensitiveValue, SkyvernPage
+from skyvern.errors.errors import UserDefinedError
from skyvern.exceptions import (
IllegitCompleteScriptTermination,
NoTOTPSecretFound,
@@ -655,6 +656,15 @@ async def test_terminate_calls_handler_and_raises(mock_scraped_page, mock_ai):
mock_context.action_order = 0
mock_task = MagicMock()
+ mock_task.error_code_mapping = {"unavailable": "The requested item is unavailable"}
+ mapped_error = UserDefinedError(
+ error_code="unavailable", reasoning="The requested item is unavailable", confidence_float=1.0
+ )
+
+ async def classify(action, *_args):
+ action.errors = [mapped_error]
+ return [MagicMock(success=True)]
+
mock_step = MagicMock()
mock_step.order = 0
@@ -676,12 +686,14 @@ async def test_terminate_calls_handler_and_raises(mock_scraped_page, mock_ai):
patch(
"skyvern.core.script_generations.script_skyvern_page.handle_terminate_action",
new_callable=AsyncMock,
- return_value=[MagicMock(success=True)],
+ side_effect=classify,
) as mock_handler,
):
- with pytest.raises(ScriptTerminationException, match="Terminate called: error1; error2"):
+ with pytest.raises(ScriptTerminationException, match="Terminate called: error1; error2") as raised:
await script_page.terminate(errors=["error1", "error2"])
+ assert raised.value.user_defined_errors == [mapped_error]
+
# Verify handler was called with correct arguments
mock_handler.assert_called_once()
call_args = mock_handler.call_args
diff --git a/tests/unit/test_taskv3_tools.py b/tests/unit/test_taskv3_tools.py
index d110222f2..a8c8c92f8 100644
--- a/tests/unit/test_taskv3_tools.py
+++ b/tests/unit/test_taskv3_tools.py
@@ -7906,12 +7906,14 @@ _SEGMENTED_DATE_SKINNED_SUBPIXEL_HTML = """
@pytest.mark.parametrize(
"template", [_SEGMENTED_DATE_HTML, _SEGMENTED_DATE_SKINNED_SUBPIXEL_HTML], ids=["unclickable", "skinned-subpixel"]
)
[email protected]("coordinate_click", [False, True], ids=["focus", "press"])
async def test_type_into_an_unclickable_field_never_reports_a_fill_that_did_not_land(
- misroute: str, text: str, template: str
+ misroute: str, text: str, template: str, coordinate_click: bool, monkeypatch: pytest.MonkeyPatch
) -> None:
- # Reaching the field by focus() alone proves nothing about the keystrokes. A success here would
- # turn today's loud failure into a date that reads as filled and is not.
+ # Reaching the field by focus() alone, or by a press no hit test checked, proves nothing about the
+ # keystrokes. A success here would turn today's loud failure into a date that reads as filled and is not.
# A raised error is the loud outcome too: the tool wrapper turns it into a tool error.
+ monkeypatch.setattr(settings, "TASK_V3_TYPE_COORDINATE_CLICK", coordinate_click)
html = template + f"<script>{misroute}</script>"
async with _content_page(html) as page:
tools = build_browser_tools(_fixed_page_provider(page))
@@ -7921,6 +7923,12 @@ async def test_type_into_an_unclickable_field_never_reports_a_fill_that_did_not_
assert "outside of the viewport" in str(exc), exc
else:
assert r.status == "error", r.content
+ if coordinate_click and text != text.strip() and template is _SEGMENTED_DATE_SKINNED_SUBPIXEL_HTML:
+ # Only the on-screen sub-pixel field is pressed. That path Tabs out, so the widget has
+ # committed its trimmed value: it is reported and left in place, not taken back.
+ assert "holds '2023'" in r.content, r.content
+ assert await page.eval_on_selector("#year", "el => el.value") == "2023"
+ return
assert "NOT filled" in r.content, r.content
assert await page.eval_on_selector("#year", "el => el.value") == ""
assert await page.eval_on_selector("#month", "el => el.value") == ""
@@ -7946,6 +7954,7 @@ _UNCLICKABLE_TYPEAHEAD_HTML = """
city.addEventListener("blur", () => {{ city.value = ""; }});
city.addEventListener("input", () => {{ window.__cityTyped = true; }});
city.addEventListener("focus", () => {{ window.__cityFocused = true; }});
+ document.addEventListener("pointerdown", () => {{ window.__cityPressed = true; }});
{echo}
</script>
"""
@@ -8030,11 +8039,13 @@ _ECHO_SCRIPT = (
"skinned-subpixel-declared-slow-rows",
],
)
[email protected]("coordinate_click", [False, True], ids=["focus", "press"])
async def test_type_into_an_unclickable_typeahead_never_reports_the_raw_query_as_filled(
- template: str, aria: str, delay_ms: int, echo: str
+ template: str, aria: str, delay_ms: int, echo: str, coordinate_click: bool, monkeypatch: pytest.MonkeyPatch
) -> None:
# A field that DECLARES a list is refused before it is focused, so the page must be untouched:
# asserting only the verdict cannot tell that guard from a later one reaching the same answer.
+ monkeypatch.setattr(settings, "TASK_V3_TYPE_COORDINATE_CLICK", coordinate_click)
declares = bool(aria)
async with _content_page(template.format(aria=aria, delay_ms=delay_ms, echo=echo)) as page:
tools = build_browser_tools(_fixed_page_provider(page))
@@ -8046,7 +8057,7 @@ async def test_type_into_an_unclickable_typeahead_never_reports_the_raw_query_as
assert r.status == "error", r.content
assert "NOT filled" in r.content, r.content
if declares:
- assert await page.evaluate("() => !window.__cityFocused && !window.__cityTyped")
+ assert await page.evaluate("() => !window.__cityFocused && !window.__cityTyped && !window.__cityPressed")
@_skip_no_browser
@@ -8071,6 +8082,129 @@ async def test_type_fills_a_segment_input_the_click_cannot_reach(html: str) -> N
assert await page.eval_on_selector("#year-display", "el => el.textContent") == "2023"
+# SKY-16501: the sub-pixel segment under its own display, from a widget that moves its section cursor only
+# on a TRUSTED pointer event on the display. Until then a digit renders in the display and the input stays
+# empty; the widget commits the value to its form model when focus leaves. Nothing here moves focus on a
+# press, so a fix that relies on the press to focus the input fails too.
+_SEGMENTED_DATE_TRUSTED_PRESS_HTML = """
+<div role="group" aria-label="Start date" style="display:flex;width:240px;height:30px">
+ <div style="position:relative;width:60px;height:30px">
+ <input id="year" type="text" role="spinbutton" aria-label="Year"
+ style="position:absolute;left:4px;top:4px;width:1px;height:0.5px;padding:0;border:0;box-sizing:border-box">
+ <div id="year-display" aria-hidden="true" style="position:absolute;inset:0;z-index:1;background:#fff">YYYY</div>
+ </div>
+ <div style="position:relative;width:40px;height:30px">
+ <input id="month" type="text" role="spinbutton" aria-label="Month"
+ style="position:absolute;left:4px;top:4px;width:1px;height:0.5px;padding:0;border:0;box-sizing:border-box">
+ <div aria-hidden="true" style="position:absolute;inset:0;z-index:1;background:#fff">MM</div>
+ </div>
+</div>
+<script>
+ const year = document.getElementById("year");
+ const display = document.getElementById("year-display");
+ let armed = false;
+ display.addEventListener("pointerdown", (e) => { if (e.isTrusted) armed = true; });
+ year.addEventListener("keydown", (e) => {
+ if (!/^[0-9]$/.test(e.key)) return;
+ e.preventDefault();
+ if (!armed) {
+ display.textContent = (display.textContent === "YYYY" ? "" : display.textContent) + e.key;
+ return;
+ }
+ year.value += e.key;
+ year.dispatchEvent(new Event("input", { bubbles: true }));
+ });
+ year.addEventListener("input", () => { display.textContent = year.value || "YYYY"; });
+ year.addEventListener("blur", () => { window.__committedYear = year.value; });
+</script>
+"""
+
+
+@_skip_no_browser
[email protected]("coordinate_click", [False, True], ids=["focus", "press"])
+async def test_type_fills_a_segment_that_takes_keys_only_after_a_trusted_press(
+ coordinate_click: bool, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.setattr(settings, "TASK_V3_TYPE_COORDINATE_CLICK", coordinate_click)
+ async with _content_page(_SEGMENTED_DATE_TRUSTED_PRESS_HTML) as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "type").handler({"selector": "#year", "text": "2023"})
+ if not coordinate_click:
+ # focus() alone: the digits show in the display and the field stays empty, so it is refused.
+ assert r.status == "error", r.content
+ assert "NOT filled" in r.content, r.content
+ assert await page.eval_on_selector("#year", "el => el.value") == ""
+ return
+ assert r.status == "ok", r.content
+ assert await page.eval_on_selector("#year", "el => el.value") == "2023"
+ assert await page.evaluate("() => window.__committedYear") == "2023"
+ assert await page.eval_on_selector("#month", "el => el.value") == ""
+
+
+_COMMIT_ON_BLUR = 'year.addEventListener("blur", () => { window.__committedYear = year.value; });'
+
+
+def _segment_reformatting_on_blur(reformat: str) -> str:
+ html = _SEGMENTED_DATE_TRUSTED_PRESS_HTML.replace(
+ _COMMIT_ON_BLUR,
+ 'year.addEventListener("blur", () => { ' + reformat + " window.__committedYear = year.value; });",
+ )
+ assert html != _SEGMENTED_DATE_TRUSTED_PRESS_HTML
+ return html
+
+
+@_skip_no_browser
+ ("reformat", "typed", "held"),
+ [
+ ('year.value = year.value.padStart(2, "0");', "3", "03"),
+ ('if (year.value.length === 2) year.value = "20" + year.value;', "23", "2023"),
+ # A substitution the model did not ask for: reported, never a success.
+ ("year.value = String(Math.min(12, Number(year.value)));", "13", "12"),
+ ('year.value = "1999";', "2023", "1999"),
+ # Differs only by whitespace: taking it back would empty the input while the widget keeps "3".
+ ("year.value = year.value.trim();", "3 ", "3"),
+ ],
+ ids=["zero-pad", "century", "clamp", "unrelated", "trim"],
+)
+async def test_type_reports_a_value_the_widget_committed_in_place_of_the_typed_text(
+ reformat: str, typed: str, held: str, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ monkeypatch.setattr(settings, "TASK_V3_TYPE_COORDINATE_CLICK", True)
+ async with _content_page(_segment_reformatting_on_blur(reformat)) as page:
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "type").handler({"selector": "#year", "text": typed})
+ assert r.status == "error", r.content
+ assert f"holds '{held}'" in r.content, r.content
+ assert "NOT filled" not in r.content, r.content
+ assert await page.eval_on_selector("#year", "el => el.value") == held
+ assert await page.evaluate("() => window.__committedYear") == held
+
+
+@_skip_no_browser
[email protected]("secret", ["typed-credential", "one-time-code-box"])
+async def test_type_does_not_echo_a_changed_value_it_may_not_show(secret: str, monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(settings, "TASK_V3_TYPE_COORDINATE_CLICK", True)
+ html = _segment_reformatting_on_blur('year.value = year.value + "9";')
+ text = "4417"
+ resolve = None
+ if secret == "typed-credential":
+ text = "placeholder_pin"
+ resolve = lambda t: "4417" if t == "placeholder_pin" else t # noqa: E731
+ else:
+ html = html.replace('<input id="year"', '<input id="year" data-skyvern-otp-box')
+ async with _content_page(html) as page:
+ tools = build_browser_tools(_fixed_page_provider(page), resolve_typed_text=resolve)
+ r = await _tool(tools, "type").handler({"selector": "#year", "text": text})
+ assert await page.eval_on_selector("#year", "el => el.value") == "44179"
+ assert r.status == "error", r.content
+ assert "different value" in r.content, r.content
+ assert "4417" not in r.content, r.content
+
+
# The field hangs directly off <body>, so EVERY overlay on the page is "inside its parent". A purely
# structural skin test reads a full-viewport modal as this field's own decoration.
_COVERED_MODAL_SHARES_PARENT_HTML = """
@@ -8178,6 +8312,99 @@ async def test_type_stops_when_the_forced_click_navigates_away() -> None:
await browser.close()
+@_skip_no_browser
+async def test_type_stops_when_the_coordinate_press_navigates_away(monkeypatch: pytest.MonkeyPatch) -> None:
+ from playwright.async_api import async_playwright # noqa: PLC0415
+
+ monkeypatch.setattr(settings, "TASK_V3_TYPE_COORDINATE_CLICK", True)
+ start_html = _SEGMENTED_DATE_TRUSTED_PRESS_HTML.replace(
+ "if (e.isTrusted) armed = true;", 'if (e.isTrusted) location.href = "/elsewhere";'
+ )
+ async with async_playwright() as pw:
+ browser = await pw.chromium.launch(headless=True, args=["--use-mock-keychain", "--password-store=basic"])
+ try:
+ context = await browser.new_context(viewport={"width": 1024, "height": 900})
+ page = await context.new_page()
+
+ async def _serve(route: Any) -> None:
+ # The destination has a field the same selector matches.
+ elsewhere = route.request.url.endswith("/elsewhere")
+ body = '<input id="year" type="text">' if elsewhere else start_html
+ await route.fulfill(status=200, content_type="text/html", body=body)
+
+ await page.route("**/*", _serve)
+ await page.goto("http://segment.test/start")
+ tools = build_browser_tools(_fixed_page_provider(page))
+ try:
+ r = await _tool(tools, "type").handler({"selector": "#year", "text": "2023"})
+ except Exception as exc:
+ assert "outside of the viewport" in str(exc), exc
+ else:
+ assert r.status == "error", r.content
+ assert page.url.endswith("/elsewhere"), page.url
+ assert await page.eval_on_selector("#year", "el => el.value") == ""
+ finally:
+ await browser.close()
+
+
+@_skip_no_browser
[email protected]("origin", ["same", "cross"])
[email protected]("placement", ["inside", "clipped"])
+async def test_type_presses_a_framed_segment_only_where_its_frame_shows_it(
+ origin: str, placement: str, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ # The press is a main-page mouse event, so it must land in frame-offset coordinates. A field the frame
+ # clips (fixed below the frame's 120px height) has its centre over the parent page, where a
+ # page-wide decoy would take the press instead.
+ from playwright.async_api import async_playwright # noqa: PLC0415
+
+ monkeypatch.setattr(settings, "TASK_V3_TYPE_COORDINATE_CLICK", True)
+ monkeypatch.setattr(settings, "TASK_V3_FRAME_PERCEPTION", True)
+ frame_html = _SEGMENTED_DATE_TRUSTED_PRESS_HTML
+ decoy = ""
+ if placement == "clipped":
+ frame_html = frame_html.replace(
+ 'style="display:flex;width:240px;height:30px"', 'style="position:fixed;left:0;top:400px"', 1
+ )
+ decoy = (
+ '<button style="position:absolute;inset:0;width:1024px;height:900px"'
+ ' onpointerdown="window.__decoyPressed = true">decoy</button>'
+ )
+ frame_host = "parent.test" if origin == "same" else "child.test"
+ parent_html = (
+ f'{decoy}<iframe src="http://{frame_host}/frame" style="position:absolute;left:150px;top:120px;'
+ 'width:300px;height:120px;border:0;z-index:1"></iframe>'
+ )
+ async with async_playwright() as pw:
+ browser = await pw.chromium.launch(headless=True, args=["--use-mock-keychain", "--password-store=basic"])
+ try:
+ context = await browser.new_context(viewport={"width": 1024, "height": 900})
+
+ async def _serve(route: Any) -> None:
+ body = frame_html if route.request.url.endswith("/frame") else parent_html
+ await route.fulfill(status=200, content_type="text/html", body=body)
+
+ await context.route("**/*", _serve)
+ page = await context.new_page()
+ await page.goto("http://parent.test/start")
+ frame = page.frames[1]
+ await frame.wait_for_selector("#year", state="attached")
+ tools = build_browser_tools(_fixed_page_provider(page))
+ r = await _tool(tools, "type").handler({"selector": "#year", "text": "2023"})
+ if placement == "inside":
+ assert r.status == "ok", r.content
+ assert await frame.evaluate("() => window.__committedYear") == "2023"
+ return
+ assert await page.evaluate("() => window.__decoyPressed") is None
+ assert r.status == "error", r.content
+ assert "could only be focused, not clicked" in r.content, r.content
+ assert await frame.eval_on_selector("#year", "el => el.value") == ""
+ finally:
+ await browser.close()
+
+
# scrollIntoView inherits CSS scroll-behavior. Smooth scrolling animates while the rect is read
# synchronously on the next line, so the element is still off-screen and the probe sees nothing.
_SMOOTH_SCROLL_BELOW_FOLD_HTML = """
diff --git a/tests/unit/test_twilio_integration_routes.py b/tests/unit/test_twilio_integration_routes.py
new file mode 100644
index 000000000..282de07f9
--- /dev/null
+++ b/tests/unit/test_twilio_integration_routes.py
@@ -0,0 +1,1437 @@
+import asyncio
+import logging
+from collections.abc import Iterator
+from contextlib import asynccontextmanager
+from datetime import UTC, datetime, timezone
+from types import SimpleNamespace
+from typing import Any
+from unittest.mock import AsyncMock, Mock, create_autospec
+
+import pytest
+from fastapi import FastAPI, HTTPException, Request, Response
+from fastapi.testclient import TestClient
+from pydantic import BaseModel, ValidationError
+from sqlalchemy import event, select
+from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
+from sqlalchemy.pool import QueuePool
+
+from skyvern.forge import request_logging
+from skyvern.forge.agent_functions import AgentFunction
+from skyvern.forge.sdk.db.agent_db import AgentDB
+from skyvern.forge.sdk.db.models import (
+ Base,
+ BitwardenLoginCredentialParameterModel,
+ OnePasswordCredentialParameterModel,
+ OrganizationPhoneNumberModel,
+ OrganizationSMSConfigModel,
+ TOTPCodeModel,
+)
+from skyvern.forge.sdk.db.repositories.sms import SMSRepository
+from skyvern.forge.sdk.db.repositories.workflow_parameters import WorkflowParametersRepository
+from skyvern.forge.sdk.db.utils import convert_to_bitwarden_login_credential_parameter
+from skyvern.forge.sdk.routes import credentials, run_blocks, sms_inbound, twilio_integration
+from skyvern.forge.sdk.schemas.credentials import (
+ CreateCredentialRequest,
+ CredentialItem,
+ CredentialType,
+ CredentialVaultType,
+ NonEmptyPasswordCredential,
+ PasswordCredential,
+ TotpType,
+)
+from skyvern.forge.sdk.schemas.organizations import CreateTwilioCredentialRequest, Organization, TwilioCredential
+from skyvern.forge.sdk.schemas.sms import (
+ DisablePhoneNumberRequest,
+ EnablePhoneNumberRequest,
+ OrganizationPhoneNumber,
+ SMSConfig,
+)
+from skyvern.forge.sdk.services.bitwarden import BitwardenConstants
+from skyvern.forge.sdk.services.credential.skyvern_credential_vault_service import SkyvernCredentialVaultService
+from skyvern.forge.sdk.services.twilio_service import TwilioApiError, build_inbound_sms_url, compute_twilio_signature
+from skyvern.forge.sdk.workflow import context_manager as cm
+from skyvern.forge.sdk.workflow.context_manager import WorkflowRunContext
+from skyvern.forge.sdk.workflow.models.parameter import (
+ BitwardenLoginCredentialParameter,
+ OnePasswordCredentialParameter,
+)
+from skyvern.forge.sdk.workflow.models.tags import CallerType
+from skyvern.forge.sdk.workflow.workflow_definition_converter import convert_workflow_definition
+from skyvern.schemas.credential_type import CredentialType as LoginCredentialType
+from skyvern.schemas.run_blocks import LoginRequest
+from skyvern.schemas.workflows import (
+ BitwardenLoginCredentialParameterYAML,
+ OnePasswordCredentialParameterYAML,
+ WorkflowDefinitionYAML,
+ WorkflowStatus,
+)
+from skyvern.services import otp_service
+from tests.unit.forge.sdk.db import conftest as db_fixtures
+
+agent_db = db_fixtures.agent_db
+db_engine = db_fixtures.db_engine
+ORG = SimpleNamespace(organization_id="o_test")
+ACCOUNT_SID = "AC" + "0123456789abcdef" * 2
+OTHER_ACCOUNT_SID = "AC" + "fedcba9876543210" * 2
+API_KEY_SID = "SK" + "0123456789abcdef" * 2
+
+
[email protected]("method", ["POST", "PUT", "PATCH", "DELETE"])
[email protected]("prefix", ["/v1", "/api/v1"])
[email protected]("path", ["integrations/twilio", "integrations/twilio/credentials", "sms", "sms/inbound/test"])
[email protected]("content_type", ["application/json", "application/x-www-form-urlencoded", ""])
+def test_twilio_middleware_hides_malformed_secret_bodies(
+ monkeypatch: pytest.MonkeyPatch,
+ caplog: pytest.LogCaptureFixture,
+ method: str,
+ prefix: str,
+ path: str,
+ content_type: str,
+) -> None:
+ api = FastAPI()
+ api.add_middleware(request_logging.RequestLoggingMiddleware)
+ monkeypatch.setattr(request_logging.settings, "LOG_RAW_API_REQUESTS", True)
+
+ @api.api_route("/{path:path}", methods=[method])
+ async def reject(request: Request) -> Response:
+ return Response(await request.body(), status_code=400, media_type="text/plain")
+
+ body = '{"credential":{"auth_token":"AUTH_SENTINEL"' if content_type == "application/json" else "Body=OTP_SENTINEL"
+ with caplog.at_level(logging.INFO), TestClient(api) as client:
+ response = client.request(method, f"{prefix}/{path}", content=body, headers={"content-type": content_type})
+ assert response.status_code == 400
+ assert "api.raw_request" in caplog.text
+ assert "SENTINEL" not in caplog.text
+ assert request_logging.REDACTED in caplog.text
+ print(f"R1: {method} {prefix}/{path} {content_type or 'missing type'}; captured logs contain no sentinel")
+ print(caplog.text)
+
+
[email protected]("model", [CreateTwilioCredentialRequest, twilio_integration.TwilioCredentialResponse])
[email protected]("shape", ["nested-missing-field", "invalid-wrapper-field", "invalid-wrapper"])
+def test_twilio_route_models_hide_validation_inputs(model: type[BaseModel], shape: str) -> None:
+ secret = {"auth_token": "AUTH_SENTINEL", "api_key_secret": "KEY_SENTINEL"}
+ payload = {"credential": secret if shape == "nested-missing-field" else list(secret.values())}
+ with pytest.raises(ValidationError) as caught:
+ model.model_validate([payload] if shape == "invalid-wrapper" else payload)
+ error = caught.value
+ for rendered in (str(error), repr(error), str(error.errors()), error.json()):
+ assert "AUTH_SENTINEL" not in rendered
+ assert "KEY_SENTINEL" not in rendered
+
+
[email protected]("model", [CreateTwilioCredentialRequest, twilio_integration.TwilioCredentialResponse])
+def test_twilio_route_models_hide_malformed_json_inputs(model: type[BaseModel]) -> None:
+ with pytest.raises(ValidationError) as caught:
+ model.model_validate_json('{"credential":{"auth_token":"AUTH_SENTINEL","api_key_secret":"KEY_SENTINEL"}')
+ for rendered in (str(caught.value), repr(caught.value), str(caught.value.errors()), caught.value.json()):
+ assert "AUTH_SENTINEL" not in rendered
+ assert "KEY_SENTINEL" not in rendered
+
+
+def _credential() -> TwilioCredential:
+ return TwilioCredential(account_sid=ACCOUNT_SID, auth_token="auth-token-secret")
+
+
+def _credential_token() -> SimpleNamespace:
+ return SimpleNamespace(credential=_credential())
+
+
+def _config(mode: str = "connected") -> SMSConfig:
+ return SMSConfig(
+ sms_config_id="smsc_test",
+ organization_id=ORG.organization_id,
+ mode=mode,
+ daily_ingest_cap=100,
+ created_at=datetime(2026, 1, 1, tzinfo=timezone.utc),
+ )
+
+
+def _registered_phone(config: SMSConfig, **overrides: object) -> OrganizationPhoneNumber:
+ values: dict[str, object] = {
+ "phone_number": "+14155550123",
+ "phone_number_id": "pn_existing",
+ "provider": "twilio",
+ "organization_id": ORG.organization_id,
+ "sms_config_id": config.sms_config_id,
+ "provider_number_sid": "PN0123456789abcdef0123456789abcdef",
+ "previous_sms_url": "https://customer.example/sms",
+ "previous_sms_method": "GET",
+ "previous_sms_application_sid": "AP_customer",
+ "credential_id": None,
+ "status": "active",
+ "created_at": config.created_at,
+ "modified_at": config.created_at,
+ }
+ values.update(overrides)
+ return OrganizationPhoneNumber.model_validate(values)
+
+
+def _database(*, token: SimpleNamespace | None = None) -> SimpleNamespace:
+ organizations = SimpleNamespace(
+ get_valid_org_auth_token=AsyncMock(return_value=token),
+ replace_org_auth_token=AsyncMock(),
+ invalidate_org_auth_tokens=AsyncMock(),
+ )
+ credentials = SimpleNamespace(get_credential=AsyncMock(return_value=SimpleNamespace(credential_id="cred_test")))
+ sms = create_autospec(SMSRepository, instance=True, spec_set=True)
+ sms.create_sms_config.return_value = (_config(), "webhook-secret")
+ for method in (
+ "get_connected_sms_config",
+ "get_sms_config_with_secret",
+ "get_sms_config_with_secret_for_inbound",
+ "get_sms_config",
+ "get_phone_number_by_number",
+ "get_phone_number",
+ "update_phone_number",
+ ):
+ getattr(sms, method).return_value = None
+ sms.list_sms_configs.return_value = []
+ sms.list_phone_numbers.return_value = []
+ sms.count_active_phone_numbers_for_config.return_value = 0
+ sms.count_active_twilio_phone_numbers_for_config.return_value = 0
+ return SimpleNamespace(organizations=organizations, credentials=credentials, sms=sms)
+
+
+def _patch_client(monkeypatch: pytest.MonkeyPatch) -> tuple[SimpleNamespace, Mock]:
+ client = SimpleNamespace(
+ get_account=AsyncMock(return_value={"status": "active"}),
+ list_incoming_phone_numbers=AsyncMock(return_value=[]),
+ get_incoming_phone_number=AsyncMock(),
+ update_inbound_sms_config=AsyncMock(),
+ )
+ factory = Mock(return_value=client)
+ monkeypatch.setattr(twilio_integration, "build_client_from_credential", factory)
+ return client, factory
+
+
+def _provider_number(**overrides: object) -> dict[str, object]:
+ result: dict[str, object] = {
+ "sid": "PN0123456789abcdef0123456789abcdef",
+ "phone_number": "+14155550123",
+ "sms_url": "",
+ "sms_method": "POST",
+ "sms_application_sid": "",
+ "capabilities": {"SMS": True},
+ }
+ result.update(overrides)
+ return result
+
+
+def _apply_provider_routing_then_raise(provider: dict[str, object], error: BaseException) -> object:
+ async def apply_then_raise(*args: object, **kwargs: object) -> None:
+ provider["sms_url"] = args[1]
+ provider["sms_method"] = args[2]
+ provider["sms_application_sid"] = args[3]
+ raise error
+
+ return apply_then_raise
+
+
[email protected](autouse=True)
+def enable_twilio_sms_routes(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setattr(
+ twilio_integration.SettingsManager,
+ "get_settings",
+ lambda: SimpleNamespace(TWILIO_SMS_2FA_ENABLED=True),
+ )
+
+
+async def test_postgres_lifecycle_permit_reserves_pool_connection_for_repository_work() -> None:
+ class FakePool(QueuePool):
+ def __init__(self) -> None:
+ super().__init__(lambda: None, pool_size=2, max_overflow=0)
+ self.connections = asyncio.Semaphore(2)
+ self.active = 0
+ self.maximum = 0
+
+ pool = FakePool()
+ engine = SimpleNamespace(
+ dialect=SimpleNamespace(name="postgresql"),
+ pool=pool,
+ )
+ database_locks: dict[str, asyncio.Lock] = {}
+ progress = 0
+
+ class FakeSession:
+ def get_bind(self) -> SimpleNamespace:
+ return engine
+
+ async def execute(self, statement: object, params: object) -> None:
+ lock_key = str(params["lock_key"]) # type: ignore[index]
+ lock = database_locks.setdefault(lock_key, asyncio.Lock())
+ if "pg_advisory_unlock" in str(statement):
+ lock.release()
+ else:
+ await lock.acquire()
+
+ async def __aenter__(self) -> "FakeSession":
+ await pool.connections.acquire()
+ pool.active += 1
+ pool.maximum = max(pool.maximum, pool.active)
+ return self
+
+ async def __aexit__(self, *args: object) -> None:
+ pool.active -= 1
+ pool.connections.release()
+
+ class FakeSessionFactory:
+ kw = {"bind": SimpleNamespace(sync_engine=engine)}
+
+ def __call__(self) -> FakeSession:
+ return FakeSession()
+
+ repository = SMSRepository(FakeSessionFactory())
+
+ async def worker(index: int) -> None:
+ nonlocal progress
+ async with repository.lifecycle_lock(f"org-{index}", f"+14155550{index:03d}"):
+ async with repository.Session():
+ progress += 1
+ await asyncio.sleep(0.01)
+
+ await asyncio.wait_for(asyncio.gather(worker(1), worker(2)), timeout=1)
+ assert progress == 2
+ assert pool.maximum == 2
+
+
+async def test_update_phone_number_persists_provider_fields_and_quarantine(
+ sqlite_engine: object, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ repository = SMSRepository(async_sessionmaker(sqlite_engine, expire_on_commit=False))
+ monkeypatch.setattr("skyvern.forge.sdk.workflow.secret_encryption.settings.ENABLE_ENCRYPTION", True)
+ monkeypatch.setattr("skyvern.forge.sdk.workflow.secret_encryption.settings.ENCRYPTOR_AES_SECRET_KEY", "test-secret")
+ async with repository.Session() as session:
+ session.add(
+ OrganizationSMSConfigModel(
+ sms_config_id="smsc_update",
+ organization_id="o_test",
+ mode="connected",
+ encrypted_webhook_secret="unused",
+ webhook_secret_encrypted_method="aes",
+ )
+ )
+ session.add(
+ OrganizationPhoneNumberModel(
+ phone_number_id="pn_update",
+ organization_id="o_test",
+ sms_config_id="smsc_update",
+ phone_number="+14155550123",
+ provider="skyvern",
+ status="disabled",
+ )
+ )
+ await session.commit()
+ quarantine_until = datetime(2026, 8, 20, 12, tzinfo=UTC)
+ updated = await repository.update_phone_number(
+ "pn_update",
+ "o_test",
+ status="quarantined",
+ provider="twilio",
+ sms_config_id="smsc_update",
+ provider_number_sid="PN_UPDATED",
+ previous_sms_url="https://customer.example/sms",
+ previous_sms_method="POST",
+ previous_sms_application_sid="AP_UPDATED",
+ credential_id=None,
+ quarantined_until=quarantine_until,
+ )
+ assert updated is not None
+ assert updated.provider == "twilio"
+ assert updated.previous_sms_url != "https://customer.example/sms"
+ assert updated.quarantined_until is not None
+ async with repository.Session() as session:
+ row = await session.scalar(
+ select(OrganizationPhoneNumberModel).where(OrganizationPhoneNumberModel.phone_number_id == "pn_update")
+ )
+ assert row is not None
+ assert row.previous_sms_url == updated.previous_sms_url
+ assert row.quarantined_until is not None
+
+
+async def test_delete_sms_config_rejects_active_non_twilio_phone(
+ sqlite_engine: object,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ repository = SMSRepository(async_sessionmaker(sqlite_engine, expire_on_commit=False))
+ async with repository.Session() as session:
+ session.add(
+ OrganizationSMSConfigModel(
+ sms_config_id="smsc_managed",
+ organization_id="o_test",
+ mode="connected",
+ encrypted_webhook_secret="unused",
+ webhook_secret_encrypted_method="aes",
+ )
+ )
+ session.add(
+ OrganizationPhoneNumberModel(
+ phone_number_id="pn_managed",
+ organization_id="o_test",
+ sms_config_id="smsc_managed",
+ phone_number="+14155550123",
+ provider="skyvern",
+ status="active",
+ )
+ )
+ await session.commit()
+ monkeypatch.setattr(twilio_integration.app, "DATABASE", SimpleNamespace(sms=repository))
+ with pytest.raises(HTTPException) as exc_info:
+ await twilio_integration.delete_sms_config("smsc_managed", ORG)
+ assert exc_info.value.status_code == 409
+ async with repository.Session() as session:
+ row = await session.scalar(
+ select(OrganizationSMSConfigModel).where(OrganizationSMSConfigModel.sms_config_id == "smsc_managed")
+ )
+ assert row is not None
+ assert row.deleted_at is None
+
+
+async def test_delete_sms_config_rejects_managed_config(monkeypatch: pytest.MonkeyPatch) -> None:
+ database = _database()
+ database.sms.get_sms_config.return_value = _config("managed")
+ monkeypatch.setattr(twilio_integration.app, "DATABASE", database)
+ with pytest.raises(HTTPException) as exc_info:
+ await twilio_integration.delete_sms_config("smsc_test", ORG)
+ assert exc_info.value.status_code == 409
+ database.sms.delete_sms_config.assert_not_awaited()
+
+
+ ("operation", "expected_detail"),
+ [
+ ("replace", "Disable all connected Twilio phone numbers before changing Twilio accounts"),
+ ("delete", "Disable all connected Twilio phone numbers before disconnecting Twilio"),
+ ],
+)
+async def test_credential_lifecycle_rejects_active_numbers(
+ monkeypatch: pytest.MonkeyPatch,
+ operation: str,
+ expected_detail: str,
+) -> None:
+ database = _database(token=_credential_token())
+ database.sms.get_connected_sms_config.return_value = (_config(), "webhook-secret")
+ database.sms.count_active_twilio_phone_numbers_for_config.return_value = 1
+ monkeypatch.setattr(twilio_integration.app, "DATABASE", database)
+ _, factory = _patch_client(monkeypatch)
+ with pytest.raises(HTTPException) as exc_info:
+ if operation == "replace":
+ await twilio_integration.create_twilio_credential(
+ CreateTwilioCredentialRequest(
+ credential=TwilioCredential(
+ account_sid=OTHER_ACCOUNT_SID,
+ auth_token="replacement-auth-token",
+ )
+ ),
+ ORG,
+ )
+ else:
+ await twilio_integration.delete_twilio_credential(ORG)
+ assert exc_info.value.status_code == 409
+ assert exc_info.value.detail == expected_detail
+ if operation == "replace":
+ factory.assert_not_called()
+ database.organizations.replace_org_auth_token.assert_not_awaited()
+
+
[email protected]("phone_status", ["active", "quarantined"])
+async def test_credential_replacement_keeps_signing_token(
+ agent_db: AgentDB,
+ monkeypatch: pytest.MonkeyPatch,
+ phone_status: str,
+) -> None:
+ database = _database(token=_credential_token())
+ database.sms.get_connected_sms_config.return_value = (_config(), "webhook-secret")
+ database.sms.count_active_twilio_phone_numbers_for_config = (
+ agent_db.sms.count_active_twilio_phone_numbers_for_config
+ )
+ async with agent_db.Session() as session:
+ session.add(OrganizationPhoneNumberModel(**_registered_phone(_config(), status=phone_status).model_dump()))
+ await session.commit()
+ monkeypatch.setattr(twilio_integration.app, "DATABASE", database)
+ _, factory = _patch_client(monkeypatch)
+ credential = TwilioCredential(
+ account_sid=_credential().account_sid,
+ api_key_sid=API_KEY_SID,
+ api_key_secret="replacement-api-key-secret",
+ )
+ with pytest.raises(HTTPException) as caught:
+ await twilio_integration.create_twilio_credential(CreateTwilioCredentialRequest(credential=credential), ORG)
+ assert caught.value.status_code == 400
+ assert "auth token is required" in caught.value.detail
+ factory.assert_not_called()
+ database.organizations.replace_org_auth_token.assert_not_awaited()
+ credential.auth_token = _credential().auth_token
+ response = await twilio_integration.create_twilio_credential(
+ CreateTwilioCredentialRequest(credential=credential), ORG
+ )
+ assert response.credential.has_auth_token
+ assert database.organizations.replace_org_auth_token.await_args.kwargs["token"].auth_token == credential.auth_token
+
+
[email protected]("operation", ["replace", "delete"])
+async def test_org_lock_blocks_credential_lifecycle_during_enable(
+ monkeypatch: pytest.MonkeyPatch,
+ operation: str,
+) -> None:
+ database = _database(token=_credential_token())
+ config = _config()
+ database.sms.get_connected_sms_config.return_value = (config, "webhook-secret")
+ saved = SimpleNamespace(phone_number_id="pn_created", status="active")
+ database.sms.create_phone_number.return_value = saved
+ database.sms.update_phone_number.return_value = saved
+ monkeypatch.setattr(twilio_integration.app, "DATABASE", database)
+ client, _ = _patch_client(monkeypatch)
+ provider = _provider_number(phone_number="+14155550123")
+ provider_reads = 0
+ enable_locked = asyncio.Event()
+ release_enable = asyncio.Event()
+
+ async def provider_lookup(phone_number_sid: str) -> dict[str, object]:
+ nonlocal provider_reads
+ provider_reads += 1
+ if provider_reads == 2:
+ enable_locked.set()
+ await release_enable.wait()
+ return provider
+
+ client.get_incoming_phone_number.side_effect = provider_lookup
+ lock = asyncio.Lock()
+ lifecycle_waiting = asyncio.Event()
+
+ @asynccontextmanager
+ async def lifecycle_lock(organization_id: str, phone_number: str) -> Iterator[None]:
+ await lock.acquire()
+ try:
+ yield
+ finally:
+ lock.release()
+
+ @asynccontextmanager
+ async def organization_lock(organization_id: str) -> Iterator[None]:
+ lifecycle_waiting.set()
+ await lock.acquire()
+ try:
+ yield
+ finally:
+ lock.release()
+
+ database.sms.lifecycle_lock.side_effect = lifecycle_lock
+ database.sms.organization_lock.side_effect = organization_lock
+ enable_task = asyncio.create_task(
+ twilio_integration.enable_twilio_phone_number(
+ EnablePhoneNumberRequest(phone_number_sid="PN0123456789abcdef0123456789abcdef"), ORG
+ )
+ )
+ await enable_locked.wait()
+ lifecycle_task = asyncio.create_task(
+ twilio_integration.create_twilio_credential(CreateTwilioCredentialRequest(credential=_credential()), ORG)
+ if operation == "replace"
+ else twilio_integration.delete_twilio_credential(ORG)
+ )
+ await lifecycle_waiting.wait()
+ if operation == "replace":
+ assert not database.organizations.replace_org_auth_token.await_count
+ else:
+ assert not database.organizations.invalidate_org_auth_tokens.await_count
+ release_enable.set()
+ await asyncio.gather(enable_task, lifecycle_task)
+ if operation == "replace":
+ database.organizations.replace_org_auth_token.assert_awaited_once()
+ else:
+ database.organizations.invalidate_org_auth_tokens.assert_awaited_once()
+
+
+async def test_enable_rejects_cross_org_credential_id(monkeypatch: pytest.MonkeyPatch) -> None:
+ database = _database(token=_credential_token())
+ database.credentials.get_credential.return_value = None
+ monkeypatch.setattr(twilio_integration.app, "DATABASE", database)
+ with pytest.raises(HTTPException) as exc_info:
+ await twilio_integration.enable_twilio_phone_number(
+ EnablePhoneNumberRequest(
+ phone_number_sid="PN0123456789abcdef0123456789abcdef", credential_id="cred_foreign"
+ ),
+ ORG,
+ )
+ assert exc_info.value.status_code == 400
+ database.credentials.get_credential.assert_awaited_once_with(
+ "cred_foreign",
+ organization_id=ORG.organization_id,
+ )
+
+
+ ("sms_method", "sms_application_sid"),
+ [("GET", ""), ("POST", "AP_customer")],
+)
+async def test_enable_requires_takeover_for_method_or_application_sid(
+ monkeypatch: pytest.MonkeyPatch,
+ sms_method: str,
+ sms_application_sid: str,
+) -> None:
+ config = _config()
+ database = _database(token=_credential_token())
+ database.sms.get_connected_sms_config.return_value = (config, "webhook-secret")
+ database.sms.get_phone_number_by_number.return_value = _registered_phone(config, status="quarantined")
+ monkeypatch.setattr(twilio_integration.app, "DATABASE", database)
+ client, _ = _patch_client(monkeypatch)
+ client.get_incoming_phone_number.return_value = _provider_number(
+ sms_url=twilio_integration._inbound_url(config.sms_config_id, "webhook-secret"),
+ sms_method=sms_method,
+ sms_application_sid=sms_application_sid,
+ )
+ with pytest.raises(HTTPException) as exc_info:
+ await twilio_integration.enable_twilio_phone_number(
+ EnablePhoneNumberRequest(phone_number_sid="PN0123456789abcdef0123456789abcdef"),
+ ORG,
+ )
+ assert exc_info.value.status_code == 409
+ assert exc_info.value.detail == {
+ "current_sms_url": twilio_integration.redact_url_for_display(
+ twilio_integration.redact_url_query(twilio_integration._inbound_url(config.sms_config_id, "webhook-secret"))
+ ),
+ "current_sms_method": sms_method,
+ "current_sms_application_sid": sms_application_sid or None,
+ }
+
+
[email protected]("operation", ["enable", "disable", "disable_decrypt"])
+async def test_provider_apply_then_raise_reconciles_local_state(
+ monkeypatch: pytest.MonkeyPatch,
+ operation: str,
+) -> None:
+ database = _database(token=_credential_token())
+ config = _config()
+ registered = _registered_phone(config, previous_sms_url="skyvern_enc:aesgcm-v1:fixture")
+ if operation == "enable":
+ saved = SimpleNamespace(phone_number_id="pn_created", status="active")
+ database.sms.get_connected_sms_config.return_value = (config, "webhook-secret")
+ database.sms.create_phone_number.return_value = saved
+ database.sms.update_phone_number.side_effect = [saved, saved]
+ else:
+ disabled = registered.model_copy(update={"status": "disabled"})
+ if operation == "disable_decrypt":
+ database.sms.get_phone_number.side_effect = [
+ registered,
+ registered,
+ registered.model_copy(update={"status": "quarantined"}),
+ ]
+ else:
+ database.sms.get_phone_number.return_value = registered
+ database.sms.get_sms_config_with_secret.return_value = (config, "webhook-secret")
+ database.sms.update_phone_number.side_effect = [registered, disabled]
+ monkeypatch.setattr(twilio_integration.app, "DATABASE", database)
+ client, _ = _patch_client(monkeypatch)
+ decrypt = AsyncMock(return_value="https://customer.example/sms")
+ if operation == "disable_decrypt":
+ decrypt.side_effect = ValueError("decrypt failed")
+ monkeypatch.setattr(twilio_integration, "decrypt_secret_field_value", decrypt)
+ provider = _provider_number(
+ phone_number="+14155550123",
+ sms_url=""
+ if operation == "enable"
+ else twilio_integration._inbound_url(config.sms_config_id, "webhook-secret"),
+ )
+ client.get_incoming_phone_number.return_value = provider
+ client.update_inbound_sms_config.side_effect = _apply_provider_routing_then_raise(
+ provider, TwilioApiError(500, "provider failed")
+ )
+ with pytest.raises(ValueError if operation == "disable_decrypt" else HTTPException) as exc_info:
+ if operation == "enable":
+ await twilio_integration.enable_twilio_phone_number(
+ EnablePhoneNumberRequest(phone_number_sid="PN0123456789abcdef0123456789abcdef"),
+ ORG,
+ )
+ else:
+ await twilio_integration.disable_twilio_phone_number(
+ DisablePhoneNumberRequest(phone_number_id="pn_existing"),
+ ORG,
+ )
+ if operation != "disable_decrypt":
+ assert exc_info.value.status_code == 502
+ assert database.sms.update_phone_number.await_args_list[-1].kwargs["status"] == (
+ "active" if operation in {"enable", "disable_decrypt"} else "disabled"
+ )
+ if operation == "disable":
+ assert provider["sms_url"] == "https://customer.example/sms"
+
+
+async def test_enable_rejects_skyvern_managed_row_before_mutation(monkeypatch: pytest.MonkeyPatch) -> None:
+ database = _database(token=_credential_token())
+ database.sms.get_phone_number_by_number.return_value = SimpleNamespace(
+ phone_number_id="pn_managed",
+ provider="skyvern",
+ )
+ monkeypatch.setattr(twilio_integration.app, "DATABASE", database)
+ client, _ = _patch_client(monkeypatch)
+ client.get_incoming_phone_number.return_value = _provider_number(phone_number="+14155550123")
+ with pytest.raises(HTTPException) as exc_info:
+ await twilio_integration.enable_twilio_phone_number(
+ EnablePhoneNumberRequest(phone_number_sid="PN0123456789abcdef0123456789abcdef"),
+ ORG,
+ )
+ assert exc_info.value.status_code == 409
+ assert exc_info.value.detail == "Skyvern-managed numbers must be released from the Skyvern Numbers integration"
+ database.sms.update_phone_number.assert_not_awaited()
+
+
[email protected]("operation", ["enable", "disable"])
+async def test_provider_apply_then_cancel_reconciles_before_propagating(
+ monkeypatch: pytest.MonkeyPatch,
+ operation: str,
+) -> None:
+ database = _database(token=_credential_token())
+ config = _config()
+ registered = _registered_phone(config)
+ if operation == "enable":
+ staged = registered.model_copy(update={"status": "quarantined"})
+ final = SimpleNamespace(**{**registered.__dict__, "status": "active"})
+ database.sms.get_connected_sms_config.return_value = (config, "webhook-secret")
+ database.sms.get_phone_number_by_number.return_value = registered
+ initial_sms_url = ""
+ else:
+ staged = registered.model_copy(update={"status": "quarantined"})
+ final = registered.model_copy(update={"status": "disabled"})
+ database.sms.get_phone_number.return_value = registered
+ database.sms.get_sms_config_with_secret.return_value = (config, "webhook-secret")
+ initial_sms_url = twilio_integration._inbound_url(config.sms_config_id, "webhook-secret")
+ database.sms.update_phone_number.side_effect = [staged, final]
+ monkeypatch.setattr(twilio_integration.app, "DATABASE", database)
+ client, _ = _patch_client(monkeypatch)
+ provider = _provider_number(phone_number="+14155550123", sms_url=initial_sms_url)
+ client.get_incoming_phone_number.return_value = provider
+ provider_started = asyncio.Event()
+ release_provider = asyncio.Event()
+
+ async def apply_provider_routing(*args: object, **kwargs: object) -> None:
+ provider_started.set()
+ await release_provider.wait()
+ provider["sms_url"], provider["sms_method"], provider["sms_application_sid"] = args[1:4]
+
+ client.update_inbound_sms_config.side_effect = apply_provider_routing
+ request = (
+ EnablePhoneNumberRequest(phone_number_sid="PN0123456789abcdef0123456789abcdef", confirm_takeover=True)
+ if operation == "enable"
+ else DisablePhoneNumberRequest(phone_number_id="pn_existing")
+ )
+ task = asyncio.create_task(
+ twilio_integration.enable_twilio_phone_number(request, ORG)
+ if operation == "enable"
+ else twilio_integration.disable_twilio_phone_number(request, ORG)
+ )
+ await provider_started.wait()
+ task.cancel()
+ await asyncio.sleep(0)
+ assert not task.done()
+ release_provider.set()
+ with pytest.raises(asyncio.CancelledError):
+ await task
+ assert provider["sms_url"] == (
+ twilio_integration._inbound_url(config.sms_config_id, "webhook-secret")
+ if operation == "enable"
+ else "https://customer.example/sms"
+ )
+
+
+_ORGANIZATION_ID = "org_sms"
+_SMS_CONFIG_ID = "smsc_test"
+_WEBHOOK_SECRET = "webhook-secret"
+_PHONE_NUMBER = "+15555550123"
+_MESSAGE_SID = "SM123"
+_TWILIO_AUTH_TOKEN = "twilio-auth-token"
+_TWIML = '<?xml version="1.0" encoding="UTF-8"?><Response></Response>'
+
+
+def _inbound_config(*, daily_ingest_cap: int = 100, mode: str = "connected") -> SimpleNamespace:
+ return SimpleNamespace(
+ sms_config_id=_SMS_CONFIG_ID,
+ organization_id=_ORGANIZATION_ID,
+ mode=mode,
+ daily_ingest_cap=daily_ingest_cap,
+ )
+
+
+def _inbound_phone_number(*, status: str = "active", sms_config_id: str = _SMS_CONFIG_ID) -> SimpleNamespace:
+ return SimpleNamespace(organization_id=_ORGANIZATION_ID, sms_config_id=sms_config_id, status=status)
+
+
+def _inbound_database(
+ *,
+ mode: str = "manual",
+ phone_number: SimpleNamespace | None = None,
+ twilio_token: SimpleNamespace | None = None,
+ daily_count: int = 0,
+ create_otp_code_if_new: AsyncMock | None = None,
+ create_raw_otp_code_if_new: AsyncMock | None = None,
+) -> SimpleNamespace:
+ database = _database(token=twilio_token)
+ database.sms.get_sms_config_with_secret_for_inbound.return_value = (_inbound_config(mode=mode), _WEBHOOK_SECRET)
+ database.sms.get_phone_number_by_number.return_value = phone_number or _inbound_phone_number()
+ database.otp = SimpleNamespace(
+ count_otp_codes_since=AsyncMock(return_value=daily_count),
+ create_otp_code_if_new=create_otp_code_if_new
+ or AsyncMock(return_value=SimpleNamespace(totp_code_id="otp_short")),
+ create_raw_otp_code_if_new=create_raw_otp_code_if_new
+ or AsyncMock(return_value=SimpleNamespace(totp_code_id="otp_raw")),
+ )
+ return database
+
+
+def _inbound_form(*, body: str = "Your verification code is 123456") -> dict[str, str]:
+ return {"From": "+15555550999", "To": _PHONE_NUMBER, "Body": body, "MessageSid": _MESSAGE_SID}
+
+
+def _post_inbound(
+ client: TestClient,
+ monkeypatch: pytest.MonkeyPatch,
+ database: SimpleNamespace,
+ *,
+ body: str = "Your verification code is 123456",
+ config_id: str = _SMS_CONFIG_ID,
+ token: str = _WEBHOOK_SECRET,
+ form: dict[str, str] | None = None,
+ signature: str | None = None,
+) -> object:
+ monkeypatch.setattr(sms_inbound.app, "DATABASE", database)
+ headers = {"X-Twilio-Signature": signature} if signature is not None else {}
+ return client.post(
+ f"/v1/sms/inbound/{config_id}?token={token}",
+ data=_inbound_form(body=body) if form is None else form,
+ headers=headers,
+ )
+
+
+def _assert_empty_twiml(response: object) -> None:
+ assert response.status_code == 200
+ assert response.text == _TWIML
+ assert response.headers["content-type"].startswith("text/xml")
+
+
+def client() -> Iterator[TestClient]:
+ fastapi_app = FastAPI()
+ fastapi_app.include_router(sms_inbound.sms_inbound_router, prefix="/v1/sms")
+ with TestClient(fastapi_app) as test_client:
+ yield test_client
+
+
[email protected]("base_url", ["", " "])
+def test_connected_inbound_requires_configured_base_url(
+ client: TestClient, monkeypatch: pytest.MonkeyPatch, base_url: str
+) -> None:
+ database = _inbound_database(
+ mode="connected", twilio_token=SimpleNamespace(credential=SimpleNamespace(auth_token=_TWILIO_AUTH_TOKEN))
+ )
+ monkeypatch.setattr(sms_inbound.settings, "SKYVERN_BASE_URL", base_url)
+ log = Mock()
+ verify = Mock(return_value=True)
+ monkeypatch.setattr(sms_inbound, "LOG", log)
+ monkeypatch.setattr(sms_inbound, "validate_twilio_signature", verify)
+ response = _post_inbound(client, monkeypatch, database)
+ assert response.status_code == 503
+ log.error.assert_called_once()
+ verify.assert_not_called()
+ database.sms.get_phone_number_by_number.assert_not_awaited()
+ print(f"F2-2: unset base URL -> HTTP {response.status_code}; signature verifier not called; one error log")
+
+
+def twilio_client() -> Iterator[TestClient]:
+ api = FastAPI()
+ api.include_router(twilio_integration.twilio_integration_router, prefix="/twilio")
+ api.dependency_overrides[twilio_integration.org_auth_service.get_current_org_for_credential_routes] = lambda: ORG
+ with TestClient(api) as test_client:
+ yield test_client
+
+
[email protected]("token_removed_under_lock", [False, True])
+def test_enable_requires_auth_token_for_signed_delivery(
+ twilio_client: TestClient, monkeypatch: pytest.MonkeyPatch, token_removed_under_lock: bool
+) -> None:
+ credential = TwilioCredential(
+ account_sid=_credential().account_sid,
+ api_key_sid=API_KEY_SID,
+ api_key_secret="api-key-secret",
+ )
+ database = _database(token=SimpleNamespace(credential=credential))
+ if token_removed_under_lock:
+ database.organizations.get_valid_org_auth_token.side_effect = [
+ _credential_token(),
+ SimpleNamespace(credential=credential),
+ ]
+ monkeypatch.setattr(twilio_integration.app, "DATABASE", database)
+ provider, factory = _patch_client(monkeypatch)
+ provider.get_incoming_phone_number.return_value = _provider_number()
+ response = twilio_client.post("/twilio/phone-numbers/enable", json={"phone_number_sid": _provider_number()["sid"]})
+ assert response.status_code == 400
+ assert "auth token is required for signed inbound delivery" in response.json()["detail"]
+ database.sms.create_sms_config.assert_not_awaited()
+ database.sms.create_phone_number.assert_not_awaited()
+ provider.update_inbound_sms_config.assert_not_awaited()
+ if not token_removed_under_lock:
+ factory.assert_not_called()
+
+
[email protected]("display_url", [None, "https://[invalid?token=prior-secret"])
+def test_phone_number_http_responses_exclude_url_secrets(
+ twilio_client: TestClient, monkeypatch: pytest.MonkeyPatch, display_url: str | None
+) -> None:
+ config = _config()
+ prior_url = "https://customer.example/sms?token=prior-secret"
+ registered = _registered_phone(config, previous_sms_url=prior_url)
+ database = _database(token=_credential_token())
+ database.sms.get_connected_sms_config.return_value = (config, _WEBHOOK_SECRET)
+ database.sms.get_sms_config_with_secret.return_value = (config, _WEBHOOK_SECRET)
+ database.sms.get_phone_number_by_number.return_value = registered
+ database.sms.get_phone_number.return_value = registered
+ database.sms.update_phone_number.return_value = registered
+ database.sms.list_phone_numbers.return_value = [registered]
+ monkeypatch.setattr(twilio_integration.app, "DATABASE", database)
+ monkeypatch.setattr(twilio_integration.settings, "SKYVERN_BASE_URL", "https://api.example.test")
+ provider, _ = _patch_client(monkeypatch)
+ provider_number = _provider_number(sms_url=prior_url)
+ provider.get_incoming_phone_number.return_value = provider_number
+ provider.list_incoming_phone_numbers.return_value = [provider_number]
+
+ def assert_safe(response: object, operation: str) -> None:
+ assert response.status_code == 200
+ assert "token=" not in response.text
+ assert "prior-secret" not in response.text
+ assert _WEBHOOK_SECRET not in response.text
+ assert "previous_sms_url" not in response.text
+ print(f"F2-1: {operation} HTTP 200; no token= or URL secrets in response")
+
+ assert_safe(
+ twilio_client.post(
+ "/twilio/phone-numbers/enable",
+ json={"phone_number_sid": provider_number["sid"], "confirm_takeover": True},
+ ),
+ "enable",
+ )
+ provider_number["sms_url"] = build_inbound_sms_url(
+ "https://api.example.test", config.sms_config_id, _WEBHOOK_SECRET
+ )
+ if display_url is not None:
+ provider.list_incoming_phone_numbers.return_value = [_provider_number(sms_url=display_url)]
+ listed = twilio_client.get("/twilio/phone-numbers")
+ assert_safe(listed, "list")
+ assert "?" not in listed.json()[0]["current_sms_url"]
+ assert_safe(twilio_client.get("/twilio/phone-number-registry"), "registry")
+ assert_safe(
+ twilio_client.post("/twilio/phone-numbers/disable", json={"phone_number_id": registered.phone_number_id}),
+ "disable",
+ )
+ database.sms.get_sms_config_with_secret.assert_awaited_once_with(
+ organization_id=ORG.organization_id, sms_config_id=config.sms_config_id
+ )
+ provider.update_inbound_sms_config.assert_awaited_with(
+ registered.provider_number_sid, prior_url, "GET", "AP_customer"
+ )
+
+
[email protected]("phone_status", ["active", "releasing"])
+async def test_valid_ingest_promotes_stored_raw_row_and_returns_xml(
+ agent_db: AgentDB,
+ client: TestClient,
+ monkeypatch: pytest.MonkeyPatch,
+ phone_status: str,
+) -> None:
+ base_url = "https://api.example.test"
+ body = "Your verification code is 123456"
+ form = _inbound_form(body=body)
+ database = _inbound_database(
+ mode="connected",
+ phone_number=_inbound_phone_number(status=phone_status),
+ twilio_token=SimpleNamespace(credential=SimpleNamespace(auth_token=_TWILIO_AUTH_TOKEN)),
+ )
+ database.otp = agent_db.otp
+ raw_ids: list[str] = []
+
+ async def extract(**kwargs: Any) -> dict[str, object]:
+ async with agent_db.Session() as session:
+ row = (await session.scalars(select(TOTPCodeModel))).one()
+ assert (row.parse_status, row.code, row.content) == ("raw", None, body)
+ assert (row.organization_id, row.totp_identifier, row.external_message_id) == (
+ _ORGANIZATION_ID,
+ _PHONE_NUMBER,
+ _MESSAGE_SID,
+ )
+ raw_ids.append(row.totp_code_id)
+ return dict(reasoning="code found", otp_type="totp", otp_value_found=True, otp_value="123456")
+
+ monkeypatch.setattr(otp_service, "get_org_aware_secondary_llm_api_handler", lambda **_: extract)
+ monkeypatch.setattr(otp_service.app, "AGENT_FUNCTION", AgentFunction())
+ monkeypatch.setattr(sms_inbound.settings, "SKYVERN_BASE_URL", base_url)
+ signature = compute_twilio_signature(
+ _TWILIO_AUTH_TOKEN,
+ build_inbound_sms_url(base_url, _SMS_CONFIG_ID, _WEBHOOK_SECRET),
+ form,
+ )
+ response = _post_inbound(client, monkeypatch, database, form=form, signature=signature)
+ _assert_empty_twiml(response)
+ async with agent_db.Session() as session:
+ row = (await session.scalars(select(TOTPCodeModel))).one()
+ assert raw_ids == [row.totp_code_id]
+ assert (row.parse_status, row.code, row.otp_type) == ("parsed", "123456", "totp")
+ invalid_response = _post_inbound(client, monkeypatch, database, form=form, signature="invalid")
+ assert invalid_response.status_code == 400
+
+
+def test_managed_config_fails_closed_until_persisted_signing_contract(
+ client: TestClient,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ database = _inbound_database(
+ mode="managed",
+ )
+ response = _post_inbound(client, monkeypatch, database, body="654321")
+ assert response.status_code == 403
+ database.organizations.get_valid_org_auth_token.assert_not_awaited()
+
+
+ "twilio_token",
+ [
+ None,
+ SimpleNamespace(
+ credential=TwilioCredential(
+ account_sid=ACCOUNT_SID,
+ api_key_sid=API_KEY_SID,
+ api_key_secret="api-key-secret",
+ )
+ ),
+ ],
+ ids=["missing-auth-token", "credential-without-auth-token"],
+)
+def test_connected_config_fails_closed_without_auth_token(
+ client: TestClient,
+ monkeypatch: pytest.MonkeyPatch,
+ twilio_token: SimpleNamespace | None,
+) -> None:
+ database = _inbound_database(
+ mode="connected",
+ twilio_token=twilio_token,
+ )
+ response = _post_inbound(client, monkeypatch, database)
+
+ assert response.status_code == 403
+ database.organizations.get_valid_org_auth_token.assert_awaited_once()
+ database.sms.get_phone_number_by_number.assert_not_awaited()
+
+
+ ("phone_status", "phone_config_id"),
+ [(None, _SMS_CONFIG_ID), ("released", _SMS_CONFIG_ID), ("active", "smsc_other")],
+)
+def test_receiver_drops_unregistered_or_foreign_phone_rows(
+ client: TestClient,
+ monkeypatch: pytest.MonkeyPatch,
+ phone_status: str | None,
+ phone_config_id: str,
+) -> None:
+ database = _inbound_database(
+ phone_number=_inbound_phone_number(status=phone_status, sms_config_id=phone_config_id)
+ if phone_status is not None
+ else _inbound_phone_number()
+ )
+ if phone_status is None:
+ database.sms.get_phone_number_by_number.return_value = None
+ response = _post_inbound(client, monkeypatch, database)
+ _assert_empty_twiml(response)
+
+
[email protected]("raw", [False, True])
+def test_duplicate_message_sid_does_not_create_or_schedule_again(
+ client: TestClient,
+ monkeypatch: pytest.MonkeyPatch,
+ raw: bool,
+) -> None:
+ duplicate = AsyncMock(return_value=None)
+ parse_and_promote = AsyncMock()
+ database = _inbound_database(
+ create_otp_code_if_new=None if raw else duplicate,
+ create_raw_otp_code_if_new=duplicate if raw else None,
+ )
+ if raw:
+ monkeypatch.setattr(sms_inbound, "_parse_and_promote", parse_and_promote)
+ response = _post_inbound(client, monkeypatch, database, body="Your code is 123456" if raw else "123456")
+ _assert_empty_twiml(response)
+ assert duplicate.await_args.kwargs["external_message_id"] == _MESSAGE_SID
+ if raw:
+ parse_and_promote.assert_not_awaited()
+ database.otp.create_otp_code_if_new.assert_not_awaited()
+ else:
+ database.otp.create_raw_otp_code_if_new.assert_not_awaited()
+
+
+TOTP_IDENTIFIER = "(415) 555-2671"
+NORMALIZED_TOTP_IDENTIFIER = "+14155552671"
+
+
+def _make_context() -> WorkflowRunContext:
+ return WorkflowRunContext(
+ workflow_title="workflow",
+ workflow_id="workflow-id",
+ workflow_permanent_id="workflow-permanent-id",
+ workflow_run_id="workflow-run-id",
+ aws_client=SimpleNamespace(),
+ )
+
+
+def _make_vault_parameter(
+ kind: str, totp_identifier: str | None
+) -> OnePasswordCredentialParameter | BitwardenLoginCredentialParameter:
+ now = datetime.now(UTC)
+ if kind == "onepassword":
+ return OnePasswordCredentialParameter(
+ onepassword_credential_parameter_id="opcp-test",
+ workflow_id="workflow-id",
+ key="onepassword_login",
+ vault_id="vault-id",
+ item_id="item-id",
+ totp_identifier=totp_identifier,
+ created_at=now,
+ modified_at=now,
+ )
+ return BitwardenLoginCredentialParameter(
+ bitwarden_login_credential_parameter_id="blcp-test",
+ workflow_id="workflow-id",
+ key="bitwarden_login",
+ bitwarden_client_id_aws_secret_key="client-id-secret",
+ bitwarden_client_secret_aws_secret_key="client-secret-secret",
+ bitwarden_master_password_aws_secret_key="master-password-secret",
+ bitwarden_collection_id="collection-id",
+ bitwarden_item_id="item-id",
+ totp_identifier=totp_identifier,
+ created_at=now,
+ modified_at=now,
+ )
+
+
[email protected]("parameter_type", ["onepassword", "bitwarden"])
+def test_vault_yaml_to_model_round_trips_totp_identifier(parameter_type: str) -> None:
+ if parameter_type == "onepassword":
+ yaml_parameter = OnePasswordCredentialParameterYAML(
+ key="onepassword_login",
+ vault_id="vault-id",
+ item_id="item-id",
+ totp_identifier=TOTP_IDENTIFIER,
+ totp_field_name="otp_code",
+ )
+ parameter_cls = OnePasswordCredentialParameter
+ model_cls = OnePasswordCredentialParameterModel
+ else:
+ yaml_parameter = BitwardenLoginCredentialParameterYAML(
+ key="bitwarden_login",
+ bitwarden_client_id_aws_secret_key="client-id-secret",
+ bitwarden_client_secret_aws_secret_key="client-secret",
+ bitwarden_master_password_aws_secret_key="master-password-secret",
+ bitwarden_collection_id="collection-id",
+ bitwarden_item_id="item-id",
+ totp_identifier=TOTP_IDENTIFIER,
+ )
+ parameter_cls = BitwardenLoginCredentialParameter
+ model_cls = BitwardenLoginCredentialParameterModel
+ parameter = convert_workflow_definition(
+ WorkflowDefinitionYAML(parameters=[yaml_parameter], blocks=[]), workflow_id="workflow-id"
+ ).parameters[0]
+ assert isinstance(parameter, parameter_cls)
+ if parameter_type == "onepassword":
+ assert parameter.totp_field_name == "otp_code"
+ model = WorkflowParametersRepository._convert_parameter_to_model(parameter)
+ assert isinstance(model, model_cls)
+ assert model.totp_identifier == TOTP_IDENTIFIER
+ converted = (
+ OnePasswordCredentialParameter.model_validate(model, from_attributes=True)
+ if parameter_type == "onepassword"
+ else convert_to_bitwarden_login_credential_parameter(model)
+ )
+ assert converted.totp_identifier == TOTP_IDENTIFIER
+
+
+async def test_vault_parameter_repository_commit_normalizes_utc_timestamps() -> None:
+ engine = create_async_engine("sqlite+aiosqlite:///:memory:")
+ models = (BitwardenLoginCredentialParameterModel, OnePasswordCredentialParameterModel)
+ async with engine.begin() as connection:
+ await connection.run_sync(Base.metadata.create_all, tables=[model.__table__ for model in models])
+ writes: list[dict[str, datetime]] = []
+
+ def capture_timestamps(_conn: Any, _cursor: Any, _sql: Any, _parameters: Any, context: Any, _many: Any) -> None:
+ if context.isinsert or context.isupdate:
+ for parameters in context.compiled_parameters:
+ writes.append({key: value for key, value in parameters.items() if isinstance(value, datetime)})
+
+ event.listen(engine.sync_engine, "before_cursor_execute", capture_timestamps)
+ repository = WorkflowParametersRepository(async_sessionmaker(engine, expire_on_commit=False))
+ await repository.save_workflow_definition_parameters(
+ [_make_vault_parameter(kind, TOTP_IDENTIFIER) for kind in ("bitwarden", "onepassword")]
+ )
+ assert len(writes) == 2
+ for write in writes:
+ assert {"created_at", "modified_at"} <= write.keys()
+ assert all(value.tzinfo is None for value in write.values())
+ async with repository.Session() as session:
+ rows = [await session.scalar(select(model)) for model in models]
+ assert all(
+ getattr(row, field).tzinfo is None for row in rows if row is not None for field in ("created_at", "modified_at")
+ )
+ await engine.dispose()
+
+
+ ("credential_type", "credential_fields"),
+ [
+ (
+ LoginCredentialType.onepassword,
+ {
+ "onepassword_vault_id": "vault-id",
+ "onepassword_item_id": "item-id",
+ "onepassword_totp_field_name": "otp_code",
+ },
+ ),
+ (
+ LoginCredentialType.bitwarden,
+ {"bitwarden_collection_id": "collection-id", "bitwarden_item_id": "item-id"},
+ ),
+ ],
+)
+async def test_login_route_parameters_reach_converter(
+ monkeypatch: pytest.MonkeyPatch,
+ credential_type: LoginCredentialType,
+ credential_fields: dict[str, str],
+) -> None:
+ now = datetime.now(UTC)
+ organization = Organization(
+ organization_id="organization-id",
+ organization_name="Test organization",
+ created_at=now,
+ modified_at=now,
+ )
+ new_workflow = SimpleNamespace(
+ title="Login", description=None, status=WorkflowStatus.auto_generated, workflow_permanent_id="wpid_login"
+ )
+ workflow = SimpleNamespace(workflow_id="workflow-id")
+ workflow_service = SimpleNamespace(
+ create_empty_workflow=AsyncMock(return_value=new_workflow),
+ create_workflow_from_request=AsyncMock(return_value=workflow),
+ )
+ monkeypatch.setattr(
+ run_blocks,
+ "app",
+ SimpleNamespace(
+ WORKFLOW_SERVICE=workflow_service, RATE_LIMITER=SimpleNamespace(rate_limit_submit_run=AsyncMock())
+ ),
+ )
+ run_result = object()
+ monkeypatch.setattr(run_blocks, "_run_workflow_and_build_response", AsyncMock(return_value=run_result))
+ login_request = LoginRequest(
+ url="https://example.com/login",
+ credential_type=credential_type,
+ totp_identifier=TOTP_IDENTIFIER,
+ **credential_fields,
+ )
+ caller = SimpleNamespace(organization=organization, caller_type=CallerType.API_KEY)
+ result = await run_blocks.login(
+ request=SimpleNamespace(), background_tasks=SimpleNamespace(), login_request=login_request, caller=caller
+ )
+ assert result is run_result
+ yaml_request = workflow_service.create_workflow_from_request.await_args.kwargs["request"]
+ converted = convert_workflow_definition(yaml_request.workflow_definition, workflow_id="workflow-id")
+ parameter = next(parameter for parameter in converted.parameters if parameter.key == "credential")
+ assert parameter.totp_identifier == TOTP_IDENTIFIER
+ if credential_type is LoginCredentialType.onepassword:
+ assert isinstance(parameter, OnePasswordCredentialParameter)
+ assert parameter.vault_id == "vault-id"
+ assert parameter.item_id == "item-id"
+ assert parameter.totp_field_name == "otp_code"
+ else:
+ assert isinstance(parameter, BitwardenLoginCredentialParameter)
+ assert parameter.bitwarden_collection_id == "collection-id"
+ assert parameter.bitwarden_item_id == "item-id"
+ assert parameter.url_parameter_key == "https://example.com/login"
+
+
+async def _register(
+ context: WorkflowRunContext,
+ kind: str,
+ parameter: OnePasswordCredentialParameter | BitwardenLoginCredentialParameter,
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ if kind == "onepassword":
+ organizations = SimpleNamespace(
+ get_valid_org_auth_token=AsyncMock(return_value=SimpleNamespace(token="service-account-token"))
+ )
+ monkeypatch.setattr(cm, "app", SimpleNamespace(DATABASE=SimpleNamespace(organizations=organizations)))
+ item = SimpleNamespace(fields=[], notes=None)
+ client = SimpleNamespace(items=SimpleNamespace(get=AsyncMock(return_value=item)))
+ monkeypatch.setattr(cm, "OnePasswordClient", SimpleNamespace(authenticate=AsyncMock(return_value=client)))
+ await context.register_onepassword_credential_parameter_value(
+ parameter, SimpleNamespace(organization_id="organization-id")
+ )
+ return
+ secret_credentials = {
+ BitwardenConstants.USERNAME: "username",
+ BitwardenConstants.PASSWORD: "password",
+ BitwardenConstants.TOTP: "",
+ }
+ context._run_bitwarden_operation_with_fallback = AsyncMock(
+ return_value=(secret_credentials, ("client-id", "client-secret", "master-password", None))
+ )
+ await context.register_bitwarden_login_credential_parameter_value(
+ parameter,
+ SimpleNamespace(
+ organization_id="organization-id",
+ bw_organization_id="bitwarden-organization-id",
+ bw_collection_ids=["collection-id"],
+ ),
+ )
+
+
+ ("totp_identifier", "expected"),
+ [
+ (
+ TOTP_IDENTIFIER,
+ {"onepassword_login": NORMALIZED_TOTP_IDENTIFIER, "bitwarden_login": NORMALIZED_TOTP_IDENTIFIER},
+ ),
+ (
+ {"onepassword_login": "[email protected]", "bitwarden_login": "[email protected]"},
+ ),
+ (
+ "login-1234567890",
+ {"onepassword_login": "login-1234567890", "bitwarden_login": "login-1234567890"},
+ ),
+ (None, {}),
+ ],
+)
+async def test_vault_parameters_register_totp_identifiers(
+ monkeypatch: pytest.MonkeyPatch,
+ totp_identifier: str | None,
+ expected: dict[str, str],
+) -> None:
+ context = _make_context()
+ await _register(context, "onepassword", _make_vault_parameter("onepassword", totp_identifier), monkeypatch)
+ await _register(context, "bitwarden", _make_vault_parameter("bitwarden", totp_identifier), monkeypatch)
+ assert context.credential_totp_identifiers == expected
+
+
+def test_password_response_exposes_seed_presence_without_secret() -> None:
+ credential = SimpleNamespace(
+ credential_id="cred_seed",
+ credential_type=CredentialType.PASSWORD,
+ username="[email protected]",
+ totp_type=TotpType.AUTHENTICATOR,
+ totp_identifier="+14155550123",
+ has_totp_seed=True,
+ name="Login",
+ vault_type=CredentialVaultType.SKYVERN,
+ browser_profile_id=None,
+ auto_profile_disabled=False,
+ pin_saved_session_ip=False,
+ tested_url=None,
+ user_context=None,
+ save_browser_session_intent=False,
+ run_sequentially=False,
+ folder_id=None,
+ proxy_location=None,
+ proxy_session_id=None,
+ )
+ response = credentials._convert_to_response(credential).model_dump()
+
+ assert response["credential"]["has_totp"] is True
+ assert response["credential"]["totp_identifier"] == "+14155550123"
+ assert "has_totp_seed" not in response["credential"]
+ assert "totp" not in response["credential"]
+
+
+async def test_seed_presence_survives_update_when_totp_is_omitted(monkeypatch: pytest.MonkeyPatch) -> None:
+ update_credential = AsyncMock(return_value=SimpleNamespace())
+ monkeypatch.setattr(
+ credentials.app,
+ "DATABASE",
+ SimpleNamespace(credentials=SimpleNamespace(update_credential_vault_data=update_credential)),
+ )
+ existing = SimpleNamespace(credential_id="cred-seed", organization_id="o_test")
+ request = CreateCredentialRequest(
+ name="Login",
+ credential_type=CredentialType.PASSWORD,
+ credential=NonEmptyPasswordCredential(username="[email protected]", password="new-password"),
+ )
+
+ await credentials.CredentialVaultService._update_db_credential(existing, request, "item-new")
+
+ assert "has_totp_seed" not in update_credential.await_args.kwargs
+
+
+class TestCredentialVaultLegacySeedPreservation:
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize("has_totp_seed", [None, True])
+ async def test_local_vault_replacement_item_preserves_omitted_totp(self, has_totp_seed: bool | None) -> None:
+ service = SkyvernCredentialVaultService()
+ existing = SimpleNamespace(credential_id="cred-seed", item_id="item-old", organization_id="org-test")
+ existing.has_totp_seed = has_totp_seed
+ existing_item = CredentialItem(
+ item_id="item-old",
+ name="Login",
+ credential_type=CredentialType.PASSWORD,
+ credential=PasswordCredential(username="old-user", password="old-password", totp="JBSWY3DPEHPK3PXP"),
+ )
+ stored = AsyncMock()
+ service.get_credential_item = AsyncMock(return_value=existing_item)
+ service._store_item = stored
+ service._update_db_credential = AsyncMock(return_value=SimpleNamespace(item_id="item-new"))
+ service._generate_item_id = lambda: "item-new"
+ request = CreateCredentialRequest(
+ name="Login",
+ credential_type=CredentialType.PASSWORD,
+ credential=NonEmptyPasswordCredential(username="new-user", password="new-password", metadata={"k": "v"}),
+ )
+
+ await service.update_credential(existing, request)
+
+ replacement = stored.await_args.args[0]
+ assert replacement.item_id == "item-new"
+ assert replacement.credential.totp == "JBSWY3DPEHPK3PXP"
+ assert service._update_db_credential.await_args.kwargs["data"].credential.totp == replacement.credential.totp
+ assert "totp" in service._update_db_credential.await_args.kwargs["data"].credential.model_fields_set
+ service.get_credential_item.assert_awaited_once_with(existing)
+
+ @pytest.mark.asyncio
+ async def test_local_vault_known_absent_totp_skips_read(self) -> None:
+ service = SkyvernCredentialVaultService()
+ existing = SimpleNamespace(
+ credential_id="cred-seed", item_id="item-old", organization_id="org-test", has_totp_seed=False
+ )
+ service.get_credential_item = AsyncMock()
+ service._store_item = AsyncMock()
+ service._update_db_credential = AsyncMock(return_value=SimpleNamespace(item_id="item-new"))
+ service._generate_item_id = lambda: "item-new"
+ request = CreateCredentialRequest(
+ name="Login",
+ credential_type=CredentialType.PASSWORD,
+ credential=NonEmptyPasswordCredential(username="new-user", password="new-password", metadata={"k": "v"}),
+ )
+
+ await service.update_credential(existing, request)
+
+ service.get_credential_item.assert_not_awaited()
+ assert service._update_db_credential.await_args.kwargs["data"] is request
+ assert "totp" not in service._update_db_credential.await_args.kwargs["data"].credential.model_fields_set
diff --git a/tests/unit/test_twilio_service.py b/tests/unit/test_twilio_service.py
new file mode 100644
index 000000000..1b5b2c60a
--- /dev/null
+++ b/tests/unit/test_twilio_service.py
@@ -0,0 +1,417 @@
+import base64
+import json
+from collections.abc import Awaitable, Callable
+from typing import Any
+from unittest.mock import AsyncMock
+from urllib.parse import parse_qs
+
+import httpx
+import pytest
+from pydantic import BaseModel, ValidationError
+
+from skyvern.forge.sdk.db import utils as db_utils
+from skyvern.forge.sdk.db.enums import OrganizationAuthTokenType
+from skyvern.forge.sdk.db.models import OrganizationAuthTokenModel
+from skyvern.forge.sdk.schemas.organizations import (
+ CreateTwilioCredentialRequest,
+ TwilioCredential,
+ TwilioCredentialResponse,
+ TwilioCredentialSafe,
+ TwilioOrganizationAuthToken,
+)
+from skyvern.forge.sdk.schemas.sms import EnablePhoneNumberRequest, RegisterManualPhoneNumberRequest
+from skyvern.forge.sdk.services import twilio_service
+
+HttpHandler = Callable[[httpx.Request], httpx.Response | Awaitable[httpx.Response]]
+
+
+def _install_transport(monkeypatch: pytest.MonkeyPatch, handler: HttpHandler) -> None:
+ transport = httpx.MockTransport(handler)
+ real_client = httpx.AsyncClient
+
+ def fake_async_client(*args: Any, **kwargs: Any) -> httpx.AsyncClient:
+ assert kwargs["timeout"] == 15.0
+ kwargs["transport"] = transport
+ return real_client(*args, **kwargs)
+
+ monkeypatch.setattr(twilio_service.httpx, "AsyncClient", fake_async_client)
+
+
+VALID_ACCOUNT_SID = "AC" + "0123456789abcdef" * 2
+VALID_API_KEY_SID = "SK" + "0123456789abcdef" * 2
+VALID_PHONE_NUMBER_SID = "PN" + "a" * 32
+
+
+def _assert_no_secret_inputs(error: ValueError) -> None:
+ surfaces = [str(error), repr(error)]
+ if isinstance(error, ValidationError):
+ surfaces.extend([str(error.errors()), error.json()])
+ for surface in surfaces:
+ assert "AUTH_SENTINEL" not in surface
+ assert "KEY_SENTINEL" not in surface
+
+
[email protected]("model", [TwilioCredential, CreateTwilioCredentialRequest])
[email protected]("encoding", [str, bytes, bytearray])
+def test_malformed_twilio_json_hides_secrets(model: type[BaseModel], encoding: type) -> None:
+ payload: dict[str, Any] = {
+ "account_sid": VALID_ACCOUNT_SID,
+ "auth_token": "AUTH_SENTINEL",
+ "api_key_secret": "KEY_SENTINEL",
+ }
+ if model is CreateTwilioCredentialRequest:
+ payload = {"credential": payload}
+ malformed = json.dumps(payload)[:-1]
+ data = malformed if encoding is str else encoding(malformed, "utf-8")
+ with pytest.raises(ValueError) as caught:
+ model.model_validate_json(data)
+ _assert_no_secret_inputs(caught.value)
+
+
[email protected]("encrypted", [False, True])
+async def test_malformed_twilio_json_reader_hides_secrets(monkeypatch: pytest.MonkeyPatch, encrypted: bool) -> None:
+ malformed = '{"auth_token":"AUTH_SENTINEL","api_key_secret":"KEY_SENTINEL"'
+ monkeypatch.setattr(db_utils, "encryptor", AsyncMock(decrypt=AsyncMock(return_value=malformed)))
+ row = OrganizationAuthTokenModel(
+ token="" if encrypted else malformed,
+ encrypted_token="ciphertext" if encrypted else None,
+ encrypted_method="aes" if encrypted else None,
+ )
+ with pytest.raises(ValueError) as caught:
+ await db_utils.convert_to_organization_auth_token(row, OrganizationAuthTokenType.twilio_credential)
+ _assert_no_secret_inputs(caught.value)
+
+
+def test_twilio_credential_hides_secrets_in_representations_and_validation_errors() -> None:
+ secrets = {"auth_token": "AUTH_SENTINEL", "api_key_secret": "KEY_SENTINEL"}
+ credential = TwilioCredential(account_sid=VALID_ACCOUNT_SID, api_key_sid=VALID_API_KEY_SID, **secrets)
+ with pytest.raises(ValidationError) as error:
+ TwilioCredential(account_sid=VALID_ACCOUNT_SID, **secrets)
+
+ for rendered in (repr(credential), str(credential), str(error.value)):
+ assert all(secret not in rendered for secret in secrets.values())
+
+
+def test_twilio_credential_accepts_valid_authentication_modes() -> None:
+ assert TwilioCredential(account_sid=VALID_ACCOUNT_SID, auth_token="auth-token").auth_token == "auth-token"
+ credential = TwilioCredential(
+ account_sid=VALID_ACCOUNT_SID,
+ api_key_sid=VALID_API_KEY_SID,
+ api_key_secret="api-secret",
+ )
+ assert credential.api_key_sid == VALID_API_KEY_SID
+
+
+ "model",
+ [
+ TwilioCredential,
+ CreateTwilioCredentialRequest,
+ TwilioOrganizationAuthToken,
+ TwilioCredentialSafe,
+ TwilioCredentialResponse,
+ ],
+)
[email protected]("malformed_field", [False, True], ids=["missing-field", "invalid-type"])
+def test_twilio_models_remove_inputs_from_all_validation_error_surfaces(
+ model: type[BaseModel], malformed_field: bool
+) -> None:
+ payload: dict[str, Any] = {
+ "account_sid": VALID_ACCOUNT_SID,
+ "auth_token": "AUTH_SENTINEL",
+ "api_key_secret": "KEY_SENTINEL",
+ }
+ if malformed_field:
+ payload["auth_token"] = ["AUTH_SENTINEL"]
+ payload["api_key_secret"] = ["KEY_SENTINEL"]
+ payload["account_sid"] = ["AUTH_SENTINEL", "KEY_SENTINEL"]
+ if model not in (TwilioCredential, TwilioCredentialSafe):
+ payload = {"credential": payload}
+ with pytest.raises(ValidationError) as caught:
+ model.model_validate(payload)
+ error = caught.value
+ for rendered in (str(error), repr(error), str(error.errors()), error.json()):
+ assert "AUTH_SENTINEL" not in rendered
+ assert "KEY_SENTINEL" not in rendered
+
+
+ "credential",
+ [
+ {"account_sid": "AC_short", "auth_token": "auth-token"},
+ {"account_sid": VALID_API_KEY_SID, "auth_token": "auth-token"},
+ {
+ "account_sid": VALID_ACCOUNT_SID,
+ "api_key_sid": "SK_short",
+ "api_key_secret": "api-secret",
+ },
+ {"account_sid": VALID_ACCOUNT_SID, "api_key_sid": VALID_API_KEY_SID, "auth_token": "auth-token"},
+ {"account_sid": VALID_ACCOUNT_SID, "api_key_secret": "api-secret", "auth_token": "auth-token"},
+ {"account_sid": VALID_ACCOUNT_SID, "auth_token": " "},
+ ],
+)
+def test_twilio_credential_rejects_malformed_values(credential: dict[str, str]) -> None:
+ with pytest.raises(ValidationError):
+ TwilioCredential(**credential)
+
+
+def test_manual_phone_number_request_normalizes_and_validates_e164() -> None:
+ assert RegisterManualPhoneNumberRequest(phone_number="+1 (415) 555-2671").phone_number == "+14155552671"
+ with pytest.raises(ValidationError):
+ RegisterManualPhoneNumberRequest(phone_number="4155552671")
+ with pytest.raises(ValidationError):
+ RegisterManualPhoneNumberRequest(phone_number="+1--415-555-2671")
+ with pytest.raises(ValidationError):
+ RegisterManualPhoneNumberRequest(phone_number="+1-EXT-415-555-2671")
+
+
+def test_enable_phone_number_request_validates_twilio_sid() -> None:
+ assert EnablePhoneNumberRequest(phone_number_sid=VALID_PHONE_NUMBER_SID).phone_number_sid == VALID_PHONE_NUMBER_SID
+ for invalid_sid in ("PN../../etc/passwd", "PN%2e%2e%2fetc%2fpasswd"):
+ with pytest.raises(ValidationError):
+ EnablePhoneNumberRequest(phone_number_sid=invalid_sid)
+
+
+def test_compute_twilio_signature_matches_published_vector() -> None:
+ # Source: twilio-python tests/unit/test_request_validator.py.
+ url = "https://mycompany.com/myapp.php?foo=1&bar=2"
+ params = {
+ "CallSid": "CA1234567890ABCDE",
+ "Digits": "1234",
+ "From": "+14158675309",
+ "To": "+18005551212",
+ "Caller": "+14158675309",
+ }
+
+ signature = twilio_service.compute_twilio_signature("12345", url, params)
+
+ assert signature == "RSOYDt4T1cUTdK1PDd93/VVr8B8="
+
+
+def test_validate_twilio_signature_is_order_independent_and_rejects_tampering() -> None:
+ url = "https://example.com/v1/sms/inbound/smsc_123?token=secret"
+ params = {
+ "From": "+14158675309",
+ "To": "+18005551212",
+ "Body": "123456",
+ "MessageSid": "SM123",
+ }
+ signature = twilio_service.compute_twilio_signature("auth-token", url, params)
+ reversed_params = dict(reversed(list(params.items())))
+
+ assert twilio_service.validate_twilio_signature("auth-token", url, reversed_params, signature)
+ assert not twilio_service.validate_twilio_signature(
+ "auth-token",
+ url,
+ {**params, "Body": "654321"},
+ signature,
+ )
+ assert not twilio_service.validate_twilio_signature("auth-token", url, params, "☃")
+
+
+ ("api_key_sid", "api_key_secret", "auth_token", "expected_username", "expected_password"),
+ [
+ ("SK_api_key", "api-secret", "auth-token", "SK_api_key", "api-secret"),
+ (None, None, "auth-token", "AC_account", "auth-token"),
+ ("SK_api_key", None, "auth-token", "AC_account", "auth-token"),
+ (None, "orphan-secret", "auth-token", "AC_account", "auth-token"),
+ ],
+)
+async def test_client_uses_expected_basic_auth(
+ monkeypatch: pytest.MonkeyPatch,
+ api_key_sid: str | None,
+ api_key_secret: str | None,
+ auth_token: str,
+ expected_username: str,
+ expected_password: str,
+) -> None:
+ authorization_headers: list[str] = []
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ authorization_headers.append(request.headers["Authorization"])
+ return httpx.Response(200, json={"sid": "AC_account"})
+
+ _install_transport(monkeypatch, handler)
+ client = twilio_service.TwilioClient(
+ "AC_account",
+ api_key_sid=api_key_sid,
+ api_key_secret=api_key_secret,
+ auth_token=auth_token,
+ )
+
+ account = await client.get_account()
+
+ expected_credentials = base64.b64encode(f"{expected_username}:{expected_password}".encode()).decode()
+ assert authorization_headers == [f"Basic {expected_credentials}"]
+ assert account == {"sid": "AC_account"}
+
+
+async def test_list_incoming_phone_numbers_follows_next_page_uri(monkeypatch: pytest.MonkeyPatch) -> None:
+ requested_urls: list[str] = []
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ requested_urls.append(str(request.url))
+ if request.url.params.get("PageToken") == "next":
+ return httpx.Response(
+ 200,
+ json={
+ "incoming_phone_numbers": [{"sid": "PN_second"}],
+ "next_page_uri": None,
+ },
+ )
+ assert request.url.params["PageSize"] == "100"
+ return httpx.Response(
+ 200,
+ json={
+ "incoming_phone_numbers": [{"sid": "PN_first"}],
+ "next_page_uri": ("/2010-04-01/Accounts/AC_account/IncomingPhoneNumbers.json?PageToken=next"),
+ },
+ )
+
+ _install_transport(monkeypatch, handler)
+ client = twilio_service.TwilioClient("AC_account", auth_token="auth-token")
+
+ phone_numbers = await client.list_incoming_phone_numbers()
+
+ assert phone_numbers == [{"sid": "PN_first"}, {"sid": "PN_second"}]
+ assert len(requested_urls) == 2
+ assert requested_urls[1].startswith(
+ "https://api.twilio.com/2010-04-01/Accounts/AC_account/IncomingPhoneNumbers.json"
+ )
+
+
+async def test_list_incoming_phone_numbers_rejects_cross_host_next_page(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 200,
+ json={
+ "incoming_phone_numbers": [{"sid": "PN_first"}],
+ "next_page_uri": "https://attacker.example/next",
+ },
+ )
+
+ _install_transport(monkeypatch, handler)
+ client = twilio_service.TwilioClient("AC_account", auth_token="auth-token")
+
+ with pytest.raises(twilio_service.TwilioApiError) as exc_info:
+ await client.list_incoming_phone_numbers()
+
+ assert exc_info.value.status_code == 0
+ assert exc_info.value.message == "Unexpected Twilio pagination URL"
+
+
[email protected]("sms_url", ["https://example.com/v1/sms/inbound/smsc_123?token=secret", ""])
+async def test_update_inbound_sms_config_posts_routing_fields(
+ monkeypatch: pytest.MonkeyPatch,
+ sms_url: str,
+) -> None:
+ requests: list[httpx.Request] = []
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ requests.append(request)
+ return httpx.Response(200, json={"sid": VALID_PHONE_NUMBER_SID, "sms_url": sms_url})
+
+ _install_transport(monkeypatch, handler)
+ client = twilio_service.TwilioClient("AC_account", auth_token="auth-token")
+
+ result = await client.update_inbound_sms_config(VALID_PHONE_NUMBER_SID, sms_url, "POST", "")
+
+ assert result == {"sid": VALID_PHONE_NUMBER_SID, "sms_url": sms_url}
+ assert len(requests) == 1
+ assert requests[0].method == "POST"
+ assert requests[0].url.path.endswith(f"/IncomingPhoneNumbers/{VALID_PHONE_NUMBER_SID}.json")
+ assert parse_qs(requests[0].content.decode(), keep_blank_values=True) == {
+ "SmsUrl": [sms_url],
+ "SmsMethod": ["POST"],
+ "SmsApplicationSid": [""],
+ }
+
+
[email protected]("invalid_sid", ["PN../../etc/passwd", "PN%2e%2e%2fetc%2fpasswd"])
+async def test_get_incoming_phone_number_rejects_invalid_sid(monkeypatch: pytest.MonkeyPatch, invalid_sid: str) -> None:
+ def handler(request: httpx.Request) -> httpx.Response:
+ pytest.fail("invalid SID reached Twilio")
+
+ _install_transport(monkeypatch, handler)
+ client = twilio_service.TwilioClient("AC_account", auth_token="auth-token")
+ with pytest.raises(ValueError, match="valid Twilio incoming phone number SID"):
+ await client.get_incoming_phone_number(invalid_sid)
+
+
[email protected]("invalid_sid", ["PN../../etc/passwd", "PN%2e%2e%2fetc%2fpasswd"])
+async def test_update_inbound_sms_config_rejects_invalid_sid(monkeypatch: pytest.MonkeyPatch, invalid_sid: str) -> None:
+ def handler(request: httpx.Request) -> httpx.Response:
+ pytest.fail("invalid SID reached Twilio")
+
+ _install_transport(monkeypatch, handler)
+ client = twilio_service.TwilioClient("AC_account", auth_token="auth-token")
+ with pytest.raises(ValueError, match="valid Twilio incoming phone number SID"):
+ await client.update_inbound_sms_config(invalid_sid, "", "POST", "")
+
+
+async def test_non_success_response_raises_twilio_api_error(monkeypatch: pytest.MonkeyPatch) -> None:
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(404, json={"message": "The requested resource was not found"})
+
+ _install_transport(monkeypatch, handler)
+ client = twilio_service.TwilioClient("AC_account", auth_token="auth-token")
+
+ with pytest.raises(twilio_service.TwilioApiError) as exc_info:
+ await client.get_incoming_phone_number(VALID_PHONE_NUMBER_SID)
+
+ assert exc_info.value.status_code == 404
+ assert exc_info.value.message == "Twilio API returned HTTP 404"
+
+
+async def test_non_success_response_does_not_expose_provider_body(monkeypatch: pytest.MonkeyPatch) -> None:
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(
+ 400,
+ json={
+ "message": "token=secret-token Body=123456 MessageSid=SM_full_sid",
+ "url": "https://example.test/v1/sms/inbound/smsc_123?token=secret-token",
+ },
+ )
+
+ _install_transport(monkeypatch, handler)
+ client = twilio_service.TwilioClient("AC_account", auth_token="auth-token")
+
+ with pytest.raises(twilio_service.TwilioApiError) as exc_info:
+ await client.get_account()
+
+ assert exc_info.value.message == "Twilio API returned HTTP 400"
+ assert "secret-token" not in str(exc_info.value)
+ assert "123456" not in str(exc_info.value)
+ assert "SM_full_sid" not in str(exc_info.value)
+
+
+async def test_transport_error_is_wrapped(monkeypatch: pytest.MonkeyPatch) -> None:
+ def handler(request: httpx.Request) -> httpx.Response:
+ raise httpx.ConnectError("connection failed", request=request)
+
+ _install_transport(monkeypatch, handler)
+ client = twilio_service.TwilioClient("AC_account", auth_token="auth-token")
+
+ with pytest.raises(twilio_service.TwilioApiError) as exc_info:
+ await client.get_account()
+
+ assert exc_info.value.status_code == 0
+
+
+def test_build_inbound_sms_url_strips_trailing_slashes() -> None:
+ assert twilio_service.build_inbound_sms_url("https://api.example.com///", "smsc_123", "secret") == (
+ "https://api.example.com/v1/sms/inbound/smsc_123?token=secret"
+ )
diff --git a/tests/unit/test_url_validators.py b/tests/unit/test_url_validators.py
index 5f6f8a12d..f328c1f3b 100644
--- a/tests/unit/test_url_validators.py
+++ b/tests/unit/test_url_validators.py
@@ -14,6 +14,8 @@ from skyvern.schemas.workflows import WorkflowCreateYAMLRequest
from skyvern.utils.url_validators import (
encode_url,
is_blocked_host,
+ redact_url_for_display,
+ redact_url_query,
validate_fetch_url,
validate_redirect_url,
validate_url,
@@ -21,6 +23,21 @@ from skyvern.utils.url_validators import (
)
+ ("url", "expected"),
+ [
+ ("https://hooks.example/sms?token=secret&key=another", "https://hooks.example/sms"),
+ ("https://hooks.example/sms?token=secret#fragment", "https://hooks.example/sms#fragment"),
+ ("https://[2001:db8::1]:8443/sms?token=secret", "https://[2001:db8::1]:8443/sms"),
+ ("HTTPS://Hooks.Example/sms%2Fpath#fragment", "HTTPS://Hooks.Example/sms%2Fpath#fragment"),
+ ("https://hooks.example/sms#fragment?not-a-query", "https://hooks.example/sms#fragment?not-a-query"),
+ ("", ""),
+ ],
+)
+def test_redact_url_query(url: str, expected: str) -> None:
+ assert redact_url_query(url) == expected
+
+
def test_encode_url_basic():
"""Test basic URL encoding with simple path"""
url = "https://example.com/path with spaces"
@@ -49,6 +66,25 @@ def test_encode_url_with_pre_encoded_chars():
assert encode_url(url) == expected
+ ("url", "expected"),
+ [
+ (
+ "https://api.skyvern.com/v1/sms/inbound/smsc_123?token=secret#fragment",
+ "https://api.skyvern.com/…?…",
+ ),
+ ("https://[2001:db8::1]:8443/hook?token=secret", "https://[2001:db8::1]:8443/…?…"),
+ ("https://customer.example/hook", "https://customer.example/…"),
+ ("https://customer.example/secret/token-value", "https://customer.example/…"),
+ (None, None),
+ ("https://hooks.example:bad/hook?token=secret", "[invalid URL]"),
+ ("/hook?token=secret", "[invalid URL]"),
+ ],
+)
+def test_redact_url_for_display_removes_query_and_fragment(url: str | None, expected: str | None) -> None:
+ assert redact_url_for_display(url) == expected
+
+
@pytest.mark.parametrize(
"host",
[