fix: V8.4.3 — B2-Fallthrough Bug: classify_and_route nach Sticky-Bypass/B2-No-Topic/Expliziter-Wechsel (Red Flag Trigger erreichbar)

Root Cause: In adapter.py, die 4 Fall-through-Pfade im Sticky-Block
(Topic-Mismatch, B2-No-Topic, Catch-All-Target, stay_in_room-Origin)
hatten den Kommentar 'Fall through to Archivar classification' aber
classify_and_route() wurde NIE aufgerufen. Der else-Branch (Z.2107)
war der else für die gesamte if/elif-Kette — wenn der Sticky-elif
matchte, wurde der else-Zweig übersprungen.

Fix: _should_classify-Flag. Intentional bypass paths setzen False.
Fall-through paths behalten True. Nach der if/elif-Kette:
if _should_classify: classify_and_route().

Tests (11 neue, alle grün):
- B2 No-Topic → classify_and_route wird aufgerufen
- B2 No-Topic + Red Flag Decision → Pre-Dispatch-Hook aktiv
- B2 No-Topic + Realer Router → Flow komplett
- Topic-Mismatch → classify_and_route wird aufgerufen
- Expired Sticky → classify_and_route wird aufgerufen
- Telemetrie: red_flag_dispatched Counter
- Telemetrie: Kein Red Flag bei 1 Raum
- Regression: Sticky+Keyword-Match routet direkt
- Regression: DM-Bypass funktioniert
- Regression: Pending-Questions-Bypass funktioniert
- Regression: Normaler Pfad ruft classify_and_route

Regression-Suite: 594 passed, 2 skipped, 1 pre-existing failure
This commit is contained in:
Bernd (Hermes)
2026-07-15 22:10:57 +02:00
parent b83fa35017
commit dadd1849ff
2 changed files with 868 additions and 47 deletions
+62 -47
View File
@@ -1915,6 +1915,11 @@ class RocketChatAdapter(BasePlatformAdapter):
logger.debug("[rocketchat] ARCHIVAR-DEBUG: Entering Archivar block — room_type=%s, is_auftrag=%s, _MULTIROOM=%s, archivar=%s", room_type, is_auftrag, _MULTIROOM(), self.archivar is not None)
# V8.4.3: Flag für zentrale classify_and_route-Aufrufstelle.
# Intentional bypass paths setzen False. Fall-through paths
# (Sticky-Bypass, B2-No-Topic, Expliziter-Wechsel-ohne-#) behalten True.
_should_classify = True
try:
if _MULTIROOM() and self.archivar is not None:
if is_button_response or room_id in self._rooms_with_pending_questions:
@@ -1924,6 +1929,7 @@ class RocketChatAdapter(BasePlatformAdapter):
routing_counter.increment('pending_questions_bypass')
logger.info("ROUTING BYPASS: reason=pending_questions_bypass, origin_room=%s", room_id)
self._rooms_with_pending_questions.discard(room_id)
_should_classify = False
elif self.archivar._state.should_stay_in_room(room_id) == "always":
# Bug 4 fix: Persistierte Entscheidung (always) aus ArchivarState lesen.
# P0-B: "never_route" ist jetzt thematisch (v7) — wird im Router
@@ -1931,6 +1937,7 @@ class RocketChatAdapter(BasePlatformAdapter):
# Nur "always" bypassed Archivar komplett.
logger.debug("[rocketchat] Room %s has persisted decision=always — Archivar bypassed", room_id[:8])
logger.debug("[rocketchat] ARCHIVAR-DEBUG: Archivar bypassed — persisted_state=always")
_should_classify = False
elif room_type == "d":
# V8: DM-Bypass bleibt, weil DM kein Multiroom-Routing braucht.
# is_auftrag wird NICHT mehr als Gate verwendet — die
@@ -1939,6 +1946,7 @@ class RocketChatAdapter(BasePlatformAdapter):
logger.debug("[rocketchat] ARCHIVAR-DEBUG: Archivar bypassed — DM (no multiroom)")
routing_counter.increment('dm_routing_skipped')
logger.info("ROUTING BYPASS: reason=dm_routing_skipped, origin_room=%s", room_id)
_should_classify = False
elif room_id in self._session_room_preferences:
# V8.3-5: Topic-Scoped Sticky — Auto-Route nur bei gleichem Thema
pref = self._session_room_preferences[room_id]
@@ -2096,53 +2104,60 @@ class RocketChatAdapter(BasePlatformAdapter):
"[rocketchat] V8.1 P2: Topic-Scoped Sticky expired for room %s",
room_id[:8],
)
else:
logger.debug("[rocketchat] ARCHIVAR-DEBUG: Calling classify_and_route for room=%s", room_id[:12])
decision = await self.archivar.classify_and_route(text, origin_room_id=room_id)
# V8.3.3: Telemetrie nach jeder Routing-Entscheidung flushen
routing_telemetry.flush()
if decision and decision.category:
category = decision.category
# Wenn Zielraum = Ursprungsraum → stay_in_room, kein Move-Request
# MUSS VOR _THEMATIC_TARGET_ROOM.set() kommen, sonst wird
# der Zielraum gesetzt und send() routet in den DM
if (hasattr(decision, 'target_room') and decision.target_room
and decision.target_room == room_id):
decision.stay_in_room = True
decision.move_request_sent = False
# ── Archivar-Kontext-Weitergabe: Zielraum + Frage-Nr ──────
# P0-BUGFIX: Nur setzen wenn NICHT stay_in_room.
# Bei stay_in_room=True (Meta-Fragen, Smalltalk, etc.)
# bleibt der Agent im Ursprungsraum — kein Zielraum nötig.
if (hasattr(decision, 'target_room') and decision.target_room
and not getattr(decision, 'stay_in_room', False)):
_THEMATIC_TARGET_ROOM.set(decision.target_room)
global _question_counter
with _question_counter_lock:
_question_counter += 1
# ── Ende Kontext-Weitergabe ──────────────────────────────
# Archivar v4 Move-Request: _pending_move_requests befüllen, damit
# der Button-Response-Handler (lines 717-764) die Antwort korrekt
# verarbeiten und _CURRENT_ORIGIN_ROOM auf den Zielraum setzen kann.
if (getattr(decision, 'move_request_sent', False)
and hasattr(decision, 'target_room')
and decision.target_room):
self._pending_move_requests[room_id] = {
'text': text,
'origin_room': room_id,
'target_room': decision.target_room,
'target_name': getattr(
decision, 'move_request_target_name', 'Zielraum'),
'username': username,
'message_id': msg_id,
}
# Lernsystem: Keywords für Feedback-Loop speichern
if hasattr(decision, 'matched_keywords') and decision.matched_keywords:
self.archivar._routed_messages[msg_id] = {
"keywords": decision.matched_keywords,
"target_room": decision.target_room,
"timestamp": time.time()
}
# V8.4.3: Kein else mehr — classify_and_route wird zentral
# nach der if/elif-Kette aufgerufen, wenn _should_classify=True.
# Fall-through paths (Sticky-Bypass, B2-No-Topic, Expired Sticky)
# behalten _should_classify=True und erreichen classify_and_route.
# ── V8.4.3: Zentrale classify_and_route-Aufrufstelle ──────────
# Alle Fall-through-Pfade (Sticky-Bypass, B2-No-Topic,
# Expliziter-Wechsel-ohne-#, Expired Sticky) landen hier.
if _should_classify:
logger.debug("[rocketchat] ARCHIVAR-DEBUG: Calling classify_and_route for room=%s", room_id[:12])
decision = await self.archivar.classify_and_route(text, origin_room_id=room_id)
# V8.3.3: Telemetrie nach jeder Routing-Entscheidung flushen
routing_telemetry.flush()
if decision and decision.category:
category = decision.category
# Wenn Zielraum = Ursprungsraum → stay_in_room, kein Move-Request
# MUSS VOR _THEMATIC_TARGET_ROOM.set() kommen, sonst wird
# der Zielraum gesetzt und send() routet in den DM
if (hasattr(decision, 'target_room') and decision.target_room
and decision.target_room == room_id):
decision.stay_in_room = True
decision.move_request_sent = False
# ── Archivar-Kontext-Weitergabe: Zielraum + Frage-Nr ──────
# P0-BUGFIX: Nur setzen wenn NICHT stay_in_room.
# Bei stay_in_room=True (Meta-Fragen, Smalltalk, etc.)
# bleibt der Agent im Ursprungsraum — kein Zielraum nötig.
if (hasattr(decision, 'target_room') and decision.target_room
and not getattr(decision, 'stay_in_room', False)):
_THEMATIC_TARGET_ROOM.set(decision.target_room)
global _question_counter
with _question_counter_lock:
_question_counter += 1
# ── Ende Kontext-Weitergabe ──────────────────────────────
# Archivar v4 Move-Request: _pending_move_requests befüllen, damit
# der Button-Response-Handler (lines 717-764) die Antwort korrekt
# verarbeiten und _CURRENT_ORIGIN_ROOM auf den Zielraum setzen kann.
if (getattr(decision, 'move_request_sent', False)
and hasattr(decision, 'target_room')
and decision.target_room):
self._pending_move_requests[room_id] = {
'text': text,
'origin_room': room_id,
'target_room': decision.target_room,
'target_name': getattr(
decision, 'move_request_target_name', 'Zielraum'),
'username': username,
'message_id': msg_id,
}
# Lernsystem: Keywords für Feedback-Loop speichern
if hasattr(decision, 'matched_keywords') and decision.matched_keywords:
self.archivar._routed_messages[msg_id] = {
"keywords": decision.matched_keywords,
"target_room": decision.target_room,
"timestamp": time.time()
}
except asyncio.TimeoutError:
logger.warning("[rocketchat] Archivar classify_and_route() timed out")
with _question_counter_lock: