Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
feat(SKY-9737): add PATCH /browser_profiles/{id} endpoint for rename (#5960)
Celal Zamanoğlu2bfb42f100e9
diff --git a/skyvern-frontend/src/util/env.ts b/skyvern-frontend/src/util/env.tsindex 346612b01..1e85996e3 100644--- a/skyvern-frontend/src/util/env.ts+++ b/skyvern-frontend/src/util/env.ts@@ -78,9 +78,14 @@ function getRuntimeApiKey(): string | null {const persisted = readPersistedApiKey();const candidate = persisted ?? buildTimeApiKey;- // Treat YOUR_API_KEY as missing. We may inherit this from .env.example- // in some cases of misconfiguration.- runtimeApiKey = candidate === "YOUR_API_KEY" ? null : candidate;+ // Treat placeholder / example values as missing so they are never sent+ // as real credentials (e.g. from .env.example or an un-replaced Docker placeholder).+ const PLACEHOLDER_VALUES = [+ "YOUR_API_KEY",+ "__SKYVERN_API_KEY_PLACEHOLDER__",+ ];+ runtimeApiKey =+ candidate && PLACEHOLDER_VALUES.includes(candidate) ? null : candidate;return runtimeApiKey;}diff --git a/skyvern/cli/mcp_tools/browser.py b/skyvern/cli/mcp_tools/browser.pyindex 9bce42a42..b2b28f2b4 100644--- a/skyvern/cli/mcp_tools/browser.py+++ b/skyvern/cli/mcp_tools/browser.py@@ -1451,13 +1451,29 @@ async def skyvern_run_task(timeout=timeout_seconds,)timer.mark("sdk")+ except asyncio.TimeoutError:+ return make_result(+ "skyvern_run_task",+ ok=False,+ browser_context=ctx,+ timing_ms=timer.timing_ms,+ error=make_error(+ ErrorCode.TIMEOUT,+ f"Task did not reach a final status within {timeout_seconds}s. It may still be running.",+ "Check the run in the Skyvern dashboard, or retry with a larger timeout_seconds.",+ ),+ )except Exception as e:return make_result("skyvern_run_task",ok=False,browser_context=ctx,timing_ms=timer.timing_ms,- error=make_error(ErrorCode.SDK_ERROR, str(e), "Check the prompt, URL, and timeout settings"),+ error=make_error(+ ErrorCode.SDK_ERROR,+ str(e) or type(e).__name__,+ "Check the prompt, URL, and timeout settings",+ ),)return make_result(@@ -1596,6 +1612,20 @@ async def skyvern_login(**_common_kwargs,)timer.mark("sdk")+ except asyncio.TimeoutError:+ return make_result(+ "skyvern_login",+ ok=False,+ browser_context=ctx,+ timing_ms=timer.timing_ms,+ error=make_error(+ ErrorCode.TIMEOUT,+ f"Login workflow did not reach a final status within {timeout_seconds}s. "+ "The login may still be running or may have already completed.",+ "Check the run in the Skyvern dashboard, or retry with a larger timeout_seconds. "+ "Use skyvern_observe to confirm the post-login page state.",+ ),+ )except Exception as e:return make_result("skyvern_login",@@ -1604,7 +1634,7 @@ async def skyvern_login(timing_ms=timer.timing_ms,error=make_error(ErrorCode.SDK_ERROR,- str(e),+ str(e) or type(e).__name__,"Check credential_type and required fields for your credential provider",),)diff --git a/skyvern/forge/sdk/copilot/agent.py b/skyvern/forge/sdk/copilot/agent.pyindex 4343a2f4c..9996c9805 100644--- a/skyvern/forge/sdk/copilot/agent.py+++ b/skyvern/forge/sdk/copilot/agent.py@@ -9,10 +9,14 @@ import asyncioimport contextlibimport jsonimport re+from collections.abc import Iterator+from dataclasses import dataclassfrom datetime import datetime, timezonefrom pathlib import Pathfrom typing import TYPE_CHECKING, Any+from opentelemetry import trace as otel_trace+if TYPE_CHECKING:from agents.result import RunResultStreaming@@ -51,7 +55,9 @@ from skyvern.forge.sdk.copilot.tracing_setup import _copilot_model_name, ensure_from skyvern.forge.sdk.schemas.persistent_browser_sessions import is_final_statusfrom skyvern.forge.sdk.schemas.workflow_copilot import (WorkflowCopilotChatHistoryMessage,+ WorkflowCopilotChatSender,)+from skyvern.forge.sdk.trace import apply_context_attrsfrom skyvern.forge.sdk.workflow.exceptions import BaseWorkflowHTTPExceptionfrom skyvern.utils.strings import escape_code_fencesfrom skyvern.utils.yaml_loader import safe_load_no_dates@@ -62,6 +68,50 @@ WORKFLOW_KNOWLEDGE_BASE_PATH = (Path(__file__).resolve().parents[2] / "prompts" / "skyvern" / "workflow_knowledge_base.txt")+_COPILOT_TURN_SPAN_NAME = "copilot.turn"+_USER_MESSAGE_PREVIEW_MAX_CHARS = 40+++def _build_user_message_preview(message: str) -> str:+ flattened = (message or "").replace("\r", " ").replace("\n", " ").strip()+ redacted = redact_raw_secrets_for_prompt(flattened)+ if len(redacted) <= _USER_MESSAGE_PREVIEW_MAX_CHARS:+ return redacted+ return redacted[: _USER_MESSAGE_PREVIEW_MAX_CHARS - 1] + "…"+++def _derive_turn_index(+ chat_history: list[WorkflowCopilotChatHistoryMessage],+ explicit: int | None,+) -> int:+ # `chat_history` may be a truncated tail of the full message log, so this+ # fallback can undercount long sessions; prefer the explicit count.+ if explicit is not None:+ return explicit+ return sum(1 for m in chat_history if m.sender == WorkflowCopilotChatSender.USER) + 1+++def _copilot_turn_span(+ *,+ chat_request: WorkflowCopilotChatRequest,+ chat_history: list[WorkflowCopilotChatHistoryMessage],+ turn_index: int | None,+) -> Iterator[None]:+ tracer = otel_trace.get_tracer("skyvern")+ with tracer.start_as_current_span(_COPILOT_TURN_SPAN_NAME) as span:+ span.set_attribute("skyvern.span.role", "wrapper")+ span.set_attribute("copilot.turn_index", _derive_turn_index(chat_history, turn_index))+ preview = _build_user_message_preview(chat_request.message)+ if preview:+ span.set_attribute("copilot.user_message_preview", preview)+ if chat_request.workflow_copilot_chat_id:+ span.set_attribute("copilot.session_id", chat_request.workflow_copilot_chat_id)+ if chat_request.workflow_permanent_id:+ span.set_attribute("workflow_permanent_id", chat_request.workflow_permanent_id)+ apply_context_attrs(span)+ yield+def _resolve_request_policy_handler(fallback_handler: Any) -> Any:with contextlib.suppress(RuntimeError, AttributeError):@@ -69,6 +119,17 @@ def _resolve_request_policy_handler(fallback_handler: Any) -> Any:return fallback_handler+@dataclass(frozen=True)+class RequestPolicyGuardrailInputs:+ user_message: str+ workflow_yaml: str+ chat_history_text: str+ global_llm_context: str+ organization_id: str+ handler: Any+ previous_user_message: str | None = None++_BLOCK_GOAL_CONTEXT_PREAMBLE = ("Interpret the latest user message in the context of the prior conversation. If it is a correction, ""refinement, or continuation, preserve the established website, actions, filters, and output requirements "@@ -174,6 +235,43 @@ def _build_block_goal_main_goal()+def _request_policy_agent_inputs(+ policy: RequestPolicy,+ *,+ user_message: str,+ chat_history_text: str,+ previous_user_message: str | None,+) -> tuple[str, str]:+ if policy.testing_intent == "skip_test" and len(user_message) < 160 and previous_user_message:+ return (+ f"{user_message}\n\nDraft the workflow requested earlier:\n"+ f"{redact_raw_secrets_for_prompt(previous_user_message)}",+ "",+ )+ return user_message, chat_history_text+++def _store_request_policy_on_context(+ ctx: CopilotContext,+ policy: RequestPolicy,+ policy_inputs: RequestPolicyGuardrailInputs,+) -> None:+ agent_user_message, policy_chat_history_text = _request_policy_agent_inputs(+ policy,+ user_message=policy_inputs.user_message,+ chat_history_text=policy_inputs.chat_history_text,+ previous_user_message=policy_inputs.previous_user_message,+ )+ ctx.request_policy = policy+ ctx.allow_untested_workflow_draft = policy.testing_intent == "skip_test"+ ctx.user_message = agent_user_message+ ctx.block_goal_main_goal = _build_block_goal_main_goal(+ user_message=agent_user_message,+ chat_history_text=policy_chat_history_text,+ global_llm_context=policy_inputs.global_llm_context,+ )++def _build_system_prompt(tool_usage_guide: str,config: CopilotConfig | None = None,@@ -191,6 +289,26 @@ def _build_system_prompt()+def _build_dynamic_system_prompt(tool_usage_guide: str, config: CopilotConfig) -> Any:+ base_system_prompt = _build_system_prompt(tool_usage_guide=tool_usage_guide, config=config)++ def instructions(context: Any, _agent: Any) -> str:+ ctx = getattr(context, "context", None)+ policy = getattr(ctx, "request_policy", None)+ if not isinstance(policy, RequestPolicy):+ return base_system_prompt+ policy_summary = escape_code_fences(redact_raw_secrets_for_prompt(policy.prompt_summary()))+ return (+ base_system_prompt+ + "\n\nREQUEST POLICY:\n```yaml\n"+ + policy_summary+ + "\n```\nFollow this policy. If `allow_run_blocks` is false, do not call block-running tools. "+ + "If `resolved_credentials` are present, use those `credential_id` values."+ )++ return instructions++def _build_user_context(workflow_yaml: str,chat_history_text: str,@@ -690,6 +808,14 @@ def _translate_to_agent_result(# cannot test. The generic rewrite would replace it with a vague# "Could you share more context", so skip it for ASK_QUESTION (and for# salvaged replies, which already describe the verified prefix).+ if _should_surface_untested_draft_despite_question(ctx, resp_type):+ LOG.info(+ "Converting copilot clarification into untested draft proposal",+ workflow_permanent_id=ctx.workflow_permanent_id,+ block_count=ctx.last_update_block_count,+ )+ resp_type = "REPLY"+if resp_type == "ASK_QUESTION":user_response = _shape_ask_question_response(str(user_response), ctx)elif not salvaged_reply:@@ -909,6 +1035,19 @@ def _agent_output_to_text(agent_output: Any) -> str:return str(agent_output)+def _should_surface_untested_draft_despite_question(ctx: CopilotContext, response_type: str) -> bool:+ request_policy = ctx.request_policy if isinstance(ctx.request_policy, RequestPolicy) else None+ return (+ response_type == "ASK_QUESTION"+ and request_policy is not None+ and request_policy.testing_intent == "skip_test"+ and ctx.allow_untested_workflow_draft+ and ctx.last_workflow is not None+ and bool(ctx.last_workflow_yaml)+ and ctx.last_test_ok is None+ )++def _evaluate_copilot_final_output_policy(ctx: CopilotContext,agent_output: Any,@@ -926,26 +1065,33 @@ def _evaluate_copilot_final_output_policy(workflow_yaml = ctx.last_workflow_yamlworkflow_attempted = ctx.last_update_block_count is not None or ctx.last_test_ok is not None+ surface_untested_draft = _should_surface_untested_draft_despite_question(ctx, response_type)+ policy_response_type = "REPLY" if surface_untested_draft else response_type+ policy_user_response = str(action_data.get("user_response") or text)+ if surface_untested_draft:+ policy_user_response = _rewrite_failed_test_response(policy_user_response, ctx)updated_workflow_for_kind = (ctx.last_workflow if ctx.last_workflow is not None else WORKFLOW_PRESENT_SENTINEL if workflow_yaml else None)- output_kind = derive_output_kind(- response_type=response_type,- request_policy=ctx.request_policy,- updated_workflow=updated_workflow_for_kind,- workflow_was_persisted=ctx.workflow_persisted,- workflow_attempted=workflow_attempted,- # SDK output guardrails enforce hard blocks only; the final translation- # path handles soft unvalidated-proposal affordance rewrites.- unvalidated=False,+ output_kind = (+ CopilotOutputKind.WORKFLOW_DRAFT_PROPOSAL+ if surface_untested_draft+ else derive_output_kind(+ response_type=response_type,+ request_policy=ctx.request_policy,+ updated_workflow=updated_workflow_for_kind,+ workflow_was_persisted=ctx.workflow_persisted,+ workflow_attempted=workflow_attempted,+ unvalidated=False,+ ))verdict = evaluate_output_policy(request_policy=ctx.request_policy,- response_type=response_type,- user_response=str(action_data.get("user_response") or text),+ response_type=policy_response_type,+ user_response=policy_user_response,global_llm_context=action_data.get("global_llm_context"),workflow_yaml=workflow_yaml,- tool_arguments={"final_output": text},+ tool_arguments=None,has_workflow_proposal=bool(workflow_yaml or ctx.last_workflow is not None),workflow_was_persisted=ctx.workflow_persisted,workflow_attempted=workflow_attempted,@@ -957,15 +1103,30 @@ def _evaluate_copilot_final_output_policy(def _build_copilot_input_guardrails(InputGuardrailCls: Any,GuardrailFunctionOutputCls: Any,+ *,+ policy_inputs: RequestPolicyGuardrailInputs | None = None,) -> list[Any]:# Guardrail classes are injected after importing the optional Agents SDK in# run_copilot_agent, keeping module import safe when the SDK is unavailable.- def request_policy_guardrail(context: Any, _agent: Any, _input: Any) -> Any:- policy = getattr(getattr(context, "context", None), "request_policy", None)+ async def request_policy_guardrail(context: Any, _agent: Any, _input: Any) -> Any:+ ctx = getattr(context, "context", None)+ policy = getattr(ctx, "request_policy", None)+ if not isinstance(policy, RequestPolicy) and policy_inputs is not None:+ policy = await build_request_policy(+ user_message=policy_inputs.user_message,+ workflow_yaml=policy_inputs.workflow_yaml,+ chat_history=policy_inputs.chat_history_text,+ global_llm_context=policy_inputs.global_llm_context,+ organization_id=policy_inputs.organization_id,+ handler=policy_inputs.handler,+ )+ if isinstance(ctx, CopilotContext):+ _store_request_policy_on_context(ctx, policy, policy_inputs)blocked = isinstance(policy, RequestPolicy) and policy.user_response_policy == "ask_clarification"if isinstance(policy, RequestPolicy):trace_data = {"surface": "agent_input",+ "policy_present": True,"blocked": blocked,"user_response_policy": policy.user_response_policy,**policy.to_trace_data(),@@ -1072,37 +1233,129 @@ async def run_copilot_agent(api_key: str | None = None,security_rules: str = "",config: CopilotConfig | None = None,+ turn_index: int | None = None,+) -> AgentResult:+ # Initialize tracing before opening the turn span so Logfire's OTel provider+ # is installed; otherwise the very first turn lands the parent span on+ # OTel's no-op ProxyTracer when running locally with COPILOT_TRACING_ENABLED.+ ensure_tracing_initialized()+ with _copilot_turn_span(+ chat_request=chat_request,+ chat_history=chat_history,+ turn_index=turn_index,+ ):+ return await _run_copilot_turn_impl(+ stream=stream,+ organization_id=organization_id,+ chat_request=chat_request,+ chat_history=chat_history,+ global_llm_context=global_llm_context,+ debug_run_info_text=debug_run_info_text,+ llm_api_handler=llm_api_handler,+ api_key=api_key,+ security_rules=security_rules,+ config=config,+ )+++async def _run_copilot_turn_impl(+ *,+ stream: EventSourceStream,+ organization_id: str,+ chat_request: WorkflowCopilotChatRequest,+ chat_history: list[WorkflowCopilotChatHistoryMessage],+ global_llm_context: str | None,+ debug_run_info_text: str,+ llm_api_handler: LLMAPIHandler | None,+ api_key: str | None,+ security_rules: str,+ config: CopilotConfig | None,) -> AgentResult:copilot_config = config or CopilotConfig(security_rules=security_rules)chat_history_text = _format_chat_history(chat_history)safe_chat_history_text = redact_raw_secrets_for_prompt(chat_history_text)safe_workflow_yaml = redact_raw_secrets_for_prompt(chat_request.workflow_yaml or "")safe_global_llm_context = redact_raw_secrets_for_prompt(global_llm_context or "")- request_policy = await build_request_policy(+ previous_user_messages = [msg.content for msg in chat_history if msg.sender == "user"]+ previous_user_message = previous_user_messages[-1] if previous_user_messages else None++ try:+ from agents import Agent, GuardrailFunctionOutput, InputGuardrail, OutputGuardrail, trace+ from agents.exceptions import (+ InputGuardrailTripwireTriggered,+ MaxTurnsExceeded,+ OutputGuardrailTripwireTriggered,+ )+ from agents.mcp import MCPServerManager+ from agents.run_context import RunContextWrapper+ except ModuleNotFoundError as e:+ if e.name == "agents":+ LOG.error(+ "OpenAI Agents SDK dependency missing",+ error=str(e),+ workflow_permanent_id=chat_request.workflow_permanent_id,+ )+ return AgentResult(+ user_response=(+ "Copilot backend is missing the OpenAI Agents SDK dependency. "+ "Rebuild or redeploy the backend image so `openai-agents` is installed."+ ),+ updated_workflow=None,+ global_llm_context=global_llm_context,+ workflow_yaml=chat_request.workflow_yaml or None,+ )+ raise++ ctx = CopilotContext(+ organization_id=organization_id,+ workflow_id=chat_request.workflow_id,+ workflow_permanent_id=chat_request.workflow_permanent_id,+ workflow_yaml=chat_request.workflow_yaml or "",+ browser_session_id=None,+ stream=stream,+ api_key=api_key,+ user_message=chat_request.message,+ workflow_copilot_chat_id=chat_request.workflow_copilot_chat_id,+ )+ policy_inputs = RequestPolicyGuardrailInputs(user_message=chat_request.message,workflow_yaml=safe_workflow_yaml,- chat_history=safe_chat_history_text,+ chat_history_text=safe_chat_history_text,global_llm_context=safe_global_llm_context,organization_id=organization_id,handler=_resolve_request_policy_handler(llm_api_handler),+ previous_user_message=previous_user_message,+ )+ request_policy_guardrails = _build_copilot_input_guardrails(+ InputGuardrail,+ GuardrailFunctionOutput,+ policy_inputs=policy_inputs,+ )+ # Run the request-policy guardrail as the authoritative input gate before+ # feasibility checks, browser/session setup, model execution, or tool calls.+ # Do not also attach it to the main Agent; the SDK would invoke it again and+ # duplicate policy telemetry.+ request_policy_guardrail_result = await request_policy_guardrails[0].run(+ Agent(name="workflow-copilot-request-policy", instructions=""),+ chat_request.message,+ RunContextWrapper(context=ctx),)- if request_policy.user_response_policy == "ask_clarification":+ request_policy = ctx.request_policy if isinstance(ctx.request_policy, RequestPolicy) else None+ if request_policy is not None and request_policy_guardrail_result.output.tripwire_triggered:return _build_request_policy_clarification_result(request_policy,prior_global_llm_context=global_llm_context,prior_workflow_yaml=chat_request.workflow_yaml,)+ if request_policy is None:+ raise RuntimeError("Copilot request-policy input guardrail did not populate request policy")- allow_untested_workflow_draft = request_policy.testing_intent == "skip_test"- agent_user_message = chat_request.message- if allow_untested_workflow_draft and len(agent_user_message) < 160:- previous_user_messages = [msg.content for msg in chat_history if msg.sender == "user"]- if previous_user_messages:- agent_user_message = (- f"{agent_user_message}\n\nDraft the workflow requested earlier:\n"- f"{redact_raw_secrets_for_prompt(previous_user_messages[-1])}"- )- safe_chat_history_text = ""+ agent_user_message, safe_chat_history_text = _request_policy_agent_inputs(+ request_policy,+ user_message=chat_request.message,+ chat_history_text=safe_chat_history_text,+ previous_user_message=previous_user_message,+ )# Preflight feasibility classifier — fires on every turn so mid-session pivots# to impossible targets are caught the same as first-turn structural mismatches.@@ -1124,32 +1377,6 @@ async def run_copilot_agent(prior_workflow_yaml=chat_request.workflow_yaml,)- try:- from agents import Agent, GuardrailFunctionOutput, InputGuardrail, OutputGuardrail, trace- from agents.exceptions import (- InputGuardrailTripwireTriggered,- MaxTurnsExceeded,- OutputGuardrailTripwireTriggered,- )- from agents.mcp import MCPServerManager- except ModuleNotFoundError as e:- if e.name == "agents":- LOG.error(- "OpenAI Agents SDK dependency missing",- error=str(e),- workflow_permanent_id=chat_request.workflow_permanent_id,- )- return AgentResult(- user_response=(- "Copilot backend is missing the OpenAI Agents SDK dependency. "- "Rebuild or redeploy the backend image so `openai-agents` is installed."- ),- updated_workflow=None,- global_llm_context=global_llm_context,- workflow_yaml=chat_request.workflow_yaml or None,- )- raise-from skyvern.cli.mcp_tools import mcp as skyvern_mcpfrom skyvern.forge.sdk.copilot.enforcement import (CopilotNonRetriableNavError,@@ -1167,33 +1394,13 @@ async def run_copilot_agent()validated_browser_session_id = await _resolve_live_browser_session_id(chat_request, organization_id)-- ctx = CopilotContext(- organization_id=organization_id,- workflow_id=chat_request.workflow_id,- workflow_permanent_id=chat_request.workflow_permanent_id,- workflow_yaml=chat_request.workflow_yaml or "",- browser_session_id=validated_browser_session_id,- stream=stream,- api_key=api_key,- user_message=agent_user_message,- allow_untested_workflow_draft=allow_untested_workflow_draft,- request_policy=request_policy,- block_goal_main_goal=_build_block_goal_main_goal(- user_message=agent_user_message,- chat_history_text=safe_chat_history_text,- global_llm_context=safe_global_llm_context,- ),- workflow_copilot_chat_id=chat_request.workflow_copilot_chat_id,- )+ ctx.browser_session_id = validated_browser_session_idmodel_name, run_config, llm_key, supports_vision = resolve_model_config(llm_api_handler,copilot_config=copilot_config,)ctx.supports_vision = supports_vision- ensure_tracing_initialized()- input_guardrails = _build_copilot_input_guardrails(InputGuardrail, GuardrailFunctionOutput)output_guardrails = _build_copilot_output_guardrails(OutputGuardrail, GuardrailFunctionOutput)alias_map = get_skyvern_mcp_alias_map()@@ -1203,7 +1410,7 @@ async def run_copilot_agent(tool_info.extend((name, overlay.description or "") for name, overlay in overlays.items())tool_usage_guide = _build_tool_usage_guide(tool_info)- system_prompt = _build_system_prompt(+ system_prompt = _build_dynamic_system_prompt(tool_usage_guide=tool_usage_guide,config=copilot_config,)@@ -1214,7 +1421,6 @@ async def run_copilot_agent(global_llm_context=safe_global_llm_context,debug_run_info_text=redact_raw_secrets_for_prompt(debug_run_info_text),user_message=agent_user_message,- request_policy_summary=request_policy.prompt_summary(),)LOG.info(@@ -1258,7 +1464,6 @@ async def run_copilot_agent(tools=list(NATIVE_TOOLS),mcp_servers=[mcp_server],model=attempt_model_name,- input_guardrails=input_guardrails,output_guardrails=output_guardrails,)session = create_copilot_session(chat_id)diff --git a/skyvern/forge/sdk/copilot/request_policy.py b/skyvern/forge/sdk/copilot/request_policy.pyindex 17b78c844..591d1dd88 100644--- a/skyvern/forge/sdk/copilot/request_policy.py+++ b/skyvern/forge/sdk/copilot/request_policy.py@@ -90,7 +90,7 @@ class RequestPolicy:"allow_run_blocks": self.allow_run_blocks,"allow_missing_credentials_in_draft": self.allow_missing_credentials_in_draft,"resolved_credential_count": len(self.resolved_credentials),- "completion_contract": self.completion_contract,+ "has_completion_contract": bool(self.completion_contract),"raw_secret_detected": self.raw_secret_detected,}diff --git a/skyvern/forge/sdk/copilot/tracing_setup.py b/skyvern/forge/sdk/copilot/tracing_setup.pyindex dd6c86f2c..218c9d448 100644--- a/skyvern/forge/sdk/copilot/tracing_setup.py+++ b/skyvern/forge/sdk/copilot/tracing_setup.py@@ -100,7 +100,12 @@ def ensure_tracing_initialized() -> None:service_name="skyvern-copilot",environment=settings.ENV,)- logfire.instrument_openai_agents()+ try:+ logfire.instrument_openai_agents()+ except ImportError as exc:+ # `instrument_openai_agents` imports the optional `agents` SDK at call time;+ # self-hosted installs without it still need to boot under COPILOT_TRACING_ENABLED.+ LOG.warning("Copilot tracing requested but openai-agents SDK not installed", error=repr(exc))_patch_agent_span_attributes()# Logfire instruments via OpenTelemetry independently of the SDK's# built-in trace processors. Clear the default OpenAI exporter so itdiff --git a/skyvern/forge/sdk/db/repositories/browser_sessions.py b/skyvern/forge/sdk/db/repositories/browser_sessions.pyindex 738b82645..cbb17c59f 100644--- a/skyvern/forge/sdk/db/repositories/browser_sessions.py+++ b/skyvern/forge/sdk/db/repositories/browser_sessions.py@@ -103,6 +103,34 @@ class BrowserSessionsRepository(BaseRepository):browser_profile.deleted_at = datetime.now(timezone.utc)await session.commit()+ @db_operation("update_browser_profile")+ async def update_browser_profile(+ self,+ profile_id: str,+ organization_id: str,+ name: str | None = None,+ description: str | None = None,+ ) -> BrowserProfile:+ async with self.Session() as session:+ query = (+ select(BrowserProfileModel)+ .filter_by(browser_profile_id=profile_id)+ .filter_by(organization_id=organization_id)+ .filter(BrowserProfileModel.deleted_at.is_(None))+ )+ browser_profile = (await session.scalars(query)).first()+ if not browser_profile:+ raise BrowserProfileNotFound(profile_id=profile_id, organization_id=organization_id)++ if name is not None:+ browser_profile.name = name+ if description is not None:+ browser_profile.description = description++ await session.commit()+ await session.refresh(browser_profile)+ return BrowserProfile.model_validate(browser_profile)+@db_operation("get_active_persistent_browser_sessions")async def get_active_persistent_browser_sessions(self,diff --git a/skyvern/forge/sdk/db/repositories/scripts.py b/skyvern/forge/sdk/db/repositories/scripts.pyindex 527bfbc22..230a8c54f 100644--- a/skyvern/forge/sdk/db/repositories/scripts.py+++ b/skyvern/forge/sdk/db/repositories/scripts.py@@ -9,6 +9,7 @@ from sqlalchemy.dialects.postgresql import insertfrom skyvern.forge.sdk.db._error_handling import db_operationfrom skyvern.forge.sdk.db.base_repository import BaseRepositoryfrom skyvern.forge.sdk.db.exceptions import NotFoundError+from skyvern.forge.sdk.db.id import generate_script_block_id, generate_script_file_idfrom skyvern.forge.sdk.db.models import (ScriptBlockModel,ScriptBranchHitModel,@@ -230,25 +231,56 @@ class ScriptsRepository(BaseRepository):encoding: str = "utf-8",artifact_id: str | None = None,) -> ScriptFile:- """Create a script file."""+ """Create a script file. Idempotent on (script_revision_id, file_path)."""async with self.Session() as session:- script_file = ScriptFileModel(- script_revision_id=script_revision_id,- script_id=script_id,- organization_id=organization_id,- file_path=file_path,- file_name=file_name,- file_type=file_type,- content_hash=content_hash,- file_size=file_size,- mime_type=mime_type,- encoding=encoding,- artifact_id=artifact_id,+ now = datetime.now(timezone.utc)+ stmt = (+ insert(ScriptFileModel)+ .values(+ file_id=generate_script_file_id(),+ script_revision_id=script_revision_id,+ script_id=script_id,+ organization_id=organization_id,+ file_path=file_path,+ file_name=file_name,+ file_type=file_type,+ content_hash=content_hash,+ file_size=file_size,+ mime_type=mime_type,+ encoding=encoding,+ artifact_id=artifact_id,+ created_at=now,+ modified_at=now,+ )+ .on_conflict_do_nothing(constraint="unique_script_file_path")+ .returning(ScriptFileModel))- session.add(script_file)+ inserted = (await session.scalars(stmt)).first()+ if inserted is None:+ LOG.info(+ "create_script_file_idempotent_hit",+ script_revision_id=script_revision_id,+ script_id=script_id,+ organization_id=organization_id,+ file_path=file_path,+ )+ inserted = (+ await session.scalars(+ select(ScriptFileModel)+ .filter_by(script_revision_id=script_revision_id)+ .filter_by(file_path=file_path)+ .filter_by(organization_id=organization_id)+ )+ ).first()+ if inserted is None:+ raise RuntimeError(+ f"create_script_file: conflict row not owned by caller "+ f"organization_id={organization_id} "+ f"script_revision_id={script_revision_id} file_path={file_path}"+ )+ converted = convert_to_script_file(inserted)await session.commit()- await session.refresh(script_file)- return convert_to_script_file(script_file)+ return converted@db_operation("create_script_block")async def create_script_block(@@ -264,24 +296,56 @@ class ScriptsRepository(BaseRepository):input_fields: list[str] | None = None,requires_agent: bool = False,) -> ScriptBlock:- """Create a script block."""+ """Create a script block. Idempotent on (script_revision_id, script_block_label)."""async with self.Session() as session:- script_block = ScriptBlockModel(- script_revision_id=script_revision_id,- script_id=script_id,- organization_id=organization_id,- script_block_label=script_block_label,- script_file_id=script_file_id,- run_signature=run_signature,- workflow_run_id=workflow_run_id,- workflow_run_block_id=workflow_run_block_id,- input_fields=input_fields,- requires_agent=requires_agent,+ now = datetime.now(timezone.utc)+ stmt = (+ insert(ScriptBlockModel)+ .values(+ script_block_id=generate_script_block_id(),+ script_revision_id=script_revision_id,+ script_id=script_id,+ organization_id=organization_id,+ script_block_label=script_block_label,+ script_file_id=script_file_id,+ run_signature=run_signature,+ workflow_run_id=workflow_run_id,+ workflow_run_block_id=workflow_run_block_id,+ input_fields=input_fields,+ requires_agent=requires_agent,+ created_at=now,+ modified_at=now,+ )+ .on_conflict_do_nothing(constraint="uc_script_revision_id_script_block_label")+ .returning(ScriptBlockModel))- session.add(script_block)+ inserted = (await session.scalars(stmt)).first()+ if inserted is None:+ LOG.info(+ "create_script_block_idempotent_hit",+ script_revision_id=script_revision_id,+ script_id=script_id,+ organization_id=organization_id,+ script_block_label=script_block_label,+ )+ inserted = (+ await session.scalars(+ select(ScriptBlockModel)+ .filter_by(script_revision_id=script_revision_id)+ .filter_by(script_block_label=script_block_label)+ .filter_by(organization_id=organization_id)+ )+ ).first()+ if inserted is None:+ raise RuntimeError(+ f"create_script_block: conflict row not owned by caller "+ f"organization_id={organization_id} "+ f"script_revision_id={script_revision_id} "+ f"script_block_label={script_block_label}"+ )+ converted = convert_to_script_block(inserted)await session.commit()- await session.refresh(script_block)- return convert_to_script_block(script_block)+ return converted@db_operation("update_script_block")async def update_script_block(diff --git a/skyvern/forge/sdk/routes/browser_profiles.py b/skyvern/forge/sdk/routes/browser_profiles.pyindex 893f256fe..2c7939a58 100644--- a/skyvern/forge/sdk/routes/browser_profiles.py+++ b/skyvern/forge/sdk/routes/browser_profiles.py@@ -21,11 +21,14 @@ from skyvern.forge.sdk.routes.code_samples import (GET_BROWSER_PROFILE_CODE_SAMPLE_TS,GET_BROWSER_PROFILES_CODE_SAMPLE_PYTHON,GET_BROWSER_PROFILES_CODE_SAMPLE_TS,+ UPDATE_BROWSER_PROFILE_CODE_SAMPLE_PYTHON,+ UPDATE_BROWSER_PROFILE_CODE_SAMPLE_TS,)from skyvern.forge.sdk.routes.routers import base_routerfrom skyvern.forge.sdk.schemas.browser_profiles import (BrowserProfile,CreateBrowserProfileRequest,+ UpdateBrowserProfileRequest,)from skyvern.forge.sdk.schemas.organizations import Organizationfrom skyvern.forge.sdk.services import org_auth_service@@ -220,6 +223,82 @@ async def get_browser_profile(return profile+@base_router.patch(+ "/browser_profiles/{profile_id}",+ response_model=BrowserProfile,+ tags=["Browser Profiles"],+ summary="Update browser profile",+ description="Update a browser profile's name and/or description",+ responses={+ 200: {"description": "Successfully updated browser profile"},+ 404: {"description": "Browser profile not found"},+ 409: {"description": "Browser profile name already exists"},+ },+ openapi_extra={+ "x-fern-sdk-method-name": "update_browser_profile",+ "x-fern-examples": [+ {+ "code-samples": [+ {"sdk": "python", "code": UPDATE_BROWSER_PROFILE_CODE_SAMPLE_PYTHON},+ {"sdk": "typescript", "code": UPDATE_BROWSER_PROFILE_CODE_SAMPLE_TS},+ ]+ }+ ],+ },+)+@base_router.patch(+ "/browser_profiles/{profile_id}/",+ response_model=BrowserProfile,+ include_in_schema=False,+)+async def update_browser_profile(+ request: UpdateBrowserProfileRequest,+ profile_id: str = Path(+ ...,+ description="The ID of the browser profile to update. browser_profile_id starts with `bp_`",+ examples=["bp_123456"],+ ),+ current_org: Organization = Depends(org_auth_service.get_current_org),+) -> BrowserProfile:+ organization_id = current_org.organization_id+ LOG.info(+ "Updating browser profile",+ organization_id=organization_id,+ browser_profile_id=profile_id,+ )++ try:+ profile = await app.DATABASE.browser_sessions.update_browser_profile(+ profile_id=profile_id,+ organization_id=organization_id,+ name=request.name,+ description=request.description,+ )+ except BrowserProfileNotFound:+ LOG.warning(+ "Browser profile not found for update",+ organization_id=organization_id,+ browser_profile_id=profile_id,+ )+ raise+ except IntegrityError as exc:+ if request.name is None:+ LOG.exception(+ "Unexpected integrity error on browser profile update without name change",+ organization_id=organization_id,+ browser_profile_id=profile_id,+ )+ raise+ _handle_duplicate_profile_name(organization_id=organization_id, name=request.name, exc=exc)++ LOG.info(+ "Updated browser profile",+ organization_id=organization_id,+ browser_profile_id=profile_id,+ )+ return profile++@base_router.delete("/browser_profiles/{profile_id}",status_code=status.HTTP_204_NO_CONTENT,diff --git a/skyvern/forge/sdk/routes/code_samples.py b/skyvern/forge/sdk/routes/code_samples.pyindex 92a41bea3..71ee7dc69 100644--- a/skyvern/forge/sdk/routes/code_samples.py+++ b/skyvern/forge/sdk/routes/code_samples.py@@ -944,3 +944,22 @@ DELETE_BROWSER_PROFILE_CODE_SAMPLE_TS = """import { SkyvernClient } from "@skyveconst skyvern = new SkyvernClient({ apiKey: "YOUR_API_KEY" });await skyvern.browserProfiles.deleteBrowserProfile("bp_123");"""+UPDATE_BROWSER_PROFILE_CODE_SAMPLE_PYTHON = """from skyvern import Skyvern++skyvern = Skyvern(api_key="YOUR_API_KEY")+browser_profile = await skyvern.browser_profiles.update_browser_profile(+ "bp_123",+ name="Renamed Profile",+ description="Updated description",+)+print(browser_profile)+"""+UPDATE_BROWSER_PROFILE_CODE_SAMPLE_TS = """import { SkyvernClient } from "@skyvern/client";++const skyvern = new SkyvernClient({ apiKey: "YOUR_API_KEY" });+const browserProfile = await skyvern.browserProfiles.updateBrowserProfile("bp_123", {+ name: "Renamed Profile",+ description: "Updated description",+});+console.log(browserProfile);+"""diff --git a/skyvern/forge/sdk/routes/workflow_copilot.py b/skyvern/forge/sdk/routes/workflow_copilot.pyindex 84bd155b7..3edfc8bf7 100644--- a/skyvern/forge/sdk/routes/workflow_copilot.py+++ b/skyvern/forge/sdk/routes/workflow_copilot.py@@ -1351,6 +1351,9 @@ async def _new_copilot_chat_post())+ # Count from the full message log; chat_history below is truncated.+ turn_index = sum(1 for m in chat_messages if m.sender == WorkflowCopilotChatSender.USER) + 1+with bind_copilot_session_id(chat.workflow_copilot_chat_id):agent_result = await run_copilot_agent(stream=stream,@@ -1362,6 +1365,7 @@ async def _new_copilot_chat_post(llm_api_handler=llm_api_handler,api_key=api_key,config=copilot_config,+ turn_index=turn_index,)if getattr(agent_result, "cancelled", False):diff --git a/skyvern/forge/sdk/schemas/browser_profiles.py b/skyvern/forge/sdk/schemas/browser_profiles.pyindex 69a536b15..b55c9fe97 100644--- a/skyvern/forge/sdk/schemas/browser_profiles.py+++ b/skyvern/forge/sdk/schemas/browser_profiles.py@@ -17,7 +17,9 @@ class BrowserProfile(BaseModel):class CreateBrowserProfileRequest(BaseModel):- name: str = Field(..., description="Name for the browser profile")+ model_config = ConfigDict(str_strip_whitespace=True)++ name: str = Field(..., min_length=1, description="Name for the browser profile")description: str | None = Field(None, description="Optional profile description")browser_session_id: str | None = Field(default=None, description="Persistent browser session to convert into a profile"@@ -31,3 +33,18 @@ class CreateBrowserProfileRequest(BaseModel):if bool(self.browser_session_id) == bool(self.workflow_run_id):raise ValueError("Provide either browser_session_id or workflow_run_id")return self+++class UpdateBrowserProfileRequest(BaseModel):+ # `None` for either field means "no change" — there is currently no way to+ # clear a description back to null through this endpoint.+ model_config = ConfigDict(str_strip_whitespace=True)++ name: str | None = Field(default=None, min_length=1, description="New name for the browser profile")+ description: str | None = Field(default=None, description="New description for the browser profile")++ @model_validator(mode="after")+ def _require_at_least_one_field(self) -> "UpdateBrowserProfileRequest":+ if self.name is None and self.description is None:+ raise ValueError("At least one of `name` or `description` must be provided")+ return selfdiff --git a/skyvern/forge/sdk/workflow/models/_jinja.py b/skyvern/forge/sdk/workflow/models/_jinja.pyindex a27069058..870920bba 100644--- a/skyvern/forge/sdk/workflow/models/_jinja.py+++ b/skyvern/forge/sdk/workflow/models/_jinja.py@@ -5,8 +5,11 @@ from __future__ import annotationsimport jsonfrom typing import Any+from jinja2 import StrictUndefinedfrom jinja2.sandbox import SandboxedEnvironment+from skyvern.config import settings+# Sentinel marker for native JSON type injection via | json filter._JSON_TYPE_MARKER = "__SKYVERN_RAW_JSON__"@@ -35,3 +38,9 @@ def _json_finalize(value: Any) -> Any:jinja_json_finalize_env = SandboxedEnvironment(finalize=_json_finalize)jinja_json_finalize_env.filters["json"] = _json_type_filter++if settings.WORKFLOW_TEMPLATING_STRICTNESS == "strict":+ jinja_json_finalize_strict_env = SandboxedEnvironment(undefined=StrictUndefined, finalize=_json_finalize)+else:+ jinja_json_finalize_strict_env = SandboxedEnvironment(finalize=_json_finalize)+jinja_json_finalize_strict_env.filters["json"] = _json_type_filterdiff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.pyindex 84aca45d2..98d06f3aa 100644--- a/skyvern/forge/sdk/workflow/models/block.py+++ b/skyvern/forge/sdk/workflow/models/block.py@@ -111,6 +111,7 @@ from skyvern.forge.sdk.workflow.loop_download_filter import (from skyvern.forge.sdk.workflow.models._jinja import (_JSON_TYPE_MARKER,_json_type_filter,+ jinja_json_finalize_strict_env,)from skyvern.forge.sdk.workflow.models.parameter import (PARAMETER_TYPE,@@ -7816,7 +7817,9 @@ class WorkflowTriggerBlock(Block):workflow_run_context: WorkflowRunContext,) -> Any:"""Render a single Jinja2 template string, handling the | json filter marker."""- rendered = self.format_block_parameter_template_from_workflow_run_context(value, workflow_run_context)+ rendered = self.format_block_parameter_template_from_workflow_run_context(+ value, workflow_run_context, env=jinja_json_finalize_strict_env+ )if rendered.startswith(_JSON_TYPE_MARKER) and rendered.endswith(_JSON_TYPE_MARKER):json_str = rendered[len(_JSON_TYPE_MARKER) : -len(_JSON_TYPE_MARKER)]try:diff --git a/skyvern/webeye/utils/page.py b/skyvern/webeye/utils/page.pyindex 13443dc52..ed375ca9a 100644--- a/skyvern/webeye/utils/page.py+++ b/skyvern/webeye/utils/page.py@@ -36,6 +36,24 @@ def load_js_script() -> str:JS_FUNCTION_DEFS = load_js_script()+_NAVIGATION_RECOVERY_MAX_ATTEMPTS = 4+_NAVIGATION_SETTLE_TIMEOUT_MS = 3000+++def _is_navigation_context_lost(error_msg: str) -> bool:+ if "Execution context was destroyed" in error_msg:+ return True+ return "ReferenceError" in error_msg and "is not defined" in error_msg+++async def _wait_for_navigation_settle(frame: Page | Frame, timeout_ms: float) -> None:+ if timeout_ms <= 0:+ return+ try:+ await frame.wait_for_load_state("load", timeout=timeout_ms)+ except PlaywrightError:+ return+def _load_cursor_overlay_js() -> str:path = f"{SKYVERN_DIR}/webeye/scraper/cursorOverlay.js"@@ -272,32 +290,104 @@ class SkyvernFrame:return await frame.evaluate(expression=expression, arg=arg)except PlaywrightError as e:error_msg = str(e)- is_context_destroyed = "Execution context was destroyed" in error_msg- is_domutils_missing = "ReferenceError" in error_msg and "is not defined" in error_msg- if is_context_destroyed or is_domutils_missing:- LOG.warning(- "JS execution context lost (likely due to page navigation), re-injecting domUtils.js and retrying",- expression=expression[:200],- error=error_msg[:200],- )- try:- await frame.evaluate(expression=JS_FUNCTION_DEFS)- except PlaywrightError:- LOG.warning("Re-injection of domUtils.js also failed, page may still be navigating")- raise- # First call failed fast (context destroyed, not timeout), so a fresh- # timeout for the retry is acceptable.- try:- async with asyncio.timeout(timeout_ms / 1000):- return await frame.evaluate(expression=expression, arg=arg)- except asyncio.TimeoutError:- LOG.exception("Skyvern timed out on retry after JS context re-injection", expression=expression)- raise TimeoutError("Skyvern timed out trying to analyze the page")- raise+ if not _is_navigation_context_lost(error_msg):+ raise+ return await SkyvernFrame._evaluate_with_navigation_recovery(+ frame=frame,+ expression=expression,+ arg=arg,+ timeout_ms=timeout_ms,+ initial_error=error_msg,+ )except asyncio.TimeoutError:LOG.exception("Skyvern timed out trying to analyze the page", expression=expression)raise TimeoutError("Skyvern timed out trying to analyze the page")+ @staticmethod+ async def _evaluate_with_navigation_recovery(+ frame: Page | Frame,+ expression: str,+ arg: Any | None,+ timeout_ms: float,+ initial_error: str,+ ) -> Any:+ # Multi-hop SSO/OIDC flows (especially response_mode=form_post) can destroy+ # the JS execution context several times in a row as the page auto-submits+ # through redirects. Wait for the page to settle between attempts instead+ # of racing the next navigation. The whole recovery shares one monotonic+ # deadline so retries can't compound into many multiples of timeout_ms.+ per_attempt_seconds = timeout_ms / 1000+ loop = asyncio.get_running_loop()+ deadline = loop.time() + per_attempt_seconds * _NAVIGATION_RECOVERY_MAX_ATTEMPTS++ def _remaining_seconds() -> float:+ return max(0.0, deadline - loop.time())++ last_error_msg = initial_error+ for attempt in range(1, _NAVIGATION_RECOVERY_MAX_ATTEMPTS + 1):+ if _remaining_seconds() <= 0:+ LOG.error(+ "Skyvern timed out trying to analyze the page after navigation recovery",+ expression=expression,+ )+ raise TimeoutError("Skyvern timed out trying to analyze the page")++ LOG.warning(+ "JS execution context lost (likely due to page navigation), re-injecting domUtils.js and retrying",+ attempt=attempt,+ expression=expression[:200],+ error=last_error_msg[:200],+ )+ settle_ms = min(_NAVIGATION_SETTLE_TIMEOUT_MS, _remaining_seconds() * 1000)+ await _wait_for_navigation_settle(frame, timeout_ms=settle_ms)++ inject_budget = min(per_attempt_seconds, _remaining_seconds())+ if inject_budget <= 0:+ LOG.error(+ "Skyvern timed out trying to analyze the page after navigation recovery",+ expression=expression,+ )+ raise TimeoutError("Skyvern timed out trying to analyze the page")+ try:+ async with asyncio.timeout(inject_budget):+ await frame.evaluate(expression=JS_FUNCTION_DEFS)+ except asyncio.TimeoutError:+ LOG.exception(+ "Skyvern timed out trying to analyze the page during domUtils.js re-injection",+ expression=expression,+ )+ raise TimeoutError("Skyvern timed out trying to analyze the page")+ except PlaywrightError as inject_err:+ last_error_msg = str(inject_err)+ if attempt == _NAVIGATION_RECOVERY_MAX_ATTEMPTS or not _is_navigation_context_lost(last_error_msg):+ LOG.warning(+ "Re-injection of domUtils.js also failed, page may still be navigating",+ attempts=attempt,+ )+ raise+ continue++ retry_budget = min(per_attempt_seconds, _remaining_seconds())+ if retry_budget <= 0:+ LOG.error(+ "Skyvern timed out trying to analyze the page after navigation recovery",+ expression=expression,+ )+ raise TimeoutError("Skyvern timed out trying to analyze the page")+ try:+ async with asyncio.timeout(retry_budget):+ return await frame.evaluate(expression=expression, arg=arg)+ except asyncio.TimeoutError:+ LOG.exception("Skyvern timed out on retry after JS context re-injection", expression=expression)+ raise TimeoutError("Skyvern timed out trying to analyze the page")+ except PlaywrightError as retry_err:+ last_error_msg = str(retry_err)+ if attempt == _NAVIGATION_RECOVERY_MAX_ATTEMPTS or not _is_navigation_context_lost(last_error_msg):+ raise++ # The loop either returns or raises; this is unreachable but keeps mypy happy.+ raise PlaywrightError(last_error_msg)+@staticmethodasync def get_url(frame: Page | Frame) -> str:return await SkyvernFrame.evaluate(frame=frame, expression="() => document.location.href")diff --git a/tests/unit/forge/sdk/db/test_repositories.py b/tests/unit/forge/sdk/db/test_repositories.pyindex 3743d18e1..67171225d 100644--- a/tests/unit/forge/sdk/db/test_repositories.py+++ b/tests/unit/forge/sdk/db/test_repositories.py@@ -111,6 +111,8 @@ def test_browser_sessions_repository_instantiation():assert repo.Session is mock_sessionassert hasattr(repo, "create_browser_profile")assert hasattr(repo, "get_browser_profile")+ assert hasattr(repo, "update_browser_profile")+ assert hasattr(repo, "delete_browser_profile")# ── Cross-dependency repositories ──diff --git a/tests/unit/forge/sdk/test_update_browser_profile_route.py b/tests/unit/forge/sdk/test_update_browser_profile_route.pynew file mode 100644index 000000000..d87d0066f--- /dev/null+++ b/tests/unit/forge/sdk/test_update_browser_profile_route.py@@ -0,0 +1,189 @@+"""Tests for PATCH /browser_profiles/{profile_id}."""++from __future__ import annotations++from datetime import datetime, timezone+from types import SimpleNamespace+from unittest.mock import AsyncMock++import pytest+from fastapi import FastAPI, Request+from fastapi.responses import JSONResponse+from fastapi.testclient import TestClient++from skyvern.exceptions import BrowserProfileNotFound, SkyvernHTTPException+from skyvern.forge import app as forge_app+from skyvern.forge.sdk.routes import routers as routers_module+from skyvern.forge.sdk.schemas.browser_profiles import BrowserProfile+from skyvern.forge.sdk.services import org_auth_service++ORG_ID = "org_oss"+PROFILE_ID = "bp_test_profile"+++def _profile(+ name: str = "My Profile",+ description: str | None = "A profile",+ organization_id: str = ORG_ID,+ profile_id: str = PROFILE_ID,+) -> BrowserProfile:+ now = datetime.now(timezone.utc)+ return BrowserProfile(+ browser_profile_id=profile_id,+ organization_id=organization_id,+ name=name,+ description=description,+ source_browser_type=None,+ created_at=now,+ modified_at=now,+ deleted_at=None,+ )+++def _build_client(monkeypatch: pytest.MonkeyPatch) -> tuple[TestClient, AsyncMock]:+ async def _fake_org() -> SimpleNamespace:+ return SimpleNamespace(organization_id=ORG_ID)++ update_browser_profile = AsyncMock()+ monkeypatch.setattr(+ forge_app.DATABASE.browser_sessions,+ "update_browser_profile",+ update_browser_profile,+ )++ fastapi_app = FastAPI()+ fastapi_app.dependency_overrides[org_auth_service.get_current_org] = _fake_org+ fastapi_app.include_router(routers_module.base_router, prefix="/v1")++ @fastapi_app.exception_handler(SkyvernHTTPException)+ async def _handle_skyvern_http_exception(request: Request, exc: SkyvernHTTPException) -> JSONResponse:+ return JSONResponse(status_code=exc.status_code, content={"detail": exc.message})++ return TestClient(fastapi_app), update_browser_profile+++def test_update_browser_profile_renames(monkeypatch: pytest.MonkeyPatch) -> None:+ client, update_browser_profile = _build_client(monkeypatch)+ update_browser_profile.return_value = _profile(name="Renamed")++ response = client.patch(+ f"/v1/browser_profiles/{PROFILE_ID}",+ json={"name": "Renamed"},+ )++ assert response.status_code == 200+ body = response.json()+ assert body["browser_profile_id"] == PROFILE_ID+ assert body["name"] == "Renamed"+ update_browser_profile.assert_awaited_once_with(+ profile_id=PROFILE_ID,+ organization_id=ORG_ID,+ name="Renamed",+ description=None,+ )+++def test_update_browser_profile_description_only(monkeypatch: pytest.MonkeyPatch) -> None:+ client, update_browser_profile = _build_client(monkeypatch)+ update_browser_profile.return_value = _profile(description="Brand new description")++ response = client.patch(+ f"/v1/browser_profiles/{PROFILE_ID}",+ json={"description": "Brand new description"},+ )++ assert response.status_code == 200+ body = response.json()+ assert body["description"] == "Brand new description"+ update_browser_profile.assert_awaited_once_with(+ profile_id=PROFILE_ID,+ organization_id=ORG_ID,+ name=None,+ description="Brand new description",+ )+++def test_update_browser_profile_name_and_description(monkeypatch: pytest.MonkeyPatch) -> None:+ client, update_browser_profile = _build_client(monkeypatch)+ update_browser_profile.return_value = _profile(name="Renamed", description="New desc")++ response = client.patch(+ f"/v1/browser_profiles/{PROFILE_ID}",+ json={"name": "Renamed", "description": "New desc"},+ )++ assert response.status_code == 200+ body = response.json()+ assert body["name"] == "Renamed"+ assert body["description"] == "New desc"+ update_browser_profile.assert_awaited_once_with(+ profile_id=PROFILE_ID,+ organization_id=ORG_ID,+ name="Renamed",+ description="New desc",+ )+++def test_update_browser_profile_not_found(monkeypatch: pytest.MonkeyPatch) -> None:+ client, update_browser_profile = _build_client(monkeypatch)+ update_browser_profile.side_effect = BrowserProfileNotFound(profile_id=PROFILE_ID, organization_id=ORG_ID)++ response = client.patch(+ f"/v1/browser_profiles/{PROFILE_ID}",+ json={"name": "Renamed"},+ )++ assert response.status_code == 404+++def test_update_browser_profile_cross_org_returns_404(monkeypatch: pytest.MonkeyPatch) -> None:+ client, update_browser_profile = _build_client(monkeypatch)++ owning_org_id = "org_other"++ async def _org_scoped_update(*, organization_id: str, **kwargs: object) -> BrowserProfile:+ if organization_id != owning_org_id:+ raise BrowserProfileNotFound(profile_id=PROFILE_ID, organization_id=organization_id)+ return _profile(organization_id=owning_org_id)++ update_browser_profile.side_effect = _org_scoped_update++ response = client.patch(+ f"/v1/browser_profiles/{PROFILE_ID}",+ json={"name": "Renamed"},+ )++ assert response.status_code == 404+ update_browser_profile.assert_awaited_once_with(+ profile_id=PROFILE_ID,+ organization_id=ORG_ID,+ name="Renamed",+ description=None,+ )+++def test_update_browser_profile_empty_body_is_rejected(monkeypatch: pytest.MonkeyPatch) -> None:+ client, update_browser_profile = _build_client(monkeypatch)++ response = client.patch(f"/v1/browser_profiles/{PROFILE_ID}", json={})++ assert response.status_code == 422+ update_browser_profile.assert_not_awaited()+++def test_update_browser_profile_alias_trailing_slash(monkeypatch: pytest.MonkeyPatch) -> None:+ client, update_browser_profile = _build_client(monkeypatch)+ update_browser_profile.return_value = _profile(name="Renamed")++ response = client.patch(+ f"/v1/browser_profiles/{PROFILE_ID}/",+ json={"name": "Renamed"},+ )++ assert response.status_code == 200+ update_browser_profile.assert_awaited_once_with(+ profile_id=PROFILE_ID,+ organization_id=ORG_ID,+ name="Renamed",+ description=None,+ )diff --git a/tests/unit/test_copilot_agent_helpers.py b/tests/unit/test_copilot_agent_helpers.pyindex 28c7620ca..f461d1dee 100644--- a/tests/unit/test_copilot_agent_helpers.py+++ b/tests/unit/test_copilot_agent_helpers.py@@ -230,6 +230,97 @@ class TestSupersededAgentIntentGates:assert not hasattr(agent_module, "_credential_validation_result_for_user_message")+class TestRequestPolicyInputGuardrail:+ @pytest.mark.asyncio+ async def test_sdk_input_guardrail_computes_and_stores_request_policy(self, monkeypatch) -> None:+ from agents import GuardrailFunctionOutput, InputGuardrail+ from agents.run_context import RunContextWrapper++ from skyvern.forge.sdk.copilot.request_policy import RequestPolicy++ policy = RequestPolicy(+ testing_intent="skip_test",+ credential_input_kind="credential_name",+ credential_refs=["Saved Login"],+ allow_run_blocks=False,+ )+ build_request_policy = AsyncMock(return_value=policy)+ monkeypatch.setattr(agent_module, "build_request_policy", build_request_policy)+ ctx = _ctx()+ policy_inputs = agent_module.RequestPolicyGuardrailInputs(+ user_message="just draft without testing",+ workflow_yaml="workflow: yaml",+ chat_history_text="user: build the login workflow",+ global_llm_context="",+ organization_id="org-1",+ handler=object(),+ previous_user_message="build the login workflow",+ )++ guardrails = agent_module._build_copilot_input_guardrails(+ InputGuardrail,+ GuardrailFunctionOutput,+ policy_inputs=policy_inputs,+ )+ result = await guardrails[0].run(SimpleNamespace(), "input", RunContextWrapper(context=ctx))++ assert result.output.tripwire_triggered is False+ assert ctx.request_policy is policy+ assert ctx.allow_untested_workflow_draft is True+ assert "Draft the workflow requested earlier" in ctx.user_message+ assert "build the login workflow" in ctx.user_message+ assert result.output.output_info["policy_present"] is True+ assert result.output.output_info["testing_intent"] == "skip_test"+ assert "completion_contract" not in result.output.output_info+ build_request_policy.assert_awaited_once_with(+ user_message="just draft without testing",+ workflow_yaml="workflow: yaml",+ chat_history="user: build the login workflow",+ global_llm_context="",+ organization_id="org-1",+ handler=policy_inputs.handler,+ )++ @pytest.mark.asyncio+ async def test_sdk_input_guardrail_trips_after_computing_blocked_policy(self, monkeypatch) -> None:+ from agents import GuardrailFunctionOutput, InputGuardrail+ from agents.run_context import RunContextWrapper++ from skyvern.forge.sdk.copilot.request_policy import RequestPolicy++ policy = RequestPolicy(+ credential_input_kind="raw_secret",+ user_response_policy="ask_clarification",+ allow_update_workflow=False,+ allow_run_blocks=False,+ raw_secret_detected=True,+ clarification_reason="raw_secret",+ clarification_question="Do not paste raw credentials.",+ )+ monkeypatch.setattr(agent_module, "build_request_policy", AsyncMock(return_value=policy))+ ctx = _ctx()+ guardrails = agent_module._build_copilot_input_guardrails(+ InputGuardrail,+ GuardrailFunctionOutput,+ policy_inputs=agent_module.RequestPolicyGuardrailInputs(+ user_message="use password=hunter2",+ workflow_yaml="",+ chat_history_text="",+ global_llm_context="",+ organization_id="org-1",+ handler=None,+ ),+ )++ result = await guardrails[0].run(SimpleNamespace(), "input", RunContextWrapper(context=ctx))++ assert result.output.tripwire_triggered is True+ assert ctx.request_policy is policy+ assert result.output.output_info["credential_input_kind"] == "raw_secret"+ assert result.output.output_info["blocked"] is True+ assert "hunter2" not in str(result.output.output_info)++class TestShouldRestorePersistedWorkflow:"""SKY-9143: auto_accept=True must still restore when no proposal shipped."""@@ -878,7 +969,7 @@ class TestCredentialRefusalReachesAgent:"""Prove the SKY-9189 refusal rule is actually delivered to the agent.`run_copilot_agent` constructs the openai-agents SDK `Agent(...)` with- `instructions=_build_system_prompt(...)` and `tools=list(NATIVE_TOOLS)`.+ dynamic instructions derived from `_build_system_prompt(...)` and `tools=list(NATIVE_TOOLS)`.A behavior test would require patching the agent loop and is fragile; aconstruction test (rule text flows through the exact helpers the routeuses) is deterministic and catches both prompt and tool-surface drift.@@ -1324,7 +1415,7 @@ class TestRequestPolicyCredentialResolution:)assert paraphrased_completion_policy.completion_contract is Noneassert "completion_contract:" not in paraphrased_completion_policy.prompt_summary()- assert paraphrased_completion_policy.to_trace_data()["completion_contract"] is None+ assert paraphrased_completion_policy.to_trace_data()["has_completion_contract"] is False@pytest.mark.asyncioasync def test_request_policy_noncredential_clarification_uses_specific_copy(self, monkeypatch) -> None:@@ -1720,3 +1811,5 @@ class TestCopilotConfig:assert result.user_response == "ok"assert resolved_keys == ["PRIMARY", "SECONDARY"]assert run_with_enforcement.await_count == 2+ for call in run_with_enforcement.await_args_list:+ assert not getattr(call.kwargs["agent"], "input_guardrails", None)diff --git a/tests/unit/test_copilot_output_policy.py b/tests/unit/test_copilot_output_policy.pyindex b75ba4ba5..54a1fd723 100644--- a/tests/unit/test_copilot_output_policy.py+++ b/tests/unit/test_copilot_output_policy.py@@ -462,6 +462,137 @@ def test_sdk_output_guardrail_hard_blocks_raw_secret_final_text() -> None:assert verdict.reason_codes == [OutputPolicyReason.RAW_SECRET_LEAK]+def test_sdk_output_guardrail_ignores_internal_context_credential_ids() -> None:+ ctx = _ctx(+ request_policy=_policy(+ credential_input_kind="credential_name",+ credential_refs=["azure_credentials"],+ allow_run_blocks=False,+ allow_missing_credentials_in_draft=True,+ ),+ )+ ctx.last_workflow = object()+ ctx.last_workflow_yaml = """+workflow_definition:+ parameters:+ - parameter_type: workflow+ workflow_parameter_type: credential_id+ key: azure_credentials+ blocks:+ - block_type: login+ label: login+ url: https://example.com/login+ parameter_keys:+ - azure_credentials+"""++ verdict, response_type = agent_module._evaluate_copilot_final_output_policy(+ ctx,+ {+ "type": "REPLY",+ "user_response": "I have a draft workflow proposal. It has not been tested.",+ "global_llm_context": "Tool observation listed cred_unrelated for a different saved credential.",+ },+ )++ assert response_type == "REPLY"+ assert verdict.allowed+ assert OutputPolicyReason.UNAPPROVED_CREDENTIAL_REFERENCE not in verdict.reason_codes+++def test_sdk_output_guardrail_defers_raw_question_after_untested_draft() -> None:+ ctx = _ctx(+ request_policy=_policy(+ testing_intent="skip_test",+ credential_input_kind="credential_name",+ credential_refs=["azure_credentials"],+ allow_run_blocks=False,+ allow_missing_credentials_in_draft=True,+ resolved_credentials=[],+ ),+ )+ ctx.allow_untested_workflow_draft = True+ ctx.last_workflow = object()+ ctx.last_update_block_count = 9+ ctx.last_workflow_yaml = """+workflow_definition:+ parameters:+ - parameter_type: workflow+ workflow_parameter_type: credential_id+ key: azure_credentials+ blocks:+ - block_type: login+ label: login+ url: https://example.com/login+ parameter_keys:+ - azure_credentials+"""++ verdict, response_type = agent_module._evaluate_copilot_final_output_policy(+ ctx,+ {+ "type": "ASK_QUESTION",+ "user_response": "I found cred_unrelated in an internal tool observation. Should I use it?",+ },+ )++ assert response_type == "ASK_QUESTION"+ assert verdict.allowed+ assert OutputPolicyReason.UNAPPROVED_CREDENTIAL_REFERENCE not in verdict.reason_codes+++def test_translation_surfaces_untested_draft_when_agent_asks_after_drafting() -> None:+ ctx = _ctx(+ request_policy=_policy(+ testing_intent="skip_test",+ credential_input_kind="credential_name",+ credential_refs=["azure_credentials"],+ allow_run_blocks=False,+ allow_missing_credentials_in_draft=True,+ resolved_credentials=[],+ ),+ )+ ctx.allow_untested_workflow_draft = True+ ctx.last_workflow = object()+ ctx.last_update_block_count = 9+ ctx.workflow_persisted = True+ ctx.last_workflow_yaml = """+workflow_definition:+ parameters:+ - parameter_type: workflow+ workflow_parameter_type: credential_id+ key: azure_credentials+ blocks:+ - block_type: login+ label: login+ url: https://example.com/login+ parameter_keys:+ - azure_credentials+"""++ result = agent_module._translate_to_agent_result(+ _fake_run_result(+ {+ "type": "ASK_QUESTION",+ "user_response": "I found cred_unrelated in an internal tool observation. Should I use it?",+ "global_llm_context": "{}",+ }+ ),+ ctx,+ "{}",+ _chat_request(),+ "org-1",+ )++ assert result.response_type == "REPLY"+ assert result.updated_workflow is ctx.last_workflow+ assert result.unvalidated is True+ assert result.clear_proposed_workflow is False+ assert "without testing it" in result.user_response+ assert "not been verified" in result.user_response+ assert "cred_unrelated" not in result.user_response++@pytest.mark.asyncioasync def test_workflow_mutation_tools_have_sdk_input_guardrails_and_reject_raw_secret() -> None:from agents import ToolInputGuardrailDatadiff --git a/tests/unit/test_copilot_turn_span.py b/tests/unit/test_copilot_turn_span.pynew file mode 100644index 000000000..717248a9d--- /dev/null+++ b/tests/unit/test_copilot_turn_span.py@@ -0,0 +1,187 @@+"""Tests for the copilot.turn span wrapper around run_copilot_agent."""++from __future__ import annotations++from datetime import datetime, timezone+from typing import Any++import pytest+from opentelemetry import trace as otel_trace++from skyvern.forge.sdk.copilot import agent as copilot_agent+from skyvern.forge.sdk.copilot.request_policy import RequestPolicy+from skyvern.forge.sdk.schemas.workflow_copilot import (+ WorkflowCopilotChatHistoryMessage,+ WorkflowCopilotChatRequest,+ WorkflowCopilotChatSender,+)+++def _make_chat_request(+ *,+ message: str = "Hello, build me a workflow",+ workflow_copilot_chat_id: str | None = "chat_abc",+ workflow_permanent_id: str = "wpid_xyz",+) -> WorkflowCopilotChatRequest:+ return WorkflowCopilotChatRequest(+ workflow_permanent_id=workflow_permanent_id,+ workflow_id="w_001",+ workflow_copilot_chat_id=workflow_copilot_chat_id,+ message=message,+ workflow_yaml="",+ )+++def _user_message(content: str) -> WorkflowCopilotChatHistoryMessage:+ return WorkflowCopilotChatHistoryMessage(+ sender=WorkflowCopilotChatSender.USER,+ content=content,+ created_at=datetime.now(timezone.utc),+ )+++async def _stub_build_request_policy_with_child_span(*_args: Any, **_kwargs: Any) -> RequestPolicy:+ """Open an OTel child span so the test can assert parentage, then short-circuit+ run_copilot_agent via the ask_clarification early-return path."""+ tracer = otel_trace.get_tracer("test.copilot.turn")+ with tracer.start_as_current_span("test.req_policy_child"):+ pass+ return RequestPolicy(+ user_response_policy="ask_clarification",+ clarification_question="What URL should I target?",+ clarification_reason="missing_target_url",+ )+++async def _stub_build_request_policy_proceed(*_args: Any, **_kwargs: Any) -> RequestPolicy:+ return RequestPolicy(user_response_policy="ask_clarification", clarification_question="?")+++def patched_build_request_policy(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(+ copilot_agent,+ "build_request_policy",+ _stub_build_request_policy_with_child_span,+ )+++def _find_span(spans: list[Any], name: str) -> Any:+ matches = [s for s in spans if s.name == name]+ assert matches, f"expected span named {name!r}, got: {[s.name for s in spans]}"+ assert len(matches) == 1, f"expected exactly one span named {name!r}, got {len(matches)}"+ return matches[0]+++async def test_copilot_turn_span_parents_inner_spans(+ span_exporter: Any,+ patched_build_request_policy: None,+) -> None:+ chat_request = _make_chat_request(message="Hello, build me a workflow")+ chat_history = [_user_message("prior question")]++ result = await copilot_agent.run_copilot_agent(+ stream=object(), # never used on the ask_clarification path+ organization_id="o_test",+ chat_request=chat_request,+ chat_history=chat_history,+ global_llm_context=None,+ debug_run_info_text="",+ llm_api_handler=None,+ )++ assert result is not None+ spans = span_exporter.get_finished_spans()+ turn_span = _find_span(spans, "copilot.turn")+ child_span = _find_span(spans, "test.req_policy_child")++ assert child_span.parent is not None+ assert child_span.parent.span_id == turn_span.context.span_id++ attrs = dict(turn_span.attributes or {})+ assert attrs.get("skyvern.span.role") == "wrapper"+ assert attrs.get("copilot.session_id") == "chat_abc"+ assert attrs.get("workflow_permanent_id") == "wpid_xyz"+ # one prior user msg in history + this turn = 2.+ assert attrs.get("copilot.turn_index") == 2+ preview = attrs.get("copilot.user_message_preview")+ assert isinstance(preview, str) and preview.startswith("Hello")+++async def test_copilot_turn_span_uses_explicit_turn_index(+ span_exporter: Any,+ patched_build_request_policy: None,+) -> None:+ chat_request = _make_chat_request()+ chat_history = [_user_message("a"), _user_message("b"), _user_message("c")]++ await copilot_agent.run_copilot_agent(+ stream=object(),+ organization_id="o_test",+ chat_request=chat_request,+ chat_history=chat_history,+ global_llm_context=None,+ debug_run_info_text="",+ llm_api_handler=None,+ turn_index=42,+ )++ turn_span = _find_span(span_exporter.get_finished_spans(), "copilot.turn")+ assert dict(turn_span.attributes or {}).get("copilot.turn_index") == 42+++async def test_copilot_turn_span_omits_session_id_when_missing(+ span_exporter: Any,+ patched_build_request_policy: None,+) -> None:+ chat_request = _make_chat_request(workflow_copilot_chat_id=None)++ await copilot_agent.run_copilot_agent(+ stream=object(),+ organization_id="o_test",+ chat_request=chat_request,+ chat_history=[],+ global_llm_context=None,+ debug_run_info_text="",+ llm_api_handler=None,+ )++ turn_span = _find_span(span_exporter.get_finished_spans(), "copilot.turn")+ attrs = dict(turn_span.attributes or {})+ assert "copilot.session_id" not in attrs+++async def test_copilot_turn_span_preview_is_single_line_and_truncated(+ span_exporter: Any,+ patched_build_request_policy: None,+) -> None:+ long_msg = "Line one\nLine two with extra content " + ("x" * 200)+ chat_request = _make_chat_request(message=long_msg)++ await copilot_agent.run_copilot_agent(+ stream=object(),+ organization_id="o_test",+ chat_request=chat_request,+ chat_history=[],+ global_llm_context=None,+ debug_run_info_text="",+ llm_api_handler=None,+ )++ turn_span = _find_span(span_exporter.get_finished_spans(), "copilot.turn")+ preview = dict(turn_span.attributes or {}).get("copilot.user_message_preview")+ assert isinstance(preview, str)+ assert "\n" not in preview+ assert "\r" not in preview+ assert len(preview) <= copilot_agent._USER_MESSAGE_PREVIEW_MAX_CHARS+ assert preview.endswith("…")+++def test_build_user_message_preview_redacts_known_secret_patterns() -> None:+ preview = copilot_agent._build_user_message_preview("password: hunter2 please")+ assert "hunter2" not in preview+ assert "REDACTED_SECRET" in previewdiff --git a/tests/unit/test_mcp_browser_tools.py b/tests/unit/test_mcp_browser_tools.pyindex 7485db869..cfa9bf01e 100644--- a/tests/unit/test_mcp_browser_tools.py+++ b/tests/unit/test_mcp_browser_tools.py@@ -2,6 +2,7 @@from __future__ import annotations+import asynciofrom types import SimpleNamespacefrom unittest.mock import AsyncMock@@ -63,3 +64,86 @@ async def test_skyvern_act_password_prompt_preflight_before_session(monkeypatch:assert result["ok"] is Falseassert result["error"]["code"] == mcp_browser.ErrorCode.INVALID_INPUTget_page.assert_not_awaited()+++def _login_page_mock(login_side_effect: object) -> object:+ agent = SimpleNamespace(login=AsyncMock(side_effect=login_side_effect))+ return SimpleNamespace(agent=agent)+++async def test_skyvern_login_timeout_returns_timeout_code(monkeypatch: pytest.MonkeyPatch) -> None:+ page = _login_page_mock(asyncio.TimeoutError())+ context = BrowserContext(mode="cloud_session", session_id="pbs_test")+ monkeypatch.setattr(mcp_browser, "get_page", AsyncMock(return_value=(page, context)))++ result = await mcp_browser.skyvern_login(+ credential_type="skyvern",+ credential_id="cred_123",+ timeout_seconds=30,+ )++ assert result["ok"] is False+ assert result["error"]["code"] == mcp_browser.ErrorCode.TIMEOUT+ assert result["error"]["message"]+ assert "30" in result["error"]["message"]+ assert "dashboard" in result["error"]["hint"].lower() or "run" in result["error"]["hint"].lower()+++async def test_skyvern_login_empty_exception_message_falls_back_to_type(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ class _Silent(Exception):+ pass++ page = _login_page_mock(_Silent())+ context = BrowserContext(mode="cloud_session", session_id="pbs_test")+ monkeypatch.setattr(mcp_browser, "get_page", AsyncMock(return_value=(page, context)))++ result = await mcp_browser.skyvern_login(+ credential_type="skyvern",+ credential_id="cred_123",+ )++ assert result["ok"] is False+ assert result["error"]["code"] == mcp_browser.ErrorCode.SDK_ERROR+ assert result["error"]["message"] == "_Silent"+++async def test_skyvern_login_success_returns_run_data(monkeypatch: pytest.MonkeyPatch) -> None:+ response = SimpleNamespace(+ run_id="wr_123",+ status="completed",+ output={"ok": True},+ failure_reason=None,+ recording_url="https://example.com/rec",+ app_url="https://app.example.com/wr_123",+ )+ page = _login_page_mock(None)+ page.agent.login = AsyncMock(return_value=response)+ context = BrowserContext(mode="cloud_session", session_id="pbs_test")+ monkeypatch.setattr(mcp_browser, "get_page", AsyncMock(return_value=(page, context)))++ result = await mcp_browser.skyvern_login(+ credential_type="skyvern",+ credential_id="cred_123",+ )++ assert result["ok"] is True+ assert result["data"]["run_id"] == "wr_123"+ assert result["data"]["status"] == "completed"+++async def test_skyvern_run_task_timeout_returns_timeout_code(monkeypatch: pytest.MonkeyPatch) -> None:+ page = SimpleNamespace(agent=SimpleNamespace(run_task=AsyncMock(side_effect=asyncio.TimeoutError())))+ context = BrowserContext(mode="cloud_session", session_id="pbs_test")+ monkeypatch.setattr(mcp_browser, "get_page", AsyncMock(return_value=(page, context)))++ result = await mcp_browser.skyvern_run_task(prompt="navigate and click", timeout_seconds=45)++ assert result["ok"] is False+ assert result["error"]["code"] == mcp_browser.ErrorCode.TIMEOUT+ assert "45" in result["error"]["message"]diff --git a/tests/unit/workflow/test_workflow_trigger_block.py b/tests/unit/workflow/test_workflow_trigger_block.pyindex b0e20669d..e1a74944e 100644--- a/tests/unit/workflow/test_workflow_trigger_block.py+++ b/tests/unit/workflow/test_workflow_trigger_block.py@@ -2,6 +2,7 @@from __future__ import annotations+import jsonfrom datetime import datetime, timezonefrom typing import Anyfrom unittest.mock import AsyncMock, MagicMock, patch@@ -243,6 +244,77 @@ class TestPayloadTemplateRenderError:assert jinja_sandbox_env is not None+class TestPayloadJsonSerialization:+ """Payload templates rendered with plain ``{{var}}`` must emit JSON for dict/list values."""++ def _render_payload_live(+ self,+ block: WorkflowTriggerBlock,+ payload: dict[str, Any],+ values: dict[str, Any],+ ) -> dict[str, Any]:+ ctx = MagicMock()+ ctx.values = values+ ctx.secrets = {}+ ctx.include_secrets_in_templates = False+ ctx.get_block_metadata = MagicMock(return_value={})+ return block._render_templates_in_payload(payload, ctx)++ def test_list_value_renders_as_json(self) -> None:+ block = _make_block()+ parties = [+ {"entity_name": "1061 Realty"},+ {"first_name": "Antonio", "last_name": "Rodriguez"},+ ]+ result = self._render_payload_live(+ block,+ {"parties_to_search": "{{ parties_to_search }}"},+ {"parties_to_search": parties},+ )+ rendered = result["parties_to_search"]+ assert isinstance(rendered, str)+ assert json.loads(rendered) == parties++ def test_dict_value_renders_as_json(self) -> None:+ block = _make_block()+ data = {"key": "value", "nested": {"x": 1}}+ result = self._render_payload_live(+ block,+ {"data": "{{ data }}"},+ {"data": data},+ )+ rendered = result["data"]+ assert isinstance(rendered, str)+ assert json.loads(rendered) == data++ def test_string_value_unchanged(self) -> None:+ block = _make_block()+ result = self._render_payload_live(+ block,+ {"file_number": "{{ file_number }}"},+ {"file_number": "ABC-123"},+ )+ assert result == {"file_number": "ABC-123"}++ def test_int_value_renders_as_decimal_string(self) -> None:+ block = _make_block()+ result = self._render_payload_live(+ block,+ {"count": "{{ count }}"},+ {"count": 42},+ )+ assert result == {"count": "42"}++ def test_json_filter_still_returns_native_typed_value(self) -> None:+ block = _make_block()+ result = self._render_payload_live(+ block,+ {"data": "{{ data | json }}"},+ {"data": {"key": "value"}},+ )+ assert result == {"data": {"key": "value"}}++class TestCheckTriggerDepth:"""Test _check_trigger_depth: boundary conditions at/above/below MAX_TRIGGER_DEPTH."""