/** * 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 * /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) => 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, 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(); 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('