codenib.index.embedding
¶
Embedding module for CodeNib. Provides vector storage and semantic search capabilities for code chunks.
Modules:
| Name | Description |
|---|---|
artifact_integrity |
Dependency-free integrity records for persisted vector levels. |
builders |
Reusable embedding builders for hierarchical pipelines. |
model_policy |
Embedding model defaults and remote-code trust policy. |
prompt_registry |
Per-model prompt registry for sentence-transformer embedders. |
text_policy |
Deterministic text bounds for remote embedding requests. |
vector_store |
Vector Store implementation using FAISS and sentence-transformers for code embeddings. |
Classes:
| Name | Description |
|---|---|
VectorStoreBuilder |
Builder class wrapping common vector store build operations. |
CodeVectorStore |
Vector store for code embeddings using FAISS and sentence-transformers. |
Functions:
| Name | Description |
|---|---|
build_hierarchical_vector_store |
Build (or load) a hierarchical vector store (L0/L2) for a repository. |
create_code_vector_store |
Factory function to create a CodeVectorStore. |
VectorStoreBuilder
¶
CodeVectorStore
¶
CodeVectorStore(
embedding_model: str = "text-embedding-ada-002",
embedding_provider: str = "openai",
dimension: int = 1536,
index_type: str = "flat",
index_metric: str = "ip",
ivf_nlist: int = 100,
ivf_nprobe: int = 8,
store_path: str | None = None,
profiler: Profiler | None = None,
embedding: Any | None = None,
artifact_metadata: dict[str, Any] | None = None,
**embedding_kwargs
)
Vector store for code embeddings using FAISS and sentence-transformers. Provides semantic search capabilities over code chunks.
Supports hierarchical indexing: - L0: File-level skeletons - L2: Function/method-level chunks for fine-grained retrieval (default)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_model
|
str
|
Name of the embedding model to use |
'text-embedding-ada-002'
|
embedding_provider
|
str
|
Provider for embeddings ("openai" or "huggingface") |
'openai'
|
dimension
|
int
|
Dimension of the embedding vectors |
1536
|
index_type
|
str
|
FAISS index type — "flat" (exact brute force, default) or "ivf" (IVF inverted-file; approximate, faster at scale). IVF indices are trained lazily on the first batch of vectors. |
'flat'
|
index_metric
|
str
|
Distance metric ("ip" for inner product, "l2" for L2 distance) |
'ip'
|
ivf_nlist
|
int
|
IVF only — number of Voronoi cells (coarse centroids). On
small corpora it is clamped down to the training-set size, since
FAISS k-means needs at least |
100
|
ivf_nprobe
|
int
|
IVF only — cells probed per query; the recall/latency
knob. Clamped to the effective |
8
|
store_path
|
str | None
|
Path to store/load the vector store |
None
|
profiler
|
Profiler | None
|
Optional profiler instance to capture detailed timings |
None
|
embedding
|
Any | None
|
A pre-built embedding wrapper to reuse. When several stores share one model (e.g. one per repo), pass the same instance so the model is loaded onto the GPU only once. |
None
|
artifact_metadata
|
dict[str, Any] | None
|
Optional immutable source/build identity persisted with the top-level configuration. |
None
|
**embedding_kwargs
|
Additional arguments for embedding model |
{}
|
Methods:
| Name | Description |
|---|---|
reuse_query_embedding |
Reuse one query vector within a composed request, then discard it. |
clear_query_cache |
Clear the single-query embedding reused by consecutive search stages. |
swap_index |
Hot-swap the FAISS index without reloading the embedding model. |
close |
Release embeddings and FAISS resources to free memory. |
add_code_chunks |
Add code chunks to the vector store. |
add_nodes_with_content |
Add NodeInfo objects (with content) to the vector store. |
search |
Search for similar code chunks using semantic similarity. |
search_with_content |
Search and return results with content included. |
search_within_ids |
Search only within a restricted set of node IDs. |
hierarchical_search |
Note: This method is implemented by Claude and is just for future reference. |
save |
Save the vector store to disk. |
load |
Load the vector store from disk. |
get_stats |
Get statistics about the vector store. |
get_embeddings_by_content_hash |
Extract raw embedding vectors from the FAISS index, keyed by content hash. |
rebuild_from_embeddings |
Clear level and rebuild its FAISS index from pre-computed embeddings. |
delta_update |
Patch a flat FAISS index in place when the change set is small. |
clear |
Clear data from the vector store. |
Attributes:
| Name | Type | Description |
|---|---|---|
closed |
bool
|
Whether every resource owned by this store has been released. |
Source code in codenib/index/embedding/vector_store.py
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 | |
reuse_query_embedding
¶
Reuse one query vector within a composed request, then discard it.
Source code in codenib/index/embedding/vector_store.py
clear_query_cache
¶
Clear the single-query embedding reused by consecutive search stages.
swap_index
¶
swap_index(
path: str, *, native_index_authorization: NativeIndexAuthorization | None = None
) -> None
Hot-swap the FAISS index without reloading the embedding model.
The replacement is fully loaded and validated before the current L0/L2 state is released. The embedding model is left intact so the caller can reuse the same model across many instances.
Source code in codenib/index/embedding/vector_store.py
close
¶
Release embeddings and FAISS resources to free memory.
Source code in codenib/index/embedding/vector_store.py
add_code_chunks
¶
Add code chunks to the vector store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code_chunks
|
list[dict[str, Any]]
|
List of code chunk dictionaries with content and metadata |
required |
level
|
Level
|
Index level to add chunks to ("l0" for file skeletons, "l2" for functions/methods) |
'l2'
|
Source code in codenib/index/embedding/vector_store.py
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 | |
add_nodes_with_content
¶
add_nodes_with_content(nodes: list[NodeInfo], level: Level = 'l2') -> None
Add NodeInfo objects (with content) to the vector store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nodes
|
list[NodeInfo]
|
List of NodeInfo objects |
required |
level
|
Level
|
Index level to add nodes to ("l0" or "l2") |
'l2'
|
Source code in codenib/index/embedding/vector_store.py
search
¶
search(
query: str,
top_k: int = 10,
score_threshold: float | None = None,
level: Level = "l2",
mask_node_ids: set[str] | None = None,
) -> list[NodeInfo]
Search for similar code chunks using semantic similarity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Search query text |
required |
top_k
|
int
|
Number of top results to return |
10
|
score_threshold
|
float | None
|
Minimum similarity score threshold |
None
|
level
|
Level
|
Index level to search ("l0" for file skeletons, "l2" for functions/methods) |
'l2'
|
mask_node_ids
|
set[str] | None
|
Optional set of CodeChunk.node_id values to filter results. |
None
|
Returns:
| Type | Description |
|---|---|
list[NodeInfo]
|
List of NodeInfo objects with scores populated |
Source code in codenib/index/embedding/vector_store.py
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 | |
search_with_content
¶
search_with_content(
query: str,
top_k: int = 10,
score_threshold: float | None = None,
level: Level = "l2",
mask_node_ids: set[str] | None = None,
) -> list[NodeInfo]
Search and return results with content included.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Search query text |
required |
top_k
|
int
|
Number of top results to return |
10
|
score_threshold
|
float | None
|
Minimum similarity score threshold |
None
|
level
|
Level
|
Index level to search ("l0" for file skeletons, "l2" for functions/methods) |
'l2'
|
mask_node_ids
|
set[str] | None
|
Optional set of CodeChunk.node_id values to filter results. |
None
|
Returns:
| Type | Description |
|---|---|
list[NodeInfo]
|
List of NodeInfo objects with content populated |
Source code in codenib/index/embedding/vector_store.py
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 | |
search_within_ids
¶
search_within_ids(
query: str, mask_node_ids: set[str], top_k: int = 10, level: Level = "l2"
) -> list[NodeInfo]
Search only within a restricted set of node IDs.
Instead of searching the full FAISS index globally and filtering afterwards, this method restricts the search space before computing similarity. It reconstructs stored vectors for matching documents and computes similarity against the query embedding directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Search query text. |
required |
mask_node_ids
|
set[str]
|
Set of node_id / node_name values to restrict search to. |
required |
top_k
|
int
|
Number of top results to return. |
10
|
level
|
Level
|
Index level to search. |
'l2'
|
Returns:
| Type | Description |
|---|---|
list[NodeInfo]
|
List of NodeInfo objects sorted by similarity score. |
Source code in codenib/index/embedding/vector_store.py
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 | |
hierarchical_search
¶
hierarchical_search(
query: str,
l0_top_k: int = 5,
l2_top_k: int = 10,
l0_score_threshold: float | None = None,
l2_score_threshold: float | None = None,
filter_l2_by_l0: bool = True,
) -> dict[str, list[NodeInfo]]
Note: This method is implemented by Claude and is just for future reference. Perform hierarchical search: first L0 (files), then L2 (functions).
This implements a coarse-to-fine retrieval strategy: 1. Search L0 to find relevant files based on their skeletons 2. Search L2 for specific functions/methods 3. Optionally filter L2 results to only include those from L0 files
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Search query text |
required |
l0_top_k
|
int
|
Number of top L0 results (files) |
5
|
l2_top_k
|
int
|
Number of top L2 results (functions/methods) |
10
|
l0_score_threshold
|
float | None
|
Score threshold for L0 results |
None
|
l2_score_threshold
|
float | None
|
Score threshold for L2 results |
None
|
filter_l2_by_l0
|
bool
|
If True, only return L2 results from files found in L0 |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, list[NodeInfo]]
|
Dict with 'l0' and 'l2' keys containing search results |
Source code in codenib/index/embedding/vector_store.py
save
¶
Save the vector store to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | None
|
Path to save the store (uses self.store_path if not provided) |
None
|
Source code in codenib/index/embedding/vector_store.py
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 | |
load
¶
load(
path: str | None = None,
*,
native_index_authorization: NativeIndexAuthorization | None = None
) -> None
Load the vector store from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | None
|
Path to load the store from (uses self.store_path if not provided) |
None
|
native_index_authorization
|
NativeIndexAuthorization | None
|
Process-local authorization bound to the exact captured tree and semantic view contract. Artifact fields cannot provide this capability. |
None
|
Source code in codenib/index/embedding/vector_store.py
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 | |
get_stats
¶
Get statistics about the vector store.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with store statistics |
Source code in codenib/index/embedding/vector_store.py
get_embeddings_by_content_hash
¶
Extract raw embedding vectors from the FAISS index, keyed by content hash.
This is used to seed the EmbeddingsCache after a full build so that
the first incremental update achieves ~100% cache hit rate for unchanged
chunks.
Each document's content is MD5-hashed to produce the key. If the
document metadata already contains a content_hash field it is used
directly; otherwise the hash is computed on the fly.
Returns:
| Type | Description |
|---|---|
dict[str, ndarray]
|
Dict mapping content_hash → np.ndarray (float32 vectors). |
Source code in codenib/index/embedding/vector_store.py
rebuild_from_embeddings
¶
Clear level and rebuild its FAISS index from pre-computed embeddings.
Used by the incremental update path: unchanged chunks contribute their cached vectors, so only genuinely new/modified chunks require model inference. No embedding model calls are made by this method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
list
|
Document-like objects with |
required |
embeddings
|
list[ndarray]
|
Corresponding embedding vectors as |
required |
level
|
Level
|
Which index level to rebuild ( |
'l2'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If documents and embeddings have different lengths. |
Source code in codenib/index/embedding/vector_store.py
delta_update
¶
delta_update(
all_documents: list,
all_embeddings: list[ndarray],
changed_content_hashes: set[str],
level: Level = "l2",
threshold: float = 0.1,
) -> None
Patch a flat FAISS index in place when the change set is small.
When the fraction of changed chunks is below threshold, this uses
IndexFlat.remove_ids + add to modify only the affected rows,
keeping unchanged vectors and their aligned documents untouched. IVF
indexes are rebuilt because removing their implicit IDs does not
compact the remaining labels to match the document array. If the
change ratio exceeds the threshold (or the index is empty), this also
falls back to :meth:rebuild_from_embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
all_documents
|
list
|
The complete desired set of documents for level
after the update. Must carry |
required |
all_embeddings
|
list[ndarray]
|
Corresponding embedding vectors, aligned with all_documents. |
required |
changed_content_hashes
|
set[str]
|
Content hashes of chunks that were added, removed, or modified in this update cycle. Used both to decide between delta/rebuild and to identify stale rows. |
required |
level
|
Level
|
Which index level to update. |
'l2'
|
threshold
|
float
|
Maximum change ratio (changed/total) for the delta path; above this a full rebuild is performed. |
0.1
|
Source code in codenib/index/embedding/vector_store.py
2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 | |
clear
¶
Clear data from the vector store.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
Level | None
|
If specified, only clear that level ("l0" or "l2"). If None, clear both levels. |
None
|
Source code in codenib/index/embedding/vector_store.py
build_hierarchical_vector_store
¶
build_hierarchical_vector_store(
*,
repo_path: str,
index_path: str,
plan_name: str | None = None,
languages: list[str] | None = None,
max_lines_per_chunk: int | None = None,
build_levels: list[str] | None = None,
embedding_model: str,
embedding_provider: str,
embedding_dimension: int | None,
embedding_kwargs: dict[str, object] | None = None,
embedding: object | None = None,
index_metric: str = "ip",
index_type: str = "flat",
ivf_nlist: int = 100,
ivf_nprobe: int = 8,
profiler: Profiler | None = None,
force_rebuild: bool = False,
strict_chunking: bool = False,
additional_ignore_dirs: list[str] | None = None,
source_selection: RepositorySourceSelection | None = None,
artifact_metadata: dict[str, Any] | None = None,
native_index_authorization: NativeIndexAuthorization | None = None,
_atomic_publish: bool = True,
_build_root_guard: _PrivateBuildDirectory | None = None
) -> CodeVectorStore
Build (or load) a hierarchical vector store (L0/L2) for a repository.
When force_rebuild=False (the default) and a saved index for the
requested embedding_model already exists at index_path, the store
is loaded from disk instead of re-chunking and re-embedding the repository.
Pass force_rebuild=True to unconditionally rebuild. Rebuilds compose a
complete private tree and atomically switch the directory only after save
and ownership verification succeed.
Source code in codenib/index/embedding/builders.py
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 | |
create_code_vector_store
¶
create_code_vector_store(
embedding_model: str = "text-embedding-ada-002",
embedding_provider: str = "openai",
store_path: str | None = None,
**kwargs
) -> CodeVectorStore
Factory function to create a CodeVectorStore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_model
|
str
|
Name of the embedding model |
'text-embedding-ada-002'
|
embedding_provider
|
str
|
Provider for embeddings |
'openai'
|
store_path
|
str | None
|
Path to store/load the vector store |
None
|
**kwargs
|
Additional arguments for CodeVectorStore |
{}
|
Returns:
| Type | Description |
|---|---|
CodeVectorStore
|
CodeVectorStore instance |