refactor(resources): share the retained close-owner adapter (#14562)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-11-26 01:24:30 +08:00
1 parent 0bbdbeaedf
commit 3d9ceeae6b
15 files changed
+45 -148

No files matched your search

+1 -11
View File
@@ -8,7 +8,6 @@ import {
runOwnedCommand,
} from "../../dist/process/OwnedCommand.js";
import { ArtifactResourceScope } from "../../dist/resources/ArtifactResourceScope.js";
import { AnalysisCleanupLease } from "../../dist/resources/AnalysisCleanupLease.js";
import { retainOwnedCommandCleanup } from "../../dist/providers/OwnedCommandCleanup.js";
/** Keep command ownership through failure and join cleanup before removing a run. */
@@ -21,16 +20,7 @@ export function createAgentEvaluationCommands({
repositoryRoot,
}) {
const resources = new ArtifactResourceScope();
const resourceOwner = {
retain(owner, resource) {
resources.retain({
kind: "lease",
resource,
lease: AnalysisCleanupLease.forCloseOwner(owner, resource),
});
},
run: (work) => resources.run(work),
};
const resourceOwner = resources.forCloseOwners();
const diagnosticBytes = 32_768;
async function execute(
+1 -12
View File
@@ -1,23 +1,12 @@
import { hashStableFile } from "../../dist/filesystem/StableFileHash.js";
import { ArtifactResourceScope } from "../../dist/resources/ArtifactResourceScope.js";
import { AnalysisCleanupLease } from "../../dist/resources/AnalysisCleanupLease.js";
import { withVerifierCleanup } from "./verifier-cleanup.mjs";
/** Finish a prerequisite probe and release its exact retained cleanup owners. */
export async function withVerifierResourceOwner(operation) {
const resources = new ArtifactResourceScope();
const resourceOwner = {
retain(owner, resource) {
resources.retain({
kind: "lease",
resource,
lease: AnalysisCleanupLease.forCloseOwner(owner, resource),
});
},
run: (work) => resources.run(work),
};
return withVerifierCleanup(
() => operation(resourceOwner),
() => operation(resources.forCloseOwners()),
[() => resources.close()],
);
}
+1 -11
View File
@@ -1,8 +1,6 @@
import { randomUUID } from "node:crypto";
import type { IDisposable, IPty } from "@lydell/node-pty";
import type { RetainedResourceScope } from "../../src/ports/AnalysisProvider.js";
import { AnalysisCleanupLease } from "../../src/resources/AnalysisCleanupLease.js";
import { ArtifactResourceScope } from "../../src/resources/ArtifactResourceScope.js";
import { err, ok, type Result } from "../../src/domain/result.js";
import {
@@ -107,15 +105,7 @@ class VerifierTerminalOwner implements VerifierCleanupOwner {
readonly #runId = randomUUID();
readonly #evidence = new ProcessOwnershipEvidenceOwner();
readonly #preparationResources = new ArtifactResourceScope();
readonly #resourceOwner: RetainedResourceScope = {
run: (work) => this.#preparationResources.run(work),
retain: (owner, resource) =>
this.#preparationResources.retain({
kind: "lease",
resource,
lease: AnalysisCleanupLease.forCloseOwner(owner, resource),
}),
};
readonly #resourceOwner = this.#preparationResources.forCloseOwners();
readonly #subscriptions = new Set<IDisposable>();
readonly #maxOutputBytes: number;
#output = "";
+3 -17
View File
@@ -1,14 +1,8 @@
import { snapshotEnvironment } from "../process/snapshotEnvironment.js";
import { HOST_PLATFORM, type HostPlatform } from "../platform/HostPlatform.js";
import type { AnalysisSettingsReader } from "../domain/analysisSettings.js";
import {
ArtifactResourceScope,
AnalysisCleanupLease,
} from "../resources/index.js";
import type {
RetainedResourceScope,
ExecutionOptions,
} from "../ports/AnalysisProvider.js";
import { ArtifactResourceScope } from "../resources/index.js";
import type { ExecutionOptions } from "../ports/AnalysisProvider.js";
import { analysisClientFailureForOperation } from "../providers/AnalysisClientFailure.js";
import { throwIfJadxInterrupted } from "./JadxInterruption.js";
import type { BigIntStats } from "node:fs";
@@ -345,15 +339,7 @@ export class JadxProvider implements AndroidAnalysisPort {
readonly #inspections = new Set<
Promise<Awaited<ReturnType<typeof inspectJadxAvailability>>>
>();
readonly #prerequisiteOwner: RetainedResourceScope = {
retain: (owner, resource) =>
this.#prerequisites.retain({
kind: "lease",
resource,
lease: AnalysisCleanupLease.forCloseOwner(owner, resource),
}),
run: (operation) => this.#prerequisites.run(operation),
};
readonly #prerequisiteOwner = this.#prerequisites.forCloseOwners();
#closePromise: Promise<void> | undefined;
constructor(
readonly environment: Readonly<Record<string, string | undefined>>,
@@ -1,6 +1,5 @@
import { ProviderInspectionScope } from "../../ports/ProviderInspectionScope.js";
import {
AnalysisCleanupLease,
ArtifactResourceScope,
ArtifactReaderFailure,
bindArtifactFileCleanup,
@@ -146,15 +145,7 @@ export class AnalysisProviderRegistry {
AnalysisCleanupRelease
>();
readonly #profileResources = new ArtifactResourceScope();
readonly #profileOwner: RetainedResourceScope = {
retain: (owner, resource) =>
this.#profileResources.retain({
kind: "lease",
resource,
lease: AnalysisCleanupLease.forCloseOwner(owner, resource),
}),
run: (operation) => this.#profileResources.run(operation),
};
readonly #profileOwner = this.#profileResources.forCloseOwners();
#closing: Promise<Result<null, AnalysisError>> | undefined;
constructor(
+1 -11
View File
@@ -35,7 +35,6 @@ import type { ConfigurationError } from "./domain/configurationErrors.js";
import { cancelledBy, carriedReason } from "./domain/analysisErrorCore.js";
import { ProviderCleanupError } from "./domain/providerCleanupError.js";
import {
AnalysisCleanupLease,
AnalysisCleanupLeaseGroup,
ArtifactResourceScope,
} from "./resources/index.js";
@@ -365,17 +364,8 @@ const withProbeResourceScope = <T>(
): Promise<T> =>
parent.run(async () => {
const child = new ArtifactResourceScope();
const scope: RetainedResourceScope = {
retain: (owner, resource) =>
child.retain({
kind: "lease",
resource,
lease: AnalysisCleanupLease.forCloseOwner(owner, resource),
}),
run: (work) => child.run(work),
};
try {
return await operation(scope);
return await operation(child.forCloseOwners());
} finally {
if (child.hasPending) parent.retain(child, label);
else await child.close();
+1 -10
View File
@@ -22,7 +22,6 @@ import type { ProviderInspectionScope } from "../ports/ProviderInspectionScope.j
import { analysisClientFailureForOperation } from "../providers/AnalysisClientFailure.js";
import { finalizedResult, runOwned } from "../providers/runOwned.js";
import {
AnalysisCleanupLease,
ArtifactResourceScope,
ArtifactReaderFailure,
bindArtifactFileCleanup,
@@ -142,15 +141,7 @@ export class GhidraProvider implements AnalysisProviderCandidate {
#inspectionAttempt:
| SharedStartupAttempt<Result<GhidraInstallationInspection, AnalysisError>>
| undefined;
readonly #inspectionOwner: RetainedResourceScope = {
retain: (owner, resource) =>
this.#inspectionResources.retain({
kind: "lease",
resource,
lease: AnalysisCleanupLease.forCloseOwner(owner, resource),
}),
run: (operation) => this.#inspectionResources.run(operation),
};
readonly #inspectionOwner = this.#inspectionResources.forCloseOwners();
#closing: Promise<Result<null, AnalysisError>> | undefined;
private readonly environment: NodeJS.ProcessEnv;
+1 -10
View File
@@ -23,7 +23,6 @@ import type {
ProviderAvailability,
ProviderIdentity,
ProviderTargetSupport,
RetainedResourceScope,
} from "../ports/AnalysisProvider.js";
import type { AnalysisProfileCommitment } from "../domain/analysisProfile.js";
import type { AppConfig } from "../config/types.js";
@@ -78,15 +77,7 @@ export class HopperProvider implements AnalysisProviderCandidate {
readonly #launcherSpelling: string;
readonly #launcherPath: string;
readonly #ownedResources = new ArtifactResourceScope();
readonly #inspectionOwner: RetainedResourceScope = {
retain: (owner, resource) =>
this.#ownedResources.retain({
kind: "lease",
resource,
lease: AnalysisCleanupLease.forCloseOwner(owner, resource),
}),
run: (operation) => this.#ownedResources.run(operation),
};
readonly #inspectionOwner = this.#ownedResources.forCloseOwners();
/**
* Host platform, injected so availability and launch-mode decisions can be
+1 -11
View File
@@ -1,7 +1,6 @@
import {
bindArtifactFileCleanup,
ArtifactResourceScope,
AnalysisCleanupLease,
} from "../resources/index.js";
import { HOST_PLATFORM } from "../platform/HostPlatform.js";
import { launchRegexMatchingLimitation } from "../process/RegexMatcher.js";
@@ -28,7 +27,6 @@ import type {
AnalysisProviderCandidate,
ProviderAvailability,
ProviderTargetSupport,
RetainedResourceScope,
} from "../ports/AnalysisProvider.js";
import type { AppConfig } from "../config/types.js";
import type { ConfigurationError } from "../domain/configurationErrors.js";
@@ -79,15 +77,7 @@ const IDA_ARCHITECTURES = {
/** Bring-your-own IDA MCP candidate; discovery performs no provider startup. */
export class IdaProvider implements AnalysisProviderCandidate {
readonly #ownedResources = new ArtifactResourceScope();
readonly #inspectionOwner: RetainedResourceScope = {
retain: (owner, resource) =>
this.#ownedResources.retain({
kind: "lease",
resource,
lease: AnalysisCleanupLease.forCloseOwner(owner, resource),
}),
run: (operation) => this.#ownedResources.run(operation),
};
readonly #inspectionOwner = this.#ownedResources.forCloseOwners();
readonly #registrationByScope = new WeakMap<
ProviderInspectionScope,
Promise<Result<IdaConfiguration, ConfigurationError>>
+3 -12
View File
@@ -37,7 +37,6 @@ import type {
AnalysisExecution,
AnalysisOperation,
ExecutionOptions,
RetainedResourceScope,
} from "../ports/AnalysisProvider.js";
import { AnalysisError } from "../domain/analysisErrorBase.js";
import {
@@ -1678,17 +1677,9 @@ export class KunaSessionClient implements AnalysisClient {
async #inspectInstallation(
signal: AbortSignal,
): Promise<Result<KunaInstallation, AnalysisError>> {
const parentResourceScope: RetainedResourceScope = {
retain: (owner, resource) =>
this.#resourceScope.retain({
kind: "lease",
resource,
lease: AnalysisCleanupLease.forCloseOwner(owner, resource),
}),
run: <T>(operation: () => Promise<T>) =>
this.#resourceScope.run(operation),
};
const kunaResources = createKunaResourceScope(parentResourceScope);
const kunaResources = createKunaResourceScope(
this.#resourceScope.forCloseOwners(),
);
try {
const inspected = await inspectKunaInstallation(this.#environment, {
platform: this.#platform,
+2 -12
View File
@@ -1,3 +1,4 @@
import type { RetainedResourceScope } from "../resources/index.js";
import type { AnalysisWorkScope } from "./AnalysisWorkScope.js";
import type { AnalysisObservation } from "../domain/analysisObservation.js";
import type { ProviderInspectionScope } from "./ProviderInspectionScope.js";
@@ -308,18 +309,7 @@ export interface AnalysisClientContext {
readonly runId: string;
}
/** One lifecycle owner for retained resources and abortable inspection work. */
export interface RetainedResourceScope {
retain(
owner: {
close(): Promise<void>;
/** Complete labels authored by this exact owner; the display label is only a fallback. */
readonly cleanupResources?: readonly string[] | undefined;
},
resource: string,
): void;
run<T>(operation: () => Promise<T>): Promise<T>;
}
export type { RetainedResourceScope };
export interface ProviderIdentity {
readonly id: string;
+1 -9
View File
@@ -29,15 +29,7 @@ import { closeAnalysisClient } from "./AnalysisClientCleanup.js";
/** Own deferred client creation while callers wait independently and close joins it. */
export class AnalysisClientCreation {
readonly #inspectionResources = new ArtifactResourceScope();
readonly #inspectionOwner: RetainedResourceScope = {
retain: (owner, resource) =>
this.#inspectionResources.retain({
kind: "lease",
resource,
lease: AnalysisCleanupLease.forCloseOwner(owner, resource),
}),
run: (operation) => this.#inspectionResources.run(operation),
};
readonly #inspectionOwner = this.#inspectionResources.forCloseOwners();
#attempt:
| SharedStartupAttempt<Result<AnalysisClient, AnalysisError>>
| undefined;
+26
View File
@@ -103,6 +103,19 @@ export class ArtifactResourceRetryFailure extends ArtifactReaderFailure {
}
}
/** One lifecycle owner for retained resources and abortable inspection work. */
export interface RetainedResourceScope {
retain(
owner: {
close(): Promise<void>;
/** Complete labels authored by this exact owner; the display label is only a fallback. */
readonly cleanupResources?: readonly string[] | undefined;
},
resource: string,
): void;
run<T>(operation: () => Promise<T>): Promise<T>;
}
/** Caller-owned scope that retains failed artifact cleanup for a later close. */
export class ArtifactResourceScope {
readonly #pending = new Set<ArtifactResourceOwner>();
@@ -137,6 +150,19 @@ export class ArtifactResourceScope {
];
}
/** Expose admission and retained close authority to an inspection or verifier. */
forCloseOwners(): RetainedResourceScope {
return {
run: (operation) => this.run(operation),
retain: (owner, resource) =>
this.retain({
kind: "lease",
resource,
lease: AnalysisCleanupLease.forCloseOwner(owner, resource),
}),
};
}
/** Admit a resource operation while the scope remains open. */
async run<T>(operation: () => Promise<T>): Promise<T> {
while (true) {
+1
View File
@@ -95,6 +95,7 @@ export {
export type {
ArtifactCleanupAttempt,
ArtifactResourceOwner,
RetainedResourceScope,
} from "./ArtifactResourceScope.js";
export {
imageFileSizeRemediation,
+1 -12
View File
@@ -1,18 +1,7 @@
import { ArtifactResourceScope } from "../../src/resources/ArtifactResourceScope.js";
import { AnalysisCleanupLease } from "../../src/resources/AnalysisCleanupLease.js";
import type { RetainedResourceScope } from "../../src/ports/AnalysisProvider.js";
/** Test-scoped real inspection leases; every retained owner is closed by the fixture. */
export const createInspectionResources = () => {
const scope = new ArtifactResourceScope();
const owner: RetainedResourceScope = {
retain: (resourceOwner, resource) =>
scope.retain({
kind: "lease",
resource,
lease: AnalysisCleanupLease.forCloseOwner(resourceOwner, resource),
}),
run: (operation) => scope.run(operation),
};
return { owner, close: () => scope.close() };
return { owner: scope.forCloseOwners(), close: () => scope.close() };
};