diff --git a/scripts/lib/agent-evaluation-commands.mjs b/scripts/lib/agent-evaluation-commands.mjs index 5ebee1522a5..d60b0b4c6fa 100644 --- a/scripts/lib/agent-evaluation-commands.mjs +++ b/scripts/lib/agent-evaluation-commands.mjs @@ -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( diff --git a/scripts/lib/verifier-file-resources.mjs b/scripts/lib/verifier-file-resources.mjs index 2e88c9a08df..cd7c5db52dd 100644 --- a/scripts/lib/verifier-file-resources.mjs +++ b/scripts/lib/verifier-file-resources.mjs @@ -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()], ); } diff --git a/scripts/lib/verifier-terminal-owner.mts b/scripts/lib/verifier-terminal-owner.mts index 3622ee074e2..2a7b1659b8d 100644 --- a/scripts/lib/verifier-terminal-owner.mts +++ b/scripts/lib/verifier-terminal-owner.mts @@ -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(); readonly #maxOutputBytes: number; #output = ""; diff --git a/src/android/JadxProvider.ts b/src/android/JadxProvider.ts index 639a8fb8492..be0af7f5466 100644 --- a/src/android/JadxProvider.ts +++ b/src/android/JadxProvider.ts @@ -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>> >(); - 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 | undefined; constructor( readonly environment: Readonly>, diff --git a/src/application/binary/AnalysisProviderRegistry.ts b/src/application/binary/AnalysisProviderRegistry.ts index 0fd1091f938..1a0fbcf775b 100644 --- a/src/application/binary/AnalysisProviderRegistry.ts +++ b/src/application/binary/AnalysisProviderRegistry.ts @@ -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> | undefined; constructor( diff --git a/src/doctorRuntime.ts b/src/doctorRuntime.ts index 4e5e8b609ec..3843671eccc 100644 --- a/src/doctorRuntime.ts +++ b/src/doctorRuntime.ts @@ -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 = ( ): Promise => 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(); diff --git a/src/ghidra/GhidraProvider.ts b/src/ghidra/GhidraProvider.ts index a714e75601e..6665ea7470c 100644 --- a/src/ghidra/GhidraProvider.ts +++ b/src/ghidra/GhidraProvider.ts @@ -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> | 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> | undefined; private readonly environment: NodeJS.ProcessEnv; diff --git a/src/hopper/HopperProvider.ts b/src/hopper/HopperProvider.ts index 0755bc02990..4aee8e9c1fd 100644 --- a/src/hopper/HopperProvider.ts +++ b/src/hopper/HopperProvider.ts @@ -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 diff --git a/src/ida/IdaProvider.ts b/src/ida/IdaProvider.ts index c41056c6c68..9002f76eab6 100644 --- a/src/ida/IdaProvider.ts +++ b/src/ida/IdaProvider.ts @@ -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> diff --git a/src/kuna/KunaSessionClient.ts b/src/kuna/KunaSessionClient.ts index 2c87d9c39c6..164332da8ee 100644 --- a/src/kuna/KunaSessionClient.ts +++ b/src/kuna/KunaSessionClient.ts @@ -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> { - const parentResourceScope: RetainedResourceScope = { - retain: (owner, resource) => - this.#resourceScope.retain({ - kind: "lease", - resource, - lease: AnalysisCleanupLease.forCloseOwner(owner, resource), - }), - run: (operation: () => Promise) => - this.#resourceScope.run(operation), - }; - const kunaResources = createKunaResourceScope(parentResourceScope); + const kunaResources = createKunaResourceScope( + this.#resourceScope.forCloseOwners(), + ); try { const inspected = await inspectKunaInstallation(this.#environment, { platform: this.#platform, diff --git a/src/ports/AnalysisProvider.ts b/src/ports/AnalysisProvider.ts index 8ee3d65b84e..6bcd86fa6e0 100644 --- a/src/ports/AnalysisProvider.ts +++ b/src/ports/AnalysisProvider.ts @@ -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; - /** Complete labels authored by this exact owner; the display label is only a fallback. */ - readonly cleanupResources?: readonly string[] | undefined; - }, - resource: string, - ): void; - run(operation: () => Promise): Promise; -} +export type { RetainedResourceScope }; export interface ProviderIdentity { readonly id: string; diff --git a/src/providers/AnalysisClientCreation.ts b/src/providers/AnalysisClientCreation.ts index f5bdbacdd09..0d42d426735 100644 --- a/src/providers/AnalysisClientCreation.ts +++ b/src/providers/AnalysisClientCreation.ts @@ -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> | undefined; diff --git a/src/resources/ArtifactResourceScope.ts b/src/resources/ArtifactResourceScope.ts index b217a73dbbe..653f4898271 100644 --- a/src/resources/ArtifactResourceScope.ts +++ b/src/resources/ArtifactResourceScope.ts @@ -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; + /** Complete labels authored by this exact owner; the display label is only a fallback. */ + readonly cleanupResources?: readonly string[] | undefined; + }, + resource: string, + ): void; + run(operation: () => Promise): Promise; +} + /** Caller-owned scope that retains failed artifact cleanup for a later close. */ export class ArtifactResourceScope { readonly #pending = new Set(); @@ -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(operation: () => Promise): Promise { while (true) { diff --git a/src/resources/index.ts b/src/resources/index.ts index f1678ae089c..f12d7e3933f 100644 --- a/src/resources/index.ts +++ b/src/resources/index.ts @@ -95,6 +95,7 @@ export { export type { ArtifactCleanupAttempt, ArtifactResourceOwner, + RetainedResourceScope, } from "./ArtifactResourceScope.js"; export { imageFileSizeRemediation, diff --git a/tests/fixtures/inspectionResources.ts b/tests/fixtures/inspectionResources.ts index c35da975d35..bc0c343500f 100644 --- a/tests/fixtures/inspectionResources.ts +++ b/tests/fixtures/inspectionResources.ts @@ -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() }; };