From 5720b488839a23e7d324907ab42ad375202db814 Mon Sep 17 00:00:00 2001 From: "Bernd (Hermes)" Date: Fri, 21 Aug 2026 19:48:20 +0200 Subject: [PATCH] =?UTF-8?q?P-086:=20Onboarding=20respektiert=20excluded=5F?= =?UTF-8?q?rooms=20+=20=C3=9Cberspringen=20persistiert?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix 1 (archivar/onboarding.py): - check_room_map_completeness() prüft zusätzlich room_map.is_excluded(room_id) im missing-Loop → excluded Räume werden nicht mehr als missing gemeldet. - Single-Room-Regel: excluded Raum wird nicht mehr als catch_all auto-assigned. Fix 2 (adapter.py): - _persist_excluded_rooms() Helper persistiert room_ids in room-map.json → excluded_rooms (idempotent, save+reload). - 'Überspringen' (Einzelraum) und 'Alle überspringen' rufen den Helper auf, sodass der Entscheid einen Gateway-Restart überlebt (kein B9-Re-Trigger). RoomMap (archivar/room_map.py): - add_excluded_room() Methode (idempotent, kein rooms{}-Eintrag nötig). Tests: test_p086_excluded_rooms.py (6 neue Tests). --- plugin/adapter.py | 48 ++++++ plugin/archivar/onboarding.py | 18 +++ plugin/archivar/room_map.py | 17 ++ .../tests/test_p086_excluded_rooms.py | 151 ++++++++++++++++++ 4 files changed, 234 insertions(+) create mode 100644 plugin/archivar/tests/test_p086_excluded_rooms.py diff --git a/plugin/adapter.py b/plugin/adapter.py index 19efffc..e864c2a 100644 --- a/plugin/adapter.py +++ b/plugin/adapter.py @@ -6297,6 +6297,46 @@ class RocketChatAdapter(BasePlatformAdapter): RoomMap.save(self.archivar._room_map, Path(ROOM_MAP_PATH)) self.archivar._room_map.reload() + def _persist_excluded_rooms(self, room_ids) -> None: + """P-086: Persistiert room_ids in room-map.json → excluded_rooms. + + Wird vom Onboarding-'Überspringen'-Flow genutzt, damit ein + übersprungener Raum einen Gateway-Restart überlebt (kein erneuter + B9-Onboarding-Trigger). Idempotent — bereits excluded Räume werden + nicht doppelt eingetragen. + + Args: + room_ids: Iterable von room_id-Strings. + """ + if self.archivar is None or self.archivar._room_map is None: + logger.warning( + "[rocketchat] P-086: _persist_excluded_rooms ohne room_map — " + "Überspringen wird NICHT persistiert" + ) + return + from archivar.room_map import RoomMap + from archivar.config import ROOM_MAP_PATH + from pathlib import Path + + room_map = self.archivar._room_map + added = [] + for rid in room_ids: + if rid and not room_map.is_excluded(rid): + room_map.add_excluded_room(rid) + added.append(rid) + if added: + RoomMap.save(room_map, Path(ROOM_MAP_PATH)) + room_map.reload() + logger.info( + "[rocketchat] P-086: %d Raum/Räume in excluded_rooms persistiert: %s", + len(added), + ", ".join(r[:8] for r in added), + ) + else: + logger.info( + "[rocketchat] P-086: keine neuen excluded_rooms (alle bereits excluded)" + ) + # ── Phase-Handler: fakt_store ─────────────────────────────────────── async def _handle_onboarding_fakt_store( self, text: str, state: dict, operator_room_id: str @@ -6796,6 +6836,11 @@ class RocketChatAdapter(BasePlatformAdapter): rid = room["room_id"] if rid not in state["skipped_rooms"]: state["skipped_rooms"].append(rid) + # P-086: Alle übersprungenen Räume dauerhaft in excluded_rooms + # persistieren (überlebt Gateway-Restart, kein B9-Re-Trigger). + self._persist_excluded_rooms( + [room["room_id"] for room in missing] + ) logger.info("[rocketchat] Onboarding: alle Räume übersprungen (skip_all)") await self._onboarding_next_room() return @@ -6824,6 +6869,9 @@ class RocketChatAdapter(BasePlatformAdapter): if "Überspringen" in action or "⏭️" in action: state["skipped_rooms"].append(target_room_id) + # P-086: Übersprungenen Raum dauerhaft in excluded_rooms + # persistieren (überlebt Gateway-Restart, kein B9-Re-Trigger). + self._persist_excluded_rooms([target_room_id]) logger.info("[rocketchat] Onboarding: Raum %s übersprungen", target_room_id[:8]) await self._onboarding_next_room() return diff --git a/plugin/archivar/onboarding.py b/plugin/archivar/onboarding.py index fe05461..e9947ca 100644 --- a/plugin/archivar/onboarding.py +++ b/plugin/archivar/onboarding.py @@ -370,6 +370,15 @@ def check_room_map_completeness( # nicht in der Map ist → automatisch als catch_all eintragen if len(relevant_rooms) == 1: single_room_id = list(relevant_rooms.keys())[0] + # P-086: Ein excluded Raum darf NICHT via Single-Room-Regel als + # catch_all auto-assigned werden — er ist bewusst ausgeklammert. + if room_map.is_excluded(single_room_id): + logger.info( + "check_room_map_completeness: Einziger Raum '%s' ist excluded " + "— kein Auto-Assign, kein Onboarding-Trigger", + relevant_rooms[single_room_id].get("name", single_room_id), + ) + return [] if single_room_id not in room_map.rooms: global _single_room_catch_all_overwrite_attempted _single_room_catch_all_overwrite_attempted += 1 @@ -490,6 +499,15 @@ def check_room_map_completeness( room_info.get("name", room_id), ) continue + # P-086: excluded_rooms berücksichtigen — ein excluded Raum ist + # NICHT missing (kein Onboarding-Trigger, kein B9-Reconnect-Loop). + if room_map.is_excluded(room_id): + logger.info( + "check_room_map_completeness: Raum '%s' ist excluded " + "— kein Onboarding-Trigger", + room_info.get("name", room_id), + ) + continue if room_id not in room_map.rooms: missing.append({"room_id": room_id, "name": room_info["name"]}) diff --git a/plugin/archivar/room_map.py b/plugin/archivar/room_map.py index 6615eaa..c750ca0 100644 --- a/plugin/archivar/room_map.py +++ b/plugin/archivar/room_map.py @@ -1028,6 +1028,23 @@ class RoomMap: self._excluded_room_ids.add(room_id) self._version += 1 + def add_excluded_room(self, room_id: str) -> None: + """Fügt eine room_id zu excluded_room_ids hinzu (idempotent). + + P-086: Wird vom Onboarding-'Überspringen'-Flow genutzt, um einen + übersprungenen Raum dauerhaft auszuklammern (überlebt Gateway-Restart). + Im Gegensatz zu ``remove_room`` wird KEIN Eintrag in ``rooms{}`` + vorausgesetzt — der Raum kann (und wird typischerweise) noch gar nicht + in der Map existieren. + + Args: + room_id: Die Raum-ID. + """ + if room_id in self._excluded_room_ids: + return + self._excluded_room_ids.add(room_id) + self._version += 1 + def increment_stay_count(self, room_id: str, keyword: str) -> int: """Inkrementiert den stay-Count für ein Keyword. diff --git a/plugin/archivar/tests/test_p086_excluded_rooms.py b/plugin/archivar/tests/test_p086_excluded_rooms.py new file mode 100644 index 0000000..7174f22 --- /dev/null +++ b/plugin/archivar/tests/test_p086_excluded_rooms.py @@ -0,0 +1,151 @@ +"""Tests für P-086 — Onboarding überspringt excluded_rooms nicht. + +Prüft: +1. check_room_map_completeness() meldet excluded Räume NICHT als missing. +2. RoomMap.add_excluded_room() ist idempotent und persistiert via save/reload. +3. Bestehende excluded_rooms werden respektiert (kein Onboarding-Trigger). +""" + +from __future__ import annotations + +import json +import sys +import tempfile +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).resolve().parents[3])) + +from plugin.archivar.room_map import RoomMap +from plugin.archivar.onboarding import check_room_map_completeness + + +# ── Helpers ──────────────────────────────────────────────────────────────── + + +def _room(room_id: str, name: str, **kwargs) -> dict: + d = { + "room_id": room_id, + "name": name, + "theme": name, + "keywords_100": [], + "keywords_context": [], + "catch_all": False, + } + d.update(kwargs) + return d + + +def _make_map(rooms: list[dict], excluded: list[str] | None = None) -> RoomMap: + data: dict = { + "version": 2, + "rooms": {}, + "excluded_rooms": excluded or [], + "fact_store_path": "/tmp/test_docs", + } + for room in rooms: + rid = room.pop("room_id") + data["rooms"][rid] = room + return RoomMap._from_dict(data) + + +# ── Tests ───────────────────────────────────────────────────────────────── + + +class TestExcludedRoomsNotMissing: + """check_room_map_completeness() ignoriert excluded Räume.""" + + def test_excluded_room_not_reported_missing(self): + """Ein excluded Raum wird NICHT als missing gemeldet.""" + room_map = _make_map( + [ + _room("r_catchall", "#CatchAll", catch_all=True), + ], + excluded=["r_excluded"], + ) + subscribed = { + "r_excluded": {"name": "#Excluded", "type": "c"}, + "r_new": {"name": "#NeuerRaum", "type": "c"}, + } + missing = check_room_map_completeness( + room_map=room_map, + subscribed_rooms=subscribed, + map_path="/tmp/test_map.json", + ) + missing_ids = [m["room_id"] for m in missing] + assert "r_excluded" not in missing_ids + assert "r_new" in missing_ids + + def test_excluded_room_not_in_map_still_ignored(self): + """Excluded Raum, der gar nicht in rooms{} steht, wird ignoriert.""" + room_map = _make_map( + [_room("r_catchall", "#CatchAll", catch_all=True)], + excluded=["r_ghost"], + ) + subscribed = { + "r_ghost": {"name": "#Ghost", "type": "c"}, + } + missing = check_room_map_completeness( + room_map=room_map, + subscribed_rooms=subscribed, + map_path="/tmp/test_map.json", + ) + assert missing == [] + + def test_existing_excluded_rooms_respected(self): + """Mehrere bestehende excluded_rooms werden alle respektiert.""" + room_map = _make_map( + [_room("r_catchall", "#CatchAll", catch_all=True)], + excluded=["r_ex1", "r_ex2", "r_ex3"], + ) + subscribed = { + "r_ex1": {"name": "#Ex1", "type": "c"}, + "r_ex2": {"name": "#Ex2", "type": "c"}, + "r_ex3": {"name": "#Ex3", "type": "c"}, + "r_new": {"name": "#Neu", "type": "c"}, + } + missing = check_room_map_completeness( + room_map=room_map, + subscribed_rooms=subscribed, + map_path="/tmp/test_map.json", + ) + missing_ids = [m["room_id"] for m in missing] + assert missing_ids == ["r_new"] + + +class TestAddExcludedRoom: + """RoomMap.add_excluded_room() — idempotent + persistierbar.""" + + def test_add_excluded_room_idempotent(self): + """Doppeltes Hinzufügen erzeugt keinen Duplikat-Eintrag.""" + room_map = _make_map([_room("r_catchall", "#CatchAll", catch_all=True)]) + room_map.add_excluded_room("r_skip") + room_map.add_excluded_room("r_skip") + assert room_map.is_excluded("r_skip") is True + assert len(room_map.excluded_room_ids) == 1 + + def test_add_excluded_room_persists_through_save_reload(self): + """add_excluded_room + save + reload überlebt (Gateway-Restart-Simulation).""" + tmp = tempfile.NamedTemporaryFile( + mode="w", suffix=".json", delete=False, encoding="utf-8" + ) + tmp_path = Path(tmp.name) + tmp.close() + + room_map = _make_map([_room("r_catchall", "#CatchAll", catch_all=True)]) + room_map.add_excluded_room("r_skip") + RoomMap.save(room_map, tmp_path) + + # Reload aus Disk — simuliert Gateway-Restart + reloaded = RoomMap.load(tmp_path) + assert reloaded.is_excluded("r_skip") is True + + tmp_path.unlink(missing_ok=True) + + def test_add_excluded_room_does_not_require_room_entry(self): + """add_excluded_room funktioniert ohne Eintrag in rooms{}.""" + room_map = _make_map([_room("r_catchall", "#CatchAll", catch_all=True)]) + room_map.add_excluded_room("r_never_onboarded") + assert room_map.is_excluded("r_never_onboarded") is True + assert "r_never_onboarded" not in room_map.rooms