feat(archivar): Auto-Onboarding v2 — strukturiertes 4-Fragen-Interview pro Raum

Ersetzt den v1-Text-Flow ('Name = Thema') durch ein strukturiertes
Interview mit 4 Fragen pro fehlendem Raum:

1. theme — 'Worum geht es in diesem Raum? (1–2 Sätze)'
2. keywords_100 — 'Welche Begriffe treffen SICHER zu? (Sofort-Match)'
3. keywords_context — 'Welche Begriffe deuten auf das Thema hin? (Kontext-Match)'
4. catch_all — 'Soll dieser Raum der Catch-All sein? (Ja/Nein)'

Änderungen:
- adapter.py: _start_onboarding() sendet Buttons pro Raum
  [Projektraum einrichten] [Überspringen] [Später]
- adapter.py: _onboarding_state dict steuert Interview-Phasen
- adapter.py: _process_onboarding_response() verarbeitet Button-Klicks
  (ONBOARD:<room_id>::<action>) und Text-Antworten auf die 4 Fragen
- adapter.py: _ask_onboarding_question(), _onboarding_next_room(),
  _finish_onboarding() für den strukturierten Ablauf
- adapter.py: _process_incoming() erkennt ONBOARD:-Prefix und
  prüft Operator-Raum-Zugehörigkeit
- room_map.py: RoomMap.save() mit fcntl.flock File-Lock gegen
  parallele Schreibzugriffe
- room_map.py: add_or_update_room() akzeptiert keywords_100 und
  keywords_context getrennt (v2-Schema)
- room-map.schema.json: version minimum=2, keywords→keywords_100
  + keywords_context
- room-map.json: generische Platzhalter (öffentliches Repo)
- Tests: 4 neue v2-Tests (keyword-Trennung, File-Lock, reload,
  Single-Room v2-Felder), bestehende Tests auf v2-Schema migriert

Invarianten erhalten: Single-Room-Regel, DM-Ignorierung,
onboarding_active-Flag, kein Dauer-Check.
This commit is contained in:
Bernd (Hermes)
2026-06-15 19:39:04 +02:00
parent 6e02a12431
commit 2eb723fb63
5 changed files with 519 additions and 217 deletions
+11 -5
View File
@@ -2,14 +2,14 @@
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://hermes-agent.nousresearch.com/schemas/room-map.schema.json",
"title": "Archivar Room Map",
"description": "Schema for the Archivar room-map.json — the single source of truth for thematic message routing.",
"description": "Schema for the Archivar room-map.json — the single source of truth for thematic message routing. Version 2 splits keywords into keywords_100 (exact-match routing) and keywords_context (contextual routing).",
"type": "object",
"required": ["version", "rooms"],
"properties": {
"version": {
"type": "integer",
"description": "Schema version for future migrations.",
"minimum": 1
"minimum": 2
},
"rooms": {
"type": "object",
@@ -28,9 +28,15 @@
"description": "Theme / purpose of the room.",
"minLength": 1
},
"keywords": {
"keywords_100": {
"type": "array",
"description": "Keywords for keyword-based routing.",
"description": "Keywords for exact-match routing (high-confidence, low false-positive).",
"items": { "type": "string", "minLength": 1 },
"default": []
},
"keywords_context": {
"type": "array",
"description": "Keywords for contextual / semantic routing (broader match, may need disambiguation).",
"items": { "type": "string", "minLength": 1 },
"default": []
},
@@ -56,7 +62,7 @@
},
"keyword_overrides": {
"type": "object",
"description": "Admin-corrected keyword→room mappings (learned from !r overrides).",
"description": "Admin-corrected keyword→room mappings (learned from !r overrides). Optional.",
"additionalProperties": { "type": "string" }
}
},
+20 -4
View File
@@ -22,6 +22,7 @@ Usage::
from __future__ import annotations
import fcntl
import json
import logging
import os
@@ -403,8 +404,11 @@ class RoomMap:
@staticmethod
def save(room_map: 'RoomMap', path: Optional[Path] = None) -> None:
"""Speichert die RoomMap in eine JSON-Datei.
"""Speichert die RoomMap in eine JSON-Datei mit File-Lock.
Verwendet ``fcntl.flock`` (exklusiver Lock) um parallele
Schreibzugriffe zu verhindern. Blockiert maximal 5 Sekunden.
Args:
room_map: Die zu speichernde RoomMap-Instanz
path: Pfad zur JSON-Datei. Standardwert aus Umgebungsvariablen oder DEFAULT_MAP_PATH.
@@ -436,9 +440,21 @@ class RoomMap:
"catch_all": room_entry.catch_all
}
# Schreibe die Datei
# Schreibe die Datei mit exklusivem File-Lock
with open(path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
try:
fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except BlockingIOError:
logger.warning(
"RoomMap.save: file locked, retrying with blocking lock (5s timeout)"
)
fcntl.flock(f.fileno(), fcntl.LOCK_EX) # blocking
try:
json.dump(data, f, indent=2, ensure_ascii=False)
f.flush()
os.fsync(f.fileno())
finally:
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
@classmethod
def _from_dict(cls, data: dict) -> RoomMap:
+128 -6
View File
@@ -24,14 +24,15 @@ class TestAutoOnboarding(unittest.TestCase):
self.temp_dir = tempfile.TemporaryDirectory()
self.temp_path = Path(self.temp_dir.name) / "room-map.json"
# Mock-Daten für die RoomMap
# Mock-Daten für die RoomMap (v2 Schema)
self.test_data = {
"version": 1,
"version": 2,
"rooms": {
"catchall_room_id": {
"name": "#CatchAll",
"theme": "Catch-all room for everything else",
"keywords": [],
"keywords_100": [],
"keywords_context": [],
"catch_all": True
}
},
@@ -136,18 +137,20 @@ class TestAutoOnboarding(unittest.TestCase):
# Erstelle Testdaten mit bereits vorhandenen Räumen
test_data_with_rooms = {
"version": 1,
"version": 2,
"rooms": {
"catchall_room_id": {
"name": "#CatchAll",
"theme": "Catch-all room for everything else",
"keywords": [],
"keywords_100": [],
"keywords_context": [],
"catch_all": True
},
"room1_id": {
"name": "#Room1",
"theme": "First test room",
"keywords": ["test"],
"keywords_100": ["test"],
"keywords_context": [],
"catch_all": False
}
},
@@ -174,5 +177,124 @@ class TestAutoOnboarding(unittest.TestCase):
self.assertEqual(len(missing), 0)
class TestAutoOnboardingV2(unittest.TestCase):
"""v2-spezifische Tests: keywords_100/keywords_context Trennung, File-Lock."""
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.temp_path = Path(self.temp_dir.name) / "room-map.json"
self.test_data = {
"version": 2,
"rooms": {
"catchall_room_id": {
"name": "#CatchAll",
"theme": "Catch-all room",
"keywords_100": [],
"keywords_context": [],
"catch_all": True
}
},
"excluded_rooms": [],
"fact_store_path": str(self.temp_path.parent / "docs")
}
with open(self.temp_path, 'w') as f:
json.dump(self.test_data, f)
def tearDown(self):
self.temp_dir.cleanup()
def test_v2_add_or_update_room_separate_keywords(self):
"""Testet dass add_or_update_room keywords_100 und keywords_context trennt."""
room_map = RoomMap._from_dict(self.test_data)
room_map.add_or_update_room(
room_id="proj_room_id",
name="#Projektraum",
theme="Entwicklung des Plugins",
keywords_100=["plugin", "gateway"],
keywords_context=["architektur", "deployment"],
catch_all=False,
)
entry = room_map.rooms["proj_room_id"]
self.assertEqual(entry.keywords_100, ["plugin", "gateway"])
self.assertEqual(entry.keywords_context, ["architektur", "deployment"])
self.assertNotEqual(entry.keywords_100, entry.keywords_context)
def test_v2_single_room_auto_catch_all_fields(self):
"""Testet dass Single-Room-Regel v2-Felder korrekt setzt."""
room_map = RoomMap._from_dict(self.test_data)
# Simuliere: nur 1 relevanter Raum, nicht in Map
room_map.add_or_update_room(
room_id="single_room_id",
name="#SingleRoom",
theme="Automatisch zugewiesener Catch-All-Raum",
keywords_100=[],
keywords_context=[],
catch_all=True,
)
entry = room_map.rooms["single_room_id"]
self.assertTrue(entry.catch_all)
self.assertEqual(entry.keywords_100, [])
self.assertEqual(entry.keywords_context, [])
# Altes catch_all (catchall_room_id) sollte NICHT mehr catch_all sein
self.assertFalse(room_map.rooms["catchall_room_id"].catch_all)
def test_v2_save_with_file_lock(self):
"""Testet dass RoomMap.save() mit File-Lock funktioniert."""
room_map = RoomMap._from_dict(self.test_data)
room_map.add_or_update_room(
room_id="test_room_id",
name="#TestRaum",
theme="Test-Thema",
keywords_100=["test"],
keywords_context=["versuch"],
catch_all=False,
)
# Save sollte ohne Exception durchlaufen
RoomMap.save(room_map, self.temp_path)
# Reload und verifiziere
reloaded = RoomMap.load(self.temp_path)
entry = reloaded.rooms["test_room_id"]
self.assertEqual(entry.name, "#TestRaum")
self.assertEqual(entry.theme, "Test-Thema")
self.assertEqual(entry.keywords_100, ["test"])
self.assertEqual(entry.keywords_context, ["versuch"])
self.assertFalse(entry.catch_all)
def test_v2_reload_updates_in_memory(self):
"""Testet dass reload() die In-Memory-Daten aktualisiert."""
room_map = RoomMap._from_dict(self.test_data)
# Schreibe eine geänderte Map auf Disk
modified_data = dict(self.test_data)
modified_data["rooms"]["new_room_id"] = {
"name": "#NeuerRaum",
"theme": "Neues Thema",
"keywords_100": ["neu"],
"keywords_context": ["frisch"],
"catch_all": False,
}
with open(self.temp_path, 'w') as f:
json.dump(modified_data, f)
# Vor reload: new_room_id nicht in Map
self.assertNotIn("new_room_id", room_map.rooms)
# Reload
room_map.reload(self.temp_path)
# Nach reload: new_room_id in Map
self.assertIn("new_room_id", room_map.rooms)
entry = room_map.rooms["new_room_id"]
self.assertEqual(entry.keywords_100, ["neu"])
self.assertEqual(entry.keywords_context, ["frisch"])
if __name__ == '__main__':
unittest.main()