Add an append-only state-log check as a verify gate

The 2026-11-26 pipeline session mangled committed wiki state-log bullets three times with prefix-anchored edits, each silent at edit time. scripts/check-state-log.ts fails when a state-log line present in HEAD is missing or modified in the working tree; additions pass. Wired as npm run wikicheck, on_settle in .pi/verify.json.
This commit is contained in:
alex committed 2026-11-26 20:05:39 +00:00
1 parent 8108628a1c
commit 01cea75342
5 files changed
+169 -3

No files matched your search

+2 -1
View File
@@ -3,6 +3,7 @@
{ "name": "typecheck", "command": "npm run check", "on_settle": true },
{ "name": "tests", "command": "npm test", "on_settle": true },
{ "name": "tlc", "command": "npm run modelcheck" },
{ "name": "smoke", "command": "npm run smoke" }
{ "name": "smoke", "command": "npm run smoke" },
{ "name": "wiki-state-log", "command": "npm run wikicheck", "on_settle": true }
]
}
+2 -1
View File
@@ -13,7 +13,8 @@
"test": "node --test --test-concurrency=1 test/*.test.ts",
"test:manager": "node --test --test-concurrency=1 test/manager.test.ts",
"modelcheck": "java -jar ${TLA2_TOOLS_JAR:-/home/alex/projects/pi-verify/vendor/tla2tools.jar} -cleanup -config spec/SubagentJobs.cfg spec/SubagentJobs.tla",
"smoke": "mkdir -p /tmp/pi-subagent-smoke && PI_CODING_AGENT_DIR=/tmp/pi-subagent-smoke pi -e . --help >/dev/null"
"smoke": "mkdir -p /tmp/pi-subagent-smoke && PI_CODING_AGENT_DIR=/tmp/pi-subagent-smoke pi -e . --help >/dev/null",
"wikicheck": "node scripts/check-state-log.ts"
},
"peerDependencies": {
"@earendil-works/pi-agent-core": "*",
+74
View File
@@ -0,0 +1,74 @@
/**
* Append-only guard for wiki state logs.
*
* The wiki's history discipline (`docs/wiki/*`, per the chief-of-staff wiki
* convention): a committed `## State log` line is never deleted or rewritten —
* changes supersede by adding a new dated entry. Recorded because the
* 2026-11-26 pipeline session mangled committed state-log bullets three times
* with prefix-anchored edits, each silent at edit time and caught only by
* read-back.
*
* Fails with a listing of the damaged lines when a state-log line that exists
* in HEAD is missing or modified in the working tree. Additions pass.
*/
import { execFileSync } from "node:child_process";
import { readdirSync, readFileSync } from "node:fs";
import { join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const WIKI_DIR = "docs/wiki";
/** Lines of a `## State log` section: after its heading, up to the next `## ` heading, blanks dropped. */
export function extractStateLogLines(text: string): string[] {
const lines = text.split("\n");
const start = lines.findIndex((line) => line.trim() === "## State log");
if (start === -1) return [];
const end = lines.findIndex((line, i) => i > start && line.startsWith("## "));
return lines.slice(start + 1, end === -1 ? undefined : end).filter((line) => line.trim() !== "");
}
/** Committed lines that are not present, character-identical, in the current section. */
export function removedCommittedLines(committed: string[], current: string[]): string[] {
const present = new Set(current);
return committed.filter((line) => !present.has(line));
}
function committedText(path: string): string | null {
try {
// stdio: the expected miss for pages not yet in HEAD must not leak git's stderr into gate output.
return execFileSync("git", ["show", `HEAD:${path}`], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] });
} catch {
return null; // Not yet in HEAD (new page) or no git — nothing committed to protect.
}
}
/** Checks every wiki page under `wikiDir` against its HEAD version. Returns the damaged lines per file. */
export function checkStateLogs(wikiDir: string): Map<string, string[]> {
const damaged = new Map<string, string[]>();
for (const entry of readdirSync(wikiDir)) {
if (!entry.endsWith(".md")) continue;
const committed = committedText(join(wikiDir, entry));
if (committed === null) continue;
const removed = removedCommittedLines(extractStateLogLines(committed), extractStateLogLines(readFileSync(join(wikiDir, entry), "utf8")));
if (removed.length > 0) damaged.set(entry, removed);
}
return damaged;
}
export function main(): number {
const damaged = checkStateLogs(WIKI_DIR);
if (damaged.size === 0) {
console.log("State log append-only: OK");
return 0;
}
for (const [file, lines] of damaged) {
console.error(`${join(WIKI_DIR, file)}: committed state-log lines were deleted or modified — append-only; supersede with a new dated entry instead:`);
for (const line of lines) console.error(` ${line}`);
}
return 1;
}
if (process.argv[1] !== undefined && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
process.exitCode = main();
}
+90
View File
@@ -0,0 +1,90 @@
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import test from "node:test";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { fileURLToPath } from "node:url";
import { extractStateLogLines, removedCommittedLines } from "../scripts/check-state-log.ts";
const scriptPath = fileURLToPath(new URL("../scripts/check-state-log.ts", import.meta.url));
test("extractStateLogLines returns the bullets between the State log heading and the next heading", () => {
const page = [
"# Page",
"",
"## State log",
"",
"- 2026-01-01: First entry.",
"- 2026-01-02: Second entry.",
"",
"## Conventions",
"",
"- Convention line.",
].join("\n");
assert.deepEqual(extractStateLogLines(page), ["- 2026-01-01: First entry.", "- 2026-01-02: Second entry."]);
});
test("extractStateLogLines returns nothing without a State log section", () => {
assert.deepEqual(extractStateLogLines("# Page\n\n- Not a state log.\n"), []);
});
test("removedCommittedLines flags modifications and deletions, passes additions and reordering", () => {
const committed = ["- 2026-01-01: First entry.", "- 2026-01-02: Second entry.", "- 2026-01-03: Third entry."];
// Append-only: no damage.
assert.deepEqual(removedCommittedLines(committed, [...committed, "- 2026-01-04: Fourth entry."]), []);
// Reordering within the section leaves every committed line intact.
assert.deepEqual(removedCommittedLines(committed, [...committed].reverse()), []);
// A modified line is missing from the current set.
const modified = [...committed];
modified[1] = "- 2026-01-02: Second entry, edited.";
assert.deepEqual(removedCommittedLines(committed, modified), ["- 2026-01-02: Second entry."]);
// A deleted line is missing.
assert.deepEqual(removedCommittedLines(committed, [committed[0], committed[2]]), ["- 2026-01-02: Second entry."]);
});
test("the CLI passes an append-only edit and fails a committed-line modification", { skip: process.platform === "win32" }, (t) => {
const tmp = mkdtempSync(join(tmpdir(), "state-log-check-"));
t.after(() => rmSync(tmp, { recursive: true, force: true }));
const wiki = join(tmp, "docs", "wiki");
mkdirSync(wiki, { recursive: true });
const page = join(wiki, "goal.md");
const committedPage = "## State log\n\n- 2026-01-01: First entry.\n";
const run = (expectFail: boolean) => {
let result: { status: number; stderr: string } | undefined;
try {
const stdout = execFileSync(process.execPath, [scriptPath], {
cwd: tmp,
encoding: "utf8",
});
result = { status: 0, stderr: stdout };
} catch (error) {
assert.ok(error instanceof Error && "status" in error, "expected an exec failure object");
const e = error as { status?: number; stderr?: string };
result = { status: e.status ?? -1, stderr: e.stderr ?? "" };
}
if (expectFail) {
assert.notEqual(result.status, 0, "expected the check to fail");
assert.match(result.stderr, /2026-01-01: First entry\./);
} else {
assert.equal(result.status, 0);
}
};
const git = (...args: string[]) => execFileSync("git", args, { cwd: tmp, encoding: "utf8" });
writeFileSync(page, committedPage);
git("init", "-q");
git("config", "user.email", "test@example.com");
git("config", "user.name", "Test");
git("add", "-A");
git("commit", "-qm", "wiki");
// Append-only edit passes; a page new to HEAD passes untouched.
writeFileSync(page, committedPage + "- 2026-01-02: Second entry.\n");
writeFileSync(join(wiki, "new-page.md"), "## State log\n\n- 2026-01-01: Fresh page.\n");
run(false);
// Modifying a committed line fails and names the line.
writeFileSync(page, committedPage.replace("First", "Rewritten") + "- 2026-01-02: Second entry.\n");
run(true);
});
+1 -1
View File
@@ -11,5 +11,5 @@
"forceConsistentCasingInFileNames": true,
"allowImportingTsExtensions": true
},
"include": ["src/*.ts", "test/**/*.ts"]
"include": ["src/*.ts", "test/**/*.ts", "scripts/*.ts"]
}