Files
gstack/test/install-verdict.test.ts
Garry Tanandcapy-ai[bot] 6665e20d56 v1.91.236.0 feat(ship-queue): require a current /ship receipt before READY, train build and train merge (#3438)
* feat(ship-queue): require a current /ship receipt before READY, train build and train merge

Garry, 2026-10-25: use the gstack ship skill before merging PRs. ready printed
SHIP_RECEIPT: none as information and returned ok, so the pool was READY'd without
/ship. Now ready, train build (dry run included) and verify --train refuse with
QUEUE_RECEIPT_MISSING or QUEUE_RECEIPT_STALE from the PR body alone; verify accepts
the receipt written for the pinned head when the current head is its stamp commit.

gstack-ship-receipt result is /ship's terminal step (GSTACK_RESULT: skill=ship,
run dir with the head and receipt), unattended /ship may update an existing PR's
body, and INSTALL_UNVERIFIED with no verdict names the project directory to
install from (GSTA-85).

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(ship): pin Step 22 in workflow-required-steps and tighten the ship skeleton

Gate edit: test/fixtures/workflow-required-steps.json pins the new
"## Step 22: Terminal line (every run)" heading (strengthens: a later
edit that drops the terminal line turns the required-steps test red).
Steps 14, 16, 18, 20 and 22 prose tightened so the carve-guards raise
shrinks to 81,600 / 1.6740 (measured skeleton 81,535, union 314,027).

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix: pre-landing review fixes (ready at the stamp commit, screen on a stale merge ref, ship-run.json)

- ready accepts the pin's receipt at a slot's stamp commit (plus the slot-2
  merge of main), as verify --train already did; a real edit stays STALE
- ready --screen refuses QUEUE_SCREEN_STALE_REF when refs/pull/<n>/merge is
  missing or merges an older head; a finished screen that planned 0 cases is
  never followed, and train build seats that slot unscreened (GSTA-95 on #3431)
- gstack-ship-receipt result writes ship-run.json (a /ship record, not a run
  v1 manifest), validates --head, and prints status=incomplete on gh failures
- bindReceipt takes an optional pr and renders one-line SHIP_RECEIPT lines
- tests for every state-matrix cell the review named; the train fixture's
  merge ref is a real merge commit

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix: cycle-2 review fixes (private screen scratch, no bare-merge receipt reuse, 12-hex --head, one receipt scrub) and align the unattended hook text with the GSTACK_RESULT writers (GSTA-91)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix: adversarial-review fixes (result refuses an unpushed HEAD, resolves --out and reports unwritable records; ready names a failed merge-ref fetch; unattended /ship never runs gh pr ready)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix: close review-cycle gaps (a stamp must change a stamp file, result requires a full --head and fails closed without headRefOid, digits-only --pr) with tests for every cycle-3 branch

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test: give the fsck-rejected merge-ref spawns a timeout (spawnsync tripwire)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix: detect a hollow screen from the plan it uploads (no paid-plan artifact or nothing planned), correct two troubleshooting facts, and normalise result --pr

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(hollow-screen): read manifest.json by name, list artifacts by name, say when the plan is unreadable; lowercase result --head

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(hollow-screen): expired and unreadable run rows, uppercase --head; docs name every SCREEN: literal

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(train build): say when the repository slug is unreadable instead of counting the screen silently

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix: name an ls-remote failure in ready --screen, refuse result --pr with --body, cap echoed receipt errors, tell an unreadable install verdict from a missing one

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test: ls-remote refusal, --pr with --body, capped receipt errors, unreadable slug and verdict (both twins); docs name the new literals

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix: scrub Unicode line separators from receipt text, report an unreadable --body as incomplete, refuse unsupported verdict schemas in the bash twin

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
2026-10-26 09:03:59 +00:00

163 lines
12 KiB
TypeScript

/**
* lib/install-verdict.ts and bin/gstack-install-verdict.sh: the one reader of
* the install verdict gstack-capy-install writes, shared by gstack-skill-start
* (Bash) and gstack-autoplan's mutating verbs (TypeScript). Pinned on the
* admission decision (who must hold a verdict, what a verdict admits), the
* verdict path keyed by the source checkout's realpath, and the source-root
* resolution through an installed root's .source-path.
*/
import { afterEach, describe, expect, test } from 'bun:test';
import { execFileSync, spawnSync } from 'child_process';
import { createHash } from 'crypto';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { checkInstallAdmission, installVerdictPath, legacyInstallVerdictPath, readInstallVerdict, sourceRoot } from '../lib/install-verdict';
const ROOT = path.resolve(import.meta.dir, '..');
const REAL = fs.realpathSync(ROOT);
const HEAD = execFileSync('git', ['-C', ROOT, 'rev-parse', 'HEAD'], { encoding: 'utf8', timeout: 30_000 }).trim();
const dirs: string[] = [];
afterEach(() => { for (const d of dirs.splice(0)) fs.rmSync(d, { recursive: true, force: true }); });
const tmp = () => { const d = fs.mkdtempSync(path.join(os.tmpdir(), 'install-verdict-')); dirs.push(d); return d; };
const writeVerdict = (stateRoot: string, over: Record<string, unknown> = {}, file = installVerdictPath(REAL, stateRoot, (over.project as string) || null)) => {
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, JSON.stringify({
schema_version: 1, checkout: REAL, head: HEAD, for: 'autoplan',
required: ['claude', 'codex', 'patch', 'runtime', 'pins', 'state-root', 'privacy', 'cores', 'revision'],
pins_planning_only: false, ok: true, trailer: 'gstack: ok 9.9.9.9 for=autoplan', min_revision: 'unknown', launch_path: '', time: '2026-10-12T00:00:00Z', ...over,
}));
return file;
};
describe('install verdict (TypeScript)', () => {
test('the verdict path is keyed by sha256 of the checkout and of the project (or "none") under <state root>/install-verdicts; the schema 1 path stays readable', () => {
const sr = tmp();
const sha = (t: string) => createHash('sha256').update(t).digest('hex');
expect(installVerdictPath(REAL, sr)).toBe(path.join(sr, 'install-verdicts', sha(REAL), `${sha('none')}.json`));
expect(installVerdictPath(REAL, sr, '/p/a')).toBe(path.join(sr, 'install-verdicts', sha(REAL), `${sha('/p/a')}.json`));
expect(legacyInstallVerdictPath(REAL, sr)).toBe(path.join(sr, 'install-verdicts', `${sha(REAL)}.json`));
const bashPath = spawnSync('bash', ['-c', `. "${path.join(ROOT, 'bin/gstack-install-verdict.sh')}" && gstack_install_verdict_path "$1" "$2" "$3" && gstack_install_verdict_path "$1" "$2" "" && gstack_install_verdict_legacy_path "$1" "$2"`, '_', sr, REAL, '/p/a'], { encoding: 'utf8', timeout: 30_000 });
expect(bashPath.stdout.trim().split('\n')).toEqual([installVerdictPath(REAL, sr, '/p/a'), installVerdictPath(REAL, sr), legacyInstallVerdictPath(REAL, sr)]);
expect(readInstallVerdict(path.join(sr, 'nope.json'))).toBeNull();
expect(readInstallVerdict(writeVerdict(sr))?.head).toBe(HEAD);
});
test('admission: only unattended and Capy-spawned sessions apply; a runtime below the minimum refuses; GSTACK_CAPY=1 needs a matching verdict', () => {
const sr = tmp();
const base = { skill: 'autoplan', gstackRoot: ROOT, bunVersion: '1.4.2' };
expect(checkInstallAdmission({ ...base, env: {} }).applies).toBe(false);
const unattended = { GSTACK_SESSION_KIND: 'unattended', GSTACK_STATE_ROOT: sr };
expect(checkInstallAdmission({ ...base, env: unattended })).toMatchObject({ applies: true, refuse: false });
const oldBun = checkInstallAdmission({ ...base, env: unattended, bunVersion: '1.3.14' });
expect(oldBun).toMatchObject({ refuse: true, status: 'runtime', code: 'RUNTIME_BELOW_MINIMUM' });
expect(oldBun.line).toMatch(/^INSTALL: fail runtime — bun 1\.3\.14 .*below the supported minimum 1\.4\.2; fix: .+ \(RUNTIME_BELOW_MINIMUM\)$/);
const capy = { ...unattended, GSTACK_CAPY: '1' };
expect(checkInstallAdmission({ ...base, env: capy })).toMatchObject({ refuse: true, status: 'verdict', code: 'INSTALL_UNVERIFIED' });
writeVerdict(sr);
expect(checkInstallAdmission({ ...base, env: capy })).toMatchObject({ refuse: false, status: 'ok' });
expect(checkInstallAdmission({ ...base, skill: 'qa', env: capy })).toMatchObject({ refuse: true, status: 'verdict' });
writeVerdict(sr, { pins_planning_only: true });
expect(checkInstallAdmission({ ...base, skill: 'ship', env: capy }).refuse).toBe(true);
writeVerdict(sr, { head: 'f'.repeat(40) });
expect(checkInstallAdmission({ ...base, env: capy }).detail).toContain('another HEAD');
const over = checkInstallAdmission({ ...base, env: { ...capy, GSTACK_ALLOW_UNSUPPORTED_RUNTIME: '1' } });
expect(over).toMatchObject({ refuse: false, overridden: true });
expect(checkInstallAdmission({ ...base, env: { GSTACK_SESSION_KIND: 'spawned', GSTACK_CAPY: '1', GSTACK_STATE_ROOT: sr } }).refuse).toBe(true);
expect(checkInstallAdmission({ ...base, env: { OPENCLAW_SESSION: '1', GSTACK_STATE_ROOT: sr }, bunVersion: '1.3.14' })).toMatchObject({ applies: true, refuse: false, warn: true });
});
});
// Plan A-c/A5: one record per (checkout, project) holding the Bun resolution;
// both twins pick the same file, bind workflows that run the repo's tests to
// the project, and refuse a running Bun the record did not certify.
describe('install verdict keyed by (checkout, project): both twins', () => {
const BUN = fs.realpathSync(process.execPath);
const project = (name: string) => { const d = path.join(fs.realpathSync(tmp()), name); fs.mkdirSync(d); execFileSync('git', ['init', '-q', d], { timeout: 30_000 }); return d; };
const bashAdmit = (skill: string, cwd: string, env: Record<string, string>) => {
const r = spawnSync('bash', ['-c', `. "$R/bin/gstack-bun-version.sh" && . "$R/bin/gstack-doctor-components.sh" && . "$R/bin/gstack-install-verdict.sh" && gstack_install_admission "$R/bin" "$1" unattended; printf '%s|%s|%s\n' "$_ia_refuse" "$_ia_status" "$_ia_line"`, '_', skill],
{ cwd, encoding: 'utf8', timeout: 30_000, env: { PATH: `${path.dirname(BUN)}:/usr/bin:/bin`, HOME: os.homedir(), R: ROOT, GSTACK_CAPY: '1', GSTACK_SESSION_KIND: 'unattended', ...env } });
const [refuse, status, line] = r.stdout.trim().split('|');
return { refuse: refuse === '1', status, line: line ?? '' };
};
const tsAdmit = (skill: string, cwd: string, env: Record<string, string>, bunPath = BUN) =>
checkInstallAdmission({ skill, gstackRoot: ROOT, cwd, bunVersion: '1.4.2', bunPath, env: { GSTACK_CAPY: '1', GSTACK_SESSION_KIND: 'unattended', ...env } });
const both = (skill: string, cwd: string, env: Record<string, string>) => {
const b = bashAdmit(skill, cwd, { _GH: env.GSTACK_STATE_ROOT, ...env });
const t = tsAdmit(skill, cwd, env);
expect({ refuse: b.refuse, status: b.status }).toEqual({ refuse: t.refuse, status: t.status });
return t;
};
test('A-B-A across two projects, a no-project directory, explicit GSTACK_INSTALL_VERDICT and the old checkout-only schema', () => {
const sr = tmp();
const env = { GSTACK_STATE_ROOT: sr };
const a = project('a'), b = project('b'), none = fs.realpathSync(tmp());
const fileA = writeVerdict(sr, { project: a, bun_path: BUN, bun_version: '1.4.2', schema_version: 2, for: 'review' });
writeVerdict(sr, { project: b, bun_path: BUN, bun_version: '1.4.2', schema_version: 2, for: 'review' });
for (const dir of [a, b, a]) expect(both('review', dir, env)).toMatchObject({ refuse: false, status: 'ok' });
expect(both('review', none, env)).toMatchObject({ refuse: true, status: 'verdict' });
writeVerdict(sr, { project: '', bun_path: BUN, bun_version: '1.4.2', schema_version: 2 });
expect(both('autoplan', none, env)).toMatchObject({ refuse: false });
fs.rmSync(installVerdictPath(REAL, sr, b));
const viaExplicit = both('review', b, { ...env, GSTACK_INSTALL_VERDICT: fileA });
expect(viaExplicit).toMatchObject({ refuse: true, status: 'verdict' });
expect(viaExplicit.detail).toBe(`the install verdict checked ${a}, not ${b}, and review runs the repo's tests`);
expect(viaExplicit.line).toContain(`from ${b}`);
expect(both('autoplan', b, { ...env, GSTACK_INSTALL_VERDICT: fileA })).toMatchObject({ refuse: false });
const legacySr = tmp();
writeVerdict(legacySr, { for: 'review' }, legacyInstallVerdictPath(REAL, legacySr));
expect(both('review', b, { GSTACK_STATE_ROOT: legacySr })).toMatchObject({ refuse: false, status: 'ok' });
});
test("GSTA-85: an installer run from the skill's own folder (no project) leaves /ship from the project with no verdict; both twins name the project directory to run the installer from", () => {
const sr = tmp();
const env = { GSTACK_STATE_ROOT: sr };
const proj = project('lane');
writeVerdict(sr, { project: '', bun_path: BUN, bun_version: '1.4.2', schema_version: 2, for: 'ship' });
const r = both('ship', proj, env);
expect(r).toMatchObject({ refuse: true, status: 'verdict', code: 'INSTALL_UNVERIFIED' });
expect(r.detail).toBe(`no install verdict at ${installVerdictPath(REAL, sr, proj)} for ${REAL}`);
const fix = `fix: run the Capy skill installer (scripts/install.sh --for ship) from ${proj}: the verdict is keyed by the project directory the installer runs in, so a run from the skill's own folder does not cover ${proj}; then launch with the GSTACK_LAUNCH line it prints (INSTALL_UNVERIFIED)`;
expect(r.line).toContain(fix);
expect(bashAdmit('ship', proj, { _GH: sr, ...env }).line).toContain(fix);
const file = installVerdictPath(REAL, sr, proj);
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, '{not json\n');
const corrupt = both('ship', proj, env);
expect(corrupt).toMatchObject({ refuse: true, status: 'verdict', code: 'INSTALL_UNVERIFIED' });
expect(corrupt.detail).toBe(`the install verdict at ${file} is unreadable or has an unsupported schema_version; rerun the installer`);
expect(bashAdmit('ship', proj, { _GH: sr, ...env }).line).toContain(`the install verdict at ${file} is unreadable or has an unsupported schema_version; rerun the installer`);
writeVerdict(sr, { project: proj, bun_path: BUN, bun_version: '1.4.2', schema_version: 3, for: 'ship' });
const future = both('ship', proj, env);
expect(future).toMatchObject({ refuse: true, status: 'verdict' });
expect(future.detail).toContain('unsupported schema_version');
});
test('a running Bun other than the recorded one refuses with BUN_RESOLUTION_DRIFT and names both', () => {
const sr = tmp();
const a = project('a');
writeVerdict(sr, { project: a, bun_path: '/opt/other/bun', bun_version: '1.4.3', schema_version: 2, for: 'review' });
const t = both('review', a, { GSTACK_STATE_ROOT: sr });
expect(t).toMatchObject({ refuse: true, status: 'runtime', code: 'BUN_RESOLUTION_DRIFT' });
expect(t.line).toBe(`INSTALL: fail runtime — bun 1.4.2 at ${BUN} is running, but the install verdict recorded bun 1.4.3 at /opt/other/bun; fix: launch with the GSTACK_LAUNCH line (PATH=/opt/other:$PATH), or re-run the Capy skill installer with --check (scripts/install.sh --check --for review) to see what changed (BUN_RESOLUTION_DRIFT)`);
expect(bashAdmit('review', a, { _GH: sr }).line).toContain('(BUN_RESOLUTION_DRIFT)');
});
});
describe('source root and support file (one helper, direct and symlinked layouts)', () => {
const bash = (bin: string, fn: string) => spawnSync('bash', ['-c', `. "${path.join(ROOT, 'bin/gstack-install-verdict.sh')}" && ${fn}`, '_', bin], { encoding: 'utf8', timeout: 30_000 });
test('a checkout bin resolves to the checkout; an installed root follows .source-path; both read capy/support.env', () => {
const runtime = tmp();
fs.symlinkSync(path.join(ROOT, 'bin'), path.join(runtime, 'bin'));
fs.writeFileSync(path.join(runtime, '.source-path'), `${ROOT}\n`);
const linked = path.join(tmp(), 'gstack');
fs.symlinkSync(ROOT, linked);
for (const bin of [path.join(ROOT, 'bin'), path.join(runtime, 'bin'), path.join(linked, 'bin')]) {
expect(bash(bin, 'gstack_source_root "$1"').stdout.trim(), bin).toBe(REAL);
expect(sourceRoot(path.dirname(bin))).toBe(REAL);
expect(bash(bin, 'gstack_support_env_get "$(gstack_source_root "$1")" GSTACK_MIN_REVISION').stdout.trim()).toMatch(/^\d+\.\d+\.\d+\.\d+$/);
}
});
});