Skip to content

The agent that grows with you

fix(native): a redelivered media message keeps its receipt despite a new staging name

Tekniumbd317862ca86
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index 1e316c71e20..4a1ae1a5e53 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -289,10 +289,12 @@ class SessionAuthority:
ref = self.register(source)
identity = json.dumps([source.profile, source.platform.value, source.chat_id,
source.thread_id, source.user_id], separators=(',', ':'))
+ request_id = str(payload['native_text_v1']['event']['message_id'] or uuid.uuid4().hex)
+ from gateway.session_ingress_media import reconcile_native_retry
+ payload = reconcile_native_retry(self.db, principal_id='messaging:' + identity, session_id=ref.session_id,
+ request_id=request_id, payload=payload)
row = admit_session_input(self.db, epoch=self.epoch, principal_id='messaging:' + identity,
- session_id=ref.session_id,
- request_id=str(payload['native_text_v1']['event']['message_id'] or uuid.uuid4().hex),
- payload=payload)
+ session_id=ref.session_id, request_id=request_id, payload=payload)
event._gateway_accepted = True
self._publish_pending(ref)
self._schedule(ref)
diff --git a/gateway/session_ingress_media.py b/gateway/session_ingress_media.py
index 8758c12c13c..101c230aa7d 100644
--- a/gateway/session_ingress_media.py
+++ b/gateway/session_ingress_media.py
@@ -93,6 +93,30 @@ def admit_attachments(attachments, *, admitted=None):
return {'attachments_v1': {'media': capture_native_media(paths), 'media_types': mimes}}
+def reconcile_native_retry(db, *, principal_id, session_id, request_id, payload):
+ """A redelivered native message (lost provider ACK, Relay buffer replay) re-downloads its media
+ under a fresh disposable staging name, so its captured path differs from the committed one.
+ Retry identity is the committed bytes, not that name: when this exact identity already holds
+ the same digests and sizes in the same order, the committed references stand in for the fresh
+ capture (whose now-unowned copy is collected) and the admission digest decides the rest, so
+ changed bytes or any other changed field still conflict."""
+ fresh = payload.get('native_text_v1', {}).get('media')
+ if not fresh:
+ return payload
+ with db._read_ctx() as conn:
+ row = conn.execute('SELECT status, payload_json FROM session_admissions WHERE principal_id=? AND '
+ 'target_session_id=? AND request_id=?', (principal_id, session_id, request_id)).fetchone()
+ committed = json.loads(row[1]).get('native_text_v1', {}).get('media') if row is not None else None
+ if not committed or [(r['sha256'], r['size']) for r in committed] != [(r['sha256'], r['size']) for r in fresh]:
+ return payload
+ if row[0] != 'terminal':
+ # A live row still executes these bytes, so they must verify (a terminal one is digest evidence).
+ restore_native_media(committed)
+ kept = {reference['path'] for reference in committed}
+ release_unheld_media(db, [reference for reference in fresh if reference['path'] not in kept], retain_history=False)
+ return {**payload, 'native_text_v1': {**payload['native_text_v1'], 'media': committed}}
+
+
def _attachment_hashes(paths):
hashes = []
for path in paths:
diff --git a/tests/gateway/test_native_media_redelivery.py b/tests/gateway/test_native_media_redelivery.py
new file mode 100644
index 00000000000..084fc8ba939
--- /dev/null
+++ b/tests/gateway/test_native_media_redelivery.py
@@ -0,0 +1,86 @@
+"""A provider redelivery of a committed media message is the same admission, whatever staging name
+its re-download got; changed bytes under the same provider id still conflict."""
+import asyncio
+from datetime import UTC, datetime
+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
+from pathlib import Path
+import threading
+
+import pytest
+
+PNG = b'\x89PNG\r\n\x1a\n' + b'\x00' * 24
+
+
+class _Media(BaseHTTPRequestHandler):
+ body = PNG
+
+ def do_GET(self):
+ self.send_response(200)
+ self.send_header('Content-Type', 'image/png')
+ self.send_header('Content-Length', str(len(self.body)))
+ self.end_headers()
+ self.wfile.write(self.body)
+
+ def log_message(self, *args):
+ pass
+
+
+async def test_redelivered_media_message_returns_its_receipt_and_changed_bytes_conflict(tmp_path, monkeypatch):
+ from gateway.config import GatewayConfig, Platform, PlatformConfig
+ from gateway.platforms.event import MessageEvent, MessageType
+ from gateway.relay.media import RelayMediaClient
+ from gateway.run import GatewayRunner
+ from gateway.session_authority import SessionAuthority, initialize_session_authority
+ from gateway.session_ingress_context import native_callback
+ from gateway.session_ingress_media import _media_root, release_admission_media
+ from hermes_constants import get_hermes_home
+ from hermes_state_runtime import (RuntimeStoreError, claim_session_input, list_session_admissions,
+ settle_session_input)
+ from plugins.platforms.discord.adapter import DiscordAdapter
+ monkeypatch.setattr(SessionAuthority, '_schedule', lambda self, ref: None)
+ monkeypatch.setenv('DISCORD_ALLOWED_USERS', '42')
+ server = ThreadingHTTPServer(('127.0.0.1', 0), _Media)
+ threading.Thread(target=server.serve_forever, daemon=True).start()
+ url = f'http://127.0.0.1:{server.server_port}/photo.png'
+ client = RelayMediaClient(f'http://127.0.0.1:{server.server_port}', None, None)
+ runner = GatewayRunner(GatewayConfig())
+ adapter = DiscordAdapter(PlatformConfig(enabled=True, token='fixture-token', typing_indicator=False))
+ runner.adapters = {Platform.DISCORD: adapter}
+ authority = await initialize_session_authority(runner, profile_id='default', instance_id='owner')
+ runner._wire_adapter_handlers(adapter)
+ source = adapter.build_source(chat_id='42', chat_type='dm', user_id='42')
+
+ async def deliver():
+ # Every delivery re-downloads the attachment, as the Relay adapter does before admission.
+ staged = await client.download(url)
+ # The Relay adapter pins the timestamp: the connector sends no provider time.
+ event = MessageEvent(text='look', source=source, message_id='provider-1', message_type=MessageType.PHOTO,
+ media_urls=[staged], media_types=['image/png'],
+ timestamp=datetime.fromtimestamp(0, UTC))
+ with native_callback(runner, event, get_hermes_home()):
+ return staged, await authority.admit_native(event)
+
+ try:
+ first_staged, first = await deliver()
+ second_staged, second = await deliver() # its ACK was lost: same provider id, same bytes
+ assert Path(first_staged).name != Path(second_staged).name
+ assert second.admission_id == first.admission_id and second.status == 'queued'
+ sid = first.ref.session_id
+ claim = claim_session_input(authority.db, epoch=authority.epoch, session_id=sid)
+ settle_session_input(authority.db, epoch=authority.epoch, admission_id=first.admission_id,
+ generation=claim['generation'], outcome='completed')
+ assert release_admission_media(authority.db, first.admission_id) == 1
+ _, late = await deliver() # redelivered after the turn ran and its bytes were released
+ assert late.admission_id == first.admission_id and late.status == 'terminal'
+ assert not list(_media_root().rglob('*.png')), 'a redelivery copy no admission owns stayed retained'
+ rows = list_session_admissions(authority.db, session_id=sid, pending_only=False)
+ assert [row['request_id'] for row in rows] == ['provider-1'], 'the message must execute once'
+ _Media.body = PNG + b'changed'
+ with pytest.raises(RuntimeStoreError, match='admission_conflict'):
+ await deliver()
+ finally:
+ _Media.body = PNG
+ server.shutdown()
+ server.server_close()
+ await asyncio.to_thread(authority.db.close)