Skip to content

The agent that grows with you

fix: retain browser authentication and withdraw native ingress on drain

Teknium5da456be8025
diff --git a/gateway/run_api.py b/gateway/run_api.py
index 9f8d477af6a..f33d9d16b55 100644
--- a/gateway/run_api.py
+++ b/gateway/run_api.py
@@ -119,7 +119,7 @@ class GatewayRuntimeAPI:
scope['app'] = self.web_app
ws = WebSocket(scope, receive, send)
ticket, reason = _gateway_ws_ticket_from_subprotocol(ws)
- if reason == 'none':
+ if reason == 'none' or ws.headers.get('origin'):
return await self.app(scope, receive, send)
from hermes_cli import web_server as web
if (reason != 'ok' or not web._DASHBOARD_EMBEDDED_CHAT_ENABLED
@@ -132,8 +132,9 @@ class GatewayRuntimeAPI:
ticket, profile_id=self.runner.session_authority.profile_id,
purpose='interactive')
except PermissionError:
- await ws.close(code=4401)
- return
+ # Browser/OAuth tickets use the same protocol but a different
+ # issuer. Let the existing gate validate them, never mint a grant.
+ return await self.app(scope, receive, send)
from tui_gateway.ws import handle_ws
await handle_ws(ws, auth_identity={'user_id': grant['subject'], 'provider': 'local'},
subprotocol='hermes-gateway-v1')
diff --git a/gateway/run_runtime.py b/gateway/run_runtime.py
index 7b8620ed7b4..325fb41c6a8 100644
--- a/gateway/run_runtime.py
+++ b/gateway/run_runtime.py
@@ -55,6 +55,10 @@ async def drain_gateway_runtime(runner):
if descriptor is None:
return
descriptor.update(state='draining', capabilities=[])
+ # Withdraw the public ingress callback without disconnecting egress needed
+ # by already admitted work. Base adapters refuse before stamping acceptance.
+ for adapter in runner.adapters.values():
+ adapter.set_message_handler(None)
store = getattr(runner, 'session_ticket_store', None)
if store is not None:
store.revoke()
diff --git a/tests/gateway/test_runtime_bootstrap_lifecycle.py b/tests/gateway/test_runtime_bootstrap_lifecycle.py
new file mode 100644
index 00000000000..9d1aac78c26
--- /dev/null
+++ b/tests/gateway/test_runtime_bootstrap_lifecycle.py
@@ -0,0 +1,98 @@
+"""Live runtime HTTP lifecycle boundaries, with no provider inference."""
+import asyncio
+import json
+
+import pytest
+from websockets.asyncio.client import connect
+
+
+async def test_runtime_preserves_browser_ticket_gate(tmp_path):
+ from gateway.config import GatewayConfig
+ from gateway.run import GatewayRunner
+ from gateway.run_runtime import (initialize_gateway_runtime, start_gateway_runtime_api,
+ publish_gateway_runtime_ready)
+ from gateway.runtime_ownership import process_ownership
+ from hermes_constants import get_hermes_home
+ from hermes_cli.dashboard_auth import register_provider, clear_providers
+ from hermes_cli.dashboard_auth.ws_tickets import mint_ticket
+ from tests.hermes_cli.conftest_dashboard_auth import StubAuthProvider
+
+ home = get_hermes_home()
+ (home / 'config.yaml').write_text('dashboard:\n public_url: https://gateway.example.test\n')
+ register_provider(StubAuthProvider())
+ process_ownership.reserve([home])
+ runner = GatewayRunner(GatewayConfig())
+ try:
+ await initialize_gateway_runtime(runner)
+ await start_gateway_runtime_api(runner)
+ assert await runner.start()
+ publish_gateway_runtime_ready(runner)
+ ticket = mint_ticket(user_id='browser-owner', provider='stub')
+ url = runner.session_api.api_origin.replace('http:', 'ws:') + '/api/ws'
+ async with connect(url, origin='https://gateway.example.test', subprotocols=[
+ 'hermes-gateway-v1', 'hermes-gateway-ticket.' + ticket]) as ws:
+ await ws.send(json.dumps({'jsonrpc': '2.0', 'id': 1, 'method': 'gateway.ping'}))
+ while True:
+ reply = json.loads(await asyncio.wait_for(ws.recv(), 10))
+ if reply.get('id') == 1:
+ break
+ assert reply['result']['ok'] is True
+ finally:
+ await runner.stop()
+ process_ownership.close()
+ clear_providers()
+
+
+async def test_drain_withdraws_native_admission_before_listener_close(tmp_path):
+ from gateway.config import GatewayConfig, Platform, PlatformConfig
+ from gateway.platforms.base import BasePlatformAdapter, SendResult
+ from gateway.platforms.event import MessageEvent
+ from gateway.run import GatewayRunner
+ from gateway.run_runtime import initialize_gateway_runtime, drain_gateway_runtime
+ from gateway.runtime_ownership import process_ownership
+ from gateway.session import SessionSource
+ from hermes_constants import get_hermes_home
+
+ class LocalAdapter(BasePlatformAdapter):
+ async def connect(self, *, is_reconnect=False):
+ return True
+
+ async def disconnect(self):
+ pass
+
+ async def get_chat_info(self, chat_id):
+ return {'id': chat_id}
+
+ async def send(self, *args, **kwargs):
+ return SendResult(success=True)
+
+ home = get_hermes_home()
+ process_ownership.reserve([home])
+ runner = GatewayRunner(GatewayConfig())
+ adapter = LocalAdapter(PlatformConfig(enabled=True), Platform.TELEGRAM)
+ handled = asyncio.Event()
+
+ async def handler(event):
+ handled.set()
+ return 'local control'
+
+ adapter.set_message_handler(handler)
+ runner.adapters[Platform.TELEGRAM] = adapter
+ source = SessionSource(platform=Platform.TELEGRAM, chat_id='owned', user_id='owned')
+ try:
+ await initialize_gateway_runtime(runner)
+ first = MessageEvent(text='/status', source=source)
+ await adapter.handle_message(first)
+ await asyncio.wait_for(handled.wait(), 5)
+ assert first._gateway_accepted
+ await drain_gateway_runtime(runner)
+ assert runner.session_runtime_descriptor['state'] == 'draining'
+ later = MessageEvent(text='/status', source=SessionSource(
+ platform=Platform.TELEGRAM, chat_id='another-owned-route', user_id='owned'))
+ await adapter.handle_message(later)
+ assert not later._gateway_accepted
+ finally:
+ await runner.stop()
+ process_ownership.close()