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
+69 -1
View File
@@ -85,6 +85,13 @@ class RoutingDecision:
is_meta: bool = False
"""True, wenn die Nachricht als Meta-Frage erkannt wurde."""
is_unrouteable: bool = False
"""True, wenn der Zielraum nicht in den subscribed rooms des Bots ist
und die Nachricht stattdessen im DocumentationStore abgelegt wird."""
unrouteable_reason: str = ""
"""Grund, warum kein Routing möglich war (nur bei is_unrouteable=True)."""
prefix: str = ""
"""Optionaler Präfix-Text für die weitergeleitete Nachricht (z.B. [FALLBACK])."""
@@ -104,6 +111,7 @@ class MessageRouter:
Usage::
router = MessageRouter(classifier)
router.set_subscribed_room_ids(["6a19...", "6a1f..."])
decision = await router.route("Wie konfiguriere ich das Plugin?")
# decision.target_room → "6a19b0ff02aeecd36b1aa16c" (#Rocketchat-Plugin)
# decision.category → "FRAGE"
@@ -117,6 +125,7 @@ class MessageRouter:
"""
self._classifier = classifier
self._confidence_threshold = CONFIDENCE_THRESHOLD
self._subscribed_room_ids: Optional[set] = None
# ── Public API ──────────────────────────────────────────────────────
@@ -198,7 +207,7 @@ class MessageRouter:
result.category, text
)
return RoutingDecision(
decision = RoutingDecision(
category=result.category,
target_room=target_room,
confidence=result.confidence,
@@ -206,6 +215,25 @@ class MessageRouter:
project=project,
)
# ── Subscription-Validierung ──────────────────────────────────
self._check_unrouteable(decision)
return decision
def _check_unrouteable(self, decision: RoutingDecision) -> None:
"""Validate target_room against subscribed IDs.
If the target room is not in the bot's subscription list, mark the
decision as unrouteable so the caller can store the message in the
DocumentationStore instead of forwarding it.
"""
reason = self._validate_target_room(decision.category, decision.target_room)
if reason:
decision.is_unrouteable = True
decision.unrouteable_reason = reason
logger.warning(
"Unrouteable [%s]: %s", decision.category, reason
)
# ── Room Resolution ────────────────────────────────────────────────
def _resolve_room(self, category: str, text: str = "") -> str:
@@ -333,3 +361,43 @@ class MessageRouter:
raise ValueError(f"Confidence threshold must be in [0.0, 1.0], got {value}")
self._confidence_threshold = value
logger.info("Confidence threshold set to %.2f", value)
def set_subscribed_room_ids(self, room_ids: Optional[list]) -> None:
"""Set the bot's current subscribed room IDs for validation.
When set, the router will ONLY route to rooms that are in this list.
Messages targeting unsubscribed rooms are marked as ``is_unrouteable``
and should be stored in the DocumentationStore instead.
Args:
room_ids: List of Rocket.Chat room IDs the bot is subscribed to.
Pass None or empty list to disable validation.
"""
if room_ids:
self._subscribed_room_ids = set(room_ids)
logger.debug(
"Router subscribed IDs set: %d rooms", len(self._subscribed_room_ids)
)
else:
self._subscribed_room_ids = None
def _validate_target_room(self, category: str, room_id: str) -> str:
"""Validate target room against subscribed IDs.
Returns:
Empty string if valid. Human-readable reason if blocked.
"""
if not room_id:
return f"no room configured for category '{category}'"
if self._subscribed_room_ids is None:
return "" # validation disabled
if room_id not in self._subscribed_room_ids:
return (
f"target room {room_id[:8]}... for category '{category}' "
f"is not in bot's subscription list "
f"({len(self._subscribed_room_ids)} subscribed rooms)"
)
return ""