Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
fix(ask): pool dangling PageRank mass — O(dangling×seeds) hang on broad queries
Shrish Dwivedic8b9cb760b33
diff --git a/src/ask/graphrank.ts b/src/ask/graphrank.tsindex 9507b6ab..73326207 100644--- a/src/ask/graphrank.ts+++ b/src/ask/graphrank.ts@@ -85,18 +85,23 @@ export function personalizedPageRank(const next = new Map<string, number>();// Teleport: every step, alpha of the mass returns to the seed set.for (const [id, r] of restart) next.set(id, alpha * r);+ // Dangling mass (nodes with no walk edges) is pooled and returned to the+ // seed set ONCE per iteration — same math as redistributing per node, but+ // O(nodes + seeds) instead of O(dangling × seeds).+ let dangling = 0;for (const [id, mass] of rank) {const nbrs = adj.get(id);if (!nbrs || nbrs.length === 0) {- // Dangling node: return its mass to the seed set (conserves probability- // and keeps it personalized — mass never escapes to the whole graph).- for (const [sid, r] of restart)- next.set(sid, (next.get(sid) ?? 0) + (1 - alpha) * mass * r);+ dangling += mass;continue;}const share = ((1 - alpha) * mass) / nbrs.length;for (const nb of nbrs) next.set(nb, (next.get(nb) ?? 0) + share);}+ if (dangling > 0) {+ const dm = (1 - alpha) * dangling;+ for (const [sid, r] of restart) next.set(sid, (next.get(sid) ?? 0) + dm * r);+ }rank = next;}diff --git a/test/graphrank.test.ts b/test/graphrank.test.tsindex d1c5d903..a2f9fdd5 100644--- a/test/graphrank.test.ts+++ b/test/graphrank.test.ts@@ -79,6 +79,48 @@ test("PageRank: edges to non-node targets (unresolved imports) are ignored", ()assert.equal(pr.has("react"), false, "the module string never becomes a ranked node");});+test("PageRank: dangling-mass pooling is algebraically identical to per-node redistribution", () => {+ // 5 nodes: a-b-c chain, d dangling with seed mass, e isolated non-seed.+ // Reference values captured from the pre-fix (per-dangling-node redistribution)+ // implementation, run once before the O(dangling×seeds) fix was applied.+ const g = graphOf(+ ["a", "b", "c", "d", "e"],+ [edge("a", "b"), edge("b", "c")],+ );+ const seeds = new Map([["a", 2], ["d", 1]]);+ const out = personalizedPageRank(g, seeds);+ // Captured from the pre-fix (per-dangling-node redistribution) implementation.+ const EXPECTED_A = 0.9577162737326514;+ const EXPECTED_B = 1;+ const EXPECTED_C = 0.37462976423958994;+ const EXPECTED_D = 0.29154325474653076;+ assert.ok(Math.abs((out.get("a") ?? 0) - EXPECTED_A) < 1e-9, `a: ${out.get("a")}`);+ assert.ok(Math.abs((out.get("b") ?? 0) - EXPECTED_B) < 1e-9, `b: ${out.get("b")}`);+ assert.ok(Math.abs((out.get("c") ?? 0) - EXPECTED_C) < 1e-9, `c: ${out.get("c")}`);+ assert.ok(Math.abs((out.get("d") ?? 0) - EXPECTED_D) < 1e-9, `d: ${out.get("d")}`);+ assert.equal(out.get("b"), 1, "top-ranked node is the hub linking both chain ends");+ assert.ok((out.get("a") ?? 0) > 0);+ assert.ok((out.get("d") ?? 0) > 0, "dangling seed keeps mass");+ assert.equal(out.get("e"), undefined, "unreached node absent");+});++test("PageRank: broad seeds on a large mostly-dangling graph complete fast", () => {+ // 20k dangling nodes (no edges) + 100 connected ones; seed EVERY node — the+ // pathological shape from real 32k-node graphs with common-word queries.+ // Pre-fix, per-dangling-node redistribution makes this O(dangling × seeds)+ // and takes minutes; pooled dangling mass makes it O(nodes + seeds).+ const ids = Array.from({ length: 20000 }, (_, i) => `n${i}`);+ const edges: EdgeV1[] = [];+ for (let i = 0; i < 100; i++) edges.push(edge(`n${i}`, `n${i + 1}`));+ const g = graphOf(ids, edges);+ const seeds = new Map(ids.map((id) => [id, 1]));+ const t0 = Date.now();+ const out = personalizedPageRank(g, seeds);+ const ms = Date.now() - t0;+ assert.ok(out.size > 0);+ assert.ok(ms < 3000, `took ${ms}ms — dangling redistribution must not be O(dangling × seeds)`);+});+// ── Integration: ask() with and without graph-rank ──────────────────────────/** A fixture with a same-word collision: `fooHandler` (wired to two helpers)