fix(hopper): merge current main into runtime cleanup

This commit is contained in:
morluto
2026-10-14 02:12:58 +08:00
453 changed files with 24856 additions and 3372 deletions
@@ -0,0 +1,99 @@
import { createHash } from "node:crypto";
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { join, sep } from "node:path";
import { createPackageWithOptions, listPackage } from "@electron/asar";
import { expect } from "vitest";
import { z } from "zod";
import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js";
import { cliTest } from "../../support/cli/cliFixture.js";
cliTest.for(["CLI", "stdio MCP"] as const)(
"links a block source-map URL containing an asterisk through %s",
async (surface, { cli }) => {
const root = await createTestTempDirectory("rea-block-map-");
const directory = join(root, "input");
await mkdir(directory);
const url = "app*part.js.map";
const directive = "/*# sourceMappingURL=" + url;
await writeFile(join(directory, "app.js"), directive + "*/");
await writeFile(
join(directory, "app_part.js.map"),
JSON.stringify({ version: 3, sources: [], names: [], mappings: "" }),
);
const archive = join(root, "app.asar");
await createPackageWithOptions(directory, archive, {});
// ASAR logical names can contain '*' even on Windows. Rename this header
// key without changing entry bytes, sizes or payload offsets.
const bytes = await readFile(archive);
const key = Buffer.from('"app_part.js.map"');
const offset = bytes.indexOf(key);
expect(offset).toBeGreaterThan(0);
expect(bytes.indexOf(key, offset + key.length)).toBe(-1);
Buffer.from('"' + url + '"').copy(bytes, offset);
await writeFile(archive, bytes);
expect(listPackage(archive, { isPack: false })).toContain(sep + url);
let document: unknown;
if (surface === "CLI") {
const result = await cli.run({
arguments: ["analyze-javascript-application", archive, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
document = result.json;
} else {
const { call } = await connectLocalToolsMcp();
const result = await call("analyze_javascript_application", {
input_path: archive,
format: "asar",
});
expect(result.isError).not.toBe(true);
document = result.structuredContent;
}
const {
normalized_result: { graph },
} = z
.object({
normalized_result: javascriptApplicationAnalysisResultSchema,
})
.parse(document);
const digest = createHash("sha256").update(url).digest("hex");
const map = graph.nodes.find(({ observations }) =>
observations.some(({ properties }) => properties.declared_url === url),
);
expect(
map,
"the complete declared URL must identify the map",
).toBeDefined();
expect(map?.observations).toContainEqual(
expect.objectContaining({
properties: {
declared_url: url,
declared_url_sha256: digest,
resolved_path: url,
available: true,
},
evidence: expect.objectContaining({
location: {
available: true,
value: {
kind: "source-range",
source: "app.js",
start: { line: 1, column: 0 },
end: { line: 1, column: directive.length },
},
},
}),
}),
);
expect(graph.edges).toContainEqual(
expect.objectContaining({
relation: "maps_to",
target_node_id: map?.node_id,
properties: { declared_url_sha256: digest, resolved_path: url },
}),
);
},
);
@@ -67,3 +67,40 @@ cliTest(
expect(session.isError, JSON.stringify(session)).not.toBe(true);
},
);
cliTest(
"rejects a file URL path field without resolving an invented local path",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-electron-file-url-");
const application = join(root, "main.js");
await writeFile(application, "module.exports = {};\n");
const response = await cli.run({
arguments: [
"capture-electron-scenario",
JSON.stringify({
executable_path: "file:///opt/electron",
application_path: application,
}),
"--format",
"json",
],
environment: { REA_LOG_LEVEL: "silent" },
});
expect(response.exitCode).toBe(1);
expect(response.json).toMatchObject({
code: "invalid_request",
details: {
issues: [
{
path: ["executable_path"],
message: expect.stringContaining(
"must be absolute local filesystem paths",
),
},
],
},
});
expect(response.stdout).not.toContain("ENOENT");
expect(response.stdout).not.toContain("file:/opt/electron");
},
);
@@ -0,0 +1,156 @@
import type { Client } from "@modelcontextprotocol/client";
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { z } from "zod";
import { parseEvidence } from "../../../src/domain/evidence.js";
import { parseEvidenceBundle } from "../../../src/domain/evidenceBundle.js";
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
const cases = [
{
name: "both limits",
args: ["--max-heap-mb", "128", "--analysis-timeout-ms", "30000"],
controls: { max_heap_mb: 128, analysis_timeout_ms: 30000 },
},
{
name: "heap limit alone",
args: ["--max-heap-mb", "256"],
controls: { max_heap_mb: 256 },
},
{
name: "timeout alone",
args: ["--analysis-timeout-ms", "40000"],
controls: { analysis_timeout_ms: 40000 },
},
{
name: "omitted limits",
args: [],
controls: {},
},
];
cliTest.for(cases)(
"preserves $name in CLI Evidence without adding ambient defaults",
async ({ args, controls }, { cli }) => {
const root = await createTestTempDirectory("rea-js-limit-provenance-");
await writeFile(join(root, "main.js"), "export const observed = 7;\n");
const response = await cli.run({
arguments: [
"analyze-javascript-application",
root,
...args,
"--format",
"json",
],
environment: { NODE_OPTIONS: "--max-old-space-size=512" },
});
expect(response.exitCode, response.stdout + response.stderr).toBe(0);
const evidence = parseEvidence(response.json);
expect(evidence.parameters).toEqual({
format: "auto",
integrity_policy: "fail",
...controls,
});
},
);
it("preserves MCP analysis limits in retained Evidence and an exported bundle round trip", async () => {
const root = await createTestTempDirectory("rea-js-limit-mcp-");
await writeFile(join(root, "main.js"), "export const observed = 7;\n");
const bundleRoot = await createTestTempDirectory("rea-js-limit-bundle-");
const path = join(bundleRoot, "evidence.json");
const { client } = await connectLocalToolsMcp({
NODE_OPTIONS: "--max-old-space-size=512",
});
const expected = new Map<string, unknown>();
for (const { controls } of cases) {
const response = await client.callTool({
name: "analyze_javascript_application",
arguments: { input_path: root, ...controls, detail: "summary" },
});
expect(response.isError, JSON.stringify(response.content)).not.toBe(true);
const { parent_evidence_id } = z
.object({
normalized_result: z.object({ parent_evidence_id: z.string() }),
})
.parse(response.structuredContent).normalized_result;
expected.set(parent_evidence_id, {
format: "auto",
integrity_policy: "fail",
...controls,
});
}
expect(expected.size).toBe(cases.length);
const retained = await getBundle(client);
for (const [evidence_id, parameters] of expected)
expect(
retained.records.find((record) => record.evidence_id === evidence_id)
?.parameters,
).toEqual(parameters);
const exported = await client.callTool({
name: "export_evidence_bundle",
arguments: { path },
});
expect(exported.isError).not.toBe(true);
expect(parseEvidenceBundle(JSON.parse(await readFile(path, "utf8")))).toEqual(
retained,
);
const closed = await client.callTool({
name: "close_binary",
arguments: {},
});
expect(closed.isError).not.toBe(true);
expect((await getBundle(client)).records).toEqual([]);
const imported = await client.callTool({
name: "import_evidence_bundle",
arguments: { path },
});
expect(imported.isError, JSON.stringify(imported.content)).not.toBe(true);
expect(await getBundle(client)).toEqual(retained);
}, 60_000);
it("retains caller limits in partial Evidence after an actual analysis timeout", async () => {
const root = await createTestTempDirectory("rea-js-limit-timeout-");
await writeFile(join(root, "main.js"), "export const observed = 7;\n");
const { client } = await connectLocalToolsMcp({
NODE_OPTIONS: "--max-old-space-size=512",
});
const response = await client.callTool({
name: "analyze_javascript_application",
arguments: {
input_path: root,
max_heap_mb: 128,
analysis_timeout_ms: 1,
},
});
expect(parseMcpToolError(response)).toMatchObject({
error: { code: "provider_timeout" },
});
const bundle = await getBundle(client);
const partial = bundle.records.find(
(record) => record.operation === "analyze_javascript_application",
);
expect(partial?.parameters).toEqual({
format: "auto",
integrity_policy: "fail",
max_heap_mb: 128,
analysis_timeout_ms: 1,
});
await client.ping();
}, 30_000);
async function getBundle(client: Client) {
const response = await client.callTool({
name: "get_evidence_bundle",
arguments: { detail: "complete" },
});
expect(response.isError).not.toBe(true);
return parseEvidenceBundle(
z.object({ result: z.unknown() }).parse(response.structuredContent).result,
);
}
@@ -0,0 +1,72 @@
import { join } from "node:path";
import { expect } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js";
import { writeFixtureFiles } from "../../support/javascriptApplicationFixture.js";
cliTest.for(["CLI", "stdio MCP"] as const)(
"keeps child argv counts unknown after spread through %s",
async (surface, { cli, processes }) => {
const root = await createTestTempDirectory("rea-child-argv-");
await writeFixtureFiles(root, {
"entry.mjs": [
'import { spawn, execFile, fork } from "node:child_process";',
'const args = ["a", "b"];',
'spawn("spread-spawn", [...args]);',
'execFile("spread-exec", ["prefix", ...args]);',
'fork("spread-fork", [...args, "suffix"]);',
'spawn("fixed", ["a", "b"]);',
'execFile("empty", []);',
].join("\n"),
"child.cjs": "console.log(process.argv.slice(2).length);",
});
const args = ["a", "b"];
const counts: number[] = [];
for (const values of [
[...args],
["prefix", ...args],
[...args, "suffix"],
["a", "b"],
[],
]) {
const control = await processes.run(
process.execPath,
[join(root, "child.cjs"), ...values],
{ timeoutMs: 5_000 },
);
expect(control.exitCode, control.stderr).toBe(0);
counts.push(Number(control.stdout));
}
expect(counts).toEqual([2, 3, 3, 2, 0]);
const graph = (await analyzeJavaScriptDirectory(surface, cli, root))
.semantic_graph;
const children = graph.nodes.filter(
(node) => node.kind === "child-process",
);
expect(children).toHaveLength(5);
for (const command of ["spread-spawn", "spread-exec", "spread-fork"]) {
const child = children.find(
(node) => node.properties.command === command,
);
expect(child?.properties.argv_count, command).toBeNull();
expect(
graph.relations.filter(
(relation) =>
relation.relation === "supplies-argv" &&
relation.target_node_id === child?.node_id,
),
).toEqual([]);
}
expect(
children.find((node) => node.properties.command === "fixed")?.properties
.argv_count,
).toBe(2);
expect(
children.find((node) => node.properties.command === "empty")?.properties
.argv_count,
).toBe(0);
},
);
@@ -0,0 +1,188 @@
import { execFile } from "node:child_process";
import { createRequire } from "node:module";
import { join, relative, sep } from "node:path";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
import { expect } from "vitest";
import type { JavaScriptApplicationGraph } from "../../../src/domain/javascript/javascriptApplicationGraph.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js";
import { writeFixtureFiles } from "../../support/javascriptApplicationFixture.js";
const execute = promisify(execFile);
const logicalPath = (root: string, path: string): string =>
relative(root, path).split(sep).join("/");
const requireSources = (
requests: readonly (readonly [string, string, string | null])[],
): Readonly<Record<string, string>> =>
Object.fromEntries(
[...new Set(requests.map(([source]) => source))].map((source) => [
source,
requests
.filter(([path]) => path === source)
.map(([, specifier]) => `require(${JSON.stringify(specifier)});`)
.join("\n"),
]),
);
const expectResolution = (
graph: JavaScriptApplicationGraph,
source: string,
specifier: string,
kind: "require" | "import",
resolvedPath: string | null,
): void => {
const edges = graph.edges.filter(
({ relation, properties, evidence }) =>
relation === "imports" &&
properties.specifier === specifier &&
evidence.location.available &&
evidence.location.value.kind === "source-range" &&
evidence.location.value.source === source,
);
expect(edges).toHaveLength(2);
expect(edges.map(({ properties }) => properties)).toEqual(
expect.arrayContaining([
expect.objectContaining({
kind: kind === "require" ? "require" : "static-import",
resolved_path: resolvedPath,
}),
expect.objectContaining({
module_link_kind: kind,
resolved_path: resolvedPath,
resolution_status: resolvedPath === null ? "not-found" : "resolved",
}),
]),
);
};
cliTest.for(["CLI", "stdio MCP"] as const)(
"preserves CommonJS directory requests before path normalization through %s",
async (surface, { cli }) => {
const root = await createTestTempDirectory("rea-cjs-directory-lookup-");
const requests = [
["main.cjs", "./dep/.", "dep/index.js"],
["main.cjs", "./dep/", "dep/index.js"],
["main.cjs", "./parent/child/..", "parent/index.js"],
["main.cjs", "./dep", "dep.js"],
["main.cjs", "./entry/.", "entry/dep.js"],
["main.cjs", "./missing/.", null],
["main.cjs", "directory/", "node_modules/directory/index.js"],
["main.cjs", "directory/.", "node_modules/directory/index.js"],
["main.cjs", "package/dep/.", "node_modules/package/dep/index.js"],
["parent/child/main.cjs", ".", "parent/child/index.js"],
["parent/child/main.cjs", "..", "parent/index.js"],
["typed.cts", "./typed.js/.", "typed.js/index.js"],
] as const;
await writeFixtureFiles(root, {
"package.json": '{"type":"commonjs"}',
"dep.js": "module.exports = 'file';",
"dep/index.js": "module.exports = 'directory';",
"entry/package.json": '{"main":"./dep/."}',
"entry/dep.js": "module.exports = 'main file';",
"entry/dep/index.js": "module.exports = 'main directory decoy';",
"missing.js": "module.exports = 'missing directory decoy';",
"parent.js": "module.exports = 'parent file';",
"parent/index.js": "module.exports = 'parent directory';",
"parent/child.js": "module.exports = 'child file';",
"parent/child/index.js": "module.exports = 'child directory';",
"node_modules/directory.js": "module.exports = 'package file';",
"node_modules/directory/index.js":
"module.exports = 'package directory';",
"node_modules/package/dep.js": "module.exports = 'subpath file';",
"node_modules/package/dep/index.js":
"module.exports = 'subpath directory';",
"typed.ts": "module.exports = 'emitted source decoy';",
"typed.js/index.js": "module.exports = 'typed request directory';",
...requireSources(requests),
});
for (const [source, specifier, expected] of requests) {
const require = createRequire(join(root, source));
if (expected === null)
expect(() => require.resolve(specifier)).toThrow(
expect.objectContaining({ code: "MODULE_NOT_FOUND" }),
);
else expect(logicalPath(root, require.resolve(specifier))).toBe(expected);
}
const { graph } = await analyzeJavaScriptDirectory(surface, cli, root);
for (const [source, specifier, expected] of requests)
expectResolution(graph, source, specifier, "require", expected);
expect(graph.coverage.status).toBe("complete");
},
);
cliTest.for(["CLI", "stdio MCP"] as const)(
"preserves loader-specific node_modules ancestors through %s",
async (surface, { cli }) => {
const root = await createTestTempDirectory("rea-cjs-ancestor-lookup-");
const requests = [
[
"node_modules/client/main.cjs",
"shared",
"node_modules/shared/index.js",
],
[
"node_modules/client/main.cjs",
"@scope/shared",
"node_modules/@scope/shared/index.js",
],
[
"node_modules/client/main.cjs",
"private",
"node_modules/client/node_modules/private/index.js",
],
[
"node_modules/@scope/client/main.cjs",
"shared",
"node_modules/shared/index.js",
],
[
"node_modules/parent/node_modules/client/main.cjs",
"shared",
"node_modules/parent/node_modules/shared/index.js",
],
] as const;
const esmSource = "node_modules/client/main.mjs";
const esmTarget = "node_modules/node_modules/shared/index.js";
await writeFixtureFiles(root, {
"package.json": '{"type":"commonjs"}',
"node_modules/shared/index.js": "module.exports = 'outer dependency';",
"node_modules/@scope/shared/index.js":
"module.exports = 'scoped dependency';",
"node_modules/node_modules/shared/index.js":
"module.exports = 'ESM dependency';",
"node_modules/node_modules/@scope/shared/index.js":
"module.exports = 'scoped decoy';",
"node_modules/client/node_modules/private/index.js":
"module.exports = 'private dependency';",
"node_modules/parent/node_modules/shared/index.js":
"module.exports = 'nested dependency';",
"node_modules/parent/node_modules/node_modules/shared/index.js":
"module.exports = 'nested decoy';",
[esmSource]:
'import "shared"; console.log(JSON.stringify(import.meta.resolve("shared")));',
...requireSources(requests),
});
for (const [source, specifier, expected] of requests)
expect(
logicalPath(root, createRequire(join(root, source)).resolve(specifier)),
).toBe(expected);
const control = await execute(process.execPath, [join(root, esmSource)], {
cwd: root,
timeout: 5_000,
});
expect(
logicalPath(root, fileURLToPath(JSON.parse(control.stdout) as string)),
).toBe(esmTarget);
const { graph } = await analyzeJavaScriptDirectory(surface, cli, root);
for (const [source, specifier, expected] of requests)
expectResolution(graph, source, specifier, "require", expected);
expectResolution(graph, esmSource, "shared", "import", esmTarget);
expect(graph.coverage.status).toBe("complete");
},
);
@@ -0,0 +1,78 @@
import { join } from "node:path";
import { expect } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
import { analyzeJavaScriptDirectory } from "../../support/cli/javascriptApplication.js";
import { writeFixtureFiles } from "../../support/javascriptApplicationFixture.js";
cliTest.for(["CLI", "stdio MCP"] as const)(
"retains configuration sources consumed by member access through %s",
async (surface, { cli, processes }) => {
const root = await createTestTempDirectory("rea-config-reads-");
await writeFixtureFiles(root, {
"entry.mjs": [
"const length = process.env.REA_ENDPOINT.length;",
"const endpoint = process.env.REA_ENDPOINT;",
"const argumentLength = process.argv[2].length;",
"const mode = process.argv[3].trim();",
"const optional = process.env.REA_MISSING?.length;",
"function shadow(process) { return process.env.LOCAL.length + process.argv[2].length; }",
'const local = { env: { FAKE: "fake" }, argv: [0, 1, "abc"] };',
'const control = shadow({ env: { LOCAL: "local" }, argv: [0, 1, "abc"] });',
"console.log(JSON.stringify([length, endpoint, argumentLength, mode, optional, control, local.env.FAKE.length]));",
].join("\n"),
});
const control = await processes.run(
process.execPath,
[join(root, "entry.mjs"), "abc", " mode "],
{ env: { REA_ENDPOINT: "endpoint" }, timeoutMs: 15_000 },
);
expect(control.exitCode, control.stderr).toBe(0);
expect(JSON.parse(control.stdout)).toEqual([
8,
"endpoint",
3,
"mode",
null,
8,
4,
]);
const graph = (await analyzeJavaScriptDirectory(surface, cli, root))
.semantic_graph;
const sources = graph.nodes.filter((node) => node.kind === "config-source");
expect(
sources
.map((node) => [node.properties.source_kind, node.properties.key])
.sort(),
).toEqual([
["argv", "2"],
["argv", "3"],
["environment", "REA_ENDPOINT"],
["environment", "REA_ENDPOINT"],
["environment", "REA_MISSING"],
]);
expect(sources.every((node) => node.properties.value === null)).toBe(true);
const endpoint = graph.nodes.find(
(node) => node.kind === "binding" && node.label === "endpoint",
);
expect(endpoint).toBeDefined();
for (const source of sources) {
const bindingTargets = graph.relations
.filter((relation) => relation.source_node_id === source.node_id)
.flatMap((relation) =>
graph.nodes.filter(
(node) =>
node.node_id === relation.target_node_id &&
node.kind === "binding",
),
);
expect(bindingTargets.map((node) => node.node_id)).toEqual(
source.identity.source_range?.start.line === 2
? [endpoint?.node_id]
: [],
);
}
},
);
@@ -0,0 +1,35 @@
import { expect } from "vitest";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
import { cliTest } from "../support/cli/cliFixture.js";
cliTest.for(["analyze", "inspect-artifact"])(
"%s rejects an explicitly empty target before resolving it against cwd",
async (command, { cli }) => {
const cwd = await createTestTempDirectory("rea-empty-target-");
const result = await cli.run({
arguments: [command, "", "--json"],
cwd,
});
expect(result.exitCode).toBe(1);
expect(result.json).toMatchObject({ code: "VALIDATION_ERROR" });
expect(result.stdout).toContain("path");
expect(result.stdout).not.toContain("directory_requires_file");
},
);
cliTest(
"inspect-artifact still admits an explicitly selected current directory",
async ({ cli }) => {
const cwd = await createTestTempDirectory("rea-explicit-target-");
const result = await cli.run({
arguments: ["inspect-artifact", ".", "--json"],
cwd,
});
expect(result.exitCode).toBe(1);
expect(result.json).toMatchObject({
code: "target_unavailable",
details: { path: cwd, constraint: "directory_requires_file" },
});
},
);
+86
View File
@@ -7,6 +7,7 @@ import {
aotFixtureImage,
engineFixtureImage,
REFERENCE_HASH,
appendElfRanges,
} from "../../src/flutter/FlutterBuild.fixture.js";
import {
createFlutterApk,
@@ -159,3 +160,88 @@ cliTest(
).toEqual([4, 4]);
},
);
cliTest(
"CLI and MCP qualify conflicting load mappings and bounded ELF notes",
async ({ cli }) => {
const note = Buffer.from(
"040000000800000003000000474e55000123456789abcdef",
"hex",
);
const engine = engineFixtureImage({
notes: Buffer.concat([note, Buffer.alloc(2000)]),
});
const denseEngine = appendElfRanges(
engine,
"program",
Array.from({ length: 2000 }, (_, index) => ({
type: 4,
offset: 0x400 + note.length + index,
size: 1,
})),
);
const { call } = await connectLocalToolsMcp();
for (const conflict of [false, true]) {
const image = appendElfRanges(aotFixtureImage(), "program", [
{
type: 1,
address: 0x2010,
offset: conflict ? 0x1010 : 0x2010,
size: 4,
},
]);
const path = await createFlutterApk(`load-${String(conflict)}.apk`, [
{ path: "lib/x86_64/libapp.so", bytes: image },
{
path: "lib/x86_64/libflutter.so",
bytes: conflict ? denseEngine : engine,
},
]);
for (const name of [
"inspect_dart_aot",
"identify_flutter_build",
] as const) {
const result = await cli.run({
arguments: [name.replaceAll("_", "-"), path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
const evidence = toolContract(name).outputSchema.parse(result.json);
const response = await call(name, { path });
expect(response.isError, JSON.stringify(response)).not.toBe(true);
const mcp = parseEvidence(response.structuredContent);
expect(mcp.normalized_result).toEqual(evidence.normalized_result);
expect(mcp.evidence_id).toBe(evidence.evidence_id);
expect(mcp.subject).toEqual(evidence.subject);
expect(mcp.limitations).toEqual(evidence.limitations);
expect(evidence.normalized_result.coverage).toBe(
conflict ? "partial" : "complete",
);
if (name === "inspect_dart_aot") {
const facts = flutterResultSchemas[name].parse(
evidence.normalized_result,
);
expect(facts.libapp.sections).toHaveLength(conflict ? 3 : 4);
expect(facts.string_pool.coverage).toBe(
conflict ? "partial" : "complete",
);
if (conflict) {
expect(facts.string_pool.package_uris).toEqual([]);
expect(evidence.limitations.join("\n")).toContain(
"conflicting PT_LOAD",
);
}
} else {
const facts = flutterResultSchemas[name].parse(
evidence.normalized_result,
);
expect(facts.abis[0]?.libflutter).toMatchObject({
build_id: conflict ? null : "0123456789abcdef",
build_id_candidates: ["0123456789abcdef"],
});
if (conflict)
expect(evidence.limitations.join("\n")).toContain("note-range");
}
}
}
},
);
@@ -0,0 +1,130 @@
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { Ajv2020 } from "ajv/dist/2020.js";
import { expect } from "vitest";
import { createEvidence, parseEvidence } from "../../src/domain/evidence.js";
import { wasmInterfaceComparisonSchema } from "../../src/contracts/wasm/wasmComparisonContracts.js";
import { composeBinarySession } from "../../src/application/binary/BinarySessionComposition.js";
import { AnalysisProviderRegistry } from "../../src/application/binary/AnalysisProviderRegistry.js";
import { createServer } from "../../src/server/createServer.js";
import { wasmArtifactFixture } from "../fixtures/wasm/artifact.js";
import { cliTest } from "../support/cli/cliFixture.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
cliTest(
"compares portable Evidence identically through CLI and retained MCP with schema/provenance",
async ({ cli }) => {
const sources = (["i32", "i64"] as const).map((parameter) => {
const report = wasmArtifactFixture();
report.interface = {
state: "complete",
issues: [],
entries: [
{
direction: "export",
module: null,
name: 'quoted"\ufeffname',
kind: "func",
index: 0,
signature: { kind: "func", params: [parameter], results: [] },
wat_form: '(export "quoted\\22\\ef\\bb\\bfname" (func 0))',
issue: null,
},
],
};
return createEvidence(
{
path: report.artifact.path,
sha256: report.artifact.sha256,
format: "file",
},
{ id: "typed-fixture", name: "Typed fixture", version: "1" },
{
operation: "inspect_wasm_artifact",
parameters: { path: report.artifact.path },
result: report,
confidence: "observed",
},
);
});
const [left, right] = sources;
if (left === undefined || right === undefined)
throw new Error("Missing paired fixture");
const root = await createTestTempDirectory("rea-wasm-comparison-");
const path = join(root, "input.json");
const input = { left, right, scope: "both" };
await writeFile(path, JSON.stringify(input));
const cliResult = await cli.run({
arguments: ["compare-wasm-interfaces", path, "--json"],
});
expect(cliResult.exitCode).toBe(0);
const cliEvidence = parseEvidence(cliResult.json);
const cliReport = wasmInterfaceComparisonSchema.parse(
cliEvidence.normalized_result,
);
expect(cliReport).toMatchObject({
coverage: "complete-within-inputs",
changes: [{ name: 'quoted"\ufeffname', state: "changed" }],
left: {
evidence_id: left.evidence_id,
artifact: {
path: "/artifacts/module.wasm",
sha256: "a".repeat(64),
bytes: 8,
},
},
});
const session = composeBinarySession(new AnalysisProviderRegistry([]));
const server = createServer({ kind: "session", session });
const client = new Client({ name: "wasm-interface-parity", version: "1" });
const [ct, st] = InMemoryTransport.createLinkedPair();
try {
await server.connect(st);
await client.connect(ct);
const tool = (await client.listTools()).tools.find(
(t) => t.name === "compare_wasm_interfaces",
);
if (tool?.outputSchema === undefined)
throw new Error("Missing comparison schema");
const outputSchema: Record<string, unknown> = tool.outputSchema;
const ajv = new Ajv2020({ strict: false, validateFormats: false });
expect(ajv.validate(tool.inputSchema, input)).toBe(true);
expect(ajv.validate(tool.inputSchema, { left, right })).toBe(false);
const mcp = await client.callTool({ name: tool.name, arguments: input });
expect(mcp.isError, JSON.stringify(mcp)).not.toBe(true);
expect(
ajv.validate(outputSchema, mcp.structuredContent),
JSON.stringify(ajv.errors),
).toBe(true);
const mcpEvidence = parseEvidence(mcp.structuredContent);
expect(mcpEvidence.normalized_result).toEqual(cliReport);
expect(mcpEvidence.evidence_id).toBe(cliEvidence.evidence_id);
expect(mcpEvidence.evidence_links).toEqual([
left.evidence_id,
right.evidence_id,
]);
const retained = await client.callTool({
name: tool.name,
arguments: {
scope: "both",
left: { kind: "retained-evidence", evidence_id: left.evidence_id },
right: { kind: "retained-evidence", evidence_id: right.evidence_id },
},
});
expect(retained.isError).not.toBe(true);
expect(parseEvidence(retained.structuredContent).evidence_id).toBe(
mcpEvidence.evidence_id,
);
const invalid = await client.callTool({
name: tool.name,
arguments: { ...input, scope: "all" },
});
expect(invalid.isError).toBe(true);
} finally {
await client.close();
await server.close();
await session.close();
}
},
);
+304
View File
@@ -0,0 +1,304 @@
import { expect, it } from "vitest";
import {
analyzeJadxRequest,
type JadxToolPort,
} from "../../../src/android/JadxAnalysis.js";
import { androidInputSchemas } from "../../../src/domain/android/androidAnalysis.js";
const summary = {
full_name: "fixture.Target",
name: "Target",
method_count: 2,
field_count: 0,
inner_class_count: 0,
methods: [
{
name: "choose",
signature: "choose(String): void",
dex_descriptor: "(Ljava/lang/String;)V",
is_constructor: false,
def_pos: 0,
},
{
name: "choose",
signature: "choose(int): void",
dex_descriptor: "(I)V",
is_constructor: false,
def_pos: 0,
},
],
fields: [],
inner_classes: [],
};
const engine = {
name: "jadx",
version: "0.7.1",
artifact_sha256: "a".repeat(64),
source_revision: null,
worker_count: 1,
heap_limit_mib: null,
};
it("publishes exact DEX prototypes instead of discarding bridge metadata", async () => {
const tools: JadxToolPort = {
json: () => Promise.resolve(summary),
text: () => Promise.reject(new Error("unexpected text call")),
};
const result = await analyzeJadxRequest(
tools,
{
operation: "inspect_android_class",
input: androidInputSchemas.inspect_android_class.parse({
path: "/fixture.apk",
class_name: "fixture.Target",
}),
},
{} as never,
engine,
);
expect(result).toMatchObject({
methods: [
{ name: "choose", dex_descriptor: "(Ljava/lang/String;)V" },
{ name: "choose", dex_descriptor: "(I)V" },
],
});
});
it("traces only the selected overloaded method and checks the producer identity", async () => {
const calls: Record<string, unknown>[] = [];
const tools: JadxToolPort = {
json: (name, input) => {
calls.push({ name, ...input });
if (name === "get_class_summary") return Promise.resolve(summary);
return Promise.resolve({
target: "fixture.Target.choose",
overload_index: 1,
reported_signature: "choose(int): void",
total: 1,
count: 1,
resolve_line: false,
items: [
{
kind: "method",
name: "caller",
full_name: "fixture.Other.caller",
containing_class: "fixture.Other",
top_class: "fixture.Other",
def_pos: 0,
direct_invokes: [
{
dex_entry: "classes.dex",
source_raw_method_id: "fixture.Other.caller()V",
dex_code_unit_offset: 0,
dex_method_index: 1,
opcode: "INVOKE_STATIC",
},
{
dex_entry: "classes.dex",
source_raw_method_id: "fixture.Other.caller()V",
dex_code_unit_offset: 3,
dex_method_index: 1,
opcode: "INVOKE_STATIC",
},
],
},
],
});
},
text: () => Promise.reject(new Error("unexpected text call")),
};
const request = {
operation: "trace_android_references",
input: androidInputSchemas.trace_android_references.parse({
path: "/fixture.apk",
class_name: "fixture.Target",
method_name: "choose",
overload_index: 1,
}),
} as const;
const result = await analyzeJadxRequest(tools, request, {} as never, engine);
expect(result).toMatchObject({
method_name: "choose",
overload_index: 1,
reported_signature: "choose(int): void",
dex_descriptor: "(I)V",
total: 1,
reference_unit: "using_node",
references: [
{
direct_invokes: [
{ dex_code_unit_offset: 0 },
{ dex_code_unit_offset: 3 },
],
},
],
});
expect(calls[1]).toMatchObject({
name: "get_xrefs_to_method",
overload_index: 1,
});
});
it("rejects an ambiguous method and a producer response for a different overload", async () => {
const xrefs = {
target: "fixture.Target.choose",
overload_index: 0,
reported_signature: "choose(String): void",
total: 0,
count: 0,
resolve_line: false,
items: [],
};
const tools: JadxToolPort = {
json: (name) =>
Promise.resolve(name === "get_class_summary" ? summary : xrefs),
text: () => Promise.reject(new Error("unexpected text call")),
};
const input = {
path: "/fixture.apk",
class_name: "fixture.Target",
method_name: "choose",
};
await expect(
analyzeJadxRequest(
tools,
{
operation: "trace_android_references",
input,
},
{} as never,
engine,
),
).rejects.toMatchObject({
_tag: "AnalysisInputError",
});
await expect(
analyzeJadxRequest(
tools,
{
operation: "trace_android_references",
input: { ...input, overload_index: 1 },
},
{} as never,
engine,
),
).rejects.toMatchObject({
_tag: "AnalysisOutputError",
});
});
const invokeSite = {
dex_entry: "classes.dex",
source_raw_method_id: "fixture.Other.caller()V",
dex_code_unit_offset: 0,
dex_method_index: 1,
opcode: "INVOKE_STATIC",
};
const traceWithReference = (fields: Record<string, unknown>) =>
analyzeJadxRequest(
{
json: (name) =>
Promise.resolve(
name === "get_class_summary"
? summary
: {
target: "fixture.Target.choose",
overload_index: 0,
reported_signature: "choose(String): void",
total: 1,
count: 1,
resolve_line: false,
items: [
{
kind: "method",
name: "caller",
full_name: "fixture.Other.caller",
containing_class: "fixture.Other",
top_class: "fixture.Other",
def_pos: 0,
...fields,
},
],
},
),
text: () => Promise.reject(new Error("unexpected text call")),
},
{
operation: "trace_android_references",
input: {
path: "/fixture.apk",
class_name: "fixture.Target",
method_name: "choose",
overload_index: 0,
},
},
{} as never,
engine,
);
it.each([
[{}, null],
[{ direct_invokes: [] }, []],
])(
"preserves unavailable versus observed-empty invoke sites: %j",
async (fields, expected) => {
const result = await traceWithReference(fields as Record<string, unknown>);
expect(result).toMatchObject({
references: [{ direct_invokes: expected }],
});
},
);
it.each([
{ ...invokeSite, dex_code_unit_offset: -1 },
{
dex_entry: "classes.dex",
source_raw_method_id: "fixture.Other.caller()V",
dex_code_unit_offset: 0,
dex_method_index: 1,
},
])("rejects malformed direct invoke producer evidence: %j", async (site) => {
await expect(
traceWithReference({ direct_invokes: [site] }),
).rejects.toThrow();
});
it("preserves bounded instruction coverage separately from using-node coverage", async () => {
const result = await traceWithReference({
direct_invokes: [invokeSite],
direct_invokes_exhaustive: false,
});
expect(result).toMatchObject({
coverage: "complete",
reference_unit: "using_node",
references: [
{ direct_invokes: [invokeSite], direct_invokes_exhaustive: false },
],
});
});
it("rejects malformed method descriptors at the Android adapter boundary", async () => {
const invalidSummary = {
...summary,
methods: summary.methods.map((method) => ({
...method,
dex_descriptor: "not-a-dex-descriptor",
})),
};
await expect(
analyzeJadxRequest(
{
json: () => Promise.resolve(invalidSummary),
text: () => Promise.reject(new Error("unexpected text call")),
},
{
operation: "inspect_android_class",
input: { path: "/fixture.apk", class_name: "fixture.Target" },
},
{} as never,
engine,
),
).rejects.toThrow();
});
@@ -0,0 +1,60 @@
import { spawn } from "node:child_process";
import { readFile } from "node:fs/promises";
import { expect, it } from "vitest";
it.skipIf(process.platform !== "win32")(
"retains guardian cleanup ownership through more than 100 transient failures",
async () => {
const guardian = await readFile(
new URL(
"../../../scripts/rea-android-workspace-guardian.mjs",
import.meta.url,
),
"utf8",
);
const withoutTimer = guardian
.replace(/^#!.*\r?\n/u, "")
.replace(
'import { setTimeout as delay } from "node:timers/promises";',
"const delay = async () => {};",
);
const injected = withoutTimer.replace(
'import { WindowsPrivateRuntime } from "../dist/windows/WindowsPrivateRuntime.js";',
`let attempts = 0;
const WindowsPrivateRuntime = {
create: () => ({
observation: { path: "synthetic-root", volumeSerial: "0", fileId: "0", privateDacl: true },
close: async () => { if (++attempts <= 101) throw new Error("transient cleanup failure"); },
}),
};`,
);
expect(withoutTimer).not.toBe(guardian);
expect(injected).not.toBe(withoutTimer);
const child = spawn(
process.execPath,
[
"--input-type=module",
"-e",
'process.argv.splice(1, 0, "guardian.mjs");\n' + injected,
"synthetic-parent",
],
{ signal: AbortSignal.timeout(3000) },
);
let stdout = "";
let stderr = "";
child.stdout.on("data", (chunk: Buffer) => {
stdout += chunk.toString();
});
child.stderr.on("data", (chunk: Buffer) => {
stderr += chunk.toString();
});
child.stdin.end();
const code = await new Promise<number | null>((resolve, reject) => {
child.once("close", resolve);
child.once("error", reject);
});
expect(code, stderr).toBe(0);
expect(JSON.parse(stdout.trim())).toMatchObject({ path: "synthetic-root" });
},
);
@@ -4,6 +4,40 @@ import {
recordedCrashFixtureBytes,
} from "../../fixtures/binaryDiagnostics/recordedCrash.js";
import { validateRecordedCrashStructure } from "../../../src/native/pwntools/RecordedCrashStructureBindings.js";
import { RECORDED_LINUX_CORE_PROFILES } from "../../../src/domain/native/recordedCrash.js";
it.each(["x86_64-little-endian", "aarch64-little-endian"] as const)(
"admits a %s core whose header carries that profile's e_machine",
(architecture) => {
const report = recordedCrashFixture(undefined, architecture);
expect(() =>
validateRecordedCrashStructure(
report,
recordedCrashFixtureBytes(report),
report.decoder_diagnostics,
),
).not.toThrow();
},
);
it.each([
["x86_64-little-endian", "aarch64-little-endian"],
["aarch64-little-endian", "x86_64-little-endian"],
] as const)(
"rejects a %s report whose header carries the other profile's e_machine",
(reported, embedded) => {
const report = recordedCrashFixture(undefined, reported);
const snapshot = recordedCrashFixtureBytes(report);
snapshot.writeUInt16LE(RECORDED_LINUX_CORE_PROFILES[embedded].machine, 18);
expect(() =>
validateRecordedCrashStructure(
report,
snapshot,
report.decoder_diagnostics,
),
).toThrow(/Reported core structure differs/);
},
);
it.each([
[6, 0],
@@ -1,5 +1,8 @@
import type { AnalysisError } from "../../../src/domain/analysisErrorBase.js";
import type { RecordedCrash } from "../../../src/domain/native/recordedCrash.js";
import type {
RecordedCrash,
RecordedCrashArchitecture,
} from "../../../src/domain/native/recordedCrash.js";
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, onTestFinished } from "vitest";
@@ -48,6 +51,7 @@ it
.runIf(host)
.each([
"success",
"success-aarch64",
"wrong-register",
"wrong-register-source",
"missing-thread",
@@ -77,9 +81,13 @@ it
] as const)(
"validates recorded source bytes and cleanup: %s",
async (scenario) => {
const architecture = scenario.endsWith("aarch64")
? "aarch64-little-endian"
: "x86_64-little-endian";
const { path, value, bytes } = await fixture(
true,
scenario === "wrong-owner-display",
architecture,
);
if (scenario.startsWith("wrong-ei-version-"))
bytes[6] = Number(scenario.at(-1));
@@ -123,8 +131,9 @@ it
path,
include_debugger_context: false,
});
if (scenario === "success") {
if (scenario === "success" || scenario === "success-aarch64") {
if (!result.ok) throw result.error;
expect(result.value.architecture).toBe(architecture);
expect(result.value.threads).toEqual(value.threads);
expect(result.value.diagnostics.stdout).toBe("recorded warning");
} else
@@ -340,13 +349,17 @@ const expectOptionalContextFailure = (
});
};
const fixture = async (includeSignal = false, opaqueOwner = false) => {
const fixture = async (
includeSignal = false,
opaqueOwner = false,
architecture: RecordedCrashArchitecture = "x86_64-little-endian",
) => {
const workspace = await createTestWorkspace("rea-recorded-core-seam-");
onTestFinished(() => removeTestWorkspace(workspace.root));
const path = join(workspace.root, "recorded.core");
const value = includeSignal
? recordedCrashSignalFixture(path)
: recordedCrashFixture(path);
? recordedCrashSignalFixture(path, architecture)
: recordedCrashFixture(path, architecture);
if (opaqueOwner) {
value.notes = value.notes.map((note) =>
note.index === 0
@@ -1,3 +1,4 @@
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect } from "vitest";
@@ -9,6 +10,7 @@ import {
} from "../../support/process/processFixture.js";
import {
BROWSER_DEVTOOLS_STARTUP_TIMEOUT_MS,
BrowserStartupError,
waitForBrowserDevtoolsPort,
} from "../../../src/browser/BrowserProcessStartup.js";
@@ -86,15 +88,68 @@ describe("browser process startup", () => {
failure: "timeout",
exitCode: null,
signalCode: null,
startupTimeoutMs: 20,
});
expect(String(failure)).toContain("stderr=<empty>");
expect(String(failure)).toContain("startup_timeout_ms=20");
} finally {
child.kill("SIGKILL");
expect(await waitForExit(child, 5_000)).toBe(true);
}
});
it("names the owner's deadline when a caller omits one", async ({
processes,
}) => {
const root = await temporaryRoot();
const child = processes.spawn(process.execPath, [
"-e",
"setTimeout(() => process.exit(3), 10)",
]);
const failure = await waitForBrowserDevtoolsPort({
child,
executable: process.execPath,
activePortPath: join(root, "DevToolsActivePort"),
stderr: () => "owner default fixture",
pollIntervalMs: 5,
}).catch((cause: unknown) => cause);
expect(failure).toBeInstanceOf(BrowserStartupError);
expect(failure).toMatchObject({
failure: "exited",
startupTimeoutMs: BROWSER_DEVTOOLS_STARTUP_TIMEOUT_MS,
});
expect(String(failure)).toContain(
`startup_timeout_ms=${String(BROWSER_DEVTOOLS_STARTUP_TIMEOUT_MS)}`,
);
});
it("keeps one owner for the DevToolsActivePort deadline", async () => {
expect(BROWSER_DEVTOOLS_STARTUP_TIMEOUT_MS).toBe(60_000);
const callers = [
"../../../scripts/verify-real-browser.mjs",
"../../../scripts/lib/browser-runtime-fixture-lifecycle.mjs",
];
for (const caller of callers) {
const source = await readFile(new URL(caller, import.meta.url), "utf8");
expect(devtoolsPortDeadlines(source)).toEqual([
["BROWSER_DEVTOOLS_STARTUP_TIMEOUT_MS"],
]);
}
});
});
const temporaryRoot = async (): Promise<string> => {
return createTestTempDirectory("rea-browser-startup-");
};
const devtoolsPortDeadlines = (source: string): string[][] => {
const calls = /waitForBrowserDevtoolsPort\(\{(?<body>[\s\S]*?)\}\)/gu;
const deadlines = /\btimeoutMs:\s*(?<value>[^\n,]+)/gu;
return [...source.matchAll(calls)].map((call) =>
[...String(call.groups?.body).matchAll(deadlines)].map((deadline) =>
String(deadline.groups?.value).trim(),
),
);
};
@@ -0,0 +1,232 @@
import { createServer, type Socket } from "node:net";
import { describe, expect, it, vi } from "vitest";
import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
import { openCdpEndpoint } from "../../../src/browser/CdpTargetSession.js";
import { AnalysisError } from "../../../src/domain/analysisErrorBase.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import {
browserObservationEndpointSchema,
inspectWebPageInputSchema,
listBrowserTargetsInputSchema,
} from "../../../src/domain/browserObservation.js";
import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js";
import { trackBrowser } from "./cdpBrowserProvider.support.js";
describe("CdpBrowserProvider: browser socket without HTTP discovery", () => {
it("discovers and inspects a page through one browser socket", async () => {
const browser = await startFakeCdpBrowser();
trackBrowser(browser);
const provider = new CdpBrowserProvider();
const throughHttp = await provider.listTargets(
listBrowserTargetsInputSchema.parse({ cdp_endpoint: browser.endpoint }),
);
if (!throughHttp.ok) throw throughHttp.error;
const httpRequests = browser.httpRequests.length;
const openedBefore = browser.openedConnectionCount();
const listed = await provider.listTargets(
listBrowserTargetsInputSchema.parse({
cdp_endpoint: browser.browserWebSocketUrl,
}),
);
if (!listed.ok) throw listed.error;
expect(listed.value.targets).toEqual(throughHttp.value.targets);
expect(listed.value.excluded).toEqual(throughHttp.value.excluded);
expect(listed.value.browser).toEqual({
...throughHttp.value.browser,
revision: "@fake-revision",
});
const commandsBefore = browser.commands.length;
const inspected = await provider.inspectPage(
inspectWebPageInputSchema.parse({
cdp_endpoint: browser.browserWebSocketUrl,
target_id: "allowed-page",
observation_ms: 0,
}),
);
if (!inspected.ok) throw inspected.error;
expect(inspected.value.target.origin).toBe(browser.allowedOrigin);
expect(
browser.commands
.slice(commandsBefore, commandsBefore + 3)
.map(({ method, params }) => ({ method, params })),
).toEqual([
{ method: "Browser.getVersion", params: {} },
{ method: "Target.getTargets", params: {} },
{
method: "Target.attachToTarget",
params: { targetId: "allowed-page", flatten: true },
},
]);
expect(browser.httpRequests).toHaveLength(httpRequests);
expect(browser.openedConnectionCount()).toBe(openedBefore + 2);
await vi.waitFor(() => expect(browser.activeConnectionCount()).toBe(0));
});
});
describe("CdpBrowserProvider: browser socket failures and cleanup", () => {
it("closes an unadopted browser socket when the target is missing", async () => {
const browser = await startFakeCdpBrowser();
trackBrowser(browser);
const result = await new CdpBrowserProvider().inspectPage(
inspectWebPageInputSchema.parse({
cdp_endpoint: browser.browserWebSocketUrl,
target_id: "missing",
observation_ms: 0,
}),
);
expect(result).toMatchObject({
ok: false,
error: { _tag: "BrowserObservationError", reason: "target_not_found" },
});
expect(browser.commands.map(({ method }) => method)).toEqual([
"Browser.getVersion",
"Target.getTargets",
]);
await vi.waitFor(() => expect(browser.activeConnectionCount()).toBe(0));
});
it("rejects malformed browser-socket discovery and closes the socket", async () => {
const browser = await startFakeCdpBrowser({
commandResult: ({ method }) =>
method === "Browser.getVersion"
? { product: "FakeChrome/1.0" }
: undefined,
});
trackBrowser(browser);
const result = await new CdpBrowserProvider().listTargets(
listBrowserTargetsInputSchema.parse({
cdp_endpoint: browser.browserWebSocketUrl,
}),
);
expect(result).toMatchObject({
ok: false,
error: {
_tag: "BrowserObservationError",
operation: "list_browser_targets",
reason: "invalid_endpoint_response",
},
});
await vi.waitFor(() => expect(browser.activeConnectionCount()).toBe(0));
});
it("reports a refused browser socket as unreachable", async () => {
const browser = await startFakeCdpBrowser();
trackBrowser(browser);
const result = await new CdpBrowserProvider().listTargets(
listBrowserTargetsInputSchema.parse({
cdp_endpoint: browser.browserWebSocketUrl.replace(
/fake$/u,
"not-this-browser",
),
}),
);
expect(result).toMatchObject({
ok: false,
error: {
_tag: "BrowserObservationError",
reason: "endpoint_unreachable",
},
});
expect(JSON.stringify(result)).not.toContain("not-this-browser");
});
it("cancels browser-socket discovery and releases the socket", async () => {
const browser = await startFakeCdpBrowser({
hangOnMethod: "Target.getTargets",
});
trackBrowser(browser);
const controller = new AbortController();
const pending = new CdpBrowserProvider().inspectPage(
inspectWebPageInputSchema.parse({
cdp_endpoint: browser.browserWebSocketUrl,
target_id: "allowed-page",
observation_ms: 0,
}),
{ signal: controller.signal },
);
await vi.waitFor(() =>
expect(browser.commands.map(({ method }) => method)).toContain(
"Target.getTargets",
),
);
controller.abort();
expect(await pending).toMatchObject({
ok: false,
error: { _tag: "AnalysisCancelledError", operation: "inspect_web_page" },
});
expect(browser.commands.map(({ method }) => method)).not.toContain(
"Target.attachToTarget",
);
await vi.waitFor(() => expect(browser.activeConnectionCount()).toBe(0));
});
});
/** Expect a discovery timeout that projects publicly as a provider timeout. */
const expectDiscoveryTimeout = async (url: string): Promise<void> => {
let failure: unknown;
try {
await openCdpEndpoint(
browserObservationEndpointSchema.parse(url),
"list_browser_targets",
undefined,
50,
);
} catch (cause: unknown) {
failure = cause;
}
expect(failure).toMatchObject({
_tag: "BrowserObservationError",
reason: "timeout",
});
if (!(failure instanceof AnalysisError))
throw new Error("Expected a typed discovery failure");
expect(projectAnalysisError(failure)).toMatchObject({
code: "provider_timeout",
category: "timeout",
});
};
describe("CdpBrowserProvider: browser socket discovery deadline", () => {
it("times out a discovery reply that never arrives and closes the socket", async () => {
const browser = await startFakeCdpBrowser({
hangOnMethod: "Target.getTargets",
});
trackBrowser(browser);
await expectDiscoveryTimeout(browser.browserWebSocketUrl);
await vi.waitFor(() => expect(browser.activeConnectionCount()).toBe(0));
});
it("times out a WebSocket handshake that never completes", async () => {
const sockets = new Set<Socket>();
// Read the upgrade request but never answer it; reading lets the
// client's close be observed here.
const server = createServer((socket) => {
sockets.add(socket);
socket.on("close", () => sockets.delete(socket));
socket.resume();
});
await new Promise<void>((resolve) =>
server.listen(0, "127.0.0.1", resolve),
);
const address = server.address();
if (address === null || typeof address === "string")
throw new Error("Expected a TCP listener address");
try {
await expectDiscoveryTimeout(
`ws://127.0.0.1:${String(address.port)}/devtools/browser/stalled`,
);
await vi.waitFor(() => expect(sockets.size).toBe(0));
} finally {
for (const socket of sockets) socket.destroy();
await new Promise<void>((resolve) => server.close(() => resolve()));
}
});
});
@@ -3,6 +3,7 @@ import { createServer } from "node:http";
import { describe, expect, it } from "vitest";
import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
import { listBrowserTargetsInputSchema } from "../../../src/domain/browserObservation.js";
describe("CDP target inventory", () => {
it("returns every allowed target within the bounded discovery payload", async () => {
@@ -51,10 +52,12 @@ describe("CDP target inventory", () => {
port = address.port;
const endpoint = `http://127.0.0.1:${String(port)}`;
const result = await new CdpBrowserProvider().listTargets({
cdp_endpoint: endpoint,
allowed_origins: ["https://allowed.example"],
});
const result = await new CdpBrowserProvider().listTargets(
listBrowserTargetsInputSchema.parse({
cdp_endpoint: endpoint,
allowed_origins: ["https://allowed.example"],
}),
);
if (!result.ok) throw result.error;
expect(result.value.targets).toHaveLength(targets.length);
@@ -0,0 +1,255 @@
import { describe, expect, it } from "vitest";
import { CdpBrowserProvider } from "../../../src/browser/CdpBrowserProvider.js";
import { inspectWebPageInputSchema } from "../../../src/domain/browserObservation.js";
import type { WebPageInspection } from "../../../src/domain/browserObservationSchemas.js";
import { compareWebCaptures } from "../../../src/domain/webCaptureDiff.js";
import { compareWebCapturesInputSchema } from "../../../src/domain/webCaptureDiffSchemas.js";
import type { FakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js";
import {
startHttpComparisonBrowser,
type HttpRequestObservation,
} from "../../fixtures/httpComparisonBrowser.js";
import { trackBrowser } from "./cdpBrowserProvider.support.js";
const inspect = async (browser: FakeCdpBrowser): Promise<WebPageInspection> => {
const result = await new CdpBrowserProvider().inspectPage(
inspectWebPageInputSchema.parse({
cdp_endpoint: browser.endpoint,
allowed_origins: [browser.allowedOrigin],
target_id: "allowed-page",
observation_ms: 0,
}),
);
if (!result.ok) throw result.error;
return result.value;
};
const capturePair = async (
before: readonly HttpRequestObservation[],
after: readonly HttpRequestObservation[],
): Promise<readonly [WebPageInspection, WebPageInspection]> => {
const fixture = await startHttpComparisonBrowser(before);
trackBrowser(fixture.browser);
const left = await inspect(fixture.browser);
fixture.setRequests(after);
return [left, await inspect(fixture.browser)];
};
const compareNetwork = (before: WebPageInspection, after: WebPageInspection) =>
compareWebCaptures(
compareWebCapturesInputSchema.parse({
before: { inspection: before },
after: { inspection: after },
}),
).dimensions.network;
const response = {
requestId: "request-before",
status: 200,
mimeType: "application/json",
} as const;
describe("HTTP comparison response coverage", () => {
it("does not compare an unavailable byte count with a completed response", async () => {
const [before, after] = await capturePair(
[response],
[{ ...response, requestId: "request-after", encodedDataLength: 100 }],
);
expect(before.network.requests[0]?.encoded_data_length).toBeNull();
expect(after.network.requests[0]?.encoded_data_length).toBe(100);
for (const [left, right] of [
[before, after],
[after, before],
] as const)
expect(compareNetwork(left, right)).toMatchObject({
status: "unknown",
total_changes: 0,
changes: [],
});
});
it("does not treat an unobserved response as a changed response", async () => {
const [before, after] = await capturePair(
[{ requestId: "request-before" }],
[{ ...response, encodedDataLength: 100 }],
);
expect(before.network.requests).toEqual([
expect.objectContaining({
status: null,
mime_type: null,
encoded_data_length: null,
}),
]);
for (const [left, right] of [
[before, after],
[after, before],
] as const)
expect(compareNetwork(left, right)).toMatchObject({
status: "unknown",
total_changes: 0,
});
});
it("retains observed status changes when response bytes are unavailable", async () => {
const [before, after] = await capturePair(
[response],
[{ ...response, status: 500 }],
);
expect(compareNetwork(before, after)).toMatchObject({
status: "changed",
changes: [expect.objectContaining({ change: "modified" })],
});
});
it.each([
{ mimeType: "text/plain", encodedDataLength: 100 },
{ mimeType: "application/json", encodedDataLength: 200 },
])("retains observed response changes: %j", async (change) => {
const [before, after] = await capturePair(
[{ ...response, encodedDataLength: 100 }],
[{ ...response, ...change }],
);
expect(compareNetwork(before, after).status).toBe("changed");
});
});
describe("HTTP comparison repeated request coverage", () => {
it("does not assign missing evidence by request order or transient ID", async () => {
const [before, after] = await capturePair(
[
{ ...response, encodedDataLength: 100 },
{ ...response, requestId: "second-before" },
],
[
{ ...response, requestId: "second-after", encodedDataLength: 200 },
{ ...response, requestId: "first-after", encodedDataLength: 100 },
],
);
expect(before.network.requests).toHaveLength(2);
expect(after.network.requests).toHaveLength(2);
expect(compareNetwork(before, after)).toMatchObject({
status: "unknown",
total_changes: 0,
});
});
it("compares complete byte counts even when another field is incomplete", async () => {
const [before, after] = await capturePair(
[
{ ...response, encodedDataLength: 100 },
{ requestId: "second-before", encodedDataLength: 200 },
],
[
{ requestId: "second-after", encodedDataLength: 300 },
{ ...response, requestId: "first-after", encodedDataLength: 100 },
],
);
expect(compareNetwork(before, after).status).toBe("changed");
});
it("preserves the association between complete response fields", async () => {
const [before, after] = await capturePair(
[
{ ...response, encodedDataLength: 100 },
{
...response,
requestId: "second-before",
status: 201,
encodedDataLength: 200,
},
],
[
{ ...response, requestId: "first-after", encodedDataLength: 200 },
{
...response,
requestId: "second-after",
status: 201,
encodedDataLength: 100,
},
],
);
expect(compareNetwork(before, after).status).toBe("changed");
});
});
it.each([
{
name: "a pending request in an empty inventory",
before: [],
after: [{ requestId: "pending" }],
},
{
name: "a known request beside an existing pending request",
before: [{ requestId: "pending" }],
after: [
{ requestId: "pending" },
{
...response,
requestId: "new",
path: "/api/new",
encodedDataLength: 100,
},
],
},
])("preserves additions and removals of $name", async (observations) => {
const [before, after] = await capturePair(
observations.before,
observations.after,
);
// Live CDP observations have attach-limited inventories. Their absence
// remains unknown even when a request is seen in the other capture.
for (const [left, right] of [
[before, after],
[after, before],
] as const)
expect(compareNetwork(left, right)).toMatchObject({
status: "unknown",
total_changes: 0,
changes: [],
});
// Project explicitly complete inventory coverage without changing the
// producer's request facts; response fields remain independently unknown.
markNetworkInventoriesComplete(before, after);
for (const [left, right, change] of [
[before, after, "added"],
[after, before, "removed"],
] as const)
expect(compareNetwork(left, right)).toMatchObject({
status: "changed",
total_changes: 1,
changes: [expect.objectContaining({ change })],
});
});
it("requires response field coverage even when capture sections are complete", async () => {
const [before, after] = await capturePair(
[response],
[{ ...response, encodedDataLength: 100 }],
);
markNetworkInventoriesComplete(before, after);
expect(compareNetwork(before, before).status).toBe("unknown");
expect(compareNetwork(before, after).status).toBe("unknown");
expect(compareNetwork(after, after).status).toBe("unchanged");
});
const markNetworkInventoriesComplete = (
...inspections: WebPageInspection[]
): void => {
const sections = new Set([
"network_requests",
"websocket_connections",
"websocket_frames",
]);
for (const inspection of inspections)
for (const key of [
"policy_filtered_sections",
"attach_limited_sections",
"truncated_sections",
"unavailable_sections",
] as const)
inspection.completeness[key] = inspection.completeness[key].filter(
(section) => !sections.has(section),
);
};
@@ -0,0 +1,69 @@
import { createHash } from "node:crypto";
import { createServer } from "node:http";
import { expect, it } from "vitest";
import { fetchWebSourceMaps } from "../../../src/browser/WebSourceMapFetcher.js";
import { analyzeWebBundleInputSchema } from "../../../src/domain/webBundleAnalysis.js";
it("retains unshadowed optional require dependencies from HTTP source-map originals", async () => {
const source = [
'require("./normal.cjs");',
'require?.("./optional.cjs");',
'function local(require) { require?.("./shadowed.cjs"); }',
"require?.(selectedModule);",
].join("\n");
const text = JSON.stringify({
version: 3,
names: [],
mappings: "AAAA",
sources: ["main.cjs"],
sourcesContent: [source],
});
const server = createServer((_request, response) => response.end(text));
try {
await new Promise<void>((resolve, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolve);
});
const address = server.address();
if (address === null || typeof address === "string")
throw new TypeError("Expected a TCP listener address");
const origin = `http://127.0.0.1:${String(address.port)}`;
const url = `${origin}/app.js.map`;
const result = await fetchWebSourceMaps(
[{ scriptKey: `scr_${"1".repeat(64)}`, declaredUrl: url, fetchUrl: url }],
analyzeWebBundleInputSchema.parse({
cdp_endpoint: origin,
target_id: "fixture",
allowed_origins: [origin],
fetch_source_maps: true,
}),
);
expect(result.items[0]).toMatchObject({
status: "included",
artifact: {
text,
bytes: Buffer.byteLength(text),
sha256: createHash("sha256").update(text).digest("hex"),
},
original_sources: [{ artifact: { text: source } }],
original_module_edges: ["./normal.cjs", "./optional.cjs"].map(
(specifier) => ({
from_source: `${origin}/main.cjs`,
kind: "require",
specifier,
resolved_source: new URL(specifier, origin).href,
}),
),
});
} finally {
server.closeAllConnections();
if (server.listening)
await new Promise<void>((resolve, reject) => {
server.close((error) =>
error === undefined ? resolve() : reject(error),
);
});
}
});
+42 -83
View File
@@ -446,91 +446,50 @@ describe("web capture WebSocket payload byte coverage", () => {
});
describe("web capture diff", () => {
it("treats unavailable HTTP response facts as unknown while retaining observed changes", async () => {
const browser = await startFakeCdpBrowser();
browsers.push(browser);
const captured = await new CdpBrowserProvider().inspectPage(
inspectWebPageInputSchema.parse({
cdp_endpoint: browser.endpoint,
allowed_origins: [browser.allowedOrigin],
target_id: "allowed-page",
observation_ms: 0,
}),
);
if (!captured.ok) throw captured.error;
const before = structuredClone(captured.value);
const request = before.network.requests[0];
if (request === undefined) throw new Error("Missing network request");
request.status = null;
request.mime_type = null;
request.encoded_data_length = null;
markSectionsComplete(before, [
"network_requests",
"websocket_connections",
"websocket_frames",
"json_body_shapes",
]);
for (const item of before.network.requests)
item.body_shapes.status = "not_approved";
it.each(["status", "mime_type"] as const)(
"keeps an independently unavailable HTTP %s unknown",
async (field) => {
const browser = await startFakeCdpBrowser();
browsers.push(browser);
const captured = await new CdpBrowserProvider().inspectPage(
inspectWebPageInputSchema.parse({
cdp_endpoint: browser.endpoint,
allowed_origins: [browser.allowedOrigin],
target_id: "allowed-page",
observation_ms: 0,
}),
);
if (!captured.ok) throw captured.error;
const observed = structuredClone(captured.value);
const request = observed.network.requests[0];
if (request === undefined) throw new Error("Missing network request");
request.status = 200;
request.mime_type = "application/json";
request.encoded_data_length = 100;
markSectionsComplete(observed, [
"network_requests",
"websocket_connections",
"websocket_frames",
"json_body_shapes",
]);
for (const item of observed.network.requests)
item.body_shapes.status = "not_approved";
expect(compareNetwork(observed, observed).status).toBe("unchanged");
const after = structuredClone(before);
const responseObserved = after.network.requests[0];
if (responseObserved === undefined)
throw new Error("Missing network request");
responseObserved.status = 200;
responseObserved.mime_type = "application/json";
responseObserved.encoded_data_length = 100;
expect(compareNetwork(before, after)).toMatchObject({
status: "unknown",
total_changes: 0,
changes: [],
});
expect(compareNetwork(after, before)).toMatchObject({
status: "unknown",
total_changes: 0,
});
expect(compareNetwork(before, before).status).toBe("unknown");
for (const field of [
"status",
"mime_type",
"encoded_data_length",
] as const) {
const partial = structuredClone(after);
const partial = structuredClone(observed);
partial.network.requests[0]![field] = null;
expect(compareNetwork(partial, after).status).toBe("unknown");
expect(compareNetwork(after, partial).status).toBe("unknown");
}
const inventoryBefore = structuredClone(before);
inventoryBefore.network.requests = [];
expect(compareNetwork(inventoryBefore, before)).toMatchObject({
status: "changed",
total_changes: 1,
changes: [expect.objectContaining({ change: "added" })],
});
const statusChanged = structuredClone(after);
statusChanged.network.requests[0]!.status = 500;
expect(compareNetwork(after, statusChanged)).toMatchObject({
status: "changed",
changes: [expect.objectContaining({ change: "modified" })],
});
after.network.requests[0]!.encoded_data_length = null;
expect(compareNetwork(after, statusChanged).status).toBe("changed");
const repeated = structuredClone(statusChanged);
const duplicate = structuredClone(repeated.network.requests[0]!);
duplicate.status = 200;
duplicate.mime_type = "text/plain";
repeated.network.requests.push(duplicate);
const reassociated = structuredClone(repeated);
reassociated.network.requests[0]!.mime_type = "text/plain";
reassociated.network.requests.at(-1)!.mime_type = "application/json";
expect(compareNetwork(repeated, reassociated).status).toBe("changed");
repeated.network.requests[0]!.status = null;
expect(compareNetwork(repeated, reassociated).status).toBe("unknown");
});
for (const [left, right] of [
[partial, observed],
[observed, partial],
[partial, partial],
] as const)
expect(compareNetwork(left, right)).toMatchObject({
status: "unknown",
total_changes: 0,
changes: [],
});
},
);
it("ignores transient request IDs and capture-approval state", async () => {
const browser = await startFakeCdpBrowser();
@@ -29,6 +29,104 @@ const expectInvalidIncludedSourceMaps = (result: WebSourceMaps): void => {
expectInvalidSourceMaps({ ...result, status: "unavailable" });
};
it.each(["regular", "indexed"])(
"collects an HTTP %s source map with omitted names without accepting invalid names",
async (format) => {
const leaf = {
version: 3,
sources: ["src/main.ts"],
sourcesContent: ["import './dependency.ts';"],
mappings: "AAAA",
};
const texts = [
leaf,
{ ...leaf, names: [] },
{ ...leaf, names: null },
{ ...leaf, names: 42 },
{ ...leaf, names: [42] },
{ ...leaf, mappings: "AAAAA" },
].map((map) =>
JSON.stringify(
format === "indexed"
? {
version: 3,
sections: [{ offset: { line: 2, column: 5 }, map }],
}
: map,
),
);
const server = createServer((incoming, response) => {
response.setHeader("content-type", "application/source-map+json");
response.end(texts[Number(incoming.url?.slice(1))]);
});
try {
await new Promise<void>((resolve, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolve);
});
const address = server.address();
if (address === null || typeof address === "string")
throw new TypeError("Expected a TCP listener address");
const localOrigin = `http://127.0.0.1:${String(address.port)}`;
const result = await fetchWebSourceMaps(
texts.map((_, index) => ({
...request,
fetchUrl: `${localOrigin}/${index}`,
declaredUrl: `${localOrigin}/${index}`,
})),
input({ allowed_origins: [localOrigin] }),
);
expect(result.items.map(({ status }) => status)).toEqual([
"included",
"included",
"invalid",
"invalid",
"invalid",
"invalid",
]);
for (const [index, text] of texts.slice(0, 2).entries())
expect(result.items[index]).toMatchObject({
artifact: {
text,
bytes: Buffer.byteLength(text),
sha256: createHash("sha256").update(text).digest("hex"),
},
original_sources: [{ source: `${localOrigin}/src/main.ts` }],
mappings: [
{
generated_line: format === "indexed" ? 3 : 1,
generated_column: format === "indexed" ? 5 : 0,
source: `${localOrigin}/src/main.ts`,
original_line: 1,
original_column: 0,
name: null,
},
],
original_module_edges: [
{
from_source: `${localOrigin}/src/main.ts`,
resolved_source: `${localOrigin}/src/dependency.ts`,
},
],
});
for (const item of result.items.slice(2))
expect(item).toMatchObject({
artifact: null,
mappings: [],
original_sources: [],
});
} finally {
server.closeAllConnections();
if (server.listening)
await new Promise<void>((resolve, reject) => {
server.close((error) =>
error === undefined ? resolve() : reject(error),
);
});
}
},
);
it("parses ECMA-426 HTTP source-map prefixes while retaining exact response evidence", async () => {
const map = JSON.stringify({
version: 3,
@@ -712,7 +810,6 @@ describe("web source-map sourcesContent validation", () => {
it.each([
["a scalar sourcesContent value", { sourcesContent: "source" }],
["a non-string, non-null source entry", { sourcesContent: [42] }],
["a sourcesContent array with the wrong length", { sourcesContent: [] }],
])("reports %s as invalid", async (_description, content) => {
const result = await fetchWebSourceMaps([request], input(), undefined, {
fetch: () =>
@@ -743,6 +840,50 @@ describe("web source-map sourcesContent validation", () => {
});
});
it.each([
["shorter", ["import './a.ts';"]],
["longer", ["import './a.ts';", null, "unused trailing content"]],
])(
"keeps every source declaration when sourcesContent is %s than sources",
async (_length, sourcesContent) => {
const result = await fetchWebSourceMaps([request], input(), undefined, {
fetch: () =>
Promise.resolve(
new Response(
JSON.stringify({
version: 3,
sources: ["a.ts", "b.ts"],
sourcesContent,
names: [],
mappings: "",
}),
{ status: 200 },
),
),
});
expect(result.items[0]).toMatchObject({
status: "included",
limitation: null,
original_sources: [
{
source_index: 0,
declaration_index: 0,
source: `${origin}/assets/a.ts`,
artifact: { text: "import './a.ts';" },
},
{
source_index: 1,
declaration_index: 1,
source: `${origin}/assets/b.ts`,
artifact: null,
},
],
});
expect(result.items[0]?.original_sources).toHaveLength(2);
},
);
it("accepts null source-map source contents", async () => {
const result = await fetchWebSourceMaps([request], input(), undefined, {
fetch: () =>
@@ -1,3 +1,7 @@
import {
descriptorFailurePath,
expectRetainedDescriptor,
} from "../../fixtures/descriptorCloseFailure.js";
import { expect, it, onTestFinished, vi } from "vitest";
import { writeFile } from "node:fs/promises";
import type { FileHandle } from "node:fs/promises";
@@ -15,27 +19,22 @@ const cleanup = vi.hoisted(() => ({
}));
vi.mock("node:fs/promises", async (importOriginal) => {
const filesystem = await importOriginal<typeof import("node:fs/promises")>();
return {
...filesystem,
open: async (...args: Parameters<typeof filesystem.open>) => {
const handle = await filesystem.open(...args);
if (args[0] === cleanup.path) {
cleanup.handles.push(handle);
const close = handle.close.bind(handle);
handle.close = async () => {
if (cleanup.denied && handle.fd >= 0)
throw new Error("Descriptor close deliberately denied");
await close();
};
}
return handle;
},
};
const { withDescriptorCloseFailure } =
await import("../../fixtures/descriptorCloseFailure.js");
return withDescriptorCloseFailure(filesystem, cleanup);
});
it
.runIf(process.platform === "linux")
.each(["capture", "tool", "limiter", "kernel"])(
.each([
"capture",
"tool",
"limiter",
"kernel",
"field-tool",
"field-limiter",
"field-kernel",
])(
"retains failed %s descriptors and retries them before further acquisition and shutdown",
async (member) => {
const directory = await createTestTempDirectory("rea-capture-descriptor-");
@@ -67,14 +66,16 @@ it
},
() => PrivateRuntimeRoot.create({ parent: directory }),
);
cleanup.path =
member === "capture"
? path
: member === "tool"
? command
: member === "limiter"
? limiter
: "/proc/self/limits";
const discovery = member.startsWith("field-");
const resource = discovery ? member.slice(6) : member;
const operation = discovery
? "inspect_packet_fields"
: "inspect_packet_capture";
cleanup.path = descriptorFailurePath(resource, {
capture: path,
tool: command,
limiter,
});
cleanup.denied = true;
cleanup.handles = [];
onTestFinished(async () => {
@@ -84,24 +85,45 @@ it
cleanup.path = "";
});
const service = new PacketCaptureService(provider);
const first = await service.inspect({ path, fields: ["frame.number"] });
expect(first).toMatchObject({
ok: false,
error: { cleanupIncomplete: true, cleanupResources: [cleanup.path] },
});
expect(cleanup.handles).toHaveLength(1);
expect(cleanup.handles[0]?.fd).toBeGreaterThanOrEqual(0);
const first = discovery
? await service.inspectFields({ query: "usb" })
: await service.inspect({ path, fields: ["frame.number"] });
expectRetainedDescriptor(first, cleanup, operation);
const launchCount = launches;
expect(
await service.inspect({ path, fields: ["frame.number"] }),
).toMatchObject({ ok: false, error: { cleanupIncomplete: true } });
).toMatchObject({
ok: false,
error: { cleanupIncomplete: true, operation },
});
expect(cleanup.handles).toHaveLength(1);
expect(launches).toBe(launchCount);
let closingOperation = operation;
if (discovery) {
cleanup.denied = false;
const recovered = await service.inspect({
path,
fields: ["frame.number"],
});
expect(recovered.ok).toBe(true);
expect(cleanup.handles.every((handle) => handle.fd === -1)).toBe(true);
expect(launches).toBeGreaterThan(launchCount);
cleanup.denied = true;
expect(
await service.inspect({ path, fields: ["frame.number"] }),
).toMatchObject({
ok: false,
error: { cleanupIncomplete: true, operation: "inspect_packet_capture" },
});
expect(cleanup.handles.at(-1)?.fd).toBeGreaterThanOrEqual(0);
closingOperation = "inspect_packet_capture";
}
await expect(provider.close()).rejects.toMatchObject({
cleanupIncomplete: true,
operation: closingOperation,
});
cleanup.denied = false;
await provider.close();
expect(cleanup.handles[0]?.fd).toBe(-1);
expect(cleanup.handles.every((handle) => handle.fd === -1)).toBe(true);
},
);
@@ -11,6 +11,9 @@ import { PrivateRuntimeRoot } from "../../../src/process/PrivateRuntimeRoot.js";
import { spawnOwnedProviderProcess } from "../../../src/process/ProviderProcess.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { createDeferred } from "../../fixtures/binarySession.js";
import { Cli } from "incur";
import { registerCaptureCommands } from "../../../src/cli/captureCommands.js";
import { silentLogger } from "../../../src/logger.js";
// A command-boundary simulator: real process ownership/filesystem, not real TShark coverage.
const fixture = async (mode = "ok", failCleanup = false) => {
@@ -625,3 +628,34 @@ it("retains completed discovery on late cancellation and cleanup failure", async
await f.service.close();
}
});
it("preserves discovery resource ownership through CLI shutdown retries", async () => {
const f = await fixture("cleanup", true);
const cli = Cli.create("discovery-cleanup", { sync: false });
registerCaptureCommands(cli, silentLogger, {}, f.service);
let stdout = "";
try {
await cli.serve(
["inspect-packet-fields", "--query", "usb", "--format", "json"],
{
stdout: (text) => {
stdout += text;
},
exit: () => {},
},
);
expect(JSON.parse(stdout)).toMatchObject({
code: "cleanup_incomplete",
details: {
diagnostics: {
cleanup_error: {
details: { operation: "inspect_packet_fields" },
},
},
},
});
} finally {
f.allowCleanup();
await f.service.close();
}
});
@@ -0,0 +1,92 @@
import { expect, it } from "vitest";
import { Cli } from "incur";
import pino from "pino";
import type { ApktoolResourceAnalysisPort } from "../../../src/application/apktool/ApktoolResourceAnalysisPort.js";
import { createAnalysisExecution } from "../../../src/application/AnalysisProvider.js";
import { registerApktoolCommands } from "../../../src/cli/apktoolCommands.js";
import { presentOmittableDefaults } from "../../../src/cli/omittableDefaults.js";
import type { ApktoolRequest } from "../../../src/domain/apktool/apktoolResourceAnalysis.js";
import { ok } from "../../../src/domain/result.js";
const invoke = async (argv: readonly string[]) => {
const requests: ApktoolRequest[] = [];
const provider: ApktoolResourceAnalysisPort = {
async inspectAvailability() {
throw new Error("unexpected provider probe");
},
async close() {},
async execute(request) {
requests.push(request);
return ok(
createAnalysisExecution(
{ strings: [] },
{ id: "apktool", name: "Apktool", version: null },
),
);
},
};
const cli = Cli.create("rea", { sync: false });
registerApktoolCommands(cli, pino({ level: "silent" }), {}, provider);
presentOmittableDefaults(cli);
let stdout = "";
let exitCode = 0;
await cli.serve([...argv], {
env: {},
exit: (code) => {
exitCode = code;
},
stdout: (text) => {
stdout += text;
},
});
return { exitCode, stdout, requests };
};
/**
* Locales the apktool input owner admits. The owner's own rejection message
* advertises `b+zh+Hans+CN`, so the CLI cannot be narrower than this list
* without refusing a caller-selected locale the decoding lane supports.
*/
const OWNER_ADMITTED_LOCALES = [
"de",
"pt-BR",
"pt-rBR",
"zh-rCN",
"en-419",
"b+zh+Hans+CN",
] as const;
it("forwards every locale the apktool input owner admits", async () => {
for (const locale of OWNER_ADMITTED_LOCALES) {
const result = await invoke([
"decode-android-resources",
"/targets/app.apk",
"--locale",
locale,
"--json",
]);
expect(result.exitCode, `exit code for locale ${locale}`).toBe(0);
expect(result.requests, `request for locale ${locale}`).toEqual([
{
operation: "decode_android_resources",
input: { path: "/targets/app.apk", include_strings: true, locale },
},
]);
}
});
it("still refuses a locale the apktool input owner refuses", async () => {
const result = await invoke([
"decode-android-resources",
"/targets/app.apk",
"--locale",
"not a locale",
"--json",
]);
expect(result.exitCode).not.toBe(0);
expect(result.requests).toEqual([]);
});
+48
View File
@@ -183,6 +183,54 @@ it(
INTEGRATION_TEST_TIMEOUT_MS,
);
it(
"browser CLI attaches through a browser socket and records it redacted",
async () => {
const browser = await startFakeCdpBrowser();
browsers.push(browser);
const inspected = await runCli(
[
"inspect-web-page",
browser.browserWebSocketUrl,
"allowed-page",
"--observation-ms",
"0",
"--json",
],
process.env,
);
expect(inspected).toMatchObject({
operation: "inspect_web_page",
parameters: {
cdp_endpoint: browser.browserWebSocketUrl.replace(
/fake$/u,
"[REDACTED]",
),
},
normalized_result: { target: { target_id: "allowed-page" } },
});
expect(JSON.stringify(inspected)).not.toContain("/devtools/browser/fake");
const refused = await runCli(
[
"list-browser-targets",
browser.browserWebSocketUrl.replace(/fake$/u, "not-this-browser"),
"--json",
],
process.env,
);
expect(refused).toMatchObject({
code: "provider_unavailable",
details: {
operation: "list_browser_targets",
reason: "endpoint_unreachable",
},
});
expect(JSON.stringify(refused)).not.toContain("not-this-browser");
},
INTEGRATION_TEST_TIMEOUT_MS,
);
describe("scenario comparison CLI diagnostics", () => {
const rule = {
rule_id: "domain",
+36 -57
View File
@@ -188,8 +188,12 @@ it("closes the service and retains an unexpected producer rejection", async () =
});
});
it("rejects an oversized selector budget before the CLI invokes its service", async () => {
const cli = Cli.create("capture-selector-test", { sync: false });
/** Serve one inspection request whose service must never be reached. */
const rejectBeforeService = async (
name: string,
args: string[],
): Promise<string> => {
const cli = Cli.create(name, { sync: false });
let called = false;
registerCaptureCommands(
cli,
@@ -207,66 +211,41 @@ it("rejects an oversized selector budget before the CLI invokes its service", as
},
);
let stdout = "";
await cli.serve(
[
"inspect-packet-capture",
"recording.pcap",
"--fields",
"x".repeat(8192),
"--display_filter",
"界".repeat(8192),
"--format",
"json",
],
{
stdout: (text) => {
stdout += text;
},
exit: () => {},
await cli.serve(args, {
stdout: (text) => {
stdout += text;
},
);
exit: () => {},
});
expect(called).toBe(false);
return stdout;
};
it("rejects an oversized selector budget before the CLI invokes its service", async () => {
const stdout = await rejectBeforeService("capture-selector-test", [
"inspect-packet-capture",
"recording.pcap",
"--fields",
"x".repeat(8192),
"--display_filter",
"界".repeat(8192),
"--format",
"json",
]);
expect(stdout).toContain("32 KiB");
});
it("rejects conflicting output options before the CLI invokes its service", async () => {
const cli = Cli.create("capture-window-test", { sync: false });
let called = false;
registerCaptureCommands(
cli,
silentLogger,
{},
{
inspectFields: async () => {
throw new Error("Unexpected field discovery call");
},
inspect: async () => {
called = true;
throw new Error("Must not inspect");
},
close: async () => {},
},
);
let stdout = "";
await cli.serve(
[
"inspect-packet-capture",
"recording.pcap",
"--fields",
"ip.addr",
"--all_matches",
"--match_limit",
"20",
"--format",
"json",
],
{
stdout: (text) => {
stdout += text;
},
exit: () => {},
},
);
expect(called).toBe(false);
const stdout = await rejectBeforeService("capture-window-test", [
"inspect-packet-capture",
"recording.pcap",
"--fields",
"ip.addr",
"--all_matches",
"--match_limit",
"20",
"--format",
"json",
]);
expect(stdout).toContain("mutually exclusive");
});
@@ -287,9 +287,12 @@ cliTest("uninstall names each retained skill directory", async ({ cli }) => {
};
const skill = report.items?.find(({ name }) => name === "skill");
expect(skill?.status).toBe("retained");
for (const directory of [shared, claude]) {
for (const [client, directory] of [
["shared", shared],
["claude_code", claude],
] as const) {
expect(skill?.detail).toContain(
`${directory}: retained (Skill manifest is not REA-owned.)`,
`${client} (${directory}): retained (Skill manifest is not REA-owned.)`,
);
expect(await readFile(join(directory, "SKILL.md"), "utf8")).toBe(
"---\nname: unrelated\n---\n",
@@ -23,7 +23,7 @@ const analyze = async (cli: TestCli, extra = "") => {
const output = await cli.run({
arguments: ["analyze-javascript-application", root, "--json"],
});
expect(output.exitCode).toBe(0);
expect(output.exitCode, output.stderr).toBe(0);
const { normalized_result: result } = z
.object({
normalized_result: javascriptApplicationAnalysisResultSchema,
@@ -122,6 +122,190 @@ cliTest(
},
);
cliTest(
"traces a nested contextBridge member through a unique IPC channel",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-electron-nested-bridge-");
await writeFixtureFiles(root, {
"preload.js": `const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("api", {
nested: { write: (value) => ipcRenderer.invoke("nested:write", value) }
});`,
"main.js": `const { ipcMain, clipboard } = require("electron");
ipcMain.handle("nested:write", (event, value) => clipboard.writeText(value));`,
});
const output = await cli.run({
arguments: ["analyze-javascript-application", root, "--json"],
});
expect(output.exitCode, output.stderr).toBe(0);
const { normalized_result: application } = z
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
.parse(output.json);
const { graph } = application;
const paths = graph.nodes
.flatMap((node) => node.observations)
.filter(
(observation) =>
observation.properties.entry === "api.nested.write" &&
observation.properties.entry_kind === "bridge-member",
);
expect(
paths.some(
(observation) => observation.properties.step_kind === "exposure",
),
).toBe(true);
expect(
paths.some(
(observation) => observation.properties.step_kind === "terminal",
),
).toBe(true);
const joins = graph.edges.filter(
(edge) =>
edge.evidence.extractor.operation === "join-electron-wrapper-paths",
);
expect(joins.map((edge) => edge.properties.channel)).toEqual([
"nested:write",
]);
const traceInput = join(root, "trace-nested-input.json");
await writeFile(
traceInput,
JSON.stringify({
application: output.json,
seed: { kind: "api", value: "api.nested.write", match: "exact" },
direction: "outgoing",
}),
);
const traced = await cli.run({
arguments: ["trace-application-feature", traceInput, "--json"],
});
expect(traced.exitCode).toBe(0);
const { normalized_result: trace } = z
.object({ normalized_result: applicationFeatureTraceResultSchema })
.parse(traced.json);
expect(trace.seed_matches).toHaveLength(1);
expect(trace.paths.some((path) => path.contains_inference)).toBe(true);
},
);
cliTest(
"preserves exact bridge member segments through the public CLI",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-electron-bridge-segments-");
await writeFixtureFiles(root, {
"preload.js": `const { contextBridge, ipcRenderer } = require("electron");
const write = () => ipcRenderer.send("write");
contextBridge.exposeInMainWorld("desktop", {
"nested.write": write,
nested: { write },
__proto__: { inherited: write },
["__proto__"]: { own: write },
});`,
"main.js": `const { ipcMain } = require("electron");
ipcMain.on("write", (event) => event.sender.copy());`,
});
const output = await cli.run({
arguments: ["analyze-javascript-application", root, "--json"],
});
expect(output.exitCode).toBe(0);
const { normalized_result: application } = z
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
.parse(output.json);
const observations = application.graph.nodes.flatMap((node) =>
node.observations.filter(
(observation) =>
observation.properties.entry_kind === "bridge-member" &&
observation.properties.step_kind === "exposure",
),
);
const bridge = application.graph.nodes
.filter((node) => node.kind === "context-bridge-api")
.flatMap((node) => node.observations)
.find((observation) => observation.properties.api_key === "desktop");
expect(bridge?.properties.members).not.toContain("__proto__.inherited");
expect(bridge?.properties.members).toContain("__proto__.own");
const paths = observations.map(
(observation) => observation.properties.entry_path,
);
expect(paths).toEqual(
expect.arrayContaining([
["desktop", "nested.write"],
["desktop", "nested", "write"],
["desktop", "__proto__", "own"],
]),
);
expect(paths).not.toContainEqual(["desktop", "__proto__", "inherited"]);
const handlerPaths = application.graph.nodes
.flatMap((node) => node.observations)
.filter(
(observation) => observation.properties.entry_kind === "ipc-handler",
);
expect(handlerPaths.length).toBeGreaterThan(0);
expect(
handlerPaths.every(
(observation) => !("entry_path" in observation.properties),
),
).toBe(true);
},
);
cliTest(
"retains a nested bridge path when its IPC handler has no external terminal",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-electron-nested-return-");
await writeFixtureFiles(root, {
"preload.js": `const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("api", {
nested: { write: (value) => ipcRenderer.invoke("nested:write", value) }
});`,
"main.js": `const { ipcMain } = require("electron");
ipcMain.handle("nested:write", (event, value) => value);`,
});
const output = await cli.run({
arguments: ["analyze-javascript-application", root, "--json"],
});
expect(output.exitCode).toBe(0);
const { normalized_result: application } = z
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
.parse(output.json);
const { graph } = application;
expect(
graph.nodes.some((node) =>
node.observations.some(
(observation) =>
observation.properties.entry === "api.nested.write" &&
observation.properties.step_kind === "exposure",
),
),
).toBe(true);
expect(
graph.edges.some(
(edge) =>
edge.evidence.extractor.operation === "join-electron-wrapper-paths",
),
).toBe(false);
const traceInput = join(root, "trace-nested-input.json");
await writeFile(
traceInput,
JSON.stringify({
application: output.json,
seed: { kind: "api", value: "api.nested.write", match: "exact" },
direction: "outgoing",
}),
);
const traced = await cli.run({
arguments: ["trace-application-feature", traceInput, "--json"],
});
expect(traced.exitCode).toBe(0);
const { normalized_result: trace } = z
.object({ normalized_result: applicationFeatureTraceResultSchema })
.parse(traced.json);
expect(trace.seed_matches).toHaveLength(1);
expect(trace.graph?.nodes.length).toBeGreaterThan(1);
},
);
for (const surface of ["CLI", "stdio MCP"] as const) {
cliTest(
`preserves handler ambiguity and short-circuit factory paths through ${surface}`,
@@ -225,3 +409,89 @@ cliTest(
expect(joins.map((e) => e.properties.channel)).toEqual(["test:cut"]);
},
);
cliTest(
"keeps shallow bridge members when a sibling exceeds the nesting limit",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-electron-deep-bridge-");
let deepMember = '() => e.ipcRenderer.send("deep:run")';
for (let index = 0; index < 129; index += 1)
deepMember = `{ level${index}: ${deepMember} }`;
await writeFixtureFiles(root, {
"preload.js": `const e=require("electron");
e.contextBridge.exposeInMainWorld("desktop", {
deep: ${deepMember},
shallow: () => e.ipcRenderer.send("shallow:run")
});`,
"main.js": `const e=require("electron");
e.ipcMain.on("shallow:run", event => event.sender.copy());
e.ipcMain.on("deep:run", event => event.sender.copy());`,
});
const output = await cli.run({
arguments: ["analyze-javascript-application", root, "--json"],
});
expect(output.exitCode).toBe(0);
const { normalized_result: application } = z
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
.parse(output.json);
const { graph } = application;
const joins = graph.edges.filter(
(edge) =>
edge.evidence.extractor.operation === "join-electron-wrapper-paths",
);
expect(joins.map((edge) => edge.properties.channel)).toEqual([
"shallow:run",
]);
expect(
graph.nodes
.flatMap((node) => node.observations)
.some(
(observation) =>
observation.properties.entry === "desktop.shallow" &&
observation.properties.step_kind === "exposure",
),
).toBe(true);
},
);
cliTest(
"preserves empty bridge member segments in nested paths",
async ({ cli }) => {
const root = await createTestTempDirectory(
"rea-electron-empty-bridge-key-",
);
await writeFixtureFiles(root, {
"preload.js": `const { contextBridge, ipcRenderer } = require("electron");
contextBridge.exposeInMainWorld("desktop", {
"": { nested: { call: () => ipcRenderer.send("empty:key") } }
});`,
"main.js": `const { ipcMain } = require("electron");
ipcMain.on("empty:key", event => event.sender.copy());`,
});
const output = await cli.run({
arguments: ["analyze-javascript-application", root, "--json"],
});
expect(output.exitCode).toBe(0);
const { normalized_result: application } = z
.object({ normalized_result: javascriptApplicationAnalysisResultSchema })
.parse(output.json);
const observations = application.graph.nodes.flatMap(
(node) => node.observations,
);
expect(
observations.some(
(observation) =>
observation.properties.entry === "desktop..nested.call" &&
observation.properties.step_kind === "exposure",
),
).toBe(true);
expect(
application.graph.edges
.filter(
(edge) =>
edge.evidence.extractor.operation === "join-electron-wrapper-paths",
)
.map((edge) => edge.properties.channel),
).toEqual(["empty:key"]);
},
);
@@ -0,0 +1,32 @@
import { readFile } from "node:fs/promises";
import { expect } from "vitest";
import { z } from "zod";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
const packageSchema = z.object({ version: z.string() });
// Node's Windows home lookup throws for an empty USERPROFILE, so a dependency
// that resolves the home during import used to stop every command there.
cliTest("starts when the selected home is empty", async ({ cli }) => {
const { version } = packageSchema.parse(
JSON.parse(await readFile("package.json", "utf8")),
);
const cwd = await createTestTempDirectory("rea-empty-home-startup-");
const environment = { HOME: "", USERPROFILE: "" };
const reported = await cli.run({
arguments: ["--version"],
cwd,
environment,
});
expect(reported.stderr).toBe("");
expect(reported.exitCode).toBe(0);
expect(reported.stdout.trim()).toBe(version);
const help = await cli.run({ arguments: ["--help"], cwd, environment });
expect(help.stderr).not.toContain("uv_os_homedir");
expect(help.exitCode).toBe(0);
});
@@ -0,0 +1,77 @@
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { webPageInspectionSchema } from "../../../src/domain/browserObservationSchemas.js";
import { parseEvidence } from "../../../src/domain/evidence.js";
import { runCliJson } from "../../fixtures/cliJsonProcess.js";
import { startHttpComparisonBrowser } from "../../fixtures/httpComparisonBrowser.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
it("CLI compares incomplete HTTP responses without inventing changes", async () => {
const root = await createTestTempDirectory("rea-http-comparison-");
const response = {
requestId: "request-1",
status: 200,
mimeType: "application/json",
};
const fixture = await startHttpComparisonBrowser([response]);
try {
const inspect = async () =>
webPageInspectionSchema.parse(
parseEvidence(
await runCliJson([
"inspect-web-page",
fixture.browser.endpoint,
"allowed-page",
"--observation-ms",
"0",
"--json",
]),
).normalized_result,
);
const pending = await inspect();
fixture.setRequests([{ ...response, encodedDataLength: 100 }]);
const finished = await inspect();
fixture.setRequests([{ ...response, status: 500, encodedDataLength: 100 }]);
const changed = await inspect();
expect(pending.network.requests).toMatchObject([
{ status: 200, mime_type: "application/json", encoded_data_length: null },
]);
expect(finished.network.requests).toMatchObject([
{ status: 200, mime_type: "application/json", encoded_data_length: 100 },
]);
const paths = [pending, finished, changed].map((_, index) =>
join(root, `capture-${String(index)}.json`),
);
await Promise.all(
[pending, finished, changed].map((inspection, index) =>
writeFile(paths[index]!, JSON.stringify({ inspection })),
),
);
for (const [left, right, status, count] of [
[0, 1, "unknown", 0],
[1, 0, "unknown", 0],
[0, 2, "changed", 1],
] as const) {
const compared = await runCliJson([
"compare-web-captures",
paths[left]!,
paths[right]!,
"--json",
]);
expect(compared).toMatchObject({
operation: "compare_web_captures",
normalized_result: {
overall_status: status,
dimensions: {
network: { status, total_changes: count },
},
},
});
}
} finally {
await fixture.browser.close();
}
}, 60_000);
@@ -79,6 +79,7 @@ describe("headless IDA timeout through the one-shot CLI", () => {
backend: "worker",
owned: null,
input_path: null,
is_active: null,
},
],
},
@@ -71,4 +71,27 @@ describe("native UI scenario CLI input", () => {
});
},
);
cliTest(
"refuses scenario steps on passive observation instead of dropping them",
async ({ cli }) => {
const result = await cli.run({
arguments: [
"observe-native-ui",
"/provider-must-not-be-invoked",
"--pid",
"1",
"--window-id",
"1",
"--steps",
'[{"kind":"wait","milliseconds":0}]',
"--json",
],
environment: { REA_LOG_LEVEL: "silent" },
});
expect(result.exitCode).not.toBe(0);
expect(result.stdout).toContain("Unknown flag: --steps");
},
);
});
@@ -0,0 +1,190 @@
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect } from "vitest";
import {
createHistoricalSourceManifest,
parseHistoricalSourceGraph,
} from "../../../src/domain/referenceSourceGraph.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { cliTest } from "../../support/cli/cliFixture.js";
describe.skipIf(process.platform === "win32")(
"compiled historical-source summaries",
() => {
cliTest(
"selects a committed summary without changing complete output",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-reference-summary-");
await mkdir(join(root, "src", "nested"), { recursive: true });
await Promise.all([
writeFile(join(root, "package.json"), '{"name":"fixture"}\n'),
writeFile(
join(root, "src", "main.js"),
'import "./nested/value.js";\n',
),
writeFile(
join(root, "src", "nested", "value.js"),
"export const value = 1;\n",
),
writeFile(join(root, "\uE000.js"), "export const first = 1;\n"),
writeFile(join(root, "\u{10000}.js"), "export const last = 2;\n"),
]);
const run = (...options: string[]) =>
cli.run({
arguments: ["import-reference-source", root, "--json", ...options],
cwd: root,
});
const complete = await run();
expect(complete.exitCode).toBe(0);
const graph = parseHistoricalSourceGraph(complete.json);
expect((await run("--detail", "full")).stdout).toBe(complete.stdout);
for (const depth of [undefined, 0, 1, 10]) {
const result = await run(
"--detail",
"summary",
...(depth === undefined ? [] : ["--outline-depth", String(depth)]),
);
expect(result.exitCode).toBe(0);
const selected = graph.entries.filter(
(entry) => entry.path.split("/").length <= (depth ?? 2),
);
expect(result.json).toMatchObject({
schema: "HistoricalSourceSummary",
authority: graph.authority,
root_alias: graph.root_alias,
manifest: createHistoricalSourceManifest(graph),
inventory_state: graph.inventory_state,
provenance: graph.provenance,
vcs: graph.vcs,
languages: graph.languages,
manifests: graph.manifests,
counts: {
entries: 7,
files: 5,
directories: 2,
symlinks: 0,
relationships: 1,
parse_failures: 0,
exclusions: 0,
},
outline: {
depth: depth ?? 2,
entries: selected.map(({ path, kind }) =>
expect.objectContaining({ path, kind }),
),
omitted_entry_count: graph.entries.length - selected.length,
},
});
expect(result.json).not.toHaveProperty("relationships");
}
},
);
cliTest(
"retains exclusions and parse coverage independently of a hidden outline",
async ({ cli }) => {
const root = await createTestTempDirectory(
"rea-reference-partial-summary-",
);
await Promise.all([
writeFile(join(root, ".gitignore"), "ignored.txt\n"),
writeFile(join(root, "ignored.txt"), "ignored\n"),
writeFile(join(root, "broken.js"), "export const = ;\n"),
]);
const result = await cli.run({
arguments: [
"import-reference-source",
root,
"--detail",
"summary",
"--outline-depth",
"0",
"--json",
],
cwd: root,
});
expect(result.exitCode).toBe(0);
expect(result.json).toMatchObject({
inventory_state: "partial",
counts: { parse_failures: 1, exclusions: 1 },
exclusions: [
{
path: "ignored.txt",
reason: "project-ignored",
pattern: "ignored.txt",
},
],
outline: { depth: 0, entries: [], omitted_entry_count: 2 },
});
},
);
},
);
describe("compiled historical-source summary input boundaries", () => {
cliTest(
"documents detail and depth in public command help",
async ({ cli }) => {
const result = await cli.run({
arguments: ["import-reference-source", "--help"],
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain("--detail");
expect(result.stdout).toContain("--outline-depth");
expect(result.stdout).toContain("requires --detail summary");
},
);
for (const options of [
["--detail", "summry"],
["--detail", "summary", "--outline-depth", "-1"],
["--detail", "summary", "--outline-depth", "1.5"],
["--detail", "summary", "--outline-depth", "NaN"],
["--detail", "summary", "--outline-depth", "9007199254740992"],
["--outline-depth", "1"],
["--detail", "full", "--outline-depth", "1"],
]) {
cliTest(`rejects ${options.join(" ")}`, async ({ cli }) => {
const root = await createTestTempDirectory(
"rea-reference-summary-input-",
);
const result = await cli.run({
arguments: ["import-reference-source", root, ...options, "--json"],
cwd: root,
});
expect(result.exitCode).toBe(1);
expect(result.stdout).not.toContain("HistoricalSourceGraph");
expect(result.stdout).not.toContain("HistoricalSourceSummary");
expect(result.stdout).toMatch(/detail|outline/u);
});
}
cliTest(
"preserves the missing-root failure in summary mode",
async ({ cli }) => {
const root = await createTestTempDirectory(
"rea-reference-summary-missing-",
);
const result = await cli.run({
arguments: [
"import-reference-source",
join(root, "missing"),
"--detail",
"summary",
"--json",
],
cwd: root,
});
expect(result.exitCode).toBe(1);
expect(result.json).toMatchObject({
error: "Import failed",
category: "invalid_input",
message: expect.stringContaining(
"Reference source root could not be resolved",
),
});
},
);
});
@@ -0,0 +1,75 @@
import { execFile } from "node:child_process";
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
import { expect, it } from "vitest";
import { solidityMetadataSchema } from "../../../src/domain/evm/solidityMetadata.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
/** Real compiled worker and unchanged WASM engine; no Linux provider-admission claim. */
it.each([
["compiler", "00a164736f6c634300081e000a", "decoded"],
["integer", "00a1011bffffffffffffffff000b", "decoded"],
["BOM text", "00a1617864efbbbf610008", "decoded"],
["nested value", "00a1617881000005", "partial"],
["unavailable trailer", "00", "unavailable"],
["unsupported tag", "00a16178c0000005", "not-decoded"],
["floating point", "00a16178fa3f8000000008", "not-decoded"],
["invalid UTF-8", "00a1617861ff0005", "not-decoded"],
])(
"retains independent %s metadata through the actual worker",
async (kind, hex, status) => {
const directory = await createTestTempDirectory("rea-solidity-worker-");
const snapshot = join(directory, "selected.hex");
const reply = join(directory, "reply.json");
const request = join(directory, "request.json");
await writeFile(snapshot, hex);
await writeFile(
request,
JSON.stringify({
snapshot_path: snapshot,
reply_path: reply,
failure_marker_path: join(directory, "resource.failure"),
encoding: "hex",
}),
);
await promisify(execFile)(
process.execPath,
[
"--max-old-space-size=256",
"--disable-wasm-trap-handler",
new URL("../../../dist/evm/EvmoleInterfaceWorker.js", import.meta.url)
.pathname,
request,
],
{ timeout: 30_000, maxBuffer: 1024 * 1024 },
);
const value = JSON.parse(await readFile(reply, "utf8"));
expect(value.ok, JSON.stringify(value)).toBe(true);
expect(value.bytecode.hex).toBe(hex);
const metadata = solidityMetadataSchema.parse(value.bytecode.metadata);
expect(metadata.status).toBe(status);
if (kind === "integer")
expect(metadata.entries[0]?.value).toEqual({
kind: "integer",
value: "18446744073709551615",
});
if (kind === "compiler")
expect(metadata.compiler_version?.value).toBe("0.8.30");
if (kind === "BOM text")
expect(metadata.entries[0]?.value).toEqual({
kind: "text",
value: "\ufeffa",
});
if (kind === "nested value")
expect(metadata.entries[0]?.value).toEqual({
kind: "uninterpreted",
container: "array",
});
if (kind === "unavailable trailer") {
expect(metadata.bytecode_offset).toBeNull();
expect(metadata.entries).toEqual([]);
expect(metadata.compiler_version).toBeNull();
}
},
);
@@ -0,0 +1,130 @@
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { buffer } from "node:stream/consumers";
import { setImmediate } from "node:timers/promises";
import { expect, it } from "vitest";
import { AsarArtifactReader } from "../../../src/artifacts/AsarArtifactReader.js";
import { readAsarListing } from "../../../src/artifacts/AsarListing.js";
import {
asarHeaderBytes,
nestedAsarHeader,
} from "../../fixtures/asarHeader.js";
import { inventoryArtifact } from "../../fixtures/artifactInventory.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const archiveWith = async (bytes: Buffer) => {
const root = await createTestTempDirectory("rea-asar-listing-");
const path = join(root, "app.asar");
await writeFile(path, bytes);
return path;
};
it("interrupts expanded listing admission between traversal batches", async () => {
const controller = new AbortController();
const header = {
files: Object.fromEntries(
Array.from({ length: 4000 }, (_, index) => [
`f${index}`,
{ size: 0, offset: "0" },
]),
),
};
const listing = readAsarListing(header, controller.signal);
await setImmediate();
controller.abort();
await expect(listing).rejects.toMatchObject({ reason: "cancelled" });
});
it("rejects path expansion before publishing any inventory, preserving the input", async () => {
const bytes = nestedAsarHeader(64, "d".repeat(256), 4000);
expect(bytes.length).toBeLessThan(150_000);
const path = await archiveWith(bytes);
await expect(inventoryArtifact(path)).rejects.toMatchObject({
reason: "limit",
message: "ASAR expanded listing exceeds its 64 MiB representation budget",
});
expect(await readFile(path)).toEqual(bytes);
});
it("reads deeply nested packed metadata without recursive listing or member lookup", async () => {
const path = await archiveWith(nestedAsarHeader(3500, "d", 1));
const reader = new AsarArtifactReader(path);
try {
let count = 0;
for await (const entry of reader.entries()) {
count++;
if (entry.kind === "file") {
expect(entry.path).toBe(`${"d/".repeat(3500)}f0`);
expect(await buffer(await reader.open(entry))).toEqual(Buffer.alloc(0));
}
}
expect(count).toBe(3501);
} finally {
await reader.close();
}
});
it.each(["..", ".", "", "a/../b", "a/b", "a\0b"])(
"rejects raw ASAR component %j before host normalization can alias another entry",
async (component) => {
const json = JSON.stringify({
files: { [component]: { size: 0, offset: "0" } },
});
await expect(
inventoryArtifact(await archiveWith(asarHeaderBytes(json))),
).rejects.toMatchObject({ reason: "path" });
},
);
it("orders raw names portably and retains prototype-named entries and links", async () => {
const path = await archiveWith(
asarHeaderBytes(
'{"files":{"a0":{"size":0,"offset":"0"},"__proto__":{"size":0,"offset":"0"},"a":{"files":{"b":{"link":"a0"}}},"😀":{"size":0,"offset":"0"},"\uE000":{"size":0,"offset":"0"}}}',
),
);
const reader = new AsarArtifactReader(path);
try {
const entries = [];
for await (const entry of reader.entries())
entries.push([entry.path, entry.kind]);
expect(entries).toEqual([
["__proto__", "file"],
["a", "directory"],
["a/b", "symlink"],
["a0", "file"],
["\uE000", "file"],
["😀", "file"],
]);
} finally {
await reader.close();
}
});
it("preserves POSIX literal backslashes and rejects Windows component separators", async () => {
const path = await archiveWith(
asarHeaderBytes(
JSON.stringify({ files: { "a\\b": { size: 0, offset: "0" } } }),
),
);
const reader = new AsarArtifactReader(path);
try {
const list = async () => {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
return entries;
};
if (process.platform === "win32") {
await expect(list()).rejects.toMatchObject({ reason: "path" });
} else {
const entries = await list();
expect(entries).toHaveLength(1);
expect(entries[0]?.path).toBe("a\\b");
expect(await buffer(await reader.open(entries[0]!))).toEqual(
Buffer.alloc(0),
);
}
} finally {
await reader.close();
}
});
@@ -118,14 +118,21 @@ it("reuses only scheduled qualification and preserves every explicit full reques
).toBe("fast");
});
it("keeps every translated root README and authored guide on the documentation lane", async () => {
it("keeps every translated README and authored guide on the documentation lane", async () => {
const { directory, base } = await fixture();
const readmes = (await readdir(new URL("../../../", import.meta.url))).filter(
(name) => /^README(?:_.*)?\.md$/u.test(name),
);
const translated = (
await readdir(new URL("../../../docs/readme/", import.meta.url))
)
.filter((name) => /^README_.*\.md$/u.test(name))
.map((name) => `docs/readme/${name}`);
expect(readmes).toContain("README.md");
expect(translated.length).toBeGreaterThan(0);
const head = await change(directory, [
...readmes,
...translated,
"docs/guide.md",
"AGENTS.md",
"CONTRIBUTING.md",
@@ -198,7 +205,7 @@ it("emits an observational owner proposal without changing ordinary or full port
it("does not charge a documentation PR for unrelated implementation updates on main", async () => {
const { directory, base } = await fixture();
await git(directory, ["switch", "-c", "docs/change"]);
const head = await change(directory, ["README_zh-TW.md"]);
const head = await change(directory, ["docs/readme/README_zh-TW.md"]);
await git(directory, ["switch", "main"]);
const advancedBase = await change(directory, ["src/anotherProvider.ts"]);
expect(advancedBase).not.toBe(base);
@@ -240,7 +247,7 @@ it("retains complete checks when implementation is renamed into documentation",
it("keeps main pushes scoped when a successful ancestor baseline is available", async () => {
const { directory, base } = await fixture();
const head = await change(directory, ["README_ja.md"]);
const head = await change(directory, ["docs/readme/README_ja.md"]);
expect(await classify(directory, base, head, "push")).toMatchObject({
tier: "fast",
code: false,
@@ -449,6 +456,10 @@ it.each([
sqlite: false,
},
],
[
"scripts/verify-solidity-metadata.mjs",
{ code: true, evm: true, browser: false, package: false },
],
[
"src/browser/execution/evaluate.ts",
{
@@ -470,7 +481,10 @@ it.each([
{ code: false, website: true, package: false },
],
["scripts/website_i18n.py", { code: false, website: true, package: false }],
["README_ja.md", { code: false, docs: true, website: true, package: false }],
[
"docs/readme/README_ja.md",
{ code: false, docs: true, website: true, package: false },
],
[
".github/workflows/website-pages.yml",
{ code: false, website: true, package: false },
@@ -924,7 +938,7 @@ it.each(["workflow_dispatch", "schedule"])(
it("honors ci:full for successive PR revisions", async () => {
const { directory, base } = await fixture();
const first = await change(directory, ["README.md"]);
const second = await change(directory, ["README_ja.md"]);
const second = await change(directory, ["docs/readme/README_ja.md"]);
for (const head of [first, second]) {
expect(
await classify(directory, base, head, "pull_request", true),
@@ -365,7 +365,9 @@ it("keeps fast feedback separate from complete qualification and release proof",
expect(ci.concurrency["cancel-in-progress"]).toBe(
"${{ github.event_name == 'pull_request' && inputs.source_sha == '' && !contains(github.event.pull_request.labels.*.name, 'ci:full') }}",
);
expect(ci.jobs.required?.if).toBe("${{ always() }}");
// Failed dependencies still reach the gate, but superseded PR runs must
// release their concurrency slot without waiting for another runner.
expect(ci.jobs.required?.if).toBe("${{ !cancelled() }}");
const shards = ci.jobs["test-shard"]?.steps ?? [];
const fast = shards.find((step) => step.run?.includes("npm run test:run"));
const full = shards.find((step) => step.run?.includes("test:ci:shard:run"));
@@ -0,0 +1,412 @@
import {
access,
mkdir,
readFile,
rm,
symlink,
writeFile,
} from "node:fs/promises";
import { dirname, join } from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { CLAUDE_DESKTOP_PACKAGE_FAMILY } from "../../../src/application/ClaudeDesktopConfigLocation.js";
import { readClientRegistrationStatuses } from "../../../src/application/ClientRegistrationStatus.js";
import { npxRegistrationCommand } from "../../../src/application/ClientRegistrationIdentity.js";
import {
configureClientConfiguration,
inspectClientConfiguration,
} from "../../../src/application/SetupClientConfiguration.js";
import { detectClients } from "../../../src/application/SetupHost.js";
import {
supportedClients,
type SetupClient,
} from "../../../src/application/SupportedClients.js";
import {
runUninstall,
systemUninstallHost,
} from "../../../src/application/Uninstall.js";
import { clearClientLocationEnvironment } from "../../fixtures/clientEnvironment.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
/** The Windows registration setup writes, independent of the test host. */
const command = npxRegistrationCommand("win32");
const CONFIG = "claude_desktop_config.json";
const roots: string[] = [];
beforeEach(clearClientLocationEnvironment);
afterEach(async () => {
vi.unstubAllEnvs();
await Promise.all(
roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
);
});
/** A Windows profile with separate roaming and local AppData roots. */
const windowsProfile = async () => {
const home = await createTestTempDirectory("rea-claude-desktop-");
roots.push(home);
const env = {
APPDATA: join(home, "AppData", "Roaming"),
LOCALAPPDATA: join(home, "AppData", "Local"),
};
const appDataConfig = join(env.APPDATA, "Claude", CONFIG);
const container = join(
env.LOCALAPPDATA,
"Packages",
CLAUDE_DESKTOP_PACKAGE_FAMILY,
);
const privateConfig = join(
container,
"LocalCache",
"Roaming",
"Claude",
CONFIG,
);
return { home, env, appDataConfig, container, privateConfig };
};
const claudeDesktop = (
home: string,
env: Readonly<Record<string, string>>,
): SetupClient => {
const client = supportedClients(home, "win32", env).find(
({ name }) => name === "claude_desktop",
);
if (client === undefined) throw new Error("claude_desktop is not supported");
return client;
};
const writeJson = async (path: string, value: unknown): Promise<void> => {
await mkdir(dirname(path), { recursive: true });
await writeFile(path, `${JSON.stringify(value, null, 2)}\n`);
};
const readJson = async (path: string): Promise<Record<string, unknown>> =>
JSON.parse(await readFile(path, "utf8")) as Record<string, unknown>;
const reaEntry = { command: command[0], args: command.slice(1) };
describe("Windows Claude Desktop configuration location", () => {
it("uses AppData when no MSIX package storage exists", async () => {
const { home, env, appDataConfig } = await windowsProfile();
await mkdir(
join(env.LOCALAPPDATA, "Packages", "Contoso.App_8wekyb3d8bbwe"),
{
recursive: true,
},
);
const client = claudeDesktop(home, env);
expect(client.configPath).toBe(appDataConfig);
expect(client.markerPath).toBe(dirname(appDataConfig));
expect(client.msixPackage).toBeUndefined();
expect(client.quitBeforeSetup).toBe(true);
});
it("uses the package-private copy that the MSIX package reads", async () => {
const { home, env, appDataConfig, container, privateConfig } =
await windowsProfile();
await writeJson(privateConfig, { preferences: { theme: "dark" } });
await writeJson(appDataConfig, { mcpServers: {} });
const client = claudeDesktop(home, env);
expect(client.configPath).toBe(privateConfig);
expect(client.markerPath).toBe(container);
expect(client.msixPackage).toEqual({
packageFamilyName: CLAUDE_DESKTOP_PACKAGE_FAMILY,
privateConfigPath: privateConfig,
appDataConfigPath: appDataConfig,
virtualizedRoots: [env.APPDATA, env.LOCALAPPDATA],
processView: "direct",
});
expect(
(await detectClients(home, "win32", env)).map(({ name }) => name),
).toContain("claude_desktop");
});
it("uses AppData while the MSIX package has no private copy", async () => {
const { home, env, appDataConfig, container, privateConfig } =
await windowsProfile();
await mkdir(container, { recursive: true });
await writeJson(appDataConfig, { preferences: {} });
const client = claudeDesktop(home, env);
expect(client.configPath).toBe(appDataConfig);
expect(client.msixPackage).toMatchObject({
privateConfigPath: privateConfig,
processView: "unknown",
});
// No private file shows whether the running package started this process.
const inspection = await inspectClientConfiguration(client, {}, command);
expect(inspection.status).toBe("invalid");
expect(inspection.status === "invalid" && inspection.remediation).toContain(
"Open Claude Desktop once if it has never run, quit it fully, then rerun setup",
);
});
it("creates the private copy when the MSIX package has no configuration", async () => {
const { home, env, privateConfig } = await windowsProfile();
await writeJson(join(dirname(privateConfig), "Local State"), {});
const client = claudeDesktop(home, env);
expect(client.configPath).toBe(privateConfig);
expect(client.msixPackage?.processView).toBe("direct");
await expect(
inspectClientConfiguration(client, {}, command),
).resolves.toEqual({ status: "create" });
await expect(
configureClientConfiguration(client, {}, command),
).resolves.toEqual({ status: "configured" });
expect(await readJson(privateConfig)).toEqual({
mcpServers: { rea: reaEntry },
});
});
it("reports an unrecognized Claude package instead of falling back to AppData", async () => {
const { home, env, appDataConfig } = await windowsProfile();
const foreign = join(env.LOCALAPPDATA, "Packages", "Claude_abcdefghjkmnp");
await mkdir(foreign, { recursive: true });
await writeJson(appDataConfig, { mcpServers: {} });
const client = claudeDesktop(home, env);
expect(client.configPath).toBe(foreign);
expect(client.configPathError).toContain("unrecognized publisher");
expect(client.configPathError).toContain(CLAUDE_DESKTOP_PACKAGE_FAMILY);
expect(
(await detectClients(home, "win32", env)).map(({ name }) => name),
).not.toContain("claude_desktop");
const status = (
await readClientRegistrationStatuses(home, "unused", {
platform: "win32",
environment: env,
})
).find(({ client: name }) => name === "claude_desktop");
expect(status).toMatchObject({ state: "invalid", config_path: foreign });
await expect(
inspectClientConfiguration(client, {}, command),
).resolves.toMatchObject({ status: "invalid" });
});
});
describe("MSIX Claude Desktop registration lifecycle", () => {
it("registers in the private copy and reports an AppData-only entry as missing", async () => {
const { home, env, appDataConfig, privateConfig } = await windowsProfile();
const privateOriginal = {
coworkUserFilesPath: "C:\\Users\\a\\Claude",
preferences: { menuBarEnabled: false },
mcpServers: { other: { command: "other", args: ["serve"] } },
};
await writeJson(privateConfig, privateOriginal);
// Earlier setups wrote AppData, which the package never reads.
await writeJson(appDataConfig, { mcpServers: { rea: reaEntry } });
const appDataOriginal = await readFile(appDataConfig, "utf8");
const status = async () =>
(
await readClientRegistrationStatuses(home, "unused", {
platform: "win32",
environment: env,
})
).find(({ client }) => client === "claude_desktop");
expect(await status()).toMatchObject({
state: "missing",
config_path: privateConfig,
});
expect((await status())?.remediation).toContain(
"Quit Claude Desktop fully",
);
const client = claudeDesktop(home, env);
await expect(
inspectClientConfiguration(client, {}, command),
).resolves.toEqual({
status: "update",
backupPath: `${privateConfig}.rea.backup`,
});
await expect(
configureClientConfiguration(client, {}, command),
).resolves.toEqual({
status: "configured",
backupPath: `${privateConfig}.rea.backup`,
});
expect(await readJson(privateConfig)).toEqual({
...privateOriginal,
mcpServers: {
...privateOriginal.mcpServers,
rea: reaEntry,
},
});
expect(await readJson(`${privateConfig}.rea.backup`)).toEqual(
privateOriginal,
);
expect(await readFile(appDataConfig, "utf8")).toBe(appDataOriginal);
expect(await status()).toMatchObject({
state: "aligned",
config_path: privateConfig,
});
await expect(
configureClientConfiguration(client, {}, command),
).resolves.toEqual({ status: "unchanged" });
});
it("refuses to write from a process the running package started", async () => {
const { home, env, privateConfig } = await windowsProfile();
await writeJson(privateConfig, { preferences: {} });
const resolved = claudeDesktop(home, env);
if (resolved.msixPackage === undefined)
throw new Error("expected MSIX package storage");
// Only a packaged process sees AppData redirected; model that observation.
const client: SetupClient = {
...resolved,
msixPackage: { ...resolved.msixPackage, processView: "redirected" },
};
const inspection = await inspectClientConfiguration(client, {}, command);
expect(inspection.status).toBe("invalid");
expect(inspection.status === "invalid" && inspection.remediation).toContain(
"Quit Claude Desktop fully, then rerun setup from a terminal that Claude Desktop did not start",
);
expect(await readJson(privateConfig)).toEqual({ preferences: {} });
await configureClientConfiguration(resolved, {}, command);
await expect(
inspectClientConfiguration(client, {}, command),
).resolves.toEqual({ status: "already_current" });
});
it("refuses a write once the package reads a newer private copy", async () => {
const { home, env, appDataConfig, privateConfig } = await windowsProfile();
await writeJson(join(dirname(privateConfig), "Preferences"), {});
await writeJson(appDataConfig, { preferences: {} });
const client = claudeDesktop(home, env);
expect(client.configPath).toBe(appDataConfig);
// Quitting the app after the plan was shown can save its private copy.
await writeJson(privateConfig, { preferences: {} });
await expect(
configureClientConfiguration(client, {}, command),
).resolves.toEqual({ status: "failed", reason: "target_changed" });
expect(await readJson(appDataConfig)).toEqual({ preferences: {} });
expect(await readJson(privateConfig)).toEqual({ preferences: {} });
});
it("registers in AppData while the package has no private copy", async () => {
const { home, env, appDataConfig, privateConfig } = await windowsProfile();
await writeJson(join(dirname(privateConfig), "Preferences"), {});
await writeJson(appDataConfig, { preferences: { theme: "light" } });
const client = claudeDesktop(home, env);
expect(client.configPath).toBe(appDataConfig);
expect(client.msixPackage?.processView).toBe("direct");
await expect(
configureClientConfiguration(client, {}, command),
).resolves.toMatchObject({ status: "configured" });
expect(await readJson(appDataConfig)).toEqual({
preferences: { theme: "light" },
mcpServers: { rea: reaEntry },
});
await expect(access(privateConfig)).rejects.toMatchObject({
code: "ENOENT",
});
});
});
describe("MSIX Claude Desktop uninstall", () => {
it("refuses uninstall when the package view is unknown", async () => {
const { home, env, appDataConfig, container } = await windowsProfile();
await mkdir(container, { recursive: true });
await writeJson(appDataConfig, { mcpServers: { rea: reaEntry } });
const result = await runUninstall(
false,
systemUninstallHost(home, undefined, env, "win32"),
);
expect(result.status).toBe("failed");
expect(result.items).toContainEqual(
expect.objectContaining({
name: "claude_desktop",
status: "failed",
detail: expect.stringContaining("rerun uninstall"),
}),
);
expect(await readJson(appDataConfig)).toEqual({
mcpServers: { rea: reaEntry },
});
});
it("retains an unrelated rea-named entry when the package view is unknown", async () => {
const { home, env, appDataConfig, container } = await windowsProfile();
await mkdir(container, { recursive: true });
const unrelated = { mcpServers: { rea: { command: "unrelated-server" } } };
await writeJson(appDataConfig, unrelated);
const result = await runUninstall(
false,
systemUninstallHost(home, undefined, env, "win32"),
);
expect(result.status).toBe("complete");
expect(await readJson(appDataConfig)).toEqual(unrelated);
});
it("blocks managed skill removal through redirected AppData before any removal", async () => {
const { home, env, appDataConfig, privateConfig } = await windowsProfile();
await writeJson(join(dirname(privateConfig), "Preferences"), {});
await mkdir(dirname(dirname(appDataConfig)), { recursive: true });
await symlink(dirname(privateConfig), dirname(appDataConfig), "dir");
const agents = join(env.APPDATA, "dsh", "agents");
const manifest = join(
agents,
"skills",
"reverse-engineer-anything",
"SKILL.md",
);
await mkdir(dirname(manifest), { recursive: true });
await writeFile(
manifest,
"---\nname: reverse-engineer-anything\n---\nmanaged fixture\n",
);
const environment = { ...env, DSH_AGENTS_HOME: agents };
const result = await runUninstall(
false,
systemUninstallHost(home, undefined, environment, "win32"),
);
expect(result.status).toBe("failed");
expect(result.items).toContainEqual(
expect.objectContaining({
name: "skill",
status: "failed",
detail: expect.stringContaining("redirects its writes"),
}),
);
expect(await readFile(manifest, "utf8")).toContain("managed fixture");
});
it("removes REA from both the private copy and AppData on uninstall", async () => {
const { home, env, appDataConfig, privateConfig } = await windowsProfile();
await writeJson(privateConfig, {
preferences: {},
mcpServers: { other: { command: "other" }, rea: reaEntry },
});
await writeJson(appDataConfig, { mcpServers: { rea: reaEntry } });
const result = await runUninstall(
false,
systemUninstallHost(home, undefined, env, "win32"),
);
expect(result.status).toBe("complete");
expect(await readJson(privateConfig)).toEqual({
preferences: {},
mcpServers: { other: { command: "other" } },
});
expect(await readJson(appDataConfig)).toEqual({ mcpServers: {} });
});
});
@@ -64,6 +64,7 @@ describe("client configuration filesystem lifecycle", () => {
"copilot_cli",
"commandcode",
"qwen_code",
"kimi",
"vscode",
"trae",
"grok_build",
@@ -1895,14 +1896,11 @@ describe("client configuration filesystem failure reporting", () => {
remove: () => Promise.reject(new Error("SECRET removal failure")),
}),
);
expect(result.items).toContainEqual(
expect.objectContaining({
name: "skill",
status: "failed",
detail: expect.stringContaining(
`Managed file could not be removed: ${join(skillRoot, "SKILL.md")}`,
),
}),
const skillItem = result.items.find(({ name }) => name === "skill");
expect(skillItem?.status).toBe("failed");
expect(skillItem?.detail).toContain(`shared (${skillRoot}): failed (`);
expect(skillItem?.detail).toContain(
`Managed file could not be removed: ${join(skillRoot, "SKILL.md")}`,
);
expect(JSON.stringify(result)).not.toContain("SECRET");
expect(await readFile(join(skillRoot, "SKILL.md"), "utf8")).toBe(
@@ -1944,6 +1942,42 @@ describe("client configuration filesystem failure reporting", () => {
});
});
describe("skill root client identifier labels", () => {
it("labels each retained or removed skill root by its client identifier", async () => {
const home = await createTestTempDirectory("rea-uninstall-skill-labels-");
roots.push(home);
const customClaudeDirectory = join(home, "claude-config");
const sharedRoot = join(home, ".agents/skills/reverse-engineer-anything");
const claudeRoot = join(
customClaudeDirectory,
"skills/reverse-engineer-anything",
);
await mkdir(sharedRoot, { recursive: true });
await mkdir(claudeRoot, { recursive: true });
await writeFile(
join(sharedRoot, "SKILL.md"),
"---\nname: another-skill\n---\n# Not REA-owned\n",
);
await writeFile(join(claudeRoot, "SKILL.md"), ownedSkillManifest);
const skillItem = await systemUninstallHost(home, testFileSystem, {
CLAUDE_CONFIG_DIR: customClaudeDirectory,
}).removeSkill();
expect(skillItem.name).toBe("skill");
expect(skillItem.detail).toContain(`shared (${sharedRoot}): retained (`);
expect(skillItem.detail).toContain(
`claude_code (${claudeRoot}): removed (`,
);
await expect(
readFile(join(claudeRoot, "SKILL.md"), "utf8"),
).rejects.toMatchObject({ code: "ENOENT" });
expect(await readFile(join(sharedRoot, "SKILL.md"), "utf8")).toContain(
"another-skill",
);
});
});
const testFileSystem: UninstallFileSystem = {
readText: (path) => readFile(path, "utf8"),
copy: (source, destination) =>
@@ -2195,3 +2195,135 @@ describe("DeepSeek Harness caller node metadata", () => {
},
);
});
describe("DeepSeek Harness uninstall of a shared operation", () => {
it("keeps a group patch's own fields and comment when its only row was REA's", async () => {
const { home, client } = await harnessClient();
const entry = canonicalEntry();
await writeFile(
client.configPath,
`- insert:
- id: user-a
name: plugin-a
# caller group patch
- id: tools
config:
sandbox: strict
insert:
- id: rea-mcp
name: "${entry.name}"
config:
serverName: rea
transport: stdio
command: ${entry.config.command}
args: ${JSON.stringify(entry.config.args)}
`,
);
expect((await systemUninstallHost(home).removeClient(client)).status).toBe(
"removed",
);
const text = await readFile(client.configPath, "utf8");
expect(parseYaml(text)).toEqual([
{ insert: [{ id: "user-a", name: "plugin-a" }] },
{ id: "tools", config: { sandbox: "strict" } },
]);
expect(text).toContain("# caller group patch");
});
it("keeps an operation's own fields when setup removes a second REA row from it", async () => {
// Setup refuses a REA row in a group patch, so the setup path meets the
// keep rule only on an operation without a selector.
const { home, client } = await harnessClient();
const entry = canonicalEntry();
const row = (launcher: string) => ` - id: rea-mcp
name: "${entry.name}"
config:
serverName: rea
transport: stdio
command: ${launcher}
args: [mcp]
`;
await writeFile(
client.configPath,
`- insert:
${row("first-launcher")}# caller operation
- disabled: false
insert:
${row("second-launcher")}`,
);
expect(
(await configureClientConfiguration(client, {}, command)).status,
).toBe("configured");
const text = await readFile(client.configPath, "utf8");
expect(text).toContain("# caller operation");
expect(loadedInserts(text)).toHaveLength(1);
expect((parseYaml(text) as unknown[]).at(-1)).toEqual({ disabled: false });
expect(await dshStatus(home)).toMatchObject({ state: "aligned" });
});
it("drops an emptied operation that targets REA's own row", async () => {
const { home, client } = await harnessClient();
await writeFile(
client.configPath,
stringifyYaml([
{ insert: [{ id: "user-a", name: "plugin-a" }] },
{
id: "rea-mcp",
config: { serverName: "rea" },
insert: [canonicalEntry()],
},
]),
);
const host = systemUninstallHost(home);
expect((await host.removeClient(client)).status).toBe("removed");
expect(parseYaml(await readFile(client.configPath, "utf8"))).toEqual([
{ insert: [{ id: "user-a", name: "plugin-a" }] },
]);
});
it.each([
{ selector: { id: "tools" } },
{ selector: { name: "tools" } },
{ selector: { id: "tools", name: "tools" } },
])(
"drops an emptied operation left with only its target selector $selector",
async ({ selector }) => {
const { home, client } = await harnessClient();
await writeFile(
client.configPath,
stringifyYaml([
{ insert: [{ id: "user-a", name: "plugin-a" }] },
{ ...selector, insert: [canonicalEntry()] },
]),
);
expect(
(await systemUninstallHost(home).removeClient(client)).status,
).toBe("removed");
expect(parseYaml(await readFile(client.configPath, "utf8"))).toEqual([
{ insert: [{ id: "user-a", name: "plugin-a" }] },
]);
},
);
it("still drops an insert-only operation that REA's row emptied", async () => {
const { home, client } = await harnessClient();
await writeFile(
client.configPath,
stringifyYaml([
{ insert: [{ id: "user-a", name: "plugin-a" }] },
{ insert: [canonicalEntry()] },
]),
);
expect((await systemUninstallHost(home).removeClient(client)).status).toBe(
"removed",
);
expect(parseYaml(await readFile(client.configPath, "utf8"))).toEqual([
{ insert: [{ id: "user-a", name: "plugin-a" }] },
]);
});
});
@@ -1,5 +1,6 @@
import { createReadStream } from "node:fs";
import { open, readFile, rm, stat, utimes, writeFile } from "node:fs/promises";
import { createServer } from "node:net";
import { join } from "node:path";
import { Readable } from "node:stream";
import { Uint8ArrayReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js";
@@ -1122,11 +1123,16 @@ it.each(["identify_flutter_build", "inspect_dart_aot"] as const)(
if (result.ok) throw new Error("Duplicate libraries must be refused");
expect(projectAnalysisError(result.error)).toMatchObject({
code: "invalid_request",
details: {
issues: [
{
message: expect.stringContaining(
`duplicate Flutter library members: ${path}:`,
),
},
],
},
});
expect(JSON.stringify(projectAnalysisError(result.error))).toContain(
"duplicate Flutter library members",
);
expect(JSON.stringify(projectAnalysisError(result.error))).toContain(path);
expect(extract).not.toHaveBeenCalled();
},
);
@@ -1171,6 +1177,69 @@ it.each(["identify_flutter_build", "inspect_dart_aot"] as const)(
},
);
const expectSelectionIssue = async (
provider: FlutterBuildProvider,
path: string,
message: string,
): Promise<void> => {
for (const operation of [
"identify_flutter_build",
"inspect_dart_aot",
] as const) {
const result = await provider.execute({ operation, input: { path } });
if (result.ok) throw new Error("A non-file APK selection must fail");
expect(projectAnalysisError(result.error)).toMatchObject({
code: "invalid_request",
details: {
issues: [
{
path: ["path"],
reason: "invalid_format",
message: `${message}: ${path}`,
},
],
},
});
}
};
it("names a missing or non-file APK selection instead of blaming package bytes", async () => {
const root = await createTestTempDirectory("rea-flutter-selection-");
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
await expectSelectionIssue(
provider,
join(root, "absent.apk"),
"Selected APK does not exist",
);
await expectSelectionIssue(
provider,
root,
"Selected APK is not a regular file",
);
});
// Windows listens on named pipes rather than filesystem sockets.
it.skipIf(process.platform === "win32")(
"names an unopenable Unix socket APK selection as a non-file",
async () => {
const root = await createTestTempDirectory("rea-flutter-socket-");
const socketPath = join(root, "s.sock");
const socket = createServer();
await new Promise<void>((resolve) => socket.listen(socketPath, resolve));
onTestFinished(
() => new Promise<void>((resolve) => socket.close(() => resolve())),
);
const provider = new FlutterBuildProvider();
onTestFinished(() => provider.close());
await expectSelectionIssue(
provider,
socketPath,
"Selected APK is not a regular file",
);
},
);
it.each(["EACCES", "EPERM"] as const)(
"preserves %s at the shared source admission boundary",
async (code) => {
@@ -0,0 +1,79 @@
import { open, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, vi } from "vitest";
import { readElfBuildIds } from "../../../src/flutter/ElfBuildId.js";
import {
appendElfRanges,
engineFixtureImage,
} from "../../../src/flutter/FlutterBuild.fixture.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const note = Buffer.from(
"040000000800000003000000474e55000123456789abcdef",
"hex",
);
it.each([
{ bits: 32, table: "program" },
{ bits: 64, table: "section" },
] as const)(
"bounds dense $bits-bit $table note ranges while retaining examined IDs",
async ({ bits, table }) => {
const count = 30_000;
const image = appendElfRanges(
engineFixtureImage({
bits,
notes: Buffer.concat([note, Buffer.alloc(count)]),
ownership: "both",
}),
table,
Array.from({ length: count }, (_, index) => ({
type: table === "program" ? 4 : 7,
offset: 0x400 + note.length + index,
size: 1,
})),
);
const root = await createTestTempDirectory("rea-flutter-notes-");
const path = join(root, "engine.so");
await writeFile(path, image);
const file = await open(path, "r");
const read = vi.spyOn(file, "read");
try {
const result = await readElfBuildIds(file, image.length, undefined);
expect(result.candidates).toEqual(["0123456789abcdef"]);
expect(result.failures).toContainEqual({
reason: expect.stringContaining("note-range"),
});
expect(read.mock.calls.length).toBeLessThanOrEqual(1027);
} finally {
await file.close();
}
},
);
it("deduplicates shared program/section notes and skips empty ranges before charging inventory", async () => {
const ranges = Array.from({ length: 2000 }, (_, index) => ({
type: 7,
offset: index % 2 === 0 ? 0x400 : index,
size: index % 2 === 0 ? note.length : 0,
}));
const image = appendElfRanges(
engineFixtureImage({ notes: note, ownership: "both" }),
"section",
ranges,
);
const root = await createTestTempDirectory("rea-flutter-notes-");
const path = join(root, "engine.so");
await writeFile(path, image);
const file = await open(path, "r");
const read = vi.spyOn(file, "read");
try {
expect(await readElfBuildIds(file, image.length, undefined)).toEqual({
candidates: ["0123456789abcdef"],
failures: [],
});
expect(read.mock.calls.length).toBe(4);
} finally {
await file.close();
}
});
+16 -21
View File
@@ -209,13 +209,7 @@ describe("installer TTY fallback", { timeout: 20_000 }, () => {
cwd: process.cwd(),
env: {
...process.env,
HOME: fixture.home,
TMPDIR: fixture.temporary,
PATH: `${fixture.bin}:/usr/bin:/bin`,
FAKE_NPM_LOG: fixture.npmLog,
FAKE_REA_LOG: fixture.reaLog,
FAKE_REA_SOURCE: join(fixture.bin, "rea-source"),
FAKE_NPM_PREFIX: join(fixture.home, ".npm-global"),
...installerEnvironment(fixture),
TERM: "xterm-256color",
},
name: "xterm-256color",
@@ -581,6 +575,19 @@ const executable = async (path: string, source: string): Promise<void> => {
await chmod(path, 0o755);
};
/** Fixture-owned installer environment shared by the exec, pipe and PTY runs. */
const installerEnvironment = (
fixture: InstallerFixture,
): Record<string, string> => ({
HOME: fixture.home,
TMPDIR: fixture.temporary,
PATH: `${fixture.bin}:/usr/bin:/bin`,
FAKE_NPM_LOG: fixture.npmLog,
FAKE_REA_LOG: fixture.reaLog,
FAKE_REA_SOURCE: join(fixture.bin, "rea-source"),
FAKE_NPM_PREFIX: join(fixture.home, ".npm-global"),
});
const runInstaller = async (
fixture: InstallerFixture,
args: readonly string[] = [],
@@ -590,13 +597,7 @@ const runInstaller = async (
execFileAsync("/bin/bash", [script, ...args], {
env: {
...process.env,
HOME: fixture.home,
TMPDIR: fixture.temporary,
PATH: `${fixture.bin}:/usr/bin:/bin`,
FAKE_NPM_LOG: fixture.npmLog,
FAKE_REA_LOG: fixture.reaLog,
FAKE_REA_SOURCE: join(fixture.bin, "rea-source"),
FAKE_NPM_PREFIX: join(fixture.home, ".npm-global"),
...installerEnvironment(fixture),
...overrides,
},
});
@@ -618,13 +619,7 @@ const runPipedInstaller = async (
new Promise((resolve, reject) => {
const child = spawn("/bin/bash", ["-s", "--", ...args], {
env: {
HOME: fixture.home,
TMPDIR: fixture.temporary,
PATH: `${fixture.bin}:/usr/bin:/bin`,
FAKE_NPM_LOG: fixture.npmLog,
FAKE_REA_LOG: fixture.reaLog,
FAKE_REA_SOURCE: join(fixture.bin, "rea-source"),
FAKE_NPM_PREFIX: join(fixture.home, ".npm-global"),
...installerEnvironment(fixture),
...overrides,
},
stdio: ["pipe", "pipe", "pipe"],
@@ -405,12 +405,16 @@ describe("keyed archive hierarchy coverage", () => {
const resources = join(bundle, "Contents", "Resources");
await mkdir(resources, { recursive: true });
const archive = join(resources, "Foundation.nib");
await compile("/usr/bin/xcrun", [
"swift",
"tests/conformance/native/keyed-archive-hierarchy.swift",
archive,
"40",
]);
await compile(
"/usr/bin/xcrun",
[
"swift",
"tests/conformance/native/keyed-archive-hierarchy.swift",
archive,
"40",
],
{ timeout: 90_000 },
);
const analysis = await analyzeInterfaceBuilderBundle({
bundlePath: bundle,
@@ -431,6 +435,7 @@ describe("keyed archive hierarchy coverage", () => {
),
).toHaveLength(41);
},
120_000,
);
});
@@ -696,6 +701,49 @@ describe("NIB projection byte amplification", () => {
});
});
cliTest(
"compiled CLI preserves distinct NIB keys and class identity",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-nib-name-identity-");
const bundle = join(root, "Example.app");
const resources = join(bundle, "Contents", "Resources");
await mkdir(resources, { recursive: true });
await mkdir(join(bundle, "Contents", "MacOS"), { recursive: true });
await writeFile(
join(bundle, "Contents", "MacOS", "App"),
thinMach(0xfeedfacf, 0x0100000c),
);
await writeFile(
join(bundle, "Contents", "Info.plist"),
"<plist><dict><key>CFBundleExecutable</key><string>App</string></dict></plist>",
);
await writeFile(
join(resources, "Identity.nib"),
encodeNibArchiveFixture({
classes: ["\uFEFFWidget"],
objects: [{ classIndex: 0, values: { count: 1, "\uFEFFcount": 2 } }],
}),
);
const output = await cli.run({
arguments: ["decode-interface-builder", bundle, "--json"],
environment: { REA_LOG_LEVEL: "silent", REA_ANALYSIS_PROVIDER: "auto" },
});
expect(output.exitCode, output.stderr).toBe(0);
const result = interfaceBuilderAnalysisSchema.parse(
parseEvidence(output.json).normalized_result,
);
expect(result.graph.nodes).toContainEqual(
expect.objectContaining({
attributes: expect.objectContaining({
class_name: "\uFEFFWidget",
nibValues: { count: 1, "\uFEFFcount": 2 },
}),
}),
);
expect(JSON.stringify(result)).toContain("\uFEFFWidget");
},
);
cliTest(
"compiled CLI preserves a good archive and reports repeated key and data omissions",
async ({ cli }) => {
@@ -9,7 +9,10 @@ import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js
import { javascriptApplicationAnalysisResultSchema } from "../../../../src/domain/javascript/javascriptApplicationAnalysis.js";
import { matchJavaScriptApplicationVersions } from "../../../../src/domain/javascript/javascriptApplicationVersionKeys.js";
import { findApplicationFeatureSeeds } from "../../../../src/domain/javascript/javascriptFeatureSeed.js";
import { traceApplicationFeatureInputSchema } from "../../../../src/domain/javascript/javascriptFeatureTraceSchemas.js";
import {
applicationFeatureSeedSchema,
traceApplicationFeatureInputSchema,
} from "../../../../src/domain/javascript/javascriptFeatureTraceSchemas.js";
import { analyzeJavaScriptApplication } from "../../../support/javascriptApplicationScope.js";
const analyzeSource = async (
@@ -76,6 +79,59 @@ it("retains empty literal evidence across the application boundary surfaces", as
);
});
it("keeps bridge entry paths stable through worker validation and API seed matching", async () => {
const output = await analyzeSource(`
const { contextBridge, ipcRenderer } = require("electron");
const literal = () => ipcRenderer.send("literal");
const nested = () => ipcRenderer.send("nested");
contextBridge.exposeInMainWorld("desktop", {
"nested.write": literal,
nested: { write: nested },
});
`);
const semanticPathIds = new Set(
(output.semantic_graph?.nodes ?? [])
.map(({ properties }) => properties.path_id)
.filter((pathId): pathId is string => typeof pathId === "string"),
);
const applicationPathIds = new Set(
output.graph.nodes.flatMap(({ observations }) =>
observations
.map(({ properties }) => properties.path_id)
.filter((pathId): pathId is string => typeof pathId === "string"),
),
);
expect(semanticPathIds).toEqual(applicationPathIds);
const matches = findApplicationFeatureSeeds(
output.graph.nodes,
applicationFeatureSeedSchema.parse({
kind: "api",
value: "desktop.nested.write",
entry_path: ["desktop", "nested.write"],
}),
);
expect(matches).toHaveLength(1);
expect(
output.graph.nodes
.find(({ node_id: nodeId }) => nodeId === matches[0]?.node_id)
?.observations.some(
({ properties }) =>
Array.isArray(properties.entry_path) &&
properties.entry_path.join("\0") === "desktop\0nested.write",
),
).toBe(true);
expect(
findApplicationFeatureSeeds(
output.graph.nodes,
applicationFeatureSeedSchema.parse({
kind: "api",
value: "desktop.nested.write",
}),
),
).toHaveLength(2);
});
it("analyzes a plain long-line loader without package metadata", async () => {
const output = await analyzeSource(
`var Module = {}; fetch(""); new Worker(""); var payload = "${"x".repeat(65_536)}";`,
@@ -240,8 +296,18 @@ it.each([
[{ kind: "channel", value: "" }, false],
[{ kind: "channel", value: "", match: "contains" }, false],
[{ kind: "node-id", value: "", match: "exact" }, false],
[{ kind: "api", value: "desktop.nested.write" }, true],
[
{
kind: "api",
value: "desktop.nested.write",
entry_path: ["desktop", "nested.write"],
},
true,
],
[{ kind: "channel", value: "channel", entry_path: ["channel"] }, false],
] as const)(
"validates empty seed %j at runtime and in the advertised schema",
"validates seed %j at runtime and in the advertised schema",
(seed, accepted) => {
expect(seedSchema.safeParse(seed).success).toBe(accepted);
expect(validateAdvertisedSeed(seed)).toBe(accepted);
@@ -135,7 +135,6 @@ it("retains every source declared by every included local source map", async ()
it.each([
["a non-array sourcesContent value", { sourcesContent: "source" }],
["a non-string, non-null source entry", { sourcesContent: [42] }],
["a source-content array with the wrong length", { sourcesContent: [] }],
])(
"reports %s as an invalid local source map",
async (_description, content) => {
@@ -210,6 +209,51 @@ it("accepts omitted and explicitly null source-map source contents", async () =>
expect(result.graph.coverage.status).toBe("complete");
});
it.each([
["shorter", ["export const answer = 42;"]],
["longer", ["export const answer = 42;", null, "unused trailing content"]],
])(
"keeps every source declaration when sourcesContent is %s than sources",
async (length, sourcesContent) => {
const root = await createTestTempDirectory(
`rea-javascript-${length}-source-content-`,
);
await writeFile(
join(root, "bundle.js.map"),
JSON.stringify({
version: 3,
sources: ["a.ts", "b.ts"],
sourcesContent,
names: [],
mappings: "AAAA;ACAA",
}),
);
const result = await reconstructJavaScriptArtifact({ input_path: root });
const originals = result.graph.nodes.filter(
({ identity }) => identity.strategy === "source-map-original",
);
expect(
originals
.map(({ identity, observations }) => ({
original_source:
"original_source" in identity ? identity.original_source : null,
source_index: observations[0]?.properties.source_index,
content_available: observations[0]?.properties.content_available,
}))
.sort(
(left, right) =>
Number(left.source_index) - Number(right.source_index),
),
).toEqual([
{ original_source: "a.ts", source_index: 0, content_available: true },
{ original_source: "b.ts", source_index: 1, content_available: false },
]);
expect(result.graph.coverage.status).toBe("complete");
},
);
it("retains unnamed source declarations without shifting later source evidence", async () => {
const root = await createTestTempDirectory("rea-javascript-unnamed-source-");
const text = JSON.stringify({
@@ -0,0 +1,384 @@
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { readClientRegistrationStatuses } from "../../../src/application/ClientRegistrationStatus.js";
import { setupRegistrationCommand } from "../../../src/application/SetupHost.js";
import { configureClientConfiguration } from "../../../src/application/SetupClientConfiguration.js";
import {
clientSkillDirectories,
supportedClients,
} from "../../../src/application/SupportedClients.js";
import { systemUninstallHost } from "../../../src/application/Uninstall.js";
import { MCP_STARTUP_POLICY } from "../../../src/mcpStartupPolicy.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import {
clearClientLocationEnvironment,
NPX_REGISTRATION_COMMAND,
} from "../../fixtures/clientEnvironment.js";
const command = NPX_REGISTRATION_COMMAND;
beforeEach(clearClientLocationEnvironment);
afterEach(() => vi.unstubAllEnvs());
const kimiPath = (
home: string,
platform: NodeJS.Platform,
env: Parameters<typeof supportedClients>[2] = {},
) => supportedClients(home, platform, env).find(({ name }) => name === "kimi");
const exists = async (path: string): Promise<boolean> => {
try {
await access(path);
return true;
} catch {
return false;
}
};
const expectedRegistration = (launcher: readonly string[]) => ({
command: launcher[0],
args: launcher.slice(1),
startupTimeoutMs: MCP_STARTUP_POLICY.kimiStartupTimeoutMs,
toolTimeoutMs: MCP_STARTUP_POLICY.kimiToolTimeoutMs,
});
describe("Kimi Code configuration paths and skills", () => {
it.each([
[undefined, join("/home/a/.kimi-code")],
["", join("/home/a/.kimi-code")],
[join("/tmp", "isolated-kimi"), join("/tmp", "isolated-kimi")],
])("resolves KIMI_CODE_HOME for %j", (override, directory) => {
const client = kimiPath(
"/home/a",
"linux",
override === undefined ? {} : { KIMI_CODE_HOME: override },
);
expect(client).toEqual({
name: "kimi",
displayName: "Kimi Code",
configPath: join(directory, "mcp.json"),
markerPath: directory,
format: "kimi",
});
});
it("uses the existing shared skill destination without a separate skill root", () => {
expect(clientSkillDirectories("/home/a", ["kimi"])).toEqual([
{ client: "shared", directory: "/home/a/.agents/skills" },
]);
});
});
describe("Kimi Code registration lifecycle", () => {
const kimiClient = async () => {
const home = await createTestTempDirectory("rea-kimi-");
const client = kimiPath(home, process.platform, {});
if (client?.markerPath === undefined)
throw new Error("missing Kimi Code client");
await mkdir(client.markerPath, { recursive: true });
return { home, client };
};
it("preserves the caller's enabledTools selection during refresh", async () => {
const { client } = await kimiClient();
const enabledTools = ["binary_session", "analyze_javascript_application"];
await writeFile(
client.configPath,
JSON.stringify({
mcpServers: {
rea: { command: "old-rea", args: ["mcp"], enabledTools },
},
}),
);
expect(
(await configureClientConfiguration(client, {}, command)).status,
).toBe("configured");
expect(
JSON.parse(await readFile(client.configPath, "utf8")).mcpServers.rea,
).toEqual({
...expectedRegistration(command),
enabledTools,
});
expect(
(await configureClientConfiguration(client, {}, command)).status,
).toBe("unchanged");
});
it("registers without type, preserves unrelated settings, backs up, and is idempotent", async () => {
const { home, client } = await kimiClient();
const original = `{
"customSetting": { "enabled": true },
"mcpServers": {
"other": { "command": "other-server", "startupTimeoutMs": 5000 }
}
}
`;
await writeFile(client.configPath, original);
expect(await configureClientConfiguration(client, {}, command)).toEqual({
status: "configured",
backupPath: `${client.configPath}.rea.backup`,
});
const text = await readFile(client.configPath, "utf8");
expect(JSON.parse(text)).toEqual({
customSetting: { enabled: true },
mcpServers: {
other: { command: "other-server", startupTimeoutMs: 5000 },
rea: expectedRegistration(command),
},
});
expect(text).not.toContain('"type"');
expect(text).not.toContain('"transport"');
expect(await readFile(`${client.configPath}.rea.backup`, "utf8")).toBe(
original,
);
expect(await configureClientConfiguration(client, {}, command)).toEqual({
status: "unchanged",
});
expect(await readFile(client.configPath, "utf8")).toBe(text);
expect(
(
await readClientRegistrationStatuses(home, undefined, {
environment: {},
})
).find(({ client: name }) => name === "kimi"),
).toMatchObject({ state: "aligned", command });
expect((await systemUninstallHost(home).removeClient(client)).status).toBe(
"removed",
);
expect(JSON.parse(await readFile(client.configPath, "utf8"))).toEqual({
customSetting: { enabled: true },
mcpServers: {
other: { command: "other-server", startupTimeoutMs: 5000 },
},
});
});
});
describe("Kimi Code package launchers", () => {
const kimiClient = async () => {
const home = await createTestTempDirectory("rea-kimi-launcher-");
const client = kimiPath(home, process.platform, {});
if (client?.markerPath === undefined)
throw new Error("missing Kimi Code client");
await mkdir(client.markerPath, { recursive: true });
return { home, client } as const;
};
it.each([
["npx", "linux"],
["bunx", "linux"],
["npx", "win32"],
["bunx", "win32"],
] as const)(
"keeps the %s launcher aligned on %s",
async (runner, platform) => {
const { home, client } = await kimiClient();
const launcher = setupRegistrationCommand(platform, runner);
expect(
(await configureClientConfiguration(client, {}, launcher)).status,
).toBe("configured");
expect(
JSON.parse(await readFile(client.configPath, "utf8")).mcpServers.rea,
).toEqual(expectedRegistration(launcher));
expect(
(
await readClientRegistrationStatuses(home, undefined, {
platform,
environment: {},
})
).find(({ client: name }) => name === "kimi"),
).toMatchObject({ state: "aligned", command: launcher });
expect(await configureClientConfiguration(client, {}, launcher)).toEqual({
status: "unchanged",
});
},
);
});
describe("Kimi Code transport validation", () => {
const kimiClient = async () => {
const home = await createTestTempDirectory("rea-kimi-validation-");
const client = kimiPath(home, process.platform, {});
if (client?.markerPath === undefined)
throw new Error("missing Kimi Code client");
await mkdir(client.markerPath, { recursive: true });
return { home, client };
};
it("creates a registration in a zero-byte file and accepts whitespace-only files", async () => {
const { home, client } = await kimiClient();
for (const original of ["", " \n\t"]) {
await writeFile(client.configPath, original);
expect(
(await configureClientConfiguration(client, {}, command)).status,
).toBe("configured");
expect(
JSON.parse(await readFile(client.configPath, "utf8")).mcpServers.rea,
).toEqual(expectedRegistration(command));
expect(await configureClientConfiguration(client, {}, command)).toEqual({
status: "unchanged",
});
expect(
(await systemUninstallHost(home).removeClient(client)).status,
).toBe("removed");
}
});
it.each([
'{"mcpServers": {',
'{/* comment */ "mcpServers": {}}',
'{"mcpServers": {},}',
'\uFEFF{"mcpServers": {}}',
])("rejects invalid strict JSON %j without writes", async (original) => {
const { client } = await kimiClient();
await writeFile(client.configPath, original);
expect(await configureClientConfiguration(client, {}, command)).toEqual({
status: "failed",
reason: "readback",
});
expect(await readFile(client.configPath, "utf8")).toBe(original);
expect(await exists(`${client.configPath}.rea.backup`)).toBe(false);
});
it("accepts explicit stdio transport without causing repeated rewrites", async () => {
const { home, client } = await kimiClient();
await writeFile(
client.configPath,
JSON.stringify({
mcpServers: {
rea: {
transport: "stdio",
...expectedRegistration(command),
},
},
}),
);
expect(
(
await readClientRegistrationStatuses(home, undefined, {
environment: {},
})
).find(({ client: name }) => name === "kimi"),
).toMatchObject({ state: "aligned", command });
expect(
(await configureClientConfiguration(client, {}, command)).status,
).toBe("configured");
expect(await configureClientConfiguration(client, {}, command)).toEqual({
status: "unchanged",
});
expect(await configureClientConfiguration(client, {}, command)).toEqual({
status: "unchanged",
});
expect(
(
await readClientRegistrationStatuses(home, undefined, {
environment: {},
})
).find(({ client: name }) => name === "kimi"),
).toMatchObject({ state: "aligned", command });
});
it.each(["sse"])(
"rejects explicit transport %j instead of silently accepting another protocol",
async (transport) => {
const { home, client } = await kimiClient();
await writeFile(
client.configPath,
JSON.stringify({
mcpServers: {
rea: {
transport,
...expectedRegistration(command),
},
},
}),
);
expect(
(
await readClientRegistrationStatuses(home, undefined, {
environment: {},
})
).find(({ client: name }) => name === "kimi"),
).toMatchObject({ state: "invalid", command: [] });
},
);
});
describe("Kimi Code timeout validation", () => {
const kimiClient = async () => {
const home = await createTestTempDirectory("rea-kimi-timeout-");
const client = kimiPath(home, process.platform, {});
if (client?.markerPath === undefined)
throw new Error("missing Kimi Code client");
await mkdir(client.markerPath, { recursive: true });
return { home, client } as const;
};
it.each([
["startupTimeoutMs", 1],
["toolTimeoutMs", 1],
["startupTimeoutMs", undefined],
["toolTimeoutMs", undefined],
] as const)("reports stale when %s is %j", async (field, value) => {
const { home, client } = await kimiClient();
const registration = {
...expectedRegistration(command),
[field]: value,
};
await writeFile(
client.configPath,
JSON.stringify({ mcpServers: { rea: registration } }),
);
expect(
(
await readClientRegistrationStatuses(home, undefined, {
environment: {},
})
).find(({ client: name }) => name === "kimi"),
).toMatchObject({ state: "stale" });
});
it("uses a direct installed REA command without requiring a package runner", async () => {
const { home, client } = await kimiClient();
const entry = join(home, "scripts", "rea.mjs");
const launcher = [process.execPath, entry, "mcp"];
expect(
(await configureClientConfiguration(client, {}, launcher)).status,
).toBe("configured");
expect(
(
await readClientRegistrationStatuses(home, entry, {
environment: {},
})
).find(({ client: name }) => name === "kimi"),
).toMatchObject({ state: "aligned", command: launcher });
});
it("leaves a foreign rea-key registration out of aligned state", async () => {
const { home, client } = await kimiClient();
await writeFile(
client.configPath,
JSON.stringify({
mcpServers: {
rea: {
...expectedRegistration(command),
command: "another-package",
args: ["mcp"],
},
},
}),
);
expect(
(
await readClientRegistrationStatuses(home, undefined, {
environment: {},
})
).find(({ client: name }) => name === "kimi"),
).toMatchObject({ state: "stale" });
});
});
@@ -37,6 +37,53 @@ import {
const root = fileURLToPath(new URL("../../..", import.meta.url));
const temporaryRoots: string[] = [];
const README_FACT_FIXTURE_PATHS = [
"README.md",
"docs/readme/README_zh.md",
"docs/readme/README_ja.md",
"docs/readme/README_ko.md",
"docs/readme/README_ar.md",
"website/locales/languages.json",
"docs/installation.md",
"AGENTS.md",
".github/pull_request_template.md",
] as const;
const README_LINK_DRIFT_MUTATIONS = [
[
"(../installation.md#supported-agents)",
"(../installation.md#missing-agents)",
],
[
"https://rea.tools/zh-CN/guides/native/",
"https://rea.tools/zh-CN/missing/",
],
[
"https://rea.tools/zh-CN/guides/native/",
"https://rea.tools/zh-TW/guides/native/",
],
[
"https://rea.tools/zh-CN/guides/native/",
"https://rea.tools/zh-CN/guides/native/?other=1",
],
[
'src="../assets/rea-hopper-analysis.png"',
'src="../assets/missing-hopper.png"',
],
['href="https://discord.gg/GkcryMnJDM"', 'href="https://discord.gg/missing"'],
] as const;
const seedReadmeFactFixture = async (directory: string) => {
for (const path of README_FACT_FIXTURE_PATHS) {
const destination = join(directory, path);
await mkdir(dirname(destination), { recursive: true });
await copyFile(join(root, path), destination);
}
await mkdir(join(directory, "skills/reverse-engineer-anything"), {
recursive: true,
});
};
afterEach(async () => {
await Promise.all(
temporaryRoots
@@ -176,49 +223,14 @@ describe("canonical product catalog drift", () => {
it("keeps translated readmes linked to canonical setup and tool details", async () => {
const directory = await createTestTempDirectory("rea-readme-facts-");
temporaryRoots.push(directory);
const paths = [
"README.md",
"README_zh.md",
"README_ja.md",
"README_ko.md",
"README_ar.md",
"website/locales/languages.json",
"docs/installation.md",
"AGENTS.md",
".github/pull_request_template.md",
];
for (const path of paths) {
const destination = join(directory, path);
await mkdir(dirname(destination), { recursive: true });
await copyFile(join(root, path), destination);
}
await mkdir(join(directory, "skills/reverse-engineer-anything"), {
recursive: true,
});
const translatedPath = join(directory, "README_zh.md");
await seedReadmeFactFixture(directory);
const translatedPath = join(directory, "docs/readme/README_zh.md");
const translated = await readFile(translatedPath, "utf8");
const catalog = await createProductCatalog(root);
await expect(documentationFactIssues(directory, catalog)).resolves.toEqual(
[],
);
for (const [original, changed] of [
[
"(docs/installation.md#supported-agents)",
"(docs/installation.md#missing-agents)",
],
[
"https://rea.tools/zh-CN/guides/native/",
"https://rea.tools/zh-CN/missing/",
],
[
"https://rea.tools/zh-CN/guides/native/",
"https://rea.tools/zh-TW/guides/native/",
],
[
"https://rea.tools/zh-CN/guides/native/",
"https://rea.tools/zh-CN/guides/native/?other=1",
],
] as const) {
for (const [original, changed] of README_LINK_DRIFT_MUTATIONS) {
expect(translated).toContain(original);
await writeFile(
translatedPath,
@@ -228,11 +240,10 @@ describe("canonical product catalog drift", () => {
await expect(
documentationFactIssues(directory, catalog),
).resolves.toEqual([
"README_zh.md: documentation links differ from README.md",
"docs/readme/README_zh.md: documentation links differ from README.md",
]);
}
});
it("fails check mode without rewriting a stale generated artifact", async () => {
const directory = await createTestTempDirectory("rea-generated-check-");
temporaryRoots.push(directory);
@@ -54,6 +54,42 @@ const holdReadHandle = (path: string, reason: string) => {
};
};
/** Linux setup host with its VS Code client configuration directory prepared. */
const preparedVsCodeSetupHost = async (prefix: string) => {
const home = await createTestTempDirectory(prefix);
const host = systemSetupHost(
{ platform: "linux", homeDirectory: home } as DoctorHost,
{ HOME: home, XDG_CONFIG_HOME: join(home, "xdg") },
);
const client = (await host.supportedClients()).find(
({ name }) => name === "vscode",
);
if (client === undefined) throw new Error("VS Code setup client missing");
await mkdir(dirname(client.configPath), { recursive: true });
return { home, host, client };
};
/** Release the held read handle and show host cleanup reuses it as a third attempt. */
const expectCloseReclaimsHandle = async (
host: { close?: () => Promise<string | undefined> },
closeAttempts: readonly object[],
release: () => void,
) => {
release();
expect(await host.close?.()).toBeUndefined();
expect(closeAttempts).toHaveLength(3);
expect(closeAttempts[2]).toBe(closeAttempts[0]);
};
/** Give up on the assertion path: release the handle and let cleanup close it. */
const cleanupHeldHandle = async (
host: { close?: () => Promise<string | undefined> },
release: () => void,
) => {
release();
await host.close?.();
};
it("retains invalid UTF-8 diagnostics and the uninstall read owner when close fails", async () => {
const home = await createTestTempDirectory("rea-uninstall-utf8-close-");
const path = join(home, "client.json");
@@ -77,13 +113,9 @@ it("retains invalid UTF-8 diagnostics and the uninstall read owner when close fa
});
expect(await readFile(path)).toEqual(original);
expect(closeAttempts).toHaveLength(2);
release();
expect(await host.close?.()).toBeUndefined();
expect(closeAttempts).toHaveLength(3);
expect(closeAttempts[2]).toBe(closeAttempts[0]);
await expectCloseReclaimsHandle(host, closeAttempts, release);
} finally {
release();
await host.close?.();
await cleanupHeldHandle(host, release);
}
});
@@ -126,13 +158,9 @@ describe("ZCode fallback cleanup ownership", () => {
await expect(readFile(client.configPath)).rejects.toMatchObject({
code: "ENOENT",
});
release();
expect(await host.close?.()).toBeUndefined();
expect(closeAttempts).toHaveLength(3);
expect(closeAttempts[2]).toBe(closeAttempts[0]);
await expectCloseReclaimsHandle(host, closeAttempts, release);
} finally {
release();
await host.close?.();
await cleanupHeldHandle(host, release);
}
},
);
@@ -145,20 +173,9 @@ describe("setup client configuration cleanup ownership", () => {
] as const)(
"stops configuration and retains the %s read owner until host close",
async (encoding, original) => {
const home = await createTestTempDirectory("rea-setup-config-close-");
const environment = {
HOME: home,
XDG_CONFIG_HOME: join(home, "xdg"),
};
const host = systemSetupHost(
{ platform: "linux", homeDirectory: home } as DoctorHost,
environment,
const { host, client } = await preparedVsCodeSetupHost(
"rea-setup-config-close-",
);
const client = (await host.supportedClients()).find(
({ name }) => name === "vscode",
);
if (client === undefined) throw new Error("VS Code setup client missing");
await mkdir(dirname(client.configPath), { recursive: true });
await writeFile(client.configPath, original);
const { closeAttempts, release } = holdReadHandle(
@@ -210,32 +227,17 @@ describe("setup client configuration cleanup ownership", () => {
expect(closeAttempts[0]).toBe(closeAttempts[1]);
expect(await readFile(client.configPath)).toEqual(original);
release();
expect(await host.close?.()).toBeUndefined();
expect(closeAttempts).toHaveLength(3);
expect(closeAttempts[2]).toBe(closeAttempts[0]);
await expectCloseReclaimsHandle(host, closeAttempts, release);
} finally {
release();
await host.close?.();
await cleanupHeldHandle(host, release);
}
},
);
it("waits for an admitted configuration read and rejects later admissions", async () => {
const home = await createTestTempDirectory("rea-setup-config-inflight-");
const environment = {
HOME: home,
XDG_CONFIG_HOME: join(home, "xdg"),
};
const host = systemSetupHost(
{ platform: "linux", homeDirectory: home } as DoctorHost,
environment,
const { host, client } = await preparedVsCodeSetupHost(
"rea-setup-config-inflight-",
);
const client = (await host.supportedClients()).find(
({ name }) => name === "vscode",
);
if (client === undefined) throw new Error("VS Code setup client missing");
await mkdir(dirname(client.configPath), { recursive: true });
await writeFile(client.configPath, '{"servers": {}}\n');
let beginRead!: () => void;
@@ -70,7 +70,7 @@ const failFirstCloses = (path: string) => {
describe("skill regular-file cleanup ownership", () => {
it("retains a failed setup skill read until setup host disposal", async () => {
const home = await createTestTempDirectory("rea-setup-skill-close-");
const [destination] = skillDestinations(home, []);
const [destination] = skillDestinations(home, [], {}, "linux");
if (destination === undefined) throw new Error("Shared skill root missing");
const manifest = join(destination.path, "SKILL.md");
await mkdir(dirname(manifest), { recursive: true });
@@ -1,4 +1,4 @@
import { chmod, realpath, writeFile } from "node:fs/promises";
import { chmod, readFile, realpath, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { systemDoctorHost } from "../../../src/application/Doctor.js";
@@ -78,7 +78,7 @@ describe("executable format refusal diagnostics", () => {
});
describe("target route diagnostics", () => {
it.each(["module.wasm", "module.bin", "misleading.js"])(
it.each(["module.wasm", "module.bin", "misleading.js", "misleading.hop"])(
"identifies a WASM v1 header in %s without suggesting native provider installation",
async (name) => {
const directory = await createTestTempDirectory("rea-wasm-target-");
@@ -346,3 +346,68 @@ describe("raw-binary load rejection diagnostics", () => {
});
});
});
describe("caller-selected and inferred target interpretation", () => {
it.each(["archive.hop", "archive.HOP"])(
"preserves inferred database routing for ZIP content named %s",
async (name) => {
const directory = await createTestTempDirectory("rea-zip-database-");
const bytes = Buffer.alloc(22);
bytes.writeUInt32LE(0x06054b50, 0);
const zipPath = join(directory, "archive.zip");
const databasePath = join(directory, name);
await writeFile(zipPath, bytes);
await writeFile(databasePath, bytes);
expect(await parseBinaryTarget(zipPath)).toMatchObject({
ok: true,
value: { kind: "archive", format: "zip" },
});
expect(await parseBinaryTarget(databasePath)).toMatchObject({
ok: true,
value: { kind: "database", format: "analysis-database" },
});
expect(await readFile(databasePath)).toEqual(bytes);
},
);
it("preserves explicit DOS COM interpretation for bytes with a WASM header", async () => {
const directory = await createTestTempDirectory("rea-wasm-explicit-com-");
const path = join(directory, "module.hop");
await writeFile(path, Buffer.from([0, 0x61, 0x73, 0x6d, 1, 0, 0, 0]));
expect(
await parseBinaryTarget(path, { formatHint: "dos-com" }),
).toMatchObject({
ok: true,
value: { kind: "executable", format: "dos-com", architecture: "x86" },
});
});
it.each(["module.wasm", "misleading.hop"])(
"preserves explicitly selected database routing for %s",
async (name) => {
const directory = await createTestTempDirectory(
"rea-wasm-explicit-database-",
);
const path = join(directory, name);
await writeFile(path, Buffer.from([0, 0x61, 0x73, 0x6d, 1, 0, 0, 0]));
expect(
await parseBinaryTarget(path, { targetKind: "database" }),
).toMatchObject({
ok: true,
value: { kind: "database", format: "analysis-database" },
});
},
);
it.each(["ordinary.hop", "ordinary.HOP"])(
"retains automatic database routing for non-WASM %s",
async (name) => {
const directory = await createTestTempDirectory("rea-ordinary-database-");
const path = join(directory, name);
await writeFile(path, "existing database content");
expect(await parseBinaryTarget(path)).toMatchObject({
ok: true,
value: { kind: "database", format: "analysis-database" },
});
},
);
});
@@ -0,0 +1,111 @@
import { describe, expect, it } from "vitest";
import { createIdaMcpConnection } from "../../src/ida/IdaMcpConnection.js";
describe("IDA startup diagnostics", () => {
it("preserves the failing launcher's stderr instead of reporting only a closed transport", async () => {
const connection = createIdaMcpConnection(
{
command: process.execPath,
args: [
"-e",
'process.stderr.write("FATAL: IDA runtime initialization failed\\n", () => process.exit(2));',
],
env: {},
mode: "headless",
timeoutMs: 1000,
},
{},
);
try {
await expect(connection.connect()).rejects.toMatchObject({
_tag: "ProviderAdapterError",
capturedOutput: {
stderr: expect.stringContaining("IDA runtime initialization failed"),
truncated: false,
},
});
} finally {
await connection.close();
}
});
it("retains only the bounded stderr tail and reports the original byte count", async () => {
const connection = createIdaMcpConnection(
{
command: process.execPath,
args: [
"-e",
'process.stderr.write("x".repeat(20000) + "FATAL-END", () => process.exit(2));',
],
env: {},
mode: "headless",
timeoutMs: 1000,
},
{},
);
try {
await expect(connection.connect()).rejects.toMatchObject({
_tag: "ProviderAdapterError",
capturedOutput: {
stderr: "x".repeat(16384 - "FATAL-END".length) + "FATAL-END",
stderr_bytes: 20000 + "FATAL-END".length,
truncated: true,
},
});
} finally {
await connection.close();
}
});
});
it("drops a leading partial UTF-8 character from the bounded stderr tail", async () => {
const connection = createIdaMcpConnection(
{
command: process.execPath,
args: [
"-e",
'process.stderr.write("€".repeat(6000), () => process.exit(2));',
],
env: {},
mode: "headless",
timeoutMs: 1000,
},
{},
);
try {
await expect(connection.connect()).rejects.toMatchObject({
capturedOutput: {
stderr: "€".repeat(Math.floor(16384 / 3)),
stderr_bytes: 18000,
truncated: true,
},
});
} finally {
await connection.close();
}
});
it("retains an in-budget undecodable leading byte without claiming tail truncation", async () => {
const connection = createIdaMcpConnection(
{
command: process.execPath,
args: [
"-e",
"process.stderr.write(Buffer.from([0x80, 0x61, 0x62]), () => process.exit(2));",
],
env: {},
mode: "headless",
timeoutMs: 1000,
},
{},
);
try {
const failure = await connection.connect().catch((cause: unknown) => cause);
expect(failure).toMatchObject({
capturedOutput: { stderr: "�ab", stderr_bytes: 3, truncated: false },
});
expect(failure).not.toHaveProperty("diagnostics.upstream_stderr");
} finally {
await connection.close();
}
});
@@ -1,7 +1,13 @@
import { createHash } from "node:crypto";
import { execFile } from "node:child_process";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
import { expect, it } from "vitest";
import { JavaScriptAnalysisWorker } from "../../../src/javascript/analysis/JavaScriptAnalysisWorker.js";
import { cleanupOwnedProcessGroup } from "../../../src/process/ProcessOwnership.js";
import type { OwnedProcessGroup } from "../../../src/process/ProcessOwnership.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { spawnOwnedProviderProcess } from "../../../src/process/ProviderProcess.js";
@@ -151,6 +157,121 @@ it("preserves a failed owned cleanup retry when the next caller cancels before a
}
});
it("ends worker stdin and unreferences process handles when cleanup reports inspection uncertainty", async () => {
let stdinEnded = false;
let unreferenced = false;
let permitCleanup = false;
let childStdin: typeof process.stdin | undefined;
const worker = new JavaScriptAnalysisWorker(
{ heapMb: 128, timeoutMs: 30_000 },
async (request) => {
const launched = await spawnOwnedProviderProcess(request);
const originalUnref = launched.process.unref;
launched.process.unref = () => {
unreferenced = true;
originalUnref?.call(launched.process);
};
childStdin = launched.process.stdin as unknown as typeof process.stdin;
launched.process.stdin?.once("finish", () => {
stdinEnded = true;
});
return {
...launched,
cleanup: async () =>
permitCleanup
? launched.cleanup === undefined
? cleanupOwnedProcessGroup(launched.ownership)
: launched.cleanup()
: {
cleaned: false,
reason: "process table could not be inspected: spawn EPERM",
},
};
},
);
try {
const file = source("app.mjs", "export const observed = 7;");
const result = await worker.analyze(file, 100);
if (!result.ok) throw result.error.error;
const closing = worker.close();
await expect(closing).rejects.toBeInstanceOf(ProviderCleanupError);
await expect(closing).rejects.toMatchObject({
cause: {
message: "process table could not be inspected: spawn EPERM",
},
});
expect(childStdin?.writableEnded || stdinEnded).toBe(true);
expect(unreferenced).toBe(true);
const errorListeners = childStdin?.listenerCount("error");
for (let retry = 0; retry < 3; retry++)
await expect(worker.close()).rejects.toBeInstanceOf(ProviderCleanupError);
expect(childStdin?.listenerCount("error")).toBeLessThanOrEqual(
errorListeners ?? 0,
);
permitCleanup = true;
await worker.close();
} finally {
permitCleanup = true;
await worker.close().catch(() => {});
}
});
it("lets a separate host exit while its live worker cleanup remains incomplete", async () => {
const root = await createTestTempDirectory("rea-worker-incomplete-host-");
const ownershipPath = join(root, "ownership.json");
const workerUrl = new URL(
"../../../dist/javascript/analysis/JavaScriptAnalysisWorker.js",
import.meta.url,
).href;
const launcherUrl = new URL(
"../../../dist/process/ProviderProcess.js",
import.meta.url,
).href;
const script = `
import { writeFile } from "node:fs/promises";
import { pathToFileURL } from "node:url";
import { JavaScriptAnalysisWorker } from ${JSON.stringify(workerUrl)};
import { spawnOwnedProviderProcess } from ${JSON.stringify(launcherUrl)};
const worker = new JavaScriptAnalysisWorker({ heapMb: 128, timeoutMs: 10000 }, async request => {
const entry = pathToFileURL(request.arguments.at(-1)).href;
const launched = await spawnOwnedProviderProcess({ ...request, arguments: [
...request.arguments.slice(0, -1), "--input-type=module", "-e",
"const interval = setInterval(() => {}, 1000); setTimeout(() => clearInterval(interval), 60000); await import(" + JSON.stringify(entry) + ");"
] });
// The launcher is deliberately orphaned when this host exits; cleanup still
// requires its run token and executable identity, rather than its old parent.
const { expectedParentPid, ...ownership } = launched.ownership;
await writeFile(${JSON.stringify(ownershipPath)}, JSON.stringify(ownership));
return { ...launched, cleanup: async () => ({ cleaned: false, reason: "fixture inspection unavailable" }) };
});
const analyzed = await worker.analyze(${JSON.stringify(source("app.mjs", "export const value = 1;"))}, 100);
if (!analyzed.ok) throw analyzed.error.error;
try { await worker.close(); throw new Error("cleanup unexpectedly completed"); }
catch (error) {
if (error.cleanupIncomplete !== true) throw error;
process.stdout.write(JSON.stringify({ code: "cleanup_incomplete", reason: error.cause.message }) + "\\n");
}
`;
try {
const result = await promisify(execFile)(
process.execPath,
["--input-type=module", "-e", script],
{ timeout: 10_000 },
);
expect(JSON.parse(result.stdout)).toMatchObject({
code: "cleanup_incomplete",
reason: "fixture inspection unavailable",
});
} finally {
const ownership = JSON.parse(
await readFile(ownershipPath, "utf8"),
) as OwnedProcessGroup;
expect(await cleanupOwnedProcessGroup(ownership)).toMatchObject({
cleaned: true,
});
}
});
// The handshake mirrors the real server: only Node.js reports an enforced V8 heap limit.
const resourceFailureWorker = (heapLimit: string) =>
new JavaScriptAnalysisWorker({ heapMb: 128, timeoutMs: 30_000 }, (request) =>
+4 -9
View File
@@ -1,3 +1,4 @@
import { advertisedMcpToolSchemas } from "../../fixtures/advertisedMcpTool.js";
import { API_SURFACE_EXAMPLE } from "../../../src/contracts/analysisViewApiSurfaceExample.js";
import { createHash } from "node:crypto";
import { readFile, writeFile } from "node:fs/promises";
@@ -212,16 +213,10 @@ it("advertises exact schemas and projects one layout section from retained Evide
inspect: () => Promise.resolve(ok(layout)),
});
const { client, session } = await connect(service);
const advertised = (await client.listTools()).tools.find(
(tool) => tool.name === "inspect_analysis_view",
const { ajv, inputSchema, outputSchema } = await advertisedMcpToolSchemas(
client,
"inspect_analysis_view",
);
if (advertised?.outputSchema === undefined)
throw new Error("inspect_analysis_view must publish both schemas");
const ajv = new Ajv2020({ strict: false, validateFormats: false });
const inputSchema: Record<string, unknown> = advertised.inputSchema;
const outputSchema: Record<string, unknown> = advertised.outputSchema;
expect(ajv.validateSchema(inputSchema)).toBe(true);
expect(ajv.validateSchema(outputSchema)).toBe(true);
expect(
ajv.validate(inputSchema, {
source: { kind: "inline", evidence: analysisViewLayoutEvidence() },
@@ -0,0 +1,60 @@
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { expect, it } from "vitest";
import { ArtifactProvider } from "../../../src/artifacts/ArtifactProvider.js";
import { createServer } from "../../../src/server/createServer.js";
import { nestedAsarHeader } from "../../fixtures/asarHeader.js";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
it("reports expanded ASAR admission failure through MCP and permits a valid retry", async () => {
const root = await createTestTempDirectory("rea-asar-listing-mcp-");
const path = join(root, "app.asar");
const bytes = nestedAsarHeader(64, "d".repeat(256), 4000);
await writeFile(path, bytes);
const session = createTestBinarySession(new ArtifactProvider(process.env));
const server = createServer({ kind: "session", session });
const client = new Client({ name: "asar-listing-test", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
expect(
(await client.callTool({ name: "open_binary", arguments: { path } }))
.isError,
).not.toBe(true);
const result = await client.callTool({
name: "inspect_artifact",
arguments: {},
});
expect(result.isError).toBe(true);
expect(parseMcpToolError(result)).toMatchObject({
error: {
code: "truncated",
category: "truncated",
},
});
expect(result.structuredContent).toBeUndefined();
expect(await readFile(path)).toEqual(bytes);
const valid = join(root, "valid.asar");
await writeFile(valid, nestedAsarHeader(1, "lib", 1));
expect(
(
await client.callTool({
name: "open_binary",
arguments: { path: valid },
})
).isError,
).not.toBe(true);
expect(
(await client.callTool({ name: "inspect_artifact", arguments: {} }))
.isError,
).not.toBe(true);
} finally {
await Promise.allSettled([client.close(), server.close(), session.close()]);
}
});
+15 -23
View File
@@ -1,9 +1,11 @@
import {
advertisedMcpToolSchemas,
assertRecordedMcpEvidence,
} from "../../fixtures/advertisedMcpTool.js";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { Ajv2020 } from "ajv/dist/2020.js";
import { expect, it, onTestFinished } from "vitest";
import { BinaryLayoutService } from "../../../src/application/binaryDiagnostics/BinaryLayoutService.js";
import { toolContract } from "../../../src/contracts/toolContracts.js";
import { parseEvidence } from "../../../src/domain/evidence.js";
import { ok } from "../../../src/domain/result.js";
import { createServer } from "../../../src/server/createServer.js";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
@@ -43,16 +45,11 @@ it("advertises exact valid schemas and records inline layout without a binary ta
InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
await client.connect(clientTransport);
const advertised = (await client.listTools()).tools.find(
(tool) => tool.name === "inspect_binary_layout",
const schemas = await advertisedMcpToolSchemas(
client,
"inspect_binary_layout",
);
if (advertised?.outputSchema === undefined)
throw new Error("layout must publish both schemas");
const ajv = new Ajv2020({ strict: false, validateFormats: false });
const inputSchema: Record<string, unknown> = advertised.inputSchema;
const outputSchema: Record<string, unknown> = advertised.outputSchema;
expect(ajv.validateSchema(inputSchema)).toBe(true);
expect(ajv.validateSchema(outputSchema)).toBe(true);
const { ajv, inputSchema } = schemas;
expect(ajv.validate(inputSchema, { path: value.artifact.path })).toBe(true);
expect(
ajv.validate(inputSchema, {
@@ -64,19 +61,14 @@ it("advertises exact valid schemas and records inline layout without a binary ta
name: "inspect_binary_layout",
arguments: { path: value.artifact.path },
});
expect(response.isError).not.toBe(true);
expect(
ajv.validate(outputSchema, response.structuredContent),
JSON.stringify(ajv.errors),
).toBe(true);
const parsed = toolContract("inspect_binary_layout").outputSchema.parse(
response.structuredContent,
const evidence = assertRecordedMcpEvidence(
response,
"inspect_binary_layout",
schemas,
session,
value,
"observed",
);
const evidence = parseEvidence(parsed);
expect(evidence.confidence).toBe("observed");
expect(evidence.subject?.digest.sha256).toBe(value.artifact.sha256);
expect(parsed.normalized_result).toEqual(value);
expect(session.evidenceById(evidence.evidence_id)).toEqual(evidence);
const viewed = await client.callTool({
name: "inspect_analysis_view",
arguments: {
+41
View File
@@ -424,6 +424,47 @@ it("delivers the source-map response URL through authenticated MCP Evidence", as
).toBe(true);
});
it("records a browser-socket endpoint redacted and reconciles its Evidence", async () => {
const browser = await startFakeCdpBrowser();
browsers.push(browser);
const connected = await connectBrowser();
const tool = (await connected.client.listTools()).tools.find(
({ name }) => name === "inspect_web_page",
);
expect(tool?.inputSchema.properties?.cdp_endpoint).toMatchObject({
description: expect.stringContaining("/devtools/browser/ID"),
});
const inspected = await connected.client.callTool({
name: "inspect_web_page",
arguments: {
cdp_endpoint: browser.browserWebSocketUrl,
target_id: "allowed-page",
observation_ms: 0,
include_script_sources: true,
},
});
expect(inspected.isError).not.toBe(true);
expect(inspected.structuredContent).toMatchObject({
parameters: {
cdp_endpoint: browser.browserWebSocketUrl.replace(/fake$/u, "[REDACTED]"),
},
});
expect(JSON.stringify(inspected.structuredContent)).not.toContain(
"/devtools/browser/fake",
);
const reconciled = await connected.client.callTool({
name: "reconcile_javascript_runtime",
arguments: {
static_layers: JAVASCRIPT_RUNTIME_RECONCILIATION_EXAMPLE.static_layers,
runtime_observations: [evidenceFor(inspected.structuredContent)],
},
});
expect(reconciled.isError).not.toBe(true);
expect(reconciled.structuredContent).toMatchObject({
normalized_result: { summary: { runtime_scripts: 1 } },
});
});
it("does not attach to a target outside the request's allowed origin scope", async () => {
const browser = await startFakeCdpBrowser();
browsers.push(browser);
@@ -49,6 +49,9 @@ const observeAgain = (
result,
confidence: source.confidence,
authority: source.authority,
...(source.relationship_coverage === null
? {}
: { relationshipCoverage: source.relationship_coverage }),
},
);
@@ -0,0 +1,86 @@
import { describe, expect, it } from "vitest";
import type {
CallToolResult,
ServerContext,
} from "@modelcontextprotocol/server";
import { STDIO_DEFAULT_MAX_BUFFER_SIZE } from "@modelcontextprotocol/server";
import { toolContract } from "../../../src/contracts/toolContracts.js";
import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js";
import { createEvidence } from "../../../src/domain/evidence.js";
import type { Evidence } from "../../../src/domain/evidence.js";
import { err, ok } from "../../../src/domain/result.js";
import { silentLogger } from "../../../src/logger.js";
import { createEvidenceToolHandler } from "../../../src/server/contractToolHandler.js";
import { EvidenceMcpServer } from "../../../src/server/EvidenceMcpServer.js";
import { ToolResultDelivery } from "../../../src/server/toolResult.js";
const UNTRUSTED_TARGET_CONTENT = {
provenance: { untrusted_content: true, source: "analysis_target" },
};
const context = {
mcpReq: { signal: new AbortController().signal },
} as ServerContext;
/**
* Deliver one retained partial-observation failure through the shared Evidence
* handler. The handler is the producer of the result: `EvidenceMcpServer` only
* adds the marker later for results that bypass `ToolResultDelivery`, so a
* missing marker here also undercounts the wire budget by the marker's bytes.
*/
const deliverPartialFailure = async (): Promise<{
readonly result: CallToolResult;
readonly evidence: Evidence;
}> => {
const evidence = createEvidence(
undefined,
{ id: "fixture", name: "Completed capture fixture", version: "1" },
{
operation: "inspect_packet_capture",
parameters: { path: "recording.pcap" },
result: { packets: [{ "frame.number": ["1"] }] },
confidence: "observed",
},
);
const failure = new AnalysisCancelledError("inspect_packet_capture", {
partialObservation: evidence,
});
const writer = { recordEvidence: () => ok("added" as const) };
const server = new EvidenceMcpServer(
{ name: "partial-error-framing-test", version: "1" },
{ capabilities: {} },
writer.recordEvidence,
new ToolResultDelivery(STDIO_DEFAULT_MAX_BUFFER_SIZE),
);
const contract = toolContract("inspect_packet_capture");
const handler = createEvidenceToolHandler(
server,
async () => err(failure),
silentLogger,
writer.recordEvidence,
);
return {
result: await handler(contract)({ path: "recording.pcap" }, context),
evidence,
};
};
describe("shared Evidence handler target-content framing", () => {
it("marks a retained partial-observation error as untrusted target content", async () => {
const { result, evidence } = await deliverPartialFailure();
expect(result.isError).toBe(true);
expect(result.structuredContent).toMatchObject({
error: {
details: {
partial_observation: {
kind: "retained-evidence",
evidence_id: evidence.evidence_id,
},
},
},
});
expect(result._meta).toEqual(UNTRUSTED_TARGET_CONTENT);
});
});
@@ -104,7 +104,16 @@ const fixturePort = (): AnalysisOperationPort => ({
"0x11": "_OBJC_CLASS_$_Fixture",
"0x12": "_OBJC_PROTOCOL_$_FixtureDelegate",
"0x13": "entry",
}),
}).map((entry) => ({
...entry,
symbol: {
primary: true,
dynamic: false,
external: false,
type: "label",
source: "analysis",
},
})),
),
);
case "decode_interface_builder":
+36 -24
View File
@@ -1,4 +1,9 @@
import {
advertisedMcpToolSchemas,
assertRecordedMcpEvidence,
} from "../../fixtures/advertisedMcpTool.js";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { createHash } from "node:crypto";
import { Ajv2020 } from "ajv/dist/2020.js";
import { expect, it, onTestFinished, vi } from "vitest";
import { EvmInterfaceService } from "../../../src/application/evm/EvmInterfaceService.js";
@@ -10,6 +15,7 @@ import {
} from "../../../src/domain/analysisErrorCore.js";
import { createEvidence, parseEvidence } from "../../../src/domain/evidence.js";
import { evmInterfaceSummaryViewResultSchema } from "../../../src/domain/analysisView/analysisView.js";
import { inspectSolidityMetadata } from "../../../src/evm/SolidityMetadata.js";
import { err, ok } from "../../../src/domain/result.js";
import { silentLogger } from "../../../src/logger.js";
import { createServer } from "../../../src/server/createServer.js";
@@ -26,8 +32,21 @@ import {
evmInterfaceExecution,
} from "../../fixtures/evm/interface.js";
it("advertises exact valid schemas and records inline EVM interface evidence without a binary target", async () => {
const decodedMetadataFixture = () => {
const bytes = Buffer.from("00a1011bffffffffffffffff000b", "hex");
const value = evmInterfaceFixture();
value.bytecode = {
...value.bytecode,
hex: bytes.toString("hex"),
bytes: bytes.length,
sha256: createHash("sha256").update(bytes).digest("hex"),
metadata: inspectSolidityMetadata(bytes),
};
return value;
};
it("advertises exact valid schemas and records inline EVM interface evidence without a binary target", async () => {
const value = decodedMetadataFixture();
const inspect = vi.fn(() =>
Promise.resolve(ok(evmInterfaceExecution(value))),
);
@@ -51,16 +70,11 @@ it("advertises exact valid schemas and records inline EVM interface evidence wit
InMemoryTransport.createLinkedPair();
await server.connect(serverTransport);
await client.connect(clientTransport);
const advertised = (await client.listTools()).tools.find(
(tool) => tool.name === "inspect_evm_interface",
const schemas = await advertisedMcpToolSchemas(
client,
"inspect_evm_interface",
);
if (advertised?.outputSchema === undefined)
throw new Error("EVM interface inspection must publish both schemas");
const ajv = new Ajv2020({ strict: false, validateFormats: false });
const inputSchema: Record<string, unknown> = advertised.inputSchema;
const outputSchema: Record<string, unknown> = advertised.outputSchema;
expect(ajv.validateSchema(inputSchema)).toBe(true);
expect(ajv.validateSchema(outputSchema)).toBe(true);
const { ajv, inputSchema } = schemas;
expect(
ajv.validate(inputSchema, { path: value.artifact.path, encoding: "hex" }),
).toBe(true);
@@ -84,19 +98,14 @@ it("advertises exact valid schemas and records inline EVM interface evidence wit
name: "inspect_evm_interface",
arguments: { path: value.artifact.path, encoding: "hex" },
});
expect(response.isError).not.toBe(true);
expect(
ajv.validate(outputSchema, response.structuredContent),
JSON.stringify(ajv.errors),
).toBe(true);
const parsed = toolContract("inspect_evm_interface").outputSchema.parse(
response.structuredContent,
assertRecordedMcpEvidence(
response,
"inspect_evm_interface",
schemas,
session,
value,
"inferred",
);
const evidence = parseEvidence(parsed);
expect(evidence.confidence).toBe("inferred");
expect(evidence.subject?.digest.sha256).toBe(value.artifact.sha256);
expect(parsed.normalized_result).toEqual(value);
expect(session.evidenceById(evidence.evidence_id)).toEqual(evidence);
expect(
(
await client.callTool({
@@ -280,7 +289,8 @@ it("settles the admitted EVM operation after SDK cancellation and accepts a subs
});
it("returns the exact inferred interface summary while preserving complete defaults and retained source", async () => {
const value = evmInterfaceFixture();
const value = decodedMetadataFixture();
expect(value.bytecode.metadata?.status).toBe("decoded");
value.functions = [
{
selector: "0x11223344",
@@ -419,6 +429,7 @@ it.each(["valid", "wrong-subject", "no-subject"])(
"preserves historical portable EVM Evidence and rejects incompatible identity: %s",
async (scenario) => {
const value = evmInterfaceFixture();
const { metadata: _metadata, ...historicalBytecode } = value.bytecode;
const source = createEvidence(
scenario === "no-subject"
? undefined
@@ -434,7 +445,7 @@ it.each(["valid", "wrong-subject", "no-subject"])(
{
operation: "inspect_evm_interface",
parameters: { path: value.artifact.path, encoding: "hex" },
result: value,
result: { ...value, bytecode: historicalBytecode },
confidence: "inferred",
rawResult: { original: true },
},
@@ -463,6 +474,7 @@ it.each(["valid", "wrong-subject", "no-subject"])(
expect(view.parent_evidence_id).toBe(source.evidence_id);
expect(view.summary.functions).toEqual([]);
expect(view.summary.discovery_completeness).toBe("unknown");
expect(view.summary.bytecode.metadata).toBeNull();
expect(view.coverage).toMatchObject({
total: 0,
examined: 0,
@@ -1,6 +1,14 @@
import { ok as resultOk } from "../../../src/domain/result.js";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { describe, expect, it } from "vitest";
import { describe, expect, it, onTestFinished } from "vitest";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { createApplicationMcpHarness } from "../../fixtures/applicationMcpHarness.js";
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
import { createEvidenceBundle } from "../../../src/domain/evidenceBundle.js";
import { compareFunctions } from "../../../src/domain/functionComparison.js";
import { jsonValueSchema } from "../../../src/domain/jsonValue.js";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
import { FUNCTION_COMPARISON_EXAMPLE } from "../../../src/contracts/functionComparisonExample.js";
@@ -271,3 +279,130 @@ describe("function comparison MCP integration", () => {
}
});
});
it.each([
{ status: "changed", expected: "fail" },
{ status: "unknown", expected: "unknown" },
{ status: "unchanged", expected: "pass" },
] as const)(
"checks the aggregate status of imported $status function comparisons",
async ({ status, expected }) => {
const left = FUNCTION_COMPARISON_EXAMPLE.left;
const right =
status === "changed"
? FUNCTION_COMPARISON_EXAMPLE.right
: createEvidence(
{
path: "/fixture/function-right",
sha256: "3".repeat(64),
format: "mach-o",
},
status === "unknown"
? { ...left.provider, id: "other-provider" }
: left.provider,
{
operation: left.operation,
parameters: left.parameters,
result: left.normalized_result,
confidence: left.confidence,
authority: left.authority,
relationshipCoverage: { status: "complete" },
},
);
const result = compareFunctions(left, right);
expect(result.status).toBe(status);
const provider = {
id: "rea-function-comparison",
name: "REA function comparison",
version: "1",
};
const observation = {
operation: "compare_functions",
predicateType: "rea.function-comparison",
parameters: {
left_evidence_id: left.evidence_id,
right_evidence_id: right.evidence_id,
},
confidence: "derived" as const,
authority: "analyst-inference" as const,
evidenceLinks: [left.evidence_id, right.evidence_id],
};
const consistent = createEvidence(undefined, provider, {
...observation,
result: jsonValueSchema.parse(result),
});
const inconsistent =
status === "unchanged"
? null
: createEvidence(undefined, provider, {
...observation,
result: jsonValueSchema.parse({ ...result, status: "unchanged" }),
});
const directory = await createTestTempDirectory("rea-function-status-");
const path = join(directory, "bundle.json");
await writeFile(
path,
JSON.stringify(
createEvidenceBundle([
left,
right,
consistent,
...(inconsistent === null ? [] : [inconsistent]),
]),
),
);
const session = createTestBinarySession(() => {
throw new Error("Imported comparison must not launch a provider");
});
const harness = await createApplicationMcpHarness({ session });
onTestFinished(harness.close);
const imported = await harness.client.callTool({
name: "import_evidence_bundle",
arguments: { path },
});
expect(imported.isError).not.toBe(true);
const verification = (evidence_id: string) =>
harness.client.callTool({
name: "verify_reconstruction",
arguments: {
specification: {
name: "Function aggregate",
claims: [
{
kind: "structural-function",
claim_id: "overall",
title: "Function agrees",
comparison_evidence_id: evidence_id,
dimension: "overall",
},
],
},
},
});
if (inconsistent !== null) {
const rejected = await verification(inconsistent.evidence_id);
expect(parseMcpToolError(rejected).error).toMatchObject({
code: "invalid_request",
details: {
issues: [
{
reason: "invalid_value",
message: expect.stringContaining(
"Function comparison status contradicts its dimensions or function match",
),
},
],
},
});
expect(harness.session.evidenceById(inconsistent.evidence_id)).toEqual(
inconsistent,
);
}
const verified = await verification(consistent.evidence_id);
expect(verified.isError).not.toBe(true);
expect(
parseEvidence(verified.structuredContent).normalized_result,
).toMatchObject({ status: expected });
await harness.client.ping();
},
);
+345 -2
View File
@@ -1,9 +1,352 @@
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
import { expect, it } from "vitest";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { nativeAotPeFixture } from "../../fixtures/nativeaotPe.js";
import { jsonValueSchema } from "../../../src/domain/jsonValue.js";
import { ok } from "../../../src/domain/result.js";
import { err, ok } from "../../../src/domain/result.js";
import { ghidraFunctionIdentity } from "../../../src/domain/ghidraValues.fixture.js";
import { connectGhidraMcp, sessionEvidence } from "./ghidraMcpHarness.js";
import {
connectGhidraMcp,
ghidraInstallationHost,
sessionEvidence,
} from "./ghidraMcpHarness.js";
import { spawn } from "node:child_process";
import { mkdir } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { GhidraClient } from "../../../src/ghidra/GhidraClient.js";
import { GhidraProvider } from "../../../src/ghidra/GhidraProvider.js";
import { parseConfig } from "../../../src/config/parseConfig.js";
import { silentLogger } from "../../../src/logger.js";
import { composeBinarySession } from "../../../src/application/binary/BinarySessionComposition.js";
import { AnalysisProviderRegistry } from "../../../src/application/binary/AnalysisProviderRegistry.js";
import { createServer } from "../../../src/server/createServer.js";
import { waitForExit } from "../../support/process/processFixture.js";
import { GhidraSessionError } from "../../../src/ghidra/GhidraSessionError.js";
it("retains byte-search coverage and rejects malformed pages before creating Evidence", async () => {
const page = {
matches: [
{
address: "0x401000",
segment: ".text",
file_offset: null,
procedure: null,
},
],
searched: {
blocks: [
{ segment: ".text", start: "0x401000", end: "0x40100f", bytes: 16 },
],
bytes: 16,
skipped_uninitialized: [
{ segment: ".bss", start: "0x402000", end: "0x40200f", bytes: 16 },
],
skipped_not_loaded: [
{ segment: ".symtab", start: "0x0", end: "0x17f", bytes: 384 },
],
},
complete: false,
next_cursor: "continuation",
};
let output = jsonValueSchema.parse(page);
const harness = await connectGhidraMcp("byte-search-projection", () =>
Promise.resolve(ok(output)),
);
try {
const first = await harness.mcp.callTool({
name: "search_bytes",
arguments: { pattern: "48 ??", limit: 1 },
});
expect(first.isError).not.toBe(true);
expect(
sessionEvidence(harness.session, first.structuredContent)
.normalized_result,
).toEqual(page);
output = jsonValueSchema.parse({
...page,
matches: [],
complete: true,
next_cursor: null,
});
const last = await harness.mcp.callTool({
name: "search_bytes",
arguments: { pattern: "48 ??", cursor: "continuation" },
});
expect(last.isError).not.toBe(true);
expect(
sessionEvidence(harness.session, last.structuredContent)
.normalized_result,
).toEqual(output);
output = jsonValueSchema.parse({ ...page, complete: true });
const malformed = await harness.mcp.callTool({
name: "search_bytes",
arguments: { pattern: "49 ??" },
});
expect(parseMcpToolError(malformed)).toMatchObject({
error: { code: "unreadable_output" },
});
for (const block of [
{ segment: ".text", start: "0x1000", end: "0x1009", bytes: 1 },
{ segment: ".text", start: "0x1009", end: "0x1000", bytes: 1 },
{ segment: ".text", start: "overlay:0x1000", end: "0x1000", bytes: 1 },
{ segment: ".text", start: "invalid", end: "0x1000", bytes: 1 },
]) {
output = jsonValueSchema.parse({
...page,
searched: { ...page.searched, blocks: [block], bytes: block.bytes },
});
const invalidCoverage = await harness.mcp.callTool({
name: "search_bytes",
arguments: { pattern: "48 ??" },
});
expect(parseMcpToolError(invalidCoverage)).toMatchObject({
error: { code: "unreadable_output" },
});
expect(invalidCoverage.structuredContent).toBeUndefined();
}
const calls = harness.calls.length;
for (const args of [{ pattern: "??" }, { pattern: "48", mask: "ffff" }]) {
const invalid = await harness.mcp.callTool({
name: "search_bytes",
arguments: args,
});
expect(invalid.isError).toBe(true);
expect(JSON.stringify(invalid.content)).toContain(
"Input validation error",
);
}
expect(harness.calls).toHaveLength(calls);
} finally {
await harness.close();
}
});
it("projects cancelled byte searches as typed failures without successful partial Evidence", async () => {
const harness = await connectGhidraMcp("byte-search-cancelled", () =>
Promise.resolve(
err(new GhidraSessionError("cancelled", "Search cancelled")),
),
);
try {
const reply = await harness.mcp.callTool({
name: "search_bytes",
arguments: { pattern: "48 8b" },
});
expect(reply.isError).toBe(true);
expect(parseMcpToolError(reply)).toMatchObject({
error: { code: "cancelled" },
});
expect(reply.structuredContent).toBeUndefined();
} finally {
await harness.close();
}
});
it("searches bytes through MCP and the socket bridge with pagination and skipped coverage", async () => {
const config = parseConfig({ GHIDRA_INSTALL_DIR: "/fixture/ghidra" });
if (!config.ok) throw config.error;
const provider = new GhidraProvider(
config.value,
silentLogger,
{},
ghidraInstallationHost(),
(options) =>
new GhidraClient({
...options,
platform: process.platform,
startupTimeoutMs: 5000,
launcher: {
launch: async (launch) => {
const projectRoot = join(launch.runtimeRoot, "project");
await mkdir(projectRoot);
const child = spawn(
process.execPath,
[
fileURLToPath(
new URL("../../fixtures/fakeGhidra.mjs", import.meta.url),
),
],
{
stdio: ["ignore", "pipe", "pipe"],
env: {
...process.env,
REA_GHIDRA_FIXTURE_CONFIG: JSON.stringify({
endpointPath: launch.endpointPath,
token: launch.token,
runId: launch.runId,
providerVersion: launch.providerVersion,
profileDigest: launch.profileDigest,
targetSha256: launch.targetSha256,
transport: launch.transport,
}),
},
},
);
return ok({
process: child,
ownsProcessLifetime: true as const,
projectRoot,
ghidraLogPath: join(launch.runtimeRoot, "ghidra.log"),
scriptLogPath: join(launch.runtimeRoot, "script.log"),
cleanup: async () => {
const signaled =
child.exitCode === null && child.signalCode === null;
if (signaled) child.kill("SIGTERM");
return (await waitForExit(child, 2000))
? { cleaned: true as const, signaled }
: { cleaned: false as const, reason: "Fixture did not exit" };
},
});
},
},
}),
);
const session = composeBinarySession(
new AnalysisProviderRegistry([provider]),
);
const server = createServer(
{ kind: "session", session },
{ logger: silentLogger },
);
const client = new Client({ name: "byte-search", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const opened = await client.callTool({
name: "open_binary",
arguments: { path: process.execPath },
});
expect(opened.isError).not.toBe(true);
const first = await client.callTool({
name: "search_bytes",
arguments: { pattern: "48 8b ?? ?? 89", limit: 1 },
});
expect(first.isError, JSON.stringify(first)).not.toBe(true);
const firstEvidence = sessionEvidence(session, first.structuredContent);
expect(firstEvidence.normalized_result).toMatchObject({
matches: [
{
address: "0x1000",
segment: ".text",
file_offset: 0,
procedure: { name: "fixture_main" },
},
],
searched: {
bytes: 10,
skipped_uninitialized: [{ segment: ".bss", bytes: 16 }],
},
complete: false,
next_cursor: "fixture-second-page",
});
const second = await client.callTool({
name: "search_bytes",
arguments: {
pattern: "48 8b ?? ?? 89",
limit: 1,
cursor: "fixture-second-page",
},
});
expect(second.isError).not.toBe(true);
expect(
sessionEvidence(session, second.structuredContent).normalized_result,
).toMatchObject({
matches: [{ address: "0x1005", file_offset: 5 }],
complete: true,
next_cursor: null,
});
const invalid = await client.callTool({
name: "search_bytes",
arguments: { pattern: "??" },
});
expect(invalid.isError).toBe(true);
expect(JSON.stringify(invalid.content)).toContain(
"Pattern must constrain at least one bit",
);
} finally {
await client.close();
await server.close();
const closed = await session.close();
if (!closed.ok) throw closed.error;
}
});
it("retains missing PDB coverage in empty PE name-search Evidence and snapshot replay", async () => {
const root = await createTestTempDirectory("rea-pdb-evidence-");
const targetPath = join(root, "fixture.exe");
await writeFile(targetPath, nativeAotPeFixture());
// Synthetic post-analysis facts at the real provider boundary; no engine is simulated as verified.
const pdb = {
file: "C:\\build\\fixture.pdb",
guid: "01234567-89AB-CDEF-0123-456789ABCDEF",
age: "a",
signature: null,
version: "RSDS",
loaded: false,
};
const harness = await connectGhidraMcp(
"ghidra-pdb-coverage",
(operation) =>
Promise.resolve(
ok(
operation === "procedure_pseudo_code"
? {
value: "int FUN_140001000(void) { return 0; }",
limitations: [],
}
: [],
),
),
{
targetPath,
pdb,
},
);
try {
const reply = await harness.mcp.callTool({
name: "search_procedures",
arguments: { pattern: "add_numbers" },
});
expect(reply.isError).not.toBe(true);
const evidence = sessionEvidence(harness.session, reply.structuredContent);
expect(evidence.normalized_result).toEqual([]);
expect(evidence.analysis_profile?.parameters.pdb_observations).toBe(
"program-info-v1",
);
const limitation = evidence.limitations.find((value) =>
value.includes(pdb.file),
);
expect(limitation).toContain(pdb.guid);
expect(limitation).toContain("age (hex) a");
expect(limitation).toContain("not loaded");
const decompiled = await harness.mcp.callTool({
name: "procedure_pseudo_code",
arguments: { procedure: "0x140001000", document: "fixture" },
});
expect(decompiled.isError).not.toBe(true);
const functionEvidence = sessionEvidence(
harness.session,
decompiled.structuredContent,
);
expect(functionEvidence.limitations).toContain(limitation);
const replay = await harness.session.execute(
"procedure_pseudo_code",
functionEvidence.parameters,
);
if (!replay.ok) throw replay.error;
expect(replay.value.limitations).toContain(limitation);
expect(replay.value.limitations).toContainEqual(
expect.stringContaining("Loaded from a local REA analysis snapshot"),
);
} finally {
await harness.close();
}
});
it("retains defined-string coverage in empty and matching search Evidence", async () => {
let output = jsonValueSchema.parse([]);
+12 -4
View File
@@ -12,6 +12,7 @@ import type { GhidraOperation } from "../../../src/ghidra/GhidraClient.js";
import { GhidraProvider } from "../../../src/ghidra/GhidraProvider.js";
import type { GhidraProviderClientFactory } from "../../../src/ghidra/GhidraProviderClient.js";
import { GHIDRA_SESSION_CAPABILITIES } from "../../../src/ghidra/GhidraSessionValues.js";
import type { GhidraSessionInfo } from "../../../src/ghidra/GhidraSessionValues.js";
import { silentLogger } from "../../../src/logger.js";
import { createServer } from "../../../src/server/createServer.js";
import {
@@ -26,12 +27,19 @@ const INSTALL = "/opt/ghidra_12.1.4_PUBLIC";
export const connectGhidraMcp = async (
name: string,
execute?: ReturnType<GhidraProviderClientFactory>["callTool"],
observations: {
readonly targetPath?: string;
readonly pdb?: GhidraSessionInfo["pdb"];
} = {},
) => {
const calls: GhidraOperation[] = [];
const factory: GhidraProviderClientFactory = (options) => ({
start: () =>
Promise.resolve(
ok(sessionInfo(options.profileDigest, options.targetSha256)),
ok({
...sessionInfo(options.profileDigest, options.targetSha256),
...(observations.pdb === undefined ? {} : { pdb: observations.pdb }),
}),
),
callTool: (operation, input, options) => {
calls.push(operation);
@@ -46,7 +54,7 @@ export const connectGhidraMcp = async (
config.value,
silentLogger,
{},
installationHost(),
ghidraInstallationHost(),
factory,
);
const session = composeBinarySession(
@@ -68,7 +76,7 @@ export const connectGhidraMcp = async (
await mcp.connect(clientTransport);
const opened = await mcp.callTool({
name: "open_binary",
arguments: { path: process.execPath },
arguments: { path: observations.targetPath ?? process.execPath },
});
if (opened.isError === true)
throw new Error(
@@ -92,7 +100,7 @@ export const sessionEvidence = (session: BinarySession, value: unknown) => {
return evidence;
};
const installationHost = (): GhidraInstallationHost => ({
export const ghidraInstallationHost = (): GhidraInstallationHost => ({
platform: "linux",
architecture: "x64",
readText: () => "application.version=12.1.4\napplication.java.min=21\n",
+23 -1
View File
@@ -43,7 +43,23 @@ describe("official operation arguments", () => {
const execute: AnalysisOperationPort["execute"] = (name, parameters) => {
calls.push({ name, parameters });
return Promise.resolve(
observed(name === "search_strings" ? [] : "int main() {}"),
observed(
name === "search_bytes"
? {
matches: [],
searched: {
blocks: [],
bytes: 0,
skipped_uninitialized: [],
skipped_not_loaded: [],
},
complete: true,
next_cursor: null,
}
: name === "search_strings"
? []
: "int main() {}",
),
);
};
const createSession = () =>
@@ -59,6 +75,7 @@ describe("official operation arguments", () => {
const client = await connect({ execute });
for (const [name, mcpParameters, cliParameters] of [
["procedure_pseudo_code", { procedure: "main" }, { procedure: "main" }],
["search_bytes", { pattern: "48 ??" }, { pattern: "48 ??", limit: 100 }],
[
"search_strings",
{ pattern: "entry" },
@@ -93,6 +110,11 @@ describe("official operation arguments", () => {
["list_procedures", { document: null }],
["procedure_pseudo_code", { procedure: "main", document: 3 }],
["search_strings", { pattern: "entry", case_sensitive: null }],
["search_bytes", { pattern: "??" }],
["search_bytes", { pattern: "48 8B", mask: "ff" }],
["search_bytes", { pattern: "4?" }],
["search_bytes", { pattern: "48", limit: 1001 }],
["search_bytes", { pattern: "48", cursor: null }],
] as const) {
const result = await client.callTool({ name, arguments: parameters });
expect(result.isError).toBe(true);
+67
View File
@@ -10,6 +10,12 @@ import { err } from "../../../src/domain/result.js";
import { observed as ok } from "../../fixtures/analysisExecution.js";
import type { JsonValue } from "../../../src/domain/jsonValue.js";
import { createServer } from "../../../src/server/createServer.js";
import { HopperProvider } from "../../../src/hopper/HopperProvider.js";
import { parseConfig } from "../../../src/config/parseConfig.js";
import { silentLogger } from "../../../src/logger.js";
import { composeBinarySession } from "../../../src/application/binary/BinarySessionComposition.js";
import { AnalysisProviderRegistry } from "../../../src/application/binary/AnalysisProviderRegistry.js";
import { createBinarySessionTargets } from "../../fixtures/binarySession.js";
interface Invocation {
readonly name: string;
@@ -18,6 +24,67 @@ interface Invocation {
const resources: Array<{ close(): Promise<void> }> = [];
it("returns typed Hopper byte-search unavailability without launching the provider", async () => {
const [target] = await createBinarySessionTargets();
const config = parseConfig({ HOPPER_LAUNCHER_PATH: process.execPath });
if (!config.ok) throw config.error;
const provider = new HopperProvider(config.value, silentLogger, {}, "linux");
const session = composeBinarySession(
new AnalysisProviderRegistry([provider]),
);
const server = createServer(
{ kind: "session", session },
{ logger: silentLogger },
);
const client = new Client({
name: "hopper-byte-search-unavailable",
version: "1",
});
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const opened = await client.callTool({
name: "open_binary",
arguments: { path: target },
});
expect(opened.isError).not.toBe(true);
const result = await client.callTool({
name: "search_bytes",
arguments: { pattern: "48 8b" },
});
expect(result.isError).toBe(true);
expect(parseMcpToolError(result)).toMatchObject({
error: {
code: "capability_unavailable",
details: { provider_id: "hopper", operation: "search_bytes" },
},
});
} finally {
await client.close();
await server.close();
const closed = await session.close();
if (!closed.ok) throw closed.error;
}
});
it("advertises strict masked-byte arguments and bounded pages", () => {
const contract = OFFICIAL_TOOL_CONTRACTS.find(
({ name }) => name === "search_bytes",
);
expect(
contract?.inputSchema.parse({ pattern: "48 8B ?? ?? 89" }),
).toMatchObject({ limit: 100 });
expect(
contract?.inputSchema.safeParse({ pattern: "48", ignored: true }).success,
).toBe(false);
expect(
contract?.inputSchema.safeParse({ pattern: "48", mask: "f0", limit: 1000 })
.success,
).toBe(true);
});
describe("read_bytes contract", () => {
it("accepts lengths above the former arbitrary ceiling and rejects zero", () => {
const contract = OFFICIAL_TOOL_CONTRACTS.find(
@@ -257,3 +257,72 @@ it("keeps the untrusted target-content marker on an oversized error's replacemen
await Promise.all([client.close(), server.close()]);
}
});
// The delivered error wire omits only the private structured carrier; its
// `isError` flag and any provenance marker count against the result budget.
const deliveredErrorBytes = (nameBytes: number, targetContent: boolean) => {
const { structuredContent: _carrier, ...wire } = failureWithName(
nameBytes,
targetContent,
);
return Buffer.byteLength(JSON.stringify(wire));
};
it.each([false, true])(
"applies the result budget to the complete delivered error (target content: %s)",
async (targetContent) => {
const fittingName =
delivery.resultBudgetBytes - deliveredErrorBytes(0, targetContent);
expect(deliveredErrorBytes(fittingName, targetContent)).toBe(
delivery.resultBudgetBytes,
);
const server = new EvidenceMcpServer(
{ name: "error-budget-test", version: "1" },
{ capabilities: {} },
undefined,
delivery,
);
server.registerTool(
"fitting_failure",
{ inputSchema: z.object({}) },
async () => failureWithName(fittingName, targetContent),
);
server.registerTool(
"one_byte_over_failure",
{ inputSchema: z.object({}) },
async () => failureWithName(fittingName + 1, targetContent),
);
const client = new Client({ name: "error-budget-client", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const fitting = await client.callTool({
name: "fitting_failure",
arguments: {},
});
expect(fitting.isError).toBe(true);
expect(parseMcpToolError(fitting)).toEqual(
failureWithName(fittingName, targetContent).structuredContent,
);
const over = await client.callTool({
name: "one_byte_over_failure",
arguments: {},
});
expect(over.isError).toBe(true);
expect(over.structuredContent).toBeUndefined();
expect(parseMcpToolError(over)).toMatchObject({
error: {
code: "resource_constraint",
details: {
reported_limits: { original_error_code: "invalid_request" },
},
},
});
} finally {
await Promise.all([client.close(), server.close()]);
}
},
20000,
);
@@ -0,0 +1,184 @@
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import type { CallToolResult } from "@modelcontextprotocol/server";
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it } from "vitest";
import { z } from "zod";
import { createServer } from "../../../src/server/createServer.js";
import { evidenceBundleSchema } from "../../../src/domain/evidenceBundle.js";
import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js";
import { err, ok } from "../../../src/domain/result.js";
import {
createCacheProvider,
createTestBinarySession,
} from "../../fixtures/binarySession.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
const bundleResult = (reply: CallToolResult) =>
z.object({ result: evidenceBundleSchema }).parse(reply.structuredContent)
.result;
const successfulTool = async (
client: Client,
name: string,
arguments_: Record<string, unknown> = {},
): Promise<CallToolResult> => {
const reply = await client.callTool({ name, arguments: arguments_ });
expect(reply.isError, `${name}: ${JSON.stringify(reply.content)}`).not.toBe(
true,
);
return reply;
};
const recordUnknownHistory = async (client: Client): Promise<void> => {
const fields = {
severity: "medium",
required_authority: "shipped-artifact",
required_confidence: "observed",
required_environment: null,
recommended_probes: [],
relationships: [],
};
const unknown = await successfulTool(client, "record_unknown", {
...fields,
question: "Which source revision produced this target?",
domain: "provenance",
});
const recorded = z
.object({ result: z.object({ unknown_id: z.string() }) })
.parse(unknown.structuredContent).result;
await successfulTool(client, "update_unknown", {
...fields,
unknown_id: recorded.unknown_id,
expected_revision: 1,
status: "investigating",
supporting_evidence_ids: [],
contradicting_evidence_ids: [],
resolution: null,
});
};
it.each(["switch", "restart"] as const)(
"preserves investigation records through failed target %s and cleanup retries",
async (transition) => {
const root = await createTestTempDirectory("rea-mcp-switch-evidence-");
const first = join(root, "first.hop");
const second = join(root, "second.hop");
await writeFile(first, "first source-owned target");
await writeFile(second, "second source-owned target");
const application = join(root, "application");
await mkdir(application);
await writeFile(
join(application, "index.js"),
'export const label = "retained";',
);
const nextTarget = transition === "switch" ? second : first;
const calls: string[] = [];
const base = createCacheProvider(calls);
let cleanupAllowed = true;
let created = 0;
const failure = new ProviderCleanupError(
"fixture",
[join(root, "runtime")],
{
reason: "fixture cleanup temporarily unavailable",
},
);
const session = createTestBinarySession({
...base,
createClient: (target, profile, context) => {
created += 1;
return {
...base.createClient(target, profile, context),
operationHealthSnapshot: () => ({
state: cleanupAllowed ? "idle" : "exited",
stage: "analysis",
retryAction: cleanupAllowed ? null : "restart_provider",
exitCode: cleanupAllowed ? null : 1,
requests: [],
}),
close: () =>
Promise.resolve(cleanupAllowed ? ok(null) : err(failure)),
};
},
});
const server = createServer(
{ kind: "session", session },
{ environment: {} },
);
const client = new Client({
name: "switch-evidence-recovery",
version: "1",
});
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
const call = (name: string, arguments_: Record<string, unknown> = {}) =>
successfulTool(client, name, arguments_);
const query = { address: "0x1000", document: "fixture" };
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
await call("open_binary", { path: first });
// The cached primitive must be discarded when the provider is retired,
// independently of the investigation's Evidence and Unknown history.
await call("address_name", query);
const analyzed = await call("analyze_javascript_application", {
input_path: application,
format: "directory",
max_heap_mb: 128,
});
const applicationId = z
.object({ evidence_id: z.string() })
.parse(analyzed.structuredContent).evidence_id;
await recordUnknownHistory(client);
const before = bundleResult(await call("get_evidence_bundle"));
expect(before.records).toHaveLength(4);
expect(before.unknowns).toHaveLength(2);
cleanupAllowed = false;
for (let attempt = 0; attempt < 2; attempt += 1) {
const replacement = await client.callTool({
name: "open_binary",
arguments: { path: nextTarget },
});
expect(parseMcpToolError(replacement)).toMatchObject({
error: { code: "cleanup_incomplete" },
});
expect(session.activeTarget()).toBeUndefined();
expect(created).toBe(1);
expect(bundleResult(await call("get_evidence_bundle"))).toEqual(before);
}
const exportedPath = join(root, "retained.json");
await call("export_evidence_bundle", { path: exportedPath });
expect(
evidenceBundleSchema.parse(
JSON.parse(await readFile(exportedPath, "utf8")),
),
).toEqual(before);
cleanupAllowed = true;
await call("open_binary", { path: nextTarget });
expect(bundleResult(await call("get_evidence_bundle"))).toEqual(before);
await call("address_name", query);
expect(
calls.filter((operation) => operation === "address_name"),
).toHaveLength(2);
expect(created).toBe(2);
await call("inspect_analysis_view", {
source: { kind: "retained-evidence", evidence_id: applicationId },
view: { kind: "page", collection: "modules", offset: 0, limit: 10 },
});
await call("close_binary");
const closed = bundleResult(await call("get_evidence_bundle"));
expect(closed.records).toEqual([]);
expect(closed.unknowns).toEqual([]);
} finally {
cleanupAllowed = true;
await client.close();
await server.close();
await session.close();
}
},
);
@@ -217,6 +217,46 @@ function resolveReference(schema: unknown, reference: string): unknown {
return value;
}
function localReferenceExpansions(schema: Record<string, unknown>): number {
// Clients that render SDK declarations from the advertised input expand
// schema-local references against a shared per-input budget; Pi 1.1.0
// code-mode allows 32 and renders every later reference as `unknown`.
// Count conservatively: every resolvable, non-cyclic reference occurrence
// reached while walking the input, never the definitions themselves.
let count = 0;
const visit = (node: unknown, active: ReadonlySet<string>): void => {
if (Array.isArray(node)) {
node.forEach((child) => visit(child, active));
return;
}
if (!isRecord(node)) return;
if (typeof node.$ref === "string" && !active.has(node.$ref)) {
const target = resolveReference(schema, node.$ref);
if (target !== undefined) {
count += 1;
visit(target, new Set([...active, node.$ref]));
return;
}
}
for (const [key, value] of Object.entries(node)) {
if (
[
"$defs",
"definitions",
"examples",
"default",
"const",
"enum",
].includes(key)
)
continue;
visit(value, active);
}
};
visit(schema, new Set());
return count;
}
function recursiveReferences(tools: readonly ToolSchemas[]): string[] {
return tools.flatMap((tool) =>
(["inputSchema", "outputSchema"] as const).flatMap((kind) => {
@@ -674,6 +714,14 @@ it("ships nonrecursive input and output schemas in the generated catalog", () =>
expect(recursiveReferences(GENERATED_MCP_TOOL_CATALOG)).toEqual([]);
});
it("keeps advertised input references within the client expansion budget", () => {
for (const tool of GENERATED_MCP_TOOL_CATALOG)
expect(
localReferenceExpansions(tool.inputSchema),
`${tool.name} exhausts the shared reference-expansion budget`,
).toBeLessThanOrEqual(32);
});
it("rejects unresolved output-schema references even for uncalled tools", () => {
const errors = schemaErrors([
{
+97 -95
View File
@@ -1,5 +1,9 @@
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { Ajv2020 } from "ajv/dist/2020.js";
import {
advertisedMcpToolSchemas,
assertRecordedMcpEvidence,
} from "../../fixtures/advertisedMcpTool.js";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { expect, it, onTestFinished, vi } from "vitest";
import { readFile, realpath, writeFile } from "node:fs/promises";
import { join } from "node:path";
@@ -20,20 +24,6 @@ import {
wasmArtifactExecution,
} from "../../fixtures/wasm/artifact.js";
const wasmToolSchemas = async (client: Client) => {
const advertised = (await client.listTools()).tools.find(
(tool) => tool.name === "inspect_wasm_artifact",
);
if (advertised?.outputSchema === undefined)
throw new Error("WASM recovery must name an advertised static tool");
const ajv = new Ajv2020({ strict: false, validateFormats: false });
const inputSchema: Record<string, unknown> = advertised.inputSchema;
const outputSchema: Record<string, unknown> = advertised.outputSchema;
expect(ajv.validateSchema(inputSchema)).toBe(true);
expect(ajv.validateSchema(outputSchema)).toBe(true);
return { name: advertised.name, ajv, inputSchema, outputSchema };
};
const assertFirstSummaryDelivery = async (
harness: Awaited<ReturnType<typeof createApplicationMcpHarness>>,
value: ReturnType<typeof wasmArtifactFixture>,
@@ -43,7 +33,7 @@ const assertFirstSummaryDelivery = async (
ajv: wasmAjv,
inputSchema,
outputSchema,
} = await wasmToolSchemas(harness.client);
} = await advertisedMcpToolSchemas(harness.client, "inspect_wasm_artifact");
const summaryInput = { path: value.artifact.path, detail: "summary" };
expect(wasmAjv.validate(inputSchema, summaryInput)).toBe(true);
expect(
@@ -91,73 +81,86 @@ const assertFirstSummaryDelivery = async (
return { summary, retained };
};
it("directs rejected native WASM opening to the advertised static tool while preserving missing-WABT diagnostics", async () => {
const directory = await createTestTempDirectory("rea-wasm-recovery-");
const path = join(
await realpath(directory),
process.platform === "win32" ? "module space.bin" : 'module "quoted".bin',
);
await writeFile(path, Buffer.from([0, 0x61, 0x73, 0x6d, 1, 0, 0, 0]));
const createClient = vi.fn(() => {
throw new Error("native provider must not start for WASM");
});
const session = createTestBinarySession(createClient);
const harness = await createApplicationMcpHarness({
session,
serverOptions: { environment: {} },
});
onTestFinished(harness.close);
const { client } = harness;
const { name, ajv, inputSchema } = await wasmToolSchemas(client);
const input = { path };
expect(
toolContract("inspect_wasm_artifact").inputSchema.safeParse(input).success,
).toBe(true);
const opening = await client.callTool({
name: "open_binary",
arguments: input,
});
expect(opening.isError).toBe(true);
const failure = parseMcpToolError(opening);
expect(failure).toMatchObject({
error: {
code: "target_unavailable",
category: "unsupported_target",
details: { constraint: "wasm_requires_static_inspection" },
},
});
expect(failure.error.remediation.action).toContain(JSON.stringify({ path }));
expect(ajv.validate(inputSchema, input)).toBe(true);
const inspection = await client.callTool({
name,
arguments: input,
});
expect(inspection.isError).toBe(true);
expect(parseMcpToolError(inspection)).toMatchObject({
error: {
code: "capability_unavailable",
details: {
operation: "inspect_wasm_artifact",
reason: expect.stringContaining("REA_WABT_BIN_DIRECTORY"),
it.each(["bin", "hop"])(
"directs rejected native WASM opening (%s suffix) to the advertised static tool while preserving missing-WABT diagnostics",
async (extension) => {
const directory = await createTestTempDirectory("rea-wasm-recovery-");
const path = join(
await realpath(directory),
process.platform === "win32"
? `module space.${extension}`
: `module "quoted".${extension}`,
);
await writeFile(path, Buffer.from([0, 0x61, 0x73, 0x6d, 1, 0, 0, 0]));
const createClient = vi.fn(() => {
throw new Error("native provider must not start for WASM");
});
const session = createTestBinarySession(createClient);
const harness = await createApplicationMcpHarness({
session,
serverOptions: { environment: {} },
});
onTestFinished(harness.close);
const { client } = harness;
const { name, ajv, inputSchema } = await advertisedMcpToolSchemas(
client,
"inspect_wasm_artifact",
);
const input = { path };
const opening = await client.callTool({
name: "open_binary",
arguments: input,
});
expect(opening.isError).toBe(true);
const failure = parseMcpToolError(opening);
expect(failure).toMatchObject({
error: {
code: "target_unavailable",
category: "unsupported_target",
details: { constraint: "wasm_requires_static_inspection" },
},
},
});
const summary = await client.callTool({
name,
arguments: { ...input, detail: "summary" },
});
expect(parseMcpToolError(summary)).toMatchObject({
error: {
code: "capability_unavailable",
details: { reason: expect.stringContaining("REA_WABT_BIN_DIRECTORY") },
},
});
await client.ping();
expect(createClient).not.toHaveBeenCalled();
expect(await readFile(path)).toEqual(
Buffer.from([0, 0x61, 0x73, 0x6d, 1, 0, 0, 0]),
);
});
});
const [, recoveryName, serializedInput] =
failure.error.remediation.action.match(
/call (\w+) with (\{.*\}) or run/,
) ?? [];
expect(recoveryName).toBe(name);
const recoveryInput = z
.object({ path: z.literal(path) })
.strict()
.parse(JSON.parse(serializedInput ?? "null"));
expect(ajv.validate(inputSchema, recoveryInput)).toBe(true);
const inspection = await client.callTool({
name: recoveryName ?? "",
arguments: recoveryInput,
});
expect(inspection.isError).toBe(true);
expect(parseMcpToolError(inspection)).toMatchObject({
error: {
code: "capability_unavailable",
details: {
operation: "inspect_wasm_artifact",
reason: expect.stringContaining("REA_WABT_BIN_DIRECTORY"),
},
},
});
const summary = await client.callTool({
name,
arguments: { ...input, detail: "summary" },
});
expect(parseMcpToolError(summary)).toMatchObject({
error: {
code: "capability_unavailable",
details: { reason: expect.stringContaining("REA_WABT_BIN_DIRECTORY") },
},
});
await client.ping();
expect(createClient).not.toHaveBeenCalled();
expect(await readFile(path)).toEqual(
Buffer.from([0, 0x61, 0x73, 0x6d, 1, 0, 0, 0]),
);
},
);
it("advertises exact valid schemas and records inline WASM interface evidence without a binary target", async () => {
const value = wasmArtifactFixture();
@@ -175,7 +178,11 @@ it("advertises exact valid schemas and records inline WASM interface evidence wi
});
onTestFinished(harness.close);
const { client } = harness;
const { ajv, inputSchema, outputSchema } = await wasmToolSchemas(client);
const schemas = await advertisedMcpToolSchemas(
client,
"inspect_wasm_artifact",
);
const { ajv, inputSchema } = schemas;
expect(ajv.validate(inputSchema, { path: value.artifact.path })).toBe(true);
expect(
ajv.validate(inputSchema, {
@@ -188,19 +195,14 @@ it("advertises exact valid schemas and records inline WASM interface evidence wi
name: "inspect_wasm_artifact",
arguments: { path: value.artifact.path },
});
expect(response.isError).not.toBe(true);
expect(
ajv.validate(outputSchema, response.structuredContent),
JSON.stringify(ajv.errors),
).toBe(true);
const parsed = toolContract("inspect_wasm_artifact").outputSchema.parse(
response.structuredContent,
assertRecordedMcpEvidence(
response,
"inspect_wasm_artifact",
schemas,
session,
value,
"observed",
);
const evidence = parseEvidence(parsed);
expect(evidence.confidence).toBe("observed");
expect(evidence.subject?.digest.sha256).toBe(value.artifact.sha256);
expect(parsed.normalized_result).toEqual(value);
expect(session.evidenceById(evidence.evidence_id)).toEqual(evidence);
expect(
(
await client.callTool({
@@ -7,6 +7,7 @@ import {
readFile,
symlink,
unlink,
writeFile,
} from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
+16 -3
View File
@@ -342,17 +342,30 @@ it("exposes overload candidates and executes only an explicitly selected ambiguo
expect(result.method).toMatchObject({
overload_index: 1,
reported_signature: "choose(int): void",
dex_descriptor: null,
dex_descriptor: "(I)V",
});
expect(result.source.text).toContain("overload 1");
const references = await service.execute("trace_android_references", input);
expect(references).toMatchObject({
ok: false,
error: {
_tag: "AnalysisCapabilityUnavailableError",
reason: expect.stringContaining("unique method name"),
_tag: "AnalysisInputError",
issues: [expect.objectContaining({ path: ["overload_index"] })],
},
});
const selectedReferences = await service.execute("trace_android_references", {
...input,
overload_index: 1,
});
if (!selectedReferences.ok) throw selectedReferences.error;
expect(
androidResultSchemas.trace_android_references.parse(
selectedReferences.value.normalized_result,
),
).toMatchObject({
overload_index: 1,
reported_signature: "choose(int): void",
});
await provider.close();
await verifyCleanup(launches);
});
@@ -486,7 +486,9 @@ describe("Ghidra Windows launcher", () => {
}),
).toThrow(/metacharacters/u);
});
});
describe("Ghidra Windows launcher runtime", () => {
it.skipIf(process.platform !== "win32").each([
{
properties:
@@ -208,6 +208,22 @@ it("preserves missing callgraph coverage through direct, graph, and trace workfl
]),
);
const dossier = await client.execute("analyze_function", {
procedure: "main",
});
if (!dossier.ok) throw dossier.error;
expect(dossier.value.result).toMatchObject({
callers: [],
callees: [],
facet_availability: {
callers: { available: false },
callees: {
available: false,
reason: expect.stringContaining("callgraph was unavailable"),
},
},
});
const application = new EnhancedTools(client);
const graph = await application.execute("get_call_graph", {
address: "main",
@@ -225,6 +225,79 @@ it("retains native inventory facts from captured Apple tool output", async () =>
});
});
it("reports a renamed re-export once under its exported name", async () => {
const client = new NativeMacOSProvider(
{},
new FixtureRunner({
"dyld_info:-exports": await fixture(
"dyld-inventory/renamed-reexports.txt",
),
nm: "_foo_renamed\n_own\n",
}),
"darwin",
).createClient(machoTarget("/private/fixture"));
const execution = await client.execute("inspect_macho", {});
if (!execution.ok) throw execution.error;
expect(execution.value.result).toMatchObject({
exports: {
items: [
{
name: "_foo_renamed",
address: null,
weak: null,
reexport: true,
source: "libfoo",
},
{ name: "_own", reexport: false, source: null },
],
limitations: [
"Merged nm/dyld_info results may be incomplete for stripped binaries.",
"Renamed re-exports are listed under their exported name; the source library's symbol name is not reported.",
],
},
});
});
it("splits re-export rows at the source library", () => {
// dyld_info -exports rows for synthetic dylibs: a source symbol named
// `operator()`, and a library whose install name contains ` from `.
expect(
parseDyldSymbols(
[
"[re-export] _call_renamed (operator() from libfoo)",
"[re-export] _g (from lib from foo)",
"[re-export] _nested (from lib (from foo))",
].join("\n"),
"exports",
null,
new Set(["_call_renamed", "_g", "_nested"]),
),
).toEqual([
{
name: "_call_renamed",
address: null,
weak: null,
reexport: true,
source: "libfoo",
sourceSymbol: "operator()",
},
{
name: "_g",
address: null,
weak: null,
reexport: true,
source: "lib from foo",
},
{
name: "_nested",
address: null,
weak: null,
reexport: true,
source: "lib (from foo)",
},
]);
});
it("retains symbol names containing spaces in dyld inventory rows", () => {
expect(
parseDyldSymbols(" 0x0000 _entry with space (from fixture)", "imports"),
@@ -449,6 +522,25 @@ it("leaves an ambiguous short and literal name to the caller's nm inventory", ()
).toEqual([]);
});
it("retains unmatched re-export rows when the known export inventory is incomplete", () => {
expect(
parseDyldSymbols(
"[re-export] _g (from libfoo)",
"exports",
null,
new Set(["_own"]),
),
).toEqual([
{
name: "_g",
address: null,
weak: null,
reexport: true,
source: "libfoo",
},
]);
});
it("retains annotation parsing without nm name evidence", () => {
expect(parseDyldSymbols("0x120 _weak [weak-def]", "exports")).toEqual([
{
@@ -229,6 +229,62 @@ it("normalizes legacy version-minimum commands into build metadata", () => {
]);
});
it("names numeric build-version platforms and tools", () => {
const load = parseOtoolLoadCommands(
withOtoolMachHeader(
[
"Load command 0",
" cmd LC_BUILD_VERSION",
" cmdsize 48",
" platform 1",
" minos 11.0",
" sdk 26.5",
" ntools 2",
" tool 3",
" version 1267.0",
" tool 1029",
" version 1.0",
"Load command 1",
" cmd LC_BUILD_VERSION",
" cmdsize 24",
" platform 99",
" minos 1.0",
" sdk 1.0",
" ntools 0",
"Load command 2",
" cmd LC_BUILD_VERSION",
" cmdsize 32",
" platform MACCATALYST",
" minos 14.0",
" sdk 14.2",
" ntools 1",
" tool SWIFT",
" version 5.9",
"",
].join("\n"),
),
);
expect(load.builds).toEqual([
{
platform: "MACOS",
minimum_os: "11.0",
sdk: "26.5",
tools: [
{ name: "LD", version: "1267.0" },
{ name: "1029", version: "1.0" },
],
},
{ platform: "99", minimum_os: "1.0", sdk: "1.0", tools: [] },
{
platform: "MACCATALYST",
minimum_os: "14.0",
sdk: "14.2",
tools: [{ name: "SWIFT", version: "5.9" }],
},
]);
expect(load.commands[0]?.fields.platform).toBe(1);
});
it("does not classify LC_LOAD_DYLINKER as a dylib dependency", async () => {
const load = parseOtoolLoadCommands(
await nativeFixture("native-macho-header/otool-header-load.txt"),
@@ -273,3 +329,78 @@ it("limits thread-state entrypoints instead of reporting none silently", async (
"Thread-state entrypoints (LC_UNIXTHREAD/LC_THREAD) are retained as raw load commands; file offsets are not derived.",
);
});
it.each([
["1", "MACOS"],
["2", "IOS"],
["3", "TVOS"],
["4", "WATCHOS"],
["5", "BRIDGEOS"],
["6", "MACCATALYST"],
["7", "IOSSIMULATOR"],
["8", "TVOSSIMULATOR"],
["9", "WATCHOSSIMULATOR"],
["10", "DRIVERKIT"],
["11", "VISIONOS"],
["12", "VISIONOSSIMULATOR"],
["13", "FIRMWARE"],
["14", "SEPOS"],
["15", "MACOS_EXCLAVECORE"],
["16", "MACOS_EXCLAVEKIT"],
["17", "IOS_EXCLAVECORE"],
["18", "IOS_EXCLAVEKIT"],
["19", "TVOS_EXCLAVECORE"],
["20", "TVOS_EXCLAVEKIT"],
["21", "WATCHOS_EXCLAVECORE"],
["22", "WATCHOS_EXCLAVEKIT"],
["23", "VISIONOS_EXCLAVECORE"],
["24", "VISIONOS_EXCLAVEKIT"],
] as const)("names build platform %s as %s", (code, name) => {
const load = parseOtoolLoadCommands(
withOtoolMachHeader(
[
"Load command 0",
"cmd LC_BUILD_VERSION",
"cmdsize 24",
`platform ${code}`,
"minos 1.0",
"sdk 1.0",
"ntools 0",
"",
].join("\n"),
),
);
expect(load.builds[0]?.platform).toBe(name);
});
it.each([
["1", "CLANG"],
["2", "SWIFT"],
["3", "LD"],
["4", "LLD"],
["1024", "METAL"],
["1025", "AIRLLD"],
["1026", "AIRNT"],
["1027", "AIRNT_PLUGIN"],
["1028", "AIRPACK"],
["1031", "GPUARCHIVER"],
["1032", "METAL_FRAMEWORK"],
] as const)("names build tool %s as %s", (code, name) => {
const load = parseOtoolLoadCommands(
withOtoolMachHeader(
[
"Load command 0",
"cmd LC_BUILD_VERSION",
"cmdsize 32",
"platform 1",
"minos 1.0",
"sdk 1.0",
"ntools 1",
`tool ${code}`,
"version 1.0",
"",
].join("\n"),
),
);
expect(load.builds[0]?.tools).toEqual([{ name, version: "1.0" }]);
});
@@ -12,6 +12,7 @@ import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js
import { NativeMacOSProvider } from "../../../../src/native/NativeMacOSProvider.js";
import {
NATIVE_COMMAND_OUTPUT_BUDGET_BYTES,
NATIVE_FAILURE_DIAGNOSTIC_BYTES,
NativeCommandFailure,
XcrunCommandRunner,
type NativeCommandRunner,
@@ -381,6 +382,36 @@ describe("native command failure capture and limits", () => {
});
});
it("bounds both failure streams at a UTF-8 boundary while keeping their totals", async () => {
const runner = new XcrunCommandRunner({}, () =>
Promise.resolve(ok({ path: process.execPath, sha256: "a".repeat(64) })),
);
// "é" is two bytes and straddles the excerpt boundary, so it is dropped whole.
const stdout = `${"x".repeat(NATIVE_FAILURE_DIAGNOSTIC_BYTES - 1)}é${"y".repeat(1024)}`;
const stderr = "z".repeat(NATIVE_FAILURE_DIAGNOSTIC_BYTES + 1024);
// Build the output in the child: one Linux argument is limited to 128 KiB.
const result = await runner.run(
"file",
[
"-e",
`const stdout = "x".repeat(${String(NATIVE_FAILURE_DIAGNOSTIC_BYTES - 1)}) + "\u00e9" + "y".repeat(1024); const stderr = "z".repeat(${String(NATIVE_FAILURE_DIAGNOSTIC_BYTES + 1024)}); process.stdout.write(stdout, () => process.stderr.write(stderr, () => process.exit(3)));`,
],
{},
);
expect(result.ok).toBe(false);
if (result.ok) return;
expect(result.error.reason).toBe("nonzero-exit");
expect(result.error.capture).toMatchObject({
stdout: "x".repeat(NATIVE_FAILURE_DIAGNOSTIC_BYTES - 1),
stderr: "z".repeat(NATIVE_FAILURE_DIAGNOSTIC_BYTES),
stdoutBytes: Buffer.byteLength(stdout),
stderrBytes: Buffer.byteLength(stderr),
exitCode: 3,
truncated: true,
});
});
it("rejects output beyond the complete native-command budget", async () => {
const runner = new XcrunCommandRunner({}, () =>
Promise.resolve(ok({ path: process.execPath, sha256: "a".repeat(64) })),
@@ -404,6 +435,13 @@ describe("native command failure capture and limits", () => {
(result.error.capture?.stdoutBytes ?? 0) +
(result.error.capture?.stderrBytes ?? 0),
).toBeLessThanOrEqual(NATIVE_COMMAND_OUTPUT_BUDGET_BYTES);
// The failure carries an excerpt, not the whole retained output budget.
expect(result.error.capture?.stdout.length).toBe(
NATIVE_FAILURE_DIAGNOSTIC_BYTES,
);
expect(result.error.capture?.stdoutBytes).toBeGreaterThan(
NATIVE_FAILURE_DIAGNOSTIC_BYTES,
);
});
});
@@ -834,6 +872,43 @@ describe("native signature entitlements", () => {
});
});
/** Fail every codesign call nonzero after writing more than one excerpt. */
class VerboseCodeSignFailureRunner extends FixtureRunner {
override async run(tool: string, arguments_: readonly string[]) {
const result = await super.run(tool, arguments_);
if (!result.ok || tool !== "codesign") return result;
const stderr = "e".repeat(NATIVE_FAILURE_DIAGNOSTIC_BYTES * 2);
return ok({
...result.value,
stderr,
stderrBytes: Buffer.byteLength(stderr),
exitCode: 1,
});
}
}
describe("native signature failure diagnostics", () => {
it("bounds accepted nonzero codesign output once it becomes a failure", async () => {
directory = await createTestTempDirectory("rea-signature-failure-");
const executable = join(directory, "fixture");
await writeFile(executable, "fixture");
const client = new NativeMacOSProvider(
{},
new VerboseCodeSignFailureRunner(),
"darwin",
).createClient(await nativeMachoTargetForFile(executable));
const signature = await client.execute("inspect_signature", {});
if (signature.ok) throw new Error("A failed codesign run must fail");
expect(signature.error.capturedOutput).toMatchObject({
stderr: "e".repeat(NATIVE_FAILURE_DIAGNOSTIC_BYTES),
stderr_bytes: NATIVE_FAILURE_DIAGNOSTIC_BYTES * 2,
truncated: true,
});
});
});
describe("native dispatch metadata error results", () => {
it("preserves tagged cancellation and integrity failures", async () => {
directory = await createTestTempDirectory("rea-dispatch-errors-");
@@ -1,3 +1,7 @@
import {
descriptorFailurePath,
expectRetainedDescriptor,
} from "../../fixtures/descriptorCloseFailure.js";
import { expect, it, onTestFinished, vi } from "vitest";
import { writeFile } from "node:fs/promises";
import type { FileHandle } from "node:fs/promises";
@@ -15,27 +19,22 @@ const cleanup = vi.hoisted(() => ({
}));
vi.mock("node:fs/promises", async (importOriginal) => {
const filesystem = await importOriginal<typeof import("node:fs/promises")>();
return {
...filesystem,
open: async (...args: Parameters<typeof filesystem.open>) => {
const handle = await filesystem.open(...args);
if (args[0] === cleanup.path) {
cleanup.handles.push(handle);
const close = handle.close.bind(handle);
handle.close = async () => {
if (cleanup.denied && handle.fd >= 0)
throw new Error("Descriptor close deliberately denied");
await close();
};
}
return handle;
},
};
const { withDescriptorCloseFailure } =
await import("../../fixtures/descriptorCloseFailure.js");
return withDescriptorCloseFailure(filesystem, cleanup);
});
it
.runIf(process.platform === "linux")
.each(["capture", "tool", "limiter", "kernel"])(
.each([
"capture",
"tool",
"limiter",
"kernel",
"field-tool",
"field-limiter",
"field-kernel",
])(
"retains failed %s descriptors and retries them before further acquisition and shutdown",
async (member) => {
const directory = await createTestTempDirectory("rea-signal-descriptor-");
@@ -67,14 +66,16 @@ it
},
() => PrivateRuntimeRoot.create({ parent: directory }),
);
cleanup.path =
member === "capture"
? path
: member === "tool"
? command
: member === "limiter"
? limiter
: "/proc/self/limits";
const discovery = member.startsWith("field-");
const resource = discovery ? member.slice(6) : member;
const operation = discovery
? "inspect_signal_decoders"
: "decode_signal_recording";
cleanup.path = descriptorFailurePath(resource, {
capture: path,
tool: command,
limiter,
});
cleanup.denied = true;
cleanup.handles = [];
onTestFinished(async () => {
@@ -84,22 +85,20 @@ it
cleanup.path = "";
});
const service = new SignalRecordingService(provider);
const first = await service.decode({ path, decoders: "uart" });
expect(first).toMatchObject({
ok: false,
error: { cleanupIncomplete: true, cleanupResources: [cleanup.path] },
});
expect(cleanup.handles).toHaveLength(1);
expect(cleanup.handles[0]?.fd).toBeGreaterThanOrEqual(0);
const first = discovery
? await service.inspectDecoders({ query: "uart" })
: await service.decode({ path, decoders: "uart" });
expectRetainedDescriptor(first, cleanup, operation);
const launchCount = launches;
expect(await service.decode({ path, decoders: "uart" })).toMatchObject({
ok: false,
error: { cleanupIncomplete: true },
error: { cleanupIncomplete: true, operation },
});
expect(cleanup.handles).toHaveLength(1);
expect(launches).toBe(launchCount);
await expect(provider.close()).rejects.toMatchObject({
cleanupIncomplete: true,
operation,
});
cleanup.denied = false;
await provider.close();
@@ -130,6 +130,71 @@ it("accounts for JSON escape expansion of a stored declaration in the reply limi
]);
});
it("attributes triggers declared with another ASCII case to the stored table name", async () => {
const path = await createDatabase(`
CREATE TABLE Users(id INTEGER PRIMARY KEY, email TEXT);
CREATE INDEX users_email ON users(email);
CREATE TRIGGER audit_insert AFTER INSERT ON users BEGIN SELECT 1; END;
CREATE TRIGGER audit_delete AFTER DELETE ON USERS BEGIN SELECT 1; END;
CREATE VIEW recent AS SELECT 1;
CREATE TRIGGER recent_insert INSTEAD OF INSERT ON Recent BEGIN SELECT 1; END;
`);
const selected = inspectSqliteDatabaseSnapshot(path, {
path,
table: "Users",
});
expect(selected.schema).toMatchObject({
scope: "selected_table",
completeness: "complete",
tables: [{ name: "Users" }],
indexes: [{ name: "users_email", table: "Users" }],
views: [],
});
expect(
selected.schema.triggers.map(({ name, table }) => ({ name, table })),
).toEqual([
{ name: "audit_insert", table: "Users" },
{ name: "audit_delete", table: "Users" },
]);
const inventory = inspectSqliteDatabaseSnapshot(path, { path });
expect(inventory.schema.triggers).toEqual([
{ name: "audit_insert", table: "Users" },
{ name: "audit_delete", table: "Users" },
{ name: "recent_insert", table: "recent" },
]);
});
it.each([
{
selection: "a differently cased table",
schema: "CREATE TABLE Users(id INTEGER PRIMARY KEY)",
trigger: "AFTER INSERT ON Users",
table: "users",
},
{
selection: "an exact view",
schema: "CREATE VIEW Recent AS SELECT 1 AS id",
trigger: "INSTEAD OF INSERT ON recent",
table: "Recent",
},
])(
"refuses $selection before expanding declarations under another spelling",
async ({ schema, trigger, table }) => {
const path = await createDatabase(`
${schema};
CREATE TRIGGER expanded ${trigger} BEGIN /*${"\u0001".repeat(3 * 1024 * 1024)}*/ SELECT 1; END;
`);
expect(() =>
inspectSqliteDatabaseSnapshot(path, { path, table }),
).toThrowError(
expect.objectContaining({
reason: "selection",
message: `Selected ordinary table does not exist with that exact name: ${table}`,
}),
);
},
);
it.each(["UTF-8", "UTF-16le", "UTF-16be"])(
"preserves exact %s schema text when decoding native bytes",
async (encoding) => {
@@ -357,6 +357,30 @@ it("does not execute views when selecting rows", async () => {
});
});
nativeTest.each(["absent.db", "absent-parent/absent.db"])(
"classifies a missing %s as an invalid value",
async (name) => {
const workspace = await createTestWorkspace("rea-sqlite-missing-");
onTestFinished(() => removeTestWorkspace(workspace.root));
const path = workspace.path(name);
const provider = createProvider();
onTestFinished(() => provider.close());
const result = await provider.inspect({ path });
if (result.ok) throw new Error("Expected a missing database rejection");
expect(projectAnalysisError(result.error)).toMatchObject({
details: {
issues: [
{
path: ["path"],
reason: "invalid_value",
message: `Selected SQLite database does not exist: ${path}`,
},
],
},
});
},
);
it("attributes table and column selection failures to their own arguments", async () => {
const { database } = await createFixture();
const provider = createProvider();
+83 -68
View File
@@ -1,6 +1,7 @@
import { rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { projectAnalysisError } from "../../src/domain/analysisErrorProjection.js";
import { IdaProvider } from "../../src/ida/IdaProvider.js";
import { createIdaTarget, RecordingIdaMcp } from "../fixtures/idaMcp.js";
import { createTestBinarySession } from "../fixtures/binarySession.js";
@@ -21,6 +22,73 @@ afterEach(async () => {
await rm(root, { recursive: true, force: true });
});
const reobservesExternallyMutableState = async (): Promise<void> => {
const { root, target } = await createIdaTarget();
roots.push(root);
const path = join(root, "registration.json");
await writeFile(path, JSON.stringify({ command: "fixture" }));
const config = parseConfig({ REA_IDA_MCP_CONFIG: path });
if (!config.ok) throw config.error;
const producer = new RecordingIdaMcp(target);
const session = createTestBinarySession(
new IdaProvider(config.value, {}, () => producer),
);
expect((await session.open(target.path)).ok).toBe(true);
const query = { procedure: "main", document: target.path };
const first = await session.execute("procedure_pseudo_code", query);
expect(first.ok && first.value.result).toContain("42");
const snapshot = session.exportAnalysisSnapshot();
if (!snapshot.ok) throw snapshot.error;
expect(snapshot.value.entries).toEqual([]);
const profile = session.analysisProfile();
if (profile === undefined) throw new Error("Missing profile");
const cached = createAnalysisExecution(
"stale output",
IDA_PROVIDER_IDENTITY,
{
analysisProfile: profile,
subject: {
path: target.path,
sha256: target.sha256,
format: target.format,
...(target.architecture === undefined
? {}
: { architecture: target.architecture }),
},
},
);
snapshot.value.entries.push(
createAnalysisSnapshotEntry({
target: snapshotTarget(target),
binding: snapshotBinding(profile),
operation: "procedure_pseudo_code",
parameters: query,
execution: cached,
}),
);
snapshot.value.evidence_bundle = createEvidenceBundle([
...snapshot.value.evidence_bundle.records,
createEvidence(target, IDA_PROVIDER_IDENTITY, {
operation: "procedure_pseudo_code",
parameters: query,
result: cached.result,
rawResult: cached.rawResult,
analysisProfile: profile,
}),
]);
expect(session.importAnalysisSnapshot(snapshot.value).ok).toBe(true);
expect(session.allowsSnapshotReplay("procedure_pseudo_code")).toBe(false);
expect(session.allowsSnapshotReplay("binary_overview")).toBe(false);
producer.pseudocode = "int main(void) { return 99; }";
const second = await session.execute("procedure_pseudo_code", query);
expect(second.ok && second.value.result).toContain("99");
expect(
producer.calls.filter(({ name }) => name === "decompile_function"),
).toHaveLength(2);
await session.close();
expect(producer.closes).toBe(1);
};
describe("IDA provider composition", () => {
it("discovers without launching and commits adapter semantics without persisting credentials", async () => {
const { root, target } = await createIdaTarget();
@@ -58,72 +126,10 @@ describe("IDA provider composition", () => {
.find(({ operation }) => operation === "procedure_callers")?.available,
).toBe(false);
});
it("re-observes externally mutable state even when a document is explicit and a snapshot is imported", async () => {
const { root, target } = await createIdaTarget();
roots.push(root);
const path = join(root, "registration.json");
await writeFile(path, JSON.stringify({ command: "fixture" }));
const config = parseConfig({ REA_IDA_MCP_CONFIG: path });
if (!config.ok) throw config.error;
const producer = new RecordingIdaMcp(target);
const session = createTestBinarySession(
new IdaProvider(config.value, {}, () => producer),
);
expect((await session.open(target.path)).ok).toBe(true);
const query = { procedure: "main", document: target.path };
const first = await session.execute("procedure_pseudo_code", query);
expect(first.ok && first.value.result).toContain("42");
const snapshot = session.exportAnalysisSnapshot();
if (!snapshot.ok) throw snapshot.error;
expect(snapshot.value.entries).toEqual([]);
const profile = session.analysisProfile();
if (profile === undefined) throw new Error("Missing profile");
const cached = createAnalysisExecution(
"stale output",
IDA_PROVIDER_IDENTITY,
{
analysisProfile: profile,
subject: {
path: target.path,
sha256: target.sha256,
format: target.format,
...(target.architecture === undefined
? {}
: { architecture: target.architecture }),
},
},
);
snapshot.value.entries.push(
createAnalysisSnapshotEntry({
target: snapshotTarget(target),
binding: snapshotBinding(profile),
operation: "procedure_pseudo_code",
parameters: query,
execution: cached,
}),
);
snapshot.value.evidence_bundle = createEvidenceBundle([
...snapshot.value.evidence_bundle.records,
createEvidence(target, IDA_PROVIDER_IDENTITY, {
operation: "procedure_pseudo_code",
parameters: query,
result: cached.result,
rawResult: cached.rawResult,
analysisProfile: profile,
}),
]);
expect(session.importAnalysisSnapshot(snapshot.value).ok).toBe(true);
expect(session.allowsSnapshotReplay("procedure_pseudo_code")).toBe(false);
expect(session.allowsSnapshotReplay("binary_overview")).toBe(false);
producer.pseudocode = "int main(void) { return 99; }";
const second = await session.execute("procedure_pseudo_code", query);
expect(second.ok && second.value.result).toContain("99");
expect(
producer.calls.filter(({ name }) => name === "decompile_function"),
).toHaveLength(2);
await session.close();
expect(producer.closes).toBe(1);
});
it(
"re-observes externally mutable state even when a document is explicit and a snapshot is imported",
reobservesExternallyMutableState,
);
it("rejects unsupported target kinds and reports incomplete upstream profiles on first analysis", async () => {
const { root, target } = await createIdaTarget();
roots.push(root);
@@ -157,12 +163,21 @@ describe("IDA provider composition", () => {
expect(analyzed).toMatchObject({
ok: false,
error: {
_tag: "AnalysisProtocolError",
message: expect.stringContaining(
_tag: "ProviderAdapterError",
userMessage: expect.stringContaining(
"headless compatibility profile is missing tools: idb_open",
),
},
});
if (analyzed.ok) throw new Error("Expected incompatible IDA profile");
expect(projectAnalysisError(analyzed.error).details).toMatchObject({
provider_id: "ida",
diagnostics: {
mode: "headless",
expected_profile: "database-supervisor",
missing_tools: expect.arrayContaining(["idb_open"]),
},
});
expect(producer.connects).toBe(1);
expect((await session.close()).ok).toBe(true);
expect(producer.closes).toBe(1);
@@ -60,6 +60,78 @@ public class ReaJadxMetadataRegression {
return apk;
}
private static void verifyDirectInvokes(SessionHolder holder, SessionConfig config,
Constructor<?> constructor, Method handle, Path sources, Path output) throws Exception {
SmaliOptions options = new SmaliOptions();
options.apiLevel = 28;
options.jobs = 1;
options.outputDexFile = output.resolve("direct-invokes.dex").toString();
require(Smali.assemble(options, List.of(
sources.resolve("Caller.smali").toString(), sources.resolve("Target.smali").toString())),
"Direct-invoke fixture assembly failed");
Path apk = output.resolve("direct-invokes.apk");
try (ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(apk))) {
zip.putNextEntry(new ZipEntry("classes.dex"));
Files.copy(Path.of(options.outputDexFile), zip);
zip.closeEntry();
}
BuildersKt.runBlocking(EmptyCoroutineContext.INSTANCE,
(scope, continuation) -> holder.unload(continuation));
BuildersKt.runBlocking(EmptyCoroutineContext.INSTANCE,
(scope, continuation) -> holder.load(apk.toString(), config, continuation));
JsonObject refs = call(handle, constructor.newInstance(holder), "get_xrefs_to_method",
methodArguments("fixture.Target", "mark"));
checkDirectInvokes(refs, "classes.dex");
SmaliOptions targetOptions = new SmaliOptions();
targetOptions.apiLevel = 28;
targetOptions.jobs = 1;
targetOptions.outputDexFile = output.resolve("direct-target.dex").toString();
SmaliOptions callerOptions = new SmaliOptions();
callerOptions.apiLevel = 28;
callerOptions.jobs = 1;
callerOptions.outputDexFile = output.resolve("direct-caller.dex").toString();
require(Smali.assemble(targetOptions, List.of(sources.resolve("Target.smali").toString())),
"Target DEX assembly failed");
require(Smali.assemble(callerOptions, List.of(sources.resolve("Caller.smali").toString())),
"Caller DEX assembly failed");
Path splitApk = output.resolve("direct-invokes-multidex.apk");
try (ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(splitApk))) {
zip.putNextEntry(new ZipEntry("classes.dex"));
Files.copy(Path.of(targetOptions.outputDexFile), zip);
zip.closeEntry();
zip.putNextEntry(new ZipEntry("classes2.dex"));
Files.copy(Path.of(callerOptions.outputDexFile), zip);
zip.closeEntry();
}
BuildersKt.runBlocking(EmptyCoroutineContext.INSTANCE,
(scope, continuation) -> holder.unload(continuation));
BuildersKt.runBlocking(EmptyCoroutineContext.INSTANCE,
(scope, continuation) -> holder.load(splitApk.toString(), config, continuation));
JsonObject crossDex = call(handle, constructor.newInstance(holder), "get_xrefs_to_method",
methodArguments("fixture.Target", "mark"));
checkDirectInvokes(crossDex, "classes2.dex");
System.out.println("DIRECT_INVOKE_REGRESSION_PASS nodes=1 offsets=0,3 cross_dex=true");
}
private static void checkDirectInvokes(JsonObject refs, String dexEntry) {
require(refs.get("total").getAsInt() == 1, "JADX using-node count changed");
JsonArray sites = refs.getAsJsonArray("items").get(0).getAsJsonObject()
.getAsJsonArray("direct_invokes");
require(sites.size() == 2, "Raw DEX invoke count differs from the fixture: " + sites);
for (int i = 0; i < sites.size(); i++) {
JsonObject site = sites.get(i).getAsJsonObject();
require(site.get("dex_entry").getAsString().equals(dexEntry), "Wrong DEX entry");
require(site.get("source_raw_method_id").getAsString().equals("fixture.Caller.twice()V"),
"Wrong raw source method");
require(site.get("dex_code_unit_offset").getAsInt() == i * 3, "Wrong DEX offset");
require(site.get("opcode").getAsString().equals("INVOKE_STATIC"), "Wrong opcode");
}
require(sites.get(0).getAsJsonObject().get("dex_method_index").getAsInt()
== sites.get(1).getAsJsonObject().get("dex_method_index").getAsInt(),
"Two invokes did not reference the same DEX method ID");
}
private static JsonObject methodArguments(String className, String methodName) {
JsonObject value = arguments(className);
value.addProperty("method_name", methodName);
@@ -152,13 +224,20 @@ public class ReaJadxMetadataRegression {
JsonObject componentArgs = arguments("androidx.activity.ComponentActivity");
JsonObject componentSummary = call(handle, bridge, "get_class_summary", componentArgs);
List<String> signatures = new ArrayList<>();
List<String> descriptors = new ArrayList<>();
for (JsonElement entry : componentSummary.getAsJsonArray("methods")) {
JsonObject method = entry.getAsJsonObject();
if (method.get("name").getAsString().equals("setContentView"))
if (method.get("name").getAsString().equals("setContentView")) {
signatures.add(method.get("signature").getAsString());
descriptors.add(method.get("dex_descriptor").getAsString());
}
}
require(signatures.size() == 3, "Fixture must contain three setContentView overloads");
require(descriptors.equals(List.of("(I)V", "(Landroid/view/View;)V",
"(Landroid/view/View;Landroid/view/ViewGroup$LayoutParams;)V")),
"Frozen DEX prototypes did not identify the three overloads: " + descriptors);
String[] declarations = { "setContentView(int ", "setContentView(View ", "setContentView(View view, ViewGroup.LayoutParams " };
List<Integer> referenceCounts = new ArrayList<>();
for (int index = 0; index < signatures.size(); index++) {
JsonObject methodArgs = arguments("androidx.activity.ComponentActivity");
methodArgs.addProperty("method_name", "setContentView");
@@ -169,12 +248,35 @@ public class ReaJadxMetadataRegression {
"Overload envelope mismatch");
require(first.get("mode").getAsString().equals("java"), "Fixture did not produce Java");
require(first.get("body").getAsString().contains(declarations[index]), "Wrong overload: " + signatures.get(index));
JsonObject references = call(handle, bridge, "get_xrefs_to_method", methodArgs);
require(references.get("overload_index").getAsInt() == index,
"Reference result did not preserve selected overload index");
require(references.get("reported_signature").getAsString().equals(signatures.get(index)),
"Reference result did not preserve selected overload signature");
if (index == 0) {
JsonArray invokes = references.getAsJsonArray("items").get(0).getAsJsonObject()
.getAsJsonArray("direct_invokes");
require(invokes.size() == 1, "Selected overload must have one raw direct invoke");
JsonObject invoke = invokes.get(0).getAsJsonObject();
require(invoke.get("source_raw_method_id").getAsString()
.equals("androidx.activity.ComponentActivity.onCreate(Landroid/os/Bundle;)V"),
"Wrong source method for overloaded target");
require(invoke.get("dex_code_unit_offset").getAsInt() == 37
&& invoke.get("opcode").getAsString().equals("INVOKE_VIRTUAL"),
"Wrong direct invoke for selected overloaded target");
}
referenceCounts.add(references.get("count").getAsInt());
}
require(referenceCounts.equals(List.of(1, 0, 0)),
"Incoming references were not isolated to the selected overload: " + referenceCounts);
require(componentSummary.equals(call(handle, bridge, "get_class_summary", componentArgs)),
"Frozen summary changed after code generation");
require(apiSummary.equals(call(handle, bridge, "get_class_summary", apiArgs)), "Unrelated frozen summary changed");
System.out.println("METADATA_REGRESSION_PASS classes=" + states.size() + " overloads=" + signatures.size());
System.out.println("METADATA_REGRESSION_PASS classes=" + states.size() + " overloads=" + signatures.size()
+ " reference_counts=" + referenceCounts);
verifyInitializers(holder, config, constructor, handle, initializerFixture(args[1], args[2]));
verifyDirectInvokes(holder, config, constructor, handle,
Path.of(args[3]), Path.of(args[2]));
} finally {
BuildersKt.runBlocking(EmptyCoroutineContext.INSTANCE,
(scope, continuation) -> holder.unload(continuation));
+83 -1
View File
@@ -160,7 +160,89 @@ export async function inspectMipsReadelf(path, byteOrder, floatAbi = "hard") {
};
}
/** Check effective Ghidra observations, not the requested analysis profile. */
/** Independent GNU readelf interpretation for the recordless legacy fixture. */
export async function inspectLegacyMipsReadelf(path, byteOrder) {
const command = process.env.REA_MIPS_READELF ?? "readelf";
const options = {
env: { ...process.env, LC_ALL: "C" },
timeout: 10000,
maxBuffer: 1024 * 1024,
};
const version = await execute(command, ["--version"], options);
assert.match(
version.stdout,
/^GNU readelf\b/u,
"This lane requires GNU readelf",
);
const header = await execute(command, ["-h", "-W", path], options);
assert.equal(
header.stderr.trim(),
"",
`readelf diagnostics: ${header.stderr}`,
);
const field = (expression, label) => {
const matches = [...header.stdout.matchAll(expression)];
assert.equal(matches.length, 1, `Missing or ambiguous readelf ${label}`);
return matches[0][1].trim();
};
assert.equal(field(/^\s*Class:\s*(.+)$/gmu, "class"), "ELF32");
assert.equal(
field(/^\s*Data:.*?,\s*(little|big) endian\s*$/gmu, "byte order"),
byteOrder,
);
assert.match(field(/^\s*Type:\s*(.+)$/gmu, "type"), /^EXEC\b/u);
assert.match(field(/^\s*Machine:\s*(.+)$/gmu, "machine"), /^MIPS\b/u);
const flags = field(/^\s*Flags:\s*(0x[\da-f]+)\b.*$/gmu, "flags");
assert.equal(
Number(flags),
0x50000001,
"Legacy fixture must carry EF_MIPS_ARCH_32 with an unspecified ABI field",
);
const abi = await execute(command, ["-A", path], options);
assert.equal(abi.stderr.trim(), "", `readelf diagnostics: ${abi.stderr}`);
// -A also prints unrelated MIPS facts such as the static GOT; only the ABI
// flags declaration itself must be gone from the recordless fixture.
assert.doesNotMatch(
abi.stdout,
/MIPS ABI Flags Version/u,
"GNU readelf still observes an ABI flags declaration in the legacy fixture",
);
const symbols = await execute(command, ["-s", "-W", path], options);
assert.equal(
symbols.stderr.trim(),
"",
`readelf diagnostics: ${symbols.stderr}`,
);
const parsed = {};
for (const name of ["rea_mips_entry", "rea_mips_probe"]) {
const matches = [
...symbols.stdout.matchAll(
new RegExp(
`^\\s*\\d+:\\s+([\\da-f]+)\\s+\\d+\\s+FUNC\\s+GLOBAL\\s+\\S+\\s+\\d+\\s+${name}\\s*$`,
"gmu",
),
),
];
assert.equal(
matches.length,
1,
`Missing or ambiguous readelf symbol ${name}`,
);
parsed[name] = `0x${BigInt(`0x${matches[0][1]}`).toString(16)}`;
}
return {
flags: Number(flags),
limitations: [],
symbols: parsed,
command,
version: version.stdout.split("\n")[0],
raw: {
header: header.stdout,
abi: abi.stdout,
symbols: symbols.stdout,
},
};
}
export function assertMipsLoadedImage(observations, byteOrder, sha256) {
assert.equal(observations.language_id, language(byteOrder));
assert.equal(observations.compiler_spec_id, "default");
+97 -6
View File
@@ -1,6 +1,6 @@
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import { readFile } from "node:fs/promises";
import { readFile, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
@@ -12,11 +12,13 @@ export async function buildMipsFixture(
directory,
byteOrder,
floatAbi = "hard",
options = {},
) {
if (byteOrder !== "little" && byteOrder !== "big")
throw new Error("MIPS fixture byte order must be little or big");
if (floatAbi !== "hard" && floatAbi !== "soft")
throw new Error("MIPS fixture float ABI must be hard or soft");
const legacy = options.legacy === true;
const soft = floatAbi === "soft";
const target = byteOrder === "little" ? "mipsel-linux-gnu" : "mips-linux-gnu";
const source = fileURLToPath(new URL("../c/mips.c", import.meta.url));
@@ -24,10 +26,13 @@ export async function buildMipsFixture(
new URL("../c/mips-soft-float.c", import.meta.url),
);
const probe = fileURLToPath(new URL("./mips-probe.S", import.meta.url));
const path = join(directory, `${target}${soft ? "-soft-float" : ""}.elf`);
const path = join(
directory,
`${target}${soft ? "-soft-float" : ""}${legacy ? "-legacy" : ""}.elf`,
);
const args = [
`--target=${target}`,
"-march=mips32r2",
`-march=${legacy ? "mips32" : "mips32r2"}`,
"-mabi=32",
...(soft ? ["-msoft-float"] : []),
"-mno-abicalls",
@@ -45,8 +50,88 @@ export async function buildMipsFixture(
"-o",
path,
];
const compiled = await compileMipsFixture(path, byteOrder, 2, args, {
expectedFlags: legacy ? 0x50001001 : 0x70001001,
});
// Legacy variant: retype the ABI record declarations out of the image and
// zero the header ABI field, matching pre-ABI-flags 2000s firmware toolchains
// (their compilers predate the record; the header leaves the ABI unspecified).
let attested = compiled;
if (legacy) {
const raw = await readFile(path);
const write16 = (offset, value) =>
byteOrder === "little"
? raw.writeUInt16LE(value, offset)
: raw.writeUInt16BE(value, offset);
const write32 = (offset, value) =>
byteOrder === "little"
? raw.writeUInt32LE(value, offset)
: raw.writeUInt32BE(value, offset);
const read16 = (offset) =>
byteOrder === "little"
? raw.readUInt16LE(offset)
: raw.readUInt16BE(offset);
const read32 = (offset) =>
byteOrder === "little"
? raw.readUInt32LE(offset)
: raw.readUInt32BE(offset);
const flags = read32(36);
if ((flags & 0xf0000000) !== 0x50000000)
throw new Error("Legacy fixture did not build as EF_MIPS_ARCH_32");
write32(36, flags & ~0x0000f000);
const programOffset = read32(28);
const programCount = read16(44);
if (read16(42) !== 32)
throw new Error("Legacy fixture has non-standard program headers");
for (let index = 0; index < programCount; index += 1) {
const offset = programOffset + index * 32;
if (read32(offset) === 0x70000003) write32(offset, 0);
}
const sectionOffset = read32(32);
const sectionCount = read16(48);
if (read16(46) !== 40)
throw new Error("Legacy fixture has non-standard section headers");
for (let index = 0; index < sectionCount; index += 1) {
const offset = sectionOffset + index * 40;
if (read32(offset + 4) === 0x7000002a) write32(offset + 4, 1);
}
// GNU readelf resolves the record by section name, not type, so a retyped
// header alone keeps a named declaration. Replace the name in the string
// table in place, preserving every remaining name and offset.
const stringTableIndex = read16(50);
const stringTableOffset =
stringTableIndex < sectionCount
? read32(sectionOffset + stringTableIndex * 40 + 16)
: -1;
const named =
stringTableOffset === -1
? -1
: raw.indexOf(
Buffer.from(".MIPS.abiflags", "ascii"),
stringTableOffset,
);
if (named === -1)
throw new Error("Legacy fixture lacks the expected ABI section name");
raw.write("x.rea.no.flags", named, "ascii");
await writeFile(path, raw);
const patched = await readFile(path);
const patchedFlags =
byteOrder === "little"
? patched.readUInt32LE(36)
: patched.readUInt32BE(36);
if (patchedFlags !== 0x50000001)
throw new Error(
"Legacy fixture did not end with the recordless o32 declaration",
);
attested = {
...compiled,
flags: patchedFlags,
sha256: createHash("sha256").update(patched).digest("hex"),
};
}
return {
...(await compileMipsFixture(path, byteOrder, 2, args)),
...attested,
legacy,
float_abi: floatAbi,
source_sha256: createHash("sha256")
.update(await readFile(source))
@@ -108,7 +193,13 @@ export async function buildMipsRelocatableFixture(
}
/** Compile and attest the shared ELF32/o32/MIPS32r2 fixture boundary. */
async function compileMipsFixture(path, byteOrder, objectType, args) {
async function compileMipsFixture(
path,
byteOrder,
objectType,
args,
options = {},
) {
const compiler = process.env.REA_MIPS_CLANG ?? "clang";
const version = await execute(compiler, ["--version"], { timeout: 10000 });
await execute(compiler, args, { timeout: 60000 });
@@ -128,7 +219,7 @@ async function compileMipsFixture(path, byteOrder, objectType, args) {
bytes[5] !== (byteOrder === "little" ? 1 : 2) ||
read16(16) !== objectType ||
read16(18) !== 8 ||
read32(36) !== 0x70001001
read32(36) !== (options.expectedFlags ?? 0x70001001)
)
throw new Error(
"Compiler did not produce the declared ELF32/o32/MIPS32r2 fixture",
+25 -20
View File
@@ -4,26 +4,6 @@ import { resolve } from "node:path";
import { describe, expect, it } from "vitest";
import { PRODUCT_IDENTITY } from "../../src/identity.js";
const translatedReadmes = [
"README_zh.md",
"README_zh-TW.md",
"README_ja.md",
"README_ko.md",
"README_tr.md",
"README_ru.md",
"README_vi.md",
"README_th.md",
"README_de.md",
"README_es.md",
"README_fr.md",
"README_uk.md",
"README_pl.md",
"README_pt-BR.md",
"README_ar.md",
"README_fa.md",
"README_id.md",
] as const;
const jsonExamples = (content: string): unknown[] =>
[...content.matchAll(/```json\s*([\s\S]*?)```/gu)].map((match): unknown =>
JSON.parse(match[1] ?? ""),
@@ -64,6 +44,14 @@ describe("onboarding documentation product facts", () => {
});
it("keeps translated onboarding commands aligned with the English examples", async () => {
const inventory = JSON.parse(
await readFile(resolve("website/locales/languages.json"), "utf8"),
) as { code: string; readme: string }[];
const translatedReadmes = inventory
.filter((language) => language.code !== "en")
.map((language) => language.readme);
expect(translatedReadmes).toContain("docs/readme/README_it.md");
expect(translatedReadmes).toHaveLength(18);
const english = shellExamples(await readFile(resolve("README.md"), "utf8"));
for (const path of translatedReadmes)
expect(
@@ -71,4 +59,21 @@ describe("onboarding documentation product facts", () => {
path,
).toEqual(english);
});
it("preserves Persian joiners in links to GitHub-rendered headings", async () => {
const persian = await readFile(resolve("docs/readme/README_fa.md"), "utf8");
// Confirmed against GitHub's rendered README, which retains U+200C.
for (const heading of [
"REA چگونه کار می‌کند",
"چه چیزهایی را می‌توان تحلیل کرد",
"نمونه‌های عملی",
"پرسش‌های متداول",
"به‌روزرسانی REA",
]) {
expect(persian).toContain(`## ${heading}`);
expect(persian).toContain(
`](#${heading.toLowerCase().replaceAll(" ", "-")})`,
);
}
});
});
@@ -5,6 +5,16 @@ import { toolContract } from "../../../src/contracts/toolContracts.js";
import { createEvidence, type Evidence } from "../../../src/domain/evidence.js";
import type { JsonValue } from "../../../src/domain/jsonValue.js";
import { evaluateCodexEvents } from "../../../src/evaluation/CodexAgentEval.js";
import {
agentFixtureAnswerInstructions,
agentFixtureClaims,
} from "../../../src/evaluation/AgentEvaluationFixtures.js";
import { inspectManagedArtifactBytes } from "../../../src/dotnet/ManagedArtifactInspector.js";
import { inspectManagedMembersBytes } from "../../../src/dotnet/ManagedMemberInspector.js";
import {
buildManagedPeFixture,
managedPeFixtureTarget,
} from "../../../src/dotnet/ManagedPe.fixture.js";
import type { FixtureClaimExpectation } from "../../../src/evaluation/KnownAnswerEvaluation.js";
import { ToolResultDelivery } from "../../../src/server/toolResult.js";
@@ -33,7 +43,13 @@ const createObservation = (
const resultFor = (evidence: Evidence) =>
delivery.toEvidenceToolResult(
evidence,
toolContract("analyze_javascript_application"),
toolContract(
evidence.operation === "inspect_managed_artifact"
? "inspect_managed_artifact"
: evidence.operation === "inspect_managed_members"
? "inspect_managed_members"
: "analyze_javascript_application",
),
undefined,
);
const delivered = (
@@ -95,6 +111,336 @@ const assess = (
);
const answer = (claims: readonly unknown[]) => JSON.stringify({ claims });
// Synthetic transcript cases exercise grading, not an SDK-built assembly oracle.
const managedTranscript = () => {
const bytes = buildManagedPeFixture({
typeNamespace: "AgentEval",
typeName: "Program",
methodName: "Main",
});
const target = managedPeFixtureTarget(bytes, "/targets/AgentEval.dll");
const artifact = inspectManagedArtifactBytes(bytes, target);
const members = inspectManagedMembersBytes(bytes, target);
const program = members.types[0];
if (program === undefined) throw new Error("Fixture type missing");
const result = {
...members,
types: [
program,
{
...program,
token: "0x02000002",
name: "Profile",
full_name: "AgentEval.Profile",
},
],
};
const rubric = agentFixtureClaims("managed", {
javascript: "",
javascriptSha256: "",
javascriptShapeLeft: "",
javascriptShapeRight: "",
managed: target.path,
managedSha256: target.sha256,
});
if (rubric === undefined) throw new Error("Managed rubric missing");
const evidenceFor = (
operation: string,
value: JsonValue,
overrides: Record<string, unknown> = {},
) =>
createEvidence(target, provider, {
operation,
parameters: {},
result: value,
confidence: "observed",
authority: "shipped-artifact",
...overrides,
});
const artifactEvidence = evidenceFor("inspect_managed_artifact", artifact);
const memberEvidence = evidenceFor("inspect_managed_members", result);
const textFor = (evidence: Evidence) =>
answer([
{
id: "public_types",
value: ["AgentEval.Profile", "AgentEval.Program"],
evidence_id: evidence.evidence_id,
confidence: "observed",
authority: "shipped-artifact",
},
{
id: "entry_point",
value: { declaring_type: "AgentEval.Program", name: "Main" },
evidence_id: evidence.evidence_id,
confidence: "observed",
authority: "shipped-artifact",
},
]);
const grade = (
events = [delivered(artifactEvidence), delivered(memberEvidence)],
text = textFor(memberEvidence),
) =>
evaluateCodexEvents([...events, final(text)], "inspect_managed_artifact", {
fixtureClaims: rubric,
});
return {
artifact,
result,
artifactEvidence,
memberEvidence,
evidenceFor,
textFor,
grade,
};
};
describe("managed known-answer fixture grading", () => {
it("grades public types independent of row order and resolves the header token", () => {
expect(managedTranscript().grade().factualCorrectness).toBe("passed");
});
it("does not pin compiler-local entry tokens or MVIDs", () => {
const fixture = managedTranscript();
const artifact = structuredClone(fixture.artifact);
const members = structuredClone(fixture.result);
if (
artifact.pe.cli === null ||
artifact.module === null ||
members.module === null ||
members.methods[0] === undefined
)
throw new Error("Fixture identity missing");
artifact.pe.cli.entry_point.value = "0x06000009";
members.methods[0].token = "0x06000009";
const mvid = "11111111-2222-3333-4444-555555555555";
artifact.module.mvid = mvid;
members.module.mvid = mvid;
members.identity_scope.requires_mvid = mvid;
members.types.reverse();
const artifactEvidence = fixture.evidenceFor(
"inspect_managed_artifact",
artifact,
);
const memberEvidence = fixture.evidenceFor(
"inspect_managed_members",
members,
);
expect(
fixture.grade(
[delivered(artifactEvidence), delivered(memberEvidence)],
fixture.textFor(memberEvidence),
).factualCorrectness,
).toBe("passed");
});
it.each([
"wrong-token",
"native-rva",
"mvid",
"null-mvid",
"digest",
"partial",
])("rejects an unsupported artifact entry point: %s", (kind) => {
const fixture = managedTranscript();
const artifact = structuredClone(fixture.artifact);
if (artifact.pe.cli === null || artifact.module === null)
throw new Error("Fixture identity missing");
if (kind === "wrong-token")
artifact.pe.cli.entry_point.value = "0x06000002";
if (kind === "native-rva") artifact.pe.cli.entry_point.kind = "native-rva";
if (kind === "mvid")
artifact.module.mvid = "11111111-2222-3333-4444-555555555555";
if (kind === "null-mvid") artifact.module.mvid = null;
if (kind === "digest") artifact.artifact.sha256 = "b".repeat(64);
if (kind === "partial") artifact.coverage.state = "partial";
const evidence = fixture.evidenceFor("inspect_managed_artifact", artifact);
expect(
fixture.grade([delivered(evidence), delivered(fixture.memberEvidence)])
.factualCorrectness,
).toBe("failed");
});
});
describe("managed inventory scope", () => {
it.each(["selection", "parameters", "both"])(
"rejects selected public-type inventories while retaining entry-point grounding: %s",
(kind) => {
const fixture = managedTranscript();
const typeNames = ["AgentEval.Profile", "AgentEval.Program"];
const members = {
...fixture.result,
selection:
kind === "parameters"
? null
: { type_names: typeNames, unmatched_type_names: [] },
};
const evidence = fixture.evidenceFor("inspect_managed_members", members, {
parameters: kind === "selection" ? {} : { type_names: typeNames },
});
expect(
fixture.grade(
[delivered(fixture.artifactEvidence), delivered(evidence)],
fixture.textFor(evidence),
),
).toMatchObject({
factualCorrectness: "failed",
factualAssessment: {
claims: [
{ id: "public_types", status: "failed" },
{ id: "entry_point", status: "passed" },
],
},
});
},
);
});
describe("managed member grounding", () => {
it.each([
"extra-public-type",
"missing-public-type",
"duplicate-method",
"decoy-main",
"scope-digest",
"scope-mvid",
"null-scope",
"partial",
"unavailable",
])("rejects unsupported member facts: %s", (kind) => {
const fixture = managedTranscript();
const members = structuredClone(fixture.result);
const method = members.methods[0];
const type = members.types[0];
if (method === undefined || type === undefined)
throw new Error("Fixture members missing");
if (kind === "extra-public-type")
members.types.push({
...type,
name: "Extra",
full_name: "AgentEval.Extra",
});
if (kind === "missing-public-type") members.types.pop();
if (kind === "duplicate-method") members.methods.push(method);
if (kind === "decoy-main") {
method.name = "Other";
members.methods.push({ ...method, token: "0x06000002", name: "Main" });
}
if (kind === "scope-digest")
members.identity_scope.requires_artifact_sha256 = "b".repeat(64);
if (kind === "scope-mvid")
members.identity_scope.requires_mvid =
"11111111-2222-3333-4444-555555555555";
if (kind === "null-scope") members.identity_scope.requires_mvid = null;
if (kind === "partial") members.metadata.status = "partial";
if (kind === "unavailable") members.coverage.state = "unavailable";
const evidence = fixture.evidenceFor("inspect_managed_members", members);
expect(
fixture.grade(
[delivered(fixture.artifactEvidence), delivered(evidence)],
fixture.textFor(evidence),
).factualCorrectness,
).toBe("failed");
});
it("rejects distinct authenticated matching artifact deliveries", () => {
const fixture = managedTranscript();
const secondArtifact = fixture.evidenceFor(
"inspect_managed_artifact",
fixture.artifact,
{ parameters: { path: "/targets/AgentEval.dll" } },
);
expect(secondArtifact.evidence_id).not.toBe(
fixture.artifactEvidence.evidence_id,
);
const member = delivered(fixture.memberEvidence);
expect(
fixture.grade([delivered(secondArtifact), member]).factualCorrectness,
).toBe("passed");
expect(
fixture.grade([
delivered(fixture.artifactEvidence),
delivered(secondArtifact),
member,
]).factualCorrectness,
).toBe("failed");
});
it("accepts identical repeated artifact deliveries", () => {
const fixture = managedTranscript();
expect(
fixture.grade([
delivered(fixture.artifactEvidence),
delivered(fixture.artifactEvidence),
delivered(fixture.memberEvidence),
]).factualCorrectness,
).toBe("passed");
});
it("requires companion delivery and rejects conflicting or failed artifact deliveries", () => {
const fixture = managedTranscript();
const member = delivered(fixture.memberEvidence);
expect(fixture.grade([member]).factualCorrectness).toBe("failed");
expect(
fixture.grade([
delivered(fixture.artifactEvidence, { status: "failed" }),
member,
]).factualCorrectness,
).toBe("failed");
const conflicting = {
...fixture.artifactEvidence,
subject:
fixture.memberEvidence.subject === null
? null
: { ...fixture.memberEvidence.subject, local_path: "/other.dll" },
};
expect(
fixture.grade([
delivered(fixture.artifactEvidence),
delivered(conflicting),
member,
]).factualCorrectness,
).toBe("failed");
});
it("binds the digest, rejects fabricated values and cites the supporting operation", () => {
const fixture = managedTranscript();
expect(
fixture.grade(undefined, fixture.textFor(fixture.artifactEvidence))
.factualCorrectness,
).toBe("failed");
expect(
fixture.grade(
undefined,
fixture.textFor(fixture.memberEvidence).replace('"Main"', '"Invented"'),
).factualCorrectness,
).toBe("failed");
const other = createEvidence(
{ path: "/targets/AgentEval.dll", sha256: "b".repeat(64), format: "pe" },
provider,
{
operation: "inspect_managed_members",
parameters: {},
result: fixture.result,
confidence: "observed",
authority: "shipped-artifact",
},
);
expect(
fixture.grade(
[delivered(fixture.artifactEvidence), delivered(other)],
fixture.textFor(other),
).factualCorrectness,
).toBe("failed");
});
it("keeps golden names out of instructions and preserves routing-only scenarios", () => {
const instructions = agentFixtureAnswerInstructions("managed");
expect(instructions).toContain("do not read target files directly");
expect(instructions).not.toMatch(/AgentEval|Profile|Program|Main/u);
expect(agentFixtureAnswerInstructions("native")).toBe("");
});
});
// Removing exact fixture comparison or accepted-source validation must break these regressions.
describe("known-answer fixture grading", () => {
it("passes closed claims against authenticated production MCP Evidence", () => {
+52
View File
@@ -0,0 +1,52 @@
import type { Client } from "@modelcontextprotocol/client";
import { Ajv2020 } from "ajv/dist/2020.js";
import { expect } from "vitest";
import { toolContract } from "../../src/contracts/toolContracts.js";
import { parseEvidence } from "../../src/domain/evidence.js";
import type { BinarySession } from "../../src/application/binary/BinarySession.js";
/** Verify the actual advertised schemas, retaining a private Ajv registry. */
export const advertisedMcpToolSchemas = async (
client: Client,
name: string,
) => {
const advertised = (await client.listTools()).tools.find(
(tool) => tool.name === name,
);
if (advertised?.outputSchema === undefined)
throw new Error(`${name} must publish both schemas`);
const ajv = new Ajv2020({ strict: false, validateFormats: false });
const inputSchema: Record<string, unknown> = advertised.inputSchema;
const outputSchema: Record<string, unknown> = advertised.outputSchema;
expect(ajv.validateSchema(inputSchema)).toBe(true);
expect(ajv.validateSchema(outputSchema)).toBe(true);
return { name: advertised.name, ajv, inputSchema, outputSchema };
};
/** Keep public output, provenance and session retention checks together. */
export const assertRecordedMcpEvidence = (
response: Awaited<ReturnType<Client["callTool"]>>,
name:
| "inspect_wasm_artifact"
| "inspect_binary_layout"
| "inspect_evm_interface",
schemas: Awaited<ReturnType<typeof advertisedMcpToolSchemas>>,
session: BinarySession,
expected: { readonly artifact: { readonly sha256: string } },
confidence: ReturnType<typeof parseEvidence>["confidence"],
) => {
expect(response.isError).not.toBe(true);
expect(
schemas.ajv.validate(schemas.outputSchema, response.structuredContent),
JSON.stringify(schemas.ajv.errors),
).toBe(true);
const parsed = toolContract(name).outputSchema.parse(
response.structuredContent,
);
const evidence = parseEvidence(parsed);
expect(evidence.confidence).toBe(confidence);
expect(evidence.subject?.digest.sha256).toBe(expected.artifact.sha256);
expect(parsed.normalized_result).toEqual(expected);
expect(session.evidenceById(evidence.evidence_id)).toEqual(evidence);
return evidence;
};
+9
View File
@@ -0,0 +1,9 @@
.class public Lfixture/Caller;
.super Ljava/lang/Object;
.method public static twice()V
.registers 0
invoke-static {}, Lfixture/Target;->mark()V
invoke-static {}, Lfixture/Target;->mark()V
return-void
.end method
+7
View File
@@ -0,0 +1,7 @@
.class public Lfixture/Target;
.super Ljava/lang/Object;
.method public static mark()V
.registers 0
return-void
.end method
+12 -1
View File
@@ -32,18 +32,21 @@ const methods = [
{
name: "onCreate",
signature: "onCreate(Bundle): void",
dex_descriptor: "(Landroid/os/Bundle;)V",
is_constructor: false,
def_pos: 0,
},
{
name: "choose",
signature: "choose(String): void",
dex_descriptor: "(Ljava/lang/String;)V",
is_constructor: false,
def_pos: 0,
},
{
name: "choose",
signature: "choose(int): void",
dex_descriptor: "(I)V",
is_constructor: false,
def_pos: 0,
},
@@ -139,10 +142,18 @@ register("get_method_body", ({ method_name, overload_index }) =>
}),
);
for (const name of ["get_xrefs_to_class", "get_xrefs_to_method"])
register(name, ({ class_name, method_name }) =>
register(name, ({ class_name, method_name, overload_index }) =>
json({
target:
method_name === undefined ? class_name : `${class_name}.${method_name}`,
...(method_name === undefined
? {}
: {
overload_index,
reported_signature: methods.filter(
(method) => method.name === method_name,
)[overload_index]?.signature,
}),
total: mode === "partial-xrefs" ? 2 : 1,
count: 1,
resolve_line: false,
+27
View File
@@ -0,0 +1,27 @@
/** Serialize a header-only ASAR, including Chromium pickle alignment. */
export const asarHeaderBytes = (json: string): Buffer => {
const text = Buffer.from(json);
const headerSize = 8 + Math.ceil(text.length / 4) * 4;
const bytes = Buffer.alloc(8 + headerSize);
bytes.writeUInt32LE(4, 0);
bytes.writeUInt32LE(headerSize, 4);
bytes.writeUInt32LE(headerSize - 4, 8);
bytes.writeUInt32LE(text.length, 12);
text.copy(bytes, 16);
return bytes;
};
/** Nested metadata without a recursive fixture builder or real directories. */
export const nestedAsarHeader = (
depth: number,
name: string,
leaves: number,
): Buffer => {
let json = `{"files":{${Array.from(
{ length: leaves },
(_, index) => `"f${index}":{"size":0,"offset":"0"}`,
).join(",")}}}`;
for (let index = 0; index < depth; index++)
json = `{"files":{${JSON.stringify(name)}:${json}}}`;
return asarHeaderBytes(json);
};
+66 -49
View File
@@ -1,17 +1,41 @@
import type { RecordedCrash } from "../../../src/domain/native/recordedCrash.js";
import {
RECORDED_LINUX_CORE_PROFILES,
type RecordedCrash,
type RecordedCrashArchitecture,
} from "../../../src/domain/native/recordedCrash.js";
// One note is a 12-byte header, the "CORE\0" owner padded to 8 bytes, then the
// descriptor. The first note starts at the end of the owned header and phdr.
const NOTE_START = 0x80;
const OWNER_START = NOTE_START + 12;
const DESCRIPTOR_START = OWNER_START + 8;
const NOTE_HEADER_BYTES = 20;
const hex = (value: number): string => `0x${value.toString(16)}`;
/** Source-owned observation seam; these bytes do not simulate a verified ELF engine. */
export const recordedCrashFixture = (
path = "/artifacts/source-owned.core",
architecture: RecordedCrashArchitecture = "x86_64-little-endian",
): RecordedCrash => {
const descriptor = Buffer.alloc(336);
const profile = RECORDED_LINUX_CORE_PROFILES[architecture];
const noteBytes = NOTE_HEADER_BYTES + profile.prstatusBytes;
const descriptor = Buffer.alloc(profile.prstatusBytes);
descriptor.writeInt32LE(123, 32);
descriptor.writeInt16LE(11, 12);
descriptor.writeBigUInt64LE(0x1122334455667788n, 224);
// One distinguishable register carries a full uint64 so retention is asserted.
descriptor.writeBigUInt64LE(
0x1122334455667788n,
profile.registerOffset + 14 * profile.registerBytes,
);
return {
artifact: { path, sha256: "a".repeat(64), bytes: 512 },
artifact: {
path,
sha256: "a".repeat(64),
bytes: Math.max(512, DESCRIPTOR_START + profile.prstatusBytes),
},
format: "elf-core",
architecture: "x86_64-little-endian",
architecture,
target_execution: "not-performed",
live_process_identity: "unknown",
segments: [
@@ -20,7 +44,7 @@ export const recordedCrashFixture = (
type: "PT_NOTE",
header_location: { offset: "0x40", bytes: "0x38" },
offset: "0x80",
file_size: "0x164",
file_size: hex(noteBytes),
memory_size: "0x0",
virtual_address: "0x0",
physical_address: "0x0",
@@ -34,11 +58,14 @@ export const recordedCrashFixture = (
index: 0,
segment_index: 0,
type: "NT_PRSTATUS",
location: { offset: "0x80", bytes: "0x164" },
owner_location: { offset: "0x8c", bytes: "0x5" },
location: { offset: hex(NOTE_START), bytes: hex(noteBytes) },
owner_location: { offset: hex(OWNER_START), bytes: "0x5" },
owner_bytes_base64: Buffer.from("CORE\0").toString("base64"),
owner_display: "CORE",
descriptor_location: { offset: "0x94", bytes: "0x150" },
descriptor_location: {
offset: hex(DESCRIPTOR_START),
bytes: hex(profile.prstatusBytes),
},
descriptor_bytes_base64: descriptor.toString("base64"),
},
],
@@ -48,40 +75,16 @@ export const recordedCrashFixture = (
note_index: 0,
historical_pid: 123,
recorded_current_signal: 11,
registers: [
"r15",
"r14",
"r13",
"r12",
"rbp",
"rbx",
"r11",
"r10",
"r9",
"r8",
"rax",
"rcx",
"rdx",
"rsi",
"rdi",
"orig_rax",
"rip",
"cs",
"eflags",
"rsp",
"ss",
"fs_base",
"gs_base",
"ds",
"es",
"fs",
"gs",
].map((name, index) => ({
registers: profile.registers.map((name, index) => ({
name,
value: name === "rdi" ? "0x1122334455667788" : "0x0",
value: index === 14 ? "0x1122334455667788" : "0x0",
location: {
offset: `0x${(148 + 112 + index * 8).toString(16)}`,
bytes: "0x8",
offset: hex(
DESCRIPTOR_START +
profile.registerOffset +
index * profile.registerBytes,
),
bytes: hex(profile.registerBytes),
},
})),
},
@@ -100,27 +103,38 @@ export const recordedCrashFixture = (
};
/** Source-owned SIGSEGV descriptor for scalar/source binding regressions. */
export const recordedCrashSignalFixture = (path?: string): RecordedCrash => {
const value = recordedCrashFixture(path);
export const recordedCrashSignalFixture = (
path?: string,
architecture: RecordedCrashArchitecture = "x86_64-little-endian",
): RecordedCrash => {
const value = recordedCrashFixture(path, architecture);
const profile = RECORDED_LINUX_CORE_PROFILES[architecture];
const descriptor = Buffer.alloc(32);
descriptor.writeInt32LE(11, 0);
descriptor.writeInt32LE(-2, 4);
descriptor.writeInt32LE(1, 8);
descriptor.writeBigUInt64LE(0x1122334455667788n, 16);
value.artifact.bytes = 640;
const prstatusNoteBytes = NOTE_HEADER_BYTES + profile.prstatusBytes;
const signalNoteStart = NOTE_START + prstatusNoteBytes;
const signalDescriptorStart = signalNoteStart + NOTE_HEADER_BYTES;
const signalNoteBytes = NOTE_HEADER_BYTES + 32;
value.artifact.bytes = Math.max(640, signalDescriptorStart + 32);
value.segments = value.segments.map((segment) => ({
...segment,
file_size: "0x198",
file_size: hex(prstatusNoteBytes + signalNoteBytes),
}));
value.notes.push({
index: 1,
segment_index: 0,
type: "NT_SIGINFO",
location: { offset: "0x1e4", bytes: "0x34" },
owner_location: { offset: "0x1f0", bytes: "0x5" },
location: { offset: hex(signalNoteStart), bytes: hex(signalNoteBytes) },
owner_location: { offset: hex(signalNoteStart + 12), bytes: "0x5" },
owner_bytes_base64: Buffer.from("CORE\0").toString("base64"),
owner_display: "CORE",
descriptor_location: { offset: "0x1f8", bytes: "0x20" },
descriptor_location: {
offset: hex(signalDescriptorStart),
bytes: "0x20",
},
descriptor_bytes_base64: descriptor.toString("base64"),
});
value.signals.push({
@@ -141,7 +155,10 @@ export const recordedCrashFixtureBytes = (value: RecordedCrash): Buffer => {
bytes.set([0x7f, 0x45, 0x4c, 0x46, 2, 1, 1]);
bytes.writeUInt32LE(1, 20);
bytes.writeUInt16LE(4, 16);
bytes.writeUInt16LE(62, 18);
bytes.writeUInt16LE(
RECORDED_LINUX_CORE_PROFILES[value.architecture].machine,
18,
);
bytes.writeBigUInt64LE(64n, 32);
bytes.writeUInt16LE(56, 54);
bytes.writeUInt16LE(value.segments.length, 56);
+1
View File
@@ -27,6 +27,7 @@ export const CLIENT_LOCATION_ENVIRONMENT = [
"GEMINI_CLI_SYSTEM_DEFAULTS_PATH",
"HERMES_DATA_DIR_SUFFIX",
"HERMES_HOME",
"KIMI_CODE_HOME",
"LOCALAPPDATA",
"OMP_PROFILE",
"OPENCODE_CONFIG",
+65
View File
@@ -0,0 +1,65 @@
import type { FileHandle } from "node:fs/promises";
import { expect } from "vitest";
import type { Result } from "../../src/domain/result.js";
import type { AnalysisError } from "../../src/domain/analysisErrorBase.js";
export interface DescriptorCloseFailure {
path: string;
denied: boolean;
handles: FileHandle[];
}
/** Public failure attribution and a still-live OS handle establish retained ownership. */
export const expectRetainedDescriptor = (
result: Result<unknown, AnalysisError>,
cleanup: DescriptorCloseFailure,
operation: string,
): void => {
expect(result).toMatchObject({
ok: false,
error: {
cleanupIncomplete: true,
cleanupResources: [cleanup.path],
operation,
},
});
expect(cleanup.handles).toHaveLength(1);
expect(cleanup.handles[0]?.fd).toBeGreaterThanOrEqual(0);
};
export const descriptorFailurePath = (
resource: string,
paths: Readonly<Record<"capture" | "tool" | "limiter", string>>,
): string => {
switch (resource) {
case "capture":
case "tool":
case "limiter":
return paths[resource];
case "kernel":
return "/proc/self/limits";
default:
throw new Error(`Unknown descriptor failure resource: ${resource}`);
}
};
/** Inject a retryable close failure while retaining the actual OS descriptor. */
export const withDescriptorCloseFailure = (
filesystem: typeof import("node:fs/promises"),
cleanup: DescriptorCloseFailure,
) => ({
...filesystem,
open: async (...args: Parameters<typeof filesystem.open>) => {
const handle = await filesystem.open(...args);
if (args[0] === cleanup.path) {
cleanup.handles.push(handle);
const close = handle.close.bind(handle);
handle.close = async () => {
if (cleanup.denied && handle.fd >= 0)
throw new Error("Descriptor close deliberately denied");
await close();
};
}
return handle;
},
});
+1
View File
@@ -18,6 +18,7 @@ export const evmInterfaceFixture = (
kind: "unknown",
deployment_authenticity: "unknown",
hardfork: "unknown",
metadata: null,
},
evidence_kind: "inferred",
functions: [],
+3
View File
@@ -28,6 +28,7 @@ export const startFakeCdpBrowser = async (
referer: string | undefined;
}[] = [];
const sockets = new Set<WebSocket>();
let openedConnections = 0;
let frameTreeReads = 0;
let port = 0;
const http = createServer((request, response) => {
@@ -85,6 +86,7 @@ export const startFakeCdpBrowser = async (
});
webSockets.on("connection", (socket: WebSocket, request: IncomingMessage) => {
const directPage = request.url?.startsWith("/devtools/page/") === true;
openedConnections += 1;
sockets.add(socket);
socket.on("close", () => sockets.delete(socket));
socket.on("message", (raw) => {
@@ -142,6 +144,7 @@ export const startFakeCdpBrowser = async (
allowedOrigin: endpoint,
commands,
activeConnectionCount: () => sockets.size,
openedConnectionCount: () => openedConnections,
emitRawMessage(message) {
for (const socket of sockets) socket.send(message);
},
+11
View File
@@ -24,6 +24,8 @@ export const resultFor = (
return responseBodyResult(options);
case "Page.captureScreenshot":
return screenshotResult();
case "Browser.getVersion":
return browserVersionResult();
case "Target.getTargets":
return { targetInfos: targets(port, options).map(endpointTargetToInfo) };
case "Storage.getUsageAndQuota":
@@ -66,6 +68,15 @@ export const resultFor = (
}
};
/** Chrome's browser-socket reply shape, mirroring the fake's `/json/version`. */
const browserVersionResult = () => ({
protocolVersion: "1.3",
product: "FakeChrome/1.0",
revision: "@fake-revision",
userAgent: "FakeChrome",
jsVersion: "13.0",
});
const attachToTargetResult = (options: FakeOptions) => ({
sessionId: options.invalidAttachedSession ? "" : "session-1",
});
+2
View File
@@ -13,6 +13,8 @@ export interface FakeCdpBrowser {
readonly commands: readonly FakeCdpCommand[];
/** Actual WebSocket connections still held by the wire fixture. */
activeConnectionCount(): number;
/** WebSocket upgrades accepted over the fixture's lifetime. */
openedConnectionCount(): number;
/** Deliver actual malformed wire data without bypassing the production transport parser. */
emitRawMessage(message: string): void;
emitEvent(event: {
+37 -2
View File
@@ -121,7 +121,7 @@ const handleRequest = (socket, server, request, state) => {
? [stringItem("0x2000", "fixture 雪🦊")]
: request.method === "list_strings" && state.mode === "oversized_result"
? [stringItem("0x2000", "x".repeat(64 * 1024 * 1024 + 1))]
: inventoryResultFor(request.method);
: inventoryResultFor(request.method, request.params);
if (inventory !== undefined) {
const response = `${JSON.stringify({ id: request.id, ok: true, result: inventory })}\n`;
if (state.mode === "fragmented") {
@@ -228,6 +228,7 @@ const sessionInfo = ({
"shutdown",
"inspect_native_load_image",
"read_bytes",
"search_bytes",
"address_to_file_offset",
"address_name",
"list_documents",
@@ -273,8 +274,42 @@ const sessionInfo = ({
},
});
const inventoryResultFor = (method) => {
const inventoryResultFor = (method, params) => {
switch (method) {
case "search_bytes": {
const continued = params.cursor === "fixture-second-page";
const paged = params.limit === 1 && !continued;
const addresses = continued
? ["0x1005"]
: paged
? ["0x1000"]
: ["0x1000", "0x1005"];
return {
matches: addresses.map((address) => ({
address,
segment: ".text",
file_offset: address === "0x1000" ? 0 : 5,
procedure: { address: "0x1000", name: "fixture_main" },
})),
searched: {
blocks: [
{
segment: ".text",
start: continued ? "0x1005" : "0x1000",
end: "0x1009",
bytes: continued ? 5 : 10,
},
],
bytes: continued ? 5 : 10,
skipped_uninitialized: [
{ segment: ".bss", start: "0x2000", end: "0x200f", bytes: 16 },
],
skipped_not_loaded: [],
},
complete: !paged,
next_cursor: paged ? "fixture-second-page" : null,
};
}
case "address_name":
return "fixture_main";
case "list_documents":
+83
View File
@@ -0,0 +1,83 @@
import { startFakeCdpBrowser, type FakeCdpBrowser } from "./fakeCdpBrowser.js";
export type HttpRequestObservation = {
readonly requestId: string;
readonly path?: string;
readonly status?: number;
readonly mimeType?: string;
readonly encodedDataLength?: number;
};
type CdpEvent = Parameters<FakeCdpBrowser["emitEvent"]>[0];
const requestEvents = (
observation: HttpRequestObservation,
origin: string,
sessionId: string | undefined,
): CdpEvent[] => {
const session = sessionId === undefined ? {} : { sessionId };
const url = `${origin}${observation.path ?? "/api/items"}`;
const { requestId, status, mimeType, encodedDataLength } = observation;
const events: CdpEvent[] = [
{
...session,
method: "Network.requestWillBeSent",
params: {
requestId,
timestamp: 1,
type: "Fetch",
request: { url, method: "GET", headers: {} },
initiator: { type: "other" },
},
},
];
if (status !== undefined || mimeType !== undefined)
events.push({
...session,
method: "Network.responseReceived",
params: {
requestId,
timestamp: 2,
type: "Fetch",
response: {
url,
headers: {},
...(status === undefined ? {} : { status }),
...(mimeType === undefined ? {} : { mimeType }),
},
},
});
if (encodedDataLength !== undefined)
events.push({
...session,
method: "Network.loadingFinished",
params: { requestId, timestamp: 3, encodedDataLength },
});
return events;
};
/** Capture changing response coverage through the real CDP wire boundary. */
export const startHttpComparisonBrowser = async (
initial: readonly HttpRequestObservation[],
): Promise<{
readonly browser: FakeCdpBrowser;
setRequests(next: readonly HttpRequestObservation[]): void;
}> => {
let requests = initial;
const browser = await startFakeCdpBrowser({
eventsBeforeReply: ["Network.enable"],
commandEvents(command, origin) {
return command.method === "Network.enable"
? requests.flatMap((request) =>
requestEvents(request, origin, command.sessionId),
)
: undefined;
},
});
return {
browser,
setRequests(next) {
requests = next;
},
};
};
+4 -1
View File
@@ -1,5 +1,8 @@
/** Build a valid minimal JVM class file, optionally with an unused UTF-8 constant. */
export const javaClassFile = (extraUtf8Bytes = 0, fatCpuLookalike = false): Buffer => {
export const javaClassFile = (
extraUtf8Bytes = 0,
fatCpuLookalike = false,
): Buffer => {
const utf8 = (value: string): Buffer => {
const bytes = Buffer.from(value, "utf8");
const entry = Buffer.alloc(3 + bytes.length);

Some files were not shown because too many files have changed in this diff Show More