mirror of
https://github.com/morluto/rea.git
synced 2026-12-04 05:51:06 +00:00
* 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
277 lines
9.2 KiB
TypeScript
277 lines
9.2 KiB
TypeScript
import type {
|
|
EvidenceInput,
|
|
EvidenceSourceInput,
|
|
} from "../domain/evidenceReference.js";
|
|
import type { Evidence } from "../domain/evidence.js";
|
|
import type { z } from "zod";
|
|
import {
|
|
getEvidenceInputContract,
|
|
type EvidenceInputPath,
|
|
type EvidenceRequestFrameInput,
|
|
type ResolvedEvidenceRequestInput,
|
|
} from "../domain/evidenceInputContract.js";
|
|
import { retainedEvidenceReferenceSchema } from "../domain/evidenceReference.js";
|
|
import type { AnalysisError } from "../domain/analysisErrorBase.js";
|
|
import { cancelledBy } from "../domain/analysisErrorCore.js";
|
|
import { EvidenceReferenceError } from "../domain/evidenceErrors.js";
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
import { isRecord } from "../kernel/isRecord.js";
|
|
import { analysisInputErrorFromIssues } from "../domain/inputIssueProjection.js";
|
|
import { parseAnalysisInput } from "./AnalysisInput.js";
|
|
import {
|
|
readEvidenceFileSource,
|
|
type EvidenceFileReadOptions,
|
|
} from "./analysisView/evidenceFileSource.js";
|
|
|
|
/** Read-only lookup supplied by the current investigation's Evidence ledger. */
|
|
export type EvidenceLookup = (evidenceId: string) => Evidence | undefined;
|
|
|
|
export interface EvidenceRequestResolutionOptions {
|
|
readonly signal?: AbortSignal;
|
|
readonly referenceScope?: "retained-closure";
|
|
/**
|
|
* Producers whose complete records this consumer reads. A selected
|
|
* inspect_analysis_view summary of one of them stands for its retained
|
|
* parent, which replaces it before admission.
|
|
*/
|
|
readonly completeProducers?: readonly string[];
|
|
/** Optional format-owner admission, after lookup and before source use. */
|
|
readonly readEvidence?: (
|
|
input: unknown,
|
|
path: EvidenceInputPath,
|
|
) => Result<Evidence, AnalysisError>;
|
|
}
|
|
|
|
interface EvidenceRequestResolution<Input> {
|
|
readonly input: Input;
|
|
readonly sources: readonly unknown[];
|
|
}
|
|
|
|
/** An admitting owner may rely on complete Evidence at each declared source. */
|
|
export function resolveEvidenceRequest<Schema extends z.ZodType>(
|
|
operation: string,
|
|
rawInput: unknown,
|
|
schema: Schema,
|
|
lookup: EvidenceLookup | undefined,
|
|
options: EvidenceRequestResolutionOptions & {
|
|
readonly readEvidence: NonNullable<
|
|
EvidenceRequestResolutionOptions["readEvidence"]
|
|
>;
|
|
},
|
|
): Result<
|
|
EvidenceRequestResolution<ResolvedEvidenceRequestInput<z.output<Schema>>>,
|
|
AnalysisError
|
|
>;
|
|
/** Source carriers remain unknown until their format owner admits them. */
|
|
export function resolveEvidenceRequest<Schema extends z.ZodType>(
|
|
operation: string,
|
|
rawInput: unknown,
|
|
schema: Schema,
|
|
lookup?: EvidenceLookup,
|
|
options?: EvidenceRequestResolutionOptions,
|
|
): Result<
|
|
EvidenceRequestResolution<EvidenceRequestFrameInput<z.output<Schema>>>,
|
|
AnalysisError
|
|
>;
|
|
/** Resolve only schema-declared Evidence fields, sharing one lookup per exact ID. */
|
|
export function resolveEvidenceRequest(
|
|
operation: string,
|
|
rawInput: unknown,
|
|
schema: z.ZodType,
|
|
lookup?: EvidenceLookup,
|
|
options: EvidenceRequestResolutionOptions = {},
|
|
): Result<EvidenceRequestResolution<unknown>, AnalysisError> {
|
|
const contract = getEvidenceInputContract(schema);
|
|
const parsed = parseAnalysisInput(
|
|
operation,
|
|
rawInput,
|
|
contract.frameSchema,
|
|
options.signal,
|
|
);
|
|
if (!parsed.ok) return parsed;
|
|
const records = new Map<string, Evidence | undefined>();
|
|
const read: EvidenceLookup = (id) => {
|
|
if (!records.has(id)) records.set(id, lookup?.(id));
|
|
return records.get(id);
|
|
};
|
|
const sources = new Set<unknown>();
|
|
const result = contract.resolve<AnalysisError>(
|
|
parsed.value,
|
|
(input, path) => {
|
|
if (options.signal?.aborted)
|
|
return err(cancelledBy(operation, options.signal));
|
|
let selected = input;
|
|
if (typeof input === "object" && input !== null && "kind" in input) {
|
|
const reference = retainedEvidenceReferenceSchema.safeParse(input);
|
|
if (!reference.success)
|
|
return err(
|
|
analysisInputErrorFromIssues(
|
|
operation,
|
|
reference.error.issues.map((issue) => ({
|
|
...issue,
|
|
path: [...path, ...issue.path],
|
|
})),
|
|
rawInput,
|
|
{ cause: reference.error },
|
|
),
|
|
);
|
|
const found = resolveEvidenceInput(
|
|
reference.data,
|
|
read,
|
|
options.referenceScope,
|
|
);
|
|
if (!found.ok)
|
|
return err(
|
|
new EvidenceReferenceError(
|
|
found.error.evidenceId,
|
|
found.error.reason,
|
|
{
|
|
cause: found.error,
|
|
expected: found.error.expected,
|
|
actual: found.error.actual,
|
|
referenceScope: found.error.referenceScope,
|
|
diagnostics: { input_path: [...path] },
|
|
},
|
|
),
|
|
);
|
|
selected = found.value;
|
|
}
|
|
if (options.completeProducers !== undefined) {
|
|
const parent = summaryParent(
|
|
selected,
|
|
options.completeProducers,
|
|
read,
|
|
options.referenceScope,
|
|
);
|
|
if (!parent.ok) {
|
|
parent.error.retainDiagnostics({ input_path: [...path] });
|
|
return parent;
|
|
}
|
|
selected = parent.value;
|
|
}
|
|
if (options.readEvidence !== undefined) {
|
|
const admitted = options.readEvidence(selected, path);
|
|
if (!admitted.ok) {
|
|
if (admitted.error.diagnostics?.input_path === undefined)
|
|
admitted.error.retainDiagnostics({ input_path: [...path] });
|
|
return admitted;
|
|
}
|
|
selected = admitted.value;
|
|
}
|
|
// A synchronous lookup or format admission can initiate cancellation.
|
|
if (options.signal?.aborted)
|
|
return err(cancelledBy(operation, options.signal));
|
|
sources.add(selected);
|
|
return ok(selected);
|
|
},
|
|
);
|
|
return result.ok
|
|
? ok({
|
|
input: result.value,
|
|
sources: [...sources],
|
|
})
|
|
: result;
|
|
}
|
|
|
|
/**
|
|
* The retained parent a summary of one of `producers` names, or `selected`
|
|
* itself. Only the parent's identity is read from the summary; the parent
|
|
* comes from the ledger and is admitted by its format owner like any record.
|
|
*/
|
|
const summaryParent = (
|
|
selected: unknown,
|
|
producers: readonly string[],
|
|
lookup: EvidenceLookup,
|
|
referenceScope: EvidenceReferenceError["referenceScope"],
|
|
): Result<unknown, EvidenceReferenceError> => {
|
|
if (!isRecord(selected) || selected.operation !== "inspect_analysis_view")
|
|
return ok(selected);
|
|
const summary = selected.normalized_result;
|
|
if (
|
|
!isRecord(summary) ||
|
|
summary.kind !== "summary" ||
|
|
typeof summary.parent_operation !== "string" ||
|
|
!producers.includes(summary.parent_operation) ||
|
|
typeof summary.parent_evidence_id !== "string"
|
|
)
|
|
return ok(selected);
|
|
const parent = resolveEvidenceInput(
|
|
{ kind: "retained-evidence", evidence_id: summary.parent_evidence_id },
|
|
lookup,
|
|
referenceScope,
|
|
);
|
|
if (!parent.ok) return parent;
|
|
return parent.value.operation === summary.parent_operation
|
|
? parent
|
|
: err(
|
|
new EvidenceReferenceError(
|
|
summary.parent_evidence_id,
|
|
"wrong_operation",
|
|
{
|
|
expected: summary.parent_operation,
|
|
actual: parent.value.operation,
|
|
},
|
|
),
|
|
);
|
|
};
|
|
|
|
/** Normalize each selected record using its file context and the current ledger. */
|
|
export interface EvidenceSourceResolutionOptions extends EvidenceFileReadOptions {
|
|
readonly operation: string;
|
|
readonly resolveEvidence?: (
|
|
evidence: Evidence,
|
|
lookup: EvidenceLookup | undefined,
|
|
side: "left" | "right",
|
|
) => Result<Evidence, AnalysisError>;
|
|
}
|
|
|
|
/** Resolve an exact reference; callers declare when recovery requires retained closure. */
|
|
export const resolveEvidenceInput = (
|
|
input: EvidenceInput,
|
|
lookup: EvidenceLookup | undefined,
|
|
referenceScope?: EvidenceReferenceError["referenceScope"],
|
|
): Result<Evidence, EvidenceReferenceError> => {
|
|
if (!("kind" in input)) return ok(input);
|
|
const record = lookup?.(input.evidence_id);
|
|
if (record === undefined)
|
|
return err(
|
|
new EvidenceReferenceError(input.evidence_id, "missing", {
|
|
expected: "Evidence available to the selected source",
|
|
actual: null,
|
|
referenceScope,
|
|
}),
|
|
);
|
|
if (record.evidence_id !== input.evidence_id)
|
|
return err(
|
|
new EvidenceReferenceError(input.evidence_id, "identity_mismatch", {
|
|
expected: input.evidence_id,
|
|
actual: record.evidence_id,
|
|
}),
|
|
);
|
|
return ok(record);
|
|
};
|
|
|
|
/** Resolve one side of a paired source with the same file and ledger owners. */
|
|
export const resolveEvidenceSource = async (
|
|
input: EvidenceSourceInput,
|
|
side: "left" | "right",
|
|
lookup: EvidenceLookup | undefined,
|
|
options: EvidenceSourceResolutionOptions,
|
|
): Promise<Result<Evidence, AnalysisError>> => {
|
|
if ("kind" in input && input.kind === "evidence-file")
|
|
return readEvidenceFileSource(input.path, options, input.evidence_id, {
|
|
operation: options.operation,
|
|
path: [side],
|
|
resolveSelected: (selected, fileLookup) =>
|
|
options.resolveEvidence?.(
|
|
selected,
|
|
(id) => fileLookup(id) ?? lookup?.(id),
|
|
side,
|
|
) ?? ok(selected),
|
|
});
|
|
const selected = resolveEvidenceInput(input, lookup);
|
|
return selected.ok
|
|
? (options.resolveEvidence?.(selected.value, lookup, side) ?? selected)
|
|
: selected;
|
|
};
|