Skip to content

The agent that grows with you

refactor(relay): declare the inbound admission lane map in RelayAdapter.__init__

unsupportedpastelsd485d55cf243
diff --git a/gateway/relay/adapter.py b/gateway/relay/adapter.py
index 62e121c243f..e44a8c1e92d 100644
--- a/gateway/relay/adapter.py
+++ b/gateway/relay/adapter.py
@@ -156,6 +156,8 @@ class RelayAdapter(RelayDiscordInteractionMixin, BasePlatformAdapter):
# (scope, chat) -> (chat_name, chat_topic); the second map is what the store holds.
self._discord_chat_labels: dict[tuple, tuple] = {}
self._discord_labels_recorded: dict[tuple, tuple] = {}
+ # (platform, chat_id) -> the chat's latest off-reader admission task (inbound_admission).
+ self._admission_lanes: dict[tuple[str, str], asyncio.Task] = {}
# Live cards: draft_key -> draft_id of the OPEN native stream. Armed by
# send_draft; consumed by send() to convert the turn-final into
# draft(final=true) instead of a duplicate post. Keyed by _draft_key (chat +
diff --git a/gateway/relay/inbound_admission.py b/gateway/relay/inbound_admission.py
index 74468439490..ba0eb88d9fd 100644
--- a/gateway/relay/inbound_admission.py
+++ b/gateway/relay/inbound_admission.py
@@ -20,7 +20,7 @@ async def admit_relay_delivery(adapter, runner, event):
if not (has_hook('pre_gateway_dispatch') or has_hook(POST_ADMISSION_HOOK)):
await _admit(adapter, runner, event)
return None
- lanes = adapter.__dict__.setdefault('_admission_lanes', {})
+ lanes = adapter._admission_lanes
lane = (event.source.platform.value, event.source.chat_id)
task = asyncio.create_task(_admit_after(lanes.get(lane), adapter, runner, event))
lanes[lane] = task
diff --git a/tests/gateway/test_pre_gateway_dispatch_relay_reader.py b/tests/gateway/test_pre_gateway_dispatch_relay_reader.py
index 1906baf0bae..32d73aa7adb 100644
--- a/tests/gateway/test_pre_gateway_dispatch_relay_reader.py
+++ b/tests/gateway/test_pre_gateway_dispatch_relay_reader.py
@@ -140,13 +140,15 @@ async def test_a_hook_waiting_for_another_chat_does_not_block_it_and_one_chat_st
b_arrived.set()
_hook(hook)
- async with _relay(tmp_path, monkeypatch) as (_adapter, ws, outgoing, authority, _admissions):
+ async with _relay(tmp_path, monkeypatch) as (adapter, ws, outgoing, authority, _admissions):
+ assert getattr(adapter, "_admission_lanes", None) == {} # declared adapter state, not added lazily
for buffer_id, message_id, chat_id in (("buf-1", "a1", "chat-a"), ("buf-2", "a2", "chat-a"),
("buf-3", "b1", "chat-b")):
event = _inbound(message_id, chat_id)
await ws.send(json.dumps({"type": "inbound", "event": event, "bufferId": buffer_id}) + "\n")
await async_wait_until(lambda: len(_acked(outgoing)) == 3, timeout=20, what="all three acknowledged")
rows = authority.db._read_all("SELECT request_id FROM session_admissions ORDER BY rowid")
+ assert adapter._admission_lanes == {} # each chat's lane is dropped once its last admission is done
assert fired == ["a1", "b1", "a2"]
assert [row["request_id"] for row in rows if row["request_id"].startswith("a")] == ["a1", "a2"]