File size: 8,431 Bytes
5753b43 8628c98 5753b43 8628c98 5753b43 8628c98 5753b43 8628c98 5753b43 f1a34f6 5753b43 f1a34f6 8628c98 f1a34f6 8628c98 f1a34f6 8628c98 f1a34f6 5753b43 8628c98 5753b43 a724795 5753b43 985f8d7 5753b43 8628c98 5753b43 8628c98 5753b43 8628c98 5753b43 8628c98 5753b43 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | """
ALLI Workflow Migration Engine (Canonical v3.0.0)
Single Source of Truth workflow migration:
- Upgrades schema_version to '3.0.0'
- Maps legacy node types to canonical types via CORE_TYPE_ALIASES
- Consolidates parameters to node.parameters (SSOT) following priority:
parameters > data.parameters > data.customData > data.* flat fields
- Renames parameter aliases (e.g. value -> literal_value) per node spec
- Drops data.parameters, data.customData, flat parameter copies, node_type, nodeType, schemaVersion
- Strips runtime fields (execution_status, execution_output, error, result, etc.)
- Enforces purity (does not mutate input) and idempotency
- Rejects unsupported future schema versions with ERR-UNSUPPORTED-SCHEMA-VERSION
"""
from copy import deepcopy
from typing import Any, Dict, List, Set
from language_core.core_nodes_generated import (
CORE_TYPE_ALIASES,
CORE_PARAM_ALIASES,
CORE_NODES_SPEC,
CORE_PORT_ALIASES,
)
SUPPORTED_INPUT_VERSIONS = {"1.0.0", "2.0.0", "2.1.0", "3.0.0", 1, "1"}
CANONICAL_SCHEMA_VERSION = "3.0.0"
RUNTIME_FIELDS_TO_STRIP = {
"execution_status", "execution_output", "execution_time", "execution_duration",
"node_status", "error", "result", "last_run", "status", "logs", "stdout", "stderr",
}
UI_FIELDS = {
"label", "name", "color", "collapsed", "notes", "title", "iconType", "sublabel", "borderColor",
"category", "icon", "iconColor", "iconBg", "iconMetaphor", "childFriendlySummary", "categoryNodeId", "description", "children",
}
NON_PARAM_DATA_FIELDS = UI_FIELDS | {
"spec", "verification", "capability_states", "inputs", "outputs", "schema",
"dynamic_inputs", "execution", "docs", "is_staged", "approval_status", "source_module", "hash", "security_warnings",
"parameters", "params", "customData", "values", "node_type", "nodeType",
}
class WorkflowMigrationError(ValueError):
def __init__(self, message: str, code: str = "ERR-UNSUPPORTED-SCHEMA-VERSION"):
super().__init__(f"[{code}] {message}")
self.code = code
def _get_spec_param_keys(canonical_type: str) -> Set[str]:
keys: Set[str] = set()
spec = CORE_NODES_SPEC.get(canonical_type)
if spec:
keys.update(p["id"] for p in spec.get("parameters", []) if isinstance(p, dict) and p.get("id"))
keys.update(inp["id"] for inp in spec.get("inputs", []) if isinstance(inp, dict) and inp.get("id"))
param_map = CORE_PARAM_ALIASES.get(canonical_type, {})
keys.update(param_map.keys())
keys.update(param_map.values())
port_map = CORE_PORT_ALIASES.get(canonical_type, {})
for bucket in ("inputs", "outputs"):
if isinstance(port_map.get(bucket), dict):
keys.update(port_map[bucket].keys())
try:
from language_core.node_schema import default_registry
if default_registry.has(canonical_type):
params = getattr(default_registry.get(canonical_type), "parameters", None) or []
for p in params:
pid = p.id if hasattr(p, "id") else (p.get("id") if isinstance(p, dict) else None)
if pid:
keys.add(pid)
except Exception:
pass
if keys:
keys.add("_input_transforms")
return keys
def _normalize_param_dict(raw: Dict[str, Any], canonical_type: str) -> Dict[str, Any]:
if not isinstance(raw, dict):
return {}
param_aliases = CORE_PARAM_ALIASES.get(canonical_type, {})
normalized: Dict[str, Any] = {}
for k, v in raw.items():
if k in param_aliases:
canon_k = param_aliases[k]
if canon_k == k or canon_k not in raw:
normalized[canon_k] = v
else:
normalized[k] = v
type_norm = {"string": "str", "integer": "int", "boolean": "bool", "number": "float"}
if "const_type" in normalized and isinstance(normalized["const_type"], str):
normalized["const_type"] = type_norm.get(normalized["const_type"].lower(), normalized["const_type"])
if "var_type" in normalized and isinstance(normalized["var_type"], str):
normalized["var_type"] = type_norm.get(normalized["var_type"].lower(), normalized["var_type"])
return normalized
def migrate_workflow(doc: Dict[str, Any]) -> Dict[str, Any]:
"""Pure and idempotent migration of a workflow document to canonical v3.0.0."""
if not isinstance(doc, dict):
raise WorkflowMigrationError("Workflow document must be a dictionary/object.", "ERR-INVALID-WORKFLOW")
raw_version = doc.get("schema_version")
if raw_version is None:
raw_version = doc.get("schemaVersion")
if raw_version is not None and raw_version not in SUPPORTED_INPUT_VERSIONS:
raise WorkflowMigrationError(
f"Unsupported schema version: '{raw_version}'. Supported versions are <= {CANONICAL_SCHEMA_VERSION}",
"ERR-UNSUPPORTED-SCHEMA-VERSION",
)
out = deepcopy(doc)
out["schema_version"] = CANONICAL_SCHEMA_VERSION
out.pop("schemaVersion", None)
raw_nodes = out.get("nodes", [])
if not isinstance(raw_nodes, list):
return out
migrated_nodes: List[Dict[str, Any]] = []
for n in raw_nodes:
if not isinstance(n, dict):
continue
node = deepcopy(n)
for rf in RUNTIME_FIELDS_TO_STRIP:
node.pop(rf, None)
raw_type = node.get("type") or node.get("node_type") or node.get("nodeType") or ""
canonical_type = CORE_TYPE_ALIASES.get(raw_type, raw_type)
node["type"] = canonical_type
node.pop("node_type", None)
node.pop("nodeType", None)
data = node.get("data")
if not isinstance(data, dict):
data = {}
if "type" in data:
dt = str(data["type"])
if dt == raw_type or dt in CORE_TYPE_ALIASES or dt in CORE_NODES_SPEC:
data.pop("type", None)
for rf in RUNTIME_FIELDS_TO_STRIP:
data.pop(rf, None)
valid_spec_keys = _get_spec_param_keys(canonical_type)
if valid_spec_keys:
flat_params = {k: v for k, v in data.items() if k in valid_spec_keys}
else:
flat_params = {k: v for k, v in data.items() if k not in NON_PARAM_DATA_FIELDS and k not in RUNTIME_FIELDS_TO_STRIP}
val_data = data.get("values")
custom_data = data.get("customData")
d_params = data.get("parameters") if data.get("parameters") is not None else data.get("params")
n_params = node.get("parameters") if node.get("parameters") is not None else node.get("params")
merged_params: Dict[str, Any] = {}
for layer in (
flat_params,
deepcopy(val_data) if isinstance(val_data, dict) else {},
deepcopy(custom_data) if isinstance(custom_data, dict) else {},
deepcopy(d_params) if isinstance(d_params, dict) else {},
deepcopy(n_params) if isinstance(n_params, dict) else {},
):
merged_params.update(_normalize_param_dict(layer, canonical_type))
for ui_field in UI_FIELDS:
merged_params.pop(ui_field, None)
node["parameters"] = merged_params
if canonical_type == "core.constant":
raw_lower = str(raw_type).lower()
if "const_type" not in merged_params:
for tokens, mapped in (
(("int", "integer"), "int"),
(("float", "number"), "float"),
(("str", "string", "text"), "str"),
(("bool", "boolean"), "bool"),
(("list",), "list"),
(("dict",), "dict"),
):
if any(tok in raw_lower for tok in tokens):
merged_params["const_type"] = mapped
break
if merged_params.get("const_type") == "bool" and "literal_value" in merged_params:
lv = merged_params["literal_value"]
merged_params["literal_value"] = lv.lower() in ("true", "1", "yes") if isinstance(lv, str) else bool(lv)
for obsolete in ("parameters", "params", "customData", "values", "node_type", "nodeType"):
data.pop(obsolete, None)
node.pop("params", None)
for k in (valid_spec_keys or list(flat_params.keys())):
data.pop(k, None)
node["data"] = data
migrated_nodes.append(node)
out["nodes"] = migrated_nodes
return out
|