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:
@@ -1,6 +1,6 @@
|
||||
import { STDIO_DEFAULT_MAX_BUFFER_SIZE } from "@modelcontextprotocol/server";
|
||||
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
|
||||
import { expect, it as test } from "vitest";
|
||||
import { expect, it as test, vi } from "vitest";
|
||||
import {
|
||||
firmwareFixture,
|
||||
assertFirmwareCleanup,
|
||||
@@ -9,8 +9,45 @@ import { firmwareResultSchemas } from "../../../src/domain/firmware/firmwareAnal
|
||||
import { FirmwareAnalysisService } from "../../../src/application/firmware/FirmwareAnalysisService.js";
|
||||
import { FirmwareProvider } from "../../../src/firmware/FirmwareProvider.js";
|
||||
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
|
||||
import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js";
|
||||
import { toolContract } from "../../../src/contracts/toolContracts.js";
|
||||
import { ToolResultDelivery } from "../../../src/server/toolResult.js";
|
||||
import { SafeOutputTree } from "../../../src/artifacts/SafeOutputTree.js";
|
||||
import { SafeOutputTreeCreationFailure } from "../../../src/artifacts/SafeOutputTreeCreationFailure.js";
|
||||
|
||||
const directoryCloseFailure = vi.hoisted(() => ({
|
||||
mode: "off" as "off" | "transient" | "persistent",
|
||||
attempts: 0,
|
||||
}));
|
||||
|
||||
vi.mock("node:fs/promises", async (importOriginal) => {
|
||||
const filesystem = await importOriginal<typeof import("node:fs/promises")>();
|
||||
return {
|
||||
...filesystem,
|
||||
open: async (...args: Parameters<typeof filesystem.open>) => {
|
||||
const handle = await filesystem.open(...args);
|
||||
if (
|
||||
directoryCloseFailure.mode !== "off" &&
|
||||
(await handle.stat()).isDirectory()
|
||||
) {
|
||||
const close = handle.close.bind(handle);
|
||||
let attempted = false;
|
||||
handle.close = async () => {
|
||||
directoryCloseFailure.attempts += 1;
|
||||
if (
|
||||
directoryCloseFailure.mode === "persistent" ||
|
||||
(directoryCloseFailure.mode === "transient" && !attempted)
|
||||
) {
|
||||
attempted = true;
|
||||
throw new Error("injected published directory close failure");
|
||||
}
|
||||
await close();
|
||||
};
|
||||
}
|
||||
return handle;
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const delivery = new ToolResultDelivery(STDIO_DEFAULT_MAX_BUFFER_SIZE);
|
||||
|
||||
@@ -111,6 +148,146 @@ it("extracts a selected interval with verified file identity and parent-byte lin
|
||||
await assertFirmwareCleanup(fixture.launches);
|
||||
});
|
||||
|
||||
it("retains a setup-failed publication tree for provider cleanup retry", async () => {
|
||||
const fixture = await firmwareFixture();
|
||||
const createTree = SafeOutputTree.create.bind(SafeOutputTree);
|
||||
let cleanupAttempts = 0;
|
||||
const create = vi
|
||||
.spyOn(SafeOutputTree, "create")
|
||||
.mockImplementationOnce(async (outputRoot, platform) => {
|
||||
const tree = await createTree(outputRoot, platform);
|
||||
const rollbackTree = tree.rollback.bind(tree);
|
||||
vi.spyOn(tree, "rollback").mockImplementation(async () => {
|
||||
cleanupAttempts += 1;
|
||||
if (cleanupAttempts === 1)
|
||||
throw new Error("injected first cleanup failure");
|
||||
return rollbackTree();
|
||||
});
|
||||
throw new SafeOutputTreeCreationFailure(
|
||||
new Error("injected post-acquisition setup failure"),
|
||||
tree,
|
||||
);
|
||||
});
|
||||
try {
|
||||
const result = await fixture.service.execute("extract_firmware", {
|
||||
path: fixture.path,
|
||||
output_directory: fixture.output,
|
||||
range: { offset: 2, length: 4 },
|
||||
});
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
cleanupIncomplete: true,
|
||||
diagnostics: {
|
||||
previous_error: expect.stringContaining(
|
||||
"injected post-acquisition setup failure",
|
||||
),
|
||||
},
|
||||
},
|
||||
});
|
||||
await expect(access(fixture.output)).resolves.toBeUndefined();
|
||||
} finally {
|
||||
create.mockRestore();
|
||||
}
|
||||
await fixture.close();
|
||||
expect(cleanupAttempts).toBe(2);
|
||||
await expect(access(fixture.output)).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
});
|
||||
|
||||
it("returns firmware output after a transient published-directory close failure", async () => {
|
||||
const fixture = await firmwareFixture();
|
||||
const closeFailure = injectPublishedDirectoryCloseFailure(false);
|
||||
try {
|
||||
const result = await fixture.service.execute("extract_firmware", {
|
||||
path: fixture.path,
|
||||
output_directory: fixture.output,
|
||||
range: { offset: 2, length: 4 },
|
||||
});
|
||||
if (!result.ok) throw result.error;
|
||||
const normalized = firmwareResultSchemas.extract_firmware.parse(
|
||||
result.value.normalized_result,
|
||||
);
|
||||
expect(normalized.output_directory).toBe(fixture.output);
|
||||
expect(await readFile(normalized.files[0]?.path ?? "", "utf8")).toBe(
|
||||
"firmware=true\n",
|
||||
);
|
||||
expect(closeFailure.attempts()).toBeGreaterThanOrEqual(4);
|
||||
} finally {
|
||||
closeFailure.restore();
|
||||
}
|
||||
});
|
||||
|
||||
it("retains the complete firmware result when published output cleanup stays uncertain", async () => {
|
||||
const fixture = await firmwareFixture();
|
||||
const closeFailure = injectPublishedDirectoryCloseFailure(true);
|
||||
try {
|
||||
const result = await fixture.service.execute("extract_firmware", {
|
||||
path: fixture.path,
|
||||
output_directory: fixture.output,
|
||||
range: { offset: 2, length: 4 },
|
||||
});
|
||||
expect(result).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
cleanupIncomplete: true,
|
||||
partialObservation: {
|
||||
kind: "firmware",
|
||||
result: {
|
||||
output_directory: fixture.output,
|
||||
files: [
|
||||
expect.objectContaining({
|
||||
relative_path: "rootfs/config",
|
||||
size: 14,
|
||||
}),
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
if (result.ok) throw new Error("Expected unresolved descriptor cleanup");
|
||||
expect(result.error).toBeInstanceOf(ProviderCleanupError);
|
||||
expect(projectAnalysisError(result.error)).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
details: {
|
||||
partial_observation: {
|
||||
kind: "firmware",
|
||||
result: { output_directory: fixture.output },
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(await readFile(`${fixture.output}/rootfs/config`, "utf8")).toBe(
|
||||
"firmware=true\n",
|
||||
);
|
||||
expect(closeFailure.attempts()).toBeGreaterThanOrEqual(4);
|
||||
} finally {
|
||||
closeFailure.restore();
|
||||
}
|
||||
await fixture.close();
|
||||
expect(await readFile(`${fixture.output}/rootfs/config`, "utf8")).toBe(
|
||||
"firmware=true\n",
|
||||
);
|
||||
});
|
||||
|
||||
const injectPublishedDirectoryCloseFailure = (persistent: boolean) => {
|
||||
directoryCloseFailure.attempts = 0;
|
||||
const commit = SafeOutputTree.prototype.commit;
|
||||
const spy = vi
|
||||
.spyOn(SafeOutputTree.prototype, "commit")
|
||||
.mockImplementation(async function (this: SafeOutputTree) {
|
||||
directoryCloseFailure.mode = persistent ? "persistent" : "transient";
|
||||
await commit.call(this);
|
||||
});
|
||||
return {
|
||||
attempts: () => directoryCloseFailure.attempts,
|
||||
restore: () => {
|
||||
directoryCloseFailure.mode = "off";
|
||||
spy.mockRestore();
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
it.each(["depth", "dependency", "link"])(
|
||||
"reports partial extraction for %s without losing inline results",
|
||||
async (mode) => {
|
||||
|
||||
Reference in New Issue
Block a user