feat(archivar): Stufe 4 Red Flag Detection + Bernd Activation

Routing-Pipeline Stufe 4: Wenn der LLM-Router unsicher ist (STAY bei
>1 Raum), verloren ist (Timeout/None) ODER ein neues Thema erkennt
(unbekannter Raum-Name), setzt der Archivar eine Red Flag und aktiviert
den Hauptagenten als Entscheidungsträger (STAY/ROUTE).

Änderungen:
- router.py: _llm_room_lookup liefert 5-Tupel (room, stay, failed,
  reason, suggestion). reason ∈ {route, stay, unavailable, new_topic}.
  Neues 'new_topic'-Signal: LLM nannte einen Raum der nicht in der
  Raum-Map steht → Red Flag mit red_flag_suggestion.
- router.py: asyncio.wait_for(LLM_ROOM_LOOKUP_TIMEOUT) um den LLM-Call
  (Rainer-Niagara-Befund #3: war ohne wait_for → Event-Loop-Blockade).
- router.py: RoutingDecision + red_flag_reason / red_flag_suggestion;
  _build_red_flag_decision(reason, suggestion) injiziert den Grund als
  Hinweis in den Red-Flag-Prompt.
- config.py: LLM_ROOM_LOOKUP_TIMEOUT (Default 15s, env-schaltbar).
- adapter.py: RED_FLAG_TIMEOUT tatsächlich anwenden (Rainer-Niagara
  Befund #4: war toter Code) via _RED_FLAG_SET_TIME ContextVar;
  Timeout → STAY-Fallback (red_flag_timeout_fallback).
- multiroom_context.py: CrossRoomEntry.source-Feld ('manual'/'move'/
  'red_flag'); add(source=...) → Ring-Feedback mit Quellen-Tag für
  Stufe-5-Lern-Semantik (Rainer FIX P2).
- adapter.py: Ring-Feedback source='red_flag'; Stufe-4-Feedback-Log
  (reason + suggestion) für spätere Lern-Semantik (nur loggen, nicht
  lernend — Andreas-Entscheidung offen).

Pitfalls adressiert: P-084 (neues Thema, kein toter Code mehr),
P-102 (Clarify-Responses greifen weiterhin NICHT — unverändert vor
classify_and_route).

Tests: test_stufe4_red_flag.py (13 Tests: new_topic, reason je Trigger,
Ring-source, Config-Timeout). 1643 Tests gesamt grün, keine Regression
in Stufe 1-3.
This commit is contained in:
Bernd (Hermes)
2026-08-23 18:29:46 +02:00
parent 5170180252
commit 059673b62d
5 changed files with 597 additions and 60 deletions
+10
View File
@@ -44,6 +44,11 @@ class CrossRoomEntry:
timestamp: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
"""Zeitstempel (UTC)."""
source: str = "manual"
"""Quelle des Eintrags: 'manual' (normaler Beitrag), 'move' (Auto-Route/
Button-Bestätigung), 'red_flag' (Agent-Entscheidung bei Red Flag).
Dient Stufe 5 (Lern-Semantik) als Filterkriterium im Ring-Summary."""
def to_summary_line(self) -> str:
"""Formatiere als eine Zeile für den System-Prompt."""
return f" {self.room_name}: {self.topic}"
@@ -74,6 +79,7 @@ class MultiRoomContextRing:
room_name: str,
topic: str,
timestamp: Optional[datetime] = None,
source: str = "manual",
) -> None:
"""Füge einen Themenbeitrag zum Ring hinzu.
@@ -85,6 +91,8 @@ class MultiRoomContextRing:
room_name: Menschenlesbarer Raum-Name.
topic: Thema / Zusammenfassung.
timestamp: Optionaler Zeitstempel (default: jetzt UTC).
source: Quellen-Tag ('manual' | 'move' | 'red_flag') für
Stufe-5-Lern-Semantik.
"""
if not room_id or not topic:
return
@@ -94,6 +102,7 @@ class MultiRoomContextRing:
room_name=room_name,
topic=topic.strip(),
timestamp=timestamp or datetime.now(timezone.utc),
source=source,
)
async with self._lock:
@@ -134,6 +143,7 @@ class MultiRoomContextRing:
"room_name": e.room_name,
"topic": e.topic,
"timestamp": e.timestamp.isoformat(),
"source": e.source,
}
for e in self._entries
]