mirror of
https://github.com/morluto/rea.git
synced 2026-10-12 02:19:24 +00:00
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.
109 lines
3.5 KiB
TypeScript
109 lines
3.5 KiB
TypeScript
import { ArtifactResourceScope } from "../../src/artifacts/ArtifactResourceScope.js";
|
|
import { onTestFinished } from "vitest";
|
|
import { ArtifactReaderFailure } from "../../src/artifacts/ArtifactReader.js";
|
|
import { classifyAndHashRoot as classifyAndHashRootOwned } from "../../src/artifacts/inventory/classify.js";
|
|
import {
|
|
inventoryArtifact as inventoryArtifactOwned,
|
|
scanArtifactInventory as scanArtifactInventoryOwned,
|
|
} from "../../src/artifacts/inventory/ArtifactInventory.js";
|
|
import { scanCanonicalArtifactInventory as scanCanonicalArtifactInventoryOwned } from "../../src/artifacts/inventory/scanCanonical.js";
|
|
import type { createReader } from "../../src/artifacts/inventory/reader.js";
|
|
import {
|
|
materializeArtifactInventory as materializeArtifactInventoryOwned,
|
|
type ArtifactExtractionInput,
|
|
} from "../../src/artifacts/extraction/ArtifactExtraction.js";
|
|
import type { ArtifactInventorySnapshot } from "../../src/domain/artifactInventorySnapshot.js";
|
|
import type { ArtifactInventoryOptions } from "../../src/artifacts/inventory/types.js";
|
|
|
|
type InventoryOptions = Omit<ArtifactInventoryOptions, "resourceScope">;
|
|
|
|
export const inventoryArtifact = (
|
|
path: string,
|
|
options: InventoryOptions = {},
|
|
) =>
|
|
withArtifactResourceScope((resourceScope) =>
|
|
inventoryArtifactOwned(path, { ...options, resourceScope }),
|
|
);
|
|
|
|
export const classifyAndHashRoot = (
|
|
path: string,
|
|
directory: boolean,
|
|
expectedMetadata: import("node:fs").Stats,
|
|
signal?: AbortSignal,
|
|
) =>
|
|
withArtifactResourceScope((resourceScope) =>
|
|
classifyAndHashRootOwned(path, directory, expectedMetadata, {
|
|
resourceScope,
|
|
...(signal === undefined ? {} : { signal }),
|
|
}),
|
|
);
|
|
|
|
export const scanArtifactInventory = (
|
|
path: string,
|
|
options: InventoryOptions = {},
|
|
) =>
|
|
withArtifactResourceScope((resourceScope) =>
|
|
scanArtifactInventoryOwned(path, { ...options, resourceScope }),
|
|
);
|
|
|
|
export const scanCanonicalArtifactInventory = (
|
|
path: string,
|
|
options: InventoryOptions = {},
|
|
readerFactory?: typeof createReader,
|
|
) =>
|
|
withArtifactResourceScope((resourceScope) =>
|
|
scanCanonicalArtifactInventoryOwned(
|
|
path,
|
|
{ ...options, resourceScope },
|
|
readerFactory,
|
|
),
|
|
);
|
|
|
|
export const materializeArtifactInventory = (
|
|
input: Omit<ArtifactExtractionInput, "resourceScope">,
|
|
sourcePath: string,
|
|
snapshot: ArtifactInventorySnapshot,
|
|
signal?: AbortSignal,
|
|
) =>
|
|
withArtifactResourceScope((resourceScope) =>
|
|
materializeArtifactInventoryOwned(
|
|
{ ...input, resourceScope },
|
|
sourcePath,
|
|
snapshot,
|
|
signal,
|
|
),
|
|
);
|
|
|
|
export const withArtifactResourceScope = async <T>(
|
|
operation: (resourceScope: ArtifactResourceScope) => Promise<T>,
|
|
): Promise<T> => {
|
|
const resourceScope = new ArtifactResourceScope();
|
|
onTestFinished(() => resourceScope.close());
|
|
let outcome:
|
|
| { readonly kind: "completed"; readonly value: T }
|
|
| { readonly kind: "failed"; readonly cause: unknown };
|
|
try {
|
|
outcome = {
|
|
kind: "completed",
|
|
value: await operation(resourceScope),
|
|
};
|
|
} catch (cause: unknown) {
|
|
outcome = { kind: "failed", cause };
|
|
}
|
|
try {
|
|
await resourceScope.close();
|
|
} catch (cleanupCause: unknown) {
|
|
if (outcome.kind === "failed")
|
|
throw ArtifactReaderFailure.withCleanup(
|
|
outcome.cause,
|
|
ArtifactReaderFailure.cleanupObservation(
|
|
cleanupCause,
|
|
"artifact resources",
|
|
),
|
|
);
|
|
throw cleanupCause;
|
|
}
|
|
if (outcome.kind === "failed") throw outcome.cause;
|
|
return outcome.value;
|
|
};
|