Files
reverse-engineering-anything/tests/boundary/providers/kuna/kunaSessionFixture.ts
T
morluto ff78165978 fix(kuna): own version-probe processes through output and cleanup
Use the shared command supervisor for installation probes and retain exact
process cleanup owners through doctor, profile resolution, and session health.
Preserve exit status, bounded captured output, cancellation reasons, and live
cleanup facts. Block subsequent inspection until retained cleanup is retried.

Generalize the existing inspection resource scope and migrate its consumers,
including verifier scripts; remove the verifier's duplicate raw version probe.

Validation: 116 distinct focused tests passed, including built CLI doctor with
inherited output, cancellation/finalization, cleanup retry, and fake-provider
verifier workflows. Build, check:fast, and test discovery passed. No real Kuna
distribution or analysis target was executed.

Synthetic Darwin inspection timing: first-call median 707ms versus 5.79ms;
same-process repeat 37.9ms versus 3.08ms. First use includes ownership-helper
preparation. Family CPU and actual peak family memory remain unmeasured.

Fixes #5557
2026-10-21 00:09:34 +08:00

108 lines
3.8 KiB
TypeScript

import { createHash } from "node:crypto";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import type { AnalysisClient } from "../../../../src/application/AnalysisProvider.js";
import type { BinaryTarget } from "../../../../src/domain/binaryTargetTypes.js";
import type { JsonValue } from "../../../../src/domain/jsonValue.js";
import type { KunaProcessLauncher } from "../../../../src/kuna/KunaCommand.js";
import { KunaProvider } from "../../../../src/kuna/KunaProvider.js";
import { cleanupOwnedProcessGroup } from "../../../../src/process/ProcessOwnership.js";
import { spawnOwnedProviderProcess } from "../../../../src/process/ProviderProcess.js";
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
import { withKunaResourceScope } from "./kunaResourceScope.js";
import {
fakeKunaCalls,
writeFakeKuna,
type FakeKunaBehavior,
} from "./fakeKuna.js";
export const FUNCTIONS = [
{ name: "main", addr: "0x1000" },
{ name: "parse", addr: "0x1040" },
{
name: "broken",
addr: "0x1080",
error: "Flow exceeded maximum instructions",
},
] as const;
export const session = async (
prefix: string,
behavior: FakeKunaBehavior = {},
environment: Readonly<Record<string, string>> = {},
launcher?: KunaProcessLauncher,
singleFunctionRuns?: number,
) => {
const root = await createTestTempDirectory(prefix);
const kuna = await writeFakeKuna(root, { functions: FUNCTIONS, ...behavior });
const targetPath = join(root, "program");
await writeFile(targetPath, "program bytes");
const target: BinaryTarget = {
path: targetPath,
sha256: createHash("sha256").update("program bytes").digest("hex"),
kind: "executable",
format: "elf",
architecture: "x86_64",
availableArchitectures: ["x86_64"],
};
const cacheRoot = join(root, "cache");
const provider = new KunaProvider(
{ REA_KUNA_COMMAND: kuna, REA_KUNA_CACHE_DIR: cacheRoot, ...environment },
{
platform: process.platform,
...(launcher === undefined ? {} : { launcher }),
...(singleFunctionRuns === undefined ? {} : { singleFunctionRuns }),
},
);
const profile = await withKunaResourceScope((resourceScope) =>
provider.resolveAnalysisProfile(target, { resourceScope }),
);
if (!profile.ok || profile.value.profile === null)
throw new Error("profile did not resolve");
const resolved = profile.value.profile;
const open = () => provider.createClient(target, resolved);
return { root, kuna, target, cacheRoot, provider, profile: resolved, open };
};
export const run = async (
client: AnalysisClient,
operation: Parameters<AnalysisClient["execute"]>[0],
parameters: Readonly<Record<string, JsonValue>> = {},
signal?: AbortSignal,
) => {
const result = await client.execute(
operation,
parameters,
signal === undefined ? {} : { signal },
);
if (!result.ok) throw result.error;
return result.value;
};
export const exportsOf = async (kuna: string) =>
(await fakeKunaCalls(kuna)).filter(
({ argv }) => argv[0] === "decompile-project",
);
/**
* Launch for real, but have cleanup of the selected subcommand report an
* unproven stop until `release` is called, as a lost ownership proof would.
*/
export const unprovenCleanupLauncher = (subcommand: string) => {
let proven = false;
const launcher: KunaProcessLauncher = async (options) => {
const spawned = await spawnOwnedProviderProcess(options);
if (options.arguments[0] !== subcommand) return spawned;
return {
...spawned,
cleanup: async () => {
const cleaned = await cleanupOwnedProcessGroup(spawned.ownership);
return proven
? cleaned
: { cleaned: false, reason: `${subcommand} stop unproven` };
},
};
};
return { launcher, release: () => (proven = true) };
};