fix(publication)!: retain cleanup owners across export workflows

Require an ArtifactResourceScope for web script exports and share its lifetime with CLI or MCP shutdown. Retain setup-failed output trees in firmware and Wakaru providers. Preserve completed publications when descriptor cleanup fails, returning the full result after a successful retry or as a partial observation while cleanup remains unresolved.
This commit is contained in:
morluto
2026-10-11 05:17:10 +08:00
parent 3f8c51e9b5
commit be75ed5e4b
15 changed files with 801 additions and 166 deletions
+29 -1
View File
@@ -25,6 +25,7 @@ import { jsonValueSchema } from "../domain/jsonValue.js";
import { err, ok } from "../domain/result.js";
import { PrivateRuntimeRoot } from "../process/PrivateRuntimeRoot.js";
import type { ProviderProcessSupervisor } from "../process/ProviderProcess.js";
import type { SafeOutputTree } from "../artifacts/SafeOutputTree.js";
import { publishFirmwareExtraction } from "./FirmwarePublication.js";
import {
FIRMWARE_LIMITS,
@@ -48,6 +49,7 @@ import {
type Outcome = Awaited<ReturnType<FirmwareAnalysisPort["execute"]>>;
interface PendingFirmwareCleanup {
supervisor?: ProviderProcessSupervisor;
tree?: SafeOutputTree;
readonly root: PrivateRuntimeRoot;
readonly engine: string;
readonly operation: string;
@@ -183,9 +185,24 @@ export class FirmwareProvider implements FirmwareAnalysisPort {
);
continue;
}
try {
if (pending.tree !== undefined) {
await pending.tree.rollback();
delete pending.tree;
}
} catch (cause: unknown) {
failures.push(
new ProviderCleanupError(
pending.engine,
[path, pending.tree?.outputRoot ?? path],
{ reason: cause instanceof Error ? cause.message : String(cause) },
{ operation: pending.operation, cause },
),
);
}
try {
await pending.root.close();
this.#pendingCleanup.delete(path);
if (pending.tree === undefined) this.#pendingCleanup.delete(path);
} catch (cause: unknown) {
// A verified stopped process cannot use this root. Keep ownership for
// later removal without blocking a new operation's disjoint workspace.
@@ -366,6 +383,17 @@ export class FirmwareProvider implements FirmwareAnalysisPort {
selection: target.selection,
engine,
signal,
retainCleanup: (tree) => {
const pending: PendingFirmwareCleanup = this.#pendingCleanup.get(
ownedRoot.path,
) ?? {
root: ownedRoot,
engine: engineName,
operation: request.operation,
};
pending.tree = tree;
this.#pendingCleanup.set(ownedRoot.path, pending);
},
});
raw = { report, execution: processRun, version_execution: versionRun };
}
+24 -2
View File
@@ -2,8 +2,10 @@ import { createReadStream } from "node:fs";
import { join } from "node:path";
import type { z } from "zod";
import { SafeOutputTree } from "../artifacts/SafeOutputTree.js";
import { SafeOutputTreeCreationFailure } from "../artifacts/SafeOutputTreeCreationFailure.js";
import { AnalysisOutputError } from "../domain/analysisErrorCore.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { jsonValueSchema } from "../domain/jsonValue.js";
import { firmwareResultSchemas } from "../domain/firmware/firmwareAnalysis.js";
import { hashFirmwareFile, type FirmwareEntry } from "./FirmwareFiles.js";
import type { normalizeUnblobReport } from "./FirmwareReports.js";
@@ -16,8 +18,12 @@ export const publishFirmwareExtraction = async (context: {
selection: { offset: number; length: number; sha256: string };
engine: z.infer<typeof firmwareResultSchemas.extract_firmware>["engine"];
signal: AbortSignal;
retainCleanup: (tree: SafeOutputTree) => void;
}) => {
let tree: SafeOutputTree | undefined;
let result:
| z.infer<typeof firmwareResultSchemas.extract_firmware>
| undefined;
try {
const materialized = new Set(
context.entries
@@ -75,7 +81,7 @@ export const publishFirmwareExtraction = async (context: {
original_file_range: null,
});
}
const result = firmwareResultSchemas.extract_firmware.parse({
result = firmwareResultSchemas.extract_firmware.parse({
engine: context.engine,
selection: context.selection,
output_directory: tree.outputRoot,
@@ -93,9 +99,18 @@ export const publishFirmwareExtraction = async (context: {
await tree.commit();
return result;
} catch (cause: unknown) {
if (cause instanceof SafeOutputTreeCreationFailure) tree = cause.tree;
try {
await tree?.rollback();
} catch (cleanupCause: unknown) {
if (tree !== undefined) context.retainCleanup(tree);
const publishedResult =
tree?.published === true && result !== undefined
? {
kind: "firmware" as const,
result: jsonValueSchema.parse(result),
}
: undefined;
throw new ProviderCleanupError(
"unblob",
[context.outputDirectory],
@@ -107,9 +122,16 @@ export const publishFirmwareExtraction = async (context: {
previous_error:
cause instanceof Error ? cause.message : String(cause),
},
{ cause: cleanupCause },
{
cause: cleanupCause,
operation: "extract_firmware",
...(publishedResult === undefined
? {}
: { partialObservation: publishedResult }),
},
);
}
if (tree?.published === true && result !== undefined) return result;
throw cause;
}
};