Public: anyone can read this; the crew can change it.
The maintain duty works in slices and yields to the serving thread #17840
Merged 4h 30m ago, live in production
torque/forge:torque/17823-the-maintain-duty-works-in-slices-and-yi → main 0 commits fork 1 thread · 1 unresolved
4 files
changed +91
−13
src/maintain/duties/dead_doc_paths.ts +9 −4
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | |
| 7 | 7 | import { existsSync, readdirSync, readFileSync } from "node:fs"; |
| 8 | 8 | import { join } from "node:path"; |
| 9 | import { sliced } from "../../kernel/breathe.ts"; | |
| 9 | 10 | import type { Duty } from "../duty.ts"; |
| 10 | 11 | |
| 11 | 12 | export interface DeadPath { |
| @@ -29,10 +30,14 @@ function docsIn(root: string, dir: string): string[] { | ||
| 29 | 30 | } |
| 30 | 31 | } |
| 31 | 32 | |
| 32 | /** Every path the docs name that is missing under `root`, in doc then line order. */ | |
| 33 | export function deadDocPaths(root: string = process.cwd()): DeadPath[] { | |
| 33 | /** | |
| 34 | * Every path the docs name that is missing under `root`, in doc then line order. The ~800 docs are | |
| 35 | * read in slices (#17823): the thread is handed back whenever `slice` says its time is up. | |
| 36 | */ | |
| 37 | export async function deadDocPaths(root: string = process.cwd(), slice: ReturnType<typeof sliced> = sliced()): Promise<DeadPath[]> { | |
| 34 | 38 | const out: DeadPath[] = []; |
| 35 | 39 | for (const doc of [...docsIn(root, "docs"), ...docsIn(root, "docs/features")]) { |
| 40 | if (slice.due()) await slice.breathe(); | |
| 36 | 41 | let text: string; |
| 37 | 42 | try { |
| 38 | 43 | text = readFileSync(join(root, doc), "utf8"); |
| @@ -57,8 +62,8 @@ export const makeDuty = (root?: string): Duty => ({ | ||
| 57 | 62 | name: "dead-doc-paths", |
| 58 | 63 | intervalMs: 24 * 3_600_000, |
| 59 | 64 | budget: { runsPerDay: 3, dollarsPerDay: 1, reason: "once a day is enough for a doc to go stale; it reads files and spends no money" }, |
| 60 | run({ now }) { | |
| 61 | const dead = deadDocPaths(root); | |
| 65 | async run({ now }) { | |
| 66 | const dead = await deadDocPaths(root); | |
| 62 | 67 | if (dead.length === 0) return { findings: [], actions: [] }; |
| 63 | 68 | // One finding per doc, every dead path in it with its line, so a person can reach them all. |
| 64 | 69 | const byDoc = new Map<string, DeadPath[]>(); |
src/maintain/duties/untested_sources.ts +20 −5
| @@ -4,10 +4,15 @@ | ||
| 4 | 4 | * forge's own checkout, never the database, and files one finding naming the first ten, not one |
| 5 | 5 | * per file; an empty list files nothing. Entry points, which a test cannot import because |
| 6 | 6 | * something else runs them, are listed in `ENTRY_POINTS` with a reason each. |
| 7 | * | |
| 8 | * The pass works in slices (#17823): reading and matching the forge's ~5,100 test files (~37 MB) | |
| 9 | * in one go held the serving thread for 0.3 s here, so it breathes (`kernel/breathe.ts`) whenever a | |
| 10 | * slice has run its 20 ms, and the files left carry to the next turn of the loop. | |
| 7 | 11 | */ |
| 8 | 12 | |
| 9 | 13 | import { readFileSync } from "node:fs"; |
| 10 | 14 | import { dirname, join, normalize } from "node:path"; |
| 15 | import { sliced } from "../../kernel/breathe.ts"; | |
| 11 | 16 | import type { Duty } from "../duty.ts"; |
| 12 | 17 | |
| 13 | 18 | /** Files a test never imports by design: each is started by the runtime or a script, with why. */ |
| @@ -41,10 +46,20 @@ function importedBy(root: string, testFile: string): string[] { | ||
| 41 | 46 | return out; |
| 42 | 47 | } |
| 43 | 48 | |
| 44 | /** Source files under `src/` that no test imports by path, minus the entry points; sorted. */ | |
| 45 | export function untestedSources(root: string, entries: readonly { readonly path: string }[] = ENTRY_POINTS): string[] { | |
| 49 | /** | |
| 50 | * Source files under `src/` that no test imports by path, minus the entry points; sorted. One test | |
| 51 | * file is read at a time and the thread is handed back whenever `slice` says its time is up. | |
| 52 | */ | |
| 53 | export async function untestedSources( | |
| 54 | root: string, | |
| 55 | entries: readonly { readonly path: string }[] = ENTRY_POINTS, | |
| 56 | slice: ReturnType<typeof sliced> = sliced(), | |
| 57 | ): Promise<string[]> { | |
| 46 | 58 | const seen = new Set<string>(); |
| 47 | for (const t of files(root, "test/**/*.ts")) for (const p of importedBy(root, t)) seen.add(p); | |
| 59 | for (const t of files(root, "test/**/*.ts")) { | |
| 60 | if (slice.due()) await slice.breathe(); | |
| 61 | for (const p of importedBy(root, t)) seen.add(p); | |
| 62 | } | |
| 48 | 63 | const skip = new Set(entries.map((e) => e.path)); |
| 49 | 64 | return files(root, "src/**/*.ts").filter((f) => !f.endsWith(".d.ts") && !skip.has(f) && !seen.has(f)); |
| 50 | 65 | } |
| @@ -61,8 +76,8 @@ export function untestedDuty(root: string, entries: readonly { readonly path: st | ||
| 61 | 76 | name: "untested-sources", |
| 62 | 77 | intervalMs: 7 * 24 * 3_600_000, |
| 63 | 78 | budget: { runsPerDay: 2, dollarsPerDay: 1, reason: "weekly; the rest is room for a catch-up, and it spends no money" }, |
| 64 | run({ now }) { | |
| 65 | const list = untestedSources(root, entries); | |
| 79 | async run({ now }) { | |
| 80 | const list = await untestedSources(root, entries); | |
| 66 | 81 | if (list.length === 0) return { findings: [], actions: [] }; |
| 67 | 82 | return { |
| 68 | 83 | findings: [{ summary: summaryOf(list), probe: { kind: "measurement", what: "source files under src/ that no test under test/ imports by path", number: list.length, at: now.toISOString() } }], |
test/maint-untested.test.ts +48 −2
| @@ -6,6 +6,7 @@ import { dirname, join } from "node:path"; | ||
| 6 | 6 | import { rulesWorld } from "./helpers/rules-app.ts"; |
| 7 | 7 | import { duties } from "../src/maintain/registry.ts"; |
| 8 | 8 | import { FOOTER, SHOWN, untestedDuty, untestedSources } from "../src/maintain/duties/untested_sources.ts"; |
| 9 | import { SLICE_MS, sliced } from "../src/kernel/breathe.ts"; | |
| 9 | 10 | |
| 10 | 11 | const dirs: string[] = []; |
| 11 | 12 | function tree(files: Record<string, string>): string { |
| @@ -22,7 +23,7 @@ const w = rulesWorld(); | ||
| 22 | 23 | const NOW = new Date("2026-11-16T00:00:00Z"); |
| 23 | 24 | const run = async (root: string, entries: readonly { path: string }[] = []) => await untestedDuty(root, entries).run({ db: w.app.db, bus: w.app.bus, app: w.app, now: NOW }); |
| 24 | 25 | |
| 25 | test("a source no test imports is listed; imported, entry-point and declaration files are not", () => { | |
| 26 | test("a source no test imports is listed; imported, entry-point and declaration files are not", async () => { | |
| 26 | 27 | const root = tree({ |
| 27 | 28 | "src/a.ts": "export const a = 1;", |
| 28 | 29 | "src/b.ts": "export const b = 1;", |
| @@ -33,7 +34,7 @@ test("a source no test imports is listed; imported, entry-point and declaration | ||
| 33 | 34 | "src/types.d.ts": "", |
| 34 | 35 | "test/a.test.ts": 'import { a } from "../src/a.ts";\nimport "../src/sub";\nconst x = await import("../src/c.ts");', |
| 35 | 36 | }); |
| 36 | expect(untestedSources(root, [{ path: "src/main.ts" }])).toEqual(["src/b.ts", "src/dyn.ts"]); | |
| 37 | expect(await untestedSources(root, [{ path: "src/main.ts" }])).toEqual(["src/b.ts", "src/dyn.ts"]); | |
| 37 | 38 | }); |
| 38 | 39 | |
| 39 | 40 | test("one finding carries the first ten names, the rest counted, and the footer", async () => { |
| @@ -59,3 +60,48 @@ test("an empty list files nothing", async () => { | ||
| 59 | 60 | test("it is registered", () => { |
| 60 | 61 | expect(duties().map((d) => d.name)).toContain("untested-sources"); |
| 61 | 62 | }); |
| 63 | ||
| 64 | /** | |
| 65 | * A slice clock that counts files instead of milliseconds: `due()` is asked once per file, so the | |
| 66 | * count at each breath is how many files that slice read. `cap` files make a slice. | |
| 67 | */ | |
| 68 | function countingSlice(cap: number) { | |
| 69 | const slices: number[] = []; | |
| 70 | let n = 0; | |
| 71 | return { | |
| 72 | slices, | |
| 73 | due: () => n++ >= cap, | |
| 74 | breathe: async () => { | |
| 75 | slices.push(n - 1); | |
| 76 | n = 1; | |
| 77 | }, | |
| 78 | }; | |
| 79 | } | |
| 80 | ||
| 81 | test("a large backlog of test files is read across many slices, none over the cap, and all of it counts (#17823)", async () => { | |
| 82 | const files: Record<string, string> = {}; | |
| 83 | for (let i = 0; i < 250; i++) { | |
| 84 | files[`src/s${i}.ts`] = ""; | |
| 85 | // Every test but the last imports its source; the last one's source is the only untested file. | |
| 86 | if (i < 249) files[`test/t${String(i).padStart(3, "0")}.test.ts`] = `import "../src/s${i}.ts";`; | |
| 87 | } | |
| 88 | const slice = countingSlice(40); | |
| 89 | expect(await untestedSources(tree(files), [], slice)).toEqual(["src/s249.ts"]); | |
| 90 | // 249 files at 40 a slice: six full slices breathe, the seventh (9 files) ends the pass. | |
| 91 | expect(slice.slices).toEqual([40, 40, 40, 40, 40, 40]); | |
| 92 | expect(Math.max(...slice.slices)).toBeLessThanOrEqual(40); | |
| 93 | }); | |
| 94 | ||
| 95 | test("the pass hands the thread back between slices: a timer set before it fires before it ends (#17823)", async () => { | |
| 96 | const files: Record<string, string> = { "src/a.ts": "" }; | |
| 97 | for (let i = 0; i < 5; i++) files[`test/t${i}.test.ts`] = 'import "../src/a.ts";'; | |
| 98 | let fired = false; | |
| 99 | setTimeout(() => (fired = true), 0); | |
| 100 | // A clock that runs SLICE_MS per look: every file is a slice of its own, so it breathes each | |
| 101 | // time. | |
| 102 | let clock = 0; | |
| 103 | const pass = untestedSources(tree(files), [], sliced(() => (clock += SLICE_MS))); | |
| 104 | expect(fired).toBe(false); | |
| 105 | expect(await pass).toEqual([]); | |
| 106 | expect(fired).toBe(true); | |
| 107 | }); |
test/maintain-dead-doc-paths.test.ts +14 −2
| @@ -6,6 +6,7 @@ import { expect, test } from "bun:test"; | ||
| 6 | 6 | import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; |
| 7 | 7 | import { tmpdir } from "node:os"; |
| 8 | 8 | import { join } from "node:path"; |
| 9 | import { SLICE_MS, sliced } from "../src/kernel/breathe.ts"; | |
| 9 | 10 | import { deadDocPaths, duty, makeDuty } from "../src/maintain/duties/dead_doc_paths.ts"; |
| 10 | 11 | import { duties } from "../src/maintain/registry.ts"; |
| 11 | 12 | import { getUserByName } from "../src/core/users.ts"; |
| @@ -26,8 +27,8 @@ writeFileSync( | ||
| 26 | 27 | ); |
| 27 | 28 | writeFileSync(join(root, "docs", "features", "F.md"), "nothing here\n"); |
| 28 | 29 | |
| 29 | test("exactly the dead path is found, with its doc and line", () => { | |
| 30 | expect(deadDocPaths(root)).toEqual([{ doc: "docs/FIX.md", line: 3, path: "src/a/gone.ts" }]); | |
| 30 | test("exactly the dead path is found, with its doc and line", async () => { | |
| 31 | expect(await deadDocPaths(root)).toEqual([{ doc: "docs/FIX.md", line: 3, path: "src/a/gone.ts" }]); | |
| 31 | 32 | }); |
| 32 | 33 | |
| 33 | 34 | test("the duty makes one finding with a probe, and none when nothing is dead", async () => { |
| @@ -60,3 +61,14 @@ test("one finding per doc, every dead path listed, and the terminal's run board | ||
| 60 | 61 | const row = runLines(new Session(w.app, { sshUsername: "alice", remoteAddress: "127.0.0.1", fingerprint: "SHA256:t", user: getUserByName(w.app.db, "alice")! })).find((l) => l.startsWith("dead-doc-paths")); |
| 61 | 62 | expect(row).toMatch(/dead-doc-paths\s+\S+ \S+\s+2\s/); |
| 62 | 63 | }); |
| 64 | ||
| 65 | test("the docs are read in slices: a timer set before the pass fires before it ends, and the finding is whole (#17823)", async () => { | |
| 66 | let fired = false; | |
| 67 | setTimeout(() => (fired = true), 0); | |
| 68 | // A clock that runs SLICE_MS per look: every doc is a slice of its own. | |
| 69 | let clock = 0; | |
| 70 | const pass = deadDocPaths(root, sliced(() => (clock += SLICE_MS))); | |
| 71 | expect(fired).toBe(false); | |
| 72 | expect(await pass).toEqual([{ doc: "docs/FIX.md", line: 3, path: "src/a/gone.ts" }]); | |
| 73 | expect(fired).toBe(true); | |
| 74 | }); |
new above
offline
The glob listing of test/*/.ts above this loop is still one synchronous pass over about 5,100 paths, before the first breath. Next step: measure it and slice it if it matters.