Files
reverse-engineering-anything/tests/boundary/process/processSessionMcp.test.ts
T
2SinceJulyand2SinceJuly 6c08320654 feat(process): add a non-executing dry run to capture_process_scenario (#8469)
* feat(process): add a non-executing dry run to capture_process_scenario

Preparation prompts asked an agent to prepare a scenario without running
it but could only suggest the tool that launches the target. Add
dry_run: true so the CLI and MCP resolve the executable, working
directory, observation paths and admission checks, then return planned
Evidence under the rea.process-scenario-plan predicate. Planned Evidence
is not capture Evidence and comparison tools refuse it as a capture.

The preparation prompt now suggests the dry-run call, and the scenario
identity commitments exclude the operation-only field.

Fixes #8398

* fix(process): redact planned sensitive input and share admission

Address review findings on the dry-run contract:

- Redact sensitive input events in planned Evidence and preserve only
  their byte length.
- Recheck cancellation after the last awaited admission step.
- Run the sensitive-matcher and filesystem-root admission checks that a
  capture performs before launch.
- Assert the prompt advertises the dry_run field without freezing
  explanatory wording.

* test(process): hold the final admission step for cancellation

Add a preparation host seam so the regression can cancel while the last
filesystem admission call is pending, then assert the returned
AnalysisCancelledError retains the caller's reason.

* refactor(process): remove the unused plan alias

Keep the plan schema as the single exported contract and satisfy the
unused-export check.

* fix(process): report admission-time aborts as planning cancellation

`prepareProcessScenarioPlan` returned the admission error verbatim, so an
abort that landed while `admitFilesystemObservationRoots` awaited its
`lstat` surfaced as a cancelled `ProcessCaptureError` with
`operation: process_capture` even though no process ran. Convert that
result through the same `cancelled()` projection the neighbouring checks
use, and pin it with a test.

* fix(contracts): keep the process result invariants on the dry-run union

The output schema for `capture_process_scenario` became a union of the
capture and plan shapes, and the union no longer carried the description
that advertised the cross-field invariants to MCP clients — the boundary
test reads `normalized_result.description`. Attach a description to the
union that names both variants and keeps the capture invariants verbatim.

* test(process): exercise the post-admission cancellation check

The previous regression aborted immediately after starting preparation and
released the held admission afterwards. Preparation awaits runtime path
resolution first, so that abort was always observed by the pre-admission
check; deleting the post-admission "if (aborted()) return cancelled();"
still left the test green.

Signal from the admitRoots seam when admission is actually entered, abort
only after that point, then let admission resolve ok. Preparation now has
to reach the check between a successful admission and the ok result, and
the assertion pins both the AnalysisCancelledError tag and the caller's
reason as its cause.

Verified red-green: removing only the post-admission check fails this test
(expected true to be false at prepared.ok) while the other two pass, and
restoring it passes all three.

* test(process): gate the admission seam on acknowledgement

Race the admission acknowledgement against planning's own settlement and a
bounded deadline, and release and join planning in `finally`, so a planning
result that arrives before admission fails the test instead of stalling it
until the suite timeout.

* fix(contracts): hold plans to the scenario refinements and redact inputs

`processScenarioPlanSchema` described the resolved dry-run response but
dropped every cross-field check the input contract applies, so a plan
could advertise out-of-order events, an event past `timeout_ms`, or a
terminal beyond the renderer cell budget while claiming it had been
validated. Extract those checks into `scenarioRefinements` and refine the
plan's scenario with them.

The plan's input event also carried `sensitive` and an optional
`input_bytes`, which accepted a sensitive event that still shipped its
data. Model the public and sensitive forms as separate exact variants:
public input forbids `input_bytes`, sensitive input requires an empty
`data` plus its byte length.

---------

Co-authored-by: 2SinceJuly <202121119+2SinceJuly@users.noreply.github.com>
2026-11-06 20:49:20 +08:00

350 lines
12 KiB
TypeScript

import { fileURLToPath } from "node:url";
import { access, chmod, mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { Ajv2020 } from "ajv/dist/2020.js";
import { afterEach, expect, it } from "vitest";
import { z } from "zod";
import { toolContract } from "../../../src/contracts/toolContracts.js";
import {
createCacheProvider,
createTestBinarySession,
} from "../../fixtures/binarySession.js";
import { createServer } from "../../../src/server/createServer.js";
import { silentLogger } from "../../../src/logger.js";
import type { ProcessCapture } from "../../../src/domain/process/processCaptureParsing.js";
import { itWithCaptureCapability } from "./processCaptureCapability.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const processFixture = fileURLToPath(
new URL("../../fixtures/processFidelity.mjs", import.meta.url),
);
const resources: Array<{ close(): Promise<unknown> }> = [];
const captureOutput = (value: unknown): ProcessCapture => {
const normalized = toolContract(
"capture_process_scenario",
).outputSchema.parse(value).normalized_result;
if ("status" in normalized && normalized.status === "planned")
throw new Error("expected process capture output");
return normalized as ProcessCapture;
};
afterEach(async () => {
await Promise.all(resources.splice(0).map((resource) => resource.close()));
});
it("rejects oversized initial and resize terminal buffers through MCP before launching", async () => {
const root = await createTestTempDirectory("rea-terminal-admission-");
const marker = join(root, "launched");
const session = createTestBinarySession(() => {
throw new Error("Terminal admission must not launch a binary provider");
});
const server = createServer(
{ kind: "session", session },
{ logger: silentLogger },
);
const client = new Client({ name: "terminal-admission", version: "1" });
resources.push(client, server, session);
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
await client.connect(clientTransport);
for (const input of [
{ terminal: { columns: 1_000, rows: 1, scrollback: 1_000 } },
{ events: [{ type: "resize", at_ms: 10, columns: 1_000, rows: 1 }] },
] as const) {
const result = await client.callTool({
name: "capture_process_scenario",
arguments: {
executable: process.execPath,
arguments: [
"-e",
"require('node:fs').writeFileSync(process.argv[1], 'launched')",
marker,
],
...input,
},
});
expect(result.isError).toBe(true);
expect(result.content).toContainEqual(
expect.objectContaining({
type: "text",
text: expect.stringContaining("1000000"),
}),
);
await expect(access(marker)).rejects.toMatchObject({ code: "ENOENT" });
}
await client.ping();
});
itWithCaptureCapability(
"returns a planned scenario without launching the target",
async () => {
const root = await createTestTempDirectory("rea-process-plan-");
const marker = join(root, "launched");
const session = createTestBinarySession(() => {
throw new Error("A dry run must not launch a binary provider");
});
const server = createServer(
{ kind: "session", session },
{ logger: silentLogger },
);
const client = new Client({ name: "process-plan", version: "1" });
resources.push(client, server, session);
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
await client.connect(clientTransport);
const result = await client.callTool({
name: "capture_process_scenario",
arguments: {
executable: process.execPath,
working_directory: root,
arguments: [
"-e",
"require('node:fs').writeFileSync(process.argv[1], 'launched')",
marker,
],
dry_run: true,
},
});
expect(result.isError, JSON.stringify(result.content)).not.toBe(true);
await expect(access(marker)).rejects.toMatchObject({ code: "ENOENT" });
const parsed = toolContract("capture_process_scenario").outputSchema.parse(
result.structuredContent,
);
expect(parsed.predicate_type).toBe("rea.process-scenario-plan");
expect(parsed.normalized_result).toMatchObject({
status: "planned",
ambient_environment: "inherited-at-run",
scenario: { executable: process.execPath },
});
},
);
itWithCaptureCapability(
"bounds normalized PTY text and preserves equal-size output through MCP",
async () => {
const root = await createTestTempDirectory(
"rea-process-normalized-budget-",
);
const session = createTestBinarySession(createCacheProvider([]));
const server = createServer(
{ kind: "session", session },
{ logger: silentLogger },
);
const client = new Client({
name: "process-normalized-budget",
version: "1",
});
resources.push(client, server, session);
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
await client.connect(clientTransport);
const capture = async (replacement: string) => {
const result = await client.callTool({
name: "capture_process_scenario",
arguments: {
executable: process.execPath,
working_directory: root,
arguments: ["-e", 'process.stdout.write("a".repeat(100) + "\\n")'],
limits: { output_bytes: 1_000 },
normalization: {
paths: false,
pids: false,
ports: false,
patterns: [{ pattern: "a", replacement }],
},
settle_ms: 50,
},
});
expect(result.isError, JSON.stringify(result.content)).not.toBe(true);
return captureOutput(result.structuredContent);
};
const expanded = await capture("x".repeat(500));
expect(expanded.truncated).toBe(true);
const retainedNormalized = expanded.frames.map(({ data }) => data).join("");
const retainedRaw = expanded.frames
.map(({ raw_data, data }) => raw_data ?? data)
.join("");
// PTY read boundaries need not match the child's write. A small chunk can
// fit even after another chunk was omitted for exceeding the budget.
expect(retainedNormalized).toBe(
retainedRaw.replaceAll("a", "x".repeat(500)),
);
expect(Buffer.byteLength(retainedNormalized)).toBeLessThanOrEqual(1_000);
expect(expanded.truncation_details.normalized_terminal).toMatchObject({
budget_bytes: 1_000,
retained_bytes: Buffer.byteLength(retainedNormalized),
omitted_frames: expect.any(Number),
});
expect(
expanded.truncation_details.normalized_terminal?.omitted_frames,
).toBeGreaterThan(0);
expect(expanded.truncation_details.raw_terminal).toMatchObject({
budget_bytes: 1_000,
observed_bytes: expect.any(Number),
retained_bytes: Buffer.byteLength(retainedRaw),
observed_frames: expect.any(Number),
retained_frames: expanded.frames.length,
});
expect(
expanded.limitations.some((limitation) =>
limitation.includes(
"final normalized sizes for the latter omissions are unknown",
),
),
).toBe(true);
expect(expanded.cleanup).toMatchObject({
owned_process_group: "verified",
temporary_root: "removed",
});
const equalSize = await capture("b");
// This short-lived target can exit before process sampling finishes.
// Assert the terminal coverage that this normalization control exercises.
expect(equalSize.truncation_details.raw_terminal).toMatchObject({
observed_bytes: Buffer.byteLength(`${"a".repeat(100)}\r\n`),
retained_bytes: Buffer.byteLength(`${"a".repeat(100)}\r\n`),
});
expect(equalSize.frames.map(({ data }) => data).join("")).toBe(
`${"b".repeat(100)}\r\n`,
);
expect(equalSize.truncation_details.normalized_terminal).toMatchObject({
budget_bytes: 1_000,
retained_bytes: Buffer.byteLength(`${"b".repeat(100)}\r\n`),
omitted_frames: 0,
});
expect(equalSize.cleanup).toMatchObject({
owned_process_group: "verified",
temporary_root: "removed",
});
},
30_000,
);
itWithCaptureCapability(
"records process residuals linked to capture Evidence",
async () => {
const session = createTestBinarySession(createCacheProvider([]));
const server = createServer(
{ kind: "session", session },
{
logger: silentLogger,
},
);
const mcp = new Client({ name: "process-unknown", version: "1.0.0" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
resources.push(mcp, server);
await server.connect(serverTransport);
await mcp.connect(clientTransport);
const captured = await mcp.callTool({
name: "capture_process_scenario",
arguments: {
executable: process.execPath,
arguments: [processFixture, "partial"],
},
});
expect(captured.isError, JSON.stringify(captured.content)).not.toBe(true);
const contract = toolContract("capture_process_scenario");
const wire = (await mcp.listTools()).tools.find(
({ name }) => name === contract.name,
);
if (wire?.outputSchema === undefined)
throw new Error("Missing process capture output schema");
expect(
new Ajv2020({ strict: false, validateFormats: false }).validate(
z.record(z.string(), z.unknown()).parse(wire.outputSchema),
captured.structuredContent,
),
).toBe(true);
const evidence = contract.outputSchema.parse(captured.structuredContent);
const listedUnknowns = await mcp.callTool({
name: "list_unknowns",
arguments: {},
});
expect(
listedUnknowns.isError,
JSON.stringify(listedUnknowns.content),
).not.toBe(true);
const listed = z
.object({
result: z.object({
items: z.array(
z.object({
question: z.string(),
domain: z.string(),
}),
),
}),
})
.parse(listedUnknowns.structuredContent).result.items;
expect(listed).toContainEqual(
expect.objectContaining({
question: `Was network behavior fully observed during capture? (Evidence ${evidence.evidence_id})`,
domain: "process-network",
}),
);
},
);
itWithCaptureCapability(
"resolves and inherits the server's selected environment through MCP",
async () => {
const root = await createTestTempDirectory("rea-process-mcp-env-");
const bin = join(root, "bin");
const executable = join(bin, "rea-injected-process-probe");
await mkdir(bin);
await writeFile(
executable,
[
"#!/bin/sh",
'printf "%s|%s\\n" "$REA_CAPTURE_INJECTED_MARKER" "$REA_CAPTURE_SCENARIO_OVERRIDE"',
].join("\n"),
);
await chmod(executable, 0o755);
const session = createTestBinarySession(createCacheProvider([]));
const server = createServer(
{ kind: "session", session },
{
logger: silentLogger,
environment: {
PATH: bin,
REA_CAPTURE_INJECTED_MARKER: "injected-value",
REA_CAPTURE_SCENARIO_OVERRIDE: "host-value",
},
},
);
const client = new Client({ name: "process-mcp-env", version: "1" });
resources.push(client, server, session);
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
await client.connect(clientTransport);
const result = await client.callTool({
name: "capture_process_scenario",
arguments: {
executable: "rea-injected-process-probe",
working_directory: root,
environment: { REA_CAPTURE_SCENARIO_OVERRIDE: "scenario-value" },
},
});
expect(result.isError, JSON.stringify(result.content)).not.toBe(true);
const capture = captureOutput(result.structuredContent);
expect(capture.frames.map(({ data }) => data).join("")).toContain(
"injected-value|scenario-value",
);
},
);