236 lines
14 KiB
Markdown
236 lines
14 KiB
Markdown
# semprini-maintainer — autonomous stack maintenance agent
|
|
|
|
A systemd service on the core host it keeps, AWS Lightsail `Semprini-Core`, keeping the
|
|
semprini.me core stack healthy and current:
|
|
- acts on critical log alerts the moment they are posted
|
|
- upgrades everything the core host runs, weekly
|
|
- keeps the semprini-core repo equal to what runs
|
|
- talks to the operator over Matrix (chat.semprini.me) like a chat
|
|
|
|
It is the *unattended driver* for the canonical stack-support agent in the **semprini-core**
|
|
repo (`agents/stack-support.agent.md`). The daemon notices events and hands the judgement
|
|
calls to that agent, which it runs in-process with [pydantic-ai](https://ai.pydantic.dev) on
|
|
Claude (Sonnet 5.5) through the Anthropic API. Any model pydantic-ai supports can be
|
|
configured instead. Design and decisions: [`docs/architecture.md`](docs/architecture.md).
|
|
|
|
## Talking to it
|
|
|
|
Two rooms on chat.semprini.me:
|
|
|
|
| Room | Written by | What happens |
|
|
|---|---|---|
|
|
| **Stack Maintenance** | you and `@maintainer-bot` | Your messages are instructions or questions: 👀 shows it arrived, typing shows it is working, and the reply lands in the room. A follow-up within 4 hours continues the same conversation. Decisions it needs arrive here as numbered options; answer with a number or in words. Reports and the daily summary go here too. |
|
|
| **Stack alerts** (`#stack-alerts`) | `@alerts` (Alertmanager bridge) | Each FIRING alert is triaged and answered in a thread under it. Reply in the thread to discuss it. Anything that is not simply "ok" is repeated in Stack Maintenance. |
|
|
|
|
Messages are read with a long-poll `/sync`: the homeserver answers as soon as something is
|
|
posted, so there is no polling delay. Every reply ends with a small line saying which model
|
|
answered and how long it took.
|
|
|
|
A message that is exactly one of these is handled at once, even mid-run:
|
|
|
|
| Command | Effect |
|
|
|---|---|
|
|
| `status` | What it is doing, the queue, the models, health, host, next upgrade |
|
|
| `cancel` / `stop` | Stop the agent run in progress, and the command it is running |
|
|
| `hold 45m <why>` | Stand down for a while: `hold` alone is 30 minutes, 8 hours is the most. Your messages still run and alerts still collect; health checks, triage, upgrades and the service desk wait, and it says so when the hold starts and when it runs out |
|
|
| `resume` | End a hold now |
|
|
| `new` | Forget this room's conversation |
|
|
| `model` | The model it runs on. With more than one configured, `model <name>` puts that one first for your messages in this room, and `model auto` goes back to the default |
|
|
| `upgrade` | Run the weekly upgrade now |
|
|
| `help` | The list |
|
|
|
|
## What it does
|
|
|
|
| Trigger | Action |
|
|
|---|---|
|
|
| Operator message | Agent run; the reply goes to the same room or thread |
|
|
| FIRING alert in `#stack-alerts` | Batched for 60s, then one triage run: read the LogQL lines, fix a real fault, or demote benign noise in Alloy. Each fingerprint is re-investigated at most every 12h; `PipelineTest` is only acknowledged |
|
|
| Every 5 min | Health check of the core host, below. Any findings become one agent run |
|
|
| — Kuma monitor DOWN for 2 checks | Diagnose and remediate |
|
|
| — Kuma monitor DOWN→UP between checks | Post-mortem from logs; changes nothing healthy |
|
|
| — Disk ≥ 85%, memory < 400 MB, kernel OOM kill, restart loop, stale Kuma | Investigate and fix; 12h cooldown per finding |
|
|
| — Core host rebooted | Verify every stack came back |
|
|
| — Health probe fails for 3 checks | 🚨 in Stack Maintenance (delivered once Matrix is back) |
|
|
| Daily, 21:00 UTC | 📋 report: monitors, downtime, alerts, agent runs, commits, pending reboot or decision |
|
|
| Weekly, Sat 22:00 UTC | Upgrade every image and host package on core: test, pin, commit, push, roll back on failure. Majors and reboots are escalated |
|
|
| Every minute | Service desk: semprini-core's `service-desk.py`, run on the core host, fulfils the open requests in [Semprini/service-desk](https://git.semprini.me/Semprini/service-desk). What needs you is posted here: 🙋 an approval (`/approve` or `/reject` on the issue), ⛔ a failure (`/retry` once fixed), 📨 an "Ask core" request, ✅ a finished one. Waits out a hold |
|
|
| Hourly | Self-update from this repo |
|
|
|
|
Scope for remediation is **whatever Uptime Kuma tracks at check time**, re-read every
|
|
cycle. Pausing a Kuma monitor is how a service is taken out of scope; see semprini-core
|
|
`docs/stack-support-runbook.md` → *Deliberately Stopped Services*. Upgrades cover every
|
|
image on the core host.
|
|
|
|
```
|
|
Stack Maintenance ──┐ long-poll /sync (HTTPS)
|
|
#stack-alerts ──────┤
|
|
▼
|
|
┌──────────── Semprini-Core (Lightsail): maintainer.py ─────────────┐
|
|
│ listener thread: commands now, tasks → queue │
|
|
│ worker: one agent run at a time │
|
|
│ (operator > alerts > health > upgrade) │
|
|
│ agent.py (pydantic-ai): shell · read/write/edit file · fetch_url │
|
|
│ probe.py: Kuma, disk, memory, OOM kills, restarts, reboot │
|
|
│ service-desk.py · ~/semprini-core: the agent edits, commits and │
|
|
│ pushes here │
|
|
└───────┬──────────────────────────┬────────────────────────────────┘
|
|
│ model (HTTPS) │ kubectl, ssh through the WireGuard tunnel
|
|
▼ ▼
|
|
Anthropic API The home cluster VMs: k3s and containers, while the
|
|
Claude Sonnet 5.5 workstation hosting them is on; Hyper-V is the operator's
|
|
```
|
|
|
|
The agent's result is a typed envelope the daemon relays:
|
|
|
|
```json
|
|
{"status":"ok|action_taken|escalate|blocked","summary":"…","details":"…","question":"…","options":["…"]}
|
|
```
|
|
|
|
## Models
|
|
|
|
One Claude model does everything: your messages, alert triage, health findings and the weekly
|
|
upgrade. In `config.json`:
|
|
|
|
```json
|
|
"models": {
|
|
"claude": {"model": "anthropic:claude-sonnet-5-5", "context_tokens": 200000, "api_key": "…"}
|
|
},
|
|
"routing": {"operator": ["claude"], "alerts": ["claude"], "health": ["claude"], "upgrade": ["claude"]}
|
|
```
|
|
|
|
- A profile is `provider:model`. `ollama:` and `anthropic:` are built in; `openai:` with a
|
|
`base_url` is any OpenAI-compatible server (llama.cpp's `llama-server`, vLLM, LM Studio);
|
|
anything else pydantic-ai knows (`openai:gpt-…`, `google-gla:…`, `openrouter:…`) works with
|
|
its key in `api_key` (`--set-api-key <profile>`) or the provider's environment variable.
|
|
`settings` passes model settings through. `context_tokens` is the window the model is run
|
|
with; older tool output is shortened as a conversation nears it. Claude's 200k is below its
|
|
real window on purpose: it caps what one long run re-sends, and pays for, at every step.
|
|
- `routing` lists the profiles each kind of task tries, best first. With a second profile in a
|
|
list, an unreachable model hands the run to the next at once, and a run that fails as a run
|
|
(it loops until its budget is spent, or cannot produce the result) starts again on the next,
|
|
told which commands the failed attempt ran. Today there is no second profile, so no fallback.
|
|
- `limits` caps each run: `request_limit`, `tool_calls_limit`, and `cost_usd` per kind of task
|
|
($5, $15 for the upgrade), which only a priced model is held to; Claude Sonnet 5.5 is priced.
|
|
- Claude is billed per token and needs an API key: `.venv/bin/python maintainer.py
|
|
--set-api-key claude` prompts for it without echo, checks it with one request and saves it
|
|
in `config.json`. A Claude subscription token (from `claude setup-token`) is for Claude Code
|
|
only and does not work here.
|
|
|
|
Until 2026-11-26 the daemon ran in WSL on the operator's workstation, on GLM-4.7-Flash in
|
|
Ollama; `--try-model <profile>` is the test a model has to pass (it calls a tool and *uses
|
|
what it returns*).
|
|
|
|
## Git: the repo is what runs
|
|
|
|
`~/semprini-core` on the core host is a git checkout of `main`. Before each run the daemon
|
|
fast-forwards it if it is clean. The agent edits that checkout in place, commits every change
|
|
it applies to the live stack to `main` and pushes it as `claude-code`; a change it only
|
|
proposes goes on a `maint/<topic>` branch with a pull request, opened from the core host.
|
|
After each run the daemon pushes anything left unpushed, retrying if Gitea is down. It reports
|
|
new commits, with a link, and any uncommitted changes. From a workstation checkout, deploy
|
|
with semprini-core's `infra/sync-to-host.sh` (push, then fast-forward the host).
|
|
|
|
semprini-blog (GitHub) and conk are readable by the agent but not pushable, so it proposes
|
|
patches for them instead.
|
|
|
|
## Setup on the core host
|
|
|
|
Prerequisites, on `ubuntu@3.107.254.151`:
|
|
- python3, git and systemd (Ubuntu has them); `install.sh` installs uv
|
|
- the core stack running, including Synapse, Gitea, matrix-alerts and Uptime Kuma
|
|
- `~/semprini-core` is a git checkout of `main`, and its git pushes as `claude-code`
|
|
(`~/.local/state/semprini-maintainer/git-credentials`, kept from the earlier install)
|
|
- an Anthropic API key
|
|
|
|
Stop any daemon running elsewhere first: one Matrix bot means one daemon. For the old WSL one,
|
|
in WSL: `sudo systemctl disable --now semprini-maintainer`.
|
|
|
|
```bash
|
|
git clone https://git.semprini.me/paul/semprini-maintainer.git ~/semprini-maintainer
|
|
cd ~/semprini-maintainer
|
|
cp config.example.json config.json # then copy matrix.* and gitea.token from the old config.json
|
|
./install.sh # uv, .venv, --check, systemd unit
|
|
.venv/bin/python maintainer.py --set-api-key claude # prompts, checks, saves
|
|
.venv/bin/python maintainer.py --try-model claude # tool use
|
|
```
|
|
|
|
`install.sh` stops before the unit if `config.json` is missing. If `repo_host` in it has an
|
|
`ssh` address (the daemon running somewhere else), it stops and disables the daemon on that
|
|
host just before starting this one.
|
|
|
|
**The home cluster** is reached through the WireGuard tunnel, only while the workstation that
|
|
hosts its VMs is on: `kubectl` as `elz-operator` and `ssh sem-k8s-1..3`, `ssh sem-gw`, set up
|
|
on this host by semprini-core (runbook → *Home Kubernetes Cluster*). Hyper-V itself is out of
|
|
reach: the agent escalates anything about the VMs to the operator.
|
|
|
|
## Configuration
|
|
|
|
`config.json` (template: `config.example.json`). The ones worth knowing:
|
|
|
|
| Key | Default | Meaning |
|
|
|---|---|---|
|
|
| `hosts` | core | Where commands run: local bash (no `ssh`), `ssh`, or `powershell`. `about` is what the agent is told about each |
|
|
| `repo_host` / `repo_dir` | `core` / `/home/ubuntu/semprini-core` | The checkout that runs |
|
|
| `models` / `routing` / `limits` | above | Model profiles, the order each kind of task tries them, per-run caps |
|
|
| `autonomy` | `full` | `full` fixes and upgrades; `fix-only` fixes and only proposes upgrades; `notify` changes nothing (and has no file-writing tools) |
|
|
| `health_interval_seconds` / `down_confirmations` | 300 / 2 | Health check cadence, and checks a monitor must fail before remediation |
|
|
| `upgrade_weekday` / `upgrade_hour_utc` | 5 / 22 | Weekly upgrade slot (0 = Monday) |
|
|
| `digest_hour_utc` | 21 | Daily report |
|
|
| `conversation_ttl_seconds` | 14400 | How long a follow-up continues the previous conversation |
|
|
| `escalation_expiry_seconds` | 604800 | An unanswered decision holds upgrades until then |
|
|
| `alerts.batch_seconds` / `alerts.cooldown_seconds` / `alerts.ignore` | 60 / 43200 / `["PipelineTest"]` | Alert batching, re-investigation interval, alerts never investigated |
|
|
| `host.*` | 85% disk, 400 MB memory | Core host check thresholds and cooldown |
|
|
| `git.repos` | semprini-core on core | Checkouts the daemon fast-forwards and pushes |
|
|
| `remote_hold_file` | `""` | A hold file on the repo host, read each minute, for a daemon running elsewhere |
|
|
|
|
## Operating
|
|
|
|
- **Logs:** `journalctl -u semprini-maintainer -f` on the core host, or `status` in Matrix.
|
|
- **Working on the stack yourself:** hold it, rather than stopping the service — a hold
|
|
expires on its own, so nothing stays paused because someone forgot. From Matrix,
|
|
`hold 45m upgrading postgres`; from a shell on the core host:
|
|
|
|
```bash
|
|
~/semprini-maintainer/maintainer.py --hold 45m --reason "upgrading postgres"
|
|
~/semprini-maintainer/maintainer.py --resume
|
|
```
|
|
|
|
`--hold` and `--resume` need only python3. On the core host they write the daemon's own
|
|
hold file; semprini-core's `infra/sync-to-host.sh` sets 15 minutes there on every deploy.
|
|
(A daemon run elsewhere reads that file through `remote_hold_file` and honours whichever
|
|
hold ends later.)
|
|
- **Stop everything it does:** `sudo systemctl stop semprini-maintainer` on the core host. For
|
|
"watch but don't touch", set `"autonomy": "notify"` and restart the service.
|
|
- **Update the daemon:** push to `main` here. It fast-forwards within the hour, runs
|
|
`uv sync` if the dependencies changed, runs `--check`, and restarts itself, or rolls back
|
|
and says why.
|
|
|
|
### When no model can run
|
|
|
|
An Anthropic outage, a revoked or missing key, or the provider refusing for billing: the
|
|
agent never started. That is a *maintainer* outage, not a stack verdict (`AgentUnavailable`). It
|
|
is tracked per kind of task, because each kind can be routed to its own models.
|
|
|
|
- The task stays in the persisted queue; nothing is lost.
|
|
- One `⛔ maintainer degraded` message per kind names the likely cause and the fix, then it
|
|
stays quiet until recovery.
|
|
- That kind's retries back off from 60s to 15 min. Saving a new key with `--set-api-key`
|
|
skips the wait.
|
|
- `✅ maintainer recovered` when a run of that kind succeeds again; its tasks then drain.
|
|
|
|
A run that hits its time limit or its `cost_usd` cap, is cancelled, or is rejected by the model
|
|
is reported as ⛔ blocked, with the commands it had run.
|
|
|
|
## Test without the service
|
|
|
|
```bash
|
|
.venv/bin/python maintainer.py --check # parsers, schedule, config, agent wiring, network
|
|
.venv/bin/python maintainer.py --check --offline # the same without touching the network
|
|
.venv/bin/python maintainer.py --try-model claude # one tool call; is the result used?
|
|
.venv/bin/python maintainer.py --once health # one health cycle and any agent work it queues
|
|
.venv/bin/python maintainer.py --once digest # post the daily report now
|
|
.venv/bin/python maintainer.py --once upgrade # run the upgrade now
|
|
```
|
|
|
|
Stop the service first: `--once` shares the state file.
|