Public: anyone can read this; the crew can change it.
Each maintenance finding on /maintenance links to the issue or pull it opened #14472
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14459-each-maintenance-finding-on-maintenance → main 4 commits fork 2 threads · 2 unresolved
5 files
changed +152
−3
docs/features/maintenance.md +1 −1
| @@ -31,7 +31,7 @@ on, "End maintenance now" sits under the status line. The forge's looks picture | ||
| 31 | 31 | |
| 32 | 32 | **Duties.** `/maintenance`, the terminal's Maintenance screen and the `duties` of MCP `maintenance {action: "status"}` |
| 33 | 33 | list each nightly job and the radar in one line (`src/maintenance/duties.ts`, one function for all three doors): |
| 34 | when it last ran (UTC), what it found ("nothing found" when it filed nothing), and "overdue". A duty is | |
| 34 | when it last ran (UTC), what it found: each issue or pull it filed is a link on the page (plain `owner/name#N` in the terminal), a finding that names none ends "nothing filed", and a reference to a repository you cannot read reads "something you cannot read", never its name, and "overdue". A duty is | |
| 35 | 35 | overdue when now is past its last slot plus one day plus 6 hours of grace (a night may run long); one |
| 36 | 36 | that never ran says so and gives its slot, and is not overdue. |
| 37 | 37 |
src/maintenance/filed.ts added +55
| @@ -0,0 +1,55 @@ | ||
| 1 | /** | |
| 2 | * What a duty filed, read back from its one-line finding: `owner/name#12` is an issue or pull the | |
| 3 | * duty opened. Findings are stored as sentences, not columns, so the reference is all there is to | |
| 4 | * link; a bare `#12` names no repository and is left as text. A reference to a repository the | |
| 5 | * viewer cannot read is replaced by words, never linked or named. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { Database } from "bun:sqlite"; | |
| 9 | import { getIssue } from "../core/issues.ts"; | |
| 10 | import { canRead, getRepo } from "../core/repos.ts"; | |
| 11 | import type { User } from "../core/users.ts"; | |
| 12 | ||
| 13 | export type Piece = | |
| 14 | | { readonly text: string } | |
| 15 | | { readonly href: string; readonly label: string }; | |
| 16 | ||
| 17 | export const NOTHING_FILED = "nothing filed"; | |
| 18 | export const HIDDEN = "something you cannot read"; | |
| 19 | ||
| 20 | const REF = /([A-Za-z0-9][A-Za-z0-9._-]{0,99})\/([A-Za-z0-9][A-Za-z0-9._-]{0,99})#(\d{1,9})/g; | |
| 21 | ||
| 22 | /** The finding as text and links; `filed` says whether it named any issue or pull at all. */ | |
| 23 | export function filedPieces(db: Database, found: string, viewer: User | undefined): { pieces: Piece[]; filed: boolean } { | |
| 24 | const pieces: Piece[] = []; | |
| 25 | let at = 0; | |
| 26 | let filed = false; | |
| 27 | for (const m of found.matchAll(REF)) { | |
| 28 | filed = true; | |
| 29 | if (m.index > at) pieces.push({ text: found.slice(at, m.index) }); | |
| 30 | at = m.index + m[0].length; | |
| 31 | const [, owner, name, n] = m as unknown as [string, string, string, string]; | |
| 32 | const repo = getRepo(db, owner, name); | |
| 33 | const readable = repo !== undefined && canRead(db, repo, viewer); | |
| 34 | const issue = readable ? getIssue(db, repo.id, Number(n)) : undefined; | |
| 35 | if (!readable || issue === undefined) pieces.push({ text: HIDDEN }); | |
| 36 | else pieces.push({ href: `/${owner}/${name}/${issue.kind === "pull" ? "pulls" : "issues"}/${n}`, label: m[0] }); | |
| 37 | } | |
| 38 | if (at < found.length) pieces.push({ text: found.slice(at) }); | |
| 39 | return { pieces, filed }; | |
| 40 | } | |
| 41 | ||
| 42 | /** | |
| 43 | * A duty's row as plain text for the terminal: references stay as `owner/name#N` (no links there). | |
| 44 | */ | |
| 45 | export function filedText( | |
| 46 | db: Database, | |
| 47 | d: { name: string; detail: string; found: string; lastRun: string; overdue: boolean }, | |
| 48 | viewer: User | undefined, | |
| 49 | ): string { | |
| 50 | const tail = d.overdue ? "; overdue" : ""; | |
| 51 | if (d.lastRun === "never" || d.found === "not finished") return `${d.name}: ${d.detail}${tail}.`; | |
| 52 | const { pieces, filed } = filedPieces(db, d.detail, viewer); | |
| 53 | const body = pieces.map((p) => ("href" in p ? p.label : p.text)).join(""); | |
| 54 | return `${d.name}: ${body}${filed ? "" : `; ${NOTHING_FILED}`}${tail}.`; | |
| 55 | } |
src/tui/screens/maintenance.ts +2 −1
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | * refused by the service. Enter, Esc or q closes it. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { filedText } from "../../maintenance/filed.ts"; | |
| 10 | 11 | import { duties, quietLine, splitDuties } from "../../maintenance/duties.ts"; |
| 11 | 12 | import { ABOUT_TO_STOP_ROWS, prepare, resumeNow, status } from "../../maintenance/service.ts"; |
| 12 | 13 | import { probeStandby, withStandby, type Standby } from "../../maintenance/standby.ts"; |
| @@ -46,7 +47,7 @@ export function maintenanceLines(f: Session, standby?: Standby): string[] { | ||
| 46 | 47 | // Quiet duties fold into one line, as on the page; the rest keep their lines. |
| 47 | 48 | const { quiet, loud } = splitDuties(duties(f.app.config.dataDir)); |
| 48 | 49 | const dutyLines = [ |
| 49 | ...loud.flatMap((d) => wrap(d.says, INNER)), | |
| 50 | ...loud.flatMap((d) => wrap(filedText(f.app.db, d, f.user), INNER)), | |
| 50 | 51 | ...(quiet.length === 0 ? [] : wrap(`${quietLine(quiet.length, loud.length === 0)}: ${quiet.map((d) => d.name).join(", ")}`, INNER)), |
| 51 | 52 | ]; |
| 52 | 53 | return [...wrap(s.says, INNER), "", ...facts, ...checks, "", ...dutyLines]; |
src/web/pages/maintenance.ts +10 −1
| @@ -8,6 +8,7 @@ import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; | ||
| 8 | 8 | import { dutyStates, switchDuty } from "../../maintain/pause.ts"; |
| 9 | 9 | import { DISMISS_DAYS, REASON_MAX, dismiss, dismissals, dismissalWords } from "../../maintain/dismiss.ts"; |
| 10 | 10 | import { duties, quietLine, splitDuties } from "../../maintenance/duties.ts"; |
| 11 | import { NOTHING_FILED, filedPieces } from "../../maintenance/filed.ts"; | |
| 11 | 12 | import { statusWithStandby } from "../../maintenance/standby.ts"; |
| 12 | 13 | import { ABOUT_TO_STOP_ROWS, prepare, resumeNow, status } from "../../maintenance/service.ts"; |
| 13 | 14 | import type { Ctx } from "../../http/context.ts"; |
| @@ -38,6 +39,14 @@ const fold = (id: string, title: string, count: string, open: boolean, body: Htm | ||
| 38 | 39 | |
| 39 | 40 | const plural = (n: number, one: string, many: string): string => `${String(n)} ${n === 1 ? one : many}`; |
| 40 | 41 | |
| 42 | /** A loud duty's row: what it found with each issue or pull it filed linked, or "nothing filed". */ | |
| 43 | function foundWords(ctx: Ctx, d: { detail: string; found: string; lastRun: string }): Html { | |
| 44 | if (d.lastRun === "never" || d.found === "not finished") return html`${d.detail}`; | |
Only the page is covered. The TUI maintenance screen prints the same findings raw and leaks unreadable repository names; apply filedPieces there too, with a test. |
||
| 45 | const { pieces, filed } = filedPieces(ctx.app.db, d.detail, ctx.auth.user); | |
| 46 | if (!filed) return html`${d.detail}; <span data-filed="none">${NOTHING_FILED}</span>`; | |
| 47 | return html`${pieces.map((p) => ("href" in p ? html`<a href="${p.href}" data-filed="link">${p.label}</a>` : html`${p.text}`))}`; | |
| 48 | } | |
| 49 | ||
| 41 | 50 | /** What waits on a person, in words; empty when nothing does. Plain data in, plain data out. */ |
| 42 | 51 | export function needsYou(o: { phase: string; healthy?: boolean; overdue: number; paused: number; stale: number }): string[] { |
| 43 | 52 | const out: string[] = []; |
| @@ -77,7 +86,7 @@ export async function maintenancePage(ctx: Ctx): Promise<Response> { | ||
| 77 | 86 | <div class="form-actions mb"><form method="post" action="/maintenance/resume">${btn("End maintenance now")}</form></div>`} |
| 78 | 87 | ${fold("maintenance-duties-fold", "Duties", `${String(ran.length)}${overdue + paused > 0 ? `, ${String(overdue + paused)} need you` : ""}`, overdue + paused > 0, |
| 79 | 88 | html`<dl id="maintenance-duties" class="status-rows"> |
| 80 | ${loud.map((d) => fact(d.name, html`${d.detail}${d.overdue ? html` ${badge("overdue", { tone: "danger" })}` : null}`))} | |
| 89 | ${loud.map((d) => fact(d.name, html`${foundWords(ctx, d)}${d.overdue ? html` ${badge("overdue", { tone: "danger" })}` : null}`))} | |
| 81 | 90 | ${quiet.length === 0 ? null : disclosure(html`<span class="muted">${quietLine(quiet.length, loud.length === 0)}</span>`, html`<dl class="status-rows">${quiet.map((d) => fact(d.name, d.detail))}</dl>`, { id: "maintenance-quiet", extra: "mb" })} |
| 82 | 91 | ${states.map((d) => { |
| 83 | 92 | const dm = dismissed.get(d.name); |
test/maintenance-filed.test.ts added +84
| @@ -0,0 +1,84 @@ | ||
| 1 | /** | |
| 2 | * A finding on /maintenance links the issue or pull it filed; one with none says so; an unreadable | |
| 3 | * one is not linked or named. | |
| 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 { createIssue } from "../src/core/issues.ts"; | |
| 11 | import { createRepo } from "../src/core/repos.ts"; | |
| 12 | import { createUser, type User } from "../src/core/users.ts"; | |
| 13 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 14 | import { maintenanceLines } from "../src/tui/screens/maintenance.ts"; | |
| 15 | import type { Session } from "../src/tui/session.ts"; | |
| 16 | import { filedPieces, HIDDEN } from "../src/maintenance/filed.ts"; | |
| 17 | import { JOBS } from "../src/nightly/jobs.ts"; | |
| 18 | import { writeLast } from "../src/nightly/stamps.ts"; | |
| 19 | import { quietLogs } from "./helpers/globals.ts"; | |
| 20 | ||
| 21 | quietLogs(); | |
| 22 | let dir = "", app: App, servers: Servers, cookie = "", pat: User; | |
| 23 | ||
| 24 | beforeAll(async () => { | |
| 25 | dir = await mkdtemp(join(tmpdir(), "forge-filed-")); | |
| 26 | app = createApp({ dataDir: dir, port: 0 }); | |
| 27 | servers = await serve(app); | |
| 28 | const mk = async (username: string, isAdmin: boolean): Promise<User> => { | |
| 29 | const made = await createUser(app.db, { username, email: `${username}@forge.test`, password: "a-good-passphrase", origin: "seed", trusted: true, isAdmin }); | |
| 30 | if (!made.ok) throw new Error("user"); | |
| 31 | return made.value; | |
| 32 | }; | |
| 33 | const boss = await mk("boss", true); | |
| 34 | pat = await mk("pat", false); | |
| 35 | const open = await createRepo(app.db, app.reposRoot, { ownerId: boss.id, name: "open", visibility: "public", description: "o" }); | |
| 36 | const closed = await createRepo(app.db, app.reposRoot, { ownerId: boss.id, name: "closed", visibility: "private", description: "c" }); | |
| 37 | if (!open.ok || !closed.ok) throw new Error("repo"); | |
| 38 | for (const r of [open.value, closed.value]) if (!createIssue(app.db, { repoId: r.id, authorId: boss.id, title: "t" }).ok) throw new Error("issue"); | |
| 39 | const res = await fetch(`${servers.url}/signin`, { | |
| 40 | method: "POST", redirect: "manual", | |
| 41 | headers: { "content-type": "application/x-www-form-urlencoded", origin: servers.url }, | |
| 42 | body: new URLSearchParams({ username: "boss", password: "a-good-passphrase" }), | |
| 43 | }); | |
| 44 | cookie = (res.headers.get("set-cookie") ?? "").split(";")[0] ?? ""; | |
| 45 | }); | |
| 46 | afterAll(async () => { | |
| 47 | servers.stop(); | |
| 48 | app.close(); | |
| 49 | await rm(dir, { recursive: true, force: true }); | |
| 50 | }); | |
| 51 | ||
| 52 | test("one linked, one unlinked and one unreadable finding", async () => { | |
| 53 | const now = Date.now(); | |
| 54 | const says = (s: string) => ({ slot: now, startedAt: now, finishedAt: now + 1000, says: s }); | |
| 55 | writeLast(dir, JOBS[0]!.name, says("filed boss/open#1")); | |
| 56 | writeLast(dir, JOBS[1]!.name, says("found 2 flakes")); | |
| 57 | writeLast(dir, JOBS[2]!.name, says("filed ghost/secret#7 and boss/closed#1")); | |
| 58 | const html = await (await fetch(`${servers.url}/maintenance`, { headers: { cookie } })).text(); | |
| 59 | expect(html).toContain('<a href="/boss/open/issues/1" data-filed="link">boss/open#1</a>'); | |
| 60 | expect(html).toContain("found 2 flakes; <span data-filed=\"none\">nothing filed</span>"); | |
| 61 | expect(html).not.toContain("ghost/secret"); | |
| 62 | expect(html).not.toContain("/ghost/"); | |
| 63 | expect(html).toContain('<a href="/boss/closed/issues/1" data-filed="link">boss/closed#1</a>'); | |
| 64 | expect(html).toContain(HIDDEN); | |
| 65 | }); | |
| 66 | ||
| 67 | test("a private repository is neither linked nor named to someone who cannot read it", () => { | |
| 68 | const seen = filedPieces(app.db, "filed boss/closed#1, boss/open#1", pat); | |
| 69 | expect(seen.filed).toBe(true); | |
| 70 | expect(JSON.stringify(seen.pieces)).not.toContain("closed"); | |
| 71 | expect(seen.pieces.filter((p) => "href" in p).map((p) => (p as { href: string }).href)).toEqual(["/boss/open/issues/1"]); | |
| 72 | expect(filedPieces(app.db, "filed nothing", pat).filed).toBe(false); | |
| 73 | }); | |
| 74 | ||
| 75 | test("the terminal prints the same words: plain references, nothing filed, nothing unreadable", () => { | |
| 76 | const lines = (user: User): string => | |
| 77 | maintenanceLines({ app, db: app.db, user } as unknown as Session).join(" ").replace(/\s+/g, " "); | |
| 78 | const pat_ = lines(pat); | |
| 79 | expect(pat_).toContain("boss/open#1"); | |
| 80 | expect(pat_).toContain(HIDDEN); | |
| 81 | expect(pat_).not.toContain("boss/closed"); | |
| 82 | expect(pat_).not.toContain("ghost"); | |
| 83 | expect(pat_).toContain("nothing filed"); | |
| 84 | }); |
new above
offline
canRead already ran on the line above; this second call is dead logic. Compute
readableonce and branch on it.