diff --git a/changelog.d/autoplan-revise-accepts-heading-less-plans.md b/changelog.d/autoplan-revise-accepts-heading-less-plans.md new file mode 100644 index 000000000..12decab39 --- /dev/null +++ b/changelog.d/autoplan-revise-accepts-heading-less-plans.md @@ -0,0 +1,2 @@ +- **`gstack-autoplan revise` no longer refuses a plan that the first `next` accepted.** A plan with no `## Implementation plan` or `## Review record` heading is reviewed whole by `next`, but once the owner added one of those headings while editing (for example a `## Review record` noting the accepted changes), `revise` stopped with `PLAN_SECTIONS_INVALID` after a full paid review round, and its fix pointed at a `--scaffold` flag the first `next` never asked for. `revise` now adds the missing heading to its own copy of the plan and prints `SCAFFOLDED:`, the way `next --scaffold` does; the owner's file is never edited. A run whose first plan already had both headings still refuses a revision that drops one, and now says why. +- **`revise --plan` with a different file now says the run follows that file.** It prints `PLAN_INPUT: -> ` and records the move, so drift checks no longer switch to another file silently. diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index c58bf2564..27b7bf46f 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -5354,7 +5354,10 @@ section is refused before anything is written. The error names the problem. **Fix.** Add the missing heading, or rerun the first `next` with `--scaffold`: it writes `/plan-source.md` with the missing heading added, reviews that copy and prints `SCAFFOLDED:`. Your plan file is never edited. Duplicate, -reversed or empty sections are ambiguous and must be fixed by hand. +reversed or empty sections are ambiguous and must be fixed by hand. On +`revise`, a run whose first plan was scaffolded or had no headings at all gets +the one missing heading added to its own copy (`SCAFFOLDED:`); a run whose first +plan had both headings refuses a revision that drops one, and says so. ### `PLAN_AMEND_PENDING` diff --git a/docs/unattended.md b/docs/unattended.md index d9b41014b..68c4da48d 100644 --- a/docs/unattended.md +++ b/docs/unattended.md @@ -690,7 +690,9 @@ by its runner. Result codes: `EXECUTION_UNKNOWN`, `RUN_LOCKED`, `answer` prints `PLAN_SOURCE_CHANGED` when the source plan moved since the snapshot. A plan without both `## Implementation plan` and `## Review record` refuses with `PLAN_SECTIONS_INVALID`; `next --scaffold` reviews a copy with - the missing heading added. `resume` completes an interrupted revise. + the missing heading added, and `revise` does the same (`SCAFFOLDED:`) when + the run's first plan was scaffolded or had no headings. A revise from a + different file prints `PLAN_INPUT: -> `. `resume` completes an interrupted revise. - **Admission and upgrades.** `next`, `submit`, `answer`, `revise`, `export` and `run` refuse on a Capy host (`GSTACK_CAPY=1`) without an ok install verdict for this checkout, HEAD and workflow at `$GSTACK_INSTALL_VERDICT` diff --git a/lib/autoplan-revise.ts b/lib/autoplan-revise.ts index 5d8846148..beaa95151 100644 --- a/lib/autoplan-revise.ts +++ b/lib/autoplan-revise.ts @@ -26,6 +26,7 @@ import { carryLines, keepPlanFile, voidPendingCheck } from './autoplan-carry'; import { atCap, pendingDeltas, roundCap, roundOf } from './autoplan-budget'; import { diffStat, earlierRows, lineDiff } from './autoplan-confirm'; import { SETTLEABLE_CHANGES } from './autoplan-reconcile'; +import { sectionHeadings } from '../bin/gstack-autoplan-snapshot'; import { normalizeRevision, reviewHash, scaffold } from './autoplan-plan-text'; import { parseScopeTokens } from './autoplan-scope'; import { PHASES, detectScope, extractHeading, phaseOrder, type Phase, type ScopeFlags } from './autoplan-prompts'; @@ -36,8 +37,17 @@ export interface ReviseResult { state: RunState; reopened: Phase[]; lines: strin const amendmentLine = (state: RunState) => `AMENDMENTS: pending=${pendingAmendments(state).map(a => a.decision).join(',') || 'none'}`; -/** The revised plan's text as the run reviews it (cut to the run's heading; scaffolded when the run was). */ -function revisionText(state: RunState, file: string): string { +/** Whether the run's first plan had no section headings, so init reviewed it whole: the restore point holds its exact bytes. */ +function firstPlanBare(state: RunState): boolean { + try { return !sectionHeadings(fs.readFileSync(state.restore, 'utf8')).length; } catch { return false; } +} + +/** + * The revised plan's text as the run reviews it (cut to the run's heading). One missing heading is scaffolded + * when the first `next` accepted a plan without both (a scaffolded run, or a plan with no headings at all); + * a run that started with both headings keeps refusing a revision that drops one, as `next` does. + */ +function revisionText(state: RunState, file: string): { text: string; added?: string } { if (!fs.existsSync(file)) throw new RunError('PLAN_SECTIONS_INVALID', `${file}: not found`, 3); let text = fs.readFileSync(file, 'utf8'); if (state.plan_heading) { @@ -46,8 +56,9 @@ function revisionText(state: RunState, file: string): string { text = section; } const diag = scaffold(text); - if (diag.problem && !(state.scaffolded && diag.text)) throw new RunError('PLAN_SECTIONS_INVALID', `${file}: ${diag.problem}`, 3); - return diag.text ?? text; + if (diag.problem && !diag.text) throw new RunError('PLAN_SECTIONS_INVALID', `${file}: ${diag.problem}`, 3); + if (diag.problem && !state.scaffolded && !firstPlanBare(state)) throw new RunError('PLAN_SECTIONS_INVALID', `${file}: ${diag.problem}; this run's first plan had both headings, so its revisions keep both`, 3); + return { text: diag.text ?? text, added: diag.added }; } function settleAmendments(state: RunState, ids: string[], status: 'incorporated' | 'withdrawn' | 'settled', at: string, extra: Record = {}): Array> { @@ -83,7 +94,7 @@ function settleEvidence(state: RunState, ids: string[], revised?: string): { pla } if (revised) return { plan_sha256: revised, changes }; let input = ''; - try { input = state.plan_input && fs.existsSync(state.plan_input) ? reviewHash(revisionText(state, state.plan_input)) : active; } catch { /* unreadable: refused below */ } + try { input = state.plan_input && fs.existsSync(state.plan_input) ? reviewHash(revisionText(state, state.plan_input).text) : active; } catch { /* unreadable: refused below */ } if (input !== active) throw new RunError('AMENDMENT_NOT_SETTLEABLE', `the plan file has edits the run has not reviewed (active ${active.slice(0, 12)}, plan file ${input.slice(0, 12) || 'unreadable'}); revise --plan --incorporates --settle ${ids.join(',')} instead`, 3); return { plan_sha256: active, changes }; } @@ -147,7 +158,7 @@ export function reviseRun(o: ReviseOptions): ReviseResult { return { state, reopened: [], lines }; } const file = path.resolve(o.plan); - const text = revisionText(state, file); + const { text, added: scaffoldedHeading } = revisionText(state, file); let newHash: string; try { newHash = reviewHash(text); } catch (e: any) { throw new RunError('PLAN_SECTIONS_INVALID', `${file}: ${e.message}`, 3); } const oldHash = reviewHash(fs.readFileSync(state.active_plan, 'utf8')); @@ -198,6 +209,7 @@ export function reviseRun(o: ReviseOptions): ReviseResult { state.phase_order = order; state.required_phases = order; state.scope = { ...detected, ui: detected.ui || state.scope.ui, dx: detected.dx || state.scope.dx }; if (reopen.length || added.length) state.status = 'running'; + const inputFrom = state.plan_input !== file ? state.plan_input : undefined; state.plan_input = file; state.plan_source = path.join(out, `plan-source.v${k}.md`); state.plan_revision = k; delete state.plan_reality; const settled = [...settleAmendments(state, incorporates, 'incorporated', at, { old_plan_sha256: oldHash, new_plan_sha256: newHash }), ...settleAmendments(state, withdraw, 'withdrawn', at, { by: 'owner' }), @@ -206,11 +218,13 @@ export function reviseRun(o: ReviseOptions): ReviseResult { if (amending) state.status = pendingAmendments(state).length ? 'amend_pending' : state.phase_order.some(p => !state.phases[p]?.closed_at) ? 'running' : pendingDeltas(state).length ? 'gate_pending' : 'complete'; // A quiet revise that ends at gate_pending still has an earlier capped delta waiting: the owner gets a new gate for it. const regate = (capped.length > 0 || quiet) && state.status === 'gate_pending'; - const committed = { event: 'plan_revised', k, plan: file, old_plan_sha256: oldHash, new_plan_sha256: newHash, source_file_sha256: createHash('sha256').update(fs.readFileSync(file)).digest('hex'), reopened: reopen, added, at_gate_rev: atGate ?? null }; + const committed = { event: 'plan_revised', k, plan: file, old_plan_sha256: oldHash, new_plan_sha256: newHash, source_file_sha256: createHash('sha256').update(fs.readFileSync(file)).digest('hex'), reopened: reopen, added, at_gate_rev: atGate ?? null, ...(inputFrom ? { plan_input_from: inputFrom } : {}) }; fs.writeFileSync(path.join(staging, FILES.run), JSON.stringify({ state, renames, committed, settled }, null, 2) + '\n'); publish(state, staging, renames, committed, settled); keepPlanFile(state.out, file); const lines = [`REVISED: plan=${newHash.slice(0, 12)} reopened=${reopen.join(',') || 'none'}${added.length ? ` added=${added.join(',')}` : ''}${capped.length ? ` capped=${capped.join(',')}` : ''}`, + ...(scaffoldedHeading ? [`SCAFFOLDED: added "${scaffoldedHeading}" (${state.scaffolded ? 'the run was scaffolded' : "the run's first plan had no section headings"}; copy at ${state.plan_source}; ${file} unchanged)`] : []), + ...(inputFrom ? [`PLAN_INPUT: ${inputFrom} -> ${file} (drift checks and later revises read ${file}; pass --plan ${inputFrom} to go back)`] : []), ...(quiet ? [`APPLIED: ${incorporates.join(',')} without re-review (${state.lean ? 'lean run: one gate revision' : 'below High'}; plan +${delta.added} -${delta.removed} lines)`] : []), amendmentLine(state)]; for (const phase of capped) lines.push(`CAPPED: ${phase} used round ${roundOf(state, phase)} of ${roundCap(state, phase)}: not reopened; the gate asks the owner about the unreviewed delta (+${delta.added} -${delta.removed} lines)`); if (regate) { diff --git a/test/autoplan-revise.test.ts b/test/autoplan-revise.test.ts index e58dbbdb3..8733b9e31 100644 --- a/test/autoplan-revise.test.ts +++ b/test/autoplan-revise.test.ts @@ -189,4 +189,48 @@ describe('plan sections', () => { expect(res.status).toBe(3); expect(res.stderr).toMatch(/Implementation plan is empty.*\(PLAN_SECTIONS_INVALID\)$/m); }); + const BARE = '# Retry backoff\n\nAdd exponential backoff to lib/worker.ts: base 500 ms, cap 30 s.\n'; + test('a run whose first plan had no headings revises a plan that gained one heading, scaffolding the other', () => { + for (const [name, v2, added] of [ + ['record', BARE.replace('30 s', '60 s') + '\n## Review record\n\nIncorporated the cap change.\n', '## Implementation plan'], + ['impl', '# Retry backoff\n\n## Implementation plan\n\nAdd exponential backoff, cap 60 s.\n', '## Review record'], + ] as const) { + const dir = path.join(r.host.base, `bare-${name}`); + const plan = writePlan(dir, BARE); + const out = path.join(dir, 'run'); + expect(r.ap(['next', '--out', out, '--plan', plan, '--no-ui']).status).toBe(0); + fs.writeFileSync(plan, v2); + const rev = r.ap(['revise', '--out', out, '--plan', plan]); + expect(rev.status, rev.stderr).toBe(0); + expect(rev.stdout).toMatch(new RegExp(`^SCAFFOLDED: added "${added}" \\(the run's first plan had no section headings; .*plan-source\\.v1\\.md`, 'm')); + expect(rev.stdout).toMatch(/^REVISED: plan=[0-9a-f]{12} reopened=ceo$/m); + expect(fs.readFileSync(plan, 'utf8')).toBe(v2); + const active = fs.readFileSync(path.join(out, 'plan-active.md'), 'utf8'); + expect(active).toContain('cap 60 s'); + expect(active.slice(0, active.indexOf('## Review record'))).not.toContain('Incorporated'); + } + }); + test('a run that started with both headings still refuses a revision missing one, and says why', () => { + const s = start(); + fs.writeFileSync(s.plan, V2.replace('## Review record\n', '')); + const res = r.ap(['revise', '--out', s.out, '--plan', s.plan]); + expect(res.status).toBe(3); + expect(res.stderr).toMatch(/no "## Review record"; this run's first plan had both headings, so its revisions keep both.*\(PLAN_SECTIONS_INVALID\)$/m); + }); +}); + +describe('plan path', () => { + test('revise from another plan file says the run now follows it; the same file prints nothing', () => { + const s = start(); + fs.writeFileSync(s.plan, V2); + const same = r.ap(['revise', '--out', s.out, '--plan', s.plan]); + expect(same.status, same.stderr).toBe(0); + expect(same.stdout).not.toContain('PLAN_INPUT:'); + const v2 = v2File(s.dir, V2.replace('jitter 10%', 'jitter 20%')); + const moved = r.ap(['revise', '--out', s.out, '--plan', v2]); + expect(moved.status, moved.stderr).toBe(0); + expect(moved.stdout).toContain(`PLAN_INPUT: ${s.plan} -> ${v2} (drift checks and later revises read ${v2}; pass --plan ${s.plan} to go back)`); + expect(readRunJson(s.out).plan_input).toBe(v2); + expect(readJsonl(path.join(s.out, 'attempts.jsonl')).filter(e => e.event === 'plan_revised').map(e => e.plan_input_from ?? null)).toEqual([null, s.plan]); + }); });