Normalize line endings before each commit, test and agent edit

AI Assisted
This commit is contained in:
jparkerweb committed 2026-10-27 18:20:30 -07:00
1 parent 528fcaaa8e
commit 7973bc093b
5 files changed
+213 -1

No files matched your search

+1 -1
View File
@@ -21,7 +21,7 @@
- **`.claude/skills/` is tracked:** a new repo-local skill only needs `git add` (see Architecture → Repo-Local Skills). A correction that is a workflow belongs there as a skill, linked from AGENTS.md, not as a Failure log line.
- **Loop `.gitignore` setup:** `ensureGitignore()` runs at startup in `Controller.run()` as a pre-flight step, not just inside `createTaskCommit()`. This is critical for phase mode where the Node controller doesn't handle commits — without it, `git add -A` would stage spec files.
- **Workflow file character limit:** All `src/plan2code-*.md` files (and the dashboard prompt `src/plan2code.md`) must be ≤ 20,000 characters (about 5k tokens, the usual Agent Skills guidance for a SKILL.md body). A husky pre-commit hook enforces this, and `plan2code-metrics` (`improver.ts`, `applier.ts`) applies the same number to proposed prompt edits, so change them together. History: 11,000 originally (headroom under Windsurf's old 12,000-char limit for platform-specific files, gone since 2.1.0's single-format build), 11,500 in 2.4.0, 20,000 in 2.4.1. The limit is now a prompt-size discipline only: the whole SKILL.md loads on every invocation while references load on demand, so situational detail still belongs in a reference file, and anything with one correct answer belongs in a script (see Skill scripts in the architecture doc).
- **Keep LF line endings:** `.gitattributes` pins `eol=lf` because `validate-char-count.js` counts the characters on disk, and a CRLF checkout adds about one per line to every prompt. Measure with `node scripts/validate-char-count.js` before trusting a near-limit prompt.
- **Keep LF line endings:** `.gitattributes` pins `eol=lf` because `validate-char-count.js` counts the characters on disk, and a CRLF checkout adds about one per line to every prompt. Measure with `node scripts/validate-char-count.js` before trusting a near-limit prompt. Git never writes CRLF here, but tools do (Python writing in text mode on Windows, some editors and generators), so `scripts/fix-line-endings.mjs` puts every text file git sees back to LF: `npm run fix:eol` by hand, `--check` to only list, and it runs by itself as npm's `pretest`, in the husky pre-commit before the char count, and after every Write or Edit through the PostToolUse hook in `.claude/settings.json`. In a Python script that writes repo files, open them with `newline="\n"` (or write bytes) anyway.
- **The pre-commit hook runs only the char-count check:** `.husky/pre-commit` is `node scripts/validate-char-count.js` alone. `skills/` drift, parse errors and test failures are caught only by `npm test`, so run it before committing. Sub-package tests are separate; see [Development Commands](./AGENTS-development-commands.md).
- **`skills/` is generated build output (gitignored):** edit `src/` only. The installer rebuilds `skills/` on every run, so it is always up to date at install time. `npm test` runs `install.js --build-skills` so a broken `SOURCE_PROMPTS` entry or unreadable source fails CI. Never hand-edit `skills/`; `install.js --verify-skills` can diff a stale tree against `src/` when diagnosing.
- **Metrics internal prompts have no char limit:** Files in `plan2code-metrics/src/prompts/` are NOT subject to the 20,000 char limit — only `src/plan2code-*.md` consumer-facing prompts are.
+16
View File
@@ -0,0 +1,16 @@
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit|MultiEdit|NotebookEdit",
"hooks": [
{
"type": "command",
"command": "node \"$CLAUDE_PROJECT_DIR/scripts/fix-line-endings.mjs\" --hook",
"timeout": 30
}
]
}
]
}
}
+1
View File
@@ -1 +1,2 @@
node scripts/fix-line-endings.mjs
node scripts/validate-char-count.js
+109
View File
@@ -0,0 +1,109 @@
// Turn CRLF line endings back into LF in the working tree.
//
// .gitattributes stores every text file as LF and checks it out as LF, so git
// itself never puts a CRLF on disk. Tools do: a Python script writing in text
// mode on Windows, an editor, a generator. A CRLF file still commits clean
// (git normalizes it), but until then it inflates scripts/validate-char-count.js,
// fills `git diff` and `git status` with warnings, and trips tests that read the
// raw bytes. This puts the working tree back the way git would check it out.
//
// node scripts/fix-line-endings.mjs fix every text file git sees
// node scripts/fix-line-endings.mjs --check list them, exit 1 if any
// node scripts/fix-line-endings.mjs --hook fix the one file a Claude Code
// PostToolUse hook names on stdin
// --root <dir> work on another repo (tests)
//
// "Every text file git sees" is what is tracked plus what is untracked but not
// ignored, less anything .gitattributes marks binary or that holds a NUL byte.
// npm test runs the fix first (pretest), so every suite reads LF.
import fs from "node:fs";
import path from "node:path";
import { execFileSync } from "node:child_process";
import { fileURLToPath } from "node:url";
const args = process.argv.slice(2);
const rootAt = args.indexOf("--root");
const ROOT = path.resolve(rootAt >= 0 ? args[rootAt + 1] : path.dirname(path.dirname(fileURLToPath(import.meta.url))));
const mode = args.includes("--hook") ? "hook" : args.includes("--check") ? "check" : "fix";
const git = (gitArgs, input) => execFileSync("git", ["-C", ROOT, ...gitArgs], { input, encoding: "utf8", maxBuffer: 64 * 1024 * 1024 });
// The repo's files that are tracked, or untracked and not ignored, relative to
// ROOT with forward slashes.
function repoFiles() {
return git(["ls-files", "-z", "--cached", "--others", "--exclude-standard"]).split("\0").filter(Boolean);
}
// The files .gitattributes marks binary (or -text), never to be touched.
function binaries(files) {
if (!files.length) return new Set();
const out = git(["check-attr", "-z", "--stdin", "binary", "text"], files.join("\0") + "\0").split("\0");
const skip = new Set();
for (let i = 0; i + 2 < out.length; i += 3) {
const [file, attr, value] = [out[i], out[i + 1], out[i + 2]];
if ((attr === "binary" && value === "set") || (attr === "text" && value === "unset")) skip.add(file);
}
return skip;
}
// Rewrites one file with LF endings if it has any CRLF. Returns whether it did.
function fixFile(abs, write) {
let bytes;
try {
if (!fs.statSync(abs).isFile()) return false;
bytes = fs.readFileSync(abs);
} catch {
return false;
}
if (bytes.includes(0) || !bytes.includes("\r\n")) return false;
if (write) fs.writeFileSync(abs, bytes.toString("latin1").replace(/\r\n/g, "\n"), "latin1");
return true;
}
// The file a PostToolUse hook for Write or Edit names, fixed if it is a text
// file inside the repo that git would check out. Never fails the tool call.
function hook() {
let file = "";
try {
const event = JSON.parse(fs.readFileSync(0, "utf8") || "{}");
file = (event.tool_input && (event.tool_input.file_path || event.tool_input.notebook_path)) || (event.tool_response && event.tool_response.filePath) || "";
} catch {
return;
}
if (!file) return;
const abs = path.resolve(file);
const rel = path.relative(ROOT, abs);
if (!rel || rel.startsWith("..") || path.isAbsolute(rel)) return;
const relGit = rel.split(path.sep).join("/");
try {
if (git(["check-ignore", "-q", "--no-index", relGit]) !== null) return;
} catch {
// check-ignore exits 1 when the path is not ignored: carry on.
}
if (binaries([relGit]).size) return;
if (fixFile(abs, true)) process.stdout.write(`fix-line-endings: ${relGit} had CRLF line endings, now LF\n`);
}
if (mode === "hook") {
try {
hook();
} catch {}
process.exit(0);
}
const files = repoFiles();
const skip = binaries(files);
const found = files.filter((f) => !skip.has(f) && fixFile(path.join(ROOT, f), mode === "fix"));
if (mode === "check") {
if (found.length) {
console.error(`${found.length} file(s) have CRLF line endings (run: npm run fix:eol):`);
for (const f of found) console.error(` ${f}`);
process.exit(1);
}
process.exit(0);
}
if (found.length) {
console.log(`fix-line-endings: ${found.length} file(s) turned from CRLF to LF:`);
for (const f of found) console.log(` ${f}`);
}
+86
View File
@@ -0,0 +1,86 @@
// Tests for scripts/fix-line-endings.mjs: CRLF in the working tree goes back to LF.
// Run with: node --test scripts/test-line-endings.mjs
//
// Each test builds a throwaway git repo with this repo's .gitattributes and
// runs the script against it with --root, so the real tree is never touched.
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { execFileSync, spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
const HERE = path.dirname(fileURLToPath(import.meta.url));
const SCRIPT = path.join(HERE, "fix-line-endings.mjs");
const ATTRS = fs.readFileSync(path.join(HERE, "..", ".gitattributes"));
// A repo holding `files` (path to Buffer or string), none of them committed,
// and a .gitignore that ignores `ignored/`.
function repo(t, files) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-eol-"));
t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
execFileSync("git", ["init", "-q", dir]);
fs.writeFileSync(path.join(dir, ".gitattributes"), ATTRS);
fs.writeFileSync(path.join(dir, ".gitignore"), "ignored/\n");
for (const [rel, body] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(dir, rel)), { recursive: true });
fs.writeFileSync(path.join(dir, rel), body);
}
return dir;
}
const run = (dir, args, input) => spawnSync(process.execPath, [SCRIPT, "--root", dir, ...args], { input, encoding: "utf8" });
const bytes = (dir, rel) => fs.readFileSync(path.join(dir, rel));
// Bytes that look like CRLF text but belong to a picture.
const PNG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x0d, 0x0a]);
test("--check lists every CRLF text file and changes nothing; a plain run fixes them", (t) => {
const dir = repo(t, {
"a.js": "one\r\ntwo\r\n",
"src/b.md": "# Title\r\n\r\nBody\r\n",
"ok.js": "fine\n",
"pic.png": PNG,
"ignored/c.js": "skip\r\n",
});
const check = run(dir, ["--check"]);
assert.equal(check.status, 1);
assert.match(check.stderr, /a\.js/);
assert.match(check.stderr, /src\/b\.md/);
assert.doesNotMatch(check.stderr, /ok\.js|pic\.png|ignored/);
assert.equal(bytes(dir, "a.js").toString(), "one\r\ntwo\r\n", "check only looks");
const fix = run(dir, []);
assert.equal(fix.status, 0);
assert.equal(bytes(dir, "a.js").toString(), "one\ntwo\n");
assert.equal(bytes(dir, "src/b.md").toString(), "# Title\n\nBody\n");
assert.deepEqual(bytes(dir, "pic.png"), PNG, "a binary file is never touched");
assert.equal(bytes(dir, "ignored/c.js").toString(), "skip\r\n", "an ignored file is left alone");
assert.equal(run(dir, ["--check"]).status, 0, "clean afterwards");
});
test("non-ASCII text survives the rewrite byte for byte", (t) => {
const dir = repo(t, { "u.md": "café ★ ✨\r\n" });
run(dir, []);
assert.equal(bytes(dir, "u.md").toString("utf8"), "café ★ ✨\n");
});
test("--hook fixes the file a Write or Edit names, and nothing outside the repo", (t) => {
const dir = repo(t, { "a.js": "x\r\n", "b.js": "y\r\n", "pic.png": PNG });
const out = run(dir, ["--hook"], JSON.stringify({ tool_name: "Write", tool_input: { file_path: path.join(dir, "a.js") } }));
assert.equal(out.status, 0);
assert.match(out.stdout, /a\.js/);
assert.equal(bytes(dir, "a.js").toString(), "x\n");
assert.equal(bytes(dir, "b.js").toString(), "y\r\n", "only the named file");
run(dir, ["--hook"], JSON.stringify({ tool_name: "Edit", tool_input: { file_path: path.join(dir, "pic.png") } }));
assert.deepEqual(bytes(dir, "pic.png"), PNG);
const outside = repo(t, { "z.js": "z\r\n" });
run(dir, ["--hook"], JSON.stringify({ tool_input: { file_path: path.join(outside, "z.js") } }));
assert.equal(bytes(outside, "z.js").toString(), "z\r\n", "a file in another folder is not ours");
for (const input of ["", "not json", "{}"]) assert.equal(run(dir, ["--hook"], input).status, 0, `never fails the tool call (${JSON.stringify(input)})`);
});