P1 Fix: OutboundGate Stale-Entry + user_map Fallback — 3 Bugs
B3: ContextVar _CURRENT_SESSION_ID (adapter.py) - Neue ContextVar _CURRENT_SESSION_ID mit default='' - Setzung in _dispatch_agent via uuid4().hex (Fallback) - Lesung in send() und send_clarify() für release() B1: _TurnHandle.owner_session_id (outbound_gate.py) - _TurnHandle dataclass: neues Feld owner_session_id: str = '' - try_acquire() neuer Parameter owner_session_id: str = '' - release() Session-ID-Fallback: wenn task_token leer aber owner_session_id existiert → release OK (Stream-Retry in anderem asyncio-Task) - 4 try_acquire-Aufrufstellen in adapter.py übergeben _CURRENT_SESSION_ID.get() B2: allow_proactive_user_map Opt-In (adapter.py) - Plugin-Config-Flag allow_proactive_user_map: false (default) - Priority 2 (_user_rooms[-1]) und _find_fallback_room_for_user NUR nutzen wenn allow_proactive_user_map=True - metadata['allow_proactive']=True überschreibt Config pro-Aufruf - BEIDE Guess-Pfade geschützt: send() und send_clarify() B+: Sweep-Intervall + max_entry_age reduziert - _gate_sweep_loop: 30s → 5s - OutboundGate default max_entry_age_s: 60.0 → 10.0 - adapter.py OutboundGate-Instanziierung: max_entry_age_s=10.0 Tests: 16 neue Tests in test_session_isolation.py - test_session_id_propagation, test_session_id_no_core_arg_fallback - test_try_acquire_records_owner_session_id - test_release_respects_owner_session_id - test_release_token_mismatch_no_session_match_skips - test_release_token_match_no_session_needed - test_user_map_blocked_by_default - test_user_map_allowed_with_optin - test_fallback_room_respects_optin - test_allow_proactive_metadata_override - test_max_entry_age_default_is_10s - test_sweep_removes_stale_after_10s - test_session_id_isolates_concurrent_sessions - test_same_session_multi_turn_allowed Bestehende Tests: 2 Tests in test_v8_1_p1_contextvar.py angepasst (allow_proactive_user_map=True für _user_rooms-Fallback) Gesamtsuite: 1381 passed, 4 skipped, 1 xfailed
This commit is contained in:
+64
-8
@@ -51,6 +51,13 @@ _CURRENT_TASK_TOKEN: contextvars.ContextVar[str] = contextvars.ContextVar(
|
||||
"_rocketchat_task_token", default=""
|
||||
)
|
||||
|
||||
# B3: ContextVar für die aktuelle Session-ID — wird in _dispatch_agent gesetzt,
|
||||
# in send() und send_clarify() für release() verwendet.
|
||||
# _CURRENT_TASK_TOKEN bleibt bestehen. _CURRENT_SESSION_ID ist ZUSÄTZLICH.
|
||||
_CURRENT_SESSION_ID: contextvars.ContextVar[str] = contextvars.ContextVar(
|
||||
"_rocketchat_session_id", default=""
|
||||
)
|
||||
|
||||
|
||||
def _set_room_context_origin(chat_id: str, room_id: str) -> str:
|
||||
"""Atomar _room_context[chat_id]["origin_room"] = room_id setzen.
|
||||
@@ -1177,7 +1184,7 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
self._multiroom_context = None
|
||||
# P5 V9.x: OutboundGate — Parallel-Stream-Schutz
|
||||
from archivar.outbound_gate import OutboundGate, GateDecision
|
||||
self._outbound_gate = OutboundGate(default_timeout_s=30.0, max_entry_age_s=60.0)
|
||||
self._outbound_gate = OutboundGate(default_timeout_s=30.0, max_entry_age_s=10.0) # B+: 10s statt 60s
|
||||
if self.archivar is not None:
|
||||
self.archivar.bind_outbound_gate(self._outbound_gate)
|
||||
# User-Flow C: Move-Requests
|
||||
@@ -4703,6 +4710,11 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
# _CURRENT_TASK_TOKEN. copy_context() friert es ein, sodass send()
|
||||
# später den Token-Match validieren kann.
|
||||
_set_room_context_origin(virtual_chat_id, room_id)
|
||||
# ── B3: Session-ID für OutboundGate owner_session_id ──
|
||||
# _CURRENT_SESSION_ID wird via copy_context() eingefroren,
|
||||
# sodass send()/send_clarify() sie für release() nutzen können.
|
||||
# Fallback: uuid4().hex wenn Core keine session_id übergibt.
|
||||
_CURRENT_SESSION_ID.set(uuid.uuid4().hex)
|
||||
# ── V8.1 P1: ContextVar-Snapshot für Drain-Task ──────────
|
||||
ctx = contextvars.copy_context()
|
||||
# V8.1 P1 diagnostic: log dispatch context
|
||||
@@ -6659,7 +6671,8 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
_gate_acquired = False # P1a: track whether WE created the gate entry
|
||||
if task_token and hasattr(self, '_outbound_gate') and self._outbound_gate is not None:
|
||||
from archivar.outbound_gate import GateDecision
|
||||
decision = await self._outbound_gate.try_acquire(_gate_room_id, task_token)
|
||||
_session_id = _CURRENT_SESSION_ID.get() # B3: Session-ID für release()-Fallback
|
||||
decision = await self._outbound_gate.try_acquire(_gate_room_id, task_token, owner_session_id=_session_id)
|
||||
if not decision.allow:
|
||||
# Blockiert — warte bis zu 30s (A4: blocking_room_id)
|
||||
blocking_room = decision.blocking_room_id
|
||||
@@ -6669,7 +6682,7 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
blocking_room_id=blocking_room, timeout_s=30.0
|
||||
)
|
||||
# Retry acquire nach wait
|
||||
decision = await self._outbound_gate.try_acquire(_gate_room_id, task_token)
|
||||
decision = await self._outbound_gate.try_acquire(_gate_room_id, task_token, owner_session_id=_session_id)
|
||||
except asyncio.TimeoutError:
|
||||
decision = GateDecision(allow=False, reason="timeout")
|
||||
|
||||
@@ -6748,6 +6761,27 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
real_room_id = thematic_target
|
||||
logger.debug("[rocketchat] send via Archivar target: %s → %s", chat_id, real_room_id)
|
||||
else:
|
||||
# B2: allow_proactive_user_map Opt-In
|
||||
# Priority 2 (_user_rooms[-1]) und _find_fallback_room_for_user
|
||||
# werden NUR genutzt wenn allow_proactive_user_map=True in der
|
||||
# Plugin-Config (config.yaml) oder metadata['allow_proactive']=True.
|
||||
_allow_proactive = (
|
||||
self.config.extra.get("allow_proactive_user_map", False)
|
||||
or (metadata or {}).get("allow_proactive", False)
|
||||
)
|
||||
if not _allow_proactive:
|
||||
logger.error(
|
||||
"[rocketchat] No resolved room for %s — "
|
||||
"provide explicit room_id or enable allow_proactive_user_map",
|
||||
chat_id,
|
||||
)
|
||||
return SendResult(
|
||||
success=False,
|
||||
error=(
|
||||
f"No resolved room for {chat_id} — "
|
||||
"provide explicit room_id or enable allow_proactive_user_map"
|
||||
),
|
||||
)
|
||||
# Priority 2: proaktive Nachricht — use most recent room for this user
|
||||
rooms = self._user_rooms.get(chat_id, [])
|
||||
if rooms:
|
||||
@@ -7111,7 +7145,8 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
_gate_acquired = False # P1a: track whether WE created the gate entry
|
||||
if task_token and hasattr(self, '_outbound_gate') and self._outbound_gate is not None:
|
||||
from archivar.outbound_gate import GateDecision
|
||||
decision = await self._outbound_gate.try_acquire(_gate_room_id, task_token)
|
||||
_session_id = _CURRENT_SESSION_ID.get() # B3: Session-ID für release()-Fallback
|
||||
decision = await self._outbound_gate.try_acquire(_gate_room_id, task_token, owner_session_id=_session_id)
|
||||
if not decision.allow:
|
||||
blocking_room = decision.blocking_room_id
|
||||
if blocking_room:
|
||||
@@ -7119,7 +7154,7 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
await self._outbound_gate.wait_until_clear(
|
||||
blocking_room_id=blocking_room, timeout_s=30.0
|
||||
)
|
||||
decision = await self._outbound_gate.try_acquire(_gate_room_id, task_token)
|
||||
decision = await self._outbound_gate.try_acquire(_gate_room_id, task_token, owner_session_id=_session_id)
|
||||
except asyncio.TimeoutError:
|
||||
decision = GateDecision(allow=False, reason="timeout")
|
||||
if not decision.allow:
|
||||
@@ -7190,6 +7225,27 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
real_room_id = thematic_target
|
||||
logger.debug("[rocketchat] send_clarify via Archivar target: %s → %s", chat_id, real_room_id)
|
||||
else:
|
||||
# B2: allow_proactive_user_map Opt-In
|
||||
# Priority 2 (_user_rooms[-1]) und _find_fallback_room_for_user
|
||||
# werden NUR genutzt wenn allow_proactive_user_map=True in der
|
||||
# Plugin-Config (config.yaml) oder metadata['allow_proactive']=True.
|
||||
_allow_proactive = (
|
||||
self.config.extra.get("allow_proactive_user_map", False)
|
||||
or (metadata or {}).get("allow_proactive", False)
|
||||
)
|
||||
if not _allow_proactive:
|
||||
logger.error(
|
||||
"[rocketchat] send_clarify: No resolved room for %s — "
|
||||
"provide explicit room_id or enable allow_proactive_user_map",
|
||||
chat_id,
|
||||
)
|
||||
return SendResult(
|
||||
success=False,
|
||||
error=(
|
||||
f"No resolved room for {chat_id} — "
|
||||
"provide explicit room_id or enable allow_proactive_user_map"
|
||||
),
|
||||
)
|
||||
# Priority 2: proactive message — use most recent room for this user
|
||||
rooms = self._user_rooms.get(chat_id, [])
|
||||
if rooms:
|
||||
@@ -7357,12 +7413,12 @@ class RocketChatAdapter(BasePlatformAdapter):
|
||||
"""P5 V9.x W2: Periodischer Sweep stale Gate-Einträge.
|
||||
|
||||
Läuft als asyncio.create_task (nicht threading.Timer).
|
||||
Sweept alle 30s — entfernt Einträge die älter als
|
||||
max_entry_age_s (60s) sind.
|
||||
B+: Sweept alle 5s (war 30s) — entfernt Einträge die älter als
|
||||
max_entry_age_s (10s, war 60s) sind.
|
||||
"""
|
||||
while self._running and not self._shutdown_event.is_set():
|
||||
try:
|
||||
await asyncio.sleep(30.0)
|
||||
await asyncio.sleep(5.0) # B+: 5s statt 30s
|
||||
if hasattr(self, '_outbound_gate') and self._outbound_gate is not None:
|
||||
swept = self._outbound_gate._sweep_stale_entries()
|
||||
if swept > 0:
|
||||
|
||||
Reference in New Issue
Block a user