Skip to content

Commit ff78165

Browse files
committed
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
1 parent 90e433d commit ff78165

41 files changed

Lines changed: 1319 additions & 263 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎scripts/lib/kuna-native-reference-verifier.mjs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ export const nativeKunaIncomingSites = async ({
1414
binary,
1515
address,
1616
retain,
17-
fileCloseResources,
17+
resourceScope,
1818
}) => {
1919
const installed = await inspectKunaInstallation(
2020
{
2121
REA_KUNA_COMMAND: executable,
2222
REA_KUNA_CACHE_DIR: cacheRoot,
2323
},
24-
{ fileCloseResources },
24+
{ resourceScope },
2525
);
2626
assert.ok(installed.ok, "Native reference oracle could not inspect Kuna");
2727
const input = await realpath(binary);
@@ -37,7 +37,7 @@ export const nativeKunaIncomingSites = async ({
3737
outputBytes: KUNA_XREF_OUTPUT_BYTES,
3838
signal: new AbortController().signal,
3939
retainFailedFileClose: (owner, resource) =>
40-
fileCloseResources.retain(owner, resource),
40+
resourceScope.retain(owner, resource),
4141
});
4242
if (outcome.kind === "retained") retain(outcome);
4343
if (outcome.kind !== "ok") throw outcome.error;

‎scripts/lib/kuna-string-verifier.mjs‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ export const nativeKunaStrings = async ({
1919
workspace,
2020
binary,
2121
retain,
22-
fileCloseResources,
22+
resourceScope,
2323
}) => {
2424
const installed = await inspectKunaInstallation(
2525
{
2626
REA_KUNA_COMMAND: executable,
2727
REA_KUNA_CACHE_DIR: cacheRoot,
2828
},
29-
{ fileCloseResources },
29+
{ resourceScope },
3030
);
3131
assert.ok(installed.ok, "Native string oracle could not inspect Kuna");
3232
const input = await realpath(binary);
@@ -51,7 +51,7 @@ export const nativeKunaStrings = async ({
5151
outputBytes: 16 * 1024 * 1024,
5252
signal: new AbortController().signal,
5353
retainFailedFileClose: (owner, resource) =>
54-
fileCloseResources.retain(owner, resource),
54+
resourceScope.retain(owner, resource),
5555
});
5656
if (outcome.kind === "retained") retain(outcome);
5757
if (outcome.kind !== "ok") throw outcome.error;
@@ -87,7 +87,6 @@ export const verifyKunaStrings = async ({
8787
cacheRoot,
8888
nativeStrings,
8989
expectedStrings = [],
90-
fileCloseResources,
9190
}) => {
9291
const expected = await nativeStrings();
9392
if (expectedStrings.length > 0)

‎scripts/lib/verifier-file-resources.d.mts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { ArtifactResourceScope } from "../../src/artifacts/ArtifactResourceScope.js";
2-
import type { FileCloseResourceOwner } from "../../src/application/AnalysisProvider.js";
2+
import type { RetainedResourceScope } from "../../src/application/AnalysisProvider.js";
33

44
/** Complete a prerequisite operation, including retained cleanup on failure. */
55
export function withVerifierResourceOwner<T>(
6-
operation: (owner: FileCloseResourceOwner) => Promise<T>,
6+
operation: (owner: RetainedResourceScope) => Promise<T>,
77
): Promise<T>;
88

99
/** Hash one verifier input and transfer failed-close ownership to its scope. */

‎scripts/verify-real-kuna.mjs‎

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ import { fileURLToPath } from "node:url";
2525
import { parseArgs, promisify } from "node:util";
2626
import { Client } from "@modelcontextprotocol/client";
2727
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
28-
import { execFileOutput } from "../dist/process/ExecFileOutput.js";
28+
import { inspectKunaInstallation } from "../dist/kuna/KunaInstallation.js";
2929
import { ArtifactResourceScope } from "../dist/artifacts/ArtifactResourceScope.js";
30+
import { AnalysisCleanupLease } from "../dist/artifacts/AnalysisCleanupLease.js";
3031
import { requireMcpOperationResult } from "./lib/mcp-verifier-results.mjs";
3132
import { verifyKunaReferences } from "./lib/kuna-reference-verifier.mjs";
3233
import {
@@ -71,11 +72,15 @@ if (values["compare-ghidra"]) {
7172
);
7273
}
7374
const run = createVerifierRun();
74-
const fileCloseScope = new ArtifactResourceScope();
75-
const fileCloseResources = {
75+
const inspectionResources = new ArtifactResourceScope();
76+
const resourceScope = {
7677
retain: (owner, resource) =>
77-
fileCloseScope.retain({ kind: "file-handle", resource, handle: owner }),
78-
run: (operation) => fileCloseScope.run(operation),
78+
inspectionResources.retain({
79+
kind: "lease",
80+
resource,
81+
lease: AnalysisCleanupLease.forCloseOwner(owner, resource),
82+
}),
83+
run: (operation) => inspectionResources.run(operation),
7984
};
8085
const workspace = await mkdtemp(join(tmpdir(), "rea-kuna-lane-"));
8186
const cacheRoot = join(workspace, "cache");
@@ -292,15 +297,14 @@ const verifyKuna = async (
292297
call: cold.call,
293298
cacheRoot,
294299
expectedStrings,
295-
fileCloseResources,
296300
nativeStrings: () =>
297301
nativeKunaStrings({
298302
executable: kuna,
299303
cacheRoot,
300304
workspace,
301305
binary: path,
302306
retain: (owner) => retainedNative.add(owner),
303-
fileCloseResources,
307+
resourceScope,
304308
}),
305309
}),
306310
});
@@ -324,7 +328,7 @@ const verifyKuna = async (
324328
binary: path,
325329
address: targetAddress,
326330
retain: (outcome) => retainedNative.add(outcome),
327-
fileCloseResources,
331+
resourceScope,
328332
}),
329333
});
330334
const pseudocode = await cold.call("procedure_pseudo_code", {
@@ -492,15 +496,17 @@ const verifyVariant = async ({ flavor, directory, symbols }, variant) => {
492496

493497
const results = [];
494498
const failures = [];
495-
let fileResourcesClean = true;
499+
let inspectionResourcesClean = true;
496500
try {
497-
kunaVersion = (
498-
await execFileOutput(kuna, ["--version"], {
499-
timeout: 10_000,
500-
maxBuffer: 64 * 1024,
501-
stopSignal: "SIGTERM",
502-
})
503-
).stdout.trim();
501+
const installation = await inspectKunaInstallation(
502+
{ REA_KUNA_COMMAND: kuna, REA_KUNA_CACHE_DIR: cacheRoot },
503+
{ resourceScope },
504+
);
505+
assert.ok(
506+
installation.ok,
507+
installation.ok ? undefined : installation.error.reason,
508+
);
509+
kunaVersion = `kuna ${installation.value.version}`;
504510
let callerPairs = [];
505511
if (values.targets !== undefined) {
506512
const names = (values.functions ?? "").split(",").filter(Boolean);
@@ -566,14 +572,16 @@ for (const retained of [...retainedNative]) {
566572
}
567573
}
568574
try {
569-
await fileCloseScope.close();
575+
await inspectionResources.close();
570576
} catch (cause) {
571-
fileResourcesClean = false;
577+
inspectionResourcesClean = false;
572578
failures.push(cause);
573579
}
574580
const lifecycle = await completeVerifierRun(run);
575581
let cleanupComplete =
576-
pendingServers.size === 0 && retainedNative.size === 0 && fileResourcesClean;
582+
pendingServers.size === 0 &&
583+
retainedNative.size === 0 &&
584+
inspectionResourcesClean;
577585
try {
578586
assertVerifierCleanupComplete(lifecycle.process_lineage);
579587
} catch (cause) {

‎src/android/JadxDoctor.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FileCloseResourceOwner } from "../application/AnalysisProvider.js";
1+
import type { RetainedResourceScope } from "../application/AnalysisProvider.js";
22
import type {
33
DoctorProviderCheck,
44
DoctorProviderInspection,
@@ -15,7 +15,7 @@ import {
1515
/** Project the validated JADX configuration into the doctor contract. */
1616
export const inspectJadxProvider = async (
1717
environment: Readonly<Record<string, string | undefined>>,
18-
resourceOwner: FileCloseResourceOwner,
18+
resourceOwner: RetainedResourceScope,
1919
signal?: AbortSignal,
2020
): Promise<DoctorProviderInspection> => {
2121
const configured = environment.REA_JADX_MCP_JAR !== undefined;

‎src/android/JadxJavaProbe.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { randomUUID } from "node:crypto";
2-
import type { FileCloseResourceOwner } from "../application/AnalysisProvider.js";
2+
import type { RetainedResourceScope } from "../application/AnalysisProvider.js";
33
import {
44
AnalysisError,
55
type AnalysisCapturedOutput,
@@ -27,7 +27,7 @@ const JAVA_MODULE_LISTING_TIMEOUT_MS = 10_000;
2727
const JAVA_MODULE_LISTING_MAX_OUTPUT_BYTES = 1024 * 1024;
2828

2929
export interface JadxJavaProbeOptions {
30-
readonly resourceOwner: FileCloseResourceOwner;
30+
readonly resourceOwner: RetainedResourceScope;
3131
readonly signal?: AbortSignal | undefined;
3232
readonly operation?: string;
3333
/** The Android operation owns this deadline; the probe also has its own bound. */

‎src/android/JadxProvider.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
22
import { AnalysisCleanupLease } from "../artifacts/AnalysisCleanupLease.js";
3-
import type { FileCloseResourceOwner } from "../application/AnalysisProvider.js";
3+
import type { RetainedResourceScope } from "../application/AnalysisProvider.js";
44
import { analysisClientFailureForOperation } from "../providers/AnalysisClientFailure.js";
55
import { throwIfJadxInterrupted } from "./JadxInterruption.js";
66
import { chmod, copyFile } from "node:fs/promises";
@@ -250,7 +250,7 @@ export class JadxProvider implements AndroidAnalysisPort {
250250
readonly #inspections = new Set<
251251
Promise<Awaited<ReturnType<typeof inspectJadxAvailability>>>
252252
>();
253-
readonly #prerequisiteOwner: FileCloseResourceOwner = {
253+
readonly #prerequisiteOwner: RetainedResourceScope = {
254254
retain: (owner, resource) =>
255255
this.#prerequisites.retain({
256256
kind: "lease",

‎src/application/AnalysisProvider.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ export interface AnalysisClientContext {
215215
readonly runId: string;
216216
}
217217

218-
/** One lifecycle owner for descriptor cleanup and abortable inspection work. */
219-
export interface FileCloseResourceOwner {
218+
/** One lifecycle owner for retained resources and abortable inspection work. */
219+
export interface RetainedResourceScope {
220220
retain(
221221
owner: {
222222
close(): Promise<void>;
@@ -246,7 +246,7 @@ export interface AnalysisProviderInspectionOptions {
246246
}
247247

248248
export interface AnalysisProfileResolutionOptions extends AnalysisProviderInspectionOptions {
249-
readonly fileCloseResources?: FileCloseResourceOwner;
249+
readonly resourceScope?: RetainedResourceScope;
250250
}
251251

252252
interface CapabilityEffects {

‎src/application/Doctor.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { STARTUP_OWNER_CLOSED } from "../process/SharedStartupAttempt.js";
1010
import { constants } from "node:fs";
1111
import { access, readdir, stat } from "node:fs/promises";
1212
import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
13-
import type { FileCloseResourceOwner } from "./AnalysisProvider.js";
13+
import type { RetainedResourceScope } from "./AnalysisProvider.js";
1414
import { homeDirectoryFromEnvironment } from "../config/homeDirectory.js";
1515
import { join } from "node:path";
1616

@@ -480,7 +480,7 @@ export interface SystemDoctorHostOptions {
480480
readonly environment?: NodeJS.ProcessEnv;
481481
readonly execFileOutput?: typeof execFileOutput;
482482
readonly providerInspections?: (
483-
fileCloseResources: FileCloseResourceOwner,
483+
resourceScope: RetainedResourceScope,
484484
signal: AbortSignal,
485485
) => Promise<readonly DoctorProviderInspection[]>;
486486
readonly providerRegistrationEnvironment?: () => Readonly<
@@ -506,7 +506,7 @@ export const systemDoctorHost = (
506506
const hostExecFileOutput = options.execFileOutput ?? execFileOutput;
507507
const homeDirectory = homeDirectoryFromEnvironment(environment, platform);
508508
const commandEnvironment = { env: environment };
509-
const fileCloseResources: FileCloseResourceOwner = {
509+
const resourceScope: RetainedResourceScope = {
510510
retain: (owner, resource) =>
511511
resources.retain({
512512
kind: "lease",
@@ -620,10 +620,7 @@ export const systemDoctorHost = (
620620
: {
621621
providerInspections: () =>
622622
resources.run(() =>
623-
options.providerInspections!(
624-
fileCloseResources,
625-
inspectionSignal,
626-
),
623+
options.providerInspections!(resourceScope, inspectionSignal),
627624
),
628625
}),
629626
...(options.providerRegistrationEnvironment === undefined

‎src/application/binary/AnalysisProviderRegistry.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
type AnalysisProviderCandidateEvaluation,
4040
} from "./AnalysisProviderEvaluation.js";
4141
import { compareUnicodeCodePoints } from "../../domain/unicodeCodePointOrder.js";
42-
import type { FileCloseResourceOwner } from "../AnalysisProvider.js";
42+
import type { RetainedResourceScope } from "../AnalysisProvider.js";
4343

4444
/** Deterministic discovery truth for one configured deep provider. */
4545
export interface AnalysisProviderCandidateStatus {
@@ -108,15 +108,15 @@ export class AnalysisProviderRegistry {
108108
>
109109
>();
110110
readonly #selectionCleanup = new Set<AnalysisError>();
111-
readonly #profileFiles = new ArtifactResourceScope();
112-
readonly #profileFileOwner: FileCloseResourceOwner = {
111+
readonly #profileResources = new ArtifactResourceScope();
112+
readonly #profileOwner: RetainedResourceScope = {
113113
retain: (owner, resource) =>
114-
this.#profileFiles.retain({
114+
this.#profileResources.retain({
115115
kind: "lease",
116116
resource,
117117
lease: AnalysisCleanupLease.forCloseOwner(owner, resource),
118118
}),
119-
run: (operation) => this.#profileFiles.run(operation),
119+
run: (operation) => this.#profileResources.run(operation),
120120
};
121121
#closing: Promise<Result<null, AnalysisError>> | undefined;
122122

@@ -186,7 +186,7 @@ export class AnalysisProviderRegistry {
186186
return (
187187
this.#selections.size > 0 ||
188188
this.#discoveries.size > 0 ||
189-
this.#profileFiles.hasPending ||
189+
this.#profileResources.hasPending ||
190190
[...this.#selectionCleanup].some((error) => error.cleanupIncomplete) ||
191191
this.#providers.some(
192192
(provider) => provider.hasPendingOwnedResources?.() === true,
@@ -215,7 +215,7 @@ export class AnalysisProviderRegistry {
215215
]);
216216
const failures: AnalysisError[] = [];
217217
try {
218-
await this.#profileFiles.retryCleanup();
218+
await this.#profileResources.retryCleanup();
219219
} catch (cause: unknown) {
220220
const cleanup = ArtifactReaderFailure.cleanupObservation(
221221
cause,
@@ -564,7 +564,7 @@ export class AnalysisProviderRegistry {
564564
provider,
565565
initial,
566566
target,
567-
{ signal, fileCloseResources: this.#profileFileOwner, scope },
567+
{ signal, resourceScope: this.#profileOwner, scope },
568568
);
569569
if (!evaluation.ok) return evaluation;
570570
const evaluated = evaluation.value;
@@ -608,7 +608,7 @@ export class AnalysisProviderRegistry {
608608
status.targetSupport.status === "supported"
609609
? evaluateAnalysisProviderCandidate(provider, status, target, {
610610
signal,
611-
fileCloseResources: this.#profileFileOwner,
611+
resourceScope: this.#profileOwner,
612612
scope,
613613
})
614614
: ok({

0 commit comments

Comments
 (0)