Skip to content

The agent that grows with you

perf(gateway): size hosted attachment chunks to the 512 KiB response line

teknium1cb4b2a1f50f9
diff --git a/gateway/session_hosted_service.py b/gateway/session_hosted_service.py
index 03f0cb4a0ec..8343ab35a7c 100644
--- a/gateway/session_hosted_service.py
+++ b/gateway/session_hosted_service.py
@@ -78,10 +78,13 @@ class CanonicalHostedRoomService(HostedControls, HostedRoomService):
if len(matches) != 1:
raise RuntimeStoreError('permission_denied')
payload = matches[0]['payload']
- result.update(prompt=payload['prompt'], attachments=payload.get('attachments', []))
if operation == 'attachment':
+ # Bytes only: the prompt/manifest already attested for the submit would
+ # otherwise compete with the chunk for the bounded response line.
from gateway.session_hosted_transport import source_attachment_chunk
- result.update(source_attachment_chunk(self, member, room_id, result['attachments'], params))
+ result.update(source_attachment_chunk(self, member, room_id, payload.get('attachments', []), params))
+ else:
+ result.update(prompt=payload['prompt'], attachments=payload.get('attachments', []))
return result
diff --git a/gateway/session_hosted_transport.py b/gateway/session_hosted_transport.py
index f36f648946a..49d15ee12c3 100644
--- a/gateway/session_hosted_transport.py
+++ b/gateway/session_hosted_transport.py
@@ -26,7 +26,11 @@ from hermes_state_runtime import RuntimeStoreError, _epoch
_BINDING = 'gateway.hosted.transport.v1:'
_OPERATIONS = frozenset({'resolve_exact', 'create', 'resume', 'submit', 'history',
'info', 'interrupt', 'discard', 'approve'})
-_CHUNK_BYTES = 24576
+# One chunk per private-socket exchange. The response is a single JSON line capped at
+# gateway.control_socket._MAX_RESPONSE_BYTES (512 KiB) on both the POSIX socket and the
+# Windows pipe: 360 KiB raw -> 480 KiB base64, leaving 32 KiB for the envelope (owner
+# subject, target home, digest); the attachment attest result carries no prompt/manifest.
+_CHUNK_BYTES = 360 * 1024
_CAPS = frozenset({'session:create', 'session:read', 'session:submit',
'session:control', 'session:approve'})
diff --git a/tests/gateway/test_session_hosted_transport.py b/tests/gateway/test_session_hosted_transport.py
index 22780b248e2..e09bf2ef582 100644
--- a/tests/gateway/test_session_hosted_transport.py
+++ b/tests/gateway/test_session_hosted_transport.py
@@ -214,3 +214,44 @@ def test_source_chunk_reads_only_the_requested_slice(tmp_path, monkeypatch):
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)
+
+
+def test_attachment_chunks_fill_the_response_line_without_overflowing_it(tmp_path, monkeypatch):
+ """Chunks nearly fill the single 512 KiB response line shared by the POSIX socket and
+ the Windows pipe, never overflow it even with a long owner subject, and the transfer
+ spends round-trips proportional to size / chunk."""
+ import math
+ from gateway.control_socket import _MAX_RESPONSE_BYTES
+ from gateway import session_hosted_transport as transport
+ from gateway.session_hosted_transport import _CHUNK_BYTES
+ size = 2 * _CHUNK_BYTES + 1
+ source = _SourceTask(tmp_path, monkeypatch, bytes(range(256)) * (size // 256 + 1),
+ name='blob.bin', mime='application/octet-stream')
+ with source.db:
+ service, params = source.service, source.params
+ monkeypatch.setattr(service, '_owner', lambda room_id: 'o' * 4096)
+ lines = []
+ server = _server(tmp_path)
+ real_handle = server.handle_request_line
+ def handle(raw, *args):
+ response = real_handle(raw, *args)
+ if b'"hosted-attest"' in raw:
+ lines.append(len(response))
+ return response
+ server.handle_request_line = handle
+ loop = asyncio.new_event_loop()
+ thread = threading.Thread(target=loop.run_forever)
+ thread.start()
+ transport.install_hosted_transport(server, source.authority, loop, attest=service.attest)
+ assert asyncio.run_coroutine_threadsafe(server.start(), loop).result()
+ try:
+ attested = service.attest(source.selector, 'submit', params)
+ assert transport._attachment_data(source.binding, attested, params) == [(source.bound[0], source.data)]
+ finally:
+ asyncio.run_coroutine_threadsafe(server.stop(), loop).result()
+ loop.call_soon_threadsafe(loop.stop)
+ thread.join()
+ loop.close()
+ assert len(lines) == math.ceil(len(source.data) / _CHUNK_BYTES) == 3
+ assert max(lines) <= _MAX_RESPONSE_BYTES
+ assert max(lines) > _MAX_RESPONSE_BYTES * 3 // 4, 'chunks leave most of the response line unused'