The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: commit in-process turn results under the publish lock
Teknium16d36f79c67c
diff --git a/gateway/session_authority.py b/gateway/session_authority.pyindex 3a47853201b..18fd385339d 100644--- a/gateway/session_authority.py+++ b/gateway/session_authority.py@@ -46,6 +46,7 @@ class SessionAuthority:self.waiters = {}self.events = {}self.native_waiters = set()+ self.pending_results = {}def authorize(self, actor, ref, capability):if actor.profile_id != self.profile_id or ref.profile_id != self.profile_id:@@ -352,7 +353,8 @@ class SessionAuthority:with live.event_stream.lock:from gateway.session_results import finish_resultsettled, response = finish_result(self.db, epoch=self.epoch, row=row,- response=response, outcome=outcome)+ response=response, outcome=outcome,+ result=self.pending_results.pop(admission_id, None))live.controls.snapshot(ref.session_id, None)self._publish_pending(ref)live.event_stream.publish(ref.session_id, {diff --git a/gateway/session_ingress.py b/gateway/session_ingress.pyindex 7fed10311b8..7957dd09c29 100644--- a/gateway/session_ingress.py+++ b/gateway/session_ingress.py@@ -45,7 +45,7 @@ async def execute_admission(authority, ref, row):scope = _profile_runtime_scope(home)from gateway.config import Platformfrom gateway.session_api_turn import api_execution, prepare_api_execution- from gateway.session_results import execution_result, retain_result+ from gateway.session_results import execution_resultis_api = live.source.platform == Platform.API_SERVERprepared = prepare_api_execution(authority, ref, row['payload']) if is_api else Noneif is_api:@@ -58,8 +58,9 @@ async def execute_admission(authority, ref, row):with scope:response = await authority.runner._handle_message(event)result = captured.get('result') or {'final_response': response or '', 'messages': []}- retain_result(authority.db, epoch=authority.epoch, row=row,- result={'result': result, 'usage': captured.get('usage', {})})+ # The drain commits this under the stream lock so no viewer reads `terminal`+ # before the completion event exists in the replay ring.+ authority.pending_results[row['admission_id']] = {'result': result, 'usage': captured.get('usage', {})}if not native and not is_api and response:adapter = authority.runner._adapter_for_source(event.source)if adapter is not None:diff --git a/gateway/session_results.py b/gateway/session_results.pyindex d4c5bc667b1..eba335b209f 100644--- a/gateway/session_results.py+++ b/gateway/session_results.py@@ -13,8 +13,12 @@ def retain_result(db, *, epoch, row, result):generation=row['generation'], outcome='completed', result=result)-def finish_result(db, *, epoch, row, response, outcome):- """Delivery failure cannot rewrite an already committed execution outcome."""+def finish_result(db, *, epoch, row, response, outcome, result=None):+ """Delivery failure cannot rewrite an already committed execution outcome.++ `result` is the exact structured result captured in-process; the managed+ worker path commits its own before this runs and is read back here.+ """with db._read_ctx() as conn:_epoch(conn, epoch)current = _admission(conn, row['admission_id'])@@ -27,9 +31,10 @@ def finish_result(db, *, epoch, row, response, outcome):raise RuntimeStoreError('storage_unavailable')result = json.loads(saved[0])return dict(current), result['result'].get('final_response') or ''+ if result is None:+ result = {'result': {'final_response': response or '', 'messages': []}, 'usage': {}}settled = settle_session_input(db, epoch=epoch, admission_id=row['admission_id'],- generation=row['generation'], outcome=outcome,- result={'result': {'final_response': response or '', 'messages': []}, 'usage': {}})+ generation=row['generation'], outcome=outcome, result=result)return settled, responsediff --git a/tests/gateway/fixtures/terminal_commit_daemon.py b/tests/gateway/fixtures/terminal_commit_daemon.pyindex 1d5e1cb07e5..2504f037f72 100644--- a/tests/gateway/fixtures/terminal_commit_daemon.py+++ b/tests/gateway/fixtures/terminal_commit_daemon.py@@ -4,15 +4,15 @@ import runpyimport signalfrom gateway import session_results-retain = session_results.retain_result+finish = session_results.finish_result-def stop_after_commit(db, *, epoch, row, result):- receipt = retain(db, epoch=epoch, row=row, result=result)+def stop_after_commit(db, *, epoch, row, response, outcome, result=None):+ receipt = finish(db, epoch=epoch, row=row, response=response, outcome=outcome, result=result)if row['payload'].get('text') == 'BLOCK_STARTED':os.kill(os.getpid(), signal.SIGSTOP)return receipt-session_results.retain_result = stop_after_commit+session_results.finish_result = stop_after_commitrunpy.run_module('gateway.run', run_name='__main__')diff --git a/tests/gateway/test_bot_result_recovery.py b/tests/gateway/test_bot_result_recovery.pyindex 273cd4aeb7c..b1ee1b812a0 100644--- a/tests/gateway/test_bot_result_recovery.py+++ b/tests/gateway/test_bot_result_recovery.py@@ -4,7 +4,7 @@ from types import SimpleNamespacefrom hermes_state import SessionDBfrom hermes_state_runtime import (admit_session_input, begin_runtime_epoch, claim_session_input,- recover_session_inputs, settle_session_input,+ recover_session_inputs,)from gateway.session_results import retain_resultfrom gateway.session_bot import _result@@ -22,10 +22,9 @@ def test_bot_reply_recovery_uses_exact_terminal_admission(tmp_path):admission = admit_session_input(db, epoch=epoch, principal_id='owner', session_id='bot',request_id=key, payload={'text': key})row = claim_session_input(db, epoch=epoch, session_id='bot')- retain_result(db, epoch=epoch, row=row, result={'result': {'final_response': reply}, 'usage': {}})+ # retain_result IS the settlement: the result commits with terminal status or not at all.if settle:- settle_session_input(db, epoch=epoch, admission_id=row['admission_id'],- generation=row['generation'], outcome='completed')+ retain_result(db, epoch=epoch, row=row, result={'result': {'final_response': reply}, 'usage': {}})records.append(dict(status='canonical', admission_id=admission['admission_id'],delivery_id=key, profile_home=str(tmp_path), session_id='bot'))db.close()