|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { existsSync } from "node:fs"; |
| 3 | +import { readFile } from "node:fs/promises"; |
| 4 | +import path from "node:path"; |
| 5 | + |
| 6 | +// Line-count ceiling for every tracked `AGENTS.md` in the repo. Enforces the |
| 7 | +// progressive-disclosure invariant: rules colocate with the surface they |
| 8 | +// govern, and no single guide is allowed to grow back into a monolith. |
| 9 | +// |
| 10 | +// The target is 150. Exemptions below are tombstoned — remove once the |
| 11 | +// corresponding migration lands. |
| 12 | +export const DEFAULT_MAX_LINES = 150; |
| 13 | + |
| 14 | +// Paths (repo-root relative, forward slash) that are intentionally allowed to |
| 15 | +// exceed the cap today. Each entry MUST name the reason and the PR/condition |
| 16 | +// under which it can be removed. |
| 17 | +export const KNOWN_EXEMPTIONS: ReadonlyArray<{ path: string; reason: string }> = [ |
| 18 | + { |
| 19 | + path: "AGENTS.md", |
| 20 | + // Remove once the root-CLAUDE.md slim-down PR (the "task -> guide" router |
| 21 | + // split) lands. Tracking: council verdict 2026-04-24. |
| 22 | + reason: |
| 23 | + "Root AGENTS.md is scheduled for extraction into docs/contributing/*. Exemption must be removed once that restructure lands.", |
| 24 | + }, |
| 25 | + { |
| 26 | + path: "docs/reference/templates/AGENTS.md", |
| 27 | + // Templates document exemplar content (copy-paste starter material). They |
| 28 | + // are not live contributor rules and never should be. |
| 29 | + reason: "Template file — documents exemplar content, not live rules.", |
| 30 | + }, |
| 31 | +] as const; |
| 32 | + |
| 33 | +function writeStdoutLine(message: string): void { |
| 34 | + process.stdout.write(`${message}\n`); |
| 35 | +} |
| 36 | + |
| 37 | +type ParsedArgs = { |
| 38 | + maxLines: number; |
| 39 | +}; |
| 40 | + |
| 41 | +function parseArgs(argv: ReadonlyArray<string>): ParsedArgs { |
| 42 | + let maxLines = DEFAULT_MAX_LINES; |
| 43 | + |
| 44 | + for (let index = 0; index < argv.length; index++) { |
| 45 | + const arg = argv[index]; |
| 46 | + if (arg === "--max") { |
| 47 | + const next = argv[index + 1]; |
| 48 | + if (!next || Number.isNaN(Number(next))) { |
| 49 | + throw new Error("Missing/invalid --max value"); |
| 50 | + } |
| 51 | + maxLines = Number(next); |
| 52 | + index++; |
| 53 | + continue; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return { maxLines }; |
| 58 | +} |
| 59 | + |
| 60 | +function normalizePath(filePath: string): string { |
| 61 | + return filePath.split(path.sep).join("/"); |
| 62 | +} |
| 63 | + |
| 64 | +function gitLsAgentsFiles(cwd?: string): string[] { |
| 65 | + const stdout = execFileSync( |
| 66 | + "git", |
| 67 | + ["ls-files", "--cached", "--others", "--exclude-standard", "*AGENTS.md"], |
| 68 | + { encoding: "utf8", cwd }, |
| 69 | + ); |
| 70 | + return stdout |
| 71 | + .split("\n") |
| 72 | + .map((line) => normalizePath(line.trim())) |
| 73 | + .filter(Boolean) |
| 74 | + .filter((filePath) => path.basename(filePath) === "AGENTS.md"); |
| 75 | +} |
| 76 | + |
| 77 | +export function isExempt( |
| 78 | + relativePath: string, |
| 79 | + exemptions: ReadonlyArray<{ path: string }> = KNOWN_EXEMPTIONS, |
| 80 | +): boolean { |
| 81 | + const normalized = normalizePath(relativePath); |
| 82 | + return exemptions.some((entry) => entry.path === normalized); |
| 83 | +} |
| 84 | + |
| 85 | +export type Offender = { |
| 86 | + filePath: string; |
| 87 | + lines: number; |
| 88 | +}; |
| 89 | + |
| 90 | +export async function findOversizedAgentsFiles( |
| 91 | + filePaths: ReadonlyArray<string>, |
| 92 | + maxLines: number, |
| 93 | + exemptions: ReadonlyArray<{ path: string }> = KNOWN_EXEMPTIONS, |
| 94 | + readFileImpl: (p: string) => Promise<string> = (p) => readFile(p, "utf8"), |
| 95 | +): Promise<Offender[]> { |
| 96 | + const results = await Promise.all( |
| 97 | + filePaths |
| 98 | + .filter((filePath) => !isExempt(filePath, exemptions)) |
| 99 | + .map(async (filePath) => { |
| 100 | + const content = await readFileImpl(filePath); |
| 101 | + // Count physical lines. Keeps the rule predictable across editors. |
| 102 | + const lines = content.split("\n").length; |
| 103 | + return { filePath, lines }; |
| 104 | + }), |
| 105 | + ); |
| 106 | + |
| 107 | + return results.filter((result) => result.lines > maxLines).toSorted((a, b) => b.lines - a.lines); |
| 108 | +} |
| 109 | + |
| 110 | +async function main(): Promise<void> { |
| 111 | + // Makes `... | head` safe. |
| 112 | + process.stdout.on("error", (error: NodeJS.ErrnoException) => { |
| 113 | + if (error.code === "EPIPE") { |
| 114 | + process.exit(0); |
| 115 | + } |
| 116 | + throw error; |
| 117 | + }); |
| 118 | + |
| 119 | + const { maxLines } = parseArgs(process.argv.slice(2)); |
| 120 | + const tracked = gitLsAgentsFiles().filter((filePath) => existsSync(filePath)); |
| 121 | + |
| 122 | + const offenders = await findOversizedAgentsFiles(tracked, maxLines); |
| 123 | + |
| 124 | + if (!offenders.length) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + writeStdoutLine(`AGENTS.md files exceeding ${maxLines} lines:`); |
| 129 | + for (const offender of offenders) { |
| 130 | + writeStdoutLine(`${offender.lines}\t${offender.filePath}`); |
| 131 | + } |
| 132 | + writeStdoutLine(""); |
| 133 | + writeStdoutLine("Extract rules into the scoped guide they govern, or into docs/contributing/*."); |
| 134 | + writeStdoutLine( |
| 135 | + "If a file genuinely must exceed the cap, add it to KNOWN_EXEMPTIONS in scripts/check-agents-md.ts with a tombstoned reason.", |
| 136 | + ); |
| 137 | + |
| 138 | + process.exitCode = 1; |
| 139 | +} |
| 140 | + |
| 141 | +// Only run main when executed directly (keeps tests clean). |
| 142 | +const invokedDirectly = |
| 143 | + typeof process.argv[1] === "string" && |
| 144 | + normalizePath(process.argv[1]).endsWith("scripts/check-agents-md.ts"); |
| 145 | +if (invokedDirectly) { |
| 146 | + await main(); |
| 147 | +} |
0 commit comments