Files
msd-core/tests/enh-2790-skill-consolidation.test.cjs
Tom Boucher a28dcec981 chore(#597): replace count-based ratchet guards with AST lint + named-set allowlists (#603)
The windows-test-parity ratchet greps test source for fs.rmSync-without-
maxRetries (and six other Windows-portability anti-patterns), failing when an
integer offender COUNT exceeds a frozen baseline (rmSync: 95). A count ratchet
is a Goodhart metric: fixing one offender and adding another keeps the count
constant, so a new defect slips through green. Replace it — and every other
count ratchet in the repo — with a layered, masking-proof design.

Behavioral seam test
- tests/helpers-cleanup.test.cjs proves helpers.cleanup() carries the Windows
  EBUSY retry budget. cleanup() delegates retries to Node's fs.rmSync via
  maxRetries (it owns no loop), so the test asserts the option contract
  (recursive/force/maxRetries>0/retryDelay>0) + real-FS removal + the cwd-guard,
  rather than a loop that does not exist. The EBUSY risk is now tested ONCE at
  the helper, not approximated textually at every call site.

Write-time ESLint rule (AST-accurate, replaces the grep)
- eslint-rules/no-raw-rmsync-in-tests.cjs (error in tests/**/*.test.cjs) bans
  raw fs.rmSync, steering to cleanup(). Catches member, computed (fs['rmSync']),
  destructured and aliased forms; escape hatch is inline
  `// eslint-disable-next-line local/no-raw-rmsync-in-tests -- <reason>` only.
- Migrated 336 raw fs.rmSync teardown calls across ~116 test files to cleanup().
  ~18 genuinely load-bearing sites (mid-test SUT/fault-injection removals,
  error-swallowing or name-colliding local teardown helpers) keep the raw call
  with an inline eslint-disable + reason.

Shared anti-ratchet primitive
- scripts/lib/allowlist-ratchet.cjs:
  - assertWithinAllowlist: fails on NOVEL ids (new offender introduced) AND on
    STALE ids (a known offender was fixed but not pruned) — identity, not count,
    and a ratchet DOWN toward zero.
  - assertTightCeiling: a size/length budget whose ceiling must stay within a
    grace band of the high-water mark, so budgets may only tighten, never creep.

Ratchets converted onto the primitive
- windows-test-parity-guard.test.cjs: rmSync rule deleted (now ESLint-enforced);
  the remaining six patterns moved from integer baselines to named-set
  allowlists with ratchet-down.
- scripts/lint-test-file-count.{cjs,allowlist.json}: per-module integer counts →
  named filename sets (closes the swap-a-file-keep-the-count blind spot); a
  module dropping under cap now FAILS to force pruning its allowlist entry.
- enh-2790 skill-count `<= 63` → named skill allowlist (ratchets toward ~58).

Size budgets hardened (tighten-only)
- agent-size / workflow-size / feat-3039 help-tiered: ceilings lowered to the
  current high-water mark and an assertTightCeiling anti-creep check added per
  tier. Fixed external-contract limits (description ≤100 chars, agent ≤100 KB)
  are intentionally left as-is — they are not grandfathered creeping budgets.

No user-facing behavior change (tests + tooling only); no USER_FACING_PREFIXES
touched, so no changeset fragment is required.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-01 22:43:49 -04:00

432 lines
15 KiB
JavaScript

// allow-test-rule: source-text-is-the-product
// commands/gsd/*.md files ARE what the runtime loads — testing their
// existence/non-existence tests the deployed skill surface contract.
'use strict';
const { describe, test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const path = require('path');
const { assertWithinAllowlist } = require('../scripts/lib/allowlist-ratchet.cjs');
// ---------------------------------------------------------------------------
// Allowlisted set of user-invocable skills (commands/gsd/*.md, ns-* excluded).
// Consolidation target ~58; this set may only SHRINK.
// Adding a new skill requires adding it here with justification.
// Removing a consolidated skill requires pruning it here.
// ---------------------------------------------------------------------------
const KNOWN_SKILLS = new Set([
'add-tests.md',
'ai-integration-phase.md',
'audit-fix.md',
'audit-milestone.md',
'audit-uat.md',
'autonomous.md',
'capture.md',
'cleanup.md',
'code-review.md',
'complete-milestone.md',
'config.md',
'debug.md',
'discuss-phase.md',
'docs-update.md',
'eval-review.md',
'execute-phase.md',
'explore.md',
'extract-learnings.md',
'fast.md',
'forensics.md',
'graphify.md',
'health.md',
'help.md',
'import.md',
'inbox.md',
'ingest-docs.md',
'manager.md',
'map-codebase.md',
'milestone-summary.md',
'mvp-phase.md',
'new-milestone.md',
'new-project.md',
'pause-work.md',
'phase.md',
'plan-phase.md',
'plan-review-convergence.md',
'pr-branch.md',
'profile-user.md',
'progress.md',
'quick.md',
'resume-work.md',
'review-backlog.md',
'review.md',
'secure-phase.md',
'settings.md',
'ship.md',
'sketch.md',
'spec-phase.md',
'spike.md',
'stats.md',
'surface.md',
'thread.md',
'ui-phase.md',
'ui-review.md',
'ultraplan-phase.md',
'undo.md',
'update.md',
'validate-phase.md',
'verify-work.md',
'workspace.md',
'workstreams.md',
]);
const COMMANDS_DIR = path.join(__dirname, '..', 'commands', 'gsd');
/**
* Parse the YAML frontmatter from a skill .md file.
* Returns an object with the frontmatter fields as strings.
* Only handles simple scalar and array values needed by these tests.
*/
function parseFrontmatter(filePath) {
const raw = fs.readFileSync(filePath, 'utf8');
// CRLF-tolerant: Windows checkouts leave \r on every line. lines.indexOf('---', 1)
// would never match because elements would be '---\r' instead of '---'.
const lines = raw.split(/\r?\n/);
if (lines[0].trim() !== '---') return {};
const endIdx = lines.indexOf('---', 1);
if (endIdx === -1) return {};
const fmLines = lines.slice(1, endIdx);
const result = {};
let currentKey = null;
for (const line of fmLines) {
const kvMatch = line.match(/^([a-zA-Z0-9_-]+):\s*(.*)/);
if (kvMatch) {
currentKey = kvMatch[1];
result[currentKey] = kvMatch[2].trim();
} else if (currentKey && line.match(/^\s+-\s+/)) {
// array item — append to existing string value so callers can check membership
const item = line.replace(/^\s+-\s+/, '').trim();
result[currentKey] = result[currentKey] ? [result[currentKey], item].join('\n') : item;
}
}
return result;
}
function skillPath(name) {
return path.join(COMMANDS_DIR, `${name}.md`);
}
// ---------------------------------------------------------------------------
// Group: New consolidated skills exist
// ---------------------------------------------------------------------------
describe('new consolidated skills exist', () => {
test('commands/gsd/capture.md exists', () => {
assert.ok(fs.existsSync(skillPath('capture')), 'capture.md does not exist');
});
test('capture.md has a name: field in frontmatter', () => {
const fm = parseFrontmatter(skillPath('capture'));
assert.ok(fm.name && fm.name.length > 0, 'capture.md missing name: in frontmatter');
});
test('commands/gsd/phase.md exists', () => {
assert.ok(fs.existsSync(skillPath('phase')), 'phase.md does not exist');
});
test('phase.md has a name: field in frontmatter', () => {
const fm = parseFrontmatter(skillPath('phase'));
assert.ok(fm.name && fm.name.length > 0, 'phase.md missing name: in frontmatter');
});
test('commands/gsd/config.md exists', () => {
assert.ok(fs.existsSync(skillPath('config')), 'config.md does not exist');
});
test('config.md has a name: field in frontmatter', () => {
const fm = parseFrontmatter(skillPath('config'));
assert.ok(fm.name && fm.name.length > 0, 'config.md missing name: in frontmatter');
});
test('commands/gsd/workspace.md exists', () => {
assert.ok(fs.existsSync(skillPath('workspace')), 'workspace.md does not exist');
});
test('workspace.md has a name: field in frontmatter', () => {
const fm = parseFrontmatter(skillPath('workspace'));
assert.ok(fm.name && fm.name.length > 0, 'workspace.md missing name: in frontmatter');
});
});
// ---------------------------------------------------------------------------
// Group: Absorbed skills are removed
// ---------------------------------------------------------------------------
describe('absorbed skills are removed', () => {
const absorbed = [
['add-todo', 'absorbed into capture.md'],
['note', 'absorbed into capture.md'],
['add-backlog', 'absorbed into capture.md'],
['plant-seed', 'absorbed into capture.md'],
['check-todos', 'absorbed into capture.md'],
['add-phase', 'absorbed into phase.md'],
['insert-phase', 'absorbed into phase.md'],
['remove-phase', 'absorbed into phase.md'],
['edit-phase', 'absorbed into phase.md'],
['settings-advanced', 'absorbed into config.md'],
['settings-integrations', 'absorbed into config.md'],
['set-profile', 'absorbed into config.md'],
['new-workspace', 'absorbed into workspace.md'],
['list-workspaces', 'absorbed into workspace.md'],
['remove-workspace', 'absorbed into workspace.md'],
['sync-skills', 'absorbed into update.md'],
['reapply-patches', 'absorbed into update.md'],
['sketch-wrap-up', 'absorbed into sketch.md'],
['spike-wrap-up', 'absorbed into spike.md'],
['scan', 'absorbed into map-codebase.md'],
['intel', 'absorbed into map-codebase.md'],
['code-review-fix', 'absorbed into code-review.md'],
['next', 'absorbed into progress.md'],
['do', 'absorbed into progress.md'],
];
for (const [name, reason] of absorbed) {
test(`commands/gsd/${name}.md does NOT exist (${reason})`, () => {
assert.ok(
!fs.existsSync(skillPath(name)),
[
`${name}.md still exists but should have been deleted`,
`(${reason})`,
].join(' '),
);
});
}
});
// ---------------------------------------------------------------------------
// Group: Outright deletions
// ---------------------------------------------------------------------------
describe('outright deleted dead skills are removed', () => {
const deleted = [
'join-discord',
// research-phase → plan-phase --research-phase (PR #3045, already absorbed)
// plan-milestone-gaps → inline in audit-milestone (PR #3038, already absorbed)
// list-phase-assumptions → discuss-phase --assumptions (pending #3131)
// session-report → pause-work --report (pending #3131)
// analyze-dependencies → manager --analyze-deps (pending #3131)
// from-gsd2 → import --from-gsd2 (pending #3131)
];
for (const name of deleted) {
test(`commands/gsd/${name}.md does NOT exist`, () => {
assert.ok(
!fs.existsSync(skillPath(name)),
`${name}.md still exists but should have been deleted (outright dead skill)`,
);
});
}
});
// ---------------------------------------------------------------------------
// Group: #3131 — re-wired workflows absorbed as flags
// ---------------------------------------------------------------------------
describe('#3131 re-wired workflows: standalone command files must not exist', () => {
const rewired = [
['list-phase-assumptions', 'absorbed into discuss-phase.md --assumptions'],
['session-report', 'absorbed into pause-work.md --report'],
['analyze-dependencies', 'absorbed into manager.md --analyze-deps'],
['from-gsd2', 'absorbed into import.md --from-gsd2'],
];
for (const [name, reason] of rewired) {
test(`commands/gsd/${name}.md does NOT exist (${reason})`, () => {
assert.ok(
!fs.existsSync(skillPath(name)),
`${name}.md still exists as a standalone command but should be absorbed (${reason})`,
);
});
}
});
describe('#3131 re-wired workflows: parent command argument-hints advertise the new flags', () => {
test('discuss-phase.md argument-hint contains --assumptions', () => {
const fm = parseFrontmatter(skillPath('discuss-phase'));
assert.ok(
(fm['argument-hint'] || '').includes('--assumptions'),
'discuss-phase.md argument-hint does not contain --assumptions. got: ' + (fm['argument-hint'] || '(none)'),
);
});
test('pause-work.md argument-hint contains --report', () => {
const fm = parseFrontmatter(skillPath('pause-work'));
assert.ok(
(fm['argument-hint'] || '').includes('--report'),
'pause-work.md argument-hint does not contain --report. got: ' + (fm['argument-hint'] || '(none)'),
);
});
test('manager.md argument-hint contains --analyze-deps', () => {
const fm = parseFrontmatter(skillPath('manager'));
assert.ok(
(fm['argument-hint'] || '').includes('--analyze-deps'),
'manager.md argument-hint does not contain --analyze-deps. got: ' + (fm['argument-hint'] || '(none)'),
);
});
test('import.md argument-hint contains --from-gsd2', () => {
const fm = parseFrontmatter(skillPath('import'));
assert.ok(
(fm['argument-hint'] || '').includes('--from-gsd2'),
'import.md argument-hint does not contain --from-gsd2. got: ' + (fm['argument-hint'] || '(none)'),
);
});
});
describe('#3131 re-wired workflows: parent command bodies dispatch to workflow files', () => {
function bodyContains(name, substring) {
const raw = fs.readFileSync(skillPath(name), 'utf8');
return raw.includes(substring);
}
test('discuss-phase.md body references list-phase-assumptions.md', () => {
assert.ok(
bodyContains('discuss-phase', 'list-phase-assumptions.md'),
'discuss-phase.md body does not reference list-phase-assumptions.md — --assumptions flag dispatch is missing',
);
});
test('pause-work.md body references session-report.md', () => {
assert.ok(
bodyContains('pause-work', 'session-report.md'),
'pause-work.md body does not reference session-report.md — --report flag dispatch is missing',
);
});
test('manager.md body references analyze-dependencies.md', () => {
assert.ok(
bodyContains('manager', 'analyze-dependencies.md'),
'manager.md body does not reference analyze-dependencies.md — --analyze-deps flag dispatch is missing',
);
});
test('import.md body references from-gsd2', () => {
assert.ok(
bodyContains('import', 'from-gsd2'),
'import.md body does not reference from-gsd2 — --from-gsd2 flag dispatch is missing',
);
});
});
// ---------------------------------------------------------------------------
// Group: Parent skills updated with new flags
// ---------------------------------------------------------------------------
describe('parent skills updated with new flags in argument-hint', () => {
test('update.md argument-hint contains --sync', () => {
const fm = parseFrontmatter(skillPath('update'));
assert.ok(
(fm['argument-hint'] || '').includes('--sync'),
[
'update.md argument-hint does not contain --sync',
'got: ' + (fm['argument-hint'] || '(none)'),
].join('. '),
);
});
test('update.md argument-hint contains --reapply', () => {
const fm = parseFrontmatter(skillPath('update'));
assert.ok(
(fm['argument-hint'] || '').includes('--reapply'),
[
'update.md argument-hint does not contain --reapply',
'got: ' + (fm['argument-hint'] || '(none)'),
].join('. '),
);
});
test('sketch.md argument-hint contains --wrap-up', () => {
const fm = parseFrontmatter(skillPath('sketch'));
assert.ok(
(fm['argument-hint'] || '').includes('--wrap-up'),
[
'sketch.md argument-hint does not contain --wrap-up',
'got: ' + (fm['argument-hint'] || '(none)'),
].join('. '),
);
});
test('spike.md argument-hint contains --wrap-up', () => {
const fm = parseFrontmatter(skillPath('spike'));
assert.ok(
(fm['argument-hint'] || '').includes('--wrap-up'),
[
'spike.md argument-hint does not contain --wrap-up',
'got: ' + (fm['argument-hint'] || '(none)'),
].join('. '),
);
});
test('map-codebase.md argument-hint contains --fast', () => {
const fm = parseFrontmatter(skillPath('map-codebase'));
assert.ok(
(fm['argument-hint'] || '').includes('--fast'),
[
'map-codebase.md argument-hint does not contain --fast',
'got: ' + (fm['argument-hint'] || '(none)'),
].join('. '),
);
});
test('code-review.md argument-hint contains --fix', () => {
const fm = parseFrontmatter(skillPath('code-review'));
assert.ok(
(fm['argument-hint'] || '').includes('--fix'),
[
'code-review.md argument-hint does not contain --fix',
'got: ' + (fm['argument-hint'] || '(none)'),
].join('. '),
);
});
test('progress.md argument-hint contains --do', () => {
const fm = parseFrontmatter(skillPath('progress'));
assert.ok(
(fm['argument-hint'] || '').includes('--do'),
[
'progress.md argument-hint does not contain --do',
'got: ' + (fm['argument-hint'] || '(none)'),
].join('. '),
);
});
});
// ---------------------------------------------------------------------------
// Group: settings.md is NOT deleted
// ---------------------------------------------------------------------------
describe('settings.md is kept (merged into config entry point or remains standalone)', () => {
test('commands/gsd/settings.md still exists', () => {
assert.ok(
fs.existsSync(skillPath('settings')),
'settings.md was deleted — it should be kept (or renamed to config.md, but not both missing)',
);
});
});
// ---------------------------------------------------------------------------
// Group: Skill set allowlisted (identity-based, consolidating toward ~58)
// ---------------------------------------------------------------------------
describe('skill set', () => {
test('user-invocable skill set is allowlisted (consolidating toward ~58)', () => {
// Exclude `ns-*.md` namespace meta-skills (#2792) from this guard.
// Those are descriptor-only routers selected first by the model and
// are not part of the consolidation surface this test tracks; their
// own contract is enforced by tests/enh-2792-namespace-skills.test.cjs.
const currentBasenames = fs.readdirSync(COMMANDS_DIR)
.filter((f) => f.endsWith('.md') && !f.startsWith('ns-'));
assertWithinAllowlist({
label: 'user-invocable skills (commands/gsd)',
current: currentBasenames,
known: KNOWN_SKILLS,
fail: assert.fail,
pruneHint: 'edit KNOWN_SKILLS in tests/enh-2790-skill-consolidation.test.cjs',
});
});
});