fix(state): count nested plans/ files in buildStateFrontmatter (#3257) (#3261)

* test: reproduce nested plans/ undercount in buildStateFrontmatter (#3257)

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

* fix(state): count nested plans/<N>-PLAN-<NN>-<slug>.md in buildStateFrontmatter (#3257)

`buildStateFrontmatter` did a flat `readdirSync` on each phase directory and
missed plan files inside the nested `plans/` subdirectory written by
gsd-plan-phase (post-#3139 / #3115). Every state mutation flowing through
`syncStateFrontmatter` overwrote the curated `progress.*` frontmatter block
with the under-counted disk scan.

The fix adds a `plans/` descent using the same regex shapes as
`roadmap.cjs:countPhasePlansAndSummaries` and `phase.cjs:looksLikePlanFile`
(#2893/#3128). Both the `{N}-PLAN-{NN}-{slug}.md` (agent-emitted) and
`PLAN-{NN}-{slug}.md` (bare-prefix) forms are now matched. Outline files
(`-PLAN-OUTLINE.md`) and pre-bounce files are excluded. Flat-layout repos
are unaffected.

Note: the same algorithm now lives in 4 places (state.cjs, roadmap.cjs,
init.cjs, phase.cjs). Shared-helper extraction per CONTEXT.md k014 is
tracked in the follow-on issue filed with this PR.

Sibling fix to #3115 / #3139 / #3191 — state.cjs was missed in the
post-#3139 migration that updated the other three files.

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

* changeset: pr=3261 for #3257

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

* docs(changelog): add entry for #3257 nested plans/ fix (#3261)

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

* fix(state): broaden PLAN_PRE_BOUNCE_RE to match bare PLAN- prefix (CR)

PLAN_PRE_BOUNCE_RE was /-PLAN.*\.pre-bounce\.md$/i, which missed bare-prefix
files like PLAN-01-foo.pre-bounce.md in the nested plans/ scan — those would
incorrectly count as real plans. Broadened to /\.pre-bounce\.md$/i to exclude
any .pre-bounce.md file regardless of prefix shape.

Adds regression test for this exclusion.

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

* fix(state): extend nested plans scan to cmdStateValidate and cmdStateSync (CR finding)

`buildStateFrontmatter` already received the nested-aware scan in this PR, but
`cmdStateValidate` and `cmdStateSync` still did flat-only `readdirSync` on the
phase root, producing false plan-count drift warnings and under-counted totals
on `phases/<N>/plans/` repos. Extend the identical scan pattern to both sites
(regex byte-identical to the `buildStateFrontmatter` site, k014). Regression
tests added for all three commands.

Closes #3257

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

* test(bug-3257): replace readFileSync+.includes() with structural dry-run idempotency check

The lint-no-source-grep rule flags readFileSync-bound variables used with
text-match methods (.includes, .match, etc.). Replace the afterContent.includes()
check with a structural idempotency assertion: run state sync --verify twice and
confirm the second run still reports a pending change, proving the first dry-run
did not mutate STATE.md.

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

* test(bug-3257): fix progress assertion to use min(plan,phase) formula (#3242)

After rebasing onto main, computeProgressPercent now applies
min(plan_fraction, phase_fraction) per #3242 Bug B. Update the
multi-phase sync test to assert 50% (min(3/5, 1/2)) instead of 60%.

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-08 09:26:35 -04:00
committed by GitHub
parent b37c487325
commit 75cc4fe660
4 changed files with 660 additions and 7 deletions

View File

@@ -815,10 +815,36 @@ 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 files = fs.readdirSync(path.join(phasesDir, dir));
const plans = files.filter(f => f.match(/-PLAN\.md$/i)).length;
const summaries = files.filter(f => f.match(/-SUMMARY\.md$/i)).length;
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++;
@@ -1433,8 +1459,32 @@ function cmdStateValidate(cwd, raw) {
if (phaseDir) {
const phaseDirPath = path.join(phasesDir, phaseDir.name);
const files = fs.readdirSync(phaseDirPath);
const diskPlans = files.filter(f => f.match(/-PLAN\.md$/i)).length;
const diskSummaries = files.filter(f => f.match(/-SUMMARY\.md$/i)).length;
// 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 */ }
}
// Check plan count mismatch
if (totalPlansInPhase !== null && diskPlans !== totalPlansInPhase) {
@@ -1513,11 +1563,37 @@ 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);
const plans = files.filter(f => f.match(/-PLAN\.md$/i)).length;
const summaries = files.filter(f => f.match(/-SUMMARY\.md$/i)).length;
// 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 */ }
}
totalDiskPlans += plans;
totalDiskSummaries += summaries;
if (plans > 0 && summaries >= plans) diskCompletedPhases++;