From f6835fc16b738dd87ca31610ca7bae2285724ae7 Mon Sep 17 00:00:00 2001 From: garrytan Date: Fri, 16 Oct 2026 23:18:15 +0000 Subject: [PATCH] feat(approval-classes): gstack-approval-classes and ship-receipt citation enforcement (W2, WIP) Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> (cherry picked from commit 847c7dae3dd5e49316733497a7b607869d51c28f) --- bin/gstack-approval-classes | 83 ++++++++++++ bin/gstack-ship-receipt | 34 ++++- docs/troubleshooting.md | 65 ++++++++++ lib/approval-citations.ts | 204 +++++++++++++++++++++++++++++ lib/approval-classes.ts | 249 ++++++++++++++++++++++++++++++++++++ lib/headless-artifacts.ts | 3 + lib/result-codes.ts | 26 ++++ lib/ship-receipt.ts | 4 +- 8 files changed, 662 insertions(+), 6 deletions(-) create mode 100755 bin/gstack-approval-classes create mode 100644 lib/approval-citations.ts create mode 100644 lib/approval-classes.ts diff --git a/bin/gstack-approval-classes b/bin/gstack-approval-classes new file mode 100755 index 000000000..0ebf2b34e --- /dev/null +++ b/bin/gstack-approval-classes @@ -0,0 +1,83 @@ +#!/usr/bin/env bun +/** + * gstack-approval-classes — changes that need the owner's approval (field list 2, W2). + * + * gstack-approval-classes [check] [--base ] [--head ] [--diff-base ] [--json] + * gstack-approval-classes init [--force] + * gstack-approval-classes validate + * + * `check` reads the classes from the committed `origin/` copy of + * .gstack/approval-classes.json (never the working tree) and the migration + * rule from the base's ship policy, scans the branch's changes since the + * merge base, and prints `APPROVAL_CLASS: :` per hit, one + * `APPROVAL_SCOPE: @` per declared class with hits, and + * `APPROVAL_CLASSES: ok | found= | none` last. Built-in detectors only + * report. Exit 0 ok or none, 1 found, 3 when the base file is invalid or the + * base is unresolvable. Enforcement is gstack-ship-receipt write + * (APPROVAL_CITATION_MISSING). Logic: lib/approval-classes.ts. + */ +import { spawnSync } from 'node:child_process'; +import * as fs from 'node:fs'; +import * as path from 'node:path'; +import { APPROVAL_CLASSES_PATH, exampleApprovalClassesJson, loadWorkingApprovalClasses, renderApprovalScan, runApprovalScan } from '../lib/approval-classes'; +import { EXIT, exitTableHelp } from '../lib/headless-artifacts'; +import { resultLine } from '../lib/result-codes'; +import { resolveBaseBranch } from '../lib/ship-policy'; + +const HELP = `gstack-approval-classes — changes that need the owner's approval (docs/ship-policy.md#approval-classes) + + check [--base ] [--head ] [--diff-base ] [--json] + changes since the merge base with origin/ (default origin/HEAD's branch), at --head or the + working tree; classes come only from the committed ${APPROVAL_CLASSES_PATH} on origin/ + init [--force] write an example ${APPROVAL_CLASSES_PATH} + validate schema and containment check of the working-tree file (before you commit it) + +A declared class with hits needs a line in the PR body that gstack-ship-receipt write can resolve: + Approved by via [for @] + +${exitTableHelp()}`; + +function flag(args: string[], name: string): string | undefined { + const i = args.indexOf(name); + return i >= 0 ? args[i + 1] : undefined; +} + +function main(argv: string[]): number { + if (argv.includes('--help') || argv.includes('-h')) { process.stdout.write(HELP + '\n'); return EXIT.ok; } + const cmd = argv[0] && !argv[0].startsWith('--') ? argv[0] : 'check'; + const rest = cmd === argv[0] ? argv.slice(1) : argv; + const root = spawnSync('git', ['rev-parse', '--show-toplevel'], { encoding: 'utf8', timeout: 15_000 }); + if (root.status !== 0) { process.stderr.write('gstack-approval-classes: not inside a git repository\n'); return EXIT.usage; } + const repoRoot = root.stdout.trim(); + const file = path.join(repoRoot, APPROVAL_CLASSES_PATH); + + if (cmd === 'init') { + if (fs.existsSync(file) && !rest.includes('--force')) { process.stderr.write(`gstack-approval-classes: ${APPROVAL_CLASSES_PATH} exists (pass --force to overwrite)\n`); return EXIT.refused; } + fs.mkdirSync(path.dirname(file), { recursive: true }); + fs.writeFileSync(file, exampleApprovalClassesJson()); + process.stdout.write(`APPROVAL_CLASSES_INIT: ${APPROVAL_CLASSES_PATH} (edit, commit and merge it; check reads only the base copy)\n`); + return EXIT.ok; + } + if (cmd === 'validate') { + const load = loadWorkingApprovalClasses(repoRoot); + if ('absent' in load) { process.stdout.write(`APPROVAL_CLASSES: none (no ${APPROVAL_CLASSES_PATH})\n`); return EXIT.ok; } + if ('errors' in load) { for (const e of load.errors) process.stderr.write(resultLine('gstack-approval-classes validate', 'APPROVAL_CLASSES_INVALID', `${e.message} at ${e.path || ''}`) + '\n'); return EXIT.refused; } + if ('unavailable' in load || !load.config) return EXIT.fail; + process.stdout.write(`APPROVAL_CLASSES_VALID: ${APPROVAL_CLASSES_PATH} classes=${load.config.classes.length}\n`); + return EXIT.ok; + } + if (cmd !== 'check') { process.stderr.write(`gstack-approval-classes: unknown command ${cmd}\n${HELP}\n`); return EXIT.usage; } + + const json = rest.includes('--json'); + const base = resolveBaseBranch(repoRoot, flag(rest, '--base')); + const outcome = runApprovalScan({ repoRoot, base, head: flag(rest, '--head'), diffBase: flag(rest, '--diff-base') }); + if ('unavailable' in outcome) { process.stderr.write(resultLine('gstack-approval-classes', 'APPROVAL_SOURCE_UNAVAILABLE', `${outcome.unavailable}; classes are read only from the base, so nothing was checked`) + '\n'); return EXIT.refused; } + if ('errors' in outcome) { for (const e of outcome.errors) process.stderr.write(resultLine('gstack-approval-classes', 'APPROVAL_CLASSES_INVALID', `${e.message} at ${e.path || ''} (${outcome.source})`) + '\n'); return EXIT.refused; } + if ('error' in outcome) { process.stderr.write(`gstack-approval-classes: ${outcome.error}\n`); return EXIT.usage; } + const { scan, absent } = outcome; + process.stdout.write(json ? JSON.stringify({ ...scan, absent }) + '\n' : renderApprovalScan(scan, absent)); + if (scan.hits.length) { process.stderr.write(resultLine('gstack-approval-classes', 'APPROVAL_CLASS_FOUND', `${scan.scopes.map(s => s.token).join(', ')} need an Approved by line`) + '\n'); return EXIT.fail; } + return EXIT.ok; +} + +process.exitCode = main(process.argv.slice(2)); diff --git a/bin/gstack-ship-receipt b/bin/gstack-ship-receipt index f8441f1a7..e74a64a48 100755 --- a/bin/gstack-ship-receipt +++ b/bin/gstack-ship-receipt @@ -6,7 +6,8 @@ * [--predecessor ] [--gate ] [--ci ] [--spend-usd ] * [--session-kind ] [--artifacts-consumed /] [--run ] [--thread ] * [--receipt ] [--policy