fix(milestone): scope stats, accomplishments, and archive to current milestone phases

cmdMilestoneComplete previously iterated ALL phase directories in
.planning/phases/, including phases from prior milestones that remain on
disk. This produced inflated stats, stale accomplishments from old
summaries, and --archive-phases moving directories that belong to
earlier milestones.

This fix parses ROADMAP.md to extract phase numbers for the current
milestone, builds a normalized Set for O(1) lookup, and filters all
phase directory operations through an isDirInMilestone() helper.

The helper handles edge cases: leading zeros (01 -> 1), letter suffixes
(3A), decimal phases (3.1), large numbers (456), and excludes
non-numeric directories (notes/, misc/).

Adds 5 tests covering scoped stats, scoped archive, prefix collision
guard, non-numeric directory exclusion, and large phase numbers.

Complements upstream PRs #756 and #783 which fix getMilestoneInfo()
milestone detection — this fix addresses milestone completion scoping.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Tibsfox
2026-02-27 03:45:42 -08:00
parent f9fc2a3f33
commit 367149d0b2
2 changed files with 224 additions and 2 deletions

View File

@@ -92,7 +92,44 @@ function cmdMilestoneComplete(cwd, version, options, raw) {
// Ensure archive directory exists
fs.mkdirSync(archiveDir, { recursive: true });
// Gather stats from phases
// Extract milestone phase numbers from ROADMAP.md to scope stats.
// Only phases listed in the current ROADMAP are counted — phases from
// prior milestones that remain on disk are excluded.
//
// Related upstream PRs (getMilestoneInfo, not milestone complete):
// #756 — fix(core): detect current milestone correctly in getMilestoneInfo
// #783 — fix: getMilestoneInfo() returns wrong version after completion
// Those PRs fix *which* milestone is detected; this fix scopes *stats*
// and *accomplishments* to only the phases belonging to that milestone.
const milestonePhaseNums = new Set();
if (fs.existsSync(roadmapPath)) {
try {
const roadmapContent = fs.readFileSync(roadmapPath, 'utf-8');
const phasePattern = /#{2,4}\s*Phase\s+(\d+[A-Z]?(?:\.\d+)*)\s*:/gi;
let phaseMatch;
while ((phaseMatch = phasePattern.exec(roadmapContent)) !== null) {
milestonePhaseNums.add(phaseMatch[1]);
}
} catch {}
}
// Pre-normalize phase numbers for O(1) lookup — strip leading zeros
// and lowercase for case-insensitive matching of letter suffixes (e.g. 3A/3a).
const normalizedPhaseNums = new Set(
[...milestonePhaseNums].map(num => (num.replace(/^0+/, '') || '0').toLowerCase())
);
// Match a phase directory name to the milestone's phase set.
// Handles: "01-foo" → "1", "3A-bar" → "3a", "3.1-baz" → "3.1"
// Returns false for non-phase directories (no leading digit).
function isDirInMilestone(dirName) {
if (normalizedPhaseNums.size === 0) return true; // no scoping
const m = dirName.match(/^0*(\d+[A-Za-z]?(?:\.\d+)*)/);
if (!m) return false; // not a phase directory
return normalizedPhaseNums.has(m[1].toLowerCase());
}
// Gather stats from phases (scoped to current milestone only)
let phaseCount = 0;
let totalPlans = 0;
let totalTasks = 0;
@@ -103,6 +140,8 @@ function cmdMilestoneComplete(cwd, version, options, raw) {
const dirs = entries.filter(e => e.isDirectory()).map(e => e.name).sort();
for (const dir of dirs) {
if (!isDirInMilestone(dir)) continue;
phaseCount++;
const phaseFiles = fs.readdirSync(path.join(phasesDir, dir));
const plans = phaseFiles.filter(f => f.endsWith('-PLAN.md') || f === 'PLAN.md');
@@ -182,10 +221,13 @@ function cmdMilestoneComplete(cwd, version, options, raw) {
const phaseEntries = fs.readdirSync(phasesDir, { withFileTypes: true });
const phaseDirNames = phaseEntries.filter(e => e.isDirectory()).map(e => e.name);
let archivedCount = 0;
for (const dir of phaseDirNames) {
if (!isDirInMilestone(dir)) continue;
fs.renameSync(path.join(phasesDir, dir), path.join(phaseArchiveDir, dir));
archivedCount++;
}
phasesArchived = phaseDirNames.length > 0;
phasesArchived = archivedCount > 0;
} catch {}
}