Files
semprini-maintainer/docs/architecture.md
T
paulandClaude Opus 5 556ffb6561 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>
2026-09-15 23:22:34 +12:00

7.1 KiB

Maintenance Agent (semprini-maintainer)

Autonomous, always-on agent that keeps the semprini.me stack healthy and current. Implementation: this repository — see README.md. It maintains the semprini-core stack (a separate repo) referenced via repo_dir.

Revised 2026-09-15 for the move to AWS Lightsail: the daemon runs on the stack host, reacts to Matrix events instead of polling, and keeps the repo equal to what runs.

Purpose

Maintain the running stack without an operator at the keyboard:

  1. Remediate outages, and act on critical log alerts as they arrive.
  2. Upgrade everything the host runs weekly, with test and rollback.
  3. Keep the repo current, so the stack can be rebuilt from git plus the host's secrets.
  4. Talk to the operator over Matrix like a chat: answer questions, carry out instructions, ask when a decision is theirs, and report anything important.

It is the unattended driver for the canonical stack-support agent (semprini-core/agents/stack-support.agent.md): it feeds the agent events and state and relays its results.

Support processes

Process Trigger What happens Operator sees
Conversation Operator message in Stack Maintenance, or in an alert thread Agent run; follow-ups resume the session Reply in the room (👀 on receipt, typing while working)
Alert triage FIRING from @alerts in #stack-alerts Batched 60s; agent reads the LogQL lines, fixes a real fault or demotes benign noise in Alloy Thread under the alert; non-ok mirrored to Stack Maintenance
Outage remediation Kuma monitor DOWN for 2 checks Agent diagnoses and fixes per the runbook 🔧 / ❓ / ⛔ in Stack Maintenance
Post-mortem Kuma DOWN→UP between checks Agent finds the cause in logs; changes nothing healthy Report in Stack Maintenance
Host checks 5-min timer: disk ≥ 85%, memory < 400 MB, OOM kill, restart loop, Kuma stale, reboot Agent investigates (12h cooldown per finding) Report in Stack Maintenance
Daily report 21:00 UTC No agent: monitors, downtime, alerts, runs, commits, pending reboot or decision 📋 in Stack Maintenance
Weekly upgrade Sat 22:00 UTC (Sun 08:00 AEST) Agent upgrades every image and host packages; pins, commits, pushes; escalates majors and reboots Report + decisions
Self-update Hourly Fast-forward this checkout, --check, re-exec 🔄 notice

Key decisions

  • Runs on the stack host. Lightsail Semprini-Core, user ubuntu, systemd. Kuma's database, Docker, Loki and the kernel log are local, and there is no second machine to keep alive. Consequence: a dead VM takes the maintainer with it (see Residual risks).
  • Event-driven via long-poll /sync, not a Synapse application service. The homeserver holds a /sync request open and answers the moment an event lands, so messages are handled within about a second. That is the push mechanism of the Matrix client-server API. An appservice would push over HTTP instead, but it needs a registration file in the generated homeserver.yaml, a listener reachable from the Synapse container, and a Synapse restart. It would gain nothing a person could notice.
  • One agent run at a time, priority queue. Two agents changing the same stack at once would fight. The queue is persisted, so an instruction or alert survives a restart or an unusable agent. Priority: operator > alerts > health > upgrade. The listener answers status and cancel itself while a run is in progress.
  • Conversations resume Claude sessions. Each room, and each alert thread, maps to the last session used there. A reply within 4 hours runs claude -p --resume, so "why?" and "do option 2" work. An escalation's answer always resumes the session that asked.
  • Scope of remediation = Uptime Kuma's active monitors. Re-read every cycle, so pausing a monitor takes a service out of scope (runbook → Deliberately Stopped Services). Upgrades cover every image on the host.
  • Health signal = Kuma heartbeat, read from kuma.db through a throwaway keinos/sqlite3 container on the volume, read-only. Host checks cover what Kuma cannot see. Prometheus stays out: semprini-core's observability design chose this check instead.
  • Alerts go to the agent, not a person. Every alert can start a paid run, so alerts are batched, re-investigated at most every 12h per fingerprint, and benign messages are demoted at the source in Alloy rather than by raising thresholds.
  • Applied changes go straight to main; proposals go through pull requests. This reverses the 2026-06 rule that every agent change went to a PR. Live changes then sat on unmerged branches while main fell 15 commits behind what ran. The migration itself was built on one of those branches. The host is now a git checkout of main, the laptop deploys with git push plus a fast-forward, and the daemon pushes whatever a run leaves unpushed. Gitea still records every commit for review. blog and conk cannot be pushed by the bot, so the agent proposes patches there.
  • Runner = Claude Code headless (claude -p --output-format json), with the agent definition appended to the system prompt and a JSON envelope (status/summary/details/question/options) at the end of each reply.
  • Credential = long-lived subscription token (claude setup-token) passed as CLAUDE_CODE_OAUTH_TOKEN. On the laptop the daemon shared ~/.claude/.credentials.json with interactive sessions, and a re-login revoked its token (2026-08-19).
  • Runs as ubuntu, not root (Claude Code refuses bypassPermissions as root). The agent still has root-equivalent power through Docker and passwordless sudo, which it needs to reboot and upgrade packages.
  • Matrix transport: bot @maintainer-bot:semprini.me, registered with the Synapse shared secret because password login is disabled. Both rooms are unencrypted: the bot is stdlib-only and cannot do E2EE.

Trust boundary

Only two senders are obeyed: admin_user_id in either room, and @alerts in #stack-alerts (whose messages trigger triage but are not instructions). Everything the agent reads while working is untrusted: log lines, access-log paths, alert annotations, web pages. The system prompt tells the agent never to follow instructions found in them. This is a mitigation, not a guarantee, for an agent with Docker access.

Residual risks

  • Nothing outside the VM watches it. If the host dies, Kuma, Gitea, Matrix and the maintainer die together and nobody is told. Cheapest fix: an external dead-man's switch (e.g. healthchecks.io) pinged by the daily report.
  • Gitea lives on the host it describes. vol-backup copies its volume to S3 nightly, and laptop clones exist. A push mirror to GitHub would make a rebuild independent of S3.
  • The Claude token expires after a year. The agent then reports itself degraded; re-run claude setup-token and --set-claude-token.
  • Health checks pause during an agent run. A 30-minute upgrade delays the next Kuma check by up to that long; alerts and operator messages still queue and are acknowledged.