feat: V7-3 "Jetzt" verblasst + Wiederherstellung — Drei-Zustands-Logik

- archivar_state.py: expire_stale_entries() mit 24h hard limit + adaptive Pause (Median×3)
- archivar_state.py: is_never_route prüft nur AKTIV — VERBLASST erlaubt Butler
- archivar_state.py: extend_never_route() + restore_never_route() (Stub für V7-4)
- router.py: Themenwechsel-Erkennung via Konversationshistorie (topic_hint Vergleich)
- router.py: expire_stale_entries VOR _check_never_route_topic in route()
- 24 neue Tests: 24h Verfall, adaptive Pause, AKTIV/VERBLASST, Verlängerung, Wiederherstellung
This commit is contained in:
Bernd (Hermes)
2026-07-13 16:00:05 +02:00
parent dff524d3e7
commit d20c06f02c
3 changed files with 821 additions and 10 deletions
+77 -1
View File
@@ -12,6 +12,7 @@ import asyncio
import logging
import re
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple
from .classifier import ClassificationResult, IntentClassifier
@@ -321,6 +322,19 @@ class BidirectionalRouter:
# auf Raum-Ebene prüfbar. Der Check erfolgt nach dem Match
# mit is_never_route(room_id, topic).
# ── V7-3: Verfall-Logik VOR never_route-Check ──────────────
# expire_stale_entries MUSS vor _check_never_route_topic laufen,
# damit VERBLASST-Einträge den Butler nicht mehr blocken.
expired = self._state.expire_stale_entries(
now=datetime.now(),
conversation_history=self._conversation_history,
)
if expired > 0:
logger.info(
"V7-3: %d never_route Einträge verfallen (AKTIV → VERBLASST)",
expired,
)
# ── Schritt 2: Meta-Fragen-Erkennung ──────────────────────────
if self._is_meta_question(text):
logger.info("Meta-question detected: %.80s...", text)
@@ -968,14 +982,23 @@ class BidirectionalRouter:
) -> bool:
"""Prüft ob das Thema des gematchten Raums in never_route ist.
V7-3: Führt auch Themenwechsel-Erkennung durch:
- Neues Thema ≠ altes Thema → altes never_route VERBLASST
- Neues Thema == altes Thema → never_route verlängern
- Aktualisiert topic_hint in conversation_history
Args:
origin_room_id: Der Ursprungsraum.
matched_room: Der gematchte Zielraum.
Returns:
True wenn das Thema in never_route ist → stay_in_room.
True wenn das Thema in never_route ist (AKTIV) → stay_in_room.
"""
topic = self._room_name_to_topic(matched_room.name)
# ── V7-3: Themenwechsel-Erkennung ──────────────────────────
self._detect_topic_change(origin_room_id, topic)
if self._state.is_never_route(origin_room_id, topic):
logger.info(
"P0-B: never_route match — room %s, topic '%s' → stay_in_room",
@@ -984,6 +1007,59 @@ class BidirectionalRouter:
return True
return False
def _detect_topic_change(self, room_id: str, new_topic: str) -> None:
"""V7-3: Erkenne Themenwechsel und aktualisiere never_route-Zustände.
- Holt altes Thema aus conversation_history
- Neues Thema ≠ altes Thema → altes never_route VERBLASST
- Neues Thema == altes Thema → never_route verlängern (extend)
- Aktualisiert topic_hint in conversation_history
Args:
room_id: Rocket.Chat Raum-ID.
new_topic: Das neue Thema (aus gematchtem Raum).
"""
if self._conversation_history is None:
return
try:
# Hole altes Thema (sync, da wir im async-Kontext sind,
# aber conversation_history._topic_hints ist ein dict)
old_topic = self._conversation_history._topic_hints.get(room_id)
except Exception:
old_topic = None
if old_topic is None:
# Kein altes Thema → nur topic_hint setzen
if self._conversation_history is not None:
try:
self._conversation_history._topic_hints[room_id] = new_topic
except Exception:
pass
return
if old_topic != new_topic:
# ── Themenwechsel: altes Thema → VERBLASST ──────────────
old_entry = self._state._never_route.get(room_id, {}).get(old_topic)
if old_entry and old_entry.get("state") == "AKTIV":
old_entry["state"] = "VERBLASST"
self._state.save()
logger.info(
"V7-3: Themenwechsel in %s — '%s' → '%s', "
"altes never_route '%s' AKTIV → VERBLASST",
room_id[:8], old_topic, new_topic, old_topic,
)
else:
# ── Gleiches Thema: never_route verlängern ──────────────
self._state.extend_never_route(room_id, new_topic)
# Aktualisiere topic_hint
if self._conversation_history is not None:
try:
self._conversation_history._topic_hints[room_id] = new_topic
except Exception:
pass
# ── Bidirektionales Routing ────────────────────────────────────────
async def move_message(self, source_room: str, target_room: str, message: str, message_id: str) -> bool: