Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302#!/usr/bin/env node
/** Container-side entrypoint for one shard. Reads the shard manifest, prepares the work tree, establishes a green baseline (tests pass, types clean, tsbuildinfo warmed), runs the mutant loop, and writes the shard report. A red baseline reports every mutant as unrun so the host treats the shard as an infra failure rather than blaming mutants. @example ```bash node /baked/package/cli/mutation-test/src/container/main.ts ``` */
import { mkdir, readFile, writeFile,} from 'node:fs/promises';import { dirname, join,} from 'node:path';import { pathToFileURL, } from 'node:url';
import { tagged, } from '@monochromatic-dev/module-logger/ts';
import { isRecord, } from '../is-record.ts';import { MANIFEST_MOUNT, REPORT_MOUNT, WORK_MOUNT,} from '../mounts.ts';import { runBuildStep, } from './build-step.ts';import { runMutantLoop, } from './mutant-loop.ts';import { runTests, type TestRunOutcome,} from './test-run.ts';import { tsgoCheck, type TsgoOutcome,} from './tsgo-check.ts';import { prepareWorkTree, } from './worktree.ts';import { SHARD_SCHEMA_VERSION, type ShardManifest, type ShardReport,} from '../shard-schema.ts';
/** Module logger for the container entrypoint. */const l = tagged({ tag: 'mutation-test-container', },);
/** Manifest file name inside the manifest mount. */export const MANIFEST_FILE_NAME = 'shard-manifest.json';
/** Report file name inside the report mount. */export const REPORT_FILE_NAME = 'shard-report.json';
/** Generous baseline timeout: the first unmutated run compiles caches and warms tsbuildinfo, so per-mutant limits do not apply to it. */const BASELINE_TIMEOUT_MS = 600_000;
/** Returns whether a parsed JSON value has the shard manifest shape. Checks the discriminating fields (version, id, package, arrays); the host is the only writer, so field-level validation stays shallow. @param value - Parsed JSON value. @returns Whether value is a usable manifest. @example ```ts isShardManifest(JSON.parse(raw)); ``` */function isShardManifest(value: unknown,): value is ShardManifest { if (!isRecord(value,)) return false;
/** Record view over the candidate manifest. */ const record = value; return (record.schemaVersion === SHARD_SCHEMA_VERSION) && ((typeof record.shardId) === 'string') && ((typeof record.packagePath) === 'string') && Array.isArray(record.mutants,) && Array.isArray(record.tests,) && ((typeof record.timeoutFloorMs) === 'number') && ((typeof record.timeoutFactor) === 'number');}
/** Reads and validates the mounted shard manifest. @returns Parsed manifest. @throws Error when the manifest is missing or has a wrong shape. @example ```ts const manifest = await readManifest(); ``` */export async function readManifest(): Promise<ShardManifest> { /** Raw manifest JSON text from the mount. */ const raw = await readFile( join( MANIFEST_MOUNT, MANIFEST_FILE_NAME, ), 'utf8', ); /** Parsed manifest before shape validation. */ const parsed: unknown = JSON.parse(raw,);
if (!isShardManifest(parsed,)) throw new Error( `manifest at ${MANIFEST_MOUNT}/${MANIFEST_FILE_NAME} has unsupported shape or version; expected schema ${String(SHARD_SCHEMA_VERSION,)}`, );
return parsed;}
/** Writes the shard report to the report mount. @param report - Completed shard report. @mutates report - `JSON.stringify` may invoke `toJSON`, getters, or proxy traps. @example ```ts await writeReport(report); ``` */async function writeReport(report: ShardReport,): Promise<void> { /** Report destination path on the report mount. */ const reportPath = join( REPORT_MOUNT, REPORT_FILE_NAME, ); await mkdir( dirname(reportPath,), { recursive: true, }, ); await writeFile( reportPath, `${JSON.stringify( report, null, 2, )}\n`, 'utf8', );}
/** Executes the full container-side shard flow. @example ```ts await runShard(); ``` */export async function runShard(): Promise<void> { /** Logger scoped to the shard run. */ const rl = tagged({ tag: runShard.name, l, },); /** Mounted shard manifest. */ const manifest = await readManifest(); rl.info(`shard ${manifest.shardId}: ${String(manifest.mutants .length,)} mutants`,);
await prepareWorkTree();
/** Target package working directory inside the work tree. */ const packageCwd = join( WORK_MOUNT, manifest.packagePath, ); /** Baseline build materialising dist output and declarations before the type gate and the output-importing tests. */ const baselineBuild = await runBuildStep({ packageCwd, },); /** Baseline type check, also warming the incremental build info. */ const baselineTsgo: TsgoOutcome = baselineBuild.clean ? await tsgoCheck({ cwd: packageCwd, },) : { clean: false, durationMs: 0, detail: baselineBuild.detail, }; /** Baseline test run over the selected tests. */ const baselineTests: TestRunOutcome = await runTests({ cwd: packageCwd, tests: manifest.tests, timeoutMs: BASELINE_TIMEOUT_MS, },); /** Whether the unmutated package passes its own gates. */ const green = baselineBuild.clean && baselineTsgo.clean && (baselineTests.kind === 'passed');
if (!green) { rl.error( `red baseline: tsgo clean=${String(baselineTsgo.clean,)}, tests=${baselineTests.kind}`, ); await writeReport({ schemaVersion: SHARD_SCHEMA_VERSION, shardId: manifest.shardId, baseline: { green: false, testsMs: baselineTests.durationMs, tsgoMs: baselineTsgo.durationMs, detail: `${baselineTsgo.detail} ${baselineTests.detail}`.trim(), }, results: [], unrun: manifest.mutants .map(function toId(mutant,): string { return mutant.id; },), anomaly: 'red baseline: unmutated package failed its own gates', },); return; }
/** Mutant loop output for this shard. */ const loop = await runMutantLoop({ packageCwd, manifest, baselineTestsMs: baselineTests.durationMs, },);
await writeReport({ schemaVersion: SHARD_SCHEMA_VERSION, shardId: manifest.shardId, baseline: { green: true, testsMs: baselineTests.durationMs, tsgoMs: baselineTsgo.durationMs, detail: '', }, results: loop.results, unrun: loop.unrun, anomaly: loop.anomaly, },); rl.info( `shard ${manifest.shardId} done: ${String(loop.results .length,)} results, ${String(loop.unrun .length,)} unrun`, );}
/** Whether this module is running as the process entrypoint. */const isDirectEntrypoint = (process.argv[1] !== undefined) && (import.meta.url === pathToFileURL(process.argv[1],) .href);
if (isDirectEntrypoint) await runShard();