feat(archivar): Zweistufiges Keyword-Evidenz-System — 100% + context

This commit is contained in:
Hermes Agent
2026-06-12 19:30:27 +02:00
parent 53dd436a86
commit 017dc2b69c
2 changed files with 199 additions and 27 deletions
+138 -6
View File
@@ -41,12 +41,22 @@ def _make_map_json(rooms: list[dict] | None = None, excluded: list[str] | None =
"fact_store_path": "/tmp/test_docs",
}
for room in (rooms or []):
data["rooms"][room["room_id"]] = {
room_data = {
"name": room["name"],
"theme": room.get("theme", ""),
"keywords": room.get("keywords", []),
"catch_all": room.get("catch_all", False),
}
# Handle both old and new room structures
if "keywords_100" in room or "keywords_context" in room:
# New structure (version 2)
room_data["keywords_100"] = room.get("keywords_100", [])
room_data["keywords_context"] = room.get("keywords_context", [])
else:
# Old structure (version 1)
room_data["keywords"] = room.get("keywords", [])
data["rooms"][room["room_id"]] = room_data
return json.dumps(data)
@@ -162,6 +172,127 @@ class TestRoomMapKeywords:
assert result.name == "#Plugin"
# ── Two-Stage Keyword Evidence System ──────────────────────────────────────
class TestTwoStageKeywordEvidence:
"""Tests for the two-stage keyword evidence system."""
@pytest.fixture
def room_map_v2(self) -> RoomMap:
"""Create a RoomMap with version 2 structure (keywords_100 + keywords_context)."""
data = {
"version": 2,
"rooms": {
"r1": {
"name": "#Plugin",
"theme": "plugin dev",
"keywords_100": ["rocketchat", "rc", "ddp", "websocket"],
"keywords_context": ["plugin", "gateway", "deployment", "button", "sync", "hermes"]
},
"r2": {
"name": "#BAT",
"theme": "trading",
"keywords_100": ["bat", "bat-team", "trading", "krypto"],
"keywords_context": ["gateway"]
},
"r3": {
"name": "DM Admin",
"theme": "admin",
"keywords_100": [],
"keywords_context": [],
"catch_all": True
}
},
"excluded_rooms": [],
"fact_store_path": "/tmp/test_docs"
}
json_str = json.dumps(data)
path = _tmp_map(json_str)
try:
return RoomMap.load(path)
finally:
path.unlink(missing_ok=True)
def test_100_percent_keywords_immediate_match(self, room_map_v2):
"""100% keywords should match immediately, even with low context score."""
# "rocketchat" is a 100% keyword for #Plugin room
result = room_map_v2.find_room_by_keywords("rocketchat plugin bug")
assert result is not None
assert result.name == "#Plugin"
def test_context_keywords_min_score(self, room_map_v2):
"""Context keywords should require min_score=2."""
# Only one context keyword match should not be enough
result = room_map_v2.find_room_by_keywords("plugin")
assert result is None
# Two context keyword matches should be enough
result = room_map_v2.find_room_by_keywords("plugin gateway")
assert result is not None
assert result.name == "#Plugin"
def test_origin_priority_with_context_keywords(self, room_map_v2):
"""Origin room should get priority with context keywords."""
# Message from BAT room with context keywords for BAT
# Use "gateway issue" which matches both rooms, but origin priority should win
# We need at least min_score=2, so add another keyword
result = room_map_v2.find_room_by_keywords("gateway bat", origin_room_id="r2")
assert result is not None
assert result.room_id == "r2" # Should go to origin room (bat is 100% keyword for BAT)
# Test with multiple context keywords
result = room_map_v2.find_room_by_keywords("gateway trading", origin_room_id="r2")
assert result is not None
assert result.room_id == "r2" # Should go to origin room (gateway + trading = 2 context keywords)
def test_global_fallback_with_context_keywords(self, room_map_v2):
"""Global fallback should work with context keywords."""
# Context keywords for Plugin room
result = room_map_v2.find_room_by_keywords("plugin deployment")
assert result is not None
assert result.name == "#Plugin"
def test_100_percent_overrides_origin_priority(self, room_map_v2):
"""100% keywords should override origin priority."""
# Message from BAT room but with 100% keyword for Plugin
result = room_map_v2.find_room_by_keywords("rocketchat bug", origin_room_id="r2")
assert result is not None
assert result.name == "#Plugin" # Should go to Plugin room, not origin
def test_backward_compatibility_with_v1_structure(self):
"""Old room-map.json (version 1) should still work."""
# Simulate old structure with only "keywords" field
data = {
"version": 1,
"rooms": {
"r1": {
"name": "#Plugin",
"theme": "plugin dev",
"keywords": ["plugin", "gateway", "deployment"]
},
"r2": {
"name": "DM Admin",
"theme": "admin",
"keywords": [],
"catch_all": True
}
},
"excluded_rooms": [],
"fact_store_path": "/tmp/test_docs"
}
json_str = json.dumps(data)
path = _tmp_map(json_str)
try:
room_map = RoomMap.load(path)
# Should work like before, treating all keywords as context keywords
result = room_map.find_room_by_keywords("plugin gateway")
assert result is not None
assert result.name == "#Plugin"
finally:
path.unlink(missing_ok=True)
# ── Exclusion ──────────────────────────────────────────────────────────────
@@ -255,7 +386,7 @@ class TestRoomMapValidation:
])
errors = rm.validate(subscribed_ids={"r999"})
assert len(errors) >= 1
assert any("catch_all room" in e.lower() for e in errors)
assert any("catch-all room" in e.lower() for e in errors)
# ── Migration from Legacy Env Vars ─────────────────────────────────────────
@@ -320,7 +451,8 @@ class TestRoomEntry:
def test_match_score(self):
entry = RoomEntry(
room_id="r1", name="#Dev", theme="dev",
keywords=["plugin", "gateway", "adapter"],
keywords_100=["plugin", "gateway"],
keywords_context=["adapter"],
)
assert entry.match_score("Das Plugin braucht einen Gateway-Fix") == 2
assert entry.match_score("Der Adapter ist kaputt") == 1
@@ -329,14 +461,14 @@ class TestRoomEntry:
def test_match_score_no_keywords(self):
entry = RoomEntry(
room_id="admin", name="DM Admin", theme="admin",
keywords=[], catch_all=True,
keywords_100=[], keywords_context=[], catch_all=True,
)
assert entry.match_score("irgendwas") == 0
def test_match_score_case_insensitive(self):
entry = RoomEntry(
room_id="r1", name="#Dev", theme="dev",
keywords=["Plugin", "GATEWAY"],
keywords_100=["Plugin"], keywords_context=["GATEWAY"],
)
assert entry.match_score("plugin gateway") == 2