Skip to content

Automate browser based workflows with AI

Return standalone (dedented) code from the MCP code-block synthesize tool (#SKY-13336) (#7976)

Aaron Perezfa6646d0c142
diff --git a/skyvern/cli/mcp_tools/code_block.py b/skyvern/cli/mcp_tools/code_block.py
index a295b3173..1ee5a970e 100644
--- a/skyvern/cli/mcp_tools/code_block.py
+++ b/skyvern/cli/mcp_tools/code_block.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import json
+import textwrap
from typing import Annotated, Any
from pydantic import Field
@@ -216,7 +217,9 @@ async def skyvern_code_block_synthesize(
)
data = {
- "code": synthesized.code,
+ # The synthesizer emits wrapper-indented code for the copilot stitch path; this tool's
+ # contract is a standalone block, so dedent before returning.
+ "code": textwrap.dedent(synthesized.code),
"parameters": synthesized.parameters,
"steps": synthesized.steps,
"notes": synthesized.notes,
diff --git a/tests/unit/test_code_block_synthesize_mcp_tool.py b/tests/unit/test_code_block_synthesize_mcp_tool.py
index f1a8dcf20..743cd65b8 100644
--- a/tests/unit/test_code_block_synthesize_mcp_tool.py
+++ b/tests/unit/test_code_block_synthesize_mcp_tool.py
@@ -5,7 +5,9 @@ OSS-synced: only example.* / RFC-2606 placeholder targets.
from __future__ import annotations
+import ast
import json
+import textwrap
from collections.abc import Iterator
from typing import Any
@@ -16,7 +18,7 @@ from skyvern.cli.core.trajectory_store import append_trajectory_entry
from skyvern.cli.mcp_tools import code_block as code_block_tools
from skyvern.cli.mcp_tools import mcp
from skyvern.cli.mcp_tools import trajectory as mcp_trajectory
-from skyvern.cli.mcp_tools.code_block import skyvern_code_block_synthesize
+from skyvern.cli.mcp_tools.code_block import skyvern_code_block_lint, skyvern_code_block_synthesize
from skyvern.forge.sdk.copilot.code_block_synthesis import synthesize_code_block
_HASH_A = "principal-a"
@@ -77,7 +79,7 @@ def _expected_synthesis_data(*, strict_selectors: bool) -> dict[str, Any]:
synthesized = synthesize_code_block(_fixture_trajectory(), strict_selectors=strict_selectors)
assert synthesized is not None
return {
- "code": synthesized.code,
+ "code": textwrap.dedent(synthesized.code),
"parameters": synthesized.parameters,
"steps": synthesized.steps,
"notes": synthesized.notes,
@@ -295,6 +297,20 @@ async def test_fixture_trajectory_synthesizes_non_empty_code_block() -> None:
assert result["data"]["emitted_interaction_count"] == 2
[email protected]("strict_selectors", [False, True])
+async def test_synthesized_code_is_standalone_and_passes_lint(strict_selectors: bool) -> None:
+ result = await skyvern_code_block_synthesize(json.dumps(_fixture_trajectory()), strict_selectors=strict_selectors)
+
+ code = result["data"]["code"]
+ ast.parse(code)
+ lint = await skyvern_code_block_lint(
+ code,
+ parameter_keys=[parameter["key"] for parameter in result["data"]["parameters"]],
+ )
+ assert lint["data"]["lint_ok"] is True, lint["data"]
+
+
@pytest.mark.asyncio
async def test_same_trajectory_synthesizes_byte_identical_code() -> None:
r1 = await skyvern_code_block_synthesize(json.dumps(_fixture_trajectory()))
diff --git a/tests/unit/test_mcp_browser_tools.py b/tests/unit/test_mcp_browser_tools.py
index 42ac905f5..aa53b9dc8 100644
--- a/tests/unit/test_mcp_browser_tools.py
+++ b/tests/unit/test_mcp_browser_tools.py
@@ -1575,6 +1575,8 @@ async def test_do_select_option_scan_observed_bare_input_uses_typeahead(
{"text": "", "value": "Fairview", "dataValues": [], "expanded": None, "optionSelected": False},
{"text": "", "value": "Fairview", "dataValues": [], "expanded": None, "optionSelected": True},
]
+ # SKY-12634: freeze the wall clock so a loaded CI runner cannot blow the 100ms budget.
+ monkeypatch.setattr(browser_ops, "time", SimpleNamespace(monotonic=Mock(return_value=0)))
assert await do_select_option(page, "#town", "Fairview", timeout=100) == "Fairview"
control.fill.assert_awaited_once_with("Fairview", timeout=100)