The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
perf(gateway): native admission derives its route without re-hashing captured media
Tekniumfa48e07828ae
diff --git a/gateway/session_authority.py b/gateway/session_authority.pyindex b0f0708deda..3170c9adef5 100644--- a/gateway/session_authority.py+++ b/gateway/session_authority.py@@ -329,11 +329,11 @@ class SessionAuthority:async def _admit_native(self, event):import json- from gateway.session_envelope import prepare_native, restore_native+ from gateway.session_envelope import prepare_native, restore_native_sourceself._require_admission_open()payload = await prepare_native(self.runner, event)self._require_admission_open()- source = restore_native(payload).source+ source = restore_native_source(payload) # no re-hash of the just-captured media on the loopfrom gateway.session_runtime_workers import tracked_writeref = self._publish_route(source, *await tracked_write(self, partial(self._reserve_route, source)))identity = json.dumps([source.profile, source.platform.value, source.chat_id,diff --git a/gateway/session_envelope.py b/gateway/session_envelope.pyindex 4fdb03bcd18..60ed56f9f78 100644--- a/gateway/session_envelope.py+++ b/gateway/session_envelope.py@@ -172,7 +172,9 @@ def _snapshot_native(runner, event, provenance, fresh_roles=False, encoded_sourcreturn payload-def restore_native(payload, runner=None):+def restore_native_source(payload, runner=None):+ """restore_native's source (provenance included) without re-verifying the captured media:+ admission needs only the route; execution's restore_native still checks every byte."""envelope = payload['native_text_v1']source = SessionSource.from_dict(envelope['source'])source.is_bot = envelope['source']['is_bot']@@ -183,6 +185,12 @@ def restore_native(payload, runner=None):runner = runner or callback_runner()if runner is not None:restore_provenance(runner, source, envelope['provenance'])+ return source+++def restore_native(payload, runner=None):+ envelope = payload['native_text_v1']+ source = restore_native_source(payload, runner)timestamp = datetime.fromisoformat(envelope['timestamp'])if timestamp.tzinfo is not None and timestamp.timestamp() == 0:# Epoch 0 pins the retry fingerprint of producers with no provider time (relay, webhook,diff --git a/tests/gateway/test_native_media_capture_lease.py b/tests/gateway/test_native_media_capture_lease.pyindex 836ca218d9d..dd9fc2b4a5b 100644--- a/tests/gateway/test_native_media_capture_lease.py+++ b/tests/gateway/test_native_media_capture_lease.py@@ -82,8 +82,8 @@ async def test_refused_native_admission_leaves_no_fresh_capture(tmp_path, monkeyreturn {'text': 'x', 'native_text_v1': {'source': {}, 'event': {'message_id': 'm'},'media': capture_native_media([cache_image_from_bytes(PNG + b'fresh', '.png')])}}monkeypatch.setattr('gateway.session_envelope.prepare_native', prepare)- monkeypatch.setattr('gateway.session_envelope.restore_native',- lambda payload: type('E', (), {'source': authority.sessions['s'].source})())+ monkeypatch.setattr('gateway.session_envelope.restore_native_source',+ lambda payload: authority.sessions['s'].source)def refused(*args, **kwargs):raise RuntimeStoreError('admission_conflict')diff --git a/tests/gateway/test_native_media_capture_off_loop.py b/tests/gateway/test_native_media_capture_off_loop.pyindex 2a782312a2d..29bc420204e 100644--- a/tests/gateway/test_native_media_capture_off_loop.py+++ b/tests/gateway/test_native_media_capture_off_loop.py@@ -47,3 +47,35 @@ async def test_native_media_capture_runs_off_loop_and_commits_owned_bytes(tmp_paassert Path(restored.media_urls[0]).read_bytes() == b'video-bytes' * 1000finally:authority.db.close()+++async def test_native_admission_does_not_rehash_captured_media_on_the_loop(tmp_path, monkeypatch):+ """After the off-loop capture, admission derives the route's source without re-verifying the+ captured bytes on the owner loop (B1-07 remainder); execution still verifies them."""+ import hashlib+ from plugins.platforms.whatsapp.adapter import WhatsAppAdapter+ monkeypatch.setenv('WHATSAPP_ALLOWED_USERS', '15551234567')+ monkeypatch.setattr(SessionAuthority, '_schedule', lambda self, ref: None)+ loop_thread, on_loop = threading.current_thread(), []+ real_digest = hashlib.file_digest++ def digest(*a, **k):+ on_loop.append(threading.current_thread() is loop_thread)+ return real_digest(*a, **k)+ monkeypatch.setattr(hashlib, 'file_digest', digest)+ runner = GatewayRunner(GatewayConfig(sessions_dir=tmp_path / 'sessions'))+ adapter = WhatsAppAdapter(PlatformConfig(enabled=True, extra={'session_path': str(tmp_path / 'wa')}))+ runner.adapters = {Platform.WHATSAPP: adapter}+ authority = await initialize_session_authority(runner, profile_id='default', instance_id='media')+ try:+ attachment = tmp_path / 'clip.mp4'+ attachment.write_bytes(b'video-bytes' * 1000)+ event = MessageEvent(text='look', message_type=MessageType.VIDEO, message_id='v2',+ source=adapter.build_source(chat_id=CHAT, user_id=CHAT, chat_type='dm'),+ media_urls=[str(attachment)], media_types=['video/mp4'])+ receipt = await authority.admit_native(event)+ assert receipt.admission_id+ assert True not in on_loop, f'{on_loop.count(True)} captured-media re-hash(es) on the owner loop'+ finally:+ authority.db.close()