Files
gstack/bin/gstack-ship-policy
T
Garry Tanandcapy-ai[bot] 7552362196 v1.91.73.0 feat(ship): repo ship policy, gstack-restamp stamp-at-merge, tree receipt, evidence bundle, ship receipt (multi-agent wave C-stamp) (#3118)
* v1.91.70.0 feat(unattended): artifact contract, unattended session kind, findings-derived review log, gate list

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* v1.91.73.0 feat(ship): repo ship policy, gstack-restamp stamp-at-merge, tree receipt, evidence bundle, ship receipt

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(ci): result-codes is selection data, receipt counts come from files, C-stamp tests leave the Windows subset

- scripts/pr-dependencies.ts: lib/result-codes.ts joins SELECTION_DATA_MODULES; its fix: prose names commands a person reruns, not files the module consumes (test/pr-dependencies.test.ts traced review cases through it to bin/gstack-next-version)
- ship/sections/pr-body.md.tmpl: gstack-ship-receipt write reads --gate/--ci from agent-written .gstack/tmp/ship-*.json files and --spend-usd from the allowlisted <spend-usd> placeholder (generated-bash lint: no free text in commands)
- scripts/lib/windows-curation.ts: the five C-stamp test files spawn bin/ shebang scripts through test/helpers/restamp-fixture.ts, which the source scan misses

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(ship): the receipt step names where each input comes from

The workflow judge (eval-slices 3, median 3/4) found pr-body's gstack-ship-receipt command demanding a tree receipt, CI counts and a spend figure no earlier step produces, with RECEIPT_INVALID halting publication. The command now takes only Step 16 stage 4's lane counts (file-written JSON); --receipt is added only when Step 16 wrote .gstack/tmp/tree-receipt.json; RECEIPT_INVALID says to fix the named input file and rerun. The unused <spend-usd> allowlist entry is removed.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
2026-10-11 01:01:42 +00:00

141 lines
7.6 KiB
TypeScript
Executable File

#!/usr/bin/env bun
/**
* gstack-ship-policy — read, validate and explain `.gstack/ship-policy.json` (plan C1).
*
* gstack-ship-policy init [--explain] [--force] write the example (every key at its default)
* gstack-ship-policy validate [--policy-from head|base] [--base <ref>] [--json]
* gstack-ship-policy show-effective [--policy-from base|head] [--base <ref>] [--json]
* gstack-ship-policy check-history [--base <ref>] [--branch <name>] [--plan merge|squash|rebase] [--json]
*
* `show-effective` reads the committed copy on origin/<base> by default (the
* reviewed policy); `validate` reads the working tree by default (the file
* you are editing). Query commands: no GSTACK_RESULT line; `--json` means
* JSON only on stdout. Logic: lib/ship-policy.ts; codes: lib/result-codes.ts.
*/
import { spawnSync } from 'node:child_process';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { EXIT, exitTableHelp } from '../lib/headless-artifacts';
import { resultLine } from '../lib/result-codes';
import {
SHIP_POLICY_PATH, examplePolicyJson, explainPolicy, isUnavailable, loadPolicyFromBase, loadPolicyFromHead,
policyUnavailableLine, renderEffective, resolveBaseBranch, type LoadedPolicy,
} from '../lib/ship-policy';
const HELP = `gstack-ship-policy — the repo ship policy (docs/ship-policy.md)
init [--explain] [--force] write ${SHIP_POLICY_PATH} with every key at its default
validate [--policy-from head|base] [--base <ref>] schema and containment check (default: the working tree)
show-effective [--policy-from base|head] [--base <ref>]
resolved values with the source of each (default: origin/<base>)
check-history [--base <ref>] [--branch <name>] [--plan merge|squash|rebase]
under history: merge-only, refuse a squash/rebase plan or a rewritten branch
--json: JSON only on stdout. Error lines end in (<CODE>) with a fix: clause.
${exitTableHelp()}`;
function flag(args: string[], name: string): string | undefined {
const i = args.indexOf(name);
return i >= 0 ? args[i + 1] : undefined;
}
function usage(message: string): number {
process.stderr.write(`gstack-ship-policy: ${message}\n${HELP}\n`);
return EXIT.usage;
}
function repoRoot(): string {
const r = spawnSync('git', ['rev-parse', '--show-toplevel'], { encoding: 'utf8', timeout: 15_000 });
return r.status === 0 && r.stdout.trim() ? r.stdout.trim() : process.cwd();
}
function load(root: string, args: string[], defaultFrom: 'base' | 'head'): LoadedPolicy | number {
const from = flag(args, '--policy-from') ?? defaultFrom;
if (from !== 'base' && from !== 'head') return usage('--policy-from needs base or head');
if (from === 'head') return loadPolicyFromHead(root);
const base = resolveBaseBranch(root, flag(args, '--base'));
const loaded = loadPolicyFromBase(root, base);
if (isUnavailable(loaded)) {
process.stderr.write(policyUnavailableLine('gstack-ship-policy', base, loaded.detail) + '\n');
return EXIT.refused;
}
return loaded;
}
function printErrors(loaded: LoadedPolicy): void {
for (const e of loaded.errors) process.stderr.write(resultLine('gstack-ship-policy', e.code, `${e.message} at ${e.path || SHIP_POLICY_PATH}`) + '\n');
}
function main(argv: string[]): number {
const [cmd, ...rest] = argv;
const json = rest.includes('--json');
if (!cmd || cmd === '--help' || cmd === '-h' || cmd === 'help') { process.stdout.write(HELP + '\n'); return cmd ? EXIT.ok : EXIT.usage; }
const root = repoRoot();
if (cmd === 'init') {
if (rest.includes('--explain')) { process.stdout.write(explainPolicy()); if (rest.length === 1) return EXIT.ok; }
const file = path.join(root, SHIP_POLICY_PATH);
if (fs.existsSync(file) && !rest.includes('--force')) {
process.stderr.write(resultLine('gstack-ship-policy init', 'POLICY_EXISTS', file) + '\n');
return EXIT.refused;
}
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, examplePolicyJson());
process.stdout.write(`POLICY_WRITTEN: ${SHIP_POLICY_PATH} (edit it, then gstack-ship-policy validate; commit it on the base branch before /ship reads it)\n`);
return EXIT.ok;
}
if (cmd === 'validate' || cmd === 'show-effective') {
const loaded = load(root, rest, cmd === 'validate' ? 'head' : 'base');
if (typeof loaded === 'number') return loaded;
if (json) {
process.stdout.write(JSON.stringify({ valid: loaded.errors.length === 0, present: loaded.present, source: loaded.source, policy: loaded.policy, values: loaded.values, errors: loaded.errors }) + '\n');
return loaded.errors.length ? EXIT.fail : EXIT.ok;
}
printErrors(loaded);
if (cmd === 'validate') {
process.stdout.write(loaded.errors.length ? `POLICY_INVALID: ${loaded.errors.length} error(s) in ${loaded.source.label}\n` : `POLICY_VALID: ${loaded.present ? loaded.source.label : `no ${SHIP_POLICY_PATH} (defaults apply)`}\n`);
return loaded.errors.length ? EXIT.fail : EXIT.ok;
}
process.stdout.write(renderEffective(loaded, { base: flag(rest, '--base') ?? resolveBaseBranch(root) }));
return loaded.errors.length ? EXIT.fail : EXIT.ok;
}
if (cmd === 'check-history') {
const loaded = load(root, rest, 'base');
if (typeof loaded === 'number') return loaded;
const branch = flag(rest, '--branch') ?? (spawnSync('git', ['branch', '--show-current'], { cwd: root, encoding: 'utf8', timeout: 15_000 }).stdout ?? '').trim();
const policy = loaded.policy.history;
const plan = flag(rest, '--plan') ?? 'merge';
if (!['merge', 'squash', 'rebase'].includes(plan)) return usage(`--plan must be merge, squash or rebase (got ${plan})`);
const result: Record<string, unknown> = { history: policy, branch: branch || null, plan };
let verdict: string;
let code = EXIT.ok;
if (policy !== 'merge-only') verdict = `HISTORY: squash-ok (no check; policy history=${policy}; plan=${plan})`;
else if (plan !== 'merge') {
verdict = `HISTORY: refused (plan=${plan} rewrites history; policy history=merge-only keeps every reviewed commit reachable so gate evidence and preregistration ancestry stay bound to the shas that were gated) — fix: integrate with a merge commit, or set history: squash-ok in .gstack/ship-policy.json on the base branch (HISTORY_POLICY_VIOLATION)`;
code = EXIT.refused;
}
else if (!branch) { verdict = 'HISTORY: merge-only (detached HEAD; no pushed branch to compare)'; }
else {
const remote = spawnSync('git', ['rev-parse', '--verify', '--quiet', `refs/remotes/origin/${branch}`], { cwd: root, encoding: 'utf8', timeout: 15_000 });
if (remote.status !== 0) verdict = `HISTORY: merge-only (origin/${branch} not pushed yet; nothing to rewrite)`;
else {
const anc = spawnSync('git', ['merge-base', '--is-ancestor', remote.stdout.trim(), 'HEAD'], { cwd: root, timeout: 15_000 });
result.remote = remote.stdout.trim();
if (anc.status === 0) verdict = `HISTORY: ok (merge-only; origin/${branch} is an ancestor of HEAD)`;
else {
verdict = `HISTORY: rewritten (origin/${branch} ${remote.stdout.trim().slice(0, 12)} is not an ancestor of HEAD) — fix: ${'merge origin/<base> instead of rebasing or squashing; never force-push under history: merge-only'} (HISTORY_POLICY_VIOLATION)`;
code = EXIT.fail;
}
}
}
result.ok = code === EXIT.ok;
result.verdict = verdict;
process.stdout.write(json ? JSON.stringify(result) + '\n' : verdict + '\n');
return code;
}
return usage(`unknown command ${cmd}`);
}
process.exitCode = main(process.argv.slice(2));