test: guard the caps of STATE.md (lines, decisions, characters)
All checks were successful
PR Check — Frontend / frontend (pull_request) Successful in 3m34s

state-sync.sh checks the lines and decisions of a direct commit on main and
never sees a PR, while /autopilot workers add release checks to STATE.md in
their own PRs. The guard mirrors the script's counts (wc -l, decision_count),
adds the 20 000-character cap, pins the unaccented headings that the
mawk-based count needs, and checks each count on its boundary.

Refs #508

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
le king fu 2026-11-03 22:07:24 -05:00
commit 1709144031

View file

@ -0,0 +1,170 @@
/**
* Guard: STATE.md, imported by CLAUDE.md (`@STATE.md`) and so loaded in every session of the
* project, stays within the caps of the monorepo convention: 60 lines, 10 entries under
* « Decisions recentes », and 20 000 characters (#508).
*
* What it protects. Until #508 the file had grown to 214 lines and 67 779 characters. Its only
* cap, `state-sync.sh commit` (`~/.claude/skills/fix-issue/scripts/state-sync.sh`), checks the
* lines and the decisions of a direct commit on `main` and never sees a PR, while an
* `/autopilot` worker adds its manual checks to « Vérifications dues à la release suivante » in
* its own PR: five such commits grew the file in two days. This guard runs on those PRs.
*
* The counts, mirrored from `state-sync.sh` (keep them in step with it):
* 1. lines as `wc -l` counts them: the newline characters;
* 2. decisions as its `decision_count` counts them: the lines that start with "- " under a
* heading that matches /^## D[eé]cisions r[eé]centes/, up to the next "## " heading, so an
* indented sub-bullet does not count;
* 3. characters as Claude Code counts a memory file: the length of the string. The script has
* no character cap yet (la-compagnie-maximus#396).
*
* The headings. The canary pins the three section headings, unaccented. The script counts with
* the `awk` of the machine, and mawk, Ubuntu's default (1.3.4 here on 2026-11-03), reads
* `[eé]` byte by byte: under « ## Décisions récentes » it counts no decision at all, and its cap
* of 10 stops working without a word. `decisionCount` below counts that heading too, which is
* stricter than the script; the canary keeps the file on the spelling both read alike. The
* skills that sync the file look these headings up by name as well.
*
* Getting back under a cap. A release check is one line that points to its trace (the
* « Vérifications manuelles dues » section of its PR, a comment, an issue): the procedure stays
* there. Decisions rotate, the ten most recent stay. History leaves the file, git keeps it.
*
* The trap. `check-frontend.yml` ignores `*.md` at the root (`paths-ignore`), so a PR that only
* touches STATE.md does not run this guard, and a direct commit on `main` runs no CI at all. The
* review of such a PR runs it by hand, `npx vitest run src/__integration__/state-guard.test.ts`.
*
* Each cap is a test of its own on STATE.md, passing ones included, after a canary that proves
* the file read is STATE.md. The samples below sit on each boundary: they check that each count
* flags what goes over its cap, and nothing that only reaches it.
*/
import { describe, it, expect } from "vitest";
import { readFileSync } from "fs";
import { resolve } from "path";
const STATE = readFileSync(resolve(import.meta.dirname, "..", "..", "STATE.md"), "utf8");
const TITLE = "# STATE — Simpl'Résultat";
const HEADINGS = ["## Position actuelle", "## Decisions recentes", "## Blockers actifs"];
const LINES = "lines, as wc -l counts them";
const DECISIONS = "entries under « Decisions recentes »";
const CHARACTERS = "characters";
/** Lines as `wc -l` counts them: the newline characters. */
function lineCount(text: string): number {
return text.split("\n").length - 1;
}
/** Entries under « Decisions recentes », as `decision_count` of `state-sync.sh` counts them. */
function decisionCount(text: string): number {
let inSection = false;
let entries = 0;
for (const line of text.split("\n")) {
if (line.startsWith("## ")) {
inSection = /^## D[eé]cisions r[eé]centes/.test(line);
continue;
}
if (inSection && line.startsWith("- ")) entries++;
}
return entries;
}
/** Characters as Claude Code counts a memory file: the length of the string. */
function characterCount(text: string): number {
return text.length;
}
// MAX_LINES and MAX_DECISIONS of `state-sync.sh`, plus the character cap of #508.
const CAPS: [cap: string, count: (text: string) => number, max: number][] = [
[LINES, lineCount, 60],
[DECISIONS, decisionCount, 10],
[CHARACTERS, characterCount, 20_000],
];
const HOW_TO_FIX =
"one line per release check, pointing to the « Vérifications manuelles dues » section of " +
"its PR; the ten most recent decisions only; history stays in git (#508)";
const UNACCENTED =
"unaccented: under « ## Décisions récentes » the mawk of state-sync.sh counts no decision";
/** The caps that a text goes over, none when it conforms. */
function stateOffences(text: string): string[] {
return CAPS.filter(([, count, max]) => count(text) > max).map(([cap]) => cap);
}
/** A conforming STATE.md with `decisions` entries and `extra` lines at the end. */
function sample(decisions: number, extra: string[] = []): string {
const entries = Array.from(
{ length: decisions },
(_, i) => `- 2026-10-${String(10 + i).padStart(2, "0")} : décision ${i + 1}. (ref #${i + 1})`,
);
return [
TITLE,
"",
"> Derniere MAJ : 2026-11-03 (exemple)",
"",
HEADINGS[0],
"",
"- Publiée : v0.17.2.",
"",
HEADINGS[1],
"",
...entries,
"",
HEADINGS[2],
"",
"- Aucun blocker externe.",
...extra,
"",
].join("\n");
}
/** The sample with ten decisions, padded with sub-bullets to exactly `lines` lines. */
function withLines(lines: number): string {
const base = sample(10);
return sample(10, Array.from({ length: lines - lineCount(base) }, () => " - ligne"));
}
/** The sample with ten decisions, its last line lengthened to exactly `characters` characters. */
function withCharacters(characters: number): string {
const base = sample(10, [" - "]);
return base.replace(/ {2}- \n$/, ` - ${"x".repeat(characters - base.length)}\n`);
}
describe("STATE.md guard (#508)", () => {
it("reads STATE.md at the root of the repository, with its three headings unaccented", () => {
const lines = STATE.split("\n");
expect(lines[0]).toBe(TITLE);
for (const heading of HEADINGS) expect(lines, `${heading}, ${UNACCENTED}`).toContain(heading);
});
it.each(CAPS)("STATE.md stays within the cap: %s", (cap, count, max) => {
const message = `${count(STATE)} ${cap} over ${max}: ${HOW_TO_FIX}`;
expect(stateOffences(STATE).includes(cap), message).toBe(false);
});
it("counts lines as wc -l does, a last line without a newline left out", () => {
expect(lineCount("")).toBe(0);
expect(lineCount("a\nb\n")).toBe(2);
expect(lineCount("a\nb")).toBe(1);
});
it("counts the decisions as decision_count does", () => {
expect(decisionCount(sample(10))).toBe(10);
// An indented sub-bullet is part of its entry.
expect(decisionCount(sample(10).replace("décision 1.", "décision 1.\n - détail"))).toBe(10);
// A bullet of another section is not a decision.
expect(decisionCount(sample(10, ["- 2026-10-01 : hors section."]))).toBe(10);
// The accented heading counts here, although the mawk of the script misses it.
expect(decisionCount(sample(10).replace(HEADINGS[1], "## Décisions récentes"))).toBe(10);
});
it.each([
[LINES, withLines(60), withLines(61)],
[DECISIONS, sample(10), sample(11)],
[CHARACTERS, withCharacters(20_000), withCharacters(20_001)],
])("flags a text over the cap (%s), not one at the cap", (cap, atCap, overCap) => {
expect(stateOffences(atCap)).toEqual([]);
expect(stateOffences(overCap)).toEqual([cap]);
});
});