fix(HOTFIX): Widerspruch-Clarify nur nach tatsächlicher Auto-Route

Der Widerspruch-Clarify feuerte ohne vorherige Auto-Route, weil
classify_and_route() detect_routing_contradiction() unbedingt aufrief —
nur der Text wurde geprüft, nicht ob _recent_auto_routes[origin_room_id]
existiert. MyTestBot1 zeigte den Clarify bei normalen Nachrichten und
ersetzte damit den Butler-Modus.

Fix: Widerspruch-Check nur wenn BEIDE Bedingungen erfüllt sind:
1. aktiver _recent_auto_routes[origin_room_id]-Eintrag (Auto-Route fand statt)
2. Text enthält Widerspruch-Muster

- adapter_integration.py: set_recent_auto_routes() + Gate im Pre-Check
- adapter.py: bindet _recent_auto_routes-Dict an den Handler
- 2 neue Tests (kein Auto-Route → kein Clarify; mit Auto-Route → Clarify)
This commit is contained in:
Bernd (Hermes)
2026-08-20 07:59:29 +02:00
parent 67ef5183d4
commit 86f4eb612b
3 changed files with 136 additions and 1 deletions
@@ -333,3 +333,102 @@ class TestAutoRouteContradictionScenarios:
# 2. Nächste Nachricht OHNE Keyword → kein Widerspruch
r2 = archivar._feedback_detector.detect("Wie ist das Wetter heute?")
assert r2 is None
# ═══════════════════════════════════════════════════════════════════════════
# Test 5: HOTFIX — Widerspruch-Clarify NUR nach tatsächlicher Auto-Route
# ═══════════════════════════════════════════════════════════════════════════
class TestContradictionRequiresPriorAutoRoute:
"""Der Widerspruch-Clarify darf NUR nach einer tatsächlichen Auto-Route feuern.
HOTFIX: classify_and_route() überspringt den Widerspruch-Check komplett,
wenn für den Ursprungsraum KEIN aktiver _recent_auto_routes-Eintrag existiert.
"""
def _make_handler(self, tmp_path):
"""Handler mit echtem Detector + Mock-Router, der normal routet."""
from plugin.archivar.adapter_integration import ArchivarMessageHandler
from plugin.archivar.router import RoutingDecision
handler = ArchivarMessageHandler.__new__(ArchivarMessageHandler)
handler._initialized = True
handler._feedback_detector = _make_detector()
handler._recent_auto_routes = {}
handler._stats = {
"total": 0, "classified": 0, "contradiction": 0, "errors": 0,
}
handler._state = _make_state(tmp_path)
handler._room_map = MagicMock()
handler._room_map.rooms = {}
handler._doc_store = None
# Callbacks für den normalen Move-Request-Pfad (Butler-Modus).
handler._send_callback = AsyncMock(return_value=True)
handler._button_send_callback = AsyncMock(return_value=True)
# Mock-Router: liefert eine normale (nicht-Widerspruch) Decision.
normal_decision = RoutingDecision(
category="work_order",
target_room="room_target",
confidence=0.9,
stay_in_room=False,
move_request_sent=True,
)
handler._router = MagicMock()
handler._router.route = AsyncMock(return_value=normal_decision)
return handler
def test_no_prior_auto_route_no_clarify(self, tmp_path):
"""Nachricht OHNE vorherige Auto-Route + 'passt hier her' → KEIN Clarify.
Der Text enthält ein Widerspruch-Muster ('passt hier her'), aber es gibt
keinen _recent_auto_routes-Eintrag → der Widerspruch-Check wird übersprungen
und die Nachricht normal verarbeitet (kein is_contradiction).
"""
handler = self._make_handler(tmp_path)
origin_room = "room_origin"
# Kein Auto-Route-Eintrag vorhanden
assert origin_room not in handler._recent_auto_routes
decision = asyncio.run(
handler.classify_and_route(
"Nein, es passt hier her", origin_room_id=origin_room
)
)
# KEIN Widerspruch → normale Verarbeitung
assert decision.is_contradiction is False
assert decision.contradiction_result is None
assert decision.category == "work_order"
assert handler._stats.get("contradiction", 0) == 0
def test_with_prior_auto_route_clarify_fires(self, tmp_path):
"""Nachricht MIT vorheriger Auto-Route + 'passt hier her' → Clarify.
Gegenprobe: existiert ein _recent_auto_routes-Eintrag, feuert der
Widerspruch-Check und setzt is_contradiction=True.
"""
handler = self._make_handler(tmp_path)
origin_room = "room_origin"
# Auto-Route hat tatsächlich stattgefunden
handler._recent_auto_routes[origin_room] = {
"origin_room": origin_room,
"target_room": "room_target",
"topic": "dokumentation",
"timestamp": 1234567890.0,
"source": "auto",
}
decision = asyncio.run(
handler.classify_and_route(
"Nein, es passt hier her", origin_room_id=origin_room
)
)
assert decision.is_contradiction is True
assert decision.contradiction_result is not None
assert decision.category == "contradiction"
assert handler._stats.get("contradiction", 0) == 1