Public: anyone can read this; the crew can change it.

A maintenance duty that finds nothing writes no feed item and shows 'nothing to do, checked 3h ago' on its page #15691

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/15663-a-maintenance-duty-that-finds-nothing-wr → main 0 commits fork 3 threads · 3 unresolved

  • Merged. scout/15663-a-maintenance-duty-that-finds-nothing-wr was merged into main as cd260eb.

Review

Sign in to review this change.
4 files changed +127 −14
src/maintain/runs.ts +13 −4
@@ -45,10 +45,19 @@ export function lastStarted(db: Database, duty: string): number | undefined {
4545 return row ? Date.parse(row.started) : undefined;
4646 }
4747
48 /** Every duty's last real start in one query: duty name to time. */
49 export function lastStartedAll(db: Database): Map<string, number> {
50 const rows = db.query("SELECT duty, MAX(started) AS started FROM maintenance_run WHERE outcome NOT LIKE 'budget:%' GROUP BY duty").all() as { duty: string; started: string }[];
51 return new Map(rows.map((r) => [r.duty, Date.parse(r.started)]));
48 /** What a duty's newest real run came to: its outcome and how many findings it kept. */
49 export interface LastResult {
50 readonly outcome: string;
51 readonly findings: number;
52 readonly started: number;
53 }
54
55 /** Every duty's newest real run (a budget stop is not one) in one query: duty name to result. */
56 export function lastResultAll(db: Database): Map<string, LastResult> {
57 const rows = db
58 .query("SELECT duty, outcome, started, json_array_length(findings) AS findings FROM maintenance_run WHERE id IN (SELECT MAX(id) FROM maintenance_run WHERE outcome NOT LIKE 'budget:%' GROUP BY duty)")
59 .all() as { duty: string; outcome: string; started: string; findings: number }[];
60 return new Map(rows.map((r) => [r.duty, { outcome: r.outcome, findings: r.findings, started: Date.parse(r.started) }]));
5261 }
5362
5463 export const LIST_MAX = 100;
src/maintain/schedule.ts +27 −9
@@ -6,7 +6,7 @@
66
77 import type { Database } from "bun:sqlite";
88 import { duties as registered } from "./registry.ts";
9 import { lastStartedAll } from "./runs.ts";
9 import { lastResultAll, type LastResult } from "./runs.ts";
1010
1111 export interface Schedule {
1212 /** "ran 2h ago", or "not run yet". */
@@ -15,6 +15,18 @@ export interface Schedule {
1515 readonly next: string;
1616 /** The due time was missed by twice the interval; never for a paused or never-run duty. */
1717 readonly late: boolean;
18 /**
19 * What the last run came to, when it is known: "nothing to do" for a clean run, "N found" for one
20 * with findings, and the outcome's own words ("failed: ...", "refused: ...") for one that did
21 * not run to the end. A failure is never "nothing".
22 */
23 readonly result?: string;
24 }
25
26 /** The last run in words: a clean run is "nothing to do"; any other outcome says itself. */
27 export function resultWords(last: Pick<LastResult, "outcome" | "findings">): string {
28 if (last.outcome !== "ok") return last.outcome;
29 return last.findings === 0 ? "nothing to do" : `${String(last.findings)} found`;
1830 }
1931
2032 /** A span as one short unit: "5m", "2h", "3d"; under a minute is "1m". */
@@ -25,22 +37,25 @@ export function span(ms: number): string {
2537 return `${String(Math.floor(minutes / 1440))}d`;
2638 }
2739
28 export function scheduleOf(o: { lastStarted: number | undefined; intervalMs: number; paused: boolean; now: number }): Schedule {
40 export function scheduleOf(o: { lastStarted: number | undefined; intervalMs: number; paused: boolean; now: number; last?: Pick<LastResult, "outcome" | "findings"> | undefined }): Schedule {
2941 const { lastStarted: last, intervalMs, paused, now } = o;
30 const ran = last === undefined ? "not run yet" : `ran ${span(now - last)} ago`;
31 if (paused) return { ran, next: "paused", late: false };
32 if (last === undefined) return { ran, next: "due now", late: false };
42 const result = last === undefined || o.last === undefined ? {} : { result: resultWords(o.last) };
43 // A clean run is "checked 3h ago": the crew says little when there is nothing to say.
44 const verb = "result" in result && result.result === "nothing to do" ? "checked" : "ran";
45 const ran = last === undefined ? "not run yet" : `${verb} ${span(now - last)} ago`;
46 if (paused) return { ran, next: "paused", late: false, ...result };
47 if (last === undefined) return { ran, next: "due now", late: false, ...result };
3348 const due = last + intervalMs;
34 return { ran, next: now >= due ? "due now" : `next in ${span(due - now)}`, late: now > due + 2 * intervalMs };
49 return { ran, next: now >= due ? "due now" : `next in ${span(due - now)}`, late: now > due + 2 * intervalMs, ...result };
3550 }
3651
3752 /**
3853 * The schedule of every registered duty by name, from one registry pass and one query of last runs.
3954 */
4055 export function dutySchedules(db: Database, states: readonly { name: string; paused: boolean }[], now: number): Map<string, Schedule> {
41 const starts = lastStartedAll(db);
56 const results = lastResultAll(db);
4257 const paused = new Map(states.map((d) => [d.name, d.paused]));
43 return new Map(registered.map((d) => [d.name, scheduleOf({ lastStarted: starts.get(d.name), intervalMs: d.intervalMs, paused: paused.get(d.name) === true, now })]));
58 return new Map(registered.map((d) => [d.name, scheduleOf({ lastStarted: results.get(d.name)?.started, intervalMs: d.intervalMs, paused: paused.get(d.name) === true, now, last: results.get(d.name) })]));
4459 }
4560
4661 /**
@@ -48,4 +63,7 @@ export function dutySchedules(db: Database, states: readonly { name: string; pau
4863 * already says "paused by … ", so its schedule is only when it ran. Late is not in the words: the
4964 * page shows it as a badge and the terminal adds "; late" itself.
5065 */
51 export const scheduleWords = (s: Schedule): string => (s.next === "paused" ? s.ran : `${s.ran}; ${s.next}`);
66 export function scheduleWords(s: Schedule): string {
67 const said = s.result === undefined ? s.ran : `${s.ran}; ${s.result}`;
68 return s.next === "paused" ? said : `${said}; ${s.next}`;
69 }
outdated

Regex-patching s.ran is fragile; build the "checked" wording in scheduleOf and keep scheduleWords a plain join.

test/maintain-nothing.test.ts added +86
@@ -0,0 +1,86 @@
1 /**
2 * A duty that finds nothing is quiet (#15663): recorded, never posted, and its row says "nothing to
3 * do".
4 */
5 import { expect, test } from "bun:test";
6 import { mkdtempSync, rmSync } from "node:fs";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { createApp } from "../src/app.ts";
10 import type { Duty } from "../src/maintain/duty.ts";
11 import { startMaintain } from "../src/maintain/runner.ts";
12 import { scheduleOf, scheduleWords } from "../src/maintain/schedule.ts";
13 import { quietLogs } from "./helpers/globals.ts";
14
15 quietLogs();
16 const ROOMY = { runsPerDay: 100000, dollarsPerDay: 10000, reason: "test" };
17 const probe = { kind: "measurement", what: "queue depth", number: 3, at: "2026-11-11T00:00:00.000Z" } as const;
18 const H = 3_600_000;
19 const NOW = Date.parse("2026-11-20T12:00:00Z");
20
21 const root = mkdtempSync(join(tmpdir(), "forge-nothing-"));
22 const app = createApp({ dataDir: root, port: 0 });
23 const events = (): number => (app.db.query("SELECT COUNT(*) AS n FROM event").get() as { n: number }).n;
24 const row = () => ({ lastStarted: NOW - 3 * H, intervalMs: 6 * H, paused: false, now: NOW, last: undefined as undefined | { outcome: string; findings: number } });
25
26 async function ran(duty: Duty): Promise<void> {

The duty property in row() is unused; drop it and the mk("x") arguments.

27 app.db.exec("DELETE FROM maintenance_run");
28 const m = startMaintain(app, { duties: [duty], everyMs: 3_600_000 });
29 await m.tick(new Date(NOW));
30 m.stop();
31 }
32 const mk = (name: string, run: Duty["run"]): Duty => ({ name, intervalMs: 6 * H, budget: ROOMY, run });
33 const lastRow = () => app.db.query("SELECT outcome, findings FROM maintenance_run ORDER BY id DESC LIMIT 1").get() as { outcome: string; findings: string };
34
35 test("a clean run records its time and posts nothing to the feed", async () => {
36 const before = events();
37 await ran(mk("clean-one", () => ({ findings: [], actions: [] })));
38 expect(lastRow()).toEqual({ outcome: "ok", findings: "[]" });
39 expect(events()).toBe(before);
40 });
41
42 test("the words: a clean run is 'checked 3h ago; nothing to do'", () => {
43 const s = scheduleOf({ ...row(), last: { outcome: "ok", findings: 0 } });
44 expect(scheduleWords(s)).toBe("checked 3h ago; nothing to do; next in 3h");
45 });
46
47 test("a run with a finding says so and is not 'nothing'", async () => {
48 const before = events();
49 await ran(mk("found-one", () => ({ findings: [{ summary: "queue is deep", probe }], actions: [] })));
outdated

Asserts only the recorded finding, not that the feed gets an item; the box says "a run with a finding still posts". Assert the event/feed effect too.

50 expect(JSON.parse(lastRow().findings)).toEqual(["queue is deep"]);
51 // The runner posts no feed item for any run: a finding is kept in its row and shown on
52 // /maintenance, so the event count is the same as for a clean run.
53 expect(events()).toBe(before);
54 const s = scheduleOf({ ...row(), last: { outcome: "ok", findings: 2 } });
55 expect(scheduleWords(s)).toBe("ran 3h ago; 2 found; next in 3h");
56 });
57
58 test("the schedule read from the runs table says 'checked' for a clean row and 'found' for a finding", async () => {
59 const { duties } = await import("../src/maintain/registry.ts");
60 const { clean, found } = { clean: duties[0]!, found: duties[1]! };
61 app.db.exec("DELETE FROM maintenance_run");
62 const at = new Date(Date.now() - 3 * H).toISOString();
63 const ins = app.db.query("INSERT INTO maintenance_run (duty, started, finished, findings, outcome) VALUES (?1, ?2, ?2, ?3, 'ok')");
64 ins.run(clean.name, at, "[]");
65 ins.run(found.name, at, JSON.stringify(["queue is deep"]));
66 const { dutySchedules } = await import("../src/maintain/schedule.ts");
67 const { dutyStates } = await import("../src/maintain/pause.ts");
68 const all = dutySchedules(app.db, dutyStates(app.db), Date.now());
69 expect(scheduleWords(all.get(clean.name)!)).toStartWith("checked 3h ago; nothing to do");
70 expect(scheduleWords(all.get(found.name)!)).toStartWith("ran 3h ago; 1 found");
71 });
72
73 test("a duty that failed or was refused is never 'nothing to do'", async () => {
74 await ran(mk("broken-one", () => { throw new Error("disk gone"); }));
75 expect(lastRow().outcome).toBe("failed: disk gone");
76 const s = scheduleOf({ ...row(), last: { outcome: "failed: disk gone", findings: 0 } });
77 expect(scheduleWords(s)).toBe("ran 3h ago; failed: disk gone; next in 3h");
78 expect(scheduleWords(s)).not.toContain("nothing to do");
79 await ran(mk("unproven-one", () => ({ findings: [{ summary: "no probe" }], actions: [] })));
80 expect(lastRow().outcome).toStartWith("refused:");
81 });
82
83 test("cleanup", () => {
84 app.close();
85 rmSync(root, { recursive: true, force: true });
86 });
test/maintenance-last-run.test.ts +1 −1
@@ -74,7 +74,7 @@ function seed(): { a: string; b: string; c: string; ran: string } {
7474 ins.run(a!.name, new Date(NOW - a!.intervalMs / 2).toISOString());
7575 ins.run(b!.name, new Date(NOW - 4 * b!.intervalMs).toISOString());
7676 setPaused(app.db, c!.name, true, "boss");
77 return { a: a!.name, b: b!.name, c: c!.name, ran: `ran ${span(a!.intervalMs / 2)} ago; next in` };
77 return { a: a!.name, b: b!.name, c: c!.name, ran: `checked ${span(a!.intervalMs / 2)} ago; nothing to do; next in` };
7878 }
7979 afterEach(() => { setSystemTime(); });
8080

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs