Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
feat(SKY-12216): heal-content sanitizer for code/error UI surfaces (#7410)
pedrohsdbfb086ae2a8f4
diff --git a/skyvern/forge/sdk/copilot/heal_content_sanitization.py b/skyvern/forge/sdk/copilot/heal_content_sanitization.pynew file mode 100644index 000000000..d36200379--- /dev/null+++ b/skyvern/forge/sdk/copilot/heal_content_sanitization.py@@ -0,0 +1,80 @@+"""Heal-content sanitization.++Primary masking is write-time and value-based via ``mask_heal_text`` and+``mask_heal_steps`` with a live agent context. ``sanitize_heal_content`` is a+read-time, shape-based backstop and is not sufficient on its own.+"""++from __future__ import annotations++import re+from typing import TYPE_CHECKING, Any++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, scrub_secrets_from_text+from skyvern.forge.sdk.workflow.context_manager import RANDOM_SECRET_ID_PREFIX+from skyvern.schemas.self_heal import HealEpisode, HealEpisodeDetail++if TYPE_CHECKING:+ from skyvern.forge.sdk.copilot.runtime import AgentContext++TRUNCATION_MARKER = "\n…[truncated]"+_PLACEHOLDER_SECRET_RE = re.compile(rf"\b{re.escape(RANDOM_SECRET_ID_PREFIX)}[A-Za-z0-9]+(?:_[A-Za-z0-9]+)*")+++def _truncate_sanitized_text(text: str, *, max_length: int) -> str:+ if max_length < 0:+ max_length = 0++ if len(text) <= max_length:+ return text++ if text.endswith(TRUNCATION_MARKER) and len(text) == max_length + len(TRUNCATION_MARKER):+ return text++ return f"{text[:max_length]}{TRUNCATION_MARKER}"+++def mask_heal_text(ctx: AgentContext, text: str | None, *, max_length: int = 20000) -> str | None:+ if text is None:+ return None++ sanitized = scrub_secrets_from_text(ctx, text)+ sanitized = redact_raw_secrets_for_prompt(sanitized)+ sanitized = _PLACEHOLDER_SECRET_RE.sub("[REDACTED_SECRET]", sanitized)+ return _truncate_sanitized_text(sanitized, max_length=max_length)+++def mask_heal_steps(ctx: AgentContext, steps: Any) -> Any:+ return scrub_secrets_from_structure(ctx, steps)+++def sanitize_heal_content(text: str | None, *, max_length: int = 20000) -> str | None:+ if text is None:+ return None++ sanitized = redact_raw_secrets_for_prompt(text)+ sanitized = _PLACEHOLDER_SECRET_RE.sub("[REDACTED_SECRET]", sanitized)+ return _truncate_sanitized_text(sanitized, max_length=max_length)+++def _sanitize_string_leaves(node: Any) -> Any:+ if isinstance(node, str):+ return sanitize_heal_content(node)+ if isinstance(node, list):+ return [_sanitize_string_leaves(item) for item in node]+ if isinstance(node, tuple):+ return tuple(_sanitize_string_leaves(item) for item in node)+ if isinstance(node, dict):+ return {key: _sanitize_string_leaves(value) for key, value in node.items()}+ return node+++def build_heal_episode_detail(episode: HealEpisode) -> HealEpisodeDetail:+ return HealEpisodeDetail(+ **episode.model_dump(exclude={"block_code", "block_prompt", "failure_message", "block_steps"}),+ sanitized_block_code=sanitize_heal_content(episode.block_code),+ sanitized_block_prompt=sanitize_heal_content(episode.block_prompt),+ sanitized_failure_message=sanitize_heal_content(episode.failure_message),+ block_steps=_sanitize_string_leaves(episode.block_steps),+ )diff --git a/skyvern/schemas/self_heal.py b/skyvern/schemas/self_heal.pyindex 033c93940..f611bcd9b 100644--- a/skyvern/schemas/self_heal.py+++ b/skyvern/schemas/self_heal.py@@ -73,6 +73,43 @@ class HealEpisode(BaseModel):modified_at: datetime+class HealEpisodeView(BaseModel):+ model_config = ConfigDict(from_attributes=True)++ heal_episode_id: str+ organization_id: str+ workflow_permanent_id: str+ workflow_id: str+ workflow_run_id: str+ workflow_run_block_id: str+ block_label: str+ engine: str+ status: HealStatus+ skip_reason: HealSkipReason | None = None+ block_steps: list | dict | None = None+ snapshot_available: bool = False+ convergence_eligible: bool = False+ parameter_binding_keys: list[str] | dict | None = None+ exception_class: str | None = None+ failing_line: int | None = None+ matched_step_index: int | None = None+ escalation_task_id: str | None = None+ wall_clock_ms: int | None = None+ action_count: int | None = None+ output_obligation: OutputObligation | None = None+ dom_snapshot_artifact_id: str | None = None+ scout_transcript_artifact_id: str | None = None+ screenshot_artifact_id: str | None = None+ created_at: datetime+ modified_at: datetime+++class HealEpisodeDetail(HealEpisodeView):+ sanitized_block_code: str | None = None+ sanitized_block_prompt: str | None = None+ sanitized_failure_message: str | None = None++class WorkflowHealProposal(BaseModel):model_config = ConfigDict(from_attributes=True)diff --git a/tests/unit/forge/sdk/copilot/test_heal_content_sanitization.py b/tests/unit/forge/sdk/copilot/test_heal_content_sanitization.pynew file mode 100644index 000000000..d400f9417--- /dev/null+++ b/tests/unit/forge/sdk/copilot/test_heal_content_sanitization.py@@ -0,0 +1,136 @@+from __future__ import annotations++from datetime import datetime, timezone+from types import SimpleNamespace++from skyvern.forge.sdk.copilot.heal_content_sanitization import (+ build_heal_episode_detail,+ mask_heal_steps,+ mask_heal_text,+ sanitize_heal_content,+)+from skyvern.schemas.self_heal import HealEpisode+++def test_sanitize_heal_content_redacts_email_password_and_colon_secret() -> None:+ text = "Use [email protected]:FakePass123! to sign in.\napi_key: MySecretValue987"++ sanitized = sanitize_heal_content(text)++ assert sanitized is not None+ assert "FakePass123!" not in sanitized+ assert "MySecretValue987" not in sanitized+ assert sanitized.count("[REDACTED_SECRET]") >= 2+++def test_sanitize_heal_content_redacts_placeholder_tokens() -> None:+ sanitized = sanitize_heal_content("username=placeholder_ABCD password=placeholder_a1B2c3")++ assert sanitized is not None+ assert "placeholder_ABCD" not in sanitized+ assert "placeholder_a1B2c3" not in sanitized+ assert sanitized.count("[REDACTED_SECRET]") == 2+++def test_mask_heal_text_scrubs_registered_novel_value() -> None:+ ctx = SimpleNamespace(secret_scrub_values=["Zq9xNovelSecretNoKnownShape"], browser_session_id=None)++ sanitized = mask_heal_text(ctx, 'x = "Zq9xNovelSecretNoKnownShape"')++ assert sanitized is not None+ assert "Zq9xNovelSecretNoKnownShape" not in sanitized+ assert "[REDACTED_SECRET]" in sanitized+++def test_sanitize_heal_content_does_not_corrupt_non_placeholder_identifiers() -> None:+ sanitized = sanitize_heal_content("block_placeholder_2 = 1")++ assert sanitized is not None+ assert "block_placeholder_2" in sanitized+++def test_sanitize_heal_content_redacts_full_placeholder_token_with_suffix() -> None:+ sanitized = sanitize_heal_content('fill("#pw", "placeholder_ABCD_password")')++ assert sanitized is not None+ assert "placeholder_ABCD_password" not in sanitized+ assert "_password" not in sanitized+++def test_mask_heal_steps_scrubs_registered_novel_value() -> None:+ ctx = SimpleNamespace(secret_scrub_values=["Zq9xNovelSecretNoKnownShape"], browser_session_id=None)++ sanitized = mask_heal_steps(ctx, [{"selector": "#pw", "value": "Zq9xNovelSecretNoKnownShape"}])++ assert isinstance(sanitized, list)+ assert sanitized[0]["value"] == "[REDACTED_SECRET]"+++def test_sanitize_heal_content_truncates_and_appends_marker() -> None:+ sanitized = sanitize_heal_content("x" * 30, max_length=10)+ assert sanitized == ("x" * 10) + "\n…[truncated]"+++def test_sanitize_heal_content_none_passthrough() -> None:+ assert sanitize_heal_content(None) is None+++def test_sanitize_heal_content_empty_string_passthrough() -> None:+ assert sanitize_heal_content("") == ""+++def test_sanitize_heal_content_is_idempotent() -> None:+ single = sanitize_heal_content("A" * 25, max_length=8)+ double = sanitize_heal_content(single, max_length=8)+ assert double == single+++def test_build_heal_episode_detail_sanitizes_and_drops_raw_fields() -> None:+ now = datetime.now(timezone.utc)+ episode = HealEpisode(+ heal_episode_id="he_123",+ organization_id="o_123",+ workflow_permanent_id="wpid_123",+ workflow_id="w_123",+ workflow_run_id="wr_123",+ workflow_run_block_id="wrb_123",+ block_label="login_block",+ engine="code",+ status="fired_failed",+ block_prompt="Use [email protected]:FakePass123! to sign in",+ block_code="api_key: MyRawApiSecret\nx = placeholder_ABCD",+ failure_message="Password: AnotherRawSecret",+ created_at=now,+ modified_at=now,+ )++ detail = build_heal_episode_detail(episode)++ assert "FakePass123!" not in (detail.sanitized_block_prompt or "")+ assert "MyRawApiSecret" not in (detail.sanitized_block_code or "")+ assert "placeholder_ABCD" not in (detail.sanitized_block_code or "")+ assert "AnotherRawSecret" not in (detail.sanitized_failure_message or "")+ assert not hasattr(detail, "block_code")+++def test_build_heal_episode_detail_sanitizes_block_steps_string_leaves() -> None:+ now = datetime.now(timezone.utc)+ episode = HealEpisode(+ heal_episode_id="he_123",+ organization_id="o_123",+ workflow_permanent_id="wpid_123",+ workflow_id="w_123",+ workflow_run_id="wr_123",+ workflow_run_block_id="wrb_123",+ block_label="login_block",+ engine="code",+ status="fired_failed",+ block_steps=[{"selector": "#pw", "value": "sk-live-secretsecretsecret"}],+ created_at=now,+ modified_at=now,+ )++ detail = build_heal_episode_detail(episode)++ assert isinstance(detail.block_steps, list)+ assert detail.block_steps[0]["value"] == "[REDACTED_SECRET]"diff --git a/tests/unit/forge/sdk/db/test_self_heal_schema.py b/tests/unit/forge/sdk/db/test_self_heal_schema.pyindex 3ae335498..a10ee737c 100644--- a/tests/unit/forge/sdk/db/test_self_heal_schema.py+++ b/tests/unit/forge/sdk/db/test_self_heal_schema.py@@ -7,6 +7,7 @@ import pytestfrom skyvern.forge.sdk.db.id import generate_heal_episode_id, generate_heal_proposal_idfrom skyvern.forge.sdk.db.models import HealEpisodeModel, WorkflowHealProposalModel+from skyvern.schemas.self_heal import HealEpisodeDetaildef test_heal_episode_and_heal_proposal_id_prefixes() -> None:@@ -46,3 +47,13 @@ def test_heal_migration_chains_from_current_head() -> None:]assert down_revisions == ["5a7c9d1e2f34"]+++def test_heal_episode_detail_exposes_only_sanitized_free_text_fields() -> None:+ fields = HealEpisodeDetail.model_fields+ assert "sanitized_block_code" in fields+ assert "sanitized_block_prompt" in fields+ assert "sanitized_failure_message" in fields+ assert "block_code" not in fields+ assert "block_prompt" not in fields+ assert "failure_message" not in fields