From 0521a095f9ad0490e6f4ff7866184bf44cd268e5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 17:56:38 -0600 Subject: [PATCH 1/2] feat(pm-console): report a unit that keeps failing on its timer, once, and its mending On 2026-11-22 pm-git-sync failed every 30 seconds for two hours and the only record was the journal. The notifier reported a failed unit the first time it saw one, which for a unit on a timer is either noise (one bad run the next run mends) or, once it had been seen, silence for as long as it went on being caught between "failed" and "starting". A service with a timer of the same name is now reported once it has failed run after run for five minutes, with what the unit is for, and again when it is seen well. Being caught mid-run is neither. Services that stay up are reported at once, as before. Six planted faults in the new logic are each caught. Refs #12520. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01YLT5K1QVuzZjfWGFhXSSNF --- pm-stack/console/README.md | 8 +++ pm-stack/console/edge/src/edge.ts | 2 +- pm-stack/console/src/notify.ts | 88 +++++++++++++++++++++++++----- pm-stack/console/test/push.test.ts | 84 +++++++++++++++++++++++++++- 4 files changed, 165 insertions(+), 17 deletions(-) diff --git a/pm-stack/console/README.md b/pm-stack/console/README.md index 55b2f3953b..e1542489a2 100644 --- a/pm-stack/console/README.md +++ b/pm-stack/console/README.md @@ -49,6 +49,14 @@ subscribes under More, Notifications, and chooses what reaches it: a reply to something asked, something new that needs the owner, new questions, a failed unit. The Android app shows them as its own. +A unit that runs on a timer (a service with a timer of the same name, +such as `pm-git-sync`) is treated differently from one that stays up. +Between runs it shows as failed whenever its last run failed, so one bad +run that the next run mends says nothing; five minutes of failing run +after run is reported once ("A unit keeps failing", with what the unit +is for), and so is its mending. That is what tells the owner when the +laptop's checkout has stopped following `main`. + ### Phones The Android app's home-screen widget reads a short summary (three counts diff --git a/pm-stack/console/edge/src/edge.ts b/pm-stack/console/edge/src/edge.ts index 77c33a4f19..80d0dc29da 100644 --- a/pm-stack/console/edge/src/edge.ts +++ b/pm-stack/console/edge/src/edge.ts @@ -215,7 +215,7 @@ export class Edge { link: new Limiter(30, 60_000, this.now), refused: new Limiter(60, 60_000, this.now), }; - this.notifier = new Notifier(this.docs, this.push, kvStore(this.kv, "seen")); + this.notifier = new Notifier(this.docs, this.push, kvStore(this.kv, "seen"), this.now); this.table = ownActions(this.push, devices); } diff --git a/pm-stack/console/src/notify.ts b/pm-stack/console/src/notify.ts index 64fd63e4af..d93694022f 100644 --- a/pm-stack/console/src/notify.ts +++ b/pm-stack/console/src/notify.ts @@ -16,9 +16,17 @@ export interface Seen { hands: string[]; questions: number; failed: string[]; + /** Units seen failed and not seen well since: when it was first seen, + * and whether the owner has been told. Absent in a state written + * before units that run on a timer were told apart. */ + failing?: Record; } export const NOTHING_SEEN: Seen = { primed: false, asks: {}, hands: [], questions: 0, failed: [] }; +/** A unit that runs on a timer is only worth a message once it has kept + * failing this long: one bad run that the next run mends is not. */ +export const SUSTAINED_MS = 5 * 60_000; + export interface Message { cls: Class; title: string; @@ -32,20 +40,21 @@ interface State { census?: { hands: { ref: string; title: string; action?: string }[] } | null; sessions?: { waiting: { id: string; title: string }[] } | null; questionCount?: { total: number } | null; - services?: { unit: string; active: string }[] | null; + services?: { unit: string; active: string; description?: string }[] | null; } const clip = (s: string, n: number) => (s.length > n ? `${s.slice(0, n - 1)}…` : s); /** The messages owed for `now`, and what will have been seen once they * are sent. A section that could not be read (null) changes nothing. */ -export function changes(seen: Seen, now: State): { messages: Message[]; seen: Seen } { +export function changes(seen: Seen, now: State, at: number = Date.now()): { messages: Message[]; seen: Seen } { const next: Seen = { primed: true, asks: { ...seen.asks }, hands: seen.hands, questions: seen.questions, failed: seen.failed, + ...(seen.failing ? { failing: seen.failing } : {}), }; const messages: Message[] = []; for (const ask of now.asks ?? []) { @@ -92,18 +101,68 @@ export function changes(seen: Seen, now: State): { messages: Message[]; seen: Se next.questions = total; } if (now.services) { - const failed = now.services.filter((u) => u.active === "failed").map((u) => u.unit); - for (const unit of failed) { - if (seen.failed.includes(unit)) continue; - messages.push({ - cls: "trouble", - title: "A unit failed", - body: unit, - url: "/console/#more/services", - tag: `unit-${unit}`, - }); + // A service with a timer of the same name runs now and then: between + // runs it shows as failed if its last run failed, and for a moment + // during a run it shows as starting. Neither moment alone says much. + const timers = new Set( + now.services.filter((u) => u.unit.endsWith(".timer")).map((u) => u.unit.slice(0, -".timer".length)), + ); + const onTimer = (unit: string) => unit.endsWith(".service") && timers.has(unit.slice(0, -".service".length)); + // Units the owner was told of before this was kept are not told again. + const failing: Record = { + ...(seen.failing ?? Object.fromEntries(seen.failed.map((unit) => [unit, { since: at, told: true }]))), + }; + const listed = new Set(); + for (const u of now.services) { + listed.add(u.unit); + const before = failing[u.unit]; + if (u.active === "failed") { + const entry = { ...(before ?? { since: at, told: false }) }; + const sustained = at - entry.since >= SUSTAINED_MS; + if (!entry.told && (!onTimer(u.unit) || sustained)) { + entry.told = true; + messages.push( + onTimer(u.unit) + ? { + cls: "trouble", + title: "A unit keeps failing", + body: clip( + `${u.unit}, every run for ${Math.round((at - entry.since) / 60_000)} minutes${u.description ? `. ${u.description}` : ""}`, + 180, + ), + url: "/console/#more/services", + tag: `unit-${u.unit}`, + } + : { + cls: "trouble", + title: "A unit failed", + body: u.unit, + url: "/console/#more/services", + tag: `unit-${u.unit}`, + }, + ); + } + failing[u.unit] = entry; + } else if (u.active === "activating" || u.active === "deactivating" || u.active === "reloading") { + // In the middle of a run: not failed, and not yet well either. + } else if (before) { + // Seen well again. One that was reported as failing run after + // run gets its ending said too. + if (before.told && onTimer(u.unit)) { + messages.push({ + cls: "trouble", + title: "A unit is working again", + body: u.unit, + url: "/console/#more/services", + tag: `unit-${u.unit}`, + }); + } + delete failing[u.unit]; + } } - next.failed = failed; + for (const unit of Object.keys(failing)) if (!listed.has(unit)) delete failing[unit]; + next.failing = failing; + next.failed = Object.keys(failing); } return { messages: seen.primed ? messages : [], seen: next }; } @@ -120,6 +179,7 @@ export class Notifier { private docs: Docs, private push: Push, private store: Store, + private now: () => number = Date.now, ) {} private async load(): Promise { @@ -142,7 +202,7 @@ export class Notifier { if ((await this.push.status()).devices === 0) return 0; await this.docs.touch(WATCHED.filter((n) => !skip.includes(n))); const now = Object.fromEntries(WATCHED.map((n) => [n, this.docs.good(n)])) as State; - const { messages, seen } = changes(await this.load(), now); + const { messages, seen } = changes(await this.load(), now, this.now()); for (const m of messages) await this.push.send(m.cls, m); // Written down only when it differs: most passes find nothing new. const before = JSON.stringify(this.seen); diff --git a/pm-stack/console/test/push.test.ts b/pm-stack/console/test/push.test.ts index ef3e80aeb2..a02c8eb5c7 100644 --- a/pm-stack/console/test/push.test.ts +++ b/pm-stack/console/test/push.test.ts @@ -11,7 +11,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { Docs } from "../src/docs.ts"; import { fileStore } from "../src/file-store.ts"; -import { changes, NOTHING_SEEN, Notifier, type Seen } from "../src/notify.ts"; +import { changes, NOTHING_SEEN, Notifier, type Seen, SUSTAINED_MS } from "../src/notify.ts"; import { contact, encrypt, endpointMark, Push, validSubscription } from "../src/push.ts"; import { b64u, browser, raw } from "./support.ts"; @@ -192,7 +192,7 @@ describe("when a notification is owed", () => { }); test("turning notifications on takes stock and says nothing", () => { - const first = changes(NOTHING_SEEN, state()); + const first = changes(NOTHING_SEEN, state(), 1_800_000_000_000); expect(first.messages).toEqual([]); expect(first.seen).toEqual({ primed: true, @@ -200,6 +200,8 @@ describe("when a notification is owed", () => { hands: ["o/r#1"], questions: 360, failed: ["b.service"], + // Already failed when notifications were turned on: counted as told. + failing: { "b.service": { since: 1_800_000_000_000, told: true } }, }); expect(changes(first.seen, state()).messages).toEqual([]); // and nothing is said twice }); @@ -289,3 +291,81 @@ describe("when a notification is owed", () => { } }); }); + +test("a unit on a timer is reported once it keeps failing, not for one bad run, and again when it mends", () => { + const T0 = 1_800_000_000_000; + const primed: Seen = { ...NOTHING_SEEN, primed: true }; + const sync = (active: string) => ({ + services: [ + { unit: "pm-git-sync.service", active, description: "pm-git-sync — keep ~/git/code on clean main" }, + { unit: "pm-git-sync.timer", active: "active" }, + { unit: "pm-console.service", active: "active" }, + ], + }); + // One failed run: nothing is said. + let step = changes(primed, sync("failed"), T0); + expect(step.messages).toEqual([]); + // The next run mends it: forgotten, and still nothing said. + step = changes(step.seen, sync("inactive"), T0 + 60_000); + expect(step.messages).toEqual([]); + expect(step.seen.failing).toEqual({}); + // Now it fails run after run. Being caught in the middle of a run is not a recovery. + step = changes(step.seen, sync("failed"), T0 + 120_000); + step = changes(step.seen, sync("activating"), T0 + 180_000); + expect(step.messages).toEqual([]); + step = changes(step.seen, sync("failed"), T0 + 120_000 + SUSTAINED_MS - 1); + expect(step.messages).toEqual([]); + step = changes(step.seen, sync("failed"), T0 + 120_000 + SUSTAINED_MS); + expect(step.messages.map((m) => [m.cls, m.title, m.body, m.url, m.tag])).toEqual([ + [ + "trouble", + "A unit keeps failing", + "pm-git-sync.service, every run for 5 minutes. pm-git-sync — keep ~/git/code on clean main", + "/console/#more/services", + "unit-pm-git-sync.service", + ], + ]); + // Said once, however long it goes on. + for (const active of ["failed", "activating", "failed"]) { + step = changes(step.seen, sync(active), T0 + 20 * 60_000); + expect(step.messages).toEqual([]); + } + // Mended: that is said once too. + step = changes(step.seen, sync("inactive"), T0 + 21 * 60_000); + expect(step.messages.map((m) => `${m.title}: ${m.body}`)).toEqual(["A unit is working again: pm-git-sync.service"]); + expect(changes(step.seen, sync("inactive"), T0 + 22 * 60_000).messages).toEqual([]); + // And if it starts failing again later, the count starts again. + step = changes(step.seen, sync("failed"), T0 + 30 * 60_000); + expect(step.messages).toEqual([]); + expect(changes(step.seen, sync("failed"), T0 + 30 * 60_000 + SUSTAINED_MS).messages).toHaveLength(1); + + // A service with no timer is still reported the moment it is seen failed, and its recovery is not news. + const console_ = (active: string) => ({ services: [{ unit: "pm-console.service", active }] }); + step = changes(primed, console_("failed"), T0); + expect(step.messages.map((m) => [m.title, m.body])).toEqual([["A unit failed", "pm-console.service"]]); + expect(changes(step.seen, console_("failed"), T0 + SUSTAINED_MS * 3).messages).toEqual([]); + expect(changes(step.seen, console_("active"), T0 + 1_000).messages).toEqual([]); + // A timer by another name does not make a service periodic. + const other = { + services: [ + { unit: "pm-console.service", active: "failed" }, + { unit: "pm-console-expose.timer", active: "active" }, + ], + }; + expect(changes(primed, other, T0).messages.map((m) => m.title)).toEqual(["A unit failed"]); + + // A state kept from before units on timers were told apart: what was reported is not reported again. + const older: Seen = { ...NOTHING_SEEN, primed: true, failed: ["pm-git-sync.service"] }; + step = changes(older, sync("failed"), T0); + expect(step.messages).toEqual([]); + expect(changes(step.seen, sync("failed"), T0 + SUSTAINED_MS * 2).messages).toEqual([]); + // A unit that is no longer listed is forgotten, not remembered as failing. + step = changes(primed, sync("failed"), T0); + step = changes(step.seen, { services: [{ unit: "pm-console.service", active: "active" }] }, T0 + 1_000); + expect(step.seen.failing).toEqual({}); + // The services could not be read: nothing changes and nothing is said. + step = changes(primed, sync("failed"), T0); + const blind = changes(step.seen, { services: null }, T0 + SUSTAINED_MS * 2); + expect(blind.messages).toEqual([]); + expect(blind.seen.failing).toEqual(step.seen.failing); +}); From e56b4029dccbe5baa02892277858ec5cc19276b6 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 22 Nov 2026 18:03:51 -0600 Subject: [PATCH 2/2] fix(pm-console): count a unit's failing between readings, and keep counting at the edge Two review findings on the sustained-failure report: - At the edge the notifier only ran when a watched section changed, and a unit that stays failed reads the same in every report, so the five minutes never elapsed. While a unit's failing is being timed, the edge now looks again on every report. - On the laptop, a list of units that could no longer be read kept its last copy, and the old failure in it was timed against the clock. How long a unit has been failing is now counted between readings, from the time the list was last actually read; and after a gap longer than the wait itself the count starts again, since nothing is known of what happened in between. Four planted faults in these fixes are each caught. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01YLT5K1QVuzZjfWGFhXSSNF --- pm-stack/console/edge/src/edge.ts | 6 +- pm-stack/console/src/docs.ts | 7 +++ pm-stack/console/src/notify.ts | 31 ++++++++-- pm-stack/console/test/edge.test.ts | 38 +++++++++++++ pm-stack/console/test/push.test.ts | 91 +++++++++++++++++++++++++++++- 5 files changed, 165 insertions(+), 8 deletions(-) diff --git a/pm-stack/console/edge/src/edge.ts b/pm-stack/console/edge/src/edge.ts index 80d0dc29da..f73f325ada 100644 --- a/pm-stack/console/edge/src/edge.ts +++ b/pm-stack/console/edge/src/edge.ts @@ -662,7 +662,11 @@ export class Edge { } } await this.setAlarm(now + SILENT_MS + 1000); - if (changed.some((n) => WATCHED.includes(n))) { + // Something the owner is told about changed, or a unit's failing is + // being timed: that clock only moves when the laptop's fresh word on + // its units is looked at, and a unit that stays failed reads the + // same in every report. + if (changed.some((n) => WATCHED.includes(n)) || (await this.notifier.pending())) { await this.notifier.tick().catch((error) => console.error(`pm-edge: notify: ${(error as Error).message}`)); } diff --git a/pm-stack/console/src/docs.ts b/pm-stack/console/src/docs.ts index 45feaaa82d..4c8f810a8e 100644 --- a/pm-stack/console/src/docs.ts +++ b/pm-stack/console/src/docs.ts @@ -164,6 +164,13 @@ export class Docs { return s && s.goodAt > 0 ? s.data : null; } + /** When that data was read from its source, or 0 if it never was. A + * read that failed since does not move it: the data is still the last + * good copy, and this is how old that copy is. */ + goodAt(name: string): number { + return this.sections.get(name)?.goodAt ?? 0; + } + /** Sections brought up to date for a caller that asks rarely (a * phone's widget): stale ones are read again and waited for, up to * `waitMs`. It does not count as someone looking, so it does not keep diff --git a/pm-stack/console/src/notify.ts b/pm-stack/console/src/notify.ts index d93694022f..b0b5cc31dd 100644 --- a/pm-stack/console/src/notify.ts +++ b/pm-stack/console/src/notify.ts @@ -19,12 +19,15 @@ export interface Seen { /** Units seen failed and not seen well since: when it was first seen, * and whether the owner has been told. Absent in a state written * before units that run on a timer were told apart. */ - failing?: Record; + failing?: Record; } export const NOTHING_SEEN: Seen = { primed: false, asks: {}, hands: [], questions: 0, failed: [] }; /** A unit that runs on a timer is only worth a message once it has kept - * failing this long: one bad run that the next run mends is not. */ + * failing this long: one bad run that the next run mends is not. It is + * also how long a failing may go unobserved and still count as the same + * failing: after a longer gap nothing is known of what happened in + * between, and the count starts again. */ export const SUSTAINED_MS = 5 * 60_000; export interface Message { @@ -46,7 +49,10 @@ interface State { const clip = (s: string, n: number) => (s.length > n ? `${s.slice(0, n - 1)}…` : s); /** The messages owed for `now`, and what will have been seen once they - * are sent. A section that could not be read (null) changes nothing. */ + * are sent. A section that could not be read (null) changes nothing. + * `at` is when the services were last actually read: how long a unit + * has been failing is counted between readings, so a list that has + * stopped being read cannot make one old failure look sustained. */ export function changes(seen: Seen, now: State, at: number = Date.now()): { messages: Message[]; seen: Seen } { const next: Seen = { primed: true, @@ -109,7 +115,7 @@ export function changes(seen: Seen, now: State, at: number = Date.now()): { mess ); const onTimer = (unit: string) => unit.endsWith(".service") && timers.has(unit.slice(0, -".service".length)); // Units the owner was told of before this was kept are not told again. - const failing: Record = { + const failing: Record = { ...(seen.failing ?? Object.fromEntries(seen.failed.map((unit) => [unit, { since: at, told: true }]))), }; const listed = new Set(); @@ -117,7 +123,10 @@ export function changes(seen: Seen, now: State, at: number = Date.now()): { mess listed.add(u.unit); const before = failing[u.unit]; if (u.active === "failed") { - const entry = { ...(before ?? { since: at, told: false }) }; + // The same failing only if it was seen recently enough; otherwise + // this is where the count starts (again). + const unbroken = before !== undefined && at - (before.last ?? before.since) <= SUSTAINED_MS; + const entry = { since: unbroken ? before.since : at, told: before?.told ?? false, last: at }; const sustained = at - entry.since >= SUSTAINED_MS; if (!entry.told && (!onTimer(u.unit) || sustained)) { entry.told = true; @@ -145,6 +154,8 @@ export function changes(seen: Seen, now: State, at: number = Date.now()): { mess failing[u.unit] = entry; } else if (u.active === "activating" || u.active === "deactivating" || u.active === "reloading") { // In the middle of a run: not failed, and not yet well either. + // It is still a sighting of the unit not being well. + if (before) failing[u.unit] = { ...before, last: at }; } else if (before) { // Seen well again. One that was reported as failing run after // run gets its ending said too. @@ -202,7 +213,8 @@ export class Notifier { if ((await this.push.status()).devices === 0) return 0; await this.docs.touch(WATCHED.filter((n) => !skip.includes(n))); const now = Object.fromEntries(WATCHED.map((n) => [n, this.docs.good(n)])) as State; - const { messages, seen } = changes(await this.load(), now, this.now()); + // The services as of when they were last read, not as of now. + const { messages, seen } = changes(await this.load(), now, this.docs.goodAt("services") || this.now()); for (const m of messages) await this.push.send(m.cls, m); // Written down only when it differs: most passes find nothing new. const before = JSON.stringify(this.seen); @@ -214,6 +226,13 @@ export class Notifier { } } + /** True while a unit has been seen failing and the owner not yet told: + * the time it has gone on is still being counted, so the next reading + * matters even if it reads the same. */ + async pending(): Promise { + return Object.values((await this.load()).failing ?? {}).some((entry) => !entry.told); + } + start(everyMs = 60_000): void { this.timer ??= setInterval( () => void this.tick().catch((e) => console.error(`pm-console: notify: ${e.message}`)), diff --git a/pm-stack/console/test/edge.test.ts b/pm-stack/console/test/edge.test.ts index 089e0c34dc..e7cddfbfff 100644 --- a/pm-stack/console/test/edge.test.ts +++ b/pm-stack/console/test/edge.test.ts @@ -1555,6 +1555,44 @@ describe("the host's report is the host's", () => { }); describe("notifications and phones, from the edge", () => { + test("a unit that stays failed is reported from the edge, though every report reads the same", async () => { + const w = await setup(); + const phone = await browser(); + const units = [ + { unit: "pm-git-sync.service", active: "inactive", description: "keep the checkout on main" }, + { unit: "pm-git-sync.timer", active: "active" }, + ]; + w.data.services = units; + const round = async (ms: number) => { + w.now.t += ms; + await w.hostReads(); + return w.link.now.once(); + }; + await round(0); + await w.act("push.on", { subscription: phone.subscription, label: "Pocket" }); + await round(11_000); + const before = w.pushes.length; + // It starts failing, and from then on the host's reports all say the same thing. + units[0].active = "failed"; + await round(11_000); + for (let i = 0; i < 9; i++) await round(30_000); + expect(w.pushes.length).toBe(before); // four and a half minutes: not yet + await round(30_000); + await round(30_000); + expect(w.pushes.length).toBe(before + 1); + expect(JSON.parse(await phone.open(w.pushes[before].body))).toMatchObject({ + title: "A unit keeps failing", + url: "/console/#more/services", + }); + // Said once, and its mending is said once. + for (let i = 0; i < 4; i++) await round(30_000); + expect(w.pushes.length).toBe(before + 1); + units[0].active = "inactive"; + await round(30_000); + expect(w.pushes.length).toBe(before + 2); + expect(JSON.parse(await phone.open(w.pushes[before + 1].body)).title).toBe("A unit is working again"); + }); + test("a subscribed device is told of a reply, and once that the host has gone quiet and that it is back", async () => { const w = await setup(); const phone = await browser(); diff --git a/pm-stack/console/test/push.test.ts b/pm-stack/console/test/push.test.ts index a02c8eb5c7..b0c949b614 100644 --- a/pm-stack/console/test/push.test.ts +++ b/pm-stack/console/test/push.test.ts @@ -201,7 +201,7 @@ describe("when a notification is owed", () => { questions: 360, failed: ["b.service"], // Already failed when notifications were turned on: counted as told. - failing: { "b.service": { since: 1_800_000_000_000, told: true } }, + failing: { "b.service": { since: 1_800_000_000_000, told: true, last: 1_800_000_000_000 } }, }); expect(changes(first.seen, state()).messages).toEqual([]); // and nothing is said twice }); @@ -359,6 +359,23 @@ test("a unit on a timer is reported once it keeps failing, not for one bad run, step = changes(older, sync("failed"), T0); expect(step.messages).toEqual([]); expect(changes(step.seen, sync("failed"), T0 + SUSTAINED_MS * 2).messages).toEqual([]); + // Seen failed, then not looked at for longer than the wait: what happened in between is + // not known, so the count starts again from the next sighting. + step = changes(primed, sync("failed"), T0); + step = changes(step.seen, sync("failed"), T0 + SUSTAINED_MS + 1); + expect(step.messages).toEqual([]); + step = changes(step.seen, sync("failed"), T0 + SUSTAINED_MS * 2); + expect(step.messages).toEqual([]); + step = changes(step.seen, sync("failed"), T0 + SUSTAINED_MS * 2 + 1); + expect(step.messages.map((m) => m.body)).toEqual([ + "pm-git-sync.service, every run for 5 minutes. pm-git-sync — keep ~/git/code on clean main", + ]); + // Caught in the middle of a run is still a sighting of the unit not being well: it joins + // two failed sightings that would otherwise be too far apart to count as one failing. + step = changes(primed, sync("failed"), T0); + step = changes(step.seen, sync("activating"), T0 + 4 * 60_000); + step = changes(step.seen, sync("failed"), T0 + 8 * 60_000); + expect(step.messages.map((m) => m.title)).toEqual(["A unit keeps failing"]); // A unit that is no longer listed is forgotten, not remembered as failing. step = changes(primed, sync("failed"), T0); step = changes(step.seen, { services: [{ unit: "pm-console.service", active: "active" }] }, T0 + 1_000); @@ -369,3 +386,75 @@ test("a unit on a timer is reported once it keeps failing, not for one bad run, expect(blind.messages).toEqual([]); expect(blind.seen.failing).toEqual(step.seen.failing); }); + +test("a list of units that has stopped being read cannot make one failed run look sustained", async () => { + const now = { t: 1_800_000_000_000 }; + const units = { + readable: true, + list: [ + { unit: "pm-git-sync.service", active: "failed", description: "keep the checkout on main" }, + { unit: "pm-git-sync.timer", active: "active" }, + ], + }; + const empty = { ttlMs: 1_000, load: async () => null }; + const docs = new Docs( + { + ...Object.fromEntries( + [ + "board", + "sessions", + "census", + "asks", + "questionCount", + "questions", + "prs", + "priorities", + "agents", + "more", + ].map((name) => [name, empty]), + ), + services: { + ttlMs: 1_000, + load: async () => { + if (!units.readable) throw new Error("systemctl did not answer"); + return structuredClone(units.list); + }, + }, + }, + () => now.t, + ); + const sent: string[] = []; + const push = { + status: async () => ({ devices: 1 }), + send: async (_cls: string, m: { title: string }) => void sent.push(m.title), + }; + const kept = { text: null as string | null }; + const notifier = new Notifier( + docs, + push as never, + { read: async () => kept.text, write: async (text) => void (kept.text = text) }, + () => now.t, + ); + const pass = async (ms: number) => { + now.t += ms; + await notifier.tick(); + }; + await pass(0); // takes stock: the unit is already failed, and that is not news + units.list[0].active = "inactive"; + await pass(30_000); + // One failed run is seen, and then the units cannot be read for half an hour. + units.list[0].active = "failed"; + await pass(30_000); + expect(await notifier.pending()).toBe(true); + units.readable = false; + for (let i = 0; i < 60; i++) await pass(30_000); + // Nothing was observed in that time, so nothing is claimed about it. + expect(sent).toEqual([]); + // Read again and still failing: the count goes on from what was seen, and is reported when it has been seen for five minutes. + units.readable = true; + await pass(30_000); + expect(sent).toEqual([]); + for (let i = 0; i < 10; i++) await pass(30_000); + expect(sent).toEqual(["A unit keeps failing"]); + expect(await notifier.pending()).toBe(false); +});