Files
msd-core/src/roadmap-upgrade.cts
Tom Boucher 0624c5da6f chore(#3212): src/text-lines.cts is the sole owner of line-terminator handling — Phase 2 (#3420)
* test(#3413): failing-first suite for the line-terminator seam

Phase 2 of epic #3212 (ADR-3212 §3/§6/§7). Tests only — src/text-lines.cts
does not exist yet, so tests/text-lines.test.cjs fails with MODULE_NOT_FOUND
at its require line, which is the intended RED.

The frontmatter.test.cjs additions drive #3360 (confirmed-bug) fail-first:
parseMustHavesBlock currently returns [] for every must_haves block on a
CRLF-authored plan file, because \r is its own LineTerminator in ECMAScript
and two /m-anchored \s* patterns can absorb it, inflating a captured indent
by one character and tripping the "not nested under must_haves" guard.
Verified locally against the current (unfixed) compiled module: both the
direct repro and the silent-exit "blank line before must_haves:" variant
return [] today. A parity property test (crlf vs lf must deep-equal for
every block name) matches a pattern this maintainer has required repeatedly
for prior CRLF fixes in this codebase (Cortex-recorded, verify_intent=held).

The no-crlf-fragile-split.rule.test.cjs additions lock the eslint rule's
future fix-hint text (pointing at splitLines()) and its self-reference
non-violation (the seam's own correct \r?\n split must never flag itself).

Design: .gsd/phase/chore-3413-text-lines-seam/40-design.md
Test matrix: .gsd/phase/chore-3413-text-lines-seam/50-test-matrix.md

* chore(#3413): src/text-lines.cts owns line-terminator handling

Phase 2 of epic #3212 (ADR-3212 §3/§6/§7). Adds splitLines/normalizeEol/
detectEol/joinLines and migrates frontmatter.cts onto it.

parseMustHavesBlock (#3360, confirmed-bug) returned [] for every
must_haves block on a CRLF plan file. Root cause: \r is its own
LineTerminator in ECMAScript, so under /m two \s*-anchored indentation
lookups could match at the position INSIDE a \r\n pair and absorb the
terminator, inflating the captured indent by one character and tripping
the "not nested under must_haves" guard. Two silent exits, one with a
diagnostic and one without (a blank line before must_haves: hits the
silent path). Fixed by converting both lookups from a whole-string /m
match to split-then-scan — splitLines first, then a per-line, non-/m
match — the same structural pattern parseYamlRegion (30 lines away in
the same file) already used safely. Nothing downstream of the two
lookups changed; blockLines is now sliced from the already-split array
instead of re-splitting a substring, but its contents are unchanged for
LF input, and the per-line dash/kv parsing loop is untouched.

A parity property test (CRLF and LF plans parse to identical must_haves
for every block name) matches a pattern this maintainer has required
repeatedly for prior CRLF fixes in this file's neighborhood (Cortex:
7 recorded decisions, verify_intent -> held).

frontmatter.cts's other .split(/\r?\n/) call sites (parseYamlRegion,
isFrontmatterShaped, sliceTopLevelFrontmatterSegments, spliceFrontmatter)
are rerouted onto splitLines — a literal 1:1 substitution, zero behavior
change, since splitLines IS that same regex plus a type guard.

The 4 scripts/normalizeLineEndings copies (gen-registry, gen-loop-host-
contract, gen-capability-registry, gen-context-index) are deleted and
rerouted onto normalizeEol, which strips a bare unpaired \r exactly like
the deleted copies did (not just \r\n pairs) -- verified against each
script's own --check mode against its real generated output.

local/no-crlf-fragile-split widens from tests/ to src/**/*.cts, with its
fix-hint message now naming splitLines() instead of the raw regex --
the prohibition finally has a primitive to point at. Detection logic
unchanged in this phase (deliberate scope limit, see design doc Known
limits: the rule doesn't yet recognize safeReadFile/platformReadSync as
a content source, and has no detector for the \s-adjacent-to-anchor
shape that is #3360's actual mechanism -- the CLASS is converged by the
direct fix + regression test regardless).

joinLines/detectEol are NOT wired into frontmatter.cts's own write path
(cmdFrontmatterSet/Merge -> platformWriteSync) -- verified that
platformWriteSync already, unconditionally converts CRLF->LF on every
.md write today as a pre-existing policy owned by a different module,
and ADR-3212's backward-compatibility clause rules out a file-format
change in any phase. Stated explicitly in Known limits rather than left
for a reader to discover.

Six-gate ripple: .gitignore, eslint.config.mjs (src/**/*.cts block),
docs/INVENTORY.md + INVENTORY-MANIFEST.json (regenerated), CONTEXT.md
glossary (Text Lines Module, mirroring Phase 1's Pattern Module entry).

Design: .gsd/phase/chore-3413-text-lines-seam/40-design.md
Test matrix: .gsd/phase/chore-3413-text-lines-seam/50-test-matrix.md

* fix(#3413): fix 13 pre-existing CRLF-fragile splits the widened rule found

Widening local/no-crlf-fragile-split from tests/ to src/**/*.cts (the
previous commit) immediately surfaced 13 real, pre-existing violations
across 10 files -- undetected until now because the rule never scanned
src/. This is the exact defect class ADR-3212 exists to close, playing
out again one phase after Phase 1 hit the same shape ("the new lint
rule -- once live -- found 27 more"). Per CLAUDE.md's no-defer rule,
fixed inline rather than deferred or suppressed; there is no
established suppression convention for this rule in src/ and inventing
one now would undermine the point of widening it.

audit.cts, broken-windows.cts, core-utils.cts, init.cts, milestone.cts,
phase.cts (x3), profile-output.cts, roadmap.cts (x2): bare-\n splits or
regex character classes widened to \r?\n / [^\r\n], each following the
same pattern already established migrating frontmatter.cts.

phase-estimation.cts: `\r?(?:\n|$)` restructured to `(?:\r?\n|\r?$)` --
already semantically CRLF-safe, but the rule's lexical scanner doesn't
recognize \r? guarding a group (only \r? immediately before a literal
\n). Verified the two forms are equivalent across all four EOL/EOF
cases before restructuring, not assumed.

roadmap-upgrade.cts needed two coupled sites, not the one flagged line:
computeMigrationPlan and applyMigration must agree on line
representation for the lines[edit.lineIndex] === edit.from equality
check to hold, and the write-back needed joinLines + detectEol -- a
plain lines.join('\n') was silently flattening a CRLF ROADMAP.md to LF
wholesale on every migration. This is the first real production
consumer of joinLines/detectEol in this epic (frontmatter.cts's own
write path doesn't use them -- see the previous commit's Known limits).

Fixing the 13 flagged sites surfaced 4 more adjacent same-shape sites
the rule doesn't track (.search() and new RegExp(dynamicString) aren't
in its tracked call/construction set). Investigated each empirically --
hand-tracing this exact bug class already produced one wrong conclusion
earlier in this phase (a detectEol design-doc arithmetic error), so
these were verified with real CRLF fixtures rather than reasoned about
on paper:

  - audit.cts (scanTodos): REAL bug, fixed. `bodyMatch.trim().split
    ('\n')[0]` leaked a trailing \r into a user-visible todo summary on
    CRLF input -- .trim() only strips the string's outer edges, not a
    \r sitting mid-string before the first bare \n. Now splitLines(...)
    [0].
  - phase.cts (cmdPhaseInsert, bullet-style branch): REAL bug, fixed.
    [^\n]* in targetBulletPattern swallowed a line's trailing \r on
    CRLF input, shifting the computed insert position to land INSIDE
    the \r\n pair; combined with a hardcoded '\n' bullet separator, a
    CRLF ROADMAP.md ended up with a mixed CRLF/LF result after an
    insert. Fixed with two coupled changes (either alone still
    corrupts, verified both ways): [^\r\n]* in the pattern, and the new
    bullet's leading terminator now comes from detectEol(rawContent).
  - roadmap.cts (cmdRoadmapAnnotateDependencies phase-boundary scan):
    investigated, genuinely safe, left untouched. The .search(/\n#{2,4}
    .../) boundary-finder and the [^\n]*-based heading match were
    empirically verified on a 3-phase CRLF fixture -- the only stray \r
    ends up at the tail of an intermediate phaseSection string that is
    only ever used for .test()-based idempotency checks, never for an
    exact-match comparison or written back to disk. No corruption on
    round-trip.

Every fix re-verified: npm run build:lib clean, npx eslint
'src/**/*.cts' --no-cache reports 0 problems (was 13), and each
fixed function's existing LF-input tests were spot-checked unchanged.

* fix(#3413): apply orthogonal review findings

Two isolated review engines (correctness + security) ran against the
full diff and found three majors, one real security issue, and several
disclosure-worthy minors. All fixed or explicitly disclosed with
evidence; nothing deferred.

MAJOR — detectEol's tie-break contradicted its own documented contract.
Code returned '\n' on a 1:1 crlf/bare-LF tie; every doc (design doc,
CONTEXT.md, the function's own comment) says ties resolve to '\r\n'.
The existing test masked this by reusing the same tie fixture the
buggy code happened to satisfy, rather than a genuine LF-majority
case. Root cause: an Edit attempted earlier in this phase to fix this
exact arithmetic error was blocked by the tier guard, and a later
dispatch was incorrectly told it had already landed. Fixed: condition
is now crlfCount >= bareLfCount; the test fixture corrected to a
genuine 2:1 majority, with a new explicit tie-case test.

MAJOR — phase.cts's cmdPhaseInsert built an EOL-aware bulletEntry via
detectEol(rawContent), justified by a comment claiming a hardcoded
'\n' corrupts a CRLF ROADMAP.md. False: this write goes through
platformWriteSync, whose normalizeContent/_normalizeMd unconditionally
converts CRLF->LF for any .md target — the templating was inert dead
code, erased before the file is ever written. Reverted to hardcoded
'\n', comment corrected to state the true reasoning. The separate
[^\n]* -> [^\r\n]* widening one function up (a real splice-position
fix, independent of final EOL) was kept.

MAJOR — roadmap-upgrade.cts's stated rationale for switching onto
splitLines/joinLines was wrong (both functions always agreed on line
representation, before and after — the claimed equality-check risk
never existed), and the change it justified introduced a real
regression: forcing every line onto one dominant terminator silently
rewrites untouched lines' EOL on a mixed-CRLF/LF ROADMAP.md. This
write path uses raw fs.writeFileSync, not platformWriteSync, so unlike
the phase.cts case above the regression is genuinely live.

Fixing this took two attempts. The first attempt (revert to
split('\n')/join('\n') plus a suppression comment) was correctly
blocked by an agent that discovered local/no-crlf-fragile-split is a
PROTECTED_RULES entry in tests/portability-rule-disable-ban.test.cjs —
a hard, out-of-band, ADR-1703-governed guardrail banning any
eslint-disable of this rule anywhere in src/**/*.cts. That agent also
detected and correctly disregarded an injected instruction that
appeared in tool output during a git operation, per this session's
untrusted-content policy. The actual fix: computeMigrationPlan
reverted to roadmapContent.split('\n') (confirmed lint-clean — the
rule's data-flow tracking only follows a variable's initializer, and
this one is declared empty then reassigned in a try block).
applyMigration's write-back now splices edits against the ORIGINAL
content string via indexOf('\n', pos) boundary-walking instead of a
full split/rejoin, so every untouched character — including every
line's own terminator — is copied byte-for-byte. A capture-group split
(/(\r\n|\n)/, preserving terminators inline) was tried first and
empirically confirmed to still trip the rule before this approach was
chosen instead.

MINOR (security) — roadmap.cts's cmdRoadmapAnnotateDependencies used
the STRING form of String#replace, so $&, $`, $', $1-$9 inside
must_haves.truths content (author-controlled) were interpreted as
replacement directives, splicing unrelated ROADMAP.md text into the
result. Fixed with the function-replacement form, which is never
pattern-interpreted. Verified before/after with the reviewer's exact
repro.

Also disclosed rather than silently left: test matrix row 31 (four
planned CRLF-materialized regression tests) was never implemented as
separate files — corrected to record the actual verification (a
manual --check run plus incidental existing coverage via each script's
normalizeLineEndings: normalizeEol alias). parseMustHavesBlock's LF
behavior was claimed byte-for-byte unchanged but the old
yaml.indexOf(blockMatch[0]) substring search could match an unrelated
earlier occurrence of the header text (e.g. inside a quoted value) —
the split-then-scan fix incidentally also closes this, a strict
improvement now recorded in the design doc rather than left implicit.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(#3413): checkpoint 2 red — missing eslint ignore entry, RuleTester config error

Checkpoint 2 came back red with 5 failures on the reviewed sha, both
gaps genuinely undetectable by any local gate.

eslint.config.mjs was missing the 'gsd-core/bin/lib/text-lines.cjs'
ignores-list entry (ADR-457: generated .cjs artifacts are excluded from
direct type-aware linting). Phase 1's sibling entry (pattern.cjs) sits
two lines above it and was the exact precedent read while researching
the six-gate ripple for this module -- missed anyway. Caught by
tests/repo-invariants.test.cjs's bin/lib coverage-tracking test, which
only runs on the remote suite.

tests/no-crlf-fragile-split.rule.test.cjs's row-32 case specified both
`messageId` and `message` on the same RuleTester error assertion --
ESLint's RuleTester rejects that combination outright. This existed
since the test was first authored and was never caught locally: `npx
eslint` only lints the file's syntax, it does not execute RuleTester,
and local `node --test` is hard-blocked in this repo -- the assertion
had never actually RUN before this checkpoint. It was even present in
checkpoint 1's failure list, listed there as one of the "expected RED"
tests; I matched it against my expected-failures list by test NAME
only and never inspected the actual failure detail closely enough to
notice it was failing for the wrong reason (a RuleTester config error,
not the intended message-text mismatch). Fixed by keeping `message`
(the exact-text assertion the test exists to make) and dropping
`messageId`. Verified the crlfFragileSplit message string in
eslint-rules/no-crlf-fragile-split.cjs matches this assertion
character-for-character, and swept every other invalid case in the
file for the same double-specification bug (none found -- all
pre-existing cases use messageId alone).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* docs(#3413): add Fixed changeset for the #3360 CRLF parsing fix

The sole user-visible effect of this phase. No breaking-change label
or Changed fragment needed — ADR-3212's Backward Compatibility section
names the Node floor (Phase 1, already shipped) as the epic's only
breaking change; Phase 2 has none.

* chore(#3413): backfill changeset pr number to 3420

---------

Co-authored-by: sim <sim@local>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-13 20:27:48 -04:00

655 lines
26 KiB
TypeScript

/**
* Roadmap Upgrade — Migration tool for converting legacy 'Phase N' phase IDs
* to milestone-prefixed 'Phase M-NN' form.
*
* ADR-457 build-at-publish: the hand-written bin/lib/roadmap-upgrade.cjs collapsed
* to a TypeScript source of truth. Behaviour is preserved byte-for-behaviour
* from the prior hand-written .cjs; only types are added.
*/
import fs from 'node:fs';
import path from 'node:path';
import { execSync } from 'node:child_process';
import { retryRenameSync } from './shell-command-projection.cjs';
// eslint-disable-next-line @typescript-eslint/no-require-imports
import planningWorkspace = require('./planning-workspace.cjs');
// eslint-disable-next-line @typescript-eslint/no-require-imports
import phaseIdMod = require('./phase-id.cjs');
const { planningDir } = planningWorkspace;
const { stripProjectCodePrefix, PHASE_NUMBER_TOKEN_SOURCE } = phaseIdMod;
// ─── Regex helpers ────────────────────────────────────────────────────────────
// Matches legacy phase headings: ### Phase N: Name (also decimal: Phase 2.1:)
// Captures: (hashes)(spaces)(phase-number)(rest-of-line)
const LEGACY_PHASE_HEADING_RE = new RegExp(
`^(#{2,4})\\s*(?:\\[[^\\]]{1,200}\\]\\s*)?Phase\\s+(${PHASE_NUMBER_TOKEN_SOURCE})\\s*:(.*)`,
'i'
);
// Matches already-migrated phase headings: ### Phase M-NN: Name
const MIGRATED_PHASE_HEADING_RE = /^#{2,4}\s*(?:\[[^\]]{1,200}\]\s*)?Phase\s+\d+-\d{2}\s*:/i;
// Matches milestone section headings: ## v1.0, ## Roadmap v2.0, ## ✅ v1.0, ## [GSD] v1.0, etc.
// The optional bracket-token prefix (e.g., [GSD]) must be tested before the emoji group.
const MILESTONE_HEADING_RE = /^##\s+(?:\[[^\]]{1,200}\]\s+|Roadmap\s+|[✅🚧]\s*)?v(\d+)\.(\d+)(?:\s|:)/iu;
// ─── Types ────────────────────────────────────────────────────────────────────
interface ParsedPhaseEntry {
lineIndex: number;
headingLine: string;
alreadyMigrated: boolean;
milestoneInt?: number | null;
legacyPhaseNum?: string;
phaseName?: string;
hashes?: string;
}
interface AssignedMapping {
newId: string;
milestoneInt: number;
subIndex: number;
legacyPhaseNum: string;
}
interface PhaseRename {
oldId: string;
newId: string;
oldDir: string;
newDir: string;
}
interface RoadmapEdit {
lineIndex: number;
from: string;
to: string;
}
interface CrossRefEdit {
file: string;
from: string;
to: string;
}
interface MigrationPlan {
alreadyMigrated: boolean;
phases: PhaseRename[];
roadmapEdits: RoadmapEdit[];
crossRefEdits: CrossRefEdit[];
}
interface ApplyMigrationResult {
applied?: boolean;
alreadyMigrated?: boolean;
dryRun?: boolean;
renamedDirs?: string[];
editedFiles?: string[];
}
// ─── Pure computation helpers ─────────────────────────────────────────────────
/**
* Parse the ROADMAP.md content and build a list of phase entries with their
* enclosing milestone major version.
*
* Returns an array of:
* { lineIndex, headingLine, milestoneInt, legacyPhaseNum, phaseName }
*/
function parseRoadmapPhases(lines: string[]): ParsedPhaseEntry[] {
const results: ParsedPhaseEntry[] = [];
let currentMilestoneInt: number | null = null;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const milestoneMatch = line.match(MILESTONE_HEADING_RE);
if (milestoneMatch) {
currentMilestoneInt = parseInt(milestoneMatch[1], 10);
continue;
}
if (MIGRATED_PHASE_HEADING_RE.test(line)) {
// Already-migrated heading found — caller will detect this
results.push({ lineIndex: i, headingLine: line, alreadyMigrated: true });
continue;
}
const phaseMatch = line.match(LEGACY_PHASE_HEADING_RE);
if (phaseMatch) {
results.push({
lineIndex: i,
headingLine: line,
milestoneInt: currentMilestoneInt,
legacyPhaseNum: phaseMatch[2],
phaseName: phaseMatch[3].trim(),
hashes: phaseMatch[1],
alreadyMigrated: false,
});
}
}
return results;
}
/**
* Assign sub-indices within each milestone, building a per-entry mapping.
*
* Input: array from parseRoadmapPhases (non-migrated entries only).
* Returns: Map<lineIndex, { newId, milestoneInt, subIndex }>
*
* Keyed by `lineIndex` (the unique position of the heading line in ROADMAP.md)
* so that identical legacy phase numbers in different milestones (e.g., two
* `Phase 1` headings in v1.0 and v2.0) each get their own correct M-NN ID
* instead of the later milestone's mapping overwriting the earlier one.
*
* Sub-indices are 1-based and sequential within each milestone.
*/
function assignSubIndices(phaseEntries: ParsedPhaseEntry[]): Map<number, AssignedMapping> {
const milestoneCounters = new Map<number, number>(); // milestoneInt → counter
const mapping = new Map<number, AssignedMapping>(); // lineIndex → { newId, milestoneInt, subIndex }
for (const entry of phaseEntries) {
if (entry.alreadyMigrated) continue;
const m = entry.milestoneInt;
if (m === null || m === undefined) continue;
const counter = (milestoneCounters.get(m) || 0) + 1;
milestoneCounters.set(m, counter);
const subIndex = String(counter).padStart(2, '0');
const newId = `${m}-${subIndex}`;
mapping.set(entry.lineIndex, { newId, milestoneInt: m, subIndex: counter, legacyPhaseNum: entry.legacyPhaseNum! });
}
return mapping;
}
/**
* Read a phase directory name and return its numeric token (stripping project_code prefix).
* e.g. "GSD-01-setup" → "01", "01-setup" → "01", "02-implement" → "02", "02.1-hotfix" → "02.1"
*/
function extractPhaseNumFromDir(dirName: string): string | null {
// Strip optional project_code prefix: "GSD-01-setup" → "01-setup"
const stripped = stripProjectCodePrefix(dirName);
// Matches: digits + optional letter + optional decimal suffix, followed by '-' or end.
// e.g. "02.1-hotfix" → "02.1", "01-setup" → "01"
const m = stripped.match(new RegExp(`^(${PHASE_NUMBER_TOKEN_SOURCE})(?:-|$)`, 'i'));
return m ? m[1] : null;
}
/**
* Build the new directory name from old name and new phase ID.
* old: "01-setup" newId: "1-02" projectCode: "GSD" → "GSD-01-02-setup"
* old: "01-setup" newId: "1-02" projectCode: null → "01-02-setup"
* old: "GSD-01-setup" newId: "1-02" projectCode: "GSD" → "GSD-01-02-setup"
*/
function buildNewDirName(oldDirName: string, newId: string, projectCode: string | null): string {
// Strip existing project_code prefix
const stripped = stripProjectCodePrefix(oldDirName);
// Extract slug: everything after "NN-" (the old phase num, including decimal like 02.1)
const slugMatch = stripped.match(new RegExp(`^${PHASE_NUMBER_TOKEN_SOURCE}-(.*)`, 'i'));
const slug = slugMatch ? slugMatch[1] : stripped;
// Build M-NN prefix (zero-pad both parts)
const [milestoneStr, subStr] = newId.split('-');
const milestoneInt = parseInt(milestoneStr, 10);
const paddedMilestone = String(milestoneInt).padStart(2, '0');
const newBase = slug ? `${paddedMilestone}-${subStr}-${slug}` : `${paddedMilestone}-${subStr}`;
return projectCode ? `${projectCode}-${newBase}` : newBase;
}
// ─── computeMigrationPlan ─────────────────────────────────────────────────────
/**
* Compute a migration plan without touching the filesystem.
*/
function computeMigrationPlan(cwd: string, options: Record<string, unknown> = {}): MigrationPlan {
void options;
const pDir = planningDir(cwd);
const roadmapPath = path.join(pDir, 'ROADMAP.md');
const configPath = path.join(pDir, 'config.json');
const phasesDir = path.join(pDir, 'phases');
// ── Check config for existing convention ─────────────────────────────────
let configData: Record<string, unknown> = {};
try {
configData = JSON.parse(fs.readFileSync(configPath, 'utf8')) as Record<string, unknown>;
} catch { /* config may not exist */ }
if (configData['phase_id_convention'] === 'milestone-prefixed') {
return { alreadyMigrated: true, phases: [], roadmapEdits: [], crossRefEdits: [] };
}
const projectCode = typeof configData['project_code'] === 'string' ? configData['project_code'] : null;
// ── Read ROADMAP.md ───────────────────────────────────────────────────────
let roadmapContent = '';
try {
roadmapContent = fs.readFileSync(roadmapPath, 'utf8');
} catch {
throw new Error(`ROADMAP.md not found at ${roadmapPath}`);
}
const lines = roadmapContent.split('\n');
const parsedPhases = parseRoadmapPhases(lines);
// Check for any already-migrated headings
const hasAnyMigrated = parsedPhases.some(e => e.alreadyMigrated);
if (hasAnyMigrated) {
return { alreadyMigrated: true, phases: [], roadmapEdits: [], crossRefEdits: [] };
}
const legacyPhases = parsedPhases.filter(e => !e.alreadyMigrated);
const idMapping = assignSubIndices(legacyPhases);
// Secondary lookup: (milestoneInt, normalizedLegacyNum) → newId
// Used for directory renames and checklist rewrites where line position is unknown.
// For simplicity, each milestone gets its own Map from legacy num → newId.
const milestoneIdMap = new Map<number, Map<string, string>>(); // milestoneInt → Map<normalizedLegacyNum, newId>
for (const [, entry] of idMapping) {
if (!milestoneIdMap.has(entry.milestoneInt)) {
milestoneIdMap.set(entry.milestoneInt, new Map<string, string>());
}
const mMap = milestoneIdMap.get(entry.milestoneInt)!;
const legacyNum = entry.legacyPhaseNum;
// Register integer forms (covers plain numeric and letter-suffix IDs)
const intPart = parseInt(legacyNum, 10);
const paddedLegacy = String(intPart).padStart(2, '0');
const unpaddedLegacy = String(intPart);
mMap.set(paddedLegacy, entry.newId);
mMap.set(unpaddedLegacy, entry.newId);
// Also register the original form and padded-integer+decimal form
// so decimal IDs like "2.1" / "02.1" round-trip correctly.
mMap.set(legacyNum, entry.newId);
const dotIdx = legacyNum.indexOf('.');
if (dotIdx !== -1) {
const decimalSuffix = legacyNum.slice(dotIdx); // e.g. ".1"
mMap.set(paddedLegacy + decimalSuffix, entry.newId);
mMap.set(unpaddedLegacy + decimalSuffix, entry.newId);
}
}
// ── Read existing phase directories ───────────────────────────────────────
let existingDirs: string[] = [];
try {
existingDirs = fs.readdirSync(phasesDir).filter(d => {
try {
return fs.statSync(path.join(phasesDir, d)).isDirectory();
} catch { return false; }
});
} catch { /* phases dir may not exist */ }
// ── Build phase rename pairs ───────────────────────────────────────────────
// Flat ordered list of (legacyPhaseNum, newId) in ROADMAP order, for dir matching.
const orderedMappings = [...idMapping.values()].map(e => ({
legacyPhaseNum: e.legacyPhaseNum,
newId: e.newId,
milestoneInt: e.milestoneInt,
_used: false,
}));
// Note: if the same legacy phase number appears in multiple milestones (the exact legacy
// ambiguity this tool is designed to resolve), directories are matched in ROADMAP document
// order — the first ROADMAP occurrence of a given number claims the first matching disk dir.
// This is the only unambiguous assignment strategy for flat dirs that carry no milestone
// context. The dry-run output shows the complete rename plan so users can review before
// applying with --apply.
const phases: PhaseRename[] = [];
for (const dirName of existingDirs) {
const phaseNum = extractPhaseNumFromDir(dirName);
if (!phaseNum) continue;
const intPart = parseInt(phaseNum, 10);
const paddedPhaseNum = String(intPart).padStart(2, '0');
const unpaddedPhaseNum = String(intPart);
// For decimal IDs like "02.1", also try "2.1"
const dotIdx = phaseNum.indexOf('.');
const decimalUnpadded = dotIdx !== -1 ? unpaddedPhaseNum + phaseNum.slice(dotIdx) : null;
// Find the first unused mapping whose legacy number matches (exact, padded, unpadded, or decimal)
const found = orderedMappings.find(m => !m._used && (
m.legacyPhaseNum === phaseNum ||
m.legacyPhaseNum === paddedPhaseNum ||
m.legacyPhaseNum === unpaddedPhaseNum ||
(decimalUnpadded && m.legacyPhaseNum === decimalUnpadded)
));
if (!found) continue;
found._used = true;
const newDirName = buildNewDirName(dirName, found.newId, projectCode);
if (newDirName !== dirName) {
phases.push({
oldId: phaseNum,
newId: found.newId,
oldDir: dirName,
newDir: newDirName,
});
}
}
// ── Build ROADMAP.md line edits ────────────────────────────────────────────
const roadmapEdits: RoadmapEdit[] = [];
for (const entry of legacyPhases) {
// Use lineIndex as the canonical key (not legacyPhaseNum, which may collide across milestones)
const mapping = idMapping.get(entry.lineIndex);
if (!mapping) continue;
// Rewrite heading line: "### Phase N: Name" → "### Phase M-NN: Name"
const oldLine = lines[entry.lineIndex];
const newLine = oldLine.replace(
new RegExp(`^(#{2,4}\\s*(?:\\[[^\\]]{1,200}\\]\\s*)?Phase\\s+)${PHASE_NUMBER_TOKEN_SOURCE}(\\s*:)`, 'i'),
`$1${mapping.newId}$2`
);
if (newLine !== oldLine) {
roadmapEdits.push({ lineIndex: entry.lineIndex, from: oldLine, to: newLine });
}
}
// Rewrite checklist lines in ROADMAP.md — use milestone context to resolve collisions.
let currentChecklistMilestone: number | null = null;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
// Track enclosing milestone section for context-aware lookup
const milestoneHeadingMatch = line.match(MILESTONE_HEADING_RE);
if (milestoneHeadingMatch) {
currentChecklistMilestone = parseInt(milestoneHeadingMatch[1], 10);
}
// Already in roadmapEdits? skip
if (roadmapEdits.some(e => e.lineIndex === i)) continue;
// Match checklist items: "- [ ] **Phase N:**" or "- [x] Phase N:" (also decimal)
const checklistMatch = line.match(
new RegExp(`^(\\s*-\\s*\\[[ x]\\]\\s*\\*{0,2}Phase\\s+)(${PHASE_NUMBER_TOKEN_SOURCE})(\\s*[:\\s*])`, 'i')
);
if (checklistMatch) {
const legacyNum = checklistMatch[2];
const cIntPart = parseInt(legacyNum, 10);
const paddedLegacy = String(cIntPart).padStart(2, '0');
const unpaddedLegacy = String(cIntPart);
const cDotIdx = legacyNum.indexOf('.');
const paddedLegacyDecimal = cDotIdx !== -1 ? paddedLegacy + legacyNum.slice(cDotIdx) : null;
// Prefer milestone-context lookup (avoids collision across milestones)
let newId: string | undefined;
if (currentChecklistMilestone !== null && milestoneIdMap.has(currentChecklistMilestone)) {
const mMap = milestoneIdMap.get(currentChecklistMilestone)!;
newId = mMap.get(legacyNum) || mMap.get(paddedLegacy) || mMap.get(unpaddedLegacy);
if (!newId && paddedLegacyDecimal) newId = mMap.get(paddedLegacyDecimal);
}
if (!newId) {
// Fallback: use ordered flat list (no milestone collision in this roadmap)
const found = orderedMappings.find(m =>
m.legacyPhaseNum === legacyNum ||
m.legacyPhaseNum === paddedLegacy ||
m.legacyPhaseNum === unpaddedLegacy ||
(paddedLegacyDecimal && m.legacyPhaseNum === paddedLegacyDecimal)
);
if (found) newId = found.newId;
}
if (newId) {
const newLine = line.replace(
new RegExp(`^(\\s*-\\s*\\[[ x]\\]\\s*\\*{0,2}Phase\\s+)${PHASE_NUMBER_TOKEN_SOURCE}(\\s*[:\\s*])`, 'i'),
`$1${newId}$2`
);
if (newLine !== line) {
roadmapEdits.push({ lineIndex: i, from: line, to: newLine });
}
}
}
}
// ── Build cross-ref edits for STATE.md and PROJECT.md ────────────────────
const crossRefEdits: CrossRefEdit[] = [];
const crossRefFiles = ['STATE.md', 'PROJECT.md'];
for (const fileName of crossRefFiles) {
const filePath = path.join(pDir, fileName);
if (!fs.existsSync(filePath)) continue;
const fileContent = fs.readFileSync(filePath, 'utf8');
// Iterate using orderedMappings (ROADMAP order) — idMapping is now keyed by lineIndex.
for (const m of orderedMappings) {
const legacyNum = m.legacyPhaseNum;
const xIntPart = parseInt(legacyNum, 10);
const paddedNum = String(xIntPart).padStart(2, '0');
const unpaddedNum = String(xIntPart);
// Decimal suffix (e.g. ".1" from "2.1") — preserve in cross-ref patterns
const xDotIdx = legacyNum.indexOf('.');
const decimalSuffix = xDotIdx !== -1 ? legacyNum.slice(xDotIdx) : '';
// Rewrite project_code-prefixed references: "GSD-01-" → "GSD-01-02-"
if (projectCode) {
const [milestoneStr, subStr] = m.newId.split('-');
const paddedMilestone = String(parseInt(milestoneStr, 10)).padStart(2, '0');
const prefixedNew = `${projectCode}-${paddedMilestone}-${subStr}-`;
// Try both padded and original forms as old prefix
for (const oldNum of new Set([paddedNum + decimalSuffix, unpaddedNum + decimalSuffix, paddedNum, unpaddedNum])) {
const prefixedOld = `${projectCode}-${oldNum}-`;
if (fileContent.includes(prefixedOld)) {
crossRefEdits.push({ file: fileName, from: prefixedOld, to: prefixedNew });
}
}
}
// Rewrite prose references: "Phase 1:" → "Phase 1-01:", "Phase 2.1:" → "Phase 1-02:"
const proseOldPatterns = new Set([
`Phase ${unpaddedNum}${decimalSuffix}:`,
`Phase ${paddedNum}${decimalSuffix}:`,
`Phase ${legacyNum}:`,
]);
for (const proseOld of proseOldPatterns) {
if (fileContent.includes(proseOld)) {
const proseNew = `Phase ${m.newId}:`;
crossRefEdits.push({ file: fileName, from: proseOld, to: proseNew });
}
}
}
}
return {
alreadyMigrated: false,
phases,
roadmapEdits,
crossRefEdits,
};
}
/**
* Apply roadmap line edits via character-offset splicing against the
* ORIGINAL content string — never a full split/rejoin (#3413). `lineIndex`
* boundaries are found by scanning for the next bare `\n`, exactly matching
* how computeMigrationPlan() itself indexes lines (`roadmapContent.split('\n')`)
* — both sides must agree on line indexing for `lineText === edit.from` to
* match, and this keeps a `\r` that precedes a `\n` as part of the LINE text
* rather than a separately-normalized terminator. Only a line whose text
* exactly equals an edit's `from` is replaced; every other character —
* including every line's own terminator, touched or not — is copied
* byte-for-byte from the original, so a mixed-EOL ROADMAP.md never has its
* untouched lines silently flattened to one dominant style.
*/
function applyRoadmapEdits(content: string, edits: RoadmapEdit[]): string {
const editByLine = new Map<number, RoadmapEdit>();
for (const edit of edits) editByLine.set(edit.lineIndex, edit);
let result = '';
let pos = 0;
let lineIndex = 0;
for (;;) {
const nlIdx = content.indexOf('\n', pos);
const lineEnd = nlIdx === -1 ? content.length : nlIdx;
const lineText = content.slice(pos, lineEnd);
const edit = editByLine.get(lineIndex);
result += edit && lineText === edit.from ? edit.to : lineText;
if (nlIdx === -1) break;
result += '\n';
pos = nlIdx + 1;
lineIndex++;
}
return result;
}
// ─── applyMigration ───────────────────────────────────────────────────────────
/**
* Apply the migration plan computed by computeMigrationPlan().
*
* @param cwd
* @param plan
* @param options
* @param options.dryRun - Print plan and exit without mutating. (default true)
*/
function applyMigration(cwd: string, plan: MigrationPlan, options: { dryRun?: boolean } = {}): ApplyMigrationResult {
const dryRun = options.dryRun !== false; // default true
if (plan.alreadyMigrated) {
return { alreadyMigrated: true };
}
if (dryRun) {
process.stdout.write(JSON.stringify(plan, null, 2) + '\n');
return { dryRun: true };
}
// ── Real run: verify clean working tree ───────────────────────────────────
let gitStatus: string;
try {
gitStatus = execSync('git status --porcelain', { cwd, encoding: 'utf8', windowsHide: true, timeout: 10_000 });
} catch (err) {
throw new Error(`git status failed: ${(err as Error).message}`);
}
if (gitStatus.trim().length > 0) {
throw new Error('Working tree is dirty. Commit or stash changes before migrating.');
}
const pDir = planningDir(cwd);
const phasesDir = path.join(pDir, 'phases');
const roadmapPath = path.join(pDir, 'ROADMAP.md');
const configPath = path.join(pDir, 'config.json');
const renamedDirs: string[] = [];
const editedFiles: string[] = [];
// Surgical, git-independent rollback state (#1542). A `git reset --hard` +
// `git clean` rollback restores NOTHING for a gitignored `.planning/`
// (commit_docs:false — the default) and is a whole-repo operation besides.
// Instead, record the exact renames performed and snapshot each file before
// rewriting it, then undo precisely those on failure — correct whether
// `.planning/` is git-tracked or ignored.
const performedRenames: Array<{ oldPath: string; newPath: string }> = [];
const fileBackups = new Map<string, { existed: boolean; content: string }>();
const snapshotFile = (filePath: string): void => {
if (fileBackups.has(filePath)) return;
try {
fileBackups.set(filePath, { existed: true, content: fs.readFileSync(filePath, 'utf8') });
} catch {
fileBackups.set(filePath, { existed: false, content: '' });
}
};
try {
// 1. Rename phase directories
for (const phaseEntry of plan.phases) {
const oldPath = path.join(phasesDir, phaseEntry.oldDir);
const newPath = path.join(phasesDir, phaseEntry.newDir);
if (fs.existsSync(oldPath)) {
retryRenameSync(oldPath, newPath);
performedRenames.push({ oldPath, newPath });
renamedDirs.push(`${phaseEntry.oldDir} → ${phaseEntry.newDir}`);
}
}
// 2. Rewrite ROADMAP.md phase headings
if (plan.roadmapEdits.length > 0) {
const roadmapContent = fs.readFileSync(roadmapPath, 'utf8');
const newRoadmapContent = applyRoadmapEdits(roadmapContent, plan.roadmapEdits);
snapshotFile(roadmapPath);
fs.writeFileSync(roadmapPath, newRoadmapContent, 'utf8');
editedFiles.push('ROADMAP.md');
}
// 3. Rewrite cross-refs in STATE.md and PROJECT.md
const crossRefsByFile = new Map<string, CrossRefEdit[]>();
for (const edit of plan.crossRefEdits) {
if (!crossRefsByFile.has(edit.file)) {
crossRefsByFile.set(edit.file, []);
}
crossRefsByFile.get(edit.file)!.push(edit);
}
for (const [fileName, edits] of crossRefsByFile) {
const filePath = path.join(pDir, fileName);
if (!fs.existsSync(filePath)) continue;
let content = fs.readFileSync(filePath, 'utf8');
let changed = false;
for (const edit of edits) {
if (content.includes(edit.from)) {
// Replace all occurrences
content = content.split(edit.from).join(edit.to);
changed = true;
}
}
if (changed) {
snapshotFile(filePath);
fs.writeFileSync(filePath, content, 'utf8');
editedFiles.push(fileName);
}
}
// 4. Update config.json: set phase_id_convention to 'milestone-prefixed'
let configData: Record<string, unknown> = {};
try {
configData = JSON.parse(fs.readFileSync(configPath, 'utf8')) as Record<string, unknown>;
} catch { /* config may not exist yet */ }
configData['phase_id_convention'] = 'milestone-prefixed';
snapshotFile(configPath);
fs.writeFileSync(configPath, JSON.stringify(configData, null, 2) + '\n', 'utf8');
editedFiles.push('config.json');
} catch (err) {
// Surgical rollback: reverse the renames (newest first) and restore every
// file we snapshotted (deleting files that did not previously exist). This
// actually restores `.planning/` regardless of git tracking — so the
// "rolled back" claim is truthful — and never touches anything else.
for (let i = performedRenames.length - 1; i >= 0; i--) {
const { oldPath, newPath } = performedRenames[i];
try {
if (fs.existsSync(newPath)) retryRenameSync(newPath, oldPath);
} catch { /* best-effort */ }
}
for (const [filePath, backup] of fileBackups) {
try {
if (backup.existed) fs.writeFileSync(filePath, backup.content, 'utf8');
else if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
} catch { /* best-effort */ }
}
throw new Error(`Migration failed and rolled back: ${(err as Error).message}`);
}
return { applied: true, renamedDirs, editedFiles };
}
// ─── Exports ──────────────────────────────────────────────────────────────────
export = {
computeMigrationPlan,
applyMigration,
};