fix(artifacts)!: retain cleanup owners through client shutdown
Keep failed reader, root descriptor, and output-tree cleanup with the caller. Drain admitted work before close, retry the same retained owners, and block new resource work while cleanup is incomplete. Preserve primary failures and complete inventory observations. Thread ownership through JavaScript reconstruction, the CLI's one-shot scope, MCP server shutdown, verifier scripts, and test fixtures. Remove redundant ownership flags and the unused validated test wrapper. Validate with 850 passing focused tests across 90 suites (two skipped), targeted classification and cleanup retries, static and unused-code checks, and real macOS CLI/MCP directory, ZIP, and DMG workflows. Native Windows and deep-analysis providers were not verified. BREAKING CHANGE: Direct artifact inventory/extraction and JavaScript analysis APIs require an explicit caller-owned ArtifactResourceScope. CLI and MCP input contracts are unchanged.
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
import type { FileHandle } from "node:fs/promises";
|
||||
|
||||
import type { ArtifactReader } from "./ArtifactReader.js";
|
||||
import { ArtifactReaderFailure } from "./ArtifactReader.js";
|
||||
import type { SafeOutputTree } from "./SafeOutputTree.js";
|
||||
|
||||
/** One artifact resource and its format-specific cleanup owner. */
|
||||
export type ArtifactResourceOwner =
|
||||
| {
|
||||
readonly kind: "reader";
|
||||
readonly resource: string;
|
||||
readonly reader: ArtifactReader;
|
||||
}
|
||||
| {
|
||||
readonly kind: "file-handle";
|
||||
readonly resource: string;
|
||||
readonly handle: FileHandle;
|
||||
}
|
||||
| {
|
||||
readonly kind: "output-tree";
|
||||
readonly resource: string;
|
||||
readonly tree: SafeOutputTree;
|
||||
};
|
||||
|
||||
export type ArtifactCleanupAttempt =
|
||||
| { readonly kind: "released" }
|
||||
| { readonly kind: "failed"; readonly cause: unknown };
|
||||
|
||||
/** Caller-owned scope that retains failed artifact cleanup for a later close. */
|
||||
export class ArtifactResourceScope {
|
||||
readonly #pending = new Set<ArtifactResourceOwner>();
|
||||
readonly #active = new Set<Promise<unknown>>();
|
||||
#draining: Promise<void> | undefined;
|
||||
#closed = false;
|
||||
|
||||
/** Admit a resource operation while the scope remains open. */
|
||||
async run<T>(operation: () => Promise<T>): Promise<T> {
|
||||
while (true) {
|
||||
if (this.#closed)
|
||||
throw new ArtifactReaderFailure(
|
||||
"unavailable",
|
||||
"Artifact resource scope is closed",
|
||||
);
|
||||
if (this.#draining !== undefined) await this.#draining;
|
||||
if (this.#closed)
|
||||
throw new ArtifactReaderFailure(
|
||||
"unavailable",
|
||||
"Artifact resource scope is closed",
|
||||
);
|
||||
if (this.#pending.size > 0) {
|
||||
await this.#retryCleanup();
|
||||
continue;
|
||||
}
|
||||
const running = Promise.resolve().then(operation);
|
||||
this.#active.add(running);
|
||||
try {
|
||||
return await running;
|
||||
} finally {
|
||||
this.#active.delete(running);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Attempt cleanup now and retain the same owner if it fails. */
|
||||
async release(owner: ArtifactResourceOwner): Promise<ArtifactCleanupAttempt> {
|
||||
try {
|
||||
await cleanup(owner);
|
||||
this.#pending.delete(owner);
|
||||
return { kind: "released" };
|
||||
} catch (cause: unknown) {
|
||||
this.#pending.add(owner);
|
||||
return { kind: "failed", cause };
|
||||
}
|
||||
}
|
||||
|
||||
/** Retry retained owners without closing resource admission. */
|
||||
#retryCleanup(): Promise<void> {
|
||||
if (this.#draining !== undefined) return this.#draining;
|
||||
this.#draining = this.#drainAndRetry().finally(() => {
|
||||
this.#draining = undefined;
|
||||
});
|
||||
return this.#draining;
|
||||
}
|
||||
|
||||
/** Stop admission, wait for active operations, and retry every retained owner. */
|
||||
close(): Promise<void> {
|
||||
this.#closed = true;
|
||||
return this.#retryCleanup();
|
||||
}
|
||||
|
||||
async #drainAndRetry(): Promise<void> {
|
||||
while (this.#active.size > 0) await Promise.allSettled(this.#active);
|
||||
const failures: {
|
||||
readonly owner: ArtifactResourceOwner;
|
||||
readonly cause: unknown;
|
||||
}[] = [];
|
||||
for (const owner of this.#pending) {
|
||||
try {
|
||||
await cleanup(owner);
|
||||
this.#pending.delete(owner);
|
||||
} catch (cause: unknown) {
|
||||
failures.push({ owner, cause });
|
||||
}
|
||||
}
|
||||
if (failures.length === 0) return;
|
||||
const observations = failures.map(({ owner, cause }) =>
|
||||
ArtifactReaderFailure.cleanupObservation(cause, owner.resource),
|
||||
);
|
||||
const resources = [
|
||||
...new Set(observations.flatMap(({ resources }) => resources)),
|
||||
];
|
||||
throw new ArtifactReaderFailure(
|
||||
"unavailable",
|
||||
`Artifact cleanup remains incomplete: ${resources.join(", ")}`,
|
||||
{
|
||||
cause: new AggregateError(
|
||||
failures.map(({ cause }) => cause),
|
||||
"One or more artifact resources remain owned",
|
||||
),
|
||||
cleanup: {
|
||||
reason: observations.map(({ reason }) => reason).join("; "),
|
||||
resources,
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const cleanup = async (owner: ArtifactResourceOwner): Promise<void> => {
|
||||
switch (owner.kind) {
|
||||
case "reader":
|
||||
await owner.reader.close();
|
||||
return;
|
||||
case "file-handle":
|
||||
await owner.handle.close();
|
||||
return;
|
||||
case "output-tree":
|
||||
await owner.tree.rollback();
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user