fix: P-056 + P-057 + P-058 — Themenring-Normalisierung, stay_once neutral, Button 4 reject
P-056 (Themenring-Bug):
- _normalize_room_id() Hilfsmethode (strip+lower) in allen _never_route-Methoden
- Logger-Statements symmetrisch in set_never_route/is_never_route
- _detect_topic_change-Guard: last_extended < 5s → überspringe VERBLASST
P-057 (stay_once neutral):
- adapter.py: feedback_type='neutral' statt 'stay' für stay_once
- handle_feedback_response: 'neutral'-Zweig VOR update_keyword_weight
- update_keyword_weight: defensiver else-Zweig für 'neutral'/unbekannt
- Button-Label: 'generell hier bleiben' → 'jetzt hier bleiben'
P-058 (Button 4 reject):
- MOVE_REQUEST_TEMPLATE um Button 4 'Falsch geroutet' (reject) erweitert
- adapter.py: reject-Branch mit feedback_type='reject', KEIN set_never_route
- update_keyword_weight: 'reject'-Zweig — Zielraum ×0.2, Ursprungsraum +LR
- _routed_messages um origin_room erweitert
- DM-Ausschluss: Button 4 in DMs ausblenden (P-040)
- Choice-Normalisierung für '4'/'4️⃣' erweitert
Tests: 21 Unit-Tests (P-056: 9, P-057: 6, P-058: 6) — alle grün
This commit is contained in:
@@ -482,6 +482,15 @@ class ArchivarState:
|
||||
|
||||
# ── P0-B: never_route v7 (thematische Sticky-Einträge) ──────────────
|
||||
|
||||
@staticmethod
|
||||
def _normalize_room_id(room_id: str) -> str:
|
||||
"""P-056: Normalisiere room_id für konsistenten Key-Vergleich.
|
||||
|
||||
Verhindert Room-ID-Mismatch durch Whitespace/Case-Differenzen
|
||||
zwischen Schreiben (set_never_route) und Lesen (is_never_route).
|
||||
"""
|
||||
return room_id.strip().lower()
|
||||
|
||||
def set_never_route(self, room_id: str, topic: str) -> bool:
|
||||
"""Setze einen thematischen Sticky-Eintrag für einen Raum.
|
||||
|
||||
@@ -492,15 +501,24 @@ class ArchivarState:
|
||||
Returns:
|
||||
True wenn erfolgreich gespeichert, False bei Fehler.
|
||||
"""
|
||||
# P-056: room_id normalisieren (strip + lower) für konsistenten
|
||||
# Key-Vergleich mit is_never_route. Verhindert Room-ID-Mismatch
|
||||
# durch Whitespace/Case-Differenzen zwischen Schreiben und Lesen.
|
||||
room_id_normalized = room_id.strip().lower()
|
||||
now = datetime.now().isoformat()
|
||||
if room_id not in self._never_route:
|
||||
self._never_route[room_id] = {}
|
||||
self._never_route[room_id][topic] = {
|
||||
if room_id_normalized not in self._never_route:
|
||||
self._never_route[room_id_normalized] = {}
|
||||
self._never_route[room_id_normalized][topic] = {
|
||||
"state": "AKTIV",
|
||||
"timestamp": now,
|
||||
"last_extended": now,
|
||||
"extension_count": 0,
|
||||
}
|
||||
logger.info(
|
||||
"P-056 set_never_route: room_id=%s (normalized=%s), topic='%s', "
|
||||
"state=AKTIV, timestamp=%s",
|
||||
repr(room_id), repr(room_id_normalized), topic, now,
|
||||
)
|
||||
return self.save()
|
||||
|
||||
def is_never_route(self, room_id: str, topic: Optional[str] = None) -> bool:
|
||||
@@ -517,18 +535,38 @@ class ArchivarState:
|
||||
Returns:
|
||||
True wenn der Raum/das Thema in never_route ist UND state=AKTIV.
|
||||
"""
|
||||
# P-056: room_id normalisieren (strip + lower) — symmetrisch zu
|
||||
# set_never_route. Verhindert Room-ID-Mismatch.
|
||||
room_id_normalized = self._normalize_room_id(room_id)
|
||||
if not self._loaded:
|
||||
self.load()
|
||||
if room_id not in self._never_route:
|
||||
if room_id_normalized not in self._never_route:
|
||||
logger.info(
|
||||
"P-056 is_never_route: room_id=%s (normalized=%s) NOT in "
|
||||
"_never_route keys=%s",
|
||||
repr(room_id), repr(room_id_normalized),
|
||||
list(self._never_route.keys())[:5],
|
||||
)
|
||||
return False
|
||||
if topic is None:
|
||||
# Fallback: Raum hat Einträge → kein Sticky (Butler darf fragen)
|
||||
return False
|
||||
entry = self._never_route[room_id].get(topic)
|
||||
entry = self._never_route[room_id_normalized].get(topic)
|
||||
if entry is None:
|
||||
logger.info(
|
||||
"P-056 is_never_route: room_id=%s, topic='%s' NOT in "
|
||||
"_never_route[room] topics=%s",
|
||||
repr(room_id_normalized), topic,
|
||||
list(self._never_route[room_id_normalized].keys()),
|
||||
)
|
||||
return False
|
||||
# V7-3: Nur AKTIV blockt. VERBLASST → False (Butler darf fragen)
|
||||
return entry.get("state") == "AKTIV"
|
||||
result = entry.get("state") == "AKTIV"
|
||||
logger.info(
|
||||
"P-056 is_never_route: room_id=%s, topic='%s', state=%s → %s",
|
||||
repr(room_id_normalized), topic, entry.get("state"), result,
|
||||
)
|
||||
return result
|
||||
|
||||
def remove_never_route(self, room_id: str, topic: Optional[str] = None) -> bool:
|
||||
"""Entferne einen thematischen Sticky-Eintrag.
|
||||
@@ -541,6 +579,7 @@ class ArchivarState:
|
||||
Returns:
|
||||
True wenn erfolgreich entfernt, False bei Fehler.
|
||||
"""
|
||||
room_id = self._normalize_room_id(room_id)
|
||||
if room_id not in self._never_route:
|
||||
return True
|
||||
if topic is None:
|
||||
@@ -569,6 +608,7 @@ class ArchivarState:
|
||||
"""
|
||||
if not self._loaded:
|
||||
self.load()
|
||||
room_id = self._normalize_room_id(room_id)
|
||||
return self._never_route.get(room_id, {}).get(topic)
|
||||
|
||||
def has_never_route_entries(self, room_id: str) -> bool:
|
||||
@@ -585,6 +625,7 @@ class ArchivarState:
|
||||
"""
|
||||
if not self._loaded:
|
||||
self.load()
|
||||
room_id = self._normalize_room_id(room_id)
|
||||
return room_id in self._never_route and len(self._never_route[room_id]) > 0
|
||||
|
||||
def get_never_route_topics(self, room_id: str) -> Dict[str, dict]:
|
||||
@@ -598,6 +639,7 @@ class ArchivarState:
|
||||
"""
|
||||
if not self._loaded:
|
||||
self.load()
|
||||
room_id = self._normalize_room_id(room_id)
|
||||
return self._never_route.get(room_id, {}).copy()
|
||||
|
||||
def expire_stale_entries(
|
||||
@@ -769,6 +811,7 @@ class ArchivarState:
|
||||
Returns:
|
||||
True wenn erfolgreich verlängert, False wenn Eintrag nicht existiert.
|
||||
"""
|
||||
room_id = self._normalize_room_id(room_id)
|
||||
if room_id not in self._never_route:
|
||||
return False
|
||||
if topic not in self._never_route[room_id]:
|
||||
@@ -802,6 +845,7 @@ class ArchivarState:
|
||||
True wenn erfolgreich wiederhergestellt, False wenn Eintrag
|
||||
nicht existiert oder bereits AKTIV ist.
|
||||
"""
|
||||
room_id = self._normalize_room_id(room_id)
|
||||
if room_id not in self._never_route:
|
||||
return False
|
||||
if topic not in self._never_route[room_id]:
|
||||
@@ -900,14 +944,18 @@ class ArchivarState:
|
||||
|
||||
# Neue Methoden für das selbstlernende Keyword-Gewicht-System
|
||||
|
||||
def update_keyword_weight(self, keyword: str, room_id: str, feedback_type: str) -> bool:
|
||||
def update_keyword_weight(
|
||||
self, keyword: str, room_id: str, feedback_type: str,
|
||||
origin_room_id: str = "",
|
||||
) -> bool:
|
||||
"""
|
||||
Aktualisiert das Gewicht basierend auf Benutzerfeedback.
|
||||
|
||||
Args:
|
||||
keyword: Das betroffene Keyword
|
||||
room_id: Die Zielraum-ID
|
||||
feedback_type: "confirm", "stay", oder "never"
|
||||
feedback_type: "confirm", "stay", "never", "reject", oder "neutral"
|
||||
origin_room_id: Ursprungsraum-ID (nur für 'reject' benötigt)
|
||||
|
||||
Returns:
|
||||
True wenn das Gewicht erfolgreich aktualisiert wurde
|
||||
@@ -1027,12 +1075,80 @@ class ArchivarState:
|
||||
rejection_count=entry.rejection_count + 2, # Zählt als 2 negative Bestätigungen
|
||||
last_confirmation=entry.last_confirmation
|
||||
)
|
||||
|
||||
# Füge Keyword zu sticky_keywords für diesen Raum hinzu
|
||||
if keyword not in self._sticky_keywords:
|
||||
self._sticky_keywords[keyword] = []
|
||||
if room_id not in self._sticky_keywords[keyword]:
|
||||
self._sticky_keywords[keyword].append(room_id)
|
||||
|
||||
elif feedback_type == "reject":
|
||||
# P-058: Stark negatives Feedback (4️⃣ Falsch geroutet)
|
||||
# Semantik: Zielraum war FALSCH, Ursprungsraum war RICHTIG.
|
||||
# Zielraum: weight × 0.2 (drastische Reduktion, min 0.05)
|
||||
# Ursprungsraum: weight hochziehen wie bei confirm
|
||||
# rejection_count für Zielraum: +2
|
||||
if not origin_room_id:
|
||||
logger.warning(
|
||||
"update_keyword_weight: 'reject' ohne origin_room_id "
|
||||
"für keyword='%s', room='%s' — nur Zielraum reduziert",
|
||||
keyword, room_id[:8],
|
||||
)
|
||||
|
||||
# Zielraum-Gewicht drastisch reduzieren
|
||||
new_target_weight = entry.weight * 0.2
|
||||
new_target_weight = max(new_target_weight, 0.05)
|
||||
self._keyword_weights[keyword][room_id] = KeywordWeightEntry(
|
||||
weight=new_target_weight,
|
||||
last_updated=now,
|
||||
confirmation_count=entry.confirmation_count,
|
||||
rejection_count=entry.rejection_count + 2,
|
||||
last_confirmation=entry.last_confirmation
|
||||
)
|
||||
logger.info(
|
||||
"P-058 reject: Zielraum %s Gewicht %.3f → %.3f (×0.2), "
|
||||
"rejection_count +2",
|
||||
room_id[:8], entry.weight, new_target_weight,
|
||||
)
|
||||
|
||||
# Ursprungsraum-Gewicht erhöhen (wie confirm)
|
||||
if origin_room_id and origin_room_id != room_id:
|
||||
# Initialisiere Ursprungsraum-Eintrag falls nötig
|
||||
if origin_room_id not in self._keyword_weights[keyword]:
|
||||
self._keyword_weights[keyword][origin_room_id] = KeywordWeightEntry(
|
||||
weight=1.0 / 3.0,
|
||||
last_updated=now,
|
||||
confirmation_count=0,
|
||||
rejection_count=0,
|
||||
last_confirmation=None,
|
||||
)
|
||||
origin_entry = self._keyword_weights[keyword][origin_room_id]
|
||||
new_origin_weight = origin_entry.weight + (1 - origin_entry.weight) * learning_rate
|
||||
new_origin_weight = min(new_origin_weight, 0.95)
|
||||
self._keyword_weights[keyword][origin_room_id] = KeywordWeightEntry(
|
||||
weight=new_origin_weight,
|
||||
last_updated=now,
|
||||
confirmation_count=origin_entry.confirmation_count + 1,
|
||||
rejection_count=origin_entry.rejection_count,
|
||||
last_confirmation=now,
|
||||
)
|
||||
logger.info(
|
||||
"P-058 reject: Ursprungsraum %s Gewicht %.3f → %.3f "
|
||||
"(+learning_rate=%.2f)",
|
||||
origin_room_id[:8], origin_entry.weight,
|
||||
new_origin_weight, learning_rate,
|
||||
)
|
||||
|
||||
else:
|
||||
# P-057: Defensiv — 'neutral' oder unbekannter feedback_type
|
||||
# darf keine Daten mutieren. stay_once ('neutral') wird
|
||||
# bereits in handle_feedback_response abgefangen.
|
||||
logger.warning(
|
||||
"update_keyword_weight: unbekannter/neutraler feedback_type '%s' "
|
||||
"für keyword='%s', room='%s' — keine Mutation",
|
||||
feedback_type, keyword, room_id[:8],
|
||||
)
|
||||
return False
|
||||
|
||||
# Füge Keyword zu sticky_keywords für diesen Raum hinzu
|
||||
if keyword not in self._sticky_keywords:
|
||||
self._sticky_keywords[keyword] = []
|
||||
if room_id not in self._sticky_keywords[keyword]:
|
||||
self._sticky_keywords[keyword].append(room_id)
|
||||
|
||||
# Prüfe auf Promotion-Berechtigung
|
||||
self.check_promotion_eligibility(keyword, room_id)
|
||||
|
||||
Reference in New Issue
Block a user