Run each driven /ship phase as one command, count its calls, and stop it at 100 minutes (GST-2984) (#4451)

A driven /ship now runs each ship/DRIVER.md phase as one command, shows
how many calls the run made, and stops itself with a printed line at 100
minutes.

Why: measured /ship runs made 121 to 308 calls where the driver plans 9
to 12, and 10 of 12 ran past the 100-minute stop while those limits were
only prose.

Nothing to do: older ship records without `calls=` still validate and
print.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Review-Finding: p1/outside-voice#1 reader=outside-voice
This commit is contained in:
Garry Tanandcapy-ai[bot] authored and GitHub committed 2026-11-17 16:44:06 +00:00
1 parent 9c6e0a0f85
commit 0ee5914628
19 files changed
+807 -49

No files matched your search

+1 -1
View File
@@ -88,7 +88,7 @@ const mark = async (reader, state, extra) => {
const reading = async () => {
if (!input.record || !input.jam) return null;
const t = await tools.thread.get({ thread_id: input.jam });
return { tokens: t.cost.tokens, credits: t.cost.credits };
return { tokens: t.cost.tokens, credits: t.cost.credits ?? Math.round(Number(String(t.cost.dollars).replace(/[^0-9.]/g, '')) * 100000) / 100 };
};
const briefText = async (r) => {
let text = '';
+69
View File
@@ -0,0 +1,69 @@
// gstack /ship phase program for Capy (lib/capy-ship-phase.js, G2). The body of an async function (tools, sleep, now, input),
// served verbatim by `gstack-ship-phase program` and loaded the way capy/skill/gstack/SKILL.md loads the review pass program;
// never retyped. One call runs one ship/DRIVER.md phase end to end:
// 1. `gstack-ship-phase run` with input.commands, in order and resumable (lib/ship-phase.ts). With input.jam, this jam's
// cost.tokens and cost.credits (cost.dollars x 1,000 when the host reports only dollars) go to the commands as
// $GSTACK_SHIP_READING and $GSTACK_SHIP_CREDITS (gstack-ship-budget start).
// A command still running at 200 s returns NEXT: rerun: call the same program again with the same input.
// 2. With input.pass, the review pass program (gstack-review-dispatch program) on that pass's input: its dispatches, polls
// and closing note. Otherwise, with input.record and input.step, the closing `gstack-ship-budget note` with a fresh reading.
// 3. The next phase's section parts (`gstack-ship-parts next`) once Phase 2 started <dir>/parts.
// It returns result lines only, at most 32 KB; any STOP: line ends the phase (STOP: past 100 minutes included).
const T0 = input.startedAt ?? (await now());
const RETURN_BYTES = 32000;
const q = (s) => "'" + String(s).replace(/'/g, "'\\''") + "'";
const lines = [];
const sh = async (cmd, env) => {
const pre = (input.launch ? '. ' + q(input.launch) + ' && ' : '') + (input.cwd ? 'cd ' + q(input.cwd) + ' && ' : '') + (env || '');
const r = await tools.capy.bash({ command: pre + cmd, description: 'Ship phase ' + input.phase, timeout: 300 });
return { code: r.exit_code, out: (r.output || '').trimEnd() };
};
const finish = () => {
const text = lines.join('\n');
if (text.length <= RETURN_BYTES) return text;
const last = '\n(return capped at 32 KB; each command\'s log is under <dir>/phases)\n' + lines[lines.length - 1];
return lines.slice(0, -1).join('\n').slice(0, Math.max(0, RETURN_BYTES - last.length)) + last;
};
const push = (out) => { for (const l of out.split('\n')) if (l.trim()) lines.push(l); };
const stopped = () => lines.some((l) => l.startsWith('STOP:'));
const reading = async () => {
const t = await tools.thread.get({ thread_id: input.jam });
return { tokens: t.cost.tokens, credits: t.cost.credits ?? Math.round(Number(String(t.cost.dollars).replace(/[^0-9.]/g, '')) * 100000) / 100 };
};
if (!input.dir || input.phase === undefined || input.phase === null) return 'STOP: input.dir and input.phase are required; nothing ran';
const commands = input.commands || [];
if (!Array.isArray(commands) || !commands.every((c) => typeof c === 'string' && c.trim())) return 'STOP: input.commands must be a list of shell command strings; nothing ran';
if (commands.length) {
const r0 = input.jam ? await reading() : null;
const env = r0 ? 'GSTACK_SHIP_READING=' + r0.tokens + ' GSTACK_SHIP_CREDITS=' + r0.credits + ' ' : '';
const run = await sh('gstack-ship-phase run --dir ' + q(input.dir) + ' --phase ' + q(input.phase) + (input.record ? ' --record ' + q(input.record) : '')
+ ' --wait-s ' + Math.max(1, Math.round((T0 + (input.budgetMs || 200000) - (await now())) / 1000)) + ' -- ' + commands.map(q).join(' '), env);
push(run.out);
if (run.code !== 0 && !stopped()) lines.push('STOP: gstack-ship-phase run exited ' + run.code + '; nothing after it ran');
if (run.code !== 0 || (lines.length && lines[lines.length - 1].startsWith('NEXT: rerun'))) return finish();
}
if (input.pass) {
const p = await sh('gstack-review-dispatch program');
const nl = p.out.indexOf('\n');
if (p.code !== 0 || !p.out.startsWith('PROGRAM: ') || nl < 0) { lines.push('STOP: gstack-review-dispatch program exited ' + p.code + ': ' + p.out.slice(0, 300)); return finish(); }
lines.push(p.out.slice(0, nl));
const AsyncFunction = Object.getPrototypeOf(async () => {}).constructor;
const passInput = Object.assign({ startedAt: T0, launch: input.launch, cwd: input.cwd, record: input.record, jam: input.jam }, input.pass);
push(String(await new AsyncFunction('tools', 'sleep', 'now', 'input', JSON.parse(p.out.slice(nl + 1).split('\n')[0]))(tools, sleep, now, passInput)));
return finish();
}
if (input.record && input.step) {
if (!input.jam) { lines.push('STOP: the closing note needs input.jam (this jam\'s id) for its reading; the phase\'s commands ran'); return finish(); }
const r1 = await reading();
const n = await sh('gstack-ship-budget note --record ' + q(input.record) + ' --step ' + q(input.step) + (input.label ? ' --label ' + q(input.label) : '')
+ ' --reading ' + r1.tokens + ' --credits ' + r1.credits);
push(n.out);
if (n.code !== 0) { if (!stopped()) lines.push('STOP: gstack-ship-budget note exited ' + n.code + ' (its lines above say why)'); return finish(); }
}
const s = await sh('test -f ' + q(String(input.dir).replace(/\/+$/, '') + '/parts/state.json') + ' && echo started || true');
if (s.out.startsWith('started') && /^\d+$/.test(String(input.phase))) {
const room = RETURN_BYTES - lines.join('\n').length - 200;
const p = await sh('gstack-ship-parts next --run ' + q(input.dir) + ' --phase ' + (Number(input.phase) + 1) + ' --room ' + Math.max(0, room));
push(p.out);
}
return finish();
+3 -1
View File
@@ -102,6 +102,8 @@ export interface PregateFile {
export interface ShipReceipt {
schema_version: 1; head: string; base: string; pr?: number; version?: string; gated_tree?: string;
gate?: Record<string, unknown>; ci?: Record<string, unknown>; spend_usd?: number | string; predecessor?: string;
/** The run's calls from the call log (lib/ship-clock.ts callsSince: gstack-command calls plus keep-alives), so a 200-call run shows at the receipt; absent on older receipts. */
calls?: number;
/** GSTA-258: the lanes CI carried at this head's tree, one entry per run (gstack-evidence carried). */
ci_carried?: Array<{ lane: string; run: number; attempt: number; tree: string; exception?: BoundLaneException }>;
session_kind?: string; artifacts_consumed?: string; run?: string;
@@ -263,7 +265,7 @@ export const ARTIFACT_SCHEMAS = {
type: 'object', required: ['schema_version', 'head', 'base'],
properties: {
schema_version: { const: 1 }, pr: { type: 'integer' }, head: str, version: str, base: str, gated_tree: str,
gate: { type: 'object' }, ci: { type: 'object' }, spend_usd: { type: ['number', 'string'] }, predecessor: str,
gate: { type: 'object' }, ci: { type: 'object' }, spend_usd: { type: ['number', 'string'] }, calls: { type: 'integer', minimum: 0 }, predecessor: str,
ci_carried: { type: 'array', items: { type: 'object', required: ['lane', 'run', 'attempt', 'tree'], properties: { lane: str, run: { type: 'integer', minimum: 1 }, attempt: { type: 'integer', minimum: 1 }, tree: str,
exception: { type: 'object', required: ['head', 'files', 'by', 'reason', 'at', 'approval', 'preflight'], properties: { head: str, files: strList, by: str, reason: str, at: str, approval: str,
preflight: { type: 'object', required: ['label', 'tests_ran'], properties: { label: str, tests_ran: { type: 'integer', minimum: 1 } } } } } } } },
+13 -5
View File
@@ -21,6 +21,7 @@ import {
} from './ship-budget';
import { callLogFile, countLoggedCalls, loggedCallsLine, readCallLog } from './ship-call-log';
import { callsLine, countCalls } from './ship-calls';
import { callsSince, timeStop } from './ship-clock';
import { claudeConfigDir, loadMeterState, newMeterState, nonceSessions, readClaudeUsage, saveMeterState, sessionJournals } from './claude-usage';
export type Opts = Record<string, string | true>;
@@ -380,14 +381,15 @@ export function cmdClose(argv: string[], ctx: Ctx): Outcome {
});
}
const noteText = (n: Note) => `${n.step}${n.label ? `:${n.label}` : ''} used=${n.used}${n.credits === undefined ? '' : ` credits=${n.credits}`}`
const noteText = (n: Note) => `${n.step}${n.label ? `:${n.label}` : ''} used=${n.used}${n.credits === undefined ? '' : ` credits=${n.credits}`}${n.calls === undefined ? '' : ` calls=${n.calls}`}`
+ (n.readers ? ` readers=${n.readers.n} readers_tokens=${n.readers.tokens}${n.readers.credits === undefined ? '' : ` readers_credits=${n.readers.credits}`}` : '');
/**
* note: record one program's readings (tokens and credits) and, when --dispatched-at
* brackets them, the readers it dispatched; stop only once used + unknown pass the cap.
* It reserves, admits and delegates nothing: ship/DRIVER.md's observe runs end every
* program with it instead of a check at each boundary.
* program with it instead of a check at each boundary. It records the calls since the previous
* note (lib/ship-clock.ts), and in observe mode past 100 minutes it prints STOP: past 100 minutes.
*/
export function cmdNote(argv: string[], ctx: Ctx): Outcome {
const o = parseArgs(argv, ['run', 'record', 'step', 'label', 'reading', 'credits', 'dispatched-at', 'dispatched-credits', 'readers']);
@@ -406,9 +408,14 @@ export function cmdNote(argv: string[], ctx: Ctx): Outcome {
const credits = creditsUsed(l);
const readers = dispatched && l.meter.last !== null ? { n: dispatched.readers ?? 1, tokens: l.meter.last - dispatched.at,
...(before !== undefined && l.credits ? { credits: Math.round((l.credits.last - before) * 100) / 100 } : {}) } : undefined;
const n: Note = { step, ...(label ? { label } : {}), at: new Date().toISOString(), used: tally(l).used, ...(credits === null ? {} : { credits }), ...(readers ? { readers } : {}) };
const at = new Date().toISOString();
const prev = l.notes?.at(-1)?.at;
const calls = callsSince(ctx.env, prev ?? l.created_at, at, prev === undefined);
const n: Note = { step, ...(label ? { label } : {}), at, used: tally(l).used, ...(credits === null ? {} : { credits }), ...(readers ? { readers } : {}), ...(calls === null ? {} : { calls }) };
(l.notes ??= []).push(n);
return observed(l, step, label ?? step, null, `NOTE: ${noteText(n)}`);
const out = observed(l, step, label ?? step, null, `NOTE: ${noteText(n)}`);
const stop = out.exit === 0 ? timeStop(l, ctx.cwd, step, ctx.env) : null;
return stop ? { stdout: [out.stdout[0]!, ...stop], exit: 3 } : out;
});
}
@@ -480,8 +487,9 @@ export function cmdResume(argv: string[], ctx: Ctx): Outcome {
}
const was = l.stopped;
delete l.stopped;
if (was?.reason === 'past_100_minutes') l.clock_from = new Date().toISOString();
pointMarker(l.marker, l);
return [`BUDGET_RUN: id=${l.run} ledger=${l.file}`, `RESUMED: from=${was ? `Step ${was.step} (${was.reason})` : 'running'}`, budgetLine(l, { call: null, next: was?.next ?? '-', projected: 0, verdict: 'continue' })];
return [`BUDGET_RUN: id=${l.run} ledger=${l.file}`, `RESUMED: from=${was ? `Step ${was.step} (${was.reason})` : 'running'}`, ...(was?.reason === 'past_100_minutes' ? [`CLOCK: restarted at ${l.clock_from} (the 100-minute limit counts from here)`] : []), budgetLine(l, { call: null, next: was?.next ?? '-', projected: 0, verdict: 'continue' })];
});
return { stdout: [...lines, ...out], exit: 0 };
}
+3 -1
View File
@@ -70,7 +70,7 @@ export interface Reservation {
starts?: Array<{ token: string; at: string }>;
}
/** One observation: the run's totals at a program's end, and the readers it dispatched when it measured them. */
export interface Note { step: string; label?: string; at: string; used: number; credits?: number; readers?: { n: number; tokens: number; credits?: number } }
export interface Note { step: string; label?: string; at: string; used: number; credits?: number; readers?: { n: number; tokens: number; credits?: number }; calls?: number }
export interface Sample { id: string; kind: 'step' | 'dispatch'; epoch: number; prior: number; projected: number; actual: number }
export interface Handoff {
n: number; sha: string; file: string; from: string; to: string; sub_cap: number; reservation: string; reading_at: number | null;
@@ -102,6 +102,8 @@ export interface Ledger {
/** tree: the reading is the root thread's counter (a subagent cannot read its own jam id), so every cost line carries TREE_METER. */
meter_scope?: 'tree';
stopped?: { at: string; reason: string; step: string; next: string }; closed_at?: string;
/** Where the 100-minute clock starts (lib/ship-clock.ts): created_at, or the resume after a time stop. */
clock_from?: string;
}
/** A refusal: exit 2 for usage and validation (stderr), exit 3 for a stop. */
+7
View File
@@ -25,6 +25,12 @@ export interface LoggedCalls {
}
const NESTED = 'GSTACK_SHIP_CALL_NESTED';
let pending: { bin: string; sub: string; started: number } | null = null;
/** This process's own row as it will be written at exit (null when it writes none), for a count taken before it exits (lib/ship-clock.ts). */
export function pendingRow(): CallRow | null {
return pending && { v: 1, bin: pending.bin, sub: pending.sub, start: new Date(pending.started).toISOString(), ms: Date.now() - pending.started, exit: 0 };
}
const ROTATE_BYTES = 8 * 1024 * 1024;
const JOIN_MS = 5_000;
const POLL_S = 200;
@@ -41,6 +47,7 @@ export function logShipCall(bin: string, argv: string[] = process.argv.slice(2))
const started = Date.now();
const flag = (name: string) => { const i = argv.indexOf(`--${name}`); return i >= 0 ? argv[i + 1] : undefined; };
const num = (name: string) => { const v = Number(flag(name)); return flag(name) !== undefined && Number.isFinite(v) ? v : undefined; };
pending = { bin, sub: argv[0] && !argv[0].startsWith('-') ? argv[0] : '', started };
process.on('exit', code => {
try {
const row: CallRow = { v: 1, bin, sub: argv[0] && !argv[0].startsWith('-') ? argv[0] : '', start: new Date(started).toISOString(), ms: Date.now() - started, exit: code };
+86
View File
@@ -0,0 +1,86 @@
/**
* ship-clock — a driven /ship's 100-minute stop and its call count, in code instead of prose (ship/DRIVER.md).
* Measured conductors made 121-308 calls against the driver's 9 to 12, and 10 of 12 ran past its 100-minute stop
* (handoffs/ship-conductor-cost-2026-11-16), so the limits now print. An observe-mode run measures its time from
* the ledger's start, or from the resume that followed a time stop; the first `gstack-ship-budget note` or
* `gstack-ship-phase run` past 100 minutes stops the run and writes its state (head, review passes, next act) to
* <ledger dir>/stop.json, so the next session starts from that file and the record, not from memory.
* Calls come from the call log (lib/ship-call-log.ts): the calls that ran a gstack command plus its keep-alive
* estimate; quick calls that run no gstack command are not in it.
*/
import { spawnSync } from 'node:child_process';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { ledgerFileFor, readLedger, type Ledger } from './ship-budget';
import { callLogFile, countLoggedCalls, pendingRow, readCallLog } from './ship-call-log';
export const TIME_LIMIT_MIN = 100;
export const STOP_FILE = 'stop.json';
export const clockFrom = (l: Pick<Ledger, 'created_at' | 'clock_from'>) => l.clock_from ?? l.created_at;
export const elapsedMin = (l: Pick<Ledger, 'created_at' | 'clock_from'>, now = Date.now()) => (now - Date.parse(clockFrom(l))) / 60_000;
/**
* Calls whose rows start after `since` (and at or before `until`): the log's calls plus its keep-alive estimate.
* Counting rows by start keeps two back-to-back windows from sharing a row. At the run's start (`fromRunStart`) a row
* still running at `since` counts too: the call that ran `gstack-ship-budget start` began before the ledger existed.
* This process's own row, written only at its exit, counts as if written. Null when the window holds none.
*/
export function callsSince(env: NodeJS.ProcessEnv, since: string, until?: string, fromRunStart = false): number | null {
const from = Date.parse(since), to = until ? Date.parse(until) : Infinity;
const self = pendingRow();
const rows = [...readCallLog(callLogFile(env)), ...(self ? [self] : [])].filter(r => { const s = Date.parse(r.start); return s <= to && (s > from || (fromRunStart && s + Math.max(0, r.ms) >= from)); });
if (!rows.length) return null;
const c = countLoggedCalls(rows);
return c.calls + c.keepaliveEst;
}
/** A run's calls from its budget record, start to now (gstack-ship-receipt write --budget-record); null when the log holds none. */
export function runCalls(record: string, env: NodeJS.ProcessEnv): number | null {
return callsSince(env, readLedger(ledgerFileFor({ record }, env)).created_at, undefined, true);
}
const HEADING = /^\s*(?:[-*]\s*)?(?:#+\s*)?(?:\*\*)?(Release|Decisions|Reviews|Checks|Documentation|Next steps)(?:\*\*)?:?(?:\*\*)?(?:\s|$)/;
/** The record's next act: the last line under its Next steps heading (one of the six lib/ship-handoff.ts names), else null. */
export function nextAct(record: string | undefined): string | null {
if (!record) return null;
let text: string;
try { text = fs.readFileSync(record, 'utf8'); } catch { return null; }
const lines = text.split('\n');
const at = lines.findIndex(l => HEADING.exec(l)?.[1] === 'Next steps');
if (at < 0) return null;
const end = lines.findIndex((l, i) => i > at && (HEADING.test(l) || l.startsWith('```')));
const body = lines.slice(at + 1, end < 0 ? lines.length : end).map(l => l.replace(/^\s*[-*]\s*/, '').trim())
.filter(l => l && !l.startsWith('<!--'));
return body.at(-1) ?? null;
}
const oneLine = (s: string) => s.replace(/[\x00-\x1f\x7f]+/g, ' ').slice(0, 300);
/**
* The time stop: null while an observe-mode run is within 100 minutes (or already stopped or closed); past them it
* marks the ledger stopped, writes stop.json and returns the STOP block. The caller persists the ledger (mutateLedger).
*/
export function timeStop(l: Ledger, cwd: string, step: string, env: NodeJS.ProcessEnv, now = Date.now(), fallbackNext = `resume, then continue after Step ${step}`): string[] | null {
if (l.mode !== 'observe' || l.stopped || l.closed_at) return null;
const min = elapsedMin(l, now);
if (!(min > TIME_LIMIT_MIN)) return null;
const at = new Date(now).toISOString();
const g = spawnSync('git', ['rev-parse', 'HEAD'], { cwd, encoding: 'utf8', timeout: 15_000 });
const head = g.status === 0 ? g.stdout.trim() : 'unknown';
const rounds = (l.notes ?? []).filter(n => n.step === '9' && n.readers).length;
const next = nextAct(l.record) ?? fallbackNext;
const calls = callsSince(env, l.created_at, at, true);
const file = path.join(l.dir, STOP_FILE);
const state = { schema_version: 1, kind: 'gstack-ship-stop', reason: 'past_100_minutes', run: l.run, record: l.record ?? null, at,
clock_from: clockFrom(l), elapsed_min: Math.floor(min), step, head, rounds, next, calls };
fs.mkdirSync(l.dir, { recursive: true, mode: 0o700 });
fs.writeFileSync(file, JSON.stringify(state, null, 2) + '\n', { mode: 0o600 });
l.stopped = { at, reason: 'past_100_minutes', step, next };
return [
`STOP: past 100 minutes (${Math.floor(min)} min since ${clockFrom(l)}) head=${head.slice(0, 12)} rounds=${rounds} calls=${calls ?? 'unknown'} next=${oneLine(next)} state=${file}`,
`RECOVER: hand ${file} to the next session; it runs gstack-ship-budget resume --record ${l.record ?? '<record>'} (the clock restarts), then reruns the phase that stopped`,
`GSTACK_RESULT: skill=ship status=incomplete run=${l.dir}`,
];
}
+123
View File
@@ -0,0 +1,123 @@
/**
* ship-phase — one ship/DRIVER.md phase as one command (G2, handoffs/ship-conductor-cost-2026-11-16): measured
* conductors made 121-308 calls where the driver plans 9 to 12, so a phase's bins now run from one command.
* `run` executes the phase's commands in order, each in the background with its output in <dir>/phases/<phase>/,
* and prints only their result lines. A command still running when --wait-s ends (200 s) keeps running and the run
* prints `NEXT: rerun`: the same command resumes the wait. The journal keeps each finished command by the sha256 of
* its text, so a rerun after a failure or a restart starts at the first command that has not passed, and a command
* a restart killed runs again. With --record, a stopped run, or one past 100 minutes (lib/ship-clock.ts), refuses to
* start the phase and prints the STOP block.
*/
import { spawn } from 'node:child_process';
import { createHash } from 'node:crypto';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { ledgerFileFor, mutateLedger, parseRecordBlock } from './ship-budget';
import { bashCommandInvocation } from './script-invocation';
import { timeStop } from './ship-clock';
export interface PhaseEntry { sha: string; cmd: string; state: 'running' | 'done' | 'failed'; pid?: number; started: string; ended?: string; exit?: number; log: string; lines?: string[] }
export interface Journal { schema_version: 1; phase: string; entries: PhaseEntry[] }
export interface PhaseOut { stdout: string[]; exit: number }
export const WAIT_S_DEFAULT = 200;
const MAX_LINES = 60;
const RESULT_RE = /^(?:[A-Z][A-Z0-9_]*(?:\([^)]*\))?|gstack-[a-z0-9-]+(?: [a-z0-9-]+)?): /;
const sha256 = (s: string) => createHash('sha256').update(s).digest('hex');
/** The lines of a command's output that a parent acts on: `CODE: …` result lines and `gstack-<bin>: …` refusals. */
export function resultLines(text: string): string[] {
const hits = text.split('\n').map(l => l.trimEnd()).filter(l => RESULT_RE.test(l)).map(l => (l.length > 2000 ? l.slice(0, 2000) + ' …' : l));
return hits.length > MAX_LINES ? [...hits.slice(0, MAX_LINES - 1), `(${hits.length - MAX_LINES + 1} more result lines in the log)`, hits[hits.length - 1]!] : hits;
}
const alive = (pid: number | undefined) => { if (!pid) return false; try { process.kill(pid, 0); return true; } catch { return false; } };
function readJournal(file: string, phase: string): Journal {
try { const j = JSON.parse(fs.readFileSync(file, 'utf8')); if (j?.schema_version === 1 && Array.isArray(j.entries)) return j; } catch { /* a new or unreadable journal starts over */ }
return { schema_version: 1, phase, entries: [] };
}
function writeJournal(file: string, j: Journal): void {
fs.writeFileSync(`${file}.tmp`, JSON.stringify(j, null, 2) + '\n', { mode: 0o600 });
fs.renameSync(`${file}.tmp`, file);
}
/** Start one command detached: its output goes to the log and its exit status to <log>.exit when it ends, even after this process returns. */
function start(cmd: string, log: string, cwd: string, env: NodeJS.ProcessEnv): number | undefined {
try { fs.rmSync(`${log}.exit`, { force: true }); } catch { /* absent */ }
const inv = bashCommandInvocation('( eval "$GSTACK_SHIP_PHASE_CMD" ) >"$GSTACK_SHIP_PHASE_LOG" 2>&1 </dev/null; echo $? >"$GSTACK_SHIP_PHASE_LOG.exit.tmp"; mv "$GSTACK_SHIP_PHASE_LOG.exit.tmp" "$GSTACK_SHIP_PHASE_LOG.exit"');
if (!inv.ok) { fs.writeFileSync(log, `gstack-ship-phase: ${inv.error}\n`); fs.writeFileSync(`${log}.exit`, '127\n'); return undefined; }
const child = spawn(inv.cmd, inv.argv, { cwd, env: { ...env, GSTACK_SHIP_PHASE_CMD: cmd, GSTACK_SHIP_PHASE_LOG: log }, detached: true, stdio: 'ignore' });
child.unref();
return child.pid;
}
/** The run's stop check before a phase starts: a stopped run, or one past 100 minutes; null to go on (no record, or no ledger yet). */
function preflight(record: string | undefined, phase: string, cwd: string, env: NodeJS.ProcessEnv): string[] | null {
if (!record || !parseRecordBlock(record)?.ledger) return null;
return mutateLedger(ledgerFileFor({ record }, env), env, l => {
if (l.stopped) return [`STOP: run ${l.run} stopped (${l.stopped.reason}) at Step ${l.stopped.step}; nothing ran; resume first: gstack-ship-budget resume --record ${l.record}`, `GSTACK_RESULT: skill=ship status=incomplete run=${l.dir}`];
return timeStop(l, cwd, `Phase ${phase}`, env, Date.now(), `resume, then rerun Phase ${phase}`);
});
}
export interface RunOpts { dir: string; phase: string; commands: string[]; record?: string; waitS?: number; cwd: string; env: NodeJS.ProcessEnv; pollMs?: number }
/** run: the phase's commands in order, resumable from the journal; see the header. */
export async function runPhase(o: RunOpts): Promise<PhaseOut> {
const t0 = Date.now();
const deadline = t0 + (o.waitS ?? WAIT_S_DEFAULT) * 1000;
const stopped = preflight(o.record, o.phase, o.cwd, o.env);
if (stopped) return { stdout: stopped, exit: 3 };
const dir = path.join(path.resolve(o.dir), 'phases', o.phase);
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
const file = path.join(dir, 'journal.json');
const j = readJournal(file, o.phase);
const out: string[] = [];
const n = o.commands.length;
const firstChanged = j.entries.findIndex((e, i) => i >= n || e.sha !== sha256(o.commands[i]!));
if (firstChanged >= 0) {
const live = j.entries.slice(firstChanged).filter(e => e.state === 'running' && alive(e.pid));
for (const e of live) {
out.push(`REPLACED: ${j.entries.indexOf(e) + 1}/${n} pid=${e.pid} ran a command this list no longer names; stopping it (log=${e.log})`);
try { process.kill(-e.pid!, 'SIGTERM'); } catch { /* already gone */ }
}
for (let t = 0; t < 50 && live.some(e => alive(e.pid)); t++) await Bun.sleep(100);
const stuck = live.filter(e => alive(e.pid));
if (stuck.length) return { stdout: [...out, `STOP: phase ${o.phase}: replaced command pid ${stuck.map(e => e.pid).join(',')} is still running and shares its log and exit file with the new list; stop it (kill -- -<pid>), then rerun the same command`], exit: 3 };
j.entries.length = firstChanged;
}
for (let i = 0; i < n; i++) {
const cmd = o.commands[i]!;
const k = `${i + 1}/${n}`;
let e = j.entries[i];
if (e?.state === 'done') { out.push(`RAN: ${k} exit=0 (done earlier)`, ...(e.lines ?? [])); continue; }
if (!e || e.state === 'failed' || (e.state === 'running' && !alive(e.pid) && !fs.existsSync(`${e.log}.exit`))) {
const log = path.join(dir, `${i + 1}.log`);
e = j.entries[i] = { sha: sha256(cmd), cmd, state: 'running', started: new Date().toISOString(), log };
e.pid = start(cmd, log, o.cwd, o.env);
writeJournal(file, j);
}
while (!fs.existsSync(`${e.log}.exit`)) {
if (Date.now() >= deadline) {
const ran = Math.round((Date.now() - Date.parse(e.started)) / 1000);
out.push(`WAITING: ${k} pid=${e.pid ?? 'unknown'} running=${ran}s log=${e.log}`, `NEXT: rerun (the same gstack-ship-phase command resumes the wait; commands before ${i + 1} stay done)`);
return { stdout: out, exit: 0 };
}
if (!alive(e.pid) && !fs.existsSync(`${e.log}.exit`)) { await Bun.sleep(200); if (!fs.existsSync(`${e.log}.exit`)) break; }
await Bun.sleep(o.pollMs ?? 500);
}
const raw = fs.existsSync(`${e.log}.exit`) ? fs.readFileSync(`${e.log}.exit`, 'utf8').trim() : '';
const code = /^\d+$/.test(raw) ? Number(raw) : -1;
const text = fs.existsSync(e.log) ? fs.readFileSync(e.log, 'utf8') : '';
e.exit = code; e.ended = new Date().toISOString(); e.lines = resultLines(text); e.state = code === 0 ? 'done' : 'failed';
writeJournal(file, j);
out.push(`RAN: ${k} exit=${code < 0 ? 'lost' : code}${code === 0 ? '' : ` log=${e.log}`}`, ...e.lines);
if (code === 0) continue;
if (!e.lines.length) out.push(...text.trimEnd().split('\n').slice(-5).map(l => ` | ${l.slice(0, 500)}`));
out.push(`STOP: phase ${o.phase} command ${k} exited ${code < 0 ? 'without a status (killed?)' : code}; fix the cause, then rerun the same command (commands before ${i + 1} stay done)`);
return { stdout: out, exit: 3 };
}
out.push(`PHASE: ${o.phase} done commands=${n} elapsed_s=${Math.round((Date.now() - t0) / 1000)} journal=${file}`);
return { stdout: out, exit: 0 };
}
+2 -1
View File
@@ -21,7 +21,7 @@ import { stampOnlyLine, type BranchSide } from './stamp-only';
export const RECEIPT_FENCE = 'gstack-ship-receipt';
const KEY_ORDER: Array<keyof ShipReceipt | 'docs'> = [
'schema_version', 'pr', 'head', 'version', 'base', 'gated_tree', 'predecessor', 'tree', 'gate_reuse', 'gate', 'ci', 'ci_carried', 'spend_usd',
'schema_version', 'pr', 'head', 'version', 'base', 'gated_tree', 'predecessor', 'tree', 'gate_reuse', 'gate', 'ci', 'ci_carried', 'spend_usd', 'calls',
'session_kind', 'artifacts_consumed', 'run', 'thread', 'policy', 'queue_mode', 'preregistration', 'history',
'head_tree', 'product_patch_id', 'gstack_version', 'review_depth', 'pregate', 'requires_remote', 'issues', 'approval', 'docs', 'review_loop',
'review_loop_exception', 'review_round_grants', 'ruled_notes', 'gate_dispositions', 'mechanical', 'review_targeted', 'design', 'review_exclude', 'carry', 'base_red', 'flaky_exception', 'no_stop',
@@ -200,6 +200,7 @@ export function receiptSummaryLine(r: ShipReceipt): string {
if (r.predecessor) parts.push(`predecessor=${r.predecessor.slice(0, 12)}`);
if (r.session_kind) parts.push(`session_kind=${r.session_kind}`);
if (r.artifacts_consumed) parts.push(`artifacts_consumed=${r.artifacts_consumed}`);
if (typeof r.calls === 'number') parts.push(`calls=${r.calls}`);
if (r.review_depth && typeof r.review_depth.depth === 'string') parts.push(`depth=${r.review_depth.depth}`);
if (r.approval) parts.push(`approval=${approvalSummary(r.approval)}`);
const loop = parseReviewLoop(r.review_loop);