feat(pi-bots): the bot service as a launchd user agent, with alert rules (#9) #17

Manually merged
bryan merged 3 commits from n9-1-pi-bots-service into main 2026-11-10 22:04:13 +00:00
Owner

Part of #9 (piece N9.1).

What

services.pi-bots (modules/services/pi-bots.nix), enabled on Studio: the bot service (bryan/pi-bots) as one launchd user agent, org.nixos.pi-bots.

  • Runs Homebrew node on <checkout>/src/cli.ts service run from the checkout (~/code/pi-bots), RunAtLoad + KeepAlive, ExitTimeOut 30. A code change in the checkout needs a launchctl kickstart -k, not a rebuild. As a Homebrew-node agent it joins base/homebrew.nix's re-pin.
  • Environment: HOME, a PATH with Homebrew, PI_BOTS_HOMESERVER, PI_BOTS_STATE_DIR, PI_BOTS_METRICS_DIR. No token or owner: the service reads its own files.
  • One service per state directory: a second copy exits 1 at once and KeepAlive retries it, so the agent takes over within seconds of another copy stopping.
  • node_exporter reads pi-bots.prom from the metrics directory. Rules:
    • PiBotsServiceDown (critical): the service stopped cleanly (pi_bots_service_up 0) for 5 min.
    • PiBotsServiceSilent (critical): the file is over 2 min stale or missing, for 5 min (a crash).
    • PiBotsBotDown: a bot's runner or Pi down for 5 min.
    • PiBotsBotCrashLooping: a bot's Pi down most of 15 min and dropping more than once (a crash loop never satisfies BotDown's for; one outage is BotDown's).
    • PiBotsAccountNotSyncing: 10 min.
    • PiBotsMessagesWaiting: an owner's message unanswered for 30 min.
    • The bot and account rules hold only while the service reports itself up and its file is fresh, so a stopped or crashed service raises one alert, not one per bot.
  • An assertion keeps the state directory the one pi-bots-backup archives.

Not here: a "clock silent" rule. The service has no clock metric yet (pi-bots W32.4 adds it); its rule follows then.

Tests

python3 scripts/test_pi_bots_service.py (23): the agent's command, environment and logs; node_exporter wiring; the Homebrew re-pin; refusals; disabled declares nothing; promtool check metrics on the service's file shape; promtool test rules for each rule, firing and quiet, including a clean stop vs a crash, a short restart, a crash loop and nothing per bot while the service is down. Mutations of each guard fail a test. darwin-rebuild build --flake .#studio builds; the neighbouring monitoring, backup, NAS, content-probe, listener and Dashy suites pass.

The agent's exact command and environment were run by hand: against the live state it refuses with exit 1 while another service holds the lock; on a scratch state it starts, writes its file, and on SIGTERM exits 0 having written pi_bots_service_up 0.

Review

An independent Pi reviewer found one should-fix: the crash-loop rule fired after a single outage ended. That is fixed (resets counts drops), and a stale file now also holds the bot rules back. Both are covered by tests that fail without them, and the reviewer approved the fix. Left as a follow-up: the shared Homebrew re-pin waits about 2 s after bootout, which a slow stop can outlast.

After update-system

The service now runs by hand in a Rex tab. The agent waits on its lock until that copy stops; then it takes over.

Part of #9 (piece N9.1). ## What `services.pi-bots` (`modules/services/pi-bots.nix`), enabled on Studio: the bot service (`bryan/pi-bots`) as one launchd user agent, `org.nixos.pi-bots`. - Runs Homebrew `node` on `<checkout>/src/cli.ts service run` from the checkout (`~/code/pi-bots`), `RunAtLoad` + `KeepAlive`, `ExitTimeOut` 30. A code change in the checkout needs a `launchctl kickstart -k`, not a rebuild. As a Homebrew-node agent it joins `base/homebrew.nix`'s re-pin. - Environment: `HOME`, a `PATH` with Homebrew, `PI_BOTS_HOMESERVER`, `PI_BOTS_STATE_DIR`, `PI_BOTS_METRICS_DIR`. No token or owner: the service reads its own files. - One service per state directory: a second copy exits 1 at once and `KeepAlive` retries it, so the agent takes over within seconds of another copy stopping. - node_exporter reads `pi-bots.prom` from the metrics directory. Rules: - `PiBotsServiceDown` (critical): the service stopped cleanly (`pi_bots_service_up 0`) for 5 min. - `PiBotsServiceSilent` (critical): the file is over 2 min stale or missing, for 5 min (a crash). - `PiBotsBotDown`: a bot's runner or Pi down for 5 min. - `PiBotsBotCrashLooping`: a bot's Pi down most of 15 min and dropping more than once (a crash loop never satisfies `BotDown`'s `for`; one outage is `BotDown`'s). - `PiBotsAccountNotSyncing`: 10 min. - `PiBotsMessagesWaiting`: an owner's message unanswered for 30 min. - The bot and account rules hold only while the service reports itself up and its file is fresh, so a stopped or crashed service raises one alert, not one per bot. - An assertion keeps the state directory the one `pi-bots-backup` archives. Not here: a "clock silent" rule. The service has no clock metric yet (pi-bots W32.4 adds it); its rule follows then. ## Tests `python3 scripts/test_pi_bots_service.py` (23): the agent's command, environment and logs; node_exporter wiring; the Homebrew re-pin; refusals; disabled declares nothing; `promtool check metrics` on the service's file shape; `promtool test rules` for each rule, firing and quiet, including a clean stop vs a crash, a short restart, a crash loop and nothing per bot while the service is down. Mutations of each guard fail a test. `darwin-rebuild build --flake .#studio` builds; the neighbouring monitoring, backup, NAS, content-probe, listener and Dashy suites pass. The agent's exact command and environment were run by hand: against the live state it refuses with exit 1 while another service holds the lock; on a scratch state it starts, writes its file, and on SIGTERM exits 0 having written `pi_bots_service_up 0`. ## Review An independent Pi reviewer found one should-fix: the crash-loop rule fired after a single outage ended. That is fixed (`resets` counts drops), and a stale file now also holds the bot rules back. Both are covered by tests that fail without them, and the reviewer approved the fix. Left as a follow-up: the shared Homebrew re-pin waits about 2 s after `bootout`, which a slow stop can outlast. ## After `update-system` The service now runs by hand in a Rex tab. The agent waits on its lock until that copy stops; then it takes over.
services.pi-bots runs `node src/cli.ts service run` from the pi-bots
checkout as org.nixos.pi-bots (RunAtLoad, KeepAlive), with the homeserver,
state and metrics directories in its environment. node_exporter reads the
service's pi-bots.prom, and six PiBots* rules alert on the service stopping
or going silent, a bot down or crash-looping, an account not syncing, and
messages left unanswered. Enabled on Studio.
bryan manually merged commit 2ff3842dbb into main 2026-11-10 22:04:13 +00:00
Sign in to join this conversation.
No reviewers
No labels
ready-for-agent
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bryan/nix-configs!17
No description provided.