Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
fix(evidence): clear staged write cleanup state after a confirmed retry (#5852)
Kaoru0822c2bee63e998c
diff --git a/src/application/EvidenceBundleCommands.ts b/src/application/EvidenceBundleCommands.tsindex 1255ea68f..036f1e21d 100644--- a/src/application/EvidenceBundleCommands.ts+++ b/src/application/EvidenceBundleCommands.ts@@ -46,15 +46,11 @@ export const exportEvidenceBundleCommand = async ();if (written.ok) return projectWrite(loaded.value, written);- let cleanupSucceeded = false;- try {- await resources.close();- cleanupSucceeded = true;- } catch {- // The original error cause retains the same staged-output owner for diagnostics.- }+ // Confirmed release resolves the bound error to its primary outcome; a+ // failed retry leaves that error incomplete with its exact retained owner.+ await resources.close().catch(() => undefined);if (- cleanupSucceeded &&+ !written.error.cleanupIncomplete &&written.error instanceof EvidenceFileError &&written.error.published) {diff --git a/src/application/JsonFiles.ts b/src/application/JsonFiles.tsindex 2e81dd8fc..1ae2b2b32 100644--- a/src/application/JsonFiles.ts+++ b/src/application/JsonFiles.ts@@ -29,6 +29,11 @@ import { NonRegularFileReadError } from "../filesystem/RegularFile.js";import { RegularFileCleanupFailure } from "./RegularFileRead.js";import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";+import { bindArtifactFileCleanup } from "../artifacts/ArtifactFileOperation.js";+import {+ type AnalysisCleanupLease,+ AnalysisCleanupLeaseGroup,+} from "../artifacts/AnalysisCleanupLease.js";import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js";import { OwnedStagedOutput } from "../filesystem/OwnedStagedOutput.js";import { absoluteCallerPath } from "../filesystem/callerPath.js";@@ -212,13 +217,14 @@ export function writeTextParts(cause instanceof ArtifactReaderFailure && cause.cleanup !== undefined? cause.cleanup: undefined;- return err(- new EvidenceFileError("write", "io", {- cause,- path: absoluteCallerPath(path),- ...(cleanup === undefined ? {} : { cleanup }),- }),- );+ const error = new EvidenceFileError("write", "io", {+ cause,+ path: absoluteCallerPath(path),+ ...(cleanup === undefined ? {} : { cleanup }),+ });+ // A failed admission retry names its exact retained leases.+ bindArtifactFileCleanup(cause, error);+ return err(error);});}@@ -268,28 +274,31 @@ const writeTextPartsOwned = async (resources: [cause.owner.directory],};if (cause.outcome.ok)- return err(+ return stagedCleanupFailure(new EvidenceFileError("write", "io", {cause,path: requestedPath,cleanup,published: { ...cause.outcome.value, path: requestedPath },}),+ cause.lease,);if (cause.outcome.error instanceof AnalysisCancelledError)- return err(+ return stagedCleanupFailure(new AnalysisCancelledError(cause.outcome.error.operation, {cause: cause.outcome.error.cause,cleanupPrimary: cause.outcome.error,cleanup,}),+ cause.lease,);- return err(+ return stagedCleanupFailure(new EvidenceFileError("write", missingOrIo(cause.outcome.error), {cause,path: requestedPath,cleanup,}),+ cause.lease,);}if (@@ -314,6 +323,17 @@ const writeTextPartsOwned = async (}};+/** The retained staging lease clears this error only when a retry confirms release. */+const stagedCleanupFailure = <+ Failure extends EvidenceFileError | AnalysisCancelledError,+>(+ error: Failure,+ lease: AnalysisCleanupLease,+): Result<never, Failure> => {+ new AnalysisCleanupLeaseGroup(error, [lease]);+ return err(error);+};+/** A missing path or parent is a selection error, not a permission failure. */const missingOrIo = (cause: unknown): "missing" | "io" => {const code = fileErrorCode(cause);@@ -373,7 +393,12 @@ const publishFile = async (output: owner,});if (released.kind === "failed")- throw new StagedOutputCleanupFailure(outcome, released.cause, owner);+ throw new StagedOutputCleanupFailure(+ outcome,+ released.cause,+ owner,+ released.lease,+ );if (!outcome.ok) throw outcome.error;return outcome.value.bytes;};@@ -383,6 +408,7 @@ class StagedOutputCleanupFailure extends Error {readonly outcome: Result<PublishedEvidenceFile, unknown>,readonly cleanupCause: unknown,readonly owner: OwnedStagedOutput,+ readonly lease: AnalysisCleanupLease,) {super("Staged output cleanup failed", {cause: outcome.ok ? cleanupCause : outcome.error,diff --git a/tests/boundary/filesystem/evidenceExportCleanupRetry.test.ts b/tests/boundary/filesystem/evidenceExportCleanupRetry.test.tsnew file mode 100644index 000000000..2a7d99f71--- /dev/null+++ b/tests/boundary/filesystem/evidenceExportCleanupRetry.test.ts@@ -0,0 +1,124 @@+import { readdir, rm, writeFile } from "node:fs/promises";+import { join } from "node:path";++import { expect, it, vi } from "vitest";++import { exportEvidenceBundleCommand } from "../../../src/application/EvidenceBundleCommands.js";+import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";+import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";++// Host filesystems cannot fail publication and then one staging removal on+// demand; every other export step runs on the real filesystem.+const fault = vi.hoisted(() => ({+ publication: false,+ stagingRemovalFailures: 0,+ stagingRemovals: 0,+}));++vi.mock("node:fs/promises", async (importOriginal) => {+ const filesystem = await importOriginal<typeof import("node:fs/promises")>();+ return {+ ...filesystem,+ link: async (...args: Parameters<typeof filesystem.link>) => {+ if (fault.publication)+ throw Object.assign(new Error("injected publication EACCES"), {+ code: "EACCES",+ });+ return filesystem.link(...args);+ },+ rmdir: async (...args: Parameters<typeof filesystem.rmdir>) => {+ if (String(args[0]).includes(".rea-write-")) {+ fault.stagingRemovals += 1;+ if (fault.stagingRemovals <= fault.stagingRemovalFailures)+ throw Object.assign(new Error("injected staging removal EBUSY"), {+ code: "EBUSY",+ });+ }+ return filesystem.rmdir(...args);+ },+ };+});++const emptyBundle = JSON.stringify({+ artifacts: [],+ providers: [],+ environments: [],+ scenarios: [],+ captures: [],+ unknowns: [],+ records: [],+});++it.each([+ {+ name: "restores the publication failure after a confirmed staging retry",+ publication: true,+ stagingRemovalFailures: 1,+ stagingRemovals: 2,+ code: "execution_failure",+ retained: false,+ },+ {+ name: "keeps cleanup incomplete with its stage when the retry also fails",+ publication: true,+ stagingRemovalFailures: 2,+ stagingRemovals: 2,+ code: "cleanup_incomplete",+ retained: true,+ },+ {+ name: "returns the published receipt after a confirmed staging retry",+ publication: false,+ stagingRemovalFailures: 1,+ stagingRemovals: 2,+ code: undefined,+ retained: false,+ },+])("$name", async (scenario) => {+ const root = await createTestTempDirectory("rea-export-cleanup-retry-");+ const source = join(root, "source.json");+ const output = join(root, "out.json");+ await writeFile(source, emptyBundle);+ Object.assign(fault, {+ publication: scenario.publication,+ stagingRemovalFailures: scenario.stagingRemovalFailures,+ stagingRemovals: 0,+ });+ try {+ const result = await exportEvidenceBundleCommand(source, output, false);+ expect(fault.stagingRemovals).toBe(scenario.stagingRemovals);+ const entries = await readdir(root);+ const stages = entries.filter((entry) => entry.startsWith(".rea-write-"));+ expect(stages).toHaveLength(scenario.retained ? 1 : 0);+ expect(entries.includes("out.json")).toBe(!scenario.publication);+ if (scenario.code === undefined) {+ expect(result).toMatchObject({+ ok: true,+ value: { path: output, records: 0 },+ });+ return;+ }+ if (result.ok) throw new Error("Expected export failure");+ const projected = projectAnalysisError(result.error);+ expect(projected.code).toBe(scenario.code);+ expect(projected.details).toMatchObject({+ operation: "write",+ reason: "io",+ path: output,+ });+ if (!scenario.retained) {+ expect(projected.details).not.toHaveProperty("cleanup");+ expect(projected.details).not.toHaveProperty("resources");+ return;+ }+ expect(projected.details).toMatchObject({+ cleanup: "incomplete",+ cleanup_reason: "injected staging removal EBUSY",+ execution_failure: "execution_failure",+ resources: [join(root, stages[0]!)],+ });+ } finally {+ Object.assign(fault, { publication: false, stagingRemovalFailures: 0 });+ await rm(root, { recursive: true, force: true });+ }+});diff --git a/tests/boundary/filesystem/jsonOutputOwnership.test.ts b/tests/boundary/filesystem/jsonOutputOwnership.test.tsindex 9f25d9ef3..cd83e0b6a 100644--- a/tests/boundary/filesystem/jsonOutputOwnership.test.ts+++ b/tests/boundary/filesystem/jsonOutputOwnership.test.ts@@ -82,6 +82,8 @@ it("closes the descriptor and retains staged content when its final identity isawait expect(fs.access(destination)).rejects.toThrow();// A closed descriptor cannot re-establish identity, so retry stays incomplete.await expect(resources.close()).rejects.toThrow();+ expect(result.error.cleanupIncomplete).toBe(true);+ expect(result.error.cleanupResources).toEqual([stage]);expect(await fs.readFile(join(stage, "content"), "utf8")).toBe("owned bytes",);@@ -179,6 +181,15 @@ it("reports committed output when post-publication staging cleanup remains owned"canonical output",);await fs.rm(join(cleanupResource, "unexpected"));+ await resources.close();+ expect(result.error.cleanupIncomplete).toBe(false);+ expect(result.error.cleanupResources).toEqual([]);+ const released = projectAnalysisError(result.error);+ expect(released).toMatchObject({+ code: "execution_failure",+ details: { published: true, published_path: join(root, "bundle.json") },+ });+ expect(released.details).not.toHaveProperty("cleanup");} finally {await resources.close();await rm(root, { recursive: true, force: true });@@ -218,12 +229,108 @@ it("preserves cancellation when staging cleanup also fails", async () => {await expect(fs.access(join(root, "cancelled.json"))).rejects.toThrow();const stage = requireStages(root)[0];if (stage !== undefined) await fs.rm(join(root, stage, "unexpected"));+ await resources.close();+ expect(requireStages(root)).toEqual([]);+ expect(result.error.cleanupIncomplete).toBe(false);+ const released = projectAnalysisError(result.error);+ expect(released).toMatchObject({+ code: "cancelled",+ details: { operation: "export_evidence_bundle", cleanup: "complete" },+ });+ expect(released.details).not.toHaveProperty("resources");} finally {await resources.close();await rm(root, { recursive: true, force: true });}});+it("restores an unpublished write failure once its retained stage is released", async () => {+ const root = await createTestTempDirectory("rea-json-write-retry-");+ const resources = new ArtifactResourceScope();+ const destination = join(root, "failed.json");+ try {+ function* failingParts(): Generator<string> {+ const stage = requireStages(root)[0];+ if (stage === undefined) throw new Error("Expected staging directory");+ writeFileSync(join(root, stage, "unexpected"), "retain this");+ yield "partial";+ throw new Error("injected staged write failure");+ }+ const result = await writeTextParts(+ failingParts(),+ destination,+ false,+ resources,+ );+ if (result.ok) throw new Error("Expected staged write failure");+ expect(result.error).toBeInstanceOf(EvidenceFileError);+ expect(result.error.published).toBeUndefined();+ const stage = result.error.cleanupResources[0];+ if (stage === undefined) throw new Error("Expected retained staging");+ expect(projectAnalysisError(result.error)).toMatchObject({+ code: "cleanup_incomplete",+ details: { cleanup: "incomplete", resources: [stage] },+ });+ await fs.rm(join(stage, "unexpected"));+ await resources.close();+ expect(requireStages(root)).toEqual([]);+ await expect(fs.access(destination)).rejects.toThrow();+ expect(result.error.cleanupIncomplete).toBe(false);+ expect(result.error.cleanupResources).toEqual([]);+ const released = projectAnalysisError(result.error);+ expect(released).toMatchObject({+ code: "execution_failure",+ details: { operation: "write", reason: "io", path: destination },+ });+ expect(released.details).not.toHaveProperty("cleanup");+ expect(released.details).not.toHaveProperty("resources");+ } finally {+ await resources.close().catch(() => undefined);+ await rm(root, { recursive: true, force: true });+ }+});++it("clears an admission retry failure only after every retained owner releases", async () => {+ const root = await createTestTempDirectory("rea-json-admission-retry-");+ const directory = join(root, ".rea-write-owned");+ await mkdir(directory);+ const unexpected = join(directory, "unexpected");+ writeFileSync(unexpected, "retain this");+ const resources = new ArtifactResourceScope();+ try {+ expect(+ await resources.release({+ kind: "staged-output",+ resource: directory,+ output: await OwnedStagedOutput.create(directory),+ }),+ ).toMatchObject({ kind: "failed" });+ const destination = join(root, "next.json");+ const result = await writeTextParts(+ ["unused"],+ destination,+ false,+ resources,+ );+ if (result.ok) throw new Error("Expected admission retry failure");+ expect(result.error).toBeInstanceOf(EvidenceFileError);+ expect(result.error.cleanupResources).toEqual([directory]);+ await expect(fs.access(destination)).rejects.toThrow();+ // A retry that still fails keeps the same exact owner unresolved.+ await expect(resources.retryCleanup()).rejects.toThrow();+ expect(result.error.cleanupIncomplete).toBe(true);+ await fs.rm(unexpected);+ await resources.close();+ await expect(fs.access(directory)).rejects.toThrow();+ expect(result.error.cleanupIncomplete).toBe(false);+ expect(projectAnalysisError(result.error).code).toBe("execution_failure");+ } finally {+ await fs.rm(unexpected, { force: true });+ await resources.close().catch(() => undefined);+ await rm(root, { recursive: true, force: true });+ }+});+it("refuses to publish a staged path replaced after its descriptor opened", async () => {const root = await createTestTempDirectory("rea-staged-swap-");const resources = new ArtifactResourceScope();