@kodus/kodus-graph 0.2.8 → 0.2.9
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/LICENSE +21 -0
- package/README.md +252 -0
- package/dist/analysis/blast-radius.d.ts +2 -0
- package/dist/analysis/blast-radius.js +57 -0
- package/dist/analysis/communities.d.ts +28 -0
- package/dist/analysis/communities.js +100 -0
- package/dist/analysis/context-builder.d.ts +34 -0
- package/dist/analysis/context-builder.js +83 -0
- package/dist/analysis/diff.d.ts +35 -0
- package/dist/analysis/diff.js +140 -0
- package/dist/analysis/enrich.d.ts +5 -0
- package/dist/analysis/enrich.js +98 -0
- package/dist/analysis/flows.d.ts +27 -0
- package/dist/analysis/flows.js +86 -0
- package/dist/analysis/inheritance.d.ts +3 -0
- package/dist/analysis/inheritance.js +31 -0
- package/dist/analysis/prompt-formatter.d.ts +2 -0
- package/dist/analysis/prompt-formatter.js +166 -0
- package/dist/analysis/risk-score.d.ts +4 -0
- package/dist/analysis/risk-score.js +51 -0
- package/dist/analysis/search.d.ts +11 -0
- package/dist/analysis/search.js +64 -0
- package/dist/analysis/test-gaps.d.ts +2 -0
- package/dist/analysis/test-gaps.js +14 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +208 -0
- package/dist/commands/analyze.d.ts +9 -0
- package/dist/commands/analyze.js +114 -0
- package/dist/commands/communities.d.ts +8 -0
- package/dist/commands/communities.js +9 -0
- package/dist/commands/context.d.ts +12 -0
- package/dist/commands/context.js +130 -0
- package/dist/commands/diff.d.ts +9 -0
- package/dist/commands/diff.js +89 -0
- package/dist/commands/flows.d.ts +8 -0
- package/dist/commands/flows.js +9 -0
- package/dist/commands/parse.d.ts +10 -0
- package/dist/commands/parse.js +101 -0
- package/dist/commands/search.d.ts +12 -0
- package/dist/commands/search.js +27 -0
- package/dist/commands/update.d.ts +7 -0
- package/dist/commands/update.js +154 -0
- package/dist/graph/builder.d.ts +2 -0
- package/dist/graph/builder.js +216 -0
- package/dist/graph/edges.d.ts +19 -0
- package/dist/graph/edges.js +105 -0
- package/dist/graph/json-writer.d.ts +9 -0
- package/dist/graph/json-writer.js +38 -0
- package/dist/graph/loader.d.ts +13 -0
- package/dist/graph/loader.js +101 -0
- package/dist/graph/merger.d.ts +7 -0
- package/dist/graph/merger.js +18 -0
- package/dist/graph/types.d.ts +249 -0
- package/dist/graph/types.js +1 -0
- package/dist/parser/batch.d.ts +4 -0
- package/dist/parser/batch.js +78 -0
- package/dist/parser/discovery.d.ts +7 -0
- package/dist/parser/discovery.js +61 -0
- package/dist/parser/extractor.d.ts +4 -0
- package/dist/parser/extractor.js +33 -0
- package/dist/parser/extractors/generic.d.ts +8 -0
- package/dist/parser/extractors/generic.js +471 -0
- package/dist/parser/extractors/python.d.ts +8 -0
- package/dist/parser/extractors/python.js +133 -0
- package/dist/parser/extractors/ruby.d.ts +8 -0
- package/dist/parser/extractors/ruby.js +153 -0
- package/dist/parser/extractors/typescript.d.ts +10 -0
- package/dist/parser/extractors/typescript.js +365 -0
- package/dist/parser/languages.d.ts +32 -0
- package/dist/parser/languages.js +303 -0
- package/dist/resolver/call-resolver.d.ts +36 -0
- package/dist/resolver/call-resolver.js +178 -0
- package/dist/resolver/import-map.d.ts +12 -0
- package/dist/resolver/import-map.js +21 -0
- package/dist/resolver/import-resolver.d.ts +19 -0
- package/dist/resolver/import-resolver.js +212 -0
- package/dist/resolver/languages/csharp.d.ts +1 -0
- package/dist/resolver/languages/csharp.js +31 -0
- package/dist/resolver/languages/go.d.ts +3 -0
- package/dist/resolver/languages/go.js +196 -0
- package/dist/resolver/languages/java.d.ts +1 -0
- package/dist/resolver/languages/java.js +108 -0
- package/dist/resolver/languages/php.d.ts +3 -0
- package/dist/resolver/languages/php.js +54 -0
- package/dist/resolver/languages/python.d.ts +11 -0
- package/dist/resolver/languages/python.js +51 -0
- package/dist/resolver/languages/ruby.d.ts +9 -0
- package/dist/resolver/languages/ruby.js +59 -0
- package/dist/resolver/languages/rust.d.ts +1 -0
- package/dist/resolver/languages/rust.js +196 -0
- package/dist/resolver/languages/typescript.d.ts +27 -0
- package/dist/resolver/languages/typescript.js +240 -0
- package/dist/resolver/re-export-resolver.d.ts +24 -0
- package/dist/resolver/re-export-resolver.js +57 -0
- package/dist/resolver/symbol-table.d.ts +17 -0
- package/dist/resolver/symbol-table.js +60 -0
- package/dist/shared/extract-calls.d.ts +26 -0
- package/dist/shared/extract-calls.js +57 -0
- package/dist/shared/file-hash.d.ts +3 -0
- package/dist/shared/file-hash.js +10 -0
- package/dist/shared/filters.d.ts +3 -0
- package/dist/shared/filters.js +240 -0
- package/dist/shared/logger.d.ts +6 -0
- package/dist/shared/logger.js +17 -0
- package/dist/shared/qualified-name.d.ts +1 -0
- package/dist/shared/qualified-name.js +9 -0
- package/dist/shared/safe-path.d.ts +6 -0
- package/dist/shared/safe-path.js +29 -0
- package/dist/shared/schemas.d.ts +43 -0
- package/dist/shared/schemas.js +30 -0
- package/dist/shared/temp.d.ts +11 -0
- package/{src/shared/temp.ts → dist/shared/temp.js} +4 -5
- package/package.json +20 -6
- package/src/analysis/blast-radius.ts +0 -54
- package/src/analysis/communities.ts +0 -135
- package/src/analysis/context-builder.ts +0 -130
- package/src/analysis/diff.ts +0 -169
- package/src/analysis/enrich.ts +0 -110
- package/src/analysis/flows.ts +0 -112
- package/src/analysis/inheritance.ts +0 -34
- package/src/analysis/prompt-formatter.ts +0 -175
- package/src/analysis/risk-score.ts +0 -62
- package/src/analysis/search.ts +0 -76
- package/src/analysis/test-gaps.ts +0 -21
- package/src/cli.ts +0 -210
- package/src/commands/analyze.ts +0 -128
- package/src/commands/communities.ts +0 -19
- package/src/commands/context.ts +0 -182
- package/src/commands/diff.ts +0 -96
- package/src/commands/flows.ts +0 -19
- package/src/commands/parse.ts +0 -124
- package/src/commands/search.ts +0 -41
- package/src/commands/update.ts +0 -166
- package/src/graph/builder.ts +0 -209
- package/src/graph/edges.ts +0 -101
- package/src/graph/json-writer.ts +0 -43
- package/src/graph/loader.ts +0 -113
- package/src/graph/merger.ts +0 -25
- package/src/graph/types.ts +0 -283
- package/src/parser/batch.ts +0 -82
- package/src/parser/discovery.ts +0 -75
- package/src/parser/extractor.ts +0 -37
- package/src/parser/extractors/generic.ts +0 -132
- package/src/parser/extractors/python.ts +0 -133
- package/src/parser/extractors/ruby.ts +0 -147
- package/src/parser/extractors/typescript.ts +0 -350
- package/src/parser/languages.ts +0 -122
- package/src/resolver/call-resolver.ts +0 -244
- package/src/resolver/import-map.ts +0 -27
- package/src/resolver/import-resolver.ts +0 -72
- package/src/resolver/languages/csharp.ts +0 -7
- package/src/resolver/languages/go.ts +0 -7
- package/src/resolver/languages/java.ts +0 -7
- package/src/resolver/languages/php.ts +0 -7
- package/src/resolver/languages/python.ts +0 -35
- package/src/resolver/languages/ruby.ts +0 -21
- package/src/resolver/languages/rust.ts +0 -7
- package/src/resolver/languages/typescript.ts +0 -168
- package/src/resolver/re-export-resolver.ts +0 -66
- package/src/resolver/symbol-table.ts +0 -67
- package/src/shared/extract-calls.ts +0 -75
- package/src/shared/file-hash.ts +0 -12
- package/src/shared/filters.ts +0 -243
- package/src/shared/logger.ts +0 -17
- package/src/shared/qualified-name.ts +0 -5
- package/src/shared/safe-path.ts +0 -31
- package/src/shared/schemas.ts +0 -32
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { basename, extname } from 'path';
|
|
2
|
+
/**
|
|
3
|
+
* Extract the "stem" from a test file name by stripping test-related
|
|
4
|
+
* prefixes/suffixes. Returns null if no test pattern was found.
|
|
5
|
+
*/
|
|
6
|
+
export function extractTestStem(testFile) {
|
|
7
|
+
const base = basename(testFile, extname(testFile));
|
|
8
|
+
const cleaned = base
|
|
9
|
+
.replace(/_spec$/, '') // user_spec → user (Ruby/RSpec)
|
|
10
|
+
.replace(/_test$/, '') // user_test → user (Python/Go)
|
|
11
|
+
.replace(/^test_/, '') // test_user → user (Python)
|
|
12
|
+
.replace(/\.test$/, '') // user.test → user (JS/TS)
|
|
13
|
+
.replace(/\.spec$/, '') // user.spec → user (JS/TS)
|
|
14
|
+
.replace(/-test$/, '') // user-test → user
|
|
15
|
+
.replace(/-spec$/, '') // user-spec → user
|
|
16
|
+
.replace(/^spec_/, '') // spec_user → user
|
|
17
|
+
.replace(/Test$/, '') // UserTest → User (Java)
|
|
18
|
+
.replace(/Spec$/, ''); // UserSpec → User (Scala)
|
|
19
|
+
if (!cleaned || cleaned === base) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
return cleaned;
|
|
23
|
+
}
|
|
24
|
+
export function deriveEdges(graph, importEdges) {
|
|
25
|
+
// INHERITS: class extends another class
|
|
26
|
+
const inherits = graph.classes
|
|
27
|
+
.filter((c) => c.extends)
|
|
28
|
+
.map((c) => ({ source: c.qualified, target: c.extends, file: c.file }));
|
|
29
|
+
// IMPLEMENTS: class implements interface(s)
|
|
30
|
+
const implements_ = [];
|
|
31
|
+
for (const c of graph.classes) {
|
|
32
|
+
for (const iface of c.implements) {
|
|
33
|
+
implements_.push({ source: c.qualified, target: iface, file: c.file });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
// TESTED_BY: two heuristics, deduplicated
|
|
37
|
+
const testFiles = new Set(graph.tests.map((t) => t.file));
|
|
38
|
+
const testedBySet = new Set();
|
|
39
|
+
const testedBy = [];
|
|
40
|
+
const addTestedBy = (source, target) => {
|
|
41
|
+
const key = `${source}|${target}`;
|
|
42
|
+
if (testedBySet.has(key)) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
testedBySet.add(key);
|
|
46
|
+
testedBy.push({ source, target });
|
|
47
|
+
};
|
|
48
|
+
// Heuristic 1: Resolved imports from test files (high signal)
|
|
49
|
+
for (const e of importEdges) {
|
|
50
|
+
if (testFiles.has(e.source) && e.resolved) {
|
|
51
|
+
addTestedBy(e.target, e.source);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
// Heuristic 2: File-name matching (catches Ruby, Python, and any
|
|
55
|
+
// language where imports don't resolve)
|
|
56
|
+
const allSourceFiles = new Set();
|
|
57
|
+
for (const f of graph.functions) {
|
|
58
|
+
allSourceFiles.add(f.file);
|
|
59
|
+
}
|
|
60
|
+
for (const c of graph.classes) {
|
|
61
|
+
allSourceFiles.add(c.file);
|
|
62
|
+
}
|
|
63
|
+
for (const i of graph.interfaces) {
|
|
64
|
+
allSourceFiles.add(i.file);
|
|
65
|
+
}
|
|
66
|
+
for (const e of graph.enums) {
|
|
67
|
+
allSourceFiles.add(e.file);
|
|
68
|
+
}
|
|
69
|
+
for (const tf of testFiles) {
|
|
70
|
+
allSourceFiles.delete(tf);
|
|
71
|
+
}
|
|
72
|
+
const sourceByBase = new Map();
|
|
73
|
+
for (const file of allSourceFiles) {
|
|
74
|
+
const base = basename(file, extname(file));
|
|
75
|
+
const list = sourceByBase.get(base);
|
|
76
|
+
if (list) {
|
|
77
|
+
list.push(file);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
sourceByBase.set(base, [file]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
for (const testFile of testFiles) {
|
|
84
|
+
const stem = extractTestStem(testFile);
|
|
85
|
+
if (!stem) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const matches = sourceByBase.get(stem);
|
|
89
|
+
if (!matches) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
for (const sourceFile of matches) {
|
|
93
|
+
addTestedBy(sourceFile, testFile);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// CONTAINS: file contains function/class
|
|
97
|
+
const contains = [];
|
|
98
|
+
for (const f of graph.functions) {
|
|
99
|
+
contains.push({ source: f.file, target: f.qualified });
|
|
100
|
+
}
|
|
101
|
+
for (const c of graph.classes) {
|
|
102
|
+
contains.push({ source: c.file, target: c.qualified });
|
|
103
|
+
}
|
|
104
|
+
return { inherits, implements: implements_, testedBy, contains };
|
|
105
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { GraphEdge, GraphNode, ParseMetadata } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Write graph output as JSON to disk using incremental serialization.
|
|
4
|
+
*
|
|
5
|
+
* Instead of JSON.stringify on the full output (which creates a ~100-300 MB
|
|
6
|
+
* string for large repos), this writes each node/edge individually.
|
|
7
|
+
* Peak memory: only one JSON.stringify(singleNode) string at a time (~1 KB).
|
|
8
|
+
*/
|
|
9
|
+
export declare function writeGraphJSON(out: string, metadata: ParseMetadata, nodes: GraphNode[], edges: GraphEdge[]): void;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { closeSync, openSync, writeSync } from 'fs';
|
|
2
|
+
/**
|
|
3
|
+
* Write graph output as JSON to disk using incremental serialization.
|
|
4
|
+
*
|
|
5
|
+
* Instead of JSON.stringify on the full output (which creates a ~100-300 MB
|
|
6
|
+
* string for large repos), this writes each node/edge individually.
|
|
7
|
+
* Peak memory: only one JSON.stringify(singleNode) string at a time (~1 KB).
|
|
8
|
+
*/
|
|
9
|
+
export function writeGraphJSON(out, metadata, nodes, edges) {
|
|
10
|
+
const fd = openSync(out, 'w');
|
|
11
|
+
try {
|
|
12
|
+
writeSync(fd, '{"metadata":');
|
|
13
|
+
writeSync(fd, JSON.stringify(metadata));
|
|
14
|
+
// Nodes
|
|
15
|
+
writeSync(fd, ',"nodes":[');
|
|
16
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
17
|
+
if (i > 0) {
|
|
18
|
+
writeSync(fd, ',');
|
|
19
|
+
}
|
|
20
|
+
writeSync(fd, '\n');
|
|
21
|
+
writeSync(fd, JSON.stringify(nodes[i]));
|
|
22
|
+
}
|
|
23
|
+
writeSync(fd, '\n]');
|
|
24
|
+
// Edges
|
|
25
|
+
writeSync(fd, ',"edges":[');
|
|
26
|
+
for (let i = 0; i < edges.length; i++) {
|
|
27
|
+
if (i > 0) {
|
|
28
|
+
writeSync(fd, ',');
|
|
29
|
+
}
|
|
30
|
+
writeSync(fd, '\n');
|
|
31
|
+
writeSync(fd, JSON.stringify(edges[i]));
|
|
32
|
+
}
|
|
33
|
+
writeSync(fd, '\n]}');
|
|
34
|
+
}
|
|
35
|
+
finally {
|
|
36
|
+
closeSync(fd);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { GraphData, GraphEdge, GraphNode, ParseMetadata } from './types';
|
|
2
|
+
export interface IndexedGraph {
|
|
3
|
+
nodes: GraphNode[];
|
|
4
|
+
edges: GraphEdge[];
|
|
5
|
+
byQualified: Map<string, GraphNode>;
|
|
6
|
+
byFile: Map<string, GraphNode[]>;
|
|
7
|
+
adjacency: Map<string, GraphEdge[]>;
|
|
8
|
+
reverseAdjacency: Map<string, GraphEdge[]>;
|
|
9
|
+
edgesByKind: Map<string, GraphEdge[]>;
|
|
10
|
+
metadata: ParseMetadata;
|
|
11
|
+
}
|
|
12
|
+
export declare function indexGraph(data: GraphData, metadata?: ParseMetadata): IndexedGraph;
|
|
13
|
+
export declare function loadGraph(path: string): IndexedGraph;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// src/graph/loader.ts
|
|
2
|
+
import { readFileSync } from 'fs';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
const ParseOutputSchema = z.object({
|
|
5
|
+
metadata: z.object({
|
|
6
|
+
repo_dir: z.string(),
|
|
7
|
+
files_parsed: z.number(),
|
|
8
|
+
total_nodes: z.number(),
|
|
9
|
+
total_edges: z.number(),
|
|
10
|
+
duration_ms: z.number(),
|
|
11
|
+
parse_errors: z.number(),
|
|
12
|
+
extract_errors: z.number(),
|
|
13
|
+
files_unchanged: z.number().optional(),
|
|
14
|
+
incremental: z.boolean().optional(),
|
|
15
|
+
}),
|
|
16
|
+
nodes: z.array(z.object({
|
|
17
|
+
kind: z.enum(['Function', 'Method', 'Constructor', 'Class', 'Interface', 'Enum', 'Test']),
|
|
18
|
+
name: z.string(),
|
|
19
|
+
qualified_name: z.string(),
|
|
20
|
+
file_path: z.string(),
|
|
21
|
+
line_start: z.number(),
|
|
22
|
+
line_end: z.number(),
|
|
23
|
+
language: z.string(),
|
|
24
|
+
is_test: z.boolean(),
|
|
25
|
+
file_hash: z.string().optional(),
|
|
26
|
+
parent_name: z.string().optional(),
|
|
27
|
+
params: z.string().optional(),
|
|
28
|
+
return_type: z.string().optional(),
|
|
29
|
+
modifiers: z.string().optional(),
|
|
30
|
+
})),
|
|
31
|
+
edges: z.array(z.object({
|
|
32
|
+
kind: z.enum(['CALLS', 'IMPORTS', 'INHERITS', 'IMPLEMENTS', 'TESTED_BY', 'CONTAINS']),
|
|
33
|
+
source_qualified: z.string(),
|
|
34
|
+
target_qualified: z.string(),
|
|
35
|
+
file_path: z.string(),
|
|
36
|
+
line: z.number(),
|
|
37
|
+
confidence: z.number().optional(),
|
|
38
|
+
})),
|
|
39
|
+
});
|
|
40
|
+
export function indexGraph(data, metadata) {
|
|
41
|
+
const { nodes, edges } = data;
|
|
42
|
+
const meta = metadata ?? {
|
|
43
|
+
repo_dir: '',
|
|
44
|
+
files_parsed: 0,
|
|
45
|
+
total_nodes: nodes.length,
|
|
46
|
+
total_edges: edges.length,
|
|
47
|
+
duration_ms: 0,
|
|
48
|
+
parse_errors: 0,
|
|
49
|
+
extract_errors: 0,
|
|
50
|
+
};
|
|
51
|
+
const byQualified = new Map();
|
|
52
|
+
const byFile = new Map();
|
|
53
|
+
const adjacency = new Map();
|
|
54
|
+
const reverseAdjacency = new Map();
|
|
55
|
+
const edgesByKind = new Map();
|
|
56
|
+
for (const node of nodes) {
|
|
57
|
+
byQualified.set(node.qualified_name, node);
|
|
58
|
+
const list = byFile.get(node.file_path);
|
|
59
|
+
if (list) {
|
|
60
|
+
list.push(node);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
byFile.set(node.file_path, [node]);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
for (const edge of edges) {
|
|
67
|
+
const fwd = adjacency.get(edge.source_qualified);
|
|
68
|
+
if (fwd) {
|
|
69
|
+
fwd.push(edge);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
adjacency.set(edge.source_qualified, [edge]);
|
|
73
|
+
}
|
|
74
|
+
const rev = reverseAdjacency.get(edge.target_qualified);
|
|
75
|
+
if (rev) {
|
|
76
|
+
rev.push(edge);
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
reverseAdjacency.set(edge.target_qualified, [edge]);
|
|
80
|
+
}
|
|
81
|
+
const byKind = edgesByKind.get(edge.kind);
|
|
82
|
+
if (byKind) {
|
|
83
|
+
byKind.push(edge);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
edgesByKind.set(edge.kind, [edge]);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return { nodes, edges, byQualified, byFile, adjacency, reverseAdjacency, edgesByKind, metadata: meta };
|
|
90
|
+
}
|
|
91
|
+
export function loadGraph(path) {
|
|
92
|
+
let raw;
|
|
93
|
+
try {
|
|
94
|
+
raw = JSON.parse(readFileSync(path, 'utf-8'));
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
throw new Error(`Failed to read graph file: ${path} — ${String(err)}`);
|
|
98
|
+
}
|
|
99
|
+
const parsed = ParseOutputSchema.parse(raw);
|
|
100
|
+
return indexGraph({ nodes: parsed.nodes, edges: parsed.edges }, parsed.metadata);
|
|
101
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { GraphData, MainGraphInput } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Merge local parse (PR changed files) with the main graph (from Postgres).
|
|
4
|
+
* Replaces all nodes/edges from changed files with the local parse.
|
|
5
|
+
* Keeps everything else from the main graph intact.
|
|
6
|
+
*/
|
|
7
|
+
export declare function mergeGraphs(mainGraph: MainGraphInput | null, localParse: GraphData, changedFiles: string[]): GraphData;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Merge local parse (PR changed files) with the main graph (from Postgres).
|
|
3
|
+
* Replaces all nodes/edges from changed files with the local parse.
|
|
4
|
+
* Keeps everything else from the main graph intact.
|
|
5
|
+
*/
|
|
6
|
+
export function mergeGraphs(mainGraph, localParse, changedFiles) {
|
|
7
|
+
if (!mainGraph) {
|
|
8
|
+
return localParse;
|
|
9
|
+
}
|
|
10
|
+
const changedSet = new Set(changedFiles);
|
|
11
|
+
// Keep main graph nodes/edges NOT in changed files
|
|
12
|
+
const mainNodes = mainGraph.nodes.filter((n) => !changedSet.has(n.file_path));
|
|
13
|
+
const mainEdges = mainGraph.edges.filter((e) => !changedSet.has(e.file_path));
|
|
14
|
+
return {
|
|
15
|
+
nodes: [...mainNodes, ...localParse.nodes],
|
|
16
|
+
edges: [...mainEdges, ...localParse.edges],
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
export type NodeKind = 'Function' | 'Method' | 'Constructor' | 'Class' | 'Interface' | 'Enum' | 'Test';
|
|
2
|
+
export type EdgeKind = 'CALLS' | 'IMPORTS' | 'INHERITS' | 'IMPLEMENTS' | 'TESTED_BY' | 'CONTAINS';
|
|
3
|
+
export interface GraphNode {
|
|
4
|
+
kind: NodeKind;
|
|
5
|
+
ast_kind?: string;
|
|
6
|
+
name: string;
|
|
7
|
+
qualified_name: string;
|
|
8
|
+
file_path: string;
|
|
9
|
+
line_start: number;
|
|
10
|
+
line_end: number;
|
|
11
|
+
language: string;
|
|
12
|
+
parent_name?: string;
|
|
13
|
+
params?: string;
|
|
14
|
+
return_type?: string;
|
|
15
|
+
modifiers?: string;
|
|
16
|
+
is_test: boolean;
|
|
17
|
+
file_hash?: string;
|
|
18
|
+
content_hash?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface GraphEdge {
|
|
21
|
+
kind: EdgeKind;
|
|
22
|
+
source_qualified: string;
|
|
23
|
+
target_qualified: string;
|
|
24
|
+
file_path: string;
|
|
25
|
+
line: number;
|
|
26
|
+
confidence?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface GraphData {
|
|
29
|
+
nodes: GraphNode[];
|
|
30
|
+
edges: GraphEdge[];
|
|
31
|
+
}
|
|
32
|
+
export interface ParseMetadata {
|
|
33
|
+
repo_dir: string;
|
|
34
|
+
files_parsed: number;
|
|
35
|
+
total_nodes: number;
|
|
36
|
+
total_edges: number;
|
|
37
|
+
duration_ms: number;
|
|
38
|
+
parse_errors: number;
|
|
39
|
+
extract_errors: number;
|
|
40
|
+
files_unchanged?: number;
|
|
41
|
+
incremental?: boolean;
|
|
42
|
+
}
|
|
43
|
+
export interface ParseOutput {
|
|
44
|
+
metadata: ParseMetadata;
|
|
45
|
+
nodes: GraphNode[];
|
|
46
|
+
edges: GraphEdge[];
|
|
47
|
+
}
|
|
48
|
+
export interface BlastRadiusResult {
|
|
49
|
+
total_functions: number;
|
|
50
|
+
total_files: number;
|
|
51
|
+
by_depth: Record<string, string[]>;
|
|
52
|
+
}
|
|
53
|
+
export interface RiskFactor {
|
|
54
|
+
weight: number;
|
|
55
|
+
value: number;
|
|
56
|
+
detail: string;
|
|
57
|
+
}
|
|
58
|
+
export interface RiskScoreResult {
|
|
59
|
+
level: 'LOW' | 'MEDIUM' | 'HIGH';
|
|
60
|
+
score: number;
|
|
61
|
+
factors: {
|
|
62
|
+
blast_radius: RiskFactor;
|
|
63
|
+
test_gaps: RiskFactor;
|
|
64
|
+
complexity: RiskFactor;
|
|
65
|
+
inheritance: RiskFactor;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
export interface TestGap {
|
|
69
|
+
function: string;
|
|
70
|
+
file_path: string;
|
|
71
|
+
line_start: number;
|
|
72
|
+
}
|
|
73
|
+
export interface AnalysisOutput {
|
|
74
|
+
blast_radius: BlastRadiusResult;
|
|
75
|
+
risk_score: RiskScoreResult;
|
|
76
|
+
test_gaps: TestGap[];
|
|
77
|
+
}
|
|
78
|
+
export interface ContextMetadata {
|
|
79
|
+
changed_functions: number;
|
|
80
|
+
caller_count: number;
|
|
81
|
+
callee_count: number;
|
|
82
|
+
untested_count: number;
|
|
83
|
+
blast_radius: {
|
|
84
|
+
functions: number;
|
|
85
|
+
files: number;
|
|
86
|
+
};
|
|
87
|
+
risk_level: 'LOW' | 'MEDIUM' | 'HIGH';
|
|
88
|
+
risk_score: number;
|
|
89
|
+
}
|
|
90
|
+
export interface ContextOutput {
|
|
91
|
+
text: string;
|
|
92
|
+
metadata: ContextMetadata;
|
|
93
|
+
}
|
|
94
|
+
export interface MainGraphInput {
|
|
95
|
+
repo_id: string;
|
|
96
|
+
sha: string;
|
|
97
|
+
nodes: GraphNode[];
|
|
98
|
+
edges: GraphEdge[];
|
|
99
|
+
}
|
|
100
|
+
export interface CallerRef {
|
|
101
|
+
qualified_name: string;
|
|
102
|
+
name: string;
|
|
103
|
+
file_path: string;
|
|
104
|
+
line: number;
|
|
105
|
+
confidence: number;
|
|
106
|
+
}
|
|
107
|
+
export interface CalleeRef {
|
|
108
|
+
qualified_name: string;
|
|
109
|
+
name: string;
|
|
110
|
+
file_path: string;
|
|
111
|
+
signature: string;
|
|
112
|
+
}
|
|
113
|
+
export interface EnrichedFunction {
|
|
114
|
+
qualified_name: string;
|
|
115
|
+
name: string;
|
|
116
|
+
kind: NodeKind;
|
|
117
|
+
signature: string;
|
|
118
|
+
file_path: string;
|
|
119
|
+
line_start: number;
|
|
120
|
+
line_end: number;
|
|
121
|
+
callers: CallerRef[];
|
|
122
|
+
callees: CalleeRef[];
|
|
123
|
+
has_test_coverage: boolean;
|
|
124
|
+
diff_changes: string[];
|
|
125
|
+
is_new: boolean;
|
|
126
|
+
in_flows: string[];
|
|
127
|
+
}
|
|
128
|
+
export interface AffectedFlow {
|
|
129
|
+
entry_point: string;
|
|
130
|
+
type: 'test' | 'http';
|
|
131
|
+
touches_changed: string[];
|
|
132
|
+
depth: number;
|
|
133
|
+
path: string[];
|
|
134
|
+
}
|
|
135
|
+
export interface InheritanceEntry {
|
|
136
|
+
qualified_name: string;
|
|
137
|
+
file_path: string;
|
|
138
|
+
extends?: string;
|
|
139
|
+
implements: string[];
|
|
140
|
+
children: string[];
|
|
141
|
+
}
|
|
142
|
+
export interface ContextAnalysisMetadata {
|
|
143
|
+
changed_functions_count: number;
|
|
144
|
+
total_callers: number;
|
|
145
|
+
total_callees: number;
|
|
146
|
+
untested_count: number;
|
|
147
|
+
affected_flows_count: number;
|
|
148
|
+
duration_ms: number;
|
|
149
|
+
min_confidence: number;
|
|
150
|
+
}
|
|
151
|
+
export interface RawFunction {
|
|
152
|
+
name: string;
|
|
153
|
+
file: string;
|
|
154
|
+
line_start: number;
|
|
155
|
+
line_end: number;
|
|
156
|
+
params: string;
|
|
157
|
+
returnType: string;
|
|
158
|
+
kind: 'Function' | 'Method' | 'Constructor';
|
|
159
|
+
ast_kind: string;
|
|
160
|
+
className: string;
|
|
161
|
+
qualified: string;
|
|
162
|
+
content_hash?: string;
|
|
163
|
+
}
|
|
164
|
+
export interface RawClass {
|
|
165
|
+
name: string;
|
|
166
|
+
file: string;
|
|
167
|
+
line_start: number;
|
|
168
|
+
line_end: number;
|
|
169
|
+
extends: string;
|
|
170
|
+
implements: string[];
|
|
171
|
+
ast_kind: string;
|
|
172
|
+
qualified: string;
|
|
173
|
+
content_hash?: string;
|
|
174
|
+
}
|
|
175
|
+
export interface RawInterface {
|
|
176
|
+
name: string;
|
|
177
|
+
file: string;
|
|
178
|
+
line_start: number;
|
|
179
|
+
line_end: number;
|
|
180
|
+
methods: string[];
|
|
181
|
+
ast_kind: string;
|
|
182
|
+
qualified: string;
|
|
183
|
+
content_hash?: string;
|
|
184
|
+
}
|
|
185
|
+
export interface RawEnum {
|
|
186
|
+
name: string;
|
|
187
|
+
file: string;
|
|
188
|
+
line_start: number;
|
|
189
|
+
line_end: number;
|
|
190
|
+
ast_kind: string;
|
|
191
|
+
qualified: string;
|
|
192
|
+
content_hash?: string;
|
|
193
|
+
}
|
|
194
|
+
export interface RawTest {
|
|
195
|
+
name: string;
|
|
196
|
+
file: string;
|
|
197
|
+
line_start: number;
|
|
198
|
+
line_end: number;
|
|
199
|
+
ast_kind: string;
|
|
200
|
+
qualified: string;
|
|
201
|
+
content_hash?: string;
|
|
202
|
+
}
|
|
203
|
+
export interface RawImport {
|
|
204
|
+
module: string;
|
|
205
|
+
file: string;
|
|
206
|
+
line: number;
|
|
207
|
+
names: string[];
|
|
208
|
+
lang: string;
|
|
209
|
+
}
|
|
210
|
+
export interface RawReExport {
|
|
211
|
+
module: string;
|
|
212
|
+
file: string;
|
|
213
|
+
line: number;
|
|
214
|
+
}
|
|
215
|
+
export interface RawCallSite {
|
|
216
|
+
source: string;
|
|
217
|
+
callName: string;
|
|
218
|
+
line: number;
|
|
219
|
+
diField?: string;
|
|
220
|
+
resolveInClass?: string;
|
|
221
|
+
}
|
|
222
|
+
export interface RawCallEdge {
|
|
223
|
+
source: string;
|
|
224
|
+
target: string;
|
|
225
|
+
callName: string;
|
|
226
|
+
line: number;
|
|
227
|
+
confidence: number;
|
|
228
|
+
}
|
|
229
|
+
export interface ImportEdge {
|
|
230
|
+
source: string;
|
|
231
|
+
target: string;
|
|
232
|
+
resolved: boolean;
|
|
233
|
+
line: number;
|
|
234
|
+
}
|
|
235
|
+
export interface RawGraph {
|
|
236
|
+
functions: RawFunction[];
|
|
237
|
+
classes: RawClass[];
|
|
238
|
+
interfaces: RawInterface[];
|
|
239
|
+
enums: RawEnum[];
|
|
240
|
+
tests: RawTest[];
|
|
241
|
+
imports: RawImport[];
|
|
242
|
+
reExports: RawReExport[];
|
|
243
|
+
rawCalls: RawCallSite[];
|
|
244
|
+
diMaps: Map<string, Map<string, string>>;
|
|
245
|
+
}
|
|
246
|
+
export interface ParseBatchResult extends RawGraph {
|
|
247
|
+
parseErrors: number;
|
|
248
|
+
extractErrors: number;
|
|
249
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { parseAsync } from '@ast-grep/napi';
|
|
2
|
+
import { readFileSync } from 'fs';
|
|
3
|
+
import { extname, relative } from 'path';
|
|
4
|
+
import { NOISE } from '../shared/filters';
|
|
5
|
+
import { log } from '../shared/logger';
|
|
6
|
+
import { extractCallsFromFile, extractFromFile } from './extractor';
|
|
7
|
+
import { getLanguage } from './languages';
|
|
8
|
+
const BATCH_SIZE = 50;
|
|
9
|
+
export async function parseBatch(files, repoRoot, options) {
|
|
10
|
+
const graph = {
|
|
11
|
+
functions: [],
|
|
12
|
+
classes: [],
|
|
13
|
+
interfaces: [],
|
|
14
|
+
enums: [],
|
|
15
|
+
tests: [],
|
|
16
|
+
imports: [],
|
|
17
|
+
reExports: [],
|
|
18
|
+
rawCalls: [],
|
|
19
|
+
diMaps: new Map(),
|
|
20
|
+
};
|
|
21
|
+
const seen = new Set();
|
|
22
|
+
let parseErrors = 0;
|
|
23
|
+
let extractErrors = 0;
|
|
24
|
+
for (let i = 0; i < files.length; i += BATCH_SIZE) {
|
|
25
|
+
const batch = files.slice(i, i + BATCH_SIZE);
|
|
26
|
+
const promises = batch.map(async (filePath) => {
|
|
27
|
+
const lang = getLanguage(extname(filePath));
|
|
28
|
+
if (!lang) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
let source;
|
|
32
|
+
try {
|
|
33
|
+
source = readFileSync(filePath, 'utf-8');
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
log.warn('Failed to read file', { file: filePath, error: String(err) });
|
|
37
|
+
parseErrors++;
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
let root;
|
|
41
|
+
try {
|
|
42
|
+
root = await parseAsync(lang, source);
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
log.warn('Failed to parse file', { file: filePath, error: String(err) });
|
|
46
|
+
parseErrors++;
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const fp = relative(repoRoot, filePath);
|
|
50
|
+
try {
|
|
51
|
+
extractFromFile(root, fp, lang, seen, graph);
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
log.error('Extraction crashed', { file: fp, error: String(err) });
|
|
55
|
+
extractErrors++;
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
// Extract calls into a temporary buffer, then filter noise before pushing
|
|
59
|
+
const rawCalls = [];
|
|
60
|
+
extractCallsFromFile(root, fp, lang, rawCalls);
|
|
61
|
+
for (const call of rawCalls) {
|
|
62
|
+
if (!NOISE.has(call.callName)) {
|
|
63
|
+
graph.rawCalls.push(call);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
log.error('Call extraction crashed', { file: fp, error: String(err) });
|
|
69
|
+
extractErrors++;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
await Promise.all(promises);
|
|
73
|
+
}
|
|
74
|
+
if (options?.skipTests) {
|
|
75
|
+
graph.tests = [];
|
|
76
|
+
}
|
|
77
|
+
return { ...graph, parseErrors, extractErrors };
|
|
78
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Walk the filesystem and find all supported source files.
|
|
3
|
+
* If `filterFiles` is provided, only return those specific files (resolved to absolute paths).
|
|
4
|
+
* If `include` patterns are provided, keep only files matching at least one pattern.
|
|
5
|
+
* If `exclude` patterns are provided, remove files matching any pattern.
|
|
6
|
+
*/
|
|
7
|
+
export declare function discoverFiles(repoDir: string, filterFiles?: string[], include?: string[], exclude?: string[]): string[];
|