Skip to content

The agent that grows with you

refactor: extract standalone web application lifespan

Tekniumf38f2444fdd5
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py
index 5ffffc9a69e..f1055eaacf2 100644
--- a/hermes_cli/web_server.py
+++ b/hermes_cli/web_server.py
@@ -129,133 +129,7 @@ def _start_desktop_cron_ticker(stop_event: "threading.Event", interval: int = 60
_DESKTOP_MCP_DISCOVERY_DELAY_S = 1.0
-@asynccontextmanager
-async def _lifespan(app: "FastAPI"):
- app.state.event_channels = {} # dict[str, set]
- app.state.event_lock = asyncio.Lock()
- app.state.pty_active_session_files = {} # dict[str, Path]
- # Serializes chat-argv resolution so concurrent /api/pty connections don't
- # overlap ``npm install`` / ``npm run build``. Locks live on app.state (not
- # module globals) so they bind to the running loop, not the import-time one.
- app.state.chat_argv_lock = asyncio.Lock()
-
- # Bring state.db schema current BEFORE the first session-list poll
- # (#79531/#80037): a store left behind by `hermes update` otherwise 500s
- # every poll while the read-probe heal loses to sibling lock contention.
- # Daemon thread so a locked store never delays the socket (Desktop
- # ready-probe times out at 10s, GH-73083).
- threading.Thread(
- target=_eager_reconcile_own_session_db,
- daemon=True,
- name="statedb-eager-reconcile",
- ).start()
-
- # Import hermes_cli.gateway *before* the yield: on Windows + 3.11 the
- # import holds the GIL, so run_in_executor still froze the loop 15-22s and
- # the Desktop's 10s ready-probe timed out (GH-73083).
- _warm_gateway_module()
-
- # Snapshot the checkout revision so lazy-import paths (model picker) can
- # refuse with "restart required" after `hermes update` replaced the code
- # (#86207); the update flow does not reliably restart the dashboard.
- from gateway.code_skew import record_boot_fingerprint
-
- record_boot_fingerprint()
-
- # Hosted Bot rooms belong to the backend process. Recovery may need a
- # contended state.db migration, so keep it off the pre-yield path: Group
- # Chat must degrade on its own rather than block every Desktop feature.
- from tui_gateway import methods_groups as _hosted_groups
- import tui_gateway.server # noqa: F401
-
- hosted_room_start_cancel = threading.Event()
-
- def _start_hosted_rooms() -> None:
- try:
- _hosted_groups.start_hosted_room_service()
- except Exception:
- _log.exception("Hosted Group Chat recovery failed during backend startup")
- finally:
- if hosted_room_start_cancel.is_set():
- _hosted_groups.stop_hosted_room_service(timeout=1.0)
-
- hosted_room_start_thread = threading.Thread(
- target=_start_hosted_rooms,
- daemon=True,
- name="hosted-room-startup",
- )
- hosted_room_start_thread.start()
-
- # Desktop-spawned backends (HERMES_DESKTOP=1) fire cron jobs themselves,
- # since the app has no gateway running the scheduler. Server `hermes
- # dashboard` is unaffected — it relies on its own gateway.
- cron_stop: "threading.Event | None" = None
- cron_thread: "threading.Thread | None" = None
- if os.getenv("HERMES_DESKTOP") == "1":
- # Reap an orphaned gateway from an abnormal previous exit (reparented to
- # launchd, still holding the platform WebSocket) before forking a fresh
- # one that would race the same credential (#77276). Runs
- # unconditionally; protection of a healthy standalone gateway lives
- # INSIDE the reaper (registration probed with cleanup_stale=False).
- try:
- from hermes_cli.gateway import _reap_unsupervised_gateway_orphans
-
- _reap_unsupervised_gateway_orphans()
- except Exception:
- _log.exception("Desktop startup: orphan gateway reap failed")
-
- cron_stop = threading.Event()
- cron_thread = threading.Thread(
- target=_start_desktop_cron_ticker,
- args=(cron_stop,),
- daemon=True,
- name="desktop-cron-ticker",
- )
- cron_thread.start()
-
- # Reap idle/dead keep-alive PTY sessions (30-min TTL).
- pty_reaper_task = asyncio.create_task(run_reaper(PTY_REGISTRY))
- # Periodic authenticated self-test feeding the ``dashboard`` component on /api/status.
- selftest_task = asyncio.create_task(_dashboard_selftest_loop())
- # Live auto-archive timer, independent of list requests.
- auto_archive_task = asyncio.create_task(_auto_archive_ticker_loop())
-
- # Managed local runtime (local_runtime.enabled): bring llama-server back so a
- # restart doesn't strand a llamacpp main model. Off-thread and best-effort;
- # failure falls back to cloud providers like a cold start. Server only —
- # models load on first inference (an empty router holds no VRAM).
- def _boot_local_runtime():
- try:
- from hermes_cli.config import load_config
- from hermes_cli.local_runtime.bootstrap import ensure_local_runtime
-
- ensure_local_runtime(load_config())
- except Exception as exc: # noqa: BLE001
- logging.getLogger(__name__).warning("local runtime boot failed: %s", exc)
-
- threading.Thread(target=_boot_local_runtime, daemon=True, name="local-runtime-boot").start()
-
- try:
- yield
- finally:
- hosted_room_start_cancel.set()
- _hosted_groups.stop_hosted_room_service(timeout=5.0)
- hosted_room_start_thread.join(timeout=1.0)
- if cron_stop is not None:
- cron_stop.set()
- pty_reaper_task.cancel()
- selftest_task.cancel()
- auto_archive_task.cancel()
- await PTY_REGISTRY.close_all()
- # Stop the managed llama-server with its parent (an orphan pins VRAM).
- try:
- from hermes_cli.local_runtime.bootstrap import shutdown_local_runtime
-
- shutdown_local_runtime()
- except Exception: # noqa: BLE001
- pass
- if os.getenv("HERMES_DESKTOP") == "1":
- _terminate_desktop_managed_gateway()
+from hermes_cli.web_server_app import standalone_lifespan # noqa: E402
def _app_state_default(app: "FastAPI", name: str, factory):
@@ -280,7 +154,7 @@ def _get_pty_active_session_files(app: "FastAPI") -> dict[str, Path]:
return _app_state_default(app, "pty_active_session_files", dict)
-app = FastAPI(title="Hermes Agent", version=__version__, lifespan=_lifespan)
+app = FastAPI(title="Hermes Agent", version=__version__, lifespan=standalone_lifespan)
# Memory-provider OAuth connect routes live in the memory layer, not here.
diff --git a/hermes_cli/web_server_app.py b/hermes_cli/web_server_app.py
new file mode 100644
index 00000000000..2344d865b15
--- /dev/null
+++ b/hermes_cli/web_server_app.py
@@ -0,0 +1,148 @@
+"""HTTP lifespan composition; standalone backend services remain opt-in owners."""
+
+from contextlib import asynccontextmanager
+import asyncio
+import logging
+import os
+import threading
+
+from fastapi import FastAPI
+from hermes_cli.pty_session import run_reaper
+
+_log = logging.getLogger("hermes_cli.web_server")
+
+
+@asynccontextmanager
+async def standalone_lifespan(app: "FastAPI"):
+ from hermes_cli.web_server import (
+ _eager_reconcile_own_session_db, _warm_gateway_module,
+ _start_desktop_cron_ticker, _dashboard_selftest_loop,
+ _auto_archive_ticker_loop, _terminate_desktop_managed_gateway,
+ )
+ from hermes_cli.web_server_chat import PTY_REGISTRY
+
+ app.state.event_channels = {} # dict[str, set]
+ app.state.event_lock = asyncio.Lock()
+ app.state.pty_active_session_files = {} # dict[str, Path]
+ # Serializes chat-argv resolution so concurrent /api/pty connections don't
+ # overlap ``npm install`` / ``npm run build``. Locks live on app.state (not
+ # module globals) so they bind to the running loop, not the import-time one.
+ app.state.chat_argv_lock = asyncio.Lock()
+
+ # Bring state.db schema current BEFORE the first session-list poll
+ # (#79531/#80037): a store left behind by `hermes update` otherwise 500s
+ # every poll while the read-probe heal loses to sibling lock contention.
+ # Daemon thread so a locked store never delays the socket (Desktop
+ # ready-probe times out at 10s, GH-73083).
+ threading.Thread(
+ target=_eager_reconcile_own_session_db,
+ daemon=True,
+ name="statedb-eager-reconcile",
+ ).start()
+
+ # Import hermes_cli.gateway *before* the yield: on Windows + 3.11 the
+ # import holds the GIL, so run_in_executor still froze the loop 15-22s and
+ # the Desktop's 10s ready-probe timed out (GH-73083).
+ _warm_gateway_module()
+
+ # Snapshot the checkout revision so lazy-import paths (model picker) can
+ # refuse with "restart required" after `hermes update` replaced the code
+ # (#86207); the update flow does not reliably restart the dashboard.
+ from gateway.code_skew import record_boot_fingerprint
+
+ record_boot_fingerprint()
+
+ # Hosted Bot rooms belong to the backend process. Recovery may need a
+ # contended state.db migration, so keep it off the pre-yield path: Group
+ # Chat must degrade on its own rather than block every Desktop feature.
+ from tui_gateway import methods_groups as _hosted_groups
+ import tui_gateway.server # noqa: F401
+
+ hosted_room_start_cancel = threading.Event()
+
+ def _start_hosted_rooms() -> None:
+ try:
+ _hosted_groups.start_hosted_room_service()
+ except Exception:
+ _log.exception("Hosted Group Chat recovery failed during backend startup")
+ finally:
+ if hosted_room_start_cancel.is_set():
+ _hosted_groups.stop_hosted_room_service(timeout=1.0)
+
+ hosted_room_start_thread = threading.Thread(
+ target=_start_hosted_rooms,
+ daemon=True,
+ name="hosted-room-startup",
+ )
+ hosted_room_start_thread.start()
+
+ # Desktop-spawned backends (HERMES_DESKTOP=1) fire cron jobs themselves,
+ # since the app has no gateway running the scheduler. Server `hermes
+ # dashboard` is unaffected — it relies on its own gateway.
+ cron_stop: "threading.Event | None" = None
+ cron_thread: "threading.Thread | None" = None
+ if os.getenv("HERMES_DESKTOP") == "1":
+ # Reap an orphaned gateway from an abnormal previous exit (reparented to
+ # launchd, still holding the platform WebSocket) before forking a fresh
+ # one that would race the same credential (#77276). Runs
+ # unconditionally; protection of a healthy standalone gateway lives
+ # INSIDE the reaper (registration probed with cleanup_stale=False).
+ try:
+ from hermes_cli.gateway import _reap_unsupervised_gateway_orphans
+
+ _reap_unsupervised_gateway_orphans()
+ except Exception:
+ _log.exception("Desktop startup: orphan gateway reap failed")
+
+ cron_stop = threading.Event()
+ cron_thread = threading.Thread(
+ target=_start_desktop_cron_ticker,
+ args=(cron_stop,),
+ daemon=True,
+ name="desktop-cron-ticker",
+ )
+ cron_thread.start()
+
+ # Reap idle/dead keep-alive PTY sessions (30-min TTL).
+ pty_reaper_task = asyncio.create_task(run_reaper(PTY_REGISTRY))
+ # Periodic authenticated self-test feeding the ``dashboard`` component on /api/status.
+ selftest_task = asyncio.create_task(_dashboard_selftest_loop())
+ # Live auto-archive timer, independent of list requests.
+ auto_archive_task = asyncio.create_task(_auto_archive_ticker_loop())
+
+ # Managed local runtime (local_runtime.enabled): bring llama-server back so a
+ # restart doesn't strand a llamacpp main model. Off-thread and best-effort;
+ # failure falls back to cloud providers like a cold start. Server only —
+ # models load on first inference (an empty router holds no VRAM).
+ def _boot_local_runtime():
+ try:
+ from hermes_cli.config import load_config
+ from hermes_cli.local_runtime.bootstrap import ensure_local_runtime
+
+ ensure_local_runtime(load_config())
+ except Exception as exc: # noqa: BLE001
+ logging.getLogger(__name__).warning("local runtime boot failed: %s", exc)
+
+ threading.Thread(target=_boot_local_runtime, daemon=True, name="local-runtime-boot").start()
+
+ try:
+ yield
+ finally:
+ hosted_room_start_cancel.set()
+ _hosted_groups.stop_hosted_room_service(timeout=5.0)
+ hosted_room_start_thread.join(timeout=1.0)
+ if cron_stop is not None:
+ cron_stop.set()
+ pty_reaper_task.cancel()
+ selftest_task.cancel()
+ auto_archive_task.cancel()
+ await PTY_REGISTRY.close_all()
+ # Stop the managed llama-server with its parent (an orphan pins VRAM).
+ try:
+ from hermes_cli.local_runtime.bootstrap import shutdown_local_runtime
+
+ shutdown_local_runtime()
+ except Exception: # noqa: BLE001
+ pass
+ if os.getenv("HERMES_DESKTOP") == "1":
+ _terminate_desktop_managed_gateway()