fix(cli): isolate Desktop compatibility probes [skip ci]

An older terminal can ignore the protocol probe flag and consume inherited gateway credentials or terminal input. Scrub its TUI environment and close stdin before probing, then pass only the selected Desktop resolver metadata to the compatible client.

Validate the complete ready frame before opening mutation gates, not just its identity metadata. Add failing-before regressions and scope the existing recovery test process stub so real lease cleanup remains testable without changing runtime ownership.
This commit is contained in:
Xubin Ren committed 2026-09-08 10:04:15 +08:00
1 parent 48e7c4f697
commit 9a594f74ff
5 files changed
+61 -6

No files matched your search

+31 -1
View File
@@ -71,7 +71,13 @@ def test_launcher_has_no_gateway_ownership_and_no_credentials_in_child_env(monke
process = MagicMock()
process.wait.return_value = 90
process.poll.return_value = 90
monkeypatch.setattr(desktop_tui.subprocess, "run", lambda *a, **kw: subprocess.CompletedProcess(a, 0, b"1\n", b""))
def probe(args, **kw):
assert args == ["terminal-client", "--desktop-protocol"]
assert kw["stdin"] == subprocess.DEVNULL
assert not any(key.startswith("NANOBOT_TUI_") for key in kw["env"])
assert "unrelated-bootstrap" not in json.dumps((args, kw))
return subprocess.CompletedProcess(args, 0, b"1\n", b"")
monkeypatch.setattr(desktop_tui.subprocess, "run", probe)
def start(args, **kw):
calls.append((args, kw))
return process
@@ -92,6 +98,30 @@ def test_launcher_has_no_gateway_ownership_and_no_credentials_in_child_env(monke
process.terminate.assert_not_called()
def test_protocol_probe_is_isolated_in_a_real_child(monkeypatch):
# Older clients ignore --desktop-protocol and consume normal connection env.
# Probe them without credentials or terminal input, before allowing launch.
command = [sys.executable, "-I", "-S", "-c", """
import os, sys
keys = {key for key in os.environ if key.startswith('NANOBOT_TUI_')}
if sys.argv[-1] == '--desktop-protocol':
if keys:
sys.exit(7)
if sys.stdin.buffer.read(1):
sys.exit(8)
print(1)
else:
assert keys == {'NANOBOT_TUI_DESKTOP_TARGET', 'NANOBOT_TUI_DESKTOP_RESOLVER'}
sys.exit(90)
"""]
monkeypatch.setattr(tui_launcher, "resolve_tui_command", lambda: command)
monkeypatch.delenv("NANOBOT_DESKTOP_CLIENT_CACHE", raising=False)
monkeypatch.setenv("NANOBOT_TUI_BOOTSTRAP_URL", "http://127.0.0.1:1/webui/bootstrap")
monkeypatch.setenv("NANOBOT_TUI_BOOTSTRAP_SECRET", "unrelated-bootstrap")
monkeypatch.setenv("NANOBOT_TUI_DESKTOP_RESOLVER", "unrelated-resolver")
assert desktop_tui.launch_desktop_tui(target()) == 0
def test_unsupported_desktop_does_not_install_or_launch_a_client(monkeypatch):
chosen = target()
chosen.request.return_value = DesktopReply("ready", frozenset({"webui"}))
+9 -3
View File
@@ -18,9 +18,9 @@ from nanobot.cli.tui_launcher import (
_initial_tui_chat_id,
_initial_tui_workspace,
_resolve_source_tui_command,
resolve_tui_command,
_websocket_chat_id,
launch_tui,
resolve_tui_command,
)
from nanobot.config.schema import Config, ModelPresetConfig
@@ -228,9 +228,15 @@ def test_launcher_keeps_the_tui_alive_while_an_existing_gateway_recovers(
monkeypatch.setattr("nanobot.gateway.GatewayRuntime", FakeRuntime)
monkeypatch.setattr("nanobot.cli.tui_launcher.resolve_tui_command", lambda: ["nanobot-tui"])
# Stub only this launcher's child; lease cleanup still needs real subprocess
# calls to check live process state (including ps on macOS/Linux).
monkeypatch.setattr(
"nanobot.cli.tui_launcher.subprocess.Popen",
lambda *args, **kwargs: FakeProcess(),
tui_launcher,
"subprocess",
SimpleNamespace(
Popen=lambda *args, **kwargs: FakeProcess(),
TimeoutExpired=subprocess.TimeoutExpired,
),
)
monkeypatch.setattr(
"nanobot.cli.tui_launcher._webui_endpoint_reachable",