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 #2025

Parked opened this
Closed without merging

scout/forge:scout/2019-vitals-raises-one-need-per-domain-whose → main 3 commits fork 1 thread · 1 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
6 files changed +139 −3
scripts/gate.ts +1
@@ -28,6 +28,7 @@ export type Exclusion = {
2828
2929 export const GATE: readonly string[] = [
3030 "test/domain-drill.test.ts",
31 "test/domain-drill-needs.test.ts",
3132 "test/conflicts.test.ts",
3233 "test/conflicts-tui.test.ts",
3334 "test/accounts-retire.test.ts",
src/core/domain_drills.ts +13 −1
@@ -2,7 +2,7 @@
22
33 import type { Database } from "bun:sqlite";
44 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";
66
77 export interface DrillRecord extends Verdict {
88 readonly id: string;
@@ -25,3 +25,15 @@ export function recordDrill(
2525 ).run(row, domain, by, seen.health, seen.board, backSeconds, verdict.green ? 1 : 0, verdict.why);
2626 return { id: row, domain, backSeconds, ...verdict };
2727 }
28
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 }
src/kernel/domain_drill.ts +54
@@ -37,3 +37,57 @@ export function drillVerdict(
3737 }
3838 return { green: true, why: `every phrase seen; back in ${backSeconds}s of ${limit}s` };
3939 }
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 /**
59 * One need per drillable domain whose newest drill is red, or whose newest
60 * (green) drill, or with none the forge's first-seen time, is over eight days
61 * old. A green drill replaces the red one as the newest, so it clears.
62 */
63 export function domainDrillNeeds(
64 domains: readonly Domain[],
65 newest: readonly LastDrill[],
66 now: number,
67 firstSeen: string | undefined,
68 ): DomainNeed[] {
69 const needs: DomainNeed[] = [];
70 for (const d of domains) {
71 if (d.drill.name === null) continue;
72 const last = newest.find((r) => r.domain === d.key);
73 if (last !== undefined && !last.green) {
74 needs.push({
75 key: `domain-drill:${d.key}`,
76 sentence: `The ${d.key} drill failed on ${last.at.slice(0, 10)}: ${phraseOf(last.why)}. Run it again until it passes.`,
77 since: last.at,
78 });
79 continue;
80 }
81 const from = Date.parse(last?.at ?? firstSeen ?? "");
82 if (!Number.isFinite(from) || !(now - from > STALE_MS)) continue;
83 needs.push({
84 key: `domain-drill:${d.key}`,
85 sentence:
86 last === undefined
87 ? `No ${d.key} drill has run in 8 days; none has ever passed.`
88 : `No ${d.key} drill has run in 8 days; the last one passed on ${last.at.slice(0, 10)}.`,
89 since: new Date(from + STALE_MS).toISOString(),
90 });
91 }
92 return needs;
93 }
src/kernel/vitals.ts +12
@@ -23,6 +23,9 @@ import { STALE_AFTER_MS } from "../runs/lease.ts";
2323 import { BUDGETS } from "./budgets.ts";
2424 import { lastMirror, mirrorNeed } from "../snapshot/mirror.ts";
2525 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";
2629 import { heldChecks, redSentence } from "../checks/red_main.ts";
2730
2831 /** Free disk under this share of the volume is something to say. */
@@ -319,6 +322,15 @@ export function vitals(input: VitalsInput): Vitals {
319322 if (owed !== undefined)
320323 need("drill", owed.sentence, owed.since, "/settings/backups");
321324
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

No test reaches this loop. Add a vitals() test with a red row and a later green row. Use a single combined need or a better link than /settings/backups.

322334 // A snapshot that did not land off the box, or a mirror a day behind, is a card until it does.
323335 const behind = mirrorNeed(lastMirror(input.dataDir), now);
324336 if (behind !== undefined) need("mirror", behind.sentence, behind.since, "/settings/backups");
test/domain-drill-needs.test.ts added +36
@@ -0,0 +1,36 @@
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 state = { ...DOMAINS.find((d) => d.key === "state")!, drill: { name: "fill-disk" } };
9 const only = [state];
10 const drill = (green: boolean, ago: number, why = "ok"): LastDrill => ({ domain: "state", green, at: iso(ago), why });
11
12 describe("domain drill needs", () => {
13 test("red raises a need naming the phrase", () => {
14 const n = domainDrillNeeds(only, [drill(false, day, 'the forge never said "Disk is nearly full:"')], now, undefined);
15 expect(n).toHaveLength(1);
16 expect(n[0]!.sentence).toContain("state");
17 expect(n[0]!.sentence).toContain("Disk is nearly full:");
18 });
19 test("a newer green clears it", () => {
20 expect(domainDrillNeeds(only, [drill(true, day)], now, undefined)).toEqual([]);
21 });
22 test("over eight days raises it, under does not", () => {
23 expect(domainDrillNeeds(only, [drill(true, 9 * day)], now, undefined)).toHaveLength(1);
24 expect(domainDrillNeeds(only, [drill(true, 7 * day)], now, undefined)).toEqual([]);
25 });
26 test("no drill: first-seen over eight days ago raises, else nothing", () => {
27 expect(domainDrillNeeds(only, [], now, iso(9 * day))).toHaveLength(1);
28 expect(domainDrillNeeds(only, [], now, iso(2 * day))).toEqual([]);
29 expect(domainDrillNeeds(only, [], now, undefined)).toEqual([]);
30 });
31 test("no drillable domain raises nothing", () => {
32 const none = DOMAINS.filter((d) => d.drill.name === null);
33 expect(none.length).toBeGreaterThan(0);
34 expect(domainDrillNeeds(none, [{ ...drill(false, day), domain: none[0]!.key }], now, iso(30 * day))).toEqual([]);
35 });
36 });
test/vitals.test.ts +23 −2
@@ -153,8 +153,8 @@ test("the number of queries does not grow with runs", () => {
153153 if (i % 2) app.db.query("UPDATE run SET state='running' WHERE id=?1").run(r.id);
154154 }
155155 expect(count()).toBe(before);
156 // 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.
157 expect(before).toBe(6);
156 // 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.
157 expect(before).toBe(7);
158158 });
159159
160160 test("errors are counted, bounded, redacted, and a burst is said", async () => {
@@ -438,3 +438,24 @@ test("a lane older than its budget is degraded with its cause; a silent run is s
438438 expect(stuck?.sentence).toBe(`Run ${live.id} is stuck: no heartbeat for 4 min (limit 3).`);
439439 app.db.query("DELETE FROM run").run();
440440 });
441
442 test("a red domain drill is a need, and a later green one clears it", () => {
443 const keys = () =>
444 vitals({ db: app.db, signals: app.signals, dataDir }).degraded.map((d) => d.cause);
445 const put = (green: boolean, why: string, at: string) =>
446 app.db
447 .query(
448 `INSERT INTO domain_drill (id, domain, run_by, health, board, back_seconds, green, why, at)
449 VALUES (?1, 'compute', 'alice', '', '', 1, ?2, ?3, ?4)`,
450 )
451 .run(`drill-${at}`, green ? 1 : 0, why, at);
452 expect(keys()).not.toContain("domain-drill:compute");
453 put(false, 'the forge never said "pod restarted"', "2026-10-01T00:00:00.000Z");
454 const red = vitals({ db: app.db, signals: app.signals, dataDir });
455 const card = red.degraded.find((d) => d.cause === "domain-drill:compute");
456 expect(card?.href).toBe("/settings/backups");
457 expect(card?.sentence).toContain("pod restarted");
458 expect(red.needs).toContain(card!.sentence);
459 put(true, "every phrase seen", new Date().toISOString());
460 expect(keys()).not.toContain("domain-drill:compute");
461 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs