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:
1 parent
46a1ec1e01
commit
556ffb6561
7 files changed
+2132
-863
No files matched your search
+88
-60
@@ -1,75 +1,103 @@
|
||||
# Maintenance Agent (semprini-maintainer)
|
||||
|
||||
Autonomous, always-on agent that keeps the container stack healthy and current.
|
||||
Implementation: this repository — see [`README.md`](../README.md). It maintains
|
||||
the **semprini-core** stack (a separate repo) referenced via `repo_dir`.
|
||||
Autonomous, always-on agent that keeps the semprini.me stack healthy and current.
|
||||
Implementation: this repository — see [`README.md`](../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
|
||||
|
||||
Continuously maintain the running stack without an operator at the keyboard:
|
||||
Maintain the running stack without an operator at the keyboard:
|
||||
|
||||
1. Detect and **remediate** service outages.
|
||||
2. Periodically **check for and apply container upgrades** (with test + rollback).
|
||||
3. **Communicate with the operator over Matrix** (chat.semprini.me), including
|
||||
asking questions when an upgrade needs a human decision.
|
||||
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
|
||||
(`agents/stack-support.agent.md`) — it does not re-implement that logic, it
|
||||
feeds the agent state and relays its results.
|
||||
(`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
|
||||
|
||||
- **Scope = Uptime Kuma's live monitor list.** Read every health cycle, never
|
||||
hard-coded. Anything Kuma tracks is in scope — including services owned by
|
||||
other projects (e.g. `semprini-blog`) that register Kuma monitors. This keeps
|
||||
the maintained set and the monitored set identical by construction.
|
||||
- **Health signal = Kuma heartbeat.** The daemon reads Kuma's own up/down
|
||||
result rather than re-probing, reusing the stack's existing monitoring.
|
||||
- **Kuma data access** via a throwaway `keinos/sqlite3` container mounting the
|
||||
`uptime-kuma-data` volume read-only (the daemon user has Docker but not host
|
||||
access to root-owned volume files).
|
||||
- **Runner = Claude Code headless** (`claude -p`), with
|
||||
`agents/stack-support.agent.md` injected as the system prompt. The agent
|
||||
returns a JSON envelope (`status/summary/question/options`) the daemon acts on.
|
||||
- **Runs as `paul`** (in the `docker` group), not root — Claude Code refuses
|
||||
`bypassPermissions` as root.
|
||||
- **Autonomy = full** by default: remediate and apply safe upgrades; escalate
|
||||
only major/breaking upgrades to Matrix and resume on reply. Configurable to
|
||||
`fix-only` or `notify`.
|
||||
- **Matrix transport**: dedicated bot `@maintainer:semprini.me` (created via the
|
||||
Synapse registration shared secret) in a private room with `@paul:semprini.me`.
|
||||
- **Code changes go through pull requests.** When the agent edits tracked files
|
||||
it branches, pushes, and opens a PR on Gitea (`git.semprini.me`) as the
|
||||
`claude-code` user rather than committing to `main`, giving the operator a
|
||||
review gate. Live remediation (restart/redeploy) is *not* gated — only the git
|
||||
change is. claude-code is an OIDC-linked, push-capable collaborator; since
|
||||
git.semprini.me disables password login + HTTP basic auth, `register-gitea-bot.sh`
|
||||
mints a personal access token via the gitea admin CLI inside the `user-gitea`
|
||||
container (mirroring the Matrix bot's shared-secret pattern). A PAT still works
|
||||
for git-over-HTTPS and the API, so no SSH exposure or core-stack change is
|
||||
required. The daemon injects the bot's git identity + credential (`GIT_CONFIG_*`,
|
||||
scoped to the Gitea host so it overrides the repo's own `user.*`) and the API
|
||||
coordinates into the headless agent; the agent opens the PR itself.
|
||||
- **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.
|
||||
|
||||
## Cadence
|
||||
## Trust boundary
|
||||
|
||||
| Loop | Default interval | Config key |
|
||||
|---|---|---|
|
||||
| Health check + remediation | 5 min | `health_interval_seconds` |
|
||||
| Upgrade review | 7 days | `upgrade_interval_seconds` |
|
||||
| Matrix reply polling | 30 s | `matrix_poll_seconds` |
|
||||
| Quiet health heartbeat | daily | `heartbeat_interval_seconds` |
|
||||
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.
|
||||
|
||||
## Escalation flow
|
||||
## Residual risks
|
||||
|
||||
1. Agent returns `status=escalate` with a `question` + `options`.
|
||||
2. Daemon posts a numbered question to Matrix and pauses upgrade cycles.
|
||||
3. Admin replies (a number or free text).
|
||||
4. Daemon feeds the decision back to the agent, which carries it out and reports.
|
||||
|
||||
## Operational notes
|
||||
|
||||
- The bot DM room invite must be accepted once by `@paul` in Element.
|
||||
- Secrets (`config.json`, `.bot-secrets`) are gitignored.
|
||||
- A single unanswered escalation blocks new upgrade cycles (health remediation
|
||||
continues regardless).
|
||||
- **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.
|
||||
Reference in new issue
Block a user