refactor: consolidate STATE.md field helpers, fix command injection in isGitIgnored

Refactoring:
- Extract stateReplaceFieldWithFallback() to state.cjs as single source of truth
  for the try-primary-then-fallback pattern that was duplicated inline across
  phase.cjs, milestone.cjs, and state.cjs
- Replace all inline bold-only regex patterns in cmdPhaseComplete with shared
  stateReplaceField/stateExtractField helpers — now supports both **Bold:**
  and plain Field: STATE.md formats (fixes the same bug as #924)
- Replace inline bold-only regex patterns in cmdMilestoneComplete with shared helpers
- Replace inline bold-only regex for Completed Phases/Total Phases/Progress
  counters in cmdPhaseComplete with stateExtractField/stateReplaceField
- Replace inline bold-only Total Phases regex in cmdPhaseRemove with shared helpers

Security:
- Fix command injection surface in isGitIgnored (core.cjs): replace execSync with
  string concatenation with execFileSync using array arguments — prevents shell
  interpretation of special characters in file paths

Tests (7 new):
- 5 tests for stateReplaceFieldWithFallback: primary field, fallback, neither,
  preference, and plain format
- 1 regression test: phase complete with plain-format STATE.md fields
- 1 regression test: milestone complete with plain-format STATE.md fields

854 tests pass (was 847). No behavioral regressions.
This commit is contained in:
Tom Boucher
2026-03-19 15:05:20 -04:00
parent 0afffb15f5
commit 3e2c85e6fd
7 changed files with 152 additions and 75 deletions

View File

@@ -201,6 +201,22 @@ function stateReplaceField(content, fieldName, newValue) {
return null;
}
/**
* Replace a STATE.md field with fallback field name support.
* Tries `primary` first, then `fallback` (if provided), returns content unchanged
* if neither matches. This consolidates the replaceWithFallback pattern that was
* previously duplicated inline across phase.cjs, milestone.cjs, and state.cjs.
*/
function stateReplaceFieldWithFallback(content, primary, fallback, value) {
let result = stateReplaceField(content, primary, value);
if (result) return result;
if (fallback) {
result = stateReplaceField(content, fallback, value);
if (result) return result;
}
return content;
}
function cmdStateAdvancePlan(cwd, raw) {
const statePath = planningPaths(cwd).state;
if (!fs.existsSync(statePath)) { output({ error: 'STATE.md not found' }, raw); return; }
@@ -232,16 +248,9 @@ function cmdStateAdvancePlan(cwd, raw) {
return;
}
const replaceField = (c, primary, fallback, value) => {
let r = stateReplaceField(c, primary, value);
if (r) return r;
if (fallback) { r = stateReplaceField(c, fallback, value); if (r) return r; }
return c;
};
if (currentPlan >= totalPlans) {
content = replaceField(content, 'Status', null, 'Phase complete — ready for verification');
content = replaceField(content, 'Last Activity', 'Last activity', today);
content = stateReplaceFieldWithFallback(content, 'Status', null, 'Phase complete — ready for verification');
content = stateReplaceFieldWithFallback(content, 'Last Activity', 'Last activity', today);
writeStateMd(statePath, content, cwd);
output({ advanced: false, reason: 'last_plan', current_plan: currentPlan, total_plans: totalPlans, status: 'ready_for_verification' }, raw, 'false');
} else {
@@ -253,8 +262,8 @@ function cmdStateAdvancePlan(cwd, raw) {
} else {
content = stateReplaceField(content, 'Current Plan', String(newPlan)) || content;
}
content = replaceField(content, 'Status', null, 'Ready to execute');
content = replaceField(content, 'Last Activity', 'Last activity', today);
content = stateReplaceFieldWithFallback(content, 'Status', null, 'Ready to execute');
content = stateReplaceFieldWithFallback(content, 'Last Activity', 'Last activity', today);
writeStateMd(statePath, content, cwd);
output({ advanced: true, previous_plan: currentPlan, current_plan: newPlan, total_plans: totalPlans }, raw, 'true');
}
@@ -906,6 +915,7 @@ function cmdSignalResume(cwd, raw) {
module.exports = {
stateExtractField,
stateReplaceField,
stateReplaceFieldWithFallback,
writeStateMd,
cmdStateLoad,
cmdStateGet,