Skip to content

Automate browser based workflows with AI

fix(SKY-11133): tolerate misnamed `block` arg in copilot validate_block (#6658)

Aaron Perezbaf204a52fa1
diff --git a/skyvern/forge/sdk/copilot/tools/mcp_hooks.py b/skyvern/forge/sdk/copilot/tools/mcp_hooks.py
index fa10b8f29..eeaf36f30 100644
--- a/skyvern/forge/sdk/copilot/tools/mcp_hooks.py
+++ b/skyvern/forge/sdk/copilot/tools/mcp_hooks.py
@@ -101,10 +101,38 @@ async def _get_block_schema_pre_hook(
}
+_BLOCK_JSON_ALIASES = ("block", "block_definition", "definition", "block_yaml")
+
+
+def _normalize_block_json_alias(params: dict[str, Any]) -> None:
+ """Promote a misnamed block payload (e.g. ``block``) to ``block_json`` in place.
+
+ The model sometimes passes the block under a shorter key than the schema's
+ ``block_json``; without this, FastMCP rejects the whole call at signature
+ validation before the tool runs. Stray alias keys are always dropped so they
+ cannot trip the "unexpected keyword argument" check.
+ """
+ has_block_json = isinstance(params.get("block_json"), str) and bool(params["block_json"].strip())
+ promoted: str | None = None
+ for alias in _BLOCK_JSON_ALIASES:
+ if alias not in params:
+ continue
+ value = params.pop(alias)
+ if has_block_json or promoted is not None:
+ continue
+ if isinstance(value, str):
+ promoted = value
+ elif isinstance(value, (dict, list)):
+ promoted = json.dumps(value)
+ if promoted is not None:
+ params["block_json"] = promoted
+
+
async def _validate_block_pre_hook(
params: dict[str, Any],
ctx: AgentContext,
) -> dict[str, Any] | None:
+ _normalize_block_json_alias(params)
if _copilot_block_authoring_policy(ctx) != BlockAuthoringPolicy.CODE_ONLY_BROWSER:
return None
block_json = params.get("block_json")
diff --git a/tests/unit/test_copilot_schema_overlay_ban.py b/tests/unit/test_copilot_schema_overlay_ban.py
index 7385e2fba..05ba83aa1 100644
--- a/tests/unit/test_copilot_schema_overlay_ban.py
+++ b/tests/unit/test_copilot_schema_overlay_ban.py
@@ -3,6 +3,7 @@ types at the discovery surface (SKY-9174, Part C)."""
from __future__ import annotations
+import json
from unittest.mock import MagicMock
import pytest
@@ -240,6 +241,72 @@ async def test_code_only_validate_block_pre_hook_leaves_shape_errors_to_validato
assert result is None
+_NAV_BLOCK = '{"block_type": "navigation", "label": "x", "url": "https://e.com", "navigation_goal": "g"}'
+
+
[email protected]("alias", ["block", "block_definition", "definition", "block_yaml"])
+async def test_validate_block_pre_hook_normalizes_misnamed_arg_to_block_json(alias: str, ctx: MagicMock) -> None:
+ """SKY-11133: the model calls validate_block with the block under a shorter
+ key (e.g. `block`). The pre-hook must promote it to `block_json` so the call
+ no longer dies at FastMCP signature validation."""
+ params = {alias: _NAV_BLOCK}
+
+ result = await _validate_block_pre_hook(params, ctx)
+
+ assert result is None
+ assert params["block_json"] == _NAV_BLOCK
+ assert alias not in params
+
+
+async def test_validate_block_pre_hook_serializes_dict_alias_value(ctx: MagicMock) -> None:
+ block = {"block_type": "navigation", "label": "x", "url": "https://e.com", "navigation_goal": "g"}
+ params: dict = {"block": block}
+
+ result = await _validate_block_pre_hook(params, ctx)
+
+ assert result is None
+ assert json.loads(params["block_json"]) == block
+ assert "block" not in params
+
+
+async def test_validate_block_pre_hook_strips_stray_alias_without_clobbering_block_json(ctx: MagicMock) -> None:
+ params = {"block_json": _NAV_BLOCK, "block": '{"block_type": "extraction", "label": "y"}'}
+
+ result = await _validate_block_pre_hook(params, ctx)
+
+ assert result is None
+ assert params["block_json"] == _NAV_BLOCK
+ assert "block" not in params
+
+
+async def test_validate_block_pre_hook_normalizes_alias_before_code_only_gate(code_only_ctx: MagicMock) -> None:
+ """Normalization must run before the code-only policy gate so a `code` block
+ passed under the wrong key is still rejected (not crashed)."""
+ params = {"block": '{"block_type": "code", "label": "x", "code": "pass"}'}
+
+ result = await _validate_block_pre_hook(params, code_only_ctx)
+
+ assert result is not None
+ assert result["ok"] is False
+ assert "validate real code blocks through update_and_run_blocks" in result["error"]
+ assert params["block_json"]
+ assert "block" not in params
+
+
+async def test_validate_block_pre_hook_alias_allows_helper_under_code_only(code_only_ctx: MagicMock) -> None:
+ params = {"block": '{"block_type": "conditional", "label": "x"}'}
+
+ result = await _validate_block_pre_hook(params, code_only_ctx)
+
+ assert result is None
+ assert params["block_json"] == '{"block_type": "conditional", "label": "x"}'
+
+
def test_pre_hook_and_post_emission_reject_share_constant() -> None:
"""SKY-9174 Part F: the pre-emission SchemaOverlay hooks and the
post-emission YAML-level reject (in `_update_workflow` + REPLACE_WORKFLOW)