Skip to content

Use Garry Tan's exact Claude Code setup: 23 opinionated tools that serve as CEO, Designer, Eng Manager, Release Manager, Doc Engineer, and QA

TypeScript
33.7 KB
Raw Blame
/**
 * GST-3260 PR 1: the /ship run record replaces the bare run marker. A run is open until a gstack command ends it (finish,
 * stop, gstack-skill-end, or gstack-ship-run abandon), never because a lease line aged out or another run's touch pruned
 * it (probe 002). A bare marker reads as legacy and an unreadable record fails closed, and both are ended with one recorded abandon. Also: a stop binds to the head its
 * stopping row read (#4591), and the round history counts a reused run id once per head (gstack-evals#406).
 */
import { afterEach, describe, expect, setDefaultTimeout, test } from 'bun:test';
import { spawn } from 'node:child_process';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { placeRunEntry, prReviewHistory, runStub, stopFrom } from '../lib/pr-review-history';
import { receiptRuns } from '../lib/ship-receipt';
import { lineOf, PREFLIGHT, shipFixture, type ShipFx } from './helpers/ship-bin-fixture';

setDefaultTimeout(120_000);
const BIN = path.dirname(PREFLIGHT);
const CHECKOUT = { GSTACK_LEASE_DIR: '', GSTACK_APPROVALS_MIRROR: '0' };
const CAPTAIN = 'Captain GStack Minder (GSTA-36)';
const fixtures: ShipFx[] = [];
const fx = (...a: Parameters<typeof shipFixture>) => { const f = shipFixture(...a); fixtures.push(f); return f; };
afterEach(() => { while (fixtures.length) fixtures.pop()!.cleanup(); });

const recordFile = (f: ShipFx) => path.resolve(f.work, f.git('rev-parse', '--git-path', 'gstack-ship-run').stdout.trim());
const record = (f: ShipFx) => { const t = fs.readFileSync(recordFile(f), 'utf8'); return t.trim().startsWith('{') ? JSON.parse(t) : { legacy: t.trim() }; };
/** Moves the record's clock back, as if the run started and last beat `minutes` ago. */
const age = (f: ShipFx, minutes: number) => {
  const t = fs.readFileSync(recordFile(f), 'utf8');
  if (!t.trim().startsWith('{')) return;
  const back = (iso: string) => new Date(Date.parse(iso) - minutes * 60_000).toISOString();
  const r = JSON.parse(t);
  fs.writeFileSync(recordFile(f), JSON.stringify({ ...r, started_at: back(r.started_at), beat_at: back(r.beat_at) }) + '\n');
};
const run = (f: ShipFx, bin: string, args: string[]) => f.run(path.join(BIN, bin), args, CHECKOUT);
const preflight = (f: ShipFx, id?: string) => f.run(PREFLIGHT, ['--base', 'main', ...(id ? ['--run', id] : [])], CHECKOUT);
const head = (f: ShipFx) => f.git('rev-parse', 'HEAD').stdout.trim();
/** An owner ruling with no --owner-ref, which an open /ship run refuses. */
const ruleBare = (f: ShipFx) => run(f, 'gstack-approvals', ['rule', '--finding', 'src/x.ts:1:correctness', '--head', head(f), '--reason', 'ships as a note', '--words', 'B, note it', '--by', CAPTAIN]);
/** A review row that stopped on one open blocker, logged through gstack-review-log on the committed tree. */
const logStoppingReview = (f: ShipFx) => {
  const start = run(f, 'gstack-review-log', ['--start', 'review']);
  expect(start.status, start.stderr).toBe(0);
  const logged = run(f, 'gstack-review-log', [JSON.stringify({ skill: 'review', timestamp: new Date().toISOString(), via: 'ship', pass: 1, review_scope: 'frozen', completed: true, converged: false, status: 'issues_found', issues_found: 1, critical: 1, informational: 0, findings: [{ fingerprint: 'src/x.ts:1:outside-voice', severity: 'CRITICAL', action: 'open', blocking: true, ledger_id: 'p1/outside-voice#1', raised_by: 'outside-voice' }] }), '--finish', start.stdout.trim()]);
  expect(logged.status, logged.stderr).toBe(0);
};

describe('a /ship run stays open until a gstack command ends it (GST-3260)', () => {
  // Value: protects=probe 002, a live run whose lease line another run's touch pruned after 120 minutes still blocks a full
  //   preflight; fails_when=preflight reads gstack-lease open and merges the base under a live run's readers; seam=the run record
  test('probe 002: a live run past 120 minutes, after another run\'s lease touch, still reads open', () => {
    const f = fx();
    expect(preflight(f, 'r1').status).toBe(0);
    const lease = path.join(f.work, '.git', 'gstack-leases', String(process.pid));
    fs.writeFileSync(lease, fs.readFileSync(lease, 'utf8').replace(/^(run r1 )(\d+)$/m, (_, p, t) => `${p}${Number(t) - 10800}`));
    const touch = f.run(path.join(BIN, 'gstack-lease'), ['touch', '--root', f.work, '--pid', String(process.pid), '--run', 'r2'], CHECKOUT);
    expect(touch.status, touch.stderr).toBe(0);
    expect(fs.readFileSync(lease, 'utf8')).not.toMatch(/^run r1 /m);
    age(f, 180);
    f.advanceBase({ 'docs/new.md': '# new\n' });
    const again = preflight(f, 'r3');
    expect(again.status).toBe(3);
    expect(again.stdout).toContain('STOP: /ship run r1 is open on this checkout and past its preflight (');
    expect(again.stdout).toContain('state open, beat_age 180m');
    expect(again.stdout).not.toContain('MERGE:');
    expect(run(f, 'gstack-ship-run', ['status']).stdout).toMatch(/^RUN_STATE: run=r1 state=open beat_age=180m /m);
  });

  // Value: protects=a bare marker an older gstack left (a run that died) refuses an unrelayed ruling with its state, and one
  //   recorded abandon ends it, with a mark in the PR body; fails_when=the marker never ends, or only rm clears it;
  //   seam=gstack-approvals rule, gstack-ship-run abandon, gstack-ship-receipt abandoned
  test('a stale bare marker reads legacy: an unrelayed ruling refuses, and abandon ends it at any age', () => {
    const f = fx();
    fs.writeFileSync(recordFile(f), 'ship-stale-1\n');
    const refused = ruleBare(f);
    expect(refused.status).toBe(2);
    expect(refused.stderr).toContain('/ship run ship-stale-1 is open on this checkout (state legacy, beat_age unknown;');
    expect(refused.stderr).toContain('gstack-ship-run abandon --run ship-stale-1 --by <lane or Captain> --reason "<one line>"');
    const body = path.join(f.root, 'body.md');
    fs.writeFileSync(body, 'Fixes a thing.\n');
    const end = run(f, 'gstack-ship-run', ['abandon', '--run', 'ship-stale-1', '--by', CAPTAIN, '--reason', 'the run died before finish', '--body', body]);
    expect(end.status, end.stderr).toBe(0);
    expect(end.stdout).toContain('RUN_ABANDONED: run=ship-stale-1 from=legacy by=Captain GStack Minder (GSTA-36)');
    expect(record(f)).toMatchObject({ v: 1, run: 'ship-stale-1', state: 'abandoned', ended_by: CAPTAIN, reason: 'the run died before finish' });
    expect(receiptRuns(fs.readFileSync(body, 'utf8'))).toEqual([expect.objectContaining({ mark: 'abandoned', run: 'ship-stale-1', by: CAPTAIN, reason: 'the run died before finish' })]);
    expect(prReviewHistory(fs.readFileSync(body, 'utf8')).rounds).toBe(0);
    const ok = ruleBare(f);
    expect(ok.status, ok.stderr).toBe(0);
    const again = run(f, 'gstack-ship-run', ['abandon', '--run', 'ship-stale-1', '--by', CAPTAIN, '--reason', 'twice']);
    expect(again.status).toBe(3);
    expect(again.stderr).toContain('(SHIP_RUN_ABANDON_REFUSED;');
  });



  // Value: protects=a stopped record of another run is replaced by a new --run, while a preflight with no --run stops on it;
  //   fails_when=a bare preflight merges the base under the stopped run's stop head; seam=lib/ship-preflight.ts shipRunClear
  test('a stopped record of another run: a new --run replaces it, a preflight with no --run stops', () => {
    const f = fx();
    expect(preflight(f, 'ro').status).toBe(0);
    const s = head(f);
    fs.writeFileSync(recordFile(f), JSON.stringify({ ...record(f), state: 'stopped', stop: { head: s, tree: f.git('rev-parse', 'HEAD^{tree}').stdout.trim() } }) + '\n');
    f.advanceBase({ 'docs/new.md': '# new\n' });
    const bare = preflight(f);
    expect(bare.status).toBe(3);
    expect(bare.stdout).toContain('STOP: /ship run ro is open on this checkout and past its preflight (');
    expect(bare.stdout).not.toContain('MERGE:');
    const next = preflight(f, 'r2');
    expect(next.status, next.stdout).toBe(0);
    expect(lineOf(next.stdout, 'MERGE')).toMatch(/^MERGE: merged origin\/main at /);
    expect(record(f)).toMatchObject({ run: 'r2', state: 'open' });
  });

  // Value: protects=stop upgrades a bare marker that names its run; fails_when=a legacy marker stays bare after stop;
  //   seam=gstack-ship-receipt stop
  test('stop upgrades a legacy marker naming its run', () => {
    const f = fx({ 'src/x.ts': 'export const x = 1;\n' }, { 'src/x.ts': 'export const x = 2;\n' });
    const s = head(f);
    fs.writeFileSync(recordFile(f), 'ro\n');
    logStoppingReview(f);
    const body = path.join(f.root, 'body.md');
    fs.writeFileSync(body, 'Fixes a thing.\n');
    expect(run(f, 'gstack-ship-receipt', ['stop', '--body', body, '--pr', '7', '--run', 'ro', '--since', '2000-01-01T00:00:00Z']).status).toBe(0);
    expect(record(f)).toMatchObject({ v: 1, run: 'ro', state: 'stopped', stop: { head: s } });
  });

  // Value: protects=an unreadable record fails closed (preflight and unrelayed rulings refuse) and one abandon naming its digest ends it;
  //   fails_when=a corrupt record reads as no run; seam=lib/ship-run-record.ts readRunRecord, gstack-ship-run
  test('an unreadable record stops preflight and rulings; abandon --run unknown takes only its digest', () => {
    const f = fx();
    fs.writeFileSync(recordFile(f), '{"v":1,"run":\n');
    f.advanceBase({ 'docs/new.md': '# new\n' });
    const pre = preflight(f, 'r2');
    expect(pre.status).toBe(3);
    expect(pre.stdout).toContain('STOP: /ship run unknown is open on this checkout and past its preflight (');
    expect(pre.stdout).toContain('state unreadable');
    expect(ruleBare(f).stderr).toContain('state unreadable');
    const status = run(f, 'gstack-ship-run', ['status']);
    expect(status.status).toBe(1);
    expect(status.stderr).toContain('(SHIP_RUN_RECORD_UNREADABLE;');
    const sha = /sha256=([0-9a-f]{64})/.exec(status.stdout)?.[1];
    expect(sha).toBeDefined();
    const end = (digest: string) => run(f, 'gstack-ship-run', ['abandon', '--run', 'unknown', '--sha256', digest, '--by', CAPTAIN, '--reason', 'corrupt record']);
    expect(end('0'.repeat(64)).status).toBe(3);
    expect(end(sha!).status).toBe(0);
    const next = preflight(f, 'r2');
    expect(next.status, next.stdout).toBe(0);
    expect(lineOf(next.stdout, 'MERGE')).toMatch(/^MERGE: merged origin\/main at /);
    expect(record(f)).toMatchObject({ run: 'r2', state: 'open' });
  });

  // Value: protects=abandon refuses a run that beat in the last 60 minutes and gstack's own names, and a review-log beat counts;
  //   fails_when=a working run can be abandoned by mistake; seam=gstack-ship-run abandon, gstack-review-log --start
  test('abandon refuses under 60 minutes and as gstack; a review start beats the run', () => {
    const f = fx();
    expect(preflight(f, 'r1').status).toBe(0);
    const end = (by = CAPTAIN) => run(f, 'gstack-ship-run', ['abandon', '--run', 'r1', '--by', by, '--reason', 'stopped answering']);
    const young = end();
    expect(young.status).toBe(3);
    expect(young.stderr).toContain('(SHIP_RUN_ABANDON_REFUSED;');
    expect(young.stderr).toMatch(/beat_age 0m; the earliest abandon is \d{4}-\d\d-\d\dT/);
    age(f, 61);
    expect(end('gstack').status).toBe(2);
    expect(run(f, 'gstack-ship-run', ['abandon', '--run', 'r9', '--by', CAPTAIN, '--reason', 'x']).status).toBe(3);
    const start = run(f, 'gstack-review-log', ['--start', 'review']);
    expect(start.status, start.stderr).toBe(0);
    expect(Date.now() - Date.parse(record(f).beat_at)).toBeLessThan(60_000);
    expect(end().status).toBe(3);
    age(f, 61);
    const ok = end();
    expect(ok.status, ok.stderr).toBe(0);
    expect(ok.stdout).toContain('RUN_ABANDON_PR: skipped (no open PR');
  });

  // Value: protects=amendment C, gstack-skill-end ends its session's run on any outcome so the next preflight replaces it, and
  //   never touches another run; fails_when=a crashed or aborted /ship leaves an open run; seam=bin/gstack-skill-end
  for (const outcome of ['error', 'abort']) {
    test(`gstack-skill-end --outcome ${outcome} finishes its own run only`, () => {
      const f = fx();
      const skillEnd = (id: string) => f.run(path.join(BIN, 'gstack-skill-end'), ['--skill', 'ship', '--outcome', outcome, '--session-id', id, '--tel-start', '0'], { GSTACK_LEASE_DIR: path.join(f.root, 'install-leases') });
      expect(preflight(f, 's1').status).toBe(0);
      expect(skillEnd('other-session').status).toBe(0);
      expect(record(f)).toMatchObject({ run: 's1', state: 'open' });
      expect(skillEnd('s1').status).toBe(0);
      expect(record(f)).toMatchObject({ run: 's1', state: 'finished', ended_by: 'skill-end' });
      f.advanceBase({ 'docs/new.md': '# new\n' });
      const reused = preflight(f, 's1');
      expect(reused.status).toBe(3);
      expect(reused.stdout).toContain('STOP: /ship run s1 already ended on this checkout (state finished, ended_by skill-end)');
      const next = preflight(f, 's2');
      expect(next.status, next.stdout).toBe(0);
      expect(record(f)).toMatchObject({ run: 's2', state: 'open' });
    });
  }

  // Value: protects=two preflights racing to open a run: the compare-and-swap under the gate lock lets exactly one write;
  //   fails_when=both record (the second overwrites the first); seam=lib/ship-run-record.ts changeRunRecord under withGateLock
  test('two racing preflights: exactly one opens the run', async () => {
    const f = fx();
    const lib = path.resolve(import.meta.dir, '..', 'lib', 'ship-run-record.ts');
    const open = (id: string) => new Promise<string>(resolve => {
      const child = spawn(process.execPath, ['--no-env-file', '--config=/dev/null', '-e', `const { changeRunRecord } = await import(${JSON.stringify(lib)}); process.stdout.write(JSON.stringify(changeRunRecord(process.cwd(), 'preflight', ${JSON.stringify(id)})));`], { cwd: f.work, stdio: ['ignore', 'pipe', 'pipe'] });
      let out = '';
      child.stdout.on('data', d => { out += d; });
      child.on('close', () => resolve(out));
    });
    const results = (await Promise.all(Array.from({ length: 6 }, (_, i) => open(`race-${i}`)))).map(o => JSON.parse(o || '{"ok":false,"why":"no output"}'));
    expect(results.filter(r => r.ok)).toHaveLength(1);
    const winner = results.find(r => r.ok).record.run;
    expect(record(f)).toMatchObject({ run: winner, state: 'open' });
    for (const r of results.filter(x => !x.ok)) expect(r.why).toContain(`of run ${winner}`);
  });

  // Value: protects=rule --owner-ref stays allowed under open, stopped and legacy records while an unrelayed ruling refuses with
  //   the record's state; fails_when=a stopped record blocks the owner's relayed ruling; seam=gstack-approvals rule
  test('rule --owner-ref records under stopped, open and legacy records; without it the refusal names the state', () => {
    const f = fx();
    const s = head(f);
    const rule = (finding: string, extra: string[] = []) => run(f, 'gstack-approvals', ['rule', '--finding', finding, '--head', s, '--reason', 'ships as a note', '--words', 'B, note it', '--by', 'GBrain Minder (GBRA-108)', ...extra]);
    const now = new Date().toISOString();
    const states: Record<string, () => void> = {
      stopped: () => fs.writeFileSync(recordFile(f), JSON.stringify({ v: 1, run: 'ro', state: 'stopped', head: s, started_at: now, beat_at: now, stop: { head: s, tree: f.git('rev-parse', 'HEAD^{tree}').stdout.trim() } }) + '\n'),
      open: () => fs.writeFileSync(recordFile(f), JSON.stringify({ v: 1, run: 'ro', state: 'open', head: s, started_at: now, beat_at: now }) + '\n'),
      legacy: () => fs.writeFileSync(recordFile(f), 'ro\n'),
    };
    for (const [state, make] of Object.entries(states)) {
      make();
      const bare = rule(`src/${state}.ts:1:correctness`);
      expect([state, bare.status]).toEqual([state, 2]);
      expect(bare.stderr).toContain(`/ship run ro is open on this checkout (state ${state}, `);
      expect(bare.stderr).toContain('(APPROVAL_RULING_RUN_OPEN');
      const relayed = rule(`src/${state}.ts:1:correctness`, ['--owner-ref', 'GBRA-108:2026-11-21T12:00:00Z']);
      expect([state, relayed.status, relayed.stderr]).toEqual([state, 0, relayed.stderr]);
    }
    expect(run(f, 'gstack-approvals', ['lane-exception', '--head', s, '--files', 'test/a.test.ts', '--reason', 'x', '--words', 'w', '--by', 'Owner']).stderr).toContain('state legacy');
  });

  // Value: protects=preflight refuses a run id the PR body already shows receipted (gstack-evals#406 reused ship-406 across three
  //   runs); fails_when=history prints nothing for a reused id; seam=gstack-ship-receipt history --run
  test('history names a --run the PR body already receipted', () => {
    const f = fx();
    const body = path.join(f.root, 'body.md');
    const receipted = placeRunEntry('Fixes a thing.\n', { head: 'a'.repeat(40), run: 'ship-406', review_loop: { passes: 1, blocking_found: 0, deferred: 0 } });
    fs.writeFileSync(body, receipted);
    const used = run(f, 'gstack-ship-receipt', ['history', '--body', body, '--pr', '7', '--base', 'main', '--run', 'ship-406']);
    expect(used.stdout).toContain(`PR_REVIEW_RUN_USED: run=ship-406 receipted at ${'a'.repeat(12)}`);
    expect(run(f, 'gstack-ship-receipt', ['history', '--body', body, '--pr', '7', '--base', 'main', '--run', 'ship-407']).stdout).not.toContain('PR_REVIEW_RUN_USED');
  });
});

describe('finish removes only the run record the PR body names (amendment F)', () => {
  // Value: protects=finish never removes another run's record; fails_when=a stopped run's late finish deletes the next run's open
  //   record; seam=lib/ship-run-record.ts finishRun
  test('a record whose run the PR body does not name stays; a named one, or one with no PR, is removed', async () => {
    const { finishRun } = await import('../lib/ship-run-record');
    const f = fx();
    expect(preflight(f, 'run-b').status).toBe(0);
    expect(finishRun(f.work, ['run-a'])).toMatchObject({ removed: false });
    expect(record(f)).toMatchObject({ run: 'run-b', state: 'open' });
    expect(finishRun(f.work, ['run-a', 'run-b'])).toMatchObject({ removed: true });
    expect(fs.existsSync(recordFile(f))).toBe(false);
    expect(preflight(f, 'run-c').status).toBe(0);
    expect(finishRun(f.work, null)).toMatchObject({ removed: true });
    expect(fs.existsSync(recordFile(f))).toBe(false);
  });

  // Value: protects=two untested transitions: finish keeps an unreadable record, and abandon ends a stopped run past 60 minutes;
  //   fails_when=finish deletes a record it cannot read, or a stopped run can never be abandoned; seam=finishRun, gstack-ship-run abandon
  test('finish keeps an unreadable record; abandon ends a stopped run past 60 minutes', async () => {
    const { finishRun } = await import('../lib/ship-run-record');
    const f = fx();
    fs.writeFileSync(recordFile(f), '{"v":1,"run":\n');
    expect(finishRun(f.work, null)).toMatchObject({ removed: false, line: expect.stringContaining('RUN_RECORD: kept (') });
    expect(fs.readFileSync(recordFile(f), 'utf8')).toBe('{"v":1,"run":\n');
    fs.rmSync(recordFile(f));
    expect(preflight(f, 'ro').status).toBe(0);
    fs.writeFileSync(recordFile(f), JSON.stringify({ ...record(f), state: 'stopped' }) + '\n');
    age(f, 61);
    const body = path.join(f.root, 'body.md');
    fs.writeFileSync(body, 'Fixes a thing.\n');
    const end = run(f, 'gstack-ship-run', ['abandon', '--run', 'ro', '--by', CAPTAIN, '--reason', 'stopped and never resumed', '--body', body]);
    expect(end.status, end.stderr).toBe(0);
    expect(record(f)).toMatchObject({ run: 'ro', state: 'abandoned', ended_by: CAPTAIN });
  });
});

describe('the stop binds to the head its stopping row read (#4591)', () => {
  const S = 'a'.repeat(40), E = 'e'.repeat(40), TS = 'b'.repeat(40), TE = 'f'.repeat(40);
  const trees: Record<string, string> = { [S]: TS, [E]: TE };
  const bound = (skill: string, commit: string, at: string, extra: Record<string, unknown> = {}) => ({ skill, via: 'ship', timestamp: at, completed: true, converged: false, commit_full: commit, tree: trees[commit], wtree: trees[commit], review_binding: { state: 'incomplete', start_wtree: trees[commit], end_wtree: trees[commit] }, ...extra });
  const from = (rows: Record<string, unknown>[]) => stopFrom(rows, '2026-11-21T00:00:00Z', { treeOf: c => trees[c], onHead: c => c === S || c === E });
  test('p1 -> fix-check -> Step 11 voices on a committed tree: the stop is the voices\' head, not the first pass\'s', () => {
    const p1 = bound('review', S, '2026-11-21T07:00:00Z', { review_binding: { state: 'changed', start_wtree: TS, end_wtree: TE } });
    const fixCheck = bound('review-fix-check', E, '2026-11-21T07:10:00Z');
    const voices = bound('adversarial-review', E, '2026-11-21T07:20:00Z');
    expect(from([p1, fixCheck, voices])).toEqual({ ok: true, head: E, tree: TE });
    expect(from([p1, fixCheck])).toEqual({ ok: true, head: E, tree: TE });
    expect(from([bound('review', S, '2026-11-21T07:00:00Z'), voices])).toEqual({ ok: true, head: E, tree: TE });
    expect(from([p1, { ...voices, wtree: TS }]).ok).toBe(false);
  });
});

describe('the round history counts a reused run id once per head (gstack-evals#406)', () => {
  test('three entries sharing one run id at three heads read rounds=3; the same run at one head counts once', () => {
    const loop = { passes: 1, blocking_found: 0, deferred: 0, rounds: 1 };
    const receipt = (h: string) => ({ head: h, run: 'ship-406', review_loop: loop });
    const body = [receipt('d'.repeat(40)), receipt('b'.repeat(40)), receipt('2'.repeat(40))].reduce((b, e) => placeRunEntry(b, e), 'Fixes a thing.\n');
    expect(prReviewHistory(body)).toMatchObject({ rounds: 3, runs: 3, heads: 3 });
    const sameHead = [runStub({ head: 'd'.repeat(40), run: 'ship-406', status: 'started', loop }), receipt('d'.repeat(40))].reduce((b, e) => placeRunEntry(b, e), 'Fixes a thing.\n');
    expect(prReviewHistory(sameHead)).toMatchObject({ rounds: 1, heads: 1 });
  });
});

describe('a stop before any PR fails when nothing records it (#4826 review)', () => {
  // Value: protects=a pre-PR stop exits 0 and prints STOP_RECORD only when the run record took it, and a valueless --pr
  //   is a usage error rather than a silent pre-PR stop; fails_when=a refused run-record write or a bare --pr reads as
  //   recorded; seam=gstack-ship-receipt stop
  test('the own run records it; another run\'s record and a bare --pr refuse with no STOP_RECORD', () => {
    const f = fx({ 'src/x.ts': 'export const x = 1;\n' }, { 'src/x.ts': 'export const x = 2;\n' });
    expect(preflight(f, 'rx').status).toBe(0);
    logStoppingReview(f);
    const since = ['--since', '2000-01-01T00:00:00Z'];
    const other = run(f, 'gstack-ship-receipt', ['stop', '--run', 'ro', ...since]);
    expect(other.status, other.stdout).toBe(1);
    expect(other.stdout).not.toContain('STOP_RECORD:');
    expect(other.stderr).toContain("the record is run rx's, not ro's");
    expect(record(f)).toMatchObject({ run: 'rx', state: 'open' });
    for (const args of [['--pr'], ['--pr', '--run', 'rx'], ['--body']]) {
      const bare = run(f, 'gstack-ship-receipt', ['stop', ...since, '--run', 'rx', ...args]);
      expect(bare.status, args.join(' ')).toBe(2);
      expect(bare.stdout).not.toContain('STOP_RECORD:');
    }
    expect(record(f)).toMatchObject({ run: 'rx', state: 'open' });
    const own = run(f, 'gstack-ship-receipt', ['stop', '--run', 'rx', ...since]);
    expect(own.status, own.stderr).toBe(0);
    expect(own.stdout).toContain('STOP_RECORD: run=rx head=' + head(f));
    expect(own.stdout).toContain(' pr=none');
    expect(record(f)).toMatchObject({ run: 'rx', state: 'stopped', stop: { head: head(f) } });
  });
});

describe('a finished run no longer blocks the next /ship, and abandon never hints a mark that refuses (GST-4109)', () => {
  const BUDGET = (f: ShipFx, args: string[]) => run(f, 'gstack-ship-budget', args);
  const startBudget = (f: ShipFx, name: string) => {
    const rec = path.join(f.root, `${name}.md`);
    fs.writeFileSync(rec, '# /ship record\n\n## Next steps\n- Step 1\n');
    const s = BUDGET(f, ['start', '--record', rec, '--mode', 'observe', '--reading', '1000', '--credits', '1']);
    expect(s.status, s.stdout + s.stderr).toBe(0);
    return rec;
  };

  // Value: protects=a /ship that finished (its ledger closed at Step 22) ends its own open run record, and a record an older gstack left open
  //   behind a closed ledger is ended by the next preflight, while a run whose ledger is still open keeps blocking; fails_when=a finished
  //   run blocks the next /ship until someone abandons it by hand (GSKI-1 #7), or a live run's record is ended;
  //   why_new=no test ran the budget close against the run record; seam=gstack-ship-budget close, lib/ship-preflight.ts shipRunClear;
  //   no_claim=a run that never reaches close, which stop, skill-end or abandon end as before
  test('close ends its own open record; preflight ends one an older close left; an unclosed run still blocks', () => {
    const f = fx();
    const rec = startBudget(f, 'r1');
    expect(preflight(f, 'r1').status).toBe(0);
    expect(BUDGET(f, ['note', '--record', rec, '--step', '9', '--reading', '1500', '--credits', '1']).status).toBe(0);
    const closed = BUDGET(f, ['close', '--record', rec, '--reading', '2000', '--credits', '2']);
    expect(closed.status, closed.stdout + closed.stderr).toBe(0);
    expect(closed.stdout).toMatch(/^RUN_ENDED: run=r1 state=finished ended_by=gstack-ship-budget close \(its budget ledger ship-\S+ closed at /m);
    expect(record(f)).toMatchObject({ run: 'r1', state: 'finished', ended_by: 'gstack-ship-budget close' });
    fs.writeFileSync(recordFile(f), JSON.stringify({ ...record(f), state: 'open', ended_by: undefined }) + '\n');
    f.advanceBase({ 'docs/new.md': '# new\n' });
    startBudget(f, 'r2');
    const next = preflight(f, 'r2');
    expect(next.status, next.stdout).toBe(0);
    expect(next.stdout).toMatch(/^RUN_ENDED: run=r1 state=finished /m);
    expect(record(f)).toMatchObject({ run: 'r2', state: 'open' });
    f.advanceBase({ 'docs/other.md': '# other\n' });
    startBudget(f, 'r3');
    const live = preflight(f, 'r3');
    expect(live.status).toBe(3);
    expect(live.stdout).toContain('STOP: /ship run r2 is open on this checkout and past its preflight (');
    expect(record(f)).toMatchObject({ run: 'r2', state: 'open' });
  });

  // Value: protects=abandon of an unreadable record with an open PR ends with no PR mark and no hint, and a mark the PR refuses prints why
  //   with no rerun hint; fails_when=abandon --run unknown, or a run the PR body never recorded, prints a gstack-ship-receipt abandoned
  //   command that refuses the same way (GSTA-891); why_new=only the no-PR case was driven; seam=bin/gstack-ship-run, a gh stub;
  //   no_claim=gh failures, which keep the add-the-entry hint since a rerun can pass once gh works
  test('abandon --run unknown with an open PR skips the mark; a refused mark prints no looping hint', () => {
    const f = fx();
    fs.writeFileSync(recordFile(f), '{"v":1,"run":\n');
    const sha = /sha256=([0-9a-f]{64})/.exec(run(f, 'gstack-ship-run', ['status']).stdout)![1]!;
    const unknown = run(f, 'gstack-ship-run', ['abandon', '--run', 'unknown', '--sha256', sha, '--pr', '7', '--by', CAPTAIN, '--reason', 'corrupt record']);
    expect(unknown.status, unknown.stderr).toBe(0);
    expect(unknown.stdout).toContain("RUN_ABANDON_PR: skipped (an unreadable record names no run, so PR #7's body has no entry to mark)");
    expect(unknown.stdout).not.toContain('gstack-ship-receipt abandoned --pr');
    const g = fx();
    expect(preflight(g, 'r1').status).toBe(0);
    age(g, 61);
    const view = JSON.stringify({ body: 'A change.', headRefName: 'feat', headRepository: { name: 'fx' }, headRepositoryOwner: { login: 'fx' }, state: 'OPEN' });
    g.gh(`if [ "$1" = pr ] && [ "$2" = view ]; then printf '%s\\n' '${view}'; exit 0; fi; exit 0`);
    const named = run(g, 'gstack-ship-run', ['abandon', '--run', 'r1', '--pr', '7', '--by', CAPTAIN, '--reason', 'stopped answering']);
    expect(named.status, named.stderr).toBe(0);
    expect(named.stdout).toContain("RUN_ABANDON_PR: not recorded (gstack-ship-receipt abandoned: PR #7's body records no entry for run r1");
    expect(named.stdout).toContain('no mark is needed');
    expect(named.stdout).not.toContain('add the entry with');
    expect(record(g)).toMatchObject({ run: 'r1', state: 'abandoned' });
  });
  // Value: protects=preflight never ends a live run that wrote no ledger of its own after an earlier run's ledger closed; fails_when=endClosedRun takes the earlier closed ledger as the later record's owner and marks a live run finished (state-machines, ship-1-p1);
  //   why_new=the case above always started a ledger per run; seam=an unmetered start; no_claim=two ledgers created in the same millisecond
  test('a later run with no ledger is never ended by an earlier closed ledger', () => {
    const f = fx();
    const rec = startBudget(f, 'r1');
    expect(preflight(f, 'r1').status).toBe(0);
    expect(BUDGET(f, ['close', '--record', rec, '--reading', '2000', '--credits', '2']).status).toBe(0);
    f.advanceBase({ 'docs/a.md': 'a\n' });
    const bare = path.join(f.root, 'r2.md');
    fs.writeFileSync(bare, '# /ship record\n');
    expect(BUDGET(f, ['start', '--record', bare]).stdout).toContain('BUDGET: unmetered');
    expect(preflight(f, 'r2').status).toBe(0);
    f.advanceBase({ 'docs/b.md': 'b\n' });
    const r3 = preflight(f, 'r3');
    expect(r3.status).toBe(3);
    expect(r3.stdout).not.toContain('RUN_ENDED:');
    expect(record(f)).toMatchObject({ run: 'r2', state: 'open' });
  });

  // Value: protects=a late close of an earlier run's ledger never ends a later run that opened its record without a ledger; fails_when=endClosedRun takes the earlier ledger, which closed after the later record opened, as that record's owner and marks the live run finished (outside-voice, ship-1-p2);
  //   why_new=the case above closed before the later run opened; seam=an unmetered start; no_claim=a ledger that keeps noting for another run
  test("an earlier run's late close never ends a later unmetered run", () => {
    const f = fx();
    const rec = startBudget(f, 'r1');
    expect(preflight(f, 'r1').status).toBe(0);
    const ledger = /ledger=(\S+)/.exec(fs.readFileSync(rec, 'utf8'))![1]!;
    const l = JSON.parse(fs.readFileSync(ledger, 'utf8'));
    const back = (iso: string) => new Date(Date.parse(iso) - 30 * 60_000).toISOString();
    fs.writeFileSync(ledger, JSON.stringify({ ...l, created_at: back(l.created_at), reservations: l.reservations.map((x: { at: string }) => ({ ...x, at: back(x.at) })) }, null, 2));
    fs.writeFileSync(recordFile(f), JSON.stringify({ ...record(f), state: 'abandoned' }) + '\n');
    f.advanceBase({ 'docs/a.md': 'a\n' });
    expect(preflight(f, 'r2').status).toBe(0);
    const late = BUDGET(f, ['close', '--record', rec, '--reading', '2000', '--credits', '2']);
    expect(late.status, late.stdout + late.stderr).toBe(0);
    expect(late.stdout).not.toContain('RUN_ENDED:');
    expect(record(f)).toMatchObject({ run: 'r2', state: 'open' });
  });

  // Value: protects=a start after the record's /ship passed preflight and ended is a new /ship, even within 5 minutes and with no note, while a rerun inside the open run re-attaches; fails_when=a quick second /ship re-attaches the ended run's ledger and its baseline (outside-voice, ship-1-p2), or a rerun of start mid-run splits the run;
  //   why_new=the cases in ship-budget.test.ts never ran a preflight; seam=the run record's state; no_claim=a preflight with no budget start
  test('start re-attaches inside an open run and supersedes once that run ended', () => {
    const f = fx();
    const rec = startBudget(f, 'r1');
    expect(preflight(f, 'r1').status).toBe(0);
    const again = BUDGET(f, ['start', '--record', rec, '--mode', 'observe', '--reading', '1100', '--credits', '1']);
    expect(again.status, again.stdout + again.stderr).toBe(0);
    expect(again.stdout).not.toContain('SUPERSEDED:');
    fs.writeFileSync(recordFile(f), JSON.stringify({ ...record(f), state: 'finished' }) + '\n');
    const next = BUDGET(f, ['start', '--record', rec, '--mode', 'observe', '--reading', '1200', '--credits', '1']);
    expect(next.status, next.stdout + next.stderr).toBe(0);
    expect(next.stdout).toMatch(/^SUPERSEDED: run \S+ on this record belongs to \/ship run r1, which passed preflight and is finished and stays as it was; /m);
  });

  // Value: protects=preflight binds the ledger its /ship started to that run id, and close ends only the record of the run its ledger is bound to,
  //   so an earlier run's late close never ends a later unmetered run, even one that opened within 5 minutes; fails_when=endClosedRun takes the
  //   earlier ledger as the later record's owner by timing (it started within 5 minutes, or noted after the record opened) and marks the live run
  //   finished (GSTA-892 ruled note at lib/ship-run-live.ts:56); why_new=the late-close case above backdated the earlier ledger past the window;
  //   seam=an unmetered start; no_claim=a ledger bound by an older gstack, which never ends a record (skill-end, stop or abandon do)
  test("preflight binds its run's ledger, and an earlier run's late close never ends a later run opened right after it", () => {
    const f = fx();
    const rec = startBudget(f, 'r1');
    expect(preflight(f, 'r1').status).toBe(0);
    const ledger = /ledger=(\S+)/.exec(fs.readFileSync(rec, 'utf8'))![1]!;
    expect(JSON.parse(fs.readFileSync(ledger, 'utf8')).ship_run).toBe('r1');
    fs.writeFileSync(recordFile(f), JSON.stringify({ ...record(f), state: 'abandoned' }) + '\n');
    f.advanceBase({ 'docs/a.md': 'a\n' });
    const bare = path.join(f.root, 'r2.md');
    fs.writeFileSync(bare, '# /ship record\n');
    expect(BUDGET(f, ['start', '--record', bare]).stdout).toContain('BUDGET: unmetered');
    expect(preflight(f, 'r2').status).toBe(0);
    expect(JSON.parse(fs.readFileSync(ledger, 'utf8')).ship_run).toBe('r1');
    expect(BUDGET(f, ['note', '--record', rec, '--step', '9', '--reading', '1500', '--credits', '1']).status).toBe(0);
    const late = BUDGET(f, ['close', '--record', rec, '--reading', '2000', '--credits', '2']);
    expect(late.status, late.stdout + late.stderr).toBe(0);
    expect(late.stdout).not.toContain('RUN_ENDED:');
    expect(record(f)).toMatchObject({ run: 'r2', state: 'open' });
  });
});