@delali/narsil 0.3.1-next.145 → 0.3.1-next.149

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 (207) hide show
  1. package/dist/adapters/filesystem.mjs +2 -2
  2. package/dist/adapters/gcs.mjs +2 -2
  3. package/dist/adapters/indexeddb.mjs +1 -1
  4. package/dist/adapters/s3.mjs +2 -2
  5. package/dist/analysis-change-ZCJNYBJM.mjs +39 -0
  6. package/dist/{analysis-change-entry-V4SQQSXQ.mjs → analysis-change-entry-YN3ELL6C.mjs} +1 -1
  7. package/dist/browser/{analysis-change-QTCQ5ETU.mjs → analysis-change-M65AZQQG.mjs} +8 -5
  8. package/dist/browser/{analysis-change-entry-KYEQSP3I.mjs → analysis-change-entry-H3YIOLXZ.mjs} +1 -1
  9. package/dist/browser/{chunk-GP4QLUQM.mjs → chunk-2JABMB4S.mjs} +10 -6
  10. package/dist/browser/{chunk-5WYWLTM5.mjs → chunk-4DYLCICG.mjs} +2 -2
  11. package/dist/browser/{chunk-75PMIFQA.mjs → chunk-4RD3AFSB.mjs} +1 -1
  12. package/dist/browser/{chunk-XWMEGKDC.mjs → chunk-5V74FFZE.mjs} +2 -2
  13. package/dist/browser/{chunk-PMBSBSCN.mjs → chunk-6OWX3OP2.mjs} +2 -2
  14. package/dist/browser/chunk-BRWY3JCN.mjs +678 -0
  15. package/dist/browser/{chunk-ZALEKEUH.mjs → chunk-BVX2MEHA.mjs} +306 -607
  16. package/dist/browser/{chunk-FXPMF65P.mjs → chunk-D6UFGBF5.mjs} +15 -45
  17. package/dist/browser/{chunk-QNDWMPV2.mjs → chunk-IAXZAVYL.mjs} +25 -66
  18. package/dist/browser/chunk-JNOAKDUM.mjs +31 -0
  19. package/dist/browser/{chunk-BTMRV5MY.mjs → chunk-KAEVGLYN.mjs} +2 -2
  20. package/dist/browser/{chunk-FHO5N7FD.mjs → chunk-LURXVCHG.mjs} +1 -1
  21. package/dist/browser/chunk-MPK76GVS.mjs +63 -0
  22. package/dist/browser/{chunk-6OFS7RUN.mjs → chunk-NVEJJP4R.mjs} +1 -1
  23. package/dist/browser/{chunk-U77XQJOY.mjs → chunk-ODQP7P3H.mjs} +455 -66
  24. package/dist/browser/{chunk-IFGTRRPU.mjs → chunk-PIS7ZAP7.mjs} +3 -25
  25. package/dist/browser/{chunk-XIHLIMFX.mjs → chunk-QMNZFFD3.mjs} +4 -3
  26. package/dist/browser/{chunk-AOKLYVTT.mjs → chunk-SRL47USS.mjs} +2 -2
  27. package/dist/browser/chunk-TY7L6OXO.mjs +69 -0
  28. package/dist/browser/{chunk-LGJ6XMH5.mjs → chunk-W643QV3L.mjs} +6 -5
  29. package/dist/browser/chunk-WXLBZJMT.mjs +25 -0
  30. package/dist/browser/{chunk-ZKVDQ5ZH.mjs → chunk-XILIT5MT.mjs} +8 -8
  31. package/dist/browser/{cold-module-Y2IJAP7S.mjs → cold-module-A5S6JFDN.mjs} +20 -20
  32. package/dist/browser/{durability-wiring-MLXZ5QQF.mjs → durability-wiring-W5AVV3RF.mjs} +53 -30
  33. package/dist/browser/gzip-5S7ROJFJ.mjs +1 -0
  34. package/dist/browser/import-PTT63AGK.mjs +12 -0
  35. package/dist/browser/{module-5LMDGXQH.mjs → module-CEKA7MQH.mjs} +13 -10
  36. package/dist/browser/module-X5PGOE47.mjs +4288 -0
  37. package/dist/browser/{phrase-module-KXNRTWCS.mjs → phrase-module-CNKMUY4U.mjs} +2 -1
  38. package/dist/browser/pool-DTYSPBMQ.mjs +296 -0
  39. package/dist/{restore-queue-UAVGBSJG.mjs → browser/restore-queue-DD4LWZK7.mjs} +2 -2
  40. package/dist/browser/{runtime-ZNEGK33G.mjs → runtime-D2ULZGCN.mjs} +3 -2
  41. package/dist/browser/{snapshot-2EGS2FYU.mjs → snapshot-DQ6A4PQP.mjs} +18 -14
  42. package/dist/browser/{write-WMZA6NSF.mjs → write-MAGRAIEH.mjs} +14 -11
  43. package/dist/{capabilities-CNnzGtqP.d.ts → capabilities-BT-HLDC8.d.ts} +2 -2
  44. package/dist/{chunk-FRJ5RGX7.mjs → chunk-2HJWQPR6.mjs} +2 -2
  45. package/dist/{chunk-DKMZ7JFW.mjs → chunk-3BVGJ4JR.mjs} +10 -4
  46. package/dist/{chunk-QYMAYU7M.mjs → chunk-3HV7M5OJ.mjs} +155 -149
  47. package/dist/chunk-3MXV3VHR.mjs +46 -0
  48. package/dist/{chunk-6FOJKNUR.mjs → chunk-3SBJVQLL.mjs} +2 -2
  49. package/dist/{chunk-PRID7CA6.mjs → chunk-3TVDTACC.mjs} +1 -1
  50. package/dist/{chunk-RTVAWNUL.mjs → chunk-5WB2A3XU.mjs} +9 -6
  51. package/dist/{chunk-LWFK3TC2.mjs → chunk-65WCV5JO.mjs} +1 -1
  52. package/dist/{chunk-G6Q5DEF5.mjs → chunk-6BWLDLS3.mjs} +3 -46
  53. package/dist/{chunk-W6M4RRAT.mjs → chunk-6C5EX2QR.mjs} +3 -3
  54. package/dist/{chunk-HOC5JP2H.mjs → chunk-6F6USYII.mjs} +119 -11
  55. package/dist/{chunk-7523VCM4.mjs → chunk-7HKWO4BJ.mjs} +7 -7
  56. package/dist/{chunk-ONDEEBTA.mjs → chunk-7N223GWV.mjs} +2 -2
  57. package/dist/{chunk-UGQULMSU.mjs → chunk-ADBS5XRI.mjs} +10 -10
  58. package/dist/{chunk-MJTEU2XK.mjs → chunk-AWNHDBKV.mjs} +429 -73
  59. package/dist/{chunk-2E2ZFOPT.mjs → chunk-B2FTZNNF.mjs} +2 -2
  60. package/dist/{chunk-NWTEJ2T7.mjs → chunk-B7XIOAMK.mjs} +3 -3
  61. package/dist/{chunk-W55X2BYN.mjs → chunk-BEZH7VEH.mjs} +1 -1
  62. package/dist/chunk-BTPGQC6Z.mjs +27 -0
  63. package/dist/{chunk-KG5N7LE6.mjs → chunk-CYYSZPVV.mjs} +11 -11
  64. package/dist/{chunk-HTMUGEFU.mjs → chunk-DOY6EG76.mjs} +6 -6
  65. package/dist/{chunk-UHVTEVF5.mjs → chunk-F22IOCR2.mjs} +10 -6
  66. package/dist/{chunk-FTQLF5JI.mjs → chunk-IRWP524D.mjs} +1 -1
  67. package/dist/{chunk-XLAI4OIO.mjs → chunk-IZT3G24Q.mjs} +2 -2
  68. package/dist/{chunk-SE7MKJVK.mjs → chunk-J7M73VY7.mjs} +1 -1
  69. package/dist/chunk-KAKQ74LS.mjs +53 -0
  70. package/dist/{chunk-GVPYMSLO.mjs → chunk-KYVRZAYP.mjs} +1 -1
  71. package/dist/{chunk-F442VZUL.mjs → chunk-KZ5GQE4Z.mjs} +5 -5
  72. package/dist/{chunk-WDEWU46C.mjs → chunk-KZQIVGW7.mjs} +2 -2
  73. package/dist/{chunk-7AJAW6NN.mjs → chunk-L7TIAYPX.mjs} +8 -8
  74. package/dist/{chunk-GRFXSEFF.mjs → chunk-LDGGR7YT.mjs} +4 -81
  75. package/dist/{chunk-WMIV7PJ7.mjs → chunk-LJ4ENXOD.mjs} +15 -14
  76. package/dist/chunk-LQG5G2LY.mjs +39 -0
  77. package/dist/{chunk-IQHZMCLX.mjs → chunk-MDF6PDIA.mjs} +2 -0
  78. package/dist/{chunk-QXH2CS4V.mjs → chunk-NVTFQFUL.mjs} +7 -26
  79. package/dist/{chunk-5K2JHKM3.mjs → chunk-NYSONUB7.mjs} +1 -1
  80. package/dist/{chunk-OH7SX5MY.mjs → chunk-O4KUNDVF.mjs} +12 -10
  81. package/dist/{chunk-EBIHRL7B.mjs → chunk-Q2UL2TPC.mjs} +1 -1
  82. package/dist/{chunk-RNUP6WZ6.mjs → chunk-QEYF2SQ6.mjs} +2 -2
  83. package/dist/{chunk-RVBUQITB.mjs → chunk-QN5WVV5K.mjs} +7 -7
  84. package/dist/{chunk-RIIUZJQ3.mjs → chunk-QYRNLLLI.mjs} +1 -1
  85. package/dist/{chunk-BTARFTEN.mjs → chunk-SOUZMPME.mjs} +4 -4
  86. package/dist/{chunk-ISUOSACK.mjs → chunk-T4CJDBBE.mjs} +1 -1
  87. package/dist/{chunk-DHEJAGVW.mjs → chunk-TTCKT2RT.mjs} +5 -5
  88. package/dist/{chunk-QU2XF7VI.mjs → chunk-U2726TL5.mjs} +5 -4
  89. package/dist/{chunk-LYKUJNAV.mjs → chunk-U46IZNCW.mjs} +2 -2
  90. package/dist/{chunk-SNB4DYL3.mjs → chunk-U6NE6RCV.mjs} +4 -4
  91. package/dist/{chunk-SA52N7G3.mjs → chunk-VP6XRMWR.mjs} +4 -4
  92. package/dist/{chunk-YUCGFPMS.mjs → chunk-VQDZVWN3.mjs} +449 -405
  93. package/dist/{chunk-IWVEG5CX.mjs → chunk-XIHUI4KB.mjs} +2 -2
  94. package/dist/{chunk-IV2XTSS2.mjs → chunk-XKPNKR3V.mjs} +16 -46
  95. package/dist/{chunk-UQS5PAS5.mjs → chunk-YGQOPB2Z.mjs} +1 -1
  96. package/dist/{chunk-SVO6NV4D.mjs → chunk-YKEHQUSZ.mjs} +4 -4
  97. package/dist/{chunk-6Z22L5RN.mjs → chunk-ZAYWOHVG.mjs} +1 -1
  98. package/dist/{chunk-QLDLQW36.mjs → chunk-ZEKDO7YX.mjs} +2 -2
  99. package/dist/chunk-ZXNNR73R.mjs +31 -0
  100. package/dist/{chunk-VJS4JFBQ.mjs → chunk-ZXX5GZEX.mjs} +3 -1
  101. package/dist/{chunk-X5SBWARI.mjs → chunk-ZYKUD5NX.mjs} +9 -7
  102. package/dist/{client-DoYPHcUn.d.ts → client-Cn3gNxUU.d.ts} +3 -3
  103. package/dist/client.d.ts +5 -5
  104. package/dist/client.mjs +2 -0
  105. package/dist/{cold-module-NUD332YD.mjs → cold-module-2WWGBFJC.mjs} +36 -33
  106. package/dist/{config-CuUfls_H.d.ts → config-CfhH1IWQ.d.ts} +1 -1
  107. package/dist/{config-Dzs46im6.d.ts → config-aIsy7mcL.d.ts} +52 -1
  108. package/dist/direct-executor-QLVHMELJ.mjs +24 -0
  109. package/dist/distribution/coordinator/etcd.mjs +1 -1
  110. package/dist/distribution/transport/grpc.mjs +1 -1
  111. package/dist/distribution.d.ts +4 -4
  112. package/dist/distribution.mjs +150 -255
  113. package/dist/{durability-wiring-K7RDXWL6.mjs → durability-wiring-GNWQSZI3.mjs} +67 -42
  114. package/dist/embeddings/openai.mjs +2 -2
  115. package/dist/{engine-C0ifUEcm.d.ts → engine-CJRUHXgf.d.ts} +1 -1
  116. package/dist/gzip-F4HMIEEQ.mjs +1 -0
  117. package/dist/import-DX55PN4G.mjs +41 -0
  118. package/dist/index.browser.mjs +7 -4
  119. package/dist/index.d.ts +539 -5
  120. package/dist/index.mjs +22 -16
  121. package/dist/index.react-native.mjs +19 -0
  122. package/dist/{long-query-thread-FKVHRCHF.mjs → long-query-thread-SCUE62TO.mjs} +20 -17
  123. package/dist/{module-GJUKW5VU.mjs → module-HW6IXUM6.mjs} +601 -108
  124. package/dist/{module-TMFIWOD2.mjs → module-V5K4ZP23.mjs} +29 -23
  125. package/dist/{paged-partition-archive-WNUEMYWM.mjs → paged-partition-archive-FDIIWZNR.mjs} +31 -25
  126. package/dist/partial-words.mjs +5 -5
  127. package/dist/pattern.mjs +3 -3
  128. package/dist/persistence/durability/checkpoint-worker.mjs +28 -22
  129. package/dist/{phrase-module-OMY3QQV7.mjs → phrase-module-OUQIRUQU.mjs} +1 -1
  130. package/dist/pool-UPV5PVPY.mjs +298 -0
  131. package/dist/react-native/chunks/analysis-change-HGW3L6NY.mjs +893 -0
  132. package/dist/react-native/chunks/analysis-change-entry-BNRU6STO.mjs +1 -0
  133. package/dist/react-native/chunks/chunk-2J2727V4.mjs +495 -0
  134. package/dist/react-native/chunks/chunk-3CTWSQRW.mjs +1251 -0
  135. package/dist/react-native/chunks/chunk-5FV4ZEVA.mjs +2619 -0
  136. package/dist/react-native/chunks/chunk-7VWQLEFV.mjs +642 -0
  137. package/dist/react-native/chunks/chunk-AAAETEJK.mjs +27 -0
  138. package/dist/react-native/chunks/chunk-AM7RSBPF.mjs +32 -0
  139. package/dist/react-native/chunks/chunk-B6OD63Z3.mjs +997 -0
  140. package/dist/react-native/chunks/chunk-BRNAVOKP.mjs +177 -0
  141. package/dist/react-native/chunks/chunk-DKJTZ625.mjs +207 -0
  142. package/dist/react-native/chunks/chunk-FKGG4UOF.mjs +177 -0
  143. package/dist/{browser/chunk-K7UFXEJS.mjs → react-native/chunks/chunk-GV5KFQ6M.mjs} +12 -6
  144. package/dist/react-native/chunks/chunk-HURV3ASY.mjs +4416 -0
  145. package/dist/react-native/chunks/chunk-IO2SMUQD.mjs +10 -0
  146. package/dist/react-native/chunks/chunk-JV6VQY2S.mjs +17 -0
  147. package/dist/react-native/chunks/chunk-KUJOBRKS.mjs +217 -0
  148. package/dist/react-native/chunks/chunk-KWRW5U65.mjs +226 -0
  149. package/dist/react-native/chunks/chunk-L4M7QJNP.mjs +110 -0
  150. package/dist/react-native/chunks/chunk-LQ6Q4L5B.mjs +360 -0
  151. package/dist/react-native/chunks/chunk-LQAKO5QV.mjs +523 -0
  152. package/dist/react-native/chunks/chunk-LRGWK367.mjs +11 -0
  153. package/dist/react-native/chunks/chunk-LVIKKAA6.mjs +93 -0
  154. package/dist/react-native/chunks/chunk-NFGBOI46.mjs +14 -0
  155. package/dist/react-native/chunks/chunk-NNE4VDMM.mjs +8 -0
  156. package/dist/react-native/chunks/chunk-PPEPTUVV.mjs +56 -0
  157. package/dist/react-native/chunks/chunk-PSWKQVVT.mjs +9 -0
  158. package/dist/react-native/chunks/chunk-PU55IOG4.mjs +910 -0
  159. package/dist/react-native/chunks/chunk-QC4ZRBSX.mjs +270 -0
  160. package/dist/react-native/chunks/chunk-R4EEMYM6.mjs +21 -0
  161. package/dist/react-native/chunks/chunk-R4OPUZQ5.mjs +86 -0
  162. package/dist/react-native/chunks/chunk-SLGIAD6B.mjs +2634 -0
  163. package/dist/react-native/chunks/chunk-TJMSKBEM.mjs +25 -0
  164. package/dist/react-native/chunks/chunk-TLYD7IJ2.mjs +23 -0
  165. package/dist/react-native/chunks/chunk-UFNCLEQM.mjs +73 -0
  166. package/dist/react-native/chunks/chunk-YH7W556M.mjs +179 -0
  167. package/dist/react-native/chunks/chunk-YJSMEKRE.mjs +22385 -0
  168. package/dist/react-native/chunks/chunk-ZCKNBPQ4.mjs +79 -0
  169. package/dist/react-native/chunks/cold-module-TSFWLYWC.mjs +755 -0
  170. package/dist/react-native/chunks/durability-wiring-PZR6KOQK.mjs +4509 -0
  171. package/dist/react-native/chunks/gzip.react-native-7JWCLVOB.mjs +3 -0
  172. package/dist/react-native/chunks/import-VTODLPLO.mjs +22 -0
  173. package/dist/{browser/module-Q6VFGLMG.mjs → react-native/chunks/module-JBBH6PZH.mjs} +609 -144
  174. package/dist/react-native/chunks/module-VEK2KCKU.mjs +25 -0
  175. package/dist/react-native/chunks/phrase-module-76UAPPSG.mjs +329 -0
  176. package/dist/react-native/chunks/pool-CD4ANYUI.mjs +296 -0
  177. package/dist/react-native/chunks/release-D7ZTIANM.mjs +33 -0
  178. package/dist/react-native/chunks/replace-IMZUVUOE.mjs +57 -0
  179. package/dist/{browser/restore-queue-SOAHKZSQ.mjs → react-native/chunks/restore-queue-OI5EH7R3.mjs} +2 -2
  180. package/dist/react-native/chunks/runtime-3MCDLJAF.mjs +559 -0
  181. package/dist/react-native/chunks/snapshot-AUZMEV36.mjs +1205 -0
  182. package/dist/react-native/chunks/write-DDC2QBRJ.mjs +197 -0
  183. package/dist/react-native/partial-words.mjs +733 -0
  184. package/dist/react-native/pattern.mjs +2442 -0
  185. package/dist/react-native/synonyms.mjs +460 -0
  186. package/dist/react.d.ts +7 -7
  187. package/dist/react.mjs +2 -0
  188. package/dist/{rerank-ZZ5CHKLW.mjs → rerank-3W54N6P2.mjs} +4 -4
  189. package/dist/rerankers/http.mjs +2 -2
  190. package/dist/restore-queue-BQWWNNQI.mjs +41 -0
  191. package/dist/{runtime-RAOCKSYA.mjs → runtime-L2DZYY6N.mjs} +5 -5
  192. package/dist/server.d.ts +8 -8
  193. package/dist/server.mjs +32 -28
  194. package/dist/{snapshot-DB5ZVCDN.mjs → snapshot-EHXTOTW5.mjs} +39 -33
  195. package/dist/synonyms.mjs +3 -3
  196. package/dist/{tasks-ChWp0wrF.d.ts → tasks-C4ElXr4P.d.ts} +2 -2
  197. package/dist/thread-I7DVHHW7.mjs +31 -0
  198. package/dist/vector/search-worker.mjs +7 -5
  199. package/dist/workers/entry.mjs +9 -9
  200. package/dist/{write-KZVTLGCI.mjs → write-SDJXXQ2G.mjs} +29 -23
  201. package/package.json +88 -7
  202. package/dist/analysis-change-D5APH4Z3.mjs +0 -34
  203. package/dist/browser/import-FTMLTPGQ.mjs +0 -9
  204. package/dist/direct-executor-FDQYQEJQ.mjs +0 -22
  205. package/dist/import-6KXFY6XI.mjs +0 -35
  206. package/dist/thread-OV6T555H.mjs +0 -28
  207. /package/dist/{chunk-22YPZVMG.mjs → chunk-ZXNQEW43.mjs} +0 -0
@@ -1,30 +1,80 @@
1
1
  import { noteWaitingForMemory, admitsBackgroundBytes } from './chunk-PELHVTO7.mjs';
2
- import { VECTOR_WORKER_COPIES_ALLOWED, DEFAULT_PROMOTION_THRESHOLD, DEFAULT_FILTER_THRESHOLD, noBuildHistory, recordCheckpoint, planCheckpoint, deserialize, assignStorePartitions, liveSize, ordinalFilterForSubsets, handOverBuildTurns, filterForOptions, OSQ4_MIN_DIMENSION, emptyFieldBeforeRestore, ESTIMATED_MS_PER_TOMBSTONE, graphNeedsRebuild, ESTIMATED_MS_PER_VECTOR_REBUILD, releaseReplacedVectors, yieldBuildTurn, allLiveDocIds, threadsHoldCurrentLayout, WORKER_COPY_MIN_VECTORS, SHUFFLED_REBUILD_MEMORY_RETRY_MS, recalibrateFromStore, signatureOf, adoptGraph, BUILD_RETRY_MAX_DELAY_MS, BUILD_RETRY_FIRST_DELAY_MS, fieldHandlesOf, liveOrdinals, RECALIBRATION_CHUNK_VECTORS, BUILD_CHUNK_SIZE, DRIFT_CHECK_GROWTH_FACTOR, DISK_ADOPTION_YIELD_INTERVAL, adoptLoadedCalibration, RECALIBRATION_DRIFT_RATIO } from './chunk-DKMZ7JFW.mjs';
3
- import { chargeHeapBytes } from './chunk-IWVEG5CX.mjs';
4
- import { isDead, NO_LIVE_VECTOR_AT_POSITION, bytesToVectors } from './chunk-QLDLQW36.mjs';
5
- import './chunk-RIIUZJQ3.mjs';
6
- import { createVectorStore } from './chunk-WMIV7PJ7.mjs';
7
- export { openSharedVectorField } from './chunk-WMIV7PJ7.mjs';
2
+ import { VECTOR_WORKER_COPIES_ALLOWED, DEFAULT_PROMOTION_THRESHOLD, DEFAULT_FILTER_THRESHOLD, noBuildHistory, recordCheckpoint, assignStorePartitions, liveSize, ordinalFilterForSubsets, handOverBuildTurns, filterForOptions, OSQ4_MIN_DIMENSION, ESTIMATED_MS_PER_TOMBSTONE, graphNeedsRebuild, ESTIMATED_MS_PER_VECTOR_REBUILD, yieldBuildTurn, threadsHoldCurrentLayout, WORKER_COPY_MIN_VECTORS, SHUFFLED_REBUILD_MEMORY_RETRY_MS, emptyFieldBeforeRestore, SEARCH_ATTEMPTS_BEFORE_HOLDING_WRITES, recalibrateFromStore, signatureOf, BUILD_RETRY_MAX_DELAY_MS, BUILD_RETRY_FIRST_DELAY_MS, allLiveDocIds, fieldHandlesOf, releaseReplacedVectors, adoptGraph, BUILD_STEPS_BETWEEN_CLOCK_CHECKS, liveOrdinals, RECALIBRATION_CHUNK_VECTORS, BUILD_CHUNK_SIZE, SEARCH_REFILL_RESERVE, SEARCH_SCAN_CHUNK_VECTORS, DRIFT_CHECK_GROWTH_FACTOR, adoptLoadedCalibration, planCheckpoint, deserialize, RECALIBRATION_DRIFT_RATIO, DISK_ADOPTION_YIELD_INTERVAL } from './chunk-3BVGJ4JR.mjs';
3
+ import { NO_LIVE_VECTOR_AT_POSITION, bytesToVectors, isDead } from './chunk-ZEKDO7YX.mjs';
4
+ import { createVectorStore } from './chunk-LJ4ENXOD.mjs';
5
+ export { openSharedVectorField } from './chunk-LJ4ENXOD.mjs';
8
6
  import './chunk-FOCV4CBZ.mjs';
9
- import { osqBitsOf, createOsqQuantizer, STORE_ORDINAL_LAYOUT_GENERATION, ordinalFilterValues, STORE_UNPLACED_COUNT, STORE_LIVE_COUNT, STORE_REMOVED_COUNT, createOrdinalFilter, addToOrdinalFilter, nativeScoresOf, toScore, ordinalFilterHas, magnitude, nativeStoreScratchBytes, toDistance, euclideanDistance, dotProduct, cosineSimilarityWithMagnitudes, createHNSWIndex, topLayerInParts, sharedMemoryAvailable, requireWholeNeighbourLists, shuffleInPlace, createCentroidSums, centroidOfCentroidSums, meanOfCentroidSums, countOfCentroidSums, squaredLengthsOfCentroidSums, releaseLocksHeldBy } from './chunk-MJTEU2XK.mjs';
10
- export { releaseLocksHeldBy } from './chunk-MJTEU2XK.mjs';
7
+ import { osqBitsOf, createOsqQuantizer, STORE_ORDINAL_LAYOUT_GENERATION, ordinalFilterValues, STORE_UNPLACED_COUNT, STORE_LIVE_COUNT, STORE_REMOVED_COUNT, createOrdinalFilter, addToOrdinalFilter, nativeScoresOf, toScore, ordinalFilterHas, threadedNativeCore, nativeStoreScratchBytes, magnitude, toDistance, sharedMemoryAvailable, euclideanDistance, dotProduct, cosineSimilarityWithMagnitudes, createHNSWIndex, topLayerInParts, shuffleInPlace, requireWholeNeighbourLists, createCentroidSums, shuffleStep, centroidOfCentroidSums, meanOfCentroidSums, countOfCentroidSums, squaredLengthsOfCentroidSums, nativeStoreFor, withNativeThreadSlot, nativeMetricCode, releaseLocksHeldBy } from './chunk-AWNHDBKV.mjs';
8
+ export { releaseLocksHeldBy } from './chunk-AWNHDBKV.mjs';
11
9
  import './chunk-37CD3OKS.mjs';
12
10
  import { noteFallback } from './chunk-DJHHF2L5.mjs';
13
- import { spawnNodeWorker } from './chunk-W55X2BYN.mjs';
14
- import { resolveWorkerCount, resolveWorkerEntry, detectRuntime, workerResourceLimits } from './chunk-G6Q5DEF5.mjs';
11
+ import { spawnNodeWorker } from './chunk-BEZH7VEH.mjs';
12
+ import { resolveWorkerEntry, detectRuntime, workerResourceLimits } from './chunk-6BWLDLS3.mjs';
13
+ import { chargeHeapBytes } from './chunk-XIHUI4KB.mjs';
14
+ import './chunk-Q2UL2TPC.mjs';
15
+ import { resolveWorkerCount } from './chunk-3MXV3VHR.mjs';
15
16
  import './chunk-EEBCDP36.mjs';
16
- import './chunk-EBIHRL7B.mjs';
17
- import './chunk-22YPZVMG.mjs';
17
+ import './chunk-QYRNLLLI.mjs';
18
+ import './chunk-ZXNQEW43.mjs';
18
19
  import './chunk-KOKM4PQN.mjs';
19
- import { MAX_RENUMBERED_SEARCH_ATTEMPTS, DISK_STORAGE_BLOCK_BYTES, UNPLACED_FLAG_CHUNK_ORDINALS, GRAPH_BUILD_CHUNKS_IN_FLIGHT_PER_WORKER, GRAPH_BUILD_DISPATCH_CHUNK, VECTOR_SEARCH_TIMEOUT_MS, VECTOR_SEARCH_LOAD_TIMEOUT_MS, VECTOR_SCRATCH_SLOTS, VECTOR_INSERT_TIMEOUT_MS } from './chunk-QXH2CS4V.mjs';
20
+ import { NATIVE_OK } from './chunk-NVTFQFUL.mjs';
21
+ import { turnsAreBudgeted, yieldBackgroundTurn, createDocIdMap, holdWritesAndBackgroundWork, backgroundTurnIsSpent, turnIsSpent, yieldTurn } from './chunk-LQG5G2LY.mjs';
22
+ import { MAX_RENUMBERED_SEARCH_ATTEMPTS, DISK_STORAGE_BLOCK_BYTES, UNPLACED_FLAG_CHUNK_ORDINALS, GRAPH_BUILD_CHUNKS_IN_FLIGHT_PER_WORKER, GRAPH_BUILD_DISPATCH_CHUNK, NATIVE_THREAD_SCAN_CHUNK_ORDINALS, VECTOR_SEARCH_TIMEOUT_MS, VECTOR_SEARCH_LOAD_TIMEOUT_MS, VECTOR_SCRATCH_SLOTS, VECTOR_INSERT_TIMEOUT_MS } from './chunk-BTPGQC6Z.mjs';
20
23
  import { noteThreadSlotLive, noteThreadSlotGone } from './chunk-RFBLQLLZ.mjs';
21
24
  import './chunk-FQBFZAWG.mjs';
22
- import './chunk-VJS4JFBQ.mjs';
25
+ import './chunk-ZXX5GZEX.mjs';
23
26
  import './chunk-U476IXKQ.mjs';
24
- import { NarsilError, ErrorCodes, isNarsilError } from './chunk-IQHZMCLX.mjs';
27
+ import { NarsilError, ErrorCodes, isNarsilError } from './chunk-MDF6PDIA.mjs';
25
28
  import './chunk-R6A773WK.mjs';
26
29
  import { compareCodePoints } from './chunk-YTJCGKM4.mjs';
27
30
 
31
+ // src/engine/vector-maintenance.ts
32
+ function resolveVectorIndexes(manager, indexName, fieldName) {
33
+ const all = manager.getVectorIndexes();
34
+ if (fieldName === void 0) return all;
35
+ const idx = all.get(fieldName);
36
+ if (!idx) {
37
+ throw new NarsilError(ErrorCodes.INDEX_NOT_FOUND, `Vector field "${fieldName}" not found in index "${indexName}"`, {
38
+ indexName,
39
+ fieldName
40
+ });
41
+ }
42
+ const single = /* @__PURE__ */ new Map();
43
+ single.set(fieldName, idx);
44
+ return single;
45
+ }
46
+ function compactVectors(manager, indexName, fieldName) {
47
+ const indexes = resolveVectorIndexes(manager, indexName, fieldName);
48
+ for (const [, idx] of indexes) {
49
+ idx.compact();
50
+ }
51
+ }
52
+ async function optimizeVectors(manager, indexName, fieldName) {
53
+ const indexes = resolveVectorIndexes(manager, indexName, fieldName);
54
+ for (const [, idx] of indexes) {
55
+ await idx.optimize();
56
+ }
57
+ }
58
+ function getVectorMaintenanceStatus(manager) {
59
+ const indexes = manager.getVectorIndexes();
60
+ const results = [];
61
+ for (const [field, idx] of indexes) {
62
+ const status = idx.maintenanceStatus();
63
+ results.push({
64
+ fieldName: field,
65
+ tombstoneRatio: status.tombstoneRatio,
66
+ graphCount: status.graphCount,
67
+ bufferSize: status.bufferSize,
68
+ building: status.building,
69
+ waitingForMemory: status.waitingForMemory,
70
+ loadingFromFiles: status.loadingFromFiles === true,
71
+ estimatedCompactMs: status.estimatedCompactMs,
72
+ estimatedOptimizeMs: status.estimatedOptimizeMs
73
+ });
74
+ }
75
+ return results;
76
+ }
77
+
28
78
  // src/core/heap.ts
29
79
  function createMaxHeap(compare) {
30
80
  return createHeap((a, b) => compare(b, a));
@@ -234,10 +284,10 @@ function createSharedVectorSearcher(options) {
234
284
  }
235
285
  function ordinalSpace() {
236
286
  const store = view.store;
237
- const layoutGeneration = ordinalLayoutGeneration();
287
+ const layoutGeneration2 = ordinalLayoutGeneration();
238
288
  return {
239
289
  fieldName,
240
- layoutGeneration,
290
+ layoutGeneration: layoutGeneration2,
241
291
  slots: store.slots,
242
292
  header: store.handles.header,
243
293
  ordinalOf: (docId) => view.ordinalOf(docId),
@@ -348,7 +398,7 @@ function createSharedVectorSearcher(options) {
348
398
  );
349
399
  }
350
400
  for (let attempt = 1; ; attempt++) {
351
- const layoutGeneration = ordinalLayoutGeneration();
401
+ const layoutGeneration2 = ordinalLayoutGeneration();
352
402
  const outcome = search2(query, k, searchOptions, filterFor(searchOptions));
353
403
  if (view.graphRetired) {
354
404
  throw new NarsilError(
@@ -357,7 +407,7 @@ function createSharedVectorSearcher(options) {
357
407
  { fieldName }
358
408
  );
359
409
  }
360
- if (ordinalLayoutGeneration() === layoutGeneration) return Promise.resolve(answerOf(outcome));
410
+ if (ordinalLayoutGeneration() === layoutGeneration2) return Promise.resolve(answerOf(outcome));
361
411
  if (attempt >= MAX_RENUMBERED_SEARCH_ATTEMPTS) {
362
412
  throw new NarsilError(
363
413
  ErrorCodes.WORKER_BUSY,
@@ -370,6 +420,113 @@ function createSharedVectorSearcher(options) {
370
420
  };
371
421
  }
372
422
 
423
+ // src/vector/vector-index/graph-pass.ts
424
+ function holdGraph(state, graph) {
425
+ const current = state.graphsHeldOffThread;
426
+ if (current !== null) {
427
+ current.graphs.push(graph);
428
+ return current;
429
+ }
430
+ let release = () => void 0;
431
+ const released = new Promise((resolve) => {
432
+ release = resolve;
433
+ });
434
+ const held = { graphs: [graph], released, release, writesAfter: [] };
435
+ state.graphsHeldOffThread = held;
436
+ return held;
437
+ }
438
+ function applyWritesHeldBack(held) {
439
+ const writes = held.writesAfter.splice(0);
440
+ let firstError = null;
441
+ for (const write of writes) {
442
+ try {
443
+ write();
444
+ } catch (error) {
445
+ firstError ??= error;
446
+ }
447
+ }
448
+ if (firstError !== null) throw firstError;
449
+ }
450
+ function releaseGraph(state, held, graph) {
451
+ const at = held.graphs.indexOf(graph);
452
+ if (at !== -1) held.graphs.splice(at, 1);
453
+ if (held.graphs.length > 0) return;
454
+ if (state.graphsHeldOffThread === held) state.graphsHeldOffThread = null;
455
+ try {
456
+ applyWritesHeldBack(held);
457
+ } finally {
458
+ held.release();
459
+ }
460
+ }
461
+ async function connectGraph(state, graph) {
462
+ const offThread = graph.connectOffThread();
463
+ if (offThread === null) {
464
+ await graph.connectOnThisThread(turnsAreBudgeted);
465
+ return;
466
+ }
467
+ const held = holdGraph(state, graph);
468
+ let connected = false;
469
+ try {
470
+ connected = await offThread;
471
+ } finally {
472
+ releaseGraph(state, held, graph);
473
+ }
474
+ if (!connected && !state.disposed) await graph.connectOnThisThread(true);
475
+ }
476
+ async function untilGraphReleased(state) {
477
+ for (; ; ) {
478
+ const held = state.graphsHeldOffThread;
479
+ if (held === null) return;
480
+ await held.released;
481
+ }
482
+ }
483
+ function waitOutGraphPass(state) {
484
+ const held = state.graphsHeldOffThread;
485
+ if (held === null) return;
486
+ for (const graph of held.graphs) graph.exclusively(() => void 0);
487
+ applyWritesHeldBack(held);
488
+ }
489
+ function withGraphFree(state, operation) {
490
+ return (...args) => {
491
+ waitOutGraphPass(state);
492
+ return operation(...args);
493
+ };
494
+ }
495
+ function onceGraphFree(state, operation) {
496
+ return async (...args) => {
497
+ await untilGraphReleased(state);
498
+ return operation(...args);
499
+ };
500
+ }
501
+ function afterGraphPass(state, write) {
502
+ const held = state.graphsHeldOffThread;
503
+ if (held === null) write();
504
+ else held.writesAfter.push(write);
505
+ }
506
+
507
+ // src/vector/vector-index/search-writes.ts
508
+ function noteSearchedDocWritten(state, docId) {
509
+ for (const watch of state.searchWatches) watch.written.add(docId);
510
+ }
511
+ function moveOutOfBuffer(state, docId) {
512
+ if (!state.buffer.delete(docId)) return;
513
+ for (const watch of state.searchWatches) watch.leftBuffer.add(docId);
514
+ }
515
+ async function watchingSearch(state, search2) {
516
+ const watch = { written: /* @__PURE__ */ new Set(), leftBuffer: /* @__PURE__ */ new Set() };
517
+ state.searchWatches.add(watch);
518
+ try {
519
+ return await search2(watch);
520
+ } finally {
521
+ state.searchWatches.delete(watch);
522
+ }
523
+ }
524
+
525
+ // src/vector/search-pool/worker-entry.ts
526
+ function resolveWorkerEntryPoint() {
527
+ return resolveWorkerEntry(import.meta.url, /\/src\/vector\/search-pool\/[^/]+$/, "vector/search-worker.mjs");
528
+ }
529
+
373
530
  // src/vector/search-pool/constants.ts
374
531
  var VECTOR_POOL_DRAIN_TIMEOUT_MS = 1e4;
375
532
 
@@ -436,9 +593,6 @@ function rejectPending(slot, err) {
436
593
  }
437
594
 
438
595
  // src/vector/search-pool/spawn.ts
439
- function resolveWorkerEntryPoint() {
440
- return resolveWorkerEntry(import.meta.url, /\/src\/vector\/search-pool\/[^/]+$/, "vector/search-worker.mjs");
441
- }
442
596
  async function spawnWorker(entryPoint) {
443
597
  if (entryPoint === null) return null;
444
598
  const runtime = detectRuntime();
@@ -988,7 +1142,9 @@ async function insertIntoGraph(state, graph, docIds, admit) {
988
1142
  await flush();
989
1143
  await Promise.all(pending);
990
1144
  if (state.disposed) return false;
991
- graph.connectUnreached();
1145
+ if (turnsAreBudgeted) await connectGraph(state, graph);
1146
+ else graph.connectUnreached();
1147
+ if (state.disposed) return false;
992
1148
  noteWrite(state);
993
1149
  return true;
994
1150
  }
@@ -1018,7 +1174,7 @@ async function placeChunk(state, graph, dispatch, chunkDocIds, chunkOrdinals, fa
1018
1174
  continue;
1019
1175
  }
1020
1176
  if (!state.tombstones.has(docId) && !graph.has(docId) && graph.insertOrdinal(ordinal)) placedHere += 1;
1021
- if (graph === state.hnsw) state.buffer.delete(docId);
1177
+ if (graph === state.hnsw) moveOutOfBuffer(state, docId);
1022
1178
  }
1023
1179
  if (placedHere > 0 && outcome !== null && !fallback.shortAnswerWarned) {
1024
1180
  fallback.shortAnswerWarned = true;
@@ -1034,22 +1190,81 @@ async function insertLocally(state, graph, docIds, admit) {
1034
1190
  if (!admit(docId)) continue;
1035
1191
  const ordinal = state.store.getOrdinal(docId);
1036
1192
  if (ordinal === void 0) continue;
1037
- graph.insertOrdinal(ordinal);
1038
- if (graph === state.hnsw) state.buffer.delete(docId);
1193
+ if (turnsAreBudgeted) {
1194
+ graph.insertOrdinal(ordinal, true);
1195
+ if (graph.holdsUnlinkedNode) await connectGraph(state, graph);
1196
+ if (state.disposed) return false;
1197
+ } else {
1198
+ graph.insertOrdinal(ordinal);
1199
+ }
1200
+ if (graph === state.hnsw) moveOutOfBuffer(state, docId);
1039
1201
  count += 1;
1040
- if (count % BUILD_CHUNK_SIZE === 0) {
1202
+ if (turnsAreBudgeted) {
1203
+ if (!backgroundTurnIsSpent()) continue;
1204
+ noteWrite(state);
1205
+ await yieldBackgroundTurn();
1206
+ } else if (count % BUILD_CHUNK_SIZE === 0) {
1041
1207
  noteWrite(state);
1042
1208
  await yieldBuildTurn(state);
1043
1209
  }
1044
1210
  }
1045
1211
  if (state.disposed) return false;
1046
1212
  if (count > 0) {
1047
- graph.connectUnreached();
1213
+ if (turnsAreBudgeted) await connectGraph(state, graph);
1214
+ else graph.connectUnreached();
1048
1215
  noteWrite(state);
1049
1216
  }
1050
1217
  return true;
1051
1218
  }
1052
1219
 
1220
+ // src/vector/vector-index/turns.ts
1221
+ function turnDue(steps) {
1222
+ return steps % BUILD_STEPS_BETWEEN_CLOCK_CHECKS === 0 && backgroundTurnIsSpent();
1223
+ }
1224
+ async function forEachInTurns(items, visit, stillWanted) {
1225
+ let steps = 0;
1226
+ for (const item of items) {
1227
+ steps += 1;
1228
+ if (turnDue(steps)) {
1229
+ await yieldBackgroundTurn();
1230
+ if (!stillWanted()) return false;
1231
+ }
1232
+ visit(item);
1233
+ }
1234
+ return true;
1235
+ }
1236
+ async function copyInTurns(items, stillWanted) {
1237
+ const copied = [];
1238
+ const finished = await forEachInTurns(items, (item) => copied.push(item), stillWanted);
1239
+ return finished ? copied : null;
1240
+ }
1241
+ async function shuffledLiveDocIdsInTurns(state) {
1242
+ const stillWanted = () => !state.disposed;
1243
+ const docIds = await copyInTurns(allLiveDocIds(state), stillWanted);
1244
+ if (docIds === null) return null;
1245
+ let stream = docIds.length;
1246
+ for (let i = docIds.length - 1; i > 0; i--) {
1247
+ stream = shuffleStep(docIds, i, stream);
1248
+ if (turnDue(i)) {
1249
+ await yieldBackgroundTurn();
1250
+ if (!stillWanted()) return null;
1251
+ }
1252
+ }
1253
+ return docIds;
1254
+ }
1255
+ async function liveOrdinalsInTurns(state) {
1256
+ const ordinals = [];
1257
+ for (let ordinal = 0; ordinal < state.store.slots; ordinal++) {
1258
+ if (turnDue(ordinal + 1)) {
1259
+ await yieldBackgroundTurn();
1260
+ if (state.disposed) return null;
1261
+ }
1262
+ const docId = state.store.docIdForOrdinal(ordinal);
1263
+ if (docId !== void 0 && !state.tombstones.has(docId)) ordinals.push(ordinal);
1264
+ }
1265
+ return Int32Array.from(ordinals);
1266
+ }
1267
+
1053
1268
  // src/vector/vector-index/drift.ts
1054
1269
  function liveVectorMeanOf(sums) {
1055
1270
  const centroid = centroidOfCentroidSums(sums);
@@ -1068,7 +1283,8 @@ function liveVectorMeanOf(sums) {
1068
1283
  }
1069
1284
  async function measureLiveVectors(state, quantizer) {
1070
1285
  const sums = createCentroidSums(state.dimension, state.metric);
1071
- const ordinals = liveOrdinals(state);
1286
+ const ordinals = turnsAreBudgeted ? await liveOrdinalsInTurns(state) : liveOrdinals(state);
1287
+ if (ordinals === null) return null;
1072
1288
  for (let start = 0; start < ordinals.length; start += RECALIBRATION_CHUNK_VECTORS) {
1073
1289
  if (start > 0) await yieldBuildTurn(state);
1074
1290
  if (state.disposed || state.osq !== quantizer) return null;
@@ -1175,11 +1391,13 @@ async function shareFilesWithEveryThread(state, paths) {
1175
1391
  }
1176
1392
  async function releaseLocations(state, paths, locations) {
1177
1393
  const fileIndexes = await shareFilesWithEveryThread(state, paths);
1394
+ await untilGraphReleased(state);
1178
1395
  if (state.disposed) return;
1179
1396
  let count = 0;
1180
1397
  for (const [docId, location] of locations) {
1181
1398
  if (count > 0 && count % DISK_ADOPTION_YIELD_INTERVAL === 0) {
1182
1399
  await yieldBuildTurn(state);
1400
+ await untilGraphReleased(state);
1183
1401
  if (state.disposed) return;
1184
1402
  }
1185
1403
  count += 1;
@@ -1247,20 +1465,27 @@ function releaseMemoryCharges(state) {
1247
1465
  }
1248
1466
 
1249
1467
  // src/vector/vector-index/build.ts
1468
+ function bufferIfTheGraphLacks(state, graph, docId) {
1469
+ const ordinal = state.store.getOrdinal(docId);
1470
+ if (ordinal !== void 0 && graph.holdsNode(ordinal) && !graph.holdsTombstone(ordinal)) moveOutOfBuffer(state, docId);
1471
+ else state.buffer.add(docId);
1472
+ }
1250
1473
  function bufferWhatTheGraphLacks(state, graph) {
1251
- for (const docId of allLiveDocIds(state)) {
1252
- const ordinal = state.store.getOrdinal(docId);
1253
- if (ordinal !== void 0 && graph.holdsNode(ordinal) && !graph.holdsTombstone(ordinal)) state.buffer.delete(docId);
1254
- else state.buffer.add(docId);
1255
- }
1474
+ for (const docId of allLiveDocIds(state)) bufferIfTheGraphLacks(state, graph, docId);
1475
+ }
1476
+ async function bufferWhatTheGraphLacksInTurns(state, graph) {
1477
+ const visit = (docId) => bufferIfTheGraphLacks(state, graph, docId);
1478
+ await forEachInTurns(allLiveDocIds(state), visit, () => !state.disposed && state.hnsw === graph);
1256
1479
  }
1257
1480
  async function buildGraphFromStore(state) {
1258
1481
  const previous = state.hnsw;
1482
+ const order = turnsAreBudgeted ? await shuffledLiveDocIdsInTurns(state) : shuffleInPlace([...allLiveDocIds(state)]);
1483
+ if (order === null || state.hnsw !== previous) return false;
1259
1484
  const graph = createHNSWIndex(state.dimension, state.store, state.hnswConfig, state.osq ?? void 0);
1260
1485
  state.freshGraph = graph;
1261
1486
  let completed = false;
1262
1487
  try {
1263
- completed = await insertIntoGraph(state, graph, shuffleInPlace([...allLiveDocIds(state)]), () => true);
1488
+ completed = await insertIntoGraph(state, graph, order, () => true);
1264
1489
  } finally {
1265
1490
  state.freshGraph = null;
1266
1491
  }
@@ -1271,7 +1496,8 @@ async function buildGraphFromStore(state) {
1271
1496
  adoptGraph(state, graph);
1272
1497
  state.shuffledBuildCount = graph.size;
1273
1498
  noteWrite(state);
1274
- bufferWhatTheGraphLacks(state, graph);
1499
+ if (turnsAreBudgeted) await bufferWhatTheGraphLacksInTurns(state, graph);
1500
+ else bufferWhatTheGraphLacks(state, graph);
1275
1501
  if (previous !== null) dropSharedGraph(state, previous);
1276
1502
  await releasePendingLocations(state);
1277
1503
  return true;
@@ -1375,7 +1601,7 @@ async function growGraph(state, graph, bufferSnapshot) {
1375
1601
  function triggerBuild(state) {
1376
1602
  if (state.building) return;
1377
1603
  state.building = true;
1378
- const bufferSnapshot = new Set(state.buffer);
1604
+ const bufferSnapshot = turnsAreBudgeted ? null : new Set(state.buffer);
1379
1605
  const existingGraph = state.hnsw;
1380
1606
  let settled = false;
1381
1607
  let timedOut = false;
@@ -1383,7 +1609,9 @@ function triggerBuild(state) {
1383
1609
  try {
1384
1610
  if (state.disposed) return;
1385
1611
  if (existingGraph !== null) {
1386
- await growGraph(state, existingGraph, bufferSnapshot);
1612
+ const snapshot = bufferSnapshot ?? await copyInTurns(state.buffer, () => !state.disposed && state.hnsw === existingGraph);
1613
+ if (snapshot === null) return;
1614
+ await growGraph(state, existingGraph, snapshot);
1387
1615
  return;
1388
1616
  }
1389
1617
  await promoteToGraph(state);
@@ -1458,7 +1686,8 @@ function scheduleBuild(state) {
1458
1686
  if (state.building || state.buildScheduled || state.disposed) return;
1459
1687
  if (!buildDue(state)) return;
1460
1688
  state.buildScheduled = true;
1461
- void yieldBuildTurn(state).then(() => {
1689
+ const turn = turnsAreBudgeted ? yieldBackgroundTurn() : yieldBuildTurn(state);
1690
+ void turn.then(() => {
1462
1691
  state.buildScheduled = false;
1463
1692
  triggerBuild(state);
1464
1693
  });
@@ -1884,26 +2113,35 @@ async function coldSearchConcurrently(state, hold, query, k, options) {
1884
2113
  }
1885
2114
 
1886
2115
  // src/vector/vector-index/flagged-documents.ts
1887
- var FlaggedDocuments = class extends Set {
2116
+ var FlaggedDocuments = class {
1888
2117
  constructor(store, layout) {
1889
- super();
1890
2118
  this.store = store;
1891
2119
  this.layout = layout;
1892
2120
  }
1893
- ordinals = /* @__PURE__ */ new Map();
2121
+ members = createDocIdMap();
2122
+ ordinals = createDocIdMap();
1894
2123
  handles = null;
1895
2124
  flags = new Uint8Array(0);
1896
2125
  chunks = null;
2126
+ get size() {
2127
+ return this.members.size;
2128
+ }
2129
+ has(docId) {
2130
+ return this.members.has(docId);
2131
+ }
2132
+ [Symbol.iterator]() {
2133
+ return this.members.keys()[Symbol.iterator]();
2134
+ }
1897
2135
  add(docId) {
1898
- super.add(docId);
2136
+ this.members.set(docId, true);
1899
2137
  const handles = this.openHandles();
1900
2138
  if (handles === null) return this;
1901
2139
  this.flagCurrentOrdinal(docId);
1902
- Atomics.store(handles.header, this.layout.countWord, this.size);
2140
+ Atomics.store(handles.header, this.layout.countWord, this.members.size);
1903
2141
  return this;
1904
2142
  }
1905
2143
  delete(docId) {
1906
- const held = super.delete(docId);
2144
+ const held = this.members.delete(docId);
1907
2145
  const handles = this.openHandles();
1908
2146
  if (handles === null) return held;
1909
2147
  const ordinal = this.ordinals.get(docId);
@@ -1911,11 +2149,11 @@ var FlaggedDocuments = class extends Set {
1911
2149
  this.ordinals.delete(docId);
1912
2150
  this.unflag(ordinal);
1913
2151
  }
1914
- Atomics.store(handles.header, this.layout.countWord, this.size);
2152
+ Atomics.store(handles.header, this.layout.countWord, this.members.size);
1915
2153
  return held;
1916
2154
  }
1917
2155
  clear() {
1918
- super.clear();
2156
+ this.members.clear();
1919
2157
  const handles = this.openHandles();
1920
2158
  if (handles !== null) {
1921
2159
  for (const ordinal of this.ordinals.values()) this.unflag(ordinal);
@@ -1945,7 +2183,7 @@ var FlaggedDocuments = class extends Set {
1945
2183
  const chunksOf = this.layout.chunksOf;
1946
2184
  this.chunks = chunksOf === null ? null : new Int32Array(chunksOf(handles));
1947
2185
  this.ordinals.clear();
1948
- for (const docId of this) this.flagCurrentOrdinal(docId);
2186
+ for (const docId of this.members.keys()) this.flagCurrentOrdinal(docId);
1949
2187
  return handles;
1950
2188
  }
1951
2189
  };
@@ -2090,46 +2328,64 @@ function fetchedOutcome(state, results, minSimilarity, filter) {
2090
2328
  function scannedOutcome(scan) {
2091
2329
  return { results: scan.results, matched: scan.matched, matchedExact: true };
2092
2330
  }
2093
- function liveCandidates(state, candidates) {
2331
+ function liveOrdinalOf(state, docId, filter) {
2332
+ if (state.tombstones.has(docId)) return void 0;
2333
+ const ordinal = state.store.getOrdinal(docId);
2334
+ if (ordinal === void 0 || filter !== void 0 && !ordinalFilterHas(filter, ordinal)) return void 0;
2335
+ return ordinal;
2336
+ }
2337
+ function liveCandidates(state, candidates, filter) {
2094
2338
  const docIds = [];
2095
2339
  const ordinals = [];
2096
2340
  for (const docId of candidates) {
2097
- if (state.tombstones.has(docId)) continue;
2098
- const ordinal = state.store.getOrdinal(docId);
2341
+ const ordinal = liveOrdinalOf(state, docId, filter);
2099
2342
  if (ordinal === void 0) continue;
2100
2343
  docIds.push(docId);
2101
2344
  ordinals.push(ordinal);
2102
2345
  }
2103
2346
  return { docIds, ordinals };
2104
2347
  }
2105
- function scanThroughTheCore(state, query, k, metric, minSimilarity, scanned) {
2106
- const distances = nativeScoresOf(state.store.handles, query, metric, scanned.ordinals);
2107
- if (distances === null) return null;
2108
- const heap = createBoundedMaxHeap(highScoreFirst, k);
2109
- let matched = 0;
2110
- for (let i = 0; i < scanned.ordinals.length; i++) {
2111
- if (distances[i] === Number.POSITIVE_INFINITY) continue;
2112
- const score = toScore(distances[i], metric);
2113
- if (score < minSimilarity) continue;
2114
- matched++;
2115
- heap.push({ docId: scanned.docIds[i], score });
2116
- }
2117
- return { results: heap.toSortedArray().reverse(), matched };
2348
+ function createScanTally(capacity) {
2349
+ return {
2350
+ best: createBoundedMaxHeap(highScoreFirst, capacity),
2351
+ capacity,
2352
+ matched: 0,
2353
+ bestDiscardedScore: Number.NEGATIVE_INFINITY
2354
+ };
2118
2355
  }
2119
- function bruteForceSearch(state, query, k, metric, minSimilarity, candidates) {
2120
- const scanned = liveCandidates(state, candidates);
2121
- const fromTheCore = scanThroughTheCore(state, query, k, metric, minSimilarity, scanned);
2122
- if (fromTheCore !== null) return fromTheCore;
2356
+ function admitScore(tally, docId, score) {
2357
+ tally.matched++;
2358
+ const worst = tally.best.size < tally.capacity ? void 0 : tally.best.peek();
2359
+ if (worst !== void 0 && score < worst.score) {
2360
+ if (score > tally.bestDiscardedScore) tally.bestDiscardedScore = score;
2361
+ return;
2362
+ }
2363
+ const result = { docId, score };
2364
+ tally.best.push(result);
2365
+ if (worst === void 0) return;
2366
+ const discarded = highScoreFirst(result, worst) < 0 ? worst.score : score;
2367
+ if (discarded > tally.bestDiscardedScore) tally.bestDiscardedScore = discarded;
2368
+ }
2369
+ function scoreCandidates2(state, query, metric, minSimilarity, docIds, ordinals, tally) {
2370
+ const count = ordinals.length;
2371
+ const distances = nativeScoresOf(state.store.handles, query, metric, ordinals);
2372
+ if (distances !== null) {
2373
+ for (let i = 0; i < count; i++) {
2374
+ if (distances[i] === Number.POSITIVE_INFINITY) continue;
2375
+ const score = toScore(distances[i], metric);
2376
+ if (score < minSimilarity) continue;
2377
+ admitScore(tally, docIds[i], score);
2378
+ }
2379
+ return;
2380
+ }
2123
2381
  noteFallback("score");
2124
2382
  const arenaQuery = state.store.prepareQueryArena(query);
2125
2383
  const queryMag = arenaQuery ? arenaQuery.magnitude : magnitude(query);
2126
- const heap = createBoundedMaxHeap(highScoreFirst, k);
2127
- let matched = 0;
2128
- for (let i = 0; i < scanned.docIds.length; i++) {
2129
- const docId = scanned.docIds[i];
2384
+ for (let i = 0; i < count; i++) {
2385
+ const docId = docIds[i];
2130
2386
  let score;
2131
2387
  if (arenaQuery) {
2132
- const distance = state.store.distanceFromArena(arenaQuery, scanned.ordinals[i], metric);
2388
+ const distance = state.store.distanceFromArena(arenaQuery, ordinals[i], metric);
2133
2389
  if (distance === Number.POSITIVE_INFINITY) continue;
2134
2390
  score = toScore(distance, metric);
2135
2391
  } else {
@@ -2150,16 +2406,29 @@ function bruteForceSearch(state, query, k, metric, minSimilarity, candidates) {
2150
2406
  }
2151
2407
  }
2152
2408
  if (score < minSimilarity) continue;
2153
- matched++;
2154
- heap.push({ docId, score });
2409
+ admitScore(tally, docId, score);
2155
2410
  }
2156
- return { results: heap.toSortedArray().reverse(), matched };
2411
+ }
2412
+ function bruteForceSearch(state, query, k, metric, minSimilarity, candidates, filter) {
2413
+ const scanned = liveCandidates(state, candidates, filter);
2414
+ const tally = createScanTally(k);
2415
+ scoreCandidates2(state, query, metric, minSimilarity, scanned.docIds, scanned.ordinals, tally);
2416
+ return { results: tally.best.toSortedArray().reverse(), matched: tally.matched };
2157
2417
  }
2158
2418
  function search(state, query, k, options) {
2159
2419
  handOverBuildTurns(state);
2160
2420
  return searchWithFilter(state, query, k, options, filterForOptions(state, options));
2161
2421
  }
2162
2422
  function searchWithFilter(state, query, k, options, filter) {
2423
+ const steps = searchSteps(state, query, k, options, filter);
2424
+ let step = steps.next();
2425
+ while (step.done !== true) {
2426
+ const { candidates, filter: remaining } = step.value;
2427
+ step = steps.next(bruteForceSearch(state, query, k, options.metric, options.minSimilarity, candidates, remaining));
2428
+ }
2429
+ return step.value;
2430
+ }
2431
+ function* searchSteps(state, query, k, options, filter) {
2163
2432
  if (query.length !== state.dimension) {
2164
2433
  throw new NarsilError(
2165
2434
  ErrorCodes.VECTOR_DIMENSION_MISMATCH,
@@ -2168,7 +2437,7 @@ function searchWithFilter(state, query, k, options, filter) {
2168
2437
  );
2169
2438
  }
2170
2439
  const currentLiveSize = liveSize(state);
2171
- const { metric, minSimilarity, efSearch, oversample, rescoreWindow } = options;
2440
+ const { minSimilarity } = options;
2172
2441
  if (currentLiveSize === 0) return { results: [], matched: 0, matchedExact: true };
2173
2442
  if (filter && filter.count === 0) return { results: [], matched: 0, matchedExact: true };
2174
2443
  if (k <= 0) return fetchedOutcome(state, [], minSimilarity, filter);
@@ -2177,26 +2446,233 @@ function searchWithFilter(state, query, k, options, filter) {
2177
2446
  }
2178
2447
  if (!state.hnsw) {
2179
2448
  const candidates = filter ? filteredDocIds(state, filter) : allLiveDocIds(state);
2180
- return scannedOutcome(bruteForceSearch(state, query, k, metric, minSimilarity, candidates));
2449
+ return scannedOutcome(yield { candidates, filter: void 0, graphResults: null });
2181
2450
  }
2182
2451
  if (filter) {
2183
2452
  const hnswLiveSize = state.hnsw.size;
2184
2453
  const selectivity = hnswLiveSize > 0 ? filter.count / hnswLiveSize : 1;
2185
2454
  if (selectivity < state.filterThreshold) {
2186
- const scan = bruteForceSearch(state, query, k, metric, minSimilarity, filteredDocIds(state, filter));
2187
- return scannedOutcome(scan);
2455
+ return scannedOutcome(yield { candidates: filteredDocIds(state, filter), filter: void 0, graphResults: null });
2188
2456
  }
2189
2457
  }
2190
- const hnswResults = state.hnsw.search(query, k, metric, minSimilarity, { filter, efSearch, oversample, rescoreWindow }).map((r) => ({ docId: r.docId, score: r.score }));
2191
- const results = state.buffer.size === 0 ? hnswResults : withBufferedVectors(state, query, k, options, filter, hnswResults, state.buffer);
2192
- return fetchedOutcome(state, results, minSimilarity, filter);
2458
+ const hnswResults = graphResultsOf(state, query, k, options, filter);
2459
+ if (state.buffer.size === 0) return fetchedOutcome(state, hnswResults, minSimilarity, filter);
2460
+ const scan = yield { candidates: state.buffer, filter, graphResults: hnswResults };
2461
+ return fetchedOutcome(state, mergeResults(hnswResults, scan.results, k), minSimilarity, filter);
2462
+ }
2463
+ function graphResultsOf(state, query, k, options, filter) {
2464
+ if (state.hnsw === null) return [];
2465
+ const { metric, minSimilarity, efSearch, oversample, rescoreWindow } = options;
2466
+ return state.hnsw.search(query, k, metric, minSimilarity, { filter, efSearch, oversample, rescoreWindow }).map((r) => ({ docId: r.docId, score: r.score }));
2193
2467
  }
2194
2468
  function withBufferedVectors(state, query, k, options, filter, graphResults, buffered) {
2195
- const candidates = bufferCandidates(state, buffered, filter);
2196
- const scan = bruteForceSearch(state, query, k, options.metric, options.minSimilarity, candidates);
2469
+ const scan = bruteForceSearch(state, query, k, options.metric, options.minSimilarity, buffered, filter);
2197
2470
  return mergeResults(graphResults, scan.results, k);
2198
2471
  }
2199
2472
 
2473
+ // src/vector/vector-index/search-in-turns.ts
2474
+ function layoutGeneration(state) {
2475
+ return Atomics.load(state.store.handles.header, STORE_ORDINAL_LAYOUT_GENERATION);
2476
+ }
2477
+ async function scanInTurns(state, query, capacity, options, request, chunkOfThisSearch) {
2478
+ const tally = createScanTally(capacity);
2479
+ const { docIds, ordinals } = chunkOfThisSearch;
2480
+ let length = 0;
2481
+ const scoreChunk = () => {
2482
+ const chunk = length === SEARCH_SCAN_CHUNK_VECTORS ? ordinals : ordinals.subarray(0, length);
2483
+ scoreCandidates2(state, query, options.metric, options.minSimilarity, docIds, chunk, tally);
2484
+ length = 0;
2485
+ };
2486
+ for (const docId of request.candidates) {
2487
+ const ordinal = liveOrdinalOf(state, docId, request.filter);
2488
+ if (ordinal === void 0) continue;
2489
+ docIds[length] = docId;
2490
+ ordinals[length] = ordinal;
2491
+ length++;
2492
+ if (length < SEARCH_SCAN_CHUNK_VECTORS) continue;
2493
+ scoreChunk();
2494
+ if (turnIsSpent()) await yieldTurn();
2495
+ }
2496
+ if (length > 0) scoreChunk();
2497
+ return {
2498
+ kept: tally.best.toSortedArray().reverse(),
2499
+ matched: tally.matched,
2500
+ bestDiscardedScore: tally.bestDiscardedScore
2501
+ };
2502
+ }
2503
+ async function passInTurns(state, query, k, options, filter, chunk) {
2504
+ const steps = searchSteps(state, query, k, options, filter);
2505
+ let graphResults = null;
2506
+ let scan = null;
2507
+ let step = steps.next();
2508
+ while (step.done !== true) {
2509
+ graphResults = step.value.graphResults;
2510
+ scan = await scanInTurns(state, query, k + SEARCH_REFILL_RESERVE, options, step.value, chunk);
2511
+ step = steps.next({ results: scan.kept.slice(0, k), matched: scan.matched });
2512
+ }
2513
+ return { outcome: step.value, graphResults, scan };
2514
+ }
2515
+ function rescoredSinceTheScan(state, query, k, options, filter, watch) {
2516
+ const touched = new Set(watch.written);
2517
+ for (const docId of watch.leftBuffer) touched.add(docId);
2518
+ return bruteForceSearch(state, query, k, options.metric, options.minSimilarity, touched, filter).results;
2519
+ }
2520
+ function outrunsEveryDiscard(results, k, bestDiscardedScore) {
2521
+ if (bestDiscardedScore === Number.NEGATIVE_INFINITY) return true;
2522
+ const lowest = results[k - 1];
2523
+ return lowest !== void 0 && lowest.score > bestDiscardedScore;
2524
+ }
2525
+ function settledOutcome(state, query, k, options, pass, watch) {
2526
+ const { written, leftBuffer } = watch;
2527
+ const { scan, graphResults } = pass;
2528
+ if (scan === null || written.size === 0 && leftBuffer.size === 0) return pass.outcome;
2529
+ if (graphResults === null) return written.size === 0 ? pass.outcome : null;
2530
+ const filter = filterForOptions(state, options);
2531
+ const graphTouched = graphResults.some((result) => written.has(result.docId));
2532
+ const graphPart = graphTouched ? graphResultsOf(state, query, k, options, filter) : graphResults;
2533
+ const survivors = scan.kept.filter((result) => !written.has(result.docId));
2534
+ const rescored = rescoredSinceTheScan(state, query, k, options, filter, watch);
2535
+ const results = mergeResults(graphPart, mergeResults(survivors, rescored, k), k);
2536
+ const refilled = survivors.length < scan.kept.length;
2537
+ if (refilled && !outrunsEveryDiscard(results, k, scan.bestDiscardedScore)) return null;
2538
+ return fetchedOutcome(state, results, options.minSimilarity, filter);
2539
+ }
2540
+ function attemptInTurns(state, query, k, options, filter, chunk) {
2541
+ const generation = liveSize(state) === 0 ? null : layoutGeneration(state);
2542
+ return watchingSearch(state, async (watch) => {
2543
+ const pass = await passInTurns(state, query, k, options, filter, chunk);
2544
+ if (state.disposed) return pass.outcome;
2545
+ if (generation !== null && layoutGeneration(state) !== generation) return null;
2546
+ return settledOutcome(state, query, k, options, pass, watch);
2547
+ });
2548
+ }
2549
+ async function searchWithFilterInTurns(state, query, k, options, filter) {
2550
+ let current = filter;
2551
+ const chunk = { docIds: [], ordinals: new Int32Array(SEARCH_SCAN_CHUNK_VECTORS) };
2552
+ for (let attempt = 1; ; attempt++) {
2553
+ const search2 = () => attemptInTurns(state, query, k, options, current, chunk);
2554
+ const outcome = attempt <= SEARCH_ATTEMPTS_BEFORE_HOLDING_WRITES ? await search2() : await holdWritesAndBackgroundWork(search2);
2555
+ if (outcome !== null) return outcome;
2556
+ current = filterForOptions(state, options);
2557
+ }
2558
+ }
2559
+ function searchFiltered(state, query, k, options, filter) {
2560
+ if (turnsAreBudgeted) return searchWithFilterInTurns(state, query, k, options, filter);
2561
+ return searchWithFilter(state, query, k, options, filter);
2562
+ }
2563
+
2564
+ // src/vector/vector-index/search-off-thread.ts
2565
+ function currentFilter(search2) {
2566
+ const { state, options, filter, confinedAtRevision } = search2;
2567
+ if (confinedAtRevision === null || state.revision === confinedAtRevision) return filter;
2568
+ return filterForOptions(state, options);
2569
+ }
2570
+ async function scoredChunk(search2, metric, ordinals, count) {
2571
+ const store = nativeStoreFor(search2.state.store.handles);
2572
+ if (store === null) return null;
2573
+ try {
2574
+ const scored = await withNativeThreadSlot(
2575
+ (slot) => search2.core.scoreAsync(store.handle, search2.query, nativeMetricCode(metric), ordinals, count, slot)
2576
+ );
2577
+ return scored.status === NATIVE_OK ? scored.distances : null;
2578
+ } catch {
2579
+ return null;
2580
+ }
2581
+ }
2582
+ async function scanOffThread(search2, candidates) {
2583
+ const { state, k, options } = search2;
2584
+ const { metric, minSimilarity } = options;
2585
+ const heap = createBoundedMaxHeap(highScoreFirst, k);
2586
+ let matched = 0;
2587
+ const docIds = [];
2588
+ const ordinals = new Int32Array(NATIVE_THREAD_SCAN_CHUNK_ORDINALS);
2589
+ const remaining = candidates[Symbol.iterator]();
2590
+ let exhausted = false;
2591
+ while (!exhausted) {
2592
+ docIds.length = 0;
2593
+ while (docIds.length < NATIVE_THREAD_SCAN_CHUNK_ORDINALS) {
2594
+ if (turnIsSpent()) await yieldTurn();
2595
+ const next = remaining.next();
2596
+ if (next.done === true) {
2597
+ exhausted = true;
2598
+ break;
2599
+ }
2600
+ if (state.tombstones.has(next.value)) continue;
2601
+ const ordinal = state.store.getOrdinal(next.value);
2602
+ if (ordinal === void 0) continue;
2603
+ ordinals[docIds.length] = ordinal;
2604
+ docIds.push(next.value);
2605
+ }
2606
+ if (docIds.length === 0) break;
2607
+ const distances = await scoredChunk(search2, metric, ordinals, docIds.length);
2608
+ if (distances === null) return null;
2609
+ for (let i = 0; i < docIds.length; i++) {
2610
+ if (distances[i] === Number.POSITIVE_INFINITY) continue;
2611
+ const score = toScore(distances[i], metric);
2612
+ if (score < minSimilarity) continue;
2613
+ matched++;
2614
+ heap.push({ docId: docIds[i], score });
2615
+ }
2616
+ }
2617
+ return { results: heap.toSortedArray().reverse(), matched };
2618
+ }
2619
+ function scansEveryCandidate(state, filter) {
2620
+ if (state.hnsw === null) return true;
2621
+ if (filter === void 0) return false;
2622
+ const graphSize = state.hnsw.size;
2623
+ const selectivity = graphSize > 0 ? filter.count / graphSize : 1;
2624
+ return selectivity < state.filterThreshold;
2625
+ }
2626
+ async function outcomeOffThread(search2) {
2627
+ const { state, query, k, options, filter } = search2;
2628
+ const { metric, minSimilarity, efSearch, oversample, rescoreWindow } = options;
2629
+ const graph = state.hnsw;
2630
+ if (graph === null || scansEveryCandidate(state, filter)) {
2631
+ const candidates = filter ? filteredDocIds(state, filter) : allLiveDocIds(state);
2632
+ const scan2 = await scanOffThread(search2, candidates);
2633
+ return scan2 === null ? null : { results: scan2.results, matched: scan2.matched, matchedExact: true };
2634
+ }
2635
+ const buffered = state.buffer.size === 0 ? null : [...state.buffer];
2636
+ const hits = await graph.searchOffThread(query, k, metric, minSimilarity, {
2637
+ filter,
2638
+ efSearch,
2639
+ oversample,
2640
+ rescoreWindow
2641
+ });
2642
+ if (hits === null) return null;
2643
+ const graphResults = hits.map((hit) => ({ docId: hit.docId, score: hit.score }));
2644
+ if (buffered === null) return fetchedOutcome(state, graphResults, minSimilarity, currentFilter(search2));
2645
+ const scan = await scanOffThread(search2, bufferCandidates(state, buffered, filter));
2646
+ if (scan === null) return null;
2647
+ return fetchedOutcome(state, mergeResults(graphResults, scan.results, k), minSimilarity, currentFilter(search2));
2648
+ }
2649
+ async function searchOnNativeThreads(search2) {
2650
+ const { state, query, k, options, filter } = search2;
2651
+ if (query.length !== state.dimension) {
2652
+ throw new NarsilError(
2653
+ ErrorCodes.VECTOR_DIMENSION_MISMATCH,
2654
+ `Vector dimension mismatch: expected ${state.dimension}, got ${query.length}`,
2655
+ { expected: state.dimension, received: query.length }
2656
+ );
2657
+ }
2658
+ if (liveSize(state) === 0) return { results: [], matched: 0, matchedExact: true };
2659
+ if (filter && filter.count === 0) return { results: [], matched: 0, matchedExact: true };
2660
+ if (k <= 0) return fetchedOutcome(state, [], options.minSimilarity, filter);
2661
+ if (state.buffer.size > 0 && !state.building && !state.buildScheduled) scheduleBuild(state);
2662
+ const generation = layoutGeneration(state);
2663
+ const settled = await watchingSearch(state, async (watch) => {
2664
+ const outcome = await outcomeOffThread(search2);
2665
+ if (outcome === null || watch.written.size > 0 || layoutGeneration(state) !== generation) return null;
2666
+ return outcome;
2667
+ });
2668
+ return settled ?? searchFiltered(state, query, k, options, filterForOptions(state, options));
2669
+ }
2670
+ function searchOffThread(state, query, k, options, filter, confinedAtRevision) {
2671
+ const core = threadedNativeCore();
2672
+ if (core === null) return null;
2673
+ return searchOnNativeThreads({ state, core, query, k, options, filter, confinedAtRevision });
2674
+ }
2675
+
2200
2676
  // src/vector/vector-index/writes.ts
2201
2677
  function createVectorWrites(state) {
2202
2678
  function validateDimension(vector) {
@@ -2228,17 +2704,23 @@ function createVectorWrites(state) {
2228
2704
  if (previous !== void 0 && holdsThisVectorAlready(docId, previous, vector)) {
2229
2705
  if (partitionId !== void 0 && state.store.partitionOfOrdinal(previous) !== partitionId) {
2230
2706
  noteWrite(state);
2707
+ noteSearchedDocWritten(state, docId);
2231
2708
  state.store.setPartition(docId, partitionId);
2232
2709
  }
2233
2710
  return;
2234
2711
  }
2235
2712
  noteWrite(state);
2713
+ noteSearchedDocWritten(state, docId);
2236
2714
  state.tombstones.delete(docId);
2237
2715
  const graphHoldsPrevious = previous !== void 0 && ((state.hnsw?.holdsNode(previous) ?? false) || (state.freshGraph?.holdsNode(previous) ?? false));
2238
2716
  state.store.insert(docId, vector, partitionId, graphHoldsPrevious);
2239
2717
  if (previous !== void 0) {
2240
- state.hnsw?.markTombstoneOrdinal(previous);
2241
- state.freshGraph?.markTombstoneOrdinal(previous);
2718
+ const graph = state.hnsw;
2719
+ const freshGraph = state.freshGraph;
2720
+ afterGraphPass(state, () => {
2721
+ graph?.markTombstoneOrdinal(previous);
2722
+ freshGraph?.markTombstoneOrdinal(previous);
2723
+ });
2242
2724
  if (!graphHoldsPrevious) state.osq?.removeOrdinal(previous);
2243
2725
  }
2244
2726
  state.buffer.add(docId);
@@ -2249,12 +2731,13 @@ function createVectorWrites(state) {
2249
2731
  handOverBuildTurns(state);
2250
2732
  if (!state.store.has(docId)) return;
2251
2733
  noteWrite(state);
2734
+ noteSearchedDocWritten(state, docId);
2252
2735
  state.tombstones.add(docId);
2253
2736
  state.buffer.delete(docId);
2254
2737
  state.pendingLocations.delete(docId);
2255
- if (state.hnsw) {
2256
- state.hnsw.markTombstone(docId);
2257
- }
2738
+ const graph = state.hnsw;
2739
+ const ordinal = state.store.getOrdinal(docId);
2740
+ if (graph !== null && ordinal !== void 0) afterGraphPass(state, () => graph.markTombstoneOrdinal(ordinal));
2258
2741
  }
2259
2742
  return {
2260
2743
  insert(docId, vector, partitionId) {
@@ -2353,7 +2836,9 @@ function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_W
2353
2836
  sharedLayoutRevision: 0,
2354
2837
  sharing: Promise.resolve(),
2355
2838
  releaseToFilesInFlight: Promise.resolve(),
2356
- cold: null
2839
+ cold: null,
2840
+ graphsHeldOffThread: null,
2841
+ searchWatches: /* @__PURE__ */ new Set()
2357
2842
  };
2358
2843
  const { insert, remove } = createVectorWrites(state);
2359
2844
  function getVector(docId) {
@@ -2371,22 +2856,24 @@ function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_W
2371
2856
  while (state.cold !== null) await state.cold.loaded;
2372
2857
  }
2373
2858
  async function searchParallel(query, k, options) {
2859
+ if (state.graphsHeldOffThread !== null) await untilGraphReleased(state);
2374
2860
  if (state.cold !== null) return coldSearchConcurrently(state, state.cold, query, k, options);
2375
2861
  handOverBuildTurns(state);
2376
2862
  const confined = options.filterSubsets !== void 0 || options.filterPartitions !== void 0;
2377
2863
  let filter = filterForOptions(state, options);
2864
+ const offThread = searchOffThread(state, query, k, options, filter, confined ? state.revision : null);
2865
+ if (offThread !== null) return offThread;
2378
2866
  if (filter !== void 0) {
2379
2867
  if (state.hnsw) {
2380
2868
  const hnswLiveSize = state.hnsw.size;
2381
2869
  const selectivity = hnswLiveSize > 0 ? filter.count / hnswLiveSize : 1;
2382
- if (selectivity < state.filterThreshold) {
2383
- return searchWithFilter(state, query, k, options, filter);
2384
- }
2870
+ if (selectivity < state.filterThreshold) return searchFiltered(state, query, k, options, filter);
2385
2871
  }
2386
2872
  }
2387
2873
  const filterRevision = state.revision;
2388
- const buffered = state.buffer.size === 0 ? null : [...state.buffer];
2389
2874
  scheduleWorkerCopyLoad(state);
2875
+ const searchesWorkerCopies = state.workerCopyPool !== null && state.workerCopyHandle !== null;
2876
+ const buffered = state.buffer.size === 0 || !searchesWorkerCopies ? null : [...state.buffer];
2390
2877
  const viaWorkerCopy = await searchViaWorkerCopies(state, query, k, options.metric, options.minSimilarity, {
2391
2878
  filter,
2392
2879
  efSearch: options.efSearch,
@@ -2400,7 +2887,7 @@ function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_W
2400
2887
  const results = buffered === null ? viaWorkerCopy : withBufferedVectors(state, query, k, options, filter, viaWorkerCopy, buffered);
2401
2888
  return fetchedOutcome(state, results, options.minSimilarity, filter);
2402
2889
  }
2403
- return searchWithFilter(state, query, k, options, filter);
2890
+ return searchFiltered(state, query, k, options, filter);
2404
2891
  }
2405
2892
  return {
2406
2893
  get size() {
@@ -2442,18 +2929,21 @@ function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_W
2442
2929
  cancelColdHold(state);
2443
2930
  disposeIndex(state);
2444
2931
  },
2445
- search: (query, k, options) => state.cold === null ? search(state, query, k, options) : coldSearch(state, state.cold, query, k, options),
2932
+ search: withGraphFree(
2933
+ state,
2934
+ (query, k, options) => state.cold === null ? search(state, query, k, options) : coldSearch(state, state.cold, query, k, options)
2935
+ ),
2446
2936
  searchParallel,
2447
2937
  refreshWorkerCopies: () => {
2448
2938
  if (state.cold === null) refreshWorkerCopies(state);
2449
2939
  },
2450
2940
  getVector,
2451
2941
  has,
2452
- compact: () => {
2942
+ compact: withGraphFree(state, () => {
2453
2943
  if (state.cold !== null) return;
2454
2944
  noteWrite(state);
2455
2945
  compact(state);
2456
- },
2946
+ }),
2457
2947
  optimize: async () => {
2458
2948
  await whenLoaded();
2459
2949
  noteWrite(state);
@@ -2462,18 +2952,21 @@ function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_W
2462
2952
  maintenanceStatus: () => ({ ...maintenanceStatus(state), loadingFromFiles: state.cold !== null }),
2463
2953
  estimateMemoryBytes: () => estimateMemoryBytes(state) + (state.cold?.field.residentBytes ?? 0),
2464
2954
  growthBytesFor: (vectors) => state.store.growthBytesFor(vectors),
2465
- deserialize: (parts) => {
2955
+ deserialize: withGraphFree(state, (parts) => {
2466
2956
  cancelColdHold(state);
2467
2957
  invalidateWorkerCopies(state);
2468
2958
  deserialize(state, parts);
2469
- },
2470
- planCheckpoint: (listedKeys, rewritesGraph, pinsRetiredVectors) => planCheckpoint(state, listedKeys, rewritesGraph, pinsRetiredVectors),
2959
+ }),
2960
+ planCheckpoint: withGraphFree(
2961
+ state,
2962
+ (listedKeys, rewritesGraph, pinsRetiredVectors) => planCheckpoint(state, listedKeys, rewritesGraph, pinsRetiredVectors)
2963
+ ),
2471
2964
  recordCheckpoint: (plan, written) => recordCheckpoint(state, plan, written),
2472
- restoreCheckpoint: (shape) => {
2965
+ restoreCheckpoint: withGraphFree(state, (shape) => {
2473
2966
  cancelColdHold(state);
2474
2967
  invalidateWorkerCopies(state);
2475
2968
  return beginCheckpointRestore(state, shape);
2476
- },
2969
+ }),
2477
2970
  openColdField: (field, load) => openColdField(state, field, load),
2478
2971
  startShuffledRebuildWhenDue: () => {
2479
2972
  if (state.cold === null) startShuffledRebuild(state);
@@ -2482,12 +2975,12 @@ function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_W
2482
2975
  const stored = state.store.dimension === 0 ? [] : state.store.handles.vectorFiles.filter((path) => path !== "");
2483
2976
  return state.cold === null ? stored : [...stored, ...state.cold.field.filePaths()];
2484
2977
  },
2485
- adoptDiskLayout: (layout) => adoptDiskLayout(state, layout),
2486
- releaseVectorFiles: async () => {
2978
+ adoptDiskLayout: onceGraphFree(state, (layout) => adoptDiskLayout(state, layout)),
2979
+ releaseVectorFiles: onceGraphFree(state, async () => {
2487
2980
  await whenLoaded();
2488
2981
  await withdrawWorkerCopies(state);
2489
2982
  state.store.closeFiles();
2490
- },
2983
+ }),
2491
2984
  relocateVectorFiles: (rewrite) => {
2492
2985
  state.store.relocateVectorFiles(rewrite);
2493
2986
  for (const [docId, location] of state.pendingLocations) {
@@ -2504,4 +2997,4 @@ function createVectorIndex(fieldName, dimension, config, workerCopies = VECTOR_W
2504
2997
  };
2505
2998
  }
2506
2999
 
2507
- export { createSharedVectorSearcher, createVectorIndex };
3000
+ export { compactVectors, createSharedVectorSearcher, createVectorIndex, getVectorMaintenanceStatus, optimizeVectors };