P1-Fix: build_help_text Längenbegrenzung + RoomMap.save Refuse-Guard

- build_help_text: Wenn Hilfe-Text > 4000 Zeichen, wird room_map_summary
  durch Platzhalter ersetzt. Verhindert HTTP 400 bei Rocket.Chat
  (Payload-Limit ~5000 Zeichen, ::hilfe mit 5 Räumen war 4571 Zeichen).
- RoomMap.save: Refuse-Guard prüft vor dem Schreiben, ob die existierende
  Disk-Datei mehr Räume enthält als die In-Memory-Map. Verweigert das
  Schreiben mit Warning wenn Disk > In-Memory. Verhindert Datenverlust
  beim Multiroom-Toggle (false→true schrieb leere Map).
- Tests: test_build_help_text_length_limit (max 4000 Zeichen),
  test_build_help_text_keeps_summary_when_short,
  TestRoomMapSaveRefuseGuard (4 Tests: refuse, allow-more, allow-equal,
  allow-no-disk).

Closes: Rainers Review P1-Fixes 1+2+3
This commit is contained in:
Bernd (Hermes)
2026-07-25 20:12:17 +02:00
parent b4ba67e80b
commit 1c53a9fe07
4 changed files with 307 additions and 2 deletions
+208
View File
@@ -516,3 +516,211 @@ class TestLLMLookup:
result = room_map.find_room_by_keywords("Wie funktioniert das BAT Trading-Signal?")
assert result is not None
assert result.name == "#BAT"
# ── P1-Fix: RoomMap.save Refuse-Guard ──────────────────────────────────────
class TestRoomMapSaveRefuseGuard:
"""P1-Fix: RoomMap.save verweigert Schreiben wenn Disk > In-Memory."""
def test_save_refuses_when_disk_has_more_rooms(self):
"""save() verweigert wenn Disk mehr Räume hat als In-Memory."""
import tempfile
from pathlib import Path
# Erstelle eine Disk-Datei mit 3 Räumen
disk_data = {
"version": 1,
"rooms": {
"r1": {"name": "#Room1", "theme": "t1", "description": "",
"keywords_100": [], "keywords_context": [], "catch_all": True,
"active_status_indikator": True,
"_stay_counts": {}, "_keyword_last_matched": {}},
"r2": {"name": "#Room2", "theme": "t2", "description": "",
"keywords_100": [], "keywords_context": [], "catch_all": False,
"active_status_indikator": True,
"_stay_counts": {}, "_keyword_last_matched": {}},
"r3": {"name": "#Room3", "theme": "t3", "description": "",
"keywords_100": [], "keywords_context": [], "catch_all": False,
"active_status_indikator": True,
"_stay_counts": {}, "_keyword_last_matched": {}},
},
"excluded_rooms": [],
"fact_store_path": "/tmp/test",
}
tmpdir = tempfile.TemporaryDirectory()
try:
map_path = Path(tmpdir.name) / "room-map.json"
with open(map_path, "w") as f:
json.dump(disk_data, f)
# Erstelle eine In-Memory-Map mit nur 1 Raum (kleiner als Disk)
small_data = {
"version": 1,
"rooms": {
"r1": {"name": "#Room1", "theme": "t1", "description": "",
"keywords_100": [], "keywords_context": [], "catch_all": True,
"active_status_indikator": True,
"_stay_counts": {}, "_keyword_last_matched": {}},
},
"excluded_rooms": [],
"fact_store_path": "/tmp/test",
}
small_map = RoomMap._from_dict(small_data)
# Versuche zu speichern — sollte verweigert werden
RoomMap.save(small_map, path=map_path)
# Disk sollte unverändert sein (3 Räume)
with open(map_path, "r") as f:
saved_data = json.load(f)
assert len(saved_data["rooms"]) == 3, (
f"Disk sollte 3 Räume haben, hat aber {len(saved_data['rooms'])}"
)
finally:
tmpdir.cleanup()
def test_save_allows_when_memory_has_more_rooms(self):
"""save() erlaubt Schreiben wenn In-Memory mehr Räume hat als Disk."""
import tempfile
from pathlib import Path
# Erstelle eine Disk-Datei mit 1 Raum
disk_data = {
"version": 1,
"rooms": {
"r1": {"name": "#Room1", "theme": "t1", "description": "",
"keywords_100": [], "keywords_context": [], "catch_all": True,
"active_status_indikator": True,
"_stay_counts": {}, "_keyword_last_matched": {}},
},
"excluded_rooms": [],
"fact_store_path": "/tmp/test",
}
tmpdir = tempfile.TemporaryDirectory()
try:
map_path = Path(tmpdir.name) / "room-map.json"
with open(map_path, "w") as f:
json.dump(disk_data, f)
# Erstelle eine In-Memory-Map mit 3 Räumen (größer als Disk)
big_data = {
"version": 1,
"rooms": {
"r1": {"name": "#Room1", "theme": "t1", "description": "",
"keywords_100": [], "keywords_context": [], "catch_all": True,
"active_status_indikator": True,
"_stay_counts": {}, "_keyword_last_matched": {}},
"r2": {"name": "#Room2", "theme": "t2", "description": "",
"keywords_100": [], "keywords_context": [], "catch_all": False,
"active_status_indikator": True,
"_stay_counts": {}, "_keyword_last_matched": {}},
"r3": {"name": "#Room3", "theme": "t3", "description": "",
"keywords_100": [], "keywords_context": [], "catch_all": False,
"active_status_indikator": True,
"_stay_counts": {}, "_keyword_last_matched": {}},
},
"excluded_rooms": [],
"fact_store_path": "/tmp/test",
}
big_map = RoomMap._from_dict(big_data)
# Speichern — sollte erlaubt sein
RoomMap.save(big_map, path=map_path)
# Disk sollte jetzt 3 Räume haben
with open(map_path, "r") as f:
saved_data = json.load(f)
assert len(saved_data["rooms"]) == 3, (
f"Disk sollte 3 Räume haben, hat aber {len(saved_data['rooms'])}"
)
finally:
tmpdir.cleanup()
def test_save_allows_when_equal_rooms(self):
"""save() erlaubt Schreiben wenn Disk und In-Memory gleich viele Räume haben."""
import tempfile
from pathlib import Path
disk_data = {
"version": 1,
"rooms": {
"r1": {"name": "#Room1", "theme": "t1", "description": "",
"keywords_100": [], "keywords_context": [], "catch_all": True,
"active_status_indikator": True,
"_stay_counts": {}, "_keyword_last_matched": {}},
"r2": {"name": "#Room2", "theme": "t2", "description": "",
"keywords_100": [], "keywords_context": [], "catch_all": False,
"active_status_indikator": True,
"_stay_counts": {}, "_keyword_last_matched": {}},
},
"excluded_rooms": [],
"fact_store_path": "/tmp/test",
}
tmpdir = tempfile.TemporaryDirectory()
try:
map_path = Path(tmpdir.name) / "room-map.json"
with open(map_path, "w") as f:
json.dump(disk_data, f)
# In-Memory hat auch 2 Räume (aber anderer Name)
mem_data = {
"version": 1,
"rooms": {
"r1": {"name": "#Room1_Renamed", "theme": "t1", "description": "",
"keywords_100": [], "keywords_context": [], "catch_all": True,
"active_status_indikator": True,
"_stay_counts": {}, "_keyword_last_matched": {}},
"r2": {"name": "#Room2", "theme": "t2", "description": "",
"keywords_100": [], "keywords_context": [], "catch_all": False,
"active_status_indikator": True,
"_stay_counts": {}, "_keyword_last_matched": {}},
},
"excluded_rooms": [],
"fact_store_path": "/tmp/test",
}
mem_map = RoomMap._from_dict(mem_data)
# Speichern — sollte erlaubt sein (gleiche Anzahl)
RoomMap.save(mem_map, path=map_path)
with open(map_path, "r") as f:
saved_data = json.load(f)
assert saved_data["rooms"]["r1"]["name"] == "#Room1_Renamed", (
"Umbenennung sollte persistiert werden"
)
finally:
tmpdir.cleanup()
def test_save_allows_when_disk_does_not_exist(self):
"""save() erlaubt Schreiben wenn Disk-Datei nicht existiert."""
import tempfile
from pathlib import Path
tmpdir = tempfile.TemporaryDirectory()
try:
map_path = Path(tmpdir.name) / "room-map.json"
# Keine Disk-Datei
mem_data = {
"version": 1,
"rooms": {
"r1": {"name": "#Room1", "theme": "t1", "description": "",
"keywords_100": [], "keywords_context": [], "catch_all": True,
"active_status_indikator": True,
"_stay_counts": {}, "_keyword_last_matched": {}},
},
"excluded_rooms": [],
"fact_store_path": "/tmp/test",
}
mem_map = RoomMap._from_dict(mem_data)
# Speichern — sollte erlaubt sein (keine Disk-Datei)
RoomMap.save(mem_map, path=map_path)
assert map_path.exists(), "Datei sollte erstellt worden sein"
finally:
tmpdir.cleanup()