From 75dc6a45080423a31fa0013aeda3ce918f6afd9b Mon Sep 17 00:00:00 2001 From: Claude Code Bot Date: Mon, 23 Nov 2026 19:26:39 -0800 Subject: [PATCH] docs(runners): one watchdog for both pools; macOS routing, recovery The Worker now watches the NOS and smartwatermelon macOS pools. The runbooks cover deploy, secrets and crons per pool, the macOS hold and manual-fix recovery, the routing runs-on expression, watchdog alerts, and lost-run re-runs. Advances #252 Advances #298 --- CLAUDE.md | 2 +- docs/runbooks/nos-runner.md | 84 ++++++++++++++++++---- docs/runbooks/swm-macos-runner.md | 115 +++++++++++++++++++++++++++--- 3 files changed, 177 insertions(+), 24 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 51adae6..a6dffb1 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -27,7 +27,7 @@ This repo is the **dev-env infrastructure repository** — it contains documenta - `scripts/nos-runner/` — Supervisor, installer, updater and pf guard for the self-hosted runner on MIMOLETTE: one Apple container VM per private nightowlstudiollc CI job, selected by the `NOS_RUNNER` org variable. Tests in `scripts/nos-runner/tests/run-tests.sh`. Credentials and the first install are by hand: see `docs/runbooks/nos-runner.md` - `scripts/agent-ro/` — Installer, root gate and ACL sweep for `agentro`, a separate ssh account agents use instead of Andrew's admin login: it reads his home and queries his services but cannot change anything. Run by hand with sudo on each target: see `docs/runbooks/agent-ro.md` - `scripts/swm-macos-runner/` — Tooling for the smartwatermelon macOS CI runner on MIMOLETTE (one Tart VM per job); the supervisor, image build, guest scripts (fork-guard hook), installer and LaunchAgent, with tests in `scripts/swm-macos-runner/tests/run-tests.sh`. Credentials and the first install are by hand: see `docs/runbooks/swm-macos-runner.md` -- `workers/nos-runner-watchdog/` — Cloudflare Worker (cron every 10 minutes) that moves `NOS_RUNNER` to `ubuntu-latest` when the runner is down, re-runs stuck runs and sends ntfy alerts; tests via `npm --prefix workers/nos-runner-watchdog test`. Deploy by hand through the Cloudflare API: see `docs/runbooks/nos-runner.md` +- `workers/nos-runner-watchdog/` — Cloudflare Worker that watches two runner pools, each on its own cron: NOS (every 10 minutes) moves `NOS_RUNNER` to `ubuntu-latest` when the runner is down, and the smartwatermelon macOS pool moves `SWM_MACOS_RUNNER` to `macos-latest` the same way. It re-runs stuck runs (and, for macOS, a job a runner restart stopped) and sends ntfy alerts; tests via `npm --prefix workers/nos-runner-watchdog test`. Deploy by hand through the Cloudflare API: see `docs/runbooks/nos-runner.md` - `.project-hooks/pre-commit` and `.project-hooks/pre-push` — Project-specific git hook extensions, run by the global hooks at `~/.config/git/hooks/` when executable - `.claude/` is ignored, as in every fleet repo (dev-env#178). The scaffold the post-checkout hook drops there (`config.sh.template`, `README.md`) comes from `smartwatermelon/dotfiles` `git/template/.claude-template/`; edit it there. diff --git a/docs/runbooks/nos-runner.md b/docs/runbooks/nos-runner.md index 29ecbb7..7bf226b 100644 --- a/docs/runbooks/nos-runner.md +++ b/docs/runbooks/nos-runner.md @@ -4,6 +4,7 @@ Private nightowlstudiollc CI jobs run on MIMOLETTE, in one Apple container VM pe The `NOS_RUNNER` org variable picks the runner label: `mimolette` or `ubuntu-latest`. A supervisor on MIMOLETTE keeps two runner slots up. A Cloudflare Worker, the watchdog, checks every 10 minutes and moves the variable to `ubuntu-latest` when the runner is down. +The same Worker also watches the smartwatermelon macOS pool, on its own cron (section 7). Design: `docs/superpowers/specs/2026-11-10-nos-self-hosted-runner-design.md`. ## 1. The two GitHub Apps @@ -14,10 +15,14 @@ Andrew creates both in the GitHub UI and installs them on nightowlstudiollc only |---|---|---| | `nos-runner` (App ID 5703224, installation 180668548) | org: Self-hosted runners write, Variables write | MIMOLETTE login Keychain | | `nos-runner-watchdog` | org: Self-hosted runners read, Variables write; repos: Actions write | Worker secret | +| `swm-runner-watchdog` (smartwatermelon, for the macOS pool) | org: Self-hosted runners read, Variables write; repos: Actions write | Worker secrets `SWM_*` | + +`swm-runner-watchdog` has the same permissions as `nos-runner-watchdog`, and no more. +It is installed on smartwatermelon, not nightowlstudiollc. Its setup is in `docs/runbooks/swm-macos-runner.md` (section 10). A leaked key reaches only that App's permissions. -Install `nos-runner-watchdog` on **All repositories**. With a smaller selection, +Install each watchdog App on **All repositories** of its org. With a smaller selection, the watchdog sees fewer repos and reports no error. ## 2. Store the supervisor key in the Keychain @@ -40,15 +45,25 @@ The item holds the PEM as base64 text, on one line. The supervisor reads it under launchd, where a prompt would block it with no window (not yet verified). 5. Verify the Keychain again, then delete the PEM file. -## 3. Prepare the watchdog key +## 3. Prepare the watchdog keys + +The Worker needs each key as PKCS8. Convert each PEM once. -The Worker needs the key as PKCS8. Convert the PEM once: +NOS: ```bash openssl pkcs8 -topk8 -nocrypt -in nos-runner-watchdog.pem -out nos-runner-watchdog.pkcs8.pem ``` -Set the contents as the Worker secret `APP_KEY_PKCS8` (section 7). Delete both files afterward. +smartwatermelon (macOS pool): + +```bash +openssl pkcs8 -topk8 -nocrypt -in swm-runner-watchdog.*.pem -out swm-runner-watchdog.pkcs8.pem +op document create swm-runner-watchdog.pkcs8.pem --title swm-runner-watchdog-pkcs8 --vault Automation +``` + +Set the contents as the Worker secret `APP_KEY_PKCS8` (NOS) or `SWM_APP_KEY_PKCS8` (macOS) (section 7). +Keep the PEM until the secret works, then delete both files. ## 4. First install on MIMOLETTE @@ -157,15 +172,27 @@ The watchdog moves it back to `mimolette` after healthy checks. ## 7. Deploy the watchdog Worker +One Worker watches both pools. Each pool has its own cron, so each tick gets its own subrequest and CPU budget: + +| Pool | Cron | Variable | KV keys | App secrets | +|---|---|---|---|---| +| NOS | `*/10 * * * *` | `NOS_RUNNER` | unprefixed: `state`, `token`, `pendingReruns`, `skips`, `drill` | `APP_ID`, `INSTALLATION_ID`, `APP_KEY_PKCS8` | +| smartwatermelon macOS | `5,15,25,35,45,55 * * * *` | `SWM_MACOS_RUNNER` | prefixed `swm-macos:` | `SWM_APP_ID`, `SWM_INSTALLATION_ID`, `SWM_APP_KEY_PKCS8` | + +`NTFY_TOPIC` and `NTFY_TOKEN` are shared. The macOS pool's alert texts start with `macOS runner:`. +The two crons never fire in the same minute. `scheduled()` picks the pool from `event.cron` and matches it exactly. +A tick with no cron (a manual run) is the NOS pool. A cron that no pool owns logs `unknown cron` and does nothing. + Deploy with wrangler (it calls the Cloudflare API), never the dashboard. Run each command from `workers/nos-runner-watchdog/`, logged in to the nightowlstudio account (`npx wrangler@4 whoami`). -First deployed 2026-11-12. +First deployed 2026-11-12. The macOS pool was added after that. 1. The KV namespace `nos-runner-watchdog-state` exists. Its ID is in `wrangler.toml`, binding `STATE`. To make a new one: `npx wrangler@4 kv namespace create nos-runner-watchdog-state`, then update the ID. -2. Upload the Worker and its cron: `npx wrangler@4 deploy`. The cron `*/10 * * * *` comes from `wrangler.toml`. - `workers_dev = false`, so the Worker has no public URL. -3. Set the five secrets from 1Password, piped so they never print: +2. Check the account's Cron Trigger count before the first deploy with two crons: the Free plan allows 5 across the account. + This Worker now uses 2. Count the others in the dashboard (Workers and Pages). It must be 4 or fewer before the deploy. +3. Set the secrets before the deploy when you add a pool, so the old code ignores them. + Set the five NOS secrets from 1Password, piped so they never print: ```bash printf '%s' | npx wrangler@4 secret put APP_ID @@ -180,15 +207,41 @@ First deployed 2026-11-12. Without it, ntfy.sh counts Worker posts against one quota shared by all of Cloudflare and returns 429. Even with it, ntfy.sh drops about half of the connections from Workers (HTTP 522 after about 20 s), so the Worker tries each alert up to 3 times (measured 2026-11-12). -4. Check a scheduled run: `npx wrangler@4 kv key list --binding STATE --remote` lists `state` after the first tick. - Never `kv key get token`: that key holds a live installation token. -5. To stop the watchdog (full stop), set `crons = []` in `wrangler.toml` and run `npx wrangler@4 triggers deploy`. - Restore `crons = ["*/10 * * * *"]` and run it again to start. (Not yet tested.) + + Set the three macOS secrets: + + ```bash + printf '%s' | npx wrangler@4 secret put SWM_APP_ID + printf '%s' | npx wrangler@4 secret put SWM_INSTALLATION_ID + op document get swm-runner-watchdog-pkcs8 --vault Automation | npx wrangler@4 secret put SWM_APP_KEY_PKCS8 + ``` + + A macOS pool with any of its three secrets missing stays silent: it logs `pool swm-macos not configured`, + makes no KV write and sends no alert. So a partial set, or a deploy before the secrets, is safe but also does nothing. + Only the macOS pool is quiet this way. The NOS pool keeps its alert path, so a lost NOS secret still shows as `watchdog skipping`. +4. Upload the Worker and both crons: `npm test && npx wrangler@4 deploy`. + The output lists both crons under the triggers. They come from `wrangler.toml`. + `workers_dev = false`, so the Worker has no public URL. +5. Check both pools after 20 minutes: `npx wrangler@4 kv key list --binding STATE --remote` lists `state` and `swm-macos:state`. + - Never `kv key get token` or `kv key get swm-macos:token`: those keys hold live installation tokens. + - **NOS tick at a `:x0` minute.** After a deploy that changes the crons, check that a NOS tick at a `:00`, `:10`, ... minute still runs and updates `state` + (watch `npx wrangler@4 tail` through one such minute, then check that `kv key get state --binding STATE --remote` shows the current `lastVar`). + Cron dispatch matches `event.cron` exactly, and a mismatch would make the NOS pool fall silent. + - If `SWM_MACOS_RUNNER` is unset, about 20 minutes after the deploy the watchdog creates it as `mimolette-macos` + and sends `macOS runner: online ...`. That is expected. No workflow reads the variable until a pilot switches to it. +6. To stop one pool (full stop), take its cron out of `wrangler.toml` and run `npx wrangler@4 triggers deploy`. + The other pool keeps running. + - NOS: remove `"*/10 * * * *"` from `crons`. + - macOS: remove `"5,15,25,35,45,55 * * * *"` from `crons`. + + To stop both, set `crons = []`. Put the crons back and run the command again to start. (Not yet tested.) + A stopped watchdog does not move a variable back, so hold the variable first (section 6, and section 6 of the macOS runbook). ## 8. Alerts All alerts go to the ntfy topic. The topic also carries the smartwatermelon macOS runner's alerts, which start with `macOS runner:` -(`docs/runbooks/swm-macos-runner.md`). +(`docs/runbooks/swm-macos-runner.md`, section 7, lists them). +The list below is the NOS pool. The macOS pool sends the same kinds, with its own texts. - **down.** The runner failed its checks. `NOS_RUNNER` is now `ubuntu-latest`. Runs stuck on `mimolette` are cancelled and re-run on GitHub. - **recovered.** `NOS_RUNNER` is back on `mimolette` after healthy checks. @@ -213,6 +266,11 @@ The supervisor also sends its own alerts: The next tick sends "NOS runner drill: watchdog alert path", then deletes `drill`. Read the result with `npx wrangler@4 kv key get drill-result --binding STATE --remote`: `ok` and `tries` (1 to 3). KV reads can lag a write by about 2 minutes. +- Watchdog, macOS pool: the same, with the prefixed keys: + `npx wrangler@4 kv key put swm-macos:drill 1 --binding STATE --remote`. + The next macOS tick (up to about 10 minutes) sends "macOS runner: drill of the watchdog alert path. No action needed." + Read `npx wrangler@4 kv key get swm-macos:drill-result --binding STATE --remote`. + The NOS pool must send no new alert. A plain `drill` key belongs to NOS, and the macOS tick does not touch it. **Recovery backoff.** After repeated outages, recovery waits longer each time: 20, then 40, then 80, then 160 minutes. diff --git a/docs/runbooks/swm-macos-runner.md b/docs/runbooks/swm-macos-runner.md index edd6118..68516fb 100644 --- a/docs/runbooks/swm-macos-runner.md +++ b/docs/runbooks/swm-macos-runner.md @@ -1,16 +1,20 @@ # smartwatermelon macOS runner: install, operate, undo smartwatermelon macOS CI jobs run on MIMOLETTE, in one Tart VM per job, one job at a time. -A workflow opts in with `runs-on: ${{ vars.SWM_MACOS_RUNNER || 'macos-latest' }}`. +A workflow opts in with the routing `runs-on` expression (section 9). It sends fork and Dependabot runs to GitHub-hosted runners. The `SWM_MACOS_RUNNER` org variable picks the runner label: | Value | Where jobs run | |---|---| -| unset | GitHub-hosted `macos-latest` (the fail-safe) | +| unset | GitHub-hosted `macos-latest`. The watchdog sets it to `mimolette-macos` when the runner is healthy, so unset is not a hold | +| `macos-latest` | GitHub-hosted. This is the watchdog's failover value. The watchdog moves it back to `mimolette-macos` when the runner is healthy, so it is not a hold | | `mimolette-macos` | This runner | | `macos-26` | GitHub-hosted `macos-26`. This is the hold value (section 6) | -The supervisor never writes `SWM_MACOS_RUNNER`. You set it by hand. +The watchdog Worker writes `SWM_MACOS_RUNNER`. The supervisor never does. Any value other than the three above, `macos-latest` or unset counts as a hold. +The watchdog is the same Worker as the NOS one: `docs/runbooks/nos-runner.md`, section 7. It checks every 10 minutes +(cron `5,15,25,35,45,55 * * * *`), moves the variable to `macos-latest` after two checks with no healthy runner, +and back to `mimolette-macos` after healthy checks. You can still set the variable by hand (section 6). Design: `docs/superpowers/specs/2026-11-17-swm-macos-tart-runner-design.md`. Run every step in a terminal on MIMOLETTE, in a GUI login session. @@ -22,6 +26,7 @@ launchd starts the supervisor in `gui/`, so Tart runs there too, never thro It is already installed on smartwatermelon. - Its key is in the MIMOLETTE login Keychain: service `swm-runner-app-key`, account `smartwatermelon`. The item holds the PEM as base64 text on one line. +- The watchdog has its own App, `swm-runner-watchdog` (section 10). It is not the App above. - Check the key. This prints nothing when it is a valid private key: ```bash @@ -155,29 +160,46 @@ Until someone clicks **Allow**, a launchd job hangs with no log line. The prompt gh variable set SWM_MACOS_RUNNER --org smartwatermelon --visibility all --body macos-26 ``` +The watchdog sends one `held` alert and then leaves the variable alone. A hold is the only value it never changes. + **Release.** Send jobs to this runner: ```bash gh variable set SWM_MACOS_RUNNER --org smartwatermelon --visibility all --body mimolette-macos ``` -Unset the variable to go back to `macos-latest`: +Or set it to `macos-latest`, and the watchdog moves it to `mimolette-macos` after healthy checks. +Unsetting the variable no longer sends jobs to `macos-latest` for long: the watchdog creates it again as `mimolette-macos` +(an `online` alert) about 20 minutes later. To keep jobs hosted, hold. -```bash -gh variable delete SWM_MACOS_RUNNER --org smartwatermelon -``` +**After a manual fix.** After you repair the runner by hand, do not wait for the watchdog to recover. Do this: + +1. Confirm the runner is up. The supervisor log shows `runner ... handed its JIT config`, + or the org runners page lists an idle `mimolette-macos` runner. +2. Run this in your own terminal. Agents cannot write org variables: + + ```bash + gh variable set SWM_MACOS_RUNNER --org smartwatermelon --visibility all --body mimolette-macos + ``` + +3. Expect no "recovered" alert, because the watchdog sees a change it did not make and resets its miss and good counts. + If the runner is unhealthy, the watchdog fails over again after 2 bad checks (about 20 minutes). -**Full stop.** Hold first, so no job waits for a runner that is gone. Then stop the job: +**Full stop.** Hold first, so no job waits for a runner that is gone, and so the watchdog does not start a failover or move it back. +Set `SWM_MACOS_RUNNER=macos-26` (the Hold command above). Then stop the job: ```bash launchctl bootout gui/$(id -u)/com.smartwatermelon.swm-macos-runner ``` -A running job finishes its teardown first (up to about 230 s). The plist stays, so a login or `install.sh` starts it again. +A running job finishes its teardown first (up to about 230 s; dev-env#312 is open on a worst case of about 330 s, over the plist's `ExitTimeOut` of 300 s). The plist stays, so a login or `install.sh` starts it again. To remove it for good, delete `~/Library/LaunchAgents/com.smartwatermelon.swm-macos-runner.plist` after the bootout. Stop the updater too (section 3a): the next time it installs new code, `install.sh` loads the supervisor again. -There is no watchdog for this runner yet. If the supervisor is down while the variable says `mimolette-macos`, jobs wait. Hold first. +If the supervisor is down while the variable says `mimolette-macos`, jobs wait until the watchdog sees two checks with no runner +(about 20 to 30 minutes) and sets `macos-latest`. A hold avoids that wait. The watchdog also moves the variable back to +`mimolette-macos` once the runner is healthy again, so a hold is the only lasting way to keep jobs hosted. To stop the watchdog +for this pool, see "To stop one pool" in `docs/runbooks/nos-runner.md`, section 7. ## 7. Alerts and logs @@ -189,6 +211,37 @@ All alerts go to the NOS ntfy topic and start with `macOS runner:`: - `tart is hung (...)`. See section 5. - `N starts failed in a row; last: ...`. Three failed starts in a row. Read the log. +The watchdog Worker sends these, from the same ntfy topic. Each one starts with `macOS runner:`: + +- `down. SWM_MACOS_RUNNER set to macos-latest; stuck runs cancelled for re-run.` Two checks in a row found no healthy runner. +- `recovered. SWM_MACOS_RUNNER set to mimolette-macos.` The runner is healthy again. +- `online. SWM_MACOS_RUNNER was unset, now mimolette-macos.` The variable did not exist, and the watchdog created it. +- `held. SWM_MACOS_RUNNER=; the watchdog leaves it alone.` A hold. Sent once. +- `watchdog skipping: `. Three ticks in a row were skipped. The variable is left alone. +- `watchdog ok: ticks are running again.` after a skipping alert. +- `drill of the watchdog alert path. No action needed.` A drill (`docs/runbooks/nos-runner.md`, section 8). + +**An image rebuild sends a `down` and a `recovered` alert.** This is expected. The runner has one slot, and it is offline +while the image builds (about 15 minutes) or while an update waits (up to 900 s). Two checks 10 minutes apart can both find +no runner, so the watchdog fails over to `macos-latest`, then moves back after the runner returns. Between the two alerts +new jobs run on GitHub-hosted runners, and a queued job is cancelled and re-run there. A gap between jobs +(teardown, clone, boot) is shorter than 10 minutes and does not trigger it. Treat a `down` that follows an +`image ... is missing; building it` log line as this, not as a fault. + +**Lost-run re-runs.** A restart of the supervisor (an update, `kickstart`, a logout) stops the job on the runner. +The watchdog finds such a run and re-runs its failed jobs once. It sends no alert: a re-run after a restart is routine +and the restart is already visible. The Worker logs `lost runner re-run: #` (and `... re-run failed: ...`). +A run counts as lost when all of these hold: + +- It is attempt 1, ended in `failure`, ran in the same repo as its head (not a fork), and its actor is not `dependabot[bot]`. +- A job on the `mimolette-macos` label failed with no failed step. A real test failure has a failed step and is not re-run. + A cancelled run, and a job that the fork guard denied, are not re-run either. + +At most one re-run per run: attempt 2 is never re-run. The watchdog looks 6 hours back, a few repos per tick. +It keeps its checked runs in the KV key `swm-macos:lostChecked`, with a `_cursor` field (the repo rotation cursor). +Entries are dropped after 6 hours. To see a re-run, read the Worker's logs, or look for attempt 2 on the run. +This rule was written from a guess about how GitHub marks a stopped job, and the restart drill is its first proof. + The log is `~/Library/Logs/swm-macos-runner.log`. Key lines: - `runner ... handed its JIT config`: the runner may be idle or busy; it is waiting for a job or running one (`job=1`). @@ -224,3 +277,45 @@ TART_HOME="$HOME/.tart" tart delete Delete the spike files with `rm` on the names `ls` printed, then remove the empty folder with `rmdir`. The supervisor's own images are in `/Volumes/extra-vieille/Caches/tart`. Do not touch them. + +## 9. Routing + +A workflow that runs on this pool uses this `runs-on`, word for word. Forks and Dependabot go to GitHub-hosted runners. +Everything else uses the pool variable, then GitHub-hosted: + +```yaml +runs-on: ${{ (((github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) || github.actor == 'dependabot[bot]' || github.event.pull_request.user.login == 'dependabot[bot]') && 'macos-latest') || vars.SWM_MACOS_RUNNER || 'macos-latest' }} +``` + +Why each part is there: + +- **`github.event_name == 'pull_request' && head.repo.full_name != github.repository`.** A pull request from a fork must not run + code on MIMOLETTE. The event-name test keeps a `push` (which has no PR head) from reading as a fork. +- **A deleted fork.** When the fork is gone, `head.repo` is null. It compares unequal, so the run goes hosted. That is the safe side. +- **`github.actor == 'dependabot[bot]'`.** Dependabot runs go hosted. `github.actor` stays the same on a re-run, + so a re-run of a Dependabot run stays hosted too. +- **`github.event.pull_request.user.login == 'dependabot[bot]'`.** This catches the case that `actor` misses: + Andrew pushing to a Dependabot branch. Each of the two tests catches a case the other misses. + The same pair is in `github-workflows/.github/workflows/dependabot-auto-merge.yml`. +- **`&& 'macos-latest'`.** When any of the above holds, the run is hosted, whatever the variable says. +- **`|| vars.SWM_MACOS_RUNNER`.** Otherwise the variable picks the runner, so a failover or a hold works with no workflow change. +- **`|| 'macos-latest'`.** The variable is unset: hosted. This is the fail-safe. + +Do not use this expression in a workflow that triggers on `pull_request_target` or `workflow_run`. In those events +`github.event.pull_request` is not the fork's head, so the test is not a safe guard. The fork-guard hook on the VM +(`scripts/swm-macos-runner/guest/`) is a second layer and does not replace it. + +## 10. The watchdog App + +The Worker uses its own GitHub App for this pool, `swm-runner-watchdog`, so a leaked key reaches only its permissions. +Andrew creates it in the GitHub UI (smartwatermelon org, Settings, Developer settings, GitHub Apps): + +1. Name `swm-runner-watchdog`. Webhook off. "Only on this account". +2. Permissions, and nothing else: Organization **Self-hosted runners: Read**, Organization **Variables: Read and write**, + Repository **Actions: Read and write** (Metadata read is automatic). + There is no Checks read: the lost-run test uses the jobs API only. +3. Install it on smartwatermelon, **All repositories**. A smaller selection gives fewer repos and no error. +4. Note the App ID and the installation ID (from the installation URL), generate a private key, then follow + `docs/runbooks/nos-runner.md` section 3 (convert the key) and section 7 (the three `SWM_*` secrets). + +Check the key by minting a token only through the Worker. Never read `swm-macos:token`: it holds a live installation token.