4 Commits

Author SHA1 Message Date
Bernd (Hermes) c54b0aa133 test(test_06): add 3s delay between posts to avoid Interrupt-Recursion
- Added time.sleep(3.0) between post A and post B
- Hermes-Core serializes incoming messages per bot instance with
  depth-3 limit; two posts without delay trigger Interrupt-Recursion
- Updated docstring: 'innerhalb 1s' → 'sequenziell mit 3s Delay'
- Updated module docstring and function docstring

Ref: Rainer's review, Befund B-5
2026-07-29 11:04:24 +02:00
Bernd (Hermes) 7dd2e274c3 test(test_05): replace long_text with keyword-free architecture question
- Replaced 'Erkläre ausführlich die Architektur des Hermes Rocket.Chat
  Plugins...' with 'Erkläre ausführlich die Architektur eines
  Chatbot-Backends mit Multi-Room-Routing, ContextVar-basierter
  Task-Isolation und einem Push-basierten Eingangskanal.'
- New text contains NO 100%-keywords (no 'rocketchat', 'ddp',
  'websocket', 'plugin', 'gateway', 'hermes') — triggers real LLM
  answer instead of auto-route
- Updated docstring to note keyword-free requirement

Ref: Rainer's review, Vorschlag F-4
2026-07-29 11:03:46 +02:00
Bernd (Hermes) 5d5e349239 test(test_03): replace keyword text, remove hardcoded fallback, update docstring
- Keyword text: 'rocket chat plugin preflight briefing dokumentieren'
  (all words only in keywords_context, no 100%-keyword match)
- Removed hardcoded _DEFAULT_ROCKETCHAT_PLUGIN_ROOM fallback
- Added _get_env_room_or_skip_plugin_room() — skips cleanly if
  RC_LIVE_ROCKETCHAT_PLUGIN_ROOM not set
- Updated assertion from 'hermes' to 'rocket'/'plugin'
- Updated docstring and fail messages

Ref: Rainer's review .hermes/reviews/rainer/test-anpassung-2026-07-29.md
2026-07-29 11:03:25 +02:00
Bernd (Hermes) cbe68efe58 fix(B-1): keyword_matcher Strategy 3 requires ALL keyword words to match
Strategy 3 (Fuzzy/Levenshtein) previously returned True if ANY single
keyword word matched ANY text word. For multi-word keywords like
'hermes-gateway', this caused false 100% matches when only 'hermes'
appeared in text like 'hermes deployment pipeline'.

Fix: ALL keyword words must find a fuzzy match in the text. Single-word
keywords still work (one word must match). Words shorter than
MIN_FUZZY_LEN that reach Strategy 3 cause the keyword to NOT match
(they were already checked by Strategy 1/2).

Added 5 unit tests:
- test_strategy3_multiword_all_words_match
- test_strategy3_multiword_only_one_word_matches (the B-1 scenario)
- test_strategy3_multiword_no_words_match
- test_strategy3_single_word_still_works (regression)
- test_strategy3_multiword_fuzzy_typo

Applied to both match_keyword() and match_keyword_with_stems().

Ref: Rainer's review .hermes/reviews/rainer/test-anpassung-2026-07-29.md
2026-07-29 11:02:16 +02:00
5 changed files with 125 additions and 26 deletions
+44 -4
View File
@@ -298,20 +298,39 @@ def match_keyword(keyword: str, text: str) -> bool:
return True
# Strategy 3: Fuzzy match (Levenshtein)
# Only for words > MIN_FUZZY_LEN-1 characters to avoid false positives
# Check each word of the keyword against each word of the text
# For multi-word keywords: ALL keyword words must find a fuzzy match
# in the text. A single matching word is not enough — otherwise
# 'hermes-gateway' would falsely match 'hermes deployment pipeline'
# just because 'hermes' == 'hermes'.
# For single-word keywords: that one word must find a fuzzy match.
# Words shorter than MIN_FUZZY_LEN are skipped here (they were already
# checked by Strategy 1/2); if we reach Strategy 3 and a keyword word
# is too short for fuzzy, the keyword as a whole does NOT match.
kw_words = kw_norm.split()
text_words = text_no_hyphen.split()
all_fuzzy_matched = True
for kw_word in kw_words:
if len(kw_word) < MIN_FUZZY_LEN:
continue
# Short words can't be reliably fuzzy-matched.
# If we're here, Strategy 1/2 didn't match them either.
# For multi-word keywords, this means the keyword doesn't fully match.
all_fuzzy_matched = False
break
found = False
for text_word in text_words:
if len(text_word) < MIN_FUZZY_LEN:
continue
# Scale: distance 1 for 5-7 chars, distance 2 for 8+ chars
max_dist = FUZZY_MAX_DIST_SHORT if len(kw_word) <= 7 else FUZZY_MAX_DIST_LONG
if levenshtein_distance(kw_word, text_word) <= max_dist:
found = True
break
if not found:
all_fuzzy_matched = False
break
if all_fuzzy_matched and len(kw_words) > 0:
return True
# Strategy 3b: Phrase-level fuzzy match (concatenated, no spaces)
@@ -398,17 +417,38 @@ def match_keyword_with_stems(
return True
# Strategy 3: Fuzzy match (Levenshtein)
# For multi-word keywords: ALL keyword words must find a fuzzy match
# in the text. A single matching word is not enough — otherwise
# 'hermes-gateway' would falsely match 'hermes deployment pipeline'
# just because 'hermes' == 'hermes'.
# For single-word keywords: that one word must find a fuzzy match.
# Words shorter than MIN_FUZZY_LEN are skipped here (they were already
# checked by Strategy 1/2); if we reach Strategy 3 and a keyword word
# is too short for fuzzy, the keyword as a whole does NOT match.
kw_words = kw_norm.split()
text_words = text_no_hyphen.split()
all_fuzzy_matched = True
for kw_word in kw_words:
if len(kw_word) < MIN_FUZZY_LEN:
continue
# Short words can't be reliably fuzzy-matched.
# If we're here, Strategy 1/2 didn't match them either.
# For multi-word keywords, this means the keyword doesn't fully match.
all_fuzzy_matched = False
break
found = False
for text_word in text_words:
if len(text_word) < MIN_FUZZY_LEN:
continue
max_dist = FUZZY_MAX_DIST_SHORT if len(kw_word) <= 7 else FUZZY_MAX_DIST_LONG
if levenshtein_distance(kw_word, text_word) <= max_dist:
found = True
break
if not found:
all_fuzzy_matched = False
break
if all_fuzzy_matched and len(kw_words) > 0:
return True
# Strategy 3b: Phrase-level fuzzy match
@@ -260,6 +260,38 @@ class TestMatchKeyword:
"""'ddp' vs 'DDP-Client' → MATCH"""
assert match_keyword("ddp", "DDP-Client") is True
# ── Strategy 3: Multi-word fuzzy matching (B-1 fix) ───────────────
def test_strategy3_multiword_all_words_match(self):
"""Multi-word keyword: ALL words must fuzzy-match for Strategy 3."""
# 'hermes gateway' (normalized from 'hermes-gateway') vs
# 'hermes gateway' → both words match → MATCH
assert match_keyword("hermes-gateway", "hermes gateway") is True
def test_strategy3_multiword_only_one_word_matches(self):
"""Multi-word keyword: single word match is NOT enough for Strategy 3."""
# 'hermes gateway' (normalized from 'hermes-gateway') vs
# 'hermes deployment pipeline' → only 'hermes' matches, 'gateway' doesn't
# → NO MATCH (B-1 fix)
assert match_keyword("hermes-gateway", "hermes deployment pipeline") is False
def test_strategy3_multiword_no_words_match(self):
"""Multi-word keyword: no words match → NO MATCH."""
assert match_keyword("hermes-gateway", "unrelated text here") is False
def test_strategy3_single_word_still_works(self):
"""Single-word keywords still work with Strategy 3 (regression check)."""
# 'hermes' (single word) vs 'hermes' → MATCH
assert match_keyword("hermes", "hermes") is True
# 'hermes' vs 'hermes deployment' → MATCH (exact substring, Strategy 1)
assert match_keyword("hermes", "hermes deployment") is True
def test_strategy3_multiword_fuzzy_typo(self):
"""Multi-word keyword with minor typo in one word still matches."""
# 'hermes gateway' vs 'hermes gatewy' (typo in 'gateway')
# 'hermes' exact match (Strategy 1), 'gatewy' fuzzy-matches 'gateway' (dist=1)
assert match_keyword("hermes-gateway", "hermes gatewy") is True
# ═══════════════════════════════════════════════════════════════════════════
# match_keyword_exact() Tests
+28 -12
View File
@@ -10,7 +10,8 @@ Räume:
Test-Szenario (echter Button-Move-Request-Pfad):
1. /reset an source_room senden (sticky-topic clear)
2. Context-Keyword-Text posten, der einen Room-Match via keywords_context
triggert (z.B. "hermes deployment pipeline" → #Rocketchat-Plugin)
triggert (z.B. "rocket chat plugin preflight briefing dokumentieren"
→ #Rocketchat-Plugin). Kein 100%-Keyword enthalten.
3. Bot sendet Move-Request-Buttons in source_room (butler_mode=cognitive)
4. Button-Payload "MOVE_REQ:<source_room_id>::confirm_move" via REST posten
5. Warten auf kopierte Nachricht im Zielraum (#Rocketchat-Plugin)
@@ -52,8 +53,9 @@ from tests.live_rc.helpers.polling import (
logger = logging.getLogger(__name__)
# Room ID for #Rocketchat-Plugin from room-map.json (fallback if env var not set)
_DEFAULT_ROCKETCHAT_PLUGIN_ROOM = "6a19b0ff02aeecd36b1aa16c"
# Room ID for #Rocketchat-Plugin from room-map.json.
# No hardcoded fallback — must be set via RC_LIVE_ROCKETCHAT_PLUGIN_ROOM env var.
# If not set, the test skips cleanly (see _get_env_room_or_skip_plugin_room).
def _get_env_room(name: str) -> str:
@@ -64,6 +66,19 @@ def _get_env_room(name: str) -> str:
return val
def _get_env_room_or_skip_plugin_room() -> str:
"""Get the Rocketchat-Plugin room ID from env var, skip if not set."""
val = (os.getenv("RC_LIVE_ROCKETCHAT_PLUGIN_ROOM") or "").strip()
if not val:
pytest.skip(
"RC_LIVE_ROCKETCHAT_PLUGIN_ROOM not set — "
"cannot verify copied message in target room. "
"Set this env var to the room ID of #Rocketchat-Plugin "
"(or a test mirror room) to run this test."
)
return val
def _get_butler_mode(rc_client) -> str:
"""Read butler_mode from env var or detect from bot.
@@ -133,9 +148,8 @@ def test_03_move_request(
target_room = _get_env_room("RC_LIVE_TARGET_ROOM")
# Zielraum für die Kopie: #Rocketchat-Plugin (aus room-map.json)
rocket_chat_plugin_room = (
os.getenv("RC_LIVE_ROCKETCHAT_PLUGIN_ROOM") or _DEFAULT_ROCKETCHAT_PLUGIN_ROOM
).strip()
# Kein hardcoded Fallback — muss via RC_LIVE_ROCKETCHAT_PLUGIN_ROOM gesetzt sein.
rocket_chat_plugin_room = _get_env_room_or_skip_plugin_room()
posted_ids_source: list = []
posted_ids_target: list = []
@@ -170,10 +184,12 @@ def test_03_move_request(
# ── Step 1: Post context-keyword message in source room ─────────────
# Use a context keyword (NOT 100% keyword) to trigger butler mode.
# "hermes" + "deployment" are keywords_context for #Rocketchat-Plugin.
# 100% keywords like "rocketchat" would auto-route (move_request_sent=False).
# All words in this text are only in keywords_context for #Rocketchat-Plugin,
# none are in keywords_100 — so the bot sends a Move-Request instead of
# auto-routing. (B-1 fix ensures multi-word keywords_100 like 'hermes-gateway'
# don't falsely match on single words.)
keyword_text = (
"hermes deployment pipeline — bitte in den Rocketchat-Plugin-Raum routen"
"rocket chat plugin preflight briefing dokumentieren"
)
msg_id, source_cursor = post_test_message(
@@ -204,7 +220,7 @@ def test_03_move_request(
pytest.fail(
f"Bot did not send move-request buttons in source room within 30s. "
f"Check that butler_mode is not 'off' and the Archivar is running. "
f"Context keyword: 'hermes deployment pipeline'. Error: {e}"
f"Context keyword: 'rocket chat plugin preflight briefing dokumentieren'. Error: {e}"
)
move_req_text = move_req_msg.get("msg", "")
@@ -276,8 +292,8 @@ def test_03_move_request(
f"Copied message missing move prefix '(Auftrag von @...'. "
f"Got: '{copied_text[:200]}'"
)
assert "hermes" in copied_text.lower(), (
f"Copied message does not contain original keyword 'hermes'. "
assert "rocket" in copied_text.lower() or "plugin" in copied_text.lower(), (
f"Copied message does not contain original keyword 'rocket'/'plugin'. "
f"Got: '{copied_text[:200]}'"
)
+7 -3
View File
@@ -6,7 +6,7 @@ kontaminieren (F6 R24: _CURRENT_TASK_TOKEN + _set_room_context_origin).
Szenario:
1. Sende eine Nachricht in test_room, die eine lange Antwort triggert
(>10s LLM-Stream, z.B. "Erkläre ausführlich die Architektur...")
(>10s LLM-Stream, z.B. Architektur-Frage ohne 100%-Keywords)
2. Sofort eine zweite Nachricht in test-rc-target mit 100%-Keyword
("rocketchat ddp bug") — kurze Antwort erwartet
3. Warte auf Bot-Antworten in beiden Räumen (sequenziell)
@@ -97,9 +97,13 @@ def test_05_contextvar_isolation_parallel_load(
)
# ── Step 2: Lange-Antwort-Trigger in test_room posten ──────────────
# Die Frage enthält KEIN 100%-Keyword (kein 'rocketchat', 'ddp', 'websocket',
# 'plugin', 'gateway', 'hermes', etc.) — sie triggert eine echte LLM-Antwort
# statt Auto-Route. Thematisch passend für ContextVar-Isolation-Test.
long_text = (
"Erkläre ausführlich die Architektur des Hermes Rocket.Chat Plugins "
"inklusive aller Komponenten, Datenflüsse und Sicherheitsmechanismen."
"Erkläre ausführlich die Architektur eines Chatbot-Backends mit "
"Multi-Room-Routing, ContextVar-basierter Task-Isolation und einem "
"Push-basierten Eingangskanal."
)
long_msg_id, long_cursor = post_test_message(
rc_client, test_room_id, long_text,
+12 -5
View File
@@ -8,9 +8,9 @@ die Streams sich kontaminieren (F6 R24: _CURRENT_TASK_TOKEN).
Szenario:
1. /reset in test_room und test-rc-source
2. Cursor für beide Räume holen
3. Beide Nachrichten INNERHALB 1s posten:
- test_room: "lan subnetz maskieren" (keyword "lan" → #LAN-Dokumentation)
- test-rc-source: "rocketchat ddp bug" (keyword "rocketchat" → #Rocketchat-Plugin)
3. Beide Nachrichten sequenziell mit 3s Delay posten
(Interrupt-Recursion-Vermeidung: Hermes-Core serialisiert eingehende
Nachrichten pro Bot-Instanz mit Tiefe-3-Limit)
4. Auf Bot-Antworten in beiden Räumen warten
5. Verifiziere: Antwort_A referenziert LAN-Routing,
Antwort_B referenziert Rocketchat-Routing
@@ -76,7 +76,7 @@ def test_06_concurrent_isolation(
Steps:
1. /reset in beiden Räumen
2. Cursor holen
3. Beide Nachrichten innerhalb 1s posten
3. Beide Nachrichten sequenziell mit 3s Delay posten
4. Auf Bot-Antworten warten
5. Cross-Contamination-Check
"""
@@ -114,7 +114,10 @@ def test_06_concurrent_isolation(
cursor_test[:19], cursor_source[:19],
)
# ── Step 3: Beide Nachrichten INNERHALB 1s posten ──────────────────
# ── Step 3: Beide Nachrichten sequenziell mit 3s Delay posten ──────
# 3s Delay zur Interrupt-Recursion-Vermeidung:
# Hermes-Core serialisiert eingehende Nachrichten pro Bot-Instanz
# mit Tiefe-3-Limit. Zwei Posts ohne Delay triggern Interrupt-Recursion.
# Nachricht A: test_room — "lan subnetz maskieren" (keyword "lan" → #LAN-Dokumentation)
text_a = "lan subnetz maskieren"
msg_a_id, cursor_a = post_test_message(
@@ -122,6 +125,10 @@ def test_06_concurrent_isolation(
posted_ids_test, rate_limiter,
)
# 3s Delay: Kern-Antwort auf Nachricht A beginnen lassen,
# bevor Nachricht B gepostet wird (Interrupt-Recursion-Vermeidung)
time.sleep(3.0)
# Nachricht B: test-rc-source — "rocketchat ddp bug" (keyword "rocketchat" → #Rocketchat-Plugin)
text_b = "rocketchat ddp bug"
msg_b_id, cursor_b = post_test_message(