Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

5.1 KB
Raw Blame
/** Committed rule coverage is derived on the event writer, never by status or API readers [S1,C7]. */
import { createHash } from 'node:crypto';
import { readFileSync } from 'node:fs';
import { tryGit } from './git.js';
import { literalShellWords } from './literal-shell-words.js';
const FILE = /^[A-Za-z0-9_.-]+(?:\/[A-Za-z0-9_.-]+)*\.(?:[cm]?js|cts)$/u;

/** Bind cached interpretation to installed parser and canonical runner bytes, independent of reader runtime. */
export function proofCoverageFingerprint() {
  const hash = createHash('sha256').update('rule-coverage-source-v2');
  for (const name of ['./rule-proof-coverage.js', './literal-shell-words.js', '../bin/run-tests.js']) {
    hash.update(name).update(readFileSync(new URL(name, import.meta.url)));
  }
  return hash.digest('hex');
}

/** Parse one literal Node test selection without pretending filtered, sharded or unknown options ran the file. */
function selectedFiles(args, tracked, canonical) {
  const files = [];
  for (let at = 0; at < args.length; at += 1) {
    const arg = args[at];
    if (/^--test-concurrency=\d+$/u.test(arg)) continue;
    if (arg === '--test-concurrency') { if (!/^\d+$/u.test(args[++at] ?? '')) return []; continue; }
    if (arg.startsWith('-')) return [];
    const file = arg.replace(/^\.\//u, '');
    if (!FILE.test(file) || !tracked.has(file)) return [];
    files.push(file);
  }
  // The canonical runner deliberately selects tracked .test.js/.test.mjs entry points, not helper modules.
  if (!files.length) return canonical ? [...tracked]
    .filter(file => !file.split('/').some(part => part === 'node_modules' || part.startsWith('.')))
    .filter(file => /(?:^|\/)[^/]+\.test\.(?:js|mjs)$/u.test(file)) : [];
  return files;
}

/** Recognize only option-free npm test and npm run script commands; options leave the gate opaque. */
function npmScriptName(words) {
  if (words?.[0] !== 'npm') return null;
  if (words[1] === 'test' && words.length === 2) return 'test';
  return words[1] === 'run' && words.length === 3 && words[2] && !words[2].startsWith('-') ? words[2] : null;
}

/** Known external tools leave the parent shell's directory/environment intact; shell builtins do not. */
function keepsShellContext(words) {
  return words && (words[0] === 'node' || words[0] === process.execPath
    || npmScriptName(words) !== null);
}

/** Resolve supported configured test commands at the receipt's committed source; opaque commands prove no file. */
function includedFiles(root, commit, command, tracked, depth = 0, context = {}) {
  if (depth > 4) return [];
  const words = literalShellWords(command);
  if (!words) {
    // A green AND chain proves its selected commands only if no earlier part changed the shell's cwd/environment.
    const parts = command.split(' && ');
    return parts.length > 1 && parts.every(part => keepsShellContext(literalShellWords(part)))
      ? parts.flatMap(part => includedFiles(root, commit, part, tracked, depth + 1, context)) : [];
  }
  if (npmScriptName(words) !== null) {
    try {
      const source = tryGit(root, ['show', `${commit}:package.json`]);
      const manifest = JSON.parse(source.stdout);
      if (Object.hasOwn(manifest, 'workspaces') || tracked.has('.npmrc')) return [];
      const script = manifest.scripts?.[npmScriptName(words)];
      return typeof script === 'string' ? includedFiles(root, commit, script, tracked, depth + 1, context) : [];
    } catch { return []; }
  }
  if (words[0] !== 'node' && words[0] !== process.execPath) return [];
  if (words[0] === process.execPath) context.execPath = process.execPath;
  if (words[1] === '--test') return selectedFiles(words.slice(2), tracked, false);
  if (words[1]?.replace(/^\.\//u, '') === 'bin/run-tests.js' && tracked.has('bin/run-tests.js')) {
    const source = tryGit(root, ['show', `${commit}:bin/run-tests.js`]);
    if (source.status !== 0 || source.stdout !== readFileSync(new URL('../bin/run-tests.js', import.meta.url), 'utf8')) return [];
    return selectedFiles(words.slice(2), tracked, true);
  }
  return [];
}

/** Resolve one immutable commit's policy/tree/coverage once; incomplete source remains unknown. */
export function committedRuleCoverage(root, head) {
  try {
    const configResult = tryGit(root, ['show', `${head}:pullboard.json`]);
    if (configResult.status !== 0) return null;
    const config = JSON.parse(configResult.stdout);
    if (typeof config.gate !== 'string' || !config.gate.trim()) return null;
    const tree = tryGit(root, ['rev-parse', `${head}^{tree}`]);
    const entries = tryGit(root, ['ls-tree', '-rz', head]);
    if (tree.status !== 0 || entries.status !== 0) return null;
    const tracked = new Set(entries.stdout.split('\0').filter(entry => /^(?:100644|100755) blob /u.test(entry))
      .map(entry => entry.slice(entry.indexOf('\t') + 1)));
    const context = {};
    const files = [...new Set(includedFiles(root, head, config.gate, tracked, 0, context))];
    return { head, tree: tree.stdout.trim(), command: createHash('sha256').update(config.gate).digest('hex'),
      fingerprint: proofCoverageFingerprint(), runtime: context.execPath ?? null, files };
  } catch { return null; }
}