neue parser logik

This commit is contained in:
Lars committed 2025-12-23 07:31:33 +01:00
1 parent 2d43e0596c
commit 99482ad65a
3 files changed
+107 -48

No files matched your search

+51 -35
View File
@@ -1,16 +1,9 @@
"""
FILE: app/services/edge_registry.py
DESCRIPTION: Single Source of Truth für Kanten-Typen.
FIX: Regex angepasst auf Format **`canonical`** (Bold + Backticks).
VERSION: 0.6.10 (Regex Precision Update)
"""
import re
import os
import json
import logging
from typing import Dict, Optional, Set
print(">>> MODULE_LOAD: edge_registry.py initialized <<<", flush=True)
import time
from typing import Dict, Optional, Set, Tuple
from app.config import get_settings
@@ -18,6 +11,8 @@ logger = logging.getLogger(__name__)
class EdgeRegistry:
_instance = None
# System-Kanten, die NIEMALS manuell im Markdown stehen dürfen
FORBIDDEN_SYSTEM_EDGES = {"next", "prev", "belongs_to"}
def __new__(cls, *args, **kwargs):
if cls._instance is None:
@@ -43,26 +38,30 @@ class EdgeRegistry:
self.unknown_log_path = "data/logs/unknown_edges.jsonl"
self.canonical_map: Dict[str, str] = {}
self.valid_types: Set[str] = set()
self._last_mtime = 0.0 # Für dynamisches Neuladen
self._load_vocabulary()
self.ensure_latest()
self.initialized = True
def _load_vocabulary(self):
"""Parst die Markdown-Tabelle im Vault."""
print(f">>> CHECK: Loading Vocabulary from {self.full_vocab_path}", flush=True)
def ensure_latest(self):
"""Prüft den Zeitstempel der Datei und lädt ggf. neu."""
if not os.path.exists(self.full_vocab_path):
print(f"!!! [DICT-ERROR] File not found: {self.full_vocab_path} !!!", flush=True)
return
# WP-22 Precision Regex:
# Sucht nach | **`typ`** | oder | **typ** |
# Die Backticks `? sind jetzt optional enthalten.
current_mtime = os.path.getmtime(self.full_vocab_path)
if current_mtime > self._last_mtime:
self._load_vocabulary()
self._last_mtime = current_mtime
def _load_vocabulary(self):
"""Parst das Wörterbuch und baut die Canonical-Map auf."""
self.canonical_map.clear()
self.valid_types.clear()
pattern = re.compile(r"\|\s*\*\*`?([a-zA-Z0-9_-]+)`?\*\*\s*\|\s*([^|]+)\|")
try:
with open(self.full_vocab_path, "r", encoding="utf-8") as f:
c_types, c_aliases = 0, 0
for line in f:
match = pattern.search(line)
if match:
@@ -71,39 +70,56 @@ class EdgeRegistry:
self.valid_types.add(canonical)
self.canonical_map[canonical] = canonical
c_types += 1
if aliases_str and "Kein Alias" not in aliases_str:
# Aliase säubern (entfernt Backticks auch hier)
aliases = [a.strip() for a in aliases_str.split(",") if a.strip()]
for alias in aliases:
clean_alias = alias.replace("`", "").lower().strip().replace(" ", "_")
self.canonical_map[clean_alias] = canonical
c_aliases += 1
if c_types == 0:
print("!!! [DICT-WARN] Pattern mismatch! Ensure types are **`canonical`** or **canonical**. !!!", flush=True)
else:
print(f"=== [DICT-SUCCESS] Registered {c_types} Canonical Types and {c_aliases} Aliases ===", flush=True)
logger.info(f"EdgeRegistry reloaded: {len(self.valid_types)} types.")
except Exception as e:
print(f"!!! [DICT-FATAL] Error reading file: {e} !!!", flush=True)
logger.error(f"Error reading vocabulary: {e}")
def resolve(self, edge_type: str) -> str:
"""Normalisiert Kanten-Typen via Registry oder loggt Unbekannte."""
def resolve(self, edge_type: str, provenance: str = "explicit", context: dict = None) -> str:
"""
Validiert Kanten gegen System-Regeln und Wörterbuch.
- provenance: 'explicit' (User/Markdown) oder 'structure' (System-Pipeline)
- context: {'file': str, 'line': int}
"""
self.ensure_latest()
if not edge_type: return "related_to"
clean_type = edge_type.lower().strip().replace(" ", "_").replace("-", "_")
clean_type = edge_type.lower().strip().replace(" ", "_").replace("-", "_")
ctx = context or {}
# 1. Check auf "verbotene" manuelle Nutzung von Systemkanten
if provenance == "explicit" and clean_type in self.FORBIDDEN_SYSTEM_EDGES:
self._log_issue(clean_type, "forbidden_system_usage", ctx)
return "related_to" # Fallback, um System-Integrität zu schützen
# 2. Stillschweigende Akzeptanz für echte System-Struktur
if provenance == "structure" and clean_type in self.FORBIDDEN_SYSTEM_EDGES:
return clean_type
# 3. Mapping gegen das Wörterbuch
if clean_type in self.canonical_map:
return self.canonical_map[clean_type]
self._log_unknown(clean_type)
# 4. Unbekannte Kante loggen
self._log_issue(clean_type, "unknown_type", ctx)
return clean_type
def _log_unknown(self, edge_type: str):
def _log_issue(self, edge_type: str, error_kind: str, ctx: dict):
"""Schreibt detaillierte Fehler mit Fundort."""
try:
os.makedirs(os.path.dirname(self.unknown_log_path), exist_ok=True)
entry = {"unknown_type": edge_type, "status": "new"}
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")
}
with open(self.unknown_log_path, "a", encoding="utf-8") as f:
f.write(json.dumps(entry) + "\n")
except Exception: pass