Local Code Index & Code Graph
Kompile's Local Code Index (local_code_index MCP tool and kompile code-index CLI) gives every agent fast, offline structural understanding of a repository — with zero server dependency. It parses source files into entities and relationships, backs search with SQLite FTS5, keeps itself fresh in the background, and projects a portable knowledge graph slice into .kompile/graph.kgraph.
How it works under the hood
1. Offline parsing & storage (~/.kompile/code-index/<projectId>/)
- Multi-language entity & relation extraction: Walks the repository and extracts
FILE,PACKAGE,CLASS,INTERFACE,METHOD,FUNCTION,FIELD,IMPORT,TYPE_ALIAS, andRATIONALEentities (plus structural call/import/inheritance relations) across Java, Kotlin, Scala, Groovy, C, C++, C#, JavaScript/TypeScript, Python, Go, Rust, Swift, Ruby, PHP, Bash, SQL, JSON, YAML, TOML, XML, HTML, CSS, Markdown, Protobuf, ANTLR, and.splan. - SQLite WAL + FTS5 (
index.db): Stores entities, relations, and full-text search tables in a local SQLite database in WAL mode so concurrent readers (including File Context) never block on background writers. Cross-process write safety is coordinated viaIndexLockManager. - Incremental fingerprinting: Each file is fingerprinted by
mtime + size + SHA-256, so re-indexing only re-parses files that actually changed and removes deleted files. - Auto-resolved
project_id: You rarely need to passproject_id— Kompile resolves the owning repository root automatically from your working directory.
2. Background freshness (BackgroundIndexService)
You don't have to manually re-run indexing after every edit:
- Background index jobs:
action=indexruns asynchronously by default (background=true), waits up towait_seconds(default10s, max120s), and returns a job handle if still running (poll withaction=index_status). - Auto-started file watchers: The first read or completed index job starts an
IndexFileWatcher(LRU-evicted, capped at8watchers by default) so filesystem edits trigger incremental updates within seconds. - Direct write hooks: Whenever Kompile's own
editorwritetools modify a file, they callnoteFileWrittenimmediately — no waiting for OS inotify delivery. - Bounded freshness join (
prepareForRead): When a read action arrives right after a local write from the same process, Kompile waits briefly (bounded byKOMPILE_CODE_INDEX_READ_WAIT_MS, default2000ms) so you immediately read your own writes; when the index is quiet, reads return with zero stat-walk overhead. - Session-start refresh & periodic backstop: Opening a chat session triggers one incremental refresh pass, and a low-frequency sweep catches anything OS watchers missed (e.g., inotify overflow).
Environment / system-property tunables
| Variable | Default | Purpose |
|---|---|---|
KOMPILE_CODE_INDEX_BACKGROUND | true | Master switch for background indexing and watchers |
KOMPILE_CODE_INDEX_WATCH | true | Auto-start per-project file watchers |
KOMPILE_CODE_INDEX_MAX_WATCHERS | 8 | Maximum concurrent LRU-managed watchers |
KOMPILE_CODE_INDEX_IDLE_SECONDS | 300 | Retire passive maintenance after inactivity |
KOMPILE_CODE_INDEX_READ_WAIT_MS | 2000 | Max wait for freshness join on read actions |
3. Portable KGraph projection (LocalCodeKGraphPublisher)
After indexing commits to SQLite, Kompile publishes a materialized slice of the code entities and structural relations into the project's portable .kompile/graph.kgraph, tagged with the index generation. SQLite remains the fast search engine; graph.kgraph feeds graph reasoning tools and file_context's bounded neighborhood lookups.
MCP actions (local_code_index)
Always run local_code_index from the repository root (never create a separate sub-index for a subdirectory). Almost every read action accepts max_tokens to cap output size cleanly.
Discovery & search
| Action | What It Returns |
|---|---|
search | Fast FTS5 entity search by name/type |
ranked_search | Multi-signal relevance ranking with query intent detection and graph boost |
blended_search | Combined lexical + structural search for broad code exploration |
spath | Semantic path query — address code by meaning/symbol hierarchy rather than filesystem layout |
signatures | Compact symbol signatures per file (70–95% token reduction compared to reading whole files) |
find / replace | Scoped text/regex find (and replace) across indexed project files |
modules | Parses Maven pom.xml files to show the multi-module dependency graph |
Call graphs, types & blast radius
| Action | What It Returns |
|---|---|
callers | All callers of a method or function |
implementors | All concrete implementations of an interface or abstract type |
usages | All references/usages of a symbol across the indexed project |
trace | BFS call-chain traversal (outgoing or incoming) |
impact | BFS blast radius — every file/entity affected if a target file or symbol changes |
spring_resolve | Resolves Spring DI bean wiring for an interface |
Composite & Git-aware debugging
| Action | Best For |
|---|---|
debug_trace | Best single call for debugging: runs search + callers + trace + impact in one composite response |
changed_context | Pre-commit / PR review: inspects uncommitted git changes and computes entity-level blast radius |
cochanges | Mines git history for files that historically change together (hidden coupling) |
Codebase quality & triage
| Action | What It Returns |
|---|---|
pagerank | PageRank file importance scores (top_files or rank of a specific file) |
clones | Detects near-duplicate / copy-pasted functions using MinHash structural similarity |
unused_exports | Finds dead code — exported/public symbols with no external references |
routing | Classifies files into fast / balanced / powerful complexity tiers |
health | Index quality & freshness score (0–100) — intentionally unrefreshed so it reports real staleness |
Index lifecycle & repair
| Action | What It Does |
|---|---|
index | Indexes a directory (background=true by default, wait_seconds=10) |
index_status | Shows running/queued background jobs and per-project maintenance state |
repair | Synchronous SQLite integrity check and in-place FTS5 repair (never destroys/replaces a live DB) |
stats / list | Per-project entity/language counts (stats) or all indexed projects (list) |
Using the standalone CLI (kompile code-index)
You can run or inspect the exact same index directly from your shell:
# Index the current repository incrementally (also publishes .kompile/graph.kgraph)
kompile code-index
# Index a specific path with include/exclude filters or force a full rebuild
kompile code-index /path/to/repo --include="*.java,*.ts" --exclude="*Test.java"
kompile code-index --force
# Search, inspect usages, or watch for live edits
kompile code-index search "UserService"
kompile code-index spath --symbol=UserService
kompile code-index usages --symbol=authenticate
kompile code-index watch
kompile code-index stats
kompile code-index list
# Structural graph queries from the CLI
kompile code-index graph callers --symbol=processRequest
kompile code-index graph impact --symbol=authenticate
kompile code-index graph dossier --symbol=UserService
kompile code-index graph tests-for --symbol=processRequestSee Code Projects for the full kompile code-index graph subcommand list and web UI integration.
Recommended agent workflow
- Start unfamiliar codebase tasks with
local_code_index(blended_search/ranked_search/debug_trace) instead of broadgreploops. - Call
file_context(orsignatures) on candidate files before reading full implementations. - Hand off to
lspwhen you need compiler-exact overload resolution, semanticrename, or fail-closedcheckverification.
Next steps
- Coding Tools Overview
- Languages & Required Tools — parser tiers (structural vs FTS5/rationale) and LSP server install commands
- File Context, Notes & Multi-Agent Activity
- LSP: Language-Server Intelligence & Edit Gate