Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-driver): keep the CLI fallback inside the ownership check

umutcagand762c223918d8
diff --git a/libs/cua-driver/scripts/tests/_fake_claude_remove.py b/libs/cua-driver/scripts/tests/_fake_claude_remove.py
new file mode 100644
index 0000000000..fe5ecc1870
--- /dev/null
+++ b/libs/cua-driver/scripts/tests/_fake_claude_remove.py
@@ -0,0 +1,14 @@
+"""Stand-in for `claude mcp remove NAME`: deletes the entry by name."""
+
+import json
+import sys
+
+config_path, name = sys.argv[1], sys.argv[2]
+with open(config_path, encoding="utf-8") as handle:
+ config = json.load(handle)
+servers = config.get("mcpServers") or {}
+if name not in servers:
+ raise SystemExit(1)
+del servers[name]
+with open(config_path, "w", encoding="utf-8") as handle:
+ json.dump(config, handle)
diff --git a/libs/cua-driver/scripts/tests/test_uninstall_claude_mcp.py b/libs/cua-driver/scripts/tests/test_uninstall_claude_mcp.py
index f5f96ae7ed..6097f21196 100644
--- a/libs/cua-driver/scripts/tests/test_uninstall_claude_mcp.py
+++ b/libs/cua-driver/scripts/tests/test_uninstall_claude_mcp.py
@@ -10,7 +10,9 @@ from __future__ import annotations
import json
import os
from pathlib import Path
+import shlex
import subprocess
+import sys
REPO_ROOT = Path(__file__).resolve().parents[4]
@@ -38,13 +40,25 @@ def _run(
if rust_marker:
(home / ".cua-driver/packages/current").mkdir(parents=True)
+ claude_json = home / ".claude.json"
+
_executable(fake_bin / "uname", "printf 'Linux\\n'")
_executable(fake_bin / "pkill", "exit 0")
_executable(fake_bin / "systemctl", "exit 0")
if with_claude_cli:
- _executable(fake_bin / "claude", f'echo "$@" >> "{tmp_path / "claude-calls"}"\nexit 1')
+ # `claude mcp remove` matches on the name alone, so the stub has to
+ # mutate the config the way the real CLI does — a stub that only
+ # records its arguments cannot show an entry being deleted after the
+ # scrub deliberately kept it.
+ _executable(
+ fake_bin / "claude",
+ f'echo "$@" >> "{tmp_path / "claude-calls"}"\n'
+ f'[ "$1" = mcp ] && [ "$2" = remove ] || exit 1\n'
+ f'exec {shlex.quote(sys.executable)} '
+ f'{shlex.quote(str(Path(__file__).with_name("_fake_claude_remove.py")))} '
+ f'"{claude_json}" "$3"\n',
+ )
- claude_json = home / ".claude.json"
claude_json.write_text(json.dumps(servers), encoding="utf-8")
env = os.environ.copy()
@@ -191,16 +205,74 @@ def _claude_removals(tmp_path: Path) -> set:
return {parts[2] for parts in (call.split() for call in calls) if parts[:2] == ["mcp", "remove"]}
-def test_cli_fallback_asks_to_remove_the_registered_name(tmp_path: Path) -> None:
+def test_cli_fallback_asks_for_a_name_the_scrub_vouched_for(tmp_path: Path) -> None:
+ """Ownership established, so the CLI may clear the name from other scopes."""
+
+ _run(
+ tmp_path,
+ {
+ "mcpServers": {
+ "cua-computer-use": {
+ "command": "/home/u/.local/bin/cua-driver",
+ "args": ["mcp"],
+ }
+ }
+ },
+ rust_marker=True,
+ with_claude_cli=True,
+ )
+
+ assert _claude_removals(tmp_path) == {"cua-driver-rs", "cua-computer-use"}
+
+
+def test_cli_fallback_asks_only_for_the_unambiguous_name_without_evidence(
+ tmp_path: Path,
+) -> None:
+ """Nothing in the config vouches for a shared name, so do not guess."""
+
_run(tmp_path, {"mcpServers": {}}, rust_marker=True, with_claude_cli=True)
- assert _claude_removals(tmp_path) == {"cua-driver-rs", "cua-driver", "cua-computer-use"}
+ assert _claude_removals(tmp_path) == {"cua-driver-rs"}
def test_cli_fallback_keeps_shared_names_without_a_rust_marker(tmp_path: Path) -> None:
- _run(tmp_path, {"mcpServers": {}}, rust_marker=False, with_claude_cli=True)
+ config, _ = _run(
+ tmp_path,
+ {
+ "mcpServers": {
+ "cua-computer-use": {
+ "command": "/home/u/.local/bin/cua-driver",
+ "args": ["mcp"],
+ }
+ }
+ },
+ rust_marker=False,
+ with_claude_cli=True,
+ )
assert _claude_removals(tmp_path) == {"cua-driver-rs"}
+ assert _user_servers(config) == {"cua-computer-use"}
+
+
+def test_cli_fallback_does_not_delete_another_installs_registration(tmp_path: Path) -> None:
+ """The scrub keeps this entry; the name-only CLI pass must not undo that."""
+
+ config, _ = _run(
+ tmp_path,
+ {
+ "mcpServers": {
+ "cua-computer-use": {
+ "command": "/home/u/.local/bin/cua-driver-sidecar",
+ "args": ["mcp"],
+ }
+ }
+ },
+ rust_marker=True,
+ with_claude_cli=True,
+ )
+
+ assert "cua-computer-use" not in _claude_removals(tmp_path)
+ assert _user_servers(config) == {"cua-computer-use"}
def test_windows_guidance_names_the_registered_server() -> None:
diff --git a/libs/cua-driver/scripts/uninstall.sh b/libs/cua-driver/scripts/uninstall.sh
index 595304a70c..fbdde3a0b1 100755
--- a/libs/cua-driver/scripts/uninstall.sh
+++ b/libs/cua-driver/scripts/uninstall.sh
@@ -776,6 +776,7 @@ if [[ "$USE_RUST_BACKEND" == "1" ]]; then
# install paths. Unrelated MCP servers are left alone, and the names
# shared with the retired Swift driver need a Rust marker on disk.
CLAUDE_JSON="$HOME/.claude.json"
+ CLAUDE_OWNED_NAMES=()
if [[ -f "$CLAUDE_JSON" ]] && command -v python3 >/dev/null 2>&1; then
PY_OUTPUT="$(
CLAUDE_JSON="$CLAUDE_JSON" HOME_DIR="$HOME_DIR" RUST_INSTALL_PRESENT="$RUST_INSTALL_PRESENT" python3 <<'PY'
@@ -798,6 +799,10 @@ except Exception as exc:
raise SystemExit(0)
removed = []
+# Names this scrub established are ours. `claude mcp remove` matches on the
+# name alone, so the CLI cleanup below can only be trusted with a name once
+# the ownership check here has vouched for it.
+owned = set()
def text_parts(value):
if isinstance(value, str):
@@ -868,6 +873,7 @@ def scrub_servers(servers, scope):
return
for name in list(servers.keys()):
if should_remove(name, servers[name]):
+ owned.add(name)
del servers[name]
removed.append(f"{scope}:{name}")
@@ -879,6 +885,9 @@ if isinstance(projects, dict):
if isinstance(project, dict):
scrub_servers(project.get("mcpServers"), "project")
+for name in sorted(owned):
+ print(f"owned-server-name: {name}")
+
if not removed:
raise SystemExit(0)
@@ -908,11 +917,17 @@ print(f"removed Claude MCP registration(s): {', '.join(removed)}")
print(f"backed up Claude config to {backup}")
PY
)"
- if [[ -n "$PY_OUTPUT" ]]; then
- while IFS= read -r line; do
+ CLAUDE_SCRUB_REPORTED=0
+ while IFS= read -r line; do
+ [[ -z "$line" ]] && continue
+ if [[ "$line" == "owned-server-name: "* ]]; then
+ CLAUDE_OWNED_NAMES+=("${line#owned-server-name: }")
+ else
log "$line"
- done <<< "$PY_OUTPUT"
- else
+ CLAUDE_SCRUB_REPORTED=1
+ fi
+ done <<< "$PY_OUTPUT"
+ if [[ "$CLAUDE_SCRUB_REPORTED" == "0" ]]; then
log "no Claude MCP registrations for cua-driver found in $CLAUDE_JSON"
fi
else
@@ -923,13 +938,20 @@ PY
# active project / user scopes — fine to run; it's a no-op when the
# entries were already scrubbed above.
if command -v claude >/dev/null 2>&1; then
- # `cua-driver-rs` was only ever ours. The two current names are
- # shared with the retired Swift driver, so they follow the same
- # Rust-marker gate the config scrub above uses.
+ # `cua-driver-rs` was only ever ours, so it is always safe to remove
+ # by name. The current names are shared with the retired Swift driver
+ # and with any side-by-side launcher, and `claude mcp remove` matches
+ # on the name alone — it cannot repeat the ownership check the scrub
+ # above made. So only ask for the names that scrub vouched for.
+ # Without that evidence (no python3, no config, or a registration
+ # bound to someone else's launcher) leaving an ambiguous entry is
+ # safer than deleting one we do not own.
CLAUDE_MCP_SERVERS=(cua-driver-rs)
- if [[ "$RUST_INSTALL_PRESENT" == "1" ]]; then
- CLAUDE_MCP_SERVERS+=(cua-driver cua-computer-use)
- fi
+ for OWNED_NAME in ${CLAUDE_OWNED_NAMES+"${CLAUDE_OWNED_NAMES[@]}"}; do
+ if [[ "$OWNED_NAME" != "cua-driver-rs" ]]; then
+ CLAUDE_MCP_SERVERS+=("$OWNED_NAME")
+ fi
+ done
for SERVER in "${CLAUDE_MCP_SERVERS[@]}"; do
for SCOPE in local project user; do
if claude mcp remove "$SERVER" -s "$SCOPE" >/dev/null 2>&1; then