fix: V8.3.4 — Sticky-Ankündigung, Empty-Topic-Guard, #-Pflicht gelockert, Telemetrie im Sticky, Ankündigungsvielfalt

B1 (KRITISCH): Sticky-Auto-Route postet jetzt Ankündigung im Ursprungsraum
  - Beide Branches (mit/ohne Archivar) rufen _build_announcement() + post_message
  - V8.3-1 Log wird auch im Sticky-Pfad geschrieben

B2 (KRITISCH): Kein 100%-Keyword-Match → Sticky ignoriert
  - Neuer elif-Zweig: sticky_topic AND NOT current_topic → STAY
  - 'Kein Thema' ≠ 'gleiches Thema', Sticky greift nicht blind

B3 (KRITISCH): #-Pflicht entfernt, Verb-basierte Erkennung
  - 'Lass uns in Rocketchat-Plugin wechseln' (ohne #) → erkannt
  - Validierung via room-map.json (case-insensitive)
  - Negativ-Liste bleibt ('in den Raum', 'nach hause', etc.)

B4 (MITTEL): Telemetrie im Sticky-Auto-Route-Pfad
  - routing_telemetry.record('sticky', 1.0, 'route', ...) + flush()
  - In beiden Sticky-Branches

B5 (NIEDRIG): Ankündigungstext random.choice aus 5 Varianten
  - _ANNOUNCEMENT_VARIANTS + _build_announcement(target_display)
  - 5 von Andreas bestätigte Formulierungen

Tests: 25 neue Tests in test_v8_3_4.py, alle grün
540 passed, 15 pre-existing fails (1 env-var, 14 async)
This commit is contained in:
Bernd (Hermes)
2026-07-15 18:20:59 +02:00
parent 641b066db0
commit 7719575497
2 changed files with 456 additions and 28 deletions
+87 -28
View File
@@ -14,6 +14,7 @@ import json
import logging
import os
import queue
import random
import re
import socket
import ssl
@@ -622,6 +623,31 @@ def _is_auftrag(text: str) -> bool:
return False
# ── V8.3.4 B5: Ankündigungsvariationen ──────────────────────────────────
# 5 gleichbedeutende Formulierungen, zufällig ausgewählt.
# <target> wird durch den Raumnamen ersetzt.
_ANNOUNCEMENT_VARIANTS: list = [
"Lass uns das Thema in #{target} aufgreifen. Ich nehme das Thema mit.",
"Das passt besser in #{target} — ich bringe das Thema rüber. Kommst du mit? 👋",
"Wechseln wir kurz in #{target} für dieses Thema. Ich sehe dich dort.",
"Gehört eigentlich in #{target} — ich schiebe das Thema rüber und antworte dort. Wechsel du auch rüber?",
"Rüber in #{target} mit diesem Thema. Komme direkt nach. ✌️",
]
def _build_announcement(target_display: str) -> str:
"""Baue eine zufällige Ankündigung für den Ursprungsraum.
Args:
target_display: Raum-Name ohne führendes # (z.B. 'Rocketchat-Plugin').
Returns:
Zufällig ausgewählte Ankündigung mit eingesetztem Raumnamen.
"""
template = random.choice(_ANNOUNCEMENT_VARIANTS)
return template.replace("{target}", target_display)
class RocketChatAdapter(BasePlatformAdapter):
HEALTH_CHECK_INTERVAL = 60.0 # seconds
HEALTH_SILENT_LIMIT = 300.0 # 5 min without any server activity = force reconnect
@@ -1775,8 +1801,9 @@ class RocketChatAdapter(BasePlatformAdapter):
del self._recent_auto_routes[room_id]
# ── V8.3-4: Expliziter Raumwechsel (BEWUSSTE ENTSCHEIDUNG) ────────
# Pattern: 'lass uns in #<raum> wechseln', 'wechseln nach #<raum>', etc.
# V8.3.1 F3: #-Pflicht, Negativ-Liste, room-map-Validierung.
# V8.3.4 B3: Verb-basierte Erkennung statt #-Pflicht.
# Pattern: 'lass uns in <raum> wechseln', 'wechseln nach <raum>', etc.
# <raum> kann mit oder ohne # sein. Validierung via room-map.json.
_EXPLICIT_SWITCH_RE = re.compile(
r'(?:lass uns |gehen wir |wechsel(?:n)? )?(?:in |nach )?#?(\w[\w-]*)',
re.IGNORECASE,
@@ -1802,32 +1829,24 @@ class RocketChatAdapter(BasePlatformAdapter):
switch_match = _EXPLICIT_SWITCH_RE.search(text)
if switch_match:
switch_room_name = switch_match.group(1).lower()
# V8.3.1 F3: #-Pflicht — der Match muss ein # vor dem Raum-Namen haben
match_pos = switch_match.start(1)
hash_present = '#' in text[max(0, match_pos - 3):match_pos + len(switch_match.group(1)) + 1]
if not hash_present:
# V8.3.4 B3: Keine #-Pflicht mehr — stattdessen room-map-Validierung.
# Raum aus room-map.json auflösen (name → room_id), case-insensitive.
room_map = self.archivar._room_map
if room_map is not None:
for rid, entry in room_map.rooms.items():
entry_name = entry.name.lstrip('#').lower().replace(' ', '-')
if switch_room_name in entry_name or entry_name in switch_room_name:
target_room_id = rid
target_room_display = entry.name
break
# V8.3.4 B3: Nur auslösen wenn Raum in room-map.json existiert
if not target_room_id:
logger.debug(
"[rocketchat] V8.3-4: Kein # im Match — kein expliziter Raumwechsel"
"[rocketchat] V8.3-4: Raum '%s' nicht in room-map.json — "
"kein expliziter Raumwechsel",
switch_room_name,
)
is_explicit_switch = False
else:
# Raum aus room-map.json auflösen (name → room_id)
room_map = self.archivar._room_map
if room_map is not None:
for rid, entry in room_map.rooms.items():
entry_name = entry.name.lstrip('#').lower().replace(' ', '-')
if switch_room_name in entry_name or entry_name in switch_room_name:
target_room_id = rid
target_room_display = entry.name
break
# V8.3.1 F3: Nur auslösen wenn Raum in room-map.json existiert
if not target_room_id:
logger.debug(
"[rocketchat] V8.3-4: Raum '%s' nicht in room-map.json — "
"kein expliziter Raumwechsel",
switch_room_name,
)
is_explicit_switch = False
else:
is_explicit_switch = False
if is_explicit_switch and target_room_id and target_room_id != room_id:
@@ -1934,8 +1953,18 @@ class RocketChatAdapter(BasePlatformAdapter):
)
# Sticky NICHT löschen — könnte später wieder matchen
# Fall through to Archivar classification
elif sticky_topic and not current_topic:
# V8.3.4 B2: Kein 100%-Keyword-Match → Sticky darf NICHT greifen
# 'Kein Thema' ≠ 'gleiches Thema'. STAY, normale Auswertung.
logger.info(
"[rocketchat] V8.3.4 B2: No topic extracted from message — "
"sticky_topic='%s' but no 100%% keyword match → ignoring sticky, "
"falling through to Archivar",
sticky_topic,
)
# Fall through to Archivar classification
else:
# Thema matcht ODER kein Topic extrahierbar → Sticky greift
# Thema matcht → Sticky greift
# V8.1.1 R1: Catch-All-Guard — nicht in Catch-All-Raum auto-routen
if self.archivar is not None and self.archivar._room_map is not None:
target_entry = self.archivar._room_map.get_room_by_id(target_room_id)
@@ -1961,6 +1990,21 @@ class RocketChatAdapter(BasePlatformAdapter):
# Fall through to Archivar classification
else:
# Guards passed — auto-route
# V8.3.4 B1: Ankündigung im Ursprungsraum posten
target_display = target_name.lstrip('#') if target_name else "Zielraum"
announcement = _build_announcement(target_display)
await self._post_message_with_retry(room_id, announcement)
logger.info(
"[rocketchat] V8.3-1: Auto-Route announcement in origin room %s → %s",
room_id[:8], target_display,
)
# V8.3.4 B4: Telemetrie im Sticky-Pfad
routing_telemetry.record(
stage='sticky', score=1.0, decision='route',
target=target_room_id, origin=room_id,
duration_ms=0.0,
)
routing_telemetry.flush()
# Kopiere Nachricht in den Zielraum
target_txt = f"*(Auftrag von @{username}, Topic-Sticky)*\n{text}"
await self._post_message_with_retry(target_room_id, target_txt)
@@ -1991,6 +2035,21 @@ class RocketChatAdapter(BasePlatformAdapter):
return
else:
# Kein Archivar/RoomMap — direkt routen (Fallback)
# V8.3.4 B1: Ankündigung im Ursprungsraum posten
target_display = target_name.lstrip('#') if target_name else "Zielraum"
announcement = _build_announcement(target_display)
await self._post_message_with_retry(room_id, announcement)
logger.info(
"[rocketchat] V8.3-1: Auto-Route announcement in origin room %s → %s",
room_id[:8], target_display,
)
# V8.3.4 B4: Telemetrie im Sticky-Pfad
routing_telemetry.record(
stage='sticky', score=1.0, decision='route',
target=target_room_id, origin=room_id,
duration_ms=0.0,
)
routing_telemetry.flush()
target_txt = f"*(Auftrag von @{username}, Topic-Sticky)*\n{text}"
await self._post_message_with_retry(target_room_id, target_txt)
_CURRENT_ORIGIN_ROOM.set(target_room_id)
@@ -2109,9 +2168,9 @@ class RocketChatAdapter(BasePlatformAdapter):
# statt matched_keywords[0] — konsistent mit current_topic-Berechnung.
sticky_topic = room_name_to_topic(target_name) if target_name else ""
# ── V8.3-1: Ankündigung im Ursprungsraum VOR Zielraum-Post ──
# Andreas: 'Der Mensch wartet. Er schreibt nicht: Halt, wir bleiben hier.'
# V8.3.4 B5: Zufällige Variante aus 5 Formulierungen.
target_display = target_name.lstrip('#') if target_name else "Zielraum"
announcement = f"Lass uns das Thema in #{target_display} aufgreifen. Ich nehme das Thema mit."
announcement = _build_announcement(target_display)
await self._post_message_with_retry(room_id, announcement)
logger.info(
"[rocketchat] V8.3-1: Auto-Route announcement in origin room %s → %s",