mindnet/app/services/edge_registry.py

165 lines
6.8 KiB
Python
Raw Normal View History

2025-12-18 11:50:52 +01:00
"""
FILE: app/services/edge_registry.py
2025-12-23 16:05:40 +01:00
DESCRIPTION: Single Source of Truth für Kanten-Typen mit dynamischem Reload.
2025-12-26 21:52:08 +01:00
WP-15b: Erweiterte Provenance-Prüfung für die Candidate-Validation.
Sichert die Graph-Integrität durch strikte Trennung von System- und Inhaltskanten.
2025-12-23 18:11:05 +01:00
WP-22: Fix für absolute Pfade außerhalb des Vaults (Prod-Dictionary).
2025-12-23 18:51:12 +01:00
WP-20: Synchronisation mit zentralen Settings (v0.6.2).
2025-12-26 21:52:08 +01:00
VERSION: 0.8.0
2025-12-23 18:51:12 +01:00
STATUS: Active
DEPENDENCIES: re, os, json, logging, time, app.config
2025-12-26 21:52:08 +01:00
LAST_ANALYSIS: 2025-12-26
2025-12-18 11:50:52 +01:00
"""
import re
import os
import json
import logging
2025-12-23 16:05:40 +01:00
import time
from typing import Dict, Optional, Set, Tuple
2025-12-18 18:50:45 +01:00
2025-12-18 13:56:45 +01:00
from app.config import get_settings
2025-12-18 11:50:52 +01:00
logger = logging.getLogger(__name__)
class EdgeRegistry:
2025-12-26 21:52:08 +01:00
"""
Zentraler Verwalter für das Kanten-Vokabular.
Implementiert das Singleton-Pattern für konsistente Validierung über alle Services.
"""
2025-12-18 11:50:52 +01:00
_instance = None
2025-12-26 21:52:08 +01:00
# System-Kanten, die nicht durch User oder KI gesetzt werden dürfen
2025-12-23 16:05:40 +01:00
FORBIDDEN_SYSTEM_EDGES = {"next", "prev", "belongs_to"}
2025-12-18 11:50:52 +01:00
2025-12-18 18:40:46 +01:00
def __new__(cls, *args, **kwargs):
2025-12-18 11:50:52 +01:00
if cls._instance is None:
cls._instance = super(EdgeRegistry, cls).__new__(cls)
cls._instance.initialized = False
return cls._instance
2025-12-23 18:51:12 +01:00
def __init__(self):
2025-12-18 12:17:10 +01:00
if self.initialized:
return
2025-12-18 18:40:46 +01:00
settings = get_settings()
2025-12-23 18:11:05 +01:00
2025-12-23 18:51:12 +01:00
# 1. Pfad aus den zentralen Settings laden (WP-20 Synchronisation)
# Priorisiert den Pfad aus der .env / config.py (v0.6.2)
self.full_vocab_path = os.path.abspath(settings.MINDNET_VOCAB_PATH)
2025-12-18 18:37:23 +01:00
self.unknown_log_path = "data/logs/unknown_edges.jsonl"
2025-12-18 12:17:10 +01:00
self.canonical_map: Dict[str, str] = {}
2025-12-18 11:50:52 +01:00
self.valid_types: Set[str] = set()
2025-12-23 16:05:40 +01:00
self._last_mtime = 0.0
2025-12-18 12:17:10 +01:00
2025-12-23 18:51:12 +01:00
# Initialer Ladevorgang
logger.info(f">>> [EDGE-REGISTRY] Initializing with Path: {self.full_vocab_path}")
2025-12-23 16:05:40 +01:00
self.ensure_latest()
2025-12-18 11:50:52 +01:00
self.initialized = True
2025-12-23 16:05:40 +01:00
def ensure_latest(self):
2025-12-23 18:51:12 +01:00
"""
Prüft den Zeitstempel der Vokabular-Datei und lädt bei Bedarf neu.
2025-12-26 21:52:08 +01:00
Verhindert Inkonsistenzen bei Laufzeit-Updates des Dictionaries.
2025-12-23 18:51:12 +01:00
"""
2025-12-18 18:37:23 +01:00
if not os.path.exists(self.full_vocab_path):
2025-12-23 18:51:12 +01:00
logger.error(f"!!! [EDGE-REGISTRY ERROR] File not found: {self.full_vocab_path} !!!")
2025-12-18 11:50:52 +01:00
return
2025-12-23 18:51:12 +01:00
try:
current_mtime = os.path.getmtime(self.full_vocab_path)
if current_mtime > self._last_mtime:
self._load_vocabulary()
self._last_mtime = current_mtime
except Exception as e:
logger.error(f"!!! [EDGE-REGISTRY] Error checking file time: {e}")
2025-12-23 16:05:40 +01:00
def _load_vocabulary(self):
2025-12-26 21:52:08 +01:00
"""
Parst das Markdown-Wörterbuch und baut die Canonical-Map auf.
Erkennt Tabellen-Strukturen und extrahiert fettgedruckte System-Typen.
"""
2025-12-23 16:05:40 +01:00
self.canonical_map.clear()
self.valid_types.clear()
2025-12-23 18:51:12 +01:00
# Regex für Tabellen-Struktur: | **Typ** | Aliase |
2025-12-18 18:53:01 +01:00
pattern = re.compile(r"\|\s*\*\*`?([a-zA-Z0-9_-]+)`?\*\*\s*\|\s*([^|]+)\|")
2025-12-18 11:50:52 +01:00
try:
2025-12-18 18:37:23 +01:00
with open(self.full_vocab_path, "r", encoding="utf-8") as f:
2025-12-18 18:50:45 +01:00
c_types, c_aliases = 0, 0
2025-12-18 11:50:52 +01:00
for line in f:
match = pattern.search(line)
if match:
2025-12-18 18:53:01 +01:00
canonical = match.group(1).strip().lower()
2025-12-18 11:50:52 +01:00
aliases_str = match.group(2).strip()
self.valid_types.add(canonical)
2025-12-18 12:17:10 +01:00
self.canonical_map[canonical] = canonical
2025-12-18 18:50:45 +01:00
c_types += 1
2025-12-18 11:50:52 +01:00
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:
2025-12-23 18:51:12 +01:00
# Normalisierung: Kleinschreibung, Underscores statt Leerzeichen
2025-12-18 18:53:01 +01:00
clean_alias = alias.replace("`", "").lower().strip().replace(" ", "_")
2025-12-18 11:50:52 +01:00
self.canonical_map[clean_alias] = canonical
2025-12-18 18:50:45 +01:00
c_aliases += 1
2025-12-18 11:50:52 +01:00
2025-12-23 18:51:12 +01:00
logger.info(f"=== [EDGE-REGISTRY SUCCESS] Loaded {c_types} Canonical Types and {c_aliases} Aliases ===")
2025-12-18 11:50:52 +01:00
except Exception as e:
2025-12-23 18:51:12 +01:00
logger.error(f"!!! [EDGE-REGISTRY FATAL] Error reading file: {e} !!!")
2025-12-18 11:50:52 +01:00
2025-12-23 16:05:40 +01:00
def resolve(self, edge_type: str, provenance: str = "explicit", context: dict = None) -> str:
2025-12-23 18:51:12 +01:00
"""
2025-12-26 21:52:08 +01:00
WP-15b: Validiert einen Kanten-Typ gegen das Vokabular und prüft Berechtigungen.
Sichert, dass nur strukturelle Prozesse System-Kanten setzen dürfen.
2025-12-23 18:51:12 +01:00
"""
2025-12-23 16:05:40 +01:00
self.ensure_latest()
2025-12-23 18:51:12 +01:00
if not edge_type:
return "related_to"
2025-12-18 18:53:01 +01:00
2025-12-23 18:51:12 +01:00
# Normalisierung des Typs
2025-12-23 16:05:40 +01:00
clean_type = edge_type.lower().strip().replace(" ", "_").replace("-", "_")
ctx = context or {}
2025-12-26 21:52:08 +01:00
# WP-15b: System-Kanten dürfen weder manuell noch durch KI/Vererbung gesetzt werden.
# Nur Provenienz 'structure' (interne Prozesse) ist autorisiert.
# Wir blockieren hier alle Provenienzen außer 'structure'.
restricted_provenance = ["explicit", "semantic_ai", "inherited", "global_pool", "rule"]
if provenance in restricted_provenance and clean_type in self.FORBIDDEN_SYSTEM_EDGES:
self._log_issue(clean_type, f"forbidden_usage_by_{provenance}", ctx)
2025-12-23 16:05:40 +01:00
return "related_to"
2025-12-26 21:52:08 +01:00
# System-Kanten sind NUR bei struktureller Provenienz erlaubt
2025-12-23 16:05:40 +01:00
if provenance == "structure" and clean_type in self.FORBIDDEN_SYSTEM_EDGES:
return clean_type
2025-12-26 21:52:08 +01:00
# Mapping auf kanonischen Namen (Alias-Auflösung)
2025-12-18 11:50:52 +01:00
if clean_type in self.canonical_map:
return self.canonical_map[clean_type]
2025-12-18 18:53:01 +01:00
2025-12-26 21:52:08 +01:00
# Fallback und Logging unbekannter Typen für Admin-Review
2025-12-23 16:05:40 +01:00
self._log_issue(clean_type, "unknown_type", ctx)
2025-12-18 12:17:10 +01:00
return clean_type
2025-12-18 11:50:52 +01:00
2025-12-23 16:05:40 +01:00
def _log_issue(self, edge_type: str, error_kind: str, ctx: dict):
2025-12-23 18:51:12 +01:00
"""Detailliertes JSONL-Logging für die Vokabular-Optimierung."""
2025-12-18 11:50:52 +01:00
try:
os.makedirs(os.path.dirname(self.unknown_log_path), exist_ok=True)
2025-12-23 16:05:40 +01:00
entry = {
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
"edge_type": edge_type,
"error": error_kind,
"file": ctx.get("file", "unknown"),
"line": ctx.get("line", "unknown"),
2025-12-26 21:52:08 +01:00
"note_id": ctx.get("note_id", "unknown"),
"provenance": ctx.get("provenance", "unknown")
2025-12-23 16:05:40 +01:00
}
2025-12-18 11:50:52 +01:00
with open(self.unknown_log_path, "a", encoding="utf-8") as f:
f.write(json.dumps(entry) + "\n")
2025-12-23 18:51:12 +01:00
except Exception:
pass
2025-12-18 11:50:52 +01:00
2025-12-26 21:52:08 +01:00
# Singleton Export für systemweiten Zugriff
2025-12-18 11:50:52 +01:00
registry = EdgeRegistry()