mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
365 lines
12 KiB
TypeScript
365 lines
12 KiB
TypeScript
import { viewParentIdentity } from "./viewParentIdentity.js";
|
|
import { analysisViewPageFields } from "./analysisViewPageFields.js";
|
|
import { z } from "zod";
|
|
import { canonicalJson } from "../comparisonSemantics.js";
|
|
import { jsonValueSchema } from "../jsonValue.js";
|
|
import { procedureIdentitySchema } from "../native/procedureInfo.js";
|
|
import {
|
|
referenceEdgeSchema,
|
|
unresolvedCallSchema,
|
|
} from "../native/nativeReference.js";
|
|
import { procedureReferencesSchema } from "../native/procedureReferences.js";
|
|
import { ok } from "../result.js";
|
|
import {
|
|
createAnalysisViewResultReader,
|
|
type AnalysisViewParent,
|
|
} from "./analysisViewParent.js";
|
|
import {
|
|
ownedView,
|
|
type AdmittedView,
|
|
type SummaryAnalysisView,
|
|
} from "./analysisViewShape.js";
|
|
import {
|
|
ANALYSIS_VIEW_PAGE_BYTES as PAGE_BYTES,
|
|
analysisViewPageTooLarge,
|
|
invalidAnalysisView,
|
|
} from "./analysisViewErrors.js";
|
|
import {
|
|
viewPageBytesWithDigest,
|
|
WIDEST_VIEW_PAGE_COVERAGE,
|
|
} from "./analysisViewPageBudget.js";
|
|
import type { JsonValue } from "../jsonValue.js";
|
|
import { selectByteBoundedViewRows } from "./byteBoundedViewRows.js";
|
|
import { jsonValueByteLength } from "../jsonValueByteLength.js";
|
|
import {
|
|
boundedViewLimitations,
|
|
completeWithinViewCoverage,
|
|
pageViewCoverage,
|
|
} from "./analysisViewCoverage.js";
|
|
|
|
const page = analysisViewPageFields("native-procedure-references");
|
|
export const procedureReferenceViewRequests = {
|
|
references: z.strictObject({
|
|
...page,
|
|
collection: z.literal("references"),
|
|
source_address: z
|
|
.string()
|
|
.min(1)
|
|
.optional()
|
|
.describe("Match an exact retained source address."),
|
|
target_address: z
|
|
.string()
|
|
.min(1)
|
|
.optional()
|
|
.describe("Match an exact retained target address."),
|
|
}),
|
|
unresolved_calls: z.strictObject({
|
|
...page,
|
|
collection: z.literal("unresolved_calls"),
|
|
}),
|
|
} as const;
|
|
export const nativeProcedureReferencesViewRequestSchema = z.discriminatedUnion(
|
|
"collection",
|
|
[
|
|
procedureReferenceViewRequests.references,
|
|
procedureReferenceViewRequests.unresolved_calls,
|
|
],
|
|
);
|
|
const endpointSchema = z.discriminatedUnion("kind", [
|
|
z.strictObject({ kind: z.literal("null") }),
|
|
z.strictObject({ kind: z.literal("selected-procedure") }),
|
|
z.strictObject({
|
|
kind: z.literal("shared"),
|
|
index: z.int().nonnegative(),
|
|
}),
|
|
]);
|
|
const rowSchema = referenceEdgeSchema.extend({
|
|
source_procedure: endpointSchema,
|
|
target_procedure: endpointSchema,
|
|
source_index: z.int().nonnegative(),
|
|
});
|
|
export const procedureReferenceViewItems = {
|
|
references: z.strictObject({
|
|
collection: z.literal("references"),
|
|
endpoints: z.array(procedureIdentitySchema),
|
|
rows: z.array(rowSchema),
|
|
}),
|
|
unresolved_calls: z.strictObject({
|
|
collection: z.literal("unresolved_calls"),
|
|
rows: z.array(
|
|
z.strictObject({
|
|
source_index: z.int().nonnegative(),
|
|
value: unresolvedCallSchema,
|
|
}),
|
|
),
|
|
}),
|
|
} as const;
|
|
export const procedureReferenceSummarySchema = z.strictObject({
|
|
procedure: procedureIdentitySchema,
|
|
direction: z.enum(["incoming", "outgoing"]),
|
|
reference_kinds_available: z.boolean(),
|
|
reference_count: z.int().nonnegative(),
|
|
unresolved_call_count: z.int().nonnegative(),
|
|
});
|
|
const readSource = createAnalysisViewResultReader(procedureReferencesSchema);
|
|
|
|
/** Page the retained result without re-querying the provider or changing source coverage. */
|
|
export const projectNativeProcedureReferencesView = (
|
|
parent: AnalysisViewParent,
|
|
admitted: AdmittedView,
|
|
) => {
|
|
const view = ownedView<
|
|
| SummaryAnalysisView
|
|
| z.output<typeof nativeProcedureReferencesViewRequestSchema>
|
|
>(admitted, "summary", "native-procedure-references");
|
|
if (view === undefined)
|
|
return invalidAnalysisView(
|
|
"Procedure-reference Evidence supports summary and native-procedure-references views.",
|
|
);
|
|
const parsed = readSource(parent);
|
|
if (!parsed.ok) return parsed;
|
|
const source = parsed.value;
|
|
const summary = {
|
|
procedure: source.procedure,
|
|
direction: source.direction,
|
|
reference_kinds_available: source.reference_kinds_available,
|
|
reference_count: source.references.length,
|
|
unresolved_call_count: source.unresolved_calls.length,
|
|
};
|
|
const shared = {
|
|
...viewParentIdentity(parent, "procedure_references"),
|
|
artifact: parent.artifact ?? null,
|
|
source_provider: parent.provider ?? null,
|
|
source_analysis_profile_digest: parent.analysisProfile?.digest ?? null,
|
|
source_parameters: jsonValueSchema.parse(parent.parameters ?? null),
|
|
limitations: boundedViewLimitations(parent),
|
|
unknowns: [
|
|
"Page coverage describes retained references, not provider discovery completeness or runtime execution.",
|
|
],
|
|
};
|
|
const tooLarge = () =>
|
|
analysisViewPageTooLarge(
|
|
parent.evidenceId,
|
|
"The selected procedure-reference view cannot fit the analysis-view byte budget.",
|
|
{
|
|
narrowing:
|
|
"Use the complete input Evidence or its local bundle, or retrieve or export the retained parent by the reported ID; if a CLI failure includes a partial observation, that observation is the completed parent.",
|
|
},
|
|
);
|
|
if (view.kind === "summary") {
|
|
const result = {
|
|
...shared,
|
|
kind: "summary" as const,
|
|
view,
|
|
summary,
|
|
coverage: completeWithinViewCoverage(1),
|
|
};
|
|
return viewPageBytesWithDigest(result as JsonValue) > PAGE_BYTES
|
|
? tooLarge()
|
|
: ok(result);
|
|
}
|
|
const common = {
|
|
...shared,
|
|
kind: "native-procedure-references" as const,
|
|
view,
|
|
summary,
|
|
};
|
|
const header = {
|
|
...common,
|
|
items:
|
|
view.collection === "references"
|
|
? { collection: view.collection, endpoints: [], rows: [] }
|
|
: { collection: view.collection, rows: [] },
|
|
byte_limited: false,
|
|
coverage: WIDEST_VIEW_PAGE_COVERAGE,
|
|
};
|
|
if (view.collection === "unresolved_calls") {
|
|
const selectedRows = selectByteBoundedViewRows(
|
|
source.unresolved_calls,
|
|
view,
|
|
{
|
|
matches: undefined,
|
|
project: (value, source_index) => ({ source_index, value }),
|
|
},
|
|
{
|
|
parentEvidenceId: parent.evidenceId,
|
|
envelope: header as JsonValue,
|
|
facet: view.collection,
|
|
},
|
|
);
|
|
if (!selectedRows.ok) return tooLarge();
|
|
|
|
const { rows, total, byte_limited } = selectedRows.value;
|
|
return ok({
|
|
...common,
|
|
view,
|
|
items: { collection: "unresolved_calls" as const, rows },
|
|
byte_limited,
|
|
coverage: pageViewCoverage(view.offset, rows.length, total),
|
|
});
|
|
}
|
|
let bytes = viewPageBytesWithDigest(header as JsonValue);
|
|
if (bytes > PAGE_BYTES) return tooLarge();
|
|
const referenceRows: z.infer<typeof rowSchema>[] = [];
|
|
let byteLimited = false;
|
|
const offer = (row: JsonValue, accepted: number, extra: number) => {
|
|
const size =
|
|
jsonValueByteLength(row, PAGE_BYTES - bytes) +
|
|
(accepted > 0 ? 1 : 0) +
|
|
extra;
|
|
if (bytes + size > PAGE_BYTES) {
|
|
byteLimited = true;
|
|
return false;
|
|
}
|
|
bytes += size;
|
|
return true;
|
|
};
|
|
const selected = canonicalJson(source.procedure);
|
|
const endpoints: z.infer<typeof procedureIdentitySchema>[] = [];
|
|
const endpointIndexes = new Map<string, number>();
|
|
let longestIdentityBytes = Buffer.byteLength(selected);
|
|
const planEndpoints = (
|
|
sourceProcedure: z.infer<typeof procedureIdentitySchema> | null,
|
|
targetProcedure: z.infer<typeof procedureIdentitySchema> | null,
|
|
) => {
|
|
const pending: z.infer<typeof procedureIdentitySchema>[] = [];
|
|
const pendingKeys: string[] = [];
|
|
let extra = 0;
|
|
let overflow = false;
|
|
const resolve = (value: z.infer<typeof procedureIdentitySchema> | null) => {
|
|
if (value === null) return { kind: "null" as const };
|
|
const comma = endpoints.length + pending.length > 0 ? 1 : 0;
|
|
const measured = jsonValueByteLength(
|
|
value as JsonValue,
|
|
Math.max(0, PAGE_BYTES - bytes - extra),
|
|
);
|
|
const size = measured + comma;
|
|
// `measured` is a UTF-8 lower bound. It can skip canonicalization only
|
|
// when it already exceeds every stored identity's UTF-8 length.
|
|
if (
|
|
bytes + extra + size > PAGE_BYTES &&
|
|
measured > longestIdentityBytes
|
|
) {
|
|
overflow = true;
|
|
return undefined;
|
|
}
|
|
const key = canonicalJson(value);
|
|
if (key === selected) return { kind: "selected-procedure" as const };
|
|
const existing = endpointIndexes.get(key);
|
|
if (existing !== undefined)
|
|
return { kind: "shared" as const, index: existing };
|
|
const pendingIndex = pendingKeys.indexOf(key);
|
|
if (pendingIndex !== -1)
|
|
return {
|
|
kind: "shared" as const,
|
|
index: endpoints.length + pendingIndex,
|
|
};
|
|
if (bytes + extra + size > PAGE_BYTES) {
|
|
overflow = true;
|
|
return undefined;
|
|
}
|
|
extra += size;
|
|
pending.push(value);
|
|
pendingKeys.push(key);
|
|
const keyBytes = Buffer.byteLength(key);
|
|
if (keyBytes > longestIdentityBytes) longestIdentityBytes = keyBytes;
|
|
return {
|
|
kind: "shared" as const,
|
|
index: endpoints.length + pending.length - 1,
|
|
};
|
|
};
|
|
const resolvedSource = resolve(sourceProcedure);
|
|
const resolvedTarget = resolve(targetProcedure);
|
|
if (
|
|
overflow ||
|
|
resolvedSource === undefined ||
|
|
resolvedTarget === undefined
|
|
)
|
|
return { overflow: true as const };
|
|
return {
|
|
overflow: false as const,
|
|
resolvedSource,
|
|
resolvedTarget,
|
|
pending,
|
|
pendingKeys,
|
|
extra,
|
|
};
|
|
};
|
|
const filtered =
|
|
view.source_address !== undefined || view.target_address !== undefined;
|
|
const references = source.references;
|
|
const visit = function* () {
|
|
if (!filtered) {
|
|
const end = Math.min(references.length, view.offset + view.limit);
|
|
for (let index = view.offset; index < end; index += 1) {
|
|
const reference = references[index];
|
|
if (reference !== undefined) yield [index, reference] as const;
|
|
}
|
|
return;
|
|
}
|
|
let seen = 0;
|
|
for (const [index, reference] of references.entries()) {
|
|
if (
|
|
(view.source_address !== undefined &&
|
|
reference.source_address !== view.source_address) ||
|
|
(view.target_address !== undefined &&
|
|
reference.target_address !== view.target_address)
|
|
)
|
|
continue;
|
|
if (seen >= view.offset && seen < view.offset + view.limit)
|
|
yield [index, reference] as const;
|
|
seen += 1;
|
|
}
|
|
};
|
|
let filteredTotal = 0;
|
|
if (filtered) {
|
|
for (const reference of references) {
|
|
if (
|
|
(view.source_address === undefined ||
|
|
reference.source_address === view.source_address) &&
|
|
(view.target_address === undefined ||
|
|
reference.target_address === view.target_address)
|
|
)
|
|
filteredTotal += 1;
|
|
}
|
|
}
|
|
for (const [index, reference] of visit()) {
|
|
const planned = planEndpoints(
|
|
reference.source_procedure,
|
|
reference.target_procedure,
|
|
);
|
|
if (planned.overflow) {
|
|
byteLimited = true;
|
|
break;
|
|
}
|
|
const row = {
|
|
...reference,
|
|
source_index: index,
|
|
source_procedure: planned.resolvedSource,
|
|
target_procedure: planned.resolvedTarget,
|
|
};
|
|
if (!offer(row as JsonValue, referenceRows.length, planned.extra)) break;
|
|
for (const [offset, value] of planned.pending.entries()) {
|
|
endpointIndexes.set(planned.pendingKeys[offset]!, endpoints.length);
|
|
endpoints.push(value);
|
|
}
|
|
referenceRows.push(row);
|
|
}
|
|
if (byteLimited && referenceRows.length === 0) return tooLarge();
|
|
return ok({
|
|
...common,
|
|
view,
|
|
items: {
|
|
collection: "references" as const,
|
|
endpoints,
|
|
rows: referenceRows,
|
|
},
|
|
byte_limited: byteLimited,
|
|
coverage: pageViewCoverage(
|
|
view.offset,
|
|
referenceRows.length,
|
|
filtered ? filteredTotal : references.length,
|
|
),
|
|
});
|
|
};
|