Skip to content

fix(constraints): floor-guard reference ignores git config and sees renames - #650

Open
Sunil56224972 wants to merge 3 commits into
addyosmani:mainfrom
Sunil56224972:fix/floor-guard-git-config
Open

Sunil56224972 wants to merge 3 commits into
addyosmani:mainfrom
Sunil56224972:fix/floor-guard-git-config

Conversation

@Sunil56224972

Copy link
Copy Markdown
Contributor

Problem

The floor-guard reference (skills/constraint-driven-development/references/floor-guard.md) parses git diff text, and anything that reshapes that text makes a violation invisible. It then exits 0 ("clean") for:

  • a test file renamed away (git mv app.test.js app.test.js.disabled): rename detection reports a rename, not a deletion
  • CONSTRAINTS.md renamed away, which removes the floor itself
  • color.ui=always or color.diff=always: lines start with ANSI codes, not +/-
  • diff.external (difftastic and similar) or GIT_EXTERNAL_DIFF: no unified diff at all
  • a textconv driver, or *.js -diff in .gitattributes: content hidden or "Binary files differ"

Each of these is common user or repo config, or a one-line change an agent can make, so the guard against weakening the floor can be bypassed without being reported. This is not covered by #599/#600/#614 (untracked files, hunk parsing, running from the top).

Fix

Both diffs run with --unified=0 --no-color --no-ext-diff --no-textconv --no-renames --text --src-prefix=a/ --dst-prefix=b/. GIT_EXTERNAL_DIFF and GIT_DIFF_OPTS are removed from the child environment. A deleted CONSTRAINTS.md is reported as rule-removed.

Proof

PoC, 15 scenarios against the reference script:

violations reported as clean
upstream 9
this PR 0

node --test scripts/floor-guard-reference-test.js: 35 of 35 pass with 7 new hostile-config tests, all 7 of which fail on upstream. Full script suite: 180 of 180 pass. validate-skills and validate-reference-links pass.

PoC script (run: node fg-poc.js skills/constraint-driven-development/references/floor-guard.md)
// Finding 3 PoC: floor-guard reference reports "clean" on real violations.
// Usage: node fg-poc.js <floor-guard.md>
'use strict';
const fs = require('fs'), os = require('os'), path = require('path');
const { spawnSync } = require('child_process');
const md = fs.readFileSync(process.argv[2], 'utf8');
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'fgpoc-'));
const guard = path.join(dir, 'floor-guard.mjs');
fs.writeFileSync(guard, md.match(/```js\n([\s\S]*?)\n```/)[1] + '\n');
const env = { ...process.env, GIT_AUTHOR_NAME: 't', GIT_AUTHOR_EMAIL: 't@t', GIT_COMMITTER_NAME: 't', GIT_COMMITTER_EMAIL: 't@t', GIT_CONFIG_GLOBAL: path.join(os.tmpdir(), 'fgpoc-empty-gitconfig'), GIT_CONFIG_NOSYSTEM: '1' };
fs.writeFileSync(path.join(os.tmpdir(), 'fgpoc-empty-gitconfig'), '[core]\n\tautocrlf = false\n');
const git = (cwd, ...a) => { const r = spawnSync('git', a, { cwd, env, encoding: 'utf8' }); if (r.status) throw new Error(a.join(' ') + r.stderr); };
const C = '# Constraints\n\n## Floor\n- No skipped tests.\n\n| Dimension | Rule |\n|---|---|\n| Coverage | >= 80% |\n';
function repo() {
  const r = fs.mkdtempSync(path.join(dir, 'r-')); git(r, 'init', '-q');
  fs.writeFileSync(path.join(r, 'app.js'), 'module.exports = 1;\n');
  fs.writeFileSync(path.join(r, 'app.test.js'), "const assert = require('assert');\nassert.equal(require('./app'), 1);\n");
  fs.writeFileSync(path.join(r, 'CONSTRAINTS.md'), C);
  git(r, 'add', '.'); git(r, 'commit', '-qm', 'base'); return r;
}
const ts = "// @ts-ignore\nmodule.exports = 1;\n";
const cases = [
  ['control: suppression added, default git config', (r) => fs.writeFileSync(path.join(r, 'app.js'), ts)],
  ['test file renamed away (git mv app.test.js app.test.js.disabled)', (r) => git(r, 'mv', 'app.test.js', 'app.test.js.disabled')],
  ['test file renamed and committed on the branch', (r) => { git(r, 'tag', 'base'); git(r, 'mv', 'app.test.js', 'app.test.disabled'); git(r, 'commit', '-qm', 'x'); return { base: 'base' }; }],
  ['CONSTRAINTS.md renamed away (floor removed)', (r) => git(r, 'mv', 'CONSTRAINTS.md', 'CONSTRAINTS.old')],
  ['suppression added + user has color.ui=always', (r) => { git(r, 'config', 'color.ui', 'always'); fs.writeFileSync(path.join(r, 'app.js'), ts); }],
  ['suppression added + difftastic setup (diff.external)', (r) => { fs.writeFileSync(path.join(r, 'difft.sh'), '#!/bin/sh\necho "$1 --- 1/1 --- JavaScript"\n'); fs.chmodSync(path.join(r, 'difft.sh'), 0o755); git(r, 'config', 'diff.external', path.join(r, 'difft.sh').replace(/\\/g, '/')); git(r, 'update-index', '--add', 'difft.sh'); fs.writeFileSync(path.join(r, 'app.js'), ts); }],
  ['suppression in NEW file + GIT_EXTERNAL_DIFF set', (r) => { fs.writeFileSync(path.join(r, 'new.js'), ts); return { GIT_EXTERNAL_DIFF: 'true' }; }],
  ['suppression added + "*.js -diff" in .gitattributes', (r) => { fs.writeFileSync(path.join(r, '.gitattributes'), '*.js -diff\n'); fs.writeFileSync(path.join(r, 'app.js'), ts); }],
  ['assertion removed + diff.noprefix=true, test under a/', (r) => { fs.mkdirSync(path.join(r, 'a')); fs.renameSync(path.join(r, 'app.test.js'), path.join(r, 'a', 'app.test.js')); git(r, 'add', '-A'); git(r, 'commit', '-qm', 'mv'); git(r, 'config', 'diff.noprefix', 'true'); fs.writeFileSync(path.join(r, 'a', 'app.test.js'), "const assert = require('assert');\n"); return {}; }],
  ['suppression added + diff textconv driver for *.js', (r) => { fs.writeFileSync(path.join(r, '.gitattributes'), '*.js diff=strip\n'); git(r, 'config', 'diff.strip.textconv', 'sed -e s/@ts-ignore//'); fs.writeFileSync(path.join(r, 'app.js'), ts); }],
  ['CONSTRAINTS.md deleted outright', (r) => fs.unlinkSync(path.join(r, 'CONSTRAINTS.md'))],
  ['suppression added + GIT_DIFF_OPTS=--unified=0 (harmless) control', (r) => { fs.writeFileSync(path.join(r, 'app.js'), ts); return { GIT_DIFF_OPTS: '--unified=0' }; }],
  ['suppression added + color.diff=always', (r) => { git(r, 'config', 'color.diff', 'always'); fs.writeFileSync(path.join(r, 'app.js'), ts); }],
  ['assertion removed from test + diff.noprefix=true', (r) => { git(r, 'config', 'diff.noprefix', 'true'); fs.writeFileSync(path.join(r, 'app.test.js'), "const assert = require('assert');\n"); }],
  ['dummy', (r) => { fs.writeFileSync(path.join(r, 'app.js'), ts); git(r, 'config', 'diff.noprefix', 'true'); fs.writeFileSync(path.join(r, 'app.test.js'), "const assert = require('assert');\n"); }],
];
let open = 0;
for (const [name, mut] of cases) {
  const r = repo(); const { base = 'HEAD', ...extra } = mut(r) || {};
  const res = spawnSync(process.execPath, [guard, '--base', base], { cwd: r, encoding: 'utf8', env: { ...env, ...extra } });
  // base for the "committed on the branch" case is the first commit
  const verdict = res.status === 0 ? 'EXIT 0 "clean"  <-- MISSED' : `exit ${res.status} (caught)`;
  if (res.status === 0 && !name.startsWith('control')) open++;
  console.log(`${verdict.padEnd(30)} ${name}`);
}
console.log(`RESULT missed=${open}`);
fs.rmSync(dir, { recursive: true, force: true });

@addyosmani

Copy link
Copy Markdown
Owner

praise: the config hardening is real. On main a violation slips through with color.ui=always, an external diff or textconv driver, or -diff in .gitattributes, and git mv app.test.js app.test.js.disabled passes with default config. Those are all caught now.

issue (blocking): two of the new flags break things main gets right. With --no-renames, renaming a test (app.test.js to app.spec.js, or test/ to tests/) is reported as test-deleted, which breaks the guard's own rule that tightening stays silent. With --text, an untracked 2 MB binary exits 2 because the diff overflows the 1 MB buffer, and a small binary containing the bytes TODO is flagged as unfinished work.

suggestion (non-blocking): keep --no-color, --no-ext-diff, --no-textconv, the prefixes and the env scrub; flag a rename only when a test becomes a non-test; drop or bound --text; and add a test for each case.

@Sunil56224972

Copy link
Copy Markdown
Contributor Author

Addressed in d2d289e. Rename detection now leaves test-to-test moves (including test/ to tests/) silent, while test-to-non-test and CONSTRAINTS.md renames remain violations. I kept --no-color, --no-ext-diff, --no-textconv, the path prefixes, and the environment scrub. --text is retained for -diff attributes but bounded to 8 MiB per Git output and aggregate diff; NUL-marked binary files are excluded from text heuristics. The new regression tests reproduced the three reported failures before the fix; the complete suite now passes 39/39. All CI checks are green.

@Sunil56224972

Copy link
Copy Markdown
Contributor Author

@addyosmani The blocking feedback is addressed in d2d289e, with a regression test for each case you listed (details in my comment above). CI is green; ready for re-review when you have a moment.

@addyosmani

Copy link
Copy Markdown
Owner

praise: the three cases I named are fixed and tested, and the original wins all still hold.

issue (blocking): the same two kinds of regression are still there in other shapes, where main is right and this isn't:

  • --text is bounded rather than removed. An untracked or modified 9 MB binary, or five 2 MB ones, exits 2. And a suppression in a text file with a NUL byte after the first 8000 bytes is now missed.
  • isTest treats test/ and tests/ as tests but not __tests__/ or spec/, so moving app.test.js to tests/app.js or spec/app_spec.js reports test-deleted. Deleting a fixture such as test/fixtures/data.json is reported as a deleted test too.
  • The test/ to tests/ regression test passes against the commit before your fix, so it isn't guarding anything.

suggestion (non-blocking): it may be simpler to let git decide what is binary and skip those files, instead of forcing --text, and to use one test-path matcher for both sides of a rename.

@Sunil56224972

Copy link
Copy Markdown
Contributor Author

Thanks for the concrete review cases. I pushed commit 0ca473c. The normal diff path now lets Git classify binary files instead of forcing --text; the bounded --text fallback is limited to regular files explicitly marked -diff. Test-path recognition now handles tests/ and spec/ on rename targets and excludes fixture paths. I replaced the non-regression test/ to tests/ case with targeted rename and fixture tests, and added coverage for 9 MiB and multiple binary files plus a suppression after a late NUL byte. The full checked-in Node suite passes: 189/189.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants