feat(process): report live capture progress through CLI and MCP
Long captures emitted only a final result, so a quiet child looked idle. Pass the shared rate-limited progress reporter through the capture lifecycle and report the observed phase, elapsed time, and collected-unit counts. CLI writes those lines to stderr; MCP forwards them only when the client supplied a progress token. Final Evidence on stdout keeps its shape.
This commit is contained in:
@@ -75,6 +75,7 @@ interface TerminalExitOptions {
|
||||
readonly interactions: InteractionEvent[];
|
||||
readonly dispatchedEventIndexes: ReadonlySet<number>;
|
||||
readonly recordEvent: RecordProcessCaptureEvent;
|
||||
readonly onLiveProgress?: () => void;
|
||||
}
|
||||
|
||||
interface CaptureResultOptions {
|
||||
@@ -601,8 +602,10 @@ export const observeSettlement = async (
|
||||
recordEvent: RecordProcessCaptureEvent = () => undefined,
|
||||
platform: NodeJS.Platform = process.platform,
|
||||
signal?: AbortSignal,
|
||||
onLiveProgress?: () => void,
|
||||
): Promise<ObservedProcessSettlement> => {
|
||||
assertNotCancelled(signal);
|
||||
onLiveProgress?.();
|
||||
if (platform === "win32") {
|
||||
recordEvent("lifecycle", 1);
|
||||
return { state: "unverifiable", elapsed_ms: 0 };
|
||||
@@ -611,6 +614,7 @@ export const observeSettlement = async (
|
||||
let consecutiveEmpty = 0;
|
||||
let deadlineReached = false;
|
||||
while (!deadlineReached) {
|
||||
onLiveProgress?.();
|
||||
assertNotCancelled(signal);
|
||||
const observations = await Promise.all(
|
||||
[...new Set(processGroupIds)].map((processGroupId) =>
|
||||
@@ -651,6 +655,7 @@ export const awaitTerminalExit = async ({
|
||||
interactions,
|
||||
dispatchedEventIndexes,
|
||||
recordEvent,
|
||||
onLiveProgress,
|
||||
}: TerminalExitOptions): Promise<{
|
||||
exitCode: number;
|
||||
signal?: number;
|
||||
@@ -661,6 +666,7 @@ export const awaitTerminalExit = async ({
|
||||
// Keep the initiating lifecycle reason so comparisons distinguish a target
|
||||
// exit from harness-owned timeout, idle-timeout, and cancellation cleanup.
|
||||
let reason: "exited" | "timeout" | "idle_timeout" | "cancelled" = "exited";
|
||||
onLiveProgress?.();
|
||||
terminal.onExit((exit) => {
|
||||
recordEvent("lifecycle", 0);
|
||||
for (const [eventIndex, event] of scenario.events.entries()) {
|
||||
@@ -688,6 +694,7 @@ export const awaitTerminalExit = async ({
|
||||
resolveExit({ ...exit, reason });
|
||||
});
|
||||
const timeout = scheduleProcessInterval(() => {
|
||||
onLiveProgress?.();
|
||||
if (signal?.aborted === true) {
|
||||
reason = "cancelled";
|
||||
terminal.kill("SIGKILL");
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
import type { ProcessCaptureCleanupReport } from "../../domain/process/processCapture.js";
|
||||
|
||||
/**
|
||||
* Progress port accepted by process capture.
|
||||
* Structurally matches the shared application `ProgressReporter`.
|
||||
*/
|
||||
export interface ProcessCaptureProgress {
|
||||
report(update: {
|
||||
readonly phase: string;
|
||||
readonly completed: number;
|
||||
readonly total: null;
|
||||
readonly message: string;
|
||||
readonly terminal?: boolean;
|
||||
}): Promise<void>;
|
||||
}
|
||||
|
||||
/** Observed collection counts. These are not a percentage of an unknown total. */
|
||||
export interface ProcessCaptureProgressCounts {
|
||||
readonly frames: number;
|
||||
readonly samples: number;
|
||||
readonly interactions: number;
|
||||
}
|
||||
|
||||
export type ProcessCaptureProgressPhase =
|
||||
| "prepare"
|
||||
| "running"
|
||||
| "settling"
|
||||
| "cleanup";
|
||||
|
||||
export type ProcessCaptureProgressDisposition =
|
||||
| "exited"
|
||||
| "timeout"
|
||||
| "idle_timeout"
|
||||
| "cancelled"
|
||||
| "failed";
|
||||
|
||||
export interface ProcessCaptureProgressReport {
|
||||
readonly phase: ProcessCaptureProgressPhase;
|
||||
readonly counts: ProcessCaptureProgressCounts;
|
||||
readonly terminal?: boolean;
|
||||
readonly disposition?: ProcessCaptureProgressDisposition;
|
||||
readonly cleanup?: ProcessCaptureCleanupReport;
|
||||
}
|
||||
|
||||
const formatProgressMessage = (
|
||||
elapsedMs: number,
|
||||
counts: ProcessCaptureProgressCounts,
|
||||
disposition: ProcessCaptureProgressDisposition | undefined,
|
||||
cleanup: ProcessCaptureCleanupReport | undefined,
|
||||
): string => {
|
||||
const status = `elapsed_ms=${String(elapsedMs)} frames=${String(counts.frames)} samples=${String(counts.samples)} interactions=${String(counts.interactions)}`;
|
||||
const dispositionText =
|
||||
disposition === undefined ? "" : ` disposition=${disposition}`;
|
||||
const cleanupText =
|
||||
cleanup === undefined
|
||||
? ""
|
||||
: ` owned_process_group=${cleanup.owned_process_group.state} terminal_renderer=${cleanup.terminal_renderer.state} temporary_root=${cleanup.temporary_root.state}`;
|
||||
return `${status}${dispositionText}${cleanupText}`;
|
||||
};
|
||||
|
||||
/** Serialize observed capture status through one rate-limited reporter. */
|
||||
export const createProcessCaptureProgressTracker = (
|
||||
progress: ProcessCaptureProgress | undefined,
|
||||
now: () => number = Date.now,
|
||||
) => {
|
||||
const started = now();
|
||||
let completed = 0;
|
||||
let live = true;
|
||||
let pending = Promise.resolve();
|
||||
return {
|
||||
closeLive(): void {
|
||||
live = false;
|
||||
},
|
||||
report(update: ProcessCaptureProgressReport): Promise<void> {
|
||||
if (progress === undefined) return Promise.resolve();
|
||||
if (update.terminal !== true && !live) return Promise.resolve();
|
||||
const units =
|
||||
update.counts.frames +
|
||||
update.counts.samples +
|
||||
update.counts.interactions;
|
||||
if (units > completed) completed = units;
|
||||
const payload = {
|
||||
phase: update.phase,
|
||||
completed,
|
||||
total: null as null,
|
||||
message: formatProgressMessage(
|
||||
Math.max(0, now() - started),
|
||||
update.counts,
|
||||
update.disposition,
|
||||
update.cleanup,
|
||||
),
|
||||
...(update.terminal === true ? { terminal: true as const } : {}),
|
||||
};
|
||||
const send = () => progress.report(payload);
|
||||
const scheduled = pending.then(send, send);
|
||||
pending = scheduled.then(
|
||||
() => undefined,
|
||||
() => undefined,
|
||||
);
|
||||
return scheduled.then(
|
||||
() => undefined,
|
||||
() => undefined,
|
||||
);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export type ProcessCaptureProgressTracker = ReturnType<
|
||||
typeof createProcessCaptureProgressTracker
|
||||
>;
|
||||
@@ -55,6 +55,13 @@ import {
|
||||
import { makeProcessCaptureEnvironment } from "./ProcessCaptureEnvironment.js";
|
||||
import { classifyFilesystemEffects } from "./ProcessFilesystemEffects.js";
|
||||
import { processCaptureOwnershipUnavailableReason } from "./ProcessCaptureCapability.js";
|
||||
import {
|
||||
createProcessCaptureProgressTracker,
|
||||
type ProcessCaptureProgress,
|
||||
type ProcessCaptureProgressCounts,
|
||||
type ProcessCaptureProgressDisposition,
|
||||
type ProcessCaptureProgressTracker,
|
||||
} from "./ProcessCaptureProgress.js";
|
||||
import type { ProcessOwnershipBaseline } from "../ProcessOwnership.js";
|
||||
|
||||
interface StartedCaptureRuntime {
|
||||
@@ -262,7 +269,10 @@ const finishProcessRun = async (options: {
|
||||
readonly executionFailure: unknown;
|
||||
readonly observations?: ProcessCaptureObservationBuffer;
|
||||
readonly rootPid?: number;
|
||||
readonly progress: ProcessCaptureProgressTracker;
|
||||
readonly progressCounts: () => ProcessCaptureProgressCounts;
|
||||
}): Promise<ProcessCapture> => {
|
||||
options.progress.closeLive();
|
||||
await options.stopSampler();
|
||||
const sampledProcessGroupIds =
|
||||
options.runtime === undefined
|
||||
@@ -281,6 +291,20 @@ const finishProcessRun = async (options: {
|
||||
temporaryRoot: options.temporaryRoot,
|
||||
...(options.cleanupHost === undefined ? {} : { host: options.cleanupHost }),
|
||||
});
|
||||
const observedExit =
|
||||
options.observations?.exit.state === "available"
|
||||
? options.observations.exit.value.reason
|
||||
: options.capture?.exit.reason;
|
||||
await options.progress.report({
|
||||
phase: "cleanup",
|
||||
counts: options.progressCounts(),
|
||||
terminal: true,
|
||||
disposition: captureProgressDisposition(
|
||||
observedExit,
|
||||
options.executionFailure,
|
||||
),
|
||||
cleanup,
|
||||
});
|
||||
const cleanupFailure = cleanupReportFailure(cleanup);
|
||||
let { capture, executionFailure } = options;
|
||||
let verifiedCapture: ProcessCapture | undefined;
|
||||
@@ -336,6 +360,7 @@ const completeCapture = async (options: {
|
||||
readonly eventJournal: readonly ProcessCaptureEventJournalEntry[];
|
||||
readonly observationBuffer: ProcessCaptureObservationBuffer;
|
||||
readonly recordEvent: RecordProcessCaptureEvent;
|
||||
readonly onLiveProgress?: () => void;
|
||||
}): Promise<PendingProcessCapture> => {
|
||||
const unavailableReason = (stage: string, cause: unknown): string =>
|
||||
`${stage} failed: ${cause instanceof Error ? cause.message : "unknown failure"}`;
|
||||
@@ -351,6 +376,7 @@ const completeCapture = async (options: {
|
||||
options.recordEvent,
|
||||
options.hostPlatform,
|
||||
options.signal,
|
||||
options.onLiveProgress,
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
options.observationBuffer.settlement = {
|
||||
@@ -460,6 +486,25 @@ const completeCapture = async (options: {
|
||||
});
|
||||
};
|
||||
|
||||
const captureProgressDisposition = (
|
||||
exitReason: string | undefined,
|
||||
executionFailure: unknown,
|
||||
): ProcessCaptureProgressDisposition => {
|
||||
if (
|
||||
exitReason === "exited" ||
|
||||
exitReason === "timeout" ||
|
||||
exitReason === "idle_timeout" ||
|
||||
exitReason === "cancelled"
|
||||
)
|
||||
return exitReason;
|
||||
if (
|
||||
executionFailure instanceof ProcessCaptureError &&
|
||||
executionFailure.reason === "cancelled"
|
||||
)
|
||||
return "cancelled";
|
||||
return "failed";
|
||||
};
|
||||
|
||||
/** Execute one caller-selected scenario and return bounded observations. */
|
||||
const runProcessScenario = async (
|
||||
scenario: ProcessScenario,
|
||||
@@ -468,12 +513,44 @@ const runProcessScenario = async (
|
||||
hostPlatform: NodeJS.Platform = process.platform,
|
||||
captureSnapshot: typeof snapshotRoots = snapshotRoots,
|
||||
cleanupHost?: ProcessCaptureCleanupHost,
|
||||
progress?: ProcessCaptureProgress,
|
||||
): Promise<ProcessCapture> => {
|
||||
const { temporaryRoot, runId, ownershipBaseline, before } =
|
||||
await prepareProcessCapture(scenario, signal, captureSnapshot);
|
||||
const progressTracker = createProcessCaptureProgressTracker(progress);
|
||||
const frames: TerminalFrame[] = [];
|
||||
const samples: ProcessSample[] = [];
|
||||
const interactions: InteractionEvent[] = [];
|
||||
const progressCounts = (): ProcessCaptureProgressCounts => ({
|
||||
frames: frames.length,
|
||||
samples: samples.length,
|
||||
interactions: interactions.length,
|
||||
});
|
||||
const reportRunning = () => {
|
||||
void progressTracker.report({ phase: "running", counts: progressCounts() });
|
||||
};
|
||||
const reportSettling = () => {
|
||||
void progressTracker.report({
|
||||
phase: "settling",
|
||||
counts: progressCounts(),
|
||||
});
|
||||
};
|
||||
let prepared: Awaited<ReturnType<typeof prepareProcessCapture>>;
|
||||
try {
|
||||
await progressTracker.report({
|
||||
phase: "prepare",
|
||||
counts: progressCounts(),
|
||||
});
|
||||
prepared = await prepareProcessCapture(scenario, signal, captureSnapshot);
|
||||
} catch (cause: unknown) {
|
||||
progressTracker.closeLive();
|
||||
await progressTracker.report({
|
||||
phase: "cleanup",
|
||||
counts: progressCounts(),
|
||||
terminal: true,
|
||||
disposition: signal?.aborted === true ? "cancelled" : "failed",
|
||||
});
|
||||
throw cause;
|
||||
}
|
||||
const { temporaryRoot, runId, ownershipBaseline, before } = prepared;
|
||||
const journal = createProcessCaptureJournal();
|
||||
const { entries: eventJournal, recordEvent } = journal;
|
||||
recordEvent("filesystem_checkpoints", 0);
|
||||
@@ -523,6 +600,7 @@ const runProcessScenario = async (
|
||||
interactions,
|
||||
dispatchedEventIndexes,
|
||||
recordEvent,
|
||||
...(progress === undefined ? {} : { onLiveProgress: reportRunning }),
|
||||
});
|
||||
observations.exit = {
|
||||
state: "available",
|
||||
@@ -549,6 +627,7 @@ const runProcessScenario = async (
|
||||
recordEvent,
|
||||
captureSnapshot,
|
||||
...(signal === undefined ? {} : { signal }),
|
||||
...(progress === undefined ? {} : { onLiveProgress: reportSettling }),
|
||||
});
|
||||
} catch (cause: unknown) {
|
||||
executionFailure = normalizeCaptureFailure(cause, signal);
|
||||
@@ -590,6 +669,8 @@ const runProcessScenario = async (
|
||||
executionFailure,
|
||||
observations,
|
||||
...(actualRootPid === undefined ? {} : { rootPid: actualRootPid }),
|
||||
progress: progressTracker,
|
||||
progressCounts,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -599,8 +680,9 @@ export const captureProcessScenario = async (
|
||||
signal?: AbortSignal,
|
||||
platform: NodeJS.Platform = process.platform,
|
||||
environment: Readonly<Record<string, string | undefined>> = process.env,
|
||||
captureSnapshot: typeof snapshotRoots = snapshotRoots,
|
||||
captureSnapshot?: typeof snapshotRoots,
|
||||
cleanupHost?: ProcessCaptureCleanupHost,
|
||||
progress?: ProcessCaptureProgress,
|
||||
): Promise<Result<ProcessCapture, ProcessCaptureError | AnalysisError>> => {
|
||||
const ownershipReason = processCaptureOwnershipUnavailableReason(platform);
|
||||
if (ownershipReason !== undefined)
|
||||
@@ -624,8 +706,9 @@ export const captureProcessScenario = async (
|
||||
signal,
|
||||
environment,
|
||||
platform,
|
||||
captureSnapshot,
|
||||
captureSnapshot ?? snapshotRoots,
|
||||
cleanupHost,
|
||||
progress,
|
||||
),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
|
||||
Reference in New Issue
Block a user