import { setImmediate } from "node:timers/promises"; import type { CodeIndexStore } from "./code-index-store"; import { ProjectEvidenceError } from "./core"; import { canonicalJson } from "./digest"; import { MAP_RELATION_TARGET_JOIN, type MapQuerySelection, mapQueryPathSql, } from "./project-map-query-selection"; import { MAP_RELATION_PRIORITY, type MapExploreInput, type MapExploreResult, type MapObject, MapObjectSchema, type MapRelation, MapRelationSchema, } from "./project-map-types"; type Edge = MapExploreResult["relations"][number]; type Frontier = MapExploreResult["traversal"]["frontier"][number]; type RelationLink = Pick; export type MapTraversal = Readonly<{ relationRefs: readonly string[]; frontier: Frontier[] }>; const allowed = (column: string) => `EXISTS (SELECT 1 FROM json_each(?) scope WHERE scope.value = '.' OR ${column} = scope.value OR substr(${column}, 1, length(scope.value) + 1) = scope.value || '/')`; const relationOrder = `CASE json_extract(r.relation_json, '$.kind') ${Object.entries( MAP_RELATION_PRIORITY, ) .map(([kind, priority]) => `WHEN '${kind}' THEN ${priority}`) .join(" ")} END, r.relation_ref`; const compareEdges = (left: RelationLink, right: RelationLink) => MAP_RELATION_PRIORITY[left.kind] - MAP_RELATION_PRIORITY[right.kind] || (left.id < right.id ? -1 : left.id > right.id ? 1 : 0); export async function mapExploreTraversal( store: CodeIndexStore, input: Pick & { scope: "RELATED" | "PROJECT"; depth: number | null; authorizationScope: string; selection: MapQuerySelection; }, signal: AbortSignal, ): Promise { const assertActive = () => { if (signal.aborted) throw new ProjectEvidenceError("PROJECT_READ_CANCELLED", true); }; const checkpoint = async () => { await setImmediate(); assertActive(); }; await checkpoint(); // Filter every endpoint of every relation read. Hidden declarations cannot // bridge otherwise visible components, including in an unrestricted traversal. const relations = (seeded: string, order: string) => store.database.prepare(` SELECT r.relation_ref, r.from_ref, r.to_ref, json_extract(r.relation_json, '$.kind') AS kind FROM project_map_relations r JOIN project_map_objects f ON f.version_ref = r.version_ref AND f.entity_ref = r.from_ref ${MAP_RELATION_TARGET_JOIN} WHERE r.version_ref = ? AND ${allowed("r.path")} AND ${allowed("f.path")} AND (r.to_ref IS NULL OR ${allowed("t.path")}) AND ${mapQueryPathSql("r.path")} AND json_extract(r.relation_json, '$.kind') IN (SELECT value FROM json_each(?)) ${seeded} ${order}`); const filters = [ input.version, input.authorizationScope, input.authorizationScope, input.authorizationScope, input.selection.path, input.selection.path, input.selection.path, input.selection.path, canonicalJson(input.selection.kinds), ]; const relationRefs: string[] = []; let inspected = 0; if (input.scope === "PROJECT") { for (const row of relations("", `ORDER BY ${relationOrder}`).iterate(...filters)) { if (++inspected % 256 === 0) await checkpoint(); relationRefs.push(String(row.relation_ref)); } assertActive(); return { relationRefs, frontier: [] }; } if (!input.entityRef) throw new ProjectEvidenceError("PROJECT_READ_INPUT_INVALID", false); // The relations leaving and entering each depth's declarations, read through their indexes. const ends = [ ...(input.direction === "INCOMING" ? [] : ["r.from_ref"]), ...(input.direction === "OUTGOING" ? [] : ["r.to_ref"]), ].map((end) => relations(`AND ${end} IN (SELECT value FROM json_each(?))`, "")); const known = new Set([input.entityRef]); const seenRelations = new Set(); const frontier: Frontier[] = []; let seeds = [input.entityRef]; let depth = 0; while (seeds.length && (input.depth === null || depth < input.depth)) { await checkpoint(); depth++; const candidates = new Map(); const seedRefs = canonicalJson(seeds); for (const end of ends) { for (const row of end.iterate(...filters, seedRefs)) { if (++inspected % 256 === 0) await checkpoint(); const id = String(row.relation_ref); if (seenRelations.has(id)) continue; candidates.set(id, { id, from: String(row.from_ref), ...(row.to_ref === null ? {} : { to: String(row.to_ref) }), kind: row.kind as MapRelation["kind"], }); } } const next: string[] = []; for (const relation of [...candidates.values()].sort(compareEdges)) { if (++inspected % 256 === 0) await checkpoint(); seenRelations.add(relation.id); relationRefs.push(relation.id); const neighbors = input.direction === "BOTH" ? [relation.from, ...(relation.to ? [relation.to] : [])] : input.direction === "INCOMING" ? [relation.from] : relation.to ? [relation.to] : []; for (const ref of neighbors) { if (known.has(ref)) continue; known.add(ref); next.push(ref); } } if (input.depth !== null && depth === input.depth) for (const ref of next) frontier.push({ entityRef: ref, direction: input.direction }); seeds = next; } assertActive(); return { relationRefs, frontier }; } export function mapExplorePageRows(store: CodeIndexStore, version: string) { const relationQuery = store.database.prepare( "SELECT relation_json FROM project_map_relations WHERE version_ref = ? AND relation_ref = ?", ); const objectQuery = store.database.prepare( "SELECT object_json FROM project_map_objects WHERE version_ref = ? AND entity_ref = ?", ); const objects = new Map(); const object = (ref: string) => { let parsed = objects.get(ref); if (!parsed) { const row = objectQuery.get(version, ref); if (!row) throw new ProjectEvidenceError("PROJECT_EVIDENCE_STORE_CORRUPT", false); parsed = MapObjectSchema.parse(JSON.parse(String(row.object_json))); objects.set(ref, parsed); } return parsed; }; return (ref: string): Edge => { const row = relationQuery.get(version, ref); if (!row) throw new ProjectEvidenceError("PROJECT_EVIDENCE_STORE_CORRUPT", false); const relation = MapRelationSchema.parse(JSON.parse(String(row.relation_json))); return { relation, from: object(relation.from), ...(relation.to ? { to: object(relation.to) } : {}), }; }; } export function mapPageFrontier(frontier: readonly Frontier[], rows: readonly Edge[]) { const visible = new Set( rows.flatMap(({ relation }) => [relation.from, ...(relation.to ? [relation.to] : [])]), ); return frontier.filter(({ entityRef }) => visible.has(entityRef)).map((node) => ({ ...node })); }