fix(app): MCP discovery never blocks a turn; secrets masked as lines are written; TTS child gets no MCP secrets

- Discovery runs outside the MCP tool cache's lock (one at a time), so a turn reading the cached
  tools never waits for a slow server; a new conversation refreshes in the background.
- The log hub masks secrets before a line is kept or written, so a key replaced or removed later
  stays hidden on the screen, in copies, files and exports, also after a restart.
- The worker takes JARVIS_MCP_ENV out of its environment at start, before MeloTTS runs, and hands
  it to the brain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-13 10:07:52 +09:00
parent 3ef4281180
commit 8b3905f1c2
8 changed files with 191 additions and 31 deletions

View File

@@ -70,12 +70,17 @@ def enabled_builtin_tools(cfg) -> Dict[str, Any]:
_mcp_tools_cache: Dict[str, "ToolSpec"] = {}
_mcp_tools_cache_lock = threading.Lock()
_mcp_config_cache: Dict[str, Any] = {}
# Held while servers are discovered (slow: it starts them); never by readers of the cache.
_mcp_discovery_lock = threading.Lock()
def initialize_mcp_tools(mcps_config: Dict[str, Any], verbose: bool = True) -> Tuple[Dict[str, "ToolSpec"], Dict[str, str]]:
"""
Initialize MCP tools cache at startup.
Discovery starts the servers and can be slow, so it runs outside the cache lock: readers
(``get_cached_mcp_tools``) never wait for it. Discoveries run one at a time.
Args:
mcps_config: MCP server configuration
verbose: Whether to print status messages
@@ -85,14 +90,16 @@ def initialize_mcp_tools(mcps_config: Dict[str, Any], verbose: bool = True) -> T
"""
global _mcp_tools_cache, _mcp_config_cache
with _mcp_tools_cache_lock:
_mcp_config_cache = mcps_config or {}
_mcp_tools_cache, errors = discover_mcp_tools(mcps_config)
with _mcp_discovery_lock:
with _mcp_tools_cache_lock:
_mcp_config_cache = mcps_config or {}
tools, errors = discover_mcp_tools(mcps_config)
with _mcp_tools_cache_lock:
_mcp_tools_cache = tools
if verbose and _mcp_tools_cache:
debug_log(f"MCP tools cache initialized with {len(_mcp_tools_cache)} tools", "mcp")
return _mcp_tools_cache.copy(), errors
if verbose and tools:
debug_log(f"MCP tools cache initialized with {len(tools)} tools", "mcp")
return tools.copy(), errors
def get_cached_mcp_tools() -> Dict[str, "ToolSpec"]:
@@ -103,28 +110,53 @@ def get_cached_mcp_tools() -> Dict[str, "ToolSpec"]:
def refresh_mcp_tools(verbose: bool = True) -> Tuple[Dict[str, "ToolSpec"], Dict[str, str]]:
"""
Refresh MCP tools cache by rediscovering all tools.
Refresh MCP tools cache by rediscovering all tools (outside the cache lock, like
``initialize_mcp_tools``).
Returns:
Tuple of (discovered_tools, errors) where errors maps server name to error message.
"""
global _mcp_tools_cache
with _mcp_tools_cache_lock:
if not _mcp_config_cache:
with _mcp_discovery_lock:
with _mcp_tools_cache_lock:
config = _mcp_config_cache
if not config:
debug_log("No MCP config cached, skipping refresh", "mcp")
return {}, {}
if verbose:
print("🔄 Refreshing MCP tools...", flush=True)
_mcp_tools_cache, errors = discover_mcp_tools(_mcp_config_cache)
tools, errors = discover_mcp_tools(config)
with _mcp_tools_cache_lock:
_mcp_tools_cache = tools
if verbose:
print(f" ✅ Found {len(_mcp_tools_cache)} MCP tools", flush=True)
if verbose:
print(f" ✅ Found {len(tools)} MCP tools", flush=True)
debug_log(f"MCP tools cache refreshed with {len(_mcp_tools_cache)} tools", "mcp")
return _mcp_tools_cache.copy(), errors
debug_log(f"MCP tools cache refreshed with {len(tools)} tools", "mcp")
return tools.copy(), errors
_mcp_background_refresh = threading.Lock()
def refresh_mcp_tools_in_background() -> None:
"""Refresh the MCP tools on a thread of its own and return at once; the cache keeps serving
the previous tools meanwhile. No second one starts while one runs."""
if not _mcp_background_refresh.acquire(blocking=False):
return
def run() -> None:
try:
refresh_mcp_tools(verbose=False)
except Exception as e: # noqa: BLE001 - the previous tools stay
debug_log(f"MCP background refresh failed: {e}", "mcp")
finally:
_mcp_background_refresh.release()
threading.Thread(target=run, name="mcp-refresh", daemon=True).start()
def is_mcp_cache_initialized() -> bool: