M13: Vollständige Garbage Collection — FAISS-Rebuild + KeywordBooster-Reset + ArchivarState-Bereinigung + Onboarding-StayTracker-Cleanup

Erweitert _sync_room_map_with_membership() um die von Andreas geforderten
Bereinigungsschritte nach room-map.json-GC:

1. FAISS-Index: rebuild + save (war bereits in M13, jetzt robuster)
2. KeywordBooster: cleanup_orphaned_rooms() — baut _keyword_to_rooms
   Mapping aus bereinigter RoomMap neu
3. ArchivarState: cleanup_orphaned_rooms() — entfernt stay_decision,
   keyword_weights, sticky_keywords, manual_overrides, promotion_candidates
   für gelöschte Räume
4. Onboarding _stay_tracker: entfernt Einträge für gelöschte Räume

Reihenfolge im Gateway-Start (adapter.py connect()):
  DDP connect → _sync_room_map_with_membership() → DDP subscribe →
  Archivar init → Auto-Onboarding

Alle neuen Methoden sind idempotent und mit Tests abgedeckt:
- test_archivar_state.py: +8 Tests (cleanup_orphaned_rooms)
- test_booster.py: +4 Tests (cleanup_orphaned_rooms)
This commit is contained in:
Bernd (Hermes)
2026-07-12 15:49:17 +02:00
parent 63ca3bed96
commit 3179ce8b7b
5 changed files with 526 additions and 1 deletions
+64
View File
@@ -538,3 +538,67 @@ class TestConflictEdgeCases:
result_after = booster.boost("plugin bug", scores)
assert result_after[0][0] == "room_a"
assert result_after[0][1] == pytest.approx(0.80) # unverändert
# ═══════════════════════════════════════════════════════════════════════════
# M13: cleanup_orphaned_rooms Tests
# ═══════════════════════════════════════════════════════════════════════════
class TestBoosterCleanup:
"""M13: Tests für KeywordBooster.cleanup_orphaned_rooms()."""
@pytest.fixture
def booster(self) -> KeywordBooster:
return KeywordBooster(_make_booster_room_map())
def test_cleanup_rebuilds_mapping(self, booster: KeywordBooster) -> None:
"""cleanup_orphaned_rooms() baut das Mapping aus der aktuellen RoomMap neu."""
before = len(booster._keyword_to_rooms)
# Entferne room_b aus der RoomMap (simuliert GC)
del booster._room_map._rooms["room_b"]
# Cleanup mit nur noch room_a + room_catchall als gültig
after = booster.cleanup_orphaned_rooms({"room_a", "room_catchall"})
# Mapping sollte kleiner sein (room_b's unique keywords sind weg)
assert after < before
# "bat" war unique für room_b → sollte nicht mehr im Mapping sein
assert "bat" not in booster._keyword_to_rooms
# "plugin" war unique für room_a → sollte noch da sein
assert "plugin" in booster._keyword_to_rooms
assert booster._keyword_to_rooms["plugin"] == {"room_a"}
def test_cleanup_no_orphans_returns_same_count(self, booster: KeywordBooster) -> None:
"""Keine orphaned Räume → Mapping bleibt gleich groß."""
before = len(booster._keyword_to_rooms)
after = booster.cleanup_orphaned_rooms({"room_a", "room_b", "room_catchall"})
assert after == before
def test_cleanup_all_rooms_removed_returns_zero(self, booster: KeywordBooster) -> None:
"""Alle nicht-catch-all Räume entfernt → leeres Mapping."""
# Entferne ALLE nicht-catch-all Räume
del booster._room_map._rooms["room_a"]
del booster._room_map._rooms["room_b"]
# room_catchall bleibt (catch_all=True, wird von _build_mapping ignoriert)
after = booster.cleanup_orphaned_rooms({"room_catchall"})
assert after == 0
assert len(booster._keyword_to_rooms) == 0
def test_cleanup_then_boost_still_works(self, booster: KeywordBooster) -> None:
"""Nach Cleanup: Boost funktioniert korrekt mit bereinigtem Mapping."""
# Entferne room_b
del booster._room_map._rooms["room_b"]
booster.cleanup_orphaned_rooms({"room_a", "room_catchall"})
scores: list[tuple[str, float]] = [
("room_a", 0.80),
("room_catchall", 0.60),
]
result = booster.boost("plugin bug", scores)
# "plugin" ist unique für room_a → +0.10 (tatsächlicher Boost-Wert)
assert result[0][0] == "room_a"
assert result[0][1] == pytest.approx(0.90)