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.7 KB
Raw Blame
#!/usr/bin/env node
/**
 * Loop Sync CLI
 * 
 * Detect and sync drift between Loop configuration files
 */

import { runSync, formatReport, type SyncOptions, type DriftReport } from './sync.js';

function parseArgs(argv: string[]): SyncOptions {
  let targetDir = '.';
  let autoFix = false;
  let dryRun = false;
  let verbose = false;
  let json = false;
  let help = false;

  for (let i = 0; i < argv.length; i++) {
    const a = argv[i];
    if (a === '--help' || a === '-h') help = true;
    else if (a === '--auto-fix' || a === '-a') autoFix = true;
    else if (a === '--dry-run' || a === '-d') dryRun = true;
    else if (a === '--verbose' || a === '-v') verbose = true;
    else if (a === '--json') json = true;
    else if (!a.startsWith('-')) targetDir = a;
  }

  return { targetDir, autoFix, dryRun, verbose, help, json };
}

async function main() {
  const args = parseArgs(process.argv.slice(2));

  if (args.help) {
    console.log(`loop-sync — detect and sync drift between Loop configuration files

Usage:
  loop-sync [target-dir] [options]

Options:
  -a, --auto-fix    Scaffold missing STATE.md, gate.yaml, loop-budget.md, and
                     loop-run-log.md with minimal defaults, and add a missing
                     STATE.md reference to LOOP.md. LOOP.md and AGENTS.md are
                     never fabricated -- their content is pattern-specific,
                     so a missing one still just suggests loop-init.
  -d, --dry-run     With --auto-fix, report what would be scaffolded/changed
                     without writing anything
  -v, --verbose     Show detailed information
  --json            Output JSON format
  -h, --help        Show this help

Examples:
  loop-sync .
  loop-sync ./my-project -v
  loop-sync ./my-project --json
  loop-sync ./my-project --auto-fix --dry-run
  loop-sync ./my-project --auto-fix

The tool checks:
  - STATE.md ↔ LOOP.md consistency
  - Required files (STATE.md, LOOP.md, AGENTS.md)
  - Skills directory structure
  - Configuration drift indicators

Score interpretation:
  - 90-100: Healthy (no issues)
  - 70-89: Warning (minor inconsistencies)
  - 0-69: Critical (major issues need attention)

Docs: https://github.com/cobusgreyling/loop-engineering/tree/main/tools/loop-sync
`);
    process.exit(0);
  }

  try {
    const report = await runSync(args);

    if (args.json) {
      console.log(JSON.stringify(report, null, 2));
    } else {
      console.log(formatReport(report));
    }

    // Exit with appropriate code
    if (report.level === 'critical') {
      process.exit(1);
    } else if (report.level === 'warning') {
      process.exit(2);
    }
  } catch (error) {
    console.error('loop-sync failed:', error instanceof Error ? error.message : error);
    process.exit(1);
  }
}

main();