641b066db0
FEATURE 1: Routing-Telemetrie (Konfidenz-Logging) - Neue Datei: archivar/routing_telemetry.py — RoutingTelemetry Klasse - Feature-Flag: ARCHIVAR_ROUTING_TELEMETRY env var (Default true) - Pro Routing-Entscheidung: stage, score, decision, target, origin, duration_ms - Format: [TELEMETRY] stage=keyword_100 score=1.00 decision=route ... - NIEMALS Nachrichteninhalte im Log (Datenschutz) - Telemetrie-Punkte in router.py an allen 5 Stufen: keyword_100, context_keyword, faiss, llm, default - Flush in adapter.py nach classify_and_route() FEATURE 2: Echte Integration-Tests - Neue Datei: archivar/tests/test_v8_3_3.py (18 Tests) - Echte RoutingDecision-Objekte statt Dict-Simulationen - 7 Integration-Test-Klassen: 1. 100%-Keyword im falschen Raum → route 2. 100%-Keyword im richtigen Raum → stay (self-match) 3. Kontext-Keyword → butler 4. FAISS hohe Ähnlichkeit → route 5. FAISS mittlere Ähnlichkeit → butler 6. LLM → STAY 7. LLM → ROUTE - Telemetrie-Tests: Logging, Datenschutz, Feature-Flag - Erweiterte conftest.py: multi_room_map, Mock-LLM Fixtures Verifikation: 527 passed, 1 pre-existing fail, 2 skipped (FAISS)
126 lines
3.8 KiB
Python
126 lines
3.8 KiB
Python
"""
|
|
Routing-Telemetrie für Archivar v8.3.3.
|
|
|
|
Sammelt pro Routing-Entscheidung Metadaten (Stufe, Score, Decision, Target,
|
|
Origin, Dauer) und flusht sie als human-readable Log-Zeile an logger.info.
|
|
|
|
NIEMALS Nachrichteninhalte loggen — nur Metadaten (Datenschutz!).
|
|
|
|
Feature-Flag: ARCHIVAR_ROUTING_TELEMETRY env var, Default AN.
|
|
|
|
Usage::
|
|
|
|
from .routing_telemetry import routing_telemetry
|
|
|
|
routing_telemetry.record(
|
|
stage='keyword_100', score=1.0, decision='route',
|
|
target='room-abc', origin='room-xyz', duration_ms=2.3,
|
|
)
|
|
routing_telemetry.flush()
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import os
|
|
import time
|
|
from typing import Dict, List, Optional
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class RoutingTelemetry:
|
|
"""Sammelt Routing-Telemetrie-Daten und flusht sie als Log-Zeilen.
|
|
|
|
Thread-safe via GIL (single-writer in async context). Keine Lock nötig,
|
|
da alle Aufrufe im selben asyncio-Event-Loop erfolgen.
|
|
"""
|
|
|
|
def __init__(self) -> None:
|
|
self._enabled: bool = self._read_feature_flag()
|
|
self._records: List[Dict[str, object]] = []
|
|
self._start_time: float = 0.0
|
|
|
|
@staticmethod
|
|
def _read_feature_flag() -> bool:
|
|
"""Lies ARCHIVAR_ROUTING_TELEMETRY env var. Default: True (AN)."""
|
|
val = os.getenv("ARCHIVAR_ROUTING_TELEMETRY", "true").strip().lower()
|
|
return val not in ("false", "0", "no", "off")
|
|
|
|
@property
|
|
def enabled(self) -> bool:
|
|
return self._enabled
|
|
|
|
def start_timer(self) -> None:
|
|
"""Starte den Stage-Timer. Wird VOR jeder Stufe aufgerufen."""
|
|
self._start_time = time.monotonic()
|
|
|
|
def elapsed_ms(self) -> float:
|
|
"""Liefere die seit start_timer() vergangene Zeit in ms."""
|
|
return (time.monotonic() - self._start_time) * 1000.0
|
|
|
|
def record(
|
|
self,
|
|
stage: str,
|
|
score: float,
|
|
decision: str,
|
|
target: str,
|
|
origin: str,
|
|
duration_ms: float,
|
|
) -> None:
|
|
"""Zeichne eine Routing-Entscheidung auf.
|
|
|
|
Args:
|
|
stage: 'keyword_100' | 'context_keyword' | 'faiss' | 'llm' | 'default'
|
|
score: FAISS-Score, LLM-Confidence, 1.0 bei 100%-Match, 0.0 bei Default
|
|
decision: 'route' | 'stay' | 'butler'
|
|
target: Zielraum-ID (oder '' bei stay)
|
|
origin: Ursprungsraum-ID
|
|
duration_ms: Dauer der Stufe in Millisekunden
|
|
"""
|
|
if not self._enabled:
|
|
return
|
|
self._records.append({
|
|
"stage": stage,
|
|
"score": score,
|
|
"decision": decision,
|
|
"target": target,
|
|
"origin": origin,
|
|
"duration_ms": duration_ms,
|
|
})
|
|
|
|
def flush(self) -> None:
|
|
"""Flush alle gesammelten Records als [TELEMETRY] Log-Zeilen.
|
|
|
|
Format:
|
|
[TELEMETRY] stage=keyword_100 score=1.00 decision=route
|
|
target=#RC-Plugin origin=#LAN-Doku duration=2ms
|
|
"""
|
|
if not self._enabled or not self._records:
|
|
return
|
|
|
|
for rec in self._records:
|
|
stage = rec["stage"]
|
|
score = rec["score"]
|
|
decision = rec["decision"]
|
|
target = rec["target"] or "(none)"
|
|
origin = rec["origin"] or "(none)"
|
|
dur = rec["duration_ms"]
|
|
|
|
# Human-readable, kein JSON — für Log-Analyse optimiert
|
|
logger.info(
|
|
"[TELEMETRY] stage=%s score=%.2f decision=%s "
|
|
"target=%s origin=%s duration=%.1fms",
|
|
stage, score, decision, target, origin, dur,
|
|
)
|
|
|
|
self._records.clear()
|
|
|
|
def clear(self) -> None:
|
|
"""Leere alle Records ohne zu loggen (für Tests)."""
|
|
self._records.clear()
|
|
|
|
|
|
# Modul-Level-Singleton — wird von router.py und adapter.py importiert
|
|
routing_telemetry = RoutingTelemetry()
|