Skip to content

The agent that grows with you

fix(tui): pre-confirm the backend path-completion listing

liuhao10246d8370af4b64
diff --git a/tests/tui_gateway/test_backend_dir_entries_preconfirmed.py b/tests/tui_gateway/test_backend_dir_entries_preconfirmed.py
new file mode 100644
index 00000000000..d04122a5c3d
--- /dev/null
+++ b/tests/tui_gateway/test_backend_dir_entries_preconfirmed.py
@@ -0,0 +1,68 @@
+"""Backend path completion must arrive at ``terminal_tool`` pre-confirmed.
+
+The composer's remote-backend directory listing wraps a fixed read-only script in
+``sh -c``, which the dangerous-command guard flags as "shell command via -c/-lc
+flag". Under smart approvals that verdict fires an auxiliary-LLM call per
+completion — the main model when no auxiliary approval model is configured — and
+Desktop's websocket reconnect loop re-runs completion on every reconnect, so an
+idle Desktop makes model calls around the clock (#115478).
+"""
+
+import json
+
+import pytest
+
+from tui_gateway.methods_complete import _backend_dir_entries
+from tools.approval_detection import detect_dangerous_command
+
+
+def _fake_terminal_tool(seen, output="src/\nREADME.md\n", exit_code=0):
+ def _capture(command, **kwargs):
+ seen["command"] = command
+ seen["kwargs"] = kwargs
+ return json.dumps({"output": output, "exit_code": exit_code})
+
+ return _capture
+
+
+def test_backend_dir_listing_is_guard_flagged_without_preconfirmation(monkeypatch):
+ """Premise of the regression: the internal listing shape reads as a shell -c invocation."""
+ seen = {}
+ monkeypatch.setattr("tools.terminal_tool.terminal_tool", _fake_terminal_tool(seen))
+ _backend_dir_entries("/workspace", session_key="sess")
+
+ is_dangerous, pattern_key, _ = detect_dangerous_command(seen["command"])
+ assert is_dangerous
+ assert pattern_key # "shell command via -c/-lc flag"
+
+
+def test_backend_dir_entries_preconfirms_internal_listing(monkeypatch):
+ """The listing is Hermes-owned plumbing: it must skip the approval gate, not consult it."""
+ seen = {}
+ monkeypatch.setattr("tools.terminal_tool.terminal_tool", _fake_terminal_tool(seen))
+ entries = _backend_dir_entries("/workspace", session_key="sess")
+
+ assert seen["kwargs"]["force"] is True
+ assert entries == [("README.md", False), ("src", True)]
+
+
+def test_backend_dir_entries_keeps_session_routing_and_parses_output(monkeypatch):
+ seen = {}
+ monkeypatch.setattr("tools.terminal_tool.terminal_tool", _fake_terminal_tool(seen))
+ _backend_dir_entries("~/proj", session_key="sess-42")
+
+ assert seen["kwargs"]["task_id"] == "sess-42"
+ assert seen["kwargs"]["timeout"] == 3
+ assert seen["command"].startswith("sh -c ")
+
+
[email protected]("payload", [
+ {"output": "boom", "exit_code": 1},
+ {"error": "backend unreachable"},
+])
+def test_backend_dir_entries_swallows_failed_listings(monkeypatch, payload):
+ def _raw(command, **kwargs):
+ return json.dumps(payload)
+
+ monkeypatch.setattr("tools.terminal_tool.terminal_tool", _raw)
+ assert _backend_dir_entries("/workspace", session_key="sess") == []
diff --git a/tui_gateway/methods_complete.py b/tui_gateway/methods_complete.py
index a917d69844c..3e7aecdbb85 100644
--- a/tui_gateway/methods_complete.py
+++ b/tui_gateway/methods_complete.py
@@ -166,8 +166,15 @@ def _backend_dir_entries(search_dir: str, session_key: str | None) -> list[tuple
)
try:
from tools.terminal_tool import terminal_tool
+ # Pre-confirm this internal read-only listing: its fixed `sh -c` script shape is
+ # guard-flagged as "shell command via -c/-lc flag", so under smart approvals every
+ # completion would fire an auxiliary-LLM call (the main model when no auxiliary is
+ # configured), and Desktop's ws reconnect loop turns that into model traffic from an
+ # idle machine (#115478). The script is a constant and the search dir is quoted, so
+ # nothing here needs an approval verdict.
result = json.loads(terminal_tool(
- f"sh -c {shlex.quote(script)} sh {shlex.quote(search_dir)}", task_id=session_key, timeout=3))
+ f"sh -c {shlex.quote(script)} sh {shlex.quote(search_dir)}", task_id=session_key, timeout=3,
+ force=True))
except Exception:
return []
if result.get("error") or result.get("exit_code") not in (0, None):