feat(state): add metrics table pruning and auto-prune on phase complete (#2087) (#2120)

- 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:
Tom Boucher
2026-04-11 15:02:55 -04:00
committed by GitHub
parent e24cb18b72
commit 805696bd03
6 changed files with 352 additions and 4 deletions

View File

@@ -149,4 +149,129 @@ describe('state prune (#1970)', () => {
const output = JSON.parse(result.output);
assert.strictEqual(output.pruned, false);
});
describe('Performance Metrics table pruning (#2087)', () => {
test('prunes old metric table rows by phase number', () => {
writeStateMd(tmpDir, [
'# Session State',
'',
'**Current Phase:** 10',
'',
'## Performance Metrics',
'',
'| Phase | Plans | Duration | Status |',
'|-------|-------|----------|--------|',
'| 1 | 3/3 | 2h | Complete |',
'| 2 | 2/2 | 1h | Complete |',
'| 3 | 4/4 | 3h | Complete |',
'| 8 | 5/5 | 4h | Complete |',
'| 9 | 2/2 | 1h | Complete |',
'| 10 | 1/3 | - | In Progress |',
'',
].join('\n'));
const result = runGsdTools('state prune --keep-recent 3', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.pruned, true);
const newState = readStateMd(tmpDir);
// Should keep phases 8, 9, 10 (within keep-recent of phase 10, cutoff=7)
assert.match(newState, /\| 8 \|/);
assert.match(newState, /\| 9 \|/);
assert.match(newState, /\| 10 \|/);
// Should prune phases 1, 2, 3
assert.doesNotMatch(newState, /\| 1 \|.*Complete/);
assert.doesNotMatch(newState, /\| 2 \|.*Complete/);
assert.doesNotMatch(newState, /\| 3 \|.*Complete/);
// Header row should be preserved
assert.match(newState, /\| Phase \| Plans \| Duration \| Status \|/);
assert.match(newState, /\|-------|-------|----------|--------\|/);
});
test('--dry-run reports metrics rows that would be pruned', () => {
writeStateMd(tmpDir, [
'# Session State',
'',
'**Current Phase:** 8',
'',
'## Performance Metrics',
'',
'| Phase | Plans | Status |',
'|-------|-------|--------|',
'| 1 | 3/3 | Complete |',
'| 2 | 2/2 | Complete |',
'| 6 | 4/4 | Complete |',
'| 7 | 2/2 | Complete |',
'| 8 | 1/3 | In Progress |',
'',
].join('\n'));
const result = runGsdTools('state prune --keep-recent 3 --dry-run', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const output = JSON.parse(result.output);
assert.strictEqual(output.dry_run, true);
assert.ok(output.total_would_archive > 0, 'should report rows to archive');
const metricsSection = output.sections.find(s => /Metrics/i.test(s.section));
assert.ok(metricsSection, 'should include Performance Metrics section');
assert.strictEqual(metricsSection.entries_would_archive, 2);
});
test('does not touch prose lines outside the metrics table', () => {
writeStateMd(tmpDir, [
'# Session State',
'',
'**Current Phase:** 10',
'',
'## Performance Metrics',
'',
'Overall project velocity is improving.',
'',
'| Phase | Plans | Status |',
'|-------|-------|--------|',
'| 1 | 3/3 | Complete |',
'| 9 | 2/2 | Complete |',
'| 10 | 1/3 | In Progress |',
'',
'Average duration: 2.5 hours per phase.',
'',
].join('\n'));
const result = runGsdTools('state prune --keep-recent 3', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const newState = readStateMd(tmpDir);
assert.match(newState, /Overall project velocity is improving\./);
assert.match(newState, /Average duration: 2\.5 hours per phase\./);
assert.doesNotMatch(newState, /\| 1 \|/);
assert.match(newState, /\| 9 \|/);
});
test('preserves table when no rows are old enough to prune', () => {
writeStateMd(tmpDir, [
'# Session State',
'',
'**Current Phase:** 5',
'',
'## Performance Metrics',
'',
'| Phase | Plans | Status |',
'|-------|-------|--------|',
'| 3 | 3/3 | Complete |',
'| 4 | 2/2 | Complete |',
'| 5 | 1/3 | In Progress |',
'',
].join('\n'));
const result = runGsdTools('state prune --keep-recent 3', tmpDir);
assert.ok(result.success, `Command failed: ${result.error}`);
const newState = readStateMd(tmpDir);
assert.match(newState, /\| 3 \|/);
assert.match(newState, /\| 4 \|/);
assert.match(newState, /\| 5 \|/);
});
});
});