Files
semprini-maintainer/install.sh
T
paulandClaude Opus 5.5 61dca2c3ca Run on the workstation, on pydantic-ai with cloud and local models
The daemon moves from the Lightsail host to WSL on the workstation (EVAI),
where the RTX 4090 can run local models and the data platform and home
cluster live. It reaches the core host over one multiplexed SSH connection:
each health check pipes probe.py to the host's python3 and gets Kuma, disk,
memory, OOM kills, restarts and reboots back in one snapshot. The agent edits
and commits core's live checkout there, as semprini-core's rules expect, and
the service desk still runs on the host. Off the host, it now notices when
the host itself stops answering.

The agent runs in-process with pydantic-ai instead of Claude Code headless.
Model profiles (anthropic, ollama, any OpenAI-compatible server) are routed
per kind of task: an unreachable model hands over at once (FallbackModel), a
run that fails as a run restarts on the next profile. The result is a typed
envelope; conversations persist as message histories; cancel stops the
command in flight; each reply names the model and its cost. `model` in Matrix
pins a room to a profile; --try-model checks a model calls tools and uses
what they return (qwen3-coder does not; GLM-4.7-Flash, the local default, does).

Cloud runs now need an API key (--set-api-key) and are billed per token; the
subscription token was for Claude Code only. Back-off is per kind of task, so
a cloud-only upgrade waiting on a key does not stall work the local model
can do. A queued task held back by a hold no longer spins the main loop.

--hold and --resume stay stdlib-only, so sync-to-host.sh can still hold the
daemon from the host's checkout; the daemon reads the host's hold each minute.
install.sh sets up WSL and retires the host's service: one Matrix bot, one
daemon.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 01:46:03 +12:00

120 lines
4.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# install.sh — install & enable the semprini-maintainer systemd service.
#
# Run on the operator's workstation, in WSL (Ubuntu, systemd enabled), from
# this checkout, as the user the service runs as (not root; sudo is used for
# the systemd parts). Idempotent.
#
# It also takes over from the daemon on the core host: the two share one
# Matrix bot, so they must never run at once. Just before the local service
# starts, the host's service is stopped and disabled. The host keeps its
# checkout and its git credentials: sync-to-host.sh's `--hold` and the host's
# own pushes still use them.
set -euo pipefail
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
UNIT="semprini-maintainer.service"
DEST="/etc/systemd/system/$UNIT"
USER_NAME="$(id -un)"
die() { echo "ERROR: $*" >&2; exit 1; }
[ "$USER_NAME" != root ] || die "run as the service user, not root"
echo "→ Pre-flight checks"
grep -qi microsoft /proc/version || echo "WARN: this does not look like WSL; carrying on"
[ "$(ps -p 1 -o comm=)" = systemd ] ||
die "systemd is not running: add [boot] systemd=true to /etc/wsl.conf, then wsl --shutdown"
for bin in python3 git ssh curl; do
command -v "$bin" >/dev/null || die "$bin not found"
done
case "$DIR" in /mnt/*) echo "WARN: $DIR is on the Windows filesystem; clone into ~ for speed and file modes";; esac
if [ ! -f "$DIR/config.json" ]; then
die "config.json missing. Copy config.example.json to config.json, bring the tokens
over from the host's config.json (matrix, gitea), and adjust the paths."
fi
chmod 600 "$DIR/config.json"
UV="$HOME/.local/bin/uv"
if [ ! -x "$UV" ]; then
echo "→ Installing uv"
curl -LsSf https://astral.sh/uv/install.sh | sh >/dev/null
fi
echo "→ Installing dependencies into .venv"
( cd "$DIR" && "$UV" sync --frozen --quiet )
PY="$DIR/.venv/bin/python"
cfg_get() {
"$PY" - "$DIR" "$1" <<'PY'
import sys
sys.path.insert(0, sys.argv[1])
import maintainer
cfg = maintainer.load_config(sys.argv[1] + "/config.json")
if sys.argv[2] == "git-credential":
env = maintainer.agent_git_env(cfg) # also writes the credential store
if env:
print(cfg["gitea"]["web_base"].rstrip("/"), env["GIT_CONFIG_VALUE_2"])
elif sys.argv[2] == "core-ssh":
print(cfg["hosts"][cfg["repo_host"]]["ssh"])
else:
print(cfg[sys.argv[2]])
PY
}
CORE="$(cfg_get core-ssh)"
echo "→ SSH to the core host ($CORE)"
if ! ssh -o BatchMode=yes -o ConnectTimeout=15 "$CORE" true 2>/dev/null; then
cat >&2 <<EOF
ERROR: cannot reach $CORE over SSH without a password. Once:
ssh-keygen -t ed25519 -N "" -C "semprini-maintainer@\$(hostname)" -f ~/.ssh/id_ed25519
ssh-keyscan -H ${CORE#*@} >> ~/.ssh/known_hosts
# from a machine that can already log in (Windows PowerShell, say):
# type \\\\wsl\$\\Ubuntu\\home\\$USER_NAME\\.ssh\\id_ed25519.pub | ssh $CORE "cat >> ~/.ssh/authorized_keys"
Then run install.sh again.
EOF
exit 1
fi
echo "→ Checking maintainer.py, the config, the hosts and the models"
"$PY" "$DIR/maintainer.py" --config "$DIR/config.json" --check
# The daemon's own checkout fetches from Gitea as claude-code (self-update).
# Scoped to this repository: WSL is also the operator's shell.
read -r WEB HELPER <<<"$(cfg_get git-credential)" || true
if [ -n "${WEB:-}" ]; then
echo "→ Git credential helper for $WEB, in this checkout only"
git -C "$DIR" config "credential.$WEB.helper" "$HELPER"
else
echo "WARN: no Gitea token — the agent cannot open pull requests. Copy it from the host's config.json."
fi
echo "→ Installing $UNIT"
sed -e "s|@USER@|$USER_NAME|g" -e "s|@HOME@|$HOME|g" -e "s|@DIR@|$DIR|g" \
"$DIR/$UNIT" | sudo tee "$DEST" >/dev/null
sudo systemctl daemon-reload
sudo systemctl enable "$UNIT"
echo "→ Retiring the daemon on $CORE (one Matrix bot, one daemon)"
ssh -o BatchMode=yes "$CORE" '
if systemctl is-enabled --quiet semprini-maintainer 2>/dev/null ||
systemctl is-active --quiet semprini-maintainer 2>/dev/null; then
sudo systemctl disable --now semprini-maintainer && echo " stopped and disabled"
else
echo " not running there"
fi' || die "could not stop the host's daemon; stop it by hand before starting this one:
ssh $CORE sudo systemctl disable --now semprini-maintainer"
sudo systemctl restart "$UNIT"
cat <<EOF
✓ Installed and started.
Logs: journalctl -u $UNIT -f
Status: systemctl status $UNIT (or send 'status' in Matrix)
Stop: sudo systemctl stop $UNIT
Models: $PY $DIR/maintainer.py --try-model local (and cloud)
WSL stops when nothing runs in it. Keep it up with a Windows scheduled task
that starts at logon (PowerShell or cmd, one line):
schtasks /Create /TN "WSL keep-alive" /SC ONLOGON /RL LIMITED /F /TR "wsl.exe -d Ubuntu --exec sleep infinity"
EOF