fix(state): record-metric/add-decision: auto-create sections + workstream routing (#3286) (#3291)

* test: state record-metric/add-decision: auto-create + workstream routing (#3286 RED)

Three failing test groups:
- Bug A: silent no-op contract (exit 0 with recorded/added:false)
- Bug B: auto-create ## Performance Metrics / ## Decisions when absent
- Bug C: --ws routing writes to workstream STATE.md, not root

* fix(state): workstream-route + auto-create sections in record-metric/add-decision (#3286)

Bug B: cmdStateRecordMetric now auto-creates ## Performance Metrics (with
canonical table header) when absent, instead of silently returning
{ recorded: false }. cmdStateAddDecision does the same for ## Decisions.
Both functions return created: true in the JSON when the section was
newly scaffolded — matching the DWIM behavior of state begin-phase and
advance-plan.

Bug A: silent no-op disappears — sections are auto-created, so recorded/added
is always true on valid input.

Bug C: workstream routing via planningPaths(cwd) already reads GSD_WORKSTREAM
which gsd-tools.cjs sets from --ws before calling state handlers. Confirmed
by the passing --ws routing tests.

Fixes #3286

* refactor(state): extend DWIM auto-create to cmdStateAddBlocker (k302/k014)

Applies the same section auto-create pattern from record-metric/add-decision
to add-blocker: when ## Blockers / ### Blockers is absent, auto-scaffold it
instead of silently returning added:false. Returns created:true in the JSON
when newly scaffolded, matching the uniform shape across all three write verbs.

Per k014 (duplicate algorithm drift), the pattern is now applied uniformly
across all cmdState* functions that mutate a named section.

* changeset: pr=3291 for #3286

* fix(state): remove dead else-branches flagged by CodeRabbit (#3291)

After the auto-create fallback, recorded/added is always true — the else
blocks emitting { recorded: false } / { added: false } were unreachable.
Removed all three dead branches (cmdStateRecordMetric, cmdStateAddDecision,
cmdStateAddBlocker) and replaced with an explanatory comment per CR nitpick.
This commit is contained in:
Tom Boucher
2026-05-09 07:40:56 -04:00
committed by GitHub
parent b08e57c92d
commit 6c27b2b338
3 changed files with 458 additions and 19 deletions

View File

@@ -400,14 +400,16 @@ function cmdStateRecordMetric(cwd, options, raw) {
}
let recorded = false;
let created = false;
readModifyWriteStateMd(statePath, (content) => {
// Find Performance Metrics section and its table
const metricsPattern = /(##\s*Performance Metrics[\s\S]*?\n\|[^\n]+\n\|[-|\s]+\n)([\s\S]*?)(?=\n##|\n$|$)/i;
const metricsMatch = content.match(metricsPattern);
const newRow = `| Phase ${phase} P${plan} | ${duration} | ${tasks || '-'} tasks | ${files || '-'} files |`;
if (metricsMatch) {
let tableBody = metricsMatch[2].trimEnd();
const newRow = `| Phase ${phase} P${plan} | ${duration} | ${tasks || '-'} tasks | ${files || '-'} files |`;
if (tableBody.trim() === '' || tableBody.includes('None yet')) {
tableBody = newRow;
@@ -418,14 +420,27 @@ function cmdStateRecordMetric(cwd, options, raw) {
recorded = true;
return content.replace(metricsPattern, (_match, header) => `${header}${tableBody}\n`);
}
return content;
// Section absent — DWIM: auto-create canonical ## Performance Metrics scaffold,
// then append the row. Matches state begin-phase / advance-plan DWIM behavior.
const scaffold = [
'',
'## Performance Metrics',
'',
'| Phase | Plan | Duration | Notes |',
'|-------|------|----------|-------|',
newRow,
'',
].join('\n');
recorded = true;
created = true;
return content.trimEnd() + '\n' + scaffold;
}, cwd);
if (recorded) {
output({ recorded: true, phase, plan, duration }, raw, 'true');
} else {
output({ recorded: false, reason: 'Performance Metrics section not found in STATE.md' }, raw, 'false');
}
// Auto-create fallback guarantees recorded === true; no else branch needed.
const result = { recorded: true, phase, plan, duration };
if (created) result.created = true;
output(result, raw, 'true');
}
function cmdStateUpdateProgress(cwd, raw) {
@@ -500,6 +515,7 @@ function cmdStateAddDecision(cwd, options, raw) {
const entry = `- [Phase ${phase || '?'}]: ${summaryText}${rationaleText ? ` — ${rationaleText}` : ''}`;
let added = false;
let created = false;
readModifyWriteStateMd(statePath, (content) => {
// Find Decisions section (various heading patterns)
@@ -514,14 +530,25 @@ function cmdStateAddDecision(cwd, options, raw) {
added = true;
return content.replace(sectionPattern, (_match, header) => `${header}${sectionBody}`);
}
return content;
// Section absent — DWIM: auto-create canonical ## Decisions scaffold,
// then append the entry. Matches state begin-phase / advance-plan DWIM behavior.
const scaffold = [
'',
'## Decisions',
'',
entry,
'',
].join('\n');
added = true;
created = true;
return content.trimEnd() + '\n' + scaffold;
}, cwd);
if (added) {
output({ added: true, decision: entry }, raw, 'true');
} else {
output({ added: false, reason: 'Decisions section not found in STATE.md' }, raw, 'false');
}
// Auto-create fallback guarantees added === true; no else branch needed.
const result = { added: true, decision: entry };
if (created) result.created = true;
output(result, raw, 'true');
}
function cmdStateAddBlocker(cwd, text, raw) {
@@ -541,6 +568,7 @@ function cmdStateAddBlocker(cwd, text, raw) {
const entry = `- ${blockerText}`;
let added = false;
let created = false;
readModifyWriteStateMd(statePath, (content) => {
const sectionPattern = /(###?\s*(?:Blockers|Blockers\/Concerns|Concerns)\s*\n)([\s\S]*?)(?=\n###?|\n##[^#]|$)/i;
@@ -553,14 +581,24 @@ function cmdStateAddBlocker(cwd, text, raw) {
added = true;
return content.replace(sectionPattern, (_match, header) => `${header}${sectionBody}`);
}
return content;
// Section absent — DWIM: auto-create canonical ### Blockers scaffold.
const scaffold = [
'',
'### Blockers',
'',
entry,
'',
].join('\n');
added = true;
created = true;
return content.trimEnd() + '\n' + scaffold;
}, cwd);
if (added) {
output({ added: true, blocker: blockerText }, raw, 'true');
} else {
output({ added: false, reason: 'Blockers section not found in STATE.md' }, raw, 'false');
}
// Auto-create fallback guarantees added === true; no else branch needed.
const result = { added: true, blocker: blockerText };
if (created) result.created = true;
output(result, raw, 'true');
}
function cmdStateResolveBlocker(cwd, text, raw) {