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:
@@ -0,0 +1,133 @@
|
||||
"""
|
||||
Documentation store for Archivar — filesystem persistence for messages
|
||||
that cannot be routed because no room mapping exists or the target room
|
||||
is not in the bot's subscription list.
|
||||
|
||||
Writes classified-but-unrouteable messages to:
|
||||
~/.hermes/hermes-agent/data/archivar_docs/{category}/{timestamp}.md
|
||||
|
||||
Each stored document is a self-contained markdown file with YAML frontmatter
|
||||
containing classification metadata and a human-readable reason why routing
|
||||
was blocked.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
|
||||
def _get_hermes_agent_dir() -> Path:
|
||||
"""Resolve hermes-agent directory (works regardless of HERMES_HOME)."""
|
||||
hermes_home = os.environ.get("HERMES_HOME", os.path.expanduser("~/.hermes"))
|
||||
return Path(hermes_home) / "hermes-agent"
|
||||
|
||||
|
||||
class DocumentationStore:
|
||||
"""Filesystem store for unrouteable classified messages.
|
||||
|
||||
When the Archivar classifies a message but no valid target room exists
|
||||
(either because ROOM_MAPPING is empty for that category, or the resolved
|
||||
target room is not in the bot's current subscription list), the message
|
||||
is saved here instead of being silently dropped or forwarded to the
|
||||
wrong room.
|
||||
|
||||
Usage::
|
||||
|
||||
store = DocumentationStore()
|
||||
path = store.store(
|
||||
text="Server monitoring shows CPU spike",
|
||||
category="FAKT",
|
||||
confidence=0.92,
|
||||
reason="ARCHIVAR_FAKT_ROOM not configured",
|
||||
origin_room="6a19b0ff02aeecd36b1aa16c",
|
||||
)
|
||||
# → ~/.hermes/hermes-agent/data/archivar_docs/fakt/2026-06-03_21-30-00.md
|
||||
"""
|
||||
|
||||
def __init__(self, base_dir: Optional[Path] = None) -> None:
|
||||
if base_dir is None:
|
||||
base_dir = _get_hermes_agent_dir() / "data" / "archivar_docs"
|
||||
self._base_dir = Path(base_dir)
|
||||
|
||||
def store(
|
||||
self,
|
||||
text: str,
|
||||
category: str,
|
||||
confidence: float,
|
||||
reason: str = "",
|
||||
origin_room: str = "",
|
||||
) -> str:
|
||||
"""Store an unrouteable message as a markdown document.
|
||||
|
||||
Args:
|
||||
text: The original message text.
|
||||
category: Classification category (FAKT, FRAGE, ALARM).
|
||||
confidence: Classification confidence (0.0–1.0).
|
||||
reason: Human-readable reason why routing was blocked.
|
||||
origin_room: Rocket.Chat room ID where the message originated.
|
||||
|
||||
Returns:
|
||||
Absolute path to the stored file.
|
||||
"""
|
||||
category_dir = self._base_dir / category.lower()
|
||||
category_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
timestamp = time.strftime("%Y-%m-%d_%H-%M-%S")
|
||||
filename = f"{timestamp}.md"
|
||||
filepath = category_dir / filename
|
||||
|
||||
content = self._format_doc(text, category, confidence, reason, origin_room)
|
||||
filepath.write_text(content, encoding="utf-8")
|
||||
|
||||
return str(filepath)
|
||||
|
||||
@staticmethod
|
||||
def _format_doc(
|
||||
text: str,
|
||||
category: str,
|
||||
confidence: float,
|
||||
reason: str,
|
||||
origin_room: str,
|
||||
) -> str:
|
||||
"""Format a message as a documentation entry with YAML frontmatter."""
|
||||
timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
|
||||
return (
|
||||
f"---\n"
|
||||
f"category: {category}\n"
|
||||
f"confidence: {confidence:.3f}\n"
|
||||
f"timestamp: {timestamp}\n"
|
||||
f"origin_room: {origin_room}\n"
|
||||
f"reason: {reason}\n"
|
||||
f"---\n\n"
|
||||
f"# {category}: Unrouteable Message\n\n"
|
||||
f"**Reason:** {reason}\n\n"
|
||||
f"{text}\n"
|
||||
)
|
||||
|
||||
def list_stored(self, category: str = "") -> list[Path]:
|
||||
"""List stored documents, optionally filtered by category.
|
||||
|
||||
Args:
|
||||
category: Filter by category name (e.g. 'fakt', 'frage', 'alarm').
|
||||
Empty string returns all.
|
||||
|
||||
Returns:
|
||||
Sorted list of Path objects, newest first.
|
||||
"""
|
||||
if category:
|
||||
target = self._base_dir / category.lower()
|
||||
else:
|
||||
target = self._base_dir
|
||||
|
||||
if not target.exists():
|
||||
return []
|
||||
|
||||
return sorted(target.glob("**/*.md"), reverse=True)
|
||||
|
||||
@property
|
||||
def base_dir(self) -> Path:
|
||||
"""Base directory for stored documents."""
|
||||
return self._base_dir
|
||||
Reference in New Issue
Block a user