P5 V9.x: Rainer Review Fixes — A1-A4 + W2

FIX A1: Adapter-Integration nachgeliefert
- adapter.py: init self._outbound_gate nach Z.1177, Gate-Check in send()
  nach Red-Flag, send_clarify() Gate-Check, release in _process_incoming
  finally und _dispatch_agent finally, _post_message_unlocked für Quick-Reply
- archivar/SKILL.md: Z.20-21 ergänzt + Outbound-Sicht V9.x Abschnitt
- archivar/adapter_integration.py: bind_outbound_gate Hook

FIX A2: Tests grün (7/7, keine Warnings)
- pytest-asyncio bereits installiert (pytest 9.1.0)
- @pytest.mark.outbound_gate in conftest.py registriert

FIX A3: try_acquire Spec-Abweichung behoben
- try_acquire jetzt ASYNC (await asyncio.Lock)
- Rückgabetyp: GateDecision Dataclass (allow, reason, blocking_room_id)
- Reasons: gate_clear, same_room_same_token, same_room_different_token,
  different_room_busy

FIX A4: wait_until_clear Spec-Abweichung behoben
- blocking_room_id Parameter hinzugefügt
- _waiters per-Raum Dict (nicht flat List) → kein False-Wake-Bug
- _TurnHandle jetzt Dataclass mit task und chat_id Feldern

W2: Sweep-Loop als asyncio.create_task im Adapter
- _gate_sweep_loop() alle 30s, nicht threading.Timer
- Gestartet in connect() nach _listen_loop

Tests: 1359 passed, 0 failed, 0 regressions.
This commit is contained in:
Bernd (Hermes)
2026-07-30 08:43:00 +02:00
parent e28c575169
commit 1958a53f0e
8 changed files with 1314 additions and 100 deletions
+7
View File
@@ -16,6 +16,13 @@ from pathlib import Path
import pytest
# P5 V9.x: OutboundGate Marker registrieren
def pytest_configure(config):
config.addinivalue_line(
"markers", "outbound_gate: P5 V9.x OutboundGate Parallel-Stream-Schutz Tests"
)
@pytest.fixture
def temp_room_map_path():
"""Erzeugt eine temporäre room-map.json mit minimalem Test-Setup.
+38 -26
View File
@@ -20,7 +20,7 @@ import pytest
# Ensure plugin package is importable (same pattern as other archivar tests)
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
from plugin.archivar.outbound_gate import OutboundGate, GateStats
from plugin.archivar.outbound_gate import OutboundGate, GateStats, GateDecision
logger = logging.getLogger(__name__)
@@ -46,20 +46,25 @@ async def test_outbound_gate_waits_then_releases():
token_a = "token-aaaa-bbbb-cccc"
token_b = "token-xxxx-yyyy-zzzz"
# Stream A acquires the gate
acquired_a = gate.try_acquire(room_a, token_a)
assert acquired_a is True, "Stream A should acquire the gate (empty)"
# Stream A acquires the gate (A3: async, returns GateDecision)
decision_a = await gate.try_acquire(room_a, token_a)
assert decision_a.allow is True, "Stream A should acquire the gate (empty)"
assert decision_a.reason == "gate_clear"
# Stream B tries to acquire — should be blocked (different room)
acquired_b = gate.try_acquire(room_b, token_b)
assert acquired_b is False, "Stream B should be blocked (different room busy)"
decision_b = await gate.try_acquire(room_b, token_b)
assert decision_b.allow is False, "Stream B should be blocked (different room busy)"
assert decision_b.reason == "different_room_busy"
assert decision_b.blocking_room_id == room_a
# Verify stats
assert gate.stats.acquired_total == 1
assert gate.stats.different_room_blocked == 1
# B waits for gate to clear (with timeout)
wait_task = asyncio.create_task(gate.wait_until_clear(timeout_s=5.0))
# B waits for gate to clear (A4: blocking_room_id parameter)
wait_task = asyncio.create_task(
gate.wait_until_clear(blocking_room_id=room_a, timeout_s=5.0)
)
# Give B a moment to start waiting
await asyncio.sleep(0.05)
@@ -72,8 +77,9 @@ async def test_outbound_gate_waits_then_releases():
await asyncio.wait_for(wait_task, timeout=2.0)
# Now B can acquire
acquired_b_retry = gate.try_acquire(room_b, token_b)
assert acquired_b_retry is True, "Stream B should acquire after A released"
decision_b_retry = await gate.try_acquire(room_b, token_b)
assert decision_b_retry.allow is True, "Stream B should acquire after A released"
assert decision_b_retry.reason == "gate_clear"
# Cleanup
gate.release(room_b, token_b)
@@ -103,15 +109,16 @@ async def test_outbound_gate_timeout_triggers_quick_reply():
token_b = "token-qr-bbbb"
# Stream A acquires
gate.try_acquire(room_a, token_a)
await gate.try_acquire(room_a, token_a)
# Stream B tries to acquire → blocked
acquired_b = gate.try_acquire(room_b, token_b)
assert acquired_b is False
decision_b = await gate.try_acquire(room_b, token_b)
assert decision_b.allow is False
assert decision_b.blocking_room_id == room_a
# B waits with short timeout (1s)
# B waits with short timeout (1s) — A4: blocking_room_id
try:
await gate.wait_until_clear(timeout_s=1.0)
await gate.wait_until_clear(blocking_room_id=room_a, timeout_s=1.0)
# Should not reach here — timeout expected
timeout_occurred = False
except asyncio.TimeoutError:
@@ -151,20 +158,23 @@ async def test_outbound_gate_multi_turn_same_room_allowed():
token_a = "token-multi-a"
# First turn: acquire (creates entry)
acquired_1 = gate.try_acquire(room_a, token_a)
assert acquired_1 is True
decision_1 = await gate.try_acquire(room_a, token_a)
assert decision_1.allow is True
assert decision_1.reason == "gate_clear"
assert gate.stats.acquired_total == 1
# Second turn: same room, same token, BEFORE release → allowed (W3)
acquired_2 = gate.try_acquire(room_a, token_a)
assert acquired_2 is True, "Multi-Turn same room same token should be allowed"
decision_2 = await gate.try_acquire(room_a, token_a)
assert decision_2.allow is True, "Multi-Turn same room same token should be allowed"
assert decision_2.reason == "same_room_same_token"
assert gate.stats.same_room_same_token == 1
# acquired_total should NOT increment for same_room_same_token (W3: KEIN neuer Eintrag)
assert gate.stats.acquired_total == 1, "Multi-Turn should NOT create new entry"
# Third turn: same room, same token, BEFORE release → allowed again
acquired_3 = gate.try_acquire(room_a, token_a)
assert acquired_3 is True
decision_3 = await gate.try_acquire(room_a, token_a)
assert decision_3.allow is True
assert decision_3.reason == "same_room_same_token"
assert gate.stats.same_room_same_token == 2
assert gate.stats.acquired_total == 1 # Still 1 — no new entries
@@ -194,13 +204,15 @@ async def test_outbound_gate_stream_retry_mismatch():
token_y = "token-retry-y"
# Token X acquires
acquired_x = gate.try_acquire(room_a, token_x)
assert acquired_x is True
decision_x = await gate.try_acquire(room_a, token_x)
assert decision_x.allow is True
assert decision_x.reason == "gate_clear"
assert gate.stats.acquired_total == 1
# Token Y (same room, different token) → blocked
acquired_y = gate.try_acquire(room_a, token_y)
assert acquired_y is False, "Different token in same room should be blocked"
decision_y = await gate.try_acquire(room_a, token_y)
assert decision_y.allow is False, "Different token in same room should be blocked"
assert decision_y.reason == "same_room_different_token"
assert gate.stats.same_room_different_token == 1
# Verify _in_flight still has token_x
@@ -238,7 +250,7 @@ async def test_outbound_gate_sweep_stale_entries():
room_a = "ROOM_STALE"
token_a = "token-stale"
gate.try_acquire(room_a, token_a)
await gate.try_acquire(room_a, token_a)
assert room_a in gate._in_flight
# Wait for entry to become stale