refactor!: simplify runtime tools and remove replay engines (#572)
* fix(hopper): report unavailable procedure body coverage * refactor!: simplify runtime tools and remove replay engines Keep direct inspection, capture, and comparison tools. Bind handlers to named contracts and let agents compose experiments with commands and local fixture servers. BREAKING CHANGE: Remove controlled replay, Node characterization, finite replay-machine, managed runtime planning, and redundant string-tracing tools. Retire process replay, shims, reactive scenarios, custom checkpoints, and active browser replay and origin-scope inputs.
This commit is contained in:
@@ -20,20 +20,11 @@ import {
|
||||
processCaptureCancelled,
|
||||
} from "./ProcessCaptureError.js";
|
||||
export { ProcessCaptureError } from "./ProcessCaptureError.js";
|
||||
import { startLoopbackReplay, type LoopbackReplay } from "./LoopbackReplay.js";
|
||||
import { startProcessSampler } from "./ProcessSampling.js";
|
||||
import { snapshotRoots } from "./FilesystemSnapshot.js";
|
||||
import { ProcessCheckpoints } from "./ProcessCheckpoints.js";
|
||||
import { TerminalRenderer } from "./TerminalRenderer.js";
|
||||
import type { ProcessTimer } from "./ProcessTimer.js";
|
||||
import {
|
||||
startCommandShimReplay,
|
||||
type CommandShimReplay,
|
||||
} from "./CommandShimReplay.js";
|
||||
import {
|
||||
normalizeProcessShimEvent,
|
||||
normalizeProcessText,
|
||||
} from "./ProcessNormalization.js";
|
||||
import { normalizeProcessText } from "./ProcessNormalization.js";
|
||||
import { selectCapturedProcessGroupIds } from "../process/ProcessOwnership.js";
|
||||
import {
|
||||
awaitTerminalExit,
|
||||
@@ -44,6 +35,7 @@ import {
|
||||
prepareProcessCapture,
|
||||
releaseProcessResources,
|
||||
resolveProcessResult,
|
||||
settleProcessCaptureJournal,
|
||||
type PendingProcessCapture,
|
||||
} from "./ProcessCaptureLifecycle.js";
|
||||
import {
|
||||
@@ -53,40 +45,27 @@ import {
|
||||
import {
|
||||
createProcessCaptureJournal,
|
||||
scheduleScenarioInteractions,
|
||||
type ProcessCaptureJournal,
|
||||
} from "./ProcessCaptureJournal.js";
|
||||
import {
|
||||
assertSupportedReactiveScenario,
|
||||
projectProcessReactiveRun,
|
||||
startProcessReactiveHarness,
|
||||
type ProcessReactiveHarness,
|
||||
} from "./ProcessReactiveHarness.js";
|
||||
import { makeProcessCaptureEnvironment } from "./ProcessCaptureEnvironment.js";
|
||||
import { classifyFilesystemEffects } from "./ProcessFilesystemEffects.js";
|
||||
import { processCaptureOwnershipUnavailableReason } from "./ProcessCaptureCapability.js";
|
||||
export { probeProcessCaptureCapability } from "./ProcessCaptureCapability.js";
|
||||
|
||||
interface StartedCaptureRuntime {
|
||||
readonly replay: LoopbackReplay;
|
||||
readonly renderer: TerminalRenderer;
|
||||
readonly checkpoints: ProcessCheckpoints;
|
||||
readonly shimReplay: CommandShimReplay;
|
||||
readonly terminal: IPty;
|
||||
readonly started: number;
|
||||
readonly startedAt: Date;
|
||||
readonly lastOutput: () => number;
|
||||
readonly framesTruncated: () => boolean;
|
||||
readonly stopSampler: () => Promise<{ readonly partial: boolean }>;
|
||||
readonly reactive: ProcessReactiveHarness | undefined;
|
||||
}
|
||||
|
||||
const cleanupFailedStartup = async (options: {
|
||||
readonly cause: unknown;
|
||||
readonly timers: Set<ProcessTimer>;
|
||||
readonly replay: LoopbackReplay | undefined;
|
||||
readonly terminal: IPty | undefined;
|
||||
readonly renderer: TerminalRenderer | undefined;
|
||||
readonly shimReplay: CommandShimReplay | undefined;
|
||||
readonly checkpoints: ProcessCheckpoints | undefined;
|
||||
readonly runId: string;
|
||||
readonly temporaryRoot: string;
|
||||
}): Promise<never> => {
|
||||
@@ -125,67 +104,23 @@ interface StartCaptureRuntimeOptions {
|
||||
readonly hostEnvironment: Readonly<Record<string, string | undefined>>;
|
||||
readonly temporaryRoot: string;
|
||||
readonly runId: string;
|
||||
readonly before: Awaited<ReturnType<typeof snapshotRoots>>;
|
||||
readonly frames: TerminalFrame[];
|
||||
readonly samples: ProcessSample[];
|
||||
readonly interactions: InteractionEvent[];
|
||||
readonly timers: Set<ProcessTimer>;
|
||||
readonly dispatchedEventIndexes: Set<number>;
|
||||
readonly recordEvent: RecordProcessCaptureEvent;
|
||||
readonly journal: ProcessCaptureJournal;
|
||||
readonly signal?: AbortSignal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait until the capture journal stops growing.
|
||||
*
|
||||
* node-pty reports buffered output through `onData` callbacks rather than an
|
||||
* awaitable read, so chunks can still be in flight after the child exits. The
|
||||
* quiet interval is measured in elapsed time rather than scheduler turns:
|
||||
* consecutive `setImmediate` turns elapse in microseconds, so any fixed number
|
||||
* of them finishes long before a delayed pty callback under host contention,
|
||||
* which is exactly when the trailing chunk matters. `maxWaitMs` bounds the
|
||||
* total wait so a pathological stream cannot stall exit.
|
||||
*/
|
||||
const settleTrailingJournal = async (
|
||||
journal: readonly ProcessCaptureEventJournalEntry[],
|
||||
quietMs = 25,
|
||||
maxWaitMs = 500,
|
||||
): Promise<void> => {
|
||||
const deadline = Date.now() + maxWaitMs;
|
||||
let observed = journal.length;
|
||||
let lastChangeAt = Date.now();
|
||||
while (Date.now() < deadline) {
|
||||
await new Promise<void>((resolve) => setTimeout(resolve, 4));
|
||||
if (journal.length !== observed) {
|
||||
observed = journal.length;
|
||||
lastChangeAt = Date.now();
|
||||
} else if (Date.now() - lastChangeAt >= quietMs) return;
|
||||
}
|
||||
};
|
||||
|
||||
const reactiveCapture = (
|
||||
options: StartCaptureRuntimeOptions,
|
||||
protocolEvents: () => LoopbackReplay["events"],
|
||||
): Parameters<typeof startProcessReactiveHarness>[0]["capture"] => ({
|
||||
...options,
|
||||
processSamples: options.samples,
|
||||
protocolEvents,
|
||||
});
|
||||
|
||||
const startCaptureRuntime = async (
|
||||
options: StartCaptureRuntimeOptions,
|
||||
): Promise<StartedCaptureRuntime> => {
|
||||
const { scenario } = options;
|
||||
let replay: LoopbackReplay | undefined;
|
||||
let renderer: TerminalRenderer | undefined;
|
||||
let checkpoints: ProcessCheckpoints | undefined;
|
||||
let shimReplay: CommandShimReplay | undefined;
|
||||
let terminal: IPty | undefined;
|
||||
let reactive: ProcessReactiveHarness | undefined;
|
||||
try {
|
||||
const { spawn } = await import("@lydell/node-pty");
|
||||
replay = await startLoopbackReplay(scenario, options.recordEvent);
|
||||
const startedAt = new Date();
|
||||
const started = Date.now();
|
||||
let lastOutput = started;
|
||||
@@ -195,19 +130,9 @@ const startCaptureRuntime = async (
|
||||
() => terminal?.pid ?? -1,
|
||||
options.recordEvent,
|
||||
);
|
||||
checkpoints = new ProcessCheckpoints(scenario, started, options.before, {
|
||||
signal: options.signal,
|
||||
recordEvent: options.recordEvent,
|
||||
});
|
||||
shimReplay = await startCommandShimReplay(
|
||||
scenario,
|
||||
options.temporaryRoot,
|
||||
started,
|
||||
options.recordEvent,
|
||||
);
|
||||
terminal = spawn(scenario.executable, [...scenario.arguments], {
|
||||
cwd: scenario.working_directory,
|
||||
env: makeProcessCaptureEnvironment({ ...options, replay, shimReplay }),
|
||||
env: makeProcessCaptureEnvironment(options),
|
||||
cols: scenario.terminal.columns,
|
||||
rows: scenario.terminal.rows,
|
||||
name: "xterm-256color",
|
||||
@@ -218,16 +143,6 @@ const startCaptureRuntime = async (
|
||||
started,
|
||||
onOutput: () => (lastOutput = Date.now()),
|
||||
renderer,
|
||||
checkpoints,
|
||||
});
|
||||
reactive = startProcessReactiveHarness({
|
||||
capture: reactiveCapture(options, () => replay?.events ?? []),
|
||||
scenario,
|
||||
terminal: () => terminal,
|
||||
renderer,
|
||||
checkpoints,
|
||||
shimReplay,
|
||||
started,
|
||||
});
|
||||
scheduleScenarioInteractions({
|
||||
...options,
|
||||
@@ -244,49 +159,26 @@ const startCaptureRuntime = async (
|
||||
recordEvent: options.recordEvent,
|
||||
});
|
||||
return {
|
||||
replay,
|
||||
renderer,
|
||||
checkpoints,
|
||||
shimReplay,
|
||||
terminal,
|
||||
started,
|
||||
startedAt,
|
||||
lastOutput: () => lastOutput,
|
||||
framesTruncated,
|
||||
stopSampler,
|
||||
reactive,
|
||||
};
|
||||
} catch (cause: unknown) {
|
||||
reactive?.unsubscribe();
|
||||
await reactive?.coordinator.close();
|
||||
return cleanupFailedStartup({
|
||||
cause,
|
||||
timers: options.timers,
|
||||
replay,
|
||||
terminal,
|
||||
renderer,
|
||||
shimReplay,
|
||||
checkpoints,
|
||||
runId: options.runId,
|
||||
temporaryRoot: options.temporaryRoot,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const normalizedShimEvents = (
|
||||
runtime: StartedCaptureRuntime,
|
||||
scenario: ProcessScenario,
|
||||
temporaryRoot: string,
|
||||
): UnverifiedProcessCapture["shim_events"] =>
|
||||
runtime.shimReplay.events.map((event) =>
|
||||
normalizeProcessShimEvent(
|
||||
event,
|
||||
scenario,
|
||||
temporaryRoot,
|
||||
runtime.terminal.pid,
|
||||
),
|
||||
);
|
||||
|
||||
const finishProcessRun = async (options: {
|
||||
readonly runtime: StartedCaptureRuntime | undefined;
|
||||
readonly timers: Set<ProcessTimer>;
|
||||
@@ -297,15 +189,11 @@ const finishProcessRun = async (options: {
|
||||
readonly capture: PendingProcessCapture | undefined;
|
||||
readonly executionFailure: unknown;
|
||||
}): Promise<ProcessCapture> => {
|
||||
options.runtime?.reactive?.unsubscribe();
|
||||
await options.stopSampler();
|
||||
const cleanupFailure = await releaseProcessResources({
|
||||
timers: options.timers,
|
||||
replay: options.runtime?.replay,
|
||||
terminal: options.runtime?.terminal,
|
||||
renderer: options.runtime?.renderer,
|
||||
shimReplay: options.runtime?.shimReplay,
|
||||
checkpoints: options.runtime?.checkpoints,
|
||||
runId: options.runId,
|
||||
temporaryRoot: options.temporaryRoot,
|
||||
capturedProcessGroupIds:
|
||||
@@ -316,11 +204,6 @@ const finishProcessRun = async (options: {
|
||||
options.samples,
|
||||
),
|
||||
});
|
||||
if (cleanupFailure !== undefined)
|
||||
await options.runtime?.reactive?.coordinator.submit({
|
||||
kind: "cleanup_failed",
|
||||
});
|
||||
await options.runtime?.reactive?.coordinator.close();
|
||||
let { capture, executionFailure } = options;
|
||||
let verifiedCapture: ProcessCapture | undefined;
|
||||
if (capture !== undefined && cleanupFailure === undefined) {
|
||||
@@ -330,7 +213,6 @@ const finishProcessRun = async (options: {
|
||||
: { ...capture.settlement, cleanup_outcome: "cleaned" };
|
||||
const completedCapture: UnverifiedProcessCapture = {
|
||||
...capture,
|
||||
reactive_run: projectProcessReactiveRun(options.runtime?.reactive),
|
||||
settlement,
|
||||
cleanup: {
|
||||
owned_process_group: "verified",
|
||||
@@ -369,45 +251,36 @@ const completeCapture = async (options: {
|
||||
readonly recordEvent: RecordProcessCaptureEvent;
|
||||
}): Promise<PendingProcessCapture> => {
|
||||
const { runtime, scenario } = options;
|
||||
runtime.checkpoints.trigger("root_exit");
|
||||
const { reason } = options.exit;
|
||||
if (reason === "cancelled") {
|
||||
if (runtime.reactive !== undefined) {
|
||||
await runtime.reactive.coordinator.submit({ kind: "cancelled" });
|
||||
runtime.reactive.unsubscribe();
|
||||
}
|
||||
throw processCaptureCancelled();
|
||||
}
|
||||
if (reason === "cancelled") throw processCaptureCancelled();
|
||||
const settlement = await observeSettlement(
|
||||
options.runId,
|
||||
selectCapturedProcessGroupIds(runtime.terminal.pid, options.samples),
|
||||
scenario.settle_ms,
|
||||
options.recordEvent,
|
||||
);
|
||||
runtime.checkpoints.trigger("settled");
|
||||
const samplingPartial = (await runtime.stopSampler()).partial;
|
||||
if (runtime.reactive !== undefined) {
|
||||
// Process, filesystem, shim, and protocol observations can be recorded
|
||||
// during settlement after the PTY exits. Make target loss terminal only
|
||||
// after that journal has drained, otherwise the coordinator's control
|
||||
// priority discards a valid multi-source completion predicate.
|
||||
//
|
||||
// Draining the coordinator is not sufficient on its own: node-pty delivers
|
||||
// buffered output through onData callbacks, which run outside the
|
||||
// coordinator's queue. A trailing chunk that has not been delivered yet has
|
||||
// produced no journal entry, so the drain has nothing to wait for and the
|
||||
// target is declared lost while a terminal trigger is still satisfiable.
|
||||
// Let any trailing chunks land first.
|
||||
await settleTrailingJournal(options.eventJournal);
|
||||
await runtime.reactive.coordinator.drain();
|
||||
await runtime.reactive.coordinator.submit({ kind: "target_lost" });
|
||||
runtime.reactive.unsubscribe();
|
||||
}
|
||||
await settleProcessCaptureJournal(options.eventJournal);
|
||||
assertNotCancelled(options.signal);
|
||||
const after = await snapshotRoots(scenario, options.signal);
|
||||
options.recordEvent("filesystem_checkpoints", 1);
|
||||
const renderedFrames = await runtime.renderer.frames();
|
||||
const checkpoints = await runtime.checkpoints.finish(after);
|
||||
await runtime.replay.close();
|
||||
const checkpoints: UnverifiedProcessCapture["filesystem_checkpoints"] = [
|
||||
{
|
||||
name: "before",
|
||||
at_ms: 0,
|
||||
files: options.before.files,
|
||||
effects: [],
|
||||
truncated: options.before.truncated,
|
||||
},
|
||||
{
|
||||
name: "after_settlement",
|
||||
at_ms: Math.max(0, Date.now() - runtime.started),
|
||||
files: after.files,
|
||||
effects: classifyFilesystemEffects(options.before.files, after.files),
|
||||
truncated: after.truncated,
|
||||
},
|
||||
];
|
||||
const manifest = await createRunManifest(
|
||||
scenario,
|
||||
runtime.startedAt,
|
||||
@@ -416,8 +289,6 @@ const completeCapture = async (options: {
|
||||
const truncated =
|
||||
options.initiallyTruncated ||
|
||||
after.truncated ||
|
||||
runtime.replay.truncated ||
|
||||
runtime.shimReplay.truncated ||
|
||||
runtime.framesTruncated() ||
|
||||
checkpoints.some(({ truncated: partial }) => partial) ||
|
||||
samplingPartial ||
|
||||
@@ -426,7 +297,6 @@ const completeCapture = async (options: {
|
||||
frames: options.frames,
|
||||
exit: { ...options.exit, reason },
|
||||
samples: options.samples,
|
||||
replay: runtime.replay,
|
||||
before: options.before,
|
||||
after,
|
||||
truncated,
|
||||
@@ -436,7 +306,6 @@ const completeCapture = async (options: {
|
||||
renderedFrames,
|
||||
interactions: options.interactions,
|
||||
checkpoints,
|
||||
shimEvents: normalizedShimEvents(runtime, scenario, options.temporaryRoot),
|
||||
settlement,
|
||||
manifest,
|
||||
eventJournal: options.eventJournal,
|
||||
@@ -449,7 +318,6 @@ const runProcessScenario = async (
|
||||
signal?: AbortSignal,
|
||||
hostEnvironment: Readonly<Record<string, string | undefined>> = process.env,
|
||||
): Promise<ProcessCapture> => {
|
||||
assertSupportedReactiveScenario(scenario);
|
||||
const { temporaryRoot, runId, before } = await prepareProcessCapture(
|
||||
scenario,
|
||||
signal,
|
||||
@@ -458,6 +326,7 @@ const runProcessScenario = async (
|
||||
const samples: ProcessSample[] = [];
|
||||
const journal = createProcessCaptureJournal();
|
||||
const { entries: eventJournal, recordEvent } = journal;
|
||||
recordEvent("filesystem_checkpoints", 0);
|
||||
let runtime: StartedCaptureRuntime | undefined;
|
||||
const timers = new Set<ProcessTimer>();
|
||||
let capture: PendingProcessCapture | undefined;
|
||||
@@ -472,14 +341,12 @@ const runProcessScenario = async (
|
||||
hostEnvironment,
|
||||
temporaryRoot,
|
||||
runId,
|
||||
before,
|
||||
frames,
|
||||
samples,
|
||||
interactions,
|
||||
timers,
|
||||
dispatchedEventIndexes,
|
||||
recordEvent,
|
||||
journal,
|
||||
...(signal === undefined ? {} : { signal }),
|
||||
});
|
||||
stopSampler = runtime.stopSampler;
|
||||
|
||||
Reference in New Issue
Block a user