Automate browser based workflows with AI
Python aiapiautomationbrowserbrowser-automationcomputergptllmplaywrightpowerautomatepuppeteerpythonrpaseleniumvisionworkflow
fix(mcp): preserve non-credential parameter↔block links on workflow edits (SKY-11467) (#6813)
Shuchang Zhengf623361e210e
diff --git a/skyvern/cli/mcp_tools/workflow.py b/skyvern/cli/mcp_tools/workflow.pyindex 8722d6b64..0514b34d9 100644--- a/skyvern/cli/mcp_tools/workflow.py+++ b/skyvern/cli/mcp_tools/workflow.py@@ -491,6 +491,11 @@ def _normalize_json_definition(raw: Any) -> dict[str, Any]:if "workflow_definition" not in raw:raise ValueError("Workflow definition missing 'workflow_definition' object")+ if _has_runtime_definition_shape(raw.get("workflow_definition")):+ # An echoed GET payload is in runtime shape; convert before validation so block<->parameter+ # links survive instead of being lost to the raw-dict fallback below.+ raw = {**raw, "workflow_definition": _workflow_definition_to_authoring_shape(raw["workflow_definition"])}+try:normalized = WorkflowCreateYAMLRequestSchema.model_validate(raw)except Exception as exc:@@ -660,6 +665,128 @@ def _strip_runtime_fields(param: dict[str, Any]) -> dict[str, Any]:}+def _parameter_to_authoring_shape(param: Any) -> dict[str, Any] | None:+ """Convert one runtime parameter dict to its authoring shape, or None to drop it.++ Auto-generated output parameters are regenerated (and rejected if author-supplied) by the+ converter, so they are dropped. Context parameters carry a resolved ``source`` object at+ runtime but the authoring schema wants ``source_parameter_key``.+ """+ if not isinstance(param, dict):+ return param+ if param.get("parameter_type") == ParameterType.OUTPUT.value:+ return None+ authoring = _strip_runtime_fields(param)+ source = authoring.get("source")+ if isinstance(source, dict) and source.get("key"):+ authoring.pop("source", None)+ authoring.setdefault("source_parameter_key", source["key"])+ return authoring+++def _block_to_authoring_shape(block: Any) -> Any:+ """Convert one runtime block dict to its authoring shape, recursing into ``loop_blocks``.++ Runtime blocks expose resolved ``parameters`` objects plus an ``output_parameter`` and (for+ loops) a resolved ``loop_over`` object; the authoring schema uses ``parameter_keys``, carries no+ output parameter, and references the loop source via ``loop_over_parameter_key``. Without this+ inverse, a get -> edit -> update round trip silently drops non-credential block<->parameter links.+ """+ if not isinstance(block, dict):+ return block+ authoring = dict(block)+ label = authoring.get("label")+ output_key = f"{label}_output" if isinstance(label, str) else None++ resolved = authoring.pop("parameters", None)+ authoring.pop("output_parameter", None)+ if isinstance(resolved, list):+ derived = [+ param["key"]+ for param in resolved+ if isinstance(param, dict) and param.get("key") and param.get("key") != output_key+ ]+ if derived:+ existing = [k for k in (authoring.get("parameter_keys") or []) if isinstance(k, str)]+ authoring["parameter_keys"] = existing + [k for k in derived if k not in existing]++ loop_over = authoring.get("loop_over")+ if isinstance(loop_over, dict):+ authoring.pop("loop_over", None)+ if loop_over.get("key"):+ authoring.setdefault("loop_over_parameter_key", loop_over["key"])+ elif loop_over is None and "loop_over" in authoring:+ authoring.pop("loop_over", None)++ loop_blocks = authoring.get("loop_blocks")+ if isinstance(loop_blocks, list):+ authoring["loop_blocks"] = [_block_to_authoring_shape(child) for child in loop_blocks]++ return authoring+++def _block_authoring_parameter_keys(block: dict[str, Any]) -> list[str]:+ """The block's parameter links as authoring keys, read from either an explicit ``parameter_keys``+ list (authoring shape) or the resolved ``parameters`` objects (runtime shape returned by GET),+ excluding the block's own auto ``{label}_output``."""+ label = block.get("label")+ output_key = f"{label}_output" if isinstance(label, str) else None+ keys: list[str] = [k for k in (block.get("parameter_keys") or []) if isinstance(k, str)]+ seen = set(keys)+ for param in block.get("parameters") or []:+ if isinstance(param, dict):+ key = param.get("key")+ if isinstance(key, str) and key != output_key and key not in seen:+ keys.append(key)+ seen.add(key)+ return keys+++def _workflow_definition_to_authoring_shape(wf_def: Any) -> Any:+ """Convert a runtime ``workflow_definition`` dict (as returned by GET) to the authoring shape the+ create/update path consumes. Inverse of the relevant parts of ``convert_workflow_definition``.+ """+ if not isinstance(wf_def, dict):+ return wf_def+ authoring = dict(wf_def)+ params = authoring.get("parameters")+ if isinstance(params, list):+ converted = [shaped for shaped in (_parameter_to_authoring_shape(p) for p in params) if shaped is not None]+ authoring["parameters"] = converted+ blocks = authoring.get("blocks")+ if isinstance(blocks, list):+ authoring["blocks"] = [_block_to_authoring_shape(block) for block in blocks]+ return authoring+++def _has_runtime_definition_shape(wf_def: Any) -> bool:+ """Whether a ``workflow_definition`` carries runtime-only fields, so it needs authoring conversion."""+ if not isinstance(wf_def, dict):+ return False+ params = wf_def.get("parameters")+ if isinstance(params, list):+ for param in params:+ if isinstance(param, dict) and (+ param.get("parameter_type") == ParameterType.OUTPUT.value or isinstance(param.get("source"), dict)+ ):+ return True++ def _block_has_runtime(block: Any) -> bool:+ if not isinstance(block, dict):+ return False+ if (+ isinstance(block.get("parameters"), list)+ or "output_parameter" in block+ or isinstance(block.get("loop_over"), dict)+ ):+ return True+ loop_blocks = block.get("loop_blocks")+ return isinstance(loop_blocks, list) and any(_block_has_runtime(child) for child in loop_blocks)++ blocks = wf_def.get("blocks")+ return isinstance(blocks, list) and any(_block_has_runtime(block) for block in blocks)++def _is_protected_update_parameter(param: Any) -> bool:"""Return True when *param* should be preserved from the existing workflow.@@ -757,7 +884,7 @@ def _iter_positional_block_matches(return result-async def _inject_workflow_update_parameters(definition: str, fmt: str, workflow_id: str) -> str:+async def _inject_workflow_update_parameters(definition: str, fmt: str, workflow_id: str) -> tuple[str, list[str]]:"""Preserve protected credential/secret parameters during MCP workflow updates.Credential references should NEVER be modifiable via MCP — the existing workflow's@@ -766,22 +893,28 @@ async def _inject_workflow_update_parameters(definition: str, fmt: str, workflow(even if the caller includes them — they may have stale/wrong data).2. Injects credential parameter_keys into blocks using per-block matcheswith label-based fallback.+ 3. Re-attaches non-credential block<->parameter links the caller dropped for a+ still-declared parameter (best-effort, surfaced as warnings).++ Returns the (possibly rewritten) definition string and any human-facing warnings."""+ warnings: list[str] = []+raw, parsed_format = _load_definition_dict(definition, fmt)if raw is None or parsed_format is None:- return definition+ return definition, warningswf_def = raw.get("workflow_definition")if not isinstance(wf_def, dict):- return definition+ return definition, warningsupdate_params: list[dict[str, Any]] = wf_def.get("parameters", [])existing_workflow = await get_workflow_by_id(workflow_id)existing_wf_def = existing_workflow.get("workflow_definition")if not isinstance(existing_wf_def, dict):- return definition+ return definition, warningsexisting_params: list[dict[str, Any]] = existing_wf_def.get("parameters", [])@@ -916,10 +1049,47 @@ async def _inject_workflow_update_parameters(definition: str, fmt: str, workflowmodified = Trueblock["parameter_keys"] = block_pkeys+ # --- Step 3: Re-attach non-credential block<->parameter links the caller dropped ---+ # Plain workflow/context/output parameter links live only in a block's parameter_keys. When the+ # caller (often an LLM) regenerates a block and omits a key that the same-label block carried+ # before — while still declaring the parameter — re-attach it. Unlike credentials this is+ # best-effort and warned: to intentionally drop the link, remove the parameter declaration too.+ declared_keys = {p["key"] for p in update_params if isinstance(p, dict) and p.get("key")}+ prior_block_param_keys: dict[str, list[str]] = {}+ for block in _iter_blocks_flat(existing_wf_def.get("blocks", [])):+ if not isinstance(block, dict):+ continue+ label = block.get("label")+ if isinstance(label, str):+ prior_block_param_keys[label] = _block_authoring_parameter_keys(block)++ for block in _iter_blocks_flat(wf_def.get("blocks", [])):+ if not isinstance(block, dict):+ continue+ label = block.get("label")+ if not isinstance(label, str) or label not in prior_block_param_keys:+ continue+ block_link_keys = _block_authoring_parameter_keys(block)+ block_link_set = set(block_link_keys)+ added = False+ for link_key in prior_block_param_keys[label]:+ if link_key in all_cred_keys or link_key in block_link_set or link_key not in declared_keys:+ continue+ block_link_keys.append(link_key)+ block_link_set.add(link_key)+ warnings.append(+ f"Re-attached parameter '{link_key}' to block '{label}': it was linked in the prior version "+ f"and this update omitted it. To intentionally drop the link, remove the parameter too."+ )+ added = True+ modified = True+ if added:+ block["parameter_keys"] = block_link_keys+if not modified:- return definition+ return definition, warnings- return _dump_definition_dict(raw, parsed_format)+ return _dump_definition_dict(raw, parsed_format), warningsdef _parse_definition(definition: str, fmt: str) -> tuple[dict[str, Any] | None, str | None, dict[str, Any] | None]:@@ -1029,6 +1199,14 @@ async def skyvern_workflow_get(error=make_error(ErrorCode.API_ERROR, str(e), "Check your API key and workflow ID"),)+ if isinstance(wf_data, dict) and isinstance(wf_data.get("workflow_definition"), dict):+ # Return the authoring shape so a get -> edit -> update round trip preserves block<->parameter+ # links (parameter_keys / loop_over_parameter_key / source_parameter_key) the runtime shape hides.+ wf_data = {+ **wf_data,+ "workflow_definition": _workflow_definition_to_authoring_shape(wf_data["workflow_definition"]),+ }+version_str = f", version={version}" if version is not None else ""return make_result("skyvern_workflow_get",@@ -1207,10 +1385,11 @@ async def skyvern_workflow_update(),)+ param_warnings: list[str] = []try:definition = await _inject_workflow_update_proxy_default(definition, format, workflow_id)definition = await _inject_workflow_update_top_level_settings(definition, format, workflow_id)- definition = await _inject_workflow_update_parameters(definition, format, workflow_id)+ definition, param_warnings = await _inject_workflow_update_parameters(definition, format, workflow_id)except NotFoundError:return make_result("skyvern_workflow_update",@@ -1274,6 +1453,8 @@ async def skyvern_workflow_update(data = _serialize_workflow(workflow)fmt_label = "json_definition" if json_def is not None else "yaml_definition"data["sdk_equivalent"] = f"await skyvern.update_workflow({workflow_id!r}, {fmt_label}=<definition>)"+ if param_warnings:+ data["warnings"] = param_warningsreturn make_result("skyvern_workflow_update", data=data, timing_ms=timer.timing_ms)diff --git a/tests/unit/test_mcp_workflow_tools.py b/tests/unit/test_mcp_workflow_tools.pyindex ddb844237..3cfd8aff7 100644--- a/tests/unit/test_mcp_workflow_tools.py+++ b/tests/unit/test_mcp_workflow_tools.py@@ -3419,3 +3419,390 @@ async def test_workflow_update_does_not_duplicate_keys_on_positional_reinjectionsent_def = request_mock.await_args.kwargs["json"]["json_definition"]login_block = sent_def["workflow_definition"]["blocks"][0]assert login_block.get("parameter_keys", []).count("credentials") == 1+++# --- Non-credential parameter detachment (MCP get->edit->update round trip) ---+++async def test_workflow_update_preserves_noncredential_parameter_keys_when_omitted(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Layer B: a still-declared non-credential parameter that the update drops from a block's+ parameter_keys (block matched by stable label) is re-attached, and the reattachment is surfaced+ as a warning. Mirrors the credential reattachment but for plain workflow params."""++ request_mock = _patch_skyvern_http(monkeypatch, response_payload=_fake_workflow_dict())++ async def fake_get_workflow_by_id(workflow_id: str, version: int | None = None) -> dict[str, object]:+ return {+ "proxy_location": "RESIDENTIAL",+ "workflow_definition": {+ "parameters": [+ {+ "parameter_type": "workflow",+ "key": "report_url",+ "workflow_parameter_type": "string",+ "default_value": "https://example.com/report",+ },+ ],+ "blocks": [+ {+ "block_type": "code",+ "label": "build_report",+ "parameter_keys": ["report_url"],+ "code": "x = report_url",+ },+ ],+ },+ }++ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)++ # Keeps the param declared, but the regenerated block dropped it from parameter_keys.+ definition = {+ "title": "Updated workflow",+ "proxy_location": "RESIDENTIAL",+ "workflow_definition": {+ "parameters": [+ {+ "parameter_type": "workflow",+ "key": "report_url",+ "workflow_parameter_type": "string",+ "default_value": "https://example.com/report",+ },+ ],+ "blocks": [+ {"block_type": "code", "label": "build_report", "code": "x = report_url # edited"},+ ],+ },+ }++ result = await workflow_tools.skyvern_workflow_update(+ workflow_id="wpid_test",+ definition=json.dumps(definition),+ format="json",+ )++ assert result["ok"] is True+ sent_def = request_mock.await_args.kwargs["json"]["json_definition"]+ block = sent_def["workflow_definition"]["blocks"][0]+ assert block.get("parameter_keys") == ["report_url"]+ warnings = result.get("data", {}).get("warnings") or []+ assert any("report_url" in w and "build_report" in w for w in warnings)+++async def test_workflow_update_does_not_reinject_intentionally_removed_parameter(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Layer B guard: if the update removes the parameter DECLARATION too (not just the block link),+ the link is not re-injected — that is the explicit-removal path."""++ request_mock = _patch_skyvern_http(monkeypatch, response_payload=_fake_workflow_dict())++ async def fake_get_workflow_by_id(workflow_id: str, version: int | None = None) -> dict[str, object]:+ return {+ "proxy_location": "RESIDENTIAL",+ "workflow_definition": {+ "parameters": [+ {"parameter_type": "workflow", "key": "report_url", "workflow_parameter_type": "string"},+ ],+ "blocks": [+ {"block_type": "code", "label": "build_report", "parameter_keys": ["report_url"], "code": "x"},+ ],+ },+ }++ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)++ definition = {+ "title": "Updated workflow",+ "proxy_location": "RESIDENTIAL",+ "workflow_definition": {+ "parameters": [],+ "blocks": [{"block_type": "code", "label": "build_report", "code": "x"}],+ },+ }++ result = await workflow_tools.skyvern_workflow_update(+ workflow_id="wpid_test",+ definition=json.dumps(definition),+ format="json",+ )++ assert result["ok"] is True+ sent_def = request_mock.await_args.kwargs["json"]["json_definition"]+ block = sent_def["workflow_definition"]["blocks"][0]+ assert "report_url" not in (block.get("parameter_keys") or [])+++async def test_workflow_get_emits_parameter_keys_and_strips_runtime_block_fields(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Layer A: skyvern_workflow_get returns the authoring shape — derive block parameter_keys from+ resolved runtime `parameters`, strip `parameters`/`output_parameter`, and drop auto output params+ from the top-level parameter list."""++ async def fake_get_workflow_by_id(workflow_id: str, version: int | None = None) -> dict[str, object]:+ return _fake_workflow_dict(+ workflow_definition={+ "parameters": [+ {+ "parameter_type": "workflow",+ "key": "report_url",+ "workflow_parameter_type": "string",+ "default_value": "https://e.com",+ "workflow_parameter_id": "wp_1",+ "workflow_id": "wf_test",+ },+ {+ "parameter_type": "output",+ "key": "build_report_output",+ "output_parameter_id": "op_1",+ "workflow_id": "wf_test",+ },+ ],+ "blocks": [+ {+ "block_type": "code",+ "label": "build_report",+ "code": "x = report_url",+ "parameters": [+ {+ "parameter_type": "workflow",+ "key": "report_url",+ "workflow_parameter_type": "string",+ "default_value": "https://e.com",+ "workflow_parameter_id": "wp_1",+ "workflow_id": "wf_test",+ },+ ],+ "output_parameter": {+ "parameter_type": "output",+ "key": "build_report_output",+ "output_parameter_id": "op_1",+ "workflow_id": "wf_test",+ },+ },+ ],+ }+ )++ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)++ result = await workflow_tools.skyvern_workflow_get(workflow_id="wpid_test")+ assert result["ok"] is True+ wf_def = result["data"]["workflow_definition"]+ block = wf_def["blocks"][0]+ assert block.get("parameter_keys") == ["report_url"]+ assert "parameters" not in block+ assert "output_parameter" not in block+ top_keys = [p["key"] for p in wf_def["parameters"]]+ assert "report_url" in top_keys+ assert "build_report_output" not in top_keys+++async def test_workflow_get_maps_for_loop_and_context_source_to_authoring(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Layer A: for-loop runtime `loop_over` object maps to authoring `loop_over_parameter_key`, and a+ context parameter's runtime `source` object maps to `source_parameter_key`."""++ async def fake_get_workflow_by_id(workflow_id: str, version: int | None = None) -> dict[str, object]:+ return _fake_workflow_dict(+ workflow_definition={+ "parameters": [+ {+ "parameter_type": "workflow",+ "key": "urls",+ "workflow_parameter_type": "json",+ "default_value": [],+ },+ {+ "parameter_type": "context",+ "key": "current_url",+ "source": {"parameter_type": "workflow", "key": "urls", "workflow_parameter_type": "json"},+ },+ ],+ "blocks": [+ {+ "block_type": "for_loop",+ "label": "each_url",+ "loop_over": {+ "parameter_type": "workflow",+ "key": "urls",+ "workflow_parameter_type": "json",+ "workflow_parameter_id": "wp_urls",+ },+ "loop_blocks": [+ {"block_type": "code", "label": "inner", "code": "y", "parameters": []},+ ],+ },+ ],+ }+ )++ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)++ result = await workflow_tools.skyvern_workflow_get(workflow_id="wpid_test")+ assert result["ok"] is True+ wf_def = result["data"]["workflow_definition"]+ loop_block = wf_def["blocks"][0]+ assert loop_block.get("loop_over_parameter_key") == "urls"+ assert "loop_over" not in loop_block+ context_param = next(p for p in wf_def["parameters"] if p["key"] == "current_url")+ assert context_param.get("source_parameter_key") == "urls"+ assert "source" not in context_param+++async def test_workflow_update_preserves_noncredential_link_from_runtime_shaped_existing(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Layer B (prod shape): the existing workflow from GET is in RUNTIME shape — block links live in+ resolved `parameters` objects, not `parameter_keys`. Step 3 must read that shape to detect the+ prior link and re-attach the omitted key."""++ request_mock = _patch_skyvern_http(monkeypatch, response_payload=_fake_workflow_dict())++ async def fake_get_workflow_by_id(workflow_id: str, version: int | None = None) -> dict[str, object]:+ return {+ "proxy_location": "RESIDENTIAL",+ "workflow_definition": {+ "parameters": [+ {+ "parameter_type": "workflow",+ "key": "report_url",+ "workflow_parameter_type": "string",+ "default_value": "https://e.com",+ "workflow_parameter_id": "wp_1",+ "workflow_id": "wf_test",+ },+ ],+ "blocks": [+ {+ "block_type": "code",+ "label": "build_report",+ "code": "x = report_url",+ "parameters": [+ {+ "parameter_type": "workflow",+ "key": "report_url",+ "workflow_parameter_type": "string",+ "default_value": "https://e.com",+ "workflow_parameter_id": "wp_1",+ "workflow_id": "wf_test",+ },+ ],+ "output_parameter": {+ "parameter_type": "output",+ "key": "build_report_output",+ "output_parameter_id": "op_1",+ "workflow_id": "wf_test",+ },+ },+ ],+ },+ }++ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)++ definition = {+ "title": "Updated workflow",+ "proxy_location": "RESIDENTIAL",+ "workflow_definition": {+ "parameters": [+ {+ "parameter_type": "workflow",+ "key": "report_url",+ "workflow_parameter_type": "string",+ "default_value": "https://e.com",+ },+ ],+ "blocks": [{"block_type": "code", "label": "build_report", "code": "x = report_url # edited"}],+ },+ }++ result = await workflow_tools.skyvern_workflow_update(+ workflow_id="wpid_test",+ definition=json.dumps(definition),+ format="json",+ )++ assert result["ok"] is True+ sent_def = request_mock.await_args.kwargs["json"]["json_definition"]+ block = sent_def["workflow_definition"]["blocks"][0]+ assert block.get("parameter_keys") == ["report_url"]+ warnings = result.get("data", {}).get("warnings") or []+ assert any("report_url" in w and "build_report" in w for w in warnings)+++async def test_get_then_update_round_trip_preserves_block_parameter_link(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """End-to-end: GET returns the authoring shape; feeding it straight back into update preserves the+ block<->parameter link with no manual re-add, and leaks no runtime fields to the backend."""++ runtime_definition = {+ "parameters": [+ {+ "parameter_type": "workflow",+ "key": "report_url",+ "workflow_parameter_type": "string",+ "default_value": "https://e.com",+ "workflow_parameter_id": "wp_1",+ "workflow_id": "wf_test",+ },+ ],+ "blocks": [+ {+ "block_type": "code",+ "label": "build_report",+ "code": "x = report_url",+ "parameters": [+ {+ "parameter_type": "workflow",+ "key": "report_url",+ "workflow_parameter_type": "string",+ "default_value": "https://e.com",+ "workflow_parameter_id": "wp_1",+ "workflow_id": "wf_test",+ },+ ],+ "output_parameter": {+ "parameter_type": "output",+ "key": "build_report_output",+ "output_parameter_id": "op_1",+ "workflow_id": "wf_test",+ },+ },+ ],+ }++ async def fake_get_workflow_by_id(workflow_id: str, version: int | None = None) -> dict[str, object]:+ return _fake_workflow_dict(proxy_location="RESIDENTIAL", workflow_definition=runtime_definition)++ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)+ request_mock = _patch_skyvern_http(monkeypatch, response_payload=_fake_workflow_dict())++ got = await workflow_tools.skyvern_workflow_get(workflow_id="wpid_test")+ assert got["ok"] is True+ authoring_wf_def = got["data"]["workflow_definition"]+ echo = {"title": "Round trip", "proxy_location": "RESIDENTIAL", "workflow_definition": authoring_wf_def}++ result = await workflow_tools.skyvern_workflow_update(+ workflow_id="wpid_test",+ definition=json.dumps(echo),+ format="json",+ )++ assert result["ok"] is True+ sent_def = request_mock.await_args.kwargs["json"]["json_definition"]+ block = sent_def["workflow_definition"]["blocks"][0]+ assert block.get("parameter_keys") == ["report_url"]+ assert "parameters" not in block+ assert "output_parameter" not in block