mirror of
https://github.com/garrytan/gstack.git
synced 2026-12-03 13:24:19 +01:00
/gstack-ship keeps a run's token cap when its budget start reruns mid-run, and four more budget-run fixes (GST-4132) (#4882)
A /gstack-ship run that reruns its budget start while it is still open keeps its own token cap and clock instead of getting a fresh (or no) cap. Why: the budget cap is the money guard on /gstack-ship, and #4877 left five ways around or past it, listed in its ruled notes. Nothing to do: the fixes take effect on the next gstack upgrade. Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/specialist-batched#1 reader=state-machines Review-Finding: p1/specialist-batched#2 reader=testing Review-Finding: p1/red-team#1 reader=red-team Review-Finding: v2/targeted#1 reader=targeted Review-Finding: v2/outside-voice#1 reader=outside-voice Review-Finding: v2/outside-voice#2 reader=outside-voice Review-Finding: v2/outside-voice#3 reader=outside-voice Review-Finding: v2/outside-voice#4 reader=outside-voice Review-Finding: v2/outside-voice#5 reader=outside-voice
This commit is contained in:
1 parent
be9b34e3dc
commit
37cad2b526
11 files changed
+621
-99
No files matched your search
+345
-2
@@ -5,9 +5,11 @@
|
||||
import { afterEach, describe, expect, test } from 'bun:test';
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import * as os from 'node:os';
|
||||
import { spawn, spawnSync } from 'node:child_process';
|
||||
import { bunIsolationArgs } from '../lib/bun-isolation';
|
||||
import { admits, loadCalibration, METER_CONTRACTS, newEpoch, parseNext, parseTokens, segmentEstimate, stepProjection, tally, unitConstants, type Ledger } from '../lib/ship-budget';
|
||||
import { BUDGET_BIN, budgetHarness, type Harness } from './helpers/ship-budget-harness';
|
||||
import { BUDGET_BIN, budgetHarness, type Harness, type Run } from './helpers/ship-budget-harness';
|
||||
|
||||
const cal = loadCalibration();
|
||||
const K = unitConstants(cal, 'capy-tokens');
|
||||
@@ -600,3 +602,344 @@ describe('a new /ship on a reused record meters only itself (GST-4109)', () => {
|
||||
expect(note.out).toMatch(/^NOTE: 6 used=2000000\b/m);
|
||||
});
|
||||
});
|
||||
|
||||
describe('start, resume and close follow one state table of run record, ledger and flags (GST-4132)', () => {
|
||||
type Rec = 'open' | 'ended' | 'missing';
|
||||
type Led = 'fresh' | 'fresh-over' | 'under' | 'over' | 'stopped' | 'closed';
|
||||
/** A first start under a 10M cap at 1M, its ledger moved to `led` and the checkout's run record to `rec`, then a second start (.context/GST-4132-state-table.md). */
|
||||
async function cell(rec: Rec, led: Led, extra: string[] = []) {
|
||||
const { changeRunRecord } = await import('../lib/ship-run-record');
|
||||
const h = fresh();
|
||||
const record = h.record();
|
||||
const first = h.sb(['start', '--record', record, '--mode', 'observe', '--token-cap', '10M', '--reading', '1000000']);
|
||||
const a = h.field(first.out, 'BUDGET_RUN', 'id'), ledger = h.field(first.out, 'BUDGET_RUN', 'ledger');
|
||||
if (rec !== 'missing') expect(changeRunRecord(h.repo, 'preflight', 'r1').ok).toBe(true);
|
||||
if (led === 'under' || led === 'over') expect(h.sb(['note', '--record', record, '--step', '9', '--reading', '5000000']).code).toBe(0);
|
||||
if (led === 'stopped') expect(h.sb(['note', '--record', record, '--step', '9', '--reading', '20000000']).code).toBe(3);
|
||||
if (rec === 'ended') expect(changeRunRecord(h.repo, 'skill-end', 'r1', { ended_by: 'skill-end' }).ok).toBe(true);
|
||||
if (led === 'closed') expect(h.sb(['close', '--record', record, '--reading', '5000000']).code).toBe(0);
|
||||
const reading = led === 'over' || led === 'fresh-over' ? '20000000' : '6000000';
|
||||
const second = h.sb(['start', '--record', record, '--mode', 'observe', '--token-cap', '10M', '--reading', reading, ...extra]);
|
||||
return { h, record, a, ledger, second, b: second.code === 0 || second.code === 3 ? h.field(second.out, 'BUDGET_RUN', 'id') : '' };
|
||||
}
|
||||
const TABLE: Array<[Rec, Led, number, 'same' | 'new' | 'superseded']> = [
|
||||
['open', 'fresh', 0, 'same'], ['open', 'fresh-over', 3, 'same'], ['open', 'under', 0, 'same'], ['open', 'over', 3, 'same'], ['open', 'stopped', 3, 'same'], ['open', 'closed', 0, 'new'],
|
||||
['ended', 'fresh', 0, 'superseded'], ['ended', 'fresh-over', 0, 'superseded'], ['ended', 'under', 0, 'superseded'], ['ended', 'over', 0, 'superseded'], ['ended', 'stopped', 3, 'same'], ['ended', 'closed', 0, 'new'],
|
||||
['missing', 'fresh', 0, 'same'], ['missing', 'fresh-over', 3, 'same'], ['missing', 'under', 0, 'superseded'], ['missing', 'over', 0, 'superseded'], ['missing', 'stopped', 3, 'same'], ['missing', 'closed', 0, 'new'],
|
||||
];
|
||||
|
||||
// Value: protects=every combination of run record and ledger state gets the one start outcome the state table names: while the checkout's /ship
|
||||
// is open, start always re-attaches its run (recording a stop past a limit), a run that ended is superseded untouched, a stop is never lifted,
|
||||
// and a closed run is left as it was; fails_when=a rerun of start inside an open /ship supersedes a run under its limit with a fresh cap and
|
||||
// clock (GSTA-892 ruled note at cmdStart), or a re-attach hands a run already past its cap an exit 0 (GST-4132);
|
||||
// why_new=#4877's cases drove single cells, and the open-record, under-limit cell was never driven; seam=the run record moved by
|
||||
// changeRunRecord; no_claim=a record that preflight bound, which test/ship-run-record.test.ts drives
|
||||
test.each(TABLE)('record %s, ledger %s: start exits %d and the run is %s', async (rec, led, code, outcome) => {
|
||||
const { h, record, a, ledger, second, b } = await cell(rec, led);
|
||||
expect(second.code, second.out + second.err).toBe(code);
|
||||
const old = h.ledger(ledger);
|
||||
if (outcome === 'same') {
|
||||
expect(b).toBe(a);
|
||||
expect(second.out).not.toContain('SUPERSEDED:');
|
||||
if (code === 3) expect(second.out).toMatch(/^STOP: /m);
|
||||
} else {
|
||||
expect(b).not.toBe(a);
|
||||
expect(fs.readFileSync(record, 'utf8')).toContain(`run=${b} `);
|
||||
expect(old.superseded_by).toBe(outcome === 'superseded' ? b : undefined);
|
||||
expect(second.out.includes('SUPERSEDED:')).toBe(outcome === 'superseded');
|
||||
if (led !== 'stopped') expect(old.stopped).toBeUndefined();
|
||||
if (led === 'over' || led === 'fresh-over') expect(old.meter.last).toBe(led === 'over' ? 5_000_000 : 1_000_000);
|
||||
}
|
||||
});
|
||||
|
||||
// Value: protects=a start that re-attaches an open /ship's run keeps that run's cap: --token-cap none or a larger cap never applies without the
|
||||
// owner's words, and the start says so; fails_when=a rerun of start with --token-cap none opens an uncapped run inside the same /ship (GSTA-892);
|
||||
// why_new=the table above passes the same cap; seam=none; no_claim=resume --token-cap, which journals a raise
|
||||
test.each([['none'], ['50M']])('a re-attaching start with --token-cap %s keeps the run\'s 10M cap', async (raw) => {
|
||||
const { h, a, ledger, second, b } = await cell('open', 'under', ['--token-cap', raw]);
|
||||
expect(second.code, second.out + second.err).toBe(0);
|
||||
expect(b).toBe(a);
|
||||
expect(h.ledger(ledger).cap).toBe(10_000_000);
|
||||
expect(second.out).toMatch(/^CAP: kept admission_cap=10000000 /m);
|
||||
});
|
||||
|
||||
// Value: protects=start --ledger never writes a new run over the record's earlier ledger, by its path, a symlink or a .. path; fails_when=the new
|
||||
// run overwrites the earlier ledger and then marks itself superseded_by its own id, so the run never meters and the earlier run's spend is lost
|
||||
// (GSTA-892 ruled note at line 300); why_new=--ledger was never driven on a reused record; seam=none; no_claim=a ledger of another record
|
||||
test('start --ledger that aliases the earlier run\'s ledger is refused and leaves it unchanged', async () => {
|
||||
for (const led of ['under', 'closed'] as const) {
|
||||
const h = fresh();
|
||||
const record = h.record();
|
||||
const first = h.sb(['start', '--record', record, '--mode', 'observe', '--token-cap', '10M', '--reading', '1000000']);
|
||||
const ledger = h.field(first.out, 'BUDGET_RUN', 'ledger');
|
||||
expect(h.sb(['note', '--record', record, '--step', '9', '--reading', '2000000']).code).toBe(0);
|
||||
if (led === 'closed') expect(h.sb(['close', '--record', record, '--reading', '3000000']).code).toBe(0);
|
||||
const before = fs.readFileSync(ledger, 'utf8');
|
||||
const dir = path.dirname(ledger);
|
||||
const aliases = [ledger, path.join(dir, '..', path.basename(dir), 'budget.json')];
|
||||
if (process.platform !== 'win32') { fs.symlinkSync(ledger, path.join(h.root, 'alias.json')); aliases.push(path.join(h.root, 'alias.json')); }
|
||||
for (const alias of aliases) {
|
||||
const r = h.sb(['start', '--record', record, '--mode', 'observe', '--reading', '4000000', '--ledger', alias]);
|
||||
expect([led, alias, r.code]).toEqual([led, alias, 2]);
|
||||
expect(r.err).toContain('is the earlier run');
|
||||
}
|
||||
expect(fs.readFileSync(ledger, 'utf8')).toBe(before);
|
||||
const other = path.join(h.root, 'other.json');
|
||||
const ok = h.sb(['start', '--record', record, '--mode', 'observe', '--reading', '4000000', '--ledger', other]);
|
||||
expect(ok.code, ok.out + ok.err).toBe(0);
|
||||
expect(h.field(ok.out, 'BUDGET_RUN', 'ledger')).toBe(other);
|
||||
}
|
||||
});
|
||||
|
||||
// Value: protects=resume --run of a superseded run marks the run that superseded it (the newest in the chain) superseded by the resumed run, so
|
||||
// it stops holding gstack upgrades, while a closed successor is left alone; fails_when=the successor stays an open, unsuperseded ledger that
|
||||
// holds every gstack upgrade on the machine for 6 hours (GSTA-892 ruled note at lines 548-554); why_new=resume of a superseded run was never
|
||||
// driven; seam=--run on the superseded id; no_claim=two resumes of different runs at once
|
||||
test('resume --run of a superseded run supersedes the newest run in its chain, and leaves a closed one alone', async () => {
|
||||
const { liveRun } = await import('../lib/ship-run-live');
|
||||
const h = fresh();
|
||||
const record = h.record();
|
||||
const id = (r: Run) => h.field(r.out, 'BUDGET_RUN', 'id');
|
||||
const ledgerOf = (run: string) => path.join(h.state, 'ship-runs', run, 'budget.json');
|
||||
const a = id(h.sb(['start', '--record', record, '--mode', 'observe', '--reading', '1000000']));
|
||||
expect(h.sb(['note', '--record', record, '--step', '9', '--reading', '2000000']).code).toBe(0);
|
||||
const b = id(h.sb(['start', '--record', record, '--mode', 'observe', '--reading', '3000000']));
|
||||
expect(h.sb(['note', '--record', record, '--step', '9', '--reading', '4000000']).code).toBe(0);
|
||||
const c = id(h.sb(['start', '--record', record, '--mode', 'observe', '--reading', '5000000']));
|
||||
expect(new Set([a, b, c]).size).toBe(3);
|
||||
const resumed = h.sb(['resume', '--run', a, '--reading', '6000000']);
|
||||
expect(resumed.code, resumed.out + resumed.err).toBe(0);
|
||||
expect(resumed.out).toMatch(new RegExp(`^SUPERSEDED: run ${c} `, 'm'));
|
||||
expect(h.ledger(ledgerOf(c)).superseded_by).toBe(a);
|
||||
expect(h.ledger(ledgerOf(b)).superseded_by).toBe(c);
|
||||
expect(h.ledger(ledgerOf(a)).superseded_by).toBeUndefined();
|
||||
expect(fs.readFileSync(record, 'utf8')).toContain(`run=${a} `);
|
||||
expect(liveRun(h.state).live).toContain(a);
|
||||
expect(h.sb(['note', '--record', record, '--step', '10', '--reading', '7000000']).out).toMatch(/^NOTE: 10 used=6000000\b/m);
|
||||
const back = h.sb(['resume', '--run', c, '--reading', '8000000']);
|
||||
expect(back.out).toMatch(new RegExp(`^SUPERSEDED: run ${a} `, 'm'));
|
||||
expect(h.sb(['close', '--run', c, '--reading', '9000000']).code).toBe(0);
|
||||
const again = h.sb(['resume', '--run', a, '--reading', '9500000']);
|
||||
expect(again.code, again.out + again.err).toBe(0);
|
||||
expect(again.out).not.toContain('SUPERSEDED:');
|
||||
expect(h.ledger(ledgerOf(c)).superseded_by).toBeUndefined();
|
||||
});
|
||||
|
||||
// Value: protects=start writes the new run, points the record at it and marks the earlier run superseded while holding the earlier ledger's lock,
|
||||
// so a note on the earlier run either lands first or finds it superseded; fails_when=start points the record at the new run before it locks the
|
||||
// earlier ledger, so a note --run <old> in between points the record back at the old run (GSTA-892 ruled note at line 315);
|
||||
// why_new=no case raced a note against a supersede; seam=the earlier ledger's lock file, held by this test; no_claim=two hosts sharing a state root
|
||||
test('a superseding start waits on the earlier ledger\'s lock before it writes anything', async () => {
|
||||
const h = fresh();
|
||||
const record = h.record();
|
||||
const first = h.sb(['start', '--record', record, '--mode', 'observe', '--reading', '1000000']);
|
||||
const a = h.field(first.out, 'BUDGET_RUN', 'id'), ledger = h.field(first.out, 'BUDGET_RUN', 'ledger');
|
||||
expect(h.sb(['note', '--record', record, '--step', '9', '--reading', '2000000']).code).toBe(0);
|
||||
const lock = `${ledger}.lock`;
|
||||
fs.writeFileSync(lock, JSON.stringify({ pid: process.pid, host: os.hostname(), at: new Date().toISOString() }));
|
||||
const child = spawn(process.execPath, [...bunIsolationArgs(), BUDGET_BIN, 'start', '--record', record, '--mode', 'observe', '--reading', '3000000'], { cwd: h.repo, env: h.env });
|
||||
let out = '', err = '';
|
||||
child.stdout.on('data', d => { out += d; });
|
||||
child.stderr.on('data', d => { err += d; });
|
||||
const exited = new Promise<number | null>(resolve => child.on('close', resolve));
|
||||
await Bun.sleep(3000);
|
||||
expect(fs.readFileSync(record, 'utf8')).toContain(`run=${a} `);
|
||||
expect(fs.readdirSync(path.join(h.state, 'ship-runs'))).toEqual([a]);
|
||||
fs.unlinkSync(lock);
|
||||
expect(await exited, out + err).toBe(0);
|
||||
const b = h.field(out, 'BUDGET_RUN', 'id');
|
||||
expect(b).not.toBe(a);
|
||||
expect(fs.readFileSync(record, 'utf8')).toContain(`run=${b} `);
|
||||
expect(h.ledger(ledger).superseded_by).toBe(b);
|
||||
}, 30_000);
|
||||
// Value: protects=a ledger preflight bound to its run re-attaches only while that run's record is open, and once another run's record is open
|
||||
// the earlier ledger is superseded even though that record opened after it; a bind skips a start older than 5 minutes and two starts at once;
|
||||
// fails_when=liveRecord ignores the binding (a later /ship re-attaches an earlier run and inherits its spend), or bindPreflightLedger binds an
|
||||
// old or ambiguous ledger (review p1 specialist-batched#2); why_new=the table above only drives unbound ledgers; seam=bindPreflightLedger called
|
||||
// with the record changeRunRecord opened; no_claim=the preflight bin itself, which test/ship-run-record.test.ts drives
|
||||
test('a bound ledger re-attaches only for its own open record, and a bind skips an old or tied start', async () => {
|
||||
const { changeRunRecord } = await import('../lib/ship-run-record');
|
||||
const { bindPreflightLedger } = await import('../lib/ship-run-live');
|
||||
const h = fresh();
|
||||
const record = h.record();
|
||||
const first = h.sb(['start', '--record', record, '--mode', 'observe', '--token-cap', '10M', '--reading', '1000000']);
|
||||
const a = h.field(first.out, 'BUDGET_RUN', 'id'), ledger = h.field(first.out, 'BUDGET_RUN', 'ledger');
|
||||
const opened = changeRunRecord(h.repo, 'preflight', 'r1');
|
||||
if (!opened.ok) throw new Error(opened.why);
|
||||
bindPreflightLedger(h.ledger(ledger).checkout, h.env, 'r1', opened.record!.started_at);
|
||||
expect(h.ledger(ledger).ship_run).toBe('r1');
|
||||
expect(h.sb(['note', '--record', record, '--step', '9', '--reading', '5000000']).code).toBe(0);
|
||||
const again = h.sb(['start', '--record', record, '--mode', 'observe', '--token-cap', '10M', '--reading', '6000000']);
|
||||
expect([again.code, h.field(again.out, 'BUDGET_RUN', 'id')]).toEqual([0, a]);
|
||||
expect(changeRunRecord(h.repo, 'abandon', 'r1', { ended_by: 'test' }).ok).toBe(true);
|
||||
expect(changeRunRecord(h.repo, 'preflight', 'r2').ok).toBe(true);
|
||||
const next = h.sb(['start', '--record', record, '--mode', 'observe', '--token-cap', '10M', '--reading', '7000000']);
|
||||
expect(next.code, next.out + next.err).toBe(0);
|
||||
const b = h.field(next.out, 'BUDGET_RUN', 'id');
|
||||
expect(b).not.toBe(a);
|
||||
expect(h.ledger(ledger).superseded_by).toBe(b);
|
||||
|
||||
const g = fresh();
|
||||
const stale = g.field(g.sb(['start', '--record', g.record('old.md'), '--mode', 'observe', '--reading', '1000000']).out, 'BUDGET_RUN', 'ledger');
|
||||
const l = g.ledger(stale);
|
||||
fs.writeFileSync(stale, JSON.stringify({ ...l, created_at: new Date(Date.now() - 10 * 60_000).toISOString() }, null, 2));
|
||||
bindPreflightLedger(l.checkout, g.env, 'r9', new Date().toISOString());
|
||||
expect(g.ledger(stale).ship_run).toBeUndefined();
|
||||
const twin = g.field(g.sb(['start', '--record', g.record('twin.md'), '--mode', 'observe', '--reading', '1000000']).out, 'BUDGET_RUN', 'ledger');
|
||||
const at = new Date().toISOString();
|
||||
for (const f of [stale, twin]) fs.writeFileSync(f, JSON.stringify({ ...g.ledger(f), created_at: at }, null, 2));
|
||||
bindPreflightLedger(l.checkout, g.env, 'r9', at);
|
||||
expect([g.ledger(stale).ship_run, g.ledger(twin).ship_run]).toEqual([undefined, undefined]);
|
||||
});
|
||||
|
||||
// Value: protects=a start while the record block names a running handoff child re-attaches the child's root parent run; fails_when=start takes
|
||||
// the child's ledger as the earlier run, so it re-attaches or supersedes the child and the parent's open run keeps its cap out of reach and
|
||||
// holds gstack upgrades (review p1 red-team#1), or the re-attach leaves the record naming the parent, so the running child's --record
|
||||
// commands meter the parent (review v2 outside-voice#4); why_new=no start case had a child on the record; seam=a child ledger written with writeLedger;
|
||||
// no_claim=the handoff commands themselves (test/ship-budget-chain.test.ts)
|
||||
test('a start while the record names a handoff child re-attaches the parent run', async () => {
|
||||
const { changeRunRecord } = await import('../lib/ship-run-record');
|
||||
const { writeLedger } = await import('../lib/ship-budget');
|
||||
const h = fresh();
|
||||
const record = h.record();
|
||||
const first = h.sb(['start', '--record', record, '--mode', 'observe', '--reading', '1000000']);
|
||||
const a = h.field(first.out, 'BUDGET_RUN', 'id'), ledger = h.field(first.out, 'BUDGET_RUN', 'ledger');
|
||||
expect(changeRunRecord(h.repo, 'preflight', 'r1').ok).toBe(true);
|
||||
expect(h.sb(['note', '--record', record, '--step', '9', '--reading', '2000000']).code).toBe(0);
|
||||
const dir = path.join(h.state, 'ship-runs', 'ship-child-1');
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
const child: Ledger = { ...h.ledger(ledger), run: 'ship-child-1', dir, file: path.join(dir, 'budget.json'), role: 'child', created_at: new Date().toISOString(), notes: [],
|
||||
parent: { run: a, ledger, handoff_sha: 'f'.repeat(64), from: '9', to: '16', return_file: path.join(dir, 'return.txt') } };
|
||||
writeLedger(child, h.env);
|
||||
expect(fs.readFileSync(record, 'utf8')).toContain('run=ship-child-1 ');
|
||||
const again = h.sb(['start', '--record', record, '--mode', 'observe', '--reading', '3000000']);
|
||||
expect(again.code, again.out + again.err).toBe(0);
|
||||
expect(h.field(again.out, 'BUDGET_RUN', 'id')).toBe(a);
|
||||
expect(again.out).not.toContain('SUPERSEDED:');
|
||||
expect(h.ledger(child.file).superseded_by).toBeUndefined();
|
||||
expect(fs.readFileSync(record, 'utf8')).toContain('run=ship-child-1 ');
|
||||
});
|
||||
|
||||
// Value: protects=resume --run walks past a closed run in the middle of the supersede chain to the newest run; fails_when=the walk stops at the
|
||||
// closed middle run and the newest run stays open, holding gstack upgrades (review p1 specialist-batched#1); why_new=the chain case above closed
|
||||
// only the newest run; seam=close --run on the middle run; no_claim=a chain whose newest ledger file was removed
|
||||
test('resume --run walks past a closed middle run to the newest run in the chain', () => {
|
||||
const h = fresh();
|
||||
const record = h.record();
|
||||
const id = (r: Run) => h.field(r.out, 'BUDGET_RUN', 'id');
|
||||
const a = id(h.sb(['start', '--record', record, '--mode', 'observe', '--reading', '1000000']));
|
||||
expect(h.sb(['note', '--record', record, '--step', '9', '--reading', '2000000']).code).toBe(0);
|
||||
const b = id(h.sb(['start', '--record', record, '--mode', 'observe', '--reading', '3000000']));
|
||||
expect(h.sb(['note', '--record', record, '--step', '9', '--reading', '4000000']).code).toBe(0);
|
||||
const c = id(h.sb(['start', '--record', record, '--mode', 'observe', '--reading', '5000000']));
|
||||
expect(h.sb(['close', '--run', b, '--reading', '5500000']).code).toBe(0);
|
||||
const resumed = h.sb(['resume', '--run', a, '--reading', '6000000']);
|
||||
expect(resumed.code, resumed.out + resumed.err).toBe(0);
|
||||
expect(resumed.out).toMatch(new RegExp(`^SUPERSEDED: run ${c} `, 'm'));
|
||||
expect(h.ledger(path.join(h.state, 'ship-runs', c, 'budget.json')).superseded_by).toBe(a);
|
||||
});
|
||||
// Value: protects=a re-attaching start applies a stricter cap (lower, or one where the run had none) at once, checking the run against it,
|
||||
// while a higher cap or none still needs resume with the owner's words; fails_when=a run started uncapped stays uncapped after a start
|
||||
// that asks for a cap, because re-attach ignores every --token-cap (review v2 targeted#1); why_new=the cap cases above only ask for
|
||||
// a higher cap or none; seam=none; no_claim=the default subagent cap, which applies only to a new run
|
||||
test('a re-attaching start applies a lower cap, and records a stop when the run is already past it', async () => {
|
||||
const { changeRunRecord } = await import('../lib/ship-run-record');
|
||||
const h = fresh();
|
||||
const record = h.record();
|
||||
const first = h.sb(['start', '--record', record, '--mode', 'observe', '--token-cap', 'none', '--reading', '1000000']);
|
||||
const a = h.field(first.out, 'BUDGET_RUN', 'id'), ledger = h.field(first.out, 'BUDGET_RUN', 'ledger');
|
||||
expect(h.ledger(ledger).cap).toBeNull();
|
||||
expect(changeRunRecord(h.repo, 'preflight', 'r1').ok).toBe(true);
|
||||
expect(h.sb(['note', '--record', record, '--step', '9', '--reading', '5000000']).code).toBe(0);
|
||||
const lower = h.sb(['start', '--record', record, '--mode', 'observe', '--token-cap', '8M', '--reading', '6000000']);
|
||||
expect([lower.code, h.field(lower.out, 'BUDGET_RUN', 'id')]).toEqual([0, a]);
|
||||
expect(lower.out).toMatch(/^CAP: lowered admission_cap=8000000 \(from none:/m);
|
||||
expect(h.ledger(ledger).cap).toBe(8_000_000);
|
||||
const past = h.sb(['start', '--record', record, '--mode', 'observe', '--token-cap', '3M', '--reading', '6500000']);
|
||||
expect([past.code, h.field(past.out, 'BUDGET_RUN', 'id')]).toEqual([3, a]);
|
||||
expect(past.out).toMatch(/^STOP: reason=budget_exhausted /m);
|
||||
expect(h.ledger(ledger)).toMatchObject({ cap: 3_000_000, stopped: { reason: 'budget_exhausted' } });
|
||||
});
|
||||
|
||||
// Value: protects=two starts racing to supersede the same earlier run open one new run between them: the second re-reads the record after the
|
||||
// lock and joins the run the first opened; fails_when=both mint a run, the second overwrites the earlier run's superseded_by and the first
|
||||
// new run is left open and unsuperseded, holding gstack upgrades (review v2 outside-voice#3, adversarial-native#1); why_new=the lock case
|
||||
// above raced a note; seam=the earlier ledger's lock file, held by this test while both starts wait; no_claim=two hosts sharing a state root
|
||||
test('two starts racing to supersede the same run open one new run between them', async () => {
|
||||
const h = fresh();
|
||||
const record = h.record();
|
||||
const first = h.sb(['start', '--record', record, '--mode', 'observe', '--reading', '1000000']);
|
||||
const a = h.field(first.out, 'BUDGET_RUN', 'id'), ledger = h.field(first.out, 'BUDGET_RUN', 'ledger');
|
||||
expect(h.sb(['note', '--record', record, '--step', '9', '--reading', '2000000']).code).toBe(0);
|
||||
const lock = `${ledger}.lock`;
|
||||
fs.writeFileSync(lock, JSON.stringify({ pid: process.pid, host: os.hostname(), at: new Date().toISOString() }));
|
||||
const launch = (reading: string) => {
|
||||
const child = spawn(process.execPath, [...bunIsolationArgs(), BUDGET_BIN, 'start', '--record', record, '--mode', 'observe', '--reading', reading], { cwd: h.repo, env: h.env });
|
||||
let out = '';
|
||||
child.stdout.on('data', d => { out += d; });
|
||||
child.stderr.on('data', d => { out += d; });
|
||||
return new Promise<{ code: number | null; out: string }>(resolve => child.on('close', code => resolve({ code, out })));
|
||||
};
|
||||
const runs = [launch('3000000'), launch('3100000')];
|
||||
await Bun.sleep(2000);
|
||||
fs.unlinkSync(lock);
|
||||
const [x, y] = await Promise.all(runs);
|
||||
expect([x.code, y.code], x.out + y.out).toEqual([0, 0]);
|
||||
const ids = new Set([h.field(x.out, 'BUDGET_RUN', 'id'), h.field(y.out, 'BUDGET_RUN', 'id')]);
|
||||
expect(ids.size).toBe(1);
|
||||
const b = [...ids][0]!;
|
||||
expect(b).not.toBe(a);
|
||||
expect(fs.readdirSync(path.join(h.state, 'ship-runs')).sort()).toEqual([a, b].sort());
|
||||
expect(h.ledger(ledger).superseded_by).toBe(b);
|
||||
expect(fs.readFileSync(record, 'utf8')).toContain(`run=${b} `);
|
||||
}, 30_000);
|
||||
|
||||
// Value: protects=preflight binds its run's ledger by the time the preflight began, so a slow fetch or merge never leaves its own ledger
|
||||
// unbound (and its record stuck open after Step 22's close); fails_when=the window is measured to the record's opening at preflight's
|
||||
// end, so a preflight that takes over 5 minutes binds nothing (review v2 outside-voice#2, adversarial-native#2); why_new=the bind cases
|
||||
// above passed the record's own time; seam=the ledger's created_at, backdated, and the time lib/ship-preflight.ts passes (its own start);
|
||||
// no_claim=a start run long before its preflight, or the preflight bin passing its start (this case drives the bind's contract only)
|
||||
test('a ledger started 8 minutes before a slow preflight opened its record is still bound', async () => {
|
||||
const { bindPreflightLedger } = await import('../lib/ship-run-live');
|
||||
const h = fresh();
|
||||
const ledger = h.field(h.sb(['start', '--record', h.record(), '--mode', 'observe', '--reading', '1000000']).out, 'BUDGET_RUN', 'ledger');
|
||||
const l = h.ledger(ledger);
|
||||
fs.writeFileSync(ledger, JSON.stringify({ ...l, created_at: new Date(Date.now() - 8 * 60_000).toISOString() }, null, 2));
|
||||
bindPreflightLedger(l.checkout, h.env, 'r1', new Date(Date.now() - 7 * 60_000).toISOString());
|
||||
expect(h.ledger(ledger).ship_run).toBe('r1');
|
||||
});
|
||||
// Value: protects=resume --run finds a successor started with --ledger <custom path> through the record block that names it; fails_when=the
|
||||
// walk looks only under ship-runs/<id>/budget.json, so the custom-path successor stays open and holds gstack upgrades (review v2
|
||||
// outside-voice#5, adversarial-native#4); why_new=every chain case above used the default path; seam=start --ledger; no_claim=a custom-path
|
||||
// run in the middle of a chain, which the record block no longer names
|
||||
test('resume --run supersedes a successor that start --ledger put at a custom path', () => {
|
||||
const h = fresh();
|
||||
const record = h.record();
|
||||
const a = h.field(h.sb(['start', '--record', record, '--mode', 'observe', '--reading', '1000000']).out, 'BUDGET_RUN', 'id');
|
||||
expect(h.sb(['note', '--record', record, '--step', '9', '--reading', '2000000']).code).toBe(0);
|
||||
const custom = path.join(h.root, 'custom', 'budget.json');
|
||||
const second = h.sb(['start', '--record', record, '--mode', 'observe', '--reading', '3000000', '--ledger', custom]);
|
||||
expect(second.code, second.out + second.err).toBe(0);
|
||||
const b = h.field(second.out, 'BUDGET_RUN', 'id');
|
||||
const resumed = h.sb(['resume', '--run', a, '--reading', '4000000']);
|
||||
expect(resumed.code, resumed.out + resumed.err).toBe(0);
|
||||
expect(resumed.out).toMatch(new RegExp(`^SUPERSEDED: run ${b} `, 'm'));
|
||||
expect(h.ledger(custom).superseded_by).toBe(a);
|
||||
});
|
||||
// Value: protects=preflight never binds a ledger another harness session started, so that session's late close never ends this run's record;
|
||||
// fails_when=an unmetered /ship binds an earlier session's unbound ledger started within 5 minutes and that ledger's close ends the newer
|
||||
// run (review v2 outside-voice#1); why_new=the bind cases above had one session; seam=the ledger's owner, rewritten to another pid;
|
||||
// no_claim=a shared harness (capy-server), whose sessions the owner cannot tell apart
|
||||
test('a bind skips a ledger another harness session started', async () => {
|
||||
const { bindPreflightLedger } = await import('../lib/ship-run-live');
|
||||
const h = fresh();
|
||||
const ledger = h.field(h.sb(['start', '--record', h.record(), '--mode', 'observe', '--reading', '1000000']).out, 'BUDGET_RUN', 'ledger');
|
||||
const l = h.ledger(ledger);
|
||||
const me = { machine: 'm', boot: 'b', pid: 4242, start: 7, comm: 'claude' };
|
||||
fs.writeFileSync(ledger, JSON.stringify({ ...l, owner: { ...me, pid: 4343 } }, null, 2));
|
||||
bindPreflightLedger(l.checkout, h.env, 'r1', new Date().toISOString(), me);
|
||||
expect(h.ledger(ledger).ship_run).toBeUndefined();
|
||||
bindPreflightLedger(l.checkout, h.env, 'r1', new Date().toISOString(), { ...me, shared: true });
|
||||
expect(h.ledger(ledger).ship_run).toBe('r1');
|
||||
});
|
||||
});
|
||||
@@ -326,6 +326,26 @@ describe('the 100-minute stop', () => {
|
||||
expect(ok.status, ok.stdout + ok.stderr).toBe(0);
|
||||
expect(ok.stdout).not.toContain('past 100 minutes');
|
||||
});
|
||||
|
||||
// Value: protects=gstack calls in a nested checkout or submodule under the run's checkout never count as the parent run's active time, while a
|
||||
// plain subdirectory of the checkout still does; fails_when=a lane working in a nested checkout keeps the parent checkout's parked run ticking
|
||||
// until it stops at 100 minutes, because the clock compared path prefixes (GSTA-892 ruled note at lib/ship-clock.ts:39);
|
||||
// why_new=the case above used a sibling directory; seam=the call log's cwd field; no_claim=a cwd deleted since its row, which falls back to the prefix
|
||||
test("a nested checkout's gstack calls do not keep the parent checkout's clock running; a subdirectory's do", () => {
|
||||
for (const [where, stops] of [['nested', false], ['src', true]] as const) {
|
||||
const b = box();
|
||||
start(b);
|
||||
backdate(b, 180, false);
|
||||
const dir = path.join(b.repo, where === 'nested' ? 'vendor/nested' : 'src');
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
if (where === 'nested') expect(spawnSync('git', ['init', '-q'], { cwd: dir, timeout: 30_000 }).status).toBe(0);
|
||||
calls(b, 180, 150, b.repo);
|
||||
calls(b, 148, 22, dir);
|
||||
calls(b, 20, 0, b.repo);
|
||||
const r = bin(b, 'gstack-ship-budget', ['note', '--record', b.record, '--step', '16', '--reading', '2000', '--credits', '2']);
|
||||
expect([where, r.status, r.stdout.includes('past 100 minutes')]).toEqual([where, stops ? 3 : 0, stops]);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('calls per phase and calls= in the receipt', () => {
|
||||
|
||||
@@ -452,4 +452,29 @@ describe('a finished run no longer blocks the next /ship, and abandon never hint
|
||||
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' });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user