Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

perf(test): reuse one compiled CLI for the remaining hot command files (#5784)

morluto0ad42752d3e9
diff --git a/docs/testing.md b/docs/testing.md
index 746706905..59ec5645f 100644
--- a/docs/testing.md
+++ b/docs/testing.md
@@ -163,8 +163,10 @@ the worker reports a resident set of at least 1 GiB. A fatal worker failure
or a broken session frame retires that process and waits for it to exit before
the next command starts.
`cli.run` still starts a fresh process, including stdin and explicit deadlines.
-File teardown stops the shared CLI process, including startup still in flight,
-and does not open another after close.
+`runFileCli` uses the file session for commands that have neither. Go binary,
+IL2CPP metadata, and dylib-resolution commands use that session. File teardown
+stops the shared CLI process, including startup still in flight, and does not
+open another after close.
`exportedFunctionBindingsCli.test.ts` keeps its own compiled CLI parity
process for the file and still compares that process's JSON command results
with the MCP session. A command that does not respond is rejected. That
@@ -172,6 +174,9 @@ process stays owned until it exits, and another process does not start while
it remains. A process that stays running after SIGKILL remains owned so a
later close can retry. Closing the session reports a failure when the process
does not exit after stdin closes, once the child has been stopped.
+`applicationWorkflowCli.test.ts` keeps one compiled CLI process for its JSON
+commands, including a non-zero status whose stdout is still a JSON document,
+and closes that process after the file.
The Apple artifact verifier step shares a job-temporary bytecode cache among
its fresh CLI/MCP children via `REA_ARTIFACT_NODE_COMPILE_CACHE`. The artifact
diff --git a/tests/acceptance/applications/applicationWorkflowCli.test.ts b/tests/acceptance/applications/applicationWorkflowCli.test.ts
index 7a96b08fc..3ecb56788 100644
--- a/tests/acceptance/applications/applicationWorkflowCli.test.ts
+++ b/tests/acceptance/applications/applicationWorkflowCli.test.ts
@@ -12,6 +12,11 @@ import { afterAll, describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { writeElectronApiAliasApplication } from "../../fixtures/electronApiAliasApplication.js";
+import {
+ createCliCommandSession,
+ type CliCommandSession,
+} from "../../support/cli/cliCommandSession.js";
+import { definedCliEnvironment } from "../../support/cli/cliFixture.js";
import {
JAVASCRIPT_FEATURE_TRACE_EXAMPLE,
@@ -34,6 +39,7 @@ import { javaScriptSemanticTraceResultSchema } from "../../../src/domain/javascr
import type { JavaScriptSemanticQueryInput } from "../../../src/domain/javascript/javascriptSemanticQuerySchemas.js";
const execute = promisify(execFile);
+let cliSession: CliCommandSession | undefined;
const requireFixture = createRequire(import.meta.url);
// Analysis requests are stateless; retain one compiled server and its SDK
// validators while every scenario still owns a separate application directory.
@@ -57,9 +63,13 @@ const connectedMcpClient = async (): Promise<Client> => {
afterAll(async () => {
try {
- await mcpClient.close();
+ try {
+ await mcpClient.close();
+ } finally {
+ await mcpTransport.close();
+ }
} finally {
- await mcpTransport.close();
+ await cliSession?.close();
}
});
const WEBPACK_FACTORY_BODY = `
@@ -1809,27 +1819,19 @@ const runCli = async (
arguments_: readonly string[],
environment: Readonly<Record<string, string>> = {},
): Promise<unknown> => {
- try {
- const { stdout } = await execute(
- process.execPath,
- ["scripts/rea.mjs", ...arguments_],
- {
- cwd: process.cwd(),
- env: { ...process.env, ...environment },
- maxBuffer: 16 * 1_024 * 1_024,
- },
- );
- return JSON.parse(stdout);
- } catch (cause: unknown) {
- if (
- typeof cause === "object" &&
- cause !== null &&
- "stdout" in cause &&
- typeof cause.stdout === "string"
- )
- return JSON.parse(cause.stdout);
- throw cause;
- }
+ cliSession ??= createCliCommandSession();
+ const result = await cliSession.run({
+ arguments: arguments_,
+ cwd: process.cwd(),
+ environment: definedCliEnvironment({ ...process.env, ...environment }),
+ });
+ const stdout = result.stdout.trim();
+ if (stdout.length > 0) return JSON.parse(stdout) as unknown;
+ throw new Error(
+ result.stderr.trim().length > 0
+ ? result.stderr
+ : `Compiled CLI command failed with status ${String(result.exitCode)}`,
+ );
};
/**
diff --git a/tests/acceptance/goBinaryCli.test.ts b/tests/acceptance/goBinaryCli.test.ts
index a17c09bab..a5dfdf0bb 100644
--- a/tests/acceptance/goBinaryCli.test.ts
+++ b/tests/acceptance/goBinaryCli.test.ts
@@ -16,7 +16,7 @@ import {
GO_MODULE_TEXT,
} from "../fixtures/go/image.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
-import { cliTest } from "../support/cli/cliFixture.js";
+import { cliTest, runFileCli } from "../support/cli/cliFixture.js";
import { parseEvidence } from "../../src/domain/evidence.js";
import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js";
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
@@ -28,12 +28,12 @@ cliTest(
const path = join(root, "application.elf");
const output = join(root, "source.json");
await writeFile(path, createGoBinaryFixture().bytes);
- const complete = await cli.run({
+ const complete = await runFileCli(cli, {
arguments: ["inspect-go-binary", path, "--json"],
});
expect(complete.exitCode, complete.stderr).toBe(0);
const full = evidenceResultOf(goBinarySchema).parse(complete.json);
- const summary = await cli.run({
+ const summary = await runFileCli(cli, {
arguments: [
"inspect-go-binary",
path,
@@ -62,7 +62,7 @@ cliTest(
.map((record) => parseEvidence(record))
.find((record) => record.evidence_id === full.evidence_id),
).toEqual(full);
- const refused = await cli.run({
+ const refused = await runFileCli(cli, {
arguments: [
"inspect-go-binary",
join(root, "missing"),
@@ -86,7 +86,7 @@ cliTest(
const root = await createTestTempDirectory("rea-go-overlap-");
const path = join(root, "overlapping.elf");
await writeFile(path, createGoOverlappingElfFixture());
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-go-binary", path, "--json"],
});
expect(result.exitCode).toBe(1);
@@ -120,7 +120,7 @@ cliTest(
const fixture = createGoSplitElfFixture({ sectionless });
const path = join(root, sectionless ? "sectionless.elf" : "original.elf");
await writeFile(path, fixture.bytes);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-go-binary", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
@@ -156,7 +156,7 @@ for (const bits of [32, 64] as const) {
] as const) {
const path = join(root, `${label}.exe`);
await writeFile(path, bytes);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-go-binary", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
@@ -208,7 +208,7 @@ cliTest(
path,
machoImageWithUnknownCommand(fixture.wide, fixture.size),
);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-go-binary", path, "--json"],
});
const response = await call("inspect_go_binary", { path });
@@ -248,7 +248,7 @@ for (const expectedErrno of ["ELOOP", "ENAMETOOLONG"] as const) {
root,
expectedErrno,
);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-go-binary", path, "--json"],
});
expect(result.exitCode).toBe(1);
@@ -279,7 +279,7 @@ cliTest(
const path = join(root, "byte-strings.elf");
const fixture = createGoBinaryByteStringFixture();
await writeFile(path, fixture.bytes);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-go-binary", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
@@ -297,7 +297,7 @@ for (const fixture of createGoBinaryDiagnosticFixtures()) {
const root = await createTestTempDirectory("rea-go-cli-mz-");
const path = join(root, `${fixture.name}.exe`);
await writeFile(path, fixture.bytes);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-go-binary", path, "--json"],
});
expect(result.exitCode).toBe(1);
@@ -318,7 +318,7 @@ cliTest(
const root = await createTestTempDirectory("rea-go-cli-");
const path = join(root, "text.bin");
await writeFile(path, "This is not an executable container.");
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-go-binary", path, "--json"],
});
expect(result.exitCode).toBe(1);
@@ -338,7 +338,7 @@ for (const format of ["elf", "pe", "macho"] as const) {
const fixture = createGoBinaryFixture({ format, encoding });
const path = join(root, "application.bin");
await writeFile(path, fixture.bytes);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-go-binary", "application.bin", "--json"],
cwd: root,
});
@@ -409,7 +409,7 @@ cliTest("preserves MIPS identity in Go CLI Evidence", async ({ cli }) => {
fixture.bytes.writeUInt16LE(8, 18); // ELF EM_MIPS.
const path = join(root, "application.elf");
await writeFile(path, fixture.bytes);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-go-binary", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
@@ -428,7 +428,7 @@ cliTest(
const root = await createTestTempDirectory("rea-go-cli-");
const path = join(root, "metadata-free.elf");
await writeFile(path, createGoBinaryFixture({ omitBuildInfo: true }).bytes);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-go-binary", path, "--json"],
});
expect(result.exitCode).toBe(0);
@@ -452,7 +452,7 @@ cliTest(
const root = await createTestTempDirectory("rea-go-gopath-cli-");
const path = join(root, "application.elf");
await writeFile(path, createGoBinaryFixture({ moduleText }).bytes);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-go-binary", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
diff --git a/tests/acceptance/il2cppMetadataCli.test.ts b/tests/acceptance/il2cppMetadataCli.test.ts
index 506a4471c..95ff5bf73 100644
--- a/tests/acceptance/il2cppMetadataCli.test.ts
+++ b/tests/acceptance/il2cppMetadataCli.test.ts
@@ -40,7 +40,7 @@ import {
import { connectLocalToolsMcp } from "../fixtures/localToolsMcp.js";
import { parseMcpToolError } from "../fixtures/mcpToolError.js";
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
-import { cliTest } from "../support/cli/cliFixture.js";
+import { cliTest, runFileCli } from "../support/cli/cliFixture.js";
const parseCliMetadata = (value: unknown) =>
il2cppMetadataSchema.parse(
@@ -63,7 +63,7 @@ cliTest(
});
const path = join(root, `valid-${count}.dat`);
await writeFile(path, bytes);
- const output = await cli.run({
+ const output = await runFileCli(cli, {
arguments: ["inspect-il2cpp-metadata", path, "--json"],
});
expect(output.exitCode, output.stderr).toBe(0);
@@ -96,7 +96,7 @@ cliTest(
);
const path = join(root, `invalid-${declaredCount}-${declaredBytes}.dat`);
await writeFile(path, fixture.bytes);
- const output = await cli.run({
+ const output = await runFileCli(cli, {
arguments: ["inspect-il2cpp-metadata", path, "--json"],
});
expect(output.exitCode).toBe(1);
@@ -136,7 +136,7 @@ cliTest(
propertiesAndEvents: true,
});
await writeFile(path, bytes);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-il2cpp-metadata", path, "--json"],
});
expect(result.exitCode, result.stderr).toBe(0);
@@ -285,7 +285,7 @@ cliTest(
] as const) {
const path = join(root, name);
await writeFile(path, bytes);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-il2cpp-metadata", path, "--json"],
});
expect(result.exitCode).toBe(1);
@@ -335,7 +335,7 @@ cliTest(
},
},
};
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["inspect-il2cpp-metadata", path, "--json"],
});
expect(result.exitCode).toBe(1);
@@ -643,7 +643,7 @@ cliTest(
const root = await createTestTempDirectory("rea-il2cpp-cli-export-");
const path = join(root, "global-metadata.dat");
const bundle = join(root, "evidence.json");
- const refused = await cli.run({
+ const refused = await runFileCli(cli, {
arguments: [
"inspect-il2cpp-metadata",
join(root, "must-not-be-read"),
@@ -667,14 +667,14 @@ cliTest(
bundle,
"--json",
];
- const result = await cli.run({ arguments: args });
+ const result = await runFileCli(cli, { arguments: args });
expect(result.exitCode, result.stderr).toBe(0);
expect(result.json).toMatchObject({
evidence_bundle: { path: bundle, bytes: expect.any(Number) },
summary: { normalized_result: { kind: "summary" } },
});
const before = await readFile(bundle);
- const overwrite = await cli.run({ arguments: args });
+ const overwrite = await runFileCli(cli, { arguments: args });
expect(overwrite.exitCode).toBe(1);
expect(await readFile(bundle)).toEqual(before);
const saved = await readEvidenceBundle(bundle);
@@ -700,7 +700,7 @@ cliTest(
},
}),
);
- const selected = await cli.run({
+ const selected = await runFileCli(cli, {
arguments: ["inspect-analysis-view", query, "--json"],
});
expect(selected.exitCode, selected.stderr).toBe(0);
@@ -727,7 +727,7 @@ cliTest.skipIf(process.platform === "win32")(
const decoy = join(root, "evidence.json");
await writeFile(decoy, "unrelated caller data");
const selected = `${root}/link/../evidence.json`;
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: [
"inspect-il2cpp-metadata",
path,
@@ -746,7 +746,7 @@ cliTest.skipIf(process.platform === "win32")(
expect(
JSON.parse(await readFile(join(actual, "evidence.json"), "utf8")),
).toEqual(saved.value);
- const read = await cli.run({
+ const read = await runFileCli(cli, {
arguments: [
"inspect-evidence-bundle",
JSON.stringify({ path: selected, detail: "summary" }),
@@ -781,7 +781,7 @@ cliTest(
rva: `0x${(address - FIXTURE_BINARY_BASE).toString(16)}`,
},
);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: [
"inspect-il2cpp-metadata",
path,
@@ -868,7 +868,7 @@ cliTest(
{ machine: 62 },
),
);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: [
"inspect-il2cpp-metadata",
path,
@@ -900,7 +900,7 @@ cliTest(
await writeFile(path, createIl2cppMetadataFixture().bytes);
await writeFile(binaryPath, createElfFixture(createIl2cppBinaryFixture()));
const run = (output: string, extra: readonly string[] = []) =>
- cli.run({
+ runFileCli(cli, {
arguments: [
"inspect-il2cpp-metadata",
path,
@@ -955,7 +955,7 @@ cliTest(
});
expect(await readFile(seeds, "utf8")).toBe("kept\n");
- const unlinked = await cli.run({
+ const unlinked = await runFileCli(cli, {
arguments: [
"inspect-il2cpp-metadata",
path,
diff --git a/tests/boundary/cli/dylibResolutionCli.test.ts b/tests/boundary/cli/dylibResolutionCli.test.ts
index 128c51d16..80ca7260e 100644
--- a/tests/boundary/cli/dylibResolutionCli.test.ts
+++ b/tests/boundary/cli/dylibResolutionCli.test.ts
@@ -17,7 +17,11 @@ import {
import { dylibResolutionResultSchema } from "../../../src/domain/apple/dylibResolution.js";
import { parseEvidence } from "../../../src/domain/evidence.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
-import { cliTest, type TestCli } from "../../support/cli/cliFixture.js";
+import {
+ cliTest,
+ runFileCli,
+ type TestCli,
+} from "../../support/cli/cliFixture.js";
const ENVIRONMENT = { REA_LOG_LEVEL: "silent", REA_ANALYSIS_PROVIDER: "auto" };
@@ -46,7 +50,7 @@ const expectRootInputError = async (
readonly message: string;
},
): Promise<void> => {
- const rejected = await cli.run({
+ const rejected = await runFileCli(cli, {
arguments: ["trace-dylib-resolution", app, "--root", root, "--json"],
environment: ENVIRONMENT,
});
@@ -127,7 +131,7 @@ cliTest(
],
}),
});
- const output = await cli.run({
+ const output = await runFileCli(cli, {
arguments: ["trace-dylib-resolution", app, "--json"],
environment: ENVIRONMENT,
});
@@ -160,7 +164,7 @@ cliTest(
describe("trace-dylib-resolution CLI", () => {
cliTest("traces every executable root of an app bundle", async ({ cli }) => {
const app = await fixtureApp();
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["trace-dylib-resolution", app, "--json"],
environment: ENVIRONMENT,
});
@@ -211,7 +215,7 @@ describe("trace-dylib-resolution CLI", () => {
"narrows roots and architecture, and rejects escaping roots",
async ({ cli }) => {
const app = await fixtureApp();
- const narrowed = await cli.run({
+ const narrowed = await runFileCli(cli, {
arguments: [
"trace-dylib-resolution",
app,
@@ -267,7 +271,7 @@ describe("trace-dylib-resolution CLI", () => {
}),
"unrelated.txt": "not traversed",
});
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: [
"trace-dylib-resolution",
join(directory, "tool"),
@@ -292,7 +296,7 @@ describe("trace-dylib-resolution CLI", () => {
async ({ cli }) => {
const app = await fixtureApp();
await withBrokenHelper(app);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["trace-dylib-resolution", app, "--json"],
environment: ENVIRONMENT,
});
@@ -343,7 +347,7 @@ cliTest(
});
}
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["trace-dylib-resolution", app, "--json"],
environment: ENVIRONMENT,
});
@@ -400,7 +404,7 @@ cliTest(
commands: [buildVersionCommand(1)],
}),
});
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["trace-dylib-resolution", join(directory, "tool"), "--json"],
environment: ENVIRONMENT,
});
@@ -450,7 +454,7 @@ describe.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
});
await chmod(join(directory, "libsecret.dylib"), 0o000);
try {
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: [
"trace-dylib-resolution",
join(directory, "tool"),
@@ -498,7 +502,7 @@ cliTest(
],
}),
});
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: [
"trace-dylib-resolution",
app,
@@ -559,7 +563,7 @@ cliTest(
{ cpu: CPU.x86_64, bytes: machoImage({ cpu: CPU.x86_64 }) },
]),
});
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: [
"trace-dylib-resolution",
app,
@@ -609,7 +613,7 @@ cliTest(
commands: [buildVersionCommand(1)],
}),
});
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: [
"trace-dylib-resolution",
app,
@@ -663,7 +667,7 @@ cliTest(
],
}),
);
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["trace-dylib-resolution", path, "--json"],
environment: ENVIRONMENT,
});
@@ -711,7 +715,7 @@ describe("trace-dylib-resolution root enumeration", () => {
"classifies every Mach-O among more files than one batch of magic reads",
async ({ cli }) => {
const app = await crowdedApp();
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["trace-dylib-resolution", app, "--json"],
environment: ENVIRONMENT,
});
@@ -742,7 +746,7 @@ describe("trace-dylib-resolution root enumeration", () => {
await writeFile(secret, "x");
await chmod(secret, 0o000);
try {
- const result = await cli.run({
+ const result = await runFileCli(cli, {
arguments: ["trace-dylib-resolution", app, "--json"],
environment: ENVIRONMENT,
});
diff --git a/tests/support/cli/cliFixture.ts b/tests/support/cli/cliFixture.ts
index db71ac40f..545ac465a 100644
--- a/tests/support/cli/cliFixture.ts
+++ b/tests/support/cli/cliFixture.ts
@@ -251,6 +251,20 @@ export const cliCompileCacheEnvironment = (
...environment,
});
+/**
+ * Run one command on the file-scoped compiled CLI. Stdin and explicit
+ * deadlines stay on `TestCli.run`, which starts a fresh process.
+ */
+export const runFileCli = (
+ cli: TestCli,
+ invocation: CliInvocation,
+): Promise<CliResult> => {
+ const run = cli.sharedCliRun;
+ if (run === undefined)
+ throw new Error("cliTest did not provide a shared CLI session");
+ return run(invocation);
+};
+
/** Create a compiled CLI driver backed by the supplied process registry. */
export const createTestCli = (
processes: TestProcesses,