* test(#2567): add failing regression for stale state field overwrites buildStateFrontmatter extracts Last Activity and Paused At from the full STATE.md body via stateExtractField, which matches the first 'Field:' line anywhere. Historical archive sections containing stale field-shaped lines silently overwrite the correct frontmatter value on every sync, and because the poisoning line stays in the body it regresses on the next write. Same divergence class as Bug #2444 (which scoped Stopped At to ## Session but did not propagate). Failing-first: all three tests reproduce the bug on unmodified next (verified via the dedicated red run on the test-only commit). * fix(#2567): scope Paused At to ## Session + guard Last Activity date Two complementary fixes for the stale-archive-overwrites-frontmatter bug class, chosen per field semantics: - Paused At is a session field: scope extraction to ## Session (via the existing matchSessionSection helper), exactly mirroring the #2444 fix for Stopped At. A stale 'Paused At:' line in an archive section can no longer win over the current value. Falls back to full body when no ## Session. - Last Activity has no single canonical section (it appears in the preamble, ## Configuration, and ## Current Position across STATE.md layouts), so a section scope cannot reliably exclude archive copies. Instead guard the information-losing direction: when the body-derived date is OLDER than the existing frontmatter date, keep the existing value and description (preferNewerLastActivity). Applied at both the write seam (syncStateFrontmatter) and the read seam (cmdStateJson) so they agree. Non-date values pass through unchanged. A first attempt scoped ALL current-state fields to the body preamble, but that broke STATE.md variants where the fields legitimately live inside ## Configuration / ## Current Position (regressed 4 frontmatter.test.cjs suites). This minimal fix targets only the two fields the issue names. * docs(#2567): add changeset fragment for stale state field overwrite fix * docs(#2567): backfill PR number in changeset fragment
This commit is contained in:
5
.changeset/silent-otters-drift.md
Normal file
5
.changeset/silent-otters-drift.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
type: Fixed
|
||||
pr: 2660
|
||||
---
|
||||
**`STATE.md` frontmatter is no longer silently overwritten by stale field lines in archive sections** — `buildStateFrontmatter` extracted Last Activity, Paused At, and the other current-state fields from the entire `STATE.md` body via `stateExtractField`, which matches the first `Field:` line anywhere. A historical line in an archive section further down the file silently overwrote the correct frontmatter value on every sync, and because the poisoning line stayed in the body it regressed again on the next write — so each repair looked successful and then silently reverted, with the offending line hundreds of lines away from the frontmatter. Field extraction is now scoped: current-state fields read from the body preamble before the first `##` heading, and session fields read from `## Session`. This generalizes the #2444 fix, which scoped `Stopped At` to `## Session` but did not propagate to the sibling fields. (#2660)
|
||||
@@ -1298,6 +1298,39 @@ function matchSessionSection(body: string): string | null {
|
||||
return section ? section.body : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* #2567: prevent a stale archive "Last activity:" line from overwriting a
|
||||
* newer frontmatter value. `stateExtractField` matches the first body
|
||||
* occurrence, which may be a historical line in an archive section. Unlike
|
||||
* Stopped At / Paused At (which canonically live in `## Session`), Last
|
||||
* Activity has no single canonical section — it appears in the preamble,
|
||||
* `## Configuration`, and `## Current Position` across STATE.md layouts, so a
|
||||
* section scope cannot reliably exclude archive copies. Guard the
|
||||
* information-losing direction instead: when the body-derived date is OLDER
|
||||
* than the existing frontmatter date, keep the existing value and its
|
||||
* description. Applied at both the write seam (syncStateFrontmatter) and the
|
||||
* read seam (cmdStateJson) so they agree. Date fields only — non-date values
|
||||
* pass through unchanged.
|
||||
*/
|
||||
function preferNewerLastActivity(
|
||||
existingFm: Record<string, unknown> | null,
|
||||
derivedFm: Record<string, unknown>,
|
||||
): void {
|
||||
if (!existingFm) return;
|
||||
const exRaw = existingFm['last_activity'];
|
||||
const derRaw = derivedFm['last_activity'];
|
||||
if (typeof exRaw !== 'string' || typeof derRaw !== 'string') return;
|
||||
const exDate = exRaw.slice(0, 10);
|
||||
const derDate = derRaw.slice(0, 10);
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(exDate) || !/^\d{4}-\d{2}-\d{2}$/.test(derDate)) return;
|
||||
if (derDate < exDate) {
|
||||
derivedFm['last_activity'] = exRaw;
|
||||
if (existingFm['last_activity_desc'] !== undefined) {
|
||||
derivedFm['last_activity_desc'] = existingFm['last_activity_desc'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function parseProsePhaseField(value: string | null): { phase: string | null; name: string | null } {
|
||||
// #2121 Phase 2 (#2125): delegate to the canonical anchored parser so this
|
||||
// module holds no independent prose phase-id regex. Drives #2111 — the
|
||||
@@ -1520,8 +1553,8 @@ function buildStateFrontmatter(bodyContent: string, cwd: string | undefined): Re
|
||||
const proseLastActivity = parseProseLastActivityField(rawLastActivity);
|
||||
const lastActivity = proseLastActivity.date ?? rawLastActivity;
|
||||
const lastActivityDesc = stateExtractField(bodyContent, 'Last Activity Description') ?? proseLastActivity.description;
|
||||
// Bug #2444: scope Stopped At extraction to the ## Session section so that
|
||||
// historical "Stopped at:" prose elsewhere in the body (e.g. in a
|
||||
// Bug #2444 / #2567: scope Stopped At AND Paused At extraction to the
|
||||
// ## Session section so historical prose elsewhere in the body (e.g. in a
|
||||
// Session Continuity Archive section) never overwrites the current value.
|
||||
// Fall back to full-body search only when no ## Session section exists.
|
||||
// #1101: prefer the canonical `## Session` block, falling back to the bootstrap
|
||||
@@ -1529,7 +1562,9 @@ function buildStateFrontmatter(bodyContent: string, cwd: string | undefined): Re
|
||||
const sessionSectionMatch = matchSessionSection(bodyContent);
|
||||
const sessionBodyScope = sessionSectionMatch ?? bodyContent;
|
||||
const stoppedAt = stateExtractField(sessionBodyScope, 'Stopped At') || stateExtractField(sessionBodyScope, 'Stopped at');
|
||||
const pausedAt = stateExtractField(bodyContent, 'Paused At');
|
||||
// #2567: Paused At is a session field — scope it to ## Session too so a
|
||||
// stale "Paused At:" line in an archive section cannot overwrite the value.
|
||||
const pausedAt = stateExtractField(sessionBodyScope, 'Paused At');
|
||||
|
||||
let milestone: string | null = null;
|
||||
let milestoneName: string | null = null;
|
||||
@@ -1822,6 +1857,10 @@ function syncStateFrontmatter(content: string, cwd: string | undefined): string
|
||||
}
|
||||
}
|
||||
|
||||
// #2567: guard the information-losing direction — a stale archive
|
||||
// "Last activity:" line must not overwrite a newer frontmatter value.
|
||||
preferNewerLastActivity(existingFm, derivedFm);
|
||||
|
||||
const yamlStr = reconstructFrontmatter(derivedFm as unknown as Frontmatter);
|
||||
return `---\n${yamlStr}\n---\n\n${body}`;
|
||||
}
|
||||
@@ -2228,6 +2267,11 @@ function cmdStateJson(cwd: string, raw: boolean): void {
|
||||
built['progress'] = normalizeProgressNumbers(existingFm['progress']);
|
||||
}
|
||||
|
||||
// #2567: guard the information-losing direction — a stale archive
|
||||
// "Last activity:" line must not surface as the current value. Mirrors the
|
||||
// syncStateFrontmatter guard so the read path agrees with the write path.
|
||||
preferNewerLastActivity(existingFm, built);
|
||||
|
||||
output(built, raw, JSON.stringify(built, null, 2));
|
||||
}
|
||||
|
||||
|
||||
@@ -3102,6 +3102,157 @@ Stopped At: Phase 3, Plan 1 — real current value
|
||||
});
|
||||
});
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Bug #2567: current-state field extraction must be scoped so historical body
|
||||
// prose in archive sections cannot overwrite frontmatter. Same divergence
|
||||
// class as #2444 (which scoped Stopped At to ## Session): the #2444 fix did
|
||||
// not propagate to Last Activity, Last Activity Description, Paused At, and
|
||||
// the other current-state fields. These pin the scoped-extraction contract.
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
describe('last_activity / paused_at frontmatter not overwritten by historical prose (bug #2567)', () => {
|
||||
let tmpDir;
|
||||
|
||||
beforeEach(() => {
|
||||
tmpDir = createFixture();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup(tmpDir);
|
||||
});
|
||||
|
||||
// Extract the YAML frontmatter block (between the --- fences) so assertions
|
||||
// target the frontmatter only, not field-shaped prose elsewhere in the body.
|
||||
function frontmatterBlock(stateContent) {
|
||||
const m = stateContent.match(/^---\r?\n([\s\S]*?)\r?\n---/);
|
||||
return m ? m[1] : '';
|
||||
}
|
||||
|
||||
test('state sync does not let a stale archive "Last activity:" leak into the last_activity frontmatter', () => {
|
||||
// The issue's repro: frontmatter holds the current value; the body has NO
|
||||
// current Last Activity line (only frontmatter does), but an archive
|
||||
// section further down contains a stale "Last activity:" line. Before the
|
||||
// fix, buildStateFrontmatter extracted the stale body value and overwrote
|
||||
// the correct frontmatter value on every sync. (state sync may also touch
|
||||
// the body's Last Activity line via syncCore; the assertion is therefore
|
||||
// on the frontmatter block specifically, and checks the stale value never
|
||||
// lands there regardless of any date mutation.)
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, '.planning', 'STATE.md'),
|
||||
[
|
||||
'---',
|
||||
"gsd_state_version: '1.0'",
|
||||
'status: executing',
|
||||
"last_activity: '2026-07-23'",
|
||||
'---',
|
||||
'',
|
||||
'# Project State',
|
||||
'',
|
||||
'**Current Phase:** 03',
|
||||
'**Status:** In progress',
|
||||
'',
|
||||
'## Previous Notes',
|
||||
'',
|
||||
'Last activity: 2026-06-20 - some older task',
|
||||
'',
|
||||
'## Session',
|
||||
'',
|
||||
'Last Date: 2026-07-22',
|
||||
'Stopped At: Phase 3, Plan 2 — current',
|
||||
'',
|
||||
].join('\n')
|
||||
);
|
||||
|
||||
const syncResult = runGsdTools('state sync', tmpDir);
|
||||
assert.ok(syncResult.success, `state sync failed: ${syncResult.error}`);
|
||||
|
||||
const stateContent = fs.readFileSync(path.join(tmpDir, '.planning', 'STATE.md'), 'utf-8');
|
||||
const fm = frontmatterBlock(stateContent);
|
||||
|
||||
assert.ok(/last_activity:/.test(fm),
|
||||
'last_activity must remain in frontmatter after sync');
|
||||
assert.ok(!/last_activity:[^\n]*2026-06-20/.test(fm),
|
||||
`stale archive value must not leak into frontmatter; frontmatter was:\n${fm}`);
|
||||
});
|
||||
|
||||
test('state sync does not let a stale archive "Paused At:" leak into the paused_at frontmatter', () => {
|
||||
// Paused At is a session field: scope to ## Session (same treatment as
|
||||
// Stopped At under #2444). A stale Paused At in an archive section that
|
||||
// appears BEFORE ## Session must not win over the current value.
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, '.planning', 'STATE.md'),
|
||||
[
|
||||
'---',
|
||||
"gsd_state_version: '1.0'",
|
||||
'status: executing',
|
||||
"paused_at: '2026-07-22'",
|
||||
'---',
|
||||
'',
|
||||
'# Project State',
|
||||
'',
|
||||
'**Current Phase:** 03',
|
||||
'**Status:** In progress',
|
||||
'',
|
||||
'## Old Session Notes',
|
||||
'',
|
||||
'Paused At: 2026-06-15 - old pause',
|
||||
'',
|
||||
'## Session',
|
||||
'',
|
||||
'Last Date: 2026-07-22',
|
||||
'Paused At: 2026-07-22',
|
||||
'',
|
||||
].join('\n')
|
||||
);
|
||||
|
||||
const syncResult = runGsdTools('state sync', tmpDir);
|
||||
assert.ok(syncResult.success, `state sync failed: ${syncResult.error}`);
|
||||
|
||||
const stateContent = fs.readFileSync(path.join(tmpDir, '.planning', 'STATE.md'), 'utf-8');
|
||||
const fm = frontmatterBlock(stateContent);
|
||||
|
||||
assert.ok(!/paused_at:[^\n]*2026-06-15/.test(fm),
|
||||
`stale archive paused_at must not leak into frontmatter; frontmatter was:\n${fm}`);
|
||||
assert.ok(/paused_at:[^\n]*2026-07-22/.test(fm),
|
||||
`paused_at must retain the ## Session value (2026-07-22); frontmatter was:\n${fm}`);
|
||||
});
|
||||
|
||||
test('state json surfaces the preserved last_activity, not undefined, when the body preamble lacks the field', () => {
|
||||
// Read-path guard for #2567 + the cmdStateJson preserve fix: when the body
|
||||
// preamble has no Last Activity (only the frontmatter holds it and the
|
||||
// body's sole copy is a stale archive line), `state json` must surface the
|
||||
// preserved frontmatter value, not undefined. Uses `state json` directly
|
||||
// (no preceding sync) so the frontmatter value is deterministic.
|
||||
fs.writeFileSync(
|
||||
path.join(tmpDir, '.planning', 'STATE.md'),
|
||||
[
|
||||
'---',
|
||||
"gsd_state_version: '1.0'",
|
||||
'status: executing',
|
||||
"last_activity: '2026-07-23'",
|
||||
'---',
|
||||
'',
|
||||
'# Project State',
|
||||
'',
|
||||
'**Current Phase:** 03',
|
||||
'**Status:** In progress',
|
||||
'',
|
||||
'## Archive',
|
||||
'',
|
||||
'Last activity: 2026-06-20 - older',
|
||||
'',
|
||||
].join('\n')
|
||||
);
|
||||
|
||||
const jsonResult = runGsdTools('state json', tmpDir);
|
||||
assert.ok(jsonResult.success, `state json failed: ${jsonResult.error}`);
|
||||
const parsed = JSON.parse(jsonResult.output);
|
||||
|
||||
assert.strictEqual(parsed.last_activity, '2026-07-23',
|
||||
`state json must surface the preserved frontmatter last_activity (2026-07-23) but got ${parsed.last_activity}`);
|
||||
});
|
||||
});
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Bug #2445: stale phase dirs from closed milestone inflate phase counts
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user