Skip to content

Automate browser based workflows with AI

Deduplicate the MCP block walk and store empty code block goals as NULL (#7698)

Aaron Perez90ba93b2a278
diff --git a/skyvern/cli/mcp_tools/workflow.py b/skyvern/cli/mcp_tools/workflow.py
index 3cbfbcb4d..583614796 100644
--- a/skyvern/cli/mcp_tools/workflow.py
+++ b/skyvern/cli/mcp_tools/workflow.py
@@ -687,16 +687,13 @@ def _inject_code_v2_defaults(definition: str, fmt: str) -> str:
def _collect_code_block_labels(blocks: Any) -> frozenset[str]:
- labels: set[str] = set()
if not isinstance(blocks, list):
return frozenset()
- for block in blocks:
- if not isinstance(block, dict):
- continue
- if block.get("block_type") == "code" and isinstance(block.get("label"), str):
- labels.add(block["label"])
- labels |= _collect_code_block_labels(block.get("loop_blocks"))
- return frozenset(labels)
+ return frozenset(
+ block["label"]
+ for block in _iter_blocks_flat(blocks)
+ if block.get("block_type") == "code" and isinstance(block.get("label"), str)
+ )
def _inject_code_block_prompt_defaults(definition: str, fmt: str, existing_code_labels: frozenset[str]) -> str:
@@ -716,24 +713,15 @@ def _inject_code_block_prompt_defaults(definition: str, fmt: str, existing_code_
return definition
changed = False
+ for block in _iter_blocks_flat(blocks):
+ if (
+ block.get("block_type") == "code"
+ and "prompt" not in block
+ and block.get("label") not in existing_code_labels
+ ):
+ block["prompt"] = ""
+ changed = True
- def _walk(items: list[Any]) -> None:
- nonlocal changed
- for block in items:
- if not isinstance(block, dict):
- continue
- if (
- block.get("block_type") == "code"
- and "prompt" not in block
- and block.get("label") not in existing_code_labels
- ):
- block["prompt"] = ""
- changed = True
- loop_blocks = block.get("loop_blocks")
- if isinstance(loop_blocks, list):
- _walk(loop_blocks)
-
- _walk(blocks)
return _dump_definition_dict(raw, parsed_format) if changed else definition
diff --git a/skyvern/forge/agent.py b/skyvern/forge/agent.py
index 058a581bc..f6b297103 100644
--- a/skyvern/forge/agent.py
+++ b/skyvern/forge/agent.py
@@ -720,7 +720,7 @@ class ForgeAgent:
task = await app.DATABASE.tasks.create_task(
url=task_url,
title=code_block.label,
- navigation_goal=code_block.prompt,
+ navigation_goal=code_block.prompt or None,
data_extraction_goal=None,
navigation_payload=None,
organization_id=organization_id,
diff --git a/tests/unit/workflow/test_code_block_recorder.py b/tests/unit/workflow/test_code_block_recorder.py
index b03664fbf..9b0615866 100644
--- a/tests/unit/workflow/test_code_block_recorder.py
+++ b/tests/unit/workflow/test_code_block_recorder.py
@@ -505,6 +505,26 @@ async def test_create_task_and_step_from_code_block_maps_goal_to_task(monkeypatc
assert create_step.await_args.kwargs["order"] == 0
+async def test_create_task_and_step_from_code_block_maps_empty_goal_to_null(monkeypatch: pytest.MonkeyPatch) -> None:
+ """A prompt-less code block stores navigation_goal NULL, not "", so action-plan lookups stay consistent."""
+ create_task = AsyncMock(return_value=_FakeTask())
+ monkeypatch.setattr(app.DATABASE.tasks, "get_last_task_for_workflow_run", AsyncMock(return_value=None))
+ monkeypatch.setattr(app.DATABASE.tasks, "create_task", create_task)
+ monkeypatch.setattr(app.DATABASE.tasks, "update_task", AsyncMock(return_value=_FakeTask()))
+ monkeypatch.setattr(app.DATABASE.tasks, "create_step", AsyncMock(return_value=_FakeStep()))
+
+ block = _make_code_block("x = 1", goal="")
+ await ForgeAgent().create_task_and_step_from_code_block(
+ code_block=block,
+ organization_id="o_test",
+ workflow_run_id="wr_test",
+ task_url="https://example.com/login",
+ )
+
+ assert create_task.await_args.kwargs["navigation_goal"] is None
+
+
@pytest.mark.asyncio
async def test_create_task_and_step_from_code_block_fails_partial_task_on_step_error(
monkeypatch: pytest.MonkeyPatch,