Doc-Store: Messages in nicht abonnierte Räume werden im Filesystem abgelegt

Harte Durchsetzung: Der Archivar darf NUR in Räume routen, die der Bot
tatsächlich abonniert hat. Alles andere ist ein Architekturfehler.

Neu:
- doc_store.py: DocumentationStore — speichert unrouteable Nachrichten
  als Markdown mit YAML-Frontmatter unter data/archivar_docs/{category}/
- router.py: RoutingDecision.is_unrouteable + unrouteable_reason,
  MessageRouter.set_subscribed_room_ids(), Zielraum-Validierung
- adapter_integration.py: ArchivarMessageHandler.set_subscribed_room_ids(),
  classify_and_route() leitet bei is_unrouteable in doc_store um
- adapter.py: set_subscribed_room_ids() wird nach connect() mit den
  realen subscribed rooms gefüttert

Verhalten: Wenn jemand 'ALARM: Server down' in einem Raum schreibt,
den der Bot nicht abonniert hat, wird die Nachricht KLASSIFIZIERT,
aber NICHT geroutet — sie landet im doc_store mit Begründung.
This commit is contained in:
Hermes Agent
2026-06-03 22:06:30 +02:00
parent abe93cc2dc
commit 886e566e77
5 changed files with 247 additions and 1 deletions
+40
View File
@@ -47,6 +47,7 @@ from .classifier import (
MessageClassifier,
)
from .config import CONFIDENCE_THRESHOLD, OLLAMA_CONFIG
from .doc_store import DocumentationStore
from .router import MessageRouter, RoutingDecision
logger = logging.getLogger(__name__)
@@ -96,6 +97,7 @@ class ArchivarMessageHandler:
)
self._router = MessageRouter(self._classifier)
self._router.set_confidence_threshold(confidence_threshold)
self._doc_store = DocumentationStore()
self._initialized = False
# Statistiken (erweitert um Latenz + Fehler)
@@ -184,6 +186,20 @@ class ArchivarMessageHandler:
self._stats["total"] += 1
if decision.is_meta:
self._stats["meta"] += 1
elif decision.is_unrouteable:
self._stats["classified"] += 1
# Store in documentation store — message is classified but
# the target room is not in our subscription list
stored = self._doc_store.store(
text=text,
category=decision.category,
confidence=decision.confidence,
reason=decision.unrouteable_reason,
)
logger.info(
"Unrouteable [%s] stored in doc_store: %s",
decision.category, stored,
)
elif decision.is_fallback:
self._stats["fallback"] += 1
elif decision.is_catch_all:
@@ -238,6 +254,10 @@ class ArchivarMessageHandler:
decision = await self.classify_and_route(text)
# If unrouteable, don't try to forward — message is already in doc_store
if decision.is_unrouteable:
return decision
# Only forward if the target differs from origin
if decision.target_room and decision.target_room != origin_room_id:
prefix = decision.prefix or ""
@@ -339,3 +359,23 @@ class ArchivarMessageHandler:
"""
self._send_callback = callback
logger.debug("Send callback registered")
def set_subscribed_room_ids(self, room_ids: Optional[list]) -> None:
"""Set the bot's current subscribed room IDs for routing validation.
When set, the router will ONLY forward messages to rooms in this list.
Messages targeting unsubscribed rooms are stored in the
DocumentationStore instead.
Call this after adapter.connect() when the subscription list is ready.
Args:
room_ids: List of Rocket.Chat room IDs the bot is currently
subscribed to. Pass None to disable validation.
"""
self._router.set_subscribed_room_ids(room_ids)
@property
def doc_store(self) -> DocumentationStore:
"""Documentation store for unrouteable messages."""
return self._doc_store