Skip to content

Practical patterns, starters & CLI tools for loop engineering with AI coding agents. Design systems that prompt and orchestrate agents (inspired by Addy Osmani and Boris Cherny). Includes loop-audit, loop-init, loop-cost.

2.8 KB
Raw Blame
#!/usr/bin/env node
import {
  checkGate,
  loadGateConfig,
  assertValidAction,
  type Action,
} from './gate.js';

interface Flags {
  help: boolean;
  gateFile: string;
  action?: string;
  paths?: string;
  json: boolean;
}

function parseFlags(argv: string[]): Flags {
  const flags: Flags = { help: false, gateFile: 'gate.yaml', json: false };
  for (let i = 0; i < argv.length; i++) {
    const a = argv[i];
    if (a === '--help' || a === '-h') flags.help = true;
    else if (a === '--action') flags.action = argv[++i];
    else if (a === '--paths') flags.paths = argv[++i];
    else if (a === '--gate-file') flags.gateFile = argv[++i];
    else if (a === '--json') flags.json = true;
  }
  return flags;
}

const HELP = `loop-gate — mechanical enforcement of static safety policy

Evaluates a proposed change (action type + changed paths) against
gate.yaml's denylist, max-files, and auto-merge allowlist. No knowledge of
run history — pair with loop-context --check for stagnation/budget triggers.

Usage:
  loop-gate check --action <commit|merge|auto-merge> --paths <f1,f2,...> [options]

Options:
  --action <commit|merge|auto-merge>   What the loop is about to do
  --paths <f1,f2,...>                  Comma-separated changed file paths
  --gate-file <path>                   Policy file (default: gate.yaml)
  --json                               Machine-readable output
  -h, --help                           This help

Exit codes: 0 allowed · 2 escalate · 1 error

Example:
  loop-gate check --action auto-merge --paths $(git diff --name-only | tr '\\n' ',')
`;

async function main(): Promise<number> {
  const argv = process.argv.slice(2);
  const command = argv[0];

  if (!command || command === '--help' || command === '-h') {
    console.log(HELP);
    return command ? 0 : 1;
  }
  if (command !== 'check') {
    console.error(`Unknown command "${command}".\n\n${HELP}`);
    return 1;
  }

  const flags = parseFlags(argv.slice(1));
  if (flags.help) {
    console.log(HELP);
    return 0;
  }
  if (!flags.action || !flags.paths) {
    throw new Error('check requires --action and --paths.');
  }
  assertValidAction(flags.action);

  const config = await loadGateConfig(flags.gateFile);
  const paths = flags.paths.split(',').map((p) => p.trim()).filter(Boolean);
  if (paths.length === 0) {
    throw new Error('--paths must contain at least one non-empty path.');
  }
  const decision = checkGate({ config, action: flags.action as Action, paths });

  if (flags.json) {
    console.log(JSON.stringify(decision, null, 2));
  } else {
    console.log(`${decision.allowed ? 'ALLOWED' : 'ESCALATE'} [${decision.trigger}] — ${decision.reason}`);
  }
  return decision.allowed ? 0 : 2;
}

main()
  .then((code) => process.exit(code))
  .catch((err) => {
    console.error(err instanceof Error ? err.message : String(err));
    process.exit(1);
  });