Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
Give recorded and compute-generated code blocks the new code block shape (SKY-13185) (#8006)
Aaron Perez8f02aad41ebd
diff --git a/skyvern/forge/sdk/workflow/models/block.py b/skyvern/forge/sdk/workflow/models/block.pyindex 634ae0186..835dece29 100644--- a/skyvern/forge/sdk/workflow/models/block.py+++ b/skyvern/forge/sdk/workflow/models/block.py@@ -4520,6 +4520,12 @@ async def wrapper({default_args}):def _compose_heal_goal(self, *, workflow_run_context: WorkflowRunContext, failing_line: int | None) -> str:safe_main = workflow_run_context.mask_secrets_in_data(self.prompt or "")+ # Steps are a code-derived outline, not an authored goal, so they may only narrow one.+ # The harness path reaches here without the floor path's `if not self.prompt` gate, and+ # a goal-less block would otherwise aim the recovery agent at a live page with a bare+ # step description and no context.+ if not self.prompt:+ return safe_mainmatched_step = self._match_step_for_failing_line(failing_line) if failing_line is not None else Noneif matched_step is None or not matched_step.description:return safe_maindiff --git a/skyvern/services/browser_recording/code_first.py b/skyvern/services/browser_recording/code_first.pyindex 6c8253c6b..aa418ddb9 100644--- a/skyvern/services/browser_recording/code_first.py+++ b/skyvern/services/browser_recording/code_first.py@@ -15,6 +15,7 @@ import structlogfrom skyvern.client.types.workflow_definition_yaml_blocks_item import WorkflowDefinitionYamlBlocksItem_Codefrom skyvern.client.types.workflow_definition_yaml_parameters_item import WorkflowDefinitionYamlParametersItem_Workflow+from skyvern.forge.sdk.copilot.code_block_steps import derive_code_block_stepsfrom skyvern.forge.sdk.copilot.code_block_synthesis import synthesize_code_block, synthesize_goto_code_blockfrom skyvern.forge.sdk.workflow.exceptions import InsecureCodeDetectedfrom skyvern.forge.sdk.workflow.models.block import CodeBlock@@ -297,6 +298,11 @@ def actions_to_code_first_blocks(parameter_keys=block_parameter_keys or None,# Editor's convertToNode reads block.parameters.map(p => p.key); mirror the action block.parameters=[{"key": key} for key in block_parameter_keys],+ # A non-null prompt is what makes the editor render the code-first node; "" is+ # runtime-neutral (every backend prompt check is truthiness based) and leaves the+ # Goal for the user, because a fabricated one would arm runtime self-heal.+ prompt="",+ steps=derive_code_block_steps(code) or None,))diff --git a/skyvern/services/pdf_import_service.py b/skyvern/services/pdf_import_service.pyindex 70762b456..cab4f650e 100644--- a/skyvern/services/pdf_import_service.py+++ b/skyvern/services/pdf_import_service.py@@ -115,6 +115,12 @@ class PDFImportService:blk[field] = cleanedreferenced.update(used)+ # A non-null prompt is what makes the editor render the code-first node; "" is+ # runtime-neutral (every backend prompt check is truthiness based) and leaves the+ # Goal for the user, because a fabricated one would arm runtime self-heal.+ if blk.get("block_type") == "code" and "prompt" not in blk:+ blk["prompt"] = ""+# Ensure required fields for text_prompt blocksif blk.get("block_type") == "text_prompt":if not blk.get("prompt"):diff --git a/skyvern/services/task_v2_service.py b/skyvern/services/task_v2_service.pyindex feb4145f7..55b976d30 100644--- a/skyvern/services/task_v2_service.py+++ b/skyvern/services/task_v2_service.py@@ -2025,7 +2025,10 @@ async def _generate_compute_task(raise last_error or InsecureCodeDetected("Failed to synthesize safe compute code")label = f"compute_{generate_random_string()}"- code_block_yaml = CodeBlockYAML(label=label, code=safe_code)+ # A non-null prompt is what makes the editor render the code-first node; "" is runtime-neutral+ # (every backend prompt check is truthiness based) and leaves the Goal for the user, because a+ # fabricated one would arm runtime self-heal on a data-only block.+ code_block_yaml = CodeBlockYAML(label=label, code=safe_code, prompt="")output_parameter = await app.WORKFLOW_SERVICE.create_output_parameter_for_block(workflow_id=workflow_id,block_yaml=code_block_yaml,@@ -2035,6 +2038,7 @@ async def _generate_compute_task(label=label,code=safe_code,parameters=[],+ prompt="",output_parameter=output_parameter,),[code_block_yaml],diff --git a/tests/unit/services/test_browser_recording_code_first.py b/tests/unit/services/test_browser_recording_code_first.pyindex d3d265bc7..218bfab80 100644--- a/tests/unit/services/test_browser_recording_code_first.py+++ b/tests/unit/services/test_browser_recording_code_first.py@@ -125,6 +125,36 @@ def test_click_and_type_synthesize_single_code_block() -> None:assert "widgets" not in block.code+def test_recorded_code_blocks_use_the_code_first_editor_shape() -> None:+ actions: list[Action] = [+ make_input(1000, "widgets", selector="#search", accessible_name="Search"),+ make_click(2000, selector="#submit", role="button", accessible_name="Go"),+ ]++ result = actions_to_code_first_blocks(actions, None)++ assert result is not None+ blocks, _ = result+ block = blocks[0]+ # A non-null prompt is what makes the editor render the code-first node; "" leaves the+ # Goal for the user to write rather than fabricating one.+ assert block.prompt == ""+ assert block.steps is not None+ assert [step.action_type for step in block.steps] == ["goto_url", "input_text", "click"]+ assert all(step.line_start is not None for step in block.steps)+++def test_goto_only_recording_carries_the_code_first_editor_shape() -> None:+ actions: list[Action] = [make_url_change(1000, "https://example.com/only")]++ result = actions_to_code_first_blocks(actions, None)++ assert result is not None+ blocks, _ = result+ assert blocks[0].prompt == ""+ assert [step.action_type for step in blocks[0].steps or []] == ["goto_url"]++def test_password_input_becomes_parameter_without_default() -> None:actions: list[Action] = [make_input(1000, "hunter2", selector="#pw", accessible_name="Password", input_type="password"),diff --git a/tests/unit/services/test_pdf_import_service.py b/tests/unit/services/test_pdf_import_service.pynew file mode 100644index 000000000..80e38fec0--- /dev/null+++ b/tests/unit/services/test_pdf_import_service.py@@ -0,0 +1,44 @@+"""Tests for the PDF/SOP import service's LLM-output sanitizer."""++from typing import Any++from skyvern.services.pdf_import_service import pdf_import_service+++def _definition(*blocks: dict[str, Any]) -> dict[str, Any]:+ return {+ "title": "Imported SOP",+ "workflow_definition": {"parameters": [], "blocks": list(blocks)},+ }+++def _code_block(**overrides: Any) -> dict[str, Any]:+ return {"label": "compute", "block_type": "code", "continue_on_failure": False, "code": "x = 1", **overrides}+++def test_code_block_without_a_prompt_gets_the_code_first_default() -> None:+ sanitized = pdf_import_service._sanitize_workflow_json(_definition(_code_block()))++ # A non-null prompt is what makes the editor render the code-first node; the LLM never+ # writes one, so an imported code block would otherwise land as a legacy node.+ assert sanitized["workflow_definition"]["blocks"][0]["prompt"] == ""+++def test_an_authored_prompt_is_preserved() -> None:+ sanitized = pdf_import_service._sanitize_workflow_json(_definition(_code_block(prompt="Total the invoice rows")))++ assert sanitized["workflow_definition"]["blocks"][0]["prompt"] == "Total the invoice rows"+++def test_explicit_null_prompt_opts_out() -> None:+ sanitized = pdf_import_service._sanitize_workflow_json(_definition(_code_block(prompt=None)))++ assert sanitized["workflow_definition"]["blocks"][0]["prompt"] is None+++def test_non_code_blocks_are_not_given_a_prompt() -> None:+ block = {"label": "wait_a_bit", "block_type": "wait", "continue_on_failure": False, "wait_sec": 1}++ sanitized = pdf_import_service._sanitize_workflow_json(_definition(block))++ assert "prompt" not in sanitized["workflow_definition"]["blocks"][0]diff --git a/tests/unit/test_task_v2_compute_task.py b/tests/unit/test_task_v2_compute_task.pyindex 4841726c9..109398f7b 100644--- a/tests/unit/test_task_v2_compute_task.py+++ b/tests/unit/test_task_v2_compute_task.py@@ -155,6 +155,20 @@ class TestGenerateComputeTask:assert block.code.startswith("gathered_data = json.loads(")CodeBlock.is_safe_code(block.code)+ @pytest.mark.asyncio+ async def test_generated_block_uses_the_code_first_editor_shape(self) -> None:+ with _patch_service({"code": SAFE_SNIPPET}):+ block, block_yaml_list, _ = await task_v2_service._generate_compute_task(+ task_v2=_make_task_v2(),+ workflow_id="w_test",+ plan="pick the cheapest tier",+ task_history=GATHERED,+ )+ # A non-null prompt is what makes the editor render the code-first node. It stays+ # empty: a fabricated goal would arm runtime self-heal on a data-only block.+ assert block_yaml_list[0].prompt == ""+ assert block.prompt == ""+@pytest.mark.asyncioasync def test_generated_block_executes_to_expected_output(self) -> None:with _patch_service({"code": SAFE_SNIPPET}):diff --git a/tests/unit/workflow/test_code_block_escalation.py b/tests/unit/workflow/test_code_block_escalation.pyindex ed1486b7b..812e35e06 100644--- a/tests/unit/workflow/test_code_block_escalation.py+++ b/tests/unit/workflow/test_code_block_escalation.py@@ -463,6 +463,33 @@ async def test_matched_step_narrows_the_goal(monkeypatch: pytest.MonkeyPatch) ->assert DEFAULT_PROMPT in goal[email protected]("prompt", [None, ""], ids=["absent_goal", "empty_goal"])+def test_steps_alone_never_manufacture_a_heal_goal(prompt: str | None) -> None:+ # The harness heal path composes its goal without the floor path's `if not self.prompt`+ # gate, so a goal-less block carrying a code-derived step outline (what the MCP seam and+ # the recording converter both emit) would otherwise send the recovery agent at a live+ # page with a bare "click the export button" and no context.+ block = _make_code_block(+ prompt=prompt,+ steps=[CodeBlockStep(description="click the export button", line_start=1, line_end=1)],+ )+ context = _make_context()++ assert block._compose_heal_goal(workflow_run_context=context, failing_line=1) == ""+++def test_an_authored_goal_is_still_narrowed_by_its_matched_step() -> None:+ block = _make_code_block(+ steps=[CodeBlockStep(description="click the export button", line_start=1, line_end=1)],+ )+ context = _make_context()++ goal = block._compose_heal_goal(workflow_run_context=context, failing_line=1)++ assert "click the export button" in goal+ assert DEFAULT_PROMPT in goal++@pytest.mark.asyncioasync def test_failing_static_goto_sets_escalation_url(monkeypatch: pytest.MonkeyPatch) -> None:monkeypatch.setattr("skyvern.config.settings.ENABLE_CODE_BLOCK_SELF_HEALING", True, raising=False)