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

Method computeGraphRelevance

src/mcp/tools.ts:4411–4464  ·  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

4409 * it's a few hundred nodes × ~25 iterations — negligible cost.
4410 */
4411 private computeGraphRelevance(
4412 nodeIds: string[],
4413 edges: Edge[],
4414 seedIds: Set<string>,
4415 ): Map<string, number> {
4416 const out = new Map<string, number>();
4417 const n = nodeIds.length;
4418 if (n === 0) return out;
4419 const idx = new Map<string, number>();
4420 for (let i = 0; i < n; i++) idx.set(nodeIds[i]!, i);
4421
4422 const RANK_EDGES = new Set<string>([
4423 'calls', 'references', 'extends', 'implements', 'overrides',
4424 'instantiates', 'returns', 'type_of', 'imports', 'navigates',
4425 ]);
4426 const adj: number[][] = Array.from({ length: n }, () => []);
4427 for (const e of edges) {
4428 if (!RANK_EDGES.has(e.kind)) continue;
4429 const i = idx.get(e.source);
4430 const j = idx.get(e.target);
4431 if (i === undefined || j === undefined || i === j) continue;
4432 adj[i]!.push(j);
4433 adj[j]!.push(i); // undirected — reachable either direction
4434 }
4435
4436 // Restart vector: uniform over seeds present in the candidate set. (Falls
4437 // back to uniform-over-all if no seed landed in the set, so we never return
4438 // all-zero.)
4439 const r = new Array<number>(n).fill(0);
4440 let rsum = 0;
4441 for (const id of seedIds) {
4442 const i = idx.get(id);
4443 if (i !== undefined) { r[i] = 1; rsum += 1; }
4444 }
4445 if (rsum === 0) { for (let i = 0; i < n; i++) r[i] = 1; rsum = n; }
4446 for (let i = 0; i < n; i++) r[i]! /= rsum;
4447
4448 const alpha = 0.25;
4449 let s = r.slice();
4450 for (let iter = 0; iter < 25; iter++) {
4451 const next = new Array<number>(n).fill(0);
4452 for (let i = 0; i < n; i++) {
4453 const si = s[i]!;
4454 if (si === 0) continue;
4455 const d = adj[i]!.length;
4456 if (d === 0) { next[i]! += si; continue; } // dangling: keep its mass
4457 const share = si / d;
4458 for (const j of adj[i]!) next[j]! += share;
4459 }
4460 for (let i = 0; i < n; i++) s[i] = (1 - alpha) * next[i]! + alpha * r[i]!;
4461 }
4462 for (let i = 0; i < n; i++) out.set(nodeIds[i]!, s[i]!);
4463 return out;
4464 }
4465
4466 /**
4467 * 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