import { decodeStrictUtf8 } from "../domain/strictUtf8.js"; import { z } from "zod"; import { ArtifactReaderFailure, AnalysisCleanupLeaseGroup, type AnalysisCleanupLease, type ArtifactResourceScope, withArtifactCleanupFailure, artifactOperationReason, artifactReaderTargetFailure, } from "../resources/index.js"; import { publishWebScripts } from "./PublishWebScripts.js"; import { selectEvidenceScriptCapture, selectScriptCapture, UNSUPPORTED_SCRIPT_CAPTURE_MESSAGE, } from "../domain/webScriptCaptureAdapters.js"; import { readEvidence } from "../domain/evidenceValidation.js"; import { invariantViolation } from "../domain/internalInvariant.js"; import { unknownTarget } from "../domain/targetIdentity.js"; import type { EvidenceInputPath } from "../domain/evidenceInputContract.js"; import { resolveEvidenceRequest, type EvidenceLookup, } from "./EvidenceInputResolver.js"; import { recordEvidenceSources } from "./investigation/recordEvidenceSources.js"; import type { EvidenceWriter } from "./investigation/InvestigationRecordPort.js"; import { AnalysisAccessDeniedError, AnalysisInputError, AnalysisOutputError, cancelledBy, carriedReason, } from "../domain/analysisErrorCore.js"; import { AnalysisError } from "../domain/analysisErrorBase.js"; import { analysisErrorWithCancellation } from "../domain/analysisErrorCancellation.js"; import { WindowsChangeTimeConfigurationError } from "../config/windowsChangeTimeSettle.js"; import { ConfigurationError } from "../domain/configurationErrors.js"; import { createEvidence, type Evidence } from "../domain/evidence.js"; import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js"; import { ProviderAdapterError } from "../domain/providerAdapterError.js"; import { ProviderCleanupError } from "../domain/providerCleanupError.js"; import { err, ok, type Result } from "../domain/result.js"; import { safeParseJson } from "../domain/safeJson.js"; import { exportWebScriptsInputSchema, WEB_SCRIPT_CAPTURE_PRODUCERS, type ExportWebScriptsInput, } from "../domain/webScriptExport.js"; import { WebScriptExportError } from "../domain/webScriptExportError.js"; import type { ExecutionOptions } from "../ports/AnalysisProvider.js"; import { WEB_SCRIPT_EXPORT_PROVIDER as PROVIDER } from "../domain/investigationProviders.js"; import { readRegularFile, RegularFileReadFailure, retryRegularFileCleanup, } from "../process/RegularFileRead.js"; import { NonRegularFileReadError, RegularFileChangedError, RegularFileOperationCancelled, } from "../filesystem/RegularFile.js"; import { RegularFileCleanupFailure, regularFileCancellation, } from "../filesystem/OwnedRegularFile.js"; import { errorMessage } from "../kernel/errorMessage.js"; import { sha256Hex } from "../kernel/sha256Hex.js"; const OPERATION = "export_web_scripts"; interface CaptureRead { readonly bytes: Buffer; readonly cleanup?: { readonly reason: string; readonly incomplete: boolean; readonly cause: unknown; readonly lease?: AnalysisCleanupLease; }; } interface PreparedCapture { /** The selected file's bytes; undefined for a capture supplied as Evidence. */ readonly bytes: Buffer | undefined; readonly loaded: ReturnType; } /** * Export one capture through the shared CLI/MCP application workflow. A * capture supplied as Evidence resolves through the shared Evidence input * owner: a retained reference, or a summary that names its retained parent. * The session retains that capture before anything is written, so a full * retention allowance refuses the export instead of hiding a completed one, * and a later failure never leaves the export without its source. */ export const exportWebScripts = async ( rawInput: unknown, resources: ArtifactResourceScope, options: ExecutionOptions = {}, evidence: { readonly lookup?: EvidenceLookup | undefined; readonly recordEvidence?: EvidenceWriter["recordEvidence"] | undefined; } = {}, ): Promise> => { const resolved = resolveEvidenceRequest( OPERATION, rawInput, exportWebScriptsInputSchema, evidence.lookup, { ...(options.signal === undefined ? {} : { signal: options.signal }), completeProducers: WEB_SCRIPT_CAPTURE_PRODUCERS, readEvidence: admitCapture, }, ); if (!resolved.ok) return resolved; const { capture, ...selection } = resolved.value.input; const input: ExportWebScriptsInput = selection; if (capture !== undefined) { const retained = recordEvidenceSources(evidence.recordEvidence, [capture]); if (!retained.ok) return retained; } try { if (options.signal?.aborted) return err(cancelledBy(OPERATION, options.signal)); const prepared = capture !== undefined ? evidenceCapture(capture) : input.capture_path !== undefined ? await prepareCapture(input.capture_path, resources, options.signal) : undefined; if (prepared === undefined) throw invariantViolation( "The export_web_scripts input admits exactly one capture selector", ); if (!prepared.ok) return prepared; const { loaded } = prepared.value; if (options.signal?.aborted) return err(cancelledBy(OPERATION, options.signal)); const sha256 = prepared.value.bytes === undefined ? null : sha256Hex(prepared.value.bytes); const result = await publishWebScripts(input, loaded, sha256, { resources, ...(options.signal === undefined ? {} : { signal: options.signal }), }); if (options.signal?.aborted === true) return err( cancelledBy(OPERATION, options.signal, { partialObservation: { kind: "web-script-export", result }, }), ); return ok( createEvidence( input.capture_path === undefined || sha256 === null ? unknownTarget("derived-from-linked-evidence") : { path: input.capture_path, sha256, format: "file" }, PROVIDER, { predicateType: "rea.web-script-export", operation: OPERATION, parameters: { ...(input.capture_path === undefined ? {} : { capture_path: input.capture_path }), ...(capture === undefined ? {} : { capture: { kind: "retained-evidence", evidence_id: capture.evidence_id, }, }), output_directory: input.output_directory, }, result, rawResult: null, confidence: "derived", authority: "historical-reference", limitations: result.limitations, locations: [{ kind: "artifact-path", path: result.manifest.path }], evidenceLinks: loaded.sourceEvidenceId === null ? [] : [loaded.sourceEvidenceId], }, ), ); } catch (cause: unknown) { return err(mapExportFailure(cause, input)); } }; /** Authenticate a capture record and require one of its producers. */ const admitCapture = ( input: unknown, path: EvidenceInputPath, ): Result => { const record = readEvidence(input); if ( !record.ok || (WEB_SCRIPT_CAPTURE_PRODUCERS as readonly string[]).includes( record.value.operation, ) ) return record; const message = `Expected inspect_web_page or capture_browser_scenario Evidence, received ${record.value.operation}`; return err( new AnalysisInputError(OPERATION, { userMessage: message }, [ { path: [...path], reason: "invalid_value", message }, ]), ); }; /** The capture an admitted record retains; it has no file bytes of its own. */ const evidenceCapture = ( evidence: Evidence, ): Result => { const loaded = selectEvidenceScriptCapture(evidence); return loaded === undefined ? err( new AnalysisInputError( OPERATION, { userMessage: UNSUPPORTED_SCRIPT_CAPTURE_MESSAGE }, [ { path: ["capture"], reason: "invalid_format", message: UNSUPPORTED_SCRIPT_CAPTURE_MESSAGE, }, ], ), ) : ok({ bytes: undefined, loaded }); }; const prepareCapture = async ( path: string, resources: ArtifactResourceScope, signal: AbortSignal | undefined, ): Promise> => { const read = await resources.run(() => readCapture(path, resources, signal)); if (!read.ok) return read; const cleanup = read.value.cleanup; const loaded = parseCapture( read.value.bytes, cleanup === undefined || cleanup.incomplete ? undefined : { cause: cleanup.cause }, ); if (!loaded.ok) return err( cleanup?.incomplete === true ? incompleteCaptureCleanup(loaded.error, path, cleanup) : loaded.error, ); if (cleanup !== undefined) { const primary = new WebScriptExportError( "io", path, `Capture handle close failed: ${cleanup.reason}`, { cause: cleanup.cause }, ); return err( cleanup.incomplete ? incompleteCaptureCleanup(primary, path, cleanup) : primary, ); } return ok({ bytes: read.value.bytes, loaded: loaded.value }); }; const mapExportFailure = ( cause: unknown, input: ExportWebScriptsInput, ): AnalysisError => { if (cause instanceof AnalysisError) return cause; if (cause instanceof ArtifactReaderFailure) { const primary = cause.cancellation !== undefined ? cancelledBy(OPERATION, carriedReason(cause.cancellation.reason), { partialObservation: cause.partialObservation, }) : (artifactReaderTargetFailure( OPERATION, input.capture_path ?? input.output_directory, cause, ) ?? new WebScriptExportError( artifactOperationReason(cause), input.output_directory, cause.message, { cause, partialObservation: cause.partialObservation }, )); return withArtifactCleanupFailure(cause, primary, PROVIDER.id, OPERATION); } if (cause instanceof z.ZodError) return new AnalysisOutputError(OPERATION, cause.message, { cause }); if ( cause instanceof Error && "code" in cause && typeof cause.code === "string" ) return new WebScriptExportError( "io", `${input.capture_path ?? "capture Evidence"} → ${input.output_directory}`, cause.message, { cause }, ); return new ProviderAdapterError(PROVIDER.id, OPERATION, { cause, diagnostics: { capture_path: input.capture_path ?? null, output_directory: input.output_directory, error_name: cause instanceof Error ? cause.name : "UnknownError", error_message: cause instanceof Error ? cause.message : typeof cause === "string" ? cause : "Unknown script export failure", }, }); }; /** * Read the caller-selected capture. A missing, non-file or unreadable * selection is a caller or host-permission failure, not an export failure. */ const readCapture = async ( path: string, resources: ArtifactResourceScope, signal: AbortSignal | undefined, ): Promise> => { try { return ok({ bytes: await readRegularFile(path, { signal, cancellationFailure: (reason) => new RegularFileOperationCancelled(reason), }), }); } catch (cause: unknown) { if (cause instanceof RegularFileCleanupFailure) { const retry = await retryRegularFileCleanup(cause, resources, signal); const cleanup = { reason: retry.cleanup?.reason ?? errorMessage(cause.cleanupCause), incomplete: retry.cleanup !== undefined, cause: cause.cleanupCause, ...(retry.cleanupLease === undefined ? {} : { lease: retry.cleanupLease }), }; if (cause.outcome.kind === "completed") { const cancellation = regularFileCancellation(cause) ?? (signal?.aborted === true ? { reason: signal.reason } : undefined); if (cancellation !== undefined) { const primary = cancelledBy( OPERATION, carriedReason(cancellation.reason), ); return err( retry.cleanup === undefined ? primary : incompleteCaptureCleanup(primary, path, cleanup), ); } return ok({ bytes: cause.outcome.value as Buffer, cleanup }); } const primary = captureReadErrorWithCancellation( captureReadFailure( path, cause, retry.cleanup === undefined ? { cause: cause.cleanupCause } : undefined, ), signal, ); return err( retry.cleanup === undefined ? primary : incompleteCaptureCleanup(primary, path, cleanup), ); } return err( captureReadErrorWithCancellation(captureReadFailure(path, cause), signal), ); } }; const captureReadErrorWithCancellation = ( error: AnalysisError, signal: AbortSignal | undefined, ): AnalysisError => signal?.aborted === true ? analysisErrorWithCancellation(error, cancelledBy(OPERATION, signal)) : error; const captureReadFailure = ( path: string, failure: unknown, closeFailure?: { readonly cause: unknown }, ): AnalysisError => { const cancellation = regularFileCancellation(failure); if (cancellation !== undefined) return cancelledBy(OPERATION, carriedReason(cancellation.reason)); const primary = failure instanceof RegularFileCleanupFailure && failure.outcome.kind === "failed" ? (failure.operationFailure ?? failure.outcome.cause) : failure; const cause = primary instanceof RegularFileReadFailure ? primary.operationCause : primary; const causeWithClose = closeFailure === undefined ? cause : new AggregateError( [cause, closeFailure.cause], "Capture read and file-handle close both failed", { cause }, ); if (cause instanceof WindowsChangeTimeConfigurationError) { if (closeFailure === undefined) return cause; return new ConfigurationError(cause.message, { cause: causeWithClose, settings: cause.settings, ...(cause.remediationAction === undefined ? {} : { remediationAction: cause.remediationAction }), }); } const code = cause instanceof Error && "code" in cause ? String(cause.code) : ""; if (code === "EACCES" || code === "EPERM") return new AnalysisAccessDeniedError(OPERATION, path, code, { cause: causeWithClose, }); if ( cause instanceof NonRegularFileReadError || cause instanceof RegularFileChangedError || code === "ENOENT" || code === "ENOTDIR" || code === "EISDIR" || code === "ENXIO" ) return new AnalysisInputError(OPERATION, { cause: causeWithClose }, [ { path: ["capture_path"], reason: "invalid_value", message: cause instanceof NonRegularFileReadError && code !== "EISDIR" ? cause.message : code === "EISDIR" ? `Selected capture is a directory, not a file: ${path}` : `Selected capture could not be read (${code}): ${path}`, }, ]); if (code !== "") return new WebScriptExportError("io", path, errorMessage(cause), { cause: causeWithClose, }); return new ProviderAdapterError(PROVIDER.id, OPERATION, { cause: causeWithClose, diagnostics: { capture_path: path, error_name: cause instanceof Error ? cause.name : "UnknownError", error_message: errorMessage(cause), }, }); }; const incompleteCaptureCleanup = ( primary: AnalysisError, path: string, cleanup: NonNullable, ): AnalysisError => { const error = analysisErrorWithCleanupFailure( primary, new ProviderCleanupError( PROVIDER.id, [path], { reason: cleanup.reason }, { cause: cleanup.cause, operation: OPERATION }, ), OPERATION, ); if (cleanup.lease !== undefined) new AnalysisCleanupLeaseGroup(error, [cleanup.lease]); return error; }; const parseCapture = ( bytes: Buffer, closeFailure?: { readonly cause: unknown }, ): Result, AnalysisError> => { try { const text = decodeStrictUtf8(bytes, "preserve"); const json = safeParseJson(text); if (!json.ok) throw new TypeError(json.error); return ok(selectScriptCapture(json.value)); } catch (cause: unknown) { const causeWithClose = closeFailure === undefined ? cause : new AggregateError( [cause, closeFailure.cause], "Capture parse and file-handle close both failed", { cause }, ); return err( new AnalysisInputError(OPERATION, { cause: causeWithClose }, [ { path: ["capture_path"], reason: "invalid_format", message: cause instanceof Error ? cause.message : "Capture is not valid UTF-8 JSON evidence.", }, ]), ); } };