Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

151 KB
Raw Blame
#!/bin/bash
# ============================================================================
# ODS macOS Installer -- Main Orchestrator
# ============================================================================
# Standalone macOS Apple Silicon installer. Does not modify any existing files.
#
# macOS: llama-server runs natively with Metal on the host. Everything else
#        runs in Docker. Colima uses a private vmnet bridge because its
#        host.docker.internal forwarding can become unreachable under load.
#
# Usage:
#   ./install-macos.sh                  # Interactive install
#   ./install-macos.sh --tier 3         # Force tier 3
#   ./install-macos.sh --dry-run        # Validate without installing
#   ./install-macos.sh --all            # Enable all optional services
#   ./install-macos.sh --non-interactive # Headless install (defaults)
#   ./install-macos.sh --no-bootstrap   # Wait for the full model before launch
#   ./install-macos.sh --preflight-only # Phase 1 environment checks only, no
#                                       # changes (get-ods.sh --force runs this
#                                       # before removing an existing install)
#
# ============================================================================

# Detect dry-run before the Bash bootstrap. macOS ships Bash 3.2, so argument
# parsing below cannot run until we hand off to Homebrew Bash. A dry-run must
# still remain mutation-free when that newer shell is not installed yet.
_ods_bootstrap_dry_run=false
for _ods_bootstrap_arg in "$@"; do
  if [ "$_ods_bootstrap_arg" = "--dry-run" ]; then
    _ods_bootstrap_dry_run=true
    break
  fi
done

_ods_bash_is_modern() {
  [ -x "$1" ] || return 1
  "$1" -c '[ "${BASH_VERSINFO[0]:-0}" -ge 4 ]' >/dev/null 2>&1
}

# Guard: macOS ships Bash 3.2 (GPL). ods-cli and our libs need Bash 4+.
if [ "${BASH_VERSINFO[0]:-0}" -lt 4 ]; then
  # Default candidate paths cover standard Apple Silicon and Intel Homebrew
  # prefixes. If brew is already on PATH we also ask it for its actual prefix,
  # which handles custom installs (e.g. /Volumes/X/homebrew).
  candidates=(/opt/homebrew/bin/bash /usr/local/bin/bash)
  if command -v brew >/dev/null 2>&1; then
    brew_prefix="$(brew --prefix 2>/dev/null)"
    [ -n "$brew_prefix" ] && candidates=("$brew_prefix/bin/bash" "${candidates[@]}")
  fi
  for candidate in "${candidates[@]}"; do
    if _ods_bash_is_modern "$candidate"; then
      exec "$candidate" "$0" "$@"
    fi
  done
  if [ "$_ods_bootstrap_dry_run" = true ]; then
    echo "[DRY RUN] Bash 4+ is not installed; a real install would run 'brew install bash'."
    echo "[DRY RUN] No host changes were made."
    exit 0
  fi
  if ! command -v brew >/dev/null 2>&1; then
    echo "ODS requires Bash 4+ (you have ${BASH_VERSION})." >&2
    echo "macOS ships only Bash 3.2 — the last GPLv2 release Apple was willing to bundle." >&2
    echo >&2
    echo "Two-step bootstrap (one-time):" >&2
    echo >&2
    echo "  1. Install Homebrew. Requires an admin password and ~3 min:" >&2
    echo "       /bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\"" >&2
    echo >&2
    echo "  2. Re-run this installer. It will detect Homebrew, run \`brew install bash\` for you, and proceed." >&2
    echo >&2
    echo "If \`brew install\` later complains \"Need sudo access on macOS\" under --non-interactive," >&2
    echo "Homebrew is asking for passwordless sudo. Workaround:" >&2
    echo "  echo \"\$USER ALL=(ALL) NOPASSWD: ALL\" | sudo tee /etc/sudoers.d/99-ods-install >/dev/null" >&2
    echo "  sudo chmod 440 /etc/sudoers.d/99-ods-install" >&2
    echo "(remove the file after install if you'd rather not keep passwordless sudo.)" >&2
    exit 1
  fi
  echo "Installing Bash 4+ via Homebrew (one-time setup)..."
  brew install bash || { echo "brew install bash failed" >&2; exit 1; }
  brew_prefix="$(brew --prefix 2>/dev/null)"
  if [ -n "$brew_prefix" ] && _ods_bash_is_modern "$brew_prefix/bin/bash"; then
    exec "$brew_prefix/bin/bash" "$0" "$@"
  fi
  for candidate in /opt/homebrew/bin/bash /usr/local/bin/bash; do
    if _ods_bash_is_modern "$candidate"; then
      exec "$candidate" "$0" "$@"
    fi
  done
  echo "Homebrew bash installed but not found in expected paths." >&2
  exit 1
fi

set -euo pipefail

# ── Parse arguments ──
DRY_RUN=false
FORCE=false
NON_INTERACTIVE=false
TIER_OVERRIDE=""
ENABLE_VOICE=false
ENABLE_WORKFLOWS=false
ENABLE_RAG=false
ENABLE_RECOMMENDED=true
RECOMMENDED_EXPLICIT=false
# Hermes Agent is the default agent when Pixel is not selected.
ENABLE_HERMES=true
HERMES_EXPLICIT=false
ENABLE_OPENCODE=false
OPENCODE_ENABLE_EXPLICIT=false
OPENCODE_DISABLE_EXPLICIT=false
OPENCODE_DISABLE_SELECTED=false
ENABLE_PIXEL=true
ENABLE_BRAVE_SEARCH=false
ENABLE_APE=true
ENABLE_PERPLEXICA=false
ENABLE_PRIVACY_SHIELD=false
ENABLE_ODS_PROXY=false
ENABLE_TAILSCALE=false
ENABLE_SEARXNG=false
ENABLE_WEB_SEARCH=false
ENABLE_LITELLM=false
ENABLE_OPEN_WEBUI=false
WEBUI_ENABLE_EXPLICIT=false
WEBUI_DISABLE_EXPLICIT=false
WEBUI_RETAINED=""
# Langfuse defaults OFF because its clickhouse + postgres + minio stack adds
# ~500MB baseline memory. Enable via --langfuse, --all, or post-install
# `ods enable langfuse`. --no-langfuse honored as explicit override so a
# --all run can still suppress Langfuse.
ENABLE_LANGFUSE=false
NO_LANGFUSE_EXPLICIT=false
ALL_FEATURES=false
CLOUD_MODE=false
NO_BOOTSTRAP=false
PREFLIGHT_ONLY=false
HERMES_CONTEXT_SIZE=65536

while [[ $# -gt 0 ]]; do
    case "$1" in
        --dry-run)       DRY_RUN=true; shift ;;
        --preflight-only) PREFLIGHT_ONLY=true; shift ;;
        --force)         FORCE=true; shift ;;
        --non-interactive) NON_INTERACTIVE=true; shift ;;
        --tier)          TIER_OVERRIDE="${2:-}"; shift 2 ;;
        --voice)         ENABLE_VOICE=true; shift ;;
        --workflows)     ENABLE_WORKFLOWS=true; shift ;;
        --rag)           ENABLE_RAG=true; shift ;;
        --recommended)   ENABLE_RECOMMENDED=true; RECOMMENDED_EXPLICIT=true; shift ;;
        --no-recommended) ENABLE_RECOMMENDED=false; RECOMMENDED_EXPLICIT=true; shift ;;
        --hermes)        ENABLE_HERMES=true; HERMES_EXPLICIT=true; shift ;;
        --no-hermes)     ENABLE_HERMES=false; HERMES_EXPLICIT=true; shift ;;
        # Kept parseable so existing scripts keep working after the removal.
        --openclaw|--no-openclaw)
            printf '[WARN] The legacy OpenClaw extension was removed; %s is ignored. Portal (Pixel) and Hermes are the supported agents.\n' "$1" >&2
            shift ;;
        --opencode)     ENABLE_OPENCODE=true; OPENCODE_ENABLE_EXPLICIT=true; shift ;;
        --no-opencode)  ENABLE_OPENCODE=false; OPENCODE_DISABLE_EXPLICIT=true; shift ;;
        --with-webui)   ENABLE_OPEN_WEBUI=true; WEBUI_ENABLE_EXPLICIT=true; shift ;;
        --no-webui)     ENABLE_OPEN_WEBUI=false; WEBUI_DISABLE_EXPLICIT=true; shift ;;
        --pixel)        ENABLE_PIXEL=true; shift ;;
        --no-pixel)     ENABLE_PIXEL=false; shift ;;
        --langfuse)      ENABLE_LANGFUSE=true; shift ;;
        --no-langfuse)   ENABLE_LANGFUSE=false; NO_LANGFUSE_EXPLICIT=true; shift ;;
        --all)           ALL_FEATURES=true; shift ;;
        --cloud)         CLOUD_MODE=true; shift ;;
        --no-bootstrap)  NO_BOOTSTRAP=true; shift ;;
        # The vLLM engine needs Linux and an NVIDIA datacenter GPU. macOS
        # always runs llama.cpp, so llama-server is accepted and changes
        # nothing (get-ods.sh passes the same options to every installer).
        --llm-backend)
            case "${2:-}" in
                llama-server) shift 2 ;;
                vllm)
                    echo "--llm-backend vllm needs Linux with an NVIDIA GPU; macOS runs llama.cpp. Nothing was changed." >&2
                    echo "Rerun without --llm-backend, or with --llm-backend llama-server." >&2
                    echo "Need help? Ask on the ODS Discord: https://discord.gg/4ntNp9MAwC" >&2
                    exit 1 ;;
                *) echo "--llm-backend requires vllm or llama-server" >&2; exit 1 ;;
            esac ;;
        # API mode is not in this installer yet; say so and name the way
        # to connect an API after installing, not "Unknown option".
        --external-llm-url|--external-llm-provider|--external-llm-model|--external-llm-key-file|--external-llm-key-env|--no-external-llm|--reuse-external-llm)
            echo "API mode ($1) is not in the macOS installer yet. Nothing was changed." >&2
            echo "Install without it, then connect your API in the dashboard (Settings > Remote model), or run:" >&2
            echo "  ods remote-provider configure --base-url URL --model MODEL --api-key-file FILE" >&2
            echo "  ods remote-provider test" >&2
            echo "Need help? Ask on the ODS Discord: https://discord.gg/4ntNp9MAwC" >&2
            exit 1 ;;
        *)               echo "Unknown option: $1"; exit 1 ;;
    esac
done

if $OPENCODE_ENABLE_EXPLICIT && $OPENCODE_DISABLE_EXPLICIT; then
    echo "--opencode and --no-opencode cannot be used together" >&2
    exit 1
fi
if $WEBUI_ENABLE_EXPLICIT && $WEBUI_DISABLE_EXPLICIT; then
    echo "--with-webui and --no-webui cannot be used together" >&2
    exit 1
fi

if $ALL_FEATURES; then
    ENABLE_VOICE=true
    ENABLE_WORKFLOWS=true
    ENABLE_RAG=true
    ENABLE_RECOMMENDED=true
    # --all enables the default Hermes Agent.
    ENABLE_HERMES=true
    $OPENCODE_DISABLE_EXPLICIT || ENABLE_OPENCODE=true
    ENABLE_APE=true
    ENABLE_PERPLEXICA=true
    ENABLE_PRIVACY_SHIELD=true
    ENABLE_ODS_PROXY=true
    $WEBUI_DISABLE_EXPLICIT || ENABLE_OPEN_WEBUI=true
    # --all enables Langfuse unless the user explicitly passed --no-langfuse.
    $NO_LANGFUSE_EXPLICIT || ENABLE_LANGFUSE=true
fi

# ── Locate script directory and source tree root ──
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"

# ── Source libraries ──
LIB_DIR="${SCRIPT_DIR}/lib"
source "${LIB_DIR}/constants.sh"

# Any failed install, including a set -e stop, ends with where to get help.
# The libraries trap EXIT only inside subshells, so this stays in place.
_macos_help_on_failure() {
    local status=$?
    if (( status != 0 )); then
        echo "" >&2
        echo "  Need help? Ask on the ODS Discord: ${ODS_HELP_DISCORD_URL}" >&2
        echo "  Share the messages above and the end of ${ODS_LOG_FILE}." >&2
    fi
    return "$status"
}
trap _macos_help_on_failure EXIT

source "${LIB_DIR}/opencode-selection.sh"
source "${LIB_DIR}/ui.sh"
macos_apply_presentation_mode
source "${LIB_DIR}/bridge-manager.sh"
source "${LIB_DIR}/native-model.sh"
source "${LIB_DIR}/native-runtime-download.sh"
source "${LIB_DIR}/tier-map.sh"
source "${LIB_DIR}/detection.sh"
source "${LIB_DIR}/preflight-fs.sh"
source "${LIB_DIR}/env-generator.sh"
source "${LIB_DIR}/installed-footprint.sh"
source "${LIB_DIR}/host-agent-listener.sh"
source "${LIB_DIR}/host-agent-install.sh"
source "${LIB_DIR}/post-pixel-install.sh"
if [[ -f "${SOURCE_ROOT}/installers/lib/compose-failure-report.sh" ]]; then
    source "${SOURCE_ROOT}/installers/lib/compose-failure-report.sh"
fi
source "${SOURCE_ROOT}/lib/safe-env.sh"
if [[ -f "${SOURCE_ROOT}/lib/python-cmd.sh" ]]; then
    source "${SOURCE_ROOT}/lib/python-cmd.sh"
fi
source "${SOURCE_ROOT}/installers/lib/readiness-summary.sh"
source "${SOURCE_ROOT}/installers/lib/secure-log.sh"

# ── File-local helpers ──
_close_inherited_fds_for_daemon() {
    local fd fd_dir fd_name

    for fd_dir in "/proc/${BASHPID:-$$}/fd" "/dev/fd"; do
        [[ -d "$fd_dir" ]] || continue
        for fd in "$fd_dir"/*; do
            fd_name="${fd##*/}"
            [[ "$fd_name" =~ ^[0-9]+$ ]] || continue
            (( fd_name <= 2 || fd_name == 255 )) && continue
            eval "exec ${fd_name}>&-" 2>/dev/null || true
        done
        return 0
    done

    for ((fd_name = 3; fd_name <= 254; fd_name++)); do
        eval "exec ${fd_name}>&-" 2>/dev/null || true
    done
}


_write_macos_cloud_auth_overlay() {
    local overlay_path="$1"

    mkdir -p "$(dirname "$overlay_path")"
    cat > "$overlay_path" <<'CLOUD_AUTH_EOF'
# Generated by install-macos.sh. The secret stays in .env and is resolved by
# Docker Compose, so this file is safe to inspect and persist in .compose-flags.
# Only always-present services belong here. Optional services read their keys
# from .env so compose.yaml <-> compose.yaml.disabled toggles remain valid.
services:
  open-webui:
    environment:
      OPENAI_API_KEY: "${LITELLM_KEY:?LITELLM_KEY must be set}"
CLOUD_AUTH_EOF
}

_macos_set_builtin_compose_state() {
    local service_id="$1" enabled="$2"
    local service_dir="${INSTALL_DIR}/extensions/services/${service_id}"
    local active="${service_dir}/compose.yaml"
    local disabled="${service_dir}/compose.yaml.disabled"

    [[ -d "$service_dir" ]] || return 0
    if [[ "$enabled" == "true" ]]; then
        if [[ -f "$active" ]]; then
            rm -f "$disabled"
        elif [[ -f "$disabled" ]]; then
            mv "$disabled" "$active"
            ai_ok "Enabled built-in extension: ${service_id}"
        fi
    elif [[ -f "$active" ]]; then
        mv -f "$active" "$disabled"
        log "Disabled built-in extension compose: ${service_id}"
    fi
}

_macos_sync_builtin_compose_states() {
    _macos_set_builtin_compose_state litellm "$ENABLE_LITELLM"
    _macos_set_builtin_compose_state searxng "$ENABLE_SEARXNG"
    _macos_set_builtin_compose_state token-spy "$ENABLE_RECOMMENDED"
    _macos_set_builtin_compose_state whisper "$ENABLE_VOICE"
    _macos_set_builtin_compose_state tts "$ENABLE_VOICE"
    _macos_set_builtin_compose_state n8n "$ENABLE_WORKFLOWS"
    _macos_set_builtin_compose_state qdrant "$ENABLE_RAG"
    _macos_set_builtin_compose_state embeddings "$ENABLE_RAG"
    _macos_set_builtin_compose_state hermes "$ENABLE_HERMES"
    _macos_set_builtin_compose_state hermes-proxy "$ENABLE_HERMES"
    _macos_set_builtin_compose_state ape "$ENABLE_APE"
    _macos_set_builtin_compose_state perplexica "$ENABLE_PERPLEXICA"
    _macos_set_builtin_compose_state privacy-shield "$ENABLE_PRIVACY_SHIELD"
    _macos_set_builtin_compose_state ods-proxy "$ENABLE_ODS_PROXY"
    _macos_set_builtin_compose_state tailscale "$ENABLE_TAILSCALE"
    _macos_set_builtin_compose_state langfuse "$ENABLE_LANGFUSE"
    _macos_set_builtin_compose_state brave-search "${ENABLE_BRAVE_SEARCH:-false}"
}

_macos_resolve_support_services() {
    # The gateway serves the base chat UI, Portal and cloud mode. Selecting it
    # must not pull the optional recommended support bundle.
    ENABLE_LITELLM=true

    ENABLE_SEARXNG=false
    if $ENABLE_RECOMMENDED || $ENABLE_PERPLEXICA || $ENABLE_HERMES; then
        ENABLE_SEARXNG=true
    fi
    if $ENABLE_PIXEL; then
        # Match native onboarding: the installed environment wins, then
        # retained private answers, then the fresh parallel-free default.
        local provider
        provider="$(read_env_value "${INSTALL_DIR}/.env" PIXEL_WEB_SEARCH_PROVIDER)"
        provider="${provider#\"}"; provider="${provider%\"}"
        provider="${provider#\'}"; provider="${provider%\'}"
        if [[ -z "$provider" ]]; then
            local answers="${INSTALL_DIR}/data/pixel-native/preparation/onboarding.json"
            provider="$(/usr/bin/python3 "${SOURCE_ROOT}/extensions/services/pixel-agent/host/native_search.py" \
                --answers-file "$answers")" || return 1
        fi
        case "$provider" in
            searxng) ENABLE_SEARXNG=true ;;
            parallel-free) ;;
            *) ai_err "Unsupported Pixel search provider: ${provider}"; return 1 ;;
        esac
    fi
    ENABLE_WEB_SEARCH=$ENABLE_SEARXNG
}

_macos_apply_fresh_feature_defaults() {
    # Unattended and dry-run fresh installs should match the interactive Core
    # default. An existing installation and explicit selections keep their
    # previous behavior; the native Pixel lifecycle guard still owns reruns.
    if [[ ! -f "${INSTALL_DIR}/.env" ]] && ! $ALL_FEATURES \
        && { $NON_INTERACTIVE || $DRY_RUN; }; then
        $RECOMMENDED_EXPLICIT || ENABLE_RECOMMENDED=false
        $HERMES_EXPLICIT || ENABLE_HERMES=false
    fi
}

_macos_ask_hermes() {
    # Portal and Hermes are alternative agents, and native Portal always
    # replaces Hermes. Asking would offer a choice the installer then ignores.
    if $ENABLE_PIXEL; then
        ENABLE_HERMES=false
        ai "Hermes Agent is not offered while Portal is the agent. To use Hermes instead, do a fresh install with --no-pixel."
        return 0
    fi
    local yn
    read -r -p "  Enable Hermes Agent (default AI agent)? [Y/n] " yn < /dev/tty
    [[ "$yn" =~ ^[nN] ]] && ENABLE_HERMES=false || ENABLE_HERMES=true
}

_macos_resolve_webui_selection() {
    [[ -f "${INSTALL_DIR}/.env" ]] || return 0
    WEBUI_RETAINED="$(read_env_value "${INSTALL_DIR}/.env" ENABLE_OPEN_WEBUI)"
    case "$WEBUI_RETAINED" in
        "") WEBUI_RETAINED=true ;;  # Older Mac installs always selected WebUI.
        true|false) ;;
        *) ai_err "Invalid retained ENABLE_OPEN_WEBUI selection"; return 1 ;;
    esac
    if ! $WEBUI_ENABLE_EXPLICIT && ! $WEBUI_DISABLE_EXPLICIT && ! $ALL_FEATURES; then
        ENABLE_OPEN_WEBUI="$WEBUI_RETAINED"
    fi
}

_macos_patch_hermes_persisted_config() {
    local model_name="$1" base_url="$2" context_length="$3"
    local state hermes_image helper_image project_image selected_image persisted_config candidate
    local -a patch_command=()

    persisted_config="${INSTALL_DIR}/data/hermes/config.yaml"
    state="$(docker inspect --format '{{.State.Status}}' ods-hermes 2>/dev/null || true)"
    if [[ "$state" == "running" ]] \
       && docker exec --user 0:0 ods-hermes python3 -c 'import yaml' >/dev/null 2>&1; then
        patch_command=(docker exec --user 0:0 -i ods-hermes python3 - "$model_name" "$base_url" "$context_length")
    fi

    if (( ${#patch_command[@]} == 0 )); then
        helper_image="$(docker inspect --format '{{.Config.Image}}' ods-dashboard-api 2>/dev/null || true)"
        project_image="$(basename "$INSTALL_DIR" | tr '[:upper:]' '[:lower:]')-dashboard-api:latest"
        hermes_image="$(docker inspect --format '{{.Config.Image}}' ods-hermes 2>/dev/null || true)"
        [[ -n "$hermes_image" ]] || hermes_image="$(read_env_value "${INSTALL_DIR}/.env" "HERMES_AGENT_IMAGE")"
        [[ -n "$hermes_image" ]] || hermes_image="nousresearch/hermes-agent:v2026.9.24@sha256:fca358f12efd65bfaaca05884166f15c0e2788375ca30d77061ac1ebc96452b7"

        # The Hermes runtime image is not guaranteed to include PyYAML. Probe
        # candidates instead of treating a cached image as a usable migrator.
        # dashboard-api is preferred because its application imports yaml.
        selected_image=""
        for candidate in "$helper_image" "$project_image" \
            "ods-dashboard-api:latest" "$hermes_image"; do
            [[ -n "$candidate" ]] || continue
            docker image inspect "$candidate" >/dev/null 2>&1 || continue
            if docker run --rm --pull never --network none --user 0:0 \
                --entrypoint python3 "$candidate" -c 'import yaml' >/dev/null 2>&1; then
                selected_image="$candidate"
                break
            fi
        done

        if [[ -z "$selected_image" ]]; then
            if [[ ! -e "$(dirname "$persisted_config")" ]] \
               || { [[ -x "$(dirname "$persisted_config")" ]] && [[ ! -e "$persisted_config" ]]; }; then
                return 3
            fi
            return 4
        fi
        patch_command=(
            docker run --rm --pull never --network none --user 0:0 -i
            -v "${INSTALL_DIR}/data/hermes:/opt/data"
            --entrypoint python3 "$selected_image" - "$model_name" "$base_url" "$context_length"
        )
    fi

    "${patch_command[@]}" <<'HERMES_LIVE_PATCH_PY'
import os
import sys
from pathlib import Path

import yaml

path = Path("/opt/data/config.yaml")
if not path.is_file():
    raise SystemExit(3)

model_name, base_url, context_text = sys.argv[1:4]
context_length = int(context_text)
data = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
if not isinstance(data, dict):
    raise SystemExit("Hermes config root is not a mapping")
model = data.setdefault("model", {})
if not isinstance(model, dict):
    raise SystemExit("Hermes model config is not a mapping")
model["default"] = model_name
model["base_url"] = base_url
model["context_length"] = context_length
# OPENAI_API_KEY from compose is authoritative. Removing a persisted key keeps
# local/cloud transitions and later extension toggles in sync with .env.
model.pop("api_key", None)
auxiliary = data.setdefault("auxiliary", {})
if not isinstance(auxiliary, dict):
    raise SystemExit("Hermes auxiliary config is not a mapping")
compression = auxiliary.setdefault("compression", {})
if not isinstance(compression, dict):
    raise SystemExit("Hermes auxiliary compression config is not a mapping")
compression["context_length"] = context_length

st = path.stat()
tmp = path.with_name(f"{path.name}.ods-transition-{os.getpid()}.tmp")
with tmp.open("w", encoding="utf-8") as handle:
    yaml.safe_dump(data, handle, sort_keys=False, allow_unicode=True)
    handle.flush()
    os.fsync(handle.fileno())
os.chmod(tmp, st.st_mode & 0o777)
try:
    os.chown(tmp, st.st_uid, st.st_gid)
except PermissionError:
    tmp_st = tmp.stat()
    if (tmp_st.st_uid, tmp_st.st_gid) != (st.st_uid, st.st_gid):
        tmp.unlink(missing_ok=True)
        raise
os.replace(tmp, path)

check = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
check_model = check.get("model") or {}
if check_model.get("default") != model_name:
    raise SystemExit("Hermes model verification failed")
if check_model.get("base_url") != base_url:
    raise SystemExit("Hermes base_url verification failed")
if int(check_model.get("context_length") or 0) != context_length:
    raise SystemExit("Hermes context verification failed")
if "api_key" in check_model:
    raise SystemExit("Hermes persisted api_key was not removed")
check_compression = (check.get("auxiliary") or {}).get("compression") or {}
if int(check_compression.get("context_length") or 0) != context_length:
    raise SystemExit("Hermes compression context verification failed")
HERMES_LIVE_PATCH_PY
}

_macos_verify_hermes_container_auth() {
    local expected_key="$1" expected_hash
    [[ -n "$expected_key" ]] || return 1
    expected_hash="$(printf '%s' "$expected_key" | shasum -a 256 | awk '{print $1}')"
    docker exec -i ods-hermes python3 - "$expected_hash" <<'HERMES_AUTH_VERIFY_PY'
import hashlib
import os
import sys

actual = os.environ.get("OPENAI_API_KEY", "")
expected_hash = sys.argv[1]
raise SystemExit(0 if actual and hashlib.sha256(actual.encode()).hexdigest() == expected_hash else 1)
HERMES_AUTH_VERIFY_PY
}

# Copy the finished example projects ODS ships (Elf of Doom) into the owner's
# native Portal workspace whenever Playground/elf-of-doom is absent and this
# workspace has never received it, so a deleted or renamed example stays that
# way. The record of received examples lives in Portal's state folder, which
# the agent's sandbox never mounts. Portal's sidebar then lists it with the
# other projects; the installer prints nothing about it. Runs after native
# activation; a failure never fails the install. The outcome, and a workspace
# layout the helper never writes into (exit status 3, which stays as the owner
# made it), go only to the install log. Only a failure warns, with its cause,
# recovery and help link.
_macos_add_example_projects() {
    local install_root status code=0
    # The native workspace path is built from the resolved install directory,
    # and the helper refuses any symlinked path component.
    if ! install_root="$(cd "$INSTALL_DIR" && pwd -P)"; then
        ai_warn "Portal is ready, but ODS could not add its Elf of Doom example project: it could not open the ODS install folder $INSTALL_DIR. Nothing in your Portal workspace was changed. Ask on Discord if you need help: ${ODS_HELP_DISCORD_URL} (details: $ODS_LOG_FILE)"
        return 0
    fi
    status="$(/usr/bin/python3 -B "$INSTALL_DIR/installers/lib/pixel-example-projects.py" \
        --workspace "$install_root/data/pixel-native/home/.openclaw/workspace-pixel" \
        --state-dir "$install_root/data/pixel-native/home/.openclaw" 2>>"$ODS_LOG_FILE")" || code=$?
    case "$code" in
        0) printf 'Portal example projects: %s\n' "$status" >>"$ODS_LOG_FILE" ;;
        3) printf 'Portal example projects not added: %s\n' "$status" >>"$ODS_LOG_FILE" ;;
        4) ai_warn "Portal is ready, but ODS could not add its Elf of Doom example project: $status Nothing in your Portal workspace was replaced. Ask on Discord if you need help: ${ODS_HELP_DISCORD_URL} (details: $ODS_LOG_FILE)" ;;
        *) ai_warn "Portal is ready, but ODS could not add its Elf of Doom example project: its copy step stopped unexpectedly (exit $code). Nothing in your Portal workspace was replaced. Ask on Discord with the details from $ODS_LOG_FILE: ${ODS_HELP_DISCORD_URL}" ;;
    esac
    return 0
}


_macos_bootstrap_upgrade_pid_is_owned() {
    local pid="$1"
    local upgrade_script="${INSTALL_DIR}/scripts/bootstrap-upgrade.sh"
    local command_line process_name

    [[ "$pid" =~ ^[1-9][0-9]*$ ]] || return 1
    [[ "$pid" -ne "$$" ]] || return 1
    process_name="$(ps -ww -p "$pid" -o comm= 2>/dev/null || true)"
    command_line="$(ps -ww -p "$pid" -o command= 2>/dev/null || true)"
    [[ "${process_name##*/}" == "bash" ]] || return 1
    case "$command_line" in
        "bash ${upgrade_script} ${INSTALL_DIR} "*|*/bash" ${upgrade_script} ${INSTALL_DIR} "*) return 0 ;;
    esac
    return 1
}

_macos_collect_process_descendants() {
    local parent_pid="$1" child_pid
    while IFS= read -r child_pid; do
        [[ "$child_pid" =~ ^[1-9][0-9]*$ ]] || continue
        _macos_collect_process_descendants "$child_pid"
        printf '%s\n' "$child_pid"
    done < <(pgrep -P "$parent_pid" 2>/dev/null || true)
}

_macos_cancel_detached_bootstrap_upgrade() {
    local cancel_reason="${1:-cloud_mode}"
    local action_label="${2:-cloud transition}"
    local status_file="${INSTALL_DIR}/data/bootstrap-status.json"
    local args_file="${INSTALL_DIR}/data/bootstrap-upgrade.args"
    local pid_file="${INSTALL_DIR}/data/bootstrap-upgrade.pid"
    local status="" pid pgid child attempt alive should_mark_cancelled=false
    local -a pids=() groups=() legacy_tree=()

    if ! command -v pgrep >/dev/null 2>&1; then
        ai_err "Cannot safely inspect detached model upgrades because pgrep is unavailable."
        return 1
    fi

    if [[ -f "$pid_file" ]]; then
        pid="$(tr -dc '0-9' < "$pid_file" 2>/dev/null || true)"
        if _macos_bootstrap_upgrade_pid_is_owned "$pid"; then
            pids+=("$pid")
        fi
    fi
    while IFS= read -r pid; do
        [[ -n "$pid" ]] || continue
        if _macos_bootstrap_upgrade_pid_is_owned "$pid"; then
            case " ${pids[*]} " in
                *" $pid "*) ;;
                *) pids+=("$pid") ;;
            esac
        fi
    done < <(pgrep -f '[/]bootstrap-upgrade[.]sh' 2>/dev/null || true)

    if [[ -f "$status_file" ]]; then
        status="$(grep -o '"status"[[:space:]]*:[[:space:]]*"[^"]*"' "$status_file" 2>/dev/null \
            | sed -n '1p' | cut -d'"' -f4 || true)"
        case "$status" in
            starting|downloading|verifying|swapping|failed) should_mark_cancelled=true ;;
        esac
    fi
    [[ -f "$args_file" ]] && should_mark_cancelled=true
    [[ -f "$pid_file" ]] && should_mark_cancelled=true
    [[ "${#pids[@]}" -gt 0 ]] && should_mark_cancelled=true

    if [[ "${#pids[@]}" -gt 0 ]]; then
        ai "Stopping ${#pids[@]} install-owned background model upgrade process(es) before ${action_label}..."
        for pid in "${pids[@]}"; do
            pgid="$(ps -ww -p "$pid" -o pgid= 2>/dev/null | tr -d '[:space:]' || true)"
            if [[ "$pgid" == "$pid" ]]; then
                groups+=("$pgid")
            else
                while IFS= read -r child; do
                    [[ "$child" =~ ^[1-9][0-9]*$ ]] && legacy_tree+=("$child")
                done < <(_macos_collect_process_descendants "$pid")
                legacy_tree+=("$pid")
            fi
        done

        # New installers launch each worker in its own session/process group,
        # so TERM reaches curl, the progress monitor, and any hot-swap child.
        # The legacy PID-tree fallback safely retires workers from older builds.
        for pgid in "${groups[@]}"; do
            kill -TERM -- "-${pgid}" 2>/dev/null || true
        done
        for pid in "${legacy_tree[@]}"; do
            kill -TERM "$pid" 2>/dev/null || true
        done

        for ((attempt=1; attempt<=40; attempt++)); do
            alive=false
            for pgid in "${groups[@]}"; do
                kill -0 -- "-${pgid}" 2>/dev/null && alive=true
            done
            for pid in "${legacy_tree[@]}"; do
                kill -0 "$pid" 2>/dev/null && alive=true
            done
            ! $alive && break
            sleep 0.25
        done

        if $alive; then
            ai_warn "Background model upgrade tree did not stop after SIGTERM; forcing remaining process-group members."
            for pgid in "${groups[@]}"; do
                kill -KILL -- "-${pgid}" 2>/dev/null || true
            done
            for pid in "${legacy_tree[@]}"; do
                kill -KILL "$pid" 2>/dev/null || true
            done
            for ((attempt=1; attempt<=20; attempt++)); do
                alive=false
                for pgid in "${groups[@]}"; do
                    kill -0 -- "-${pgid}" 2>/dev/null && alive=true
                done
                for pid in "${legacy_tree[@]}"; do
                    kill -0 "$pid" 2>/dev/null && alive=true
                done
                ! $alive && break
                sleep 0.1
            done
        fi

        if $alive; then
            ai_err "Could not stop the complete install-owned background upgrade tree; refusing to continue ${action_label}."
            return 1
        fi
    fi

    rm -f "$args_file" "$pid_file" 2>/dev/null || {
        ai_err "Could not disable bootstrap-upgrade process/retry metadata."
        return 1
    }
    if [[ "$should_mark_cancelled" == "true" ]]; then
        local status_tmp="${status_file}.${cancel_reason}.$$"
        local cancelled_at
        cancelled_at="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
        mkdir -p "$(dirname "$status_file")"
        if ! printf '{"status":"cancelled","reason":"%s","updatedAt":"%s"}\n' "$cancel_reason" "$cancelled_at" > "$status_tmp" \
           || ! mv "$status_tmp" "$status_file"; then
            rm -f "$status_tmp" 2>/dev/null || true
            ai_err "Could not mark the background model upgrade cancelled for ${action_label}."
            return 1
        fi
        ai_ok "Background model upgrade disabled for ${action_label}"
    fi
}

_macos_launch_detached_bootstrap_upgrade() {
    local upgrade_script="$1"
    shift
    local pid_file="${INSTALL_DIR}/data/bootstrap-upgrade.pid"
    local log_file="${INSTALL_DIR}/logs/model-upgrade.log"
    local python_cmd="${PYTHON_CMD:-/usr/bin/python3}"
    local bash_cmd="${BASH:-bash}"
    [[ -x "$python_cmd" ]] || python_cmd="/usr/bin/python3"
    if command -v cygpath >/dev/null 2>&1; then
        bash_cmd="$(cygpath -w "$bash_cmd" 2>/dev/null || printf '%s' "$bash_cmd")"
    fi

    BOOTSTRAP_BASH="$bash_cmd" "$python_cmd" - "$pid_file" "$log_file" "$upgrade_script" "$@" <<'BOOTSTRAP_LAUNCH_PY'
import os
import subprocess
import sys
from pathlib import Path

pid_path = Path(sys.argv[1])
log_path = Path(sys.argv[2])
script = sys.argv[3]
script_args = sys.argv[4:]
if not script_args:
    raise SystemExit("bootstrap launcher requires the install directory")
bash_exe = os.environ.get("BOOTSTRAP_BASH") or os.environ.get("BASH") or "bash"
log_path.parent.mkdir(parents=True, exist_ok=True)
pid_path.parent.mkdir(parents=True, exist_ok=True)
with log_path.open("ab", buffering=0) as log_handle:
    proc = subprocess.Popen(
        [bash_exe, script, *script_args],
        cwd=script_args[0],
        stdin=subprocess.DEVNULL,
        stdout=log_handle,
        stderr=subprocess.STDOUT,
        close_fds=True,
        start_new_session=True,
    )
tmp = pid_path.with_name(f"{pid_path.name}.{os.getpid()}.tmp")
tmp.write_text(f"{proc.pid}\n", encoding="ascii")
os.chmod(tmp, 0o600)
os.replace(tmp, pid_path)
BOOTSTRAP_LAUNCH_PY
}

_macos_persist_bootstrap_upgrade_args() {
    local full_gguf_file="$1"
    local full_gguf_url="$2"
    local full_gguf_sha256="$3"
    local full_llm_model="$4"
    local full_max_context="$5"
    local bootstrap_gguf_file="${6:-Qwen3.5-2B-Q4_K_M.gguf}"
    local args_file="${INSTALL_DIR}/data/bootstrap-upgrade.args"

    mkdir -p "${INSTALL_DIR}/data"
    {
        printf '%s\n' "$full_gguf_file"
        printf '%s\n' "$full_gguf_url"
        printf '%s\n' "$full_gguf_sha256"
        printf '%s\n' "$full_llm_model"
        printf '%s\n' "$full_max_context"
        printf '%s\n' "$bootstrap_gguf_file"
    } > "${args_file}.tmp" && mv "${args_file}.tmp" "$args_file" || {
        rm -f "${args_file}.tmp" 2>/dev/null || true
        ai_warn "Could not persist bootstrap-upgrade retry metadata"
        return 1
    }
    chmod 600 "$args_file" 2>/dev/null || true
}

_macos_native_llama_cwd_is_owned() {
    local process_cwd="$1" home_dir="${HOME:-}"
    [[ -n "$process_cwd" ]] || return 1
    [[ "$process_cwd" == "$INSTALL_DIR" ]] && return 0
    return 1
}

_macos_native_llama_pid_is_owned() {
    local pid="$1" command_line process_name process_cwd
    [[ "$pid" =~ ^[1-9][0-9]*$ ]] || return 1
    process_name="$(ps -ww -p "$pid" -o comm= 2>/dev/null || true)"
    command_line="$(ps -ww -p "$pid" -o command= 2>/dev/null || true)"
    [[ "${process_name##*/}" == "llama-server" || "${process_name##*/}" == "${LLAMA_SERVER_BIN##*/}" ]] || return 1
    [[ "$command_line" == *"${INSTALL_DIR}/bin/llama-server"* ]] && return 0
    if [[ -n "${LLAMA_SERVER_BIN:-}" && "$command_line" == *"$LLAMA_SERVER_BIN"* ]]; then
        # A registered runtime can be shared by multiple installs. Its path
        # alone is no longer ownership proof; the launcher anchors its cwd.
        process_cwd="$(lsof -a -p "$pid" -d cwd -Fn 2>/dev/null | sed -n 's/^n//p' | head -1)"
        _macos_native_llama_cwd_is_owned "$process_cwd"
        return
    fi
    case "$command_line" in
        ./bin/llama-server*|bin/llama-server*)
            process_cwd="$(lsof -a -p "$pid" -d cwd -Fn 2>/dev/null | sed -n 's/^n//p' | head -1)"
            _macos_native_llama_cwd_is_owned "$process_cwd"
            ;;
        *) return 1 ;;
    esac
}

_macos_stop_install_owned_native_llama() {
    local reason="${1:-Stopping install-owned native llama-server}" pid attempt
    local -a candidates=() remaining=()
    if [[ -f "$INSTALL_DIR/installers/macos/lib/native-llama-service.sh" ]]; then
        bash "$INSTALL_DIR/installers/macos/lib/native-llama-service.sh" stop \
            "$INSTALL_DIR" "$LLAMA_SERVER_BIN" "$LLAMA_SERVER_PID_FILE" || return 1
    fi
    if [[ -f "$LLAMA_SERVER_PID_FILE" ]]; then
        pid="$(tr -dc '0-9' < "$LLAMA_SERVER_PID_FILE" 2>/dev/null || true)"
        _macos_native_llama_pid_is_owned "$pid" && candidates+=("$pid")
    fi
    if command -v pgrep >/dev/null 2>&1; then
        while IFS= read -r pid; do
            _macos_native_llama_pid_is_owned "$pid" || continue
            case " ${candidates[*]} " in
                *" $pid "*) ;;
                *) candidates+=("$pid") ;;
            esac
        done < <(pgrep -f '[/]llama-server' 2>/dev/null || true)
    fi
    [[ "${#candidates[@]}" -gt 0 ]] || { rm -f "$LLAMA_SERVER_PID_FILE" 2>/dev/null || true; return 0; }

    ai "$reason"
    for pid in "${candidates[@]}"; do kill -TERM "$pid" 2>/dev/null || true; done
    for ((attempt=1; attempt<=20; attempt++)); do
        remaining=()
        for pid in "${candidates[@]}"; do
            kill -0 "$pid" 2>/dev/null && remaining+=("$pid")
        done
        candidates=("${remaining[@]}")
        [[ "${#candidates[@]}" -eq 0 ]] && break
        sleep 0.25
    done
    for pid in "${candidates[@]}"; do kill -KILL "$pid" 2>/dev/null || true; done
    for ((attempt=1; attempt<=20; attempt++)); do
        remaining=()
        for pid in "${candidates[@]}"; do
            kill -0 "$pid" 2>/dev/null && remaining+=("$pid")
        done
        candidates=("${remaining[@]}")
        [[ "${#candidates[@]}" -eq 0 ]] && break
        sleep 0.1
    done
    if [[ "${#candidates[@]}" -gt 0 ]]; then
        ai_err "Could not stop the complete install-owned native llama-server process set."
        return 1
    fi
    rm -f "$LLAMA_SERVER_PID_FILE" 2>/dev/null || true
}


COLIMA_VM_IP=""
COLIMA_HOST_IP=""
COLIMA_PRIVATE_ROUTE_PREFERRED=false
COLIMA_PROFILE=""

_resolve_active_colima_profile() {
    local context endpoint candidate=""

    context="$(docker context show 2>>"$ODS_LOG_FILE" || true)"
    [[ -n "$context" ]] || return 1
    case "$context" in
        colima)
            candidate="default"
            ;;
        colima-*)
            candidate="${context#colima-}"
            ;;
        *)
            endpoint="$(docker context inspect "$context" \
                --format '{{.Endpoints.docker.Host}}' 2>>"$ODS_LOG_FILE" || true)"
            if [[ "$endpoint" =~ /\.colima/([^/]+)/docker\.sock$ ]]; then
                candidate="${BASH_REMATCH[1]}"
            fi
            ;;
    esac

    # Colima profile names become filesystem and Docker-context components.
    # Fail closed rather than passing an ambiguous value back to the CLI.
    [[ "$candidate" =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ ]] || return 1
    COLIMA_PROFILE="$candidate"
}

_active_colima() {
    local subcommand="$1"
    shift
    [[ -n "$COLIMA_PROFILE" ]] || return 1
    if [[ "$COLIMA_PROFILE" == "default" ]]; then
        command colima "$subcommand" "$@"
    else
        command colima "$subcommand" --profile "$COLIMA_PROFILE" "$@"
    fi
}

_active_colima_hint_args() {
    if [[ -n "$COLIMA_PROFILE" && "$COLIMA_PROFILE" != "default" ]]; then
        printf ' --profile %s' "$COLIMA_PROFILE"
    fi
}

_detect_colima_private_network() {
    local status_json interface_name colima_config preferred_route
    COLIMA_VM_IP=""
    COLIMA_HOST_IP=""
    COLIMA_PRIVATE_ROUTE_PREFERRED=false

    status_json="$(_active_colima status --json 2>>"$ODS_LOG_FILE" || true)"
    [[ -n "$status_json" ]] || return 1
    COLIMA_VM_IP="$(printf '%s' "$status_json" | /usr/bin/python3 -c '
import json, sys
try:
    value = json.load(sys.stdin).get("ip_address", "")
except (AttributeError, json.JSONDecodeError):
    value = ""
print(value if isinstance(value, str) else "")
' 2>/dev/null || true)"
    [[ -n "$COLIMA_VM_IP" ]] || return 1

    interface_name="$(/sbin/route -n get "$COLIMA_VM_IP" 2>/dev/null | awk '/interface:/{print $2; exit}')"
    [[ -n "$interface_name" ]] || return 1
    COLIMA_HOST_IP="$(/sbin/ifconfig "$interface_name" 2>/dev/null | awk '/inet / && $2 != "127.0.0.1" {print $2; exit}')"
    [[ -n "$COLIMA_HOST_IP" ]] || return 1

    COLIMA_VM_IP="$COLIMA_VM_IP" COLIMA_HOST_IP="$COLIMA_HOST_IP" /usr/bin/python3 -c '
import ipaddress, os
vm = ipaddress.ip_address(os.environ["COLIMA_VM_IP"])
host = ipaddress.ip_address(os.environ["COLIMA_HOST_IP"])
network = ipaddress.ip_network(f"{vm}/24", strict=False)
valid = vm.version == 4 and host.version == 4 and host != vm and host in network and host.is_private
raise SystemExit(0 if valid else 1)
' >/dev/null 2>&1 || return 1

    colima_config="${COLIMA_HOME:-$HOME/.colima}/${COLIMA_PROFILE}/colima.yaml"
    preferred_route="$(awk '
        /^network:/ { in_network=1; next }
        in_network && /^[^[:space:]]/ { in_network=0 }
        in_network && /^[[:space:]]+preferredRoute:/ { print $2; exit }
    ' "$colima_config" 2>/dev/null || true)"
    [[ "$preferred_route" == "true" ]] && COLIMA_PRIVATE_ROUTE_PREFERRED=true
    return 0
}

_ensure_colima_private_network() {
    [[ "${DOCKER_BACKEND:-unknown}" == "colima" ]] || return 0
    if ! command -v colima >/dev/null 2>&1; then
        ai_err "Docker is using Colima, but the colima CLI is not on PATH."
        return 1
    fi
    if ! _resolve_active_colima_profile; then
        ai_err "Could not map the active Docker context to a safe Colima profile."
        ai "  Select a Colima context (for example: docker context use colima) and re-run."
        return 1
    fi

    if _detect_colima_private_network && [[ "$COLIMA_PRIVATE_ROUTE_PREFERRED" == "true" ]]; then
        ai_ok "Colima private host bridge ready (${COLIMA_HOST_IP} <-> ${COLIMA_VM_IP})"
        return 0
    fi

    if $DRY_RUN; then
        ai "[DRY RUN] Would restart Colima with private vmnet as the preferred route"
        return 0
    fi
    if $PREFLIGHT_ONLY; then
        ai "The installer will restart Colima with private vmnet as the preferred route"
        return 0
    fi

    ai_warn "Colima needs a preferred private VM route; restarting its VM to configure one."
    ai_warn "Running non-ODS containers will restart with the Colima VM. Container data is preserved."
    if ! _active_colima stop >>"$ODS_LOG_FILE" 2>&1 \
       || ! _active_colima start --network-address --network-preferred-route >>"$ODS_LOG_FILE" 2>&1; then
        ai_err "Could not enable Colima private networking."
        local profile_args
        profile_args="$(_active_colima_hint_args)"
        ai "  Run: colima stop${profile_args} && colima start${profile_args} --network-address --network-preferred-route"
        return 1
    fi

    local attempt
    for attempt in $(seq 1 60); do
        docker info >/dev/null 2>&1 && break
        sleep 1
    done
    if ! docker info >/dev/null 2>&1 \
       || ! _detect_colima_private_network \
       || [[ "$COLIMA_PRIVATE_ROUTE_PREFERRED" != "true" ]]; then
        ai_err "Colima restarted, but its preferred private host route could not be verified."
        return 1
    fi
    ai_ok "Colima private host bridge enabled (${COLIMA_HOST_IP} <-> ${COLIMA_VM_IP})"
}

_configure_macos_llm_bridge() {
    macos_configure_llm_bridge_from_env "${INSTALL_DIR}/.env" "$INSTALL_DIR"
}



# One Docker CPU floor covers every feature selection. The env generator caps
# each service's CPU limit (TTS, Whisper, Hermes) to Docker's CPUs, so no
# selection can ask compose for more CPUs than Docker has (#3600).
_require_docker_cpu_budget() {
    local min_cpus="${1:-6}"
    local docker_ncpu

    if ! [[ "$min_cpus" =~ ^[0-9]+$ ]] || [[ "$min_cpus" -lt 1 ]]; then
        min_cpus=6
    fi

    docker_ncpu=$(get_docker_available_cpus)
    if [[ "$docker_ncpu" =~ ^[0-9]+$ ]] && [[ "$docker_ncpu" -lt "$min_cpus" ]]; then
        ai_err "Docker has only ${docker_ncpu} CPU(s); the ODS installer requires at least ${min_cpus}."
        case "${DOCKER_BACKEND:-unknown}" in
            colima)
                local profile_args
                profile_args="$(_active_colima_hint_args)"
                ai "Stop and re-create the Colima VM with more CPUs:"
                ai "    colima stop${profile_args} && colima start${profile_args} --cpu ${min_cpus} --memory 12 --disk 60"
                ai "Then re-run this installer."
                ;;
            desktop)
                ai "Open Docker Desktop -> Settings -> Resources -> Advanced and raise CPUs to ${min_cpus}+, apply, then re-run."
                ;;
            rancher)
                ai "Open Rancher Desktop -> Preferences -> Virtual Machine -> Hardware and raise CPUs to ${min_cpus}+, apply, then re-run."
                ;;
            orbstack)
                ai "Open OrbStack -> Settings -> System and raise CPU allocation to ${min_cpus}+, then re-run."
                ;;
            *)
                ai "Raise your docker daemon's CPU allocation to ${min_cpus}+ and re-run."
                ;;
        esac
        ai "Or install with ${docker_ncpu} anyway (ODS runs slower): re-run with ODS_MIN_DOCKER_CPUS=${docker_ncpu} set."
        exit 1
    fi
    ai_ok "Docker CPU budget: ${docker_ncpu} (>=${min_cpus} required)"
}

# Speech recognition (WHISPER_CPU_LIMIT=4.0) and text-to-speech (at most 4
# threads) are sized for 4 CPUs each, so voice runs at full speed on the
# default floor. Fewer is only possible with a lower ODS_MIN_DOCKER_CPUS.
_warn_voice_cpu_budget() {
    local sized_for=4
    local docker_ncpu
    docker_ncpu=$(get_docker_available_cpus)
    if [[ "$docker_ncpu" =~ ^[0-9]+$ ]] && [[ "$docker_ncpu" -lt "$sized_for" ]]; then
        ai_warn "Voice: Docker has ${docker_ncpu} CPU(s), fewer than the ${sized_for} that speech recognition and text-to-speech are sized for, so speech will be slower. Voice still works."
    fi
}

_macos_python_imports_yaml() {
    local pycmd="${1:-python3}"
    "$pycmd" -c 'import yaml' >/dev/null 2>&1
}

_set_installer_python_cmd() {
    local pycmd="$1"
    export ODS_PYTHON_CMD="$pycmd"
    # python-cmd.sh caches the first runnable interpreter. Keep the cache aligned
    # when this installer creates a private venv after the first detection.
    if declare -p _ods_python_cmd_cached >/dev/null 2>&1; then
        _ods_python_cmd_cached="$pycmd"
    fi
}


_ensure_macos_pyyaml() {
    local pycmd=""
    if declare -f ods_detect_python_cmd >/dev/null 2>&1; then
        pycmd="$(ods_detect_python_cmd 2>/dev/null || true)"
    fi
    if [[ -z "$pycmd" ]]; then
        pycmd="$(command -v python3 2>/dev/null || true)"
    fi

    if [[ -z "$pycmd" ]]; then
        ai_err "python3 not available -- required for compose resolver"
        exit 1
    fi

    if _macos_python_imports_yaml "$pycmd"; then
        _set_installer_python_cmd "$pycmd"
        ai_ok "PyYAML available for $pycmd"
        return 0
    fi

    if $DRY_RUN; then
        ai_warn "PyYAML is not importable by $pycmd (dry-run: would create installer Python venv)."
        return 0
    fi
    if $PREFLIGHT_ONLY; then
        ai "PyYAML is not importable by $pycmd; the installer will create its Python venv."
        return 0
    fi

    local venv_dir="${INSTALL_DIR}/.venv/installer-python"
    local venv_python="${venv_dir}/bin/python"

    ai "Installing PyYAML in isolated installer Python runtime..."
    mkdir -p "$(dirname "$venv_dir")"
    if ! "$pycmd" -m venv "$venv_dir" 2>&1 | tee -a "$ODS_LOG_FILE" >/dev/null; then
        ai_err "Failed to create installer Python venv at $venv_dir."
        ai "  Your Python may be missing the venv module."
        ai "  Try: brew install python"
        ai "  Then re-run this installer."
        exit 1
    fi

    if "$venv_python" -m pip install --quiet --no-warn-script-location pyyaml 2>&1 | tee -a "$ODS_LOG_FILE" >/dev/null \
       && _macos_python_imports_yaml "$venv_python"; then
        _set_installer_python_cmd "$venv_python"
        ai_ok "PyYAML available in installer venv"
        return 0
    fi

    ai_err "Failed to install PyYAML for the macOS compose resolver."
    ai "  Log file: $ODS_LOG_FILE"
    ai "  Manual recovery:"
    ai "    $pycmd -m venv '$venv_dir' && '$venv_python' -m pip install pyyaml"
    exit 1
}

# The README clones the repository and runs ods/install.sh from the checkout.
# On a case-insensitive volume (the APFS default) ~/ods and a clone at ~/ODS are
# the same directory, so a string comparison misses that the install target is
# the checkout itself: the product would be copied into the clone and uninstall
# would delete it. Compare by inode and refuse any overlap other than running
# the installer from the install directory itself.
_ods_macos_install_dir_overlaps_source() {
    local install_dir="$1" source_dir="$2" dir
    [[ -n "$install_dir" && -n "$source_dir" ]] || return 1
    if [[ -e "$install_dir" ]]; then
        # The install target is an ancestor of the checkout, e.g. the clone root.
        dir="$(cd -P -- "$source_dir/.." 2>/dev/null && pwd -P)" || return 1
        while [[ -n "$dir" && "$dir" != "/" ]]; do
            [[ "$dir" -ef "$install_dir" ]] && return 0
            dir="$(dirname -- "$dir")"
        done
        [[ "$install_dir" -ef "$source_dir" ]] && return 1
    fi
    # The install target is inside the checkout.
    dir="$(dirname -- "$install_dir")"
    while [[ -n "$dir" && "$dir" != "/" && "$dir" != "." ]]; do
        [[ -e "$dir" && "$dir" -ef "$source_dir" ]] && return 0
        dir="$(dirname -- "$dir")"
    done
    return 1
}

# Resolve install directory
INSTALL_DIR="${ODS_INSTALL_DIR}"
if _ods_macos_install_dir_overlaps_source "$INSTALL_DIR" "$SOURCE_ROOT"; then
    ai_err "The install directory ${INSTALL_DIR} overlaps this source checkout (${SOURCE_ROOT})."
    ai "  On a case-insensitive disk, ~/ods and a clone at ~/ODS are the same folder."
    ai "  Move the checkout (for example: mv ~/ODS ~/src/ODS) or set ODS_INSTALL_DIR"
    ai "  to a separate path, then run the installer again. Nothing was changed."
    exit 1
fi
_macos_apply_fresh_feature_defaults
_macos_resolve_webui_selection || exit 1
if ! $OPENCODE_ENABLE_EXPLICIT && ! $OPENCODE_DISABLE_EXPLICIT && ! $ALL_FEATURES; then
    if ods_macos_opencode_retained "$OPENCODE_PLIST" "$OPENCODE_PLIST_LABEL" \
        "$OPENCODE_BUN_TMPDIR" "$(id -u)"; then
        ENABLE_OPENCODE=true
    fi
fi
if $ENABLE_OPENCODE && [[ -e "$OPENCODE_PLIST" || -L "$OPENCODE_PLIST" ]] \
    && ! ods_macos_opencode_plist_owned "$OPENCODE_PLIST" "$OPENCODE_PLIST_LABEL" "$OPENCODE_BUN_TMPDIR"; then
    ai_err "The OpenCode LaunchAgent path is not an ODS-owned plist; refusing to replace it."
    exit 1
fi
if $ENABLE_OPENCODE && launchctl print "gui/$(id -u)/${OPENCODE_PLIST_LABEL}" >/dev/null 2>&1 \
    && ! ods_macos_opencode_loaded_owned "$OPENCODE_PLIST" "$OPENCODE_PLIST_LABEL" \
        "$OPENCODE_BUN_TMPDIR" "$(id -u)"; then
    ai_err "The loaded OpenCode service is not backed by an ODS-owned plist; refusing to stop it."
    exit 1
fi

# --preflight-only runs while get-ods.sh --force still has the installation it
# is about to replace on disk. installers/reinstall-preflight.sh measures what
# removing it returns to this filesystem; any other run ignores the value.
PREFLIGHT_RECLAIMABLE_GB=0
if $PREFLIGHT_ONLY && [[ "${ODS_PREFLIGHT_RECLAIMABLE_KB:-}" =~ ^[0-9]+$ ]]; then
    PREFLIGHT_RECLAIMABLE_GB=$(( ODS_PREFLIGHT_RECLAIMABLE_KB / 1048576 ))
fi

# Both Pixel checks inspect the installation's own native Pixel state. During
# --preflight-only that is the installation being replaced, which the
# candidate uninstaller retires; the real install run checks the new tree.
if ! $PREFLIGHT_ONLY && ! $ENABLE_PIXEL && [[ -e "${INSTALL_DIR}/data/pixel-native" || -L "${INSTALL_DIR}/data/pixel-native" ]]; then
    ai_err "Existing native Pixel installation detected. The base installer cannot migrate it or disable it safely."
    ai "Your configuration is unchanged. Keep data/pixel-native; use the qualified native migration/update path when available."
    exit 1
fi

if $ENABLE_PIXEL && ! $PREFLIGHT_ONLY; then
    _pixel_install_args=(--install-dir "$INSTALL_DIR")
    if ! $NON_INTERACTIVE && ! $DRY_RUN; then
        _pixel_install_args+=(--prompt-for-sudo)
    fi
    /usr/bin/python3 "${LIB_DIR}/pixel-native-install.py" "${_pixel_install_args[@]}" \
        --preflight-only || exit 1
    ENABLE_HERMES=false
fi

# Reuse the same private-log guard as Linux. In particular, an existing log
# under macOS /tmp must be privatized before any diagnostic can append to it.
ods_prepare_install_log "$ODS_LOG_FILE" || exit 1

# ============================================================================
# PHASE 1 -- PREFLIGHT CHECKS
# ============================================================================
# With --preflight-only this phase is the whole run: every check below that
# can stop the install runs, steps that would change the host only report
# what the installer will do, and the script exits after the last check.
if $PREFLIGHT_ONLY; then
    show_phase 1 6 "PREFLIGHT CHECKS (preflight only)" "30 seconds"
    ai "Checking this host before an existing installation is replaced. No changes are made."
else
    show_ods_banner
    show_phase 1 6 "PREFLIGHT CHECKS" "30 seconds"
fi

# macOS version
get_macos_version
info_box "macOS:" "${MACOS_NAME} ${MACOS_VERSION} (${MACOS_BUILD})"
if [[ "$MACOS_MAJOR" -lt "$MIN_MACOS_MAJOR" ]]; then
    ai_err "macOS ${MIN_MACOS_MAJOR}+ (Ventura) is required for Metal 3. Found: ${MACOS_VERSION}"
    exit 1
fi
ai_ok "macOS version OK"

# Apple Silicon check
get_apple_silicon_info
if ! $APPLE_IS_APPLE_SILICON; then
    ai_err "Apple Silicon (arm64) is required. Detected: ${APPLE_ARCH}"
    ai_err "Intel Macs do not have Metal GPU acceleration needed for local inference."
    exit 1
fi
info_box "Chip:" "${APPLE_CHIP}"
info_box "Variant:" "${APPLE_CHIP_VARIANT}"
ai_ok "Apple Silicon detected"

# Docker engine (Docker Desktop, Colima, Rancher Desktop, OrbStack, or a
# forwarded socket are all acceptable — see lib/detection.sh).
test_docker_desktop
if ! $DOCKER_INSTALLED; then
    ai_err "Docker engine not found (no \`docker\` CLI on PATH)."
    ai_err "Pick one and install:"
    ai_err "  - Docker Desktop:  https://docs.docker.com/desktop/install/mac-install/"
    ai_err "  - Colima (CLI):    brew install colima docker docker-compose && colima start"
    ai_err "  - Rancher Desktop: https://rancherdesktop.io"
    ai_err "  - OrbStack:        https://orbstack.dev"
    exit 1
fi
ai_ok "Docker CLI found"

if ! $DOCKER_RUNNING; then
    ai_err "Docker daemon is not responding."
    case "${DOCKER_BACKEND:-unknown}" in
        desktop)  ai "Start Docker Desktop from /Applications or the menu bar, then re-run this installer." ;;
        colima)   ai "Run \`colima start\` (e.g. \`colima start --cpu 6 --memory 12 --disk 60\`) then re-run this installer." ;;
        rancher)  ai "Open Rancher Desktop and wait for the daemon to come up, then re-run this installer." ;;
        orbstack) ai "Open OrbStack and wait for the daemon to come up, then re-run this installer." ;;
        *)        ai "Start your docker daemon (Docker Desktop, Colima, Rancher Desktop, OrbStack, ...) and re-run this installer." ;;
    esac
    exit 1
fi
ai_ok "Docker daemon ready (v${DOCKER_VERSION}, backend=${DOCKER_BACKEND:-unknown})"

if ! _ensure_colima_private_network; then
    exit 1
fi

# Pre-flight the docker daemon's CPU allocation. Trip early with a clear
# message rather than after pulls/builds. Voice and the Full Stack share this
# floor: their CPU limits are capped to Docker's CPUs, so an 8-core Mac can
# run them (#3600). Voice is checked after the user picks features.
_require_docker_cpu_budget "${ODS_MIN_DOCKER_CPUS:-6}"

# Catch a common Colima-after-DockerDesktop config bomb: when a prior
# Docker Desktop install left `"credsStore": "desktop"` in ~/.docker/config.json
# and the user has since moved to Colima/Rancher/OrbStack, every `docker
# compose pull` and `docker compose up` will crash with `error getting
# credentials - err: exec: "docker-credential-desktop": executable file not
# found in $PATH`. We strip the stale entry rather than failing the
# install, because the helper is only meaningful with Docker Desktop.
if [[ "${DOCKER_BACKEND:-unknown}" != "desktop" ]] && test_stale_docker_creds_store; then
    ai_warn "Found stale \`credsStore: desktop\` in ~/.docker/config.json — incompatible with backend=${DOCKER_BACKEND:-unknown}."
    if $PREFLIGHT_ONLY && command -v python3 >/dev/null 2>&1; then
        ai "The installer will strip credsStore=desktop from ~/.docker/config.json"
    elif command -v python3 >/dev/null 2>&1; then
        python3 -c 'import json,sys,os
p=os.path.expanduser("~/.docker/config.json")
d=json.load(open(p))
d.pop("credsStore",None)
json.dump(d, open(p,"w"), indent=2)' && ai_ok "Stripped credsStore=desktop from ~/.docker/config.json"
    else
        ai_err "python3 not available — please remove the \`credsStore\` line from ~/.docker/config.json manually."
        exit 1
    fi
fi

# Filesystem POSIX-permission check
# The .env file (chmod 600) lives at INSTALL_DIR; a non-POSIX FS makes that
# a silent no-op and leaks secrets. Block install before any directory
# creation so the user can pick a different path.
test_install_dir_filesystem "$INSTALL_DIR"
info_box "Filesystem:" "${INSTALL_FS_TYPE}"
if $INSTALL_FS_FATAL; then
    ai_err "INSTALL_DIR (${INSTALL_DIR}) is on a ${INSTALL_FS_TYPE} filesystem."
    ai_err "ODS requires a POSIX-permission filesystem (apfs/hfs) so .env"
    ai_err "secrets can be locked down with chmod 600. ${INSTALL_FS_TYPE} silently"
    ai_err "ignores chmod/chown, leaving secrets world-readable."
    ai "Pick a path on your APFS system volume (e.g. ~/ods) and re-run."
    exit 1
fi
ai_ok "Filesystem supports POSIX permissions"

# Networked-filesystem advisory (warn-only).
# chmod 600 still applies on NFS/SMB/AFP, but the actual access control is
# enforced server-side by the share's ACL — other clients with access to the
# share may read .env regardless of local permissions.
if [[ "${INSTALL_FS_NETWORKED:-false}" == "true" ]]; then
    ai_warn "INSTALL_DIR ($INSTALL_DIR) is on a networked filesystem ($INSTALL_FS_TYPE)."
    ai_warn ".env permissions (chmod 600) are advisory — actual access control is governed by the share's ACL on the server."
    ai_warn "If this share is exposed to other clients, sensitive credentials may be readable from those hosts."
fi

# Docker Desktop file-sharing allowlist check
# Bind-mounts of paths outside the allowlist fail with cryptic OCI errors at
# `docker compose up`. Probe with a throwaway container so we surface a clear
# message before any compose work starts.
test_docker_desktop_sharing "$INSTALL_DIR"
if ! $DOCKER_SHARE_OK; then
    if [[ "$DOCKER_SHARE_REASON" == "denied" ]]; then
        ai_err "Docker Desktop cannot bind-mount ${INSTALL_DIR}."
        ai_err "Add the path to Docker Desktop > Settings > Resources > File Sharing,"
        ai_err "apply, then re-run this installer."
    fi
    ai_err "$DOCKER_SHARE_ERR"
    exit 1
fi
ai_ok "Docker Desktop file sharing OK"

# Disk space. A --preflight-only run also counts the space that removing the
# installation being replaced returns (0 for every other run).
test_disk_space "$INSTALL_DIR" 30 "$PREFLIGHT_RECLAIMABLE_GB"
if [[ "$DISK_RECLAIMABLE_GB" -gt 0 ]]; then
    info_box "Disk free:" "${DISK_FREE_GB} GB now, ${DISK_AVAILABLE_GB} GB once the existing installation is removed"
else
    info_box "Disk free:" "${DISK_FREE_GB} GB"
fi
if ! $DISK_SUFFICIENT; then
    ai_err "At least ${DISK_REQUIRED_GB} GB free space required. Found ${DISK_AVAILABLE_GB} GB."
    exit 1
fi
ai_ok "Disk space OK"

# PyYAML -- required by scripts/resolve-compose-stack.sh for the compose
# security scan (it parses every extension/overlay manifest before letting
# user composes through). Homebrew Python on macOS is externally managed, so
# `pip --user` can fail under PEP 668. Keep the resolver dependency in a
# ODS-owned venv and point shared Python helpers at that interpreter.
_ensure_macos_pyyaml

if $PREFLIGHT_ONLY; then
    ai_ok "Preflight passed; no changes were made."
    exit 0
fi

# Ollama conflict detection
check_ollama_conflict
if $OLLAMA_RUNNING; then
    ai_warn "Ollama is running (PID ${OLLAMA_PID}) and may conflict with ODS."
    ai "  Both use port 11434/8080. Ollama will shadow llama-server."
    if ! $NON_INTERACTIVE; then
        read -r -p "  Stop Ollama for this session? [Y/n] " ollama_choice < /dev/tty
        if [[ ! "$ollama_choice" =~ ^[nN] ]]; then
            kill "$OLLAMA_PID" 2>/dev/null || true
            sleep 2
            if pgrep -x ollama >/dev/null 2>&1; then
                ai_warn "Ollama restarted automatically. You may need to quit it from the menu bar."
            else
                ai_ok "Ollama stopped"
            fi
        else
            ai_warn "Ollama left running. Port conflicts may occur."
        fi
    else
        ai_warn "Ollama detected. Run without --non-interactive to resolve, or stop Ollama manually."
    fi
fi

# Port conflict checks — dynamically read from extension manifests
_conflict_ports=(8080 11434)  # llama-server (native) + Ollama default (host conflict, no manifest)
for _manifest in "${SOURCE_ROOT}/extensions/services/"*/manifest.yaml; do
    [[ -f "$_manifest" ]] || continue
    _port=$(grep 'external_port_default:' "$_manifest" 2>/dev/null | awk '{print $2}' | tr -d '"') || true
    # Skip port 0 — used by internal-only services (e.g. hermes is gated
    # behind hermes-proxy and exposes nothing) as a "no external port"
    # sentinel. Passing 0 to check_port_conflict trips lsof rows where the
    # local-port column reads 0 (identityservicesd does this on macOS) and
    # produces a confusing "Port 0 is in use" warning.
    if [[ -n "$_port" && "$_port" =~ ^[0-9]+$ && "$_port" -gt 0 && "$_port" -ne 8080 ]]; then
        _conflict_ports+=("$_port")
    fi
done

for port_check in "${_conflict_ports[@]}"; do
    if check_port_conflict "$port_check"; then
        ai_warn "Port ${port_check} is in use by ${PORT_CONFLICT_PROC} (PID ${PORT_CONFLICT_PID})"
    fi
done

# macOS AirPlay Receiver and other resident services commonly use port 9000.
# Auto-reassign Whisper to 9100 instead of shadowing an existing listener.
if check_port_conflict 9000 "existing listener"; then
    export WHISPER_PORT=9100
    ai_ok "Port 9000 in use by ${PORT_CONFLICT_PROC} -- Whisper reassigned to port ${WHISPER_PORT}"
fi

# ============================================================================
# PHASE 2 -- HARDWARE DETECTION
# ============================================================================
show_phase 2 6 "HARDWARE DETECTION" "10 seconds"

get_system_ram_gb

info_box "Chip:" "${APPLE_CHIP}"
info_box "Variant:" "${APPLE_CHIP_VARIANT}"
info_box "RAM:" "${SYSTEM_RAM_GB} GB (unified memory = effective VRAM)"
info_box "P-Cores:" "${APPLE_PERF_CORES}"
info_box "E-Cores:" "${APPLE_EFF_CORES}"
info_box "GPU Cores:" "${APPLE_GPU_CORES}"
info_box "Neural Engine:" "${APPLE_HAS_NEURAL_ENGINE}"
info_box "Backend:" "apple (Metal)"

# Auto-select tier (or use override)
if $CLOUD_MODE; then
    SELECTED_TIER="CLOUD"
elif [[ -n "$TIER_OVERRIDE" ]]; then
    SELECTED_TIER=$(echo "$TIER_OVERRIDE" | tr '[:lower:]' '[:upper:]')
    # Normalize T-prefix: T1 -> 1, T2 -> 2, etc.
    if [[ "$SELECTED_TIER" =~ ^T([0-9])$ ]]; then
        SELECTED_TIER="${BASH_REMATCH[1]}"
    fi
else
    SELECTED_TIER=$(auto_select_tier "$SYSTEM_RAM_GB" "$APPLE_CHIP_VARIANT")
fi

if [[ -z "${MODEL_PROFILE:-}" ]]; then
    if [[ -f "${INSTALL_DIR}/.env" ]]; then
        _existing_model_profile=$(grep -m1 '^MODEL_PROFILE=' "${INSTALL_DIR}/.env" 2>/dev/null | cut -d= -f2- | tr -d '\r' || true)
        MODEL_PROFILE="${_existing_model_profile:-qwen}"
    else
        MODEL_PROFILE="qwen"
    fi
fi

resolve_tier_config "$SELECTED_TIER"
if [[ "${ODS_DISABLE_CATALOG_MODEL_SELECTOR:-false}" != "true" && "$SELECTED_TIER" != "CLOUD" ]]; then
    _selector_script="${SOURCE_ROOT}/scripts/select-model.py"
    _selector_catalog="${SOURCE_ROOT}/config/model-library.json"
    if [[ -f "$_selector_script" && -f "$_selector_catalog" ]]; then
        _selector_python=""
        if [[ -f "${SOURCE_ROOT}/lib/python-cmd.sh" ]]; then
            # shellcheck source=/dev/null
            . "${SOURCE_ROOT}/lib/python-cmd.sh"
            _selector_python="$(ods_detect_python_cmd || true)"
        fi
        if [[ -z "$_selector_python" ]]; then
            if command -v python3 >/dev/null 2>&1; then
                _selector_python="python3"
            elif command -v python >/dev/null 2>&1; then
                _selector_python="python"
            fi
        fi
        if [[ -n "$_selector_python" ]]; then
            _selector_status=0
            # The tier map's size is a preference the Hermes re-select below
            # applies too (the selector drops it when nothing fits under it).
            _selector_max_size_mb="${LLM_MODEL_SIZE_MB:-0}"
            _selector_env="$("$_selector_python" "$_selector_script" \
                --catalog "$_selector_catalog" \
                --backend "apple" \
                --memory-type "unified" \
                --vram-mb "0" \
                --ram-gb "${SYSTEM_RAM_GB:-0}" \
                --profile "${MODEL_PROFILE_EFFECTIVE:-${MODEL_PROFILE:-qwen}}" \
                --tier "$SELECTED_TIER" \
                --max-size-mb "$_selector_max_size_mb" \
                --host-arch "$(uname -m 2>/dev/null || echo unknown)" \
                --installable-only \
                --min-context "$HERMES_CONTEXT_SIZE" \
                --env 2>>"$ODS_LOG_FILE")" || _selector_status=$?
            if [[ "$_selector_status" -eq 2 ]]; then
                ai_warn "No catalog model fits the detected memory and selected profile. Choose a smaller model profile or use cloud mode; refusing an unsafe tier-map fallback."
                exit 1
            fi
            if [[ -n "$_selector_env" ]]; then
                load_model_selector_env_from_output <<< "$_selector_env"
                ai "Model selector: ${MODEL_RECOMMENDATION_REASON:-$LLM_MODEL}"
            else
                ai "Model selector unavailable; using tier-map model ${LLM_MODEL}"
            fi
        fi
    fi
fi
if [[ -n "${LLAMA_CPP_RELEASE_TAG_OVERRIDE:-}" ]]; then
    LLAMA_CPP_RELEASE_TAG="$LLAMA_CPP_RELEASE_TAG_OVERRIDE"
    LLAMA_CPP_MACOS_ASSET="llama-${LLAMA_CPP_RELEASE_TAG}-bin-macos-arm64.tar.gz"
    LLAMA_CPP_MACOS_URL="https://github.com/ggml-org/llama.cpp/releases/download/${LLAMA_CPP_RELEASE_TAG}/${LLAMA_CPP_MACOS_ASSET}"
fi
ai_ok "Selected tier: ${SELECTED_TIER} (${TIER_NAME})"
info_box "Model:" "${LLM_MODEL}"
info_box "GGUF:" "${GGUF_FILE}"
info_box "Context:" "${MAX_CONTEXT}"

# Re-check disk space for model + Docker images. Prefer the catalog selector's
# exact model size when available; it can choose entries that do not fit the
# older tier-map filename heuristics.
_model_size_mb="${LLM_MODEL_SIZE_MB:-0}"
if [[ "$_model_size_mb" =~ ^[0-9]+$ && "$_model_size_mb" -gt 0 ]]; then
    _model_gb=$(( (_model_size_mb + 1023) / 1024 ))
    NEEDED_GB=$(( _model_gb + 15 ))
elif [[ "$GGUF_FILE" =~ 80B|Coder-Next ]]; then
    NEEDED_GB=65
elif [[ "$GGUF_FILE" =~ 31B ]]; then
    NEEDED_GB=38
elif [[ "$GGUF_FILE" =~ 30B|26B ]]; then
    NEEDED_GB=35
elif [[ "$GGUF_FILE" =~ 14B ]]; then
    NEEDED_GB=27
elif [[ "$GGUF_FILE" =~ E4B ]]; then
    NEEDED_GB=25
else
    NEEDED_GB=23
fi
test_disk_space "$INSTALL_DIR" "$NEEDED_GB"
if ! $DISK_SUFFICIENT; then
    ai_warn "Tier ${SELECTED_TIER} needs ~${NEEDED_GB} GB (model + Docker images). Only ${DISK_FREE_GB} GB free."
    if ! $FORCE; then exit 1; fi
fi

# ============================================================================
# PHASE 3 -- FEATURE SELECTION
# ============================================================================
show_phase 3 6 "FEATURES" "interactive"

if ! $NON_INTERACTIVE && ! $ALL_FEATURES && ! $DRY_RUN; then
    chapter "Select Features"
    ai "Choose your ODS configuration:"
    echo ""
    echo -e "  ${BGRN}[1]${NC} Full Stack   -- Everything enabled (voice, workflows, RAG, agents)"
    echo -e "  ${WHT}[2]${NC} Core Only    -- Chat + LLM inference (lean and fast)"
    echo -e "  ${WHT}[3]${NC} Custom       -- Choose individually"
    echo ""

    _macos_feature_default=2
    [[ -f "${INSTALL_DIR}/.env" ]] && _macos_feature_default=1
    read -r -p "  Selection (1/2/3) [${_macos_feature_default}]: " feature_choice < /dev/tty
    case "${feature_choice:-$_macos_feature_default}" in
        1)
            ENABLE_VOICE=true; ENABLE_WORKFLOWS=true
            ENABLE_RAG=true; ENABLE_HERMES=true
            ENABLE_RECOMMENDED=true
            if [[ -n "$feature_choice" ]] && ! $OPENCODE_DISABLE_EXPLICIT; then ENABLE_OPENCODE=true; fi
            ENABLE_APE=true
            ENABLE_PERPLEXICA=true
            ENABLE_PRIVACY_SHIELD=true
            ENABLE_LANGFUSE=true
            ENABLE_OPEN_WEBUI=true
            ;;
        2)
            ENABLE_VOICE=false; ENABLE_WORKFLOWS=false
            ENABLE_RAG=false; ENABLE_RECOMMENDED=false
            ENABLE_HERMES=false
            if ! $OPENCODE_ENABLE_EXPLICIT; then
                ENABLE_OPENCODE=false
                OPENCODE_DISABLE_SELECTED=true
            fi
            ENABLE_APE=false
            ENABLE_PERPLEXICA=false
            ENABLE_PRIVACY_SHIELD=false
            ENABLE_LANGFUSE=false
            ENABLE_OPEN_WEBUI=false
            ;;
        3)
            read -r -p "  Enable Voice (Whisper + Kokoro)? [y/N] " yn < /dev/tty
            [[ "$yn" =~ ^[yY] ]] && ENABLE_VOICE=true
            read -r -p "  Enable Workflows (n8n)?           [y/N] " yn < /dev/tty
            [[ "$yn" =~ ^[yY] ]] && ENABLE_WORKFLOWS=true
            read -r -p "  Enable RAG (Qdrant + embeddings)? [y/N] " yn < /dev/tty
            [[ "$yn" =~ ^[yY] ]] && ENABLE_RAG=true
            read -r -p "  Enable extra support (SearXNG + Token Spy)? [Y/n] " yn < /dev/tty
            [[ "$yn" =~ ^[nN] ]] && ENABLE_RECOMMENDED=false || ENABLE_RECOMMENDED=true
            _macos_ask_hermes
            if ! $OPENCODE_ENABLE_EXPLICIT && ! $OPENCODE_DISABLE_EXPLICIT; then
                read -r -p "  Enable OpenCode browser IDE? [y/N] " yn < /dev/tty
                if [[ "$yn" =~ ^[yY] ]]; then
                    ENABLE_OPENCODE=true
                else
                    ENABLE_OPENCODE=false
                    OPENCODE_DISABLE_SELECTED=true
                fi
            fi
            read -r -p "  Enable Perplexica deep research? [y/N] " yn < /dev/tty
            [[ "$yn" =~ ^[yY] ]] && ENABLE_PERPLEXICA=true
            read -r -p "  Add Open WebUI alongside Portal? [y/N] " yn < /dev/tty
            [[ "$yn" =~ ^[yY] ]] && ENABLE_OPEN_WEBUI=true || ENABLE_OPEN_WEBUI=false
            read -r -p "  Enable Privacy Shield? [y/N] " yn < /dev/tty
            [[ "$yn" =~ ^[yY] ]] && ENABLE_PRIVACY_SHIELD=true
            read -r -p "  Enable Langfuse (LLM observability, ~500MB)? [y/N] " yn < /dev/tty
            [[ "$yn" =~ ^[yY] ]] && ENABLE_LANGFUSE=true
            ;;
        *)
            ENABLE_VOICE=true; ENABLE_WORKFLOWS=true
            ENABLE_RAG=true; ENABLE_HERMES=true
            ENABLE_RECOMMENDED=true
            $OPENCODE_DISABLE_EXPLICIT || ENABLE_OPENCODE=true
            ENABLE_APE=true
            ENABLE_PERPLEXICA=true
            ENABLE_PRIVACY_SHIELD=true
            ENABLE_LANGFUSE=true
            ENABLE_OPEN_WEBUI=true
            ;;
    esac
    unset _macos_feature_default
fi

if [[ -z "${feature_choice:-}" && -n "$WEBUI_RETAINED" ]] \
    && ! $WEBUI_ENABLE_EXPLICIT && ! $WEBUI_DISABLE_EXPLICIT && ! $ALL_FEATURES; then
    ENABLE_OPEN_WEBUI="$WEBUI_RETAINED"
fi
$WEBUI_DISABLE_EXPLICIT && ENABLE_OPEN_WEBUI=false
$WEBUI_ENABLE_EXPLICIT && ENABLE_OPEN_WEBUI=true
if $ENABLE_ODS_PROXY && ! $ENABLE_OPEN_WEBUI; then
    ai_err "ODS proxy requires Open WebUI; choose --with-webui or omit --no-webui."
    exit 1
fi

$OPENCODE_DISABLE_EXPLICIT && ENABLE_OPENCODE=false
$OPENCODE_ENABLE_EXPLICIT && ENABLE_OPENCODE=true

if $ENABLE_PIXEL; then
    ENABLE_HERMES=false
fi
if ! $ENABLE_HERMES; then
    ENABLE_APE=false
fi

# SearXNG backs optional search consumers. Native Pixel defaults to its own
# keyless provider; a retained SearXNG choice remains authoritative.
_macos_resolve_support_services || exit 1

# Hermes needs 64K context; the raise grows the KV cache, so it is re-checked
# with the selector against the same unified-memory budget (see
# installers/phases/03-features.sh for the Linux flow): raise when the pick
# fits at 64K, otherwise re-select a model that does, otherwise keep the
# largest context that fits and say ODS Talk is unavailable.
HERMES_CONTEXT_BELOW_FLOOR=false
if $ENABLE_HERMES && ! $CLOUD_MODE; then
    if [[ "${MAX_CONTEXT:-0}" =~ ^[0-9]+$ ]] && (( MAX_CONTEXT < HERMES_CONTEXT_SIZE )); then
        _hermes_floor_action="raise-unverified"
        if [[ -n "${_selector_python:-}" && -f "${_selector_script:-}" && -f "${_selector_catalog:-}" \
            && "${ODS_DISABLE_CATALOG_MODEL_SELECTOR:-false}" != "true" ]]; then
            _hermes_selector_args=(
                --catalog "$_selector_catalog"
                --backend "apple"
                --memory-type "unified"
                --vram-mb "0"
                --ram-gb "${SYSTEM_RAM_GB:-0}"
                --profile "${MODEL_PROFILE_EFFECTIVE:-${MODEL_PROFILE:-qwen}}"
                --tier "$SELECTED_TIER"
                --host-arch "$(uname -m 2>/dev/null || echo unknown)"
            )
            _hermes_profile_args=()
            [[ -n "${MODEL_RUNTIME_PROFILE:-}" ]] && _hermes_profile_args=(--runtime-profile "$MODEL_RUNTIME_PROFILE")
            _hermes_fit_status=0
            "$_selector_python" "$_selector_script" "${_hermes_selector_args[@]}" \
                --check-fit --model-id "${GGUF_FILE:-${LLM_MODEL:-}}" --context "$HERMES_CONTEXT_SIZE" \
                ${_hermes_profile_args[@]+"${_hermes_profile_args[@]}"} \
                >>"$ODS_LOG_FILE" 2>&1 || _hermes_fit_status=$?
            case "$_hermes_fit_status" in
                0) _hermes_floor_action="raise" ;;
                3)
                    _hermes_env="$("$_selector_python" "$_selector_script" "${_hermes_selector_args[@]}" \
                        --max-size-mb "${_selector_max_size_mb:-0}" --installable-only \
                        --min-context "$HERMES_CONTEXT_SIZE" --require-min-context \
                        --env 2>>"$ODS_LOG_FILE")" || _hermes_env=""
                    if [[ -n "$_hermes_env" ]]; then
                        _hermes_previous="${LLM_MODEL:-} at ${MAX_CONTEXT}"
                        # Drop the previous pick's llama-server settings (the
                        # LLAMA_ARG_* set installers/phases/03-features.sh
                        # clears); the loader omits unset optional values.
                        unset MODEL_RUNTIME_PROFILE MODEL_RUNTIME_PROFILE_LABEL MODEL_RUNTIME_PROFILE_SOURCE
                        unset LLAMA_ARG_CACHE_TYPE_K LLAMA_ARG_CACHE_TYPE_V LLAMA_ARG_FLASH_ATTN
                        unset LLAMA_ARG_N_CPU_MOE LLAMA_ARG_NO_CACHE_PROMPT LLAMA_ARG_CHECKPOINT_EVERY_NT
                        unset LLAMA_ARG_SPEC_TYPE LLAMA_ARG_SPEC_DRAFT_N_MAX
                        unset LLAMA_ARG_CTX_CHECKPOINTS LLAMA_ARG_CACHE_RAM
                        load_model_selector_env_from_output <<< "$_hermes_env"
                        ai_warn "Hermes needs 64K context: ${_hermes_previous} cannot serve 64K here, so ${LLM_MODEL} was selected at ${MAX_CONTEXT}."
                        _hermes_floor_action="reselected"
                        unset _hermes_previous
                    else
                        _hermes_floor_action="cap"
                    fi
                    unset _hermes_env
                    ;;
            esac
            unset _hermes_selector_args _hermes_profile_args _hermes_fit_status
        fi
        case "$_hermes_floor_action" in
            raise|raise-unverified)
                ai_warn "Hermes enabled: increasing macOS llama context from ${MAX_CONTEXT} to ${HERMES_CONTEXT_SIZE} (64K floor)."
                if [[ -n "${MODEL_RECOMMENDATION_REASON:-}" ]]; then
                    MODEL_RECOMMENDATION_REASON="${MODEL_RECOMMENDATION_REASON} Hermes requires at least 64K context, so runtime context was raised to ${HERMES_CONTEXT_SIZE}."
                fi
                MAX_CONTEXT="$HERMES_CONTEXT_SIZE"
                ;;
            cap)
                HERMES_CONTEXT_BELOW_FLOOR=true
                ai_warn "Hermes needs at least 64K context, but ${LLM_MODEL:-this model} runs at ${MAX_CONTEXT} in this Mac's memory budget (64K does not fit or exceeds its native context)."
                ai_warn "ODS Talk stays unavailable (the Dashboard says why) until you choose a model that fits 64K in Models."
                MODEL_RECOMMENDATION_REASON="${MODEL_RECOMMENDATION_REASON:-} Hermes requires 64K context, which does not fit here; ODS Talk is unavailable with this model."
                ;;
        esac
        unset _hermes_floor_action
    fi
fi

ai "Features:"
info_box "  Voice:" "$(if $ENABLE_VOICE; then echo enabled; else echo disabled; fi)"
info_box "  Workflows:" "$(if $ENABLE_WORKFLOWS; then echo enabled; else echo disabled; fi)"
info_box "  RAG:" "$(if $ENABLE_RAG; then echo enabled; else echo disabled; fi)"
info_box "  SearXNG search:" "$(if $ENABLE_SEARXNG; then echo enabled; else echo disabled; fi)"
info_box "  Token Spy:" "$(if $ENABLE_RECOMMENDED; then echo enabled; else echo disabled; fi)"
info_box "  LiteLLM gateway:" "$(if $ENABLE_LITELLM; then echo enabled; else echo disabled; fi)"
info_box "  Hermes:" "$(if $ENABLE_HERMES; then echo enabled; elif $ENABLE_PIXEL; then echo 'disabled (Portal is the agent)'; else echo disabled; fi)"
info_box "  Portal (native):" "$(if $ENABLE_PIXEL; then echo enabled; else echo disabled; fi)"
info_box "  OpenCode:" "$(if $ENABLE_OPENCODE; then echo enabled; else echo disabled; fi)"
info_box "  Perplexica:" "$(if $ENABLE_PERPLEXICA; then echo enabled; else echo disabled; fi)"
info_box "  Privacy Shield:" "$(if $ENABLE_PRIVACY_SHIELD; then echo enabled; else echo disabled; fi)"
info_box "  Langfuse:" "$(if $ENABLE_LANGFUSE; then echo enabled; else echo disabled; fi)"
# The macOS installer doesn't currently ship a ComfyUI container — none of
# the published ComfyUI images target Apple Silicon Metal, and the upstream
# Python build under MPS is non-trivial to package as a Docker service.
# Surface this to operators who passed --all so they aren't left wondering
# why the dashboard shows no image-gen tile after install.
info_box "  ComfyUI:" "not available on macOS (no MPS Docker image upstream)"

if $ENABLE_VOICE; then
    _warn_voice_cpu_budget
fi

# ============================================================================
# PHASE 4 -- SETUP (directories, copy source, generate .env)
# ============================================================================
show_phase 4 6 "SETUP" "1-2 minutes"

if $DRY_RUN; then
    ai "[DRY RUN] Would create: ${INSTALL_DIR}"
    ai "[DRY RUN] Would copy source files"
    ai "[DRY RUN] Would generate .env with secrets"
    ai "[DRY RUN] Would generate SearXNG config"
    $ENABLE_HERMES && ai "[DRY RUN] Would configure Hermes Agent (data: ${INSTALL_DIR}/data/hermes)"
    $ENABLE_LANGFUSE && ai "[DRY RUN] Would enable Langfuse (LLM observability)"
    $ENABLE_OPENCODE && ai "[DRY RUN] Would install and start OpenCode"
    if ! $ENABLE_OPENCODE; then
        ai "[DRY RUN] Would skip OpenCode download and LaunchAgent"
        if $OPENCODE_DISABLE_EXPLICIT || $OPENCODE_DISABLE_SELECTED; then
            ai "[DRY RUN] Would disable future ODS OpenCode login starts when owned"
        fi
    fi
else
    # Create directory structure
    mkdir -p "${INSTALL_DIR}/config/searxng"
    mkdir -p "${INSTALL_DIR}/config/n8n"
    mkdir -p "${INSTALL_DIR}/config/litellm"
    mkdir -p "${INSTALL_DIR}/config/llama-server"
    mkdir -p "${INSTALL_DIR}/data/open-webui"
    mkdir -p "${INSTALL_DIR}/data/whisper"
    mkdir -p "${INSTALL_DIR}/data/tts"
    mkdir -p "${INSTALL_DIR}/data/n8n"
    mkdir -p "${INSTALL_DIR}/data/qdrant"
    mkdir -p "${INSTALL_DIR}/data/models"
    mkdir -p "${INSTALL_DIR}/data/privacy-shield"
    mkdir -p "${INSTALL_DIR}/data/ape"
    mkdir -p "${INSTALL_DIR}/data/token-spy"
    mkdir -p "${INSTALL_DIR}/data/hermes"
    mkdir -p "${INSTALL_DIR}/data/hermes-proxy/caddy-data"
    mkdir -p "${INSTALL_DIR}/data/hermes-proxy/caddy-config"
    mkdir -p "${INSTALL_DIR}/data/langfuse/postgres"
    mkdir -p "${INSTALL_DIR}/data/langfuse/clickhouse"
    mkdir -p "${INSTALL_DIR}/data/langfuse/redis"
    mkdir -p "${INSTALL_DIR}/data/langfuse/minio"
    mkdir -p "${INSTALL_DIR}/data/remote-provider/secrets"
    mkdir -p "${INSTALL_DIR}/bin"
    ai_ok "Created directory structure"

    # Copy source tree (skip .git, data, logs, .env, models)
    if [[ "$SOURCE_ROOT" != "$INSTALL_DIR" ]]; then
        ai "Copying source files to ${INSTALL_DIR}..."
        _ods_prune_stale_dev_paths=false
        if [[ -f "${INSTALL_DIR}/.env" ]] \
            && [[ -f "${INSTALL_DIR}/manifest.json" ]] \
            && [[ -f "${INSTALL_DIR}/docker-compose.base.yml" ]]; then
            _ods_prune_stale_dev_paths=true
        fi
        _ods_dev_only_dirs=(tests docs examples .github)
        _ods_dev_only_files=(
            CHANGELOG.md
            CODE_OF_CONDUCT.md
            CONTRIBUTING.md
            EDGE-QUICKSTART.md
            FAQ.md
            QUICKSTART.md
            SECURITY.md
            README.md
            .shellcheckrc
            PSScriptAnalyzerSettings.psd1
            test-stack.sh
            .gitignore
        )
        _ods_dev_rsync_excludes=()
        for _ods_dev_path in "${_ods_dev_only_dirs[@]}"; do
            _ods_dev_rsync_excludes+=(--exclude="/${_ods_dev_path}/")
        done
        for _ods_dev_path in "${_ods_dev_only_files[@]}"; do
            _ods_dev_rsync_excludes+=(--exclude="/${_ods_dev_path}")
        done
        rsync -a --quiet \
            --exclude='.git' \
            --exclude='data' \
            --exclude='logs' \
            --exclude='models' \
            --exclude='node_modules' \
            --exclude='dist' \
            --exclude='.env' \
            --exclude='*.log' \
            --exclude='.current-mode' \
            --exclude='.profiles' \
            --exclude='.target-model' \
            --exclude='.target-quantization' \
            --exclude='.offline-mode' \
            "${_ods_dev_rsync_excludes[@]}" \
            "$SOURCE_ROOT/" "$INSTALL_DIR/"

        # Excludes leave files copied by an older installer in place. Move
        # managed-upgrade leftovers to a recoverable backup instead of deleting
        # possible user modifications. Unmanaged targets remain untouched.
        if $_ods_prune_stale_dev_paths; then
            _ods_dev_backup="$(
                ods_quarantine_development_paths \
                    "$INSTALL_DIR" \
                    "${_ods_dev_only_dirs[@]}" \
                    "${_ods_dev_only_files[@]}"
            )"
            if [[ -n "$_ods_dev_backup" ]]; then
                ai_warn "Older development files were moved to ${_ods_dev_backup}"
            fi
        fi
        unset _ods_prune_stale_dev_paths _ods_dev_only_dirs _ods_dev_only_files _ods_dev_rsync_excludes _ods_dev_path _ods_dev_backup
        ai_ok "Source files installed"
    else
        ai "Running in-place, skipping file copy"
    fi

    # Retired from the shipped stack after Hermes became the default agent
    # surface. Remove stale service files left behind by non-pruning upgrades,
    # while preserving data/odsforge for user-controlled archival.
    if [[ -d "${INSTALL_DIR}/extensions/services/odsforge" ]]; then
        rm -rf "${INSTALL_DIR}/extensions/services/odsforge"
        log "Removed retired ODSForge service files from extensions/services"
    fi
    # The legacy OpenClaw extension (the ods-openclaw container) was removed;
    # Portal (Pixel) and Hermes are the supported agents. Remove its stale
    # service files the same way, so `up --remove-orphans` below drops the old
    # container.
    if [[ -d "${INSTALL_DIR}/extensions/services/openclaw" ]]; then
        rm -rf "${INSTALL_DIR}/extensions/services/openclaw"
        log "Removed retired OpenClaw service files from extensions/services"
    fi
    # Every release copied the OpenClaw templates into config/openclaw, used
    # or not. A template that is still byte-identical to a shipped version is
    # not owner data; anything else, and data/openclaw, stays.
    _macos_file_sha256() {
        if command -v shasum >/dev/null 2>&1; then
            shasum -a 256 "$1"
        else
            sha256sum "$1"
        fi | cut -d ' ' -f 1
    }
    _macos_openclaw_config="${INSTALL_DIR}/config/openclaw"
    _macos_openclaw_data="${INSTALL_DIR}/data/openclaw"
    _macos_openclaw_manifest="${SOURCE_ROOT}/installers/lib/retired-openclaw-config.sha256"
    if [[ -d "$_macos_openclaw_config" && ! -L "$_macos_openclaw_config" && -f "$_macos_openclaw_manifest" ]]; then
        while read -r _macos_digest _macos_relative; do
            # A source tree copied from a Windows checkout has CRLF lines.
            _macos_relative="${_macos_relative%$'\r'}"
            [[ -n "$_macos_digest" && "$_macos_digest" != \#* && "$_macos_relative" != *..* ]] || continue
            _macos_path="${_macos_openclaw_config}/${_macos_relative}"
            # Never reach a template through a linked folder.
            [[ "$_macos_relative" != */* || ! -L "${_macos_openclaw_config}/${_macos_relative%/*}" ]] || continue
            [[ -f "$_macos_path" && ! -L "$_macos_path" ]] || continue
            # An unreadable file has no digest, so it is kept.
            [[ "$(_macos_file_sha256 "$_macos_path" 2>/dev/null)" == "$_macos_digest" ]] || continue
            rm -f "$_macos_path" || log "Could not remove the unchanged OpenClaw template ${_macos_path} (non-fatal)"
        done < "$_macos_openclaw_manifest"
        for _macos_path in "${_macos_openclaw_config}/workspace" "$_macos_openclaw_config"; do
            if [[ -d "$_macos_path" && ! -L "$_macos_path" && -r "$_macos_path" && -x "$_macos_path" && -z "$(ls -A "$_macos_path")" ]]; then
                rmdir "$_macos_path" || log "Could not remove the empty folder ${_macos_path} (non-fatal)"
            fi
        done
    fi
    _macos_openclaw_folders=""
    if [[ -e "$_macos_openclaw_config" || -L "$_macos_openclaw_config" ]]; then
        _macos_openclaw_folders="config/openclaw"
    fi
    # An unreadable data folder may still hold the agent's state.
    if [[ -d "$_macos_openclaw_data" ]]; then
        if [[ ! -r "$_macos_openclaw_data" || ! -x "$_macos_openclaw_data" ]] || [[ -n "$(ls -A "$_macos_openclaw_data")" ]]; then
            _macos_openclaw_folders="${_macos_openclaw_folders:+$_macos_openclaw_folders and }data/openclaw"
        fi
    fi
    if [[ -n "$_macos_openclaw_folders" ]]; then
        ai "The legacy OpenClaw extension was removed. Its remaining files in ${_macos_openclaw_folders} were kept; delete them by hand when you no longer need them (docs/MIGRATION-OPENCLAW-TO-HERMES.md explains how)."
    fi
    unset _macos_openclaw_config _macos_openclaw_data _macos_openclaw_manifest _macos_openclaw_folders \
        _macos_digest _macos_relative _macos_path
    unset -f _macos_file_sha256

    # Copy extensions library to data dir for dashboard portal.
    # Source resolution: dev installs and full checkouts read the product-owned
    # library under extensions/library/. Bootstrap installs also get the same
    # templates bundled by get-ods.sh under extensions-library-bundle/.
    _ext_lib_src=""
    for _candidate in \
        "${SOURCE_ROOT}/extensions/library/services" \
        "${INSTALL_DIR}/extensions/library/services" \
        "${INSTALL_DIR}/extensions-library-bundle/services"
    do
        if [[ -d "$_candidate" ]]; then _ext_lib_src="$_candidate"; break; fi
    done
    if [[ -n "$_ext_lib_src" ]]; then
        mkdir -p "${INSTALL_DIR}/data/extensions-library"
        cp -r "$_ext_lib_src/." "${INSTALL_DIR}/data/extensions-library/"
        ai_ok "Extensions library copied to data/extensions-library/ (from $_ext_lib_src)"
    else
        ai_warn "Extensions library not found; dashboard Extensions page will return 503 until populated"
    fi

    # Copy CLI tool to install root
    if [[ -f "${SCRIPT_DIR}/ods-macos.sh" ]]; then
        cp "${SCRIPT_DIR}/ods-macos.sh" "${INSTALL_DIR}/ods-macos.sh"
        chmod +x "${INSTALL_DIR}/ods-macos.sh"
        # Also copy the lib/ directory ods-macos.sh needs
        mkdir -p "${INSTALL_DIR}/lib"
        cp "${SCRIPT_DIR}/lib/"*.sh "${INSTALL_DIR}/lib/"
        ai_ok "Installed ods-macos.sh CLI"
    fi

    # The resolver treats compose.yaml as enabled and compose.yaml.disabled as
    # disabled. Persist every installer feature choice in that canonical form
    # so cache invalidation and later dashboard toggles cannot resurrect the
    # installer's unselected built-ins.
    _macos_sync_builtin_compose_states

    # A detached bootstrap worker can rewrite GGUF_FILE/LLM_MODEL after its
    # download finishes. Stop and disable it before cloud mode or a forced
    # fresh install touches .env so the values below are authoritative.
    if $CLOUD_MODE; then
        if ! _macos_cancel_detached_bootstrap_upgrade "cloud_mode" "cloud transition"; then
            exit 1
        fi
    elif $FORCE && ! _macos_cancel_detached_bootstrap_upgrade "fresh_install" "fresh install"; then
        exit 1
    fi

    # Generate .env (idempotent unless --force)
    env_existed=false
    [[ -f "${INSTALL_DIR}/.env" ]] && env_existed=true
    _previous_ods_mode="$(read_env_value "${INSTALL_DIR}/.env" "ODS_MODE")"
    _previous_llm_bind="$(read_env_value "${INSTALL_DIR}/.env" "BIND_ADDRESS")"
    _previous_macos_gateway="$(read_env_value "${INSTALL_DIR}/.env" "ODS_MACOS_HOST_GATEWAY")"
    generate_ods_env "$INSTALL_DIR" "$SELECTED_TIER" "$FORCE"
    # generate_ods_env preserves existing .env without --force. Persist an
    # explicit addback or opt-out there too, so cache rebuilds keep the choice.
    upsert_env_value "${INSTALL_DIR}/.env" "ENABLE_OPEN_WEBUI" "$ENABLE_OPEN_WEBUI"
    # Pixel can stop before OpenCode setup; retain the choice for continuation.
    upsert_env_value "${INSTALL_DIR}/.env" "ENABLE_OPENCODE" "$ENABLE_OPENCODE"
    # Reinstalls preserve .env, including an earlier AirPlay port remap.
    # Use that same port for Compose, model downloads and readiness checks.
    WHISPER_PORT="$(read_env_value "$INSTALL_DIR/.env" "WHISPER_PORT")"
    WHISPER_PORT="${WHISPER_PORT//\"/}"
    WHISPER_PORT="${WHISPER_PORT//\'/}"
    export WHISPER_PORT="${WHISPER_PORT:-9000}"
    _MACOS_EXTERNAL_MODEL_READY=false
    _macos_active_store="$(read_env_value "${INSTALL_DIR}/.env" "ODS_ACTIVE_MODEL_STORE")"
    _macos_active_store="${_macos_active_store//\"/}"
    _macos_active_store="${_macos_active_store//\'/}"
    if ! $CLOUD_MODE && [[ "$_previous_ods_mode" != cloud && -n "$_macos_active_store" && "$_macos_active_store" != default ]]; then
        # A retained SSD selection owns the runtime contract, not this tier's
        # recommendation. Verify it before any native listener is replaced.
        macos_resolve_native_model "$INSTALL_DIR" "$LLAMA_SERVER_BIN" \
            "$(read_env_value "${INSTALL_DIR}/.env" "CTX_SIZE")" true || exit 1
        _MACOS_EXTERNAL_MODEL_READY=true
        GGUF_FILE="$(basename "$MACOS_NATIVE_MODEL_PATH")"
        LLM_MODEL="$(read_env_value "${INSTALL_DIR}/.env" "LLM_MODEL")"
        LLM_MODEL="${LLM_MODEL:-$GGUF_FILE}"
        MAX_CONTEXT="${MACOS_NATIVE_CONTEXT:-65536}"
        GGUF_URL=""
    fi
    _macos_switchboard_mode="$(read_env_value "${INSTALL_DIR}/.env" "ODS_MODEL_SWITCHBOARD")"
    case "${_macos_switchboard_mode:-enabled}" in
        legacy|observe|enabled) ;;
        *) _macos_switchboard_mode="enabled" ;;
    esac
    upsert_env_value "${INSTALL_DIR}/.env" "ODS_MODEL_SWITCHBOARD" "$_macos_switchboard_mode"
    _macos_agent_bind_raw="$(read_env_value "${INSTALL_DIR}/.env" "ODS_AGENT_BIND")"
    _macos_agent_bind="$(macos_normalize_agent_bind "${_macos_agent_bind_raw:-127.0.0.1}")"
    if [[ -n "$_macos_agent_bind_raw" && "$_macos_agent_bind" != "$_macos_agent_bind_raw" ]]; then
        ai_warn "ODS_AGENT_BIND=${_macos_agent_bind_raw} uses an unsupported IPv6 server socket; using ${_macos_agent_bind}"
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_AGENT_BIND" "$_macos_agent_bind"
    fi
    # Persist the interpreter that proved it can import yaml. _ensure_macos_pyyaml
    # may have satisfied the compose resolver through an ODS-owned venv, but that
    # choice only lives in this installer process. Without recording it, a later
    # `ods enable` / `ods start` resolves python again, falls back to a host
    # python3 that cannot import yaml, and scripts/resolve-compose-stack.sh fails
    # after the install already reported success. lib/safe-env.sh exports .env
    # keys, so the CLI and the resolver subprocess both pick this up.
    if [[ -n "${ODS_PYTHON_CMD:-}" ]] && ! _macos_python_imports_yaml python3; then
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_PYTHON_CMD" "$ODS_PYTHON_CMD"
        ai_ok "Recorded compose-resolver Python: ${ODS_PYTHON_CMD}"
    fi

    _macos_llm_bridge_enabled="false"
    if [[ "${DOCKER_BACKEND:-unknown}" == "colima" ]]; then
        _macos_llm_bind="127.0.0.1"
        [[ -n "$_macos_llm_bind" ]] || _macos_llm_bind="127.0.0.1"
        _macos_llm_bridge_enabled="true"
        _macos_agent_bridge_enabled="true"
        macos_bind_uses_direct_gateway "$_macos_llm_bind" "$COLIMA_HOST_IP" && _macos_llm_bridge_enabled="false"
        macos_bind_uses_direct_gateway "$_macos_agent_bind" "$COLIMA_HOST_IP" && _macos_agent_bridge_enabled="false"
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_MACOS_HOST_GATEWAY" "$COLIMA_HOST_IP"
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_MACOS_VM_IP" "$COLIMA_VM_IP"
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_MACOS_HOST_AGENT_BRIDGE_ENABLED" "$_macos_agent_bridge_enabled"
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_AGENT_HOST" "$COLIMA_HOST_IP"
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_MACOS_LLM_BRIDGE_ENABLED" "$_macos_llm_bridge_enabled"
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_NATIVE_LLAMA_PORT" "${ODS_NATIVE_LLAMA_PORT:-8080}"
        unset _macos_llm_bind _macos_agent_bridge_enabled
    else
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_MACOS_HOST_AGENT_BRIDGE_ENABLED" "false"
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_MACOS_LLM_BRIDGE_ENABLED" "false"
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_AGENT_HOST" "host.docker.internal"
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_MACOS_HOST_GATEWAY" ""
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_MACOS_VM_IP" ""
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_NATIVE_LLAMA_PORT" "${ODS_NATIVE_LLAMA_PORT:-8080}"
    fi
    if $CLOUD_MODE; then
        _macos_litellm_key="$(read_env_value "${INSTALL_DIR}/.env" "LITELLM_KEY")"
        if [[ -z "$_macos_litellm_key" ]]; then
            ai_err "Cloud mode requires the generated LiteLLM master key, but LITELLM_KEY is empty."
            exit 1
        fi
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_MODE" "cloud"
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_MACOS_LLM_BRIDGE_ENABLED" "false"
        _macos_llm_bridge_enabled="false"
        upsert_env_value "${INSTALL_DIR}/.env" "LLM_API_URL" "http://litellm:4000"
        upsert_env_value "${INSTALL_DIR}/.env" "HERMES_LLM_BASE_URL" "http://litellm:4000/v1"
        upsert_env_value "${INSTALL_DIR}/.env" "HERMES_LLM_API_KEY" "$_macos_litellm_key"
        upsert_env_value "${INSTALL_DIR}/.env" "OPEN_WEBUI_LLM_BASE_URL" "http://litellm:4000"
        upsert_env_value "${INSTALL_DIR}/.env" "OPEN_WEBUI_LLM_API_KEY" "$_macos_litellm_key"
        upsert_env_value "${INSTALL_DIR}/.env" "LLM_MODEL" "$LLM_MODEL"
        upsert_env_value "${INSTALL_DIR}/.env" "GGUF_FILE" ""
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_ACTIVE_MODEL_STORE" "default"
        upsert_env_value "${INSTALL_DIR}/.env" "MAX_CONTEXT" "$MAX_CONTEXT"
        upsert_env_value "${INSTALL_DIR}/.env" "CTX_SIZE" "$MAX_CONTEXT"
    else
        upsert_env_value "${INSTALL_DIR}/.env" "ODS_MODE" "local"
        upsert_env_value "${INSTALL_DIR}/.env" "LLM_BACKEND" "llama-server"
        upsert_env_value "${INSTALL_DIR}/.env" "HERMES_LLM_API_KEY" "sk-ods-hermes-local"
        if [[ "$_previous_ods_mode" == "cloud" ]]; then
            upsert_env_value "${INSTALL_DIR}/.env" "ODS_ACTIVE_MODEL_STORE" "default"
            upsert_env_value "${INSTALL_DIR}/.env" "LLM_MODEL" "$LLM_MODEL"
            upsert_env_value "${INSTALL_DIR}/.env" "GGUF_FILE" "$GGUF_FILE"
            upsert_env_value "${INSTALL_DIR}/.env" "MAX_CONTEXT" "$MAX_CONTEXT"
            upsert_env_value "${INSTALL_DIR}/.env" "CTX_SIZE" "$MAX_CONTEXT"
        fi
        if [[ "${DOCKER_BACKEND:-unknown}" == "colima" ]]; then
            upsert_env_value "${INSTALL_DIR}/.env" "LLM_API_URL" "http://${COLIMA_HOST_IP}:${ODS_NATIVE_LLAMA_PORT:-8080}"
            upsert_env_value "${INSTALL_DIR}/.env" "HERMES_LLM_BASE_URL" "http://${COLIMA_HOST_IP}:${ODS_NATIVE_LLAMA_PORT:-8080}/v1"
        else
            upsert_env_value "${INSTALL_DIR}/.env" "LLM_API_URL" "http://host.docker.internal:${ODS_NATIVE_LLAMA_PORT:-8080}"
            upsert_env_value "${INSTALL_DIR}/.env" "HERMES_LLM_BASE_URL" "http://host.docker.internal:${ODS_NATIVE_LLAMA_PORT:-8080}/v1"
        fi
        if [[ "$_macos_switchboard_mode" == "enabled" ]]; then
            _macos_litellm_key="$(read_env_value "${INSTALL_DIR}/.env" "LITELLM_KEY")"
            if [[ -z "$_macos_litellm_key" ]]; then
                ai_err "Switchboard mode requires the generated LiteLLM master key, but LITELLM_KEY is empty."
                exit 1
            fi
            upsert_env_value "${INSTALL_DIR}/.env" "HERMES_LLM_BASE_URL" "http://model-router:9099/v1"
            upsert_env_value "${INSTALL_DIR}/.env" "HERMES_LLM_API_KEY" "no-key"
            upsert_env_value "${INSTALL_DIR}/.env" "OPEN_WEBUI_LLM_BASE_URL" "http://litellm:4000"
            upsert_env_value "${INSTALL_DIR}/.env" "OPEN_WEBUI_LLM_API_KEY" "$_macos_litellm_key"
        fi
    fi
    if [[ "${DOCKER_BACKEND:-unknown}" == "colima" ]] \
       && [[ "$_macos_llm_bridge_enabled" == "true" ]] \
       && macos_bind_uses_direct_gateway "${_previous_llm_bind:-127.0.0.1}" "$_previous_macos_gateway"; then
        _macos_stop_install_owned_native_llama \
            "Stopping the old direct native listener before recreating the loopback Colima bridge..."
    fi
    if ! _configure_macos_llm_bridge; then
        exit 1
    fi
    unset _macos_agent_bind _macos_agent_bind_raw _previous_ods_mode \
        _previous_llm_bind _previous_macos_gateway _macos_llm_bridge_enabled \
        _macos_litellm_key
    CONTAINER_LLM_URL="$(read_env_value "${INSTALL_DIR}/.env" "LLM_API_URL")"
    [[ -n "$CONTAINER_LLM_URL" ]] || CONTAINER_LLM_URL="http://host.docker.internal:${ODS_NATIVE_LLAMA_PORT:-8080}"
    _macos_runtime_renderer="${ODS_PYTHON_CMD:-python3}"
    _macos_renderer_key="$(read_env_value "${INSTALL_DIR}/.env" "LITELLM_KEY")"
    if [[ ! -f "${INSTALL_DIR}/scripts/render-runtime-configs.py" ]] \
        || ! command -v "$_macos_runtime_renderer" >/dev/null 2>&1; then
        ai_err "Model router config renderer is unavailable"
        exit 1
    fi
    _macos_router_args=(
        --switchboard-mode "$_macos_switchboard_mode"
        --ods-mode "$(read_env_value "${INSTALL_DIR}/.env" "ODS_MODE")"
        --gpu-backend "apple"
        --model "${LLM_MODEL:-}"
        --gguf-file "${GGUF_FILE:-}"
        --llm-base-url "${CONTAINER_LLM_URL}"
        --context-length "${MAX_CONTEXT:-65536}"
        --output-root "$INSTALL_DIR"
        --write
    )
    for _macos_router_surface in model-router-endpoints; do
        if ! ODS_RENDER_LITELLM_KEY="$_macos_renderer_key" \
            "$_macos_runtime_renderer" "${INSTALL_DIR}/scripts/render-runtime-configs.py" \
            --surface "$_macos_router_surface" "${_macos_router_args[@]}" >> "$ODS_LOG_FILE" 2>&1; then
            ai_err "Failed to render required ${_macos_router_surface} config"
            exit 1
        fi
    done
    if [[ "$_macos_switchboard_mode" == "enabled" ]] \
       && [[ "$(read_env_value "${INSTALL_DIR}/.env" "ODS_MODE")" != "cloud" ]] \
       && ! ODS_RENDER_LITELLM_KEY="$_macos_renderer_key" \
            "$_macos_runtime_renderer" "${INSTALL_DIR}/scripts/render-runtime-configs.py" \
            --surface litellm-switchboard "${_macos_router_args[@]}" >> "$ODS_LOG_FILE" 2>&1; then
        ai_err "Failed to render required litellm-switchboard config"
        exit 1
    fi
    unset _macos_runtime_renderer _macos_renderer_key _macos_router_args _macos_router_surface
    if $env_existed && ! $FORCE; then
        ai_ok "Preserved existing .env (use --force to regenerate secrets)"
    else
        ai_ok "Generated .env with secure secrets"
    fi
    if $ENABLE_HERMES && ! $CLOUD_MODE; then
        upsert_env_value "${INSTALL_DIR}/.env" "MAX_CONTEXT" "$MAX_CONTEXT"
        upsert_env_value "${INSTALL_DIR}/.env" "CTX_SIZE" "$MAX_CONTEXT"
        ai_ok "Set macOS llama context to ${MAX_CONTEXT} for Hermes"
    fi

    # Generate SearXNG config
    searx_existed=false
    [[ -f "${INSTALL_DIR}/config/searxng/settings.yml" ]] && searx_existed=true
    generate_searxng_config "$INSTALL_DIR" "$ENV_SEARXNG_SECRET" "$FORCE"
    if $searx_existed && ! $FORCE; then
        ai_ok "Preserved existing SearXNG config (use --force to regenerate)"
    else
        ai_ok "Generated SearXNG config"
    fi

    # Create llama-server models.ini (empty -- populated later)
    local_models_ini="${INSTALL_DIR}/config/llama-server/models.ini"
    if [[ ! -f "$local_models_ini" ]]; then
        echo "# ODS model registry" > "$local_models_ini"
    fi
fi

# ============================================================================
# PHASE 5 -- LAUNCH (download model, start services)
# ============================================================================
show_phase 5 6 "LAUNCH" "2-30 minutes (model download)"

if $DRY_RUN; then
    [[ -n "$GGUF_URL" ]] && ai "[DRY RUN] Would download: ${GGUF_FILE}"
    ai "[DRY RUN] Would download llama-server (Metal build)"
    ai "[DRY RUN] Would start native llama-server on port 8080"
    ai "[DRY RUN] Would run: docker compose up -d --remove-orphans --no-build --pull never"
    if $ENABLE_OPEN_WEBUI; then
        ai "[DRY RUN] Would include Open WebUI alongside Dashboard/Portal"
    else
        ai "[DRY RUN] Would skip Open WebUI image and container; retain its data"
    fi
    $ENABLE_PIXEL && ai "[DRY RUN] Would prepare and activate native Pixel after the base stack"
else
    # Change to install directory for docker compose
    cd "$INSTALL_DIR"

    # ── Bootstrap fast-start ──────────────────────────────────────────────
    _BOOTSTRAP_ACTIVE=false
    if [[ "${_MACOS_EXTERNAL_MODEL_READY:-false}" != true ]] && bootstrap_needed "$SELECTED_TIER" "$INSTALL_DIR" "$GGUF_FILE"; then
        _BOOTSTRAP_ACTIVE=true
        FULL_GGUF_FILE="$GGUF_FILE"
        FULL_GGUF_URL="$GGUF_URL"
        FULL_GGUF_SHA256="$GGUF_SHA256"
        FULL_LLM_MODEL="$LLM_MODEL"
        FULL_MAX_CONTEXT="$MAX_CONTEXT"

        GGUF_FILE="$BOOTSTRAP_GGUF_FILE"
        GGUF_URL="$BOOTSTRAP_GGUF_URL"
        GGUF_SHA256="${BOOTSTRAP_GGUF_SHA256:-}"
        LLM_MODEL="$BOOTSTRAP_LLM_MODEL"
        MAX_CONTEXT="$BOOTSTRAP_MAX_CONTEXT"
        ai "Fast-start mode: downloading bootstrap model (~1.5GB) for instant chat."
        ai "Your full model ($FULL_LLM_MODEL) will download in the background."
    fi

    # ── Download GGUF model (if not cloud-only) ──
    if [[ -n "$GGUF_URL" ]] && ! $CLOUD_MODE; then
        MODEL_PATH="${INSTALL_DIR}/data/models/${GGUF_FILE}"

        if [[ -f "$MODEL_PATH" ]]; then
            # Verify integrity if hash is available
            if verify_sha256 "$MODEL_PATH" "$GGUF_SHA256" "Model ${GGUF_FILE}"; then
                ai_ok "Model already present and verified: ${GGUF_FILE}"
            else
                ai "Removing corrupt file and re-downloading..."
                rm -f "$MODEL_PATH"
            fi
        fi

        if [[ ! -f "$MODEL_PATH" ]]; then
            # Download with retry logic (built into download_with_progress)
            if ! download_with_progress "$GGUF_URL" "$MODEL_PATH" "Downloading ${GGUF_FILE}"; then
                ai_err "Model download failed after retries. Re-run the installer to try again."
                exit 1
            fi

            # Verify freshly downloaded file
            if ! verify_sha256 "$MODEL_PATH" "$GGUF_SHA256" "Downloaded ${GGUF_FILE}"; then
                rm -f "$MODEL_PATH"
                ai_err "Downloaded file is corrupt. Re-run the installer to try again."
                exit 1
            fi
        fi
    fi

    # ── Patch .env for bootstrap model ──────────────────────────────────────
    if [[ "$_BOOTSTRAP_ACTIVE" == "true" ]]; then
        _env_file="$INSTALL_DIR/.env"
        if [[ -f "$_env_file" ]]; then
            sed -i '' "s|^GGUF_FILE=.*|GGUF_FILE=${GGUF_FILE}|" "$_env_file"
            sed -i '' "s|^LLM_MODEL=.*|LLM_MODEL=${LLM_MODEL}|" "$_env_file"
            sed -i '' "s|^MAX_CONTEXT=.*|MAX_CONTEXT=${MAX_CONTEXT}|" "$_env_file"
            sed -i '' "s|^CTX_SIZE=.*|CTX_SIZE=${MAX_CONTEXT}|" "$_env_file"
            ai_ok "Patched .env for bootstrap model ($GGUF_FILE)"
        fi

    fi

    # ── Hermes config substitution (macOS-specific) ──
    #
    # Same pattern as the Linux phase 11 substitution but with the
    # macOS-specific base_url. The template ships with
    # `base_url: "http://llama-server:8080/v1"`, which only resolves
    # inside the ods compose bridge. On macOS llama-server
    # runs native on the host (Metal binary, port 8080), so the
    # Hermes container reaches it through the scoped macOS host route — and
    # crucially `model.base_url` in cli-config.yaml WINS over the
    # OPENAI_BASE_URL env compose.yaml sets, so the env override the
    # Linux path relies on doesn't help here.
    #
    # Also patch model.default to the actual served file name. Native
    # Mac llama.cpp serves under the file basename (Qwen3.5-9B-Q4_K_M.gguf
    # rather than the friendly "qwen3.5-9b" the template ships with).
    # This must run even while Hermes is disabled: the dashboard can re-enable
    # it later, and persisted /opt/data/config.yaml wins over the template.
    _hermes_tpl="${INSTALL_DIR}/extensions/services/hermes/cli-config.yaml.template"
    if [[ -f "$_hermes_tpl" ]]; then
            _hermes_base_url="$(read_env_value "${INSTALL_DIR}/.env" "HERMES_LLM_BASE_URL")"
            [[ -n "$_hermes_base_url" ]] || _hermes_base_url="${CONTAINER_LLM_URL%/}/v1"
            _hermes_model="$GGUF_FILE"
            $CLOUD_MODE && _hermes_model="default"
            _hermes_patcher="${INSTALL_DIR}/scripts/patch-hermes-config.py"
            if [[ -f "$_hermes_patcher" ]]; then
                if ! python3 "$_hermes_patcher" "$_hermes_tpl" \
                    --model "$_hermes_model" \
                    --base-url "$_hermes_base_url" \
                    --context-length "$MAX_CONTEXT" >>"$ODS_LOG_FILE" 2>&1; then
                    ai_err "Hermes template patch failed: ${_hermes_tpl}"
                    exit 1
                fi
            else
                sed -i '' \
                    -e "s|^  default: .*|  default: \"${_hermes_model}\"|" \
                    -e "s|^  base_url: .*|  base_url: \"${_hermes_base_url}\"|" \
                    -e "s|^  context_length: .*|  context_length: ${MAX_CONTEXT}|" \
                    -e "s|^    context_length: .*|    context_length: ${MAX_CONTEXT}|" \
                    "$_hermes_tpl"
            fi
            if ! grep -Fq "base_url: \"${_hermes_base_url}\"" "$_hermes_tpl" \
               || ! grep -Fq "  default: \"${_hermes_model}\"" "$_hermes_tpl" \
               || ! grep -q "^  context_length: ${MAX_CONTEXT}$" "$_hermes_tpl"; then
                ai_err "Hermes template verification failed: ${_hermes_tpl}"
                exit 1
            fi

            _hermes_prepatch_rc=0
            _macos_patch_hermes_persisted_config \
                "$_hermes_model" "$_hermes_base_url" "$MAX_CONTEXT" \
                >>"$ODS_LOG_FILE" 2>&1 || _hermes_prepatch_rc=$?
            case "$_hermes_prepatch_rc" in
                0) ai_ok "Updated persisted Hermes routing before service transition" ;;
                3) log "No persisted Hermes config was available before compose" ;;
                4) ai_err "Persisted Hermes routing exists, but no local Python service image can patch it safely"; exit 1 ;;
                *) ai_err "Could not update persisted Hermes config before transition"; exit 1 ;;
            esac
            unset _hermes_prepatch_rc

            # Render data/persona/SOUL.md = static persona + dynamic install
            # context. The Hermes compose mounts this file as the agent's
            # SOUL.md so it answers truthfully about what services + hardware
            # it has on this ODS. MUST run before docker compose up,
            # otherwise Docker's bind-mount engine auto-creates the source
            # path as a *directory* and the install fails at compose-up with
            # "not a directory: Are you trying to mount a directory onto a
            # file" — which then persists across reinstalls because `nuke
            # install dir` preserves data/.
            if $ENABLE_HERMES; then
                _soul_builder="${INSTALL_DIR}/scripts/build-installation-context.py"
                if [[ -f "$_soul_builder" ]]; then
                    python3 "$_soul_builder" >>"$ODS_LOG_FILE" 2>&1 || \
                        ai_warn "Could not generate Hermes installation-context SOUL.md (non-fatal — Hermes will use the template default)"
                fi
            fi
            ai_ok "Prepared Hermes routing for macOS (model=${_hermes_model}, context=${MAX_CONTEXT}, base_url=${_hermes_base_url})"
    fi

    # Cloud transitions must retire every native process owned by this install
    # before local bridge and compose routing are removed.
    if $CLOUD_MODE; then
        _macos_stop_install_owned_native_llama \
            "Stopping install-owned native inference for cloud mode..."
    fi

    # ── Download and start native llama-server (Metal) ──
    if ! $CLOUD_MODE; then
        chapter "NATIVE LLAMA-SERVER (METAL)"

        if [[ "${_MACOS_EXTERNAL_MODEL_READY:-false}" != true ]]; then
            macos_resolve_native_model "$INSTALL_DIR" "$LLAMA_SERVER_BIN" "$MAX_CONTEXT" true || exit 1
        fi
        LLAMA_SERVER_BIN="$MACOS_NATIVE_BINARY"
        LLAMA_SERVER_DIR="$(dirname "$LLAMA_SERVER_BIN")"
        MAX_CONTEXT="$MACOS_NATIVE_CONTEXT"

        # Fresh private download/extraction; authenticate executable bytes first.
        macos_install_native_llama "$LLAMA_SERVER_BIN" "$LLAMA_CPP_RELEASE_TAG" \
            "$LLAMA_CPP_MACOS_ASSET" "$LLAMA_CPP_MACOS_URL" || exit 1

        # Start native llama-server with Metal
        ai "Starting native llama-server (Metal)..."
        MODEL_FULL_PATH="$MACOS_NATIVE_MODEL_PATH"

        mkdir -p "$(dirname "$LLAMA_SERVER_PID_FILE")"

        # Read reasoning mode from .env (default off to prevent thinking models
        # from consuming the entire token budget on internal reasoning)
        _reasoning=$(grep '^LLAMA_REASONING=' "$INSTALL_DIR/.env" 2>/dev/null | cut -d= -f2 || echo "")
        [[ -z "$_reasoning" ]] && _reasoning="off"
        # Modes are case-insensitive, as in ods-host-agent.py; llama-server
        # only accepts the lowercase spelling.
        _reasoning_lower="$(printf '%s' "$_reasoning" | tr '[:upper:]' '[:lower:]')"
        case "$_reasoning_lower" in off|on|auto) _reasoning="$_reasoning_lower" ;; esac
        # Map .env values (off/on/auto) to llama-server --reasoning-format values
        case "$_reasoning" in
            off)  _reasoning_fmt="none" ;;
            on)   _reasoning_fmt="deepseek" ;;
            *)    _reasoning_fmt="$_reasoning" ;;
        esac

        # LAN access is limited to authenticated UIs; inference stays private.
        _bind="127.0.0.1"
        [[ -z "$_bind" ]] && _bind="127.0.0.1"
        _native_llama_port=$(read_env_value "$INSTALL_DIR/.env" "ODS_NATIVE_LLAMA_PORT")
        [[ "$_native_llama_port" =~ ^[0-9]+$ ]] || _native_llama_port="8080"
        _llama_probe_host="$(macos_bind_probe_host "$_bind")"

        _flash_attn=$(grep '^LLAMA_ARG_FLASH_ATTN=' "$INSTALL_DIR/.env" 2>/dev/null | cut -d= -f2 | tr -d '"' || echo "")
        _cache_type_k=$(grep '^LLAMA_ARG_CACHE_TYPE_K=' "$INSTALL_DIR/.env" 2>/dev/null | cut -d= -f2 | tr -d '"' || echo "")
        _cache_type_v=$(grep '^LLAMA_ARG_CACHE_TYPE_V=' "$INSTALL_DIR/.env" 2>/dev/null | cut -d= -f2 | tr -d '"' || echo "")
        _n_cpu_moe=$(grep '^LLAMA_ARG_N_CPU_MOE=' "$INSTALL_DIR/.env" 2>/dev/null | cut -d= -f2 | tr -d '"' || echo "")
        _gpu_layers=$(grep '^N_GPU_LAYERS=' "$INSTALL_DIR/.env" 2>/dev/null | cut -d= -f2 | tr -d '"' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' || echo "")
        [[ -z "$_gpu_layers" ]] && _gpu_layers="auto"
        _spec_type=$(grep '^LLAMA_ARG_SPEC_TYPE=' "$INSTALL_DIR/.env" 2>/dev/null | cut -d= -f2 | tr -d '"' || echo "")
        _llama_args=(
            --host "$_bind" --port "$_native_llama_port"
            --model "$MODEL_FULL_PATH"
            --ctx-size "$MAX_CONTEXT"
            --n-gpu-layers "$_gpu_layers"
            --metrics
        )
        if [[ "$MACOS_NATIVE_PROFILE" == true ]]; then
            _llama_args+=(--reasoning-format "$_reasoning_fmt" "${MACOS_NATIVE_PROFILE_ARGS[@]}")
        else
        _parallel="$(read_env_value "$INSTALL_DIR/.env" "LLAMA_PARALLEL")"
        _llama_args+=(--parallel "${_parallel:-1}")
        [[ -n "$_flash_attn" ]] && _llama_args+=(--flash-attn "$_flash_attn")
        [[ -n "$_cache_type_k" ]] && _llama_args+=(--cache-type-k "$_cache_type_k")
        [[ -n "$_cache_type_v" ]] && _llama_args+=(--cache-type-v "$_cache_type_v")
        [[ -n "$_n_cpu_moe" ]] && _llama_args+=(--n-cpu-moe "$_n_cpu_moe")
        [[ -n "$_spec_type" ]] && _llama_args+=(--spec-type "$_spec_type")
        # Draft flags, --ctx-checkpoints 32, the ngram-mod default and the
        # reasoning flags (--reasoning on b9014, else this --reasoning-format)
        # are spelled for, and only added when supported by, this runtime.
        macos_resolve_checkpoint_args "$INSTALL_DIR" "$LLAMA_SERVER_BIN" "$_reasoning_fmt" || exit 1
        _llama_args+=(${MACOS_NATIVE_CHECKPOINT_ARGS[@]+"${MACOS_NATIVE_CHECKPOINT_ARGS[@]}"})
        fi
        # The projector the host agent's switch launches with: a qualified profile's
        # or a vision import's own (WP2), so a restart keeps the model's vision.
        [[ -n "${MACOS_NATIVE_PROJECTOR_PATH:-}" ]] && _llama_args+=(--mmproj "$MACOS_NATIVE_PROJECTOR_PATH")
        # A fixed chat template the switch chose for an exact template match (WP5).
        [[ -n "${MACOS_NATIVE_CHAT_TEMPLATE_PATH:-}" ]] && _llama_args+=(--chat-template-file "$MACOS_NATIVE_CHAT_TEMPLATE_PATH")

        _macos_stop_install_owned_native_llama \
            "Stopping prior install-owned native inference before replacement..."
        bash "$INSTALL_DIR/installers/macos/lib/native-llama-service.sh" start \
            "$INSTALL_DIR" "$LLAMA_SERVER_BIN" "$LLAMA_SERVER_PID_FILE" "${_llama_args[@]}"
        LLAMA_PID="$(cat "$LLAMA_SERVER_PID_FILE")"

        # Wait for health endpoint
        ai "Waiting for llama-server to load model..."
        MAX_WAIT=180
        WAITED=0
        HEALTHY=false
        while [[ "$WAITED" -lt "$MAX_WAIT" ]]; do
            sleep 2
            WAITED=$((WAITED + 2))
            if curl -sf --max-time 10 "http://${_llama_probe_host}:${_native_llama_port}/health" >/dev/null 2>&1; then
                HEALTHY=true
                break
            fi
            # Check if process died
            if ! kill -0 "$LLAMA_PID" 2>/dev/null; then
                ai_err "llama-server process died. Check logs:"
                ai "  tail -50 ${LLAMA_SERVER_LOG}"
                exit 1
            fi
            if (( WAITED % 10 == 0 )); then
                ai "  Still loading... (${WAITED}s)"
            fi
        done

        if $HEALTHY; then
            ai_ok "Native llama-server healthy (PID ${LLAMA_PID})"

            # ── Pre-warm the LLM slot ──
            # /health returning 200 only means the model is mmap'd. The
            # FIRST chat completion still has to materialize KV cache and
            # JIT compile fused kernels — during that, llama-server 503s
            # concurrent requests. Hermes Agent's default 3-retry / 120s
            # budget burns out on this on slower hardware, so we force
            # the slot through cold path here while we're already in the
            # "this may take a minute" install context. Bounded by curl
            # --max-time so a stalled llama-server can't hang the install.
            _prewarm_url="http://${_llama_probe_host}:${_native_llama_port}/v1/chat/completions"
            _prewarm_model="${GGUF_FILE:-default}"
            _prewarm_body="{\"model\":\"${_prewarm_model}\",\"messages\":[{\"role\":\"user\",\"content\":\"hi\"}],\"max_tokens\":1,\"temperature\":0,\"stream\":false}"
            if curl -sf --max-time 120 -X POST "$_prewarm_url" \
                -H "Content-Type: application/json" \
                -d "$_prewarm_body" >/dev/null 2>&1; then
                ai_ok "LLM slot pre-warmed (first real chat will be fast)"
            else
                ai_warn "LLM pre-warm timed out — first Hermes prompt may need a retry while the slot warms."
            fi
        else
            ai_warn "llama-server did not become healthy within ${MAX_WAIT}s. It may still be loading."
        fi
    fi

    # ── Assemble Docker Compose flags ──
    COMPOSE_FLAGS=("-f" "docker-compose.base.yml")

    MACOS_CLOUD_AUTH_OVERLAY=""
    if $CLOUD_MODE; then
        # Cloud mode has no native llama process or macOS readiness sidecar.
        COMPOSE_FLAGS+=("-f" "docker-compose.cloud.yml")
        MACOS_CLOUD_AUTH_OVERLAY="data/generated/docker-compose.macos-cloud-auth.yml"
        _write_macos_cloud_auth_overlay "${INSTALL_DIR}/${MACOS_CLOUD_AUTH_OVERLAY}"
    else
        # Normal macOS mode: native llama-server
        COMPOSE_FLAGS+=("-f" "installers/macos/docker-compose.macos.yml")
    fi
    if ! $ENABLE_OPEN_WEBUI; then
        COMPOSE_FLAGS+=("-f" "docker-compose.gateway-only.yml")
    fi

    # Discover enabled extension compose fragments via manifests
    EXT_DIR="${INSTALL_DIR}/extensions/services"
    CURRENT_BACKEND="apple"
    $CLOUD_MODE && CURRENT_BACKEND="none"
    if [[ -d "$EXT_DIR" ]]; then
        for SVC_DIR in "$EXT_DIR"/*/; do
            [[ ! -d "$SVC_DIR" ]] && continue
            SVC_NAME=$(basename "$SVC_DIR")

            # Read manifest
            MANIFEST_PATH="${SVC_DIR}manifest.yaml"
            [[ ! -f "$MANIFEST_PATH" ]] && MANIFEST_PATH="${SVC_DIR}manifest.yml"
            [[ ! -f "$MANIFEST_PATH" ]] && continue

            # Quick manifest validation: must contain schema_version: ods.services.v1
            if ! grep -q "schema_version:.*ods\.services\.v1" "$MANIFEST_PATH" 2>/dev/null; then
                continue
            fi

            # Check gpu_backends compatibility
            BACKENDS_LINE=$(grep "gpu_backends:" "$MANIFEST_PATH" 2>/dev/null || true)
            if [[ -n "$BACKENDS_LINE" ]] && [[ "$CURRENT_BACKEND" != "none" ]]; then
                if ! echo "$BACKENDS_LINE" | grep -qE "(${CURRENT_BACKEND}|all)" 2>/dev/null; then
                    # Check if "apple" is not listed but service works on CPU
                    # Extension services like whisper, tts work on CPU in Docker
                    # Allow if gpu_backends contains "amd" or "nvidia" (CPU fallback)
                    if ! echo "$BACKENDS_LINE" | grep -qE "(amd|nvidia)" 2>/dev/null; then
                        continue
                    fi
                fi
            fi

            # Find compose file
            COMPOSE_FILE="compose.yaml"
            COMPOSE_REF=$(grep "compose_file:" "$MANIFEST_PATH" 2>/dev/null | awk -F: '{print $2}' | tr -d ' "'"'" || true)
            [[ -n "$COMPOSE_REF" ]] && COMPOSE_FILE="$COMPOSE_REF"

            COMPOSE_PATH="${SVC_DIR}${COMPOSE_FILE}"
            [[ ! -f "$COMPOSE_PATH" ]] && continue

            # Check feature flags
            SKIP=false
            case "$SVC_NAME" in
                litellm)       $ENABLE_LITELLM || SKIP=true ;;
                token-spy)     $ENABLE_RECOMMENDED || SKIP=true ;;
                searxng)       $ENABLE_SEARXNG || SKIP=true ;;
                whisper|tts)   $ENABLE_VOICE || SKIP=true ;;
                n8n)           $ENABLE_WORKFLOWS || SKIP=true ;;
                qdrant|embeddings) $ENABLE_RAG || SKIP=true ;;
                hermes|hermes-proxy) $ENABLE_HERMES || SKIP=true ;;
                ape)           $ENABLE_APE || SKIP=true ;;
                perplexica)    $ENABLE_PERPLEXICA || SKIP=true ;;
                privacy-shield) $ENABLE_PRIVACY_SHIELD || SKIP=true ;;
                ods-proxy)   $ENABLE_ODS_PROXY || SKIP=true ;;
                tailscale)     $ENABLE_TAILSCALE || SKIP=true ;;
                langfuse)      $ENABLE_LANGFUSE || SKIP=true ;;
                brave-search)  [[ "${ENABLE_BRAVE_SEARCH:-false}" == "true" ]] || SKIP=true ;;
            esac
            $SKIP && continue

            REL_PATH="${COMPOSE_PATH#"${INSTALL_DIR}/"}"
            COMPOSE_FLAGS+=("-f" "$REL_PATH")

            # GPU-backend overlay (mirrors resolve-compose-stack.sh discovery).
            # E.g. extensions/services/litellm/compose.apple.yaml on macOS.
            # Skipped in cloud mode (CURRENT_BACKEND=none) since no native
            # GPU/host-gateway patches apply when llama-server runs remotely.
            if [[ "$CURRENT_BACKEND" != "none" ]]; then
                GPU_OVERLAY_PATH="${SVC_DIR}compose.${CURRENT_BACKEND}.yaml"
                if [[ -f "$GPU_OVERLAY_PATH" ]]; then
                    COMPOSE_FLAGS+=("-f" "${GPU_OVERLAY_PATH#"${INSTALL_DIR}/"}")
                fi
            fi
        done
    fi

    # Layer Tier 0 memory overlay for low-RAM machines
    if [[ "$SELECTED_TIER" == "0" && -f "${INSTALL_DIR}/docker-compose.tier0.yml" ]]; then
        COMPOSE_FLAGS+=("-f" "docker-compose.tier0.yml")
        ai "Applying lightweight memory limits for Tier 0"
    fi

    # Both cloud and native Mac installs keep private API SQLite in the VM.
    COMPOSE_FLAGS+=("-f" "installers/macos/dashboard-private-state.compose.yaml")

    # Docker compose override (user customizations)
    if [[ -f "${INSTALL_DIR}/docker-compose.override.yml" ]]; then
        COMPOSE_FLAGS+=("-f" "docker-compose.override.yml")
    fi

    # Keep this last so cloud clients always authenticate with the same
    # LITELLM_KEY that configures LiteLLM's LITELLM_MASTER_KEY. The generated
    # file contains only variable references, never the secret itself.
    if [[ -n "$MACOS_CLOUD_AUTH_OVERLAY" ]]; then
        COMPOSE_FLAGS+=("-f" "$MACOS_CLOUD_AUTH_OVERLAY")
    fi

    # ── Validate compose files exist before launching ──
    for ((i=0; i<${#COMPOSE_FLAGS[@]}; i++)); do
        if [[ "${COMPOSE_FLAGS[$i]}" == "-f" ]] && (( i+1 < ${#COMPOSE_FLAGS[@]} )); then
            CF="${COMPOSE_FLAGS[$((i+1))]}"
            if [[ ! -f "$CF" ]]; then
                ai_err "Compose file not found: ${CF}"
                ai "The source tree may not have copied correctly. Try re-running with --force."
                exit 1
            fi
        fi
    done

    # ── Unload stale LaunchAgents before compose (crash-safe) ──
    # If a previous install registered these agents and this run fails at
    # compose-up, the old agents would keep running with stale paths
    # (ODS_HOME pointing at the deleted install dir).  Clearing them
    # here guarantees a clean slate regardless of what happens below.
    launchctl bootout "gui/$(id -u)/${ODS_AGENT_PLIST_LABEL}" 2>/dev/null || true
    launchctl bootout "gui/$(id -u)/${HOST_AGENT_BRIDGE_PLIST_LABEL}" 2>/dev/null || true
    rm -f "$HOST_AGENT_BRIDGE_PLIST" 2>/dev/null || true
    if $ENABLE_OPENCODE; then
        launchctl bootout "gui/$(id -u)/${OPENCODE_PLIST_LABEL}" 2>/dev/null || true
    fi
    for _legacy_plist_label in \
        com.ods.full-model-download; do
        launchctl bootout "gui/$(id -u)/${_legacy_plist_label}" 2>/dev/null || true
        rm -f "$HOME/Library/LaunchAgents/${_legacy_plist_label}.plist" 2>/dev/null || true
    done
    unset _legacy_plist_label

    # ── Start Docker services ──
    chapter "STARTING SERVICES"

    _macos_compose_up_args=(up -d --remove-orphans --no-build --pull never)

    _macos_is_local_image() {
        local image="${1:-}"
        case "$image" in
            ""|ods-*|ods-*:*|docker.io/library/ods-*|localhost/*|localhost:*/*|127.0.0.1:*/*)
                return 0
                ;;
        esac
        return 1
    }

    _macos_compose_external_images() {
        local config_json
        if config_json="$(docker compose "${COMPOSE_FLAGS[@]}" config --format json 2>>"$ODS_LOG_FILE")"; then
            if printf '%s' "$config_json" | python3 -c '
import json
import sys

try:
    data = json.load(sys.stdin)
except Exception:
    sys.exit(1)

for service in (data.get("services") or {}).values():
    if service.get("build") is not None:
        continue
    image = str(service.get("image") or "").strip()
    if not image:
        continue
    platform = str(service.get("platform") or "").strip()
    print(image + "\t" + platform)
' | while IFS=$'\t' read -r _image _platform; do
                _macos_is_local_image "$_image" && continue
                printf '%s\t%s\n' "$_image" "$_platform"
            done | awk '!seen[$0]++'; then
                return 0
            fi
        fi

        docker compose "${COMPOSE_FLAGS[@]}" config --images 2>>"$ODS_LOG_FILE" | while IFS= read -r _image; do
            _macos_is_local_image "$_image" && continue
            printf '%s\n' "$_image"
        done | awk '!seen[$0]++'
    }

    _macos_normalize_image_platform() {
        local raw="${1:-}" os arch variant extra
        raw="${raw//$'\r'/}"
        raw="${raw//$'\n'/}"
        raw="${raw//[[:space:]]/}"
        raw="${raw,,}"
        raw="${raw#\"}"
        raw="${raw%\"}"
        raw="${raw#\'}"
        raw="${raw%\'}"
        [[ -n "$raw" ]] || return 1

        if [[ "$raw" != */* ]]; then
            raw="linux/$raw"
        fi
        IFS='/' read -r os arch variant extra <<< "$raw"
        [[ -n "$os" && -n "$arch" && -z "$extra" ]] || return 1

        case "$arch" in
            amd64|x86_64|x86-64)
                arch="amd64"
                ;;
            arm64|aarch64)
                arch="arm64"
                ;;
            *)
                return 1
                ;;
        esac

        case "${variant:-}" in
            ""|"<no value>")
                variant=""
                ;;
            v8)
                [[ "$arch" == "arm64" ]] || return 1
                variant=""
                ;;
            *)
                return 1
                ;;
        esac

        printf '%s/%s\n' "$os" "$arch"
    }

    _macos_cached_image_platform() {
        local image="$1" inspected
        inspected="$(docker image inspect \
            --format '{{.Os}}/{{.Architecture}}{{if .Variant}}/{{.Variant}}{{end}}' \
            "$image" 2>/dev/null)" || return 1
        _macos_normalize_image_platform "$inspected"
    }

    _macos_pull_image_with_retry() {
        # $2 is the compose service's platform pin (may be empty). Without it,
        # docker pull resolves the host platform (linux/arm64 on Apple
        # Silicon), which hard-fails for amd64-only images like TEI even
        # though compose would run them pinned under emulation.
        local image="$1" platform="${2:-}" attempt max_attempts delay cached_platform requested_platform
        local -a delays=(5 15 30)
        local -a pull_cmd=(docker pull "$image")
        [[ -n "$platform" ]] && pull_cmd=(docker pull --platform "$platform" "$image")

        if [[ -z "$platform" ]]; then
            if docker image inspect "$image" >/dev/null 2>&1; then
                log "Compose image already cached: $image"
                return 0
            fi
        else
            requested_platform="$(_macos_normalize_image_platform "$platform" 2>/dev/null || true)"
            cached_platform="$(_macos_cached_image_platform "$image" 2>/dev/null || true)"
            if [[ -n "$requested_platform" && "$cached_platform" == "$requested_platform" ]]; then
                log "Compose image already cached for $requested_platform: $image"
                return 0
            fi
            if [[ -n "$cached_platform" ]]; then
                log "Compose image cache platform mismatch for $image: cached=$cached_platform requested=${requested_platform:-$platform}"
            fi
        fi

        max_attempts="${ODS_DOCKER_PULL_MAX_ATTEMPTS:-4}"
        for ((attempt=1; attempt<=max_attempts; attempt++)); do
            ai "Pulling Compose image ($attempt/$max_attempts): $image${platform:+ [$platform]}"
            if "${pull_cmd[@]}" >>"$ODS_LOG_FILE" 2>&1; then
                ai_ok "Pulled $image"
                return 0
            fi
            if (( attempt < max_attempts )); then
                delay="${delays[$((attempt - 1))]:-30}"
                ai_warn "Pull failed for $image; retrying in ${delay}s"
                sleep "$delay"
            fi
        done

        ai_err "Failed to pull Compose image after retries: $image"
        return 1
    }

    _macos_pre_pull_compose_images() {
        local image_output image platform failed
        image_output="$(_macos_compose_external_images)" || {
            ai_err "Could not resolve macOS Docker Compose images before service launch"
            ai "Inspect compose config with: cd '$INSTALL_DIR' && docker compose ${COMPOSE_FLAGS[*]} config --images"
            return 1
        }
        [[ -n "$image_output" ]] || return 0

        ai "Verifying Compose image cache before launch..."
        failed=0
        while IFS=$'\t' read -r image platform; do
            [[ -n "$image" ]] || continue
            _macos_pull_image_with_retry "$image" "$platform" || failed=$((failed + 1))
        done <<< "$image_output"

        if [[ "$failed" -eq 0 ]]; then
            ai_ok "Compose image cache ready"
            return 0
        fi

        ai_err "$failed Compose image(s) could not be pulled before launch"
        ai "macOS installer will not allow Docker Compose to pull images implicitly."
        ai "Fix Docker registry/network/disk access, then re-run ./installers/macos/install-macos.sh."
        return 1
    }

    # ── Rebuild local-built images ─────────────────────────────────────
    # Mirrors phases/11-services.sh on Linux: local Dockerfiles can drift from
    # baked images, so rebuild the local services that are actually present in
    # the resolved macOS compose stack. Optional services such as
    # privacy-shield may be disabled by feature flags; building them anyway can
    # surface unrelated Dockerfile failures and make a healthy selected stack
    # look broken.
    ai "Rebuilding local-built images..."
    _macos_candidate_build_services=(dashboard dashboard-api model-router remote-provider-egress remote-provider-ssh-tunnel ape token-spy privacy-shield brave-search pixel-inference langfuse-minio langfuse-minio-init)
    if ! _macos_enabled_services="$(docker compose "${COMPOSE_FLAGS[@]}" config --services 2>>"$ODS_LOG_FILE")"; then
        ai_err "Could not resolve macOS compose services for local image rebuilds."
        ai "Inspect compose config with: cd '$INSTALL_DIR' && docker compose ${COMPOSE_FLAGS[*]} config --services"
        exit 1
    fi
    _macos_build_services=()
    for _svc in "${_macos_candidate_build_services[@]}"; do
        if printf '%s\n' "$_macos_enabled_services" | grep -qx "$_svc"; then
            _macos_build_services+=("$_svc")
        else
            log "Skipping local image rebuild for disabled service: $_svc"
        fi
    done

    _macos_build_max_attempts="${ODS_DOCKER_BUILD_MAX_ATTEMPTS:-3}"
    [[ "$_macos_build_max_attempts" =~ ^[1-9][0-9]*$ ]] || _macos_build_max_attempts=3
    _macos_build_failed=0
    _macos_build_delays=(5 15 30)
    for _svc in "${_macos_build_services[@]}"; do
        _macos_build_ok=false
        for ((_attempt=1; _attempt<=_macos_build_max_attempts; _attempt++)); do
            ai "Building local image: ${_svc} (${_attempt}/${_macos_build_max_attempts})"
            if docker compose "${COMPOSE_FLAGS[@]}" build "$_svc" >> "$ODS_LOG_FILE" 2>&1; then
                _macos_build_ok=true
                ai_ok "Built local image: ${_svc}"
                break
            fi
            if (( _attempt < _macos_build_max_attempts )); then
                _delay="${_macos_build_delays[$((_attempt - 1))]:-30}"
                ai_warn "Build failed for ${_svc}; retrying in ${_delay}s (see $ODS_LOG_FILE)"
                sleep "$_delay"
            fi
        done
        if [[ "$_macos_build_ok" != "true" ]]; then
            ai_err "Build failed for required local service ${_svc} after ${_macos_build_max_attempts} attempts (see $ODS_LOG_FILE)"
            _macos_build_failed=$((_macos_build_failed + 1))
        fi
    done
    if [[ "$_macos_build_failed" -ne 0 ]]; then
        ai_err "${_macos_build_failed} required local image(s) failed to build; refusing to launch stale images."
        exit 1
    fi
    ai_ok "Local images rebuilt"

    mkdir -p "${INSTALL_DIR}/logs"
    _compose_up_log="${INSTALL_DIR}/logs/compose-up.log"
    : > "$_compose_up_log"

    if ! _macos_pre_pull_compose_images; then
        if command -v write_compose_failure_report >/dev/null 2>&1; then
            _compose_report_path="$(COMPOSE_FLAGS_REPORT="${COMPOSE_FLAGS[*]}" write_compose_failure_report \
                "$INSTALL_DIR" \
                "install-macos compose image preflight" \
                "docker compose ${COMPOSE_FLAGS[*]} ${_macos_compose_up_args[*]}" \
                "$_compose_up_log" \
                "apple" \
                "A required Compose image did not download during the retry-protected preflight. Fix Docker registry/network/disk access, then re-run ./installers/macos/install-macos.sh." |
                tail -n 1)" || true
            [[ -n "${_compose_report_path:-}" ]] && ai_warn "Compose failure report saved: $_compose_report_path"
        fi
        exit 1
    fi

    _compose_launch_record="${INSTALL_DIR}/logs/compose-launch.txt"
    {
        printf 'timestamp=%s\n' "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
        printf 'cwd=%s\n' "$INSTALL_DIR"
        printf 'compose_command=docker compose %s %s\n' "${COMPOSE_FLAGS[*]}" "${_macos_compose_up_args[*]}"
        printf 'compose_flags=%s\n' "${COMPOSE_FLAGS[*]}"
        printf 'compose_flags_file=%s\n' "$INSTALL_DIR/.compose-flags"
        printf "compose_ps_command=cd '%s' && docker compose %s ps -a\n" "$INSTALL_DIR" "${COMPOSE_FLAGS[*]}"
        printf "compose_logs_command=cd '%s' && docker compose %s logs --tail 200\n" "$INSTALL_DIR" "${COMPOSE_FLAGS[*]}"
        printf 'compose_files=\n'
        _expect_file=false
        for _arg in "${COMPOSE_FLAGS[@]}"; do
            if $_expect_file; then
                printf '  - %s\n' "$_arg"
                _expect_file=false
            elif [[ "$_arg" == "-f" ]]; then
                _expect_file=true
            fi
        done
    } > "$_compose_launch_record"
    # Save the stack before starting it. ods-uninstall.sh reads this file first,
    # and a later failure (including a partial compose up) must not leave it
    # naming an older run's stack whose volumes no longer match.
    echo "${COMPOSE_FLAGS[*]}" > "${INSTALL_DIR}/.compose-flags"
    ai "Running: docker compose ${COMPOSE_FLAGS[*]} ${_macos_compose_up_args[*]}"
    set +o pipefail  # pipefail would abort on compose exit before PIPESTATUS is read; capture it first
    /usr/bin/python3 "$INSTALL_DIR/installers/macos/lib/dashboard-private-state.py" \
        launch --operation install --install-dir "$INSTALL_DIR" --flags="${COMPOSE_FLAGS[*]}" \
        2>&1 | tee -a "$_compose_up_log" | while IFS= read -r line; do
        echo "  $line"
    done
    compose_exit="${PIPESTATUS[0]}"
    set -o pipefail

    if [[ "$compose_exit" -ne 0 ]]; then
        if command -v write_compose_failure_report >/dev/null 2>&1; then
            _compose_report_path="$(COMPOSE_FLAGS_REPORT="${COMPOSE_FLAGS[*]}" write_compose_failure_report \
                "$INSTALL_DIR" \
                "install-macos docker compose up" \
                "docker compose ${COMPOSE_FLAGS[*]} ${_macos_compose_up_args[*]}" \
                "$_compose_up_log" \
                "apple" \
                "Open the saved report, fix the failed image/port/compose error it identifies, then re-run ./installers/macos.sh." |
                tail -n 1)" || true
            [[ -n "${_compose_report_path:-}" ]] && ai_warn "Compose failure report saved: $_compose_report_path"
        fi
        ai_err "docker compose up failed"
        exit 1
    fi
    _compose_container_ids="$(docker compose "${COMPOSE_FLAGS[@]}" ps -q 2>>"$ODS_LOG_FILE" || true)"
    _compose_container_count="$(printf '%s\n' "$_compose_container_ids" | sed '/^[[:space:]]*$/d' | wc -l | tr -d '[:space:]')"
    if [[ "${_compose_container_count:-0}" -eq 0 ]]; then
        if command -v write_compose_failure_report >/dev/null 2>&1; then
            _compose_report_path="$(COMPOSE_FLAGS_REPORT="${COMPOSE_FLAGS[*]}" write_compose_failure_report \
                "$INSTALL_DIR" \
                "install-macos zero managed containers" \
                "docker compose ${COMPOSE_FLAGS[*]} ${_macos_compose_up_args[*]}" \
                "$_compose_up_log" \
                "apple" \
                "No ODS containers were created. Run the saved ps/logs commands from logs/compose-launch.txt, fix the compose/runtime failure, then re-run ./installers/macos.sh." |
                tail -n 1)" || true
            [[ -n "${_compose_report_path:-}" ]] && ai_warn "Compose failure report saved: $_compose_report_path"
        fi
        ai_err "docker compose up completed but created no managed containers"
        ai "Launch record: $_compose_launch_record"
        ai "Inspect with: cd '$INSTALL_DIR' && docker compose ${COMPOSE_FLAGS[*]} ps -a"
        exit 1
    fi
    ai_ok "Docker services started"

    if $ENABLE_HERMES; then
        _hermes_running=false
        for _hermes_wait_i in $(seq 1 90); do
            if [[ "$(docker inspect --format '{{.State.Status}}' ods-hermes 2>/dev/null || true)" == "running" ]]; then
                _hermes_running=true
                break
            fi
            sleep 1
        done
        if [[ "$_hermes_running" != "true" ]]; then
            ai_err "Hermes was selected but its container did not reach running state."
            exit 1
        fi

        _hermes_live_verified=false
        # First boot can spend several minutes fixing image ownership before
        # creating config.yaml, especially under Docker Desktop emulation.
        for _hermes_wait_i in $(seq 1 600); do
            _hermes_patch_rc=0
            _macos_patch_hermes_persisted_config \
                "$_hermes_model" "$_hermes_base_url" "$MAX_CONTEXT" \
                >>"$ODS_LOG_FILE" 2>&1 || _hermes_patch_rc=$?
            if [[ "$_hermes_patch_rc" -eq 0 ]]; then
                _hermes_live_verified=true
                break
            fi
            [[ "$_hermes_patch_rc" -eq 3 ]] || break
            sleep 1
        done
        if [[ "$_hermes_live_verified" != "true" ]]; then
            ai_err "Could not authoritatively update and verify Hermes's persisted container-owned config."
            exit 1
        fi
        if ! docker restart ods-hermes >>"$ODS_LOG_FILE" 2>&1; then
            ai_err "Could not restart Hermes after updating its persisted routing."
            exit 1
        fi
        _hermes_restarted_healthy=false
        for _hermes_wait_i in $(seq 1 180); do
            if [[ "$(docker inspect --format '{{.State.Health.Status}}' ods-hermes 2>/dev/null || true)" == "healthy" ]]; then
                _hermes_restarted_healthy=true
                break
            fi
            sleep 1
        done
        if [[ "$_hermes_restarted_healthy" != "true" ]]; then
            ai_err "Hermes did not become healthy after applying persisted routing."
            exit 1
        fi
        _hermes_expected_key="$(read_env_value "$INSTALL_DIR/.env" "HERMES_LLM_API_KEY")"
        if ! _macos_verify_hermes_container_auth "$_hermes_expected_key" \
            >>"$ODS_LOG_FILE" 2>&1; then
            ai_err "Hermes container authentication does not match the configured inference gateway."
            exit 1
        fi
        ai_ok "Hermes persisted routing and authentication verified"
        unset _hermes_running _hermes_wait_i _hermes_patch_rc \
            _hermes_live_verified _hermes_restarted_healthy _hermes_expected_key
    fi

    # Refresh the generated Hermes persona now that the stack is actually
    # running, then copy it into Hermes's runtime data dir from inside the
    # container. This avoids Docker Desktop's nested bind-mount restriction
    # while still keeping /opt/data/SOUL.md current for new sessions.
    _soul_builder="${INSTALL_DIR}/scripts/build-installation-context.py"
    if [[ -f "$_soul_builder" ]]; then
        python3 "$_soul_builder" >>"$ODS_LOG_FILE" 2>&1 || \
            ai_warn "Could not refresh Hermes installation-context SOUL.md after compose-up"
        if docker ps --format '{{.Names}}' 2>/dev/null | grep -qx 'ods-hermes'; then
            docker exec ods-hermes cp /opt/hermes/docker/SOUL.md /opt/data/SOUL.md \
                >>"$ODS_LOG_FILE" 2>&1 || \
                ai_warn "Could not sync installation-context SOUL.md into running Hermes container"
        fi
    fi

    if $ENABLE_PIXEL; then
        ai "Preparing native Pixel and its Docker services..."
        _pixel_install_args+=(--ods-source "$INSTALL_DIR")
        [[ -z "${PIXEL_SOURCE_REF:-}" ]] || _pixel_install_args+=(--ref "$PIXEL_SOURCE_REF")
        for ((_pixel_i=0; _pixel_i<${#COMPOSE_FLAGS[@]}; _pixel_i+=2)); do
            [[ "${COMPOSE_FLAGS[_pixel_i]}" == -f ]] || { ai_err "Unexpected Compose selection"; exit 1; }
            _pixel_install_args+=(--compose-file "$INSTALL_DIR/${COMPOSE_FLAGS[_pixel_i+1]}")
        done
        _pixel_compose_fragments=(
            -f extensions/services/pixel-model-relay/compose.yaml.disabled
            -f extensions/services/pixel-edge/compose.yaml.disabled
            -f installers/macos/pixel-native.compose.yaml.disabled
        )
        if ! /usr/bin/python3 "$LIB_DIR/pixel-native-install.py" "${_pixel_install_args[@]}" \
            2>&1 | tee -a "$ODS_LOG_FILE"; then
            # Activation journals an attempt only after these fragments render
            # and before it starts their containers. Record them for the
            # uninstaller then; without the journal no Pixel container exists
            # and its .env bindings may be missing, so leave them out.
            if [[ -f "${INSTALL_DIR}/data/pixel-native/preparation/activation.json" ]]; then
                echo "${COMPOSE_FLAGS[*]} ${_pixel_compose_fragments[*]}" > "${INSTALL_DIR}/.compose-flags"
            fi
            ai_err "Native Pixel setup stopped. Keep data/pixel-native and its private receipts for diagnosis."
            exit 1
        fi
        COMPOSE_FLAGS+=("${_pixel_compose_fragments[@]}")
        ai_ok "Native Pixel activated for Dashboard/Portal"
    fi

    # Save compose flags for ods-macos.sh
    echo "${COMPOSE_FLAGS[*]}" > "${INSTALL_DIR}/.compose-flags"
    if $ENABLE_PIXEL; then
        _macos_add_example_projects
    fi

    # ── Launch background model upgrade ──────────────────────────────────
    if [[ "$_BOOTSTRAP_ACTIVE" == "true" ]]; then
        ai "Launching background download for $FULL_LLM_MODEL..."
        mkdir -p "$INSTALL_DIR/logs"
        _upgrade_script="$INSTALL_DIR/scripts/bootstrap-upgrade.sh"

        if [[ -x "$_upgrade_script" ]] || [[ -f "$_upgrade_script" ]]; then
            _macos_persist_bootstrap_upgrade_args \
                "$FULL_GGUF_FILE" "$FULL_GGUF_URL" "$FULL_GGUF_SHA256" \
                "$FULL_LLM_MODEL" "$FULL_MAX_CONTEXT" "$BOOTSTRAP_GGUF_FILE" || true
            # Start the long-lived downloader from a child shell after closing
            # inherited non-stdio FDs, matching the Linux installer contract.
            if ! (
                _close_inherited_fds_for_daemon
                _macos_launch_detached_bootstrap_upgrade "$_upgrade_script" \
                    "$INSTALL_DIR" "$FULL_GGUF_FILE" "$FULL_GGUF_URL" \
                    "$FULL_GGUF_SHA256" "$FULL_LLM_MODEL" "$FULL_MAX_CONTEXT" \
                    "$BOOTSTRAP_GGUF_FILE"
            ); then
                ai_err "Could not launch the isolated background model upgrade."
                exit 1
            fi
            ai "Full model ($FULL_LLM_MODEL) downloading in background."
            ai "Check progress: tail -f $INSTALL_DIR/logs/model-upgrade.log"
        else
            ai_warn "bootstrap-upgrade.sh not found. Download the full model manually."
        fi
    fi

    ods_macos_install_opencode
fi

# ── ODS Host Agent (extension lifecycle management) ──
if $DRY_RUN; then
    ai "[DRY RUN] Would install, configure, and verify the authenticated dashboard host-agent path"
else
ods_macos_install_host_agent
fi

# ============================================================================
# PHASE 6 -- VERIFICATION
# ============================================================================
show_phase 6 6 "VERIFICATION" "30 seconds"

if $DRY_RUN; then
    ai "[DRY RUN] Would health-check all services"
    ai "[DRY RUN] Would auto-configure Perplexica for ${LLM_MODEL}"
    ai "[DRY RUN] Install validation complete"
    ai_ok "Dry run finished -- no changes made"
    exit 0
fi

# Health check loop
ai "Running health checks..."
MAX_ATTEMPTS=90   # 90 * 2s = 180s -- covers base compose start_period (60s) + image pull
ALL_HEALTHY=true
CLOUD_REQUIRED_HEALTHY=true

# Parallel arrays (Bash 3.2 compatible -- no associative arrays).
# HEALTH_CONTAINERS holds the Docker container name when a service runs in
# Docker; an empty string means the service is host-native (llama-server
# runs natively on macOS via Metal; OpenCode is a LaunchAgent). Docker
# services wait on `docker inspect ... .State.Health.Status == healthy`;
# host-native services fall back to an HTTP probe on 127.0.0.1.
if $CLOUD_MODE; then
    HEALTH_NAMES=("LiteLLM gateway")
    HEALTH_URLS=("http://127.0.0.1:4000/health/readiness")
    HEALTH_CONTAINERS=("ods-litellm")
else
    _health_bind="127.0.0.1"
    _health_llama_host="$(macos_bind_probe_host "${_health_bind:-127.0.0.1}")"
    _health_llama_port="$(read_env_value "$INSTALL_DIR/.env" "ODS_NATIVE_LLAMA_PORT")"
    [[ "$_health_llama_port" =~ ^[0-9]+$ ]] || _health_llama_port="8080"
    HEALTH_NAMES=("LLM (llama-server)")
    HEALTH_URLS=("http://${_health_llama_host}:${_health_llama_port}/health")
    HEALTH_CONTAINERS=("")
fi
$ENABLE_OPEN_WEBUI && HEALTH_NAMES+=("Chat UI (Open WebUI)") \
    && HEALTH_URLS+=("http://127.0.0.1:3000") && HEALTH_CONTAINERS+=("ods-webui")
$ENABLE_VOICE && HEALTH_NAMES+=("Whisper (STT)") && HEALTH_URLS+=("http://127.0.0.1:${WHISPER_PORT:-9000}/health") && HEALTH_CONTAINERS+=("ods-whisper")
$ENABLE_WORKFLOWS && HEALTH_NAMES+=("n8n (Workflows)") && HEALTH_URLS+=("http://127.0.0.1:5678/healthz") && HEALTH_CONTAINERS+=("ods-n8n")
$ENABLE_OPENCODE && [[ -x "$OPENCODE_BIN" ]] && HEALTH_NAMES+=("OpenCode (IDE)") && HEALTH_URLS+=("http://127.0.0.1:${OPENCODE_PORT}") && HEALTH_CONTAINERS+=("")

for ((idx=0; idx<${#HEALTH_NAMES[@]}; idx++)); do
    NAME="${HEALTH_NAMES[$idx]}"
    URL="${HEALTH_URLS[$idx]}"
    CONTAINER="${HEALTH_CONTAINERS[$idx]}"
    HEALTHY=false

    for ((attempt=1; attempt<=MAX_ATTEMPTS; attempt++)); do
        if [[ -n "$CONTAINER" ]]; then
            # Docker service -- wait for the container healthcheck to report healthy.
            STATUS=$(docker inspect --format '{{.State.Health.Status}}' "$CONTAINER" 2>/dev/null || echo "missing")
            if [[ "$STATUS" == "healthy" ]]; then
                HEALTHY=true
                break
            fi
        else
            # Host-native service -- poll HTTP on 127.0.0.1.
            # Bound each probe: a listening-but-still-loading server accepts the
            # connection and would otherwise block past the loop's own budget
            # (matches the --max-time 10 used elsewhere in this installer).
            HTTP_CODE=$(curl -s --connect-timeout 5 --max-time 10 \
                -o /dev/null -w "%{http_code}" "$URL" 2>/dev/null || echo "000")
            if [[ "$HTTP_CODE" -ge 200 ]] && [[ "$HTTP_CODE" -lt 400 ]]; then
                HEALTHY=true
                break
            fi
            # 401/403 means service is responding (auth-protected) -- treat as healthy
            if [[ "$HTTP_CODE" == "401" ]] || [[ "$HTTP_CODE" == "403" ]]; then
                HEALTHY=true
                break
            fi
        fi
        if (( attempt <= 3 || attempt % 5 == 0 )); then
            ai "  Waiting for ${NAME}... (${attempt}/${MAX_ATTEMPTS})"
        fi
        sleep 2
    done

    if $HEALTHY; then
        ai_ok "${NAME}: healthy"
    else
        ai_warn "${NAME}: not responding after ${MAX_ATTEMPTS} attempts"
        ALL_HEALTHY=false
        if $CLOUD_MODE && (( idx < 2 )); then
            CLOUD_REQUIRED_HEALTHY=false
        fi
    fi
done

if $CLOUD_MODE; then
    _cloud_health_key="$(read_env_value "$INSTALL_DIR/.env" "LITELLM_KEY")"
    _cloud_health_bind="127.0.0.1"
    _cloud_health_host="$(macos_bind_probe_host "${_cloud_health_bind:-127.0.0.1}")"
    _cloud_health_port="$(read_env_value "$INSTALL_DIR/.env" "LITELLM_PORT")"
    [[ "$_cloud_health_port" =~ ^[0-9]+$ ]] || _cloud_health_port="4000"
    _cloud_auth_ok=false
    if [[ -n "$_cloud_health_key" ]]; then
        for _cloud_health_i in $(seq 1 30); do
            if curl -fsS --connect-timeout 2 --max-time 5 \
                -H @<(printf 'Authorization: Bearer %s\n' "$_cloud_health_key") \
                "http://${_cloud_health_host}:${_cloud_health_port}/v1/models" \
                >/dev/null 2>&1; then
                _cloud_auth_ok=true
                break
            fi
            sleep 1
        done
    fi
    if [[ "$_cloud_auth_ok" != "true" ]]; then
        ai_err "Authenticated LiteLLM readiness failed; cloud inference is not usable."
        CLOUD_REQUIRED_HEALTHY=false
        ALL_HEALTHY=false
    else
        ai_ok "LiteLLM authenticated model route: healthy"
    fi
    unset _cloud_health_key _cloud_health_bind _cloud_health_host \
        _cloud_health_port _cloud_health_i _cloud_auth_ok

    if [[ "$CLOUD_REQUIRED_HEALTHY" != "true" ]]; then
        ai_err "Required cloud inference services failed readiness; refusing to report a successful install."
        exit 1
    fi
fi

ods_macos_prepare_voice

ods_macos_configure_perplexica

# ── Pre-mark setup wizard complete ──
# The dashboard-api reads ${INSTALL_DIR}/data/config/setup-complete.json
# (mounted at /data/config/setup-complete.json inside the container) to
# decide first_run state. Writing this here prevents the wizard from
# reappearing on every visit after a fresh install. Non-fatal.
_setup_config_dir="${INSTALL_DIR}/data/config"
_setup_complete_file="${_setup_config_dir}/setup-complete.json"
_completed_at=$(date -u +%Y-%m-%dT%H:%M:%SZ)
if mkdir -p "${_setup_config_dir}" 2>/dev/null \
    && printf '{"completed_at": "%s", "version": "1.0.0"}\n' "${_completed_at}" > "${_setup_complete_file}" 2>/dev/null \
    && chmod 644 "${_setup_complete_file}" 2>/dev/null; then
    ai_ok "Setup wizard pre-marked complete"
else
    ai_warn "Could not write ${_setup_complete_file} (non-fatal)"
fi

# ── Success card ──
if ! $ALL_HEALTHY; then
    echo ""
    ai_warn "Some services may still be starting. Check with:"
    echo -e "  ${GRN}./ods-macos.sh status${NC}"
    echo ""
fi

{
    printf 'Dashboard|http://127.0.0.1:3001|ods-dashboard|http://localhost:3001\n'
    $ENABLE_OPEN_WEBUI && printf 'Chat UI (Open WebUI)|http://127.0.0.1:3000|ods-webui|http://localhost:3000\n'
    if $CLOUD_MODE; then
        printf 'LiteLLM|http://127.0.0.1:4000/health/readiness|ods-litellm|http://localhost:4000\n'
    else
        printf 'llama-server|http://%s:%s/health||http://localhost:%s/v1\n' "$_health_llama_host" "$_health_llama_port" "$_health_llama_port"
    fi
    printf 'Dashboard API|http://127.0.0.1:3002/health|ods-dashboard-api|http://localhost:3002\n'
    $ENABLE_PERPLEXICA && printf 'Perplexica|http://127.0.0.1:3004|ods-perplexica|http://localhost:3004\n'
    $ENABLE_VOICE && printf 'Whisper (STT)|http://127.0.0.1:%s/health|ods-whisper|http://localhost:%s\n' "${WHISPER_PORT:-9000}" "${WHISPER_PORT:-9000}"
    $ENABLE_WORKFLOWS && printf 'n8n|http://127.0.0.1:5678/healthz|ods-n8n|http://localhost:5678\n'
    if $ENABLE_OPENCODE && [[ -x "$OPENCODE_BIN" ]]; then
        printf 'OpenCode (IDE)|http://127.0.0.1:%s||http://localhost:%s\n' "$OPENCODE_PORT" "$OPENCODE_PORT"
    fi
} | ods_readiness_summary "./ods-macos.sh status" "$ODS_LOG_FILE" "http://localhost:3001" "Dashboard"

# get-ods.sh --force exports this when the reinstall removed a saved one.
if [[ "${ODS_REINSTALL_REMOTE_ROUTE_REMOVED:-false}" == "true" ]]; then
    ai_warn "This reinstall removed your model API connection; ODS uses the model on this computer."
    ai "To use the API again, connect it in Settings > Remote model."
fi

show_success_card