/** * 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 * /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) => l.clock_from ?? l.created_at; export const elapsedMin = (l: Pick, 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('