Skip to content

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, and RATIONALE entities (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 via IndexLockManager.
  • 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 pass project_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=index runs asynchronously by default (background=true), waits up to wait_seconds (default 10s, max 120s), and returns a job handle if still running (poll with action=index_status).
  • Auto-started file watchers: The first read or completed index job starts an IndexFileWatcher (LRU-evicted, capped at 8 watchers by default) so filesystem edits trigger incremental updates within seconds.
  • Direct write hooks: Whenever Kompile's own edit or write tools modify a file, they call noteFileWritten immediately — 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 by KOMPILE_CODE_INDEX_READ_WAIT_MS, default 2000 ms) 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 ​

VariableDefaultPurpose
KOMPILE_CODE_INDEX_BACKGROUNDtrueMaster switch for background indexing and watchers
KOMPILE_CODE_INDEX_WATCHtrueAuto-start per-project file watchers
KOMPILE_CODE_INDEX_MAX_WATCHERS8Maximum concurrent LRU-managed watchers
KOMPILE_CODE_INDEX_IDLE_SECONDS300Retire passive maintenance after inactivity
KOMPILE_CODE_INDEX_READ_WAIT_MS2000Max 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.

ActionWhat It Returns
searchFast FTS5 entity search by name/type
ranked_searchMulti-signal relevance ranking with query intent detection and graph boost
blended_searchCombined lexical + structural search for broad code exploration
spathSemantic path query — address code by meaning/symbol hierarchy rather than filesystem layout
signaturesCompact symbol signatures per file (70–95% token reduction compared to reading whole files)
find / replaceScoped text/regex find (and replace) across indexed project files
modulesParses Maven pom.xml files to show the multi-module dependency graph

Call graphs, types & blast radius ​

ActionWhat It Returns
callersAll callers of a method or function
implementorsAll concrete implementations of an interface or abstract type
usagesAll references/usages of a symbol across the indexed project
traceBFS call-chain traversal (outgoing or incoming)
impactBFS blast radius — every file/entity affected if a target file or symbol changes
spring_resolveResolves Spring DI bean wiring for an interface

Composite & Git-aware debugging ​

ActionBest For
debug_traceBest single call for debugging: runs search + callers + trace + impact in one composite response
changed_contextPre-commit / PR review: inspects uncommitted git changes and computes entity-level blast radius
cochangesMines git history for files that historically change together (hidden coupling)

Codebase quality & triage ​

ActionWhat It Returns
pagerankPageRank file importance scores (top_files or rank of a specific file)
clonesDetects near-duplicate / copy-pasted functions using MinHash structural similarity
unused_exportsFinds dead code — exported/public symbols with no external references
routingClassifies files into fast / balanced / powerful complexity tiers
healthIndex quality & freshness score (0–100) — intentionally unrefreshed so it reports real staleness

Index lifecycle & repair ​

ActionWhat It Does
indexIndexes a directory (background=true by default, wait_seconds=10)
index_statusShows running/queued background jobs and per-project maintenance state
repairSynchronous SQLite integrity check and in-place FTS5 repair (never destroys/replaces a live DB)
stats / listPer-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:

bash
# 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=processRequest

See Code Projects for the full kompile code-index graph subcommand list and web UI integration.

  1. Start unfamiliar codebase tasks with local_code_index (blended_search / ranked_search / debug_trace) instead of broad grep loops.
  2. Call file_context (or signatures) on candidate files before reading full implementations.
  3. Hand off to lsp when you need compiler-exact overload resolution, semantic rename, or fail-closed check verification.

Next steps ​

Released under the Apache-2.0 License.