fix: retain relay deliveries until canonical admission commits

This commit is contained in:
Teknium
2026-09-08 16:41:37 -07:00
parent 9d9e4789dd
commit e75bc52f50
2 changed files with 28 additions and 14 deletions

View File

@@ -826,7 +826,9 @@ class RelayAdapter(BasePlatformAdapter):
# the connector replays its durable buffer, and a long turn straddling a
# quiet socket drop got re-run (final answer 2-5x). Platform message identity
# is stable across replays.
dedupe_key = self._inbound_dedupe_key(event)
runner = getattr(self._message_handler, '__self__', None)
native = getattr(runner, 'session_authority', None) is not None and not event.get_command()
dedupe_key = None if native else self._inbound_dedupe_key(event)
if dedupe_key is not None:
if dedupe_key in self._seen_inbound:
logger.info("relay inbound dropped as replay (dedupe key=%s)", dedupe_key)
@@ -840,21 +842,18 @@ class RelayAdapter(BasePlatformAdapter):
if await self._consume_prompt_response(event):
return
await self._localize_inbound_media(event)
runner = getattr(self._message_handler, '__self__', None)
if getattr(runner, 'session_authority', None) is not None:
# Busy authority dispatch waits for the final. Never await it on the
# WS reader: that same reader must receive the outbound_result ACK.
task = asyncio.create_task(self.handle_message(event))
self._background_tasks.add(task)
task.add_done_callback(self._relay_ingress_done)
if native:
from datetime import datetime, timezone
from gateway.platforms.webhook_ingress import admit_producer
# The connector does not transmit a provider timestamp. Local arrival
# time must not change the fingerprint of a retried provider identity.
event.timestamp = datetime.fromtimestamp(0, timezone.utc)
# Wait only for the SQLite receipt, never inference or outbound ACKs
# (those need this same WS reader). Exceptions deliberately suppress ACK.
await admit_producer(self, event)
else:
await self.handle_message(event)
def _relay_ingress_done(self, task):
self._background_tasks.discard(task)
if not task.cancelled() and (error := task.exception()) is not None:
logger.error('Relay ingress failed', exc_info=error)
_SEEN_INBOUND_MAX = 512
def _inbound_dedupe_key(self, event) -> Optional[str]:

View File

@@ -86,7 +86,8 @@ async def probe(peer):
'platform': 'telegram', 'chat_id': 'relay-chat', 'chat_type': 'dm', 'user_id': 'relay-user'}}
async def send(raw_event):
await connector_ws.send(json.dumps({'type': 'inbound', 'event': raw_event}) + '\n')
await connector_ws.send(json.dumps({'type': 'inbound', 'event': raw_event,
'bufferId': raw_event['message_id']}) + '\n')
async with asyncio.timeout(25):
while True:
entries = list(authority.sessions.values())
@@ -95,6 +96,8 @@ async def probe(peer):
if any(r['request_id'] == raw_event['message_id'] and r['status'] == 'terminal' for r in rows):
while adapter._active_sessions:
await asyncio.sleep(.01)
assert any(f.get('type') == 'inbound_ack' and f.get('bufferId') == raw_event['message_id']
for f in outgoing), outgoing
return rows
# Fail on the real adapter's completed rejection, rather than waiting for a timeout.
errors = [f for f in outgoing if 'invalid_params' in json.dumps(f) or 'not_found' in json.dumps(f)]
@@ -102,6 +105,18 @@ async def probe(peer):
await asyncio.sleep(.02)
try:
failed_insert = asyncio.Event()
authority.db._execute_write(lambda conn: conn.create_function(
'ingress_failure_witness', 0, lambda: failed_insert.set() or 1))
authority.db._execute_write(lambda conn: conn.execute("CREATE TRIGGER fail_relay BEFORE INSERT ON session_admissions BEGIN SELECT ingress_failure_witness(); SELECT RAISE(ABORT, 'owned storage failure'); END"))
await connector_ws.send(json.dumps({'type': 'inbound', 'event': raw,
'bufferId': 'durable-buffer-1'}) + '\n')
await asyncio.wait_for(failed_insert.wait(), 10)
await connector_ws.close()
connector_ws = await asyncio.wait_for(ready.get(), 5)
assert not any(f.get('type') == 'inbound_ack' for f in outgoing), outgoing
assert not peer.requests
authority.db._execute_write(lambda conn: conn.execute('DROP TRIGGER fail_relay'))
rows = await send(raw)
sid = next(iter(authority.sessions))
warm_agent = authority.agent(SessionRef('default', sid))