fix: Reply-Quote-Stripping P0+P1 — verhindert Fehl-Routing bei zitierten Parent-Nachrichten

This commit is contained in:
Bernd (Hermes)
2026-07-23 06:34:46 +02:00
parent f9e4760f58
commit ce4789fe30
3 changed files with 629 additions and 17 deletions
+48 -17
View File
@@ -50,6 +50,9 @@ if _MULTIROOM():
from archivar.routing_counter import routing_counter
from archivar.routing_telemetry import routing_telemetry
# P0: Reply-Quote-Stripping — verhindert Fehl-Routing durch zitierte Parent-Nachrichten
from archivar.reply_quote_stripper import strip_reply_quote_for_routing
# ── Access __init__ symbols (plugin loaded as flat module, no relative imports) ──
import sys as _sys
_init_module = _sys.modules.get('rocketchat')
@@ -1587,7 +1590,28 @@ class RocketChatAdapter(BasePlatformAdapter):
else:
text = file_info_text
msg_obj["msg"] = text
# ── P0+P1: Reply-Quote-Stripping ──────────────────────────────────
# Verhindert Fehl-Routing durch zitierte Parent-Nachrichten.
# Zweistufig defensiv:
# 1. Attachments-basiert (Rocket.Chat Reply-Attachments)
# 2. Text-basiert (Markdown-Blockquotes, [quote]-Blöcke)
# routing_text (ohne Quote) → Archivar/Keyword-Matcher/classify_and_route
# display_text (mit Quote) → Agent-Dispatch + Conversation-History
routing_text, display_text, quote_stripped = strip_reply_quote_for_routing(
text, attachments
)
if quote_stripped:
routing_counter.increment('reply_quote_stripped')
logger.debug(
"[rocketchat] P0: Reply-Quote gestrippt — "
"routing_text=%s, display_text=%s",
routing_text[:80], display_text[:80],
)
# text bleibt display_text für Agent/History;
# routing_text wird für Archivar-Klassifikation verwendet.
# ── Ende P0+P1 ────────────────────────────────────────────────────
# Skip own messages (case-insensitive comparison)
filter_match = (username == (self.user or "").lower())
logger.debug("Filter check: username=%s, self.user=%s, match=%s", username, self.user, filter_match)
@@ -1627,7 +1651,7 @@ class RocketChatAdapter(BasePlatformAdapter):
# ── R5: Inline-Command-Erkennung (VOR Onboarding + Hermes-Dispatch) ──
# ::-Befehle und natürliche Hilfeseiten-Anfragen müssen VOR dem
# generischen Hermes-Agent-Onboarding erkannt werden.
inline_handled = await self._handle_inline_command(text, room_id, room_name, username)
inline_handled = await self._handle_inline_command(routing_text, room_id, room_name, username)
if inline_handled:
logger.debug("[rocketchat] R5: Inline-Command verarbeitet — skipping agent dispatch")
routing_counter.increment('inline_command_handled')
@@ -1636,13 +1660,15 @@ class RocketChatAdapter(BasePlatformAdapter):
# ── Ende R5 Inline-Command-Erkennung ────────────────────────────────
# ── B5v6.5: Conversation History — User-Nachricht aufzeichnen ──
# P1: display_text (mit Quote) für Agent/History, damit kein Kontext verloren geht
if _MULTIROOM() and self._conversation_history is not None:
await self._conversation_history.add(room_id, "user", text)
await self._conversation_history.add(room_id, "user", display_text)
# V8.1 P4: MultiRoomContext-Ring — Thema aus Nachricht extrahieren
if _MULTIROOM() and self._multiroom_context is not None:
# Extrahiere ein kurzes Topic aus der Nachricht (erste 80 Zeichen)
topic = text[:80].replace("\n", " ").strip()
# P1: routing_text für Topic-Extraktion (ohne Quote)
topic = routing_text[:80].replace("\n", " ").strip()
if topic:
await self._multiroom_context.add(room_id, room_name, topic)
@@ -2131,7 +2157,7 @@ class RocketChatAdapter(BasePlatformAdapter):
del self._recent_auto_routes[room_id]
# Dispatch im Ursprungsraum
await self._dispatch_agent(
text=text,
text=display_text,
room_id=room_id,
room_name=room_name,
room_type=room_type,
@@ -2231,7 +2257,7 @@ class RocketChatAdapter(BasePlatformAdapter):
target_room_name_disp = target_room_info.get("name", target_room_display)
target_room_type = target_room_info.get("type", "c")
await self._dispatch_agent(
text=text,
text=display_text,
room_id=target_room_id,
room_name=target_room_name_disp,
room_type=target_room_type,
@@ -2249,7 +2275,7 @@ class RocketChatAdapter(BasePlatformAdapter):
# Nur die Nachricht selbst wird — nach User-Entscheidung — kopiert.
category = ""
decision = None
is_auftrag = _is_auftrag(text)
is_auftrag = _is_auftrag(routing_text)
logger.debug("[rocketchat] ARCHIVAR-DEBUG: Entering Archivar block — room_type=%s, is_auftrag=%s, _MULTIROOM=%s, archivar=%s", room_type, is_auftrag, _MULTIROOM(), self.archivar is not None)
@@ -2363,7 +2389,8 @@ class RocketChatAdapter(BasePlatformAdapter):
)
routing_telemetry.flush()
# Kopiere Nachricht in den Zielraum
target_txt = f"*(Auftrag von @{username}, Topic-Sticky)*\n{text}"
# P1: display_text (mit Quote) für Zielraum-Kopie
target_txt = f"*(Auftrag von @{username}, Topic-Sticky)*\n{display_text}"
await self._post_message_with_retry(target_room_id, target_txt)
_CURRENT_ORIGIN_ROOM.set(target_room_id)
_THEMATIC_TARGET_ROOM.set(target_room_id)
@@ -2371,7 +2398,7 @@ class RocketChatAdapter(BasePlatformAdapter):
logger.info(
"[rocketchat] V8.3-5: Topic-Scoped Sticky active — "
"auto-routing '%s' → %s, topic='%s' (%.0fs remaining)",
text[:80], target_name, sticky_topic,
display_text[:80], target_name, sticky_topic,
pref["expires_at"] - time.time(),
)
# Dispatch direkt — kein Archivar
@@ -2379,7 +2406,7 @@ class RocketChatAdapter(BasePlatformAdapter):
target_room_name = target_room_info.get("name", target_name)
target_room_type = target_room_info.get("type", "c")
await self._dispatch_agent(
text=text,
text=display_text,
room_id=target_room_id,
room_name=target_room_name,
room_type=target_room_type,
@@ -2407,7 +2434,8 @@ class RocketChatAdapter(BasePlatformAdapter):
duration_ms=0.0,
)
routing_telemetry.flush()
target_txt = f"*(Auftrag von @{username}, Topic-Sticky)*\n{text}"
# P1: display_text (mit Quote) für Zielraum-Kopie
target_txt = f"*(Auftrag von @{username}, Topic-Sticky)*\n{display_text}"
await self._post_message_with_retry(target_room_id, target_txt)
_CURRENT_ORIGIN_ROOM.set(target_room_id)
_THEMATIC_TARGET_ROOM.set(target_room_id)
@@ -2415,7 +2443,7 @@ class RocketChatAdapter(BasePlatformAdapter):
logger.info(
"[rocketchat] V8.3-5: Topic-Scoped Sticky active — "
"auto-routing '%s' → %s, topic='%s' (%.0fs remaining)",
text[:80], target_name, sticky_topic,
display_text[:80], target_name, sticky_topic,
pref["expires_at"] - time.time(),
)
# Dispatch direkt — kein Archivar
@@ -2423,7 +2451,7 @@ class RocketChatAdapter(BasePlatformAdapter):
target_room_name = target_room_info.get("name", target_name)
target_room_type = target_room_info.get("type", "c")
await self._dispatch_agent(
text=text,
text=display_text,
room_id=target_room_id,
room_name=target_room_name,
room_type=target_room_type,
@@ -2453,7 +2481,8 @@ class RocketChatAdapter(BasePlatformAdapter):
# P0-FIX: is_dm=True bei DMs — der Router braucht das für den DM-Guard.
# room_type wird in L1579 aus self._room_info gelesen.
_is_dm = (room_type == "d")
decision = await self.archivar.classify_and_route(text, origin_room_id=room_id, is_dm=_is_dm)
# P0: routing_text (ohne Quote) für Klassifikation verwenden
decision = await self.archivar.classify_and_route(routing_text, origin_room_id=room_id, is_dm=_is_dm)
# V8.3.3: Telemetrie nach jeder Routing-Entscheidung flushen
routing_telemetry.flush()
if decision and decision.category:
@@ -2483,7 +2512,8 @@ class RocketChatAdapter(BasePlatformAdapter):
and hasattr(decision, 'target_room')
and decision.target_room):
self._pending_move_requests[room_id] = {
'text': text,
# P1: display_text (mit Quote) für Move-Request speichern
'text': display_text,
'origin_room': room_id,
'target_room': decision.target_room,
'target_name': getattr(
@@ -2575,7 +2605,8 @@ class RocketChatAdapter(BasePlatformAdapter):
room_id[:8],
)
# Kopiere Nachricht in den Zielraum
target_txt = f"*(Auftrag von @{username}, automatisch geroutet)*\n{text}"
# P1: display_text (mit Quote) für Zielraum-Kopie
target_txt = f"*(Auftrag von @{username}, automatisch geroutet)*\n{display_text}"
target_message_id = await self._post_message_with_retry(target_room_id, target_txt)
# Setze _CURRENT_ORIGIN_ROOM auf Zielraum → Agent antwortet dort
_CURRENT_ORIGIN_ROOM.set(target_room_id)
@@ -2585,7 +2616,7 @@ class RocketChatAdapter(BasePlatformAdapter):
logger.info(
"[rocketchat] V8.1 P1: Auto-Route '%s' → %s — "
"Agent dispatched in target room",
text[:80], target_name,
display_text[:80], target_name,
)
# V8.1.1 R2: Ring-GC bei Auto-Route — stale Topics aus Ursprungsraum entfernen
if _MULTIROOM() and self._multiroom_context is not None: