Skip to content
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
19347f3
fix(streaming): run MCP discovery off the turn's critical path
dankkush Aug 13, 2026
3b8a352
fix(streaming): assert profile home via contextvar in MCP discovery t…
dankkush Aug 13, 2026
68a3a42
fix(streaming): use version-gated override resolver in MCP discovery …
dankkush Aug 13, 2026
35d3e97
fix(streaming): bounded join + coalescing for background MCP discovery
dankkush Aug 14, 2026
99ea1a5
fix(streaming): synchronous fallback on agents without the home overr…
dankkush Aug 14, 2026
8228c2a
fix(streaming): profile-scoped MCP readiness instead of timed join
dankkush Aug 14, 2026
e017cf0
fix(streaming): generation-based single-flight MCP readiness
dankkush Aug 14, 2026
e088f4d
fix(commands): reload-mcp asserts the profile home during re-discovery
dankkush Aug 14, 2026
efe6309
fix(commands): reload-mcp authority is deterministic default profile
dankkush Aug 14, 2026
5a9e57b
fix(streaming): physically single-flight owners and canonical readine…
dankkush Aug 15, 2026
8c04b84
fix(commands): fail closed when a discovery owner survives the reload…
dankkush Aug 15, 2026
1fb90fc
fix(streaming): fence owner creation behind the reload teardown window
dankkush Aug 15, 2026
2736411
fix(streaming): hold the reload fence across the entire rebuild
dankkush Aug 15, 2026
118ca77
fix(commands): phase-split the reload fence; never stall first turns
dankkush Aug 17, 2026
9b96ad3
fix(streaming): timeout retirement wakes peer waiters
dankkush Aug 17, 2026
00380f4
fix(streaming): refreshable readiness, canonical keys, wait-loop hard…
dankkush Aug 24, 2026
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
150 changes: 147 additions & 3 deletions api/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from __future__ import annotations
from contextlib import nullcontext
import logging
import os
import threading
from typing import Any

Expand Down Expand Up @@ -269,8 +270,137 @@ def _run_reload_mcp_command() -> str:
with _lock:
old_servers = set(_servers.keys())

shutdown_mcp_servers()
new_tools = discover_mcp_tools()
# Route the re-discovery through the SAME readiness authority
# the stream worker uses (api.streaming), so a subsequent turn
# observes the fresh outcome instead of a stale 'completed' /
# 'failed' readiness recorded before this reload. The command
# waits on the shared readiness event so its synchronous
# report below is still accurate.
#
# The authority is keyed by the CANONICAL resolved default
# profile-home path (the same key the startup kickoff and
# default-profile turns consult). /reload-mcp is a GLOBAL
# operation (it shuts down and reconnects every configured
# server), so it deliberately does NOT key off the exec-time
# HERMES_HOME env: a concurrent stream can mutate that
# process-global while the command runs, which would make the
# reload capture another profile (Greptile P1).
from api.streaming import (
_MCP_READINESS_WAIT_CAP_S,
_MCP_RELOAD_FENCE,
_canonical_readiness_key,
_invalidate_mcp_readiness,
_mcp_retry_discovery,
_mcp_wait_readiness,
_prepare_global_reload,
)

def _reload_discover():
"""Re-discover MCP tools; returns bool.

Installs the DEFAULT profile-home override (like the
stream worker's discovery closure), so a background
reload run can never read another stream's HERMES_HOME
after env mutation (Greptile P1).
"""
_mcp_home_token = None
try:
from api.profiles import _resolve_hermes_home_override
_hc_mod = _resolve_hermes_home_override()
if _hc_mod is not None:
_mcp_home = getattr(
_hc_mod, 'get_default_hermes_root', lambda: ''
)()
if _mcp_home:
_mcp_home_token = _hc_mod.set_hermes_home_override(
str(_mcp_home)
)
try:
discover_mcp_tools()
return True
except Exception:
return False
finally:
if _mcp_home_token is not None:
_hc_mod.reset_hermes_home_override(_mcp_home_token)
except Exception:
return False
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

# Coordinate the global shutdown with every live discovery
# owner (cancel + bounded join) so a body mid-discovery cannot
# re-register old-config servers after the registry is cleared.
# Fail CLOSED: if any owner survived the join cap, abort the
# reload rather than run a discovery body concurrently with a
# registry rebuild (Greptile review round 10).
#
# The owner-creation FENCE is held across the DESTRUCTIVE
# phase only: snapshot, join, shutdown, and readiness
# invalidation. It is released BEFORE the replacement
# discovery (the additive phase), so unrelated FIRST turns
# for new profiles never stall for the reload's discovery
# wait (Greptile review round 13). Round 12's fix held the
# fence across the whole rebuild; that traded a benign
# stale-churn window for a real first-turn stall, and this
# phase split resolves both: invalidation now runs BEFORE
# any post-shutdown registration, so a stream owner created
# during the rebuild is FRESH and never gets invalidated as
# stale, and no new body can register into the shutdown.
# The reload's own retry spawn skips the re-acquire
# (`_fence_held=True`) because threading.Lock is not
# reentrant; waiting turns are unaffected — they only wait
# on readiness events, never the fence.
with _MCP_RELOAD_FENCE:
if not _prepare_global_reload():
return (
"MCP reload aborted: a discovery run is still in "
f"progress after waiting {int(_MCP_READINESS_WAIT_CAP_S)}s. "
"Retry once it finishes."
)
shutdown_mcp_servers()
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

# The global reload just shut down the registry: every
# OTHER profile's readiness is stale. Remove those
# entries BEFORE any post-shutdown owner can be created
# (still inside the fence), so a fresh registration can
# never be invalidated out from under it.
_invalidate_mcp_readiness(except_key=_canonical_readiness_key(''))

try:
from api.profiles import _resolve_hermes_home_override
_reload_override_available = _resolve_hermes_home_override() is not None
except Exception:
_reload_override_available = False

if _reload_override_available:
_readiness = _mcp_retry_discovery(
_canonical_readiness_key(''),
_reload_discover,
"mcp-reload",
_fence_held=True,
)
else:
_readiness = None

# Fence released — the ADDITIVE phase (replacement
# discovery) runs unfenced: waiting on it never blocks
# unrelated first turns. Concurrent registrations into the
# process-global `_servers` registry during the additive
# phase are the pre-existing multi-profile limitation (the
# registry is keyed by raw server name — documented in
# api/streaming.py); they converge on the next per-turn
# refresh and are not a reload-exclusivity defect.
if _readiness is not None:
_reload_status = _mcp_wait_readiness(_readiness)
else:
# Older agent: no context-local override — a background
# reload would read the process env while other streams
# mutate it. Run inline (pre-PR semantics), mirroring the
# stream worker's old-agent path. The env read stays
# pre-PR on these agents (no override API exists to scope
# it); this is the documented compatibility behavior.
_reload_status = (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Legacy reload loses default profile

If /reload-mcp runs on an older agent while a non-default-profile stream mutates the process environment, this inline fallback installs no profile-local override and reads the stream's HERMES_HOME. Reload then registers and reports that profile's MCP servers while leaving the default profile's tooling unchanged.

Context Used: AGENTS.md (source)

Knowledge Base Used: Configuration and integrations

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fixed in ruizanthony/hermes-webui@e40c285. The legacy /reload-mcp path stays inline and now uses the same serialized legacy-discovery helper with the canonical default-profile home pinned; the regression asserts that a foreign stream environment is ignored and restored.

"completed" if _reload_discover() else "failed"
)

with _lock:
connected_servers = set(_servers.keys())
Expand All @@ -291,10 +421,24 @@ def _run_reload_mcp_command() -> str:
lines.append(f"Removed: {', '.join(sorted(removed))}")

if connected_servers:
lines.append(f"{len(new_tools or [])} tool(s) available across {len(connected_servers)} server(s)")
_tool_count = 0
for _server_name in connected_servers:
try:
_tool_count += len(
getattr(_servers[_server_name], "_registered_tool_names", [])
or []
)
except Exception:
pass
lines.append(
f"{_tool_count} tool(s) available across {len(connected_servers)} server(s)"
)
else:
lines.append("No MCP servers connected")

if _reload_status == "failed":
lines.append("MCP discovery failed during reload — check the server logs")

if not reconnected and not added and not removed:
lines.append("Tooling state was already current")

Expand Down
Loading