@dreamtree-org/graphify 1.3.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-K322XB4U.js → chunk-7LTO76UD.js} +2 -1
- package/dist/chunk-7LTO76UD.js.map +1 -0
- package/dist/{chunk-UBXYMMXJ.js → chunk-EHMBINRV.js} +115 -102
- package/dist/chunk-EHMBINRV.js.map +1 -0
- package/dist/cli/index.cjs +19 -7
- package/dist/cli/index.cjs.map +1 -1
- package/dist/cli/index.js +2 -2
- package/dist/index.cjs +564 -108
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +229 -17
- package/dist/index.d.ts +229 -17
- package/dist/index.js +442 -2
- package/dist/index.js.map +1 -1
- package/dist/mcp/server.cjs.map +1 -1
- package/dist/mcp/server.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-K322XB4U.js.map +0 -1
- package/dist/chunk-UBXYMMXJ.js.map +0 -1
|
@@ -604,6 +604,7 @@ export {
|
|
|
604
604
|
loadGraph,
|
|
605
605
|
scoreNodes,
|
|
606
606
|
resolveNode,
|
|
607
|
+
nodeTokenCost,
|
|
607
608
|
queryGraph,
|
|
608
609
|
shortestPath,
|
|
609
610
|
explainNode,
|
|
@@ -615,4 +616,4 @@ export {
|
|
|
615
616
|
testsForNode,
|
|
616
617
|
testsForChangedFiles
|
|
617
618
|
};
|
|
618
|
-
//# sourceMappingURL=chunk-
|
|
619
|
+
//# sourceMappingURL=chunk-7LTO76UD.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/store/serialize.ts","../src/store/localFile.ts","../src/graphStore.ts","../src/query.ts","../src/impact.ts","../src/context.ts","../src/testmap.ts"],"sourcesContent":["import Graph from 'graphology';\nimport type { SerializedGraph } from './types.js';\n\n/**\n * The stable serialization contract for graphs at rest: graphology's\n * node-link JSON. exportGraph() has always written exactly this shape to\n * graph.json, so anything serialized here stays loadable by every existing\n * consumer (and vice versa).\n */\nexport function serializeGraph(graph: Graph): SerializedGraph {\n return graph.export();\n}\n\nexport function deserializeGraph(data: SerializedGraph): Graph {\n return Graph.from(data);\n}\n","import * as fs from 'node:fs/promises';\nimport type Graph from 'graphology';\nimport { validateGraphPath } from '../security.js';\nimport { deserializeGraph, serializeGraph } from './serialize.js';\nimport type { GraphStore, SerializedGraph } from './types.js';\n\n/**\n * The reference GraphStore: one graph.json per directory, `ref` = the\n * outDir path. Reproduces the classic graphify-out/ behavior byte-for-byte\n * (same pretty-printed JSON exportGraph() writes) and routes every path\n * through security.validateGraphPath(), so a caller-supplied ref — or a\n * symlinked graph.json — can never read or write outside the directory it\n * resolves to.\n */\nexport class LocalFileGraphStore implements GraphStore {\n async load(ref: string): Promise<Graph | null> {\n let jsonPath: string;\n try {\n jsonPath = validateGraphPath('graph.json', ref);\n } catch (error) {\n // A ref whose directory doesn't exist simply has no graph yet;\n // traversal/symlink-escape rejections must still propagate.\n if ((error as Error).message.startsWith('Base directory does not exist')) return null;\n throw error;\n }\n\n let raw: string;\n try {\n raw = await fs.readFile(jsonPath, 'utf-8');\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') return null;\n throw error;\n }\n return deserializeGraph(JSON.parse(raw) as SerializedGraph);\n }\n\n async save(ref: string, graph: Graph): Promise<void> {\n await fs.mkdir(ref, { recursive: true });\n const jsonPath = validateGraphPath('graph.json', ref);\n await fs.writeFile(jsonPath, JSON.stringify(serializeGraph(graph), null, 2), 'utf-8');\n }\n}\n","import * as path from 'node:path';\nimport type Graph from 'graphology';\nimport { LocalFileGraphStore } from './store/localFile.js';\n\n/**\n * Load a previously-exported graph.json back into a graphology Graph.\n * Thin wrapper over LocalFileGraphStore (which enforces the path-traversal\n * guard — see SECURITY.md) that throws when no graph exists, because every\n * CLI/MCP caller treats \"no graph yet\" as a hard error. Library callers who\n * want the null-on-missing contract should use a GraphStore directly.\n */\nexport async function loadGraph(outDir: string = path.join(process.cwd(), 'graphify-out')): Promise<Graph> {\n const graph = await new LocalFileGraphStore().load(outDir);\n if (graph === null) {\n throw new Error(`No graph found in ${outDir} — run \\`graphify <path>\\` to build one first.`);\n }\n return graph;\n}\n","import type Graph from 'graphology';\n\n/**\n * Library-level graph queries shared by the CLI (`graphify query/path/\n * explain`) and the MCP server tools of the same name. None of this is an\n * LLM call — it's lexical node matching + graph traversal, so it works\n * fully offline and deterministically.\n */\n\nconst STOPWORDS = new Set([\n 'a', 'an', 'the', 'is', 'are', 'was', 'were', 'do', 'does', 'did', 'how',\n 'what', 'where', 'when', 'why', 'who', 'which', 'to', 'of', 'in', 'on',\n 'for', 'and', 'or', 'this', 'that', 'it', 'does', 'that\\'s',\n]);\n\n/** Insert spaces at camelCase boundaries so identifier words become separate tokens. */\nfunction splitCamelCase(text: string): string {\n return text\n .replace(/([a-z0-9])([A-Z])/g, '$1 $2')\n .replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2');\n}\n\nfunction tokenize(text: string): string[] {\n return splitCamelCase(text)\n .toLowerCase()\n .split(/[^a-z0-9]+/)\n .filter((token) => token.length > 1 && !STOPWORDS.has(token));\n}\n\n/**\n * Split an identifier-ish string (node label/id) into its word subtokens:\n * camelCase, snake_case, kebab-case, dots, and path separators all become\n * boundaries, so `src/a/fileStorage.js::saveAttachment` yields\n * `src, file, storage, js, save, attachment`.\n */\nfunction subtokenize(text: string): string[] {\n return splitCamelCase(text)\n .toLowerCase()\n .split(/[^a-z0-9]+/)\n .filter((token) => token.length > 1);\n}\n\n/** Don't run the fuzzy tier on tokens this short — too many false positives. */\nconst FUZZY_MIN_TOKEN_LEN = 3;\n/** Minimum similarity for a fuzzy match to count at all. */\nconst FUZZY_THRESHOLD = 0.7;\nconst SUBTOKEN_MATCH_SCORE = 1;\nconst SUBSTRING_MATCH_SCORE = 0.6;\nconst FUZZY_MATCH_WEIGHT = 0.5;\n\n/**\n * Optimal-string-alignment Damerau-Levenshtein distance. Chosen over\n * bigram-set similarity because the common code-search typo is a\n * transposition (\"sorceNodes\"), which OSA counts as one edit but bigram\n * overlap punishes brutally.\n */\nfunction osaDistance(a: string, b: string): number {\n let prev2: number[] = [];\n let prev: number[] = Array.from({ length: b.length + 1 }, (_, j) => j);\n let curr: number[] = new Array<number>(b.length + 1).fill(0);\n\n for (let i = 1; i <= a.length; i++) {\n curr[0] = i;\n for (let j = 1; j <= b.length; j++) {\n const substitution = a[i - 1] === b[j - 1] ? 0 : 1;\n let best = Math.min(\n (prev[j] as number) + 1,\n (curr[j - 1] as number) + 1,\n (prev[j - 1] as number) + substitution,\n );\n if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {\n best = Math.min(best, (prev2[j - 2] as number) + 1);\n }\n curr[j] = best;\n }\n [prev2, prev, curr] = [prev, curr, new Array<number>(b.length + 1).fill(0)];\n }\n return prev[b.length] as number;\n}\n\n/** Normalized similarity in [0, 1]; short-circuits pairs whose length gap alone puts them under FUZZY_THRESHOLD. */\nfunction fuzzySimilarity(a: string, b: string): number {\n if (a === b) return 1;\n const maxLen = Math.max(a.length, b.length);\n if (maxLen === 0) return 1;\n if (Math.abs(a.length - b.length) / maxLen > 1 - FUZZY_THRESHOLD) return 0;\n return 1 - osaDistance(a, b) / maxLen;\n}\n\nexport interface NodeMatch {\n id: string;\n label: string;\n score: number;\n}\n\n/**\n * Score every node against the query's tokens. Identifier-aware and\n * typo-tolerant, in three tiers per token (best tier wins):\n *\n * 1. exact subtoken match (camelCase/snake_case-split word of the label\n * or id) — \"storage\" matches `fileStorage.js`;\n * 2. plain substring of the label/id (the original v1 behavior);\n * 3. fuzzy (Damerau-Levenshtein) match against a subtoken — \"sorce\"\n * still finds `scoreNodes`.\n *\n * Still fully lexical and deterministic — no LLM, no randomness.\n */\nexport function scoreNodes(graph: Graph, query: string): NodeMatch[] {\n const tokens = tokenize(query);\n const matches: NodeMatch[] = [];\n\n graph.forEachNode((id, attributes) => {\n const label = (attributes.label as string | undefined) ?? id;\n const haystack = `${label} ${id}`.toLowerCase();\n const subtokens = new Set(subtokenize(`${label} ${id}`));\n let score = 0;\n for (const token of tokens) {\n if (subtokens.has(token)) {\n score += SUBTOKEN_MATCH_SCORE;\n continue;\n }\n if (haystack.includes(token)) {\n score += SUBSTRING_MATCH_SCORE;\n continue;\n }\n if (token.length >= FUZZY_MIN_TOKEN_LEN) {\n let best = 0;\n for (const subtoken of subtokens) {\n const similarity = fuzzySimilarity(token, subtoken);\n if (similarity > best) best = similarity;\n }\n if (best >= FUZZY_THRESHOLD) score += best * FUZZY_MATCH_WEIGHT;\n }\n }\n if (score > 0) matches.push({ id, label, score });\n });\n\n // On equal score, real source nodes beat module:/external: placeholders —\n // \"devtools\" should resolve to the devtools middleware, not the\n // @redux-devtools package node that happens to share a subtoken.\n const placeholderRank = (id: string): number =>\n id.startsWith('module:') || id.startsWith('external:') ? 1 : 0;\n matches.sort(\n (a, b) =>\n b.score - a.score || placeholderRank(a.id) - placeholderRank(b.id) || a.id.localeCompare(b.id),\n );\n return matches;\n}\n\n/**\n * Best-effort single-node resolution: the highest-scoring lexical match,\n * or null if nothing in the graph matches at all. Deliberately does *not*\n * fall back to \"the most connected node\" — path/explain name a specific\n * node by intent, and silently substituting an unrelated one would be\n * more misleading than clearly saying \"no match\".\n */\nexport function resolveNode(graph: Graph, query: string): NodeMatch | null {\n const matches = scoreNodes(graph, query);\n return matches[0] ?? null;\n}\n\nexport interface VisitedNode {\n id: string;\n label: string;\n sourceFile: string;\n sourceLocation: string;\n depth: number;\n}\n\nexport interface TraversalEdge {\n source: string;\n target: string;\n relation: string;\n confidence: string;\n}\n\nexport interface QueryResult {\n seeds: NodeMatch[];\n visited: VisitedNode[];\n edges: TraversalEdge[];\n}\n\nexport interface QueryOptions {\n dfs?: boolean;\n /** Hard cap on how many nodes to include. Defaults to a generous ceiling derived from `tokenBudget`. */\n budget?: number;\n /** Approximate cap, in tokens (~4 chars each), on the serialized size of the result. Default 2000. */\n tokenBudget?: number;\n maxDepth?: number;\n maxSeeds?: number;\n}\n\nconst DEFAULT_MAX_DEPTH = 2;\nconst DEFAULT_MAX_SEEDS = 5;\nconst DEFAULT_BUDGET = 40;\nconst DEFAULT_TOKEN_BUDGET = 2000;\n/** When only tokenBudget is given, allow up to this many nodes per budgeted token-decile as a safety ceiling. */\nconst NODES_PER_TOKEN = 1 / 10;\n\n/**\n * Estimated token contribution of one node to the rendered result: its own\n * line (label, file, location) plus a rough allowance for the edge lines\n * that mention its id. Chars/4 is the usual serviceable approximation.\n */\nexport function nodeTokenCost(graph: Graph, id: string): number {\n const attrs = graph.getNodeAttributes(id);\n const label = (attrs.label as string | undefined) ?? id;\n const sourceFile = (attrs.sourceFile as string | undefined) ?? '';\n return Math.ceil((id.length * 2 + label.length + sourceFile.length + 24) / 4);\n}\n\n/**\n * BFS (default, broad context) or DFS (--dfs, trace a specific path)\n * traversal from the nodes whose label best matches `question`'s\n * keywords. This is lexical retrieval, not natural-language\n * understanding — it surfaces the neighborhood of the graph an agent (or\n * a human) should look at to answer the question, it does not itself\n * compose an answer.\n */\nexport function queryGraph(graph: Graph, question: string, options: QueryOptions = {}): QueryResult {\n const maxDepth = options.maxDepth ?? DEFAULT_MAX_DEPTH;\n const maxSeeds = options.maxSeeds ?? DEFAULT_MAX_SEEDS;\n const tokenBudget = options.tokenBudget ?? DEFAULT_TOKEN_BUDGET;\n const budget = options.budget ?? Math.max(DEFAULT_BUDGET, Math.ceil(tokenBudget * NODES_PER_TOKEN));\n\n const allMatches = scoreNodes(graph, question);\n const seeds = allMatches.length > 0 ? allMatches.slice(0, maxSeeds) : [];\n\n const visited = new Map<string, number>(); // id -> depth\n const frontier: Array<{ id: string; depth: number }> = seeds.map((s) => ({ id: s.id, depth: 0 }));\n let spentTokens = 0;\n for (const seed of seeds) {\n visited.set(seed.id, 0);\n spentTokens += nodeTokenCost(graph, seed.id);\n }\n\n while (frontier.length > 0 && visited.size < budget && spentTokens < tokenBudget) {\n const current = options.dfs ? (frontier.pop() as { id: string; depth: number }) : (frontier.shift() as { id: string; depth: number });\n if (current.depth >= maxDepth) continue;\n\n const neighbors = [...graph.neighbors(current.id)].sort((a, b) => a.localeCompare(b));\n for (const neighbor of neighbors) {\n if (visited.has(neighbor) || visited.size >= budget || spentTokens >= tokenBudget) continue;\n visited.set(neighbor, current.depth + 1);\n spentTokens += nodeTokenCost(graph, neighbor);\n frontier.push({ id: neighbor, depth: current.depth + 1 });\n }\n }\n\n const visitedNodes: VisitedNode[] = [...visited.entries()]\n .map(([id, depth]) => {\n const attrs = graph.getNodeAttributes(id);\n return {\n id,\n label: (attrs.label as string | undefined) ?? id,\n sourceFile: (attrs.sourceFile as string | undefined) ?? '',\n sourceLocation: (attrs.sourceLocation as string | undefined) ?? '',\n depth,\n };\n })\n .sort((a, b) => a.depth - b.depth || a.id.localeCompare(b.id));\n\n const edges: TraversalEdge[] = [];\n graph.forEachEdge((_edge, attrs, source, target) => {\n if (visited.has(source) && visited.has(target)) {\n edges.push({\n source,\n target,\n relation: String(attrs.relation),\n confidence: String(attrs.confidence),\n });\n }\n });\n edges.sort(\n (a, b) => a.source.localeCompare(b.source) || a.target.localeCompare(b.target) || a.relation.localeCompare(b.relation),\n );\n\n return enforceTokenBudget({ seeds, visited: visitedNodes, edges }, tokenBudget);\n}\n\n/**\n * The traversal-time cost estimate can't see how densely connected the\n * visited neighborhood is — a hub-heavy subgraph induces far more edge\n * lines than nodes. Enforce the budget on the *actual* result: drop the\n * deepest/last nodes (and the edges that touched them) until the\n * serialized size fits. Seeds are never dropped.\n */\nfunction enforceTokenBudget(result: QueryResult, tokenBudget: number): QueryResult {\n const cost = (value: unknown): number => Math.ceil(JSON.stringify(value).length / 4);\n\n const visited = [...result.visited];\n let edges = [...result.edges];\n let total = cost(result.seeds) + visited.reduce((s, n) => s + cost(n), 0) + edges.reduce((s, e) => s + cost(e), 0);\n\n const seedIds = new Set(result.seeds.map((s) => s.id));\n // visited is sorted by (depth, id) — pop from the end so the deepest,\n // least-central context goes first and the result stays deterministic.\n while (total > tokenBudget && visited.length > 0) {\n const last = visited[visited.length - 1] as VisitedNode;\n if (seedIds.has(last.id)) break;\n visited.pop();\n total -= cost(last);\n const remaining: TraversalEdge[] = [];\n for (const edge of edges) {\n if (edge.source === last.id || edge.target === last.id) {\n total -= cost(edge);\n } else {\n remaining.push(edge);\n }\n }\n edges = remaining;\n }\n\n return { seeds: result.seeds, visited, edges };\n}\n\nexport interface PathResult {\n from: NodeMatch;\n to: NodeMatch;\n found: boolean;\n path: string[];\n edges: TraversalEdge[];\n}\n\n/** Shortest path between the nodes that best match `fromQuery` and `toQuery` (undirected BFS — connectivity, not call direction). */\nexport function shortestPath(graph: Graph, fromQuery: string, toQuery: string): PathResult | null {\n const from = resolveNode(graph, fromQuery);\n const to = resolveNode(graph, toQuery);\n if (!from || !to) return null;\n\n if (from.id === to.id) {\n return { from, to, found: true, path: [from.id], edges: [] };\n }\n\n const predecessor = new Map<string, string>();\n const visited = new Set<string>([from.id]);\n const queue: string[] = [from.id];\n\n while (queue.length > 0) {\n const current = queue.shift() as string;\n if (current === to.id) break;\n const neighbors = [...graph.neighbors(current)].sort((a, b) => a.localeCompare(b));\n for (const neighbor of neighbors) {\n if (visited.has(neighbor)) continue;\n visited.add(neighbor);\n predecessor.set(neighbor, current);\n queue.push(neighbor);\n }\n }\n\n if (!visited.has(to.id)) {\n return { from, to, found: false, path: [], edges: [] };\n }\n\n const path: string[] = [to.id];\n let cursor = to.id;\n while (cursor !== from.id) {\n const prev = predecessor.get(cursor);\n if (!prev) break;\n path.unshift(prev);\n cursor = prev;\n }\n\n const edges: TraversalEdge[] = [];\n for (let i = 0; i < path.length - 1; i++) {\n const a = path[i] as string;\n const b = path[i + 1] as string;\n const key = graph.edges(a, b)[0] ?? graph.edges(b, a)[0];\n if (key) {\n const attrs = graph.getEdgeAttributes(key);\n edges.push({ source: a, target: b, relation: String(attrs.relation), confidence: String(attrs.confidence) });\n }\n }\n\n return { from, to, found: true, path, edges };\n}\n\nexport interface ExplainResult {\n id: string;\n label: string;\n sourceFile: string;\n sourceLocation: string;\n communityLabel?: string;\n outgoing: TraversalEdge[];\n incoming: TraversalEdge[];\n}\n\n/** Plain-language-ready explanation of the node that best matches `query`. */\nexport function explainNode(graph: Graph, query: string): ExplainResult | null {\n const match = resolveNode(graph, query);\n if (!match) return null;\n\n const attrs = graph.getNodeAttributes(match.id);\n const outgoing: TraversalEdge[] = [];\n const incoming: TraversalEdge[] = [];\n\n graph.forEachOutEdge(match.id, (_edge, edgeAttrs, source, target) => {\n outgoing.push({ source, target, relation: String(edgeAttrs.relation), confidence: String(edgeAttrs.confidence) });\n });\n graph.forEachInEdge(match.id, (_edge, edgeAttrs, source, target) => {\n incoming.push({ source, target, relation: String(edgeAttrs.relation), confidence: String(edgeAttrs.confidence) });\n });\n\n outgoing.sort((a, b) => a.target.localeCompare(b.target) || a.relation.localeCompare(b.relation));\n incoming.sort((a, b) => a.source.localeCompare(b.source) || a.relation.localeCompare(b.relation));\n\n return {\n id: match.id,\n label: match.label,\n sourceFile: (attrs.sourceFile as string | undefined) ?? '',\n sourceLocation: (attrs.sourceLocation as string | undefined) ?? '',\n communityLabel: attrs.communityLabel as string | undefined,\n outgoing,\n incoming,\n };\n}\n","import type Graph from 'graphology';\nimport { resolveNode, type NodeMatch } from './query.js';\nimport type { Confidence, Relation } from './types.js';\n\n/**\n * Reverse impact analysis — \"what breaks if I change X\". Walks *incoming*\n * dependency edges (who calls / imports / inherits from the target)\n * transitively, so depth 1 is the direct blast radius and deeper levels are\n * ripple effects. Like everything in query.ts this is lexical + structural,\n * fully offline and deterministic.\n */\n\n/**\n * Relations where `source --rel--> target` means \"source depends on target\",\n * i.e. changing the target can break the source. `contains`/`method` are\n * included because changing an entity plausibly affects its container file\n * or class signature consumers see.\n */\nconst DEPENDENCY_RELATIONS: ReadonlySet<Relation> = new Set([\n 'calls',\n 'imports',\n 'imports_from',\n 'inherits',\n 'implements',\n 'mixes_in',\n 'embeds',\n 'references',\n 're_exports',\n 'method',\n 'contains',\n] satisfies Relation[]);\n\nexport interface AffectedNode {\n id: string;\n label: string;\n sourceFile: string;\n sourceLocation: string;\n /** 1 = depends on the target directly, 2 = one step removed, ... */\n depth: number;\n /** The dependency edge that pulled this node in (this node -> something already affected). */\n via: {\n relation: Relation;\n confidence: Confidence;\n /** The already-affected node this one depends on. */\n dependsOn: string;\n };\n}\n\nexport interface ImpactResult {\n target: NodeMatch;\n affected: AffectedNode[];\n /** How many affected nodes there are per confidence tier of the edge that pulled each one in. */\n byConfidence: Record<Confidence, number>;\n maxDepth: number;\n truncated: boolean;\n}\n\nexport interface ImpactOptions {\n /** How many reverse hops to follow (default 3). */\n maxDepth?: number;\n /** Hard cap on affected nodes reported (default 200). */\n limit?: number;\n /** Restrict the traversal to these relations (default: every dependency-carrying relation). */\n relations?: Relation[];\n}\n\nconst DEFAULT_IMPACT_DEPTH = 3;\nconst DEFAULT_IMPACT_LIMIT = 200;\n\nconst CONFIDENCE_RANK: Record<Confidence, number> = { EXTRACTED: 3, INFERRED: 2, AMBIGUOUS: 1 };\n\n/**\n * Resolve `nodeQuery` to its best-matching node, then reverse-BFS over\n * incoming dependency edges. Returns null when nothing in the graph matches\n * the query at all (same contract as explainNode()).\n */\nexport function affectedBy(graph: Graph, nodeQuery: string, options: ImpactOptions = {}): ImpactResult | null {\n const target = resolveNode(graph, nodeQuery);\n if (!target) return null;\n\n const maxDepth = options.maxDepth ?? DEFAULT_IMPACT_DEPTH;\n const limit = options.limit ?? DEFAULT_IMPACT_LIMIT;\n const followed: ReadonlySet<Relation> = options.relations ? new Set(options.relations) : DEPENDENCY_RELATIONS;\n\n const affected: AffectedNode[] = [];\n const visited = new Set<string>([target.id]);\n let frontier: string[] = [target.id];\n let truncated = false;\n\n for (let depth = 1; depth <= maxDepth && frontier.length > 0 && !truncated; depth++) {\n // Collect every dependent of the current frontier before descending, so\n // each node's reported depth is its *shortest* reverse distance.\n const nextFrontier = new Map<string, AffectedNode>();\n\n for (const current of [...frontier].sort((a, b) => a.localeCompare(b))) {\n graph.forEachInEdge(current, (_edge, edgeAttrs, source) => {\n if (visited.has(source)) return;\n const relation = edgeAttrs.relation as Relation;\n if (!followed.has(relation)) return;\n\n const confidence = edgeAttrs.confidence as Confidence;\n const existing = nextFrontier.get(source);\n // Same node reachable via several edges this round: keep the\n // strongest-confidence one, mirroring buildGraph()'s merge rule.\n if (existing && CONFIDENCE_RANK[existing.via.confidence] >= CONFIDENCE_RANK[confidence]) return;\n\n const attrs = graph.getNodeAttributes(source);\n nextFrontier.set(source, {\n id: source,\n label: (attrs.label as string | undefined) ?? source,\n sourceFile: (attrs.sourceFile as string | undefined) ?? '',\n sourceLocation: (attrs.sourceLocation as string | undefined) ?? '',\n depth,\n via: { relation, confidence, dependsOn: current },\n });\n });\n }\n\n const roundNodes = [...nextFrontier.values()].sort((a, b) => a.id.localeCompare(b.id));\n for (const node of roundNodes) {\n if (affected.length >= limit) {\n truncated = true;\n break;\n }\n visited.add(node.id);\n affected.push(node);\n }\n frontier = roundNodes.filter((n) => visited.has(n.id)).map((n) => n.id);\n }\n\n const byConfidence: Record<Confidence, number> = { EXTRACTED: 0, INFERRED: 0, AMBIGUOUS: 0 };\n for (const node of affected) byConfidence[node.via.confidence] += 1;\n\n return { target, affected, byConfidence, maxDepth, truncated };\n}\n","import type Graph from 'graphology';\nimport { scoreNodes } from './query.js';\n\n/**\n * `graphify context` — the working-set pack. Instead of returning node\n * names for the agent to chase (query -> then read whole files anyway),\n * this fuses the two steps: the graph knows every entity's file and line,\n * so rank the nodes relevant to the task, read just the relevant line\n * ranges, and pack the actual code into a token budget. One call returns\n * the code an agent needs to start a task.\n */\n\nexport interface ContextSnippet {\n nodeId: string;\n label: string;\n file: string;\n startLine: number;\n endLine: number;\n code: string;\n /** Why this snippet is in the pack (lexical match / neighbor-of relation). */\n reason: string;\n score: number;\n}\n\nexport interface ContextPack {\n task: string;\n snippets: ContextSnippet[];\n /** Approximate tokens used out of the budget. */\n tokens: number;\n tokenBudget: number;\n /** Ranked nodes that didn't fit the budget — the agent can pull them explicitly. */\n overflow: Array<{ nodeId: string; file: string }>;\n}\n\nexport interface ContextOptions {\n /** Approximate token cap for the pack (default 4000). */\n tokenBudget?: number;\n maxSeeds?: number;\n /** Cap on snippet length in lines (default 60). */\n maxSnippetLines?: number;\n}\n\nexport type FileReader = (path: string) => Promise<string>;\n\nconst DEFAULT_CONTEXT_BUDGET = 4000;\nconst DEFAULT_MAX_SEEDS = 8;\nconst DEFAULT_MAX_SNIPPET_LINES = 60;\n/** A neighbor inherits this fraction of the score of the node that pulled it in. */\nconst NEIGHBOR_DECAY = 0.4;\n\nconst tokensOf = (text: string): number => Math.ceil(text.length / 4);\n\nfunction lineNumberOf(sourceLocation: string): number | null {\n const match = /^L(\\d+)$/.exec(sourceLocation);\n return match ? Number(match[1]) : null;\n}\n\nfunction isReadableFile(sourceFile: string): boolean {\n return sourceFile !== '' && !sourceFile.startsWith('<') && !sourceFile.includes('://');\n}\n\ninterface RankedNode {\n id: string;\n score: number;\n reason: string;\n}\n\n/**\n * Rank nodes for the task: lexical seeds first (their match score), then\n * their graph neighbors at a decayed score, labeled with the relation that\n * connects them. Deterministic (score desc, id asc).\n */\nexport function rankForTask(graph: Graph, task: string, maxSeeds = DEFAULT_MAX_SEEDS): RankedNode[] {\n const seeds = scoreNodes(graph, task).slice(0, maxSeeds);\n const ranked = new Map<string, RankedNode>();\n for (const seed of seeds) {\n ranked.set(seed.id, { id: seed.id, score: seed.score, reason: 'matches the task' });\n }\n\n for (const seed of seeds) {\n graph.forEachEdge(seed.id, (_edge, attrs, source, target) => {\n const neighbor = source === seed.id ? target : source;\n if (ranked.has(neighbor)) return;\n const relation = String(attrs.relation);\n const direction = source === seed.id ? `${relation} ->` : `<- ${relation}`;\n ranked.set(neighbor, {\n id: neighbor,\n score: seed.score * NEIGHBOR_DECAY,\n reason: `${direction} ${seed.label}`,\n });\n });\n }\n\n return [...ranked.values()].sort((a, b) => b.score - a.score || a.id.localeCompare(b.id));\n}\n\n/**\n * Extract the snippet for a node: from its declaration line to just before\n * the next known entity in the same file (capped), so snippets align with\n * real code block boundaries without needing a parser here.\n */\nfunction snippetRange(\n startLine: number,\n fileLineCount: number,\n entityStartsInFile: number[],\n maxLines: number,\n): { start: number; end: number } {\n const nextStart = entityStartsInFile.find((l) => l > startLine);\n const hardEnd = nextStart !== undefined ? nextStart - 1 : fileLineCount;\n return { start: startLine, end: Math.min(hardEnd, startLine + maxLines - 1) };\n}\n\nexport async function buildContextPack(\n graph: Graph,\n task: string,\n readFile: FileReader,\n options: ContextOptions = {},\n): Promise<ContextPack> {\n const tokenBudget = options.tokenBudget ?? DEFAULT_CONTEXT_BUDGET;\n const maxSnippetLines = options.maxSnippetLines ?? DEFAULT_MAX_SNIPPET_LINES;\n const ranked = rankForTask(graph, task, options.maxSeeds);\n\n // Every known entity start line per file — used to end snippets at the\n // next declaration instead of an arbitrary window.\n const entityStarts = new Map<string, number[]>();\n graph.forEachNode((_id, attrs) => {\n const file = attrs.sourceFile as string | undefined;\n const line = lineNumberOf((attrs.sourceLocation as string | undefined) ?? '');\n if (file && line !== null) {\n const starts = entityStarts.get(file) ?? [];\n starts.push(line);\n entityStarts.set(file, starts);\n }\n });\n for (const starts of entityStarts.values()) starts.sort((a, b) => a - b);\n\n const fileCache = new Map<string, string[] | null>();\n const readLines = async (file: string): Promise<string[] | null> => {\n const cached = fileCache.get(file);\n if (cached !== undefined) return cached;\n let lines: string[] | null;\n try {\n lines = (await readFile(file)).split('\\n');\n } catch {\n lines = null;\n }\n fileCache.set(file, lines);\n return lines;\n };\n\n const snippets: ContextSnippet[] = [];\n const overflow: Array<{ nodeId: string; file: string }> = [];\n const coveredRanges = new Map<string, Array<{ start: number; end: number }>>();\n let tokens = 0;\n\n for (const node of ranked) {\n const attrs = graph.getNodeAttributes(node.id);\n const file = (attrs.sourceFile as string | undefined) ?? '';\n const startLine = lineNumberOf((attrs.sourceLocation as string | undefined) ?? '');\n if (!isReadableFile(file) || startLine === null) continue;\n\n const lines = await readLines(file);\n if (lines === null) continue;\n\n const { start, end } = snippetRange(startLine, lines.length, entityStarts.get(file) ?? [], maxSnippetLines);\n\n // Skip if an already-packed snippet from the same file covers this range.\n const covered = (coveredRanges.get(file) ?? []).some((r) => start >= r.start && end <= r.end);\n if (covered) continue;\n\n const code = lines.slice(start - 1, end).join('\\n');\n const cost = tokensOf(code) + 15; // header overhead\n if (tokens + cost > tokenBudget) {\n overflow.push({ nodeId: node.id, file: `${file}:L${startLine}` });\n continue;\n }\n\n tokens += cost;\n snippets.push({\n nodeId: node.id,\n label: (attrs.label as string | undefined) ?? node.id,\n file,\n startLine: start,\n endLine: end,\n code,\n reason: node.reason,\n score: node.score,\n });\n const ranges = coveredRanges.get(file) ?? [];\n ranges.push({ start, end });\n coveredRanges.set(file, ranges);\n }\n\n return { task, snippets, tokens, tokenBudget, overflow };\n}\n\n/** Render a pack as agent-ready markdown. */\nexport function renderContextPack(pack: ContextPack): string {\n const lines: string[] = [\n `# Context pack: ${pack.task}`,\n '',\n `_~${pack.tokens} of ${pack.tokenBudget} token budget, ${pack.snippets.length} snippet(s)._`,\n '',\n ];\n\n if (pack.snippets.length === 0) {\n lines.push('No matching code found — try different keywords.');\n return lines.join('\\n');\n }\n\n for (const snippet of pack.snippets) {\n lines.push(`## ${snippet.label} — \\`${snippet.file}:L${snippet.startLine}-L${snippet.endLine}\\``);\n lines.push(`_${snippet.reason}_`);\n lines.push('```');\n lines.push(snippet.code);\n lines.push('```');\n lines.push('');\n }\n\n if (pack.overflow.length > 0) {\n lines.push(`## Didn't fit the budget (${pack.overflow.length})`);\n for (const item of pack.overflow.slice(0, 15)) {\n lines.push(`- ${item.nodeId} (${item.file})`);\n }\n lines.push('');\n }\n\n return lines.join('\\n');\n}\n","import type Graph from 'graphology';\nimport { affectedBy } from './impact.js';\nimport { resolveNode } from './query.js';\nimport type { Relation } from './types.js';\n\n/**\n * `graphify tests` — structural test selection. Coverage tools need\n * instrumentation and a full test run to know what covers what; the graph\n * already knows, statically: a test file imports/calls the code it\n * exercises, so the test files inside a change's reverse blast radius are\n * exactly the ones worth running. Cross-language, no instrumentation.\n */\n\n/** Test-file naming conventions across the supported languages. */\nconst TEST_FILE_PATTERNS: RegExp[] = [\n /(^|\\/)tests?\\//, // tests/ or test/ directory\n /(^|\\/)__tests__\\//,\n /(^|\\/)spec\\//,\n /\\.test\\.[cm]?[jt]sx?$/,\n /\\.spec\\.[cm]?[jt]sx?$/,\n /(^|\\/)test_[^/]+\\.py$/,\n /_test\\.py$/,\n /_test\\.go$/,\n /Tests?\\.(java|cs)$/,\n /_spec\\.rb$/,\n /_test\\.rb$/,\n];\n\nexport function isTestFile(path: string): boolean {\n return TEST_FILE_PATTERNS.some((p) => p.test(path));\n}\n\nexport interface TestSelection {\n /** What the selection was computed for (resolved node id, or the changed files). */\n target: string;\n /** Unique test files, most directly affected first. */\n testFiles: Array<{ file: string; depth: number; via: string }>;\n /** How many non-test nodes were in the blast radius (context for confidence). */\n affectedNodeCount: number;\n /**\n * `usage` — selected by following actual use (calls/inheritance), tight;\n * `import-reachability` — the broad fallback: anything that can reach the\n * target through imports/re-exports, which over-includes files that share\n * a barrel entry with the target.\n */\n precision: 'usage' | 'import-reachability';\n}\n\nconst TEST_REACH_DEPTH = 4;\nconst TEST_REACH_LIMIT = 2000;\n\n/** Relations that mean \"actually uses it\", as opposed to \"can merely reach it through an import\". */\nconst USAGE_RELATIONS: Relation[] = ['calls', 'inherits', 'implements', 'mixes_in', 'embeds', 'references'];\n\nfunction selectionFromImpact(\n graph: Graph,\n targetIds: string[],\n targetLabel: string,\n relations?: Relation[],\n): TestSelection {\n const best = new Map<string, { depth: number; via: string }>();\n let affectedNodeCount = 0;\n\n for (const id of targetIds) {\n const impact = affectedBy(graph, id, { maxDepth: TEST_REACH_DEPTH, limit: TEST_REACH_LIMIT, relations });\n if (!impact) continue;\n affectedNodeCount += impact.affected.length;\n for (const node of impact.affected) {\n if (!isTestFile(node.sourceFile)) continue;\n const existing = best.get(node.sourceFile);\n if (!existing || node.depth < existing.depth) {\n best.set(node.sourceFile, { depth: node.depth, via: node.via.dependsOn });\n }\n }\n }\n\n const testFiles = [...best.entries()]\n .map(([file, info]) => ({ file, depth: info.depth, via: info.via }))\n .sort((a, b) => a.depth - b.depth || a.file.localeCompare(b.file));\n\n return {\n target: targetLabel,\n testFiles,\n affectedNodeCount,\n precision: relations ? 'usage' : 'import-reachability',\n };\n}\n\n/** A file node's entities (so a usage-relation traversal has call targets to start from). */\nfunction entityRootsOf(graph: Graph, id: string): string[] {\n if (id.includes('::')) return [id];\n const roots: string[] = [];\n graph.forEachOutEdge(id, (_edge, attrs, _source, target) => {\n if (String(attrs.relation) === 'contains') roots.push(target);\n });\n roots.sort((a, b) => a.localeCompare(b));\n return roots.length > 0 ? roots : [id];\n}\n\n/**\n * Test files reachable (reverse) from the node best matching `nodeQuery`.\n * Tries a tight usage-relation pass first (tests that actually CALL the\n * target — precise even when many files share a barrel entry point); falls\n * back to full import-reachability when usage finds nothing. Null if\n * nothing matches the query.\n */\nexport function testsForNode(graph: Graph, nodeQuery: string): TestSelection | null {\n const match = resolveNode(graph, nodeQuery);\n if (!match) return null;\n\n const precise = selectionFromImpact(graph, entityRootsOf(graph, match.id), match.id, USAGE_RELATIONS);\n if (precise.testFiles.length > 0) return precise;\n\n return selectionFromImpact(graph, [match.id], match.id);\n}\n\n/**\n * Test files reachable from any of the given changed files (as reported by\n * `git diff --name-only`). Changed test files select themselves. Unknown\n * files (not in the graph) are skipped.\n */\nexport function testsForChangedFiles(graph: Graph, changedFiles: string[]): TestSelection {\n const fileIds: string[] = [];\n const selfSelected = new Map<string, { depth: number; via: string }>();\n\n for (const file of changedFiles) {\n if (isTestFile(file)) {\n selfSelected.set(file, { depth: 0, via: '(changed directly)' });\n continue;\n }\n if (graph.hasNode(file)) fileIds.push(file);\n }\n\n const label = changedFiles.join(', ');\n const preciseRoots = fileIds.flatMap((f) => entityRootsOf(graph, f));\n let selection = selectionFromImpact(graph, preciseRoots, label, USAGE_RELATIONS);\n if (selection.testFiles.length === 0) {\n selection = selectionFromImpact(graph, fileIds, label);\n }\n for (const [file, info] of selfSelected) {\n if (!selection.testFiles.some((t) => t.file === file)) {\n selection.testFiles.push({ file, depth: info.depth, via: info.via });\n }\n }\n selection.testFiles.sort((a, b) => a.depth - b.depth || a.file.localeCompare(b.file));\n return selection;\n}\n"],"mappings":";;;;;AAAA,OAAO,WAAW;AASX,SAAS,eAAe,OAA+B;AAC5D,SAAO,MAAM,OAAO;AACtB;AAEO,SAAS,iBAAiB,MAA8B;AAC7D,SAAO,MAAM,KAAK,IAAI;AACxB;;;ACfA,YAAY,QAAQ;AAcb,IAAM,sBAAN,MAAgD;AAAA,EACrD,MAAM,KAAK,KAAoC;AAC7C,QAAI;AACJ,QAAI;AACF,iBAAW,kBAAkB,cAAc,GAAG;AAAA,IAChD,SAAS,OAAO;AAGd,UAAK,MAAgB,QAAQ,WAAW,+BAA+B,EAAG,QAAO;AACjF,YAAM;AAAA,IACR;AAEA,QAAI;AACJ,QAAI;AACF,YAAM,MAAS,YAAS,UAAU,OAAO;AAAA,IAC3C,SAAS,OAAO;AACd,UAAK,MAAgC,SAAS,SAAU,QAAO;AAC/D,YAAM;AAAA,IACR;AACA,WAAO,iBAAiB,KAAK,MAAM,GAAG,CAAoB;AAAA,EAC5D;AAAA,EAEA,MAAM,KAAK,KAAa,OAA6B;AACnD,UAAS,SAAM,KAAK,EAAE,WAAW,KAAK,CAAC;AACvC,UAAM,WAAW,kBAAkB,cAAc,GAAG;AACpD,UAAS,aAAU,UAAU,KAAK,UAAU,eAAe,KAAK,GAAG,MAAM,CAAC,GAAG,OAAO;AAAA,EACtF;AACF;;;ACzCA,YAAY,UAAU;AAWtB,eAAsB,UAAU,SAAsB,UAAK,QAAQ,IAAI,GAAG,cAAc,GAAmB;AACzG,QAAM,QAAQ,MAAM,IAAI,oBAAoB,EAAE,KAAK,MAAM;AACzD,MAAI,UAAU,MAAM;AACl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EDS","readFile","path"]}
|
|
@@ -2,7 +2,7 @@ import {
|
|
|
2
2
|
LocalFileGraphStore,
|
|
3
3
|
affectedBy,
|
|
4
4
|
testsForNode
|
|
5
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-7LTO76UD.js";
|
|
6
6
|
import {
|
|
7
7
|
escapeHtml,
|
|
8
8
|
sanitizeLabel,
|
|
@@ -1955,13 +1955,15 @@ var RelationSchema = z.enum([
|
|
|
1955
1955
|
"references",
|
|
1956
1956
|
"contains",
|
|
1957
1957
|
"method",
|
|
1958
|
-
"re_exports"
|
|
1958
|
+
"re_exports",
|
|
1959
|
+
"follows"
|
|
1959
1960
|
]);
|
|
1960
1961
|
var GraphNodeSchema = z.object({
|
|
1961
1962
|
id: z.string().min(1, "node id must be non-empty"),
|
|
1962
1963
|
label: z.string(),
|
|
1963
1964
|
sourceFile: z.string(),
|
|
1964
|
-
sourceLocation: z.string()
|
|
1965
|
+
sourceLocation: z.string(),
|
|
1966
|
+
kind: z.string().optional()
|
|
1965
1967
|
});
|
|
1966
1968
|
var GraphEdgeSchema = z.object({
|
|
1967
1969
|
source: z.string().min(1, "edge source must be non-empty"),
|
|
@@ -2010,16 +2012,27 @@ function buildGraph(extractions) {
|
|
|
2010
2012
|
const validated = extractions.map(
|
|
2011
2013
|
(extraction, index) => validateExtraction(extraction, `extraction #${index}`)
|
|
2012
2014
|
);
|
|
2015
|
+
mergeExtractionsInto(graph, validated);
|
|
2016
|
+
return graph;
|
|
2017
|
+
}
|
|
2018
|
+
function mergeExtractionsInto(graph, validated) {
|
|
2013
2019
|
const allNodes = validated.flatMap((extraction) => extraction.nodes);
|
|
2014
2020
|
const allEdges = validated.flatMap((extraction) => extraction.edges);
|
|
2015
2021
|
const sortedNodes = [...allNodes].sort((a, b) => a.id.localeCompare(b.id));
|
|
2016
2022
|
for (const node of sortedNodes) {
|
|
2017
|
-
|
|
2018
|
-
graph.addNode(node.id, {
|
|
2023
|
+
const attributes = {
|
|
2019
2024
|
label: node.label,
|
|
2020
2025
|
sourceFile: node.sourceFile,
|
|
2021
|
-
sourceLocation: node.sourceLocation
|
|
2022
|
-
|
|
2026
|
+
sourceLocation: node.sourceLocation,
|
|
2027
|
+
...node.kind !== void 0 ? { kind: node.kind } : {}
|
|
2028
|
+
};
|
|
2029
|
+
if (graph.hasNode(node.id)) {
|
|
2030
|
+
if (graph.getNodeAttribute(node.id, "sourceFile") === "<unknown>") {
|
|
2031
|
+
graph.mergeNodeAttributes(node.id, attributes);
|
|
2032
|
+
}
|
|
2033
|
+
continue;
|
|
2034
|
+
}
|
|
2035
|
+
graph.addNode(node.id, attributes);
|
|
2023
2036
|
}
|
|
2024
2037
|
const sortedEdges = [...allEdges].sort(
|
|
2025
2038
|
(a, b) => a.source.localeCompare(b.source) || a.target.localeCompare(b.target) || a.relation.localeCompare(b.relation)
|
|
@@ -2038,6 +2051,98 @@ function buildGraph(extractions) {
|
|
|
2038
2051
|
confidence: edge.confidence
|
|
2039
2052
|
});
|
|
2040
2053
|
}
|
|
2054
|
+
}
|
|
2055
|
+
|
|
2056
|
+
// src/cluster.ts
|
|
2057
|
+
import { createHash } from "crypto";
|
|
2058
|
+
import Graph2 from "graphology";
|
|
2059
|
+
import leiden from "@aflsolutions/graphology-communities-leiden";
|
|
2060
|
+
import louvain from "graphology-communities-louvain";
|
|
2061
|
+
var FIXED_SEED = 1592594996;
|
|
2062
|
+
function mulberry32(seed) {
|
|
2063
|
+
let a = seed >>> 0;
|
|
2064
|
+
return function next() {
|
|
2065
|
+
a = a + 1831565813 | 0;
|
|
2066
|
+
let t = Math.imul(a ^ a >>> 15, 1 | a);
|
|
2067
|
+
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
|
|
2068
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
2069
|
+
};
|
|
2070
|
+
}
|
|
2071
|
+
function sortedWorkingCopy(graph) {
|
|
2072
|
+
const copy = new Graph2({ type: graph.type, multi: graph.multi, allowSelfLoops: graph.allowSelfLoops });
|
|
2073
|
+
const nodeIds = graph.nodes().sort((a, b) => a.localeCompare(b));
|
|
2074
|
+
for (const id of nodeIds) {
|
|
2075
|
+
copy.addNode(id, { ...graph.getNodeAttributes(id) });
|
|
2076
|
+
}
|
|
2077
|
+
const edgeEntries = [];
|
|
2078
|
+
graph.forEachEdge((edge, attributes, source, target) => {
|
|
2079
|
+
edgeEntries.push({ key: edge, source, target, attributes });
|
|
2080
|
+
});
|
|
2081
|
+
edgeEntries.sort(
|
|
2082
|
+
(a, b) => a.source.localeCompare(b.source) || a.target.localeCompare(b.target) || String(a.attributes.relation).localeCompare(String(b.attributes.relation))
|
|
2083
|
+
);
|
|
2084
|
+
for (const entry of edgeEntries) {
|
|
2085
|
+
copy.addEdgeWithKey(entry.key, entry.source, entry.target, { ...entry.attributes });
|
|
2086
|
+
}
|
|
2087
|
+
return copy;
|
|
2088
|
+
}
|
|
2089
|
+
function toUndirectedCopy(graph) {
|
|
2090
|
+
const copy = new Graph2({ type: "undirected", multi: true, allowSelfLoops: graph.allowSelfLoops });
|
|
2091
|
+
graph.forEachNode((nodeId, attributes) => copy.addNode(nodeId, { ...attributes }));
|
|
2092
|
+
graph.forEachEdge((edgeKey, attributes, source, target) => {
|
|
2093
|
+
copy.addEdgeWithKey(edgeKey, source, target, { ...attributes });
|
|
2094
|
+
});
|
|
2095
|
+
return copy;
|
|
2096
|
+
}
|
|
2097
|
+
function runAlgorithm(working, options) {
|
|
2098
|
+
const rng = mulberry32(FIXED_SEED);
|
|
2099
|
+
if (options.algorithm === "leiden") {
|
|
2100
|
+
const undirected = toUndirectedCopy(working);
|
|
2101
|
+
leiden.assign(undirected, { rng, maxIterations: options.maxIterations ?? 0 });
|
|
2102
|
+
undirected.forEachNode((nodeId, attributes) => {
|
|
2103
|
+
working.setNodeAttribute(nodeId, "community", attributes.community);
|
|
2104
|
+
});
|
|
2105
|
+
return;
|
|
2106
|
+
}
|
|
2107
|
+
louvain.assign(working, { rng });
|
|
2108
|
+
}
|
|
2109
|
+
function cluster(graph, options = {}) {
|
|
2110
|
+
if (graph.order === 0) return graph;
|
|
2111
|
+
const working = sortedWorkingCopy(graph);
|
|
2112
|
+
runAlgorithm(working, options);
|
|
2113
|
+
const hubs = /* @__PURE__ */ new Map();
|
|
2114
|
+
working.forEachNode((nodeId) => {
|
|
2115
|
+
const community = working.getNodeAttribute(nodeId, "community");
|
|
2116
|
+
const degree = working.degree(nodeId);
|
|
2117
|
+
const current = hubs.get(community);
|
|
2118
|
+
if (!current || degree > current.degree) {
|
|
2119
|
+
hubs.set(community, { id: nodeId, degree });
|
|
2120
|
+
}
|
|
2121
|
+
});
|
|
2122
|
+
const membersByCommunity = /* @__PURE__ */ new Map();
|
|
2123
|
+
working.forEachNode((nodeId) => {
|
|
2124
|
+
const community = working.getNodeAttribute(nodeId, "community");
|
|
2125
|
+
const list = membersByCommunity.get(community);
|
|
2126
|
+
if (list) list.push(nodeId);
|
|
2127
|
+
else membersByCommunity.set(community, [nodeId]);
|
|
2128
|
+
});
|
|
2129
|
+
const labelByCommunity = /* @__PURE__ */ new Map();
|
|
2130
|
+
const hashByCommunity = /* @__PURE__ */ new Map();
|
|
2131
|
+
for (const [community, members] of membersByCommunity) {
|
|
2132
|
+
members.sort((a, b) => a.localeCompare(b));
|
|
2133
|
+
hashByCommunity.set(community, createHash("sha256").update(members.join("\n")).digest("hex"));
|
|
2134
|
+
const hub = hubs.get(community);
|
|
2135
|
+
const hubLabel = hub ? working.getNodeAttribute(hub.id, "label") : void 0;
|
|
2136
|
+
labelByCommunity.set(community, hubLabel && hubLabel.length > 0 ? hubLabel : `community-${community}`);
|
|
2137
|
+
}
|
|
2138
|
+
working.forEachNode((nodeId) => {
|
|
2139
|
+
const community = working.getNodeAttribute(nodeId, "community");
|
|
2140
|
+
graph.mergeNodeAttributes(nodeId, {
|
|
2141
|
+
community,
|
|
2142
|
+
communityLabel: labelByCommunity.get(community),
|
|
2143
|
+
communityHash: hashByCommunity.get(community)
|
|
2144
|
+
});
|
|
2145
|
+
});
|
|
2041
2146
|
return graph;
|
|
2042
2147
|
}
|
|
2043
2148
|
|
|
@@ -2235,99 +2340,6 @@ function resolveCrossFileReferences(extractions, options = {}) {
|
|
|
2235
2340
|
return { resolvedEndpoints, droppedPlaceholders };
|
|
2236
2341
|
}
|
|
2237
2342
|
|
|
2238
|
-
// src/cluster.ts
|
|
2239
|
-
import { createHash } from "crypto";
|
|
2240
|
-
import Graph2 from "graphology";
|
|
2241
|
-
import leiden from "@aflsolutions/graphology-communities-leiden";
|
|
2242
|
-
import louvain from "graphology-communities-louvain";
|
|
2243
|
-
var FIXED_SEED = 1592594996;
|
|
2244
|
-
function mulberry32(seed) {
|
|
2245
|
-
let a = seed >>> 0;
|
|
2246
|
-
return function next() {
|
|
2247
|
-
a = a + 1831565813 | 0;
|
|
2248
|
-
let t = Math.imul(a ^ a >>> 15, 1 | a);
|
|
2249
|
-
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
|
|
2250
|
-
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
2251
|
-
};
|
|
2252
|
-
}
|
|
2253
|
-
function sortedWorkingCopy(graph) {
|
|
2254
|
-
const copy = new Graph2({ type: graph.type, multi: graph.multi, allowSelfLoops: graph.allowSelfLoops });
|
|
2255
|
-
const nodeIds = graph.nodes().sort((a, b) => a.localeCompare(b));
|
|
2256
|
-
for (const id of nodeIds) {
|
|
2257
|
-
copy.addNode(id, { ...graph.getNodeAttributes(id) });
|
|
2258
|
-
}
|
|
2259
|
-
const edgeEntries = [];
|
|
2260
|
-
graph.forEachEdge((edge, attributes, source, target) => {
|
|
2261
|
-
edgeEntries.push({ key: edge, source, target, attributes });
|
|
2262
|
-
});
|
|
2263
|
-
edgeEntries.sort(
|
|
2264
|
-
(a, b) => a.source.localeCompare(b.source) || a.target.localeCompare(b.target) || String(a.attributes.relation).localeCompare(String(b.attributes.relation))
|
|
2265
|
-
);
|
|
2266
|
-
for (const entry of edgeEntries) {
|
|
2267
|
-
copy.addEdgeWithKey(entry.key, entry.source, entry.target, { ...entry.attributes });
|
|
2268
|
-
}
|
|
2269
|
-
return copy;
|
|
2270
|
-
}
|
|
2271
|
-
function toUndirectedCopy(graph) {
|
|
2272
|
-
const copy = new Graph2({ type: "undirected", multi: true, allowSelfLoops: graph.allowSelfLoops });
|
|
2273
|
-
graph.forEachNode((nodeId, attributes) => copy.addNode(nodeId, { ...attributes }));
|
|
2274
|
-
graph.forEachEdge((edgeKey, attributes, source, target) => {
|
|
2275
|
-
copy.addEdgeWithKey(edgeKey, source, target, { ...attributes });
|
|
2276
|
-
});
|
|
2277
|
-
return copy;
|
|
2278
|
-
}
|
|
2279
|
-
function runAlgorithm(working, options) {
|
|
2280
|
-
const rng = mulberry32(FIXED_SEED);
|
|
2281
|
-
if (options.algorithm === "leiden") {
|
|
2282
|
-
const undirected = toUndirectedCopy(working);
|
|
2283
|
-
leiden.assign(undirected, { rng, maxIterations: options.maxIterations ?? 0 });
|
|
2284
|
-
undirected.forEachNode((nodeId, attributes) => {
|
|
2285
|
-
working.setNodeAttribute(nodeId, "community", attributes.community);
|
|
2286
|
-
});
|
|
2287
|
-
return;
|
|
2288
|
-
}
|
|
2289
|
-
louvain.assign(working, { rng });
|
|
2290
|
-
}
|
|
2291
|
-
function cluster(graph, options = {}) {
|
|
2292
|
-
if (graph.order === 0) return graph;
|
|
2293
|
-
const working = sortedWorkingCopy(graph);
|
|
2294
|
-
runAlgorithm(working, options);
|
|
2295
|
-
const hubs = /* @__PURE__ */ new Map();
|
|
2296
|
-
working.forEachNode((nodeId) => {
|
|
2297
|
-
const community = working.getNodeAttribute(nodeId, "community");
|
|
2298
|
-
const degree = working.degree(nodeId);
|
|
2299
|
-
const current = hubs.get(community);
|
|
2300
|
-
if (!current || degree > current.degree) {
|
|
2301
|
-
hubs.set(community, { id: nodeId, degree });
|
|
2302
|
-
}
|
|
2303
|
-
});
|
|
2304
|
-
const membersByCommunity = /* @__PURE__ */ new Map();
|
|
2305
|
-
working.forEachNode((nodeId) => {
|
|
2306
|
-
const community = working.getNodeAttribute(nodeId, "community");
|
|
2307
|
-
const list = membersByCommunity.get(community);
|
|
2308
|
-
if (list) list.push(nodeId);
|
|
2309
|
-
else membersByCommunity.set(community, [nodeId]);
|
|
2310
|
-
});
|
|
2311
|
-
const labelByCommunity = /* @__PURE__ */ new Map();
|
|
2312
|
-
const hashByCommunity = /* @__PURE__ */ new Map();
|
|
2313
|
-
for (const [community, members] of membersByCommunity) {
|
|
2314
|
-
members.sort((a, b) => a.localeCompare(b));
|
|
2315
|
-
hashByCommunity.set(community, createHash("sha256").update(members.join("\n")).digest("hex"));
|
|
2316
|
-
const hub = hubs.get(community);
|
|
2317
|
-
const hubLabel = hub ? working.getNodeAttribute(hub.id, "label") : void 0;
|
|
2318
|
-
labelByCommunity.set(community, hubLabel && hubLabel.length > 0 ? hubLabel : `community-${community}`);
|
|
2319
|
-
}
|
|
2320
|
-
working.forEachNode((nodeId) => {
|
|
2321
|
-
const community = working.getNodeAttribute(nodeId, "community");
|
|
2322
|
-
graph.mergeNodeAttributes(nodeId, {
|
|
2323
|
-
community,
|
|
2324
|
-
communityLabel: labelByCommunity.get(community),
|
|
2325
|
-
communityHash: hashByCommunity.get(community)
|
|
2326
|
-
});
|
|
2327
|
-
});
|
|
2328
|
-
return graph;
|
|
2329
|
-
}
|
|
2330
|
-
|
|
2331
2343
|
// src/analyze.ts
|
|
2332
2344
|
var ABSOLUTE_DEGREE_FLOOR = 10;
|
|
2333
2345
|
var MEAN_DEGREE_MULTIPLIER = 3;
|
|
@@ -3106,8 +3118,9 @@ export {
|
|
|
3106
3118
|
ExtractionValidationError,
|
|
3107
3119
|
validateExtraction,
|
|
3108
3120
|
buildGraph,
|
|
3109
|
-
|
|
3121
|
+
mergeExtractionsInto,
|
|
3110
3122
|
cluster,
|
|
3123
|
+
resolveCrossFileReferences,
|
|
3111
3124
|
analyze,
|
|
3112
3125
|
renderReport,
|
|
3113
3126
|
exportGraph,
|
|
@@ -3125,4 +3138,4 @@ export {
|
|
|
3125
3138
|
validateRules,
|
|
3126
3139
|
checkRules
|
|
3127
3140
|
};
|
|
3128
|
-
//# sourceMappingURL=chunk-
|
|
3141
|
+
//# sourceMappingURL=chunk-EHMBINRV.js.map
|