Skip to content

The agent that grows with you

fix(gateway): the authority answers the Desktop's approval replay reads

Teknium1a4f063cbe77
diff --git a/gateway/session_ancillary.py b/gateway/session_ancillary.py
index 9a50cf05161..2dee1801b15 100644
--- a/gateway/session_ancillary.py
+++ b/gateway/session_ancillary.py
@@ -17,10 +17,36 @@ _TAIL_BYTES = 16384
def handlers(connection):
- return {name: partial(read, connection, kind=kind) for name, kind in {
+ return {**{name: partial(read, connection, kind=kind) for name, kind in {
'session.control.read': 'control', 'process.list': 'processes',
'subagent.list': 'subagents', 'subagent.tail': 'tail',
- }.items()}
+ }.items()}, 'approval.pending': partial(approvals, connection),
+ 'approval.received': partial(approvals, connection, ack=True)}
+
+
+async def approvals(connection, ref, params, *, ack=False):
+ """The Desktop's approval replay (``approval.pending``) and card ack (``approval.received``)
+ over the owner's generation-bound prompt projection. Left to the legacy sidecar they answered
+ ``session not found`` for every authority session, which the renderer reads as a reaped
+ runtime and answers with a mid-turn re-resume."""
+ fields = {'session_id', 'profile'} | ({'request_id'} if ack else set())
+ if (set(params) - fields or not isinstance(ref.session_id, str) or not ref.session_id
+ or (ack and (not isinstance(params.get('request_id'), str) or not params['request_id']))):
+ raise RuntimeStoreError('invalid_params')
+ authority = connection.authority
+ if ref.session_id not in authority.sessions:
+ raise RuntimeStoreError('not_found')
+ authorize(connection, ref, params, 'session:read')
+ live = authority.sessions[ref.session_id]
+ with live.event_stream.lock:
+ pending = [(route, prompt) for route, prompt in live.controls.pending.values() if prompt['kind'] == 'approval']
+ if ack:
+ from tools.approval import ack_gateway_approval
+ route = next((route for route, prompt in pending if prompt['prompt_id'] == params['request_id']), None)
+ return {'acknowledged': route is not None and ack_gateway_approval(route, params['request_id'])}
+ return {'approvals': [{'request_id': prompt['prompt_id'], 'command': prompt['command'],
+ 'description': prompt['description'], 'choices': list(prompt['choices']),
+ 'allow_permanent': 'always' in prompt['choices']} for _, prompt in pending]}
async def read(connection, ref, params, *, kind):
diff --git a/tests/gateway/test_authority_approval_projection.py b/tests/gateway/test_authority_approval_projection.py
new file mode 100644
index 00000000000..aa25aac9135
--- /dev/null
+++ b/tests/gateway/test_authority_approval_projection.py
@@ -0,0 +1,42 @@
+"""The authority answers the Desktop's approval replay reads for its own sessions.
+
+The Desktop replays ``approval.pending`` on every ``session.info`` of the routed session and acks
+each card with ``approval.received``. Unanswered by the authority, both fell through to the legacy
+sidecar, which holds no authority session and answered ``4001 session not found``; the renderer
+reads that as a reaped runtime and re-resumes the chat mid-turn, racing a stored-page read against
+the live reply (core e2e behind-window-send: the reply painted twice in the sending window).
+"""
+from types import SimpleNamespace
+
+import pytest
+
+from gateway.session_authority import LiveSession, SessionAuthority
+from gateway.session_controls import AuthorityConnection
+from hermes_state import SessionDB
+from hermes_state_runtime import begin_runtime_epoch
+
+
+async def test_approval_replay_reads_are_served_from_the_owner_projection(tmp_path):
+ with SessionDB(db_path=tmp_path / 'state.db') as db:
+ db.create_session('s', source='test')
+ epoch = begin_runtime_epoch(db, instance_id='current')
+ authority = SessionAuthority(SimpleNamespace(_draining=False), profile_id='owned',
+ instance_id='current', db=db, epoch=epoch)
+ live = authority.sessions['s'] = LiveSession(None, 'route')
+ live.controls.pending['p1'] = ('route', {
+ 'kind': 'approval', 'prompt_id': 'p1', 'execution_generation': 1,
+ 'command': 'rm -rf build', 'description': 'recursive delete', 'choices': ['once', 'deny', 'always']})
+ live.controls.pending['c1'] = (object(), {'kind': 'clarify', 'prompt_id': 'c1', 'execution_generation': 1,
+ 'question': 'which?', 'choices': [], 'multi_select': False})
+ viewer = AuthorityConnection(authority, object(), {'user_id': 'owner'})
+ try:
+ pending = await viewer.dispatch({'id': 1, 'method': 'approval.pending', 'params': {'session_id': 's'}})
+ assert pending.get('result') == {'approvals': [{
+ 'request_id': 'p1', 'command': 'rm -rf build', 'description': 'recursive delete',
+ 'choices': ['once', 'deny', 'always'], 'allow_permanent': True}]}, pending
+ received = await viewer.dispatch({'id': 2, 'method': 'approval.received',
+ 'params': {'session_id': 's', 'request_id': 'p1'}})
+ assert received.get('result') == {'acknowledged': False}, received
+ finally:
+ await viewer.close()