Python API
Everything RCE Code does is reached through one class: CodeEngine, the public facade over an index. It owns the SQLite database, the embedder, and the lexical, semantic, symbol, hybrid, and grep searchers, and exposes them as plain methods. This page documents every public method — exact signature, return type, and behavior — followed by the dataclasses those methods return.
Import the facade and the data types from the package root:
One facade, one database
CodeEngine instance wraps a single SQLite index for one repository. Construct it with CodeEngine.open_or_create(...) — never call CodeEngine(...) directly — and call close() when you are done. All query methods are read paths over the same database; only index(), refresh(), and embed() mutate it.Lifecycle
open_or_create is the sole constructor. It builds a CodeConfig if you do not pass one, opens the SQLite database, and runs migrations before returning a ready instance.
| Method | Signature | Returns | Description |
|---|---|---|---|
open_or_create | open_or_create(repo_path: str | Path, config: CodeConfig | None = None, embedder: Embedder | None = None) | CodeEngine | Classmethod. Open (or create) the index DB for repo_path and return a ready CodeEngine. Runs migrations; db_path defaults to <repo>/.rce/code/index.db. Defaults to the offline HashingEmbedder when no embedder is given. |
close | close() | None | Close the underlying database connection. |
Indexing
index() builds (and incrementally updates) the index; refresh() is an alias of it; embed() fills in vector embeddings for chunks that lack a current one. See the Indexing Pipeline for what each stage does.
| Method | Signature | Returns | Description |
|---|---|---|---|
index | index() | IndexSummary | Build or update the index for the repo (walk → parse → chunk → symbols → refs → resolve). Idempotent; unchanged files are hash-skipped. |
refresh | refresh() | IndexSummary | Re-index the repo incrementally (alias of index): hash-skips unchanged files, re-parses changed ones, and drops files that no longer exist. |
embed | embed() | int | Embed chunks that lack a current embedding. Returns the count embedded. Required before search_semantic returns anything; see Embedders. |
Search
Four query paths over the indexed corpus. The three search_* methods return SearchResult lists ranked by score; grep runs live ripgrep over the working tree and returns GrepHit lists. See Search & Retrieval for when to reach for each.
| Method | Signature | Returns | Description |
|---|---|---|---|
search_lexical | search_lexical(query: str, top_k: int = 10) | list[SearchResult] | FTS5 BM25 lexical search over indexed chunks; returns the top_k SearchResults. See Lexical Search. |
search_semantic | search_semantic(query: str, top_k: int = 10) | list[SearchResult] | Embedding cosine-KNN search over indexed chunks; returns the top_k SearchResults (empty if nothing is embedded yet). See Semantic Search. |
search_hybrid | search_hybrid(query: str, top_k: int = 10) | list[SearchResult] | Reciprocal-Rank-Fusion of lexical + semantic + symbol search; returns the top_k fused SearchResults. See Hybrid Search. |
grep | grep(pattern: str, *, glob: str | None = None, regex: bool = False, case_sensitive: bool = False, max_count: int | None = None, raw: bool = False) | list[GrepHit] | Live ripgrep over the working tree (fixed-string by default; regex if regex=True); honours the index ignore set unless raw=True. Returns GrepHits. See Grep. |
grep keyword-only options
grep parameter after pattern is keyword-only (note the * in the signature). Pass them by name, e.g. engine.grep("TODO", glob="*.py", max_count=20).Structural
Symbol- and reference-level queries over the resolved call graph. Methods that take a symbol_id expect the id of a Symbol you already have (typically from find_symbol or get_definitions). See Symbols & References.
| Method | Signature | Returns | Description |
|---|---|---|---|
find_symbol | find_symbol(name: str, kind: SymbolKind | None = None) | list[Symbol] | Symbol definitions matching the exact name, optionally filtered by kind. |
get_definitions | get_definitions(file: str) | list[Symbol] | All symbol definitions in the given file, in source order. |
get_definition_at | get_definition_at(file: str, line: int, col: int) | Symbol | None | Innermost symbol whose span contains (line, col), or None (col is 0-indexed). |
get_symbols_in_chunk | get_symbols_in_chunk(chunk_id: int) | list[Symbol] | Symbol definitions that belong to the given chunk. |
find_references | find_references(name: str, is_import: bool | None = None) | list[Reference] | References by target name (resolved or not), optionally filtered to imports only. |
get_references_from | get_references_from(symbol_id: int) | list[Reference] | Outgoing references originating in the given symbol's body (by name; may be unresolved). |
get_references | get_references(symbol_id: int) | list[Reference] | Resolved references whose target is this symbol (incoming). |
get_callers | get_callers(symbol_id: int) | list[Symbol] | Symbols that call the given symbol (resolved call graph). |
get_callees | get_callees(symbol_id: int) | list[Symbol] | Symbols called by the given symbol (resolved call graph). |
get_references vs. get_references_from
get_references(symbol_id) returns the incoming resolved references whose target is this symbol; get_references_from(symbol_id) returns the outgoing references that originate inside this symbol's body (which may still be unresolved).Discovery
File-level helpers for listing and reading source. find_files and read_file work against the working tree on disk; list_files reads the set of files already in the index.
| Method | Signature | Returns | Description |
|---|---|---|---|
find_files | find_files(glob: str) | list[str] | Repo-relative paths of files on disk matching the glob, sorted. |
read_file | read_file(rel_path: str, line_range: tuple[int, int] | None = None) | str | Read a repo-relative file as UTF-8 text; if line_range=(lo, hi) is given, return only those 1-indexed inclusive lines. |
list_files | list_files(language: str | None = None) | list[str] | Indexed file paths, sorted; optionally filtered to a single language. |
Repo map
A single method produces an Aider-style, relevance-ranked map of the codebase, fit to a token budget. See Repo Map.
| Method | Signature | Returns | Description |
|---|---|---|---|
build_repo_map | build_repo_map(focused_files: list[str] | None = None, mentioned_idents: list[str] | None = None, token_budget: int = 1024) | str | Aider-style relevance-ranked repo map as text, fit to token_budget; bias toward focused_files / mentioned_idents. |
Health
| Method | Signature | Returns | Description |
|---|---|---|---|
stats | stats() | IndexStats | Index health snapshot: file/chunk/symbol/ref counts, embedding coverage, last sync, and FTS status. |
Return types
All return types are frozen dataclasses (and string enums) from rce.code.types. The tables below cover the fields you will use most; every dataclass is immutable.
SearchResult
Returned by search_lexical, search_semantic, and search_hybrid.
| Field | Type | Description |
|---|---|---|
chunk | Chunk | The matched chunk (see below). |
score | float | Relevance score; higher is better. Comparable within one modality. |
source | SearchSource | Which modality produced the hit: lexical, semantic, symbol, or hybrid. |
freshness | Freshness | Staleness / embedding status of the underlying file (see below). |
snippet | str | None | An optional highlighted excerpt of the match. |
matched_terms | list[str] | The query terms that matched. |
Chunk
A stored chunk — the unit of retrieval. Reached via SearchResult.chunk.
| Field | Type | Description |
|---|---|---|
id | int | Stable chunk id. |
file_id | int | Id of the file this chunk belongs to. |
file_path | str | Denormalized repo-relative path of the file. |
start_line / end_line | int | 1-indexed inclusive line span. |
start_byte / end_byte | int | Byte offsets of the chunk in the file. |
kind | ChunkKind | function, class, method, module, block, or doc. |
name | str | None | Definition name, when the chunk is a named definition. |
parent_chunk_id | int | None | Enclosing chunk, e.g. the class a method lives in. |
is_skeleton | bool | True for a structural skeleton chunk rather than full body. |
content | str | The chunk's source text. |
search_terms | str | Expanded terms indexed for lexical search (e.g. identifier sub-tokens). |
content_hash | str | Content-address used for incremental indexing. |
token_count | int | Token count of the chunk. |
truncated | bool | True if the chunk content was truncated. |
language | str | Detected language of the file. |
Symbol
A stored symbol definition. Returned by find_symbol, get_definitions, get_definition_at, get_symbols_in_chunk, get_callers, and get_callees.
| Field | Type | Description |
|---|---|---|
id | int | Stable symbol id — pass this to the call-graph and reference methods. |
name | str | Bare symbol name. |
qualified_name | str | Fully-qualified name, e.g. module.Class.method. |
kind | SymbolKind | function, method, class, interface, type, enum, variable, constant, field, parameter, or module. |
file_id / file_path | int / str | Owning file (id and denormalized repo-relative path). |
chunk_id | int | None | The chunk this symbol resolves into, if any. |
start_line / start_col | int | Definition start position (col 0-indexed). |
end_line / end_col | int | Definition end position. |
parent_id | int | None | Enclosing symbol (e.g. the class of a method). |
signature | str | None | Extracted signature text, when available. |
docstring | str | None | Attached docstring, when present. |
visibility | Visibility | None | public, private, protected, or internal, when inferable. |
language | str | Language of the defining file. |
Reference
A stored reference (call or import). Returned by find_references, get_references_from, and get_references.
| Field | Type | Description |
|---|---|---|
id | int | Stable reference id. |
from_symbol_id | int | None | Symbol the reference originates in, if attributable. |
from_file_id / from_file_path | int / str | Source file the reference appears in. |
to_name | str | Target name as written at the reference site. |
to_symbol_id | int | None | Resolved target symbol; None until the resolver matches it. |
resolution_confidence | float | Confidence of the resolution. |
is_import | bool | True for an import reference, false for a call. |
line / col | int | Position of the reference site. |
language | str | Language of the source file. |
GrepHit
Returned by grep — one per matching line.
| Field | Type | Description |
|---|---|---|
file_path | str | Repo-relative path of the matching file. |
line | int | 1-indexed line number of the match. |
column | int | Column of the match within the line. |
line_text | str | Full text of the matching line. |
context_before / context_after | list[str] | Surrounding context lines (empty unless requested). |
IndexStats
Returned by stats().
| Field | Type | Description |
|---|---|---|
files / chunks / symbols / refs | int | Row counts for each indexed entity. |
embedding_coverage | float | Fraction of chunks that have a current embedding (0.0–1.0). |
last_full_sync_at | float | None | Unix timestamp of the last full sync, or None. |
stale_files | int | Count of files newer on disk than in the index. |
skipped | dict[str, int] | Skip reasons mapped to counts (e.g. too_large, binary). |
fts_ok | bool | Whether the FTS5 lexical index is healthy. |
IndexSummary
Returned by index() and refresh() — the result of one indexing run.
| Field | Type | Description |
|---|---|---|
files_indexed | int | Files added or re-indexed this run. |
files_deleted | int | Files removed from the index (no longer on disk). |
skipped | dict[str, int] | Skip reasons mapped to counts. |
Freshness
Carried on every SearchResult as result.freshness.
| Field | Type | Description |
|---|---|---|
file_mtime | float | On-disk modification time of the file. |
indexed_at | float | When the file was last indexed. |
is_stale | bool | True if the file changed on disk after it was indexed. |
has_embedding | bool | True if the chunk has a current embedding. |
End-to-end examples
Open or create an index, build it, embed pending chunks, and run each modality:
Walk the call graph from a named definition — resolve the symbol, then ask for its callers and callees:
Construct once, reuse
open_or_create runs migrations and opens the database, so call it once per repo and reuse the instance for many queries. Pair it with a try/finally that calls close(), or wrap the lifetime in your own context manager.