fix(#2640): report truthful state_updated + keep progress frontmatter in sync after phase remove (#2974)
* test(#2640): add regression for state_updated false positive + stale progress Three cases: (1) state_updated reflects actual content change, not just file existence; (2) progress.total_phases resync'd even when the body lacks 'Total Phases:' (the no-op guard was skipping syncStateFrontmatter); (3) state_updated is false when STATE.md doesn't exist. * fix(#2640): report truthful state_updated + force frontmatter resync Two defects in cmdPhaseRemove: 1. state_updated was fs.existsSync(statePath) — trivially true, since the file existed before and readModifyWriteStateMd never deletes it. Now captures the boolean return from readModifyWriteStateMd (changed from void to boolean: true when content was written, false on no-op). 2. progress.* frontmatter stayed stale when the body lacked 'Total Phases:' or 'of N' — readModifyWriteStateMd's no-op guard (#948) skipped syncStateFrontmatter when the body transform was unchanged. Now the transform forces a body diff when a phase was actually removed, so the guard passes and syncStateFrontmatter rebuilds progress.* from the post-deletion disk/ROADMAP state. * fix(#2640): address review — gate forced-diff on targetDir, strengthen assertions Two MAJOR findings from isolated adversarial review: 1. Forced-diff injected a spurious 'Total Phases:' line even when no directory was removed (targetDir === null). Now gated on targetDir !== null. 2. Test #2 asserted 'not 3' instead of '2' — would pass for any wrong count. Now asserts exact value. Test #1 strengthened to assert body Total Phases and frontmatter total_phases both equal 1. * chore(#2640): add changeset fragment * chore(#2640): backfill changeset PR number 2974 --------- Co-authored-by: sim <sim@local>
This commit is contained in:
@@ -1598,24 +1598,56 @@ function cmdPhaseRemove(
|
||||
);
|
||||
|
||||
const statePath = path.join(planningDir(cwd), 'STATE.md');
|
||||
let stateUpdated = false;
|
||||
if (fs.existsSync(statePath)) {
|
||||
readModifyWriteStateMd(
|
||||
// #2640: report whether STATE.md content actually changed, not just file
|
||||
// existence (fs.existsSync was trivially true). Also ensure the body
|
||||
// transform produces a diff so readModifyWriteStateMd's no-op guard
|
||||
// (#948) doesn't skip the frontmatter resync — without that, the
|
||||
// progress.* frontmatter block stays stale when the body has no
|
||||
// 'Total Phases:' or 'of N' phrase.
|
||||
stateUpdated = readModifyWriteStateMd(
|
||||
statePath,
|
||||
(stateContent: string) => {
|
||||
const totalRaw = stateExtractField(stateContent, 'Total Phases');
|
||||
let modified = stateContent;
|
||||
const totalRaw = stateExtractField(modified, 'Total Phases');
|
||||
if (totalRaw) {
|
||||
stateContent =
|
||||
stateReplaceField(stateContent, 'Total Phases', String(parseInt(totalRaw, 10) - 1)) ||
|
||||
stateContent;
|
||||
modified =
|
||||
stateReplaceField(modified, 'Total Phases', String(parseInt(totalRaw, 10) - 1)) ||
|
||||
modified;
|
||||
}
|
||||
const ofMatch = stateContent.match(/(\bof\s+)(\d+)(\s*(?:\(|phases?))/i);
|
||||
const ofMatch = modified.match(/(\bof\s+)(\d+)(\s*(?:\(|phases?))/i);
|
||||
if (ofMatch) {
|
||||
stateContent = stateContent.replace(
|
||||
modified = modified.replace(
|
||||
/(\bof\s+)(\d+)(\s*(?:\(|phases?))/i,
|
||||
`$1${parseInt(ofMatch[2], 10) - 1}$3`,
|
||||
);
|
||||
}
|
||||
return stateContent;
|
||||
// #2640: if neither body field was found, the transform is a no-op.
|
||||
// readModifyWriteStateMd's no-op guard (#948) would then skip the
|
||||
// frontmatter resync, leaving progress.* stale. Force a body diff
|
||||
// ONLY when a phase directory was actually removed (targetDir !== null)
|
||||
// so the guard passes and syncStateFrontmatter rebuilds the frontmatter
|
||||
// from the post-deletion disk/ROADMAP state. Without the targetDir gate,
|
||||
// a no-op removal (ROADMAP-only phase, no directory) would inject a
|
||||
// spurious 'Total Phases:' line into a body that intentionally lacked one.
|
||||
if (targetDir && modified === stateContent) {
|
||||
// subdirs was read before the deletion; excluding the removed target
|
||||
// gives the remaining count. Renumbering changes names but not count.
|
||||
const remainingPhases = subdirs.filter(
|
||||
(d) => phaseTokenMatches(d, normalized) === false,
|
||||
).length;
|
||||
if (totalRaw) {
|
||||
modified =
|
||||
stateReplaceField(modified, 'Total Phases', String(remainingPhases)) || modified;
|
||||
} else {
|
||||
// No 'Total Phases:' field in the body — append one so the no-op
|
||||
// guard sees a diff. syncStateFrontmatter will then rebuild the
|
||||
// frontmatter progress.* block from the real disk/ROADMAP count.
|
||||
modified = `Total Phases: ${remainingPhases}\n` + modified;
|
||||
}
|
||||
}
|
||||
return modified;
|
||||
},
|
||||
cwd,
|
||||
);
|
||||
@@ -1628,7 +1660,7 @@ function cmdPhaseRemove(
|
||||
renamed_directories: renamedDirs,
|
||||
renamed_files: renamedFiles,
|
||||
roadmap_updated: true,
|
||||
state_updated: fs.existsSync(statePath),
|
||||
state_updated: stateUpdated,
|
||||
},
|
||||
raw,
|
||||
);
|
||||
|
||||
@@ -2213,7 +2213,7 @@ function writeStateMd(statePath: string, content: string, cwd?: string, clock?:
|
||||
* @param clock
|
||||
* Optional clock seam; defaults to realClock. Passed through to acquireStateLock.
|
||||
*/
|
||||
function readModifyWriteStateMd(statePath: string, transformFn: (content: string) => string, cwd: string, options?: ReadModifyWriteOptions, clock?: StateLockClock): void {
|
||||
function readModifyWriteStateMd(statePath: string, transformFn: (content: string) => string, cwd: string, options?: ReadModifyWriteOptions, clock?: StateLockClock): boolean {
|
||||
const resync = !options || options.resync !== false;
|
||||
const lockPath = acquireStateLock(statePath, clock);
|
||||
try {
|
||||
@@ -2261,7 +2261,7 @@ function readModifyWriteStateMd(statePath: string, transformFn: (content: string
|
||||
// content already returns the mutated string, and callers that detect a
|
||||
// no-op explicitly return the original content unchanged.
|
||||
if (modified === content) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
let synced = syncStateFrontmatter(modified, cwd, options?.authoritativeFm);
|
||||
@@ -2317,6 +2317,7 @@ function readModifyWriteStateMd(statePath: string, transformFn: (content: string
|
||||
}
|
||||
|
||||
platformWriteSync(statePath, synced);
|
||||
return true;
|
||||
} finally {
|
||||
releaseStateLock(lockPath);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user