feat(phase-plans): extract shared scanPhasePlans helper (k014) (#3308)

* test(phase-plans): red — shared scanPhasePlans contract + parity across call sites (#3262)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* feat(phase-plans): extract shared scanPhasePlans helper (k014) (#3262)

Eliminates four divergent copies of the plan-scan algorithm:
- roadmap.cjs:countPhasePlansAndSummaries (root call site)
- state.cjs:buildStateFrontmatter (1 of 3)
- state.cjs:cmdStateValidate (2 of 3)
- state.cjs:cmdStateSync (3 of 3)
- init.cjs:listPhasePlanFiles / listPhaseSummaryFiles

New bin/lib/plan-scan.cjs exports scanPhasePlans(phaseDir) → {
  planCount, summaryCount, completed, hasNestedPlans,
  planFiles, summaryFiles
}

Divergences resolved:
- roadmap.cjs used a broad isPlanFile (any .md containing PLAN in name,
  matching the extended layout 5-PLAN-01-setup.md); canonical helper
  adopts this wider pattern as the reference implementation.
- state.cjs used a strict endsWith(-PLAN.md) filter, missing extended-
  layout root files; now unified with roadmap.cjs semantics.
- init.cjs listPhasePlanFiles used ^PLAN-\d+ for nested, missing
  the -PLAN-\d+ variant state.cjs also matched; helper includes both.
- pre-bounce exclusion broadened to /.pre-bounce.md$/i (any pre-bounce
  file), not just -PLAN.*\.pre-bounce\.md (roadmap form) or flat
  .pre-bounce.md (state form).
- OUTLINE exclusion broadened to /-OUTLINE\.md$/i to catch both
  flat (-PLAN-OUTLINE.md) and nested (PLAN-01-OUTLINE.md) forms.

Sibling audit: no 5th call site found. phase.cjs:looksLikePlanFile is
a diagnostic probe for non-canonical naming (not a counter) — left
in place per its distinct purpose.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* chore(changelog): add entry for #3262 scanPhasePlans extraction

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* chore(changeset): add changeset fragment for #3262

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* docs(inventory): add plan-scan.cjs row to INVENTORY.md CLI Modules table (#3262)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(3262): update bug-3128 test + INVENTORY counts for plan-scan.cjs

- Update tests/bug-3128-roadmap-plan-count-slug-layout.test.cjs to verify
  that roadmap.cjs delegates to plan-scan.cjs (require check) and that the
  extended filter lives in plan-scan.cjs as isRootPlanFile with /PLAN/i
- Bump docs/INVENTORY.md CLI Modules headline from 46 to 47 (plan-scan.cjs)
- Regenerate docs/INVENTORY-MANIFEST.json to include cli_modules/plan-scan.cjs

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(3262): migrate two missed call sites to scanPhasePlans (k014)

- init.cjs cmdInitExecutePhase: replace inline /-PLAN\.md$/i filter
  with listPhasePlanFiles(path) to honour nested, extended-layout, OUTLINE
  and pre-bounce exclusions (CR finding)
- state.cjs cmdStateUpdateProgress: replace dual /-PLAN\.md$/i and
  /-SUMMARY\.md$/i filters with scanPhasePlans() so the progress-bar
  body field uses the same counts as buildStateFrontmatter frontmatter

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(3262): correct INVENTORY-MANIFEST.json to tracked files only

Remove 3 untracked local entries from cli_modules so the manifest matches
what CI sees (47 tracked .cjs files, not 50 local). Previous regeneration
ran against the local filesystem which included cjs-command-router-adapter.cjs,
state-document.cjs, and workstream-inventory.cjs (all untracked on this branch).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-05-09 11:39:31 -04:00
committed by GitHub
parent 2436da0486
commit 1a49d2fcfc
10 changed files with 495 additions and 143 deletions

View File

@@ -7,6 +7,7 @@ const path = require('path');
const { escapeRegex, loadConfig, getMilestoneInfo, getMilestonePhaseFilter, normalizeMd, output, error, atomicWriteFileSync } = require('./core.cjs');
const { planningDir, planningPaths } = require('./planning-workspace.cjs');
const { extractFrontmatter, reconstructFrontmatter } = require('./frontmatter.cjs');
const scanPhasePlans = require('./plan-scan.cjs');
// Cache disk scan results from buildStateFrontmatter per cwd per process (#1967).
// Avoids re-reading N+1 directories on every state write when the phase structure
@@ -458,9 +459,9 @@ function cmdStateUpdateProgress(cwd, raw) {
.filter(e => e.isDirectory()).map(e => e.name)
.filter(isDirInMilestone);
for (const dir of phaseDirs) {
const files = fs.readdirSync(path.join(phasesDir, dir));
totalPlans += files.filter(f => f.match(/-PLAN\.md$/i)).length;
totalSummaries += files.filter(f => f.match(/-SUMMARY\.md$/i)).length;
const { planCount, summaryCount } = scanPhasePlans(path.join(phasesDir, dir));
totalPlans += planCount;
totalSummaries += summaryCount;
}
}
@@ -872,39 +873,12 @@ function buildStateFrontmatter(bodyContent, cwd) {
let diskTotalSummaries = 0;
let diskCompletedPhases = 0;
// Regex constants mirror roadmap.cjs:countPhasePlansAndSummaries (#3257).
const PLAN_OUTLINE_RE = /-PLAN-OUTLINE\.md$/i;
const PLAN_PRE_BOUNCE_RE = /\.pre-bounce\.md$/i;
for (const dir of phaseDirs) {
const phaseDir = path.join(phasesDir, dir);
const topFiles = fs.readdirSync(phaseDir);
// Canonical flat-layout plan files in phase root.
let plans = topFiles.filter(f =>
(f.endsWith('-PLAN.md') || f === 'PLAN.md') &&
!PLAN_OUTLINE_RE.test(f) && !PLAN_PRE_BOUNCE_RE.test(f)
).length;
let summaries = topFiles.filter(f =>
f.endsWith('-SUMMARY.md') || f === 'SUMMARY.md'
).length;
// Nested layout (post-#3139): phases/<N>/plans/<N>-PLAN-<NN>-<slug>.md
// Mirrors roadmap.cjs:countPhasePlansAndSummaries — do NOT extract here
// (shared helper is tracked as follow-on for k014).
const nestedPlansDir = path.join(phaseDir, 'plans');
if (fs.existsSync(nestedPlansDir)) {
try {
const nested = fs.readdirSync(nestedPlansDir);
plans += nested.filter(f =>
(/^PLAN-\d+.*\.md$/i.test(f) || /-PLAN-\d+.*\.md$/i.test(f)) &&
!PLAN_OUTLINE_RE.test(f) && !PLAN_PRE_BOUNCE_RE.test(f)
).length;
summaries += nested.filter(f =>
/^SUMMARY-\d+.*\.md$/i.test(f) || /-SUMMARY-\d+.*\.md$/i.test(f)
).length;
} catch { /* ignore if plans/ is not a readable directory */ }
}
diskTotalPlans += plans;
diskTotalSummaries += summaries;
if (plans > 0 && summaries >= plans) diskCompletedPhases++;
const { planCount, summaryCount, completed } = scanPhasePlans(phaseDir);
diskTotalPlans += planCount;
diskTotalSummaries += summaryCount;
if (completed) diskCompletedPhases++;
}
cached = {
totalPhases: isDirInMilestone.phaseCount > 0
@@ -1515,33 +1489,7 @@ function cmdStateValidate(cwd, raw) {
const phaseDir = entries.find(e => e.isDirectory() && e.name.startsWith(normalized.replace(/^0+/, '').padStart(2, '0')));
if (phaseDir) {
const phaseDirPath = path.join(phasesDir, phaseDir.name);
const files = fs.readdirSync(phaseDirPath);
// Regex constants mirror roadmap.cjs:countPhasePlansAndSummaries (#3257).
const PLAN_OUTLINE_RE_V = /-PLAN-OUTLINE\.md$/i;
const PLAN_PRE_BOUNCE_RE_V = /\.pre-bounce\.md$/i;
let diskPlans = files.filter(f =>
(f.endsWith('-PLAN.md') || f === 'PLAN.md') &&
!PLAN_OUTLINE_RE_V.test(f) && !PLAN_PRE_BOUNCE_RE_V.test(f)
).length;
let diskSummaries = files.filter(f =>
f.endsWith('-SUMMARY.md') || f === 'SUMMARY.md'
).length;
// Nested layout (post-#3139): phases/<N>/plans/<N>-PLAN-<NN>-<slug>.md
// Mirrors roadmap.cjs:countPhasePlansAndSummaries — do NOT extract here
// (shared helper is tracked as follow-on for k014).
const nestedPlansDirV = path.join(phaseDirPath, 'plans');
if (fs.existsSync(nestedPlansDirV)) {
try {
const nested = fs.readdirSync(nestedPlansDirV);
diskPlans += nested.filter(f =>
(/^PLAN-\d+.*\.md$/i.test(f) || /-PLAN-\d+.*\.md$/i.test(f)) &&
!PLAN_OUTLINE_RE_V.test(f) && !PLAN_PRE_BOUNCE_RE_V.test(f)
).length;
diskSummaries += nested.filter(f =>
/^SUMMARY-\d+.*\.md$/i.test(f) || /-SUMMARY-\d+.*\.md$/i.test(f)
).length;
} catch { /* ignore if plans/ is not a readable directory */ }
}
const { planCount: diskPlans, summaryCount: diskSummaries } = scanPhasePlans(phaseDirPath);
// Check plan count mismatch
if (totalPlansInPhase !== null && diskPlans !== totalPlansInPhase) {
@@ -1550,6 +1498,7 @@ function cmdStateValidate(cwd, raw) {
}
// Check for VERIFICATION.md
const files = fs.readdirSync(phaseDirPath);
const verificationFiles = files.filter(f => f.includes('VERIFICATION') && f.endsWith('.md'));
for (const vf of verificationFiles) {
try {
@@ -1620,40 +1569,12 @@ function cmdStateSync(cwd, options, raw) {
let highestIncompletePhaseplanCount = 0;
let highestIncompletePhaseSummaryCount = 0;
// Regex constants mirror roadmap.cjs:countPhasePlansAndSummaries (#3257).
const PLAN_OUTLINE_RE_S = /-PLAN-OUTLINE\.md$/i;
const PLAN_PRE_BOUNCE_RE_S = /\.pre-bounce\.md$/i;
for (const dir of entries) {
const dirPath = path.join(phasesDir, dir);
const files = fs.readdirSync(dirPath);
// Canonical flat-layout plan files in phase root.
let plans = files.filter(f =>
(f.endsWith('-PLAN.md') || f === 'PLAN.md') &&
!PLAN_OUTLINE_RE_S.test(f) && !PLAN_PRE_BOUNCE_RE_S.test(f)
).length;
let summaries = files.filter(f =>
f.endsWith('-SUMMARY.md') || f === 'SUMMARY.md'
).length;
// Nested layout (post-#3139): phases/<N>/plans/<N>-PLAN-<NN>-<slug>.md
// Mirrors roadmap.cjs:countPhasePlansAndSummaries — do NOT extract here
// (shared helper is tracked as follow-on for k014).
const nestedPlansDirS = path.join(dirPath, 'plans');
if (fs.existsSync(nestedPlansDirS)) {
try {
const nested = fs.readdirSync(nestedPlansDirS);
plans += nested.filter(f =>
(/^PLAN-\d+.*\.md$/i.test(f) || /-PLAN-\d+.*\.md$/i.test(f)) &&
!PLAN_OUTLINE_RE_S.test(f) && !PLAN_PRE_BOUNCE_RE_S.test(f)
).length;
summaries += nested.filter(f =>
/^SUMMARY-\d+.*\.md$/i.test(f) || /-SUMMARY-\d+.*\.md$/i.test(f)
).length;
} catch { /* ignore if plans/ is not a readable directory */ }
}
const { planCount: plans, summaryCount: summaries, completed } = scanPhasePlans(dirPath);
totalDiskPlans += plans;
totalDiskSummaries += summaries;
if (plans > 0 && summaries >= plans) diskCompletedPhases++;
if (completed) diskCompletedPhases++;
// Track the highest phase with incomplete plans (or any plans)
const phaseMatch = dir.match(/^(\d+[A-Z]?(?:\.\d+)*)/i);