Files
msd-core/scripts/lint-test-file-count.cjs
Tom Boucher 79781e68eb enhance(#2401): ground verify-command paths and inherit prior-phase commands (#3678)
* feat(#2401): ground <automated> verify-command paths and inherit prior-phase commands

Adds a deterministic resolvability probe over each PLAN.md <automated> verify
command and surfaces the nearest prior phase's proven commands to the planner
at every context window.

- src/verify-command-grounding.cts: recognizer (not a shell interpreter) that
  grounds a leading cd <literal> chain and npm --prefix <literal>, and reports
  unresolvable rather than guessing. Never executes command text.
- gsd-tools check verify-command-paths <N>: per-phase probe, wired into
  plan-phase.md before the plan-check pass.
- init.plan-phase gains prior_verify_commands, ungated by context_window.
- gsd-plan-checker: new Verify Command Path Resolvability dimension that
  reports the failing target and never prescribes a replacement.

Also fixes first-match-wins prefix bucketing in scripts/lint-test-file-count.cjs
(readdir order is not stable across platforms, so a module whose name extends
another's with a hyphen bucketed differently on Linux than on macOS).

Closes #2401

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(#2401): ground the canonical --prefix form, quoted paths, and absolute cd resets

Independent review found three defects in the recognizer:

- npm --prefix DIR run SCRIPT never reached the script-existence check,
  because the pattern required npm and run to be adjacent. That is the
  form the docs tell planners to prefer, so script_missing never fired
  for it. The prefix flag and its value are now stripped before matching.
- --prefix captured with \S+, so a quoted path containing a space was
  truncated to a stray opening quote and reported as a missing directory
  - a false blocker, worse than the bug this feature fixes. The capture
  is now quote-aware.
- A chained cd whose later segment was absolute concatenated instead of
  resetting, producing a nonsense path and another false blocker. The
  fold now resets on an absolute segment.

Also replaces the bespoke phase-directory regex with the canonical
phase-id helpers. Real phase directories are NN-slug, not phase-N-slug,
so the prior-command harvest matched nothing outside its own fixtures
and the planner-inheritance half of this feature was dead code.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* refactor(#2401): source task blocks from the canonical sectionizer

The module carried its own copy of the <task>-block grammar - a fourth
hand-rolled mirror of the one markdown-sectionizer owns. verify.cts keeps
its copy only because it needs the type= attribute the canonical helper
discards; this module never reads that attribute, so it can share the
owner outright instead of adding a test around a copy.

extractAutomatedCommands now takes task bodies from extractTaggedBlocks
and the out-of-task remainder from stripTaggedBlocks. A task-grammar
parity test pins the attributed task-name set against the canonical
helper across six awkward task shapes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(#2401): extract agent-file overflow to references and repair the property arbitrary

The remote matrix run came back red with 19 failures, four root causes:

- agents/gsd-plan-checker.md and agents/gsd-planner.md both blew the
  49152 agent cap. Their bodies move to gsd-core/references/, leaving
  @-reference stubs, per the documented overflow pattern.
- The new checker dimension invoked gsd_run before the canonical
  preamble that defines it. The call is deleted outright: plan-phase.md
  already runs the probe and hands the result in as {VERIFY_PATHS}, so
  the dimension consumes that rather than re-running anything.
- fc.fullUnicodeString does not exist in fast-check 4.8.0. Replaced with
  fc.string({ unit: 'binary' }), which covers the same 0000-10FFFF range.
- Three runtime-loaded files grew; acknowledged in the existing ack
  fragments that already own those bare filenames, since two ack sources
  may never name the same path.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* test(#2401): regenerate golden install-tree fixtures for the new references

Adding two files under gsd-core/references/ changes what the installer
emits into every runtime's tree, so all 19 golden install-parity
fixtures went stale. Regenerated with npm run gen:install-tree; the
delta is exactly the two new reference paths per runtime, no removals.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* chore(#2401): backfill changeset pr number to 3678

* fix(#2401): treat ~ as a home expansion only at the start of a path

Windows CI caught this on both shards; the Linux-only remote matrix
cannot see it. The dynamic-path refusal rejected ~ anywhere, and a
GitHub Windows runner's tmpdir is an 8.3 short name -
C:\Users\RUNNER~1\AppData\Local\Temp - so a valid absolute Windows
path came back unresolvable/dynamic_path.

This was a production bug, not a test artifact: any Windows user whose
project path carries an 8.3 short name, or any literal ~, silently lost
the probe entirely - every command degrading to unresolvable with no
explanation.

~ is a home expansion only at the start of a path; elsewhere it is an
ordinary literal. The check is now split: $, backtick, *, ? and newline
stay refused anywhere (substitution and globs, and the glob characters
are illegal in Windows path components regardless), while ~ is refused
only leading, tolerating one leading quote since the check runs before
quote stripping.

The prior tests only caught this on Windows because only Windows puts a
~ in tmpdir. Four new tests pin it on every platform via a fixture
directory literally named RUNNER~1.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: sim <sim@local>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-19 15:21:15 -04:00

260 lines
9.6 KiB
JavaScript

#!/usr/bin/env node
/**
* lint-test-file-count.cjs — max 2 test files per production module.
*
* Scans sdk/src/query/, sdk/src/, gsd-core/bin/lib/, bin/ for production
* modules, then counts matching test files in tests/ and sdk/src (recursive). Cap is 2
* (primary + one integration). Over-limit clusters must be in the allowlist with the
* EXACT set of test filenames grandfathered (identity ratchet via allowlist-ratchet.cjs).
* Adding a new test file to a capped module is a novel offender; removing one requires
* pruning the allowlist entry (stale). --json emits structured output.
*
* Verdicts: OK_UNDER_LIMIT | OK_IN_ALLOWLIST | FAIL_EXCEEDS_LIMIT |
* FAIL_NOVEL_FILES | FAIL_STALE_ALLOWLIST
*/
'use strict';
const fs = require('fs');
const path = require('path');
const { assertWithinAllowlist } = require('./lib/allowlist-ratchet.cjs');
const { runMain } = require('./lib/cli-exit.cjs');
const ROOT = path.join(__dirname, '..');
const PROD_DIRS = [
path.join(ROOT, 'sdk', 'src', 'query'),
path.join(ROOT, 'sdk', 'src'),
path.join(ROOT, 'gsd-core', 'bin', 'lib'),
path.join(ROOT, 'bin'),
];
const TEST_DIRS = [
path.join(ROOT, 'tests'),
path.join(ROOT, 'sdk', 'src'),
];
const ALLOWLIST_PATH = path.join(__dirname, 'lint-test-file-count.allowlist.json');
const MAX_FILES = 2;
const Verdict = Object.freeze({
OK_UNDER_LIMIT: 'OK_UNDER_LIMIT',
OK_IN_ALLOWLIST: 'OK_IN_ALLOWLIST',
FAIL_EXCEEDS_LIMIT: 'FAIL_EXCEEDS_LIMIT',
FAIL_NOVEL_FILES: 'FAIL_NOVEL_FILES',
FAIL_STALE_ALLOWLIST: 'FAIL_STALE_ALLOWLIST',
});
function isTestFile(name) {
return name.endsWith('.test.ts') || name.endsWith('.test.cjs');
}
function listFiles(dir, pred) {
try {
return fs.readdirSync(dir, { withFileTypes: true })
.filter(e => e.isFile() && pred(e.name))
.map(e => path.join(dir, e.name));
} catch (_) { return []; }
}
function findTestFilesRecursive(dir) {
const out = [];
let entries;
try { entries = fs.readdirSync(dir, { withFileTypes: true }); }
catch (_) { return out; }
for (const e of entries) {
const full = path.join(dir, e.name);
if (e.isDirectory()) out.push(...findTestFilesRecursive(full));
else if (isTestFile(e.name)) out.push(full);
}
return out;
}
function prodPrefix(filename) {
return filename.replace(/\.(cjs|ts|js)$/, '');
}
// Strip .test.{cjs,ts} and .integration.test.ts, then strip issue stamps.
function testEffectivePrefix(testName) {
const bare = testName
.replace(/\.integration\.test\.(ts|cjs)$/, '')
.replace(/\.test\.(ts|cjs)$/, '');
const m = bare.match(/^(?:feat|bug|enh|fix)-\d+(?:-\d+)*-(.+)$/);
return m ? m[1] : bare;
}
function collectProdPrefixes() {
const map = new Map();
for (const dir of PROD_DIRS) {
for (const f of listFiles(dir, n =>
!isTestFile(n) &&
!/\.(generated|md|json)(\.|$)/.test(n) &&
/\.(ts|cjs|js)$/.test(n)
)) {
const prefix = prodPrefix(path.basename(f));
if (!map.has(prefix)) map.set(prefix, f);
}
}
return map;
}
function collectAllTestFiles() {
const seen = new Set();
const all = [];
for (const dir of TEST_DIRS) {
for (const f of findTestFilesRecursive(dir)) {
if (!seen.has(f)) { seen.add(f); all.push(f); }
}
}
return all;
}
function buildTestMap(prodPrefixes, allTestFiles) {
const map = new Map([...prodPrefixes.keys()].map(p => [p, []]));
for (const tf of allTestFiles) {
const ep = testEffectivePrefix(path.basename(tf));
// fs.readdirSync order (and therefore prodPrefixes' Map insertion order,
// which is built from it in collectProdPrefixes) is NOT stable across
// platforms/filesystems — e.g. ext4 hash order on Linux CI differs from
// HFS+/APFS on macOS. When one module's name is a hyphen-extension of
// another's (e.g. `verify` and `verify-command-grounding`), first-match-
// wins bucketing is therefore platform-dependent. Scan every candidate
// and keep the longest (most specific) matching prefix, so the bucket a
// test file lands in is decided by specificity, never iteration order.
let bestPrefix = null;
for (const prefix of prodPrefixes.keys()) {
if (ep === prefix || ep.startsWith(prefix + '-')) {
if (bestPrefix === null || prefix.length > bestPrefix.length) {
bestPrefix = prefix;
}
}
}
if (bestPrefix !== null) {
map.get(bestPrefix).push(tf);
}
}
return map;
}
function loadAllowlist() {
try { return JSON.parse(fs.readFileSync(ALLOWLIST_PATH, 'utf-8')).modules || {}; }
catch (_) { return {}; }
}
/**
* Evaluate one module's test files against the allowlist.
*
* For modules under the default cap (≤ MAX_FILES): simple count check.
* For modules with an allowlist entry: identity check via assertWithinAllowlist.
* - count now ≤ MAX_FILES → FAIL_STALE_ALLOWLIST (all known files are stale; prune entry)
* - novel files (in current but not in known) → FAIL_NOVEL_FILES
* - stale files (in known but not in current) → FAIL_STALE_ALLOWLIST
* - exact match → OK_IN_ALLOWLIST
* For modules over the cap with no allowlist entry: FAIL_EXCEEDS_LIMIT.
*
* Returns { verdict, prefix, count, knownFiles, novel, stale, files }
*/
function evaluateLint({ prefix, testFiles, allowlist }) {
const count = testFiles.length;
const entry = allowlist[prefix];
const currentNames = testFiles.map(f => path.basename(f));
if (entry !== undefined) {
const knownFiles = Array.isArray(entry.files) ? entry.files : [];
// If the module is now at or under the default cap, the entire allowlist entry is
// stale and must be removed — this is a ratchet-DOWN failure, not a hint.
// All known files are stale (the whole entry can go).
if (count <= MAX_FILES) {
return {
verdict: Verdict.FAIL_STALE_ALLOWLIST,
prefix, count,
knownFiles,
novel: [],
stale: knownFiles.slice().sort(),
files: testFiles,
};
}
// Identity check via assertWithinAllowlist
const messages = [];
const { novel, stale } = assertWithinAllowlist({
label: prefix,
current: currentNames,
known: knownFiles,
fail: (msg) => messages.push(msg),
pruneHint: 'scripts/lint-test-file-count.allowlist.json',
});
if (novel.length > 0) {
return { verdict: Verdict.FAIL_NOVEL_FILES, prefix, count, knownFiles, novel, stale, files: testFiles };
}
if (stale.length > 0) {
return { verdict: Verdict.FAIL_STALE_ALLOWLIST, prefix, count, knownFiles, novel, stale, files: testFiles };
}
return { verdict: Verdict.OK_IN_ALLOWLIST, prefix, count, knownFiles, novel: [], stale: [], files: testFiles };
}
if (count <= MAX_FILES) {
return { verdict: Verdict.OK_UNDER_LIMIT, prefix, count, knownFiles: null, novel: [], stale: [], files: testFiles };
}
return { verdict: Verdict.FAIL_EXCEEDS_LIMIT, prefix, count, knownFiles: null, novel: [], stale: [], files: testFiles };
}
function run() {
const jsonMode = process.argv.includes('--json');
const prodPrefixes = collectProdPrefixes();
const allTestFiles = collectAllTestFiles();
const testMap = buildTestMap(prodPrefixes, allTestFiles);
const allowlist = loadAllowlist();
const results = [];
for (const [prefix, files] of testMap) {
if (files.length === 0) continue;
results.push(evaluateLint({ prefix, testFiles: files, allowlist }));
}
const failures = results.filter(r =>
r.verdict === Verdict.FAIL_EXCEEDS_LIMIT ||
r.verdict === Verdict.FAIL_NOVEL_FILES ||
r.verdict === Verdict.FAIL_STALE_ALLOWLIST);
if (jsonMode) {
console.log(JSON.stringify({ ok: failures.length === 0, results, failures, hints: [] }, null, 2));
return failures.length > 0 ? 1 : 0;
}
if (failures.length === 0) {
const inAllowlist = results.filter(r => r.verdict === Verdict.OK_IN_ALLOWLIST).length;
console.log(`ok lint-test-file-count: ${results.length} module(s) checked, 0 failures` +
(inAllowlist > 0 ? `, ${inAllowlist} allowlisted` : ''));
return 0;
}
process.stderr.write(`\nERROR lint-test-file-count: ${failures.length} module(s) exceed the test-file limit\n\n`);
for (const f of failures) {
if (f.verdict === Verdict.FAIL_EXCEEDS_LIMIT) {
process.stderr.write(` ${f.prefix}: ${f.count} files (limit ${MAX_FILES}) — not in allowlist\n`);
for (const tf of f.files) process.stderr.write(` ${path.relative(ROOT, tf)}\n`);
} else if (f.verdict === Verdict.FAIL_NOVEL_FILES) {
process.stderr.write(` ${f.prefix}: ${f.novel.length} NEW test file(s) not in allowlist\n`);
for (const n of f.novel) process.stderr.write(` + ${n}\n`);
} else if (f.verdict === Verdict.FAIL_STALE_ALLOWLIST) {
if (f.count <= MAX_FILES) {
const staleList = f.stale.join(', ');
process.stderr.write(` "${f.prefix}": now at ${f.count} file(s) (≤ ${MAX_FILES}) — remove its entry from the allowlist (stale: ${staleList})\n`);
} else {
process.stderr.write(` ${f.prefix}: ${f.stale.length} allowlisted file(s) no longer present — prune allowlist\n`);
for (const s of f.stale) process.stderr.write(` - ${s}\n`);
}
}
}
process.stderr.write('\nFix: consolidate test files per module (one primary + one integration).\n');
process.stderr.write('Or update scripts/lint-test-file-count.allowlist.json with PR justification.\n\n');
return 1;
}
module.exports = {
Verdict, evaluateLint, testEffectivePrefix, prodPrefix,
_collectProdPrefixes: collectProdPrefixes,
_collectAllTestFiles: collectAllTestFiles,
_buildTestMap: buildTestMap,
_loadAllowlist: loadAllowlist,
};
if (require.main === module) runMain(run);