fix(#2316): stop phase complete silently dropping ghost requirement IDs (#2339)

* test(#2316): fail-first tests for ghost REQ-IDs, v-heading over-match, all-orphan gap check

Red phase: 4 of 10 fail against current source (#2316-1 ghost-ID warning,
-3 requirements_updated honesty, -4a v1-heading suppression, -6b all-orphan gap
rows). The other 6 are controls/boundaries that must keep passing — including the
#1159 deferred-heading guard and the literal "TBD" placeholder boundary.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SLufH5sDuqA1AiEGu45cuA

* fix(#2316): stop phase complete silently dropping ghost requirement IDs

phase complete parses a phase's `**Requirements**:` line from ROADMAP.md and
reconciles it into REQUIREMENTS.md. When a cited ID was registered nowhere, every
branch degraded to a no-op and the report was indistinguishable from a run that
applied every update: `requirements_updated: true, warnings: [], has_warnings:
false`, file byte-for-byte unchanged.

Four defects on that path, all long-standing (traced to 2bc295b32d, v1.2.0):

1. The only cross-check compared REQUIREMENTS.md's own body against its own
   Traceability table, so an ID cited by ROADMAP but defined in neither was
   invisible to it. `citedReqIds` is now hoisted out of the `if (reqMatch)` block
   and cross-checked; ghost IDs raise a warning through the existing
   warnings/has_warnings surface that execute-phase.md already prints.
2. `if (reqUpdate.ok)` had no `else`, so a Traceability-row write matching
   nothing was discarded silently. Misses are now recorded.
3. `requirements_updated` was set unconditionally inside `if (existsSync)`,
   reporting "the file was in the transaction" rather than "a write landed". It
   now reflects whether the content actually changed.
4. DEFERRED_HEADING_RE's bare `v\d+` alternative treated an active `## v1
   Requirements` heading as deferred, zeroing the body scan for that section.
   Dropped; `deferred|backlog|future` still match, so #1159's suite still holds.

Also fixes the same class in gap-checker: the `items.length === 0` early return
fired before ghost rows were folded in, so an all-unregistered phase reported
LESS than a partially-unregistered one. It is now gated on ghostReqIds too.

Follows the #2140 precedent in milestone.cts, which hardened this exact class for
`requirements mark-complete`; adapted to this function's single `warnings` array
rather than adding fields execute-phase.md never reads.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SLufH5sDuqA1AiEGu45cuA

* fix(#2316): address review — milestone-aware deferred headings, probe-based ghost check

Independent review found the first pass green but wrong — the tests were built to
miss the shape that breaks.

BLOCKER: dropping the `v\d+` alternative from DEFERRED_HEADING_RE regressed #1159
against GSD's OWN shipped template. gsd-core/templates/requirements.md:35 ships
`## v2 Requirements` with the deferred-ness in the BODY PROSE ("Deferred to future
release"), not the heading — so `v\d+` was the only alternative matching it.
Deleting it made every scaffolded project emit a false traceability warning on
every phase complete, forever. The real problem is that `v\d+` matched BOTH the
active v1 and the deferred v2: deleting it swings from suppress-everything to
suppress-nothing. The heading check is now milestone-aware — a `## v<N>` heading
is deferred only when <N> is not the current milestone's major, resolved through
the existing state.cjs seam. Unresolvable milestone fails safe to the old
always-deferred behavior, since suppressing a warning beats spamming every project.

HIGH: the ghost check re-derived membership from two lossy index sets that
disagreed with the case-insensitive write paths, so it warned "not registered
anywhere" about an ID whose checkbox it had just ticked, and about a
case-mismatched ID whose write landed. It now probes the same two write surfaces
the writes use, mirroring milestone.cts's #2140 precedent (probe the write, don't
re-derive it) — which is what the original brief asked for and the first pass
didn't do.

HIGH: the cited-ID tokenizer split the rest of the line on whitespace, so every
trailing word became a "cited REQ-ID". The shipped roadmap.md template puts an
inline HTML comment on that exact line, so a fully correct run told users to
register `<!--` and `-->` as requirements. Cited IDs are now filtered to the
REQ-ID shape, matching what bodyReqIds/tableReqIds already require.

HIGH: gap-checker had the identical early-return defect 34 lines above the one
fixed — the could-not-parse branch gated ghost rows behind the same unguarded
items.length. One malformed <decisions> line made all ghost rows vanish.

Tests: the #2316-5 guard only covered `## Deferred`/`## Backlog`/`## Future` —
headings that trivially still match — omitting `## v2 Requirements`, the only
shape the change altered. Added the shipped-template fixture plus guards for each
finding above.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SLufH5sDuqA1AiEGu45cuA

* docs(#2316): backfill PR number 2339 into changeset

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SLufH5sDuqA1AiEGu45cuA

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-07-17 06:49:58 -04:00
committed by GitHub
parent ada79bee97
commit 19c1f54a2a
5 changed files with 908 additions and 12 deletions

View File

@@ -434,6 +434,108 @@ describe('Full pipeline: render-hooks plan:post discovers gate, then check dispa
});
});
// ─── Section 3b: issue #2316 (Secondary B) — all-unregistered ghost REQ-IDs ──
//
// runGapAnalysis's `items.length === 0` short-circuit fires BEFORE ghostReqIds
// (phaseReqIds cited by ROADMAP but absent from REQUIREMENTS.md) is folded
// into `rows`. When EVERY cited REQ-ID is a ghost, reqItems is filtered down
// to [] and there is no CONTEXT.md, so items.length === 0 and the ghost rows
// never appear — the phase reports "No requirements or decisions to check."
// instead of the ⚠ Missing from REQUIREMENTS.md rows. A phase with ONE
// unregistered ID mixed with a registered one already surfaces correctly
// (see the mapped-REQ-ID-absent test in Section 2 above) — only the
// ALL-unregistered case is broken.
describe('issue #2316 (Secondary B): all-unregistered phaseReqIds must still emit ghost rows, not the empty-state message', () => {
let tmpDir;
let phaseDir;
beforeEach(() => {
tmpDir = createTempProject();
phaseDir = path.join(tmpDir, '.planning', 'phases', '01-test');
fs.mkdirSync(phaseDir, { recursive: true });
const init = runGsdTools('config-ensure-section', tmpDir);
assert.ok(init.success, `config-ensure-section failed: ${init.error}`);
writeRequirements(path.join(tmpDir, '.planning'), ['KNOWN-01']);
writePlan(phaseDir, '01', '# Plan\n\nImplements KNOWN-01.\n');
});
afterEach(() => cleanup(tmpDir));
test(
'#2316-6a (control, mixed known+ghost): a phase mapping one registered and one ghost REQ-ID already surfaces the ghost row',
() => {
const r = runGapCheck([phaseDir, 'KNOWN-01,ORPHAN-01'], tmpDir);
assert.ok(r.success, `check failed: ${r.error}`);
const out = JSON.parse(r.output);
assert.ok(
out.table.includes('ORPHAN-01') && out.table.includes('Missing from REQUIREMENTS.md'),
`#2316-6a FAILED: mixed control must still list ORPHAN-01 as Missing, got table: ${out.table}`,
);
assert.strictEqual(out.counts.total, 2, '#2316-6a FAILED: total must count both REQ-IDs');
},
);
test(
'#2316-6b: a phase whose EVERY cited REQ-ID is unregistered must still return the ghost rows, not "No requirements or decisions to check."',
() => {
const r = runGapCheck([phaseDir, 'ORPHAN-01,ORPHAN-02'], tmpDir);
assert.ok(r.success, `check failed: ${r.error}`);
const out = JSON.parse(r.output);
assert.ok(
!/No requirements or decisions to check/.test(out.table),
`#2316-6b FAILED: all-unregistered REQ-IDs must not collapse to the empty-state message.\nFull table: ${out.table}`,
);
assert.ok(
out.table.includes('ORPHAN-01') && out.table.includes('ORPHAN-02'),
`#2316-6b FAILED: table must list both ghost REQ-IDs, got: ${out.table}`,
);
assert.ok(
out.table.includes('Missing from REQUIREMENTS.md'),
`#2316-6b FAILED: table must show "Missing from REQUIREMENTS.md" status for the ghost IDs, got: ${out.table}`,
);
assert.strictEqual(out.counts.total, 2, '#2316-6b FAILED: total must count both ghost REQ-IDs, not 0');
assert.strictEqual(out.counts.uncovered, 2, '#2316-6b FAILED: uncovered must count both ghost REQ-IDs, not 0');
assert.strictEqual(out.enabled, true, '#2316-6b FAILED: enabled must still be true');
},
);
test(
'#2334 HIGH 1: all-ghost REQ-IDs + a malformed CONTEXT.md <decisions> line (could-not-parse) must still return the ghost rows, not silently drop to the mismatch-only message',
() => {
// Same "items.length === 0" defect #2316-6b fixed ~34 lines below in
// runGapAnalysis, but in the sibling `ctxExtraction.outcome ===
// 'could-not-parse'` branch a few lines ABOVE it: that branch gated its
// own ghost-row inclusion on the unguarded `items.length > 0`, so a
// malformed <decisions> line made both ghost rows vanish and `total`
// drop 2 -> 0 for the exact same all-ghost phase #2316-6b already
// covers on the happy (non-malformed) CONTEXT.md path.
fs.writeFileSync(
path.join(phaseDir, 'CONTEXT.md'),
'# Phase Context\n\n<decisions>\n## Implementation Decisions\n\n' +
'- **D-01 this line is malformed and has no closing bold/colon\n</decisions>\n',
);
const r = runGapCheck([phaseDir, 'ORPHAN-01,ORPHAN-02'], tmpDir);
assert.ok(r.success, `check failed: ${r.error}`);
const out = JSON.parse(r.output);
assert.ok(
!/No requirements or decisions to check/.test(out.table),
`#2334 HIGH 1 FAILED: all-ghost + could-not-parse must not collapse to the empty-state message.\nFull table: ${out.table}`,
);
assert.ok(
out.table.includes('ORPHAN-01') && out.table.includes('ORPHAN-02'),
`#2334 HIGH 1 FAILED: table must still list both ghost REQ-IDs despite the malformed decisions line, got: ${out.table}`,
);
assert.ok(
/format mismatch/i.test(out.table),
`#2334 HIGH 1 FAILED: the could-not-parse signal must still be present in the table, got: ${out.table}`,
);
assert.strictEqual(out.counts.total, 2, '#2334 HIGH 1 FAILED: total must count both ghost REQ-IDs, not 0');
assert.strictEqual(out.counts.uncovered, 2, '#2334 HIGH 1 FAILED: uncovered must count both ghost REQ-IDs, not 0');
},
);
});
// ─── Section 4: Pure resolver tests against real registry ────────────────────
describe('resolveLoopHooks plan:post — pure function against real registry', () => {

View File

@@ -8743,3 +8743,607 @@ describe('bug-2502: insert-phase must update STATE.md next-phase recommendation'
});
});
}
// ─────────────────────────────────────────────────────────────────────────────
// Regressions: issue #2316 — phase complete ghost REQ-ID / silent-discard defects
//
// ROADMAP.md may cite a REQ-ID in a phase's `**Requirements:**` line that was
// never registered anywhere in REQUIREMENTS.md (neither its body nor its
// Traceability table). The only existing cross-check compares REQUIREMENTS.md's
// own body against its own Traceability table — it never consults the REQ-IDs
// ROADMAP actually cites — so such a "ghost" REQ-ID is invisible: the checkbox/
// Traceability write for it silently matches nothing, and the command reports
// `requirements_updated: true, warnings: []` identically to a run that wrote
// something real.
//
// Contract asserted by #2316-3: `requirements_updated` reflects whether
// REQUIREMENTS.md's content actually changed (before !== after write), not
// merely whether the file existed in the transaction.
// ─────────────────────────────────────────────────────────────────────────────
/**
* Build a 3-phase fixture:
* Phase 01 "known" — cites KNOWN-01, which IS registered in REQUIREMENTS.md
* (body checkbox + Traceability row). CONTROL phase.
* Phase 02 "orphan" — cites ORPHAN-01, ORPHAN-02, which are registered
* NOWHERE in REQUIREMENTS.md. BUG phase.
* Phase 03 "tbd" — carries the literal `**Requirements**: TBD` placeholder
* seeded by phase.add/-batch/-insert. BOUNDARY phase:
* "TBD" itself must never be treated as a ghost REQ-ID.
*/
function build2316GhostReqFixture() {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-2316-ghost-'));
const planDir = path.join(tmpDir, '.planning');
const phasesDir = path.join(planDir, 'phases');
const phase1Dir = path.join(phasesDir, '01-known');
const phase2Dir = path.join(phasesDir, '02-orphan');
const phase3Dir = path.join(phasesDir, '03-tbd');
fs.mkdirSync(phase1Dir, { recursive: true });
fs.mkdirSync(phase2Dir, { recursive: true });
fs.mkdirSync(phase3Dir, { recursive: true });
fs.writeFileSync(path.join(planDir, 'REQUIREMENTS.md'), [
'# Requirements',
'',
'## Functional Requirements',
'',
'- [ ] **KNOWN-01** Known and registered requirement.',
'',
'## Traceability',
'',
'| Requirement | Phase | Status |',
'|-------------|-------|--------|',
'| KNOWN-01 | Phase 01 | Pending |',
'',
].join('\n'));
fs.writeFileSync(path.join(planDir, 'ROADMAP.md'), [
'# Roadmap',
'',
'- [ ] Phase 01: Known',
'- [ ] Phase 02: Orphan',
'- [ ] Phase 03: Tbd',
'',
'### Phase 01: Known',
'**Goal:** Build the known thing',
'**Requirements:** KNOWN-01',
'**Plans:** 1 plans',
'',
'### Phase 02: Orphan',
'**Goal:** Build the orphan thing',
'**Requirements:** ORPHAN-01, ORPHAN-02',
'**Plans:** 1 plans',
'',
'### Phase 03: Tbd',
'**Goal:** Not yet mapped',
'**Requirements**: TBD',
'**Plans:** 1 plans',
'',
'## Progress',
'',
'| Phase | Plans Complete | Status | Completed |',
'|-------|----------------|--------|-----------|',
'| 01. Known | 0/1 | Not started | - |',
'| 02. Orphan | 0/1 | Not started | - |',
'| 03. Tbd | 0/1 | Not started | - |',
'',
].join('\n'));
fs.writeFileSync(path.join(planDir, 'STATE.md'), [
'# State',
'',
'**Current Phase:** 01',
'**Current Phase Name:** Known',
'**Status:** In progress',
'**Completed Phases:** 0',
'**Total Phases:** 3',
'**Progress:** 0%',
'',
].join('\n'));
for (const [dir, n] of [[phase1Dir, '01'], [phase2Dir, '02'], [phase3Dir, '03']]) {
fs.writeFileSync(path.join(dir, `${n}-01-PLAN.md`), '# Plan\nDo the work.\n');
fs.writeFileSync(path.join(dir, `${n}-01-SUMMARY.md`), '# Summary\nDone.\n');
}
return { tmpDir, planDir };
}
describe('issue #2316: phase complete ghost REQ-ID / silent-discard defects', () => {
test(
'#2316-1: ROADMAP-cited REQ-ID absent from REQUIREMENTS.md entirely → phase complete must report non-empty warnings naming the ghost ID(s)',
() => {
const { tmpDir } = build2316GhostReqFixture();
try {
const { output } = runVerifiedPhaseComplete(['phase', 'complete', '2'], tmpDir);
const parsed = JSON.parse(output);
const warnings = parsed.warnings || [];
assert.ok(
warnings.length > 0,
`#2316-1 FAILED: expected non-empty warnings for ghost REQ-IDs ORPHAN-01/ORPHAN-02 ` +
`(cited by ROADMAP Phase 02 but registered nowhere in REQUIREMENTS.md), got warnings:[].\n` +
`Full output: ${output}`,
);
assert.ok(
warnings.some((w) => w.includes('ORPHAN-01')),
`#2316-1 FAILED: warnings must name ORPHAN-01, got: ${JSON.stringify(warnings)}`,
);
assert.ok(
warnings.some((w) => w.includes('ORPHAN-02')),
`#2316-1 FAILED: warnings must name ORPHAN-02, got: ${JSON.stringify(warnings)}`,
);
assert.strictEqual(
parsed.has_warnings, true,
`#2316-1 FAILED: has_warnings must be true when ghost REQ-IDs are cited, got: ${JSON.stringify(parsed)}`,
);
} finally {
cleanup(tmpDir);
}
},
);
test(
'#2316-2 (control): a phase citing only a registered REQ-ID still ticks the checkbox + flips the Traceability row, and does not warn',
() => {
const { tmpDir, planDir } = build2316GhostReqFixture();
try {
const { output } = runVerifiedPhaseComplete(['phase', 'complete', '1'], tmpDir);
const parsed = JSON.parse(output);
assert.deepStrictEqual(
parsed.warnings || [], [],
`#2316-2 FAILED: control phase (KNOWN-01 is registered) must not warn, got: ${JSON.stringify(parsed.warnings)}`,
);
assert.strictEqual(parsed.has_warnings, false, '#2316-2 FAILED: has_warnings must be false for the control phase');
const reqContent = fs.readFileSync(path.join(planDir, 'REQUIREMENTS.md'), 'utf-8');
assert.ok(
/-\s*\[x\]\s*\*\*KNOWN-01\*\*/.test(reqContent),
`#2316-2 FAILED: checkbox for KNOWN-01 must be ticked.\nREQUIREMENTS.md:\n${reqContent}`,
);
assert.ok(
/\|\s*KNOWN-01\s*\|\s*Phase 01\s*\|\s*Complete\s*\|/.test(reqContent),
`#2316-2 FAILED: Traceability row for KNOWN-01 must flip to Complete.\nREQUIREMENTS.md:\n${reqContent}`,
);
} finally {
cleanup(tmpDir);
}
},
);
test(
'#2316-3: requirements_updated must reflect whether REQUIREMENTS.md content actually changed, not merely that the file existed in the transaction',
() => {
// Case A (BUG): nothing written (ghost REQ-IDs only) → requirements_updated must be false.
const ghost = build2316GhostReqFixture();
try {
const { output } = runVerifiedPhaseComplete(['phase', 'complete', '2'], ghost.tmpDir);
const parsed = JSON.parse(output);
assert.strictEqual(
parsed.requirements_updated, false,
`#2316-3a FAILED: requirements_updated must be false when REQUIREMENTS.md content did not change ` +
`(ghost REQ-IDs ORPHAN-01/ORPHAN-02 match nothing to tick or flip).\nFull output: ${output}`,
);
} finally {
cleanup(ghost.tmpDir);
}
// Case B (CONTROL): a real write landed → requirements_updated must remain true.
// Guards against a fix that over-broadens to "always false".
const known = build2316GhostReqFixture();
try {
const { output } = runVerifiedPhaseComplete(['phase', 'complete', '1'], known.tmpDir);
const parsed = JSON.parse(output);
assert.strictEqual(
parsed.requirements_updated, true,
`#2316-3b FAILED: requirements_updated must remain true when a real checkbox/Traceability write landed.\n` +
`Full output: ${output}`,
);
} finally {
cleanup(known.tmpDir);
}
},
);
test(
'#2316-7 (boundary): the literal "**Requirements**: TBD" placeholder seeded by phase.add/-batch/-insert must not produce a ghost-ID warning for the token "TBD"',
() => {
const { tmpDir } = build2316GhostReqFixture();
try {
const { output } = runVerifiedPhaseComplete(['phase', 'complete', '3'], tmpDir);
const parsed = JSON.parse(output);
const warnings = parsed.warnings || [];
assert.ok(
!warnings.some((w) => /\bTBD\b/.test(w)),
`#2316-7 FAILED: the TBD placeholder must not be treated as a ghost REQ-ID, got warnings: ${JSON.stringify(warnings)}`,
);
} finally {
cleanup(tmpDir);
}
},
);
});
/**
* Build a 2-phase fixture where REQUIREMENTS.md has ONE body section (name
* controlled by `heading`) containing a REQ-ID (PRESET-01) that is absent from
* the Traceability table. `heading` is the only variable across callers, so
* this isolates the DEFERRED_HEADING_RE / milestone-aware-version-heading
* behavior precisely.
*
* `milestone` (default `v1.3`, #2334 BLOCKER regression guard) is written
* into STATE.md's `milestone:` frontmatter so a `## v<N> ...` heading resolves
* deterministically against the current milestone's MAJOR version instead of
* silently exercising the "milestone unresolved" fail-safe path. Pass `null`
* to omit the field entirely (fail-safe probe).
*
* `bodyProse`, when given, is inserted as its own paragraph directly under
* `heading` and above the PRESET-01 line — used to pin the shipped
* `templates/requirements.md` shape, where the deferred-ness lives in body
* prose ("Deferred to future release...") rather than in the heading text.
*/
function build2316HeadingFixture(heading, milestone = 'v1.3', bodyProse = null) {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-2316-heading-'));
const planDir = path.join(tmpDir, '.planning');
const phasesDir = path.join(planDir, 'phases');
const phase1Dir = path.join(phasesDir, '01-preset');
const phase2Dir = path.join(phasesDir, '02-next');
fs.mkdirSync(phase1Dir, { recursive: true });
fs.mkdirSync(phase2Dir, { recursive: true });
fs.writeFileSync(path.join(planDir, 'REQUIREMENTS.md'), [
'# Requirements',
'',
heading,
'',
...(bodyProse ? [bodyProse, ''] : []),
'- **PRESET-01** Body requirement missing from traceability table.',
'',
'## Traceability',
'',
'| Requirement | Phase | Status |',
'|-------------|-------|--------|',
'',
].join('\n'));
fs.writeFileSync(path.join(planDir, 'ROADMAP.md'), [
'# Roadmap',
'',
'- [ ] Phase 01: Preset',
'- [ ] Phase 02: Next',
'',
'### Phase 01: Preset',
'**Goal:** Build preset',
'**Requirements:** TBD',
'**Plans:** 1 plans',
'',
'### Phase 02: Next',
'**Goal:** whatever',
'**Requirements:** TBD',
'**Plans:** 1 plans',
'',
'## Progress',
'',
'| Phase | Plans Complete | Status | Completed |',
'|-------|----------------|--------|-----------|',
'| 01. Preset | 0/1 | Not started | - |',
'| 02. Next | 0/1 | Not started | - |',
'',
].join('\n'));
fs.writeFileSync(path.join(planDir, 'STATE.md'), [
...(milestone !== null ? ['---', `milestone: ${milestone}`, '---'] : []),
'# State',
'',
'**Current Phase:** 01',
'**Completed Phases:** 0',
'**Total Phases:** 2',
'**Progress:** 0%',
'',
].join('\n'));
for (const [dir, n] of [[phase1Dir, '01'], [phase2Dir, '02']]) {
fs.writeFileSync(path.join(dir, `${n}-01-PLAN.md`), '# Plan\nDo the work.\n');
fs.writeFileSync(path.join(dir, `${n}-01-SUMMARY.md`), '# Summary\nDone.\n');
}
return tmpDir;
}
describe('issue #2316 (Secondary A): DEFERRED_HEADING_RE\'s bare `v\\d+` alternative over-matches an active heading', () => {
test(
'#2316-4a: "## v1 Requirements" heading with a body REQ-ID missing from the Traceability table must still surface the body-vs-table warning (an active "v1" heading is not deferred)',
() => {
const tmpDir = build2316HeadingFixture('## v1 Requirements');
try {
const { output } = runVerifiedPhaseComplete(['phase', 'complete', '1'], tmpDir);
const parsed = JSON.parse(output);
const warnings = parsed.warnings || [];
assert.ok(
warnings.some((w) => /PRESET-01/.test(w) && /Traceability/i.test(w)),
`#2316-4a FAILED: expected a body-vs-table Traceability warning naming PRESET-01 under the ` +
`"## v1 Requirements" heading (the bare v\\d+ regex alternative wrongly treats it as deferred), ` +
`got warnings: ${JSON.stringify(warnings)}`,
);
} finally {
cleanup(tmpDir);
}
},
);
test(
'#2316-4b (control): "## Functional" heading (identical fixture, non-versioned heading name) must surface the same body-vs-table warning',
() => {
const tmpDir = build2316HeadingFixture('## Functional');
try {
const { output } = runVerifiedPhaseComplete(['phase', 'complete', '1'], tmpDir);
const parsed = JSON.parse(output);
const warnings = parsed.warnings || [];
assert.ok(
warnings.some((w) => /PRESET-01/.test(w) && /Traceability/i.test(w)),
`#2316-4b FAILED: control heading "## Functional" must also warn for PRESET-01, got warnings: ${JSON.stringify(warnings)}`,
);
} finally {
cleanup(tmpDir);
}
},
);
test(
'#2334 BLOCKER: "## v2 Requirements" heading with the shipped templates/requirements.md body prose ("Deferred to future release. Tracked but not in current roadmap."), milestone v1.3 -> the bare v\\d+ heading must be treated as deferred (SAME major-version mismatch: v2 != v1) and NOT surface the body-vs-table warning',
() => {
// Fixture copied VERBATIM from gsd-core/templates/requirements.md:35-37.
const tmpDir = build2316HeadingFixture(
'## v2 Requirements',
'v1.3',
'Deferred to future release. Tracked but not in current roadmap.',
);
try {
const { output } = runVerifiedPhaseComplete(['phase', 'complete', '1'], tmpDir);
const parsed = JSON.parse(output);
const warnings = parsed.warnings || [];
const traceWarnings = warnings.filter((w) => /Traceability/i.test(w));
assert.strictEqual(
traceWarnings.length, 0,
`#2334 BLOCKER FAILED: "## v2 Requirements" under milestone v1.3 (the shipped template's OWN deferred ` +
`section — #1159's original fix target) must stay suppressed, got: ${JSON.stringify(traceWarnings)}`,
);
} finally {
cleanup(tmpDir);
}
},
);
test(
'#2334 BLOCKER (milestone-unresolved fail-safe): "## v1 Requirements" heading with NO milestone field in STATE.md must fall back to the OLD pre-#2316-4a behavior and suppress the warning (a false "deferred" only ever suppresses, never spams)',
() => {
const tmpDir = build2316HeadingFixture('## v1 Requirements', /* milestone */ null);
try {
const { output } = runVerifiedPhaseComplete(['phase', 'complete', '1'], tmpDir);
const parsed = JSON.parse(output);
const warnings = parsed.warnings || [];
const traceWarnings = warnings.filter((w) => /Traceability/i.test(w));
assert.strictEqual(
traceWarnings.length, 0,
`#2334 fail-safe FAILED: with no resolvable milestone, "## v1 Requirements" must fall back to treating ` +
`v\\d+ as deferred (suppress), got: ${JSON.stringify(traceWarnings)}`,
);
} finally {
cleanup(tmpDir);
}
},
);
for (const heading of ['## Deferred', '## Backlog', '## Future']) {
test(
`#2316-5: genuinely deferred heading "${heading}" must still suppress the body-vs-table warning (regression guard — #1159 introduced this filter deliberately; the v\\d+ fix must not remove deferred/backlog/future detection)`,
() => {
const tmpDir = build2316HeadingFixture(heading);
try {
const { output } = runVerifiedPhaseComplete(['phase', 'complete', '1'], tmpDir);
const parsed = JSON.parse(output);
const warnings = parsed.warnings || [];
const traceWarnings = warnings.filter((w) => /Traceability/i.test(w));
assert.strictEqual(
traceWarnings.length, 0,
`#2316-5 FAILED: heading "${heading}" must still be treated as deferred (no warning), ` +
`got: ${JSON.stringify(traceWarnings)}`,
);
} finally {
cleanup(tmpDir);
}
},
);
}
});
// ─────────────────────────────────────────────────────────────────────────────
// #2334 HIGH 2 / HIGH 3: ghost-REQ-ID classification must probe the ACTUAL
// write surfaces (checkbox + Traceability row, case-insensitive — mirroring
// milestone.cts's notFound/hasRow/doneCheckbox classification), not set-diff
// the deferred-filtered/case-sensitive bodyReqIds/tableReqIds indexes; and the
// cited-REQ-ID tokenizer must be filtered to the REQ-ID shape before it ever
// feeds a warning.
// ─────────────────────────────────────────────────────────────────────────────
function build2334GhostSurfaceFixture({ reqBody, roadmapRequirementsLine, traceabilityRows = [] }) {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-2334-ghost-surface-'));
const planDir = path.join(tmpDir, '.planning');
const phasesDir = path.join(planDir, 'phases');
const phase1Dir = path.join(phasesDir, '01-preset');
const phase2Dir = path.join(phasesDir, '02-next');
fs.mkdirSync(phase1Dir, { recursive: true });
fs.mkdirSync(phase2Dir, { recursive: true });
fs.writeFileSync(path.join(planDir, 'REQUIREMENTS.md'), [
'# Requirements',
'',
...reqBody,
'',
'## Traceability',
'',
'| Requirement | Phase | Status |',
'|-------------|-------|--------|',
...traceabilityRows,
'',
].join('\n'));
fs.writeFileSync(path.join(planDir, 'ROADMAP.md'), [
'# Roadmap',
'',
'- [ ] Phase 01: Preset',
'- [ ] Phase 02: Next',
'',
'### Phase 01: Preset',
'**Goal:** Build preset',
`**Requirements**: ${roadmapRequirementsLine}`,
'**Plans:** 1 plans',
'',
'### Phase 02: Next',
'**Goal:** whatever',
'**Requirements:** TBD',
'**Plans:** 1 plans',
'',
'## Progress',
'',
'| Phase | Plans Complete | Status | Completed |',
'|-------|----------------|--------|-----------|',
'| 01. Preset | 0/1 | Not started | - |',
'| 02. Next | 0/1 | Not started | - |',
'',
].join('\n'));
fs.writeFileSync(path.join(planDir, 'STATE.md'), [
'---', 'milestone: v1.3', '---',
'# State',
'',
'**Current Phase:** 01',
'**Completed Phases:** 0',
'**Total Phases:** 2',
'**Progress:** 0%',
'',
].join('\n'));
for (const [dir, n] of [[phase1Dir, '01'], [phase2Dir, '02']]) {
fs.writeFileSync(path.join(dir, `${n}-01-PLAN.md`), '# Plan\nDo the work.\n');
fs.writeFileSync(path.join(dir, `${n}-01-SUMMARY.md`), '# Summary\nDone.\n');
}
return tmpDir;
}
describe('issue #2334: ghost-REQ-ID classification must probe write surfaces, not lossy indexes', () => {
test(
'#2334 HIGH 2a: an ID under "## Deferred" whose checkbox this run TICKS must NOT be reported as a ghost',
() => {
const tmpDir = build2334GhostSurfaceFixture({
reqBody: ['## Deferred', '', '- [ ] **KNOWN-01**: some deferred requirement'],
roadmapRequirementsLine: '[KNOWN-01]',
});
try {
const { output } = runVerifiedPhaseComplete(['phase', 'complete', '1'], tmpDir);
const parsed = JSON.parse(output);
const warnings = parsed.warnings || [];
const reqContent = fs.readFileSync(path.join(tmpDir, '.planning', 'REQUIREMENTS.md'), 'utf-8');
assert.ok(
/-\s*\[x\]\s*\*\*KNOWN-01\*\*/i.test(reqContent),
`#2334 HIGH 2a FAILED (fixture invariant): checkbox for KNOWN-01 must have been ticked.\n${reqContent}`,
);
assert.ok(
!warnings.some((w) => /not registered anywhere/i.test(w) && /KNOWN-01/i.test(w)),
`#2334 HIGH 2a FAILED: KNOWN-01 (checkbox ticked this run, under a Deferred heading) must not be ` +
`reported as a ghost, got: ${JSON.stringify(warnings)}`,
);
} finally {
cleanup(tmpDir);
}
},
);
test(
'#2334 HIGH 2b: a case-mismatched citation ("known-01" vs "**KNOWN-01**") whose write lands must NOT be reported as a ghost',
() => {
const tmpDir = build2334GhostSurfaceFixture({
reqBody: ['## Active', '', '- [ ] **KNOWN-01**: some active requirement'],
roadmapRequirementsLine: '[known-01]',
traceabilityRows: ['| KNOWN-01 | Phase 1 | Pending |'],
});
try {
const { output } = runVerifiedPhaseComplete(['phase', 'complete', '1'], tmpDir);
const parsed = JSON.parse(output);
const warnings = parsed.warnings || [];
const reqContent = fs.readFileSync(path.join(tmpDir, '.planning', 'REQUIREMENTS.md'), 'utf-8');
assert.ok(
/-\s*\[x\]\s*\*\*KNOWN-01\*\*/i.test(reqContent),
`#2334 HIGH 2b FAILED (fixture invariant): checkbox must have been ticked.\n${reqContent}`,
);
assert.ok(
/\|\s*KNOWN-01\s*\|[^|]*\|\s*Complete\s*\|/i.test(reqContent),
`#2334 HIGH 2b FAILED (fixture invariant): Traceability row must have flipped to Complete.\n${reqContent}`,
);
assert.strictEqual(parsed.requirements_updated, true, '#2334 HIGH 2b FAILED: requirements_updated must be true');
assert.ok(
!warnings.some((w) => /not registered anywhere/i.test(w)),
`#2334 HIGH 2b FAILED: a case-mismatched citation whose write landed must not warn as a ghost, ` +
`got: ${JSON.stringify(warnings)}`,
);
} finally {
cleanup(tmpDir);
}
},
);
test(
'#2334 HIGH 3: the shipped templates/roadmap.md inline-HTML-comment Requirements line must not warn to register "<!--", "brackets", "optional", "parser", "handles", "both", "formats", or "-->"',
() => {
const tmpDir = build2334GhostSurfaceFixture({
reqBody: ['## Active', '', '- [ ] **REQ-01**: something'],
roadmapRequirementsLine: '[REQ-01, REQ-02] <!-- brackets optional, parser handles both formats -->',
traceabilityRows: ['| REQ-01 | Phase 1 | Pending |'],
});
try {
const { output } = runVerifiedPhaseComplete(['phase', 'complete', '1'], tmpDir);
const parsed = JSON.parse(output);
const warnings = parsed.warnings || [];
const junkTokens = ['<!--', 'brackets', 'optional', 'parser', 'handles', 'both', 'formats', '-->'];
for (const token of junkTokens) {
assert.ok(
!warnings.some((w) => w.includes(token)),
`#2334 HIGH 3 FAILED: warnings must not name the non-REQ-ID token "${token}" from the inline HTML ` +
`comment, got: ${JSON.stringify(warnings)}`,
);
}
// REQ-02 is a genuine, shape-valid, unregistered REQ-ID — it MUST
// still be flagged (the shape filter must not over-broaden to
// silence real ghosts).
assert.ok(
warnings.some((w) => /not registered anywhere/i.test(w) && /REQ-02/.test(w)),
`#2334 HIGH 3 FAILED: REQ-02 (real, shape-valid, unregistered REQ-ID) must still be flagged as a ` +
`ghost, got: ${JSON.stringify(warnings)}`,
);
} finally {
cleanup(tmpDir);
}
},
);
test(
'#2334 HIGH 3 (boundary): "**Requirements:** None" must not warn to register the literal word "None"',
() => {
const tmpDir = build2334GhostSurfaceFixture({
reqBody: ['## Active'],
roadmapRequirementsLine: 'None',
});
try {
const { output } = runVerifiedPhaseComplete(['phase', 'complete', '1'], tmpDir);
const parsed = JSON.parse(output);
const warnings = parsed.warnings || [];
assert.ok(
!warnings.some((w) => /\bNone\b/.test(w)),
`#2334 HIGH 3 boundary FAILED: "None" must not be treated as a cited REQ-ID, got: ${JSON.stringify(warnings)}`,
);
} finally {
cleanup(tmpDir);
}
},
);
});