- Extend cmdStatePrune to prune Performance Metrics table rows older than cutoff - Add workflow.auto_prune_state config key (default: false) - Call cmdStatePrune automatically in cmdPhaseComplete when enabled - Document workflow.auto_prune_state in planning-config.md reference - Add silent option to cmdStatePrune for programmatic use without stdout Closes #2087 Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -22,6 +22,7 @@ const VALID_CONFIG_KEYS = new Set([
|
||||
'workflow.research_before_questions',
|
||||
'workflow.discuss_mode',
|
||||
'workflow.skip_discuss',
|
||||
'workflow.auto_prune_state',
|
||||
'workflow._auto_chain_active',
|
||||
'workflow.use_worktrees',
|
||||
'workflow.code_review',
|
||||
@@ -170,6 +171,7 @@ function buildNewProjectConfig(userChoices) {
|
||||
plan_bounce: false,
|
||||
plan_bounce_script: null,
|
||||
plan_bounce_passes: 2,
|
||||
auto_prune_state: false,
|
||||
},
|
||||
hooks: {
|
||||
context_warnings: true,
|
||||
|
||||
@@ -937,6 +937,21 @@ function cmdPhaseComplete(cwd, phaseNum, raw) {
|
||||
}, cwd);
|
||||
}
|
||||
|
||||
// Auto-prune STATE.md on phase boundary when configured (#2087)
|
||||
let autoPruned = false;
|
||||
try {
|
||||
const configPath = path.join(planningDir(cwd), 'config.json');
|
||||
if (fs.existsSync(configPath)) {
|
||||
const rawConfig = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
||||
const autoPruneEnabled = rawConfig.workflow && rawConfig.workflow.auto_prune_state === true;
|
||||
if (autoPruneEnabled && fs.existsSync(statePath)) {
|
||||
const { cmdStatePrune } = require('./state.cjs');
|
||||
cmdStatePrune(cwd, { keepRecent: '3', dryRun: false, silent: true }, true);
|
||||
autoPruned = true;
|
||||
}
|
||||
}
|
||||
} catch { /* intentionally empty — auto-prune is best-effort */ }
|
||||
|
||||
const result = {
|
||||
completed_phase: phaseNum,
|
||||
phase_name: phaseInfo.phase_name,
|
||||
@@ -948,6 +963,7 @@ function cmdPhaseComplete(cwd, phaseNum, raw) {
|
||||
roadmap_updated: fs.existsSync(roadmapPath),
|
||||
state_updated: fs.existsSync(statePath),
|
||||
requirements_updated: requirementsUpdated,
|
||||
auto_pruned: autoPruned,
|
||||
warnings,
|
||||
has_warnings: warnings.length > 0,
|
||||
};
|
||||
|
||||
@@ -1417,8 +1417,10 @@ function cmdStateSync(cwd, options, raw) {
|
||||
* dryRun: if true, return what would be pruned without modifying STATE.md
|
||||
*/
|
||||
function cmdStatePrune(cwd, options, raw) {
|
||||
const silent = !!options.silent;
|
||||
const emit = silent ? () => {} : (result, r, v) => output(result, r, v);
|
||||
const statePath = planningPaths(cwd).state;
|
||||
if (!fs.existsSync(statePath)) { output({ error: 'STATE.md not found' }, raw); return; }
|
||||
if (!fs.existsSync(statePath)) { emit({ error: 'STATE.md not found' }, raw); return; }
|
||||
|
||||
const keepRecent = parseInt(options.keepRecent, 10) || 3;
|
||||
const dryRun = !!options.dryRun;
|
||||
@@ -1427,7 +1429,7 @@ function cmdStatePrune(cwd, options, raw) {
|
||||
const cutoff = currentPhase - keepRecent;
|
||||
|
||||
if (cutoff <= 0) {
|
||||
output({ pruned: false, reason: `Only ${currentPhase} phases — nothing to prune with --keep-recent ${keepRecent}` }, raw, 'false');
|
||||
emit({ pruned: false, reason: `Only ${currentPhase} phases — nothing to prune with --keep-recent ${keepRecent}` }, raw, 'false');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1504,6 +1506,35 @@ function cmdStatePrune(cwd, options, raw) {
|
||||
}
|
||||
}
|
||||
|
||||
// Prune Performance Metrics table rows: keep only rows for phases > cutoff.
|
||||
// Preserves header rows (| Phase | ... and |---|...) and any prose around the table.
|
||||
const metricsPattern = /(###?\s*Performance Metrics\s*\n)([\s\S]*?)(?=\n###?|\n##[^#]|$)/i;
|
||||
const metricsMatch = content.match(metricsPattern);
|
||||
if (metricsMatch) {
|
||||
const sectionLines = metricsMatch[2].split('\n');
|
||||
const keep = [];
|
||||
const archive = [];
|
||||
for (const line of sectionLines) {
|
||||
// Table data row: starts with | followed by a number (phase)
|
||||
const tableRowMatch = line.match(/^\|\s*(\d+)\s*\|/);
|
||||
if (tableRowMatch) {
|
||||
const rowPhase = parseInt(tableRowMatch[1], 10);
|
||||
if (rowPhase <= cutoff) {
|
||||
archive.push(line);
|
||||
} else {
|
||||
keep.push(line);
|
||||
}
|
||||
} else {
|
||||
// Header row, separator row, or prose — always keep
|
||||
keep.push(line);
|
||||
}
|
||||
}
|
||||
if (archive.length > 0) {
|
||||
sections.push({ section: 'Performance Metrics', count: archive.length, lines: archive });
|
||||
content = content.replace(metricsPattern, (_m, header) => `${header}${keep.join('\n')}`);
|
||||
}
|
||||
}
|
||||
|
||||
return { newContent: content, archivedSections: sections };
|
||||
}
|
||||
|
||||
@@ -1512,7 +1543,7 @@ function cmdStatePrune(cwd, options, raw) {
|
||||
const content = fs.readFileSync(statePath, 'utf-8');
|
||||
const result = prunePass(content);
|
||||
const totalPruned = result.archivedSections.reduce((sum, s) => sum + s.count, 0);
|
||||
output({
|
||||
emit({
|
||||
pruned: false,
|
||||
dry_run: true,
|
||||
cutoff_phase: cutoff,
|
||||
@@ -1547,7 +1578,7 @@ function cmdStatePrune(cwd, options, raw) {
|
||||
}
|
||||
|
||||
const totalPruned = archived.reduce((sum, s) => sum + s.count, 0);
|
||||
output({
|
||||
emit({
|
||||
pruned: totalPruned > 0,
|
||||
cutoff_phase: cutoff,
|
||||
keep_recent: keepRecent,
|
||||
|
||||
@@ -248,6 +248,7 @@ Set via `workflow.*` namespace in config.json (e.g., `"workflow": { "research":
|
||||
| `workflow.plan_check` | boolean | `true` | `true`, `false` | Run plan-checker agent to validate plans. _Alias:_ `plan_checker` is the flat-key form used in `CONFIG_DEFAULTS`; `workflow.plan_check` is the canonical namespaced form. |
|
||||
| `workflow.verifier` | boolean | `true` | `true`, `false` | Run verifier agent after execution |
|
||||
| `workflow.nyquist_validation` | boolean | `true` | `true`, `false` | Enable Nyquist-inspired validation gates |
|
||||
| `workflow.auto_prune_state` | boolean | `false` | `true`, `false` | Automatically prune old STATE.md entries on phase completion (keeps 3 most recent phases) |
|
||||
| `workflow.auto_advance` | boolean | `false` | `true`, `false` | Auto-advance to next phase after completion |
|
||||
| `workflow.node_repair` | boolean | `true` | `true`, `false` | Attempt automatic repair of failed plan nodes |
|
||||
| `workflow.node_repair_budget` | number | `2` | Any positive integer | Max repair retries per failed node |
|
||||
|
||||
Reference in New Issue
Block a user