* fix(#2969): deterministic Step 5 verification gate for /gsd-reapply-patches The prior Step 5 "Hunk Verification Gate" was prescribed correctly in the workflow text — but executed laxly by the LLM, which filled in `verified: yes` without actually checking content presence. The reporter observed three distinct files (skills/gsd-discuss-phase/SKILL.md, skills/gsd-autonomous/ SKILL.md, get-shit-done/workflows/new-project.md) where archives contained substantive user-added blocks that did not survive into the merged result, yet the gate reported clean. Move verification from LLM-driven prose into a deterministic Node script the workflow calls. The script can't be shortcut. Changes: - scripts/verify-reapply-patches.cjs (new): pure Node, no external deps. For each file in the patches dir, computes user-added significant lines as the line-set diff between backup and pristine baseline (when available; falls back to "every significant backup line" when no pristine — over-broad but the safe direction for this bug class). Asserts each line appears literally in the merged installed file via String.prototype.includes. Filters trivial lines (length < 12 chars, pure punctuation, decorative comments) so harmless drift doesn't trigger false failures. Exits 0 on pass, 1 on any miss with per-file diagnostic, 2 on usage error. Supports --json for workflow consumption. - get-shit-done/workflows/reapply-patches.md: rewrite Step 5 to call the script and parse its JSON output. The Step 4 Hunk Verification Table remains as advisory Claude-readable summary, but the gate is now the script's exit code. - tests/bug-2969-verify-reapply-patches.test.cjs (new): 6 tests covering (a) pass when every line survives, (b) fail when a line is missing, (c) fail when the merged file is deleted entirely, (d) --json structured report shape, (e) backup-meta.json is correctly skipped as metadata, (f) no-pristine-dir fallback exercises the safe over-broad path. All pass. Out of scope: the manifest-baseline tightening described in #2969 Failure 1 (saveLocalPatches comparing against the wrong baseline so prior silent wipes poison subsequent updates). That's a separate, bigger architectural change involving pristine-content infrastructure; this PR addresses the gate fidelity half so users at least see the diagnostic when content goes missing. Closes #2969 (partial — Failure 2 only) * fix(#2969): preserve #1999 Hunk Verification Table assertions alongside new script gate CI failure on PR #2972 surfaced that tests/reapply-patches.test.cjs (the #1999 contract) asserts Step 5 references: - "Hunk Verification Table" - `verified: no` failure condition - explicit STOP/halt/abort directive - "table absent / missing" halt path My initial Step 5 rewrite for #2969 substituted the deterministic script for the table-based gate entirely, stripping those references. The script is the strictly stronger gate, but the existing #1999 test enforces the table-based safety net as a defense-in-depth contract. Restore both gates as a layered Step 5: - 5a (binding): deterministic verifier script — script gate, exits non-zero on any miss, cannot be shortcut by the LLM - 5b (advisory): Hunk Verification Table review — preserved as redundant safety net for the case where the script has a bug or the pristine baseline is unavailable Both gates must pass. Verified: tests/reapply-patches.test.cjs (5 tests in the #1999 suite) and tests/bug-2969-verify-reapply-patches.test.cjs (6 tests in the #2969 suite) all pass — 21/21 total in this fixture. * fix(#2969): address CodeRabbit findings on workflow + script Five CR findings on PR #2972, all valid; addressed in this commit: 1. (Major) Stderr was merged into VERIFY_OUTPUT via `2>&1`, so any Node warning, deprecation notice, or stack trace would corrupt the JSON parse downstream. Capture stdout only; stderr remains on the controlling terminal for operator visibility. 2. (Major) verifyFile() crashed with EISDIR/EACCES instead of producing a structured diagnostic when the installed path was a directory or unreadable. Wrap statSync/readFileSync in try/catch and emit a per-file fail row; the whole-run gate continues with structured output. Added test case asserting the directory-at-installed-path case fails with `not a regular file` diagnostic instead of crashing. 3. (Minor) PRISTINE_FLAG built as a single string + unquoted expansion would split paths with spaces. Switched to a bash array (VERIFY_ARGS) that preserves whitespace through expansion. 4. (Minor) Fenced code block missing language tag (markdownlint MD040). Added `text` tag to the error message block. 5. (Minor) Usage comment said pristine fallback was "backup-meta lookup" but the actual code path falls back to significant-line checks from backup content. Corrected the comment to match implementation. Verified all 21 tests in tests/reapply-patches.test.cjs (#1999 contract) + tests/bug-2969-verify-reapply-patches.test.cjs (now 7 tests with the new directory case) pass. * test(#2969): structured JSON assertions, no substring matching on script output Replace every assert.match(r.stdout, /pattern/) call with structured assertions on the parsed JSON report from the script's own --json mode. The script's --json contract IS the structured shape we test against — the test author should never depend on the human-readable formatter output, just as no test should depend on substring presence in source. Changes: - All 7 tests now run the verifier with --json (via a runVerifier() helper) and parse the resulting JSON document into { status, report, stderr }. Diagnostic stderr is preserved as a separate channel for debug output but is not used for assertions. - Each previously substring-matched diagnostic ("Failures: 1", "not a regular file", "installed file missing after merge", file path, dropped line) is now a deepEqual / equal / Array.includes against typed report fields: report.failures, report.results[i].status, report.results[i].reason, report.results[i].file, report.results[i].missing[]. - Added an explicit "documented shape" test asserting the JSON output has exactly the keys { file, missing, reason, status } per result — locks the public contract of the --json mode. - DRY'd up fixture reset into a resetFixture() helper since every test starts with a fresh patches/installed/pristine triple. Linter: scripts/lint-no-source-grep.cjs reports 0 violations across 348 test files. Combined run of bug-2969-...test.cjs (7 tests) + reapply-patches.test.cjs (5 tests in the #1999 suite) all pass — 22/22 in the relevant fixture. * fix(#2969): typed REASON enum + raw-text-matching rule shipped repo-wide This commit closes the loop on the no-source-grep discipline: 1. scripts/verify-reapply-patches.cjs: - Frozen REASON enum exposes the diagnostic surface as stable codes: OK_NO_USER_LINES_VS_PRISTINE, OK_NO_SIGNIFICANT_BACKUP_LINES, FAIL_INSTALLED_MISSING, FAIL_INSTALLED_NOT_REGULAR_FILE, FAIL_READ_ERROR, FAIL_USER_LINES_MISSING. - Each result.reason is now a code from this enum, not free text. Tests assert via REASON.X equality, not regex on prose. - REASON exported from module.exports. 2. tests/bug-2969-verify-reapply-patches.test.cjs: - Full rewrite. Every assertion on typed structured fields: report.results[0].status === 'fail', report.results[0].reason === REASON.FAIL_INSTALLED_NOT_REGULAR_FILE, report.results[0].missing.includes(droppedLine) (Array set membership, not String substring). - Locks the REASON enum surface via Object.keys(REASON).sort() deepEqual. - Locks the JSON report shape via Object.keys(report).sort() deepEqual. - Zero regex, zero String#includes, zero startsWith/endsWith on text. 3. CONTRIBUTING.md: - New section "Prohibited: Raw Text Matching on Test Outputs" with concrete BAD/GOOD examples (substring on file content; assert.match on stdout; "structured parser" hiding string ops; regex on free-form reason fields). - The rule statement: "Tests assert on typed structured values. If the code under test produces text, the code under test must also expose a structured intermediate representation, and the test must assert on that IR — never on the rendered text." - Required structured-surface table: file IR, --json mode, frozen enum, fs facts. - "Hiding grep behind a function is still grep" callout — the parser-wrapper anti-pattern. - New `pre-existing-text-matching` exemption category for the 8 grandfathered files. Marked Transitional; new tests cannot use it. 4. scripts/lint-no-source-grep.cjs: - Three new patterns enforced (in addition to the existing .cjs-source readFileSync rule): - assert.match/doesNotMatch on .stdout/.stderr - .stdout/.stderr.<includes|startsWith|endsWith>( - readFileSync(...).<includes|startsWith|endsWith>( - Aggregated violations per file (multiple findings now report together). - Updated diagnostic message references both CONTRIBUTING.md sections. 5. 8 pre-existing tests annotated with `// allow-test-rule: pre-existing-text-matching` so the lint passes on this commit; each carries the prose "Tracked for migration to typed-IR assertions; do not copy this pattern." Files: bug-2649, bug-2687, bug-2796, bug-2838, bug-2943, graphify, hooks-opt-in, security-scan. Verification: lint 0 violations across 348 test files; full suite passes. * fix(#2969): rename exemption category to pending-migration-to-typed-ir + cite tracking issue Per maintainer feedback: 1. "Grandfathered" / "legacy" framing is wrong — both terms imply permanent or condoned exemption. The 8 files are tracked for correction, not exempted. 2. Each annotated file must cite the tracking issue so the migration work is auditable. Changes: - CONTRIBUTING.md: rename exemption category from `pre-existing-text-matching` to `pending-migration-to-typed-ir`. Update prose to "Tracked for correction, not exempted" and require each annotation to cite the open migration issue (e.g. `// allow-test-rule: pending-migration-to-typed-ir [#NNNN]`). - 8 test files: update annotation to cite #2974 (the tracking issue opened for migrating these files to typed-IR assertions).
248 lines
8.7 KiB
JavaScript
Executable File
248 lines
8.7 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
'use strict';
|
|
|
|
/**
|
|
* Deterministic verifier for the /gsd-reapply-patches Step 5 "Hunk Verification
|
|
* Gate". For each backed-up patch file, asserts that the user's added lines
|
|
* (computed from a real diff against the pristine baseline, not from the
|
|
* LLM's prose summary) survive into the merged output.
|
|
*
|
|
* Usage:
|
|
* node scripts/verify-reapply-patches.cjs \
|
|
* --patches-dir <path> \ # gsd-local-patches/
|
|
* --config-dir <path> \ # ~/.claude (or runtime equivalent)
|
|
* [--pristine-dir <path>] # gsd-pristine/; if absent, falls back to
|
|
* # treating every significant backup line as
|
|
* # required (over-broad but safe for #2969:
|
|
* # false-positive halts beat silent successes
|
|
* # on lost content)
|
|
* [--json] # emit JSON report instead of human text
|
|
*
|
|
* Exit codes:
|
|
* 0 — every user-added line is present in the merged file (gate passes)
|
|
* 1 — at least one missing line in at least one file (gate fails)
|
|
* 2 — usage / structural error (e.g. patches dir missing)
|
|
*
|
|
* Bug #2969: the Step 5 gate previously trusted Claude's free-text "verified:
|
|
* yes/no" reporting per hunk. The LLM was filling in `yes` even when content
|
|
* had been silently dropped. Moving the check to a deterministic script is the
|
|
* durability fix.
|
|
*/
|
|
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
const SIGNIFICANT_MIN_CHARS = 12;
|
|
|
|
function parseArgs(argv) {
|
|
const opts = { patchesDir: null, configDir: null, pristineDir: null, json: false };
|
|
for (let i = 0; i < argv.length; i++) {
|
|
const arg = argv[i];
|
|
if (arg === '--patches-dir') opts.patchesDir = argv[++i];
|
|
else if (arg === '--config-dir') opts.configDir = argv[++i];
|
|
else if (arg === '--pristine-dir') opts.pristineDir = argv[++i];
|
|
else if (arg === '--json') opts.json = true;
|
|
else if (arg === '--help' || arg === '-h') {
|
|
process.stdout.write(
|
|
'usage: verify-reapply-patches.cjs --patches-dir <path> --config-dir <path> [--pristine-dir <path>] [--json]\n',
|
|
);
|
|
process.exit(0);
|
|
} else {
|
|
process.stderr.write(`unknown argument: ${arg}\n`);
|
|
process.exit(2);
|
|
}
|
|
}
|
|
return opts;
|
|
}
|
|
|
|
function isSignificantLine(line) {
|
|
const trimmed = line.trim();
|
|
if (trimmed.length < SIGNIFICANT_MIN_CHARS) return false;
|
|
// Pure punctuation / closing brackets carry too little structural info to
|
|
// reliably distinguish a survived hunk from incidental similarity.
|
|
if (/^[\s})\];,]+$/.test(trimmed)) return false;
|
|
// Generic decorative comments like `// ----` similarly fail the test.
|
|
if (/^[\s\-=#*/]+$/.test(trimmed)) return false;
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Walk a directory, returning every file's path relative to the root.
|
|
*/
|
|
function walk(rootDir, relPrefix = '') {
|
|
const out = [];
|
|
if (!fs.existsSync(rootDir)) return out;
|
|
for (const entry of fs.readdirSync(rootDir, { withFileTypes: true })) {
|
|
const rel = relPrefix ? path.join(relPrefix, entry.name) : entry.name;
|
|
const abs = path.join(rootDir, entry.name);
|
|
if (entry.isDirectory()) {
|
|
out.push(...walk(abs, rel));
|
|
} else if (entry.isFile()) {
|
|
out.push(rel);
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* Compute the set of "user-added" lines: lines present in the backup but
|
|
* absent from the pristine baseline. If no pristine is provided, falls back
|
|
* to using every significant line in the backup (over-broad but safe — favours
|
|
* false-positive failures over silent successes, which is the right side to
|
|
* err on for #2969).
|
|
*/
|
|
function computeUserAddedLines(backupContent, pristineContent) {
|
|
const backupLines = backupContent.split(/\r?\n/);
|
|
if (!pristineContent) {
|
|
return backupLines.filter(isSignificantLine);
|
|
}
|
|
const pristineSet = new Set(pristineContent.split(/\r?\n/));
|
|
return backupLines.filter((line) => isSignificantLine(line) && !pristineSet.has(line));
|
|
}
|
|
|
|
/**
|
|
* Stable reason codes for the per-file result. Tests assert via
|
|
* `assert.equal(result.reason, REASON.X)` rather than regex-matching prose,
|
|
* so the diagnostic surface is a typed enum, not free text.
|
|
*
|
|
* Adding a new reason requires updating the REASON map AND the tests'
|
|
* shape assertion that locks the documented set of codes.
|
|
*/
|
|
const REASON = Object.freeze({
|
|
OK_NO_USER_LINES_VS_PRISTINE: 'ok_no_user_lines_vs_pristine',
|
|
OK_NO_SIGNIFICANT_BACKUP_LINES: 'ok_no_significant_backup_lines',
|
|
FAIL_INSTALLED_MISSING: 'fail_installed_missing',
|
|
FAIL_INSTALLED_NOT_REGULAR_FILE: 'fail_installed_not_regular_file',
|
|
FAIL_READ_ERROR: 'fail_read_error',
|
|
FAIL_USER_LINES_MISSING: 'fail_user_lines_missing',
|
|
});
|
|
|
|
function verifyFile({ relPath, patchesDir, configDir, pristineDir }) {
|
|
const backupPath = path.join(patchesDir, relPath);
|
|
const installedPath = path.join(configDir, relPath);
|
|
const result = { file: relPath, status: 'ok', missing: [], reason: null };
|
|
|
|
if (!fs.existsSync(backupPath) || !fs.statSync(backupPath).isFile()) {
|
|
return result; // walked entry no longer exists — non-fatal
|
|
}
|
|
|
|
// Installed path checks: must exist, must be a regular file, must be
|
|
// readable. Anything else is a fail-with-diagnostic, not a crash that
|
|
// aborts the whole gate run and drops structured output.
|
|
let installedStat;
|
|
try {
|
|
installedStat = fs.statSync(installedPath);
|
|
} catch {
|
|
result.status = 'fail';
|
|
result.reason = REASON.FAIL_INSTALLED_MISSING;
|
|
return result;
|
|
}
|
|
if (!installedStat.isFile()) {
|
|
result.status = 'fail';
|
|
result.reason = REASON.FAIL_INSTALLED_NOT_REGULAR_FILE;
|
|
return result;
|
|
}
|
|
|
|
let backupContent;
|
|
let installedContent;
|
|
try {
|
|
backupContent = fs.readFileSync(backupPath, 'utf8');
|
|
installedContent = fs.readFileSync(installedPath, 'utf8');
|
|
} catch {
|
|
result.status = 'fail';
|
|
result.reason = REASON.FAIL_READ_ERROR;
|
|
return result;
|
|
}
|
|
|
|
let pristineContent = null;
|
|
if (pristineDir) {
|
|
const pristinePath = path.join(pristineDir, relPath);
|
|
try {
|
|
const stat = fs.statSync(pristinePath);
|
|
if (stat.isFile()) {
|
|
pristineContent = fs.readFileSync(pristinePath, 'utf8');
|
|
}
|
|
} catch {
|
|
// Pristine missing or unreadable — fall through to over-broad mode.
|
|
}
|
|
}
|
|
|
|
const userAdded = computeUserAddedLines(backupContent, pristineContent);
|
|
if (userAdded.length === 0) {
|
|
// Backup and pristine match exactly (or no significant content) — nothing
|
|
// to verify but also nothing to lose. Report as ok with diagnostic code.
|
|
result.reason = pristineContent
|
|
? REASON.OK_NO_USER_LINES_VS_PRISTINE
|
|
: REASON.OK_NO_SIGNIFICANT_BACKUP_LINES;
|
|
return result;
|
|
}
|
|
|
|
for (const line of userAdded) {
|
|
if (!installedContent.includes(line)) {
|
|
result.missing.push(line.trim());
|
|
}
|
|
}
|
|
if (result.missing.length > 0) {
|
|
result.status = 'fail';
|
|
result.reason = REASON.FAIL_USER_LINES_MISSING;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function main() {
|
|
const opts = parseArgs(process.argv.slice(2));
|
|
if (!opts.patchesDir || !opts.configDir) {
|
|
process.stderr.write('--patches-dir and --config-dir are required\n');
|
|
process.exit(2);
|
|
}
|
|
if (!fs.existsSync(opts.patchesDir)) {
|
|
process.stderr.write(`patches dir not found: ${opts.patchesDir}\n`);
|
|
process.exit(2);
|
|
}
|
|
if (!fs.existsSync(opts.configDir)) {
|
|
process.stderr.write(`config dir not found: ${opts.configDir}\n`);
|
|
process.exit(2);
|
|
}
|
|
|
|
const files = walk(opts.patchesDir).filter((f) => !f.endsWith('backup-meta.json'));
|
|
const results = files.map((relPath) =>
|
|
verifyFile({
|
|
relPath,
|
|
patchesDir: opts.patchesDir,
|
|
configDir: opts.configDir,
|
|
pristineDir: opts.pristineDir,
|
|
}),
|
|
);
|
|
|
|
const failures = results.filter((r) => r.status === 'fail');
|
|
|
|
if (opts.json) {
|
|
process.stdout.write(JSON.stringify({ checked: results.length, failures: failures.length, results }, null, 2) + '\n');
|
|
} else {
|
|
process.stdout.write(`# Hunk Verification Gate (#2969)\n\n`);
|
|
process.stdout.write(`Checked: ${results.length} file(s)\n`);
|
|
process.stdout.write(`Failures: ${failures.length}\n\n`);
|
|
if (failures.length > 0) {
|
|
process.stdout.write(`## Files with missing user-added content\n\n`);
|
|
for (const r of failures) {
|
|
process.stdout.write(`- ${r.file}\n`);
|
|
if (r.reason) process.stdout.write(` reason: ${r.reason}\n`);
|
|
for (const line of r.missing.slice(0, 5)) {
|
|
process.stdout.write(` missing: ${line}\n`);
|
|
}
|
|
if (r.missing.length > 5) {
|
|
process.stdout.write(` …and ${r.missing.length - 5} more line(s)\n`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
process.exit(failures.length > 0 ? 1 : 0);
|
|
}
|
|
|
|
if (require.main === module) {
|
|
main();
|
|
}
|
|
|
|
module.exports = { computeUserAddedLines, isSignificantLine, verifyFile, walk, REASON };
|