open-memex
Version:
Local-first memory layer and protocol for AI coding agents. Markdown source of truth, SQLite FTS5 index, zero cloud.
123 lines (122 loc) • 4.77 kB
JavaScript
/**
* §20.4 (D88): graph traversal as a read path — a node's neighbours and
* the shortest path between two memories. Pure over the extracted edge
* list; the store layer (src/store/graph.ts) owns persistence and gets
* edges back from here untouched.
*
* Traversal is undirected: a `supersedes` edge says new → old in the
* frontmatter, but from the reader's side a chain link works both ways.
* Every hop carries its edge (type + provenance + confidence + via) so a
* query receipt can tell the reader which connection was travelled and
* whether it was INFERRED — §20.3's rule that an inferred link must never
* look like a fact that was in the source.
*
* Traversal never crosses into a node that is not in the node set: a
* dangling id in an edge is lint's finding to report, not a ghost node
* to walk through.
*/
/** Undirected adjacency: id → outgoing + incoming edges. */
export function buildAdjacency(edges, nodeIds) {
const known = new Set(nodeIds);
const adj = new Map();
for (const id of known)
adj.set(id, []);
for (const e of edges) {
if (!known.has(e.from) || !known.has(e.to))
continue;
adj.get(e.from).push(e);
const back = { ...e, from: e.to, to: e.from };
adj.get(e.to).push(back);
}
return adj;
}
/**
* BFS neighbours of `startId`, nearest first (depth ascending, id
* ascending within a depth — deterministic output). `maxDepth` caps the
* walk; `maxNodes` caps the result the same way a search `limit` does.
* The start node itself is never returned.
*/
export function graphNeighbours(startId, edges, nodeIds, opts = {}) {
const maxDepth = Math.max(1, Math.min(opts.maxDepth ?? 1, 6));
const maxNodes = Math.max(1, Math.min(opts.maxNodes ?? 20, 100));
const adj = buildAdjacency(edges, nodeIds);
if (!adj.has(startId))
return [];
const visited = new Set([startId]);
const hits = [];
let frontier = [];
const enqueue = (edges) => {
for (const e of [...edges].sort((a, b) => a.to.localeCompare(b.to))) {
if (visited.has(e.to))
continue;
visited.add(e.to);
frontier.push({ id: e.to, via: e });
}
};
enqueue(adj.get(startId));
let depth = 1;
for (; depth <= maxDepth && frontier.length > 0 && hits.length < maxNodes; depth++) {
const layer = [...frontier]
.sort((a, b) => a.id.localeCompare(b.id) || a.via.via.localeCompare(b.via.via))
.slice(0, maxNodes - hits.length);
for (const f of layer)
hits.push({ id: f.id, depth, via: f.via });
const next = [];
for (const f of layer) {
if (next.length + hits.length >= maxNodes)
break;
for (const e of adj.get(f.id) ?? []) {
if (visited.has(e.to))
continue;
visited.add(e.to);
next.push({ id: e.to, via: e });
}
}
frontier = next;
}
return hits;
}
/**
* Shortest path between two ids (BFS, undirected, `maxDepth` cap). Each
* hop keeps the edge object in travel direction — the receipt reads
* "A —[relates-to INFERRED/medium (shared-tag:x)]—> B", whether or not
* that was the stored direction.
*/
export function shortestPath(fromId, toId, edges, nodeIds, opts = {}) {
const maxDepth = Math.max(1, Math.min(opts.maxDepth ?? 4, 6));
if (fromId === toId)
return { found: true, hops: [] };
const adj = buildAdjacency(edges, nodeIds);
if (!adj.has(fromId) || !adj.has(toId))
return { found: false, hops: [] };
const prev = new Map();
const visited = new Set([fromId]);
let frontier = [fromId];
for (let depth = 1; depth <= maxDepth; depth++) {
if (frontier.length === 0)
break;
const next = [];
for (const cur of frontier.sort((a, b) => a.localeCompare(b))) {
for (const e of [...(adj.get(cur) ?? [])].sort((a, b) => a.to.localeCompare(b.to))) {
if (visited.has(e.to))
continue;
visited.add(e.to);
prev.set(e.to, e);
if (e.to === toId) {
// Walk back, then reverse into travel order.
const hops = [];
let at = toId;
while (at !== fromId) {
const edge = prev.get(at);
hops.unshift(edge);
at = edge.from;
}
return { found: true, hops };
}
next.push(e.to);
}
}
frontier = next;
}
return { found: false, hops: [] };
}