mirror of
https://github.com/morluto/rea.git
synced 2026-10-29 19:05:45 +00:00
* feat(python): query retained pyc code-object facts * test(python): bind complete pyc selection benchmark tasks * test(python): include server instructions in token accounting * docs(perf): record pyc selected-facts task measurements * test(python): keep oracle dependencies separate from selected targets * docs(perf): record guarded oracle requalification * test(python): narrow complete Evidence through its owning contract
68 lines
2.4 KiB
TypeScript
68 lines
2.4 KiB
TypeScript
import type { z } from "zod";
|
|
import type { AnalysisInputError } from "../analysisErrorCore.js";
|
|
import type { AnalysisError } from "../analysisErrorBase.js";
|
|
import { analysisInputErrorFromIssues } from "../inputIssueProjection.js";
|
|
import { err, ok, type Result } from "../result.js";
|
|
import type { AnalysisViewParent } from "./analysisView.js";
|
|
import type { JsonValue } from "../jsonValue.js";
|
|
import {
|
|
canBorrowParsedJsonSnapshot,
|
|
freezeOwnedJsonTreeSnapshot,
|
|
isImmutableJsonSnapshot,
|
|
} from "../immutableJson.js";
|
|
|
|
/** One owning schema reuses validated immutable facts for their retained lifetime. */
|
|
export const createAnalysisViewResultReader = <
|
|
Value extends JsonValue,
|
|
>(parser: {
|
|
readonly safeParse: (input: unknown) => z.ZodSafeParseResult<Value>;
|
|
}): ((parent: AnalysisViewParent) => Result<Value, AnalysisError>) => {
|
|
const checked = new WeakMap<object, Value>();
|
|
return (parent) => {
|
|
const source = parent.normalizedResult;
|
|
const reusable =
|
|
parent.sealedResult === true &&
|
|
typeof source === "object" &&
|
|
source !== null &&
|
|
isImmutableJsonSnapshot(source);
|
|
const known = reusable ? checked.get(source) : undefined;
|
|
if (known !== undefined) return ok(known);
|
|
const parsed = parseAnalysisViewResult(parser, parent);
|
|
if (!parsed.ok || !reusable) return parsed;
|
|
const report = canBorrowParsedJsonSnapshot(source, parsed.value)
|
|
? source
|
|
: freezeOwnedJsonTreeSnapshot(parsed.value);
|
|
checked.set(source, report);
|
|
return ok(report);
|
|
};
|
|
};
|
|
|
|
/** Parse a parent's result with its owning parser; issues point into the view source. */
|
|
export const parseAnalysisViewResult = <Value>(
|
|
parser: {
|
|
readonly safeParse: (input: unknown) => z.ZodSafeParseResult<Value>;
|
|
},
|
|
parent: AnalysisViewParent,
|
|
): Result<Value, AnalysisError> => {
|
|
const parsed = parser.safeParse(parent.normalizedResult);
|
|
if (parsed.success) return ok(parsed.data);
|
|
return err(
|
|
analysisViewResultInputError(parent.normalizedResult, parsed.error),
|
|
);
|
|
};
|
|
|
|
/** Locate schema failures inside the retained result, including throwing parsers. */
|
|
export const analysisViewResultInputError = (
|
|
input: unknown,
|
|
cause: z.ZodError,
|
|
): AnalysisInputError =>
|
|
analysisInputErrorFromIssues(
|
|
"inspect_analysis_view",
|
|
cause.issues.map((issue) => ({
|
|
...issue,
|
|
path: ["source", "normalized_result", ...issue.path],
|
|
})),
|
|
{ source: { normalized_result: input } },
|
|
{ cause },
|
|
);
|