Files
gstack/lib/ship-clock.ts
T
Garry Tanandcapy-ai[bot] 37cad2b526 /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
2026-11-28 18:47:13 +00:00

130 lines
8.6 KiB
TypeScript

/**
* 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 active 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 active 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.
* Active time (GST-4109) is wall time minus parked time: any stretch over PARKED_AFTER_MS with no gstack call in the call
* log is the lane parked on CI, a Captain or a ruling, so only its first PARKED_AFTER_MS count. A run that keeps calling
* gstack (a runaway pass or phase loop) never parks, so it still stops at 100 minutes; with no call log (off Capy) the
* clock is wall time.
* 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';
/** The call log's idle line (lib/ship-call-log.ts, a gap that outlives the prompt cache): a longer stretch with no gstack call is parked past it. */
export const PARKED_AFTER_MS = 300_000;
export const clockFrom = (l: Pick<Ledger, 'created_at' | 'clock_from'>) => l.clock_from ?? l.created_at;
/** The canonical git toplevel of `cwd` (the form a ledger's checkout records), or '' where git cannot resolve one (a deleted directory, no checkout). */
export function gitToplevel(cwd: string): string {
const r = spawnSync('git', ['rev-parse', '--show-toplevel'], { cwd, encoding: 'utf8', timeout: 10_000 });
return r.status === 0 ? r.stdout.trim() : '';
}
/**
* The run's clock in minutes since clockFrom: wall, parked (each stretch between call-log rows, or from the clock's start to the first
* row and from the last row to now, past PARKED_AFTER_MS) and active (wall minus parked), which the 100-minute stop compares. A row
* counts when it ran in the run's checkout, lasting as long as it ran, and so does this process's own pending row (a row from before rows
* carried a cwd counts too). In the checkout means the row's cwd has the checkout as its git toplevel (GST-4132): a plain subdirectory
* counts, a nested checkout or submodule under it does not; a cwd git cannot resolve (since deleted) falls back to the path prefix. With no row in the window (no call log: off Capy) the clock is wall time and source is wall.
*/
export function runClock(l: Pick<Ledger, 'created_at' | 'clock_from' | 'checkout'>, env: NodeJS.ProcessEnv, now = Date.now()): { active: number; wall: number; parked: number; source: 'log' | 'wall' } {
const from = Date.parse(clockFrom(l));
const wall = Math.max(0, now - from);
const self = pendingRow();
const tops = new Map<string, string>();
const here = (cwd: string | undefined) => {
if (!cwd || !l.checkout || cwd === l.checkout) return true;
if (!tops.has(cwd)) tops.set(cwd, gitToplevel(cwd));
const top = tops.get(cwd)!;
return top ? top === l.checkout : cwd.startsWith(l.checkout + path.sep);
};
const spans = [...readCallLog(callLogFile(env)), ...(self ? [self] : [])]
.map(r => ({ cwd: r.cwd, s: Date.parse(r.start), e: Date.parse(r.start) + Math.max(0, r.ms) }))
.filter(r => r.e >= from && r.s <= now && here(r.cwd)).map(r => [Math.max(r.s, from), Math.min(r.e, now)] as const).sort((a, b) => a[0] - b[0]);
if (!spans.length) return { active: wall / 60_000, wall: wall / 60_000, parked: 0, source: 'wall' };
let parked = 0, cursor = from;
for (const [s, e] of [...spans, [now, now] as const]) {
parked += Math.max(0, s - cursor - PARKED_AFTER_MS);
cursor = Math.max(cursor, e);
}
return { active: (wall - parked) / 60_000, wall: wall / 60_000, parked: parked / 60_000, source: 'log' };
}
/**
* 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 active minutes (runClock; 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 clock = runClock(l, env, now);
const min = clock.active;
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(clock.wall), active_min: Math.floor(min), parked_min: Math.floor(clock.parked), clock_source: clock.source, 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)} active min since ${clockFrom(l)}, ${Math.floor(clock.parked)} min parked) 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}`,
];
}