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(spaces-macos): uninstall-local.sh takes this Mac off the relay and Local's entries out of your coding agents (CUA-2212, CUA-2222) (#5882)

cua-agents[bot]b43f30bd667e
diff --git a/apps/cua-spaces-macos/scripts/tests/install-local-test.sh b/apps/cua-spaces-macos/scripts/tests/install-local-test.sh
index ab1957297b..98e54a6044 100755
--- a/apps/cua-spaces-macos/scripts/tests/install-local-test.sh
+++ b/apps/cua-spaces-macos/scripts/tests/install-local-test.sh
@@ -20,7 +20,9 @@
# checked first, in one message;
# - on a fresh Keychain (a certificate codesign cannot use yet) it says so and
# sets it up only when asked (--setup-signing), or prints the exact steps;
-# - uninstall-local.sh removes the Local pieces and only those;
+# - uninstall-local.sh removes the Local pieces and only those, and first
+# takes this Mac off the relay when Local is signed in, as Local and with
+# nothing of the caller's Stable environment;
# - the names in scripts/_local-common.sh are the ones CuaChannel::Local has.
#
# scripts/tests/install-local-test.sh
@@ -1219,6 +1221,37 @@ check "and Stable files, lume included, are byte-identical" '[ "$(snap_stable)"
# --- 8. Uninstall --------------------------------------------------------------------------------------------------
+# The bundled cua of the installed Local app, as a script that logs how it was
+# called: its arguments, the variables it got, and whether the sign-in still
+# existed. `auth status`, `host remove` and `agents remove` answer as
+# STUB_AUTH_STATUS, STUB_HOST_REMOVE and STUB_AGENTS_REMOVE say (0 unless set).
+export LOCAL_CUA_LOG="$WORK/local-cua.log"
+write_local_cli() {
+ : > "$LOCAL_CUA_LOG"
+ cat > "$LOCAL_APP/Contents/MacOS/cua" <<'STUBEOF'
+#!/bin/bash
+# What the CLI would read and remove: the sign-in and the record of what Local
+# wrote into the coding agents, in the home of the environment it was asked for.
+state="$HOME/.cua-local"; [ "${CUA_ENV-}" = prod ] && state="$state/envs/prod"
+echo "cua $* [CUA_CHANNEL=${CUA_CHANNEL-}] [CUA_HOME=${CUA_HOME-}] [CUA_API_KEY=${CUA_API_KEY-}] [CUA_RELAY_URL=${CUA_RELAY_URL-}] [CUA_ENV=${CUA_ENV-}] creds=$([ -e "$state/credentials.json" ] && echo present || echo gone) agents=$([ -e "$state/agent-setup.json" ] && echo present || echo gone)" >> "$LOCAL_CUA_LOG"
+case "$*" in
+ "auth status") exit "${STUB_AUTH_STATUS:-0}" ;;
+ "host remove --force --uninstalling") exit "${STUB_HOST_REMOVE:-0}" ;;
+ "agents remove --yes")
+ # STUB_AGENTS_REMOVE=hang: start a program of its own (like `claude mcp
+ # remove`) and wait for it, so only a timeout on the whole group ends it.
+ if [ "${STUB_AGENTS_REMOVE:-0}" = hang ]; then
+ sleep 300 &
+ echo $! > "$STUB_CHILD_PID"
+ wait
+ fi
+ exit "${STUB_AGENTS_REMOVE:-0}" ;;
+esac
+exit 0
+STUBEOF
+ chmod +x "$LOCAL_APP/Contents/MacOS/cua"
+}
+
echo "== uninstall =="
reset_fixture
ALL_BEFORE="$(snap_all)"
@@ -1227,6 +1260,7 @@ install
mkdir -p "$FAKE_HOME/.cua-local/helper" "$FAKE_HOME/Library/Application Support/com.trycua.spaces.macos.local" "$FAKE_HOME/Cua Volume Local"
echo '{}' > "$FAKE_HOME/.cua-local/helper/service.json"
echo '{}' > "$FAKE_HOME/.cua-local/credentials.json"
+echo '{}' > "$FAKE_HOME/.cua-local/agent-setup.json"
echo '{}' > "$FAKE_HOME/Library/Application Support/com.trycua.spaces.macos.local/settings.json"
echo '<plist/>' > "$FAKE_HOME/Library/LaunchAgents/com.trycua.spacesd.host.local.plist"
# What macOS keeps for the Local bundle id in ~/Library.
@@ -1239,6 +1273,7 @@ echo 'web' > "$FAKE_HOME/Library/WebKit/com.trycua.spaces.macos.local/data"
echo 'cookies' > "$FAKE_HOME/Library/Cookies/com.trycua.spaces.macos.local.binarycookies"
echo 'state' > "$FAKE_HOME/Library/Saved Application State/com.trycua.spaces.macos.local.savedState/windows.plist"
echo 'mine' > "$FAKE_HOME/Cua Volume Local/keep.txt"
+write_local_cli
BEFORE_UNINSTALL="$(snap_all)"
: > "$SHIM_LOG"; : > "$STUB_LOG"
@@ -1258,6 +1293,12 @@ check "the Local bundle id's preferences, caches, web view data, cookies and win
'[ ! -e "$FAKE_HOME/Library/Preferences/com.trycua.spaces.macos.local.plist" ] && [ ! -e "$FAKE_HOME/Library/Caches/com.trycua.spaces.macos.local" ] && [ ! -e "$FAKE_HOME/Library/HTTPStorages/com.trycua.spaces.macos.local" ] && [ ! -e "$FAKE_HOME/Library/WebKit/com.trycua.spaces.macos.local" ] && [ ! -e "$FAKE_HOME/Library/Cookies/com.trycua.spaces.macos.local.binarycookies" ] && [ ! -e "$FAKE_HOME/Library/Saved Application State/com.trycua.spaces.macos.local.savedState" ]'
check "and Stable's preferences, caches, web view data, cookies and window state of the same kinds are still there" \
'[ -f "$FAKE_HOME/Library/Preferences/com.trycua.spaces.macos.plist" ] && [ -f "$FAKE_HOME/Library/Caches/com.trycua.spaces.macos/Cache.db" ] && [ -f "$FAKE_HOME/Library/WebKit/com.trycua.spaces.macos/data" ] && [ -f "$FAKE_HOME/Library/Cookies/com.trycua.spaces.macos.binarycookies" ] && [ -f "$FAKE_HOME/Library/Saved Application State/com.trycua.spaces.macos.savedState/windows.plist" ]'
+check "this Mac was taken off the relay, as Local, in staging (the only environment with state), before the sign-in was deleted" \
+ 'grep >/dev/null "^cua auth status \[CUA_CHANNEL=local\]" "$LOCAL_CUA_LOG" && grep >/dev/null "^cua host remove --force --uninstalling \[CUA_CHANNEL=local\] .*\[CUA_ENV=staging\] creds=present" "$LOCAL_CUA_LOG" && ! grep >/dev/null "CUA_ENV=prod" "$LOCAL_CUA_LOG"'
+check "Local's MCP server and skills were removed from the coding agents by the Local CLI (agents remove --yes, nothing more), as Local, in staging, while its record was still there" \
+ '[ "$(grep -c "^cua agents remove" "$LOCAL_CUA_LOG")" = 1 ] && grep >/dev/null "^cua agents remove --yes \[CUA_CHANNEL=local\] \[CUA_HOME=\] .*\[CUA_ENV=staging\] creds=.* agents=present\$" "$LOCAL_CUA_LOG"'
+check "and it was the Local CLI only: the released app's cua was never run, so its entries and cua-driver were not asked about" \
+ '! grep >/dev/null "^$FAKE_HOME/.local/bin/cua " "$STUB_LOG" && ! grep >/dev/null -e "cua-driver" -e "agents setup" "$LOCAL_CUA_LOG"'
check "the Local host runner job file is gone" '[ ! -e "$FAKE_HOME/Library/LaunchAgents/com.trycua.spacesd.host.local.plist" ]'
check "the helper login item was unregistered by the Local app itself" 'grep >/dev/null -- "--service-helper unregister \[CUA_CHANNEL=local\]" "$STUB_LOG"'
check "launchctl was asked about the two Local labels only" \
@@ -1294,6 +1335,165 @@ check "uninstall removes the leftovers of both ids when no app is installed (sta
check "and Stable files, whose names the new Local id extends, are byte-identical" '[ "$(snap_stable)" = "$STABLE_BEFORE" ]'
check "uninstall never resets permission rows (macOS keeps them for a reinstall)" '! grep >/dev/null "^tccutil" "$SHIM_LOG"'
+echo "== uninstall takes this Mac off the relay, safely =="
+# A stray environment of the released Cua Spaces in the caller's shell: its
+# home and its API key must not reach the Local CLI (CUA_ENV, the opt-in to
+# production, does).
+reset_fixture
+STABLE_BEFORE="$(snap_stable)"
+install
+mkdir -p "$FAKE_HOME/.cua-local/envs/prod"
+echo '{}' > "$FAKE_HOME/.cua-local/credentials.json"
+echo '{}' > "$FAKE_HOME/.cua-local/envs/prod/credentials.json"
+echo '{}' > "$FAKE_HOME/.cua-local/agent-setup.json"
+echo '{}' > "$FAKE_HOME/.cua-local/envs/prod/agent-setup.json"
+write_local_cli
+run env CUA_HOME="$FAKE_HOME/.cua" CUA_API_KEY=stable-key CUA_RELAY_URL=https://relay.example.invalid CUA_ENV=prod "$FAKE_SCRIPTS/uninstall-local.sh" --yes
+check "uninstall with a stray Stable environment (status $RC)" '[ "$RC" = 0 ] && [ ! -e "$LOCAL_APP" ] && [ ! -e "$FAKE_HOME/.cua-local" ]'
+check "the Local CLI got none of CUA_HOME, CUA_API_KEY or CUA_RELAY_URL" \
+ '[ "$(grep -c "^cua host remove .*\[CUA_HOME=\] \[CUA_API_KEY=\] \[CUA_RELAY_URL=\]" "$LOCAL_CUA_LOG")" = 2 ]'
+check "both Local environments with state are taken off their relay, each with its own sign-in still there: staging, and prod (the opt-in), whatever CUA_ENV the shell had" \
+ 'grep >/dev/null "^cua host remove .*\[CUA_ENV=staging\] creds=present" "$LOCAL_CUA_LOG" && grep >/dev/null "^cua host remove .*\[CUA_ENV=prod\] creds=present" "$LOCAL_CUA_LOG"'
+check "the same for the coding agents: each environment's Local entries are removed by the Local CLI with none of the stray variables, while that environment's record is still there" \
+ '[ "$(grep -c "^cua agents remove --yes .*\[CUA_HOME=\] \[CUA_API_KEY=\] \[CUA_RELAY_URL=\]" "$LOCAL_CUA_LOG")" = 2 ] && grep >/dev/null "^cua agents remove .*\[CUA_ENV=staging\] creds=.* agents=present" "$LOCAL_CUA_LOG" && grep >/dev/null "^cua agents remove .*\[CUA_ENV=prod\] creds=.* agents=present" "$LOCAL_CUA_LOG"'
+check "Stable files, library folders included, are byte-identical" '[ "$(snap_stable)" = "$STABLE_BEFORE" ]'
+
+# A Local home that is a link into the released Cua Spaces' folders: the CLI
+# would unregister Stable's host and delete its state, so it is not asked, and
+# only the link is removed.
+for linked in home host host-config host-token host-spaces helper-spec credentials host-elsewhere shared-helper; do
+ reset_fixture
+ mkdir -p "$FAKE_HOME/.cua/host"
+ echo '{"stable":true}' > "$FAKE_HOME/.cua/host/config.json"
+ echo 'stable-token' > "$FAKE_HOME/.cua/host/machine-token"
+ echo '[]' > "$FAKE_HOME/.cua/host/provided-spaces.json"
+ STABLE_BEFORE="$(snap_stable)"
+ install
+ write_local_cli
+ case "$linked" in
+ home) ln -s "$FAKE_HOME/.cua" "$FAKE_HOME/.cua-local" ;;
+ host)
+ mkdir -p "$FAKE_HOME/.cua-local"
+ echo '{}' > "$FAKE_HOME/.cua-local/credentials.json"
+ ln -s "$FAKE_HOME/.cua" "$FAKE_HOME/.cua-local/host" ;;
+ host-config|host-token|host-spaces)
+ # A real folder whose files are links into Stable's host folder: the
+ # CLI would read Stable's host config and token and unregister it.
+ mkdir -p "$FAKE_HOME/.cua-local/host"
+ echo '{}' > "$FAKE_HOME/.cua-local/credentials.json"
+ case "$linked" in
+ host-config) ln -s "$FAKE_HOME/.cua/host/config.json" "$FAKE_HOME/.cua-local/host/config.json" ;;
+ host-token) ln -s "$FAKE_HOME/.cua/host/machine-token" "$FAKE_HOME/.cua-local/host/machine-token" ;;
+ host-spaces) ln -s "$FAKE_HOME/.cua/host/provided-spaces.json" "$FAKE_HOME/.cua-local/host/provided-spaces.json" ;;
+ esac ;;
+ shared-helper)
+ # The helper folder is shared by every environment, so a link in it
+ # into Stable's counts for the prod home too.
+ mkdir -p "$FAKE_HOME/.cua-local/envs/prod"
+ echo '{}' > "$FAKE_HOME/.cua-local/envs/prod/credentials.json"
+ ln -s "$FAKE_HOME/.cua/helper" "$FAKE_HOME/.cua-local/helper" ;;
+ host-elsewhere)
+ # The host folder is a link to a folder of its own that holds the
+ # link into Stable's host folder: one indirection more.
+ mkdir -p "$WORK/elsewhere-host" "$FAKE_HOME/.cua-local"
+ echo '{}' > "$FAKE_HOME/.cua-local/credentials.json"
+ ln -s "$FAKE_HOME/.cua/host/config.json" "$WORK/elsewhere-host/config.json"
+ ln -s "$WORK/elsewhere-host" "$FAKE_HOME/.cua-local/host" ;;
+ helper-spec)
+ mkdir -p "$FAKE_HOME/.cua-local/helper"
+ echo '{}' > "$FAKE_HOME/.cua-local/credentials.json"
+ ln -s "$FAKE_HOME/.cua/helper/service.json" "$FAKE_HOME/.cua-local/helper/service.json" ;;
+ credentials)
+ mkdir -p "$FAKE_HOME/.cua-local"
+ ln -s "$FAKE_HOME/.cua/credentials.json" "$FAKE_HOME/.cua-local/credentials.json" ;;
+ esac
+ uninstall --yes
+ check "a Local $linked that is, or holds, a link into ~/.cua: the Local CLI is not run, and it says why (status $RC)" \
+ '[ "$RC" = 0 ] && [ ! -s "$LOCAL_CUA_LOG" ] && printf "%s" "$OUT" | grep -F >/dev/null "not taking this Mac off the staging relay or removing its coding agent entries"'
+ check "a Local $linked that is, or holds, a link into ~/.cua: only Local's own go, Stable files are byte-identical" \
+ '[ ! -e "$FAKE_HOME/.cua-local" ] && [ ! -L "$FAKE_HOME/.cua-local" ] && [ "$(snap_stable)" = "$STABLE_BEFORE" ]'
+done
+
+# A Local host folder kept elsewhere (a link to a folder that holds nothing of
+# the released app's) is fine: the CLI is run, and only the link goes.
+reset_fixture
+STABLE_BEFORE="$(snap_stable)"
+install
+mkdir -p "$WORK/own-host" "$FAKE_HOME/.cua-local"
+echo '{}' > "$FAKE_HOME/.cua-local/credentials.json"
+echo '{}' > "$WORK/own-host/config.json"
+ln -s "$WORK/own-host" "$FAKE_HOME/.cua-local/host"
+write_local_cli
+uninstall --yes
+check "a Local host folder kept elsewhere, with no link into ~/.cua: the Local CLI is run (status $RC)" \
+ '[ "$RC" = 0 ] && grep >/dev/null "^cua host remove" "$LOCAL_CUA_LOG" && grep >/dev/null "^cua agents remove" "$LOCAL_CUA_LOG" && [ ! -e "$FAKE_HOME/.cua-local" ] && [ -f "$WORK/own-host/config.json" ] && [ "$(snap_stable)" = "$STABLE_BEFORE" ]'
+reset_fixture
+STABLE_BEFORE="$(snap_stable)"
+
+# A Local that was used (it has a home), as the checks below need.
+used_local() {
+ reset_fixture
+ install
+ mkdir -p "$FAKE_HOME/.cua-local"
+ echo '{}' > "$FAKE_HOME/.cua-local/credentials.json"
+ echo '{}' > "$FAKE_HOME/.cua-local/agent-setup.json"
+ write_local_cli
+}
+
+# Not signed in: nothing to unregister, and nothing is said about it.
+used_local
+STUB_AUTH_STATUS=1 uninstall --yes
+check "not signed in: it asks nothing of the relay and says nothing" \
+ '[ "$RC" = 0 ] && grep >/dev/null "^cua auth status" "$LOCAL_CUA_LOG" && ! grep >/dev/null "host remove" "$LOCAL_CUA_LOG" && ! printf "%s" "$OUT" | grep >/dev/null -e "taking this Mac off" -e "could not take" -e warning'
+check "not signed in: the rest is removed all the same" '[ ! -e "$LOCAL_APP" ]'
+check "not signed in: the coding agents are still cleaned (it needs no sign-in)" 'grep >/dev/null "^cua agents remove --yes" "$LOCAL_CUA_LOG"'
+
+# Signed in but the relay cannot be reached: one warning, and the uninstall goes on.
+used_local
+STUB_HOST_REMOVE=1 uninstall --yes
+check "relay unreachable: one warning, and Local is removed all the same" \
+ '[ "$RC" = 0 ] && [ "$(printf "%s" "$OUT" | grep -c "could not take this Mac off the staging relay")" = 1 ] && [ ! -e "$LOCAL_APP" ]'
+check "relay unreachable: Stable files are byte-identical" '[ "$(snap_stable)" = "$STABLE_BEFORE" ]'
+check "relay unreachable: the coding agents are still cleaned" 'grep >/dev/null "^cua agents remove --yes" "$LOCAL_CUA_LOG"'
+
+# The Local CLI starts a program (an agent's own CLI) and hangs: the limit stops
+# the whole process group, so nothing keeps running (and editing an agent's
+# configuration) after the uninstall has finished; the uninstall goes on.
+used_local
+rm -f "$WORK/child.pid"
+start=$SECONDS
+CUA_UNINSTALL_CLI_SECS=2 STUB_AGENTS_REMOVE=hang STUB_CHILD_PID="$WORK/child.pid" uninstall --yes
+elapsed=$((SECONDS - start))
+check "agents remove hangs: given up at the limit, not waited for (${elapsed}s, status $RC)" \
+ '[ "$RC" = 0 ] && [ "$elapsed" -lt 30 ] && [ ! -e "$LOCAL_APP" ] && [ ! -e "$FAKE_HOME/.cua-local" ] && [ "$(printf "%s" "$OUT" | grep -c "could not remove Local.s entries from your coding agents")" = 1 ]'
+check "agents remove hangs: the program it started is stopped too" \
+ '[ -s "$WORK/child.pid" ] && ! kill -0 "$(cat "$WORK/child.pid")" 2>/dev/null'
+check "agents remove hangs: the shell prints nothing about it" '! printf "%s" "$OUT" | grep >/dev/null -e Terminated -e Killed'
+
+# The coding agents cannot be cleaned: one warning, and the uninstall goes on.
+used_local
+STUB_AGENTS_REMOVE=1 uninstall --yes
+check "agents remove fails: one warning naming what may be left, and Local is removed all the same" \
+ '[ "$RC" = 0 ] && [ "$(printf "%s" "$OUT" | grep -c "could not remove Local.s entries from your coding agents")" = 1 ] && printf "%s" "$OUT" | grep >/dev/null "MCP server and the \*-local skills" && [ ! -e "$LOCAL_APP" ] && [ ! -e "$LOCAL_CLI" ] && [ ! -e "$FAKE_HOME/.cua-local" ]'
+check "agents remove fails: the relay was still asked, and Stable files are byte-identical" \
+ 'grep >/dev/null "^cua host remove" "$LOCAL_CUA_LOG" && [ "$(snap_stable)" = "$STABLE_BEFORE" ]'
+
+# Nothing installed (or the app already gone): no CLI to ask, nothing said.
+: > "$LOCAL_CUA_LOG"
+uninstall --yes
+check "no Local app: it does not try the relay or the coding agents" '[ "$RC" = 0 ] && [ ! -s "$LOCAL_CUA_LOG" ] && ! printf "%s" "$OUT" | grep >/dev/null -e "taking this Mac off" -e "could not take" -e "removing Local.s MCP server" -e warning'
+# A Local home but no app (a half-removed install): there is no Local CLI to ask,
+# and the released app's cua is never used in its place.
+reset_fixture
+mkdir -p "$FAKE_HOME/.cua-local"
+echo '{}' > "$FAKE_HOME/.cua-local/credentials.json"
+echo '{}' > "$FAKE_HOME/.cua-local/agent-setup.json"
+STABLE_BEFORE="$(snap_stable)"
+: > "$STUB_LOG"
+uninstall --yes
+check "a Local home but no app: nothing is asked of any CLI (not Stable's either), and the home is removed (status $RC)" \
+ '[ "$RC" = 0 ] && [ ! -e "$FAKE_HOME/.cua-local" ] && ! grep >/dev/null "host remove\|agents remove" "$STUB_LOG" && [ "$(snap_stable)" = "$STABLE_BEFORE" ]'
+
echo "== install, then uninstall, leaves the HOME as it was =="
reset_fixture
ALL_BEFORE="$(snap_all)"
diff --git a/apps/cua-spaces-macos/scripts/uninstall-local.sh b/apps/cua-spaces-macos/scripts/uninstall-local.sh
index 3cab02c133..431e008c4f 100755
--- a/apps/cua-spaces-macos/scripts/uninstall-local.sh
+++ b/apps/cua-spaces-macos/scripts/uninstall-local.sh
@@ -16,6 +16,13 @@
# (default $HOME); the state is always removed from $HOME
#
# It removes, after asking:
+# - this Mac's entry in the relay, in each environment Local is signed in to
+# (best effort: the machine would otherwise stay listed as offline), before
+# the sign-in goes
+# - Local's MCP server (`cua-local`) and skills (`*-local`) in Claude Code and
+# the other coding agents (`cua-local agents remove`, best effort), before
+# the CLI and ~/.cua-local, which records what Local wrote, go; the released
+# app's entries and `cua-driver` stay
# - the Local helper login item (unregistered) and the Local host runner job
# - ~/Applications/Cua Spaces Local.app
# - ~/.local/bin/cua-local
@@ -119,6 +126,8 @@ echo " state: $LOCAL_HOME and $LOCAL_APP_SUPPORT"
echo " macOS leftovers: the $LOCAL_BUNDLE_ID preferences, caches, web view data and window state in ~/Library"
echo " (and those of the old id $LOCAL_LEGACY_BUNDLE_ID, if any)"
echo " Keychain items: ${LOCAL_KEYCHAIN_SERVICES[*]}"
+echo " relay: this Mac's entry, if Local is signed in"
+echo " coding agents: Local's MCP server ($LOCAL_CLI_NAME) and its *-local skills; the released app's entries stay"
if [ "$YES" != 1 ]; then
confirm "Remove them?" || { log "cancelled, nothing was removed"; exit 0; }
fi
@@ -130,6 +139,133 @@ elif [ "$YES" != 1 ] && lume_local_present; then
confirm "Also remove $LOCAL_LUME_BIN_NAME and its VMs in ~/$LOCAL_LUME_DIR_NAME?" && REMOVE_LUME=1
fi
+# --- This Mac's entry in the relay, and Local's entries in your coding agents --------
+
+# The bundled Local CLI for one environment (staging, or prod for the opt-in),
+# as Local and nothing else: every CUA_ variable of the caller's shell is
+# dropped, so a stray CUA_HOME or CUA_API_KEY for the released Cua Spaces can
+# neither aim it at that install nor hand its key to this one, and CUA_ENV is
+# the one asked for. It gives up after 30 seconds (CUA_UNINSTALL_CLI_SECS
+# changes that): the CLI runs in a process group of its own, and the whole group
+# is stopped, so a program it started (`claude mcp remove`) cannot go on editing
+# an agent's configuration after the uninstall has finished.
+run_local_cli() {
+ local cli="$1" env="$2" secs="${CUA_UNINSTALL_CLI_SECS:-30}" var
+ shift 2
+ (
+ for var in $(compgen -e); do
+ case "$var" in CUA_*) unset "$var" ;; esac
+ done
+ export CUA_CHANNEL=local CUA_ENV="$env" CUA_TELEMETRY=0
+ exec /usr/bin/perl -e '
+ my $secs = shift @ARGV;
+ setpgrp(0, 0);
+ my $pid = fork();
+ exit 127 unless defined $pid;
+ if (!$pid) { exec @ARGV or exit 127 }
+ $SIG{ALRM} = sub {
+ $SIG{TERM} = "IGNORE";
+ kill "TERM", -$$;
+ my $until = time + 2;
+ while (time < $until && waitpid($pid, 1) == 0) { select(undef, undef, undef, 0.1) }
+ kill "KILL", -$$;
+ exit 124;
+ };
+ alarm $secs;
+ waitpid($pid, 0);
+ my $st = $?;
+ exit($st & 127 ? 128 + ($st & 127) : $st >> 8);
+ ' -- "$secs" "$cli" "$@"
+ )
+}
+
+# Succeeds when every link in `dir` (down to `maxdepth` levels) resolves to a
+# place of its own and not to anything of the released Cua Spaces, and so does
+# every link inside the folders those links lead to (`levels` folders deep).
+# `find -H` looks inside `dir` when `dir` itself is a link.
+links_are_isolated() {
+ local dir="$1" levels="$2" maxdepth="$3" entry resolved
+ [ "$levels" -gt 0 ] || return 0
+ while IFS= read -r entry; do
+ resolved="$(canonicalize_path "$entry")" || return 1
+ reject_stable_overlap "Local state" "$resolved" >/dev/null 2>&1 || return 1
+ if [ -d "$resolved" ]; then
+ links_are_isolated "$resolved" $((levels - 1)) 4 || return 1
+ fi
+ done < <(find -H "$dir" -maxdepth "$maxdepth" -type l 2>/dev/null)
+}
+
+# Whether a Local home may be handed to the Local CLI: it, the folders the CLI
+# reads and removes in it, and every link in them, however indirect, resolve to
+# places of their own and not to anything of the released Cua Spaces (a link such
+# as ~/.cua-local -> ~/.cua, or ~/.cua-local/host/config.json ->
+# ~/.cua/host/config.json, or a host folder elsewhere that holds such a link,
+# would make the CLI unregister and delete Stable's host or its Spaces).
+local_home_is_isolated() {
+ local home="$1" sub resolved
+ for sub in "" /host /helper /agent-setup.json; do
+ resolved="$(canonicalize_path "$home$sub")" || return 1
+ reject_stable_overlap "Local state" "$resolved" >/dev/null 2>&1 || return 1
+ done
+ links_are_isolated "$home" 3 1 || return 1
+ links_are_isolated "$home/host" 3 4 || return 1
+ links_are_isolated "$home/helper" 3 4 || return 1
+ # The helper's spec is shared by every environment: it lives in the Local
+ # home itself, and `host remove` deletes it whichever environment it runs for.
+ resolved="$(canonicalize_path "$LOCAL_HOME/helper")" || return 1
+ reject_stable_overlap "Local state" "$resolved" >/dev/null 2>&1 || return 1
+ links_are_isolated "$LOCAL_HOME/helper" 3 4 || return 1
+}
+
+# Takes this Mac off the relay, while the sign-in and the host's machine token
+# still exist: they are what proves it may. Best effort. Not signed in (or no
+# app left to ask): there is nothing to unregister, so it says nothing.
+take_off_relay() {
+ local cli="$1" env="$2"
+ run_local_cli "$cli" "$env" auth status >/dev/null 2>&1 || return 0
+ log "taking this Mac off the $env relay"
+ if ! run_local_cli "$cli" "$env" host remove --force --uninstalling >/dev/null 2>&1; then
+ warn "could not take this Mac off the $env relay (offline?): it may stay listed, offline, in your computers until you remove it there."
+ fi
+}
+
+# Takes Local's MCP server (`cua-local`) and skills (`*-local`) out of Claude
+# Code and the other coding agents, while the home that records what Local
+# wrote is still there: without it they would point at a deleted binary.
+# `agents remove` undoes only what this home's record says Local wrote, so the
+# released Cua Spaces' `cua` entry and skills, and `cua-driver`, stay. Best
+# effort, with the same limit as the relay call; it needs no sign-in.
+remove_agent_entries() {
+ local cli="$1" env="$2"
+ log "removing Local's MCP server and skills from your coding agents"
+ if ! run_local_cli "$cli" "$env" agents remove --yes >/dev/null 2>&1; then
+ warn "could not remove Local's entries from your coding agents ($env): the $LOCAL_CLI_NAME MCP server and the *-local skills may still be listed there; remove them by hand."
+ fi
+}
+
+# For each environment Local has state for (staging, and prod when Local was
+# opted in to it): off the relay, then out of the coding agents. Nothing is
+# asked of the Local CLI when it is missing (a half-removed install) or when the
+# home leads into the released Cua Spaces' folders.
+unregister_local() {
+ local cli="$APP_DEST/Contents/MacOS/cua" env home
+ [ -f "$cli" ] && [ -x "$cli" ] && [ ! -L "$cli" ] || return 0
+ for env in staging prod; do
+ case "$env" in
+ staging) home="$LOCAL_HOME" ;;
+ *) home="$LOCAL_HOME/envs/prod" ;;
+ esac
+ [ -d "$home" ] || continue
+ if ! local_home_is_isolated "$home"; then
+ warn "not taking this Mac off the $env relay or removing its coding agent entries: $home leads into the released Cua Spaces' folders."
+ continue
+ fi
+ take_off_relay "$cli" "$env"
+ remove_agent_entries "$cli" "$env"
+ done
+}
+unregister_local
+
# --- The helper login item and the host runner ------------------------------------
# Only this bundle's own code may unregister its login item, so ask the app
diff --git a/libs/cua/crates/cua-agent-setup/src/lib.rs b/libs/cua/crates/cua-agent-setup/src/lib.rs
index a7fde08100..95261ddd50 100644
--- a/libs/cua/crates/cua-agent-setup/src/lib.rs
+++ b/libs/cua/crates/cua-agent-setup/src/lib.rs
@@ -1160,13 +1160,20 @@ impl AgentSetup {
let file = self.mcp_file(m);
// The channel's server (`cua`, or `cua-local`) always gets a
// row; other servers (`cua-driver`) only when cua registered
- // them there.
+ // them there, and never for Local (below).
let mut names = state.names_in(&file);
let own = self.env.channel.mcp_server_name();
if state.record(own, &file).is_none() {
names.insert(0, own.to_string());
}
for name in names {
+ // Local removes its own server and no other: `cua` is the
+ // released app's and `cua-driver` is Cua Driver's, names
+ // Local does not own even when its record says it wrote
+ // the entry first (CUA-1776).
+ if self.env.channel.is_local() && name != own {
+ continue;
+ }
let mut o = Outcome {
agents: vec![a.id.into()],
target: Target::Mcp,
diff --git a/libs/cua/crates/cua-cli/tests/agents_login.rs b/libs/cua/crates/cua-cli/tests/agents_login.rs
index 411f7e09b9..da7667f7ea 100644
--- a/libs/cua/crates/cua-cli/tests/agents_login.rs
+++ b/libs/cua/crates/cua-cli/tests/agents_login.rs
@@ -489,6 +489,169 @@ async fn the_local_cli_registers_cua_local_beside_stables_cua() {
assert!(stable_skill.join("SKILL.md").is_file() && !local_skill.exists());
}
+/// What `uninstall-local.sh` runs (`cua-local agents remove --yes`) removes
+/// Local's own MCP server and skills and nothing of the released app's: its
+/// `cua` and `cua-driver` entries and skills stay, also when Local was asked to
+/// set up `cua-driver` itself after Stable had (CUA-1776).
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn the_local_cli_removes_only_its_own_entries_and_leaves_stables_cua_and_cua_driver() {
+ let mut h = home();
+ mkdirs(&h, &[".cursor"]);
+ let cursor = h.dir.path().join(".cursor/mcp.json");
+ let root = h.dir.path().to_path_buf();
+ let skill = move |name: &str| root.join(".agents/skills").join(name);
+ let servers = |p: &std::path::Path| -> Vec<String> {
+ let v: serde_json::Value =
+ serde_json::from_str(&std::fs::read_to_string(p).unwrap()).unwrap();
+ let mut names: Vec<String> = v["mcpServers"]
+ .as_object()
+ .map(|o| o.keys().cloned().collect())
+ .unwrap_or_default();
+ names.sort();
+ names
+ };
+
+ // The released app: `cua`, and Cua Driver's `cua-driver`.
+ h.run(&[
+ "agents",
+ "setup",
+ "--agents",
+ "cursor",
+ "--yes",
+ "--mcp-command",
+ "/opt/cua/bin/cua",
+ ])
+ .await
+ .ok();
+ h.run(&[
+ "agents",
+ "setup",
+ "--cua-driver",
+ "--agents",
+ "cursor",
+ "--yes",
+ "--mcp-command",
+ "/opt/drv/cua-driver",
+ ])
+ .await
+ .ok();
+ assert_eq!(servers(&cursor), ["cua", "cua-driver"]);
+ let stable_config = std::fs::read_to_string(&cursor).unwrap();
+ assert!(skill("cua-spaces").join("SKILL.md").is_file());
+ assert!(skill("cua-driver").join("SKILL.md").is_file());
+
+ // Local, in its own home: `cua-local`, and (asked to) `cua-driver` too.
+ let local_home = h.dir.path().join(".cua-local");
+ h.set("CUA_CHANNEL", "local")
+ .set("CUA_HOME", local_home.display().to_string());
+ h.run(&[
+ "agents",
+ "setup",
+ "--agents",
+ "cursor",
+ "--yes",
+ "--mcp-command",
+ "/opt/cua-local/bin/cua-local",
+ ])
+ .await
+ .ok();
+ h.run(&[
+ "agents",
+ "setup",
+ "--cua-driver",
+ "--agents",
+ "cursor",
+ "--yes",
+ "--mcp-command",
+ "/opt/drv/cua-driver",
+ ])
+ .await
+ .ok();
+ assert_eq!(servers(&cursor), ["cua", "cua-driver", "cua-local"]);
+ assert!(skill("cua-spaces-local").join("SKILL.md").is_file());
+
+ // The uninstaller's call.
+ h.run(&["agents", "remove", "--yes"]).await.ok();
+ assert_eq!(
+ servers(&cursor),
+ ["cua", "cua-driver"],
+ "only Local's server went"
+ );
+ let left: serde_json::Value =
+ serde_json::from_str(&std::fs::read_to_string(&cursor).unwrap()).unwrap();
+ let before: serde_json::Value = serde_json::from_str(&stable_config).unwrap();
+ assert_eq!(left, before, "Stable's entries are exactly as they were");
+ assert!(!skill("cua-spaces-local").exists(), "Local's skills went");
+ assert!(
+ skill("cua-spaces").join("SKILL.md").is_file()
+ && skill("cua-driver").join("SKILL.md").is_file(),
+ "Stable's skills, cua-driver's included, stay"
+ );
+}
+
+/// The other order: Local registered `cua-driver` first and the released app
+/// later set up the same command. Local's record still says it wrote the entry,
+/// but the name is Cua Driver's, so removing Local's setup leaves it, and the
+/// released app's `cua-driver` skill, alone.
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn the_local_cli_leaves_cua_driver_alone_even_when_it_registered_it_first() {
+ let mut h = home();
+ mkdirs(&h, &[".cursor"]);
+ let cursor = h.dir.path().join(".cursor/mcp.json");
+ let root = h.dir.path().to_path_buf();
+ let skill = move |name: &str| root.join(".agents/skills").join(name);
+ let driver = [
+ "agents",
+ "setup",
+ "--cua-driver",
+ "--agents",
+ "cursor",
+ "--yes",
+ "--mcp-command",
+ "/opt/drv/cua-driver",
+ ];
+
+ let local_home = h.dir.path().join(".cua-local");
+ h.set("CUA_CHANNEL", "local")
+ .set("CUA_HOME", local_home.display().to_string());
+ h.run(&driver).await.ok();
+ h.run(&[
+ "agents",
+ "setup",
+ "--agents",
+ "cursor",
+ "--yes",
+ "--mcp-command",
+ "/opt/cua-local/bin/cua-local",
+ ])
+ .await
+ .ok();
+ // Then the released app, its own home, the same command.
+ let stable_home = h.dir.path().join(".cua").display().to_string();
+ h.set("CUA_CHANNEL", "stable").set("CUA_HOME", stable_home);
+ h.run(&driver).await.ok();
+ assert!(skill("cua-driver").join("SKILL.md").is_file());
+
+ h.set("CUA_CHANNEL", "local")
+ .set("CUA_HOME", local_home.display().to_string());
+ h.run(&["agents", "remove", "--yes"]).await.ok();
+ let left: serde_json::Value =
+ serde_json::from_str(&std::fs::read_to_string(&cursor).unwrap()).unwrap();
+ assert!(left["mcpServers"]["cua-local"].is_null(), "{left}");
+ assert_eq!(
+ left["mcpServers"]["cua-driver"]["command"], "/opt/drv/cua-driver",
+ "Cua Driver's server stays: {left}"
+ );
+ assert!(
+ skill("cua-driver").join("SKILL.md").is_file(),
+ "the released app's cua-driver skill stays"
+ );
+ assert!(
+ !skill("cua-driver-local").exists() && !skill("cua-spaces-local").exists(),
+ "Local's own skills went"
+ );
+}
+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn agents_setup_cua_driver_adds_the_driver_skill_and_server() {
let h = home();