2026-07-12 21:12:18 +00:00
import {
2026-10-11 13:53:01 +08:00
createImmutableEvidenceBundle ,
2026-10-08 13:59:48 +00:00
describeEvidenceBundleFailure ,
describeValidationFailure ,
2026-07-12 21:12:18 +00:00
parseEvidenceBundle ,
type EvidenceBundle ,
} from "../domain/evidenceBundle.js" ;
2026-10-06 05:57:38 +08:00
import {
EvidenceFileError ,
EvidenceIntegrityError ,
} from "../domain/evidenceErrors.js" ;
2026-07-12 21:12:18 +00:00
import { err , ok , type Result } from "../domain/result.js" ;
2026-10-10 03:39:35 +08:00
import {
AnalysisCancelledError ,
type AnalysisResourceConstraintError ,
} from "../domain/analysisErrorCore.js" ;
2026-10-09 22:45:18 +08:00
import { parseProcessCapture } from "../domain/process/processCaptureParsing.js" ;
2026-10-08 23:46:02 +08:00
import {
bufferedJsonParts ,
canonicalJsonParts ,
} from "../domain/jsonSerialization.js" ;
import { readJsonFile , writeTextParts } from "./JsonFiles.js" ;
2026-10-11 15:59:21 +08:00
import { RegularFileCleanupFailure } from "./RegularFileRead.js" ;
2026-10-12 06:29:27 +08:00
import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js" ;
2026-07-12 21:12:18 +00:00
2026-10-10 03:39:35 +08:00
type EvidenceReadFailure =
| EvidenceFileError
| EvidenceIntegrityError
2026-10-11 15:59:21 +08:00
| AnalysisResourceConstraintError
| AnalysisCancelledError ;
2026-10-09 09:29:22 +08:00
type EvidenceWriteFailure =
| EvidenceFileError
| EvidenceIntegrityError
| AnalysisCancelledError ;
2026-07-12 21:12:18 +00:00
2026-09-29 14:53:20 +08:00
/** Read and validate an evidence bundle at the caller-supplied path. */
2026-10-11 13:53:01 +08:00
export const readEvidenceBundle = (
2026-07-12 21:12:18 +00:00
path : string ,
2026-10-11 13:53:01 +08:00
) : Promise < Result < EvidenceBundle , EvidenceReadFailure >> =>
readBundle ( path , parseEvidenceBundle );
/** Read an authenticated immutable snapshot for a subsequent session import. */
export const readImmutableEvidenceBundle = (
path : string ,
2026-10-11 15:59:21 +08:00
signal? : AbortSignal ,
2026-10-11 13:53:01 +08:00
) : Promise < Result < EvidenceBundle , EvidenceReadFailure >> =>
2026-10-11 15:59:21 +08:00
readBundle (
path ,
( input ) => {
const bundle = parseEvidenceBundle ( input );
return createImmutableEvidenceBundle ( bundle . records , bundle . unknowns );
},
signal ,
);
2026-10-11 13:53:01 +08:00
const readBundle = async (
path : string ,
parseBundle : ( input : unknown ) => EvidenceBundle ,
2026-10-11 15:59:21 +08:00
signal? : AbortSignal ,
2026-07-12 21:12:18 +00:00
) : Promise < Result < EvidenceBundle , EvidenceReadFailure >> => {
2026-10-11 15:59:21 +08:00
if ( signal ? . aborted )
return err ( new AnalysisCancelledError ( "import_evidence_bundle" ));
const loaded = await readJsonFile ( path , signal ). catch (( cause : unknown ) => {
// A failed close transfers a live handle owner; preserve that failure.
if (
signal ? . aborted === true &&
! ( cause instanceof RegularFileCleanupFailure )
)
return err (
new AnalysisCancelledError ( "import_evidence_bundle" , { cause }),
);
throw cause ;
});
2026-07-14 04:18:38 +08:00
if ( ! loaded . ok ) return loaded ;
2026-10-11 15:59:21 +08:00
if ( signal ? . aborted )
return err ( new AnalysisCancelledError ( "import_evidence_bundle" ));
2026-10-08 13:59:48 +00:00
let bundle : EvidenceBundle ;
2026-07-12 21:12:18 +00:00
try {
2026-10-11 13:53:01 +08:00
bundle = parseBundle ( loaded . value );
2026-07-12 21:12:18 +00:00
} catch ( cause : unknown ) {
2026-07-14 04:18:38 +08:00
return err (
new EvidenceIntegrityError ( "Evidence bundle validation failed" , {
cause ,
2026-10-08 13:59:48 +00:00
userMessage : describeEvidenceBundleFailure ( loaded . value , cause ),
2026-07-14 04:18:38 +08:00
}),
);
2026-07-12 21:12:18 +00:00
}
2026-10-08 13:59:48 +00:00
for ( const record of bundle . records ) {
if ( record . predicate_type !== "rea.process-capture" ) continue ;
try {
parseProcessCapture ( record . normalized_result );
} catch ( cause : unknown ) {
return err (
new EvidenceIntegrityError ( "Evidence bundle validation failed" , {
cause ,
userMessage : `Process capture record ${ record . evidence_id } has an invalid normalized_result ( ${ describeValidationFailure ( cause ) } ). Recreate or re-export the bundle, then try again.` ,
}),
);
}
}
return ok ( bundle );
2026-07-12 21:12:18 +00:00
};
2026-09-29 14:53:20 +08:00
/** Atomically write deterministic evidence JSON at the caller-supplied path. */
2026-07-12 21:12:18 +00:00
export const writeEvidenceBundle = async (
bundle : EvidenceBundle ,
path : string ,
overwrite : boolean ,
2026-10-12 06:29:27 +08:00
resources : ArtifactResourceScope ,
2026-10-09 09:29:22 +08:00
signal? : AbortSignal ,
2026-07-12 21:12:18 +00:00
) : Promise <
Result <
{ readonly path : string ; readonly bytes : number },
EvidenceWriteFailure
>
> => {
2026-10-09 09:29:22 +08:00
if ( signal ? . aborted === true )
return err ( new AnalysisCancelledError ( "export_evidence_bundle" ));
2026-10-08 23:46:02 +08:00
let checked : EvidenceBundle ;
2026-07-12 21:12:18 +00:00
try {
2026-10-08 23:46:02 +08:00
checked = parseEvidenceBundle ( bundle );
2026-07-12 21:12:18 +00:00
} catch ( cause : unknown ) {
return err (
new EvidenceIntegrityError ( "Evidence bundle validation failed" , {
cause ,
}),
);
}
2026-10-08 23:46:02 +08:00
return writeTextParts (
bufferedJsonParts ( canonicalJsonParts ( checked )),
path ,
overwrite ,
2026-10-12 06:29:27 +08:00
resources ,
2026-10-09 09:29:22 +08:00
signal === undefined
? undefined
: { signal , operation : "export_evidence_bundle" },
2026-10-08 23:46:02 +08:00
);
2026-07-12 21:12:18 +00:00
};