fix(milestone): insert MILESTONES.md entries in reverse chronological order

cmdMilestoneComplete previously appended new milestone entries at the
end of MILESTONES.md. Over successive milestones this produced
chronological order (oldest first), which is counterintuitive — users
expect the most recent milestone at the top.

This fix detects the file header (h1-h3) and inserts the new entry
immediately after it, pushing older entries down. Files without a
recognizable header get the entry prepended. New files still get a
default '# Milestones' header.

Adds 2 tests: single insertion ordering assertion and three-sequential-
completions verification (v1.2 < v1.1 < v1.0 in file order).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Tibsfox
2026-02-27 03:46:37 -08:00
parent f9fc2a3f33
commit 732ea09f81
2 changed files with 56 additions and 3 deletions

View File

@@ -150,7 +150,16 @@ function cmdMilestoneComplete(cwd, version, options, raw) {
if (fs.existsSync(milestonesPath)) {
const existing = fs.readFileSync(milestonesPath, 'utf-8');
fs.writeFileSync(milestonesPath, existing + '\n' + milestoneEntry, 'utf-8');
// Insert after the header line(s) for reverse chronological order (newest first)
const headerMatch = existing.match(/^(#{1,3}\s+[^\n]*\n\n?)/);
if (headerMatch) {
const header = headerMatch[1];
const rest = existing.slice(header.length);
fs.writeFileSync(milestonesPath, header + milestoneEntry + rest, 'utf-8');
} else {
// No recognizable header — prepend the entry
fs.writeFileSync(milestonesPath, milestoneEntry + existing, 'utf-8');
}
} else {
fs.writeFileSync(milestonesPath, `# Milestones\n\n${milestoneEntry}`, 'utf-8');
}