Run on the stack host, react to Matrix events, keep the repo current

The stack moved to AWS Lightsail, so the maintainer now runs on that host
as ubuntu and is triggered by events rather than polling:

- A long-poll /sync delivers operator messages and #stack-alerts within
  about a second. Follow-ups resume the Claude session, so the Stack
  Maintenance room works like a chat. status, cancel, new, upgrade and
  help are answered even while a run is in progress.
- FIRING alerts from the Alertmanager bridge are batched and triaged in a
  thread, with a per-fingerprint cooldown.
- Agent runs go one at a time from a persisted priority queue, so an
  unusable agent defers work instead of losing it.
- Health checks add host resources (disk, memory, OOM kills, restart
  loops, stale Kuma) and reboot detection; one cycle's findings share a run.
- The daily report and the weekly upgrade run at fixed UTC times; the
  upgrade covers every image on the host.
- Applied changes are committed to semprini-core main and pushed. The
  daemon fast-forwards before a run and pushes leftovers after it.
- The Claude credential comes from `claude setup-token` via
  --set-claude-token. Self-update runs hourly, gated by --check.
- install.sh renders the unit for the current user and configures the git
  credential helper.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
paulandClaude Opus 5 committed 2026-09-15 23:22:34 +12:00
1 parent 46a1ec1e01
commit 556ffb6561
7 files changed
+2132 -863

No files matched your search

+45 -22
View File
@@ -1,44 +1,67 @@
#!/usr/bin/env bash
# install.sh — install & enable the semprini-maintainer systemd service.
#
# Run from this directory. Requires sudo for the systemd parts.
# Run on the stack host, from this checkout, as the user the service runs as
# (not root; sudo is used for the systemd parts). Idempotent.
set -euo pipefail
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
UNIT="semprini-maintainer.service"
DEST="/etc/systemd/system/$UNIT"
USER_NAME="$(id -un)"
[ "$USER_NAME" != root ] || { echo "ERROR: run as the service user, not root"; exit 1; }
echo "→ Pre-flight checks"
command -v docker >/dev/null || { echo "ERROR: docker not found"; exit 1; }
command -v python3 >/dev/null || { echo "ERROR: python3 not found"; exit 1; }
CLAUDE_BIN="$(python3 -c "import json;print(json.load(open('$DIR/config.json'))['claude_bin'])" 2>/dev/null || echo /home/paul/.local/bin/claude)"
[ -x "$CLAUDE_BIN" ] || echo "WARN: claude binary not executable at $CLAUDE_BIN"
for bin in docker python3 git curl; do
command -v "$bin" >/dev/null || { echo "ERROR: $bin not found"; exit 1; }
done
id -nG | grep -qw docker || echo "WARN: $USER_NAME is not in the docker group"
if [ ! -f "$DIR/config.json" ]; then
echo "ERROR: config.json missing. Run:"
echo " cp $DIR/config.example.json $DIR/config.json"
echo " $DIR/register-matrix-bot.sh"
echo "ERROR: config.json missing. Copy config.example.json to config.json and"
echo " adjust the paths, then run register-matrix-bot.sh and register-gitea-bot.sh."
exit 1
fi
chmod 600 "$DIR/config.json"
TOKEN="$(python3 -c "import json;print(json.load(open('$DIR/config.json'))['matrix']['access_token'])")"
case "$TOKEN" in
""|FILLED_BY_*) echo "ERROR: Matrix not provisioned. Run register-matrix-bot.sh first."; exit 1;;
esac
cfg_get() {
python3 - "$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"])
else:
print(cfg[sys.argv[2]])
PY
}
# Gitea (push/PR capability) is optional — warn but don't block if unprovisioned.
GTOK="$(python3 -c "import json;print(json.load(open('$DIR/config.json')).get('gitea',{}).get('token',''))")"
case "$GTOK" in
""|FILLED_BY_*) echo "WARN: Gitea not provisioned — the agent will not push or open PRs.";
echo " Run register-gitea-bot.sh to enable it.";;
esac
CLAUDE_BIN="$(cfg_get claude_bin)"
if [ ! -x "$CLAUDE_BIN" ]; then
echo "→ Installing Claude Code"
curl -fsSL https://claude.ai/install.sh | bash
fi
echo "→ Syntax-checking maintainer.py"
echo "→ Checking maintainer.py and config"
python3 -m py_compile "$DIR/maintainer.py"
python3 "$DIR/maintainer.py" --config "$DIR/config.json" --check
# Git on this host authenticates to Gitea as claude-code — for the agent, for
# the daemon's own pushes, and for sync-to-host.sh's fast-forward alike.
read -r WEB HELPER <<<"$(cfg_get git-credential)" || true
if [ -n "${WEB:-}" ]; then
echo "→ Git credential helper for $WEB"
git config --global "credential.$WEB.helper" "$HELPER"
else
echo "WARN: no Gitea token — the agent cannot push. Run register-gitea-bot.sh."
fi
echo "→ Installing $UNIT"
sudo cp "$DIR/$UNIT" "$DEST"
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"
sudo systemctl restart "$UNIT"
@@ -46,5 +69,5 @@ sudo systemctl restart "$UNIT"
echo
echo "✓ Installed and started."
echo " Logs: journalctl -u $UNIT -f"
echo " Status: systemctl status $UNIT"
echo " Status: systemctl status $UNIT (or send 'status' in Matrix)"
echo " Stop: sudo systemctl stop $UNIT"