Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion hermes_cli/mcp_startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,19 @@ def _discover() -> None:


def wait_for_mcp_discovery(timeout: float = 0.75) -> None:
"""Briefly wait for background MCP discovery before the first tool snapshot."""
"""Briefly wait for background MCP discovery before the first tool snapshot.

Kanban workers (detected via ``HERMES_KANBAN_TASK``) automatically use a
longer timeout because they depend on MCP tools for task execution and run
in the background where a blocking wait is acceptable. See #43273.
"""
import os

thread = _mcp_discovery_thread
if thread is None or not thread.is_alive():
return
# Kanban workers can afford to wait longer — MCP tools are critical
# for task execution and the worker is a background subprocess.
if os.environ.get("HERMES_KANBAN_TASK"):
timeout = max(timeout, 60.0)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current main no longer has this fixed timeout=0.75 contract: wait_for_mcp_discovery() resolves mcp_discovery_timeout through _resolve_discovery_timeout(). Please integrate the kanban minimum after that resolver so configured bounds and the current API are retained.

thread.join(timeout=timeout)
57 changes: 57 additions & 0 deletions tests/hermes_cli/test_mcp_startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,60 @@ def _fake_agent(*_a, **_k):
monkeypatch.setattr(cli_mod, "AIAgent", _fake_agent)

assert cli._init_agent() is True


def test_wait_for_mcp_discovery_uses_longer_timeout_for_kanban_workers(monkeypatch):
"""Kanban workers (HERMES_KANBAN_TASK set) must wait up to 60s for MCP."""
from unittest.mock import MagicMock

joined_with = {"timeout": None}
fake_thread = MagicMock()
fake_thread.is_alive.return_value = True

def _capture_join(timeout=None):
joined_with["timeout"] = timeout

fake_thread.join.side_effect = _capture_join
monkeypatch.setattr(mcp_startup, "_mcp_discovery_thread", fake_thread)
monkeypatch.setenv("HERMES_KANBAN_TASK", "task-123")

mcp_startup.wait_for_mcp_discovery(timeout=0.75)
assert joined_with["timeout"] == 60.0


def test_wait_for_mcp_discovery_preserves_explicit_timeout_for_kanban_workers(monkeypatch):
"""If caller passes a timeout > 60s for kanban workers, it is preserved."""
from unittest.mock import MagicMock

joined_with = {"timeout": None}
fake_thread = MagicMock()
fake_thread.is_alive.return_value = True

def _capture_join(timeout=None):
joined_with["timeout"] = timeout

fake_thread.join.side_effect = _capture_join
monkeypatch.setattr(mcp_startup, "_mcp_discovery_thread", fake_thread)
monkeypatch.setenv("HERMES_KANBAN_TASK", "task-123")

mcp_startup.wait_for_mcp_discovery(timeout=120.0)
assert joined_with["timeout"] == 120.0


def test_wait_for_mcp_discovery_uses_default_timeout_for_non_kanban(monkeypatch):
"""Non-kanban sessions use the caller-provided timeout unchanged."""
from unittest.mock import MagicMock

joined_with = {"timeout": None}
fake_thread = MagicMock()
fake_thread.is_alive.return_value = True

def _capture_join(timeout=None):
joined_with["timeout"] = timeout

fake_thread.join.side_effect = _capture_join
monkeypatch.setattr(mcp_startup, "_mcp_discovery_thread", fake_thread)
monkeypatch.delenv("HERMES_KANBAN_TASK", raising=False)

mcp_startup.wait_for_mcp_discovery(timeout=0.75)
assert joined_with["timeout"] == 0.75
Loading