fix: V8.5 — DM-Bypass differenziert: 100%-Keywords in DMs erkannt + Butler Move-Request (R8)
This commit is contained in:
@@ -705,9 +705,10 @@ class TestV843RegressionGate(unittest.TestCase):
|
||||
"Agent should be dispatched in target room (sticky route)")
|
||||
|
||||
def test_dm_bypass_still_works(self):
|
||||
"""DM-Nachrichten bypassen Archivar weiterhin.
|
||||
"""V8.5: DM-Nachrichten rufen Archivar auf (100%-Keyword-Check).
|
||||
|
||||
Regression-Gate: DM-Bypass darf NICHT durch den Fix gebrochen werden.
|
||||
Regression-Gate: DM ruft classify_and_route auf, aber
|
||||
ohne 100%-Keyword → Catch-All (stay_in_room).
|
||||
"""
|
||||
msg_obj = {
|
||||
"_id": "msg_dm_001",
|
||||
@@ -731,9 +732,9 @@ class TestV843RegressionGate(unittest.TestCase):
|
||||
|
||||
asyncio.run(self.adapter._process_incoming(msg_obj))
|
||||
|
||||
# DM should bypass Archivar → NO classify_and_route
|
||||
self.assertEqual(call_count[0], 0,
|
||||
"classify_and_route should NOT be called for DM messages")
|
||||
# V8.5: DM ruft classify_and_route auf (100%-Keyword-Check)
|
||||
self.assertEqual(call_count[0], 1,
|
||||
"classify_and_route SHOULD be called for DM messages (V8.5)")
|
||||
|
||||
def test_pending_questions_bypass_still_works(self):
|
||||
"""Pending-Questions-Bypass funktioniert weiterhin.
|
||||
|
||||
@@ -0,0 +1,347 @@
|
||||
"""
|
||||
V8.5: DM-Routing differenziert — 100%-Keywords → Butler, Rest → Catch-All.
|
||||
|
||||
Tests:
|
||||
1. DM + 100%-Keyword → Move-Request (Butler, move_request_sent=True)
|
||||
2. DM + weiches Keyword (context) → Catch-All (stay_in_room=True)
|
||||
3. DM + kein Keyword → Catch-All
|
||||
4. DM + Strategie-Thema → stay_in_room (existing behavior)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
|
||||
|
||||
from plugin.archivar.router import BidirectionalRouter, RoutingDecision
|
||||
from plugin.archivar.room_map import RoomMap, RoomEntry
|
||||
from plugin.archivar.classifier import IntentClassifier, EmbeddingClient
|
||||
from plugin.archivar.archivar_state import ArchivarState
|
||||
from plugin.archivar.routing_telemetry import routing_telemetry
|
||||
from plugin.archivar.routing_counter import routing_counter
|
||||
|
||||
|
||||
# ── Helpers ────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class MockEmbeddingClient(EmbeddingClient):
|
||||
"""Mock embedding client that returns fixed vectors."""
|
||||
|
||||
def get_embedding(self, text: str) -> Optional[list]:
|
||||
return [0.1] * 768
|
||||
|
||||
|
||||
def _make_room_map_with_rc_plugin() -> RoomMap:
|
||||
"""Build a RoomMap with #RC-Plugin (100%: 'Rocket.Chat Plugin') + Catch-All."""
|
||||
tmp = tempfile.TemporaryDirectory()
|
||||
map_path = Path(tmp.name) / "room-map.json"
|
||||
|
||||
test_data = {
|
||||
"version": 5,
|
||||
"rooms": {
|
||||
"room_rc_plugin": {
|
||||
"name": "#RC-Plugin",
|
||||
"theme": "Rocket.Chat Plugin Entwicklung",
|
||||
"description": "Entwicklung des Hermes Rocket.Chat Plugins",
|
||||
"keywords_100": ["Rocket.Chat Plugin", "plugin", "rocketchat"],
|
||||
"keywords_context": ["adapter", "gateway"],
|
||||
"catch_all": False,
|
||||
},
|
||||
"room_lan_doku": {
|
||||
"name": "#LAN-Doku",
|
||||
"theme": "LAN Dokumentation",
|
||||
"description": "Dokumentation des LAN-Netzwerks",
|
||||
"keywords_100": ["LAN-Dokumentation", "dokumentation", "doku"],
|
||||
"keywords_context": ["netzwerk", "lan"],
|
||||
"catch_all": False,
|
||||
},
|
||||
"room_catchall": {
|
||||
"name": "DM mit Andreas",
|
||||
"theme": "Catch-All für DMs",
|
||||
"description": "Catch-All für alle Themen ohne eigenen Raum",
|
||||
"keywords_100": [],
|
||||
"keywords_context": [],
|
||||
"catch_all": True,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
with open(map_path, "w") as f:
|
||||
json.dump(test_data, f)
|
||||
|
||||
return RoomMap.load(map_path)
|
||||
|
||||
|
||||
def _make_router(room_map: RoomMap, mock_llm_fn=None) -> BidirectionalRouter:
|
||||
"""Build a BidirectionalRouter with mock classifier and state."""
|
||||
classifier = IntentClassifier(
|
||||
embedding_client=MockEmbeddingClient(),
|
||||
llm_client=None,
|
||||
)
|
||||
|
||||
if mock_llm_fn is not None:
|
||||
classifier._llm_raw = mock_llm_fn
|
||||
|
||||
state = ArchivarState(":memory:")
|
||||
state.load()
|
||||
|
||||
router = BidirectionalRouter(room_map, classifier, state)
|
||||
router.set_subscribed_room_ids(list(room_map.rooms.keys()))
|
||||
return router
|
||||
|
||||
|
||||
# ── DM Routing Tests ───────────────────────────────────────────────────────
|
||||
|
||||
|
||||
class TestDMRouting100PercentKeyword:
|
||||
"""V8.5: DM + 100%-Keyword → Move-Request (Butler-Prinzip)."""
|
||||
|
||||
@pytest.fixture
|
||||
def router(self):
|
||||
room_map = _make_room_map_with_rc_plugin()
|
||||
return _make_router(room_map)
|
||||
|
||||
def test_dm_100_percent_keyword_rc_plugin(self, router):
|
||||
"""DM + 'Rocket.Chat Plugin' → Move-Request für #RC-Plugin."""
|
||||
decision = asyncio.run(router.route(
|
||||
"Rocket.Chat Plugin: Wie implementiere ich den neuen Adapter?",
|
||||
origin_room_id="", # DM
|
||||
))
|
||||
|
||||
assert decision is not None
|
||||
assert decision.move_request_sent is True, (
|
||||
f"Expected move_request_sent=True (Butler), got {decision.move_request_sent}"
|
||||
)
|
||||
assert decision.move_request_target_name == "#RC-Plugin", (
|
||||
f"Expected target '#RC-Plugin', got '{decision.move_request_target_name}'"
|
||||
)
|
||||
assert decision.move_request_target_id == "room_rc_plugin", (
|
||||
f"Expected target_id 'room_rc_plugin', got '{decision.move_request_target_id}'"
|
||||
)
|
||||
assert decision.stay_in_room is False, (
|
||||
f"Expected stay_in_room=False, got {decision.stay_in_room}"
|
||||
)
|
||||
assert decision.category == "work_order", (
|
||||
f"Expected category='work_order', got '{decision.category}'"
|
||||
)
|
||||
|
||||
def test_dm_100_percent_keyword_lan_doku(self, router):
|
||||
"""DM + 'LAN-Dokumentation' → Move-Request für #LAN-Doku."""
|
||||
decision = asyncio.run(router.route(
|
||||
"LAN-Dokumentation: Bitte das Netzwerk-Diagramm aktualisieren",
|
||||
origin_room_id="", # DM
|
||||
))
|
||||
|
||||
assert decision is not None
|
||||
assert decision.move_request_sent is True, (
|
||||
f"Expected move_request_sent=True (Butler), got {decision.move_request_sent}"
|
||||
)
|
||||
assert decision.move_request_target_name == "#LAN-Doku", (
|
||||
f"Expected target '#LAN-Doku', got '{decision.move_request_target_name}'"
|
||||
)
|
||||
assert decision.move_request_target_id == "room_lan_doku", (
|
||||
f"Expected target_id 'room_lan_doku', got '{decision.move_request_target_id}'"
|
||||
)
|
||||
assert decision.stay_in_room is False
|
||||
|
||||
def test_dm_100_percent_keyword_plugin_lowercase(self, router):
|
||||
"""DM + 'plugin' (lowercase 100%-Keyword) → Move-Request für #RC-Plugin."""
|
||||
decision = asyncio.run(router.route(
|
||||
"plugin: Gibt es einen Bug im Adapter?",
|
||||
origin_room_id="", # DM
|
||||
))
|
||||
|
||||
assert decision is not None
|
||||
assert decision.move_request_sent is True, (
|
||||
f"Expected move_request_sent=True (Butler), got {decision.move_request_sent}"
|
||||
)
|
||||
assert decision.move_request_target_name == "#RC-Plugin"
|
||||
|
||||
|
||||
class TestDMRoutingSoftKeyword:
|
||||
"""V8.5: DM + weiches Keyword (context) → Catch-All (kein Routing)."""
|
||||
|
||||
@pytest.fixture
|
||||
def router(self):
|
||||
room_map = _make_room_map_with_rc_plugin()
|
||||
return _make_router(room_map)
|
||||
|
||||
def test_dm_context_keyword_adapter(self, router):
|
||||
"""DM + 'adapter' (context keyword) → Catch-All, kein Routing."""
|
||||
decision = asyncio.run(router.route(
|
||||
"Der Adapter funktioniert nicht richtig",
|
||||
origin_room_id="", # DM
|
||||
))
|
||||
|
||||
assert decision is not None
|
||||
assert decision.is_catch_all is True, (
|
||||
f"Expected is_catch_all=True, got {decision.is_catch_all}"
|
||||
)
|
||||
assert decision.stay_in_room is True, (
|
||||
f"Expected stay_in_room=True (Catch-All), got {decision.stay_in_room}"
|
||||
)
|
||||
assert decision.move_request_sent is False, (
|
||||
f"Expected move_request_sent=False (kein Routing), got {decision.move_request_sent}"
|
||||
)
|
||||
assert decision.target_room == "", (
|
||||
f"Expected empty target_room, got '{decision.target_room}'"
|
||||
)
|
||||
|
||||
def test_dm_context_keyword_gateway(self, router):
|
||||
"""DM + 'gateway' (context keyword) → Catch-All, kein Routing."""
|
||||
decision = asyncio.run(router.route(
|
||||
"Der Gateway verbindet sich nicht",
|
||||
origin_room_id="", # DM
|
||||
))
|
||||
|
||||
assert decision is not None
|
||||
assert decision.is_catch_all is True
|
||||
assert decision.stay_in_room is True
|
||||
assert decision.move_request_sent is False
|
||||
|
||||
def test_dm_context_keyword_netzwerk(self, router):
|
||||
"""DM + 'netzwerk' (context keyword für #LAN-Doku) → Catch-All."""
|
||||
decision = asyncio.run(router.route(
|
||||
"Das Netzwerk ist langsam heute",
|
||||
origin_room_id="", # DM
|
||||
))
|
||||
|
||||
assert decision is not None
|
||||
assert decision.is_catch_all is True
|
||||
assert decision.stay_in_room is True
|
||||
assert decision.move_request_sent is False
|
||||
|
||||
|
||||
class TestDMRoutingNoKeyword:
|
||||
"""V8.5: DM + kein Keyword → Catch-All (LLM antwortet im DM)."""
|
||||
|
||||
@pytest.fixture
|
||||
def router(self):
|
||||
room_map = _make_room_map_with_rc_plugin()
|
||||
return _make_router(room_map)
|
||||
|
||||
def test_dm_no_keyword_smalltalk(self, router):
|
||||
"""DM + 'Wie geht es dir?' → Catch-All."""
|
||||
decision = asyncio.run(router.route(
|
||||
"Wie geht es dir?",
|
||||
origin_room_id="", # DM
|
||||
))
|
||||
|
||||
assert decision is not None
|
||||
assert decision.is_catch_all is True, (
|
||||
f"Expected is_catch_all=True, got {decision.is_catch_all}"
|
||||
)
|
||||
assert decision.stay_in_room is True, (
|
||||
f"Expected stay_in_room=True (Catch-All), got {decision.stay_in_room}"
|
||||
)
|
||||
assert decision.move_request_sent is False, (
|
||||
f"Expected move_request_sent=False, got {decision.move_request_sent}"
|
||||
)
|
||||
|
||||
def test_dm_no_keyword_generic_question(self, router):
|
||||
"""DM + generische Frage → Catch-All."""
|
||||
decision = asyncio.run(router.route(
|
||||
"Kannst du mir helfen?",
|
||||
origin_room_id="", # DM
|
||||
))
|
||||
|
||||
assert decision is not None
|
||||
assert decision.is_catch_all is True
|
||||
assert decision.stay_in_room is True
|
||||
assert decision.move_request_sent is False
|
||||
|
||||
def test_dm_no_keyword_empty_text(self, router):
|
||||
"""DM + leerer Text → Catch-All."""
|
||||
decision = asyncio.run(router.route(
|
||||
"",
|
||||
origin_room_id="", # DM
|
||||
))
|
||||
|
||||
assert decision is not None
|
||||
assert decision.is_catch_all is True
|
||||
assert decision.stay_in_room is True
|
||||
|
||||
|
||||
class TestDMRoutingStrategieTopic:
|
||||
"""V8.5: DM + Strategie-Thema → stay_in_room (bestehendes Verhalten)."""
|
||||
|
||||
@pytest.fixture
|
||||
def router(self):
|
||||
room_map = _make_room_map_with_rc_plugin()
|
||||
return _make_router(room_map)
|
||||
|
||||
def test_dm_strategie_absprache(self, router):
|
||||
"""DM + 'absprache' → stay_in_room (Strategie-Modus)."""
|
||||
decision = asyncio.run(router.route(
|
||||
"Wir müssen eine Absprache zum Plugin-Release treffen",
|
||||
origin_room_id="", # DM
|
||||
))
|
||||
|
||||
assert decision is not None
|
||||
assert decision.stay_in_room is True, (
|
||||
f"Expected stay_in_room=True (Strategie), got {decision.stay_in_room}"
|
||||
)
|
||||
assert decision.move_request_sent is False, (
|
||||
f"Expected move_request_sent=False, got {decision.move_request_sent}"
|
||||
)
|
||||
|
||||
def test_dm_strategie_entscheidung(self, router):
|
||||
"""DM + 'entscheidung' → stay_in_room (Strategie-Modus)."""
|
||||
decision = asyncio.run(router.route(
|
||||
"Wir brauchen eine Entscheidung zur Architektur",
|
||||
origin_room_id="", # DM
|
||||
))
|
||||
|
||||
assert decision is not None
|
||||
assert decision.stay_in_room is True
|
||||
assert decision.move_request_sent is False
|
||||
|
||||
|
||||
class TestDMRoutingNonDMUnaffected:
|
||||
"""V8.5: Nicht-DM-Räume bleiben unverändert (Regression-Guard)."""
|
||||
|
||||
@pytest.fixture
|
||||
def router(self):
|
||||
room_map = _make_room_map_with_rc_plugin()
|
||||
return _make_router(room_map)
|
||||
|
||||
def test_non_dm_100_percent_keyword_auto_route(self, router):
|
||||
"""Nicht-DM + 100%-Keyword → Auto-Route (kein Butler)."""
|
||||
decision = asyncio.run(router.route(
|
||||
"Rocket.Chat Plugin: Bug im Adapter",
|
||||
origin_room_id="room_lan_doku", # Nicht-DM
|
||||
))
|
||||
|
||||
assert decision is not None
|
||||
# In Nicht-DM: 100%-Keywords → Auto-Route (move_request_sent=False)
|
||||
assert decision.move_request_sent is False, (
|
||||
f"Expected move_request_sent=False (Auto-Route in non-DM), "
|
||||
f"got {decision.move_request_sent}"
|
||||
)
|
||||
assert decision.target_room == "room_rc_plugin", (
|
||||
f"Expected target 'room_rc_plugin', got '{decision.target_room}'"
|
||||
)
|
||||
|
||||
def test_non_dm_context_keyword_butler(self, router):
|
||||
"""Nicht-DM + context keyword → Butler (move_request_sent=True)."""
|
||||
decision = asyncio.run(router.route(
|
||||
"Der Adapter hat einen Fehler",
|
||||
origin_room_id="room_lan_doku", # Nicht-DM
|
||||
))
|
||||
|
||||
assert decision is not None
|
||||
# In Nicht-DM: context keywords → Butler
|
||||
assert decision.move_request_sent is True, (
|
||||
f"Expected move_request_sent=True (Butler in non-DM), "
|
||||
f"got {decision.move_request_sent}"
|
||||
)
|
||||
Reference in New Issue
Block a user