MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / computeGraphRelevance

Method computeGraphRelevance

src/mcp/tools.ts:4528–4581  ·  view source on GitHub ↗

* Graph-connectivity relevance via Random-Walk-with-Restart (personalized * PageRank) from the query's matched SEED nodes over the call/reference graph. * * This is the ranking signal text search (FTS/bm25) CANNOT provide, and it's * codegraph's home turf: relevance by STRUCTURE, not wor

(
    nodeIds: string[],
    edges: Edge[],
    seedIds: Set<string>,
  )

Source from the content-addressed store, hash-verified

4526 * it's a few hundred nodes × ~25 iterations — negligible cost.
4527 */
4528 private computeGraphRelevance(
4529 nodeIds: string[],
4530 edges: Edge[],
4531 seedIds: Set<string>,
4532 ): Map<string, number> {
4533 const out = new Map<string, number>();
4534 const n = nodeIds.length;
4535 if (n === 0) return out;
4536 const idx = new Map<string, number>();
4537 for (let i = 0; i < n; i++) idx.set(nodeIds[i]!, i);
4538
4539 const RANK_EDGES = new Set<string>([
4540 'calls', 'references', 'extends', 'implements', 'overrides',
4541 'instantiates', 'returns', 'type_of', 'imports', 'navigates',
4542 ]);
4543 const adj: number[][] = Array.from({ length: n }, () => []);
4544 for (const e of edges) {
4545 if (!RANK_EDGES.has(e.kind)) continue;
4546 const i = idx.get(e.source);
4547 const j = idx.get(e.target);
4548 if (i === undefined || j === undefined || i === j) continue;
4549 adj[i]!.push(j);
4550 adj[j]!.push(i); // undirected — reachable either direction
4551 }
4552
4553 // Restart vector: uniform over seeds present in the candidate set. (Falls
4554 // back to uniform-over-all if no seed landed in the set, so we never return
4555 // all-zero.)
4556 const r = new Array<number>(n).fill(0);
4557 let rsum = 0;
4558 for (const id of seedIds) {
4559 const i = idx.get(id);
4560 if (i !== undefined) { r[i] = 1; rsum += 1; }
4561 }
4562 if (rsum === 0) { for (let i = 0; i < n; i++) r[i] = 1; rsum = n; }
4563 for (let i = 0; i < n; i++) r[i]! /= rsum;
4564
4565 const alpha = 0.25;
4566 let s = r.slice();
4567 for (let iter = 0; iter < 25; iter++) {
4568 const next = new Array<number>(n).fill(0);
4569 for (let i = 0; i < n; i++) {
4570 const si = s[i]!;
4571 if (si === 0) continue;
4572 const d = adj[i]!.length;
4573 if (d === 0) { next[i]! += si; continue; } // dangling: keep its mass
4574 const share = si / d;
4575 for (const j of adj[i]!) next[j]! += share;
4576 }
4577 for (let i = 0; i < n; i++) s[i] = (1 - alpha) * next[i]! + alpha * r[i]!;
4578 }
4579 for (let i = 0; i < n; i++) out.set(nodeIds[i]!, s[i]!);
4580 return out;
4581 }
4582
4583 /**
4584 * Handle codegraph_explore — deep exploration in a single call

Callers 1

handleExploreMethod · 0.95

Calls 6

fromMethod · 0.80
hasMethod · 0.80
pushMethod · 0.80
fillMethod · 0.80
getMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected