Indexing the code graph
Turn your mirrored repos into a queryable code graph: incremental indexing, and the full node and edge model across 27 languages, Terraform, SQL, and web topology.
On this page
For engineers who want a queryable graph of their code.
Indexing turns your mirrored repos into a queryable knowledge graph. contextlake kb index --workspace
~/work walks every git repo under a folder and builds the graph. Runs are incremental by default;
--force rebuilds from scratch.
It also writes two documents per repository it indexed, <store>/docs/api/<repo>.md and
<store>/docs/design/<repo>.md, so a fresh index leaves a store you can read as well as
query. No model is ever run for them. A repository whose head commit has not moved since its
documents were written is skipped, so the cost of the per-commit hook stays flat. --no-docs
switches them off, and contextlake kb docs regenerates them unconditionally. See
Generate documentation.
A run over a seven-repository workspace. The live progress bar is redrawn in place, so what stays on screen is one line per repository and the summary:
$ contextlake kb index --workspace /tmp/work
Found 7 repositories under /tmp/work (incremental); indexing 7 with 8 worker(s)
✓ acme/alerts: 13 nodes, 13 edges
✓ acme/sensor-ingest: 12 nodes, 12 edges
✓ acme/console-ui: 16 nodes, 15 edges
✓ acme/station-registry: 10 nodes, 11 edges
✓ acme/shared-lib: 11 nodes, 8 edges
✓ acme/forecast-api: 9 nodes, 8 edges
✓ demo/app: 5 nodes, 4 edges
✓ Workspace indexed: 7 repos, 76 nodes, 71 edges (0 unchanged, 0 failed)
✓ Documents: 7 written, 0 unchanged, documents failed for 0 repo(s) → /tmp/work-kb/docs
flowchart TD
D(["a directory you point kb index at"]) --> B{"--bundle passed?"}
B -->|yes| ONE["indexed as one repository"]
B -->|no| G{"is it itself a git repo?"}
G -->|yes| ONE
G -->|no| S{"does it hold several git repos, with only a
trivial share of files outside them?"}
S -->|yes| R(["refused, naming the command that fits"])
S -->|no| O{"does it hold one git repo,
with nothing outside it?"}
O -->|yes| R2(["refused, naming the command that fits"])
O -->|no| ONE
R -.->|"several repos:
kb index --workspace"| EACH["each repo indexed under its own id"]
R2 -.->|"kb index on that repo"| SOLO["that repo indexed under its own id"]
ONE --> GR[("the graph")]
EACH --> GR
SOLO --> GR
The shape of the directory is measured before any of it is indexed, and the next section walks each outcome.
Prerequisites#
- The knowledge layer installed (the
[kb]extra). See Installing. - One or more git repositories on disk, such as the ones mirroring cloned.
Which command for which directory#
kb index <dir> indexes that directory as one repository. kb index --workspace <dir> walks it
and indexes each git repository under it separately, under its own identity. Getting those two
the wrong way round is the most expensive mistake available here, so a directory that is not itself a
git repository but holds some is refused, with the command that fits what was found:
$ contextlake kb index ~/work
✗ /home/you/work isn't itself a git repo, but contains 20 git working tree(s) at depths 1-2
(alpha, beta, billing-core, frontend, gateway, …); no indexable file lies outside them.
That is a workspace mirroring several repositories. Indexing it this way files all of them
under ONE id, so every symbol they hold is in the graph twice and the copies cannot be told apart.
→ Run this instead, which indexes each repository separately under its own identity:
contextlake kb index --workspace ~/work
→ Or pass --bundle to index this directory as one repository anyway. …
It refuses rather than switching to --workspace for you, because switching can lose data.
--workspace indexes the nested repositories and nothing outside them. So on a tree of your own
loose sources that happens to carry a dependency with its own .git, it would index the
dependency and drop your files.
The shape is measured first: how much indexable content lies outside the nested repositories. Only then does it decide:
| What the directory looks like | What happens |
|---|---|
| Several repositories, and little of your own outside them (10 files or fewer, or 5% or less of the indexable files) | refused; run --workspace <dir> |
| One repository, nothing at all outside it | refused; the directory is one level too high, and the command names the repository |
| Real content of yours outside the repositories (your sources plus a vendored dependency) | indexed as one repository, with a line saying so |
A git repository (.git present), whatever it contains |
indexed, no diagnosis at all |
--bundle opts back in. It indexes the directory as one repository regardless of shape, and it
is read before anything is measured, so it always works. Reach for it when you want one
bundled repository. Note the cost you are accepting: the nested repositories' files are filed
under the directory's name, so if you later index them properly they are in the graph twice. kb
forget <repo-id> removes a bundle you did not mean to create.
Incremental and time-travel#
index --workspace is incremental. It re-indexes only repos whose git HEAD moved since their
last index, so a scheduled run stays cheap.
--forcerebuilds everything.- A repo whose HEAD has not moved is not parsed again, but its row takes the path of the clone the
run found. After you move a workspace, one
kb index --workspace NEWpoints every row at the new location, andkb index --source NEW/repodoes the same for one repo.kb refresh,kb lintandkb index --source IDthen use it. --watch [--interval N]keeps re-indexing in a loop. The same two flags also driveconnectandembed.
Every indexed snapshot is kept, which gives you time travel:
query "<text>" --repo R --as-of <commit> searches repo R as it was at a previously indexed
commit.
Re-indexing one repository by its id#
A repository's id is derived from its origin remote (so it survives being moved or re-cloned) and has
no relation to where the clone sits on disk. That is the id kb lint and the dashboard report, so
--source accepts it directly:
contextlake kb index --source example.com/team/widgets # the id, exactly as reported
contextlake kb index --source widgets # the tail, when only one repo ends that way
The repository is re-indexed under the id it was already filed as, never a second time under its directory name. If the id is unknown, the error names near-miss ids from the store; if it is known but its recorded checkout is gone, the error names the path it was indexed from.
Use --repo to set the id explicitly, for a checkout with no usable origin or one you want
filed under a different name:
contextlake kb index --source ./widgets --repo team/widgets
Without it, the id comes from the origin remote. A git repository with no origin is filed as
name@<first 12 characters of its root commit>, for example widgets@3f9c2a71b0de, where name is
the folder name. Only a directory that is not a git repository is filed under its bare folder name.
The name half still depends on where the clone sits on disk, which is the fragility this section
exists to avoid. Name it once with --repo, and use the id kb index prints in kb query --repo
and kb forget.
A named id is kept. Each later kb index ... --repo team/widgets updates the same row and keeps
its history snapshots, so --as-of still reaches old commits. A later run that names no id and
covers the checkout (a plain kb index PATH, or a --workspace run that does not filter it out
with --repos) files it under the id its origin gives and replaces the row you named. A run
never changes the row of a repo it does not index.
Parallelism and noise-pruning#
Repositories are parsed across worker processes (CPU-bound work) while the SQLite store is written
serially from the parent. The spawn start method is used on every platform, so behaviour is identical on
Linux, macOS, and Windows, with an automatic serial fallback if a worker pool can't start. It defaults to
cpu_count - 1 workers (capped at 8); set [kb] index_workers to tune it (1 forces serial).
The parser skips machine-generated and derived files. It skips a file when any of these match:
- a name like
*.designer.cs,*.min.jsorAssemblyInfo.cs - one of four header markers in the first 2048 bytes, matched case-insensitively:
<auto-generated,@generated,do not edit,code generated by - a size over
[kb] max_file_bytes, which defaults to 5 MB
The marker list catches more than it looks. A file whose banner only says "DO NOT EDIT" is skipped too.
All of that is derived noise, not real source. A run that skips generated or oversized
files prints one line for each group with the first five paths, such as
1 file(s) over max_file_bytes skipped: big.py. -v prints a line for every skipped file. A run
that skips none prints nothing. To index them anyway, set [kb] skip_generated = false or raise
max_file_bytes.
Files matched by .contextlakeignore are only counted, in the -v summary, because you chose
those patterns.
Source encodings#
The grammars read UTF-8. The parser converts any other code file to UTF-8 before it parses, so names and line numbers come out right:
- A file that starts with a UTF-16 or UTF-32 byte order mark is read in that encoding.
- A Python file with a
# -*- coding: latin-1 -*-line (PEP 263) in its first two lines is read in the encoding it names. - Any other bytes that are not valid UTF-8, such as C# or C on a Windows code page, are read as Windows-1252, one byte at a time. The valid UTF-8 around them is kept.
A file is never skipped for its encoding. -v prints read as <encoding> for each file that
was converted. These are not detected: UTF-16 without a byte order mark, and the declarations of
languages other than Python. SQL, Terraform, XML and the other non-code kinds are read as
UTF-8, as before.
The per-repository memory budget#
max_file_bytes bounds one FILE. It cannot bound a repository that is wide rather than deep,
and that distinction is not theoretical: one repository in a 660-repo fleet took a 15.4 GB
machine down while its largest single file was 3.57 MB, comfortably under the 5 MB cap. The
cap never fired once. What broke it was 1,432 XML files averaging 0.42 MB, adding up to
671 MB.
[kb] max_repo_memory bounds the whole repository, and is checked before any file is
read rather than after a shard exists. It defaults to 3 GB. A repository whose estimate
exceeds it is skipped with its name, its estimated cost, and the file kinds that dominate,
and the rest of the run continues.
The estimate weights each file kind by measured cost per byte, because they differ by a lot:
| Kind | Peak memory per byte of source |
|---|---|
| code | 19.6x |
| SQL | 5.0x |
| XSD / XSL | 4.3x |
| XML | 3.5x |
Code is six times worse than markup, and the reason is edge count rather than node count: a sample of 5,738 C# files produced 82,000 nodes and 467,000 edges. So 400 MB of code is more expensive than 400 MB of XML, and a budget in raw bytes would refuse the wrong ones.
Two caveats worth knowing before you tune it:
- The estimate is linear and the real cost is not. Cross-file reference resolution grows with the number of symbols, so the estimate runs low on the largest repositories, which is where it matters most. Treat it as a coarse guard, not a prediction. The shard-item guard that runs after a shard is built is the second layer.
- This bounds one repository; a run's peak is this times the worker count. On a smaller
machine, lower
max_repo_memory,index_workers, or both.
Set max_repo_memory = 0 to disable the check.
Discovery also skips vendored nested repos: an upstream clone carried inside the mirror,
with its own .git, under a module-federation path segment. That is not your source, and
indexing it would flood the global graph with upstream demo nodes. Each skip is logged.
node_modules trees are pruned before discovery even descends into them.
To exclude your own paths, drop a .contextlakeignore at a repo's root. One glob per line;
# comments and blank lines are ignored.
Each glob is matched against the file's path relative to the repo, and against its name. So:
*.lockignores by name, anywhere in the repovendor/ignores that directory and everything under it
It is a small, dependency-free subset of gitignore syntax. No negation, no **, no anchoring.
Enough to drop vendored trees and lockfiles from the graph.
Verification#
- Read the last line of the run. A good run ends with the workspace line and no failures:
✓ Workspace indexed: N repos, N nodes, N edges (N unchanged, 0 failed)
- Query a symbol name you know is in one of the repos:
contextlake kb query load_readings
Each hit is one line: repo, file, kind and name.
3. Run contextlake kb lint. It exits 0 when the graph has no dangling edges, no HEAD-stale
repos and no unreadable repos.
Health and maintenance#
contextlake doctor checks the environment: FTS5, git and glab on PATH, the store, the
embedder and the ANN index. It exits non-zero if anything is wrong, so it works as a CI health
gate.
It also flags any shard indexed with an older parser version than the one installed. That flag
is advisory and does not affect the exit code. contextlake kb index rebuilds those on its next
run, so they pick up parser fixes.
contextlake kb lint audits the graph itself. It reports:
- stale repos, where HEAD moved since they were indexed
- dangling edges, where an edge's endpoint node is missing
- the same older-parser repos doctor reports, so the two commands never disagree
- orphan vectors, where the semantic store holds vectors for a partition that has no nodes in the graph (see below)
- shared file nodes, where several files in the store share one file node (see Shared file nodes)
Both exit non-zero on problems. For lint that means dangling edges, HEAD-stale repos, or repos it cannot read.
The orphan-vector check is advisory too. For each partition that has vectors and no nodes, lint prints a line:
orphan vectors: <partition> (vectors with no nodes behind them, so nothing can reach them -- ...)
and the summary line ends with N partition(s) with orphaned vectors. Run contextlake kb embed
to rebuild them, or contextlake kb forget to remove them. Before 9.3.0 an interrupted
kb forget could leave this state. The check does not affect the exit code.
It runs only when [embeddings] is enabled and the vector store exists. Otherwise it reports
none. It also runs in the CLI only: the dashboard's lint view does not include it, because the scan
reads the vector store.
kb lint --json carries two fields for it, on every run:
| Field | Type | Meaning |
|---|---|---|
orphan_vectors |
number | how many partitions have vectors and no nodes; 0 when there are none |
orphan_vector_partitions |
list of strings | the partition ids, sorted; [] when there are none |
The older-parser count is reported but deliberately kept out of lint's exit code. Otherwise upgrading to a build with a new parser could turn a green CI gate red on its own.
Two states are reported apart from stale, because re-indexing clears a stale repository and cannot clear either of them:
- empty: the repository has no commits at all, so there is no HEAD and nothing to index. Reported, and not counted against the exit code: there is nothing for a reader to do about it.
- shard-imported: indexed from a graph-shard JSON instead of a checkout, so it has no history to be behind. Also advisory, for the same reason.
- unreadable: the recorded path no longer exists, or git will not answer for a repository there. Re-clone it or drop it from the store. This one does fail the run, because nothing can be cited from it.
Shared file nodes#
Every file in the graph has a node. Before parser version 13, its id was built from the repo id
and the file path by lowercasing the text and turning every run of punctuation into one _. So
these names got the same id:
a-b.py,a_b.pyandA_B.pyin one repopkg/__main__.pyandpkg/main.pynumpy/_core/records.pyandnumpy/core/records.py- the repos
grp/a-bandgrp/a/b, for a file with the same path in both
The store keeps one row per id, so all but one of those files were missing from the graph.
Since parser version 13, each of them has its own node. A file id keeps the readable form
above and adds 8 hex of a hash over the exact repo id and path. The same holds for the names
inside a file: tables, config keys, Terraform resources, XSD and XSL items, ADRs, entry points
and web routes. The path is written one way on every system (/ separators, no leading ./),
so Linux and Windows give a file the same id. SQL object names still ignore case, as SQL does.
kb lint names each such node, with the repo and every path it can see:
shared file node: <repo>: A_B.py, a-b.py, a_b.py share one node, so 2 of them are missing from the graph (indexed before parser 13; `kb index` re-indexes it, see docs/indexing-the-code-graph.md#shared-file-nodes; not counted in this command's exit code)
When two repos share the node, the line says across repos and puts the repo in front of each
path. The summary line ends with N shared file node(s).
On a store indexed by parser version 13 or later the check reports nothing. A report means a
repo was last indexed by an older version: kb index re-indexes it (the version change makes it
do so) and the report clears. The check changes nothing itself and does not affect the exit
code.
The check is a lower bound. The tables keep a trace of a file only through its node row and its
contains edges. A colliding file that has no definitions, and is not the file the node row
names, leaves no trace, so lint cannot see it.
It runs in the CLI only, like the orphan-vector check. It reads SQLite, and the dashboard's lint view answers from a cache of the shards. On a synthetic store of 700,000 nodes and 1.7 million edges it took 0.45 s.
kb lint --json carries three fields for it, on every run:
| Field | Type | Meaning |
|---|---|---|
shared_file_nodes |
number or null | how many file nodes stand for more than one file; 0 when there are none; null when the check could not run |
shared_file_node_repos |
list of strings | every repo that shares a node, sorted; [] when there are none |
shared_file_nodes_sample |
list of objects | the first 20 findings. Each has node (the id), repos, cross_repo (true when more than one repo shares it) and files (a list of {repo, path}) |
The full node and edge model, language by language, is in The code graph model.
What an index produces#

This one is contextlake's own repository, indexed by the commands on this page. It is the shipped visualizer, not a recording, and it runs offline with no network calls.
