fix(artifacts): retain read cleanup ownership through CLI and MCP shutdown
Keep failed stable-read handles and WABT runtime roots under retryable resource scopes. Preserve cancellation causes and primary errors, close the same service instances bound to MCP tools, and consolidate CLI operation/cleanup projection. Validation: fast typecheck and lint, cached runtime build, docs checks, SDK schema validation, focused service and real-file cleanup tests, CLI cleanup regressions, and MCP lifecycle/shutdown workflows. Provider process fixtures simulate engine output; real WABT and Linux EVM execution are not established by this pass.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { lstat, realpath } from "node:fs/promises";
|
||||
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
||||
import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
|
||||
import { ArtifactResourceScope } from "../../artifacts/ArtifactResourceScope.js";
|
||||
import { readStableArtifact } from "../../artifacts/readStableArtifact.js";
|
||||
import { normalizeArtifactPath } from "../../artifacts/ArtifactPaths.js";
|
||||
import type { WebScriptArtifactPort } from "../../application/WebScriptArtifactPort.js";
|
||||
@@ -19,10 +20,36 @@ import {
|
||||
|
||||
/** Read one digest-verified captured source under the manifest's current location. */
|
||||
export class LocalWebScriptArtifacts implements WebScriptArtifactPort {
|
||||
constructor(readonly operation: WebArtifactReadContext["operation"]) {}
|
||||
constructor(
|
||||
readonly operation: WebArtifactReadContext["operation"],
|
||||
readonly resources = new ArtifactResourceScope(),
|
||||
) {}
|
||||
|
||||
close(): Promise<void> {
|
||||
return this.resources.close();
|
||||
}
|
||||
|
||||
/** Read the selected source; relocated exports retain their reported original root. */
|
||||
async load(input: SelectedWebScriptInput, options?: ExecutionOptions) {
|
||||
try {
|
||||
return await this.resources.run(() => this.#load(input, options));
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
webArtifactReadError(
|
||||
cause,
|
||||
{
|
||||
operation: this.operation,
|
||||
field: ["manifest_path"],
|
||||
targetPath: input.manifest_path,
|
||||
callerSelected: true,
|
||||
},
|
||||
options?.signal,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async #load(input: SelectedWebScriptInput, options?: ExecutionOptions) {
|
||||
let field: readonly (string | number)[] = ["manifest_path"];
|
||||
let targetPath = input.manifest_path;
|
||||
let callerSelected = true;
|
||||
@@ -31,6 +58,7 @@ export class LocalWebScriptArtifacts implements WebScriptArtifactPort {
|
||||
input.manifest_path,
|
||||
32 * 1024 * 1024,
|
||||
options?.signal,
|
||||
this.resources,
|
||||
);
|
||||
const manifest = webScriptExportManifestSchema.parse(manifestBytes.value);
|
||||
field = ["script_index"];
|
||||
@@ -65,6 +93,8 @@ export class LocalWebScriptArtifacts implements WebScriptArtifactPort {
|
||||
sourcePath,
|
||||
16 * 1024 * 1024,
|
||||
options?.signal,
|
||||
undefined,
|
||||
this.resources,
|
||||
);
|
||||
await assertContained(root, sourcePath);
|
||||
if (
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { z } from "zod";
|
||||
import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
|
||||
import type { ArtifactResourceScope } from "../../artifacts/ArtifactResourceScope.js";
|
||||
import { readStableArtifact } from "../../artifacts/readStableArtifact.js";
|
||||
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
|
||||
import {
|
||||
@@ -9,6 +10,8 @@ import {
|
||||
} from "../../domain/analysisErrorCore.js";
|
||||
import { ArtifactOperationError } from "../../domain/artifactOperationError.js";
|
||||
import { ProviderAdapterError } from "../../domain/providerAdapterError.js";
|
||||
import { ProviderCleanupError } from "../../domain/providerCleanupError.js";
|
||||
import { analysisErrorWithCleanupFailure } from "../../domain/analysisErrorCleanup.js";
|
||||
import { safeParseJson } from "../../domain/safeJson.js";
|
||||
|
||||
/** The selected artifact and input field owning a reader failure. */
|
||||
@@ -42,8 +45,15 @@ export const readWebArtifactJson = async (
|
||||
path: string,
|
||||
maximumBytes: number,
|
||||
signal?: AbortSignal,
|
||||
resourceScope?: ArtifactResourceScope,
|
||||
) => {
|
||||
const data = await readStableArtifact(path, maximumBytes, signal);
|
||||
const data = await readStableArtifact(
|
||||
path,
|
||||
maximumBytes,
|
||||
signal,
|
||||
undefined,
|
||||
resourceScope,
|
||||
);
|
||||
const value = safeParseJson(decodeWebArtifact(data.bytes));
|
||||
if (!value.ok) throw new WebArtifactFormatFailure(value.error);
|
||||
return { ...data, value: value.value };
|
||||
@@ -55,8 +65,30 @@ export const webArtifactReadError = (
|
||||
context: WebArtifactReadContext,
|
||||
signal?: AbortSignal,
|
||||
): AnalysisError => {
|
||||
if (cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined) {
|
||||
const primary =
|
||||
signal?.aborted === true
|
||||
? new AnalysisCancelledError(context.operation, { cause })
|
||||
: new ArtifactOperationError(
|
||||
context.operation,
|
||||
cause.reason,
|
||||
cause.details,
|
||||
cause.message,
|
||||
{ cause },
|
||||
);
|
||||
return analysisErrorWithCleanupFailure(
|
||||
primary,
|
||||
new ProviderCleanupError(
|
||||
"captured-web-artifacts",
|
||||
cause.cleanup.resources,
|
||||
{ reason: cause.cleanup.reason },
|
||||
{ cause, operation: context.operation },
|
||||
),
|
||||
context.operation,
|
||||
);
|
||||
}
|
||||
if (signal?.aborted === true)
|
||||
return new AnalysisCancelledError(context.operation);
|
||||
return new AnalysisCancelledError(context.operation, { cause });
|
||||
const selection = context.callerSelected
|
||||
? selectedFileError(cause, context)
|
||||
: undefined;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { WebSourceLocationArtifactPort } from "../../application/WebSourceLocationPorts.js";
|
||||
import type { WebScriptArtifactPort } from "../../application/WebScriptArtifactPort.js";
|
||||
import type { ExecutionOptions } from "../../application/AnalysisProvider.js";
|
||||
import { ArtifactResourceScope } from "../../artifacts/ArtifactResourceScope.js";
|
||||
import { readStableArtifact } from "../../artifacts/readStableArtifact.js";
|
||||
import {
|
||||
WEB_SOURCE_MAP_LIMITS,
|
||||
@@ -12,14 +13,64 @@ import { decodeWebArtifact, webArtifactReadError } from "./WebArtifactReads.js";
|
||||
|
||||
/** Read one verified capture source and the caller-selected map, without fetching. */
|
||||
export class LocalWebSourceLocationArtifacts implements WebSourceLocationArtifactPort {
|
||||
readonly scripts: WebScriptArtifactPort;
|
||||
readonly #resources: ArtifactResourceScope;
|
||||
|
||||
constructor(
|
||||
readonly scripts: WebScriptArtifactPort = new LocalWebScriptArtifacts(
|
||||
"trace_web_source_location",
|
||||
),
|
||||
) {}
|
||||
scripts?: WebScriptArtifactPort,
|
||||
resources = scripts instanceof LocalWebScriptArtifacts
|
||||
? scripts.resources
|
||||
: new ArtifactResourceScope(),
|
||||
) {
|
||||
this.#resources = resources;
|
||||
this.scripts =
|
||||
scripts ??
|
||||
new LocalWebScriptArtifacts("trace_web_source_location", resources);
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
const closeScript =
|
||||
this.scripts instanceof LocalWebScriptArtifacts &&
|
||||
this.scripts.resources === this.#resources
|
||||
? undefined
|
||||
: this.scripts.close?.bind(this.scripts);
|
||||
const results = await Promise.allSettled([
|
||||
Promise.resolve().then(() => this.#resources.close()),
|
||||
...(closeScript === undefined
|
||||
? []
|
||||
: [Promise.resolve().then(closeScript)]),
|
||||
]);
|
||||
const failures = results.flatMap((result) =>
|
||||
result.status === "rejected" ? [result.reason] : [],
|
||||
);
|
||||
if (failures.length > 0)
|
||||
throw new AggregateError(
|
||||
failures,
|
||||
"Web source-location artifact cleanup failed",
|
||||
);
|
||||
}
|
||||
|
||||
/** Preserve actual map identity separately from the selected deployment URL. */
|
||||
async load(input: WebSourceLocationInput, options?: ExecutionOptions) {
|
||||
try {
|
||||
return await this.#resources.run(() => this.#load(input, options));
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
webArtifactReadError(
|
||||
cause,
|
||||
{
|
||||
operation: "trace_web_source_location",
|
||||
field: ["source_map", "path"],
|
||||
targetPath: input.source_map.path,
|
||||
callerSelected: true,
|
||||
},
|
||||
options?.signal,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async #load(input: WebSourceLocationInput, options?: ExecutionOptions) {
|
||||
const loaded = await this.scripts.load(input, options);
|
||||
if (!loaded.ok) return loaded;
|
||||
try {
|
||||
@@ -27,6 +78,8 @@ export class LocalWebSourceLocationArtifacts implements WebSourceLocationArtifac
|
||||
input.source_map.path,
|
||||
WEB_SOURCE_MAP_LIMITS.mapBytes,
|
||||
options?.signal,
|
||||
undefined,
|
||||
this.#resources,
|
||||
);
|
||||
return ok({
|
||||
...loaded.value,
|
||||
|
||||
Reference in New Issue
Block a user