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:
+31
-18
@@ -484,14 +484,16 @@ class TestSendPriority0TokenValidation:
|
||||
correct_room = "rc-test-automated"
|
||||
token = str(uuid.uuid4())
|
||||
|
||||
# Setup: ContextVar + _room_context mit gleichem Token
|
||||
# Setup: ContextVar + _room_context mit gleichem Token (P-082: Slot-Struktur)
|
||||
_CURRENT_ORIGIN_ROOM.set(correct_room)
|
||||
_CURRENT_TASK_TOKEN.set(token)
|
||||
_room_context.clear()
|
||||
_room_context[chat_id] = {
|
||||
"origin_room": correct_room,
|
||||
"task_token": token,
|
||||
"timestamp": "2026-07-28T12:00:00+00:00",
|
||||
correct_room: {
|
||||
"origin_room": correct_room,
|
||||
"task_token": token,
|
||||
"timestamp": "2026-07-28T12:00:00+00:00",
|
||||
}
|
||||
}
|
||||
|
||||
adapter = RocketChatAdapter.__new__(RocketChatAdapter)
|
||||
@@ -542,9 +544,11 @@ class TestSendPriority0TokenValidation:
|
||||
_CURRENT_TASK_TOKEN.set(auto_route_token)
|
||||
_room_context.clear()
|
||||
_room_context[chat_id] = {
|
||||
"origin_room": correct_room,
|
||||
"task_token": widerspruch_token, # ANDERES Token!
|
||||
"timestamp": "2026-07-28T12:00:00+00:00",
|
||||
correct_room: {
|
||||
"origin_room": correct_room,
|
||||
"task_token": widerspruch_token, # ANDERES Token!
|
||||
"timestamp": "2026-07-28T12:00:00+00:00",
|
||||
}
|
||||
}
|
||||
|
||||
adapter = RocketChatAdapter.__new__(RocketChatAdapter)
|
||||
@@ -587,9 +591,11 @@ class TestSendPriority0TokenValidation:
|
||||
_CURRENT_TASK_TOKEN.set("some-token")
|
||||
_room_context.clear()
|
||||
_room_context[chat_id] = {
|
||||
"origin_room": "irrelevant-old-room",
|
||||
# KEIN task_token — backward-compat: alter Eintrag vor P1-Fix
|
||||
"timestamp": "2026-07-28T12:00:00+00:00",
|
||||
"irrelevant-old-room": {
|
||||
"origin_room": "irrelevant-old-room",
|
||||
# KEIN task_token — backward-compat: alter Eintrag vor P1-Fix
|
||||
"timestamp": "2026-07-28T12:00:00+00:00",
|
||||
}
|
||||
}
|
||||
|
||||
adapter = RocketChatAdapter.__new__(RocketChatAdapter)
|
||||
@@ -639,9 +645,11 @@ class TestSendClarifyTokenValidation:
|
||||
_CURRENT_TASK_TOKEN.set(auto_route_token)
|
||||
_room_context.clear()
|
||||
_room_context[chat_id] = {
|
||||
"origin_room": correct_room,
|
||||
"task_token": widerspruch_token,
|
||||
"timestamp": "2026-07-28T12:00:00+00:00",
|
||||
correct_room: {
|
||||
"origin_room": correct_room,
|
||||
"task_token": widerspruch_token,
|
||||
"timestamp": "2026-07-28T12:00:00+00:00",
|
||||
}
|
||||
}
|
||||
|
||||
adapter = RocketChatAdapter.__new__(RocketChatAdapter)
|
||||
@@ -651,6 +659,7 @@ class TestSendClarifyTokenValidation:
|
||||
adapter._user_rooms = {chat_id: [correct_room]}
|
||||
adapter._find_fallback_room_for_user = MagicMock(return_value=None)
|
||||
adapter._rooms_with_pending_questions = set()
|
||||
adapter._presence_engine = None # P-067: send_clarify prüft _presence_engine.is_enabled()
|
||||
|
||||
with patch("plugin.adapter._MULTIROOM", return_value=False):
|
||||
result = asyncio.run(
|
||||
@@ -707,9 +716,11 @@ class TestAutoRouteWiderspruchEndToEnd:
|
||||
|
||||
# ── Phase 2: Widerspruch ─────────────────────────────────────
|
||||
# Widerspruch erkannt → Thema zurückholen
|
||||
# _CURRENT_ORIGIN_ROOM zurück auf Ursprungsraum
|
||||
# _CURRENT_ORIGIN_ROOM zurück auf Ursprungsraum.
|
||||
# P-082: invalidate_others=True — Widerspruch ersetzt die Auto-Route,
|
||||
# der stale Auto-Route-Slot (Drohnenkurs) wird gelöscht.
|
||||
_CURRENT_ORIGIN_ROOM.set(origin_room)
|
||||
widerspruch_token = _set_room_context_origin(chat_id, origin_room)
|
||||
widerspruch_token = _set_room_context_origin(chat_id, origin_room, invalidate_others=True)
|
||||
|
||||
# ── Phase 3: _dispatch_agent (Widerspruch) ───────────────────
|
||||
# _dispatch_agent setzt _room_context erneut (mit neuem Token)
|
||||
@@ -777,9 +788,11 @@ class TestPriority0MismatchTelemetry:
|
||||
_CURRENT_TASK_TOKEN.set(auto_route_token)
|
||||
_room_context.clear()
|
||||
_room_context[chat_id] = {
|
||||
"origin_room": correct_room,
|
||||
"task_token": widerspruch_token,
|
||||
"timestamp": "2026-07-28T12:00:00+00:00",
|
||||
correct_room: {
|
||||
"origin_room": correct_room,
|
||||
"task_token": widerspruch_token,
|
||||
"timestamp": "2026-07-28T12:00:00+00:00",
|
||||
}
|
||||
}
|
||||
|
||||
# Snapshot counter before
|
||||
|
||||
Reference in New Issue
Block a user