Objective
Pre-build an adjacency list (Map<string, RelationEdge[]>) during compilation or at query-engine initialization so that buildFrontier() uses O(1) per-node lookups instead of scanning the full relationGraph array.
Current behavior: buildFrontier() iterates over the entire relationGraph for every selected node — O(selected_nodes × total_edges).
Proposed fix:
const adjacency = new Map<string, RelationEdge[]>();
for (const edge of snapshot.relationGraph) {
if (!adjacency.has(edge.source)) adjacency.set(edge.source, []);
adjacency.get(edge.source)!.push(edge);
if (!adjacency.has(edge.target)) adjacency.set(edge.target, []);
adjacency.get(edge.target)!.push(edge);
}
Then buildFrontier() calls adjacency.get(nodeId) instead of scanning.
"Done" looks like: buildFrontier() uses a pre-built adjacency list; same results, faster execution; no API changes.
Kind
refactor
Affected area
src/runtime/ (compiler & query engine)
Acceptance criteria
Agent-delegable?
yes
Additional context
src/runtime/query-engine.ts: buildFrontier() method (line ~865), full scan at line ~912
src/runtime/types.ts: RelationEdge type
- Low complexity, self-contained change with no API surface changes
Measurable impact
| Metric |
Before |
After (target) |
How to measure |
buildFrontier() uses adjacency map lookup |
No (full array scan) |
Yes (O(1) per-node) |
grep -c 'adjacency.get' src/runtime/query-engine.ts (before: 0, after: ≥1) |
Full relationGraph iteration in buildFrontier() |
1 (line ~912: for...of snapshot.relationGraph) |
0 |
grep -c 'for.*of.*\.relationGraph' src/runtime/query-engine.ts |
| Existing test pass rate |
all pass |
all pass (no regressions) |
bun run test |
| Behavioral change |
N/A |
none — identical query results |
bun run cli -- query --question "What is U.BoundedContext?" --mode verbose (diff output before/after) |
Objective
Pre-build an adjacency list (
Map<string, RelationEdge[]>) during compilation or at query-engine initialization so thatbuildFrontier()uses O(1) per-node lookups instead of scanning the fullrelationGrapharray.Current behavior:
buildFrontier()iterates over the entirerelationGraphfor every selected node — O(selected_nodes × total_edges).Proposed fix:
Then
buildFrontier()callsadjacency.get(nodeId)instead of scanning."Done" looks like:
buildFrontier()uses a pre-built adjacency list; same results, faster execution; no API changes.Kind
refactor
Affected area
src/runtime/ (compiler & query engine)
Acceptance criteria
snapshot.relationGraphbuildFrontier()uses map lookup instead of full array scanAgent-delegable?
yes
Additional context
src/runtime/query-engine.ts:buildFrontier()method (line ~865), full scan at line ~912src/runtime/types.ts:RelationEdgetypeMeasurable impact
buildFrontier()uses adjacency map lookupgrep -c 'adjacency.get' src/runtime/query-engine.ts(before: 0, after: ≥1)relationGraphiteration inbuildFrontier()for...of snapshot.relationGraph)grep -c 'for.*of.*\.relationGraph' src/runtime/query-engine.tsbun run testbun run cli -- query --question "What is U.BoundedContext?" --mode verbose(diff output before/after)