fix(firmware): recover after workspace removal failures (#1616)
This commit is contained in:
@@ -324,7 +324,7 @@ it("cancels a waiting request promptly while keeping later launches behind the a
|
||||
await assertFirmwareCleanup(fixture.launches);
|
||||
});
|
||||
|
||||
it("retains an uncertain worker and prevents another launch", async () => {
|
||||
it("retains an uncertain worker's workspace and blocks later launches", async () => {
|
||||
const fixture = await firmwareFixture("cleanup-failure");
|
||||
const first = await fixture.service.execute("inspect_firmware_regions", {
|
||||
path: fixture.path,
|
||||
@@ -335,14 +335,22 @@ it("retains an uncertain worker and prevents another launch", async () => {
|
||||
});
|
||||
const workspace = fixture.launches[0]?.cwd;
|
||||
expect(workspace).toBeDefined();
|
||||
if (workspace === undefined) return;
|
||||
if (workspace === undefined) throw new Error("Expected retained workspace");
|
||||
await access(workspace);
|
||||
const originalLaunches = [...fixture.launches];
|
||||
const next = await fixture.service.execute("inspect_firmware_regions", {
|
||||
path: fixture.path,
|
||||
});
|
||||
expect(next).toMatchObject({ ok: false, error: { cleanupIncomplete: true } });
|
||||
expect(fixture.launches).toHaveLength(1);
|
||||
expect(fixture.launches).toEqual(originalLaunches);
|
||||
await access(workspace);
|
||||
fixture.restoreCleanup();
|
||||
const resumed = await fixture.service.execute("inspect_firmware_regions", {
|
||||
path: fixture.path,
|
||||
});
|
||||
if (!resumed.ok) throw resumed.error;
|
||||
expect(resumed.value.subject?.local_path).toBe(fixture.path);
|
||||
await assertFirmwareCleanup(fixture.launches);
|
||||
});
|
||||
|
||||
it("accepts another build on the verified release line and reports it", async () => {
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
import { access, chmod, readFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { expect, it } from "vitest";
|
||||
import { STDIO_DEFAULT_MAX_BUFFER_SIZE } from "@modelcontextprotocol/server";
|
||||
|
||||
import { toolContract } from "../../../src/contracts/toolContracts.js";
|
||||
import { firmwareResultSchemas } from "../../../src/domain/firmware/firmwareAnalysis.js";
|
||||
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
|
||||
import { ToolResultDelivery } from "../../../src/server/toolResult.js";
|
||||
import {
|
||||
assertFirmwareCleanup,
|
||||
firmwareFixture,
|
||||
} from "../../fixtures/firmware/provider.js";
|
||||
|
||||
it.skipIf(process.platform !== "linux" || process.getuid?.() === 0)(
|
||||
"retains completed extractions and retries all workspaces without blocking stopped-worker operations",
|
||||
async () => {
|
||||
const fixture = await firmwareFixture("workspace-cleanup-failure");
|
||||
const sourceBytes = await readFile(fixture.path);
|
||||
const sourceSha256 = createHash("sha256").update(sourceBytes).digest("hex");
|
||||
const workspaces = new Set<string>();
|
||||
try {
|
||||
for (const output of [
|
||||
fixture.output,
|
||||
join(fixture.root, "second-output"),
|
||||
]) {
|
||||
const extracted = await fixture.service.execute("extract_firmware", {
|
||||
path: fixture.path,
|
||||
output_directory: output,
|
||||
range: { offset: 2, length: 4 },
|
||||
});
|
||||
const workspace = fixture.launches.at(-1)?.cwd;
|
||||
if (workspace === undefined)
|
||||
throw new Error("Expected owned workspace");
|
||||
expect(workspaces.has(workspace)).toBe(false);
|
||||
workspaces.add(workspace);
|
||||
if (extracted.ok) throw new Error("Expected workspace removal failure");
|
||||
expect(extracted.error.cleanupIncomplete).toBe(true);
|
||||
const partial = extracted.error.partialObservation;
|
||||
if (
|
||||
partial === undefined ||
|
||||
!("kind" in partial) ||
|
||||
partial.kind !== "firmware"
|
||||
)
|
||||
throw new Error("Expected completed firmware observation");
|
||||
expect(partial.provenance).toMatchObject({
|
||||
subject: {
|
||||
format: "file",
|
||||
path: fixture.path,
|
||||
sha256: sourceSha256,
|
||||
},
|
||||
provider: { id: "unblob", version: "26.6.4" },
|
||||
raw_result: {
|
||||
report: expect.any(Array),
|
||||
execution: { exit_code: 0 },
|
||||
},
|
||||
locations: [{ kind: "file-offset-range", start: 2, end: 6 }],
|
||||
});
|
||||
const result = firmwareResultSchemas.extract_firmware.parse(
|
||||
partial.result,
|
||||
);
|
||||
expect(result.selection).toEqual({
|
||||
offset: 2,
|
||||
length: 4,
|
||||
sha256: createHash("sha256")
|
||||
.update(sourceBytes.subarray(2, 6))
|
||||
.digest("hex"),
|
||||
});
|
||||
const file = result.files[0];
|
||||
if (file === undefined)
|
||||
throw new Error("Expected published extracted file");
|
||||
expect(await readFile(file.path, "utf8")).toBe("firmware=true\n");
|
||||
expect(extracted.error.cleanupResources).toContain(workspace);
|
||||
await access(workspace);
|
||||
const delivered = new ToolResultDelivery(
|
||||
STDIO_DEFAULT_MAX_BUFFER_SIZE,
|
||||
).toCallToolResult(extracted, toolContract("extract_firmware"));
|
||||
expect(delivered.isError).toBe(true);
|
||||
expect(delivered.structuredContent).toMatchObject({
|
||||
error: {
|
||||
details: {
|
||||
partial_observation: {
|
||||
kind: "firmware",
|
||||
result,
|
||||
provenance: partial.provenance,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const inspected = await fixture.service.execute(
|
||||
"inspect_firmware_regions",
|
||||
{
|
||||
path: fixture.path,
|
||||
},
|
||||
);
|
||||
if (!inspected.ok) throw inspected.error;
|
||||
expect(inspected.value.subject?.local_path).toBe(fixture.path);
|
||||
expect(
|
||||
firmwareResultSchemas.inspect_firmware_regions.parse(
|
||||
inspected.value.normalized_result,
|
||||
).regions[0]?.signature,
|
||||
).toBe("gzip");
|
||||
}
|
||||
await expect(fixture.close()).rejects.toMatchObject({
|
||||
cleanupResources: expect.arrayContaining([...workspaces]),
|
||||
});
|
||||
for (const workspace of workspaces) await access(workspace);
|
||||
} finally {
|
||||
for (const workspace of workspaces)
|
||||
await chmod(join(workspace, "cleanup-blocked"), 0o700);
|
||||
await fixture.close();
|
||||
}
|
||||
await assertFirmwareCleanup(fixture.launches);
|
||||
expect(await readFile(fixture.path)).toEqual(sourceBytes);
|
||||
for (const output of [fixture.output, join(fixture.root, "second-output")])
|
||||
expect(await readFile(join(output, "rootfs/config"), "utf8")).toBe(
|
||||
"firmware=true\n",
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it.skipIf(process.platform !== "linux" || process.getuid?.() === 0)(
|
||||
"preserves the report failure alongside a workspace removal failure",
|
||||
async () => {
|
||||
const fixture = await firmwareFixture(
|
||||
"workspace-cleanup-failure-malformed",
|
||||
);
|
||||
let workspace: string | undefined;
|
||||
try {
|
||||
const failed = await fixture.service.execute("extract_firmware", {
|
||||
path: fixture.path,
|
||||
output_directory: fixture.output,
|
||||
});
|
||||
workspace = fixture.launches.at(-1)?.cwd;
|
||||
if (failed.ok || workspace === undefined)
|
||||
throw new Error("Expected both report and cleanup failures");
|
||||
expect(projectAnalysisError(failed.error)).toMatchObject({
|
||||
code: "cleanup_incomplete",
|
||||
details: {
|
||||
diagnostics: {
|
||||
primary_error: { code: "unreadable_output" },
|
||||
cleanup_error: {
|
||||
code: "cleanup_incomplete",
|
||||
details: {
|
||||
diagnostics: { reason: expect.stringContaining("EACCES") },
|
||||
},
|
||||
},
|
||||
},
|
||||
resources: expect.arrayContaining([workspace]),
|
||||
},
|
||||
});
|
||||
await expect(access(fixture.output)).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
} finally {
|
||||
if (workspace !== undefined)
|
||||
await chmod(join(workspace, "cleanup-blocked"), 0o700);
|
||||
await fixture.close();
|
||||
}
|
||||
await assertFirmwareCleanup(fixture.launches);
|
||||
},
|
||||
);
|
||||
Vendored
+13
-2
@@ -1,5 +1,5 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdir, readFile, writeFile, symlink } from "node:fs/promises";
|
||||
import { chmod, mkdir, readFile, writeFile, symlink } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
const args = process.argv.slice(2);
|
||||
const mode = process.env.REA_FIRMWARE_FIXTURE_MODE;
|
||||
@@ -125,8 +125,19 @@ if (args.includes("--version")) {
|
||||
];
|
||||
await writeFile(
|
||||
reportPath,
|
||||
mode === "malformed" ? "[" : JSON.stringify(records),
|
||||
mode === "malformed" || mode === "workspace-cleanup-failure-malformed"
|
||||
? "["
|
||||
: JSON.stringify(records),
|
||||
);
|
||||
if (mode === "dependency") process.exitCode = 1;
|
||||
if (
|
||||
mode === "workspace-cleanup-failure" ||
|
||||
mode === "workspace-cleanup-failure-malformed"
|
||||
) {
|
||||
const blocked = join(process.cwd(), "cleanup-blocked");
|
||||
await mkdir(blocked);
|
||||
await writeFile(join(blocked, "retained"), "owned workspace bytes");
|
||||
await chmod(blocked, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+22
-8
@@ -5,6 +5,7 @@ import { expect, onTestFinished } from "vitest";
|
||||
import { createFirmwareAnalysisProvider } from "../../../src/composition/firmware.js";
|
||||
import { FirmwareAnalysisService } from "../../../src/application/firmware/FirmwareAnalysisService.js";
|
||||
import { spawnOwnedProviderProcess } from "../../../src/process/ProviderProcess.js";
|
||||
import { cleanupOwnedProcessGroup } from "../../../src/process/ProcessOwnership.js";
|
||||
import { createTestTempDirectory } from "../temporaryDirectory.js";
|
||||
|
||||
/** Exercise real subprocess ownership with synthetic pinned producer representations. */
|
||||
@@ -18,6 +19,7 @@ export const firmwareFixture = async (mode = "normal", banner?: string) => {
|
||||
pid: number | undefined;
|
||||
args: readonly string[];
|
||||
}[] = [];
|
||||
let cleanupUnverified = mode === "cleanup-failure";
|
||||
const provider = createFirmwareAnalysisProvider(
|
||||
{
|
||||
...process.env,
|
||||
@@ -54,21 +56,29 @@ export const firmwareFixture = async (mode = "normal", banner?: string) => {
|
||||
args: options.arguments,
|
||||
});
|
||||
if (mode === "cleanup-failure") {
|
||||
onTestFinished(async () => {
|
||||
if (options.cwd !== undefined)
|
||||
await rm(options.cwd, { recursive: true, force: true });
|
||||
});
|
||||
return {
|
||||
...spawned,
|
||||
cleanup: async () => ({
|
||||
cleaned: false,
|
||||
reason: "injected ownership failure",
|
||||
}),
|
||||
cleanup: () =>
|
||||
cleanupUnverified
|
||||
? Promise.resolve({
|
||||
cleaned: false,
|
||||
reason: "injected ownership failure",
|
||||
})
|
||||
: cleanupOwnedProcessGroup(spawned.ownership),
|
||||
};
|
||||
}
|
||||
return spawned;
|
||||
},
|
||||
);
|
||||
const close = async () => {
|
||||
if (provider.close === undefined)
|
||||
throw new Error("Expected firmware provider cleanup capability");
|
||||
await provider.close();
|
||||
};
|
||||
onTestFinished(async () => {
|
||||
cleanupUnverified = false;
|
||||
await close();
|
||||
});
|
||||
return {
|
||||
provider,
|
||||
service: new FirmwareAnalysisService(provider),
|
||||
@@ -76,6 +86,10 @@ export const firmwareFixture = async (mode = "normal", banner?: string) => {
|
||||
output: join(root, "output"),
|
||||
root,
|
||||
launches,
|
||||
close,
|
||||
restoreCleanup: () => {
|
||||
cleanupUnverified = false;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user