mirror of
https://github.com/morluto/rea.git
synced 2026-12-02 13:11:18 -05:00
402ce20b80
* fix(contracts): declare the open-target prerequisite once in availability Replace the requiresActiveTarget boolean with an "active-target" value of the availability declaration. 18 contracts that read a required caller path, all declared standalone or host-restricted or named inventory rules, no longer claim to need an open target. * fix(session): admit tools through one session evaluator shared with binary_session binary_session availability and call-time dispatch now read one admission evaluator for active-target tools and external mutations. Resource-only lane refusals and Ghidra or resource-only external-mutation refusals carry the open_binary or close_binary recovery that binary_session reports, instead of a generic capability or unreadable-output error. Packet capture, Wakaru, firmware and mitmproxy refuse an unsupported host before reading tool configuration. * fix(mcp): advertise the open-target prerequisite and name open_binary on first contact Each active-target tool's advertised description ends with one sentence derived from its availability declaration. When the SDK refuses such a tool's arguments while the session would also refuse it, the invalid_request remediation names the session recovery first, read from the same admission evaluator as binary_session and dispatch. * fix(mcp): refuse composed workflows as a whole and keep scoped recovery and catalog identity in step A composed workflow such as batch_decompile now refuses with the advertised open_binary or close_binary recovery before it runs, instead of reporting a batch of failed children. Malformed arguments on a connection that does not advertise session tools lead with the scoped restart recovery, and catalog digests cover the advertised prerequisite sentence. The prerequisite test reads union alternatives.
1150 lines
40 KiB
TypeScript
1150 lines
40 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import type { AnalysisError } from "../../../src/domain/analysisErrorBase.js";
|
|
import { ProviderProcessSupervisor } from "../../../src/process/ProviderProcess.js";
|
|
import { PrivateRuntimeRoot } from "../../../src/process/PrivateRuntimeRoot.js";
|
|
import {
|
|
access,
|
|
chmod,
|
|
mkdir,
|
|
readFile,
|
|
stat,
|
|
symlink,
|
|
truncate,
|
|
writeFile,
|
|
} from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { expect, it, onTestFinished, vi } from "vitest";
|
|
import { phaseGate } from "../../support/lifecycleHarness.js";
|
|
import { HistoricalCaptureDecoder } from "../../../src/browser/history/HistoricalCaptureDecoder.js";
|
|
import { MitmproxyCaptureAdapter } from "../../../src/browser/history/MitmproxyCaptureAdapter.js";
|
|
import {
|
|
MITMPROXY_CAPTURE_PROVIDER_IDENTITY,
|
|
HAR_CAPTURE_PROVIDER_IDENTITY,
|
|
} from "../../../src/browser/history/CaptureProviderIdentities.js";
|
|
import {
|
|
inspectWebNetworkCaptureInputSchema,
|
|
WEB_NETWORK_CAPTURE_LIMITS,
|
|
} from "../../../src/domain/webNetworkCapture.js";
|
|
import { historicalHar } from "../../fixtures/historicalHar.js";
|
|
import { decodeHarCapture } from "../../../src/browser/history/HarCapture.js";
|
|
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
|
|
import { primaryAnalysisError } from "../../../src/domain/analysisErrorBase.js";
|
|
import { AnalysisCapabilityUnavailableError } from "../../../src/domain/analysisErrorCore.js";
|
|
import { OwnedFileHandle } from "../../../src/filesystem/OwnedFileHandle.js";
|
|
import { waitFor } from "../../support/testWait.js";
|
|
|
|
const linux = it.skipIf(process.platform !== "linux");
|
|
|
|
linux("refuses a directory configured as the mitmdump executable", async () => {
|
|
const root = await createTestTempDirectory("rea-mitmdump-command-");
|
|
const command = join(root, "mitmdump");
|
|
await mkdir(command);
|
|
|
|
await expect(
|
|
new MitmproxyCaptureAdapter({ REA_MITMDUMP_COMMAND: command }).command(
|
|
join(root, "request.json"),
|
|
join(root, "runtime"),
|
|
),
|
|
).rejects.toMatchObject({
|
|
_tag: "AnalysisCapabilityUnavailableError",
|
|
reason: expect.stringContaining(command),
|
|
});
|
|
});
|
|
|
|
linux("allows a symlink to a regular mitmdump executable", async () => {
|
|
const root = await createTestTempDirectory("rea-mitmdump-command-symlink-");
|
|
const command = join(root, "mitmdump");
|
|
const target = join(root, "mitmdump-bin");
|
|
await writeFile(target, "#!/bin/sh\n", { mode: 0o700 });
|
|
await symlink(target, command);
|
|
|
|
await expect(
|
|
new MitmproxyCaptureAdapter({ REA_MITMDUMP_COMMAND: command }).command(
|
|
join(root, "request.json"),
|
|
join(root, "runtime"),
|
|
),
|
|
).resolves.toMatchObject({ command });
|
|
});
|
|
|
|
it("refuses an unsupported host before reading the mitmdump configuration", async () => {
|
|
const refusal: unknown = await new MitmproxyCaptureAdapter(
|
|
{ REA_MITMDUMP_COMMAND: "/opt/mitmdump" },
|
|
"darwin",
|
|
)
|
|
.command("/unused/request.json", "/unused/runtime")
|
|
.catch((cause: unknown) => cause);
|
|
if (!(refusal instanceof AnalysisCapabilityUnavailableError))
|
|
throw new Error("Expected a capability refusal");
|
|
// The variable is already set; the remediation names the host instead.
|
|
expect(projectAnalysisError(refusal)).toMatchObject({
|
|
code: "capability_unavailable",
|
|
remediation: {
|
|
action: expect.stringContaining("on a Linux host"),
|
|
},
|
|
});
|
|
});
|
|
|
|
it("keeps the admitted decoder environment across adapter preparation and refreshes it for the next call", async () => {
|
|
const root = await createTestTempDirectory("rea-capture-environment-");
|
|
const path = join(root, "capture.har");
|
|
const observedPath = join(root, "observed.json");
|
|
const text = JSON.stringify(historicalHar());
|
|
await writeFile(path, text);
|
|
const environment = { ...process.env, REA_CAPTURE_TEST_VALUE: "first" };
|
|
let runtimePath: string | undefined;
|
|
const decoder = new HistoricalCaptureDecoder(
|
|
[
|
|
{
|
|
format: "har",
|
|
identity: HAR_CAPTURE_PROVIDER_IDENTITY,
|
|
command: async (_, selectedRuntime) => {
|
|
// Adapter preparation can yield while callers reconfigure the next operation.
|
|
await Promise.resolve();
|
|
environment.REA_CAPTURE_TEST_VALUE = "changed-during-preparation";
|
|
return {
|
|
command: process.execPath,
|
|
arguments: [
|
|
"-e",
|
|
`
|
|
const fs = require("node:fs");
|
|
fs.writeFileSync(process.argv[1], JSON.stringify(process.env.REA_CAPTURE_TEST_VALUE));
|
|
fs.writeFileSync(process.argv[2], process.argv[3]);
|
|
`,
|
|
observedPath,
|
|
join(selectedRuntime, "reply.json"),
|
|
JSON.stringify({ ok: true, value: decodeHarCapture(text, []) }),
|
|
],
|
|
};
|
|
},
|
|
},
|
|
],
|
|
environment,
|
|
async () => {
|
|
const runtime = await PrivateRuntimeRoot.create({
|
|
prefix: "rea-capture-environment-",
|
|
});
|
|
runtimePath = runtime.path;
|
|
return runtime;
|
|
},
|
|
);
|
|
onTestFinished(() => decoder.close());
|
|
for (const selected of ["first", "second"]) {
|
|
environment.REA_CAPTURE_TEST_VALUE = selected;
|
|
const result = await decoder.inspect(
|
|
inspectWebNetworkCaptureInputSchema.parse({
|
|
capture_path: path,
|
|
format: "har",
|
|
}),
|
|
);
|
|
expect(result.ok).toBe(true);
|
|
expect(JSON.parse(await readFile(observedPath, "utf8"))).toBe(selected);
|
|
if (runtimePath === undefined) throw new Error("runtime was not acquired");
|
|
await expect(access(runtimePath)).rejects.toMatchObject({ code: "ENOENT" });
|
|
}
|
|
});
|
|
|
|
it.skipIf(process.platform === "win32")(
|
|
"binds independently selected mitmproxy configuration before runtime preparation",
|
|
async () => {
|
|
const root = await createTestTempDirectory("rea-mitmproxy-environment-");
|
|
const capturePath = join(root, "capture.mitm");
|
|
const observedPath = join(root, "observed.json");
|
|
await writeFile(capturePath, "inert selected capture");
|
|
const first = join(root, "first");
|
|
const second = join(root, "second");
|
|
const value = {
|
|
...decodeHarCapture(JSON.stringify(historicalHar()), []),
|
|
decoder: MITMPROXY_CAPTURE_PROVIDER_IDENTITY,
|
|
};
|
|
for (const command of [first, second]) {
|
|
await writeFile(
|
|
command,
|
|
`#!${process.execPath}
|
|
const fs = require("node:fs");
|
|
const request = JSON.parse(fs.readFileSync(process.argv.find(a => a.startsWith("rea_request_path=")).slice("rea_request_path=".length), "utf8"));
|
|
fs.writeFileSync(${JSON.stringify(observedPath)}, JSON.stringify({ command: ${JSON.stringify(command)}, host: process.env.REA_CAPTURE_TEST_VALUE }));
|
|
fs.writeFileSync(request.reply_path, ${JSON.stringify(JSON.stringify({ ok: true, value }))});
|
|
`,
|
|
{ mode: 0o700 },
|
|
);
|
|
}
|
|
// Exercise the Linux adapter gate while the actual owned inert child uses this host.
|
|
const command = MitmproxyCaptureAdapter.prototype.command;
|
|
vi.spyOn(MitmproxyCaptureAdapter.prototype, "command").mockImplementation(
|
|
function (this: MitmproxyCaptureAdapter, request, runtime) {
|
|
return command.call(
|
|
new MitmproxyCaptureAdapter(this.environment, "linux"),
|
|
request,
|
|
runtime,
|
|
);
|
|
},
|
|
);
|
|
onTestFinished(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
const adapterEnvironment = { REA_MITMDUMP_COMMAND: first };
|
|
const hostEnvironment = {
|
|
...process.env,
|
|
REA_MITMDUMP_COMMAND: "independent-host-setting",
|
|
REA_CAPTURE_TEST_VALUE: "first-host",
|
|
};
|
|
let runtimePath: string | undefined;
|
|
const decoder = new HistoricalCaptureDecoder(
|
|
[new MitmproxyCaptureAdapter(adapterEnvironment)],
|
|
hostEnvironment,
|
|
async () => {
|
|
adapterEnvironment.REA_MITMDUMP_COMMAND = second;
|
|
hostEnvironment.REA_CAPTURE_TEST_VALUE = "changed-during-preparation";
|
|
const runtime = await PrivateRuntimeRoot.create({
|
|
prefix: "rea-mitmproxy-environment-",
|
|
});
|
|
runtimePath = runtime.path;
|
|
return runtime;
|
|
},
|
|
);
|
|
onTestFinished(() => decoder.close());
|
|
for (const [selectedCommand, selectedHost] of [
|
|
[first, "first-host"],
|
|
[second, "second-host"],
|
|
] as const) {
|
|
adapterEnvironment.REA_MITMDUMP_COMMAND = selectedCommand;
|
|
hostEnvironment.REA_CAPTURE_TEST_VALUE = selectedHost;
|
|
const result = await decoder.inspect(
|
|
inspectWebNetworkCaptureInputSchema.parse({
|
|
capture_path: capturePath,
|
|
format: "mitmproxy",
|
|
}),
|
|
);
|
|
expect(
|
|
result.ok,
|
|
result.ok ? "" : JSON.stringify(projectAnalysisError(result.error)),
|
|
).toBe(true);
|
|
expect(JSON.parse(await readFile(observedPath, "utf8"))).toEqual({
|
|
command: selectedCommand,
|
|
host: selectedHost,
|
|
});
|
|
if (runtimePath === undefined)
|
|
throw new Error("runtime was not acquired");
|
|
await expect(access(runtimePath)).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
}
|
|
},
|
|
);
|
|
|
|
const expectOwnedDecoderPrimary = (
|
|
error: AnalysisError,
|
|
outcome: "cancelled" | "process",
|
|
reason: unknown,
|
|
) => {
|
|
const primary = primaryAnalysisError(error);
|
|
if (outcome === "cancelled") {
|
|
expect(primary._tag).toBe("AnalysisCancelledError");
|
|
expect(primary.cause).toBe(reason);
|
|
} else {
|
|
expect(primary.diagnostics).toMatchObject({
|
|
exit_code: 7,
|
|
stderr: "authored workload failure",
|
|
});
|
|
}
|
|
};
|
|
|
|
it.each(["cancelled", "process"] as const)(
|
|
"retains an unresolved decoder and its root across concurrent inspection and shutdown (%s)",
|
|
async (outcome) => {
|
|
const root = await createTestTempDirectory("rea-capture-owned-cleanup-");
|
|
const capturePath = join(root, "capture.har");
|
|
const markerPath = join(root, "started.json");
|
|
const finishPath = join(root, "finish");
|
|
const text = JSON.stringify(historicalHar());
|
|
await writeFile(capturePath, text);
|
|
const peerCommand = phaseGate();
|
|
const controller = new AbortController();
|
|
const reason = { request: "cancel owned inert decoder" };
|
|
let targetPid: number | undefined;
|
|
let targetOwner: ProviderProcessSupervisor | undefined;
|
|
let denied = true;
|
|
let targetStops = 0;
|
|
const stop = ProviderProcessSupervisor.prototype.stop;
|
|
vi.spyOn(ProviderProcessSupervisor.prototype, "stop").mockImplementation(
|
|
function (this: ProviderProcessSupervisor, options) {
|
|
if (this.launch.process.pid === targetPid) {
|
|
targetOwner = this;
|
|
targetStops++;
|
|
if (denied)
|
|
return Promise.resolve({
|
|
status: "incomplete",
|
|
reason: "controlled stop refusal",
|
|
});
|
|
}
|
|
return stop.call(this, options);
|
|
},
|
|
);
|
|
let calls = 0;
|
|
let roots = 0;
|
|
let targetRuntime: string | undefined;
|
|
let targetRootCloses = 0;
|
|
let denyRoot = false;
|
|
const decoder = new HistoricalCaptureDecoder(
|
|
[
|
|
{
|
|
format: "har",
|
|
identity: HAR_CAPTURE_PROVIDER_IDENTITY,
|
|
command: async (_, runtimePath) => {
|
|
if (calls++ === 0)
|
|
return {
|
|
command: process.execPath,
|
|
arguments: [
|
|
"-e",
|
|
`const fs = require("node:fs"); fs.writeFileSync(process.argv[1], JSON.stringify(process.pid)); setInterval(() => { if (fs.existsSync(process.argv[2])) { process.stderr.write("authored workload failure"); process.exit(7); } }, 10)`,
|
|
markerPath,
|
|
finishPath,
|
|
],
|
|
};
|
|
await peerCommand.hold();
|
|
await writeFile(
|
|
join(runtimePath, "reply.json"),
|
|
JSON.stringify({ ok: true, value: decodeHarCapture(text, []) }),
|
|
);
|
|
return { command: process.execPath, arguments: ["-e", ""] };
|
|
},
|
|
},
|
|
],
|
|
process.env,
|
|
async () => {
|
|
const runtime = await PrivateRuntimeRoot.create({
|
|
prefix: "capture-private-runtime-",
|
|
});
|
|
if (roots++ !== 0) return runtime;
|
|
targetRuntime = runtime.path;
|
|
return {
|
|
path: runtime.path,
|
|
close: async () => {
|
|
targetRootCloses++;
|
|
if (denyRoot) throw new Error("controlled root close refusal");
|
|
await runtime.close();
|
|
},
|
|
};
|
|
},
|
|
);
|
|
onTestFinished(async () => {
|
|
denied = false;
|
|
denyRoot = false;
|
|
peerCommand.release();
|
|
await targetOwner?.stop();
|
|
await decoder.close();
|
|
vi.restoreAllMocks();
|
|
});
|
|
const input = inspectWebNetworkCaptureInputSchema.parse({
|
|
capture_path: capturePath,
|
|
format: "har",
|
|
sensitive_values: ["capture-private-runtime"],
|
|
});
|
|
const target = decoder.inspect(input, { signal: controller.signal });
|
|
await waitFor(async () => {
|
|
targetPid = JSON.parse(await readFile(markerPath, "utf8"));
|
|
});
|
|
const peer = decoder.inspect(input);
|
|
await peerCommand.reached(peer, "peer decoder command");
|
|
if (outcome === "cancelled") controller.abort(reason);
|
|
else await writeFile(finishPath, "finish workload");
|
|
const failed = await target;
|
|
if (failed.ok) throw new Error("cancelled decoder must fail");
|
|
expect(targetRootCloses).toBe(0);
|
|
expectOwnedDecoderPrimary(failed.error, outcome, reason);
|
|
expect(failed.error.partialObservation).toMatchObject({
|
|
kind: "file-snapshot-hash",
|
|
path: capturePath,
|
|
bytes: Buffer.byteLength(text),
|
|
sha256: expect.stringMatching(/^[a-f0-9]{64}$/),
|
|
});
|
|
expect(projectAnalysisError(failed.error).code).toBe("cleanup_incomplete");
|
|
expect(JSON.stringify(projectAnalysisError(failed.error))).not.toContain(
|
|
"capture-private-runtime",
|
|
);
|
|
if (targetRuntime === undefined) throw new Error("runtime missing");
|
|
await expect(access(targetRuntime)).resolves.toBeUndefined();
|
|
expect(targetOwner?.launch.process.exitCode).toBe(
|
|
outcome === "cancelled" ? null : 7,
|
|
);
|
|
const closing = decoder.close().then(
|
|
() => undefined,
|
|
(cause) => cause,
|
|
);
|
|
peerCommand.release();
|
|
expect((await peer).ok).toBe(true);
|
|
expect(await closing).toBeInstanceOf(Error);
|
|
expect(targetRootCloses).toBe(0);
|
|
denied = false;
|
|
denyRoot = true;
|
|
await expect(decoder.close()).rejects.toBeInstanceOf(Error);
|
|
expect(targetRootCloses).toBe(1);
|
|
const releasedStops = targetStops;
|
|
denyRoot = false;
|
|
await decoder.close();
|
|
expect(targetStops).toBe(releasedStops);
|
|
await expect(access(targetRuntime)).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
expect(projectAnalysisError(failed.error).code).not.toBe(
|
|
"cleanup_incomplete",
|
|
);
|
|
expectOwnedDecoderPrimary(failed.error, outcome, reason);
|
|
expect(projectAnalysisError(failed.error).code).toBe(
|
|
outcome === "cancelled" ? "cancelled" : "execution_failure",
|
|
);
|
|
},
|
|
);
|
|
|
|
it("applies a non-default capture deadline and releases its runtime", async () => {
|
|
const root = await createTestTempDirectory("rea-capture-deadline-");
|
|
const path = join(root, "capture.har");
|
|
await writeFile(path, JSON.stringify(historicalHar()));
|
|
let runtimePath: string | undefined;
|
|
const decoder = new HistoricalCaptureDecoder(
|
|
[
|
|
{
|
|
format: "har",
|
|
identity: HAR_CAPTURE_PROVIDER_IDENTITY,
|
|
command: () =>
|
|
Promise.resolve({
|
|
command: process.execPath,
|
|
arguments: ["-e", "setInterval(() => {}, 1_000_000)"],
|
|
}),
|
|
},
|
|
],
|
|
process.env,
|
|
async () => {
|
|
const runtime = await PrivateRuntimeRoot.create({
|
|
prefix: "rea-capture-deadline-",
|
|
});
|
|
runtimePath = runtime.path;
|
|
return runtime;
|
|
},
|
|
);
|
|
const result = await decoder.inspect(
|
|
inspectWebNetworkCaptureInputSchema.parse({
|
|
capture_path: path,
|
|
format: "har",
|
|
capture_timeout_ms: 250,
|
|
}),
|
|
);
|
|
if (result.ok) throw new Error("expected capture timeout");
|
|
expect(result.error._tag).toBe("AnalysisTimeoutError");
|
|
expect(result.error.userMessage).toContain("250");
|
|
expect(result.error.userMessage).toContain("capture_timeout_ms");
|
|
if (runtimePath === undefined) throw new Error("runtime was not acquired");
|
|
await expect(access(runtimePath)).rejects.toMatchObject({ code: "ENOENT" });
|
|
});
|
|
|
|
it.each([true, false])(
|
|
"binds decoder replies to the complete selected provider identity (matching name: %s)",
|
|
async (matching) => {
|
|
const root = await createTestTempDirectory("rea-capture-identity-");
|
|
const path = join(root, "capture.har");
|
|
const text = JSON.stringify(historicalHar());
|
|
await writeFile(path, text);
|
|
const decoder = new HistoricalCaptureDecoder(
|
|
[
|
|
{
|
|
format: "har",
|
|
identity: HAR_CAPTURE_PROVIDER_IDENTITY,
|
|
command: async (_, runtimePath) => {
|
|
const value = decodeHarCapture(text, []);
|
|
if (!matching) value.decoder.name = "different adapter";
|
|
await writeFile(
|
|
join(runtimePath, "reply.json"),
|
|
JSON.stringify({ ok: true, value }),
|
|
);
|
|
return { command: process.execPath, arguments: ["-e", ""] };
|
|
},
|
|
},
|
|
],
|
|
process.env,
|
|
);
|
|
const result = await decoder.inspect(
|
|
inspectWebNetworkCaptureInputSchema.parse({
|
|
capture_path: path,
|
|
format: "har",
|
|
}),
|
|
);
|
|
expect(result.ok).toBe(matching);
|
|
if (!result.ok) expect(result.error._tag).toBe("AnalysisOutputError");
|
|
else expect(result.value.decoder).toEqual(HAR_CAPTURE_PROVIDER_IDENTITY);
|
|
},
|
|
);
|
|
|
|
it("classifies an oversized selected capture before creating an owned decoder", async () => {
|
|
const root = await createTestTempDirectory("rea-historical-limit-");
|
|
const path = join(root, "input.har");
|
|
await writeFile(path, "");
|
|
await truncate(path, WEB_NETWORK_CAPTURE_LIMITS.inputBytes + 1);
|
|
const decoder = new HistoricalCaptureDecoder(
|
|
[
|
|
{
|
|
format: "har",
|
|
identity: HAR_CAPTURE_PROVIDER_IDENTITY,
|
|
command: async () => {
|
|
throw new Error("Oversized input must not launch decoding");
|
|
},
|
|
},
|
|
],
|
|
process.env,
|
|
);
|
|
const result = await decoder.inspect(
|
|
inspectWebNetworkCaptureInputSchema.parse({
|
|
capture_path: path,
|
|
format: "har",
|
|
}),
|
|
);
|
|
if (result.ok) throw new Error("Oversized input must fail");
|
|
expect(result.error._tag).toBe("AnalysisInputError");
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
category: "invalid_input",
|
|
details: {
|
|
issues: [
|
|
{
|
|
path: ["capture_path"],
|
|
reason: "out_of_range",
|
|
expected: {
|
|
maximum_capture_bytes: WEB_NETWORK_CAPTURE_LIMITS.inputBytes,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
});
|
|
});
|
|
|
|
it("retains a failed capture reader and retries it before a fresh load", async () => {
|
|
const root = await createTestTempDirectory("rea-capture-reader-close-");
|
|
const path = join(root, "input.har");
|
|
await writeFile(path, JSON.stringify(historicalHar()));
|
|
const originalClose = OwnedFileHandle.prototype.close;
|
|
const close = vi.spyOn(OwnedFileHandle.prototype, "close");
|
|
let allowClose = false;
|
|
let closeAttempts = 0;
|
|
close.mockImplementation(async function (this: OwnedFileHandle) {
|
|
closeAttempts += 1;
|
|
if (!allowClose) throw new Error("injected capture reader close failure");
|
|
await originalClose.call(this);
|
|
});
|
|
let decoderCalls = 0;
|
|
const decoder = new HistoricalCaptureDecoder(
|
|
[
|
|
{
|
|
format: "har",
|
|
identity: HAR_CAPTURE_PROVIDER_IDENTITY,
|
|
command: async () => {
|
|
decoderCalls += 1;
|
|
return {
|
|
command: process.execPath,
|
|
arguments: ["-e", "process.exit(0)"],
|
|
};
|
|
},
|
|
},
|
|
],
|
|
process.env,
|
|
);
|
|
const input = inspectWebNetworkCaptureInputSchema.parse({
|
|
capture_path: path,
|
|
format: "har",
|
|
});
|
|
try {
|
|
const first = await decoder.inspect(input);
|
|
if (first.ok) throw new Error("Reader close failure must fail inspection");
|
|
expect(first.error.cleanupIncomplete).toBe(true);
|
|
expect(first.error.cleanupResources).toContain(path);
|
|
expect(decoderCalls).toBe(0);
|
|
|
|
const refused = await decoder.inspect(input);
|
|
if (refused.ok) throw new Error("Fresh read must wait for owner cleanup");
|
|
expect(refused.error.cleanupIncomplete).toBe(true);
|
|
expect(refused.error.cleanupResources).toContain(path);
|
|
expect(decoderCalls).toBe(0);
|
|
expect(closeAttempts).toBe(2);
|
|
|
|
allowClose = true;
|
|
await decoder.close();
|
|
expect(closeAttempts).toBe(3);
|
|
expect(first.error.cleanupIncomplete).toBe(false);
|
|
expect(first.error.cleanupResources).toEqual([]);
|
|
} finally {
|
|
allowClose = true;
|
|
await decoder.close().catch(() => undefined);
|
|
close.mockRestore();
|
|
}
|
|
});
|
|
|
|
it("does not misclassify decoder filesystem failures as missing selected input", async () => {
|
|
const root = await createTestTempDirectory("rea-historical-command-");
|
|
const path = join(root, "input.har");
|
|
await writeFile(path, JSON.stringify(historicalHar()));
|
|
const decoder = new HistoricalCaptureDecoder(
|
|
[
|
|
{
|
|
format: "har",
|
|
identity: HAR_CAPTURE_PROVIDER_IDENTITY,
|
|
command: async () => {
|
|
throw Object.assign(
|
|
new Error("Configured decoder command file missing"),
|
|
{ code: "ENOENT" },
|
|
);
|
|
},
|
|
},
|
|
],
|
|
process.env,
|
|
);
|
|
const result = await decoder.inspect(
|
|
inspectWebNetworkCaptureInputSchema.parse({
|
|
capture_path: path,
|
|
format: "har",
|
|
}),
|
|
);
|
|
if (result.ok) throw new Error("Failed decoder command must fail");
|
|
expect(result.error._tag).toBe("ProviderAdapterError");
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
details: {
|
|
diagnostics: {
|
|
phase: "decoder",
|
|
reason: "Configured decoder command file missing",
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it.each(["missing-capture", "missing-reply", "failed-command"])(
|
|
"redacts declared paths and diagnostics while preserving the %s failure",
|
|
async (mode) => {
|
|
const root = await createTestTempDirectory("rea-historical-sensitive-");
|
|
const path = join(root, "explicit-private-value.har");
|
|
if (mode !== "missing-capture")
|
|
await writeFile(path, JSON.stringify(historicalHar()));
|
|
let privatePath: string | undefined;
|
|
const decoder = new HistoricalCaptureDecoder(
|
|
[
|
|
{
|
|
format: "har",
|
|
identity: HAR_CAPTURE_PROVIDER_IDENTITY,
|
|
command: async (_, runtimePath) => {
|
|
privatePath = runtimePath;
|
|
return {
|
|
command: process.execPath,
|
|
arguments: [
|
|
"-e",
|
|
mode === "failed-command"
|
|
? 'process.stderr.write("explicit-private-value ordinary diagnostic"); process.exit(2)'
|
|
: "process.exit(0)",
|
|
],
|
|
};
|
|
},
|
|
},
|
|
],
|
|
process.env,
|
|
);
|
|
onTestFinished(() => decoder.close());
|
|
const result = await decoder.inspect(
|
|
inspectWebNetworkCaptureInputSchema.parse({
|
|
capture_path: path,
|
|
format: "har",
|
|
sensitive_values: ["explicit-private-value", "REDACTED"],
|
|
}),
|
|
);
|
|
if (result.ok) throw new Error("Failure required");
|
|
const projected = projectAnalysisError(result.error);
|
|
const serialized = JSON.stringify(projected);
|
|
expect(serialized).not.toContain("explicit-private-value");
|
|
expect(serialized).not.toContain("REDACTED");
|
|
expect(result.error._tag).toBe(
|
|
mode === "missing-capture"
|
|
? "AnalysisInputError"
|
|
: mode === "missing-reply"
|
|
? "AnalysisOutputError"
|
|
: "ProviderAdapterError",
|
|
);
|
|
if (mode === "missing-capture") {
|
|
expect(privatePath).toBeUndefined();
|
|
expect(serialized).toContain("ENOENT");
|
|
return;
|
|
}
|
|
if (privatePath === undefined)
|
|
throw new Error("Private root was not acquired");
|
|
await expect(access(privatePath)).rejects.toMatchObject({ code: "ENOENT" });
|
|
if (mode === "missing-reply")
|
|
expect(result.error.message).toContain("ENOENT");
|
|
if (mode === "failed-command")
|
|
expect(serialized).toContain('"exit_code":2');
|
|
},
|
|
);
|
|
|
|
const inspectDuringRuntimeClose = async (
|
|
onFirstClose: (controller: AbortController) => void,
|
|
) => {
|
|
const root = await createTestTempDirectory("rea-historical-runtime-close-");
|
|
const path = join(root, "input.har");
|
|
const text = JSON.stringify(historicalHar());
|
|
await writeFile(path, text);
|
|
const reply = JSON.stringify({ ok: true, value: decodeHarCapture(text, []) });
|
|
const runtime = await PrivateRuntimeRoot.create({ parent: root });
|
|
onTestFinished(() => runtime.close());
|
|
const controller = new AbortController();
|
|
let attempts = 0;
|
|
const decoder = new HistoricalCaptureDecoder(
|
|
[
|
|
{
|
|
format: "har",
|
|
identity: HAR_CAPTURE_PROVIDER_IDENTITY,
|
|
command: async (_, runtimePath) => ({
|
|
command: process.execPath,
|
|
arguments: [
|
|
"-e",
|
|
"require('node:fs').writeFileSync(process.argv[1], process.argv[2])",
|
|
join(runtimePath, "reply.json"),
|
|
reply,
|
|
],
|
|
}),
|
|
},
|
|
],
|
|
process.env,
|
|
async () => ({
|
|
path: runtime.path,
|
|
close: async () => {
|
|
if (++attempts === 1) onFirstClose(controller);
|
|
await runtime.close();
|
|
},
|
|
}),
|
|
);
|
|
onTestFinished(() => decoder.close());
|
|
const result = await decoder.inspect(
|
|
inspectWebNetworkCaptureInputSchema.parse({
|
|
capture_path: path,
|
|
format: "har",
|
|
}),
|
|
{ signal: controller.signal },
|
|
);
|
|
if (result.ok) throw new Error("Expected finalization failure");
|
|
const { error } = result;
|
|
expect(error.partialObservation).toMatchObject({
|
|
artifact: {
|
|
path,
|
|
sha256: createHash("sha256").update(text).digest("hex"),
|
|
bytes: Buffer.byteLength(text),
|
|
},
|
|
records: [{ ordinal: 0 }],
|
|
});
|
|
expect(attempts).toBe(1);
|
|
return {
|
|
error,
|
|
runtimePath: runtime.path,
|
|
async expectCleanupRetry() {
|
|
expect(projectAnalysisError(error).code).toBe("cleanup_incomplete");
|
|
expect(error.cleanupIncomplete).toBe(true);
|
|
expect(error.cleanupResources).toEqual([runtime.path]);
|
|
await expect(access(runtime.path)).resolves.toBeUndefined();
|
|
await decoder.close();
|
|
expect(attempts).toBe(2);
|
|
await expect(access(runtime.path)).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
expect(error.cleanupIncomplete).toBe(false);
|
|
expect(error.cleanupResources).toEqual([]);
|
|
},
|
|
};
|
|
};
|
|
|
|
it("preserves the exact cancellation reason observed during final runtime close", async () => {
|
|
const reason = { request: "cancel during runtime close" };
|
|
const { error, runtimePath } = await inspectDuringRuntimeClose((controller) =>
|
|
controller.abort(reason),
|
|
);
|
|
expect(error._tag).toBe("AnalysisCancelledError");
|
|
expect(error.cause).toBe(reason);
|
|
expect(error.cleanupIncomplete).toBe(false);
|
|
await expect(access(runtimePath)).rejects.toMatchObject({ code: "ENOENT" });
|
|
});
|
|
|
|
it("retains completed capture facts and binds final root cleanup to its retry lease", async () => {
|
|
const { expectCleanupRetry } = await inspectDuringRuntimeClose(() => {
|
|
throw new Error("injected private root close failure");
|
|
});
|
|
await expectCleanupRetry();
|
|
});
|
|
|
|
it("keeps late cancellation primary when runtime close also fails, then clears on retry", async () => {
|
|
const reason = { request: "cancel during failed root close" };
|
|
const { error, expectCleanupRetry } = await inspectDuringRuntimeClose(
|
|
(controller) => {
|
|
controller.abort(reason);
|
|
throw new Error("injected close failure after cancellation");
|
|
},
|
|
);
|
|
expect(error._tag).toBe("ProviderAdapterError");
|
|
expect(error.cleanupPrimary?._tag).toBe("AnalysisCancelledError");
|
|
expect(error.cleanupPrimary?.cause).toBe(reason);
|
|
expect(JSON.stringify(error.diagnostics)).not.toContain(
|
|
"partial_observation",
|
|
);
|
|
await expectCleanupRetry();
|
|
});
|
|
|
|
it.each([
|
|
{ reason: "resource-limit", tag: "AnalysisResourceConstraintError" },
|
|
{ reason: "decoder", tag: "ProviderAdapterError" },
|
|
{ reason: "limit", tag: "AnalysisOutputError" },
|
|
])(
|
|
"preserves $reason decoder failures through the public error boundary",
|
|
async ({ reason, tag }) => {
|
|
const root = await createTestTempDirectory("rea-historical-reply-");
|
|
const path = join(root, "input.har");
|
|
await writeFile(path, JSON.stringify(historicalHar()));
|
|
let privatePath: string | undefined;
|
|
const decoder = new HistoricalCaptureDecoder(
|
|
[
|
|
{
|
|
format: "har",
|
|
identity: HAR_CAPTURE_PROVIDER_IDENTITY,
|
|
command: async (_, runtimePath) => {
|
|
privatePath = runtimePath;
|
|
await writeFile(
|
|
join(runtimePath, "reply.json"),
|
|
JSON.stringify({
|
|
ok: false,
|
|
reason,
|
|
message: "Specific decoder constraint",
|
|
pointer: "/extension/child",
|
|
}),
|
|
);
|
|
return {
|
|
command: process.execPath,
|
|
arguments: ["-e", "process.exit(0)"],
|
|
};
|
|
},
|
|
},
|
|
],
|
|
process.env,
|
|
);
|
|
const result = await decoder.inspect(
|
|
inspectWebNetworkCaptureInputSchema.parse({
|
|
capture_path: path,
|
|
format: "har",
|
|
}),
|
|
);
|
|
if (result.ok) throw new Error("Failure required");
|
|
expect(result.error._tag).toBe(tag);
|
|
const projected = projectAnalysisError(result.error);
|
|
if (reason === "limit") {
|
|
expect(projected).toMatchObject({
|
|
code: "unreadable_output",
|
|
retryable: false,
|
|
message: expect.stringContaining(
|
|
String(WEB_NETWORK_CAPTURE_LIMITS.outputBytes),
|
|
),
|
|
details: {
|
|
operation: "inspect_web_network_capture",
|
|
diagnostics: {
|
|
maximum_reply_bytes: WEB_NETWORK_CAPTURE_LIMITS.outputBytes,
|
|
budget: {
|
|
parameter: "max_reply_bytes",
|
|
effective_value: WEB_NETWORK_CAPTURE_LIMITS.outputBytes,
|
|
applies_at: "next_operation",
|
|
update_mode: "parameter",
|
|
},
|
|
},
|
|
},
|
|
});
|
|
expect(projected.remediation.action).toContain(
|
|
"do not recreate a completed report from a refused decode",
|
|
);
|
|
expect(projected.remediation.action).not.toContain("rea doctor");
|
|
expect(result.error.partialObservation).toMatchObject({
|
|
kind: "file-snapshot-hash",
|
|
path,
|
|
sha256: expect.stringMatching(/^[a-f0-9]{64}$/),
|
|
});
|
|
} else if (tag === "ProviderAdapterError") {
|
|
expect(projected).toMatchObject({
|
|
details: {
|
|
diagnostics: {
|
|
phase: "decoder",
|
|
reason: "Specific decoder constraint",
|
|
failure_kind: reason,
|
|
pointer: "/extension/child",
|
|
},
|
|
},
|
|
});
|
|
}
|
|
if (privatePath === undefined)
|
|
throw new Error("Private root was not acquired");
|
|
await expect(access(privatePath)).rejects.toMatchObject({ code: "ENOENT" });
|
|
},
|
|
);
|
|
|
|
it.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
|
|
"reports a real filesystem permission denial separately from missing capture input",
|
|
async () => {
|
|
const root = await createTestTempDirectory("rea-historical-denied-");
|
|
const path = join(root, "unreadable.har");
|
|
await writeFile(path, JSON.stringify(historicalHar()), { mode: 0o000 });
|
|
const decoder = new HistoricalCaptureDecoder(
|
|
[
|
|
{
|
|
format: "har",
|
|
identity: HAR_CAPTURE_PROVIDER_IDENTITY,
|
|
command: async () => {
|
|
throw new Error("Permission-denied input must not launch decoding");
|
|
},
|
|
},
|
|
],
|
|
process.env,
|
|
);
|
|
const result = await decoder.inspect(
|
|
inspectWebNetworkCaptureInputSchema.parse({
|
|
capture_path: path,
|
|
format: "har",
|
|
}),
|
|
);
|
|
await chmod(path, 0o600);
|
|
if (result.ok) throw new Error("Permission denial required");
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
code: "access_denied",
|
|
category: "unavailable",
|
|
retryable: false,
|
|
details: {
|
|
operation: "inspect_web_network_capture",
|
|
path,
|
|
system_code: "EACCES",
|
|
},
|
|
});
|
|
expect(projectAnalysisError(result.error).remediation.action).toContain(
|
|
"read access",
|
|
);
|
|
},
|
|
);
|
|
|
|
it.each(["malformed", "process"])(
|
|
"preserves the structured %s failure when runtime cleanup fails",
|
|
async (kind) => {
|
|
const root = await createTestTempDirectory("rea-historical-cleanup-");
|
|
const path = join(root, "capture-private.har");
|
|
await writeFile(path, JSON.stringify(historicalHar()));
|
|
const decoder = new HistoricalCaptureDecoder(
|
|
[
|
|
{
|
|
format: "har",
|
|
identity: HAR_CAPTURE_PROVIDER_IDENTITY,
|
|
command: async (_, runtimePath) => {
|
|
if (kind === "malformed")
|
|
await writeFile(
|
|
join(runtimePath, "reply.json"),
|
|
JSON.stringify({
|
|
ok: false,
|
|
reason: "malformed",
|
|
message: "Malformed retained record",
|
|
pointer: "/metadata/capture-private",
|
|
}),
|
|
);
|
|
return {
|
|
command: process.execPath,
|
|
arguments: [
|
|
"-e",
|
|
kind === "malformed"
|
|
? "process.exit(0)"
|
|
: 'process.stderr.write("Specific provider fault"); process.exit(3)',
|
|
],
|
|
};
|
|
},
|
|
},
|
|
],
|
|
process.env,
|
|
async () => {
|
|
const runtime = await PrivateRuntimeRoot.create({
|
|
prefix: "rea-cleanup-proof-",
|
|
});
|
|
onTestFinished(() => runtime.close());
|
|
return {
|
|
path: runtime.path,
|
|
close: async () => {
|
|
throw new Error("Deliberate cleanup denial");
|
|
},
|
|
};
|
|
},
|
|
);
|
|
const result = await decoder.inspect(
|
|
inspectWebNetworkCaptureInputSchema.parse({
|
|
capture_path: path,
|
|
format: "har",
|
|
sensitive_values: ["capture-private"],
|
|
}),
|
|
);
|
|
if (result.ok) throw new Error("Cleanup failure required");
|
|
expect(result.error.cleanupPrimary?._tag).toBe(
|
|
kind === "malformed" ? "AnalysisInputError" : "ProviderAdapterError",
|
|
);
|
|
const projected = projectAnalysisError(result.error);
|
|
expect(projected).toMatchObject({
|
|
code: "cleanup_incomplete",
|
|
details: {
|
|
diagnostics: {
|
|
primary_error:
|
|
kind === "malformed"
|
|
? {
|
|
category: "invalid_input",
|
|
details: {
|
|
issues: [
|
|
{
|
|
path: ["capture_path", "/metadata"],
|
|
reason: "invalid_format",
|
|
message: "Malformed retained record",
|
|
},
|
|
],
|
|
},
|
|
}
|
|
: {
|
|
code: "execution_failure",
|
|
details: {
|
|
diagnostics: {
|
|
phase: "decoder",
|
|
exit_code: 3,
|
|
stderr: "Specific provider fault",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
expect(JSON.stringify(projected)).not.toContain("capture-private");
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
{ closeFails: false, reply: "capture" },
|
|
{ closeFails: true, reply: "capture" },
|
|
{ closeFails: false, reply: "malformed" },
|
|
{ closeFails: true, reply: "malformed" },
|
|
{ closeFails: false, reply: "refusal" },
|
|
{ closeFails: true, reply: "refusal" },
|
|
])(
|
|
"retains validated reply completion through descriptor cancellation %j",
|
|
async ({ closeFails, reply }) => {
|
|
const root = await createTestTempDirectory("rea-reply-finalization-");
|
|
const path = join(root, "input.har");
|
|
const text = JSON.stringify(historicalHar());
|
|
const sha256 = createHash("sha256").update(text).digest("hex");
|
|
await writeFile(path, text);
|
|
const controller = new AbortController();
|
|
const reason = { caller: "reply descriptor close" };
|
|
const originalClose = OwnedFileHandle.prototype.close;
|
|
let replyPath: string | undefined;
|
|
let replyCloses = 0;
|
|
const close = vi
|
|
.spyOn(OwnedFileHandle.prototype, "close")
|
|
.mockImplementation(async function (this: OwnedFileHandle) {
|
|
let isReply = false;
|
|
if (replyPath !== undefined) {
|
|
try {
|
|
const named = await stat(replyPath);
|
|
const opened = await this.handle.stat();
|
|
isReply = named.dev === opened.dev && named.ino === opened.ino;
|
|
} catch (cause) {
|
|
if ((cause as NodeJS.ErrnoException).code !== "ENOENT") throw cause;
|
|
}
|
|
}
|
|
if (isReply) replyCloses += 1;
|
|
if (isReply && replyCloses === 1 && closeFails) {
|
|
controller.abort(reason);
|
|
throw new Error("controlled reply close denial");
|
|
}
|
|
await originalClose.call(this);
|
|
if (isReply && replyCloses === 1) controller.abort(reason);
|
|
});
|
|
const decoder = new HistoricalCaptureDecoder(
|
|
[
|
|
{
|
|
format: "har",
|
|
identity: HAR_CAPTURE_PROVIDER_IDENTITY,
|
|
command: async (_, runtimePath) => {
|
|
replyPath = join(runtimePath, "reply.json");
|
|
const content =
|
|
reply === "malformed"
|
|
? "{"
|
|
: JSON.stringify(
|
|
reply === "refusal"
|
|
? {
|
|
ok: false,
|
|
reason: "limit",
|
|
message: "Complete reply allowance exceeded",
|
|
pointer: "",
|
|
}
|
|
: { ok: true, value: decodeHarCapture(text, []) },
|
|
);
|
|
return {
|
|
command: process.execPath,
|
|
arguments: [
|
|
"-e",
|
|
"require('node:fs').writeFileSync(process.argv[1], process.argv[2])",
|
|
replyPath,
|
|
content,
|
|
],
|
|
};
|
|
},
|
|
},
|
|
],
|
|
process.env,
|
|
);
|
|
try {
|
|
const result = await decoder.inspect(
|
|
inspectWebNetworkCaptureInputSchema.parse({
|
|
capture_path: path,
|
|
format: "har",
|
|
}),
|
|
{ signal: controller.signal },
|
|
);
|
|
if (result.ok)
|
|
throw new Error("Expected reply finalization cancellation");
|
|
expect(projectAnalysisError(result.error).code).toBe(
|
|
closeFails
|
|
? "cleanup_incomplete"
|
|
: reply === "capture"
|
|
? "cancelled"
|
|
: "unreadable_output",
|
|
);
|
|
if (reply !== "capture")
|
|
expect(result.error.partialObservation).toMatchObject({
|
|
kind: "file-snapshot-hash",
|
|
path,
|
|
bytes: Buffer.byteLength(text),
|
|
sha256,
|
|
});
|
|
else
|
|
expect(result.error.partialObservation).toMatchObject({
|
|
artifact: { path, sha256, bytes: Buffer.byteLength(text) },
|
|
records: [{ ordinal: 0 }],
|
|
});
|
|
await decoder.close();
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(projectAnalysisError(result.error).code).toBe(
|
|
reply === "capture" ? "cancelled" : "unreadable_output",
|
|
);
|
|
const primary = primaryAnalysisError(result.error);
|
|
if (reply !== "capture") {
|
|
expect(primary.diagnostics).toMatchObject({
|
|
reply_finalization_error: { code: "cancelled" },
|
|
});
|
|
expect(
|
|
primary.secondaryFailures.some(
|
|
(failure) =>
|
|
failure._tag === "AnalysisCancelledError" &&
|
|
failure.cause === reason,
|
|
),
|
|
).toBe(true);
|
|
if (reply === "malformed")
|
|
expect(primary.message).toContain("Reply is malformed JSON.");
|
|
} else expect(primary.cause).toBe(reason);
|
|
expect(replyCloses).toBe(closeFails ? 2 : 1);
|
|
} finally {
|
|
close.mockRestore();
|
|
await decoder.close();
|
|
}
|
|
},
|
|
);
|