Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Normalize feature flag resolution logging (#5472)
Shuchang Zhengae0d613a56ff
diff --git a/fern/openapi/skyvern_openapi.json b/fern/openapi/skyvern_openapi.jsonindex ecd0b6de5..5a64fddc3 100644--- a/fern/openapi/skyvern_openapi.json+++ b/fern/openapi/skyvern_openapi.json@@ -3970,599 +3970,6 @@},"x-fern-sdk-method-name": "run_sdk_action"}- },- "/v1/schedules": {- "get": {- "tags": [- "Schedules"- ],- "summary": "List all schedules for the organization",- "operationId": "schedules_list_all",- "parameters": [- {- "name": "page",- "in": "query",- "required": false,- "schema": {- "type": "integer",- "minimum": 1,- "default": 1,- "title": "Page"- }- },- {- "name": "page_size",- "in": "query",- "required": false,- "schema": {- "type": "integer",- "maximum": 100,- "minimum": 1,- "default": 10,- "title": "Page Size"- }- },- {- "name": "status",- "in": "query",- "required": false,- "schema": {- "anyOf": [- {- "enum": [- "active",- "paused"- ],- "type": "string"- },- {- "type": "null"- }- ],- "description": "Filter by status: 'active' or 'paused'",- "title": "Status"- },- "description": "Filter by status: 'active' or 'paused'"- },- {- "name": "search",- "in": "query",- "required": false,- "schema": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "description": "Search by workflow title or schedule name",- "title": "Search"- },- "description": "Search by workflow title or schedule name"- },- {- "name": "x-api-key",- "in": "header",- "required": false,- "schema": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings.",- "title": "X-Api-Key"- },- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings."- }- ],- "responses": {- "200": {- "description": "Successful Response",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/OrganizationScheduleListResponse"- }- }- }- },- "422": {- "description": "Validation Error",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/HTTPValidationError"- }- }- }- }- }- }- },- "/v1/workflows/{workflow_permanent_id}/schedules": {- "post": {- "tags": [- "Schedules"- ],- "summary": "Create a schedule for a workflow",- "operationId": "schedules_create",- "parameters": [- {- "name": "workflow_permanent_id",- "in": "path",- "required": true,- "schema": {- "type": "string",- "title": "Workflow Permanent Id"- }- },- {- "name": "x-api-key",- "in": "header",- "required": false,- "schema": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings.",- "title": "X-Api-Key"- },- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings."- }- ],- "requestBody": {- "required": true,- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/WorkflowScheduleUpsertRequest"- }- }- }- },- "responses": {- "200": {- "description": "Successful Response",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/WorkflowScheduleResponse"- }- }- }- },- "422": {- "description": "Validation Error",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/HTTPValidationError"- }- }- }- }- }- },- "get": {- "tags": [- "Schedules"- ],- "summary": "List schedules for a workflow",- "operationId": "schedules_list",- "parameters": [- {- "name": "workflow_permanent_id",- "in": "path",- "required": true,- "schema": {- "type": "string",- "title": "Workflow Permanent Id"- }- },- {- "name": "x-api-key",- "in": "header",- "required": false,- "schema": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings.",- "title": "X-Api-Key"- },- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings."- }- ],- "responses": {- "200": {- "description": "Successful Response",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/WorkflowScheduleListResponse"- }- }- }- },- "422": {- "description": "Validation Error",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/HTTPValidationError"- }- }- }- }- }- }- },- "/v1/workflows/{workflow_permanent_id}/schedules/{workflow_schedule_id}": {- "get": {- "tags": [- "Schedules"- ],- "summary": "Get a workflow schedule by id",- "operationId": "schedules_get",- "parameters": [- {- "name": "workflow_permanent_id",- "in": "path",- "required": true,- "schema": {- "type": "string",- "title": "Workflow Permanent Id"- }- },- {- "name": "workflow_schedule_id",- "in": "path",- "required": true,- "schema": {- "type": "string",- "title": "Workflow Schedule Id"- }- },- {- "name": "x-api-key",- "in": "header",- "required": false,- "schema": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings.",- "title": "X-Api-Key"- },- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings."- }- ],- "responses": {- "200": {- "description": "Successful Response",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/WorkflowScheduleResponse"- }- }- }- },- "422": {- "description": "Validation Error",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/HTTPValidationError"- }- }- }- }- }- },- "put": {- "tags": [- "Schedules"- ],- "summary": "Update a workflow schedule",- "operationId": "schedules_update",- "parameters": [- {- "name": "workflow_permanent_id",- "in": "path",- "required": true,- "schema": {- "type": "string",- "title": "Workflow Permanent Id"- }- },- {- "name": "workflow_schedule_id",- "in": "path",- "required": true,- "schema": {- "type": "string",- "title": "Workflow Schedule Id"- }- },- {- "name": "x-api-key",- "in": "header",- "required": false,- "schema": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings.",- "title": "X-Api-Key"- },- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings."- }- ],- "requestBody": {- "required": true,- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/WorkflowScheduleUpsertRequest"- }- }- }- },- "responses": {- "200": {- "description": "Successful Response",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/WorkflowScheduleResponse"- }- }- }- },- "422": {- "description": "Validation Error",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/HTTPValidationError"- }- }- }- }- }- },- "delete": {- "tags": [- "Schedules"- ],- "summary": "Delete a workflow schedule",- "operationId": "schedules_delete",- "parameters": [- {- "name": "workflow_permanent_id",- "in": "path",- "required": true,- "schema": {- "type": "string",- "title": "Workflow Permanent Id"- }- },- {- "name": "workflow_schedule_id",- "in": "path",- "required": true,- "schema": {- "type": "string",- "title": "Workflow Schedule Id"- }- },- {- "name": "x-api-key",- "in": "header",- "required": false,- "schema": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings.",- "title": "X-Api-Key"- },- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings."- }- ],- "responses": {- "200": {- "description": "Successful Response",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/DeleteScheduleResponse"- }- }- }- },- "422": {- "description": "Validation Error",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/HTTPValidationError"- }- }- }- }- }- }- },- "/v1/workflows/{workflow_permanent_id}/schedules/{workflow_schedule_id}/enable": {- "post": {- "tags": [- "Schedules"- ],- "summary": "Enable a workflow schedule",- "operationId": "schedules_enable",- "parameters": [- {- "name": "workflow_permanent_id",- "in": "path",- "required": true,- "schema": {- "type": "string",- "title": "Workflow Permanent Id"- }- },- {- "name": "workflow_schedule_id",- "in": "path",- "required": true,- "schema": {- "type": "string",- "title": "Workflow Schedule Id"- }- },- {- "name": "x-api-key",- "in": "header",- "required": false,- "schema": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings.",- "title": "X-Api-Key"- },- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings."- }- ],- "responses": {- "200": {- "description": "Successful Response",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/WorkflowScheduleResponse"- }- }- }- },- "422": {- "description": "Validation Error",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/HTTPValidationError"- }- }- }- }- }- }- },- "/v1/workflows/{workflow_permanent_id}/schedules/{workflow_schedule_id}/disable": {- "post": {- "tags": [- "Schedules"- ],- "summary": "Disable a workflow schedule",- "operationId": "schedules_disable",- "parameters": [- {- "name": "workflow_permanent_id",- "in": "path",- "required": true,- "schema": {- "type": "string",- "title": "Workflow Permanent Id"- }- },- {- "name": "workflow_schedule_id",- "in": "path",- "required": true,- "schema": {- "type": "string",- "title": "Workflow Schedule Id"- }- },- {- "name": "x-api-key",- "in": "header",- "required": false,- "schema": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings.",- "title": "X-Api-Key"- },- "description": "Skyvern API key for authentication. API key can be found at https://app.skyvern.com/settings."- }- ],- "responses": {- "200": {- "description": "Successful Response",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/WorkflowScheduleResponse"- }- }- }- },- "422": {- "description": "Validation Error",- "content": {- "application/json": {- "schema": {- "$ref": "#/components/schemas/HTTPValidationError"- }- }- }- }- }- }}},"components": {@@ -6576,7 +5983,7 @@"properties": {"file": {"type": "string",- "contentMediaType": "application/octet-stream",+ "format": "binary","title": "File"}},@@ -8147,19 +7554,6 @@"title": "CreditCardCredentialResponse","description": "Response model for credit card credentials — non-sensitive fields only.\n\nSECURITY: Must NEVER include full card number, CVV, expiration date, or card holder name."},- "DeleteScheduleResponse": {- "properties": {- "ok": {- "type": "boolean",- "title": "Ok"- }- },- "type": "object",- "required": [- "ok"- ],- "title": "DeleteScheduleResponse"- },"DeployScriptRequest": {"properties": {"files": {@@ -13146,164 +12540,9 @@"title": "Parameter Type","default": "onepassword"},- "key": {- "type": "string",- "title": "Key"- },- "description": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "title": "Description"- },- "onepassword_credential_parameter_id": {- "type": "string",- "title": "Onepassword Credential Parameter Id"- },- "workflow_id": {- "type": "string",- "title": "Workflow Id"- },- "vault_id": {- "type": "string",- "title": "Vault Id"- },- "item_id": {- "type": "string",- "title": "Item Id"- },- "created_at": {- "type": "string",- "format": "date-time",- "title": "Created At"- },- "modified_at": {- "type": "string",- "format": "date-time",- "title": "Modified At"- },- "deleted_at": {- "anyOf": [- {- "type": "string",- "format": "date-time"- },- {- "type": "null"- }- ],- "title": "Deleted At"- }- },- "type": "object",- "required": [- "key",- "onepassword_credential_parameter_id",- "workflow_id",- "vault_id",- "item_id",- "created_at",- "modified_at"- ],- "title": "OnePasswordCredentialParameter"- },- "OnePasswordCredentialParameterYAML": {- "properties": {- "parameter_type": {- "type": "string",- "const": "onepassword",- "title": "Parameter Type",- "default": "onepassword"- },- "key": {- "type": "string",- "title": "Key"- },- "description": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "title": "Description"- },- "vault_id": {- "type": "string",- "title": "Vault Id"- },- "item_id": {- "type": "string",- "title": "Item Id"- }- },- "type": "object",- "required": [- "key",- "vault_id",- "item_id"- ],- "title": "OnePasswordCredentialParameterYAML"- },- "OrganizationScheduleItem": {- "properties": {- "workflow_schedule_id": {- "type": "string",- "title": "Workflow Schedule Id"- },- "organization_id": {- "type": "string",- "title": "Organization Id"- },- "workflow_permanent_id": {- "type": "string",- "title": "Workflow Permanent Id"- },- "workflow_title": {- "type": "string",- "title": "Workflow Title"- },- "cron_expression": {- "type": "string",- "title": "Cron Expression"- },- "timezone": {- "type": "string",- "title": "Timezone"- },- "enabled": {- "type": "boolean",- "title": "Enabled"- },- "parameters": {- "anyOf": [- {- "additionalProperties": true,- "type": "object"- },- {- "type": "null"- }- ],- "title": "Parameters"- },- "name": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "title": "Name"+ "key": {+ "type": "string",+ "title": "Key"},"description": {"anyOf": [@@ -13316,17 +12555,21 @@],"title": "Description"},- "next_run": {- "anyOf": [- {- "type": "string",- "format": "date-time"- },- {- "type": "null"- }- ],- "title": "Next Run"+ "onepassword_credential_parameter_id": {+ "type": "string",+ "title": "Onepassword Credential Parameter Id"+ },+ "workflow_id": {+ "type": "string",+ "title": "Workflow Id"+ },+ "vault_id": {+ "type": "string",+ "title": "Vault Id"+ },+ "item_id": {+ "type": "string",+ "title": "Item Id"},"created_at": {"type": "string",@@ -13337,53 +12580,71 @@"type": "string","format": "date-time","title": "Modified At"+ },+ "deleted_at": {+ "anyOf": [+ {+ "type": "string",+ "format": "date-time"+ },+ {+ "type": "null"+ }+ ],+ "title": "Deleted At"}},"type": "object","required": [- "workflow_schedule_id",- "organization_id",- "workflow_permanent_id",- "workflow_title",- "cron_expression",- "timezone",- "enabled",+ "key",+ "onepassword_credential_parameter_id",+ "workflow_id",+ "vault_id",+ "item_id","created_at","modified_at"],- "title": "OrganizationScheduleItem",- "description": "Compact schedule projection for the org-wide list endpoint.\n\nIntentionally omits `backend_schedule_id` — the list view is for browsing\nschedules in the dashboard, not for managing the underlying execution-backend\nbinding. Callers that need the backend id should fetch the individual\nschedule via the per-workflow get endpoint."+ "title": "OnePasswordCredentialParameter"},- "OrganizationScheduleListResponse": {+ "OnePasswordCredentialParameterYAML": {"properties": {- "schedules": {- "items": {- "$ref": "#/components/schemas/OrganizationScheduleItem"- },- "type": "array",- "title": "Schedules"+ "parameter_type": {+ "type": "string",+ "const": "onepassword",+ "title": "Parameter Type",+ "default": "onepassword"},- "total_count": {- "type": "integer",- "title": "Total Count"+ "key": {+ "type": "string",+ "title": "Key"},- "page": {- "type": "integer",- "title": "Page"+ "description": {+ "anyOf": [+ {+ "type": "string"+ },+ {+ "type": "null"+ }+ ],+ "title": "Description"},- "page_size": {- "type": "integer",- "title": "Page Size"+ "vault_id": {+ "type": "string",+ "title": "Vault Id"+ },+ "item_id": {+ "type": "string",+ "title": "Item Id"}},"type": "object","required": [- "schedules",- "total_count",- "page",- "page_size"+ "key",+ "vault_id",+ "item_id"],- "title": "OrganizationScheduleListResponse"+ "title": "OnePasswordCredentialParameterYAML"},"OutputParameter": {"properties": {@@ -15652,8 +14913,8 @@},"engine": {"$ref": "#/components/schemas/RunEngine",- "description": "\nThe engine that powers the agent task. The default value is `skyvern-1.0`, which is good for simple tasks like filling a form, or searching for information on Google. `skyvern-2.0` is the latest Skyvern agent that performs well with complex and multi-step tasks. The `openai-cua` engine uses OpenAI's CUA model. The `anthropic-cua` uses Anthropic's Claude Sonnet 3.7 model with the computer use tool.\n",- "default": "skyvern-1.0"+ "description": "\nThe engine that powers the agent task. The default value is `skyvern-2.0`, the latest Skyvern agent that performs pretty well with complex and multi-step tasks. `skyvern-1.0` is good for simple tasks like filling a form, or searching for information on Google. The `openai-cua` engine uses OpenAI's CUA model. The `anthropic-cua` uses Anthropic's Claude Sonnet 3.7 model with the computer use tool.\n",+ "default": "skyvern-2.0"},"title": {"anyOf": [@@ -20635,206 +19896,6 @@"title": "WorkflowRunTriggerType","description": "How a workflow run was initiated.\n\n- manual: User clicked \"Run\" in the UI\n- api: Direct API call to the run endpoint\n- scheduled: Triggered by a cron schedule\n- webhook: Triggered by an external system via the webhook endpoint"},- "WorkflowSchedule": {- "properties": {- "workflow_schedule_id": {- "type": "string",- "title": "Workflow Schedule Id"- },- "organization_id": {- "type": "string",- "title": "Organization Id"- },- "workflow_permanent_id": {- "type": "string",- "title": "Workflow Permanent Id"- },- "cron_expression": {- "type": "string",- "title": "Cron Expression"- },- "timezone": {- "type": "string",- "title": "Timezone"- },- "enabled": {- "type": "boolean",- "title": "Enabled"- },- "parameters": {- "anyOf": [- {- "additionalProperties": true,- "type": "object"- },- {- "type": "null"- }- ],- "title": "Parameters"- },- "temporal_schedule_id": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "title": "Temporal Schedule Id"- },- "name": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "title": "Name"- },- "description": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "title": "Description"- },- "created_at": {- "type": "string",- "format": "date-time",- "title": "Created At"- },- "modified_at": {- "type": "string",- "format": "date-time",- "title": "Modified At"- },- "deleted_at": {- "anyOf": [- {- "type": "string",- "format": "date-time"- },- {- "type": "null"- }- ],- "title": "Deleted At"- }- },- "type": "object",- "required": [- "workflow_schedule_id",- "organization_id",- "workflow_permanent_id",- "cron_expression",- "timezone",- "enabled",- "created_at",- "modified_at"- ],- "title": "WorkflowSchedule"- },- "WorkflowScheduleListResponse": {- "properties": {- "schedules": {- "items": {- "$ref": "#/components/schemas/WorkflowSchedule"- },- "type": "array",- "title": "Schedules"- }- },- "type": "object",- "required": [- "schedules"- ],- "title": "WorkflowScheduleListResponse"- },- "WorkflowScheduleResponse": {- "properties": {- "schedule": {- "$ref": "#/components/schemas/WorkflowSchedule"- },- "next_runs": {- "items": {- "type": "string",- "format": "date-time"- },- "type": "array",- "title": "Next Runs"- }- },- "type": "object",- "required": [- "schedule"- ],- "title": "WorkflowScheduleResponse"- },- "WorkflowScheduleUpsertRequest": {- "properties": {- "cron_expression": {- "type": "string",- "title": "Cron Expression"- },- "timezone": {- "type": "string",- "title": "Timezone"- },- "enabled": {- "type": "boolean",- "title": "Enabled",- "default": true- },- "parameters": {- "anyOf": [- {- "additionalProperties": true,- "type": "object"- },- {- "type": "null"- }- ],- "title": "Parameters"- },- "name": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "title": "Name"- },- "description": {- "anyOf": [- {- "type": "string"- },- {- "type": "null"- }- ],- "title": "Description"- }- },- "type": "object",- "required": [- "cron_expression",- "timezone"- ],- "title": "WorkflowScheduleUpsertRequest"- },"WorkflowStatus": {"type": "string","enum": [diff --git a/skyvern/forge/sdk/core/skyvern_context.py b/skyvern/forge/sdk/core/skyvern_context.pyindex 9c2e23f99..52feb3e9f 100644--- a/skyvern/forge/sdk/core/skyvern_context.py+++ b/skyvern/forge/sdk/core/skyvern_context.py@@ -1,10 +1,16 @@-from contextvars import ContextVar+from contextlib import contextmanager+from contextvars import ContextVar, Tokenfrom dataclasses import dataclass, field-from typing import Any+from typing import Any, Iteratorfrom zoneinfo import ZoneInfo+import structlogfrom playwright.async_api import Frame, Page+from skyvern.config import settings++LOG = structlog.get_logger()+@dataclassclass SkyvernContext:@@ -31,6 +37,7 @@ class SkyvernContext:max_screenshot_scrolls: int | None = Nonebrowser_container_ip: str | None = Nonebrowser_container_task_arn: str | None = None+ workflow_feature_flags_entries: dict[str, bool | str | None] = field(default_factory=dict)# feature flagsenable_page_ready_wait: bool = False@@ -116,6 +123,36 @@ class SkyvernContext:return Falsereturn True+ def flush_workflow_feature_flags(self) -> None:+ if not self.workflow_feature_flags_entries:+ return+ if not self.workflow_run_id:+ LOG.debug(+ "Discarding workflow_feature_flags entries for non-workflow context",+ count=len(self.workflow_feature_flags_entries),+ )+ self.workflow_feature_flags_entries.clear()+ return++ feature_resolutions = dict(sorted(self.workflow_feature_flags_entries.items()))+ log_fields: dict[str, Any] = {+ "organization_id": str(self.organization_id or ""),+ "workflow_run_id": str(self.workflow_run_id),+ "workflow_permanent_id": str(self.workflow_permanent_id or ""),+ "feature_resolutions": feature_resolutions,+ "service_name": settings.OTEL_SERVICE_NAME,+ }+ if self.task_id:+ log_fields["task_id"] = str(self.task_id)+ if self.task_v2_id:+ log_fields["task_v2_id"] = str(self.task_v2_id)+ if self.browser_session_id:+ log_fields["browser_session_id"] = str(self.browser_session_id)+ if self.request_id:+ log_fields["request_id"] = str(self.request_id)+ LOG.info("workflow_feature_flags", **log_fields)+ self.workflow_feature_flags_entries.clear()+_context: ContextVar[SkyvernContext | None] = ContextVar("Global context",@@ -162,6 +199,57 @@ def set(context: SkyvernContext) -> None:_context.set(context)+def replace(context: SkyvernContext) -> None:+ """+ Flush the current context summary, then replace it with a new context.++ Args:+ context: The context to set++ Returns:+ None+ """+ _flush_workflow_feature_flags_if_needed(current())+ _context.set(context)+++def _flush_workflow_feature_flags_if_needed(context: SkyvernContext | None) -> None:+ if context is not None and context.workflow_feature_flags_entries:+ context.flush_workflow_feature_flags()+++def _restore(token: Token[SkyvernContext | None]) -> None:+ """+ Flush the current context summary and restore the previous context using a token.++ Args:+ token: ContextVar token returned by ContextVar.set()++ Returns:+ None+ """+ _flush_workflow_feature_flags_if_needed(current())+ _context.reset(token)+++@contextmanager+def scoped(context: SkyvernContext) -> Iterator[SkyvernContext]:+ """+ Temporarily scope the current context to a fresh child context.++ Args:+ context: The child context to set for the scope++ Yields:+ The child context+ """+ token = _context.set(context)+ try:+ yield context+ finally:+ _restore(token)++def reset() -> None:"""Reset the current context@@ -169,4 +257,5 @@ def reset() -> None:Returns:None"""+ _flush_workflow_feature_flags_if_needed(current())_context.set(None)diff --git a/skyvern/forge/sdk/experimentation/providers.py b/skyvern/forge/sdk/experimentation/providers.pyindex c6ee4bca8..cefaa6dc5 100644--- a/skyvern/forge/sdk/experimentation/providers.py+++ b/skyvern/forge/sdk/experimentation/providers.py@@ -1,77 +1,221 @@+import jsonfrom abc import ABC, abstractmethod+from typing import Any, Literalimport structlogfrom cachetools import TTLCache+from skyvern.forge.sdk.core import skyvern_context+LOG = structlog.get_logger()EXPERIMENTATION_CACHE_TTL = 300 # seconds (5 minutes)EXPERIMENTATION_CACHE_MAX_SIZE = 100000 # Max entries per cache+ResolutionKind = Literal["enabled", "value", "payload"]+++def _serialize_properties(properties: dict | None = None) -> str:+ return json.dumps(properties or {}, sort_keys=True, default=str)+++def _make_cache_key(feature_name: str, distinct_id: str, properties: dict | None = None) -> tuple[str, str, str]:+ return feature_name, distinct_id, _serialize_properties(properties)+++def _serialize_feature_resolution_value(resolution_kind: ResolutionKind, resolved_value: Any) -> bool | str | None:+ if resolved_value is None:+ return None+ if isinstance(resolved_value, bool):+ return resolved_value+ if isinstance(resolved_value, str):+ return resolved_value+ if isinstance(resolved_value, (int, float)):+ return str(resolved_value)+ if resolution_kind == "payload":+ return json.dumps(resolved_value, sort_keys=True, default=str)+ return str(resolved_value)+++def _should_emit_workflow_feature_flags(context: skyvern_context.SkyvernContext) -> bool:+ return bool(context.workflow_run_id)+++def _record_workflow_feature_flags(+ context: skyvern_context.SkyvernContext | None,+ *,+ feature_name: str,+ resolved_value: bool | str | None,+) -> None:+ if context is None:+ return+ if not _should_emit_workflow_feature_flags(context):+ return++ context.workflow_feature_flags_entries[feature_name] = resolved_value+++def _build_feature_flag_resolution_log_fields(+ context: skyvern_context.SkyvernContext | None,+ *,+ feature_name: str,+ resolution_kind: ResolutionKind,+ resolved_value: Any,+) -> dict[str, Any]:+ log_fields: dict[str, Any] = {+ "feature_name": feature_name,+ "resolution_kind": resolution_kind,+ "resolved_value": resolved_value,+ }+ if context is None:+ return log_fields++ if context.organization_id:+ log_fields["organization_id"] = context.organization_id+ if context.request_id:+ log_fields["request_id"] = context.request_id+ if context.task_id:+ log_fields["task_id"] = context.task_id+ if context.task_v2_id:+ log_fields["task_v2_id"] = context.task_v2_id+ if context.workflow_run_id:+ log_fields["workflow_run_id"] = context.workflow_run_id+ if context.workflow_permanent_id:+ log_fields["workflow_permanent_id"] = context.workflow_permanent_id+ if context.browser_session_id:+ log_fields["browser_session_id"] = context.browser_session_id+ return log_fields+++def flush_workflow_feature_flags(context: skyvern_context.SkyvernContext | None = None) -> None:+ resolved_context = context or skyvern_context.current()+ if resolved_context is not None:+ resolved_context.flush_workflow_feature_flags()+++def record_feature_flag_resolution(+ *,+ feature_name: str,+ resolution_kind: ResolutionKind,+ resolved_value: Any,+) -> None:+ context = skyvern_context.current()+ serialized_resolved_value = _serialize_feature_resolution_value(resolution_kind, resolved_value)+ LOG.debug(+ "feature_flag_resolution",+ **_build_feature_flag_resolution_log_fields(+ context,+ feature_name=feature_name,+ resolution_kind=resolution_kind,+ resolved_value=resolved_value,+ ),+ )+ _record_workflow_feature_flags(+ context,+ feature_name=feature_name,+ resolved_value=serialized_resolved_value,+ )+class BaseExperimentationProvider(ABC):def __init__(self) -> None:- # Cache with composite key (feature_name, distinct_id) for per-entry TTL expirationself.result_map: TTLCache = TTLCache(maxsize=EXPERIMENTATION_CACHE_MAX_SIZE, ttl=EXPERIMENTATION_CACHE_TTL)self.variant_map: TTLCache = TTLCache(maxsize=EXPERIMENTATION_CACHE_MAX_SIZE, ttl=EXPERIMENTATION_CACHE_TTL)self.payload_map: TTLCache = TTLCache(maxsize=EXPERIMENTATION_CACHE_MAX_SIZE, ttl=EXPERIMENTATION_CACHE_TTL)@abstractmethod- async def is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:+ async def _is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:"""Check if a specific feature is enabled."""+ async def is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:+ feature_flag_value = await self._is_feature_enabled(feature_name, distinct_id, properties)+ record_feature_flag_resolution(+ feature_name=feature_name,+ resolution_kind="enabled",+ resolved_value=feature_flag_value,+ )+ return feature_flag_value+async def is_feature_enabled_cached(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:- cache_key = (feature_name, distinct_id)- if cache_key not in self.result_map:- feature_flag_value = await self.is_feature_enabled(feature_name, distinct_id, properties)+ cache_key = _make_cache_key(feature_name, distinct_id, properties)+ if cache_key in self.result_map:+ feature_flag_value = self.result_map[cache_key]+ else:+ feature_flag_value = await self._is_feature_enabled(feature_name, distinct_id, properties)self.result_map[cache_key] = feature_flag_value- if feature_flag_value:- LOG.info("Feature flag is enabled", flag=feature_name, distinct_id=distinct_id)-- return self.result_map[cache_key]+ record_feature_flag_resolution(+ feature_name=feature_name,+ resolution_kind="enabled",+ resolved_value=feature_flag_value,+ )+ return feature_flag_value@abstractmethod- async def get_value(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:+ async def _get_value(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:"""Get the value of a feature."""+ async def get_value(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:+ variant = await self._get_value(feature_name, distinct_id, properties)+ record_feature_flag_resolution(+ feature_name=feature_name,+ resolution_kind="value",+ resolved_value=variant,+ )+ return variant+@abstractmethod- async def get_payload(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:+ async def _get_payload(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> Any:"""Get the payload for a feature flag if it exists."""+ async def get_payload(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> Any:+ payload = await self._get_payload(feature_name, distinct_id, properties)+ record_feature_flag_resolution(+ feature_name=feature_name,+ resolution_kind="payload",+ resolved_value=payload,+ )+ return payload+async def get_value_cached(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:- """Get the value of a feature."""- cache_key = (feature_name, distinct_id)- if cache_key not in self.variant_map:- variant = await self.get_value(feature_name, distinct_id, properties)+ cache_key = _make_cache_key(feature_name, distinct_id, properties)+ if cache_key in self.variant_map:+ variant = self.variant_map[cache_key]+ else:+ variant = await self._get_value(feature_name, distinct_id, properties)self.variant_map[cache_key] = variant- if variant:- LOG.info("Feature is found", flag=feature_name, distinct_id=distinct_id, variant=variant)- return self.variant_map[cache_key]-- async def get_payload_cached(- self, feature_name: str, distinct_id: str, properties: dict | None = None- ) -> str | None:- """Get the payload for a feature flag if it exists."""- cache_key = (feature_name, distinct_id)- if cache_key not in self.payload_map:- payload = await self.get_payload(feature_name, distinct_id, properties)+ record_feature_flag_resolution(+ feature_name=feature_name,+ resolution_kind="value",+ resolved_value=variant,+ )+ return variant++ async def get_payload_cached(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> Any:+ cache_key = _make_cache_key(feature_name, distinct_id, properties)+ if cache_key in self.payload_map:+ payload = self.payload_map[cache_key]+ else:+ payload = await self._get_payload(feature_name, distinct_id, properties)self.payload_map[cache_key] = payload- if payload:- LOG.info("Feature payload is found", flag=feature_name, distinct_id=distinct_id, payload=payload)- return self.payload_map[cache_key]+ record_feature_flag_resolution(+ feature_name=feature_name,+ resolution_kind="payload",+ resolved_value=payload,+ )+ return payloadclass NoOpExperimentationProvider(BaseExperimentationProvider):def __init__(self) -> None:super().__init__()- async def is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:+ async def _is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:return False- async def get_value(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:+ async def _get_value(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:return None- async def get_payload(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:+ async def _get_payload(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> Any:return Nonediff --git a/skyvern/forge/sdk/routes/sdk.py b/skyvern/forge/sdk/routes/sdk.pyindex b920e16b7..c2533dead 100644--- a/skyvern/forge/sdk/routes/sdk.py+++ b/skyvern/forge/sdk/routes/sdk.py@@ -136,7 +136,7 @@ async def run_sdk_action()context = skyvern_context.ensure_context()- skyvern_context.set(+ skyvern_context.replace(SkyvernContext(request_id=context.request_id,organization_id=task.organization_id,diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.pyindex a1fa17c86..883ce4543 100644--- a/skyvern/forge/sdk/workflow/models/block.py+++ b/skyvern/forge/sdk/workflow/models/block.py@@ -4723,37 +4723,32 @@ class TaskV2Block(Block):organization_id=organization_id,block_workflow_run_id=task_v2.workflow_run_id,)-- task_v2 = await task_v2_service.run_task_v2(- organization=organization,- task_v2_id=task_v2.observer_cruise_id,- request_id=None,- max_steps_override=self.max_steps,- browser_session_id=browser_session_id,+ except Exception as e:+ LOG.exception("Failed to initialize or queue TaskV2", error=e)+ output_reason = f"Failed to initialize or queue TaskV2: {str(e)}"+ await self.record_output_parameter_value(+ workflow_run_context, workflow_run_id, {"failure_reason": output_reason})- finally:- context: skyvern_context.SkyvernContext | None = skyvern_context.current()- current_run_id = context.run_id if context and context.run_id else workflow_run_id- root_workflow_run_id = (- context.root_workflow_run_id if context and context.root_workflow_run_id else workflow_run_id- )- # Preserve loop_internal_state so per-iteration download filtering- # continues to work for subsequent blocks in the same loop iteration (SKY-7005)- loop_state = context.loop_internal_state if context else None- skyvern_context.set(- skyvern_context.SkyvernContext(- organization_id=organization_id,- organization_name=organization.organization_name,- workflow_id=workflow_run.workflow_id,- workflow_permanent_id=workflow_run.workflow_permanent_id,- workflow_run_id=workflow_run_id,- root_workflow_run_id=root_workflow_run_id,- run_id=current_run_id,- browser_session_id=browser_session_id,- max_screenshot_scrolls=workflow_run.max_screenshot_scrolls,- loop_internal_state=loop_state,- )+ return await self.build_block_result(+ success=False,+ failure_reason=output_reason,+ output_parameter_value=None,+ status=BlockStatus.failed,+ workflow_run_block_id=workflow_run_block_id,+ organization_id=organization_id,)++ # run_task_v2 uses scoped() internally, so context is always restored+ # even if it raises. Its own exception handlers mark the task as+ # failed/terminated with proper status, so we let exceptions propagate+ # to the status-mapping logic below.+ task_v2 = await task_v2_service.run_task_v2(+ organization=organization,+ task_v2_id=task_v2.observer_cruise_id,+ request_id=None,+ max_steps_override=self.max_steps,+ browser_session_id=browser_session_id,+ )result_dict = Noneif task_v2:result_dict = task_v2.output@@ -6877,92 +6872,79 @@ class WorkflowTriggerBlock(Block):browser_session_id=resolved_browser_session_id,)- # Save the parent's skyvern_context because setup_workflow_run and- # execute_workflow overwrite it with the child's values. We restore- # it after the child finishes so subsequent parent blocks get correct- # context (logs, observability, workflow_run_id, etc.).- from skyvern.forge.sdk.core import skyvern_context # noqa: PLC0415-+ # Isolate the synchronous child workflow in a placeholder scope so+ # setup_workflow_run() can replace the current context without+ # flushing the parent's pending workflow_feature_flags summary.parent_context = skyvern_context.current()- try:- triggered_workflow_run = await app.WORKFLOW_SERVICE.setup_workflow_run(- request_id=None,- workflow_request=workflow_request,- workflow_permanent_id=resolved_workflow_permanent_id,- organization=organization,+ with skyvern_context.scoped(+ skyvern_context.SkyvernContext(+ run_id=parent_context.run_id if parent_context else None,+ root_workflow_run_id=parent_context.root_workflow_run_id if parent_context else None,+ )+ ):+ try:+ triggered_workflow_run = await app.WORKFLOW_SERVICE.setup_workflow_run(+ request_id=None,+ workflow_request=workflow_request,+ workflow_permanent_id=resolved_workflow_permanent_id,+ organization=organization,+ parent_workflow_run_id=workflow_run_id,+ )+ except Exception as e:+ error_msg = get_user_facing_exception_message(e)+ return await _fail(f"Failed to setup triggered workflow run: {error_msg}")++ triggered_run_id = triggered_workflow_run.workflow_run_id++ LOG.info(+ "Triggered workflow run (sync)",parent_workflow_run_id=workflow_run_id,+ triggered_workflow_run_id=triggered_run_id,+ triggered_workflow_permanent_id=resolved_workflow_permanent_id,)- except Exception as e:- error_msg = get_user_facing_exception_message(e)- if parent_context:- skyvern_context.set(parent_context)- if created_fresh_session and resolved_browser_session_id:++ try:+ final_run = await app.WORKFLOW_SERVICE.execute_workflow(+ workflow_run_id=triggered_run_id,+ api_key=None,+ organization=organization,+ browser_session_id=resolved_browser_session_id,+ )+ success = final_run.status == WorkflowRunStatus.completed+ output_data = {+ "workflow_run_id": triggered_run_id,+ "workflow_permanent_id": resolved_workflow_permanent_id,+ "status": str(final_run.status),+ "failure_reason": final_run.failure_reason,+ }+ # Include the child workflow's output parameters so downstream+ # blocks can reference them (e.g. block_3_output.outputs.block_2_output)try:- await app.PERSISTENT_SESSIONS_MANAGER.close_session(- organization_id, resolved_browser_session_id+ child_output_params = (+ await app.WORKFLOW_SERVICE.get_output_parameter_workflow_run_output_parameter_tuples(+ workflow_id=final_run.workflow_id,+ workflow_run_id=triggered_run_id,+ ))+ child_outputs: dict[str, Any] = {}+ for output_param, run_output_param in child_output_params:+ child_outputs[output_param.key] = run_output_param.value+ output_data["outputs"] = child_outputsexcept Exception:LOG.warning(- "Failed to close child browser session after setup failure",- child_browser_session_id=resolved_browser_session_id,+ "Failed to fetch child workflow outputs",+ triggered_workflow_run_id=triggered_run_id,exc_info=True,)- return await _fail(f"Failed to setup triggered workflow run: {error_msg}")-- triggered_run_id = triggered_workflow_run.workflow_run_id-- LOG.info(- "Triggered workflow run (sync)",- parent_workflow_run_id=workflow_run_id,- triggered_workflow_run_id=triggered_run_id,- triggered_workflow_permanent_id=resolved_workflow_permanent_id,- )-- try:- final_run = await app.WORKFLOW_SERVICE.execute_workflow(- workflow_run_id=triggered_run_id,- api_key=None,- organization=organization,- browser_session_id=resolved_browser_session_id,- )- success = final_run.status == WorkflowRunStatus.completed- output_data = {- "workflow_run_id": triggered_run_id,- "workflow_permanent_id": resolved_workflow_permanent_id,- "status": str(final_run.status),- "failure_reason": final_run.failure_reason,- }- # Include the child workflow's output parameters so downstream- # blocks can reference them (e.g. block_3_output.outputs.block_2_output)- try:- child_output_params = (- await app.WORKFLOW_SERVICE.get_output_parameter_workflow_run_output_parameter_tuples(- workflow_id=final_run.workflow_id,- workflow_run_id=triggered_run_id,- )- )- child_outputs: dict[str, Any] = {}- for output_param, run_output_param in child_output_params:- child_outputs[output_param.key] = run_output_param.value- output_data["outputs"] = child_outputs- except Exception:- LOG.warning(- "Failed to fetch child workflow outputs",- triggered_workflow_run_id=triggered_run_id,- exc_info=True,- )- except Exception as e:- error_msg = get_user_facing_exception_message(e)- output_data = {- "workflow_run_id": triggered_run_id,- "workflow_permanent_id": resolved_workflow_permanent_id,- "status": "failed",- "failure_reason": f"Triggered workflow execution failed: {error_msg}",- }- success = False- finally:- if parent_context:- skyvern_context.set(parent_context)+ except Exception as e:+ error_msg = get_user_facing_exception_message(e)+ output_data = {+ "workflow_run_id": triggered_run_id,+ "workflow_permanent_id": resolved_workflow_permanent_id,+ "status": "failed",+ "failure_reason": f"Triggered workflow execution failed: {error_msg}",+ }+ success = Falseif created_fresh_session and resolved_browser_session_id:try:await app.PERSISTENT_SESSIONS_MANAGER.close_session(diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.pyindex 1601b10ca..16d99c1b9 100644--- a/skyvern/forge/sdk/workflow/service.py+++ b/skyvern/forge/sdk/workflow/service.py@@ -700,7 +700,7 @@ class WorkflowService:root_workflow_run_id = (context.root_workflow_run_id if context and context.root_workflow_run_id else workflow_run.workflow_run_id)- skyvern_context.set(+ skyvern_context.replace(SkyvernContext(organization_id=organization.organization_id,organization_name=organization.organization_name,diff --git a/skyvern/services/task_v2_service.py b/skyvern/services/task_v2_service.pyindex b9f6781af..f1d0f653a 100644--- a/skyvern/services/task_v2_service.py+++ b/skyvern/services/task_v2_service.py@@ -482,59 +482,72 @@ async def run_task_v2(raise TaskV2NotFound(task_v2_id=task_v2_id)workflow, workflow_run = None, None- try:- workflow, workflow_run, task_v2 = await run_task_v2_helper(- organization=organization,- task_v2=task_v2,- request_id=request_id,- max_steps_override=max_steps_override,- browser_session_id=browser_session_id,- )- except TaskTerminationError as e:- task_v2 = await mark_task_v2_as_terminated(- task_v2_id=task_v2_id,- workflow_run_id=task_v2.workflow_run_id,- organization_id=organization_id,- failure_reason=e.message,- )- LOG.info("Task v2 is terminated", task_v2_id=task_v2_id, failure_reason=e.message)- return task_v2- except OperationalError:- LOG.error("Database error when running task v2", exc_info=True)- task_v2 = await mark_task_v2_as_failed(- task_v2_id,- workflow_run_id=task_v2.workflow_run_id,- failure_reason="Database error when running task 2.0",- organization_id=organization_id,- )- except Exception as e:- LOG.error("Failed to run task v2", exc_info=True)- failure_reason = f"Failed to run task 2.0: {str(e)}"- task_v2 = await mark_task_v2_as_failed(- task_v2_id,- workflow_run_id=task_v2.workflow_run_id,- failure_reason=failure_reason,- organization_id=organization_id,- )- finally:- if task_v2.workflow_id and not workflow:- workflow = await app.WORKFLOW_SERVICE.get_workflow(task_v2.workflow_id, organization_id=organization_id)- if task_v2.workflow_run_id and not workflow_run:- workflow_run = await app.WORKFLOW_SERVICE.get_workflow_run(- task_v2.workflow_run_id, organization_id=organization_id- )- if workflow and workflow_run and workflow_run.parent_workflow_run_id is None:- await app.WORKFLOW_SERVICE.clean_up_workflow(- workflow=workflow,- workflow_run=workflow_run,+ parent_context = skyvern_context.current()+ current_run_id = parent_context.run_id if parent_context and parent_context.run_id else task_v2_id+ context = SkyvernContext(+ organization_id=organization_id,+ organization_name=organization.organization_name,+ root_workflow_run_id=parent_context.root_workflow_run_id if parent_context else None,+ task_v2_id=task_v2_id,+ run_id=current_run_id,+ request_id=request_id,+ browser_session_id=browser_session_id,+ )+ # SKY-7005: scoped() restores the parent context on exit, preserving+ # loop_internal_state so per-iteration download filtering continues to+ # work for subsequent blocks in the same loop iteration.+ with skyvern_context.scoped(context):+ try:+ workflow, workflow_run, task_v2 = await run_task_v2_helper(+ organization=organization,+ task_v2=task_v2,+ request_id=request_id,+ max_steps_override=max_steps_override,browser_session_id=browser_session_id,- close_browser_on_completion=browser_session_id is None and not workflow_run.browser_address,- need_call_webhook=False,)- else:- LOG.warning("Workflow or workflow run not found")-- skyvern_context.reset()+ except TaskTerminationError as e:+ task_v2 = await mark_task_v2_as_terminated(+ task_v2_id=task_v2_id,+ workflow_run_id=task_v2.workflow_run_id,+ organization_id=organization_id,+ failure_reason=e.message,+ )+ LOG.info("Task v2 is terminated", task_v2_id=task_v2_id, failure_reason=e.message)+ return task_v2+ except OperationalError:+ LOG.error("Database error when running task v2", exc_info=True)+ task_v2 = await mark_task_v2_as_failed(+ task_v2_id,+ workflow_run_id=task_v2.workflow_run_id,+ failure_reason="Database error when running task 2.0",+ organization_id=organization_id,+ )+ except Exception as e:+ LOG.error("Failed to run task v2", exc_info=True)+ failure_reason = f"Failed to run task 2.0: {str(e)}"+ task_v2 = await mark_task_v2_as_failed(+ task_v2_id,+ workflow_run_id=task_v2.workflow_run_id,+ failure_reason=failure_reason,+ organization_id=organization_id,+ )+ finally:+ if task_v2.workflow_id and not workflow:+ workflow = await app.WORKFLOW_SERVICE.get_workflow(task_v2.workflow_id, organization_id=organization_id)+ if task_v2.workflow_run_id and not workflow_run:+ workflow_run = await app.WORKFLOW_SERVICE.get_workflow_run(+ task_v2.workflow_run_id, organization_id=organization_id+ )+ if workflow and workflow_run and workflow_run.parent_workflow_run_id is None:+ await app.WORKFLOW_SERVICE.clean_up_workflow(+ workflow=workflow,+ workflow_run=workflow_run,+ browser_session_id=browser_session_id,+ close_browser_on_completion=browser_session_id is None and not workflow_run.browser_address,+ need_call_webhook=False,+ )+ else:+ LOG.warning("Workflow or workflow run not found")return task_v2@@ -618,29 +631,30 @@ async def run_task_v2_helper(###################### run task v2 ######################- context: skyvern_context.SkyvernContext | None = skyvern_context.current()+ # NOTE: run_task_v2 pushes a partial SkyvernContext via scoped() before+ # calling this function. We enrich it here with workflow-level fields+ # that are only available after the DB lookups above. This function+ # MUST be called inside run_task_v2's scoped() block.+ context = skyvern_context.ensure_context()current_run_id = context.run_id if context and context.run_id else task_v2_id# task v2 can be nested inside a workflow run, so we need to use the root workflow run idroot_workflow_run_id = context.root_workflow_run_id if context and context.root_workflow_run_id else workflow_run_id+ context.organization_id = organization_id+ context.workflow_id = workflow_id+ context.workflow_permanent_id = workflow.workflow_permanent_id+ context.workflow_run_id = workflow_run_id+ context.root_workflow_run_id = root_workflow_run_id+ context.request_id = request_id+ context.task_v2_id = task_v2_id+ context.run_id = current_run_id+ context.browser_session_id = browser_session_id+ context.max_screenshot_scrolls = task_v2.max_screenshot_scrollsenable_parse_select_in_extract = await app.EXPERIMENTATION_PROVIDER.is_feature_enabled_cached("ENABLE_PARSE_SELECT_IN_EXTRACT",current_run_id,properties={"organization_id": organization_id, "task_url": task_v2.url},)- skyvern_context.set(- SkyvernContext(- organization_id=organization_id,- workflow_id=workflow_id,- workflow_run_id=workflow_run_id,- root_workflow_run_id=root_workflow_run_id,- request_id=request_id,- task_v2_id=task_v2_id,- run_id=current_run_id,- browser_session_id=browser_session_id,- max_screenshot_scrolls=task_v2.max_screenshot_scrolls,- enable_parse_select_in_extract=bool(enable_parse_select_in_extract),- )- )+ context.enable_parse_select_in_extract = bool(enable_parse_select_in_extract)task_v2 = await app.DATABASE.observer.update_task_v2(task_v2_id=task_v2_id, organization_id=organization_id, status=TaskV2Status.runningdiff --git a/tests/unit/test_feature_flag_resolution_logging.py b/tests/unit/test_feature_flag_resolution_logging.pynew file mode 100644index 000000000..a2a717978--- /dev/null+++ b/tests/unit/test_feature_flag_resolution_logging.py@@ -0,0 +1,481 @@+import json+from typing import Any++import pytest++from skyvern.forge.sdk.core import skyvern_context+from skyvern.forge.sdk.core.skyvern_context import SkyvernContext+from skyvern.forge.sdk.experimentation import providers as providers_module+from skyvern.forge.sdk.experimentation.providers import BaseExperimentationProvider+++class CaptureLogger:+ def __init__(self) -> None:+ self.records: list[tuple[str, str, dict[str, Any]]] = []++ def debug(self, event: str, **kwargs: Any) -> None:+ self.records.append(("debug", event, kwargs))++ def info(self, event: str, **kwargs: Any) -> None:+ self.records.append(("info", event, kwargs))++ def warning(self, event: str, **kwargs: Any) -> None:+ self.records.append(("warning", event, kwargs))++ def exception(self, event: str, **kwargs: Any) -> None:+ self.records.append(("exception", event, kwargs))+++class FakeExperimentationProvider(BaseExperimentationProvider):+ def __init__(+ self,+ *,+ enabled_results: list[bool] | None = None,+ value_results: list[str | None] | None = None,+ payload_results: list[Any] | None = None,+ ) -> None:+ super().__init__()+ self.enabled_results = list(enabled_results or [])+ self.value_results = list(value_results or [])+ self.payload_results = list(payload_results or [])+ self.enabled_calls: list[tuple[str, str, dict | None]] = []+ self.value_calls: list[tuple[str, str, dict | None]] = []+ self.payload_calls: list[tuple[str, str, dict | None]] = []++ async def _is_feature_enabled(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> bool:+ self.enabled_calls.append((feature_name, distinct_id, properties))+ resolved_value = self.enabled_results.pop(0)+ return resolved_value++ async def _get_value(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> str | None:+ self.value_calls.append((feature_name, distinct_id, properties))+ resolved_value = self.value_results.pop(0)+ return resolved_value++ async def _get_payload(self, feature_name: str, distinct_id: str, properties: dict | None = None) -> Any:+ self.payload_calls.append((feature_name, distinct_id, properties))+ resolved_value = self.payload_results.pop(0)+ return resolved_value++[email protected](autouse=True)+def reset_context() -> None:+ skyvern_context.reset()+ yield+ skyvern_context.reset()+++def _set_context() -> None:+ skyvern_context.set(+ SkyvernContext(+ request_id="req_123",+ organization_id="ctx_org",+ task_id="tsk_123",+ workflow_run_id="wr_123",+ workflow_permanent_id="wfp_123",+ browser_session_id="pbs_123",+ )+ )+++def _set_logger(monkeypatch: pytest.MonkeyPatch, logger: CaptureLogger) -> None:+ monkeypatch.setattr(skyvern_context, "LOG", logger)+ monkeypatch.setattr(providers_module, "LOG", logger)+++def test_record_workflow_feature_flags_uses_context_metadata(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ logger = CaptureLogger()+ _set_logger(monkeypatch, logger)+ skyvern_context.set(+ SkyvernContext(+ request_id="req_123",+ organization_id="org_123",+ workflow_run_id="wr_123",+ workflow_permanent_id="wfp_123",+ task_id="tsk_123",+ browser_session_id="pbs_123",+ )+ )++ providers_module.record_feature_flag_resolution(+ feature_name="TEST_FLAG",+ resolution_kind="value",+ resolved_value="variant-a",+ )++ skyvern_context.reset()++ assert [event for _, event, _ in logger.records if event == "feature_flag_resolution"] == [+ "feature_flag_resolution"+ ]+ _, event, fields = logger.records[0]+ assert event == "feature_flag_resolution"+ assert fields["feature_name"] == "TEST_FLAG"+ assert fields["resolution_kind"] == "value"+ assert fields["resolved_value"] == "variant-a"+ assert fields["organization_id"] == "org_123"+ assert fields["workflow_run_id"] == "wr_123"+ assert fields["workflow_permanent_id"] == "wfp_123"+ assert fields["task_id"] == "tsk_123"+ assert fields["browser_session_id"] == "pbs_123"+ assert fields["request_id"] == "req_123"++ _, event, fields = logger.records[1]+ assert event == "workflow_feature_flags"+ assert fields["organization_id"] == "org_123"+ assert fields["workflow_run_id"] == "wr_123"+ assert fields["workflow_permanent_id"] == "wfp_123"+ assert fields["task_id"] == "tsk_123"+ assert fields["browser_session_id"] == "pbs_123"+ assert fields["request_id"] == "req_123"+ assert fields["feature_resolutions"] == {"TEST_FLAG": "variant-a"}+++async def test_cached_enabled_logging_is_normalized_and_property_aware(monkeypatch: pytest.MonkeyPatch) -> None:+ logger = CaptureLogger()+ _set_logger(monkeypatch, logger)+ _set_context()++ provider = FakeExperimentationProvider(enabled_results=[True, False])+ first_properties = {+ "task_url": "https://first.example.com/path?foo=bar",+ "organization_id": "org_123",+ "alpha": "1",+ }+ second_properties = {+ "task_url": "https://second.example.com/other",+ "organization_id": "org_123",+ "alpha": "1",+ }++ assert await provider.is_feature_enabled_cached("TEST_FLAG", "distinct_123", first_properties) is True+ assert await provider.is_feature_enabled_cached("TEST_FLAG", "distinct_123", second_properties) is False+ assert await provider.is_feature_enabled_cached("TEST_FLAG", "distinct_123", second_properties) is False++ assert len(provider.enabled_calls) == 2++ skyvern_context.reset()++ debug_records = [fields for _, event, fields in logger.records if event == "feature_flag_resolution"]+ assert len(debug_records) == 3+ assert debug_records[0]["resolved_value"] is True+ assert debug_records[1]["resolved_value"] is False+ assert debug_records[2]["resolved_value"] is False+ assert debug_records[0]["organization_id"] == "ctx_org"+ assert debug_records[0]["workflow_run_id"] == "wr_123"+ assert debug_records[0]["workflow_permanent_id"] == "wfp_123"+ assert debug_records[0]["task_id"] == "tsk_123"+ assert debug_records[0]["browser_session_id"] == "pbs_123"++ _, event, fields = logger.records[-1]+ assert event == "workflow_feature_flags"+ assert fields["workflow_run_id"] == "wr_123"+ assert fields["feature_resolutions"] == {"TEST_FLAG": False}+ assert "task_v2_id" not in fields+++async def test_payload_logging_serializes_payload_values(monkeypatch: pytest.MonkeyPatch) -> None:+ logger = CaptureLogger()+ _set_logger(monkeypatch, logger)+ _set_context()++ provider = FakeExperimentationProvider(+ payload_results=[+ {+ "cpu": "1",+ "memory": "4096",+ "mode": "burst",+ "nested": {"region": "us-east-1"},+ "items": [{"kind": "worker", "size": "large"}],+ }+ ]+ )++ payload = await provider.get_payload_cached(+ "WORKFLOW_COMPUTE_PROFILE",+ "distinct_123",+ {"organization_id": "org_456", "task_url": "https://secure.example.com/run/1"},+ )++ assert isinstance(payload, dict)++ skyvern_context.reset()++ debug_records = [fields for _, event, fields in logger.records if event == "feature_flag_resolution"]+ assert len(debug_records) == 1+ assert debug_records[0]["feature_name"] == "WORKFLOW_COMPUTE_PROFILE"+ assert debug_records[0]["resolution_kind"] == "payload"+ assert debug_records[0]["organization_id"] == "ctx_org"++ _, event, fields = logger.records[-1]+ assert event == "workflow_feature_flags"+ serialized_payload = json.loads(fields["feature_resolutions"]["WORKFLOW_COMPUTE_PROFILE"])+ assert serialized_payload["cpu"] == "1"+ assert serialized_payload["memory"] == "4096"+ assert serialized_payload["mode"] == "burst"+ assert serialized_payload["nested"]["region"] == "us-east-1"+ assert serialized_payload["items"][0]["kind"] == "worker"+++async def test_direct_experimentation_methods_emit_feature_resolution_logging(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ logger = CaptureLogger()+ _set_logger(monkeypatch, logger)+ _set_context()++ provider = FakeExperimentationProvider(+ enabled_results=[True],+ value_results=["variant-a"],+ payload_results=[{"mode": "burst"}],+ )++ assert await provider.is_feature_enabled("DIRECT_ENABLED", "distinct_123", {"organization_id": "org_123"}) is True+ assert await provider.get_value("DIRECT_VALUE", "distinct_123", {"organization_id": "org_123"}) == "variant-a"+ assert await provider.get_payload("DIRECT_PAYLOAD", "distinct_123", {"organization_id": "org_123"}) == {+ "mode": "burst"+ }++ skyvern_context.reset()++ debug_records = [fields for _, event, fields in logger.records if event == "feature_flag_resolution"]+ assert len(debug_records) == 3+ assert debug_records[0]["feature_name"] == "DIRECT_ENABLED"+ assert debug_records[0]["resolved_value"] is True+ assert debug_records[1]["feature_name"] == "DIRECT_VALUE"+ assert debug_records[1]["resolved_value"] == "variant-a"+ assert debug_records[2]["feature_name"] == "DIRECT_PAYLOAD"+ assert debug_records[2]["resolved_value"] == {"mode": "burst"}++ summary_fields = [fields for _, event, fields in logger.records if event == "workflow_feature_flags"][0]+ assert summary_fields["feature_resolutions"] == {+ "DIRECT_ENABLED": True,+ "DIRECT_PAYLOAD": json.dumps({"mode": "burst"}, sort_keys=True),+ "DIRECT_VALUE": "variant-a",+ }+++async def test_cache_hit_resolutions_only_appear_in_summary_per_workflow_run(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ logger = CaptureLogger()+ _set_logger(monkeypatch, logger)+ provider = FakeExperimentationProvider(enabled_results=[False])+ properties = {"organization_id": "org_123"}++ skyvern_context.set(+ SkyvernContext(+ request_id="req_123",+ organization_id="org_123",+ task_id="tsk_123",+ workflow_run_id="wr_123",+ workflow_permanent_id="wfp_123",+ )+ )+ assert await provider.is_feature_enabled_cached("TEST_FLAG", "org_123", properties) is False+ assert await provider.is_feature_enabled_cached("TEST_FLAG", "org_123", properties) is False+ assert await provider.is_feature_enabled_cached("TEST_FLAG", "org_123", properties) is False++ debug_records = [fields for _, event, fields in logger.records if event == "feature_flag_resolution"]+ assert len(debug_records) == 3+ assert all(record["resolved_value"] is False for record in debug_records)++ skyvern_context.reset()++ first_summary = [fields for _, event, fields in logger.records if event == "workflow_feature_flags"][0]+ assert first_summary["workflow_run_id"] == "wr_123"+ assert first_summary["feature_resolutions"] == {"TEST_FLAG": False}++ skyvern_context.set(+ SkyvernContext(+ request_id="req_456",+ organization_id="org_123",+ task_id="tsk_456",+ workflow_run_id="wr_456",+ workflow_permanent_id="wfp_123",+ )+ )+ assert await provider.is_feature_enabled_cached("TEST_FLAG", "org_123", properties) is False++ debug_records_after_second_run = [+ fields for _, event, fields in logger.records if event == "feature_flag_resolution"+ ]+ assert len(debug_records_after_second_run) == 4+ assert all(record["resolved_value"] is False for record in debug_records_after_second_run)++ skyvern_context.reset()++ summary_records = [fields for _, event, fields in logger.records if event == "workflow_feature_flags"]+ assert len(summary_records) == 2+ assert summary_records[1]["workflow_run_id"] == "wr_456"+ assert summary_records[1]["feature_resolutions"] == {"TEST_FLAG": False}+++def test_workflow_feature_flags_reads_metadata_from_context(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ logger = CaptureLogger()+ _set_logger(monkeypatch, logger)+ skyvern_context.set(+ SkyvernContext(+ request_id="req_123",+ organization_id="org_123",+ workflow_run_id="wr_123",+ workflow_permanent_id="wfp_123",+ task_id="tsk_123",+ )+ )++ providers_module.record_feature_flag_resolution(+ feature_name="TEST_FLAG",+ resolution_kind="value",+ resolved_value="variant-a",+ )++ skyvern_context.reset()++ debug_fields = [fields for _, event, fields in logger.records if event == "feature_flag_resolution"][0]+ assert debug_fields["organization_id"] == "org_123"+ assert debug_fields["workflow_run_id"] == "wr_123"+ assert debug_fields["workflow_permanent_id"] == "wfp_123"+ assert debug_fields["task_id"] == "tsk_123"+ assert debug_fields["request_id"] == "req_123"++ summary_fields = [fields for _, event, fields in logger.records if event == "workflow_feature_flags"][0]+ assert summary_fields["organization_id"] == "org_123"+ assert summary_fields["workflow_run_id"] == "wr_123"+ assert summary_fields["workflow_permanent_id"] == "wfp_123"+ assert summary_fields["task_id"] == "tsk_123"+ assert summary_fields["request_id"] == "req_123"+++def test_workflow_feature_flags_uses_last_effective_value_for_feature(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ logger = CaptureLogger()+ _set_logger(monkeypatch, logger)+ skyvern_context.set(+ SkyvernContext(+ organization_id="org_123",+ workflow_run_id="wr_123",+ workflow_permanent_id="wfp_123",+ )+ )++ providers_module.record_feature_flag_resolution(+ feature_name="YESCAPTCHA_VERSION",+ resolution_kind="value",+ resolved_value=None,+ )+ providers_module.record_feature_flag_resolution(+ feature_name="YESCAPTCHA_VERSION",+ resolution_kind="value",+ resolved_value="1-3-0",+ )++ skyvern_context.reset()++ summary_fields = [fields for _, event, fields in logger.records if event == "workflow_feature_flags"][0]+ assert summary_fields["feature_resolutions"]["YESCAPTCHA_VERSION"] == "1-3-0"+++async def test_get_value_cached_caches_none_and_uses_sorted_properties_key() -> None:+ provider = FakeExperimentationProvider(value_results=[None, "variant-b"])+ properties_a = {"workflow_permanent_id": "wfp_123", "organization_id": "org_123"}+ properties_b = {"organization_id": "org_123", "workflow_permanent_id": "wfp_123"}+ properties_c = {"organization_id": "org_123", "workflow_permanent_id": "wfp_123", "task_url": "https://example.com"}++ assert await provider.get_value_cached("VALUE_FLAG", "distinct_123", properties_a) is None+ assert await provider.get_value_cached("VALUE_FLAG", "distinct_123", properties_b) is None+ assert await provider.get_value_cached("VALUE_FLAG", "distinct_123", properties_c) == "variant-b"++ assert provider.value_calls == [+ ("VALUE_FLAG", "distinct_123", properties_a),+ ("VALUE_FLAG", "distinct_123", properties_c),+ ]+++async def test_get_payload_cached_caches_none_payload_results() -> None:+ provider = FakeExperimentationProvider(payload_results=[None, {"mode": "burst"}])+ properties = {"organization_id": "org_123"}+ other_properties = {"organization_id": "org_123", "task_url": "https://example.com"}++ assert await provider.get_payload_cached("PAYLOAD_FLAG", "distinct_123", properties) is None+ assert await provider.get_payload_cached("PAYLOAD_FLAG", "distinct_123", properties) is None+ assert await provider.get_payload_cached("PAYLOAD_FLAG", "distinct_123", other_properties) == {"mode": "burst"}++ assert provider.payload_calls == [+ ("PAYLOAD_FLAG", "distinct_123", properties),+ ("PAYLOAD_FLAG", "distinct_123", other_properties),+ ]+++async def test_cached_helpers_use_independent_caches_per_resolution_kind() -> None:+ provider = FakeExperimentationProvider(+ enabled_results=[True],+ value_results=["variant-a"],+ payload_results=[{"mode": "burst"}],+ )+ properties = {"organization_id": "org_123"}++ assert await provider.is_feature_enabled_cached("SHARED_FLAG", "distinct_123", properties) is True+ assert await provider.is_feature_enabled_cached("SHARED_FLAG", "distinct_123", properties) is True+ assert await provider.get_value_cached("SHARED_FLAG", "distinct_123", properties) == "variant-a"+ assert await provider.get_value_cached("SHARED_FLAG", "distinct_123", properties) == "variant-a"+ assert await provider.get_payload_cached("SHARED_FLAG", "distinct_123", properties) == {"mode": "burst"}+ assert await provider.get_payload_cached("SHARED_FLAG", "distinct_123", properties) == {"mode": "burst"}++ assert provider.enabled_calls == [("SHARED_FLAG", "distinct_123", properties)]+ assert provider.value_calls == [("SHARED_FLAG", "distinct_123", properties)]+ assert provider.payload_calls == [("SHARED_FLAG", "distinct_123", properties)]+++def test_request_only_context_does_not_emit_workflow_feature_flags(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ logger = CaptureLogger()+ _set_logger(monkeypatch, logger)+ skyvern_context.set(SkyvernContext(request_id="req_only", organization_id="org_123"))++ providers_module.record_feature_flag_resolution(+ feature_name="TEST_FLAG",+ resolution_kind="enabled",+ resolved_value=False,+ )++ skyvern_context.reset()++ debug_fields = [fields for _, event, fields in logger.records if event == "feature_flag_resolution"][0]+ assert debug_fields["request_id"] == "req_only"+ assert debug_fields["organization_id"] == "org_123"+ assert [event for _, event, _ in logger.records if event == "workflow_feature_flags"] == []+++def test_task_only_context_does_not_emit_workflow_feature_flags(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ logger = CaptureLogger()+ _set_logger(monkeypatch, logger)+ skyvern_context.set(SkyvernContext(task_id="tsk_only", organization_id="org_123"))++ providers_module.record_feature_flag_resolution(+ feature_name="TEST_FLAG",+ resolution_kind="enabled",+ resolved_value=False,+ )++ skyvern_context.reset()++ debug_fields = [fields for _, event, fields in logger.records if event == "feature_flag_resolution"][0]+ assert debug_fields["task_id"] == "tsk_only"+ assert debug_fields["organization_id"] == "org_123"+ assert [event for _, event, _ in logger.records if event == "workflow_feature_flags"] == []diff --git a/tests/unit/test_taskv2_block_context_preservation.py b/tests/unit/test_taskv2_block_context_preservation.pyindex 3589c65de..15e17da4b 100644--- a/tests/unit/test_taskv2_block_context_preservation.py+++ b/tests/unit/test_taskv2_block_context_preservation.py@@ -1,35 +1,33 @@+from typing import Any++import pytest++from skyvern.config import settingsfrom skyvern.forge.sdk.core import skyvern_contextfrom skyvern.forge.sdk.core.skyvern_context import SkyvernContext+from skyvern.forge.sdk.experimentation import providers as providers_module-def test_loop_internal_state_dropped_by_naive_context_replacement():- """Demonstrates the bug: creating a new context without carrying loop_internal_state drops it."""- original_context = SkyvernContext(- organization_id="org_1",- workflow_run_id="wr_1",- run_id="wr_1",- loop_internal_state={"downloaded_file_signatures_before_iteration": [("a.pdf", "abc", "https://files/a.pdf")]},- )- skyvern_context.set(original_context)+class CaptureLogger:+ def __init__(self) -> None:+ self.records: list[tuple[str, str, dict[str, Any]]] = []- # Simulate TaskV2Block.finally creating a new context WITHOUT preserving loop state- context = skyvern_context.current()- new_context = SkyvernContext(- organization_id="org_1",- workflow_run_id="wr_1",- run_id=context.run_id,- )- skyvern_context.set(new_context)+ def info(self, event: str, **kwargs: Any) -> None:+ self.records.append(("info", event, kwargs))- result_context = skyvern_context.current()- # BUG: loop_internal_state is None- assert result_context.loop_internal_state is None+ def debug(self, event: str, **kwargs: Any) -> None:+ self.records.append(("debug", event, kwargs))+[email protected](autouse=True)+def reset_context() -> None:+ skyvern_context.reset()+ yieldskyvern_context.reset()-def test_loop_internal_state_preserved_after_fix():- """After fix: new context preserves loop_internal_state from old context."""+def test_scoped_child_context_preserves_parent_loop_internal_state():+ """Nested child scopes should not clobber the parent's loop state."""original_state = {"downloaded_file_signatures_before_iteration": [("a.pdf", "abc", "https://files/a.pdf")]}original_context = SkyvernContext(organization_id="org_1",@@ -39,19 +37,186 @@ def test_loop_internal_state_preserved_after_fix():)skyvern_context.set(original_context)- # Simulate the FIXED TaskV2Block.finally- context = skyvern_context.current()- loop_state = context.loop_internal_state if context else None- new_context = SkyvernContext(- organization_id="org_1",- workflow_run_id="wr_1",- run_id=context.run_id,- loop_internal_state=loop_state,- )- skyvern_context.set(new_context)+ with skyvern_context.scoped(+ SkyvernContext(+ organization_id="org_child",+ workflow_run_id="wr_child",+ workflow_permanent_id="wfp_child",+ task_v2_id="tsk_v2_child",+ run_id="wr_1",+ )+ ):+ current_context = skyvern_context.ensure_context()+ assert current_context.workflow_run_id == "wr_child"+ assert current_context.loop_internal_state is Noneresult_context = skyvern_context.current()assert result_context.loop_internal_state is not Noneassert result_context.loop_internal_state == original_state++def test_scoped_child_context_restores_parent_and_flushes_child_summary(monkeypatch: pytest.MonkeyPatch) -> None:+ logger = CaptureLogger()+ monkeypatch.setattr(skyvern_context, "LOG", logger)++ parent_context = SkyvernContext(+ organization_id="org_parent",+ workflow_run_id="wr_parent",+ workflow_permanent_id="wfp_parent",+ step_id="step_parent",+ script_id="script_parent",+ run_id="wr_parent",+ )+ skyvern_context.set(parent_context)++ child_context = SkyvernContext(+ organization_id="org_child",+ workflow_run_id="wr_child",+ workflow_permanent_id="wfp_child",+ task_v2_id="tsk_v2_child",+ run_id="wr_parent",+ )++ with skyvern_context.scoped(child_context):+ current_context = skyvern_context.ensure_context()+ assert current_context.workflow_run_id == "wr_child"+ assert current_context.step_id is None+ assert current_context.script_id is None++ providers_module.record_feature_flag_resolution(+ feature_name="TEST_FLAG",+ resolution_kind="enabled",+ resolved_value=True,+ )++ restored_context = skyvern_context.ensure_context()+ assert restored_context is parent_context+ assert restored_context.workflow_run_id == "wr_parent"+ assert restored_context.workflow_permanent_id == "wfp_parent"+ assert restored_context.step_id == "step_parent"+ assert restored_context.script_id == "script_parent"++ assert logger.records == [+ (+ "info",+ "workflow_feature_flags",+ {+ "organization_id": "org_child",+ "workflow_run_id": "wr_child",+ "workflow_permanent_id": "wfp_child",+ "task_v2_id": "tsk_v2_child",+ "feature_resolutions": {"TEST_FLAG": True},+ "service_name": settings.OTEL_SERVICE_NAME,+ },+ )+ ]+++def test_scoped_child_summary_does_not_fragment_parent_summary(monkeypatch: pytest.MonkeyPatch) -> None:+ logger = CaptureLogger()+ monkeypatch.setattr(skyvern_context, "LOG", logger)++ parent_context = SkyvernContext(+ organization_id="org_parent",+ workflow_run_id="wr_parent",+ workflow_permanent_id="wfp_parent",+ run_id="wr_parent",+ )+ skyvern_context.set(parent_context)+ providers_module.record_feature_flag_resolution(+ feature_name="PARENT_BEFORE",+ resolution_kind="enabled",+ resolved_value=True,+ )++ with skyvern_context.scoped(+ SkyvernContext(+ organization_id="org_child",+ workflow_run_id="wr_child",+ workflow_permanent_id="wfp_child",+ task_v2_id="tsk_v2_child",+ run_id="wr_parent",+ )+ ):+ providers_module.record_feature_flag_resolution(+ feature_name="CHILD_FLAG",+ resolution_kind="enabled",+ resolved_value=False,+ )++ providers_module.record_feature_flag_resolution(+ feature_name="PARENT_AFTER",+ resolution_kind="enabled",+ resolved_value=False,+ )skyvern_context.reset()++ assert logger.records == [+ (+ "info",+ "workflow_feature_flags",+ {+ "organization_id": "org_child",+ "workflow_run_id": "wr_child",+ "workflow_permanent_id": "wfp_child",+ "task_v2_id": "tsk_v2_child",+ "feature_resolutions": {"CHILD_FLAG": False},+ "service_name": settings.OTEL_SERVICE_NAME,+ },+ ),+ (+ "info",+ "workflow_feature_flags",+ {+ "organization_id": "org_parent",+ "workflow_run_id": "wr_parent",+ "workflow_permanent_id": "wfp_parent",+ "feature_resolutions": {+ "PARENT_AFTER": False,+ "PARENT_BEFORE": True,+ },+ "service_name": settings.OTEL_SERVICE_NAME,+ },+ ),+ ]+++def test_replace_flushes_existing_context_before_overwrite(monkeypatch: pytest.MonkeyPatch) -> None:+ logger = CaptureLogger()+ monkeypatch.setattr(skyvern_context, "LOG", logger)++ original_context = SkyvernContext(+ organization_id="org_original",+ workflow_run_id="wr_original",+ workflow_permanent_id="wfp_original",+ run_id="wr_original",+ )+ skyvern_context.set(original_context)+ providers_module.record_feature_flag_resolution(+ feature_name="ORIGINAL_FLAG",+ resolution_kind="value",+ resolved_value="variant-a",+ )++ replacement_context = SkyvernContext(+ organization_id="org_replacement",+ workflow_run_id="wr_replacement",+ workflow_permanent_id="wfp_replacement",+ run_id="wr_replacement",+ )+ skyvern_context.replace(replacement_context)++ assert logger.records == [+ (+ "info",+ "workflow_feature_flags",+ {+ "organization_id": "org_original",+ "workflow_run_id": "wr_original",+ "workflow_permanent_id": "wfp_original",+ "feature_resolutions": {"ORIGINAL_FLAG": "variant-a"},+ "service_name": settings.OTEL_SERVICE_NAME,+ },+ )+ ]+ assert skyvern_context.current() is replacement_contextdiff --git a/tests/unit/test_taskv2_block_downloaded_files.py b/tests/unit/test_taskv2_block_downloaded_files.pyindex 803b24b7a..e0a7063d9 100644--- a/tests/unit/test_taskv2_block_downloaded_files.py+++ b/tests/unit/test_taskv2_block_downloaded_files.py@@ -90,43 +90,38 @@ def test_taskv2_output_empty_when_no_new_downloads_in_iteration() -> None:assert task_v2_output["downloaded_file_urls"] == []-def test_taskv2_context_loop_state_available_after_task_execution() -> None:- """Verify that loop_internal_state on the context survives TaskV2Block's context reset- and is available for the downloaded_files filtering step."""+def test_taskv2_context_loop_state_available_after_nested_task_execution() -> None:+ """Verify loop_internal_state survives nested task execution and remains available for filtering."""loop_state = {"downloaded_file_signatures_before_iteration": [["a.pdf", "abc", "https://files/a.pdf"],],}- # Set context with loop state (simulating what ForLoopBlock does before executing child)- skyvern_context.set(- SkyvernContext(- organization_id="org_1",- workflow_run_id="wr_1",- run_id="wr_1",- loop_internal_state=loop_state,- )+ parent_context = SkyvernContext(+ organization_id="org_1",+ workflow_run_id="wr_1",+ run_id="wr_1",+ loop_internal_state=loop_state,)+ skyvern_context.set(parent_context)- # Simulate TaskV2Block's finally block preserving loop_internal_state- context = skyvern_context.current()- preserved_loop_state = context.loop_internal_state if context else None- skyvern_context.set(+ with skyvern_context.scoped(SkyvernContext(organization_id="org_1",- workflow_run_id="wr_1",- run_id=context.run_id if context else "wr_1",- loop_internal_state=preserved_loop_state,+ workflow_run_id="wr_child",+ workflow_permanent_id="wfp_child",+ task_v2_id="tsk_v2_child",+ run_id="wr_1",)- )+ ):+ pass- # Now read context the way the new downloaded_files code doescurrent_context = skyvern_context.current()assert current_context is not None+ assert current_context is parent_contextassert current_context.loop_internal_state == loop_state- # Filter should work correctlyall_files = [_file("https://files/a.pdf?sig=old", "a.pdf", "abc"),_file("https://files/b.pdf?sig=new", "b.pdf", "def"),diff --git a/tests/unit/workflow/test_workflow_trigger_block.py b/tests/unit/workflow/test_workflow_trigger_block.pyindex b731d3263..662425a3c 100644--- a/tests/unit/workflow/test_workflow_trigger_block.py+++ b/tests/unit/workflow/test_workflow_trigger_block.py@@ -8,6 +8,9 @@ from unittest.mock import AsyncMock, MagicMock, patchimport pytest+from skyvern.forge.sdk.core import skyvern_context+from skyvern.forge.sdk.core.skyvern_context import SkyvernContext+from skyvern.forge.sdk.experimentation import providers as providers_modulefrom skyvern.forge.sdk.workflow.exceptions import InvalidWorkflowDefinitionfrom skyvern.forge.sdk.workflow.models.block import (_JSON_TYPE_MARKER,@@ -15,9 +18,21 @@ from skyvern.forge.sdk.workflow.models.block import (WorkflowTriggerBlock,)from skyvern.forge.sdk.workflow.models.parameter import OutputParameter+from skyvern.forge.sdk.workflow.models.workflow import WorkflowRunStatusfrom skyvern.schemas.workflows import BlockType+class CaptureLogger:+ def __init__(self) -> None:+ self.records: list[tuple[str, str, dict[str, Any]]] = []++ def info(self, event: str, **kwargs: Any) -> None:+ self.records.append(("info", event, kwargs))++ def debug(self, event: str, **kwargs: Any) -> None:+ self.records.append(("debug", event, kwargs))++def _make_output_parameter() -> OutputParameter:now = datetime.now(timezone.utc)return OutputParameter(@@ -199,6 +214,101 @@ class TestCheckTriggerDepth:assert depth == 0+async def test_sync_trigger_preserves_parent_feature_flag_summary(monkeypatch: pytest.MonkeyPatch) -> None:+ logger = CaptureLogger()+ monkeypatch.setattr(skyvern_context, "LOG", logger)++ block = _make_block(use_parent_browser_session=True)+ parent_context = SkyvernContext(+ organization_id="org_parent",+ workflow_run_id="wr_parent",+ workflow_permanent_id="wfp_parent",+ root_workflow_run_id="wr_parent",+ run_id="wr_parent",+ )+ skyvern_context.set(parent_context)+ providers_module.record_feature_flag_resolution(+ feature_name="PARENT_BEFORE",+ resolution_kind="enabled",+ resolved_value=True,+ )++ organization = MagicMock()+ organization.organization_id = "org_parent"+ organization.organization_name = "Org Parent"++ async def _setup_workflow_run(**_: Any) -> Any:+ skyvern_context.replace(+ SkyvernContext(+ organization_id="org_parent",+ organization_name="Org Parent",+ workflow_run_id="wr_child",+ workflow_permanent_id="wfp_child",+ root_workflow_run_id="wr_parent",+ run_id="wr_parent",+ )+ )+ workflow_run = MagicMock()+ workflow_run.workflow_run_id = "wr_child"+ workflow_run.workflow_permanent_id = "wfp_child"+ return workflow_run++ async def _execute_workflow(**_: Any) -> Any:+ providers_module.record_feature_flag_resolution(+ feature_name="CHILD_FLAG",+ resolution_kind="enabled",+ resolved_value=False,+ )+ workflow_run = MagicMock()+ workflow_run.status = WorkflowRunStatus.completed+ workflow_run.failure_reason = None+ workflow_run.workflow_id = "wf_child"+ return workflow_run++ monkeypatch.setattr(WorkflowTriggerBlock, "get_workflow_run_context", lambda self, workflow_run_id: MagicMock())+ monkeypatch.setattr(WorkflowTriggerBlock, "format_potential_template_parameters", lambda self, ctx: None)+ monkeypatch.setattr(WorkflowTriggerBlock, "_check_trigger_depth", AsyncMock(return_value=0))+ monkeypatch.setattr(WorkflowTriggerBlock, "record_output_parameter_value", AsyncMock())+ monkeypatch.setattr(WorkflowTriggerBlock, "build_block_result", AsyncMock(return_value=MagicMock()))++ try:+ with patch("skyvern.forge.sdk.workflow.models.block.app") as mock_app:+ mock_app.DATABASE.organizations.get_organization = AsyncMock(return_value=organization)+ mock_app.WORKFLOW_SERVICE.setup_workflow_run = AsyncMock(side_effect=_setup_workflow_run)+ mock_app.WORKFLOW_SERVICE.execute_workflow = AsyncMock(side_effect=_execute_workflow)+ mock_app.WORKFLOW_SERVICE.get_output_parameter_workflow_run_output_parameter_tuples = AsyncMock(+ return_value=[]+ )++ await block.execute(+ workflow_run_id="wr_parent",+ workflow_run_block_id="wrb_parent",+ organization_id="org_parent",+ browser_session_id="pbs_parent",+ )++ assert skyvern_context.current() is parent_context++ providers_module.record_feature_flag_resolution(+ feature_name="PARENT_AFTER",+ resolution_kind="enabled",+ resolved_value=False,+ )+ finally:+ skyvern_context.reset()++ summary_records = [fields for _, event, fields in logger.records if event == "workflow_feature_flags"]+ assert len(summary_records) == 2+ assert summary_records[0]["workflow_run_id"] == "wr_child"+ assert summary_records[0]["feature_resolutions"] == {"CHILD_FLAG": False}+ assert summary_records[1]["workflow_run_id"] == "wr_parent"+ assert summary_records[1]["feature_resolutions"] == {+ "PARENT_AFTER": False,+ "PARENT_BEFORE": True,+ }++class TestBlockMetadata:"""Verify basic block properties."""