pycode-kg 0.16.0__py3-none-any.whl
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.
- pycode_kg/.DS_Store +0 -0
- pycode_kg/__init__.py +91 -0
- pycode_kg/__main__.py +11 -0
- pycode_kg/analysis/__init__.py +15 -0
- pycode_kg/analysis/bridge.py +108 -0
- pycode_kg/analysis/centrality.py +412 -0
- pycode_kg/analysis/framework_detector.py +103 -0
- pycode_kg/analysis/hybrid_rank.py +53 -0
- pycode_kg/app.py +1335 -0
- pycode_kg/architecture.py +624 -0
- pycode_kg/build_pycodekg_lancedb.py +15 -0
- pycode_kg/build_pycodekg_sqlite.py +15 -0
- pycode_kg/cli/__init__.py +29 -0
- pycode_kg/cli/cmd_analyze.py +96 -0
- pycode_kg/cli/cmd_architecture.py +150 -0
- pycode_kg/cli/cmd_bridges.py +26 -0
- pycode_kg/cli/cmd_build.py +154 -0
- pycode_kg/cli/cmd_build_full.py +242 -0
- pycode_kg/cli/cmd_centrality.py +131 -0
- pycode_kg/cli/cmd_explain.py +180 -0
- pycode_kg/cli/cmd_framework_nodes.py +18 -0
- pycode_kg/cli/cmd_hooks.py +137 -0
- pycode_kg/cli/cmd_init.py +312 -0
- pycode_kg/cli/cmd_mcp.py +71 -0
- pycode_kg/cli/cmd_model.py +53 -0
- pycode_kg/cli/cmd_query.py +211 -0
- pycode_kg/cli/cmd_snapshot.py +421 -0
- pycode_kg/cli/cmd_viz.py +180 -0
- pycode_kg/cli/main.py +23 -0
- pycode_kg/cli/options.py +63 -0
- pycode_kg/config.py +78 -0
- pycode_kg/graph.py +125 -0
- pycode_kg/index.py +542 -0
- pycode_kg/kg.py +220 -0
- pycode_kg/layout3d.py +470 -0
- pycode_kg/mcp/bridge_tools.py +19 -0
- pycode_kg/mcp/framework_tools.py +18 -0
- pycode_kg/mcp_server.py +1965 -0
- pycode_kg/module/__init__.py +83 -0
- pycode_kg/module/base.py +720 -0
- pycode_kg/module/extractor.py +276 -0
- pycode_kg/module/types.py +532 -0
- pycode_kg/pycodekg.py +543 -0
- pycode_kg/pycodekg_query.py +1 -0
- pycode_kg/pycodekg_snippet_packer.py +1 -0
- pycode_kg/pycodekg_thorough_analysis.py +2751 -0
- pycode_kg/pycodekg_viz.py +1 -0
- pycode_kg/pycodekg_viz3d.py +1 -0
- pycode_kg/ranking/__init__.py +1 -0
- pycode_kg/ranking/cli_rank.py +92 -0
- pycode_kg/ranking/coderank.py +555 -0
- pycode_kg/snapshots.py +612 -0
- pycode_kg/sql/004_add_centrality_table.sql +12 -0
- pycode_kg/store.py +766 -0
- pycode_kg/utils.py +39 -0
- pycode_kg/visitor.py +413 -0
- pycode_kg/viz3d.py +1353 -0
- pycode_kg/viz3d_timeline.py +364 -0
- pycode_kg-0.16.0.dist-info/METADATA +305 -0
- pycode_kg-0.16.0.dist-info/RECORD +63 -0
- pycode_kg-0.16.0.dist-info/WHEEL +4 -0
- pycode_kg-0.16.0.dist-info/entry_points.txt +19 -0
- pycode_kg-0.16.0.dist-info/licenses/LICENSE +94 -0
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
"""
|
|
2
|
+
viz3d_timeline.py — Temporal Metrics Visualization for PyCodeKG Snapshots
|
|
3
|
+
|
|
4
|
+
Visualizes codebase metrics evolution across commits using 3D plotting.
|
|
5
|
+
Shows trends in nodes, edges, coverage, and critical issues over time.
|
|
6
|
+
|
|
7
|
+
Features:
|
|
8
|
+
- Interactive 3D line charts for metric trends
|
|
9
|
+
- Side-by-side comparison of multiple metrics
|
|
10
|
+
- Commit-based X-axis with version labels
|
|
11
|
+
- Color-coded risk assessment (green→yellow→red)
|
|
12
|
+
- Hover tooltips with snapshot details
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
import plotly.graph_objects as go
|
|
21
|
+
import plotly.subplots as subplots
|
|
22
|
+
|
|
23
|
+
from pycode_kg.snapshots import SnapshotManager
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def load_snapshots_timeline(snapshots_dir: Path) -> dict[str, Any]:
|
|
27
|
+
"""
|
|
28
|
+
Load all snapshots and extract timeline data.
|
|
29
|
+
|
|
30
|
+
:param snapshots_dir: Path to .pycodekg/snapshots/
|
|
31
|
+
:return: Dict with timeline data indexed by metric name.
|
|
32
|
+
"""
|
|
33
|
+
mgr = SnapshotManager(snapshots_dir)
|
|
34
|
+
snapshots = mgr.list_snapshots() # Chronological order
|
|
35
|
+
|
|
36
|
+
if not snapshots:
|
|
37
|
+
return {}
|
|
38
|
+
|
|
39
|
+
# Extract timeline data
|
|
40
|
+
timeline: dict[str, list[Any]] = {
|
|
41
|
+
"commits": [],
|
|
42
|
+
"versions": [],
|
|
43
|
+
"timestamps": [],
|
|
44
|
+
"nodes": [],
|
|
45
|
+
"edges": [],
|
|
46
|
+
"coverage": [],
|
|
47
|
+
"critical_issues": [],
|
|
48
|
+
"complexity_median": [],
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
for snap in snapshots:
|
|
52
|
+
timeline["commits"].append(snap["commit"][:7]) # Short hash
|
|
53
|
+
timeline["versions"].append(snap["version"])
|
|
54
|
+
timeline["timestamps"].append(snap["timestamp"])
|
|
55
|
+
timeline["nodes"].append(snap["metrics"]["nodes"])
|
|
56
|
+
timeline["edges"].append(snap["metrics"]["edges"])
|
|
57
|
+
timeline["coverage"].append(snap["metrics"]["coverage"] * 100) # Percentage
|
|
58
|
+
timeline["critical_issues"].append(snap["metrics"]["critical_issues"])
|
|
59
|
+
timeline["complexity_median"].append(snap.get("metrics", {}).get("complexity_median", 0))
|
|
60
|
+
|
|
61
|
+
return timeline
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def create_timeline_figure(snapshots_dir: Path) -> go.Figure:
|
|
65
|
+
"""
|
|
66
|
+
Create interactive 3D timeline visualization.
|
|
67
|
+
|
|
68
|
+
Shows 4 metrics:
|
|
69
|
+
1. Nodes (absolute count)
|
|
70
|
+
2. Edges (absolute count)
|
|
71
|
+
3. Coverage (percentage)
|
|
72
|
+
4. Critical Issues (count)
|
|
73
|
+
|
|
74
|
+
:param snapshots_dir: Path to .pycodekg/snapshots/
|
|
75
|
+
:return: Plotly Figure ready for display.
|
|
76
|
+
"""
|
|
77
|
+
timeline = load_snapshots_timeline(snapshots_dir)
|
|
78
|
+
|
|
79
|
+
if not timeline["commits"]:
|
|
80
|
+
return go.Figure().add_annotation(text="No snapshots found")
|
|
81
|
+
|
|
82
|
+
# Create subplot figure with 2x2 layout
|
|
83
|
+
fig = subplots.make_subplots(
|
|
84
|
+
rows=2,
|
|
85
|
+
cols=2,
|
|
86
|
+
subplot_titles=("Total Nodes", "Total Edges", "Docstring Coverage %", "Critical Issues"),
|
|
87
|
+
specs=[
|
|
88
|
+
[{"secondary_y": False}, {"secondary_y": False}],
|
|
89
|
+
[{"secondary_y": False}, {"secondary_y": False}],
|
|
90
|
+
],
|
|
91
|
+
vertical_spacing=0.15,
|
|
92
|
+
horizontal_spacing=0.12,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
# Hover text for all traces
|
|
96
|
+
hover_text = [
|
|
97
|
+
f"<b>{v}</b> ({c})<br>Timestamp: {t}"
|
|
98
|
+
for v, c, t in zip(timeline["versions"], timeline["commits"], timeline["timestamps"])
|
|
99
|
+
]
|
|
100
|
+
|
|
101
|
+
# 1. Total Nodes (green trend)
|
|
102
|
+
fig.add_trace(
|
|
103
|
+
go.Scatter(
|
|
104
|
+
x=timeline["commits"],
|
|
105
|
+
y=timeline["nodes"],
|
|
106
|
+
mode="lines+markers",
|
|
107
|
+
name="Nodes",
|
|
108
|
+
line=dict(color="#22c55e", width=3),
|
|
109
|
+
marker=dict(size=8),
|
|
110
|
+
hovertemplate="<b>Nodes:</b> %{y}<br>" + "%{customdata}<extra></extra>",
|
|
111
|
+
customdata=hover_text,
|
|
112
|
+
),
|
|
113
|
+
row=1,
|
|
114
|
+
col=1,
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
# 2. Total Edges (blue trend)
|
|
118
|
+
fig.add_trace(
|
|
119
|
+
go.Scatter(
|
|
120
|
+
x=timeline["commits"],
|
|
121
|
+
y=timeline["edges"],
|
|
122
|
+
mode="lines+markers",
|
|
123
|
+
name="Edges",
|
|
124
|
+
line=dict(color="#3b82f6", width=3),
|
|
125
|
+
marker=dict(size=8),
|
|
126
|
+
hovertemplate="<b>Edges:</b> %{y}<br>" + "%{customdata}<extra></extra>",
|
|
127
|
+
customdata=hover_text,
|
|
128
|
+
),
|
|
129
|
+
row=1,
|
|
130
|
+
col=2,
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
# 3. Coverage % (cyan trend, with target line)
|
|
134
|
+
fig.add_trace(
|
|
135
|
+
go.Scatter(
|
|
136
|
+
x=timeline["commits"],
|
|
137
|
+
y=timeline["coverage"],
|
|
138
|
+
mode="lines+markers",
|
|
139
|
+
name="Coverage %",
|
|
140
|
+
line=dict(color="#06b6d4", width=3),
|
|
141
|
+
marker=dict(size=8),
|
|
142
|
+
hovertemplate="<b>Coverage:</b> %{y:.1f}%<br>" + "%{customdata}<extra></extra>",
|
|
143
|
+
customdata=hover_text,
|
|
144
|
+
),
|
|
145
|
+
row=2,
|
|
146
|
+
col=1,
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
# Add 90% target line for coverage
|
|
150
|
+
fig.add_hline(
|
|
151
|
+
y=90,
|
|
152
|
+
line_dash="dash",
|
|
153
|
+
line_color="orange",
|
|
154
|
+
annotation_text="90% Target",
|
|
155
|
+
row=2,
|
|
156
|
+
col=1,
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
# 4. Critical Issues (red risk indicator, lower is better)
|
|
160
|
+
fig.add_trace(
|
|
161
|
+
go.Scatter(
|
|
162
|
+
x=timeline["commits"],
|
|
163
|
+
y=timeline["critical_issues"],
|
|
164
|
+
mode="lines+markers",
|
|
165
|
+
name="Critical Issues",
|
|
166
|
+
line=dict(color="#ef4444", width=3),
|
|
167
|
+
marker=dict(size=8),
|
|
168
|
+
hovertemplate="<b>Issues:</b> %{y}<br>" + "%{customdata}<extra></extra>",
|
|
169
|
+
customdata=hover_text,
|
|
170
|
+
),
|
|
171
|
+
row=2,
|
|
172
|
+
col=2,
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
# Update layout
|
|
176
|
+
fig.update_layout(
|
|
177
|
+
title_text="<b>PyCodeKG Temporal Metrics Evolution</b><br><sub>Snapshots across commits</sub>",
|
|
178
|
+
title_font_size=20,
|
|
179
|
+
height=800,
|
|
180
|
+
width=1400,
|
|
181
|
+
hovermode="x unified",
|
|
182
|
+
template="plotly_dark",
|
|
183
|
+
showlegend=False,
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
# Update X axes
|
|
187
|
+
for i in range(1, 5):
|
|
188
|
+
row = (i - 1) // 2 + 1
|
|
189
|
+
col = (i - 1) % 2 + 1
|
|
190
|
+
fig.update_xaxes(title_text="Commit", row=row, col=col)
|
|
191
|
+
|
|
192
|
+
# Update Y axes
|
|
193
|
+
fig.update_yaxes(title_text="Count", row=1, col=1)
|
|
194
|
+
fig.update_yaxes(title_text="Count", row=1, col=2)
|
|
195
|
+
fig.update_yaxes(title_text="Percentage", row=2, col=1)
|
|
196
|
+
fig.update_yaxes(title_text="Count", row=2, col=2)
|
|
197
|
+
|
|
198
|
+
return fig
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def create_3d_timeline_figure(snapshots_dir: Path) -> go.Figure:
|
|
202
|
+
"""
|
|
203
|
+
Create 3D surface plot showing metrics evolution.
|
|
204
|
+
|
|
205
|
+
X: Commit index
|
|
206
|
+
Y: Metric value (normalized)
|
|
207
|
+
Z: Different metrics stacked
|
|
208
|
+
|
|
209
|
+
:param snapshots_dir: Path to .pycodekg/snapshots/
|
|
210
|
+
:return: Plotly 3D Figure.
|
|
211
|
+
"""
|
|
212
|
+
timeline = load_snapshots_timeline(snapshots_dir)
|
|
213
|
+
|
|
214
|
+
if not timeline["commits"]:
|
|
215
|
+
return go.Figure().add_annotation(text="No snapshots found")
|
|
216
|
+
|
|
217
|
+
# Normalize metrics to 0-100 scale for comparison
|
|
218
|
+
max_nodes = max(timeline["nodes"]) if timeline["nodes"] else 100
|
|
219
|
+
max_edges = max(timeline["edges"]) if timeline["edges"] else 100
|
|
220
|
+
max_issues = max(timeline["critical_issues"]) if timeline["critical_issues"] else 1
|
|
221
|
+
|
|
222
|
+
normalized_nodes = [n / max_nodes * 100 for n in timeline["nodes"]]
|
|
223
|
+
normalized_edges = [e / max_edges * 100 for e in timeline["edges"]]
|
|
224
|
+
normalized_coverage = timeline["coverage"] # Already 0-100
|
|
225
|
+
normalized_issues = [i / max_issues * 100 for i in timeline["critical_issues"]]
|
|
226
|
+
|
|
227
|
+
fig = go.Figure()
|
|
228
|
+
|
|
229
|
+
# Commit indices for X axis
|
|
230
|
+
commits_idx = list(range(len(timeline["commits"])))
|
|
231
|
+
|
|
232
|
+
# Add surface traces for each metric
|
|
233
|
+
# Nodes (green)
|
|
234
|
+
fig.add_trace(
|
|
235
|
+
go.Scatter3d(
|
|
236
|
+
x=commits_idx,
|
|
237
|
+
y=normalized_nodes,
|
|
238
|
+
z=[0] * len(commits_idx),
|
|
239
|
+
mode="lines+markers",
|
|
240
|
+
name="Nodes (normalized)",
|
|
241
|
+
line=dict(color="#22c55e", width=4),
|
|
242
|
+
marker=dict(size=6),
|
|
243
|
+
hovertemplate="<b>Commit:</b> %{customdata}<br><b>Nodes:</b> %{y:.0f}%<extra></extra>",
|
|
244
|
+
customdata=timeline["commits"],
|
|
245
|
+
)
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
# Edges (blue)
|
|
249
|
+
fig.add_trace(
|
|
250
|
+
go.Scatter3d(
|
|
251
|
+
x=commits_idx,
|
|
252
|
+
y=normalized_edges,
|
|
253
|
+
z=[1] * len(commits_idx),
|
|
254
|
+
mode="lines+markers",
|
|
255
|
+
name="Edges (normalized)",
|
|
256
|
+
line=dict(color="#3b82f6", width=4),
|
|
257
|
+
marker=dict(size=6),
|
|
258
|
+
hovertemplate="<b>Commit:</b> %{customdata}<br><b>Edges:</b> %{y:.0f}%<extra></extra>",
|
|
259
|
+
customdata=timeline["commits"],
|
|
260
|
+
)
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
# Coverage (cyan)
|
|
264
|
+
fig.add_trace(
|
|
265
|
+
go.Scatter3d(
|
|
266
|
+
x=commits_idx,
|
|
267
|
+
y=normalized_coverage,
|
|
268
|
+
z=[2] * len(commits_idx),
|
|
269
|
+
mode="lines+markers",
|
|
270
|
+
name="Coverage %",
|
|
271
|
+
line=dict(color="#06b6d4", width=4),
|
|
272
|
+
marker=dict(size=6),
|
|
273
|
+
hovertemplate="<b>Commit:</b> %{customdata}<br><b>Coverage:</b> %{y:.0f}%<extra></extra>",
|
|
274
|
+
customdata=timeline["commits"],
|
|
275
|
+
)
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
# Critical Issues (red - inverted so lower is up)
|
|
279
|
+
fig.add_trace(
|
|
280
|
+
go.Scatter3d(
|
|
281
|
+
x=commits_idx,
|
|
282
|
+
y=[100 - v for v in normalized_issues], # Invert so lower is better (up)
|
|
283
|
+
z=[3] * len(commits_idx),
|
|
284
|
+
mode="lines+markers",
|
|
285
|
+
name="Health (inverse issues)",
|
|
286
|
+
line=dict(color="#ef4444", width=4),
|
|
287
|
+
marker=dict(size=6),
|
|
288
|
+
hovertemplate="<b>Commit:</b> %{customdata}<br><b>Health:</b> %{y:.0f}%<extra></extra>",
|
|
289
|
+
customdata=timeline["commits"],
|
|
290
|
+
)
|
|
291
|
+
)
|
|
292
|
+
|
|
293
|
+
fig.update_layout(
|
|
294
|
+
title="<b>PyCodeKG 3D Temporal Metrics</b><br><sub>Evolution across commits</sub>",
|
|
295
|
+
scene=dict(
|
|
296
|
+
xaxis_title="Commit Index",
|
|
297
|
+
yaxis_title="Metric (0-100%)",
|
|
298
|
+
zaxis=dict(
|
|
299
|
+
title="Metric Type",
|
|
300
|
+
tickvals=[0, 1, 2, 3],
|
|
301
|
+
ticktext=["Nodes", "Edges", "Coverage", "Health"],
|
|
302
|
+
),
|
|
303
|
+
camera=dict(
|
|
304
|
+
eye=dict(x=1.5, y=1.5, z=1.3),
|
|
305
|
+
),
|
|
306
|
+
),
|
|
307
|
+
width=1200,
|
|
308
|
+
height=800,
|
|
309
|
+
template="plotly_dark",
|
|
310
|
+
hovermode="closest",
|
|
311
|
+
)
|
|
312
|
+
|
|
313
|
+
return fig
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def display_timeline_summary(snapshots_dir: Path) -> str:
|
|
317
|
+
"""
|
|
318
|
+
Generate text summary of timeline metrics.
|
|
319
|
+
|
|
320
|
+
:param snapshots_dir: Path to .pycodekg/snapshots/
|
|
321
|
+
:return: Formatted summary string.
|
|
322
|
+
"""
|
|
323
|
+
timeline = load_snapshots_timeline(snapshots_dir)
|
|
324
|
+
|
|
325
|
+
if not timeline["commits"]:
|
|
326
|
+
return "No snapshots found"
|
|
327
|
+
|
|
328
|
+
# Calculate deltas
|
|
329
|
+
nodes_delta = timeline["nodes"][-1] - timeline["nodes"][0]
|
|
330
|
+
edges_delta = timeline["edges"][-1] - timeline["edges"][0]
|
|
331
|
+
coverage_delta = timeline["coverage"][-1] - timeline["coverage"][0]
|
|
332
|
+
issues_delta = timeline["critical_issues"][-1] - timeline["critical_issues"][0]
|
|
333
|
+
|
|
334
|
+
summary = f"""
|
|
335
|
+
+================================================================+
|
|
336
|
+
| PyCodeKG Temporal Metrics Summary |
|
|
337
|
+
+================================================================+
|
|
338
|
+
| Snapshots Captured: {len(timeline["commits"]):<41} |
|
|
339
|
+
| Time Range: {timeline["commits"][0]} → {timeline["commits"][-1]:<44} |
|
|
340
|
+
| Versions: {timeline["versions"][0]} → {timeline["versions"][-1]:<48} |
|
|
341
|
+
+================================================================+
|
|
342
|
+
| NODES: |
|
|
343
|
+
| First: {timeline["nodes"][0]:<47} |
|
|
344
|
+
| Latest: {timeline["nodes"][-1]:<47} |
|
|
345
|
+
| Δ: {nodes_delta:+d:<46} |
|
|
346
|
+
+================================================================+
|
|
347
|
+
| EDGES: |
|
|
348
|
+
| First: {timeline["edges"][0]:<47} |
|
|
349
|
+
| Latest: {timeline["edges"][-1]:<47} |
|
|
350
|
+
| Δ: {edges_delta:+d:<46} |
|
|
351
|
+
+================================================================+
|
|
352
|
+
| DOCSTRING COVERAGE: |
|
|
353
|
+
| First: {timeline["coverage"][0]:.1f}%<{45} |
|
|
354
|
+
| Latest: {timeline["coverage"][-1]:.1f}%<{45} |
|
|
355
|
+
| Δ: {coverage_delta:+.1f}%<{44} |
|
|
356
|
+
+================================================================+
|
|
357
|
+
| CRITICAL ISSUES: |
|
|
358
|
+
| First: {timeline["critical_issues"][0]:<47} |
|
|
359
|
+
| Latest: {timeline["critical_issues"][-1]:<47} |
|
|
360
|
+
| Δ: {issues_delta:+d:<46} |
|
|
361
|
+
| Trend: {"Improving" if issues_delta <= 0 else "Regressing":<43} |
|
|
362
|
+
+================================================================+
|
|
363
|
+
"""
|
|
364
|
+
return summary
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pycode-kg
|
|
3
|
+
Version: 0.16.0
|
|
4
|
+
Summary: A tool to build a searchable knowledge graph from Python repositories
|
|
5
|
+
License-Expression: Elastic-2.0
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Keywords: knowledge-graph,code-analysis,ast,lancedb,sqlite,semantic-search
|
|
8
|
+
Author: Eric G. Suchanek, PhD
|
|
9
|
+
Author-email: suchanek@mac.com
|
|
10
|
+
Requires-Python: >=3.12,<3.14
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
14
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Provides-Extra: all
|
|
19
|
+
Provides-Extra: dev
|
|
20
|
+
Provides-Extra: kgdeps
|
|
21
|
+
Provides-Extra: viz
|
|
22
|
+
Provides-Extra: viz3d
|
|
23
|
+
Requires-Dist: PyQt5 (>=5.15.0) ; extra == "all"
|
|
24
|
+
Requires-Dist: PyQt5 (>=5.15.0) ; extra == "viz3d"
|
|
25
|
+
Requires-Dist: click (>=8.1.0,<9)
|
|
26
|
+
Requires-Dist: detect-secrets (>=1.5.0) ; extra == "all"
|
|
27
|
+
Requires-Dist: detect-secrets (>=1.5.0) ; extra == "dev"
|
|
28
|
+
Requires-Dist: doc-kg (>=0.11.0) ; extra == "dev"
|
|
29
|
+
Requires-Dist: doc-kg (>=0.11.0) ; extra == "kgdeps"
|
|
30
|
+
Requires-Dist: kg-snapshot (>=0.3.0)
|
|
31
|
+
Requires-Dist: lancedb (>=0.29.0)
|
|
32
|
+
Requires-Dist: markdown (>=3.6) ; extra == "all"
|
|
33
|
+
Requires-Dist: markdown (>=3.6) ; extra == "viz3d"
|
|
34
|
+
Requires-Dist: mcp (>=1.0.0)
|
|
35
|
+
Requires-Dist: mypy (>=1.10.0) ; extra == "all"
|
|
36
|
+
Requires-Dist: mypy (>=1.10.0) ; extra == "dev"
|
|
37
|
+
Requires-Dist: numpy (>=1.24.0)
|
|
38
|
+
Requires-Dist: pandas (>=2.0.0)
|
|
39
|
+
Requires-Dist: param (>=2.0.0) ; extra == "all"
|
|
40
|
+
Requires-Dist: param (>=2.0.0) ; extra == "viz3d"
|
|
41
|
+
Requires-Dist: pdoc (>=14.0.0) ; extra == "all"
|
|
42
|
+
Requires-Dist: pdoc (>=14.0.0) ; extra == "dev"
|
|
43
|
+
Requires-Dist: plotly (>=5.14.0) ; extra == "all"
|
|
44
|
+
Requires-Dist: plotly (>=5.14.0) ; extra == "viz"
|
|
45
|
+
Requires-Dist: pre-commit (>=4.5.1) ; extra == "all"
|
|
46
|
+
Requires-Dist: pre-commit (>=4.5.1) ; extra == "dev"
|
|
47
|
+
Requires-Dist: pylint (>=4.0.5) ; extra == "all"
|
|
48
|
+
Requires-Dist: pylint (>=4.0.5) ; extra == "dev"
|
|
49
|
+
Requires-Dist: pytest (>=8.0.0) ; extra == "all"
|
|
50
|
+
Requires-Dist: pytest (>=8.0.0) ; extra == "dev"
|
|
51
|
+
Requires-Dist: pytest-cov (>=5.0.0) ; extra == "all"
|
|
52
|
+
Requires-Dist: pytest-cov (>=5.0.0) ; extra == "dev"
|
|
53
|
+
Requires-Dist: pyvis (>=0.3.2) ; extra == "all"
|
|
54
|
+
Requires-Dist: pyvis (>=0.3.2) ; extra == "viz"
|
|
55
|
+
Requires-Dist: pyvista[jupyter] (>=0.44.0) ; extra == "all"
|
|
56
|
+
Requires-Dist: pyvista[jupyter] (>=0.44.0) ; extra == "viz3d"
|
|
57
|
+
Requires-Dist: pyvistaqt (>=0.11.0) ; extra == "all"
|
|
58
|
+
Requires-Dist: pyvistaqt (>=0.11.0) ; extra == "viz3d"
|
|
59
|
+
Requires-Dist: rich (>=14.3.3,<15)
|
|
60
|
+
Requires-Dist: ruff (>=0.4.0) ; extra == "all"
|
|
61
|
+
Requires-Dist: ruff (>=0.4.0) ; extra == "dev"
|
|
62
|
+
Requires-Dist: safetensors (>=0.5.0)
|
|
63
|
+
Requires-Dist: sentence-transformers (>=5.4.1)
|
|
64
|
+
Requires-Dist: streamlit (>=1.35.0) ; extra == "all"
|
|
65
|
+
Requires-Dist: streamlit (>=1.35.0) ; extra == "viz"
|
|
66
|
+
Requires-Dist: torch (>=2.5.1)
|
|
67
|
+
Requires-Dist: trame-vtk (>=2.0.0) ; extra == "all"
|
|
68
|
+
Requires-Dist: trame-vtk (>=2.0.0) ; extra == "viz3d"
|
|
69
|
+
Requires-Dist: transformers (>=4.57.6)
|
|
70
|
+
Project-URL: Homepage, https://github.com/Flux-Frontiers/pycode_kg
|
|
71
|
+
Project-URL: Repository, https://github.com/Flux-Frontiers/pycode_kg
|
|
72
|
+
Description-Content-Type: text/markdown
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
[](https://www.python.org/)
|
|
76
|
+
[](https://www.elastic.co/licensing/elastic-license)
|
|
77
|
+
[](https://github.com/Flux-Frontiers/pycode_kg/releases)
|
|
78
|
+
[](https://github.com/Flux-Frontiers/pycode_kg/actions/workflows/ci.yml)
|
|
79
|
+
[](https://python-poetry.org/)
|
|
80
|
+
[](https://zenodo.org/badge/latestdoi/1202379010)
|
|
81
|
+
|
|
82
|
+
<p align="center">
|
|
83
|
+
<img src="assets/logo-md-256x256.png" alt="PyCodeKG logo" width="256"/>
|
|
84
|
+
</p>
|
|
85
|
+
|
|
86
|
+
**PyCodeKG** — A Deterministic Knowledge Graph for Python Codebases
|
|
87
|
+
with Semantic Indexing and Source-Grounded Snippet Packing
|
|
88
|
+
|
|
89
|
+
*Author: Eric G. Suchanek, PhD*
|
|
90
|
+
|
|
91
|
+
*Flux-Frontiers, Liberty TWP, OH*
|
|
92
|
+
|
|
93
|
+
[Technical Paper (PDF)](article/pycode_kg.pdf)
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## Overview
|
|
98
|
+
|
|
99
|
+
PyCodeKG constructs a **deterministic, explainable knowledge graph** from a Python codebase using static analysis. The graph captures structural relationships — definitions, calls, imports, and inheritance — directly from the Python AST, stores them in SQLite, and augments retrieval with vector embeddings via LanceDB.
|
|
100
|
+
|
|
101
|
+
Structure is treated as **ground truth**; semantic search is strictly an acceleration layer. The result is a searchable, auditable representation of a codebase that supports precise navigation, contextual snippet extraction, and downstream reasoning without hallucination.
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## What Agents Say
|
|
106
|
+
|
|
107
|
+
*From independent assessments run against PyCodeKG's own codebase. See [assessments/](assessments/) for the full reports.*
|
|
108
|
+
|
|
109
|
+
> "The workflow compression is real and substantial. Rather than reading files sequentially or running grep searches in the dark, an agent equipped with PyCodeKG can orient itself in seconds."
|
|
110
|
+
> — Claude Sonnet 4.6
|
|
111
|
+
|
|
112
|
+
> "Replaces hours of manual exploration with a single call. The most valuable tool in the suite."
|
|
113
|
+
> — Claude Opus 4, on `analyze_repo()`
|
|
114
|
+
|
|
115
|
+
> "It let me move from broad orientation to intent-driven discovery and then to structural validation without dropping down into manual grep or repeated file reads."
|
|
116
|
+
> — GPT-5 (via Cline)
|
|
117
|
+
|
|
118
|
+
> "Traditional file reading and grep-based exploration are slow, linear, and context-poor. PyCodeKG's semantic search, graph navigation, and architectural analysis provide a quantum leap in speed and depth of understanding."
|
|
119
|
+
> — GPT-4.1
|
|
120
|
+
|
|
121
|
+
> "`pack_snippets()` provided source excerpts around each hit, making the code instantly readable. Context lines and relevance metadata obviated manual file open."
|
|
122
|
+
> — Raptor Mini
|
|
123
|
+
|
|
124
|
+
> "Dramatically more effective than traditional grep/file-reading workflows. Unique value proposition: hybrid search combining natural-language intent with precise structural relationships."
|
|
125
|
+
> — Claude Haiku 4.5
|
|
126
|
+
|
|
127
|
+
---
|
|
128
|
+
|
|
129
|
+
## Quick Start
|
|
130
|
+
|
|
131
|
+
Run the one-line installer from within the repo you want to index:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
curl -fsSL https://raw.githubusercontent.com/Flux-Frontiers/pycode_kg/main/scripts/install-skill.sh | bash
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
This sets up everything end-to-end:
|
|
138
|
+
|
|
139
|
+
1. Installs SKILL.md reference files for Claude Code, Kilo Code, and other agents
|
|
140
|
+
2. Installs Claude Code slash commands (`/pycodekg`, `/setup-mcp`)
|
|
141
|
+
3. Installs the `pycode-kg` package if not already present
|
|
142
|
+
4. Builds the SQLite knowledge graph and LanceDB semantic index
|
|
143
|
+
5. Writes MCP configuration for Claude Code, Kilo Code, GitHub Copilot, and Cline
|
|
144
|
+
|
|
145
|
+
After the script completes, restart your AI agent to activate the MCP server.
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
# Preview without making changes
|
|
149
|
+
curl -fsSL .../install-skill.sh | bash -s -- --dry-run
|
|
150
|
+
|
|
151
|
+
# Claude Code and GitHub Copilot only
|
|
152
|
+
curl -fsSL .../install-skill.sh | bash -s -- --providers claude,copilot
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
→ **Full installation options, manual setup, and MCP config:** [docs/INSTALLATION.md](docs/INSTALLATION.md)
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## Features
|
|
160
|
+
|
|
161
|
+
- **Static analysis pipeline** — Three-pass AST extraction: structure, call graph, data-flow
|
|
162
|
+
- **Deterministic knowledge graph** — SQLite-backed canonical store with provenance
|
|
163
|
+
- **Symbol resolution** — `RESOLVES_TO` edges bridge cross-module call sites via import aliases
|
|
164
|
+
- **Hybrid query model** — Semantic seeding (LanceDB) + structural expansion (graph traversal)
|
|
165
|
+
- **Source-grounded snippet packing** — Definition and call-site snippets with line numbers
|
|
166
|
+
- **Precise fan-in lookup** — Two-phase reverse traversal resolving cross-module caller chains
|
|
167
|
+
- **MCP server** — Ten tools for AI agent integration
|
|
168
|
+
- **Streamlit web app** — Interactive graph browser, hybrid query UI, snippet pack explorer
|
|
169
|
+
- **3D visualizer** — PyVista/PyQt5 interactive graph explorer
|
|
170
|
+
- **Zero-config MCP setup** — Single-line installer configures Claude Code, Kilo Code, GitHub Copilot, and Cline
|
|
171
|
+
|
|
172
|
+
---
|
|
173
|
+
|
|
174
|
+
## Usage
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
# Build the knowledge graph
|
|
178
|
+
pycodekg build --repo /path/to/your/repo
|
|
179
|
+
|
|
180
|
+
# Natural-language query
|
|
181
|
+
pycodekg query "authentication flow"
|
|
182
|
+
|
|
183
|
+
# Source-grounded snippet pack — paste straight into an LLM prompt
|
|
184
|
+
pycodekg pack "database connection setup" --format md --out context.md
|
|
185
|
+
|
|
186
|
+
# Full architectural analysis
|
|
187
|
+
pycodekg analyze /path/to/your/repo
|
|
188
|
+
|
|
189
|
+
# Launch the interactive web app
|
|
190
|
+
pycodekg viz
|
|
191
|
+
|
|
192
|
+
# Start the MCP server
|
|
193
|
+
pycodekg mcp --repo /path/to/your/repo
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### MCP Tools (once the server is running)
|
|
197
|
+
|
|
198
|
+
```
|
|
199
|
+
graph_stats() # node/edge counts by kind
|
|
200
|
+
query_codebase("authentication flow") # hybrid semantic + structural search
|
|
201
|
+
pack_snippets("database layer") # source-grounded snippets as Markdown
|
|
202
|
+
get_node("fn:store:GraphStore.write") # fetch a single node by ID
|
|
203
|
+
callers("fn:store:GraphStore.write") # precise fan-in lookup
|
|
204
|
+
explain("fn:store:GraphStore.write") # natural-language explanation
|
|
205
|
+
analyze_repo() # full architectural analysis as Markdown
|
|
206
|
+
snapshot_list() # list saved snapshots with deltas
|
|
207
|
+
snapshot_show("latest") # inspect the latest snapshot
|
|
208
|
+
snapshot_diff("<key_a>", "<key_b>") # compare two snapshots
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
### Python API
|
|
212
|
+
|
|
213
|
+
```python
|
|
214
|
+
from pycode_kg import PyCodeKG
|
|
215
|
+
|
|
216
|
+
kg = PyCodeKG(repo_root="/path/to/repo")
|
|
217
|
+
kg.build(wipe=True)
|
|
218
|
+
|
|
219
|
+
result = kg.query("database connection setup", k=8, hop=1)
|
|
220
|
+
for node in result.nodes:
|
|
221
|
+
print(node["id"], node["name"])
|
|
222
|
+
|
|
223
|
+
pack = kg.pack("authentication flow")
|
|
224
|
+
pack.save("context.md")
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
## Architecture
|
|
230
|
+
|
|
231
|
+
<p align="center">
|
|
232
|
+
<img src="assets/codeKG_arch_square-web.jpg" alt="PyCodeKG architecture workflow" width="600"/>
|
|
233
|
+
</p>
|
|
234
|
+
|
|
235
|
+
```
|
|
236
|
+
Repository
|
|
237
|
+
↓
|
|
238
|
+
AST parsing — Pass 1: structure, Pass 2: calls, Pass 3: data-flow
|
|
239
|
+
↓
|
|
240
|
+
SQLite graph — nodes + edges
|
|
241
|
+
↓
|
|
242
|
+
Symbol resolution — RESOLVES_TO edges (sym: stubs → fn:/cls: defs)
|
|
243
|
+
↓
|
|
244
|
+
Vector indexing — LanceDB
|
|
245
|
+
↓
|
|
246
|
+
Hybrid query — semantic + graph
|
|
247
|
+
↓
|
|
248
|
+
Ranking + deduplication
|
|
249
|
+
↓
|
|
250
|
+
├──▶ Streamlit web app
|
|
251
|
+
└──▶ MCP server tools
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
The five design principles:
|
|
255
|
+
|
|
256
|
+
1. **Structure is authoritative** — The AST-derived graph is the source of truth.
|
|
257
|
+
2. **Semantics accelerate, never decide** — Embeddings seed and rank retrieval but never invent structure.
|
|
258
|
+
3. **Everything is traceable** — Nodes and edges map to concrete files and line numbers.
|
|
259
|
+
4. **Determinism over heuristics** — Identical input yields identical output.
|
|
260
|
+
5. **Composable artifacts** — SQLite for structure, LanceDB for vectors, Markdown/JSON for consumption.
|
|
261
|
+
|
|
262
|
+
→ **Full architecture documentation:** [docs/Architecture.md](docs/Architecture.md)
|
|
263
|
+
|
|
264
|
+
---
|
|
265
|
+
|
|
266
|
+
## Contribution Checklist
|
|
267
|
+
|
|
268
|
+
When changing MCP tools in `src/pycode_kg/mcp_server.py` (signature, params, defaults, or behavior), update all three in the same commit:
|
|
269
|
+
|
|
270
|
+
- Module docstring `Tools` list at the top of `src/pycode_kg/mcp_server.py`
|
|
271
|
+
- `mcp = FastMCP(..., instructions=(...))` tool descriptions in `src/pycode_kg/mcp_server.py`
|
|
272
|
+
- The runtime tool implementation and `:param:` docstrings
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
## Citation
|
|
277
|
+
|
|
278
|
+
If you use PyCodeKG in your research or project, please cite it:
|
|
279
|
+
|
|
280
|
+
[](https://zenodo.org/badge/latestdoi/1202379010)
|
|
281
|
+
|
|
282
|
+
**APA**
|
|
283
|
+
|
|
284
|
+
> Suchanek, E. G. (2026). *PyCodeKG: Semantic Knowledge Graph for Python Codebases* (Version 0.15.0) [Software]. Flux-Frontiers. https://doi.org/10.5281/zenodo.PLACEHOLDER
|
|
285
|
+
|
|
286
|
+
**BibTeX**
|
|
287
|
+
|
|
288
|
+
```bibtex
|
|
289
|
+
@software{suchanek_pycode_kg,
|
|
290
|
+
author = {Suchanek, Eric G.},
|
|
291
|
+
title = {{PyCodeKG}: Semantic Knowledge Graph for Python Codebases},
|
|
292
|
+
version = {0.15.0},
|
|
293
|
+
year = {2026},
|
|
294
|
+
publisher = {Flux-Frontiers},
|
|
295
|
+
url = {https://github.com/Flux-Frontiers/pycode_kg},
|
|
296
|
+
doi = {10.5281/zenodo.PLACEHOLDER},
|
|
297
|
+
}
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
302
|
+
## License
|
|
303
|
+
|
|
304
|
+
[Elastic License 2.0](https://www.elastic.co/licensing/elastic-license) — see [LICENSE](LICENSE).
|
|
305
|
+
|