Public: anyone can read this; the crew can change it.
Vitals raises one need per domain whose last drill is red or eight days old, and nothing else changes #2055
scout/forge:scout/2019-vitals-raises-one-need-per-domain-whose → main 9 commits fork 4 threads · 4 unresolved
7 files
changed +171
−8
README.md +1 −1
Diff not shown — a line 4,279 characters wide (limit 2,000). +1 −1
View filesrc/actions/resilience.ts +2 −2
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | import type { App } from "../app.ts"; |
| 9 | import { lastDrill, type LastDrill } from "../core/domain_drills.ts"; | |
| 9 | import { lastDrill, type KeptDrill } from "../core/domain_drills.ts"; | |
| 10 | 10 | import type { User } from "../core/users.ts"; |
| 11 | 11 | import { DOMAINS } from "../kernel/domains.ts"; |
| 12 | 12 | import { err, ok, type Result } from "../kernel/result.ts"; |
| @@ -19,7 +19,7 @@ export interface ResilienceRow { | ||
| 19 | 19 | readonly backMinutes: number; |
| 20 | 20 | /** A drill's name and its last result (none yet), or why there is none. */ |
| 21 | 21 | readonly drill: |
| 22 | | { readonly name: string; readonly last: LastDrill | null } | |
| 22 | | { readonly name: string; readonly last: KeptDrill | null } | |
| 23 | 23 | | { readonly name: null; readonly why: string }; |
| 24 | 24 | } |
| 25 | 25 |
src/core/domain_drills.ts +15 −3
| @@ -2,7 +2,7 @@ | ||
| 2 | 2 | |
| 3 | 3 | import type { Database } from "bun:sqlite"; |
| 4 | 4 | import { id } from "../kernel/id.ts"; |
| 5 | import type { Observed, Verdict } from "../kernel/domain_drill.ts"; | |
| 5 | import type { LastDrill, Observed, Verdict } from "../kernel/domain_drill.ts"; | |
| 6 | 6 | |
| 7 | 7 | export interface DrillRecord extends Verdict { |
| 8 | 8 | readonly id: string; |
| @@ -26,7 +26,19 @@ export function recordDrill( | ||
| 26 | 26 | return { id: row, domain, backSeconds, ...verdict }; |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | export interface LastDrill { | |
| 29 | /** Each domain's newest drill, one query. */ | |
| 30 | export function newestDrills(db: Database): LastDrill[] { | |
| 31 | return ( | |
| 32 | db | |
| 33 | .query( | |
| 34 | `SELECT domain, at, green, why FROM domain_drill d | |
| 35 | WHERE id = (SELECT id FROM domain_drill WHERE domain = d.domain ORDER BY at DESC, id DESC LIMIT 1)`, | |
| 36 | ) | |
| 37 | .all() as { domain: string; at: string; green: number; why: string }[] | |
| 38 | ).map((r) => ({ domain: r.domain, at: r.at, green: r.green === 1, why: r.why })); | |
| 39 | } | |
| 40 | ||
| 41 | export interface KeptDrill { | |
| 30 | 42 | readonly green: boolean; |
| 31 | 43 | readonly why: string; |
| 32 | 44 | readonly backSeconds: number; |
| @@ -34,7 +46,7 @@ export interface LastDrill { | ||
| 34 | 46 | } |
| 35 | 47 | |
| 36 | 48 | /** The newest drill kept for a domain, or null when none has run. */ |
| 37 | export function lastDrill(db: Database, domain: string): LastDrill | null { | |
| 49 | export function lastDrill(db: Database, domain: string): KeptDrill | null { | |
| 38 | 50 | const r = db |
| 39 | 51 | .query<{ green: number; why: string; back_seconds: number; at: string }, [string]>( |
| 40 | 52 | `SELECT green, why, back_seconds, at FROM domain_drill |
src/kernel/domain_drill.ts +68
| @@ -37,3 +37,71 @@ export function drillVerdict( | ||
| 37 | 37 | } |
| 38 | 38 | return { green: true, why: `every phrase seen; back in ${backSeconds}s of ${limit}s` }; |
| 39 | 39 | } |
| 40 | ||
| 41 | /** A domain's newest drill: when, and its verdict. */ | |
| 42 | export interface LastDrill extends Verdict { | |
| 43 | readonly domain: string; | |
| 44 | readonly at: string; | |
| 45 | } | |
| 46 | ||
| 47 | export interface DomainNeed { | |
| 48 | readonly key: string; | |
| 49 | readonly sentence: string; | |
| 50 | readonly since: string; | |
| 51 | } | |
| 52 | ||
| 53 | const STALE_MS = 8 * 24 * 3_600_000; | |
| 54 | ||
| 55 | /** The failing phrase when the reason quotes one, else the whole reason. */ | |
| 56 | const phraseOf = (why: string): string => /"([^"]+)"/.exec(why)?.[1] ?? why; | |
| 57 | ||
| 58 | /** How an owner runs one: the request that keeps a drill (README, "Keeping a domain drill"). */ | |
| 59 | const HOW = "Run it with POST /api/v1/drills"; | |
| 60 | ||
| 61 | /** | |
| 62 | * One need per drillable domain whose newest drill is red, or whose newest | |
| 63 | * (green) drill is over eight days old. A green drill replaces the red one as | |
| 64 | * the newest, so it clears. Domains never drilled are one need between them | |
| 65 | * (`domain-drill:never`), once the forge's first-seen time is eight days | |
| 66 | * old, so a forge nobody has drilled yet is one card, not one per domain. | |
| 67 | */ | |
| 68 | export function domainDrillNeeds( | |
| 69 | domains: readonly Domain[], | |
| 70 | newest: readonly LastDrill[], | |
| 71 | now: number, | |
| 72 | firstSeen: string | undefined, | |
| 73 | ): DomainNeed[] { | |
| 74 | const needs: DomainNeed[] = []; | |
| 75 | const never: string[] = []; | |
| 76 | for (const d of domains) { | |
| 77 | if (d.drill.name === null) continue; | |
| 78 | const last = newest.find((r) => r.domain === d.key); | |
| 79 | if (last === undefined) { | |
| 80 | never.push(d.key); | |
| 81 | continue; | |
| 82 | } | |
| 83 | if (!last.green) { | |
| 84 | needs.push({ | |
| 85 | key: `domain-drill:${d.key}`, | |
| 86 | sentence: `The ${d.key} drill failed on ${last.at.slice(0, 10)}: ${phraseOf(last.why)}. ${HOW} {"domain":"${d.key}"} until it passes.`, | |
Raw HTTP in a card for a person is nerdy, and the body shown lacks seen and backSeconds, so it 422s. Plain words plus a place on the page/tty to run it. |
||
| 87 | since: last.at, | |
| 88 | }); | |
| 89 | continue; | |
| 90 | } | |
| 91 | const from = Date.parse(last.at); | |
| 92 | if (!Number.isFinite(from) || !(now - from > STALE_MS)) continue; | |
| 93 | needs.push({ | |
| 94 | key: `domain-drill:${d.key}`, | |
| 95 | sentence: `No ${d.key} drill has run in 8 days; the last one passed on ${last.at.slice(0, 10)}. ${HOW} {"domain":"${d.key}"}.`, | |
| 96 | since: new Date(from + STALE_MS).toISOString(), | |
| 97 | }); | |
| 98 | } | |
| 99 | const from = Date.parse(firstSeen ?? ""); | |
| 100 | if (never.length > 0 && Number.isFinite(from) && now - from > STALE_MS) | |
| 101 | needs.push({ | |
| 102 | key: "domain-drill:never", | |
|
||
| 103 | sentence: `No drill has ever run for ${never.join(", ")}. ${HOW} with {"domain": "<one of those>"}.`, | |
| 104 | since: new Date(from + STALE_MS).toISOString(), | |
| 105 | }); | |
| 106 | return needs; | |
| 107 | } |
src/kernel/vitals.ts +12
| @@ -23,6 +23,9 @@ import { STALE_AFTER_MS } from "../runs/lease.ts"; | ||
| 23 | 23 | import { BUDGETS } from "./budgets.ts"; |
| 24 | 24 | import { lastMirror, mirrorNeed } from "../snapshot/mirror.ts"; |
| 25 | 25 | import { drillNeed, firstSeen as drillFirstSeen, lastVerdict } from "../snapshot/verdict.ts"; |
| 26 | import { newestDrills } from "../core/domain_drills.ts"; | |
| 27 | import { domainDrillNeeds } from "./domain_drill.ts"; | |
| 28 | import { DOMAINS } from "./domains.ts"; | |
| 26 | 29 | import { heldChecks, redSentence } from "../checks/red_main.ts"; |
| 27 | 30 | |
| 28 | 31 | /** Free disk under this share of the volume is something to say. */ |
| @@ -319,6 +322,15 @@ export function vitals(input: VitalsInput): Vitals { | ||
| 319 | 322 | if (owed !== undefined) |
| 320 | 323 | need("drill", owed.sentence, owed.since, "/settings/backups"); |
| 321 | 324 | |
| 325 | // A domain whose last drill is red, or none in eight days, is a card until one passes. | |
| 326 | for (const owed of domainDrillNeeds( | |
| 327 | DOMAINS, | |
| 328 | newestDrills(input.db), | |
| 329 | now, | |
| 330 | drillFirstSeen(input.dataDir), | |
| 331 | )) | |
| 332 | need(owed.key, owed.sentence, owed.since, "/settings/backups"); | |
| 333 | ||
href is /settings/backups for every domain; wrong for compute, auth and runners. Point at the drill docs, or a per-domain target. |
||
| 322 | 334 | // A snapshot that did not land off the box, or a mirror a day behind, is a card until it does. |
| 323 | 335 | const behind = mirrorNeed(lastMirror(input.dataDir), now); |
| 324 | 336 | if (behind !== undefined) need("mirror", behind.sentence, behind.since, "/settings/backups"); |
test/domain-drill-needs.test.ts added +50
| @@ -0,0 +1,50 @@ | ||
| 1 | import { describe, expect, test } from "bun:test"; | |
| 2 | import { domainDrillNeeds, type LastDrill } from "../src/kernel/domain_drill.ts"; | |
| 3 | import { DOMAINS } from "../src/kernel/domains.ts"; | |
| 4 | ||
| 5 | const now = Date.parse("2026-10-16T12:00:00Z"); | |
| 6 | const day = 86_400_000; | |
| 7 | const iso = (ago: number): string => new Date(now - ago).toISOString(); | |
| 8 | const compute = DOMAINS.find((d) => d.key === "compute")!; | |
| 9 | expect(compute.drill.name).not.toBeNull(); | |
| 10 | const only = [compute]; | |
| 11 | const drill = (green: boolean, ago: number, why = "ok"): LastDrill => ({ domain: "compute", green, at: iso(ago), why }); | |
| 12 | ||
| 13 | describe("domain drill needs", () => { | |
| 14 | test("red raises a need naming the phrase", () => { | |
| 15 | const n = domainDrillNeeds(only, [drill(false, day, 'the forge never said "pod restarted"')], now, undefined); | |
| 16 | expect(n).toHaveLength(1); | |
| 17 | expect(n[0]!.sentence).toContain("compute"); | |
| 18 | expect(n[0]!.sentence).toContain("pod restarted"); | |
| 19 | }); | |
| 20 | test("a newer green clears it", () => { | |
| 21 | expect(domainDrillNeeds(only, [drill(true, day)], now, undefined)).toEqual([]); | |
| 22 | }); | |
| 23 | test("over eight days raises it, under does not", () => { | |
| 24 | expect(domainDrillNeeds(only, [drill(true, 9 * day)], now, undefined)).toHaveLength(1); | |
| 25 | expect(domainDrillNeeds(only, [drill(true, 7 * day)], now, undefined)).toEqual([]); | |
| 26 | }); | |
| 27 | test("no drill: first-seen over eight days ago raises, else nothing", () => { | |
| 28 | expect(domainDrillNeeds(only, [], now, iso(9 * day))).toHaveLength(1); | |
| 29 | expect(domainDrillNeeds(only, [], now, iso(9 * day))[0]!.key).toBe("domain-drill:never"); | |
| 30 | expect(domainDrillNeeds(only, [], now, iso(2 * day))).toEqual([]); | |
| 31 | expect(domainDrillNeeds(only, [], now, undefined)).toEqual([]); | |
| 32 | }); | |
| 33 | test("never-drilled domains are one need, not one each", () => { | |
| 34 | const drillable = DOMAINS.filter((d) => d.drill.name !== null); | |
| 35 | expect(drillable.length).toBeGreaterThan(1); | |
| 36 | const n = domainDrillNeeds(DOMAINS, [], now, iso(9 * day)); | |
| 37 | expect(n).toHaveLength(1); | |
| 38 | for (const d of drillable) expect(n[0]!.sentence).toContain(d.key); | |
| 39 | const some = domainDrillNeeds(DOMAINS, [{ ...drill(true, day), domain: drillable[0]!.key }], now, iso(9 * day)); | |
| 40 | expect(some[0]!.sentence).not.toContain(drillable[0]!.key); | |
| 41 | }); | |
| 42 | test("the sentence carries the request to run", () => { | |
| 43 | expect(domainDrillNeeds(only, [drill(false, day)], now, undefined)[0]!.sentence).toContain("POST /api/v1/drills"); | |
| 44 | }); | |
| 45 | test("no drillable domain raises nothing", () => { | |
| 46 | const none = DOMAINS.filter((d) => d.drill.name === null); | |
| 47 | expect(none.length).toBeGreaterThan(0); | |
| 48 | expect(domainDrillNeeds(none, [{ ...drill(false, day), domain: none[0]!.key }], now, iso(30 * day))).toEqual([]); | |
| 49 | }); | |
| 50 | }); |
test/vitals.test.ts +23 −2
| @@ -154,8 +154,8 @@ test("the number of queries does not grow with runs", () => { | ||
| 154 | 154 | if (i % 2) app.db.query("UPDATE run SET state='running' WHERE id=?1").run(r.id); |
| 155 | 155 | } |
| 156 | 156 | expect(count()).toBe(before); |
| 157 | // The queue, the running runs, the runs killed twice for their budget, the lane's depth and finished checks, and the red mains holding pulls' checks. | |
| 158 | expect(before).toBe(6); | |
| 157 | // The queue, the running runs, the runs killed twice for their budget, the lane's depth and finished checks, the red mains holding pulls' checks, and each domain's newest drill. | |
| 158 | expect(before).toBe(7); | |
| 159 | 159 | }); |
| 160 | 160 | |
| 161 | 161 | test("errors are counted, bounded, redacted, and a burst is said", async () => { |
| @@ -443,3 +443,24 @@ test("a lane older than its budget is degraded with its cause; a silent run is s | ||
| 443 | 443 | expect(stuck?.sentence).toBe(`Run ${live.id} is stuck: no heartbeat for 4 min (limit 3).`); |
| 444 | 444 | app.db.query("DELETE FROM run").run(); |
| 445 | 445 | }); |
| 446 | ||
| 447 | test("a red domain drill is a need, and a later green one clears it", () => { | |
| 448 | const keys = () => | |
| 449 | vitals({ db: app.db, signals: app.signals, dataDir }).degraded.map((d) => d.cause); | |
| 450 | const put = (green: boolean, why: string, at: string) => | |
| 451 | app.db | |
| 452 | .query( | |
| 453 | `INSERT INTO domain_drill (id, domain, run_by, health, board, back_seconds, green, why, at) | |
| 454 | VALUES (?1, 'compute', 'alice', '', '', 1, ?2, ?3, ?4)`, | |
| 455 | ) | |
| 456 | .run(`drill-${at}`, green ? 1 : 0, why, at); | |
| 457 | expect(keys()).not.toContain("domain-drill:compute"); | |
| 458 | put(false, 'the forge never said "pod restarted"', new Date(Date.now() - 3_600_000).toISOString()); | |
| 459 | const red = vitals({ db: app.db, signals: app.signals, dataDir }); | |
| 460 | const card = red.degraded.find((d) => d.cause === "domain-drill:compute"); | |
| 461 | expect(card?.href).toBe("/settings/backups"); | |
| 462 | expect(card?.sentence).toContain("pod restarted"); | |
| 463 | expect(red.needs).toContain(card!.sentence); | |
| 464 | put(true, "every phrase seen", new Date(Date.now() + 1000).toISOString()); | |
| 465 | expect(keys()).not.toContain("domain-drill:compute"); | |
| 466 | }); |
new above
offline
'Run it again until it passes' does not say how. Name the door (POST /api/v1/drills, admin) so the card is actionable, and mirror it in the README.