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
/**
* ship-run-live — whether a /ship or review run on this machine still holds gstack upgrades (GST-3546, bin/gstack-capy-session's
* open_run, R75's workflow boundary). The candidates are the ones open_run always read: an unclosed ship-budget ledger written in
* the last 6 hours and an unconsumed review start under 120 minutes old. A candidate no longer holds once its run provably ended:
* its owner process is gone (ownerGone: the harness it ran under exited or the machine restarted; a shared harness such as
* capy-server proves nothing), or the checkout's /ship run record says the run finished or was abandoned and the record is this
* ledger's: the ledger is the one newest ledger for that checkout created at or before the record opened, so neither an earlier
* run's record nor a later run started on the same checkout releases it (for a review start, the record naming the run its claim
* stamped). A stopped run can still claim its fix-check grant and resume, so it keeps
* the hold, and so does a record that is missing, open, legacy, unreadable, older than the ledger or another ledger's.
* A ledger a later start superseded (superseded_by) no longer holds either.
* A finished run ends (endClosedRun, GST-4109): an open record whose own ledger closed (Step 22's close) is marked finished, so it no
* longer blocks the next /ship on the checkout, while a record whose ledger is still open keeps blocking. Its own ledger is the one its
* preflight bound to its run id (bindPreflightLedger, GST-4132), never one picked by timing.
*/
import * as fs from 'node:fs';
import * as path from 'node:path';
import { mutateLedger } from './ship-budget';
import { changeRunRecord, ownerGone, readRunRecord, type RunOwner, type RunRecord } from './ship-run-record';
import { resolveStateRoot } from './state-root';
export const LEDGER_HOLD_MS = 360 * 60_000;
export const REVIEW_START_HOLD_MS = 120 * 60_000;
const PREFLIGHT_AFTER_START_MS = 5 * 60_000;
const ENDED = new Set(['finished', 'abandoned']);
const recordEnded = (checkout: unknown, ours: (r: RunRecord) => boolean) => {
if (typeof checkout !== 'string' || !checkout) return false;
const read = readRunRecord(checkout);
return read.kind === 'record' && ENDED.has(read.record.state) && ours(read.record);
};
const fresh = (file: string, within: number, now: number) => { try { return now - fs.statSync(file).mtimeMs < within; } catch { return false; } };
const json = (file: string): Record<string, any> | null => { try { const v = JSON.parse(fs.readFileSync(file, 'utf8')); return v && typeof v === 'object' ? v : null; } catch { return null; } };
const dirs = (dir: string) => { try { return fs.readdirSync(dir, { withFileTypes: true }).filter(d => d.isDirectory()).map(d => path.join(dir, d.name)); } catch { return []; } };
const files = (dir: string, ext: string) => { try { return fs.readdirSync(dir).filter(n => n.endsWith(ext)).map(n => path.join(dir, n)); } catch { return []; } };
type Ledgers = Array<{ file: string; l: Record<string, any> }>;
const shipLedgers = (stateRoot: string): Ledgers => dirs(path.join(stateRoot, 'ship-runs')).map(d => ({ file: path.join(d, 'budget.json'), l: json(path.join(d, 'budget.json')) }))
.filter((x): x is Ledgers[number] => x.l?.kind === 'gstack-ship-budget');
const created = (l: Record<string, any>) => Date.parse(String(l.created_at));
/** Whether a run record is this ledger's: the ledger is the one newest ledger for its checkout created at or before the record opened. */
const owns = (ledgers: Ledgers, l: Record<string, any>) => (r: RunRecord) => {
const before = ledgers.filter(x => x.l.checkout === l.checkout && created(x.l) <= Date.parse(r.started_at)).map(x => created(x.l));
const newest = Math.max(...before);
return created(l) === newest && before.filter(t => t === newest).length === 1;
};
/**
* Bind the ledger of the /ship whose preflight just opened `run`'s record (GST-4132): the one newest parent ledger for the checkout that is
* open, unsuperseded and unbound, and started at most PREFLIGHT_AFTER_START_MS before this preflight began (/ship's start-then-preflight;
* measured from the preflight's start, so a slow fetch or merge never pushes its own ledger out of the window), and not started under
* another harness process than this preflight's `owner` (a harness every session shares, such as capy-server, proves nothing).
* No such ledger (an unmetered /ship, a start long before its preflight, two started at once) binds nothing, so its close ends no record.
* A bind that cannot take the ledger's lock is skipped: the record then stays open until skill-end, stop or abandon end it.
*/
export function bindPreflightLedger(checkout: string, env: NodeJS.ProcessEnv, run: string, preflightAt: string, owner: RunOwner | null = null): void {
const began = Date.parse(preflightAt);
const otherSession = (o: RunOwner | undefined) => !!o && !!owner && !o.shared && !owner.shared
&& (o.machine !== owner.machine || o.boot !== owner.boot || o.pid !== owner.pid || o.start !== owner.start);
const candidates = shipLedgers(resolveStateRoot(env)).filter(({ l }) => l.checkout === checkout && l.role !== 'child' && !l.closed_at && !l.superseded_by && !l.ship_run
&& created(l) <= began && began - created(l) <= PREFLIGHT_AFTER_START_MS && !otherSession(l.owner));
const newest = Math.max(...candidates.map(x => created(x.l)));
const mine = candidates.filter(x => created(x.l) === newest);
if (mine.length !== 1) return;
try { mutateLedger(mine[0]!.file, env, l => { if (!l.closed_at && !l.superseded_by && !l.ship_run) l.ship_run = run; }); } catch { /* unbound: fails closed */ }
}
/**
* End a finished run: when the checkout's record is open and the ledger its preflight bound to its run (bindPreflightLedger) has closed,
* mark it finished (ended_by gstack-ship-budget close) and return the RUN_ENDED line; null when there is nothing to end. Any other
* ledger belongs to another run (a later run that wrote no ledger of its own, such as an unmetered start, sits behind it), so its late
* close never ends that record, however close in time the two started. `checkout` is the git toplevel a ledger records.
*/
export function endClosedRun(checkout: string, stateRoot: string): string | null {
const read = readRunRecord(checkout);
if (read.kind !== 'record' || read.record.state !== 'open') return null;
const run = read.record.run;
const mine = shipLedgers(stateRoot).find(x => x.l.checkout === checkout && x.l.ship_run === run && x.l.closed_at);
if (!mine) return null;
const done = changeRunRecord(checkout, 'close', run, { ended_by: 'gstack-ship-budget close' });
return done.ok ? `RUN_ENDED: run=${run} state=finished ended_by=gstack-ship-budget close (its budget ledger ${path.basename(path.dirname(mine.file))} closed at ${mine.l.closed_at})` : null;
}
/** The first run that still holds upgrades, as "<kind> run <id> (<file>)", or undefined; ended lists what no longer holds and why. */
export function liveRun(stateRoot: string, now = Date.now()): { live?: string; ended: string[] } {
const ended: string[] = [];
const ledgers = shipLedgers(stateRoot);
for (const { file, l } of ledgers) {
if (!fresh(file, LEDGER_HOLD_MS, now)) continue;
if (!l || l.closed_at || l.superseded_by) continue;
const id = path.basename(path.dirname(file));
if (ownerGone(l.owner)) { ended.push(`/ship run ${id}: its owner process ${l.owner.pid} is gone`); continue; }
if (recordEnded(l.checkout, owns(ledgers, l))) { ended.push(`/ship run ${id}: the run record in ${l.checkout} has ended`); continue; }
return { live: `/ship run ${id} (${file})`, ended };
}
for (const project of dirs(path.join(stateRoot, 'projects'))) {
for (const file of files(path.join(project, '.review-starts'), '.json')) {
if (!fresh(file, REVIEW_START_HOLD_MS, now)) continue;
const s = json(file);
const id = path.basename(file, '.json');
if (ownerGone(s?.owner)) { ended.push(`review run ${id}: its owner process ${s!.owner.pid} is gone`); continue; }
if (s?.fix_check && recordEnded(s.repo, r => r.run === s.fix_check.run)) { ended.push(`review run ${id}: the /ship run record in ${s.repo} has ended`); continue; }
return { live: `review run ${id} (${file})`, ended };
}
}
return { ended };
}