fix(archivar): Timeout-Fallback auf Ursprungsraum + Keyword-Matching vor LLM
Drei Routing-Bug-Fixes basierend auf Analyse des Timeout-Falls vom 15.06.2026: FIX 1 (KRITISCH): Timeout-Fallback auf Ursprungsraum statt Catch-All - _llm_room_lookup() gibt jetzt (RoomEntry|None, bool) zurück - bool=True signalisiert LLM-Aufruf-Fehler (Exception/Timeout) - Bei LLM-Fehler: stay_in_room=True, is_catch_all=False - Nur bei explizitem CATCH_ALL vom LLM: catch_all-Raum als Ziel FIX 2: Keyword-Matching vor LLM-Aufruf (Embedding+Keyword-Fallback) - Nach Embedding-Vorfilter: wenn work_order mit conf < 0.9 - find_room_by_keywords() aufrufen, bei Match direkt zurückgeben - Verhindert unnötige LLM-Calls für klare Keyword-Matches FIX 3: find_room_by_keywords() verbessert - keywords_100-Matches: nicht erster Treffer, sondern Raum mit den MEISTEN 100%-Matches gewinnt (best_100_score) - keywords_context bleibt Fallback wenn kein 100%-Match existiert Tests: 57/58 passed (1 pre-existing env-var failure)
This commit is contained in:
+61
-21
@@ -297,10 +297,33 @@ class BidirectionalRouter:
|
||||
stay_in_room=True,
|
||||
)
|
||||
|
||||
# ── Schritt 4.5: Keyword-Matching vor LLM (FIX 2) ────────────
|
||||
# Wenn Embedding 'work_order' mit niedriger Konfidenz (< 0.9):
|
||||
# versuche Keyword-Match bevor wir das LLM aufrufen.
|
||||
# Verhindert unnötige LLM-Calls für Nachrichten mit klaren Keywords.
|
||||
if result is not None and result.category == "work_order" and result.confidence < 0.9:
|
||||
matched_room = self._room_map.find_room_by_keywords(text, origin_room_id=origin_room_id)
|
||||
if matched_room is not None:
|
||||
logger.info(
|
||||
"Keyword-Match vor LLM: '%s' → %s (embedding conf=%.3f)",
|
||||
text[:80], matched_room.name, result.confidence,
|
||||
)
|
||||
room_name = matched_room.name.replace("#", "") if matched_room.name.startswith("#") else matched_room.name
|
||||
notification = f"📋 Bearbeite ich in #{room_name} — [Link]"
|
||||
matched_keywords = self._extract_matched_keywords(text, matched_room)
|
||||
return RoutingDecision(
|
||||
category="work_order",
|
||||
target_room=matched_room.room_id,
|
||||
confidence=0.95,
|
||||
matched_room_name=matched_room.name,
|
||||
source_room_notification=notification,
|
||||
matched_keywords=matched_keywords,
|
||||
)
|
||||
|
||||
# ── Schritt 5: LLM-Raum-Router (zentraler Prompt) ────────────
|
||||
# LLM_ROOM_PROMPT erhält Nachricht + Raumliste mit Keywords,
|
||||
# liefert room_id direkt (keine Kategorien, kein Name-Matching)
|
||||
llm_room = self._llm_room_lookup(text)
|
||||
llm_room, llm_failed = self._llm_room_lookup(text)
|
||||
if llm_room is not None:
|
||||
room_name = llm_room.name.replace("#", "") if llm_room.name.startswith("#") else llm_room.name
|
||||
logger.info(
|
||||
@@ -321,18 +344,33 @@ class BidirectionalRouter:
|
||||
move_request_target_id=llm_room.room_id,
|
||||
)
|
||||
|
||||
# ── Schritt 7: Kein Treffer → catch_all ──────────────────────
|
||||
logger.info(
|
||||
"LLM-Raum-Router: kein Treffer für '%.80s...' → catch_all", text
|
||||
)
|
||||
catch_all = self._room_map.catch_all_room
|
||||
return RoutingDecision(
|
||||
category="direct_question",
|
||||
target_room=catch_all.room_id if catch_all else "",
|
||||
confidence=0.0,
|
||||
is_catch_all=True,
|
||||
stay_in_room=True,
|
||||
)
|
||||
# ── Schritt 7: LLM-Fehler oder kein Treffer ──────────────────
|
||||
if llm_failed:
|
||||
# FIX 1: Timeout/Exception → Fallback auf Ursprungsraum, NICHT Catch-All
|
||||
logger.warning(
|
||||
"LLM-Raum-Router: Aufruf fehlgeschlagen für '%.80s...' → stay_in_room (Ursprungsraum)",
|
||||
text,
|
||||
)
|
||||
return RoutingDecision(
|
||||
category="direct_question",
|
||||
target_room="",
|
||||
confidence=0.0,
|
||||
stay_in_room=True,
|
||||
is_catch_all=False,
|
||||
)
|
||||
else:
|
||||
# LLM erfolgreich, aber kein Match → catch_all
|
||||
logger.info(
|
||||
"LLM-Raum-Router: kein Treffer für '%.80s...' → catch_all", text
|
||||
)
|
||||
catch_all = self._room_map.catch_all_room
|
||||
return RoutingDecision(
|
||||
category="direct_question",
|
||||
target_room=catch_all.room_id if catch_all else "",
|
||||
confidence=0.0,
|
||||
is_catch_all=True,
|
||||
stay_in_room=True,
|
||||
)
|
||||
|
||||
def _check_unrouteable(self, decision: RoutingDecision) -> None:
|
||||
"""Validate target_room against subscribed IDs.
|
||||
@@ -372,7 +410,7 @@ class BidirectionalRouter:
|
||||
|
||||
return catch_all.room_id
|
||||
|
||||
def _llm_room_lookup(self, text: str) -> Optional[RoomEntry]:
|
||||
def _llm_room_lookup(self, text: str) -> Tuple[Optional[RoomEntry], bool]:
|
||||
"""Fragt das LLM: „In welchen Raum gehört diese Nachricht?“
|
||||
|
||||
Baut eine Raum-Liste mit IDs, Namen und ALLEN Keywords aus der RoomMap
|
||||
@@ -383,7 +421,9 @@ class BidirectionalRouter:
|
||||
text: Der Nachrichtentext.
|
||||
|
||||
Returns:
|
||||
Den gematchten ``RoomEntry``, oder ``None`` bei Fehler/kein Match.
|
||||
(RoomEntry, False) — LLM erfolgreich, Raum gefunden
|
||||
(None, False) — LLM erfolgreich, kein Match (CATCH_ALL)
|
||||
(None, True) — LLM-Aufruf fehlgeschlagen (Exception/Timeout)
|
||||
"""
|
||||
# Build room list for LLM prompt: "ID | Name | Keywords"
|
||||
room_list_parts = []
|
||||
@@ -412,29 +452,29 @@ class BidirectionalRouter:
|
||||
# Try LLM — expects room ID directly
|
||||
llm_result = self._classifier._llm_raw_sync(prompt)
|
||||
if llm_result is None:
|
||||
return None
|
||||
return None, True # LLM-Aufruf fehlgeschlagen
|
||||
|
||||
# Parse LLM response — should be a room ID or "CATCH_ALL"
|
||||
response = llm_result.strip()
|
||||
|
||||
if response.upper() in ("CATCH_ALL", "CATCH-ALL", "CATCHALL"):
|
||||
return None
|
||||
return None, False # LLM erfolgreich, kein Match
|
||||
|
||||
# Direct lookup by room ID (LLM returns the ID directly)
|
||||
room = self._room_map.get_room_by_id(response)
|
||||
if room is not None:
|
||||
return room
|
||||
return room, False
|
||||
|
||||
# Fallback: try name matching (for LLMs that return names instead of IDs)
|
||||
response_lower = response.lower()
|
||||
for room in self._room_map.rooms.values():
|
||||
if response_lower == room.name.lower():
|
||||
return room
|
||||
return room, False
|
||||
if response_lower in room.name.lower():
|
||||
return room
|
||||
return room, False
|
||||
|
||||
logger.debug("LLM returned unknown room: %s", response)
|
||||
return None
|
||||
return None, False
|
||||
|
||||
# ── Route Validation ─────────────────────────────────────────────
|
||||
|
||||
|
||||
Reference in New Issue
Block a user