2026-10-06 05:57:38 +08:00
|
|
|
import { EvidenceIntegrityError } from "../domain/evidenceErrors.js";
|
2026-10-10 21:04:05 -07:00
|
|
|
import type { AnalysisError } from "../domain/analysisErrorBase.js";
|
2026-09-29 14:53:20 +08:00
|
|
|
import type { EvidenceBundle } from "../domain/evidenceBundle.js";
|
2026-07-12 21:12:18 +00:00
|
|
|
import type { JsonValue } from "../domain/jsonValue.js";
|
2026-07-13 03:15:01 +00:00
|
|
|
import { jsonValueSchema } from "../domain/jsonValue.js";
|
2026-07-12 21:12:18 +00:00
|
|
|
import { err, ok, type Result } from "../domain/result.js";
|
2026-07-13 03:15:01 +00:00
|
|
|
import { compareBundles } from "../domain/bundleComparison.js";
|
2026-07-12 21:12:18 +00:00
|
|
|
import {
|
|
|
|
|
readEvidenceBundle,
|
|
|
|
|
writeEvidenceBundle,
|
|
|
|
|
} from "./EvidenceBundleFiles.js";
|
|
|
|
|
|
2026-10-11 03:33:26 +08:00
|
|
|
/** Validate a standalone bundle and report its complete record/revision counts. */
|
2026-07-12 21:12:18 +00:00
|
|
|
export const importEvidenceBundleCommand = async (
|
|
|
|
|
path: string,
|
|
|
|
|
): Promise<Result<JsonValue, AnalysisError>> => {
|
2026-09-29 14:53:20 +08:00
|
|
|
const loaded = await readEvidenceBundle(path);
|
2026-07-12 21:12:18 +00:00
|
|
|
if (!loaded.ok) return loaded;
|
2026-10-11 03:33:26 +08:00
|
|
|
return ok({
|
|
|
|
|
imported: loaded.value.records.length,
|
|
|
|
|
unknowns_added: loaded.value.unknowns.length,
|
|
|
|
|
total: loaded.value.records.length,
|
|
|
|
|
});
|
2026-07-12 21:12:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** Validate a source bundle and atomically export canonical bytes. */
|
|
|
|
|
export const exportEvidenceBundleCommand = async (
|
|
|
|
|
sourcePath: string,
|
|
|
|
|
outputPath: string,
|
|
|
|
|
overwrite: boolean,
|
|
|
|
|
): Promise<Result<JsonValue, AnalysisError>> => {
|
2026-09-29 14:53:20 +08:00
|
|
|
const loaded = await readEvidenceBundle(sourcePath);
|
2026-07-12 21:12:18 +00:00
|
|
|
if (!loaded.ok) return loaded;
|
|
|
|
|
return projectWrite(
|
|
|
|
|
loaded.value,
|
2026-09-29 14:53:20 +08:00
|
|
|
await writeEvidenceBundle(loaded.value, outputPath, overwrite),
|
2026-07-12 21:12:18 +00:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2026-09-29 07:08:46 +08:00
|
|
|
/** Compare two validated canonical Evidence bundles without session state. */
|
2026-07-13 03:15:01 +00:00
|
|
|
export const compareEvidenceBundlesCommand = async (input: {
|
|
|
|
|
readonly leftPath: string;
|
|
|
|
|
readonly rightPath: string;
|
|
|
|
|
}): Promise<Result<JsonValue, AnalysisError>> => {
|
|
|
|
|
const [left, right] = await Promise.all([
|
2026-09-29 14:53:20 +08:00
|
|
|
readEvidenceBundle(input.leftPath),
|
|
|
|
|
readEvidenceBundle(input.rightPath),
|
2026-07-13 03:15:01 +00:00
|
|
|
]);
|
|
|
|
|
if (!left.ok) return left;
|
|
|
|
|
if (!right.ok) return right;
|
|
|
|
|
try {
|
2026-09-29 08:07:24 +08:00
|
|
|
return ok(jsonValueSchema.parse(compareBundles(left.value, right.value)));
|
2026-07-13 03:15:01 +00:00
|
|
|
} catch (cause: unknown) {
|
|
|
|
|
return err(
|
|
|
|
|
new EvidenceIntegrityError("Evidence bundle comparison failed", {
|
|
|
|
|
cause,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-07-12 21:12:18 +00:00
|
|
|
const projectWrite = (
|
|
|
|
|
bundle: EvidenceBundle,
|
|
|
|
|
written: Awaited<ReturnType<typeof writeEvidenceBundle>>,
|
|
|
|
|
): Result<JsonValue, AnalysisError> =>
|
|
|
|
|
written.ok
|
|
|
|
|
? ok({
|
|
|
|
|
path: written.value.path,
|
|
|
|
|
bytes: written.value.bytes,
|
|
|
|
|
records: bundle.records.length,
|
|
|
|
|
})
|
|
|
|
|
: err(written.error);
|