ctxgraph 0.1.0__tar.gz

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.
Files changed (47) hide show
  1. ctxgraph-0.1.0/PKG-INFO +448 -0
  2. ctxgraph-0.1.0/README.md +419 -0
  3. ctxgraph-0.1.0/pyproject.toml +59 -0
  4. ctxgraph-0.1.0/setup.cfg +4 -0
  5. ctxgraph-0.1.0/src/ctxgraph/__init__.py +0 -0
  6. ctxgraph-0.1.0/src/ctxgraph/analyzers/__init__.py +0 -0
  7. ctxgraph-0.1.0/src/ctxgraph/analyzers/python/__init__.py +0 -0
  8. ctxgraph-0.1.0/src/ctxgraph/analyzers/python/importer.py +140 -0
  9. ctxgraph-0.1.0/src/ctxgraph/analyzers/python/semantic.py +75 -0
  10. ctxgraph-0.1.0/src/ctxgraph/analyzers/python/symbols.py +221 -0
  11. ctxgraph-0.1.0/src/ctxgraph/capsule/__init__.py +0 -0
  12. ctxgraph-0.1.0/src/ctxgraph/capsule/renderer.py +183 -0
  13. ctxgraph-0.1.0/src/ctxgraph/cli/__init__.py +0 -0
  14. ctxgraph-0.1.0/src/ctxgraph/cli/main.py +253 -0
  15. ctxgraph-0.1.0/src/ctxgraph/clients/__init__.py +0 -0
  16. ctxgraph-0.1.0/src/ctxgraph/clients/models.py +44 -0
  17. ctxgraph-0.1.0/src/ctxgraph/config/__init__.py +0 -0
  18. ctxgraph-0.1.0/src/ctxgraph/config/providers.py +150 -0
  19. ctxgraph-0.1.0/src/ctxgraph/config/settings.py +232 -0
  20. ctxgraph-0.1.0/src/ctxgraph/exclude/__init__.py +0 -0
  21. ctxgraph-0.1.0/src/ctxgraph/exclude/patterns.py +75 -0
  22. ctxgraph-0.1.0/src/ctxgraph/graph/__init__.py +0 -0
  23. ctxgraph-0.1.0/src/ctxgraph/graph/builder.py +76 -0
  24. ctxgraph-0.1.0/src/ctxgraph/graph/models.py +83 -0
  25. ctxgraph-0.1.0/src/ctxgraph/graph/query.py +135 -0
  26. ctxgraph-0.1.0/src/ctxgraph/graph/storage.py +224 -0
  27. ctxgraph-0.1.0/src/ctxgraph/mcp/__init__.py +0 -0
  28. ctxgraph-0.1.0/src/ctxgraph/mcp/server.py +216 -0
  29. ctxgraph-0.1.0/src/ctxgraph/view/__init__.py +0 -0
  30. ctxgraph-0.1.0/src/ctxgraph/view/visualizer.py +282 -0
  31. ctxgraph-0.1.0/src/ctxgraph/wrapper/__init__.py +0 -0
  32. ctxgraph-0.1.0/src/ctxgraph/wrapper/claude.py +139 -0
  33. ctxgraph-0.1.0/src/ctxgraph.egg-info/PKG-INFO +448 -0
  34. ctxgraph-0.1.0/src/ctxgraph.egg-info/SOURCES.txt +45 -0
  35. ctxgraph-0.1.0/src/ctxgraph.egg-info/dependency_links.txt +1 -0
  36. ctxgraph-0.1.0/src/ctxgraph.egg-info/entry_points.txt +3 -0
  37. ctxgraph-0.1.0/src/ctxgraph.egg-info/requires.txt +11 -0
  38. ctxgraph-0.1.0/src/ctxgraph.egg-info/top_level.txt +1 -0
  39. ctxgraph-0.1.0/tests/test_analyzers.py +170 -0
  40. ctxgraph-0.1.0/tests/test_benchmark.py +130 -0
  41. ctxgraph-0.1.0/tests/test_capsule.py +56 -0
  42. ctxgraph-0.1.0/tests/test_config.py +85 -0
  43. ctxgraph-0.1.0/tests/test_integration.py +68 -0
  44. ctxgraph-0.1.0/tests/test_model_mode.py +31 -0
  45. ctxgraph-0.1.0/tests/test_models.py +92 -0
  46. ctxgraph-0.1.0/tests/test_query.py +69 -0
  47. ctxgraph-0.1.0/tests/test_storage.py +100 -0
@@ -0,0 +1,448 @@
1
+ Metadata-Version: 2.4
2
+ Name: ctxgraph
3
+ Version: 0.1.0
4
+ Summary: Context graph engine for AI coding assistants — build knowledge graphs, generate context capsules
5
+ Author: ctxgraph contributors
6
+ License: MIT
7
+ Keywords: code-graph,knowledge-graph,claude,code-analysis,ai-context
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
16
+ Classifier: Topic :: Software Development :: Code Generators
17
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
18
+ Requires-Python: >=3.10
19
+ Description-Content-Type: text/markdown
20
+ Requires-Dist: typer>=0.9
21
+ Requires-Dist: rich>=13.0
22
+ Provides-Extra: mcp
23
+ Requires-Dist: mcp>=1.0.0; extra == "mcp"
24
+ Requires-Dist: anyio>=4.0; extra == "mcp"
25
+ Provides-Extra: dev
26
+ Requires-Dist: pytest>=7.0; extra == "dev"
27
+ Requires-Dist: pytest-cov>=4.0; extra == "dev"
28
+ Requires-Dist: ruff>=0.1.0; extra == "dev"
29
+
30
+ # ctxgraph
31
+
32
+ **Context graph engine for AI coding assistants.** Builds a multi-layer knowledge graph from your Python codebase and generates token-efficient context capsules for Claude, OpenAI, Ollama, and other AI tools.
33
+
34
+ ```bash
35
+ pip install ctxgraph
36
+
37
+ # Build knowledge graph
38
+ ctx build
39
+
40
+ # Generate context for a task (92-99% fewer tokens than raw code)
41
+ ctx capsule "fix JWT expiry in auth module"
42
+
43
+ # Launch Claude with context pre-loaded
44
+ ccg "fix the login redirect bug"
45
+
46
+ # Visualize your codebase
47
+ ctx view
48
+
49
+ # Search the graph
50
+ ctx query "auth jwt validate"
51
+ ```
52
+
53
+ ---
54
+
55
+ ## How It Works
56
+
57
+ ctxgraph analyzes your Python codebase using static AST analysis to build a **multi-layer knowledge graph** in SQLite:
58
+
59
+ ```
60
+ Repository (.py files)
61
+
62
+
63
+ ┌──────────────────────────────────────────────┐
64
+ │ ctx build │
65
+ │ │
66
+ │ 1. importer.py (AST) │
67
+ │ └── Extract imports → file-to-file edges │
68
+ │ │
69
+ │ 2. symbols.py (AST) │
70
+ │ └── Extract classes, functions, methods │
71
+ │ calls, inheritance → symbol nodes │
72
+ │ │
73
+ │ 3. semantic.py (docstrings) │
74
+ │ └── Extract summaries → node enrichment │
75
+ │ │
76
+ │ Store: SQLite (nodes + edges tables) │
77
+ └──────────────────┬───────────────────────────┘
78
+
79
+
80
+ ┌──────────────────────────────────────────────┐
81
+ │ Context Capsule Generation │
82
+ │ │
83
+ │ 1. Tokenize query → keyword search │
84
+ │ 2. Score: name matches (2x), text (0.5x) │
85
+ │ 3. BFS neighborhood expansion (depth=2) │
86
+ │ 4. Render token-efficient DSL format │
87
+ └──────────────────────────────────────────────┘
88
+ ```
89
+
90
+ ### Architecture
91
+
92
+ ```
93
+ ┌─────────┐ ┌──────────────┐ ┌──────────────┐
94
+ │ CLI │───▶│ Analyzers │───▶│ SQLite DB │
95
+ │ typer │ │ AST-based │ │ .ctxgraph/ │
96
+ └────┬────┘ └──────────────┘ └──────┬───────┘
97
+ │ │
98
+ ├── ctx build ───────────────────────▶│ Graph build
99
+ │ │
100
+ ├── ctx capsule ◀─────────────────────│ Query + BFS
101
+ │ │
102
+ ├── ctx query ◀──────────────────────│ Search
103
+ │ │
104
+ ├── ctx view ◀───────────────────────│ D3.js viz
105
+ │ │
106
+ ├── ctx serve ◀──────────────────────│ MCP server
107
+ │ │
108
+ └── ccg wrapper ───▶ Claude Code ────┘ AI tool
109
+ ```
110
+
111
+ ---
112
+
113
+ ## Token-Efficient DSL Format
114
+
115
+ ctxgraph uses a custom DSL format instead of JSON, saving **~4.7× tokens** on average:
116
+
117
+ ```
118
+ JSON: 426 tokens DSL: 143 tokens
119
+ ───── ────
120
+ { [CTX]calculator expression parsing
121
+ "nodes": [
122
+ { [F]calc/parser.py
123
+ "id": "file:calc/parser.py", D:Tokenize and parse math expressions
124
+ "type": "file", S:tokenize, parse, Expression
125
+ "name": "parser.py", [F]calc/core.py
126
+ "path": "calc/parser.py", D:Core math operations
127
+ "summary": "Tokenize..." [C]Calculator
128
+ }, D:Main calculator class
129
+ ...
130
+ ], [DEP]
131
+ "edges": [...] parser.py → core.py
132
+ } parser.py → plugins.py
133
+ ```
134
+
135
+ ---
136
+
137
+ ## Commands
138
+
139
+ ### `ctx build` — Build knowledge graph
140
+
141
+ ```bash
142
+ # Current directory
143
+ ctx build
144
+
145
+ # Specific repo
146
+ ctx build /path/to/project
147
+
148
+ # Custom exclude patterns
149
+ ctx build --exclude "vendor/*" --exclude "legacy/*"
150
+ ```
151
+
152
+ ### `ctx capsule <query>` — Generate context
153
+
154
+ ```bash
155
+ # Balanced (default: 20 nodes, depth 2)
156
+ ctx capsule "fix JWT token validation"
157
+
158
+ # Fast (10 nodes, depth 1)
159
+ ctx capsule "fix JWT token validation" --mode fast
160
+
161
+ # Deep (40 nodes, depth 3)
162
+ ctx capsule "fix JWT token validation" --mode deep
163
+
164
+ # Project architecture overview
165
+ ctx capsule --overview
166
+ ```
167
+
168
+ ### `ctx query <search>` — Search graph
169
+
170
+ ```bash
171
+ ctx query "user auth"
172
+ ctx query "payment gateway" --mode deep
173
+ ```
174
+
175
+ Returns ranked nodes with relevance scores.
176
+
177
+ ### `ctx view` — Visualize graph
178
+
179
+ ```bash
180
+ ctx view
181
+ ctx view --output graph.html
182
+ ctx view --port 8080 --no-open
183
+ ```
184
+
185
+ Generates interactive D3.js force-directed HTML (zero JS toolchain).
186
+
187
+ ### `ctx serve` — MCP server
188
+
189
+ ```bash
190
+ pip install ctxgraph[mcp]
191
+ ctx serve
192
+ ```
193
+
194
+ Starts an MCP protocol server. Claude Desktop config:
195
+
196
+ ```json
197
+ {
198
+ "mcpServers": {
199
+ "ctxgraph": {
200
+ "command": "ctx",
201
+ "args": ["serve"]
202
+ }
203
+ }
204
+ }
205
+ ```
206
+
207
+ Tools: `search_graph`, `get_context_capsule`, `get_file_dependencies`, `get_project_overview`.
208
+
209
+ ### `ctx info` — Graph statistics
210
+
211
+ ```bash
212
+ ctx info
213
+ # ┌────────────────────┬───────┐
214
+ # │ Total Nodes │ 1090 │
215
+ # │ Total Edges │ 1565 │
216
+ # │ files │ 147 │
217
+ # │ classes │ 45 │
218
+ # │ functions │ 312 │
219
+ # └────────────────────┴───────┘
220
+ ```
221
+
222
+ ---
223
+
224
+ ## Claude Wrapper (`ccg`)
225
+
226
+ ```bash
227
+ # Single-shot
228
+ ccg "fix the JWT expiry bug in auth module"
229
+
230
+ # Interactive session with context pre-loaded
231
+ ccg --chat "refactor the payment flow"
232
+
233
+ # Project overview
234
+ ccg --overview
235
+
236
+ # With specific mode
237
+ ccg --mode deep "redesign the database schema"
238
+ ```
239
+
240
+ ---
241
+
242
+ ## Modes
243
+
244
+ | Mode | Max Nodes | BFS Depth | Use Case |
245
+ |------|-----------|-----------|----------|
246
+ | `fast` | 10 | 1 | Quick questions, small fixes |
247
+ | `balanced` (default) | 20 | 2 | General development |
248
+ | `deep` | 40 | 3 | Complex refactoring, architecture |
249
+
250
+ ---
251
+
252
+ ## Configuration
253
+
254
+ `.ctxgraph/config.toml` (or `.ctxgraph/config.json`):
255
+
256
+ ```toml
257
+ [graph]
258
+ exclude = ["legacy/*", "vendor/*"]
259
+
260
+ [ai]
261
+ provider = "ollama" # ollama, claude, openai, custom
262
+ model = "qwen2.5-coder:7b"
263
+ endpoint = "http://localhost:11434"
264
+
265
+ [context]
266
+ mode = "balanced"
267
+ max_nodes = 20
268
+ max_depth = 2
269
+ ```
270
+
271
+ Environment overrides:
272
+
273
+ | Variable | Overrides |
274
+ |----------|-----------|
275
+ | `CTXGRAPH_PROVIDER` | `ai.provider` |
276
+ | `CTXGRAPH_MODEL` | `ai.model` |
277
+ | `CTXGRAPH_ENDPOINT` | `ai.endpoint` |
278
+ | `ANTHROPIC_API_KEY` | Claude API key |
279
+ | `OPENAI_API_KEY` | OpenAI API key |
280
+
281
+ ### Provider Switching
282
+
283
+ ```bash
284
+ # Ollama (default, no API key)
285
+ ctx capsule "query"
286
+
287
+ # Claude
288
+ CTXGRAPH_PROVIDER=claude CTXGRAPH_MODEL=claude-sonnet-4-20250514 ctx capsule "query"
289
+
290
+ # OpenAI
291
+ CTXGRAPH_PROVIDER=openai CTXGRAPH_MODEL=gpt-4o ctx capsule "query"
292
+
293
+ # Custom (OpenAI-compatible API)
294
+ CTXGRAPH_PROVIDER=custom CTXGRAPH_ENDPOINT=http://my-api/v1 ctx capsule "query"
295
+ ```
296
+
297
+ ---
298
+
299
+ ## Benchmark Results
300
+
301
+ ### Methodology
302
+
303
+ All benchmarks measure **token count** (whitespace-split word count) as a reproducible proxy for LLM token usage.
304
+
305
+ #### Token Efficiency (Capsule vs Raw Files)
306
+
307
+ **Baseline ("without graph"):** All `.py` files in the project directory (excluding build artifacts like `__pycache__`, `.git`, `venv`). This represents what an AI assistant would need to read to understand the codebase without ctxgraph.
308
+
309
+ **Measurement:** For each project, we build the graph once, then run multiple queries across all three modes (fast/balanced/deep). The capsule token count is averaged across queries and compared against the raw file token count.
310
+
311
+ **Savings formula:** `(1 - capsule_tokens / raw_tokens) × 100`
312
+
313
+ | Project | Files | Raw Tokens | Avg Capsule Tokens | **Avg Saved** | Build Time |
314
+ |---------|-------|-----------|-------------------|:------------:|:----------:|
315
+ | tiny_app | 7 | 1,558 | ~112 | **92.8%** | ~82ms |
316
+ | web_api | 23 | 6,567 | ~136 | **97.9%** | ~474ms |
317
+ | microsvc | 22 | 10,587 | ~63 | **99.4%** | ~916ms |
318
+ | dataflow | 35 | ~12,500 | ~78 | **~99.4%** | ~560ms |
319
+
320
+ **Overall: 97.0% average token savings** across all 4 projects and 42 benchmark runs.
321
+
322
+ #### DSL vs JSON Format Efficiency
323
+
324
+ **Methodology:** For the same set of nodes and edges, we render both a DSL capsule and an equivalent JSON structure. We compare token counts across both representations.
325
+
326
+ | Project | Query | DSL Tokens | JSON Tokens | **Ratio** |
327
+ |---------|-------|:----------:|:-----------:|:---------:|
328
+ | tiny_app | calculator | 147 | 434 | **3.0×** |
329
+ | tiny_app | parse expression | 137 | 451 | **3.3×** |
330
+ | web_api | user management | 126 | 403 | **3.2×** |
331
+ | web_api | JWT auth login | 136 | 308 | **2.3×** |
332
+ | microsvc | auth service | 32 | 219 | **6.8×** |
333
+ | microsvc | payment billing | 42 | 395 | **9.4×** |
334
+
335
+ **Overall: 4.7× fewer tokens** than equivalent JSON representation.
336
+
337
+ #### Ollama Comparison (With vs Without Graph)
338
+
339
+ **Methodology:** We compare LLM answer quality (keyword recall coverage) with and without ctxgraph context. For each query:
340
+ 1. **Without graph:** Ask Ollama the question directly (no code context)
341
+ 2. **With graph:** Build a context capsule from the graph, prepend it to the same question
342
+ 3. **Coverage score:** % of predefined keywords (file names, concepts) that appear in the answer
343
+
344
+ | Query | Coverage (no ctx) | Coverage (with ctx) | Δ |
345
+ |-------|:-----------------:|:-------------------:|:-:|
346
+ | Calculator expression parsing (tiny_app) | 100% | 100% | — |
347
+ | Plugin registration system (tiny_app) | 33% | **100%** | **+67pp** |
348
+ | JWT authentication (web_api) | 75% | **100%** | **+25pp** |
349
+ | Middleware pipeline (web_api) | 100% | 100% | — |
350
+ | Circuit breaker (microsvc) | 75% | 75% | — |
351
+ | Services & communication (microsvc) | 50% | **100%** | **+50pp** |
352
+ | PipelineBuilder pattern (dataflow) | 100% | 75% | -25pp |
353
+ | Processor registration (dataflow) | 33% | **67%** | **+34pp** |
354
+ | Event bus & error handling (dataflow) | 100% | 100% | — |
355
+
356
+ **Results:** Average coverage improvement of **+16.7pp**. Coverage improved on **4/9** queries (44%). For project-specific questions (plugin system, services, processors), the graph provides concrete file and class names the model cannot guess from training data alone.
357
+
358
+ > **Note:** The one regression (PipelineBuilder) occurred because without context the model gave a generic answer matching all keywords, while with context it focused on the actual codebase implementation and missed the "scheduler" keyword — a more honest and useful answer for the developer.
359
+
360
+ ---
361
+
362
+ ## Examples
363
+
364
+ ### Debug a failing test
365
+
366
+ ```bash
367
+ ctx build
368
+ ctx capsule "test_user_login is failing with auth error" --mode deep
369
+ # Output →
370
+ # [F]tests/test_auth.py
371
+ # [F]src/auth/login.py
372
+ # [C]AuthService
373
+ # [DEP] auth/login.py → core/database.py, auth/session.py
374
+ ```
375
+
376
+ ### Understand a new codebase
377
+
378
+ ```bash
379
+ ctx capsule "project architecture" --overview
380
+ ccg --chat "explain the overall architecture and data flow"
381
+ ```
382
+
383
+ ### Refactor across modules
384
+
385
+ ```bash
386
+ ctx capsule "extract payment processing into separate module" --mode deep
387
+ ```
388
+
389
+ ---
390
+
391
+ ## Development
392
+
393
+ ```bash
394
+ git clone https://github.com/shashi3070/ctxgraph.git
395
+ cd ctxgraph
396
+
397
+ # Install with dev dependencies
398
+ pip install -e ".[dev]"
399
+
400
+ # Run tests
401
+ pytest
402
+
403
+ # Run benchmarks
404
+ python benchmarks/run_benchmarks.py
405
+
406
+ # Ollama comparison (requires local Ollama)
407
+ python benchmarks/run_ollama_comparison.py
408
+ ```
409
+
410
+ ### Project Structure
411
+
412
+ ```
413
+ src/ctxgraph/
414
+ ├── cli/main.py — Typer CLI (6 commands)
415
+ ├── graph/
416
+ │ ├── models.py — Node, Edge, Graph dataclasses
417
+ │ ├── storage.py — SQLite persistence
418
+ │ ├── builder.py — Graph build orchestrator
419
+ │ └── query.py — Tokenizer + BFS + relevance scoring
420
+ ├── capsule/renderer.py — DSL context generation
421
+ ├── analyzers/python/
422
+ │ ├── importer.py — AST import extraction
423
+ │ ├── symbols.py — AST class/function/method analysis
424
+ │ └── semantic.py — Docstring summarization
425
+ ├── config/
426
+ │ ├── settings.py — TOML/JSON/env config loading
427
+ │ └── providers.py — Ollama, Claude, OpenAI API clients
428
+ ├── clients/models.py — Model mode enum (fast/balanced/deep)
429
+ ├── exclude/patterns.py — Exclusion pattern matching
430
+ ├── view/visualizer.py — D3.js HTML graph generator
431
+ ├── wrapper/claude.py — ccg Claude wrapper
432
+ └── mcp/server.py — MCP protocol server
433
+ ```
434
+
435
+ ---
436
+
437
+ ## Known Limitations
438
+
439
+ - **Python-only analysis** — other languages get file-level nodes only
440
+ - **Keyword-based search** — no semantic/embedding matching (planned)
441
+ - **No incremental rebuild** — full rebuild on every `ctx build` (planned)
442
+ - **MCP server** — stdio mode only, SSE not yet supported
443
+
444
+ ---
445
+
446
+ ## License
447
+
448
+ MIT