Files
rea/src/replay/LinuxRuntimeClosure.ts
T
N0zoM1z0 d86231ad33 feat(application): add isolated JavaScript replay
Expose one plan/execute replay contract through both CLI and MCP with exact source, input, runtime, sandbox, limit, and export commitments.

Run selected ESM and extracted CommonJS/Rspack factories in a disposable Linux x64 Bubblewrap namespace with a content-addressed Node ELF closure, architecture-checked seccomp, delegated systemd cgroup limits, bounded protocol capture, cancellation, and observable cleanup.

Preserve left and right observations as Evidence, derive differential comparisons without erasing unknowns, add optional separately authorized reproducers, and fail closed on stale plans or unavailable host boundaries.

Add source-owned parser, sanitizer, clipboard, denial, timeout, OOM, protocol-forgery, and cancellation coverage; a real Linux verifier and workflow; regenerated catalogs and TypeDoc; and updated the packaged REA analysis skill for the 83-tool inventory.
2026-07-16 15:45:55 +09:00

68 lines
2.1 KiB
TypeScript

import { spawn } from "node:child_process";
import { createHash } from "node:crypto";
import { readFile, realpath } from "node:fs/promises";
import type { ReplayRuntimeFileIdentity } from "../application/JavaScriptReplayPlanning.js";
const LDD_OUTPUT_LIMIT = 64 * 1024;
/** Resolve and content-address the exact ELF closure mounted into a replay. */
export const resolveLinuxRuntimeClosure = async (
nodePath: string,
): Promise<readonly ReplayRuntimeFileIdentity[]> => {
const canonicalNode = await realpath(nodePath);
const collected = await collectLdd(canonicalNode);
if (collected.code !== 0)
throw new TypeError(
"Node runtime dependency closure could not be resolved",
);
const paths = [...collected.stdout.matchAll(/(?:=>\s+)?(\/[^\s()]+)/gu)]
.map((match) => match[1])
.filter((value): value is string => value !== undefined);
const libraries = await Promise.all(
[...new Set(paths)].map(async (path) => {
const sourcePath = await realpath(path);
return identity(sourcePath, path);
}),
);
return [await identity(canonicalNode, "/runtime/node"), ...libraries];
};
const identity = async (
sourcePath: string,
destinationPath: string,
): Promise<ReplayRuntimeFileIdentity> => ({
sourcePath,
destinationPath,
sha256: createHash("sha256")
.update(await readFile(sourcePath))
.digest("hex"),
});
const collectLdd = async (
path: string,
): Promise<{ readonly code: number | null; readonly stdout: string }> =>
new Promise((resolve, reject) => {
const child = spawn("/usr/bin/ldd", [path], {
stdio: ["ignore", "pipe", "ignore"],
});
const timeout = setTimeout(() => child.kill("SIGKILL"), 5_000);
timeout.unref();
let stdout = "";
child.stdout.on("data", (chunk: Buffer) => {
const remaining = Math.max(
0,
LDD_OUTPUT_LIMIT - Buffer.byteLength(stdout),
);
stdout += chunk.subarray(0, remaining).toString("utf8");
});
child.once("error", (error) => {
clearTimeout(timeout);
reject(error);
});
child.once("close", (code) => {
clearTimeout(timeout);
resolve({ code, stdout });
});
});