The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(api): room grants answer the clarify prompts their runs raise (X21, reported by @Xipong)
teknium15ecf42a99b38
diff --git a/gateway/platforms/api_server_runs.py b/gateway/platforms/api_server_runs.pyindex 27bba1ecf9c..c1c5c04795d 100644--- a/gateway/platforms/api_server_runs.py+++ b/gateway/platforms/api_server_runs.py@@ -186,11 +186,15 @@ def _make_run_event_callback(self, run_id: str, loop: "asyncio.AbstractEventLoopreturn _callback+# Answering a prompt the run raised is one capability whichever tool asked: approvals+# and clarify share the grant's ``approve`` permission.+_ROOM_CONTROL_PERMISSIONS = {"/stop": "stop", "/approval": "approve", "/clarify": "approve"}++def _room_permission_for(request: "web.Request") -> str:- if request.path.endswith("/stop"):- return "stop"- if request.path.endswith("/approval"):- return "approve"+ for suffix, permission in _ROOM_CONTROL_PERMISSIONS.items():+ if request.path.endswith(suffix):+ return permissionreturn "status" if request.method == "GET" else "dispatch"@@ -923,7 +927,7 @@ async def _respond_authority_run(self, run_id, body, *, kind, _api_server):async def _handle_run_clarify(self, request, *, _api_server):run_id, _, _, _, err = _load_owned_run(- self, request, _api_server=_api_server, permission=None, active_fallback=False)+ self, request, _api_server=_api_server, permission="approve", active_fallback=False)if err is not None:return errbody, err = await self._read_json_body(request)diff --git a/tests/gateway/test_api_canonical_projection.py b/tests/gateway/test_api_canonical_projection.pyindex db610547f2f..4b5ac3da644 100644--- a/tests/gateway/test_api_canonical_projection.py+++ b/tests/gateway/test_api_canonical_projection.py@@ -207,3 +207,58 @@ async def test_cancelled_observer_is_unregistered_and_never_breaks_the_survivor(assert survivor == [('call-1', 'read_file', {'path': 'a.txt'}, 'body')]assert cancelled_saw == []assert row['admission_id'] not in owner.api_observers+++async def test_room_grant_answers_the_clarify_prompt_its_run_raised(api, owner, tmp_path):+ """A room-scoped run's clarify prompt is answerable by the same room grant that dispatched+ it (no gateway API key), and a revoked grant can no longer answer."""+ import time+ from gateway import hosted_rooms+ from gateway.hosted_room_peer import decode_room_grant, issue_room_grant+ from gateway.platforms.api_server_authority_runs import run_projection+ from tools import clarify_gateway+ api._api_key = 'sk-secret'+ now = time.time()+ grant = issue_room_grant(+ api._room_grant_secret(), grant_id='grant-clarify', room_id='room-1', home_install_id='install-home',+ authority_gateway_id='install-home', authority_epoch=1, member_id='member-peer',+ target_install_id=hosted_rooms.local_authority_gateway_id(), target_profile='default',+ issued_at=now, ttl_seconds=300, status_expires_at=now + 1000)+ claims = decode_room_grant(api._room_grant_secret(), grant, permission='status')+ hosted_rooms.reserve_peer_room(hosted_rooms.default_db_path(), claims=claims, expires_at=now + 1000)+ headers = {'Authorization': f'HermesRoom {grant}'}+ admitted = admit_api_turn(api, session_id='room-clarify', request_id='run_room', user_message='hello',+ conversation_history=[])+ _, ref, row = admitted+ launch, queue = _launch(api, admitted, run_id='run_room')+ scope_request = SimpleNamespace(headers=headers, path='/v1/runs/run_room/clarify', method='POST')+ api._run_owners['run_room'] = api._run_idempotency_scope(scope_request)++ async def handle(event):+ from gateway.session_results import execution_result+ turn = _turn_runner(owner, ref)+ entry = clarify_gateway.register('clarify-room', 'api', 'Which one?', ['a', 'b'])+ owner.register_clarify(ref.session_id, turn._approval_owner[2], entry)+ await asyncio.to_thread(entry.event.wait, 10)+ execution_result.get()['result'] = {'final_response': entry.response or 'unanswered'}+ return entry.response+ owner.runner._handle_message = handle+ run = asyncio.ensure_future(_execute_run(api, launch, _api_server=_api_server))+ async with asyncio.timeout(10):+ while not (run_projection(api, 'run_room') or {}).get('pending_controls'):+ await asyncio.sleep(0.02)+ prompt = run_projection(api, 'run_room')['pending_controls'][0]+ body = {'request_id': prompt['prompt_id'], 'execution_generation': prompt['execution_generation'], 'answer': 'b'}+ app = web.Application()+ app.router.add_post('/v1/runs/{run_id}/clarify', api._handle_run_clarify)+ async with TestClient(TestServer(app)) as client:+ answered = await client.post('/v1/runs/run_room/clarify', json=body, headers=headers)+ assert answered.status == 200, await answered.text()+ assert (await answered.json())['status'] == 'resolved'+ await asyncio.wait_for(run, timeout=10)+ assert api._run_statuses['run_room']['output'] == 'b'+ hosted_rooms.revoke_room_grant_scope(hosted_rooms.default_db_path(), claims=claims, expires_at=now + 1000)+ denied = await client.post('/v1/runs/run_room/clarify', json=body, headers=headers)+ assert denied.status == 403+ assert (await denied.json())['error']['code'] == 'room_reauthorization_required'