fix(phase): scan .planning/phases/ for orphan dirs in phase add (#2034)

cmdPhaseAdd computed maxPhase from ROADMAP.md only, allowing orphan
directories on disk (untracked in roadmap) to silently collide with
newly added phases. The new phase's mkdirSync succeeded against the
existing directory, contaminating it with fresh content.

Fix: take max(roadmapMax, diskMax) where diskMax scans
.planning/phases/ and strips optional project_code prefix before
parsing the leading integer. Backlog orphans (>=999) are skipped.

Adds 3 regression tests covering:
- orphan dir with number higher than roadmap max
- prefixed orphan dirs (project_code-NN-slug)
- no collision when orphan number is lower than roadmap max

Fixes #2026

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Tom Boucher
2026-04-10 12:04:33 -04:00
committed by GitHub
parent aa3e9cfaf4
commit 7f11362952
2 changed files with 129 additions and 1 deletions

View File

@@ -340,7 +340,9 @@ function cmdPhaseAdd(cwd, description, raw, customId) {
if (!_newPhaseId) error('--id required when phase_naming is "custom"');
_dirName = `${prefix}${_newPhaseId}-${slug}`;
} else {
// Sequential mode: find highest integer phase number (in current milestone only)
// Sequential mode: find highest integer phase number from two sources:
// 1. ROADMAP.md (current milestone only)
// 2. .planning/phases/ on disk (orphan directories not tracked in roadmap)
// Skip 999.x backlog phases — they live outside the active sequence
const phasePattern = /#{2,4}\s*Phase\s+(\d+)[A-Z]?(?:\.\d+)*:/gi;
let maxPhase = 0;
@@ -351,6 +353,21 @@ function cmdPhaseAdd(cwd, description, raw, customId) {
if (num > maxPhase) maxPhase = num;
}
// Also scan .planning/phases/ for orphan directories not tracked in ROADMAP.
// Directory names follow: [PREFIX-]NN-slug (e.g. 03-api or CK-05-old-feature).
// Strip the optional project_code prefix before extracting the leading integer.
const phasesOnDisk = path.join(planningDir(cwd), 'phases');
if (fs.existsSync(phasesOnDisk)) {
const dirNumPattern = /^(?:[A-Z][A-Z0-9]*-)?(\d+)-/;
for (const entry of fs.readdirSync(phasesOnDisk)) {
const match = entry.match(dirNumPattern);
if (!match) continue;
const num = parseInt(match[1], 10);
if (num >= 999) continue; // skip backlog orphans
if (num > maxPhase) maxPhase = num;
}
}
_newPhaseId = maxPhase + 1;
const paddedNum = String(_newPhaseId).padStart(2, '0');
_dirName = `${prefix}${paddedNum}-${slug}`;