The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): serve hosted attachment chunks from a ranged blob read
teknium1524c8ffaf2ff
diff --git a/gateway/hosted_room_attachments.py b/gateway/hosted_room_attachments.pyindex 9d6ff087618..2a3c7137f05 100644--- a/gateway/hosted_room_attachments.py+++ b/gateway/hosted_room_attachments.py@@ -451,7 +451,8 @@ class HostedRoomAttachmentStore:os.close(descriptor)temp.unlink(missing_ok=True)- def _read_blob(self, *, blob_id: str, size: int, sha256: str) -> bytes:+ def _open_blob(self, *, blob_id: str, size: int) -> int:+ """Open the regular blob file and fence its size against the durable row."""path = self._blob_path(blob_id)flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)try:@@ -462,6 +463,14 @@ class HostedRoomAttachmentStore:info = os.fstat(descriptor)if not stat.S_ISREG(info.st_mode) or info.st_size != size:raise AttachmentIntegrityError("canonical attachment blob size changed")+ except BaseException:+ os.close(descriptor)+ raise+ return descriptor++ def _read_blob(self, *, blob_id: str, size: int, sha256: str) -> bytes:+ descriptor = self._open_blob(blob_id=blob_id, size=size)+ try:with os.fdopen(descriptor, "rb", closefd=False) as handle:data = handle.read(MAX_ATTACHMENT_BYTES + 1)finally:@@ -470,6 +479,20 @@ class HostedRoomAttachmentStore:raise AttachmentIntegrityError("canonical attachment blob failed SHA-256 validation")return data+ def _read_blob_range(self, *, blob_id: str, size: int, offset: int, length: int) -> bytes:+ """One slice of the blob; the row's SHA-256 is the caller's whole-file digest and the+ receiver verifies the reassembled bytes against it, so no slice is hashed here."""+ descriptor = self._open_blob(blob_id=blob_id, size=size)+ try:+ with os.fdopen(descriptor, "rb", closefd=False) as handle:+ handle.seek(offset)+ data = handle.read(length)+ finally:+ os.close(descriptor)+ if len(data) != min(length, size - offset):+ raise AttachmentIntegrityError("canonical attachment blob size changed")+ return data+@staticmethoddef _metadata(row: sqlite3.Row, *, idempotent: bool = False) -> dict[str, Any]:result = {@@ -864,6 +887,36 @@ class HostedRoomAttachmentStore:)return AttachmentData(self._metadata(row), data)+ def read_range(+ self,+ *,+ room_id: Any,+ attachment_id: Any,+ recipient_member_id: Any,+ event_id: Any,+ offset: int,+ length: int,+ ) -> AttachmentData:+ """``read`` for one slice: same authorization, metadata carries the stored whole-file+ SHA-256 and the receiver verifies the reassembled bytes against it."""+ room_id = _identifier(room_id, label="room_id")+ attachment_id = _attachment_id(attachment_id)+ recipient_member_id = _identifier(recipient_member_id, label="recipient_member_id")+ normalized_event = _identifier(event_id, label="event_id")+ if type(offset) is not int or type(length) is not int or offset < 0 or length <= 0:+ raise AttachmentError("attachment range must be non-negative integers")+ with self._transaction() as conn:+ row = self._read_committed_row(+ conn, room_id=room_id, attachment_id=attachment_id,+ recipient_member_id=recipient_member_id,+ normalized_event=normalized_event, viewer=False, now=float(self.clock()),+ )+ if offset >= int(row["size"]):+ raise AttachmentError("attachment range starts past the end")+ data = self._read_blob_range(+ blob_id=str(row["blob_id"]), size=int(row["size"]), offset=offset, length=length)+ return AttachmentData(self._metadata(row), data)+def reconcile_room_events(self) -> int:"""Recover only attachment commitments named by the durable event payload."""diff --git a/gateway/session_hosted_transport.py b/gateway/session_hosted_transport.pyindex 010649764a2..f36f648946a 100644--- a/gateway/session_hosted_transport.py+++ b/gateway/session_hosted_transport.py@@ -98,20 +98,25 @@ def _attest(binding, operation, params):def source_attachment_chunk(service, member, room_id, manifest, params):- """Read scoped source bytes only on the source owner's authenticated handler."""+ """Read scoped source bytes only on the source owner's authenticated handler.++ Serves one slice per call; the row's stored SHA-256 (verified at upload) rides along+ so the target can verify the reassembled file without the source re-hashing 15 MB+ per 24 KiB chunk.+ """index, offset = params.get('index'), params.get('offset')if (type(index) is not int or not 0 <= index < len(manifest)or type(offset) is not int or not 0 <= offset < manifest[index]['size']):raise RuntimeStoreError('permission_denied')from gateway.hosted_room_attachments import HostedRoomAttachmentStoreitem = manifest[index]- saved = HostedRoomAttachmentStore(service.db_path).read(room_id=room_id,- attachment_id=item['attachment_id'], event_id=item['event_id'], recipient_member_id=member)+ saved = HostedRoomAttachmentStore(service.db_path).read_range(room_id=room_id,+ attachment_id=item['attachment_id'], event_id=item['event_id'], recipient_member_id=member,+ offset=offset, length=_CHUNK_BYTES)if any(saved.attachment[key] != item[key] for key in ('kind', 'name', 'mime', 'size')):raise RuntimeStoreError('permission_denied')- data = saved.data- return {'data_base64': base64.b64encode(data[offset:offset + _CHUNK_BYTES]).decode('ascii'),- 'sha256': hashlib.sha256(data).hexdigest()}+ return {'data_base64': base64.b64encode(saved.data).decode('ascii'),+ 'sha256': saved.attachment['sha256']}def _attachment_data(binding, attested, params):diff --git a/tests/gateway/test_session_hosted_transport.py b/tests/gateway/test_session_hosted_transport.pyindex f6f4c5ad4ec..22780b248e2 100644--- a/tests/gateway/test_session_hosted_transport.py+++ b/tests/gateway/test_session_hosted_transport.py@@ -87,51 +87,65 @@ def test_authenticated_owner_transport_rechecks_source_and_cold_binding(owner, tasyncio.run_coroutine_threadsafe(server.stop(), loop).result()-def test_source_attestation_binds_bytes_to_task_member_and_current_home(tmp_path, monkeypatch):- import base64- import time- from types import SimpleNamespace- from gateway.session_hosted_service import CanonicalHostedRoomService- from gateway import hosted_room_driver as tasks- from gateway.hosted_rooms import create_room, local_authority_gateway_id- from gateway.hosted_room_attachments import HostedRoomAttachmentStore- from hermes_state import SessionDB- from hermes_state_runtime import begin_runtime_epoch, RuntimeStoreError- from tui_gateway.hosted_room_driver import HostedRoomBinding- import gateway.run- monkeypatch.setenv('HERMES_HOME', str(tmp_path))- homes = {'other': str(tmp_path / 'profiles' / 'other')}- monkeypatch.setattr(gateway.run, '_load_gateway_config', lambda: {'hosted_rooms': {'profiles': homes}})- with SessionDB(tmp_path / 'state.db') as db:- authority = SimpleNamespace(db=db, profile_id=str(tmp_path), epoch=begin_runtime_epoch(db, instance_id='test'))- service = CanonicalHostedRoomService(authority, None)- service.authorize_room('alice', 'room', create=True)- gateway = local_authority_gateway_id()- create_room(db.db_path, room_id='room', name='Room', authority_gateway_id=gateway, members=[+class _SourceTask:+ """One committed attachment bound to a running task on a real source owner."""++ def __init__(self, tmp_path, monkeypatch, data, *, name='note.txt', mime='text/plain', kind='file'):+ import time+ from types import SimpleNamespace+ from gateway.session_hosted_service import CanonicalHostedRoomService+ from gateway import hosted_room_driver as tasks+ from gateway.hosted_rooms import create_room, local_authority_gateway_id+ from gateway.hosted_room_attachments import HostedRoomAttachmentStore+ from hermes_state import SessionDB+ from hermes_state_runtime import begin_runtime_epoch+ import gateway.run+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))+ self.homes = {'other': str(tmp_path / 'profiles' / 'other')}+ monkeypatch.setattr(gateway.run, '_load_gateway_config', lambda: {'hosted_rooms': {'profiles': self.homes}})+ self.db = db = SessionDB(tmp_path / 'state.db')+ self.authority = SimpleNamespace(db=db, profile_id=str(tmp_path), epoch=begin_runtime_epoch(db, instance_id='test'))+ self.service = CanonicalHostedRoomService(self.authority, None)+ self.service.authorize_room('alice', 'room', create=True)+ self.gateway = local_authority_gateway_id()+ create_room(db.db_path, room_id='room', name='Room', authority_gateway_id=self.gateway, members=[{'member_id': 'one', 'profile': 'default', 'handle': 'one'},{'member_id': 'two', 'profile': 'other', 'handle': 'two'}])- data = b'owned source bytes' * 4000- store = HostedRoomAttachmentStore(db.db_path)- saved = store.put(room_id='room', upload_id='upload', kind='file', name='note.txt', mime='text/plain', data=data)+ self.data = data+ self.store = store = HostedRoomAttachmentStore(db.db_path)+ saved = store.put(room_id='room', upload_id='upload', kind=kind, name=name, mime=mime, data=data)manifest = [{k: saved[k] for k in ('attachment_id', 'kind', 'name', 'size', 'mime')}]store.commit_message(room_id='room', event_id='event', manifest=manifest, recipient_member_ids=['two'])- bound = [{**manifest[0], 'event_id': 'event'}]- identity = tasks.TaskIdentity('room', 'task', 'thread', 'turn')+ self.bound = [{**manifest[0], 'event_id': 'event'}]+ self.identity = tasks.TaskIdentity('room', 'task', 'thread', 'turn')payload = {'target_profile': 'other', 'target_member_id': 'two', 'source_event_seq': 1,- 'prompt': 'frozen', 'attachments': bound}- tasks.admit_task(db.db_path, identity, payload=payload, clock=time.time)- lease = tasks.acquire_lease(db.db_path, room_id='room', gateway_id=gateway, authority_epoch=1,- process_generation='test', ttl_seconds=30, clock=time.time)- tasks.start_task(db.db_path, identity, lease, expected_cancel_generation=0, clock=time.time)- task, = tasks.list_tasks(db.db_path, room_id='room')- selector = dict(room_id='room', member_id='two', profile='other')- params = dict(task=asdict(identity), execution_generation=task['execution_generation'],- prompt='frozen', attachments=bound, _target_home=homes['other'])+ 'prompt': 'frozen', 'attachments': self.bound}+ tasks.admit_task(db.db_path, self.identity, payload=payload, clock=time.time)+ lease = tasks.acquire_lease(db.db_path, room_id='room', gateway_id=self.gateway, authority_epoch=1,+ process_generation='test', ttl_seconds=30, clock=time.time)+ tasks.start_task(db.db_path, self.identity, lease, expected_cancel_generation=0, clock=time.time)+ self.task, = tasks.list_tasks(db.db_path, room_id='room')+ self.selector = dict(room_id='room', member_id='two', profile='other')+ self.params = dict(task=asdict(self.identity), execution_generation=self.task['execution_generation'],+ prompt='frozen', attachments=self.bound, _target_home=self.homes['other'])+ self.binding = {'source_home': str(tmp_path), 'target_home': self.homes['other'], 'selector': self.selector}+++def test_source_attestation_binds_bytes_to_task_member_and_current_home(tmp_path, monkeypatch):+ import base64+ from hermes_state_runtime import RuntimeStoreError+ from tui_gateway.hosted_room_driver import HostedRoomBinding+ from gateway.session_hosted_transport import _CHUNK_BYTES+ source = _SourceTask(tmp_path, monkeypatch, b'owned source bytes' * 4000)+ with source.db:+ service, data, bound, task, gateway, homes, authority = (+ source.service, source.data, source.bound, source.task, source.gateway, source.homes, source.authority)+ selector, params = source.selector, source.paramswith pytest.raises(RuntimeStoreError, match='permission_denied'):service.attest(selector, 'submit', {**params, 'attachments': []})assert service.attest(selector, 'submit', params)['attachments'] == boundchunks = []- for offset in range(0, len(data), 24576):+ for offset in range(0, len(data), _CHUNK_BYTES):result = service.attest(selector, 'attachment', {**params, 'index': 0, 'offset': offset})chunks.append(base64.b64decode(result['data_base64']))assert b''.join(chunks) == data@@ -145,7 +159,7 @@ def test_source_attestation_binds_bytes_to_task_member_and_current_home(tmp_pathinstall_hosted_transport(server, authority, loop, attest=service.attest)assert asyncio.run_coroutine_threadsafe(server.start(), loop).result()try:- transport_binding = {'source_home': str(tmp_path), 'target_home': homes['other'], 'selector': selector}+ transport_binding = source.bindingattested = service.attest(selector, 'submit', params)assert _attachment_data(transport_binding, attested, params) == [(bound[0], data)]finally:@@ -166,3 +180,37 @@ def test_source_attestation_binds_bytes_to_task_member_and_current_home(tmp_pathhomes.clear()with pytest.raises(RuntimeStoreError, match='permission_denied'):service._resolve_member_transport(binding, task)+++def test_source_chunk_reads_only_the_requested_slice(tmp_path, monkeypatch):+ """Source cost per chunk is bounded by the chunk, not by the whole blob; the+ target still refuses a blob whose bytes drifted from the attested digest."""+ import base64+ import hashlib+ from gateway.hosted_room_attachments import HostedRoomAttachmentStore+ from gateway import session_hosted_transport as transport+ from hermes_state_runtime import RuntimeStoreError+ source = _SourceTask(tmp_path, monkeypatch, bytes(range(256)) * (4 * transport._CHUNK_BYTES // 256 + 1),+ name='blob.bin', mime='application/octet-stream')+ with source.db:+ service, params = source.service, source.params+ monkeypatch.setattr(HostedRoomAttachmentStore, '_read_blob',+ lambda *a, **k: pytest.fail('chunk request read the whole blob'))+ hashed = []+ real_sha256 = hashlib.sha256+ monkeypatch.setattr(hashlib, 'sha256', lambda data=b'': (hashed.append(len(data)), real_sha256(data))[1])+ result = service.attest(source.selector, 'attachment',+ {**params, 'index': 0, 'offset': transport._CHUNK_BYTES})+ chunk = base64.b64decode(result['data_base64'])+ assert chunk == source.data[transport._CHUNK_BYTES:2 * transport._CHUNK_BYTES]+ assert result['sha256'] == real_sha256(source.data).hexdigest()+ assert sum(hashed) < len(chunk), 'source re-hashed more than the served slice'+ # End-to-end: the target verifies the assembled bytes against the attested digest.+ monkeypatch.setattr(transport, 'owner_request', lambda home, verb, p, **kw: service.attest(+ p['selector'], p['operation'], p['params']))+ attested = service.attest(source.selector, 'submit', params)+ assert transport._attachment_data(source.binding, attested, params) == [(source.bound[0], source.data)]+ blob, = source.store.blob_root.iterdir()+ blob.write_bytes(source.data[:-1] + bytes([source.data[-1] ^ 1]))+ with pytest.raises(RuntimeStoreError, match='permission_denied'):+ transport._attachment_data(source.binding, attested, params)