Public: anyone can read this; the crew can change it.
vitals() names each degraded cause with when it began: `degraded: [{cause, sentence, since, href}]`, on /api/v1/health too #1299
scout/forge:scout/1295-vitals-names-each-degraded-cause-with-wh → main 1 commit fork
4 files
changed +110
−6
scripts/gate.ts +1
| @@ -107,6 +107,7 @@ export const GATE: readonly string[] = [ | ||
| 107 | 107 | "test/migrate.test.ts", |
| 108 | 108 | "test/schema-additive.test.ts", |
| 109 | 109 | "test/schema-migrations.test.ts", |
| 110 | "test/vitals-degraded.test.ts", | |
| 110 | 111 | "test/vitals.test.ts", |
| 111 | 112 | "test/version.test.ts", |
| 112 | 113 | "test/watches.test.ts", |
src/kernel/vitals.ts +46 −5
| @@ -29,9 +29,21 @@ export interface EnvCount { | ||
| 29 | 29 | readonly state: string; |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | /** One thing that is wrong, keyed so a history can tell it is the same thing. */ | |
| 33 | export interface Degraded { | |
| 34 | /** `disk`, `errors`, `queue:<kind>` or `stuck:<runId>`: stable while the condition holds. */ | |
| 35 | readonly cause: string; | |
| 36 | readonly sentence: string; | |
| 37 | /** When the forge's own evidence says it began; `at` where none is on record. */ | |
| 38 | readonly since: string; | |
| 39 | /** Where to look, or null. */ | |
| 40 | readonly href: string | null; | |
| 41 | } | |
| 42 | ||
| 32 | 43 | export interface Vitals { |
| 33 | 44 | readonly ok: boolean; |
| 34 | 45 | readonly needs: string[]; |
| 46 | readonly degraded: Degraded[]; | |
| 35 | 47 | readonly disk: { freeBytes: number; totalBytes: number }; |
| 36 | 48 | readonly memory: { |
| 37 | 49 | rssBytes: number; |
| @@ -62,6 +74,17 @@ export interface VitalsInput { | ||
| 62 | 74 | export function vitals(input: VitalsInput): Vitals { |
| 63 | 75 | const now = input.now ?? Date.now(); |
| 64 | 76 | const needs: string[] = []; |
| 77 | const degraded: Degraded[] = []; | |
| 78 | const at = new Date(now).toISOString(); | |
| 79 | const need = ( | |
| 80 | cause: string, | |
| 81 | sentence: string, | |
| 82 | since: string, | |
| 83 | href: string | null, | |
| 84 | ) => { | |
| 85 | needs.push(sentence); | |
| 86 | degraded.push({ cause, sentence, since, href }); | |
| 87 | }; | |
| 65 | 88 | |
| 66 | 89 | const fs = statfsSync(input.dataDir); |
| 67 | 90 | const disk = { |
| @@ -69,16 +92,22 @@ export function vitals(input: VitalsInput): Vitals { | ||
| 69 | 92 | totalBytes: Number(fs.blocks) * Number(fs.bsize), |
| 70 | 93 | }; |
| 71 | 94 | if (disk.totalBytes > 0 && disk.freeBytes / disk.totalBytes < DISK_FREE_MIN) |
| 72 | needs.push( | |
| 95 | need( | |
| 96 | "disk", | |
| 73 | 97 | `Disk is nearly full: ${Math.round((disk.freeBytes / disk.totalBytes) * 100)}% free.`, |
| 98 | at, | |
| 99 | null, | |
| 74 | 100 | ); |
| 75 | 101 | |
| 76 | 102 | const queue: Record<string, number> = {}; |
| 77 | 103 | for (const row of queueDepth(input.db, now)) { |
| 78 | 104 | queue[row.kind] = row.queued; |
| 79 | 105 | if (now - new Date(row.oldest).getTime() > QUEUE_WAIT_MAX_MS) |
| 80 | needs.push( | |
| 106 | need( | |
| 107 | `queue:${row.kind}`, | |
| 81 | 108 | `The ${row.kind} queue has waited over ${QUEUE_WAIT_MAX_MS / 60_000} minutes.`, |
| 109 | row.oldest, | |
| 110 | "/settings/capacity", | |
| 82 | 111 | ); |
| 83 | 112 | } |
| 84 | 113 | |
| @@ -93,10 +122,18 @@ export function vitals(input: VitalsInput): Vitals { | ||
| 93 | 122 | since: run.heartbeatAt ?? run.startedAt ?? run.createdAt, |
| 94 | 123 | }); |
| 95 | 124 | } |
| 96 | if (stuck.length > 0) | |
| 125 | if (stuck.length > 0) { | |
| 97 | 126 | needs.push( |
| 98 | 127 | `${stuck.length} run${stuck.length === 1 ? " is" : "s are"} stuck.`, |
| 99 | 128 | ); |
| 129 | for (const s of stuck) | |
| 130 | degraded.push({ | |
| 131 | cause: `stuck:${s.runId}`, | |
| 132 | sentence: "A run is stuck.", | |
| 133 | since: s.since, | |
| 134 | href: `/${s.repo}/runs/${s.runId}`, | |
| 135 | }); | |
| 136 | } | |
| 100 | 137 | |
| 101 | 138 | const apps = { serving: 0, asleep: 0, previews: 0 }; |
| 102 | 139 | for (const e of input.envs ?? []) { |
| @@ -107,13 +144,17 @@ export function vitals(input: VitalsInput): Vitals { | ||
| 107 | 144 | |
| 108 | 145 | const seen = recentErrors(5, now); |
| 109 | 146 | if (seen.lastHour >= ERROR_BURST) |
| 110 | needs.push( | |
| 147 | need( | |
| 148 | "errors", | |
| 111 | 149 | `${seen.lastHour} errors in the last hour (${ERROR_BURST} or more is a burst).`, |
| 150 | at, | |
| 151 | null, | |
| 112 | 152 | ); |
| 113 | 153 | |
| 114 | 154 | return { |
| 115 | 155 | ok: needs.length === 0, |
| 116 | 156 | needs, |
| 157 | degraded, | |
| 117 | 158 | disk, |
| 118 | 159 | memory: { |
| 119 | 160 | rssBytes: process.memoryUsage().rss, |
| @@ -124,6 +165,6 @@ export function vitals(input: VitalsInput): Vitals { | ||
| 124 | 165 | stuck, |
| 125 | 166 | apps, |
| 126 | 167 | errors: { lastHour: seen.lastHour, latest: seen.latest }, |
| 127 | at: new Date(now).toISOString(), | |
| 168 | at, | |
| 128 | 169 | }; |
| 129 | 170 | } |
test/vitals-degraded.test.ts added +62
| @@ -0,0 +1,62 @@ | ||
| 1 | /** Each degraded cause, keyed and dated by the forge's own evidence — no server. */ | |
| 2 | ||
| 3 | import type { Database } from "bun:sqlite"; | |
| 4 | import { expect, test } from "bun:test"; | |
| 5 | import { tmpdir } from "node:os"; | |
| 6 | import { createRun } from "../src/core/runs.ts"; | |
| 7 | import { migrations } from "../src/core/schema.ts"; | |
| 8 | import { openDb } from "../src/kernel/db.ts"; | |
| 9 | import { QUEUE_WAIT_MAX_MS, vitals } from "../src/kernel/vitals.ts"; | |
| 10 | ||
| 11 | const NOW = Date.parse("2026-10-13T12:00:00.000Z"); | |
| 12 | ||
| 13 | function fixture() { | |
| 14 | const db = openDb(":memory:", migrations); | |
| 15 | db.query("INSERT INTO user (id, username, email, password_hash) VALUES ('u1', 'alice', '[email protected]', 'x')").run(); | |
| 16 | db.query("INSERT INTO repo (id, owner_id, name) VALUES ('r1', 'u1', 'r')").run(); | |
| 17 | return db; | |
| 18 | } | |
| 19 | const read = (db: Database) => vitals({ db, dataDir: tmpdir(), now: NOW }); | |
| 20 | const iso = (ms: number) => new Date(ms).toISOString(); | |
| 21 | ||
| 22 | test("a healthy forge has no degraded entries", () => { | |
| 23 | const v = read(fixture()); | |
| 24 | expect(v.degraded).toEqual([]); | |
| 25 | expect(v.needs).toEqual([]); | |
| 26 | }); | |
| 27 | ||
| 28 | test("an old queue is queue:<kind>, since its oldest run", () => { | |
| 29 | const db = fixture(); | |
| 30 | const oldest = iso(NOW - QUEUE_WAIT_MAX_MS - 60_000); | |
| 31 | const a = createRun(db, { repoId: "r1", kind: "agent" }); | |
| 32 | const b = createRun(db, { repoId: "r1", kind: "agent" }); | |
| 33 | db.query("UPDATE run SET created_at=?2 WHERE id=?1").run(a.id, oldest); | |
| 34 | db.query("UPDATE run SET created_at=?2 WHERE id=?1").run(b.id, iso(NOW - 1000)); | |
| 35 | const v = read(db); | |
| 36 | const d = v.degraded.filter((x) => x.cause === "queue:agent"); | |
| 37 | expect(d).toHaveLength(1); | |
| 38 | expect(d[0]!.since).toBe(oldest); | |
| 39 | expect(d[0]!.sentence).toBe(v.needs[0]!); | |
| 40 | expect(read(db).degraded).toEqual(v.degraded); | |
| 41 | }); | |
| 42 | ||
| 43 | test("each stuck run is its own entry, since it last moved", () => { | |
| 44 | const db = fixture(); | |
| 45 | const beat = [iso(NOW - 600_000), iso(NOW - 900_000)]; | |
| 46 | const runs = beat.map((t) => { | |
| 47 | const run = createRun(db, { repoId: "r1", kind: "deploy", env: "production" }); | |
| 48 | db.query( | |
| 49 | "UPDATE run SET state='running', started_at=?2, heartbeat_at=?2 WHERE id=?1", | |
| 50 | ).run(run.id, t); | |
| 51 | return run; | |
| 52 | }); | |
| 53 | const d = read(db).degraded.filter((x) => x.cause.startsWith("stuck:")); | |
| 54 | expect(d.map((x) => x.cause).sort()).toEqual( | |
| 55 | runs.map((r) => `stuck:${r.id}`).sort(), | |
| 56 | ); | |
| 57 | for (const [i, r] of runs.entries()) { | |
| 58 | const e = d.find((x) => x.cause === `stuck:${r.id}`)!; | |
| 59 | expect(e.since).toBe(beat[i]!); | |
| 60 | expect(e.href).toBe(`/alice/r/runs/${r.id}`); | |
| 61 | } | |
| 62 | }); |
test/vitals.test.ts +1 −1
| @@ -80,7 +80,7 @@ test("a site admin with a read-only token gets the shape; others get the short o | ||
| 80 | 80 | const v = (await res.json()) as Record<string, any>; |
| 81 | 81 | expect(Object.keys(v).sort()).toEqual( |
| 82 | 82 | [ |
| 83 | "apps", "at", "checkedAt", "commit", "disk", "draining", "errors", | |
| 83 | "apps", "at", "checkedAt", "commit", "degraded", "disk", "draining", "errors", | |
| 84 | 84 | "incidents", "memory", "needs", "ok", "queue", "rollout", "stale", "stuck", |
| 85 | 85 | "version", |
| 86 | 86 | ].sort(), |
new above
offline