Skip to content

The agent that grows with you

fix: bind shared authority when starting gateway API

Teknium11958d461893
diff --git a/gateway/run_api.py b/gateway/run_api.py
index b5b4a726107..c1474dd06da 100644
--- a/gateway/run_api.py
+++ b/gateway/run_api.py
@@ -42,6 +42,7 @@ async def start_gateway_api(runner, *, host: str = "127.0.0.1", port: int = 0) -
raise
web.app.state.gateway_runner = runner
+ web.app.state.session_authority = getattr(runner, "session_authority", None)
web.app.state.bound_host = host
web.app.state.bound_port = listener.getsockname()[1]
try:
@@ -54,6 +55,7 @@ async def start_gateway_api(runner, *, host: str = "127.0.0.1", port: int = 0) -
except BaseException:
listener.close()
web.app.state.gateway_runner = None
+ web.app.state.session_authority = None
if hasattr(server, "lifespan") and not server.lifespan.should_exit:
await server.lifespan.shutdown()
raise
@@ -67,6 +69,7 @@ async def start_gateway_api(runner, *, host: str = "127.0.0.1", port: int = 0) -
finally:
listener.close()
web.app.state.gateway_runner = None
+ web.app.state.session_authority = None
task = asyncio.create_task(serve(), name="gateway-api")
origin_host = f"[{host}]" if ":" in host else host
diff --git a/tests/gateway/test_runtime_api.py b/tests/gateway/test_runtime_api.py
index 60cc02b40a5..fdec8851f79 100644
--- a/tests/gateway/test_runtime_api.py
+++ b/tests/gateway/test_runtime_api.py
@@ -31,7 +31,8 @@ async def test_http_lifespan_does_not_start_or_stop_runtime_services(monkeypatch
@pytest.mark.asyncio
-async def test_owner_serves_existing_routes_and_ws_without_signal_ownership(monkeypatch, tmp_path):
[email protected]('shared', [False, True])
+async def test_owner_serves_existing_routes_and_ws_without_signal_ownership(monkeypatch, tmp_path, shared):
from gateway.run_api import start_gateway_api, stop_gateway_api
from hermes_cli import web_server as web
@@ -40,6 +41,14 @@ async def test_owner_serves_existing_routes_and_ws_without_signal_ownership(monk
runner = GatewayRunner(GatewayConfig(sessions_dir=tmp_path / "gateway-sessions"))
assert runner.adapters == {}
+ request = {"jsonrpc": "2.0", "id": 1, "method": "ping", "params": {}}
+ if shared:
+ from gateway.config import Platform
+ from gateway.session import SessionSource
+ from gateway.session_authority import initialize_session_authority
+ authority = await initialize_session_authority(runner, profile_id='fixture', instance_id='api-owner')
+ ref = authority.register(SessionSource(platform=Platform.TELEGRAM, chat_id='api-test', user_id='fixture'))
+ request.update(method='session.resume', params={'session_id': ref.session_id})
before = {sig: signal.getsignal(sig) for sig in (signal.SIGINT, signal.SIGTERM)}
handle = await start_gateway_api(runner)
try:
@@ -59,19 +68,24 @@ async def test_owner_serves_existing_routes_and_ws_without_signal_ownership(monk
pass
assert rejected.value.response.status_code == 403
async with connect(url + "?token=" + web._SESSION_TOKEN) as ws:
- await ws.send(json.dumps({"jsonrpc": "2.0", "id": 1, "method": "ping", "params": {}}))
+ await ws.send(json.dumps(request))
while True:
reply = json.loads(await asyncio.wait_for(ws.recv(), 10))
if reply.get("id") == 1:
break
assert "result" in reply, reply
+ if shared:
+ assert reply['result'].get('authority_epoch') == authority.epoch, reply
+ assert reply['result']['session_id'] == ref.session_id
+ assert len(authority.sessions[ref.session_id].subscribers) == 1
with pytest.raises(RuntimeError, match="already"):
await start_gateway_api(runner)
finally:
await stop_gateway_api(handle)
if runner._session_db is not None:
- runner._session_db.close()
+ await runner._session_db.close()
await stop_gateway_api(handle)
+ assert web.app.state.session_authority is None
assert before == {sig: signal.getsignal(sig) for sig in before}
with pytest.raises(httpx.ConnectError):
async with httpx.AsyncClient() as client: