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:
@@ -7,11 +7,12 @@ import {
|
||||
} from "../composition/binaryDiagnostics.js";
|
||||
import { CLI_COMMANDS } from "../cliCommandNames.js";
|
||||
import { logCliCommand } from "../cliLogging.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import type { Logger } from "pino";
|
||||
import type { CliInstance } from "./types.js";
|
||||
import { withCommandCancellation } from "./commandCancellation.js";
|
||||
import { inspectPeResourcesInputSchema } from "../domain/native/peResources.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import { finishAnalysisCommand } from "./finishAnalysisCommand.js";
|
||||
|
||||
/** Inspect operator-selected files through the same shared workflow as MCP. */
|
||||
export const registerBinaryDiagnosticsCommands = (
|
||||
@@ -19,7 +20,6 @@ export const registerBinaryDiagnosticsCommands = (
|
||||
logger: Logger,
|
||||
environment: Readonly<NodeJS.ProcessEnv>,
|
||||
): void => {
|
||||
const pe = createPeResourcesService();
|
||||
cli.command(CLI_COMMANDS.inspectPeResources, {
|
||||
description:
|
||||
"Inspect PE resource identities, payload ranges, hashes and icon-group relationships",
|
||||
@@ -29,19 +29,28 @@ export const registerBinaryDiagnosticsCommands = (
|
||||
maxEntries: inspectPeResourcesInputSchema.shape.max_entries,
|
||||
}),
|
||||
run: ({ args, options }) =>
|
||||
withCommandCancellation((signal) =>
|
||||
logCliCommand(logger, CLI_COMMANDS.inspectPeResources, async () => {
|
||||
const result = await pe.inspect(
|
||||
{
|
||||
path: resolve(args.path),
|
||||
max_file_bytes: options.maxFileBytes,
|
||||
max_entries: options.maxEntries,
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
return result.ok ? result.value : projectAnalysisError(result.error);
|
||||
}),
|
||||
),
|
||||
withCommandCancellation(async (signal) => {
|
||||
const pe = createPeResourcesService();
|
||||
return await logCliCommand(
|
||||
logger,
|
||||
CLI_COMMANDS.inspectPeResources,
|
||||
async () =>
|
||||
finishAnalysisCommand(
|
||||
() =>
|
||||
pe.inspect(
|
||||
{
|
||||
path: resolve(args.path),
|
||||
max_file_bytes: options.maxFileBytes,
|
||||
max_entries: options.maxEntries,
|
||||
},
|
||||
{ signal },
|
||||
),
|
||||
pe,
|
||||
"rea-pe-resources",
|
||||
"inspect_pe_resources",
|
||||
),
|
||||
);
|
||||
}),
|
||||
});
|
||||
const service = createBinaryLayoutService(environment);
|
||||
cli.command(CLI_COMMANDS.inspectBinaryLayout, {
|
||||
|
||||
+22
-43
@@ -1,9 +1,6 @@
|
||||
import { inspectPacketFieldsInputSchema } from "../domain/captures/packetFields.js";
|
||||
import { z } from "incur";
|
||||
import type { PacketCaptureService } from "../application/captures/PacketCaptureService.js";
|
||||
import { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js";
|
||||
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
||||
import { resolve } from "node:path";
|
||||
import { createPacketCaptureService } from "../composition/captures.js";
|
||||
import {
|
||||
@@ -13,10 +10,10 @@ import {
|
||||
} from "../domain/captures/packetCapture.js";
|
||||
import { CLI_COMMANDS } from "../cliCommandNames.js";
|
||||
import { logCliCommand } from "../cliLogging.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import type { Logger } from "pino";
|
||||
import type { CliInstance } from "./types.js";
|
||||
import { withCommandCancellation } from "./commandCancellation.js";
|
||||
import { finishAnalysisCommand } from "./finishAnalysisCommand.js";
|
||||
export const registerCaptureCommands = (
|
||||
cli: CliInstance,
|
||||
logger: Logger,
|
||||
@@ -26,32 +23,6 @@ export const registerCaptureCommands = (
|
||||
"inspect" | "inspectFields" | "close"
|
||||
> = createPacketCaptureService(environment),
|
||||
): void => {
|
||||
const finish = async (
|
||||
result: Awaited<ReturnType<PacketCaptureService["inspect"]>>,
|
||||
operation: string,
|
||||
) => {
|
||||
// A one-shot command must retry retained cleanup before discarding its owner.
|
||||
try {
|
||||
await service.close();
|
||||
} catch (cause) {
|
||||
const cleanup =
|
||||
cause instanceof AnalysisError
|
||||
? cause
|
||||
: new ProviderCleanupError(
|
||||
"tshark-capture",
|
||||
[],
|
||||
{ reason: String(cause) },
|
||||
{ cause, operation: operation },
|
||||
);
|
||||
if (result.ok) cleanup.retainPartialObservation(result.value);
|
||||
return projectAnalysisError(
|
||||
result.ok
|
||||
? cleanup
|
||||
: analysisErrorWithCleanupFailure(result.error, cleanup, operation),
|
||||
);
|
||||
}
|
||||
return result.ok ? result.value : projectAnalysisError(result.error);
|
||||
};
|
||||
cli.command(CLI_COMMANDS.inspectPacketFields, {
|
||||
description: "Search installed TShark protocol and field definitions",
|
||||
options: z
|
||||
@@ -80,11 +51,14 @@ export const registerCaptureCommands = (
|
||||
run: ({ options: { all, ...options } }) =>
|
||||
withCommandCancellation((signal) =>
|
||||
logCliCommand(logger, CLI_COMMANDS.inspectPacketFields, async () =>
|
||||
finish(
|
||||
await service.inspectFields(
|
||||
{ ...options, ...(all ? { limit: null } : {}) },
|
||||
{ signal },
|
||||
),
|
||||
finishAnalysisCommand(
|
||||
() =>
|
||||
service.inspectFields(
|
||||
{ ...options, ...(all ? { limit: null } : {}) },
|
||||
{ signal },
|
||||
),
|
||||
service,
|
||||
"tshark-capture",
|
||||
"inspect_packet_fields",
|
||||
),
|
||||
),
|
||||
@@ -125,15 +99,20 @@ export const registerCaptureCommands = (
|
||||
run: ({ args, options: { all_matches, ...options } }) =>
|
||||
withCommandCancellation((signal) =>
|
||||
logCliCommand(logger, CLI_COMMANDS.inspectPacketCapture, async () => {
|
||||
const result = await service.inspect(
|
||||
{
|
||||
...options,
|
||||
...(all_matches ? { match_limit: null } : {}),
|
||||
path: resolve(args.path),
|
||||
},
|
||||
{ signal },
|
||||
return finishAnalysisCommand(
|
||||
() =>
|
||||
service.inspect(
|
||||
{
|
||||
...options,
|
||||
...(all_matches ? { match_limit: null } : {}),
|
||||
path: resolve(args.path),
|
||||
},
|
||||
{ signal },
|
||||
),
|
||||
service,
|
||||
"tshark-capture",
|
||||
"inspect_packet_capture",
|
||||
);
|
||||
return finish(result, "inspect_packet_capture");
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
|
||||
import {
|
||||
AnalysisError,
|
||||
type AnalysisPartialObservation,
|
||||
} from "../domain/analysisErrorBase.js";
|
||||
import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
|
||||
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
||||
import type { Result } from "../domain/result.js";
|
||||
|
||||
/** Finish a one-shot CLI operation without discarding its primary result on close failure. */
|
||||
export const finishAnalysisCommand = async <
|
||||
Value extends AnalysisPartialObservation,
|
||||
>(
|
||||
run: () => Promise<Result<Value, AnalysisError>>,
|
||||
service: { close(): Promise<void> },
|
||||
providerId: string,
|
||||
canonicalOperation: string,
|
||||
fallbackResource?: string,
|
||||
): Promise<Value | ReturnType<typeof projectAnalysisError>> => {
|
||||
let result: Result<Value, AnalysisError>;
|
||||
try {
|
||||
result = await run();
|
||||
} catch (cause) {
|
||||
result = {
|
||||
ok: false,
|
||||
error:
|
||||
cause instanceof AnalysisError
|
||||
? cause
|
||||
: new ProviderAdapterError(providerId, canonicalOperation, { cause }),
|
||||
};
|
||||
}
|
||||
try {
|
||||
await service.close();
|
||||
} catch (cause) {
|
||||
let cleanup: AnalysisError;
|
||||
if (cause instanceof AnalysisError) cleanup = cause;
|
||||
else {
|
||||
const observation =
|
||||
cause instanceof ArtifactReaderFailure
|
||||
? ArtifactReaderFailure.cleanupObservation(
|
||||
cause,
|
||||
fallbackResource ?? `${canonicalOperation} resources`,
|
||||
)
|
||||
: {
|
||||
reason: cause instanceof Error ? cause.message : String(cause),
|
||||
resources: [],
|
||||
};
|
||||
cleanup = new ProviderCleanupError(
|
||||
providerId,
|
||||
observation.resources,
|
||||
{ reason: observation.reason },
|
||||
{ cause, operation: canonicalOperation },
|
||||
);
|
||||
}
|
||||
if (result.ok) cleanup.retainPartialObservation(result.value);
|
||||
return projectAnalysisError(
|
||||
result.ok
|
||||
? cleanup
|
||||
: analysisErrorWithCleanupFailure(
|
||||
result.error,
|
||||
cleanup,
|
||||
canonicalOperation,
|
||||
),
|
||||
);
|
||||
}
|
||||
return result.ok ? result.value : projectAnalysisError(result.error);
|
||||
};
|
||||
+15
-11
@@ -3,14 +3,13 @@ import { resolve } from "node:path";
|
||||
import { createGoBinaryService } from "../composition/go.js";
|
||||
import { CLI_COMMANDS } from "../cliCommandNames.js";
|
||||
import { logCliCommand } from "../cliLogging.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import type { Logger } from "pino";
|
||||
import type { CliInstance } from "./types.js";
|
||||
import { withCommandCancellation } from "./commandCancellation.js";
|
||||
import { finishAnalysisCommand } from "./finishAnalysisCommand.js";
|
||||
|
||||
/** Resolve caller paths and inspect static build metadata through the shared workflow. */
|
||||
export const registerGoCommands = (cli: CliInstance, logger: Logger): void => {
|
||||
const service = createGoBinaryService();
|
||||
cli.command(CLI_COMMANDS.inspectGoBinary, {
|
||||
description:
|
||||
"Inspect embedded Go compiler, modules and build settings without target execution",
|
||||
@@ -18,14 +17,19 @@ export const registerGoCommands = (cli: CliInstance, logger: Logger): void => {
|
||||
path: z.string().describe("Local ELF, PE or thin Mach-O image"),
|
||||
}),
|
||||
run: ({ args }) =>
|
||||
withCommandCancellation((signal) =>
|
||||
logCliCommand(logger, CLI_COMMANDS.inspectGoBinary, async () => {
|
||||
const result = await service.inspect(
|
||||
{ path: resolve(args.path) },
|
||||
{ signal },
|
||||
);
|
||||
return result.ok ? result.value : projectAnalysisError(result.error);
|
||||
}),
|
||||
),
|
||||
withCommandCancellation(async (signal) => {
|
||||
const service = createGoBinaryService();
|
||||
return await logCliCommand(
|
||||
logger,
|
||||
CLI_COMMANDS.inspectGoBinary,
|
||||
async () =>
|
||||
finishAnalysisCommand(
|
||||
() => service.inspect({ path: resolve(args.path) }, { signal }),
|
||||
service,
|
||||
"go-build-info",
|
||||
"inspect_go_binary",
|
||||
),
|
||||
);
|
||||
}),
|
||||
});
|
||||
};
|
||||
|
||||
+22
-43
@@ -1,9 +1,6 @@
|
||||
import { inspectSignalDecodersInputSchema } from "../domain/signals/signalDecoders.js";
|
||||
import { z } from "incur";
|
||||
import type { SignalRecordingService } from "../application/signals/SignalRecordingService.js";
|
||||
import { AnalysisError } from "../domain/analysisErrorBase.js";
|
||||
import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js";
|
||||
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
||||
import { resolve } from "node:path";
|
||||
import { createSignalRecordingService } from "../composition/signals.js";
|
||||
import {
|
||||
@@ -13,10 +10,10 @@ import {
|
||||
} from "../domain/signals/signalRecording.js";
|
||||
import { CLI_COMMANDS } from "../cliCommandNames.js";
|
||||
import { logCliCommand } from "../cliLogging.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import type { Logger } from "pino";
|
||||
import type { CliInstance } from "./types.js";
|
||||
import { withCommandCancellation } from "./commandCancellation.js";
|
||||
import { finishAnalysisCommand } from "./finishAnalysisCommand.js";
|
||||
export const registerSignalCommands = (
|
||||
cli: CliInstance,
|
||||
logger: Logger,
|
||||
@@ -26,32 +23,6 @@ export const registerSignalCommands = (
|
||||
"decode" | "inspectDecoders" | "close"
|
||||
> = createSignalRecordingService(environment),
|
||||
): void => {
|
||||
const finish = async (
|
||||
result: Awaited<ReturnType<SignalRecordingService["decode"]>>,
|
||||
operation: string,
|
||||
) => {
|
||||
// A one-shot command must retry retained cleanup before discarding its owner.
|
||||
try {
|
||||
await service.close();
|
||||
} catch (cause) {
|
||||
const cleanup =
|
||||
cause instanceof AnalysisError
|
||||
? cause
|
||||
: new ProviderCleanupError(
|
||||
"sigrok-recording",
|
||||
[],
|
||||
{ reason: String(cause) },
|
||||
{ cause, operation: operation },
|
||||
);
|
||||
if (result.ok) cleanup.retainPartialObservation(result.value);
|
||||
return projectAnalysisError(
|
||||
result.ok
|
||||
? cleanup
|
||||
: analysisErrorWithCleanupFailure(result.error, cleanup, operation),
|
||||
);
|
||||
}
|
||||
return result.ok ? result.value : projectAnalysisError(result.error);
|
||||
};
|
||||
cli.command(CLI_COMMANDS.inspectSignalDecoders, {
|
||||
description: "Search installed sigrok decoders or selected decoder help",
|
||||
options: z
|
||||
@@ -82,11 +53,14 @@ export const registerSignalCommands = (
|
||||
run: ({ options: { all, ...options } }) =>
|
||||
withCommandCancellation((signal) =>
|
||||
logCliCommand(logger, CLI_COMMANDS.inspectSignalDecoders, async () =>
|
||||
finish(
|
||||
await service.inspectDecoders(
|
||||
{ ...options, ...(all ? { limit: null } : {}) },
|
||||
{ signal },
|
||||
),
|
||||
finishAnalysisCommand(
|
||||
() =>
|
||||
service.inspectDecoders(
|
||||
{ ...options, ...(all ? { limit: null } : {}) },
|
||||
{ signal },
|
||||
),
|
||||
service,
|
||||
"sigrok-recording",
|
||||
"inspect_signal_decoders",
|
||||
),
|
||||
),
|
||||
@@ -127,15 +101,20 @@ export const registerSignalCommands = (
|
||||
run: ({ args, options: { all_lines, ...options } }) =>
|
||||
withCommandCancellation((signal) =>
|
||||
logCliCommand(logger, CLI_COMMANDS.decodeSignalRecording, async () => {
|
||||
const result = await service.decode(
|
||||
{
|
||||
...options,
|
||||
...(all_lines ? { line_limit: null } : {}),
|
||||
path: resolve(args.path),
|
||||
},
|
||||
{ signal },
|
||||
return finishAnalysisCommand(
|
||||
() =>
|
||||
service.decode(
|
||||
{
|
||||
...options,
|
||||
...(all_lines ? { line_limit: null } : {}),
|
||||
path: resolve(args.path),
|
||||
},
|
||||
{ signal },
|
||||
),
|
||||
service,
|
||||
"sigrok-recording",
|
||||
"decode_signal_recording",
|
||||
);
|
||||
return finish(result, "decode_signal_recording");
|
||||
}),
|
||||
),
|
||||
});
|
||||
|
||||
@@ -3,10 +3,10 @@ import { WEB_NETWORK_CAPTURE_FORMATS } from "../domain/webNetworkCapture.js";
|
||||
import { createWebNetworkCaptureService } from "../composition/webNetworkCaptures.js";
|
||||
import { CLI_COMMANDS } from "../cliCommandNames.js";
|
||||
import { logCliCommand } from "../cliLogging.js";
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import type { Logger } from "pino";
|
||||
import type { CliInstance } from "./types.js";
|
||||
import { withCommandCancellation } from "./commandCancellation.js";
|
||||
import { finishAnalysisCommand } from "./finishAnalysisCommand.js";
|
||||
|
||||
/** Expose offline historical capture inspection with the same selection contract as MCP. */
|
||||
export const registerWebNetworkCaptureCommands = (
|
||||
@@ -42,20 +42,24 @@ export const registerWebNetworkCaptureCommands = (
|
||||
logger,
|
||||
CLI_COMMANDS.inspectWebNetworkCapture,
|
||||
async () => {
|
||||
const result = await service.inspect(
|
||||
{
|
||||
capture_path: args.capturePath,
|
||||
format: args.format,
|
||||
sensitive_values: options.sensitiveValue,
|
||||
...(options.record === undefined
|
||||
? {}
|
||||
: { record_ordinals: options.record }),
|
||||
},
|
||||
{ signal },
|
||||
return finishAnalysisCommand(
|
||||
() =>
|
||||
service.inspect(
|
||||
{
|
||||
capture_path: args.capturePath,
|
||||
format: args.format,
|
||||
sensitive_values: options.sensitiveValue,
|
||||
...(options.record === undefined
|
||||
? {}
|
||||
: { record_ordinals: options.record }),
|
||||
},
|
||||
{ signal },
|
||||
),
|
||||
service,
|
||||
"web-network-capture",
|
||||
"inspect_web_network_capture",
|
||||
"Web network capture resources",
|
||||
);
|
||||
return result.ok
|
||||
? result.value
|
||||
: projectAnalysisError(result.error);
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user