fix(adapter): P-082 ContextVar token mismatch bei parallelen Nachrichten

Root Cause: _room_context hatte nur EINEN Slot pro chat_id. Bei
parallelen/versetzten Nachrichten desselben Users in verschiedenen
Räumen überschrieb Dispatch B den Eintrag von Dispatch A → send() für A
meldete fälschlich 'token mismatch / cross-contamination' und blockierte
die Antwort.

Fix:
- Keying auf (chat_id, room_id): jeder Dispatch bekommt eigenen Slot
- _get_room_context_origin matcht Token über alle Slots
- _priority0_origin_is_stale() ersetzt die Single-Slot-Pre-Read-Validierung
- invalidate_others=True für ersetzende Flows (Widerspruch, Move-Confirm,
  Clarify-Dispatch) löscht den stale Vorgänger-Slot
- _sweep_room_context() (DDP-Reader-Loop, 5s) räumt abgelaufene Slots auf
- adapter_integration.py: thematic_target-Write auf ContextVar umgestellt
  (war No-Op, kollidierte mit neuer Slot-Struktur)

Tests: 7 neue Unit-Tests (TestP082ParallelDispatchIsolation) + bestehende
Tests auf Slot-Struktur migriert. 1590 passed, 1 pre-existing failure
(test_connect_fresh_adapter_no_attribute_error, _crash_forensic_done).
This commit is contained in:
Bernd (Hermes)
2026-08-21 18:41:46 +02:00
parent d6e80328a0
commit c13e6efff7
4 changed files with 577 additions and 136 deletions
+16 -20
View File
@@ -974,45 +974,41 @@ class ArchivarMessageHandler:
True wenn das Feedback erfolgreich verarbeitet wurde, False bei Fehler
"""
try:
# ── 'stay': _room_context zurücksetzen ─────────────────────
# ── 'stay': thematic_target zurücksetzen ─────────────────────
# Wenn der User "Hier bleiben" klickt, muss der thematic_target
# geleert werden, damit der nächste Send nicht auf den falschen
# (Archivar-vorgeschlagenen) Zielraum fällt.
# P-082: _THEMATIC_TARGET_ROOM ist die tatsächlich gelesene Quelle
# (ContextVar). Der alte _room_context["thematic_target"]-Write war
# ein No-Op (Feld wurde nie gelesen) und würde mit der neuen
# (chat_id, room_id)-Slot-Struktur kollidieren.
if feedback_type == "stay" and username:
# Lazy import to break circular dependency with adapter.py
try:
from adapter import _room_context, _room_context_lock
from adapter import _THEMATIC_TARGET_ROOM
except ImportError:
from ..adapter import _room_context, _room_context_lock
from ..adapter import _THEMATIC_TARGET_ROOM
virtual_chat_id = f"user:{username}"
with _room_context_lock:
ctx = _room_context.setdefault(virtual_chat_id, {})
ctx["thematic_target"] = ""
# origin_room bleibt unverändert
_THEMATIC_TARGET_ROOM.set("")
logger.info(
"Stay-Feedback: _room_context[%s].thematic_target geleert",
virtual_chat_id,
"Stay-Feedback: _THEMATIC_TARGET_ROOM geleert für user:%s",
username,
)
# ── 'neutral': stay_once — kein Gewicht-Update, nur Cleanup ──
# P-057: stay_once meint semantisch 'Vorschlag war richtig,
# Ausführung nur diesmal hier'. Kein negatives Feedback.
# Tut: _room_context bereinigen, Cache-Eintrag löschen,
# Tut: thematic_target bereinigen, Cache-Eintrag löschen,
# update_keyword_weight ÜBERSPRINGEN.
if feedback_type == "neutral":
if username:
try:
from adapter import _room_context, _room_context_lock
from adapter import _THEMATIC_TARGET_ROOM
except ImportError:
from ..adapter import _room_context, _room_context_lock
virtual_chat_id = f"user:{username}"
with _room_context_lock:
ctx = _room_context.setdefault(virtual_chat_id, {})
ctx["thematic_target"] = ""
from ..adapter import _THEMATIC_TARGET_ROOM
_THEMATIC_TARGET_ROOM.set("")
logger.info(
"Neutral-Feedback: _room_context[%s].thematic_target geleert",
virtual_chat_id,
"Neutral-Feedback: _THEMATIC_TARGET_ROOM geleert für user:%s",
username,
)
# Cache-Eintrag löschen falls vorhanden
if message_id in self._routed_messages: