syntaxai/tdd.md · main · scripts / measure-working-set.ts

measure-working-set.ts 77 lines · 2115 bytes raw
#!/usr/bin/env bun
// measure-working-set — CLI for the §5 polyglot workingSetFit metric.
// Given a path to a checked-out Go or Rust source tree, emit the
// measured ratio as JSON to stdout.
//
// Usage:
//   bun scripts/measure-working-set.ts <repo-path> --lang go
//   bun scripts/measure-working-set.ts <repo-path> --lang rust
//
// The number it emits is reproducible: given the same checked-out
// source tree, every run prints the same ratio to full float precision.
// Pair the output with the repo's commit SHA when reporting; see
// /sama/v2 §5 (operational) for the bounds reasoning.

import { measureWorkingSetForRepo } from "../src/c14_working_set_walker.ts";
import type { PolyglotLanguage } from "../src/b32_working_set_polyglot.ts";

const args = process.argv.slice(2);

const usage = (): never => {
  console.error(
    "Usage: bun scripts/measure-working-set.ts <repo-path> --lang go|rust [--verbose]",
  );
  process.exit(2);
};

if (args.length < 3) usage();

const repoPath = args[0]!;
let lang: PolyglotLanguage | null = null;
let verbose = false;

for (let i = 1; i < args.length; i++) {
  const a = args[i];
  if (a === "--lang") {
    const v = args[++i];
    if (v !== "go" && v !== "rust") {
      console.error(`--lang must be "go" or "rust", got: ${v}`);
      process.exit(2);
    }
    lang = v;
  } else if (a === "--verbose") {
    verbose = true;
  } else {
    console.error(`unknown argument: ${a}`);
    usage();
  }
}

if (lang === null) usage();

const result = measureWorkingSetForRepo(repoPath, lang!);

const output: Record<string, unknown> = {
  language: result.language,
  repoPath,
  minLoc: result.minLoc,
  maxLoc: result.maxLoc,
  total: result.total,
  included: result.included,
  ratio: result.ratio,
  ratioPercent: Number((result.ratio * 100).toFixed(2)),
};

if (verbose) {
  output.files = result.files.map((f) => ({
    path: f.path,
    locCount: f.locCount,
    inBand:
      f.locCount >= result.minLoc &&
      f.locCount <= result.maxLoc &&
      !(lang === "go" && f.path.endsWith("_test.go")),
  }));
}

console.log(JSON.stringify(output, null, 2));