Skip to content

The agent that grows with you

fix(gateway): a secondary with an unusable state.db parks itself, not the boot

teknium1eb7ff90043fe
diff --git a/gateway/run_runtime.py b/gateway/run_runtime.py
index 13c691ded67..07fb758d56c 100644
--- a/gateway/run_runtime.py
+++ b/gateway/run_runtime.py
@@ -6,7 +6,8 @@ profile's runtime scope against that profile's ``state.db``. ``runner.session_au
stays the launch profile's authority so single-profile behaviour is byte-identical.
The served set is not frozen at boot: ``serve_profile_runtime`` / ``unserve_profile_runtime``
-grow and shrink it for the hot-serve reconcile.
+grow and shrink it for the hot-serve reconcile, and a secondary whose store cannot be opened
+is parked (logged, left out of ``served_profiles``) instead of aborting every other profile.
"""
from __future__ import annotations
@@ -55,6 +56,16 @@ async def _build_profile_authority(runner, name, home, *, register):
return authority
+def _park_reserved_profile(runner, name, home, exc):
+ """A secondary whose store is unusable is parked: logged, unreserved, left out of
+ ``served_profiles`` and named under ``parked_profiles`` in the runtime descriptor. Boot
+ continues for every other profile (a runtime hot-add parks the same way)."""
+ logger.error("[MULTIPLEX] Profile '%s' not served: its session store is unusable (%s): %s",
+ name, home, exc)
+ release_profile_home(runner, home)
+ runner.session_runtime_descriptor.setdefault('parked_profiles', []).append(name)
+
+
async def initialize_gateway_runtime(runner):
from gateway.runtime_bootstrap import TicketStore
from gateway.runtime_ownership import process_ownership
@@ -74,7 +85,12 @@ async def initialize_gateway_runtime(runner):
registry = SessionAuthorities(homes[0][1])
runner.session_authorities = registry
for index, (name, home) in enumerate(homes):
- await _build_profile_authority(runner, name, home, register=index == 0)
+ try:
+ await _build_profile_authority(runner, name, home, register=index == 0)
+ except Exception as exc:
+ if index == 0:
+ raise # the launch profile's store is the process's own; nothing to park it behind
+ _park_reserved_profile(runner, name, home, exc)
descriptor['authority_epoch'] = registry.launch.epoch
descriptor['served_profiles'] = registry.served_profiles()
runner.session_ticket_store = TicketStore(instance_id, registry.profile_ids())
diff --git a/tests/gateway/test_multiplex_hot_serve_live.py b/tests/gateway/test_multiplex_hot_serve_live.py
new file mode 100644
index 00000000000..3d44af06bfe
--- /dev/null
+++ b/tests/gateway/test_multiplex_hot_serve_live.py
@@ -0,0 +1,202 @@
+"""Live hot-serve contract of a real ``python -m gateway.run`` multiplexer (Linux; loopback model).
+
+A profile created under the running multiplexer is served by it without a restart: ``identify``
+lists the new home, a ticket minted for it opens a session owned by this process, and ``profiles/``
+(not the boot-time reservation) is what the next restart reserves — so ``hermes -p new`` finds an
+owner instead of spawning a stray per-profile daemon that would later collide with the
+multiplexer's all-or-nothing reserve. A secondary whose ``state.db`` is unusable parks only itself.
+"""
+import asyncio
+import json
+import os
+from pathlib import Path
+import signal
+import socket
+import subprocess
+import sys
+import time
+
+import pytest
+from websockets.asyncio.client import connect
+
+ROOT = Path(__file__).resolve().parents[2]
+# The daemon is a foreground child of the test (never detached), boots a disposable custom root
+# with HOME redirected (no systemd unit, no webhook port) and is reaped in every path below.
+pytestmark = [pytest.mark.linux_only, pytest.mark.spawns_gateway_lookalike]
+
+
+def control(home, verb, params=None, timeout=10):
+ request = json.dumps({'protocol': 1, 'id': 1, 'verb': verb, 'params': params or {}}).encode() + b'\n'
+ from gateway.control_socket import resolve_client_socket_path
+ path = resolve_client_socket_path(home)
+ if path is None:
+ raise FileNotFoundError('control socket absent')
+ with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as peer:
+ peer.settimeout(timeout)
+ peer.connect(str(path))
+ peer.sendall(request)
+ with peer.makefile('rb') as stream:
+ raw = stream.readline()
+ reply = json.loads(raw.split(b'\n', 1)[0])
+ assert reply.get('ok'), reply
+ return reply['result']
+
+
+def peer():
+ from http.server import ThreadingHTTPServer
+ import threading
+ from tests.gateway.fixtures.shared_authority_peer import ModelPeer
+ server = ThreadingHTTPServer(('127.0.0.1', 0), ModelPeer)
+ server.requests, server.metadata_requests = [], []
+ thread = threading.Thread(target=server.serve_forever, daemon=True)
+ thread.start()
+ try:
+ yield server
+ finally:
+ server.shutdown()
+ server.server_close()
+ thread.join(timeout=5)
+
+
+def mux(tmp_path, peer):
+ """Disposable custom root (``<root>/profiles/<name>`` layout, as in Docker) with
+ ``multiplex_profiles`` on, plus one boot-time secondary."""
+ user = tmp_path / 'user'
+ user.mkdir()
+ root = tmp_path / 'state'
+ root.mkdir(mode=0o700)
+ url = f'http://127.0.0.1:{peer.server_port}/v1'
+ config = {'gateway': {'multiplex_profiles': True},
+ 'model': {'provider': 'custom', 'default': 'local-wire-stub', 'base_url': url},
+ 'auxiliary': {'title_generation': {'enabled': False}}}
+ (root / 'config.yaml').write_text(json.dumps(config))
+ boot = root / 'profiles' / 'boot'
+ boot.mkdir(parents=True, mode=0o700)
+ (boot / 'config.yaml').write_text(json.dumps(config))
+ env = {k: os.environ[k] for k in ('PATH', 'LANG', 'TZ') if k in os.environ}
+ env.update(HOME=str(user), USERPROFILE=str(user), HERMES_HOME=str(root), PYTHONPATH=str(ROOT),
+ PYTHONUNBUFFERED='1', OPENAI_API_KEY='loopback-only', OPENAI_BASE_URL=url)
+ return {'root': root, 'boot': boot, 'env': env, 'config': config, 'log': tmp_path / 'gateway.log'}
+
+
+def start_daemon(mux, timeout=90):
+ """Boot the multiplexer the way ``hermes gateway run`` does; the canonical gateway command-line
+ matcher (``gateway.status.looks_like_gateway_command_line``) is what the CLI's liveness probes
+ apply to the recorded PID, so the fixture must spawn a recognisable launcher."""
+ log = mux['log'].open('a+', encoding='utf-8', errors='replace')
+ proc = subprocess.Popen([sys.executable, '-m', 'hermes_cli.main', 'gateway', 'run', '--quiet'], cwd=ROOT,
+ env=mux['env'], stdin=subprocess.DEVNULL, stdout=log, stderr=subprocess.STDOUT)
+ desc = {}
+ deadline = time.monotonic() + timeout
+ while proc.poll() is None and time.monotonic() < deadline:
+ try:
+ desc = control(mux['root'], 'identify', timeout=2)
+ if desc.get('state') == 'ready':
+ break
+ except (OSError, ValueError, AssertionError):
+ pass
+ time.sleep(.1)
+ log.flush()
+ assert desc.get('state') == 'ready', (desc, proc.poll(), tail(mux))
+ return proc, desc
+
+
+def tail(mux, n=6000):
+ """stdout capture plus the daemon's own ``logs/gateway.log`` (``--quiet`` sends records there)."""
+ texts = [p.read_text(encoding='utf-8', errors='replace') for p in (mux['log'], mux['root'] / 'logs' / 'gateway.log')
+ if p.exists()]
+ return '\n'.join(t[-n:] for t in texts)
+
+
+def stop(proc, expect=0):
+ if proc.poll() is None:
+ proc.send_signal(signal.SIGINT)
+ try:
+ code = proc.wait(timeout=45)
+ except subprocess.TimeoutExpired:
+ proc.kill()
+ proc.wait(timeout=10)
+ raise
+ if expect is not None:
+ assert code == expect, code
+
+
+def served_homes(desc):
+ return {Path(entry['home']).resolve() for entry in desc.get('served_profiles', [])}
+
+
+def recorded_served(root):
+ return json.loads((root / 'gateway_state.json').read_text(encoding='utf-8')).get('served_profiles')
+
+
+async def open_session_for(root, home, desc):
+ """A ticket bound to *home* opens a session the multiplexer commits into *home*'s state.db."""
+ grant = control(root, 'session-ticket', {'profile_id': str(home.resolve()),
+ 'instance_id': desc['instance_id'], 'purpose': 'interactive'})
+ url = desc['api_origin'].replace('http:', 'ws:') + '/api/ws'
+ async with connect(url, subprotocols=['hermes-gateway-v1', 'hermes-gateway-ticket.' + grant['ticket']]) as ws:
+ await ws.send(json.dumps({'jsonrpc': '2.0', 'id': 1, 'method': 'session.create',
+ 'params': {'request_id': 'hot-served', 'source': 'cli'}}))
+ async with asyncio.timeout(20):
+ while True:
+ reply = json.loads(await ws.recv())
+ if reply.get('id') == 1:
+ assert 'result' in reply, reply
+ return reply['result']['session_id']
+
+
+def test_profile_created_under_live_multiplexer_is_served_and_owned(mux):
+ root, env = mux['root'], mux['env']
+ proc, desc = start_daemon(mux)
+ try:
+ assert served_homes(desc) == {root.resolve(), mux['boot'].resolve()}
+ # ``hermes profile create`` under the multiplexer's root: the CLI signals the control socket.
+ created = subprocess.run([sys.executable, '-m', 'hermes_cli.main', 'profile', 'create', 'fresh'],
+ cwd=ROOT, env=env, capture_output=True, text=True, timeout=120)
+ assert created.returncode == 0, (created.stdout, created.stderr)
+ fresh = root / 'profiles' / 'fresh'
+ assert fresh.is_dir()
+ deadline = time.monotonic() + 20
+ while time.monotonic() < deadline and fresh.resolve() not in served_homes(desc):
+ time.sleep(.2)
+ desc = control(root, 'identify')
+ assert fresh.resolve() in served_homes(desc), (desc, tail(mux))
+ assert recorded_served(root) == ['default', 'boot', 'fresh']
+ # The new profile's sessions are this multiplexer's: same instance, its own state.db.
+ sid = asyncio.run(open_session_for(root, fresh, desc))
+ import sqlite3
+ with sqlite3.connect(f"file:{fresh / 'state.db'}?mode=ro", uri=True) as db:
+ assert db.execute('SELECT instance_id FROM runtime_epoch').fetchone() == (desc['instance_id'],)
+ assert db.execute('SELECT 1 FROM sessions WHERE id=?', (sid,)).fetchone()
+ # ``hermes -p fresh`` resolves to the multiplexer instead of spawning a daemon of its own.
+ ensured = subprocess.run([sys.executable, '-m', 'hermes_cli.main', '-p', 'fresh', 'gateway', 'ensure',
+ '--timeout', '15'], cwd=ROOT, env=env, capture_output=True, text=True, timeout=60)
+ payload = json.loads(ensured.stdout.strip().splitlines()[-1])
+ assert payload['state'] == 'ready' and payload['endpoint']['instance_id'] == desc['instance_id'], (payload, ensured.stderr)
+ assert not (fresh / 'gateway.pid').exists()
+ stop(proc)
+ # The restart reserves what exists now, the hot-served profile included: no OwnershipConflict.
+ proc, desc = start_daemon(mux)
+ assert served_homes(desc) == {root.resolve(), mux['boot'].resolve(), fresh.resolve()}
+ assert 'Cannot reserve gateway profiles' not in tail(mux)
+ finally:
+ stop(proc, expect=None)
+
+
+def test_broken_secondary_state_db_parks_only_that_profile(mux):
+ root = mux['root']
+ broken = root / 'profiles' / 'broken'
+ broken.mkdir(parents=True, mode=0o700)
+ (broken / 'config.yaml').write_text(json.dumps(mux['config']))
+ # Not a SQLite file and not the zero-byte case the quarantine repairs: an unopenable store.
+ (broken / 'state.db').mkdir()
+ proc, desc = start_daemon(mux)
+ try:
+ assert served_homes(desc) == {root.resolve(), mux['boot'].resolve()}, desc
+ assert recorded_served(root) == ['default', 'boot']
+ assert "Profile 'broken' not served" in tail(mux), tail(mux)
+ stop(proc)
+ finally:
+ stop(proc, expect=None)
diff --git a/website/docs/user-guide/multi-profile-gateways.md b/website/docs/user-guide/multi-profile-gateways.md
index a3b45cb85de..10c8da62591 100644
--- a/website/docs/user-guide/multi-profile-gateways.md
+++ b/website/docs/user-guide/multi-profile-gateways.md
@@ -471,6 +471,15 @@ one-credential-one-poller rule still applies: a
hot-added profile that reuses another profile's token is parked with a
`duplicate_credential` error, never started as a second poller.
+Hot-serving also grows the multiplexer's **profile reservation**: the new
+profile's `gateway.lock` is taken and its own session authority is built, so
+`hermes -p <name>` finds the multiplexer as its owner (no stray per-profile
+daemon) and the next `hermes gateway restart` reserves the whole set cleanly.
+A profile whose `state.db` cannot be opened is **parked** — logged as
+`Profile '<name>' not served`, left out of `served_profiles` — and every other
+profile keeps booting; fix the store and re-run `hermes profile create` or wait
+for the periodic rescan.
+
### Routing shared-bot chats to profiles (`profile_routes`)
Multiplexing selects a profile per **credential** (each profile's own bot