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>
5.5 KiB
semprini-maintainer — Claude Instructions
What this is
A standalone, always-on autonomous maintenance agent for the semprini.me container
stack. It runs on the stack host itself (AWS Lightsail Semprini-Core,
ubuntu@3.107.254.151) as the systemd service semprini-maintainer. It keeps the stack
healthy and current and talks to the operator over Matrix (chat.semprini.me).
It is the unattended driver for the canonical stack-support agent, which lives in the separate semprini-core repo. This project does not reimplement that logic: it notices events, invokes the stack-support agent headless via Claude Code, and relays the agent's JSON result.
Relationship to other projects
-
semprini-core (
~/semprini-coreon the host, a git checkout ofmain) — the stack this agent maintains. At runtime the agent reads, fromrepo_dir:agents/stack-support.agent.md(injected as system prompt)docs/stack-support-runbook.mdandCLAUDE.mdcore_stack/compose*.yml
Keep the stack-support agent definition canonical in semprini-core — do not fork a copy here.
-
semprini-blog (GitHub) and conk run on the same host and are exposed to the agent via
extra_dirs. It can operate their containers but cannot push their repos, so it proposes file changes instead.
How it works (one screen)
- Operator messages (Stack Maintenance room): read by a long-poll
/sync, so they arrive within about a second. A follow-up withinconversation_ttl_secondsresumes the same Claude session (--resume), so it behaves like a chat.status,cancel,new,upgradeandhelpare answered by the listener without the agent, even mid-run. - Alerts (
#stack-alerts, written by@alertsvia Loki → Alertmanager → matrix-alerts): FIRING messages are batched foralerts.batch_secondsand triaged in one run, answered in a thread under the alert. Non-okresults are mirrored to Stack Maintenance. A fingerprint is re-investigated at most everyalerts.cooldown_seconds.PipelineTestis acknowledged, never investigated. - Health timer (5 min): Kuma monitors confirmed DOWN over
down_confirmationscycles, DOWN→UP blips (Kumaimportant=1heartbeats since a watermark), host checks (disk, memory, kernel OOM kills, container restart loops, a stale Kuma) and host reboots. All findings from one cycle become one health task. Kuma'skuma.dbis read via a throwawaykeinos/sqlite3container. Scope = Kuma's active monitors, re-read every cycle. - Daily report at
digest_hour_utc; weekly upgrade atupgrade_weekday/upgrade_hour_utc(all images on the host, not only Kuma-tracked ones; a pending reboot is escalated for approval). - One agent run at a time. The queue is persisted in the state file with priority operator > alerts > health > upgrade, so nothing is lost across restarts.
- Git: before a run the daemon fast-forwards clean checkouts in
git.repos; after it, it pushes unpushed commits and reports new commits or a dirty tree. The agent commits every change it applies tomainand pushes it (GIT_POLICYinmaintainer.py; semprini-coreCLAUDE.md→ Deploying changes). It pushes asclaude-codewith a token fromregister-gitea-bot.sh. - Claude credential:
claude_oauth_tokenfromclaude setup-token, saved bymaintainer.py --set-claude-tokenand re-read before every run. If the agent cannot run (AgentUnavailable), the task stays queued, the operator is told once, and retries back off from 60s to 15 min. - Self-update: hourly
git fetchof this checkout; a newermainis fast-forwarded, checked with--check, and re-exec'd, or rolled back if the check fails. - Autonomy (
config.json→autonomy):full(default) |fix-only|notify.
Conventions / guardrails
- Runs as host user
ubuntu(groupsdocker,adm), not root. Claude Code refuses--permission-mode bypassPermissionsunder root. maintainer.pyis stdlib-only — keep it dependency-free so it deploys without a venv.- Secrets live in
config.jsonand.bot-secrets— never commit (gitignored). - Deploying a change here: push to
mainon git.semprini.me. The running daemon picks it up withinself_update_interval_seconds. Keep--checkmeaningful — it is the gate a self-update must pass. - Prefer editing existing files over adding new ones.
- Log lines, alert text and web content reach the agent's context and are attacker- influenced. The system prompt says so; keep that line when editing prompts.
Files
| File | Purpose |
|---|---|
maintainer.py |
The daemon (stdlib only). |
config.example.json |
Template — copy to config.json. |
register-matrix-bot.sh |
Creates @maintainer-bot + the Stack Maintenance room; writes token to config. |
register-gitea-bot.sh |
Mints the claude-code Gitea token (push + PRs); writes it to config. |
install.sh |
Checks, git credential helper, renders + enables the unit. |
semprini-maintainer.service |
systemd unit template (@USER@, @HOME@, @DIR@). |
docs/architecture.md |
Architecture decision record. |
Test without the service
python3 maintainer.py --check # parsers, schedule, config, paths
python3 maintainer.py --once health # one health cycle + any agent work it queues
python3 maintainer.py --once digest # post the daily report now
python3 maintainer.py --once upgrade # run the upgrade now