""" 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