diff --git a/AGENTS.md b/AGENTS.md index b0b157f29..526f1fdd4 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -65,4 +65,6 @@ Maintainer/agent docs live under [docs/internal/](docs/internal/): - [EVENTS.md](docs/internal/EVENTS.md) — the same catalog with rationale and ADR references - [IDEAS.md](docs/internal/IDEAS.md) — post-v1 ideas; don't implement these unless asked - [KNOWN-PITFALLS.md](docs/internal/KNOWN-PITFALLS.md) — accepted gaps and their planned fixes +- [LOOP.md](docs/internal/LOOP.md) — the old stage-implementation loop, kept as + history; DELIVERY.md replaces it - [adr/](docs/internal/adr/) — architecture decision records and their status diff --git a/docs/HTTP-API.md b/docs/HTTP-API.md index 339faad88..5f12614bb 100644 --- a/docs/HTTP-API.md +++ b/docs/HTTP-API.md @@ -493,11 +493,7 @@ not distinguish them. (`lease.renew`, `lease.release`, `device.exec`) directly, so another requester's own, still-live lease answers `403 FORBIDDEN` from them — the same answer the socket transport gives, via the same operation's `ownsLease` -authorize hook. (0.3.0 briefly had every lease route answering `404` here; -that overcorrected the lease-*request* routes' old `403` and is why renew -and release were moved off the `lease.list`-filtered lookup — see -`docs/internal/KNOWN-PITFALLS.md`. `exec` arrives on the dispatching side of that -split, with renew and release.) +authorize hook. This is different again from the lease-*request* routes below (`/v1/lease-requests/{id}` and friends), which answer `403 FORBIDDEN` for a diff --git a/docs/loop.md b/docs/internal/LOOP.md similarity index 95% rename from docs/loop.md rename to docs/internal/LOOP.md index e58bc57c5..df99f58c4 100644 --- a/docs/loop.md +++ b/docs/internal/LOOP.md @@ -3,6 +3,9 @@ Historical instructions for implementing a Simlock stage. Completed stage specifications have been removed from `docs/stages/`. +Do not follow this loop for new work. [DELIVERY.md](DELIVERY.md) and the +delivery rules in `docs/internal/agent-rules/delivery.md` replace it. + ## Phase 0 — Orient 1. Read `AGENTS.md`, all of `docs/internal/agent-rules/`, and `docs/internal/ARCHITECTURE.md`. diff --git a/src/docs-audience.test.ts b/src/docs-audience.test.ts new file mode 100644 index 000000000..12c961e11 --- /dev/null +++ b/src/docs-audience.test.ts @@ -0,0 +1,172 @@ +import { execFileSync } from "node:child_process"; +import { readFileSync, statSync } from "node:fs"; +import { basename, dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; + +import { describe, expect, it } from "vitest"; + +const REPO_ROOT = join(dirname(fileURLToPath(import.meta.url)), ".."); + +/** Every tracked path, from the git index, so no doc escapes by sitting in a directory the test did not name. */ +const trackedFiles = execFileSync("git", ["ls-files", "-z"], { + cwd: REPO_ROOT, + encoding: "utf8", + maxBuffer: 32 * 1024 * 1024, +}) + .split("\0") + .filter((name) => name.length > 0) + // A path deleted from the working tree is still listed by `git ls-files`; only regular files + // can be read. + .filter((name) => { + try { + return statSync(join(REPO_ROOT, name)).isFile(); + } catch { + return false; + } + }); + +/** A markdown file by extension, in any case: `.md` or `.markdown`. */ +const isMarkdown = (name: string) => /\.(?:md|markdown)$/i.test(name); + +/** `README.md` and every markdown file under `docs/` that is not under `docs/internal/`. */ +const endUserDocs = trackedFiles.filter( + (name) => + name === "README.md" || + (name.startsWith("docs/") && !name.startsWith("docs/internal/") && isMarkdown(name)), +); + +const endUserNames = endUserDocs.map((doc) => basename(doc)); + +/** + * File names that exist only under `docs/internal/`. `EVENTS.md` and `README.md` are left out: + * an end-user doc of the same name exists, so the bare name says nothing about the audience. + */ +const internalOnlyNames = [ + ...new Set( + trackedFiles + .filter((name) => name.startsWith("docs/internal/") && isMarkdown(name)) + .map((name) => basename(name)), + ), +].filter((name) => !endUserNames.includes(name)); + +const escape = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + +/** + * An internal-only name, matched in any case unless that would also match an end-user doc's + * name: `events.md` is matched exactly, since ignoring case it is `EVENTS.md`. + */ +const internalName = (name: string) => + new RegExp( + `(? doc.toLowerCase() === name.toLowerCase()) ? "" : "i", + ); + +/** What documentation rule 2 forbids an end-user doc from naming. */ +const FORBIDDEN: RegExp[] = [ + /docs\/internal\b/, + // `internal/` starting a path: an inline, angle-bracket, reference-style or HTML link from + // `docs/`, or the same path in backticks or prose. + /(? { + it("include the known end-user docs, beside a list of internal-only names", () => { + // Without this, an empty enumeration would leave the scan below with nothing to read and + // the suite would pass while checking no doc at all. + expect(endUserDocs).toEqual( + expect.arrayContaining([ + "README.md", + "docs/ABOUT.md", + "docs/CLI.md", + "docs/CLIENT.md", + "docs/CONFIGURATION.md", + "docs/EVENTS.md", + "docs/HTTP-API.md", + ]), + ); + expect(internalOnlyNames).toEqual( + expect.arrayContaining(["ARCHITECTURE.md", "KNOWN-PITFALLS.md", "events.md", "testing.md"]), + ); + }); + + it("name no path under docs/internal/, no internal-only doc by file name, and no ADR", () => { + const offenders = endUserDocs.flatMap((doc) => + readFileSync(join(REPO_ROOT, doc), "utf8") + .split("\n") + .flatMap((text, index) => + FORBIDDEN.flatMap((pattern) => { + const match = pattern.exec(text); + return match === null ? [] : [`${doc}:${index + 1}: ${match[0]}`]; + }), + ), + ); + + // `ARCHITECTURE.md` and `architecture.md` both match either spelling; report a hit once. + expect([...new Set(offenders)]).toEqual([]); + }); +}); + +/** The text of `markdown` from the first line starting with `from` up to the next starting with `to`. */ +function section(markdown: string, from: string, to: string): string { + const lines = markdown.split("\n"); + const start = lines.findIndex((line) => line.startsWith(from)); + const end = lines.findIndex((line, index) => index > start && line.startsWith(to)); + return start === -1 || end === -1 ? "" : lines.slice(start, end).join("\n"); +} + +/** + * Documentation rule 1: `docs/` is the end-user directory, and a doc for maintainers or agents + * goes under `docs/internal/`. The two places that name the end-user docs -- AGENTS.md's list + * and rule 2's list -- are what a markdown file under `docs/` and outside `docs/internal/` has + * to be one of. Both lists name files directly in `docs/`, so a doc in any other subdirectory + * of `docs/` fails. + */ +describe("the markdown files under docs/ outside docs/internal/", () => { + const inDocsDir = endUserDocs + .filter((name) => name !== "README.md") + .toSorted((a, b) => a.localeCompare(b)); + + const listedInAgents = [ + ...section( + readFileSync(join(REPO_ROOT, "AGENTS.md"), "utf8"), + "End-user docs live directly under", + "Maintainer/agent docs live under", + ).matchAll(/\]\((docs\/[^/)]+\.md)\)/g), + ] + .map((match) => match[1] ?? "") + .toSorted((a, b) => a.localeCompare(b)); + + const listedInRule2 = [ + ...section( + readFileSync(join(REPO_ROOT, "docs/internal/agent-rules/documentation.md"), "utf8"), + "2. **", + "3. **", + ).matchAll(/`(docs\/[^/`]+\.md)`/g), + ] + .map((match) => match[1] ?? "") + .toSorted((a, b) => a.localeCompare(b)); + + it("were read, along with both lists of end-user docs", () => { + // Without this, a heading reworded in AGENTS.md or documentation.md would empty a list + // and the comparison below would fail for the wrong reason -- or, with docs/ also + // empty, pass while checking nothing. + for (const list of [inDocsDir, listedInAgents, listedInRule2]) { + expect(list).toEqual(expect.arrayContaining(["docs/CLI.md", "docs/HTTP-API.md"])); + } + }); + + it("are exactly the end-user docs AGENTS.md and documentation rule 2 list", () => { + expect({ listedInAgents: inDocsDir, listedInRule2: inDocsDir }).toEqual({ + listedInAgents, + listedInRule2, + }); + }); +});