refactor(providers): finalize apktool and shared artifact resources through runOwned (#10653)

* refactor(providers): finalize apktool and shared artifact resources through runOwned

runOwned moves to the provider layer so provider sites can use it without depending on application workflows. It gains a completed-value projection (partial) for values that are not themselves partial observations, an optional signal, and cleanupLeases that bind a close failure to the exact leases whose confirmed release resolves it.

ArtifactAnalysisResources.finish is removed; TShark and sigrok finalize through runOwned with the shared release as close. Apktool's hand-written release, cancellation and lease-group composition is replaced by runOwned. A failed primary now keeps a cancellation observed during final cleanup as a secondary failure, as the documented precedence requires.

A counted architecture guard refuses new hand-written completed-result abort reclassification outside runOwned.

* refactor(adb): finalize runtime-root work through runOwned (#10660)

* refactor(adb): finalize runtime-root work through runOwned

The ADB runtime-root finalizer re-derived the precedence by hand and kept its own cancellation-with-cleanup combiner. It now runs through runOwned: the work body maps snapshot failures, close releases the root lease and binds its own cleanup error, and partial projects the completed execution. A cancellation whose cleanup fails is now the shared cleanup composite with the cancellation as its primary, as runOwned produces for every other owner.

* refactor(providers): keep completed values as diagnostics in runOwned and migrate four finalizers (#10691)

* refactor(providers): keep completed values as diagnostics in runOwned and migrate signature, Ghidra, bundle and LLDB finalization

* test(providers): run the lifecycle scenarios over every runOwned adopter shape (#10707)

One table of testing.md lifecycle rows runs over partial, diagnostics, publishing and lease adopters, replacing the hand-written precedence cases it covers.

* chore(lint): move runOwned's classifier baseline entry with the file
This commit is contained in:
morluto authored and GitHub committed 2026-11-15 17:44:13 +08:00
1 parent b87b318ae7
commit 26520bd290
23 files changed
+1066 -709

No files matched your search

+41 -7
View File
@@ -27,6 +27,30 @@ import { err, ok } from "../domain/result.js";
import { ArtifactAnalysisResources } from "./ArtifactAnalysisResources.js";
import { readLinuxResourceLimits } from "./LinuxResourceLimits.js";
import { ProviderProcessSupervisor } from "./ProviderProcess.js";
import type {
AnalysisError,
AnalysisPartialObservation,
} from "../domain/analysisErrorBase.js";
import type { Result } from "../domain/result.js";
import { finalizedResult, runOwned } from "../providers/runOwned.js";
/** Providers finalize these resources through runOwned with their release as the close. */
const finish = async <T>(
resources: ArtifactAnalysisResources,
result: Result<T, AnalysisError>,
partial: (value: T) => AnalysisPartialObservation,
operation = resources.operation,
signal?: AbortSignal,
): Promise<Result<T, AnalysisError>> =>
finalizedResult(
await runOwned(async () => result, {
providerId: resources.providerId,
operation,
signal,
close: () => resources.release(),
partial,
}),
);
it.runIf(process.platform !== "win32")(
"offers a resolved-file retry from the shared selected-artifact boundary",
@@ -155,7 +179,8 @@ it("projects the specific reason and member evidence for retained cleanup", asyn
);
resources.retain({ path: "runtime-root", close: async () => {} }, owner);
const result = await resources.finish(
const result = await finish(
resources,
err(new ProviderAdapterError("fixture-provider", "fixture-operation")),
() => {
throw new Error("unexpected success observation");
@@ -237,7 +262,7 @@ it("preserves the primary error and resolves cleanup status after retry", async
},
);
const result = await resources.finish(err(primary), () => {
const result = await finish(resources, err(primary), () => {
throw new Error("unexpected success observation");
});
@@ -277,7 +302,8 @@ it("resolves each returned cleanup error with its own retained root", async () =
if (closesA === 1) throw new Error("root A close failure");
},
});
const first = await resources.finish(
const first = await finish(
resources,
err(new AnalysisOutputError("fixture-operation", "primary failure")),
() => {
throw new Error("unexpected success observation");
@@ -302,7 +328,7 @@ it("resolves each returned cleanup error with its own retained root", async () =
confidence: "observed",
},
);
const second = await resources.finish(ok("completed"), () => partial);
const second = await finish(resources, ok("completed"), () => partial);
if (second.ok) throw new Error("expected root B cleanup failure");
expect(second.error.partialObservation).toEqual(partial);
@@ -428,7 +454,7 @@ it("keeps a runtime root until its descriptor cleanup owner is released", async
},
});
const outer = await resources.finish(first, () => {
const outer = await finish(resources, first, () => {
throw new Error("unexpected success observation");
});
@@ -807,7 +833,7 @@ it("preserves typed caller cancellation through failed workspace cleanup and ret
},
});
try {
const result = await resources.finish(err(primary), () => {
const result = await finish(resources, err(primary), () => {
throw new Error("Must not produce success Evidence");
});
if (result.ok) throw new Error("Expected retained workspace");
@@ -931,7 +957,8 @@ it.each([false, true])(
},
});
try {
const result = await resources.finish(
const result = await finish(
resources,
err(primary),
() => {
throw new Error("Failed work must not produce success Evidence");
@@ -943,6 +970,13 @@ it.each([false, true])(
expect(failsClose ? result.error.cleanupPrimary : result.error).toBe(
primary,
);
// The recorded failure stays primary and keeps the late cancellation.
expect(primary.secondaryFailures).toEqual([
expect.objectContaining({
_tag: "AnalysisCancelledError",
cause: { request: "late cancellation" },
}),
]);
denied = false;
await resources.close();
expect(result.error.cleanupIncomplete).toBe(false);
-54
View File
@@ -14,7 +14,6 @@ import { stableArtifactInputRecovery } from "../artifacts/StableArtifactFailure.
import {
AnalysisError,
type AnalysisCapturedOutput,
type AnalysisPartialObservation,
} from "../domain/analysisErrorBase.js";
import {
AnalysisAccessDeniedError,
@@ -351,12 +350,6 @@ export class ArtifactAnalysisResources {
: ownedCommandResources(ownership, root.path);
}
get #pendingResources(): readonly string[] {
return [...this.#pending.keys()].flatMap((root) =>
this.#resourcesFor(root),
);
}
async release(): Promise<void> {
const failures: ProviderCleanupError[] = [];
for (const [root, retained] of this.#pending) {
@@ -567,53 +560,6 @@ export class ArtifactAnalysisResources {
});
}
async finish<T>(
result: Result<T, AnalysisError>,
observation: (value: T) => AnalysisPartialObservation,
operation = this.operation,
signal?: AbortSignal,
): Promise<Result<T, AnalysisError>> {
try {
await this.release();
if (result.ok && signal?.aborted)
return err(
cancelledBy(operation, signal, {
partialObservation: observation(result.value),
}),
);
return result;
} catch (cause: unknown) {
const partialObservation = result.ok
? observation(result.value)
: result.error.partialObservation;
const primary = !result.ok
? result.error
: signal?.aborted
? cancelledBy(operation, signal, { partialObservation })
: undefined;
const cleanup =
cause instanceof AnalysisError
? cause
: new ProviderCleanupError(
this.providerId,
this.#pendingResources,
{ reason: String(cause) },
{ operation, cause },
);
const failure =
primary === undefined
? cleanup
: analysisErrorWithCleanupFailure(
primary,
cleanup,
operation,
).bindCleanupDependencies([primary, cleanup]);
if (partialObservation !== undefined)
failure.retainPartialObservation(partialObservation);
return err(failure);
}
}
close(): Promise<void> {
this.#closed = true;
const work = this.#tail.then(() => this.#closeResources());