fix(process): localize capture truncation diagnostics (#1284)
Report per-source capture coverage and existing file-hash omissions while preserving comparable observations in unaffected dimensions. Keep legacy truncated captures conservative and retain known partial trace events. Closes #1250 Partially addresses #1251; independent hashing remains out of scope.
This commit is contained in:
@@ -207,16 +207,16 @@ captureTest(
|
||||
60_000,
|
||||
);
|
||||
|
||||
captureTest.each(["unknown", "truncated"] as const)(
|
||||
captureTest.each(["untruncated", "truncated"] as const)(
|
||||
"captures real process choices and composes a %s self-comparison",
|
||||
async (status) => {
|
||||
async (captureState) => {
|
||||
const root = await createTestTempDirectory("rea-local-capture-");
|
||||
const { call } = await connectLocalToolsMcp();
|
||||
const captured = await call("capture_process_scenario", {
|
||||
executable: process.execPath,
|
||||
arguments: [
|
||||
"-e",
|
||||
`require('node:fs').writeFileSync('state','selected');console.log(process.env['app.setting'],process.env.MY_PASSWORD,process.env['REA_PROCESS_RUN_ID'+String.fromCharCode(10)]);${status === "truncated" ? "console.log('x'.repeat(5000))" : ""}`,
|
||||
`require('node:fs').writeFileSync('state','selected');console.log(process.env['app.setting'],process.env.MY_PASSWORD,process.env['REA_PROCESS_RUN_ID'+String.fromCharCode(10)]);${captureState === "truncated" ? "console.log('x'.repeat(5000))" : ""}`,
|
||||
],
|
||||
working_directory: root,
|
||||
environment: {
|
||||
@@ -226,14 +226,14 @@ captureTest.each(["unknown", "truncated"] as const)(
|
||||
"REA_PROCESS_RUN_ID\n": "caller-value",
|
||||
},
|
||||
filesystem_observation_paths: [root],
|
||||
limits: status === "truncated" ? { output_bytes: 128 } : {},
|
||||
limits: captureState === "truncated" ? { output_bytes: 128 } : {},
|
||||
});
|
||||
expect(captured.isError, JSON.stringify(captured)).not.toBe(true);
|
||||
const capture = toolContract("capture_process_scenario").outputSchema.parse(
|
||||
captured.structuredContent,
|
||||
);
|
||||
const source = capture;
|
||||
if (status === "unknown")
|
||||
if (captureState === "untruncated")
|
||||
expect(
|
||||
capture.normalized_result.frames.map((frame) => frame.data).join(""),
|
||||
).toContain("value ordinary-evidence caller-value");
|
||||
@@ -266,7 +266,13 @@ captureTest.each(["unknown", "truncated"] as const)(
|
||||
const comparison = toolContract(
|
||||
"compare_process_captures",
|
||||
).outputSchema.parse(compared.structuredContent);
|
||||
expect(comparison.normalized_result.status).toBe(status);
|
||||
expect(comparison.normalized_result).toMatchObject({
|
||||
status: "unknown",
|
||||
terminal: captureState === "truncated" ? "unknown" : "unchanged",
|
||||
interaction: "unchanged",
|
||||
exit: "unchanged",
|
||||
process: "unknown",
|
||||
});
|
||||
const verification = await call("verify_reconstruction", {
|
||||
specification: {
|
||||
name: "Self comparison retains uncertainty",
|
||||
@@ -304,7 +310,7 @@ captureTest.each(["unknown", "truncated"] as const)(
|
||||
});
|
||||
expect(behavior.isError, JSON.stringify(behavior)).not.toBe(true);
|
||||
expect(behavior.structuredContent).toMatchObject({
|
||||
normalized_result: { behavior_status: status },
|
||||
normalized_result: { behavior_status: "unknown" },
|
||||
});
|
||||
const unknowns = await call("list_unknowns", {
|
||||
domain: "process-comparison",
|
||||
|
||||
@@ -129,7 +129,7 @@ itWithLinuxCaptureCapability.each([false, true])(
|
||||
state: "quiesced",
|
||||
cleanup_outcome: "not_required",
|
||||
});
|
||||
expect(capture.cleanup).toEqual({
|
||||
expect(capture.cleanup).toMatchObject({
|
||||
owned_process_group: "verified",
|
||||
temporary_root: "removed",
|
||||
});
|
||||
|
||||
@@ -0,0 +1,270 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
||||
import { expect, onTestFinished } from "vitest";
|
||||
import { parseEvidence } from "../../../src/domain/evidence.js";
|
||||
import {
|
||||
compareProcessCaptures,
|
||||
parseProcessCapture,
|
||||
processCaptureSchema,
|
||||
} from "../../../src/domain/process/processCapture.js";
|
||||
import { processSourceTruncated } from "../../../src/domain/process/processCaptureCoverage.js";
|
||||
import { compareProcessTraces } from "../../../src/domain/process/processTraceComparison.js";
|
||||
import { createServer } from "../../../src/server/createServer.js";
|
||||
import { createTestBinarySession } from "../../fixtures/binarySession.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { itWithCaptureCapability } from "./processCaptureCapability.js";
|
||||
|
||||
const exec = promisify(execFile);
|
||||
const capture = async (
|
||||
adapter: "cli" | "mcp",
|
||||
scenario: Record<string, unknown>,
|
||||
) => {
|
||||
if (adapter === "cli") {
|
||||
const path = join(
|
||||
await createTestTempDirectory("rea-truncation-scenario-"),
|
||||
"scenario.json",
|
||||
);
|
||||
await writeFile(path, JSON.stringify(scenario));
|
||||
const { stdout } = await exec(process.execPath, [
|
||||
"scripts/rea.mjs",
|
||||
"capture-process",
|
||||
path,
|
||||
"--format",
|
||||
"json",
|
||||
]);
|
||||
return parseProcessCapture(
|
||||
parseEvidence(JSON.parse(stdout)).normalized_result,
|
||||
);
|
||||
}
|
||||
const session = createTestBinarySession(() => {
|
||||
throw new Error("Capture must not launch a binary provider");
|
||||
});
|
||||
const server = createServer(session, session);
|
||||
const client = new Client({ name: "truncation-coverage", version: "1" });
|
||||
onTestFinished(async () => {
|
||||
await client.close();
|
||||
await server.close();
|
||||
await session.close();
|
||||
});
|
||||
const [clientTransport, serverTransport] =
|
||||
InMemoryTransport.createLinkedPair();
|
||||
await server.connect(serverTransport);
|
||||
await client.connect(clientTransport);
|
||||
const result = await client.callTool({
|
||||
name: "capture_process_scenario",
|
||||
arguments: scenario,
|
||||
});
|
||||
expect(result.isError, JSON.stringify(result)).not.toBe(true);
|
||||
await client.ping();
|
||||
return parseProcessCapture(
|
||||
parseEvidence(result.structuredContent).normalized_result,
|
||||
);
|
||||
};
|
||||
|
||||
itWithCaptureCapability.each(["cli", "mcp"] as const)(
|
||||
"localizes sparse rendered-state exhaustion through %s",
|
||||
async (adapter) => {
|
||||
const root = await createTestTempDirectory("rea-rendered-budget-");
|
||||
const result = await capture(adapter, {
|
||||
executable: process.execPath,
|
||||
working_directory: root,
|
||||
arguments: [
|
||||
"-e",
|
||||
"let n=0;const timer=setInterval(()=>{console.log('board '+String(n).padStart(2,'0')+' completed; score '+String(n*10));if(++n===65)clearInterval(timer)},20)",
|
||||
],
|
||||
normalization: {
|
||||
ports: false,
|
||||
pids: false,
|
||||
paths: false,
|
||||
time_bucket_ms: 1,
|
||||
},
|
||||
limits: { output_bytes: 30_000 },
|
||||
});
|
||||
const details = result.truncation_details;
|
||||
if (details === undefined) throw new Error("Expected producer coverage");
|
||||
expect(result.frames.map(({ data }) => data).join("")).toContain(
|
||||
"board 64 completed; score 640",
|
||||
);
|
||||
expect(details.raw_terminal.observed_bytes).toBe(2004);
|
||||
expect(details.raw_terminal.retained_bytes).toBe(
|
||||
details.raw_terminal.observed_bytes,
|
||||
);
|
||||
expect(details.raw_terminal.observed_frames).toBe(result.frames.length);
|
||||
expect(details.rendered_terminal.observed_frames).toBeGreaterThan(
|
||||
result.rendered_frames.length,
|
||||
);
|
||||
expect(details.rendered_terminal.retained_bytes).toBeLessThanOrEqual(
|
||||
30_000,
|
||||
);
|
||||
expect(details.process.coverage).toBe("sampled");
|
||||
expect(processSourceTruncated(result, "terminal_raw")).toBe(false);
|
||||
expect(processSourceTruncated(result, "terminal_rendered")).toBe(true);
|
||||
const events = result.frames.map(({ sequence, at_ms, data }) => ({
|
||||
id: `chunk_${String(sequence)}`,
|
||||
source: "terminal_raw" as const,
|
||||
exact: { sequence, at_ms, data },
|
||||
cardinality: { kind: "required" as const },
|
||||
}));
|
||||
const specification = {
|
||||
events,
|
||||
language: {
|
||||
kind: "finite_traces" as const,
|
||||
variants: [{ id: "observed", trace: events.map(({ id }) => id) }],
|
||||
},
|
||||
};
|
||||
expect(compareProcessTraces(result, result, specification).verdict).toBe(
|
||||
"equivalent",
|
||||
);
|
||||
const legacy = parseProcessCapture({
|
||||
...result,
|
||||
truncation_details: undefined,
|
||||
});
|
||||
expect(compareProcessTraces(legacy, legacy, specification).verdict).toBe(
|
||||
"unknown",
|
||||
);
|
||||
expect(compareProcessCaptures(result, result)).toMatchObject({
|
||||
terminal: "unknown",
|
||||
interaction: "unchanged",
|
||||
exit: "unchanged",
|
||||
});
|
||||
expect(
|
||||
processCaptureSchema.safeParse({ ...result, truncated: false }).success,
|
||||
).toBe(false);
|
||||
},
|
||||
20_000,
|
||||
);
|
||||
|
||||
itWithCaptureCapability.each(["cli", "mcp"] as const)(
|
||||
"distinguishes raw chunk omission and filesystem exhaustion through %s",
|
||||
async (adapter) => {
|
||||
const root = await createTestTempDirectory("rea-raw-files-budget-");
|
||||
await writeFile(join(root, "a.txt"), "abc");
|
||||
await writeFile(join(root, "b.txt"), "12345678");
|
||||
const result = await capture(adapter, {
|
||||
executable: process.execPath,
|
||||
working_directory: root,
|
||||
arguments: [
|
||||
"-e",
|
||||
"process.stdout.write('x'.repeat(1000));require('node:fs').writeFileSync('c.txt','new');setTimeout(()=>{},100)",
|
||||
],
|
||||
filesystem_observation_paths: [root],
|
||||
limits: { output_bytes: 50, files: 3, file_bytes: 4 },
|
||||
});
|
||||
const details = result.truncation_details;
|
||||
if (details === undefined) throw new Error("Expected producer coverage");
|
||||
expect(details.raw_terminal.observed_bytes).toBe(1000);
|
||||
expect(details.raw_terminal.retained_bytes).toBeLessThan(
|
||||
details.raw_terminal.observed_bytes,
|
||||
);
|
||||
expect(details.filesystem_before).toMatchObject({
|
||||
enumeration_truncated: false,
|
||||
hashed_bytes: 3,
|
||||
hash_omissions: [
|
||||
{
|
||||
path: "root_0:b.txt",
|
||||
size_bytes: 8,
|
||||
remaining_budget_bytes: 1,
|
||||
reason: "file_bytes_budget",
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(details.filesystem_after.enumeration_reasons).toContain(
|
||||
"files_limit",
|
||||
);
|
||||
expect(
|
||||
result.files_after.find(({ path }) => path === "root_0:b.txt")?.sha256,
|
||||
).toBeNull();
|
||||
expect(processSourceTruncated(result, "terminal_rendered")).toBe(true);
|
||||
expect(compareProcessCaptures(result, result)).toMatchObject({
|
||||
terminal: "unknown",
|
||||
filesystem: "unknown",
|
||||
interaction: "unchanged",
|
||||
exit: "unchanged",
|
||||
});
|
||||
},
|
||||
20_000,
|
||||
);
|
||||
|
||||
itWithCaptureCapability.each(["cli", "mcp"] as const)(
|
||||
"keeps terminal facts comparable when only file digests are omitted through %s",
|
||||
async (adapter) => {
|
||||
const root = await createTestTempDirectory("rea-hash-budget-");
|
||||
await writeFile(join(root, "a.txt"), "abc");
|
||||
await writeFile(join(root, "b.txt"), "12345678");
|
||||
await writeFile(join(root, "c.txt"), "z");
|
||||
const result = await capture(adapter, {
|
||||
executable: process.execPath,
|
||||
working_directory: root,
|
||||
arguments: ["-e", "console.log('done');setTimeout(()=>{},100)"],
|
||||
filesystem_observation_paths: [root],
|
||||
limits: { file_bytes: 4 },
|
||||
});
|
||||
expect(result.truncation_details?.filesystem_after).toMatchObject({
|
||||
enumeration_truncated: false,
|
||||
hashed_bytes: 4,
|
||||
hash_omissions: [
|
||||
{
|
||||
path: "root_0:b.txt",
|
||||
reason: "file_bytes_budget",
|
||||
remaining_budget_bytes: 1,
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(
|
||||
result.files_after.find(({ path }) => path === "root_0:c.txt")?.sha256,
|
||||
).toMatch(/^[a-f0-9]{64}$/u);
|
||||
expect(compareProcessCaptures(result, result)).toMatchObject({
|
||||
terminal: "unchanged",
|
||||
filesystem: "unknown",
|
||||
interaction: "unchanged",
|
||||
exit: "unchanged",
|
||||
});
|
||||
const differentExit = parseProcessCapture({
|
||||
...result,
|
||||
exit: { ...result.exit, code: 1 },
|
||||
});
|
||||
expect(compareProcessCaptures(result, differentExit)).toMatchObject({
|
||||
status: "changed",
|
||||
terminal: "unchanged",
|
||||
filesystem: "unknown",
|
||||
exit: "changed",
|
||||
first_divergence: { status: "unknown" },
|
||||
});
|
||||
},
|
||||
20_000,
|
||||
);
|
||||
|
||||
itWithCaptureCapability.each(["cli", "mcp"] as const)(
|
||||
"reports process sample-limit exhaustion without erasing other dimensions through %s",
|
||||
async (adapter) => {
|
||||
const root = await createTestTempDirectory("rea-process-budget-");
|
||||
const result = await capture(adapter, {
|
||||
executable: process.execPath,
|
||||
working_directory: root,
|
||||
arguments: [
|
||||
"-e",
|
||||
"setTimeout(()=>require('node:child_process').spawn(process.execPath,['-e','setTimeout(()=>{},800)'],{stdio:'ignore'}),100);console.log('ready')",
|
||||
],
|
||||
limits: { processes: 1 },
|
||||
});
|
||||
expect(result.truncation_details?.process).toMatchObject({
|
||||
sample_limit: 1,
|
||||
sampling_partial: true,
|
||||
retained_samples: 1,
|
||||
sample_limit_reached: true,
|
||||
sampling_failures: 0,
|
||||
first_sampling_failure: null,
|
||||
coverage: "sampled",
|
||||
});
|
||||
expect(compareProcessCaptures(result, result)).toMatchObject({
|
||||
terminal: "unchanged",
|
||||
interaction: "unchanged",
|
||||
exit: "unchanged",
|
||||
process: "unknown",
|
||||
});
|
||||
},
|
||||
20_000,
|
||||
);
|
||||
Reference in New Issue
Block a user