Public: anyone can read this; the crew can change it.
A maintenance finding with no action for 14 days closes itself with a note #16007
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15982-a-maintenance-finding-with-no-action-for → main 0 commits fork 4 threads · 4 unresolved
docs/features/maintenance-autoclose.md added +17
| @@ -0,0 +1,17 @@ | ||
| 1 | --- | |
| 2 | topic: When something is wrong | |
| 3 | row: Findings that close themselves | |
| 4 | --- | |
| 5 | # Findings that close themselves (#15982) | |
| 6 | ||
| 7 | A finding on the maintenance board that stands 14 days with no action closes itself. The row then | |
| 8 | reads "closed 2026-11-15: no action in 14 days", and the Findings fold counts "N auto-closed". | |
| 9 | Reopening it starts its 14 days again. | |
| 10 | ||
| 11 | - **Page**: `/maintenance`, Findings. An auto-closed row has a **Reopen** button (admins). | |
| 12 | - **Terminal**: the Maintenance screen (`src/tui/screens/maintenance.ts`) lists them under "Closed | |
| 13 | for want of action"; `o` asks for a number and reopens that one, through the same | |
| 14 | `reopenFinding` as the page. | |
| 15 | - **How it runs**: the maintenance tick calls `autoClose` (`src/maintenance/findings.ts`) after | |
| 16 | the degraded history is recorded; a failure of its own is logged as "findings: auto-close failed". | |
| 17 | Nothing is deleted: the span is only marked. |
src/core/migration-pins/20261123130000-finding-autoclose.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | f64d7a1bf8bc086349a4fef22c1796311dcdedf70ba55f7c3b23f81780307019 |
src/core/migrations/20261123130000-finding-autoclose.ts added +8
| @@ -0,0 +1,8 @@ | ||
| 1 | // A finding nobody acted on for 14 days is closed with a note (#15982, maintenance/findings.ts). | |
| 2 | // `swept_at` is when the maintenance tick closed it (epoch ms; null while it stands), and | |
| 3 | // `clock_at` is when its 14 days last began again, set when a person reopens it. The span itself | |
| 4 | // keeps following the cause, so nothing the vitals say is hidden or lost. | |
| 5 | export default ` | |
| 6 | ALTER TABLE degraded_span ADD COLUMN swept_at INTEGER; | |
| 7 | ALTER TABLE degraded_span ADD COLUMN clock_at INTEGER; | |
| 8 | `; |
src/http/server.ts +2 −1
| @@ -44,7 +44,7 @@ import { contributePage } from "../web/pages/contribute.ts"; | ||
| 44 | 44 | import { pipelinePage } from "../web/pages/pipeline.ts"; |
| 45 | 45 | import { adminDataPage, appDataCsv, appDataPage, dataPage } from "../web/pages/data.ts"; |
| 46 | 46 | import { fleetAct, fleetReceipt, fleetUndo } from "../web/pages/fleet-ops.ts"; |
| 47 | import { maintenanceDuty, maintenanceDismiss, maintenanceQuiet, maintenanceUnquiet, maintenanceJson, maintenancePage, maintenancePrepare, maintenanceResume, staleAction, mergedAction, previewAction } from "../web/pages/maintenance.ts"; | |
| 47 | import { maintenanceDuty, maintenanceDismiss, maintenanceQuiet, maintenanceUnquiet, maintenanceReopen, maintenanceJson, maintenancePage, maintenancePrepare, maintenanceResume, staleAction, mergedAction, previewAction } from "../web/pages/maintenance.ts"; | |
| 48 | 48 | import { maintenanceBudget } from "../web/pages/maintenance_budgets.ts"; |
| 49 | 49 | import { nightlyOutputPage, resiliencePage } from "../web/pages/resilience.ts"; |
| 50 | 50 | import { boundedSignal } from "../kernel/fetch_timeout.ts"; |
| @@ -1185,6 +1185,7 @@ export function buildRouter(): Router<Handler> { | ||
| 1185 | 1185 | router.add("POST", "/maintenance/dismiss", maintenanceDismiss); |
| 1186 | 1186 | router.add("POST", "/maintenance/quiet", maintenanceQuiet); |
| 1187 | 1187 | router.add("POST", "/maintenance/unquiet", maintenanceUnquiet); |
| 1188 | router.add("POST", "/maintenance/findings/reopen", maintenanceReopen); | |
| 1188 | 1189 | router.add("POST", "/maintenance/stale/:id/:what", staleAction); |
| 1189 | 1190 | router.add("POST", "/maintenance/branches/delete", mergedAction); |
| 1190 | 1191 | router.add("POST", "/maintenance/previews/remove", previewAction); |
src/main.ts +7
| @@ -109,6 +109,7 @@ import { configProblem, dataDirOf, preflight } from "./kernel/preflight.ts"; | ||
| 109 | 109 | import { running } from "./kernel/version.ts"; |
| 110 | 110 | import { record as recordDegraded } from "./kernel/degraded_history.ts"; |
| 111 | 111 | import { vitals } from "./kernel/vitals.ts"; |
| 112 | import { autoClose } from "./maintenance/findings.ts"; | |
| 112 | 113 | import { markReady, readFootprint, keepFootprint } from "./system/footprint.ts"; |
| 113 | 114 | import { walkSmallForge } from "./release/small_forge.ts"; |
| 114 | 115 | import { proveRunning } from "./release/prove.ts"; |
| @@ -895,6 +896,12 @@ const tickAsks = async (): Promise<void> => { | ||
| 895 | 896 | } catch (error) { |
| 896 | 897 | app.log.warn("degraded history: vitals failed", { error: String(error) }); |
| 897 | 898 | } |
| 899 | // Findings with no action for 14 days close themselves; its failure is its own. | |
| 900 | try { | |
| 901 | autoClose(app.db, Date.now()); | |
| 902 | } catch (error) { | |
| 903 | app.log.warn("findings: auto-close failed", { error: String(error) }); | |
| 904 | } | |
| 898 | 905 | }; |
| 899 | 906 | // Followed upstreams are read on a slow tick (`actions/upstream_follow.ts`): |
| 900 | 907 | // the tip and how far behind land on the follow row; nothing is opened. |
src/maintenance/findings.ts +55 −5
| @@ -16,13 +16,22 @@ export interface FindingRow { | ||
| 16 | 16 | readonly days: number; |
| 17 | 17 | /** True while the newest span is still open. */ |
| 18 | 18 | readonly open: boolean; |
| 19 | /** When the tick closed it for want of action (epoch ms), or null while it stands. */ | |
| 20 | readonly autoClosedAt: number | null; | |
| 19 | 21 | } |
| 20 | 22 | |
| 23 | /** Days a finding may stand with no action before the tick closes it. */ | |
| 24 | export const AUTO_CLOSE_DAYS = 14; | |
| 25 | /** The note an auto-closed finding carries. */ | |
| 26 | export const AUTO_CLOSE_NOTE = `no action in ${String(AUTO_CLOSE_DAYS)} days`; | |
| 27 | ||
| 21 | 28 | /** Rows a board shows; the rest are counted, not listed. */ |
| 22 | 29 | export const FINDING_ROWS = 20; |
| 23 | 30 | |
| 24 | 31 | export interface Findings { |
| 25 | 32 | readonly rows: FindingRow[]; |
| 33 | /** Every cause the tick closed for want of action, listed or not. */ | |
| 34 | readonly autoClosed: number; | |
| 26 | 35 | /** Causes past the cap, not listed. */ |
| 27 | 36 | readonly more: number; |
| 28 | 37 | } |
| @@ -33,16 +42,17 @@ export interface Findings { | ||
| 33 | 42 | */ |
| 34 | 43 | export function findingRows(db: Database, cap = FINDING_ROWS): Findings { |
| 35 | 44 | const { got, more } = latestSpans(db, cap); |
| 36 | return { rows: got.map((r) => ({ cause: r.cause, latest: r.sentence, days: r.days, open: r.closed_at === null })), more }; | |
| 45 | const rows = got.map((r) => ({ cause: r.cause, latest: r.sentence, days: r.days, open: r.closed_at === null, autoClosedAt: r.swept_at })); | |
| 46 | return { rows, autoClosed: autoClosedCauses(db).length, more }; | |
| 37 | 47 | } |
| 38 | 48 | |
| 39 | interface SpanRow { cause: string; sentence: string; href: string; closed_at: number | null; days: number; total: number } | |
| 49 | interface SpanRow { cause: string; sentence: string; href: string; closed_at: number | null; swept_at: number | null; days: number; total: number } | |
| 40 | 50 | |
| 41 | 51 | /** The one query behind every reading of the findings: the board, the terminal and the JSON. */ |
| 42 | 52 | function latestSpans(db: Database, cap: number): { got: SpanRow[]; more: number } { |
| 43 | 53 | const got = db |
| 44 | 54 | .query<SpanRow, [number, number]>( |
| 45 | `SELECT s.cause, s.sentence, s.href, s.closed_at, g.days, COUNT(*) OVER () AS total | |
| 55 | `SELECT s.cause, s.sentence, s.href, s.closed_at, s.swept_at, g.days, COUNT(*) OVER () AS total | |
| 46 | 56 | FROM degraded_span s |
| 47 | 57 | JOIN (SELECT cause, MAX(id) AS last, COUNT(DISTINCT opened_at / ?1) AS days FROM degraded_span GROUP BY cause) g ON s.id = g.last |
| 48 | 58 | ORDER BY s.opened_at DESC, s.id DESC LIMIT ?2`, |
| @@ -51,6 +61,46 @@ function latestSpans(db: Database, cap: number): { got: SpanRow[]; more: number | ||
| 51 | 61 | return { got, more: Math.max(0, (got[0]?.total ?? 0) - got.length) }; |
| 52 | 62 | } |
| 53 | 63 | |
| 64 | /** | |
| 65 | * The maintenance tick's sweep: a cause whose newest span is open and has stood `AUTO_CLOSE_DAYS` | |
| 66 | * since it opened or was last reopened is marked closed. Returns the causes it closed. | |
| 67 | */ | |
| 68 | export function autoClose(db: Database, now: number): string[] { | |
| 69 | return db | |
| 70 | .query<{ cause: string }, [number, number]>( | |
| 71 | `UPDATE degraded_span SET swept_at = ?1 | |
| 72 | WHERE closed_at IS NULL AND swept_at IS NULL AND COALESCE(clock_at, opened_at) <= ?2 | |
| 73 | RETURNING cause`, | |
| 74 | ) | |
| 75 | .all(now, now - AUTO_CLOSE_DAYS * DAY_MS) | |
| 76 | .map((r) => r.cause); | |
| 77 | } | |
| 78 | ||
| 79 | /** The causes the tick closed and nobody has reopened, newest first: the terminal numbers them. */ | |
| 80 | export function autoClosedCauses(db: Database): string[] { | |
| 81 | return db | |
| 82 | .query<{ cause: string }, []>( | |
| 83 | `SELECT cause FROM degraded_span WHERE swept_at IS NOT NULL | |
| 84 | GROUP BY cause ORDER BY MAX(opened_at) DESC, MAX(id) DESC`, | |
Non-blocking: this groups all spans of a cause, so an old swept span keeps the cause listed after a newer span opened. Join on the newest span (MAX(id)) as latestSpans does. |
||
| 85 | ) | |
| 86 | .all() | |
| 87 | .map((r) => r.cause); | |
| 88 | } | |
| 89 | ||
| 90 | /** Reopen an auto-closed finding: it stands again and its 14 days begin now. */ | |
| 91 | export function reopenFinding(db: Database, cause: string, now: number): boolean { | |
| 92 | return ( | |
| 93 | db | |
| 94 | .query<{ id: number }, [string, number]>( | |
| 95 | "UPDATE degraded_span SET swept_at = NULL, clock_at = ?2 WHERE cause = ?1 AND swept_at IS NOT NULL RETURNING id", | |
| 96 | ) | |
| 97 | .all(cause, now).length > 0 | |
| 98 | ); | |
| 99 | } | |
| 100 | ||
| 101 | /** The date an auto-closed finding was closed, "2026-11-20". */ | |
| 102 | export const closedDate = (ms: number): string => new Date(ms).toISOString().slice(0, 10); | |
| 103 | ||
| 54 | 104 | /** One finding for an agent: what degraded, how bad, in one line, and the one place to act. */ |
| 55 | 105 | export interface FindingJson { |
| 56 | 106 | /** The watcher that raised it: the degraded cause's name. */ |
| @@ -68,7 +118,7 @@ export function findingsJson(db: Database): { findings: FindingJson[] } { | ||
| 68 | 118 | return { |
| 69 | 119 | findings: latestSpans(db, FINDING_ROWS).got.map((r) => ({ |
| 70 | 120 | duty: r.cause, |
| 71 | severity: r.closed_at === null ? "degraded" : "cleared", | |
| 121 | severity: r.closed_at === null && r.swept_at === null ? "degraded" : "cleared", | |
| 72 | 122 | cause: r.sentence, |
| 73 | 123 | action: r.href, |
| 74 | 124 | })), |
| @@ -85,7 +135,7 @@ export function findingLines(db: Database): string[] { | ||
| 85 | 135 | return [ |
| 86 | 136 | "", |
| 87 | 137 | "Findings (what degraded, latest reading):", |
| 88 | ...rows.map((r) => `${r.cause}: ${r.latest} (${seenWords(r.days)}${r.open ? "; still open" : ""})`), | |
| 138 | ...rows.map((r) => `${r.cause}: ${r.latest} (${seenWords(r.days)}${r.autoClosedAt !== null ? `; closed ${closedDate(r.autoClosedAt)}: ${AUTO_CLOSE_NOTE}` : r.open ? "; still open" : ""})`), | |
| 89 | 139 | ...(more > 0 ? [`And ${String(more)} more, older causes, not listed.`] : []), |
| 90 | 140 | ]; |
| 91 | 141 | } |
src/tui/screens/maintenance.ts +12 −2
| @@ -8,7 +8,7 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | import { filedText } from "../../maintenance/filed.ts"; |
| 11 | import { findingLines } from "../../maintenance/findings.ts"; | |
| 11 | import { autoClosedCauses, findingLines, reopenFinding } from "../../maintenance/findings.ts"; | |
| 12 | 12 | import { LEVEL_WORDS, nextLevel, returnLevel, stateLevel, type Level } from "../../maintenance/level.ts"; |
| 13 | 13 | import { dutyStates } from "../../maintain/pause.ts"; |
| 14 | 14 | import { duties, quietLine, splitDuties } from "../../maintenance/duties.ts"; |
| @@ -180,6 +180,14 @@ export function maintenanceScreen(f: Session, ctx: Ctx): Screen { | ||
| 180 | 180 | learnLive(); |
| 181 | 181 | }; |
| 182 | 182 | |
| 183 | /** | |
| 184 | * Reopen the numbered auto-closed finding: the page's button, through the same `reopenFinding`. | |
| 185 | */ | |
| 186 | const doReopen = (i: number): void => { | |
| 187 | const cause = autoClosedCauses(f.app.db)[i]; | |
| 188 | say(cause !== undefined && reopenFinding(f.app.db, cause, Date.now()) ? `Reopened ${cause}.` : "That finding is not closed."); | |
| 189 | }; | |
| 190 | ||
| 183 | 191 | /** |
| 184 | 192 | * Ask the stalled pull whether it is still wanted: the page's button, through the same `nudge`. |
| 185 | 193 | */ |
| @@ -202,7 +210,7 @@ export function maintenanceScreen(f: Session, ctx: Ctx): Screen { | ||
| 202 | 210 | ? [binding(["p"], "prepare for maintenance")] |
| 203 | 211 | : [binding(["c"], "check again, retake the backup"), binding(["e"], "end maintenance")]), |
| 204 | 212 | ...(!mayAct() || busy || stalledNow(f.db, new Date()) === undefined ? [] : [binding(["a"], "ask the stalled pull: still wanted?")]), |
| 205 | ...(!mayAct() || busy ? [] : [...(live.length > 0 ? [binding(["z"], "quiet a finding for a day")] : []), ...(quieted(f.app.db).length > 0 ? [binding(["b"], "bring a quieted finding back")] : [])]), | |
| 213 | ...(!mayAct() || busy ? [] : [...(live.length > 0 ? [binding(["z"], "quiet a finding for a day")] : []), ...(quieted(f.app.db).length > 0 ? [binding(["b"], "bring a quieted finding back")] : []), ...(autoClosedCauses(f.app.db).length > 0 ? [binding(["o"], "reopen a finding closed for want of action")] : [])]), | |
| 206 | 214 | binding(["enter", "esc", "q"], "close"), |
| 207 | 215 | ]; |
| 208 | 216 | }, |
| @@ -214,6 +222,7 @@ export function maintenanceScreen(f: Session, ctx: Ctx): Screen { | ||
| 214 | 222 | ...(level === undefined ? [] : [`Showing: ${LEVEL_WORDS[level]}`, ""]), |
| 215 | 223 | ...maintenanceLines(f, standby, level), |
| 216 | 224 | // The numbered findings wait on a person, so the calm view leaves them out. |
| 225 | ...(level === "needs-you" ? [] : autoClosedCauses(f.app.db).flatMap((c, i) => [...(i === 0 ? ["", "Closed for want of action (o reopens one):"] : []), ...wrap(`${String(i + 1)}. ${c}`, INNER)])), | |
| 217 | 226 | ...(live.length === 0 || level === "fyi" ? [] : ["", "Findings (z quiets one):", ...live.flatMap((l, i) => wrap(`${String(i + 1)}. ${l.label} (${l.kind})`, INNER))]), |
| 218 | 227 | ], |
| 219 | 228 | actions: [{ label: "Close" }], |
| @@ -235,6 +244,7 @@ export function maintenanceScreen(f: Session, ctx: Ctx): Screen { | ||
| 235 | 244 | else if (e.key === "c" && !idle) doPrepare(""); |
| 236 | 245 | else if (e.key === "e" && !idle) ctx.push(askEnd()); |
| 237 | 246 | else if (e.key === "z" && live.length > 0) ctx.push(askNumber("Quiet a finding", "Number of the finding, quieted for a day", live.length, (i) => live[i] && doQuiet(live[i]!))); |
| 247 | else if (e.key === "o" && autoClosedCauses(f.app.db).length > 0) ctx.push(askNumber("Reopen a finding", "Number under closed for want of action", autoClosedCauses(f.app.db).length, doReopen)); | |
| 238 | 248 | else if (e.key === "b" && quieted(f.app.db).length > 0) ctx.push(askNumber("Bring a finding back", "Number under quieted", quieted(f.app.db).length, doBringBack)); |
| 239 | 249 | return true; |
| 240 | 250 | }, |
src/web/pages/maintenance.ts +18 −4
| @@ -11,7 +11,7 @@ import { dutyStates, switchDuty } from "../../maintain/pause.ts"; | ||
| 11 | 11 | import { DISMISS_DAYS, REASON_MAX, dismiss, dismissals, dismissalWords } from "../../maintain/dismiss.ts"; |
| 12 | 12 | import { duties, quietLine, splitDuties } from "../../maintenance/duties.ts"; |
| 13 | 13 | import { LEVELS, LEVEL_WORDS, dutiesLevel, levelOf, returnLevel, stateLevel, type Level } from "../../maintenance/level.ts"; |
| 14 | import { findingRows, findingsJson, seenWords } from "../../maintenance/findings.ts"; | |
| 14 | import { AUTO_CLOSE_NOTE, closedDate, findingRows, findingsJson, reopenFinding, seenWords } from "../../maintenance/findings.ts"; | |
| 15 | 15 | import { NOTHING_FILED, filedPieces } from "../../maintenance/filed.ts"; |
| 16 | 16 | import { stalledStep } from "../../maintenance/stalled.ts"; |
| 17 | 17 | import { statusWithStandby } from "../../maintenance/standby.ts"; |
| @@ -53,11 +53,12 @@ const plural = (n: number, one: string, many: string): string => `${String(n)} $ | ||
| 53 | 53 | * nothing when none. Open when a cause is still open, which each row says once. |
| 54 | 54 | */ |
| 55 | 55 | function findingsFold(db: Parameters<typeof findingRows>[0]): Html { |
| 56 | const { rows, more } = findingRows(db); | |
| 56 | const { rows, more, autoClosed } = findingRows(db); | |
| 57 | 57 | if (rows.length === 0) return html``; |
| 58 | return fold("maintenance-findings", "Findings", String(rows.length + more), rows.some((r) => r.open), | |
| 58 | const count = `${String(rows.length + more)}${autoClosed > 0 ? `, ${String(autoClosed)} auto-closed` : ""}`; | |
| 59 | return fold("maintenance-findings", "Findings", count, rows.some((r) => r.open && r.autoClosedAt === null), | |
| 59 | 60 | html`<dl id="maintenance-findings-rows" class="status-rows">${rows.map( |
| 60 | (r) => statusRow(r.cause, html`<span data-finding="${r.cause}">${r.latest}</span> <span class="muted">${seenWords(r.days)}${r.open ? "; still open" : ""}</span>`), | |
| 61 | (r) => statusRow(r.cause, html`<span data-finding="${r.cause}">${r.latest}</span> <span class="muted">${seenWords(r.days)}${r.autoClosedAt !== null ? html`; closed ${closedDate(r.autoClosedAt)}: ${AUTO_CLOSE_NOTE}` : r.open ? "; still open" : ""}</span>${r.autoClosedAt !== null ? html` <form method="post" action="/maintenance/findings/reopen" class="inline"><input type="hidden" name="cause" value="${r.cause}">${btn("Reopen", { variant: "ghost", size: "sm" })}</form>` : null}`), | |
outdated
Principles 1 and 8: 'came back N times' was not asked for and crowds the row. Remove it, or justify it. Also the inline form with class |
||
| 61 | 62 | )}${more > 0 ? statusRow(`And ${String(more)} more`, html`<span class="muted">older causes, not listed</span>`) : html``}</dl>`); |
| 62 | 63 | } |
| 63 | 64 | |
| @@ -310,6 +311,19 @@ export async function maintenanceQuiet(ctx: Ctx): Promise<Response> { | ||
| 310 | 311 | return flashTo(ctx, "/maintenance", r.ok ? { message: r.says } : { tone: "danger", message: r.says }); |
| 311 | 312 | } |
| 312 | 313 | |
| 314 | /** | |
| 315 | * POST /maintenance/findings/reopen (field cause): an auto-closed finding stands again, one tap. | |
| 316 | */ | |
| 317 | export async function maintenanceReopen(ctx: Ctx): Promise<Response> { | |
Both doors: Reopen has no terminal equivalent. Add a src/tui/ screen entry plus docs/features/<name>.md, with a test. |
||
| 318 | const denied = mayQuiet(ctx); | |
| 319 | if (denied) return denied; | |
| 320 | const form = await readForm(ctx, "admin"); | |
| 321 | if (form instanceof Response) return form; | |
| 322 | const cause = formField(form, "cause"); | |
| 323 | const ok = cause.length > 0 && cause.length <= 200 && reopenFinding(ctx.app.db, cause, Date.now()); | |
| 324 | return flashTo(ctx, "/maintenance", ok ? { message: `Reopened ${cause}.` } : { tone: "danger", message: "That finding is not closed." }); | |
| 325 | } | |
| 326 | ||
| 313 | 327 | /** POST /maintenance/unquiet (fields kind, key): undo a quiet now, receipted. */ |
| 314 | 328 | export async function maintenanceUnquiet(ctx: Ctx): Promise<Response> { |
| 315 | 329 | const denied = mayQuiet(ctx); |
test/maintenance-autoclose.test.ts added +86
| @@ -0,0 +1,86 @@ | ||
| 1 | /** | |
| 2 | * A finding with no action for 14 days closes itself with a note, and reopens in one tap (#15982). | |
| 3 | */ | |
| 4 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 5 | import { Database } from "bun:sqlite"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | import { AUTO_CLOSE_NOTE, autoClose, closedDate, findingLines, findingRows, reopenFinding } from "../src/maintenance/findings.ts"; | |
| 12 | import { adminCookie } from "./helpers/admin.ts"; | |
| 13 | import { quietLogs } from "./helpers/globals.ts"; | |
| 14 | ||
| 15 | quietLogs(); | |
| 16 | const DAY = 86_400_000; | |
| 17 | const T0 = Date.parse("2026-11-01T12:00:00.000Z"); | |
| 18 | ||
| 19 | function seed(): Database { | |
| 20 | const db = new Database(":memory:"); | |
| 21 | db.run("CREATE TABLE degraded_span (id INTEGER PRIMARY KEY AUTOINCREMENT, cause TEXT NOT NULL, sentence TEXT NOT NULL, href TEXT NOT NULL, opened_at INTEGER NOT NULL, closed_at INTEGER, swept_at INTEGER, clock_at INTEGER)"); | |
| 22 | return db; | |
| 23 | } | |
| 24 | const add = (db: Database, cause: string, at: number, closedAt: number | null = null): void => { | |
| 25 | db.query("INSERT INTO degraded_span (cause, sentence, href, opened_at, closed_at) VALUES (?1, 'x', '/system', ?2, ?3)").run(cause, at, closedAt); | |
| 26 | }; | |
| 27 | ||
| 28 | test("day 13 stands, day 14 closes with the note, and the sweep says it once", () => { | |
| 29 | const db = seed(); | |
| 30 | add(db, "disk-free", T0); | |
| 31 | expect(autoClose(db, T0 + 13 * DAY)).toEqual([]); | |
| 32 | expect(findingRows(db).rows[0]!.autoClosedAt).toBeNull(); | |
| 33 | expect(autoClose(db, T0 + 14 * DAY)).toEqual(["disk-free"]); | |
| 34 | const f = findingRows(db); | |
| 35 | expect(f.autoClosed).toBe(1); | |
| 36 | expect(f.rows[0]!.autoClosedAt).toBe(T0 + 14 * DAY); | |
| 37 | expect(findingLines(db).join("\n")).toContain(`closed 2026-11-15: ${AUTO_CLOSE_NOTE}`); | |
| 38 | expect(autoClose(db, T0 + 15 * DAY)).toEqual([]); | |
| 39 | }); | |
| 40 | ||
| 41 | test("a reopened finding stands again and its 14 days begin at the reopening", () => { | |
| 42 | const db = seed(); | |
| 43 | add(db, "disk-free", T0); | |
| 44 | autoClose(db, T0 + 14 * DAY); | |
| 45 | expect(reopenFinding(db, "disk-free", T0 + 16 * DAY)).toBe(true); | |
| 46 | expect(reopenFinding(db, "disk-free", T0 + 16 * DAY)).toBe(false); | |
| 47 | expect(findingRows(db).autoClosed).toBe(0); | |
| 48 | expect(autoClose(db, T0 + 29 * DAY)).toEqual([]); // day 13 after reopening | |
| 49 | expect(autoClose(db, T0 + 30 * DAY)).toEqual(["disk-free"]); | |
| 50 | }); | |
| 51 | ||
| 52 | test("the summary counts every auto-closed cause, past the listed rows", () => { | |
| 53 | const db = seed(); | |
| 54 | for (let i = 0; i < 22; i++) add(db, `cause-${String(i)}`, T0 + i); | |
| 55 | autoClose(db, T0 + 14 * DAY + 100); | |
| 56 | const f = findingRows(db); | |
| 57 | expect(f.rows.length).toBe(20); | |
| 58 | expect(f.autoClosed).toBe(22); | |
| 59 | }); | |
| 60 | ||
| 61 | let dir = "", app: App, servers: Servers, cookie = ""; | |
| 62 | beforeAll(async () => { | |
| 63 | dir = await mkdtemp(join(tmpdir(), "forge-autoclose-")); | |
| 64 | app = createApp({ dataDir: dir, port: 0 }); | |
| 65 | servers = await serve(app); | |
| 66 | cookie = await adminCookie(app, { username: "boss", email: "[email protected]", password: "a-good-passphrase" }); | |
| 67 | }); | |
| 68 | afterAll(async () => { await servers.stop(); await rm(dir, { recursive: true, force: true }); }); | |
| 69 | ||
| 70 | test("the page shows the closing with its date and a Reopen button; the summary counts it; one tap reopens", async () => { | |
| 71 | const t = Date.now() - 20 * DAY; | |
| 72 | add(app.db, "disk-free", t); | |
| 73 | autoClose(app.db, Date.now()); | |
| 74 | const page = await (await fetch(`${servers.url}/maintenance`, { headers: { cookie } })).text(); | |
| 75 | expect(page).toContain(AUTO_CLOSE_NOTE); | |
| 76 | expect(page).toContain(closedDate(findingRows(app.db).rows[0]!.autoClosedAt!)); | |
| 77 | expect(page).toContain("1 auto-closed"); | |
| 78 | expect(page).toContain('action="/maintenance/findings/reopen"'); | |
| 79 | const res = await fetch(`${servers.url}/maintenance/findings/reopen`, { | |
| 80 | method: "POST", redirect: "manual", | |
| 81 | headers: { cookie, origin: servers.url, "content-type": "application/x-www-form-urlencoded" }, | |
| 82 | body: new URLSearchParams({ cause: "disk-free" }), | |
| 83 | }); | |
| 84 | expect(res.status).toBe(303); | |
| 85 | expect(findingRows(app.db).autoClosed).toBe(0); | |
| 86 | }); |
test/maintenance-findings.test.ts +3 −3
| @@ -8,7 +8,7 @@ const T0 = Date.parse("2026-11-01T12:00:00.000Z"); | ||
| 8 | 8 | |
| 9 | 9 | function seed(): Database { |
| 10 | 10 | const db = new Database(":memory:"); |
| 11 | db.run("CREATE TABLE degraded_span (id INTEGER PRIMARY KEY AUTOINCREMENT, cause TEXT NOT NULL, sentence TEXT NOT NULL, href TEXT NOT NULL, opened_at INTEGER NOT NULL, closed_at INTEGER)"); | |
| 11 | db.run("CREATE TABLE degraded_span (id INTEGER PRIMARY KEY AUTOINCREMENT, cause TEXT NOT NULL, sentence TEXT NOT NULL, href TEXT NOT NULL, opened_at INTEGER NOT NULL, closed_at INTEGER, swept_at INTEGER, clock_at INTEGER)"); | |
| 12 | 12 | return db; |
| 13 | 13 | } |
| 14 | 14 | const add = (db: Database, cause: string, sentence: string, at: number, closed: boolean): void => { |
| @@ -25,8 +25,8 @@ test("three days of one finding are one row with the latest reading; another fin | ||
| 25 | 25 | const { rows } = findingRows(db); |
| 26 | 26 | expect(rows.length).toBe(2); |
| 27 | 27 | const mem = rows.find((r) => r.cause === "memory-growth")!; |
| 28 | expect(mem).toEqual({ cause: "memory-growth", latest: "50 MiB", days: 3, open: true }); | |
| 29 | expect(rows.find((r) => r.cause === "disk-free")).toEqual({ cause: "disk-free", latest: "5 GiB", days: 1, open: false }); | |
| 28 | expect(mem).toEqual({ cause: "memory-growth", latest: "50 MiB", days: 3, open: true, autoClosedAt: null }); | |
| 29 | expect(rows.find((r) => r.cause === "disk-free")).toEqual({ cause: "disk-free", latest: "5 GiB", days: 1, open: false, autoClosedAt: null }); | |
| 30 | 30 | expect(seenWords(3)).toBe("seen 3 days"); |
| 31 | 31 | expect(seenWords(1)).toBe("seen 1 day"); |
| 32 | 32 | }); |
test/tui-maintenance-reopen.test.ts added +43
| @@ -0,0 +1,43 @@ | ||
| 1 | /** | |
| 2 | * The terminal's maintenance screen reopens an auto-closed finding with o, as the page's button | |
| 3 | * does (#15982). | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { createUser, type User } from "../src/core/users.ts"; | |
| 11 | import { autoClose, findingRows } from "../src/maintenance/findings.ts"; | |
| 12 | import { openMaintenance } from "./helpers/tui-maintenance.ts"; | |
| 13 | import { quietLogs } from "./helpers/globals.ts"; | |
| 14 | import { settleUntil } from "./helpers/tui.ts"; | |
| 15 | ||
| 16 | quietLogs(); | |
| 17 | let dir = "", app: App, admin: User; | |
| 18 | ||
| 19 | beforeAll(async () => { | |
| 20 | dir = await mkdtemp(join(tmpdir(), "forge-tui-reopen-")); | |
| 21 | app = createApp({ dataDir: dir, port: 0 }); | |
| 22 | const r = await createUser(app.db, { username: "boss", email: "[email protected]", password: "a-good-passphrase", origin: "seed", trusted: true, isAdmin: true }); | |
| 23 | if (!r.ok) throw new Error("user"); | |
| 24 | admin = r.value; | |
| 25 | }); | |
| 26 | afterAll(async () => { | |
| 27 | app.close(); | |
| 28 | await rm(dir, { recursive: true, force: true }); | |
| 29 | }); | |
| 30 | ||
| 31 | test("o lists the auto-closed findings and reopens the chosen one", async () => { | |
| 32 | const opened = Date.now() - 20 * 86_400_000; | |
| 33 | app.db.query("INSERT INTO degraded_span (cause, sentence, href, opened_at) VALUES ('disk-free', 'x', '/system', ?1)").run(opened); | |
| 34 | autoClose(app.db, Date.now()); | |
| 35 | expect(findingRows(app.db).autoClosed).toBe(1); | |
| 36 | const s = await openMaintenance(app, admin); | |
| 37 | expect(s.text()).toContain("o reopens one"); | |
| 38 | await s.press("o"); | |
| 39 | await s.press("1"); | |
| 40 | await s.press("\r"); | |
| 41 | await settleUntil(s.tui, "Reopened disk-free."); | |
| 42 | expect(findingRows(app.db).autoClosed).toBe(0); | |
| 43 | }); |
The sweep shares the try with the vitals record: a vitals failure skips it, and its own failure logs as 'vitals failed'. Give it its own try/catch.