refactoring for canon conformity

This commit is contained in:
2026-05-23 14:00:59 +02:00
parent 0193c97094
commit 653411ffb8
16 changed files with 819 additions and 29 deletions

View File

@@ -9,6 +9,7 @@ from datetime import datetime, timezone
from pathlib import Path
from typing import Any
from .canon import edge_canon_mapping, node_canon_mapping
from .loader import repo_root
from .schema_validation import draft202012_validator
@@ -416,13 +417,7 @@ class RegistryStore:
nodes[str(node.get("id", ""))] = node
for edge in graph.get("edges", []):
if isinstance(edge, dict):
edges.append(
{
"from": str(edge.get("from", "")),
"to": str(edge.get("to", "")),
"type": str(edge.get("type", "")),
}
)
edges.append(_edge_with_canon_metadata(edge))
return {
"apiVersion": "railiance.fabric/v1alpha1",
"kind": "FabricGraphExport",
@@ -1485,7 +1480,18 @@ def _project_discovery_snapshot(
target = key_to_graph_id.get(str(candidate.get("target_key") or ""))
if not source or not target:
continue
edge = {"from": source, "to": target, "type": str(candidate.get("edge_type") or "")}
edge = _edge_with_canon_metadata(
{
"from": source,
"to": target,
"type": str(candidate.get("edge_type") or ""),
"canonical_type": candidate.get("canonical_type", ""),
"canon_anchor": candidate.get("canon_anchor", ""),
"mapping_fit": candidate.get("mapping_fit", ""),
"display_only": candidate.get("display_only", False),
"evidence_state": candidate.get("evidence_state", ""),
}
)
edge_key = _edge_key(edge)
if edge["type"] and edge_key not in existing_edges:
existing_edges.add(edge_key)
@@ -1496,6 +1502,7 @@ def _project_discovery_snapshot(
def _project_candidate_node(candidate: dict[str, Any], graph_id: str) -> dict[str, Any]:
attributes = candidate.get("attributes") if isinstance(candidate.get("attributes"), dict) else {}
canon_mapping = node_canon_mapping(str(candidate.get("kind") or "DiscoveredEntity"))
return {
"id": graph_id,
"kind": str(candidate.get("kind") or "DiscoveredEntity"),
@@ -1503,6 +1510,10 @@ def _project_candidate_node(candidate: dict[str, Any], graph_id: str) -> dict[st
"repo": str(candidate.get("repo") or ""),
"domain": str(candidate.get("domain") or ""),
"lifecycle": str(candidate.get("lifecycle") or "active"),
"canon_category": str(candidate.get("canon_category") or canon_mapping.category),
"canon_anchor": str(candidate.get("canon_anchor") or canon_mapping.canon_anchor),
"mapping_fit": str(candidate.get("mapping_fit") or canon_mapping.fit),
"evidence_state": str(candidate.get("evidence_state") or "declared"),
"attributes": {
**attributes,
"discovery_stable_key": candidate.get("stable_key"),
@@ -1604,6 +1615,22 @@ def _edge_key(edge: dict[str, Any]) -> tuple[str, str, str]:
return (str(edge.get("from", "")), str(edge.get("to", "")), str(edge.get("type", "")))
def _edge_with_canon_metadata(edge: dict[str, Any]) -> dict[str, Any]:
edge_type = str(edge.get("type") or "")
canon_mapping = edge_canon_mapping(edge_type)
return {
"from": str(edge.get("from", "")),
"to": str(edge.get("to", "")),
"type": edge_type,
"canonical_type": str(edge.get("canonical_type") or canon_mapping.canonical_type),
"canon_anchor": str(edge.get("canon_anchor") or canon_mapping.canon_anchor),
"mapping_fit": str(edge.get("mapping_fit") or canon_mapping.fit),
"display_only": bool(edge.get("display_only", canon_mapping.display_only)),
"evidence_state": str(edge.get("evidence_state") or "declared"),
"attributes": edge.get("attributes", {}) if isinstance(edge.get("attributes"), dict) else {},
}
def _stable_json(value: Any) -> str:
return json.dumps(value, sort_keys=True, separators=(",", ":"))