ctxgraph-code 0.2.1__tar.gz → 0.4.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 (39) hide show
  1. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/PKG-INFO +133 -34
  2. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/README.md +132 -33
  3. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/pyproject.toml +1 -1
  4. ctxgraph_code-0.4.0/src/ctxgraph_code/__init__.py +1 -0
  5. ctxgraph_code-0.4.0/src/ctxgraph_code/analyzers/treesitter/__init__.py +4 -0
  6. ctxgraph_code-0.4.0/src/ctxgraph_code/analyzers/treesitter/analyzer.py +249 -0
  7. ctxgraph_code-0.4.0/src/ctxgraph_code/analyzers/treesitter/languages.py +229 -0
  8. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code/cli.py +1065 -742
  9. ctxgraph_code-0.4.0/src/ctxgraph_code/config/build_status.py +93 -0
  10. ctxgraph_code-0.4.0/src/ctxgraph_code/config/hooks.py +165 -0
  11. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code/config/settings.py +2 -2
  12. ctxgraph_code-0.4.0/src/ctxgraph_code/graph/builder.py +381 -0
  13. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code/graph/storage.py +44 -30
  14. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code.egg-info/PKG-INFO +133 -34
  15. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code.egg-info/SOURCES.txt +5 -0
  16. ctxgraph_code-0.2.1/src/ctxgraph_code/graph/builder.py +0 -204
  17. ctxgraph_code-0.2.1/src/ctxgraph_code/view/__init__.py +0 -0
  18. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/setup.cfg +0 -0
  19. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code/__main__.py +0 -0
  20. {ctxgraph_code-0.2.1/src/ctxgraph_code → ctxgraph_code-0.4.0/src/ctxgraph_code/analyzers}/__init__.py +0 -0
  21. {ctxgraph_code-0.2.1/src/ctxgraph_code/analyzers → ctxgraph_code-0.4.0/src/ctxgraph_code/analyzers/python}/__init__.py +0 -0
  22. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code/analyzers/python/importer.py +0 -0
  23. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code/analyzers/python/semantic.py +0 -0
  24. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code/analyzers/python/symbols.py +0 -0
  25. {ctxgraph_code-0.2.1/src/ctxgraph_code/analyzers/python → ctxgraph_code-0.4.0/src/ctxgraph_code/config}/__init__.py +0 -0
  26. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code/config/global_paths.py +0 -0
  27. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code/config/init.py +0 -0
  28. {ctxgraph_code-0.2.1/src/ctxgraph_code/config → ctxgraph_code-0.4.0/src/ctxgraph_code/exclude}/__init__.py +0 -0
  29. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code/exclude/patterns.py +0 -0
  30. {ctxgraph_code-0.2.1/src/ctxgraph_code/exclude → ctxgraph_code-0.4.0/src/ctxgraph_code/graph}/__init__.py +0 -0
  31. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code/graph/models.py +0 -0
  32. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code/graph/query.py +0 -0
  33. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code/render.py +0 -0
  34. {ctxgraph_code-0.2.1/src/ctxgraph_code/graph → ctxgraph_code-0.4.0/src/ctxgraph_code/view}/__init__.py +0 -0
  35. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code/view/visualizer.py +0 -0
  36. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code.egg-info/dependency_links.txt +0 -0
  37. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code.egg-info/entry_points.txt +0 -0
  38. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code.egg-info/requires.txt +0 -0
  39. {ctxgraph_code-0.2.1 → ctxgraph_code-0.4.0}/src/ctxgraph_code.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ctxgraph-code
3
- Version: 0.2.1
3
+ Version: 0.4.0
4
4
  Summary: Code knowledge graph for Claude Code. Build a relationship graph of your Python codebase and query it during coding sessions.
5
5
  Author: ctxgraph-code contributors
6
6
  License: MIT
@@ -23,7 +23,7 @@ Requires-Dist: pytest>=7.0; extra == "dev"
23
23
 
24
24
  # ctxgraph-code
25
25
 
26
- **Code knowledge graph for Claude Code.** Build a relationship graph of your Python codebase so Claude Code understands imports, class hierarchies, function calls, and cross-file dependencies without reading every file.
26
+ **Code knowledge graph for Claude Code.** Build a relationship graph of your codebase (Python, JavaScript, TypeScript, C, Go, Rust, and more) so Claude Code understands imports, class hierarchies, function calls, and cross-file dependencies without reading every file.
27
27
 
28
28
  ```bash
29
29
  pip install ctxgraph-code
@@ -76,15 +76,23 @@ Interactive walkthrough — prompts for:
76
76
 
77
77
  Does everything in one step:
78
78
  1. Creates `.ctxgraph/config.toml` with your chosen extensions and excludes
79
- 2. Builds the knowledge graph from all matching files
80
- 3. Creates `.claude/commands/ctxgraph-code.md` with instructions for Claude Code
79
+ 2. Installs the `/ctxgraph-code` slash command globally (works in every Claude Code session)
80
+ 3. Builds the knowledge graph from all matching files
81
81
 
82
- Non-interactive mode (skip prompts):
82
+ Non-interactive mode:
83
83
  ```bash
84
84
  ctxgraph-code setup --extensions .py,.js,.ts --exclude tests/,examples/
85
85
  ctxgraph-code setup -y # all defaults
86
86
  ```
87
87
 
88
+ Options:
89
+ - `--project-slash` — install slash command in project's `.claude/` instead of globally
90
+ - `--background` / `-b` — launch build in background and exit immediately (check with `build-status`)
91
+ - `--jobs` / `-j` — number of parallel workers (default: CPU count)
92
+ - `--incremental` / `-i` — only rebuild files that changed since last build
93
+ - `--verbose` / `-v` — show per-file progress
94
+ - `--no-summary` — skip docstring extraction for faster builds
95
+
88
96
  ### `init`
89
97
 
90
98
  ```bash
@@ -103,24 +111,27 @@ ctxgraph-code build --exclude tests/ --exclude *.generated.py
103
111
 
104
112
  Scans all matching files in the project, runs AST analysis. Extensions are read from config (`.py` by default, or whatever was set in `setup`).
105
113
 
106
- - **Imports**: which files import other files
107
- - **Class definitions**: class names, base classes, methods
108
- - **Function definitions**: function names, arguments
109
- - **Function calls**: which functions call which (within the project)
110
- - **Docstrings**: extracted as node summaries
114
+ **By default, builds a separate graph per top-level directory** (e.g., `src/`, `api/`, `tests/`) in parallel. This keeps each graph small and fast to query. Use `--dir <name>` on query commands to select one, or let it auto-detect from file paths.
115
+
116
+ - `--all` / `-a` build a single combined graph instead of per-directory
117
+ - `--jobs` / `-j` number of parallel workers (default: CPU count)
118
+ - `--incremental` / `-i` only rebuild files that changed since last build
119
+ - `--verbose` / `-v` — show per-file progress
120
+ - `--no-summary` — skip docstring extraction for faster builds
111
121
 
112
- Stores the result in `.ctxgraph/graph.db`.
122
+ Stores graphs in `.ctxgraph/graphs/<dir>.db` (per-directory) or `.ctxgraph/graph.db` (combined).
113
123
 
114
- > The graph is a **static snapshot**. If code changes, run `ctxgraph-code build` again to refresh. Claude Code will also rebuild when it detects the graph is stale.
124
+ > The graph is a **static snapshot**. If code changes, run `ctxgraph-code build` again to refresh. Use `--incremental` to only reprocess changed files.
115
125
 
116
126
  ### `query`
117
127
 
118
128
  ```bash
119
129
  ctxgraph-code query "user authentication"
120
130
  ctxgraph-code query "database connection" --max 20
131
+ ctxgraph-code query "api routes" --dir src
121
132
  ```
122
133
 
123
- Searches the graph by relevance scoring (name matches > summary matches > path matches) and expands to neighboring nodes via BFS up to depth 2.
134
+ Searches the graph by relevance scoring (name matches > summary matches > path matches) and expands to neighboring nodes via BFS up to depth 2. Use `--dir` to scope to a specific per-directory graph.
124
135
 
125
136
  ### `deps`
126
137
 
@@ -128,7 +139,7 @@ Searches the graph by relevance scoring (name matches > summary matches > path m
128
139
  ctxgraph-code deps src/api/routes.py
129
140
  ```
130
141
 
131
- Shows all relationships for a file: imports, imported-by, function calls, class definitions.
142
+ Shows all relationships for a file: imports, imported-by, function calls, class definitions. Auto-detects the per-directory graph from the file path.
132
143
 
133
144
  ### `usedby`
134
145
 
@@ -142,9 +153,10 @@ Shows every file that imports or calls something in the given file. Useful to un
142
153
 
143
154
  ```bash
144
155
  ctxgraph-code overview
156
+ ctxgraph-code overview --dir src
145
157
  ```
146
158
 
147
- Prints the project structure: every file with its summary and top-level symbols.
159
+ Prints the project structure: every file with its summary and top-level symbols. Use `--dir` to scope to a per-directory graph.
148
160
 
149
161
  ### `symbols`
150
162
 
@@ -181,10 +193,59 @@ Use `--tree` for a terminal-friendly text view of the directory hierarchy with s
181
193
 
182
194
  ```bash
183
195
  ctxgraph-code info
196
+ ctxgraph-code info --dir src
184
197
  ```
185
198
 
186
199
  Shows graph statistics: node/edge counts, type distribution, build time.
187
200
 
201
+ ### `install-slash`
202
+
203
+ ```bash
204
+ ctxgraph-code install-slash
205
+ ctxgraph-code install-slash --project-slash # project-local instead of global
206
+ ```
207
+
208
+ Install or update the `/ctxgraph-code` slash command for Claude Code. By default installs globally so it works in every Claude Code session. Use `--project-slash` to install in the project's `.claude/commands/` directory instead.
209
+
210
+ ### `build-status`
211
+
212
+ ```bash
213
+ ctxgraph-code build-status
214
+ ```
215
+
216
+ Check whether a background build (`ctxgraph-code setup --background` or `ctxgraph-code build`) completed, failed, or is still running. Shows PID and start time for in-progress builds.
217
+
218
+ ### `probe`
219
+
220
+ ```bash
221
+ ctxgraph-code probe "database connection pool"
222
+ ctxgraph-code probe "user authentication" --max 3
223
+ ```
224
+
225
+ Searches the graph for relevant nodes **and reads the actual source code** inline. Claude gets paths + source in one command, saving 1–2 tool calls. Shows the first 40 lines of matched files with syntax highlighting.
226
+
227
+ - `--max` / `-m` — max files to probe (default: 5)
228
+ - `--context` / `-c` — lines of context around matches (default: 3)
229
+
230
+ ### `install-hooks`
231
+
232
+ ```bash
233
+ ctxgraph-code install-hooks
234
+ ctxgraph-code install-hooks --local
235
+ ```
236
+
237
+ Installs a **PreToolUse hook** in `.claude/settings.json` (global) or `.claude/settings.local.json` (project-local). On every `Bash|Glob|Grep` tool call, Claude Code automatically runs `ctxgraph-code hook-check` and injects graph context into the conversation — letting it know the graph exists, key files, and whether anything is stale.
238
+
239
+ Also prompted during `ctxgraph-code setup`.
240
+
241
+ ### `uninstall-hooks`
242
+
243
+ ```bash
244
+ ctxgraph-code uninstall-hooks
245
+ ```
246
+
247
+ Removes the PreToolUse hooks installed by `install-hooks`.
248
+
188
249
  ---
189
250
 
190
251
  ## How It Works
@@ -192,10 +253,10 @@ Shows graph statistics: node/edge counts, type distribution, build time.
192
253
  ```
193
254
  Python files ──AST──> Import/Symbol/Call analysis ──> SQLite graph.db
194
255
 
195
- Claude Code ──/ctxgraph-code──> CLI query/deps/overview <────┘
256
+ Claude Code ──/ctxgraph-code──> CLI query/deps/overview <───┘
196
257
  ```
197
258
 
198
- 1. **Build phase**: `ctxgraph-code build` parses every `.py` file with Python's `ast` module. It extracts imports, class/function definitions, function calls, and docstrings. The result is a graph of **nodes** (files, classes, functions) and **edges** (imports, defines, extends, calls) stored in SQLite.
259
+ 1. **Build phase**: `ctxgraph-code build` scans every matching file. Python files are analyzed with Python's `ast` module (fast, rich analysis with docstrings). All other languages (C, C++, JavaScript, TypeScript, Go, Rust, Java, Ruby, and more) are analyzed with **tree-sitter** via `tree-sitter-language-pack`. The result is a graph of **nodes** (files, functions, classes, structs, imports, calls) and **edges** (imports, defines, calls) stored in SQLite.
199
260
 
200
261
  2. **Query phase**: In Claude Code, the `/ctxgraph-code` slash command injects instructions into the conversation. Claude then runs `ctxgraph-code` commands as shell commands to query the graph. Claude reads the text output and reasons about it alongside its own file-reading capabilities.
201
262
 
@@ -204,8 +265,13 @@ Claude Code ──/ctxgraph-code──> CLI query/deps/overview <────
204
265
  | Node type | Example ID | Stored |
205
266
  |-----------|-----------|--------|
206
267
  | `file` | `file:src/api/routes.py` | Name, path, size, summary |
207
- | `class` | `class:src/api/routes.py::UserAPI` | Name, path, parent file, docstring, line number |
208
- | `function` | `func:src/models.py::get_user` | Name, path, parent, docstring, line number |
268
+ | `function` | `func:src/models.py::get_user` | Name, path, parent, summary, line number |
269
+ | `class` | `class:src/api/routes.py::UserAPI` | Name, path, parent file, summary, line number |
270
+ | `struct` | `struct:src/types.c::Point` | Name, path, parent file, line number |
271
+ | `interface` | `interface:src/types.ts::IUser` | Name, path, parent file, line number |
272
+ | `trait` | `trait:src/main.rs::Drawable` | Name, path, parent file, line number |
273
+ | `import` | `import:src/main.c::stdio.h` | Import path, parent file |
274
+ | `call` | `call:src/index.js::parse` | Function name, parent file, line number |
209
275
 
210
276
  | Edge relation | Meaning |
211
277
  |--------------|---------|
@@ -213,8 +279,19 @@ Claude Code ──/ctxgraph-code──> CLI query/deps/overview <────
213
279
  | `defines` | A file/class defines a class/function |
214
280
  | `extends` | Class A extends class B |
215
281
  | `calls` | Function A calls function B |
282
+ | `includes` | C/C++ file includes a header |
216
283
 
217
- Edge weights: `imports=1.0`, `defines=1.0`, `extends=0.8`, `calls=0.7`
284
+ Edge weights: `imports=1.0`, `defines=1.0`, `extends=0.8`, `calls=0.7`, `includes=0.6`
285
+
286
+ ### Tamper Detection
287
+
288
+ Every build stores **SHA256 content hashes** for every analyzed file in the graph metadata. On every query, `ctxgraph-code` re-reads the file content, recomputes the hash, and compares:
289
+
290
+ - **No match → tamper warning** shown at the bottom of every command output
291
+ - PreToolUse hook also warns inside Claude Code before any search/grep tool runs
292
+ - Use `ctxgraph-code build --incremental` to refresh only tampered files
293
+
294
+ This catches the common case where you edit a file after building the graph and Claude would otherwise see stale data.
218
295
 
219
296
  ### Query relevance scoring
220
297
 
@@ -226,25 +303,47 @@ Edge weights: `imports=1.0`, `defines=1.0`, `extends=0.8`, `calls=0.7`
226
303
 
227
304
  ---
228
305
 
229
- ## Using with Claude Code
306
+ ## Performance
307
+
308
+ `ctxgraph-code` includes several optimizations for large codebases:
309
+
310
+ | Optimization | Details |
311
+ |---|---|
312
+ | **Multi-language** | Uses Python `ast` for `.py` (fast + docstrings) and **tree-sitter** for 25+ languages (C, C++, JS, TS, Go, Rust, Java, Ruby, Kotlin, Swift, and more) |
313
+ | **Parallel builds** | Per-directory graphs build concurrently via `ThreadPoolExecutor` |
314
+ | **Multiprocessing** | Combined graphs split files across CPU cores via `multiprocessing.Pool` |
315
+ | **`--jobs`** | Control parallelism level (default: CPU count) |
316
+ | **Incremental builds** | `--incremental` caches file mtimes, only reprocesses changed files |
317
+ | **Trivial file skip** | `_quick_scan()` pre-checks before AST parse for files with no imports/classes/functions |
318
+ | **Cached excludes** | `lru_cache` on `should_exclude()` during `os.walk` |
319
+ | **Batch SQLite inserts** | `executemany` instead of per-row `INSERT` statements |
320
+ | **`--no-summary`** | Skips docstring extraction (fastest rebuilds) |
321
+ | **`--background`** | Detach build process and continue working immediately |
322
+ | **Tamper detection** | SHA256 content hashes per file; warns on tampered data in every command output |
323
+ | **PreToolUse hook** | Claude Code auto-injects graph context before Bash/Glob/Grep, saving 1-3 exploratory tool calls |
230
324
 
231
- After `ctxgraph-code setup`, Claude Code in the project directory will have the `/ctxgraph-code` slash command available.
325
+ ---
326
+
327
+ ## Using with Claude Code
232
328
 
233
- When you type `/ctxgraph-code`, Claude sees:
329
+ After `ctxgraph-code setup`, the `/ctxgraph-code` slash command is installed globally by default — it works in every Claude Code session. Claude sees:
234
330
 
235
331
  ```
236
332
  # ctxgraph-code: Code Relationship Graph
237
333
 
238
- This project has a knowledge graph at `.ctxgraph/graph.db`.
239
- The graph knows about imports, class hierarchies, and function calls.
240
-
241
- Available commands:
242
- - ctxgraph-code query "search terms" -- Find relevant files, classes, and functions
243
- - ctxgraph-code deps <path> -- Show what a file imports and what calls it
244
- - ctxgraph-code usedby <path> -- Show what depends on a file
245
- - ctxgraph-code overview -- Show the full project structure
246
- - ctxgraph-code symbols <path> -- List classes/functions defined in a file
247
- - ctxgraph-code context "task" -- Generate a focused context summary
334
+ **First time in this project?** Tell the user to run `ctxgraph-code setup`.
335
+ **Graph needs refresh?** Tell the user to run `ctxgraph-code build`.
336
+ **Available graphs:** src api tests
337
+
338
+ **Commands:**
339
+ - `ctxgraph-code query "terms"` -- Find relevant files, classes, and functions
340
+ - `ctxgraph-code probe "question"` -- Search graph and read matching source inline
341
+ - `ctxgraph-code deps <path>` -- Show what a file imports and what calls it
342
+ - `ctxgraph-code usedby <path>` -- Show what depends on a file
343
+ - `ctxgraph-code overview --dir <name>` -- Show project structure for a specific graph
344
+ - `ctxgraph-code symbols <path>` -- List classes/functions defined in a file
345
+ - `ctxgraph-code context "task"` -- Generate a focused context summary
346
+ - `ctxgraph-code view --dir <name>` -- Visualize a graph interactively
248
347
  ```
249
348
 
250
349
  Claude then uses these commands as needed during the conversation.
@@ -287,7 +386,7 @@ Built-in default exclusion patterns (always applied): `__pycache__`, `*.pyc`, `.
287
386
 
288
387
  | Feature | ctxgraph | ctxgraph-code |
289
388
  |---------|----------|---------------|
290
- | CLI commands | 9 (build, capsule, query, view, serve, info, init, ask, chat, history, skill) | 9 (init, build, query, deps, usedby, overview, symbols, context, setup, view, info) |
389
+ | CLI commands | 9+ | 16 (init, build, query, deps, usedby, overview, symbols, context, setup, view, info, install-slash, build-status, probe, install-hooks, uninstall-hooks) |
291
390
  | LLM integration | Built-in (Ollama, Claude, OpenAI, Azure) | None (delegates to Claude Code) |
292
391
  | Chat sessions | Yes | No |
293
392
  | Visualizer | D3.js HTML + SVG | D3.js HTML (`view` opens in browser, `--tree` for text) |
@@ -1,6 +1,6 @@
1
1
  # ctxgraph-code
2
2
 
3
- **Code knowledge graph for Claude Code.** Build a relationship graph of your Python codebase so Claude Code understands imports, class hierarchies, function calls, and cross-file dependencies without reading every file.
3
+ **Code knowledge graph for Claude Code.** Build a relationship graph of your codebase (Python, JavaScript, TypeScript, C, Go, Rust, and more) so Claude Code understands imports, class hierarchies, function calls, and cross-file dependencies without reading every file.
4
4
 
5
5
  ```bash
6
6
  pip install ctxgraph-code
@@ -53,15 +53,23 @@ Interactive walkthrough — prompts for:
53
53
 
54
54
  Does everything in one step:
55
55
  1. Creates `.ctxgraph/config.toml` with your chosen extensions and excludes
56
- 2. Builds the knowledge graph from all matching files
57
- 3. Creates `.claude/commands/ctxgraph-code.md` with instructions for Claude Code
56
+ 2. Installs the `/ctxgraph-code` slash command globally (works in every Claude Code session)
57
+ 3. Builds the knowledge graph from all matching files
58
58
 
59
- Non-interactive mode (skip prompts):
59
+ Non-interactive mode:
60
60
  ```bash
61
61
  ctxgraph-code setup --extensions .py,.js,.ts --exclude tests/,examples/
62
62
  ctxgraph-code setup -y # all defaults
63
63
  ```
64
64
 
65
+ Options:
66
+ - `--project-slash` — install slash command in project's `.claude/` instead of globally
67
+ - `--background` / `-b` — launch build in background and exit immediately (check with `build-status`)
68
+ - `--jobs` / `-j` — number of parallel workers (default: CPU count)
69
+ - `--incremental` / `-i` — only rebuild files that changed since last build
70
+ - `--verbose` / `-v` — show per-file progress
71
+ - `--no-summary` — skip docstring extraction for faster builds
72
+
65
73
  ### `init`
66
74
 
67
75
  ```bash
@@ -80,24 +88,27 @@ ctxgraph-code build --exclude tests/ --exclude *.generated.py
80
88
 
81
89
  Scans all matching files in the project, runs AST analysis. Extensions are read from config (`.py` by default, or whatever was set in `setup`).
82
90
 
83
- - **Imports**: which files import other files
84
- - **Class definitions**: class names, base classes, methods
85
- - **Function definitions**: function names, arguments
86
- - **Function calls**: which functions call which (within the project)
87
- - **Docstrings**: extracted as node summaries
91
+ **By default, builds a separate graph per top-level directory** (e.g., `src/`, `api/`, `tests/`) in parallel. This keeps each graph small and fast to query. Use `--dir <name>` on query commands to select one, or let it auto-detect from file paths.
92
+
93
+ - `--all` / `-a` build a single combined graph instead of per-directory
94
+ - `--jobs` / `-j` number of parallel workers (default: CPU count)
95
+ - `--incremental` / `-i` only rebuild files that changed since last build
96
+ - `--verbose` / `-v` — show per-file progress
97
+ - `--no-summary` — skip docstring extraction for faster builds
88
98
 
89
- Stores the result in `.ctxgraph/graph.db`.
99
+ Stores graphs in `.ctxgraph/graphs/<dir>.db` (per-directory) or `.ctxgraph/graph.db` (combined).
90
100
 
91
- > The graph is a **static snapshot**. If code changes, run `ctxgraph-code build` again to refresh. Claude Code will also rebuild when it detects the graph is stale.
101
+ > The graph is a **static snapshot**. If code changes, run `ctxgraph-code build` again to refresh. Use `--incremental` to only reprocess changed files.
92
102
 
93
103
  ### `query`
94
104
 
95
105
  ```bash
96
106
  ctxgraph-code query "user authentication"
97
107
  ctxgraph-code query "database connection" --max 20
108
+ ctxgraph-code query "api routes" --dir src
98
109
  ```
99
110
 
100
- Searches the graph by relevance scoring (name matches > summary matches > path matches) and expands to neighboring nodes via BFS up to depth 2.
111
+ Searches the graph by relevance scoring (name matches > summary matches > path matches) and expands to neighboring nodes via BFS up to depth 2. Use `--dir` to scope to a specific per-directory graph.
101
112
 
102
113
  ### `deps`
103
114
 
@@ -105,7 +116,7 @@ Searches the graph by relevance scoring (name matches > summary matches > path m
105
116
  ctxgraph-code deps src/api/routes.py
106
117
  ```
107
118
 
108
- Shows all relationships for a file: imports, imported-by, function calls, class definitions.
119
+ Shows all relationships for a file: imports, imported-by, function calls, class definitions. Auto-detects the per-directory graph from the file path.
109
120
 
110
121
  ### `usedby`
111
122
 
@@ -119,9 +130,10 @@ Shows every file that imports or calls something in the given file. Useful to un
119
130
 
120
131
  ```bash
121
132
  ctxgraph-code overview
133
+ ctxgraph-code overview --dir src
122
134
  ```
123
135
 
124
- Prints the project structure: every file with its summary and top-level symbols.
136
+ Prints the project structure: every file with its summary and top-level symbols. Use `--dir` to scope to a per-directory graph.
125
137
 
126
138
  ### `symbols`
127
139
 
@@ -158,10 +170,59 @@ Use `--tree` for a terminal-friendly text view of the directory hierarchy with s
158
170
 
159
171
  ```bash
160
172
  ctxgraph-code info
173
+ ctxgraph-code info --dir src
161
174
  ```
162
175
 
163
176
  Shows graph statistics: node/edge counts, type distribution, build time.
164
177
 
178
+ ### `install-slash`
179
+
180
+ ```bash
181
+ ctxgraph-code install-slash
182
+ ctxgraph-code install-slash --project-slash # project-local instead of global
183
+ ```
184
+
185
+ Install or update the `/ctxgraph-code` slash command for Claude Code. By default installs globally so it works in every Claude Code session. Use `--project-slash` to install in the project's `.claude/commands/` directory instead.
186
+
187
+ ### `build-status`
188
+
189
+ ```bash
190
+ ctxgraph-code build-status
191
+ ```
192
+
193
+ Check whether a background build (`ctxgraph-code setup --background` or `ctxgraph-code build`) completed, failed, or is still running. Shows PID and start time for in-progress builds.
194
+
195
+ ### `probe`
196
+
197
+ ```bash
198
+ ctxgraph-code probe "database connection pool"
199
+ ctxgraph-code probe "user authentication" --max 3
200
+ ```
201
+
202
+ Searches the graph for relevant nodes **and reads the actual source code** inline. Claude gets paths + source in one command, saving 1–2 tool calls. Shows the first 40 lines of matched files with syntax highlighting.
203
+
204
+ - `--max` / `-m` — max files to probe (default: 5)
205
+ - `--context` / `-c` — lines of context around matches (default: 3)
206
+
207
+ ### `install-hooks`
208
+
209
+ ```bash
210
+ ctxgraph-code install-hooks
211
+ ctxgraph-code install-hooks --local
212
+ ```
213
+
214
+ Installs a **PreToolUse hook** in `.claude/settings.json` (global) or `.claude/settings.local.json` (project-local). On every `Bash|Glob|Grep` tool call, Claude Code automatically runs `ctxgraph-code hook-check` and injects graph context into the conversation — letting it know the graph exists, key files, and whether anything is stale.
215
+
216
+ Also prompted during `ctxgraph-code setup`.
217
+
218
+ ### `uninstall-hooks`
219
+
220
+ ```bash
221
+ ctxgraph-code uninstall-hooks
222
+ ```
223
+
224
+ Removes the PreToolUse hooks installed by `install-hooks`.
225
+
165
226
  ---
166
227
 
167
228
  ## How It Works
@@ -169,10 +230,10 @@ Shows graph statistics: node/edge counts, type distribution, build time.
169
230
  ```
170
231
  Python files ──AST──> Import/Symbol/Call analysis ──> SQLite graph.db
171
232
 
172
- Claude Code ──/ctxgraph-code──> CLI query/deps/overview <────┘
233
+ Claude Code ──/ctxgraph-code──> CLI query/deps/overview <───┘
173
234
  ```
174
235
 
175
- 1. **Build phase**: `ctxgraph-code build` parses every `.py` file with Python's `ast` module. It extracts imports, class/function definitions, function calls, and docstrings. The result is a graph of **nodes** (files, classes, functions) and **edges** (imports, defines, extends, calls) stored in SQLite.
236
+ 1. **Build phase**: `ctxgraph-code build` scans every matching file. Python files are analyzed with Python's `ast` module (fast, rich analysis with docstrings). All other languages (C, C++, JavaScript, TypeScript, Go, Rust, Java, Ruby, and more) are analyzed with **tree-sitter** via `tree-sitter-language-pack`. The result is a graph of **nodes** (files, functions, classes, structs, imports, calls) and **edges** (imports, defines, calls) stored in SQLite.
176
237
 
177
238
  2. **Query phase**: In Claude Code, the `/ctxgraph-code` slash command injects instructions into the conversation. Claude then runs `ctxgraph-code` commands as shell commands to query the graph. Claude reads the text output and reasons about it alongside its own file-reading capabilities.
178
239
 
@@ -181,8 +242,13 @@ Claude Code ──/ctxgraph-code──> CLI query/deps/overview <────
181
242
  | Node type | Example ID | Stored |
182
243
  |-----------|-----------|--------|
183
244
  | `file` | `file:src/api/routes.py` | Name, path, size, summary |
184
- | `class` | `class:src/api/routes.py::UserAPI` | Name, path, parent file, docstring, line number |
185
- | `function` | `func:src/models.py::get_user` | Name, path, parent, docstring, line number |
245
+ | `function` | `func:src/models.py::get_user` | Name, path, parent, summary, line number |
246
+ | `class` | `class:src/api/routes.py::UserAPI` | Name, path, parent file, summary, line number |
247
+ | `struct` | `struct:src/types.c::Point` | Name, path, parent file, line number |
248
+ | `interface` | `interface:src/types.ts::IUser` | Name, path, parent file, line number |
249
+ | `trait` | `trait:src/main.rs::Drawable` | Name, path, parent file, line number |
250
+ | `import` | `import:src/main.c::stdio.h` | Import path, parent file |
251
+ | `call` | `call:src/index.js::parse` | Function name, parent file, line number |
186
252
 
187
253
  | Edge relation | Meaning |
188
254
  |--------------|---------|
@@ -190,8 +256,19 @@ Claude Code ──/ctxgraph-code──> CLI query/deps/overview <────
190
256
  | `defines` | A file/class defines a class/function |
191
257
  | `extends` | Class A extends class B |
192
258
  | `calls` | Function A calls function B |
259
+ | `includes` | C/C++ file includes a header |
193
260
 
194
- Edge weights: `imports=1.0`, `defines=1.0`, `extends=0.8`, `calls=0.7`
261
+ Edge weights: `imports=1.0`, `defines=1.0`, `extends=0.8`, `calls=0.7`, `includes=0.6`
262
+
263
+ ### Tamper Detection
264
+
265
+ Every build stores **SHA256 content hashes** for every analyzed file in the graph metadata. On every query, `ctxgraph-code` re-reads the file content, recomputes the hash, and compares:
266
+
267
+ - **No match → tamper warning** shown at the bottom of every command output
268
+ - PreToolUse hook also warns inside Claude Code before any search/grep tool runs
269
+ - Use `ctxgraph-code build --incremental` to refresh only tampered files
270
+
271
+ This catches the common case where you edit a file after building the graph and Claude would otherwise see stale data.
195
272
 
196
273
  ### Query relevance scoring
197
274
 
@@ -203,25 +280,47 @@ Edge weights: `imports=1.0`, `defines=1.0`, `extends=0.8`, `calls=0.7`
203
280
 
204
281
  ---
205
282
 
206
- ## Using with Claude Code
283
+ ## Performance
284
+
285
+ `ctxgraph-code` includes several optimizations for large codebases:
286
+
287
+ | Optimization | Details |
288
+ |---|---|
289
+ | **Multi-language** | Uses Python `ast` for `.py` (fast + docstrings) and **tree-sitter** for 25+ languages (C, C++, JS, TS, Go, Rust, Java, Ruby, Kotlin, Swift, and more) |
290
+ | **Parallel builds** | Per-directory graphs build concurrently via `ThreadPoolExecutor` |
291
+ | **Multiprocessing** | Combined graphs split files across CPU cores via `multiprocessing.Pool` |
292
+ | **`--jobs`** | Control parallelism level (default: CPU count) |
293
+ | **Incremental builds** | `--incremental` caches file mtimes, only reprocesses changed files |
294
+ | **Trivial file skip** | `_quick_scan()` pre-checks before AST parse for files with no imports/classes/functions |
295
+ | **Cached excludes** | `lru_cache` on `should_exclude()` during `os.walk` |
296
+ | **Batch SQLite inserts** | `executemany` instead of per-row `INSERT` statements |
297
+ | **`--no-summary`** | Skips docstring extraction (fastest rebuilds) |
298
+ | **`--background`** | Detach build process and continue working immediately |
299
+ | **Tamper detection** | SHA256 content hashes per file; warns on tampered data in every command output |
300
+ | **PreToolUse hook** | Claude Code auto-injects graph context before Bash/Glob/Grep, saving 1-3 exploratory tool calls |
207
301
 
208
- After `ctxgraph-code setup`, Claude Code in the project directory will have the `/ctxgraph-code` slash command available.
302
+ ---
303
+
304
+ ## Using with Claude Code
209
305
 
210
- When you type `/ctxgraph-code`, Claude sees:
306
+ After `ctxgraph-code setup`, the `/ctxgraph-code` slash command is installed globally by default — it works in every Claude Code session. Claude sees:
211
307
 
212
308
  ```
213
309
  # ctxgraph-code: Code Relationship Graph
214
310
 
215
- This project has a knowledge graph at `.ctxgraph/graph.db`.
216
- The graph knows about imports, class hierarchies, and function calls.
217
-
218
- Available commands:
219
- - ctxgraph-code query "search terms" -- Find relevant files, classes, and functions
220
- - ctxgraph-code deps <path> -- Show what a file imports and what calls it
221
- - ctxgraph-code usedby <path> -- Show what depends on a file
222
- - ctxgraph-code overview -- Show the full project structure
223
- - ctxgraph-code symbols <path> -- List classes/functions defined in a file
224
- - ctxgraph-code context "task" -- Generate a focused context summary
311
+ **First time in this project?** Tell the user to run `ctxgraph-code setup`.
312
+ **Graph needs refresh?** Tell the user to run `ctxgraph-code build`.
313
+ **Available graphs:** src api tests
314
+
315
+ **Commands:**
316
+ - `ctxgraph-code query "terms"` -- Find relevant files, classes, and functions
317
+ - `ctxgraph-code probe "question"` -- Search graph and read matching source inline
318
+ - `ctxgraph-code deps <path>` -- Show what a file imports and what calls it
319
+ - `ctxgraph-code usedby <path>` -- Show what depends on a file
320
+ - `ctxgraph-code overview --dir <name>` -- Show project structure for a specific graph
321
+ - `ctxgraph-code symbols <path>` -- List classes/functions defined in a file
322
+ - `ctxgraph-code context "task"` -- Generate a focused context summary
323
+ - `ctxgraph-code view --dir <name>` -- Visualize a graph interactively
225
324
  ```
226
325
 
227
326
  Claude then uses these commands as needed during the conversation.
@@ -264,7 +363,7 @@ Built-in default exclusion patterns (always applied): `__pycache__`, `*.pyc`, `.
264
363
 
265
364
  | Feature | ctxgraph | ctxgraph-code |
266
365
  |---------|----------|---------------|
267
- | CLI commands | 9 (build, capsule, query, view, serve, info, init, ask, chat, history, skill) | 9 (init, build, query, deps, usedby, overview, symbols, context, setup, view, info) |
366
+ | CLI commands | 9+ | 16 (init, build, query, deps, usedby, overview, symbols, context, setup, view, info, install-slash, build-status, probe, install-hooks, uninstall-hooks) |
268
367
  | LLM integration | Built-in (Ollama, Claude, OpenAI, Azure) | None (delegates to Claude Code) |
269
368
  | Chat sessions | Yes | No |
270
369
  | Visualizer | D3.js HTML + SVG | D3.js HTML (`view` opens in browser, `--tree` for text) |
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "ctxgraph-code"
7
- version = "0.2.1"
7
+ version = "0.4.0"
8
8
  description = "Code knowledge graph for Claude Code. Build a relationship graph of your Python codebase and query it during coding sessions."
9
9
  readme = "README.md"
10
10
  license = {text = "MIT"}
@@ -0,0 +1,4 @@
1
+ from __future__ import annotations
2
+
3
+ from .languages import EXTENSION_LANG, LANG_QUERIES # noqa: F401
4
+ from .analyzer import TSAnalyzer, TSAnalyzerResult # noqa: F401