|
57 | 57 | sanitize_content, |
58 | 58 | ) |
59 | 59 | from .version import __version__ # noqa: E402 |
60 | | -from .backends.chroma import ChromaBackend, ChromaCollection # noqa: E402 |
| 60 | +from .backends.chroma import ChromaBackend, ChromaCollection, _pin_hnsw_threads # noqa: E402 |
61 | 61 | from .query_sanitizer import sanitize_query # noqa: E402 |
62 | 62 | from .searcher import search_memories # noqa: E402 |
63 | 63 | from .palace_graph import ( # noqa: E402 |
@@ -219,20 +219,23 @@ def _get_collection(create=False): |
219 | 219 | if create: |
220 | 220 | # hnsw:num_threads=1 disables ChromaDB's multi-threaded ParallelFor |
221 | 221 | # HNSW insert path, which has a race in repairConnectionsForUpdate / |
222 | | - # addPoint (see issues #974, #965). The setting is only honored at |
223 | | - # collection creation time — pre-existing palaces created before |
224 | | - # this fix keep the unsafe default; users must `mempalace nuke` + |
225 | | - # re-mine to get the protection on legacy palaces. |
226 | | - _collection_cache = ChromaCollection( |
227 | | - client.get_or_create_collection( |
228 | | - _config.collection_name, |
229 | | - metadata={"hnsw:space": "cosine", "hnsw:num_threads": 1}, |
230 | | - ) |
| 222 | + # addPoint (see issues #974, #965). Set via metadata on fresh |
| 223 | + # collections and re-applied via _pin_hnsw_threads() for legacy |
| 224 | + # palaces whose collections were created before this fix (the |
| 225 | + # runtime config does not persist cross-process in chromadb 1.5.x, |
| 226 | + # so the retrofit runs every time _get_collection opens a cache). |
| 227 | + raw = client.get_or_create_collection( |
| 228 | + _config.collection_name, |
| 229 | + metadata={"hnsw:space": "cosine", "hnsw:num_threads": 1}, |
231 | 230 | ) |
| 231 | + _pin_hnsw_threads(raw) |
| 232 | + _collection_cache = ChromaCollection(raw) |
232 | 233 | _metadata_cache = None |
233 | 234 | _metadata_cache_time = 0 |
234 | 235 | elif _collection_cache is None: |
235 | | - _collection_cache = ChromaCollection(client.get_collection(_config.collection_name)) |
| 236 | + raw = client.get_collection(_config.collection_name) |
| 237 | + _pin_hnsw_threads(raw) |
| 238 | + _collection_cache = ChromaCollection(raw) |
236 | 239 | _metadata_cache = None |
237 | 240 | _metadata_cache_time = 0 |
238 | 241 | return _collection_cache |
|
0 commit comments