@delali/narsil 0.2.4-next.59 → 0.3.1-next.101

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. package/README.md +9 -8
  2. package/dist/adapters/filesystem.d.ts +1 -1
  3. package/dist/adapters/filesystem.mjs +1 -1
  4. package/dist/adapters/indexeddb.d.ts +1 -1
  5. package/dist/adapters/indexeddb.mjs +1 -1
  6. package/dist/adapters/memory.d.ts +1 -1
  7. package/dist/{adapters-79OIa4xm.d.ts → adapters-BQFQ4PRz.d.ts} +27 -1
  8. package/dist/browser/chunk-BDOSMEEP.mjs +493 -0
  9. package/dist/browser/chunk-EJDUFWSO.mjs +602 -0
  10. package/dist/browser/chunk-HOZW3PKP.mjs +9 -0
  11. package/dist/browser/chunk-JOVZ6HMT.mjs +4358 -0
  12. package/dist/browser/chunk-JPTQZWJQ.mjs +10 -0
  13. package/dist/browser/chunk-KPJMDPHG.mjs +24558 -0
  14. package/dist/browser/chunk-PGIM3HIM.mjs +2743 -0
  15. package/dist/browser/chunk-RHG4O77A.mjs +206 -0
  16. package/dist/browser/chunk-RIJSLY2I.mjs +360 -0
  17. package/dist/browser/durability-wiring-SRUYT3VV.mjs +3875 -0
  18. package/dist/browser/module-CBB2ETYP.mjs +3111 -0
  19. package/dist/browser/release-76MXRPGV.mjs +33 -0
  20. package/dist/browser/replace-RURXTGVG.mjs +57 -0
  21. package/dist/browser/restore-queue-EEZQO6RF.mjs +35 -0
  22. package/dist/browser/runtime-7ZAHCHWR.mjs +557 -0
  23. package/dist/browser/snapshot-OAAA5UJ7.mjs +1210 -0
  24. package/dist/browser/write-7D6VIK6O.mjs +208 -0
  25. package/dist/{capabilities-DSsOGz6F.d.ts → capabilities-DiGlB6Sr.d.ts} +68 -7
  26. package/dist/chunk-2RIRGOUL.mjs +113 -0
  27. package/dist/chunk-37CD3OKS.mjs +87 -0
  28. package/dist/chunk-44W3CBNG.mjs +7 -0
  29. package/dist/chunk-46D6JQIG.mjs +426 -0
  30. package/dist/{chunk-VMRR3DQU.mjs → chunk-5JNA5SKS.mjs} +546 -35
  31. package/dist/chunk-747OHOUA.mjs +11 -0
  32. package/dist/chunk-75XIEF6H.mjs +126 -0
  33. package/dist/chunk-7X5JST5L.mjs +114 -0
  34. package/dist/chunk-BOFBLJFZ.mjs +209 -0
  35. package/dist/{chunk-USKFU7J5.mjs → chunk-BWJVNZPR.mjs} +8 -1
  36. package/dist/chunk-C7SLA6CM.mjs +19 -0
  37. package/dist/{chunk-POWOTUKJ.mjs → chunk-CQUMNIIG.mjs} +3 -6
  38. package/dist/chunk-CVXLLEQ3.mjs +1011 -0
  39. package/dist/chunk-DJHHF2L5.mjs +17 -0
  40. package/dist/chunk-E74YLAIG.mjs +24 -0
  41. package/dist/{chunk-UXNWQZST.mjs → chunk-EMRMGYRE.mjs} +236 -18
  42. package/dist/chunk-EWZLIYHV.mjs +57 -0
  43. package/dist/chunk-GBEEG5AN.mjs +35 -0
  44. package/dist/chunk-GES7U2PM.mjs +68 -0
  45. package/dist/chunk-HJSN7UYG.mjs +10 -0
  46. package/dist/chunk-HLPRHOSO.mjs +17 -0
  47. package/dist/chunk-HMFNFVP7.mjs +14 -0
  48. package/dist/chunk-J5QP52HA.mjs +1751 -0
  49. package/dist/{chunk-YNVIUGF5.mjs → chunk-JZVARCRZ.mjs} +3 -28
  50. package/dist/chunk-KOKM4PQN.mjs +52 -0
  51. package/dist/{chunk-B2TFROFX.mjs → chunk-KWTW6VTG.mjs} +465 -250
  52. package/dist/chunk-KZ3NX2P7.mjs +21 -0
  53. package/dist/chunk-L5ORED7V.mjs +25 -0
  54. package/dist/{chunk-THYRHHO2.mjs → chunk-LGAMKHA2.mjs} +2009 -2351
  55. package/dist/chunk-LKOB5COA.mjs +117 -0
  56. package/dist/chunk-LPQOEU5M.mjs +96 -0
  57. package/dist/{chunk-3LHGNRYE.mjs → chunk-LXWDQLTW.mjs} +7 -2
  58. package/dist/{chunk-L6TNPLAR.mjs → chunk-OEYI3KRB.mjs} +270 -108
  59. package/dist/chunk-OM362BRO.mjs +948 -0
  60. package/dist/chunk-OZDFA4WC.mjs +67 -0
  61. package/dist/chunk-P36MUFEJ.mjs +10 -0
  62. package/dist/chunk-PC2G76VW.mjs +2540 -0
  63. package/dist/{chunk-GCM3JFVA.mjs → chunk-PNEYIHA4.mjs} +11 -2
  64. package/dist/chunk-PNFKUCG3.mjs +1130 -0
  65. package/dist/chunk-Q6XWVUZX.mjs +44 -0
  66. package/dist/chunk-R27EMH5M.mjs +617 -0
  67. package/dist/{chunk-YMZCMH5M.mjs → chunk-R6A773WK.mjs} +19 -24
  68. package/dist/chunk-RF7PLG43.mjs +106 -0
  69. package/dist/chunk-RFBLQLLZ.mjs +16 -0
  70. package/dist/{chunk-Y3L5MDRV.mjs → chunk-RFVUDM5E.mjs} +17 -2
  71. package/dist/chunk-S7FL6UAH.mjs +521 -0
  72. package/dist/chunk-S7GNDRPS.mjs +651 -0
  73. package/dist/chunk-SP3NM3ZG.mjs +3537 -0
  74. package/dist/chunk-U476IXKQ.mjs +10 -0
  75. package/dist/chunk-UA3AUGNV.mjs +462 -0
  76. package/dist/chunk-UTG5YNXV.mjs +507 -0
  77. package/dist/chunk-VI4R6W25.mjs +8484 -0
  78. package/dist/chunk-WQBCBKY3.mjs +8 -0
  79. package/dist/{chunk-24RXHMEL.mjs → chunk-WVCOCKBU.mjs} +3794 -7570
  80. package/dist/chunk-X5CV2E2B.mjs +1 -0
  81. package/dist/chunk-XADZPPZI.mjs +614 -0
  82. package/dist/chunk-YEEQHT4V.mjs +212 -0
  83. package/dist/chunk-YTJCGKM4.mjs +56 -0
  84. package/dist/chunk-YYQIODMF.mjs +275 -0
  85. package/dist/chunk-ZI34TVOA.mjs +144 -0
  86. package/dist/{chunk-OOSIDHA3.mjs → chunk-ZKWYWW4Y.mjs} +1 -1
  87. package/dist/chunk-ZM7KU4YW.mjs +35 -0
  88. package/dist/{client-BQK6Tf6A.d.ts → client-BOStAkc5.d.ts} +160 -37
  89. package/dist/client.d.ts +11 -10
  90. package/dist/client.mjs +209 -45
  91. package/dist/{config-DmXULPa-.d.ts → config-Cjvzbs_j.d.ts} +51 -3
  92. package/dist/config-DffQlmRq.d.ts +421 -0
  93. package/dist/direct-executor-ZD64PPEZ.mjs +24 -0
  94. package/dist/distribution/coordinator/etcd.d.ts +79 -18
  95. package/dist/distribution/coordinator/etcd.mjs +553 -109
  96. package/dist/distribution/coordinator/in-memory.d.ts +4 -3
  97. package/dist/distribution/coordinator/in-memory.mjs +22 -3
  98. package/dist/distribution/transport/grpc.d.ts +2 -2
  99. package/dist/distribution/transport/grpc.mjs +79 -14
  100. package/dist/distribution/transport/in-memory.d.ts +2 -2
  101. package/dist/distribution/transport/in-memory.mjs +12 -7
  102. package/dist/distribution/transport/tcp.d.ts +2 -2
  103. package/dist/distribution/transport/tcp.mjs +50 -28
  104. package/dist/distribution.d.ts +144 -23
  105. package/dist/distribution.mjs +14479 -8173
  106. package/dist/durability-wiring-FEUBR5BT.mjs +3330 -0
  107. package/dist/embeddings/openai.d.ts +2 -2
  108. package/dist/embeddings/openai.mjs +22 -133
  109. package/dist/{engine-CIOyFozK.d.ts → engine-rC0vjjY-.d.ts} +165 -141
  110. package/dist/{memory-yFaVn2xZ.d.ts → events-DqOIg5xa.d.ts} +645 -136
  111. package/dist/{types-DAK7mnUZ.d.ts → index-Bkdt8nDw.d.ts} +182 -45
  112. package/dist/index.browser.mjs +4 -32180
  113. package/dist/index.d.ts +91 -20
  114. package/dist/index.mjs +33 -16
  115. package/dist/invalidation/broadcast-channel.d.ts +1 -1
  116. package/dist/invalidation/filesystem.d.ts +1 -1
  117. package/dist/invalidation/filesystem.mjs +2 -1
  118. package/dist/invalidation/noop.d.ts +1 -1
  119. package/dist/module-3S65MVCM.mjs +2151 -0
  120. package/dist/partial-words-DTIHO8G4.d.ts +19 -0
  121. package/dist/partial-words.d.ts +22 -0
  122. package/dist/partial-words.mjs +17 -0
  123. package/dist/pattern-Cledccbl.d.ts +18 -0
  124. package/dist/pattern.d.ts +22 -0
  125. package/dist/pattern.mjs +12 -0
  126. package/dist/persistence/durability/checkpoint-worker.d.ts +119 -72
  127. package/dist/persistence/durability/checkpoint-worker.mjs +51 -18
  128. package/dist/react.d.ts +279 -183
  129. package/dist/react.mjs +201 -48
  130. package/dist/release-BH3DKW3T.mjs +33 -0
  131. package/dist/replace-CEO5KJJ3.mjs +57 -0
  132. package/dist/rerank-K7C4WZFG.mjs +60 -0
  133. package/dist/rerankers/http.d.ts +50 -0
  134. package/dist/rerankers/http.mjs +104 -0
  135. package/dist/restore-queue-KQHA7ZV2.mjs +35 -0
  136. package/dist/runtime-6Y4ZYXOG.mjs +177 -0
  137. package/dist/{schema-d5Afu03A.d.ts → schema-4W_7tk1f.d.ts} +81 -11
  138. package/dist/server.d.ts +13 -12
  139. package/dist/server.mjs +611 -80
  140. package/dist/snapshot-QGZPODM5.mjs +799 -0
  141. package/dist/synonyms-CpOAHPa-.d.ts +52 -0
  142. package/dist/synonyms.d.ts +22 -0
  143. package/dist/synonyms.mjs +12 -0
  144. package/dist/{tasks-CH2Dod8w.d.ts → tasks-CwPTz-tP.d.ts} +10 -3
  145. package/dist/thread-N3IUGUS3.mjs +697 -0
  146. package/dist/{types-BQDlIvfS.d.ts → types-2qoitTXg.d.ts} +1 -1
  147. package/dist/{types-D5OwhtsQ.d.ts → types-Cz5okLml.d.ts} +2 -2
  148. package/dist/vector/search-worker.d.ts +46 -4
  149. package/dist/vector/search-worker.mjs +21 -11
  150. package/dist/workers/entry.mjs +9 -6
  151. package/dist/write-U5RI7MAX.mjs +241 -0
  152. package/package.json +55 -9
  153. package/dist/chunk-3L4U6EJQ.mjs +0 -49
  154. package/dist/chunk-IFBKLBGB.mjs +0 -7
  155. package/dist/chunk-ORBLD3EL.mjs +0 -6
  156. package/dist/chunk-U7KYW4BA.mjs +0 -135
  157. package/dist/chunk-ZJ4CSAB2.mjs +0 -13
  158. package/dist/chunk-ZNZBXMP5.mjs +0 -2117
  159. package/dist/chunk-ZZT3EAUC.mjs +0 -11168
  160. package/dist/config-BhldZgaK.d.ts +0 -194
  161. package/dist/direct-executor-ALAVE7FC.mjs +0 -12
  162. package/dist/serialization/crc32-worker.d.ts +0 -20
  163. package/dist/serialization/crc32-worker.mjs +0 -62
  164. package/dist/simd-DW5AtJ6B.d.ts +0 -41
  165. package/dist/thread-DYHEQRVY.mjs +0 -402
@@ -0,0 +1,2151 @@
1
+ import { VECTOR_WORKER_COPIES_ALLOWED, DEFAULT_PROMOTION_THRESHOLD, DEFAULT_FILTER_THRESHOLD, noBuildHistory, recordCheckpoint, planCheckpoint, deserialize, assignStorePartitions, liveSize, ordinalFilterForSubsets, handOverBuildTurns, threadsHoldCurrentLayout, filterForOptions, OSQ4_MIN_DIMENSION, emptyFieldBeforeRestore, ESTIMATED_MS_PER_TOMBSTONE, graphNeedsRebuild, ESTIMATED_MS_PER_VECTOR_REBUILD, releaseReplacedVectors, yieldBuildTurn, WORKER_COPY_MIN_VECTORS, allLiveDocIds, recalibrateFromStore, signatureOf, adoptGraph, BUILD_RETRY_MAX_DELAY_MS, BUILD_RETRY_FIRST_DELAY_MS, fieldHandlesOf, liveOrdinals, RECALIBRATION_CHUNK_VECTORS, BUILD_CHUNK_SIZE, DRIFT_CHECK_GROWTH_FACTOR, DISK_ADOPTION_YIELD_INTERVAL, adoptLoadedCalibration, RECALIBRATION_DRIFT_RATIO } from './chunk-S7GNDRPS.mjs';
2
+ import { isDead, NO_LIVE_VECTOR_AT_POSITION, bytesToVectors } from './chunk-YYQIODMF.mjs';
3
+ import { createVectorStore } from './chunk-CVXLLEQ3.mjs';
4
+ export { openSharedVectorField } from './chunk-CVXLLEQ3.mjs';
5
+ import { osqBitsOf, createOsqQuantizer, STORE_ORDINAL_LAYOUT_GENERATION, ordinalFilterValues, STORE_UNPLACED_COUNT, STORE_LIVE_COUNT, STORE_REMOVED_COUNT, createOrdinalFilter, addToOrdinalFilter, nativeScoresOf, toScore, ordinalFilterHas, magnitude, nativeStoreScratchBytes, toDistance, euclideanDistance, dotProduct, cosineSimilarityWithMagnitudes, createHNSWIndex, topLayerInParts, sharedMemoryAvailable, requireWholeNeighbourLists, shuffleInPlace, createCentroidSums, centroidOfCentroidSums, meanOfCentroidSums, countOfCentroidSums, squaredLengthsOfCentroidSums, releaseLocksHeldBy } from './chunk-LGAMKHA2.mjs';
6
+ export { releaseLocksHeldBy } from './chunk-LGAMKHA2.mjs';
7
+ import { noteFallback } from './chunk-DJHHF2L5.mjs';
8
+ import './chunk-37CD3OKS.mjs';
9
+ import { spawnNodeWorker } from './chunk-C7SLA6CM.mjs';
10
+ import { resolveWorkerCount, resolveWorkerEntry, workerResourceLimits } from './chunk-OZDFA4WC.mjs';
11
+ import { detectRuntime } from './chunk-AV4IJKD2.mjs';
12
+ import './chunk-LXWDQLTW.mjs';
13
+ import './chunk-KOKM4PQN.mjs';
14
+ import { MAX_RENUMBERED_SEARCH_ATTEMPTS, DISK_STORAGE_BLOCK_BYTES, UNPLACED_FLAG_CHUNK_ORDINALS, GRAPH_BUILD_CHUNKS_IN_FLIGHT_PER_WORKER, GRAPH_BUILD_DISPATCH_CHUNK, VECTOR_SEARCH_TIMEOUT_MS, VECTOR_SEARCH_LOAD_TIMEOUT_MS, VECTOR_SCRATCH_SLOTS, VECTOR_INSERT_TIMEOUT_MS } from './chunk-GBEEG5AN.mjs';
15
+ import './chunk-JZVARCRZ.mjs';
16
+ import './chunk-U476IXKQ.mjs';
17
+ import './chunk-E74YLAIG.mjs';
18
+ import './chunk-HMFNFVP7.mjs';
19
+ import './chunk-R6A773WK.mjs';
20
+ import { compareCodePoints } from './chunk-YTJCGKM4.mjs';
21
+ import { noteThreadSlotLive, noteThreadSlotGone } from './chunk-RFBLQLLZ.mjs';
22
+ import { NarsilError, ErrorCodes, isNarsilError } from './chunk-BWJVNZPR.mjs';
23
+
24
+ // src/core/heap.ts
25
+ function createMaxHeap(compare) {
26
+ return createHeap((a, b) => compare(b, a));
27
+ }
28
+ function createBoundedMaxHeap(compare, capacity) {
29
+ const heap = createMaxHeap(compare);
30
+ const innerPush = heap.push;
31
+ return {
32
+ push(item) {
33
+ if (heap.size < capacity) {
34
+ innerPush(item);
35
+ return;
36
+ }
37
+ const top = heap.peek();
38
+ if (top !== void 0 && compare(item, top) < 0) {
39
+ heap.pop();
40
+ innerPush(item);
41
+ }
42
+ },
43
+ pop: heap.pop,
44
+ peek: heap.peek,
45
+ get size() {
46
+ return heap.size;
47
+ },
48
+ toSortedArray: heap.toSortedArray
49
+ };
50
+ }
51
+ function createHeap(compare) {
52
+ const items = [];
53
+ function siftUp(idx) {
54
+ while (idx > 0) {
55
+ const parent = idx - 1 >> 1;
56
+ if (compare(items[idx], items[parent]) >= 0) break;
57
+ const tmp = items[idx];
58
+ items[idx] = items[parent];
59
+ items[parent] = tmp;
60
+ idx = parent;
61
+ }
62
+ }
63
+ function siftDown(idx) {
64
+ const len = items.length;
65
+ while (true) {
66
+ let smallest = idx;
67
+ const left = 2 * idx + 1;
68
+ const right = 2 * idx + 2;
69
+ if (left < len && compare(items[left], items[smallest]) < 0) smallest = left;
70
+ if (right < len && compare(items[right], items[smallest]) < 0) smallest = right;
71
+ if (smallest === idx) break;
72
+ const tmp = items[idx];
73
+ items[idx] = items[smallest];
74
+ items[smallest] = tmp;
75
+ idx = smallest;
76
+ }
77
+ }
78
+ return {
79
+ push(item) {
80
+ items.push(item);
81
+ siftUp(items.length - 1);
82
+ },
83
+ pop() {
84
+ if (items.length === 0) return void 0;
85
+ const top = items[0];
86
+ const last = items.pop();
87
+ if (items.length > 0) {
88
+ items[0] = last;
89
+ siftDown(0);
90
+ }
91
+ return top;
92
+ },
93
+ peek() {
94
+ return items[0];
95
+ },
96
+ get size() {
97
+ return items.length;
98
+ },
99
+ toSortedArray() {
100
+ return [...items].sort(compare);
101
+ }
102
+ };
103
+ }
104
+
105
+ // src/vector/vector-index/merge-results.ts
106
+ function highScoreFirst(a, b) {
107
+ return b.score - a.score || compareCodePoints(a.docId, b.docId);
108
+ }
109
+ function mergeResults(hnswResults, bufferResults, k) {
110
+ const seen = /* @__PURE__ */ new Set();
111
+ const merged = [];
112
+ let hi = 0;
113
+ let bi = 0;
114
+ while (merged.length < k && (hi < hnswResults.length || bi < bufferResults.length)) {
115
+ const h = hi < hnswResults.length ? hnswResults[hi] : void 0;
116
+ const b = bi < bufferResults.length ? bufferResults[bi] : void 0;
117
+ let pick;
118
+ if (h && b) {
119
+ if (h.score > b.score || h.score === b.score && compareCodePoints(h.docId, b.docId) < 0) {
120
+ pick = h;
121
+ hi++;
122
+ } else {
123
+ pick = b;
124
+ bi++;
125
+ }
126
+ } else if (h) {
127
+ pick = h;
128
+ hi++;
129
+ } else if (b) {
130
+ pick = b;
131
+ bi++;
132
+ } else {
133
+ break;
134
+ }
135
+ if (seen.has(pick.docId)) continue;
136
+ seen.add(pick.docId);
137
+ merged.push(pick);
138
+ }
139
+ return merged;
140
+ }
141
+
142
+ // src/vector/shared-field/store-flags.ts
143
+ function bytesOf(flags, source) {
144
+ if (flags.source !== source) {
145
+ flags.source = source;
146
+ flags.view = new Uint8Array(source);
147
+ }
148
+ return flags.view;
149
+ }
150
+ function countsOf(chunks, source) {
151
+ if (chunks.source !== source) {
152
+ chunks.source = source;
153
+ chunks.view = new Int32Array(source);
154
+ }
155
+ return chunks.view;
156
+ }
157
+ function readStoreFlags(store) {
158
+ const unplaced = { source: null, view: new Uint8Array(0) };
159
+ const unplacedChunks = { source: null, view: new Int32Array(0) };
160
+ const removed = { source: null, view: new Uint8Array(0) };
161
+ return {
162
+ liveDocuments() {
163
+ const header = store.handles.header;
164
+ return Atomics.load(header, STORE_LIVE_COUNT) - Atomics.load(header, STORE_REMOVED_COUNT);
165
+ },
166
+ isRemoved(ordinal) {
167
+ const bytes = bytesOf(removed, store.handles.removed);
168
+ return ordinal < bytes.length && Atomics.load(bytes, ordinal) === 1;
169
+ },
170
+ unplacedOrdinals() {
171
+ const handles = store.handles;
172
+ if (Atomics.load(handles.header, STORE_UNPLACED_COUNT) === 0) return [];
173
+ const bytes = bytesOf(unplaced, handles.unplaced);
174
+ const counts = countsOf(unplacedChunks, handles.unplacedChunks);
175
+ const end = Math.min(store.slots, bytes.length);
176
+ const ordinals = [];
177
+ for (let chunk = 0; chunk < counts.length; chunk++) {
178
+ const from = chunk * UNPLACED_FLAG_CHUNK_ORDINALS;
179
+ if (from >= end) break;
180
+ if (Atomics.load(counts, chunk) === 0) continue;
181
+ const to = Math.min(end, from + UNPLACED_FLAG_CHUNK_ORDINALS);
182
+ for (let ordinal = from; ordinal < to; ordinal++) {
183
+ if (Atomics.load(bytes, ordinal) === 1) ordinals.push(ordinal);
184
+ }
185
+ }
186
+ return ordinals;
187
+ }
188
+ };
189
+ }
190
+
191
+ // src/vector/shared-field/searcher.ts
192
+ function distancesInTypeScript(store, query, metric, ordinals) {
193
+ noteFallback("score");
194
+ const arenaQuery = store.prepareQueryArena(query);
195
+ const queryMagnitude = arenaQuery ? arenaQuery.magnitude : magnitude(query);
196
+ const distances = new Float64Array(ordinals.length).fill(Number.POSITIVE_INFINITY);
197
+ for (let i = 0; i < ordinals.length; i++) {
198
+ if (arenaQuery) {
199
+ distances[i] = store.distanceFromArena(arenaQuery, ordinals[i], metric);
200
+ continue;
201
+ }
202
+ const entry = store.entryForOrdinal(ordinals[i]);
203
+ if (entry) distances[i] = toDistance(query, entry.vector, queryMagnitude, entry.magnitude, metric);
204
+ }
205
+ return distances;
206
+ }
207
+ function scoreCandidates(store, candidates, query, k, metric, minSimilarity) {
208
+ const { ordinals, docIds } = candidates;
209
+ if (ordinals.length === 0) return { results: [], matched: 0 };
210
+ const distances = nativeScoresOf(store.handles, query, metric, ordinals) ?? distancesInTypeScript(store, query, metric, ordinals);
211
+ const heap = createBoundedMaxHeap(highScoreFirst, k);
212
+ let matched = 0;
213
+ for (let i = 0; i < ordinals.length; i++) {
214
+ if (distances[i] === Number.POSITIVE_INFINITY) continue;
215
+ const score = toScore(distances[i], metric);
216
+ if (score < minSimilarity) continue;
217
+ matched++;
218
+ heap.push({ docId: docIds[i], score });
219
+ }
220
+ return { results: heap.toSortedArray().reverse(), matched };
221
+ }
222
+ function createSharedVectorSearcher(options) {
223
+ const { fieldName, view, holdsDocument } = options;
224
+ const flags = readStoreFlags(view.store);
225
+ function ordinalLayoutGeneration() {
226
+ return Atomics.load(view.store.handles.header, STORE_ORDINAL_LAYOUT_GENERATION);
227
+ }
228
+ function ordinalSpace() {
229
+ const store = view.store;
230
+ const layoutGeneration = ordinalLayoutGeneration();
231
+ return {
232
+ fieldName,
233
+ layoutGeneration,
234
+ slots: store.slots,
235
+ header: store.handles.header,
236
+ ordinalOf: (docId) => view.ordinalOf(docId),
237
+ holdsCurrentOrdinal: (ordinal) => store.holdsOrdinal(ordinal) && store.partitionAt(ordinal) !== void 0,
238
+ isRemoved: (ordinal) => flags.isRemoved(ordinal)
239
+ };
240
+ }
241
+ function filterForPartitions(allowed) {
242
+ const slots = view.store.slots;
243
+ const filter = createOrdinalFilter(slots);
244
+ for (let ordinal = 0; ordinal < slots; ordinal++) {
245
+ const partition = view.store.partitionAt(ordinal);
246
+ if (partition === void 0 || !allowed.has(partition)) continue;
247
+ if (!view.store.holdsOrdinal(ordinal) || flags.isRemoved(ordinal)) continue;
248
+ addToOrdinalFilter(filter, ordinal);
249
+ }
250
+ return filter;
251
+ }
252
+ function filterFor(searchOptions) {
253
+ if (searchOptions.filterSubsets !== void 0) {
254
+ return ordinalFilterForSubsets(searchOptions.filterSubsets, ordinalSpace());
255
+ }
256
+ if (searchOptions.filterPartitions !== void 0) return filterForPartitions(searchOptions.filterPartitions);
257
+ return void 0;
258
+ }
259
+ function candidatesOf(ordinals, filter) {
260
+ const candidates = { ordinals: [], docIds: [] };
261
+ for (const ordinal of ordinals) {
262
+ if (filter !== void 0 && !ordinalFilterHas(filter, ordinal)) continue;
263
+ if (flags.isRemoved(ordinal)) continue;
264
+ const docId = view.docIdOf(ordinal);
265
+ if (docId === void 0 || view.ordinalOf(docId) !== ordinal) continue;
266
+ candidates.ordinals.push(ordinal);
267
+ candidates.docIds.push(docId);
268
+ }
269
+ return candidates;
270
+ }
271
+ function answerOf(outcome) {
272
+ const results = outcome.results.filter((result) => holdsDocument(result.docId));
273
+ const holdsEveryHit = outcome.resolvedEveryHit && results.length === outcome.results.length;
274
+ return { results, matched: outcome.matched, matchedExact: outcome.matchedExact && holdsEveryHit };
275
+ }
276
+ function scanned(scan) {
277
+ return { results: scan.results, matched: scan.matched, matchedExact: true, resolvedEveryHit: true };
278
+ }
279
+ function search2(query, k, searchOptions, filter) {
280
+ const { metric, minSimilarity, efSearch, oversample, rescoreWindow } = searchOptions;
281
+ const empty = { results: [], matched: 0, matchedExact: true, resolvedEveryHit: true };
282
+ const liveDocuments = flags.liveDocuments();
283
+ if (liveDocuments <= 0) return empty;
284
+ if (filter !== void 0 && filter.count === 0) return empty;
285
+ function fetched(results, resolvedEveryHit2) {
286
+ if (minSimilarity === Number.NEGATIVE_INFINITY) {
287
+ const matched = filter === void 0 ? liveDocuments : filter.count;
288
+ return { results, matched, matchedExact: true, resolvedEveryHit: resolvedEveryHit2 };
289
+ }
290
+ return { results, matched: results.length, matchedExact: false, resolvedEveryHit: resolvedEveryHit2 };
291
+ }
292
+ if (k <= 0) return fetched([], true);
293
+ const graph = view.graph;
294
+ if (graph === null) {
295
+ const ordinals = filter === void 0 ? flags.unplacedOrdinals() : ordinalFilterValues(filter);
296
+ return scanned(scoreCandidates(view.store, candidatesOf(ordinals, filter), query, k, metric, minSimilarity));
297
+ }
298
+ if (filter !== void 0) {
299
+ const graphLiveSize = view.liveSize;
300
+ const selectivity = graphLiveSize > 0 ? filter.count / graphLiveSize : 1;
301
+ if (selectivity < view.handles.filterThreshold) {
302
+ const candidates = candidatesOf(ordinalFilterValues(filter), filter);
303
+ return scanned(scoreCandidates(view.store, candidates, query, k, metric, minSimilarity));
304
+ }
305
+ }
306
+ let resolvedEveryHit = true;
307
+ const graphResults = [];
308
+ for (const hit of view.searchOrdinals(query, k, metric, minSimilarity, {
309
+ filter,
310
+ efSearch,
311
+ oversample,
312
+ rescoreWindow
313
+ })) {
314
+ const docId = view.docIdOf(hit.ord);
315
+ if (docId === void 0) {
316
+ resolvedEveryHit = false;
317
+ continue;
318
+ }
319
+ graphResults.push({ docId, score: hit.score });
320
+ }
321
+ const unplaced = flags.unplacedOrdinals();
322
+ if (unplaced.length === 0) return fetched(graphResults, resolvedEveryHit);
323
+ const scan = scoreCandidates(view.store, candidatesOf(unplaced, filter), query, k, metric, minSimilarity);
324
+ return fetched(mergeResults(graphResults, scan.results, k), resolvedEveryHit);
325
+ }
326
+ return {
327
+ fieldName,
328
+ dimension: view.handles.dimension,
329
+ partitionsKnown: () => true,
330
+ assignPartitions: () => void 0,
331
+ searchParallel(query, k, searchOptions) {
332
+ if (query.length !== view.handles.dimension) {
333
+ throw new NarsilError(
334
+ ErrorCodes.VECTOR_DIMENSION_MISMATCH,
335
+ `Vector dimension mismatch: expected ${view.handles.dimension}, got ${query.length}`,
336
+ { expected: view.handles.dimension, received: query.length }
337
+ );
338
+ }
339
+ for (let attempt = 1; ; attempt++) {
340
+ const layoutGeneration = ordinalLayoutGeneration();
341
+ const outcome = search2(query, k, searchOptions, filterFor(searchOptions));
342
+ if (view.graphRetired) {
343
+ throw new NarsilError(
344
+ ErrorCodes.WORKER_BUSY,
345
+ `The main thread replaced the graph of vector field "${fieldName}" while this thread searched it`,
346
+ { fieldName }
347
+ );
348
+ }
349
+ if (ordinalLayoutGeneration() === layoutGeneration) return Promise.resolve(answerOf(outcome));
350
+ if (attempt >= MAX_RENUMBERED_SEARCH_ATTEMPTS) {
351
+ throw new NarsilError(
352
+ ErrorCodes.WORKER_BUSY,
353
+ `The main thread renumbered the vectors of field "${fieldName}" during each of ${attempt} searches on this thread`,
354
+ { fieldName, attempts: attempt }
355
+ );
356
+ }
357
+ }
358
+ }
359
+ };
360
+ }
361
+
362
+ // src/vector/search-pool/constants.ts
363
+ var VECTOR_POOL_DRAIN_TIMEOUT_MS = 1e4;
364
+
365
+ // src/vector/search-pool/slot.ts
366
+ function createSlot(worker, scratchSlot) {
367
+ noteThreadSlotLive(scratchSlot);
368
+ return {
369
+ worker,
370
+ scratchSlot,
371
+ pending: /* @__PURE__ */ new Map(),
372
+ alive: true,
373
+ outstanding: 0,
374
+ graphs: /* @__PURE__ */ new Map(),
375
+ working: /* @__PURE__ */ new Map(),
376
+ idleWaiters: /* @__PURE__ */ new Set()
377
+ };
378
+ }
379
+ function noteGraphOpened(slot, handle, graph) {
380
+ if (graph !== null) slot.graphs.set(handle, graph);
381
+ }
382
+ function noteWorkStarted(slot, requestId, handle) {
383
+ slot.working.set(requestId, handle);
384
+ }
385
+ function stillWorkingOn(slot, handle) {
386
+ for (const working of slot.working.values()) if (working === handle) return true;
387
+ return false;
388
+ }
389
+ function forgetDroppedGraphs(slot, shared) {
390
+ for (const handle of [...slot.graphs.keys()]) {
391
+ if (!shared.has(handle) && !stillWorkingOn(slot, handle)) slot.graphs.delete(handle);
392
+ }
393
+ }
394
+ function noteAnswered(slot, requestId, shared) {
395
+ if (!slot.working.delete(requestId)) return;
396
+ forgetDroppedGraphs(slot, shared);
397
+ if (slot.working.size > 0) return;
398
+ for (const waiter of [...slot.idleWaiters]) waiter();
399
+ }
400
+ function untilIdle(slot, deadlineMs) {
401
+ if (slot.working.size === 0) return Promise.resolve();
402
+ return new Promise((resolve) => {
403
+ const finish = () => {
404
+ clearTimeout(timer);
405
+ slot.idleWaiters.delete(finish);
406
+ resolve();
407
+ };
408
+ const timer = setTimeout(finish, deadlineMs);
409
+ slot.idleWaiters.add(finish);
410
+ });
411
+ }
412
+ function releaseSlotLocks(slot) {
413
+ noteThreadSlotGone(slot.scratchSlot);
414
+ for (const graph of slot.graphs.values()) releaseLocksHeldBy(graph, slot.scratchSlot);
415
+ slot.graphs.clear();
416
+ slot.working.clear();
417
+ for (const waiter of [...slot.idleWaiters]) waiter();
418
+ }
419
+ function rejectPending(slot, err) {
420
+ for (const [, waiting] of slot.pending) {
421
+ clearTimeout(waiting.timer);
422
+ waiting.reject(err);
423
+ }
424
+ slot.pending.clear();
425
+ }
426
+
427
+ // src/vector/search-pool/spawn.ts
428
+ function resolveWorkerEntryPoint() {
429
+ return resolveWorkerEntry(import.meta.url, /\/src\/vector\/search-pool\/[^/]+$/, "vector/search-worker.mjs");
430
+ }
431
+ async function spawnWorker(entryPoint) {
432
+ if (entryPoint === null) return null;
433
+ const runtime = detectRuntime();
434
+ if (runtime.supportsWorkerThreads) {
435
+ try {
436
+ return await spawnNodeWorker(new URL(entryPoint), workerResourceLimits());
437
+ } catch {
438
+ return null;
439
+ }
440
+ }
441
+ if (runtime.supportsWebWorkers) {
442
+ try {
443
+ const WorkerCtor = globalThis.Worker;
444
+ if (typeof WorkerCtor !== "function") return null;
445
+ return new WorkerCtor(entryPoint, { type: "module" });
446
+ } catch {
447
+ return null;
448
+ }
449
+ }
450
+ return null;
451
+ }
452
+ function listen(worker, handler) {
453
+ if (typeof worker.on === "function") {
454
+ worker.on("message", handler);
455
+ return;
456
+ }
457
+ worker.addEventListener?.("message", (event) => {
458
+ handler(event.data);
459
+ });
460
+ }
461
+ function listenForFailure(worker, handler) {
462
+ if (typeof worker.on === "function") {
463
+ worker.on("error", (err) => handler(err instanceof Error ? err : new Error(String(err))));
464
+ worker.on("exit", () => handler(new Error("Vector search worker exited")));
465
+ return;
466
+ }
467
+ worker.addEventListener?.("error", () => handler(new Error("Vector search worker failed")));
468
+ }
469
+
470
+ // src/vector/search-pool/index.ts
471
+ function searchPoolScratchSlot(index) {
472
+ return VECTOR_SCRATCH_SLOTS - 1 - index;
473
+ }
474
+ function graphSearchOptionsOf(options) {
475
+ return {
476
+ ...options.filter !== void 0 ? { filter: options.filter } : {},
477
+ ...options.efSearch !== void 0 ? { efSearch: options.efSearch } : {},
478
+ ...options.oversample !== void 0 ? { oversample: options.oversample } : {},
479
+ ...options.rescoreWindow !== void 0 ? { rescoreWindow: options.rescoreWindow } : {}
480
+ };
481
+ }
482
+ async function createVectorSearchPool(requestedCount) {
483
+ const entryPoint = resolveWorkerEntryPoint();
484
+ const count = resolveWorkerCount(requestedCount);
485
+ const slots = [];
486
+ const sharedFields = /* @__PURE__ */ new Map();
487
+ for (let i = 0; i < count; i++) {
488
+ const worker = await spawnWorker(entryPoint);
489
+ if (worker === null) break;
490
+ const slot = createSlot(worker, searchPoolScratchSlot(i));
491
+ listen(worker, (raw) => {
492
+ const message = raw;
493
+ const key = message.requestId;
494
+ if (typeof key !== "string") return;
495
+ noteAnswered(slot, key, sharedFields);
496
+ const waiting = slot.pending.get(key);
497
+ if (!waiting) return;
498
+ slot.pending.delete(key);
499
+ clearTimeout(waiting.timer);
500
+ waiting.resolve(message);
501
+ });
502
+ listenForFailure(worker, (err) => {
503
+ slot.alive = false;
504
+ releaseSlotLocks(slot);
505
+ rejectPending(slot, err);
506
+ });
507
+ slots.push(slot);
508
+ }
509
+ if (slots.length === 0) return null;
510
+ let requestCounter = 0;
511
+ function send(slot, key, message, timeoutMs, handle) {
512
+ return new Promise((resolve, reject) => {
513
+ if (!slot.alive) {
514
+ reject(new Error("Vector search worker is not running"));
515
+ return;
516
+ }
517
+ const timer = setTimeout(() => {
518
+ slot.pending.delete(key);
519
+ reject(new Error(`Vector search worker did not answer within ${timeoutMs}ms`));
520
+ }, timeoutMs);
521
+ slot.pending.set(key, { resolve, reject, timer });
522
+ if (handle !== void 0) noteWorkStarted(slot, key, handle);
523
+ try {
524
+ slot.worker.postMessage(message);
525
+ } catch (err) {
526
+ slot.pending.delete(key);
527
+ noteAnswered(slot, key, sharedFields);
528
+ clearTimeout(timer);
529
+ reject(err instanceof Error ? err : new Error(String(err)));
530
+ }
531
+ });
532
+ }
533
+ function refusalOf(message) {
534
+ return message.type === "error" ? message.message : `the worker answered "${message.type}"`;
535
+ }
536
+ function pickSlot() {
537
+ let slot = null;
538
+ for (const candidate of slots) {
539
+ if (!candidate.alive) continue;
540
+ if (slot === null || candidate.outstanding < slot.outstanding) slot = candidate;
541
+ if (slot.outstanding === 0) break;
542
+ }
543
+ return slot;
544
+ }
545
+ async function sendBusy(slot, request, timeoutMs) {
546
+ slot.outstanding += 1;
547
+ try {
548
+ return await send(slot, request.requestId, request, timeoutMs, request.handle);
549
+ } finally {
550
+ slot.outstanding -= 1;
551
+ }
552
+ }
553
+ async function broadcast(build, timeoutMs) {
554
+ const outcomes = await Promise.allSettled(
555
+ slots.map((slot) => {
556
+ requestCounter += 1;
557
+ const requestId = `${requestCounter}`;
558
+ return send(slot, requestId, build(slot, requestId), timeoutMs);
559
+ })
560
+ );
561
+ return outcomes.every(
562
+ (outcome) => outcome.status === "fulfilled" && outcome.value.type === "ack"
563
+ );
564
+ }
565
+ return {
566
+ get workerCount() {
567
+ return slots.filter((slot) => slot.alive).length;
568
+ },
569
+ load(handle, snapshot) {
570
+ return broadcast(
571
+ (_slot, requestId) => ({ type: "load", requestId, handle, snapshot }),
572
+ VECTOR_SEARCH_LOAD_TIMEOUT_MS
573
+ );
574
+ },
575
+ async loadShared(handle, handles) {
576
+ sharedFields.set(handle, handles);
577
+ const loaded = await broadcast((slot, requestId) => {
578
+ noteGraphOpened(slot, handle, handles.graph);
579
+ return { type: "loadShared", requestId, handle, scratchSlot: slot.scratchSlot, handles };
580
+ }, VECTOR_SEARCH_LOAD_TIMEOUT_MS);
581
+ if (loaded) return true;
582
+ sharedFields.delete(handle);
583
+ for (const slot of slots) forgetDroppedGraphs(slot, sharedFields);
584
+ return false;
585
+ },
586
+ async drop(handle) {
587
+ sharedFields.delete(handle);
588
+ await broadcast((_slot, requestId) => ({ type: "drop", requestId, handle }), VECTOR_SEARCH_TIMEOUT_MS);
589
+ for (const slot of slots) forgetDroppedGraphs(slot, sharedFields);
590
+ },
591
+ async insertOrdinals(handle, ordinals) {
592
+ const slot = pickSlot();
593
+ if (slot === null) return null;
594
+ requestCounter += 1;
595
+ const request = { type: "insertOrdinals", requestId: `${requestCounter}`, handle, ordinals };
596
+ const message = await sendBusy(slot, request, VECTOR_INSERT_TIMEOUT_MS);
597
+ if (message.type !== "inserted") {
598
+ console.warn("A vector worker placed no vectors in the shared graph:", refusalOf(message));
599
+ return null;
600
+ }
601
+ return message.outcome;
602
+ },
603
+ async search(handle, query, k, metric, minSimilarity, options = {}) {
604
+ const slot = pickSlot();
605
+ if (slot === null) throw new Error("No vector search worker is running");
606
+ requestCounter += 1;
607
+ const request = {
608
+ type: "search",
609
+ requestId: `${requestCounter}`,
610
+ handle,
611
+ query,
612
+ k,
613
+ metric,
614
+ minSimilarity,
615
+ ...graphSearchOptionsOf(options)
616
+ };
617
+ const message = await sendBusy(slot, request, VECTOR_SEARCH_TIMEOUT_MS);
618
+ if (message.type === "error") throw new Error(message.message);
619
+ if (message.type !== "result") throw new Error("Vector search worker returned an unexpected message");
620
+ const results = new Array(message.docIds.length);
621
+ for (let i = 0; i < message.docIds.length; i++) {
622
+ results[i] = { docId: message.docIds[i], score: message.scores[i] };
623
+ }
624
+ return results;
625
+ },
626
+ async searchOrdinals(handle, query, k, metric, minSimilarity, options = {}) {
627
+ const slot = pickSlot();
628
+ if (slot === null) throw new Error("No vector search worker is running");
629
+ requestCounter += 1;
630
+ const request = {
631
+ type: "searchOrdinals",
632
+ requestId: `${requestCounter}`,
633
+ handle,
634
+ query,
635
+ k,
636
+ metric,
637
+ minSimilarity,
638
+ ...graphSearchOptionsOf(options)
639
+ };
640
+ const message = await sendBusy(slot, request, VECTOR_SEARCH_TIMEOUT_MS);
641
+ if (message.type === "error") throw new Error(message.message);
642
+ if (message.type !== "ordinalResult") throw new Error("Vector search worker returned an unexpected message");
643
+ return { ordinals: message.ordinals, scores: message.scores };
644
+ },
645
+ async shutdown() {
646
+ for (const slot of slots) slot.alive = false;
647
+ await Promise.all(slots.map((slot) => untilIdle(slot, VECTOR_POOL_DRAIN_TIMEOUT_MS)));
648
+ for (const slot of slots) {
649
+ try {
650
+ await slot.worker.terminate();
651
+ } catch {
652
+ slot.alive = false;
653
+ }
654
+ releaseSlotLocks(slot);
655
+ rejectPending(slot, new Error("Vector search pool has shut down"));
656
+ }
657
+ }
658
+ };
659
+ }
660
+ var sharedPool = null;
661
+ var poolHolders = 0;
662
+ var workersUnavailable = false;
663
+ var closingPool = Promise.resolve();
664
+ function acquireVectorSearchPool(requestedCount) {
665
+ poolHolders += 1;
666
+ if (workersUnavailable) return Promise.resolve(null);
667
+ if (sharedPool === null) {
668
+ sharedPool = closingPool.then(() => createVectorSearchPool(requestedCount)).then(
669
+ (pool) => {
670
+ if (pool === null) workersUnavailable = true;
671
+ return pool;
672
+ },
673
+ () => {
674
+ workersUnavailable = true;
675
+ return null;
676
+ }
677
+ );
678
+ }
679
+ return sharedPool;
680
+ }
681
+ async function releaseVectorSearchPool() {
682
+ if (poolHolders === 0) return;
683
+ poolHolders -= 1;
684
+ if (poolHolders > 0 || sharedPool === null) return;
685
+ const pending = sharedPool;
686
+ sharedPool = null;
687
+ const closing = closingPool.then(async () => {
688
+ const pool = await pending;
689
+ await pool?.shutdown();
690
+ });
691
+ closingPool = closing.catch(() => void 0);
692
+ await closing;
693
+ }
694
+
695
+ // src/vector/vector-index/worker-copies.ts
696
+ var handleCounter = 0;
697
+ function nextHandle(state) {
698
+ handleCounter += 1;
699
+ return `${state.indexName}/${state.fieldName}#${handleCounter}`;
700
+ }
701
+ async function dropHandle(state, handle) {
702
+ const host = state.workerCopies.host;
703
+ if (host !== void 0) {
704
+ await host.drop(state.indexName, state.fieldName, handle).catch(() => void 0);
705
+ return;
706
+ }
707
+ const pool = state.workerCopyPool;
708
+ if (pool !== null) {
709
+ await pool.drop(handle).catch(() => void 0);
710
+ await releaseVectorSearchPool().catch(() => void 0);
711
+ }
712
+ }
713
+ function dropSharedGraph(state, graph) {
714
+ const shared = state.sharedHandles.get(graph);
715
+ if (shared === void 0) return;
716
+ state.sharedHandles.delete(graph);
717
+ if (state.workerCopyHandle === shared.handle) {
718
+ state.workerCopyHandle = null;
719
+ state.workerCopyRevision = -1;
720
+ state.workerCopyMode = null;
721
+ }
722
+ void dropHandle(state, shared.handle);
723
+ }
724
+ function invalidateWorkerCopies(state) {
725
+ state.revision += 1;
726
+ for (const graph of [...state.sharedHandles.keys()]) dropSharedGraph(state, graph);
727
+ if (state.workerCopyHandle !== null) {
728
+ const handle = state.workerCopyHandle;
729
+ state.workerCopyHandle = null;
730
+ state.workerCopyRevision = -1;
731
+ state.workerCopyMode = null;
732
+ void dropHandle(state, handle);
733
+ }
734
+ if (state.workerCopies.host === void 0 && state.workerCopyPool !== null && state.sharedHandles.size === 0) {
735
+ state.workerCopyPool = null;
736
+ }
737
+ }
738
+ async function withdrawWorkerCopies(state) {
739
+ state.revision += 1;
740
+ const handles = /* @__PURE__ */ new Set();
741
+ for (const shared of state.sharedHandles.values()) handles.add(shared.handle);
742
+ if (state.workerCopyHandle !== null) handles.add(state.workerCopyHandle);
743
+ state.sharedHandles.clear();
744
+ state.workerCopyHandle = null;
745
+ state.workerCopyRevision = -1;
746
+ state.workerCopyMode = null;
747
+ await Promise.all([...handles].map((handle) => dropHandle(state, handle)));
748
+ if (state.workerCopies.host === void 0 && state.sharedHandles.size === 0) state.workerCopyPool = null;
749
+ }
750
+ async function holdWorkerCopies(state) {
751
+ state.workerCopiesHeld = true;
752
+ await state.sharing;
753
+ await withdrawWorkerCopies(state);
754
+ }
755
+ function noteWrite(state) {
756
+ state.revision += 1;
757
+ if (state.workerCopyMode === "clone") invalidateWorkerCopies(state);
758
+ }
759
+ function refreshWorkerCopies(state) {
760
+ invalidateWorkerCopies(state);
761
+ scheduleWorkerCopyLoad(state);
762
+ }
763
+ function recordSharedField(state, record) {
764
+ const { graph, handle, searchable, mode, revision, layoutRevision } = record;
765
+ state.sharedHandles.set(graph, { handle, searchable, mode });
766
+ state.sharedLayoutRevision = layoutRevision;
767
+ if (!searchable) return;
768
+ state.workerCopyHandle = handle;
769
+ state.workerCopyRevision = revision;
770
+ state.workerCopyMode = mode;
771
+ }
772
+ async function loadOnHost(state, host, graph, searchable) {
773
+ if (!host.holdsIndex(state.indexName)) return null;
774
+ if (!state.store.partitionsKnown) {
775
+ assignStorePartitions(state, (docId) => host.resolvePartition(state.indexName, docId));
776
+ }
777
+ const handle = state.sharedHandles.get(graph)?.handle ?? nextHandle(state);
778
+ const layoutRevision = state.store.handles.layoutRevision;
779
+ let loaded = false;
780
+ try {
781
+ loaded = await host.loadShared(state.indexName, state.fieldName, handle, fieldHandlesOf(state, graph, searchable));
782
+ } catch {
783
+ loaded = false;
784
+ }
785
+ if (!loaded || state.disposed) return null;
786
+ recordSharedField(state, { graph, handle, searchable, mode: "hosted", revision: state.revision, layoutRevision });
787
+ return handle;
788
+ }
789
+ function captureCloneCodes(state) {
790
+ const quantizer = state.osq;
791
+ const centroid = quantizer?.centroid ?? null;
792
+ const layout = state.store.handles.codeLayout;
793
+ if (quantizer === null || centroid === null || layout === null) return null;
794
+ const slots = state.store.slots;
795
+ const records = new Uint8Array(slots * layout.slotStride);
796
+ const present = new Uint8Array(slots);
797
+ for (let ordinal = 0; ordinal < slots; ordinal++) {
798
+ const record = quantizer.recordAtOrdinal(ordinal);
799
+ if (record === void 0) continue;
800
+ records.set(record, ordinal * layout.slotStride);
801
+ present[ordinal] = 1;
802
+ }
803
+ return { centroid: Float32Array.from(centroid), records, present };
804
+ }
805
+ function captureCloneSnapshot(state, graph) {
806
+ return {
807
+ dimension: state.dimension,
808
+ quantization: state.quantizationMode,
809
+ metric: state.metric,
810
+ codes: captureCloneCodes(state),
811
+ store: state.store.exportSnapshot(),
812
+ graph: graph.exportSnapshot(),
813
+ tombstones: Array.from(state.tombstones)
814
+ };
815
+ }
816
+ async function loadOnPool(state, graph, searchable) {
817
+ if (graph === null) return null;
818
+ const revision = state.revision;
819
+ const pool = state.workerCopyPool ?? await acquireVectorSearchPool(state.workerCopies.count);
820
+ if (pool === null) {
821
+ await releaseVectorSearchPool();
822
+ return null;
823
+ }
824
+ if (state.disposed) {
825
+ await releaseVectorSearchPool();
826
+ return null;
827
+ }
828
+ state.workerCopyPool = pool;
829
+ const handle = state.sharedHandles.get(graph)?.handle ?? nextHandle(state);
830
+ const layoutRevision = state.store.handles.layoutRevision;
831
+ let mode = "shared";
832
+ let loaded = false;
833
+ try {
834
+ if (sharedMemoryAvailable()) {
835
+ loaded = await pool.loadShared(handle, fieldHandlesOf(state, graph, searchable));
836
+ }
837
+ if (!loaded && searchable) {
838
+ mode = "clone";
839
+ loaded = await pool.load(handle, captureCloneSnapshot(state, graph));
840
+ }
841
+ } catch {
842
+ loaded = false;
843
+ }
844
+ if (!loaded || state.disposed || mode === "clone" && state.revision !== revision) {
845
+ await pool.drop(handle).catch(() => void 0);
846
+ return null;
847
+ }
848
+ recordSharedField(state, { graph, handle, searchable, mode, revision, layoutRevision });
849
+ return handle;
850
+ }
851
+ function shareGraph(state, graph, searchable) {
852
+ const run = state.sharing.then(async () => {
853
+ if (!state.workerCopies.enabled || state.disposed || state.workerCopiesHeld) return null;
854
+ const existing = state.sharedHandles.get(graph);
855
+ const layoutUnchanged = threadsHoldCurrentLayout(state);
856
+ if (existing !== void 0 && layoutUnchanged && (existing.searchable || !searchable)) return existing.handle;
857
+ const host = state.workerCopies.host;
858
+ return host !== void 0 ? loadOnHost(state, host, graph, searchable) : loadOnPool(state, graph, searchable);
859
+ });
860
+ state.sharing = run.then(
861
+ () => void 0,
862
+ () => void 0
863
+ );
864
+ return run;
865
+ }
866
+ function resendSharedHandles(state) {
867
+ const run = state.sharing.then(async () => {
868
+ if (!state.workerCopies.enabled || state.disposed || state.workerCopiesHeld) return;
869
+ const host = state.workerCopies.host;
870
+ for (const [graph, shared] of [...state.sharedHandles]) {
871
+ if (host !== void 0) await loadOnHost(state, host, graph, shared.searchable);
872
+ else await loadOnPool(state, graph, shared.searchable);
873
+ }
874
+ });
875
+ state.sharing = run.then(
876
+ () => void 0,
877
+ () => void 0
878
+ );
879
+ return run;
880
+ }
881
+ function scheduleWorkerCopyLoad(state) {
882
+ if (!state.workerCopies.enabled || state.workerCopiesHeld) return;
883
+ if (state.disposed || state.workerCopyLoading || state.building) return;
884
+ const graph = state.hnsw;
885
+ const host = state.workerCopies.host;
886
+ if (graph === null && host === void 0) return;
887
+ const existing = state.sharedHandles.get(graph);
888
+ if (existing?.searchable && threadsHoldCurrentLayout(state)) return;
889
+ if (host === void 0 && liveSize(state) < WORKER_COPY_MIN_VECTORS) return;
890
+ state.workerCopyLoading = true;
891
+ void shareGraph(state, graph, true).then(
892
+ () => {
893
+ state.workerCopyLoading = false;
894
+ },
895
+ () => {
896
+ state.workerCopyLoading = false;
897
+ }
898
+ );
899
+ }
900
+ async function searchViaWorkerCopies(state, query, k, metric, minSimilarity, options) {
901
+ const pool = state.workerCopyPool;
902
+ const handle = state.workerCopyHandle;
903
+ if (pool === null || handle === null || state.workerCopyMode === "hosted") return null;
904
+ if (state.workerCopyMode === "clone" && state.workerCopyRevision !== state.revision) return null;
905
+ try {
906
+ if (state.workerCopyMode === "shared") {
907
+ const outcome = await pool.searchOrdinals(handle, query, k, metric, minSimilarity, options);
908
+ const results = [];
909
+ for (let i = 0; i < outcome.ordinals.length; i++) {
910
+ const docId = state.store.docIdForOrdinal(outcome.ordinals[i]);
911
+ if (docId === void 0) continue;
912
+ results.push({ docId, score: outcome.scores[i] });
913
+ }
914
+ return results;
915
+ }
916
+ return await pool.search(handle, query, k, metric, minSimilarity, options);
917
+ } catch {
918
+ return null;
919
+ }
920
+ }
921
+
922
+ // src/vector/vector-index/build-host.ts
923
+ async function dispatcherFor(state, graph) {
924
+ if (!state.workerCopies.enabled || state.disposed) return null;
925
+ const host = state.workerCopies.host;
926
+ if (host !== void 0) {
927
+ if (!host.holdsIndex(state.indexName)) return null;
928
+ const handle2 = await shareGraph(state, graph, graph === state.hnsw);
929
+ if (handle2 === null) return null;
930
+ return {
931
+ send: (ordinals) => host.insertOrdinals(state.indexName, state.fieldName, handle2, ordinals),
932
+ workerCount: Math.max(1, host.workerCount)
933
+ };
934
+ }
935
+ if (liveSize(state) < WORKER_COPY_MIN_VECTORS || !sharedMemoryAvailable()) return null;
936
+ const handle = await shareGraph(state, graph, graph === state.hnsw);
937
+ const pool = state.workerCopyPool;
938
+ if (handle === null || pool === null || state.sharedHandles.get(graph)?.mode !== "shared") return null;
939
+ return {
940
+ send: (ordinals) => pool.insertOrdinals(handle, ordinals),
941
+ workerCount: Math.max(1, pool.workerCount)
942
+ };
943
+ }
944
+ async function insertIntoGraph(state, graph, docIds, admit) {
945
+ const found = await dispatcherFor(state, graph);
946
+ if (found === null) return insertLocally(state, graph, docIds, admit);
947
+ const dispatch = found;
948
+ const pending = /* @__PURE__ */ new Set();
949
+ const limit = dispatch.workerCount * GRAPH_BUILD_CHUNKS_IN_FLIGHT_PER_WORKER;
950
+ const fallback = { noWorkerWarned: false, shortAnswerWarned: false };
951
+ let batchDocIds = [];
952
+ let batchOrdinals = [];
953
+ async function flush() {
954
+ if (batchOrdinals.length === 0) return;
955
+ const chunkDocIds = batchDocIds;
956
+ const chunkOrdinals = Int32Array.from(batchOrdinals);
957
+ batchDocIds = [];
958
+ batchOrdinals = [];
959
+ if (!threadsHoldCurrentLayout(state)) await shareGraph(state, graph, graph === state.hnsw);
960
+ const run = placeChunk(state, graph, dispatch, chunkDocIds, chunkOrdinals, fallback).finally(() => {
961
+ pending.delete(run);
962
+ });
963
+ pending.add(run);
964
+ if (pending.size >= limit) await Promise.race(pending);
965
+ }
966
+ for (const docId of docIds) {
967
+ if (state.disposed) break;
968
+ if (!admit(docId)) continue;
969
+ const ordinal = state.store.getOrdinal(docId);
970
+ if (ordinal === void 0) continue;
971
+ batchDocIds.push(docId);
972
+ batchOrdinals.push(ordinal);
973
+ if (batchOrdinals.length >= GRAPH_BUILD_DISPATCH_CHUNK) await flush();
974
+ }
975
+ await flush();
976
+ await Promise.all(pending);
977
+ if (state.disposed) return false;
978
+ graph.connectUnreached();
979
+ noteWrite(state);
980
+ return true;
981
+ }
982
+ async function placeChunk(state, graph, dispatch, chunkDocIds, chunkOrdinals, fallback) {
983
+ let outcome = null;
984
+ try {
985
+ outcome = await dispatch.send(chunkOrdinals);
986
+ } catch {
987
+ outcome = null;
988
+ }
989
+ if (state.disposed) return;
990
+ if (outcome === null) {
991
+ if (!fallback.noWorkerWarned) {
992
+ fallback.noWorkerWarned = true;
993
+ console.warn(
994
+ `No worker thread placed the vectors of "${state.indexName}/${state.fieldName}", so this thread is building its graph itself`
995
+ );
996
+ }
997
+ for (const ordinal of chunkOrdinals) graph.insertOrdinal(ordinal);
998
+ }
999
+ let placedHere = 0;
1000
+ for (let i = 0; i < chunkDocIds.length; i++) {
1001
+ const docId = chunkDocIds[i];
1002
+ const ordinal = chunkOrdinals[i];
1003
+ if (state.store.getOrdinal(docId) !== ordinal) {
1004
+ graph.markTombstoneOrdinal(ordinal);
1005
+ continue;
1006
+ }
1007
+ if (!state.tombstones.has(docId) && !graph.has(docId) && graph.insertOrdinal(ordinal)) placedHere += 1;
1008
+ if (graph === state.hnsw) state.buffer.delete(docId);
1009
+ }
1010
+ if (placedHere > 0 && outcome !== null && !fallback.shortAnswerWarned) {
1011
+ fallback.shortAnswerWarned = true;
1012
+ console.warn(
1013
+ `A worker thread answered a chunk of "${state.indexName}/${state.fieldName}" without placing ${placedHere} of its vectors, so this thread placed them itself`
1014
+ );
1015
+ }
1016
+ }
1017
+ async function insertLocally(state, graph, docIds, admit) {
1018
+ let count = 0;
1019
+ for (const docId of docIds) {
1020
+ if (state.disposed) return false;
1021
+ if (!admit(docId)) continue;
1022
+ const ordinal = state.store.getOrdinal(docId);
1023
+ if (ordinal === void 0) continue;
1024
+ graph.insertOrdinal(ordinal);
1025
+ if (graph === state.hnsw) state.buffer.delete(docId);
1026
+ count += 1;
1027
+ if (count % BUILD_CHUNK_SIZE === 0) {
1028
+ noteWrite(state);
1029
+ await yieldBuildTurn(state);
1030
+ }
1031
+ }
1032
+ if (state.disposed) return false;
1033
+ if (count > 0) {
1034
+ graph.connectUnreached();
1035
+ noteWrite(state);
1036
+ }
1037
+ return true;
1038
+ }
1039
+
1040
+ // src/vector/vector-index/drift.ts
1041
+ function liveVectorMeanOf(sums) {
1042
+ const centroid = centroidOfCentroidSums(sums);
1043
+ if (centroid === null) return null;
1044
+ const mean = meanOfCentroidSums(sums);
1045
+ const count = countOfCentroidSums(sums);
1046
+ let meanSquares = 0;
1047
+ for (let i = 0; i < mean.length; i++) meanSquares += mean[i] * mean[i];
1048
+ return {
1049
+ centroid,
1050
+ mean,
1051
+ meanLength: Math.sqrt(meanSquares),
1052
+ count,
1053
+ spread: Math.sqrt(Math.max(0, squaredLengthsOfCentroidSums(sums) / count - meanSquares))
1054
+ };
1055
+ }
1056
+ async function measureLiveVectors(state, quantizer) {
1057
+ const sums = createCentroidSums(state.dimension, state.metric);
1058
+ const ordinals = liveOrdinals(state);
1059
+ for (let start = 0; start < ordinals.length; start += RECALIBRATION_CHUNK_VECTORS) {
1060
+ if (start > 0) await yieldBuildTurn(state);
1061
+ if (state.disposed || state.osq !== quantizer) return null;
1062
+ quantizer.measure(ordinals.subarray(start, start + RECALIBRATION_CHUNK_VECTORS), sums);
1063
+ }
1064
+ return liveVectorMeanOf(sums);
1065
+ }
1066
+ function driftRatio(live, calibrated, calibratedCount, calibratedMeanLength, metric) {
1067
+ if (calibratedCount <= 0 || live.count <= calibratedCount) return 0;
1068
+ const scale = metric === "cosine" ? calibratedMeanLength ?? live.meanLength : 1;
1069
+ let squares = 0;
1070
+ for (let i = 0; i < live.mean.length; i++) {
1071
+ const difference = live.mean[i] - calibrated[i] * scale;
1072
+ squares += difference * difference;
1073
+ }
1074
+ const expected = live.spread * Math.sqrt(1 / calibratedCount - 1 / live.count);
1075
+ if (expected === 0) return squares === 0 ? 0 : Number.POSITIVE_INFINITY;
1076
+ return Math.sqrt(squares) / expected;
1077
+ }
1078
+ function nextDriftCheck(calibratedCount, held) {
1079
+ let next = Math.max(1, calibratedCount) * DRIFT_CHECK_GROWTH_FACTOR;
1080
+ while (next <= held) next *= DRIFT_CHECK_GROWTH_FACTOR;
1081
+ return next;
1082
+ }
1083
+
1084
+ // src/vector/vector-index/calibration.ts
1085
+ function noteCalibration(state, live) {
1086
+ state.calibratedCount = live.count;
1087
+ state.calibratedMeanLength = state.metric === "cosine" ? live.meanLength : null;
1088
+ state.nextDriftCheckAt = nextDriftCheck(live.count, live.count);
1089
+ }
1090
+ async function recodeEveryVector(state, live) {
1091
+ const quantizer = state.osq;
1092
+ if (quantizer === null) return false;
1093
+ quantizer.beginRecoding(live.centroid);
1094
+ try {
1095
+ for (let ordinal = 0; ordinal < state.store.slots; ordinal++) {
1096
+ if (state.disposed || state.osq !== quantizer) {
1097
+ quantizer.abandonRecoding();
1098
+ return false;
1099
+ }
1100
+ quantizer.recode(ordinal);
1101
+ if ((ordinal + 1) % RECALIBRATION_CHUNK_VECTORS === 0) await yieldBuildTurn(state);
1102
+ }
1103
+ const graph = state.hnsw;
1104
+ return graph === null ? quantizer.finishRecoding() : graph.exclusively(() => quantizer.finishRecoding());
1105
+ } catch (error) {
1106
+ quantizer.abandonRecoding();
1107
+ throw error;
1108
+ }
1109
+ }
1110
+ async function calibrateFromLiveVectors(state, live) {
1111
+ const quantizer = state.osq;
1112
+ if (quantizer === null || state.disposed) return false;
1113
+ if (quantizer.isCalibrated()) {
1114
+ if (!await recodeEveryVector(state, live)) return false;
1115
+ noteWrite(state);
1116
+ } else {
1117
+ quantizer.restoreCentroid(live.centroid);
1118
+ }
1119
+ noteCalibration(state, live);
1120
+ return true;
1121
+ }
1122
+ async function calibrateBeforeBuild(state) {
1123
+ if (state.osq === null) return true;
1124
+ const live = await measureLiveVectors(state, state.osq);
1125
+ return live !== null && await calibrateFromLiveVectors(state, live);
1126
+ }
1127
+ async function recalibrateWhenDrifted(state, held) {
1128
+ if (state.osq === null || held < state.nextDriftCheckAt) return false;
1129
+ const live = await measureLiveVectors(state, state.osq);
1130
+ const calibrated = state.osq?.centroid ?? null;
1131
+ const calibratedCount = state.calibratedCount;
1132
+ if (live === null) return false;
1133
+ if (calibrated !== null && calibratedCount !== null) {
1134
+ const ratio = driftRatio(live, calibrated, calibratedCount, state.calibratedMeanLength, state.metric);
1135
+ if (ratio <= RECALIBRATION_DRIFT_RATIO) {
1136
+ state.nextDriftCheckAt = nextDriftCheck(calibratedCount, live.count);
1137
+ return false;
1138
+ }
1139
+ }
1140
+ return calibrateFromLiveVectors(state, live);
1141
+ }
1142
+
1143
+ // src/vector/vector-index/disk.ts
1144
+ function readsFromDisk(state) {
1145
+ return state.storage === "disk";
1146
+ }
1147
+ function holdsVectorsOnDisk(state) {
1148
+ return readsFromDisk(state) && state.hnsw !== null;
1149
+ }
1150
+ function* locationsOf(state, layout) {
1151
+ const stride = state.dimension * 4;
1152
+ for (let i = 0; i < layout.docIds.length; i++) {
1153
+ if (isDead(layout.dead, i)) continue;
1154
+ yield [layout.docIds[i], { path: layout.path, offset: layout.vectorsOffset + i * stride }];
1155
+ }
1156
+ }
1157
+ async function shareFilesWithEveryThread(state, paths) {
1158
+ const fileIndexes = /* @__PURE__ */ new Map();
1159
+ for (const path of paths) fileIndexes.set(path, state.store.addVectorFile(path));
1160
+ await resendSharedHandles(state);
1161
+ return fileIndexes;
1162
+ }
1163
+ async function releaseLocations(state, paths, locations) {
1164
+ const fileIndexes = await shareFilesWithEveryThread(state, paths);
1165
+ if (state.disposed) return;
1166
+ let count = 0;
1167
+ for (const [docId, location] of locations) {
1168
+ if (count > 0 && count % DISK_ADOPTION_YIELD_INTERVAL === 0) {
1169
+ await yieldBuildTurn(state);
1170
+ if (state.disposed) return;
1171
+ }
1172
+ count += 1;
1173
+ const ordinal = state.store.getOrdinal(docId);
1174
+ const fileIndex = fileIndexes.get(location.path);
1175
+ if (ordinal === void 0 || fileIndex === void 0) continue;
1176
+ state.store.releaseToFile(ordinal, { fileIndex, offset: location.offset });
1177
+ }
1178
+ state.store.releaseColdBlocks();
1179
+ await resendSharedHandles(state);
1180
+ }
1181
+ function releaseInTurn(state, release) {
1182
+ const run = state.releaseToFilesInFlight.then(release);
1183
+ state.releaseToFilesInFlight = run.then(
1184
+ () => void 0,
1185
+ () => void 0
1186
+ );
1187
+ return run;
1188
+ }
1189
+ async function adoptDiskLayout(state, layout) {
1190
+ if (!readsFromDisk(state) || state.disposed || layout.docIds.length === 0) return;
1191
+ if (!holdsVectorsOnDisk(state)) {
1192
+ for (const [docId, location] of locationsOf(state, layout)) {
1193
+ if (state.store.has(docId)) state.pendingLocations.set(docId, location);
1194
+ }
1195
+ return;
1196
+ }
1197
+ await releaseInTurn(state, () => releaseLocations(state, [layout.path], locationsOf(state, layout)));
1198
+ }
1199
+ async function releasePendingLocations(state) {
1200
+ if (state.pendingLocations.size === 0 || !holdsVectorsOnDisk(state) || state.disposed) return;
1201
+ const pending = [...state.pendingLocations];
1202
+ state.pendingLocations.clear();
1203
+ const paths = new Set(pending.map(([, location]) => location.path));
1204
+ await releaseInTurn(state, () => releaseLocations(state, paths, pending));
1205
+ }
1206
+
1207
+ // src/vector/vector-index/build.ts
1208
+ function bufferWhatTheGraphLacks(state, graph) {
1209
+ for (const docId of allLiveDocIds(state)) {
1210
+ const ordinal = state.store.getOrdinal(docId);
1211
+ if (ordinal !== void 0 && graph.holdsNode(ordinal) && !graph.holdsTombstone(ordinal)) state.buffer.delete(docId);
1212
+ else state.buffer.add(docId);
1213
+ }
1214
+ }
1215
+ async function buildGraphFromStore(state) {
1216
+ const previous = state.hnsw;
1217
+ const graph = createHNSWIndex(state.dimension, state.store, state.hnswConfig, state.osq ?? void 0);
1218
+ state.freshGraph = graph;
1219
+ let completed = false;
1220
+ try {
1221
+ completed = await insertIntoGraph(state, graph, shuffleInPlace([...allLiveDocIds(state)]), () => true);
1222
+ } finally {
1223
+ state.freshGraph = null;
1224
+ }
1225
+ if (!completed || state.hnsw !== previous) {
1226
+ dropSharedGraph(state, graph);
1227
+ return false;
1228
+ }
1229
+ adoptGraph(state, graph);
1230
+ state.shuffledBuildCount = graph.size;
1231
+ noteWrite(state);
1232
+ bufferWhatTheGraphLacks(state, graph);
1233
+ if (previous !== null) dropSharedGraph(state, previous);
1234
+ await releasePendingLocations(state);
1235
+ return true;
1236
+ }
1237
+ async function promoteToGraph(state) {
1238
+ if (liveSize(state) === 0) return;
1239
+ if (await calibrateBeforeBuild(state)) await buildGraphFromStore(state);
1240
+ }
1241
+ function shuffledRebuildDue(state, held = state.hnsw?.size ?? 0) {
1242
+ if (state.hnsw === null || held === 0) return false;
1243
+ if (state.shuffledBuildCount === null) return true;
1244
+ return state.osq !== null && held >= state.nextDriftCheckAt;
1245
+ }
1246
+ async function recalibratesForRebuild(state, graph) {
1247
+ if (state.shuffledBuildCount === null) return calibrateBeforeBuild(state);
1248
+ return recalibrateWhenDrifted(state, graph.size);
1249
+ }
1250
+ async function buildExclusively(state, work, waitsForShuffledRebuild = true) {
1251
+ for (; ; ) {
1252
+ if (state.pendingBuild !== null) await state.pendingBuild;
1253
+ else if (waitsForShuffledRebuild && state.shuffledRebuild !== null) await state.shuffledRebuild;
1254
+ else break;
1255
+ }
1256
+ if (state.disposed) return;
1257
+ state.building = true;
1258
+ const run = work();
1259
+ state.pendingBuild = run;
1260
+ try {
1261
+ await run;
1262
+ } finally {
1263
+ state.building = false;
1264
+ state.pendingBuild = null;
1265
+ if (state.buffer.size > 0) {
1266
+ scheduleBuild(state);
1267
+ }
1268
+ }
1269
+ }
1270
+ async function rebuildInShuffledOrder(state) {
1271
+ await yieldBuildTurn(state);
1272
+ const replacing = { graph: null, loadedWithoutCount: false };
1273
+ await buildExclusively(
1274
+ state,
1275
+ async () => {
1276
+ const graph = state.hnsw;
1277
+ if (graph === null || !shuffledRebuildDue(state)) return;
1278
+ const loadedWithoutCount = state.shuffledBuildCount === null;
1279
+ if (!await recalibratesForRebuild(state, graph) || state.hnsw !== graph) return;
1280
+ replacing.graph = graph;
1281
+ replacing.loadedWithoutCount = loadedWithoutCount;
1282
+ },
1283
+ false
1284
+ );
1285
+ if (replacing.graph === null || state.hnsw !== replacing.graph) return;
1286
+ const adopted = await buildGraphFromStore(state);
1287
+ if (adopted && replacing.loadedWithoutCount) state.onLoadedGraphRepaired();
1288
+ }
1289
+ function startShuffledRebuild(state) {
1290
+ if (state.shuffledRebuild !== null || state.disposed || !shuffledRebuildDue(state)) return;
1291
+ state.shuffledRebuild = rebuildInShuffledOrder(state).catch((error) => {
1292
+ console.warn(
1293
+ `Rebuilding the graph of "${state.indexName}/${state.fieldName}" in a shuffled order failed, so the field keeps the graph it holds:`,
1294
+ error
1295
+ );
1296
+ }).finally(() => {
1297
+ state.shuffledRebuild = null;
1298
+ scheduleBuild(state);
1299
+ scheduleWorkerCopyLoad(state);
1300
+ });
1301
+ }
1302
+ async function growGraph(state, graph, bufferSnapshot) {
1303
+ const admit = (docId) => {
1304
+ if (state.hnsw !== graph) return false;
1305
+ if (state.tombstones.has(docId) || !state.store.has(docId)) {
1306
+ state.buffer.delete(docId);
1307
+ return false;
1308
+ }
1309
+ return true;
1310
+ };
1311
+ if (state.osq && !state.osq.isCalibrated() && !await calibrateBeforeBuild(state)) return;
1312
+ await insertIntoGraph(state, graph, bufferSnapshot, admit);
1313
+ startShuffledRebuild(state);
1314
+ }
1315
+ function triggerBuild(state) {
1316
+ if (state.building) return;
1317
+ state.building = true;
1318
+ const bufferSnapshot = new Set(state.buffer);
1319
+ const existingGraph = state.hnsw;
1320
+ let settled = false;
1321
+ let timedOut = false;
1322
+ const buildPromise = (async () => {
1323
+ try {
1324
+ if (state.disposed) return;
1325
+ if (existingGraph !== null) {
1326
+ await growGraph(state, existingGraph, bufferSnapshot);
1327
+ return;
1328
+ }
1329
+ await promoteToGraph(state);
1330
+ } catch (error) {
1331
+ if (!timedOutOnAnotherThread(state, error)) throw error;
1332
+ timedOut = true;
1333
+ } finally {
1334
+ settled = true;
1335
+ state.building = false;
1336
+ state.pendingBuild = null;
1337
+ if (timedOut) {
1338
+ retryAfterBackoff(state);
1339
+ } else {
1340
+ state.buildRetryDelayMs = 0;
1341
+ if (state.buffer.size > 0) scheduleBuild(state);
1342
+ else scheduleWorkerCopyLoad(state);
1343
+ }
1344
+ }
1345
+ })();
1346
+ if (!settled) state.pendingBuild = buildPromise;
1347
+ }
1348
+ function timedOutOnAnotherThread(state, error) {
1349
+ if (!isNarsilError(error) || error.code !== ErrorCodes.WORKER_TIMEOUT) return false;
1350
+ console.warn(
1351
+ `Building the graph of "${state.indexName}/${state.fieldName}" waited too long for another thread, so it tries again shortly:`,
1352
+ error.message
1353
+ );
1354
+ return true;
1355
+ }
1356
+ function armBuildTimer(state, delayMs, run) {
1357
+ if (state.disposed || state.buildTimer !== null) return;
1358
+ const timer = setTimeout(() => {
1359
+ state.buildTimer = null;
1360
+ if (!state.disposed) run();
1361
+ }, delayMs);
1362
+ timer.unref?.();
1363
+ state.buildTimer = timer;
1364
+ }
1365
+ function retryAfterBackoff(state) {
1366
+ const delayMs = Math.min(BUILD_RETRY_MAX_DELAY_MS, Math.max(BUILD_RETRY_FIRST_DELAY_MS, state.buildRetryDelayMs * 2));
1367
+ state.buildRetryDelayMs = delayMs;
1368
+ if (state.buildTimer !== null) clearTimeout(state.buildTimer);
1369
+ state.buildTimer = null;
1370
+ armBuildTimer(state, delayMs, () => {
1371
+ const graph = state.hnsw;
1372
+ if (state.building) {
1373
+ retryAfterBackoff(state);
1374
+ return;
1375
+ }
1376
+ if (state.buffer.size > 0 || graph === null) {
1377
+ triggerBuild(state);
1378
+ return;
1379
+ }
1380
+ try {
1381
+ graph.connectUnreached();
1382
+ noteWrite(state);
1383
+ state.buildRetryDelayMs = 0;
1384
+ scheduleWorkerCopyLoad(state);
1385
+ } catch (error) {
1386
+ if (timedOutOnAnotherThread(state, error)) retryAfterBackoff(state);
1387
+ else console.warn(`Connecting the graph of "${state.indexName}/${state.fieldName}" failed:`, error);
1388
+ }
1389
+ });
1390
+ }
1391
+ function buildDue(state) {
1392
+ if (state.hnsw === null) return liveSize(state) >= state.promotionThreshold;
1393
+ if (state.buffer.size === 0) return false;
1394
+ if (state.workerCopies.enabled && state.workerCopies.host !== void 0) return true;
1395
+ return state.buffer.size >= state.promotionThreshold;
1396
+ }
1397
+ function scheduleBuild(state) {
1398
+ if (state.building || state.buildScheduled || state.disposed) return;
1399
+ if (!buildDue(state)) return;
1400
+ state.buildScheduled = true;
1401
+ void yieldBuildTurn(state).then(() => {
1402
+ state.buildScheduled = false;
1403
+ triggerBuild(state);
1404
+ });
1405
+ }
1406
+ async function awaitBuilds(state) {
1407
+ for (; ; ) {
1408
+ const running = state.pendingBuild ?? state.shuffledRebuild;
1409
+ if (running === null) return;
1410
+ await running;
1411
+ }
1412
+ }
1413
+ function releaseHeldMemory(state) {
1414
+ state.hnsw = null;
1415
+ state.freshGraph = null;
1416
+ state.osq = null;
1417
+ state.tombstones.clear();
1418
+ state.buffer.clear();
1419
+ state.pendingLocations.clear();
1420
+ state.savedFiles = [];
1421
+ state.savedSignature = null;
1422
+ state.store.release();
1423
+ }
1424
+ function disposeIndex(state) {
1425
+ state.disposed = true;
1426
+ if (state.buildTimer !== null) clearTimeout(state.buildTimer);
1427
+ state.buildTimer = null;
1428
+ invalidateWorkerCopies(state);
1429
+ const release = () => releaseHeldMemory(state);
1430
+ if (state.pendingBuild !== null || state.shuffledRebuild !== null) {
1431
+ void awaitBuilds(state).then(release, release);
1432
+ return;
1433
+ }
1434
+ release();
1435
+ }
1436
+
1437
+ // src/vector/vector-index/checkpoint-restore.ts
1438
+ var CALIBRATION_NEVER_CURRENT = -1;
1439
+ function sameCentroid(a, b) {
1440
+ if (a.length !== b.length) return false;
1441
+ for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
1442
+ return true;
1443
+ }
1444
+ function bufferEveryVectorOutsideTheGraph(state) {
1445
+ for (const docId of state.store.docIds()) {
1446
+ if (state.tombstones.has(docId)) continue;
1447
+ if (state.hnsw === null || !state.hnsw.has(docId)) state.buffer.add(docId);
1448
+ }
1449
+ }
1450
+ function insertNodesOfLaterGraph(state, graph, later, ordinals) {
1451
+ for (const part of later.parts) requireWholeNeighbourLists(part);
1452
+ for (let number = 0; number < ordinals.length; number++) {
1453
+ const ordinal = ordinals[number];
1454
+ if (ordinal === NO_LIVE_VECTOR_AT_POSITION || topLayerInParts(later, number) < 0) continue;
1455
+ const docId = state.store.docIdForOrdinal(ordinal);
1456
+ if (docId !== void 0 && !graph.has(docId)) graph.insertOrdinal(ordinal);
1457
+ }
1458
+ }
1459
+ function restoreGraph(state, payload, ordinalOfNumber, tombstoned, calibrationLoaded) {
1460
+ const first = payload?.graphs[0]?.parts[0];
1461
+ if (payload === null || first === void 0) {
1462
+ releaseReplacedVectors(state);
1463
+ bufferEveryVectorOutsideTheGraph(state);
1464
+ return;
1465
+ }
1466
+ const graph = createHNSWIndex(
1467
+ state.dimension,
1468
+ state.store,
1469
+ { m: first.m, efConstruction: first.efConstruction, metric: first.metric },
1470
+ state.osq ?? void 0
1471
+ );
1472
+ graph.deserializeNumbered(payload.graphs[0], ordinalOfNumber);
1473
+ for (const ordinal of tombstoned) graph.markTombstoneOrdinal(ordinal);
1474
+ adoptGraph(state, graph);
1475
+ state.shuffledBuildCount = first.shuffledBuildCount ?? null;
1476
+ if (calibrationLoaded && state.shuffledBuildCount !== null) {
1477
+ adoptLoadedCalibration(state, state.shuffledBuildCount, first.calibratedMeanLength ?? null);
1478
+ }
1479
+ for (const later of payload.graphs.slice(1)) insertNodesOfLaterGraph(state, graph, later, ordinalOfNumber);
1480
+ bufferEveryVectorOutsideTheGraph(state);
1481
+ if (state.buffer.size <= graph.size) return;
1482
+ adoptGraph(state, null);
1483
+ releaseReplacedVectors(state);
1484
+ state.buffer.clear();
1485
+ bufferEveryVectorOutsideTheGraph(state);
1486
+ }
1487
+ function beginCheckpointRestore(state, shape) {
1488
+ emptyFieldBeforeRestore(state);
1489
+ const cold = readsFromDisk(state) && (shape.holdsGraph || shape.liveVectors >= state.promotionThreshold);
1490
+ const restoresCodes = state.osq !== null && osqBitsOf(state.quantizationMode) !== null && shape.holdsGraph;
1491
+ const files = [];
1492
+ const graphNodes = shape.graph?.graphs[0] ?? null;
1493
+ const tombstoned = /* @__PURE__ */ new Set();
1494
+ let nextNumber = 0;
1495
+ let sharedCodes = null;
1496
+ let codesAgree = true;
1497
+ function restoreRecords(payload, ordinals) {
1498
+ const quantizer = state.osq;
1499
+ const layout = state.store.handles.codeLayout;
1500
+ if (!restoresCodes || quantizer === null || layout === null || !codesAgree) return;
1501
+ const codes = payload.codes;
1502
+ if (codes === null || codes.bits !== quantizer.bits) {
1503
+ codesAgree = false;
1504
+ return;
1505
+ }
1506
+ if (sharedCodes === null) {
1507
+ sharedCodes = codes;
1508
+ quantizer.restoreCentroid(Float32Array.from(codes.centroid));
1509
+ } else if (!sameCentroid(sharedCodes.centroid, codes.centroid)) {
1510
+ codesAgree = false;
1511
+ return;
1512
+ }
1513
+ const stride = layout.slotStride;
1514
+ for (let i = 0; i < ordinals.length; i++) {
1515
+ if (ordinals[i] === NO_LIVE_VECTOR_AT_POSITION) continue;
1516
+ const record = codes.records.subarray(i * stride, (i + 1) * stride);
1517
+ if (tombstoned.has(ordinals[i])) quantizer.restoreRecordAtOrdinal(ordinals[i], record);
1518
+ else quantizer.restoreRecord(payload.docIds[i], record);
1519
+ }
1520
+ }
1521
+ function graphHoldsNodeFor(number) {
1522
+ return graphNodes !== null && topLayerInParts(graphNodes, number) >= 0;
1523
+ }
1524
+ function addFile(file) {
1525
+ const { payload, location } = file;
1526
+ if (payload.dimension !== state.dimension) {
1527
+ throw new NarsilError(
1528
+ ErrorCodes.VECTOR_DIMENSION_MISMATCH,
1529
+ `Payload dimension ${payload.dimension} does not match index dimension ${state.dimension}`,
1530
+ { expected: state.dimension, received: payload.dimension }
1531
+ );
1532
+ }
1533
+ const dimension = state.dimension;
1534
+ const onDisk = readsFromDisk(state) && location !== null;
1535
+ const vectors = bytesToVectors(payload.vectors);
1536
+ const fileIndex = onDisk && cold ? state.store.addVectorFile(location.path) : null;
1537
+ const ordinals = new Int32Array(payload.docIds.length).fill(NO_LIVE_VECTOR_AT_POSITION);
1538
+ const firstNumber = nextNumber;
1539
+ nextNumber += payload.docIds.length;
1540
+ for (let i = 0; i < payload.docIds.length; i++) {
1541
+ const dead = isDead(file.dead, i);
1542
+ if (dead && !graphHoldsNodeFor(firstNumber + i)) continue;
1543
+ const docId = payload.docIds[i];
1544
+ const vector = vectors.subarray(i * dimension, (i + 1) * dimension);
1545
+ const offset = onDisk ? location.vectorsOffset + i * dimension * 4 : 0;
1546
+ if (dead) {
1547
+ ordinals[i] = state.store.insertReplaced(docId, vector, fileIndex === null ? void 0 : { fileIndex, offset });
1548
+ tombstoned.add(ordinals[i]);
1549
+ continue;
1550
+ }
1551
+ const replaced = state.store.getOrdinal(docId);
1552
+ if (replaced !== void 0) state.osq?.removeOrdinal(replaced);
1553
+ if (fileIndex !== null) {
1554
+ ordinals[i] = state.store.insertCold(docId, magnitude(vector), { fileIndex, offset });
1555
+ continue;
1556
+ }
1557
+ ordinals[i] = state.store.insert(docId, vector);
1558
+ if (onDisk) state.pendingLocations.set(docId, { path: location.path, offset });
1559
+ }
1560
+ restoreRecords(payload, ordinals);
1561
+ files.push({ id: file.id, key: file.key, ordinals, coded: payload.codes !== null });
1562
+ }
1563
+ function finish() {
1564
+ const codesRestored = restoresCodes && codesAgree && sharedCodes !== null;
1565
+ if (restoresCodes && !codesRestored) {
1566
+ recalibrateFromStore(state);
1567
+ for (const ordinal of tombstoned) state.osq?.writeCodes(ordinal);
1568
+ }
1569
+ let total = 0;
1570
+ for (const file of files) total += file.ordinals.length;
1571
+ const ordinalOfNumber = new Int32Array(total);
1572
+ let number = 0;
1573
+ for (const file of files) {
1574
+ for (let i = 0; i < file.ordinals.length; i++) {
1575
+ const ordinal = file.ordinals[i];
1576
+ if (!state.store.holdsOrdinal(ordinal) && !tombstoned.has(ordinal)) {
1577
+ file.ordinals[i] = NO_LIVE_VECTOR_AT_POSITION;
1578
+ }
1579
+ }
1580
+ ordinalOfNumber.set(file.ordinals, number);
1581
+ number += file.ordinals.length;
1582
+ }
1583
+ restoreGraph(state, shape.graph, ordinalOfNumber, tombstoned, codesRestored);
1584
+ const current = signatureOf(state).calibration;
1585
+ const codedUnder = codesRestored && current !== null ? current : CALIBRATION_NEVER_CURRENT;
1586
+ const saved = files.map((file) => ({
1587
+ id: file.id,
1588
+ key: file.key,
1589
+ ordinals: file.ordinals,
1590
+ calibration: file.coded ? codedUnder : null
1591
+ }));
1592
+ state.savedFiles = saved;
1593
+ state.savedSignature = signatureOf(state);
1594
+ }
1595
+ return { addFile, finish };
1596
+ }
1597
+
1598
+ // src/vector/vector-index/flagged-documents.ts
1599
+ var FlaggedDocuments = class extends Set {
1600
+ constructor(store, layout) {
1601
+ super();
1602
+ this.store = store;
1603
+ this.layout = layout;
1604
+ }
1605
+ ordinals = /* @__PURE__ */ new Map();
1606
+ handles = null;
1607
+ flags = new Uint8Array(0);
1608
+ chunks = null;
1609
+ add(docId) {
1610
+ super.add(docId);
1611
+ const handles = this.openHandles();
1612
+ if (handles === null) return this;
1613
+ this.flagCurrentOrdinal(docId);
1614
+ Atomics.store(handles.header, this.layout.countWord, this.size);
1615
+ return this;
1616
+ }
1617
+ delete(docId) {
1618
+ const held = super.delete(docId);
1619
+ const handles = this.openHandles();
1620
+ if (handles === null) return held;
1621
+ const ordinal = this.ordinals.get(docId);
1622
+ if (ordinal !== void 0) {
1623
+ this.ordinals.delete(docId);
1624
+ this.unflag(ordinal);
1625
+ }
1626
+ Atomics.store(handles.header, this.layout.countWord, this.size);
1627
+ return held;
1628
+ }
1629
+ clear() {
1630
+ super.clear();
1631
+ const handles = this.openHandles();
1632
+ if (handles !== null) {
1633
+ for (const ordinal of this.ordinals.values()) this.unflag(ordinal);
1634
+ Atomics.store(handles.header, this.layout.countWord, 0);
1635
+ }
1636
+ this.ordinals.clear();
1637
+ }
1638
+ flagCurrentOrdinal(docId) {
1639
+ const ordinal = this.store.getOrdinal(docId);
1640
+ const previous = this.ordinals.get(docId);
1641
+ if (ordinal === void 0 || ordinal === previous) return;
1642
+ if (this.chunks !== null) Atomics.add(this.chunks, Math.floor(ordinal / UNPLACED_FLAG_CHUNK_ORDINALS), 1);
1643
+ Atomics.store(this.flags, ordinal, 1);
1644
+ this.ordinals.set(docId, ordinal);
1645
+ if (previous !== void 0) this.unflag(previous);
1646
+ }
1647
+ unflag(ordinal) {
1648
+ Atomics.store(this.flags, ordinal, 0);
1649
+ if (this.chunks !== null) Atomics.sub(this.chunks, Math.floor(ordinal / UNPLACED_FLAG_CHUNK_ORDINALS), 1);
1650
+ }
1651
+ openHandles() {
1652
+ if (this.store.dimension === 0) return null;
1653
+ const handles = this.store.handles;
1654
+ if (handles === this.handles) return handles;
1655
+ this.handles = handles;
1656
+ this.flags = new Uint8Array(this.layout.flagsOf(handles));
1657
+ const chunksOf = this.layout.chunksOf;
1658
+ this.chunks = chunksOf === null ? null : new Int32Array(chunksOf(handles));
1659
+ this.ordinals.clear();
1660
+ for (const docId of this) this.flagCurrentOrdinal(docId);
1661
+ return handles;
1662
+ }
1663
+ };
1664
+ function createUnplacedVectors(store) {
1665
+ return new FlaggedDocuments(store, {
1666
+ flagsOf: (handles) => handles.unplaced,
1667
+ chunksOf: (handles) => handles.unplacedChunks,
1668
+ countWord: STORE_UNPLACED_COUNT
1669
+ });
1670
+ }
1671
+ function createRemovedDocuments(store) {
1672
+ return new FlaggedDocuments(store, {
1673
+ flagsOf: (handles) => handles.removed,
1674
+ chunksOf: null,
1675
+ countWord: STORE_REMOVED_COUNT
1676
+ });
1677
+ }
1678
+
1679
+ // src/vector/vector-index/maintenance.ts
1680
+ function compact(state) {
1681
+ if (state.tombstones.size === 0 && state.store.replacedVectorCount === 0) return;
1682
+ if (state.hnsw) {
1683
+ state.compactedNodeCount += state.hnsw.tombstoneCount;
1684
+ state.hnsw.compactTombstones();
1685
+ }
1686
+ state.freshGraph?.compactTombstones();
1687
+ for (const docId of state.tombstones) {
1688
+ state.store.remove(docId);
1689
+ state.buffer.delete(docId);
1690
+ if (state.osq) {
1691
+ state.osq.remove(docId);
1692
+ }
1693
+ }
1694
+ releaseReplacedVectors(state);
1695
+ state.tombstones.clear();
1696
+ }
1697
+ async function insertMissing(state, graph) {
1698
+ const missingOrReplaced = (docId) => !graph.has(docId) || state.buffer.has(docId);
1699
+ const heldWhenTheCallStarted = [...allLiveDocIds(state)];
1700
+ const stillLiveAndUnplaced = (docId) => !state.tombstones.has(docId) && missingOrReplaced(docId);
1701
+ await insertIntoGraph(state, graph, heldWhenTheCallStarted, stillLiveAndUnplaced);
1702
+ startShuffledRebuild(state);
1703
+ }
1704
+ async function foldIntoGraph(state) {
1705
+ const rebuildNeeded = graphNeedsRebuild(state);
1706
+ compact(state);
1707
+ if (liveSize(state) === 0) {
1708
+ const previous = state.hnsw;
1709
+ adoptGraph(state, null);
1710
+ if (previous !== null) dropSharedGraph(state, previous);
1711
+ state.buffer.clear();
1712
+ if (state.osq) {
1713
+ state.osq.clear();
1714
+ }
1715
+ return;
1716
+ }
1717
+ const graph = state.hnsw;
1718
+ if (graph === null || rebuildNeeded || state.shuffledBuildCount === null) {
1719
+ if (await calibrateBeforeBuild(state)) await buildGraphFromStore(state);
1720
+ return;
1721
+ }
1722
+ if (await recalibrateWhenDrifted(state, liveSize(state))) {
1723
+ await buildGraphFromStore(state);
1724
+ return;
1725
+ }
1726
+ await insertMissing(state, graph);
1727
+ }
1728
+ function optimize(state) {
1729
+ return buildExclusively(state, () => foldIntoGraph(state));
1730
+ }
1731
+ async function fillGraph(state) {
1732
+ const graph = state.hnsw;
1733
+ if (graph === null) {
1734
+ if (liveSize(state) >= state.promotionThreshold) await promoteToGraph(state);
1735
+ return;
1736
+ }
1737
+ if (state.osq && !state.osq.isCalibrated() && !await calibrateBeforeBuild(state)) return;
1738
+ if (state.shuffledRebuild === null && graphNeedsRebuild(state)) {
1739
+ await buildGraphFromStore(state);
1740
+ return;
1741
+ }
1742
+ await insertMissing(state, graph);
1743
+ }
1744
+ function completeGraph(state) {
1745
+ return buildExclusively(state, () => fillGraph(state), false);
1746
+ }
1747
+ function maintenanceStatus(state) {
1748
+ const storeSize = state.store.size;
1749
+ const graphCount = state.hnsw ? 1 : 0;
1750
+ const graphNodes = state.hnsw === null ? storeSize : state.hnsw.size + state.hnsw.tombstoneCount;
1751
+ const tombstoned = state.tombstones.size + state.store.replacedVectorCount;
1752
+ const tombstoneRatio = graphNodes > 0 ? tombstoned / graphNodes : 0;
1753
+ const estimatedCompactMs = Math.round(tombstoned * ESTIMATED_MS_PER_TOMBSTONE * state.dimensionScale);
1754
+ const rebuilds = state.hnsw === null || graphNeedsRebuild(state) || shuffledRebuildDue(state, liveSize(state));
1755
+ const optimizeVectorCount = rebuilds ? storeSize : state.buffer.size;
1756
+ const estimatedOptimizeMs = Math.round(optimizeVectorCount * ESTIMATED_MS_PER_VECTOR_REBUILD * state.dimensionScale);
1757
+ return {
1758
+ tombstoneRatio,
1759
+ graphCount,
1760
+ bufferSize: state.buffer.size,
1761
+ building: state.building || state.buildScheduled || state.buildTimer !== null || state.shuffledRebuild !== null,
1762
+ estimatedCompactMs,
1763
+ estimatedOptimizeMs
1764
+ };
1765
+ }
1766
+ function flatScanScratchBytes(state) {
1767
+ return state.store.dimension === 0 ? 0 : nativeStoreScratchBytes(state.store.handles);
1768
+ }
1769
+ function estimateMemoryBytes(state) {
1770
+ if (state.hnsw === null) return state.store.memoryBytes() + flatScanScratchBytes(state);
1771
+ const freshGraphBytes = state.freshGraph?.graphBytes ?? 0;
1772
+ return state.store.memoryBytes() + state.hnsw.graphBytes + state.hnsw.searchScratchBytes + freshGraphBytes;
1773
+ }
1774
+
1775
+ // src/vector/vector-index/search.ts
1776
+ function* bufferCandidates(state, filter) {
1777
+ for (const docId of state.buffer) {
1778
+ if (state.tombstones.has(docId)) continue;
1779
+ if (filter) {
1780
+ const ordinal = state.store.getOrdinal(docId);
1781
+ if (ordinal === void 0 || !ordinalFilterHas(filter, ordinal)) continue;
1782
+ }
1783
+ yield docId;
1784
+ }
1785
+ }
1786
+ function* filteredDocIds(state, filter) {
1787
+ for (const ordinal of ordinalFilterValues(filter)) {
1788
+ const docId = state.store.docIdForOrdinal(ordinal);
1789
+ if (docId !== void 0) yield docId;
1790
+ }
1791
+ }
1792
+ function admittedTotal(state, filter) {
1793
+ return filter === void 0 ? liveSize(state) : filter.count;
1794
+ }
1795
+ function fetchedOutcome(state, results, minSimilarity, filter) {
1796
+ if (minSimilarity === Number.NEGATIVE_INFINITY) {
1797
+ return { results, matched: admittedTotal(state, filter), matchedExact: true };
1798
+ }
1799
+ return { results, matched: results.length, matchedExact: false };
1800
+ }
1801
+ function scannedOutcome(scan) {
1802
+ return { results: scan.results, matched: scan.matched, matchedExact: true };
1803
+ }
1804
+ function liveCandidates(state, candidates) {
1805
+ const docIds = [];
1806
+ const ordinals = [];
1807
+ for (const docId of candidates) {
1808
+ if (state.tombstones.has(docId)) continue;
1809
+ const ordinal = state.store.getOrdinal(docId);
1810
+ if (ordinal === void 0) continue;
1811
+ docIds.push(docId);
1812
+ ordinals.push(ordinal);
1813
+ }
1814
+ return { docIds, ordinals };
1815
+ }
1816
+ function scanThroughTheCore(state, query, k, metric, minSimilarity, scanned) {
1817
+ const distances = nativeScoresOf(state.store.handles, query, metric, scanned.ordinals);
1818
+ if (distances === null) return null;
1819
+ const heap = createBoundedMaxHeap(highScoreFirst, k);
1820
+ let matched = 0;
1821
+ for (let i = 0; i < scanned.ordinals.length; i++) {
1822
+ if (distances[i] === Number.POSITIVE_INFINITY) continue;
1823
+ const score = toScore(distances[i], metric);
1824
+ if (score < minSimilarity) continue;
1825
+ matched++;
1826
+ heap.push({ docId: scanned.docIds[i], score });
1827
+ }
1828
+ return { results: heap.toSortedArray().reverse(), matched };
1829
+ }
1830
+ function bruteForceSearch(state, query, k, metric, minSimilarity, candidates) {
1831
+ const scanned = liveCandidates(state, candidates);
1832
+ const fromTheCore = scanThroughTheCore(state, query, k, metric, minSimilarity, scanned);
1833
+ if (fromTheCore !== null) return fromTheCore;
1834
+ noteFallback("score");
1835
+ const arenaQuery = state.store.prepareQueryArena(query);
1836
+ const queryMag = arenaQuery ? arenaQuery.magnitude : magnitude(query);
1837
+ const heap = createBoundedMaxHeap(highScoreFirst, k);
1838
+ let matched = 0;
1839
+ for (let i = 0; i < scanned.docIds.length; i++) {
1840
+ const docId = scanned.docIds[i];
1841
+ let score;
1842
+ if (arenaQuery) {
1843
+ const distance = state.store.distanceFromArena(arenaQuery, scanned.ordinals[i], metric);
1844
+ if (distance === Number.POSITIVE_INFINITY) continue;
1845
+ score = toScore(distance, metric);
1846
+ } else {
1847
+ const entry = state.store.get(docId);
1848
+ if (!entry) continue;
1849
+ switch (metric) {
1850
+ case "cosine":
1851
+ score = cosineSimilarityWithMagnitudes(query, entry.vector, queryMag, entry.magnitude);
1852
+ break;
1853
+ case "dotProduct":
1854
+ score = dotProduct(query, entry.vector);
1855
+ break;
1856
+ case "euclidean": {
1857
+ const dist = euclideanDistance(query, entry.vector);
1858
+ score = 1 / (1 + dist);
1859
+ break;
1860
+ }
1861
+ }
1862
+ }
1863
+ if (score < minSimilarity) continue;
1864
+ matched++;
1865
+ heap.push({ docId, score });
1866
+ }
1867
+ return { results: heap.toSortedArray().reverse(), matched };
1868
+ }
1869
+ function search(state, query, k, options) {
1870
+ handOverBuildTurns(state);
1871
+ return searchWithFilter(state, query, k, options, filterForOptions(state, options));
1872
+ }
1873
+ function searchWithFilter(state, query, k, options, filter) {
1874
+ if (query.length !== state.dimension) {
1875
+ throw new NarsilError(
1876
+ ErrorCodes.VECTOR_DIMENSION_MISMATCH,
1877
+ `Vector dimension mismatch: expected ${state.dimension}, got ${query.length}`,
1878
+ { expected: state.dimension, received: query.length }
1879
+ );
1880
+ }
1881
+ const currentLiveSize = liveSize(state);
1882
+ const { metric, minSimilarity, efSearch, oversample, rescoreWindow } = options;
1883
+ if (currentLiveSize === 0) return { results: [], matched: 0, matchedExact: true };
1884
+ if (filter && filter.count === 0) return { results: [], matched: 0, matchedExact: true };
1885
+ if (k <= 0) return fetchedOutcome(state, [], minSimilarity, filter);
1886
+ if (state.buffer.size > 0 && !state.building && !state.buildScheduled) {
1887
+ scheduleBuild(state);
1888
+ }
1889
+ if (!state.hnsw) {
1890
+ const candidates = filter ? filteredDocIds(state, filter) : allLiveDocIds(state);
1891
+ return scannedOutcome(bruteForceSearch(state, query, k, metric, minSimilarity, candidates));
1892
+ }
1893
+ if (filter) {
1894
+ const hnswLiveSize = state.hnsw.size;
1895
+ const selectivity = hnswLiveSize > 0 ? filter.count / hnswLiveSize : 1;
1896
+ if (selectivity < state.filterThreshold) {
1897
+ const scan = bruteForceSearch(state, query, k, metric, minSimilarity, filteredDocIds(state, filter));
1898
+ return scannedOutcome(scan);
1899
+ }
1900
+ }
1901
+ const hnswResults = state.hnsw.search(query, k, metric, minSimilarity, { filter, efSearch, oversample, rescoreWindow }).map((r) => ({ docId: r.docId, score: r.score }));
1902
+ return fetchedOutcome(
1903
+ state,
1904
+ withBufferedVectors(state, query, k, options, filter, hnswResults),
1905
+ minSimilarity,
1906
+ filter
1907
+ );
1908
+ }
1909
+ function withBufferedVectors(state, query, k, options, filter, graphResults) {
1910
+ if (state.buffer.size === 0) return graphResults;
1911
+ const candidates = bufferCandidates(state, filter);
1912
+ const buffered = bruteForceSearch(state, query, k, options.metric, options.minSimilarity, candidates);
1913
+ return mergeResults(graphResults, buffered.results, k);
1914
+ }
1915
+
1916
+ // src/vector/vector-index/index.ts
1917
+ function defaultQuantizationFor(dimension) {
1918
+ if (dimension >= OSQ4_MIN_DIMENSION) return "osq4";
1919
+ return "osq8";
1920
+ }
1921
+ function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_WORKER_COPIES_ALLOWED, indexName = "", storage = "memory", onLoadedGraphRepaired = () => void 0) {
1922
+ if (!Number.isInteger(dimension) || dimension <= 0) {
1923
+ throw new NarsilError(
1924
+ ErrorCodes.VECTOR_DIMENSION_MISMATCH,
1925
+ `Vector dimension must be a positive integer, got ${dimension}`,
1926
+ { dimension }
1927
+ );
1928
+ }
1929
+ const promotionThreshold = config?.threshold ?? DEFAULT_PROMOTION_THRESHOLD;
1930
+ const filterThreshold = Math.max(0, Math.min(1, config?.filterThreshold ?? DEFAULT_FILTER_THRESHOLD));
1931
+ const quantizationMode = config?.quantization ?? defaultQuantizationFor(dimension);
1932
+ const codeBits = osqBitsOf(quantizationMode);
1933
+ const metric = config?.hnswConfig?.metric ?? "cosine";
1934
+ const rawHnswM = config?.hnswConfig?.m;
1935
+ const hnswConfig = config?.hnswConfig ? { ...config.hnswConfig, m: rawHnswM !== void 0 ? Math.max(rawHnswM, 2) : void 0 } : void 0;
1936
+ const dimensionScale = dimension / 256;
1937
+ const store = createVectorStore({
1938
+ dimension,
1939
+ codeBits,
1940
+ ...storage === "disk" ? { blockBytes: DISK_STORAGE_BLOCK_BYTES } : {}
1941
+ });
1942
+ const state = {
1943
+ indexName,
1944
+ fieldName,
1945
+ dimension,
1946
+ dimensionScale,
1947
+ promotionThreshold,
1948
+ filterThreshold,
1949
+ quantizationMode,
1950
+ storage,
1951
+ metric,
1952
+ hnswConfig,
1953
+ workerCopies,
1954
+ store,
1955
+ tombstones: createRemovedDocuments(store),
1956
+ buffer: createUnplacedVectors(store),
1957
+ pendingLocations: /* @__PURE__ */ new Map(),
1958
+ savedFiles: [],
1959
+ savedSignature: null,
1960
+ osq: codeBits === null ? null : createOsqQuantizer(dimension, codeBits, metric, store),
1961
+ hnsw: null,
1962
+ freshGraph: null,
1963
+ ...noBuildHistory(),
1964
+ onLoadedGraphRepaired,
1965
+ compactedNodeCount: 0,
1966
+ building: false,
1967
+ buildScheduled: false,
1968
+ buildTimer: null,
1969
+ buildRetryDelayMs: 0,
1970
+ buildTurnWaiters: /* @__PURE__ */ new Map(),
1971
+ pendingBuild: null,
1972
+ disposed: false,
1973
+ revision: 0,
1974
+ workerCopyPool: null,
1975
+ workerCopyHandle: null,
1976
+ workerCopyRevision: -1,
1977
+ workerCopyMode: null,
1978
+ workerCopyLoading: false,
1979
+ workerCopiesHeld: false,
1980
+ sharedHandles: /* @__PURE__ */ new Map(),
1981
+ sharedLayoutRevision: 0,
1982
+ sharing: Promise.resolve(),
1983
+ releaseToFilesInFlight: Promise.resolve()
1984
+ };
1985
+ function validateDimension(vector) {
1986
+ if (vector.length !== state.dimension) {
1987
+ throw new NarsilError(
1988
+ ErrorCodes.VECTOR_DIMENSION_MISMATCH,
1989
+ `Vector dimension mismatch: expected ${state.dimension}, got ${vector.length}`,
1990
+ { expected: state.dimension, received: vector.length }
1991
+ );
1992
+ }
1993
+ }
1994
+ function heldVectorOrUndefinedWhereItsFileFailsToRead(ordinal) {
1995
+ try {
1996
+ return state.store.entryForOrdinal(ordinal)?.vector;
1997
+ } catch {
1998
+ return void 0;
1999
+ }
2000
+ }
2001
+ function holdsThisVectorAlready(docId, ordinal, vector) {
2002
+ if (state.tombstones.has(docId)) return false;
2003
+ const held = heldVectorOrUndefinedWhereItsFileFailsToRead(ordinal);
2004
+ if (held === void 0) return false;
2005
+ for (let i = 0; i < vector.length; i++) if (held[i] !== vector[i]) return false;
2006
+ return true;
2007
+ }
2008
+ function insert(docId, vector, partitionId) {
2009
+ validateDimension(vector);
2010
+ handOverBuildTurns(state);
2011
+ const previous = state.store.getOrdinal(docId);
2012
+ if (previous !== void 0 && holdsThisVectorAlready(docId, previous, vector)) {
2013
+ if (partitionId !== void 0 && state.store.partitionOfOrdinal(previous) !== partitionId) {
2014
+ noteWrite(state);
2015
+ state.store.setPartition(docId, partitionId);
2016
+ }
2017
+ return;
2018
+ }
2019
+ noteWrite(state);
2020
+ state.tombstones.delete(docId);
2021
+ const graphHoldsPrevious = previous !== void 0 && ((state.hnsw?.holdsNode(previous) ?? false) || (state.freshGraph?.holdsNode(previous) ?? false));
2022
+ state.store.insert(docId, vector, partitionId, graphHoldsPrevious);
2023
+ if (previous !== void 0) {
2024
+ state.hnsw?.markTombstoneOrdinal(previous);
2025
+ state.freshGraph?.markTombstoneOrdinal(previous);
2026
+ if (!graphHoldsPrevious) state.osq?.removeOrdinal(previous);
2027
+ }
2028
+ state.buffer.add(docId);
2029
+ state.pendingLocations.delete(docId);
2030
+ if (!threadsHoldCurrentLayout(state)) scheduleWorkerCopyLoad(state);
2031
+ }
2032
+ function remove(docId) {
2033
+ handOverBuildTurns(state);
2034
+ if (!state.store.has(docId)) return;
2035
+ noteWrite(state);
2036
+ state.tombstones.add(docId);
2037
+ state.buffer.delete(docId);
2038
+ state.pendingLocations.delete(docId);
2039
+ if (state.hnsw) {
2040
+ state.hnsw.markTombstone(docId);
2041
+ }
2042
+ }
2043
+ function getVector(docId) {
2044
+ if (state.tombstones.has(docId)) return null;
2045
+ const entry = state.store.get(docId);
2046
+ if (!entry) return null;
2047
+ return new Float32Array(entry.vector);
2048
+ }
2049
+ function has(docId) {
2050
+ return state.store.has(docId) && !state.tombstones.has(docId);
2051
+ }
2052
+ async function searchParallel(query, k, options) {
2053
+ handOverBuildTurns(state);
2054
+ const confined = options.filterSubsets !== void 0 || options.filterPartitions !== void 0;
2055
+ let filter = filterForOptions(state, options);
2056
+ if (filter !== void 0) {
2057
+ if (state.hnsw) {
2058
+ const hnswLiveSize = state.hnsw.size;
2059
+ const selectivity = hnswLiveSize > 0 ? filter.count / hnswLiveSize : 1;
2060
+ if (selectivity < state.filterThreshold) {
2061
+ return searchWithFilter(state, query, k, options, filter);
2062
+ }
2063
+ }
2064
+ }
2065
+ const filterRevision = state.revision;
2066
+ scheduleWorkerCopyLoad(state);
2067
+ const viaWorkerCopy = await searchViaWorkerCopies(state, query, k, options.metric, options.minSimilarity, {
2068
+ filter,
2069
+ efSearch: options.efSearch,
2070
+ oversample: options.oversample,
2071
+ rescoreWindow: options.rescoreWindow
2072
+ });
2073
+ if (confined && state.revision !== filterRevision) {
2074
+ filter = filterForOptions(state, options);
2075
+ }
2076
+ if (viaWorkerCopy !== null) {
2077
+ const results = withBufferedVectors(state, query, k, options, filter, viaWorkerCopy);
2078
+ return fetchedOutcome(state, results, options.minSimilarity, filter);
2079
+ }
2080
+ return searchWithFilter(state, query, k, options, filter);
2081
+ }
2082
+ return {
2083
+ get size() {
2084
+ return liveSize(state);
2085
+ },
2086
+ get dimension() {
2087
+ return state.dimension;
2088
+ },
2089
+ get fieldName() {
2090
+ return state.fieldName;
2091
+ },
2092
+ get quantization() {
2093
+ return state.quantizationMode;
2094
+ },
2095
+ get storage() {
2096
+ return state.storage;
2097
+ },
2098
+ insert,
2099
+ remove,
2100
+ partitionsKnown: () => state.store.partitionsKnown,
2101
+ assignPartitions: (resolve) => assignStorePartitions(state, resolve),
2102
+ scheduleBuild: () => scheduleBuild(state),
2103
+ awaitPendingBuild: () => awaitBuilds(state),
2104
+ completeGraph: () => completeGraph(state),
2105
+ dispose: () => disposeIndex(state),
2106
+ search: (query, k, options) => search(state, query, k, options),
2107
+ searchParallel,
2108
+ refreshWorkerCopies: () => refreshWorkerCopies(state),
2109
+ getVector,
2110
+ has,
2111
+ compact: () => {
2112
+ noteWrite(state);
2113
+ compact(state);
2114
+ },
2115
+ optimize: async () => {
2116
+ noteWrite(state);
2117
+ await optimize(state);
2118
+ },
2119
+ maintenanceStatus: () => maintenanceStatus(state),
2120
+ estimateMemoryBytes: () => estimateMemoryBytes(state),
2121
+ deserialize: (parts) => {
2122
+ invalidateWorkerCopies(state);
2123
+ deserialize(state, parts);
2124
+ },
2125
+ planCheckpoint: (listedKeys, rewritesGraph) => planCheckpoint(state, listedKeys, rewritesGraph),
2126
+ recordCheckpoint: (plan, written) => recordCheckpoint(state, plan, written),
2127
+ restoreCheckpoint: (shape) => {
2128
+ invalidateWorkerCopies(state);
2129
+ return beginCheckpointRestore(state, shape);
2130
+ },
2131
+ startShuffledRebuildWhenDue: () => startShuffledRebuild(state),
2132
+ vectorFilesInUse: () => state.store.dimension === 0 ? [] : state.store.handles.vectorFiles.filter((path) => path !== ""),
2133
+ adoptDiskLayout: (layout) => adoptDiskLayout(state, layout),
2134
+ releaseVectorFiles: async () => {
2135
+ await withdrawWorkerCopies(state);
2136
+ state.store.closeFiles();
2137
+ },
2138
+ relocateVectorFiles: (rewrite) => {
2139
+ state.store.relocateVectorFiles(rewrite);
2140
+ for (const [docId, location] of state.pendingLocations) {
2141
+ state.pendingLocations.set(docId, { ...location, path: rewrite(location.path) });
2142
+ }
2143
+ },
2144
+ holdWorkerCopies: () => holdWorkerCopies(state),
2145
+ resumeWorkerCopies: () => {
2146
+ state.workerCopiesHeld = false;
2147
+ }
2148
+ };
2149
+ }
2150
+
2151
+ export { createSharedVectorSearcher, createVectorIndex };