# 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-core` on the host, a git checkout of `main`) — the stack this agent maintains. At runtime the agent reads, from `repo_dir`: - `agents/stack-support.agent.md` (injected as system prompt) - `docs/stack-support-runbook.md` and `CLAUDE.md` - `core_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 within `conversation_ttl_seconds` resumes the same Claude session (`--resume`), so it behaves like a chat. `status`, `cancel`, `new`, `upgrade`, `hold`, `resume` and `help` are answered by the listener without the agent, even mid-run. - **Hold** (`hold 45m ` in Matrix, `maintainer.py --hold 45m --reason …` from a shell): a time-boxed pause on everything the daemon starts by itself — health cycles, alert triage and upgrades — while operator messages still run and alerts still batch, so one triage run covers the whole hold. It lives in `hold.json` next to the state file because a shell has to reach a daemon that owns its own state file, and it expires by itself so nothing stays paused by accident. semprini-core's `sync-to-host.sh` sets 15 minutes on each deploy. `--hold` from a shell never shortens a longer hold already in force; `hold` in Matrix does what it says. - **Service desk** (1 min, `desk` in config): runs semprini-core's `core_stack/scripts/service-desk.py poll --json`, which fulfils the open requests in `Semprini/service-desk` (zones, for now) and answers in each issue. It is a script, not an agent run: it takes its turn between runs and waits out a hold. Requests needing approval, failures, "Ask core" requests and finished requests are posted to Stack Maintenance. Nothing from an issue reaches the agent unless the operator passes it on. - **Alerts** (`#stack-alerts`, written by `@alerts` via Loki → Alertmanager → matrix-alerts): FIRING messages are batched for `alerts.batch_seconds` and triaged in one run, answered in a thread under the alert. Non-`ok` results are mirrored to Stack Maintenance. A fingerprint is re-investigated at most every `alerts.cooldown_seconds`. `PipelineTest` is acknowledged, never investigated. - **Health timer** (5 min): Kuma monitors confirmed DOWN over `down_confirmations` cycles, DOWN→UP blips (Kuma `important=1` heartbeats 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's `kuma.db` is read via a throwaway `keinos/sqlite3` container. Scope = Kuma's active monitors, re-read every cycle. - **Daily report** at `digest_hour_utc`; **weekly upgrade** at `upgrade_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 to `main` and pushes it (`GIT_POLICY` in `maintainer.py`; semprini-core `CLAUDE.md` → *Deploying changes*). It pushes as `claude-code` with a token from `register-gitea-bot.sh`. - **Claude credential:** `claude_oauth_token` from `claude setup-token`, saved by `maintainer.py --set-claude-token` and 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 fetch` of this checkout; a newer `main` is 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`** (groups `docker`, `adm`), **not root**. Claude Code refuses `--permission-mode bypassPermissions` under root. - `maintainer.py` is **stdlib-only** — keep it dependency-free so it deploys without a venv. - Secrets live in `config.json` and `.bot-secrets` — **never commit** (gitignored). - **Deploying a change here:** push to `main` on git.semprini.me. The running daemon picks it up within `self_update_interval_seconds`. Keep `--check` meaningful — 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 ```bash 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 ```