The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(dashboard): fire the armed discovery off-loop; flat branch; behaviour-shaped test
kshitijk4poor5583566049d1
diff --git a/hermes_cli/main.py b/hermes_cli/main.pyindex 12f8013cc41..09c2b6efbde 100644--- a/hermes_cli/main.py+++ b/hermes_cli/main.py@@ -2636,31 +2636,24 @@ def _dashboard_prepare_runtime(args, headless_backend) -> bool:# A standalone (non-Desktop) dashboard may sit idle and unvisited for days# (#58733): it arms discovery instead and the first /api/ws client fires it.desktop = os.environ.get("HERMES_DESKTOP") == "1"- mcp_discovery_after_bind = headless_backend and desktop- if not mcp_discovery_after_bind:- try:- from hermes_cli.mcp_startup import (- defer_background_mcp_discovery,- start_background_mcp_discovery,- )+ if headless_backend and desktop:+ return True+ try:+ from hermes_cli.mcp_startup import (+ defer_background_mcp_discovery,+ start_background_mcp_discovery,+ )- if desktop:- start_background_mcp_discovery(- logger=logger,- thread_name="dashboard-mcp-discovery",- )- else:- defer_background_mcp_discovery(- logger=logger,- thread_name="dashboard-mcp-discovery",- delay=None,- )- except Exception:- logger.debug(- "Background MCP tool discovery failed at dashboard startup",- exc_info=True,- )- return mcp_discovery_after_bind+ if desktop:+ start_background_mcp_discovery(logger=logger, thread_name="dashboard-mcp-discovery")+ else:+ defer_background_mcp_discovery(logger=logger, thread_name="dashboard-mcp-discovery", delay=None)+ except Exception:+ logger.debug(+ "Background MCP tool discovery failed at dashboard startup",+ exc_info=True,+ )+ return Falsedef cmd_dashboard(args):diff --git a/hermes_cli/mcp_startup.py b/hermes_cli/mcp_startup.pyindex 768d981aac6..55712eccecd 100644--- a/hermes_cli/mcp_startup.py+++ b/hermes_cli/mcp_startup.py@@ -199,7 +199,9 @@ def defer_background_mcp_discovery(*, logger, thread_name: str, delay: float | N_mcp_discovery_deferred = Nonestart_background_mcp_discovery(logger=logger, thread_name=thread_name)- timer = threading.Timer(delay or 0, _fire)+ # ``None`` builds the Timer only as the holder of ``_fire``; it is never started and+ # ``start_deferred_mcp_discovery_now`` runs ``timer.function()`` directly.+ timer = threading.Timer(0 if delay is None else delay, _fire)timer.daemon = Truetimer.name = f"{thread_name}-deferred"_mcp_discovery_deferred = timerdiff --git a/hermes_cli/web_routers/chat_ws.py b/hermes_cli/web_routers/chat_ws.pyindex d2faadc18eb..6a827f7f94d 100644--- a/hermes_cli/web_routers/chat_ws.py+++ b/hermes_cli/web_routers/chat_ws.py@@ -577,8 +577,9 @@ async def gateway_ws(ws: WebSocket) -> None:from hermes_cli.mcp_startup import start_deferred_mcp_discovery_nowfrom tui_gateway.ws import handle_ws- # First chat client of a standalone dashboard: fire the discovery armed at boot (no-op otherwise).- start_deferred_mcp_discovery_now()+ # First chat client of a standalone dashboard: fire the discovery armed at boot (no-op+ # otherwise). Off-loop: the first act is a config read + the ~350 ms `mcp` SDK import.+ await asyncio.to_thread(start_deferred_mcp_discovery_now)# The authenticated identity (ticket / internal credential) stamped by# _ws_auth_reason becomes the identity authority for privileged RPCsdiff --git a/tests/hermes_cli/test_serve_mcp_discovery_after_bind.py b/tests/hermes_cli/test_serve_mcp_discovery_after_bind.pyindex 3f56679de72..4e319427f37 100644--- a/tests/hermes_cli/test_serve_mcp_discovery_after_bind.py+++ b/tests/hermes_cli/test_serve_mcp_discovery_after_bind.py@@ -1,4 +1,5 @@-"""Desktop `serve` starts background MCP discovery only after the socket binds.+"""Desktop `serve` starts background MCP discovery only after the socket binds; a standalone+`hermes dashboard` arms it at boot and the first /api/ws client (or agent build) fires it (#58733).The MCP SDK import (~350ms) used to run on a thread started BEFOREweb_server was imported, holding the GIL against the main thread's own@@ -99,8 +100,9 @@ def test_standalone_dashboard_boot_arms_discovery_without_starting_it(monkeypatcafter_bind = main_mod._dashboard_prepare_runtime(types.SimpleNamespace(skip_build=True), False)assert after_bind is False and calls == []- armed = mcp_startup._mcp_discovery_deferred- assert isinstance(armed, threading.Timer) and not armed.is_alive()+ # armed, not started: firing on demand runs it exactly once+ mcp_startup.start_deferred_mcp_discovery_now()+ assert calls == ["dashboard-mcp-discovery"]def test_first_gateway_ws_client_starts_the_armed_discovery_once(monkeypatch):