@delali/narsil 0.3.1-next.99 → 0.4.1-next.211

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 (426) hide show
  1. package/NOTICE +45 -0
  2. package/README.md +30 -11
  3. package/dist/adapters/filesystem.d.ts +1 -1
  4. package/dist/adapters/filesystem.mjs +101 -13
  5. package/dist/adapters/gcs.d.ts +49 -0
  6. package/dist/adapters/gcs.mjs +213 -0
  7. package/dist/adapters/indexeddb.d.ts +1 -1
  8. package/dist/adapters/indexeddb.mjs +2 -7
  9. package/dist/adapters/memory.d.ts +1 -1
  10. package/dist/adapters/memory.mjs +5 -0
  11. package/dist/adapters/s3.d.ts +53 -0
  12. package/dist/adapters/s3.mjs +263 -0
  13. package/dist/{adapters-BQFQ4PRz.d.ts → adapters-pljAqmrC.d.ts} +33 -0
  14. package/dist/browser/chunk-4E5APHTM.mjs +1459 -0
  15. package/dist/browser/chunk-NDYADQ4Y.mjs +23426 -0
  16. package/dist/browser/chunk-QAOVAYAG.mjs +564 -0
  17. package/dist/browser/chunk-WC2DEXWU.mjs +29 -0
  18. package/dist/browser/chunk-YINZFPLT.mjs +1239 -0
  19. package/dist/browser/chunk-YJHGOJAC.mjs +15 -0
  20. package/dist/browser/{chunk-Z7QH2XHB.mjs → core-leaf-HWAXCEWX.mjs} +3987 -2103
  21. package/dist/browser/custom-token-results-R5BIX7SL.mjs +59 -0
  22. package/dist/browser/digest-leaf-64IPRPZD.mjs +151 -0
  23. package/dist/browser/durability-config-75NSFCQN.mjs +97 -0
  24. package/dist/browser/embed-GG6PS3EA.mjs +264 -0
  25. package/dist/browser/geo-index-GNHY4PUB.mjs +1484 -0
  26. package/dist/browser/gzip-JF3S6PPT.mjs +31 -0
  27. package/dist/browser/module-ZZTNG5JZ.mjs +5762 -0
  28. package/dist/browser/msgpack-leaf-44XNWSSY.mjs +302 -0
  29. package/dist/browser/optional-reads-XMVPG6HG.mjs +3230 -0
  30. package/dist/browser/optional-writes-76DWMRQH.mjs +3603 -0
  31. package/dist/browser/phrase-module-6UGFVW5J.mjs +617 -0
  32. package/dist/browser/resolve-vector-text-XRR2OVHE.mjs +85 -0
  33. package/dist/browser/restore-queue-SG5O735W.mjs +196 -0
  34. package/dist/browser/{runtime-NYE5TB6O.mjs → runtime-OMKE7DF4.mjs} +83 -25
  35. package/dist/browser/segment-codec-leaf-A427QJUX.mjs +554 -0
  36. package/dist/browser/segment-merge-leaf-7BZ54JPI.mjs +1636 -0
  37. package/dist/browser/storage-leaf-5UTR5K7J.mjs +19829 -0
  38. package/dist/browser/store-leaf-3L33V35P.mjs +2070 -0
  39. package/dist/browser/suggestions-leaf-UBNDVKBD.mjs +1430 -0
  40. package/dist/browser/worker-copies-JXSGYR5X.mjs +2951 -0
  41. package/dist/{capabilities-BTTIs96E.d.ts → capabilities-DMte5K9_.d.ts} +162 -18
  42. package/dist/chunk-2AFZE7Y3.mjs +72 -0
  43. package/dist/chunk-2CKDCC65.mjs +47 -0
  44. package/dist/chunk-3ECIRFS5.mjs +61 -0
  45. package/dist/chunk-44W3CBNG.mjs +7 -0
  46. package/dist/chunk-4H4RG2FR.mjs +13 -0
  47. package/dist/chunk-4PY4JYMY.mjs +11 -0
  48. package/dist/chunk-4ZPVAHWF.mjs +1289 -0
  49. package/dist/chunk-5EI5CXO6.mjs +197 -0
  50. package/dist/chunk-5F5MXWS5.mjs +283 -0
  51. package/dist/{chunk-OHP7QR33.mjs → chunk-5FLRXF73.mjs} +25 -4
  52. package/dist/chunk-6FOENO4E.mjs +273 -0
  53. package/dist/chunk-6GHX35ZM.mjs +108 -0
  54. package/dist/chunk-6MSZ543Y.mjs +32 -0
  55. package/dist/chunk-6N6HHBCQ.mjs +4596 -0
  56. package/dist/{chunk-LXPIXDUJ.mjs → chunk-7P5YWDRQ.mjs} +403 -169
  57. package/dist/{chunk-TGP5M5R7.mjs → chunk-7T4LAYT4.mjs} +1 -1
  58. package/dist/{chunk-GGSH6KBQ.mjs → chunk-ASZLNVWW.mjs} +23 -3
  59. package/dist/chunk-AVBPB3MI.mjs +602 -0
  60. package/dist/{chunk-UCDF35PU.mjs → chunk-AVGUEEQ3.mjs} +494 -245
  61. package/dist/chunk-BUMREPLU.mjs +15 -0
  62. package/dist/chunk-BY6NALRK.mjs +31921 -0
  63. package/dist/chunk-C4ZMXXTL.mjs +92 -0
  64. package/dist/{chunk-2WYMG3OZ.mjs → chunk-C74KPRR7.mjs} +274 -19
  65. package/dist/{chunk-RSRVGAIQ.mjs → chunk-D456ZFPQ.mjs} +3 -2
  66. package/dist/chunk-DXZ6JDWG.mjs +797 -0
  67. package/dist/chunk-FJXJZUWF.mjs +209 -0
  68. package/dist/chunk-FS3HPPSC.mjs +212 -0
  69. package/dist/chunk-FT4KLZXW.mjs +1164 -0
  70. package/dist/chunk-GR5GMFJJ.mjs +104 -0
  71. package/dist/chunk-GXCDWLQI.mjs +193 -0
  72. package/dist/chunk-GXED6R3Q.mjs +271 -0
  73. package/dist/chunk-HLPRHOSO.mjs +17 -0
  74. package/dist/chunk-IL5DRCNU.mjs +3041 -0
  75. package/dist/{chunk-P36MUFEJ.mjs → chunk-JDDXV6UF.mjs} +2 -1
  76. package/dist/chunk-K5HSUPYS.mjs +11 -0
  77. package/dist/chunk-KYIRXTCA.mjs +100 -0
  78. package/dist/chunk-LFXS6OF7.mjs +289 -0
  79. package/dist/chunk-LNGE7SLY.mjs +151 -0
  80. package/dist/chunk-M2Z5WVWW.mjs +194 -0
  81. package/dist/{chunk-45AVKHMJ.mjs → chunk-M3B7SGJB.mjs} +63 -50
  82. package/dist/chunk-M3OW2XSP.mjs +40 -0
  83. package/dist/chunk-MFPSQZ6P.mjs +127 -0
  84. package/dist/chunk-MRCJBWJZ.mjs +1292 -0
  85. package/dist/chunk-MSIN4NM3.mjs +25 -0
  86. package/dist/chunk-MW3CJVGP.mjs +219 -0
  87. package/dist/chunk-MYM6CAVK.mjs +76 -0
  88. package/dist/chunk-NJJN4FMM.mjs +84 -0
  89. package/dist/chunk-NM7I3BM7.mjs +59 -0
  90. package/dist/chunk-OIJ64LGT.mjs +19 -0
  91. package/dist/{chunk-K7D4ZTSW.mjs → chunk-P4F4UWLC.mjs} +1 -1
  92. package/dist/chunk-Q2AP2FSN.mjs +15 -0
  93. package/dist/chunk-R2PJKSNL.mjs +54 -0
  94. package/dist/{chunk-HXJBSHXF.mjs → chunk-R724DJSG.mjs} +5056 -2619
  95. package/dist/chunk-S2POFTLN.mjs +10 -0
  96. package/dist/chunk-S5QMWECL.mjs +7 -0
  97. package/dist/chunk-SLAC5PAB.mjs +5008 -0
  98. package/dist/chunk-T4HMSZCX.mjs +5 -0
  99. package/dist/chunk-TACP6VK6.mjs +111 -0
  100. package/dist/chunk-TGPKS2UM.mjs +125 -0
  101. package/dist/chunk-TKKXPKEO.mjs +979 -0
  102. package/dist/chunk-V6EUBAIT.mjs +19 -0
  103. package/dist/chunk-XL56L4IR.mjs +228 -0
  104. package/dist/chunk-XQIAMDAG.mjs +3076 -0
  105. package/dist/chunk-YTUZ4R22.mjs +1063 -0
  106. package/dist/{chunk-KV3AZFT3.mjs → chunk-ZKOQR7EA.mjs} +45 -1
  107. package/dist/chunk-ZPJFGGGI.mjs +638 -0
  108. package/dist/chunk-ZXX5GZEX.mjs +17 -0
  109. package/dist/{client-i9eLJrw5.d.ts → client-BKqtl0_V.d.ts} +442 -22
  110. package/dist/client.d.ts +11 -10
  111. package/dist/client.mjs +8 -898
  112. package/dist/config-Bnl7L-0H.d.ts +608 -0
  113. package/dist/{config-P__Diiy4.d.ts → config-CJ-jfI0a.d.ts} +119 -9
  114. package/dist/core-leaf-6JVHEVFB.mjs +8 -0
  115. package/dist/custom-token-results-NXHBVY7U.mjs +59 -0
  116. package/dist/digest-leaf-KXBGKYHZ.mjs +1 -0
  117. package/dist/direct-executor-V3WOYU2Y.mjs +17 -0
  118. package/dist/distribution/coordinator/etcd.d.ts +63 -7
  119. package/dist/distribution/coordinator/etcd.mjs +725 -81
  120. package/dist/distribution/coordinator/in-memory.d.ts +3 -3
  121. package/dist/distribution/coordinator/in-memory.mjs +65 -10
  122. package/dist/distribution/transport/grpc.d.ts +2 -2
  123. package/dist/distribution/transport/grpc.mjs +39 -10
  124. package/dist/distribution/transport/in-memory.d.ts +2 -2
  125. package/dist/distribution/transport/in-memory.mjs +9 -6
  126. package/dist/distribution/transport/tcp.d.ts +2 -2
  127. package/dist/distribution/transport/tcp.mjs +20 -15
  128. package/dist/distribution.d.ts +267 -56
  129. package/dist/distribution.mjs +23156 -11362
  130. package/dist/durability-config-VWS2K24E.mjs +96 -0
  131. package/dist/embed-4PHHKGBK.mjs +263 -0
  132. package/dist/embeddings/openai.d.ts +1 -1
  133. package/dist/embeddings/openai.mjs +2 -2
  134. package/dist/{memory-CUEDwr-F.d.ts → engine-qjAxJSKk.d.ts} +2254 -1123
  135. package/dist/geo-index-2ZMMWWSK.mjs +1484 -0
  136. package/dist/gzip-2UCZW2MM.mjs +31 -0
  137. package/dist/{index-lcdGLyJH.d.ts → index-B_vmxpgW.d.ts} +260 -102
  138. package/dist/index.browser.mjs +4 -4
  139. package/dist/index.d.ts +2133 -57
  140. package/dist/index.mjs +16 -32
  141. package/dist/index.react-native.mjs +12 -0
  142. package/dist/invalidation/broadcast-channel.d.ts +1 -1
  143. package/dist/invalidation/filesystem.d.ts +1 -1
  144. package/dist/invalidation/filesystem.mjs +1 -2
  145. package/dist/invalidation/noop.d.ts +1 -1
  146. package/dist/languages/albanian.mjs +1 -1
  147. package/dist/languages/amharic.mjs +1 -1
  148. package/dist/languages/arabic.mjs +1 -1
  149. package/dist/languages/armenian.mjs +1 -1
  150. package/dist/languages/azerbaijani.mjs +1 -1
  151. package/dist/languages/bambara.mjs +1 -1
  152. package/dist/languages/basque.mjs +1 -1
  153. package/dist/languages/belarusian.mjs +1 -1
  154. package/dist/languages/bengali.mjs +1 -1
  155. package/dist/languages/bosnian.mjs +1 -1
  156. package/dist/languages/breton.mjs +1 -1
  157. package/dist/languages/bulgarian.mjs +1 -1
  158. package/dist/languages/burmese.mjs +1 -1
  159. package/dist/languages/catalan.mjs +1 -1
  160. package/dist/languages/chinese.mjs +1 -1
  161. package/dist/languages/croatian.mjs +1 -1
  162. package/dist/languages/czech.mjs +1 -1
  163. package/dist/languages/dagbani.mjs +1 -1
  164. package/dist/languages/danish.mjs +1 -1
  165. package/dist/languages/dutch.mjs +1 -1
  166. package/dist/languages/english.mjs +1 -1
  167. package/dist/languages/esperanto.mjs +1 -1
  168. package/dist/languages/estonian.mjs +1 -1
  169. package/dist/languages/ewe.mjs +1 -1
  170. package/dist/languages/faroese.mjs +1 -1
  171. package/dist/languages/fijian.mjs +1 -1
  172. package/dist/languages/finnish.mjs +1 -1
  173. package/dist/languages/french.mjs +1 -1
  174. package/dist/languages/ga.mjs +1 -1
  175. package/dist/languages/galician.mjs +1 -1
  176. package/dist/languages/georgian.mjs +1 -1
  177. package/dist/languages/german.mjs +1 -1
  178. package/dist/languages/greek.mjs +1 -1
  179. package/dist/languages/guarani.mjs +1 -1
  180. package/dist/languages/gujarati.mjs +1 -1
  181. package/dist/languages/haitian-creole.mjs +1 -1
  182. package/dist/languages/hausa.mjs +1 -1
  183. package/dist/languages/hawaiian.mjs +1 -1
  184. package/dist/languages/hebrew.mjs +1 -1
  185. package/dist/languages/hindi.mjs +1 -1
  186. package/dist/languages/hungarian.mjs +1 -1
  187. package/dist/languages/icelandic.mjs +1 -1
  188. package/dist/languages/igbo.mjs +1 -1
  189. package/dist/languages/indonesian.mjs +1 -1
  190. package/dist/languages/irish.mjs +1 -1
  191. package/dist/languages/italian.mjs +1 -1
  192. package/dist/languages/japanese.mjs +1 -1
  193. package/dist/languages/kannada.mjs +1 -1
  194. package/dist/languages/kazakh.mjs +1 -1
  195. package/dist/languages/khmer.mjs +1 -1
  196. package/dist/languages/kinyarwanda.mjs +1 -1
  197. package/dist/languages/kirundi.mjs +1 -1
  198. package/dist/languages/korean.mjs +1 -1
  199. package/dist/languages/kurmanji.mjs +1 -1
  200. package/dist/languages/kyrgyz.mjs +1 -1
  201. package/dist/languages/lao.mjs +1 -1
  202. package/dist/languages/latin.mjs +1 -1
  203. package/dist/languages/latvian.mjs +1 -1
  204. package/dist/languages/lingala.mjs +1 -1
  205. package/dist/languages/lithuanian.mjs +1 -1
  206. package/dist/languages/luxembourgish.mjs +1 -1
  207. package/dist/languages/macedonian.mjs +1 -1
  208. package/dist/languages/malagasy.mjs +1 -1
  209. package/dist/languages/malay.mjs +1 -1
  210. package/dist/languages/malayalam.mjs +1 -1
  211. package/dist/languages/maltese.mjs +1 -1
  212. package/dist/languages/maori.mjs +1 -1
  213. package/dist/languages/marathi.mjs +1 -1
  214. package/dist/languages/nepali.mjs +1 -1
  215. package/dist/languages/norwegian.mjs +1 -1
  216. package/dist/languages/oromo.mjs +1 -1
  217. package/dist/languages/persian.mjs +1 -1
  218. package/dist/languages/polish.mjs +1 -1
  219. package/dist/languages/portuguese.mjs +1 -1
  220. package/dist/languages/punjabi.mjs +1 -1
  221. package/dist/languages/romanian.mjs +1 -1
  222. package/dist/languages/russian.mjs +1 -1
  223. package/dist/languages/samoan.mjs +1 -1
  224. package/dist/languages/sanskrit.mjs +1 -1
  225. package/dist/languages/scottish-gaelic.mjs +1 -1
  226. package/dist/languages/serbian.mjs +1 -1
  227. package/dist/languages/shona.mjs +1 -1
  228. package/dist/languages/sinhala.mjs +1 -1
  229. package/dist/languages/slovak.mjs +1 -1
  230. package/dist/languages/slovenian.mjs +1 -1
  231. package/dist/languages/sorani.mjs +1 -1
  232. package/dist/languages/spanish.mjs +1 -1
  233. package/dist/languages/swahili.mjs +1 -1
  234. package/dist/languages/swedish.mjs +1 -1
  235. package/dist/languages/tagalog.mjs +1 -1
  236. package/dist/languages/tamil.mjs +1 -1
  237. package/dist/languages/tatar.mjs +1 -1
  238. package/dist/languages/telugu.mjs +1 -1
  239. package/dist/languages/thai.mjs +1 -1
  240. package/dist/languages/tibetan.mjs +1 -1
  241. package/dist/languages/tigrinya.mjs +1 -1
  242. package/dist/languages/tongan.mjs +1 -1
  243. package/dist/languages/turkish.mjs +1 -1
  244. package/dist/languages/twi.mjs +1 -1
  245. package/dist/languages/ukrainian.mjs +1 -1
  246. package/dist/languages/urdu.mjs +1 -1
  247. package/dist/languages/vietnamese.mjs +1 -1
  248. package/dist/languages/welsh.mjs +1 -1
  249. package/dist/languages/wolof.mjs +1 -1
  250. package/dist/languages/xhosa.mjs +1 -1
  251. package/dist/languages/yoruba.mjs +1 -1
  252. package/dist/languages/zulu.mjs +1 -1
  253. package/dist/long-query-thread-UCRK7MSI.mjs +154 -0
  254. package/dist/module-5ZSLUQII.mjs +3729 -0
  255. package/dist/msgpack-leaf-H5ZYW7S7.mjs +9 -0
  256. package/dist/optional-reads-5HFMF2QZ.mjs +3373 -0
  257. package/dist/optional-writes-4UOQKETU.mjs +247 -0
  258. package/dist/paged-partition-archive-BBRLZCIF.mjs +253 -0
  259. package/dist/partial-words.browser.mjs +1650 -0
  260. package/dist/partial-words.mjs +17 -11
  261. package/dist/{chunk-ASY5PHYI.mjs → pattern.browser.mjs} +253 -311
  262. package/dist/pattern.mjs +6 -7
  263. package/dist/persistence/durability/checkpoint-worker.d.ts +93 -25
  264. package/dist/persistence/durability/checkpoint-worker.mjs +51 -41
  265. package/dist/phrase-module-EFESWOCR.mjs +635 -0
  266. package/dist/query-actions-HMJUVWGT.mjs +119 -0
  267. package/dist/query-suggestions/merge-worker.d.ts +55 -0
  268. package/dist/query-suggestions/merge-worker.mjs +53 -0
  269. package/dist/query-suggestions-CBYkrQ5T.d.ts +311 -0
  270. package/dist/query-suggestions.browser.mjs +47 -0
  271. package/dist/query-suggestions.d.ts +20 -0
  272. package/dist/query-suggestions.mjs +19 -0
  273. package/dist/react-native/chunks/chunk-3OEFSHVV.mjs +970 -0
  274. package/dist/{browser/chunk-RELZRJNR.mjs → react-native/chunks/chunk-43R4GCM7.mjs} +49 -50
  275. package/dist/react-native/chunks/chunk-AKRYHAVG.mjs +6729 -0
  276. package/dist/react-native/chunks/chunk-CW2ZFXSE.mjs +14 -0
  277. package/dist/react-native/chunks/chunk-G3WCDTOF.mjs +29640 -0
  278. package/dist/react-native/chunks/chunk-GXXHHA2T.mjs +212 -0
  279. package/dist/react-native/chunks/chunk-H3MDVPBO.mjs +151 -0
  280. package/dist/react-native/chunks/chunk-HQEVQEDJ.mjs +52 -0
  281. package/dist/react-native/chunks/chunk-HRDMIQ63.mjs +131 -0
  282. package/dist/react-native/chunks/chunk-HTBY4GON.mjs +565 -0
  283. package/dist/react-native/chunks/chunk-LX62Y4B5.mjs +61 -0
  284. package/dist/react-native/chunks/chunk-MRH36RN3.mjs +902 -0
  285. package/dist/react-native/chunks/chunk-Q32DDFKX.mjs +19 -0
  286. package/dist/react-native/chunks/chunk-UE4ANHSX.mjs +127 -0
  287. package/dist/react-native/chunks/chunk-UNZPWLVL.mjs +1063 -0
  288. package/dist/react-native/chunks/chunk-UO44MA5M.mjs +232 -0
  289. package/dist/react-native/chunks/chunk-UYKIV2KT.mjs +42 -0
  290. package/dist/react-native/chunks/chunk-VUJCSQG5.mjs +11 -0
  291. package/dist/react-native/chunks/chunk-WVHF4ZAK.mjs +802 -0
  292. package/dist/react-native/chunks/chunk-ZUJCUPVS.mjs +54 -0
  293. package/dist/react-native/chunks/core-leaf-S2KNBWDY.mjs +7 -0
  294. package/dist/react-native/chunks/custom-token-results-3OZEMRE7.mjs +59 -0
  295. package/dist/react-native/chunks/digest-leaf-X6OIFDIU.mjs +1 -0
  296. package/dist/react-native/chunks/durability-config-FCSD4MXY.mjs +96 -0
  297. package/dist/react-native/chunks/embed-HA2I7RCF.mjs +263 -0
  298. package/dist/react-native/chunks/geo-index-QEYKEVPH.mjs +1484 -0
  299. package/dist/react-native/chunks/gzip.react-native-CVM4NQG2.mjs +12 -0
  300. package/dist/react-native/chunks/module-FMBX75GP.mjs +5209 -0
  301. package/dist/react-native/chunks/msgpack-leaf-JMNGSTRW.mjs +7 -0
  302. package/dist/react-native/chunks/optional-reads-AFPO2FEB.mjs +3364 -0
  303. package/dist/react-native/chunks/optional-writes-XOH25XZI.mjs +296 -0
  304. package/dist/react-native/chunks/phrase-module-IGYBK7XH.mjs +636 -0
  305. package/dist/react-native/chunks/replace-IMZUVUOE.mjs +57 -0
  306. package/dist/react-native/chunks/resolve-vector-text-GE2CRJKI.mjs +84 -0
  307. package/dist/react-native/chunks/restore-queue-LLNMYB4Z.mjs +187 -0
  308. package/dist/react-native/chunks/runtime-UOHD2XUJ.mjs +615 -0
  309. package/dist/react-native/chunks/segment-codec-leaf-VR6O5GLC.mjs +13 -0
  310. package/dist/react-native/chunks/segment-merge-leaf-LLMWU4EW.mjs +14 -0
  311. package/dist/react-native/chunks/storage-leaf-JD7PIDO4.mjs +19633 -0
  312. package/dist/react-native/chunks/store-leaf-4UVCMYGN.mjs +15 -0
  313. package/dist/react-native/chunks/suggestions-leaf-J5VT5RHY.mjs +1321 -0
  314. package/dist/react-native/chunks/worker-copies-ZYHY7FKJ.mjs +2831 -0
  315. package/dist/react-native/partial-words.mjs +1658 -0
  316. package/dist/react-native/pattern.mjs +2570 -0
  317. package/dist/react-native/query-suggestions.mjs +55 -0
  318. package/dist/react-native/sync.mjs +2759 -0
  319. package/dist/react-native/synonyms.mjs +676 -0
  320. package/dist/react.d.ts +12 -8
  321. package/dist/react.mjs +45 -1
  322. package/dist/{rerank-NWTKK636.mjs → rerank-RI2PMNOD.mjs} +22 -12
  323. package/dist/rerankers/http.d.ts +1 -1
  324. package/dist/rerankers/http.mjs +2 -2
  325. package/dist/resolve-vector-text-OD4J3WHF.mjs +3 -0
  326. package/dist/restore-queue-PCKWDAKQ.mjs +192 -0
  327. package/dist/runtime-USQYKN3I.mjs +615 -0
  328. package/dist/{schema-4W_7tk1f.d.ts → schema-CzG9OUw5.d.ts} +70 -30
  329. package/dist/segment-codec-leaf-QJM7I6QG.mjs +18 -0
  330. package/dist/segment-merge-leaf-4BJGIJHY.mjs +18 -0
  331. package/dist/server.d.ts +348 -22
  332. package/dist/server.mjs +2211 -327
  333. package/dist/storage-leaf-GRRRI7UV.mjs +5962 -0
  334. package/dist/store-leaf-6VMMSVBE.mjs +20 -0
  335. package/dist/stored-values-BWIMpiT_.d.ts +25 -0
  336. package/dist/suggestions-leaf-4KPTMPTL.mjs +54 -0
  337. package/dist/sync/filesystem.d.ts +30 -0
  338. package/dist/sync/filesystem.mjs +288 -0
  339. package/dist/sync/indexeddb.browser.mjs +287 -0
  340. package/dist/sync/indexeddb.d.ts +30 -0
  341. package/dist/sync/indexeddb.mjs +242 -0
  342. package/dist/sync.browser.mjs +1271 -0
  343. package/dist/sync.d.ts +23 -0
  344. package/dist/sync.mjs +2712 -0
  345. package/dist/synonyms.browser.mjs +658 -0
  346. package/dist/synonyms.mjs +17 -7
  347. package/dist/tasks-65b6t6lk.d.ts +782 -0
  348. package/dist/thread-UJ3W5LVI.mjs +31 -0
  349. package/dist/{types-B3b68nfF.d.ts → types-Ck97nB3D.d.ts} +1 -1
  350. package/dist/types-CpCg-Gq-.d.ts +227 -0
  351. package/dist/{types-Cb9kiY5P.d.ts → types-DvncmvuQ.d.ts} +12 -1
  352. package/dist/vector/search-worker.d.ts +10 -2
  353. package/dist/vector/search-worker.mjs +11 -11
  354. package/dist/worker-copies-YSXZXIGC.mjs +2232 -0
  355. package/dist/workers/entry.mjs +75 -15
  356. package/openapi.json +6840 -0
  357. package/package.json +404 -19
  358. package/dist/browser/chunk-CN4I4FZU.mjs +0 -602
  359. package/dist/browser/chunk-HOZW3PKP.mjs +0 -9
  360. package/dist/browser/chunk-JPTQZWJQ.mjs +0 -10
  361. package/dist/browser/chunk-LD55ZGU3.mjs +0 -347
  362. package/dist/browser/chunk-NAFT3K6X.mjs +0 -24546
  363. package/dist/browser/chunk-NNG3EEB6.mjs +0 -2728
  364. package/dist/browser/chunk-QWES44AW.mjs +0 -473
  365. package/dist/browser/durability-wiring-PY3L7DES.mjs +0 -3679
  366. package/dist/browser/module-PDXYHT75.mjs +0 -2822
  367. package/dist/browser/release-76MXRPGV.mjs +0 -33
  368. package/dist/browser/restore-queue-DVIYJ7RU.mjs +0 -35
  369. package/dist/browser/snapshot-J57MZK6L.mjs +0 -1207
  370. package/dist/browser/write-L7FAQA47.mjs +0 -208
  371. package/dist/chunk-37CD3OKS.mjs +0 -87
  372. package/dist/chunk-5XNIRLFC.mjs +0 -96
  373. package/dist/chunk-7X5JST5L.mjs +0 -114
  374. package/dist/chunk-AV4IJKD2.mjs +0 -35
  375. package/dist/chunk-AY3AQ3UH.mjs +0 -73
  376. package/dist/chunk-BKB3XTMI.mjs +0 -707
  377. package/dist/chunk-C7SLA6CM.mjs +0 -19
  378. package/dist/chunk-COVCQ73F.mjs +0 -587
  379. package/dist/chunk-DJHHF2L5.mjs +0 -17
  380. package/dist/chunk-E74YLAIG.mjs +0 -24
  381. package/dist/chunk-FMWRCP2C.mjs +0 -57
  382. package/dist/chunk-GES7U2PM.mjs +0 -68
  383. package/dist/chunk-HJSN7UYG.mjs +0 -10
  384. package/dist/chunk-JFRNNBOM.mjs +0 -35
  385. package/dist/chunk-K25LZA54.mjs +0 -8484
  386. package/dist/chunk-KEHK2O6P.mjs +0 -13
  387. package/dist/chunk-KOKM4PQN.mjs +0 -52
  388. package/dist/chunk-KZ3NX2P7.mjs +0 -21
  389. package/dist/chunk-L3L5YFXC.mjs +0 -614
  390. package/dist/chunk-L5ORED7V.mjs +0 -25
  391. package/dist/chunk-LH4NZNJJ.mjs +0 -1731
  392. package/dist/chunk-LU2CE5AE.mjs +0 -507
  393. package/dist/chunk-LXWDQLTW.mjs +0 -30
  394. package/dist/chunk-MHOSCQCQ.mjs +0 -948
  395. package/dist/chunk-NKRBEGJZ.mjs +0 -1705
  396. package/dist/chunk-NTRMFARG.mjs +0 -462
  397. package/dist/chunk-NW4XDSP2.mjs +0 -212
  398. package/dist/chunk-OOEXLUGR.mjs +0 -117
  399. package/dist/chunk-ORBLD3EL.mjs +0 -6
  400. package/dist/chunk-OWNZYMRE.mjs +0 -3591
  401. package/dist/chunk-PPY2GSIB.mjs +0 -44
  402. package/dist/chunk-QY7GC2MW.mjs +0 -33
  403. package/dist/chunk-R6A773WK.mjs +0 -226
  404. package/dist/chunk-R6PR4WYZ.mjs +0 -68
  405. package/dist/chunk-R7EEKAJE.mjs +0 -593
  406. package/dist/chunk-RONEOEON.mjs +0 -426
  407. package/dist/chunk-VWULPWRJ.mjs +0 -7469
  408. package/dist/chunk-WCJKUGHB.mjs +0 -262
  409. package/dist/chunk-WQBCBKY3.mjs +0 -8
  410. package/dist/chunk-X5CV2E2B.mjs +0 -1
  411. package/dist/chunk-YA7XLE2R.mjs +0 -1256
  412. package/dist/chunk-YTJCGKM4.mjs +0 -56
  413. package/dist/chunk-ZG477IPV.mjs +0 -526
  414. package/dist/chunk-ZYNAMIYX.mjs +0 -113
  415. package/dist/config-BGB_fDtq.d.ts +0 -237
  416. package/dist/direct-executor-CXZ66ACP.mjs +0 -22
  417. package/dist/durability-wiring-QTHOGFMZ.mjs +0 -3229
  418. package/dist/engine-BGCEm9E_.d.ts +0 -577
  419. package/dist/module-3NONKBZX.mjs +0 -1860
  420. package/dist/release-BH3DKW3T.mjs +0 -33
  421. package/dist/restore-queue-JPRBUYII.mjs +0 -35
  422. package/dist/runtime-ZKYZFJJC.mjs +0 -177
  423. package/dist/snapshot-HOGXNO5W.mjs +0 -793
  424. package/dist/tasks-HG3rlxx6.d.ts +0 -302
  425. package/dist/thread-B657LQXK.mjs +0 -692
  426. package/dist/write-XLIGTXBU.mjs +0 -238
@@ -0,0 +1,3729 @@
1
+ import { resolveHybridFusion, reciprocalRankFusion, linearCombination, noteWaitingForMemory, admitsBackgroundBytes } from './chunk-GXED6R3Q.mjs';
2
+ export { linearCombination, reciprocalRankFusion, resolveHybridFusion } from './chunk-GXED6R3Q.mjs';
3
+ import { createVectorStore, requireOrdinalWithinLimit } from './chunk-AVGUEEQ3.mjs';
4
+ export { openSharedVectorField } from './chunk-AVGUEEQ3.mjs';
5
+ export { bindVectorHost } from './chunk-T4HMSZCX.mjs';
6
+ import { VECTOR_WORKER_COPIES_ALLOWED, DEFAULT_PROMOTION_THRESHOLD, DEFAULT_FILTER_THRESHOLD, defaultQuantizationFor, osqBitsOf, DEFAULT_VECTOR_METRIC, noBuildHistory, createOsqQuantizer, recordCheckpoint, assignStorePartitions, liveSize, STORE_ORDINAL_LAYOUT_GENERATION, writeSequenceOf, writeSequenceWords, storePinsOf, admissionAt, handOverBuildTurns, filterForOptions, STORE_UNPLACED_COUNT, STORE_LIVE_COUNT, STORE_REMOVED_COUNT, ESTIMATED_MS_PER_TOMBSTONE, graphNeedsRebuild, ESTIMATED_MS_PER_VECTOR_REBUILD, yieldBuildTurn, holdAdmission, fieldHandlesOf, ordinalFilterForSubsets, ordinalFilterValues, nativeScoresOf, toScore, ordinalFilterHas, threadedNativeCore, threadsHoldCurrentLayout, WORKER_COPY_MIN_VECTORS, SHUFFLED_REBUILD_MEMORY_RETRY_MS, nativeStoreScratchBytes, createOrdinalFilter, addToOrdinalFilter, magnitude, toDistance, requireBytesWithinLimit, VECTOR_MEMORY_LIMITS, emptyFieldBeforeRestore, SEARCH_ATTEMPTS_BEFORE_HOLDING_WRITES, BUILD_RETRY_MAX_DELAY_MS, BUILD_RETRY_FIRST_DELAY_MS, recalibrateFromStore, NO_LIVE_VECTOR_AT_POSITION, signatureOf, bytesToVectors, isDead, allLiveDocIds, sharedMemoryAvailable, euclideanDistance, dotProduct, cosineSimilarityWithMagnitudes, shuffleInPlace, createHNSWIndex, adoptGraph, releaseReplacedVectors, topLayerInParts, BUILD_STEPS_BETWEEN_CLOCK_CHECKS, createCentroidSums, liveOrdinals, RECALIBRATION_CHUNK_VECTORS, shuffleStep, requireWholeNeighbourLists, SEARCH_REFILL_RESERVE, centroidOfCentroidSums, meanOfCentroidSums, countOfCentroidSums, squaredLengthsOfCentroidSums, BUILD_CHUNK_SIZE, nativeStoreFor, nativeMetricCode, withNativeThreadSlot, SEARCH_SCAN_CHUNK_VECTORS, releasePinsHeldBy, releaseLocksHeldBy, DRIFT_CHECK_GROWTH_FACTOR, adoptLoadedCalibration, planCheckpoint, deserialize, RECALIBRATION_DRIFT_RATIO, DISK_ADOPTION_YIELD_INTERVAL } from './chunk-R724DJSG.mjs';
7
+ export { holdAdmission, releaseLocksHeldBy, releasePinsHeldBy, requireUsableVectorOptions } from './chunk-R724DJSG.mjs';
8
+ import { chargeHeapBytes, workerResourceLimits } from './chunk-2AFZE7Y3.mjs';
9
+ import { requirePinnedState, signalOfSearch, bindSearchSignal, createDocIdMap, resolveWorkerCount, resolveWorkerEntry, detectRuntime, spawnNodeWorker } from './chunk-FJXJZUWF.mjs';
10
+ import { MAX_RENUMBERED_SEARCH_ATTEMPTS, DISK_STORAGE_BLOCK_BYTES, turnsAreBudgeted, yieldBackgroundTurn, UNPLACED_FLAG_CHUNK_ORDINALS, holdWritesAndBackgroundWork, backgroundTurnIsSpent, GRAPH_BUILD_CHUNKS_IN_FLIGHT_PER_WORKER, GRAPH_BUILD_DISPATCH_CHUNK, NATIVE_THREAD_SCAN_CHUNK_ORDINALS, turnIsSpent, yieldTurn, noteThreadSlotLive, noteThreadSlotGone, VECTOR_SEARCH_TIMEOUT_MS, VECTOR_SEARCH_LOAD_TIMEOUT_MS, VECTOR_SCRATCH_SLOTS, VECTOR_INSERT_TIMEOUT_MS } from './chunk-5EI5CXO6.mjs';
11
+ import { RESULT_WINDOW } from './chunk-3ECIRFS5.mjs';
12
+ import { noteFallback, NATIVE_OK } from './chunk-ASZLNVWW.mjs';
13
+ import './chunk-ZXX5GZEX.mjs';
14
+ import './chunk-4PY4JYMY.mjs';
15
+ import { NarsilError, ErrorCodes, isNarsilError } from './chunk-ZKOQR7EA.mjs';
16
+ import { compareCodePoints } from './chunk-TGPKS2UM.mjs';
17
+
18
+ // src/engine/query/subset-search.ts
19
+ function filterSubsetsMatchNothing(subsets) {
20
+ for (const subset of subsets) {
21
+ for (let wordIndex = 0; wordIndex < subset.bits.length; wordIndex++) {
22
+ if (subset.bits[wordIndex] !== 0) return false;
23
+ }
24
+ }
25
+ return true;
26
+ }
27
+ function filterSubsetsRenumbered(subsets) {
28
+ return subsets.some((subset) => subset.owner.docStore.numberingGeneration() !== subset.numberingGeneration);
29
+ }
30
+ async function searchCurrentSubsets(fieldName, first, collectAgain, search2) {
31
+ let subsets = first;
32
+ for (let attempt = 1; ; attempt++) {
33
+ const answer = await search2(subsets);
34
+ if (!filterSubsetsRenumbered(subsets)) return answer;
35
+ if (attempt >= MAX_RENUMBERED_SEARCH_ATTEMPTS) {
36
+ throw new NarsilError(
37
+ ErrorCodes.WORKER_BUSY,
38
+ `The index renumbered its documents during each of ${attempt} filtered searches of vector field "${fieldName}"`,
39
+ { fieldName, attempts: attempt }
40
+ );
41
+ }
42
+ subsets = collectAgain();
43
+ }
44
+ }
45
+
46
+ // src/engine/query/vector.ts
47
+ function vectorFetchDepth(limit, offset, cursorDepth) {
48
+ const reach = cursorDepth + offset + limit;
49
+ if (reach > RESULT_WINDOW) {
50
+ throw new NarsilError(
51
+ ErrorCodes.SEARCH_RESULT_WINDOW_EXCEEDED,
52
+ `The engine pages a vector search through the first ${RESULT_WINDOW} results, and this page ends at ${reach}. Narrow the search with a filter or a similarity floor`,
53
+ { limit, offset, cursorDepth, window: RESULT_WINDOW }
54
+ );
55
+ }
56
+ return Math.min(reach + 1, RESULT_WINDOW);
57
+ }
58
+ function vectorClock(context) {
59
+ const signal = signalOfSearch(context.turns);
60
+ const clock = context.patternWork.clock;
61
+ if (signal === void 0) return clock;
62
+ const nativeWindow = clock?.nativeWindow;
63
+ const bound = {
64
+ check() {
65
+ signal.throwIfAborted();
66
+ clock?.check();
67
+ },
68
+ tick(units) {
69
+ signal.throwIfAborted();
70
+ clock?.tick(units);
71
+ },
72
+ ...nativeWindow === void 0 ? {} : { nativeWindow: () => nativeWindow.call(clock) }
73
+ };
74
+ bindSearchSignal(bound, signal);
75
+ return bound;
76
+ }
77
+ async function searchField(search2, k, host) {
78
+ const { vecIndex, vectorConfig, queryVector, context, filtered, rescoreWindow } = search2;
79
+ const options = {
80
+ clock: vectorClock(context),
81
+ metric: vectorConfig.metric ?? "cosine",
82
+ minSimilarity: vectorConfig.similarity ?? -Infinity,
83
+ efSearch: vectorConfig.efSearch,
84
+ oversample: vectorConfig.oversample,
85
+ ...rescoreWindow !== void 0 ? { rescoreWindow } : {}
86
+ };
87
+ if (filtered !== void 0) {
88
+ const outcome2 = await searchCurrentSubsets(
89
+ vecIndex.fieldName,
90
+ filtered.first,
91
+ filtered.collectAgain,
92
+ (filterSubsets) => vecIndex.searchParallel(queryVector, k, { ...options, filterSubsets })
93
+ );
94
+ requirePinnedState(context.pin);
95
+ return outcome2;
96
+ }
97
+ const filterPartitions = host.partitionsForVectorSearch(context.manager, vecIndex, context.partitionIds);
98
+ const outcome = await vecIndex.searchParallel(
99
+ queryVector,
100
+ k,
101
+ filterPartitions === void 0 ? options : { ...options, filterPartitions }
102
+ );
103
+ requirePinnedState(context.pin);
104
+ return outcome;
105
+ }
106
+ async function executeVectorSearch(host, params, context, limit, offset, cursorDepth = 0) {
107
+ const vectorConfig = params.vector;
108
+ if (!vectorConfig || !vectorConfig.value) {
109
+ return { scored: [], totalMatched: 0 };
110
+ }
111
+ const vecIndex = host.resolveVectorIndex(context, vectorConfig.field);
112
+ if (!vecIndex) {
113
+ return { scored: [], totalMatched: 0 };
114
+ }
115
+ const filtered = host.filteredSearchOf(params, context);
116
+ if (filtered !== void 0 && filterSubsetsMatchNothing(filtered.first)) {
117
+ return { scored: [], totalMatched: 0 };
118
+ }
119
+ const search2 = {
120
+ vecIndex,
121
+ vectorConfig,
122
+ queryVector: new Float32Array(vectorConfig.value),
123
+ context,
124
+ filtered,
125
+ rescoreWindow: context.rescoreWindow
126
+ };
127
+ const outcome = await searchField(search2, vectorFetchDepth(limit, offset, cursorDepth), host);
128
+ const scored = host.vectorResultsToScored(outcome.results);
129
+ return { scored, totalMatched: outcome.matched, matchedExact: outcome.matchedExact };
130
+ }
131
+ async function vectorLegWith(host, vectorConfig, queryVector, context, filtered, k, rescoreWindow) {
132
+ const vecIndex = host.resolveVectorIndex(context, vectorConfig.field);
133
+ if (!vecIndex) return { scored: [], holdsEveryMatch: true };
134
+ const search2 = { vecIndex, vectorConfig, queryVector, context, filtered, rescoreWindow };
135
+ const outcome = await searchField(search2, k, host);
136
+ return {
137
+ scored: host.vectorResultsToScored(outcome.results),
138
+ holdsEveryMatch: outcome.matchedExact && outcome.results.length === outcome.matched
139
+ };
140
+ }
141
+ async function executeHybridSearch(host, params, context, limit, offset, cursorDepth = 0) {
142
+ const { vector: vectorConfig, mode: _mode, hybrid: _hybrid, ...textOnlyParams } = params;
143
+ const fusion = resolveHybridFusion(params.hybrid);
144
+ const filtered = host.filteredSearchOf(params, context);
145
+ if (filtered !== void 0 && filterSubsetsMatchNothing(filtered.first)) {
146
+ return { scored: [], totalMatched: 0 };
147
+ }
148
+ if (!vectorConfig || !vectorConfig.value) {
149
+ const textOnly = await host.textLeg(textOnlyParams, context);
150
+ return {
151
+ scored: textOnly.scored,
152
+ totalMatched: textOnly.totalMatched,
153
+ facets: textOnly.facets,
154
+ ...textOnly.documents !== void 0 ? { documents: textOnly.documents } : {}
155
+ };
156
+ }
157
+ const vectorPending = vectorLegWith(
158
+ host,
159
+ vectorConfig,
160
+ new Float32Array(vectorConfig.value),
161
+ context,
162
+ filtered,
163
+ vectorFetchDepth(limit, offset, cursorDepth)
164
+ );
165
+ const [textFanOutResult, vectorOutcome] = await Promise.all([host.textLeg(textOnlyParams, context), vectorPending]);
166
+ const vectorScored = vectorOutcome.scored;
167
+ let fusedScored;
168
+ if (fusion.strategy === "rrf") {
169
+ fusedScored = reciprocalRankFusion([textFanOutResult.scored, vectorScored], { k: fusion.k });
170
+ } else {
171
+ fusedScored = linearCombination(textFanOutResult.scored, vectorScored, { alpha: fusion.alpha });
172
+ }
173
+ if (params.minScore !== void 0 && params.minScore > 0) {
174
+ const threshold = params.minScore;
175
+ fusedScored = fusedScored.filter((doc) => doc.score >= threshold);
176
+ }
177
+ const textHoldsEveryMatch = textFanOutResult.scored.length === textFanOutResult.totalMatched;
178
+ return {
179
+ scored: fusedScored,
180
+ totalMatched: fusedScored.length,
181
+ matchedExact: vectorOutcome.holdsEveryMatch && textHoldsEveryMatch,
182
+ facets: textFanOutResult.facets,
183
+ ...textFanOutResult.documents !== void 0 ? { documents: textFanOutResult.documents } : {}
184
+ };
185
+ }
186
+
187
+ // src/engine/vector-maintenance.ts
188
+ function resolveVectorIndexes(manager, indexName, fieldName) {
189
+ const all = manager.getVectorIndexes();
190
+ if (fieldName === void 0) return all;
191
+ const idx = all.get(fieldName);
192
+ if (!idx) {
193
+ throw new NarsilError(ErrorCodes.INDEX_NOT_FOUND, `Vector field "${fieldName}" not found in index "${indexName}"`, {
194
+ indexName,
195
+ fieldName
196
+ });
197
+ }
198
+ const single = /* @__PURE__ */ new Map();
199
+ single.set(fieldName, idx);
200
+ return single;
201
+ }
202
+ function compactVectors(manager, indexName, fieldName) {
203
+ const indexes = resolveVectorIndexes(manager, indexName, fieldName);
204
+ for (const [, idx] of indexes) {
205
+ idx.compact();
206
+ }
207
+ }
208
+ async function optimizeVectors(manager, indexName, fieldName) {
209
+ const indexes = resolveVectorIndexes(manager, indexName, fieldName);
210
+ for (const [, idx] of indexes) {
211
+ await idx.optimize();
212
+ }
213
+ }
214
+ function getVectorMaintenanceStatus(manager) {
215
+ const indexes = manager.getVectorIndexes();
216
+ const results = [];
217
+ for (const [field, idx] of indexes) {
218
+ const status = idx.maintenanceStatus();
219
+ results.push({
220
+ fieldName: field,
221
+ tombstoneRatio: status.tombstoneRatio,
222
+ graphCount: status.graphCount,
223
+ bufferSize: status.bufferSize,
224
+ building: status.building,
225
+ waitingForMemory: status.waitingForMemory,
226
+ loadingFromFiles: status.loadingFromFiles === true,
227
+ estimatedCompactMs: status.estimatedCompactMs,
228
+ estimatedOptimizeMs: status.estimatedOptimizeMs
229
+ });
230
+ }
231
+ return results;
232
+ }
233
+
234
+ // src/vector/query-values.ts
235
+ function isFloat32Representable(value) {
236
+ return typeof value === "number" && Number.isFinite(Math.fround(value));
237
+ }
238
+ function requireUsableQueryVector(value) {
239
+ if (value === void 0) return;
240
+ if (!Array.isArray(value) && !(value instanceof Float32Array)) {
241
+ throw new NarsilError(ErrorCodes.DOC_VALIDATION_FAILED, `Vector query 'value' must be an array of numbers`, {
242
+ received: typeof value
243
+ });
244
+ }
245
+ for (let index = 0; index < value.length; index++) {
246
+ if (isFloat32Representable(value[index])) continue;
247
+ throw new NarsilError(
248
+ ErrorCodes.DOC_VALIDATION_FAILED,
249
+ `Vector query 'value' element at index ${index} is not a number that a 32-bit float holds as a finite value`,
250
+ { index, value: value[index] }
251
+ );
252
+ }
253
+ }
254
+
255
+ // src/core/heap.ts
256
+ function createMaxHeap(compare) {
257
+ return createHeap((a, b) => compare(b, a));
258
+ }
259
+ function createBoundedMaxHeap(compare, capacity) {
260
+ const heap = createMaxHeap(compare);
261
+ const innerPush = heap.push;
262
+ return {
263
+ push(item) {
264
+ if (heap.size < capacity) {
265
+ innerPush(item);
266
+ return;
267
+ }
268
+ const top = heap.peek();
269
+ if (top !== void 0 && compare(item, top) < 0) {
270
+ heap.pop();
271
+ innerPush(item);
272
+ }
273
+ },
274
+ pop: heap.pop,
275
+ peek: heap.peek,
276
+ get size() {
277
+ return heap.size;
278
+ },
279
+ toSortedArray: heap.toSortedArray
280
+ };
281
+ }
282
+ function createHeap(compare) {
283
+ const items = [];
284
+ function siftUp(idx) {
285
+ while (idx > 0) {
286
+ const parent = idx - 1 >> 1;
287
+ if (compare(items[idx], items[parent]) >= 0) break;
288
+ const tmp = items[idx];
289
+ items[idx] = items[parent];
290
+ items[parent] = tmp;
291
+ idx = parent;
292
+ }
293
+ }
294
+ function siftDown(idx) {
295
+ const len = items.length;
296
+ while (true) {
297
+ let smallest = idx;
298
+ const left = 2 * idx + 1;
299
+ const right = 2 * idx + 2;
300
+ if (left < len && compare(items[left], items[smallest]) < 0) smallest = left;
301
+ if (right < len && compare(items[right], items[smallest]) < 0) smallest = right;
302
+ if (smallest === idx) break;
303
+ const tmp = items[idx];
304
+ items[idx] = items[smallest];
305
+ items[smallest] = tmp;
306
+ idx = smallest;
307
+ }
308
+ }
309
+ return {
310
+ push(item) {
311
+ items.push(item);
312
+ siftUp(items.length - 1);
313
+ },
314
+ pop() {
315
+ if (items.length === 0) return void 0;
316
+ const top = items[0];
317
+ const last = items.pop();
318
+ if (items.length > 0) {
319
+ items[0] = last;
320
+ siftDown(0);
321
+ }
322
+ return top;
323
+ },
324
+ peek() {
325
+ return items[0];
326
+ },
327
+ get size() {
328
+ return items.length;
329
+ },
330
+ toSortedArray() {
331
+ return [...items].sort(compare);
332
+ }
333
+ };
334
+ }
335
+
336
+ // src/vector/vector-index/merge-results.ts
337
+ function highScoreFirst(a, b) {
338
+ return b.score - a.score || compareCodePoints(a.docId, b.docId);
339
+ }
340
+ function mergeResults(hnswResults, bufferResults, k) {
341
+ const seen = /* @__PURE__ */ new Set();
342
+ const merged = [];
343
+ let hi = 0;
344
+ let bi = 0;
345
+ while (merged.length < k && (hi < hnswResults.length || bi < bufferResults.length)) {
346
+ const h = hi < hnswResults.length ? hnswResults[hi] : void 0;
347
+ const b = bi < bufferResults.length ? bufferResults[bi] : void 0;
348
+ let pick;
349
+ if (h && b) {
350
+ if (h.score > b.score || h.score === b.score && compareCodePoints(h.docId, b.docId) < 0) {
351
+ pick = h;
352
+ hi++;
353
+ } else {
354
+ pick = b;
355
+ bi++;
356
+ }
357
+ } else if (h) {
358
+ pick = h;
359
+ hi++;
360
+ } else if (b) {
361
+ pick = b;
362
+ bi++;
363
+ } else {
364
+ break;
365
+ }
366
+ if (seen.has(pick.docId)) continue;
367
+ seen.add(pick.docId);
368
+ merged.push(pick);
369
+ }
370
+ return merged;
371
+ }
372
+
373
+ // src/vector/shared-field/store-flags.ts
374
+ function bytesOf(flags, source) {
375
+ if (flags.source !== source) {
376
+ flags.source = source;
377
+ flags.view = new Uint8Array(source);
378
+ }
379
+ return flags.view;
380
+ }
381
+ function countsOf(chunks, source) {
382
+ if (chunks.source !== source) {
383
+ chunks.source = source;
384
+ chunks.view = new Int32Array(source);
385
+ }
386
+ return chunks.view;
387
+ }
388
+ function readStoreFlags(store) {
389
+ const unplaced = { source: null, view: new Uint8Array(0) };
390
+ const unplacedChunks = { source: null, view: new Int32Array(0) };
391
+ const removed = { source: null, view: new Uint8Array(0) };
392
+ return {
393
+ liveDocuments() {
394
+ const header = store.handles.header;
395
+ return Atomics.load(header, STORE_LIVE_COUNT) - Atomics.load(header, STORE_REMOVED_COUNT);
396
+ },
397
+ isRemoved(ordinal) {
398
+ const bytes = bytesOf(removed, store.handles.removed);
399
+ return ordinal < bytes.length && Atomics.load(bytes, ordinal) === 1;
400
+ },
401
+ unplacedOrdinals() {
402
+ const handles = store.handles;
403
+ if (Atomics.load(handles.header, STORE_UNPLACED_COUNT) === 0) return [];
404
+ const bytes = bytesOf(unplaced, handles.unplaced);
405
+ const counts = countsOf(unplacedChunks, handles.unplacedChunks);
406
+ const end = Math.min(store.slots, bytes.length);
407
+ const ordinals = [];
408
+ for (let chunk = 0; chunk < counts.length; chunk++) {
409
+ const from = chunk * UNPLACED_FLAG_CHUNK_ORDINALS;
410
+ if (from >= end) break;
411
+ if (Atomics.load(counts, chunk) === 0) continue;
412
+ const to = Math.min(end, from + UNPLACED_FLAG_CHUNK_ORDINALS);
413
+ for (let ordinal = from; ordinal < to; ordinal++) {
414
+ if (Atomics.load(bytes, ordinal) === 1) ordinals.push(ordinal);
415
+ }
416
+ }
417
+ return ordinals;
418
+ }
419
+ };
420
+ }
421
+
422
+ // src/vector/shared-field/searcher.ts
423
+ function* storeOrdinals(store) {
424
+ const slots = store.slots;
425
+ for (let ordinal = 0; ordinal < slots; ordinal++) yield ordinal;
426
+ }
427
+ function distancesInTypeScript(store, query, metric, ordinals, admission, clock) {
428
+ noteFallback("score");
429
+ const arenaQuery = store.prepareQueryArena(query);
430
+ const queryMagnitude = arenaQuery ? arenaQuery.magnitude : magnitude(query);
431
+ const distances = new Float64Array(ordinals.length).fill(Number.POSITIVE_INFINITY);
432
+ for (let i = 0; i < ordinals.length; i++) {
433
+ if (clock !== void 0) clock.tick(query.length);
434
+ if (!admission.admits(ordinals[i])) continue;
435
+ if (arenaQuery) {
436
+ distances[i] = store.distanceFromArena(arenaQuery, ordinals[i], metric);
437
+ continue;
438
+ }
439
+ const entry = store.entryForOrdinal(ordinals[i]);
440
+ if (entry) distances[i] = toDistance(query, entry.vector, queryMagnitude, entry.magnitude, metric);
441
+ }
442
+ return distances;
443
+ }
444
+ function scoreCandidates(scan, candidates) {
445
+ const { store, query, k, metric, minSimilarity, admission, clock } = scan;
446
+ const { ordinals, docIds } = candidates;
447
+ if (ordinals.length === 0) return { results: [], matched: 0 };
448
+ clock?.check();
449
+ const distances = nativeScoresOf(store.handles, query, metric, ordinals, admission.pin) ?? distancesInTypeScript(store, query, metric, ordinals, admission, clock);
450
+ clock?.check();
451
+ const heap = createBoundedMaxHeap(highScoreFirst, k);
452
+ let matched = 0;
453
+ for (let i = 0; i < ordinals.length; i++) {
454
+ if (distances[i] === Number.POSITIVE_INFINITY) continue;
455
+ const score = toScore(distances[i], metric);
456
+ if (score < minSimilarity) continue;
457
+ matched++;
458
+ heap.push({ docId: docIds[i], score, ordinal: ordinals[i] });
459
+ }
460
+ return { results: heap.toSortedArray().reverse(), matched };
461
+ }
462
+ function createSharedVectorSearcher(options) {
463
+ const { fieldName, view, holdsDocument } = options;
464
+ const flags = readStoreFlags(view.store);
465
+ function ordinalLayoutGeneration() {
466
+ return Atomics.load(view.store.handles.header, STORE_ORDINAL_LAYOUT_GENERATION);
467
+ }
468
+ function ordinalSpace(admission) {
469
+ const store = view.store;
470
+ const layoutGeneration2 = ordinalLayoutGeneration();
471
+ let retained;
472
+ function ordinalOf(docId) {
473
+ const current = view.ordinalOf(docId);
474
+ if (admission === void 0 || current !== void 0 && admission.admits(current)) return current;
475
+ if (retained === void 0) {
476
+ retained = /* @__PURE__ */ new Map();
477
+ for (let ordinal = 0; ordinal < store.slots; ordinal++) {
478
+ const id = admission.docIdOf(ordinal);
479
+ if (id !== void 0) retained.set(id, ordinal);
480
+ }
481
+ }
482
+ return retained.get(docId);
483
+ }
484
+ return {
485
+ fieldName,
486
+ layoutGeneration: admission === void 0 ? layoutGeneration2 : -admission.pin - 1,
487
+ slots: store.slots,
488
+ header: store.handles.header,
489
+ ordinalOf,
490
+ holdsCurrentOrdinal: (ordinal) => admission === void 0 ? store.holdsOrdinal(ordinal) && store.partitionAt(ordinal) !== void 0 : admission.admits(ordinal),
491
+ isRemoved: (ordinal) => admission === void 0 ? flags.isRemoved(ordinal) : !admission.admits(ordinal)
492
+ };
493
+ }
494
+ function filterForPartitions(allowed, admission) {
495
+ const slots = view.store.slots;
496
+ const filter = createOrdinalFilter(slots);
497
+ for (let ordinal = 0; ordinal < slots; ordinal++) {
498
+ const partition = view.store.partitionAt(ordinal);
499
+ if (partition === void 0 || !allowed.has(partition)) continue;
500
+ if (admission === void 0 ? !view.store.holdsOrdinal(ordinal) || flags.isRemoved(ordinal) : !admission.admits(ordinal))
501
+ continue;
502
+ addToOrdinalFilter(filter, ordinal);
503
+ }
504
+ return filter;
505
+ }
506
+ function filterFor(searchOptions, admission) {
507
+ if (searchOptions.filterSubsets !== void 0) {
508
+ return ordinalFilterForSubsets(searchOptions.filterSubsets, ordinalSpace(admission));
509
+ }
510
+ if (searchOptions.filterPartitions !== void 0)
511
+ return filterForPartitions(searchOptions.filterPartitions, admission);
512
+ return void 0;
513
+ }
514
+ function candidatesOf(ordinals, filter, admission) {
515
+ const candidates = { ordinals: [], docIds: [] };
516
+ for (const ordinal of ordinals) {
517
+ if (filter !== void 0 && !ordinalFilterHas(filter, ordinal)) continue;
518
+ const docId = admission.docIdOf(ordinal);
519
+ if (docId === void 0) continue;
520
+ candidates.ordinals.push(ordinal);
521
+ candidates.docIds.push(docId);
522
+ }
523
+ return candidates;
524
+ }
525
+ function answerOf(outcome) {
526
+ const results = outcome.results.filter((result) => holdsDocument(result.docId));
527
+ const holdsEveryHit = outcome.resolvedEveryHit && results.length === outcome.results.length;
528
+ return { results, matched: outcome.matched, matchedExact: outcome.matchedExact && holdsEveryHit };
529
+ }
530
+ function scanned(scan) {
531
+ return { results: scan.results, matched: scan.matched, matchedExact: true, resolvedEveryHit: true };
532
+ }
533
+ function search2(query, k, searchOptions, filter, admission) {
534
+ const { metric, minSimilarity, efSearch, oversample, rescoreWindow } = searchOptions;
535
+ const clock = searchOptions.clock;
536
+ const scan = { store: view.store, query, k, metric, minSimilarity, admission, clock };
537
+ const empty = { results: [], matched: 0, matchedExact: true, resolvedEveryHit: true };
538
+ const liveDocuments = flags.liveDocuments();
539
+ if (liveDocuments <= 0) return empty;
540
+ if (filter !== void 0 && filter.count === 0) return empty;
541
+ function fetched(results, resolvedEveryHit2) {
542
+ if (minSimilarity === Number.NEGATIVE_INFINITY) {
543
+ const matched = filter === void 0 ? liveDocuments : filter.count;
544
+ return { results, matched, matchedExact: true, resolvedEveryHit: resolvedEveryHit2 };
545
+ }
546
+ return { results, matched: results.length, matchedExact: false, resolvedEveryHit: resolvedEveryHit2 };
547
+ }
548
+ if (k <= 0) return fetched([], true);
549
+ const graph = view.graph;
550
+ if (graph === null) {
551
+ const ordinals = filter === void 0 ? storeOrdinals(view.store) : ordinalFilterValues(filter);
552
+ return scanned(scoreCandidates(scan, candidatesOf(ordinals, filter, admission)));
553
+ }
554
+ if (filter !== void 0) {
555
+ const graphLiveSize = view.liveSize;
556
+ const selectivity = graphLiveSize > 0 ? filter.count / graphLiveSize : 1;
557
+ if (selectivity < view.handles.filterThreshold) {
558
+ return scanned(scoreCandidates(scan, candidatesOf(ordinalFilterValues(filter), filter, admission)));
559
+ }
560
+ }
561
+ const unplaced = flags.unplacedOrdinals();
562
+ let resolvedEveryHit = true;
563
+ const graphResults = [];
564
+ for (const hit of view.searchOrdinals(query, k, metric, minSimilarity, {
565
+ filter,
566
+ efSearch,
567
+ oversample,
568
+ rescoreWindow,
569
+ clock,
570
+ admission
571
+ })) {
572
+ const docId = admission.docIdOf(hit.ord);
573
+ if (docId === void 0) {
574
+ resolvedEveryHit = false;
575
+ continue;
576
+ }
577
+ graphResults.push({ docId, score: hit.score, ordinal: hit.ord });
578
+ }
579
+ if (unplaced.length === 0) return fetched(graphResults, resolvedEveryHit);
580
+ const unplacedScan = scoreCandidates(scan, candidatesOf(unplaced, filter, admission));
581
+ return fetched(mergeResults(graphResults, unplacedScan.results, k), resolvedEveryHit);
582
+ }
583
+ function searchAtOnePin(query, k, searchOptions) {
584
+ const held = searchOptions.admission;
585
+ if (held !== void 0) {
586
+ const filter = filterFor(searchOptions, held);
587
+ const sequence = writeSequenceOf(writeSequenceWords(view.store.handles.header));
588
+ const outcome = sequence === held.pin && !view.graphRetired ? search2(query, k, searchOptions, filter, held) : void 0;
589
+ if (outcome !== void 0 && writeSequenceOf(writeSequenceWords(view.store.handles.header)) === held.pin && !view.graphRetired)
590
+ return outcome;
591
+ const ordinals = [];
592
+ for (let ordinal = 0; ordinal < view.store.slots; ordinal++) {
593
+ if (held.admits(ordinal)) ordinals.push(ordinal);
594
+ }
595
+ return scanned(
596
+ scoreCandidates(
597
+ {
598
+ store: view.store,
599
+ query,
600
+ k,
601
+ metric: searchOptions.metric,
602
+ minSimilarity: searchOptions.minSimilarity,
603
+ admission: held,
604
+ clock: searchOptions.clock
605
+ },
606
+ candidatesOf(ordinals, filter, held)
607
+ )
608
+ );
609
+ }
610
+ const pins = storePinsOf(view.store);
611
+ const pin = pins.take();
612
+ try {
613
+ return search2(query, k, searchOptions, filterFor(searchOptions), admissionAt(view.store, pin));
614
+ } finally {
615
+ pins.release(pin);
616
+ }
617
+ }
618
+ return {
619
+ fieldName,
620
+ dimension: view.handles.dimension,
621
+ partitionsKnown: () => true,
622
+ assignPartitions: () => void 0,
623
+ searchParallel(query, k, searchOptions) {
624
+ if (query.length !== view.handles.dimension) {
625
+ throw new NarsilError(
626
+ ErrorCodes.VECTOR_DIMENSION_MISMATCH,
627
+ `Vector dimension mismatch: expected ${view.handles.dimension}, got ${query.length}`,
628
+ { expected: view.handles.dimension, received: query.length }
629
+ );
630
+ }
631
+ if (searchOptions.admission !== void 0)
632
+ return Promise.resolve(answerOf(searchAtOnePin(query, k, searchOptions)));
633
+ for (let attempt = 1; ; attempt++) {
634
+ const layoutGeneration2 = ordinalLayoutGeneration();
635
+ const outcome = searchAtOnePin(query, k, searchOptions);
636
+ if (view.graphRetired && searchOptions.admission === void 0) {
637
+ throw new NarsilError(
638
+ ErrorCodes.WORKER_BUSY,
639
+ `The main thread replaced the graph of vector field "${fieldName}" while this thread searched it`,
640
+ { fieldName }
641
+ );
642
+ }
643
+ if (ordinalLayoutGeneration() === layoutGeneration2) return Promise.resolve(answerOf(outcome));
644
+ if (attempt >= MAX_RENUMBERED_SEARCH_ATTEMPTS) {
645
+ throw new NarsilError(
646
+ ErrorCodes.WORKER_BUSY,
647
+ `The main thread renumbered the vectors of field "${fieldName}" during each of ${attempt} searches on this thread`,
648
+ { fieldName, attempts: attempt }
649
+ );
650
+ }
651
+ }
652
+ }
653
+ };
654
+ }
655
+ function closeIdleTurnHop() {
656
+ }
657
+
658
+ // src/vector/vector-index/graph-pass.ts
659
+ function holdGraph(state, graph) {
660
+ const current = state.graphsHeldOffThread;
661
+ if (current !== null) {
662
+ current.graphs.push(graph);
663
+ return current;
664
+ }
665
+ let release = () => void 0;
666
+ const released = new Promise((resolve) => {
667
+ release = resolve;
668
+ });
669
+ const held = { graphs: [graph], released, release, writesAfter: [] };
670
+ state.graphsHeldOffThread = held;
671
+ return held;
672
+ }
673
+ function applyWritesHeldBack(held) {
674
+ const writes = held.writesAfter.splice(0);
675
+ let firstError = null;
676
+ for (const write of writes) {
677
+ try {
678
+ write();
679
+ } catch (error) {
680
+ firstError ??= error;
681
+ }
682
+ }
683
+ if (firstError !== null) throw firstError;
684
+ }
685
+ function releaseGraph(state, held, graph) {
686
+ const at = held.graphs.indexOf(graph);
687
+ if (at !== -1) held.graphs.splice(at, 1);
688
+ if (held.graphs.length > 0) return;
689
+ if (state.graphsHeldOffThread === held) state.graphsHeldOffThread = null;
690
+ try {
691
+ applyWritesHeldBack(held);
692
+ } finally {
693
+ held.release();
694
+ }
695
+ }
696
+ async function connectGraph(state, graph) {
697
+ const offThread = graph.connectOffThread();
698
+ if (offThread === null) {
699
+ await graph.connectOnThisThread(turnsAreBudgeted);
700
+ return;
701
+ }
702
+ const held = holdGraph(state, graph);
703
+ let connected = false;
704
+ try {
705
+ connected = await offThread;
706
+ } finally {
707
+ releaseGraph(state, held, graph);
708
+ }
709
+ if (!connected && !state.disposed) await graph.connectOnThisThread(true);
710
+ }
711
+ async function untilGraphReleased(state) {
712
+ for (; ; ) {
713
+ const held = state.graphsHeldOffThread;
714
+ if (held === null) return;
715
+ await held.released;
716
+ }
717
+ }
718
+ function waitOutGraphPass(state) {
719
+ const held = state.graphsHeldOffThread;
720
+ if (held === null) return;
721
+ for (const graph of held.graphs) graph.exclusively(() => void 0);
722
+ applyWritesHeldBack(held);
723
+ }
724
+ function withGraphFree(state, operation) {
725
+ return (...args) => {
726
+ waitOutGraphPass(state);
727
+ return operation(...args);
728
+ };
729
+ }
730
+ function onceGraphFree(state, operation) {
731
+ return async (...args) => {
732
+ await untilGraphReleased(state);
733
+ return operation(...args);
734
+ };
735
+ }
736
+ function afterGraphPass(state, write) {
737
+ const held = state.graphsHeldOffThread;
738
+ if (held === null) write();
739
+ else held.writesAfter.push(write);
740
+ }
741
+
742
+ // src/vector/vector-index/search-writes.ts
743
+ function noteSearchedDocWritten(state, docId) {
744
+ for (const watch of state.searchWatches) watch.written.add(docId);
745
+ }
746
+ function moveOutOfBuffer(state, docId) {
747
+ if (!state.buffer.delete(docId)) return;
748
+ for (const watch of state.searchWatches) watch.leftBuffer.add(docId);
749
+ }
750
+ function writeWatchesOf(state) {
751
+ return {
752
+ watchWrites(listener) {
753
+ state.writeListeners.add(listener);
754
+ return () => {
755
+ state.writeListeners.delete(listener);
756
+ };
757
+ }
758
+ };
759
+ }
760
+ async function watchingSearch(state, search2) {
761
+ const watch = { written: /* @__PURE__ */ new Set(), leftBuffer: /* @__PURE__ */ new Set() };
762
+ state.searchWatches.add(watch);
763
+ try {
764
+ return await search2(watch);
765
+ } finally {
766
+ state.searchWatches.delete(watch);
767
+ }
768
+ }
769
+
770
+ // src/vector/search-pool/worker-entry.ts
771
+ function resolveWorkerEntryPoint() {
772
+ return resolveWorkerEntry(import.meta.url, /\/src\/vector\/search-pool\/[^/]+$/, "vector/search-worker.mjs");
773
+ }
774
+
775
+ // src/vector/search-pool/constants.ts
776
+ var VECTOR_POOL_DRAIN_TIMEOUT_MS = 1e4;
777
+
778
+ // src/vector/search-pool/slot.ts
779
+ function createSlot(worker, scratchSlot) {
780
+ noteThreadSlotLive(scratchSlot);
781
+ return {
782
+ worker,
783
+ scratchSlot,
784
+ pending: /* @__PURE__ */ new Map(),
785
+ alive: true,
786
+ outstanding: 0,
787
+ fields: /* @__PURE__ */ new Map(),
788
+ working: /* @__PURE__ */ new Map(),
789
+ idleWaiters: /* @__PURE__ */ new Set()
790
+ };
791
+ }
792
+ function noteFieldOpened(slot, handle, handles) {
793
+ slot.fields.set(handle, handles);
794
+ }
795
+ function noteWorkStarted(slot, requestId, handle) {
796
+ slot.working.set(requestId, handle);
797
+ }
798
+ function stillWorkingOn(slot, handle) {
799
+ for (const working of slot.working.values()) if (working === handle) return true;
800
+ return false;
801
+ }
802
+ function forgetDroppedGraphs(slot, shared) {
803
+ for (const handle of [...slot.fields.keys()]) {
804
+ if (!shared.has(handle) && !stillWorkingOn(slot, handle)) slot.fields.delete(handle);
805
+ }
806
+ }
807
+ function noteAnswered(slot, requestId, shared) {
808
+ if (!slot.working.delete(requestId)) return;
809
+ forgetDroppedGraphs(slot, shared);
810
+ if (slot.working.size > 0) return;
811
+ for (const waiter of [...slot.idleWaiters]) waiter();
812
+ }
813
+ function untilIdle(slot, deadlineMs) {
814
+ if (slot.working.size === 0) return Promise.resolve();
815
+ return new Promise((resolve) => {
816
+ const finish = () => {
817
+ clearTimeout(timer);
818
+ slot.idleWaiters.delete(finish);
819
+ resolve();
820
+ };
821
+ const timer = setTimeout(finish, deadlineMs);
822
+ slot.idleWaiters.add(finish);
823
+ });
824
+ }
825
+ function releaseSlotLocks(slot) {
826
+ noteThreadSlotGone(slot.scratchSlot);
827
+ for (const field of slot.fields.values()) {
828
+ releasePinsHeldBy(field.store.pins, slot.scratchSlot);
829
+ if (field.graph !== null) releaseLocksHeldBy(field.graph, slot.scratchSlot);
830
+ }
831
+ slot.fields.clear();
832
+ slot.working.clear();
833
+ for (const waiter of [...slot.idleWaiters]) waiter();
834
+ }
835
+ function rejectPending(slot, err) {
836
+ for (const [, waiting] of slot.pending) {
837
+ clearTimeout(waiting.timer);
838
+ waiting.reject(err);
839
+ }
840
+ slot.pending.clear();
841
+ }
842
+
843
+ // src/vector/search-pool/spawn.ts
844
+ async function spawnWorker(entryPoint) {
845
+ if (entryPoint === null) return null;
846
+ const runtime = detectRuntime();
847
+ if (runtime.supportsWorkerThreads) {
848
+ try {
849
+ return await spawnNodeWorker(new URL(entryPoint), workerResourceLimits());
850
+ } catch {
851
+ return null;
852
+ }
853
+ }
854
+ if (runtime.supportsWebWorkers) {
855
+ try {
856
+ const WorkerCtor = globalThis.Worker;
857
+ if (typeof WorkerCtor !== "function") return null;
858
+ return new WorkerCtor(entryPoint, { type: "module" });
859
+ } catch {
860
+ return null;
861
+ }
862
+ }
863
+ return null;
864
+ }
865
+ function listen(worker, handler) {
866
+ if (typeof worker.on === "function") {
867
+ worker.on("message", handler);
868
+ return;
869
+ }
870
+ worker.addEventListener?.("message", (event) => {
871
+ handler(event.data);
872
+ });
873
+ }
874
+ function listenForFailure(worker, handler) {
875
+ if (typeof worker.on === "function") {
876
+ worker.on("error", (err) => handler(err instanceof Error ? err : new Error(String(err))));
877
+ worker.on("exit", () => handler(new Error("Vector search worker exited")));
878
+ return;
879
+ }
880
+ worker.addEventListener?.("error", () => handler(new Error("Vector search worker failed")));
881
+ }
882
+
883
+ // src/vector/search-pool/index.ts
884
+ function searchPoolScratchSlot(index) {
885
+ return VECTOR_SCRATCH_SLOTS - 1 - index;
886
+ }
887
+ function graphSearchOptionsOf(options) {
888
+ return {
889
+ ...options.filter !== void 0 ? { filter: options.filter } : {},
890
+ ...options.efSearch !== void 0 ? { efSearch: options.efSearch } : {},
891
+ ...options.oversample !== void 0 ? { oversample: options.oversample } : {},
892
+ ...options.rescoreWindow !== void 0 ? { rescoreWindow: options.rescoreWindow } : {}
893
+ };
894
+ }
895
+ async function createVectorSearchPool(requestedCount) {
896
+ const entryPoint = resolveWorkerEntryPoint();
897
+ const count = resolveWorkerCount(requestedCount);
898
+ const slots = [];
899
+ const sharedFields = /* @__PURE__ */ new Map();
900
+ for (let i = 0; i < count; i++) {
901
+ const worker = await spawnWorker(entryPoint);
902
+ if (worker === null) break;
903
+ const slot = createSlot(worker, searchPoolScratchSlot(i));
904
+ listen(worker, (raw) => {
905
+ const message = raw;
906
+ const key = message.requestId;
907
+ if (typeof key !== "string") return;
908
+ noteAnswered(slot, key, sharedFields);
909
+ const waiting = slot.pending.get(key);
910
+ if (!waiting) return;
911
+ slot.pending.delete(key);
912
+ clearTimeout(waiting.timer);
913
+ waiting.resolve(message);
914
+ });
915
+ listenForFailure(worker, (err) => {
916
+ slot.alive = false;
917
+ releaseSlotLocks(slot);
918
+ rejectPending(slot, err);
919
+ });
920
+ slots.push(slot);
921
+ }
922
+ if (slots.length === 0) return null;
923
+ let requestCounter = 0;
924
+ function send(slot, key, message, timeoutMs, handle) {
925
+ return new Promise((resolve, reject) => {
926
+ if (!slot.alive) {
927
+ reject(new Error("Vector search worker is not running"));
928
+ return;
929
+ }
930
+ const timer = setTimeout(() => {
931
+ slot.pending.delete(key);
932
+ reject(new Error(`Vector search worker did not answer within ${timeoutMs}ms`));
933
+ }, timeoutMs);
934
+ slot.pending.set(key, { resolve, reject, timer });
935
+ if (handle !== void 0) noteWorkStarted(slot, key, handle);
936
+ try {
937
+ slot.worker.postMessage(message);
938
+ } catch (err) {
939
+ slot.pending.delete(key);
940
+ noteAnswered(slot, key, sharedFields);
941
+ clearTimeout(timer);
942
+ reject(err instanceof Error ? err : new Error(String(err)));
943
+ }
944
+ });
945
+ }
946
+ function refusalOf(message) {
947
+ return message.type === "error" ? message.message : `the worker answered "${message.type}"`;
948
+ }
949
+ function pickSlot() {
950
+ let slot = null;
951
+ for (const candidate of slots) {
952
+ if (!candidate.alive) continue;
953
+ if (slot === null || candidate.outstanding < slot.outstanding) slot = candidate;
954
+ if (slot.outstanding === 0) break;
955
+ }
956
+ return slot;
957
+ }
958
+ async function sendBusy(slot, request, timeoutMs) {
959
+ slot.outstanding += 1;
960
+ try {
961
+ return await send(slot, request.requestId, request, timeoutMs, request.handle);
962
+ } finally {
963
+ slot.outstanding -= 1;
964
+ }
965
+ }
966
+ async function broadcast(build, timeoutMs) {
967
+ const outcomes = await Promise.allSettled(
968
+ slots.map((slot) => {
969
+ requestCounter += 1;
970
+ const requestId = `${requestCounter}`;
971
+ return send(slot, requestId, build(slot, requestId), timeoutMs);
972
+ })
973
+ );
974
+ return outcomes.every(
975
+ (outcome) => outcome.status === "fulfilled" && outcome.value.type === "ack"
976
+ );
977
+ }
978
+ return {
979
+ get workerCount() {
980
+ return slots.filter((slot) => slot.alive).length;
981
+ },
982
+ load(handle, snapshot) {
983
+ return broadcast(
984
+ (_slot, requestId) => ({ type: "load", requestId, handle, snapshot }),
985
+ VECTOR_SEARCH_LOAD_TIMEOUT_MS
986
+ );
987
+ },
988
+ async loadShared(handle, handles) {
989
+ sharedFields.set(handle, handles);
990
+ const loaded = await broadcast((slot, requestId) => {
991
+ noteFieldOpened(slot, handle, handles);
992
+ return { type: "loadShared", requestId, handle, scratchSlot: slot.scratchSlot, handles };
993
+ }, VECTOR_SEARCH_LOAD_TIMEOUT_MS);
994
+ if (loaded) return true;
995
+ sharedFields.delete(handle);
996
+ for (const slot of slots) forgetDroppedGraphs(slot, sharedFields);
997
+ return false;
998
+ },
999
+ async drop(handle) {
1000
+ sharedFields.delete(handle);
1001
+ await broadcast((_slot, requestId) => ({ type: "drop", requestId, handle }), VECTOR_SEARCH_TIMEOUT_MS);
1002
+ for (const slot of slots) forgetDroppedGraphs(slot, sharedFields);
1003
+ },
1004
+ async insertOrdinals(handle, ordinals) {
1005
+ const slot = pickSlot();
1006
+ if (slot === null) return null;
1007
+ requestCounter += 1;
1008
+ const request = { type: "insertOrdinals", requestId: `${requestCounter}`, handle, ordinals };
1009
+ const message = await sendBusy(slot, request, VECTOR_INSERT_TIMEOUT_MS);
1010
+ if (message.type !== "inserted") {
1011
+ console.warn("A vector worker placed no vectors in the shared graph:", refusalOf(message));
1012
+ return null;
1013
+ }
1014
+ return message.outcome;
1015
+ },
1016
+ async search(handle, query, k, metric, minSimilarity, options = {}) {
1017
+ const slot = pickSlot();
1018
+ if (slot === null) throw new Error("No vector search worker is running");
1019
+ requestCounter += 1;
1020
+ const request = {
1021
+ type: "search",
1022
+ requestId: `${requestCounter}`,
1023
+ handle,
1024
+ query,
1025
+ k,
1026
+ metric,
1027
+ minSimilarity,
1028
+ ...graphSearchOptionsOf(options)
1029
+ };
1030
+ const message = await sendBusy(slot, request, VECTOR_SEARCH_TIMEOUT_MS);
1031
+ if (message.type === "error") throw new Error(message.message);
1032
+ if (message.type !== "result") throw new Error("Vector search worker returned an unexpected message");
1033
+ const results = new Array(message.docIds.length);
1034
+ for (let i = 0; i < message.docIds.length; i++) {
1035
+ results[i] = { docId: message.docIds[i], score: message.scores[i] };
1036
+ }
1037
+ return results;
1038
+ },
1039
+ async searchOrdinals(handle, query, k, metric, minSimilarity, options = {}) {
1040
+ const slot = pickSlot();
1041
+ if (slot === null) throw new Error("No vector search worker is running");
1042
+ requestCounter += 1;
1043
+ const request = {
1044
+ type: "searchOrdinals",
1045
+ requestId: `${requestCounter}`,
1046
+ handle,
1047
+ query,
1048
+ k,
1049
+ metric,
1050
+ minSimilarity,
1051
+ ...graphSearchOptionsOf(options)
1052
+ };
1053
+ const message = await sendBusy(slot, request, VECTOR_SEARCH_TIMEOUT_MS);
1054
+ if (message.type === "error") throw new Error(message.message);
1055
+ if (message.type !== "ordinalResult") throw new Error("Vector search worker returned an unexpected message");
1056
+ return { ordinals: message.ordinals, scores: message.scores };
1057
+ },
1058
+ async shutdown() {
1059
+ for (const slot of slots) slot.alive = false;
1060
+ await Promise.all(slots.map((slot) => untilIdle(slot, VECTOR_POOL_DRAIN_TIMEOUT_MS)));
1061
+ for (const slot of slots) {
1062
+ try {
1063
+ await slot.worker.terminate();
1064
+ } catch {
1065
+ slot.alive = false;
1066
+ }
1067
+ releaseSlotLocks(slot);
1068
+ rejectPending(slot, new Error("Vector search pool has shut down"));
1069
+ }
1070
+ }
1071
+ };
1072
+ }
1073
+ var sharedPool = null;
1074
+ var poolHolders = 0;
1075
+ var workersUnavailable = false;
1076
+ var closingPool = Promise.resolve();
1077
+ function acquireVectorSearchPool(requestedCount) {
1078
+ poolHolders += 1;
1079
+ if (workersUnavailable) return Promise.resolve(null);
1080
+ if (sharedPool === null) {
1081
+ sharedPool = closingPool.then(() => createVectorSearchPool(requestedCount)).then(
1082
+ (pool) => {
1083
+ if (pool === null) workersUnavailable = true;
1084
+ return pool;
1085
+ },
1086
+ () => {
1087
+ workersUnavailable = true;
1088
+ return null;
1089
+ }
1090
+ );
1091
+ }
1092
+ return sharedPool;
1093
+ }
1094
+ async function releaseVectorSearchPool() {
1095
+ if (poolHolders === 0) return;
1096
+ poolHolders -= 1;
1097
+ if (poolHolders > 0 || sharedPool === null) return;
1098
+ const pending = sharedPool;
1099
+ sharedPool = null;
1100
+ const closing = closingPool.then(async () => {
1101
+ const pool = await pending;
1102
+ await pool?.shutdown();
1103
+ });
1104
+ closingPool = closing.catch(() => void 0);
1105
+ await closing;
1106
+ }
1107
+
1108
+ // src/vector/vector-index/worker-copies.ts
1109
+ var handleCounter = 0;
1110
+ function nextHandle(state) {
1111
+ handleCounter += 1;
1112
+ return `${state.indexName}/${state.fieldName}#${handleCounter}`;
1113
+ }
1114
+ async function dropHandle(state, handle) {
1115
+ const pool = state.workerCopyPool;
1116
+ if (pool !== null) {
1117
+ await pool.drop(handle).catch(() => void 0);
1118
+ return;
1119
+ }
1120
+ const host = state.workerCopies.host;
1121
+ if (host !== void 0) await host.drop(state.indexName, state.fieldName, handle).catch(() => void 0);
1122
+ }
1123
+ function letGoOfUnusedPool(state) {
1124
+ if (state.workerCopyPool === null || state.sharedHandles.size > 0 || state.workerCopyHandle !== null) {
1125
+ return Promise.resolve();
1126
+ }
1127
+ state.workerCopyPool = null;
1128
+ return releaseVectorSearchPool().catch(() => void 0);
1129
+ }
1130
+ function dropSharedGraph(state, graph) {
1131
+ const shared = state.sharedHandles.get(graph);
1132
+ if (shared === void 0) return;
1133
+ state.sharedHandles.delete(graph);
1134
+ if (state.workerCopyHandle === shared.handle) {
1135
+ state.workerCopyHandle = null;
1136
+ state.workerCopyRevision = -1;
1137
+ state.workerCopyMode = null;
1138
+ }
1139
+ void dropHandle(state, shared.handle);
1140
+ }
1141
+ function invalidateWorkerCopies(state) {
1142
+ state.revision += 1;
1143
+ for (const graph of [...state.sharedHandles.keys()]) dropSharedGraph(state, graph);
1144
+ if (state.workerCopyHandle !== null) {
1145
+ const handle = state.workerCopyHandle;
1146
+ state.workerCopyHandle = null;
1147
+ state.workerCopyRevision = -1;
1148
+ state.workerCopyMode = null;
1149
+ void dropHandle(state, handle);
1150
+ }
1151
+ void letGoOfUnusedPool(state);
1152
+ }
1153
+ async function withdrawWorkerCopies(state) {
1154
+ state.revision += 1;
1155
+ const handles = /* @__PURE__ */ new Set();
1156
+ for (const shared of state.sharedHandles.values()) handles.add(shared.handle);
1157
+ if (state.workerCopyHandle !== null) handles.add(state.workerCopyHandle);
1158
+ state.sharedHandles.clear();
1159
+ state.workerCopyHandle = null;
1160
+ state.workerCopyRevision = -1;
1161
+ state.workerCopyMode = null;
1162
+ await Promise.all([...handles].map((handle) => dropHandle(state, handle)));
1163
+ await letGoOfUnusedPool(state);
1164
+ }
1165
+ async function holdWorkerCopies(state) {
1166
+ state.workerCopiesHeld = true;
1167
+ await state.sharing;
1168
+ await withdrawWorkerCopies(state);
1169
+ }
1170
+ function noteWrite(state) {
1171
+ state.revision += 1;
1172
+ if (state.workerCopyMode === "clone") invalidateWorkerCopies(state);
1173
+ }
1174
+ function refreshWorkerCopies(state) {
1175
+ invalidateWorkerCopies(state);
1176
+ scheduleWorkerCopyLoad(state);
1177
+ }
1178
+ function recordSharedField(state, record) {
1179
+ const { graph, handle, searchable, mode, revision, layoutRevision } = record;
1180
+ state.sharedHandles.set(graph, { handle, searchable, mode });
1181
+ state.sharedLayoutRevision = layoutRevision;
1182
+ if (!searchable) return;
1183
+ state.workerCopyHandle = handle;
1184
+ state.workerCopyRevision = revision;
1185
+ state.workerCopyMode = mode;
1186
+ }
1187
+ async function loadOnHost(state, host, graph, searchable) {
1188
+ if (!host.holdsIndex(state.indexName)) return null;
1189
+ if (!state.store.partitionsKnown) {
1190
+ assignStorePartitions(state, (docId) => host.resolvePartition(state.indexName, docId));
1191
+ }
1192
+ const handle = state.sharedHandles.get(graph)?.handle ?? nextHandle(state);
1193
+ const layoutRevision = state.store.handles.layoutRevision;
1194
+ let loaded = false;
1195
+ try {
1196
+ loaded = await host.loadShared(state.indexName, state.fieldName, handle, fieldHandlesOf(state, graph, searchable));
1197
+ } catch {
1198
+ loaded = false;
1199
+ }
1200
+ if (!loaded || state.disposed) return null;
1201
+ recordSharedField(state, { graph, handle, searchable, mode: "hosted", revision: state.revision, layoutRevision });
1202
+ return handle;
1203
+ }
1204
+ function captureCloneCodes(state) {
1205
+ const quantizer = state.osq;
1206
+ const centroid = quantizer?.centroid ?? null;
1207
+ const layout = state.store.handles.codeLayout;
1208
+ if (quantizer === null || centroid === null || layout === null) return null;
1209
+ const slots = state.store.slots;
1210
+ const records = new Uint8Array(slots * layout.slotStride);
1211
+ const present = new Uint8Array(slots);
1212
+ for (let ordinal = 0; ordinal < slots; ordinal++) {
1213
+ const record = quantizer.recordAtOrdinal(ordinal);
1214
+ if (record === void 0) continue;
1215
+ records.set(record, ordinal * layout.slotStride);
1216
+ present[ordinal] = 1;
1217
+ }
1218
+ return { centroid: Float32Array.from(centroid), records, present };
1219
+ }
1220
+ function captureCloneSnapshot(state, graph) {
1221
+ return {
1222
+ dimension: state.dimension,
1223
+ quantization: state.quantizationMode,
1224
+ metric: state.metric,
1225
+ codes: captureCloneCodes(state),
1226
+ store: state.store.exportSnapshot(),
1227
+ graph: graph.exportSnapshot(),
1228
+ tombstones: Array.from(state.tombstones)
1229
+ };
1230
+ }
1231
+ async function loadOnPool(state, graph, searchable) {
1232
+ if (graph === null) return null;
1233
+ const revision = state.revision;
1234
+ const pool = state.workerCopyPool ?? await acquireVectorSearchPool(state.workerCopies.count);
1235
+ if (pool === null) {
1236
+ await releaseVectorSearchPool();
1237
+ return null;
1238
+ }
1239
+ if (state.disposed) {
1240
+ await releaseVectorSearchPool();
1241
+ return null;
1242
+ }
1243
+ state.workerCopyPool = pool;
1244
+ const handle = state.sharedHandles.get(graph)?.handle ?? nextHandle(state);
1245
+ const layoutRevision = state.store.handles.layoutRevision;
1246
+ let mode = "shared";
1247
+ let loaded = false;
1248
+ try {
1249
+ if (sharedMemoryAvailable()) {
1250
+ loaded = await pool.loadShared(handle, fieldHandlesOf(state, graph, searchable));
1251
+ }
1252
+ if (!loaded && searchable) {
1253
+ mode = "clone";
1254
+ loaded = await pool.load(handle, captureCloneSnapshot(state, graph));
1255
+ }
1256
+ } catch {
1257
+ loaded = false;
1258
+ }
1259
+ if (!loaded || state.disposed || mode === "clone" && state.revision !== revision) {
1260
+ await pool.drop(handle).catch(() => void 0);
1261
+ return null;
1262
+ }
1263
+ recordSharedField(state, { graph, handle, searchable, mode, revision, layoutRevision });
1264
+ return handle;
1265
+ }
1266
+ function shareGraph(state, graph, searchable) {
1267
+ const run = state.sharing.then(async () => {
1268
+ if (!state.workerCopies.enabled || state.disposed || state.workerCopiesHeld) return null;
1269
+ const existing = state.sharedHandles.get(graph);
1270
+ const layoutUnchanged = threadsHoldCurrentLayout(state);
1271
+ if (existing !== void 0 && layoutUnchanged && (existing.searchable || !searchable)) return existing.handle;
1272
+ const host = state.workerCopies.host;
1273
+ return host !== void 0 ? loadOnHost(state, host, graph, searchable) : loadOnPool(state, graph, searchable);
1274
+ });
1275
+ state.sharing = run.then(
1276
+ () => void 0,
1277
+ () => void 0
1278
+ );
1279
+ return run;
1280
+ }
1281
+ function resendSharedHandles(state) {
1282
+ const run = state.sharing.then(async () => {
1283
+ if (!state.workerCopies.enabled || state.disposed || state.workerCopiesHeld) return;
1284
+ const host = state.workerCopies.host;
1285
+ for (const [graph, shared] of [...state.sharedHandles]) {
1286
+ if (host !== void 0) await loadOnHost(state, host, graph, shared.searchable);
1287
+ else await loadOnPool(state, graph, shared.searchable);
1288
+ }
1289
+ });
1290
+ state.sharing = run.then(
1291
+ () => void 0,
1292
+ () => void 0
1293
+ );
1294
+ return run;
1295
+ }
1296
+ function scheduleWorkerCopyLoad(state) {
1297
+ if (!state.workerCopies.enabled || state.workerCopiesHeld) return;
1298
+ if (state.disposed || state.workerCopyLoading || state.building) return;
1299
+ const graph = state.hnsw;
1300
+ const host = state.workerCopies.host;
1301
+ if (graph === null && host === void 0) return;
1302
+ const existing = state.sharedHandles.get(graph);
1303
+ if (existing?.searchable && threadsHoldCurrentLayout(state)) return;
1304
+ if (host === void 0 && liveSize(state) < WORKER_COPY_MIN_VECTORS) return;
1305
+ state.workerCopyLoading = true;
1306
+ void shareGraph(state, graph, true).then(
1307
+ () => {
1308
+ state.workerCopyLoading = false;
1309
+ },
1310
+ () => {
1311
+ state.workerCopyLoading = false;
1312
+ }
1313
+ );
1314
+ }
1315
+ async function searchViaWorkerCopies(state, query, k, metric, minSimilarity, options) {
1316
+ const pool = state.workerCopyPool;
1317
+ const handle = state.workerCopyHandle;
1318
+ if (pool === null || handle === null || state.workerCopyMode === "hosted") return null;
1319
+ if (state.workerCopyMode === "clone" && state.workerCopyRevision !== state.revision) return null;
1320
+ try {
1321
+ if (state.workerCopyMode === "shared") {
1322
+ const outcome = await pool.searchOrdinals(handle, query, k, metric, minSimilarity, options);
1323
+ const results = [];
1324
+ for (let i = 0; i < outcome.ordinals.length; i++) {
1325
+ const docId = state.store.docIdForOrdinal(outcome.ordinals[i]);
1326
+ if (docId === void 0) continue;
1327
+ results.push({ docId, score: outcome.scores[i] });
1328
+ }
1329
+ return results;
1330
+ }
1331
+ return await pool.search(handle, query, k, metric, minSimilarity, options);
1332
+ } catch {
1333
+ return null;
1334
+ }
1335
+ }
1336
+
1337
+ // src/vector/vector-index/build-host.ts
1338
+ async function dispatcherFor(state, graph) {
1339
+ if (!state.workerCopies.enabled || state.disposed) return null;
1340
+ const host = state.workerCopies.host;
1341
+ if (host !== void 0) {
1342
+ if (!host.holdsIndex(state.indexName)) return null;
1343
+ const handle2 = await shareGraph(state, graph, graph === state.hnsw);
1344
+ if (handle2 === null) return null;
1345
+ return {
1346
+ send: (ordinals) => host.insertOrdinals(state.indexName, state.fieldName, handle2, ordinals),
1347
+ workerCount: Math.max(1, host.workerCount)
1348
+ };
1349
+ }
1350
+ if (liveSize(state) < WORKER_COPY_MIN_VECTORS || !sharedMemoryAvailable()) return null;
1351
+ const handle = await shareGraph(state, graph, graph === state.hnsw);
1352
+ const pool = state.workerCopyPool;
1353
+ if (handle === null || pool === null || state.sharedHandles.get(graph)?.mode !== "shared") return null;
1354
+ return {
1355
+ send: (ordinals) => pool.insertOrdinals(handle, ordinals),
1356
+ workerCount: Math.max(1, pool.workerCount)
1357
+ };
1358
+ }
1359
+ async function insertIntoGraph(state, graph, docIds, admit) {
1360
+ const found = await dispatcherFor(state, graph);
1361
+ if (found === null) return insertLocally(state, graph, docIds, admit);
1362
+ const dispatch = found;
1363
+ const pending = /* @__PURE__ */ new Set();
1364
+ const limit = dispatch.workerCount * GRAPH_BUILD_CHUNKS_IN_FLIGHT_PER_WORKER;
1365
+ const fallback = { noWorkerWarned: false, shortAnswerWarned: false, workerAnswerFailed: false };
1366
+ let batchDocIds = [];
1367
+ let batchOrdinals = [];
1368
+ async function flush() {
1369
+ if (batchOrdinals.length === 0) return;
1370
+ const chunkDocIds = batchDocIds;
1371
+ const chunkOrdinals = Int32Array.from(batchOrdinals);
1372
+ batchDocIds = [];
1373
+ batchOrdinals = [];
1374
+ if (!threadsHoldCurrentLayout(state)) await shareGraph(state, graph, graph === state.hnsw);
1375
+ const run = placeChunk(state, graph, dispatch, chunkDocIds, chunkOrdinals, fallback).finally(() => {
1376
+ pending.delete(run);
1377
+ });
1378
+ pending.add(run);
1379
+ if (pending.size >= limit) await Promise.race(pending);
1380
+ }
1381
+ state.graphsPlacedIntoSinceConnect.add(graph);
1382
+ for (const docId of docIds) {
1383
+ if (state.disposed) break;
1384
+ if (!admit(docId)) continue;
1385
+ const ordinal = state.store.getOrdinal(docId);
1386
+ if (ordinal === void 0) continue;
1387
+ batchDocIds.push(docId);
1388
+ batchOrdinals.push(ordinal);
1389
+ if (batchOrdinals.length >= GRAPH_BUILD_DISPATCH_CHUNK) await flush();
1390
+ }
1391
+ await flush();
1392
+ await Promise.all(pending);
1393
+ if (state.disposed) return false;
1394
+ if (turnsAreBudgeted) await connectGraph(state, graph);
1395
+ else graph.connectUnreached();
1396
+ if (state.disposed) return false;
1397
+ if (!fallback.workerAnswerFailed) state.graphsPlacedIntoSinceConnect.delete(graph);
1398
+ noteWrite(state);
1399
+ return true;
1400
+ }
1401
+ async function placeChunk(state, graph, dispatch, chunkDocIds, chunkOrdinals, fallback) {
1402
+ let outcome = null;
1403
+ try {
1404
+ outcome = await dispatch.send(chunkOrdinals);
1405
+ } catch {
1406
+ outcome = null;
1407
+ fallback.workerAnswerFailed = true;
1408
+ }
1409
+ if (state.disposed) return;
1410
+ if (outcome === null) {
1411
+ if (!fallback.noWorkerWarned) {
1412
+ fallback.noWorkerWarned = true;
1413
+ console.warn(
1414
+ `No worker thread placed the vectors of "${state.indexName}/${state.fieldName}", so this thread is building its graph itself`
1415
+ );
1416
+ }
1417
+ for (const ordinal of chunkOrdinals) graph.insertOrdinal(ordinal);
1418
+ }
1419
+ let placedHere = 0;
1420
+ for (let i = 0; i < chunkDocIds.length; i++) {
1421
+ const docId = chunkDocIds[i];
1422
+ const ordinal = chunkOrdinals[i];
1423
+ if (state.store.getOrdinal(docId) !== ordinal) {
1424
+ graph.markTombstoneOrdinal(ordinal);
1425
+ continue;
1426
+ }
1427
+ if (!state.tombstones.has(docId) && !graph.has(docId) && graph.insertOrdinal(ordinal)) placedHere += 1;
1428
+ if (graph === state.hnsw) moveOutOfBuffer(state, docId);
1429
+ }
1430
+ if (placedHere > 0 && outcome !== null && !fallback.shortAnswerWarned) {
1431
+ fallback.shortAnswerWarned = true;
1432
+ console.warn(
1433
+ `A worker thread answered a chunk of "${state.indexName}/${state.fieldName}" without placing ${placedHere} of its vectors, so this thread placed them itself`
1434
+ );
1435
+ }
1436
+ }
1437
+ async function insertLocally(state, graph, docIds, admit) {
1438
+ let count = 0;
1439
+ for (const docId of docIds) {
1440
+ if (state.disposed) return false;
1441
+ if (!admit(docId)) continue;
1442
+ const ordinal = state.store.getOrdinal(docId);
1443
+ if (ordinal === void 0) continue;
1444
+ state.graphsPlacedIntoSinceConnect.add(graph);
1445
+ if (turnsAreBudgeted) {
1446
+ graph.insertOrdinal(ordinal, true);
1447
+ if (graph.holdsUnlinkedNode) await connectGraph(state, graph);
1448
+ if (state.disposed) return false;
1449
+ } else {
1450
+ graph.insertOrdinal(ordinal);
1451
+ }
1452
+ if (graph === state.hnsw) moveOutOfBuffer(state, docId);
1453
+ count += 1;
1454
+ if (turnsAreBudgeted) {
1455
+ if (!backgroundTurnIsSpent()) continue;
1456
+ noteWrite(state);
1457
+ await yieldBackgroundTurn();
1458
+ } else if (count % BUILD_CHUNK_SIZE === 0) {
1459
+ noteWrite(state);
1460
+ await yieldBuildTurn(state);
1461
+ }
1462
+ }
1463
+ if (state.disposed) return false;
1464
+ if (count > 0) {
1465
+ if (turnsAreBudgeted) await connectGraph(state, graph);
1466
+ else graph.connectUnreached();
1467
+ state.graphsPlacedIntoSinceConnect.delete(graph);
1468
+ noteWrite(state);
1469
+ }
1470
+ return true;
1471
+ }
1472
+
1473
+ // src/vector/vector-index/turns.ts
1474
+ function turnDue(steps) {
1475
+ return steps % BUILD_STEPS_BETWEEN_CLOCK_CHECKS === 0 && backgroundTurnIsSpent();
1476
+ }
1477
+ async function forEachInTurns(items, visit, stillWanted) {
1478
+ let steps = 0;
1479
+ for (const item of items) {
1480
+ steps += 1;
1481
+ if (turnDue(steps)) {
1482
+ await yieldBackgroundTurn();
1483
+ if (!stillWanted()) return false;
1484
+ }
1485
+ visit(item);
1486
+ }
1487
+ return true;
1488
+ }
1489
+ async function copyInTurns(items, stillWanted) {
1490
+ const copied = [];
1491
+ const finished = await forEachInTurns(items, (item) => copied.push(item), stillWanted);
1492
+ return finished ? copied : null;
1493
+ }
1494
+ async function shuffledLiveDocIdsInTurns(state) {
1495
+ const stillWanted = () => !state.disposed;
1496
+ const docIds = await copyInTurns(allLiveDocIds(state), stillWanted);
1497
+ if (docIds === null) return null;
1498
+ let stream = docIds.length;
1499
+ for (let i = docIds.length - 1; i > 0; i--) {
1500
+ stream = shuffleStep(docIds, i, stream);
1501
+ if (turnDue(i)) {
1502
+ await yieldBackgroundTurn();
1503
+ if (!stillWanted()) return null;
1504
+ }
1505
+ }
1506
+ return docIds;
1507
+ }
1508
+ async function liveOrdinalsInTurns(state) {
1509
+ const ordinals = [];
1510
+ for (let ordinal = 0; ordinal < state.store.slots; ordinal++) {
1511
+ if (turnDue(ordinal + 1)) {
1512
+ await yieldBackgroundTurn();
1513
+ if (state.disposed) return null;
1514
+ }
1515
+ const docId = state.store.docIdForOrdinal(ordinal);
1516
+ if (docId !== void 0 && !state.tombstones.has(docId)) ordinals.push(ordinal);
1517
+ }
1518
+ return Int32Array.from(ordinals);
1519
+ }
1520
+
1521
+ // src/vector/vector-index/drift.ts
1522
+ function liveVectorMeanOf(sums) {
1523
+ const centroid = centroidOfCentroidSums(sums);
1524
+ if (centroid === null) return null;
1525
+ const mean = meanOfCentroidSums(sums);
1526
+ const count = countOfCentroidSums(sums);
1527
+ let meanSquares = 0;
1528
+ for (let i = 0; i < mean.length; i++) meanSquares += mean[i] * mean[i];
1529
+ return {
1530
+ centroid,
1531
+ mean,
1532
+ meanLength: Math.sqrt(meanSquares),
1533
+ count,
1534
+ spread: Math.sqrt(Math.max(0, squaredLengthsOfCentroidSums(sums) / count - meanSquares))
1535
+ };
1536
+ }
1537
+ async function measureLiveVectors(state, quantizer) {
1538
+ const sums = createCentroidSums(state.dimension, state.metric);
1539
+ const ordinals = turnsAreBudgeted ? await liveOrdinalsInTurns(state) : liveOrdinals(state);
1540
+ if (ordinals === null) return null;
1541
+ for (let start = 0; start < ordinals.length; start += RECALIBRATION_CHUNK_VECTORS) {
1542
+ if (start > 0) await yieldBuildTurn(state);
1543
+ if (state.disposed || state.osq !== quantizer) return null;
1544
+ quantizer.measure(ordinals.subarray(start, start + RECALIBRATION_CHUNK_VECTORS), sums);
1545
+ }
1546
+ return liveVectorMeanOf(sums);
1547
+ }
1548
+ function driftRatio(live, calibrated, calibratedCount, calibratedMeanLength, metric) {
1549
+ if (calibratedCount <= 0 || live.count <= calibratedCount) return 0;
1550
+ const scale = metric === "cosine" ? calibratedMeanLength ?? live.meanLength : 1;
1551
+ let squares = 0;
1552
+ for (let i = 0; i < live.mean.length; i++) {
1553
+ const difference = live.mean[i] - calibrated[i] * scale;
1554
+ squares += difference * difference;
1555
+ }
1556
+ const expected = live.spread * Math.sqrt(1 / calibratedCount - 1 / live.count);
1557
+ if (expected === 0) return squares === 0 ? 0 : Number.POSITIVE_INFINITY;
1558
+ return Math.sqrt(squares) / expected;
1559
+ }
1560
+ function nextDriftCheck(calibratedCount, held) {
1561
+ let next = Math.max(1, calibratedCount) * DRIFT_CHECK_GROWTH_FACTOR;
1562
+ while (next <= held) next *= DRIFT_CHECK_GROWTH_FACTOR;
1563
+ return next;
1564
+ }
1565
+
1566
+ // src/vector/vector-index/calibration.ts
1567
+ function noteCalibration(state, live) {
1568
+ state.calibratedCount = live.count;
1569
+ state.calibratedMeanLength = state.metric === "cosine" ? live.meanLength : null;
1570
+ state.nextDriftCheckAt = nextDriftCheck(live.count, live.count);
1571
+ }
1572
+ async function recodeEveryVector(state, live) {
1573
+ const quantizer = state.osq;
1574
+ if (quantizer === null) return false;
1575
+ quantizer.beginRecoding(live.centroid);
1576
+ try {
1577
+ for (let ordinal = 0; ordinal < state.store.slots; ordinal++) {
1578
+ if (state.disposed || state.osq !== quantizer) {
1579
+ quantizer.abandonRecoding();
1580
+ return false;
1581
+ }
1582
+ quantizer.recode(ordinal);
1583
+ if ((ordinal + 1) % RECALIBRATION_CHUNK_VECTORS === 0) await yieldBuildTurn(state);
1584
+ }
1585
+ const graph = state.hnsw;
1586
+ return graph === null ? quantizer.finishRecoding() : graph.exclusively(() => quantizer.finishRecoding());
1587
+ } catch (error) {
1588
+ quantizer.abandonRecoding();
1589
+ throw error;
1590
+ }
1591
+ }
1592
+ async function calibrateFromLiveVectors(state, live) {
1593
+ const quantizer = state.osq;
1594
+ if (quantizer === null || state.disposed) return false;
1595
+ if (quantizer.isCalibrated()) {
1596
+ if (!await recodeEveryVector(state, live)) return false;
1597
+ noteWrite(state);
1598
+ } else {
1599
+ quantizer.restoreCentroid(live.centroid);
1600
+ }
1601
+ noteCalibration(state, live);
1602
+ return true;
1603
+ }
1604
+ async function calibrateBeforeBuild(state) {
1605
+ if (state.osq === null) return true;
1606
+ const live = await measureLiveVectors(state, state.osq);
1607
+ return live !== null && await calibrateFromLiveVectors(state, live);
1608
+ }
1609
+ async function recalibrateWhenDrifted(state, held) {
1610
+ if (state.osq === null || held < state.nextDriftCheckAt) return false;
1611
+ const live = await measureLiveVectors(state, state.osq);
1612
+ const calibrated = state.osq?.centroid ?? null;
1613
+ const calibratedCount = state.calibratedCount;
1614
+ if (live === null) return false;
1615
+ if (calibrated !== null && calibratedCount !== null) {
1616
+ const ratio = driftRatio(live, calibrated, calibratedCount, state.calibratedMeanLength, state.metric);
1617
+ if (ratio <= RECALIBRATION_DRIFT_RATIO) {
1618
+ state.nextDriftCheckAt = nextDriftCheck(calibratedCount, live.count);
1619
+ return false;
1620
+ }
1621
+ }
1622
+ return calibrateFromLiveVectors(state, live);
1623
+ }
1624
+
1625
+ // src/vector/vector-index/disk.ts
1626
+ function readsFromDisk(state) {
1627
+ return state.storage === "disk";
1628
+ }
1629
+ function holdsVectorsOnDisk(state) {
1630
+ return readsFromDisk(state) && state.hnsw !== null;
1631
+ }
1632
+ function* locationsOf(state, layout) {
1633
+ const stride = state.dimension * 4;
1634
+ for (let i = 0; i < layout.docIds.length; i++) {
1635
+ if (isDead(layout.dead, i)) continue;
1636
+ yield [layout.docIds[i], { path: layout.path, offset: layout.vectorsOffset + i * stride }];
1637
+ }
1638
+ }
1639
+ async function shareFilesWithEveryThread(state, paths) {
1640
+ const fileIndexes = /* @__PURE__ */ new Map();
1641
+ for (const path of paths) fileIndexes.set(path, state.store.addVectorFile(path));
1642
+ await resendSharedHandles(state);
1643
+ return fileIndexes;
1644
+ }
1645
+ async function releaseLocations(state, paths, locations) {
1646
+ const fileIndexes = await shareFilesWithEveryThread(state, paths);
1647
+ await untilGraphReleased(state);
1648
+ if (state.disposed) return;
1649
+ let count = 0;
1650
+ for (const [docId, location] of locations) {
1651
+ if (count > 0 && count % DISK_ADOPTION_YIELD_INTERVAL === 0) {
1652
+ await yieldBuildTurn(state);
1653
+ await untilGraphReleased(state);
1654
+ if (state.disposed) return;
1655
+ }
1656
+ count += 1;
1657
+ const ordinal = state.store.getOrdinal(docId);
1658
+ const fileIndex = fileIndexes.get(location.path);
1659
+ if (ordinal === void 0 || fileIndex === void 0) continue;
1660
+ state.store.releaseToFile(ordinal, { fileIndex, offset: location.offset });
1661
+ }
1662
+ state.store.releaseColdBlocks();
1663
+ await resendSharedHandles(state);
1664
+ }
1665
+ function releaseInTurn(state, release) {
1666
+ const run = state.releaseToFilesInFlight.then(release);
1667
+ state.releaseToFilesInFlight = run.then(
1668
+ () => void 0,
1669
+ () => void 0
1670
+ );
1671
+ return run;
1672
+ }
1673
+ async function adoptDiskLayout(state, layout) {
1674
+ if (!readsFromDisk(state) || state.disposed || layout.docIds.length === 0) return;
1675
+ if (!holdsVectorsOnDisk(state)) {
1676
+ for (const [docId, location] of locationsOf(state, layout)) {
1677
+ if (state.store.has(docId)) state.pendingLocations.set(docId, location);
1678
+ }
1679
+ return;
1680
+ }
1681
+ await releaseInTurn(state, () => releaseLocations(state, [layout.path], locationsOf(state, layout)));
1682
+ }
1683
+ async function releasePendingLocations(state) {
1684
+ if (state.pendingLocations.size === 0 || !holdsVectorsOnDisk(state) || state.disposed) return;
1685
+ const pending = [...state.pendingLocations];
1686
+ state.pendingLocations.clear();
1687
+ const paths = new Set(pending.map(([, location]) => location.path));
1688
+ await releaseInTurn(state, () => releaseLocations(state, paths, pending));
1689
+ }
1690
+
1691
+ // src/vector/vector-index/memory-charge.ts
1692
+ function waitForMemory(state) {
1693
+ state.waitingForMemory = true;
1694
+ noteWaitingForMemory(state, { indexName: state.indexName, fieldName: state.fieldName });
1695
+ }
1696
+ function admitGrowingCopy(state, graph) {
1697
+ if (admitsBackgroundBytes(graph.graphBytes)) return true;
1698
+ waitForMemory(state);
1699
+ return false;
1700
+ }
1701
+ function admitShuffledRebuild(state) {
1702
+ const bytes = state.hnsw?.graphBytes ?? 0;
1703
+ if (!admitsBackgroundBytes(bytes)) {
1704
+ waitForMemory(state);
1705
+ return false;
1706
+ }
1707
+ stopWaitingForMemory(state);
1708
+ chargeHeapBytes(bytes - state.rebuildChargeBytes);
1709
+ state.rebuildChargeBytes = bytes;
1710
+ return true;
1711
+ }
1712
+ function stopWaitingForMemory(state) {
1713
+ if (!state.waitingForMemory) return;
1714
+ state.waitingForMemory = false;
1715
+ noteWaitingForMemory(state, null);
1716
+ }
1717
+ function releaseRebuildCharge(state) {
1718
+ chargeHeapBytes(-state.rebuildChargeBytes);
1719
+ state.rebuildChargeBytes = 0;
1720
+ }
1721
+ function releaseMemoryCharges(state) {
1722
+ releaseRebuildCharge(state);
1723
+ stopWaitingForMemory(state);
1724
+ if (state.rebuildWaitTimer !== null) clearTimeout(state.rebuildWaitTimer);
1725
+ state.rebuildWaitTimer = null;
1726
+ }
1727
+
1728
+ // src/vector/vector-index/growing-copy.ts
1729
+ var lineages = /* @__PURE__ */ new WeakMap();
1730
+ function lineageOfGraph(graph) {
1731
+ const known = lineages.get(graph);
1732
+ if (known !== void 0) return known;
1733
+ const lineage = {};
1734
+ lineages.set(graph, lineage);
1735
+ return lineage;
1736
+ }
1737
+ function lineageOf(graph) {
1738
+ return graph === null ? null : lineageOfGraph(graph);
1739
+ }
1740
+ function moveWhatTheCopyHoldsOutOfBuffer(state, copy) {
1741
+ for (const docId of [...state.buffer]) {
1742
+ const ordinal = state.store.getOrdinal(docId);
1743
+ if (ordinal !== void 0 && copy.holdsNode(ordinal) && !copy.holdsTombstone(ordinal)) moveOutOfBuffer(state, docId);
1744
+ }
1745
+ }
1746
+ async function publishCopy(state, graph, copy) {
1747
+ if (state.sharedHandles.get(graph)?.searchable === true) await shareGraph(state, copy, true);
1748
+ if (state.disposed || state.hnsw !== graph) return false;
1749
+ lineages.set(copy, lineageOfGraph(graph));
1750
+ state.hnsw = copy;
1751
+ graph.retire();
1752
+ moveWhatTheCopyHoldsOutOfBuffer(state, copy);
1753
+ dropSharedGraph(state, graph);
1754
+ noteWrite(state);
1755
+ return true;
1756
+ }
1757
+ function* startingWith(first, rest) {
1758
+ yield first;
1759
+ for (let next = rest.next(); !next.done; next = rest.next()) yield next.value;
1760
+ }
1761
+ async function placeInGrowingCopy(state, graph, docIds, admit) {
1762
+ const pending = docIds[Symbol.iterator]();
1763
+ let first = pending.next();
1764
+ while (!first.done && !admit(first.value)) first = pending.next();
1765
+ if (first.done) return "nothingToPlace";
1766
+ if (!admitGrowingCopy(state, graph)) return "waitingForMemory";
1767
+ const copy = graph.copy();
1768
+ const chargedBytes = copy.graphBytes;
1769
+ chargeHeapBytes(chargedBytes);
1770
+ state.growingCopy = copy;
1771
+ let published = false;
1772
+ try {
1773
+ const placed = await insertIntoGraph(state, copy, startingWith(first.value, pending), admit);
1774
+ published = placed && !state.disposed && state.hnsw === graph && await publishCopy(state, graph, copy);
1775
+ return published ? "published" : "dropped";
1776
+ } finally {
1777
+ if (state.growingCopy === copy) state.growingCopy = null;
1778
+ chargeHeapBytes(-chargedBytes);
1779
+ if (!published) {
1780
+ copy.retire();
1781
+ dropSharedGraph(state, copy);
1782
+ }
1783
+ }
1784
+ }
1785
+
1786
+ // src/vector/vector-index/build.ts
1787
+ function bufferIfTheGraphLacks(state, graph, docId) {
1788
+ const ordinal = state.store.getOrdinal(docId);
1789
+ if (ordinal !== void 0 && graph.holdsNode(ordinal) && !graph.holdsTombstone(ordinal)) moveOutOfBuffer(state, docId);
1790
+ else state.buffer.add(docId);
1791
+ }
1792
+ function bufferWhatTheGraphLacks(state, graph) {
1793
+ for (const docId of allLiveDocIds(state)) bufferIfTheGraphLacks(state, graph, docId);
1794
+ }
1795
+ async function bufferWhatTheGraphLacksInTurns(state, graph) {
1796
+ const visit = (docId) => bufferIfTheGraphLacks(state, graph, docId);
1797
+ await forEachInTurns(allLiveDocIds(state), visit, () => !state.disposed && state.hnsw === graph);
1798
+ }
1799
+ async function buildGraphFromStore(state) {
1800
+ const lineage = lineageOf(state.hnsw);
1801
+ const order = turnsAreBudgeted ? await shuffledLiveDocIdsInTurns(state) : shuffleInPlace([...allLiveDocIds(state)]);
1802
+ if (order === null || lineageOf(state.hnsw) !== lineage) return false;
1803
+ const graph = createHNSWIndex(state.dimension, state.store, state.hnswConfig, state.osq ?? void 0);
1804
+ state.freshGraph = graph;
1805
+ let completed = false;
1806
+ try {
1807
+ completed = await insertIntoGraph(state, graph, order, () => true);
1808
+ } finally {
1809
+ state.freshGraph = null;
1810
+ }
1811
+ if (!completed || lineageOf(state.hnsw) !== lineage) {
1812
+ dropSharedGraph(state, graph);
1813
+ return false;
1814
+ }
1815
+ const replaced = state.hnsw;
1816
+ adoptGraph(state, graph);
1817
+ state.shuffledBuildCount = graph.size;
1818
+ noteWrite(state);
1819
+ if (turnsAreBudgeted) await bufferWhatTheGraphLacksInTurns(state, graph);
1820
+ else bufferWhatTheGraphLacks(state, graph);
1821
+ if (replaced !== null) dropSharedGraph(state, replaced);
1822
+ await releasePendingLocations(state);
1823
+ return true;
1824
+ }
1825
+ async function promoteToGraph(state) {
1826
+ if (liveSize(state) === 0) return;
1827
+ if (await calibrateBeforeBuild(state)) await buildGraphFromStore(state);
1828
+ }
1829
+ function shuffledRebuildDue(state, held = state.hnsw?.size ?? 0) {
1830
+ if (state.hnsw === null || held === 0) return false;
1831
+ if (state.shuffledBuildCount === null) return true;
1832
+ return state.osq !== null && held >= state.nextDriftCheckAt;
1833
+ }
1834
+ async function recalibratesForRebuild(state, graph) {
1835
+ if (state.shuffledBuildCount === null) return calibrateBeforeBuild(state);
1836
+ return recalibrateWhenDrifted(state, graph.size);
1837
+ }
1838
+ async function keepBuildsOut(state) {
1839
+ while (state.buildsKeptOut !== null) await state.buildsKeptOut.ended;
1840
+ if (state.disposed) return () => void 0;
1841
+ let end = () => void 0;
1842
+ const ended = new Promise((resolve) => {
1843
+ end = resolve;
1844
+ });
1845
+ const kept = { ended, end };
1846
+ state.buildsKeptOut = kept;
1847
+ return () => {
1848
+ if (state.buildsKeptOut !== kept) return;
1849
+ letBuildsIn(state);
1850
+ scheduleBuild(state);
1851
+ };
1852
+ }
1853
+ function letBuildsIn(state) {
1854
+ const kept = state.buildsKeptOut;
1855
+ state.buildsKeptOut = null;
1856
+ kept?.end();
1857
+ }
1858
+ async function buildExclusively(state, work, waitsForShuffledRebuild = true, keepsBuildsOut = false) {
1859
+ for (; ; ) {
1860
+ if (state.pendingBuild !== null) await state.pendingBuild;
1861
+ else if (waitsForShuffledRebuild && state.shuffledRebuild !== null) await state.shuffledRebuild;
1862
+ else if (!keepsBuildsOut && state.buildsKeptOut !== null) await state.buildsKeptOut.ended;
1863
+ else break;
1864
+ }
1865
+ if (state.disposed) return;
1866
+ state.building = true;
1867
+ const run = work();
1868
+ state.pendingBuild = run;
1869
+ try {
1870
+ await run;
1871
+ } finally {
1872
+ state.building = false;
1873
+ state.pendingBuild = null;
1874
+ if (state.buffer.size > 0) {
1875
+ scheduleBuild(state);
1876
+ }
1877
+ }
1878
+ }
1879
+ async function rebuildInShuffledOrder(state) {
1880
+ await yieldBuildTurn(state);
1881
+ const replacing = {
1882
+ lineage: null,
1883
+ loadedWithoutCount: false
1884
+ };
1885
+ await buildExclusively(
1886
+ state,
1887
+ async () => {
1888
+ const graph = state.hnsw;
1889
+ if (graph === null || !shuffledRebuildDue(state)) return;
1890
+ const loadedWithoutCount = state.shuffledBuildCount === null;
1891
+ if (!await recalibratesForRebuild(state, graph) || state.hnsw !== graph) return;
1892
+ replacing.lineage = lineageOf(graph);
1893
+ replacing.loadedWithoutCount = loadedWithoutCount;
1894
+ },
1895
+ false
1896
+ );
1897
+ if (replacing.lineage === null || lineageOf(state.hnsw) !== replacing.lineage) return;
1898
+ const adopted = await buildGraphFromStore(state);
1899
+ if (adopted && replacing.loadedWithoutCount) state.onLoadedGraphRepaired();
1900
+ }
1901
+ function retryWhenMemoryAllows(state) {
1902
+ if (state.rebuildWaitTimer !== null || state.disposed) return;
1903
+ const timer = setTimeout(() => {
1904
+ state.rebuildWaitTimer = null;
1905
+ if (state.hnsw !== null && state.buffer.size > 0) placeWhatWaitedForMemory(state);
1906
+ else startShuffledRebuild(state);
1907
+ }, SHUFFLED_REBUILD_MEMORY_RETRY_MS);
1908
+ timer.unref?.();
1909
+ state.rebuildWaitTimer = timer;
1910
+ }
1911
+ function placeWhatWaitedForMemory(state) {
1912
+ if (state.building || state.buildsKeptOut !== null) retryWhenMemoryAllows(state);
1913
+ else triggerBuild(state);
1914
+ }
1915
+ function startShuffledRebuild(state) {
1916
+ if (state.shuffledRebuild !== null || state.disposed) return;
1917
+ if (!shuffledRebuildDue(state)) {
1918
+ stopWaitingForMemory(state);
1919
+ return;
1920
+ }
1921
+ if (!admitShuffledRebuild(state)) {
1922
+ retryWhenMemoryAllows(state);
1923
+ return;
1924
+ }
1925
+ state.shuffledRebuild = rebuildInShuffledOrder(state).catch((error) => {
1926
+ console.warn(
1927
+ `Rebuilding the graph of "${state.indexName}/${state.fieldName}" in a shuffled order failed, so the field keeps the graph it holds:`,
1928
+ error
1929
+ );
1930
+ }).finally(() => {
1931
+ state.shuffledRebuild = null;
1932
+ releaseRebuildCharge(state);
1933
+ scheduleBuild(state);
1934
+ scheduleWorkerCopyLoad(state);
1935
+ });
1936
+ }
1937
+ async function growGraph(state, graph, bufferSnapshot) {
1938
+ const admit = (docId) => {
1939
+ if (state.hnsw !== graph) return false;
1940
+ if (state.tombstones.has(docId) || !state.store.has(docId)) {
1941
+ state.buffer.delete(docId);
1942
+ return false;
1943
+ }
1944
+ return true;
1945
+ };
1946
+ if (state.osq && !state.osq.isCalibrated() && !await calibrateBeforeBuild(state)) return;
1947
+ continueAfterGrowth(state, await placeInGrowingCopy(state, graph, bufferSnapshot, admit));
1948
+ }
1949
+ function continueAfterGrowth(state, outcome) {
1950
+ if (outcome === "waitingForMemory") retryWhenMemoryAllows(state);
1951
+ else startShuffledRebuild(state);
1952
+ }
1953
+ function triggerBuild(state) {
1954
+ if (state.building || state.buildsKeptOut !== null) return;
1955
+ state.building = true;
1956
+ const bufferSnapshot = turnsAreBudgeted ? null : new Set(state.buffer);
1957
+ const existingGraph = state.hnsw;
1958
+ let settled = false;
1959
+ let timedOut = false;
1960
+ const buildPromise = (async () => {
1961
+ try {
1962
+ if (state.disposed) return;
1963
+ if (existingGraph !== null) {
1964
+ const snapshot = bufferSnapshot ?? await copyInTurns(state.buffer, () => !state.disposed && state.hnsw === existingGraph);
1965
+ if (snapshot === null) return;
1966
+ await growGraph(state, existingGraph, snapshot);
1967
+ return;
1968
+ }
1969
+ await promoteToGraph(state);
1970
+ } catch (error) {
1971
+ if (!timedOutOnAnotherThread(state, error)) throw error;
1972
+ timedOut = true;
1973
+ } finally {
1974
+ settled = true;
1975
+ state.building = false;
1976
+ state.pendingBuild = null;
1977
+ if (timedOut) {
1978
+ retryAfterBackoff(state);
1979
+ } else {
1980
+ state.buildRetryDelayMs = 0;
1981
+ if (state.buffer.size > 0) scheduleBuild(state);
1982
+ else scheduleWorkerCopyLoad(state);
1983
+ }
1984
+ }
1985
+ })();
1986
+ if (!settled) state.pendingBuild = buildPromise;
1987
+ }
1988
+ function timedOutOnAnotherThread(state, error) {
1989
+ if (!isNarsilError(error) || error.code !== ErrorCodes.WORKER_TIMEOUT) return false;
1990
+ console.warn(
1991
+ `Building the graph of "${state.indexName}/${state.fieldName}" waited too long for another thread, so it tries again shortly:`,
1992
+ error.message
1993
+ );
1994
+ return true;
1995
+ }
1996
+ function armBuildTimer(state, delayMs, run) {
1997
+ if (state.disposed || state.buildTimer !== null) return;
1998
+ const timer = setTimeout(() => {
1999
+ state.buildTimer = null;
2000
+ if (!state.disposed) run();
2001
+ }, delayMs);
2002
+ timer.unref?.();
2003
+ state.buildTimer = timer;
2004
+ }
2005
+ function connectAfterUnfinishedPass(state, graph) {
2006
+ graph.connectUnreached();
2007
+ state.graphsPlacedIntoSinceConnect.delete(graph);
2008
+ noteWrite(state);
2009
+ }
2010
+ function connectWhereAPassStoppedEarly(state) {
2011
+ const graph = state.hnsw;
2012
+ if (graph !== null && state.graphsPlacedIntoSinceConnect.has(graph)) connectAfterUnfinishedPass(state, graph);
2013
+ }
2014
+ function retryAfterBackoff(state) {
2015
+ const delayMs = Math.min(BUILD_RETRY_MAX_DELAY_MS, Math.max(BUILD_RETRY_FIRST_DELAY_MS, state.buildRetryDelayMs * 2));
2016
+ state.buildRetryDelayMs = delayMs;
2017
+ if (state.buildTimer !== null) clearTimeout(state.buildTimer);
2018
+ state.buildTimer = null;
2019
+ armBuildTimer(state, delayMs, () => {
2020
+ const graph = state.hnsw;
2021
+ if (state.building || state.buildsKeptOut !== null) {
2022
+ retryAfterBackoff(state);
2023
+ return;
2024
+ }
2025
+ if (state.buffer.size > 0 || graph === null) {
2026
+ triggerBuild(state);
2027
+ return;
2028
+ }
2029
+ try {
2030
+ connectAfterUnfinishedPass(state, graph);
2031
+ state.buildRetryDelayMs = 0;
2032
+ scheduleWorkerCopyLoad(state);
2033
+ } catch (error) {
2034
+ if (timedOutOnAnotherThread(state, error)) retryAfterBackoff(state);
2035
+ else console.warn(`Connecting the graph of "${state.indexName}/${state.fieldName}" failed:`, error);
2036
+ }
2037
+ });
2038
+ }
2039
+ function buildDue(state) {
2040
+ if (state.hnsw === null) return liveSize(state) >= state.promotionThreshold;
2041
+ if (state.buffer.size === 0) return false;
2042
+ if (state.waitingForMemory && state.rebuildWaitTimer !== null) return false;
2043
+ if (state.workerCopies.enabled && state.workerCopies.host !== void 0) return true;
2044
+ return state.buffer.size >= state.promotionThreshold;
2045
+ }
2046
+ function scheduleBuild(state) {
2047
+ if (state.building || state.buildScheduled || state.disposed || state.buildsKeptOut !== null) return;
2048
+ if (!buildDue(state)) return;
2049
+ state.buildScheduled = true;
2050
+ const turn = turnsAreBudgeted ? yieldBackgroundTurn() : yieldBuildTurn(state);
2051
+ const scheduled = turn.then(() => {
2052
+ state.buildScheduled = false;
2053
+ if (state.scheduledBuild === scheduled) state.scheduledBuild = null;
2054
+ triggerBuild(state);
2055
+ });
2056
+ state.scheduledBuild = scheduled;
2057
+ }
2058
+ async function awaitBuilds(state) {
2059
+ for (; ; ) {
2060
+ const running = state.pendingBuild ?? state.shuffledRebuild ?? state.scheduledBuild;
2061
+ if (running === null) return;
2062
+ await running;
2063
+ }
2064
+ }
2065
+ function releaseHeldMemory(state) {
2066
+ state.store.release();
2067
+ state.hnsw = null;
2068
+ state.freshGraph = null;
2069
+ state.growingCopy = null;
2070
+ state.osq = null;
2071
+ state.tombstones.clear();
2072
+ state.buffer.clear();
2073
+ state.pendingLocations.clear();
2074
+ state.savedFiles = [];
2075
+ state.savedSignature = null;
2076
+ releaseMemoryCharges(state);
2077
+ }
2078
+ function disposeIndex(state) {
2079
+ state.disposed = true;
2080
+ letBuildsIn(state);
2081
+ if (state.rebuildWaitTimer !== null) clearTimeout(state.rebuildWaitTimer);
2082
+ state.rebuildWaitTimer = null;
2083
+ if (state.buildTimer !== null) clearTimeout(state.buildTimer);
2084
+ state.buildTimer = null;
2085
+ invalidateWorkerCopies(state);
2086
+ const release = () => releaseHeldMemory(state);
2087
+ if (state.pendingBuild !== null || state.shuffledRebuild !== null || state.scheduledBuild !== null) {
2088
+ return awaitBuilds(state).then(release, release);
2089
+ }
2090
+ release();
2091
+ return Promise.resolve();
2092
+ }
2093
+
2094
+ // src/vector/vector-index/checkpoint-restore.ts
2095
+ var CALIBRATION_NEVER_CURRENT = -1;
2096
+ function sameCentroid(a, b) {
2097
+ if (a.length !== b.length) return false;
2098
+ for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
2099
+ return true;
2100
+ }
2101
+ function bufferEveryVectorOutsideTheGraph(state) {
2102
+ for (const docId of state.store.docIds()) {
2103
+ if (state.tombstones.has(docId)) continue;
2104
+ if (state.hnsw === null || !state.hnsw.has(docId)) state.buffer.add(docId);
2105
+ }
2106
+ }
2107
+ function insertNodesOfLaterGraph(state, graph, later, ordinals) {
2108
+ for (const part of later.parts) requireWholeNeighbourLists(part);
2109
+ let placed = 0;
2110
+ for (let number = 0; number < ordinals.length; number++) {
2111
+ const ordinal = ordinals[number];
2112
+ if (ordinal === NO_LIVE_VECTOR_AT_POSITION || topLayerInParts(later, number) < 0) continue;
2113
+ const docId = state.store.docIdForOrdinal(ordinal);
2114
+ if (docId !== void 0 && !graph.has(docId) && graph.insertOrdinal(ordinal)) placed += 1;
2115
+ }
2116
+ if (placed > 0) graph.connectUnreached();
2117
+ }
2118
+ function restoreGraph(state, payload, ordinalOfNumber, tombstoned, calibrationLoaded) {
2119
+ const first = payload?.graphs[0]?.parts[0];
2120
+ if (payload === null || first === void 0) {
2121
+ releaseReplacedVectors(state);
2122
+ bufferEveryVectorOutsideTheGraph(state);
2123
+ return;
2124
+ }
2125
+ const graph = createHNSWIndex(
2126
+ state.dimension,
2127
+ state.store,
2128
+ { m: first.m, efConstruction: first.efConstruction, metric: first.metric },
2129
+ state.osq ?? void 0
2130
+ );
2131
+ graph.deserializeNumbered(payload.graphs[0], ordinalOfNumber);
2132
+ for (const ordinal of tombstoned) graph.markTombstoneOrdinal(ordinal);
2133
+ adoptGraph(state, graph);
2134
+ state.shuffledBuildCount = first.shuffledBuildCount ?? null;
2135
+ if (calibrationLoaded && state.shuffledBuildCount !== null) {
2136
+ adoptLoadedCalibration(state, state.shuffledBuildCount, first.calibratedMeanLength ?? null);
2137
+ }
2138
+ for (const later of payload.graphs.slice(1)) insertNodesOfLaterGraph(state, graph, later, ordinalOfNumber);
2139
+ bufferEveryVectorOutsideTheGraph(state);
2140
+ if (state.buffer.size <= graph.size) return;
2141
+ adoptGraph(state, null);
2142
+ releaseReplacedVectors(state);
2143
+ state.buffer.clear();
2144
+ bufferEveryVectorOutsideTheGraph(state);
2145
+ }
2146
+ function beginCheckpointRestore(state, shape) {
2147
+ emptyFieldBeforeRestore(state);
2148
+ const cold = readsFromDisk(state) && (shape.holdsGraph || shape.liveVectors >= state.promotionThreshold);
2149
+ const restoresCodes = state.osq !== null && osqBitsOf(state.quantizationMode) !== null && shape.holdsGraph;
2150
+ const files = [];
2151
+ const graphNodes = shape.graph?.graphs[0] ?? null;
2152
+ const tombstoned = /* @__PURE__ */ new Set();
2153
+ let nextNumber = 0;
2154
+ let sharedCodes = null;
2155
+ let codesAgree = true;
2156
+ function restoreRecords(payload, ordinals) {
2157
+ const quantizer = state.osq;
2158
+ const layout = state.store.handles.codeLayout;
2159
+ if (!restoresCodes || quantizer === null || layout === null || !codesAgree) return;
2160
+ const codes = payload.codes;
2161
+ if (codes === null || codes.bits !== quantizer.bits) {
2162
+ codesAgree = false;
2163
+ return;
2164
+ }
2165
+ if (sharedCodes === null) {
2166
+ sharedCodes = codes;
2167
+ quantizer.restoreCentroid(Float32Array.from(codes.centroid));
2168
+ } else if (!sameCentroid(sharedCodes.centroid, codes.centroid)) {
2169
+ codesAgree = false;
2170
+ return;
2171
+ }
2172
+ const stride = layout.slotStride;
2173
+ for (let i = 0; i < ordinals.length; i++) {
2174
+ if (ordinals[i] === NO_LIVE_VECTOR_AT_POSITION) continue;
2175
+ const record = codes.records.subarray(i * stride, (i + 1) * stride);
2176
+ if (tombstoned.has(ordinals[i])) quantizer.restoreRecordAtOrdinal(ordinals[i], record);
2177
+ else quantizer.restoreRecord(payload.docIds[i], record);
2178
+ }
2179
+ }
2180
+ function graphHoldsNodeFor(number) {
2181
+ return graphNodes !== null && topLayerInParts(graphNodes, number) >= 0;
2182
+ }
2183
+ function addFile(file) {
2184
+ const { payload, location } = file;
2185
+ if (payload.dimension !== state.dimension) {
2186
+ throw new NarsilError(
2187
+ ErrorCodes.VECTOR_DIMENSION_MISMATCH,
2188
+ `Payload dimension ${payload.dimension} does not match index dimension ${state.dimension}`,
2189
+ { expected: state.dimension, received: payload.dimension }
2190
+ );
2191
+ }
2192
+ const dimension = state.dimension;
2193
+ const onDisk = readsFromDisk(state) && location !== null;
2194
+ const vectors = bytesToVectors(payload.vectors);
2195
+ const fileIndex = onDisk && cold ? state.store.addVectorFile(location.path) : null;
2196
+ const ordinals = new Int32Array(payload.docIds.length).fill(NO_LIVE_VECTOR_AT_POSITION);
2197
+ const firstNumber = nextNumber;
2198
+ nextNumber += payload.docIds.length;
2199
+ for (let i = 0; i < payload.docIds.length; i++) {
2200
+ const dead = isDead(file.dead, i);
2201
+ if (dead && !graphHoldsNodeFor(firstNumber + i)) continue;
2202
+ const docId = payload.docIds[i];
2203
+ const offset = onDisk ? location.vectorsOffset + i * dimension * 4 : 0;
2204
+ const vectorHere = () => file.byParts === void 0 ? vectors.subarray(i * dimension, (i + 1) * dimension) : file.byParts.vectorAt(i);
2205
+ if (dead) {
2206
+ ordinals[i] = state.store.insertReplaced(
2207
+ docId,
2208
+ vectorHere(),
2209
+ fileIndex === null ? void 0 : { fileIndex, offset }
2210
+ );
2211
+ tombstoned.add(ordinals[i]);
2212
+ continue;
2213
+ }
2214
+ const replaced = state.store.getOrdinal(docId);
2215
+ if (replaced !== void 0) state.osq?.removeOrdinal(replaced);
2216
+ if (fileIndex !== null) {
2217
+ const known = file.byParts?.magnitudes[i];
2218
+ ordinals[i] = state.store.insertCold(docId, known ?? magnitude(vectorHere()), { fileIndex, offset });
2219
+ continue;
2220
+ }
2221
+ const vector = vectorHere();
2222
+ ordinals[i] = state.store.insert(docId, vector);
2223
+ if (onDisk) state.pendingLocations.set(docId, { path: location.path, offset });
2224
+ }
2225
+ restoreRecords(payload, ordinals);
2226
+ files.push({ id: file.id, key: file.key, ordinals, coded: payload.codes !== null });
2227
+ }
2228
+ function finish() {
2229
+ const codesRestored = restoresCodes && codesAgree && sharedCodes !== null;
2230
+ if (restoresCodes && !codesRestored) {
2231
+ recalibrateFromStore(state);
2232
+ for (const ordinal of tombstoned) state.osq?.writeCodes(ordinal);
2233
+ }
2234
+ let total = 0;
2235
+ for (const file of files) total += file.ordinals.length;
2236
+ const ordinalOfNumber = new Int32Array(total);
2237
+ let number = 0;
2238
+ for (const file of files) {
2239
+ for (let i = 0; i < file.ordinals.length; i++) {
2240
+ const ordinal = file.ordinals[i];
2241
+ if (!state.store.holdsOrdinal(ordinal) && !tombstoned.has(ordinal)) {
2242
+ file.ordinals[i] = NO_LIVE_VECTOR_AT_POSITION;
2243
+ }
2244
+ }
2245
+ ordinalOfNumber.set(file.ordinals, number);
2246
+ number += file.ordinals.length;
2247
+ }
2248
+ restoreGraph(state, shape.graph, ordinalOfNumber, tombstoned, codesRestored);
2249
+ const current = signatureOf(state).calibration;
2250
+ const codedUnder = codesRestored && current !== null ? current : CALIBRATION_NEVER_CURRENT;
2251
+ const saved = files.map((file) => ({
2252
+ id: file.id,
2253
+ key: file.key,
2254
+ ordinals: file.ordinals,
2255
+ calibration: file.coded ? codedUnder : null
2256
+ }));
2257
+ state.savedFiles = saved;
2258
+ state.savedSignature = signatureOf(state);
2259
+ }
2260
+ return { addFile, finish };
2261
+ }
2262
+
2263
+ // src/vector/vector-index/cold.ts
2264
+ function beginColdHold(state, field, loaded, loadWanted) {
2265
+ const hold = {
2266
+ field,
2267
+ loaded,
2268
+ loadWanted,
2269
+ written: /* @__PURE__ */ new Map(),
2270
+ removedNumbers: /* @__PURE__ */ new Set(),
2271
+ removedIds: /* @__PURE__ */ new Set(),
2272
+ removals: /* @__PURE__ */ new Map(),
2273
+ replacements: /* @__PURE__ */ new Map(),
2274
+ writes: [],
2275
+ reserved: { slots: 0, docIdBytes: 0 },
2276
+ live: field.liveCount
2277
+ };
2278
+ state.cold = hold;
2279
+ return hold;
2280
+ }
2281
+ function endColdHold(state) {
2282
+ const hold = state.cold;
2283
+ if (hold === null) return;
2284
+ state.cold = null;
2285
+ try {
2286
+ for (const write of hold.writes) write();
2287
+ } catch (error) {
2288
+ state.cold = hold;
2289
+ throw error;
2290
+ }
2291
+ hold.field.close();
2292
+ }
2293
+ async function untilColdLoaded(state) {
2294
+ while (state.cold !== null) {
2295
+ state.cold.loadWanted();
2296
+ await state.cold.loaded;
2297
+ }
2298
+ }
2299
+ function cancelColdHold(state) {
2300
+ const hold = state.cold;
2301
+ if (hold === null) return;
2302
+ state.cold = null;
2303
+ hold.loadWanted();
2304
+ hold.field.close();
2305
+ }
2306
+ function openColdField(state, field, load) {
2307
+ cancelColdHold(state);
2308
+ let finished = () => void 0;
2309
+ let failed = () => void 0;
2310
+ const loaded = new Promise((resolve, reject) => {
2311
+ finished = resolve;
2312
+ failed = reject;
2313
+ });
2314
+ let loadWanted = () => void 0;
2315
+ const loadingWanted = new Promise((resolve) => {
2316
+ loadWanted = resolve;
2317
+ });
2318
+ const hold = beginColdHold(state, field, loaded, loadWanted);
2319
+ const settled = load(
2320
+ (shape) => beginCheckpointRestore(state, shape),
2321
+ () => state.cold === hold,
2322
+ loadingWanted
2323
+ ).then(() => {
2324
+ if (state.cold === hold) endColdHold(state);
2325
+ }).then(finished, failed);
2326
+ loaded.catch(() => void 0);
2327
+ return settled;
2328
+ }
2329
+ function coldHas(hold, docId) {
2330
+ if (hold.written.has(docId)) return true;
2331
+ if (hold.removedIds.has(docId)) return false;
2332
+ return hold.field.numberOf(docId) !== void 0;
2333
+ }
2334
+ function coldVectorOf(hold, docId) {
2335
+ const written = hold.written.get(docId);
2336
+ if (written !== void 0) return written.vector.slice();
2337
+ if (hold.removedIds.has(docId)) return null;
2338
+ const number = hold.field.numberOf(docId);
2339
+ return number === void 0 ? null : hold.field.vectorOfNumber(number);
2340
+ }
2341
+ function isAdmitted(subsets, docId) {
2342
+ for (const subset of subsets) {
2343
+ const { docStore } = subset.owner;
2344
+ if (docStore.numberingGeneration() !== subset.numberingGeneration) continue;
2345
+ const internalId = docStore.getInternalId(docId);
2346
+ if (internalId === void 0) continue;
2347
+ const word = subset.bits[internalId >>> 5];
2348
+ if (word !== void 0 && (word & 1 << (internalId & 31)) !== 0 && docStore.resolver().isLive(internalId)) {
2349
+ return true;
2350
+ }
2351
+ }
2352
+ return false;
2353
+ }
2354
+ function admissionOf(options, hold) {
2355
+ const { filterSubsets, filterPartitions } = options;
2356
+ if (filterSubsets !== void 0) return (docId) => isAdmitted(filterSubsets, docId);
2357
+ if (filterPartitions !== void 0) {
2358
+ return (docId) => {
2359
+ const partition = hold.field.partitionOf(docId);
2360
+ return partition !== void 0 && filterPartitions.has(partition);
2361
+ };
2362
+ }
2363
+ return null;
2364
+ }
2365
+ function bitCount(word) {
2366
+ let value = word - (word >>> 1 & 1431655765);
2367
+ value = (value & 858993459) + (value >>> 2 & 858993459);
2368
+ return (value + (value >>> 4) & 252645135) * 16843009 >>> 24;
2369
+ }
2370
+ function admittedBySubsets(subsets) {
2371
+ let admitted = 0;
2372
+ for (const subset of subsets) {
2373
+ for (let at = 0; at < subset.bits.length; at++) admitted += bitCount(subset.bits[at]);
2374
+ }
2375
+ return admitted;
2376
+ }
2377
+ function* idsOfSubsets(subsets) {
2378
+ for (const subset of subsets) {
2379
+ const { bits } = subset;
2380
+ const { docStore } = subset.owner;
2381
+ if (docStore.numberingGeneration() !== subset.numberingGeneration) continue;
2382
+ for (let wordIndex = 0; wordIndex < bits.length; wordIndex++) {
2383
+ let word = bits[wordIndex];
2384
+ while (word !== 0) {
2385
+ const internalId = (wordIndex << 5) + (Math.clz32(word & -word) ^ 31);
2386
+ word &= word - 1;
2387
+ if (!docStore.resolver().isLive(internalId)) continue;
2388
+ const docId = docStore.getExternalId(internalId);
2389
+ if (docId !== void 0) yield docId;
2390
+ }
2391
+ }
2392
+ }
2393
+ }
2394
+ function scoreOf(query, qMag, vector, metric) {
2395
+ const vectorMagnitude = metric === "cosine" ? magnitude(vector) : 0;
2396
+ return toScore(toDistance(query, vector, qMag, vectorMagnitude, metric), metric);
2397
+ }
2398
+ function scanWritten(hold, query, k, options, admits) {
2399
+ if (hold.written.size === 0) return [];
2400
+ const qMag = magnitude(query);
2401
+ const heap = createBoundedMaxHeap(highScoreFirst, Math.max(k, 1));
2402
+ for (const [docId, { vector }] of hold.written) {
2403
+ if (admits !== null && !admits(docId)) continue;
2404
+ const score = scoreOf(query, qMag, vector, options.metric);
2405
+ if (score >= options.minSimilarity) heap.push({ docId, score });
2406
+ }
2407
+ return heap.toSortedArray().reverse();
2408
+ }
2409
+ function scanSubsets(hold, query, k, options, subsets) {
2410
+ const qMag = magnitude(query);
2411
+ const heap = createBoundedMaxHeap(highScoreFirst, Math.max(k, 1));
2412
+ let matched = 0;
2413
+ for (const docId of idsOfSubsets(subsets)) {
2414
+ const written = hold.written.get(docId);
2415
+ let score;
2416
+ if (written !== void 0) {
2417
+ score = scoreOf(query, qMag, written.vector, options.metric);
2418
+ } else {
2419
+ if (hold.removedIds.has(docId)) continue;
2420
+ const number = hold.field.numberOf(docId);
2421
+ if (number === void 0) continue;
2422
+ score = scoreOf(query, qMag, hold.field.reusedVectorOfNumber(number), options.metric);
2423
+ }
2424
+ if (score < options.minSimilarity) continue;
2425
+ matched += 1;
2426
+ heap.push({ docId, score });
2427
+ }
2428
+ return { results: heap.toSortedArray().reverse(), matched };
2429
+ }
2430
+ function outcomeWithoutGraphWalk(state, hold, query, k, options) {
2431
+ if (hold.live === 0) return { results: [], matched: 0, matchedExact: true };
2432
+ const subsets = options.filterSubsets;
2433
+ let admitted = hold.live;
2434
+ if (subsets !== void 0) {
2435
+ admitted = admittedBySubsets(subsets);
2436
+ if (admitted === 0) return { results: [], matched: 0, matchedExact: true };
2437
+ if (admitted / hold.live < state.filterThreshold) {
2438
+ const { results, matched } = scanSubsets(hold, query, k, options, subsets);
2439
+ return { results: results.slice(0, Math.max(k, 0)), matched, matchedExact: true };
2440
+ }
2441
+ }
2442
+ if (k <= 0) {
2443
+ return options.minSimilarity === Number.NEGATIVE_INFINITY ? { results: [], matched: admitted, matchedExact: true } : { results: [], matched: 0, matchedExact: false };
2444
+ }
2445
+ return { admits: admissionOf(options, hold), admitted };
2446
+ }
2447
+ function outcomeOfGraphWalk(hold, walk, fromGraph, query, k, options) {
2448
+ const results = mergeResults(fromGraph, scanWritten(hold, query, k, options, walk.admits), k);
2449
+ return options.minSimilarity === Number.NEGATIVE_INFINITY ? { results, matched: walk.admitted, matchedExact: true } : { results, matched: results.length, matchedExact: false };
2450
+ }
2451
+ function coldSearch(state, hold, query, k, options) {
2452
+ const walk = outcomeWithoutGraphWalk(state, hold, query, k, options);
2453
+ if ("results" in walk) {
2454
+ hold.loadWanted();
2455
+ return walk;
2456
+ }
2457
+ const fromGraph = hold.field.search(query, k, options, walk.admits, hold.removedNumbers);
2458
+ hold.loadWanted();
2459
+ return outcomeOfGraphWalk(hold, walk, fromGraph, query, k, options);
2460
+ }
2461
+ async function coldSearchConcurrently(state, hold, query, k, options) {
2462
+ const walk = outcomeWithoutGraphWalk(state, hold, query, k, options);
2463
+ if ("results" in walk) {
2464
+ hold.loadWanted();
2465
+ return walk;
2466
+ }
2467
+ const fromGraph = await hold.field.searchConcurrently(query, k, options, walk.admits, hold.removedNumbers);
2468
+ hold.loadWanted();
2469
+ return outcomeOfGraphWalk(hold, walk, fromGraph, query, k, options);
2470
+ }
2471
+
2472
+ // src/vector/vector-index/cold-writes.ts
2473
+ var encoder = new TextEncoder();
2474
+ var queuedWriteUndone = () => void 0;
2475
+ function sameVector(held, vector) {
2476
+ for (let at = 0; at < vector.length; at++) if (held[at] !== vector[at]) return false;
2477
+ return true;
2478
+ }
2479
+ function claimsOfField(hold, wanted) {
2480
+ const atMost = hold.field.claimsAtMost;
2481
+ const fits = atMost.slots + wanted.slots <= VECTOR_MEMORY_LIMITS.maxOrdinals && atMost.docIdBytes + wanted.docIdBytes <= VECTOR_MEMORY_LIMITS.maxDocIdTableBytes;
2482
+ return fits ? atMost : hold.field.claims();
2483
+ }
2484
+ function requireRoomInHold(hold, docId) {
2485
+ const { reserved } = hold;
2486
+ const wanted = { slots: reserved.slots + 1, docIdBytes: reserved.docIdBytes + encoder.encode(docId).length };
2487
+ const loaded = claimsOfField(hold, wanted);
2488
+ requireOrdinalWithinLimit(loaded.slots + reserved.slots);
2489
+ requireBytesWithinLimit(loaded.docIdBytes + wanted.docIdBytes, VECTOR_MEMORY_LIMITS.maxDocIdTableBytes);
2490
+ return wanted;
2491
+ }
2492
+ function reserveReplayedInsert(hold, docId) {
2493
+ const wanted = requireRoomInHold(hold, docId);
2494
+ const added = { slots: wanted.slots - hold.reserved.slots, docIdBytes: wanted.docIdBytes - hold.reserved.docIdBytes };
2495
+ hold.reserved.slots = wanted.slots;
2496
+ hold.reserved.docIdBytes = wanted.docIdBytes;
2497
+ return added;
2498
+ }
2499
+ function retireCopy(hold, docId) {
2500
+ const written = hold.written.get(docId);
2501
+ if (written !== void 0) {
2502
+ hold.written.delete(docId);
2503
+ return written;
2504
+ }
2505
+ if (hold.removedIds.has(docId)) return void 0;
2506
+ const number = hold.field.numberOf(docId);
2507
+ if (number === void 0) return void 0;
2508
+ hold.removedIds.add(docId);
2509
+ hold.removedNumbers.add(number);
2510
+ return number;
2511
+ }
2512
+ function vectorOfCopy(hold, copy) {
2513
+ return typeof copy === "number" ? hold.field.reusedVectorOfNumber(copy) : copy.vector;
2514
+ }
2515
+ function putBackCopy(hold, docId, copy) {
2516
+ if (typeof copy === "number") {
2517
+ hold.removedIds.delete(docId);
2518
+ hold.removedNumbers.delete(copy);
2519
+ } else {
2520
+ hold.written.set(docId, copy);
2521
+ }
2522
+ }
2523
+ function restoreRemovedCopy(hold, docId, vector) {
2524
+ const removal = hold.removals.get(docId);
2525
+ if (removal === void 0) return false;
2526
+ const { copy, queuedAt } = removal;
2527
+ if (!sameVector(vectorOfCopy(hold, copy), vector)) return false;
2528
+ hold.removals.delete(docId);
2529
+ hold.writes[queuedAt] = queuedWriteUndone;
2530
+ putBackCopy(hold, docId, copy);
2531
+ hold.live += 1;
2532
+ return true;
2533
+ }
2534
+ function keepReplacement(hold, docId, replacement) {
2535
+ const kept = hold.replacements.get(docId);
2536
+ if (kept === void 0) hold.replacements.set(docId, [replacement]);
2537
+ else kept.push(replacement);
2538
+ }
2539
+ function fieldHoldsVector(hold, docId, vector) {
2540
+ if (hold.removedIds.has(docId)) return false;
2541
+ const number = hold.field.numberOf(docId);
2542
+ return number !== void 0 && sameVector(hold.field.reusedVectorOfNumber(number), vector);
2543
+ }
2544
+ function coldInsert(hold, docId, vector, partitionId, keepsReplacedCopy, replay) {
2545
+ const restored = !hold.written.has(docId) && restoreRemovedCopy(hold, docId, vector);
2546
+ const written = hold.written.get(docId);
2547
+ if (written !== void 0 && sameVector(written.vector, vector)) {
2548
+ if (partitionId === void 0 || partitionId === written.partitionId) return restored;
2549
+ hold.written.set(docId, { vector: written.vector, partitionId });
2550
+ hold.writes.push(replay);
2551
+ return true;
2552
+ }
2553
+ if (written === void 0 && (restored || fieldHoldsVector(hold, docId, vector))) {
2554
+ if (partitionId === void 0) return restored;
2555
+ hold.writes.push(replay);
2556
+ return true;
2557
+ }
2558
+ const reserved = reserveReplayedInsert(hold, docId);
2559
+ const removal = hold.removals.get(docId);
2560
+ hold.removals.delete(docId);
2561
+ const retired = retireCopy(hold, docId);
2562
+ if (retired === void 0) hold.live += 1;
2563
+ const inserted = { vector, partitionId };
2564
+ hold.written.set(docId, inserted);
2565
+ const copy = removal === void 0 ? retired : removal.copy;
2566
+ if (keepsReplacedCopy && copy !== void 0) {
2567
+ const queuedAt = removal === void 0 ? [hold.writes.length] : [removal.queuedAt, hold.writes.length];
2568
+ keepReplacement(hold, docId, { copy, inserted, queuedAt, reserved });
2569
+ }
2570
+ hold.writes.push(replay);
2571
+ return true;
2572
+ }
2573
+ function coldRemove(hold, docId, replay) {
2574
+ const copy = retireCopy(hold, docId);
2575
+ if (copy === void 0) return false;
2576
+ hold.live -= 1;
2577
+ hold.removals.set(docId, { queuedAt: hold.writes.length, copy });
2578
+ hold.writes.push(replay);
2579
+ return true;
2580
+ }
2581
+ function coldRestore(hold, docId, vector) {
2582
+ if (!hold.written.has(docId)) return restoreRemovedCopy(hold, docId, vector);
2583
+ const replacements = hold.replacements.get(docId);
2584
+ const last = replacements?.[replacements.length - 1];
2585
+ if (replacements === void 0 || last === void 0 || hold.written.get(docId) !== last.inserted) return false;
2586
+ if (!sameVector(vectorOfCopy(hold, last.copy), vector)) return false;
2587
+ replacements.pop();
2588
+ if (replacements.length === 0) hold.replacements.delete(docId);
2589
+ for (const at of last.queuedAt) hold.writes[at] = queuedWriteUndone;
2590
+ hold.written.delete(docId);
2591
+ putBackCopy(hold, docId, last.copy);
2592
+ hold.reserved.slots -= last.reserved.slots;
2593
+ hold.reserved.docIdBytes -= last.reserved.docIdBytes;
2594
+ return true;
2595
+ }
2596
+
2597
+ // src/vector/vector-index/flagged-documents.ts
2598
+ var FlaggedDocuments = class {
2599
+ constructor(store, layout) {
2600
+ this.store = store;
2601
+ this.layout = layout;
2602
+ }
2603
+ members = createDocIdMap();
2604
+ ordinals = createDocIdMap();
2605
+ retired = /* @__PURE__ */ new Set();
2606
+ handles = null;
2607
+ flags = new Uint8Array(0);
2608
+ chunks = null;
2609
+ get size() {
2610
+ return this.members.size;
2611
+ }
2612
+ get retiredCount() {
2613
+ return this.retired.size;
2614
+ }
2615
+ has(docId) {
2616
+ return this.members.has(docId);
2617
+ }
2618
+ [Symbol.iterator]() {
2619
+ return this.members.keys()[Symbol.iterator]();
2620
+ }
2621
+ add(docId) {
2622
+ this.members.set(docId, true);
2623
+ const handles = this.openHandles();
2624
+ if (handles === null) return this;
2625
+ this.flagCurrentOrdinal(docId);
2626
+ this.publishCount(handles);
2627
+ return this;
2628
+ }
2629
+ delete(docId) {
2630
+ return this.leave(docId, false);
2631
+ }
2632
+ retire(docId) {
2633
+ return this.leave(docId, this.layout.keepsRetiredFlags);
2634
+ }
2635
+ forgetRetired(mayForget) {
2636
+ if (this.retired.size === 0) return;
2637
+ const handles = this.openHandles();
2638
+ if (handles === null) return;
2639
+ for (const ordinal of this.retired) {
2640
+ if (!mayForget(ordinal)) continue;
2641
+ this.retired.delete(ordinal);
2642
+ this.unflag(ordinal);
2643
+ }
2644
+ this.publishCount(handles);
2645
+ }
2646
+ clear() {
2647
+ this.members.clear();
2648
+ const handles = this.openHandles();
2649
+ if (handles !== null) {
2650
+ for (const ordinal of this.ordinals.values()) this.unflag(ordinal);
2651
+ for (const ordinal of this.retired) this.unflag(ordinal);
2652
+ Atomics.store(handles.header, this.layout.countWord, 0);
2653
+ }
2654
+ this.ordinals.clear();
2655
+ this.retired.clear();
2656
+ }
2657
+ leave(docId, keepsFlag) {
2658
+ const held = this.members.delete(docId);
2659
+ const store = this.store;
2660
+ if (!held && this.handles !== null && store.dimension !== 0 && store.handles === this.handles) {
2661
+ if (!this.ordinals.has(docId)) return false;
2662
+ }
2663
+ const handles = this.openHandles();
2664
+ if (handles === null) return held;
2665
+ const ordinal = this.ordinals.get(docId);
2666
+ if (ordinal !== void 0) {
2667
+ this.ordinals.delete(docId);
2668
+ if (keepsFlag) this.retired.add(ordinal);
2669
+ else this.unflag(ordinal);
2670
+ }
2671
+ Atomics.store(handles.header, this.layout.countWord, this.members.size + this.retired.size);
2672
+ return held;
2673
+ }
2674
+ visitTables(name, visit) {
2675
+ visit(name, this.members.size, false);
2676
+ visit(`${name}-ordinals`, this.ordinals.size, false);
2677
+ }
2678
+ publishCount(handles) {
2679
+ Atomics.store(handles.header, this.layout.countWord, this.members.size + this.retired.size);
2680
+ }
2681
+ flagCurrentOrdinal(docId) {
2682
+ const ordinal = this.store.getOrdinal(docId);
2683
+ const previous = this.ordinals.get(docId);
2684
+ if (ordinal === void 0 || ordinal === previous) return;
2685
+ if (!this.retired.delete(ordinal)) {
2686
+ if (this.chunks !== null) Atomics.add(this.chunks, Math.floor(ordinal / UNPLACED_FLAG_CHUNK_ORDINALS), 1);
2687
+ Atomics.store(this.flags, ordinal, 1);
2688
+ }
2689
+ this.ordinals.set(docId, ordinal);
2690
+ if (previous === void 0) return;
2691
+ if (this.layout.keepsRetiredFlags) this.retired.add(previous);
2692
+ else this.unflag(previous);
2693
+ }
2694
+ unflag(ordinal) {
2695
+ Atomics.store(this.flags, ordinal, 0);
2696
+ if (this.chunks !== null) Atomics.sub(this.chunks, Math.floor(ordinal / UNPLACED_FLAG_CHUNK_ORDINALS), 1);
2697
+ }
2698
+ openHandles() {
2699
+ if (this.store.dimension === 0) return null;
2700
+ const handles = this.store.handles;
2701
+ if (handles === this.handles) return handles;
2702
+ this.handles = handles;
2703
+ this.flags = new Uint8Array(this.layout.flagsOf(handles));
2704
+ const chunksOf = this.layout.chunksOf;
2705
+ this.chunks = chunksOf === null ? null : new Int32Array(chunksOf(handles));
2706
+ this.ordinals.clear();
2707
+ this.retired.clear();
2708
+ for (const docId of this.members.keys()) this.flagCurrentOrdinal(docId);
2709
+ return handles;
2710
+ }
2711
+ };
2712
+ function createUnplacedVectors(store) {
2713
+ return new FlaggedDocuments(store, {
2714
+ flagsOf: (handles) => handles.unplaced,
2715
+ chunksOf: (handles) => handles.unplacedChunks,
2716
+ countWord: STORE_UNPLACED_COUNT,
2717
+ keepsRetiredFlags: true
2718
+ });
2719
+ }
2720
+ function createRemovedDocuments(store) {
2721
+ return new FlaggedDocuments(store, {
2722
+ flagsOf: (handles) => handles.removed,
2723
+ chunksOf: null,
2724
+ countWord: STORE_REMOVED_COUNT,
2725
+ keepsRetiredFlags: false
2726
+ });
2727
+ }
2728
+ function visitFlaggedTables(name, documents, visit) {
2729
+ if (documents instanceof FlaggedDocuments) documents.visitTables(name, visit);
2730
+ }
2731
+
2732
+ // src/vector/vector-index/maintenance.ts
2733
+ function compact(state) {
2734
+ state.store.freeRetiredVectors();
2735
+ const oldestPin = state.store.oldestPin();
2736
+ state.buffer.forgetRetired((ordinal) => state.store.mayFreeOrdinal(ordinal, oldestPin));
2737
+ if (state.tombstones.size === 0 && state.store.replacedVectorCount === 0) return;
2738
+ if (state.hnsw) {
2739
+ const before = state.hnsw.tombstoneCount;
2740
+ state.hnsw.compactTombstones(oldestPin);
2741
+ state.compactedNodeCount += before - state.hnsw.tombstoneCount;
2742
+ }
2743
+ state.freshGraph?.compactTombstones(oldestPin);
2744
+ state.growingCopy?.compactTombstones(oldestPin);
2745
+ for (const docId of state.tombstones) {
2746
+ const ordinal = state.store.getOrdinal(docId);
2747
+ if (ordinal !== void 0 && !state.store.mayFreeOrdinal(ordinal, oldestPin)) continue;
2748
+ state.store.remove(docId, oldestPin);
2749
+ state.buffer.delete(docId);
2750
+ state.tombstones.delete(docId);
2751
+ if (state.osq) {
2752
+ state.osq.remove(docId);
2753
+ }
2754
+ }
2755
+ releaseReplacedVectors(state, oldestPin);
2756
+ }
2757
+ async function insertMissing(state, graph) {
2758
+ const missingOrReplaced = (docId) => !graph.has(docId) || state.buffer.has(docId);
2759
+ const heldWhenTheCallStarted = [...allLiveDocIds(state)];
2760
+ const stillLiveAndUnplaced = (docId) => !state.tombstones.has(docId) && missingOrReplaced(docId);
2761
+ continueAfterGrowth(state, await placeInGrowingCopy(state, graph, heldWhenTheCallStarted, stillLiveAndUnplaced));
2762
+ }
2763
+ async function foldIntoGraph(state) {
2764
+ const rebuildNeeded = graphNeedsRebuild(state);
2765
+ compact(state);
2766
+ if (liveSize(state) === 0) {
2767
+ const previous = state.hnsw;
2768
+ adoptGraph(state, null);
2769
+ if (previous !== null) dropSharedGraph(state, previous);
2770
+ state.buffer.clear();
2771
+ if (state.osq) {
2772
+ state.osq.clear();
2773
+ }
2774
+ return;
2775
+ }
2776
+ const graph = state.hnsw;
2777
+ if (graph === null || rebuildNeeded || state.shuffledBuildCount === null) {
2778
+ if (await calibrateBeforeBuild(state)) await buildGraphFromStore(state);
2779
+ return;
2780
+ }
2781
+ if (await recalibrateWhenDrifted(state, liveSize(state))) {
2782
+ await buildGraphFromStore(state);
2783
+ return;
2784
+ }
2785
+ await insertMissing(state, graph);
2786
+ }
2787
+ function optimize(state) {
2788
+ return buildExclusively(state, () => foldIntoGraph(state));
2789
+ }
2790
+ async function fillGraph(state) {
2791
+ const graph = state.hnsw;
2792
+ if (graph === null) {
2793
+ if (liveSize(state) >= state.promotionThreshold) await promoteToGraph(state);
2794
+ return;
2795
+ }
2796
+ if (state.osq && !state.osq.isCalibrated() && !await calibrateBeforeBuild(state)) return;
2797
+ if (state.shuffledRebuild === null && graphNeedsRebuild(state)) {
2798
+ await buildGraphFromStore(state);
2799
+ return;
2800
+ }
2801
+ await insertMissing(state, graph);
2802
+ }
2803
+ function completeGraph(state) {
2804
+ return buildExclusively(state, () => fillGraph(state), false, true);
2805
+ }
2806
+ function maintenanceStatus(state) {
2807
+ const storeSize = state.store.size;
2808
+ const graphCount = state.hnsw || state.cold ? 1 : 0;
2809
+ const graphNodes = state.hnsw === null ? storeSize : state.hnsw.size + state.hnsw.tombstoneCount;
2810
+ const tombstoned = state.tombstones.size + state.store.replacedVectorCount;
2811
+ const tombstoneRatio = graphNodes > 0 ? tombstoned / graphNodes : 0;
2812
+ const estimatedCompactMs = Math.round(tombstoned * ESTIMATED_MS_PER_TOMBSTONE * state.dimensionScale);
2813
+ const rebuilds = state.hnsw === null || graphNeedsRebuild(state) || shuffledRebuildDue(state, liveSize(state));
2814
+ const optimizeVectorCount = rebuilds ? storeSize : state.buffer.size;
2815
+ const estimatedOptimizeMs = Math.round(optimizeVectorCount * ESTIMATED_MS_PER_VECTOR_REBUILD * state.dimensionScale);
2816
+ return {
2817
+ tombstoneRatio,
2818
+ graphCount,
2819
+ bufferSize: state.buffer.size,
2820
+ building: state.building || state.buildScheduled || state.buildTimer !== null || state.shuffledRebuild !== null,
2821
+ waitingForMemory: state.waitingForMemory,
2822
+ estimatedCompactMs,
2823
+ estimatedOptimizeMs
2824
+ };
2825
+ }
2826
+ function flatScanScratchBytes(state) {
2827
+ return state.store.dimension === 0 ? 0 : nativeStoreScratchBytes(state.store.handles);
2828
+ }
2829
+ function estimateMemoryBytes(state) {
2830
+ if (state.hnsw === null) return state.store.memoryBytes() + flatScanScratchBytes(state);
2831
+ const passGraphBytes = (state.freshGraph?.graphBytes ?? 0) + (state.growingCopy?.graphBytes ?? 0);
2832
+ return state.store.memoryBytes() + state.hnsw.graphBytes + state.hnsw.searchScratchBytes + passGraphBytes;
2833
+ }
2834
+
2835
+ // src/vector/vector-index/publication.ts
2836
+ var nextStore = 0;
2837
+ function vectorPublicationSource(state) {
2838
+ const handle = `publication-${++nextStore}`;
2839
+ return {
2840
+ prepareCopyPublication: () => untilColdLoaded(state),
2841
+ holdCopyPublication() {
2842
+ const pin = holdAdmission(state.store.view);
2843
+ try {
2844
+ const handles = fieldHandlesOf(state, state.hnsw, true);
2845
+ return {
2846
+ field: { handle, sequence: pin.admission.pin, handles },
2847
+ release: pin.release
2848
+ };
2849
+ } catch (error) {
2850
+ pin.release();
2851
+ throw error;
2852
+ }
2853
+ }
2854
+ };
2855
+ }
2856
+
2857
+ // src/vector/vector-index/rollback-holds.ts
2858
+ function releaseHold(state, docId, hold) {
2859
+ hold.writes -= 1;
2860
+ if (hold.writes > 0) return;
2861
+ if (state.rollbackHolds.get(docId) === hold) state.rollbackHolds.delete(docId);
2862
+ for (const { view, pin } of hold.pins) storePinsOf(view).release(pin);
2863
+ hold.pins.length = 0;
2864
+ hold.ordinals.length = 0;
2865
+ state.cold?.replacements.delete(docId);
2866
+ }
2867
+ function holdForRollback(state, docId) {
2868
+ let hold = state.rollbackHolds.get(docId);
2869
+ if (hold === void 0) {
2870
+ hold = { writes: 0, ordinals: [], pins: [] };
2871
+ state.rollbackHolds.set(docId, hold);
2872
+ }
2873
+ hold.writes += 1;
2874
+ const held = hold;
2875
+ let released = false;
2876
+ return () => {
2877
+ if (released) return;
2878
+ released = true;
2879
+ releaseHold(state, docId, held);
2880
+ };
2881
+ }
2882
+ function keepRetiredOrdinal(state, docId, ordinal) {
2883
+ const hold = state.rollbackHolds.get(docId);
2884
+ if (hold === void 0 || hold.ordinals[hold.ordinals.length - 1] === ordinal) return;
2885
+ const view = state.store.view;
2886
+ hold.pins.push({ view, pin: storePinsOf(view).take() });
2887
+ hold.ordinals.push(ordinal);
2888
+ }
2889
+ function forgetKeptOrdinal(state, docId, ordinal) {
2890
+ const ordinals = state.rollbackHolds.get(docId)?.ordinals;
2891
+ const at = ordinals?.lastIndexOf(ordinal) ?? -1;
2892
+ if (ordinals !== void 0 && at !== -1) ordinals.splice(at, 1);
2893
+ }
2894
+ function keptOrdinalsOf(state, docId) {
2895
+ return state.rollbackHolds.get(docId)?.ordinals ?? [];
2896
+ }
2897
+
2898
+ // src/vector/vector-index/search.ts
2899
+ function* bufferCandidates(state, buffered, filter) {
2900
+ for (const docId of buffered) {
2901
+ if (state.tombstones.has(docId)) continue;
2902
+ if (filter) {
2903
+ const ordinal = state.store.getOrdinal(docId);
2904
+ if (ordinal === void 0 || !ordinalFilterHas(filter, ordinal)) continue;
2905
+ }
2906
+ yield docId;
2907
+ }
2908
+ }
2909
+ function* filteredDocIds(state, filter) {
2910
+ for (const ordinal of ordinalFilterValues(filter)) {
2911
+ const docId = state.store.docIdForOrdinal(ordinal);
2912
+ if (docId !== void 0) yield docId;
2913
+ }
2914
+ }
2915
+ function admittedTotal(state, filter) {
2916
+ return filter === void 0 ? liveSize(state) : filter.count;
2917
+ }
2918
+ function fetchedOutcome(state, results, minSimilarity, filter) {
2919
+ if (minSimilarity === Number.NEGATIVE_INFINITY) {
2920
+ return { results, matched: admittedTotal(state, filter), matchedExact: true };
2921
+ }
2922
+ return { results, matched: results.length, matchedExact: false };
2923
+ }
2924
+ function scannedOutcome(scan) {
2925
+ return { results: scan.results, matched: scan.matched, matchedExact: true };
2926
+ }
2927
+ function liveOrdinalOf(state, docId, filter) {
2928
+ if (state.tombstones.has(docId)) return void 0;
2929
+ const ordinal = state.store.getOrdinal(docId);
2930
+ if (ordinal === void 0 || filter !== void 0 && !ordinalFilterHas(filter, ordinal)) return void 0;
2931
+ return ordinal;
2932
+ }
2933
+ function liveCandidates(state, candidates, filter) {
2934
+ const docIds = [];
2935
+ const ordinals = [];
2936
+ for (const docId of candidates) {
2937
+ const ordinal = liveOrdinalOf(state, docId, filter);
2938
+ if (ordinal === void 0) continue;
2939
+ docIds.push(docId);
2940
+ ordinals.push(ordinal);
2941
+ }
2942
+ return { docIds, ordinals };
2943
+ }
2944
+ function createScanTally(capacity) {
2945
+ return {
2946
+ best: createBoundedMaxHeap(highScoreFirst, capacity),
2947
+ capacity,
2948
+ matched: 0,
2949
+ bestDiscardedScore: Number.NEGATIVE_INFINITY
2950
+ };
2951
+ }
2952
+ function admitScore(tally, docId, score) {
2953
+ tally.matched++;
2954
+ const worst = tally.best.size < tally.capacity ? void 0 : tally.best.peek();
2955
+ if (worst !== void 0 && score < worst.score) {
2956
+ if (score > tally.bestDiscardedScore) tally.bestDiscardedScore = score;
2957
+ return;
2958
+ }
2959
+ const result = { docId, score };
2960
+ tally.best.push(result);
2961
+ if (worst === void 0) return;
2962
+ const discarded = highScoreFirst(result, worst) < 0 ? worst.score : score;
2963
+ if (discarded > tally.bestDiscardedScore) tally.bestDiscardedScore = discarded;
2964
+ }
2965
+ function scoreCandidates2(state, query, metric, minSimilarity, docIds, ordinals, tally) {
2966
+ const count = ordinals.length;
2967
+ const distances = nativeScoresOf(state.store.handles, query, metric, ordinals, state.store.writeSequence());
2968
+ if (distances !== null) {
2969
+ for (let i = 0; i < count; i++) {
2970
+ if (distances[i] === Number.POSITIVE_INFINITY) continue;
2971
+ const score = toScore(distances[i], metric);
2972
+ if (score < minSimilarity) continue;
2973
+ admitScore(tally, docIds[i], score);
2974
+ }
2975
+ return;
2976
+ }
2977
+ noteFallback("score");
2978
+ const arenaQuery = state.store.prepareQueryArena(query);
2979
+ const queryMag = arenaQuery ? arenaQuery.magnitude : magnitude(query);
2980
+ for (let i = 0; i < count; i++) {
2981
+ const docId = docIds[i];
2982
+ let score;
2983
+ if (arenaQuery) {
2984
+ const distance = state.store.distanceFromArena(arenaQuery, ordinals[i], metric);
2985
+ if (distance === Number.POSITIVE_INFINITY) continue;
2986
+ score = toScore(distance, metric);
2987
+ } else {
2988
+ const entry = state.store.get(docId);
2989
+ if (!entry) continue;
2990
+ switch (metric) {
2991
+ case "cosine":
2992
+ score = cosineSimilarityWithMagnitudes(query, entry.vector, queryMag, entry.magnitude);
2993
+ break;
2994
+ case "dotProduct":
2995
+ score = dotProduct(query, entry.vector);
2996
+ break;
2997
+ case "euclidean": {
2998
+ const dist = euclideanDistance(query, entry.vector);
2999
+ score = 1 / (1 + dist);
3000
+ break;
3001
+ }
3002
+ }
3003
+ }
3004
+ if (score < minSimilarity) continue;
3005
+ admitScore(tally, docId, score);
3006
+ }
3007
+ }
3008
+ function bruteForceSearch(state, query, k, metric, minSimilarity, candidates, filter) {
3009
+ const scanned = liveCandidates(state, candidates, filter);
3010
+ const tally = createScanTally(k);
3011
+ scoreCandidates2(state, query, metric, minSimilarity, scanned.docIds, scanned.ordinals, tally);
3012
+ return { results: tally.best.toSortedArray().reverse(), matched: tally.matched };
3013
+ }
3014
+ function search(state, query, k, options) {
3015
+ handOverBuildTurns(state);
3016
+ return searchWithFilter(state, query, k, options, filterForOptions(state, options));
3017
+ }
3018
+ function searchWithFilter(state, query, k, options, filter) {
3019
+ const steps = searchSteps(state, query, k, options, filter);
3020
+ let step = steps.next();
3021
+ while (step.done !== true) {
3022
+ const { candidates, filter: remaining } = step.value;
3023
+ step = steps.next(bruteForceSearch(state, query, k, options.metric, options.minSimilarity, candidates, remaining));
3024
+ }
3025
+ return step.value;
3026
+ }
3027
+ function* searchSteps(state, query, k, options, filter) {
3028
+ if (query.length !== state.dimension) {
3029
+ throw new NarsilError(
3030
+ ErrorCodes.VECTOR_DIMENSION_MISMATCH,
3031
+ `Vector dimension mismatch: expected ${state.dimension}, got ${query.length}`,
3032
+ { expected: state.dimension, received: query.length }
3033
+ );
3034
+ }
3035
+ const currentLiveSize = liveSize(state);
3036
+ const { minSimilarity } = options;
3037
+ if (currentLiveSize === 0) return { results: [], matched: 0, matchedExact: true };
3038
+ if (filter && filter.count === 0) return { results: [], matched: 0, matchedExact: true };
3039
+ if (k <= 0) return fetchedOutcome(state, [], minSimilarity, filter);
3040
+ if (state.buffer.size > 0 && !state.building && !state.buildScheduled) {
3041
+ scheduleBuild(state);
3042
+ }
3043
+ if (!state.hnsw) {
3044
+ const candidates = filter ? filteredDocIds(state, filter) : allLiveDocIds(state);
3045
+ return scannedOutcome(yield { candidates, filter: void 0, graphResults: null });
3046
+ }
3047
+ if (filter) {
3048
+ const hnswLiveSize = state.hnsw.size;
3049
+ const selectivity = hnswLiveSize > 0 ? filter.count / hnswLiveSize : 1;
3050
+ if (selectivity < state.filterThreshold) {
3051
+ return scannedOutcome(yield { candidates: filteredDocIds(state, filter), filter: void 0, graphResults: null });
3052
+ }
3053
+ }
3054
+ const hnswResults = graphResultsOf(state, query, k, options, filter);
3055
+ if (state.buffer.size === 0) return fetchedOutcome(state, hnswResults, minSimilarity, filter);
3056
+ const scan = yield { candidates: state.buffer, filter, graphResults: hnswResults };
3057
+ return fetchedOutcome(state, mergeResults(hnswResults, scan.results, k), minSimilarity, filter);
3058
+ }
3059
+ function graphResultsOf(state, query, k, options, filter) {
3060
+ if (state.hnsw === null) return [];
3061
+ const { metric, minSimilarity, efSearch, oversample, rescoreWindow } = options;
3062
+ return state.hnsw.search(query, k, metric, minSimilarity, { filter, efSearch, oversample, rescoreWindow });
3063
+ }
3064
+ function withBufferedVectors(state, query, k, options, filter, graphResults, buffered) {
3065
+ const scan = bruteForceSearch(state, query, k, options.metric, options.minSimilarity, buffered, filter);
3066
+ return mergeResults(graphResults, scan.results, k);
3067
+ }
3068
+
3069
+ // src/vector/vector-index/search-in-turns.ts
3070
+ function layoutGeneration(state) {
3071
+ return Atomics.load(state.store.handles.header, STORE_ORDINAL_LAYOUT_GENERATION);
3072
+ }
3073
+ async function scanInTurns(state, query, capacity, options, request, chunkOfThisSearch) {
3074
+ const tally = createScanTally(capacity);
3075
+ const { docIds, ordinals } = chunkOfThisSearch;
3076
+ let length = 0;
3077
+ const scoreChunk = () => {
3078
+ const chunk = length === SEARCH_SCAN_CHUNK_VECTORS ? ordinals : ordinals.subarray(0, length);
3079
+ scoreCandidates2(state, query, options.metric, options.minSimilarity, docIds, chunk, tally);
3080
+ length = 0;
3081
+ };
3082
+ for (const docId of request.candidates) {
3083
+ const ordinal = liveOrdinalOf(state, docId, request.filter);
3084
+ if (ordinal === void 0) continue;
3085
+ docIds[length] = docId;
3086
+ ordinals[length] = ordinal;
3087
+ length++;
3088
+ if (length < SEARCH_SCAN_CHUNK_VECTORS) continue;
3089
+ scoreChunk();
3090
+ if (turnIsSpent()) await yieldTurn();
3091
+ }
3092
+ if (length > 0) scoreChunk();
3093
+ return {
3094
+ kept: tally.best.toSortedArray().reverse(),
3095
+ matched: tally.matched,
3096
+ bestDiscardedScore: tally.bestDiscardedScore
3097
+ };
3098
+ }
3099
+ async function passInTurns(state, query, k, options, filter, chunk) {
3100
+ const steps = searchSteps(state, query, k, options, filter);
3101
+ let graphResults = null;
3102
+ let scan = null;
3103
+ let step = steps.next();
3104
+ while (step.done !== true) {
3105
+ graphResults = step.value.graphResults;
3106
+ scan = await scanInTurns(state, query, k + SEARCH_REFILL_RESERVE, options, step.value, chunk);
3107
+ step = steps.next({ results: scan.kept.slice(0, k), matched: scan.matched });
3108
+ }
3109
+ return { outcome: step.value, graphResults, scan };
3110
+ }
3111
+ function rescoredSinceTheScan(state, query, k, options, filter, watch) {
3112
+ const touched = new Set(watch.written);
3113
+ for (const docId of watch.leftBuffer) touched.add(docId);
3114
+ return bruteForceSearch(state, query, k, options.metric, options.minSimilarity, touched, filter).results;
3115
+ }
3116
+ function outrunsEveryDiscard(results, k, bestDiscardedScore) {
3117
+ if (bestDiscardedScore === Number.NEGATIVE_INFINITY) return true;
3118
+ const lowest = results[k - 1];
3119
+ return lowest !== void 0 && lowest.score > bestDiscardedScore;
3120
+ }
3121
+ function settledOutcome(state, query, k, options, pass, watch) {
3122
+ const { written, leftBuffer } = watch;
3123
+ const { scan, graphResults } = pass;
3124
+ if (scan === null || written.size === 0 && leftBuffer.size === 0) return pass.outcome;
3125
+ if (graphResults === null) return written.size === 0 ? pass.outcome : null;
3126
+ const filter = filterForOptions(state, options);
3127
+ const graphTouched = graphResults.some((result) => written.has(result.docId));
3128
+ const graphPart = graphTouched ? graphResultsOf(state, query, k, options, filter) : graphResults;
3129
+ const survivors = scan.kept.filter((result) => !written.has(result.docId));
3130
+ const rescored = rescoredSinceTheScan(state, query, k, options, filter, watch);
3131
+ const results = mergeResults(graphPart, mergeResults(survivors, rescored, k), k);
3132
+ const refilled = survivors.length < scan.kept.length;
3133
+ if (refilled && !outrunsEveryDiscard(results, k, scan.bestDiscardedScore)) return null;
3134
+ return fetchedOutcome(state, results, options.minSimilarity, filter);
3135
+ }
3136
+ function attemptInTurns(state, query, k, options, filter, chunk) {
3137
+ const generation = liveSize(state) === 0 ? null : layoutGeneration(state);
3138
+ return watchingSearch(state, async (watch) => {
3139
+ const pass = await passInTurns(state, query, k, options, filter, chunk);
3140
+ if (state.disposed) return pass.outcome;
3141
+ if (generation !== null && layoutGeneration(state) !== generation) return null;
3142
+ return settledOutcome(state, query, k, options, pass, watch);
3143
+ });
3144
+ }
3145
+ async function searchWithFilterInTurns(state, query, k, options, filter) {
3146
+ let current = filter;
3147
+ const chunk = { docIds: [], ordinals: new Int32Array(SEARCH_SCAN_CHUNK_VECTORS) };
3148
+ for (let attempt = 1; ; attempt++) {
3149
+ const search2 = () => attemptInTurns(state, query, k, options, current, chunk);
3150
+ const outcome = attempt <= SEARCH_ATTEMPTS_BEFORE_HOLDING_WRITES ? await search2() : await holdWritesAndBackgroundWork(search2);
3151
+ if (outcome !== null) return outcome;
3152
+ current = filterForOptions(state, options);
3153
+ }
3154
+ }
3155
+ function searchFiltered(state, query, k, options, filter) {
3156
+ if (turnsAreBudgeted) return searchWithFilterInTurns(state, query, k, options, filter);
3157
+ return searchWithFilter(state, query, k, options, filter);
3158
+ }
3159
+
3160
+ // src/vector/vector-index/search-off-thread.ts
3161
+ function currentFilter(search2) {
3162
+ const { state, options, filter, confinedAtRevision } = search2;
3163
+ if (confinedAtRevision === null || state.revision === confinedAtRevision) return filter;
3164
+ return filterForOptions(state, options);
3165
+ }
3166
+ async function scoredChunk(search2, metric, ordinals, count) {
3167
+ const store = nativeStoreFor(search2.state.store.handles);
3168
+ if (store === null) return null;
3169
+ try {
3170
+ const code = nativeMetricCode(metric);
3171
+ const pin = search2.admission.pin;
3172
+ const scored = await withNativeThreadSlot(
3173
+ (slot) => search2.core.scoreAsync(store.handle, search2.query, code, ordinals, count, pin, slot)
3174
+ );
3175
+ return scored.status === NATIVE_OK ? scored.distances : null;
3176
+ } catch {
3177
+ return null;
3178
+ }
3179
+ }
3180
+ async function scanOffThread(search2, candidates) {
3181
+ const { state, k, options } = search2;
3182
+ const { metric, minSimilarity } = options;
3183
+ const heap = createBoundedMaxHeap(highScoreFirst, k);
3184
+ let matched = 0;
3185
+ const docIds = [];
3186
+ const ordinals = new Int32Array(NATIVE_THREAD_SCAN_CHUNK_ORDINALS);
3187
+ const remaining = candidates[Symbol.iterator]();
3188
+ let exhausted = false;
3189
+ while (!exhausted) {
3190
+ docIds.length = 0;
3191
+ while (docIds.length < NATIVE_THREAD_SCAN_CHUNK_ORDINALS) {
3192
+ if (turnIsSpent()) await yieldTurn();
3193
+ const next = remaining.next();
3194
+ if (next.done === true) {
3195
+ exhausted = true;
3196
+ break;
3197
+ }
3198
+ if (state.tombstones.has(next.value)) continue;
3199
+ const ordinal = state.store.getOrdinal(next.value);
3200
+ if (ordinal === void 0) continue;
3201
+ ordinals[docIds.length] = ordinal;
3202
+ docIds.push(next.value);
3203
+ }
3204
+ if (docIds.length === 0) break;
3205
+ const distances = await scoredChunk(search2, metric, ordinals, docIds.length);
3206
+ if (distances === null) return null;
3207
+ for (let i = 0; i < docIds.length; i++) {
3208
+ if (distances[i] === Number.POSITIVE_INFINITY) continue;
3209
+ const score = toScore(distances[i], metric);
3210
+ if (score < minSimilarity) continue;
3211
+ matched++;
3212
+ heap.push({ docId: docIds[i], score });
3213
+ }
3214
+ }
3215
+ return { results: heap.toSortedArray().reverse(), matched };
3216
+ }
3217
+ function scansEveryCandidate(state, filter) {
3218
+ if (state.hnsw === null) return true;
3219
+ if (filter === void 0) return false;
3220
+ const graphSize = state.hnsw.size;
3221
+ const selectivity = graphSize > 0 ? filter.count / graphSize : 1;
3222
+ return selectivity < state.filterThreshold;
3223
+ }
3224
+ async function outcomeOffThread(search2) {
3225
+ const { state, query, k, options, filter } = search2;
3226
+ const { metric, minSimilarity, efSearch, oversample, rescoreWindow } = options;
3227
+ const graph = state.hnsw;
3228
+ if (graph === null || scansEveryCandidate(state, filter)) {
3229
+ const candidates = filter ? filteredDocIds(state, filter) : allLiveDocIds(state);
3230
+ const scan2 = await scanOffThread(search2, candidates);
3231
+ return scan2 === null ? null : { results: scan2.results, matched: scan2.matched, matchedExact: true };
3232
+ }
3233
+ const buffered = state.buffer.size === 0 ? null : [...state.buffer];
3234
+ const hits = await graph.searchOffThread(query, k, metric, minSimilarity, {
3235
+ filter,
3236
+ efSearch,
3237
+ oversample,
3238
+ rescoreWindow,
3239
+ admission: search2.admission
3240
+ });
3241
+ if (hits === null) return null;
3242
+ if (buffered === null) return fetchedOutcome(state, hits, minSimilarity, currentFilter(search2));
3243
+ const scan = await scanOffThread(search2, bufferCandidates(state, buffered, filter));
3244
+ if (scan === null) return null;
3245
+ return fetchedOutcome(state, mergeResults(hits, scan.results, k), minSimilarity, currentFilter(search2));
3246
+ }
3247
+ async function searchOnNativeThreads(search2) {
3248
+ const { state, core, query, k, options, filter, confinedAtRevision } = search2;
3249
+ if (query.length !== state.dimension) {
3250
+ throw new NarsilError(
3251
+ ErrorCodes.VECTOR_DIMENSION_MISMATCH,
3252
+ `Vector dimension mismatch: expected ${state.dimension}, got ${query.length}`,
3253
+ { expected: state.dimension, received: query.length }
3254
+ );
3255
+ }
3256
+ if (liveSize(state) === 0) return { results: [], matched: 0, matchedExact: true };
3257
+ if (filter && filter.count === 0) return { results: [], matched: 0, matchedExact: true };
3258
+ if (k <= 0) return fetchedOutcome(state, [], options.minSimilarity, filter);
3259
+ if (state.buffer.size > 0 && !state.building && !state.buildScheduled) scheduleBuild(state);
3260
+ const generation = layoutGeneration(state);
3261
+ const settled = await watchingSearch(state, async (watch) => {
3262
+ const view = state.store.view;
3263
+ const pins = storePinsOf(view);
3264
+ const pin = pins.take();
3265
+ try {
3266
+ const outcome = await outcomeOffThread({
3267
+ state,
3268
+ core,
3269
+ query,
3270
+ k,
3271
+ options,
3272
+ filter,
3273
+ confinedAtRevision,
3274
+ admission: admissionAt(view, pin)
3275
+ });
3276
+ if (outcome === null || watch.written.size > 0 || layoutGeneration(state) !== generation) return null;
3277
+ return outcome;
3278
+ } finally {
3279
+ pins.release(pin);
3280
+ }
3281
+ });
3282
+ return settled ?? searchFiltered(state, query, k, options, filterForOptions(state, options));
3283
+ }
3284
+ function searchOffThread(state, query, k, options, filter, confinedAtRevision) {
3285
+ const core = threadedNativeCore();
3286
+ if (core === null) return null;
3287
+ return searchOnNativeThreads({ state, core, query, k, options, filter, confinedAtRevision });
3288
+ }
3289
+
3290
+ // src/vector/vector-index/writes.ts
3291
+ function createVectorWrites(state) {
3292
+ function validateDimension(vector) {
3293
+ if (vector.length !== state.dimension) {
3294
+ throw new NarsilError(
3295
+ ErrorCodes.VECTOR_DIMENSION_MISMATCH,
3296
+ `Vector dimension mismatch: expected ${state.dimension}, got ${vector.length}`,
3297
+ { expected: state.dimension, received: vector.length }
3298
+ );
3299
+ }
3300
+ }
3301
+ function heldVectorOrUndefinedWhereItsFileFailsToRead(ordinal) {
3302
+ try {
3303
+ return state.store.entryForOrdinal(ordinal)?.vector;
3304
+ } catch {
3305
+ return void 0;
3306
+ }
3307
+ }
3308
+ function holdsThisVectorAlready(docId, ordinal, vector) {
3309
+ if (state.tombstones.has(docId)) return false;
3310
+ const held = heldVectorOrUndefinedWhereItsFileFailsToRead(ordinal);
3311
+ return held !== void 0 && sameVector(held, vector);
3312
+ }
3313
+ function forgetRetiredFlags() {
3314
+ const oldestPin = state.store.oldestPin();
3315
+ state.buffer.forgetRetired((ordinal) => state.store.mayFreeOrdinal(ordinal, oldestPin));
3316
+ }
3317
+ function applyInsert(docId, vector, partitionId) {
3318
+ handOverBuildTurns(state);
3319
+ const previous = state.store.getOrdinal(docId);
3320
+ if (previous !== void 0 && holdsThisVectorAlready(docId, previous, vector)) {
3321
+ if (partitionId !== void 0 && state.store.partitionOfOrdinal(previous) !== partitionId) {
3322
+ noteWrite(state);
3323
+ noteSearchedDocWritten(state, docId);
3324
+ state.store.setPartition(docId, partitionId);
3325
+ }
3326
+ return;
3327
+ }
3328
+ state.store.requireRoomFor(docId);
3329
+ noteWrite(state);
3330
+ noteSearchedDocWritten(state, docId);
3331
+ state.tombstones.delete(docId);
3332
+ const graphHoldsPrevious = previous !== void 0 && graphsHoldNode(previous);
3333
+ if (previous !== void 0) keepRetiredOrdinal(state, docId, previous);
3334
+ state.store.insert(docId, vector, partitionId, graphHoldsPrevious);
3335
+ if (previous !== void 0) retireFromGraphs(previous, graphHoldsPrevious);
3336
+ finishPlacingInBuffer(docId);
3337
+ }
3338
+ function graphsHoldNode(ordinal) {
3339
+ return (state.hnsw?.holdsNode(ordinal) ?? false) || (state.freshGraph?.holdsNode(ordinal) ?? false) || (state.growingCopy?.holdsNode(ordinal) ?? false);
3340
+ }
3341
+ function retireFromGraphs(previous, graphHoldsPrevious) {
3342
+ const graph = state.hnsw;
3343
+ const freshGraph = state.freshGraph;
3344
+ const growingCopy = state.growingCopy;
3345
+ afterGraphPass(state, () => {
3346
+ graph?.markTombstoneOrdinal(previous);
3347
+ freshGraph?.markTombstoneOrdinal(previous);
3348
+ growingCopy?.markTombstoneOrdinal(previous);
3349
+ });
3350
+ if (!graphHoldsPrevious) state.osq?.removeOrdinal(previous);
3351
+ }
3352
+ function finishPlacingInBuffer(docId) {
3353
+ state.buffer.add(docId);
3354
+ if (state.buffer.retiredCount !== 0) forgetRetiredFlags();
3355
+ state.pendingLocations.delete(docId);
3356
+ if (!threadsHoldCurrentLayout(state)) scheduleWorkerCopyLoad(state);
3357
+ }
3358
+ function keptOrdinalHolding(docId, vector) {
3359
+ const kept = keptOrdinalsOf(state, docId);
3360
+ for (let at = kept.length - 1; at >= 0; at--) {
3361
+ const held = heldVectorOrUndefinedWhereItsFileFailsToRead(kept[at]);
3362
+ if (held !== void 0 && sameVector(held, vector)) return kept[at];
3363
+ }
3364
+ return void 0;
3365
+ }
3366
+ function applyRestore(docId, vector, partitionId) {
3367
+ const ordinal = keptOrdinalHolding(docId, vector);
3368
+ if (ordinal === void 0) return false;
3369
+ handOverBuildTurns(state);
3370
+ const current = state.store.getOrdinal(docId);
3371
+ if (current === ordinal && !state.tombstones.has(docId)) return true;
3372
+ const previous = current === ordinal ? void 0 : current;
3373
+ const graphHoldsPrevious = previous !== void 0 && graphsHoldNode(previous);
3374
+ if (!state.store.reclaim(docId, ordinal, partitionId, graphHoldsPrevious)) return false;
3375
+ forgetKeptOrdinal(state, docId, ordinal);
3376
+ noteWrite(state);
3377
+ noteSearchedDocWritten(state, docId);
3378
+ state.tombstones.delete(docId);
3379
+ if (previous !== void 0) retireFromGraphs(previous, graphHoldsPrevious);
3380
+ const graph = state.hnsw;
3381
+ const freshGraph = state.freshGraph;
3382
+ const growingCopy = state.growingCopy;
3383
+ afterGraphPass(state, () => {
3384
+ graph?.reviveOrdinal(ordinal);
3385
+ freshGraph?.reviveOrdinal(ordinal);
3386
+ growingCopy?.reviveOrdinal(ordinal);
3387
+ });
3388
+ finishPlacingInBuffer(docId);
3389
+ return true;
3390
+ }
3391
+ function applyRemove(docId) {
3392
+ handOverBuildTurns(state);
3393
+ const ordinal = state.store.getOrdinal(docId);
3394
+ if (ordinal === void 0) return;
3395
+ noteWrite(state);
3396
+ noteSearchedDocWritten(state, docId);
3397
+ state.tombstones.add(docId);
3398
+ keepRetiredOrdinal(state, docId, ordinal);
3399
+ state.store.retire(ordinal);
3400
+ state.buffer.retire(docId);
3401
+ if (state.buffer.retiredCount !== 0) forgetRetiredFlags();
3402
+ state.pendingLocations.delete(docId);
3403
+ const graph = state.hnsw;
3404
+ const growingCopy = state.growingCopy;
3405
+ afterGraphPass(state, () => {
3406
+ graph?.markTombstoneOrdinal(ordinal);
3407
+ growingCopy?.markTombstoneOrdinal(ordinal);
3408
+ });
3409
+ }
3410
+ return {
3411
+ insert(docId, vector, partitionId) {
3412
+ validateDimension(vector);
3413
+ for (const listener of state.writeListeners) listener(docId);
3414
+ const hold = state.cold;
3415
+ if (hold === null) {
3416
+ applyInsert(docId, vector, partitionId);
3417
+ return;
3418
+ }
3419
+ const held = vector.slice();
3420
+ const keepsReplacedCopy = state.rollbackHolds.has(docId);
3421
+ if (coldInsert(hold, docId, held, partitionId, keepsReplacedCopy, () => applyInsert(docId, held, partitionId))) {
3422
+ noteWrite(state);
3423
+ }
3424
+ },
3425
+ restore(docId, vector, partitionId) {
3426
+ validateDimension(vector);
3427
+ for (const listener of state.writeListeners) listener(docId);
3428
+ const hold = state.cold;
3429
+ if (hold === null) {
3430
+ if (!applyRestore(docId, vector, partitionId)) applyInsert(docId, vector, partitionId);
3431
+ return;
3432
+ }
3433
+ if (coldRestore(hold, docId, vector)) {
3434
+ noteWrite(state);
3435
+ return;
3436
+ }
3437
+ const held = vector.slice();
3438
+ if (coldInsert(hold, docId, held, partitionId, false, () => applyInsert(docId, held, partitionId))) {
3439
+ noteWrite(state);
3440
+ }
3441
+ },
3442
+ requireRoomForNewVector(docId) {
3443
+ if (state.cold === null) state.store.requireRoomFor(docId);
3444
+ else requireRoomInHold(state.cold, docId);
3445
+ },
3446
+ remove(docId) {
3447
+ for (const listener of state.writeListeners) listener(docId);
3448
+ const hold = state.cold;
3449
+ if (hold === null) {
3450
+ applyRemove(docId);
3451
+ return;
3452
+ }
3453
+ if (coldRemove(hold, docId, () => applyRemove(docId))) noteWrite(state);
3454
+ }
3455
+ };
3456
+ }
3457
+
3458
+ // src/vector/vector-index/index.ts
3459
+ function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_WORKER_COPIES_ALLOWED, indexName = "", storage = "memory", onLoadedGraphRepaired = () => void 0) {
3460
+ if (!Number.isInteger(dimension) || dimension <= 0) {
3461
+ throw new NarsilError(
3462
+ ErrorCodes.VECTOR_DIMENSION_MISMATCH,
3463
+ `Vector dimension must be a positive integer, got ${dimension}`,
3464
+ { dimension }
3465
+ );
3466
+ }
3467
+ const promotionThreshold = config?.threshold ?? DEFAULT_PROMOTION_THRESHOLD;
3468
+ const filterThreshold = Math.max(0, Math.min(1, config?.filterThreshold ?? DEFAULT_FILTER_THRESHOLD));
3469
+ const quantizationMode = config?.quantization ?? defaultQuantizationFor(dimension);
3470
+ const codeBits = osqBitsOf(quantizationMode);
3471
+ const metric = config?.hnswConfig?.metric ?? DEFAULT_VECTOR_METRIC;
3472
+ const rawHnswM = config?.hnswConfig?.m;
3473
+ const hnswConfig = config?.hnswConfig ? { ...config.hnswConfig, m: rawHnswM !== void 0 ? Math.max(rawHnswM, 2) : void 0 } : void 0;
3474
+ const dimensionScale = dimension / 256;
3475
+ const store = createVectorStore({
3476
+ dimension,
3477
+ codeBits,
3478
+ ...storage === "disk" ? { blockBytes: DISK_STORAGE_BLOCK_BYTES } : {}
3479
+ });
3480
+ const state = {
3481
+ indexName,
3482
+ fieldName,
3483
+ dimension,
3484
+ dimensionScale,
3485
+ promotionThreshold,
3486
+ filterThreshold,
3487
+ quantizationMode,
3488
+ storage,
3489
+ metric,
3490
+ hnswConfig,
3491
+ workerCopies,
3492
+ store,
3493
+ tombstones: createRemovedDocuments(store),
3494
+ buffer: createUnplacedVectors(store),
3495
+ pendingLocations: /* @__PURE__ */ new Map(),
3496
+ savedFiles: [],
3497
+ savedSignature: null,
3498
+ osq: codeBits === null ? null : createOsqQuantizer(dimension, codeBits, metric, store),
3499
+ hnsw: null,
3500
+ freshGraph: null,
3501
+ growingCopy: null,
3502
+ ...noBuildHistory(),
3503
+ rebuildChargeBytes: 0,
3504
+ waitingForMemory: false,
3505
+ rebuildWaitTimer: null,
3506
+ onLoadedGraphRepaired,
3507
+ compactedNodeCount: 0,
3508
+ building: false,
3509
+ buildScheduled: false,
3510
+ scheduledBuild: null,
3511
+ buildTimer: null,
3512
+ buildRetryDelayMs: 0,
3513
+ buildTurnWaiters: /* @__PURE__ */ new Map(),
3514
+ pendingBuild: null,
3515
+ buildsKeptOut: null,
3516
+ disposed: false,
3517
+ revision: 0,
3518
+ workerCopyPool: null,
3519
+ workerCopyHandle: null,
3520
+ workerCopyRevision: -1,
3521
+ workerCopyMode: null,
3522
+ workerCopyLoading: false,
3523
+ workerCopiesHeld: false,
3524
+ sharedHandles: /* @__PURE__ */ new Map(),
3525
+ sharedLayoutRevision: 0,
3526
+ sharing: Promise.resolve(),
3527
+ releaseToFilesInFlight: Promise.resolve(),
3528
+ cold: null,
3529
+ graphsHeldOffThread: null,
3530
+ graphsPlacedIntoSinceConnect: /* @__PURE__ */ new WeakSet(),
3531
+ searchWatches: /* @__PURE__ */ new Set(),
3532
+ writeListeners: /* @__PURE__ */ new Set(),
3533
+ rollbackHolds: /* @__PURE__ */ new Map()
3534
+ };
3535
+ const { insert, remove, requireRoomForNewVector, restore: restoreVector } = createVectorWrites(state);
3536
+ function getVector(docId) {
3537
+ if (state.cold !== null) return coldVectorOf(state.cold, docId);
3538
+ if (state.tombstones.has(docId)) return null;
3539
+ const entry = state.store.get(docId);
3540
+ if (!entry) return null;
3541
+ return new Float32Array(entry.vector);
3542
+ }
3543
+ function requireRoomFor(docId, vector) {
3544
+ try {
3545
+ requireRoomForNewVector(docId);
3546
+ } catch (error) {
3547
+ const held = getVector(docId);
3548
+ if (held === null || held.length !== vector.length || !sameVector(held, vector)) throw error;
3549
+ }
3550
+ }
3551
+ function restore(docId, vector, partitionId) {
3552
+ const held = getVector(docId);
3553
+ if (held !== null && held.length === vector.length && sameVector(held, vector)) return;
3554
+ restoreVector(docId, vector, partitionId);
3555
+ }
3556
+ function has(docId) {
3557
+ if (state.cold !== null) return coldHas(state.cold, docId);
3558
+ return state.store.has(docId) && !state.tombstones.has(docId);
3559
+ }
3560
+ const whenLoaded = () => untilColdLoaded(state);
3561
+ const coldLoadsInFlight = /* @__PURE__ */ new Set();
3562
+ function openColdFieldTracked(field, load) {
3563
+ const settled = openColdField(state, field, load);
3564
+ coldLoadsInFlight.add(settled);
3565
+ void settled.then(() => coldLoadsInFlight.delete(settled));
3566
+ }
3567
+ async function searchParallel(query, k, options) {
3568
+ if (state.graphsHeldOffThread !== null) await untilGraphReleased(state);
3569
+ if (state.cold !== null) return coldSearchConcurrently(state, state.cold, query, k, options);
3570
+ handOverBuildTurns(state);
3571
+ const confined = options.filterSubsets !== void 0 || options.filterPartitions !== void 0;
3572
+ let filter = filterForOptions(state, options);
3573
+ const offThread = searchOffThread(state, query, k, options, filter, confined ? state.revision : null);
3574
+ if (offThread !== null) return offThread;
3575
+ if (filter !== void 0) {
3576
+ if (state.hnsw) {
3577
+ const hnswLiveSize = state.hnsw.size;
3578
+ const selectivity = hnswLiveSize > 0 ? filter.count / hnswLiveSize : 1;
3579
+ if (selectivity < state.filterThreshold) return searchFiltered(state, query, k, options, filter);
3580
+ }
3581
+ }
3582
+ const filterRevision = state.revision;
3583
+ scheduleWorkerCopyLoad(state);
3584
+ const searchesWorkerCopies = state.workerCopyPool !== null && state.workerCopyHandle !== null;
3585
+ const buffered = state.buffer.size === 0 || !searchesWorkerCopies ? null : [...state.buffer];
3586
+ const viaWorkerCopy = await searchViaWorkerCopies(state, query, k, options.metric, options.minSimilarity, {
3587
+ filter,
3588
+ efSearch: options.efSearch,
3589
+ oversample: options.oversample,
3590
+ rescoreWindow: options.rescoreWindow
3591
+ });
3592
+ if (confined && state.revision !== filterRevision) {
3593
+ filter = filterForOptions(state, options);
3594
+ }
3595
+ if (viaWorkerCopy !== null) {
3596
+ const results = buffered === null ? viaWorkerCopy : withBufferedVectors(state, query, k, options, filter, viaWorkerCopy, buffered);
3597
+ return fetchedOutcome(state, results, options.minSimilarity, filter);
3598
+ }
3599
+ return searchFiltered(state, query, k, options, filter);
3600
+ }
3601
+ return {
3602
+ ...vectorPublicationSource(state),
3603
+ get size() {
3604
+ return liveSize(state);
3605
+ },
3606
+ get dimension() {
3607
+ return state.dimension;
3608
+ },
3609
+ get fieldName() {
3610
+ return state.fieldName;
3611
+ },
3612
+ get quantization() {
3613
+ return state.quantizationMode;
3614
+ },
3615
+ get storage() {
3616
+ return state.storage;
3617
+ },
3618
+ get metric() {
3619
+ return state.metric;
3620
+ },
3621
+ insert,
3622
+ requireRoomFor,
3623
+ restore,
3624
+ holdForRollback: (docId) => holdForRollback(state, docId),
3625
+ remove,
3626
+ partitionsKnown: () => state.cold !== null || state.store.partitionsKnown,
3627
+ assignPartitions: (resolve) => {
3628
+ if (state.cold === null) assignStorePartitions(state, resolve);
3629
+ },
3630
+ scheduleBuild: () => {
3631
+ if (state.cold === null) scheduleBuild(state);
3632
+ },
3633
+ awaitPendingBuild: async () => {
3634
+ await whenLoaded();
3635
+ await awaitBuilds(state);
3636
+ },
3637
+ holdGraphComplete: async () => {
3638
+ await whenLoaded();
3639
+ const letBuildsIn2 = await keepBuildsOut(state);
3640
+ try {
3641
+ await completeGraph(state);
3642
+ return letBuildsIn2;
3643
+ } catch (error) {
3644
+ letBuildsIn2();
3645
+ throw error;
3646
+ }
3647
+ },
3648
+ dispose: () => {
3649
+ cancelColdHold(state);
3650
+ return Promise.all([disposeIndex(state), ...coldLoadsInFlight]).then(closeIdleTurnHop);
3651
+ },
3652
+ search: withGraphFree(
3653
+ state,
3654
+ (query, k, options) => state.cold === null ? search(state, query, k, options) : coldSearch(state, state.cold, query, k, options)
3655
+ ),
3656
+ searchParallel,
3657
+ ...writeWatchesOf(state),
3658
+ refreshWorkerCopies: () => {
3659
+ if (state.cold === null) refreshWorkerCopies(state);
3660
+ },
3661
+ getVector,
3662
+ has,
3663
+ compact: withGraphFree(state, () => {
3664
+ if (state.cold !== null) return;
3665
+ noteWrite(state);
3666
+ compact(state);
3667
+ }),
3668
+ optimize: async () => {
3669
+ await whenLoaded();
3670
+ noteWrite(state);
3671
+ await optimize(state);
3672
+ },
3673
+ maintenanceStatus: () => ({ ...maintenanceStatus(state), loadingFromFiles: state.cold !== null }),
3674
+ estimateMemoryBytes: () => estimateMemoryBytes(state) + (state.cold?.field.residentBytes ?? 0),
3675
+ growthBytesFor: (vectors) => state.store.growthBytesFor(vectors),
3676
+ visitDocumentTables: (visit) => {
3677
+ state.store.visitDocumentTables(visit);
3678
+ visitFlaggedTables("removed", state.tombstones, visit);
3679
+ visitFlaggedTables("unplaced", state.buffer, visit);
3680
+ },
3681
+ deserialize: withGraphFree(state, (parts) => {
3682
+ cancelColdHold(state);
3683
+ invalidateWorkerCopies(state);
3684
+ deserialize(state, parts);
3685
+ }),
3686
+ planCheckpoint: withGraphFree(
3687
+ state,
3688
+ (listedKeys, rewritesGraph, pinsRetiredVectors) => {
3689
+ connectWhereAPassStoppedEarly(state);
3690
+ return planCheckpoint(state, listedKeys, rewritesGraph, pinsRetiredVectors);
3691
+ }
3692
+ ),
3693
+ recordCheckpoint: (plan, written) => recordCheckpoint(state, plan, written),
3694
+ restoreCheckpoint: withGraphFree(state, (shape) => {
3695
+ cancelColdHold(state);
3696
+ invalidateWorkerCopies(state);
3697
+ return beginCheckpointRestore(state, shape);
3698
+ }),
3699
+ openColdField: openColdFieldTracked,
3700
+ startShuffledRebuildWhenDue: () => {
3701
+ if (state.cold === null) startShuffledRebuild(state);
3702
+ },
3703
+ vectorFilesInUse: () => {
3704
+ const stored = state.store.dimension === 0 ? [] : state.store.handles.vectorFiles.filter((path) => path !== "");
3705
+ return state.cold === null ? stored : [...stored, ...state.cold.field.filePaths()];
3706
+ },
3707
+ adoptDiskLayout: onceGraphFree(state, (layout) => adoptDiskLayout(state, layout)),
3708
+ releaseVectorFiles: onceGraphFree(state, async () => {
3709
+ await whenLoaded();
3710
+ await withdrawWorkerCopies(state);
3711
+ state.store.closeFiles();
3712
+ }),
3713
+ relocateVectorFiles: (rewrite) => {
3714
+ state.store.relocateVectorFiles(rewrite);
3715
+ for (const [docId, location] of state.pendingLocations) {
3716
+ state.pendingLocations.set(docId, { ...location, path: rewrite(location.path) });
3717
+ }
3718
+ },
3719
+ holdWorkerCopies: async () => {
3720
+ await whenLoaded();
3721
+ await holdWorkerCopies(state);
3722
+ },
3723
+ resumeWorkerCopies: () => {
3724
+ state.workerCopiesHeld = false;
3725
+ }
3726
+ };
3727
+ }
3728
+
3729
+ export { compactVectors, createSharedVectorSearcher, createVectorIndex, executeHybridSearch, executeVectorSearch, filterSubsetsMatchNothing, getVectorMaintenanceStatus, optimizeVectors, requireUsableQueryVector, vectorLegWith };