Files
rea/src/domain/webScriptCaptureAdapters.ts
T
morluto fbfcad3152 fix(evidence): route web script export and WASM comparison through shared Evidence inputs (#15385)
* fix(evidence): resolve web script and WASM comparison sources through the shared Evidence input owner

export_web_scripts accepts its capture as declared Evidence input: a
complete record, a retained reference, or a retained inspect_analysis_view
summary, which reads its retained parent. compare_wasm_interfaces admits
both sides through resolveEvidenceRequest, so a malformed side is a
bounded evidence_integrity_mismatch and a summary reads its parent. The
summary-to-parent rule lives in the shared resolver as completeProducers.
A catalog test checks that every declared Evidence input resolves a
retained reference through the shared resolver.

Refs #15341

* fix(evidence): retain an inline web script capture before publishing and commit the schema summary
2026-11-30 20:42:45 -05:00

97 lines
3.6 KiB
TypeScript

import { webPageInspectionSchema } from "./browserObservationSchemas.js";
import { retainedBrowserScenarioCaptureSchema } from "./browserScenarioCapture.js";
import { parseEvidence, type Evidence } from "./evidence.js";
import type {
CapturedWebScript,
WebScriptExportResult,
} from "./webScriptExport.js";
import { pageScripts } from "./pageScriptCapture.js";
import { scenarioScripts } from "./scenarioScriptCapture.js";
/** Capture-specific parsing and source selection, independent of publication. */
export interface ScriptCaptureAdapter {
readonly operation: string;
readonly parse: (input: unknown) => SelectedScriptCapture | undefined;
}
/** Verified source records with the original capture coverage and limitations. */
export interface SelectedScriptCapture {
readonly kind: WebScriptExportResult["capture_kind"];
readonly completeness: WebScriptExportResult["capture_completeness"];
readonly scripts: readonly CapturedWebScript[];
readonly limitations: readonly string[];
}
/** Built-in adapters; add capture formats here without changing the publisher. */
export const SCRIPT_CAPTURE_ADAPTERS: readonly ScriptCaptureAdapter[] = [
{
operation: "inspect_web_page",
parse: (input) => {
const parsed = webPageInspectionSchema.safeParse(input);
return parsed.success
? {
kind: "page-inspection",
completeness: parsed.data.completeness,
scripts: pageScripts(parsed.data),
limitations: parsed.data.limitations,
}
: undefined;
},
},
{
operation: "capture_browser_scenario",
parse: (input) => {
const parsed = retainedBrowserScenarioCaptureSchema.safeParse(input);
return parsed.success
? {
kind: "browser-scenario",
completeness: parsed.data.completeness,
scripts: scenarioScripts(parsed.data),
limitations: parsed.data.limitations,
}
: undefined;
},
},
];
/** The capture an authenticated producer record retains, if it has script sources. */
export const selectEvidenceScriptCapture = (
evidence: Evidence,
adapters: readonly ScriptCaptureAdapter[] = SCRIPT_CAPTURE_ADAPTERS,
):
| (SelectedScriptCapture & { readonly sourceEvidenceId: string })
| undefined => {
const selected = adapters
.find(({ operation }) => operation === evidence.operation)
?.parse(evidence.normalized_result);
return selected === undefined
? undefined
: { ...selected, sourceEvidenceId: evidence.evidence_id };
};
/** The refusal for a record or file without exportable script sources. */
export const UNSUPPORTED_SCRIPT_CAPTURE_MESSAGE =
"Expected a valid inspect_web_page or capture_browser_scenario capture, with retained script sources or response bytes. Evidence envelopes must have a matching operation and semantic identifier.";
/** Reject unsupported/malformed captures and tampered Evidence before writing. */
export const selectScriptCapture = (
raw: unknown,
adapters: readonly ScriptCaptureAdapter[] = SCRIPT_CAPTURE_ADAPTERS,
): SelectedScriptCapture & { readonly sourceEvidenceId: string | null } => {
if (
typeof raw === "object" &&
raw !== null &&
("normalized_result" in raw || "evidence_id" in raw)
) {
const selected = selectEvidenceScriptCapture(parseEvidence(raw), adapters);
if (selected !== undefined) return selected;
} else {
for (const adapter of adapters) {
const selected = adapter.parse(raw);
if (selected !== undefined)
return { ...selected, sourceEvidenceId: null };
}
}
throw new TypeError(UNSUPPORTED_SCRIPT_CAPTURE_MESSAGE);
};