Files
reverse-engineering-anything/src/domain/analysisView/nativeGraphView.ts
T
2026-11-09 20:28:46 +08:00

113 lines
3.7 KiB
TypeScript

import { z } from "zod";
import type { NativeCallGraph } from "../native/nativeCallGraph.js";
import type { RelationshipCoverage } from "../relationshipCoverage.js";
import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
import {
nativeGraphViewItemsSchema,
referencedRelationshipCoverageSchema,
} from "./nativeObservationSchemas.js";
type GraphItems = z.output<typeof nativeGraphViewItemsSchema>;
type Coverage = z.output<typeof referencedRelationshipCoverageSchema>;
const canonicalDepth = /^(?:0|[1-9][0-9]*)$/;
const compareGraphLevels = (left: string, right: string): number => {
const leftNumeric = canonicalDepth.test(left);
const rightNumeric = canonicalDepth.test(right);
if (leftNumeric && rightNumeric) {
if (left.length !== right.length) return left.length - right.length;
return compareUnicodeCodePoints(left, right);
}
if (leftNumeric !== rightNumeric) return leftNumeric ? -1 : 1;
return compareUnicodeCodePoints(left, right);
};
const referenceCoverage = (
source: RelationshipCoverage,
intern: (message: string) => number,
): Coverage => {
const diagnostic =
source.diagnostics === undefined
? {}
: { diagnostic_ids: source.diagnostics.map(intern) };
const discovery =
source.discovery === undefined ? {} : { discovery: source.discovery };
if (source.status === "complete")
return { status: "complete", ...diagnostic, ...discovery };
const directions =
source.limited_directions === undefined
? {}
: { limited_directions: source.limited_directions };
return source.status === "partial"
? {
status: "partial",
limitation_ids: source.limitations.map(intern),
...directions,
...diagnostic,
...discovery,
}
: {
status: "unknown",
reason_id: intern(source.reason),
...directions,
...diagnostic,
...discovery,
};
};
/** Object levels have canonical key order; row identity is (parent, level, source_index). */
export const projectNativeGraphRows = (
source: NativeCallGraph,
view: { readonly offset: number; readonly limit: number },
): { items: GraphItems; total: number } => {
const messages: string[] = [];
const known = new Map<string, number>();
const intern = (message: string): number => {
const prior = known.get(message);
if (prior !== undefined) return prior;
const id = messages.length;
messages.push(message);
known.set(message, id);
return id;
};
const rows: GraphItems["rows"] = [];
let total = 0;
for (const level of Object.keys(source.graph).sort(compareGraphLevels)) {
const nodes = source.graph[level]!;
if (nodes.length === 0) {
if (total >= view.offset && rows.length < view.limit)
rows.push({ level, source_index: null, status: "empty-level" });
total++;
continue;
}
const begin = Math.max(0, view.offset - total);
const end = Math.min(
nodes.length,
begin + Math.max(0, view.limit - rows.length),
);
for (let index = begin; index < end; index++) {
const node = nodes[index]!;
rows.push(
node.status === "error"
? { level, source_index: index, ...node }
: {
level,
source_index: index,
address: node.address,
status: node.status,
calls: node.calls,
relationship_coverage: referenceCoverage(
node.relationship_coverage,
intern,
),
...(node.limitations === undefined
? {}
: { limitation_ids: node.limitations.map(intern) }),
},
);
}
total += nodes.length;
}
return { items: { type: "graph-nodes", messages, rows }, total };
};