Merge pull request #1074 from morluto/fix/evidence-file-missing

fix(evidence): report missing evidence files instead of permission advice
This commit is contained in:
Kaoru0822
2026-10-08 07:22:31 -07:00
committed by GitHub
6 changed files with 92 additions and 16 deletions
+44 -9
View File
@@ -10,10 +10,14 @@ import { err, ok, type Result } from "../domain/result.js";
export const readJsonFile = async ( export const readJsonFile = async (
path: string, path: string,
): Promise<Result<unknown, EvidenceFileError>> => { ): Promise<Result<unknown, EvidenceFileError>> => {
const requestedPath = resolve(path);
try { try {
const canonicalPath = await realpath(resolve(path)); const canonicalPath = await realpath(requestedPath);
const stats = await lstat(canonicalPath); const stats = await lstat(canonicalPath);
if (!stats.isFile()) return err(new EvidenceFileError("read", "not-file")); if (!stats.isFile())
return err(
new EvidenceFileError("read", "not-file", { path: requestedPath }),
);
const encoded = await readFile(canonicalPath); const encoded = await readFile(canonicalPath);
let decoded: unknown; let decoded: unknown;
try { try {
@@ -23,11 +27,21 @@ export const readJsonFile = async (
), ),
); );
} catch (cause: unknown) { } catch (cause: unknown) {
return err(new EvidenceFileError("read", "invalid-json", { cause })); return err(
new EvidenceFileError("read", "invalid-json", {
cause,
path: requestedPath,
}),
);
} }
return ok(decoded); return ok(decoded);
} catch (cause: unknown) { } catch (cause: unknown) {
return err(new EvidenceFileError("read", "io", { cause })); return err(
new EvidenceFileError("read", missingOrIo(cause), {
cause,
path: requestedPath,
}),
);
} }
}; };
@@ -40,8 +54,8 @@ export const writeTextFile = async (
Result<{ readonly path: string; readonly bytes: number }, EvidenceFileError> Result<{ readonly path: string; readonly bytes: number }, EvidenceFileError>
> => { > => {
const bytes = Buffer.byteLength(encoded, "utf8"); const bytes = Buffer.byteLength(encoded, "utf8");
const requestedPath = resolve(path);
try { try {
const requestedPath = resolve(path);
const canonicalParent = await realpath(dirname(requestedPath)); const canonicalParent = await realpath(dirname(requestedPath));
const destination = resolve(canonicalParent, basename(requestedPath)); const destination = resolve(canonicalParent, basename(requestedPath));
const existing = await lstat(destination).catch((cause: unknown) => { const existing = await lstat(destination).catch((cause: unknown) => {
@@ -49,9 +63,14 @@ export const writeTextFile = async (
throw cause; throw cause;
}); });
if (existing !== undefined) { if (existing !== undefined) {
if (!overwrite) return err(new EvidenceFileError("write", "exists")); if (!overwrite)
return err(
new EvidenceFileError("write", "exists", { path: requestedPath }),
);
if (!existing.isFile() || existing.isSymbolicLink()) if (!existing.isFile() || existing.isSymbolicLink())
return err(new EvidenceFileError("write", "not-file")); return err(
new EvidenceFileError("write", "not-file", { path: requestedPath }),
);
} }
if (overwrite) if (overwrite)
await writeFileAtomic(destination, encoded, { await writeFileAtomic(destination, encoded, {
@@ -63,11 +82,27 @@ export const writeTextFile = async (
return ok({ path: requestedPath, bytes }); return ok({ path: requestedPath, bytes });
} catch (cause: unknown) { } catch (cause: unknown) {
if (!overwrite && fileErrorCode(cause) === "EEXIST") if (!overwrite && fileErrorCode(cause) === "EEXIST")
return err(new EvidenceFileError("write", "exists", { cause })); return err(
return err(new EvidenceFileError("write", "io", { cause })); new EvidenceFileError("write", "exists", {
cause,
path: requestedPath,
}),
);
return err(
new EvidenceFileError("write", missingOrIo(cause), {
cause,
path: requestedPath,
}),
);
} }
}; };
/** A missing path or parent is a selection error, not a permission failure. */
const missingOrIo = (cause: unknown): "missing" | "io" => {
const code = fileErrorCode(cause);
return code === "ENOENT" || code === "ENOTDIR" ? "missing" : "io";
};
const publishNewFile = async ( const publishNewFile = async (
destination: string, destination: string,
encoded: string, encoded: string,
+9 -3
View File
@@ -162,8 +162,7 @@ export const analysisErrorUserMessage = (error: AnalysisError): string => {
: `Evidence ${error.evidenceId} does not match the requested reference (${error.reason}). Check the expected and actual identity in the diagnostic details.`; : `Evidence ${error.evidenceId} does not match the requested reference (${error.reason}). Check the expected and actual identity in the diagnostic details.`;
if (error instanceof EvidenceIntegrityError) if (error instanceof EvidenceIntegrityError)
return "Evidence is invalid or has changed. Recreate or re-import it, then try again."; return "Evidence is invalid or has changed. Recreate or re-import it, then try again.";
if (error instanceof EvidenceFileError) if (error instanceof EvidenceFileError) return evidenceFileMessage(error);
return evidenceFileMessage(error.reason);
if (error instanceof UnknownRegistryError && error.reason === "not-found") if (error instanceof UnknownRegistryError && error.reason === "not-found")
return "The requested residual unknown does not exist in this session. Check the unknown_id and try again."; return "The requested residual unknown does not exist in this session. Check the unknown_id and try again.";
if (error instanceof UnknownRegistryError) if (error instanceof UnknownRegistryError)
@@ -258,7 +257,14 @@ const artifactMessage = (reason: ArtifactOperationError["reason"]): string => {
return "Artifact could not be read or written. Check file access and try again."; return "Artifact could not be read or written. Check file access and try again.";
}; };
const evidenceFileMessage = (reason: EvidenceFileError["reason"]): string => { const evidenceFileMessage = ({
operation,
reason,
}: EvidenceFileError): string => {
if (reason === "missing")
return operation === "read"
? "Evidence file does not exist at the selected path. Check the path and try again."
: "Evidence output directory does not exist. Choose an existing directory and try again.";
if (reason === "not-file") if (reason === "not-file")
return "Evidence path does not point to a regular file. Choose a file and try again."; return "Evidence path does not point to a regular file. Choose a file and try again.";
if (reason === "exists") if (reason === "exists")
+5 -1
View File
@@ -259,7 +259,11 @@ const artifactStateErrorDetails = (
}; };
if (error instanceof UnknownRegistryError) return { reason: error.reason }; if (error instanceof UnknownRegistryError) return { reason: error.reason };
if (error instanceof EvidenceFileError) if (error instanceof EvidenceFileError)
return { operation: error.operation, reason: error.reason }; return {
operation: error.operation,
reason: error.reason,
...(error.path === undefined ? {} : { path: error.path }),
};
return undefined; return undefined;
}; };
+6 -2
View File
@@ -25,11 +25,15 @@ export class EvidenceReferenceError extends EvidenceIntegrityError {
export class EvidenceFileError extends AnalysisError { export class EvidenceFileError extends AnalysisError {
readonly _tag = "EvidenceFileError"; readonly _tag = "EvidenceFileError";
/** Caller-selected path that failed, resolved against the working directory. */
readonly path: string | undefined;
constructor( constructor(
readonly operation: "read" | "write", readonly operation: "read" | "write",
readonly reason: "not-file" | "exists" | "invalid-json" | "io", readonly reason: "not-file" | "exists" | "invalid-json" | "missing" | "io",
options?: ErrorOptions, options?: ErrorOptions & { readonly path?: string },
) { ) {
super(`Evidence bundle ${operation} failed: ${reason}`, options); super(`Evidence bundle ${operation} failed: ${reason}`, options);
this.path = options?.path;
} }
} }
@@ -10,6 +10,7 @@ import {
writeEvidenceBundle, writeEvidenceBundle,
} from "../../../src/application/EvidenceBundleFiles.js"; } from "../../../src/application/EvidenceBundleFiles.js";
import { compareEvidenceBundlesCommand } from "../../../src/application/EvidenceBundleCommands.js"; import { compareEvidenceBundlesCommand } from "../../../src/application/EvidenceBundleCommands.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { createEvidence } from "../../../src/domain/evidence.js"; import { createEvidence } from "../../../src/domain/evidence.js";
import { import {
createEvidenceBundle, createEvidenceBundle,
@@ -116,6 +117,32 @@ describe("evidence bundle filesystem adapter", () => {
}); });
}); });
it("reports a missing file or output directory with the selected path", async () => {
const directory = await createTestTempDirectory("rea-evidence-");
const absent = join(directory, "absent.json");
const read = await readEvidenceBundle(absent);
expect(read).toMatchObject({
ok: false,
error: { _tag: "EvidenceFileError", reason: "missing", path: absent },
});
if (read.ok) throw new Error("Expected a missing-file failure");
expect(projectAnalysisError(read.error)).toMatchObject({
message: expect.stringContaining("does not exist at the selected path"),
details: { operation: "read", reason: "missing", path: absent },
});
const orphan = join(directory, "absent", "bundle.json");
const written = await writeEvidenceBundle(bundle(), orphan, false);
expect(written).toMatchObject({
ok: false,
error: { _tag: "EvidenceFileError", reason: "missing", path: orphan },
});
if (written.ok) throw new Error("Expected a missing-directory failure");
expect(projectAnalysisError(written.error).message).toContain(
"output directory does not exist",
);
});
it("rejects malformed and tampered input", async () => { it("rejects malformed and tampered input", async () => {
const directory = await createTestTempDirectory("rea-evidence-"); const directory = await createTestTempDirectory("rea-evidence-");
const malformed = join(directory, "malformed.json"); const malformed = join(directory, "malformed.json");
@@ -25,7 +25,7 @@ describe("analysis error projection contract", () => {
).map( ).map(
(reason) => new ArtifactOperationError("inventory_artifact", reason), (reason) => new ArtifactOperationError("inventory_artifact", reason),
), ),
...(["not-file", "exists", "invalid-json", "io"] as const).map( ...(["not-file", "exists", "invalid-json", "missing", "io"] as const).map(
(reason) => new EvidenceFileError("read", reason), (reason) => new EvidenceFileError("read", reason),
), ),
...( ...(