spec: instance memory alert (#996), draft for review
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
docs/superpowers/specs/2026-11-13-instance-memory-alert-design.md+158 −0
Intent
<task-notification> <task-id>a0cb75f46636465fa</task-id> <tool-use-id>toolu_01Vnrxq4FAU8Mo3LAfpUYmPu</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/96a09f74-e398-48ed-a97f-140bd1f7e255/tasks/a0cb75f46636465fa.output</output-file> <status>completed</status> <summary>Agent "Draft #996 memory alert design" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>I've written the design to `/home/paulca/amadan.net/carlos/platform/.claude/worktrees/plan-tito-nesting-moves/docs/superpowers/specs/2026-11-13-instance-memory-alert-design.md`. It is about 1,200 words counting the file:line citations, a little over the target. Nothing else was edited, committed or posted. **Recommendation:** - **Signal:** each box's existing 60-second metrics sampler counts throttling (the `high` counter) and OOM kills (`oom_kill`) per instance. It reads them at the cgroup that holds the cap: the slice or generation cgroup for nested instances, the unit or exec cgroup for flat ones. - **Not a trigger:** memory in use against the limit is only the figure shown ("470 of 512 MB"). Page cache counts toward it, so a healthy app that reads a lot of files would trip it all day. - **Carrier:** the box decides when an episode starts and ends and adds the verdict to its existing 5-minute status report, the private block that already carries the database size per instance. - **Readers:** the `carlos instances list` REASON column, the console instance page and the alerts engine all read that one report, so they can't disagree. Nothing reads `memory.events` today, so the counters are a new read. - **Calibration first:** run it reporting-only for a week before anything is sent. **Open questions for Paul:** 1. Who hears it before the alerts plan that adds account and app alerts (Plan 3, not built)? Today the engine only alerts operators. Either show it in REASON and on the console page plus an operator rule now, or build a minimal app alert now. 2. What should the "next step" say? A per-instance size is operator-only and the move (#992) doesn't exist yet. 3. Is an OOM kill an incident or a warning? 4. Should this replace the memory half of the planned "too busy" alert, so there is one memory signal? **Risks:** - **Root reads:** none. The agent runs as `carlos` and already reads files in these cgroup folders. File modes are still unchecked; the spec gives the `stat` command to run on dev. - **S3 cost:** no new objects and no new requests, only extra bytes on a status write that already happens, and only for instances in an episode. - **Census:** the new field is keyed by hostname, so it must stay in the private block, which is where the census should look. - **Thresholds:** the noise thresholds are proposals until they've been calibrated.</result> <usage><subagent_tokens>119430</subagent_tokens><tool_uses>40</tool_uses><duration_ms>443246</duration_ms></usage> </task-notification>
diff --git a/docs/superpowers/specs/2026-11-13-instance-memory-alert-design.md b/docs/superpowers/specs/2026-11-13-instance-memory-alert-design.md| new file mode 100644 |
| index 000000000..536ea7933 |
| --- /dev/null |
| +++ b/docs/superpowers/specs/2026-11-13-instance-memory-alert-design.md |
| @@ -0,0 +1,158 @@ |
| +# Instance memory alert (#996): design |
| + |
| +Status: draft for Paul. Grounded in origin/main 4c641678. Owner-facing strings |
| +are drafts for a later copy review. |
| + |
| +## 1. Why |
| + |
| +Nothing tells an owner that one instance is running out of room. The cap is |
| +real: `memory.max` from the instance's size, `memory.high` at 90 percent |
| +(`internal/cgroup/cgroup.go:158-161`, written at `cgroup.go:771-783`), and |
| +`memory.oom.group=1`, so a kill takes the whole family |
| +(`cgroup.go:764-766`, `:800`). Throttling and kills are silent from the |
| +owner's side. Box memory is watched (`internal/host/memwatch.go:21-33`); |
| +instances are not. |
| + |
| +Two triggers. #992: in an app with one instance per customer, the owner needs |
| +to see which customer outgrew shared hosting before moving it. And the nesting |
| +plan's W2 conversion batches (`docs/superpowers/plans/2026-11-13-tito-nesting-and-self-serve-moves.md` |
| +W5) need a per-instance view while instances land under their caps. |
| + |
| +## 2. Goals, non-goals, anti-goals |
| + |
| +- **Goals.** An owner sees, per instance, "near its limit" and "ran out and |
| + restarted", with the next step. Operators see the same during batches. |
| +- **Non-goals.** Changing sizes or caps; forecasting memory creep (left for |
| + later by the alerts spec §7.1); the move itself (#992). |
| +- **Anti-goals.** Noise: fire only when sustained, one alert per instance per |
| + episode (alerts spec §6 rules 1, 3, 6). Any new per-box S3 polling or new |
| + object. Any new root read of tenant-writable files: none is needed (§4). |
| + |
| +## 3. The signal |
| + |
| +Read per instance, at the cgroup that holds the cap, once a minute: |
| + |
| +| File | Use | |
| +|---|---| |
| +| `memory.events` `high` | delta per minute: throttled at `memory.high` | |
| +| `memory.events` `oom_kill` | delta: kills since last tick | |
| +| `memory.current`, `memory.max` | the figure shown ("470 of 512 MB"), never a trigger alone | |
| +| `memory.pressure` `some avg60` (proposal) | stall time, to tell real pain from a full page cache | |
| + |
| +Why not `memory.current` near `memory.max` (the alerts spec's "too busy", |
| +§7.3 line 415): the charge includes page cache (`cgroup.go:758-762`), so a |
| +healthy file-reading app sits near `memory.high` all day. Swap is allowed up |
| +to the cap (`cgroup.go:785-791`), so `current` can also look fine while the |
| +app is swapping. Nothing reads `memory.events` or `memory.pressure` today |
| +(git grep finds no reader). |
| + |
| +**Proposed thresholds** (calibrate before switch-on, §6): |
| + |
| +- *Near its limit*: `high` grew in at least 8 of the last 10 minutes, and, |
| + where `memory.pressure` reads, `some avg60` at or above 5 percent. Clears |
| + after 30 minutes with no `high` growth. Ten minutes matches the box `mem` |
| + rule's sustain (`internal/metrics/alert.go:16-17`, `:42-43`); the gentler |
| + clear is §6 rule 4. |
| +- *Ran out*: any `oom_kill` delta. A one-off event: it closes after 24 hours |
| + quiet (§6 rule 8), and further kills in that window update its count, not |
| + a new message. |
| + |
| +Counters reset when the cgroup is remade. A new cgroup id |
| +(`internal/host/metrics_nested_linux.go:13`) or a smaller value is a new |
| +baseline, never a negative delta. |
| + |
| +## 4. Where it lives |
| + |
| +**Computed on the box**, in the metrics sampler: 60-second ticks |
| +(`internal/host/metrics_loop.go:1-8`, `:1024`). It already walks every |
| +instance cgroup and reads `memory.current` and `memory.peak` |
| +(`internal/host/metrics_probes.go:223-241`): flat units in |
| +`system-carlos\x2dapp.slice` (`metrics_probes.go:82-90`, `:97`), exec children |
| +under the edge's delegated subtree (`metrics_probes.go:261`), and nested units |
| +(`internal/host/metrics_nested.go:80-111`). It keeps per-unit state between |
| +ticks already (`metrics_loop.go:71-80`). Readings are vouched to a tenant |
| +(`metrics_loop.go:130-139`); an episode should only count vouched readings. |
| + |
| +The cap's cgroup differs by layout. Nested: the host's slice, or the |
| +generation cgroup when one exists (`internal/host/sizestatus.go:64-81`), while |
| +the sampler reads the units below it (`metrics_nested.go:74-76`). |
| +`memory.events` is hierarchical, so reading it at the slice covers every |
| +generation. gVisor caps at the physical limit (`sizestatus.go:90-93`). |
| + |
| +**No root read.** The agent runs as `User=carlos` |
| +(`internal/host/units/carlos-edge.service:41`) and already reads |
| +`memory.max` and `memory.current` in these same directories |
| +(`sizestatus.go:82`, `metrics_probes.go:231`). cgroupfs files are written by |
| +the kernel; a tenant cannot write them. Unknown: check how, by running |
| +`stat -c '%a %U' memory.events memory.pressure` on a nested slice and an exec |
| +cgroup on dev. |
| + |
| +**Carried** in the 5-minute status heartbeat's private `Metrics` block |
| +(`status.SnapshotPeriod`, `internal/status/fleet.go:120`), as a host-keyed map |
| +beside `RouteDBBytes` (`internal/status/snapshot.go:720-726`), private for |
| +the same reason (`snapshot.go:579-583`). Only instances in an episode, or with |
| +one closed in the last 24 hours, get an entry: verdict, since, current, max, |
| +kill count. That adds bytes to a PUT that already happens, no new request. |
| + |
| +**Judged** by the alerts engine, which reads heartbeats every fifth pass with |
| +conditional GETs (`internal/console/alertengine_pass.go:46-51`, |
| +`alertsignals.go:3-7`). Grouping, flapping and snooze are its noise rules. |
| +Today it has only fleet scope (`alertsignals.go:444-450`); account and app |
| +scope is the alerts spec's Plan 3 (§11), not built. |
| + |
| +**Shown** in `carlos instances list` REASON (`instances_cli.go:713-727`), joined |
| +from heartbeats as DB is (`internal/console/apiinstances.go:948`, `:1096`); |
| +on the console instance page (`internal/console/instancepage.go`, which |
| +already shows box-reported words); and in alerts. |
| + |
| +## 5. Words an owner sees (drafts) |
| + |
| +- REASON: `memory: near its limit, 470 of 512 MB since 14:02` |
| +- REASON: `memory: ran out and restarted 3 times today` |
| +- Instance page, state line: "Near its memory limit" / "Ran out of memory and |
| + restarted" / "Memory OK". |
| +- Instance page, body: "This instance has used most of its 512 MB for 25 |
| + minutes, so it’s slowing down. If it keeps growing it will run out and |
| + restart. A bigger size or a box of its own will fix it." |
| +- Alert, fire: "shop-42.example.com is near its memory limit. It has used most |
| + of its 512 MB for 10 minutes and is slowing down. Next step: a bigger size, |
| + or its own box." |
| +- Alert, kill: "shop-42.example.com ran out of memory and restarted at 14:07." |
| +- Alert, clear: "shop-42.example.com is back under its memory limit." |
| + |
| +## 6. Approaches |
| + |
| +1. **Box judges, heartbeat carries, engine and listings read it.** |
| + Recommended. One carrier, so REASON and Slack never disagree (alerts spec |
| + §3, line 103). No new object or read. Minute-level judging on the box, |
| + up to 10 minutes' delivery delay, which suits a warning. |
| +2. **Per-host box rules in `alerts/boxes/<box>.json`.** The shape exists |
| + (`restarts@<unit>`, `verify_failed@<host>`, `alert.go:188`), written on |
| + change and read every minute. Faster, but the engine maps box rules to |
| + fleet scope, and the listings would need a second read to show REASON. |
| +3. **Console judges from raw chunks** (`metrics/boxes/<box>/raw/`, |
| + `metrics_loop.go:175-180`) or Plan 3's per-app window object (§7.2). Raw |
| + chunks change on every flush, so each read is a full GET per box every 5 |
| + minutes; the window object is per app, not per instance. Rejected. |
| + |
| +Calibration first: ship the box half reporting only, read a week of dev and |
| +production heartbeats, and count would-be fires (§6 rule 6), as edge |
| +protection was calibrated. |
| + |
| +## 7. Left to the builder |
| + |
| +The heartbeat field's names and cap; reading `memory.pressure` only where |
| +present; the generation-or-slice choice per tick; the cgroup-id baseline; |
| +tests with fixture cgroup trees as `metrics_probes_test.go` does. |
| + |
| +## 8. Open questions for Paul |
| + |
| +1. Until Plan 3, who hears? (a) REASON and the instance page now, plus an |
| + operator fleet rule grouped per box; owners get alerts with Plan 3. Or |
| + (b) build a minimal app scope now. |
| +2. Next step wording: `carlos instances set --size` is operator-only |
| + (`docs/site/cli.md:1348`) and the move does not exist (#992). Say "contact |
| + us", or name the commands? |
| +3. Is a kill an incident (pages through quiet hours) or a warning? |
| +4. Should this replace "too busy"'s memory half (§7.3), so there is one |
| + memory signal? |