From 0ee5914628eb0373876bfc9c511a48178a6700fa Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Wed, 18 Nov 2026 00:44:06 +0800 Subject: [PATCH] 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 --- bin/gstack-ship-phase | 60 +++++ bin/gstack-ship-receipt | 14 +- capy/skill/gstack/SKILL.md | 2 +- changelog.d/capy-run-each-driven-ship.md | 1 + docs/ship-budget.md | 51 +++- lib/capy-review-pass.js | 2 +- lib/capy-ship-phase.js | 69 +++++ lib/headless-artifacts.ts | 4 +- lib/ship-budget-cli.ts | 18 +- lib/ship-budget.ts | 4 +- lib/ship-call-log.ts | 7 + lib/ship-clock.ts | 86 ++++++ lib/ship-phase.ts | 123 +++++++++ lib/ship-receipt.ts | 3 +- ship/DRIVER.md | 34 +-- ship/DRIVER.md.tmpl | 34 +-- test/exercised-map.test.ts | 1 + test/review-pass.test.ts | 13 + test/ship-phase.test.ts | 330 +++++++++++++++++++++++ 19 files changed, 807 insertions(+), 49 deletions(-) create mode 100755 bin/gstack-ship-phase create mode 100644 changelog.d/capy-run-each-driven-ship.md create mode 100644 lib/capy-ship-phase.js create mode 100644 lib/ship-clock.ts create mode 100644 lib/ship-phase.ts create mode 100644 test/ship-phase.test.ts diff --git a/bin/gstack-ship-phase b/bin/gstack-ship-phase new file mode 100755 index 000000000..802e55fe4 --- /dev/null +++ b/bin/gstack-ship-phase @@ -0,0 +1,60 @@ +#!/usr/bin/env -S bun --no-env-file --config=/dev/null +/** + * gstack-ship-phase — one ship/DRIVER.md phase as one call (G2). + * + * gstack-ship-phase run --dir --phase [--record ] [--wait-s ] -- '' ['' ...] + * Runs the phase's commands in order, each in the background (output in /phases//.log), and prints + * only result lines: RAN: / exit= with each command's CODE: lines, then PHASE: done. A command still + * running after --wait-s (default 200) keeps running and the run prints WAITING: and NEXT: rerun; rerun the same + * command to resume. A failed command prints STOP: (exit 3); fix it and rerun the same command: finished commands + * stay done (the journal keys each by the sha256 of its text). With --record, a stopped run or one past 100 + * minutes prints STOP: and runs nothing (exit 3); past 100 minutes it also writes /stop.json. + * gstack-ship-phase program + * Prints the checked-in Capy phase program (lib/capy-ship-phase.js): PROGRAM: v sha256= bytes=, then its + * text as one JSON string. Load it the way capy/skill/gstack/SKILL.md loads the review pass program. + * + * Exit: 0 done or waiting · 3 stop · 2 usage. Logic: lib/ship-phase.ts, lib/ship-clock.ts. + */ +import { createHash } from 'node:crypto'; +import * as fs from 'node:fs'; +import * as path from 'node:path'; +import { runPhase } from '../lib/ship-phase'; +import { logShipCall } from '../lib/ship-call-log'; + +logShipCall('gstack-ship-phase'); + +const PROGRAM_VERSION = 1; +const [command, ...rest] = process.argv.slice(2); +const usage = (why: string) => { process.stderr.write(`gstack-ship-phase: ${why} (usage; see gstack-ship-phase --help)\n`); process.exit(2); }; + +if (!command || command === '--help' || command === '-h') { + const lines = (await Bun.file(import.meta.path).text()).split('\n'); + (command ? console.log : console.error)(lines.slice(2, lines.indexOf(' */')).map(l => l.replace(/^ \* ?/, '')).join('\n')); + process.exit(command ? 0 : 2); +} +if (command === 'program') { + const text = fs.readFileSync(path.join(import.meta.dir, '..', 'lib', 'capy-ship-phase.js'), 'utf8'); + const sha = createHash('sha256').update(text).digest('hex'); + process.stdout.write(`PROGRAM: v${PROGRAM_VERSION} sha256=${sha} bytes=${Buffer.byteLength(text)}\n${JSON.stringify(text)}\n`); + process.exit(0); +} +if (command !== 'run') usage(`unknown command '${command}'`); +const sep = rest.indexOf('--'); +const opts = sep < 0 ? rest : rest.slice(0, sep); +const commands = sep < 0 ? [] : rest.slice(sep + 1); +const flag = (name: string) => { const i = opts.indexOf(`--${name}`); return i >= 0 ? opts[i + 1] : undefined; }; +const known = new Set(['--dir', '--phase', '--record', '--wait-s']); +for (let i = 0; i < opts.length; i += 2) if (!known.has(opts[i]!) || opts[i + 1] === undefined) usage(`run: unexpected '${opts[i]}'`); +const dir = flag('dir'), phase = flag('phase'), waitRaw = flag('wait-s'); +if (!dir) usage('run needs --dir '); +if (!phase || !/^[A-Za-z0-9._-]+$/.test(phase)) usage('run needs --phase (letters, digits, dot, underscore or dash)'); +if (!commands.length || commands.some(c => !c.trim())) usage("run needs the phase's commands after --, one argument each"); +if (waitRaw !== undefined && !/^\d+$/.test(waitRaw)) usage(`--wait-s '${waitRaw}' is not a whole number of seconds`); +try { + const out = await runPhase({ dir: dir!, phase: phase!, commands, record: flag('record'), waitS: waitRaw === undefined ? undefined : Number(waitRaw), cwd: process.cwd(), env: process.env }); + if (out.stdout.length) process.stdout.write(out.stdout.join('\n') + '\n'); + process.exit(out.exit); +} catch (e: any) { + process.stderr.write(`gstack-ship-phase: ${e?.message ?? e}\n`); + process.exit(e?.exit === 3 ? 3 : 2); +} diff --git a/bin/gstack-ship-receipt b/bin/gstack-ship-receipt index 6d8b048e0..607dd3d00 100755 --- a/bin/gstack-ship-receipt +++ b/bin/gstack-ship-receipt @@ -3,7 +3,7 @@ * gstack-ship-receipt — the handoff record, identifiers first (plan C5). * * gstack-ship-receipt write [--pr ] [--head ] [--base ] [--version ] [--gated-tree ] - * [--predecessor ] [--gate ] [--ci ] [--spend-usd ] + * [--predecessor ] [--gate ] [--ci ] [--spend-usd ] [--calls | --budget-record ] * [--session-kind ] [--artifacts-consumed /] [--run ] [--thread ] * [--receipt ] [--policy