Skip to content

The agent that grows with you

fix(serve): the app lifespan is an async context manager again

alt-glitch10cec6ec951f
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py
index 5fad79374b1..2c22cc36c2c 100644
--- a/hermes_cli/web_server.py
+++ b/hermes_cli/web_server.py
@@ -176,7 +176,6 @@ def _start_desktop_cron_ticker(stop_event: threading.Event, interval: int = 60)
_DESKTOP_MCP_DISCOVERY_DELAY_S = 1.0
-@asynccontextmanager
def _probe_install_before_state_db() -> None:
from hermes_cli.anon_auth import on_local_desktop_backend
@@ -190,6 +189,7 @@ def _probe_install_before_state_db() -> None:
_log.warning("first-install check at boot failed", exc_info=True)
+@asynccontextmanager
async def _lifespan(app: FastAPI):
app.state.event_channels = {} # dict[str, set]
app.state.event_lock = asyncio.Lock()
diff --git a/tests/hermes_cli/test_web_server_boot_handshake.py b/tests/hermes_cli/test_web_server_boot_handshake.py
index 91e5625feb6..03a023e0f8f 100644
--- a/tests/hermes_cli/test_web_server_boot_handshake.py
+++ b/tests/hermes_cli/test_web_server_boot_handshake.py
@@ -62,6 +62,25 @@ def test_lifespan_warmup_is_synchronous(monkeypatch):
)
+def test_lifespan_enters_and_exits_as_an_async_context_manager(monkeypatch):
+ """FastAPI gets ``_lifespan`` as an async context manager, never a bare async generator
+ (Starlette's deprecated path), and entering it runs the desktop's first-install check."""
+ import hermes_cli.anon_auth as anon_auth
+ import hermes_cli.install_state as install_state
+
+ probed: list[bool] = []
+ monkeypatch.setattr(web_server_mod, "_warm_gateway_module", lambda: None)
+ monkeypatch.setattr(web_server_mod, "_eager_reconcile_own_session_db", lambda: None)
+ monkeypatch.setattr(anon_auth, "on_local_desktop_backend", lambda: True)
+ monkeypatch.setattr(install_state, "probe", lambda: probed.append(True))
+
+ async def _run() -> None:
+ async with web_server_mod._lifespan(web_server_mod.app):
+ assert probed == [True]
+
+ asyncio.run(_run())
+
+
def test_hosted_room_recovery_cannot_block_or_abort_backend_startup(monkeypatch):
from fastapi.testclient import TestClient
from tui_gateway import methods_groups