This commit is contained in:
Lars committed 2025-12-18 13:15:58 +01:00
1 parent 9a18f3cc8b
commit 3eac646cb6
4 files changed
+131 -226

No files matched your search

+8 -12
View File
@@ -2,7 +2,7 @@
FILE: app/services/edge_registry.py
DESCRIPTION: Single Source of Truth für Kanten-Typen. Parst '01_User_Manual/01_edge_vocabulary.md'.
WP-22 Teil B: Registry & Validation.
FIX: Beachtet MINDNET_VAULT_ROOT aus .env korrekt.
Beachtet den dynamischen Vault-Root aus ENV oder Parameter.
"""
import re
import os
@@ -25,15 +25,11 @@ class EdgeRegistry:
if self.initialized:
return
# Priorität 1: Übergebener Parameter (z.B. für Tests)
# Priorität 2: Environment Variable (z.B. Production ./vault_master)
# Priorität 3: Default Fallback (./vault)
# Priorität: 1. Parameter -> 2. ENV -> 3. Default
self.vault_root = vault_root or os.getenv("MINDNET_VAULT_ROOT", "./vault")
# Der relative Pfad ist laut Spezifikation fest definiert
self.vocab_rel_path = os.path.join("01_User_Manual", "01_edge_vocabulary.md")
self.unknown_log_path = "data/logs/unknown_edges.jsonl"
self.canonical_map: Dict[str, str] = {}
self.valid_types: Set[str] = set()
@@ -42,15 +38,13 @@ class EdgeRegistry:
def _load_vocabulary(self):
"""Parst die Markdown-Tabelle im Vault."""
# Absoluten Pfad auflösen, um Verwirrung mit cwd zu vermeiden
full_path = os.path.abspath(os.path.join(self.vault_root, self.vocab_rel_path))
if not os.path.exists(full_path):
# Wir loggen den vollen Pfad, damit Debugging einfacher ist
logger.warning(f"Edge Vocabulary NOT found at: {full_path}. Registry is empty.")
return
# Regex: | **canonical** | alias, alias |
# Regex für Markdown Tabellen: | **canonical** | Aliases | ...
pattern = re.compile(r"\|\s*\*\*([a-z_]+)\*\*\s*\|\s*([^|]+)\|")
try:
@@ -67,7 +61,7 @@ class EdgeRegistry:
if aliases_str and "Kein Alias" not in aliases_str:
aliases = [a.strip() for a in aliases_str.split(",") if a.strip()]
for alias in aliases:
clean_alias = alias.replace("`", "")
clean_alias = alias.replace("`", "").lower().strip()
self.canonical_map[clean_alias] = canonical
logger.info(f"EdgeRegistry loaded from {full_path}: {len(self.valid_types)} types.")
@@ -76,6 +70,7 @@ class EdgeRegistry:
logger.error(f"Failed to parse Edge Vocabulary at {full_path}: {e}")
def resolve(self, edge_type: str) -> str:
"""Normalisiert Kanten-Typen via Registry oder loggt Unbekannte."""
if not edge_type: return "related_to"
clean_type = edge_type.lower().strip().replace(" ", "_")
@@ -86,6 +81,7 @@ class EdgeRegistry:
return clean_type
def _log_unknown(self, edge_type: str):
"""Schreibt unbekannte Typen für Review in ein Log."""
try:
os.makedirs(os.path.dirname(self.unknown_log_path), exist_ok=True)
entry = {"unknown_type": edge_type, "status": "new"}
@@ -94,5 +90,5 @@ class EdgeRegistry:
except Exception:
pass
# Default Instanz
# Singleton Instanz
registry = EdgeRegistry()