> ## Documentation Index
> Fetch the complete documentation index at: https://docs.runlayer.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Legacy macOS Detect Cleanup

> Retire the legacy script-based AI Watch Detect rollout: MDM-side deletion, key revocation, and optional user-space tidy-up alongside the AI Watch .pkg.

## When to use this

Run this cleanup if your fleet was previously deployed with the script-based Detect path (an MDM Script + the `runlayer-scan` PPPC profile) and you are migrating to the macOS `.pkg` flow (see the per-MDM guides linked from [Deploy AI Watch](https://docs.runlayer.com/shadow-ai/deploy.md)). The two paths use different binary identifiers, paths, and PPPC pins — leaving the old artifacts in place causes:

* Duplicate scans (per-user `com.runlayer.scan` LaunchAgent + system-wide `com.runlayer.aiwatch` LaunchAgent both fire).
* Dangling TCC grants for `/usr/local/bin/runlayer-scan` that survive even after the script is unscoped.
* Confusing log output split across `/var/log/runlayer/ai_watch_detect.log` (old) and the new macOS unified log under `process == "aiwatch"`.

The legacy script was generated by the dashboard (**Settings → Shadow MCPs → Detect → Configure**) and pasted into your MDM. The dashboard no longer offers it; this page is the only remaining reference. It ran as root on every MDM and installed per signed-in user. Workspace ONE and Intune fleets never had the script flow and need none of this.

AI Watch `0.31.14` or newer retires the on-device agent for you: the `.pkg` preinstall and the uninstall package both boot out every user's `com.runlayer.scan` LaunchAgent and remove its plist and the `runlayer-scan` wrapper. What remains for you is the MDM side, the legacy org API key in Runlayer, and optional user-space tidy-up.

> [!NOTE]
>
> This page cleans up the legacy script-based Detect rollout. To remove the current AI Watch package itself, use [Remove AI Watch](https://docs.runlayer.com/shadow-ai/deploy/remove.md).

## What gets left behind

The script-based deployment installs and configures the following on each enrolled device. **Removed by** says who cleans it up.

| Artifact | Path / Identifier | Removed by |
| - | - | - |
| Wrapper binary | `/usr/local/bin/runlayer-scan` (ad-hoc signed, identifier `com.runlayer.scan`) | `.pkg` 0.31.14+ / uninstall package / tidy script |
| Per-user LaunchAgent | `~/Library/LaunchAgents/com.runlayer.scan.plist`, loaded as `gui/<uid>/com.runlayer.scan` | `.pkg` 0.31.14+ / uninstall package / tidy script |
| Runlayer CLI (uv tool) | `~/.local/share/uv/tools/runlayer` plus the shim `~/.local/bin/runlayer` | tidy script |
| uv download cache | `~/.cache/uv` | left in place |
| MDM org API key on the device | `~/.runlayer/config.yaml` — only the `org_api_keys.ai_watch_mdm` entry for your host | tidy script |
| Legacy org API key in Runlayer | **Settings → Organization API keys** (dialog-generated, or a hand-made key with the Security Scan role) | you, step 3 below |
| Logs | `/var/log/runlayer/ai_watch_detect.log` (fallback `/tmp/runlayer-ai_watch_detect.log`), plus `/tmp/runlayer-scan.{stdout,stderr}.log` | tidy script |
| MDM-side script | One Script per MDM (Jamf, SimpleMDM, Mosyle, Iru/Kandji, …), usually named "AI Watch Detect" | you, step 1 below |
| MDM-side PPPC profile | Display name `Runlayer AI Watch - Full Disk Access`, identifier `com.runlayer.scan.pppc` (pins Full Disk Access to `/usr/local/bin/runlayer-scan`) | you, step 1 below |

`/var/log/runlayer/` is shared with the Enforce hook and sync MDM scripts; only `ai_watch_detect.log` belongs to the legacy Detect rollout.

## Order of operations

1. **Delete the legacy MDM artifacts** (Script + old PPPC profile) so devices stop receiving the script-based deployment. On Iru/Kandji delete the Custom Script library item first: its Audit Script reinstalls the agent on every run until it is gone.
2. **Deploy the `.pkg`** (`0.31.14` or newer) via the guide for your MDM platform — see [Deploy AI Watch](https://docs.runlayer.com/shadow-ai/deploy.md). The installer retires the legacy agent and wrapper on every device it reaches. Wait one MDM check-in cycle after step 1 so no device gets the legacy script re-pushed after the package lands.
3. **Revoke the legacy org API key** in **Settings → Organization API keys**. Every legacy device stored this key; revoking it is what ends access, whether or not a given device is ever cleaned.
4. **Optional: tidy user space** with the [script below](#per-device-tidy-script). It removes the uv-installed CLI, its shim, the stored key entry, and the old logs, and also covers devices still on a `.pkg` older than `0.31.14`.
5. **Verify** with the [MDM verification script](#verification).

## MDM-side cleanup

For each MDM, remove or unscope the two script-based artifacts. Names below match the artifacts suggested in the original setup guides; yours may differ. The PPPC profile shows up in every MDM under its display name `Runlayer AI Watch - Full Disk Access` (identifier `com.runlayer.scan.pppc`).

> [tab] Jamf Pro
>
> 1. **Computers → Policies** → open the "Deploy AI Watch Detect" Policy (earliest adopters: "Deploy Runlayer Detect") → set **Scope** to none, then delete it. Its trigger was **Recurring Check-in** or **Login** with frequency **Ongoing**, so an unscoped-but-present policy keeps logging failures once the script is gone.
> 2. **Settings → Computer Management → Scripts** → delete the "AI Watch Detect" Script ("Runlayer Detect" for earliest adopters).
> 3. **Computers → Configuration Profiles** → delete "Runlayer AI Watch - Full Disk Access" (`com.runlayer.scan.pppc`).
>
> To run the tidy script, create a Policy with the script as a payload, trigger **Login** + **Recurring Check-in**, frequency **Once per computer**. To find stragglers, paste the [audit variant](#audit-variant-exit-1-on-leftovers) of the verification script into an **Extension Attribute** and build a Smart Group on a non-zero result. Leave `EXPECT_TIDY=0` until the tidy Policy has run, or the group targets every legacy device.

> [tab] Iru/Kandji
>
> 1. **Library** → open the "AI Watch Detect" Custom Script → **Delete** (or remove it from every Blueprint). The legacy script lived in this item's **Audit Script** field and re-ran daily or every 15 minutes; until the item is gone it reinstalls what any cleanup removes.
> 2. **Library** → open "Runlayer AI Watch - Full Disk Access" (`com.runlayer.scan.pppc`) → **Delete**.
>
> Iru runs scripts as root whether or not anyone is signed in. To tidy user space with Iru doing the bookkeeping, add a new Custom Script, **Execution Frequency: Run once per device**, with the [audit variant](#audit-variant-exit-1-on-leftovers) (with `EXPECT_TIDY=1`) as the **Audit Script** and the [tidy script](#per-device-tidy-script) as the **Remediation Script**. Iru remediates only the devices whose audit fails and reports compliance per device.

> [tab] SimpleMDM
>
> 1. **Scripts** → delete the "AI Watch Detect" Script (or unassign it from all groups).
> 2. **Profiles** → delete "Runlayer AI Watch - Full Disk Access" (`com.runlayer.scan.pppc`).
>
> To run the tidy script: **Scripts → Add Script**, assign the group, run once. SimpleMDM has no audit model; read the per-device output in the script job log.

> [tab] Mosyle
>
> 1. **Management → Custom Commands** → delete the "AI Watch Detect" command.
> 2. **Management → Certificates / Custom Profiles** → delete "AI Watch Detect - Full Disk Access" (`com.runlayer.scan.pppc`).
>
> To run the tidy script: new Custom Command, paste the script, **Execution Settings → run once**. The legacy command used the `%Email%` and `%DeviceName%` variables; the tidy script needs none.

> [tab] Other MDM
>
> Remove the script and the PPPC profile using your MDM's standard "delete script" and "delete custom configuration profile" workflows. Profile display name: `Runlayer AI Watch - Full Disk Access`; identifier `com.runlayer.scan.pppc`. Script log path: `/var/log/runlayer/ai_watch_detect.log`. The legacy script ran as root, and so must the tidy and verification scripts below.

## Per-device tidy script

Optional. Push it as a one-time root **Custom Script** to devices that previously ran the script-based deployment. It removes what the `.pkg` leaves alone — the uv-installed Runlayer CLI, its shim, the stored `ai_watch_mdm` key entry, and the old logs — and also retires the agent and wrapper on devices still running a `.pkg` older than `0.31.14`. It walks every home under `/Users`, so it works at the login window and on shared Macs. It is idempotent, a no-op on devices that never had the old deployment, and always exits `0`.

The key entry is removed under every host stored in `~/.runlayer/config.yaml`. The name `ai_watch_mdm` was only ever written by the legacy script, so there is no tenant URL to fill in.

```bash theme={null}
#!/bin/bash
# Runlayer: tidy up after the legacy script-based AI Watch Detect rollout.
# Runs as root from your MDM. Idempotent; always exits 0.
set -u

LABEL="com.runlayer.scan"
KEY_NAME="ai_watch_mdm"

# Hosts stored in ~/.runlayer/config.yaml: the `url:` of every entry under
# `hosts:`. The CLI matches --host against that stored url, so pass it as is.
stored_hosts() {
    /usr/bin/awk '
        /^hosts:/ { inhosts = 1; next }
        inhosts && /^[^ ]/ { inhosts = 0 }
        inhosts && /^    url:[ \t]*/ {
            sub(/^    url:[ \t]*/, ""); gsub(/^["'"'"']|["'"'"']$/, ""); print
        }' "$1" 2>/dev/null
}

# Run a command as the home's owner with the environment the legacy
# installer used, so uv and the CLI see the same tool dir and config.
# Plain sudo, not `launchctl asuser`: the latter needs the user's GUI
# bootstrap, which does not exist at the login window, where MDMs often
# run one-time scripts.
as_owner() {
    local user="$1" home="$2"
    shift 2
    /usr/bin/sudo -u "$user" /usr/bin/env HOME="$home" UV_CACHE_DIR="$home/.cache/uv" "$@"
}

for home in /Users/*; do
    [ -d "$home" ] || continue
    case "$home" in /Users/Shared) continue ;; esac
    uid=$(/usr/bin/stat -f %u "$home" 2>/dev/null || echo "")
    user=$(/usr/bin/stat -f %Su "$home" 2>/dev/null || echo "")
    case "$uid" in ""|0|*[!0-9]*) continue ;; esac

    plist="$home/Library/LaunchAgents/$LABEL.plist"
    if [ -e "$plist" ]; then
        /bin/launchctl bootout "gui/${uid}/${LABEL}" 2>/dev/null || true
        rm -f "$plist"
    fi

    # Remove only the MDM key entry from ~/.runlayer/config.yaml, under every
    # stored host; personal `runlayer login` credentials and the hosts
    # themselves are preserved. Needs the CLI, so it runs before the uv tool
    # is removed. Falls back to uvx when the shim is already gone (that path
    # downloads the CLI; network needed).
    shim="$home/.local/bin/runlayer"
    config="$home/.runlayer/config.yaml"
    cli=""
    if [ -x "$shim" ]; then
        cli="$shim"
    elif [ -x /usr/local/bin/uvx ]; then
        cli="/usr/local/bin/uvx runlayer"
    fi
    if [ -n "$cli" ] && /usr/bin/grep -qs "$KEY_NAME" "$config"; then
        while IFS= read -r url; do
            [ -n "$url" ] || continue
            # shellcheck disable=SC2086  # $cli is intentionally "uvx runlayer" in the fallback
            as_owner "$user" "$home" $cli org-api-key remove "$KEY_NAME" --host "$url" >/dev/null 2>&1 || true
        done <<< "$(stored_hosts "$config")"
    fi

    # uv-installed CLI + shim. `uv tool uninstall` keeps uv's own records
    # consistent; the rm lines cover a half-removed install.
    if [ -x /usr/local/bin/uv ]; then
        as_owner "$user" "$home" /usr/local/bin/uv tool uninstall runlayer >/dev/null 2>&1 || true
    fi
    rm -rf "$home/.local/share/uv/tools/runlayer"
    if [ -L "$shim" ] && [ ! -e "$shim" ]; then
        rm -f "$shim"   # dangling shim left by a removed tool dir
    fi
done

rm -f /usr/local/bin/runlayer-scan
rm -f /var/log/runlayer/ai_watch_detect.log
rmdir /var/log/runlayer 2>/dev/null || true   # only if nothing else logs there
rm -f /tmp/runlayer-ai_watch_detect.log /tmp/runlayer-scan.stdout.log /tmp/runlayer-scan.stderr.log

echo "Legacy AI Watch Detect tidy-up complete."
exit 0
```

> [!NOTE]
>
> The script intentionally leaves the following in place:
>
> * **`uv` itself** (`/usr/local/bin/uv`, `/usr/local/bin/uvx`) and its download cache **`~/.cache/uv`** — general-purpose tooling that may be in use for other workflows. Only the `runlayer` uv-tool install is removed.
> * **`~/.runlayer/config.yaml`** — only the `org_api_keys.ai_watch_mdm` entry for your host is removed. Other host entries and any personal `runlayer login` credentials are kept. The file is **not** deleted, even if it ends up empty.
> * **Other files in `/var/log/runlayer/`** — the Enforce hook and sync MDM scripts log there too. Only `ai_watch_detect.log` is removed; the directory goes only if it is empty afterwards.
> * **OS Keychain entries** — the script-based deployment never wrote to the keychain. Any `runlayer` keychain entries belong to an interactive `runlayer login` session and are left untouched.

## Verification

### Audit variant (exit 1 on leftovers)

Push this as a root **Custom Script** after the `.pkg` has rolled out. It checks every home under `/Users`, prints one line per check, and exits `0` when the agent, its plist, and the wrapper are gone and `1` when any is left, so your MDM's script status matches reality. Use it as the **Audit Script** on Iru/Kandji, as an **Extension Attribute** on Jamf Pro, and as a plain one-time script elsewhere.

The checks come in two groups. **Agent checks** (plist, loaded agent, wrapper) are what the `.pkg` guarantees and always count. **User-space checks** (uv tool, stored key entry) are only cleared by the tidy script, so by default they print as `NOTE` and do not fail the audit. Once you have deployed the tidy script, set `EXPECT_TIDY=1` at the top so they count too; that is what makes an Iru Audit + Remediation pairing retry the tidy script.

```bash theme={null}
#!/bin/bash
set -u

EXPECT_TIDY=0   # set to 1 once the tidy script has been deployed

LEFTOVERS=0
check_gone() {
    if [ -e "$2" ] || [ -L "$2" ]; then
        echo "FAIL: $1 still present: $2"
        LEFTOVERS=$((LEFTOVERS + 1))
    else
        echo "OK: $1 removed"
    fi
}
# User-space leftovers count only when the tidy script is expected to have run.
user_space_leftover() {
    if [ "$EXPECT_TIDY" = "1" ]; then
        echo "FAIL: $1 (run the tidy script)"
        LEFTOVERS=$((LEFTOVERS + 1))
    else
        echo "NOTE: $1 (tidy script not run yet)"
    fi
}

check_gone "old wrapper" /usr/local/bin/runlayer-scan

for home in /Users/*; do
    [ -d "$home" ] || continue
    case "$home" in /Users/Shared) continue ;; esac
    uid=$(/usr/bin/stat -f %u "$home" 2>/dev/null || echo "")
    case "$uid" in ""|0|*[!0-9]*) continue ;; esac
    name=$(basename "$home")

    check_gone "old LaunchAgent plist ($name)" "$home/Library/LaunchAgents/com.runlayer.scan.plist"
    if [ -e "$home/.local/share/uv/tools/runlayer" ]; then
        user_space_leftover "uv-installed Runlayer CLI still present for $name"
    else
        echo "OK: old Runlayer CLI removed ($name)"
    fi
    if /usr/bin/grep -qs "ai_watch_mdm" "$home/.runlayer/config.yaml"; then
        user_space_leftover "ai_watch_mdm key entry still in ~/.runlayer/config.yaml for $name"
    else
        echo "OK: ai_watch_mdm key entry removed ($name)"
    fi
    # A GUI domain exists only for signed-in users; absent is as good as unloaded.
    if /bin/launchctl print "gui/${uid}/com.runlayer.scan" >/dev/null 2>&1; then
        echo "FAIL: old LaunchAgent com.runlayer.scan is still loaded for $name"
        LEFTOVERS=$((LEFTOVERS + 1))
    else
        echo "OK: old LaunchAgent not loaded ($name)"
    fi
done

if [ "$LEFTOVERS" -gt 0 ]; then
    echo "Legacy cleanup incomplete: $LEFTOVERS item(s) left."
    exit 1
fi
echo "Legacy cleanup verified."
exit 0
```

On Jamf Pro, wrap the script so the Extension Attribute reports `<result>` with the exit code, and build a Smart Group on a non-zero value to re-target stragglers with the tidy script.

### Spot-check a test device

After the `.pkg` is deployed, run these in **Terminal as the signed-in user** (not from MDM: `id -u` would be root's) to confirm there is exactly one Runlayer scan agent. Every line exits `0`; read the printed message, not the exit status.

```bash theme={null}
# 1. Old wrapper gone
test ! -e /usr/local/bin/runlayer-scan && echo "old wrapper removed"

# 2. Old per-user LaunchAgent gone (launchctl exits 113 when the service does not exist)
launchctl print "gui/$(id -u)/com.runlayer.scan" >/dev/null 2>&1; [ $? -eq 113 ] && echo "old LaunchAgent removed"

# 3. New system LaunchAgent loaded
launchctl print "gui/$(id -u)/com.runlayer.aiwatch" >/dev/null 2>&1 && echo "new LaunchAgent loaded"

# 4. Only the new PPPC pin is active (com.runlayer.aiwatch, signed by Anysource Inc.)
sudo log show --predicate 'subsystem == "com.apple.TCC"' --last 1h | grep -E "com\.runlayer\.(scan|aiwatch)"
```

A clean fleet shows TCC entries only for `com.runlayer.aiwatch`. Empty output is also clean: it means no TCC decision was logged in the last hour. Any remaining `com.runlayer.scan` entry means the device still has the old PPPC profile or wrapper; re-run the audit script against it.

## Rollback

If you need to revert to the script-based deployment temporarily, re-scope the original Script and PPPC profile in your MDM; the legacy script reinstalls the agent and wrapper on its next run. The `.pkg` and the old wrapper can coexist for short periods (they use distinct identifiers and paths), but scheduled scans then run twice per cycle, and the next `.pkg` install or self-update retires the legacy agent again — use only as a short-term safety net during migration.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.