The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: publish hosted file shares with cursor-fenced retries
Teknium49e27d8b4c9a
diff --git a/tests/tui_gateway/test_hosted_file_publication.py b/tests/tui_gateway/test_hosted_file_publication.pynew file mode 100644index 00000000000..58f4ebfcb09--- /dev/null+++ b/tests/tui_gateway/test_hosted_file_publication.py@@ -0,0 +1,65 @@+"""Service share and raced publication preserve the original durable task."""+import time+from types import SimpleNamespace++from gateway import hosted_rooms, hosted_room_driver as driver+from gateway.hosted_room_attachments import HostedRoomAttachmentStore+from tui_gateway.hosted_room_service import HostedRoomService+++def service_at(db):+ service = HostedRoomService(SimpleNamespace(), db_path=db)+ service.local_profiles = lambda: ('default', 'ops')+ return service+++def test_service_share_retains_bytes_and_reuses_frozen_task(tmp_path):+ db = tmp_path / 'state.db'+ service = service_at(db)+ service.create_room(room_id='room', name='Room', members=[+ {'member_id': 'default', 'profile': 'default', 'handle': 'hermes'},+ {'member_id': 'ops', 'profile': 'ops', 'handle': 'ops'}])+ store = HostedRoomAttachmentStore(db)+ item = store.put(room_id='room', upload_id='upload', kind='file', name='proof.txt',+ mime='application/octet-stream', data=b'\x00exact shared bytes\xff')+ manifest = [{k: item[k] for k in ('attachment_id', 'kind', 'name', 'mime', 'size')}]+ event = service.send(room_id='room', event_id='share', payload={+ 'text': '@hermes inspect', 'thread_id': 'thread', 'attachments': manifest})+ before = driver.list_tasks(db, room_id='room')[0]+ assert before['payload']['attachments'][0]['event_id'] == event['event_id']+ restored = service_at(db)+ restored.prepare_room(restored.bindings()[0])+ assert driver.list_tasks(db, room_id='room')[0]['payload'] == before['payload']+ assert HostedRoomAttachmentStore(db, clock=lambda: time.time() + 999999).read_viewer(+ room_id='room', attachment_id=item['attachment_id'], event_id='share',+ authority_gateway_id=service.bindings()[0].gateway_id, authority_epoch=1).data == b'\x00exact shared bytes\xff'+++def test_publication_cursor_race_retries_without_new_task(tmp_path, monkeypatch):+ db = tmp_path / 'state.db'+ service = service_at(db)+ service.create_room(room_id='room', name='Room', members=[+ {'member_id': 'default', 'profile': 'default', 'handle': 'hermes'},+ {'member_id': 'ops', 'profile': 'ops', 'handle': 'ops'}])+ service.send(room_id='room', event_id='share', payload={'text': '@hermes inspect', 'thread_id': 'thread'})+ binding = service.bindings()[0]+ task = driver.list_tasks(db, room_id='room')[0]+ lease = driver.acquire_lease(db, room_id='room', gateway_id=binding.gateway_id,+ authority_epoch=binding.authority_epoch, process_generation='test', ttl_seconds=30, clock=time.time)+ attempt = driver.start_task(db, task['identity'], lease, expected_cancel_generation=0, clock=time.time)+ driver.settle_task(db, attempt, settlement_id='result', status='settled', result={'text':'done'}, clock=time.time)+ append = hosted_rooms.append_event+ raced = []+ def interleave(path, **kwargs):+ if kwargs['kind'] == 'message.member' and not raced:+ raced.append(True)+ append(path, room_id='room', event_id='race', kind='room.activity',+ actor={'kind':'gateway','id':binding.gateway_id}, payload={'status':'idle'},+ authority_gateway_id=binding.gateway_id, authority_epoch=binding.authority_epoch)+ return append(path, **kwargs)+ monkeypatch.setattr(hosted_rooms, 'append_event', interleave)+ service.prepare_room(binding)+ assert not any(e['kind'] == 'message.member' for e in service._events('room'))+ service.prepare_room(binding)+ assert sum(e['kind'] == 'message.member' for e in service._events('room')) == 1+ assert driver.get_task_for_turn(db, task['identity'])['execution_generation'] == attempt.execution_generationdiff --git a/tui_gateway/hosted_room_service.py b/tui_gateway/hosted_room_service.pyindex cc5ace10cc9..6e20c69acd3 100644--- a/tui_gateway/hosted_room_service.py+++ b/tui_gateway/hosted_room_service.py@@ -367,6 +367,7 @@ class HostedRoomService:def _publish_terminal_tasks(self, room: Mapping[str, Any]) -> bool:changed, room_id, local_profiles = False, str(room["room_id"]), self.local_profiles()+ cursor = int(room["latest_seq"])for task in self._list_tasks(room_id, _TERMINAL_STATUSES):status, execution_generation = task["status"], int(task["execution_generation"])if self.policy_checkpoint.publication_exists(@@ -388,7 +389,9 @@ class HostedRoomService:execution_generation=execution_generation if status == "deferred" else None,local_profiles=local_profiles)for event in publication.events:- hosted_rooms.append_event(self.db_path, **event.append_kwargs(room_id))+ appended = hosted_rooms.append_event(+ self.db_path, **event.append_kwargs(room_id), expected_latest_seq=cursor)+ cursor = max(cursor, int(appended["seq"]))changed = Truereturn changed@@ -411,7 +414,12 @@ class HostedRoomService:with self._policy_lock:room = self._room(binding.room_id)snapshot = self._policy_snapshot(room) # sync() side effect feeds the publish below- if self._publish_terminal_tasks(room):+ try:+ changed = self._publish_terminal_tasks(room)+ except hosted_rooms.EventCursorConflictError:+ # Rebuild publication next poll; the settled task is never readmitted.+ return+ if changed:room = self._room(binding.room_id)snapshot = self._policy_snapshot(room)self.policy_checkpoint.compact_completed(room_id=binding.room_id)@@ -470,10 +478,10 @@ class HostedRoomService:def send(self, *, room_id: str, event_id: str, payload: Any) -> dict[str, Any]:normalized = discussion.validate_user_payload(payload)gateway_id, epoch = self._owned_authority(room_id)- event = hosted_rooms.append_event(- self.db_path, room_id=room_id, event_id=event_id, kind="message.user",- actor={"kind": "user", "id": "desktop"}, payload=normalized,- authority_gateway_id=gateway_id, authority_epoch=epoch)+ from gateway.session_hosted_attachments import append_user_event+ event = append_user_event(+ self, room_id=room_id, event_id=event_id, payload=normalized,+ gateway_id=gateway_id, epoch=epoch)binding = next((b for b in self.bindings() if b.room_id == room_id), None)if binding is None:raise hosted_rooms.RoomNotFoundError("hosted room not found")