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

Closed opened this
Closed without merging

scout/forge:scout/1295-vitals-names-each-degraded-cause-with-wh → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +110 −6
scripts/gate.ts +1
@@ -107,6 +107,7 @@ export const GATE: readonly string[] = [
107107 "test/migrate.test.ts",
108108 "test/schema-additive.test.ts",
109109 "test/schema-migrations.test.ts",
110 "test/vitals-degraded.test.ts",
110111 "test/vitals.test.ts",
111112 "test/version.test.ts",
112113 "test/watches.test.ts",
src/kernel/vitals.ts +46 −5
@@ -29,9 +29,21 @@ export interface EnvCount {
2929 readonly state: string;
3030 }
3131
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
3243 export interface Vitals {
3344 readonly ok: boolean;
3445 readonly needs: string[];
46 readonly degraded: Degraded[];
3547 readonly disk: { freeBytes: number; totalBytes: number };
3648 readonly memory: {
3749 rssBytes: number;
@@ -62,6 +74,17 @@ export interface VitalsInput {
6274 export function vitals(input: VitalsInput): Vitals {
6375 const now = input.now ?? Date.now();
6476 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 };
6588
6689 const fs = statfsSync(input.dataDir);
6790 const disk = {
@@ -69,16 +92,22 @@ export function vitals(input: VitalsInput): Vitals {
6992 totalBytes: Number(fs.blocks) * Number(fs.bsize),
7093 };
7194 if (disk.totalBytes > 0 && disk.freeBytes / disk.totalBytes < DISK_FREE_MIN)
72 needs.push(
95 need(
96 "disk",
7397 `Disk is nearly full: ${Math.round((disk.freeBytes / disk.totalBytes) * 100)}% free.`,
98 at,
99 null,
74100 );
75101
76102 const queue: Record<string, number> = {};
77103 for (const row of queueDepth(input.db, now)) {
78104 queue[row.kind] = row.queued;
79105 if (now - new Date(row.oldest).getTime() > QUEUE_WAIT_MAX_MS)
80 needs.push(
106 need(
107 `queue:${row.kind}`,
81108 `The ${row.kind} queue has waited over ${QUEUE_WAIT_MAX_MS / 60_000} minutes.`,
109 row.oldest,
110 "/settings/capacity",
82111 );
83112 }
84113
@@ -93,10 +122,18 @@ export function vitals(input: VitalsInput): Vitals {
93122 since: run.heartbeatAt ?? run.startedAt ?? run.createdAt,
94123 });
95124 }
96 if (stuck.length > 0)
125 if (stuck.length > 0) {
97126 needs.push(
98127 `${stuck.length} run${stuck.length === 1 ? " is" : "s are"} stuck.`,
99128 );
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 }
100137
101138 const apps = { serving: 0, asleep: 0, previews: 0 };
102139 for (const e of input.envs ?? []) {
@@ -107,13 +144,17 @@ export function vitals(input: VitalsInput): Vitals {
107144
108145 const seen = recentErrors(5, now);
109146 if (seen.lastHour >= ERROR_BURST)
110 needs.push(
147 need(
148 "errors",
111149 `${seen.lastHour} errors in the last hour (${ERROR_BURST} or more is a burst).`,
150 at,
151 null,
112152 );
113153
114154 return {
115155 ok: needs.length === 0,
116156 needs,
157 degraded,
117158 disk,
118159 memory: {
119160 rssBytes: process.memoryUsage().rss,
@@ -124,6 +165,6 @@ export function vitals(input: VitalsInput): Vitals {
124165 stuck,
125166 apps,
126167 errors: { lastHour: seen.lastHour, latest: seen.latest },
127 at: new Date(now).toISOString(),
168 at,
128169 };
129170 }
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
8080 const v = (await res.json()) as Record<string, any>;
8181 expect(Object.keys(v).sort()).toEqual(
8282 [
83 "apps", "at", "checkedAt", "commit", "disk", "draining", "errors",
83 "apps", "at", "checkedAt", "commit", "degraded", "disk", "draining", "errors",
8484 "incidents", "memory", "needs", "ok", "queue", "rollout", "stale", "stuck",
8585 "version",
8686 ].sort(),

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs