autoplan: closable runs - submit --record/--attest/--unavailable, revise, amendment interlock, agreeing-UC defaults, admission

B1a: an older run directory prints GSTACK_VERSION_CHANGED once and migrates legacy decision ids; a newer one refuses.
B1b: submit binds hand-run records (fence, table, severity list, No findings.), attested binds need --attempt,
--attempt is optional with one open attempt, --unavailable/--failed close an outside voice native_only, and submit
caches the plan-reality report by Implementation-plan hash.
B1c: revise reviews a revised plan in the same run as one recoverable transaction (reopen on hash drift plus Eng,
add newly in-scope phases, .r<rev>/.v<k> archives, gate_rev+1 at the gate); PLAN_SECTIONS_INVALID and next --scaffold;
PLAN_CHANGED / PLAN_SOURCE_CHANGED drift hints.
B2a/B2b: user challenges carry changes; agreeing voices with the same changes on a confirmed finding recommend b with
both remedies shown (or the reconcile proposal); accepted changes print APPLY and become pending amendments that next
and export refuse past until revise --incorporates names them or --withdraw drops one; all as-is; partial replies,
DECIDED block, carry-forward and rerun demotion.
Mutating verbs pass admission (GSTACK_CAPY install verdict; unattended Bun minimum; override recorded as a deviation).
Intentional pin rewrite: test/autoplan-answer-invalidation.test.ts reopen tests now revise --incorporates before rerun.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
garrytanandcapy-ai[bot] committed 2026-10-13 01:10:13 +00:00
1 parent ee7b341e5d
commit b8cbfaea18
15 files changed
+1619 -143

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
/**
* Admission at `gstack-autoplan`'s mutating verbs (plan Lane A amendments,
* as corrected: the verdict is required only under GSTACK_CAPY=1). On a Capy
* host (GSTACK_CAPY=1) `next`, `submit`, `answer`, `revise` and `export`
* refuse unless the install verdict at GSTACK_INSTALL_VERDICT says `gstack:
* ok` for this checkout and HEAD; any unattended session also refuses when
* the PATH Bun is below gstack's supported minimum. `status` and `resume` stay
* available. GSTACK_ALLOW_UNSUPPORTED_RUNTIME=1 overrides and is recorded as
* a run deviation. Interactive sessions are never gated here.
*/
import { afterAll, describe, expect, test } from 'bun:test';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { ROOT } from './helpers/autoplan-run-fixture';
import { HEAD, capyRunner, readRunJson, writePlan } from './helpers/autoplan-capy';
const runners: Array<{ cleanup(): void }> = [];
afterAll(() => runners.forEach(r => r.cleanup()));
const make = (o: Parameters<typeof capyRunner>[0] = {}) => { const r = capyRunner(o); runners.push(r); return r; };
const first = (r: ReturnType<typeof capyRunner>, name: string, extra: Record<string, string> = {}) => {
const dir = path.join(r.host.base, name);
const out = path.join(dir, 'run');
return { out, res: r.ap(['next', '--out', out, '--plan', writePlan(dir), '--no-ui'], extra) };
};
describe('admission', () => {
test('GSTACK_CAPY=1 without a verdict refuses before writing anything', () => {
const r = make({ verdict: null });
const { out, res } = first(r, 'none');
expect(res.status).toBe(3);
expect(res.stderr).toMatch(/install verdict.*not found.*\(INSTALL_VERDICT_FAILED\)$/m);
expect(res.stdout).toContain('GSTACK_RESULT: skill=autoplan status=refused');
expect(fs.existsSync(path.join(out, 'run.json'))).toBe(false);
});
test('a failing verdict, or one for another HEAD or checkout, refuses with the trailer', () => {
const r = make();
r.writeVerdict({ trailer: 'gstack: fail runtime', head: HEAD, checkout: fs.realpathSync(ROOT) });
expect(first(r, 'fail').res.stderr).toMatch(/gstack: fail runtime.*\(INSTALL_VERDICT_FAILED\)$/m);
r.writeVerdict({ trailer: 'gstack: ok 1.91.95.0 for=autoplan', head: '0'.repeat(40), checkout: fs.realpathSync(ROOT) });
expect(first(r, 'head').res.stderr).toMatch(/another HEAD.*\(INSTALL_VERDICT_FAILED\)$/m);
r.writeVerdict({ trailer: 'gstack: ok 1.91.95.0 for=autoplan', head: HEAD, checkout: '/elsewhere/gstack' });
expect(first(r, 'checkout').res.stderr).toMatch(/another checkout.*\(INSTALL_VERDICT_FAILED\)$/m);
});
test('an ok verdict admits; status and resume need none; every mutating verb is gated', () => {
const r = make();
const { out, res } = first(r, 'ok');
expect(res.status, res.stderr).toBe(0);
fs.rmSync(r.verdict);
expect(r.ap(['status', '--out', out]).status).toBe(0);
expect(r.ap(['resume', '--out', out]).status).toBe(0);
for (const args of [['next'], ['submit', '--phase', 'ceo', '--voice', 'outside', '--unavailable', 'x'], ['answer', '--gate-rev', '1', '--reply', 'all'], ['revise', '--plan', path.join(r.host.base, 'ok', 'plan.md')], ['export']]) {
const res2 = r.ap([args[0]!, '--out', out, ...args.slice(1)]);
expect(res2.status, args[0]).toBe(3);
expect(res2.stderr, args[0]).toMatch(/\(INSTALL_VERDICT_FAILED\)$/m);
}
});
test('an unattended session with the PATH Bun below the supported minimum refuses even with an ok verdict', () => {
const r = make({ launched: false });
const { res } = first(r, 'oldbun');
expect(res.status).toBe(3);
expect(res.stderr).toMatch(/bun 1\.3\.14.*\(RUNTIME_BELOW_MINIMUM\)$/m);
});
test('GSTACK_ALLOW_UNSUPPORTED_RUNTIME=1 overrides and is recorded as a run deviation', () => {
const r = make({ verdict: null, launched: false });
const { out, res } = first(r, 'override', { GSTACK_ALLOW_UNSUPPORTED_RUNTIME: '1' });
expect(res.status, res.stderr).toBe(0);
expect(res.stdout).toMatch(/^ADMISSION: override GSTACK_ALLOW_UNSUPPORTED_RUNTIME=1/m);
expect(readRunJson(out).deviations.some((d: string) => d.startsWith('GSTACK_ALLOW_UNSUPPORTED_RUNTIME=1'))).toBe(true);
});
test('an interactive session is not gated', () => {
const r = make({ verdict: null, launched: false });
const { res } = first(r, 'interactive', { GSTACK_SESSION_KIND: 'interactive', GSTACK_CAPY: '' });
expect(res.status, res.stderr).toBe(0);
});
});
+32 -3
View File
@@ -13,7 +13,7 @@ import { spawnSync } from 'node:child_process';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { attemptsOf, autoplan, journal, readRun, review, ROOT, runPhase, startSmallRun } from './helpers/autoplan-run-fixture';
import { attemptsOf, autoplan, journal, readRun, review, ROOT, runPhase, SMALL_PLAN, startSmallRun } from './helpers/autoplan-run-fixture';
const TMP = fs.mkdtempSync(path.join(os.tmpdir(), 'autoplan-answer-'));
afterAll(() => fs.rmSync(TMP, { recursive: true, force: true }));
@@ -40,6 +40,25 @@ function gatedRun(name: string): string {
}
const decisions = (out: string) => fs.readFileSync(path.join(out, 'decisions.jsonl'), 'utf8').trim().split('\n').map(l => JSON.parse(l));
/**
* An accepted plan change is a pending amendment (B-core interlock): `next` refuses with PLAN_AMEND_PENDING and writes
* nothing until the plan is edited and `revise --incorporates` names the decision. Returns the revise output.
*/
function incorporate(out: string, ids: string[], edit: string) {
const before = fs.readFileSync(path.join(out, 'run.json'), 'utf8');
const blocked = autoplan(['next', '--out', out]);
expect(blocked.status).toBe(3);
expect(blocked.stderr).toMatch(new RegExp(`${ids.join(',')}.*\\(PLAN_AMEND_PENDING\\)$`, 'm'));
expect(fs.readFileSync(path.join(out, 'run.json'), 'utf8')).toBe(before);
const plan = path.join(path.dirname(out), 'plan.md');
fs.writeFileSync(plan, SMALL_PLAN.replace('Tests cover', `${edit} Tests cover`));
const revised = autoplan(['revise', '--out', out, '--plan', plan, '--incorporates', ids.join(',')]);
expect(revised.status, revised.stderr).toBe(0);
expect(revised.stdout).toMatch(/^AMENDMENTS: pending=none$/m);
expect(journal(out).filter(j => j.event === 'amendment_incorporated').map(j => j.decision)).toEqual(expect.arrayContaining(ids));
return revised;
}
describe('answer', () => {
test('the gate lists the disagreement as an auto item and the user challenges as approval items', () => {
const out = gatedRun('shape');
@@ -97,7 +116,8 @@ describe('answer', () => {
const out = gatedRun('reopen');
const r = autoplan(['answer', '--out', out, '--gate-rev', '1', '--reply', 'all except d1b', '--json']);
expect(r.status, r.stderr).toBe(0);
expect(JSON.parse(r.stdout)).toMatchObject({ status: 'running', outcome: 'reopened', reopened: ['ceo', 'eng'] });
const runId = readRun(out).run;
expect(JSON.parse(r.stdout)).toMatchObject({ status: 'running', outcome: 'reopened', reopened: ['ceo', 'eng'], amendments: [`${runId}-d1`] });
const run = readRun(out);
expect(run.phases.ceo).toEqual({});
expect(run.phases.eng).toEqual({});
@@ -109,6 +129,9 @@ describe('answer', () => {
const d1 = decisions(out).find((d: any) => d.label === 'D1');
expect(d1).toMatchObject({ status: 'overridden', chosen: 'b', gate_rev: 1 });
expect(journal(out).filter(j => j.event === 'reopened').map(j => j.phase)).toEqual(['ceo', 'eng']);
// the accepted change is applied to the plan and named before any phase reruns; the reopened phases stay reopened once
incorporate(out, [`${runId}-d1`], 'Gate reuse also checks the lockfile hash.');
expect(journal(out).filter(j => j.event === 'reopened').map(j => j.phase)).toEqual(['ceo', 'eng']);
// the loop continues: ceo first, then eng, then a new gate at gate_rev 2 whose rows join the old ones
const ceo = runPhase(out, 'ceo');
expect(ceo.next.stdout).toMatch(/^PHASE: ceo /m);
@@ -140,8 +163,14 @@ describe('answer', () => {
test('an eng-only override reopens eng alone; p1d reopens every phase; p1c keeps the gate; p1e ends incomplete', () => {
const out = gatedRun('eng-only');
const engOnly = autoplan(['answer', '--out', out, '--gate-rev', '1', '--reply', 'all except uc2b', '--json']);
expect(JSON.parse(engOnly.stdout)).toMatchObject({ outcome: 'reopened', reopened: ['eng'] });
const runId = readRun(out).run;
expect(JSON.parse(engOnly.stdout)).toMatchObject({ outcome: 'reopened', reopened: ['eng'], amendments: [`${runId}-uc2`] });
expect(readRun(out).phases.ceo.closed_at).toBeTruthy();
// the revised plan changes the hash ceo reviewed, so revise (not the answer) reruns ceo on v2 before eng
const revised = incorporate(out, [`${runId}-uc2`], 'Retries stop at the dead-letter queue.');
expect(revised.stdout).toMatch(/^REVISED: plan=\S+ reopened=ceo$/m);
expect(journal(out).filter(j => j.event === 'reopened').map(j => [j.phase, j.reason ?? null])).toEqual([['eng', expect.anything()], ['ceo', expect.stringContaining('revise')]]);
expect(runPhase(out, 'ceo').outside.stdout).toMatch(/PHASE_CLOSED: ceo/);
const eng = runPhase(out, 'eng');
expect(eng.outside.stdout).toContain('status=gate_pending');
const interrogate = autoplan(['answer', '--out', out, '--gate-rev', '2', '--reply', 'all except p1c', '--json']);
+183
View File
@@ -0,0 +1,183 @@
/**
* End-to-end unattended runs on the simulated Capy host, driven only through
* the CLI (plan B1a/B1b/B1c, the wave's mandatory free-tier acceptance): a
* three-revision run validates with unique ids; phases bound from free-form
* records with one outside voice unavailable close, answer, export and
* validate; a run gated on <=1.91.94-style decision ids validates after the
* load-time migration; run.json records gstack_version, a newer run refuses
* and an older one migrates forward. One conformance run strings every
* mechanism together.
*/
import { afterAll, describe, expect, test } from 'bun:test';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { ROOT, SMALL_PLAN } from './helpers/autoplan-run-fixture';
import { capyRunner, phaseVia, readJsonl, readRunJson, writePlan, type CapyRunner } from './helpers/autoplan-capy';
const r = capyRunner();
afterAll(() => r.cleanup());
const VERSION = fs.readFileSync(path.join(ROOT, 'VERSION'), 'utf8').trim();
let n = 0;
function start(args: string[] = []): { out: string; dir: string; plan: string } {
const dir = path.join(r.host.base, `e2e${++n}`);
const plan = writePlan(dir);
const out = path.join(dir, 'run');
const res = r.ap(['next', '--out', out, '--plan', plan, '--no-ui', ...args]);
if (res.status !== 0) throw new Error(`first next failed: ${res.stderr}\n${res.stdout}`);
return { out, dir, plan };
}
function expectUniqueIds(out: string) {
for (const f of ['decisions.jsonl', 'findings.jsonl', 'tasks.jsonl']) {
const ids = readJsonl(path.join(out, f)).map(x => x.id);
expect(new Set(ids).size, f).toBe(ids.length);
}
const attempts = readJsonl(path.join(out, 'attempts.jsonl')).filter(e => e.event === 'dispatched').map(e => e.attempt);
expect(new Set(attempts).size).toBe(attempts.length);
}
/** Every completed voice reviewed its phase's snapshot (receipt or attestation), recorded as reviewed_sha256. */
function expectReviewedHashes(out: string) {
const run = readRunJson(out);
for (const p of run.phase_order) for (const voice of ['native', 'outside']) {
const v = run.phases[p][voice];
if (v.status !== 'completed') continue;
expect(v.reviewed_sha256, `${p} ${voice}`).toBe(run.phases[p].snapshot);
if (v.receipt !== 'attested') expect(fs.readFileSync(path.join(out, v.output), 'utf8')).toContain(`INPUT: ${p} ${run.phases[p].snapshot}`);
}
}
const ok = (res: { status: number | null; stderr: string; stdout: string }) => { expect(res.status, res.stderr + res.stdout).toBe(0); return res; };
describe('unattended runs on the Capy host', () => {
test('a three-revision run driven only through the CLI validates with unique ids and records gstack_version', () => {
const s = start();
expect(readRunJson(s.out).gstack_version).toBe(VERSION);
for (const rev of [1, 2]) {
phaseVia(r, s.out, 'ceo');
expect(phaseVia(r, s.out, 'eng').outside.stdout).toContain('status=gate_pending');
expectReviewedHashes(s.out);
expect(ok(r.ap(['answer', '--out', s.out, '--gate-rev', String(rev), '--reply', 'all except p1d'])).stdout).toMatch(/^REOPENED: ceo,eng \(Eng last\) reason=revise$/m);
}
phaseVia(r, s.out, 'ceo');
phaseVia(r, s.out, 'eng');
expect(ok(r.ap(['answer', '--out', s.out, '--gate-rev', '3', '--reply', 'all'])).stdout).toContain('status=complete');
ok(r.ap(['export', '--out', s.out]));
const v = ok(r.validate(s.out));
expect(v.stdout).toMatch(/^ARTIFACT_VALID: run=\S+ status=complete/m);
expectUniqueIds(s.out);
expectReviewedHashes(s.out);
expect(readJsonl(path.join(s.out, 'decisions.jsonl')).filter(d => d.gate_rev === 3).every(d => d.id.includes('-r3-'))).toBe(true);
});
test('free-form records (table, list, No findings.) and one unavailable outside voice reach the gate, answer, export and validate', () => {
const s = start();
const run = readRunJson(s.out);
const sha = run.phases.ceo.snapshot;
const rec = (name: string, text: string) => { const f = path.join(s.dir, name); fs.writeFileSync(f, text); return f; };
ok(r.ap(['submit', '--out', s.out, '--phase', 'ceo', '--voice', 'native', '--model', 'anthropic/claude-opus-5-5', '--attest', 'Capy subagent read the prompt file', '--attempt', run.phases.ceo.native.attempt,
'--record', rec('ceo-n.md', '# CEO review\n\n| Severity | Title | File |\n|---|---|---|\n| High | Retry cap is unbounded on restart | lib/worker.ts:10 |\n| Low | Name the env var | |\n')]));
const closed = ok(r.ap(['submit', '--out', s.out, '--phase', 'ceo', '--voice', 'outside', '--model', 'openai/gpt-6.1-sol', '--record', rec('ceo-o.md', `INPUT: ceo ${sha}\n\n- [High] Jitter bounds are untested\n- [Medium] Resume path skips the cap\n`)]));
expect(closed.stdout).toMatch(/^PHASE_CLOSED: ceo .*coverage=both/m);
const eng = ok(r.ap(['next', '--out', s.out]));
const engSha = /^PHASE: eng snapshot=([0-9a-f]{64})/m.exec(eng.stdout)![1];
ok(r.ap(['submit', '--out', s.out, '--phase', 'eng', '--voice', 'native', '--model', 'anthropic/claude-opus-5-5', '--record', rec('eng-n.md', `INPUT: eng ${engSha}\n\nRead the whole plan.\n\nNo findings.\n`)]));
const gated = ok(r.ap(['submit', '--out', s.out, '--phase', 'eng', '--voice', 'outside', '--unavailable', 'no cross-family subagent on this host']));
expect(gated.stdout).toMatch(/^PHASE_CLOSED: eng .*coverage=native_only/m);
expect(gated.stdout).toContain('status=gate_pending');
expect(ok(r.ap(['answer', '--out', s.out, '--gate-rev', '1', '--reply', 'all'])).stdout).toContain('status=complete');
ok(r.ap(['export', '--out', s.out]));
expect(ok(r.validate(s.out)).stdout).toMatch(/^ARTIFACT_VALID: run=\S+ status=complete attested=1$/m);
expect(fs.readFileSync(path.join(s.out, 'review-record.md'), 'utf8')).toMatch(/attested/);
expectReviewedHashes(s.out);
});
test('a run gated on <=1.91.94-style decision ids validates after the load-time migration', () => {
const s = start();
phaseVia(r, s.out, 'ceo');
phaseVia(r, s.out, 'eng');
ok(r.ap(['answer', '--out', s.out, '--gate-rev', '1', '--reply', 'all except p1d']));
phaseVia(r, s.out, 'ceo');
phaseVia(r, s.out, 'eng');
const file = path.join(s.out, 'decisions.jsonl');
fs.writeFileSync(file, readJsonl(file).map(d => JSON.stringify({ ...d, id: d.id.replace(/-r\d+-/, '-') })).join('\n') + '\n');
const manifest = readRunJson(s.out);
delete manifest.gstack_version;
fs.writeFileSync(path.join(s.out, 'run.json'), JSON.stringify(manifest, null, 2) + '\n');
const res = ok(r.ap(['answer', '--out', s.out, '--gate-rev', '2', '--reply', 'all']));
expect(res.stdout).toMatch(/^GSTACK_VERSION_CHANGED: run unknown -> /m);
expect(res.stdout).toContain('status=complete');
ok(r.ap(['export', '--out', s.out]));
const v = r.validate(s.out);
expect(v.stderr).not.toContain('ARTIFACT_DUPLICATE_ID');
expect(v.stderr).not.toContain('ARTIFACT_DANGLING_REF');
expect(v.status, v.stderr).toBe(0);
const migrated = readJsonl(path.join(s.out, 'attempts.jsonl')).filter(e => e.event === 'decision_ids_migrated');
expect(migrated).toHaveLength(1);
expect(migrated[0].n).toBeGreaterThan(0);
expect(readRunJson(s.out).gstack_version).toBe(VERSION);
});
test('a run written by a newer gstack refuses; an older one prints GSTACK_VERSION_CHANGED once and moves forward', () => {
const s = start();
const set = (v: string) => { const m = readRunJson(s.out); m.gstack_version = v; fs.writeFileSync(path.join(s.out, 'run.json'), JSON.stringify(m, null, 2) + '\n'); };
set('99.0.0.0');
const newer = r.ap(['next', '--out', s.out]);
expect(newer.status).toBe(3);
expect(newer.stderr).toMatch(/99\.0\.0\.0.*\(GSTACK_VERSION_CHANGED\)$/m);
set('1.91.22.0');
const older = ok(r.ap(['next', '--out', s.out]));
expect(older.stdout).toMatch(new RegExp(`^GSTACK_VERSION_CHANGED: run 1\\.91\\.22\\.0 -> ${VERSION.replace(/\./g, '\\.')}`, 'm'));
expect(ok(r.ap(['next', '--out', s.out])).stdout).not.toMatch(/GSTACK_VERSION_CHANGED/);
expect(readJsonl(path.join(s.out, 'attempts.jsonl')).filter(e => e.event === 'gstack_version_changed')).toHaveLength(1);
});
});
describe('conformance: one run through every mechanism', () => {
test('canonical and attested binds, an unavailable voice, partial then full replies, PLAN_AMEND_PENDING, revise --incorporates, an interruption, three gate revisions', () => {
const c: CapyRunner = r;
const s = start();
const run = readRunJson(s.out).run;
// ceo: native canonical (fixture), outside attested record
ok(c.review(path.join(s.out, 'ceo-native-prompt.md')));
ok(c.ap(['submit', '--out', s.out, '--phase', 'ceo', '--voice', 'native', '--result', path.join(s.out, 'ceo-native.md'), '--model', 'anthropic/claude-opus-5-5']));
const hand = path.join(s.dir, 'ceo-outside-hand.md');
fs.writeFileSync(hand, '| Severity | Title |\n|---|---|\n| Medium | Retry storage cost is unpriced |\n');
ok(c.ap(['submit', '--out', s.out, '--phase', 'ceo', '--voice', 'outside', '--record', hand, '--model', 'openai/gpt-6.1-sol', '--attest', 'Capy subagent', '--attempt', readRunJson(s.out).phases.ceo.outside.attempt]));
// eng: native challenge with changes, outside unavailable
ok(c.ap(['next', '--out', s.out]));
ok(c.review(path.join(s.out, 'eng-native-prompt.md'), { FIXTURE_FINDINGS: JSON.stringify([{ id: 'F1', severity: 'High', title: 'Gate the backoff behind a flag', user_challenge: true, changes: 'scope', fix: 'RETRY_BACKOFF=1' }]) }));
ok(c.ap(['submit', '--out', s.out, '--phase', 'eng', '--voice', 'native', '--result', path.join(s.out, 'eng-native.md'), '--model', 'anthropic/claude-opus-5-5']));
expect(ok(c.ap(['submit', '--out', s.out, '--phase', 'eng', '--voice', 'outside', '--unavailable', 'no outside runner'])).stdout).toContain('status=gate_pending');
const accept = JSON.parse(fs.readFileSync(path.join(s.out, 'gate.json'), 'utf8')).items.find((i: any) => i.title.startsWith('Gate the backoff')).id;
// partial, then full
expect(ok(c.ap(['answer', '--out', s.out, '--gate-rev', '1', '--reply', `${accept}b`])).stdout).toMatch(/^GATE_PENDING: p1/m);
const full = ok(c.ap(['answer', '--out', s.out, '--gate-rev', '1', '--reply', 'all']));
expect(full.stdout).toMatch(new RegExp(`^APPLY: ${run}-${accept} `, 'm'));
expect(c.ap(['next', '--out', s.out]).stderr).toMatch(/\(PLAN_AMEND_PENDING\)$/m);
const v2 = path.join(s.dir, 'plan-v2.md');
fs.writeFileSync(v2, SMALL_PLAN.replace('Tests cover', 'Ship behind RETRY_BACKOFF=1, default off. Tests cover'));
expect(ok(c.ap(['revise', '--out', s.out, '--plan', v2, '--incorporates', `${run}-${accept}`])).stdout).toMatch(/^AMENDMENTS: pending=none$/m);
// interruption: attempts dispatched, the driver dies holding the lock; resume reclaims and the parent redispatches
ok(c.ap(['next', '--out', s.out]));
fs.writeFileSync(path.join(s.out, 'run.lock'), JSON.stringify({ pid: 999_999_999, host: require('node:os').hostname(), at: new Date().toISOString() }));
const resumed = ok(c.ap(['resume', '--out', s.out]));
expect(resumed.stdout).toMatch(/execution_unknown=2/);
const unknown = [...resumed.stdout.matchAll(/^EXECUTION_UNKNOWN: (\S+)$/gm)].map(m => m[1]!);
ok(c.ap(['next', '--out', s.out, ...unknown.flatMap(a => ['--redispatch', a])]));
for (const voice of ['native', 'outside']) ok(c.review(path.join(s.out, `ceo-${voice}-prompt.md`)));
for (const voice of ['native', 'outside']) ok(c.ap(['submit', '--out', s.out, '--phase', 'ceo', '--voice', voice, '--result', path.join(s.out, `ceo-${voice}.md`), '--model', voice === 'native' ? 'anthropic/claude-opus-5-5' : 'openai/gpt-6.1-sol']));
phaseVia(c, s.out, 'eng');
expect(readRunJson(s.out).gate_rev).toBe(2);
ok(c.ap(['answer', '--out', s.out, '--gate-rev', '2', '--reply', 'all except p1d']));
phaseVia(c, s.out, 'ceo');
phaseVia(c, s.out, 'eng');
expect(ok(c.ap(['answer', '--out', s.out, '--gate-rev', '3', '--reply', 'all'])).stdout).toContain('status=complete');
ok(c.ap(['export', '--out', s.out]));
expect(ok(c.validate(s.out)).stdout).toMatch(/^ARTIFACT_VALID: run=\S+ status=complete/m);
const final = readRunJson(s.out);
expect(final.counts.decisions_pending).toBe(0);
expect((final.amendments ?? []).filter((a: any) => a.status === 'pending')).toEqual([]);
expect(final.gate_rev).toBe(3);
expectUniqueIds(s.out);
expectReviewedHashes(s.out);
expect(fs.readFileSync(path.join(s.out, `ceo-native.md`), 'utf8')).toContain(final.phases.eng.snapshot);
});
});
+204
View File
@@ -0,0 +1,204 @@
/**
* Gate judgment (plan B2a/B2b, items 8 and 10, with the B-core amendments):
* findings carry `changes`; a user challenge both voices raised on a confirmed
* finding with the same `changes` and both remedies recommends `b`, rendering
* both remedies verbatim and the basis; anything weaker keeps `a` with its
* basis. Reopen and APPLY key on the chosen option's `changes_plan`; accepted
* changes are pending amendments that `next` refuses past (PLAN_AMEND_PENDING)
* until `revise --incorporates` binds them to a changed plan or `--withdraw`
* drops them. `all as-is` keeps every direction. Partial replies resolve
* against persisted decisions. On reruns a challenge without `changes` is an
* agent decision that reopens nothing, rerun prompts carry the decided items,
* and answered items from phases that did not rerun carry forward.
*/
import { afterAll, describe, expect, test } from 'bun:test';
import { spawnSync } from 'node:child_process';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { ROOT, SMALL_PLAN } from './helpers/autoplan-run-fixture';
import { capyRunner, phaseVia, readJsonl, readRunJson, writePlan } from './helpers/autoplan-capy';
const r = capyRunner();
afterAll(() => r.cleanup());
let n = 0;
const esc = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const AGREE = {
native: [{ id: 'F1', severity: 'High', title: 'Ship the backoff behind a flag first', user_challenge: true, changes: 'scope', fix: 'gate the change behind RETRY_BACKOFF=1' }],
outside: [{ id: 'O1', severity: 'High', title: 'Ship the backoff behind a flag first', user_challenge: true, changes: 'scope', fix: 'add a kill switch flag for the new schedule' }],
};
const DISAGREE = {
native: [{ id: 'F1', severity: 'Critical', title: 'Persist attempts in a separate table', file: 'lib/worker.ts', line: 10, user_challenge: true, changes: 'scope', fix: 'new table' }],
outside: [{ id: 'O1', severity: 'Low', title: 'Persist attempts in a separate table', file: 'lib/worker.ts', line: 11, user_challenge: true, changes: 'scope', fix: 'keep the column' }],
};
const MISMATCH = {
native: [{ id: 'F1', severity: 'Medium', title: 'Drop the jitter requirement', user_challenge: true, changes: 'scope', fix: 'remove jitter' }],
outside: [{ id: 'O1', severity: 'Medium', title: 'Drop the jitter requirement', user_challenge: true, changes: 'cost', fix: 'remove jitter' }],
};
function gated(ceo: { native?: unknown[]; outside?: unknown[] }, eng: { native?: unknown[]; outside?: unknown[] }): { out: string; dir: string; plan: string; run: string } {
const dir = path.join(r.host.base, `gj${++n}`);
const plan = writePlan(dir);
const out = path.join(dir, 'run');
expect(r.ap(['next', '--out', out, '--plan', plan, '--no-ui']).status).toBe(0);
phaseVia(r, out, 'ceo', ceo);
expect(phaseVia(r, out, 'eng', eng).outside.stdout).toContain('status=gate_pending');
return { out, dir, plan, run: readRunJson(out).run };
}
const gate = (out: string) => JSON.parse(fs.readFileSync(path.join(out, 'gate.json'), 'utf8'));
const decisions = (out: string) => readJsonl(path.join(out, 'decisions.jsonl'));
const answer = (out: string, rev: number, reply: string, json = false) => r.ap(['answer', '--out', out, '--gate-rev', String(rev), '--reply', reply, ...(json ? ['--json'] : [])]);
const v = (dir: string, text: string) => { const f = path.join(dir, `v${++n}.md`); fs.writeFileSync(f, text); return f; };
describe('changes and user-challenge defaults', () => {
test('`changes` reaches findings.jsonl, the schema and decisions; an unknown value is refused', () => {
const g = gated(AGREE, {});
expect(readJsonl(path.join(g.out, 'ceo-native.md.findings.jsonl'))[0]).toMatchObject({ changes: 'scope', user_challenge: true });
const schema = spawnSync('bun', [path.join(ROOT, 'bin', 'gstack-artifact'), 'schema', 'findings'], { cwd: ROOT, encoding: 'utf8', timeout: 60_000 });
expect(JSON.parse(schema.stdout).properties.changes).toEqual({ enum: ['scope', 'cost', 'direction'] });
expect(decisions(g.out).find(d => d.label === 'UC1')).toMatchObject({ changes: 'scope', recommended: 'b', recommended_basis: 'both voices; remedies shown' });
const dir = path.join(r.host.base, 'badchanges');
const out = path.join(dir, 'run');
expect(r.ap(['next', '--out', out, '--plan', writePlan(dir), '--no-ui']).status).toBe(0);
expect(r.review(path.join(out, 'ceo-native-prompt.md'), { FIXTURE_FINDINGS: JSON.stringify([{ severity: 'Low', title: 'x', user_challenge: true, changes: 'packaging' }]) }).status).toBe(0);
const bad = r.ap(['submit', '--out', out, '--phase', 'ceo', '--voice', 'native', '--result', path.join(out, 'ceo-native.md'), '--model', 'anthropic/claude-opus-5-5']);
expect(bad.status).toBe(1);
expect(bad.stderr).toMatch(/changes "packaging" is not one of scope\|cost\|direction.*\(RESULT_FINDINGS_MISSING\)$/m);
});
test('agreeing voices recommend b with both remedies verbatim; disagreement and mismatched changes keep a with the basis', () => {
const g = gated({ native: [...AGREE.native, ...MISMATCH.native.map(f => ({ ...f, id: 'F2' }))], outside: [...AGREE.outside, ...MISMATCH.outside.map(f => ({ ...f, id: 'O2' }))] }, DISAGREE);
const items = gate(g.out).items;
const agree = items.find((i: any) => i.title.startsWith('Ship the backoff'));
expect(agree).toMatchObject({ kind: 'approval', recommended: 'b', recommended_basis: 'both voices; remedies shown' });
expect(agree.options.find((o: any) => o.key === 'b')).toMatchObject({ changes_plan: true });
expect(agree.options.find((o: any) => o.key === 'b').text).toContain('native: gate the change behind RETRY_BACKOFF=1; outside: add a kill switch flag for the new schedule');
expect(agree.options.find((o: any) => o.key === 'a').changes_plan).toBe(false);
expect(items.find((i: any) => i.title.startsWith('Drop the jitter'))).toMatchObject({ recommended: 'a', recommended_basis: 'plan direction; voices name different changes' });
expect(items.find((i: any) => i.title.startsWith('Persist attempts'))).toMatchObject({ recommended: 'a', recommended_basis: 'plan direction; voices disagree' });
const render = spawnSync('bun', [path.join(ROOT, 'bin', 'gstack-gate'), 'render', path.join(g.out, 'gate.json')], { cwd: ROOT, encoding: 'utf8', timeout: 60_000 });
expect(render.stdout).toMatch(new RegExp(`\\[${agree.id}\\] .*recommended: b \\(both voices; remedies shown\\)`));
expect(render.stdout).toMatch(/recommended: a \(plan direction; voices disagree\)/);
const p1 = items.find((i: any) => i.id === 'p1');
expect(p1.options.find((o: any) => o.key === p1.recommended).text).toContain(`${agree.id}b`);
});
test('a parent reconcile override naming one shared proposal renders that proposal as option b', () => {
const dir = path.join(r.host.base, 'shared');
const out = path.join(dir, 'run');
expect(r.ap(['next', '--out', out, '--plan', writePlan(dir), '--no-ui']).status).toBe(0);
r.review(path.join(out, 'ceo-native-prompt.md'), { FIXTURE_FINDINGS: JSON.stringify(AGREE.native) });
r.review(path.join(out, 'ceo-outside-prompt.md'), { FIXTURE_FINDINGS: JSON.stringify(AGREE.outside) });
const run = readRunJson(out);
expect(r.ap(['submit', '--out', out, '--phase', 'ceo', '--voice', 'native', '--result', path.join(out, 'ceo-native.md'), '--model', 'anthropic/claude-opus-5-5']).status).toBe(0);
const overrides = path.join(dir, 'reconcile.jsonl');
fs.writeFileSync(overrides, JSON.stringify({ outside_id: `${run.run}-ceo-outside-1`, native_id: `${run.run}-ceo-native-1`, disposition: 'confirmed', resolution: 'same change', proposal: 'ship behind RETRY_BACKOFF=1, default off' }) + '\n');
expect(r.ap(['submit', '--out', out, '--phase', 'ceo', '--voice', 'outside', '--result', path.join(out, 'ceo-outside.md'), '--model', 'openai/gpt-6.1-sol', '--reconcile', overrides]).status).toBe(0);
phaseVia(r, out, 'eng', {});
const uc = gate(out).items.find((i: any) => i.title.startsWith('Ship the backoff'));
expect(uc).toMatchObject({ recommended: 'b', recommended_basis: 'both voices; shared proposal' });
expect(uc.options.find((o: any) => o.key === 'b').text).toContain('ship behind RETRY_BACKOFF=1, default off');
});
});
describe('accepted changes are amendments', () => {
test('`all` accepts an agreeing change: APPLY, REOPENED, NEXT revise; next refuses until revise --incorporates binds it', () => {
const g = gated(AGREE, {});
const res = answer(g.out, 1, 'all');
expect(res.status, res.stderr).toBe(0);
expect(res.stdout).toMatch(new RegExp(`^APPLY: ${esc(g.run)}-uc1 Ship the backoff behind a flag first .* -> accept the change both reviewers recommend`, 'm'));
expect(res.stdout).toMatch(/^REOPENED: ceo,eng \(Eng last\) reason=uc1b$/m);
expect(res.stdout).toMatch(new RegExp(`^NEXT: gstack-autoplan revise --out ${esc(g.out)} --plan ${esc(g.plan)} --incorporates ${esc(g.run)}-uc1$`, 'm'));
expect(res.stdout).not.toMatch(/^NEXT: gstack-autoplan next/m);
expect(readRunJson(g.out).amendments).toEqual([expect.objectContaining({ decision: `${g.run}-uc1`, status: 'pending' })]);
const blocked = r.ap(['next', '--out', g.out]);
expect(blocked.status).toBe(3);
expect(blocked.stderr).toMatch(new RegExp(`${esc(g.run)}-uc1.*\\(PLAN_AMEND_PENDING\\)$`, 'm'));
const runFile = path.join(g.out, 'run.json');
const saved = fs.readFileSync(runFile, 'utf8');
fs.writeFileSync(runFile, JSON.stringify({ ...JSON.parse(saved), status: 'complete' }, null, 2) + '\n');
const exported = r.ap(['export', '--out', g.out]);
expect(exported.status).toBe(3);
expect(exported.stderr).toMatch(new RegExp(`${esc(g.run)}-uc1.*\\(PLAN_AMEND_PENDING\\)$`, 'm'));
fs.writeFileSync(runFile, saved);
const unrelated = r.ap(['revise', '--out', g.out, '--plan', v(g.dir, SMALL_PLAN.replace('cap 30 s', 'cap 45 s'))]);
expect(unrelated.status, unrelated.stderr).toBe(0);
expect(unrelated.stdout).toMatch(new RegExp(`^AMENDMENTS: pending=${esc(g.run)}-uc1$`, 'm'));
expect(r.ap(['next', '--out', g.out]).status).toBe(3);
const unchanged = r.ap(['revise', '--out', g.out, '--plan', v(g.dir, SMALL_PLAN.replace('cap 30 s', 'cap 45 s')), '--incorporates', `${g.run}-uc1`]);
expect(unchanged.status).toBe(3);
expect(unchanged.stderr).toMatch(/did not change.*\(PLAN_AMEND_PENDING\)$/m);
const bound = r.ap(['revise', '--out', g.out, '--plan', v(g.dir, SMALL_PLAN.replace('cap 30 s', 'cap 45 s, behind RETRY_BACKOFF=1')), '--incorporates', `${g.run}-uc1`]);
expect(bound.status, bound.stderr).toBe(0);
expect(bound.stdout).toMatch(/^AMENDMENTS: pending=none$/m);
const j = readJsonl(path.join(g.out, 'attempts.jsonl')).find(e => e.event === 'amendment_incorporated');
expect(j).toMatchObject({ decision: `${g.run}-uc1` });
expect(j.old_plan_sha256).not.toBe(j.new_plan_sha256);
expect(r.ap(['next', '--out', g.out]).status).toBe(0);
});
test('`all as-is` keeps every direction; --withdraw is a journaled owner decision', () => {
const g = gated(AGREE, {});
const asIs = answer(g.out, 1, 'all as-is');
expect(asIs.status, asIs.stderr).toBe(0);
expect(asIs.stdout).toMatch(/^APPROVED: gate_rev=1 as-is$/m);
expect(asIs.stdout).not.toMatch(/^APPLY:/m);
expect(decisions(g.out).find(d => d.label === 'UC1')).toMatchObject({ chosen: 'a', status: 'overridden' });
const w = gated(AGREE, {});
expect(answer(w.out, 1, 'all').stdout).toMatch(/^APPLY:/m);
const withdraw = r.ap(['revise', '--out', w.out, '--withdraw', `${w.run}-uc1`]);
expect(withdraw.status, withdraw.stderr).toBe(0);
expect(readJsonl(path.join(w.out, 'attempts.jsonl')).find(e => e.event === 'amendment_withdrawn')).toMatchObject({ decision: `${w.run}-uc1` });
expect(r.ap(['next', '--out', w.out]).status).toBe(0);
});
});
describe('partial replies resolve against persisted decisions', () => {
test('a later `all` never overwrites an earlier override, and p1 alone with approvals unanswered keeps the gate', () => {
const g = gated({}, {});
const p1only = answer(g.out, 1, 'p1a', true);
expect(JSON.parse(p1only.stdout)).toMatchObject({ status: 'gate_pending', outcome: 'gate_pending', pending: ['uc1', 'uc2'] });
const first = answer(g.out, 1, 'uc1b', true);
expect(JSON.parse(first.stdout)).toMatchObject({ outcome: 'gate_pending', pending: ['uc2'] });
const second = answer(g.out, 1, 'all', true);
expect(JSON.parse(second.stdout)).toMatchObject({ outcome: 'reopened', reopened: ['ceo', 'eng'] });
expect(decisions(g.out).find(d => d.label === 'UC1')).toMatchObject({ chosen: 'b', status: 'overridden' });
});
test('validate rejects a complete run with a pending approval', () => {
const g = gated({}, {});
expect(answer(g.out, 1, 'all').stdout).toContain('status=complete');
const file = path.join(g.out, 'decisions.jsonl');
fs.writeFileSync(file, decisions(g.out).map(d => JSON.stringify(d.label === 'UC2' ? { ...d, status: 'pending', chosen: null } : d)).join('\n') + '\n');
r.ap(['export', '--out', g.out]);
const res = r.validate(g.out);
expect(res.status).toBe(1);
expect(res.stderr).toMatch(/UC2.*\(ARTIFACT_GATE_UNSETTLED\)$/m);
});
});
describe('reruns', () => {
test('rerun prompts carry the decided items, a ceo item answered at rev 1 carries forward, and a rerun challenge without `changes` reopens nothing', () => {
const g = gated({}, AGREE);
const res = answer(g.out, 1, 'uc1a uc2b p1a');
expect(res.stdout).toMatch(/^REOPENED: eng \(Eng last\) reason=uc2b$/m);
expect(r.ap(['revise', '--out', g.out, '--withdraw', `${g.run}-uc2`]).status).toBe(0);
const eng = phaseVia(r, g.out, 'eng', {
native: [{ id: 'F9', severity: 'Low', title: 'Name the PR branch per lane', user_challenge: true }],
outside: [{ id: 'O9', severity: 'Medium', title: 'Price the retry storage before shipping', user_challenge: true, changes: 'cost' }],
});
const prompt = fs.readFileSync(path.join(g.out, 'eng-native-prompt.md'), 'utf8');
expect(prompt).toContain('DECIDED AT THE GATE');
expect(prompt).toMatch(/uc2b .*Ship the backoff behind a flag first/);
expect(prompt).toMatch(/uc1a /);
expect(eng.outside.stdout).toContain('status=gate_pending');
const rev2 = decisions(g.out).filter(d => d.gate_rev === 2);
expect(rev2.find(d => d.label === 'UC1')).toMatchObject({ status: 'approved', chosen: 'a', carried_from: `${g.run}-uc1` });
const items = gate(g.out).items;
const demoted = items.find((i: any) => i.title.startsWith('Name the PR branch'));
expect(demoted).toMatchObject({ kind: 'auto', reopens: false });
expect(items.find((i: any) => i.title.startsWith('Price the retry storage'))).toMatchObject({ kind: 'auto' });
expect(items.find((i: any) => i.title.startsWith('Price the retry storage')).reopens).not.toBe(false);
const done = answer(g.out, 2, `${demoted.id}b p1a`, true);
expect(JSON.parse(done.stdout)).toMatchObject({ outcome: 'complete', reopened: [] });
expect(decisions(g.out).find(d => d.gate_rev === 2 && d.label === demoted.id.toUpperCase())).toMatchObject({ chosen: 'b', source: 'agent' });
});
});
+153
View File
@@ -0,0 +1,153 @@
/**
* `gstack-autoplan submit --record` (plan B1b, item 7): a phase reviewed by a
* host subagent binds from its own record. Accepted shapes are the canonical
* fence, an exact `No findings.` line, or exactly one findings table or
* severity-tagged list; anything else, or two shapes, is refused. A record
* without the INPUT receipt binds only with `--attest <reason>` and an
* explicit `--attempt` (receipt: attested); a wrong receipt always refuses.
* `--voice outside --unavailable|--failed` closes the phase native_only, and
* the terminal outcome is journaled so `next` and `resume` never redispatch
* or resurrect it. Runs on the simulated Capy host.
*/
import { afterAll, describe, expect, test } from 'bun:test';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { capyRunner, readJsonl, readRunJson, writePlan } from './helpers/autoplan-capy';
const r = capyRunner();
afterAll(() => r.cleanup());
let n = 0;
function start(): string {
const dir = path.join(r.host.base, `rec${++n}`);
const plan = writePlan(dir);
const out = path.join(dir, 'run');
const first = r.ap(['next', '--out', out, '--plan', plan, '--no-ui']);
if (first.status !== 0) throw new Error(`first next failed: ${first.stderr}\n${first.stdout}`);
return out;
}
const write = (out: string, name: string, text: string) => { const f = path.join(path.dirname(out), name); fs.writeFileSync(f, text); return f; };
const snapshotOf = (out: string, phase = 'ceo') => readRunJson(out).phases[phase].snapshot as string;
const TABLE = `# CEO review (hand-run subagent)
Prose about the plan.
| # | Severity | Finding | File | Fix |
|---|---|---|---|---|
| 1 | High | Retry cap is unbounded on restart | lib/worker.ts:10 | persist the cap |
| 2 | Medium | Split persistence change into its own plan | | split it |
`;
const list = (sha: string) => `INPUT: ceo ${sha}
Review prose.
- [High] Jitter bounds are untested
- [Low] Name the environment variable
`;
describe('submit --record', () => {
test('a table record without a receipt is refused unless attested, and an attested bind names its attempt', () => {
const out = start();
const rec = write(out, 'ceo-table.md', TABLE);
const before = fs.readFileSync(path.join(out, 'run.json'), 'utf8');
const bare = r.ap(['submit', '--out', out, '--phase', 'ceo', '--voice', 'native', '--record', rec, '--model', 'anthropic/claude-opus-5-5']);
expect(bare.status).toBe(1);
expect(bare.stderr).toMatch(/--attest.*\(RESULT_RECEIPT_MISSING\)$/m);
const noAttempt = r.ap(['submit', '--out', out, '--phase', 'ceo', '--voice', 'native', '--record', rec, '--model', 'anthropic/claude-opus-5-5', '--attest', 'subagent ran from the prompt file']);
expect(noAttempt.status).toBe(3);
expect(noAttempt.stderr).toMatch(/--attempt.*\(ATTEMPT_MISMATCH\)$/m);
expect(fs.readFileSync(path.join(out, 'run.json'), 'utf8')).toBe(before);
const attempt = readRunJson(out).phases.ceo.native.attempt;
const ok = r.ap(['submit', '--out', out, '--phase', 'ceo', '--voice', 'native', '--record', rec, '--model', 'anthropic/claude-opus-5-5', '--attest', 'subagent ran from the prompt file', '--attempt', attempt]);
expect(ok.status, ok.stderr).toBe(0);
expect(ok.stdout).toMatch(new RegExp(`^BOUND: ${attempt} phase=ceo voice=native .*findings=2 .*receipt=attested`, 'm'));
const v = readRunJson(out).phases.ceo.native;
expect(v).toMatchObject({ status: 'completed', receipt: 'attested', attest_reason: 'subagent ran from the prompt file', findings_source: 'table' });
expect(fs.readFileSync(path.join(out, 'ceo-native.md'), 'utf8')).toBe(TABLE);
const rows = readJsonl(path.join(out, 'ceo-native.md.findings.jsonl'));
expect(rows.map(x => [x.severity, x.title, x.file ?? null, x.line ?? null])).toEqual([['High', 'Retry cap is unbounded on restart', 'lib/worker.ts', 10], ['Medium', 'Split persistence change into its own plan', null, null]]);
expect(readRunJson(out).deviations.some((d: string) => /attested.*ceo native/.test(d))).toBe(true);
});
test('a record with the receipt binds a severity-tagged list without --attempt, and `No findings.` binds zero rows', () => {
const out = start();
const ok = r.ap(['submit', '--out', out, '--phase', 'ceo', '--voice', 'native', '--record', write(out, 'l.md', list(snapshotOf(out))), '--model', 'anthropic/claude-opus-5-5']);
expect(ok.status, ok.stderr).toBe(0);
expect(ok.stdout).toMatch(/^BOUND: \S+-a1 phase=ceo voice=native .*findings=2 .*receipt=verified/m);
expect(readRunJson(out).phases.ceo.native).toMatchObject({ receipt: 'verified', findings_source: 'list' });
const none = r.ap(['submit', '--out', out, '--phase', 'ceo', '--voice', 'outside', '--record', write(out, 'none.md', `INPUT: ceo ${snapshotOf(out)}\n\nRead it all.\n\nNo findings.\n`), '--model', 'openai/gpt-6.1-sol']);
expect(none.status, none.stderr).toBe(0);
expect(none.stdout).toMatch(/^BOUND: \S+-a2 phase=ceo voice=outside .*findings=0/m);
expect(none.stdout).toMatch(/^PHASE_CLOSED: ceo .*coverage=both/m);
});
test('refusals write nothing: a wrong receipt (even attested), prose with no findings shape, two shapes, an unknown severity', () => {
const out = start();
const attempt = readRunJson(out).phases.ceo.native.attempt;
const before = { run: fs.readFileSync(path.join(out, 'run.json'), 'utf8'), journal: readJsonl(path.join(out, 'attempts.jsonl')).length };
const submit = (text: string, extra: string[] = []) => r.ap(['submit', '--out', out, '--phase', 'ceo', '--voice', 'native', '--record', write(out, `bad${++n}.md`, text), '--model', 'anthropic/claude-opus-5-5', ...extra]);
const wrong = submit(`INPUT: ceo ${'0'.repeat(64)}\n\nNo findings.\n`, ['--attest', 'trust me', '--attempt', attempt]);
expect(wrong.status).toBe(1);
expect(wrong.stderr).toMatch(/\(RESULT_RECEIPT_MISSING\)$/m);
const prose = submit(`INPUT: ceo ${snapshotOf(out)}\n\nThe plan looks fine overall but I have concerns.\n`);
expect(prose.status).toBe(1);
expect(prose.stderr).toMatch(/table.*list.*No findings\..*\(RESULT_FINDINGS_MISSING\)$/m);
const two = submit(`${list(snapshotOf(out))}\n${TABLE}`);
expect(two.status).toBe(1);
expect(two.stderr).toMatch(/more than one findings shape.*\(RESULT_FINDINGS_MISSING\)$/m);
const spicy = submit(`INPUT: ceo ${snapshotOf(out)}\n\n| Severity | Title |\n|---|---|\n| Spicy | Too hot |\n`);
expect(spicy.status).toBe(1);
expect(spicy.stderr).toMatch(/row 1: severity "Spicy".*\(RESULT_FINDINGS_MISSING\)$/m);
expect(fs.readFileSync(path.join(out, 'run.json'), 'utf8')).toBe(before.run);
expect(readJsonl(path.join(out, 'attempts.jsonl')).length).toBe(before.journal);
});
test('the canonical --result path binds without --attempt when exactly one attempt is open', () => {
const out = start();
expect(r.review(path.join(out, 'ceo-native-prompt.md')).status).toBe(0);
const ok = r.ap(['submit', '--out', out, '--phase', 'ceo', '--voice', 'native', '--result', path.join(out, 'ceo-native.md'), '--model', 'anthropic/claude-opus-5-5']);
expect(ok.status, ok.stderr).toBe(0);
expect(ok.stdout).toMatch(/^BOUND: \S+-a1 phase=ceo voice=native .*findings=3/m);
});
test('a phase that next has not opened is refused with the next command', () => {
const out = start();
const res = r.ap(['submit', '--out', out, '--phase', 'eng', '--voice', 'native', '--record', write(out, 'eng.md', 'No findings.\n'), '--model', 'anthropic/claude-opus-5-5']);
expect(res.status).toBe(3);
expect(res.stderr).toMatch(new RegExp(`run gstack-autoplan next --out ${out.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}.*\\(PHASE_NOT_AWAITING\\)$`, 'm'));
});
test('--voice outside --unavailable closes the phase native_only; next never redispatches it and resume never resurrects it', () => {
const out = start();
const outsideAttempt = readRunJson(out).phases.ceo.outside.attempt;
const un = r.ap(['submit', '--out', out, '--phase', 'ceo', '--voice', 'outside', '--unavailable', 'no codex on this host']);
expect(un.status, un.stderr).toBe(0);
expect(un.stdout).toMatch(/^OUTSIDE: ceo unavailable/m);
const j = readJsonl(path.join(out, 'attempts.jsonl'));
expect(j.find(e => e.event === 'outside_unavailable')).toMatchObject({ phase: 'ceo', attempt: outsideAttempt, detail: 'no codex on this host' });
expect(readRunJson(out).phases.ceo.outside).toMatchObject({ status: 'unavailable', detail: 'no codex on this host' });
const again = r.ap(['next', '--out', out]);
expect(again.status, again.stderr).toBe(0);
expect(again.stdout).not.toMatch(/voice=outside/);
expect(again.stdout).toMatch(/^ATTEMPT: \S+ phase=ceo voice=native .*\(open\)$/m);
const nativeAttempt = readRunJson(out).phases.ceo.native.attempt;
const resumed = r.ap(['resume', '--out', out]);
expect(resumed.stdout).toMatch(/execution_unknown=1 /);
expect(resumed.stdout).toContain(`EXECUTION_UNKNOWN: ${nativeAttempt}\n`);
expect(resumed.stdout).not.toContain(outsideAttempt);
expect(readRunJson(out).phases.ceo.outside.status).toBe('unavailable');
expect(r.review(path.join(out, 'ceo-native-prompt.md')).status).toBe(0);
const close = r.ap(['submit', '--out', out, '--phase', 'ceo', '--voice', 'native', '--result', path.join(out, 'ceo-native.md'), '--model', 'anthropic/claude-opus-5-5']);
expect(close.stdout).toMatch(/^PHASE_CLOSED: ceo .*coverage=native_only/m);
});
test('--failed records a failed outside voice and closes the phase once native is bound', () => {
const out = start();
expect(r.review(path.join(out, 'ceo-native-prompt.md')).status).toBe(0);
expect(r.ap(['submit', '--out', out, '--phase', 'ceo', '--voice', 'native', '--result', path.join(out, 'ceo-native.md'), '--model', 'anthropic/claude-opus-5-5']).status).toBe(0);
const failed = r.ap(['submit', '--out', out, '--phase', 'ceo', '--voice', 'outside', '--failed', 'runner exited 1']);
expect(failed.status, failed.stderr).toBe(0);
expect(failed.stdout).toMatch(/^PHASE_CLOSED: ceo .*coverage=native_only/m);
expect(readRunJson(out).phases.ceo.outside).toMatchObject({ status: 'failed', detail: 'runner exited 1' });
});
});
+174
View File
@@ -0,0 +1,174 @@
/**
* `gstack-autoplan revise --out <dir> --plan <v2>` (plan B1c, item 9): the run
* reviews a revised plan in the same run directory. Drift is the
* Implementation-plan hash: every opened phase whose reviewed hash differs is
* reopened (open attempts superseded), newly in-scope phases are added, the
* archives keep `.r<rev>` names with `.v<k>` only on collision, revise at
* gate_pending is its own journal event and the next gate is gate_rev + 1.
* `next` and `answer` print the drift hints; a plan missing one of the two
* sections refuses with PLAN_SECTIONS_INVALID and `next --scaffold` reviews a
* scaffolded copy. Runs on the simulated Capy host.
*/
import { afterAll, describe, expect, test } from 'bun:test';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { SMALL_PLAN } from './helpers/autoplan-run-fixture';
import { capyRunner, phaseVia, readJsonl, readRunJson, writePlan } from './helpers/autoplan-capy';
const r = capyRunner();
afterAll(() => r.cleanup());
let n = 0;
const V2 = SMALL_PLAN.replace('cap 30 s', 'cap 60 s, dead-letter after 8 attempts');
const RECORD_ONLY = SMALL_PLAN + '\nReviewed once; notes only.\n';
const DX_V2 = SMALL_PLAN.replace('Tests cover', 'Add a CLI flag --max-retries and an error message for a bad value. Tests cover');
function start(args: string[] = [], text = SMALL_PLAN): { out: string; dir: string; plan: string } {
const dir = path.join(r.host.base, `rev${++n}`);
const plan = writePlan(dir, text);
const out = path.join(dir, 'run');
const first = r.ap(['next', '--out', out, '--plan', plan, '--no-ui', ...args]);
if (first.status !== 0) throw new Error(`first next failed: ${first.stderr}\n${first.stdout}`);
return { out, dir, plan };
}
const v2File = (dir: string, text = V2) => { const f = path.join(dir, `plan-v2-${++n}.md`); fs.writeFileSync(f, text); return f; };
function gated(): { out: string; dir: string; plan: string } {
const s = start();
phaseVia(r, s.out, 'ceo', { native: [], outside: [] });
expect(phaseVia(r, s.out, 'eng', { native: [], outside: [] }).outside.stdout).toContain('status=gate_pending');
return s;
}
describe('revise', () => {
test('revise after ceo closes reopens ceo, and dx and eng then review the v2 snapshot', () => {
const s = start(['--developer-tool']);
phaseVia(r, s.out, 'ceo', { native: [], outside: [] });
const v1 = readRunJson(s.out).phases.ceo.snapshot;
const rev = r.ap(['revise', '--out', s.out, '--plan', v2File(s.dir)]);
expect(rev.status, rev.stderr).toBe(0);
expect(rev.stdout).toMatch(/^REVISED: plan=[0-9a-f]{12} reopened=ceo$/m);
expect(rev.stdout).toContain('GSTACK_RESULT: skill=autoplan status=running');
for (const phase of ['ceo', 'dx', 'eng']) {
const p = phaseVia(r, s.out, phase, { native: [], outside: [] });
expect(p.next.stdout).toMatch(new RegExp(`^PHASE: ${phase} snapshot=`, 'm'));
expect(fs.readFileSync(path.join(s.out, `${phase}-native-prompt.md`), 'utf8')).toContain('dead-letter after 8 attempts');
expect(fs.readFileSync(path.join(s.out, `${phase}-outside-prompt.md`), 'utf8')).toContain('dead-letter after 8 attempts');
}
const run = readRunJson(s.out);
expect(run.status).toBe('gate_pending');
expect(new Set(['ceo', 'dx', 'eng'].map(p => run.phases[p].snapshot)).size).toBe(1);
expect(run.phases.ceo.snapshot).not.toBe(v1);
expect(readJsonl(path.join(s.out, 'attempts.jsonl')).filter(e => e.event === 'plan_revised')).toHaveLength(1);
expect(fs.existsSync(path.join(s.out, 'ceo-native.r0.md'))).toBe(true);
});
test('a Review-record-only edit keeps every phase; an open phase whose hash changed has its attempts superseded', () => {
const s = start();
phaseVia(r, s.out, 'ceo', { native: [], outside: [] });
const same = r.ap(['revise', '--out', s.out, '--plan', v2File(s.dir, RECORD_ONLY)]);
expect(same.status, same.stderr).toBe(0);
expect(same.stdout).toMatch(/reopened=none/);
expect(readRunJson(s.out).phases.ceo.closed_at).toBeTruthy();
const eng = r.ap(['next', '--out', s.out]);
expect(eng.stdout).toMatch(/^PHASE: eng /m);
const open = readRunJson(s.out).phases.eng;
const changed = r.ap(['revise', '--out', s.out, '--plan', v2File(s.dir)]);
expect(changed.stdout).toMatch(/reopened=ceo,eng/);
const superseded = readJsonl(path.join(s.out, 'attempts.jsonl')).filter(e => e.event === 'superseded').map(e => e.attempt);
expect(superseded.sort()).toEqual([open.native.attempt, open.outside.attempt].sort());
});
test('revise at gate_pending is its own journal event, reruns on v2, and the next gate is gate_rev 2', () => {
const s = gated();
const rev = r.ap(['revise', '--out', s.out, '--plan', v2File(s.dir), '--json']);
expect(rev.status, rev.stderr).toBe(0);
expect(JSON.parse(rev.stdout)).toMatchObject({ status: 'running', reopened: ['ceo', 'eng'] });
const j = readJsonl(path.join(s.out, 'attempts.jsonl'));
expect(j.find(e => e.event === 'plan_revised')).toMatchObject({ at_gate_rev: 1 });
expect(j.filter(e => e.event === 'answered')).toEqual([]);
expect(fs.existsSync(path.join(s.out, 'gate.r1.json'))).toBe(true);
phaseVia(r, s.out, 'ceo', { native: [], outside: [] });
phaseVia(r, s.out, 'eng', { native: [], outside: [] });
expect(fs.readFileSync(path.join(s.out, 'eng-native-prompt.md'), 'utf8')).toContain('dead-letter after 8 attempts');
expect(readRunJson(s.out)).toMatchObject({ status: 'gate_pending', gate_rev: 2 });
});
test('a second revise never overwrites the first archive', () => {
const s = gated();
expect(r.ap(['revise', '--out', s.out, '--plan', v2File(s.dir)]).status).toBe(0);
const first = fs.readFileSync(path.join(s.out, 'ceo-native.r1.md'));
phaseVia(r, s.out, 'ceo', { native: [], outside: [] });
expect(r.ap(['revise', '--out', s.out, '--plan', v2File(s.dir, V2.replace('jitter 10%', 'jitter 20%'))]).status).toBe(0);
expect(fs.readFileSync(path.join(s.out, 'ceo-native.r1.md')).equals(first)).toBe(true);
expect(fs.existsSync(path.join(s.out, 'ceo-native.r1.v2.md'))).toBe(true);
});
test('revise adds a newly in-scope phase and never removes one', () => {
const s = start();
expect(readRunJson(s.out).phase_order).toEqual(['ceo', 'eng']);
const rev = r.ap(['revise', '--out', s.out, '--plan', v2File(s.dir, DX_V2)]);
expect(rev.status, rev.stderr).toBe(0);
const run = readRunJson(s.out);
expect(run.phase_order).toEqual(['ceo', 'dx', 'eng']);
expect(run.required_phases).toEqual(['ceo', 'dx', 'eng']);
expect(run.phases.dx.skipped).toBeUndefined();
expect(r.ap(['revise', '--out', s.out, '--plan', v2File(s.dir)]).status).toBe(0);
expect(readRunJson(s.out).phase_order).toEqual(['ceo', 'dx', 'eng']);
});
test('an attested bind is refused when the plan was revised after its attempt was dispatched', () => {
const s = start();
const attempt = readRunJson(s.out).phases.ceo.native.attempt;
expect(r.ap(['revise', '--out', s.out, '--plan', v2File(s.dir, RECORD_ONLY)]).stdout).toMatch(/reopened=none/);
const rec = path.join(s.dir, 'hand.md');
fs.writeFileSync(rec, 'Hand review.\n\nNo findings.\n');
const res = r.ap(['submit', '--out', s.out, '--phase', 'ceo', '--voice', 'native', '--record', rec, '--model', 'anthropic/claude-opus-5-5', '--attest', 'ran it', '--attempt', attempt]);
expect(res.status).toBe(1);
expect(res.stderr).toMatch(/revised after .*dispatched.*\(RESULT_RECEIPT_MISSING\)$/m);
});
});
describe('drift hints', () => {
test('next prints PLAN_CHANGED when the source plan changed since the snapshot', () => {
const s = start();
fs.writeFileSync(s.plan, V2);
const res = r.ap(['next', '--out', s.out]);
expect(res.stdout).toMatch(new RegExp(`^PLAN_CHANGED: ${s.plan.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')} differs from the reviewed plan; run gstack-autoplan revise --out \\S+ --plan \\S+$`, 'm'));
});
test('answer prints PLAN_SOURCE_CHANGED when a reply reopens phases after the source plan changed', () => {
const s = gated();
fs.writeFileSync(s.plan, V2);
const res = r.ap(['answer', '--out', s.out, '--gate-rev', '1', '--reply', 'all except p1d']);
expect(res.stdout).toMatch(/^REOPENED: ceo,eng \(Eng last\)/m);
expect(res.stdout).toMatch(/^PLAN_SOURCE_CHANGED: .* differs from the reviewed plan; run gstack-autoplan revise --out \S+ --plan \S+$/m);
});
});
describe('plan sections', () => {
test('only an Implementation plan: next refuses with PLAN_SECTIONS_INVALID and stdout; --scaffold reviews a copy', () => {
const dir = path.join(r.host.base, 'scaffold');
const text = '# P\n\n## Implementation plan\n\nDo the thing in lib/x.ts with a 30 s timeout.\n';
const plan = writePlan(dir, text);
const refused = r.ap(['next', '--out', path.join(dir, 'a'), '--plan', plan, '--no-ui']);
expect(refused.status).toBe(3);
expect(refused.stderr).toMatch(/no "## Review record".*--scaffold.*\(PLAN_SECTIONS_INVALID\)$/m);
expect(refused.stdout).toContain('GSTACK_RESULT: skill=autoplan status=refused');
const ok = r.ap(['next', '--out', path.join(dir, 'b'), '--plan', plan, '--no-ui', '--scaffold']);
expect(ok.status, ok.stderr).toBe(0);
expect(ok.stdout).toMatch(/^SCAFFOLDED: added "## Review record"/m);
expect(ok.stdout).toMatch(/^PHASE: ceo /m);
expect(fs.readFileSync(plan, 'utf8')).toBe(text);
expect(fs.readFileSync(path.join(dir, 'b', 'plan-source.md'), 'utf8')).toContain('## Review record');
});
test('duplicate or reversed headings refuse with the code even with --scaffold; an empty section says so', () => {
const dir = path.join(r.host.base, 'ambiguous');
for (const [name, text] of [['dup', '# P\n\n## Implementation plan\n\nA.\n\n## Implementation plan\n\nB.\n\n## Review record\n'], ['rev', '# P\n\n## Review record\n\nx\n\n## Implementation plan\n\nA.\n']] as const) {
const plan = writePlan(path.join(dir, name), text);
const res = r.ap(['next', '--out', path.join(dir, name, 'run'), '--plan', plan, '--no-ui', '--scaffold']);
expect(res.status).toBe(3);
expect(res.stderr).toMatch(/\(PLAN_SECTIONS_INVALID\)$/m);
}
const plan = writePlan(path.join(dir, 'empty'), '# P\n\n## Implementation plan\n\n## Review record\n');
const res = r.ap(['next', '--out', path.join(dir, 'empty', 'run'), '--plan', plan, '--no-ui']);
expect(res.status).toBe(3);
expect(res.stderr).toMatch(/Implementation plan is empty.*\(PLAN_SECTIONS_INVALID\)$/m);
});
});
+4
View File
@@ -85,6 +85,10 @@
{ "file": "lib/autoplan-export.ts", "movedFrom": "(new: the unattended export file set, B2/B12)" },
{ "file": "lib/spec-review-cap.ts", "movedFrom": "(new: the spec-review loop at the cap: numbered gaps and deterministic fix verification, B8)" },
{ "file": "bin/gstack-autoplan", "movedFrom": "(new: thin entrypoint over lib/autoplan-run.ts, B2)" },
{ "file": "lib/autoplan-plan-text.ts", "movedFrom": "(new: plan wrap, Implementation-plan review hash, scaffold and drift lines, field wave B1c)" },
{ "file": "lib/autoplan-admission.ts", "movedFrom": "(new: install verdict and Bun minimum admission for mutating autoplan verbs, field wave B-core)" },
{ "file": "lib/autoplan-migrate.ts", "movedFrom": "(new: gstack_version stamp and legacy decision-id migration at load, field wave B1a)" },
{ "file": "lib/autoplan-revise.ts", "movedFrom": "(new: the revise transaction, amendments and its resume recovery, field wave B1c)" },
{ "file": "bin/gstack-outside-voice", "movedFrom": "(new: thin entrypoint over lib/outside-voice-runner.ts, B3)" },
{ "file": "lib/plan-reality.ts", "movedFrom": "(new: the reality-row table, the REALITY: line grammar and the row checker, D1)" },
{ "file": "bin/gstack-plan-reality", "movedFrom": "(new: thin entrypoint over lib/plan-reality.ts, D1)" },
+73
View File
@@ -0,0 +1,73 @@
/**
* autoplan-capy — the unattended runner on the simulated Capy host
* (test/helpers/capy-host.ts): Bun 1.3.14 first on PATH unless a command is
* launched with the GSTACK_LAUNCH PATH (verified Bun 1.4.2 first), no codex,
* an ephemeral home, GSTACK_CAPY=1 and an install verdict for this checkout.
* Every spawn carries a timeout.
*/
import { spawnSync, type SpawnSyncReturns } from 'node:child_process';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { capyHost, type CapyHost } from './capy-host';
import { AUTOPLAN, REVIEWER, ROOT, SMALL_PLAN } from './autoplan-run-fixture';
const SESSION_KIND_VAR = ['GSTACK', 'SESSION', 'KIND'].join('_');
const RUNNER_KIND = 'un' + 'attended';
export const HEAD = spawnSync('git', ['-C', ROOT, 'rev-parse', 'HEAD'], { encoding: 'utf8', timeout: 30_000 }).stdout.trim();
export interface CapyRunner {
host: CapyHost; env: Record<string, string>; verdict: string;
ap(args: string[], extra?: Record<string, string>): SpawnSyncReturns<string>;
review(prompt: string, extra?: Record<string, string>): SpawnSyncReturns<string>;
validate(out: string): SpawnSyncReturns<string>;
writeVerdict(fields: Record<string, string>): void;
cleanup(): void;
}
/** A launched command on the Capy host: GSTACK_LAUNCH PATH, unattended, GSTACK_CAPY=1, an ok verdict for this HEAD. */
export function capyRunner(o: { launched?: boolean; verdict?: Record<string, string> | null } = {}): CapyRunner {
const host = capyHost({ verifiedBun: '1.4.2' });
const verdict = path.join(host.stateRoot, 'install-verdict');
const env: Record<string, string> = {
...host.env, PATH: o.launched === false ? host.env.PATH! : host.launchPath,
[SESSION_KIND_VAR]: RUNNER_KIND, GSTACK_CAPY: '1', GSTACK_INSTALL_VERDICT: verdict,
};
const writeVerdict = (fields: Record<string, string>) => {
fs.mkdirSync(path.dirname(verdict), { recursive: true });
fs.writeFileSync(verdict, JSON.stringify(fields) + '\n');
};
if (o.verdict !== null) writeVerdict(o.verdict ?? { trailer: 'gstack: ok 1.91.95.0 for=autoplan', head: HEAD, checkout: fs.realpathSync(ROOT), at: new Date().toISOString() });
const run = (args: string[], extra: Record<string, string> = {}) =>
spawnSync('bun', args, { cwd: ROOT, env: { ...env, ...extra }, encoding: 'utf8', timeout: 120_000 });
return {
host, env, verdict, writeVerdict,
ap: (args, extra) => run([AUTOPLAN, ...args], extra),
review: (prompt, extra) => run([REVIEWER, prompt], extra),
validate: out => run([path.join(ROOT, 'bin', 'gstack-artifact'), 'validate', path.join(out, 'run.json')]),
cleanup: host.cleanup,
};
}
export function writePlan(dir: string, text = SMALL_PLAN): string {
fs.mkdirSync(dir, { recursive: true });
const plan = path.join(dir, 'plan.md');
fs.writeFileSync(plan, text);
return plan;
}
export const readJsonl = (file: string): any[] => (fs.existsSync(file) ? fs.readFileSync(file, 'utf8').split('\n').filter(Boolean).map(l => JSON.parse(l)) : []);
export const readRunJson = (out: string): any => JSON.parse(fs.readFileSync(path.join(out, 'run.json'), 'utf8'));
/** next (expects exit 0), fixture-review both prompts, submit both through --result. */
export function phaseVia(r: CapyRunner, out: string, phase: string, findings: { native?: unknown[]; outside?: unknown[] } = {}): { next: SpawnSyncReturns<string>; native: SpawnSyncReturns<string>; outside: SpawnSyncReturns<string> } {
const next = r.ap(['next', '--out', out]);
if (next.status !== 0) throw new Error(`next failed: ${next.stderr}\n${next.stdout}`);
for (const voice of ['native', 'outside'] as const) {
const f = findings[voice];
const rv = r.review(path.join(out, `${phase}-${voice}-prompt.md`), f ? { FIXTURE_FINDINGS: JSON.stringify(f) } : {});
if (rv.status !== 0) throw new Error(`fixture reviewer failed: ${rv.stderr}`);
}
const run = readRunJson(out);
const native = r.ap(['submit', '--out', out, '--phase', phase, '--voice', 'native', '--result', path.join(out, `${phase}-native.md`), '--model', 'anthropic/claude-opus-5-5', '--attempt', run.phases[phase].native.attempt, '--runner', 'host-subagent']);
const outside = r.ap(['submit', '--out', out, '--phase', phase, '--voice', 'outside', '--result', path.join(out, `${phase}-outside.md`), '--model', 'openai/gpt-6.1-sol', '--attempt', run.phases[phase].outside.attempt, '--runner', 'host-subagent']);
return { next, native, outside };
}