Elffuss BraidHub

kikocis/continue public

main · 3.6 KiBRenderedRawPermalink
1# Codebase Indexing
2
3This is a small Rust library for efficiently keeping a codebase index up to date.
4
5### How it works
6
7> Important definition: a _tag_ is a (workspace, branch, provider_id) pair that uniquely identifies an index. Since we use content-based addressing within the index, much of the data is shared for efficiency.
8
9The output of the sync_results function is a list of 4 lists of tuples. Each tuple contains a file path and a hash of the file contents. The 4 lists are:
10
111. Compute: Files that need to be newly computed or updated
122. Delete: Files that need to be deleted from the index
133. Add label: Files that exist in the index but need to have a label added for a new tag
144. Remove label: Files that exist in the index but need to have a label removed
15
16The labels help us filter when retrieving results from an index like Meilisearch or Chroma. All ids of the items in these indices are the hash of the file contents (possibly plus a chunk index at the end).
17
18The first time, a Merkle tree of the codebase folder is constructed, ignoring any files in .gitignore or .continueignore. Every file found will be returned as needing to be computed added to the index.
19
20Thereafter, the following steps are performed:
21
221. Load the previously computed merkle tree for the tag
232. Compute the current merkle tree of the codebase
243. Update the .last_sync file with current timestamp
254. Save the new tree to disk
265. Compute the diff of the trees, which tells you which files have been a) added or b) removed
276. For each file added:
28 - If in the global cache, append it to `add_label`
29 - Otherwise, append it to `compute`
307. For each file removed:
31 - If in the global cache, but only in rev_tags for this tag, append it to `delete`
32 - If in global cache for more than this tag, append it to `remove_label`
33 - Otherwise, ignore. This should never happen.
348. Return (compute, delete, add_label, remove_label)
35
36### Files created
37
38Several files are stored and updated on disk in the ~/.continue/index folder to keep track of indexed files:
39
40- `~/.continue/index/tags/<dir>/<branch>/<provider_id>/merkle_tree` - the last computed Merkle tree of the codebase for a given tag
41- `~/.continue/index/tags/<dir>/<branch>/<provider_id>/.last_sync` - the last time the tag was synced
42- The index cache contains a list of hashes that have already been computed both in general and per tag. These are always kept in sync.
43 - `~/.continue/index/.index_cache` - contains the global cache (flat file of hashes)
44 - `~/.continue/index/tags/<dir>/<branch>/<provider_id>/.index_cache` - contains the tag-specific cache (flat file of hashes)
45 - `~/.continue/index/rev_tags` - contains a mapping from hash to tags that the hash is currently indexed for. This is a directory of files, where each file is prefixed with the first 2 characters of the hash. The file is a JSON mapping from hash to list of tags.
46
47### Files
48
49- `lib.rs` contains just the top-level function that is called by the Python bindings
50- `sync/merkle.rs` contains the Merkle tree implementation (for building and comparing trees)
51- `sync/mod.rs` contains the main sync logic, which handles maintenance of the on-disk database of which hashes are included in which tags
52
53### Current limitations:
54
55- Only handles local files, so is not currently being used in situations where the Continue server is on a different machine from the IDE or the workspace (Remote SSH, WSL, or a Continue server being run for a team).
56- Currently not using stat to check for recent changes to files, is instaed re-calculating the entire Merkle tree on every IDE reload. This is fine for now since it only takes 0.2 seconds on the Continue codebase, but is a quick improvement we can make later.