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.

56.8 KB
Raw Blame
#!/usr/bin/env bash
# cua-driver uninstaller (Rust implementation only). Mirrors uninstall.ps1 on
# Windows: one canonical script per shell, no private `_uninstall-rust.sh`
# helper.
#
# Behaviour by host + flag:
#   all hosts + no flag             → Rust uninstall
#   --backend=rust/swift            → no-op (Rust is the only supported backend)
#   --experimental-rust             → legacy alias (no-op)
#
# Swift uninstall removes:
#   - ~/.local/bin/cua-driver symlink (+ legacy /usr/local/bin/cua-driver)
#   - /Applications/CuaDriver.app bundle
#   - ~/.cua-driver/ (telemetry id + install marker)
#   - ~/Library/Application Support/Cua Driver/ (config.json)
#   - ~/Library/Caches/cua-driver/ (daemon/cache state)
#   - Skill symlinks under ~/.claude/skills/cua-driver, ~/.agents/skills/
#     cua-driver, ~/.openclaw/skills/cua-driver, ~/.config/opencode/
#     skills/cua-driver (only when they point at our app bundle)
#   - Claude MCP registrations in ~/.claude.json (cua-driver / cua-computer-use)
#
# Rust uninstall removes:
#   Linux:
#     - ~/.local/bin/cua-driver symlink (only when it resolves to a
#       cua-driver path — a Swift-driver symlink is left in place)
#     - versioned packages/current symlink under ~/.cua-driver/
#     - telemetry id, preference, and registration markers are preserved by
#       default so a reinstall remains the same pseudonymous installation
#     - ~/.config/systemd/user/cua-driver.service (if --autostart
#       was used via install-local.sh — stop + disable + remove), plus the
#       legacy cua-driver-rs.service unit
#     - Skill symlinks under ~/.claude/skills/cua-driver(-rs), ~/.agents/
#       skills/…, ~/.openclaw/skills/…, ~/.config/opencode/skills/…
#   macOS:
#     - /Applications/CuaDriver.app bundle (+ legacy CuaDriverRs.app)
#     - ~/.local/bin/cua-driver symlink (only when it resolves into
#       /Applications/CuaDriver.app)
#     - runtime payloads under ~/.cua-driver/ and legacy ~/.cua-driver-rs/;
#       telemetry state remains unless --purge is passed
#     - ~/Library/LaunchAgents/com.trycua.cua-driver.plist (if --autostart
#       was used via install-local.sh — unload + remove), plus the legacy
#       com.trycua.cua-driver-rs.plist LaunchAgent
#     - Skill symlinks under ~/.claude/skills/cua-driver(-rs), etc.
#
# Shared-path safety: /Applications/CuaDriver.app + its ~/.local/bin
# symlink use the same bundle id (com.trycua.driver) as the Swift driver,
# so they're only removed when an unambiguous Rust marker is on disk
# (~/.cua-driver/packages/, legacy ~/.cua-driver-rs/, CuaDriverRs.app,
# the LaunchAgent/systemd unit, or current Rust telemetry state).
#
# Also scrubs release-owned Claude MCP registrations in ~/.claude.json.
# Entries are removed only when their original absolute command resolves to a
# live file in a directory selected for recursive deletion. Unproven entries
# (including missing, relative and legacy-name-only commands) are kept.
#
# Keeps the macOS permissions (Accessibility + Screen Recording) by default, so
# an accidental or deliberate uninstall followed by a reinstall of the same
# signed app needs no new approval. Pass --reset-permissions to revoke them
# (Accessibility, Screen Recording and Automation for com.trycua.driver).
# --reset-tcc is the older spelling; --keep-tcc is accepted and does nothing.
#
# Usage:
#   /bin/bash -c "$(curl -fsSL https://cua.ai/driver/uninstall.sh)"
#   /bin/bash -c "$(curl -fsSL https://cua.ai/driver/uninstall.sh)" -- --purge
#
# Env overrides (mirror install side):
#   CUA_DRIVER_HOME       Rust package home to remove (default ~/.cua-driver)
#   CUA_DRIVER_RS_HOME    Legacy alias for CUA_DRIVER_HOME
set -euo pipefail

# ----------------------------------------------------------------------
# Flag parsing — same two-pass shape as install.sh so the argv shapes
# stay bit-compatible across install/uninstall and a future Rust-only
# flag flows through without edits.
# ----------------------------------------------------------------------
USE_RUST_BACKEND=1
RESET_TCC=0
PURGE_DATA=0
FORWARDED_ARGS=()
PASSTHROUGH=0
while [[ $# -gt 0 ]]; do
    if [[ "$PASSTHROUGH" == "1" ]]; then
        FORWARDED_ARGS+=("$1"); shift; continue
    fi
    case "$1" in
        --experimental-rust) shift ;;  # legacy alias for default Rust path
        --backend=rust)      shift ;;
        --backend=swift)     shift ;;  # retired Swift (no-op)
        --reset-permissions) RESET_TCC=1; shift ;;  # opt in: revoke the macOS permissions
        --reset-tcc)         RESET_TCC=1; shift ;;  # older spelling of --reset-permissions
        --keep-tcc)          RESET_TCC=0; shift ;;  # the default; accepted for old callers
        --purge)             PURGE_DATA=1; shift ;;  # also delete pseudonymous identity + preference
        --backend=*)
            printf 'error: unknown backend %q; supported: rust\n' "${1#*=}" >&2
            exit 2
            ;;
        --)                  PASSTHROUGH=1; shift ;;  # forward the rest verbatim
        *)                   FORWARDED_ARGS+=("$1"); shift ;;
    esac
done

# Legacy --backend=swift is accepted as a no-op for backward compat.
OS="$(uname -s 2>/dev/null || echo unknown)"
if [[ "$USE_RUST_BACKEND" == "1" ]]; then
    if [[ "$OS" != "Darwin" ]]; then
        printf 'note: detected non-macOS host (%s); uninstalling cua-driver via the Rust implementation.\n' "$OS" >&2
    else
        printf 'note: uninstalling cua-driver via the Rust implementation.\n' >&2
    fi
fi

# ----------------------------------------------------------------------
# Shared helpers
# ----------------------------------------------------------------------
log() { printf '==> %s\n' "$*"; }

report_surviving_local_install() {
    local local_home="${CUA_DRIVER_LOCAL_HOME:-$HOME/.cua-driver-local}"
    local local_bin_dir="${CUA_DRIVER_LOCAL_INSTALL_DIR:-$HOME/.local/bin}"
    local local_cli="$local_bin_dir/cua-driver-local"
    local local_marker="$local_home/packages/current/cua-driver-local"
    local path_cli=""
    local survivor=""

    path_cli="$(command -v cua-driver-local 2>/dev/null || true)"
    if [[ -e "$local_cli" || -L "$local_cli" ]]; then
        survivor="$local_cli"
    elif [[ -e "$local_marker" || -L "$local_marker" ]]; then
        survivor="$local_marker"
    elif [[ -n "$path_cli" ]]; then
        survivor="$path_cli"
    elif [[ "$OS" == "Darwin" && -x "/Applications/CuaDriverLocal.app/Contents/MacOS/cua-driver-local" ]]; then
        survivor="/Applications/CuaDriverLocal.app"
    else
        return 0
    fi

    cat <<LOCALUNMSG

Note: a separate source-built cua-driver-local installation remains at $survivor.
The cua-driver uninstaller intentionally leaves this local product untouched.
To remove it, run from your Cua checkout:

  ./libs/cua-driver/scripts/uninstall-local.sh
LOCALUNMSG
}

purge_macos_history() {
    local app_bundle="$1"
    local helper="$2"
    local rust_install_present="$3"
    local codesign_tool="$4"
    if [[ "$rust_install_present" != "1" || ! -x "$helper" ]] \
        || ! "$codesign_tool" --verify --deep --strict "$app_bundle" >/dev/null 2>&1; then
        printf 'history_purge_incomplete: installed signed Cua Driver helper unavailable; preserved history state for retry\n' >&2
        return 1
    fi
    if ! "$helper" history purge-offline --yes; then
        printf 'history_purge_incomplete: exact-namespace key destruction was not verified; preserved history state and app for retry\n' >&2
        return 1
    fi
}

purge_linux_history() {
    local helper="$1"
    local rust_install_present="$2"
    if [[ "$rust_install_present" != "1" || ! -x "$helper" ]]; then
        printf 'history_purge_incomplete: installed Cua Driver helper unavailable; preserved history state for retry\n' >&2
        return 1
    fi
    if ! "$helper" history purge-offline --yes; then
        printf 'history_purge_incomplete: exact-namespace Secret Service key destruction was not verified; preserved history state and runtime for retry\n' >&2
        return 1
    fi
}

daemon_pid_file_path() {
    case "$OS" in
        Darwin) printf '%s/Library/Caches/cua-driver/cua-driver.pid' "$HOME" ;;
        Linux)  printf '%s/.cache/cua-driver/cua-driver.pid' "$HOME" ;;
        *)      printf '/tmp/cua-driver.pid' ;;
    esac
}

read_daemon_pid() {
    local pid
    [[ -r "$1" ]] || return 1
    IFS= read -r pid < "$1" || true
    [[ "$pid" =~ ^[1-9][0-9]*$ ]] || return 1
    printf '%s' "$pid"
}

daemon_pid_alive() { kill -0 "$1" 2>/dev/null; }

daemon_process_generation() {
    local start=""
    command -v ps >/dev/null 2>&1 || return 2
    start="$(LC_ALL=C ps -ww -o lstart= -p "$1" 2>/dev/null)" || return 2
    start="${start#"${start%%[![:space:]]*}"}"
    start="${start%"${start##*[![:space:]]}"}"
    [[ -n "$start" ]] || return 2
    printf '%s' "$start"
}

daemon_pid_matches_generation() {
    local pid="$1" expected="$2" current="" identity_status=0
    daemon_pid_alive "$pid" || return 1
    current="$(daemon_process_generation "$pid")" || return 2
    [[ "$current" == "$expected" ]] || return 1
    daemon_pid_is_release "$pid" || identity_status=$?
    [[ "$identity_status" == "0" ]] && return 0
    [[ "$identity_status" == "2" ]] && return 2
    return 1
}

daemon_signal_if_current() {
    local status=0
    daemon_pid_matches_generation "$1" "$2" || status=$?
    [[ "$status" == "2" ]] && return 2
    [[ "$status" == "0" ]] || return 1
    kill -"$3" "$1" 2>/dev/null || return 1
}

daemon_wait_for_exit() {
    local pid="$1" attempts=0 limit=$(( ${CUA_DRIVER_STOP_TIMEOUT:-5} * 10 ))
    while daemon_pid_alive "$pid"; do
        [[ "$attempts" -lt "$limit" ]] || return 1
        sleep 0.1 2>/dev/null || sleep 1 || true
        attempts=$((attempts + 1))
    done
}

daemon_process_identity() {
    local pid="$1" identity=""
    if [[ -L "/proc/$pid/exe" ]]; then
        identity="$(readlink "/proc/$pid/exe" 2>/dev/null || true)"
        identity="${identity% (deleted)}"
    fi
    if [[ -z "$identity" ]]; then
        command -v ps >/dev/null 2>&1 || return 2
        identity="$(ps -ww -o command= -p "$pid" 2>/dev/null)" || return 2
    fi
    identity="${identity#"${identity%%[![:space:]]*}"}"
    identity="${identity%"${identity##*[![:space:]]}"}"
    [[ -n "$identity" ]] || return 1
    printf '%s' "$identity"
}

# A daemon can run from ANY copy of CuaDriver.app (an old release in ~/Downloads, the
# Trash, a benchmark folder), not only the one this script removes. These three
# helpers identify a Cua Driver process by its bundle id instead of its path. They
# are copied verbatim from _install-common.sh (a test keeps the copies identical)
# because this script must work standalone via curl | bash.
CUA_DRIVER_RELEASE_BUNDLE_ID="com.trycua.driver"

cua_driver_plist_string() {
    local plist="$1" key="$2" value=""
    [ -f "$plist" ] || return 1
    if [ -x /usr/libexec/PlistBuddy ]; then
        value="$(/usr/libexec/PlistBuddy -c "Print :$key" "$plist" 2>/dev/null)" || value=""
    fi
    if [ -z "$value" ]; then
        value="$(awk -v key="$key" '
            function text(line) { sub(".*<string>", "", line); sub("</string>.*", "", line); return line }
            index($0, "<key>" key "</key>") {
                if ($0 ~ /<string>/) { print text($0); exit }
                want = 1; next
            }
            want && /<string>/ { print text($0); exit }
            want && /<key>/ { exit }
        ' "$plist" 2>/dev/null)" || value=""
    fi
    [ -n "$value" ] || return 1
    printf '%s' "$value"
}

cua_driver_bundle_of_command() {
    local command_line="$1" bundle
    case "$command_line" in
        */Contents/MacOS/cua-driver|*/Contents/MacOS/cua-driver\ *) ;;
        *) return 1 ;;
    esac
    bundle="${command_line%%/Contents/MacOS/cua-driver*}"
    case "$bundle" in
        /*.app) printf '%s' "$bundle" ;;
        *) return 1 ;;
    esac
}

cua_driver_bundle_is_release() {
    local bundle="$1" id=""
    id="$(cua_driver_plist_string "$bundle/Contents/Info.plist" CFBundleIdentifier)" || id=""
    if [ -n "$id" ]; then
        [ "$id" = "$CUA_DRIVER_RELEASE_BUNDLE_ID" ]
        return
    fi
    case "${bundle##*/}" in
        CuaDriver.app|CuaDriver-*.app|"CuaDriver "*.app) return 0 ;;
        *) return 1 ;;
    esac
}

cua_driver_ancestor_pids() {
    local pid="$$" depth=0 parent
    while [ -n "$pid" ] && [ "$pid" -gt 1 ] 2>/dev/null && [ "$depth" -lt 32 ]; do
        printf '%s\n' "$pid"
        parent="$(ps -o ppid= -p "$pid" 2>/dev/null | tr -d '[:space:]')" || break
        [ -n "$parent" ] || break
        pid="$parent"
        depth=$((depth + 1))
    done
}

cua_driver_run_bounded() {
    local limit="$1" child tenths
    shift
    "$@" >/dev/null 2>&1 &
    child=$!
    tenths=$((limit * 10))
    while kill -0 "$child" 2>/dev/null && [ "$tenths" -gt 0 ]; do
        sleep 0.1 2>/dev/null || sleep 1
        tenths=$((tenths - 1))
    done
    kill "$child" 2>/dev/null || true
    wait "$child" 2>/dev/null || true
}

daemon_identity_matches_install() {
    case "$1" in
        "$APP_BUNDLE"/Contents/MacOS/cua-driver|"$APP_BUNDLE"/Contents/MacOS/cua-driver[[:space:]]*|\
        "$LEGACY_APP_BUNDLE"/Contents/MacOS/cua-driver|"$LEGACY_APP_BUNDLE"/Contents/MacOS/cua-driver[[:space:]]*|\
        "$USER_BIN_LINK"|"$USER_BIN_LINK"[[:space:]]*|\
        "$HOME_DIR"/packages/current/cua-driver|"$HOME_DIR"/packages/current/cua-driver[[:space:]]*|\
        "$HOME_DIR"/packages/releases/*/cua-driver|"$HOME_DIR"/packages/releases/*/cua-driver[[:space:]]*|\
        "$LEGACY_HOME_DIR"/packages/current/cua-driver|"$LEGACY_HOME_DIR"/packages/current/cua-driver[[:space:]]*|\
        "$LEGACY_HOME_DIR"/packages/releases/*/cua-driver|"$LEGACY_HOME_DIR"/packages/releases/*/cua-driver[[:space:]]*) return 0 ;;
    esac
    # Not one of our install paths: still ours when it runs a com.trycua.driver
    # app bundle from anywhere else.
    local bundle
    bundle="$(cua_driver_bundle_of_command "$1")" || return 1
    cua_driver_bundle_is_release "$bundle"
}

daemon_pid_is_release() {
    local identity
    identity="$(daemon_process_identity "$1" 2>/dev/null)" || return 2
    daemon_identity_matches_install "$identity"
}

release_daemon_fallback_pids() {
    command -v pgrep >/dev/null 2>&1 || return 2
    command -v ps >/dev/null 2>&1 || return 2
    local uid pid identity candidates="" pgrep_status=0 ancestors
    uid="$(id -u 2>/dev/null || true)"
    [[ "$uid" =~ ^[0-9]+$ ]] || return 2
    # Never select this script or the process that started it.
    ancestors="$(cua_driver_ancestor_pids)"

    candidates="$(pgrep -U "$uid" -f '(^|[[:space:]/])cua-driver([[:space:]]|$)' 2>/dev/null)" || pgrep_status=$?
    case "$pgrep_status" in
        0) ;;
        1) return 0 ;;
        *) return 2 ;;
    esac

    while IFS= read -r pid; do
        [[ -n "$pid" ]] || continue
        [[ "$pid" =~ ^[1-9][0-9]*$ ]] || return 2
        printf '%s\n' "$ancestors" | grep -qx "$pid" && continue
        identity="$(daemon_process_identity "$pid" 2>/dev/null)" || return 2
        if daemon_identity_matches_install "$identity"; then
            printf '%s\n' "$pid"
        fi
    done <<< "$candidates"
}

verify_release_daemon_absent() {
    local pids="" status=0 waited=0 limit=$(( ${CUA_DRIVER_STOP_TIMEOUT:-5} * 2 ))
    while :; do
        status=0
        pids="$(release_daemon_fallback_pids 2>/dev/null)" || status=$?
        if [[ "$status" != "0" ]]; then
            printf 'daemon_stop_incomplete: process inspection failed while verifying release daemon shutdown.\n' >&2
            return 1
        fi
        [[ -n "$pids" ]] || return 0
        # A stop request starts short-lived cua-driver processes of its own (the
        # CLI re-executes itself); give them a moment before failing.
        [[ "$waited" -lt "$limit" ]] || break
        sleep 0.5 2>/dev/null || sleep 1
        waited=$((waited + 1))
    done
    printf 'daemon_stop_incomplete: Cua Driver process(es) still running after SIGTERM and SIGKILL (pid: %s).\n' \
        "$(printf '%s' "$pids" | tr '\n' ' ' | sed 's/ *$//')" >&2
    return 1
}

# Stop every Cua Driver process of this user that is not the pid-file daemon:
# daemons started from another copy of the app, and extra instances. Each gets,
# in order, a graceful stop request through its own bundle's CLI (or the trusted
# installed helper), SIGTERM, then SIGKILL, with CUA_DRIVER_STOP_TIMEOUT seconds
# (default 5) between steps. A pid is re-identified and its start time compared
# before every signal, so a recycled pid is never signalled. Other users'
# processes and other apps (any other bundle id) are never selected.
stop_stray_release_daemons() {
    local pids="" status=0 pid identity bundle version label generation helper how
    pids="$(release_daemon_fallback_pids 2>/dev/null)" || status=$?
    # Inspection failures are reported (and fail closed) by verify_release_daemon_absent.
    [[ "$status" == "0" && -n "$pids" ]] || return 0
    while IFS= read -r pid; do
        [[ -n "$pid" ]] || continue
        daemon_pid_alive "$pid" || continue
        identity="$(daemon_process_identity "$pid" 2>/dev/null)" || continue
        generation="$(daemon_process_generation "$pid" 2>/dev/null)" || continue
        bundle="$(cua_driver_bundle_of_command "$identity" || true)"
        version=""
        [[ -z "$bundle" ]] || version="$(cua_driver_plist_string "$bundle/Contents/Info.plist" CFBundleShortVersionString || true)"
        label="pid $pid, ${version:+version $version, }${bundle:-$identity}"
        how=""

        helper="${bundle:+$bundle/Contents/MacOS/cua-driver}"
        [[ -n "$helper" && -x "$helper" ]] || helper="${DAEMON_STOP_HELPER:-}"
        if [[ -n "$helper" && -x "$helper" ]] && daemon_pid_matches_generation "$pid" "$generation"; then
            cua_driver_run_bounded "${CUA_DRIVER_STOP_TIMEOUT:-5}" "$helper" --expected-pid "$pid" stop
            if daemon_wait_for_exit "$pid"; then how="a graceful stop request"; fi
        fi
        if [[ -z "$how" ]] && daemon_signal_if_current "$pid" "$generation" TERM; then
            if daemon_wait_for_exit "$pid"; then how="SIGTERM"; fi
        fi
        if [[ -z "$how" ]] && daemon_signal_if_current "$pid" "$generation" KILL; then
            if daemon_wait_for_exit "$pid"; then
                how="SIGKILL after it ignored SIGTERM for ${CUA_DRIVER_STOP_TIMEOUT:-5}s"
            fi
        fi
        if [[ -z "$how" ]] && ! daemon_pid_alive "$pid"; then how="its own exit"; fi
        if [[ -n "$how" ]]; then
            log "stopped Cua Driver ($label) with $how"
        else
            printf 'daemon_stop_incomplete: could not stop Cua Driver (%s); it is still running.\n' "$label" >&2
        fi
    done <<< "$pids"
    return 0
}

stop_release_daemon() {
    local pid="" helper="${DAEMON_STOP_HELPER:-}" generation="" status=0 helper_stderr="" label="" bundle="" version=""
    if [[ -n "${DAEMON_PID_FILE:-}" ]] \
        && pid="$(read_daemon_pid "$DAEMON_PID_FILE" 2>/dev/null)" \
        && daemon_pid_alive "$pid"; then
        # Describe the daemon now: once it has exited there is nothing left to read.
        label="$(daemon_process_identity "$pid" 2>/dev/null || true)"
        bundle="$(cua_driver_bundle_of_command "$label" || true)"
        if [[ -n "$bundle" ]]; then
            version="$(cua_driver_plist_string "$bundle/Contents/Info.plist" CFBundleShortVersionString || true)"
            label="$bundle"
        fi
        label="pid $pid, ${version:+version $version, }${label:-unknown path}"
        daemon_pid_is_release "$pid" || status=$?
        if [[ "$status" == "2" ]]; then
            printf 'daemon_stop_incomplete: failed to identify live daemon pid %s safely.\n' "$pid" >&2
            return 1
        fi
        if [[ "$status" == "0" ]] && [[ ! -n "$helper" || ! -x "$helper" ]]; then
            # The installed copy is gone or was never the one running (the daemon
            # belongs to another copy of the app): the signal sweep below handles it.
            printf 'note: no trusted stop helper for pid %s; stopping it with signals.\n' "$pid" >&2
        elif [[ "$status" == "0" ]]; then
            generation="$(daemon_process_generation "$pid")" || {
                printf 'daemon_stop_incomplete: failed to capture process generation for daemon pid %s.\n' "$pid" >&2
                return 1
            }
            status=0
            helper_stderr="$("$helper" --expected-pid "$pid" stop 2>&1 >/dev/null)" || status=$?
            if [[ "$status" != "0" ]]; then
                printf 'note: trusted stop helper exited %s for pid %s; falling back to signals.\n' "$status" "$pid" >&2
                [[ -z "$helper_stderr" ]] || printf '%s\n' "$helper_stderr" >&2
            fi
            if ! daemon_wait_for_exit "$pid"; then
                status=0
                daemon_signal_if_current "$pid" "$generation" TERM || status=$?
                [[ "$status" != "2" ]] || {
                    printf 'daemon_stop_incomplete: failed to revalidate daemon pid %s before TERM.\n' "$pid" >&2
                    return 1
                }
                if [[ "$status" == "0" ]] && ! daemon_wait_for_exit "$pid"; then
                    status=0
                    daemon_signal_if_current "$pid" "$generation" KILL || status=$?
                    [[ "$status" != "2" ]] || {
                        printf 'daemon_stop_incomplete: failed to revalidate daemon pid %s before KILL.\n' "$pid" >&2
                        return 1
                    }
                    if [[ "$status" == "0" ]] && ! daemon_wait_for_exit "$pid"; then
                        printf 'daemon_stop_incomplete: validated release daemon pid %s is still running.\n' "$pid" >&2
                        return 1
                    fi
                fi
            fi
            log "stopped Cua Driver daemon ($label)"
        fi
    fi
    stop_stray_release_daemons
    sleep 0.2 2>/dev/null || true
    verify_release_daemon_absent
}

reject_root_invocation() {
    local effective_uid="$1"
    if [[ "$effective_uid" == "0" ]]; then
        printf 'error: do not run the Cua Driver uninstaller with sudo; run it as the login user so Computer History is purged from the correct home directory and native credential store. The script elevates only protected app removal when needed.\n' >&2
        return 77
    fi
}

# TCC revocation is opt-in (--reset-permissions). The grants belong to the
# signed app identity, not to the files, so they survive an uninstall followed
# by a reinstall; wiping them by default made every accidental uninstall cost a
# manual re-approval in System Settings. Nothing that calls this script on a
# user's behalf (cua uninstall.sh --all, tests) passes the flag.
# When enabled, revoke Accessibility + Screen-Recording + Automation for
# com.trycua.driver. macOS-only; no-op elsewhere.
maybe_reset_tcc() {
    [[ "$RESET_TCC" == "1" ]] || return 0
    if [[ "$OS" != "Darwin" ]]; then
        log "TCC reset is macOS-only; nothing to revoke on $OS"
        return 0
    fi
    if ! command -v tccutil >/dev/null 2>&1; then
        log "TCC reset: tccutil not found; skipping"
        return 0
    fi
    # `tccutil reset <svc> com.trycua.driver` resolves the bundle id through
    # LaunchServices. If the bundle isn't registered — or this runs AFTER the
    # app was removed — tccutil fails with -10814 and the grant silently
    # survives. This MUST run while /Applications/CuaDriver.app still exists;
    # force a synchronous LaunchServices registration first so a present-but-
    # not-yet-registered bundle (fresh install race) still resolves.
    local app_bundle="/Applications/CuaDriver.app"
    if [[ -d "$app_bundle" ]]; then
        local lsregister="/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"
        if [[ -x "$lsregister" ]]; then
            "$lsregister" -f "$app_bundle" >/dev/null 2>&1 || true
        fi
    else
        log "  warning: CuaDriver.app already removed; TCC reset may not resolve the bundle id"
    fi
    log "revoking TCC grants for com.trycua.driver"
    log "  note: com.trycua.driver is shared with the retired Swift driver;"
    log "  this clears grants for both."
    for SVC in Accessibility ScreenCapture AppleEvents; do
        if tccutil reset "$SVC" com.trycua.driver >/dev/null 2>&1; then
            log "  reset $SVC"
        else
            log "  $SVC: nothing to reset (or reset failed)"
        fi
    done
}

# Resolve a symlink target to an absolute path. realpath -e fails when
# the target is missing — we want to inspect dangling symlinks too (a
# leftover from a half-removed install should still be cleaned up), so
# fall back to readlink + manual normalize when realpath errors out.
resolve_link() {
    local link="$1"
    if [[ ! -L "$link" ]]; then
        printf ''; return 0
    fi
    local target
    if target="$(realpath "$link" 2>/dev/null)"; then
        printf '%s' "$target"
        return 0
    fi
    target="$(readlink "$link" 2>/dev/null || true)"
    case "$target" in
        /*) printf '%s' "$target" ;;
        *)  printf '%s/%s' "$(cd -- "$(dirname -- "$link")" && pwd)" "$target" ;;
    esac
}

select_daemon_stop_helper() {
    local candidate resolved
    for candidate in \
        "$APP_BUNDLE/Contents/MacOS/cua-driver" \
        "$LEGACY_APP_BUNDLE/Contents/MacOS/cua-driver" \
        "$PACKAGES_DIR/current/cua-driver" \
        "$LEGACY_HOME_DIR/packages/current/cua-driver"; do
        [[ -x "$candidate" ]] || continue
        resolved="$(realpath "$candidate" 2>/dev/null || true)"
        [[ -n "$resolved" ]] || resolved="$candidate"
        case "$resolved" in
            "$APP_BUNDLE/Contents/MacOS/cua-driver"|"$LEGACY_APP_BUNDLE/Contents/MacOS/cua-driver"|\
            "$HOME_DIR"/packages/releases/*/cua-driver|"$LEGACY_HOME_DIR"/packages/releases/*/cua-driver)
                printf '%s' "$resolved"
                return 0
                ;;
        esac
    done
    return 1
}

release_supervisor() {
    local action="$1" path name found=0
    case "$OS" in
        Linux)
            for path in "$SYSTEMD_USER_UNIT" "$LEGACY_SYSTEMD_USER_UNIT"; do
                [[ -f "$path" ]] || continue
                found=1
                name="${path##*/}"
                if [[ "$action" == "stop" ]]; then
                    command -v systemctl >/dev/null 2>&1 \
                        && systemctl --user stop "$name" >/dev/null 2>&1 || {
                            printf 'daemon_stop_incomplete: failed to stop systemd user unit %s.\n' "$name" >&2
                            return 1
                        }
                else
                    command -v systemctl >/dev/null 2>&1 \
                        && systemctl --user disable "$name" >/dev/null 2>&1 || true
                    rm -f "$path"
                    log "disabled + removed systemd --user unit $name"
                fi
            done
            if [[ "$action" == "remove" ]]; then
                [[ "$found" == "1" ]] || log "no current or legacy systemd --user unit found (skipping)"
                command -v systemctl >/dev/null 2>&1 \
                    && systemctl --user daemon-reload >/dev/null 2>&1 || true
            fi
            ;;
        Darwin)
            for path in "$LAUNCHAGENT_PLIST" "$LEGACY_LAUNCHAGENT_PLIST"; do
                [[ -f "$path" ]] || continue
                found=1
                if [[ "$action" == "stop" ]]; then
                    command -v launchctl >/dev/null 2>&1 \
                        && launchctl unload "$path" >/dev/null 2>&1 || {
                            printf 'daemon_stop_incomplete: failed to unload LaunchAgent %s.\n' "$path" >&2
                            return 1
                        }
                else
                    rm -f "$path"
                    log "removed LaunchAgent $path"
                fi
            done
            if [[ "$action" == "remove" && "$found" == "0" ]]; then
                log "no current or legacy LaunchAgent found (skipping)"
            fi
            ;;
    esac
}

# One standalone helper for preflight and fresh cleanup. Keep its heredoc out of
# command substitution: macOS Bash 3.2 parses quotes differently inside $().
claude_release_config() {
    local mode="$1"
    shift
    if [[ ! -e "$CLAUDE_JSON" && ! -L "$CLAUDE_JSON" ]]; then
        return 0
    fi
    if ! command -v python3 >/dev/null 2>&1; then
        printf 'error: python3 is required to safely inspect Claude MCP ownership before uninstalling a release with %s; install python3 and retry. No release files were removed.\n' "$CLAUDE_JSON" >&2
        return 1
    fi
    python3 - "$mode" "$CLAUDE_JSON" "$@" <<'PYCLAUDE'
import io
import json
import os
import shutil
import stat
import sys
import tempfile


def clean():
    mode, path, *payloads = sys.argv[1:]
    try:
        with open(path, "rb") as handle:
            original = handle.read()
        data = json.loads(original.decode("utf-8"))
        if not isinstance(data, dict):
            raise ValueError("top-level value must be a JSON object")
    except Exception as exc:
        raise ValueError(f"could not read Claude config {path}: {exc}") from exc

    # These exact operands also drive rm below. A final symlink is only
    # unlinked; symlinks in its parent directories are traversed by rm.
    roots = set()
    for payload in payloads:
        if not payload:
            continue
        try:
            before = os.lstat(payload)
        except FileNotFoundError:
            continue
        if not stat.S_ISDIR(before.st_mode):
            continue
        resolved = os.path.realpath(payload)
        after = os.stat(resolved)
        if resolved == os.path.sep or not os.path.samestat(before, after):
            raise ValueError(f"unsafe release removal operand: {payload}")
        roots.add(resolved)
    if mode == "check":
        return

    def owned(server):
        if not isinstance(server, dict):
            return False
        command = server.get("command")
        if not isinstance(command, str) or not os.path.isabs(command):
            return False
        try:
            # stat ORIGINAL spelling first: missing/../file cannot be repaired
            # into an executable, nor can symlink/.. be collapsed lexically.
            before = os.stat(command)
            if not stat.S_ISREG(before.st_mode):
                return False
            resolved = os.path.realpath(command)
            if not os.path.samestat(before, os.stat(resolved)):
                return False
            return any(os.path.commonpath((resolved, root)) == root for root in roots)
        except (OSError, ValueError):
            return False

    removed, kept, local = [], [], []

    def scrub(servers, scope):
        if not isinstance(servers, dict):
            return
        for name in list(servers):
            server = servers[name]
            if owned(server):
                del servers[name]
                removed.append(f"{scope}:{name}")
            else:
                command = server.get("command") if isinstance(server, dict) else None
                basename = os.path.basename(command) if isinstance(command, str) else ""
                if basename == "cua-driver-local":
                    local.append(f"{scope}:{name}")
                elif name in ("cua-driver", "cua-driver-rs", "cua-computer-use") or basename == "cua-driver":
                    kept.append(f"{scope}:{name}")

    scrub(data.get("mcpServers"), "user")
    projects = data.get("projects")
    if isinstance(projects, dict):
        for project_name, project in projects.items():
            if isinstance(project, dict):
                scrub(project.get("mcpServers"), f"project:{project_name}")

    if removed:
        directory = os.path.dirname(path) or "."
        fd, backup = tempfile.mkstemp(
            prefix=os.path.basename(path) + ".bak-cua-driver-rs-uninstall-", dir=directory
        )
        try:
            with os.fdopen(fd, "wb") as handle:
                shutil.copyfileobj(io.BytesIO(original), handle)
        except Exception:
            os.unlink(backup)
            raise
        fd, temporary = tempfile.mkstemp(prefix=".claude.json.", suffix=".tmp", dir=directory)
        try:
            with os.fdopen(fd, "w", encoding="utf-8") as handle:
                json.dump(data, handle, indent=2, ensure_ascii=False)
                handle.write("\n")
            os.replace(temporary, path)
        finally:
            if os.path.exists(temporary):
                os.unlink(temporary)
        print("==> removed Claude MCP registration(s): " + ", ".join(removed))
        print(f"==> backed up Claude config to {backup}")
    if local:
        print("==> preserved local Claude MCP registration(s): " + ", ".join(local)
              + "; remove cua-driver-local with libs/cua-driver/scripts/uninstall-local.sh from the source checkout")
    if kept:
        print("==> preserved unproven Claude MCP registration(s): " + ", ".join(kept)
              + "; inspect the exact entry before removing it manually")


try:
    clean()
except Exception as exc:
    print(f"error: Claude MCP cleanup failed: {exc}", file=sys.stderr)
    raise SystemExit(1)
PYCLAUDE
}

if [[ "${CUA_DRIVER_UNINSTALL_TEST_SOURCE_ONLY:-0}" == "1" ]]; then
    return 0
fi

if ! reject_root_invocation "$(id -u)"; then
    exit 77
fi

# ----------------------------------------------------------------------
# Rust uninstall branch (default on Linux + macOS).
# ----------------------------------------------------------------------
if [[ "$USE_RUST_BACKEND" == "1" ]]; then
    USER_BIN_LINK="$HOME/.local/bin/cua-driver"
    # Canonical bundle path (post-rename — shares bundle id
    # `com.trycua.driver` with the Swift driver). The Rust install
    # replaces Swift here; both uninstallers target this path.
    APP_BUNDLE="/Applications/CuaDriver.app"
    # Legacy bundle path from earlier Rust releases that coexisted with
    # Swift under a separate name. Cleaned up if found.
    LEGACY_APP_BUNDLE="/Applications/CuaDriverRs.app"
    # Canonical package home is ~/.cua-driver (renamed from ~/.cua-driver-rs
    # in v0.2.16 / PR #1644). The old name is swept too — uninstall.sh
    # was missed in that rename and kept defaulting to the stale dir, so a
    # current install left nothing matching and the whole uninstall no-op'd.
    HOME_DIR="${CUA_DRIVER_HOME:-${CUA_DRIVER_RS_HOME:-$HOME/.cua-driver}}"
    LEGACY_HOME_DIR="$HOME/.cua-driver-rs"
    # The versioned package store (`packages/releases/*` + `current`) is
    # written only by the Rust install-local / self-updater path — it's the
    # one unambiguous on-disk Rust discriminator now that the .app bundle +
    # bundle id are shared with Swift.
    PACKAGES_DIR="$HOME_DIR/packages"
    LAUNCHAGENT_PLIST="$HOME/Library/LaunchAgents/com.trycua.cua-driver.plist"
    LEGACY_LAUNCHAGENT_PLIST="$HOME/Library/LaunchAgents/com.trycua.cua-driver-rs.plist"
    SYSTEMD_USER_UNIT="$HOME/.config/systemd/user/cua-driver.service"
    LEGACY_SYSTEMD_USER_UNIT="$HOME/.config/systemd/user/cua-driver-rs.service"
    SKILL_PACK_NAME="cua-driver"
    # Pre-rename skill pack name — swept alongside the current one so
    # users who installed under the legacy name end up clean after
    # `uninstall.sh --backend=rust`.
    LEGACY_SKILL_PACK_NAME="cua-driver-rs"

    # Rust-install marker. The Rust bundle path `/Applications/CuaDriver.app`
    # is shared with the Swift driver (same bundle id `com.trycua.driver`),
    # so we can't use that path alone as a discriminator — a Swift-only Mac
    # that runs `uninstall.sh --backend=rust` by mistake would lose its
    # Swift bundle, symlink, and Claude MCP registrations. This marker says
    # "there's at least one unambiguously-Rust artifact on disk." We gate
    # every shared-path removal below on it.
    #
    # Markers (any one suffices):
    #   - ~/.cua-driver/packages/ exists (Rust install-local / updater store)
    #   - ~/.cua-driver-rs/ exists (legacy Rust state dir, pre-rename)
    #   - /Applications/CuaDriverRs.app exists (legacy bundle, pre-rename)
    #   - current or legacy LaunchAgent plist / systemd unit exists
    #     (autostart was used)
    #   - current telemetry identity/registration marker exists (release
    #     installs on macOS live in /Applications and have no packages dir)
    RUST_INSTALL_PRESENT=0
    if [[ -d "$PACKAGES_DIR" || -d "$LEGACY_HOME_DIR" || -d "$LEGACY_APP_BUNDLE" || -f "$LAUNCHAGENT_PLIST" || -f "$LEGACY_LAUNCHAGENT_PLIST" || -f "$SYSTEMD_USER_UNIT" || -f "$LEGACY_SYSTEMD_USER_UNIT" || -f "$HOME_DIR/.telemetry_id" || -f "$HOME_DIR/.installation_recorded" ]]; then
        RUST_INSTALL_PRESENT=1
    fi

    # Select executable-bearing removals once; proof and deletion consume the
    # same operands. Empty slots mean this invocation will not remove that tree.
    APP_PAYLOAD="" LEGACY_APP_PAYLOAD="" HOME_PAYLOAD="" LEGACY_HOME_PAYLOAD=""
    if [[ "$OS" == "Darwin" ]]; then
        if [[ -d "$LEGACY_APP_BUNDLE" ]]; then LEGACY_APP_PAYLOAD="$LEGACY_APP_BUNDLE"; fi
        if [[ -d "$APP_BUNDLE" && "$RUST_INSTALL_PRESENT" == "1" ]]; then APP_PAYLOAD="$APP_BUNDLE"; fi
    fi
    if [[ -d "$HOME_DIR" && ( "$RUST_INSTALL_PRESENT" == "1" || "$PURGE_DATA" == "1" ) ]]; then
        if [[ "$PURGE_DATA" == "1" ]]; then HOME_PAYLOAD="$HOME_DIR"; else HOME_PAYLOAD="$PACKAGES_DIR"; fi
    fi
    if [[ -d "$LEGACY_HOME_DIR" ]]; then
        if [[ "$PURGE_DATA" == "1" ]]; then LEGACY_HOME_PAYLOAD="$LEGACY_HOME_DIR"; else LEGACY_HOME_PAYLOAD="$LEGACY_HOME_DIR/packages"; fi
    fi
    # Freeze physical parent traversal before any earlier removal can unlink an
    # alias used by a later slot. Preserve a final symlink as an unlink operand;
    # it must never grant ownership of its target. No Python is needed here.
    prepare_payload_operand() {
        local payload="$1" parent
        if [[ -z "$payload" ]]; then printf -v "$2" '%s' ""; return 0; fi
        case "$payload" in
            */|.|..|./*|../*|*/./*|*/../*|*/.|*/..)
                printf 'error: unsafe release removal operand: %s\n' "$payload" >&2
                return 1 ;;
        esac
        if [[ -d "$payload" && ! -L "$payload" ]]; then
            if [[ ! -r "$payload" || ! -x "$payload" ]]; then
                printf 'error: inaccessible release removal operand: %s\n' "$payload" >&2
                return 1
            fi
            # A sentinel protects trailing newlines from command substitution.
            payload="$(CDPATH= cd -- "$payload" && pwd -P && printf '.')" || return 1
            payload="${payload%$'\n.'}"
            if [[ "$payload" == "/" ]]; then
                printf 'error: unsafe release removal operand: /\n' >&2
                return 1
            fi
        elif [[ -L "$payload" ]]; then
            case "$payload" in
                */*) parent="${payload%/*}"; [[ -n "$parent" ]] || parent="/" ;;
                *) parent="." ;;
            esac
            parent="$(CDPATH= cd -- "$parent" && pwd -P && printf '.')" || return 1
            parent="${parent%$'\n.'}"
            payload="$parent/${payload##*/}"
        fi
        # printf -v avoids another lossy command substitution at the call site.
        printf -v "$2" '%s' "$payload"
    }
    prepare_payload_operand "$APP_PAYLOAD" APP_PAYLOAD || exit 1
    prepare_payload_operand "$LEGACY_APP_PAYLOAD" LEGACY_APP_PAYLOAD || exit 1
    prepare_payload_operand "$HOME_PAYLOAD" HOME_PAYLOAD || exit 1
    prepare_payload_operand "$LEGACY_HOME_PAYLOAD" LEGACY_HOME_PAYLOAD || exit 1
    CLAUDE_JSON="$HOME/.claude.json"
    claude_release_config check "$APP_PAYLOAD" "$LEGACY_APP_PAYLOAD" "$HOME_PAYLOAD" "$LEGACY_HOME_PAYLOAD" || exit 1

    DAEMON_PID_FILE="$(daemon_pid_file_path)"
    DAEMON_STOP_HELPER="$(select_daemon_stop_helper || true)"

    if [[ "$RUST_INSTALL_PRESENT" == "1" ]]; then
        if ! release_supervisor stop; then
            log "could not stop the release supervisor; runtime preserved"
            exit 1
        fi
        if ! stop_release_daemon; then
            log "could not safely stop and verify the release daemon; runtime preserved"
            exit 1
        fi
        log "verified no running release cua-driver daemon"
    else
        log "no Rust install marker; leaving any running cua-driver process untouched"
    fi

    # Cryptographic history purge must run while the exact installed helper
    # executable still exists. The helper uses the production KeyProvider and
    # its own bundle-derived namespace, then takes the exclusive writer lease;
    # failure leaves the runtime and all retryable history state in place.
    if [[ "$OS" == "Darwin" && "$PURGE_DATA" == "1" ]]; then
        HISTORY_PURGE_HELPER="$APP_BUNDLE/Contents/MacOS/cua-driver"
        if ! purge_macos_history \
            "$APP_BUNDLE" "$HISTORY_PURGE_HELPER" "$RUST_INSTALL_PRESENT" /usr/bin/codesign; then
            exit 1
        fi
        log "cryptographically purged release Computer History key and local history state"
    elif [[ "$OS" == "Darwin" ]]; then
        log "preserved encrypted Computer History if present; reinstall to reopen it or run uninstall.sh --purge to destroy it"
    elif [[ "$OS" == "Linux" && "$PURGE_DATA" == "1" ]]; then
        HISTORY_PURGE_HELPER="$PACKAGES_DIR/current/cua-driver"
        if ! purge_linux_history "$HISTORY_PURGE_HELPER" "$RUST_INSTALL_PRESENT"; then
            exit 1
        fi
        log "cryptographically purged release Computer History Secret Service key and local history state"
    elif [[ "$OS" == "Linux" ]]; then
        log "preserved encrypted Computer History if present; reinstall to reopen it or run uninstall.sh --purge to destroy it"
    fi

    # Fresh proof while payloads still exist. No name-only CLI fallback may
    # bypass this per-entry decision, including for the legacy server name.
    claude_release_config apply "$APP_PAYLOAD" "$LEGACY_APP_PAYLOAD" "$HOME_PAYLOAD" "$LEGACY_HOME_PAYLOAD" || exit 1

    # --- CLI symlink ---
    # Only remove ~/.local/bin/cua-driver when it resolves into a
    # cua-driver-rs install. Pre-rename installs at
    # /Applications/CuaDriverRs.app are unambiguously Rust and always
    # removed. Post-rename, the Rust install lives at
    # /Applications/CuaDriver.app — the SAME path the Swift driver
    # uses — so we only remove that link when $RUST_INSTALL_PRESENT.
    if [[ -L "$USER_BIN_LINK" ]]; then
        RESOLVED="$(resolve_link "$USER_BIN_LINK")"
        case "$RESOLVED" in
            *"CuaDriverRs.app"*|*"$HOME_DIR"*|*".cua-driver-rs"*)
                # Unambiguous Rust paths.
                rm -f "$USER_BIN_LINK"
                log "removed $USER_BIN_LINK -> $RESOLVED"
                ;;
            *"/Applications/CuaDriver.app"*)
                # Shared with the Swift driver — require a Rust marker.
                if [[ "$RUST_INSTALL_PRESENT" == "1" ]]; then
                    rm -f "$USER_BIN_LINK"
                    log "removed $USER_BIN_LINK -> $RESOLVED"
                else
                    log "$USER_BIN_LINK -> $RESOLVED (shared with Swift driver and no Rust marker on disk; skipping)"
                fi
                ;;
            *)
                log "$USER_BIN_LINK resolves to $RESOLVED (not a cua-driver-rs path; skipping)"
                ;;
        esac
    elif [[ -e "$USER_BIN_LINK" ]]; then
        log "$USER_BIN_LINK exists but is not a symlink (skipping; refusing to clobber a real file)"
    else
        log "no CLI symlink at $USER_BIN_LINK (skipping)"
    fi

    release_supervisor remove

    # --- Revoke TCC grants BEFORE removing the app ---
    # tccutil resolves com.trycua.driver through LaunchServices, so the reset
    # only works while /Applications/CuaDriver.app is still installed. Running
    # it here (not at the closing message) is what makes the revoke actually
    # take — otherwise it fails with -10814 and the grant silently survives.
    maybe_reset_tcc

    # --- Selected app bundles (macOS only) ---
    for PAYLOAD in "$LEGACY_APP_PAYLOAD" "$APP_PAYLOAD"; do
        [[ -n "$PAYLOAD" ]] || continue
        SUDO=""
        if [[ ! -w "$(dirname "$PAYLOAD")" ]]; then SUDO="sudo"; fi
        $SUDO rm -rf -- "$PAYLOAD"
        log "removed $PAYLOAD"
    done

    # --- Package home ---
    # A normal uninstall deliberately keeps the pseudonymous installation ID,
    # persisted telemetry preference, and install/release markers. This lets a
    # later reinstall be counted as a returning installation without sending
    # any events while disabled. `--purge` is the explicit identity reset.
    # All removal remains gated on the Rust marker so a mistaken invocation
    # cannot damage a Swift-only Mac's shared ~/.cua-driver state.
    if [[ -n "$HOME_PAYLOAD" ]]; then
        rm -rf -- "$HOME_PAYLOAD"
        if [[ "$PURGE_DATA" == "1" ]]; then
            log "purged $HOME_DIR (including telemetry identity and preference)"
        else
            rm -rf -- "$HOME_DIR/skills"
            rm -f -- "$HOME_DIR/.tcc-signing-identity" "$HOME_DIR/serve.out.log" "$HOME_DIR/serve.err.log"
            log "removed runtime payloads from $HOME_DIR"
            log "preserved telemetry identity, preference, and registration markers"
        fi
    else
        log "no selected release package home at $HOME_DIR (skipping)"
    fi
    if [[ -n "$LEGACY_HOME_PAYLOAD" ]]; then
        rm -rf -- "$LEGACY_HOME_PAYLOAD"
        if [[ "$PURGE_DATA" == "1" ]]; then
            log "purged legacy package home $LEGACY_HOME_DIR"
        else
            rm -rf -- "$LEGACY_HOME_DIR/skills"
            rm -f -- "$LEGACY_HOME_DIR/.tcc-signing-identity" "$LEGACY_HOME_DIR/serve.out.log" "$LEGACY_HOME_DIR/serve.err.log"
            log "removed legacy runtime payloads and preserved legacy telemetry state"
        fi
    fi

    # --- Swift-era macOS data dirs (leave nothing behind) ---
    # The .app bundle + bundle id are shared with the retired Swift driver,
    # so a default (Rust) uninstall already removes the shared bundle. Sweep
    # the two Swift-only support/cache dirs here too so one `uninstall.sh`
    # leaves nothing behind regardless of which backend originally installed
    # — no second `--backend=swift` pass needed. Gated on the Rust marker
    # for the same reason the shared bundle is: a Swift-only Mac that runs
    # the default uninstall by mistake keeps its data.
    if [[ "$OS" == "Darwin" && "$RUST_INSTALL_PRESENT" == "1" ]]; then
        for SWIFT_DATA_DIR in \
            "$HOME/Library/Application Support/Cua Driver" \
            "$HOME/Library/Caches/cua-driver"; do
            if [[ -d "$SWIFT_DATA_DIR" ]]; then
                rm -rf "$SWIFT_DATA_DIR"
                log "removed $SWIFT_DATA_DIR"
            fi
        done
    fi

    # --- Agent skill symlinks ---
    # Only remove when the link is a symlink — never clobber a real
    # directory (a dev user with a hand-managed skills dir is safe).
    # We don't check the target here because `cua-driver skills install`
    # writes platform-dependent targets (the local copy under $HOME_DIR/
    # skills/cua-driver-rs/). The [[ -L ]] check is the load-bearing
    # safety bar.
    if [[ "$RUST_INSTALL_PRESENT" == "1" ]]; then
        for SKILL_LINK in \
            "$HOME/.claude/skills/$SKILL_PACK_NAME" \
            "$HOME/.agents/skills/$SKILL_PACK_NAME" \
            "$HOME/.openclaw/skills/$SKILL_PACK_NAME" \
            "$HOME/.config/opencode/skills/$SKILL_PACK_NAME" \
            "$HOME/.gemini/skills/$SKILL_PACK_NAME" \
            "$HOME/.hermes/skills/$SKILL_PACK_NAME" \
            "$HOME/.claude/skills/$LEGACY_SKILL_PACK_NAME" \
            "$HOME/.agents/skills/$LEGACY_SKILL_PACK_NAME" \
            "$HOME/.openclaw/skills/$LEGACY_SKILL_PACK_NAME" \
            "$HOME/.config/opencode/skills/$LEGACY_SKILL_PACK_NAME" \
            "$HOME/.gemini/skills/$LEGACY_SKILL_PACK_NAME" \
            "$HOME/.hermes/skills/$LEGACY_SKILL_PACK_NAME"; do
            if [[ -L "$SKILL_LINK" ]]; then
                rm -f "$SKILL_LINK"
                log "removed skill symlink $SKILL_LINK"
            elif [[ -d "$SKILL_LINK" ]]; then
                log "$SKILL_LINK is a real directory, not a symlink (skipping)"
            else
                log "no skill symlink at $SKILL_LINK (skipping)"
            fi
        done
        for SKILL_LINK in \
            "$HOME/.gemini/config/skills/$SKILL_PACK_NAME" \
            "$HOME/.gemini/antigravity/skills/$SKILL_PACK_NAME" \
            "$HOME/.gemini/antigravity-cli/skills/$SKILL_PACK_NAME" \
            "$HOME/.gemini/config/skills/$LEGACY_SKILL_PACK_NAME" \
            "$HOME/.gemini/antigravity/skills/$LEGACY_SKILL_PACK_NAME" \
            "$HOME/.gemini/antigravity-cli/skills/$LEGACY_SKILL_PACK_NAME"; do
            if [[ -L "$SKILL_LINK" ]]; then
                SKILL_LINK_TARGET="$(readlink "$SKILL_LINK")"
                case "$SKILL_LINK_TARGET" in
                    "$HOME_DIR"/*|"$LEGACY_HOME_DIR"/*)
                        rm -f "$SKILL_LINK"
                        log "removed skill symlink $SKILL_LINK" ;;
                    *)
                        log "$SKILL_LINK points outside the driver home (skipping)" ;;
                esac
            else
                log "no skill symlink at $SKILL_LINK (skipping)"
            fi
        done
    else
        log "no Rust install marker; leaving agent skill symlinks untouched"
    fi

    # --- Closing message ---
    # TCC grants were already revoked above, before the app was removed, so
    # the reset could still resolve the bundle id through LaunchServices.
    if [[ "$OS" == "Darwin" ]]; then
        echo ""
        echo "cua-driver uninstalled."
        if [[ "$PURGE_DATA" == "0" ]]; then
            cat << 'TELEMETRYUNMSG'

Telemetry identity and preference were preserved for a future reinstall.
To delete them too, re-run with --purge:

  /bin/bash -c "$(curl -fsSL https://cua.ai/driver/uninstall.sh)" -- --purge
TELEMETRYUNMSG
        fi
        if [[ "$RESET_TCC" != "1" ]]; then
            cat << 'FINALUNMSG'

macOS permissions (Accessibility + Screen Recording) were kept, so
reinstalling Cua Driver needs no new approval. To revoke them as well,
re-run with --reset-permissions, or run:

  tccutil reset Accessibility com.trycua.driver
  tccutil reset ScreenCapture com.trycua.driver
FINALUNMSG
        fi
    else
        cat << 'FINALUNMSG'

cua-driver uninstalled.
FINALUNMSG
        if [[ "$PURGE_DATA" == "0" ]]; then
            cat << 'TELEMETRYUNMSG'

Telemetry identity and preference were preserved for a future reinstall.
To delete them too, re-run with --purge:

  /bin/bash -c "$(curl -fsSL https://cua.ai/driver/uninstall.sh)" -- --purge
TELEMETRYUNMSG
        fi
    fi
    report_surviving_local_install
    exit 0
fi

USER_BIN_LINK="$HOME/.local/bin/cua-driver"
SYSTEM_BIN_LINK="/usr/local/bin/cua-driver"
APP_BUNDLE="/Applications/CuaDriver.app"
USER_DATA="$HOME/.cua-driver"
CONFIG_DIR="$HOME/Library/Application Support/Cua Driver"
CACHE_DIR="$HOME/Library/Caches/cua-driver"
# Legacy — remove if present from older installs.
LEGACY_UPDATE_SCRIPT="/usr/local/bin/cua-driver-update"
LEGACY_UPDATER_PLIST="$HOME/Library/LaunchAgents/com.trycua.cua_driver_updater.plist"

# CLI symlinks. Try the user-bin first (no sudo), then the legacy
# /usr/local/bin path (needs sudo on default macOS).
for BIN_LINK in "$USER_BIN_LINK" "$SYSTEM_BIN_LINK"; do
    if [[ -L "$BIN_LINK" ]] || [[ -e "$BIN_LINK" ]]; then
        SUDO=""
        [[ ! -w "$(dirname "$BIN_LINK")" ]] && SUDO="sudo"
        $SUDO rm -f "$BIN_LINK"
        log "removed $BIN_LINK"
    fi
done

# Legacy update script + LaunchAgent (present in installs before 0.0.6).
if [[ -f "$LEGACY_UPDATE_SCRIPT" ]]; then
    SUDO=""; [[ ! -w "$(dirname "$LEGACY_UPDATE_SCRIPT")" ]] && SUDO="sudo"
    $SUDO rm -f "$LEGACY_UPDATE_SCRIPT"
    log "removed legacy $LEGACY_UPDATE_SCRIPT"
fi
if [[ -f "$LEGACY_UPDATER_PLIST" ]]; then
    launchctl unload "$LEGACY_UPDATER_PLIST" 2>/dev/null || true
    rm -f "$LEGACY_UPDATER_PLIST"
    log "removed legacy $LEGACY_UPDATER_PLIST"
fi

# .app bundle (in /Applications, usually writable by the user).
if [[ -d "$APP_BUNDLE" ]]; then
    SUDO=""
    if [[ ! -w "$(dirname "$APP_BUNDLE")" ]]; then
        SUDO="sudo"
    fi
    $SUDO rm -rf "$APP_BUNDLE"
    log "removed $APP_BUNDLE"
else
    log "no app bundle at $APP_BUNDLE (skipping)"
fi

# User-data directory (telemetry id + install marker).
if [[ -d "$USER_DATA" ]]; then
    rm -rf "$USER_DATA"
    log "removed $USER_DATA"
else
    log "no user data at $USER_DATA (skipping)"
fi

# Persisted config.
if [[ -d "$CONFIG_DIR" ]]; then
    rm -rf "$CONFIG_DIR"
    log "removed $CONFIG_DIR"
else
    log "no config at $CONFIG_DIR (skipping)"
fi

# Cache / daemon state.
if [[ -d "$CACHE_DIR" ]]; then
    rm -rf "$CACHE_DIR"
    log "removed $CACHE_DIR"
else
    log "no cache at $CACHE_DIR (skipping)"
fi

# Agent skill symlinks (Claude Code + Codex). Only remove when the link
# is ours — a dev user pointing the symlink at a working copy of the
# repo keeps theirs untouched.
SKILL_TARGET_EXPECTED="$APP_BUNDLE/Contents/Resources/Skills/cua-driver"
for SKILL_LINK in \
    "$HOME/.claude/skills/cua-driver" \
    "$HOME/.agents/skills/cua-driver" \
    "$HOME/.openclaw/skills/cua-driver" \
    "$HOME/.config/opencode/skills/cua-driver" \
    "$HOME/.gemini/config/skills/cua-driver" \
    "$HOME/.gemini/antigravity/skills/cua-driver" \
    "$HOME/.gemini/antigravity-cli/skills/cua-driver" \
    "$HOME/.gemini/skills/cua-driver" \
    "$HOME/.hermes/skills/cua-driver"; do
    if [[ -L "$SKILL_LINK" ]] && [[ "$(readlink "$SKILL_LINK")" == "$SKILL_TARGET_EXPECTED" ]]; then
        rm -f "$SKILL_LINK"
        log "removed $SKILL_LINK"
    else
        log "no install-created skill symlink at $SKILL_LINK (skipping)"
    fi
done

# Claude Code MCP registrations. `claude mcp remove` only removes from
# the current project / user scopes, while ~/.claude.json can also
# contain stale project entries for other directories. Scrub only
# registrations explicitly named cua-driver or whose command points at
# a cua-driver binary, so unrelated servers named "computer-use" are
# left alone.
CLAUDE_JSON="$HOME/.claude.json"
if [[ -f "$CLAUDE_JSON" ]] && command -v python3 >/dev/null 2>&1; then
    PY_OUTPUT="$(
        CLAUDE_JSON="$CLAUDE_JSON" python3 <<'PY'
import json
import os
import shutil
import sys
import tempfile
import time

path = os.environ["CLAUDE_JSON"]

try:
    with open(path, "r", encoding="utf-8") as f:
        data = json.load(f)
except Exception as exc:
    print(f"could not read Claude config {path}: {exc}", file=sys.stderr)
    raise SystemExit(0)

removed = []

def text_parts(value):
    if isinstance(value, str):
        return [value]
    if isinstance(value, list):
        return [item for item in value if isinstance(item, str)]
    return []

def invokes_cua_driver(server):
    if not isinstance(server, dict):
        return False
    parts = []
    parts.extend(text_parts(server.get("command")))
    parts.extend(text_parts(server.get("args")))
    joined = " ".join(parts)
    return "cua-driver" in joined or "CuaDriver.app" in joined

def should_remove(name, server):
    return name in {"cua-driver", "cua-computer-use"} or invokes_cua_driver(server)

def scrub_servers(servers, scope):
    if not isinstance(servers, dict):
        return
    for name in list(servers.keys()):
        if should_remove(name, servers[name]):
            del servers[name]
            removed.append(f"{scope}:{name}")

scrub_servers(data.get("mcpServers"), "user")

projects = data.get("projects")
if isinstance(projects, dict):
    for project in projects.values():
        if isinstance(project, dict):
            scrub_servers(project.get("mcpServers"), "project")

if not removed:
    raise SystemExit(0)

backup = f"{path}.bak-cua-driver-uninstall-{int(time.time())}"
shutil.copy2(path, backup)

directory = os.path.dirname(path) or "."
fd, tmp_path = tempfile.mkstemp(
    prefix=".claude.json.",
    suffix=".tmp",
    dir=directory,
    text=True,
)
try:
    with os.fdopen(fd, "w", encoding="utf-8") as f:
        json.dump(data, f, indent=2, ensure_ascii=False)
        f.write("\n")
    os.replace(tmp_path, path)
except Exception:
    try:
        os.unlink(tmp_path)
    except OSError:
        pass
    raise

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
            log "$line"
        done <<< "$PY_OUTPUT"
    else
        log "no Claude MCP registrations for cua-driver found in $CLAUDE_JSON"
    fi
else
    log "no Claude config cleanup via python3 (missing $CLAUDE_JSON or python3)"
fi

# Best-effort CLI cleanup for the active Claude project. This covers
# .mcp.json / current-working-directory scopes when present and is
# harmless when the entries were already removed above.
if command -v claude >/dev/null 2>&1; then
    for SERVER in cua-driver cua-computer-use; do
        for SCOPE in local project user; do
            if claude mcp remove "$SERVER" -s "$SCOPE" >/dev/null 2>&1; then
                log "removed Claude MCP server $SERVER from $SCOPE scope"
            fi
        done
    done
else
    log "claude CLI not found (skipping Claude MCP CLI cleanup)"
fi

maybe_reset_tcc

echo ""
echo "cua-driver uninstalled."
if [[ "$RESET_TCC" != "1" ]]; then
    cat << 'FINALUNMSG'

macOS permissions (Accessibility + Screen Recording) were kept, so
reinstalling Cua Driver needs no new approval. To revoke them as well,
re-run with --reset-permissions, or run:

  tccutil reset Accessibility com.trycua.driver
  tccutil reset ScreenCapture com.trycua.driver
FINALUNMSG
fi