Skip to content

Automate browser based workflows with AI

fix(SKY-11248): reattach login credentials on MCP workflow edits (#6761)

Shuchang Zhengcbf4e8da8ddd
diff --git a/skyvern/cli/mcp_tools/workflow.py b/skyvern/cli/mcp_tools/workflow.py
index baa23ae48..8722d6b64 100644
--- a/skyvern/cli/mcp_tools/workflow.py
+++ b/skyvern/cli/mcp_tools/workflow.py
@@ -710,6 +710,53 @@ def _iter_blocks_flat(blocks: list[dict[str, Any]]) -> list[dict[str, Any]]:
return result
+def _iter_positional_block_matches(
+ existing_blocks: list[dict[str, Any]],
+ update_blocks: list[dict[str, Any]],
+ *,
+ parent_has_identity: bool = True,
+) -> list[tuple[str | None, bool]]:
+ """Return positional block types plus whether position identifies the block.
+
+ A position is considered identifying only when the sibling shape is unchanged
+ and the block either kept its label or is the sole child under an identified
+ parent. This lets us repair dropped block_type values for singleton edits
+ without using bare index to swap credentials across reordered siblings.
+ """
+ result: list[tuple[str | None, bool]] = []
+ existing_dict_blocks = [block for block in existing_blocks if isinstance(block, dict)]
+ update_dict_blocks = [block for block in update_blocks if isinstance(block, dict)]
+ same_sibling_count = len(existing_dict_blocks) == len(update_dict_blocks)
+
+ for index, update_block in enumerate(update_dict_blocks):
+ existing_block = existing_dict_blocks[index] if same_sibling_count else None
+ raw_existing_block_type = existing_block.get("block_type") if existing_block else None
+ existing_block_type = raw_existing_block_type if isinstance(raw_existing_block_type, str) else None
+ existing_label = existing_block.get("label") if existing_block else None
+ update_label = update_block.get("label")
+ stable_label_match = bool(existing_label and update_label and existing_label == update_label)
+ singleton_position = same_sibling_count and len(update_dict_blocks) == 1
+ has_identity = parent_has_identity and same_sibling_count and (stable_label_match or singleton_position)
+ result.append((existing_block_type, has_identity))
+
+ update_loop_blocks = update_block.get("loop_blocks")
+ if not isinstance(update_loop_blocks, list):
+ continue
+
+ existing_loop_blocks = existing_block.get("loop_blocks") if existing_block else None
+ if not isinstance(existing_loop_blocks, list):
+ existing_loop_blocks = []
+ result.extend(
+ _iter_positional_block_matches(
+ existing_loop_blocks,
+ update_loop_blocks,
+ parent_has_identity=has_identity,
+ )
+ )
+
+ return result
+
+
async def _inject_workflow_update_parameters(definition: str, fmt: str, workflow_id: str) -> str:
"""Preserve protected credential/secret parameters during MCP workflow updates.
@@ -717,9 +764,8 @@ async def _inject_workflow_update_parameters(definition: str, fmt: str, workflow
values always win. This function:
1. Always replaces protected parameters with the existing workflow's versions
(even if the caller includes them — they may have stale/wrong data).
- 2. Injects credential parameter_keys into blocks using type-based matching
- (login blocks always get ALL credential keys) with label-based fallback
- for non-login blocks.
+ 2. Injects credential parameter_keys into blocks using per-block matches
+ with label-based fallback.
"""
raw, parsed_format = _load_definition_dict(definition, fmt)
@@ -765,11 +811,6 @@ async def _inject_workflow_update_parameters(definition: str, fmt: str, workflow
wf_def["parameters"] = update_params
# --- Step 2: Inject credential parameter keys into blocks ---
- # Login blocks get credential-type keys via type-based matching (resilient to label
- # renames by Claude). Non-login blocks fall back to label-based matching — so if Claude
- # renames a non-login block that references aws_secret/bitwarden/etc., the key reference
- # is lost. This asymmetry is accepted because login blocks are the critical path for
- # credential injection; non-login secret refs are rare and still work when labels match.
all_cred_keys: set[str] = set()
login_cred_keys: set[str] = set()
for param in existing_params:
@@ -781,29 +822,92 @@ async def _inject_workflow_update_parameters(definition: str, fmt: str, workflow
if all_cred_keys:
existing_blocks: list[dict[str, Any]] = existing_wf_def.get("blocks", [])
update_blocks: list[dict[str, Any]] = wf_def.get("blocks", [])
-
- # Build label-based map for fallback (non-login blocks)
+ existing_blocks_flat = _iter_blocks_flat(existing_blocks)
+ update_blocks_flat = _iter_blocks_flat(update_blocks)
+ n_existing_blocks = len(existing_blocks_flat)
+ n_update_blocks = len(update_blocks_flat)
+ positional_block_matches = _iter_positional_block_matches(existing_blocks, update_blocks)
+
+ existing_block_type_by_label: dict[str, str] = {}
+ # Login labels resolve through existing_block_login_cred_keys; this map
+ # keeps all protected keys for non-login label recovery.
existing_block_cred_keys: dict[str, list[str]] = {}
- for block in _iter_blocks_flat(existing_blocks):
+ existing_block_login_cred_keys: dict[str, list[str]] = {}
+ existing_login_block_login_keys: list[list[str]] = []
+ existing_login_block_count = 0
+ for block in existing_blocks_flat:
+ raw_existing_block_type = block.get("block_type")
+ existing_block_type = raw_existing_block_type if isinstance(raw_existing_block_type, str) else None
+ existing_pkeys = block.get("parameter_keys") or []
+ cred_keys = sorted(k for k in existing_pkeys if k in all_cred_keys)
+ login_keys = sorted(k for k in existing_pkeys if k in login_cred_keys)
+ if existing_block_type == "login":
+ existing_login_block_count += 1
+ if login_keys:
+ existing_login_block_login_keys.append(login_keys)
+
label = block.get("label")
if not label:
continue
- existing_pkeys = block.get("parameter_keys") or []
- cred_keys = [k for k in existing_pkeys if k in all_cred_keys]
+ if existing_block_type:
+ existing_block_type_by_label[label] = existing_block_type
if cred_keys:
existing_block_cred_keys[label] = cred_keys
+ if login_keys:
+ existing_block_login_cred_keys[label] = login_keys
+
+ # Label matches identify block ownership. Positional reuse can only restore
+ # a dropped login type when the login credential choice is unambiguous.
+ use_positional_correspondence = n_existing_blocks == n_update_blocks
+ single_global_login_cred_keys = sorted(login_cred_keys) if len(login_cred_keys) == 1 else []
+ single_existing_login_block_keys = (
+ existing_login_block_login_keys[0]
+ if len(existing_login_block_login_keys) == 1 and existing_login_block_count == 1
+ else []
+ )
+ single_login_cred_keys = single_existing_login_block_keys or single_global_login_cred_keys
- for block in _iter_blocks_flat(update_blocks):
+ for index, block in enumerate(update_blocks_flat):
block_type = block.get("block_type")
label = block.get("label")
+ positional_block_type: str | None = None
+ positional_has_identity = False
+ if use_positional_correspondence and index < len(positional_block_matches):
+ positional_block_type, positional_has_identity = positional_block_matches[index]
keys_to_inject: list[str] = []
+ block_type_to_restore: str | None = None
if block_type == "login":
- keys_to_inject = sorted(login_cred_keys)
+ if label and label in existing_block_login_cred_keys:
+ keys_to_inject = existing_block_login_cred_keys[label]
+ elif single_login_cred_keys:
+ keys_to_inject = single_login_cred_keys
+ elif not block_type:
+ # Recover by stable label first. If identity is gone, only recover
+ # the single-login case; leave other ambiguous secrets untouched.
+ label_block_type = existing_block_type_by_label.get(label) if label else None
+ if label_block_type == "login":
+ if label and label in existing_block_login_cred_keys:
+ keys_to_inject = existing_block_login_cred_keys[label]
+ block_type_to_restore = label_block_type
+ elif single_login_cred_keys:
+ keys_to_inject = single_login_cred_keys
+ block_type_to_restore = label_block_type
+ elif label_block_type:
+ if label and label in existing_block_cred_keys:
+ keys_to_inject = existing_block_cred_keys[label]
+ block_type_to_restore = label_block_type
+ elif positional_has_identity and positional_block_type == "login":
+ if single_login_cred_keys:
+ keys_to_inject = single_login_cred_keys
+ block_type_to_restore = positional_block_type
elif label and label in existing_block_cred_keys:
- keys_to_inject = sorted(existing_block_cred_keys[label])
+ keys_to_inject = existing_block_cred_keys[label]
if keys_to_inject:
+ if block_type_to_restore:
+ block["block_type"] = block_type_to_restore
+ modified = True
block_pkeys: list[str] = list(block.get("parameter_keys") or [])
current_keys = set(block_pkeys)
for cred_key in keys_to_inject:
diff --git a/tests/unit/test_mcp_workflow_tools.py b/tests/unit/test_mcp_workflow_tools.py
index 52dbad586..ddb844237 100644
--- a/tests/unit/test_mcp_workflow_tools.py
+++ b/tests/unit/test_mcp_workflow_tools.py
@@ -1833,6 +1833,64 @@ async def test_workflow_update_login_block_only_gets_credential_type_keys_not_aw
assert "api_secret" in task_pkeys
+async def test_workflow_update_login_label_fallback_excludes_non_login_secret(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "aws_secret",
+ "key": "api_secret",
+ "aws_key": "my-secret-key",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "task",
+ "label": "api_call",
+ "parameter_keys": ["api_secret"],
+ "navigation_goal": "Make API call",
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "api_call",
+ "navigation_goal": "Login",
+ },
+ ],
+ },
+ }
+
+ 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"]
+ login_block = sent_def["workflow_definition"]["blocks"][0]
+ assert "api_secret" not in (login_block.get("parameter_keys") or [])
+
+
@pytest.mark.asyncio
async def test_workflow_update_strips_runtime_fields_from_credential_params(
monkeypatch: pytest.MonkeyPatch,
@@ -2318,3 +2376,1046 @@ async def test_workflow_update_injects_azure_vault_key_into_login_block_paramete
login_block = next(b for b in blocks if b.get("block_type") == "login")
pkeys = login_block.get("parameter_keys", [])
assert "azure_creds" in pkeys
+
+
+async def test_workflow_update_reattaches_credential_when_login_block_type_and_label_changed(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "credential",
+ "key": "credentials",
+ "credential_id": "cred_a",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "old_login",
+ "parameter_keys": ["credentials"],
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "label": "new_login",
+ "navigation_goal": "Login",
+ },
+ ],
+ },
+ }
+
+ 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"]
+ workflow_tools.WorkflowCreateYAMLRequestSchema.model_validate(sent_def)
+ blocks = sent_def["workflow_definition"]["blocks"]
+ login_block = next(b for b in blocks if b.get("label") == "new_login")
+ assert login_block.get("block_type") == "login"
+ assert "credentials" in login_block.get("parameter_keys", [])
+
+
+async def test_workflow_update_reattaches_credential_when_login_block_type_dropped_label_kept(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "credential",
+ "key": "credentials",
+ "credential_id": "cred_a",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "login_block",
+ "parameter_keys": ["credentials"],
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "label": "login_block",
+ "navigation_goal": "Login",
+ },
+ ],
+ },
+ }
+
+ 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"]
+ workflow_tools.WorkflowCreateYAMLRequestSchema.model_validate(sent_def)
+ blocks = sent_def["workflow_definition"]["blocks"]
+ login_block = next(b for b in blocks if b.get("label") == "login_block")
+ assert login_block.get("block_type") == "login"
+ assert "credentials" in login_block.get("parameter_keys", [])
+
+
+async def test_workflow_update_does_not_cross_contaminate_multiple_login_credentials(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "credential",
+ "key": "cred_a",
+ "credential_id": "credential_a",
+ },
+ {
+ "parameter_type": "credential",
+ "key": "cred_b",
+ "credential_id": "credential_b",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "login_a",
+ "parameter_keys": ["cred_a"],
+ },
+ {
+ "block_type": "login",
+ "label": "login_b",
+ "parameter_keys": ["cred_b"],
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "login_a",
+ "parameter_keys": [],
+ "navigation_goal": "Login A",
+ },
+ {
+ "block_type": "login",
+ "label": "login_b",
+ "parameter_keys": [],
+ "navigation_goal": "Login B",
+ },
+ ],
+ },
+ }
+
+ 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"]
+ blocks = sent_def["workflow_definition"]["blocks"]
+ login_a = next(b for b in blocks if b.get("label") == "login_a")
+ login_b = next(b for b in blocks if b.get("label") == "login_b")
+ assert login_a.get("parameter_keys", []) == ["cred_a"]
+ assert login_b.get("parameter_keys", []) == ["cred_b"]
+
+
+async def test_workflow_update_prefers_login_label_match_when_blocks_reordered(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "credential",
+ "key": "cred_a",
+ "credential_id": "credential_a",
+ },
+ {
+ "parameter_type": "credential",
+ "key": "cred_b",
+ "credential_id": "credential_b",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "login_a",
+ "parameter_keys": ["cred_a"],
+ },
+ {
+ "block_type": "login",
+ "label": "login_b",
+ "parameter_keys": ["cred_b"],
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "login_b",
+ "parameter_keys": [],
+ "navigation_goal": "Login B",
+ },
+ {
+ "block_type": "login",
+ "label": "login_a",
+ "parameter_keys": [],
+ "navigation_goal": "Login A",
+ },
+ ],
+ },
+ }
+
+ 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"]
+ blocks = sent_def["workflow_definition"]["blocks"]
+ login_b = next(b for b in blocks if b.get("label") == "login_b")
+ login_a = next(b for b in blocks if b.get("label") == "login_a")
+ assert login_b.get("parameter_keys", []) == ["cred_b"]
+ assert login_a.get("parameter_keys", []) == ["cred_a"]
+
+
+async def test_workflow_update_does_not_guess_multi_login_credentials_when_labels_renamed_and_reordered(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "credential",
+ "key": "cred_a",
+ "credential_id": "credential_a",
+ },
+ {
+ "parameter_type": "credential",
+ "key": "cred_b",
+ "credential_id": "credential_b",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "login_a",
+ "parameter_keys": ["cred_a"],
+ },
+ {
+ "block_type": "login",
+ "label": "login_b",
+ "parameter_keys": ["cred_b"],
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "renamed_login_b",
+ "parameter_keys": [],
+ "navigation_goal": "Login B",
+ },
+ {
+ "block_type": "login",
+ "label": "renamed_login_a",
+ "parameter_keys": [],
+ "navigation_goal": "Login A",
+ },
+ ],
+ },
+ }
+
+ 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"]
+ blocks = sent_def["workflow_definition"]["blocks"]
+ login_b = next(b for b in blocks if b.get("label") == "renamed_login_b")
+ login_a = next(b for b in blocks if b.get("label") == "renamed_login_a")
+ assert login_b.get("parameter_keys", []) == []
+ assert login_a.get("parameter_keys", []) == []
+
+
+async def test_workflow_update_does_not_overattach_single_key_when_multiple_login_blocks_renamed(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "credential",
+ "key": "primary_login_cred",
+ "credential_id": "credential_a",
+ },
+ {
+ "parameter_type": "credential",
+ "key": "secondary_login_cred",
+ "credential_id": "credential_b",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "login_a",
+ "parameter_keys": ["primary_login_cred"],
+ },
+ {
+ "block_type": "login",
+ "label": "login_b",
+ "parameter_keys": [],
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "renamed_login_a",
+ "parameter_keys": [],
+ "navigation_goal": "Login A",
+ },
+ {
+ "block_type": "login",
+ "label": "renamed_login_b",
+ "parameter_keys": [],
+ "navigation_goal": "Login B",
+ },
+ ],
+ },
+ }
+
+ 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"]
+ blocks = sent_def["workflow_definition"]["blocks"]
+ login_a = next(b for b in blocks if b.get("label") == "renamed_login_a")
+ login_b = next(b for b in blocks if b.get("label") == "renamed_login_b")
+ assert login_a.get("parameter_keys", []) == []
+ assert login_b.get("parameter_keys", []) == []
+
+
+async def test_workflow_update_prefers_label_match_when_untyped_blocks_reordered(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "credential",
+ "key": "credentials",
+ "credential_id": "cred_a",
+ },
+ {
+ "parameter_type": "aws_secret",
+ "key": "api_secret",
+ "aws_key": "my-secret-key",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "login_block",
+ "parameter_keys": ["credentials"],
+ },
+ {
+ "block_type": "task",
+ "label": "api_call",
+ "parameter_keys": ["api_secret"],
+ "navigation_goal": "Make API call",
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "label": "api_call",
+ "navigation_goal": "Make API call",
+ },
+ {
+ "label": "login_block",
+ "navigation_goal": "Login",
+ },
+ ],
+ },
+ }
+
+ 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"]
+ workflow_tools.WorkflowCreateYAMLRequestSchema.model_validate(sent_def)
+ blocks = sent_def["workflow_definition"]["blocks"]
+ task_block = next(b for b in blocks if b.get("label") == "api_call")
+ login_block = next(b for b in blocks if b.get("label") == "login_block")
+ assert task_block.get("block_type") == "task"
+ assert task_block.get("parameter_keys", []) == ["api_secret"]
+ assert login_block.get("block_type") == "login"
+ assert login_block.get("parameter_keys", []) == ["credentials"]
+
+
+async def test_workflow_update_does_not_guess_non_login_secrets_when_untyped_labels_renamed_and_reordered(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "aws_secret",
+ "key": "secret_a",
+ "aws_key": "secret-a",
+ },
+ {
+ "parameter_type": "aws_secret",
+ "key": "secret_b",
+ "aws_key": "secret-b",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "task",
+ "label": "task_a",
+ "parameter_keys": ["secret_a"],
+ "navigation_goal": "Task A",
+ },
+ {
+ "block_type": "task",
+ "label": "task_b",
+ "parameter_keys": ["secret_b"],
+ "navigation_goal": "Task B",
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "label": "renamed_task_b",
+ "navigation_goal": "Task B",
+ },
+ {
+ "label": "renamed_task_a",
+ "navigation_goal": "Task A",
+ },
+ ],
+ },
+ }
+
+ 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"]
+ blocks = sent_def["workflow_definition"]["blocks"]
+ task_b = next(b for b in blocks if b.get("label") == "renamed_task_b")
+ task_a = next(b for b in blocks if b.get("label") == "renamed_task_a")
+ assert task_b.get("parameter_keys", []) == []
+ assert task_a.get("parameter_keys", []) == []
+ assert "block_type" not in task_b
+ assert "block_type" not in task_a
+
+
+async def test_workflow_update_does_not_restore_untyped_login_from_renamed_reordered_position(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "credential",
+ "key": "credentials",
+ "credential_id": "cred_a",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "old_login",
+ "parameter_keys": ["credentials"],
+ },
+ {
+ "block_type": "task",
+ "label": "old_task",
+ "parameter_keys": [],
+ "navigation_goal": "Do task",
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "label": "renamed_task",
+ "navigation_goal": "Do task",
+ },
+ {
+ "label": "renamed_login",
+ "navigation_goal": "Login",
+ },
+ ],
+ },
+ }
+
+ 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"]
+ blocks = sent_def["workflow_definition"]["blocks"]
+ task_block = next(b for b in blocks if b.get("label") == "renamed_task")
+ login_block = next(b for b in blocks if b.get("label") == "renamed_login")
+ assert task_block.get("parameter_keys", []) == []
+ assert login_block.get("parameter_keys", []) == []
+ assert "block_type" not in task_block
+ assert "block_type" not in login_block
+
+
+async def test_workflow_update_does_not_attach_all_login_credentials_when_mapping_is_ambiguous(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "credential",
+ "key": "cred_a",
+ "credential_id": "credential_a",
+ },
+ {
+ "parameter_type": "credential",
+ "key": "cred_b",
+ "credential_id": "credential_b",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "login_a",
+ "parameter_keys": ["cred_a"],
+ },
+ {
+ "block_type": "login",
+ "label": "login_b",
+ "parameter_keys": ["cred_b"],
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "renamed_login",
+ "parameter_keys": [],
+ "navigation_goal": "Login",
+ }
+ ],
+ },
+ }
+
+ 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"]
+ login_block = sent_def["workflow_definition"]["blocks"][0]
+ assert login_block.get("parameter_keys", []) == []
+
+
+async def test_workflow_update_login_in_nested_loop_reattaches_credential(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "credential",
+ "key": "credentials",
+ "credential_id": "cred_a",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "for_loop",
+ "label": "loop_block",
+ "loop_blocks": [
+ {
+ "block_type": "login",
+ "label": "old_login",
+ "parameter_keys": ["credentials"],
+ },
+ ],
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "block_type": "for_loop",
+ "label": "loop_block",
+ "loop_blocks": [
+ {
+ "label": "new_login",
+ "navigation_goal": "Login",
+ },
+ ],
+ },
+ ],
+ },
+ }
+
+ 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"]
+ workflow_tools.WorkflowCreateYAMLRequestSchema.model_validate(sent_def)
+ loop_block = sent_def["workflow_definition"]["blocks"][0]
+ login_block = loop_block["loop_blocks"][0]
+ assert login_block.get("block_type") == "login"
+ assert "credentials" in login_block.get("parameter_keys", [])
+
+
+async def test_workflow_update_single_login_block_still_gets_all_login_keys_when_unmatchable(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "credential",
+ "key": "credentials",
+ "credential_id": "cred_a",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "old_login",
+ "parameter_keys": [],
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "new_login",
+ "parameter_keys": [],
+ "navigation_goal": "Login",
+ },
+ ],
+ },
+ }
+
+ 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"]
+ login_block = sent_def["workflow_definition"]["blocks"][0]
+ assert "credentials" in login_block.get("parameter_keys", [])
+
+
+async def test_workflow_update_reattaches_unique_login_block_key_when_other_login_capable_params_exist(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "credential",
+ "key": "login_cred",
+ "credential_id": "cred_login",
+ },
+ {
+ "parameter_type": "workflow",
+ "key": "api_cred",
+ "workflow_parameter_type": "credential_id",
+ "default_value": "cred_api",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "old_login",
+ "parameter_keys": ["login_cred"],
+ },
+ {
+ "block_type": "task",
+ "label": "api_task",
+ "parameter_keys": ["api_cred"],
+ "navigation_goal": "Call API",
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "renamed_login",
+ "parameter_keys": [],
+ "navigation_goal": "Login",
+ },
+ {
+ "block_type": "task",
+ "label": "api_task",
+ "parameter_keys": [],
+ "navigation_goal": "Call API",
+ },
+ ],
+ },
+ }
+
+ 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"]
+ blocks = sent_def["workflow_definition"]["blocks"]
+ login_block = next(b for b in blocks if b.get("label") == "renamed_login")
+ task_block = next(b for b in blocks if b.get("label") == "api_task")
+ assert login_block.get("parameter_keys", []) == ["login_cred"]
+ assert task_block.get("parameter_keys", []) == ["api_cred"]
+
+
+async def test_workflow_update_untyped_single_login_block_still_gets_single_login_key(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "credential",
+ "key": "credentials",
+ "credential_id": "cred_a",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "old_login",
+ "parameter_keys": [],
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "label": "new_login",
+ "parameter_keys": [],
+ "navigation_goal": "Login",
+ },
+ ],
+ },
+ }
+
+ 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"]
+ workflow_tools.WorkflowCreateYAMLRequestSchema.model_validate(sent_def)
+ login_block = sent_def["workflow_definition"]["blocks"][0]
+ assert login_block.get("block_type") == "login"
+ assert login_block.get("parameter_keys", []) == ["credentials"]
+
+
+async def test_workflow_update_does_not_duplicate_keys_on_positional_reinjection(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ 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": "credential",
+ "key": "credentials",
+ "credential_id": "cred_a",
+ },
+ ],
+ "blocks": [
+ {
+ "block_type": "login",
+ "label": "old_login",
+ "parameter_keys": ["credentials"],
+ },
+ ],
+ },
+ }
+
+ _patch_get_workflow_by_id(monkeypatch, fake_get_workflow_by_id)
+
+ definition = {
+ "title": "Updated workflow",
+ "proxy_location": "RESIDENTIAL",
+ "workflow_definition": {
+ "parameters": [],
+ "blocks": [
+ {
+ "label": "new_login",
+ "parameter_keys": ["credentials"],
+ "navigation_goal": "Login",
+ },
+ ],
+ },
+ }
+
+ 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"]
+ login_block = sent_def["workflow_definition"]["blocks"][0]
+ assert login_block.get("parameter_keys", []).count("credentials") == 1