fix: P0 — ContextVar-Verlust im Drain-Task (Agent antwortete im Quellraum statt Target-Raum nach Auto-Route)

This commit is contained in:
Bernd (Hermes)
2026-07-20 13:19:15 +02:00
parent 01a799c57a
commit 15057373ae
2 changed files with 347 additions and 2 deletions
+32 -2
View File
@@ -2664,7 +2664,25 @@ class RocketChatAdapter(BasePlatformAdapter):
raw_message=msg_obj,
)
event.source = source
await self.handle_message(event)
# ── V8.1 P1: ContextVar-Snapshot für Drain-Task ──────────
# contextvars.copy_context() friert die aktuellen ContextVar-Werte
# (_CURRENT_ORIGIN_ROOM, _THEMATIC_TARGET_ROOM) ein. handle_message()
# erzeugt intern asyncio.create_task (Drain-Task), der ContextVars
# vom aufrufenden Task erbt. Ohne Snapshot kann der Drain-Task
# veraltete Werte einer vorherigen Nachricht erben (Race Condition
# bei zwei schnellen Nachrichten hintereinander).
ctx = contextvars.copy_context()
# V8.1 P1 diagnostic: log dispatch context
_origin = _CURRENT_ORIGIN_ROOM.get()
_target = _THEMATIC_TARGET_ROOM.get()
logger.info(
"V8.1 P1 dispatch: chat_id=%s → target_room=%s, "
"_CURRENT_ORIGIN_ROOM=%s, _THEMATIC_TARGET_ROOM=%s",
virtual_chat_id, _target if _target else room_id,
_origin[:8] if _origin else "",
_target[:8] if _target else "",
)
await ctx.run(self.handle_message, event)
except Exception as e:
logger.error("[rocketchat] handle_message error: %s", e)
# Fallback: Wenn COMMAND fehlschlägt (z.B. nach Gateway-Neustart
@@ -3490,7 +3508,19 @@ class RocketChatAdapter(BasePlatformAdapter):
raw_message=msg_obj,
)
event.source = source
await self.handle_message(event)
# ── V8.1 P1: ContextVar-Snapshot für Drain-Task ──────────
ctx = contextvars.copy_context()
# V8.1 P1 diagnostic: log dispatch context
_origin = _CURRENT_ORIGIN_ROOM.get()
_target = _THEMATIC_TARGET_ROOM.get()
logger.info(
"V8.1 P1 dispatch: chat_id=%s → target_room=%s, "
"_CURRENT_ORIGIN_ROOM=%s, _THEMATIC_TARGET_ROOM=%s",
virtual_chat_id, _target if _target else room_id,
_origin[:8] if _origin else "",
_target[:8] if _target else "",
)
await ctx.run(self.handle_message, event)
except Exception as e:
logger.error("[rocketchat] handle_message error: %s", e)
# Fallback: Wenn COMMAND fehlschlägt