Files
ai-agent/sidecar/install.sh
gabrielvidalandClaude Opus 5.5 94c07968d3 feat(plugins): harnessHooks[] — plugins hook into the agent harness; gcloud_reauth leaves the core
A live plugin declares harnessHooks[]: declarative rules (command/output
regex → additionalContext, evaluated by the stdlib sidecar/plugin_hooks.py,
no plugin code — owner-approvable) or .py script hooks (operator approval).
Regexes compiled + ReDoS-checked at install, paths kept inside the plugin,
the declaration in the approval fingerprint (unchanged for plugins without
it). Placed by the plugin's where/profiles/workers settings; guests'
computers and scoped runs only get guestAllowed hooks.

The lab reads /data/plugins/live-hooks (PluginStore.sync_hooks on every
owner plugin change and at start); a worker gets its filtered copy in the
/sync bundle's hooks map (~/.ai-agent-worker/hooks). _claude_settings wires
them in place of the hard-coded gcloud hook; sidecar/gcloud_reauth.py and its
test are gone — the gcloud-reauth plugin (ai-agent-plugins) carries it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-26 19:12:32 +01:00

140 lines
7.2 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# install.sh — set up the claude-sidecar as a systemd *user* service.
#
# The sidecar must run natively on the host (not in Docker) as the repo owner so
# the `claude -p` sessions it spawns use the real ~/.claude auth, hooks and
# skills. This installs a venv, writes a systemd user unit, and starts it.
#
# Reboot survival needs lingering (`sudo loginctl enable-linger $USER`); we try
# it but it's fine if it fails — the service still runs for the current login.
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$HERE/.." && pwd)" # sidecar/ -> repo root
VENV="$HERE/.venv"
PORT="${SIDECAR_PORT:-8790}"
UNIT_DIR="$HOME/.config/systemd/user"
UNIT="$UNIT_DIR/claude-sidecar.service"
# The .env holding SIDECAR_TOKEN (+ optional SIDECAR_PORT), single source of
# truth shared with the docker-compose deployment. Override with
# SIDECAR_ENV_FILE; defaults to this repo's .env, then the homelab checkout's
# (where the compose stack that proxies to this sidecar reads it from).
ENV_FILE="${SIDECAR_ENV_FILE:-}"
if [[ -z "$ENV_FILE" ]]; then
# The first one that actually holds the token: this repo's .env can exist
# for other settings, and grepping a token-less file dies under pipefail.
for f in "$PROJECT_ROOT/.env" "$HOME/homelab/.env"; do
[[ -f "$f" ]] && grep -q '^SIDECAR_TOKEN=' "$f" && { ENV_FILE="$f"; break; }
done
fi
if [[ -n "$ENV_FILE" && -f "$ENV_FILE" ]]; then
TOKEN="$(grep -E '^SIDECAR_TOKEN=' "$ENV_FILE" | head -1 | cut -d= -f2-)"
PORT="$(grep -E '^SIDECAR_PORT=' "$ENV_FILE" | head -1 | cut -d= -f2- || true)"
PORT="${PORT:-8790}"
ACCOUNTS_ENV="$(grep -E '^SIDECAR_ACCOUNTS=' "$ENV_FILE" | head -1 | cut -d= -f2- || true)"
OC_ACCOUNTS_ENV="$(grep -E '^SIDECAR_OPENCODE_ACCOUNTS=' "$ENV_FILE" | head -1 | cut -d= -f2- || true)"
OC_KEYLESS_ENV="$(grep -E '^SIDECAR_OPENCODE_KEYLESS=' "$ENV_FILE" | head -1 | cut -d= -f2- || true)"
AGY_ACCOUNTS_ENV="$(grep -E '^SIDECAR_AGY_ACCOUNTS=' "$ENV_FILE" | head -1 | cut -d= -f2- || true)"
fi
[[ -n "${TOKEN:-}" ]] || { echo "error: SIDECAR_TOKEN not found (looked in ${ENV_FILE:-\$PROJECT_ROOT/.env, ~/homelab/.env})" >&2; exit 1; }
# Where spawned `claude -p` sessions start. Defaults to the dir the .env came
# from (the homelab checkout there), overridable with SIDECAR_CWD.
SIDECAR_CWD="${SIDECAR_CWD:-$(dirname "$ENV_FILE")}"
# Claude accounts a run can launch on besides the default (~/.claude): comma-
# separated id=config-dir pairs, exported per run as CLAUDE_CONFIG_DIR. From the
# environment, else the .env's SIDECAR_ACCOUNTS, else a work seat at
# ~/.claude-work when that dir exists (scripts/setup-claude-work.sh makes it).
SIDECAR_ACCOUNTS="${SIDECAR_ACCOUNTS:-${ACCOUNTS_ENV:-}}"
if [[ -z "$SIDECAR_ACCOUNTS" && -d "$HOME/.claude-work" ]]; then
SIDECAR_ACCOUNTS="work=$HOME/.claude-work"
fi
# opencode accounts this host runs (id=data-dir pairs, `default` = opencode's
# own store) and the keyless ones among them (no ChatGPT login: pi's local
# models only — the lab's `opencode-lab`). Same sources as SIDECAR_ACCOUNTS.
SIDECAR_OPENCODE_ACCOUNTS="${SIDECAR_OPENCODE_ACCOUNTS:-${OC_ACCOUNTS_ENV:-}}"
SIDECAR_OPENCODE_KEYLESS="${SIDECAR_OPENCODE_KEYLESS:-${OC_KEYLESS_ENV:-}}"
OC_UNIT_ENV=""
[[ -n "$SIDECAR_OPENCODE_ACCOUNTS" ]] && OC_UNIT_ENV+="Environment=SIDECAR_OPENCODE_ACCOUNTS=$SIDECAR_OPENCODE_ACCOUNTS"$'\n'
[[ -n "$SIDECAR_OPENCODE_KEYLESS" ]] && OC_UNIT_ENV+="Environment=SIDECAR_OPENCODE_KEYLESS=$SIDECAR_OPENCODE_KEYLESS"$'\n'
# agy accounts (Google logins for the Antigravity CLI, `google-personal` = the
# host user's own ~/.gemini login). Same sources.
SIDECAR_AGY_ACCOUNTS="${SIDECAR_AGY_ACCOUNTS:-${AGY_ACCOUNTS_ENV:-}}"
[[ -n "$SIDECAR_AGY_ACCOUNTS" ]] && OC_UNIT_ENV+="Environment=SIDECAR_AGY_ACCOUNTS=$SIDECAR_AGY_ACCOUNTS"$'\n'
# The harness and provider plugins (strict plugin-only: the sidecar has no
# built-in driver). The hub materializes them into its /data volume — on the
# homelab the shim's services/ai-agent/data — so the host sidecar reads that
# dir; before the hub ever wrote it, the sidecar falls back to the seed
# plugins (SIDECAR_SEED_PLUGINS_DIR, default ~/projects/ai-agent-plugins/plugins).
HUB_DATA="${SIDECAR_HUB_DATA_DIR:-$SIDECAR_CWD/services/ai-agent/data}"
SIDECAR_LIVE_HARNESSES_DIR="${SIDECAR_LIVE_HARNESSES_DIR:-$HUB_DATA/plugins/live-harnesses}"
SIDECAR_LIVE_PROVIDERS_DIR="${SIDECAR_LIVE_PROVIDERS_DIR:-$HUB_DATA/plugins/live-providers}"
OC_UNIT_ENV+="Environment=SIDECAR_LIVE_HARNESSES_DIR=$SIDECAR_LIVE_HARNESSES_DIR"$'\n'
OC_UNIT_ENV+="Environment=SIDECAR_LIVE_PROVIDERS_DIR=$SIDECAR_LIVE_PROVIDERS_DIR"$'\n'
# The owner's plugins' harnessHooks that apply to the lab (plugin_hooks.py).
SIDECAR_LIVE_HOOKS_DIR="${SIDECAR_LIVE_HOOKS_DIR:-$HUB_DATA/plugins/live-hooks}"
OC_UNIT_ENV+="Environment=SIDECAR_LIVE_HOOKS_DIR=$SIDECAR_LIVE_HOOKS_DIR"$'\n'
[[ -n "${SIDECAR_SEED_PLUGINS_DIR:-}" ]] && OC_UNIT_ENV+="Environment=SIDECAR_SEED_PLUGINS_DIR=$SIDECAR_SEED_PLUGINS_DIR"$'\n'
# The interface uvicorn listens on. Its only client is the backend container,
# which reaches the host through `host.docker.internal` = docker's host-gateway,
# i.e. the default bridge's gateway (docker0). Binding there instead of 0.0.0.0
# keeps the run API off the LAN and the tailnet. SIDECAR_BIND overrides (e.g.
# 0.0.0.0 on a box where the container reaches the host some other way).
if [[ -z "${SIDECAR_BIND:-}" ]]; then
SIDECAR_BIND="$(ip -4 -o addr show docker0 2>/dev/null | awk '{print $4}' | cut -d/ -f1 | head -1)"
SIDECAR_BIND="${SIDECAR_BIND:-127.0.0.1}"
fi
echo "==> venv + deps ($VENV)"
command -v uv >/dev/null || { echo "error: 'uv' not found on PATH" >&2; exit 1; }
[[ -d "$VENV" ]] || uv venv "$VENV"
uv pip install --quiet --python "$VENV" -r "$HERE/requirements.txt"
echo "==> writing $UNIT"
mkdir -p "$UNIT_DIR"
cat > "$UNIT" <<EOF
[Unit]
Description=claude-sidecar — spawns \`claude -p\` sessions for the ai-agent viewer
After=network-online.target
# Bound to the docker bridge (SIDECAR_BIND): until docker has created it the
# bind fails, so keep retrying instead of hitting systemd's start limit.
StartLimitIntervalSec=0
[Service]
Type=simple
WorkingDirectory=$HERE
Environment=SIDECAR_TOKEN=$TOKEN
Environment=SIDECAR_CWD=$SIDECAR_CWD
Environment=SIDECAR_ACCOUNTS=$SIDECAR_ACCOUNTS
${OC_UNIT_ENV}Environment=PATH=$HOME/.local/bin:/usr/local/bin:/usr/bin:/bin
ExecStart=$VENV/bin/uvicorn sidecar:app --host $SIDECAR_BIND --port $PORT
Restart=on-failure
RestartSec=3
# Spawned \`claude -p\` runs are detached daemons (start_new_session). KillMode=
# process makes systemd signal only uvicorn on stop/restart, so redeploying the
# sidecar (e.g. while Claude is editing it) leaves those in-flight runs alive;
# the restarted sidecar re-discovers them from logs/*.pid.
KillMode=process
[Install]
WantedBy=default.target
EOF
echo "==> enabling + starting"
systemctl --user daemon-reload
systemctl --user enable --now claude-sidecar.service
sleep 1
systemctl --user --no-pager status claude-sidecar.service | head -8 || true
# Survive logout/reboot (needs privileges; ignore failure).
loginctl enable-linger "$USER" 2>/dev/null \
&& echo "==> lingering enabled (survives logout/reboot)" \
|| echo "note: run 'sudo loginctl enable-linger $USER' for reboot survival"
echo "==> done. health: curl -s $SIDECAR_BIND:$PORT/health"