Skip to content

LLM Wiki is a cross-platform desktop application that turns your documents into an organized, interlinked knowledge base — automatically. Instead of traditional RAG (retrieve-and-answer from scratch every time), the LLM incrementally builds and maintains a persistent wiki from your sources。

TypeScript

fix: make sparse cluster insights scale independent

nash_su37210ca45689
diff --git a/src/components/graph/graph-view.tsx b/src/components/graph/graph-view.tsx
index d2e9806e..57139a79 100644
--- a/src/components/graph/graph-view.tsx
+++ b/src/components/graph/graph-view.tsx
@@ -1468,8 +1468,8 @@ export function GraphView() {
{c.topNodes[0] ?? `${t("graph.cluster", { id: c.id })}`}
</span>
<span className="text-muted-foreground/60 ml-auto shrink-0">{c.nodeCount}</span>
- {c.cohesion < 0.15 && c.nodeCount >= 3 && (
- <span className="text-amber-500 shrink-0" title={`Low cohesion: ${c.cohesion.toFixed(2)}`}>!</span>
+ {c.meanIntraDegree < 2 && c.nodeCount >= 3 && (
+ <span className="text-amber-500 shrink-0" title={`Low internal connectivity: ${c.meanIntraDegree.toFixed(1)} links per page`}>!</span>
)}
</div>
))}
diff --git a/src/lib/graph-insights.test.ts b/src/lib/graph-insights.test.ts
new file mode 100644
index 00000000..ef1e56dd
--- /dev/null
+++ b/src/lib/graph-insights.test.ts
@@ -0,0 +1,41 @@
+import { describe, expect, it } from "vitest"
+import { detectKnowledgeGaps } from "./graph-insights"
+import type { CommunityInfo, GraphNode } from "./wiki-graph"
+
+function nodes(count: number): GraphNode[] {
+ return Array.from({ length: count }, (_, index) => ({
+ id: `node-${index}`,
+ label: `Node ${index}`,
+ type: "concept",
+ path: `/wiki/node-${index}.md`,
+ linkCount: 4,
+ community: 0,
+ }))
+}
+
+function community(overrides: Partial<CommunityInfo>): CommunityInfo {
+ return {
+ id: 0,
+ nodeCount: 100,
+ cohesion: 0.02,
+ meanIntraDegree: 2,
+ topNodes: ["Node 0"],
+ ...overrides,
+ }
+}
+
+describe("detectKnowledgeGaps sparse communities", () => {
+ it("does not flag a large community whose mean internal degree is healthy", () => {
+ const gaps = detectKnowledgeGaps(nodes(100), [], [community({})])
+ expect(gaps.some((gap) => gap.type === "sparse-community")).toBe(false)
+ })
+
+ it("flags a community averaging fewer than two internal links per page", () => {
+ const gaps = detectKnowledgeGaps(nodes(20), [], [community({
+ nodeCount: 20,
+ meanIntraDegree: 1.4,
+ })])
+ expect(gaps.find((gap) => gap.type === "sparse-community")?.description)
+ .toContain("1.4 internal links per page")
+ })
+})
diff --git a/src/lib/graph-insights.ts b/src/lib/graph-insights.ts
index 2bb7385c..87108999 100644
--- a/src/lib/graph-insights.ts
+++ b/src/lib/graph-insights.ts
@@ -108,7 +108,7 @@ export function findSurprisingConnections(
/**
* Detect knowledge gaps based on graph structure:
* - Isolated nodes (degree ≤ 1)
- * - Sparse communities (cohesion < 0.15 with ≥ 3 nodes)
+ * - Sparse communities (fewer than 2 mean internal links per page, with ≥ 3 nodes)
* - Bridge nodes (high betweenness — connected to multiple communities)
*/
export function detectKnowledgeGaps(
@@ -136,13 +136,14 @@ export function detectKnowledgeGaps(
})
}
- // 2. Sparse communities (low cohesion)
+ // 2. Sparse communities. Raw density shrinks as communities grow, so use
+ // mean internal degree: a stable and directly actionable metric.
for (const comm of communities) {
- if (comm.cohesion < 0.15 && comm.nodeCount >= 3) {
+ if (comm.meanIntraDegree < 2 && comm.nodeCount >= 3) {
gaps.push({
type: "sparse-community",
title: `Sparse cluster: ${comm.topNodes[0] ?? `Community ${comm.id}`}`,
- description: `${comm.nodeCount} pages with cohesion ${comm.cohesion.toFixed(2)} — internal connections are weak.`,
+ description: `${comm.nodeCount} pages average ${comm.meanIntraDegree.toFixed(1)} internal links per page.`,
nodeIds: nodes.filter((n) => n.community === comm.id).map((n) => n.id),
suggestion: `This knowledge area lacks internal cross-references. Consider adding links between these pages or researching to fill gaps.`,
})
diff --git a/src/lib/wiki-graph-analysis.test.ts b/src/lib/wiki-graph-analysis.test.ts
index 7c47d8d9..081d752a 100644
--- a/src/lib/wiki-graph-analysis.test.ts
+++ b/src/lib/wiki-graph-analysis.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest"
-import { detectCommunities } from "./wiki-graph-analysis"
+import { computeCommunityConnectivity, detectCommunities } from "./wiki-graph-analysis"
describe("wiki graph community analysis", () => {
it("computes cohesion from internal edges without counting cross-community edges", () => {
@@ -19,6 +19,16 @@ describe("wiki graph community analysis", () => {
expect(result.assignments.size).toBe(4)
expect(result.communities.reduce((sum, community) => sum + community.nodeCount, 0)).toBe(4)
expect(result.communities.every((community) => community.cohesion >= 0 && community.cohesion <= 1)).toBe(true)
+ expect(result.communities.every((community) => community.meanIntraDegree >= 0)).toBe(true)
expect(result.communities.flatMap((community) => community.topNodes)).toContain("A")
})
+
+ it("keeps mean internal degree stable for equally connected communities of different sizes", () => {
+ const small = computeCommunityConnectivity(8, 8)
+ const large = computeCommunityConnectivity(80, 80)
+
+ expect(small.meanIntraDegree).toBe(2)
+ expect(large.meanIntraDegree).toBe(2)
+ expect(large.cohesion).toBeLessThan(small.cohesion)
+ })
})
diff --git a/src/lib/wiki-graph-analysis.ts b/src/lib/wiki-graph-analysis.ts
index cf88fdb1..3ff963af 100644
--- a/src/lib/wiki-graph-analysis.ts
+++ b/src/lib/wiki-graph-analysis.ts
@@ -2,6 +2,17 @@ import Graph from "graphology"
import louvain from "graphology-communities-louvain"
import type { CommunityInfo, GraphEdge } from "./wiki-graph"
+export function computeCommunityConnectivity(nodeCount: number, intraEdges: number): {
+ cohesion: number
+ meanIntraDegree: number
+} {
+ const possibleEdges = nodeCount > 1 ? (nodeCount * (nodeCount - 1)) / 2 : 1
+ return {
+ cohesion: intraEdges / possibleEdges,
+ meanIntraDegree: nodeCount > 0 ? (2 * intraEdges) / nodeCount : 0,
+ }
+}
+
/** Run Louvain community detection and compute cohesion per community. */
export function detectCommunities(
nodes: { id: string; label: string; linkCount: number }[],
@@ -51,8 +62,8 @@ export function detectCommunities(
const communities: CommunityInfo[] = []
for (const [communityId, memberIds] of groups) {
const nodeCount = memberIds.length
- const possibleEdges = nodeCount > 1 ? (nodeCount * (nodeCount - 1)) / 2 : 1
- const cohesion = (intraEdgesByCommunity.get(communityId) ?? 0) / possibleEdges
+ const intraEdges = intraEdgesByCommunity.get(communityId) ?? 0
+ const { cohesion, meanIntraDegree } = computeCommunityConnectivity(nodeCount, intraEdges)
const topNodes = [...memberIds]
.sort(
(left, right) =>
@@ -61,7 +72,7 @@ export function detectCommunities(
)
.slice(0, 5)
.map((id) => nodeInfo.get(id)?.label ?? id)
- communities.push({ id: communityId, nodeCount, cohesion, topNodes })
+ communities.push({ id: communityId, nodeCount, cohesion, meanIntraDegree, topNodes })
}
communities.sort((left, right) => right.nodeCount - left.nodeCount)
diff --git a/src/lib/wiki-graph.ts b/src/lib/wiki-graph.ts
index ffdf77a2..5241cbff 100644
--- a/src/lib/wiki-graph.ts
+++ b/src/lib/wiki-graph.ts
@@ -24,6 +24,7 @@ export interface CommunityInfo {
id: number
nodeCount: number
cohesion: number // intra-community edge density
+ meanIntraDegree: number // average internal links per page; scale-independent
topNodes: string[] // top nodes by linkCount (labels)
}