Skip to content

Commit fecec43

Browse files
committed
CI: add AGENTS.md size guard to check-additional
Caps every tracked AGENTS.md at 150 lines. Exempts root AGENTS.md (tombstoned until the CLAUDE.md restructure extracts to docs/contributing/*) and docs/reference/templates/AGENTS.md (template, not live rules). Wires into the sharded check-additional layout via BOUNDARY_CHECKS in scripts/run-additional-boundary-checks.mjs.
1 parent ebd7f19 commit fecec43

4 files changed

Lines changed: 252 additions & 0 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,7 @@
13641364
"ios:version:sync": "node --import tsx scripts/ios-sync-versioning.ts --write",
13651365
"lint": "node scripts/run-oxlint-shards.mjs",
13661366
"lint:agent:ingress-owner": "node scripts/check-ingress-agent-owner-context.mjs",
1367+
"lint:agents-md:size": "node --import tsx scripts/check-agents-md.ts",
13671368
"lint:all": "node scripts/run-oxlint.mjs",
13681369
"lint:apps": "pnpm lint:swift",
13691370
"lint:auth:no-pairing-store-group": "node scripts/check-no-pairing-store-group-auth.mjs",

‎scripts/check-agents-md.ts‎

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
}

‎scripts/run-additional-boundary-checks.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const BOUNDARY_CHECKS = [
5252
["run", "lint:extensions:no-relative-outside-package"],
5353
],
5454
["lint:ui:no-raw-window-open", "pnpm", ["lint:ui:no-raw-window-open"]],
55+
["lint:agents-md:size", "pnpm", ["run", "lint:agents-md:size"]],
5556
].map(([label, command, args]) => ({ label, command, args }));
5657

5758
export function resolveConcurrency(value, fallback = 4) {
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
import { describe, expect, it } from "vitest";
4+
import {
5+
DEFAULT_MAX_LINES,
6+
findOversizedAgentsFiles,
7+
isExempt,
8+
KNOWN_EXEMPTIONS,
9+
} from "../../scripts/check-agents-md.ts";
10+
import { createScriptTestHarness } from "./test-helpers.js";
11+
12+
const { createTempDir } = createScriptTestHarness();
13+
14+
function writeAgentsFile(root: string, relPath: string, lineCount: number): string {
15+
const fullPath = path.join(root, relPath);
16+
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
17+
// `lineCount` physical lines = that many `\n`-separated chunks.
18+
const content = Array.from({ length: lineCount }, (_, i) => `line ${i + 1}`).join("\n");
19+
fs.writeFileSync(fullPath, content);
20+
return fullPath;
21+
}
22+
23+
describe("check-agents-md", () => {
24+
it("flags AGENTS.md files over the cap", async () => {
25+
const root = createTempDir("openclaw-agents-md-");
26+
writeAgentsFile(root, "src/foo/AGENTS.md", 200);
27+
28+
const offenders = await findOversizedAgentsFiles(["src/foo/AGENTS.md"], 150, [], (p) =>
29+
Promise.resolve(fs.readFileSync(path.join(root, p), "utf8")),
30+
);
31+
32+
expect(offenders).toEqual([{ filePath: "src/foo/AGENTS.md", lines: 200 }]);
33+
});
34+
35+
it("passes files at or below the cap", async () => {
36+
const root = createTempDir("openclaw-agents-md-");
37+
writeAgentsFile(root, "src/small/AGENTS.md", 100);
38+
writeAgentsFile(root, "src/exact/AGENTS.md", 150);
39+
40+
const offenders = await findOversizedAgentsFiles(
41+
["src/small/AGENTS.md", "src/exact/AGENTS.md"],
42+
150,
43+
[],
44+
(p) => Promise.resolve(fs.readFileSync(path.join(root, p), "utf8")),
45+
);
46+
47+
expect(offenders).toEqual([]);
48+
});
49+
50+
it("skips files on the exemption allowlist", async () => {
51+
const root = createTempDir("openclaw-agents-md-");
52+
writeAgentsFile(root, "AGENTS.md", 400);
53+
54+
const offenders = await findOversizedAgentsFiles(
55+
["AGENTS.md"],
56+
150,
57+
[{ path: "AGENTS.md" }],
58+
(p) => Promise.resolve(fs.readFileSync(path.join(root, p), "utf8")),
59+
);
60+
61+
expect(offenders).toEqual([]);
62+
});
63+
64+
it("sorts offenders by size descending", async () => {
65+
const root = createTempDir("openclaw-agents-md-");
66+
writeAgentsFile(root, "src/a/AGENTS.md", 160);
67+
writeAgentsFile(root, "src/b/AGENTS.md", 300);
68+
writeAgentsFile(root, "src/c/AGENTS.md", 200);
69+
70+
const offenders = await findOversizedAgentsFiles(
71+
["src/a/AGENTS.md", "src/b/AGENTS.md", "src/c/AGENTS.md"],
72+
150,
73+
[],
74+
(p) => Promise.resolve(fs.readFileSync(path.join(root, p), "utf8")),
75+
);
76+
77+
expect(offenders.map((o) => o.filePath)).toEqual([
78+
"src/b/AGENTS.md",
79+
"src/c/AGENTS.md",
80+
"src/a/AGENTS.md",
81+
]);
82+
});
83+
84+
it("normalizes Windows-style path separators when matching exemptions", () => {
85+
expect(isExempt("AGENTS.md", [{ path: "AGENTS.md" }])).toBe(true);
86+
expect(
87+
isExempt("docs\\reference\\templates\\AGENTS.md", [
88+
{ path: "docs/reference/templates/AGENTS.md" },
89+
]),
90+
).toBe(true);
91+
expect(isExempt("extensions/AGENTS.md", [{ path: "AGENTS.md" }])).toBe(false);
92+
});
93+
94+
it("exposes a default line cap of 150", () => {
95+
expect(DEFAULT_MAX_LINES).toBe(150);
96+
});
97+
98+
it("documents every exemption with a reason", () => {
99+
for (const entry of KNOWN_EXEMPTIONS) {
100+
expect(entry.reason.length).toBeGreaterThan(20);
101+
}
102+
});
103+
});

0 commit comments

Comments
 (0)