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

The maintain duty works in slices and yields to the serving thread #17840

opened this

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

  • Merged. torque/17823-the-maintain-duty-works-in-slices-and-yi was merged into main as fadecfc.

Review

Sign in to review this change.
4 files changed +91 −13
src/maintain/duties/dead_doc_paths.ts +9 −4
@@ -6,6 +6,7 @@
66
77 import { existsSync, readdirSync, readFileSync } from "node:fs";
88 import { join } from "node:path";
9 import { sliced } from "../../kernel/breathe.ts";
910 import type { Duty } from "../duty.ts";
1011
1112 export interface DeadPath {
@@ -29,10 +30,14 @@ function docsIn(root: string, dir: string): string[] {
2930 }
3031 }
3132
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[]> {
3438 const out: DeadPath[] = [];
3539 for (const doc of [...docsIn(root, "docs"), ...docsIn(root, "docs/features")]) {
40 if (slice.due()) await slice.breathe();
3641 let text: string;
3742 try {
3843 text = readFileSync(join(root, doc), "utf8");
@@ -57,8 +62,8 @@ export const makeDuty = (root?: string): Duty => ({
5762 name: "dead-doc-paths",
5863 intervalMs: 24 * 3_600_000,
5964 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);
6267 if (dead.length === 0) return { findings: [], actions: [] };
6368 // One finding per doc, every dead path in it with its line, so a person can reach them all.
6469 const byDoc = new Map<string, DeadPath[]>();
src/maintain/duties/untested_sources.ts +20 −5
@@ -4,10 +4,15 @@
44 * forge's own checkout, never the database, and files one finding naming the first ten, not one
55 * per file; an empty list files nothing. Entry points, which a test cannot import because
66 * 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.
711 */
812
913 import { readFileSync } from "node:fs";
1014 import { dirname, join, normalize } from "node:path";
15 import { sliced } from "../../kernel/breathe.ts";
1116 import type { Duty } from "../duty.ts";
1217
1318 /** 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[] {
4146 return out;
4247 }
4348
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[]> {
4658 const seen = new Set<string>();

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.

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 }
4863 const skip = new Set(entries.map((e) => e.path));
4964 return files(root, "src/**/*.ts").filter((f) => !f.endsWith(".d.ts") && !skip.has(f) && !seen.has(f));
5065 }
@@ -61,8 +76,8 @@ export function untestedDuty(root: string, entries: readonly { readonly path: st
6176 name: "untested-sources",
6277 intervalMs: 7 * 24 * 3_600_000,
6378 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);
6681 if (list.length === 0) return { findings: [], actions: [] };
6782 return {
6883 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";
66 import { rulesWorld } from "./helpers/rules-app.ts";
77 import { duties } from "../src/maintain/registry.ts";
88 import { FOOTER, SHOWN, untestedDuty, untestedSources } from "../src/maintain/duties/untested_sources.ts";
9 import { SLICE_MS, sliced } from "../src/kernel/breathe.ts";
910
1011 const dirs: string[] = [];
1112 function tree(files: Record<string, string>): string {
@@ -22,7 +23,7 @@ const w = rulesWorld();
2223 const NOW = new Date("2026-11-16T00:00:00Z");
2324 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 });
2425
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 () => {
2627 const root = tree({
2728 "src/a.ts": "export const a = 1;",
2829 "src/b.ts": "export const b = 1;",
@@ -33,7 +34,7 @@ test("a source no test imports is listed; imported, entry-point and declaration
3334 "src/types.d.ts": "",
3435 "test/a.test.ts": 'import { a } from "../src/a.ts";\nimport "../src/sub";\nconst x = await import("../src/c.ts");',
3536 });
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"]);
3738 });
3839
3940 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 () => {
5960 test("it is registered", () => {
6061 expect(duties().map((d) => d.name)).toContain("untested-sources");
6162 });
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";
66 import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
77 import { tmpdir } from "node:os";
88 import { join } from "node:path";
9 import { SLICE_MS, sliced } from "../src/kernel/breathe.ts";
910 import { deadDocPaths, duty, makeDuty } from "../src/maintain/duties/dead_doc_paths.ts";
1011 import { duties } from "../src/maintain/registry.ts";
1112 import { getUserByName } from "../src/core/users.ts";
@@ -26,8 +27,8 @@ writeFileSync(
2627 );
2728 writeFileSync(join(root, "docs", "features", "F.md"), "nothing here\n");
2829
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" }]);
3132 });
3233
3334 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
6061 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"));
6162 expect(row).toMatch(/dead-doc-paths\s+\S+ \S+\s+2\s/);
6263 });
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 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs