Skip to content

Automate browser based workflows with AI

SKY-15269 Preserve current-page inspection after credential runs (#8351)

Andrew Neilson791be964bfa7
diff --git a/skyvern/forge/sdk/copilot/mcp_adapter.py b/skyvern/forge/sdk/copilot/mcp_adapter.py
index 74ac0f059..fcb426255 100644
--- a/skyvern/forge/sdk/copilot/mcp_adapter.py
+++ b/skyvern/forge/sdk/copilot/mcp_adapter.py
@@ -58,6 +58,7 @@ from skyvern.forge.sdk.copilot.runtime import (
mcp_to_copilot,
resolve_browser_state_for_context,
retire_browser_session_id,
+ sensitive_origin_page_has_active_run,
sensitive_origin_page_is_tainted,
)
from skyvern.forge.sdk.copilot.screenshot_utils import (
@@ -65,7 +66,11 @@ from skyvern.forge.sdk.copilot.screenshot_utils import (
ScreenshotProvenance,
enqueue_screenshot_from_result,
)
-from skyvern.forge.sdk.copilot.secret_scrub import scrub_secrets_from_structure
+from skyvern.forge.sdk.copilot.secret_scrub import (
+ matching_origin_run_redaction_parameters,
+ register_matching_origin_run_redaction_values,
+ scrub_secrets_from_structure,
+)
from skyvern.forge.sdk.copilot.turn_origin import TurnOrigin
from skyvern.utils.contained_effects import contained_effect
from skyvern.webeye.browser_state import BrowserState
@@ -439,6 +444,7 @@ class SchemaOverlay:
# stripped before the call, so the underlying tool never sees an argument it cannot accept.
copilot_params: dict[str, Any] = field(default_factory=dict)
requires_browser: bool = False
+ redacts_sensitive_origin_structured_result: bool = False
timeout: int | None = None
pre_hook: PreHook | None = None
post_hook: PostHook | None = None
@@ -630,14 +636,22 @@ def _scrub_tool_result(ctx: AgentContext, result: Any) -> dict[str, Any]:
scrubbed_secrets = scrub_secrets_from_structure(ctx, result)
if not isinstance(scrubbed_secrets, dict) or not _mapping_keys_preserved(result, scrubbed_secrets):
return {}
+ parameter_sets: list[dict[str, Any]] = []
parameters = getattr(ctx, "codeblock_redaction_parameters", None)
- if not isinstance(parameters, dict) or not parameters:
- return scrubbed_secrets
- scrubbed = app.AGENT_FUNCTION.redact_codeblock_parameter_values(scrubbed_secrets, parameters)
- if not isinstance(scrubbed, dict) or not _mapping_keys_preserved(scrubbed_secrets, scrubbed):
- return {}
- if type(scrubbed_secrets.get("ok")) is bool:
- scrubbed["ok"] = scrubbed_secrets["ok"]
+ if isinstance(parameters, dict) and parameters:
+ parameter_sets.append(parameters)
+ origin_parameters = matching_origin_run_redaction_parameters(ctx)
+ if origin_parameters:
+ parameter_sets.append(origin_parameters)
+
+ scrubbed = scrubbed_secrets
+ for parameter_set in parameter_sets:
+ candidate = app.AGENT_FUNCTION.redact_codeblock_parameter_values(scrubbed, parameter_set)
+ if not isinstance(candidate, dict) or not _mapping_keys_preserved(scrubbed, candidate):
+ return {}
+ if type(scrubbed.get("ok")) is bool:
+ candidate["ok"] = scrubbed["ok"]
+ scrubbed = candidate
return scrubbed
@@ -1848,7 +1862,16 @@ class SkyvernOverlayMCPServer(MCPServer):
# The adapter is the last disclosure boundary. Production custody changes share
# this call's outer lock; this gate also fails closed if an unexpected producer
# marks the exact session while an enrichment awaits.
- if overlay.requires_browser and sensitive_origin_page_is_tainted(copilot_ctx):
+ sensitive_result_is_scrubbable = (
+ overlay.redacts_sensitive_origin_structured_result
+ and not sensitive_origin_page_has_active_run(copilot_ctx)
+ and register_matching_origin_run_redaction_values(copilot_ctx)
+ )
+ if (
+ overlay.requires_browser
+ and sensitive_origin_page_is_tainted(copilot_ctx)
+ and not sensitive_result_is_scrubbable
+ ):
_restore_post_hook_context(copilot_ctx, ctx_snapshot)
copilot_result = {
"ok": False,
diff --git a/skyvern/forge/sdk/copilot/runtime.py b/skyvern/forge/sdk/copilot/runtime.py
index 8f1a875b6..f3ef901ae 100644
--- a/skyvern/forge/sdk/copilot/runtime.py
+++ b/skyvern/forge/sdk/copilot/runtime.py
@@ -84,15 +84,19 @@ def _immutable_redaction_value(value: Any) -> Any:
@dataclass(frozen=True)
class OriginRunRedactionRegistry:
- """Serialized parameter values owned by one concrete workflow run."""
+ """Run-bound redaction sets for model disclosures and persisted artifacts."""
workflow_run_id: str
parameters: Mapping[str, Any]
contains_sensitive_values: bool
contains_all_sensitive_values: bool
+ contains_all_static_sensitive_values: bool = True
+ awaiting_runtime_secret_values: bool = False
+ artifact_parameters: Mapping[str, Any] = field(default_factory=dict)
def __post_init__(self) -> None:
object.__setattr__(self, "parameters", _immutable_redaction_value(self.parameters))
+ object.__setattr__(self, "artifact_parameters", _immutable_redaction_value(self.artifact_parameters))
_SESSION_CLEANUP_TIMEOUT_SECONDS = 5.0
diff --git a/skyvern/forge/sdk/copilot/secret_scrub.py b/skyvern/forge/sdk/copilot/secret_scrub.py
index 9185885d2..2d3afae44 100644
--- a/skyvern/forge/sdk/copilot/secret_scrub.py
+++ b/skyvern/forge/sdk/copilot/secret_scrub.py
@@ -7,6 +7,7 @@ stay scrubbed on readbacks in later turns whose per-turn context is empty.
from __future__ import annotations
+from collections.abc import Mapping
from typing import TYPE_CHECKING, Any
import structlog
@@ -65,6 +66,53 @@ def register_secret_scrub_value(ctx: AgentContext, value: str | None) -> None:
_SESSION_SCRUB_VALUES.pop(next(iter(_SESSION_SCRUB_VALUES)))
+def register_secret_scrub_values_from_structure(ctx: AgentContext, obj: Any) -> None:
+ """Register string leaves without treating field names as secret values."""
+ if isinstance(obj, str):
+ register_secret_scrub_value(ctx, obj)
+ return
+ if isinstance(obj, Mapping):
+ for value in obj.values():
+ register_secret_scrub_values_from_structure(ctx, value)
+ return
+ if isinstance(obj, (list, tuple, set, frozenset)):
+ for value in obj:
+ register_secret_scrub_values_from_structure(ctx, value)
+
+
+def matching_origin_run_redaction_parameters(ctx: AgentContext, run_id: str | None = None) -> dict[str, Any] | None:
+ """Return a mutable copy of the complete, run-bound model-disclosure scrub set."""
+ expected_run_id = run_id or getattr(ctx, "last_run_blocks_workflow_run_id", None)
+ registry = getattr(ctx, "origin_run_redaction_registry", None)
+ if (
+ not isinstance(expected_run_id, str)
+ or not expected_run_id
+ or registry is None
+ or registry.workflow_run_id != expected_run_id
+ or not registry.contains_sensitive_values
+ or not registry.contains_all_sensitive_values
+ ):
+ return None
+
+ def mutable(value: Any) -> Any:
+ if isinstance(value, Mapping):
+ return {key: mutable(item) for key, item in value.items()}
+ if isinstance(value, (list, tuple, set, frozenset)):
+ return [mutable(item) for item in value]
+ return value
+
+ return mutable(registry.parameters)
+
+
+def register_matching_origin_run_redaction_values(ctx: AgentContext, run_id: str | None = None) -> bool:
+ """Bind the terminal origin run's known values to the existing exact-value scrubber."""
+ parameters = matching_origin_run_redaction_parameters(ctx, run_id)
+ if parameters is None:
+ return False
+ register_secret_scrub_values_from_structure(ctx, parameters)
+ return True
+
+
def clear_session_scrub_values(session_id: str | None) -> None:
if isinstance(session_id, str):
_SESSION_SCRUB_VALUES.pop(session_id, None)
diff --git a/skyvern/forge/sdk/copilot/tools/composition_capture.py b/skyvern/forge/sdk/copilot/tools/composition_capture.py
index 7c7d4014d..933b0fa62 100644
--- a/skyvern/forge/sdk/copilot/tools/composition_capture.py
+++ b/skyvern/forge/sdk/copilot/tools/composition_capture.py
@@ -52,6 +52,10 @@ from skyvern.forge.sdk.copilot.screenshot_utils import (
enqueue_screenshot,
screenshot_result_facts,
)
+from skyvern.forge.sdk.copilot.secret_scrub import (
+ register_matching_origin_run_redaction_values,
+ scrub_secrets_from_structure,
+)
from skyvern.forge.sdk.copilot.tracing_setup import copilot_span
from ._shared import (
@@ -854,24 +858,7 @@ async def _inspect_page_for_composition_under_custody(
use_current_page = (target_url or "").strip().lower() in _CURRENT_PAGE_INSPECTION_TARGETS
run_id = getattr(copilot_ctx, "last_run_blocks_workflow_run_id", None)
- origin_registry = getattr(copilot_ctx, "origin_run_redaction_registry", None)
- sensitive_same_turn_run = (
- isinstance(run_id, str)
- and origin_registry is not None
- and origin_registry.workflow_run_id == run_id
- and origin_registry.contains_sensitive_values
- )
- if use_current_page and sensitive_same_turn_run:
- result = {
- "ok": False,
- "data": None,
- "error": (
- "Current-page inspection is unavailable after a run with sensitive inputs. "
- "Inspect a specific named URL instead; that route performs a fresh navigation before inspection."
- ),
- }
- record_tool_step_result_for_ctx(copilot_ctx, "inspect_page_for_composition", arguments, result)
- return result
+ sensitive_same_turn_run = register_matching_origin_run_redaction_values(copilot_ctx, run_id)
if not use_current_page:
_clear_pending_browser_interaction_observation(copilot_ctx)
bypass_budget_for_post_run_current_page = _allows_post_run_current_page_inspection_budget_bypass(
@@ -990,7 +977,7 @@ async def _inspect_page_for_composition_under_custody(
)
evidence, observation_error, visual_fallback_frame = _capture_result_parts(capture)
- if sensitive_origin_page_is_tainted(copilot_ctx):
+ if sensitive_origin_page_is_tainted(copilot_ctx) and not sensitive_same_turn_run:
result = {"ok": False, "data": None, "error": SENSITIVE_ORIGIN_PAGE_ERROR}
record_tool_step_result_for_ctx(copilot_ctx, "inspect_page_for_composition", arguments, result)
return result
@@ -1030,6 +1017,11 @@ async def _inspect_page_for_composition_under_custody(
record_tool_step_result_for_ctx(copilot_ctx, "inspect_page_for_composition", arguments, result)
return result
+ if sensitive_same_turn_run:
+ evidence = scrub_secrets_from_structure(copilot_ctx, evidence)
+ # Exact-value scrubbing applies to structured evidence, not pixels.
+ visual_fallback_frame = None
+
if isinstance(run_id, str) and run_id:
session_provenance = evidence.get("browser_session_provenance")
mixed_session_provenance = isinstance(session_provenance, dict) and session_provenance.get("mixed") is True
diff --git a/skyvern/forge/sdk/copilot/tools/mcp_hooks.py b/skyvern/forge/sdk/copilot/tools/mcp_hooks.py
index a134b540b..9e9c15171 100644
--- a/skyvern/forge/sdk/copilot/tools/mcp_hooks.py
+++ b/skyvern/forge/sdk/copilot/tools/mcp_hooks.py
@@ -58,7 +58,11 @@ from skyvern.forge.sdk.copilot.runtime import (
sensitive_origin_page_is_tainted,
)
from skyvern.forge.sdk.copilot.secret_redaction import redact_raw_secrets_for_prompt
-from skyvern.forge.sdk.copilot.secret_scrub import registered_scrub_values
+from skyvern.forge.sdk.copilot.secret_scrub import (
+ register_matching_origin_run_redaction_values,
+ registered_scrub_values,
+ scrub_secrets_from_structure,
+)
from skyvern.forge.sdk.schemas.credentials import Credential
from skyvern.schemas.workflows import TaskBlockYAML
@@ -647,7 +651,12 @@ async def _evaluate_pre_hook(
# call's post-hook to consume.
ctx.pending_scout_read_expression = None
ctx.pending_scout_read_output_path = None
- sensitive_page_refusal = _sensitive_origin_page_refusal(ctx)
+ matching_registry_is_scrubbable = register_matching_origin_run_redaction_values(ctx)
+ sensitive_page_refusal = None
+ if sensitive_origin_page_has_active_run(ctx) or (
+ sensitive_origin_page_is_tainted(ctx) and not matching_registry_is_scrubbable
+ ):
+ sensitive_page_refusal = _sensitive_origin_page_refusal(ctx)
if sensitive_page_refusal is not None:
return sensitive_page_refusal
raw_expression = params.get("expression")
@@ -1505,11 +1514,18 @@ async def _evaluate_post_hook(
ctx: AgentContext,
) -> dict[str, Any]:
ctx.scout_observation_contract = None
- sensitive_page_refusal = _sensitive_origin_page_refusal(ctx)
+ matching_registry_is_scrubbable = register_matching_origin_run_redaction_values(ctx)
+ sensitive_page_refusal = None
+ if sensitive_origin_page_has_active_run(ctx) or (
+ sensitive_origin_page_is_tainted(ctx) and not matching_registry_is_scrubbable
+ ):
+ sensitive_page_refusal = _sensitive_origin_page_refusal(ctx)
if sensitive_page_refusal is not None:
ctx.pending_scout_read_expression = None
ctx.pending_scout_read_output_path = None
return sensitive_page_refusal
+ if matching_registry_is_scrubbable:
+ result = scrub_secrets_from_structure(ctx, result)
data = result.get("data")
if not result.get("ok") or not isinstance(data, dict) or not data:
return result
@@ -1923,6 +1939,7 @@ def _build_skyvern_mcp_overlays(
BROWSER_TARGET_PARAM_NAME: BROWSER_TARGET_PARAM,
},
requires_browser=True,
+ redacts_sensitive_origin_structured_result=True,
timeout=30,
pre_hook=_evaluate_pre_hook,
post_hook=_evaluate_post_hook,
diff --git a/skyvern/forge/sdk/copilot/tools/run_execution.py b/skyvern/forge/sdk/copilot/tools/run_execution.py
index 412a5e450..4cd574273 100644
--- a/skyvern/forge/sdk/copilot/tools/run_execution.py
+++ b/skyvern/forge/sdk/copilot/tools/run_execution.py
@@ -143,13 +143,17 @@ from skyvern.forge.sdk.copilot.screenshot_utils import (
enqueue_screenshot,
)
from skyvern.forge.sdk.copilot.secret_redaction import redact_raw_secrets_for_prompt
-from skyvern.forge.sdk.copilot.secret_scrub import scrub_secrets_from_structure
+from skyvern.forge.sdk.copilot.secret_scrub import (
+ register_matching_origin_run_redaction_values,
+ register_secret_scrub_values_from_structure,
+ scrub_secrets_from_structure,
+)
from skyvern.forge.sdk.copilot.tracing_setup import copilot_span
from skyvern.forge.sdk.copilot.turn_halt import stash_turn_halt_from_blocker_signal
from skyvern.forge.sdk.copilot.workflow_yaml import _process_workflow_yaml, runner_code_block_associations
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.executor.factory import AsyncExecutorFactory
-from skyvern.forge.sdk.schemas.credentials import CredentialVaultType, PasswordCredential
+from skyvern.forge.sdk.schemas.credentials import CredentialVaultType
from skyvern.forge.sdk.schemas.workflow_copilot import (
WorkflowCopilotRunOutcomeUpdate,
WorkflowCopilotRunStartedUpdate,
@@ -168,6 +172,7 @@ from skyvern.forge.sdk.workflow.models.parameter import (
)
from skyvern.forge.sdk.workflow.models.workflow import Workflow, WorkflowRun, WorkflowRunStatus
from skyvern.forge.sdk.workflow.runtime_completion import contract_from_request_criteria
+from skyvern.forge.sdk.workflow.runtime_secret_bridge import consume_copilot_runtime_secret_values
from skyvern.forge.sdk.workflow.service import run_selection_is_partial
from skyvern.schemas.workflows import BlockStatus, BlockType
from skyvern.utils.files import initialize_skyvern_state_file
@@ -1814,14 +1819,23 @@ async def _capture_and_store_post_run_page(
) -> BuildTestPacketPageCapture:
"""A failed or hollow capture neutralizes stale evidence to None only when it would not cleanly match
this run_id, so the matcher's destructive clear cannot fire on the pending failure-string context."""
+ sensitive_origin_run = register_matching_origin_run_redaction_values(ctx, run_id)
registry = ctx.origin_run_redaction_registry
- if registry is not None and registry.workflow_run_id == run_id and registry.contains_sensitive_values:
+ if (
+ registry is not None
+ and registry.workflow_run_id == run_id
+ and registry.contains_sensitive_values
+ and not sensitive_origin_run
+ ):
ctx.composition_page_evidence = None
return BuildTestPacketPageCapture(status="unavailable", omission="page_capture_unavailable")
evidence, observed_session_id, _, captured_frame = await _read_run_session_page_evidence(
ctx, run_session_id=run_session_id, current_url=current_url
)
if evidence is not None and repair_page_evidence_is_admissible(evidence):
+ if sensitive_origin_run:
+ evidence = scrub_secrets_from_structure(ctx, evidence)
+ captured_frame = None
_, preserved_stored_evidence = store_post_run_page_evidence(
ctx,
evidence,
@@ -1937,6 +1951,9 @@ async def _fetch_dispatched_terminal_page_evidence(
else None
)
origin_redaction_parameters = matching_registry.parameters if matching_registry is not None else None
+ artifact_redaction_parameters = None
+ if matching_registry is not None:
+ artifact_redaction_parameters = matching_registry.artifact_parameters or matching_registry.parameters
if workflow is None or (
_workflow_requires_terminal_artifact_redaction(workflow)
and (
@@ -1974,9 +1991,9 @@ async def _fetch_dispatched_terminal_page_evidence(
raw_html = artifact_bytes.decode("utf-8", errors="ignore")[:_MAX_POST_RUN_PAGE_HTML_CHARS]
scrubbed_html = (
app.AGENT_FUNCTION.redact_codeblock_parameter_values(
- raw_html, _mutable_redaction_value(origin_redaction_parameters)
+ raw_html, _mutable_redaction_value(artifact_redaction_parameters)
)
- if origin_redaction_parameters
+ if artifact_redaction_parameters
else raw_html
)
if not isinstance(scrubbed_html, str) or not scrubbed_html:
@@ -2161,8 +2178,16 @@ async def _bind_origin_run_redaction_registry(
credential_ids: Sequence[str],
sensitive_parameter_keys: Sequence[str],
) -> OriginRunRedactionRegistry:
- serialized = app.AGENT_FUNCTION.serialize_codeblock_parameters(dict(parameter_values))
+ # The model-disclosure registry is a sensitive-value scrub set, not a copy of the run request.
+ # Persisted worker HTML retains the pre-existing, broader all-parameter redaction set without
+ # registering capability references at the workflow write seam.
+ serialized_artifact_parameters = app.AGENT_FUNCTION.serialize_codeblock_parameters(dict(parameter_values))
+ sensitive_parameter_values = {
+ key: parameter_values[key] for key in sensitive_parameter_keys if key in parameter_values
+ }
+ serialized = app.AGENT_FUNCTION.serialize_codeblock_parameters(sensitive_parameter_values)
sensitive_values_complete = all(key in parameter_values and key in serialized for key in sensitive_parameter_keys)
+ awaiting_runtime_secret_values = False
credential_parameters: dict[str, Any] = {}
for index, credential_id in enumerate(credential_ids):
try:
@@ -2185,12 +2210,20 @@ async def _bind_origin_run_redaction_registry(
credential_item=credential_item,
)
credential = credential_item.credential
- if isinstance(credential, PasswordCredential) and (
- credential.totp or credential.totp_identifier or db_credential.totp_identifier
- ):
- sensitive_values_complete = False
- continue
- credential_parameters[f"copilot_run_credential_{index}"] = credential.model_dump(exclude_none=True)
+ # TOTP routing metadata identifies a retrieval capability; it is not the runtime OTP.
+ # Registering it as a secret value can corrupt ordinary generated code (for example,
+ # an identifier named ``totp_input``) before the workflow write seam sees it.
+ credential_parameters[f"copilot_run_credential_{index}"] = credential.model_dump(
+ exclude_none=True,
+ exclude={"totp_identifier", "totp_type"},
+ )
+ # A code block can mint or poll an OTP after parameter binding. Keep disclosure closed
+ # until the terminal run context has supplied the exact values it actually generated.
+ awaiting_runtime_secret_values = awaiting_runtime_secret_values or bool(
+ getattr(db_credential, "totp_identifier", None)
+ or getattr(credential, "totp", None)
+ or getattr(credential, "totp_identifier", None)
+ )
except Exception:
sensitive_values_complete = False
LOG.warning(
@@ -2204,16 +2237,58 @@ async def _bind_origin_run_redaction_registry(
if set(serialized_credentials) != set(credential_parameters):
sensitive_values_complete = False
serialized.update(serialized_credentials)
+ serialized_artifact_parameters.update(serialized_credentials)
registry = OriginRunRedactionRegistry(
workflow_run_id=workflow_run_id,
parameters=dict(serialized),
contains_sensitive_values=bool(sensitive_parameter_keys or credential_ids),
- contains_all_sensitive_values=sensitive_values_complete,
+ contains_all_sensitive_values=sensitive_values_complete and not awaiting_runtime_secret_values,
+ contains_all_static_sensitive_values=sensitive_values_complete,
+ awaiting_runtime_secret_values=awaiting_runtime_secret_values,
+ artifact_parameters=dict(serialized_artifact_parameters),
)
ctx.origin_run_redaction_registry = registry
+ if registry.contains_sensitive_values:
+ register_secret_scrub_values_from_structure(ctx, registry.parameters)
return registry
+async def _complete_origin_run_redaction_registry_from_runtime(
+ ctx: CopilotContext,
+ workflow_run_id: str,
+) -> OriginRunRedactionRegistry | None:
+ """Import the terminal run's exact runtime secrets before admitting structured page reads."""
+ registry = getattr(ctx, "origin_run_redaction_registry", None)
+ if registry is None or registry.workflow_run_id != workflow_run_id or not registry.awaiting_runtime_secret_values:
+ return registry
+
+ runtime_secret_values = await consume_copilot_runtime_secret_values(
+ organization_id=ctx.organization_id,
+ workflow_run_id=workflow_run_id,
+ )
+ if runtime_secret_values is None:
+ return registry
+
+ parameters = dict(registry.parameters)
+ artifact_parameters = dict(registry.artifact_parameters or registry.parameters)
+ if runtime_secret_values:
+ parameters["copilot_run_runtime_secret_values"] = tuple(sorted(runtime_secret_values))
+ artifact_parameters["copilot_run_runtime_secret_values"] = tuple(sorted(runtime_secret_values))
+ completed = OriginRunRedactionRegistry(
+ workflow_run_id=registry.workflow_run_id,
+ parameters=parameters,
+ contains_sensitive_values=registry.contains_sensitive_values,
+ contains_all_sensitive_values=registry.contains_all_static_sensitive_values,
+ contains_all_static_sensitive_values=registry.contains_all_static_sensitive_values,
+ awaiting_runtime_secret_values=False,
+ artifact_parameters=artifact_parameters,
+ )
+ ctx.origin_run_redaction_registry = completed
+ if completed.contains_all_sensitive_values:
+ register_secret_scrub_values_from_structure(ctx, completed.parameters)
+ return completed
+
+
def terminal_ready_for_latch(
*,
current_workflow_labels: list[str],
@@ -3168,6 +3243,7 @@ async def _run_blocks_and_collect_debug(
ctx.last_run_blocks_block_labels = list(dict.fromkeys(block.label for block in run_block_rows if block.label))
await _attach_action_traces(blocks, results, ctx.organization_id, include_completed=True)
+ await _complete_origin_run_redaction_registry_from_runtime(ctx, workflow_run.workflow_run_id)
recorded_origin_registry = ctx.origin_run_redaction_registry
sensitive_origin_run = _origin_registry_contains_sensitive_values(
recorded_origin_registry,
@@ -3238,23 +3314,24 @@ async def _run_blocks_and_collect_debug(
locator_observations: list[AuthoredLocatorObservationRow] | None = None
post_run_page_capture: BuildTestPacketPageCapture | None = None
- if not sensitive_origin_run:
- if (
- not dispatch_to_worker
- and run_session_id
- and _copilot_block_authoring_policy(ctx) == BlockAuthoringPolicy.CODE_ONLY_BROWSER
- and not ctx.copilot_total_timeout_exceeded
- ):
- # CDP capture against the run session: worker-owned for dispatched runs, so skip it.
- # A dispatched run therefore carries no locator observation, and the packet says so.
- _pin_pre_run_page_reference(ctx, workflow_run.workflow_run_id)
- post_run_page_capture = await _capture_and_store_post_run_page(
- ctx,
- run_session_id=run_session_id,
- run_id=workflow_run.workflow_run_id,
- current_url=current_url,
- )
+ if (
+ not dispatch_to_worker
+ and run_session_id
+ and _copilot_block_authoring_policy(ctx) == BlockAuthoringPolicy.CODE_ONLY_BROWSER
+ and not ctx.copilot_total_timeout_exceeded
+ ):
+ # Structured evidence is admitted through the origin registry scrubber. Pixel capture
+ # and locator probes remain withheld for sensitive runs because they have no equivalent
+ # exact-value disclosure boundary.
+ _pin_pre_run_page_reference(ctx, workflow_run.workflow_run_id)
+ post_run_page_capture = await _capture_and_store_post_run_page(
+ ctx,
+ run_session_id=run_session_id,
+ run_id=workflow_run.workflow_run_id,
+ current_url=current_url,
+ )
+ if not sensitive_origin_run:
locator_observations = await _observe_authored_locators(
ctx,
run_session_id=run_session_id,
@@ -3408,7 +3485,7 @@ async def _run_blocks_and_collect_debug(
async def _get_run_results(
params: dict[str, Any],
- ctx: AgentContext,
+ ctx: CopilotContext,
*,
read_live_page: bool = True,
admit_sensitive_origin_artifact: bool = True,
@@ -3450,6 +3527,8 @@ async def _get_run_results(
return {"ok": False, "error": f"Workflow run not found: {workflow_run_id}"}
if getattr(run, "workflow_permanent_id", None) != ctx.workflow_permanent_id:
return {"ok": False, "error": f"Workflow run not found for this workflow: {workflow_run_id}"}
+ if WorkflowRunStatus(run.status).is_final():
+ await _complete_origin_run_redaction_registry_from_runtime(ctx, workflow_run_id)
run_browser_session_id = getattr(run, "browser_session_id", None)
if isinstance(run_browser_session_id, str) and run_browser_session_id and WorkflowRunStatus(run.status).is_final():
async with browser_page_custody_lock(ctx, session_id=run_browser_session_id):
diff --git a/skyvern/forge/sdk/workflow/runtime_secret_bridge.py b/skyvern/forge/sdk/workflow/runtime_secret_bridge.py
new file mode 100644
index 000000000..1898372e9
--- /dev/null
+++ b/skyvern/forge/sdk/workflow/runtime_secret_bridge.py
@@ -0,0 +1,136 @@
+"""Short-lived worker-to-API handoff for Copilot origin-run secret scrubbing."""
+
+from __future__ import annotations
+
+import asyncio
+import json
+from collections.abc import AsyncIterator
+from contextlib import asynccontextmanager
+from dataclasses import dataclass, field
+from typing import Any
+
+import structlog
+
+from skyvern.forge import app
+from skyvern.forge.sdk.encrypt import encryptor
+from skyvern.forge.sdk.encrypt.base import EncryptMethod
+from skyvern.utils.secret_redaction import collect_redactable_secret_values
+
+LOG = structlog.get_logger()
+
+_CACHE_TTL_SECONDS = 600
+_CACHE_PREFIX = "copilot:origin-run-runtime-secrets:v1"
+_ENCRYPTED_PREFIX = "aes:"
+_LOCAL_PREFIX = "local:"
+_CLEAR_SENTINEL = "cleared"
+_READ_ATTEMPTS = 9
+_READ_INTERVAL_SECONDS = 0.25
+
+
+@dataclass
+class _LocalLockEntry:
+ lock: asyncio.Lock = field(default_factory=asyncio.Lock)
+ users: int = 0
+
+
+_LOCAL_LOCKS: dict[tuple[asyncio.AbstractEventLoop, str], _LocalLockEntry] = {}
+
+
+def _cache_key(organization_id: str, workflow_run_id: str) -> str:
+ return f"{_CACHE_PREFIX}:{organization_id}:{workflow_run_id}"
+
+
+@asynccontextmanager
+async def _consume_lock(key: str) -> AsyncIterator[None]:
+ local_key = (asyncio.get_running_loop(), key)
+ entry = _LOCAL_LOCKS.setdefault(local_key, _LocalLockEntry())
+ entry.users += 1
+ try:
+ async with entry.lock:
+ async with app.CACHE.get_lock(f"{key}:consume", blocking_timeout=5, timeout=10):
+ yield
+ finally:
+ entry.users -= 1
+ if entry.users == 0 and _LOCAL_LOCKS.get(local_key) is entry:
+ _LOCAL_LOCKS.pop(local_key, None)
+
+
+async def publish_copilot_runtime_secret_values(
+ *,
+ organization_id: str,
+ workflow_run_id: str,
+ workflow_run_context: Any,
+) -> bool:
+ """Publish exact terminal values; shared caches receive ciphertext only."""
+ # Static credential values are already bound to the origin-run registry before dispatch.
+ # Bridge only values minted at runtime: ``secrets`` also contains routing metadata such as
+ # ``totp_identifier``, which is a capability reference and must never become a scrub value.
+ values = collect_redactable_secret_values({}, otp_values=workflow_run_context.runtime_otp_values)
+ payload = json.dumps(
+ {
+ "organization_id": organization_id,
+ "workflow_run_id": workflow_run_id,
+ "values": sorted(values),
+ },
+ separators=(",", ":"),
+ )
+ try:
+ if app.CACHE.is_shared:
+ stored = _ENCRYPTED_PREFIX + await encryptor.encrypt(payload, EncryptMethod.AES)
+ else:
+ stored = _LOCAL_PREFIX + payload
+ await app.CACHE.set(_cache_key(organization_id, workflow_run_id), stored, ex=_CACHE_TTL_SECONDS)
+ except Exception:
+ LOG.warning(
+ "Copilot origin-run runtime secrets could not be handed off",
+ organization_id=organization_id,
+ workflow_run_id=workflow_run_id,
+ exc_info=True,
+ )
+ return False
+ return True
+
+
+async def consume_copilot_runtime_secret_values(
+ *,
+ organization_id: str,
+ workflow_run_id: str,
+) -> set[str] | None:
+ """Consume a matching handoff once; None means disclosure must remain closed."""
+ key = _cache_key(organization_id, workflow_run_id)
+ try:
+ async with _consume_lock(key):
+ stored: Any = None
+ for attempt in range(_READ_ATTEMPTS):
+ stored = await app.CACHE.get(key)
+ if stored is not None:
+ break
+ if attempt + 1 < _READ_ATTEMPTS:
+ await asyncio.sleep(_READ_INTERVAL_SECONDS)
+ if not isinstance(stored, str) or stored == _CLEAR_SENTINEL:
+ return None
+ if stored.startswith(_ENCRYPTED_PREFIX):
+ payload = await encryptor.decrypt(stored[len(_ENCRYPTED_PREFIX) :], EncryptMethod.AES)
+ elif stored.startswith(_LOCAL_PREFIX) and not app.CACHE.is_shared:
+ payload = stored[len(_LOCAL_PREFIX) :]
+ else:
+ return None
+ decoded = json.loads(payload)
+ if (
+ not isinstance(decoded, dict)
+ or decoded.get("organization_id") != organization_id
+ or decoded.get("workflow_run_id") != workflow_run_id
+ or not isinstance(decoded.get("values"), list)
+ or any(not isinstance(value, str) for value in decoded["values"])
+ ):
+ return None
+ await app.CACHE.set(key, _CLEAR_SENTINEL, ex=1)
+ return set(decoded["values"])
+ except Exception:
+ LOG.warning(
+ "Copilot origin-run runtime secret handoff could not be consumed",
+ organization_id=organization_id,
+ workflow_run_id=workflow_run_id,
+ exc_info=True,
+ )
+ return None
diff --git a/skyvern/forge/sdk/workflow/service.py b/skyvern/forge/sdk/workflow/service.py
index 37e29c9a8..5820e57e8 100644
--- a/skyvern/forge/sdk/workflow/service.py
+++ b/skyvern/forge/sdk/workflow/service.py
@@ -186,6 +186,7 @@ from skyvern.forge.sdk.workflow.runtime_completion import (
grade_completion_contract,
parse_completion_contract,
)
+from skyvern.forge.sdk.workflow.runtime_secret_bridge import publish_copilot_runtime_secret_values
from skyvern.forge.sdk.workflow.secret_encryption import encrypt_workflow_definition_secrets
from skyvern.forge.sdk.workflow.sequential_key import (
REUSE_ADMISSION_OFF_DISABLED,
@@ -245,6 +246,7 @@ from skyvern.services.webhook_delivery import (
describe_delivery_error,
)
from skyvern.services.workflow_script_service import BLOCK_TYPES_THAT_SHOULD_BE_CACHED
+from skyvern.utils.contained_effects import contained_effect
from skyvern.utils.css_selector import build_action_summaries_with_timing # shared with script_service
from skyvern.utils.secret_headers import merge_masked_headers
from skyvern.utils.secret_redaction import redact_console_log_bytes, redact_har_bytes
@@ -5484,6 +5486,20 @@ class WorkflowService:
exc_info=True,
)
+ if getattr(workflow_run, "copilot_session_id", None):
+ with contained_effect(
+ "publish Copilot origin-run runtime secrets",
+ workflow_run_id=workflow_run.workflow_run_id,
+ ):
+ workflow_run_context = app.WORKFLOW_CONTEXT_MANAGER.get_workflow_run_context(
+ workflow_run.workflow_run_id
+ )
+ await publish_copilot_runtime_secret_values(
+ organization_id=workflow_run.organization_id,
+ workflow_run_id=workflow_run.workflow_run_id,
+ workflow_run_context=workflow_run_context,
+ )
+
if renewal_task is not None and not renewal_task.done():
renewal_task.cancel()
try:
diff --git a/tests/unit/test_copilot_diagnosis_repair_contract.py b/tests/unit/test_copilot_diagnosis_repair_contract.py
index 8bb137043..5c6400724 100644
--- a/tests/unit/test_copilot_diagnosis_repair_contract.py
+++ b/tests/unit/test_copilot_diagnosis_repair_contract.py
@@ -49,6 +49,7 @@ from skyvern.forge.sdk.copilot.request_policy import RequestPolicy
from skyvern.forge.sdk.copilot.run_outcome import (
RecordedRunOutcome,
)
+from skyvern.forge.sdk.copilot.runtime import OriginRunRedactionRegistry
from skyvern.forge.sdk.copilot.runtime_authoring_repair import (
OBSTRUCTION_SUMMARY_MAX_CHARS,
finalize_runtime_authoring_repair_context_from_page_observation,
@@ -2683,6 +2684,74 @@ async def test_bounded_seam_capture_is_stored_stamped_without_touching_budget(
assert ctx.browser_session_id is None
+async def test_sensitive_post_run_capture_redacts_registry_values_and_keeps_structure(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ ctx = _ctx()
+ ctx.origin_run_redaction_registry = OriginRunRedactionRegistry(
+ "wr_sensitive",
+ {
+ "credential": {"username": "private-user", "password": "private-pass"},
+ "copilot_run_runtime_secret_values": ("654321",),
+ },
+ contains_sensitive_values=True,
+ contains_all_sensitive_values=True,
+ )
+
+ async def fake_read(
+ _ctx: CopilotContext, *, run_session_id: str, current_url: str
+ ) -> tuple[dict[str, object], str, None, None]:
+ evidence = _bounded_failure_page_evidence()
+ evidence["forms"] = [{"fields": [{"label": "Verification code", "selector": "#totp", "value": "654321"}]}]
+ return evidence, run_session_id, None, None
+
+ monkeypatch.setattr(run_execution_module, "_read_run_session_page_evidence", fake_read)
+
+ capture = await run_execution_module._capture_and_store_post_run_page(
+ ctx,
+ run_session_id="run_session",
+ run_id="wr_sensitive",
+ current_url="https://example.test/otp",
+ )
+
+ assert capture.status == "captured"
+ stored = ctx.composition_page_evidence
+ assert isinstance(stored, dict)
+ assert stored["forms"][0]["fields"][0]["selector"] == "#totp"
+ assert "private-user" not in json.dumps(stored)
+ assert "private-pass" not in json.dumps(stored)
+ assert "654321" not in json.dumps(stored)
+ assert "[REDACTED_SECRET]" in json.dumps(stored)
+
+
+async def test_sensitive_post_run_capture_stays_withheld_when_registry_is_incomplete(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ ctx = _ctx()
+ ctx.composition_page_evidence = _bounded_failure_page_evidence()
+ ctx.origin_run_redaction_registry = OriginRunRedactionRegistry(
+ "wr_sensitive",
+ {"credential": {"username": "private-user"}},
+ contains_sensitive_values=True,
+ contains_all_sensitive_values=False,
+ )
+ read = AsyncMock()
+ monkeypatch.setattr(run_execution_module, "_read_run_session_page_evidence", read)
+
+ capture = await run_execution_module._capture_and_store_post_run_page(
+ ctx,
+ run_session_id="run_session",
+ run_id="wr_sensitive",
+ current_url="https://example.test/otp",
+ )
+
+ assert capture.status == "unavailable"
+ assert ctx.composition_page_evidence is None
+ read.assert_not_awaited()
+
+
@pytest.mark.asyncio
async def test_post_run_capture_refused_for_a_foreign_session_reports_unavailable(
monkeypatch: pytest.MonkeyPatch,
@@ -2854,6 +2923,58 @@ async def test_post_run_current_page_inspect_is_sourced_from_the_run_session(
assert not any(entry.get("event") == "copilot_post_run_evidence_source_mismatch_refused" for entry in logs)
+async def test_sensitive_current_page_inspect_redacts_registry_values_and_keeps_fields(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ ctx = _post_run_inspect_ctx()
+ ctx.browser_session_id = "run_session"
+ ctx.last_run_blocks_browser_session_id = "run_session"
+ ctx.origin_run_redaction_registry = OriginRunRedactionRegistry(
+ "wr_failed",
+ {
+ "credential": {"username": "private-user", "password": "private-pass"},
+ "copilot_run_runtime_secret_values": ("654321",),
+ },
+ contains_sensitive_values=True,
+ contains_all_sensitive_values=True,
+ )
+ ctx.sensitive_origin_browser_session_ids = {"run_session"}
+ evidence = _bounded_failure_page_evidence()
+ evidence["forms"] = [{"fields": [{"label": "Verification code", "selector": "#totp", "value": "654321"}]}]
+
+ result = await _drive_inspect_page(monkeypatch, ctx, captured=evidence)
+
+ assert result["ok"] is True
+ assert result["data"]["forms"][0]["fields"][0]["label"] == "Verification code"
+ assert "private-user" not in json.dumps(result)
+ assert "private-pass" not in json.dumps(result)
+ assert "654321" not in json.dumps(result)
+ assert "[REDACTED_SECRET]" in json.dumps(result)
+
+
+async def test_sensitive_current_page_inspect_stays_withheld_while_origin_run_is_active(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ ctx = _post_run_inspect_ctx()
+ ctx.browser_session_id = "run_session"
+ ctx.last_run_blocks_browser_session_id = "run_session"
+ ctx.origin_run_redaction_registry = OriginRunRedactionRegistry(
+ "wr_failed",
+ {"credential": {"username": "private-user", "password": "private-pass"}},
+ contains_sensitive_values=True,
+ contains_all_sensitive_values=True,
+ )
+ ctx.sensitive_origin_browser_session_ids = {"run_session"}
+ ctx.active_sensitive_origin_browser_session_ids = {"run_session"}
+
+ result = await _drive_inspect_page(monkeypatch, ctx, captured=_bounded_failure_page_evidence())
+
+ assert result["ok"] is False
+ assert "specific named URL" in result["error"]
+
+
@pytest.mark.asyncio
async def test_post_run_inspect_drops_a_packet_whose_source_session_is_unprovable(
monkeypatch: pytest.MonkeyPatch,
diff --git a/tests/unit/test_copilot_fill_credential_field.py b/tests/unit/test_copilot_fill_credential_field.py
index 00d8c5157..c843527a4 100644
--- a/tests/unit/test_copilot_fill_credential_field.py
+++ b/tests/unit/test_copilot_fill_credential_field.py
@@ -416,6 +416,29 @@ class _FakePage:
return args[0] if args else None
+ ("field", "secret_value"),
+ [("totp", "123456"), ("password", "short-pass-13")],
+)
+async def test_the_readback_comes_from_the_page_not_the_tool_layer(
+ monkeypatch: pytest.MonkeyPatch, field: str, secret_value: str
+) -> None:
+ page = _FakePage()
+ _wire_impl(monkeypatch, page, secret_value=secret_value)
+ ctx = _ctx()
+
+ selector = f"#{field}"
+ result = await tools_module._fill_credential_field_impl(ctx, selector, "cred_123", field)
+
+ assert result["ok"] is True
+ assert result["data"]["readback_outcome"] == "exact_match"
+ # Through the tool layer a registered secret returns as `[REDACTED_SECRET]`, so only a
+ # read taken on the page handle that filled the field sees what was actually typed.
+ assert page.read_calls == [selector]
+ assert ctx.scout_trajectory[-1]["credential_field"] == field
+
+
def _wire_impl(
monkeypatch: pytest.MonkeyPatch,
page: _FakePage,
@@ -512,24 +535,6 @@ def _wire_impl(
)
assert mcp_hooks_module._scout_readback_outcome(otp, otp) is mcp_hooks_module.ScoutReadbackOutcome.EXACT_MATCH
- @pytest.mark.asyncio
- @pytest.mark.parametrize("secret_value", ["mk-one", "mk-two-value"])
- async def test_the_readback_comes_from_the_page_not_the_tool_layer(
- self, monkeypatch: pytest.MonkeyPatch, secret_value: str
- ) -> None:
- page = _FakePage()
- _wire_impl(monkeypatch, page, secret_value=secret_value)
- ctx = _ctx()
-
- result = await tools_module._fill_credential_field_impl(ctx, "#totp", "cred_123", "totp")
-
- assert result["ok"] is True
- assert result["data"]["readback_outcome"] == "exact_match"
- # Through the tool layer a registered secret returns as `[REDACTED_SECRET]`, so only a
- # read taken on the page handle that filled the field sees what was actually typed.
- assert page.read_calls == ["#totp"]
- assert ctx.scout_trajectory[-1]["credential_field"] == "totp"
-
@pytest.mark.asyncio
async def test_multi_match_selector_still_reaches_an_outcome(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""Reading the un-narrowed locator raises Playwright strict mode, which would leave the
diff --git a/tests/unit/test_copilot_mcp_adapter.py b/tests/unit/test_copilot_mcp_adapter.py
index 2a178f4c0..519a3ee78 100644
--- a/tests/unit/test_copilot_mcp_adapter.py
+++ b/tests/unit/test_copilot_mcp_adapter.py
@@ -5,6 +5,7 @@ from contextlib import asynccontextmanager
from types import SimpleNamespace
from typing import Any
from unittest.mock import MagicMock
+from urllib.parse import quote
import pytest
from mcp.types import CallToolResult
@@ -32,6 +33,7 @@ from skyvern.forge.sdk.copilot.runtime import (
AgentContext,
CopilotBrowserLivenessUndetermined,
CopilotBrowserSessionUnavailable,
+ OriginRunRedactionRegistry,
bound_call_browser_session,
browser_page_custody_lock,
mcp_to_copilot,
@@ -54,6 +56,49 @@ def _schema() -> dict:
}
+def test_scrub_tool_result_redacts_encoded_matching_origin_values(monkeypatch: pytest.MonkeyPatch) -> None:
+ secret = "[email protected]?token=one&next=/done"
+ encoded = quote(secret, safe="")
+ seen_parameters: list[dict[str, Any]] = []
+
+ def redact(value: Any, parameters: dict[str, Any]) -> Any:
+ seen_parameters.append(parameters)
+
+ def walk(node: Any) -> Any:
+ if isinstance(node, str):
+ return node.replace(encoded, "[redacted]")
+ if isinstance(node, dict):
+ return {key: walk(item) for key, item in node.items()}
+ return node
+
+ return walk(value)
+
+ ctx = make_copilot_ctx()
+ ctx.last_run_blocks_workflow_run_id = "wr_sensitive"
+ ctx.origin_run_redaction_registry = OriginRunRedactionRegistry(
+ "wr_sensitive",
+ {"magic_link": secret},
+ contains_sensitive_values=True,
+ contains_all_sensitive_values=True,
+ )
+ monkeypatch.setattr(
+ mcp_adapter.app,
+ "AGENT_FUNCTION",
+ SimpleNamespace(redact_codeblock_parameter_values=redact),
+ )
+
+ scrubbed = mcp_adapter._scrub_tool_result(
+ ctx,
+ {"ok": True, "data": {"url": f"https://example.test/callback?payload={encoded}"}},
+ )
+
+ assert scrubbed["ok"] is True
+ assert secret not in str(scrubbed)
+ assert encoded not in str(scrubbed)
+ assert "[redacted]" in str(scrubbed)
+ assert seen_parameters == [{"magic_link": secret}]
+
+
class TestRequestedOutputPathChoices:
def test_the_turn_s_requested_paths_become_the_choices(self) -> None:
# Live shape (SKY-13226): a free-form path let the model name its own purpose, so the read
@@ -893,6 +938,123 @@ class TestMCPToolTiming:
assert ctx.flow_evidence == [existing_flow]
assert ctx.scouted_output_covered_paths == {"output.existing"}
+ @pytest.mark.asyncio
+ async def test_evaluate_on_sensitive_origin_redacts_matching_run_registry_before_disclosure(
+ self, _stub_browser_session: None
+ ) -> None:
+ ctx = make_copilot_ctx(browser_session_id="pbs_1")
+ ctx.last_run_blocks_workflow_run_id = "wr_sensitive"
+ ctx.origin_run_redaction_registry = OriginRunRedactionRegistry(
+ "wr_sensitive",
+ {
+ "credential": {"username": "private-user", "password": "private-pass"},
+ "copilot_run_runtime_secret_values": ("654321",),
+ },
+ contains_sensitive_values=True,
+ contains_all_sensitive_values=True,
+ )
+ ctx.sensitive_origin_browser_session_ids.add("pbs_1")
+ overlay = _build_skyvern_mcp_overlays()["evaluate"]
+ server = _make_server(
+ ctx,
+ {
+ "ok": True,
+ "data": {
+ "result": {
+ "form": {"selector": "#otp-form", "field": "#totp"},
+ "echo": "private-user:private-pass:654321",
+ },
+ "url": "https://example.test/otp",
+ "title": "Verification for private-user",
+ },
+ },
+ overlay,
+ alias_map=get_skyvern_mcp_alias_map(),
+ )
+
+ result = await server.call_tool("evaluate", {"expression": "scan()"})
+
+ surfaced = json.loads(result.content[0].text)
+ assert surfaced["ok"] is True
+ assert surfaced["data"]["result"]["form"] == {"selector": "#otp-form", "field": "#totp"}
+ assert "private-user" not in result.content[0].text
+ assert "private-pass" not in result.content[0].text
+ assert "654321" not in result.content[0].text
+ assert "[REDACTED_SECRET]" in result.content[0].text
+ retained = json.dumps(
+ {
+ "composition_page_evidence": ctx.composition_page_evidence,
+ "flow_evidence": ctx.flow_evidence,
+ "scout_trajectory": ctx.scout_trajectory,
+ },
+ default=str,
+ )
+ assert "private-user" not in retained
+ assert "private-pass" not in retained
+ assert "654321" not in retained
+
+ @pytest.mark.asyncio
+ async def test_evaluate_on_sensitive_origin_stays_withheld_when_matching_registry_is_incomplete(
+ self, _stub_browser_session: None
+ ) -> None:
+ ctx = make_copilot_ctx(browser_session_id="pbs_1")
+ ctx.last_run_blocks_workflow_run_id = "wr_sensitive"
+ ctx.origin_run_redaction_registry = OriginRunRedactionRegistry(
+ "wr_sensitive",
+ {"credential": {"username": "private-user"}},
+ contains_sensitive_values=True,
+ contains_all_sensitive_values=False,
+ )
+ ctx.sensitive_origin_browser_session_ids.add("pbs_1")
+ server = _make_server(
+ ctx,
+ {
+ "ok": True,
+ "data": {
+ "result": "private-user:unregistered-pass",
+ "url": "https://example.test/otp",
+ },
+ },
+ _build_skyvern_mcp_overlays()["evaluate"],
+ alias_map=get_skyvern_mcp_alias_map(),
+ )
+
+ result = await server.call_tool("evaluate", {"expression": "scan()"})
+
+ surfaced = json.loads(result.content[0].text)
+ assert surfaced["ok"] is False
+ assert "specific named URL" in surfaced["error"]
+ assert "private-user" not in result.content[0].text
+ assert "unregistered-pass" not in result.content[0].text
+
+ @pytest.mark.asyncio
+ async def test_evaluate_stays_withheld_while_the_sensitive_origin_run_is_active(
+ self, _stub_browser_session: None
+ ) -> None:
+ ctx = make_copilot_ctx(browser_session_id="pbs_1")
+ ctx.last_run_blocks_workflow_run_id = "wr_sensitive"
+ ctx.origin_run_redaction_registry = OriginRunRedactionRegistry(
+ "wr_sensitive",
+ {"password": "private-pass"},
+ contains_sensitive_values=True,
+ contains_all_sensitive_values=True,
+ )
+ ctx.sensitive_origin_browser_session_ids.add("pbs_1")
+ ctx.active_sensitive_origin_browser_session_ids.add("pbs_1")
+ server = _make_server(
+ ctx,
+ {"ok": True, "data": {"result": "private-pass"}},
+ _build_skyvern_mcp_overlays()["evaluate"],
+ alias_map=get_skyvern_mcp_alias_map(),
+ )
+
+ result = await server.call_tool("evaluate", {"expression": "scan()"})
+
+ surfaced = json.loads(result.content[0].text)
+ assert surfaced["ok"] is False
+ assert "specific named URL" in surfaced["error"]
+ assert "private-pass" not in result.content[0].text
+
@pytest.mark.asyncio
@pytest.mark.parametrize(
("tool_name", "args"),
diff --git a/tests/unit/test_copilot_positive_evidence_carriers.py b/tests/unit/test_copilot_positive_evidence_carriers.py
index b57299b12..b96ea364d 100644
--- a/tests/unit/test_copilot_positive_evidence_carriers.py
+++ b/tests/unit/test_copilot_positive_evidence_carriers.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import asyncio
import os
from dataclasses import FrozenInstanceError
from datetime import datetime, timezone
@@ -37,6 +38,7 @@ from skyvern.forge.sdk.copilot.tools.credentials import (
_extract_credential_ids_from_workflow_definition,
)
from skyvern.forge.sdk.schemas.credentials import CredentialVaultType, PasswordCredential
+from skyvern.forge.sdk.workflow import runtime_secret_bridge
from tests.unit.copilot_test_helpers import (
DISPATCHED_NAV_ONLY_HTML,
make_completion_criterion,
@@ -547,11 +549,11 @@ def _snapshot_from_ctx(ctx: SimpleNamespace, run_id: str) -> RunEvidenceSnapshot
@pytest.mark.asyncio
-async def test_track_a_binds_serialized_redaction_registry_to_origin_run(monkeypatch: pytest.MonkeyPatch) -> None:
+async def test_track_a_excludes_nonsensitive_parameters_from_origin_registry(monkeypatch: pytest.MonkeyPatch) -> None:
ctx = _producer_ctx()
def serialize(values: dict[str, object]) -> dict[str, object]:
- return {"account": values["account"]}
+ return dict(values)
monkeypatch.setattr(
run_execution_module,
@@ -569,16 +571,52 @@ async def test_track_a_binds_serialized_redaction_registry_to_origin_run(monkeyp
assert registry == OriginRunRedactionRegistry(
"wr_origin",
- {
- "account": "private-value",
- },
+ {},
contains_sensitive_values=False,
contains_all_sensitive_values=True,
+ artifact_parameters={"account": "private-value"},
)
assert ctx.origin_run_redaction_registry is registry
assert ctx.codeblock_redaction_parameters == {}
+async def test_nonsensitive_origin_parameters_still_scrub_dispatched_terminal_html(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ parameter_value = "ordinary-run-parameter"
+ html = f"<html><body><p>{parameter_value}</p><p>safe evidence</p></body></html>"
+ artifacts = [_html_artifact("art_parameter", ArtifactType.HTML_ACTION)]
+ _stub_app(monkeypatch, artifacts, {"art_parameter": html.encode()})
+ seen_parameters: list[object] = []
+
+ def redact(value: object, parameters: object) -> object:
+ seen_parameters.append(parameters)
+ return value.replace(parameter_value, "[REDACTED]") if isinstance(value, str) else value
+
+ run_execution_module.app.AGENT_FUNCTION = SimpleNamespace(redact_codeblock_parameter_values=redact)
+ ctx = _producer_ctx()
+
+ result = await run_execution_module._fetch_dispatched_terminal_page_evidence(
+ run_id="wr_origin",
+ organization_id="o_1",
+ current_url="",
+ workflow=ctx.last_workflow,
+ origin_redaction_registry=OriginRunRedactionRegistry(
+ "wr_origin",
+ {},
+ contains_sensitive_values=False,
+ contains_all_sensitive_values=True,
+ artifact_parameters={"account": parameter_value},
+ ),
+ )
+
+ assert result is not None
+ assert seen_parameters == [{"account": parameter_value}]
+ assert parameter_value not in str(result)
+ assert "safe evidence" in page_evidence_prose_text(result)
+
+
def test_origin_run_redaction_registry_defensively_copies_and_exposes_parameters_immutably() -> None:
mutable = {"password": "origin-secret", "credential": {"fields": ["username", "password"]}}
registry = OriginRunRedactionRegistry(
@@ -647,6 +685,7 @@ async def test_track_a_registry_admits_complete_run_bound_static_credential_valu
) -> None:
ctx = _producer_ctx()
ctx.organization_id = "o_1"
+ ctx.secret_scrub_values = []
db_credential = SimpleNamespace(vault_type=None, totp_identifier=None)
credential = PasswordCredential(username="private-user", password="private-pass")
credential_item = SimpleNamespace(credential=credential)
@@ -674,8 +713,12 @@ async def test_track_a_registry_admits_complete_run_bound_static_credential_valu
)
assert registry.contains_all_sensitive_values is True
+ assert "cred_origin" not in registry.parameters.values()
+ assert "cred_origin" not in ctx.secret_scrub_values
assert registry.parameters["copilot_run_credential_0"]["username"] == "private-user"
assert registry.parameters["copilot_run_credential_0"]["password"] == "private-pass"
+ assert "private-user" in ctx.secret_scrub_values
+ assert "private-pass" in ctx.secret_scrub_values
process.assert_awaited_once_with(
workflow_run_id="wr_origin",
db_credential=db_credential,
@@ -683,6 +726,326 @@ async def test_track_a_registry_admits_complete_run_bound_static_credential_valu
)
+async def test_track_a_registry_keeps_static_credential_values_when_totp_is_dynamic(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ ctx = _producer_ctx()
+ ctx.organization_id = "o_1"
+ ctx.secret_scrub_values = []
+ db_credential = SimpleNamespace(vault_type=None, totp_identifier="totp_ref")
+ credential = PasswordCredential(username="private-user", password="private-pass", totp_identifier="totp_ref")
+ credential_item = SimpleNamespace(credential=credential)
+ service = SimpleNamespace(get_credential_item=AsyncMock(return_value=credential_item))
+ monkeypatch.setattr(
+ run_execution_module,
+ "app",
+ SimpleNamespace(
+ AGENT_FUNCTION=SimpleNamespace(
+ serialize_codeblock_parameters=lambda values: values,
+ process_registered_credential_item=AsyncMock(return_value=credential_item),
+ ),
+ DATABASE=SimpleNamespace(credentials=SimpleNamespace(get_credential=AsyncMock(return_value=db_credential))),
+ CREDENTIAL_VAULT_SERVICES={CredentialVaultType.BITWARDEN: service},
+ ),
+ )
+
+ registry = await run_execution_module._bind_origin_run_redaction_registry(
+ ctx,
+ workflow_run_id="wr_origin",
+ parameter_values={"login_credential": "cred_origin"},
+ credential_ids=["cred_origin"],
+ sensitive_parameter_keys=[],
+ )
+
+ assert registry.contains_all_sensitive_values is False
+ assert registry.contains_all_static_sensitive_values is True
+ assert registry.awaiting_runtime_secret_values is True
+ assert registry.parameters["copilot_run_credential_0"]["username"] == "private-user"
+ assert registry.parameters["copilot_run_credential_0"]["password"] == "private-pass"
+ assert "totp_identifier" not in registry.parameters["copilot_run_credential_0"]
+ assert "totp_ref" not in ctx.secret_scrub_values
+
+
+async def test_track_a_registry_imports_dispatched_runtime_otp_without_local_run_context(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ runtime_otp = "654321"
+ ctx = _producer_ctx()
+ ctx.organization_id = "o_1"
+ ctx.secret_scrub_values = []
+ ctx.origin_run_redaction_registry = OriginRunRedactionRegistry(
+ "wr_origin",
+ {"credential": {"username": "private-user", "password": "private-pass"}},
+ contains_sensitive_values=True,
+ contains_all_sensitive_values=False,
+ contains_all_static_sensitive_values=True,
+ awaiting_runtime_secret_values=True,
+ artifact_parameters={"account": "ordinary-run-parameter"},
+ )
+ consume = AsyncMock(return_value={runtime_otp})
+ monkeypatch.setattr(run_execution_module, "consume_copilot_runtime_secret_values", consume)
+
+ registry = await run_execution_module._complete_origin_run_redaction_registry_from_runtime(ctx, "wr_origin")
+
+ assert registry is not None
+ assert registry.contains_all_sensitive_values is True
+ assert registry.awaiting_runtime_secret_values is False
+ assert registry.parameters["copilot_run_runtime_secret_values"] == (runtime_otp,)
+ assert registry.artifact_parameters["account"] == "ordinary-run-parameter"
+ assert registry.artifact_parameters["copilot_run_runtime_secret_values"] == (runtime_otp,)
+ assert runtime_otp in ctx.secret_scrub_values
+ consume.assert_awaited_once_with(organization_id=ctx.organization_id, workflow_run_id="wr_origin")
+
+
+async def test_runtime_bridge_completion_excludes_totp_routing_metadata(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ values: dict[str, str] = {}
+
+ class FakeLocalCache:
+ is_shared = False
+
+ def get_lock(self, *_args: object, **_kwargs: object) -> asyncio.Lock:
+ return asyncio.Lock()
+
+ async def set(self, key: str, value: str, ex: int) -> None:
+ values[key] = value
+
+ async def get(self, key: str) -> str | None:
+ return values.get(key)
+
+ monkeypatch.setattr(runtime_secret_bridge, "app", SimpleNamespace(CACHE=FakeLocalCache()))
+ monkeypatch.setattr(
+ run_execution_module,
+ "consume_copilot_runtime_secret_values",
+ runtime_secret_bridge.consume_copilot_runtime_secret_values,
+ )
+ runtime_otp = "654321"
+ ctx = _producer_ctx()
+ ctx.organization_id = "o_1"
+ ctx.secret_scrub_values = ["private-user", "private-pass"]
+ ctx.origin_run_redaction_registry = OriginRunRedactionRegistry(
+ "wr_origin",
+ {"credential": {"username": "private-user", "password": "private-pass"}},
+ contains_sensitive_values=True,
+ contains_all_sensitive_values=False,
+ contains_all_static_sensitive_values=True,
+ awaiting_runtime_secret_values=True,
+ )
+ workflow_run_context = SimpleNamespace(
+ secrets={
+ "username": "private-user",
+ "password": "private-pass",
+ "totp_type": "authenticator",
+ "totp_identifier": "totp",
+ "runtime": runtime_otp,
+ },
+ runtime_otp_values={runtime_otp},
+ )
+
+ assert await runtime_secret_bridge.publish_copilot_runtime_secret_values(
+ organization_id=ctx.organization_id,
+ workflow_run_id="wr_origin",
+ workflow_run_context=workflow_run_context,
+ )
+ registry = await run_execution_module._complete_origin_run_redaction_registry_from_runtime(ctx, "wr_origin")
+
+ assert registry is not None
+ assert registry.parameters["credential"] == {"username": "private-user", "password": "private-pass"}
+ assert registry.parameters["copilot_run_runtime_secret_values"] == (runtime_otp,)
+ assert {"private-user", "private-pass", runtime_otp}.issubset(ctx.secret_scrub_values)
+ assert "totp" not in ctx.secret_scrub_values
+ assert "authenticator" not in ctx.secret_scrub_values
+
+
+async def test_runtime_secret_bridge_round_trips_exact_values_through_local_cache(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ values: dict[str, str] = {}
+
+ class FakeLocalCache:
+ is_shared = False
+
+ def get_lock(self, *_args: object, **_kwargs: object) -> asyncio.Lock:
+ return asyncio.Lock()
+
+ async def set(self, key: str, value: str, ex: int) -> None:
+ values[key] = value
+
+ async def get(self, key: str) -> str | None:
+ return values.get(key)
+
+ monkeypatch.setattr(runtime_secret_bridge, "app", SimpleNamespace(CACHE=FakeLocalCache()))
+ workflow_run_context = SimpleNamespace(
+ secrets={"password": "private-pass", "totp_identifier": "totp", "runtime": "654321"},
+ runtime_otp_values={"654321"},
+ )
+
+ published = await runtime_secret_bridge.publish_copilot_runtime_secret_values(
+ organization_id="o_1",
+ workflow_run_id="wr_origin",
+ workflow_run_context=workflow_run_context,
+ )
+ consumed = await runtime_secret_bridge.consume_copilot_runtime_secret_values(
+ organization_id="o_1",
+ workflow_run_id="wr_origin",
+ )
+
+ assert published is True
+ assert consumed == {"654321"}
+
+
+async def test_runtime_secret_bridge_encrypts_shared_cache_payload(monkeypatch: pytest.MonkeyPatch) -> None:
+ values: dict[str, str] = {}
+ plaintext: str | None = None
+
+ class FakeSharedCache:
+ is_shared = True
+
+ def get_lock(self, *_args: object, **_kwargs: object) -> asyncio.Lock:
+ return asyncio.Lock()
+
+ async def set(self, key: str, value: str, ex: int) -> None:
+ values[key] = value
+
+ async def get(self, key: str) -> str | None:
+ return values.get(key)
+
+ async def fake_encrypt(value: str, _method: object) -> str:
+ nonlocal plaintext
+ plaintext = value
+ return "opaque-ciphertext"
+
+ async def fake_decrypt(_value: str, _method: object) -> str:
+ assert plaintext is not None
+ return plaintext
+
+ monkeypatch.setattr(runtime_secret_bridge, "app", SimpleNamespace(CACHE=FakeSharedCache()))
+ monkeypatch.setattr(
+ runtime_secret_bridge,
+ "encryptor",
+ SimpleNamespace(encrypt=fake_encrypt, decrypt=fake_decrypt),
+ )
+ workflow_run_context = SimpleNamespace(
+ secrets={"password": "private-pass", "runtime": "654321"},
+ runtime_otp_values={"654321"},
+ )
+
+ assert await runtime_secret_bridge.publish_copilot_runtime_secret_values(
+ organization_id="o_1",
+ workflow_run_id="wr_origin",
+ workflow_run_context=workflow_run_context,
+ )
+ assert all("private-pass" not in stored and "654321" not in stored for stored in values.values())
+ assert await runtime_secret_bridge.consume_copilot_runtime_secret_values(
+ organization_id="o_1",
+ workflow_run_id="wr_origin",
+ ) == {"654321"}
+
+
+async def test_runtime_secret_bridge_allows_exactly_one_concurrent_consumer(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ values: dict[str, str] = {}
+
+ class FakeLocalCache:
+ is_shared = False
+
+ def get_lock(self, *_args: object, **_kwargs: object) -> asyncio.Lock:
+ return asyncio.Lock()
+
+ async def set(self, key: str, value: str, ex: int) -> None:
+ values[key] = value
+
+ async def get(self, key: str) -> str | None:
+ return values.get(key)
+
+ monkeypatch.setattr(runtime_secret_bridge, "app", SimpleNamespace(CACHE=FakeLocalCache()))
+ workflow_run_context = SimpleNamespace(
+ secrets={"runtime": "654321"},
+ runtime_otp_values={"654321"},
+ )
+ assert await runtime_secret_bridge.publish_copilot_runtime_secret_values(
+ organization_id="o_1",
+ workflow_run_id="wr_origin",
+ workflow_run_context=workflow_run_context,
+ )
+
+ consumed = await asyncio.gather(
+ runtime_secret_bridge.consume_copilot_runtime_secret_values(organization_id="o_1", workflow_run_id="wr_origin"),
+ runtime_secret_bridge.consume_copilot_runtime_secret_values(organization_id="o_1", workflow_run_id="wr_origin"),
+ )
+
+ assert consumed.count({"654321"}) == 1
+ assert consumed.count(None) == 1
+
+
+async def test_origin_registry_recovers_when_a_later_terminal_read_finds_the_handoff(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ ctx = _producer_ctx()
+ ctx.organization_id = "o_1"
+ ctx.workflow_permanent_id = "wp_origin"
+ ctx.secret_scrub_values = []
+ ctx.origin_run_redaction_registry = OriginRunRedactionRegistry(
+ "wr_origin",
+ {"credential": {"password": "private-pass"}},
+ contains_sensitive_values=True,
+ contains_all_sensitive_values=False,
+ contains_all_static_sensitive_values=True,
+ awaiting_runtime_secret_values=True,
+ )
+ consume = AsyncMock(side_effect=[None, {"654321"}])
+ monkeypatch.setattr(run_execution_module, "consume_copilot_runtime_secret_values", consume)
+
+ run = SimpleNamespace(
+ status="completed",
+ workflow_permanent_id=ctx.workflow_permanent_id,
+ workflow_id="wf_origin",
+ failure_reason=None,
+ browser_session_id=None,
+ )
+ workflow = SimpleNamespace(workflow_definition=SimpleNamespace(parameters=[]))
+ monkeypatch.setattr(
+ run_execution_module,
+ "app",
+ SimpleNamespace(
+ DATABASE=SimpleNamespace(
+ workflow_runs=SimpleNamespace(get_workflow_run=AsyncMock(return_value=run)),
+ workflows=SimpleNamespace(get_workflow_for_workflow_run=AsyncMock(return_value=workflow)),
+ observer=SimpleNamespace(get_workflow_run_blocks=AsyncMock(return_value=[])),
+ ),
+ AGENT_FUNCTION=SimpleNamespace(
+ should_dispatch_copilot_block_run_to_worker=AsyncMock(return_value=False),
+ ),
+ ),
+ )
+ monkeypatch.setattr(run_execution_module, "_attach_action_traces", AsyncMock())
+ monkeypatch.setattr(run_execution_module, "_attach_registered_output_parameter_values", AsyncMock())
+ monkeypatch.setattr(run_execution_module, "_fetch_dispatched_terminal_page_evidence", AsyncMock(return_value=None))
+
+ first = await run_execution_module._complete_origin_run_redaction_registry_from_runtime(ctx, "wr_origin")
+ result = await run_execution_module._get_run_results(
+ {"workflow_run_id": "wr_origin"},
+ ctx,
+ read_live_page=False,
+ )
+ second = ctx.origin_run_redaction_registry
+
+ assert first is not None and first.contains_all_sensitive_values is False
+ assert result["ok"] is True
+ assert second is not None and second.contains_all_sensitive_values is True
+ assert second.parameters["copilot_run_runtime_secret_values"] == ("654321",)
+ assert consume.await_count == 2
+
+
@pytest.mark.asyncio
async def test_sensitive_terminal_artifact_rejected_when_noncredential_secret_value_is_unregistered(
monkeypatch: pytest.MonkeyPatch,
diff --git a/tests/unit/test_workflow_elapsed_timeout.py b/tests/unit/test_workflow_elapsed_timeout.py
index 5edb64cae..210233c6d 100644
--- a/tests/unit/test_workflow_elapsed_timeout.py
+++ b/tests/unit/test_workflow_elapsed_timeout.py
@@ -190,6 +190,7 @@ async def test_execute_workflow_returns_after_elapsed_timeout_without_finally(mo
running_run = _workflow_run(WorkflowRunStatus.running, started_at=started_at)
timed_out_run = _workflow_run(WorkflowRunStatus.timed_out, started_at=started_at)
timed_out_run.failure_reason = "Workflow run exceeded max elapsed runtime limit of 1 minute."
+ timed_out_run.copilot_session_id = "copilot_1"
workflow = SimpleNamespace(
workflow_id="wf_1",
@@ -208,9 +209,10 @@ async def test_execute_workflow_returns_after_elapsed_timeout_without_finally(mo
)
organization = SimpleNamespace(organization_id="org_1")
+ workflow_run_context = SimpleNamespace(browser_session_id=None, secrets={"runtime_otp": "654321"})
workflow_context_manager = SimpleNamespace(
initialize_workflow_run_context=AsyncMock(),
- get_workflow_run_context=lambda _workflow_run_id: SimpleNamespace(browser_session_id=None),
+ get_workflow_run_context=lambda _workflow_run_id: workflow_run_context,
)
monkeypatch.setattr(service_module.app, "WORKFLOW_CONTEXT_MANAGER", workflow_context_manager)
monkeypatch.setattr(service_module.workflow_script_service, "workflow_has_conditionals", lambda _workflow: False)
@@ -227,7 +229,17 @@ async def test_execute_workflow_returns_after_elapsed_timeout_without_finally(mo
execute_workflow_blocks = AsyncMock()
generate_script_if_needed = AsyncMock()
execute_finally_block_if_configured = AsyncMock()
- clean_up_workflow = AsyncMock()
+ finalization_order: list[str] = []
+
+ async def publish_runtime_secrets(**kwargs: object) -> bool:
+ assert kwargs["workflow_run_context"] is workflow_run_context
+ finalization_order.append("publish")
+ return True
+
+ async def clean_up_workflow(**_kwargs: object) -> None:
+ finalization_order.append("cleanup")
+
+ monkeypatch.setattr(service_module, "publish_copilot_runtime_secret_values", publish_runtime_secrets)
monkeypatch.setattr(svc, "get_workflow_run", AsyncMock(return_value=created_run))
monkeypatch.setattr(svc, "get_workflow", AsyncMock(return_value=workflow))
@@ -262,7 +274,7 @@ async def test_execute_workflow_returns_after_elapsed_timeout_without_finally(mo
generate_script_if_needed.assert_not_awaited()
execute_finally_block_if_configured.assert_not_awaited()
mark_failed_if_not_final.assert_not_awaited()
- clean_up_workflow.assert_awaited_once()
+ assert finalization_order == ["publish", "cleanup"]
@pytest.mark.asyncio