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:
@@ -130,3 +130,89 @@ def test_the_v2_brain_offers_no_host_files_or_screen_and_connects_mcp_with_the_a
|
||||
assert "s3cret" not in err
|
||||
# The config file the app wrote never holds the secret.
|
||||
assert "s3cret" not in (tmp_path / "config.json").read_text()
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_a_slow_mcp_server_never_holds_up_a_turn_reading_the_tools(monkeypatch):
|
||||
"""Discovery (which starts the servers) runs outside the cache lock: a turn reading the cached
|
||||
tools, and a new conversation's refresh, never wait for a slow server."""
|
||||
import threading
|
||||
|
||||
from jarvis.tools import registry
|
||||
|
||||
release = threading.Event()
|
||||
calls = []
|
||||
|
||||
def slow_discover(cfg):
|
||||
calls.append(cfg)
|
||||
release.wait(10)
|
||||
return {}, {}
|
||||
|
||||
monkeypatch.setattr(registry, "discover_mcp_tools", slow_discover)
|
||||
monkeypatch.setattr(registry, "_mcp_tools_cache", {})
|
||||
monkeypatch.setattr(registry, "_mcp_config_cache", {})
|
||||
t = threading.Thread(target=registry.initialize_mcp_tools, args=({"slow": {"command": "x"}}, False))
|
||||
t.start()
|
||||
try:
|
||||
deadline = time.monotonic() + 5
|
||||
while not calls and time.monotonic() < deadline:
|
||||
time.sleep(0.01)
|
||||
t0 = time.monotonic()
|
||||
assert registry.get_cached_mcp_tools() == {}
|
||||
assert registry.is_mcp_cache_initialized()
|
||||
# A new conversation's refresh starts in the background and returns at once.
|
||||
registry.refresh_mcp_tools_in_background()
|
||||
assert time.monotonic() - t0 < 1
|
||||
finally:
|
||||
release.set()
|
||||
t.join(10)
|
||||
# Let the background refresh end while the fake discovery is still in place.
|
||||
assert registry._mcp_background_refresh.acquire(timeout=10)
|
||||
registry._mcp_background_refresh.release()
|
||||
assert len(calls) == 2 # the start's discovery and the refresh's
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_the_tts_child_never_inherits_the_mcp_secrets(monkeypatch):
|
||||
"""The worker takes the app's MCP environment before it starts any child process (MeloTTS) and
|
||||
hands it to the brain."""
|
||||
import os
|
||||
|
||||
import voice_worker.__main__ as main_mod
|
||||
from voice_worker import brain as brain_mod
|
||||
|
||||
seen = {}
|
||||
|
||||
class Stop(Exception):
|
||||
pass
|
||||
|
||||
class FakeTranscriber:
|
||||
def __init__(self, *a):
|
||||
pass
|
||||
|
||||
def warm_up(self):
|
||||
pass
|
||||
|
||||
class FakeMelo:
|
||||
def __init__(self, *a):
|
||||
seen["tts_env"] = brain_mod.MCP_ENV_VAR in os.environ
|
||||
|
||||
class FakeBrain:
|
||||
def __init__(self, **kw):
|
||||
seen["brain_env"] = kw.get("mcp_env")
|
||||
raise Stop
|
||||
|
||||
import voice_worker.stt_whisper as stt
|
||||
import voice_worker.tts_melo as melo
|
||||
import voice_worker.vad as vad
|
||||
monkeypatch.setattr(stt, "WhisperTranscriber", FakeTranscriber)
|
||||
monkeypatch.setattr(vad, "SileroVad", lambda: None)
|
||||
monkeypatch.setattr(melo, "MeloEngine", FakeMelo)
|
||||
monkeypatch.setattr(brain_mod, "JarvisBrain", FakeBrain)
|
||||
monkeypatch.setenv("JARVIS_VOICE_TOKEN", "t")
|
||||
monkeypatch.setenv(brain_mod.MCP_ENV_VAR, json.dumps({"fake": {"FAKE_KEY": "s3cret"}}))
|
||||
monkeypatch.setattr(sys, "argv", ["voice_worker", "--melo-python", "py", "--history", ":memory:", "--brain"])
|
||||
with pytest.raises(Stop):
|
||||
main_mod.main()
|
||||
assert seen["tts_env"] is False
|
||||
assert seen["brain_env"] == {"fake": {"FAKE_KEY": "s3cret"}}
|
||||
|
||||
Reference in New Issue
Block a user