Files
rea/src/application/EvidenceInputResolver.ts
T
gensokyo-dev 37827af591 feat(artifacts): compare inventories from Evidence sources (#7869)
* feat(artifacts): compare artifacts by Evidence reference and from the CLI

compare_artifacts made agents send back the complete inventory Evidence
that REA already retains, and the CLI had no artifact comparison.

Each side of compare_artifacts now takes exactly one of: complete inline
Evidence (unchanged), {kind: "retained-evidence", evidence_id}, or
{kind: "evidence-file", path, evidence_id?}. The two references are the
sources inspect_analysis_view accepts; both tools now compose one
definition in evidenceReference.ts. A summary still resolves to its exact
retained parent, and every source form gives the same comparison Evidence.
An unresolvable retained reference is refused with a typed missing
reference error.

The application layer owns source resolution and the comparison Evidence,
so MCP and the new `rea compare-artifacts` command share one workflow.
readEvidenceFileSource reports bundle-selection refusals at the caller's
own field path.

* refactor(artifacts): resolve compare_artifacts sides like compare_functions

compare_functions, compare_wasm_interfaces and the JavaScript application
comparisons name each side with evidenceInputSchema and resolve it with
resolvePairedEvidenceRequest. compare_artifacts reached the same result
for inline and retained sides through its own dispatch.

evidenceSourceInputSchema now extends evidenceInputSchema with the
Evidence-file reference, and resolvePairedEvidenceSources sends inline and
retained sides to the same resolveEvidenceInput and file sides to the
shared Evidence file reader. compare_artifacts uses both, so the forms it
shares with compare_functions are parsed and resolved by one owner. The
advertised input schema is unchanged.

* docs(cli): point rea compare to compare-artifacts for inventory contents

rea compare (compare_bundles) compares records by Evidence identity and
explicit pairs and never reads artifact contents, so two build
inventories stay unresolved there. Its help and the CLI guide now name
compare-artifacts for that comparison.

* fix(artifacts): resolve summary parents within Evidence source bundles

* perf(evidence): reuse file-owned immutable authentication proofs

* test(evidence): inject failures at both authentication boundaries

---------

Co-authored-by: Entrpi <entrpi@proton.me>
2026-10-29 03:12:03 +08:00

143 lines
4.4 KiB
TypeScript

import type {
EvidenceInput,
EvidenceSourceInput,
} from "../domain/evidenceReference.js";
import type { Evidence } from "../domain/evidence.js";
import type { AnalysisError } from "../domain/analysisErrorBase.js";
import { EvidenceReferenceError } from "../domain/evidenceErrors.js";
import { err, ok, type Result } from "../domain/result.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;
/** 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 without selecting a target or running analysis. */
export const resolveEvidenceInput = (
input: EvidenceInput,
lookup: EvidenceLookup | undefined,
): 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",
"Evidence available to the selected source",
null,
),
);
if (record.evidence_id !== input.evidence_id)
return err(
new EvidenceReferenceError(
input.evidence_id,
"identity_mismatch",
input.evidence_id,
record.evidence_id,
),
);
return ok(record);
};
/** Resolve an application's Evidence before its existing semantic validation. */
export const resolveApplicationEvidenceRequest = <
T extends { readonly application: EvidenceInput },
>(
input: T,
lookup: EvidenceLookup | undefined,
): Result<
Omit<T, "application"> & { readonly application: Evidence },
EvidenceReferenceError
> => {
const application = resolveEvidenceInput(input.application, lookup);
if (!application.ok) return application;
return ok({ ...input, application: application.value });
};
/** Resolve explicitly paired Evidence without guessing another session's record. */
export const resolvePairedEvidenceRequest = <
T extends { readonly left: EvidenceInput; readonly right: EvidenceInput },
>(
input: T,
lookup: EvidenceLookup | undefined,
): Result<
Omit<T, "left" | "right"> & {
readonly left: Evidence;
readonly right: Evidence;
},
EvidenceReferenceError
> => {
const left = resolveEvidenceInput(input.left, lookup);
if (!left.ok) return left;
const right = resolveEvidenceInput(input.right, lookup);
if (!right.ok) return right;
return ok({ ...input, left: left.value, right: right.value });
};
/** Resolve paired sources like resolvePairedEvidenceRequest; Evidence files use the shared file reader. */
export const resolvePairedEvidenceSources = async <
T extends {
readonly left: EvidenceSourceInput;
readonly right: EvidenceSourceInput;
},
>(
input: T,
lookup: EvidenceLookup | undefined,
options: EvidenceSourceResolutionOptions,
): Promise<
Result<
Omit<T, "left" | "right"> & {
readonly left: Evidence;
readonly right: Evidence;
},
AnalysisError
>
> => {
const left = await resolveEvidenceSource(input.left, "left", lookup, options);
if (!left.ok) return left;
const right = await resolveEvidenceSource(
input.right,
"right",
lookup,
options,
);
if (!right.ok) return right;
return ok({ ...input, left: left.value, right: right.value });
};
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;
};