scripts/repro/lib/exec-harness.mjs

GitHub
// Execution-equivalence harness for the reproduction matrices.
//
// Reads JSON on stdin: { env, returns, programs: [lowered, recovered] },
// runs both programs in isolated `node:vm` contexts with identical
// deterministic environments, records every observable effect (calls into
// stub functions with deeply serialized arguments, plus any thrown error),
// and writes a JSON verdict on stdout.
//
// Substring assertions check that recovery produced the right *shape*; this
// harness checks that it produced the right *program*: a recovery that
// assigns to a `const`, double-evaluates a receiver, or reads a stale key
// produces a different effect log even when every expected needle matches.
//
// Environment model per program run:
// - Each name in `env` is defined as a global with a fresh JSON clone of its
//   value (mutations cannot leak between the two runs).
// - Each name in `returns` becomes a recording stub returning a fresh clone
//   of the given value on every call.
// - Any other free identifier is auto-stubbed on first `ReferenceError`: a
//   recording function that returns a deterministic per-call token object.
//   The run is retried from scratch after each new stub, so both programs
//   see identical environments regardless of which names they touch first.
// - `Math.random` and `Date.now` are deterministic inside the context.
//
// Programs run as classic scripts. Module syntax (import/export) cannot be
// executed here; such rows report `skipped` and keep their substring verdict.

import { readFileSync } from "node:fs";
import vm from "node:vm";

const MAX_STUB_RETRIES = 100;
const EVAL_TIMEOUT_MS = 2000;
const SERIALIZE_DEPTH = 6;

function serialize(value, depth = SERIALIZE_DEPTH, seen = new WeakSet()) {
  if (value === null) return null;
  const type = typeof value;
  if (type === "function") return `