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.

2.9 KB
Raw Blame
/** Collect private command output through pipes; dependency files are never size-limited [V18,V2]. */
import { spawn } from 'node:child_process';
import {
  captureTestRuntimes,
  executionEnvironment,
} from './execution-environment.js';
import {
  createWriteStream,
  readFileSync,
  writeFileSync,
} from 'node:fs';

const { command, timeout, pidFile, logPath } = JSON.parse(readFileSync(0, 'utf8'));
const LIMIT = 8 * 1024 * 1024;
const HALF = LIMIT / 2;
let head = Buffer.alloc(0);
let tail = Buffer.alloc(0);
let bytes = 0;
let timedOut = false;
let error = null;
let logError = null;
const log = createWriteStream(logPath, { flags: 'wx', mode: 0o600 });
log.on('error', cause => {
  logError = { code: cause.code ?? 'CHECK_LOG' };
  // A failed log must not leave either pipe paused forever waiting for a drain that cannot happen.
  child.stdout?.resume();
  child.stderr?.resume();
});

/** Drain every byte while retaining only the beginning and end of the combined output. */
function capture(chunk) {
  bytes += chunk.length;
  const take = Math.min(chunk.length, HALF - head.length);
  if (take) head = Buffer.concat([head, chunk.subarray(0, take)]);
  const rest = chunk.subarray(take);
  if (rest.length) tail = Buffer.concat([tail, rest]).subarray(-HALF);
  if (!logError && !log.write(chunk)) {
    child.stdout.pause();
    child.stderr.pause();
    log.once('drain', () => { child.stdout.resume(); child.stderr.resume(); });
  }
}

const runtimes = captureTestRuntimes(process.env);
const child = spawn('sh', ['-c', `(\n${command}\n) 2>&1`], { detached: true, env: runtimes.env, stdio: ['ignore', 'pipe', 'pipe'] });
if (child.pid) writeFileSync(pidFile, String(child.pid), { mode: 0o600 });

/** Stop the command's entire process group, including background descendants that retain pipes. */
function stop() {
  if (child.pid) {
    try { process.kill(-child.pid, 'SIGKILL'); } catch { /* The group has already exited. */ }
  }
}

const timer = setTimeout(() => { timedOut = true; stop(); }, timeout);
child.stdout.on('data', capture);
child.stderr.on('data', capture);
child.once('error', cause => { error = { code: cause.code ?? 'CHECK_RUNNER' }; });
child.once('exit', stop);
child.once('close', (status, signal) => {
  clearTimeout(timer);
  stop();
  let sent = false;
  /** Return one bounded result after the output file has either closed or failed. */
  const finish = () => {
    if (sent) return;
    sent = true;
    const marker = bytes > LIMIT ? '\n[output capped at 8 MiB; middle omitted]\n' : '';
    const output = head.toString('utf8') + marker + tail.toString('utf8');
    const testRuntimes = runtimes.finish();
    process.stdout.write(JSON.stringify({ status, signal, executed: Boolean(child.pid), error: timedOut ? { code: 'ETIMEDOUT' } : error, logError, output, environment: executionEnvironment(), testRuntimes }));
  };
  log.once('close', finish);
  if (log.closed) finish();
  else log.end();
});